blob: 7ef67d58bb1506f614101be3f0b922eda335e582 [file] [log] [blame]
Chris Lattner3b66ecb2003-12-28 08:19:41 +00001//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3b66ecb2003-12-28 08:19:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3b66ecb2003-12-28 08:19:41 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerb71fd782006-11-15 18:00:10 +000010// This file implements the IntrinsicLowering class.
Chris Lattner3b66ecb2003-12-28 08:19:41 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnercf899082004-02-14 02:47:17 +000014#include "llvm/Constants.h"
Chris Lattner5fe51cc2004-02-12 17:01:09 +000015#include "llvm/DerivedTypes.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000016#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000018#include "llvm/Type.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000019#include "llvm/CodeGen/IntrinsicLowering.h"
20#include "llvm/Support/Streams.h"
Reid Spencer6addf2c2007-01-29 17:42:06 +000021#include "llvm/Target/TargetData.h"
Chris Lattner990b8492007-02-13 06:01:22 +000022#include "llvm/ADT/SmallVector.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000023using namespace llvm;
24
Chris Lattner0979ca72004-05-09 04:29:57 +000025template <class ArgIt>
Chris Lattnerb76efb72007-01-07 08:12:01 +000026static void EnsureFunctionExists(Module &M, const char *Name,
27 ArgIt ArgBegin, ArgIt ArgEnd,
28 const Type *RetTy) {
29 // Insert a correctly-typed definition now.
Chris Lattner0979ca72004-05-09 04:29:57 +000030 std::vector<const Type *> ParamTys;
31 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
32 ParamTys.push_back(I->getType());
Chris Lattnerb76efb72007-01-07 08:12:01 +000033 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
Chris Lattner0979ca72004-05-09 04:29:57 +000034}
35
Chris Lattner588e72d2004-02-15 22:16:39 +000036/// ReplaceCallWith - This function is used when we want to lower an intrinsic
37/// call to a call of an external function. This handles hard cases such as
38/// when there was already a prototype for the external function, and if that
39/// prototype doesn't match the arguments we expect to pass in.
40template <class ArgIt>
41static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
Chris Lattnerb76efb72007-01-07 08:12:01 +000042 ArgIt ArgBegin, ArgIt ArgEnd,
43 const Type *RetTy, Constant *&FCache) {
Chris Lattner588e72d2004-02-15 22:16:39 +000044 if (!FCache) {
45 // If we haven't already looked up this function, check to see if the
46 // program already contains a function with this name.
47 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattnerb76efb72007-01-07 08:12:01 +000048 // Get or insert the definition now.
49 std::vector<const Type *> ParamTys;
50 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
51 ParamTys.push_back((*I)->getType());
52 FCache = M->getOrInsertFunction(NewFn,
53 FunctionType::get(RetTy, ParamTys, false));
Chris Lattner588e72d2004-02-15 22:16:39 +000054 }
Chris Lattner588e72d2004-02-15 22:16:39 +000055
Chris Lattner990b8492007-02-13 06:01:22 +000056 SmallVector<Value*, 8> Operands(ArgBegin, ArgEnd);
57 CallInst *NewCI = new CallInst(FCache, &Operands[0], Operands.size(),
58 CI->getName(), CI);
Chris Lattnerb76efb72007-01-07 08:12:01 +000059 if (!CI->use_empty())
60 CI->replaceAllUsesWith(NewCI);
Chris Lattner02348ca2004-06-11 02:54:02 +000061 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000062}
63
Chris Lattnerb71fd782006-11-15 18:00:10 +000064void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattner0979ca72004-05-09 04:29:57 +000065 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000066 if (I->isDeclaration() && !I->use_empty())
Chris Lattner0979ca72004-05-09 04:29:57 +000067 switch (I->getIntrinsicID()) {
68 default: break;
69 case Intrinsic::setjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000070 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Reid Spencer47857812006-12-31 05:55:36 +000071 Type::Int32Ty);
Chris Lattner0979ca72004-05-09 04:29:57 +000072 break;
73 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000074 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
75 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000076 break;
77 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000078 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
79 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000080 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000081 case Intrinsic::memcpy_i32:
82 case Intrinsic::memcpy_i64:
Reid Spencer1e9126b2007-01-28 22:28:00 +000083 M.getOrInsertFunction("memcpy", PointerType::get(Type::Int8Ty),
84 PointerType::get(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +000085 PointerType::get(Type::Int8Ty),
86 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +000087 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000088 case Intrinsic::memmove_i32:
89 case Intrinsic::memmove_i64:
Reid Spencer1e9126b2007-01-28 22:28:00 +000090 M.getOrInsertFunction("memmove", PointerType::get(Type::Int8Ty),
91 PointerType::get(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +000092 PointerType::get(Type::Int8Ty),
93 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +000094 break;
Chris Lattner03dd4652006-03-03 00:00:25 +000095 case Intrinsic::memset_i32:
96 case Intrinsic::memset_i64:
Reid Spencer47857812006-12-31 05:55:36 +000097 M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +000098 PointerType::get(Type::Int8Ty), Type::Int32Ty,
99 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000100 break;
Reid Spencer0b118202006-01-16 21:12:35 +0000101 case Intrinsic::sqrt_f32:
102 case Intrinsic::sqrt_f64:
Alkis Evlogimenosb1beff02005-04-30 07:13:31 +0000103 if(I->arg_begin()->getType() == Type::FloatTy)
Chris Lattner1f243e92005-05-08 19:46:29 +0000104 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
105 Type::FloatTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000106 else
Chris Lattner1f243e92005-05-08 19:46:29 +0000107 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
108 Type::DoubleTy);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000109 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000110 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000111}
Chris Lattner588e72d2004-02-15 22:16:39 +0000112
Nate Begemane5981812006-01-16 07:57:00 +0000113/// LowerBSWAP - Emit the code to lower bswap of V before the specified
114/// instruction IP.
115static Value *LowerBSWAP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000116 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
Nate Begemane5981812006-01-16 07:57:00 +0000117
Nate Begemane5981812006-01-16 07:57:00 +0000118 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
119
120 switch(BitSize) {
121 default: assert(0 && "Unhandled type size of value to byteswap!");
122 case 16: {
Reid Spencer1b19cd32007-02-02 14:09:34 +0000123 Value *Tmp1 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000124 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000125 Value *Tmp2 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000126 ConstantInt::get(V->getType(),8),"bswap.1",IP);
Nate Begemane5981812006-01-16 07:57:00 +0000127 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
128 break;
129 }
130 case 32: {
Reid Spencer1b19cd32007-02-02 14:09:34 +0000131 Value *Tmp4 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000132 ConstantInt::get(V->getType(),24),"bswap.4", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000133 Value *Tmp3 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000134 ConstantInt::get(V->getType(),8),"bswap.3",IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000135 Value *Tmp2 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000136 ConstantInt::get(V->getType(),8),"bswap.2",IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000137 Value *Tmp1 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000138 ConstantInt::get(V->getType(),24),"bswap.1", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000139 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000140 ConstantInt::get(Type::Int32Ty, 0xFF0000),
Nate Begemane5981812006-01-16 07:57:00 +0000141 "bswap.and3", IP);
142 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000143 ConstantInt::get(Type::Int32Ty, 0xFF00),
Nate Begemane5981812006-01-16 07:57:00 +0000144 "bswap.and2", IP);
145 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
146 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
147 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
148 break;
149 }
150 case 64: {
Reid Spencer1b19cd32007-02-02 14:09:34 +0000151 Value *Tmp8 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000152 ConstantInt::get(V->getType(),56),"bswap.8", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000153 Value *Tmp7 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000154 ConstantInt::get(V->getType(),40),"bswap.7", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000155 Value *Tmp6 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000156 ConstantInt::get(V->getType(),24),"bswap.6", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000157 Value *Tmp5 = BinaryOperator::createShl(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000158 ConstantInt::get(V->getType(),8),"bswap.5", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000159 Value* Tmp4 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000160 ConstantInt::get(V->getType(),8),"bswap.4", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000161 Value* Tmp3 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000162 ConstantInt::get(V->getType(),24),"bswap.3", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000163 Value* Tmp2 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000164 ConstantInt::get(V->getType(),40),"bswap.2", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000165 Value* Tmp1 = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000166 ConstantInt::get(V->getType(),56),"bswap.1", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000167 Tmp7 = BinaryOperator::createAnd(Tmp7,
Reid Spencer47857812006-12-31 05:55:36 +0000168 ConstantInt::get(Type::Int64Ty,
Reid Spencerb83eb642006-10-20 07:07:24 +0000169 0xFF000000000000ULL),
170 "bswap.and7", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000171 Tmp6 = BinaryOperator::createAnd(Tmp6,
Reid Spencer47857812006-12-31 05:55:36 +0000172 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000173 "bswap.and6", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000174 Tmp5 = BinaryOperator::createAnd(Tmp5,
Reid Spencer47857812006-12-31 05:55:36 +0000175 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000176 "bswap.and5", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000177 Tmp4 = BinaryOperator::createAnd(Tmp4,
Reid Spencer47857812006-12-31 05:55:36 +0000178 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000179 "bswap.and4", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000180 Tmp3 = BinaryOperator::createAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000181 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000182 "bswap.and3", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000183 Tmp2 = BinaryOperator::createAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000184 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Reid Spencerb83eb642006-10-20 07:07:24 +0000185 "bswap.and2", IP);
Nate Begemane5981812006-01-16 07:57:00 +0000186 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
187 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
188 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
189 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
190 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
191 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
192 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
193 break;
194 }
195 }
Nate Begemane5981812006-01-16 07:57:00 +0000196 return V;
197}
198
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000199/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000200/// instruction IP.
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000201static Value *LowerCTPOP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000202 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000203
204 static const uint64_t MaskValues[6] = {
205 0x5555555555555555ULL, 0x3333333333333333ULL,
206 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
207 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
208 };
209
Chris Lattner98cf45b2005-05-11 20:24:12 +0000210 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Reid Spencer3822ff52006-11-08 06:47:33 +0000211
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000212 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
Chris Lattner3bb7e3f2006-12-12 05:19:46 +0000213 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000214 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000215 Value *VShift = BinaryOperator::createLShr(V,
Reid Spencer832254e2007-02-02 02:16:23 +0000216 ConstantInt::get(V->getType(), i), "ctpop.sh", IP);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000217 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
218 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
219 }
220
Reid Spencerdc1966e2007-04-02 01:01:49 +0000221 return CastInst::createIntegerCast(V, Type::Int32Ty, false, "ctpop", IP);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000222}
223
Chris Lattner98cf45b2005-05-11 20:24:12 +0000224/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000225/// instruction IP.
Chris Lattner98cf45b2005-05-11 20:24:12 +0000226static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner98cf45b2005-05-11 20:24:12 +0000227
228 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
229 for (unsigned i = 1; i != BitSize; i <<= 1) {
Reid Spencer832254e2007-02-02 02:16:23 +0000230 Value *ShVal = ConstantInt::get(V->getType(), i);
Reid Spencer1b19cd32007-02-02 14:09:34 +0000231 ShVal = BinaryOperator::createLShr(V, ShVal, "ctlz.sh", IP);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000232 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
233 }
234
Chris Lattner98cf45b2005-05-11 20:24:12 +0000235 V = BinaryOperator::createNot(V, "", IP);
236 return LowerCTPOP(V, IP);
237}
238
Reid Spencerf75b8742007-04-12 02:48:46 +0000239/// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes
240/// three integer arguments. The first argument is the Value from which the
241/// bits will be selected. It may be of any bit width. The second and third
242/// arguments specify a range of bits to select with the second argument
243/// specifying the low bit and the third argument specifying the high bit. Both
244/// must be type i32. The result is the corresponding selected bits from the
245/// Value in the same width as the Value (first argument). If the low bit index
246/// is higher than the high bit index then the inverse selection is done and
247/// the bits are returned in inverse order.
248/// @brief Lowering of llvm.part.select intrinsic.
249static Instruction *LowerPartSelect(CallInst *CI) {
Reid Spenceraddd11d2007-04-04 23:48:25 +0000250 // Make sure we're dealing with a part select intrinsic here
251 Function *F = CI->getCalledFunction();
252 const FunctionType *FT = F->getFunctionType();
253 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
254 FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
255 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
256 return CI;
257
258 // Get the intrinsic implementation function by converting all the . to _
259 // in the intrinsic's function name and then reconstructing the function
260 // declaration.
261 std::string Name(F->getName());
262 for (unsigned i = 4; i < Name.length(); ++i)
263 if (Name[i] == '.')
264 Name[i] = '_';
265 Module* M = F->getParent();
266 F = cast<Function>(M->getOrInsertFunction(Name, FT));
267 F->setLinkage(GlobalValue::InternalLinkage);
268
269 // If we haven't defined the impl function yet, do so now
270 if (F->isDeclaration()) {
271
272 // Get the arguments to the function
273 Value* Val = F->getOperand(0);
Reid Spencerf75b8742007-04-12 02:48:46 +0000274 Value* Right = F->getOperand(1);
275 Value* Left = F->getOperand(2);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000276
277 // We want to select a range of bits here such that [Left, Right] is shifted
278 // down to the low bits. However, it is quite possible that Left is smaller
279 // than Right in which case the bits have to be reversed.
280
281 // Create the blocks we will need for the two cases (forward, reverse)
282 BasicBlock* CurBB = new BasicBlock("entry", F);
283 BasicBlock *RevSize = new BasicBlock("revsize", CurBB->getParent());
284 BasicBlock *FwdSize = new BasicBlock("fwdsize", CurBB->getParent());
285 BasicBlock *Compute = new BasicBlock("compute", CurBB->getParent());
286 BasicBlock *Reverse = new BasicBlock("reverse", CurBB->getParent());
287 BasicBlock *RsltBlk = new BasicBlock("result", CurBB->getParent());
288
289 // Cast Left and Right to the size of Val so the widths are all the same
290 if (Left->getType() != Val->getType())
291 Left = CastInst::createIntegerCast(Left, Val->getType(), false,
292 "tmp", CurBB);
293 if (Right->getType() != Val->getType())
294 Right = CastInst::createIntegerCast(Right, Val->getType(), false,
295 "tmp", CurBB);
296
297 // Compute a few things that both cases will need, up front.
298 Constant* Zero = ConstantInt::get(Val->getType(), 0);
299 Constant* One = ConstantInt::get(Val->getType(), 1);
300 Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
301
302 // Compare the Left and Right bit positions. This is used to determine
303 // which case we have (forward or reverse)
304 ICmpInst *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, Left, Right, "less",CurBB);
305 new BranchInst(RevSize, FwdSize, Cmp, CurBB);
306
307 // First, copmute the number of bits in the forward case.
308 Instruction* FBitSize =
309 BinaryOperator::createSub(Left, Right,"fbits", FwdSize);
310 new BranchInst(Compute, FwdSize);
311
312 // Second, compute the number of bits in the reverse case.
313 Instruction* RBitSize =
314 BinaryOperator::createSub(Right, Left, "rbits", RevSize);
315 new BranchInst(Compute, RevSize);
316
317 // Now, compute the bit range. Start by getting the bitsize and the shift
318 // amount (either Left or Right) from PHI nodes. Then we compute a mask for
319 // the number of bits we want in the range. We shift the bits down to the
320 // least significant bits, apply the mask to zero out unwanted high bits,
321 // and we have computed the "forward" result. It may still need to be
322 // reversed.
323
324 // Get the BitSize from one of the two subtractions
325 PHINode *BitSize = new PHINode(Val->getType(), "bits", Compute);
326 BitSize->reserveOperandSpace(2);
327 BitSize->addIncoming(FBitSize, FwdSize);
328 BitSize->addIncoming(RBitSize, RevSize);
329
330 // Get the ShiftAmount as the smaller of Left/Right
331 PHINode *ShiftAmt = new PHINode(Val->getType(), "shiftamt", Compute);
332 ShiftAmt->reserveOperandSpace(2);
333 ShiftAmt->addIncoming(Right, FwdSize);
334 ShiftAmt->addIncoming(Left, RevSize);
335
336 // Increment the bit size
337 Instruction *BitSizePlusOne =
338 BinaryOperator::createAdd(BitSize, One, "bits", Compute);
339
340 // Create a Mask to zero out the high order bits.
341 Instruction* Mask =
342 BinaryOperator::createShl(AllOnes, BitSizePlusOne, "mask", Compute);
343 Mask = BinaryOperator::createNot(Mask, "mask", Compute);
344
345 // Shift the bits down and apply the mask
346 Instruction* FRes =
347 BinaryOperator::createLShr(Val, ShiftAmt, "fres", Compute);
348 FRes = BinaryOperator::createAnd(FRes, Mask, "fres", Compute);
349 new BranchInst(Reverse, RsltBlk, Cmp, Compute);
350
351 // In the Reverse block we have the mask already in FRes but we must reverse
352 // it by shifting FRes bits right and putting them in RRes by shifting them
353 // in from left.
354
355 // First set up our loop counters
356 PHINode *Count = new PHINode(Val->getType(), "count", Reverse);
357 Count->reserveOperandSpace(2);
358 Count->addIncoming(BitSizePlusOne, Compute);
359
360 // Next, get the value that we are shifting.
361 PHINode *BitsToShift = new PHINode(Val->getType(), "val", Reverse);
362 BitsToShift->reserveOperandSpace(2);
363 BitsToShift->addIncoming(FRes, Compute);
364
365 // Finally, get the result of the last computation
366 PHINode *RRes = new PHINode(Val->getType(), "rres", Reverse);
367 RRes->reserveOperandSpace(2);
368 RRes->addIncoming(Zero, Compute);
369
370 // Decrement the counter
371 Instruction *Decr = BinaryOperator::createSub(Count, One, "decr", Reverse);
372 Count->addIncoming(Decr, Reverse);
373
374 // Compute the Bit that we want to move
375 Instruction *Bit =
376 BinaryOperator::createAnd(BitsToShift, One, "bit", Reverse);
377
378 // Compute the new value for next iteration.
379 Instruction *NewVal =
380 BinaryOperator::createLShr(BitsToShift, One, "rshift", Reverse);
381 BitsToShift->addIncoming(NewVal, Reverse);
382
383 // Shift the bit into the low bits of the result.
384 Instruction *NewRes =
385 BinaryOperator::createShl(RRes, One, "lshift", Reverse);
386 NewRes = BinaryOperator::createOr(NewRes, Bit, "addbit", Reverse);
387 RRes->addIncoming(NewRes, Reverse);
388
389 // Terminate loop if we've moved all the bits.
390 ICmpInst *Cond =
391 new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "cond", Reverse);
392 new BranchInst(RsltBlk, Reverse, Cond, Reverse);
393
394 // Finally, in the result block, select one of the two results with a PHI
395 // node and return the result;
396 CurBB = RsltBlk;
397 PHINode *BitSelect = new PHINode(Val->getType(), "part_select", CurBB);
398 BitSelect->reserveOperandSpace(2);
399 BitSelect->addIncoming(FRes, Compute);
400 BitSelect->addIncoming(NewRes, Reverse);
401 new ReturnInst(BitSelect, CurBB);
402 }
403
404 // Return a call to the implementation function
405 Value *Args[3];
406 Args[0] = CI->getOperand(0);
407 Args[1] = CI->getOperand(1);
408 Args[2] = CI->getOperand(2);
409 return new CallInst(F, Args, 3, CI->getName(), CI);
410}
411
Reid Spencerf75b8742007-04-12 02:48:46 +0000412/// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes
413/// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High)
414/// The first two arguments can be any bit width. The result is the same width
415/// as %Value. The operation replaces bits between %Low and %High with the value
416/// in %Replacement. If %Replacement is not the same width, it is truncated or
417/// zero extended as appropriate to fit the bits being replaced. If %Low is
418/// greater than %High then the inverse set of bits are replaced.
419/// @brief Lowering of llvm.bit.part.set intrinsic.
420static Instruction *LowerPartSet(CallInst *CI) {
421 // Make sure we're dealing with a part select intrinsic here
422 Function *F = CI->getCalledFunction();
423 const FunctionType *FT = F->getFunctionType();
424 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
425 FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() ||
426 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() ||
427 !FT->getParamType(3)->isInteger())
428 return CI;
429
430 // Get the intrinsic implementation function by converting all the . to _
431 // in the intrinsic's function name and then reconstructing the function
432 // declaration.
433 std::string Name(F->getName());
434 for (unsigned i = 4; i < Name.length(); ++i)
435 if (Name[i] == '.')
436 Name[i] = '_';
437 Module* M = F->getParent();
438 F = cast<Function>(M->getOrInsertFunction(Name, FT));
439 F->setLinkage(GlobalValue::InternalLinkage);
440
441 // If we haven't defined the impl function yet, do so now
442 if (F->isDeclaration()) {
443 // Note: the following code is based on code generated by llvm2cpp with
444 // the following input. This is just *one* example of a generated function.
445 // The functions vary by bit width of result and first two arguments.
446 // The generated code has been changed to deal with any bit width not just
447 // the 32/64 bitwidths used in the above sample.
448 //
449 // define i64 @part_set(i64 %Val, i32 %Rep, i32 %Lo, i32 %Hi) {
450 // entry:
451 // %is_forward = icmp ult i32 %Lo, %Hi
452 // %Lo.pn = select i1 %is_forward, i32 %Hi, i32 %Lo
453 // %Hi.pn = select i1 %is_forward, i32 %Lo, i32 %Hi
454 // %iftmp.16.0 = sub i32 %Lo.pn, %Hi.pn
455 // icmp ult i32 %iftmp.16.0, 32
456 // br i1 %1, label %cond_true11, label %cond_next19
457 // cond_true11:
458 // %tmp13 = sub i32 32, %iftmp.16.0
459 // %tmp14 = lshr i32 -1, %tmp13
460 // %tmp16 = and i32 %tmp14, %Rep
461 // br label %cond_next19
462 // cond_next19:
463 // %iftmp.17.0 = phi i32 [ %tmp16, %cond_true11 ], [ %Rep, %entry ]
464 // %tmp2021 = zext i32 %iftmp.17.0 to i64
465 // icmp ugt i32 %Lo, %Hi
466 // br i1 %2, label %cond_next60, label %cond_true24
467 // cond_true24:
468 // %tmp25.cast = zext i32 %Hi to i64
469 // %tmp26 = lshr i64 -1, %tmp25.cast
470 // %tmp27.cast = zext i32 %Lo to i64
471 // %tmp28 = shl i64 %tmp26, %tmp27.cast
472 // %tmp28not = xor i64 %tmp28, -1
473 // %tmp31 = shl i64 %tmp2021, %tmp27.cast
474 // %tmp34 = and i64 %tmp28not, %Val
475 // %Val_addr.064 = or i64 %tmp31, %tmp34
476 // ret i64 %Val_addr.064
477 // cond_next60:
478 // %tmp39.cast = zext i32 %Lo to i64
479 // %tmp40 = shl i64 -1, %tmp39.cast
480 // %tmp41.cast = zext i32 %Hi to i64
481 // %tmp42 = shl i64 -1, %tmp41.cast
482 // %tmp45.demorgan = or i64 %tmp42, %tmp40
483 // %tmp45 = xor i64 %tmp45.demorgan, -1
484 // %tmp47 = and i64 %tmp45, %Val
485 // %tmp50 = shl i64 %tmp2021, %tmp39.cast
486 // %tmp52 = sub i32 32, %Hi
487 // %tmp52.cast = zext i32 %tmp52 to i64
488 // %tmp54 = lshr i64 %tmp2021, %tmp52.cast
489 // %tmp57 = or i64 %tmp50, %tmp47
490 // %Val_addr.0 = or i64 %tmp57, %tmp54
491 // ret i64 %Val_addr.0
492 // }
493
494 // Get the arguments for the function.
495 Function::arg_iterator args = F->arg_begin();
496 Value* Val = args++; Val->setName("Val");
497 Value* Rep = args++; Rep->setName("Rep");
498 Value* Lo = args++; Lo->setName("Lo");
499 Value* Hi = args++; Hi->setName("Hi");
500
501 // Get some types we need
502 const IntegerType* ValTy = cast<IntegerType>(Val->getType());
503 const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
504 uint32_t ValBits = ValTy->getBitWidth();
505 uint32_t RepBits = RepTy->getBitWidth();
506
507 // Constant Definitions
508 ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
509 ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
510 ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
511
512 BasicBlock* entry = new BasicBlock("entry",F,0);
513 BasicBlock* large = new BasicBlock("large",F,0);
514 BasicBlock* small = new BasicBlock("small",F,0);
Reid Spencer37958092007-04-12 12:46:33 +0000515 BasicBlock* forward = new BasicBlock("forward",F,0);
516 BasicBlock* reverse = new BasicBlock("reverse",F,0);
Reid Spencerf75b8742007-04-12 02:48:46 +0000517
518 // Block entry (entry)
519 // First, convert Lo and Hi to ValTy bit width
520 if (ValBits > 32) {
521 Hi = new ZExtInst(Hi, ValTy, "", entry);
522 Lo = new ZExtInst(Lo, ValTy, "", entry);
523 } else if (ValBits < 32) {
524 Hi = new TruncInst(Hi, ValTy, "", entry);
525 Lo = new TruncInst(Lo, ValTy, "", entry);
526 }
527 ICmpInst* is_forward =
528 new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry);
529 SelectInst* Lo_pn = new SelectInst(is_forward, Hi, Lo, "", entry);
530 SelectInst* Hi_pn = new SelectInst(is_forward, Lo, Hi, "", entry);
531 BinaryOperator* NumBits = BinaryOperator::createSub(Lo_pn, Hi_pn, "",entry);
532 ICmpInst* is_large =
533 new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry);
534 new BranchInst(large, small, is_large, entry);
535
536 // Block "large"
537 BinaryOperator* MaskBits =
538 BinaryOperator::createSub(RepBitWidth, NumBits, "", large);
539 BinaryOperator* Mask1 =
540 BinaryOperator::createLShr(RepMask, MaskBits, "", large);
541 BinaryOperator* Rep2 = BinaryOperator::createAnd(Mask1, Rep, "", large);
542 new BranchInst(small, large);
543
544 // Block "small"
545 PHINode* Rep3 = new PHINode(RepTy, "", small);
546 Rep3->reserveOperandSpace(2);
547 Rep3->addIncoming(Rep2, small);
548 Rep3->addIncoming(Rep, entry);
Reid Spencer37958092007-04-12 12:46:33 +0000549 Value* Rep4 = Rep3;
550 if (ValBits > RepBits)
551 Rep4 = new ZExtInst(Rep3, ValTy, "", small);
552 else if (ValBits < RepBits)
553 Rep4 = new TruncInst(Rep3, ValTy, "", small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000554 ICmpInst* is_reverse =
555 new ICmpInst(ICmpInst::ICMP_UGT, Lo, Hi, "", small);
556 new BranchInst(reverse, forward, is_reverse, small);
557
558 // Block "forward"
559 Value* t1 = BinaryOperator::createLShr(ValMask, Hi, "", forward);
560 Value* t2 = BinaryOperator::createShl(t1, Lo, "", forward);
561 Value* nott2 = BinaryOperator::createXor(t2, ValMask, "", forward);
562 Value* t3 = BinaryOperator::createShl(Rep4, Lo, "", forward);
563 Value* t4 = BinaryOperator::createAnd(nott2, Val, "", forward);
564 Value* FRslt = BinaryOperator::createOr(t3, t4, "", forward);
565 new ReturnInst(FRslt, forward);
566
567 // Block "reverse"
568 Value* t5 = BinaryOperator::createShl(ValMask, Lo, "", reverse);
569 Value* t6 = BinaryOperator::createShl(ValMask, Hi, "", reverse);
570 Value* t7 = BinaryOperator::createOr(t6, t5, "", reverse);
571 Value* t8 = BinaryOperator::createXor(t7, ValMask, "", reverse);
572 Value* t9 = BinaryOperator::createAnd(t8, Val, "", reverse);
573 Value* t10 = BinaryOperator::createShl(Rep4, Lo, "", reverse);
574 Value* t11 = BinaryOperator::createSub(RepBitWidth, Hi, "", reverse);
575 Value* t12 = new ZExtInst(t11, ValTy, "", reverse);
576 Value* t13 = BinaryOperator::createLShr(Rep4, t12, "",reverse);
577 Value* t14 = BinaryOperator::createOr(t10, t9, "", reverse);
578 Value* RRslt = BinaryOperator::createOr(t14, t13, "", reverse);
579 new ReturnInst(RRslt, reverse);
580 }
581
582 // Return a call to the implementation function
583 Value *Args[3];
584 Args[0] = CI->getOperand(0);
585 Args[1] = CI->getOperand(1);
586 Args[2] = CI->getOperand(2);
587 Args[3] = CI->getOperand(3);
588 return new CallInst(F, Args, 4, CI->getName(), CI);
589}
590
Reid Spenceraddd11d2007-04-04 23:48:25 +0000591
Chris Lattnerb71fd782006-11-15 18:00:10 +0000592void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000593 Function *Callee = CI->getCalledFunction();
594 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000595
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000596 switch (Callee->getIntrinsicID()) {
597 case Intrinsic::not_intrinsic:
Bill Wendlinge8156192006-12-07 01:30:32 +0000598 cerr << "Cannot lower a call to a non-intrinsic function '"
599 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000600 abort();
601 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000602 cerr << "Error: Code generator does not support intrinsic function '"
603 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000604 abort();
605
Chris Lattner588e72d2004-02-15 22:16:39 +0000606 // The setjmp/longjmp intrinsics should only exist in the code if it was
607 // never optimized (ie, right out of the CFE), or if it has been hacked on
608 // by the lowerinvoke pass. In both cases, the right thing to do is to
609 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000610 case Intrinsic::setjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000611 static Constant *SetjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000612 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000613 Type::Int32Ty, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000614 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000615 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000616 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000617 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000618 case Intrinsic::sigsetjmp:
Chris Lattner9b700f72004-02-15 22:24:51 +0000619 if (CI->getType() != Type::VoidTy)
620 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
621 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000622
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000623 case Intrinsic::longjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000624 static Constant *LongjmpFCache = 0;
Chris Lattner9b700f72004-02-15 22:24:51 +0000625 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000626 Type::VoidTy, LongjmpFCache);
Chris Lattner9b700f72004-02-15 22:24:51 +0000627 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000628 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000629
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000630 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000631 // Insert the call to abort
Chris Lattnerb76efb72007-01-07 08:12:01 +0000632 static Constant *AbortFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000633 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000634 Type::VoidTy, AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000635 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000636 }
Reid Spencere9391fd2007-04-01 07:35:23 +0000637 case Intrinsic::ctpop:
Reid Spencer0b118202006-01-16 21:12:35 +0000638 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
639 break;
640
Reid Spencere9391fd2007-04-01 07:35:23 +0000641 case Intrinsic::bswap:
Nate Begemane5981812006-01-16 07:57:00 +0000642 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
643 break;
644
Reid Spencere9391fd2007-04-01 07:35:23 +0000645 case Intrinsic::ctlz:
Chris Lattner98cf45b2005-05-11 20:24:12 +0000646 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000647 break;
Nate Begemane5981812006-01-16 07:57:00 +0000648
Reid Spencere9391fd2007-04-01 07:35:23 +0000649 case Intrinsic::cttz: {
Chris Lattnera8011722005-05-11 20:02:14 +0000650 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000651 Value *Src = CI->getOperand(1);
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000652 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
Chris Lattnera8011722005-05-11 20:02:14 +0000653 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
654 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
655 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000656 CI->replaceAllUsesWith(Src);
657 break;
658 }
Chris Lattner77b13302004-01-05 05:36:30 +0000659
Chris Lattnerc6eb6d72007-04-10 03:20:39 +0000660 case Intrinsic::part_select:
Reid Spencerf75b8742007-04-12 02:48:46 +0000661 CI->replaceAllUsesWith(LowerPartSelect(CI));
662 break;
663
664 case Intrinsic::part_set:
665 CI->replaceAllUsesWith(LowerPartSet(CI));
Reid Spenceraddd11d2007-04-04 23:48:25 +0000666 break;
667
Chris Lattner0c067bc2006-01-13 02:22:08 +0000668 case Intrinsic::stacksave:
669 case Intrinsic::stackrestore: {
670 static bool Warned = false;
671 if (!Warned)
Bill Wendlinge8156192006-12-07 01:30:32 +0000672 cerr << "WARNING: this target does not support the llvm.stack"
673 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
674 "save" : "restore") << " intrinsic.\n";
Chris Lattner0c067bc2006-01-13 02:22:08 +0000675 Warned = true;
676 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
677 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
678 break;
679 }
680
Chris Lattnercf899082004-02-14 02:47:17 +0000681 case Intrinsic::returnaddress:
682 case Intrinsic::frameaddress:
Bill Wendlinge8156192006-12-07 01:30:32 +0000683 cerr << "WARNING: this target does not support the llvm."
684 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
685 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000686 CI->replaceAllUsesWith(ConstantPointerNull::get(
687 cast<PointerType>(CI->getType())));
688 break;
689
Chris Lattner0942b7c2005-02-28 19:27:23 +0000690 case Intrinsic::prefetch:
691 break; // Simply strip out prefetches on unsupported architectures
692
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000693 case Intrinsic::pcmarker:
694 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000695 case Intrinsic::readcyclecounter: {
Bill Wendlinge8156192006-12-07 01:30:32 +0000696 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
697 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencer47857812006-12-31 05:55:36 +0000698 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000699 break;
700 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000701
Chris Lattner77b13302004-01-05 05:36:30 +0000702 case Intrinsic::dbg_stoppoint:
703 case Intrinsic::dbg_region_start:
704 case Intrinsic::dbg_region_end:
705 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000706 case Intrinsic::dbg_declare:
Jim Laskeyb180aa12007-02-21 22:53:45 +0000707 case Intrinsic::eh_exception:
Jim Laskey63f3e3f2007-02-28 18:37:50 +0000708 case Intrinsic::eh_selector:
Jim Laskey0b4711b2007-03-01 20:24:30 +0000709 case Intrinsic::eh_filter:
Jim Laskey774b8642007-02-22 18:51:19 +0000710 break; // Simply strip out debugging and eh intrinsics
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000711
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000712 case Intrinsic::memcpy_i32:
Reid Spencer3da59db2006-11-27 01:05:10 +0000713 case Intrinsic::memcpy_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000714 static Constant *MemcpyFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000715 Value *Size = CI->getOperand(3);
716 const Type *IntPtr = TD.getIntPtrType();
717 if (Size->getType()->getPrimitiveSizeInBits() <
718 IntPtr->getPrimitiveSizeInBits())
719 Size = new ZExtInst(Size, IntPtr, "", CI);
720 else if (Size->getType()->getPrimitiveSizeInBits() >
721 IntPtr->getPrimitiveSizeInBits())
722 Size = new TruncInst(Size, IntPtr, "", CI);
723 Value *Ops[3];
724 Ops[0] = CI->getOperand(1);
725 Ops[1] = CI->getOperand(2);
726 Ops[2] = Size;
727 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
728 MemcpyFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000729 break;
730 }
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000731 case Intrinsic::memmove_i32:
Chris Lattner03dd4652006-03-03 00:00:25 +0000732 case Intrinsic::memmove_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000733 static Constant *MemmoveFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000734 Value *Size = CI->getOperand(3);
735 const Type *IntPtr = TD.getIntPtrType();
736 if (Size->getType()->getPrimitiveSizeInBits() <
737 IntPtr->getPrimitiveSizeInBits())
738 Size = new ZExtInst(Size, IntPtr, "", CI);
739 else if (Size->getType()->getPrimitiveSizeInBits() >
740 IntPtr->getPrimitiveSizeInBits())
741 Size = new TruncInst(Size, IntPtr, "", CI);
742 Value *Ops[3];
743 Ops[0] = CI->getOperand(1);
744 Ops[1] = CI->getOperand(2);
745 Ops[2] = Size;
746 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
747 MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000748 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000749 }
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000750 case Intrinsic::memset_i32:
Chris Lattner03dd4652006-03-03 00:00:25 +0000751 case Intrinsic::memset_i64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000752 static Constant *MemsetFCache = 0;
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000753 Value *Size = CI->getOperand(3);
Chris Lattner7d6f77d2007-02-06 06:07:51 +0000754 const Type *IntPtr = TD.getIntPtrType();
755 if (Size->getType()->getPrimitiveSizeInBits() <
756 IntPtr->getPrimitiveSizeInBits())
757 Size = new ZExtInst(Size, IntPtr, "", CI);
758 else if (Size->getType()->getPrimitiveSizeInBits() >
759 IntPtr->getPrimitiveSizeInBits())
760 Size = new TruncInst(Size, IntPtr, "", CI);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000761 Value *Ops[3];
762 Ops[0] = CI->getOperand(1);
763 // Extend the amount to i32.
764 Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI);
765 Ops[2] = Size;
766 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
767 MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000768 break;
769 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000770 case Intrinsic::sqrt_f32: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000771 static Constant *sqrtfFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000772 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000773 Type::FloatTy, sqrtfFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000774 break;
775 }
776 case Intrinsic::sqrt_f64: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000777 static Constant *sqrtFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000778 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000779 Type::DoubleTy, sqrtFCache);
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000780 break;
781 }
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000782 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000783
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000784 assert(CI->use_empty() &&
785 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000786 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000787}