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