For PR1284:
[oota-llvm.git] / lib / CodeGen / IntrinsicLowering.cpp
1 //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
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/Instructions.h"
18 #include "llvm/Type.h"
19 #include "llvm/CodeGen/IntrinsicLowering.h"
20 #include "llvm/Support/Streams.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/ADT/SmallVector.h"
23 using namespace llvm;
24
25 template <class ArgIt>
26 static void EnsureFunctionExists(Module &M, const char *Name,
27                                  ArgIt ArgBegin, ArgIt ArgEnd,
28                                  const Type *RetTy) {
29   // Insert a correctly-typed definition now.
30   std::vector<const Type *> ParamTys;
31   for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
32     ParamTys.push_back(I->getType());
33   M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
34 }
35
36 /// ReplaceCallWith - This function is used when we want to lower an intrinsic
37 /// call to a call of an external function.  This handles hard cases such as
38 /// when there was already a prototype for the external function, and if that
39 /// prototype doesn't match the arguments we expect to pass in.
40 template <class ArgIt>
41 static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
42                                  ArgIt ArgBegin, ArgIt ArgEnd,
43                                  const Type *RetTy, Constant *&FCache) {
44   if (!FCache) {
45     // If we haven't already looked up this function, check to see if the
46     // program already contains a function with this name.
47     Module *M = CI->getParent()->getParent()->getParent();
48     // Get or insert the definition now.
49     std::vector<const Type *> ParamTys;
50     for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
51       ParamTys.push_back((*I)->getType());
52     FCache = M->getOrInsertFunction(NewFn,
53                                     FunctionType::get(RetTy, ParamTys, false));
54   }
55
56   SmallVector<Value*, 8> Operands(ArgBegin, ArgEnd);
57   CallInst *NewCI = new CallInst(FCache, &Operands[0], Operands.size(),
58                                  CI->getName(), CI);
59   if (!CI->use_empty())
60     CI->replaceAllUsesWith(NewCI);
61   return NewCI;
62 }
63
64 void IntrinsicLowering::AddPrototypes(Module &M) {
65   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
66     if (I->isDeclaration() && !I->use_empty())
67       switch (I->getIntrinsicID()) {
68       default: break;
69       case Intrinsic::setjmp:
70         EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(),
71                              Type::Int32Ty);
72         break;
73       case Intrinsic::longjmp:
74         EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(),
75                              Type::VoidTy);
76         break;
77       case Intrinsic::siglongjmp:
78         EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(),
79                              Type::VoidTy);
80         break;
81       case Intrinsic::memcpy_i32:
82       case Intrinsic::memcpy_i64:
83         M.getOrInsertFunction("memcpy", PointerType::get(Type::Int8Ty),
84                               PointerType::get(Type::Int8Ty), 
85                               PointerType::get(Type::Int8Ty), 
86                               TD.getIntPtrType(), (Type *)0);
87         break;
88       case Intrinsic::memmove_i32:
89       case Intrinsic::memmove_i64:
90         M.getOrInsertFunction("memmove", PointerType::get(Type::Int8Ty),
91                               PointerType::get(Type::Int8Ty), 
92                               PointerType::get(Type::Int8Ty), 
93                               TD.getIntPtrType(), (Type *)0);
94         break;
95       case Intrinsic::memset_i32:
96       case Intrinsic::memset_i64:
97         M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty),
98                               PointerType::get(Type::Int8Ty), Type::Int32Ty, 
99                               TD.getIntPtrType(), (Type *)0);
100         break;
101       case Intrinsic::sqrt_f32:
102       case Intrinsic::sqrt_f64:
103         if(I->arg_begin()->getType() == Type::FloatTy)
104           EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(),
105                                Type::FloatTy);
106         else
107           EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(),
108                                Type::DoubleTy);
109         break;
110       }
111 }
112
113 /// LowerBSWAP - Emit the code to lower bswap of V before the specified
114 /// instruction IP.
115 static Value *LowerBSWAP(Value *V, Instruction *IP) {
116   assert(V->getType()->isInteger() && "Can't bswap a non-integer type!");
117
118   unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
119   
120   switch(BitSize) {
121   default: assert(0 && "Unhandled type size of value to byteswap!");
122   case 16: {
123     Value *Tmp1 = BinaryOperator::createShl(V,
124                                 ConstantInt::get(V->getType(),8),"bswap.2",IP);
125     Value *Tmp2 = BinaryOperator::createLShr(V,
126                                 ConstantInt::get(V->getType(),8),"bswap.1",IP);
127     V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
128     break;
129   }
130   case 32: {
131     Value *Tmp4 = BinaryOperator::createShl(V,
132                               ConstantInt::get(V->getType(),24),"bswap.4", IP);
133     Value *Tmp3 = BinaryOperator::createShl(V,
134                               ConstantInt::get(V->getType(),8),"bswap.3",IP);
135     Value *Tmp2 = BinaryOperator::createLShr(V,
136                               ConstantInt::get(V->getType(),8),"bswap.2",IP);
137     Value *Tmp1 = BinaryOperator::createLShr(V,
138                               ConstantInt::get(V->getType(),24),"bswap.1", IP);
139     Tmp3 = BinaryOperator::createAnd(Tmp3, 
140                                      ConstantInt::get(Type::Int32Ty, 0xFF0000),
141                                      "bswap.and3", IP);
142     Tmp2 = BinaryOperator::createAnd(Tmp2, 
143                                      ConstantInt::get(Type::Int32Ty, 0xFF00),
144                                      "bswap.and2", IP);
145     Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
146     Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
147     V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP);
148     break;
149   }
150   case 64: {
151     Value *Tmp8 = BinaryOperator::createShl(V,
152                               ConstantInt::get(V->getType(),56),"bswap.8", IP);
153     Value *Tmp7 = BinaryOperator::createShl(V,
154                               ConstantInt::get(V->getType(),40),"bswap.7", IP);
155     Value *Tmp6 = BinaryOperator::createShl(V,
156                               ConstantInt::get(V->getType(),24),"bswap.6", IP);
157     Value *Tmp5 = BinaryOperator::createShl(V,
158                               ConstantInt::get(V->getType(),8),"bswap.5", IP);
159     Value* Tmp4 = BinaryOperator::createLShr(V,
160                               ConstantInt::get(V->getType(),8),"bswap.4", IP);
161     Value* Tmp3 = BinaryOperator::createLShr(V,
162                               ConstantInt::get(V->getType(),24),"bswap.3", IP);
163     Value* Tmp2 = BinaryOperator::createLShr(V,
164                               ConstantInt::get(V->getType(),40),"bswap.2", IP);
165     Value* Tmp1 = BinaryOperator::createLShr(V,
166                               ConstantInt::get(V->getType(),56),"bswap.1", IP);
167     Tmp7 = BinaryOperator::createAnd(Tmp7,
168                              ConstantInt::get(Type::Int64Ty, 
169                                0xFF000000000000ULL),
170                              "bswap.and7", IP);
171     Tmp6 = BinaryOperator::createAnd(Tmp6,
172                              ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
173                              "bswap.and6", IP);
174     Tmp5 = BinaryOperator::createAnd(Tmp5,
175                              ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
176                              "bswap.and5", IP);
177     Tmp4 = BinaryOperator::createAnd(Tmp4,
178                              ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
179                              "bswap.and4", IP);
180     Tmp3 = BinaryOperator::createAnd(Tmp3,
181                              ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
182                              "bswap.and3", IP);
183     Tmp2 = BinaryOperator::createAnd(Tmp2,
184                              ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
185                              "bswap.and2", IP);
186     Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
187     Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
188     Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
189     Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
190     Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
191     Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
192     V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
193     break;
194   }
195   }
196   return V;
197 }
198
199 /// LowerCTPOP - Emit the code to lower ctpop of V before the specified
200 /// instruction IP.
201 static Value *LowerCTPOP(Value *V, Instruction *IP) {
202   assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!");
203
204   static const uint64_t MaskValues[6] = {
205     0x5555555555555555ULL, 0x3333333333333333ULL,
206     0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
207     0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
208   };
209
210   unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
211
212   for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) {
213     Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
214     Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP);
215     Value *VShift = BinaryOperator::createLShr(V,
216                       ConstantInt::get(V->getType(), i), "ctpop.sh", IP);
217     Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
218     V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
219   }
220
221   return CastInst::createIntegerCast(V, Type::Int32Ty, false, "ctpop", IP);
222 }
223
224 /// LowerCTLZ - Emit the code to lower ctlz of V before the specified
225 /// instruction IP.
226 static Value *LowerCTLZ(Value *V, Instruction *IP) {
227
228   unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
229   for (unsigned i = 1; i != BitSize; i <<= 1) {
230     Value *ShVal = ConstantInt::get(V->getType(), i);
231     ShVal = BinaryOperator::createLShr(V, ShVal, "ctlz.sh", IP);
232     V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
233   }
234
235   V = BinaryOperator::createNot(V, "", IP);
236   return LowerCTPOP(V, IP);
237 }
238
239 /// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes 
240 /// three integer arguments. The first argument is the Value from which the
241 /// bits will be selected. It may be of any bit width. The second and third
242 /// arguments specify a range of bits to select with the second argument 
243 /// specifying the low bit and the third argument specifying the high bit. Both
244 /// must be type i32. The result is the corresponding selected bits from the
245 /// Value in the same width as the Value (first argument). If the low bit index
246 /// is higher than the high bit index then the inverse selection is done and 
247 /// the bits are returned in inverse order. 
248 /// @brief Lowering of llvm.part.select intrinsic.
249 static Instruction *LowerPartSelect(CallInst *CI) {
250   // Make sure we're dealing with a part select intrinsic here
251   Function *F = CI->getCalledFunction();
252   const FunctionType *FT = F->getFunctionType();
253   if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
254       FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
255       !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
256     return CI;
257
258   // Get the intrinsic implementation function by converting all the . to _
259   // in the intrinsic's function name and then reconstructing the function
260   // declaration.
261   std::string Name(F->getName());
262   for (unsigned i = 4; i < Name.length(); ++i)
263     if (Name[i] == '.')
264       Name[i] = '_';
265   Module* M = F->getParent();
266   F = cast<Function>(M->getOrInsertFunction(Name, FT));
267   F->setLinkage(GlobalValue::InternalLinkage);
268
269   // If we haven't defined the impl function yet, do so now
270   if (F->isDeclaration()) {
271
272     // Get the arguments to the function
273     Value* Val = F->getOperand(0);
274     Value* Right = F->getOperand(1);
275     Value* Left = F->getOperand(2);
276
277     // We want to select a range of bits here such that [Left, Right] is shifted
278     // down to the low bits. However, it is quite possible that Left is smaller
279     // than Right in which case the bits have to be reversed. 
280     
281     // Create the blocks we will need for the two cases (forward, reverse)
282     BasicBlock* CurBB   = new BasicBlock("entry", F);
283     BasicBlock *RevSize = new BasicBlock("revsize", CurBB->getParent());
284     BasicBlock *FwdSize = new BasicBlock("fwdsize", CurBB->getParent());
285     BasicBlock *Compute = new BasicBlock("compute", CurBB->getParent());
286     BasicBlock *Reverse = new BasicBlock("reverse", CurBB->getParent());
287     BasicBlock *RsltBlk = new BasicBlock("result",  CurBB->getParent());
288
289     // Cast Left and Right to the size of Val so the widths are all the same
290     if (Left->getType() != Val->getType())
291       Left = CastInst::createIntegerCast(Left, Val->getType(), false, 
292                                          "tmp", CurBB);
293     if (Right->getType() != Val->getType())
294       Right = CastInst::createIntegerCast(Right, Val->getType(), false, 
295                                           "tmp", CurBB);
296
297     // Compute a few things that both cases will need, up front.
298     Constant* Zero = ConstantInt::get(Val->getType(), 0);
299     Constant* One = ConstantInt::get(Val->getType(), 1);
300     Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
301
302     // Compare the Left and Right bit positions. This is used to determine 
303     // which case we have (forward or reverse)
304     ICmpInst *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, Left, Right, "less",CurBB);
305     new BranchInst(RevSize, FwdSize, Cmp, CurBB);
306
307     // First, copmute the number of bits in the forward case.
308     Instruction* FBitSize = 
309       BinaryOperator::createSub(Left, Right,"fbits", FwdSize);
310     new BranchInst(Compute, FwdSize);
311
312     // Second, compute the number of bits in the reverse case.
313     Instruction* RBitSize = 
314       BinaryOperator::createSub(Right, Left, "rbits", RevSize);
315     new BranchInst(Compute, RevSize);
316
317     // Now, compute the bit range. Start by getting the bitsize and the shift
318     // amount (either Left or Right) from PHI nodes. Then we compute a mask for 
319     // the number of bits we want in the range. We shift the bits down to the 
320     // least significant bits, apply the mask to zero out unwanted high bits, 
321     // and we have computed the "forward" result. It may still need to be 
322     // reversed.
323
324     // Get the BitSize from one of the two subtractions
325     PHINode *BitSize = new PHINode(Val->getType(), "bits", Compute);
326     BitSize->reserveOperandSpace(2);
327     BitSize->addIncoming(FBitSize, FwdSize);
328     BitSize->addIncoming(RBitSize, RevSize);
329
330     // Get the ShiftAmount as the smaller of Left/Right
331     PHINode *ShiftAmt = new PHINode(Val->getType(), "shiftamt", Compute);
332     ShiftAmt->reserveOperandSpace(2);
333     ShiftAmt->addIncoming(Right, FwdSize);
334     ShiftAmt->addIncoming(Left, RevSize);
335
336     // Increment the bit size
337     Instruction *BitSizePlusOne = 
338       BinaryOperator::createAdd(BitSize, One, "bits", Compute);
339
340     // Create a Mask to zero out the high order bits.
341     Instruction* Mask = 
342       BinaryOperator::createShl(AllOnes, BitSizePlusOne, "mask", Compute);
343     Mask = BinaryOperator::createNot(Mask, "mask", Compute);
344
345     // Shift the bits down and apply the mask
346     Instruction* FRes = 
347       BinaryOperator::createLShr(Val, ShiftAmt, "fres", Compute);
348     FRes = BinaryOperator::createAnd(FRes, Mask, "fres", Compute);
349     new BranchInst(Reverse, RsltBlk, Cmp, Compute);
350
351     // In the Reverse block we have the mask already in FRes but we must reverse
352     // it by shifting FRes bits right and putting them in RRes by shifting them 
353     // in from left.
354
355     // First set up our loop counters
356     PHINode *Count = new PHINode(Val->getType(), "count", Reverse);
357     Count->reserveOperandSpace(2);
358     Count->addIncoming(BitSizePlusOne, Compute);
359
360     // Next, get the value that we are shifting.
361     PHINode *BitsToShift   = new PHINode(Val->getType(), "val", Reverse);
362     BitsToShift->reserveOperandSpace(2);
363     BitsToShift->addIncoming(FRes, Compute);
364
365     // Finally, get the result of the last computation
366     PHINode *RRes  = new PHINode(Val->getType(), "rres", Reverse);
367     RRes->reserveOperandSpace(2);
368     RRes->addIncoming(Zero, Compute);
369
370     // Decrement the counter
371     Instruction *Decr = BinaryOperator::createSub(Count, One, "decr", Reverse);
372     Count->addIncoming(Decr, Reverse);
373
374     // Compute the Bit that we want to move
375     Instruction *Bit = 
376       BinaryOperator::createAnd(BitsToShift, One, "bit", Reverse);
377
378     // Compute the new value for next iteration.
379     Instruction *NewVal = 
380       BinaryOperator::createLShr(BitsToShift, One, "rshift", Reverse);
381     BitsToShift->addIncoming(NewVal, Reverse);
382
383     // Shift the bit into the low bits of the result.
384     Instruction *NewRes = 
385       BinaryOperator::createShl(RRes, One, "lshift", Reverse);
386     NewRes = BinaryOperator::createOr(NewRes, Bit, "addbit", Reverse);
387     RRes->addIncoming(NewRes, Reverse);
388     
389     // Terminate loop if we've moved all the bits.
390     ICmpInst *Cond = 
391       new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "cond", Reverse);
392     new BranchInst(RsltBlk, Reverse, Cond, Reverse);
393
394     // Finally, in the result block, select one of the two results with a PHI
395     // node and return the result;
396     CurBB = RsltBlk;
397     PHINode *BitSelect = new PHINode(Val->getType(), "part_select", CurBB);
398     BitSelect->reserveOperandSpace(2);
399     BitSelect->addIncoming(FRes, Compute);
400     BitSelect->addIncoming(NewRes, Reverse);
401     new ReturnInst(BitSelect, CurBB);
402   }
403
404   // Return a call to the implementation function
405   Value *Args[3];
406   Args[0] = CI->getOperand(0);
407   Args[1] = CI->getOperand(1);
408   Args[2] = CI->getOperand(2);
409   return new CallInst(F, Args, 3, CI->getName(), CI);
410 }
411
412 /// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes 
413 /// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High)
414 /// The first two arguments can be any bit width. The result is the same width
415 /// as %Value. The operation replaces bits between %Low and %High with the value
416 /// in %Replacement. If %Replacement is not the same width, it is truncated or
417 /// zero extended as appropriate to fit the bits being replaced. If %Low is
418 /// greater than %High then the inverse set of bits are replaced.
419 /// @brief Lowering of llvm.bit.part.set intrinsic.
420 static Instruction *LowerPartSet(CallInst *CI) {
421   // Make sure we're dealing with a part select intrinsic here
422   Function *F = CI->getCalledFunction();
423   const FunctionType *FT = F->getFunctionType();
424   if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
425       FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() ||
426       !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() ||
427       !FT->getParamType(3)->isInteger())
428     return CI;
429
430   // Get the intrinsic implementation function by converting all the . to _
431   // in the intrinsic's function name and then reconstructing the function
432   // declaration.
433   std::string Name(F->getName());
434   for (unsigned i = 4; i < Name.length(); ++i)
435     if (Name[i] == '.')
436       Name[i] = '_';
437   Module* M = F->getParent();
438   F = cast<Function>(M->getOrInsertFunction(Name, FT));
439   F->setLinkage(GlobalValue::InternalLinkage);
440
441   // If we haven't defined the impl function yet, do so now
442   if (F->isDeclaration()) {
443     // Note: the following code is based on code generated by llvm2cpp with 
444     // the following input. This is just *one* example of a generated function.
445     // The functions vary by bit width of result and first two arguments.
446     // The generated code has been changed to deal with any bit width not just
447     // the 32/64 bitwidths used in the above sample.
448     //
449     // define i64 @part_set(i64 %Val, i32 %Rep, i32 %Lo, i32 %Hi) {
450     // entry:
451     //   %is_forward = icmp ult i32 %Lo, %Hi
452     //   %Lo.pn = select i1 %is_forward, i32 %Hi, i32 %Lo
453     //   %Hi.pn = select i1 %is_forward, i32 %Lo, i32 %Hi
454     //   %iftmp.16.0 = sub i32 %Lo.pn, %Hi.pn
455     //   icmp ult i32 %iftmp.16.0, 32
456     //   br i1 %1, label %cond_true11, label %cond_next19
457     // cond_true11:
458     //   %tmp13 = sub i32 32, %iftmp.16.0
459     //   %tmp14 = lshr i32 -1, %tmp13
460     //   %tmp16 = and i32 %tmp14, %Rep
461     //   br label %cond_next19
462     // cond_next19:
463     //   %iftmp.17.0 = phi i32 [ %tmp16, %cond_true11 ], [ %Rep, %entry ]
464     //   %tmp2021 = zext i32 %iftmp.17.0 to i64
465     //   icmp ugt i32 %Lo, %Hi
466     //   br i1 %2, label %cond_next60, label %cond_true24
467     // cond_true24:
468     //   %tmp25.cast = zext i32 %Hi to i64
469     //   %tmp26 = lshr i64 -1, %tmp25.cast
470     //   %tmp27.cast = zext i32 %Lo to i64
471     //   %tmp28 = shl i64 %tmp26, %tmp27.cast
472     //   %tmp28not = xor i64 %tmp28, -1
473     //   %tmp31 = shl i64 %tmp2021, %tmp27.cast
474     //   %tmp34 = and i64 %tmp28not, %Val
475     //   %Val_addr.064 = or i64 %tmp31, %tmp34
476     //   ret i64 %Val_addr.064
477     // cond_next60:
478     //   %tmp39.cast = zext i32 %Lo to i64
479     //   %tmp40 = shl i64 -1, %tmp39.cast
480     //   %tmp41.cast = zext i32 %Hi to i64
481     //   %tmp42 = shl i64 -1, %tmp41.cast
482     //   %tmp45.demorgan = or i64 %tmp42, %tmp40
483     //   %tmp45 = xor i64 %tmp45.demorgan, -1
484     //   %tmp47 = and i64 %tmp45, %Val
485     //   %tmp50 = shl i64 %tmp2021, %tmp39.cast
486     //   %tmp52 = sub i32 32, %Hi
487     //   %tmp52.cast = zext i32 %tmp52 to i64
488     //   %tmp54 = lshr i64 %tmp2021, %tmp52.cast
489     //   %tmp57 = or i64 %tmp50, %tmp47
490     //   %Val_addr.0 = or i64 %tmp57, %tmp54
491     //   ret i64 %Val_addr.0
492     // }
493
494     // Get the arguments for the function.
495     Function::arg_iterator args = F->arg_begin();
496     Value* Val = args++; Val->setName("Val");
497     Value* Rep = args++; Rep->setName("Rep");
498     Value* Lo  = args++; Lo->setName("Lo");
499     Value* Hi  = args++; Hi->setName("Hi");
500
501     // Get some types we need
502     const IntegerType* ValTy = cast<IntegerType>(Val->getType());
503     const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
504     uint32_t ValBits = ValTy->getBitWidth();
505     uint32_t RepBits = RepTy->getBitWidth();
506
507     // Constant Definitions
508     ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
509     ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
510     ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
511
512     BasicBlock* entry = new BasicBlock("entry",F,0);
513     BasicBlock* large = new BasicBlock("large",F,0);
514     BasicBlock* small = new BasicBlock("small",F,0);
515     BasicBlock* forward = new BasicBlock("cond_true24",F,0);
516     BasicBlock* reverse = new BasicBlock("cond_next60",F,0);
517
518     // Block entry (entry)
519     // First, convert Lo and Hi to ValTy bit width
520     if (ValBits > 32) {
521       Hi = new ZExtInst(Hi, ValTy, "", entry);
522       Lo = new ZExtInst(Lo, ValTy, "", entry);
523     } else if (ValBits < 32) {
524       Hi = new TruncInst(Hi, ValTy, "", entry);
525       Lo = new TruncInst(Lo, ValTy, "", entry);
526     }
527     ICmpInst* is_forward = 
528       new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry);
529     SelectInst* Lo_pn = new SelectInst(is_forward, Hi, Lo, "", entry);
530     SelectInst* Hi_pn = new SelectInst(is_forward, Lo, Hi, "", entry);
531     BinaryOperator* NumBits = BinaryOperator::createSub(Lo_pn, Hi_pn, "",entry);
532     ICmpInst* is_large = 
533       new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry);
534     new BranchInst(large, small, is_large, entry);
535
536     // Block "large"
537     BinaryOperator* MaskBits = 
538       BinaryOperator::createSub(RepBitWidth, NumBits, "", large);
539     BinaryOperator* Mask1 = 
540       BinaryOperator::createLShr(RepMask, MaskBits, "", large);
541     BinaryOperator* Rep2 = BinaryOperator::createAnd(Mask1, Rep, "", large);
542     new BranchInst(small, large);
543
544     // Block "small"
545     PHINode* Rep3 = new PHINode(RepTy, "", small);
546     Rep3->reserveOperandSpace(2);
547     Rep3->addIncoming(Rep2, small);
548     Rep3->addIncoming(Rep, entry);
549     CastInst* Rep4 = new ZExtInst(Rep3, ValTy, "", small);
550     ICmpInst* is_reverse = 
551       new ICmpInst(ICmpInst::ICMP_UGT, Lo, Hi, "", small);
552     new BranchInst(reverse, forward, is_reverse, small);
553
554     // Block "forward"
555     Value* t1    = BinaryOperator::createLShr(ValMask, Hi, "", forward);
556     Value* t2    = BinaryOperator::createShl(t1, Lo, "", forward);
557     Value* nott2 = BinaryOperator::createXor(t2, ValMask, "", forward);
558     Value* t3    = BinaryOperator::createShl(Rep4, Lo, "", forward);
559     Value* t4    = BinaryOperator::createAnd(nott2, Val, "", forward);
560     Value* FRslt = BinaryOperator::createOr(t3, t4, "", forward);
561     new ReturnInst(FRslt, forward);
562
563     // Block "reverse"
564     Value* t5    = BinaryOperator::createShl(ValMask, Lo, "", reverse);
565     Value* t6    = BinaryOperator::createShl(ValMask, Hi, "", reverse);
566     Value* t7    = BinaryOperator::createOr(t6, t5, "", reverse);
567     Value* t8    = BinaryOperator::createXor(t7, ValMask, "", reverse);
568     Value* t9    = BinaryOperator::createAnd(t8, Val, "", reverse);
569     Value* t10   = BinaryOperator::createShl(Rep4, Lo, "", reverse);
570     Value* t11   = BinaryOperator::createSub(RepBitWidth, Hi, "", reverse);
571     Value* t12   = new ZExtInst(t11, ValTy, "", reverse);
572     Value* t13   = BinaryOperator::createLShr(Rep4, t12, "",reverse);
573     Value* t14   = BinaryOperator::createOr(t10, t9, "", reverse);
574     Value* RRslt = BinaryOperator::createOr(t14, t13, "", reverse);
575     new ReturnInst(RRslt, reverse);
576   }
577
578   // Return a call to the implementation function
579   Value *Args[3];
580   Args[0] = CI->getOperand(0);
581   Args[1] = CI->getOperand(1);
582   Args[2] = CI->getOperand(2);
583   Args[3] = CI->getOperand(3);
584   return new CallInst(F, Args, 4, CI->getName(), CI);
585 }
586
587
588 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
589   Function *Callee = CI->getCalledFunction();
590   assert(Callee && "Cannot lower an indirect call!");
591
592   switch (Callee->getIntrinsicID()) {
593   case Intrinsic::not_intrinsic:
594     cerr << "Cannot lower a call to a non-intrinsic function '"
595          << Callee->getName() << "'!\n";
596     abort();
597   default:
598     cerr << "Error: Code generator does not support intrinsic function '"
599          << Callee->getName() << "'!\n";
600     abort();
601
602     // The setjmp/longjmp intrinsics should only exist in the code if it was
603     // never optimized (ie, right out of the CFE), or if it has been hacked on
604     // by the lowerinvoke pass.  In both cases, the right thing to do is to
605     // convert the call to an explicit setjmp or longjmp call.
606   case Intrinsic::setjmp: {
607     static Constant *SetjmpFCache = 0;
608     Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
609                                Type::Int32Ty, SetjmpFCache);
610     if (CI->getType() != Type::VoidTy)
611       CI->replaceAllUsesWith(V);
612     break;
613   }
614   case Intrinsic::sigsetjmp:
615      if (CI->getType() != Type::VoidTy)
616        CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
617      break;
618
619   case Intrinsic::longjmp: {
620     static Constant *LongjmpFCache = 0;
621     ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
622                     Type::VoidTy, LongjmpFCache);
623     break;
624   }
625
626   case Intrinsic::siglongjmp: {
627     // Insert the call to abort
628     static Constant *AbortFCache = 0;
629     ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), 
630                     Type::VoidTy, AbortFCache);
631     break;
632   }
633   case Intrinsic::ctpop:
634     CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
635     break;
636
637   case Intrinsic::bswap:
638     CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
639     break;
640     
641   case Intrinsic::ctlz:
642     CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
643     break;
644
645   case Intrinsic::cttz: {
646     // cttz(x) -> ctpop(~X & (X-1))
647     Value *Src = CI->getOperand(1);
648     Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
649     Value *SrcM1  = ConstantInt::get(Src->getType(), 1);
650     SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
651     Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
652     CI->replaceAllUsesWith(Src);
653     break;
654   }
655
656   case Intrinsic::part_select:
657     CI->replaceAllUsesWith(LowerPartSelect(CI));
658     break;
659
660   case Intrinsic::part_set:
661     CI->replaceAllUsesWith(LowerPartSet(CI));
662     break;
663
664   case Intrinsic::stacksave:
665   case Intrinsic::stackrestore: {
666     static bool Warned = false;
667     if (!Warned)
668       cerr << "WARNING: this target does not support the llvm.stack"
669            << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
670                "save" : "restore") << " intrinsic.\n";
671     Warned = true;
672     if (Callee->getIntrinsicID() == Intrinsic::stacksave)
673       CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
674     break;
675   }
676     
677   case Intrinsic::returnaddress:
678   case Intrinsic::frameaddress:
679     cerr << "WARNING: this target does not support the llvm."
680          << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
681              "return" : "frame") << "address intrinsic.\n";
682     CI->replaceAllUsesWith(ConstantPointerNull::get(
683                                             cast<PointerType>(CI->getType())));
684     break;
685
686   case Intrinsic::prefetch:
687     break;    // Simply strip out prefetches on unsupported architectures
688
689   case Intrinsic::pcmarker:
690     break;    // Simply strip out pcmarker on unsupported architectures
691   case Intrinsic::readcyclecounter: {
692     cerr << "WARNING: this target does not support the llvm.readcyclecoun"
693          << "ter intrinsic.  It is being lowered to a constant 0\n";
694     CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
695     break;
696   }
697
698   case Intrinsic::dbg_stoppoint:
699   case Intrinsic::dbg_region_start:
700   case Intrinsic::dbg_region_end:
701   case Intrinsic::dbg_func_start:
702   case Intrinsic::dbg_declare:
703   case Intrinsic::eh_exception:
704   case Intrinsic::eh_selector:
705   case Intrinsic::eh_filter:
706     break;    // Simply strip out debugging and eh intrinsics
707
708   case Intrinsic::memcpy_i32:
709   case Intrinsic::memcpy_i64: {
710     static Constant *MemcpyFCache = 0;
711     Value *Size = CI->getOperand(3);
712     const Type *IntPtr = TD.getIntPtrType();
713     if (Size->getType()->getPrimitiveSizeInBits() <
714         IntPtr->getPrimitiveSizeInBits())
715       Size = new ZExtInst(Size, IntPtr, "", CI);
716     else if (Size->getType()->getPrimitiveSizeInBits() >
717              IntPtr->getPrimitiveSizeInBits())
718       Size = new TruncInst(Size, IntPtr, "", CI);
719     Value *Ops[3];
720     Ops[0] = CI->getOperand(1);
721     Ops[1] = CI->getOperand(2);
722     Ops[2] = Size;
723     ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
724                     MemcpyFCache);
725     break;
726   }
727   case Intrinsic::memmove_i32: 
728   case Intrinsic::memmove_i64: {
729     static Constant *MemmoveFCache = 0;
730     Value *Size = CI->getOperand(3);
731     const Type *IntPtr = TD.getIntPtrType();
732     if (Size->getType()->getPrimitiveSizeInBits() <
733         IntPtr->getPrimitiveSizeInBits())
734       Size = new ZExtInst(Size, IntPtr, "", CI);
735     else if (Size->getType()->getPrimitiveSizeInBits() >
736              IntPtr->getPrimitiveSizeInBits())
737       Size = new TruncInst(Size, IntPtr, "", CI);
738     Value *Ops[3];
739     Ops[0] = CI->getOperand(1);
740     Ops[1] = CI->getOperand(2);
741     Ops[2] = Size;
742     ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
743                     MemmoveFCache);
744     break;
745   }
746   case Intrinsic::memset_i32:
747   case Intrinsic::memset_i64: {
748     static Constant *MemsetFCache = 0;
749     Value *Size = CI->getOperand(3);
750     const Type *IntPtr = TD.getIntPtrType();
751     if (Size->getType()->getPrimitiveSizeInBits() <
752         IntPtr->getPrimitiveSizeInBits())
753       Size = new ZExtInst(Size, IntPtr, "", CI);
754     else if (Size->getType()->getPrimitiveSizeInBits() >
755              IntPtr->getPrimitiveSizeInBits())
756       Size = new TruncInst(Size, IntPtr, "", CI);
757     Value *Ops[3];
758     Ops[0] = CI->getOperand(1);
759     // Extend the amount to i32.
760     Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI);
761     Ops[2] = Size;
762     ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
763                     MemsetFCache);
764     break;
765   }
766   case Intrinsic::sqrt_f32: {
767     static Constant *sqrtfFCache = 0;
768     ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
769                     Type::FloatTy, sqrtfFCache);
770     break;
771   }
772   case Intrinsic::sqrt_f64: {
773     static Constant *sqrtFCache = 0;
774     ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
775                     Type::DoubleTy, sqrtFCache);
776     break;
777   }
778   }
779
780   assert(CI->use_empty() &&
781          "Lowering should have eliminated any uses of the intrinsic call!");
782   CI->eraseFromParent();
783 }