apparently some people commit without building the tree, or they forget to
[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.bit.part_select.iX.iY.iZ intrinsic. This intrinsic takes 
240 /// three integer operands of arbitrary bit width. The first operand is the 
241 /// value from which to select the bits. The second and third operands define a 
242 /// range of bits to select.  The result is the bits selected and has a 
243 /// corresponding width of Left-Right (second operand - third operand).
244 /// @see IEEE 1666-2005, System C, Section 7.2.6, pg 175. 
245 /// @brief Lowering of llvm.bit.part_select intrinsic.
246 static Instruction *LowerBitPartSelect(CallInst *CI) {
247   // Make sure we're dealing with a part select intrinsic here
248   Function *F = CI->getCalledFunction();
249   const FunctionType *FT = F->getFunctionType();
250   if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
251       FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
252       !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
253     return CI;
254
255   // Get the intrinsic implementation function by converting all the . to _
256   // in the intrinsic's function name and then reconstructing the function
257   // declaration.
258   std::string Name(F->getName());
259   for (unsigned i = 4; i < Name.length(); ++i)
260     if (Name[i] == '.')
261       Name[i] = '_';
262   Module* M = F->getParent();
263   F = cast<Function>(M->getOrInsertFunction(Name, FT));
264   F->setLinkage(GlobalValue::InternalLinkage);
265
266   // If we haven't defined the impl function yet, do so now
267   if (F->isDeclaration()) {
268
269     // Get the arguments to the function
270     Value* Val = F->getOperand(0);
271     Value* Left = F->getOperand(1);
272     Value* Right = F->getOperand(2);
273
274     // We want to select a range of bits here such that [Left, Right] is shifted
275     // down to the low bits. However, it is quite possible that Left is smaller
276     // than Right in which case the bits have to be reversed. 
277     
278     // Create the blocks we will need for the two cases (forward, reverse)
279     BasicBlock* CurBB   = new BasicBlock("entry", F);
280     BasicBlock *RevSize = new BasicBlock("revsize", CurBB->getParent());
281     BasicBlock *FwdSize = new BasicBlock("fwdsize", CurBB->getParent());
282     BasicBlock *Compute = new BasicBlock("compute", CurBB->getParent());
283     BasicBlock *Reverse = new BasicBlock("reverse", CurBB->getParent());
284     BasicBlock *RsltBlk = new BasicBlock("result",  CurBB->getParent());
285
286     // Cast Left and Right to the size of Val so the widths are all the same
287     if (Left->getType() != Val->getType())
288       Left = CastInst::createIntegerCast(Left, Val->getType(), false, 
289                                          "tmp", CurBB);
290     if (Right->getType() != Val->getType())
291       Right = CastInst::createIntegerCast(Right, Val->getType(), false, 
292                                           "tmp", CurBB);
293
294     // Compute a few things that both cases will need, up front.
295     Constant* Zero = ConstantInt::get(Val->getType(), 0);
296     Constant* One = ConstantInt::get(Val->getType(), 1);
297     Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
298
299     // Compare the Left and Right bit positions. This is used to determine 
300     // which case we have (forward or reverse)
301     ICmpInst *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, Left, Right, "less",CurBB);
302     new BranchInst(RevSize, FwdSize, Cmp, CurBB);
303
304     // First, copmute the number of bits in the forward case.
305     Instruction* FBitSize = 
306       BinaryOperator::createSub(Left, Right,"fbits", FwdSize);
307     new BranchInst(Compute, FwdSize);
308
309     // Second, compute the number of bits in the reverse case.
310     Instruction* RBitSize = 
311       BinaryOperator::createSub(Right, Left, "rbits", RevSize);
312     new BranchInst(Compute, RevSize);
313
314     // Now, compute the bit range. Start by getting the bitsize and the shift
315     // amount (either Left or Right) from PHI nodes. Then we compute a mask for 
316     // the number of bits we want in the range. We shift the bits down to the 
317     // least significant bits, apply the mask to zero out unwanted high bits, 
318     // and we have computed the "forward" result. It may still need to be 
319     // reversed.
320
321     // Get the BitSize from one of the two subtractions
322     PHINode *BitSize = new PHINode(Val->getType(), "bits", Compute);
323     BitSize->reserveOperandSpace(2);
324     BitSize->addIncoming(FBitSize, FwdSize);
325     BitSize->addIncoming(RBitSize, RevSize);
326
327     // Get the ShiftAmount as the smaller of Left/Right
328     PHINode *ShiftAmt = new PHINode(Val->getType(), "shiftamt", Compute);
329     ShiftAmt->reserveOperandSpace(2);
330     ShiftAmt->addIncoming(Right, FwdSize);
331     ShiftAmt->addIncoming(Left, RevSize);
332
333     // Increment the bit size
334     Instruction *BitSizePlusOne = 
335       BinaryOperator::createAdd(BitSize, One, "bits", Compute);
336
337     // Create a Mask to zero out the high order bits.
338     Instruction* Mask = 
339       BinaryOperator::createShl(AllOnes, BitSizePlusOne, "mask", Compute);
340     Mask = BinaryOperator::createNot(Mask, "mask", Compute);
341
342     // Shift the bits down and apply the mask
343     Instruction* FRes = 
344       BinaryOperator::createLShr(Val, ShiftAmt, "fres", Compute);
345     FRes = BinaryOperator::createAnd(FRes, Mask, "fres", Compute);
346     new BranchInst(Reverse, RsltBlk, Cmp, Compute);
347
348     // In the Reverse block we have the mask already in FRes but we must reverse
349     // it by shifting FRes bits right and putting them in RRes by shifting them 
350     // in from left.
351
352     // First set up our loop counters
353     PHINode *Count = new PHINode(Val->getType(), "count", Reverse);
354     Count->reserveOperandSpace(2);
355     Count->addIncoming(BitSizePlusOne, Compute);
356
357     // Next, get the value that we are shifting.
358     PHINode *BitsToShift   = new PHINode(Val->getType(), "val", Reverse);
359     BitsToShift->reserveOperandSpace(2);
360     BitsToShift->addIncoming(FRes, Compute);
361
362     // Finally, get the result of the last computation
363     PHINode *RRes  = new PHINode(Val->getType(), "rres", Reverse);
364     RRes->reserveOperandSpace(2);
365     RRes->addIncoming(Zero, Compute);
366
367     // Decrement the counter
368     Instruction *Decr = BinaryOperator::createSub(Count, One, "decr", Reverse);
369     Count->addIncoming(Decr, Reverse);
370
371     // Compute the Bit that we want to move
372     Instruction *Bit = 
373       BinaryOperator::createAnd(BitsToShift, One, "bit", Reverse);
374
375     // Compute the new value for next iteration.
376     Instruction *NewVal = 
377       BinaryOperator::createLShr(BitsToShift, One, "rshift", Reverse);
378     BitsToShift->addIncoming(NewVal, Reverse);
379
380     // Shift the bit into the low bits of the result.
381     Instruction *NewRes = 
382       BinaryOperator::createShl(RRes, One, "lshift", Reverse);
383     NewRes = BinaryOperator::createOr(NewRes, Bit, "addbit", Reverse);
384     RRes->addIncoming(NewRes, Reverse);
385     
386     // Terminate loop if we've moved all the bits.
387     ICmpInst *Cond = 
388       new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "cond", Reverse);
389     new BranchInst(RsltBlk, Reverse, Cond, Reverse);
390
391     // Finally, in the result block, select one of the two results with a PHI
392     // node and return the result;
393     CurBB = RsltBlk;
394     PHINode *BitSelect = new PHINode(Val->getType(), "part_select", CurBB);
395     BitSelect->reserveOperandSpace(2);
396     BitSelect->addIncoming(FRes, Compute);
397     BitSelect->addIncoming(NewRes, Reverse);
398     new ReturnInst(BitSelect, CurBB);
399   }
400
401   // Return a call to the implementation function
402   Value *Args[3];
403   Args[0] = CI->getOperand(0);
404   Args[1] = CI->getOperand(1);
405   Args[2] = CI->getOperand(2);
406   return new CallInst(F, Args, 3, CI->getName(), CI);
407 }
408
409
410 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
411   Function *Callee = CI->getCalledFunction();
412   assert(Callee && "Cannot lower an indirect call!");
413
414   switch (Callee->getIntrinsicID()) {
415   case Intrinsic::not_intrinsic:
416     cerr << "Cannot lower a call to a non-intrinsic function '"
417          << Callee->getName() << "'!\n";
418     abort();
419   default:
420     cerr << "Error: Code generator does not support intrinsic function '"
421          << Callee->getName() << "'!\n";
422     abort();
423
424     // The setjmp/longjmp intrinsics should only exist in the code if it was
425     // never optimized (ie, right out of the CFE), or if it has been hacked on
426     // by the lowerinvoke pass.  In both cases, the right thing to do is to
427     // convert the call to an explicit setjmp or longjmp call.
428   case Intrinsic::setjmp: {
429     static Constant *SetjmpFCache = 0;
430     Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
431                                Type::Int32Ty, SetjmpFCache);
432     if (CI->getType() != Type::VoidTy)
433       CI->replaceAllUsesWith(V);
434     break;
435   }
436   case Intrinsic::sigsetjmp:
437      if (CI->getType() != Type::VoidTy)
438        CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
439      break;
440
441   case Intrinsic::longjmp: {
442     static Constant *LongjmpFCache = 0;
443     ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
444                     Type::VoidTy, LongjmpFCache);
445     break;
446   }
447
448   case Intrinsic::siglongjmp: {
449     // Insert the call to abort
450     static Constant *AbortFCache = 0;
451     ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), 
452                     Type::VoidTy, AbortFCache);
453     break;
454   }
455   case Intrinsic::ctpop:
456     CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
457     break;
458
459   case Intrinsic::bswap:
460     CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
461     break;
462     
463   case Intrinsic::ctlz:
464     CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
465     break;
466
467   case Intrinsic::cttz: {
468     // cttz(x) -> ctpop(~X & (X-1))
469     Value *Src = CI->getOperand(1);
470     Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
471     Value *SrcM1  = ConstantInt::get(Src->getType(), 1);
472     SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
473     Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
474     CI->replaceAllUsesWith(Src);
475     break;
476   }
477
478   case Intrinsic::part_select:
479     CI->replaceAllUsesWith(LowerBitPartSelect(CI));
480     break;
481
482   case Intrinsic::stacksave:
483   case Intrinsic::stackrestore: {
484     static bool Warned = false;
485     if (!Warned)
486       cerr << "WARNING: this target does not support the llvm.stack"
487            << (Callee->getIntrinsicID() == Intrinsic::stacksave ?
488                "save" : "restore") << " intrinsic.\n";
489     Warned = true;
490     if (Callee->getIntrinsicID() == Intrinsic::stacksave)
491       CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
492     break;
493   }
494     
495   case Intrinsic::returnaddress:
496   case Intrinsic::frameaddress:
497     cerr << "WARNING: this target does not support the llvm."
498          << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
499              "return" : "frame") << "address intrinsic.\n";
500     CI->replaceAllUsesWith(ConstantPointerNull::get(
501                                             cast<PointerType>(CI->getType())));
502     break;
503
504   case Intrinsic::prefetch:
505     break;    // Simply strip out prefetches on unsupported architectures
506
507   case Intrinsic::pcmarker:
508     break;    // Simply strip out pcmarker on unsupported architectures
509   case Intrinsic::readcyclecounter: {
510     cerr << "WARNING: this target does not support the llvm.readcyclecoun"
511          << "ter intrinsic.  It is being lowered to a constant 0\n";
512     CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
513     break;
514   }
515
516   case Intrinsic::dbg_stoppoint:
517   case Intrinsic::dbg_region_start:
518   case Intrinsic::dbg_region_end:
519   case Intrinsic::dbg_func_start:
520   case Intrinsic::dbg_declare:
521   case Intrinsic::eh_exception:
522   case Intrinsic::eh_selector:
523   case Intrinsic::eh_filter:
524     break;    // Simply strip out debugging and eh intrinsics
525
526   case Intrinsic::memcpy_i32:
527   case Intrinsic::memcpy_i64: {
528     static Constant *MemcpyFCache = 0;
529     Value *Size = CI->getOperand(3);
530     const Type *IntPtr = TD.getIntPtrType();
531     if (Size->getType()->getPrimitiveSizeInBits() <
532         IntPtr->getPrimitiveSizeInBits())
533       Size = new ZExtInst(Size, IntPtr, "", CI);
534     else if (Size->getType()->getPrimitiveSizeInBits() >
535              IntPtr->getPrimitiveSizeInBits())
536       Size = new TruncInst(Size, IntPtr, "", CI);
537     Value *Ops[3];
538     Ops[0] = CI->getOperand(1);
539     Ops[1] = CI->getOperand(2);
540     Ops[2] = Size;
541     ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
542                     MemcpyFCache);
543     break;
544   }
545   case Intrinsic::memmove_i32: 
546   case Intrinsic::memmove_i64: {
547     static Constant *MemmoveFCache = 0;
548     Value *Size = CI->getOperand(3);
549     const Type *IntPtr = TD.getIntPtrType();
550     if (Size->getType()->getPrimitiveSizeInBits() <
551         IntPtr->getPrimitiveSizeInBits())
552       Size = new ZExtInst(Size, IntPtr, "", CI);
553     else if (Size->getType()->getPrimitiveSizeInBits() >
554              IntPtr->getPrimitiveSizeInBits())
555       Size = new TruncInst(Size, IntPtr, "", CI);
556     Value *Ops[3];
557     Ops[0] = CI->getOperand(1);
558     Ops[1] = CI->getOperand(2);
559     Ops[2] = Size;
560     ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
561                     MemmoveFCache);
562     break;
563   }
564   case Intrinsic::memset_i32:
565   case Intrinsic::memset_i64: {
566     static Constant *MemsetFCache = 0;
567     Value *Size = CI->getOperand(3);
568     const Type *IntPtr = TD.getIntPtrType();
569     if (Size->getType()->getPrimitiveSizeInBits() <
570         IntPtr->getPrimitiveSizeInBits())
571       Size = new ZExtInst(Size, IntPtr, "", CI);
572     else if (Size->getType()->getPrimitiveSizeInBits() >
573              IntPtr->getPrimitiveSizeInBits())
574       Size = new TruncInst(Size, IntPtr, "", CI);
575     Value *Ops[3];
576     Ops[0] = CI->getOperand(1);
577     // Extend the amount to i32.
578     Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI);
579     Ops[2] = Size;
580     ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(),
581                     MemsetFCache);
582     break;
583   }
584   case Intrinsic::sqrt_f32: {
585     static Constant *sqrtfFCache = 0;
586     ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(),
587                     Type::FloatTy, sqrtfFCache);
588     break;
589   }
590   case Intrinsic::sqrt_f64: {
591     static Constant *sqrtFCache = 0;
592     ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(),
593                     Type::DoubleTy, sqrtFCache);
594     break;
595   }
596   }
597
598   assert(CI->use_empty() &&
599          "Lowering should have eliminated any uses of the intrinsic call!");
600   CI->eraseFromParent();
601 }