Deleted Added
full compact
IntrinsicLowering.cpp (208954) IntrinsicLowering.cpp (210299)
1//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the IntrinsicLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
16#include "llvm/Module.h"
17#include "llvm/Type.h"
18#include "llvm/CodeGen/IntrinsicLowering.h"
1//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the IntrinsicLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
16#include "llvm/Module.h"
17#include "llvm/Type.h"
18#include "llvm/CodeGen/IntrinsicLowering.h"
19#include "llvm/Support/CallSite.h"
19#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/IRBuilder.h"
21#include "llvm/Support/raw_ostream.h"
22#include "llvm/Target/TargetData.h"
23#include "llvm/ADT/SmallVector.h"
24using namespace llvm;
25
26template <class ArgIt>
27static void EnsureFunctionExists(Module &M, const char *Name,
28 ArgIt ArgBegin, ArgIt ArgEnd,
29 const Type *RetTy) {
30 // Insert a correctly-typed definition now.
31 std::vector<const Type *> ParamTys;
32 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
33 ParamTys.push_back(I->getType());
34 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
35}
36
37static void EnsureFPIntrinsicsExist(Module &M, Function *Fn,
38 const char *FName,
39 const char *DName, const char *LDName) {
40 // Insert definitions for all the floating point types.
41 switch((int)Fn->arg_begin()->getType()->getTypeID()) {
42 case Type::FloatTyID:
43 EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(),
44 Type::getFloatTy(M.getContext()));
45 break;
46 case Type::DoubleTyID:
47 EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(),
48 Type::getDoubleTy(M.getContext()));
49 break;
50 case Type::X86_FP80TyID:
51 case Type::FP128TyID:
52 case Type::PPC_FP128TyID:
53 EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(),
54 Fn->arg_begin()->getType());
55 break;
56 }
57}
58
59/// ReplaceCallWith - This function is used when we want to lower an intrinsic
60/// call to a call of an external function. This handles hard cases such as
61/// when there was already a prototype for the external function, and if that
62/// prototype doesn't match the arguments we expect to pass in.
63template <class ArgIt>
64static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
65 ArgIt ArgBegin, ArgIt ArgEnd,
66 const Type *RetTy) {
67 // If we haven't already looked up this function, check to see if the
68 // program already contains a function with this name.
69 Module *M = CI->getParent()->getParent()->getParent();
70 // Get or insert the definition now.
71 std::vector<const Type *> ParamTys;
72 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
73 ParamTys.push_back((*I)->getType());
74 Constant* FCache = M->getOrInsertFunction(NewFn,
75 FunctionType::get(RetTy, ParamTys, false));
76
77 IRBuilder<> Builder(CI->getParent(), CI);
78 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
79 CallInst *NewCI = Builder.CreateCall(FCache, Args.begin(), Args.end());
80 NewCI->setName(CI->getName());
81 if (!CI->use_empty())
82 CI->replaceAllUsesWith(NewCI);
83 return NewCI;
84}
85
86// VisualStudio defines setjmp as _setjmp
87#if defined(_MSC_VER) && defined(setjmp)
88#define setjmp_undefined_for_visual_studio
89#undef setjmp
90#endif
91
92void IntrinsicLowering::AddPrototypes(Module &M) {
93 LLVMContext &Context = M.getContext();
94 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
95 if (I->isDeclaration() && !I->use_empty())
96 switch (I->getIntrinsicID()) {
97 default: break;
98 case Intrinsic::setjmp:
99 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
100 Type::getInt32Ty(M.getContext()));
101 break;
102 case Intrinsic::longjmp:
103 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
104 Type::getVoidTy(M.getContext()));
105 break;
106 case Intrinsic::siglongjmp:
107 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
108 Type::getVoidTy(M.getContext()));
109 break;
110 case Intrinsic::memcpy:
111 M.getOrInsertFunction("memcpy",
112 Type::getInt8PtrTy(Context),
113 Type::getInt8PtrTy(Context),
114 Type::getInt8PtrTy(Context),
115 TD.getIntPtrType(Context), (Type *)0);
116 break;
117 case Intrinsic::memmove:
118 M.getOrInsertFunction("memmove",
119 Type::getInt8PtrTy(Context),
120 Type::getInt8PtrTy(Context),
121 Type::getInt8PtrTy(Context),
122 TD.getIntPtrType(Context), (Type *)0);
123 break;
124 case Intrinsic::memset:
125 M.getOrInsertFunction("memset",
126 Type::getInt8PtrTy(Context),
127 Type::getInt8PtrTy(Context),
128 Type::getInt32Ty(M.getContext()),
129 TD.getIntPtrType(Context), (Type *)0);
130 break;
131 case Intrinsic::sqrt:
132 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
133 break;
134 case Intrinsic::sin:
135 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
136 break;
137 case Intrinsic::cos:
138 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
139 break;
140 case Intrinsic::pow:
141 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
142 break;
143 case Intrinsic::log:
144 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
145 break;
146 case Intrinsic::log2:
147 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
148 break;
149 case Intrinsic::log10:
150 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
151 break;
152 case Intrinsic::exp:
153 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
154 break;
155 case Intrinsic::exp2:
156 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
157 break;
158 }
159}
160
161/// LowerBSWAP - Emit the code to lower bswap of V before the specified
162/// instruction IP.
163static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
164 assert(V->getType()->isIntegerTy() && "Can't bswap a non-integer type!");
165
166 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
167
168 IRBuilder<> Builder(IP->getParent(), IP);
169
170 switch(BitSize) {
171 default: llvm_unreachable("Unhandled type size of value to byteswap!");
172 case 16: {
173 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
174 "bswap.2");
175 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
176 "bswap.1");
177 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
178 break;
179 }
180 case 32: {
181 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
182 "bswap.4");
183 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
184 "bswap.3");
185 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
186 "bswap.2");
187 Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
188 "bswap.1");
189 Tmp3 = Builder.CreateAnd(Tmp3,
190 ConstantInt::get(Type::getInt32Ty(Context), 0xFF0000),
191 "bswap.and3");
192 Tmp2 = Builder.CreateAnd(Tmp2,
193 ConstantInt::get(Type::getInt32Ty(Context), 0xFF00),
194 "bswap.and2");
195 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
196 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
197 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
198 break;
199 }
200 case 64: {
201 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
202 "bswap.8");
203 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
204 "bswap.7");
205 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
206 "bswap.6");
207 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
208 "bswap.5");
209 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
210 "bswap.4");
211 Value* Tmp3 = Builder.CreateLShr(V,
212 ConstantInt::get(V->getType(), 24),
213 "bswap.3");
214 Value* Tmp2 = Builder.CreateLShr(V,
215 ConstantInt::get(V->getType(), 40),
216 "bswap.2");
217 Value* Tmp1 = Builder.CreateLShr(V,
218 ConstantInt::get(V->getType(), 56),
219 "bswap.1");
220 Tmp7 = Builder.CreateAnd(Tmp7,
221 ConstantInt::get(Type::getInt64Ty(Context),
222 0xFF000000000000ULL),
223 "bswap.and7");
224 Tmp6 = Builder.CreateAnd(Tmp6,
225 ConstantInt::get(Type::getInt64Ty(Context),
226 0xFF0000000000ULL),
227 "bswap.and6");
228 Tmp5 = Builder.CreateAnd(Tmp5,
229 ConstantInt::get(Type::getInt64Ty(Context),
230 0xFF00000000ULL),
231 "bswap.and5");
232 Tmp4 = Builder.CreateAnd(Tmp4,
233 ConstantInt::get(Type::getInt64Ty(Context),
234 0xFF000000ULL),
235 "bswap.and4");
236 Tmp3 = Builder.CreateAnd(Tmp3,
237 ConstantInt::get(Type::getInt64Ty(Context),
238 0xFF0000ULL),
239 "bswap.and3");
240 Tmp2 = Builder.CreateAnd(Tmp2,
241 ConstantInt::get(Type::getInt64Ty(Context),
242 0xFF00ULL),
243 "bswap.and2");
244 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
245 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
246 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
247 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
248 Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
249 Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
250 V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
251 break;
252 }
253 }
254 return V;
255}
256
257/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
258/// instruction IP.
259static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
260 assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
261
262 static const uint64_t MaskValues[6] = {
263 0x5555555555555555ULL, 0x3333333333333333ULL,
264 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
265 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
266 };
267
268 IRBuilder<> Builder(IP->getParent(), IP);
269
270 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
271 unsigned WordSize = (BitSize + 63) / 64;
272 Value *Count = ConstantInt::get(V->getType(), 0);
273
274 for (unsigned n = 0; n < WordSize; ++n) {
275 Value *PartValue = V;
276 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
277 i <<= 1, ++ct) {
278 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
279 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
280 Value *VShift = Builder.CreateLShr(PartValue,
281 ConstantInt::get(V->getType(), i),
282 "ctpop.sh");
283 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
284 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
285 }
286 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
287 if (BitSize > 64) {
288 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
289 "ctpop.part.sh");
290 BitSize -= 64;
291 }
292 }
293
294 return Count;
295}
296
297/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
298/// instruction IP.
299static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
300
301 IRBuilder<> Builder(IP->getParent(), IP);
302
303 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
304 for (unsigned i = 1; i < BitSize; i <<= 1) {
305 Value *ShVal = ConstantInt::get(V->getType(), i);
306 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
307 V = Builder.CreateOr(V, ShVal, "ctlz.step");
308 }
309
310 V = Builder.CreateNot(V);
311 return LowerCTPOP(Context, V, IP);
312}
313
314static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
315 const char *Dname,
316 const char *LDname) {
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/IRBuilder.h"
22#include "llvm/Support/raw_ostream.h"
23#include "llvm/Target/TargetData.h"
24#include "llvm/ADT/SmallVector.h"
25using namespace llvm;
26
27template <class ArgIt>
28static void EnsureFunctionExists(Module &M, const char *Name,
29 ArgIt ArgBegin, ArgIt ArgEnd,
30 const Type *RetTy) {
31 // Insert a correctly-typed definition now.
32 std::vector<const Type *> ParamTys;
33 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
34 ParamTys.push_back(I->getType());
35 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
36}
37
38static void EnsureFPIntrinsicsExist(Module &M, Function *Fn,
39 const char *FName,
40 const char *DName, const char *LDName) {
41 // Insert definitions for all the floating point types.
42 switch((int)Fn->arg_begin()->getType()->getTypeID()) {
43 case Type::FloatTyID:
44 EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(),
45 Type::getFloatTy(M.getContext()));
46 break;
47 case Type::DoubleTyID:
48 EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(),
49 Type::getDoubleTy(M.getContext()));
50 break;
51 case Type::X86_FP80TyID:
52 case Type::FP128TyID:
53 case Type::PPC_FP128TyID:
54 EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(),
55 Fn->arg_begin()->getType());
56 break;
57 }
58}
59
60/// ReplaceCallWith - This function is used when we want to lower an intrinsic
61/// call to a call of an external function. This handles hard cases such as
62/// when there was already a prototype for the external function, and if that
63/// prototype doesn't match the arguments we expect to pass in.
64template <class ArgIt>
65static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
66 ArgIt ArgBegin, ArgIt ArgEnd,
67 const Type *RetTy) {
68 // If we haven't already looked up this function, check to see if the
69 // program already contains a function with this name.
70 Module *M = CI->getParent()->getParent()->getParent();
71 // Get or insert the definition now.
72 std::vector<const Type *> ParamTys;
73 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
74 ParamTys.push_back((*I)->getType());
75 Constant* FCache = M->getOrInsertFunction(NewFn,
76 FunctionType::get(RetTy, ParamTys, false));
77
78 IRBuilder<> Builder(CI->getParent(), CI);
79 SmallVector<Value *, 8> Args(ArgBegin, ArgEnd);
80 CallInst *NewCI = Builder.CreateCall(FCache, Args.begin(), Args.end());
81 NewCI->setName(CI->getName());
82 if (!CI->use_empty())
83 CI->replaceAllUsesWith(NewCI);
84 return NewCI;
85}
86
87// VisualStudio defines setjmp as _setjmp
88#if defined(_MSC_VER) && defined(setjmp)
89#define setjmp_undefined_for_visual_studio
90#undef setjmp
91#endif
92
93void IntrinsicLowering::AddPrototypes(Module &M) {
94 LLVMContext &Context = M.getContext();
95 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
96 if (I->isDeclaration() && !I->use_empty())
97 switch (I->getIntrinsicID()) {
98 default: break;
99 case Intrinsic::setjmp:
100 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
101 Type::getInt32Ty(M.getContext()));
102 break;
103 case Intrinsic::longjmp:
104 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
105 Type::getVoidTy(M.getContext()));
106 break;
107 case Intrinsic::siglongjmp:
108 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
109 Type::getVoidTy(M.getContext()));
110 break;
111 case Intrinsic::memcpy:
112 M.getOrInsertFunction("memcpy",
113 Type::getInt8PtrTy(Context),
114 Type::getInt8PtrTy(Context),
115 Type::getInt8PtrTy(Context),
116 TD.getIntPtrType(Context), (Type *)0);
117 break;
118 case Intrinsic::memmove:
119 M.getOrInsertFunction("memmove",
120 Type::getInt8PtrTy(Context),
121 Type::getInt8PtrTy(Context),
122 Type::getInt8PtrTy(Context),
123 TD.getIntPtrType(Context), (Type *)0);
124 break;
125 case Intrinsic::memset:
126 M.getOrInsertFunction("memset",
127 Type::getInt8PtrTy(Context),
128 Type::getInt8PtrTy(Context),
129 Type::getInt32Ty(M.getContext()),
130 TD.getIntPtrType(Context), (Type *)0);
131 break;
132 case Intrinsic::sqrt:
133 EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
134 break;
135 case Intrinsic::sin:
136 EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
137 break;
138 case Intrinsic::cos:
139 EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
140 break;
141 case Intrinsic::pow:
142 EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
143 break;
144 case Intrinsic::log:
145 EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
146 break;
147 case Intrinsic::log2:
148 EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
149 break;
150 case Intrinsic::log10:
151 EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
152 break;
153 case Intrinsic::exp:
154 EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
155 break;
156 case Intrinsic::exp2:
157 EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
158 break;
159 }
160}
161
162/// LowerBSWAP - Emit the code to lower bswap of V before the specified
163/// instruction IP.
164static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
165 assert(V->getType()->isIntegerTy() && "Can't bswap a non-integer type!");
166
167 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
168
169 IRBuilder<> Builder(IP->getParent(), IP);
170
171 switch(BitSize) {
172 default: llvm_unreachable("Unhandled type size of value to byteswap!");
173 case 16: {
174 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
175 "bswap.2");
176 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
177 "bswap.1");
178 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
179 break;
180 }
181 case 32: {
182 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
183 "bswap.4");
184 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
185 "bswap.3");
186 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
187 "bswap.2");
188 Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
189 "bswap.1");
190 Tmp3 = Builder.CreateAnd(Tmp3,
191 ConstantInt::get(Type::getInt32Ty(Context), 0xFF0000),
192 "bswap.and3");
193 Tmp2 = Builder.CreateAnd(Tmp2,
194 ConstantInt::get(Type::getInt32Ty(Context), 0xFF00),
195 "bswap.and2");
196 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
197 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
198 V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
199 break;
200 }
201 case 64: {
202 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
203 "bswap.8");
204 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
205 "bswap.7");
206 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
207 "bswap.6");
208 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
209 "bswap.5");
210 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
211 "bswap.4");
212 Value* Tmp3 = Builder.CreateLShr(V,
213 ConstantInt::get(V->getType(), 24),
214 "bswap.3");
215 Value* Tmp2 = Builder.CreateLShr(V,
216 ConstantInt::get(V->getType(), 40),
217 "bswap.2");
218 Value* Tmp1 = Builder.CreateLShr(V,
219 ConstantInt::get(V->getType(), 56),
220 "bswap.1");
221 Tmp7 = Builder.CreateAnd(Tmp7,
222 ConstantInt::get(Type::getInt64Ty(Context),
223 0xFF000000000000ULL),
224 "bswap.and7");
225 Tmp6 = Builder.CreateAnd(Tmp6,
226 ConstantInt::get(Type::getInt64Ty(Context),
227 0xFF0000000000ULL),
228 "bswap.and6");
229 Tmp5 = Builder.CreateAnd(Tmp5,
230 ConstantInt::get(Type::getInt64Ty(Context),
231 0xFF00000000ULL),
232 "bswap.and5");
233 Tmp4 = Builder.CreateAnd(Tmp4,
234 ConstantInt::get(Type::getInt64Ty(Context),
235 0xFF000000ULL),
236 "bswap.and4");
237 Tmp3 = Builder.CreateAnd(Tmp3,
238 ConstantInt::get(Type::getInt64Ty(Context),
239 0xFF0000ULL),
240 "bswap.and3");
241 Tmp2 = Builder.CreateAnd(Tmp2,
242 ConstantInt::get(Type::getInt64Ty(Context),
243 0xFF00ULL),
244 "bswap.and2");
245 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
246 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
247 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
248 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
249 Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
250 Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
251 V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
252 break;
253 }
254 }
255 return V;
256}
257
258/// LowerCTPOP - Emit the code to lower ctpop of V before the specified
259/// instruction IP.
260static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
261 assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
262
263 static const uint64_t MaskValues[6] = {
264 0x5555555555555555ULL, 0x3333333333333333ULL,
265 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
266 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
267 };
268
269 IRBuilder<> Builder(IP->getParent(), IP);
270
271 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
272 unsigned WordSize = (BitSize + 63) / 64;
273 Value *Count = ConstantInt::get(V->getType(), 0);
274
275 for (unsigned n = 0; n < WordSize; ++n) {
276 Value *PartValue = V;
277 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
278 i <<= 1, ++ct) {
279 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
280 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
281 Value *VShift = Builder.CreateLShr(PartValue,
282 ConstantInt::get(V->getType(), i),
283 "ctpop.sh");
284 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
285 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
286 }
287 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
288 if (BitSize > 64) {
289 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
290 "ctpop.part.sh");
291 BitSize -= 64;
292 }
293 }
294
295 return Count;
296}
297
298/// LowerCTLZ - Emit the code to lower ctlz of V before the specified
299/// instruction IP.
300static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
301
302 IRBuilder<> Builder(IP->getParent(), IP);
303
304 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
305 for (unsigned i = 1; i < BitSize; i <<= 1) {
306 Value *ShVal = ConstantInt::get(V->getType(), i);
307 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
308 V = Builder.CreateOr(V, ShVal, "ctlz.step");
309 }
310
311 V = Builder.CreateNot(V);
312 return LowerCTPOP(Context, V, IP);
313}
314
315static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
316 const char *Dname,
317 const char *LDname) {
317 switch (CI->getOperand(1)->getType()->getTypeID()) {
318 CallSite CS(CI);
319 switch (CI->getArgOperand(0)->getType()->getTypeID()) {
318 default: llvm_unreachable("Invalid type in intrinsic");
319 case Type::FloatTyID:
320 default: llvm_unreachable("Invalid type in intrinsic");
321 case Type::FloatTyID:
320 ReplaceCallWith(Fname, CI, CI->op_begin() + 1, CI->op_end(),
322 ReplaceCallWith(Fname, CI, CS.arg_begin(), CS.arg_end(),
321 Type::getFloatTy(CI->getContext()));
322 break;
323 case Type::DoubleTyID:
323 Type::getFloatTy(CI->getContext()));
324 break;
325 case Type::DoubleTyID:
324 ReplaceCallWith(Dname, CI, CI->op_begin() + 1, CI->op_end(),
326 ReplaceCallWith(Dname, CI, CS.arg_begin(), CS.arg_end(),
325 Type::getDoubleTy(CI->getContext()));
326 break;
327 case Type::X86_FP80TyID:
328 case Type::FP128TyID:
329 case Type::PPC_FP128TyID:
327 Type::getDoubleTy(CI->getContext()));
328 break;
329 case Type::X86_FP80TyID:
330 case Type::FP128TyID:
331 case Type::PPC_FP128TyID:
330 ReplaceCallWith(LDname, CI, CI->op_begin() + 1, CI->op_end(),
331 CI->getOperand(1)->getType());
332 ReplaceCallWith(LDname, CI, CS.arg_begin(), CS.arg_end(),
333 CI->getArgOperand(0)->getType());
332 break;
333 }
334}
335
336void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
337 IRBuilder<> Builder(CI->getParent(), CI);
338 LLVMContext &Context = CI->getContext();
339
340 const Function *Callee = CI->getCalledFunction();
341 assert(Callee && "Cannot lower an indirect call!");
342
334 break;
335 }
336}
337
338void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
339 IRBuilder<> Builder(CI->getParent(), CI);
340 LLVMContext &Context = CI->getContext();
341
342 const Function *Callee = CI->getCalledFunction();
343 assert(Callee && "Cannot lower an indirect call!");
344
345 CallSite CS(CI);
343 switch (Callee->getIntrinsicID()) {
344 case Intrinsic::not_intrinsic:
345 report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
346 Callee->getName() + "'!");
347 default:
348 report_fatal_error("Code generator does not support intrinsic function '"+
349 Callee->getName()+"'!");
350
351 // The setjmp/longjmp intrinsics should only exist in the code if it was
352 // never optimized (ie, right out of the CFE), or if it has been hacked on
353 // by the lowerinvoke pass. In both cases, the right thing to do is to
354 // convert the call to an explicit setjmp or longjmp call.
355 case Intrinsic::setjmp: {
346 switch (Callee->getIntrinsicID()) {
347 case Intrinsic::not_intrinsic:
348 report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
349 Callee->getName() + "'!");
350 default:
351 report_fatal_error("Code generator does not support intrinsic function '"+
352 Callee->getName()+"'!");
353
354 // The setjmp/longjmp intrinsics should only exist in the code if it was
355 // never optimized (ie, right out of the CFE), or if it has been hacked on
356 // by the lowerinvoke pass. In both cases, the right thing to do is to
357 // convert the call to an explicit setjmp or longjmp call.
358 case Intrinsic::setjmp: {
356 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin() + 1, CI->op_end(),
359 Value *V = ReplaceCallWith("setjmp", CI, CS.arg_begin(), CS.arg_end(),
357 Type::getInt32Ty(Context));
358 if (!CI->getType()->isVoidTy())
359 CI->replaceAllUsesWith(V);
360 break;
361 }
362 case Intrinsic::sigsetjmp:
363 if (!CI->getType()->isVoidTy())
364 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
365 break;
366
367 case Intrinsic::longjmp: {
360 Type::getInt32Ty(Context));
361 if (!CI->getType()->isVoidTy())
362 CI->replaceAllUsesWith(V);
363 break;
364 }
365 case Intrinsic::sigsetjmp:
366 if (!CI->getType()->isVoidTy())
367 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
368 break;
369
370 case Intrinsic::longjmp: {
368 ReplaceCallWith("longjmp", CI, CI->op_begin() + 1, CI->op_end(),
371 ReplaceCallWith("longjmp", CI, CS.arg_begin(), CS.arg_end(),
369 Type::getVoidTy(Context));
370 break;
371 }
372
373 case Intrinsic::siglongjmp: {
374 // Insert the call to abort
372 Type::getVoidTy(Context));
373 break;
374 }
375
376 case Intrinsic::siglongjmp: {
377 // Insert the call to abort
375 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(),
378 ReplaceCallWith("abort", CI, CS.arg_end(), CS.arg_end(),
376 Type::getVoidTy(Context));
377 break;
378 }
379 case Intrinsic::ctpop:
379 Type::getVoidTy(Context));
380 break;
381 }
382 case Intrinsic::ctpop:
380 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getOperand(1), CI));
383 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getArgOperand(0), CI));
381 break;
382
383 case Intrinsic::bswap:
384 break;
385
386 case Intrinsic::bswap:
384 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getOperand(1), CI));
387 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getArgOperand(0), CI));
385 break;
386
387 case Intrinsic::ctlz:
388 break;
389
390 case Intrinsic::ctlz:
388 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getOperand(1), CI));
391 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getArgOperand(0), CI));
389 break;
390
391 case Intrinsic::cttz: {
392 // cttz(x) -> ctpop(~X & (X-1))
392 break;
393
394 case Intrinsic::cttz: {
395 // cttz(x) -> ctpop(~X & (X-1))
393 Value *Src = CI->getOperand(1);
396 Value *Src = CI->getArgOperand(0);
394 Value *NotSrc = Builder.CreateNot(Src);
395 NotSrc->setName(Src->getName() + ".not");
396 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
397 SrcM1 = Builder.CreateSub(Src, SrcM1);
398 Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
399 CI->replaceAllUsesWith(Src);
400 break;
401 }
402
403 case Intrinsic::stacksave:
404 case Intrinsic::stackrestore: {
405 if (!Warned)
406 errs() << "WARNING: this target does not support the llvm.stack"
407 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
408 "save" : "restore") << " intrinsic.\n";
409 Warned = true;
410 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
411 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
412 break;
413 }
414
415 case Intrinsic::returnaddress:
416 case Intrinsic::frameaddress:
417 errs() << "WARNING: this target does not support the llvm."
418 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
419 "return" : "frame") << "address intrinsic.\n";
420 CI->replaceAllUsesWith(ConstantPointerNull::get(
421 cast<PointerType>(CI->getType())));
422 break;
423
424 case Intrinsic::prefetch:
425 break; // Simply strip out prefetches on unsupported architectures
426
427 case Intrinsic::pcmarker:
428 break; // Simply strip out pcmarker on unsupported architectures
429 case Intrinsic::readcyclecounter: {
430 errs() << "WARNING: this target does not support the llvm.readcyclecoun"
431 << "ter intrinsic. It is being lowered to a constant 0\n";
432 CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
433 break;
434 }
435
436 case Intrinsic::dbg_declare:
437 break; // Simply strip out debugging intrinsics
438
439 case Intrinsic::eh_exception:
440 case Intrinsic::eh_selector:
441 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
442 break;
443
444 case Intrinsic::eh_typeid_for:
445 // Return something different to eh_selector.
446 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
447 break;
448
449 case Intrinsic::var_annotation:
450 break; // Strip out annotate intrinsic
451
452 case Intrinsic::memcpy: {
453 const IntegerType *IntPtr = TD.getIntPtrType(Context);
397 Value *NotSrc = Builder.CreateNot(Src);
398 NotSrc->setName(Src->getName() + ".not");
399 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
400 SrcM1 = Builder.CreateSub(Src, SrcM1);
401 Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
402 CI->replaceAllUsesWith(Src);
403 break;
404 }
405
406 case Intrinsic::stacksave:
407 case Intrinsic::stackrestore: {
408 if (!Warned)
409 errs() << "WARNING: this target does not support the llvm.stack"
410 << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
411 "save" : "restore") << " intrinsic.\n";
412 Warned = true;
413 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
414 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
415 break;
416 }
417
418 case Intrinsic::returnaddress:
419 case Intrinsic::frameaddress:
420 errs() << "WARNING: this target does not support the llvm."
421 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
422 "return" : "frame") << "address intrinsic.\n";
423 CI->replaceAllUsesWith(ConstantPointerNull::get(
424 cast<PointerType>(CI->getType())));
425 break;
426
427 case Intrinsic::prefetch:
428 break; // Simply strip out prefetches on unsupported architectures
429
430 case Intrinsic::pcmarker:
431 break; // Simply strip out pcmarker on unsupported architectures
432 case Intrinsic::readcyclecounter: {
433 errs() << "WARNING: this target does not support the llvm.readcyclecoun"
434 << "ter intrinsic. It is being lowered to a constant 0\n";
435 CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
436 break;
437 }
438
439 case Intrinsic::dbg_declare:
440 break; // Simply strip out debugging intrinsics
441
442 case Intrinsic::eh_exception:
443 case Intrinsic::eh_selector:
444 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
445 break;
446
447 case Intrinsic::eh_typeid_for:
448 // Return something different to eh_selector.
449 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
450 break;
451
452 case Intrinsic::var_annotation:
453 break; // Strip out annotate intrinsic
454
455 case Intrinsic::memcpy: {
456 const IntegerType *IntPtr = TD.getIntPtrType(Context);
454 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
457 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
455 /* isSigned */ false);
456 Value *Ops[3];
458 /* isSigned */ false);
459 Value *Ops[3];
457 Ops[0] = CI->getOperand(1);
458 Ops[1] = CI->getOperand(2);
460 Ops[0] = CI->getArgOperand(0);
461 Ops[1] = CI->getArgOperand(1);
459 Ops[2] = Size;
462 Ops[2] = Size;
460 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType());
463 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
461 break;
462 }
463 case Intrinsic::memmove: {
464 const IntegerType *IntPtr = TD.getIntPtrType(Context);
464 break;
465 }
466 case Intrinsic::memmove: {
467 const IntegerType *IntPtr = TD.getIntPtrType(Context);
465 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
468 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
466 /* isSigned */ false);
467 Value *Ops[3];
469 /* isSigned */ false);
470 Value *Ops[3];
468 Ops[0] = CI->getOperand(1);
469 Ops[1] = CI->getOperand(2);
471 Ops[0] = CI->getArgOperand(0);
472 Ops[1] = CI->getArgOperand(1);
470 Ops[2] = Size;
473 Ops[2] = Size;
471 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType());
474 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
472 break;
473 }
474 case Intrinsic::memset: {
475 const IntegerType *IntPtr = TD.getIntPtrType(Context);
475 break;
476 }
477 case Intrinsic::memset: {
478 const IntegerType *IntPtr = TD.getIntPtrType(Context);
476 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
479 Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
477 /* isSigned */ false);
478 Value *Ops[3];
480 /* isSigned */ false);
481 Value *Ops[3];
479 Ops[0] = CI->getOperand(1);
482 Ops[0] = CI->getArgOperand(0);
480 // Extend the amount to i32.
483 // Extend the amount to i32.
481 Ops[1] = Builder.CreateIntCast(CI->getOperand(2), Type::getInt32Ty(Context),
484 Ops[1] = Builder.CreateIntCast(CI->getArgOperand(1), Type::getInt32Ty(Context),
482 /* isSigned */ false);
483 Ops[2] = Size;
485 /* isSigned */ false);
486 Ops[2] = Size;
484 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType());
487 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
485 break;
486 }
487 case Intrinsic::sqrt: {
488 ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
489 break;
490 }
491 case Intrinsic::log: {
492 ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
493 break;
494 }
495 case Intrinsic::log2: {
496 ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
497 break;
498 }
499 case Intrinsic::log10: {
500 ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
501 break;
502 }
503 case Intrinsic::exp: {
504 ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
505 break;
506 }
507 case Intrinsic::exp2: {
508 ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
509 break;
510 }
511 case Intrinsic::pow: {
512 ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
513 break;
514 }
515 case Intrinsic::flt_rounds:
516 // Lower to "round to the nearest"
517 if (!CI->getType()->isVoidTy())
518 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
519 break;
520 case Intrinsic::invariant_start:
521 case Intrinsic::lifetime_start:
522 // Discard region information.
523 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
524 break;
525 case Intrinsic::invariant_end:
526 case Intrinsic::lifetime_end:
527 // Discard region information.
528 break;
529 }
530
531 assert(CI->use_empty() &&
532 "Lowering should have eliminated any uses of the intrinsic call!");
533 CI->eraseFromParent();
534}
488 break;
489 }
490 case Intrinsic::sqrt: {
491 ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
492 break;
493 }
494 case Intrinsic::log: {
495 ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
496 break;
497 }
498 case Intrinsic::log2: {
499 ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
500 break;
501 }
502 case Intrinsic::log10: {
503 ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
504 break;
505 }
506 case Intrinsic::exp: {
507 ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
508 break;
509 }
510 case Intrinsic::exp2: {
511 ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
512 break;
513 }
514 case Intrinsic::pow: {
515 ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
516 break;
517 }
518 case Intrinsic::flt_rounds:
519 // Lower to "round to the nearest"
520 if (!CI->getType()->isVoidTy())
521 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
522 break;
523 case Intrinsic::invariant_start:
524 case Intrinsic::lifetime_start:
525 // Discard region information.
526 CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
527 break;
528 case Intrinsic::invariant_end:
529 case Intrinsic::lifetime_end:
530 // Discard region information.
531 break;
532 }
533
534 assert(CI->use_empty() &&
535 "Lowering should have eliminated any uses of the intrinsic call!");
536 CI->eraseFromParent();
537}