CGBuiltin.cpp revision 79dcf5f96ad75259867d19bbf69512f320032fce
1022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// 2022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson// 3022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson// The LLVM Compiler Infrastructure 4022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 7022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson// 8022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson//===----------------------------------------------------------------------===// 9022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson// 10022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson// This contains code to emit Builtin calls as LLVM code. 11022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson// 12022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson//===----------------------------------------------------------------------===// 13022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson 14022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson#include "CodeGenFunction.h" 15022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson#include "CodeGenModule.h" 16ca6fcfad547dcec3fdd17790b4fab0918df74b37Anders Carlsson#include "clang/Basic/TargetInfo.h" 171f32999ec79a980576e100d64d5f3267eb19ea49Chris Lattner#include "clang/AST/APValue.h" 18bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "clang/AST/ASTContext.h" 19c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/Decl.h" 20564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson#include "clang/AST/TargetBuiltins.h" 21793680ed8104bf088d1b382b963a8badcb3f07deAnders Carlsson#include "llvm/Intrinsics.h" 22022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlssonusing namespace clang; 23022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlssonusing namespace CodeGen; 24ca6fcfad547dcec3fdd17790b4fab0918df74b37Anders Carlssonusing namespace llvm; 25ca6fcfad547dcec3fdd17790b4fab0918df74b37Anders Carlsson 260002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar/// Utility to insert an atomic instruction based on Instrinsic::ID 270002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar/// and the expression node. 280002d23aaf10f307273dab5facda01c137283d22Daniel Dunbarstatic RValue EmitBinaryAtomic(CodeGenFunction& CGF, 291ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang Intrinsic::ID Id, const CallExpr *E) { 30c5004516489c931cf839bdbdde5a76e4074cc888Mon P Wang const llvm::Type *ResType[2]; 310002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar ResType[0] = CGF.ConvertType(E->getType()); 320002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar ResType[1] = CGF.ConvertType(E->getArg(0)->getType()); 330002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2); 340002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return RValue::get(CGF.Builder.CreateCall2(AtomF, 350002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar CGF.EmitScalarExpr(E->getArg(0)), 360002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar CGF.EmitScalarExpr(E->getArg(1)))); 370002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar} 380002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar 390002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar/// Utility to insert an atomic instruction based Instrinsic::ID and 400002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar// the expression node, where the return value is the result of the 410002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar// operation. 420002d23aaf10f307273dab5facda01c137283d22Daniel Dunbarstatic RValue EmitBinaryAtomicPost(CodeGenFunction& CGF, 430002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Intrinsic::ID Id, const CallExpr *E, 440002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Instruction::BinaryOps Op) { 450002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar const llvm::Type *ResType[2]; 460002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar ResType[0] = CGF.ConvertType(E->getType()); 470002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar ResType[1] = CGF.ConvertType(E->getArg(0)->getType()); 480002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *AtomF = CGF.CGM.getIntrinsic(Id, ResType, 2); 490002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *Ptr = CGF.EmitScalarExpr(E->getArg(0)); 500002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *Operand = CGF.EmitScalarExpr(E->getArg(1)); 510002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *Result = CGF.Builder.CreateCall2(AtomF, Ptr, Operand); 52eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner 53eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner if (Id == Intrinsic::atomic_load_nand) 54eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner Result = CGF.Builder.CreateNot(Result); 55eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner 56eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner 570002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return RValue::get(CGF.Builder.CreateBinOp(Op, Result, Operand)); 581ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang} 591ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang 60ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel DunbarRValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, 61ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar unsigned BuiltinID, const CallExpr *E) { 62564ea2a99b3afeac9ded332730a56db1f6358a58Chris Lattner // See if we can constant fold this builtin. If so, don't emit it at all. 63f35d35a2316dcb65d078844696c2032b71a7f103Anders Carlsson Expr::EvalResult Result; 646ee7aa154e8bbb21a21254293410b944f78b0bfeChris Lattner if (E->Evaluate(Result, CGM.getContext())) { 65f35d35a2316dcb65d078844696c2032b71a7f103Anders Carlsson if (Result.Val.isInt()) 66f35d35a2316dcb65d078844696c2032b71a7f103Anders Carlsson return RValue::get(llvm::ConstantInt::get(Result.Val.getInt())); 673941b18b8e441c8c466efecd557de60b9a32d10bEli Friedman else if (Result.Val.isFloat()) 683941b18b8e441c8c466efecd557de60b9a32d10bEli Friedman return RValue::get(llvm::ConstantFP::get(Result.Val.getFloat())); 691f32999ec79a980576e100d64d5f3267eb19ea49Chris Lattner } 701f32999ec79a980576e100d64d5f3267eb19ea49Chris Lattner 71564ea2a99b3afeac9ded332730a56db1f6358a58Chris Lattner switch (BuiltinID) { 72564ea2a99b3afeac9ded332730a56db1f6358a58Chris Lattner default: break; // Handle intrinsics and libm functions below. 73506ff88f44562df267b6a06608ab841b76df2a2bChris Lattner case Builtin::BI__builtin___CFStringMakeConstantString: 74e9352cc9818ba59e7cf88500ef048991c90f3821Anders Carlsson return RValue::get(CGM.EmitConstantExpr(E, E->getType(), 0)); 756a705f0443f8398ece14d80e71d8e8c9e71aa84aChris Lattner case Builtin::BI__builtin_stdarg_start: 76793680ed8104bf088d1b382b963a8badcb3f07deAnders Carlsson case Builtin::BI__builtin_va_start: 77793680ed8104bf088d1b382b963a8badcb3f07deAnders Carlsson case Builtin::BI__builtin_va_end: { 780785570af3ef5f8c5a0377129e41efe6f3f8d770Daniel Dunbar Value *ArgValue = EmitVAListRef(E->getArg(0)); 79ddc23f3e6fdc4f83dd46ef7e20394cfbd6063ff9Christopher Lamb const llvm::Type *DestType = 80ddc23f3e6fdc4f83dd46ef7e20394cfbd6063ff9Christopher Lamb llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 81793680ed8104bf088d1b382b963a8badcb3f07deAnders Carlsson if (ArgValue->getType() != DestType) 82793680ed8104bf088d1b382b963a8badcb3f07deAnders Carlsson ArgValue = Builder.CreateBitCast(ArgValue, DestType, 83793680ed8104bf088d1b382b963a8badcb3f07deAnders Carlsson ArgValue->getNameStart()); 84793680ed8104bf088d1b382b963a8badcb3f07deAnders Carlsson 856a705f0443f8398ece14d80e71d8e8c9e71aa84aChris Lattner Intrinsic::ID inst = (BuiltinID == Builtin::BI__builtin_va_end) ? 866a705f0443f8398ece14d80e71d8e8c9e71aa84aChris Lattner Intrinsic::vaend : Intrinsic::vastart; 877acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return RValue::get(Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue)); 88793680ed8104bf088d1b382b963a8badcb3f07deAnders Carlsson } 89a28ef8b5c6ea452472d89041128fac8b683fe968Anders Carlsson case Builtin::BI__builtin_va_copy: { 904fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman Value *DstPtr = EmitVAListRef(E->getArg(0)); 914fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman Value *SrcPtr = EmitVAListRef(E->getArg(1)); 92a28ef8b5c6ea452472d89041128fac8b683fe968Anders Carlsson 93a28ef8b5c6ea452472d89041128fac8b683fe968Anders Carlsson const llvm::Type *Type = 94a28ef8b5c6ea452472d89041128fac8b683fe968Anders Carlsson llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 95a28ef8b5c6ea452472d89041128fac8b683fe968Anders Carlsson 96a28ef8b5c6ea452472d89041128fac8b683fe968Anders Carlsson DstPtr = Builder.CreateBitCast(DstPtr, Type); 97a28ef8b5c6ea452472d89041128fac8b683fe968Anders Carlsson SrcPtr = Builder.CreateBitCast(SrcPtr, Type); 983eae03e7d165f20a863a9a4d7122ba2a691ab16dChris Lattner return RValue::get(Builder.CreateCall2(CGM.getIntrinsic(Intrinsic::vacopy), 993eae03e7d165f20a863a9a4d7122ba2a691ab16dChris Lattner DstPtr, SrcPtr)); 100a28ef8b5c6ea452472d89041128fac8b683fe968Anders Carlsson } 101c2251dc59b0edc28f9303637dec970a7520939adAnders Carlsson case Builtin::BI__builtin_abs: { 1021feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner Value *ArgValue = EmitScalarExpr(E->getArg(0)); 103c2251dc59b0edc28f9303637dec970a7520939adAnders Carlsson 1049a847f598c4907a72b8593b364b9e6b94b086e75Chris Lattner Value *NegOp = Builder.CreateNeg(ArgValue, "neg"); 1051feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner Value *CmpResult = 1069a847f598c4907a72b8593b364b9e6b94b086e75Chris Lattner Builder.CreateICmpSGE(ArgValue, Constant::getNullValue(ArgValue->getType()), 1079a847f598c4907a72b8593b364b9e6b94b086e75Chris Lattner "abscond"); 1081feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner Value *Result = 109c2251dc59b0edc28f9303637dec970a7520939adAnders Carlsson Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); 110c2251dc59b0edc28f9303637dec970a7520939adAnders Carlsson 111c2251dc59b0edc28f9303637dec970a7520939adAnders Carlsson return RValue::get(Result); 112c2251dc59b0edc28f9303637dec970a7520939adAnders Carlsson } 1133a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson case Builtin::BI__builtin_ctz: 1143a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson case Builtin::BI__builtin_ctzl: 1153a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson case Builtin::BI__builtin_ctzll: { 1163a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson Value *ArgValue = EmitScalarExpr(E->getArg(0)); 1173a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson 1183a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson const llvm::Type *ArgType = ArgValue->getType(); 1193a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); 1203a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson 1213a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson const llvm::Type *ResultType = ConvertType(E->getType()); 1223a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); 1233a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson if (Result->getType() != ResultType) 1243a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson Result = Builder.CreateIntCast(Result, ResultType, "cast"); 1253a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson return RValue::get(Result); 1263a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson } 127f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman case Builtin::BI__builtin_clz: 128f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman case Builtin::BI__builtin_clzl: 129f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman case Builtin::BI__builtin_clzll: { 130f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman Value *ArgValue = EmitScalarExpr(E->getArg(0)); 131f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman 132f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman const llvm::Type *ArgType = ArgValue->getType(); 133f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1); 134f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman 135f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman const llvm::Type *ResultType = ConvertType(E->getType()); 136f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); 137f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman if (Result->getType() != ResultType) 138f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman Result = Builder.CreateIntCast(Result, ResultType, "cast"); 139f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman return RValue::get(Result); 140f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman } 14104b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_ffs: 14204b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_ffsl: 14304b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_ffsll: { 14404b290030eee33295600728450f348989d1a627eDaniel Dunbar // ffs(x) -> x ? cttz(x) + 1 : 0 14504b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *ArgValue = EmitScalarExpr(E->getArg(0)); 14604b290030eee33295600728450f348989d1a627eDaniel Dunbar 14704b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ArgType = ArgValue->getType(); 14804b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); 14904b290030eee33295600728450f348989d1a627eDaniel Dunbar 15004b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ResultType = ConvertType(E->getType()); 15104b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"), 15204b290030eee33295600728450f348989d1a627eDaniel Dunbar ConstantInt::get(ArgType, 1), "tmp"); 15304b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Zero = llvm::Constant::getNullValue(ArgType); 15404b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); 15504b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs"); 15604b290030eee33295600728450f348989d1a627eDaniel Dunbar if (Result->getType() != ResultType) 15704b290030eee33295600728450f348989d1a627eDaniel Dunbar Result = Builder.CreateIntCast(Result, ResultType, "cast"); 15804b290030eee33295600728450f348989d1a627eDaniel Dunbar return RValue::get(Result); 15904b290030eee33295600728450f348989d1a627eDaniel Dunbar } 16004b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_parity: 16104b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_parityl: 16204b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_parityll: { 16304b290030eee33295600728450f348989d1a627eDaniel Dunbar // parity(x) -> ctpop(x) & 1 16404b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *ArgValue = EmitScalarExpr(E->getArg(0)); 16504b290030eee33295600728450f348989d1a627eDaniel Dunbar 16604b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ArgType = ArgValue->getType(); 16704b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); 16804b290030eee33295600728450f348989d1a627eDaniel Dunbar 16904b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ResultType = ConvertType(E->getType()); 17004b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp"); 17104b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Result = Builder.CreateAnd(Tmp, ConstantInt::get(ArgType, 1), 17204b290030eee33295600728450f348989d1a627eDaniel Dunbar "tmp"); 17304b290030eee33295600728450f348989d1a627eDaniel Dunbar if (Result->getType() != ResultType) 17404b290030eee33295600728450f348989d1a627eDaniel Dunbar Result = Builder.CreateIntCast(Result, ResultType, "cast"); 17504b290030eee33295600728450f348989d1a627eDaniel Dunbar return RValue::get(Result); 17604b290030eee33295600728450f348989d1a627eDaniel Dunbar } 17704b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_popcount: 17804b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_popcountl: 17904b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_popcountll: { 18004b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *ArgValue = EmitScalarExpr(E->getArg(0)); 18104b290030eee33295600728450f348989d1a627eDaniel Dunbar 18204b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ArgType = ArgValue->getType(); 18304b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); 18404b290030eee33295600728450f348989d1a627eDaniel Dunbar 18504b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ResultType = ConvertType(E->getType()); 18604b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); 18704b290030eee33295600728450f348989d1a627eDaniel Dunbar if (Result->getType() != ResultType) 18804b290030eee33295600728450f348989d1a627eDaniel Dunbar Result = Builder.CreateIntCast(Result, ResultType, "cast"); 18904b290030eee33295600728450f348989d1a627eDaniel Dunbar return RValue::get(Result); 19004b290030eee33295600728450f348989d1a627eDaniel Dunbar } 1911feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner case Builtin::BI__builtin_expect: 192a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar // FIXME: pass expect through to LLVM 1931feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner return RValue::get(EmitScalarExpr(E->getArg(0))); 194df4852ac816e6050d53b808b86d7c1c9738eb99eAnders Carlsson case Builtin::BI__builtin_bswap32: 195df4852ac816e6050d53b808b86d7c1c9738eb99eAnders Carlsson case Builtin::BI__builtin_bswap64: { 1961feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner Value *ArgValue = EmitScalarExpr(E->getArg(0)); 197df4852ac816e6050d53b808b86d7c1c9738eb99eAnders Carlsson const llvm::Type *ArgType = ArgValue->getType(); 1987acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); 1991feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); 2004493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar } 201d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar case Builtin::BI__builtin_object_size: { 202d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar // FIXME: Implement. For now we just always fail and pretend we 203d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar // don't know the object size. 2049a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman llvm::APSInt TypeArg = E->getArg(1)->EvaluateAsInt(CGM.getContext()); 205d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar const llvm::Type *ResType = ConvertType(E->getType()); 206d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar // bool UseSubObject = TypeArg.getZExtValue() & 1; 207d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar bool UseMinimum = TypeArg.getZExtValue() & 2; 208d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar return RValue::get(ConstantInt::get(ResType, UseMinimum ? 0 : -1LL)); 209d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar } 2104493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar case Builtin::BI__builtin_prefetch: { 2114493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0)); 2124493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar // FIXME: Technically these constants should of type 'int', yes? 2134493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : 2144493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar ConstantInt::get(llvm::Type::Int32Ty, 0); 2154493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : 2164493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar ConstantInt::get(llvm::Type::Int32Ty, 3); 2174493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0); 2184493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar return RValue::get(Builder.CreateCall3(F, Address, RW, Locality)); 2194493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar } 2204493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar case Builtin::BI__builtin_trap: { 2214493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0); 2224493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar return RValue::get(Builder.CreateCall(F)); 223df4852ac816e6050d53b808b86d7c1c9738eb99eAnders Carlsson } 2244493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar 225a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar case Builtin::BI__builtin_powi: 226a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar case Builtin::BI__builtin_powif: 227a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar case Builtin::BI__builtin_powil: { 228a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar Value *Base = EmitScalarExpr(E->getArg(0)); 229a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar Value *Exponent = EmitScalarExpr(E->getArg(1)); 230a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar const llvm::Type *ArgType = Base->getType(); 231a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1); 232a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); 233a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar } 234a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar 235fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isgreater: 236fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isgreaterequal: 237fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isless: 238fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_islessequal: 239fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_islessgreater: 240fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isunordered: { 241fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner // Ordered comparisons: we know the arguments to these are matching scalar 242fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner // floating point values. 243fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner Value *LHS = EmitScalarExpr(E->getArg(0)); 244fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner Value *RHS = EmitScalarExpr(E->getArg(1)); 245fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner 246fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner switch (BuiltinID) { 247fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner default: assert(0 && "Unknown ordered comparison"); 248fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isgreater: 249fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); 250fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 251fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isgreaterequal: 252fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); 253fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 254fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isless: 255fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); 256fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 257fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_islessequal: 258fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); 259fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 260fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_islessgreater: 261fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); 262fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 263fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isunordered: 264fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); 265fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 266fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner } 267fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner // ZExt bool to int type. 268fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), 269fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner "tmp")); 270fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner } 2719e800e3dd80d77f6c47054738177bf824089f55aChris Lattner case Builtin::BI__builtin_alloca: { 2729e800e3dd80d77f6c47054738177bf824089f55aChris Lattner // FIXME: LLVM IR Should allow alloca with an i64 size! 2739e800e3dd80d77f6c47054738177bf824089f55aChris Lattner Value *Size = EmitScalarExpr(E->getArg(0)); 2749e800e3dd80d77f6c47054738177bf824089f55aChris Lattner Size = Builder.CreateIntCast(Size, llvm::Type::Int32Ty, false, "tmp"); 2751caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar return RValue::get(Builder.CreateAlloca(llvm::Type::Int8Ty, Size, "tmp")); 2761caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar } 2771caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar case Builtin::BI__builtin_bzero: { 2781caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Value *Address = EmitScalarExpr(E->getArg(0)); 2791caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Builder.CreateCall4(CGM.getMemSetFn(), Address, 2801caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar llvm::ConstantInt::get(llvm::Type::Int8Ty, 0), 2811caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(1)), 2821caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); 2831caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar return RValue::get(Address); 2849e800e3dd80d77f6c47054738177bf824089f55aChris Lattner } 285d4b32e46517358f34e8cfbea35010adfcc3786e0Eli Friedman case Builtin::BI__builtin_memcpy: { 2861caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Value *Address = EmitScalarExpr(E->getArg(0)); 2871caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Builder.CreateCall4(CGM.getMemCpyFn(), Address, 2881caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(1)), 2891caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(2)), 2901caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); 2911caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar return RValue::get(Address); 2921caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar } 2931caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar case Builtin::BI__builtin_memmove: { 2941caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Value *Address = EmitScalarExpr(E->getArg(0)); 2951caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Builder.CreateCall4(CGM.getMemMoveFn(), Address, 2961caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(1)), 2971caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(2)), 2981caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); 2991caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar return RValue::get(Address); 3001caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar } 3011caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar case Builtin::BI__builtin_memset: { 3021caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Value *Address = EmitScalarExpr(E->getArg(0)); 3031caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Builder.CreateCall4(CGM.getMemSetFn(), Address, 30462c29c667f5bcb854e99ccb3aec1280c84115951Daniel Dunbar Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), 30562c29c667f5bcb854e99ccb3aec1280c84115951Daniel Dunbar llvm::Type::Int8Ty), 3061caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(2)), 3071caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); 3081caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar return RValue::get(Address); 309d4b32e46517358f34e8cfbea35010adfcc3786e0Eli Friedman } 310256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman case Builtin::BI__builtin_return_address: { 311256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); 312256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0)))); 313256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman } 314256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman case Builtin::BI__builtin_frame_address: { 315256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); 316256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0)))); 317256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman } 3183b660efb9f9fa3e87096f4a96a2093cd17c43c2eEli Friedman case Builtin::BI__builtin_extract_return_addr: { 3193b660efb9f9fa3e87096f4a96a2093cd17c43c2eEli Friedman // FIXME: There should be a target hook for this 3203b660efb9f9fa3e87096f4a96a2093cd17c43c2eEli Friedman return RValue::get(EmitScalarExpr(E->getArg(0))); 3213b660efb9f9fa3e87096f4a96a2093cd17c43c2eEli Friedman } 3221ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_add: 3231ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_sub: 3245caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or: 3255caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and: 3265caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor: 3275caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch: 3285caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch: 3295caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch: 3305caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch: 3315caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch: 3325caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap: 3335caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap: 3345caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set: 3355caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release: 3365caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner assert(0 && "Shouldn't make it through sema"); 3375caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add_1: 3385caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add_2: 3395caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add_4: 3405caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add_8: 3415caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add_16: 3425caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); 3435caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub_1: 3445caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub_2: 3455caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub_4: 3465caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub_8: 3475caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub_16: 34809b6bf5bfa2ba9cbbd353cbd7846af8f49b020e7Mon P Wang return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); 3495caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or_1: 3505caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or_2: 3515caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or_4: 3525caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or_8: 3535caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or_16: 3545caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); 3555caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and_1: 3565caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and_2: 3575caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and_4: 3585caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and_8: 3595caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and_16: 3605caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); 3615caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor_1: 3625caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor_2: 3635caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor_4: 3645caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor_8: 3655caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor_16: 3665caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); 367eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_fetch_and_nand_1: 368eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_fetch_and_nand_2: 369eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_fetch_and_nand_4: 370eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_fetch_and_nand_8: 371eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_fetch_and_nand_16: 372eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner return EmitBinaryAtomic(*this, Intrinsic::atomic_load_nand, E); 373eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner 3745caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // Clang extensions: not overloaded yet. 3751ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_min: 3761ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); 3771ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_max: 3781ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); 3791ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_umin: 3801ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); 3811ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_umax: 3821ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); 3830002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar 3845caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch_1: 3855caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch_2: 3865caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch_4: 3875caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch_8: 3885caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch_16: 3890002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, 3900002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar llvm::Instruction::Add); 3915caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch_1: 3925caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch_2: 3935caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch_4: 3945caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch_8: 3955caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch_16: 3960002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, 3970002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar llvm::Instruction::Sub); 3985caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch_1: 3995caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch_2: 4005caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch_4: 4015caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch_8: 4025caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch_16: 4030002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, 4040002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar llvm::Instruction::And); 4055caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch_1: 4065caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch_2: 4075caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch_4: 4085caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch_8: 4095caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch_16: 4100002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, 4110002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar llvm::Instruction::Or); 4125caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch_1: 4135caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch_2: 4145caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch_4: 4155caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch_8: 4165caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch_16: 4170002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, 4180002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar llvm::Instruction::Xor); 419eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_nand_and_fetch_1: 420eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_nand_and_fetch_2: 421eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_nand_and_fetch_4: 422eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_nand_and_fetch_8: 423eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_nand_and_fetch_16: 424eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_nand, E, 425eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner llvm::Instruction::And); 426eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner 4275caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap_1: 4285caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap_2: 4295caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap_4: 4305caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap_8: 4315caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap_16: 4325caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner { 433c5004516489c931cf839bdbdde5a76e4074cc888Mon P Wang const llvm::Type *ResType[2]; 434c5004516489c931cf839bdbdde5a76e4074cc888Mon P Wang ResType[0]= ConvertType(E->getType()); 435c5004516489c931cf839bdbdde5a76e4074cc888Mon P Wang ResType[1] = ConvertType(E->getArg(0)->getType()); 436c5004516489c931cf839bdbdde5a76e4074cc888Mon P Wang Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); 437958444659c5287a81a43188bfaa2a4902752b947Anders Carlsson return RValue::get(Builder.CreateCall3(AtomF, 438958444659c5287a81a43188bfaa2a4902752b947Anders Carlsson EmitScalarExpr(E->getArg(0)), 439958444659c5287a81a43188bfaa2a4902752b947Anders Carlsson EmitScalarExpr(E->getArg(1)), 440958444659c5287a81a43188bfaa2a4902752b947Anders Carlsson EmitScalarExpr(E->getArg(2)))); 441022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson } 4420002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar 4435caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap_1: 4445caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap_2: 4455caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap_4: 4465caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap_8: 4475caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap_16: 4485caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner { 4490002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar const llvm::Type *ResType[2]; 4505caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner ResType[0]= ConvertType(E->getArg(1)->getType()); 4515caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner ResType[1] = llvm::PointerType::getUnqual(ResType[0]); 4520002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); 4530002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *OldVal = EmitScalarExpr(E->getArg(1)); 4540002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *PrevVal = Builder.CreateCall3(AtomF, 4550002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar EmitScalarExpr(E->getArg(0)), 4560002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar OldVal, 4570002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar EmitScalarExpr(E->getArg(2))); 4580002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); 4590002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar // zext bool to int. 4600002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); 4610002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar } 4620002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar 4635caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set_1: 4645caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set_2: 4655caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set_4: 4665caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set_8: 4675caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set_16: 4687ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); 4695caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release_1: 4705caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release_2: 4715caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release_4: 4725caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release_8: 473f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner case Builtin::BI__sync_lock_release_16: { 474f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner Value *Ptr = EmitScalarExpr(E->getArg(0)); 475f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner const llvm::Type *ElTy = 476f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner cast<llvm::PointerType>(Ptr->getType())->getElementType(); 477f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner Builder.CreateStore(llvm::Constant::getNullValue(ElTy), Ptr, true); 478f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner return RValue(); 479f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner } 480ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar 481f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner case Builtin::BI__sync_synchronize: { 482f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner Value *C[5]; 483f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner C[0] = C[1] = C[2] = C[3] = llvm::ConstantInt::get(llvm::Type::Int1Ty, 1); 484f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner C[4] = ConstantInt::get(llvm::Type::Int1Ty, 0); 485f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); 486f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner return RValue(); 487f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner } 488f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner 489ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar // Library functions with special handling. 490ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIsqrt: 491ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIsqrtf: 492ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIsqrtl: { 493ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar // Rewrite sqrt to intrinsic if allowed. 494b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (!FD->hasAttr<ConstAttr>()) 495ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar break; 496ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar Value *Arg0 = EmitScalarExpr(E->getArg(0)); 497ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar const llvm::Type *ArgType = Arg0->getType(); 498ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1); 499ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar return RValue::get(Builder.CreateCall(F, Arg0, "tmp")); 500ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar } 501ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar 502ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIpow: 503ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIpowf: 504ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIpowl: { 505ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar // Rewrite sqrt to intrinsic if allowed. 506b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (!FD->hasAttr<ConstAttr>()) 507ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar break; 508ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar Value *Base = EmitScalarExpr(E->getArg(0)); 509ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar Value *Exponent = EmitScalarExpr(E->getArg(1)); 510ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar const llvm::Type *ArgType = Base->getType(); 511ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); 512ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); 513ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar } 5147ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman } 515b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 516b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // If this is an alias for a libm function (e.g. __builtin_sin) turn it into 517b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // that function. 5183e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || 5193e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 520b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner return EmitCallExpr(CGM.getBuiltinLibFunction(BuiltinID), 521b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner E->getCallee()->getType(), E->arg_begin(), 522b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner E->arg_end()); 523b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 524b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // See if we have a target specific intrinsic. 525a6f80ef997f0363386749087b325607eaa5adcfcDale Johannesen const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); 526a6f80ef997f0363386749087b325607eaa5adcfcDale Johannesen Intrinsic::ID IntrinsicID = 527a6f80ef997f0363386749087b325607eaa5adcfcDale Johannesen Intrinsic::getIntrinsicForGCCBuiltin(Target.getTargetPrefix(), Name); 528b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 529b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (IntrinsicID != Intrinsic::not_intrinsic) { 530b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner SmallVector<Value*, 16> Args; 531b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 532b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner Function *F = CGM.getIntrinsic(IntrinsicID); 533b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner const llvm::FunctionType *FTy = F->getFunctionType(); 534b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 535b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 536b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner Value *ArgValue = EmitScalarExpr(E->getArg(i)); 537b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 538b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // If the intrinsic arg type is different from the builtin arg type 539b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // we need to do a bit cast. 540b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner const llvm::Type *PTy = FTy->getParamType(i); 541b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (PTy != ArgValue->getType()) { 542b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && 543b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner "Must be able to losslessly bit cast to param"); 544b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner ArgValue = Builder.CreateBitCast(ArgValue, PTy); 545b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner } 546b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 547b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner Args.push_back(ArgValue); 548b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner } 549b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 550b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner Value *V = Builder.CreateCall(F, &Args[0], &Args[0] + Args.size()); 551b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner QualType BuiltinRetType = E->getType(); 552b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 553b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner const llvm::Type *RetTy = llvm::Type::VoidTy; 554b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); 555b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 556b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (RetTy != V->getType()) { 557b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner assert(V->getType()->canLosslesslyBitCastTo(RetTy) && 558b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner "Must be able to losslessly bit cast result type"); 559b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner V = Builder.CreateBitCast(V, RetTy); 560b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner } 561b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 562b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner return RValue::get(V); 563b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner } 564b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 565b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // See if we have a target specific builtin that needs to be lowered. 566f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) 567b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner return RValue::get(V); 568b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 569488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar ErrorUnsupported(E, "builtin function"); 570b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 571b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // Unknown builtin, for now just dump it out and return undef. 572b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (hasAggregateLLVMType(E->getType())) 573b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType()))); 574b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner return RValue::get(UndefValue::get(ConvertType(E->getType()))); 575b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner} 576564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson 577f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel DunbarValue *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, 578f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar const CallExpr *E) { 579f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar const char *TargetPrefix = Target.getTargetPrefix(); 580f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar if (strcmp(TargetPrefix, "x86") == 0) 581f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar return EmitX86BuiltinExpr(BuiltinID, E); 582f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar else if (strcmp(TargetPrefix, "ppc") == 0) 583f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar return EmitPPCBuiltinExpr(BuiltinID, E); 584f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar return 0; 585f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar} 586f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar 5871feedd84221e8dbcc3faf3de27cc42b559db845dChris LattnerValue *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, 5881feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner const CallExpr *E) { 5892929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson 5902929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson llvm::SmallVector<Value*, 4> Ops; 5912929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson 5922929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) 5932929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson Ops.push_back(EmitScalarExpr(E->getArg(i))); 5942929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson 595564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson switch (BuiltinID) { 59646a26b0b0e2ec1557bad9b70e8e20836524ebdfcAnders Carlsson default: return 0; 59746a26b0b0e2ec1557bad9b70e8e20836524ebdfcAnders Carlsson case X86::BI__builtin_ia32_mulps: 5982929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson return Builder.CreateMul(Ops[0], Ops[1], "mulps"); 599e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_mulpd: 600e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateMul(Ops[0], Ops[1], "mulpd"); 6014e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_pand: 602e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_pand128: 6032929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson return Builder.CreateAnd(Ops[0], Ops[1], "pand"); 6044e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_por: 605e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_por128: 606e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateOr(Ops[0], Ops[1], "por"); 6074e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_pxor: 608e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_pxor128: 609e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateXor(Ops[0], Ops[1], "pxor"); 610e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_pandn: 611e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_pandn128: 6122929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson Ops[0] = Builder.CreateNot(Ops[0], "tmp"); 6132929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson return Builder.CreateAnd(Ops[0], Ops[1], "pandn"); 6144e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_paddb: 615e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_paddb128: 6164e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_paddd: 617e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_paddd128: 6184e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_paddq: 619e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_paddq128: 6204e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_paddw: 621e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_paddw128: 622cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson case X86::BI__builtin_ia32_addps: 623e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_addpd: 624cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson return Builder.CreateAdd(Ops[0], Ops[1], "add"); 6254e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_psubb: 626e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psubb128: 6274e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_psubd: 628e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psubd128: 6294e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_psubq: 630e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psubq128: 6314e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson case X86::BI__builtin_ia32_psubw: 632e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psubw128: 633cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson case X86::BI__builtin_ia32_subps: 634e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_subpd: 635cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson return Builder.CreateSub(Ops[0], Ops[1], "sub"); 636cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson case X86::BI__builtin_ia32_divps: 637cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson return Builder.CreateFDiv(Ops[0], Ops[1], "divps"); 638e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_divpd: 639e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateFDiv(Ops[0], Ops[1], "divpd"); 640db832305d35a7c9ef08bbafcf79d8da162af3374Anders Carlsson case X86::BI__builtin_ia32_pmullw: 641e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_pmullw128: 6422929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson return Builder.CreateMul(Ops[0], Ops[1], "pmul"); 643db832305d35a7c9ef08bbafcf79d8da162af3374Anders Carlsson case X86::BI__builtin_ia32_punpckhbw: 6447acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return EmitShuffleVector(Ops[0], Ops[1], 4, 12, 5, 13, 6, 14, 7, 15, 645db832305d35a7c9ef08bbafcf79d8da162af3374Anders Carlsson "punpckhbw"); 646e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_punpckhbw128: 647e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 8, 24, 9, 25, 10, 26, 11, 27, 648e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 12, 28, 13, 29, 14, 30, 15, 31, 649e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman "punpckhbw"); 650db832305d35a7c9ef08bbafcf79d8da162af3374Anders Carlsson case X86::BI__builtin_ia32_punpckhwd: 6517acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhwd"); 652e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_punpckhwd128: 653e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 4, 12, 5, 13, 6, 14, 7, 15, 654e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman "punpckhwd"); 655db832305d35a7c9ef08bbafcf79d8da162af3374Anders Carlsson case X86::BI__builtin_ia32_punpckhdq: 6567acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhdq"); 657e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_punpckhdq128: 658e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "punpckhdq"); 659abfaf9984eee3d076ded8770bf9fce156f10c185Nate Begeman case X86::BI__builtin_ia32_punpckhqdq128: 660abfaf9984eee3d076ded8770bf9fce156f10c185Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "punpckhqdq"); 661db832305d35a7c9ef08bbafcf79d8da162af3374Anders Carlsson case X86::BI__builtin_ia32_punpcklbw: 6627acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return EmitShuffleVector(Ops[0], Ops[1], 0, 8, 1, 9, 2, 10, 3, 11, 663db832305d35a7c9ef08bbafcf79d8da162af3374Anders Carlsson "punpcklbw"); 664db832305d35a7c9ef08bbafcf79d8da162af3374Anders Carlsson case X86::BI__builtin_ia32_punpcklwd: 6657acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpcklwd"); 666db832305d35a7c9ef08bbafcf79d8da162af3374Anders Carlsson case X86::BI__builtin_ia32_punpckldq: 6677acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpckldq"); 668e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_punpckldq128: 669e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "punpckldq"); 670abfaf9984eee3d076ded8770bf9fce156f10c185Nate Begeman case X86::BI__builtin_ia32_punpcklqdq128: 671abfaf9984eee3d076ded8770bf9fce156f10c185Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "punpcklqdq"); 672e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_pslldi128: 673e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psllqi128: 674e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psllwi128: 675e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psradi128: 676e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrawi128: 677e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrldi128: 678e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrlqi128: 679e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrlwi128: { 680e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); 681e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 2); 682e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); 683e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), 684e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1], Zero, "insert"); 685e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); 686e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const char *name = 0; 687e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Intrinsic::ID ID = Intrinsic::not_intrinsic; 688e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 689e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman switch (BuiltinID) { 690e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman default: assert(0 && "Unsupported shift intrinsic!"); 691e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_pslldi128: 692e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "pslldi"; 693e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psll_d; 694e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 695e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psllqi128: 696e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psllqi"; 697e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psll_q; 698e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 699e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psllwi128: 700e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psllwi"; 701e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psll_w; 702e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 703e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psradi128: 704e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psradi"; 705e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psra_d; 706e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 707e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrawi128: 708e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psrawi"; 709e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psra_w; 710e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 711e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrldi128: 712e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psrldi"; 713e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psrl_d; 714e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 715e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrlqi128: 716e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psrlqi"; 717e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psrl_q; 718e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 719e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrlwi128: 720e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psrlwi"; 721e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psrl_w; 722e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 723e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 724e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Function *F = CGM.getIntrinsic(ID); 725e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); 726e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 7272929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_pslldi: 7282929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psllqi: 7292929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psllwi: 7302929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psradi: 7312929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrawi: 7322929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrldi: 7332929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrlqi: 7342929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrlwi: { 7352929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); 7362929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 1); 7372929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); 7382929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson const char *name = 0; 7392929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson Intrinsic::ID ID = Intrinsic::not_intrinsic; 7404e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson 7412929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson switch (BuiltinID) { 7422929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson default: assert(0 && "Unsupported shift intrinsic!"); 7432929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_pslldi: 7442929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "pslldi"; 7452929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psll_d; 7462929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7472929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psllqi: 7482929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psllqi"; 7492929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psll_q; 7502929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7512929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psllwi: 7522929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psllwi"; 7532929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psll_w; 7542929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7552929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psradi: 7562929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psradi"; 7572929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psra_d; 7582929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7592929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrawi: 7602929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psrawi"; 7612929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psra_w; 7622929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7632929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrldi: 7642929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psrldi"; 7652929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psrl_d; 7662929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7672929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrlqi: 7682929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psrlqi"; 7692929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psrl_q; 7702929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7712929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrlwi: 7722929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psrlwi"; 7732929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psrl_w; 7742929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7752929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson } 7767acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner llvm::Function *F = CGM.getIntrinsic(ID); 7772929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); 7782929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson } 7794ee51b464fb0d452fb2a996dd2e86203eb70b0d7Anders Carlsson case X86::BI__builtin_ia32_pshufw: { 7804ee51b464fb0d452fb2a996dd2e86203eb70b0d7Anders Carlsson unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); 7814ee51b464fb0d452fb2a996dd2e86203eb70b0d7Anders Carlsson return EmitShuffleVector(Ops[0], Ops[0], 7824ee51b464fb0d452fb2a996dd2e86203eb70b0d7Anders Carlsson i & 0x3, (i & 0xc) >> 2, 7834ee51b464fb0d452fb2a996dd2e86203eb70b0d7Anders Carlsson (i & 0x30) >> 4, (i & 0xc0) >> 6, 7844ee51b464fb0d452fb2a996dd2e86203eb70b0d7Anders Carlsson "pshufw"); 7854ee51b464fb0d452fb2a996dd2e86203eb70b0d7Anders Carlsson } 7867ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman case X86::BI__builtin_ia32_pshuflw: { 7877ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); 7887ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman return EmitShuffleVector(Ops[0], Ops[0], 7897ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman i & 0x3, (i & 0xc) >> 2, 7907ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman (i & 0x30) >> 4, (i & 0xc0) >> 6, 4, 5, 6, 7, 7917ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman "pshuflw"); 7927ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman } 7937ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman case X86::BI__builtin_ia32_pshufhw: { 7947ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); 7957ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman return EmitShuffleVector(Ops[0], Ops[0], 0, 1, 2, 3, 7967ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman 4 + (i & 0x3), 4 + ((i & 0xc) >> 2), 7977ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman 4 + ((i & 0x30) >> 4), 4 + ((i & 0xc0) >> 6), 7987ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman "pshufhw"); 7997ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman } 8002929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_pshufd: { 8017acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned i = cast<ConstantInt>(Ops[1])->getZExtValue(); 8022929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson return EmitShuffleVector(Ops[0], Ops[0], 8034e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson i & 0x3, (i & 0xc) >> 2, 8044e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson (i & 0x30) >> 4, (i & 0xc0) >> 6, 8054e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson "pshufd"); 8064e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson } 8076086bbd1799e22e75561c3d31dc9b923f0508fa5Anders Carlsson case X86::BI__builtin_ia32_vec_init_v4hi: 8086086bbd1799e22e75561c3d31dc9b923f0508fa5Anders Carlsson case X86::BI__builtin_ia32_vec_init_v8qi: 8096086bbd1799e22e75561c3d31dc9b923f0508fa5Anders Carlsson case X86::BI__builtin_ia32_vec_init_v2si: 8106086bbd1799e22e75561c3d31dc9b923f0508fa5Anders Carlsson return EmitVector(&Ops[0], Ops.size()); 8116086bbd1799e22e75561c3d31dc9b923f0508fa5Anders Carlsson case X86::BI__builtin_ia32_vec_ext_v2si: 812e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_vec_ext_v2di: 813e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_vec_ext_v4sf: 814e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_vec_ext_v4si: 8156fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman case X86::BI__builtin_ia32_vec_ext_v8hi: 8166fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman case X86::BI__builtin_ia32_vec_ext_v4hi: 817e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_vec_ext_v2df: 8186086bbd1799e22e75561c3d31dc9b923f0508fa5Anders Carlsson return Builder.CreateExtractElement(Ops[0], Ops[1], "result"); 81979dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson case X86::BI__builtin_ia32_cmpps: { 82079dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); 82179dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); 82279dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson } 82379dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson case X86::BI__builtin_ia32_cmpss: { 82479dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); 82579dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); 826cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson } 827e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_ldmxcsr: { 828e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 829e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Value *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); 830e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp"); 831e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Builder.CreateStore(Ops[0], Tmp); 832e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), 8333eae03e7d165f20a863a9a4d7122ba2a691ab16dChris Lattner Builder.CreateBitCast(Tmp, PtrTy)); 834e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 835e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_stmxcsr: { 836e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 837e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Value *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); 838e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp"); 839e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), 8403eae03e7d165f20a863a9a4d7122ba2a691ab16dChris Lattner Builder.CreateBitCast(Tmp, PtrTy)); 841e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateLoad(Tmp, "stmxcsr"); 842e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 84379dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson case X86::BI__builtin_ia32_cmppd: { 84479dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); 84579dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); 84679dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson } 84779dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson case X86::BI__builtin_ia32_cmpsd: { 84879dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); 84979dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); 850cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson } 851cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson case X86::BI__builtin_ia32_movss: 852cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson return EmitShuffleVector(Ops[0], Ops[1], 4, 1, 2, 3, "movss"); 853730f2c13e8ff0a2a3cc54fce9913add68107cb51Torok Edwin case X86::BI__builtin_ia32_shufps: { 8547acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue(); 855cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson return EmitShuffleVector(Ops[0], Ops[1], 856cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson i & 0x3, (i & 0xc) >> 2, 857cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson ((i & 0x30) >> 4) + 4, 85809b6bf5bfa2ba9cbbd353cbd7846af8f49b020e7Mon P Wang ((i & 0xc0) >> 6) + 4, "shufps"); 859730f2c13e8ff0a2a3cc54fce9913add68107cb51Torok Edwin } 8606c5df7f54f40090df6032a74e650b8e4d1c96207Mon P Wang case X86::BI__builtin_ia32_shufpd: { 8616c5df7f54f40090df6032a74e650b8e4d1c96207Mon P Wang unsigned i = cast<ConstantInt>(Ops[2])->getZExtValue(); 862f29c8ad917e5fdc8fdd3d43707073be217bad625Chris Lattner return EmitShuffleVector(Ops[0], Ops[1], i & 1, 863f29c8ad917e5fdc8fdd3d43707073be217bad625Chris Lattner ((i & 2) >> 1)+2, "shufpd"); 8646c5df7f54f40090df6032a74e650b8e4d1c96207Mon P Wang } 865dbebb73b86d9fe56c2227bf75c13ea3a49248261Nate Begeman case X86::BI__builtin_ia32_punpcklbw128: 866dbebb73b86d9fe56c2227bf75c13ea3a49248261Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 0, 16, 1, 17, 2, 18, 3, 19, 867dbebb73b86d9fe56c2227bf75c13ea3a49248261Nate Begeman 4, 20, 5, 21, 6, 22, 7, 23, 868dbebb73b86d9fe56c2227bf75c13ea3a49248261Nate Begeman "punpcklbw"); 869dbebb73b86d9fe56c2227bf75c13ea3a49248261Nate Begeman case X86::BI__builtin_ia32_punpcklwd128: 870dbebb73b86d9fe56c2227bf75c13ea3a49248261Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 0, 8, 1, 9, 2, 10, 3, 11, 871dbebb73b86d9fe56c2227bf75c13ea3a49248261Nate Begeman "punpcklwd"); 872e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_movlhps: 873e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 0, 1, 4, 5, "movlhps"); 874e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_movhlps: 875e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 6, 7, 2, 3, "movhlps"); 876e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_unpckhps: 877e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 2, 6, 3, 7, "unpckhps"); 878e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_unpcklps: 879e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 0, 4, 1, 5, "unpcklps"); 880abfaf9984eee3d076ded8770bf9fce156f10c185Nate Begeman case X86::BI__builtin_ia32_unpckhpd: 881abfaf9984eee3d076ded8770bf9fce156f10c185Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 1, 3, "unpckhpd"); 882abfaf9984eee3d076ded8770bf9fce156f10c185Nate Begeman case X86::BI__builtin_ia32_unpcklpd: 883abfaf9984eee3d076ded8770bf9fce156f10c185Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 0, 2, "unpcklpd"); 884abfaf9984eee3d076ded8770bf9fce156f10c185Nate Begeman case X86::BI__builtin_ia32_movsd: 885abfaf9984eee3d076ded8770bf9fce156f10c185Nate Begeman return EmitShuffleVector(Ops[0], Ops[1], 2, 1, "movsd"); 886e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_movqv4si: { 887e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 2); 888e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateBitCast(Ops[0], Ty); 889e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 890e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_loadlps: 891e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_loadhps: { 892e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // FIXME: This should probably be represented as 893e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // shuffle (dst, (v4f32 (insert undef, (load i64), 0)), shuf mask hi/lo) 894e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const llvm::Type *EltTy = llvm::Type::DoubleTy; 895e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); 896e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const llvm::Type *OrigTy = Ops[0]->getType(); 897e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman unsigned Index = BuiltinID == X86::BI__builtin_ia32_loadlps ? 0 : 1; 898e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index); 899e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateBitCast(Ops[1], llvm::PointerType::getUnqual(EltTy)); 900e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateLoad(Ops[1], "tmp"); 901e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateBitCast(Ops[0], VecTy, "cast"); 902e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateInsertElement(Ops[0], Ops[1], Idx, "loadps"); 903e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateBitCast(Ops[0], OrigTy, "loadps"); 904e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 9057f57b000f232bc596dca627c976ace250c8171e3Mon P Wang case X86::BI__builtin_ia32_loadlpd: 9067f57b000f232bc596dca627c976ace250c8171e3Mon P Wang case X86::BI__builtin_ia32_loadhpd: { 9077f57b000f232bc596dca627c976ace250c8171e3Mon P Wang Ops[1] = Builder.CreateLoad(Ops[1], "tmp"); 9087f57b000f232bc596dca627c976ace250c8171e3Mon P Wang unsigned Index = BuiltinID == X86::BI__builtin_ia32_loadlpd ? 0 : 1; 9097f57b000f232bc596dca627c976ace250c8171e3Mon P Wang llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index); 9107f57b000f232bc596dca627c976ace250c8171e3Mon P Wang return Builder.CreateInsertElement(Ops[0], Ops[1], Idx, "loadpd"); 9117f57b000f232bc596dca627c976ace250c8171e3Mon P Wang } 912e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_storehps: 913e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_storelps: { 914e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const llvm::Type *EltTy = llvm::Type::Int64Ty; 915e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); 916e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); 917e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 918e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // cast val v2i64 919e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); 920e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 921e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // extract (0, 1) 922e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; 923e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index); 924e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); 925e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 926e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // cast pointer to i64 & store 927e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); 928e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateStore(Ops[1], Ops[0]); 929e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 930e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_loadlv4si: { 931e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // load i64 932e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const llvm::Type *EltTy = llvm::Type::Int64Ty; 933e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); 934e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); 935e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateLoad(Ops[0], "load"); 936e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 937e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // scalar to vector: insert i64 into 2 x i64 undef 938e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); 939e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); 940e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateInsertElement(llvm::UndefValue::get(VecTy), 941e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0], Zero, "s2v"); 942e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 943e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // shuffle into zero vector. 944e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman std::vector<llvm::Constant *>Elts; 945e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Elts.resize(2, llvm::ConstantInt::get(EltTy, 0)); 946e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Value *ZV = ConstantVector::get(Elts); 947e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = EmitShuffleVector(ZV, Ops[0], 2, 1, "loadl"); 948e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 949e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // bitcast to result. 950e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateBitCast(Ops[0], 951e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::VectorType::get(llvm::Type::Int32Ty, 4)); 952e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 95324512501319aa6d7f70b1bd79470df0197b832c8Nate Begeman case X86::BI__builtin_ia32_vec_set_v4hi: 95424512501319aa6d7f70b1bd79470df0197b832c8Nate Begeman case X86::BI__builtin_ia32_vec_set_v8hi: 95524512501319aa6d7f70b1bd79470df0197b832c8Nate Begeman return Builder.CreateInsertElement(Ops[0], Ops[1], Ops[2], "pinsrw"); 956bc4f4030e245e36642f894d848f6a8bf94426cc2Mon P Wang case X86::BI__builtin_ia32_vec_set_v4si: 957bc4f4030e245e36642f894d848f6a8bf94426cc2Mon P Wang return Builder.CreateInsertElement(Ops[0], Ops[1], Ops[2], "pinsrd"); 958bc4f4030e245e36642f894d848f6a8bf94426cc2Mon P Wang case X86::BI__builtin_ia32_vec_set_v2di: 959bc4f4030e245e36642f894d848f6a8bf94426cc2Mon P Wang return Builder.CreateInsertElement(Ops[0], Ops[1], Ops[2], "pinsrq"); 960e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_andps: 961e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_andpd: 962e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_andnps: 963e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_andnpd: 964e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_orps: 965e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_orpd: 966e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_xorpd: 967e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_xorps: { 968e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const llvm::Type *ITy = llvm::VectorType::get(llvm::Type::Int32Ty, 4); 969e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const llvm::Type *FTy = Ops[0]->getType(); 970e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateBitCast(Ops[0], ITy, "bitcast"); 971e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateBitCast(Ops[1], ITy, "bitcast"); 972e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman switch (BuiltinID) { 973e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_andps: 974e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andps"); 975e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 976e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_andpd: 977e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andpd"); 978e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 979e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_andnps: 980e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateNot(Ops[0], "not"); 981e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andnps"); 982e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 983e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_andnpd: 984e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateNot(Ops[0], "not"); 985e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateAnd(Ops[0], Ops[1], "andnpd"); 986e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 987e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_orps: 988e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateOr(Ops[0], Ops[1], "orps"); 989e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 990e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_orpd: 991e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateOr(Ops[0], Ops[1], "orpd"); 992e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 993e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_xorps: 994e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateXor(Ops[0], Ops[1], "xorps"); 995e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 996e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_xorpd: 997e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateXor(Ops[0], Ops[1], "xorpd"); 998e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 999e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 1000e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateBitCast(Ops[0], FTy, "bitcast"); 1001e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 1002564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson } 1003564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson} 1004564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson 10051feedd84221e8dbcc3faf3de27cc42b559db845dChris LattnerValue *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, 10061feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner const CallExpr *E) { 1007564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson switch (BuiltinID) { 100846a26b0b0e2ec1557bad9b70e8e20836524ebdfcAnders Carlsson default: return 0; 1009564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson } 1010564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson} 1011