blob: e6912b82c38f9ba614da56295949d67dddd16381 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000017#include "llvm/Type.h"
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000018#include "llvm/CodeGen/IntrinsicLowering.h"
Jay Foade1e20142009-05-12 20:27:44 +000019#include "llvm/Support/IRBuilder.h"
Reid Spencer6addf2c2007-01-29 17:42:06 +000020#include "llvm/Target/TargetData.h"
Chris Lattner990b8492007-02-13 06:01:22 +000021#include "llvm/ADT/SmallVector.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000022using namespace llvm;
23
Chris Lattner0979ca72004-05-09 04:29:57 +000024template <class ArgIt>
Chris Lattnerb76efb72007-01-07 08:12:01 +000025static void EnsureFunctionExists(Module &M, const char *Name,
26 ArgIt ArgBegin, ArgIt ArgEnd,
27 const Type *RetTy) {
28 // Insert a correctly-typed definition now.
Chris Lattner0979ca72004-05-09 04:29:57 +000029 std::vector<const Type *> ParamTys;
30 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
31 ParamTys.push_back(I->getType());
Chris Lattnerb76efb72007-01-07 08:12:01 +000032 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
Chris Lattner0979ca72004-05-09 04:29:57 +000033}
34
Chris Lattner556b4a62009-02-07 22:37:06 +000035static void EnsureFPIntrinsicsExist(Module &M, Function *Fn,
36 const char *FName,
37 const char *DName, const char *LDName) {
Dale Johannesenc4342ea2008-09-22 19:51:58 +000038 // Insert definitions for all the floating point types.
Chris Lattner556b4a62009-02-07 22:37:06 +000039 switch((int)Fn->arg_begin()->getType()->getTypeID()) {
Dale Johannesenc4342ea2008-09-22 19:51:58 +000040 case Type::FloatTyID:
Chris Lattner556b4a62009-02-07 22:37:06 +000041 EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(),
Dale Johannesenc4342ea2008-09-22 19:51:58 +000042 Type::FloatTy);
Chris Lattner556b4a62009-02-07 22:37:06 +000043 break;
Dale Johannesenc4342ea2008-09-22 19:51:58 +000044 case Type::DoubleTyID:
Chris Lattner556b4a62009-02-07 22:37:06 +000045 EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(),
Dale Johannesenc4342ea2008-09-22 19:51:58 +000046 Type::DoubleTy);
Chris Lattner556b4a62009-02-07 22:37:06 +000047 break;
Dale Johannesenc4342ea2008-09-22 19:51:58 +000048 case Type::X86_FP80TyID:
49 case Type::FP128TyID:
50 case Type::PPC_FP128TyID:
Chris Lattner556b4a62009-02-07 22:37:06 +000051 EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(),
52 Fn->arg_begin()->getType());
53 break;
Dale Johannesenc4342ea2008-09-22 19:51:58 +000054 }
55}
56
Chris Lattner588e72d2004-02-15 22:16:39 +000057/// ReplaceCallWith - This function is used when we want to lower an intrinsic
58/// call to a call of an external function. This handles hard cases such as
59/// when there was already a prototype for the external function, and if that
60/// prototype doesn't match the arguments we expect to pass in.
61template <class ArgIt>
62static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
Chris Lattnerb76efb72007-01-07 08:12:01 +000063 ArgIt ArgBegin, ArgIt ArgEnd,
64 const Type *RetTy, Constant *&FCache) {
Chris Lattner588e72d2004-02-15 22:16:39 +000065 if (!FCache) {
66 // If we haven't already looked up this function, check to see if the
67 // program already contains a function with this name.
68 Module *M = CI->getParent()->getParent()->getParent();
Chris Lattnerb76efb72007-01-07 08:12:01 +000069 // Get or insert the definition now.
70 std::vector<const Type *> ParamTys;
71 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
72 ParamTys.push_back((*I)->getType());
73 FCache = M->getOrInsertFunction(NewFn,
74 FunctionType::get(RetTy, ParamTys, false));
Chris Lattner588e72d2004-02-15 22:16:39 +000075 }
Chris Lattner588e72d2004-02-15 22:16:39 +000076
Jay Foade1e20142009-05-12 20:27:44 +000077 IRBuilder<> Builder(CI->getParent(), CI);
David Greene52eec542007-08-01 03:43:44 +000078 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
Jay Foade1e20142009-05-12 20:27:44 +000079 CallInst *NewCI = Builder.CreateCall(FCache, Args.begin(), Args.end());
80 NewCI->setName(CI->getName());
Chris Lattnerb76efb72007-01-07 08:12:01 +000081 if (!CI->use_empty())
82 CI->replaceAllUsesWith(NewCI);
Chris Lattner02348ca2004-06-11 02:54:02 +000083 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000084}
85
Chris Lattnerb71fd782006-11-15 18:00:10 +000086void IntrinsicLowering::AddPrototypes(Module &M) {
Chris Lattner0979ca72004-05-09 04:29:57 +000087 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000088 if (I->isDeclaration() && !I->use_empty())
Chris Lattner0979ca72004-05-09 04:29:57 +000089 switch (I->getIntrinsicID()) {
90 default: break;
91 case Intrinsic::setjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000092 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
Reid Spencer47857812006-12-31 05:55:36 +000093 Type::Int32Ty);
Chris Lattner0979ca72004-05-09 04:29:57 +000094 break;
95 case Intrinsic::longjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +000096 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
97 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +000098 break;
99 case Intrinsic::siglongjmp:
Chris Lattner1f243e92005-05-08 19:46:29 +0000100 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
101 Type::VoidTy);
Chris Lattner0979ca72004-05-09 04:29:57 +0000102 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000103 case Intrinsic::memcpy:
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000104 M.getOrInsertFunction("memcpy", PointerType::getUnqual(Type::Int8Ty),
105 PointerType::getUnqual(Type::Int8Ty),
106 PointerType::getUnqual(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +0000107 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000108 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000109 case Intrinsic::memmove:
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000110 M.getOrInsertFunction("memmove", PointerType::getUnqual(Type::Int8Ty),
111 PointerType::getUnqual(Type::Int8Ty),
112 PointerType::getUnqual(Type::Int8Ty),
Reid Spencer6addf2c2007-01-29 17:42:06 +0000113 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000114 break;
Chris Lattner824b9582008-11-21 16:42:48 +0000115 case Intrinsic::memset:
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000116 M.getOrInsertFunction("memset", PointerType::getUnqual(Type::Int8Ty),
117 PointerType::getUnqual(Type::Int8Ty),
118 Type::Int32Ty,
Reid Spencer6addf2c2007-01-29 17:42:06 +0000119 TD.getIntPtrType(), (Type *)0);
Chris Lattner0979ca72004-05-09 04:29:57 +0000120 break;
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000121 case Intrinsic::sqrt:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000122 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
Chris Lattnerb42a9ff2005-04-30 04:07:50 +0000123 break;
Dan Gohmanc4c96602007-10-15 22:07:31 +0000124 case Intrinsic::sin:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000125 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000126 break;
127 case Intrinsic::cos:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000128 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000129 break;
130 case Intrinsic::pow:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000131 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
Dan Gohmanc4c96602007-10-15 22:07:31 +0000132 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000133 case Intrinsic::log:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000134 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000135 break;
136 case Intrinsic::log2:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000137 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000138 break;
139 case Intrinsic::log10:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000140 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000141 break;
142 case Intrinsic::exp:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000143 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000144 break;
145 case Intrinsic::exp2:
Dale Johannesenc4342ea2008-09-22 19:51:58 +0000146 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000147 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000148 }
Chris Lattner0979ca72004-05-09 04:29:57 +0000149}
Chris Lattner588e72d2004-02-15 22:16:39 +0000150
Nate Begemane5981812006-01-16 07:57:00 +0000151/// LowerBSWAP - Emit the code to lower bswap of V before the specified
152/// instruction IP.
153static Value *LowerBSWAP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000154 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
Nate Begemane5981812006-01-16 07:57:00 +0000155
Nate Begemane5981812006-01-16 07:57:00 +0000156 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
157
Jay Foade1e20142009-05-12 20:27:44 +0000158 IRBuilder<> Builder(IP->getParent(), IP);
159
Nate Begemane5981812006-01-16 07:57:00 +0000160 switch(BitSize) {
161 default: assert(0 && "Unhandled type size of value to byteswap!");
162 case 16: {
Jay Foade1e20142009-05-12 20:27:44 +0000163 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
164 "bswap.2");
165 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
166 "bswap.1");
167 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
Nate Begemane5981812006-01-16 07:57:00 +0000168 break;
169 }
170 case 32: {
Jay Foade1e20142009-05-12 20:27:44 +0000171 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
172 "bswap.4");
173 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
174 "bswap.3");
175 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
176 "bswap.2");
177 Value *Tmp1 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 24),
178 "bswap.1");
179 Tmp3 = Builder.CreateAnd(Tmp3, ConstantInt::get(Type::Int32Ty, 0xFF0000),
180 "bswap.and3");
181 Tmp2 = Builder.CreateAnd(Tmp2, ConstantInt::get(Type::Int32Ty, 0xFF00),
182 "bswap.and2");
183 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
184 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
185 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
Nate Begemane5981812006-01-16 07:57:00 +0000186 break;
187 }
188 case 64: {
Jay Foade1e20142009-05-12 20:27:44 +0000189 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
190 "bswap.8");
191 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
192 "bswap.7");
193 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
194 "bswap.6");
195 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
196 "bswap.5");
197 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
198 "bswap.4");
199 Value* Tmp3 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 24),
200 "bswap.3");
201 Value* Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 40),
202 "bswap.2");
203 Value* Tmp1 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 56),
204 "bswap.1");
205 Tmp7 = Builder.CreateAnd(Tmp7,
206 ConstantInt::get(Type::Int64Ty,
207 0xFF000000000000ULL),
208 "bswap.and7");
209 Tmp6 = Builder.CreateAnd(Tmp6,
210 ConstantInt::get(Type::Int64Ty,
211 0xFF0000000000ULL),
212 "bswap.and6");
213 Tmp5 = Builder.CreateAnd(Tmp5,
Reid Spencer47857812006-12-31 05:55:36 +0000214 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
Jay Foade1e20142009-05-12 20:27:44 +0000215 "bswap.and5");
216 Tmp4 = Builder.CreateAnd(Tmp4,
Reid Spencer47857812006-12-31 05:55:36 +0000217 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
Jay Foade1e20142009-05-12 20:27:44 +0000218 "bswap.and4");
219 Tmp3 = Builder.CreateAnd(Tmp3,
Reid Spencer47857812006-12-31 05:55:36 +0000220 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
Jay Foade1e20142009-05-12 20:27:44 +0000221 "bswap.and3");
222 Tmp2 = Builder.CreateAnd(Tmp2,
Reid Spencer47857812006-12-31 05:55:36 +0000223 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
Jay Foade1e20142009-05-12 20:27:44 +0000224 "bswap.and2");
225 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
226 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
227 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
228 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
229 Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
230 Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
231 V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
Nate Begemane5981812006-01-16 07:57:00 +0000232 break;
233 }
234 }
Nate Begemane5981812006-01-16 07:57:00 +0000235 return V;
236}
237
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000238/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000239/// instruction IP.
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000240static Value *LowerCTPOP(Value *V, Instruction *IP) {
Chris Lattner42a75512007-01-15 02:27:26 +0000241 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000242
243 static const uint64_t MaskValues[6] = {
244 0x5555555555555555ULL, 0x3333333333333333ULL,
245 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
246 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
247 };
248
Jay Foade1e20142009-05-12 20:27:44 +0000249 IRBuilder<> Builder(IP->getParent(), IP);
250
Chris Lattner98cf45b2005-05-11 20:24:12 +0000251 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng02031c02007-06-02 04:10:33 +0000252 unsigned WordSize = (BitSize + 63) / 64;
253 Value *Count = ConstantInt::get(V->getType(), 0);
Reid Spencer3822ff52006-11-08 06:47:33 +0000254
Zhou Sheng02031c02007-06-02 04:10:33 +0000255 for (unsigned n = 0; n < WordSize; ++n) {
256 Value *PartValue = V;
257 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
258 i <<= 1, ++ct) {
259 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
Jay Foade1e20142009-05-12 20:27:44 +0000260 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
261 Value *VShift = Builder.CreateLShr(PartValue,
262 ConstantInt::get(V->getType(), i),
263 "ctpop.sh");
264 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
265 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
Zhou Sheng02031c02007-06-02 04:10:33 +0000266 }
Jay Foade1e20142009-05-12 20:27:44 +0000267 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
Zhou Sheng02031c02007-06-02 04:10:33 +0000268 if (BitSize > 64) {
Jay Foade1e20142009-05-12 20:27:44 +0000269 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
270 "ctpop.part.sh");
Zhou Sheng02031c02007-06-02 04:10:33 +0000271 BitSize -= 64;
272 }
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000273 }
274
Chris Lattner914ce452007-08-06 16:36:18 +0000275 return Count;
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000276}
277
Chris Lattner98cf45b2005-05-11 20:24:12 +0000278/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
Nate Begemane5981812006-01-16 07:57:00 +0000279/// instruction IP.
Chris Lattner98cf45b2005-05-11 20:24:12 +0000280static Value *LowerCTLZ(Value *V, Instruction *IP) {
Chris Lattner98cf45b2005-05-11 20:24:12 +0000281
Jay Foade1e20142009-05-12 20:27:44 +0000282 IRBuilder<> Builder(IP->getParent(), IP);
283
Chris Lattner98cf45b2005-05-11 20:24:12 +0000284 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
Zhou Sheng02031c02007-06-02 04:10:33 +0000285 for (unsigned i = 1; i < BitSize; i <<= 1) {
Reid Spencer832254e2007-02-02 02:16:23 +0000286 Value *ShVal = ConstantInt::get(V->getType(), i);
Jay Foade1e20142009-05-12 20:27:44 +0000287 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
288 V = Builder.CreateOr(V, ShVal, "ctlz.step");
Chris Lattner98cf45b2005-05-11 20:24:12 +0000289 }
290
Jay Foade1e20142009-05-12 20:27:44 +0000291 V = Builder.CreateNot(V);
Chris Lattner98cf45b2005-05-11 20:24:12 +0000292 return LowerCTPOP(V, IP);
293}
294
Reid Spencerf75b8742007-04-12 02:48:46 +0000295/// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes
296/// three integer arguments. The first argument is the Value from which the
297/// bits will be selected. It may be of any bit width. The second and third
298/// arguments specify a range of bits to select with the second argument
299/// specifying the low bit and the third argument specifying the high bit. Both
300/// must be type i32. The result is the corresponding selected bits from the
301/// Value in the same width as the Value (first argument). If the low bit index
302/// is higher than the high bit index then the inverse selection is done and
303/// the bits are returned in inverse order.
304/// @brief Lowering of llvm.part.select intrinsic.
305static Instruction *LowerPartSelect(CallInst *CI) {
Jay Foade1e20142009-05-12 20:27:44 +0000306 IRBuilder<> Builder;
307
Reid Spenceraddd11d2007-04-04 23:48:25 +0000308 // Make sure we're dealing with a part select intrinsic here
309 Function *F = CI->getCalledFunction();
310 const FunctionType *FT = F->getFunctionType();
311 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
312 FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
313 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
314 return CI;
315
316 // Get the intrinsic implementation function by converting all the . to _
317 // in the intrinsic's function name and then reconstructing the function
318 // declaration.
319 std::string Name(F->getName());
320 for (unsigned i = 4; i < Name.length(); ++i)
321 if (Name[i] == '.')
322 Name[i] = '_';
323 Module* M = F->getParent();
324 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Duncan Sands667d4b82009-03-07 15:45:40 +0000325 F->setLinkage(GlobalValue::WeakAnyLinkage);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000326
327 // If we haven't defined the impl function yet, do so now
328 if (F->isDeclaration()) {
329
330 // Get the arguments to the function
Reid Spencereeedcb62007-04-12 13:30:14 +0000331 Function::arg_iterator args = F->arg_begin();
332 Value* Val = args++; Val->setName("Val");
333 Value* Lo = args++; Lo->setName("Lo");
Gabor Greif051a9502008-04-06 20:25:17 +0000334 Value* Hi = args++; Hi->setName("High");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000335
Reid Spencereeedcb62007-04-12 13:30:14 +0000336 // We want to select a range of bits here such that [Hi, Lo] is shifted
337 // down to the low bits. However, it is quite possible that Hi is smaller
338 // than Lo in which case the bits have to be reversed.
Reid Spenceraddd11d2007-04-04 23:48:25 +0000339
340 // Create the blocks we will need for the two cases (forward, reverse)
Gabor Greif051a9502008-04-06 20:25:17 +0000341 BasicBlock* CurBB = BasicBlock::Create("entry", F);
342 BasicBlock *RevSize = BasicBlock::Create("revsize", CurBB->getParent());
343 BasicBlock *FwdSize = BasicBlock::Create("fwdsize", CurBB->getParent());
344 BasicBlock *Compute = BasicBlock::Create("compute", CurBB->getParent());
345 BasicBlock *Reverse = BasicBlock::Create("reverse", CurBB->getParent());
346 BasicBlock *RsltBlk = BasicBlock::Create("result", CurBB->getParent());
Reid Spenceraddd11d2007-04-04 23:48:25 +0000347
Jay Foade1e20142009-05-12 20:27:44 +0000348 Builder.SetInsertPoint(CurBB);
349
Reid Spencereeedcb62007-04-12 13:30:14 +0000350 // Cast Hi and Lo to the size of Val so the widths are all the same
351 if (Hi->getType() != Val->getType())
Jay Foade1e20142009-05-12 20:27:44 +0000352 Hi = Builder.CreateIntCast(Hi, Val->getType(), /* isSigned */ false,
353 "tmp");
Reid Spencereeedcb62007-04-12 13:30:14 +0000354 if (Lo->getType() != Val->getType())
Jay Foade1e20142009-05-12 20:27:44 +0000355 Lo = Builder.CreateIntCast(Lo, Val->getType(), /* isSigned */ false,
356 "tmp");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000357
358 // Compute a few things that both cases will need, up front.
359 Constant* Zero = ConstantInt::get(Val->getType(), 0);
360 Constant* One = ConstantInt::get(Val->getType(), 1);
361 Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
362
Reid Spencereeedcb62007-04-12 13:30:14 +0000363 // Compare the Hi and Lo bit positions. This is used to determine
Reid Spenceraddd11d2007-04-04 23:48:25 +0000364 // which case we have (forward or reverse)
Jay Foade1e20142009-05-12 20:27:44 +0000365 Value *Cmp = Builder.CreateICmpULT(Hi, Lo, "less");
366 Builder.CreateCondBr(Cmp, RevSize, FwdSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000367
Jay Foade1e20142009-05-12 20:27:44 +0000368 // First, compute the number of bits in the forward case.
369 Builder.SetInsertPoint(FwdSize);
370 Value* FBitSize = Builder.CreateSub(Hi, Lo, "fbits");
371 Builder.CreateBr(Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000372
373 // Second, compute the number of bits in the reverse case.
Jay Foade1e20142009-05-12 20:27:44 +0000374 Builder.SetInsertPoint(RevSize);
375 Value* RBitSize = Builder.CreateSub(Lo, Hi, "rbits");
376 Builder.CreateBr(Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000377
378 // Now, compute the bit range. Start by getting the bitsize and the shift
Reid Spencereeedcb62007-04-12 13:30:14 +0000379 // amount (either Hi or Lo) from PHI nodes. Then we compute a mask for
Reid Spenceraddd11d2007-04-04 23:48:25 +0000380 // the number of bits we want in the range. We shift the bits down to the
381 // least significant bits, apply the mask to zero out unwanted high bits,
382 // and we have computed the "forward" result. It may still need to be
383 // reversed.
Jay Foade1e20142009-05-12 20:27:44 +0000384 Builder.SetInsertPoint(Compute);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000385
386 // Get the BitSize from one of the two subtractions
Jay Foade1e20142009-05-12 20:27:44 +0000387 PHINode *BitSize = Builder.CreatePHI(Val->getType(), "bits");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000388 BitSize->reserveOperandSpace(2);
389 BitSize->addIncoming(FBitSize, FwdSize);
390 BitSize->addIncoming(RBitSize, RevSize);
391
Reid Spencereeedcb62007-04-12 13:30:14 +0000392 // Get the ShiftAmount as the smaller of Hi/Lo
Jay Foade1e20142009-05-12 20:27:44 +0000393 PHINode *ShiftAmt = Builder.CreatePHI(Val->getType(), "shiftamt");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000394 ShiftAmt->reserveOperandSpace(2);
Reid Spencereeedcb62007-04-12 13:30:14 +0000395 ShiftAmt->addIncoming(Lo, FwdSize);
396 ShiftAmt->addIncoming(Hi, RevSize);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000397
398 // Increment the bit size
Jay Foade1e20142009-05-12 20:27:44 +0000399 Value *BitSizePlusOne = Builder.CreateAdd(BitSize, One, "bits");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000400
401 // Create a Mask to zero out the high order bits.
Jay Foade1e20142009-05-12 20:27:44 +0000402 Value* Mask = Builder.CreateShl(AllOnes, BitSizePlusOne, "mask");
403 Mask = Builder.CreateNot(Mask, "mask");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000404
405 // Shift the bits down and apply the mask
Jay Foade1e20142009-05-12 20:27:44 +0000406 Value* FRes = Builder.CreateLShr(Val, ShiftAmt, "fres");
407 FRes = Builder.CreateAnd(FRes, Mask, "fres");
408 Builder.CreateCondBr(Cmp, Reverse, RsltBlk);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000409
410 // In the Reverse block we have the mask already in FRes but we must reverse
411 // it by shifting FRes bits right and putting them in RRes by shifting them
412 // in from left.
Jay Foade1e20142009-05-12 20:27:44 +0000413 Builder.SetInsertPoint(Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000414
415 // First set up our loop counters
Jay Foade1e20142009-05-12 20:27:44 +0000416 PHINode *Count = Builder.CreatePHI(Val->getType(), "count");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000417 Count->reserveOperandSpace(2);
418 Count->addIncoming(BitSizePlusOne, Compute);
419
420 // Next, get the value that we are shifting.
Jay Foade1e20142009-05-12 20:27:44 +0000421 PHINode *BitsToShift = Builder.CreatePHI(Val->getType(), "val");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000422 BitsToShift->reserveOperandSpace(2);
423 BitsToShift->addIncoming(FRes, Compute);
424
425 // Finally, get the result of the last computation
Jay Foade1e20142009-05-12 20:27:44 +0000426 PHINode *RRes = Builder.CreatePHI(Val->getType(), "rres");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000427 RRes->reserveOperandSpace(2);
428 RRes->addIncoming(Zero, Compute);
429
430 // Decrement the counter
Jay Foade1e20142009-05-12 20:27:44 +0000431 Value *Decr = Builder.CreateSub(Count, One, "decr");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000432 Count->addIncoming(Decr, Reverse);
433
434 // Compute the Bit that we want to move
Jay Foade1e20142009-05-12 20:27:44 +0000435 Value *Bit = Builder.CreateAnd(BitsToShift, One, "bit");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000436
437 // Compute the new value for next iteration.
Jay Foade1e20142009-05-12 20:27:44 +0000438 Value *NewVal = Builder.CreateLShr(BitsToShift, One, "rshift");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000439 BitsToShift->addIncoming(NewVal, Reverse);
440
441 // Shift the bit into the low bits of the result.
Jay Foade1e20142009-05-12 20:27:44 +0000442 Value *NewRes = Builder.CreateShl(RRes, One, "lshift");
443 NewRes = Builder.CreateOr(NewRes, Bit, "addbit");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000444 RRes->addIncoming(NewRes, Reverse);
445
446 // Terminate loop if we've moved all the bits.
Jay Foade1e20142009-05-12 20:27:44 +0000447 Value *Cond = Builder.CreateICmpEQ(Decr, Zero, "cond");
448 Builder.CreateCondBr(Cond, RsltBlk, Reverse);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000449
450 // Finally, in the result block, select one of the two results with a PHI
451 // node and return the result;
Jay Foade1e20142009-05-12 20:27:44 +0000452 Builder.SetInsertPoint(RsltBlk);
453 PHINode *BitSelect = Builder.CreatePHI(Val->getType(), "part_select");
Reid Spenceraddd11d2007-04-04 23:48:25 +0000454 BitSelect->reserveOperandSpace(2);
455 BitSelect->addIncoming(FRes, Compute);
456 BitSelect->addIncoming(NewRes, Reverse);
Jay Foade1e20142009-05-12 20:27:44 +0000457 Builder.CreateRet(BitSelect);
Reid Spenceraddd11d2007-04-04 23:48:25 +0000458 }
459
460 // Return a call to the implementation function
Jay Foade1e20142009-05-12 20:27:44 +0000461 Builder.SetInsertPoint(CI->getParent(), CI);
462 CallInst *NewCI = Builder.CreateCall3(F, CI->getOperand(1),
463 CI->getOperand(2), CI->getOperand(3));
464 NewCI->setName(CI->getName());
465 return NewCI;
Reid Spenceraddd11d2007-04-04 23:48:25 +0000466}
467
Reid Spencerf75b8742007-04-12 02:48:46 +0000468/// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes
469/// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High)
470/// The first two arguments can be any bit width. The result is the same width
471/// as %Value. The operation replaces bits between %Low and %High with the value
472/// in %Replacement. If %Replacement is not the same width, it is truncated or
473/// zero extended as appropriate to fit the bits being replaced. If %Low is
474/// greater than %High then the inverse set of bits are replaced.
475/// @brief Lowering of llvm.bit.part.set intrinsic.
476static Instruction *LowerPartSet(CallInst *CI) {
Jay Foade1e20142009-05-12 20:27:44 +0000477 IRBuilder<> Builder;
478
Reid Spencerf75b8742007-04-12 02:48:46 +0000479 // Make sure we're dealing with a part select intrinsic here
480 Function *F = CI->getCalledFunction();
481 const FunctionType *FT = F->getFunctionType();
482 if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
483 FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() ||
484 !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() ||
485 !FT->getParamType(3)->isInteger())
486 return CI;
487
488 // Get the intrinsic implementation function by converting all the . to _
489 // in the intrinsic's function name and then reconstructing the function
490 // declaration.
491 std::string Name(F->getName());
492 for (unsigned i = 4; i < Name.length(); ++i)
493 if (Name[i] == '.')
494 Name[i] = '_';
495 Module* M = F->getParent();
496 F = cast<Function>(M->getOrInsertFunction(Name, FT));
Duncan Sands667d4b82009-03-07 15:45:40 +0000497 F->setLinkage(GlobalValue::WeakAnyLinkage);
Reid Spencerf75b8742007-04-12 02:48:46 +0000498
499 // If we haven't defined the impl function yet, do so now
500 if (F->isDeclaration()) {
Reid Spencerf75b8742007-04-12 02:48:46 +0000501 // Get the arguments for the function.
502 Function::arg_iterator args = F->arg_begin();
503 Value* Val = args++; Val->setName("Val");
504 Value* Rep = args++; Rep->setName("Rep");
505 Value* Lo = args++; Lo->setName("Lo");
506 Value* Hi = args++; Hi->setName("Hi");
507
508 // Get some types we need
509 const IntegerType* ValTy = cast<IntegerType>(Val->getType());
510 const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
Reid Spencerf75b8742007-04-12 02:48:46 +0000511 uint32_t RepBits = RepTy->getBitWidth();
512
513 // Constant Definitions
514 ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
515 ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
516 ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
Reid Spencer76c94b62007-05-15 02:26:52 +0000517 ConstantInt* One = ConstantInt::get(Type::Int32Ty, 1);
518 ConstantInt* ValOne = ConstantInt::get(ValTy, 1);
519 ConstantInt* Zero = ConstantInt::get(Type::Int32Ty, 0);
520 ConstantInt* ValZero = ConstantInt::get(ValTy, 0);
Reid Spencerf75b8742007-04-12 02:48:46 +0000521
Reid Spencer76c94b62007-05-15 02:26:52 +0000522 // Basic blocks we fill in below.
Gabor Greif051a9502008-04-06 20:25:17 +0000523 BasicBlock* entry = BasicBlock::Create("entry", F, 0);
524 BasicBlock* large = BasicBlock::Create("large", F, 0);
525 BasicBlock* small = BasicBlock::Create("small", F, 0);
526 BasicBlock* reverse = BasicBlock::Create("reverse", F, 0);
527 BasicBlock* result = BasicBlock::Create("result", F, 0);
Reid Spencerf75b8742007-04-12 02:48:46 +0000528
Reid Spencer76c94b62007-05-15 02:26:52 +0000529 // BASIC BLOCK: entry
Jay Foade1e20142009-05-12 20:27:44 +0000530 Builder.SetInsertPoint(entry);
Reid Spencereeedcb62007-04-12 13:30:14 +0000531 // First, get the number of bits that we're placing as an i32
Jay Foade1e20142009-05-12 20:27:44 +0000532 Value* is_forward = Builder.CreateICmpULT(Lo, Hi);
533 Value* Hi_pn = Builder.CreateSelect(is_forward, Hi, Lo);
534 Value* Lo_pn = Builder.CreateSelect(is_forward, Lo, Hi);
535 Value* NumBits = Builder.CreateSub(Hi_pn, Lo_pn);
536 NumBits = Builder.CreateAdd(NumBits, One);
Reid Spencereeedcb62007-04-12 13:30:14 +0000537 // Now, convert Lo and Hi to ValTy bit width
Jay Foade1e20142009-05-12 20:27:44 +0000538 Lo = Builder.CreateIntCast(Lo_pn, ValTy, /* isSigned */ false);
Reid Spencereeedcb62007-04-12 13:30:14 +0000539 // Determine if the replacement bits are larger than the number of bits we
540 // are replacing and deal with it.
Jay Foade1e20142009-05-12 20:27:44 +0000541 Value* is_large = Builder.CreateICmpULT(NumBits, RepBitWidth);
542 Builder.CreateCondBr(is_large, large, small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000543
Reid Spencer76c94b62007-05-15 02:26:52 +0000544 // BASIC BLOCK: large
Jay Foade1e20142009-05-12 20:27:44 +0000545 Builder.SetInsertPoint(large);
546 Value* MaskBits = Builder.CreateSub(RepBitWidth, NumBits);
547 MaskBits = Builder.CreateIntCast(MaskBits, RepMask->getType(),
548 /* isSigned */ false);
549 Value* Mask1 = Builder.CreateLShr(RepMask, MaskBits);
550 Value* Rep2 = Builder.CreateAnd(Mask1, Rep);
551 Builder.CreateBr(small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000552
Reid Spencer76c94b62007-05-15 02:26:52 +0000553 // BASIC BLOCK: small
Jay Foade1e20142009-05-12 20:27:44 +0000554 Builder.SetInsertPoint(small);
555 PHINode* Rep3 = Builder.CreatePHI(RepTy);
Reid Spencerf75b8742007-04-12 02:48:46 +0000556 Rep3->reserveOperandSpace(2);
Reid Spencereeedcb62007-04-12 13:30:14 +0000557 Rep3->addIncoming(Rep2, large);
Reid Spencerf75b8742007-04-12 02:48:46 +0000558 Rep3->addIncoming(Rep, entry);
Jay Foade1e20142009-05-12 20:27:44 +0000559 Value* Rep4 = Builder.CreateIntCast(Rep3, ValTy, /* isSigned */ false);
560 Builder.CreateCondBr(is_forward, result, reverse);
Reid Spencerf75b8742007-04-12 02:48:46 +0000561
Reid Spencer76c94b62007-05-15 02:26:52 +0000562 // BASIC BLOCK: reverse (reverses the bits of the replacement)
Jay Foade1e20142009-05-12 20:27:44 +0000563 Builder.SetInsertPoint(reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000564 // Set up our loop counter as a PHI so we can decrement on each iteration.
565 // We will loop for the number of bits in the replacement value.
Jay Foade1e20142009-05-12 20:27:44 +0000566 PHINode *Count = Builder.CreatePHI(Type::Int32Ty, "count");
Reid Spencer76c94b62007-05-15 02:26:52 +0000567 Count->reserveOperandSpace(2);
568 Count->addIncoming(NumBits, small);
Reid Spencerf75b8742007-04-12 02:48:46 +0000569
Reid Spencer76c94b62007-05-15 02:26:52 +0000570 // Get the value that we are shifting bits out of as a PHI because
571 // we'll change this with each iteration.
Jay Foade1e20142009-05-12 20:27:44 +0000572 PHINode *BitsToShift = Builder.CreatePHI(Val->getType(), "val");
Reid Spencer76c94b62007-05-15 02:26:52 +0000573 BitsToShift->reserveOperandSpace(2);
574 BitsToShift->addIncoming(Rep4, small);
575
576 // Get the result of the last computation or zero on first iteration
Jay Foade1e20142009-05-12 20:27:44 +0000577 PHINode *RRes = Builder.CreatePHI(Val->getType(), "rres");
Reid Spencer76c94b62007-05-15 02:26:52 +0000578 RRes->reserveOperandSpace(2);
579 RRes->addIncoming(ValZero, small);
580
581 // Decrement the loop counter by one
Jay Foade1e20142009-05-12 20:27:44 +0000582 Value *Decr = Builder.CreateSub(Count, One);
Reid Spencer76c94b62007-05-15 02:26:52 +0000583 Count->addIncoming(Decr, reverse);
584
585 // Get the bit that we want to move into the result
Jay Foade1e20142009-05-12 20:27:44 +0000586 Value *Bit = Builder.CreateAnd(BitsToShift, ValOne);
Reid Spencer76c94b62007-05-15 02:26:52 +0000587
588 // Compute the new value of the bits to shift for the next iteration.
Jay Foade1e20142009-05-12 20:27:44 +0000589 Value *NewVal = Builder.CreateLShr(BitsToShift, ValOne);
Reid Spencer76c94b62007-05-15 02:26:52 +0000590 BitsToShift->addIncoming(NewVal, reverse);
591
592 // Shift the bit we extracted into the low bit of the result.
Jay Foade1e20142009-05-12 20:27:44 +0000593 Value *NewRes = Builder.CreateShl(RRes, ValOne);
594 NewRes = Builder.CreateOr(NewRes, Bit);
Reid Spencer76c94b62007-05-15 02:26:52 +0000595 RRes->addIncoming(NewRes, reverse);
596
597 // Terminate loop if we've moved all the bits.
Jay Foade1e20142009-05-12 20:27:44 +0000598 Value *Cond = Builder.CreateICmpEQ(Decr, Zero);
599 Builder.CreateCondBr(Cond, result, reverse);
Reid Spencer76c94b62007-05-15 02:26:52 +0000600
601 // BASIC BLOCK: result
Jay Foade1e20142009-05-12 20:27:44 +0000602 Builder.SetInsertPoint(result);
603 PHINode *Rplcmnt = Builder.CreatePHI(Val->getType());
Reid Spencer76c94b62007-05-15 02:26:52 +0000604 Rplcmnt->reserveOperandSpace(2);
605 Rplcmnt->addIncoming(NewRes, reverse);
606 Rplcmnt->addIncoming(Rep4, small);
Jay Foade1e20142009-05-12 20:27:44 +0000607 Value* t0 = Builder.CreateIntCast(NumBits, ValTy, /* isSigned */ false);
608 Value* t1 = Builder.CreateShl(ValMask, Lo);
609 Value* t2 = Builder.CreateNot(t1);
610 Value* t3 = Builder.CreateShl(t1, t0);
611 Value* t4 = Builder.CreateOr(t2, t3);
612 Value* t5 = Builder.CreateAnd(t4, Val);
613 Value* t6 = Builder.CreateShl(Rplcmnt, Lo);
614 Value* Rslt = Builder.CreateOr(t5, t6, "part_set");
615 Builder.CreateRet(Rslt);
Reid Spencerf75b8742007-04-12 02:48:46 +0000616 }
617
618 // Return a call to the implementation function
Jay Foade1e20142009-05-12 20:27:44 +0000619 Builder.SetInsertPoint(CI->getParent(), CI);
620 CallInst *NewCI = Builder.CreateCall4(F, CI->getOperand(1),
621 CI->getOperand(2), CI->getOperand(3),
622 CI->getOperand(4));
623 NewCI->setName(CI->getName());
624 return NewCI;
Reid Spencerf75b8742007-04-12 02:48:46 +0000625}
626
Dale Johannesenf74185b2008-09-22 20:51:30 +0000627static void ReplaceFPIntrinsicWithCall(CallInst *CI, Constant *FCache,
628 Constant *DCache, Constant *LDCache,
629 const char *Fname, const char *Dname,
630 const char *LDname) {
631 switch (CI->getOperand(1)->getType()->getTypeID()) {
632 default: assert(0 && "Invalid type in intrinsic"); abort();
633 case Type::FloatTyID:
Jay Foade1e20142009-05-12 20:27:44 +0000634 ReplaceCallWith(Fname, CI, CI->op_begin() + 1, CI->op_end(),
Dale Johannesenf74185b2008-09-22 20:51:30 +0000635 Type::FloatTy, FCache);
636 break;
637 case Type::DoubleTyID:
Jay Foade1e20142009-05-12 20:27:44 +0000638 ReplaceCallWith(Dname, CI, CI->op_begin() + 1, CI->op_end(),
Dale Johannesenf74185b2008-09-22 20:51:30 +0000639 Type::DoubleTy, DCache);
640 break;
641 case Type::X86_FP80TyID:
642 case Type::FP128TyID:
643 case Type::PPC_FP128TyID:
Jay Foade1e20142009-05-12 20:27:44 +0000644 ReplaceCallWith(LDname, CI, CI->op_begin() + 1, CI->op_end(),
Dale Johannesenf74185b2008-09-22 20:51:30 +0000645 CI->getOperand(1)->getType(), LDCache);
646 break;
647 }
648}
Reid Spenceraddd11d2007-04-04 23:48:25 +0000649
Chris Lattnerb71fd782006-11-15 18:00:10 +0000650void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
Jay Foade1e20142009-05-12 20:27:44 +0000651 IRBuilder<> Builder(CI->getParent(), CI);
652
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000653 Function *Callee = CI->getCalledFunction();
654 assert(Callee && "Cannot lower an indirect call!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000655
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000656 switch (Callee->getIntrinsicID()) {
657 case Intrinsic::not_intrinsic:
Bill Wendlinge8156192006-12-07 01:30:32 +0000658 cerr << "Cannot lower a call to a non-intrinsic function '"
659 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000660 abort();
661 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000662 cerr << "Error: Code generator does not support intrinsic function '"
663 << Callee->getName() << "'!\n";
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000664 abort();
665
Chris Lattner588e72d2004-02-15 22:16:39 +0000666 // The setjmp/longjmp intrinsics should only exist in the code if it was
667 // never optimized (ie, right out of the CFE), or if it has been hacked on
668 // by the lowerinvoke pass. In both cases, the right thing to do is to
669 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000670 case Intrinsic::setjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000671 static Constant *SetjmpFCache = 0;
Jay Foade1e20142009-05-12 20:27:44 +0000672 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin() + 1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000673 Type::Int32Ty, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000674 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000675 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000676 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000677 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000678 case Intrinsic::sigsetjmp:
Chris Lattner9b700f72004-02-15 22:24:51 +0000679 if (CI->getType() != Type::VoidTy)
680 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
681 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000682
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000683 case Intrinsic::longjmp: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000684 static Constant *LongjmpFCache = 0;
Jay Foade1e20142009-05-12 20:27:44 +0000685 ReplaceCallWith("longjmp", CI, CI->op_begin() + 1, CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000686 Type::VoidTy, LongjmpFCache);
Chris Lattner9b700f72004-02-15 22:24:51 +0000687 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000688 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000689
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000690 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000691 // Insert the call to abort
Chris Lattnerb76efb72007-01-07 08:12:01 +0000692 static Constant *AbortFCache = 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000693 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
Chris Lattnerb76efb72007-01-07 08:12:01 +0000694 Type::VoidTy, AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000695 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000696 }
Reid Spencere9391fd2007-04-01 07:35:23 +0000697 case Intrinsic::ctpop:
Reid Spencer0b118202006-01-16 21:12:35 +0000698 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
699 break;
700
Reid Spencere9391fd2007-04-01 07:35:23 +0000701 case Intrinsic::bswap:
Nate Begemane5981812006-01-16 07:57:00 +0000702 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
703 break;
704
Reid Spencere9391fd2007-04-01 07:35:23 +0000705 case Intrinsic::ctlz:
Chris Lattner98cf45b2005-05-11 20:24:12 +0000706 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000707 break;
Nate Begemane5981812006-01-16 07:57:00 +0000708
Reid Spencere9391fd2007-04-01 07:35:23 +0000709 case Intrinsic::cttz: {
Chris Lattnera8011722005-05-11 20:02:14 +0000710 // cttz(x) -> ctpop(~X & (X-1))
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000711 Value *Src = CI->getOperand(1);
Jay Foade1e20142009-05-12 20:27:44 +0000712 Value *NotSrc = Builder.CreateNot(Src);
713 NotSrc->setName(Src->getName() + ".not");
Gabor Greif051a9502008-04-06 20:25:17 +0000714 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
Jay Foade1e20142009-05-12 20:27:44 +0000715 SrcM1 = Builder.CreateSub(Src, SrcM1);
716 Src = LowerCTPOP(Builder.CreateAnd(NotSrc, SrcM1), CI);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000717 CI->replaceAllUsesWith(Src);
718 break;
719 }
Chris Lattner77b13302004-01-05 05:36:30 +0000720
Chris Lattnerc6eb6d72007-04-10 03:20:39 +0000721 case Intrinsic::part_select:
Reid Spencerf75b8742007-04-12 02:48:46 +0000722 CI->replaceAllUsesWith(LowerPartSelect(CI));
723 break;
724
725 case Intrinsic::part_set:
726 CI->replaceAllUsesWith(LowerPartSet(CI));
Reid Spenceraddd11d2007-04-04 23:48:25 +0000727 break;
728
Chris Lattner0c067bc2006-01-13 02:22:08 +0000729 case Intrinsic::stacksave:
730 case Intrinsic::stackrestore: {
731 static bool Warned = false;
732 if (!Warned)
Bill Wendlinge8156192006-12-07 01:30:32 +0000733 cerr << "WARNING: this target does not support the llvm.stack"
734 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
735 "save" : "restore") << " intrinsic.\n";
Chris Lattner0c067bc2006-01-13 02:22:08 +0000736 Warned = true;
737 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
738 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
739 break;
740 }
741
Chris Lattnercf899082004-02-14 02:47:17 +0000742 case Intrinsic::returnaddress:
743 case Intrinsic::frameaddress:
Bill Wendlinge8156192006-12-07 01:30:32 +0000744 cerr << "WARNING: this target does not support the llvm."
745 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
746 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000747 CI->replaceAllUsesWith(ConstantPointerNull::get(
748 cast<PointerType>(CI->getType())));
749 break;
750
Chris Lattner0942b7c2005-02-28 19:27:23 +0000751 case Intrinsic::prefetch:
752 break; // Simply strip out prefetches on unsupported architectures
753
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000754 case Intrinsic::pcmarker:
755 break; // Simply strip out pcmarker on unsupported architectures
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000756 case Intrinsic::readcyclecounter: {
Bill Wendlinge8156192006-12-07 01:30:32 +0000757 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
758 << "ter intrinsic. It is being lowered to a constant 0\n";
Reid Spencer47857812006-12-31 05:55:36 +0000759 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000760 break;
761 }
Andrew Lenharth7f4ec3b2005-03-28 20:05:49 +0000762
Chris Lattner77b13302004-01-05 05:36:30 +0000763 case Intrinsic::dbg_stoppoint:
764 case Intrinsic::dbg_region_start:
765 case Intrinsic::dbg_region_end:
766 case Intrinsic::dbg_func_start:
Jim Laskey43970fe2006-03-23 18:06:46 +0000767 case Intrinsic::dbg_declare:
Duncan Sandsf664e412007-07-06 14:46:23 +0000768 break; // Simply strip out debugging intrinsics
769
Jim Laskeyb180aa12007-02-21 22:53:45 +0000770 case Intrinsic::eh_exception:
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000771 case Intrinsic::eh_selector_i32:
772 case Intrinsic::eh_selector_i64:
Duncan Sandsf664e412007-07-06 14:46:23 +0000773 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
774 break;
775
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000776 case Intrinsic::eh_typeid_for_i32:
777 case Intrinsic::eh_typeid_for_i64:
Duncan Sandsf664e412007-07-06 14:46:23 +0000778 // Return something different to eh_selector.
779 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
780 break;
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000781
Tanya Lattner24e5aad2007-06-15 22:26:58 +0000782 case Intrinsic::var_annotation:
783 break; // Strip out annotate intrinsic
784
Chris Lattner824b9582008-11-21 16:42:48 +0000785 case Intrinsic::memcpy: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000786 static Constant *MemcpyFCache = 0;
Jay Foad589b1ef2009-05-11 11:32:25 +0000787 const IntegerType *IntPtr = TD.getIntPtrType();
Jay Foade1e20142009-05-12 20:27:44 +0000788 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
789 /* isSigned */ false);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000790 Value *Ops[3];
791 Ops[0] = CI->getOperand(1);
792 Ops[1] = CI->getOperand(2);
793 Ops[2] = Size;
794 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
795 MemcpyFCache);
Reid Spencer3da59db2006-11-27 01:05:10 +0000796 break;
797 }
Chris Lattner824b9582008-11-21 16:42:48 +0000798 case Intrinsic::memmove: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000799 static Constant *MemmoveFCache = 0;
Jay Foad589b1ef2009-05-11 11:32:25 +0000800 const IntegerType *IntPtr = TD.getIntPtrType();
Jay Foade1e20142009-05-12 20:27:44 +0000801 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
802 /* isSigned */ false);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000803 Value *Ops[3];
804 Ops[0] = CI->getOperand(1);
805 Ops[1] = CI->getOperand(2);
806 Ops[2] = Size;
807 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
808 MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000809 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000810 }
Chris Lattner824b9582008-11-21 16:42:48 +0000811 case Intrinsic::memset: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000812 static Constant *MemsetFCache = 0;
Jay Foad589b1ef2009-05-11 11:32:25 +0000813 const IntegerType *IntPtr = TD.getIntPtrType();
Jay Foade1e20142009-05-12 20:27:44 +0000814 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
815 /* isSigned */ false);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000816 Value *Ops[3];
817 Ops[0] = CI->getOperand(1);
818 // Extend the amount to i32.
Jay Foade1e20142009-05-12 20:27:44 +0000819 Ops[1] = Builder.CreateIntCast(CI->getOperand(2), Type::Int32Ty,
820 /* isSigned */ false);
Chris Lattnerc67da0c2007-02-06 19:06:38 +0000821 Ops[2] = Size;
822 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
823 MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000824 break;
825 }
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000826 case Intrinsic::sqrt: {
Chris Lattnerb76efb72007-01-07 08:12:01 +0000827 static Constant *sqrtFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000828 static Constant *sqrtDCache = 0;
Dale Johannesen9ab7fb32007-10-02 17:43:59 +0000829 static Constant *sqrtLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000830 ReplaceFPIntrinsicWithCall(CI, sqrtFCache, sqrtDCache, sqrtLDCache,
831 "sqrtf", "sqrt", "sqrtl");
Dale Johannesen4292d1c2007-09-28 18:06:58 +0000832 break;
833 }
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000834 case Intrinsic::log: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000835 static Constant *logFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000836 static Constant *logDCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000837 static Constant *logLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000838 ReplaceFPIntrinsicWithCall(CI, logFCache, logDCache, logLDCache,
839 "logf", "log", "logl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000840 break;
841 }
842 case Intrinsic::log2: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000843 static Constant *log2FCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000844 static Constant *log2DCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000845 static Constant *log2LDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000846 ReplaceFPIntrinsicWithCall(CI, log2FCache, log2DCache, log2LDCache,
847 "log2f", "log2", "log2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000848 break;
849 }
850 case Intrinsic::log10: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000851 static Constant *log10FCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000852 static Constant *log10DCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000853 static Constant *log10LDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000854 ReplaceFPIntrinsicWithCall(CI, log10FCache, log10DCache, log10LDCache,
855 "log10f", "log10", "log10l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000856 break;
857 }
858 case Intrinsic::exp: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000859 static Constant *expFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000860 static Constant *expDCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000861 static Constant *expLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000862 ReplaceFPIntrinsicWithCall(CI, expFCache, expDCache, expLDCache,
863 "expf", "exp", "expl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000864 break;
865 }
866 case Intrinsic::exp2: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000867 static Constant *exp2FCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000868 static Constant *exp2DCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000869 static Constant *exp2LDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000870 ReplaceFPIntrinsicWithCall(CI, exp2FCache, exp2DCache, exp2LDCache,
871 "exp2f", "exp2", "exp2l");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000872 break;
873 }
874 case Intrinsic::pow: {
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000875 static Constant *powFCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000876 static Constant *powDCache = 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000877 static Constant *powLDCache = 0;
Dale Johannesenf74185b2008-09-22 20:51:30 +0000878 ReplaceFPIntrinsicWithCall(CI, powFCache, powDCache, powLDCache,
879 "powf", "pow", "powl");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000880 break;
881 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +0000882 case Intrinsic::flt_rounds:
883 // Lower to "round to the nearest"
884 if (CI->getType() != Type::VoidTy)
885 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
886 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000887 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000888
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000889 assert(CI->use_empty() &&
890 "Lowering should have eliminated any uses of the intrinsic call!");
Chris Lattner86f3e0c2005-05-11 19:42:05 +0000891 CI->eraseFromParent();
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000892}