Home | History | Annotate | Download | only in VMCore
      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/Operator.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(ConstantVector *CV,
     45                                        VectorType *DstTy) {
     46 
     47   if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
     48   if (CV->isNullValue()) return Constant::getNullValue(DstTy);
     49 
     50   // If this cast changes element count then we can't handle it here:
     51   // doing so requires endianness information.  This should be handled by
     52   // Analysis/ConstantFolding.cpp
     53   unsigned NumElts = DstTy->getNumElements();
     54   if (NumElts != CV->getNumOperands())
     55     return 0;
     56 
     57   // Check to verify that all elements of the input are simple.
     58   for (unsigned i = 0; i != NumElts; ++i) {
     59     if (!isa<ConstantInt>(CV->getOperand(i)) &&
     60         !isa<ConstantFP>(CV->getOperand(i)))
     61       return 0;
     62   }
     63 
     64   // Bitcast each element now.
     65   std::vector<Constant*> Result;
     66   Type *DstEltTy = DstTy->getElementType();
     67   for (unsigned i = 0; i != NumElts; ++i)
     68     Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i),
     69                                                     DstEltTy));
     70   return ConstantVector::get(Result);
     71 }
     72 
     73 /// This function determines which opcode to use to fold two constant cast
     74 /// expressions together. It uses CastInst::isEliminableCastPair to determine
     75 /// the opcode. Consequently its just a wrapper around that function.
     76 /// @brief Determine if it is valid to fold a cast of a cast
     77 static unsigned
     78 foldConstantCastPair(
     79   unsigned opc,          ///< opcode of the second cast constant expression
     80   ConstantExpr *Op,      ///< the first cast constant expression
     81   Type *DstTy      ///< desintation type of the first cast
     82 ) {
     83   assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
     84   assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
     85   assert(CastInst::isCast(opc) && "Invalid cast opcode");
     86 
     87   // The the types and opcodes for the two Cast constant expressions
     88   Type *SrcTy = Op->getOperand(0)->getType();
     89   Type *MidTy = Op->getType();
     90   Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
     91   Instruction::CastOps secondOp = Instruction::CastOps(opc);
     92 
     93   // Let CastInst::isEliminableCastPair do the heavy lifting.
     94   return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
     95                                         Type::getInt64Ty(DstTy->getContext()));
     96 }
     97 
     98 static Constant *FoldBitCast(Constant *V, Type *DestTy) {
     99   Type *SrcTy = V->getType();
    100   if (SrcTy == DestTy)
    101     return V; // no-op cast
    102 
    103   // Check to see if we are casting a pointer to an aggregate to a pointer to
    104   // the first element.  If so, return the appropriate GEP instruction.
    105   if (PointerType *PTy = dyn_cast<PointerType>(V->getType()))
    106     if (PointerType *DPTy = dyn_cast<PointerType>(DestTy))
    107       if (PTy->getAddressSpace() == DPTy->getAddressSpace()) {
    108         SmallVector<Value*, 8> IdxList;
    109         Value *Zero =
    110           Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));
    111         IdxList.push_back(Zero);
    112         Type *ElTy = PTy->getElementType();
    113         while (ElTy != DPTy->getElementType()) {
    114           if (StructType *STy = dyn_cast<StructType>(ElTy)) {
    115             if (STy->getNumElements() == 0) break;
    116             ElTy = STy->getElementType(0);
    117             IdxList.push_back(Zero);
    118           } else if (SequentialType *STy =
    119                      dyn_cast<SequentialType>(ElTy)) {
    120             if (ElTy->isPointerTy()) break;  // Can't index into pointers!
    121             ElTy = STy->getElementType();
    122             IdxList.push_back(Zero);
    123           } else {
    124             break;
    125           }
    126         }
    127 
    128         if (ElTy == DPTy->getElementType())
    129           // This GEP is inbounds because all indices are zero.
    130           return ConstantExpr::getInBoundsGetElementPtr(V, &IdxList[0],
    131                                                         IdxList.size());
    132       }
    133 
    134   // Handle casts from one vector constant to another.  We know that the src
    135   // and dest type have the same size (otherwise its an illegal cast).
    136   if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
    137     if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
    138       assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
    139              "Not cast between same sized vectors!");
    140       SrcTy = NULL;
    141       // First, check for null.  Undef is already handled.
    142       if (isa<ConstantAggregateZero>(V))
    143         return Constant::getNullValue(DestTy);
    144 
    145       if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
    146         return BitCastConstantVector(CV, DestPTy);
    147     }
    148 
    149     // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
    150     // This allows for other simplifications (although some of them
    151     // can only be handled by Analysis/ConstantFolding.cpp).
    152     if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
    153       return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy);
    154   }
    155 
    156   // Finally, implement bitcast folding now.   The code below doesn't handle
    157   // bitcast right.
    158   if (isa<ConstantPointerNull>(V))  // ptr->ptr cast.
    159     return ConstantPointerNull::get(cast<PointerType>(DestTy));
    160 
    161   // Handle integral constant input.
    162   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
    163     if (DestTy->isIntegerTy())
    164       // Integral -> Integral. This is a no-op because the bit widths must
    165       // be the same. Consequently, we just fold to V.
    166       return V;
    167 
    168     if (DestTy->isFloatingPointTy())
    169       return ConstantFP::get(DestTy->getContext(),
    170                              APFloat(CI->getValue(),
    171                                      !DestTy->isPPC_FP128Ty()));
    172 
    173     // Otherwise, can't fold this (vector?)
    174     return 0;
    175   }
    176 
    177   // Handle ConstantFP input: FP -> Integral.
    178   if (ConstantFP *FP = dyn_cast<ConstantFP>(V))
    179     return ConstantInt::get(FP->getContext(),
    180                             FP->getValueAPF().bitcastToAPInt());
    181 
    182   return 0;
    183 }
    184 
    185 
    186 /// ExtractConstantBytes - V is an integer constant which only has a subset of
    187 /// its bytes used.  The bytes used are indicated by ByteStart (which is the
    188 /// first byte used, counting from the least significant byte) and ByteSize,
    189 /// which is the number of bytes used.
    190 ///
    191 /// This function analyzes the specified constant to see if the specified byte
    192 /// range can be returned as a simplified constant.  If so, the constant is
    193 /// returned, otherwise null is returned.
    194 ///
    195 static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
    196                                       unsigned ByteSize) {
    197   assert(C->getType()->isIntegerTy() &&
    198          (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
    199          "Non-byte sized integer input");
    200   unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
    201   assert(ByteSize && "Must be accessing some piece");
    202   assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
    203   assert(ByteSize != CSize && "Should not extract everything");
    204 
    205   // Constant Integers are simple.
    206   if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
    207     APInt V = CI->getValue();
    208     if (ByteStart)
    209       V = V.lshr(ByteStart*8);
    210     V = V.trunc(ByteSize*8);
    211     return ConstantInt::get(CI->getContext(), V);
    212   }
    213 
    214   // In the input is a constant expr, we might be able to recursively simplify.
    215   // If not, we definitely can't do anything.
    216   ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
    217   if (CE == 0) return 0;
    218 
    219   switch (CE->getOpcode()) {
    220   default: return 0;
    221   case Instruction::Or: {
    222     Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
    223     if (RHS == 0)
    224       return 0;
    225 
    226     // X | -1 -> -1.
    227     if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS))
    228       if (RHSC->isAllOnesValue())
    229         return RHSC;
    230 
    231     Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
    232     if (LHS == 0)
    233       return 0;
    234     return ConstantExpr::getOr(LHS, RHS);
    235   }
    236   case Instruction::And: {
    237     Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
    238     if (RHS == 0)
    239       return 0;
    240 
    241     // X & 0 -> 0.
    242     if (RHS->isNullValue())
    243       return RHS;
    244 
    245     Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
    246     if (LHS == 0)
    247       return 0;
    248     return ConstantExpr::getAnd(LHS, RHS);
    249   }
    250   case Instruction::LShr: {
    251     ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
    252     if (Amt == 0)
    253       return 0;
    254     unsigned ShAmt = Amt->getZExtValue();
    255     // Cannot analyze non-byte shifts.
    256     if ((ShAmt & 7) != 0)
    257       return 0;
    258     ShAmt >>= 3;
    259 
    260     // If the extract is known to be all zeros, return zero.
    261     if (ByteStart >= CSize-ShAmt)
    262       return Constant::getNullValue(IntegerType::get(CE->getContext(),
    263                                                      ByteSize*8));
    264     // If the extract is known to be fully in the input, extract it.
    265     if (ByteStart+ByteSize+ShAmt <= CSize)
    266       return ExtractConstantBytes(CE->getOperand(0), ByteStart+ShAmt, ByteSize);
    267 
    268     // TODO: Handle the 'partially zero' case.
    269     return 0;
    270   }
    271 
    272   case Instruction::Shl: {
    273     ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
    274     if (Amt == 0)
    275       return 0;
    276     unsigned ShAmt = Amt->getZExtValue();
    277     // Cannot analyze non-byte shifts.
    278     if ((ShAmt & 7) != 0)
    279       return 0;
    280     ShAmt >>= 3;
    281 
    282     // If the extract is known to be all zeros, return zero.
    283     if (ByteStart+ByteSize <= ShAmt)
    284       return Constant::getNullValue(IntegerType::get(CE->getContext(),
    285                                                      ByteSize*8));
    286     // If the extract is known to be fully in the input, extract it.
    287     if (ByteStart >= ShAmt)
    288       return ExtractConstantBytes(CE->getOperand(0), ByteStart-ShAmt, ByteSize);
    289 
    290     // TODO: Handle the 'partially zero' case.
    291     return 0;
    292   }
    293 
    294   case Instruction::ZExt: {
    295     unsigned SrcBitSize =
    296       cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth();
    297 
    298     // If extracting something that is completely zero, return 0.
    299     if (ByteStart*8 >= SrcBitSize)
    300       return Constant::getNullValue(IntegerType::get(CE->getContext(),
    301                                                      ByteSize*8));
    302 
    303     // If exactly extracting the input, return it.
    304     if (ByteStart == 0 && ByteSize*8 == SrcBitSize)
    305       return CE->getOperand(0);
    306 
    307     // If extracting something completely in the input, if if the input is a
    308     // multiple of 8 bits, recurse.
    309     if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize)
    310       return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize);
    311 
    312     // Otherwise, if extracting a subset of the input, which is not multiple of
    313     // 8 bits, do a shift and trunc to get the bits.
    314     if ((ByteStart+ByteSize)*8 < SrcBitSize) {
    315       assert((SrcBitSize&7) && "Shouldn't get byte sized case here");
    316       Constant *Res = CE->getOperand(0);
    317       if (ByteStart)
    318         Res = ConstantExpr::getLShr(Res,
    319                                  ConstantInt::get(Res->getType(), ByteStart*8));
    320       return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(),
    321                                                           ByteSize*8));
    322     }
    323 
    324     // TODO: Handle the 'partially zero' case.
    325     return 0;
    326   }
    327   }
    328 }
    329 
    330 /// getFoldedSizeOf - Return a ConstantExpr with type DestTy for sizeof
    331 /// on Ty, with any known factors factored out. If Folded is false,
    332 /// return null if no factoring was possible, to avoid endlessly
    333 /// bouncing an unfoldable expression back into the top-level folder.
    334 ///
    335 static Constant *getFoldedSizeOf(Type *Ty, Type *DestTy,
    336                                  bool Folded) {
    337   if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
    338     Constant *N = ConstantInt::get(DestTy, ATy->getNumElements());
    339     Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
    340     return ConstantExpr::getNUWMul(E, N);
    341   }
    342 
    343   if (StructType *STy = dyn_cast<StructType>(Ty))
    344     if (!STy->isPacked()) {
    345       unsigned NumElems = STy->getNumElements();
    346       // An empty struct has size zero.
    347       if (NumElems == 0)
    348         return ConstantExpr::getNullValue(DestTy);
    349       // Check for a struct with all members having the same size.
    350       Constant *MemberSize =
    351         getFoldedSizeOf(STy->getElementType(0), DestTy, true);
    352       bool AllSame = true;
    353       for (unsigned i = 1; i != NumElems; ++i)
    354         if (MemberSize !=
    355             getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
    356           AllSame = false;
    357           break;
    358         }
    359       if (AllSame) {
    360         Constant *N = ConstantInt::get(DestTy, NumElems);
    361         return ConstantExpr::getNUWMul(MemberSize, N);
    362       }
    363     }
    364 
    365   // Pointer size doesn't depend on the pointee type, so canonicalize them
    366   // to an arbitrary pointee.
    367   if (PointerType *PTy = dyn_cast<PointerType>(Ty))
    368     if (!PTy->getElementType()->isIntegerTy(1))
    369       return
    370         getFoldedSizeOf(PointerType::get(IntegerType::get(PTy->getContext(), 1),
    371                                          PTy->getAddressSpace()),
    372                         DestTy, true);
    373 
    374   // If there's no interesting folding happening, bail so that we don't create
    375   // a constant that looks like it needs folding but really doesn't.
    376   if (!Folded)
    377     return 0;
    378 
    379   // Base case: Get a regular sizeof expression.
    380   Constant *C = ConstantExpr::getSizeOf(Ty);
    381   C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
    382                                                     DestTy, false),
    383                             C, DestTy);
    384   return C;
    385 }
    386 
    387 /// getFoldedAlignOf - Return a ConstantExpr with type DestTy for alignof
    388 /// on Ty, with any known factors factored out. If Folded is false,
    389 /// return null if no factoring was possible, to avoid endlessly
    390 /// bouncing an unfoldable expression back into the top-level folder.
    391 ///
    392 static Constant *getFoldedAlignOf(Type *Ty, Type *DestTy,
    393                                   bool Folded) {
    394   // The alignment of an array is equal to the alignment of the
    395   // array element. Note that this is not always true for vectors.
    396   if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
    397     Constant *C = ConstantExpr::getAlignOf(ATy->getElementType());
    398     C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
    399                                                       DestTy,
    400                                                       false),
    401                               C, DestTy);
    402     return C;
    403   }
    404 
    405   if (StructType *STy = dyn_cast<StructType>(Ty)) {
    406     // Packed structs always have an alignment of 1.
    407     if (STy->isPacked())
    408       return ConstantInt::get(DestTy, 1);
    409 
    410     // Otherwise, struct alignment is the maximum alignment of any member.
    411     // Without target data, we can't compare much, but we can check to see
    412     // if all the members have the same alignment.
    413     unsigned NumElems = STy->getNumElements();
    414     // An empty struct has minimal alignment.
    415     if (NumElems == 0)
    416       return ConstantInt::get(DestTy, 1);
    417     // Check for a struct with all members having the same alignment.
    418     Constant *MemberAlign =
    419       getFoldedAlignOf(STy->getElementType(0), DestTy, true);
    420     bool AllSame = true;
    421     for (unsigned i = 1; i != NumElems; ++i)
    422       if (MemberAlign != getFoldedAlignOf(STy->getElementType(i), DestTy, true)) {
    423         AllSame = false;
    424         break;
    425       }
    426     if (AllSame)
    427       return MemberAlign;
    428   }
    429 
    430   // Pointer alignment doesn't depend on the pointee type, so canonicalize them
    431   // to an arbitrary pointee.
    432   if (PointerType *PTy = dyn_cast<PointerType>(Ty))
    433     if (!PTy->getElementType()->isIntegerTy(1))
    434       return
    435         getFoldedAlignOf(PointerType::get(IntegerType::get(PTy->getContext(),
    436                                                            1),
    437                                           PTy->getAddressSpace()),
    438                          DestTy, true);
    439 
    440   // If there's no interesting folding happening, bail so that we don't create
    441   // a constant that looks like it needs folding but really doesn't.
    442   if (!Folded)
    443     return 0;
    444 
    445   // Base case: Get a regular alignof expression.
    446   Constant *C = ConstantExpr::getAlignOf(Ty);
    447   C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
    448                                                     DestTy, false),
    449                             C, DestTy);
    450   return C;
    451 }
    452 
    453 /// getFoldedOffsetOf - Return a ConstantExpr with type DestTy for offsetof
    454 /// on Ty and FieldNo, with any known factors factored out. If Folded is false,
    455 /// return null if no factoring was possible, to avoid endlessly
    456 /// bouncing an unfoldable expression back into the top-level folder.
    457 ///
    458 static Constant *getFoldedOffsetOf(Type *Ty, Constant *FieldNo,
    459                                    Type *DestTy,
    460                                    bool Folded) {
    461   if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
    462     Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, false,
    463                                                                 DestTy, false),
    464                                         FieldNo, DestTy);
    465     Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
    466     return ConstantExpr::getNUWMul(E, N);
    467   }
    468 
    469   if (StructType *STy = dyn_cast<StructType>(Ty))
    470     if (!STy->isPacked()) {
    471       unsigned NumElems = STy->getNumElements();
    472       // An empty struct has no members.
    473       if (NumElems == 0)
    474         return 0;
    475       // Check for a struct with all members having the same size.
    476       Constant *MemberSize =
    477         getFoldedSizeOf(STy->getElementType(0), DestTy, true);
    478       bool AllSame = true;
    479       for (unsigned i = 1; i != NumElems; ++i)
    480         if (MemberSize !=
    481             getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
    482           AllSame = false;
    483           break;
    484         }
    485       if (AllSame) {
    486         Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo,
    487                                                                     false,
    488                                                                     DestTy,
    489                                                                     false),
    490                                             FieldNo, DestTy);
    491         return ConstantExpr::getNUWMul(MemberSize, N);
    492       }
    493     }
    494 
    495   // If there's no interesting folding happening, bail so that we don't create
    496   // a constant that looks like it needs folding but really doesn't.
    497   if (!Folded)
    498     return 0;
    499 
    500   // Base case: Get a regular offsetof expression.
    501   Constant *C = ConstantExpr::getOffsetOf(Ty, FieldNo);
    502   C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
    503                                                     DestTy, false),
    504                             C, DestTy);
    505   return C;
    506 }
    507 
    508 Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
    509                                             Type *DestTy) {
    510   if (isa<UndefValue>(V)) {
    511     // zext(undef) = 0, because the top bits will be zero.
    512     // sext(undef) = 0, because the top bits will all be the same.
    513     // [us]itofp(undef) = 0, because the result value is bounded.
    514     if (opc == Instruction::ZExt || opc == Instruction::SExt ||
    515         opc == Instruction::UIToFP || opc == Instruction::SIToFP)
    516       return Constant::getNullValue(DestTy);
    517     return UndefValue::get(DestTy);
    518   }
    519 
    520   // No compile-time operations on this type yet.
    521   if (V->getType()->isPPC_FP128Ty() || DestTy->isPPC_FP128Ty())
    522     return 0;
    523 
    524   if (V->isNullValue() && !DestTy->isX86_MMXTy())
    525     return Constant::getNullValue(DestTy);
    526 
    527   // If the cast operand is a constant expression, there's a few things we can
    528   // do to try to simplify it.
    529   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
    530     if (CE->isCast()) {
    531       // Try hard to fold cast of cast because they are often eliminable.
    532       if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
    533         return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
    534     } else if (CE->getOpcode() == Instruction::GetElementPtr) {
    535       // If all of the indexes in the GEP are null values, there is no pointer
    536       // adjustment going on.  We might as well cast the source pointer.
    537       bool isAllNull = true;
    538       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
    539         if (!CE->getOperand(i)->isNullValue()) {
    540           isAllNull = false;
    541           break;
    542         }
    543       if (isAllNull)
    544         // This is casting one pointer type to another, always BitCast
    545         return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
    546     }
    547   }
    548 
    549   // If the cast operand is a constant vector, perform the cast by
    550   // operating on each element. In the cast of bitcasts, the element
    551   // count may be mismatched; don't attempt to handle that here.
    552   if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
    553     if (DestTy->isVectorTy() &&
    554         cast<VectorType>(DestTy)->getNumElements() ==
    555         CV->getType()->getNumElements()) {
    556       std::vector<Constant*> res;
    557       VectorType *DestVecTy = cast<VectorType>(DestTy);
    558       Type *DstEltTy = DestVecTy->getElementType();
    559       for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
    560         res.push_back(ConstantExpr::getCast(opc,
    561                                             CV->getOperand(i), DstEltTy));
    562       return ConstantVector::get(res);
    563     }
    564 
    565   // We actually have to do a cast now. Perform the cast according to the
    566   // opcode specified.
    567   switch (opc) {
    568   default:
    569     llvm_unreachable("Failed to cast constant expression");
    570   case Instruction::FPTrunc:
    571   case Instruction::FPExt:
    572     if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
    573       bool ignored;
    574       APFloat Val = FPC->getValueAPF();
    575       Val.convert(DestTy->isFloatTy() ? APFloat::IEEEsingle :
    576                   DestTy->isDoubleTy() ? APFloat::IEEEdouble :
    577                   DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended :
    578                   DestTy->isFP128Ty() ? APFloat::IEEEquad :
    579                   APFloat::Bogus,
    580                   APFloat::rmNearestTiesToEven, &ignored);
    581       return ConstantFP::get(V->getContext(), Val);
    582     }
    583     return 0; // Can't fold.
    584   case Instruction::FPToUI:
    585   case Instruction::FPToSI:
    586     if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
    587       const APFloat &V = FPC->getValueAPF();
    588       bool ignored;
    589       uint64_t x[2];
    590       uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
    591       (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
    592                                 APFloat::rmTowardZero, &ignored);
    593       APInt Val(DestBitWidth, x);
    594       return ConstantInt::get(FPC->getContext(), Val);
    595     }
    596     return 0; // Can't fold.
    597   case Instruction::IntToPtr:   //always treated as unsigned
    598     if (V->isNullValue())       // Is it an integral null value?
    599       return ConstantPointerNull::get(cast<PointerType>(DestTy));
    600     return 0;                   // Other pointer types cannot be casted
    601   case Instruction::PtrToInt:   // always treated as unsigned
    602     // Is it a null pointer value?
    603     if (V->isNullValue())
    604       return ConstantInt::get(DestTy, 0);
    605     // If this is a sizeof-like expression, pull out multiplications by
    606     // known factors to expose them to subsequent folding. If it's an
    607     // alignof-like expression, factor out known factors.
    608     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
    609       if (CE->getOpcode() == Instruction::GetElementPtr &&
    610           CE->getOperand(0)->isNullValue()) {
    611         Type *Ty =
    612           cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
    613         if (CE->getNumOperands() == 2) {
    614           // Handle a sizeof-like expression.
    615           Constant *Idx = CE->getOperand(1);
    616           bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne();
    617           if (Constant *C = getFoldedSizeOf(Ty, DestTy, !isOne)) {
    618             Idx = ConstantExpr::getCast(CastInst::getCastOpcode(Idx, true,
    619                                                                 DestTy, false),
    620                                         Idx, DestTy);
    621             return ConstantExpr::getMul(C, Idx);
    622           }
    623         } else if (CE->getNumOperands() == 3 &&
    624                    CE->getOperand(1)->isNullValue()) {
    625           // Handle an alignof-like expression.
    626           if (StructType *STy = dyn_cast<StructType>(Ty))
    627             if (!STy->isPacked()) {
    628               ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2));
    629               if (CI->isOne() &&
    630                   STy->getNumElements() == 2 &&
    631                   STy->getElementType(0)->isIntegerTy(1)) {
    632                 return getFoldedAlignOf(STy->getElementType(1), DestTy, false);
    633               }
    634             }
    635           // Handle an offsetof-like expression.
    636           if (Ty->isStructTy() || Ty->isArrayTy()) {
    637             if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2),
    638                                                 DestTy, false))
    639               return C;
    640           }
    641         }
    642       }
    643     // Other pointer types cannot be casted
    644     return 0;
    645   case Instruction::UIToFP:
    646   case Instruction::SIToFP:
    647     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
    648       APInt api = CI->getValue();
    649       APFloat apf(APInt::getNullValue(DestTy->getPrimitiveSizeInBits()), true);
    650       (void)apf.convertFromAPInt(api,
    651                                  opc==Instruction::SIToFP,
    652                                  APFloat::rmNearestTiesToEven);
    653       return ConstantFP::get(V->getContext(), apf);
    654     }
    655     return 0;
    656   case Instruction::ZExt:
    657     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
    658       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
    659       return ConstantInt::get(V->getContext(),
    660                               CI->getValue().zext(BitWidth));
    661     }
    662     return 0;
    663   case Instruction::SExt:
    664     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
    665       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
    666       return ConstantInt::get(V->getContext(),
    667                               CI->getValue().sext(BitWidth));
    668     }
    669     return 0;
    670   case Instruction::Trunc: {
    671     uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
    672     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
    673       return ConstantInt::get(V->getContext(),
    674                               CI->getValue().trunc(DestBitWidth));
    675     }
    676 
    677     // The input must be a constantexpr.  See if we can simplify this based on
    678     // the bytes we are demanding.  Only do this if the source and dest are an
    679     // even multiple of a byte.
    680     if ((DestBitWidth & 7) == 0 &&
    681         (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
    682       if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
    683         return Res;
    684 
    685     return 0;
    686   }
    687   case Instruction::BitCast:
    688     return FoldBitCast(V, DestTy);
    689   }
    690 }
    691 
    692 Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
    693                                               Constant *V1, Constant *V2) {
    694   if (ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
    695     return CB->getZExtValue() ? V1 : V2;
    696 
    697   // Check for zero aggregate and ConstantVector of zeros
    698   if (Cond->isNullValue()) return V2;
    699 
    700   if (ConstantVector* CondV = dyn_cast<ConstantVector>(Cond)) {
    701 
    702     if (CondV->isAllOnesValue()) return V1;
    703 
    704     VectorType *VTy = cast<VectorType>(V1->getType());
    705     ConstantVector *CP1 = dyn_cast<ConstantVector>(V1);
    706     ConstantVector *CP2 = dyn_cast<ConstantVector>(V2);
    707 
    708     if ((CP1 || isa<ConstantAggregateZero>(V1)) &&
    709         (CP2 || isa<ConstantAggregateZero>(V2))) {
    710 
    711       // Find the element type of the returned vector
    712       Type *EltTy = VTy->getElementType();
    713       unsigned NumElem = VTy->getNumElements();
    714       std::vector<Constant*> Res(NumElem);
    715 
    716       bool Valid = true;
    717       for (unsigned i = 0; i < NumElem; ++i) {
    718         ConstantInt* c = dyn_cast<ConstantInt>(CondV->getOperand(i));
    719         if (!c) {
    720           Valid = false;
    721           break;
    722         }
    723         Constant *C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
    724         Constant *C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
    725         Res[i] = c->getZExtValue() ? C1 : C2;
    726       }
    727       // If we were able to build the vector, return it
    728       if (Valid) return ConstantVector::get(Res);
    729     }
    730   }
    731 
    732 
    733   if (isa<UndefValue>(Cond)) {
    734     if (isa<UndefValue>(V1)) return V1;
    735     return V2;
    736   }
    737   if (isa<UndefValue>(V1)) return V2;
    738   if (isa<UndefValue>(V2)) return V1;
    739   if (V1 == V2) return V1;
    740 
    741   if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) {
    742     if (TrueVal->getOpcode() == Instruction::Select)
    743       if (TrueVal->getOperand(0) == Cond)
    744 	return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2);
    745   }
    746   if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) {
    747     if (FalseVal->getOpcode() == Instruction::Select)
    748       if (FalseVal->getOperand(0) == Cond)
    749 	return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2));
    750   }
    751 
    752   return 0;
    753 }
    754 
    755 Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
    756                                                       Constant *Idx) {
    757   if (isa<UndefValue>(Val))  // ee(undef, x) -> undef
    758     return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
    759   if (Val->isNullValue())  // ee(zero, x) -> zero
    760     return Constant::getNullValue(
    761                           cast<VectorType>(Val->getType())->getElementType());
    762 
    763   if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
    764     if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
    765       return CVal->getOperand(CIdx->getZExtValue());
    766     } else if (isa<UndefValue>(Idx)) {
    767       // ee({w,x,y,z}, undef) -> w (an arbitrary value).
    768       return CVal->getOperand(0);
    769     }
    770   }
    771   return 0;
    772 }
    773 
    774 Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val,
    775                                                      Constant *Elt,
    776                                                      Constant *Idx) {
    777   ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
    778   if (!CIdx) return 0;
    779   APInt idxVal = CIdx->getValue();
    780   if (isa<UndefValue>(Val)) {
    781     // Insertion of scalar constant into vector undef
    782     // Optimize away insertion of undef
    783     if (isa<UndefValue>(Elt))
    784       return Val;
    785     // Otherwise break the aggregate undef into multiple undefs and do
    786     // the insertion
    787     unsigned numOps =
    788       cast<VectorType>(Val->getType())->getNumElements();
    789     std::vector<Constant*> Ops;
    790     Ops.reserve(numOps);
    791     for (unsigned i = 0; i < numOps; ++i) {
    792       Constant *Op =
    793         (idxVal == i) ? Elt : UndefValue::get(Elt->getType());
    794       Ops.push_back(Op);
    795     }
    796     return ConstantVector::get(Ops);
    797   }
    798   if (isa<ConstantAggregateZero>(Val)) {
    799     // Insertion of scalar constant into vector aggregate zero
    800     // Optimize away insertion of zero
    801     if (Elt->isNullValue())
    802       return Val;
    803     // Otherwise break the aggregate zero into multiple zeros and do
    804     // the insertion
    805     unsigned numOps =
    806       cast<VectorType>(Val->getType())->getNumElements();
    807     std::vector<Constant*> Ops;
    808     Ops.reserve(numOps);
    809     for (unsigned i = 0; i < numOps; ++i) {
    810       Constant *Op =
    811         (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
    812       Ops.push_back(Op);
    813     }
    814     return ConstantVector::get(Ops);
    815   }
    816   if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
    817     // Insertion of scalar constant into vector constant
    818     std::vector<Constant*> Ops;
    819     Ops.reserve(CVal->getNumOperands());
    820     for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
    821       Constant *Op =
    822         (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
    823       Ops.push_back(Op);
    824     }
    825     return ConstantVector::get(Ops);
    826   }
    827 
    828   return 0;
    829 }
    830 
    831 /// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef
    832 /// return the specified element value.  Otherwise return null.
    833 static Constant *GetVectorElement(Constant *C, unsigned EltNo) {
    834   if (ConstantVector *CV = dyn_cast<ConstantVector>(C))
    835     return CV->getOperand(EltNo);
    836 
    837   Type *EltTy = cast<VectorType>(C->getType())->getElementType();
    838   if (isa<ConstantAggregateZero>(C))
    839     return Constant::getNullValue(EltTy);
    840   if (isa<UndefValue>(C))
    841     return UndefValue::get(EltTy);
    842   return 0;
    843 }
    844 
    845 Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1,
    846                                                      Constant *V2,
    847                                                      Constant *Mask) {
    848   // Undefined shuffle mask -> undefined value.
    849   if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType());
    850 
    851   unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements();
    852   unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements();
    853   Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
    854 
    855   // Loop over the shuffle mask, evaluating each element.
    856   SmallVector<Constant*, 32> Result;
    857   for (unsigned i = 0; i != MaskNumElts; ++i) {
    858     Constant *InElt = GetVectorElement(Mask, i);
    859     if (InElt == 0) return 0;
    860 
    861     if (isa<UndefValue>(InElt))
    862       InElt = UndefValue::get(EltTy);
    863     else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) {
    864       unsigned Elt = CI->getZExtValue();
    865       if (Elt >= SrcNumElts*2)
    866         InElt = UndefValue::get(EltTy);
    867       else if (Elt >= SrcNumElts)
    868         InElt = GetVectorElement(V2, Elt - SrcNumElts);
    869       else
    870         InElt = GetVectorElement(V1, Elt);
    871       if (InElt == 0) return 0;
    872     } else {
    873       // Unknown value.
    874       return 0;
    875     }
    876     Result.push_back(InElt);
    877   }
    878 
    879   return ConstantVector::get(Result);
    880 }
    881 
    882 Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
    883                                                     ArrayRef<unsigned> Idxs) {
    884   // Base case: no indices, so return the entire value.
    885   if (Idxs.empty())
    886     return Agg;
    887 
    888   if (isa<UndefValue>(Agg))  // ev(undef, x) -> undef
    889     return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(),
    890                                                             Idxs));
    891 
    892   if (isa<ConstantAggregateZero>(Agg))  // ev(0, x) -> 0
    893     return
    894       Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
    895                                                               Idxs));
    896 
    897   // Otherwise recurse.
    898   if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Agg))
    899     return ConstantFoldExtractValueInstruction(CS->getOperand(Idxs[0]),
    900                                                Idxs.slice(1));
    901 
    902   if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg))
    903     return ConstantFoldExtractValueInstruction(CA->getOperand(Idxs[0]),
    904                                                Idxs.slice(1));
    905   ConstantVector *CV = cast<ConstantVector>(Agg);
    906   return ConstantFoldExtractValueInstruction(CV->getOperand(Idxs[0]),
    907                                              Idxs.slice(1));
    908 }
    909 
    910 Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
    911                                                    Constant *Val,
    912                                                    ArrayRef<unsigned> Idxs) {
    913   // Base case: no indices, so replace the entire value.
    914   if (Idxs.empty())
    915     return Val;
    916 
    917   if (isa<UndefValue>(Agg)) {
    918     // Insertion of constant into aggregate undef
    919     // Optimize away insertion of undef.
    920     if (isa<UndefValue>(Val))
    921       return Agg;
    922 
    923     // Otherwise break the aggregate undef into multiple undefs and do
    924     // the insertion.
    925     CompositeType *AggTy = cast<CompositeType>(Agg->getType());
    926     unsigned numOps;
    927     if (ArrayType *AR = dyn_cast<ArrayType>(AggTy))
    928       numOps = AR->getNumElements();
    929     else
    930       numOps = cast<StructType>(AggTy)->getNumElements();
    931 
    932     std::vector<Constant*> Ops(numOps);
    933     for (unsigned i = 0; i < numOps; ++i) {
    934       Type *MemberTy = AggTy->getTypeAtIndex(i);
    935       Constant *Op =
    936         (Idxs[0] == i) ?
    937         ConstantFoldInsertValueInstruction(UndefValue::get(MemberTy),
    938                                            Val, Idxs.slice(1)) :
    939         UndefValue::get(MemberTy);
    940       Ops[i] = Op;
    941     }
    942 
    943     if (StructType* ST = dyn_cast<StructType>(AggTy))
    944       return ConstantStruct::get(ST, Ops);
    945     return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
    946   }
    947 
    948   if (isa<ConstantAggregateZero>(Agg)) {
    949     // Insertion of constant into aggregate zero
    950     // Optimize away insertion of zero.
    951     if (Val->isNullValue())
    952       return Agg;
    953 
    954     // Otherwise break the aggregate zero into multiple zeros and do
    955     // the insertion.
    956     CompositeType *AggTy = cast<CompositeType>(Agg->getType());
    957     unsigned numOps;
    958     if (ArrayType *AR = dyn_cast<ArrayType>(AggTy))
    959       numOps = AR->getNumElements();
    960     else
    961       numOps = cast<StructType>(AggTy)->getNumElements();
    962 
    963     std::vector<Constant*> Ops(numOps);
    964     for (unsigned i = 0; i < numOps; ++i) {
    965       Type *MemberTy = AggTy->getTypeAtIndex(i);
    966       Constant *Op =
    967         (Idxs[0] == i) ?
    968         ConstantFoldInsertValueInstruction(Constant::getNullValue(MemberTy),
    969                                            Val, Idxs.slice(1)) :
    970         Constant::getNullValue(MemberTy);
    971       Ops[i] = Op;
    972     }
    973 
    974     if (StructType *ST = dyn_cast<StructType>(AggTy))
    975       return ConstantStruct::get(ST, Ops);
    976     return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
    977   }
    978 
    979   if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) {
    980     // Insertion of constant into aggregate constant.
    981     std::vector<Constant*> Ops(Agg->getNumOperands());
    982     for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
    983       Constant *Op = cast<Constant>(Agg->getOperand(i));
    984       if (Idxs[0] == i)
    985         Op = ConstantFoldInsertValueInstruction(Op, Val, Idxs.slice(1));
    986       Ops[i] = Op;
    987     }
    988 
    989     if (StructType* ST = dyn_cast<StructType>(Agg->getType()))
    990       return ConstantStruct::get(ST, Ops);
    991     return ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops);
    992   }
    993 
    994   return 0;
    995 }
    996 
    997 
    998 Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
    999                                               Constant *C1, Constant *C2) {
   1000   // No compile-time operations on this type yet.
   1001   if (C1->getType()->isPPC_FP128Ty())
   1002     return 0;
   1003 
   1004   // Handle UndefValue up front.
   1005   if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
   1006     switch (Opcode) {
   1007     case Instruction::Xor:
   1008       if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
   1009         // Handle undef ^ undef -> 0 special case. This is a common
   1010         // idiom (misuse).
   1011         return Constant::getNullValue(C1->getType());
   1012       // Fallthrough
   1013     case Instruction::Add:
   1014     case Instruction::Sub:
   1015       return UndefValue::get(C1->getType());
   1016     case Instruction::And:
   1017       if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
   1018         return C1;
   1019       return Constant::getNullValue(C1->getType());   // undef & X -> 0
   1020     case Instruction::Mul: {
   1021       ConstantInt *CI;
   1022       // X * undef -> undef   if X is odd or undef
   1023       if (((CI = dyn_cast<ConstantInt>(C1)) && CI->getValue()[0]) ||
   1024           ((CI = dyn_cast<ConstantInt>(C2)) && CI->getValue()[0]) ||
   1025           (isa<UndefValue>(C1) && isa<UndefValue>(C2)))
   1026         return UndefValue::get(C1->getType());
   1027 
   1028       // X * undef -> 0       otherwise
   1029       return Constant::getNullValue(C1->getType());
   1030     }
   1031     case Instruction::UDiv:
   1032     case Instruction::SDiv:
   1033       // undef / 1 -> undef
   1034       if (Opcode == Instruction::UDiv || Opcode == Instruction::SDiv)
   1035         if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2))
   1036           if (CI2->isOne())
   1037             return C1;
   1038       // FALL THROUGH
   1039     case Instruction::URem:
   1040     case Instruction::SRem:
   1041       if (!isa<UndefValue>(C2))                    // undef / X -> 0
   1042         return Constant::getNullValue(C1->getType());
   1043       return C2;                                   // X / undef -> undef
   1044     case Instruction::Or:                          // X | undef -> -1
   1045       if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
   1046         return C1;
   1047       return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
   1048     case Instruction::LShr:
   1049       if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
   1050         return C1;                                  // undef lshr undef -> undef
   1051       return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
   1052                                                     // undef lshr X -> 0
   1053     case Instruction::AShr:
   1054       if (!isa<UndefValue>(C2))                     // undef ashr X --> all ones
   1055         return Constant::getAllOnesValue(C1->getType());
   1056       else if (isa<UndefValue>(C1))
   1057         return C1;                                  // undef ashr undef -> undef
   1058       else
   1059         return C1;                                  // X ashr undef --> X
   1060     case Instruction::Shl:
   1061       if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
   1062         return C1;                                  // undef shl undef -> undef
   1063       // undef << X -> 0   or   X << undef -> 0
   1064       return Constant::getNullValue(C1->getType());
   1065     }
   1066   }
   1067 
   1068   // Handle simplifications when the RHS is a constant int.
   1069   if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
   1070     switch (Opcode) {
   1071     case Instruction::Add:
   1072       if (CI2->equalsInt(0)) return C1;                         // X + 0 == X
   1073       break;
   1074     case Instruction::Sub:
   1075       if (CI2->equalsInt(0)) return C1;                         // X - 0 == X
   1076       break;
   1077     case Instruction::Mul:
   1078       if (CI2->equalsInt(0)) return C2;                         // X * 0 == 0
   1079       if (CI2->equalsInt(1))
   1080         return C1;                                              // X * 1 == X
   1081       break;
   1082     case Instruction::UDiv:
   1083     case Instruction::SDiv:
   1084       if (CI2->equalsInt(1))
   1085         return C1;                                            // X / 1 == X
   1086       if (CI2->equalsInt(0))
   1087         return UndefValue::get(CI2->getType());               // X / 0 == undef
   1088       break;
   1089     case Instruction::URem:
   1090     case Instruction::SRem:
   1091       if (CI2->equalsInt(1))
   1092         return Constant::getNullValue(CI2->getType());        // X % 1 == 0
   1093       if (CI2->equalsInt(0))
   1094         return UndefValue::get(CI2->getType());               // X % 0 == undef
   1095       break;
   1096     case Instruction::And:
   1097       if (CI2->isZero()) return C2;                           // X & 0 == 0
   1098       if (CI2->isAllOnesValue())
   1099         return C1;                                            // X & -1 == X
   1100 
   1101       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
   1102         // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
   1103         if (CE1->getOpcode() == Instruction::ZExt) {
   1104           unsigned DstWidth = CI2->getType()->getBitWidth();
   1105           unsigned SrcWidth =
   1106             CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
   1107           APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
   1108           if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
   1109             return C1;
   1110         }
   1111 
   1112         // If and'ing the address of a global with a constant, fold it.
   1113         if (CE1->getOpcode() == Instruction::PtrToInt &&
   1114             isa<GlobalValue>(CE1->getOperand(0))) {
   1115           GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
   1116 
   1117           // Functions are at least 4-byte aligned.
   1118           unsigned GVAlign = GV->getAlignment();
   1119           if (isa<Function>(GV))
   1120             GVAlign = std::max(GVAlign, 4U);
   1121 
   1122           if (GVAlign > 1) {
   1123             unsigned DstWidth = CI2->getType()->getBitWidth();
   1124             unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
   1125             APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
   1126 
   1127             // If checking bits we know are clear, return zero.
   1128             if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
   1129               return Constant::getNullValue(CI2->getType());
   1130           }
   1131         }
   1132       }
   1133       break;
   1134     case Instruction::Or:
   1135       if (CI2->equalsInt(0)) return C1;    // X | 0 == X
   1136       if (CI2->isAllOnesValue())
   1137         return C2;                         // X | -1 == -1
   1138       break;
   1139     case Instruction::Xor:
   1140       if (CI2->equalsInt(0)) return C1;    // X ^ 0 == X
   1141 
   1142       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
   1143         switch (CE1->getOpcode()) {
   1144         default: break;
   1145         case Instruction::ICmp:
   1146         case Instruction::FCmp:
   1147           // cmp pred ^ true -> cmp !pred
   1148           assert(CI2->equalsInt(1));
   1149           CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
   1150           pred = CmpInst::getInversePredicate(pred);
   1151           return ConstantExpr::getCompare(pred, CE1->getOperand(0),
   1152                                           CE1->getOperand(1));
   1153         }
   1154       }
   1155       break;
   1156     case Instruction::AShr:
   1157       // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
   1158       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
   1159         if (CE1->getOpcode() == Instruction::ZExt)  // Top bits known zero.
   1160           return ConstantExpr::getLShr(C1, C2);
   1161       break;
   1162     }
   1163   } else if (isa<ConstantInt>(C1)) {
   1164     // If C1 is a ConstantInt and C2 is not, swap the operands.
   1165     if (Instruction::isCommutative(Opcode))
   1166       return ConstantExpr::get(Opcode, C2, C1);
   1167   }
   1168 
   1169   // At this point we know neither constant is an UndefValue.
   1170   if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
   1171     if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
   1172       using namespace APIntOps;
   1173       const APInt &C1V = CI1->getValue();
   1174       const APInt &C2V = CI2->getValue();
   1175       switch (Opcode) {
   1176       default:
   1177         break;
   1178       case Instruction::Add:
   1179         return ConstantInt::get(CI1->getContext(), C1V + C2V);
   1180       case Instruction::Sub:
   1181         return ConstantInt::get(CI1->getContext(), C1V - C2V);
   1182       case Instruction::Mul:
   1183         return ConstantInt::get(CI1->getContext(), C1V * C2V);
   1184       case Instruction::UDiv:
   1185         assert(!CI2->isNullValue() && "Div by zero handled above");
   1186         return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V));
   1187       case Instruction::SDiv:
   1188         assert(!CI2->isNullValue() && "Div by zero handled above");
   1189         if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
   1190           return UndefValue::get(CI1->getType());   // MIN_INT / -1 -> undef
   1191         return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V));
   1192       case Instruction::URem:
   1193         assert(!CI2->isNullValue() && "Div by zero handled above");
   1194         return ConstantInt::get(CI1->getContext(), C1V.urem(C2V));
   1195       case Instruction::SRem:
   1196         assert(!CI2->isNullValue() && "Div by zero handled above");
   1197         if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
   1198           return UndefValue::get(CI1->getType());   // MIN_INT % -1 -> undef
   1199         return ConstantInt::get(CI1->getContext(), C1V.srem(C2V));
   1200       case Instruction::And:
   1201         return ConstantInt::get(CI1->getContext(), C1V & C2V);
   1202       case Instruction::Or:
   1203         return ConstantInt::get(CI1->getContext(), C1V | C2V);
   1204       case Instruction::Xor:
   1205         return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
   1206       case Instruction::Shl: {
   1207         uint32_t shiftAmt = C2V.getZExtValue();
   1208         if (shiftAmt < C1V.getBitWidth())
   1209           return ConstantInt::get(CI1->getContext(), C1V.shl(shiftAmt));
   1210         else
   1211           return UndefValue::get(C1->getType()); // too big shift is undef
   1212       }
   1213       case Instruction::LShr: {
   1214         uint32_t shiftAmt = C2V.getZExtValue();
   1215         if (shiftAmt < C1V.getBitWidth())
   1216           return ConstantInt::get(CI1->getContext(), C1V.lshr(shiftAmt));
   1217         else
   1218           return UndefValue::get(C1->getType()); // too big shift is undef
   1219       }
   1220       case Instruction::AShr: {
   1221         uint32_t shiftAmt = C2V.getZExtValue();
   1222         if (shiftAmt < C1V.getBitWidth())
   1223           return ConstantInt::get(CI1->getContext(), C1V.ashr(shiftAmt));
   1224         else
   1225           return UndefValue::get(C1->getType()); // too big shift is undef
   1226       }
   1227       }
   1228     }
   1229 
   1230     switch (Opcode) {
   1231     case Instruction::SDiv:
   1232     case Instruction::UDiv:
   1233     case Instruction::URem:
   1234     case Instruction::SRem:
   1235     case Instruction::LShr:
   1236     case Instruction::AShr:
   1237     case Instruction::Shl:
   1238       if (CI1->equalsInt(0)) return C1;
   1239       break;
   1240     default:
   1241       break;
   1242     }
   1243   } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
   1244     if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
   1245       APFloat C1V = CFP1->getValueAPF();
   1246       APFloat C2V = CFP2->getValueAPF();
   1247       APFloat C3V = C1V;  // copy for modification
   1248       switch (Opcode) {
   1249       default:
   1250         break;
   1251       case Instruction::FAdd:
   1252         (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
   1253         return ConstantFP::get(C1->getContext(), C3V);
   1254       case Instruction::FSub:
   1255         (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
   1256         return ConstantFP::get(C1->getContext(), C3V);
   1257       case Instruction::FMul:
   1258         (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
   1259         return ConstantFP::get(C1->getContext(), C3V);
   1260       case Instruction::FDiv:
   1261         (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
   1262         return ConstantFP::get(C1->getContext(), C3V);
   1263       case Instruction::FRem:
   1264         (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
   1265         return ConstantFP::get(C1->getContext(), C3V);
   1266       }
   1267     }
   1268   } else if (VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
   1269     ConstantVector *CP1 = dyn_cast<ConstantVector>(C1);
   1270     ConstantVector *CP2 = dyn_cast<ConstantVector>(C2);
   1271     if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) &&
   1272         (CP2 != NULL || isa<ConstantAggregateZero>(C2))) {
   1273       std::vector<Constant*> Res;
   1274       Type* EltTy = VTy->getElementType();
   1275       Constant *C1 = 0;
   1276       Constant *C2 = 0;
   1277       switch (Opcode) {
   1278       default:
   1279         break;
   1280       case Instruction::Add:
   1281         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1282           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1283           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1284           Res.push_back(ConstantExpr::getAdd(C1, C2));
   1285         }
   1286         return ConstantVector::get(Res);
   1287       case Instruction::FAdd:
   1288         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1289           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1290           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1291           Res.push_back(ConstantExpr::getFAdd(C1, C2));
   1292         }
   1293         return ConstantVector::get(Res);
   1294       case Instruction::Sub:
   1295         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1296           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1297           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1298           Res.push_back(ConstantExpr::getSub(C1, C2));
   1299         }
   1300         return ConstantVector::get(Res);
   1301       case Instruction::FSub:
   1302         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1303           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1304           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1305           Res.push_back(ConstantExpr::getFSub(C1, C2));
   1306         }
   1307         return ConstantVector::get(Res);
   1308       case Instruction::Mul:
   1309         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1310           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1311           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1312           Res.push_back(ConstantExpr::getMul(C1, C2));
   1313         }
   1314         return ConstantVector::get(Res);
   1315       case Instruction::FMul:
   1316         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1317           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1318           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1319           Res.push_back(ConstantExpr::getFMul(C1, C2));
   1320         }
   1321         return ConstantVector::get(Res);
   1322       case Instruction::UDiv:
   1323         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1324           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1325           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1326           Res.push_back(ConstantExpr::getUDiv(C1, C2));
   1327         }
   1328         return ConstantVector::get(Res);
   1329       case Instruction::SDiv:
   1330         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1331           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1332           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1333           Res.push_back(ConstantExpr::getSDiv(C1, C2));
   1334         }
   1335         return ConstantVector::get(Res);
   1336       case Instruction::FDiv:
   1337         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1338           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1339           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1340           Res.push_back(ConstantExpr::getFDiv(C1, C2));
   1341         }
   1342         return ConstantVector::get(Res);
   1343       case Instruction::URem:
   1344         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1345           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1346           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1347           Res.push_back(ConstantExpr::getURem(C1, C2));
   1348         }
   1349         return ConstantVector::get(Res);
   1350       case Instruction::SRem:
   1351         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1352           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1353           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1354           Res.push_back(ConstantExpr::getSRem(C1, C2));
   1355         }
   1356         return ConstantVector::get(Res);
   1357       case Instruction::FRem:
   1358         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1359           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1360           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1361           Res.push_back(ConstantExpr::getFRem(C1, C2));
   1362         }
   1363         return ConstantVector::get(Res);
   1364       case Instruction::And:
   1365         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1366           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1367           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1368           Res.push_back(ConstantExpr::getAnd(C1, C2));
   1369         }
   1370         return ConstantVector::get(Res);
   1371       case Instruction::Or:
   1372         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1373           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1374           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1375           Res.push_back(ConstantExpr::getOr(C1, C2));
   1376         }
   1377         return ConstantVector::get(Res);
   1378       case Instruction::Xor:
   1379         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1380           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1381           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1382           Res.push_back(ConstantExpr::getXor(C1, C2));
   1383         }
   1384         return ConstantVector::get(Res);
   1385       case Instruction::LShr:
   1386         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1387           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1388           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1389           Res.push_back(ConstantExpr::getLShr(C1, C2));
   1390         }
   1391         return ConstantVector::get(Res);
   1392       case Instruction::AShr:
   1393         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1394           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1395           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1396           Res.push_back(ConstantExpr::getAShr(C1, C2));
   1397         }
   1398         return ConstantVector::get(Res);
   1399       case Instruction::Shl:
   1400         for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
   1401           C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
   1402           C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
   1403           Res.push_back(ConstantExpr::getShl(C1, C2));
   1404         }
   1405         return ConstantVector::get(Res);
   1406       }
   1407     }
   1408   }
   1409 
   1410   if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
   1411     // There are many possible foldings we could do here.  We should probably
   1412     // at least fold add of a pointer with an integer into the appropriate
   1413     // getelementptr.  This will improve alias analysis a bit.
   1414 
   1415     // Given ((a + b) + c), if (b + c) folds to something interesting, return
   1416     // (a + (b + c)).
   1417     if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
   1418       Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
   1419       if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
   1420         return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
   1421     }
   1422   } else if (isa<ConstantExpr>(C2)) {
   1423     // If C2 is a constant expr and C1 isn't, flop them around and fold the
   1424     // other way if possible.
   1425     if (Instruction::isCommutative(Opcode))
   1426       return ConstantFoldBinaryInstruction(Opcode, C2, C1);
   1427   }
   1428 
   1429   // i1 can be simplified in many cases.
   1430   if (C1->getType()->isIntegerTy(1)) {
   1431     switch (Opcode) {
   1432     case Instruction::Add:
   1433     case Instruction::Sub:
   1434       return ConstantExpr::getXor(C1, C2);
   1435     case Instruction::Mul:
   1436       return ConstantExpr::getAnd(C1, C2);
   1437     case Instruction::Shl:
   1438     case Instruction::LShr:
   1439     case Instruction::AShr:
   1440       // We can assume that C2 == 0.  If it were one the result would be
   1441       // undefined because the shift value is as large as the bitwidth.
   1442       return C1;
   1443     case Instruction::SDiv:
   1444     case Instruction::UDiv:
   1445       // We can assume that C2 == 1.  If it were zero the result would be
   1446       // undefined through division by zero.
   1447       return C1;
   1448     case Instruction::URem:
   1449     case Instruction::SRem:
   1450       // We can assume that C2 == 1.  If it were zero the result would be
   1451       // undefined through division by zero.
   1452       return ConstantInt::getFalse(C1->getContext());
   1453     default:
   1454       break;
   1455     }
   1456   }
   1457 
   1458   // We don't know how to fold this.
   1459   return 0;
   1460 }
   1461 
   1462 /// isZeroSizedType - This type is zero sized if its an array or structure of
   1463 /// zero sized types.  The only leaf zero sized type is an empty structure.
   1464 static bool isMaybeZeroSizedType(Type *Ty) {
   1465   if (StructType *STy = dyn_cast<StructType>(Ty)) {
   1466     if (STy->isOpaque()) return true;  // Can't say.
   1467 
   1468     // If all of elements have zero size, this does too.
   1469     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
   1470       if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
   1471     return true;
   1472 
   1473   } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
   1474     return isMaybeZeroSizedType(ATy->getElementType());
   1475   }
   1476   return false;
   1477 }
   1478 
   1479 /// IdxCompare - Compare the two constants as though they were getelementptr
   1480 /// indices.  This allows coersion of the types to be the same thing.
   1481 ///
   1482 /// If the two constants are the "same" (after coersion), return 0.  If the
   1483 /// first is less than the second, return -1, if the second is less than the
   1484 /// first, return 1.  If the constants are not integral, return -2.
   1485 ///
   1486 static int IdxCompare(Constant *C1, Constant *C2, Type *ElTy) {
   1487   if (C1 == C2) return 0;
   1488 
   1489   // Ok, we found a different index.  If they are not ConstantInt, we can't do
   1490   // anything with them.
   1491   if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
   1492     return -2; // don't know!
   1493 
   1494   // Ok, we have two differing integer indices.  Sign extend them to be the same
   1495   // type.  Long is always big enough, so we use it.
   1496   if (!C1->getType()->isIntegerTy(64))
   1497     C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(C1->getContext()));
   1498 
   1499   if (!C2->getType()->isIntegerTy(64))
   1500     C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(C1->getContext()));
   1501 
   1502   if (C1 == C2) return 0;  // They are equal
   1503 
   1504   // If the type being indexed over is really just a zero sized type, there is
   1505   // no pointer difference being made here.
   1506   if (isMaybeZeroSizedType(ElTy))
   1507     return -2; // dunno.
   1508 
   1509   // If they are really different, now that they are the same type, then we
   1510   // found a difference!
   1511   if (cast<ConstantInt>(C1)->getSExtValue() <
   1512       cast<ConstantInt>(C2)->getSExtValue())
   1513     return -1;
   1514   else
   1515     return 1;
   1516 }
   1517 
   1518 /// evaluateFCmpRelation - This function determines if there is anything we can
   1519 /// decide about the two constants provided.  This doesn't need to handle simple
   1520 /// things like ConstantFP comparisons, but should instead handle ConstantExprs.
   1521 /// If we can determine that the two constants have a particular relation to
   1522 /// each other, we should return the corresponding FCmpInst predicate,
   1523 /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
   1524 /// ConstantFoldCompareInstruction.
   1525 ///
   1526 /// To simplify this code we canonicalize the relation so that the first
   1527 /// operand is always the most "complex" of the two.  We consider ConstantFP
   1528 /// to be the simplest, and ConstantExprs to be the most complex.
   1529 static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
   1530   assert(V1->getType() == V2->getType() &&
   1531          "Cannot compare values of different types!");
   1532 
   1533   // No compile-time operations on this type yet.
   1534   if (V1->getType()->isPPC_FP128Ty())
   1535     return FCmpInst::BAD_FCMP_PREDICATE;
   1536 
   1537   // Handle degenerate case quickly
   1538   if (V1 == V2) return FCmpInst::FCMP_OEQ;
   1539 
   1540   if (!isa<ConstantExpr>(V1)) {
   1541     if (!isa<ConstantExpr>(V2)) {
   1542       // We distilled thisUse the standard constant folder for a few cases
   1543       ConstantInt *R = 0;
   1544       R = dyn_cast<ConstantInt>(
   1545                       ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
   1546       if (R && !R->isZero())
   1547         return FCmpInst::FCMP_OEQ;
   1548       R = dyn_cast<ConstantInt>(
   1549                       ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
   1550       if (R && !R->isZero())
   1551         return FCmpInst::FCMP_OLT;
   1552       R = dyn_cast<ConstantInt>(
   1553                       ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
   1554       if (R && !R->isZero())
   1555         return FCmpInst::FCMP_OGT;
   1556 
   1557       // Nothing more we can do
   1558       return FCmpInst::BAD_FCMP_PREDICATE;
   1559     }
   1560 
   1561     // If the first operand is simple and second is ConstantExpr, swap operands.
   1562     FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
   1563     if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
   1564       return FCmpInst::getSwappedPredicate(SwappedRelation);
   1565   } else {
   1566     // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
   1567     // constantexpr or a simple constant.
   1568     ConstantExpr *CE1 = cast<ConstantExpr>(V1);
   1569     switch (CE1->getOpcode()) {
   1570     case Instruction::FPTrunc:
   1571     case Instruction::FPExt:
   1572     case Instruction::UIToFP:
   1573     case Instruction::SIToFP:
   1574       // We might be able to do something with these but we don't right now.
   1575       break;
   1576     default:
   1577       break;
   1578     }
   1579   }
   1580   // There are MANY other foldings that we could perform here.  They will
   1581   // probably be added on demand, as they seem needed.
   1582   return FCmpInst::BAD_FCMP_PREDICATE;
   1583 }
   1584 
   1585 /// evaluateICmpRelation - This function determines if there is anything we can
   1586 /// decide about the two constants provided.  This doesn't need to handle simple
   1587 /// things like integer comparisons, but should instead handle ConstantExprs
   1588 /// and GlobalValues.  If we can determine that the two constants have a
   1589 /// particular relation to each other, we should return the corresponding ICmp
   1590 /// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
   1591 ///
   1592 /// To simplify this code we canonicalize the relation so that the first
   1593 /// operand is always the most "complex" of the two.  We consider simple
   1594 /// constants (like ConstantInt) to be the simplest, followed by
   1595 /// GlobalValues, followed by ConstantExpr's (the most complex).
   1596 ///
   1597 static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
   1598                                                 bool isSigned) {
   1599   assert(V1->getType() == V2->getType() &&
   1600          "Cannot compare different types of values!");
   1601   if (V1 == V2) return ICmpInst::ICMP_EQ;
   1602 
   1603   if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
   1604       !isa<BlockAddress>(V1)) {
   1605     if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) &&
   1606         !isa<BlockAddress>(V2)) {
   1607       // We distilled this down to a simple case, use the standard constant
   1608       // folder.
   1609       ConstantInt *R = 0;
   1610       ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
   1611       R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
   1612       if (R && !R->isZero())
   1613         return pred;
   1614       pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
   1615       R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
   1616       if (R && !R->isZero())
   1617         return pred;
   1618       pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
   1619       R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
   1620       if (R && !R->isZero())
   1621         return pred;
   1622 
   1623       // If we couldn't figure it out, bail.
   1624       return ICmpInst::BAD_ICMP_PREDICATE;
   1625     }
   1626 
   1627     // If the first operand is simple, swap operands.
   1628     ICmpInst::Predicate SwappedRelation =
   1629       evaluateICmpRelation(V2, V1, isSigned);
   1630     if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
   1631       return ICmpInst::getSwappedPredicate(SwappedRelation);
   1632 
   1633   } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
   1634     if (isa<ConstantExpr>(V2)) {  // Swap as necessary.
   1635       ICmpInst::Predicate SwappedRelation =
   1636         evaluateICmpRelation(V2, V1, isSigned);
   1637       if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
   1638         return ICmpInst::getSwappedPredicate(SwappedRelation);
   1639       return ICmpInst::BAD_ICMP_PREDICATE;
   1640     }
   1641 
   1642     // Now we know that the RHS is a GlobalValue, BlockAddress or simple
   1643     // constant (which, since the types must match, means that it's a
   1644     // ConstantPointerNull).
   1645     if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
   1646       // Don't try to decide equality of aliases.
   1647       if (!isa<GlobalAlias>(GV) && !isa<GlobalAlias>(GV2))
   1648         if (!GV->hasExternalWeakLinkage() || !GV2->hasExternalWeakLinkage())
   1649           return ICmpInst::ICMP_NE;
   1650     } else if (isa<BlockAddress>(V2)) {
   1651       return ICmpInst::ICMP_NE; // Globals never equal labels.
   1652     } else {
   1653       assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
   1654       // GlobalVals can never be null unless they have external weak linkage.
   1655       // We don't try to evaluate aliases here.
   1656       if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV))
   1657         return ICmpInst::ICMP_NE;
   1658     }
   1659   } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
   1660     if (isa<ConstantExpr>(V2)) {  // Swap as necessary.
   1661       ICmpInst::Predicate SwappedRelation =
   1662         evaluateICmpRelation(V2, V1, isSigned);
   1663       if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
   1664         return ICmpInst::getSwappedPredicate(SwappedRelation);
   1665       return ICmpInst::BAD_ICMP_PREDICATE;
   1666     }
   1667 
   1668     // Now we know that the RHS is a GlobalValue, BlockAddress or simple
   1669     // constant (which, since the types must match, means that it is a
   1670     // ConstantPointerNull).
   1671     if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
   1672       // Block address in another function can't equal this one, but block
   1673       // addresses in the current function might be the same if blocks are
   1674       // empty.
   1675       if (BA2->getFunction() != BA->getFunction())
   1676         return ICmpInst::ICMP_NE;
   1677     } else {
   1678       // Block addresses aren't null, don't equal the address of globals.
   1679       assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
   1680              "Canonicalization guarantee!");
   1681       return ICmpInst::ICMP_NE;
   1682     }
   1683   } else {
   1684     // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
   1685     // constantexpr, a global, block address, or a simple constant.
   1686     ConstantExpr *CE1 = cast<ConstantExpr>(V1);
   1687     Constant *CE1Op0 = CE1->getOperand(0);
   1688 
   1689     switch (CE1->getOpcode()) {
   1690     case Instruction::Trunc:
   1691     case Instruction::FPTrunc:
   1692     case Instruction::FPExt:
   1693     case Instruction::FPToUI:
   1694     case Instruction::FPToSI:
   1695       break; // We can't evaluate floating point casts or truncations.
   1696 
   1697     case Instruction::UIToFP:
   1698     case Instruction::SIToFP:
   1699     case Instruction::BitCast:
   1700     case Instruction::ZExt:
   1701     case Instruction::SExt:
   1702       // If the cast is not actually changing bits, and the second operand is a
   1703       // null pointer, do the comparison with the pre-casted value.
   1704       if (V2->isNullValue() &&
   1705           (CE1->getType()->isPointerTy() || CE1->getType()->isIntegerTy())) {
   1706         if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
   1707         if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
   1708         return evaluateICmpRelation(CE1Op0,
   1709                                     Constant::getNullValue(CE1Op0->getType()),
   1710                                     isSigned);
   1711       }
   1712       break;
   1713 
   1714     case Instruction::GetElementPtr:
   1715       // Ok, since this is a getelementptr, we know that the constant has a
   1716       // pointer type.  Check the various cases.
   1717       if (isa<ConstantPointerNull>(V2)) {
   1718         // If we are comparing a GEP to a null pointer, check to see if the base
   1719         // of the GEP equals the null pointer.
   1720         if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
   1721           if (GV->hasExternalWeakLinkage())
   1722             // Weak linkage GVals could be zero or not. We're comparing that
   1723             // to null pointer so its greater-or-equal
   1724             return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
   1725           else
   1726             // If its not weak linkage, the GVal must have a non-zero address
   1727             // so the result is greater-than
   1728             return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
   1729         } else if (isa<ConstantPointerNull>(CE1Op0)) {
   1730           // If we are indexing from a null pointer, check to see if we have any
   1731           // non-zero indices.
   1732           for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
   1733             if (!CE1->getOperand(i)->isNullValue())
   1734               // Offsetting from null, must not be equal.
   1735               return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
   1736           // Only zero indexes from null, must still be zero.
   1737           return ICmpInst::ICMP_EQ;
   1738         }
   1739         // Otherwise, we can't really say if the first operand is null or not.
   1740       } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
   1741         if (isa<ConstantPointerNull>(CE1Op0)) {
   1742           if (GV2->hasExternalWeakLinkage())
   1743             // Weak linkage GVals could be zero or not. We're comparing it to
   1744             // a null pointer, so its less-or-equal
   1745             return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
   1746           else
   1747             // If its not weak linkage, the GVal must have a non-zero address
   1748             // so the result is less-than
   1749             return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
   1750         } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
   1751           if (GV == GV2) {
   1752             // If this is a getelementptr of the same global, then it must be
   1753             // different.  Because the types must match, the getelementptr could
   1754             // only have at most one index, and because we fold getelementptr's
   1755             // with a single zero index, it must be nonzero.
   1756             assert(CE1->getNumOperands() == 2 &&
   1757                    !CE1->getOperand(1)->isNullValue() &&
   1758                    "Surprising getelementptr!");
   1759             return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
   1760           } else {
   1761             // If they are different globals, we don't know what the value is,
   1762             // but they can't be equal.
   1763             return ICmpInst::ICMP_NE;
   1764           }
   1765         }
   1766       } else {
   1767         ConstantExpr *CE2 = cast<ConstantExpr>(V2);
   1768         Constant *CE2Op0 = CE2->getOperand(0);
   1769 
   1770         // There are MANY other foldings that we could perform here.  They will
   1771         // probably be added on demand, as they seem needed.
   1772         switch (CE2->getOpcode()) {
   1773         default: break;
   1774         case Instruction::GetElementPtr:
   1775           // By far the most common case to handle is when the base pointers are
   1776           // obviously to the same or different globals.
   1777           if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
   1778             if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
   1779               return ICmpInst::ICMP_NE;
   1780             // Ok, we know that both getelementptr instructions are based on the
   1781             // same global.  From this, we can precisely determine the relative
   1782             // ordering of the resultant pointers.
   1783             unsigned i = 1;
   1784 
   1785             // The logic below assumes that the result of the comparison
   1786             // can be determined by finding the first index that differs.
   1787             // This doesn't work if there is over-indexing in any
   1788             // subsequent indices, so check for that case first.
   1789             if (!CE1->isGEPWithNoNotionalOverIndexing() ||
   1790                 !CE2->isGEPWithNoNotionalOverIndexing())
   1791                return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
   1792 
   1793             // Compare all of the operands the GEP's have in common.
   1794             gep_type_iterator GTI = gep_type_begin(CE1);
   1795             for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
   1796                  ++i, ++GTI)
   1797               switch (IdxCompare(CE1->getOperand(i),
   1798                                  CE2->getOperand(i), GTI.getIndexedType())) {
   1799               case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
   1800               case 1:  return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
   1801               case -2: return ICmpInst::BAD_ICMP_PREDICATE;
   1802               }
   1803 
   1804             // Ok, we ran out of things they have in common.  If any leftovers
   1805             // are non-zero then we have a difference, otherwise we are equal.
   1806             for (; i < CE1->getNumOperands(); ++i)
   1807               if (!CE1->getOperand(i)->isNullValue()) {
   1808                 if (isa<ConstantInt>(CE1->getOperand(i)))
   1809                   return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
   1810                 else
   1811                   return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
   1812               }
   1813 
   1814             for (; i < CE2->getNumOperands(); ++i)
   1815               if (!CE2->getOperand(i)->isNullValue()) {
   1816                 if (isa<ConstantInt>(CE2->getOperand(i)))
   1817                   return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
   1818                 else
   1819                   return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
   1820               }
   1821             return ICmpInst::ICMP_EQ;
   1822           }
   1823         }
   1824       }
   1825     default:
   1826       break;
   1827     }
   1828   }
   1829 
   1830   return ICmpInst::BAD_ICMP_PREDICATE;
   1831 }
   1832 
   1833 Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
   1834                                                Constant *C1, Constant *C2) {
   1835   Type *ResultTy;
   1836   if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
   1837     ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
   1838                                VT->getNumElements());
   1839   else
   1840     ResultTy = Type::getInt1Ty(C1->getContext());
   1841 
   1842   // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
   1843   if (pred == FCmpInst::FCMP_FALSE)
   1844     return Constant::getNullValue(ResultTy);
   1845 
   1846   if (pred == FCmpInst::FCMP_TRUE)
   1847     return Constant::getAllOnesValue(ResultTy);
   1848 
   1849   // Handle some degenerate cases first
   1850   if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
   1851     // For EQ and NE, we can always pick a value for the undef to make the
   1852     // predicate pass or fail, so we can return undef.
   1853     // Also, if both operands are undef, we can return undef.
   1854     if (ICmpInst::isEquality(ICmpInst::Predicate(pred)) ||
   1855         (isa<UndefValue>(C1) && isa<UndefValue>(C2)))
   1856       return UndefValue::get(ResultTy);
   1857     // Otherwise, pick the same value as the non-undef operand, and fold
   1858     // it to true or false.
   1859     return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(pred));
   1860   }
   1861 
   1862   // No compile-time operations on this type yet.
   1863   if (C1->getType()->isPPC_FP128Ty())
   1864     return 0;
   1865 
   1866   // icmp eq/ne(null,GV) -> false/true
   1867   if (C1->isNullValue()) {
   1868     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
   1869       // Don't try to evaluate aliases.  External weak GV can be null.
   1870       if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
   1871         if (pred == ICmpInst::ICMP_EQ)
   1872           return ConstantInt::getFalse(C1->getContext());
   1873         else if (pred == ICmpInst::ICMP_NE)
   1874           return ConstantInt::getTrue(C1->getContext());
   1875       }
   1876   // icmp eq/ne(GV,null) -> false/true
   1877   } else if (C2->isNullValue()) {
   1878     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
   1879       // Don't try to evaluate aliases.  External weak GV can be null.
   1880       if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
   1881         if (pred == ICmpInst::ICMP_EQ)
   1882           return ConstantInt::getFalse(C1->getContext());
   1883         else if (pred == ICmpInst::ICMP_NE)
   1884           return ConstantInt::getTrue(C1->getContext());
   1885       }
   1886   }
   1887 
   1888   // If the comparison is a comparison between two i1's, simplify it.
   1889   if (C1->getType()->isIntegerTy(1)) {
   1890     switch(pred) {
   1891     case ICmpInst::ICMP_EQ:
   1892       if (isa<ConstantInt>(C2))
   1893         return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
   1894       return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
   1895     case ICmpInst::ICMP_NE:
   1896       return ConstantExpr::getXor(C1, C2);
   1897     default:
   1898       break;
   1899     }
   1900   }
   1901 
   1902   if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
   1903     APInt V1 = cast<ConstantInt>(C1)->getValue();
   1904     APInt V2 = cast<ConstantInt>(C2)->getValue();
   1905     switch (pred) {
   1906     default: llvm_unreachable("Invalid ICmp Predicate"); return 0;
   1907     case ICmpInst::ICMP_EQ:  return ConstantInt::get(ResultTy, V1 == V2);
   1908     case ICmpInst::ICMP_NE:  return ConstantInt::get(ResultTy, V1 != V2);
   1909     case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2));
   1910     case ICmpInst::ICMP_SGT: return ConstantInt::get(ResultTy, V1.sgt(V2));
   1911     case ICmpInst::ICMP_SLE: return ConstantInt::get(ResultTy, V1.sle(V2));
   1912     case ICmpInst::ICMP_SGE: return ConstantInt::get(ResultTy, V1.sge(V2));
   1913     case ICmpInst::ICMP_ULT: return ConstantInt::get(ResultTy, V1.ult(V2));
   1914     case ICmpInst::ICMP_UGT: return ConstantInt::get(ResultTy, V1.ugt(V2));
   1915     case ICmpInst::ICMP_ULE: return ConstantInt::get(ResultTy, V1.ule(V2));
   1916     case ICmpInst::ICMP_UGE: return ConstantInt::get(ResultTy, V1.uge(V2));
   1917     }
   1918   } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
   1919     APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
   1920     APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
   1921     APFloat::cmpResult R = C1V.compare(C2V);
   1922     switch (pred) {
   1923     default: llvm_unreachable("Invalid FCmp Predicate"); return 0;
   1924     case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy);
   1925     case FCmpInst::FCMP_TRUE:  return Constant::getAllOnesValue(ResultTy);
   1926     case FCmpInst::FCMP_UNO:
   1927       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered);
   1928     case FCmpInst::FCMP_ORD:
   1929       return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered);
   1930     case FCmpInst::FCMP_UEQ:
   1931       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
   1932                                         R==APFloat::cmpEqual);
   1933     case FCmpInst::FCMP_OEQ:
   1934       return ConstantInt::get(ResultTy, R==APFloat::cmpEqual);
   1935     case FCmpInst::FCMP_UNE:
   1936       return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual);
   1937     case FCmpInst::FCMP_ONE:
   1938       return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
   1939                                         R==APFloat::cmpGreaterThan);
   1940     case FCmpInst::FCMP_ULT:
   1941       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
   1942                                         R==APFloat::cmpLessThan);
   1943     case FCmpInst::FCMP_OLT:
   1944       return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan);
   1945     case FCmpInst::FCMP_UGT:
   1946       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
   1947                                         R==APFloat::cmpGreaterThan);
   1948     case FCmpInst::FCMP_OGT:
   1949       return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan);
   1950     case FCmpInst::FCMP_ULE:
   1951       return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan);
   1952     case FCmpInst::FCMP_OLE:
   1953       return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
   1954                                         R==APFloat::cmpEqual);
   1955     case FCmpInst::FCMP_UGE:
   1956       return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan);
   1957     case FCmpInst::FCMP_OGE:
   1958       return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan ||
   1959                                         R==APFloat::cmpEqual);
   1960     }
   1961   } else if (C1->getType()->isVectorTy()) {
   1962     SmallVector<Constant*, 16> C1Elts, C2Elts;
   1963     C1->getVectorElements(C1Elts);
   1964     C2->getVectorElements(C2Elts);
   1965     if (C1Elts.empty() || C2Elts.empty())
   1966       return 0;
   1967 
   1968     // If we can constant fold the comparison of each element, constant fold
   1969     // the whole vector comparison.
   1970     SmallVector<Constant*, 4> ResElts;
   1971     // Compare the elements, producing an i1 result or constant expr.
   1972     for (unsigned i = 0, e = C1Elts.size(); i != e; ++i)
   1973       ResElts.push_back(ConstantExpr::getCompare(pred, C1Elts[i], C2Elts[i]));
   1974 
   1975     return ConstantVector::get(ResElts);
   1976   }
   1977 
   1978   if (C1->getType()->isFloatingPointTy()) {
   1979     int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
   1980     switch (evaluateFCmpRelation(C1, C2)) {
   1981     default: llvm_unreachable("Unknown relation!");
   1982     case FCmpInst::FCMP_UNO:
   1983     case FCmpInst::FCMP_ORD:
   1984     case FCmpInst::FCMP_UEQ:
   1985     case FCmpInst::FCMP_UNE:
   1986     case FCmpInst::FCMP_ULT:
   1987     case FCmpInst::FCMP_UGT:
   1988     case FCmpInst::FCMP_ULE:
   1989     case FCmpInst::FCMP_UGE:
   1990     case FCmpInst::FCMP_TRUE:
   1991     case FCmpInst::FCMP_FALSE:
   1992     case FCmpInst::BAD_FCMP_PREDICATE:
   1993       break; // Couldn't determine anything about these constants.
   1994     case FCmpInst::FCMP_OEQ: // We know that C1 == C2
   1995       Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
   1996                 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
   1997                 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
   1998       break;
   1999     case FCmpInst::FCMP_OLT: // We know that C1 < C2
   2000       Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
   2001                 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
   2002                 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
   2003       break;
   2004     case FCmpInst::FCMP_OGT: // We know that C1 > C2
   2005       Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
   2006                 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
   2007                 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
   2008       break;
   2009     case FCmpInst::FCMP_OLE: // We know that C1 <= C2
   2010       // We can only partially decide this relation.
   2011       if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
   2012         Result = 0;
   2013       else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
   2014         Result = 1;
   2015       break;
   2016     case FCmpInst::FCMP_OGE: // We known that C1 >= C2
   2017       // We can only partially decide this relation.
   2018       if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
   2019         Result = 0;
   2020       else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
   2021         Result = 1;
   2022       break;
   2023     case FCmpInst::FCMP_ONE: // We know that C1 != C2
   2024       // We can only partially decide this relation.
   2025       if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
   2026         Result = 0;
   2027       else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
   2028         Result = 1;
   2029       break;
   2030     }
   2031 
   2032     // If we evaluated the result, return it now.
   2033     if (Result != -1)
   2034       return ConstantInt::get(ResultTy, Result);
   2035 
   2036   } else {
   2037     // Evaluate the relation between the two constants, per the predicate.
   2038     int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
   2039     switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
   2040     default: llvm_unreachable("Unknown relational!");
   2041     case ICmpInst::BAD_ICMP_PREDICATE:
   2042       break;  // Couldn't determine anything about these constants.
   2043     case ICmpInst::ICMP_EQ:   // We know the constants are equal!
   2044       // If we know the constants are equal, we can decide the result of this
   2045       // computation precisely.
   2046       Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred);
   2047       break;
   2048     case ICmpInst::ICMP_ULT:
   2049       switch (pred) {
   2050       case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
   2051         Result = 1; break;
   2052       case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
   2053         Result = 0; break;
   2054       }
   2055       break;
   2056     case ICmpInst::ICMP_SLT:
   2057       switch (pred) {
   2058       case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
   2059         Result = 1; break;
   2060       case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
   2061         Result = 0; break;
   2062       }
   2063       break;
   2064     case ICmpInst::ICMP_UGT:
   2065       switch (pred) {
   2066       case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
   2067         Result = 1; break;
   2068       case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
   2069         Result = 0; break;
   2070       }
   2071       break;
   2072     case ICmpInst::ICMP_SGT:
   2073       switch (pred) {
   2074       case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
   2075         Result = 1; break;
   2076       case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
   2077         Result = 0; break;
   2078       }
   2079       break;
   2080     case ICmpInst::ICMP_ULE:
   2081       if (pred == ICmpInst::ICMP_UGT) Result = 0;
   2082       if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1;
   2083       break;
   2084     case ICmpInst::ICMP_SLE:
   2085       if (pred == ICmpInst::ICMP_SGT) Result = 0;
   2086       if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1;
   2087       break;
   2088     case ICmpInst::ICMP_UGE:
   2089       if (pred == ICmpInst::ICMP_ULT) Result = 0;
   2090       if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1;
   2091       break;
   2092     case ICmpInst::ICMP_SGE:
   2093       if (pred == ICmpInst::ICMP_SLT) Result = 0;
   2094       if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1;
   2095       break;
   2096     case ICmpInst::ICMP_NE:
   2097       if (pred == ICmpInst::ICMP_EQ) Result = 0;
   2098       if (pred == ICmpInst::ICMP_NE) Result = 1;
   2099       break;
   2100     }
   2101 
   2102     // If we evaluated the result, return it now.
   2103     if (Result != -1)
   2104       return ConstantInt::get(ResultTy, Result);
   2105 
   2106     // If the right hand side is a bitcast, try using its inverse to simplify
   2107     // it by moving it to the left hand side.  We can't do this if it would turn
   2108     // a vector compare into a scalar compare or visa versa.
   2109     if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
   2110       Constant *CE2Op0 = CE2->getOperand(0);
   2111       if (CE2->getOpcode() == Instruction::BitCast &&
   2112           CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy()) {
   2113         Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
   2114         return ConstantExpr::getICmp(pred, Inverse, CE2Op0);
   2115       }
   2116     }
   2117 
   2118     // If the left hand side is an extension, try eliminating it.
   2119     if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
   2120       if ((CE1->getOpcode() == Instruction::SExt && ICmpInst::isSigned(pred)) ||
   2121           (CE1->getOpcode() == Instruction::ZExt && !ICmpInst::isSigned(pred))){
   2122         Constant *CE1Op0 = CE1->getOperand(0);
   2123         Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
   2124         if (CE1Inverse == CE1Op0) {
   2125           // Check whether we can safely truncate the right hand side.
   2126           Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
   2127           if (ConstantExpr::getZExt(C2Inverse, C2->getType()) == C2) {
   2128             return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse);
   2129           }
   2130         }
   2131       }
   2132     }
   2133 
   2134     if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
   2135         (C1->isNullValue() && !C2->isNullValue())) {
   2136       // If C2 is a constant expr and C1 isn't, flip them around and fold the
   2137       // other way if possible.
   2138       // Also, if C1 is null and C2 isn't, flip them around.
   2139       pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
   2140       return ConstantExpr::getICmp(pred, C2, C1);
   2141     }
   2142   }
   2143   return 0;
   2144 }
   2145 
   2146 /// isInBoundsIndices - Test whether the given sequence of *normalized* indices
   2147 /// is "inbounds".
   2148 template<typename IndexTy>
   2149 static bool isInBoundsIndices(IndexTy const *Idxs, size_t NumIdx) {
   2150   // No indices means nothing that could be out of bounds.
   2151   if (NumIdx == 0) return true;
   2152 
   2153   // If the first index is zero, it's in bounds.
   2154   if (cast<Constant>(Idxs[0])->isNullValue()) return true;
   2155 
   2156   // If the first index is one and all the rest are zero, it's in bounds,
   2157   // by the one-past-the-end rule.
   2158   if (!cast<ConstantInt>(Idxs[0])->isOne())
   2159     return false;
   2160   for (unsigned i = 1, e = NumIdx; i != e; ++i)
   2161     if (!cast<Constant>(Idxs[i])->isNullValue())
   2162       return false;
   2163   return true;
   2164 }
   2165 
   2166 template<typename IndexTy>
   2167 static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
   2168                                                bool inBounds,
   2169                                                ArrayRef<IndexTy> Idxs) {
   2170   if (Idxs.empty()) return C;
   2171   Constant *Idx0 = cast<Constant>(Idxs[0]);
   2172   if ((Idxs.size() == 1 && Idx0->isNullValue()))
   2173     return C;
   2174 
   2175   if (isa<UndefValue>(C)) {
   2176     PointerType *Ptr = cast<PointerType>(C->getType());
   2177     Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs.begin(), Idxs.end());
   2178     assert(Ty != 0 && "Invalid indices for GEP!");
   2179     return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
   2180   }
   2181 
   2182   if (C->isNullValue()) {
   2183     bool isNull = true;
   2184     for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
   2185       if (!cast<Constant>(Idxs[i])->isNullValue()) {
   2186         isNull = false;
   2187         break;
   2188       }
   2189     if (isNull) {
   2190       PointerType *Ptr = cast<PointerType>(C->getType());
   2191       Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs.begin(),
   2192                                                    Idxs.end());
   2193       assert(Ty != 0 && "Invalid indices for GEP!");
   2194       return ConstantPointerNull::get(PointerType::get(Ty,
   2195                                                        Ptr->getAddressSpace()));
   2196     }
   2197   }
   2198 
   2199   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
   2200     // Combine Indices - If the source pointer to this getelementptr instruction
   2201     // is a getelementptr instruction, combine the indices of the two
   2202     // getelementptr instructions into a single instruction.
   2203     //
   2204     if (CE->getOpcode() == Instruction::GetElementPtr) {
   2205       Type *LastTy = 0;
   2206       for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
   2207            I != E; ++I)
   2208         LastTy = *I;
   2209 
   2210       if ((LastTy && LastTy->isArrayTy()) || Idx0->isNullValue()) {
   2211         SmallVector<Value*, 16> NewIndices;
   2212         NewIndices.reserve(Idxs.size() + CE->getNumOperands());
   2213         for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
   2214           NewIndices.push_back(CE->getOperand(i));
   2215 
   2216         // Add the last index of the source with the first index of the new GEP.
   2217         // Make sure to handle the case when they are actually different types.
   2218         Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
   2219         // Otherwise it must be an array.
   2220         if (!Idx0->isNullValue()) {
   2221           Type *IdxTy = Combined->getType();
   2222           if (IdxTy != Idx0->getType()) {
   2223             Type *Int64Ty = Type::getInt64Ty(IdxTy->getContext());
   2224             Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Int64Ty);
   2225             Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, Int64Ty);
   2226             Combined = ConstantExpr::get(Instruction::Add, C1, C2);
   2227           } else {
   2228             Combined =
   2229               ConstantExpr::get(Instruction::Add, Idx0, Combined);
   2230           }
   2231         }
   2232 
   2233         NewIndices.push_back(Combined);
   2234         NewIndices.append(Idxs.begin() + 1, Idxs.end());
   2235         return (inBounds && cast<GEPOperator>(CE)->isInBounds()) ?
   2236           ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0),
   2237                                                  &NewIndices[0],
   2238                                                  NewIndices.size()) :
   2239           ConstantExpr::getGetElementPtr(CE->getOperand(0),
   2240                                          &NewIndices[0],
   2241                                          NewIndices.size());
   2242       }
   2243     }
   2244 
   2245     // Implement folding of:
   2246     //    i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
   2247     //                        i64 0, i64 0)
   2248     // To: i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
   2249     //
   2250     if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) {
   2251       if (PointerType *SPT =
   2252           dyn_cast<PointerType>(CE->getOperand(0)->getType()))
   2253         if (ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
   2254           if (ArrayType *CAT =
   2255         dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
   2256             if (CAT->getElementType() == SAT->getElementType())
   2257               return inBounds ?
   2258                 ConstantExpr::getInBoundsGetElementPtr(
   2259                       (Constant*)CE->getOperand(0), Idxs.data(), Idxs.size()) :
   2260                 ConstantExpr::getGetElementPtr(
   2261                       (Constant*)CE->getOperand(0), Idxs.data(), Idxs.size());
   2262     }
   2263   }
   2264 
   2265   // Check to see if any array indices are not within the corresponding
   2266   // notional array bounds. If so, try to determine if they can be factored
   2267   // out into preceding dimensions.
   2268   bool Unknown = false;
   2269   SmallVector<Constant *, 8> NewIdxs;
   2270   Type *Ty = C->getType();
   2271   Type *Prev = 0;
   2272   for (unsigned i = 0, e = Idxs.size(); i != e;
   2273        Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
   2274     if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
   2275       if (ArrayType *ATy = dyn_cast<ArrayType>(Ty))
   2276         if (ATy->getNumElements() <= INT64_MAX &&
   2277             ATy->getNumElements() != 0 &&
   2278             CI->getSExtValue() >= (int64_t)ATy->getNumElements()) {
   2279           if (isa<SequentialType>(Prev)) {
   2280             // It's out of range, but we can factor it into the prior
   2281             // dimension.
   2282             NewIdxs.resize(Idxs.size());
   2283             ConstantInt *Factor = ConstantInt::get(CI->getType(),
   2284                                                    ATy->getNumElements());
   2285             NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
   2286 
   2287             Constant *PrevIdx = cast<Constant>(Idxs[i-1]);
   2288             Constant *Div = ConstantExpr::getSDiv(CI, Factor);
   2289 
   2290             // Before adding, extend both operands to i64 to avoid
   2291             // overflow trouble.
   2292             if (!PrevIdx->getType()->isIntegerTy(64))
   2293               PrevIdx = ConstantExpr::getSExt(PrevIdx,
   2294                                            Type::getInt64Ty(Div->getContext()));
   2295             if (!Div->getType()->isIntegerTy(64))
   2296               Div = ConstantExpr::getSExt(Div,
   2297                                           Type::getInt64Ty(Div->getContext()));
   2298 
   2299             NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div);
   2300           } else {
   2301             // It's out of range, but the prior dimension is a struct
   2302             // so we can't do anything about it.
   2303             Unknown = true;
   2304           }
   2305         }
   2306     } else {
   2307       // We don't know if it's in range or not.
   2308       Unknown = true;
   2309     }
   2310   }
   2311 
   2312   // If we did any factoring, start over with the adjusted indices.
   2313   if (!NewIdxs.empty()) {
   2314     for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
   2315       if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
   2316     return inBounds ?
   2317       ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(),
   2318                                              NewIdxs.size()) :
   2319       ConstantExpr::getGetElementPtr(C, NewIdxs.data(), NewIdxs.size());
   2320   }
   2321 
   2322   // If all indices are known integers and normalized, we can do a simple
   2323   // check for the "inbounds" property.
   2324   if (!Unknown && !inBounds &&
   2325       isa<GlobalVariable>(C) && isInBoundsIndices(Idxs.data(), Idxs.size()))
   2326     return ConstantExpr::getInBoundsGetElementPtr(C, Idxs.data(), Idxs.size());
   2327 
   2328   return 0;
   2329 }
   2330 
   2331 Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
   2332                                           bool inBounds,
   2333                                           ArrayRef<Constant *> Idxs) {
   2334   return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs);
   2335 }
   2336 
   2337 Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
   2338                                           bool inBounds,
   2339                                           ArrayRef<Value *> Idxs) {
   2340   return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs);
   2341 }
   2342