3294865a10ad84dac6ea337a58660677bfe5ae90
[oota-llvm.git] / lib / Analysis / ConstantFolding.cpp
1 //===-- ConstantFolding.cpp - Fold instructions into constants ------------===//
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 defines routines for folding instructions into constants.
11 //
12 // Also, to supplement the basic VMCore ConstantExpr simplifications,
13 // this file defines some additional folding routines that can make use of
14 // TargetData information. These functions cannot go in VMCore due to library
15 // dependency issues.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Analysis/ConstantFolding.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Intrinsics.h"
26 #include "llvm/LLVMContext.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/StringMap.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/GetElementPtrTypeIterator.h"
33 #include "llvm/Support/MathExtras.h"
34 #include <cerrno>
35 #include <cmath>
36 using namespace llvm;
37
38 //===----------------------------------------------------------------------===//
39 // Constant Folding internal helper functions
40 //===----------------------------------------------------------------------===//
41
42 /// IsConstantOffsetFromGlobal - If this constant is actually a constant offset
43 /// from a global, return the global and the constant.  Because of
44 /// constantexprs, this function is recursive.
45 static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
46                                        int64_t &Offset, const TargetData &TD) {
47   // Trivial case, constant is the global.
48   if ((GV = dyn_cast<GlobalValue>(C))) {
49     Offset = 0;
50     return true;
51   }
52   
53   // Otherwise, if this isn't a constant expr, bail out.
54   ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
55   if (!CE) return false;
56   
57   // Look through ptr->int and ptr->ptr casts.
58   if (CE->getOpcode() == Instruction::PtrToInt ||
59       CE->getOpcode() == Instruction::BitCast)
60     return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD);
61   
62   // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)    
63   if (CE->getOpcode() == Instruction::GetElementPtr) {
64     // Cannot compute this if the element type of the pointer is missing size
65     // info.
66     if (!cast<PointerType>(CE->getOperand(0)->getType())
67                  ->getElementType()->isSized())
68       return false;
69     
70     // If the base isn't a global+constant, we aren't either.
71     if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD))
72       return false;
73     
74     // Otherwise, add any offset that our operands provide.
75     gep_type_iterator GTI = gep_type_begin(CE);
76     for (User::const_op_iterator i = CE->op_begin() + 1, e = CE->op_end();
77          i != e; ++i, ++GTI) {
78       ConstantInt *CI = dyn_cast<ConstantInt>(*i);
79       if (!CI) return false;  // Index isn't a simple constant?
80       if (CI->getZExtValue() == 0) continue;  // Not adding anything.
81       
82       if (const StructType *ST = dyn_cast<StructType>(*GTI)) {
83         // N = N + Offset
84         Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue());
85       } else {
86         const SequentialType *SQT = cast<SequentialType>(*GTI);
87         Offset += TD.getTypeAllocSize(SQT->getElementType())*CI->getSExtValue();
88       }
89     }
90     return true;
91   }
92   
93   return false;
94 }
95
96 /// ReadDataFromGlobal - Recursive helper to read bits out of global.  C is the
97 /// constant being copied out of. ByteOffset is an offset into C.  CurPtr is the
98 /// pointer to copy results into and BytesLeft is the number of bytes left in
99 /// the CurPtr buffer.  TD is the target data.
100 static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset,
101                                unsigned char *CurPtr, unsigned BytesLeft,
102                                const TargetData &TD) {
103   assert(ByteOffset <= TD.getTypeAllocSize(C->getType()) &&
104          "Out of range access");
105   
106   if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
107     return true;
108   
109   if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
110     if (CI->getBitWidth() > 64 ||
111         (CI->getBitWidth() & 7) != 0)
112       return false;
113     
114     uint64_t Val = CI->getZExtValue();
115     unsigned IntBytes = unsigned(CI->getBitWidth()/8);
116     
117     for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
118       CurPtr[i] = (unsigned char)(Val >> ByteOffset * 8);
119       ++ByteOffset;
120     }
121     return true;
122   }
123   
124   if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
125     if (CFP->getType()->isDoubleTy()) {
126       C = ConstantExpr::getBitCast(C, Type::getInt64Ty(C->getContext()));
127       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
128     }
129     if (CFP->getType()->isFloatTy()){
130       C = ConstantExpr::getBitCast(C, Type::getInt32Ty(C->getContext()));
131       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
132     }
133   }
134
135   if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
136     const StructLayout *SL = TD.getStructLayout(CS->getType());
137     unsigned Index = SL->getElementContainingOffset(ByteOffset);
138     uint64_t CurEltOffset = SL->getElementOffset(Index);
139     ByteOffset -= CurEltOffset;
140     
141     while (1) {
142       // If the element access is to the element itself and not to tail padding,
143       // read the bytes from the element.
144       uint64_t EltSize = TD.getTypeAllocSize(CS->getOperand(Index)->getType());
145
146       if (ByteOffset < EltSize &&
147           !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
148                               BytesLeft, TD))
149         return false;
150       
151       ++Index;
152       
153       // Check to see if we read from the last struct element, if so we're done.
154       if (Index == CS->getType()->getNumElements())
155         return true;
156
157       // If we read all of the bytes we needed from this element we're done.
158       uint64_t NextEltOffset = SL->getElementOffset(Index);
159
160       if (BytesLeft <= NextEltOffset-CurEltOffset-ByteOffset)
161         return true;
162
163       // Move to the next element of the struct.
164       BytesLeft -= NextEltOffset-CurEltOffset-ByteOffset;
165       ByteOffset = 0;
166       CurEltOffset = NextEltOffset;
167     }
168     // not reached.
169   }
170
171   if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
172     uint64_t EltSize = TD.getTypeAllocSize(CA->getType()->getElementType());
173     uint64_t Index = ByteOffset / EltSize;
174     uint64_t Offset = ByteOffset - Index * EltSize;
175     for (; Index != CA->getType()->getNumElements(); ++Index) {
176       if (!ReadDataFromGlobal(CA->getOperand(Index), Offset, CurPtr,
177                               BytesLeft, TD))
178         return false;
179       if (EltSize >= BytesLeft)
180         return true;
181       
182       Offset = 0;
183       BytesLeft -= EltSize;
184       CurPtr += EltSize;
185     }
186     return true;
187   }
188   
189   if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
190     uint64_t EltSize = TD.getTypeAllocSize(CV->getType()->getElementType());
191     uint64_t Index = ByteOffset / EltSize;
192     uint64_t Offset = ByteOffset - Index * EltSize;
193     for (; Index != CV->getType()->getNumElements(); ++Index) {
194       if (!ReadDataFromGlobal(CV->getOperand(Index), Offset, CurPtr,
195                               BytesLeft, TD))
196         return false;
197       if (EltSize >= BytesLeft)
198         return true;
199       
200       Offset = 0;
201       BytesLeft -= EltSize;
202       CurPtr += EltSize;
203     }
204     return true;
205   }
206   
207   // Otherwise, unknown initializer type.
208   return false;
209 }
210
211 static Constant *FoldReinterpretLoadFromConstPtr(Constant *C,
212                                                  const TargetData &TD) {
213   const Type *InitializerTy = cast<PointerType>(C->getType())->getElementType();
214   const IntegerType *IntType = dyn_cast<IntegerType>(InitializerTy);
215   
216   // If this isn't an integer load we can't fold it directly.
217   if (!IntType) {
218     // If this is a float/double load, we can try folding it as an int32/64 load
219     // and then bitcast the result.  This can be useful for union cases.
220     const Type *MapTy;
221     if (InitializerTy->isFloatTy())
222       MapTy = Type::getInt32PtrTy(C->getContext());
223     else if (InitializerTy->isDoubleTy())
224       MapTy = Type::getInt64PtrTy(C->getContext());
225     else
226       return 0;
227
228     C = ConstantExpr::getBitCast(C, MapTy);
229     if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, TD))
230       return ConstantExpr::getBitCast(Res, InitializerTy);
231     return 0;
232   }
233   
234   unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
235   if (BytesLoaded > 8 || BytesLoaded == 0) return 0;
236   
237   GlobalValue *GVal;
238   int64_t Offset;
239   if (!IsConstantOffsetFromGlobal(C, GVal, Offset, TD))
240     return 0;
241   
242   GlobalVariable *GV = dyn_cast<GlobalVariable>(GVal);
243   if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
244       !GV->hasDefinitiveInitializer() ||
245       !GV->getInitializer()->getType()->isSized())
246     return 0;
247
248   // If we're loading off the beginning of the global, some bytes may be valid,
249   // but we don't try to handle this.
250   if (Offset < 0) return 0;
251   
252   // If we're not accessing anything in this constant, the result is undefined.
253   if (uint64_t(Offset) >= TD.getTypeAllocSize(GV->getInitializer()->getType()))
254     return UndefValue::get(IntType);
255   
256   unsigned char RawBytes[8] = {0};
257   if (!ReadDataFromGlobal(GV->getInitializer(), Offset, RawBytes,
258                           BytesLoaded, TD))
259     return 0;
260
261   uint64_t ResultVal = 0;
262   for (unsigned i = 0; i != BytesLoaded; ++i)
263     ResultVal |= (uint64_t)RawBytes[i] << (i * 8);
264
265   return ConstantInt::get(IntType, ResultVal);
266 }
267
268 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
269 /// produce if it is constant and determinable.  If this is not determinable,
270 /// return null.
271 Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C,
272                                              const TargetData *TD) {
273   // First, try the easy cases:
274   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
275     if (GV->isConstant() && GV->hasDefinitiveInitializer())
276       return GV->getInitializer();
277
278   // If the loaded value isn't a constant expr, we can't handle it.
279   ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
280   if (!CE) return 0;
281   
282   if (CE->getOpcode() == Instruction::GetElementPtr) {
283     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
284       if (GV->isConstant() && GV->hasDefinitiveInitializer())
285         if (Constant *V = 
286              ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
287           return V;
288   }
289   
290   // Instead of loading constant c string, use corresponding integer value
291   // directly if string length is small enough.
292   std::string Str;
293   if (TD && GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) {
294     unsigned StrLen = Str.length();
295     const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
296     unsigned NumBits = Ty->getPrimitiveSizeInBits();
297     // Replace LI with immediate integer store.
298     if ((NumBits >> 3) == StrLen + 1) {
299       APInt StrVal(NumBits, 0);
300       APInt SingleChar(NumBits, 0);
301       if (TD->isLittleEndian()) {
302         for (signed i = StrLen-1; i >= 0; i--) {
303           SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
304           StrVal = (StrVal << 8) | SingleChar;
305         }
306       } else {
307         for (unsigned i = 0; i < StrLen; i++) {
308           SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
309           StrVal = (StrVal << 8) | SingleChar;
310         }
311         // Append NULL at the end.
312         SingleChar = 0;
313         StrVal = (StrVal << 8) | SingleChar;
314       }
315       return ConstantInt::get(CE->getContext(), StrVal);
316     }
317   }
318   
319   // If this load comes from anywhere in a constant global, and if the global
320   // is all undef or zero, we know what it loads.
321   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getUnderlyingObject())){
322     if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
323       const Type *ResTy = cast<PointerType>(C->getType())->getElementType();
324       if (GV->getInitializer()->isNullValue())
325         return Constant::getNullValue(ResTy);
326       if (isa<UndefValue>(GV->getInitializer()))
327         return UndefValue::get(ResTy);
328     }
329   }
330   
331   // Try hard to fold loads from bitcasted strange and non-type-safe things.  We
332   // currently don't do any of this for big endian systems.  It can be
333   // generalized in the future if someone is interested.
334   if (TD && TD->isLittleEndian())
335     return FoldReinterpretLoadFromConstPtr(CE, *TD);
336   return 0;
337 }
338
339 static Constant *ConstantFoldLoadInst(const LoadInst *LI, const TargetData *TD){
340   if (LI->isVolatile()) return 0;
341   
342   if (Constant *C = dyn_cast<Constant>(LI->getOperand(0)))
343     return ConstantFoldLoadFromConstPtr(C, TD);
344
345   return 0;
346 }
347
348 /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression.
349 /// Attempt to symbolically evaluate the result of a binary operator merging
350 /// these together.  If target data info is available, it is provided as TD, 
351 /// otherwise TD is null.
352 static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
353                                            Constant *Op1, const TargetData *TD,
354                                            LLVMContext &Context){
355   // SROA
356   
357   // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
358   // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
359   // bits.
360   
361   
362   // If the constant expr is something like &A[123] - &A[4].f, fold this into a
363   // constant.  This happens frequently when iterating over a global array.
364   if (Opc == Instruction::Sub && TD) {
365     GlobalValue *GV1, *GV2;
366     int64_t Offs1, Offs2;
367     
368     if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD))
369       if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) &&
370           GV1 == GV2) {
371         // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
372         return ConstantInt::get(Op0->getType(), Offs1-Offs2);
373       }
374   }
375     
376   return 0;
377 }
378
379 /// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP
380 /// constant expression, do so.
381 static Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps,
382                                          const Type *ResultTy,
383                                          LLVMContext &Context,
384                                          const TargetData *TD) {
385   Constant *Ptr = Ops[0];
386   if (!TD || !cast<PointerType>(Ptr->getType())->getElementType()->isSized())
387     return 0;
388
389   unsigned BitWidth = TD->getTypeSizeInBits(TD->getIntPtrType(Context));
390   APInt BasePtr(BitWidth, 0);
391   bool BaseIsInt = true;
392   if (!Ptr->isNullValue()) {
393     // If this is a inttoptr from a constant int, we can fold this as the base,
394     // otherwise we can't.
395     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
396       if (CE->getOpcode() == Instruction::IntToPtr)
397         if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0))) {
398           BasePtr = Base->getValue();
399           BasePtr.zextOrTrunc(BitWidth);
400         }
401     
402     if (BasePtr == 0)
403       BaseIsInt = false;
404   }
405
406   // If this is a constant expr gep that is effectively computing an
407   // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
408   for (unsigned i = 1; i != NumOps; ++i)
409     if (!isa<ConstantInt>(Ops[i]))
410       return 0;
411   
412   APInt Offset = APInt(BitWidth,
413                        TD->getIndexedOffset(Ptr->getType(),
414                                             (Value**)Ops+1, NumOps-1));
415   // If the base value for this address is a literal integer value, fold the
416   // getelementptr to the resulting integer value casted to the pointer type.
417   if (BaseIsInt) {
418     Constant *C = ConstantInt::get(Context, Offset+BasePtr);
419     return ConstantExpr::getIntToPtr(C, ResultTy);
420   }
421
422   // Otherwise form a regular getelementptr. Recompute the indices so that
423   // we eliminate over-indexing of the notional static type array bounds.
424   // This makes it easy to determine if the getelementptr is "inbounds".
425   // Also, this helps GlobalOpt do SROA on GlobalVariables.
426   const Type *Ty = Ptr->getType();
427   SmallVector<Constant*, 32> NewIdxs;
428   do {
429     if (const SequentialType *ATy = dyn_cast<SequentialType>(Ty)) {
430       // The only pointer indexing we'll do is on the first index of the GEP.
431       if (isa<PointerType>(ATy) && !NewIdxs.empty())
432         break;
433       // Determine which element of the array the offset points into.
434       APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType()));
435       if (ElemSize == 0)
436         return 0;
437       APInt NewIdx = Offset.udiv(ElemSize);
438       Offset -= NewIdx * ElemSize;
439       NewIdxs.push_back(ConstantInt::get(TD->getIntPtrType(Context), NewIdx));
440       Ty = ATy->getElementType();
441     } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
442       // Determine which field of the struct the offset points into. The
443       // getZExtValue is at least as safe as the StructLayout API because we
444       // know the offset is within the struct at this point.
445       const StructLayout &SL = *TD->getStructLayout(STy);
446       unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
447       NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Context), ElIdx));
448       Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
449       Ty = STy->getTypeAtIndex(ElIdx);
450     } else {
451       // We've reached some non-indexable type.
452       break;
453     }
454   } while (Ty != cast<PointerType>(ResultTy)->getElementType());
455
456   // If we haven't used up the entire offset by descending the static
457   // type, then the offset is pointing into the middle of an indivisible
458   // member, so we can't simplify it.
459   if (Offset != 0)
460     return 0;
461
462   // Create a GEP.
463   Constant *C =
464     ConstantExpr::getGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size());
465   assert(cast<PointerType>(C->getType())->getElementType() == Ty &&
466          "Computed GetElementPtr has unexpected type!");
467
468   // If we ended up indexing a member with a type that doesn't match
469   // the type of what the original indices indexed, add a cast.
470   if (Ty != cast<PointerType>(ResultTy)->getElementType())
471     C = ConstantExpr::getBitCast(C, ResultTy);
472
473   return C;
474 }
475
476 /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with 
477 /// targetdata.  Return 0 if unfoldable.
478 static Constant *FoldBitCast(Constant *C, const Type *DestTy,
479                              const TargetData &TD, LLVMContext &Context) {
480   // If this is a bitcast from constant vector -> vector, fold it.
481   if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
482     if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
483       // If the element types match, VMCore can fold it.
484       unsigned NumDstElt = DestVTy->getNumElements();
485       unsigned NumSrcElt = CV->getNumOperands();
486       if (NumDstElt == NumSrcElt)
487         return 0;
488       
489       const Type *SrcEltTy = CV->getType()->getElementType();
490       const Type *DstEltTy = DestVTy->getElementType();
491       
492       // Otherwise, we're changing the number of elements in a vector, which 
493       // requires endianness information to do the right thing.  For example,
494       //    bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
495       // folds to (little endian):
496       //    <4 x i32> <i32 0, i32 0, i32 1, i32 0>
497       // and to (big endian):
498       //    <4 x i32> <i32 0, i32 0, i32 0, i32 1>
499       
500       // First thing is first.  We only want to think about integer here, so if
501       // we have something in FP form, recast it as integer.
502       if (DstEltTy->isFloatingPoint()) {
503         // Fold to an vector of integers with same size as our FP type.
504         unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
505         const Type *DestIVTy = VectorType::get(
506                                  IntegerType::get(Context, FPWidth), NumDstElt);
507         // Recursively handle this integer conversion, if possible.
508         C = FoldBitCast(C, DestIVTy, TD, Context);
509         if (!C) return 0;
510         
511         // Finally, VMCore can handle this now that #elts line up.
512         return ConstantExpr::getBitCast(C, DestTy);
513       }
514       
515       // Okay, we know the destination is integer, if the input is FP, convert
516       // it to integer first.
517       if (SrcEltTy->isFloatingPoint()) {
518         unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
519         const Type *SrcIVTy = VectorType::get(
520                                  IntegerType::get(Context, FPWidth), NumSrcElt);
521         // Ask VMCore to do the conversion now that #elts line up.
522         C = ConstantExpr::getBitCast(C, SrcIVTy);
523         CV = dyn_cast<ConstantVector>(C);
524         if (!CV) return 0;  // If VMCore wasn't able to fold it, bail out.
525       }
526       
527       // Now we know that the input and output vectors are both integer vectors
528       // of the same size, and that their #elements is not the same.  Do the
529       // conversion here, which depends on whether the input or output has
530       // more elements.
531       bool isLittleEndian = TD.isLittleEndian();
532       
533       SmallVector<Constant*, 32> Result;
534       if (NumDstElt < NumSrcElt) {
535         // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
536         Constant *Zero = Constant::getNullValue(DstEltTy);
537         unsigned Ratio = NumSrcElt/NumDstElt;
538         unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
539         unsigned SrcElt = 0;
540         for (unsigned i = 0; i != NumDstElt; ++i) {
541           // Build each element of the result.
542           Constant *Elt = Zero;
543           unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
544           for (unsigned j = 0; j != Ratio; ++j) {
545             Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(SrcElt++));
546             if (!Src) return 0;  // Reject constantexpr elements.
547             
548             // Zero extend the element to the right size.
549             Src = ConstantExpr::getZExt(Src, Elt->getType());
550             
551             // Shift it to the right place, depending on endianness.
552             Src = ConstantExpr::getShl(Src, 
553                              ConstantInt::get(Src->getType(), ShiftAmt));
554             ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
555             
556             // Mix it in.
557             Elt = ConstantExpr::getOr(Elt, Src);
558           }
559           Result.push_back(Elt);
560         }
561       } else {
562         // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
563         unsigned Ratio = NumDstElt/NumSrcElt;
564         unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits();
565         
566         // Loop over each source value, expanding into multiple results.
567         for (unsigned i = 0; i != NumSrcElt; ++i) {
568           Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(i));
569           if (!Src) return 0;  // Reject constantexpr elements.
570
571           unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
572           for (unsigned j = 0; j != Ratio; ++j) {
573             // Shift the piece of the value into the right place, depending on
574             // endianness.
575             Constant *Elt = ConstantExpr::getLShr(Src, 
576                             ConstantInt::get(Src->getType(), ShiftAmt));
577             ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
578
579             // Truncate and remember this piece.
580             Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
581           }
582         }
583       }
584       
585       return ConstantVector::get(Result.data(), Result.size());
586     }
587   }
588   
589   return 0;
590 }
591
592
593 //===----------------------------------------------------------------------===//
594 // Constant Folding public APIs
595 //===----------------------------------------------------------------------===//
596
597
598 /// ConstantFoldInstruction - Attempt to constant fold the specified
599 /// instruction.  If successful, the constant result is returned, if not, null
600 /// is returned.  Note that this function can only fail when attempting to fold
601 /// instructions like loads and stores, which have no constant expression form.
602 ///
603 Constant *llvm::ConstantFoldInstruction(Instruction *I, LLVMContext &Context,
604                                         const TargetData *TD) {
605   if (PHINode *PN = dyn_cast<PHINode>(I)) {
606     if (PN->getNumIncomingValues() == 0)
607       return UndefValue::get(PN->getType());
608
609     Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
610     if (Result == 0) return 0;
611
612     // Handle PHI nodes specially here...
613     for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
614       if (PN->getIncomingValue(i) != Result && PN->getIncomingValue(i) != PN)
615         return 0;   // Not all the same incoming constants...
616
617     // If we reach here, all incoming values are the same constant.
618     return Result;
619   }
620
621   // Scan the operand list, checking to see if they are all constants, if so,
622   // hand off to ConstantFoldInstOperands.
623   SmallVector<Constant*, 8> Ops;
624   for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
625     if (Constant *Op = dyn_cast<Constant>(*i))
626       Ops.push_back(Op);
627     else
628       return 0;  // All operands not constant!
629
630   if (const CmpInst *CI = dyn_cast<CmpInst>(I))
631     return ConstantFoldCompareInstOperands(CI->getPredicate(),
632                                            Ops.data(), Ops.size(), 
633                                            Context, TD);
634   
635   if (const LoadInst *LI = dyn_cast<LoadInst>(I))
636     return ConstantFoldLoadInst(LI, TD);
637   
638   return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
639                                   Ops.data(), Ops.size(), Context, TD);
640 }
641
642 /// ConstantFoldConstantExpression - Attempt to fold the constant expression
643 /// using the specified TargetData.  If successful, the constant result is
644 /// result is returned, if not, null is returned.
645 Constant *llvm::ConstantFoldConstantExpression(ConstantExpr *CE,
646                                                LLVMContext &Context,
647                                                const TargetData *TD) {
648   SmallVector<Constant*, 8> Ops;
649   for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
650     Ops.push_back(cast<Constant>(*i));
651
652   if (CE->isCompare())
653     return ConstantFoldCompareInstOperands(CE->getPredicate(),
654                                            Ops.data(), Ops.size(), 
655                                            Context, TD);
656   return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(),
657                                   Ops.data(), Ops.size(), Context, TD);
658 }
659
660 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
661 /// specified opcode and operands.  If successful, the constant result is
662 /// returned, if not, null is returned.  Note that this function can fail when
663 /// attempting to fold instructions like loads and stores, which have no
664 /// constant expression form.
665 ///
666 Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy, 
667                                          Constant* const* Ops, unsigned NumOps,
668                                          LLVMContext &Context,
669                                          const TargetData *TD) {
670   // Handle easy binops first.
671   if (Instruction::isBinaryOp(Opcode)) {
672     if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1]))
673       if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD,
674                                                   Context))
675         return C;
676     
677     return ConstantExpr::get(Opcode, Ops[0], Ops[1]);
678   }
679   
680   switch (Opcode) {
681   default: return 0;
682   case Instruction::Call:
683     if (Function *F = dyn_cast<Function>(Ops[0]))
684       if (canConstantFoldCallTo(F))
685         return ConstantFoldCall(F, Ops+1, NumOps-1);
686     return 0;
687   case Instruction::ICmp:
688   case Instruction::FCmp:
689     llvm_unreachable("This function is invalid for compares: no predicate specified");
690   case Instruction::PtrToInt:
691     // If the input is a inttoptr, eliminate the pair.  This requires knowing
692     // the width of a pointer, so it can't be done in ConstantExpr::getCast.
693     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
694       if (TD && CE->getOpcode() == Instruction::IntToPtr) {
695         Constant *Input = CE->getOperand(0);
696         unsigned InWidth = Input->getType()->getScalarSizeInBits();
697         if (TD->getPointerSizeInBits() < InWidth) {
698           Constant *Mask = 
699             ConstantInt::get(Context, APInt::getLowBitsSet(InWidth,
700                                                   TD->getPointerSizeInBits()));
701           Input = ConstantExpr::getAnd(Input, Mask);
702         }
703         // Do a zext or trunc to get to the dest size.
704         return ConstantExpr::getIntegerCast(Input, DestTy, false);
705       }
706     }
707     return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
708   case Instruction::IntToPtr:
709     // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
710     // the int size is >= the ptr size.  This requires knowing the width of a
711     // pointer, so it can't be done in ConstantExpr::getCast.
712     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
713       if (TD &&
714           TD->getPointerSizeInBits() <=
715           CE->getType()->getScalarSizeInBits()) {
716         if (CE->getOpcode() == Instruction::PtrToInt) {
717           Constant *Input = CE->getOperand(0);
718           Constant *C = FoldBitCast(Input, DestTy, *TD, Context);
719           return C ? C : ConstantExpr::getBitCast(Input, DestTy);
720         }
721         // If there's a constant offset added to the integer value before
722         // it is casted back to a pointer, see if the expression can be
723         // converted into a GEP.
724         if (CE->getOpcode() == Instruction::Add)
725           if (ConstantInt *L = dyn_cast<ConstantInt>(CE->getOperand(0)))
726             if (ConstantExpr *R = dyn_cast<ConstantExpr>(CE->getOperand(1)))
727               if (R->getOpcode() == Instruction::PtrToInt)
728                 if (GlobalVariable *GV =
729                       dyn_cast<GlobalVariable>(R->getOperand(0))) {
730                   const PointerType *GVTy = cast<PointerType>(GV->getType());
731                   if (const ArrayType *AT =
732                         dyn_cast<ArrayType>(GVTy->getElementType())) {
733                     const Type *ElTy = AT->getElementType();
734                     uint64_t AllocSize = TD->getTypeAllocSize(ElTy);
735                     APInt PSA(L->getValue().getBitWidth(), AllocSize);
736                     if (ElTy == cast<PointerType>(DestTy)->getElementType() &&
737                         L->getValue().urem(PSA) == 0) {
738                       APInt ElemIdx = L->getValue().udiv(PSA);
739                       if (ElemIdx.ult(APInt(ElemIdx.getBitWidth(),
740                                             AT->getNumElements()))) {
741                         Constant *Index[] = {
742                           Constant::getNullValue(CE->getType()),
743                           ConstantInt::get(Context, ElemIdx)
744                         };
745                         return
746                         ConstantExpr::getGetElementPtr(GV, &Index[0], 2);
747                       }
748                     }
749                   }
750                 }
751       }
752     }
753     return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
754   case Instruction::Trunc:
755   case Instruction::ZExt:
756   case Instruction::SExt:
757   case Instruction::FPTrunc:
758   case Instruction::FPExt:
759   case Instruction::UIToFP:
760   case Instruction::SIToFP:
761   case Instruction::FPToUI:
762   case Instruction::FPToSI:
763       return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
764   case Instruction::BitCast:
765     if (TD)
766       if (Constant *C = FoldBitCast(Ops[0], DestTy, *TD, Context))
767         return C;
768     return ConstantExpr::getBitCast(Ops[0], DestTy);
769   case Instruction::Select:
770     return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
771   case Instruction::ExtractElement:
772     return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
773   case Instruction::InsertElement:
774     return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
775   case Instruction::ShuffleVector:
776     return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
777   case Instruction::GetElementPtr:
778     if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, DestTy, Context, TD))
779       return C;
780     
781     return ConstantExpr::getGetElementPtr(Ops[0], Ops+1, NumOps-1);
782   }
783 }
784
785 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
786 /// instruction (icmp/fcmp) with the specified operands.  If it fails, it
787 /// returns a constant expression of the specified operands.
788 ///
789 Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
790                                                 Constant*const * Ops, 
791                                                 unsigned NumOps,
792                                                 LLVMContext &Context,
793                                                 const TargetData *TD) {
794   // fold: icmp (inttoptr x), null         -> icmp x, 0
795   // fold: icmp (ptrtoint x), 0            -> icmp x, null
796   // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
797   // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
798   //
799   // ConstantExpr::getCompare cannot do this, because it doesn't have TD
800   // around to know if bit truncation is happening.
801   if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops[0])) {
802     if (TD && Ops[1]->isNullValue()) {
803       const Type *IntPtrTy = TD->getIntPtrType(Context);
804       if (CE0->getOpcode() == Instruction::IntToPtr) {
805         // Convert the integer value to the right size to ensure we get the
806         // proper extension or truncation.
807         Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
808                                                    IntPtrTy, false);
809         Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) };
810         return ConstantFoldCompareInstOperands(Predicate, NewOps, 2,
811                                                Context, TD);
812       }
813       
814       // Only do this transformation if the int is intptrty in size, otherwise
815       // there is a truncation or extension that we aren't modeling.
816       if (CE0->getOpcode() == Instruction::PtrToInt && 
817           CE0->getType() == IntPtrTy) {
818         Constant *C = CE0->getOperand(0);
819         Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) };
820         // FIXME!
821         return ConstantFoldCompareInstOperands(Predicate, NewOps, 2,
822                                                Context, TD);
823       }
824     }
825     
826     if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops[1])) {
827       if (TD && CE0->getOpcode() == CE1->getOpcode()) {
828         const Type *IntPtrTy = TD->getIntPtrType(Context);
829
830         if (CE0->getOpcode() == Instruction::IntToPtr) {
831           // Convert the integer value to the right size to ensure we get the
832           // proper extension or truncation.
833           Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
834                                                       IntPtrTy, false);
835           Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
836                                                       IntPtrTy, false);
837           Constant *NewOps[] = { C0, C1 };
838           return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, 
839                                                  Context, TD);
840         }
841
842         // Only do this transformation if the int is intptrty in size, otherwise
843         // there is a truncation or extension that we aren't modeling.
844         if ((CE0->getOpcode() == Instruction::PtrToInt &&
845              CE0->getType() == IntPtrTy &&
846              CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType())) {
847           Constant *NewOps[] = { 
848             CE0->getOperand(0), CE1->getOperand(0) 
849           };
850           return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, 
851                                                  Context, TD);
852         }
853       }
854     }
855   }
856   return ConstantExpr::getCompare(Predicate, Ops[0], Ops[1]);
857 }
858
859
860 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
861 /// getelementptr constantexpr, return the constant value being addressed by the
862 /// constant expression, or null if something is funny and we can't decide.
863 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, 
864                                                        ConstantExpr *CE) {
865   if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType()))
866     return 0;  // Do not allow stepping over the value!
867   
868   // Loop over all of the operands, tracking down which value we are
869   // addressing...
870   gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
871   for (++I; I != E; ++I)
872     if (const StructType *STy = dyn_cast<StructType>(*I)) {
873       ConstantInt *CU = cast<ConstantInt>(I.getOperand());
874       assert(CU->getZExtValue() < STy->getNumElements() &&
875              "Struct index out of range!");
876       unsigned El = (unsigned)CU->getZExtValue();
877       if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
878         C = CS->getOperand(El);
879       } else if (isa<ConstantAggregateZero>(C)) {
880         C = Constant::getNullValue(STy->getElementType(El));
881       } else if (isa<UndefValue>(C)) {
882         C = UndefValue::get(STy->getElementType(El));
883       } else {
884         return 0;
885       }
886     } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) {
887       if (const ArrayType *ATy = dyn_cast<ArrayType>(*I)) {
888         if (CI->getZExtValue() >= ATy->getNumElements())
889          return 0;
890         if (ConstantArray *CA = dyn_cast<ConstantArray>(C))
891           C = CA->getOperand(CI->getZExtValue());
892         else if (isa<ConstantAggregateZero>(C))
893           C = Constant::getNullValue(ATy->getElementType());
894         else if (isa<UndefValue>(C))
895           C = UndefValue::get(ATy->getElementType());
896         else
897           return 0;
898       } else if (const VectorType *VTy = dyn_cast<VectorType>(*I)) {
899         if (CI->getZExtValue() >= VTy->getNumElements())
900           return 0;
901         if (ConstantVector *CP = dyn_cast<ConstantVector>(C))
902           C = CP->getOperand(CI->getZExtValue());
903         else if (isa<ConstantAggregateZero>(C))
904           C = Constant::getNullValue(VTy->getElementType());
905         else if (isa<UndefValue>(C))
906           C = UndefValue::get(VTy->getElementType());
907         else
908           return 0;
909       } else {
910         return 0;
911       }
912     } else {
913       return 0;
914     }
915   return C;
916 }
917
918
919 //===----------------------------------------------------------------------===//
920 //  Constant Folding for Calls
921 //
922
923 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
924 /// the specified function.
925 bool
926 llvm::canConstantFoldCallTo(const Function *F) {
927   switch (F->getIntrinsicID()) {
928   case Intrinsic::sqrt:
929   case Intrinsic::powi:
930   case Intrinsic::bswap:
931   case Intrinsic::ctpop:
932   case Intrinsic::ctlz:
933   case Intrinsic::cttz:
934   case Intrinsic::uadd_with_overflow:
935   case Intrinsic::usub_with_overflow:
936   case Intrinsic::sadd_with_overflow:
937   case Intrinsic::ssub_with_overflow:
938     return true;
939   default:
940     return false;
941   case 0: break;
942   }
943
944   if (!F->hasName()) return false;
945   StringRef Name = F->getName();
946   
947   // In these cases, the check of the length is required.  We don't want to
948   // return true for a name like "cos\0blah" which strcmp would return equal to
949   // "cos", but has length 8.
950   switch (Name[0]) {
951   default: return false;
952   case 'a':
953     return Name == "acos" || Name == "asin" || 
954       Name == "atan" || Name == "atan2";
955   case 'c':
956     return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh";
957   case 'e':
958     return Name == "exp";
959   case 'f':
960     return Name == "fabs" || Name == "fmod" || Name == "floor";
961   case 'l':
962     return Name == "log" || Name == "log10";
963   case 'p':
964     return Name == "pow";
965   case 's':
966     return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
967       Name == "sinf" || Name == "sqrtf";
968   case 't':
969     return Name == "tan" || Name == "tanh";
970   }
971 }
972
973 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, 
974                                 const Type *Ty, LLVMContext &Context) {
975   errno = 0;
976   V = NativeFP(V);
977   if (errno != 0) {
978     errno = 0;
979     return 0;
980   }
981   
982   if (Ty->isFloatTy())
983     return ConstantFP::get(Context, APFloat((float)V));
984   if (Ty->isDoubleTy())
985     return ConstantFP::get(Context, APFloat(V));
986   llvm_unreachable("Can only constant fold float/double");
987   return 0; // dummy return to suppress warning
988 }
989
990 static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
991                                       double V, double W,
992                                       const Type *Ty,
993                                       LLVMContext &Context) {
994   errno = 0;
995   V = NativeFP(V, W);
996   if (errno != 0) {
997     errno = 0;
998     return 0;
999   }
1000   
1001   if (Ty->isFloatTy())
1002     return ConstantFP::get(Context, APFloat((float)V));
1003   if (Ty->isDoubleTy())
1004     return ConstantFP::get(Context, APFloat(V));
1005   llvm_unreachable("Can only constant fold float/double");
1006   return 0; // dummy return to suppress warning
1007 }
1008
1009 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
1010 /// with the specified arguments, returning null if unsuccessful.
1011 Constant *
1012 llvm::ConstantFoldCall(Function *F, 
1013                        Constant *const *Operands, unsigned NumOperands) {
1014   if (!F->hasName()) return 0;
1015   LLVMContext &Context = F->getContext();
1016   StringRef Name = F->getName();
1017
1018   const Type *Ty = F->getReturnType();
1019   if (NumOperands == 1) {
1020     if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
1021       if (!Ty->isFloatTy() && !Ty->isDoubleTy())
1022         return 0;
1023       /// Currently APFloat versions of these functions do not exist, so we use
1024       /// the host native double versions.  Float versions are not called
1025       /// directly but for all these it is true (float)(f((double)arg)) ==
1026       /// f(arg).  Long double not supported yet.
1027       double V = Ty->isFloatTy() ? (double)Op->getValueAPF().convertToFloat() :
1028                                      Op->getValueAPF().convertToDouble();
1029       switch (Name[0]) {
1030       case 'a':
1031         if (Name == "acos")
1032           return ConstantFoldFP(acos, V, Ty, Context);
1033         else if (Name == "asin")
1034           return ConstantFoldFP(asin, V, Ty, Context);
1035         else if (Name == "atan")
1036           return ConstantFoldFP(atan, V, Ty, Context);
1037         break;
1038       case 'c':
1039         if (Name == "ceil")
1040           return ConstantFoldFP(ceil, V, Ty, Context);
1041         else if (Name == "cos")
1042           return ConstantFoldFP(cos, V, Ty, Context);
1043         else if (Name == "cosh")
1044           return ConstantFoldFP(cosh, V, Ty, Context);
1045         else if (Name == "cosf")
1046           return ConstantFoldFP(cos, V, Ty, Context);
1047         break;
1048       case 'e':
1049         if (Name == "exp")
1050           return ConstantFoldFP(exp, V, Ty, Context);
1051         break;
1052       case 'f':
1053         if (Name == "fabs")
1054           return ConstantFoldFP(fabs, V, Ty, Context);
1055         else if (Name == "floor")
1056           return ConstantFoldFP(floor, V, Ty, Context);
1057         break;
1058       case 'l':
1059         if (Name == "log" && V > 0)
1060           return ConstantFoldFP(log, V, Ty, Context);
1061         else if (Name == "log10" && V > 0)
1062           return ConstantFoldFP(log10, V, Ty, Context);
1063         else if (Name == "llvm.sqrt.f32" ||
1064                  Name == "llvm.sqrt.f64") {
1065           if (V >= -0.0)
1066             return ConstantFoldFP(sqrt, V, Ty, Context);
1067           else // Undefined
1068             return Constant::getNullValue(Ty);
1069         }
1070         break;
1071       case 's':
1072         if (Name == "sin")
1073           return ConstantFoldFP(sin, V, Ty, Context);
1074         else if (Name == "sinh")
1075           return ConstantFoldFP(sinh, V, Ty, Context);
1076         else if (Name == "sqrt" && V >= 0)
1077           return ConstantFoldFP(sqrt, V, Ty, Context);
1078         else if (Name == "sqrtf" && V >= 0)
1079           return ConstantFoldFP(sqrt, V, Ty, Context);
1080         else if (Name == "sinf")
1081           return ConstantFoldFP(sin, V, Ty, Context);
1082         break;
1083       case 't':
1084         if (Name == "tan")
1085           return ConstantFoldFP(tan, V, Ty, Context);
1086         else if (Name == "tanh")
1087           return ConstantFoldFP(tanh, V, Ty, Context);
1088         break;
1089       default:
1090         break;
1091       }
1092       return 0;
1093     }
1094     
1095     
1096     if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
1097       if (Name.startswith("llvm.bswap"))
1098         return ConstantInt::get(Context, Op->getValue().byteSwap());
1099       else if (Name.startswith("llvm.ctpop"))
1100         return ConstantInt::get(Ty, Op->getValue().countPopulation());
1101       else if (Name.startswith("llvm.cttz"))
1102         return ConstantInt::get(Ty, Op->getValue().countTrailingZeros());
1103       else if (Name.startswith("llvm.ctlz"))
1104         return ConstantInt::get(Ty, Op->getValue().countLeadingZeros());
1105       return 0;
1106     }
1107     
1108     return 0;
1109   }
1110   
1111   if (NumOperands == 2) {
1112     if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
1113       if (!Ty->isFloatTy() && !Ty->isDoubleTy())
1114         return 0;
1115       double Op1V = Ty->isFloatTy() ? 
1116                       (double)Op1->getValueAPF().convertToFloat() :
1117                       Op1->getValueAPF().convertToDouble();
1118       if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
1119         if (Op2->getType() != Op1->getType())
1120           return 0;
1121         
1122         double Op2V = Ty->isFloatTy() ? 
1123                       (double)Op2->getValueAPF().convertToFloat():
1124                       Op2->getValueAPF().convertToDouble();
1125
1126         if (Name == "pow")
1127           return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty, Context);
1128         if (Name == "fmod")
1129           return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty, Context);
1130         if (Name == "atan2")
1131           return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty, Context);
1132       } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
1133         if (Name == "llvm.powi.f32")
1134           return ConstantFP::get(Context, APFloat((float)std::pow((float)Op1V,
1135                                                  (int)Op2C->getZExtValue())));
1136         if (Name == "llvm.powi.f64")
1137           return ConstantFP::get(Context, APFloat((double)std::pow((double)Op1V,
1138                                                  (int)Op2C->getZExtValue())));
1139       }
1140       return 0;
1141     }
1142     
1143     
1144     if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
1145       if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
1146         switch (F->getIntrinsicID()) {
1147         default: break;
1148         case Intrinsic::uadd_with_overflow: {
1149           Constant *Res = ConstantExpr::getAdd(Op1, Op2);           // result.
1150           Constant *Ops[] = {
1151             Res, ConstantExpr::getICmp(CmpInst::ICMP_ULT, Res, Op1) // overflow.
1152           };
1153           return ConstantStruct::get(F->getContext(), Ops, 2, false);
1154         }
1155         case Intrinsic::usub_with_overflow: {
1156           Constant *Res = ConstantExpr::getSub(Op1, Op2);           // result.
1157           Constant *Ops[] = {
1158             Res, ConstantExpr::getICmp(CmpInst::ICMP_UGT, Res, Op1) // overflow.
1159           };
1160           return ConstantStruct::get(F->getContext(), Ops, 2, false);
1161         }
1162         case Intrinsic::sadd_with_overflow: {
1163           Constant *Res = ConstantExpr::getAdd(Op1, Op2);           // result.
1164           Constant *Overflow = ConstantExpr::getSelect(
1165               ConstantExpr::getICmp(CmpInst::ICMP_SGT,
1166                 ConstantInt::get(Op1->getType(), 0), Op1),
1167               ConstantExpr::getICmp(CmpInst::ICMP_SGT, Res, Op2), 
1168               ConstantExpr::getICmp(CmpInst::ICMP_SLT, Res, Op2)); // overflow.
1169
1170           Constant *Ops[] = { Res, Overflow };
1171           return ConstantStruct::get(F->getContext(), Ops, 2, false);
1172         }
1173         case Intrinsic::ssub_with_overflow: {
1174           Constant *Res = ConstantExpr::getSub(Op1, Op2);           // result.
1175           Constant *Overflow = ConstantExpr::getSelect(
1176               ConstantExpr::getICmp(CmpInst::ICMP_SGT,
1177                 ConstantInt::get(Op2->getType(), 0), Op2),
1178               ConstantExpr::getICmp(CmpInst::ICMP_SLT, Res, Op1), 
1179               ConstantExpr::getICmp(CmpInst::ICMP_SGT, Res, Op1)); // overflow.
1180
1181           Constant *Ops[] = { Res, Overflow };
1182           return ConstantStruct::get(F->getContext(), Ops, 2, false);
1183         }
1184         }
1185       }
1186       
1187       return 0;
1188     }
1189     return 0;
1190   }
1191   return 0;
1192 }
1193