Rework the global split cost calculation.
[oota-llvm.git] / lib / CodeGen / IntrinsicLowering.cpp
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"
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"
25 using namespace llvm;
26
27 template <class ArgIt>
28 static 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
38 static 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.
64 template <class ArgIt>
65 static 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                          !defined(setjmp_undefined_for_msvc)
90 #  pragma push_macro("setjmp")
91 #  undef setjmp
92 #  define setjmp_undefined_for_msvc
93 #endif
94
95 void IntrinsicLowering::AddPrototypes(Module &M) {
96   LLVMContext &Context = M.getContext();
97   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
98     if (I->isDeclaration() && !I->use_empty())
99       switch (I->getIntrinsicID()) {
100       default: break;
101       case Intrinsic::setjmp:
102         EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
103                              Type::getInt32Ty(M.getContext()));
104         break;
105       case Intrinsic::longjmp:
106         EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
107                              Type::getVoidTy(M.getContext()));
108         break;
109       case Intrinsic::siglongjmp:
110         EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
111                              Type::getVoidTy(M.getContext()));
112         break;
113       case Intrinsic::memcpy:
114         M.getOrInsertFunction("memcpy",
115           Type::getInt8PtrTy(Context),
116                               Type::getInt8PtrTy(Context), 
117                               Type::getInt8PtrTy(Context), 
118                               TD.getIntPtrType(Context), (Type *)0);
119         break;
120       case Intrinsic::memmove:
121         M.getOrInsertFunction("memmove",
122           Type::getInt8PtrTy(Context),
123                               Type::getInt8PtrTy(Context), 
124                               Type::getInt8PtrTy(Context), 
125                               TD.getIntPtrType(Context), (Type *)0);
126         break;
127       case Intrinsic::memset:
128         M.getOrInsertFunction("memset",
129           Type::getInt8PtrTy(Context),
130                               Type::getInt8PtrTy(Context), 
131                               Type::getInt32Ty(M.getContext()), 
132                               TD.getIntPtrType(Context), (Type *)0);
133         break;
134       case Intrinsic::sqrt:
135         EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl");
136         break;
137       case Intrinsic::sin:
138         EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl");
139         break;
140       case Intrinsic::cos:
141         EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl");
142         break;
143       case Intrinsic::pow:
144         EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl");
145         break;
146       case Intrinsic::log:
147         EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl");
148         break;
149       case Intrinsic::log2:
150         EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l");
151         break;
152       case Intrinsic::log10:
153         EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l");
154         break;
155       case Intrinsic::exp:
156         EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl");
157         break;
158       case Intrinsic::exp2:
159         EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l");
160         break;
161       }
162 }
163
164 /// LowerBSWAP - Emit the code to lower bswap of V before the specified
165 /// instruction IP.
166 static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) {
167   assert(V->getType()->isIntegerTy() && "Can't bswap a non-integer type!");
168
169   unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
170   
171   IRBuilder<> Builder(IP->getParent(), IP);
172
173   switch(BitSize) {
174   default: llvm_unreachable("Unhandled type size of value to byteswap!");
175   case 16: {
176     Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
177                                     "bswap.2");
178     Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
179                                      "bswap.1");
180     V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
181     break;
182   }
183   case 32: {
184     Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
185                                     "bswap.4");
186     Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
187                                     "bswap.3");
188     Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
189                                      "bswap.2");
190     Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
191                                      "bswap.1");
192     Tmp3 = Builder.CreateAnd(Tmp3,
193                          ConstantInt::get(Type::getInt32Ty(Context), 0xFF0000),
194                              "bswap.and3");
195     Tmp2 = Builder.CreateAnd(Tmp2,
196                            ConstantInt::get(Type::getInt32Ty(Context), 0xFF00),
197                              "bswap.and2");
198     Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
199     Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
200     V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32");
201     break;
202   }
203   case 64: {
204     Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
205                                     "bswap.8");
206     Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
207                                     "bswap.7");
208     Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
209                                     "bswap.6");
210     Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
211                                     "bswap.5");
212     Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
213                                      "bswap.4");
214     Value* Tmp3 = Builder.CreateLShr(V, 
215                                      ConstantInt::get(V->getType(), 24),
216                                      "bswap.3");
217     Value* Tmp2 = Builder.CreateLShr(V, 
218                                      ConstantInt::get(V->getType(), 40),
219                                      "bswap.2");
220     Value* Tmp1 = Builder.CreateLShr(V, 
221                                      ConstantInt::get(V->getType(), 56),
222                                      "bswap.1");
223     Tmp7 = Builder.CreateAnd(Tmp7,
224                              ConstantInt::get(Type::getInt64Ty(Context),
225                                               0xFF000000000000ULL),
226                              "bswap.and7");
227     Tmp6 = Builder.CreateAnd(Tmp6,
228                              ConstantInt::get(Type::getInt64Ty(Context),
229                                               0xFF0000000000ULL),
230                              "bswap.and6");
231     Tmp5 = Builder.CreateAnd(Tmp5,
232                         ConstantInt::get(Type::getInt64Ty(Context),
233                              0xFF00000000ULL),
234                              "bswap.and5");
235     Tmp4 = Builder.CreateAnd(Tmp4,
236                         ConstantInt::get(Type::getInt64Ty(Context),
237                              0xFF000000ULL),
238                              "bswap.and4");
239     Tmp3 = Builder.CreateAnd(Tmp3,
240                              ConstantInt::get(Type::getInt64Ty(Context),
241                              0xFF0000ULL),
242                              "bswap.and3");
243     Tmp2 = Builder.CreateAnd(Tmp2,
244                              ConstantInt::get(Type::getInt64Ty(Context),
245                              0xFF00ULL),
246                              "bswap.and2");
247     Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
248     Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
249     Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3");
250     Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4");
251     Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5");
252     Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6");
253     V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64");
254     break;
255   }
256   }
257   return V;
258 }
259
260 /// LowerCTPOP - Emit the code to lower ctpop of V before the specified
261 /// instruction IP.
262 static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) {
263   assert(V->getType()->isIntegerTy() && "Can't ctpop a non-integer type!");
264
265   static const uint64_t MaskValues[6] = {
266     0x5555555555555555ULL, 0x3333333333333333ULL,
267     0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
268     0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
269   };
270
271   IRBuilder<> Builder(IP->getParent(), IP);
272
273   unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
274   unsigned WordSize = (BitSize + 63) / 64;
275   Value *Count = ConstantInt::get(V->getType(), 0);
276
277   for (unsigned n = 0; n < WordSize; ++n) {
278     Value *PartValue = V;
279     for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize); 
280          i <<= 1, ++ct) {
281       Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
282       Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
283       Value *VShift = Builder.CreateLShr(PartValue,
284                                         ConstantInt::get(V->getType(), i),
285                                          "ctpop.sh");
286       Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
287       PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
288     }
289     Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
290     if (BitSize > 64) {
291       V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
292                              "ctpop.part.sh");
293       BitSize -= 64;
294     }
295   }
296
297   return Count;
298 }
299
300 /// LowerCTLZ - Emit the code to lower ctlz of V before the specified
301 /// instruction IP.
302 static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) {
303
304   IRBuilder<> Builder(IP->getParent(), IP);
305
306   unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
307   for (unsigned i = 1; i < BitSize; i <<= 1) {
308     Value *ShVal = ConstantInt::get(V->getType(), i);
309     ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
310     V = Builder.CreateOr(V, ShVal, "ctlz.step");
311   }
312
313   V = Builder.CreateNot(V);
314   return LowerCTPOP(Context, V, IP);
315 }
316
317 static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
318                                        const char *Dname,
319                                        const char *LDname) {
320   CallSite CS(CI);
321   switch (CI->getArgOperand(0)->getType()->getTypeID()) {
322   default: llvm_unreachable("Invalid type in intrinsic");
323   case Type::FloatTyID:
324     ReplaceCallWith(Fname, CI, CS.arg_begin(), CS.arg_end(),
325                   Type::getFloatTy(CI->getContext()));
326     break;
327   case Type::DoubleTyID:
328     ReplaceCallWith(Dname, CI, CS.arg_begin(), CS.arg_end(),
329                   Type::getDoubleTy(CI->getContext()));
330     break;
331   case Type::X86_FP80TyID:
332   case Type::FP128TyID:
333   case Type::PPC_FP128TyID:
334     ReplaceCallWith(LDname, CI, CS.arg_begin(), CS.arg_end(),
335                   CI->getArgOperand(0)->getType());
336     break;
337   }
338 }
339
340 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
341   IRBuilder<> Builder(CI->getParent(), CI);
342   LLVMContext &Context = CI->getContext();
343
344   const Function *Callee = CI->getCalledFunction();
345   assert(Callee && "Cannot lower an indirect call!");
346
347   CallSite CS(CI);
348   switch (Callee->getIntrinsicID()) {
349   case Intrinsic::not_intrinsic:
350     report_fatal_error("Cannot lower a call to a non-intrinsic function '"+
351                       Callee->getName() + "'!");
352   default:
353     report_fatal_error("Code generator does not support intrinsic function '"+
354                       Callee->getName()+"'!");
355
356     // The setjmp/longjmp intrinsics should only exist in the code if it was
357     // never optimized (ie, right out of the CFE), or if it has been hacked on
358     // by the lowerinvoke pass.  In both cases, the right thing to do is to
359     // convert the call to an explicit setjmp or longjmp call.
360   case Intrinsic::setjmp: {
361     Value *V = ReplaceCallWith("setjmp", CI, CS.arg_begin(), CS.arg_end(),
362                                Type::getInt32Ty(Context));
363     if (!CI->getType()->isVoidTy())
364       CI->replaceAllUsesWith(V);
365     break;
366   }
367   case Intrinsic::sigsetjmp:
368      if (!CI->getType()->isVoidTy())
369        CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
370      break;
371
372   case Intrinsic::longjmp: {
373     ReplaceCallWith("longjmp", CI, CS.arg_begin(), CS.arg_end(),
374                     Type::getVoidTy(Context));
375     break;
376   }
377
378   case Intrinsic::siglongjmp: {
379     // Insert the call to abort
380     ReplaceCallWith("abort", CI, CS.arg_end(), CS.arg_end(), 
381                     Type::getVoidTy(Context));
382     break;
383   }
384   case Intrinsic::ctpop:
385     CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getArgOperand(0), CI));
386     break;
387
388   case Intrinsic::bswap:
389     CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getArgOperand(0), CI));
390     break;
391     
392   case Intrinsic::ctlz:
393     CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getArgOperand(0), CI));
394     break;
395
396   case Intrinsic::cttz: {
397     // cttz(x) -> ctpop(~X & (X-1))
398     Value *Src = CI->getArgOperand(0);
399     Value *NotSrc = Builder.CreateNot(Src);
400     NotSrc->setName(Src->getName() + ".not");
401     Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
402     SrcM1 = Builder.CreateSub(Src, SrcM1);
403     Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
404     CI->replaceAllUsesWith(Src);
405     break;
406   }
407
408   case Intrinsic::stacksave:
409   case Intrinsic::stackrestore: {
410     if (!Warned)
411       errs() << "WARNING: this target does not support the llvm.stack"
412              << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
413                "save" : "restore") << " intrinsic.\n";
414     Warned = true;
415     if (Callee->getIntrinsicID() == Intrinsic::stacksave)
416       CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
417     break;
418   }
419     
420   case Intrinsic::returnaddress:
421   case Intrinsic::frameaddress:
422     errs() << "WARNING: this target does not support the llvm."
423            << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
424              "return" : "frame") << "address intrinsic.\n";
425     CI->replaceAllUsesWith(ConstantPointerNull::get(
426                                             cast<PointerType>(CI->getType())));
427     break;
428
429   case Intrinsic::prefetch:
430     break;    // Simply strip out prefetches on unsupported architectures
431
432   case Intrinsic::pcmarker:
433     break;    // Simply strip out pcmarker on unsupported architectures
434   case Intrinsic::readcyclecounter: {
435     errs() << "WARNING: this target does not support the llvm.readcyclecoun"
436            << "ter intrinsic.  It is being lowered to a constant 0\n";
437     CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0));
438     break;
439   }
440
441   case Intrinsic::dbg_declare:
442     break;    // Simply strip out debugging intrinsics
443
444   case Intrinsic::eh_exception:
445   case Intrinsic::eh_selector:
446     CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
447     break;
448
449   case Intrinsic::eh_typeid_for:
450     // Return something different to eh_selector.
451     CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
452     break;
453
454   case Intrinsic::var_annotation:
455     break;   // Strip out annotate intrinsic
456     
457   case Intrinsic::memcpy: {
458     const IntegerType *IntPtr = TD.getIntPtrType(Context);
459     Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
460                                         /* isSigned */ false);
461     Value *Ops[3];
462     Ops[0] = CI->getArgOperand(0);
463     Ops[1] = CI->getArgOperand(1);
464     Ops[2] = Size;
465     ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
466     break;
467   }
468   case Intrinsic::memmove: {
469     const IntegerType *IntPtr = TD.getIntPtrType(Context);
470     Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
471                                         /* isSigned */ false);
472     Value *Ops[3];
473     Ops[0] = CI->getArgOperand(0);
474     Ops[1] = CI->getArgOperand(1);
475     Ops[2] = Size;
476     ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
477     break;
478   }
479   case Intrinsic::memset: {
480     const IntegerType *IntPtr = TD.getIntPtrType(Context);
481     Value *Size = Builder.CreateIntCast(CI->getArgOperand(2), IntPtr,
482                                         /* isSigned */ false);
483     Value *Ops[3];
484     Ops[0] = CI->getArgOperand(0);
485     // Extend the amount to i32.
486     Ops[1] = Builder.CreateIntCast(CI->getArgOperand(1),
487                                    Type::getInt32Ty(Context),
488                                    /* isSigned */ false);
489     Ops[2] = Size;
490     ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getArgOperand(0)->getType());
491     break;
492   }
493   case Intrinsic::sqrt: {
494     ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl");
495     break;
496   }
497   case Intrinsic::log: {
498     ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl");
499     break;
500   }
501   case Intrinsic::log2: {
502     ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l");
503     break;
504   }
505   case Intrinsic::log10: {
506     ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l");
507     break;
508   }
509   case Intrinsic::exp: {
510     ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl");
511     break;
512   }
513   case Intrinsic::exp2: {
514     ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l");
515     break;
516   }
517   case Intrinsic::pow: {
518     ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl");
519     break;
520   }
521   case Intrinsic::flt_rounds:
522      // Lower to "round to the nearest"
523      if (!CI->getType()->isVoidTy())
524        CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
525      break;
526   case Intrinsic::invariant_start:
527   case Intrinsic::lifetime_start:
528     // Discard region information.
529     CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
530     break;
531   case Intrinsic::invariant_end:
532   case Intrinsic::lifetime_end:
533     // Discard region information.
534     break;
535   }
536
537   assert(CI->use_empty() &&
538          "Lowering should have eliminated any uses of the intrinsic call!");
539   CI->eraseFromParent();
540 }
541
542 bool IntrinsicLowering::LowerToByteSwap(CallInst *CI) {
543   // Verify this is a simple bswap.
544   if (CI->getNumArgOperands() != 1 ||
545       CI->getType() != CI->getArgOperand(0)->getType() ||
546       !CI->getType()->isIntegerTy())
547     return false;
548
549   const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
550   if (!Ty)
551     return false;
552
553   // Okay, we can do this xform, do so now.
554   const Type *Tys[] = { Ty };
555   Module *M = CI->getParent()->getParent()->getParent();
556   Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
557
558   Value *Op = CI->getArgOperand(0);
559   Op = CallInst::Create(Int, Op, CI->getName(), CI);
560
561   CI->replaceAllUsesWith(Op);
562   CI->eraseFromParent();
563   return true;
564 }