CGBuiltin.cpp revision 69243825cb5c91ec7207256aa57ae327cfaf8cb2
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" 206b15cdc1312f8fc45c86ee75e2a85106700e97f6Chris Lattner#include "clang/Basic/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 = 10669243825cb5c91ec7207256aa57ae327cfaf8cb2Owen Anderson Builder.CreateICmpSGE(ArgValue, 10769243825cb5c91ec7207256aa57ae327cfaf8cb2Owen Anderson getLLVMContext().getNullValue(ArgValue->getType()), 1089a847f598c4907a72b8593b364b9e6b94b086e75Chris Lattner "abscond"); 1091feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner Value *Result = 110c2251dc59b0edc28f9303637dec970a7520939adAnders Carlsson Builder.CreateSelect(CmpResult, ArgValue, NegOp, "abs"); 111c2251dc59b0edc28f9303637dec970a7520939adAnders Carlsson 112c2251dc59b0edc28f9303637dec970a7520939adAnders Carlsson return RValue::get(Result); 113c2251dc59b0edc28f9303637dec970a7520939adAnders Carlsson } 1143a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson case Builtin::BI__builtin_ctz: 1153a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson case Builtin::BI__builtin_ctzl: 1163a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson case Builtin::BI__builtin_ctzll: { 1173a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson Value *ArgValue = EmitScalarExpr(E->getArg(0)); 1183a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson 1193a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson const llvm::Type *ArgType = ArgValue->getType(); 1203a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); 1213a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson 1223a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson const llvm::Type *ResultType = ConvertType(E->getType()); 1233a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); 1243a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson if (Result->getType() != ResultType) 1253a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson Result = Builder.CreateIntCast(Result, ResultType, "cast"); 1263a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson return RValue::get(Result); 1273a31d60cffedfb7c9e6d129a5c9ba15fa74f179aAnders Carlsson } 128f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman case Builtin::BI__builtin_clz: 129f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman case Builtin::BI__builtin_clzl: 130f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman case Builtin::BI__builtin_clzll: { 131f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman Value *ArgValue = EmitScalarExpr(E->getArg(0)); 132f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman 133f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman const llvm::Type *ArgType = ArgValue->getType(); 134f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman Value *F = CGM.getIntrinsic(Intrinsic::ctlz, &ArgType, 1); 135f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman 136f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman const llvm::Type *ResultType = ConvertType(E->getType()); 137f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); 138f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman if (Result->getType() != ResultType) 139f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman Result = Builder.CreateIntCast(Result, ResultType, "cast"); 140f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman return RValue::get(Result); 141f4e853340590d5c32e58379e8c379ea1777d3101Eli Friedman } 14204b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_ffs: 14304b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_ffsl: 14404b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_ffsll: { 14504b290030eee33295600728450f348989d1a627eDaniel Dunbar // ffs(x) -> x ? cttz(x) + 1 : 0 14604b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *ArgValue = EmitScalarExpr(E->getArg(0)); 14704b290030eee33295600728450f348989d1a627eDaniel Dunbar 14804b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ArgType = ArgValue->getType(); 14904b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::cttz, &ArgType, 1); 15004b290030eee33295600728450f348989d1a627eDaniel Dunbar 15104b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ResultType = ConvertType(E->getType()); 15204b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Tmp = Builder.CreateAdd(Builder.CreateCall(F, ArgValue, "tmp"), 15304b290030eee33295600728450f348989d1a627eDaniel Dunbar ConstantInt::get(ArgType, 1), "tmp"); 15469243825cb5c91ec7207256aa57ae327cfaf8cb2Owen Anderson Value *Zero = getLLVMContext().getNullValue(ArgType); 15504b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); 15604b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs"); 15704b290030eee33295600728450f348989d1a627eDaniel Dunbar if (Result->getType() != ResultType) 15804b290030eee33295600728450f348989d1a627eDaniel Dunbar Result = Builder.CreateIntCast(Result, ResultType, "cast"); 15904b290030eee33295600728450f348989d1a627eDaniel Dunbar return RValue::get(Result); 16004b290030eee33295600728450f348989d1a627eDaniel Dunbar } 16104b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_parity: 16204b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_parityl: 16304b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_parityll: { 16404b290030eee33295600728450f348989d1a627eDaniel Dunbar // parity(x) -> ctpop(x) & 1 16504b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *ArgValue = EmitScalarExpr(E->getArg(0)); 16604b290030eee33295600728450f348989d1a627eDaniel Dunbar 16704b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ArgType = ArgValue->getType(); 16804b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); 16904b290030eee33295600728450f348989d1a627eDaniel Dunbar 17004b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ResultType = ConvertType(E->getType()); 17104b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Tmp = Builder.CreateCall(F, ArgValue, "tmp"); 17204b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Result = Builder.CreateAnd(Tmp, ConstantInt::get(ArgType, 1), 17304b290030eee33295600728450f348989d1a627eDaniel Dunbar "tmp"); 17404b290030eee33295600728450f348989d1a627eDaniel Dunbar if (Result->getType() != ResultType) 17504b290030eee33295600728450f348989d1a627eDaniel Dunbar Result = Builder.CreateIntCast(Result, ResultType, "cast"); 17604b290030eee33295600728450f348989d1a627eDaniel Dunbar return RValue::get(Result); 17704b290030eee33295600728450f348989d1a627eDaniel Dunbar } 17804b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_popcount: 17904b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_popcountl: 18004b290030eee33295600728450f348989d1a627eDaniel Dunbar case Builtin::BI__builtin_popcountll: { 18104b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *ArgValue = EmitScalarExpr(E->getArg(0)); 18204b290030eee33295600728450f348989d1a627eDaniel Dunbar 18304b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ArgType = ArgValue->getType(); 18404b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::ctpop, &ArgType, 1); 18504b290030eee33295600728450f348989d1a627eDaniel Dunbar 18604b290030eee33295600728450f348989d1a627eDaniel Dunbar const llvm::Type *ResultType = ConvertType(E->getType()); 18704b290030eee33295600728450f348989d1a627eDaniel Dunbar Value *Result = Builder.CreateCall(F, ArgValue, "tmp"); 18804b290030eee33295600728450f348989d1a627eDaniel Dunbar if (Result->getType() != ResultType) 18904b290030eee33295600728450f348989d1a627eDaniel Dunbar Result = Builder.CreateIntCast(Result, ResultType, "cast"); 19004b290030eee33295600728450f348989d1a627eDaniel Dunbar return RValue::get(Result); 19104b290030eee33295600728450f348989d1a627eDaniel Dunbar } 1921feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner case Builtin::BI__builtin_expect: 193a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar // FIXME: pass expect through to LLVM 1941feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner return RValue::get(EmitScalarExpr(E->getArg(0))); 195df4852ac816e6050d53b808b86d7c1c9738eb99eAnders Carlsson case Builtin::BI__builtin_bswap32: 196df4852ac816e6050d53b808b86d7c1c9738eb99eAnders Carlsson case Builtin::BI__builtin_bswap64: { 1971feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner Value *ArgValue = EmitScalarExpr(E->getArg(0)); 198df4852ac816e6050d53b808b86d7c1c9738eb99eAnders Carlsson const llvm::Type *ArgType = ArgValue->getType(); 1997acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1); 2001feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner return RValue::get(Builder.CreateCall(F, ArgValue, "tmp")); 2014493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar } 202d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar case Builtin::BI__builtin_object_size: { 203d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar // FIXME: Implement. For now we just always fail and pretend we 204d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar // don't know the object size. 2059a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman llvm::APSInt TypeArg = E->getArg(1)->EvaluateAsInt(CGM.getContext()); 206d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar const llvm::Type *ResType = ConvertType(E->getType()); 207d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar // bool UseSubObject = TypeArg.getZExtValue() & 1; 208d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar bool UseMinimum = TypeArg.getZExtValue() & 2; 209d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar return RValue::get(ConstantInt::get(ResType, UseMinimum ? 0 : -1LL)); 210d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar } 2114493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar case Builtin::BI__builtin_prefetch: { 2124493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0)); 2134493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar // FIXME: Technically these constants should of type 'int', yes? 2144493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) : 2154493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar ConstantInt::get(llvm::Type::Int32Ty, 0); 2164493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : 2174493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar ConstantInt::get(llvm::Type::Int32Ty, 3); 2184493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0); 2194493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar return RValue::get(Builder.CreateCall3(F, Address, RW, Locality)); 2204493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar } 2214493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar case Builtin::BI__builtin_trap: { 2224493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0); 2234493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar return RValue::get(Builder.CreateCall(F)); 224df4852ac816e6050d53b808b86d7c1c9738eb99eAnders Carlsson } 2254493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar 226a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar case Builtin::BI__builtin_powi: 227a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar case Builtin::BI__builtin_powif: 228a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar case Builtin::BI__builtin_powil: { 229a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar Value *Base = EmitScalarExpr(E->getArg(0)); 230a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar Value *Exponent = EmitScalarExpr(E->getArg(1)); 231a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar const llvm::Type *ArgType = Base->getType(); 232a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::powi, &ArgType, 1); 233a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); 234a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar } 235a933c3c052bbd87b01cc6fc7a7745e1c4b1757fbDaniel Dunbar 236fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isgreater: 237fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isgreaterequal: 238fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isless: 239fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_islessequal: 240fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_islessgreater: 241fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isunordered: { 242fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner // Ordered comparisons: we know the arguments to these are matching scalar 243fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner // floating point values. 244fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner Value *LHS = EmitScalarExpr(E->getArg(0)); 245fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner Value *RHS = EmitScalarExpr(E->getArg(1)); 246fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner 247fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner switch (BuiltinID) { 248fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner default: assert(0 && "Unknown ordered comparison"); 249fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isgreater: 250fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp"); 251fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 252fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isgreaterequal: 253fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp"); 254fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 255fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isless: 256fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp"); 257fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 258fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_islessequal: 259fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp"); 260fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 261fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_islessgreater: 262fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp"); 263fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 264fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner case Builtin::BI__builtin_isunordered: 265fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp"); 266fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner break; 267fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner } 268fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner // ZExt bool to int type. 269fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType()), 270fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner "tmp")); 271fe23e217774aaec350086fab839210d7d9e1e3f4Chris Lattner } 272b52fe9ce99970955a5f581f5c66fcd89be9a268bEli Friedman case Builtin::BIalloca: 2739e800e3dd80d77f6c47054738177bf824089f55aChris Lattner case Builtin::BI__builtin_alloca: { 2749e800e3dd80d77f6c47054738177bf824089f55aChris Lattner // FIXME: LLVM IR Should allow alloca with an i64 size! 2759e800e3dd80d77f6c47054738177bf824089f55aChris Lattner Value *Size = EmitScalarExpr(E->getArg(0)); 2769e800e3dd80d77f6c47054738177bf824089f55aChris Lattner Size = Builder.CreateIntCast(Size, llvm::Type::Int32Ty, false, "tmp"); 2771caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar return RValue::get(Builder.CreateAlloca(llvm::Type::Int8Ty, Size, "tmp")); 2781caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar } 2791caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar case Builtin::BI__builtin_bzero: { 2801caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Value *Address = EmitScalarExpr(E->getArg(0)); 2811caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Builder.CreateCall4(CGM.getMemSetFn(), Address, 2821caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar llvm::ConstantInt::get(llvm::Type::Int8Ty, 0), 2831caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(1)), 2841caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); 2851caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar return RValue::get(Address); 2869e800e3dd80d77f6c47054738177bf824089f55aChris Lattner } 287d4b32e46517358f34e8cfbea35010adfcc3786e0Eli Friedman case Builtin::BI__builtin_memcpy: { 2881caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Value *Address = EmitScalarExpr(E->getArg(0)); 2891caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Builder.CreateCall4(CGM.getMemCpyFn(), Address, 2901caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(1)), 2911caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(2)), 2921caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); 2931caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar return RValue::get(Address); 2941caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar } 2951caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar case Builtin::BI__builtin_memmove: { 2961caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Value *Address = EmitScalarExpr(E->getArg(0)); 2971caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Builder.CreateCall4(CGM.getMemMoveFn(), Address, 2981caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(1)), 2991caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(2)), 3001caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); 3011caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar return RValue::get(Address); 3021caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar } 3031caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar case Builtin::BI__builtin_memset: { 3041caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Value *Address = EmitScalarExpr(E->getArg(0)); 3051caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar Builder.CreateCall4(CGM.getMemSetFn(), Address, 30662c29c667f5bcb854e99ccb3aec1280c84115951Daniel Dunbar Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)), 30762c29c667f5bcb854e99ccb3aec1280c84115951Daniel Dunbar llvm::Type::Int8Ty), 3081caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar EmitScalarExpr(E->getArg(2)), 3091caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); 3101caae959017b355e9bb61250d5a0d04edbf468b0Daniel Dunbar return RValue::get(Address); 311d4b32e46517358f34e8cfbea35010adfcc3786e0Eli Friedman } 312256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman case Builtin::BI__builtin_return_address: { 313256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman Value *F = CGM.getIntrinsic(Intrinsic::returnaddress, 0, 0); 314256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0)))); 315256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman } 316256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman case Builtin::BI__builtin_frame_address: { 317256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman Value *F = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); 318256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman return RValue::get(Builder.CreateCall(F, EmitScalarExpr(E->getArg(0)))); 319256f77e431bc6b920ec94cf0bb4ad339ca21b8c9Eli Friedman } 3203b660efb9f9fa3e87096f4a96a2093cd17c43c2eEli Friedman case Builtin::BI__builtin_extract_return_addr: { 3213b660efb9f9fa3e87096f4a96a2093cd17c43c2eEli Friedman // FIXME: There should be a target hook for this 3223b660efb9f9fa3e87096f4a96a2093cd17c43c2eEli Friedman return RValue::get(EmitScalarExpr(E->getArg(0))); 3233b660efb9f9fa3e87096f4a96a2093cd17c43c2eEli Friedman } 324a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman case Builtin::BI__builtin_unwind_init: { 325a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Value *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init, 0, 0); 326a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman return RValue::get(Builder.CreateCall(F)); 327a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman } 328a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman#if 0 329a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman // FIXME: Finish/enable when LLVM backend support stabilizes 330a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman case Builtin::BI__builtin_setjmp: { 331a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Value *Buf = EmitScalarExpr(E->getArg(0)); 332a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman // Store the frame pointer to the buffer 333a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Value *FrameAddrF = CGM.getIntrinsic(Intrinsic::frameaddress, 0, 0); 334a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Value *FrameAddr = 335a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Builder.CreateCall(FrameAddrF, 336a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Constant::getNullValue(llvm::Type::Int32Ty)); 337a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Builder.CreateStore(FrameAddr, Buf); 338a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman // Call the setjmp intrinsic 339a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp, 0, 0); 340a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman const llvm::Type *DestType = 341a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 342a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Buf = Builder.CreateBitCast(Buf, DestType); 343a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman return RValue::get(Builder.CreateCall(F, Buf)); 344a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman } 345a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman case Builtin::BI__builtin_longjmp: { 346a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Value *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp, 0, 0); 347a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Value *Buf = EmitScalarExpr(E->getArg(0)); 348a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman const llvm::Type *DestType = 349a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 350a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman Buf = Builder.CreateBitCast(Buf, DestType); 351a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman return RValue::get(Builder.CreateCall(F, Buf)); 352a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman } 353a6d75c0324ac690107bbaa7193b526ef21466212Eli Friedman#endif 3541ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_add: 3551ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_sub: 3565caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or: 3575caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and: 3585caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor: 3595caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch: 3605caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch: 3615caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch: 3625caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch: 3635caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch: 3645caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap: 3655caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap: 3665caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set: 3675caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release: 3685caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner assert(0 && "Shouldn't make it through sema"); 3695caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add_1: 3705caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add_2: 3715caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add_4: 3725caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add_8: 3735caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add_16: 3745caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner return EmitBinaryAtomic(*this, Intrinsic::atomic_load_add, E); 3755caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub_1: 3765caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub_2: 3775caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub_4: 3785caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub_8: 3795caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub_16: 38009b6bf5bfa2ba9cbbd353cbd7846af8f49b020e7Mon P Wang return EmitBinaryAtomic(*this, Intrinsic::atomic_load_sub, E); 3815caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or_1: 3825caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or_2: 3835caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or_4: 3845caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or_8: 3855caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or_16: 3865caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner return EmitBinaryAtomic(*this, Intrinsic::atomic_load_or, E); 3875caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and_1: 3885caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and_2: 3895caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and_4: 3905caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and_8: 3915caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and_16: 3925caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner return EmitBinaryAtomic(*this, Intrinsic::atomic_load_and, E); 3935caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor_1: 3945caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor_2: 3955caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor_4: 3965caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor_8: 3975caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor_16: 3985caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner return EmitBinaryAtomic(*this, Intrinsic::atomic_load_xor, E); 399eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_fetch_and_nand_1: 400eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_fetch_and_nand_2: 401eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_fetch_and_nand_4: 402eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_fetch_and_nand_8: 403eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_fetch_and_nand_16: 404eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner return EmitBinaryAtomic(*this, Intrinsic::atomic_load_nand, E); 405eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner 4065caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // Clang extensions: not overloaded yet. 4071ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_min: 4081ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang return EmitBinaryAtomic(*this, Intrinsic::atomic_load_min, E); 4091ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_max: 4101ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang return EmitBinaryAtomic(*this, Intrinsic::atomic_load_max, E); 4111ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_umin: 4121ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umin, E); 4131ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang case Builtin::BI__sync_fetch_and_umax: 4141ffe281890f3cd7728316b45a1f3dd4d3120af7bMon P Wang return EmitBinaryAtomic(*this, Intrinsic::atomic_load_umax, E); 4150002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar 4165caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch_1: 4175caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch_2: 4185caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch_4: 4195caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch_8: 4205caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch_16: 4210002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_add, E, 4220002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar llvm::Instruction::Add); 4235caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch_1: 4245caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch_2: 4255caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch_4: 4265caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch_8: 4275caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch_16: 4280002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_sub, E, 4290002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar llvm::Instruction::Sub); 4305caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch_1: 4315caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch_2: 4325caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch_4: 4335caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch_8: 4345caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch_16: 4350002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_and, E, 4360002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar llvm::Instruction::And); 4375caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch_1: 4385caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch_2: 4395caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch_4: 4405caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch_8: 4415caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch_16: 4420002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_or, E, 4430002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar llvm::Instruction::Or); 4445caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch_1: 4455caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch_2: 4465caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch_4: 4475caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch_8: 4485caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch_16: 4490002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_xor, E, 4500002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar llvm::Instruction::Xor); 451eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_nand_and_fetch_1: 452eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_nand_and_fetch_2: 453eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_nand_and_fetch_4: 454eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_nand_and_fetch_8: 455eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner case Builtin::BI__sync_nand_and_fetch_16: 456eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner return EmitBinaryAtomicPost(*this, Intrinsic::atomic_load_nand, E, 457eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner llvm::Instruction::And); 458eebd9d2165a705ce6a8a6b6a7327864189d02785Chris Lattner 4595caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap_1: 4605caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap_2: 4615caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap_4: 4625caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap_8: 4635caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap_16: 4645caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner { 465c5004516489c931cf839bdbdde5a76e4074cc888Mon P Wang const llvm::Type *ResType[2]; 466c5004516489c931cf839bdbdde5a76e4074cc888Mon P Wang ResType[0]= ConvertType(E->getType()); 467c5004516489c931cf839bdbdde5a76e4074cc888Mon P Wang ResType[1] = ConvertType(E->getArg(0)->getType()); 468c5004516489c931cf839bdbdde5a76e4074cc888Mon P Wang Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); 469958444659c5287a81a43188bfaa2a4902752b947Anders Carlsson return RValue::get(Builder.CreateCall3(AtomF, 470958444659c5287a81a43188bfaa2a4902752b947Anders Carlsson EmitScalarExpr(E->getArg(0)), 471958444659c5287a81a43188bfaa2a4902752b947Anders Carlsson EmitScalarExpr(E->getArg(1)), 472958444659c5287a81a43188bfaa2a4902752b947Anders Carlsson EmitScalarExpr(E->getArg(2)))); 473022012e6e5626c3372e1a5493c0929dfc1fa9e47Anders Carlsson } 4740002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar 4755caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap_1: 4765caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap_2: 4775caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap_4: 4785caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap_8: 4795caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap_16: 4805caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner { 4810002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar const llvm::Type *ResType[2]; 4825caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner ResType[0]= ConvertType(E->getArg(1)->getType()); 4835caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner ResType[1] = llvm::PointerType::getUnqual(ResType[0]); 4840002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap, ResType, 2); 4850002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *OldVal = EmitScalarExpr(E->getArg(1)); 4860002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *PrevVal = Builder.CreateCall3(AtomF, 4870002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar EmitScalarExpr(E->getArg(0)), 4880002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar OldVal, 4890002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar EmitScalarExpr(E->getArg(2))); 4900002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal); 4910002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar // zext bool to int. 4920002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType()))); 4930002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar } 4940002d23aaf10f307273dab5facda01c137283d22Daniel Dunbar 4955caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set_1: 4965caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set_2: 4975caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set_4: 4985caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set_8: 4995caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set_16: 5007ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman return EmitBinaryAtomic(*this, Intrinsic::atomic_swap, E); 5015caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release_1: 5025caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release_2: 5035caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release_4: 5045caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release_8: 505f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner case Builtin::BI__sync_lock_release_16: { 506f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner Value *Ptr = EmitScalarExpr(E->getArg(0)); 507f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner const llvm::Type *ElTy = 508f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner cast<llvm::PointerType>(Ptr->getType())->getElementType(); 50969243825cb5c91ec7207256aa57ae327cfaf8cb2Owen Anderson Builder.CreateStore(getLLVMContext().getNullValue(ElTy), Ptr, true); 510eb4f81e174b11633f7b85f555ea5d2834d6dae8aDaniel Dunbar return RValue::get(0); 511f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner } 512ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar 513f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner case Builtin::BI__sync_synchronize: { 514f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner Value *C[5]; 515f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner C[0] = C[1] = C[2] = C[3] = llvm::ConstantInt::get(llvm::Type::Int1Ty, 1); 516f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner C[4] = ConstantInt::get(llvm::Type::Int1Ty, 0); 517f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner Builder.CreateCall(CGM.getIntrinsic(Intrinsic::memory_barrier), C, C + 5); 518eb4f81e174b11633f7b85f555ea5d2834d6dae8aDaniel Dunbar return RValue::get(0); 519f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner } 520f58cd9bca9c2e7373300fc8bb7c57cff7e4eda4fChris Lattner 521ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar // Library functions with special handling. 522ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIsqrt: 523ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIsqrtf: 524ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIsqrtl: { 525ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar // Rewrite sqrt to intrinsic if allowed. 52640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (!FD->hasAttr<ConstAttr>()) 527ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar break; 528ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar Value *Arg0 = EmitScalarExpr(E->getArg(0)); 529ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar const llvm::Type *ArgType = Arg0->getType(); 530ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1); 531ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar return RValue::get(Builder.CreateCall(F, Arg0, "tmp")); 532ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar } 533ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar 534ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIpow: 535ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIpowf: 536ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar case Builtin::BIpowl: { 537ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar // Rewrite sqrt to intrinsic if allowed. 53840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (!FD->hasAttr<ConstAttr>()) 539ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar break; 540ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar Value *Base = EmitScalarExpr(E->getArg(0)); 541ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar Value *Exponent = EmitScalarExpr(E->getArg(1)); 542ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar const llvm::Type *ArgType = Base->getType(); 543ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar Value *F = CGM.getIntrinsic(Intrinsic::pow, &ArgType, 1); 544ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar return RValue::get(Builder.CreateCall2(F, Base, Exponent, "tmp")); 545ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar } 5467ea2e3f6aae9b7511686d3d26dee690fee81c3aaNate Begeman } 547b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 548b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // If this is an alias for a libm function (e.g. __builtin_sin) turn it into 549b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // that function. 5503e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (getContext().BuiltinInfo.isLibFunction(BuiltinID) || 5513e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID)) 552986477149db76e6d9f8d3c9859c9880c11b680aeAnders Carlsson return EmitCall(CGM.getBuiltinLibFunction(BuiltinID), 553986477149db76e6d9f8d3c9859c9880c11b680aeAnders Carlsson E->getCallee()->getType(), E->arg_begin(), 554986477149db76e6d9f8d3c9859c9880c11b680aeAnders Carlsson E->arg_end()); 555b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 556b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // See if we have a target specific intrinsic. 557a6f80ef997f0363386749087b325607eaa5adcfcDale Johannesen const char *Name = getContext().BuiltinInfo.GetName(BuiltinID); 558a6f80ef997f0363386749087b325607eaa5adcfcDale Johannesen Intrinsic::ID IntrinsicID = 559a6f80ef997f0363386749087b325607eaa5adcfcDale Johannesen Intrinsic::getIntrinsicForGCCBuiltin(Target.getTargetPrefix(), Name); 560b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 561b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (IntrinsicID != Intrinsic::not_intrinsic) { 562b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner SmallVector<Value*, 16> Args; 563b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 564b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner Function *F = CGM.getIntrinsic(IntrinsicID); 565b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner const llvm::FunctionType *FTy = F->getFunctionType(); 566b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 567b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) { 568b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner Value *ArgValue = EmitScalarExpr(E->getArg(i)); 569b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 570b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // If the intrinsic arg type is different from the builtin arg type 571b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // we need to do a bit cast. 572b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner const llvm::Type *PTy = FTy->getParamType(i); 573b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (PTy != ArgValue->getType()) { 574b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) && 575b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner "Must be able to losslessly bit cast to param"); 576b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner ArgValue = Builder.CreateBitCast(ArgValue, PTy); 577b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner } 578b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 579b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner Args.push_back(ArgValue); 580b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner } 581b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 582beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad Value *V = Builder.CreateCall(F, Args.data(), Args.data() + Args.size()); 583b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner QualType BuiltinRetType = E->getType(); 584b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 585b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner const llvm::Type *RetTy = llvm::Type::VoidTy; 586b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (!BuiltinRetType->isVoidType()) RetTy = ConvertType(BuiltinRetType); 587b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 588b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (RetTy != V->getType()) { 589b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner assert(V->getType()->canLosslesslyBitCastTo(RetTy) && 590b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner "Must be able to losslessly bit cast result type"); 591b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner V = Builder.CreateBitCast(V, RetTy); 592b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner } 593b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 594b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner return RValue::get(V); 595b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner } 596b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 597b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // See if we have a target specific builtin that needs to be lowered. 598f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar if (Value *V = EmitTargetBuiltinExpr(BuiltinID, E)) 599b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner return RValue::get(V); 600b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 601488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar ErrorUnsupported(E, "builtin function"); 602b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 603b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner // Unknown builtin, for now just dump it out and return undef. 604b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (hasAggregateLLVMType(E->getType())) 605b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType()))); 606b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner return RValue::get(UndefValue::get(ConvertType(E->getType()))); 607b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner} 608564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson 609f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel DunbarValue *CodeGenFunction::EmitTargetBuiltinExpr(unsigned BuiltinID, 610f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar const CallExpr *E) { 611f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar const char *TargetPrefix = Target.getTargetPrefix(); 612f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar if (strcmp(TargetPrefix, "x86") == 0) 613f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar return EmitX86BuiltinExpr(BuiltinID, E); 614f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar else if (strcmp(TargetPrefix, "ppc") == 0) 615f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar return EmitPPCBuiltinExpr(BuiltinID, E); 616f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar return 0; 617f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar} 618f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar 6191feedd84221e8dbcc3faf3de27cc42b559db845dChris LattnerValue *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID, 6201feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner const CallExpr *E) { 6212929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson 6222929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson llvm::SmallVector<Value*, 4> Ops; 6232929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson 6242929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) 6252929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson Ops.push_back(EmitScalarExpr(E->getArg(i))); 6262929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson 627564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson switch (BuiltinID) { 62846a26b0b0e2ec1557bad9b70e8e20836524ebdfcAnders Carlsson default: return 0; 629e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_pslldi128: 630e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psllqi128: 631e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psllwi128: 632e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psradi128: 633e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrawi128: 634e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrldi128: 635e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrlqi128: 636e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrlwi128: { 637e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); 638e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 2); 639e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Value *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); 640e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateInsertElement(llvm::UndefValue::get(Ty), 641e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1], Zero, "insert"); 642e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateBitCast(Ops[1], Ops[0]->getType(), "bitcast"); 643e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const char *name = 0; 644e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Intrinsic::ID ID = Intrinsic::not_intrinsic; 645e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 646e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman switch (BuiltinID) { 647e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman default: assert(0 && "Unsupported shift intrinsic!"); 648e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_pslldi128: 649e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "pslldi"; 650e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psll_d; 651e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 652e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psllqi128: 653e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psllqi"; 654e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psll_q; 655e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 656e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psllwi128: 657e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psllwi"; 658e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psll_w; 659e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 660e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psradi128: 661e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psradi"; 662e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psra_d; 663e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 664e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrawi128: 665e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psrawi"; 666e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psra_w; 667e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 668e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrldi128: 669e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psrldi"; 670e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psrl_d; 671e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 672e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrlqi128: 673e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psrlqi"; 674e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psrl_q; 675e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 676e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_psrlwi128: 677e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman name = "psrlwi"; 678e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman ID = Intrinsic::x86_sse2_psrl_w; 679e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman break; 680e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 681e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Function *F = CGM.getIntrinsic(ID); 682e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); 683e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 6842929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_pslldi: 6852929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psllqi: 6862929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psllwi: 6872929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psradi: 6882929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrawi: 6892929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrldi: 6902929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrlqi: 6912929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrlwi: { 6922929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson Ops[1] = Builder.CreateZExt(Ops[1], llvm::Type::Int64Ty, "zext"); 6932929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson const llvm::Type *Ty = llvm::VectorType::get(llvm::Type::Int64Ty, 1); 6942929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson Ops[1] = Builder.CreateBitCast(Ops[1], Ty, "bitcast"); 6952929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson const char *name = 0; 6962929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson Intrinsic::ID ID = Intrinsic::not_intrinsic; 6974e4ee211b56e3276df80b350a6194f7cbf04b92cAnders Carlsson 6982929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson switch (BuiltinID) { 6992929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson default: assert(0 && "Unsupported shift intrinsic!"); 7002929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_pslldi: 7012929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "pslldi"; 7022929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psll_d; 7032929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7042929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psllqi: 7052929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psllqi"; 7062929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psll_q; 7072929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7082929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psllwi: 7092929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psllwi"; 7102929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psll_w; 7112929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7122929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psradi: 7132929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psradi"; 7142929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psra_d; 7152929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7162929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrawi: 7172929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psrawi"; 7182929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psra_w; 7192929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7202929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrldi: 7212929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psrldi"; 7222929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psrl_d; 7232929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7242929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrlqi: 7252929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psrlqi"; 7262929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psrl_q; 7272929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7282929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson case X86::BI__builtin_ia32_psrlwi: 7292929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson name = "psrlwi"; 7302929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson ID = Intrinsic::x86_mmx_psrl_w; 7312929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson break; 7322929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson } 7337acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner llvm::Function *F = CGM.getIntrinsic(ID); 7342929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), name); 7352929cfa9b7df1d5b0571b54161783e4d791a0b77Anders Carlsson } 73679dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson case X86::BI__builtin_ia32_cmpps: { 73779dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ps); 73879dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpps"); 73979dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson } 74079dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson case X86::BI__builtin_ia32_cmpss: { 74179dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse_cmp_ss); 74279dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpss"); 743cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson } 744e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_ldmxcsr: { 745e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 746e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Value *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); 747e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp"); 748e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Builder.CreateStore(Ops[0], Tmp); 749e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_ldmxcsr), 7503eae03e7d165f20a863a9a4d7122ba2a691ab16dChris Lattner Builder.CreateBitCast(Tmp, PtrTy)); 751e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 752e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_stmxcsr: { 753e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 754e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Value *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); 755e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Value *Tmp = Builder.CreateAlloca(llvm::Type::Int32Ty, One, "tmp"); 756e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman One = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse_stmxcsr), 7573eae03e7d165f20a863a9a4d7122ba2a691ab16dChris Lattner Builder.CreateBitCast(Tmp, PtrTy)); 758e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateLoad(Tmp, "stmxcsr"); 759e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 76079dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson case X86::BI__builtin_ia32_cmppd: { 76179dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_pd); 76279dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmppd"); 76379dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson } 76479dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson case X86::BI__builtin_ia32_cmpsd: { 76579dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson llvm::Function *F = CGM.getIntrinsic(Intrinsic::x86_sse2_cmp_sd); 76679dcf5f96ad75259867d19bbf69512f320032fceAnders Carlsson return Builder.CreateCall(F, &Ops[0], &Ops[0] + Ops.size(), "cmpsd"); 767cc8b7f911892b3257e381677d0a82d43bf7b067cAnders Carlsson } 768e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_storehps: 769e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman case X86::BI__builtin_ia32_storelps: { 770e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman const llvm::Type *EltTy = llvm::Type::Int64Ty; 771e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *PtrTy = llvm::PointerType::getUnqual(EltTy); 772e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Type *VecTy = llvm::VectorType::get(EltTy, 2); 773e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 774e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // cast val v2i64 775e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateBitCast(Ops[1], VecTy, "cast"); 776e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 777e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // extract (0, 1) 778e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman unsigned Index = BuiltinID == X86::BI__builtin_ia32_storelps ? 0 : 1; 779e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Index); 780e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[1] = Builder.CreateExtractElement(Ops[1], Idx, "extract"); 781e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman 782e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman // cast pointer to i64 & store 783e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman Ops[0] = Builder.CreateBitCast(Ops[0], PtrTy); 784e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman return Builder.CreateStore(Ops[1], Ops[0]); 785e7722103abc4583366c914374d6aa8560e145fa1Nate Begeman } 786564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson } 787564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson} 788564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson 7891feedd84221e8dbcc3faf3de27cc42b559db845dChris LattnerValue *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, 7901feedd84221e8dbcc3faf3de27cc42b559db845dChris Lattner const CallExpr *E) { 791564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson switch (BuiltinID) { 79246a26b0b0e2ec1557bad9b70e8e20836524ebdfcAnders Carlsson default: return 0; 793564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson } 794564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson} 795