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