0e4bf6c7bfb6b086ba07cb9c849c8867b43cdf20
[oota-llvm.git] / lib / VMCore / ConstantFold.cpp
1 //===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements folding of constants for LLVM.  This implements the
11 // (internal) ConstantFold.h interface, which is used by the
12 // ConstantExpr::get* methods to automatically fold constants when possible.
13 //
14 // The current constant folding implementation is implemented in two pieces: the
15 // template-based folder for simple primitive constants like ConstantInt, and
16 // the special case hackery that we use to symbolically evaluate expressions
17 // that use ConstantExprs.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #include "ConstantFold.h"
22 #include "llvm/Constants.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/Function.h"
26 #include "llvm/GlobalAlias.h"
27 #include "llvm/LLVMContext.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/GetElementPtrTypeIterator.h"
32 #include "llvm/Support/ManagedStatic.h"
33 #include "llvm/Support/MathExtras.h"
34 #include <limits>
35 using namespace llvm;
36
37 //===----------------------------------------------------------------------===//
38 //                ConstantFold*Instruction Implementations
39 //===----------------------------------------------------------------------===//
40
41 /// BitCastConstantVector - Convert the specified ConstantVector node to the
42 /// specified vector type.  At this point, we know that the elements of the
43 /// input vector constant are all simple integer or FP values.
44 static Constant *BitCastConstantVector(LLVMContext &Context, ConstantVector *CV,
45                                        const VectorType *DstTy) {
46   // If this cast changes element count then we can't handle it here:
47   // doing so requires endianness information.  This should be handled by
48   // Analysis/ConstantFolding.cpp
49   unsigned NumElts = DstTy->getNumElements();
50   if (NumElts != CV->getNumOperands())
51     return 0;
52   
53   // Check to verify that all elements of the input are simple.
54   for (unsigned i = 0; i != NumElts; ++i) {
55     if (!isa<ConstantInt>(CV->getOperand(i)) &&
56         !isa<ConstantFP>(CV->getOperand(i)))
57       return 0;
58   }
59
60   // Bitcast each element now.
61   std::vector<Constant*> Result;
62   const Type *DstEltTy = DstTy->getElementType();
63   for (unsigned i = 0; i != NumElts; ++i)
64     Result.push_back(Context.getConstantExprBitCast(CV->getOperand(i),
65                                                     DstEltTy));
66   return Context.getConstantVector(Result);
67 }
68
69 /// This function determines which opcode to use to fold two constant cast 
70 /// expressions together. It uses CastInst::isEliminableCastPair to determine
71 /// the opcode. Consequently its just a wrapper around that function.
72 /// @brief Determine if it is valid to fold a cast of a cast
73 static unsigned
74 foldConstantCastPair(
75   unsigned opc,          ///< opcode of the second cast constant expression
76   const ConstantExpr*Op, ///< the first cast constant expression
77   const Type *DstTy      ///< desintation type of the first cast
78 ) {
79   assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
80   assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
81   assert(CastInst::isCast(opc) && "Invalid cast opcode");
82   
83   // The the types and opcodes for the two Cast constant expressions
84   const Type *SrcTy = Op->getOperand(0)->getType();
85   const Type *MidTy = Op->getType();
86   Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
87   Instruction::CastOps secondOp = Instruction::CastOps(opc);
88
89   // Let CastInst::isEliminableCastPair do the heavy lifting.
90   return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
91                                         Type::Int64Ty);
92 }
93
94 static Constant *FoldBitCast(LLVMContext &Context, 
95                              Constant *V, const Type *DestTy) {
96   const Type *SrcTy = V->getType();
97   if (SrcTy == DestTy)
98     return V; // no-op cast
99   
100   // Check to see if we are casting a pointer to an aggregate to a pointer to
101   // the first element.  If so, return the appropriate GEP instruction.
102   if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
103     if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy))
104       if (PTy->getAddressSpace() == DPTy->getAddressSpace()) {
105         SmallVector<Value*, 8> IdxList;
106         IdxList.push_back(Context.getNullValue(Type::Int32Ty));
107         const Type *ElTy = PTy->getElementType();
108         while (ElTy != DPTy->getElementType()) {
109           if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
110             if (STy->getNumElements() == 0) break;
111             ElTy = STy->getElementType(0);
112             IdxList.push_back(Context.getNullValue(Type::Int32Ty));
113           } else if (const SequentialType *STy = 
114                      dyn_cast<SequentialType>(ElTy)) {
115             if (isa<PointerType>(ElTy)) break;  // Can't index into pointers!
116             ElTy = STy->getElementType();
117             IdxList.push_back(IdxList[0]);
118           } else {
119             break;
120           }
121         }
122         
123         if (ElTy == DPTy->getElementType())
124           return Context.getConstantExprGetElementPtr(V, &IdxList[0],
125                                                       IdxList.size());
126       }
127   
128   // Handle casts from one vector constant to another.  We know that the src 
129   // and dest type have the same size (otherwise its an illegal cast).
130   if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
131     if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
132       assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
133              "Not cast between same sized vectors!");
134       SrcTy = NULL;
135       // First, check for null.  Undef is already handled.
136       if (isa<ConstantAggregateZero>(V))
137         return Context.getNullValue(DestTy);
138       
139       if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
140         return BitCastConstantVector(Context, CV, DestPTy);
141     }
142
143     // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
144     // This allows for other simplifications (although some of them
145     // can only be handled by Analysis/ConstantFolding.cpp).
146     if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
147       return Context.getConstantExprBitCast(
148                                      Context.getConstantVector(&V, 1), DestPTy);
149   }
150   
151   // Finally, implement bitcast folding now.   The code below doesn't handle
152   // bitcast right.
153   if (isa<ConstantPointerNull>(V))  // ptr->ptr cast.
154     return Context.getConstantPointerNull(cast<PointerType>(DestTy));
155   
156   // Handle integral constant input.
157   if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
158     if (DestTy->isInteger())
159       // Integral -> Integral. This is a no-op because the bit widths must
160       // be the same. Consequently, we just fold to V.
161       return V;
162
163     if (DestTy->isFloatingPoint())
164       return Context.getConstantFP(APFloat(CI->getValue(),
165                                      DestTy != Type::PPC_FP128Ty));
166
167     // Otherwise, can't fold this (vector?)
168     return 0;
169   }
170
171   // Handle ConstantFP input.
172   if (const ConstantFP *FP = dyn_cast<ConstantFP>(V))
173     // FP -> Integral.
174     return Context.getConstantInt(FP->getValueAPF().bitcastToAPInt());
175
176   return 0;
177 }
178
179
180 Constant *llvm::ConstantFoldCastInstruction(LLVMContext &Context, 
181                                             unsigned opc, const Constant *V,
182                                             const Type *DestTy) {
183   if (isa<UndefValue>(V)) {
184     // zext(undef) = 0, because the top bits will be zero.
185     // sext(undef) = 0, because the top bits will all be the same.
186     // [us]itofp(undef) = 0, because the result value is bounded.
187     if (opc == Instruction::ZExt || opc == Instruction::SExt ||
188         opc == Instruction::UIToFP || opc == Instruction::SIToFP)
189       return Context.getNullValue(DestTy);
190     return Context.getUndef(DestTy);
191   }
192   // No compile-time operations on this type yet.
193   if (V->getType() == Type::PPC_FP128Ty || DestTy == Type::PPC_FP128Ty)
194     return 0;
195
196   // If the cast operand is a constant expression, there's a few things we can
197   // do to try to simplify it.
198   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
199     if (CE->isCast()) {
200       // Try hard to fold cast of cast because they are often eliminable.
201       if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
202         return Context.getConstantExprCast(newOpc, CE->getOperand(0), DestTy);
203     } else if (CE->getOpcode() == Instruction::GetElementPtr) {
204       // If all of the indexes in the GEP are null values, there is no pointer
205       // adjustment going on.  We might as well cast the source pointer.
206       bool isAllNull = true;
207       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
208         if (!CE->getOperand(i)->isNullValue()) {
209           isAllNull = false;
210           break;
211         }
212       if (isAllNull)
213         // This is casting one pointer type to another, always BitCast
214         return Context.getConstantExprPointerCast(CE->getOperand(0), DestTy);
215     }
216   }
217
218   // If the cast operand is a constant vector, perform the cast by
219   // operating on each element. In the cast of bitcasts, the element
220   // count may be mismatched; don't attempt to handle that here.
221   if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
222     if (isa<VectorType>(DestTy) &&
223         cast<VectorType>(DestTy)->getNumElements() ==
224         CV->getType()->getNumElements()) {
225       std::vector<Constant*> res;
226       const VectorType *DestVecTy = cast<VectorType>(DestTy);
227       const Type *DstEltTy = DestVecTy->getElementType();
228       for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
229         res.push_back(Context.getConstantExprCast(opc,
230                                             CV->getOperand(i), DstEltTy));
231       return Context.getConstantVector(DestVecTy, res);
232     }
233
234   // We actually have to do a cast now. Perform the cast according to the
235   // opcode specified.
236   switch (opc) {
237   case Instruction::FPTrunc:
238   case Instruction::FPExt:
239     if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
240       bool ignored;
241       APFloat Val = FPC->getValueAPF();
242       Val.convert(DestTy == Type::FloatTy ? APFloat::IEEEsingle :
243                   DestTy == Type::DoubleTy ? APFloat::IEEEdouble :
244                   DestTy == Type::X86_FP80Ty ? APFloat::x87DoubleExtended :
245                   DestTy == Type::FP128Ty ? APFloat::IEEEquad :
246                   APFloat::Bogus,
247                   APFloat::rmNearestTiesToEven, &ignored);
248       return Context.getConstantFP(Val);
249     }
250     return 0; // Can't fold.
251   case Instruction::FPToUI: 
252   case Instruction::FPToSI:
253     if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
254       const APFloat &V = FPC->getValueAPF();
255       bool ignored;
256       uint64_t x[2]; 
257       uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
258       (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
259                                 APFloat::rmTowardZero, &ignored);
260       APInt Val(DestBitWidth, 2, x);
261       return Context.getConstantInt(Val);
262     }
263     return 0; // Can't fold.
264   case Instruction::IntToPtr:   //always treated as unsigned
265     if (V->isNullValue())       // Is it an integral null value?
266       return Context.getConstantPointerNull(cast<PointerType>(DestTy));
267     return 0;                   // Other pointer types cannot be casted
268   case Instruction::PtrToInt:   // always treated as unsigned
269     if (V->isNullValue())       // is it a null pointer value?
270       return Context.getConstantInt(DestTy, 0);
271     return 0;                   // Other pointer types cannot be casted
272   case Instruction::UIToFP:
273   case Instruction::SIToFP:
274     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
275       APInt api = CI->getValue();
276       const uint64_t zero[] = {0, 0};
277       APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(),
278                                   2, zero));
279       (void)apf.convertFromAPInt(api, 
280                                  opc==Instruction::SIToFP,
281                                  APFloat::rmNearestTiesToEven);
282       return Context.getConstantFP(apf);
283     }
284     return 0;
285   case Instruction::ZExt:
286     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
287       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
288       APInt Result(CI->getValue());
289       Result.zext(BitWidth);
290       return Context.getConstantInt(Result);
291     }
292     return 0;
293   case Instruction::SExt:
294     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
295       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
296       APInt Result(CI->getValue());
297       Result.sext(BitWidth);
298       return Context.getConstantInt(Result);
299     }
300     return 0;
301   case Instruction::Trunc:
302     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
303       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
304       APInt Result(CI->getValue());
305       Result.trunc(BitWidth);
306       return Context.getConstantInt(Result);
307     }
308     return 0;
309   case Instruction::BitCast:
310     return FoldBitCast(Context, const_cast<Constant*>(V), DestTy);
311   default:
312     assert(!"Invalid CE CastInst opcode");
313     break;
314   }
315
316   llvm_unreachable("Failed to cast constant expression");
317   return 0;
318 }
319
320 Constant *llvm::ConstantFoldSelectInstruction(LLVMContext&,
321                                               const Constant *Cond,
322                                               const Constant *V1,
323                                               const Constant *V2) {
324   if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
325     return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
326
327   if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
328   if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
329   if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
330   if (V1 == V2) return const_cast<Constant*>(V1);
331   return 0;
332 }
333
334 Constant *llvm::ConstantFoldExtractElementInstruction(LLVMContext &Context,
335                                                       const Constant *Val,
336                                                       const Constant *Idx) {
337   if (isa<UndefValue>(Val))  // ee(undef, x) -> undef
338     return Context.getUndef(cast<VectorType>(Val->getType())->getElementType());
339   if (Val->isNullValue())  // ee(zero, x) -> zero
340     return Context.getNullValue(
341                           cast<VectorType>(Val->getType())->getElementType());
342   
343   if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
344     if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
345       return CVal->getOperand(CIdx->getZExtValue());
346     } else if (isa<UndefValue>(Idx)) {
347       // ee({w,x,y,z}, undef) -> w (an arbitrary value).
348       return CVal->getOperand(0);
349     }
350   }
351   return 0;
352 }
353
354 Constant *llvm::ConstantFoldInsertElementInstruction(LLVMContext &Context,
355                                                      const Constant *Val,
356                                                      const Constant *Elt,
357                                                      const Constant *Idx) {
358   const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
359   if (!CIdx) return 0;
360   APInt idxVal = CIdx->getValue();
361   if (isa<UndefValue>(Val)) { 
362     // Insertion of scalar constant into vector undef
363     // Optimize away insertion of undef
364     if (isa<UndefValue>(Elt))
365       return const_cast<Constant*>(Val);
366     // Otherwise break the aggregate undef into multiple undefs and do
367     // the insertion
368     unsigned numOps = 
369       cast<VectorType>(Val->getType())->getNumElements();
370     std::vector<Constant*> Ops; 
371     Ops.reserve(numOps);
372     for (unsigned i = 0; i < numOps; ++i) {
373       const Constant *Op =
374         (idxVal == i) ? Elt : Context.getUndef(Elt->getType());
375       Ops.push_back(const_cast<Constant*>(Op));
376     }
377     return Context.getConstantVector(Ops);
378   }
379   if (isa<ConstantAggregateZero>(Val)) {
380     // Insertion of scalar constant into vector aggregate zero
381     // Optimize away insertion of zero
382     if (Elt->isNullValue())
383       return const_cast<Constant*>(Val);
384     // Otherwise break the aggregate zero into multiple zeros and do
385     // the insertion
386     unsigned numOps = 
387       cast<VectorType>(Val->getType())->getNumElements();
388     std::vector<Constant*> Ops; 
389     Ops.reserve(numOps);
390     for (unsigned i = 0; i < numOps; ++i) {
391       const Constant *Op =
392         (idxVal == i) ? Elt : Context.getNullValue(Elt->getType());
393       Ops.push_back(const_cast<Constant*>(Op));
394     }
395     return Context.getConstantVector(Ops);
396   }
397   if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
398     // Insertion of scalar constant into vector constant
399     std::vector<Constant*> Ops; 
400     Ops.reserve(CVal->getNumOperands());
401     for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
402       const Constant *Op =
403         (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
404       Ops.push_back(const_cast<Constant*>(Op));
405     }
406     return Context.getConstantVector(Ops);
407   }
408
409   return 0;
410 }
411
412 /// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef
413 /// return the specified element value.  Otherwise return null.
414 static Constant *GetVectorElement(LLVMContext &Context, const Constant *C,
415                                   unsigned EltNo) {
416   if (const ConstantVector *CV = dyn_cast<ConstantVector>(C))
417     return CV->getOperand(EltNo);
418   
419   const Type *EltTy = cast<VectorType>(C->getType())->getElementType();
420   if (isa<ConstantAggregateZero>(C))
421     return Context.getNullValue(EltTy);
422   if (isa<UndefValue>(C))
423     return Context.getUndef(EltTy);
424   return 0;
425 }
426
427 Constant *llvm::ConstantFoldShuffleVectorInstruction(LLVMContext &Context,
428                                                      const Constant *V1,
429                                                      const Constant *V2,
430                                                      const Constant *Mask) {
431   // Undefined shuffle mask -> undefined value.
432   if (isa<UndefValue>(Mask)) return Context.getUndef(V1->getType());
433
434   unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements();
435   unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements();
436   const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
437
438   // Loop over the shuffle mask, evaluating each element.
439   SmallVector<Constant*, 32> Result;
440   for (unsigned i = 0; i != MaskNumElts; ++i) {
441     Constant *InElt = GetVectorElement(Context, Mask, i);
442     if (InElt == 0) return 0;
443
444     if (isa<UndefValue>(InElt))
445       InElt = Context.getUndef(EltTy);
446     else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) {
447       unsigned Elt = CI->getZExtValue();
448       if (Elt >= SrcNumElts*2)
449         InElt = Context.getUndef(EltTy);
450       else if (Elt >= SrcNumElts)
451         InElt = GetVectorElement(Context, V2, Elt - SrcNumElts);
452       else
453         InElt = GetVectorElement(Context, V1, Elt);
454       if (InElt == 0) return 0;
455     } else {
456       // Unknown value.
457       return 0;
458     }
459     Result.push_back(InElt);
460   }
461
462   return Context.getConstantVector(&Result[0], Result.size());
463 }
464
465 Constant *llvm::ConstantFoldExtractValueInstruction(LLVMContext &Context,
466                                                     const Constant *Agg,
467                                                     const unsigned *Idxs,
468                                                     unsigned NumIdx) {
469   // Base case: no indices, so return the entire value.
470   if (NumIdx == 0)
471     return const_cast<Constant *>(Agg);
472
473   if (isa<UndefValue>(Agg))  // ev(undef, x) -> undef
474     return Context.getUndef(ExtractValueInst::getIndexedType(Agg->getType(),
475                                                             Idxs,
476                                                             Idxs + NumIdx));
477
478   if (isa<ConstantAggregateZero>(Agg))  // ev(0, x) -> 0
479     return
480       Context.getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
481                                                               Idxs,
482                                                               Idxs + NumIdx));
483
484   // Otherwise recurse.
485   return ConstantFoldExtractValueInstruction(Context, Agg->getOperand(*Idxs),
486                                              Idxs+1, NumIdx-1);
487 }
488
489 Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
490                                                    const Constant *Agg,
491                                                    const Constant *Val,
492                                                    const unsigned *Idxs,
493                                                    unsigned NumIdx) {
494   // Base case: no indices, so replace the entire value.
495   if (NumIdx == 0)
496     return const_cast<Constant *>(Val);
497
498   if (isa<UndefValue>(Agg)) {
499     // Insertion of constant into aggregate undef
500     // Optimize away insertion of undef
501     if (isa<UndefValue>(Val))
502       return const_cast<Constant*>(Agg);
503     // Otherwise break the aggregate undef into multiple undefs and do
504     // the insertion
505     const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
506     unsigned numOps;
507     if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
508       numOps = AR->getNumElements();
509     else
510       numOps = cast<StructType>(AggTy)->getNumElements();
511     std::vector<Constant*> Ops(numOps); 
512     for (unsigned i = 0; i < numOps; ++i) {
513       const Type *MemberTy = AggTy->getTypeAtIndex(i);
514       const Constant *Op =
515         (*Idxs == i) ?
516         ConstantFoldInsertValueInstruction(Context, Context.getUndef(MemberTy),
517                                            Val, Idxs+1, NumIdx-1) :
518         Context.getUndef(MemberTy);
519       Ops[i] = const_cast<Constant*>(Op);
520     }
521     if (isa<StructType>(AggTy))
522       return Context.getConstantStruct(Ops);
523     else
524       return Context.getConstantArray(cast<ArrayType>(AggTy), Ops);
525   }
526   if (isa<ConstantAggregateZero>(Agg)) {
527     // Insertion of constant into aggregate zero
528     // Optimize away insertion of zero
529     if (Val->isNullValue())
530       return const_cast<Constant*>(Agg);
531     // Otherwise break the aggregate zero into multiple zeros and do
532     // the insertion
533     const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
534     unsigned numOps;
535     if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
536       numOps = AR->getNumElements();
537     else
538       numOps = cast<StructType>(AggTy)->getNumElements();
539     std::vector<Constant*> Ops(numOps);
540     for (unsigned i = 0; i < numOps; ++i) {
541       const Type *MemberTy = AggTy->getTypeAtIndex(i);
542       const Constant *Op =
543         (*Idxs == i) ?
544         ConstantFoldInsertValueInstruction(Context, 
545                                            Context.getNullValue(MemberTy),
546                                            Val, Idxs+1, NumIdx-1) :
547         Context.getNullValue(MemberTy);
548       Ops[i] = const_cast<Constant*>(Op);
549     }
550     if (isa<StructType>(AggTy))
551       return Context.getConstantStruct(Ops);
552     else
553       return Context.getConstantArray(cast<ArrayType>(AggTy), Ops);
554   }
555   if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) {
556     // Insertion of constant into aggregate constant
557     std::vector<Constant*> Ops(Agg->getNumOperands());
558     for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
559       const Constant *Op =
560         (*Idxs == i) ?
561         ConstantFoldInsertValueInstruction(Context, Agg->getOperand(i),
562                                            Val, Idxs+1, NumIdx-1) :
563         Agg->getOperand(i);
564       Ops[i] = const_cast<Constant*>(Op);
565     }
566     Constant *C;
567     if (isa<StructType>(Agg->getType()))
568       C = Context.getConstantStruct(Ops);
569     else
570       C = Context.getConstantArray(cast<ArrayType>(Agg->getType()), Ops);
571     return C;
572   }
573
574   return 0;
575 }
576
577
578 Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context,
579                                               unsigned Opcode,
580                                               const Constant *C1,
581                                               const Constant *C2) {
582   // No compile-time operations on this type yet.
583   if (C1->getType() == Type::PPC_FP128Ty)
584     return 0;
585
586   // Handle UndefValue up front
587   if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
588     switch (Opcode) {
589     case Instruction::Xor:
590       if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
591         // Handle undef ^ undef -> 0 special case. This is a common
592         // idiom (misuse).
593         return Context.getNullValue(C1->getType());
594       // Fallthrough
595     case Instruction::Add:
596     case Instruction::Sub:
597       return Context.getUndef(C1->getType());
598     case Instruction::Mul:
599     case Instruction::And:
600       return Context.getNullValue(C1->getType());
601     case Instruction::UDiv:
602     case Instruction::SDiv:
603     case Instruction::URem:
604     case Instruction::SRem:
605       if (!isa<UndefValue>(C2))                    // undef / X -> 0
606         return Context.getNullValue(C1->getType());
607       return const_cast<Constant*>(C2);            // X / undef -> undef
608     case Instruction::Or:                          // X | undef -> -1
609       if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
610         return Context.getAllOnesValue(PTy);
611       return Context.getAllOnesValue(C1->getType());
612     case Instruction::LShr:
613       if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
614         return const_cast<Constant*>(C1);           // undef lshr undef -> undef
615       return Context.getNullValue(C1->getType()); // X lshr undef -> 0
616                                                     // undef lshr X -> 0
617     case Instruction::AShr:
618       if (!isa<UndefValue>(C2))
619         return const_cast<Constant*>(C1);           // undef ashr X --> undef
620       else if (isa<UndefValue>(C1)) 
621         return const_cast<Constant*>(C1);           // undef ashr undef -> undef
622       else
623         return const_cast<Constant*>(C1);           // X ashr undef --> X
624     case Instruction::Shl:
625       // undef << X -> 0   or   X << undef -> 0
626       return Context.getNullValue(C1->getType());
627     }
628   }
629
630   // Handle simplifications when the RHS is a constant int.
631   if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
632     switch (Opcode) {
633     case Instruction::Add:
634       if (CI2->equalsInt(0)) return const_cast<Constant*>(C1);  // X + 0 == X
635       break;
636     case Instruction::Sub:
637       if (CI2->equalsInt(0)) return const_cast<Constant*>(C1);  // X - 0 == X
638       break;
639     case Instruction::Mul:
640       if (CI2->equalsInt(0)) return const_cast<Constant*>(C2);  // X * 0 == 0
641       if (CI2->equalsInt(1))
642         return const_cast<Constant*>(C1);                       // X * 1 == X
643       break;
644     case Instruction::UDiv:
645     case Instruction::SDiv:
646       if (CI2->equalsInt(1))
647         return const_cast<Constant*>(C1);                     // X / 1 == X
648       if (CI2->equalsInt(0))
649         return Context.getUndef(CI2->getType());               // X / 0 == undef
650       break;
651     case Instruction::URem:
652     case Instruction::SRem:
653       if (CI2->equalsInt(1))
654         return Context.getNullValue(CI2->getType());        // X % 1 == 0
655       if (CI2->equalsInt(0))
656         return Context.getUndef(CI2->getType());               // X % 0 == undef
657       break;
658     case Instruction::And:
659       if (CI2->isZero()) return const_cast<Constant*>(C2);    // X & 0 == 0
660       if (CI2->isAllOnesValue())
661         return const_cast<Constant*>(C1);                     // X & -1 == X
662       
663       if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
664         // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
665         if (CE1->getOpcode() == Instruction::ZExt) {
666           unsigned DstWidth = CI2->getType()->getBitWidth();
667           unsigned SrcWidth =
668             CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
669           APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
670           if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
671             return const_cast<Constant*>(C1);
672         }
673         
674         // If and'ing the address of a global with a constant, fold it.
675         if (CE1->getOpcode() == Instruction::PtrToInt && 
676             isa<GlobalValue>(CE1->getOperand(0))) {
677           GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
678         
679           // Functions are at least 4-byte aligned.
680           unsigned GVAlign = GV->getAlignment();
681           if (isa<Function>(GV))
682             GVAlign = std::max(GVAlign, 4U);
683           
684           if (GVAlign > 1) {
685             unsigned DstWidth = CI2->getType()->getBitWidth();
686             unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
687             APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
688
689             // If checking bits we know are clear, return zero.
690             if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
691               return Context.getNullValue(CI2->getType());
692           }
693         }
694       }
695       break;
696     case Instruction::Or:
697       if (CI2->equalsInt(0)) return const_cast<Constant*>(C1);  // X | 0 == X
698       if (CI2->isAllOnesValue())
699         return const_cast<Constant*>(C2);  // X | -1 == -1
700       break;
701     case Instruction::Xor:
702       if (CI2->equalsInt(0)) return const_cast<Constant*>(C1);  // X ^ 0 == X
703       break;
704     case Instruction::AShr:
705       // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
706       if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
707         if (CE1->getOpcode() == Instruction::ZExt)  // Top bits known zero.
708           return Context.getConstantExprLShr(const_cast<Constant*>(C1),
709                                              const_cast<Constant*>(C2));
710       break;
711     }
712   }
713   
714   // At this point we know neither constant is an UndefValue.
715   if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
716     if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
717       using namespace APIntOps;
718       const APInt &C1V = CI1->getValue();
719       const APInt &C2V = CI2->getValue();
720       switch (Opcode) {
721       default:
722         break;
723       case Instruction::Add:     
724         return Context.getConstantInt(C1V + C2V);
725       case Instruction::Sub:     
726         return Context.getConstantInt(C1V - C2V);
727       case Instruction::Mul:     
728         return Context.getConstantInt(C1V * C2V);
729       case Instruction::UDiv:
730         assert(!CI2->isNullValue() && "Div by zero handled above");
731         return Context.getConstantInt(C1V.udiv(C2V));
732       case Instruction::SDiv:
733         assert(!CI2->isNullValue() && "Div by zero handled above");
734         if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
735           return Context.getUndef(CI1->getType());   // MIN_INT / -1 -> undef
736         return Context.getConstantInt(C1V.sdiv(C2V));
737       case Instruction::URem:
738         assert(!CI2->isNullValue() && "Div by zero handled above");
739         return Context.getConstantInt(C1V.urem(C2V));
740       case Instruction::SRem:
741         assert(!CI2->isNullValue() && "Div by zero handled above");
742         if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
743           return Context.getUndef(CI1->getType());   // MIN_INT % -1 -> undef
744         return Context.getConstantInt(C1V.srem(C2V));
745       case Instruction::And:
746         return Context.getConstantInt(C1V & C2V);
747       case Instruction::Or:
748         return Context.getConstantInt(C1V | C2V);
749       case Instruction::Xor:
750         return Context.getConstantInt(C1V ^ C2V);
751       case Instruction::Shl: {
752         uint32_t shiftAmt = C2V.getZExtValue();
753         if (shiftAmt < C1V.getBitWidth())
754           return Context.getConstantInt(C1V.shl(shiftAmt));
755         else
756           return Context.getUndef(C1->getType()); // too big shift is undef
757       }
758       case Instruction::LShr: {
759         uint32_t shiftAmt = C2V.getZExtValue();
760         if (shiftAmt < C1V.getBitWidth())
761           return Context.getConstantInt(C1V.lshr(shiftAmt));
762         else
763           return Context.getUndef(C1->getType()); // too big shift is undef
764       }
765       case Instruction::AShr: {
766         uint32_t shiftAmt = C2V.getZExtValue();
767         if (shiftAmt < C1V.getBitWidth())
768           return Context.getConstantInt(C1V.ashr(shiftAmt));
769         else
770           return Context.getUndef(C1->getType()); // too big shift is undef
771       }
772       }
773     }
774
775     switch (Opcode) {
776     case Instruction::SDiv:
777     case Instruction::UDiv:
778     case Instruction::URem:
779     case Instruction::SRem:
780     case Instruction::LShr:
781     case Instruction::AShr:
782     case Instruction::Shl:
783       if (CI1->equalsInt(0)) return const_cast<Constant*>(C1);
784       break;
785     default:
786       break;
787     }
788   } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
789     if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
790       APFloat C1V = CFP1->getValueAPF();
791       APFloat C2V = CFP2->getValueAPF();
792       APFloat C3V = C1V;  // copy for modification
793       switch (Opcode) {
794       default:                   
795         break;
796       case Instruction::FAdd:
797         (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
798         return Context.getConstantFP(C3V);
799       case Instruction::FSub:
800         (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
801         return Context.getConstantFP(C3V);
802       case Instruction::FMul:
803         (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
804         return Context.getConstantFP(C3V);
805       case Instruction::FDiv:
806         (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
807         return Context.getConstantFP(C3V);
808       case Instruction::FRem:
809         (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
810         return Context.getConstantFP(C3V);
811       }
812     }
813   } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
814     const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1);
815     const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2);
816     if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) &&
817         (CP2 != NULL || isa<ConstantAggregateZero>(C2))) {
818       std::vector<Constant*> Res;
819       const Type* EltTy = VTy->getElementType();  
820       const Constant *C1 = 0;
821       const Constant *C2 = 0;
822       switch (Opcode) {
823       default:
824         break;
825       case Instruction::Add:
826         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
827           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
828           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
829           Res.push_back(Context.getConstantExprAdd(const_cast<Constant*>(C1),
830                                                    const_cast<Constant*>(C2)));
831         }
832         return Context.getConstantVector(Res);
833       case Instruction::FAdd:
834         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
835           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
836           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
837           Res.push_back(Context.getConstantExprFAdd(const_cast<Constant*>(C1),
838                                                     const_cast<Constant*>(C2)));
839         }
840         return Context.getConstantVector(Res);
841       case Instruction::Sub:
842         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
843           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
844           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
845           Res.push_back(Context.getConstantExprSub(const_cast<Constant*>(C1),
846                                                    const_cast<Constant*>(C2)));
847         }
848         return Context.getConstantVector(Res);
849       case Instruction::FSub:
850         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
851           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
852           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
853           Res.push_back(Context.getConstantExprFSub(const_cast<Constant*>(C1),
854                                                     const_cast<Constant*>(C2)));
855         }
856         return Context.getConstantVector(Res);
857       case Instruction::Mul:
858         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
859           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
860           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
861           Res.push_back(Context.getConstantExprMul(const_cast<Constant*>(C1),
862                                                    const_cast<Constant*>(C2)));
863         }
864         return Context.getConstantVector(Res);
865       case Instruction::FMul:
866         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
867           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
868           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
869           Res.push_back(Context.getConstantExprFMul(const_cast<Constant*>(C1),
870                                                     const_cast<Constant*>(C2)));
871         }
872         return Context.getConstantVector(Res);
873       case Instruction::UDiv:
874         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
875           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
876           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
877           Res.push_back(Context.getConstantExprUDiv(const_cast<Constant*>(C1),
878                                                     const_cast<Constant*>(C2)));
879         }
880         return Context.getConstantVector(Res);
881       case Instruction::SDiv:
882         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
883           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
884           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
885           Res.push_back(Context.getConstantExprSDiv(const_cast<Constant*>(C1),
886                                                     const_cast<Constant*>(C2)));
887         }
888         return Context.getConstantVector(Res);
889       case Instruction::FDiv:
890         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
891           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
892           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
893           Res.push_back(Context.getConstantExprFDiv(const_cast<Constant*>(C1),
894                                                     const_cast<Constant*>(C2)));
895         }
896         return Context.getConstantVector(Res);
897       case Instruction::URem:
898         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
899           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
900           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
901           Res.push_back(Context.getConstantExprURem(const_cast<Constant*>(C1),
902                                                     const_cast<Constant*>(C2)));
903         }
904         return Context.getConstantVector(Res);
905       case Instruction::SRem:
906         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
907           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
908           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
909           Res.push_back(Context.getConstantExprSRem(const_cast<Constant*>(C1),
910                                                     const_cast<Constant*>(C2)));
911         }
912         return Context.getConstantVector(Res);
913       case Instruction::FRem:
914         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
915           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
916           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
917           Res.push_back(Context.getConstantExprFRem(const_cast<Constant*>(C1),
918                                                     const_cast<Constant*>(C2)));
919         }
920         return Context.getConstantVector(Res);
921       case Instruction::And: 
922         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
923           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
924           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
925           Res.push_back(Context.getConstantExprAnd(const_cast<Constant*>(C1),
926                                                    const_cast<Constant*>(C2)));
927         }
928         return Context.getConstantVector(Res);
929       case Instruction::Or:
930         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
931           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
932           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
933           Res.push_back(Context.getConstantExprOr(const_cast<Constant*>(C1),
934                                                   const_cast<Constant*>(C2)));
935         }
936         return Context.getConstantVector(Res);
937       case Instruction::Xor:
938         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
939           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
940           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
941           Res.push_back(Context.getConstantExprXor(const_cast<Constant*>(C1),
942                                                    const_cast<Constant*>(C2)));
943         }
944         return Context.getConstantVector(Res);
945       case Instruction::LShr:
946         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
947           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
948           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
949           Res.push_back(Context.getConstantExprLShr(const_cast<Constant*>(C1),
950                                                     const_cast<Constant*>(C2)));
951         }
952         return Context.getConstantVector(Res);
953       case Instruction::AShr:
954         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
955           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
956           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
957           Res.push_back(Context.getConstantExprAShr(const_cast<Constant*>(C1),
958                                                     const_cast<Constant*>(C2)));
959         }
960         return Context.getConstantVector(Res);
961       case Instruction::Shl:
962         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
963           C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
964           C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
965           Res.push_back(Context.getConstantExprShl(const_cast<Constant*>(C1),
966                                                    const_cast<Constant*>(C2)));
967         }
968         return Context.getConstantVector(Res);
969       }
970     }
971   }
972
973   if (isa<ConstantExpr>(C1)) {
974     // There are many possible foldings we could do here.  We should probably
975     // at least fold add of a pointer with an integer into the appropriate
976     // getelementptr.  This will improve alias analysis a bit.
977   } else if (isa<ConstantExpr>(C2)) {
978     // If C2 is a constant expr and C1 isn't, flop them around and fold the
979     // other way if possible.
980     switch (Opcode) {
981     case Instruction::Add:
982     case Instruction::FAdd:
983     case Instruction::Mul:
984     case Instruction::FMul:
985     case Instruction::And:
986     case Instruction::Or:
987     case Instruction::Xor:
988       // No change of opcode required.
989       return ConstantFoldBinaryInstruction(Context, Opcode, C2, C1);
990       
991     case Instruction::Shl:
992     case Instruction::LShr:
993     case Instruction::AShr:
994     case Instruction::Sub:
995     case Instruction::FSub:
996     case Instruction::SDiv:
997     case Instruction::UDiv:
998     case Instruction::FDiv:
999     case Instruction::URem:
1000     case Instruction::SRem:
1001     case Instruction::FRem:
1002     default:  // These instructions cannot be flopped around.
1003       break;
1004     }
1005   }
1006   
1007   // We don't know how to fold this.
1008   return 0;
1009 }
1010
1011 /// isZeroSizedType - This type is zero sized if its an array or structure of
1012 /// zero sized types.  The only leaf zero sized type is an empty structure.
1013 static bool isMaybeZeroSizedType(const Type *Ty) {
1014   if (isa<OpaqueType>(Ty)) return true;  // Can't say.
1015   if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1016
1017     // If all of elements have zero size, this does too.
1018     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1019       if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
1020     return true;
1021
1022   } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1023     return isMaybeZeroSizedType(ATy->getElementType());
1024   }
1025   return false;
1026 }
1027
1028 /// IdxCompare - Compare the two constants as though they were getelementptr
1029 /// indices.  This allows coersion of the types to be the same thing.
1030 ///
1031 /// If the two constants are the "same" (after coersion), return 0.  If the
1032 /// first is less than the second, return -1, if the second is less than the
1033 /// first, return 1.  If the constants are not integral, return -2.
1034 ///
1035 static int IdxCompare(LLVMContext &Context, Constant *C1, Constant *C2, 
1036                       const Type *ElTy) {
1037   if (C1 == C2) return 0;
1038
1039   // Ok, we found a different index.  If they are not ConstantInt, we can't do
1040   // anything with them.
1041   if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1042     return -2; // don't know!
1043
1044   // Ok, we have two differing integer indices.  Sign extend them to be the same
1045   // type.  Long is always big enough, so we use it.
1046   if (C1->getType() != Type::Int64Ty)
1047     C1 = Context.getConstantExprSExt(C1, Type::Int64Ty);
1048
1049   if (C2->getType() != Type::Int64Ty)
1050     C2 = Context.getConstantExprSExt(C2, Type::Int64Ty);
1051
1052   if (C1 == C2) return 0;  // They are equal
1053
1054   // If the type being indexed over is really just a zero sized type, there is
1055   // no pointer difference being made here.
1056   if (isMaybeZeroSizedType(ElTy))
1057     return -2; // dunno.
1058
1059   // If they are really different, now that they are the same type, then we
1060   // found a difference!
1061   if (cast<ConstantInt>(C1)->getSExtValue() < 
1062       cast<ConstantInt>(C2)->getSExtValue())
1063     return -1;
1064   else
1065     return 1;
1066 }
1067
1068 /// evaluateFCmpRelation - This function determines if there is anything we can
1069 /// decide about the two constants provided.  This doesn't need to handle simple
1070 /// things like ConstantFP comparisons, but should instead handle ConstantExprs.
1071 /// If we can determine that the two constants have a particular relation to 
1072 /// each other, we should return the corresponding FCmpInst predicate, 
1073 /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
1074 /// ConstantFoldCompareInstruction.
1075 ///
1076 /// To simplify this code we canonicalize the relation so that the first
1077 /// operand is always the most "complex" of the two.  We consider ConstantFP
1078 /// to be the simplest, and ConstantExprs to be the most complex.
1079 static FCmpInst::Predicate evaluateFCmpRelation(LLVMContext &Context,
1080                                                 const Constant *V1, 
1081                                                 const Constant *V2) {
1082   assert(V1->getType() == V2->getType() &&
1083          "Cannot compare values of different types!");
1084
1085   // No compile-time operations on this type yet.
1086   if (V1->getType() == Type::PPC_FP128Ty)
1087     return FCmpInst::BAD_FCMP_PREDICATE;
1088
1089   // Handle degenerate case quickly
1090   if (V1 == V2) return FCmpInst::FCMP_OEQ;
1091
1092   if (!isa<ConstantExpr>(V1)) {
1093     if (!isa<ConstantExpr>(V2)) {
1094       // We distilled thisUse the standard constant folder for a few cases
1095       ConstantInt *R = 0;
1096       Constant *C1 = const_cast<Constant*>(V1);
1097       Constant *C2 = const_cast<Constant*>(V2);
1098       R = dyn_cast<ConstantInt>(
1099                       Context.getConstantExprFCmp(FCmpInst::FCMP_OEQ, C1, C2));
1100       if (R && !R->isZero()) 
1101         return FCmpInst::FCMP_OEQ;
1102       R = dyn_cast<ConstantInt>(
1103                       Context.getConstantExprFCmp(FCmpInst::FCMP_OLT, C1, C2));
1104       if (R && !R->isZero()) 
1105         return FCmpInst::FCMP_OLT;
1106       R = dyn_cast<ConstantInt>(
1107                       Context.getConstantExprFCmp(FCmpInst::FCMP_OGT, C1, C2));
1108       if (R && !R->isZero()) 
1109         return FCmpInst::FCMP_OGT;
1110
1111       // Nothing more we can do
1112       return FCmpInst::BAD_FCMP_PREDICATE;
1113     }
1114     
1115     // If the first operand is simple and second is ConstantExpr, swap operands.
1116     FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(Context, V2, V1);
1117     if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
1118       return FCmpInst::getSwappedPredicate(SwappedRelation);
1119   } else {
1120     // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
1121     // constantexpr or a simple constant.
1122     const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1123     switch (CE1->getOpcode()) {
1124     case Instruction::FPTrunc:
1125     case Instruction::FPExt:
1126     case Instruction::UIToFP:
1127     case Instruction::SIToFP:
1128       // We might be able to do something with these but we don't right now.
1129       break;
1130     default:
1131       break;
1132     }
1133   }
1134   // There are MANY other foldings that we could perform here.  They will
1135   // probably be added on demand, as they seem needed.
1136   return FCmpInst::BAD_FCMP_PREDICATE;
1137 }
1138
1139 /// evaluateICmpRelation - This function determines if there is anything we can
1140 /// decide about the two constants provided.  This doesn't need to handle simple
1141 /// things like integer comparisons, but should instead handle ConstantExprs
1142 /// and GlobalValues.  If we can determine that the two constants have a
1143 /// particular relation to each other, we should return the corresponding ICmp
1144 /// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
1145 ///
1146 /// To simplify this code we canonicalize the relation so that the first
1147 /// operand is always the most "complex" of the two.  We consider simple
1148 /// constants (like ConstantInt) to be the simplest, followed by
1149 /// GlobalValues, followed by ConstantExpr's (the most complex).
1150 ///
1151 static ICmpInst::Predicate evaluateICmpRelation(LLVMContext &Context,
1152                                                 const Constant *V1, 
1153                                                 const Constant *V2,
1154                                                 bool isSigned) {
1155   assert(V1->getType() == V2->getType() &&
1156          "Cannot compare different types of values!");
1157   if (V1 == V2) return ICmpInst::ICMP_EQ;
1158
1159   if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
1160     if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1161       // We distilled this down to a simple case, use the standard constant
1162       // folder.
1163       ConstantInt *R = 0;
1164       Constant *C1 = const_cast<Constant*>(V1);
1165       Constant *C2 = const_cast<Constant*>(V2);
1166       ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
1167       R = dyn_cast<ConstantInt>(Context.getConstantExprICmp(pred, C1, C2));
1168       if (R && !R->isZero()) 
1169         return pred;
1170       pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1171       R = dyn_cast<ConstantInt>(Context.getConstantExprICmp(pred, C1, C2));
1172       if (R && !R->isZero())
1173         return pred;
1174       pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1175       R = dyn_cast<ConstantInt>(Context.getConstantExprICmp(pred, C1, C2));
1176       if (R && !R->isZero())
1177         return pred;
1178       
1179       // If we couldn't figure it out, bail.
1180       return ICmpInst::BAD_ICMP_PREDICATE;
1181     }
1182     
1183     // If the first operand is simple, swap operands.
1184     ICmpInst::Predicate SwappedRelation = 
1185       evaluateICmpRelation(Context, V2, V1, isSigned);
1186     if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1187       return ICmpInst::getSwappedPredicate(SwappedRelation);
1188
1189   } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
1190     if (isa<ConstantExpr>(V2)) {  // Swap as necessary.
1191       ICmpInst::Predicate SwappedRelation = 
1192         evaluateICmpRelation(Context, V2, V1, isSigned);
1193       if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1194         return ICmpInst::getSwappedPredicate(SwappedRelation);
1195       else
1196         return ICmpInst::BAD_ICMP_PREDICATE;
1197     }
1198
1199     // Now we know that the RHS is a GlobalValue or simple constant,
1200     // which (since the types must match) means that it's a ConstantPointerNull.
1201     if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
1202       // Don't try to decide equality of aliases.
1203       if (!isa<GlobalAlias>(CPR1) && !isa<GlobalAlias>(CPR2))
1204         if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
1205           return ICmpInst::ICMP_NE;
1206     } else {
1207       assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
1208       // GlobalVals can never be null.  Don't try to evaluate aliases.
1209       if (!CPR1->hasExternalWeakLinkage() && !isa<GlobalAlias>(CPR1))
1210         return ICmpInst::ICMP_NE;
1211     }
1212   } else {
1213     // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
1214     // constantexpr, a CPR, or a simple constant.
1215     const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1216     const Constant *CE1Op0 = CE1->getOperand(0);
1217
1218     switch (CE1->getOpcode()) {
1219     case Instruction::Trunc:
1220     case Instruction::FPTrunc:
1221     case Instruction::FPExt:
1222     case Instruction::FPToUI:
1223     case Instruction::FPToSI:
1224       break; // We can't evaluate floating point casts or truncations.
1225
1226     case Instruction::UIToFP:
1227     case Instruction::SIToFP:
1228     case Instruction::BitCast:
1229     case Instruction::ZExt:
1230     case Instruction::SExt:
1231       // If the cast is not actually changing bits, and the second operand is a
1232       // null pointer, do the comparison with the pre-casted value.
1233       if (V2->isNullValue() &&
1234           (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
1235         bool sgnd = isSigned;
1236         if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1237         if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
1238         return evaluateICmpRelation(Context, CE1Op0,
1239                                     Context.getNullValue(CE1Op0->getType()), 
1240                                     sgnd);
1241       }
1242
1243       // If the dest type is a pointer type, and the RHS is a constantexpr cast
1244       // from the same type as the src of the LHS, evaluate the inputs.  This is
1245       // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)",
1246       // which happens a lot in compilers with tagged integers.
1247       if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
1248         if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
1249             CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
1250             CE1->getOperand(0)->getType()->isInteger()) {
1251           bool sgnd = isSigned;
1252           if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1253           if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
1254           return evaluateICmpRelation(Context, CE1->getOperand(0), 
1255                                       CE2->getOperand(0), sgnd);
1256         }
1257       break;
1258
1259     case Instruction::GetElementPtr:
1260       // Ok, since this is a getelementptr, we know that the constant has a
1261       // pointer type.  Check the various cases.
1262       if (isa<ConstantPointerNull>(V2)) {
1263         // If we are comparing a GEP to a null pointer, check to see if the base
1264         // of the GEP equals the null pointer.
1265         if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1266           if (GV->hasExternalWeakLinkage())
1267             // Weak linkage GVals could be zero or not. We're comparing that
1268             // to null pointer so its greater-or-equal
1269             return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
1270           else 
1271             // If its not weak linkage, the GVal must have a non-zero address
1272             // so the result is greater-than
1273             return isSigned ? ICmpInst::ICMP_SGT :  ICmpInst::ICMP_UGT;
1274         } else if (isa<ConstantPointerNull>(CE1Op0)) {
1275           // If we are indexing from a null pointer, check to see if we have any
1276           // non-zero indices.
1277           for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1278             if (!CE1->getOperand(i)->isNullValue())
1279               // Offsetting from null, must not be equal.
1280               return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1281           // Only zero indexes from null, must still be zero.
1282           return ICmpInst::ICMP_EQ;
1283         }
1284         // Otherwise, we can't really say if the first operand is null or not.
1285       } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
1286         if (isa<ConstantPointerNull>(CE1Op0)) {
1287           if (CPR2->hasExternalWeakLinkage())
1288             // Weak linkage GVals could be zero or not. We're comparing it to
1289             // a null pointer, so its less-or-equal
1290             return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
1291           else
1292             // If its not weak linkage, the GVal must have a non-zero address
1293             // so the result is less-than
1294             return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1295         } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
1296           if (CPR1 == CPR2) {
1297             // If this is a getelementptr of the same global, then it must be
1298             // different.  Because the types must match, the getelementptr could
1299             // only have at most one index, and because we fold getelementptr's
1300             // with a single zero index, it must be nonzero.
1301             assert(CE1->getNumOperands() == 2 &&
1302                    !CE1->getOperand(1)->isNullValue() &&
1303                    "Suprising getelementptr!");
1304             return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1305           } else {
1306             // If they are different globals, we don't know what the value is,
1307             // but they can't be equal.
1308             return ICmpInst::ICMP_NE;
1309           }
1310         }
1311       } else {
1312         const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1313         const Constant *CE2Op0 = CE2->getOperand(0);
1314
1315         // There are MANY other foldings that we could perform here.  They will
1316         // probably be added on demand, as they seem needed.
1317         switch (CE2->getOpcode()) {
1318         default: break;
1319         case Instruction::GetElementPtr:
1320           // By far the most common case to handle is when the base pointers are
1321           // obviously to the same or different globals.
1322           if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1323             if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
1324               return ICmpInst::ICMP_NE;
1325             // Ok, we know that both getelementptr instructions are based on the
1326             // same global.  From this, we can precisely determine the relative
1327             // ordering of the resultant pointers.
1328             unsigned i = 1;
1329
1330             // Compare all of the operands the GEP's have in common.
1331             gep_type_iterator GTI = gep_type_begin(CE1);
1332             for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1333                  ++i, ++GTI)
1334               switch (IdxCompare(Context, CE1->getOperand(i),
1335                                  CE2->getOperand(i), GTI.getIndexedType())) {
1336               case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1337               case 1:  return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1338               case -2: return ICmpInst::BAD_ICMP_PREDICATE;
1339               }
1340
1341             // Ok, we ran out of things they have in common.  If any leftovers
1342             // are non-zero then we have a difference, otherwise we are equal.
1343             for (; i < CE1->getNumOperands(); ++i)
1344               if (!CE1->getOperand(i)->isNullValue()) {
1345                 if (isa<ConstantInt>(CE1->getOperand(i)))
1346                   return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1347                 else
1348                   return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1349               }
1350
1351             for (; i < CE2->getNumOperands(); ++i)
1352               if (!CE2->getOperand(i)->isNullValue()) {
1353                 if (isa<ConstantInt>(CE2->getOperand(i)))
1354                   return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1355                 else
1356                   return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1357               }
1358             return ICmpInst::ICMP_EQ;
1359           }
1360         }
1361       }
1362     default:
1363       break;
1364     }
1365   }
1366
1367   return ICmpInst::BAD_ICMP_PREDICATE;
1368 }
1369
1370 Constant *llvm::ConstantFoldCompareInstruction(LLVMContext &Context,
1371                                                unsigned short pred, 
1372                                                const Constant *C1, 
1373                                                const Constant *C2) {
1374   const Type *ResultTy;
1375   if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
1376     ResultTy = Context.getVectorType(Type::Int1Ty, VT->getNumElements());
1377   else
1378     ResultTy = Type::Int1Ty;
1379
1380   // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
1381   if (pred == FCmpInst::FCMP_FALSE)
1382     return Context.getNullValue(ResultTy);
1383
1384   if (pred == FCmpInst::FCMP_TRUE)
1385     return Context.getAllOnesValue(ResultTy);
1386
1387   // Handle some degenerate cases first
1388   if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
1389     return Context.getUndef(ResultTy);
1390
1391   // No compile-time operations on this type yet.
1392   if (C1->getType() == Type::PPC_FP128Ty)
1393     return 0;
1394
1395   // icmp eq/ne(null,GV) -> false/true
1396   if (C1->isNullValue()) {
1397     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1398       // Don't try to evaluate aliases.  External weak GV can be null.
1399       if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
1400         if (pred == ICmpInst::ICMP_EQ)
1401           return Context.getFalse();
1402         else if (pred == ICmpInst::ICMP_NE)
1403           return Context.getTrue();
1404       }
1405   // icmp eq/ne(GV,null) -> false/true
1406   } else if (C2->isNullValue()) {
1407     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1408       // Don't try to evaluate aliases.  External weak GV can be null.
1409       if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
1410         if (pred == ICmpInst::ICMP_EQ)
1411           return Context.getFalse();
1412         else if (pred == ICmpInst::ICMP_NE)
1413           return Context.getTrue();
1414       }
1415   }
1416
1417   if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1418     APInt V1 = cast<ConstantInt>(C1)->getValue();
1419     APInt V2 = cast<ConstantInt>(C2)->getValue();
1420     switch (pred) {
1421     default: llvm_unreachable("Invalid ICmp Predicate"); return 0;
1422     case ICmpInst::ICMP_EQ:
1423       return Context.getConstantInt(Type::Int1Ty, V1 == V2);
1424     case ICmpInst::ICMP_NE: 
1425       return Context.getConstantInt(Type::Int1Ty, V1 != V2);
1426     case ICmpInst::ICMP_SLT:
1427       return Context.getConstantInt(Type::Int1Ty, V1.slt(V2));
1428     case ICmpInst::ICMP_SGT:
1429       return Context.getConstantInt(Type::Int1Ty, V1.sgt(V2));
1430     case ICmpInst::ICMP_SLE:
1431       return Context.getConstantInt(Type::Int1Ty, V1.sle(V2));
1432     case ICmpInst::ICMP_SGE:
1433       return Context.getConstantInt(Type::Int1Ty, V1.sge(V2));
1434     case ICmpInst::ICMP_ULT:
1435       return Context.getConstantInt(Type::Int1Ty, V1.ult(V2));
1436     case ICmpInst::ICMP_UGT:
1437       return Context.getConstantInt(Type::Int1Ty, V1.ugt(V2));
1438     case ICmpInst::ICMP_ULE:
1439       return Context.getConstantInt(Type::Int1Ty, V1.ule(V2));
1440     case ICmpInst::ICMP_UGE:
1441       return Context.getConstantInt(Type::Int1Ty, V1.uge(V2));
1442     }
1443   } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1444     APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
1445     APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
1446     APFloat::cmpResult R = C1V.compare(C2V);
1447     switch (pred) {
1448     default: llvm_unreachable("Invalid FCmp Predicate"); return 0;
1449     case FCmpInst::FCMP_FALSE: return Context.getFalse();
1450     case FCmpInst::FCMP_TRUE:  return Context.getTrue();
1451     case FCmpInst::FCMP_UNO:
1452       return Context.getConstantInt(Type::Int1Ty, R==APFloat::cmpUnordered);
1453     case FCmpInst::FCMP_ORD:
1454       return Context.getConstantInt(Type::Int1Ty, R!=APFloat::cmpUnordered);
1455     case FCmpInst::FCMP_UEQ:
1456       return Context.getConstantInt(Type::Int1Ty, R==APFloat::cmpUnordered ||
1457                                             R==APFloat::cmpEqual);
1458     case FCmpInst::FCMP_OEQ:   
1459       return Context.getConstantInt(Type::Int1Ty, R==APFloat::cmpEqual);
1460     case FCmpInst::FCMP_UNE:
1461       return Context.getConstantInt(Type::Int1Ty, R!=APFloat::cmpEqual);
1462     case FCmpInst::FCMP_ONE:   
1463       return Context.getConstantInt(Type::Int1Ty, R==APFloat::cmpLessThan ||
1464                                             R==APFloat::cmpGreaterThan);
1465     case FCmpInst::FCMP_ULT: 
1466       return Context.getConstantInt(Type::Int1Ty, R==APFloat::cmpUnordered ||
1467                                             R==APFloat::cmpLessThan);
1468     case FCmpInst::FCMP_OLT:   
1469       return Context.getConstantInt(Type::Int1Ty, R==APFloat::cmpLessThan);
1470     case FCmpInst::FCMP_UGT:
1471       return Context.getConstantInt(Type::Int1Ty, R==APFloat::cmpUnordered ||
1472                                             R==APFloat::cmpGreaterThan);
1473     case FCmpInst::FCMP_OGT:
1474       return Context.getConstantInt(Type::Int1Ty, R==APFloat::cmpGreaterThan);
1475     case FCmpInst::FCMP_ULE:
1476       return Context.getConstantInt(Type::Int1Ty, R!=APFloat::cmpGreaterThan);
1477     case FCmpInst::FCMP_OLE: 
1478       return Context.getConstantInt(Type::Int1Ty, R==APFloat::cmpLessThan ||
1479                                             R==APFloat::cmpEqual);
1480     case FCmpInst::FCMP_UGE:
1481       return Context.getConstantInt(Type::Int1Ty, R!=APFloat::cmpLessThan);
1482     case FCmpInst::FCMP_OGE: 
1483       return Context.getConstantInt(Type::Int1Ty, R==APFloat::cmpGreaterThan ||
1484                                             R==APFloat::cmpEqual);
1485     }
1486   } else if (isa<VectorType>(C1->getType())) {
1487     SmallVector<Constant*, 16> C1Elts, C2Elts;
1488     C1->getVectorElements(Context, C1Elts);
1489     C2->getVectorElements(Context, C2Elts);
1490     
1491     // If we can constant fold the comparison of each element, constant fold
1492     // the whole vector comparison.
1493     SmallVector<Constant*, 4> ResElts;
1494     for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) {
1495       // Compare the elements, producing an i1 result or constant expr.
1496       ResElts.push_back(
1497                     Context.getConstantExprCompare(pred, C1Elts[i], C2Elts[i]));
1498     }
1499     return Context.getConstantVector(&ResElts[0], ResElts.size());
1500   }
1501
1502   if (C1->getType()->isFloatingPoint()) {
1503     int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
1504     switch (evaluateFCmpRelation(Context, C1, C2)) {
1505     default: llvm_unreachable("Unknown relation!");
1506     case FCmpInst::FCMP_UNO:
1507     case FCmpInst::FCMP_ORD:
1508     case FCmpInst::FCMP_UEQ:
1509     case FCmpInst::FCMP_UNE:
1510     case FCmpInst::FCMP_ULT:
1511     case FCmpInst::FCMP_UGT:
1512     case FCmpInst::FCMP_ULE:
1513     case FCmpInst::FCMP_UGE:
1514     case FCmpInst::FCMP_TRUE:
1515     case FCmpInst::FCMP_FALSE:
1516     case FCmpInst::BAD_FCMP_PREDICATE:
1517       break; // Couldn't determine anything about these constants.
1518     case FCmpInst::FCMP_OEQ: // We know that C1 == C2
1519       Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1520                 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1521                 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1522       break;
1523     case FCmpInst::FCMP_OLT: // We know that C1 < C2
1524       Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1525                 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1526                 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1527       break;
1528     case FCmpInst::FCMP_OGT: // We know that C1 > C2
1529       Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1530                 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1531                 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1532       break;
1533     case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1534       // We can only partially decide this relation.
1535       if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) 
1536         Result = 0;
1537       else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) 
1538         Result = 1;
1539       break;
1540     case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1541       // We can only partially decide this relation.
1542       if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) 
1543         Result = 0;
1544       else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) 
1545         Result = 1;
1546       break;
1547     case ICmpInst::ICMP_NE: // We know that C1 != C2
1548       // We can only partially decide this relation.
1549       if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) 
1550         Result = 0;
1551       else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE) 
1552         Result = 1;
1553       break;
1554     }
1555     
1556     // If we evaluated the result, return it now.
1557     if (Result != -1)
1558       return Context.getConstantInt(Type::Int1Ty, Result);
1559
1560   } else {
1561     // Evaluate the relation between the two constants, per the predicate.
1562     int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
1563     switch (evaluateICmpRelation(Context, C1, C2, CmpInst::isSigned(pred))) {
1564     default: llvm_unreachable("Unknown relational!");
1565     case ICmpInst::BAD_ICMP_PREDICATE:
1566       break;  // Couldn't determine anything about these constants.
1567     case ICmpInst::ICMP_EQ:   // We know the constants are equal!
1568       // If we know the constants are equal, we can decide the result of this
1569       // computation precisely.
1570       Result = (pred == ICmpInst::ICMP_EQ  ||
1571                 pred == ICmpInst::ICMP_ULE ||
1572                 pred == ICmpInst::ICMP_SLE ||
1573                 pred == ICmpInst::ICMP_UGE ||
1574                 pred == ICmpInst::ICMP_SGE);
1575       break;
1576     case ICmpInst::ICMP_ULT:
1577       // If we know that C1 < C2, we can decide the result of this computation
1578       // precisely.
1579       Result = (pred == ICmpInst::ICMP_ULT ||
1580                 pred == ICmpInst::ICMP_NE  ||
1581                 pred == ICmpInst::ICMP_ULE);
1582       break;
1583     case ICmpInst::ICMP_SLT:
1584       // If we know that C1 < C2, we can decide the result of this computation
1585       // precisely.
1586       Result = (pred == ICmpInst::ICMP_SLT ||
1587                 pred == ICmpInst::ICMP_NE  ||
1588                 pred == ICmpInst::ICMP_SLE);
1589       break;
1590     case ICmpInst::ICMP_UGT:
1591       // If we know that C1 > C2, we can decide the result of this computation
1592       // precisely.
1593       Result = (pred == ICmpInst::ICMP_UGT ||
1594                 pred == ICmpInst::ICMP_NE  ||
1595                 pred == ICmpInst::ICMP_UGE);
1596       break;
1597     case ICmpInst::ICMP_SGT:
1598       // If we know that C1 > C2, we can decide the result of this computation
1599       // precisely.
1600       Result = (pred == ICmpInst::ICMP_SGT ||
1601                 pred == ICmpInst::ICMP_NE  ||
1602                 pred == ICmpInst::ICMP_SGE);
1603       break;
1604     case ICmpInst::ICMP_ULE:
1605       // If we know that C1 <= C2, we can only partially decide this relation.
1606       if (pred == ICmpInst::ICMP_UGT) Result = 0;
1607       if (pred == ICmpInst::ICMP_ULT) Result = 1;
1608       break;
1609     case ICmpInst::ICMP_SLE:
1610       // If we know that C1 <= C2, we can only partially decide this relation.
1611       if (pred == ICmpInst::ICMP_SGT) Result = 0;
1612       if (pred == ICmpInst::ICMP_SLT) Result = 1;
1613       break;
1614
1615     case ICmpInst::ICMP_UGE:
1616       // If we know that C1 >= C2, we can only partially decide this relation.
1617       if (pred == ICmpInst::ICMP_ULT) Result = 0;
1618       if (pred == ICmpInst::ICMP_UGT) Result = 1;
1619       break;
1620     case ICmpInst::ICMP_SGE:
1621       // If we know that C1 >= C2, we can only partially decide this relation.
1622       if (pred == ICmpInst::ICMP_SLT) Result = 0;
1623       if (pred == ICmpInst::ICMP_SGT) Result = 1;
1624       break;
1625
1626     case ICmpInst::ICMP_NE:
1627       // If we know that C1 != C2, we can only partially decide this relation.
1628       if (pred == ICmpInst::ICMP_EQ) Result = 0;
1629       if (pred == ICmpInst::ICMP_NE) Result = 1;
1630       break;
1631     }
1632     
1633     // If we evaluated the result, return it now.
1634     if (Result != -1)
1635       return Context.getConstantInt(Type::Int1Ty, Result);
1636     
1637     if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
1638       // If C2 is a constant expr and C1 isn't, flip them around and fold the
1639       // other way if possible.
1640       switch (pred) {
1641       case ICmpInst::ICMP_EQ:
1642       case ICmpInst::ICMP_NE:
1643         // No change of predicate required.
1644         return ConstantFoldCompareInstruction(Context, pred, C2, C1);
1645
1646       case ICmpInst::ICMP_ULT:
1647       case ICmpInst::ICMP_SLT:
1648       case ICmpInst::ICMP_UGT:
1649       case ICmpInst::ICMP_SGT:
1650       case ICmpInst::ICMP_ULE:
1651       case ICmpInst::ICMP_SLE:
1652       case ICmpInst::ICMP_UGE:
1653       case ICmpInst::ICMP_SGE:
1654         // Change the predicate as necessary to swap the operands.
1655         pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
1656         return ConstantFoldCompareInstruction(Context, pred, C2, C1);
1657
1658       default:  // These predicates cannot be flopped around.
1659         break;
1660       }
1661     }
1662   }
1663   return 0;
1664   }
1665
1666 Constant *llvm::ConstantFoldGetElementPtr(LLVMContext &Context, 
1667                                           const Constant *C,
1668                                           Constant* const *Idxs,
1669                                           unsigned NumIdx) {
1670   if (NumIdx == 0 ||
1671       (NumIdx == 1 && Idxs[0]->isNullValue()))
1672     return const_cast<Constant*>(C);
1673
1674   if (isa<UndefValue>(C)) {
1675     const PointerType *Ptr = cast<PointerType>(C->getType());
1676     const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
1677                                                        (Value **)Idxs,
1678                                                        (Value **)Idxs+NumIdx);
1679     assert(Ty != 0 && "Invalid indices for GEP!");
1680     return Context.getUndef(Context.getPointerType(Ty, Ptr->getAddressSpace()));
1681   }
1682
1683   Constant *Idx0 = Idxs[0];
1684   if (C->isNullValue()) {
1685     bool isNull = true;
1686     for (unsigned i = 0, e = NumIdx; i != e; ++i)
1687       if (!Idxs[i]->isNullValue()) {
1688         isNull = false;
1689         break;
1690       }
1691     if (isNull) {
1692       const PointerType *Ptr = cast<PointerType>(C->getType());
1693       const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
1694                                                          (Value**)Idxs,
1695                                                          (Value**)Idxs+NumIdx);
1696       assert(Ty != 0 && "Invalid indices for GEP!");
1697       return  Context.getConstantPointerNull(
1698                             Context.getPointerType(Ty,Ptr->getAddressSpace()));
1699     }
1700   }
1701
1702   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1703     // Combine Indices - If the source pointer to this getelementptr instruction
1704     // is a getelementptr instruction, combine the indices of the two
1705     // getelementptr instructions into a single instruction.
1706     //
1707     if (CE->getOpcode() == Instruction::GetElementPtr) {
1708       const Type *LastTy = 0;
1709       for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1710            I != E; ++I)
1711         LastTy = *I;
1712
1713       if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1714         SmallVector<Value*, 16> NewIndices;
1715         NewIndices.reserve(NumIdx + CE->getNumOperands());
1716         for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
1717           NewIndices.push_back(CE->getOperand(i));
1718
1719         // Add the last index of the source with the first index of the new GEP.
1720         // Make sure to handle the case when they are actually different types.
1721         Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
1722         // Otherwise it must be an array.
1723         if (!Idx0->isNullValue()) {
1724           const Type *IdxTy = Combined->getType();
1725           if (IdxTy != Idx0->getType()) {
1726             Constant *C1 =
1727               Context.getConstantExprSExtOrBitCast(Idx0, Type::Int64Ty);
1728             Constant *C2 = Context.getConstantExprSExtOrBitCast(Combined, 
1729                                                           Type::Int64Ty);
1730             Combined = Context.getConstantExpr(Instruction::Add, C1, C2);
1731           } else {
1732             Combined =
1733               Context.getConstantExpr(Instruction::Add, Idx0, Combined);
1734           }
1735         }
1736
1737         NewIndices.push_back(Combined);
1738         NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
1739         return Context.getConstantExprGetElementPtr(CE->getOperand(0),
1740                                               &NewIndices[0],
1741                                               NewIndices.size());
1742       }
1743     }
1744
1745     // Implement folding of:
1746     //    int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1747     //                        long 0, long 0)
1748     // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1749     //
1750     if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) {
1751       if (const PointerType *SPT =
1752           dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1753         if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1754           if (const ArrayType *CAT =
1755         dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
1756             if (CAT->getElementType() == SAT->getElementType())
1757               return Context.getConstantExprGetElementPtr(
1758                       (Constant*)CE->getOperand(0), Idxs, NumIdx);
1759     }
1760     
1761     // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1)
1762     // Into: inttoptr (i64 0 to i8*)
1763     // This happens with pointers to member functions in C++.
1764     if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 &&
1765         isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) &&
1766         cast<PointerType>(CE->getType())->getElementType() == Type::Int8Ty) {
1767       Constant *Base = CE->getOperand(0);
1768       Constant *Offset = Idxs[0];
1769       
1770       // Convert the smaller integer to the larger type.
1771       if (Offset->getType()->getPrimitiveSizeInBits() < 
1772           Base->getType()->getPrimitiveSizeInBits())
1773         Offset = Context.getConstantExprSExt(Offset, Base->getType());
1774       else if (Base->getType()->getPrimitiveSizeInBits() <
1775                Offset->getType()->getPrimitiveSizeInBits())
1776         Base = Context.getConstantExprZExt(Base, Offset->getType());
1777       
1778       Base = Context.getConstantExprAdd(Base, Offset);
1779       return Context.getConstantExprIntToPtr(Base, CE->getType());
1780     }
1781   }
1782   return 0;
1783 }
1784