LLVM API Documentation

ConstantFold.cpp
Go to the documentation of this file.
00001 //===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements folding of constants for LLVM.  This implements the
00011 // (internal) ConstantFold.h interface, which is used by the
00012 // ConstantExpr::get* methods to automatically fold constants when possible.
00013 //
00014 // The current constant folding implementation is implemented in two pieces: the
00015 // pieces that don't need DataLayout, and the pieces that do. This is to avoid
00016 // a dependence in IR on Target.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #include "ConstantFold.h"
00021 #include "llvm/ADT/SmallVector.h"
00022 #include "llvm/IR/Constants.h"
00023 #include "llvm/IR/DerivedTypes.h"
00024 #include "llvm/IR/Function.h"
00025 #include "llvm/IR/GetElementPtrTypeIterator.h"
00026 #include "llvm/IR/GlobalAlias.h"
00027 #include "llvm/IR/GlobalVariable.h"
00028 #include "llvm/IR/Instructions.h"
00029 #include "llvm/IR/Operator.h"
00030 #include "llvm/Support/Compiler.h"
00031 #include "llvm/Support/ErrorHandling.h"
00032 #include "llvm/Support/ManagedStatic.h"
00033 #include "llvm/Support/MathExtras.h"
00034 #include <limits>
00035 using namespace llvm;
00036 
00037 //===----------------------------------------------------------------------===//
00038 //                ConstantFold*Instruction Implementations
00039 //===----------------------------------------------------------------------===//
00040 
00041 /// BitCastConstantVector - Convert the specified vector Constant node to the
00042 /// specified vector type.  At this point, we know that the elements of the
00043 /// input vector constant are all simple integer or FP values.
00044 static Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) {
00045 
00046   if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
00047   if (CV->isNullValue()) return Constant::getNullValue(DstTy);
00048 
00049   // If this cast changes element count then we can't handle it here:
00050   // doing so requires endianness information.  This should be handled by
00051   // Analysis/ConstantFolding.cpp
00052   unsigned NumElts = DstTy->getNumElements();
00053   if (NumElts != CV->getType()->getVectorNumElements())
00054     return nullptr;
00055   
00056   Type *DstEltTy = DstTy->getElementType();
00057 
00058   SmallVector<Constant*, 16> Result;
00059   Type *Ty = IntegerType::get(CV->getContext(), 32);
00060   for (unsigned i = 0; i != NumElts; ++i) {
00061     Constant *C =
00062       ConstantExpr::getExtractElement(CV, ConstantInt::get(Ty, i));
00063     C = ConstantExpr::getBitCast(C, DstEltTy);
00064     Result.push_back(C);
00065   }
00066 
00067   return ConstantVector::get(Result);
00068 }
00069 
00070 /// This function determines which opcode to use to fold two constant cast 
00071 /// expressions together. It uses CastInst::isEliminableCastPair to determine
00072 /// the opcode. Consequently its just a wrapper around that function.
00073 /// @brief Determine if it is valid to fold a cast of a cast
00074 static unsigned
00075 foldConstantCastPair(
00076   unsigned opc,          ///< opcode of the second cast constant expression
00077   ConstantExpr *Op,      ///< the first cast constant expression
00078   Type *DstTy            ///< destination type of the first cast
00079 ) {
00080   assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
00081   assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
00082   assert(CastInst::isCast(opc) && "Invalid cast opcode");
00083 
00084   // The the types and opcodes for the two Cast constant expressions
00085   Type *SrcTy = Op->getOperand(0)->getType();
00086   Type *MidTy = Op->getType();
00087   Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
00088   Instruction::CastOps secondOp = Instruction::CastOps(opc);
00089 
00090   // Assume that pointers are never more than 64 bits wide, and only use this
00091   // for the middle type. Otherwise we could end up folding away illegal
00092   // bitcasts between address spaces with different sizes.
00093   IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext());
00094 
00095   // Let CastInst::isEliminableCastPair do the heavy lifting.
00096   return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
00097                                         nullptr, FakeIntPtrTy, nullptr);
00098 }
00099 
00100 static Constant *FoldBitCast(Constant *V, Type *DestTy) {
00101   Type *SrcTy = V->getType();
00102   if (SrcTy == DestTy)
00103     return V; // no-op cast
00104 
00105   // Check to see if we are casting a pointer to an aggregate to a pointer to
00106   // the first element.  If so, return the appropriate GEP instruction.
00107   if (PointerType *PTy = dyn_cast<PointerType>(V->getType()))
00108     if (PointerType *DPTy = dyn_cast<PointerType>(DestTy))
00109       if (PTy->getAddressSpace() == DPTy->getAddressSpace()
00110           && DPTy->getElementType()->isSized()) {
00111         SmallVector<Value*, 8> IdxList;
00112         Value *Zero =
00113           Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));
00114         IdxList.push_back(Zero);
00115         Type *ElTy = PTy->getElementType();
00116         while (ElTy != DPTy->getElementType()) {
00117           if (StructType *STy = dyn_cast<StructType>(ElTy)) {
00118             if (STy->getNumElements() == 0) break;
00119             ElTy = STy->getElementType(0);
00120             IdxList.push_back(Zero);
00121           } else if (SequentialType *STy = 
00122                      dyn_cast<SequentialType>(ElTy)) {
00123             if (ElTy->isPointerTy()) break;  // Can't index into pointers!
00124             ElTy = STy->getElementType();
00125             IdxList.push_back(Zero);
00126           } else {
00127             break;
00128           }
00129         }
00130 
00131         if (ElTy == DPTy->getElementType())
00132           // This GEP is inbounds because all indices are zero.
00133           return ConstantExpr::getInBoundsGetElementPtr(V, IdxList);
00134       }
00135 
00136   // Handle casts from one vector constant to another.  We know that the src 
00137   // and dest type have the same size (otherwise its an illegal cast).
00138   if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
00139     if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
00140       assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
00141              "Not cast between same sized vectors!");
00142       SrcTy = nullptr;
00143       // First, check for null.  Undef is already handled.
00144       if (isa<ConstantAggregateZero>(V))
00145         return Constant::getNullValue(DestTy);
00146 
00147       // Handle ConstantVector and ConstantAggregateVector.
00148       return BitCastConstantVector(V, DestPTy);
00149     }
00150 
00151     // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
00152     // This allows for other simplifications (although some of them
00153     // can only be handled by Analysis/ConstantFolding.cpp).
00154     if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
00155       return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy);
00156   }
00157 
00158   // Finally, implement bitcast folding now.   The code below doesn't handle
00159   // bitcast right.
00160   if (isa<ConstantPointerNull>(V))  // ptr->ptr cast.
00161     return ConstantPointerNull::get(cast<PointerType>(DestTy));
00162 
00163   // Handle integral constant input.
00164   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00165     if (DestTy->isIntegerTy())
00166       // Integral -> Integral. This is a no-op because the bit widths must
00167       // be the same. Consequently, we just fold to V.
00168       return V;
00169 
00170     if (DestTy->isFloatingPointTy())
00171       return ConstantFP::get(DestTy->getContext(),
00172                              APFloat(DestTy->getFltSemantics(),
00173                                      CI->getValue()));
00174 
00175     // Otherwise, can't fold this (vector?)
00176     return nullptr;
00177   }
00178 
00179   // Handle ConstantFP input: FP -> Integral.
00180   if (ConstantFP *FP = dyn_cast<ConstantFP>(V))
00181     return ConstantInt::get(FP->getContext(),
00182                             FP->getValueAPF().bitcastToAPInt());
00183 
00184   return nullptr;
00185 }
00186 
00187 
00188 /// ExtractConstantBytes - V is an integer constant which only has a subset of
00189 /// its bytes used.  The bytes used are indicated by ByteStart (which is the
00190 /// first byte used, counting from the least significant byte) and ByteSize,
00191 /// which is the number of bytes used.
00192 ///
00193 /// This function analyzes the specified constant to see if the specified byte
00194 /// range can be returned as a simplified constant.  If so, the constant is
00195 /// returned, otherwise null is returned.
00196 /// 
00197 static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
00198                                       unsigned ByteSize) {
00199   assert(C->getType()->isIntegerTy() &&
00200          (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
00201          "Non-byte sized integer input");
00202   unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
00203   assert(ByteSize && "Must be accessing some piece");
00204   assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
00205   assert(ByteSize != CSize && "Should not extract everything");
00206   
00207   // Constant Integers are simple.
00208   if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
00209     APInt V = CI->getValue();
00210     if (ByteStart)
00211       V = V.lshr(ByteStart*8);
00212     V = V.trunc(ByteSize*8);
00213     return ConstantInt::get(CI->getContext(), V);
00214   }
00215   
00216   // In the input is a constant expr, we might be able to recursively simplify.
00217   // If not, we definitely can't do anything.
00218   ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
00219   if (!CE) return nullptr;
00220 
00221   switch (CE->getOpcode()) {
00222   default: return nullptr;
00223   case Instruction::Or: {
00224     Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
00225     if (!RHS)
00226       return nullptr;
00227     
00228     // X | -1 -> -1.
00229     if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS))
00230       if (RHSC->isAllOnesValue())
00231         return RHSC;
00232     
00233     Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
00234     if (!LHS)
00235       return nullptr;
00236     return ConstantExpr::getOr(LHS, RHS);
00237   }
00238   case Instruction::And: {
00239     Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
00240     if (!RHS)
00241       return nullptr;
00242     
00243     // X & 0 -> 0.
00244     if (RHS->isNullValue())
00245       return RHS;
00246     
00247     Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
00248     if (!LHS)
00249       return nullptr;
00250     return ConstantExpr::getAnd(LHS, RHS);
00251   }
00252   case Instruction::LShr: {
00253     ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
00254     if (!Amt)
00255       return nullptr;
00256     unsigned ShAmt = Amt->getZExtValue();
00257     // Cannot analyze non-byte shifts.
00258     if ((ShAmt & 7) != 0)
00259       return nullptr;
00260     ShAmt >>= 3;
00261     
00262     // If the extract is known to be all zeros, return zero.
00263     if (ByteStart >= CSize-ShAmt)
00264       return Constant::getNullValue(IntegerType::get(CE->getContext(),
00265                                                      ByteSize*8));
00266     // If the extract is known to be fully in the input, extract it.
00267     if (ByteStart+ByteSize+ShAmt <= CSize)
00268       return ExtractConstantBytes(CE->getOperand(0), ByteStart+ShAmt, ByteSize);
00269     
00270     // TODO: Handle the 'partially zero' case.
00271     return nullptr;
00272   }
00273     
00274   case Instruction::Shl: {
00275     ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
00276     if (!Amt)
00277       return nullptr;
00278     unsigned ShAmt = Amt->getZExtValue();
00279     // Cannot analyze non-byte shifts.
00280     if ((ShAmt & 7) != 0)
00281       return nullptr;
00282     ShAmt >>= 3;
00283     
00284     // If the extract is known to be all zeros, return zero.
00285     if (ByteStart+ByteSize <= ShAmt)
00286       return Constant::getNullValue(IntegerType::get(CE->getContext(),
00287                                                      ByteSize*8));
00288     // If the extract is known to be fully in the input, extract it.
00289     if (ByteStart >= ShAmt)
00290       return ExtractConstantBytes(CE->getOperand(0), ByteStart-ShAmt, ByteSize);
00291     
00292     // TODO: Handle the 'partially zero' case.
00293     return nullptr;
00294   }
00295       
00296   case Instruction::ZExt: {
00297     unsigned SrcBitSize =
00298       cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth();
00299     
00300     // If extracting something that is completely zero, return 0.
00301     if (ByteStart*8 >= SrcBitSize)
00302       return Constant::getNullValue(IntegerType::get(CE->getContext(),
00303                                                      ByteSize*8));
00304 
00305     // If exactly extracting the input, return it.
00306     if (ByteStart == 0 && ByteSize*8 == SrcBitSize)
00307       return CE->getOperand(0);
00308     
00309     // If extracting something completely in the input, if if the input is a
00310     // multiple of 8 bits, recurse.
00311     if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize)
00312       return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize);
00313       
00314     // Otherwise, if extracting a subset of the input, which is not multiple of
00315     // 8 bits, do a shift and trunc to get the bits.
00316     if ((ByteStart+ByteSize)*8 < SrcBitSize) {
00317       assert((SrcBitSize&7) && "Shouldn't get byte sized case here");
00318       Constant *Res = CE->getOperand(0);
00319       if (ByteStart)
00320         Res = ConstantExpr::getLShr(Res, 
00321                                  ConstantInt::get(Res->getType(), ByteStart*8));
00322       return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(),
00323                                                           ByteSize*8));
00324     }
00325     
00326     // TODO: Handle the 'partially zero' case.
00327     return nullptr;
00328   }
00329   }
00330 }
00331 
00332 /// getFoldedSizeOf - Return a ConstantExpr with type DestTy for sizeof
00333 /// on Ty, with any known factors factored out. If Folded is false,
00334 /// return null if no factoring was possible, to avoid endlessly
00335 /// bouncing an unfoldable expression back into the top-level folder.
00336 ///
00337 static Constant *getFoldedSizeOf(Type *Ty, Type *DestTy,
00338                                  bool Folded) {
00339   if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
00340     Constant *N = ConstantInt::get(DestTy, ATy->getNumElements());
00341     Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
00342     return ConstantExpr::getNUWMul(E, N);
00343   }
00344 
00345   if (StructType *STy = dyn_cast<StructType>(Ty))
00346     if (!STy->isPacked()) {
00347       unsigned NumElems = STy->getNumElements();
00348       // An empty struct has size zero.
00349       if (NumElems == 0)
00350         return ConstantExpr::getNullValue(DestTy);
00351       // Check for a struct with all members having the same size.
00352       Constant *MemberSize =
00353         getFoldedSizeOf(STy->getElementType(0), DestTy, true);
00354       bool AllSame = true;
00355       for (unsigned i = 1; i != NumElems; ++i)
00356         if (MemberSize !=
00357             getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
00358           AllSame = false;
00359           break;
00360         }
00361       if (AllSame) {
00362         Constant *N = ConstantInt::get(DestTy, NumElems);
00363         return ConstantExpr::getNUWMul(MemberSize, N);
00364       }
00365     }
00366 
00367   // Pointer size doesn't depend on the pointee type, so canonicalize them
00368   // to an arbitrary pointee.
00369   if (PointerType *PTy = dyn_cast<PointerType>(Ty))
00370     if (!PTy->getElementType()->isIntegerTy(1))
00371       return
00372         getFoldedSizeOf(PointerType::get(IntegerType::get(PTy->getContext(), 1),
00373                                          PTy->getAddressSpace()),
00374                         DestTy, true);
00375 
00376   // If there's no interesting folding happening, bail so that we don't create
00377   // a constant that looks like it needs folding but really doesn't.
00378   if (!Folded)
00379     return nullptr;
00380 
00381   // Base case: Get a regular sizeof expression.
00382   Constant *C = ConstantExpr::getSizeOf(Ty);
00383   C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
00384                                                     DestTy, false),
00385                             C, DestTy);
00386   return C;
00387 }
00388 
00389 /// getFoldedAlignOf - Return a ConstantExpr with type DestTy for alignof
00390 /// on Ty, with any known factors factored out. If Folded is false,
00391 /// return null if no factoring was possible, to avoid endlessly
00392 /// bouncing an unfoldable expression back into the top-level folder.
00393 ///
00394 static Constant *getFoldedAlignOf(Type *Ty, Type *DestTy,
00395                                   bool Folded) {
00396   // The alignment of an array is equal to the alignment of the
00397   // array element. Note that this is not always true for vectors.
00398   if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
00399     Constant *C = ConstantExpr::getAlignOf(ATy->getElementType());
00400     C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
00401                                                       DestTy,
00402                                                       false),
00403                               C, DestTy);
00404     return C;
00405   }
00406 
00407   if (StructType *STy = dyn_cast<StructType>(Ty)) {
00408     // Packed structs always have an alignment of 1.
00409     if (STy->isPacked())
00410       return ConstantInt::get(DestTy, 1);
00411 
00412     // Otherwise, struct alignment is the maximum alignment of any member.
00413     // Without target data, we can't compare much, but we can check to see
00414     // if all the members have the same alignment.
00415     unsigned NumElems = STy->getNumElements();
00416     // An empty struct has minimal alignment.
00417     if (NumElems == 0)
00418       return ConstantInt::get(DestTy, 1);
00419     // Check for a struct with all members having the same alignment.
00420     Constant *MemberAlign =
00421       getFoldedAlignOf(STy->getElementType(0), DestTy, true);
00422     bool AllSame = true;
00423     for (unsigned i = 1; i != NumElems; ++i)
00424       if (MemberAlign != getFoldedAlignOf(STy->getElementType(i), DestTy, true)) {
00425         AllSame = false;
00426         break;
00427       }
00428     if (AllSame)
00429       return MemberAlign;
00430   }
00431 
00432   // Pointer alignment doesn't depend on the pointee type, so canonicalize them
00433   // to an arbitrary pointee.
00434   if (PointerType *PTy = dyn_cast<PointerType>(Ty))
00435     if (!PTy->getElementType()->isIntegerTy(1))
00436       return
00437         getFoldedAlignOf(PointerType::get(IntegerType::get(PTy->getContext(),
00438                                                            1),
00439                                           PTy->getAddressSpace()),
00440                          DestTy, true);
00441 
00442   // If there's no interesting folding happening, bail so that we don't create
00443   // a constant that looks like it needs folding but really doesn't.
00444   if (!Folded)
00445     return nullptr;
00446 
00447   // Base case: Get a regular alignof expression.
00448   Constant *C = ConstantExpr::getAlignOf(Ty);
00449   C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
00450                                                     DestTy, false),
00451                             C, DestTy);
00452   return C;
00453 }
00454 
00455 /// getFoldedOffsetOf - Return a ConstantExpr with type DestTy for offsetof
00456 /// on Ty and FieldNo, with any known factors factored out. If Folded is false,
00457 /// return null if no factoring was possible, to avoid endlessly
00458 /// bouncing an unfoldable expression back into the top-level folder.
00459 ///
00460 static Constant *getFoldedOffsetOf(Type *Ty, Constant *FieldNo,
00461                                    Type *DestTy,
00462                                    bool Folded) {
00463   if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
00464     Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, false,
00465                                                                 DestTy, false),
00466                                         FieldNo, DestTy);
00467     Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
00468     return ConstantExpr::getNUWMul(E, N);
00469   }
00470 
00471   if (StructType *STy = dyn_cast<StructType>(Ty))
00472     if (!STy->isPacked()) {
00473       unsigned NumElems = STy->getNumElements();
00474       // An empty struct has no members.
00475       if (NumElems == 0)
00476         return nullptr;
00477       // Check for a struct with all members having the same size.
00478       Constant *MemberSize =
00479         getFoldedSizeOf(STy->getElementType(0), DestTy, true);
00480       bool AllSame = true;
00481       for (unsigned i = 1; i != NumElems; ++i)
00482         if (MemberSize !=
00483             getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
00484           AllSame = false;
00485           break;
00486         }
00487       if (AllSame) {
00488         Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo,
00489                                                                     false,
00490                                                                     DestTy,
00491                                                                     false),
00492                                             FieldNo, DestTy);
00493         return ConstantExpr::getNUWMul(MemberSize, N);
00494       }
00495     }
00496 
00497   // If there's no interesting folding happening, bail so that we don't create
00498   // a constant that looks like it needs folding but really doesn't.
00499   if (!Folded)
00500     return nullptr;
00501 
00502   // Base case: Get a regular offsetof expression.
00503   Constant *C = ConstantExpr::getOffsetOf(Ty, FieldNo);
00504   C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
00505                                                     DestTy, false),
00506                             C, DestTy);
00507   return C;
00508 }
00509 
00510 Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
00511                                             Type *DestTy) {
00512   if (isa<UndefValue>(V)) {
00513     // zext(undef) = 0, because the top bits will be zero.
00514     // sext(undef) = 0, because the top bits will all be the same.
00515     // [us]itofp(undef) = 0, because the result value is bounded.
00516     if (opc == Instruction::ZExt || opc == Instruction::SExt ||
00517         opc == Instruction::UIToFP || opc == Instruction::SIToFP)
00518       return Constant::getNullValue(DestTy);
00519     return UndefValue::get(DestTy);
00520   }
00521 
00522   if (V->isNullValue() && !DestTy->isX86_MMXTy())
00523     return Constant::getNullValue(DestTy);
00524 
00525   // If the cast operand is a constant expression, there's a few things we can
00526   // do to try to simplify it.
00527   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
00528     if (CE->isCast()) {
00529       // Try hard to fold cast of cast because they are often eliminable.
00530       if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
00531         return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
00532     } else if (CE->getOpcode() == Instruction::GetElementPtr &&
00533                // Do not fold addrspacecast (gep 0, .., 0). It might make the
00534                // addrspacecast uncanonicalized.
00535                opc != Instruction::AddrSpaceCast) {
00536       // If all of the indexes in the GEP are null values, there is no pointer
00537       // adjustment going on.  We might as well cast the source pointer.
00538       bool isAllNull = true;
00539       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
00540         if (!CE->getOperand(i)->isNullValue()) {
00541           isAllNull = false;
00542           break;
00543         }
00544       if (isAllNull)
00545         // This is casting one pointer type to another, always BitCast
00546         return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
00547     }
00548   }
00549 
00550   // If the cast operand is a constant vector, perform the cast by
00551   // operating on each element. In the cast of bitcasts, the element
00552   // count may be mismatched; don't attempt to handle that here.
00553   if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
00554       DestTy->isVectorTy() &&
00555       DestTy->getVectorNumElements() == V->getType()->getVectorNumElements()) {
00556     SmallVector<Constant*, 16> res;
00557     VectorType *DestVecTy = cast<VectorType>(DestTy);
00558     Type *DstEltTy = DestVecTy->getElementType();
00559     Type *Ty = IntegerType::get(V->getContext(), 32);
00560     for (unsigned i = 0, e = V->getType()->getVectorNumElements(); i != e; ++i) {
00561       Constant *C =
00562         ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
00563       res.push_back(ConstantExpr::getCast(opc, C, DstEltTy));
00564     }
00565     return ConstantVector::get(res);
00566   }
00567 
00568   // We actually have to do a cast now. Perform the cast according to the
00569   // opcode specified.
00570   switch (opc) {
00571   default:
00572     llvm_unreachable("Failed to cast constant expression");
00573   case Instruction::FPTrunc:
00574   case Instruction::FPExt:
00575     if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
00576       bool ignored;
00577       APFloat Val = FPC->getValueAPF();
00578       Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf :
00579                   DestTy->isFloatTy() ? APFloat::IEEEsingle :
00580                   DestTy->isDoubleTy() ? APFloat::IEEEdouble :
00581                   DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended :
00582                   DestTy->isFP128Ty() ? APFloat::IEEEquad :
00583                   DestTy->isPPC_FP128Ty() ? APFloat::PPCDoubleDouble :
00584                   APFloat::Bogus,
00585                   APFloat::rmNearestTiesToEven, &ignored);
00586       return ConstantFP::get(V->getContext(), Val);
00587     }
00588     return nullptr; // Can't fold.
00589   case Instruction::FPToUI: 
00590   case Instruction::FPToSI:
00591     if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
00592       const APFloat &V = FPC->getValueAPF();
00593       bool ignored;
00594       uint64_t x[2]; 
00595       uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
00596       (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
00597                                 APFloat::rmTowardZero, &ignored);
00598       APInt Val(DestBitWidth, x);
00599       return ConstantInt::get(FPC->getContext(), Val);
00600     }
00601     return nullptr; // Can't fold.
00602   case Instruction::IntToPtr:   //always treated as unsigned
00603     if (V->isNullValue())       // Is it an integral null value?
00604       return ConstantPointerNull::get(cast<PointerType>(DestTy));
00605     return nullptr;                   // Other pointer types cannot be casted
00606   case Instruction::PtrToInt:   // always treated as unsigned
00607     // Is it a null pointer value?
00608     if (V->isNullValue())
00609       return ConstantInt::get(DestTy, 0);
00610     // If this is a sizeof-like expression, pull out multiplications by
00611     // known factors to expose them to subsequent folding. If it's an
00612     // alignof-like expression, factor out known factors.
00613     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00614       if (CE->getOpcode() == Instruction::GetElementPtr &&
00615           CE->getOperand(0)->isNullValue()) {
00616         Type *Ty =
00617           cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
00618         if (CE->getNumOperands() == 2) {
00619           // Handle a sizeof-like expression.
00620           Constant *Idx = CE->getOperand(1);
00621           bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne();
00622           if (Constant *C = getFoldedSizeOf(Ty, DestTy, !isOne)) {
00623             Idx = ConstantExpr::getCast(CastInst::getCastOpcode(Idx, true,
00624                                                                 DestTy, false),
00625                                         Idx, DestTy);
00626             return ConstantExpr::getMul(C, Idx);
00627           }
00628         } else if (CE->getNumOperands() == 3 &&
00629                    CE->getOperand(1)->isNullValue()) {
00630           // Handle an alignof-like expression.
00631           if (StructType *STy = dyn_cast<StructType>(Ty))
00632             if (!STy->isPacked()) {
00633               ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2));
00634               if (CI->isOne() &&
00635                   STy->getNumElements() == 2 &&
00636                   STy->getElementType(0)->isIntegerTy(1)) {
00637                 return getFoldedAlignOf(STy->getElementType(1), DestTy, false);
00638               }
00639             }
00640           // Handle an offsetof-like expression.
00641           if (Ty->isStructTy() || Ty->isArrayTy()) {
00642             if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2),
00643                                                 DestTy, false))
00644               return C;
00645           }
00646         }
00647       }
00648     // Other pointer types cannot be casted
00649     return nullptr;
00650   case Instruction::UIToFP:
00651   case Instruction::SIToFP:
00652     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00653       APInt api = CI->getValue();
00654       APFloat apf(DestTy->getFltSemantics(),
00655                   APInt::getNullValue(DestTy->getPrimitiveSizeInBits()));
00656       (void)apf.convertFromAPInt(api, 
00657                                  opc==Instruction::SIToFP,
00658                                  APFloat::rmNearestTiesToEven);
00659       return ConstantFP::get(V->getContext(), apf);
00660     }
00661     return nullptr;
00662   case Instruction::ZExt:
00663     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00664       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
00665       return ConstantInt::get(V->getContext(),
00666                               CI->getValue().zext(BitWidth));
00667     }
00668     return nullptr;
00669   case Instruction::SExt:
00670     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00671       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
00672       return ConstantInt::get(V->getContext(),
00673                               CI->getValue().sext(BitWidth));
00674     }
00675     return nullptr;
00676   case Instruction::Trunc: {
00677     if (V->getType()->isVectorTy())
00678       return nullptr;
00679 
00680     uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
00681     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00682       return ConstantInt::get(V->getContext(),
00683                               CI->getValue().trunc(DestBitWidth));
00684     }
00685     
00686     // The input must be a constantexpr.  See if we can simplify this based on
00687     // the bytes we are demanding.  Only do this if the source and dest are an
00688     // even multiple of a byte.
00689     if ((DestBitWidth & 7) == 0 &&
00690         (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
00691       if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
00692         return Res;
00693       
00694     return nullptr;
00695   }
00696   case Instruction::BitCast:
00697     return FoldBitCast(V, DestTy);
00698   case Instruction::AddrSpaceCast:
00699     return nullptr;
00700   }
00701 }
00702 
00703 Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
00704                                               Constant *V1, Constant *V2) {
00705   // Check for i1 and vector true/false conditions.
00706   if (Cond->isNullValue()) return V2;
00707   if (Cond->isAllOnesValue()) return V1;
00708 
00709   // If the condition is a vector constant, fold the result elementwise.
00710   if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
00711     SmallVector<Constant*, 16> Result;
00712     Type *Ty = IntegerType::get(CondV->getContext(), 32);
00713     for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){
00714       Constant *V;
00715       Constant *V1Element = ConstantExpr::getExtractElement(V1,
00716                                                     ConstantInt::get(Ty, i));
00717       Constant *V2Element = ConstantExpr::getExtractElement(V2,
00718                                                     ConstantInt::get(Ty, i));
00719       Constant *Cond = dyn_cast<Constant>(CondV->getOperand(i));
00720       if (V1Element == V2Element) {
00721         V = V1Element;
00722       } else if (isa<UndefValue>(Cond)) {
00723         V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
00724       } else {
00725         if (!isa<ConstantInt>(Cond)) break;
00726         V = Cond->isNullValue() ? V2Element : V1Element;
00727       }
00728       Result.push_back(V);
00729     }
00730     
00731     // If we were able to build the vector, return it.
00732     if (Result.size() == V1->getType()->getVectorNumElements())
00733       return ConstantVector::get(Result);
00734   }
00735 
00736   if (isa<UndefValue>(Cond)) {
00737     if (isa<UndefValue>(V1)) return V1;
00738     return V2;
00739   }
00740   if (isa<UndefValue>(V1)) return V2;
00741   if (isa<UndefValue>(V2)) return V1;
00742   if (V1 == V2) return V1;
00743 
00744   if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) {
00745     if (TrueVal->getOpcode() == Instruction::Select)
00746       if (TrueVal->getOperand(0) == Cond)
00747         return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2);
00748   }
00749   if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) {
00750     if (FalseVal->getOpcode() == Instruction::Select)
00751       if (FalseVal->getOperand(0) == Cond)
00752         return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2));
00753   }
00754 
00755   return nullptr;
00756 }
00757 
00758 Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
00759                                                       Constant *Idx) {
00760   if (isa<UndefValue>(Val))  // ee(undef, x) -> undef
00761     return UndefValue::get(Val->getType()->getVectorElementType());
00762   if (Val->isNullValue())  // ee(zero, x) -> zero
00763     return Constant::getNullValue(Val->getType()->getVectorElementType());
00764   // ee({w,x,y,z}, undef) -> undef
00765   if (isa<UndefValue>(Idx))
00766     return UndefValue::get(Val->getType()->getVectorElementType());
00767 
00768   if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
00769     uint64_t Index = CIdx->getZExtValue();
00770     // ee({w,x,y,z}, wrong_value) -> undef
00771     if (Index >= Val->getType()->getVectorNumElements())
00772       return UndefValue::get(Val->getType()->getVectorElementType());
00773     return Val->getAggregateElement(Index);
00774   }
00775   return nullptr;
00776 }
00777 
00778 Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val,
00779                                                      Constant *Elt,
00780                                                      Constant *Idx) {
00781   ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
00782   if (!CIdx) return nullptr;
00783   const APInt &IdxVal = CIdx->getValue();
00784   
00785   SmallVector<Constant*, 16> Result;
00786   Type *Ty = IntegerType::get(Val->getContext(), 32);
00787   for (unsigned i = 0, e = Val->getType()->getVectorNumElements(); i != e; ++i){
00788     if (i == IdxVal) {
00789       Result.push_back(Elt);
00790       continue;
00791     }
00792     
00793     Constant *C =
00794       ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
00795     Result.push_back(C);
00796   }
00797   
00798   return ConstantVector::get(Result);
00799 }
00800 
00801 Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1,
00802                                                      Constant *V2,
00803                                                      Constant *Mask) {
00804   unsigned MaskNumElts = Mask->getType()->getVectorNumElements();
00805   Type *EltTy = V1->getType()->getVectorElementType();
00806 
00807   // Undefined shuffle mask -> undefined value.
00808   if (isa<UndefValue>(Mask))
00809     return UndefValue::get(VectorType::get(EltTy, MaskNumElts));
00810 
00811   // Don't break the bitcode reader hack.
00812   if (isa<ConstantExpr>(Mask)) return nullptr;
00813   
00814   unsigned SrcNumElts = V1->getType()->getVectorNumElements();
00815 
00816   // Loop over the shuffle mask, evaluating each element.
00817   SmallVector<Constant*, 32> Result;
00818   for (unsigned i = 0; i != MaskNumElts; ++i) {
00819     int Elt = ShuffleVectorInst::getMaskValue(Mask, i);
00820     if (Elt == -1) {
00821       Result.push_back(UndefValue::get(EltTy));
00822       continue;
00823     }
00824     Constant *InElt;
00825     if (unsigned(Elt) >= SrcNumElts*2)
00826       InElt = UndefValue::get(EltTy);
00827     else if (unsigned(Elt) >= SrcNumElts) {
00828       Type *Ty = IntegerType::get(V2->getContext(), 32);
00829       InElt =
00830         ConstantExpr::getExtractElement(V2,
00831                                         ConstantInt::get(Ty, Elt - SrcNumElts));
00832     } else {
00833       Type *Ty = IntegerType::get(V1->getContext(), 32);
00834       InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt));
00835     }
00836     Result.push_back(InElt);
00837   }
00838 
00839   return ConstantVector::get(Result);
00840 }
00841 
00842 Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
00843                                                     ArrayRef<unsigned> Idxs) {
00844   // Base case: no indices, so return the entire value.
00845   if (Idxs.empty())
00846     return Agg;
00847 
00848   if (Constant *C = Agg->getAggregateElement(Idxs[0]))
00849     return ConstantFoldExtractValueInstruction(C, Idxs.slice(1));
00850 
00851   return nullptr;
00852 }
00853 
00854 Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
00855                                                    Constant *Val,
00856                                                    ArrayRef<unsigned> Idxs) {
00857   // Base case: no indices, so replace the entire value.
00858   if (Idxs.empty())
00859     return Val;
00860 
00861   unsigned NumElts;
00862   if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
00863     NumElts = ST->getNumElements();
00864   else if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType()))
00865     NumElts = AT->getNumElements();
00866   else
00867     NumElts = Agg->getType()->getVectorNumElements();
00868 
00869   SmallVector<Constant*, 32> Result;
00870   for (unsigned i = 0; i != NumElts; ++i) {
00871     Constant *C = Agg->getAggregateElement(i);
00872     if (!C) return nullptr;
00873 
00874     if (Idxs[0] == i)
00875       C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1));
00876     
00877     Result.push_back(C);
00878   }
00879   
00880   if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
00881     return ConstantStruct::get(ST, Result);
00882   if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType()))
00883     return ConstantArray::get(AT, Result);
00884   return ConstantVector::get(Result);
00885 }
00886 
00887 
00888 Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
00889                                               Constant *C1, Constant *C2) {
00890   // Handle UndefValue up front.
00891   if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
00892     switch (Opcode) {
00893     case Instruction::Xor:
00894       if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
00895         // Handle undef ^ undef -> 0 special case. This is a common
00896         // idiom (misuse).
00897         return Constant::getNullValue(C1->getType());
00898       // Fallthrough
00899     case Instruction::Add:
00900     case Instruction::Sub:
00901       return UndefValue::get(C1->getType());
00902     case Instruction::And:
00903       if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
00904         return C1;
00905       return Constant::getNullValue(C1->getType());   // undef & X -> 0
00906     case Instruction::Mul: {
00907       ConstantInt *CI;
00908       // X * undef -> undef   if X is odd or undef
00909       if (((CI = dyn_cast<ConstantInt>(C1)) && CI->getValue()[0]) ||
00910           ((CI = dyn_cast<ConstantInt>(C2)) && CI->getValue()[0]) ||
00911           (isa<UndefValue>(C1) && isa<UndefValue>(C2)))
00912         return UndefValue::get(C1->getType());
00913 
00914       // X * undef -> 0       otherwise
00915       return Constant::getNullValue(C1->getType());
00916     }
00917     case Instruction::UDiv:
00918     case Instruction::SDiv:
00919       // undef / 1 -> undef
00920       if (Opcode == Instruction::UDiv || Opcode == Instruction::SDiv)
00921         if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2))
00922           if (CI2->isOne())
00923             return C1;
00924       // FALL THROUGH
00925     case Instruction::URem:
00926     case Instruction::SRem:
00927       if (!isa<UndefValue>(C2))                    // undef / X -> 0
00928         return Constant::getNullValue(C1->getType());
00929       return C2;                                   // X / undef -> undef
00930     case Instruction::Or:                          // X | undef -> -1
00931       if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
00932         return C1;
00933       return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
00934     case Instruction::LShr:
00935       if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
00936         return C1;                                  // undef lshr undef -> undef
00937       return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
00938                                                     // undef lshr X -> 0
00939     case Instruction::AShr:
00940       if (!isa<UndefValue>(C2))                     // undef ashr X --> all ones
00941         return Constant::getAllOnesValue(C1->getType());
00942       else if (isa<UndefValue>(C1)) 
00943         return C1;                                  // undef ashr undef -> undef
00944       else
00945         return C1;                                  // X ashr undef --> X
00946     case Instruction::Shl:
00947       if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
00948         return C1;                                  // undef shl undef -> undef
00949       // undef << X -> 0   or   X << undef -> 0
00950       return Constant::getNullValue(C1->getType());
00951     }
00952   }
00953 
00954   // Handle simplifications when the RHS is a constant int.
00955   if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
00956     switch (Opcode) {
00957     case Instruction::Add:
00958       if (CI2->equalsInt(0)) return C1;                         // X + 0 == X
00959       break;
00960     case Instruction::Sub:
00961       if (CI2->equalsInt(0)) return C1;                         // X - 0 == X
00962       break;
00963     case Instruction::Mul:
00964       if (CI2->equalsInt(0)) return C2;                         // X * 0 == 0
00965       if (CI2->equalsInt(1))
00966         return C1;                                              // X * 1 == X
00967       break;
00968     case Instruction::UDiv:
00969     case Instruction::SDiv:
00970       if (CI2->equalsInt(1))
00971         return C1;                                            // X / 1 == X
00972       if (CI2->equalsInt(0))
00973         return UndefValue::get(CI2->getType());               // X / 0 == undef
00974       break;
00975     case Instruction::URem:
00976     case Instruction::SRem:
00977       if (CI2->equalsInt(1))
00978         return Constant::getNullValue(CI2->getType());        // X % 1 == 0
00979       if (CI2->equalsInt(0))
00980         return UndefValue::get(CI2->getType());               // X % 0 == undef
00981       break;
00982     case Instruction::And:
00983       if (CI2->isZero()) return C2;                           // X & 0 == 0
00984       if (CI2->isAllOnesValue())
00985         return C1;                                            // X & -1 == X
00986 
00987       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
00988         // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
00989         if (CE1->getOpcode() == Instruction::ZExt) {
00990           unsigned DstWidth = CI2->getType()->getBitWidth();
00991           unsigned SrcWidth =
00992             CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
00993           APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
00994           if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
00995             return C1;
00996         }
00997 
00998         // If and'ing the address of a global with a constant, fold it.
00999         if (CE1->getOpcode() == Instruction::PtrToInt && 
01000             isa<GlobalValue>(CE1->getOperand(0))) {
01001           GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
01002 
01003           // Functions are at least 4-byte aligned.
01004           unsigned GVAlign = GV->getAlignment();
01005           if (isa<Function>(GV))
01006             GVAlign = std::max(GVAlign, 4U);
01007 
01008           if (GVAlign > 1) {
01009             unsigned DstWidth = CI2->getType()->getBitWidth();
01010             unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
01011             APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
01012 
01013             // If checking bits we know are clear, return zero.
01014             if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
01015               return Constant::getNullValue(CI2->getType());
01016           }
01017         }
01018       }
01019       break;
01020     case Instruction::Or:
01021       if (CI2->equalsInt(0)) return C1;    // X | 0 == X
01022       if (CI2->isAllOnesValue())
01023         return C2;                         // X | -1 == -1
01024       break;
01025     case Instruction::Xor:
01026       if (CI2->equalsInt(0)) return C1;    // X ^ 0 == X
01027 
01028       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
01029         switch (CE1->getOpcode()) {
01030         default: break;
01031         case Instruction::ICmp:
01032         case Instruction::FCmp:
01033           // cmp pred ^ true -> cmp !pred
01034           assert(CI2->equalsInt(1));
01035           CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
01036           pred = CmpInst::getInversePredicate(pred);
01037           return ConstantExpr::getCompare(pred, CE1->getOperand(0),
01038                                           CE1->getOperand(1));
01039         }
01040       }
01041       break;
01042     case Instruction::AShr:
01043       // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
01044       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
01045         if (CE1->getOpcode() == Instruction::ZExt)  // Top bits known zero.
01046           return ConstantExpr::getLShr(C1, C2);
01047       break;
01048     }
01049   } else if (isa<ConstantInt>(C1)) {
01050     // If C1 is a ConstantInt and C2 is not, swap the operands.
01051     if (Instruction::isCommutative(Opcode))
01052       return ConstantExpr::get(Opcode, C2, C1);
01053   }
01054 
01055   // At this point we know neither constant is an UndefValue.
01056   if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
01057     if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
01058       const APInt &C1V = CI1->getValue();
01059       const APInt &C2V = CI2->getValue();
01060       switch (Opcode) {
01061       default:
01062         break;
01063       case Instruction::Add:     
01064         return ConstantInt::get(CI1->getContext(), C1V + C2V);
01065       case Instruction::Sub:     
01066         return ConstantInt::get(CI1->getContext(), C1V - C2V);
01067       case Instruction::Mul:     
01068         return ConstantInt::get(CI1->getContext(), C1V * C2V);
01069       case Instruction::UDiv:
01070         assert(!CI2->isNullValue() && "Div by zero handled above");
01071         return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V));
01072       case Instruction::SDiv:
01073         assert(!CI2->isNullValue() && "Div by zero handled above");
01074         if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
01075           return UndefValue::get(CI1->getType());   // MIN_INT / -1 -> undef
01076         return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V));
01077       case Instruction::URem:
01078         assert(!CI2->isNullValue() && "Div by zero handled above");
01079         return ConstantInt::get(CI1->getContext(), C1V.urem(C2V));
01080       case Instruction::SRem:
01081         assert(!CI2->isNullValue() && "Div by zero handled above");
01082         if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
01083           return UndefValue::get(CI1->getType());   // MIN_INT % -1 -> undef
01084         return ConstantInt::get(CI1->getContext(), C1V.srem(C2V));
01085       case Instruction::And:
01086         return ConstantInt::get(CI1->getContext(), C1V & C2V);
01087       case Instruction::Or:
01088         return ConstantInt::get(CI1->getContext(), C1V | C2V);
01089       case Instruction::Xor:
01090         return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
01091       case Instruction::Shl: {
01092         uint32_t shiftAmt = C2V.getZExtValue();
01093         if (shiftAmt < C1V.getBitWidth())
01094           return ConstantInt::get(CI1->getContext(), C1V.shl(shiftAmt));
01095         else
01096           return UndefValue::get(C1->getType()); // too big shift is undef
01097       }
01098       case Instruction::LShr: {
01099         uint32_t shiftAmt = C2V.getZExtValue();
01100         if (shiftAmt < C1V.getBitWidth())
01101           return ConstantInt::get(CI1->getContext(), C1V.lshr(shiftAmt));
01102         else
01103           return UndefValue::get(C1->getType()); // too big shift is undef
01104       }
01105       case Instruction::AShr: {
01106         uint32_t shiftAmt = C2V.getZExtValue();
01107         if (shiftAmt < C1V.getBitWidth())
01108           return ConstantInt::get(CI1->getContext(), C1V.ashr(shiftAmt));
01109         else
01110           return UndefValue::get(C1->getType()); // too big shift is undef
01111       }
01112       }
01113     }
01114 
01115     switch (Opcode) {
01116     case Instruction::SDiv:
01117     case Instruction::UDiv:
01118     case Instruction::URem:
01119     case Instruction::SRem:
01120     case Instruction::LShr:
01121     case Instruction::AShr:
01122     case Instruction::Shl:
01123       if (CI1->equalsInt(0)) return C1;
01124       break;
01125     default:
01126       break;
01127     }
01128   } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
01129     if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
01130       APFloat C1V = CFP1->getValueAPF();
01131       APFloat C2V = CFP2->getValueAPF();
01132       APFloat C3V = C1V;  // copy for modification
01133       switch (Opcode) {
01134       default:                   
01135         break;
01136       case Instruction::FAdd:
01137         (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
01138         return ConstantFP::get(C1->getContext(), C3V);
01139       case Instruction::FSub:
01140         (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
01141         return ConstantFP::get(C1->getContext(), C3V);
01142       case Instruction::FMul:
01143         (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
01144         return ConstantFP::get(C1->getContext(), C3V);
01145       case Instruction::FDiv:
01146         (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
01147         return ConstantFP::get(C1->getContext(), C3V);
01148       case Instruction::FRem:
01149         (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
01150         return ConstantFP::get(C1->getContext(), C3V);
01151       }
01152     }
01153   } else if (VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
01154     // Perform elementwise folding.
01155     SmallVector<Constant*, 16> Result;
01156     Type *Ty = IntegerType::get(VTy->getContext(), 32);
01157     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
01158       Constant *LHS =
01159         ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i));
01160       Constant *RHS =
01161         ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i));
01162       
01163       Result.push_back(ConstantExpr::get(Opcode, LHS, RHS));
01164     }
01165     
01166     return ConstantVector::get(Result);
01167   }
01168 
01169   if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
01170     // There are many possible foldings we could do here.  We should probably
01171     // at least fold add of a pointer with an integer into the appropriate
01172     // getelementptr.  This will improve alias analysis a bit.
01173 
01174     // Given ((a + b) + c), if (b + c) folds to something interesting, return
01175     // (a + (b + c)).
01176     if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
01177       Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
01178       if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
01179         return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
01180     }
01181   } else if (isa<ConstantExpr>(C2)) {
01182     // If C2 is a constant expr and C1 isn't, flop them around and fold the
01183     // other way if possible.
01184     if (Instruction::isCommutative(Opcode))
01185       return ConstantFoldBinaryInstruction(Opcode, C2, C1);
01186   }
01187 
01188   // i1 can be simplified in many cases.
01189   if (C1->getType()->isIntegerTy(1)) {
01190     switch (Opcode) {
01191     case Instruction::Add:
01192     case Instruction::Sub:
01193       return ConstantExpr::getXor(C1, C2);
01194     case Instruction::Mul:
01195       return ConstantExpr::getAnd(C1, C2);
01196     case Instruction::Shl:
01197     case Instruction::LShr:
01198     case Instruction::AShr:
01199       // We can assume that C2 == 0.  If it were one the result would be
01200       // undefined because the shift value is as large as the bitwidth.
01201       return C1;
01202     case Instruction::SDiv:
01203     case Instruction::UDiv:
01204       // We can assume that C2 == 1.  If it were zero the result would be
01205       // undefined through division by zero.
01206       return C1;
01207     case Instruction::URem:
01208     case Instruction::SRem:
01209       // We can assume that C2 == 1.  If it were zero the result would be
01210       // undefined through division by zero.
01211       return ConstantInt::getFalse(C1->getContext());
01212     default:
01213       break;
01214     }
01215   }
01216 
01217   // We don't know how to fold this.
01218   return nullptr;
01219 }
01220 
01221 /// isZeroSizedType - This type is zero sized if its an array or structure of
01222 /// zero sized types.  The only leaf zero sized type is an empty structure.
01223 static bool isMaybeZeroSizedType(Type *Ty) {
01224   if (StructType *STy = dyn_cast<StructType>(Ty)) {
01225     if (STy->isOpaque()) return true;  // Can't say.
01226 
01227     // If all of elements have zero size, this does too.
01228     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
01229       if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
01230     return true;
01231 
01232   } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
01233     return isMaybeZeroSizedType(ATy->getElementType());
01234   }
01235   return false;
01236 }
01237 
01238 /// IdxCompare - Compare the two constants as though they were getelementptr
01239 /// indices.  This allows coersion of the types to be the same thing.
01240 ///
01241 /// If the two constants are the "same" (after coersion), return 0.  If the
01242 /// first is less than the second, return -1, if the second is less than the
01243 /// first, return 1.  If the constants are not integral, return -2.
01244 ///
01245 static int IdxCompare(Constant *C1, Constant *C2, Type *ElTy) {
01246   if (C1 == C2) return 0;
01247 
01248   // Ok, we found a different index.  If they are not ConstantInt, we can't do
01249   // anything with them.
01250   if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
01251     return -2; // don't know!
01252 
01253   // Ok, we have two differing integer indices.  Sign extend them to be the same
01254   // type.  Long is always big enough, so we use it.
01255   if (!C1->getType()->isIntegerTy(64))
01256     C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(C1->getContext()));
01257 
01258   if (!C2->getType()->isIntegerTy(64))
01259     C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(C1->getContext()));
01260 
01261   if (C1 == C2) return 0;  // They are equal
01262 
01263   // If the type being indexed over is really just a zero sized type, there is
01264   // no pointer difference being made here.
01265   if (isMaybeZeroSizedType(ElTy))
01266     return -2; // dunno.
01267 
01268   // If they are really different, now that they are the same type, then we
01269   // found a difference!
01270   if (cast<ConstantInt>(C1)->getSExtValue() < 
01271       cast<ConstantInt>(C2)->getSExtValue())
01272     return -1;
01273   else
01274     return 1;
01275 }
01276 
01277 /// evaluateFCmpRelation - This function determines if there is anything we can
01278 /// decide about the two constants provided.  This doesn't need to handle simple
01279 /// things like ConstantFP comparisons, but should instead handle ConstantExprs.
01280 /// If we can determine that the two constants have a particular relation to 
01281 /// each other, we should return the corresponding FCmpInst predicate, 
01282 /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
01283 /// ConstantFoldCompareInstruction.
01284 ///
01285 /// To simplify this code we canonicalize the relation so that the first
01286 /// operand is always the most "complex" of the two.  We consider ConstantFP
01287 /// to be the simplest, and ConstantExprs to be the most complex.
01288 static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
01289   assert(V1->getType() == V2->getType() &&
01290          "Cannot compare values of different types!");
01291 
01292   // Handle degenerate case quickly
01293   if (V1 == V2) return FCmpInst::FCMP_OEQ;
01294 
01295   if (!isa<ConstantExpr>(V1)) {
01296     if (!isa<ConstantExpr>(V2)) {
01297       // We distilled thisUse the standard constant folder for a few cases
01298       ConstantInt *R = nullptr;
01299       R = dyn_cast<ConstantInt>(
01300                       ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
01301       if (R && !R->isZero()) 
01302         return FCmpInst::FCMP_OEQ;
01303       R = dyn_cast<ConstantInt>(
01304                       ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
01305       if (R && !R->isZero()) 
01306         return FCmpInst::FCMP_OLT;
01307       R = dyn_cast<ConstantInt>(
01308                       ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
01309       if (R && !R->isZero()) 
01310         return FCmpInst::FCMP_OGT;
01311 
01312       // Nothing more we can do
01313       return FCmpInst::BAD_FCMP_PREDICATE;
01314     }
01315 
01316     // If the first operand is simple and second is ConstantExpr, swap operands.
01317     FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
01318     if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
01319       return FCmpInst::getSwappedPredicate(SwappedRelation);
01320   } else {
01321     // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
01322     // constantexpr or a simple constant.
01323     ConstantExpr *CE1 = cast<ConstantExpr>(V1);
01324     switch (CE1->getOpcode()) {
01325     case Instruction::FPTrunc:
01326     case Instruction::FPExt:
01327     case Instruction::UIToFP:
01328     case Instruction::SIToFP:
01329       // We might be able to do something with these but we don't right now.
01330       break;
01331     default:
01332       break;
01333     }
01334   }
01335   // There are MANY other foldings that we could perform here.  They will
01336   // probably be added on demand, as they seem needed.
01337   return FCmpInst::BAD_FCMP_PREDICATE;
01338 }
01339 
01340 static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
01341                                                       const GlobalValue *GV2) {
01342   // Don't try to decide equality of aliases.
01343   if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
01344     if (!GV1->hasExternalWeakLinkage() || !GV2->hasExternalWeakLinkage())
01345       return ICmpInst::ICMP_NE;
01346   return ICmpInst::BAD_ICMP_PREDICATE;
01347 }
01348 
01349 /// evaluateICmpRelation - This function determines if there is anything we can
01350 /// decide about the two constants provided.  This doesn't need to handle simple
01351 /// things like integer comparisons, but should instead handle ConstantExprs
01352 /// and GlobalValues.  If we can determine that the two constants have a
01353 /// particular relation to each other, we should return the corresponding ICmp
01354 /// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
01355 ///
01356 /// To simplify this code we canonicalize the relation so that the first
01357 /// operand is always the most "complex" of the two.  We consider simple
01358 /// constants (like ConstantInt) to be the simplest, followed by
01359 /// GlobalValues, followed by ConstantExpr's (the most complex).
01360 ///
01361 static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
01362                                                 bool isSigned) {
01363   assert(V1->getType() == V2->getType() &&
01364          "Cannot compare different types of values!");
01365   if (V1 == V2) return ICmpInst::ICMP_EQ;
01366 
01367   if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
01368       !isa<BlockAddress>(V1)) {
01369     if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) &&
01370         !isa<BlockAddress>(V2)) {
01371       // We distilled this down to a simple case, use the standard constant
01372       // folder.
01373       ConstantInt *R = nullptr;
01374       ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
01375       R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
01376       if (R && !R->isZero()) 
01377         return pred;
01378       pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
01379       R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
01380       if (R && !R->isZero())
01381         return pred;
01382       pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
01383       R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
01384       if (R && !R->isZero())
01385         return pred;
01386 
01387       // If we couldn't figure it out, bail.
01388       return ICmpInst::BAD_ICMP_PREDICATE;
01389     }
01390 
01391     // If the first operand is simple, swap operands.
01392     ICmpInst::Predicate SwappedRelation = 
01393       evaluateICmpRelation(V2, V1, isSigned);
01394     if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
01395       return ICmpInst::getSwappedPredicate(SwappedRelation);
01396 
01397   } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
01398     if (isa<ConstantExpr>(V2)) {  // Swap as necessary.
01399       ICmpInst::Predicate SwappedRelation = 
01400         evaluateICmpRelation(V2, V1, isSigned);
01401       if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
01402         return ICmpInst::getSwappedPredicate(SwappedRelation);
01403       return ICmpInst::BAD_ICMP_PREDICATE;
01404     }
01405 
01406     // Now we know that the RHS is a GlobalValue, BlockAddress or simple
01407     // constant (which, since the types must match, means that it's a
01408     // ConstantPointerNull).
01409     if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
01410       return areGlobalsPotentiallyEqual(GV, GV2);
01411     } else if (isa<BlockAddress>(V2)) {
01412       return ICmpInst::ICMP_NE; // Globals never equal labels.
01413     } else {
01414       assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
01415       // GlobalVals can never be null unless they have external weak linkage.
01416       // We don't try to evaluate aliases here.
01417       if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV))
01418         return ICmpInst::ICMP_NE;
01419     }
01420   } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
01421     if (isa<ConstantExpr>(V2)) {  // Swap as necessary.
01422       ICmpInst::Predicate SwappedRelation = 
01423         evaluateICmpRelation(V2, V1, isSigned);
01424       if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
01425         return ICmpInst::getSwappedPredicate(SwappedRelation);
01426       return ICmpInst::BAD_ICMP_PREDICATE;
01427     }
01428     
01429     // Now we know that the RHS is a GlobalValue, BlockAddress or simple
01430     // constant (which, since the types must match, means that it is a
01431     // ConstantPointerNull).
01432     if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
01433       // Block address in another function can't equal this one, but block
01434       // addresses in the current function might be the same if blocks are
01435       // empty.
01436       if (BA2->getFunction() != BA->getFunction())
01437         return ICmpInst::ICMP_NE;
01438     } else {
01439       // Block addresses aren't null, don't equal the address of globals.
01440       assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
01441              "Canonicalization guarantee!");
01442       return ICmpInst::ICMP_NE;
01443     }
01444   } else {
01445     // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
01446     // constantexpr, a global, block address, or a simple constant.
01447     ConstantExpr *CE1 = cast<ConstantExpr>(V1);
01448     Constant *CE1Op0 = CE1->getOperand(0);
01449 
01450     switch (CE1->getOpcode()) {
01451     case Instruction::Trunc:
01452     case Instruction::FPTrunc:
01453     case Instruction::FPExt:
01454     case Instruction::FPToUI:
01455     case Instruction::FPToSI:
01456       break; // We can't evaluate floating point casts or truncations.
01457 
01458     case Instruction::UIToFP:
01459     case Instruction::SIToFP:
01460     case Instruction::BitCast:
01461     case Instruction::ZExt:
01462     case Instruction::SExt:
01463       // If the cast is not actually changing bits, and the second operand is a
01464       // null pointer, do the comparison with the pre-casted value.
01465       if (V2->isNullValue() &&
01466           (CE1->getType()->isPointerTy() || CE1->getType()->isIntegerTy())) {
01467         if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
01468         if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
01469         return evaluateICmpRelation(CE1Op0,
01470                                     Constant::getNullValue(CE1Op0->getType()), 
01471                                     isSigned);
01472       }
01473       break;
01474 
01475     case Instruction::GetElementPtr: {
01476       GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
01477       // Ok, since this is a getelementptr, we know that the constant has a
01478       // pointer type.  Check the various cases.
01479       if (isa<ConstantPointerNull>(V2)) {
01480         // If we are comparing a GEP to a null pointer, check to see if the base
01481         // of the GEP equals the null pointer.
01482         if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
01483           if (GV->hasExternalWeakLinkage())
01484             // Weak linkage GVals could be zero or not. We're comparing that
01485             // to null pointer so its greater-or-equal
01486             return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
01487           else 
01488             // If its not weak linkage, the GVal must have a non-zero address
01489             // so the result is greater-than
01490             return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
01491         } else if (isa<ConstantPointerNull>(CE1Op0)) {
01492           // If we are indexing from a null pointer, check to see if we have any
01493           // non-zero indices.
01494           for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
01495             if (!CE1->getOperand(i)->isNullValue())
01496               // Offsetting from null, must not be equal.
01497               return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
01498           // Only zero indexes from null, must still be zero.
01499           return ICmpInst::ICMP_EQ;
01500         }
01501         // Otherwise, we can't really say if the first operand is null or not.
01502       } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
01503         if (isa<ConstantPointerNull>(CE1Op0)) {
01504           if (GV2->hasExternalWeakLinkage())
01505             // Weak linkage GVals could be zero or not. We're comparing it to
01506             // a null pointer, so its less-or-equal
01507             return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
01508           else
01509             // If its not weak linkage, the GVal must have a non-zero address
01510             // so the result is less-than
01511             return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
01512         } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
01513           if (GV == GV2) {
01514             // If this is a getelementptr of the same global, then it must be
01515             // different.  Because the types must match, the getelementptr could
01516             // only have at most one index, and because we fold getelementptr's
01517             // with a single zero index, it must be nonzero.
01518             assert(CE1->getNumOperands() == 2 &&
01519                    !CE1->getOperand(1)->isNullValue() &&
01520                    "Surprising getelementptr!");
01521             return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
01522           } else {
01523             if (CE1GEP->hasAllZeroIndices())
01524               return areGlobalsPotentiallyEqual(GV, GV2);
01525             return ICmpInst::BAD_ICMP_PREDICATE;
01526           }
01527         }
01528       } else {
01529         ConstantExpr *CE2 = cast<ConstantExpr>(V2);
01530         Constant *CE2Op0 = CE2->getOperand(0);
01531 
01532         // There are MANY other foldings that we could perform here.  They will
01533         // probably be added on demand, as they seem needed.
01534         switch (CE2->getOpcode()) {
01535         default: break;
01536         case Instruction::GetElementPtr:
01537           // By far the most common case to handle is when the base pointers are
01538           // obviously to the same global.
01539           if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
01540             // Don't know relative ordering, but check for inequality.
01541             if (CE1Op0 != CE2Op0) {
01542               GEPOperator *CE2GEP = cast<GEPOperator>(CE2);
01543               if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
01544                 return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0),
01545                                                   cast<GlobalValue>(CE2Op0));
01546               return ICmpInst::BAD_ICMP_PREDICATE;
01547             }
01548             // Ok, we know that both getelementptr instructions are based on the
01549             // same global.  From this, we can precisely determine the relative
01550             // ordering of the resultant pointers.
01551             unsigned i = 1;
01552 
01553             // The logic below assumes that the result of the comparison
01554             // can be determined by finding the first index that differs.
01555             // This doesn't work if there is over-indexing in any
01556             // subsequent indices, so check for that case first.
01557             if (!CE1->isGEPWithNoNotionalOverIndexing() ||
01558                 !CE2->isGEPWithNoNotionalOverIndexing())
01559                return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
01560 
01561             // Compare all of the operands the GEP's have in common.
01562             gep_type_iterator GTI = gep_type_begin(CE1);
01563             for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
01564                  ++i, ++GTI)
01565               switch (IdxCompare(CE1->getOperand(i),
01566                                  CE2->getOperand(i), GTI.getIndexedType())) {
01567               case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
01568               case 1:  return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
01569               case -2: return ICmpInst::BAD_ICMP_PREDICATE;
01570               }
01571 
01572             // Ok, we ran out of things they have in common.  If any leftovers
01573             // are non-zero then we have a difference, otherwise we are equal.
01574             for (; i < CE1->getNumOperands(); ++i)
01575               if (!CE1->getOperand(i)->isNullValue()) {
01576                 if (isa<ConstantInt>(CE1->getOperand(i)))
01577                   return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
01578                 else
01579                   return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
01580               }
01581 
01582             for (; i < CE2->getNumOperands(); ++i)
01583               if (!CE2->getOperand(i)->isNullValue()) {
01584                 if (isa<ConstantInt>(CE2->getOperand(i)))
01585                   return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
01586                 else
01587                   return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
01588               }
01589             return ICmpInst::ICMP_EQ;
01590           }
01591         }
01592       }
01593     }
01594     default:
01595       break;
01596     }
01597   }
01598 
01599   return ICmpInst::BAD_ICMP_PREDICATE;
01600 }
01601 
01602 Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, 
01603                                                Constant *C1, Constant *C2) {
01604   Type *ResultTy;
01605   if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
01606     ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
01607                                VT->getNumElements());
01608   else
01609     ResultTy = Type::getInt1Ty(C1->getContext());
01610 
01611   // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
01612   if (pred == FCmpInst::FCMP_FALSE)
01613     return Constant::getNullValue(ResultTy);
01614 
01615   if (pred == FCmpInst::FCMP_TRUE)
01616     return Constant::getAllOnesValue(ResultTy);
01617 
01618   // Handle some degenerate cases first
01619   if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
01620     // For EQ and NE, we can always pick a value for the undef to make the
01621     // predicate pass or fail, so we can return undef.
01622     // Also, if both operands are undef, we can return undef.
01623     if (ICmpInst::isEquality(ICmpInst::Predicate(pred)) ||
01624         (isa<UndefValue>(C1) && isa<UndefValue>(C2)))
01625       return UndefValue::get(ResultTy);
01626     // Otherwise, pick the same value as the non-undef operand, and fold
01627     // it to true or false.
01628     return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(pred));
01629   }
01630 
01631   // icmp eq/ne(null,GV) -> false/true
01632   if (C1->isNullValue()) {
01633     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
01634       // Don't try to evaluate aliases.  External weak GV can be null.
01635       if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
01636         if (pred == ICmpInst::ICMP_EQ)
01637           return ConstantInt::getFalse(C1->getContext());
01638         else if (pred == ICmpInst::ICMP_NE)
01639           return ConstantInt::getTrue(C1->getContext());
01640       }
01641   // icmp eq/ne(GV,null) -> false/true
01642   } else if (C2->isNullValue()) {
01643     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
01644       // Don't try to evaluate aliases.  External weak GV can be null.
01645       if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
01646         if (pred == ICmpInst::ICMP_EQ)
01647           return ConstantInt::getFalse(C1->getContext());
01648         else if (pred == ICmpInst::ICMP_NE)
01649           return ConstantInt::getTrue(C1->getContext());
01650       }
01651   }
01652 
01653   // If the comparison is a comparison between two i1's, simplify it.
01654   if (C1->getType()->isIntegerTy(1)) {
01655     switch(pred) {
01656     case ICmpInst::ICMP_EQ:
01657       if (isa<ConstantInt>(C2))
01658         return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
01659       return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
01660     case ICmpInst::ICMP_NE:
01661       return ConstantExpr::getXor(C1, C2);
01662     default:
01663       break;
01664     }
01665   }
01666 
01667   if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
01668     APInt V1 = cast<ConstantInt>(C1)->getValue();
01669     APInt V2 = cast<ConstantInt>(C2)->getValue();
01670     switch (pred) {
01671     default: llvm_unreachable("Invalid ICmp Predicate");
01672     case ICmpInst::ICMP_EQ:  return ConstantInt::get(ResultTy, V1 == V2);
01673     case ICmpInst::ICMP_NE:  return ConstantInt::get(ResultTy, V1 != V2);
01674     case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2));
01675     case ICmpInst::ICMP_SGT: return ConstantInt::get(ResultTy, V1.sgt(V2));
01676     case ICmpInst::ICMP_SLE: return ConstantInt::get(ResultTy, V1.sle(V2));
01677     case ICmpInst::ICMP_SGE: return ConstantInt::get(ResultTy, V1.sge(V2));
01678     case ICmpInst::ICMP_ULT: return ConstantInt::get(ResultTy, V1.ult(V2));
01679     case ICmpInst::ICMP_UGT: return ConstantInt::get(ResultTy, V1.ugt(V2));
01680     case ICmpInst::ICMP_ULE: return ConstantInt::get(ResultTy, V1.ule(V2));
01681     case ICmpInst::ICMP_UGE: return ConstantInt::get(ResultTy, V1.uge(V2));
01682     }
01683   } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
01684     APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
01685     APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
01686     APFloat::cmpResult R = C1V.compare(C2V);
01687     switch (pred) {
01688     default: llvm_unreachable("Invalid FCmp Predicate");
01689     case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy);
01690     case FCmpInst::FCMP_TRUE:  return Constant::getAllOnesValue(ResultTy);
01691     case FCmpInst::FCMP_UNO:
01692       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered);
01693     case FCmpInst::FCMP_ORD:
01694       return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered);
01695     case FCmpInst::FCMP_UEQ:
01696       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
01697                                         R==APFloat::cmpEqual);
01698     case FCmpInst::FCMP_OEQ:   
01699       return ConstantInt::get(ResultTy, R==APFloat::cmpEqual);
01700     case FCmpInst::FCMP_UNE:
01701       return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual);
01702     case FCmpInst::FCMP_ONE:   
01703       return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
01704                                         R==APFloat::cmpGreaterThan);
01705     case FCmpInst::FCMP_ULT: 
01706       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
01707                                         R==APFloat::cmpLessThan);
01708     case FCmpInst::FCMP_OLT:   
01709       return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan);
01710     case FCmpInst::FCMP_UGT:
01711       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
01712                                         R==APFloat::cmpGreaterThan);
01713     case FCmpInst::FCMP_OGT:
01714       return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan);
01715     case FCmpInst::FCMP_ULE:
01716       return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan);
01717     case FCmpInst::FCMP_OLE: 
01718       return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
01719                                         R==APFloat::cmpEqual);
01720     case FCmpInst::FCMP_UGE:
01721       return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan);
01722     case FCmpInst::FCMP_OGE: 
01723       return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan ||
01724                                         R==APFloat::cmpEqual);
01725     }
01726   } else if (C1->getType()->isVectorTy()) {
01727     // If we can constant fold the comparison of each element, constant fold
01728     // the whole vector comparison.
01729     SmallVector<Constant*, 4> ResElts;
01730     Type *Ty = IntegerType::get(C1->getContext(), 32);
01731     // Compare the elements, producing an i1 result or constant expr.
01732     for (unsigned i = 0, e = C1->getType()->getVectorNumElements(); i != e;++i){
01733       Constant *C1E =
01734         ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i));
01735       Constant *C2E =
01736         ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i));
01737       
01738       ResElts.push_back(ConstantExpr::getCompare(pred, C1E, C2E));
01739     }
01740     
01741     return ConstantVector::get(ResElts);
01742   }
01743 
01744   if (C1->getType()->isFloatingPointTy()) {
01745     int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
01746     switch (evaluateFCmpRelation(C1, C2)) {
01747     default: llvm_unreachable("Unknown relation!");
01748     case FCmpInst::FCMP_UNO:
01749     case FCmpInst::FCMP_ORD:
01750     case FCmpInst::FCMP_UEQ:
01751     case FCmpInst::FCMP_UNE:
01752     case FCmpInst::FCMP_ULT:
01753     case FCmpInst::FCMP_UGT:
01754     case FCmpInst::FCMP_ULE:
01755     case FCmpInst::FCMP_UGE:
01756     case FCmpInst::FCMP_TRUE:
01757     case FCmpInst::FCMP_FALSE:
01758     case FCmpInst::BAD_FCMP_PREDICATE:
01759       break; // Couldn't determine anything about these constants.
01760     case FCmpInst::FCMP_OEQ: // We know that C1 == C2
01761       Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
01762                 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
01763                 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
01764       break;
01765     case FCmpInst::FCMP_OLT: // We know that C1 < C2
01766       Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
01767                 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
01768                 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
01769       break;
01770     case FCmpInst::FCMP_OGT: // We know that C1 > C2
01771       Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
01772                 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
01773                 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
01774       break;
01775     case FCmpInst::FCMP_OLE: // We know that C1 <= C2
01776       // We can only partially decide this relation.
01777       if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) 
01778         Result = 0;
01779       else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) 
01780         Result = 1;
01781       break;
01782     case FCmpInst::FCMP_OGE: // We known that C1 >= C2
01783       // We can only partially decide this relation.
01784       if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) 
01785         Result = 0;
01786       else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) 
01787         Result = 1;
01788       break;
01789     case FCmpInst::FCMP_ONE: // We know that C1 != C2
01790       // We can only partially decide this relation.
01791       if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) 
01792         Result = 0;
01793       else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE) 
01794         Result = 1;
01795       break;
01796     }
01797 
01798     // If we evaluated the result, return it now.
01799     if (Result != -1)
01800       return ConstantInt::get(ResultTy, Result);
01801 
01802   } else {
01803     // Evaluate the relation between the two constants, per the predicate.
01804     int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
01805     switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
01806     default: llvm_unreachable("Unknown relational!");
01807     case ICmpInst::BAD_ICMP_PREDICATE:
01808       break;  // Couldn't determine anything about these constants.
01809     case ICmpInst::ICMP_EQ:   // We know the constants are equal!
01810       // If we know the constants are equal, we can decide the result of this
01811       // computation precisely.
01812       Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred);
01813       break;
01814     case ICmpInst::ICMP_ULT:
01815       switch (pred) {
01816       case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
01817         Result = 1; break;
01818       case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
01819         Result = 0; break;
01820       }
01821       break;
01822     case ICmpInst::ICMP_SLT:
01823       switch (pred) {
01824       case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
01825         Result = 1; break;
01826       case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
01827         Result = 0; break;
01828       }
01829       break;
01830     case ICmpInst::ICMP_UGT:
01831       switch (pred) {
01832       case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
01833         Result = 1; break;
01834       case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
01835         Result = 0; break;
01836       }
01837       break;
01838     case ICmpInst::ICMP_SGT:
01839       switch (pred) {
01840       case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
01841         Result = 1; break;
01842       case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
01843         Result = 0; break;
01844       }
01845       break;
01846     case ICmpInst::ICMP_ULE:
01847       if (pred == ICmpInst::ICMP_UGT) Result = 0;
01848       if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1;
01849       break;
01850     case ICmpInst::ICMP_SLE:
01851       if (pred == ICmpInst::ICMP_SGT) Result = 0;
01852       if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1;
01853       break;
01854     case ICmpInst::ICMP_UGE:
01855       if (pred == ICmpInst::ICMP_ULT) Result = 0;
01856       if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1;
01857       break;
01858     case ICmpInst::ICMP_SGE:
01859       if (pred == ICmpInst::ICMP_SLT) Result = 0;
01860       if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1;
01861       break;
01862     case ICmpInst::ICMP_NE:
01863       if (pred == ICmpInst::ICMP_EQ) Result = 0;
01864       if (pred == ICmpInst::ICMP_NE) Result = 1;
01865       break;
01866     }
01867 
01868     // If we evaluated the result, return it now.
01869     if (Result != -1)
01870       return ConstantInt::get(ResultTy, Result);
01871 
01872     // If the right hand side is a bitcast, try using its inverse to simplify
01873     // it by moving it to the left hand side.  We can't do this if it would turn
01874     // a vector compare into a scalar compare or visa versa.
01875     if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
01876       Constant *CE2Op0 = CE2->getOperand(0);
01877       if (CE2->getOpcode() == Instruction::BitCast &&
01878           CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy()) {
01879         Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
01880         return ConstantExpr::getICmp(pred, Inverse, CE2Op0);
01881       }
01882     }
01883 
01884     // If the left hand side is an extension, try eliminating it.
01885     if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
01886       if ((CE1->getOpcode() == Instruction::SExt && ICmpInst::isSigned(pred)) ||
01887           (CE1->getOpcode() == Instruction::ZExt && !ICmpInst::isSigned(pred))){
01888         Constant *CE1Op0 = CE1->getOperand(0);
01889         Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
01890         if (CE1Inverse == CE1Op0) {
01891           // Check whether we can safely truncate the right hand side.
01892           Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
01893           if (ConstantExpr::getCast(CE1->getOpcode(), C2Inverse,
01894                                     C2->getType()) == C2)
01895             return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse);
01896         }
01897       }
01898     }
01899 
01900     if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
01901         (C1->isNullValue() && !C2->isNullValue())) {
01902       // If C2 is a constant expr and C1 isn't, flip them around and fold the
01903       // other way if possible.
01904       // Also, if C1 is null and C2 isn't, flip them around.
01905       pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
01906       return ConstantExpr::getICmp(pred, C2, C1);
01907     }
01908   }
01909   return nullptr;
01910 }
01911 
01912 /// isInBoundsIndices - Test whether the given sequence of *normalized* indices
01913 /// is "inbounds".
01914 template<typename IndexTy>
01915 static bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) {
01916   // No indices means nothing that could be out of bounds.
01917   if (Idxs.empty()) return true;
01918 
01919   // If the first index is zero, it's in bounds.
01920   if (cast<Constant>(Idxs[0])->isNullValue()) return true;
01921 
01922   // If the first index is one and all the rest are zero, it's in bounds,
01923   // by the one-past-the-end rule.
01924   if (!cast<ConstantInt>(Idxs[0])->isOne())
01925     return false;
01926   for (unsigned i = 1, e = Idxs.size(); i != e; ++i)
01927     if (!cast<Constant>(Idxs[i])->isNullValue())
01928       return false;
01929   return true;
01930 }
01931 
01932 /// \brief Test whether a given ConstantInt is in-range for a SequentialType.
01933 static bool isIndexInRangeOfSequentialType(const SequentialType *STy,
01934                                            const ConstantInt *CI) {
01935   if (const PointerType *PTy = dyn_cast<PointerType>(STy))
01936     // Only handle pointers to sized types, not pointers to functions.
01937     return PTy->getElementType()->isSized();
01938 
01939   uint64_t NumElements = 0;
01940   // Determine the number of elements in our sequential type.
01941   if (const ArrayType *ATy = dyn_cast<ArrayType>(STy))
01942     NumElements = ATy->getNumElements();
01943   else if (const VectorType *VTy = dyn_cast<VectorType>(STy))
01944     NumElements = VTy->getNumElements();
01945 
01946   assert((isa<ArrayType>(STy) || NumElements > 0) &&
01947          "didn't expect non-array type to have zero elements!");
01948 
01949   // We cannot bounds check the index if it doesn't fit in an int64_t.
01950   if (CI->getValue().getActiveBits() > 64)
01951     return false;
01952 
01953   // A negative index or an index past the end of our sequential type is
01954   // considered out-of-range.
01955   int64_t IndexVal = CI->getSExtValue();
01956   if (IndexVal < 0 || (NumElements > 0 && (uint64_t)IndexVal >= NumElements))
01957     return false;
01958 
01959   // Otherwise, it is in-range.
01960   return true;
01961 }
01962 
01963 template<typename IndexTy>
01964 static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
01965                                                bool inBounds,
01966                                                ArrayRef<IndexTy> Idxs) {
01967   if (Idxs.empty()) return C;
01968   Constant *Idx0 = cast<Constant>(Idxs[0]);
01969   if ((Idxs.size() == 1 && Idx0->isNullValue()))
01970     return C;
01971 
01972   if (isa<UndefValue>(C)) {
01973     PointerType *Ptr = cast<PointerType>(C->getType());
01974     Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs);
01975     assert(Ty && "Invalid indices for GEP!");
01976     return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
01977   }
01978 
01979   if (C->isNullValue()) {
01980     bool isNull = true;
01981     for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
01982       if (!cast<Constant>(Idxs[i])->isNullValue()) {
01983         isNull = false;
01984         break;
01985       }
01986     if (isNull) {
01987       PointerType *Ptr = cast<PointerType>(C->getType());
01988       Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs);
01989       assert(Ty && "Invalid indices for GEP!");
01990       return ConstantPointerNull::get(PointerType::get(Ty,
01991                                                        Ptr->getAddressSpace()));
01992     }
01993   }
01994 
01995   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
01996     // Combine Indices - If the source pointer to this getelementptr instruction
01997     // is a getelementptr instruction, combine the indices of the two
01998     // getelementptr instructions into a single instruction.
01999     //
02000     if (CE->getOpcode() == Instruction::GetElementPtr) {
02001       Type *LastTy = nullptr;
02002       for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
02003            I != E; ++I)
02004         LastTy = *I;
02005 
02006       // We cannot combine indices if doing so would take us outside of an
02007       // array or vector.  Doing otherwise could trick us if we evaluated such a
02008       // GEP as part of a load.
02009       //
02010       // e.g. Consider if the original GEP was:
02011       // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c,
02012       //                    i32 0, i32 0, i64 0)
02013       //
02014       // If we then tried to offset it by '8' to get to the third element,
02015       // an i8, we should *not* get:
02016       // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c,
02017       //                    i32 0, i32 0, i64 8)
02018       //
02019       // This GEP tries to index array element '8  which runs out-of-bounds.
02020       // Subsequent evaluation would get confused and produce erroneous results.
02021       //
02022       // The following prohibits such a GEP from being formed by checking to see
02023       // if the index is in-range with respect to an array or vector.
02024       bool PerformFold = false;
02025       if (Idx0->isNullValue())
02026         PerformFold = true;
02027       else if (SequentialType *STy = dyn_cast_or_null<SequentialType>(LastTy))
02028         if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx0))
02029           PerformFold = isIndexInRangeOfSequentialType(STy, CI);
02030 
02031       if (PerformFold) {
02032         SmallVector<Value*, 16> NewIndices;
02033         NewIndices.reserve(Idxs.size() + CE->getNumOperands());
02034         for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
02035           NewIndices.push_back(CE->getOperand(i));
02036 
02037         // Add the last index of the source with the first index of the new GEP.
02038         // Make sure to handle the case when they are actually different types.
02039         Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
02040         // Otherwise it must be an array.
02041         if (!Idx0->isNullValue()) {
02042           Type *IdxTy = Combined->getType();
02043           if (IdxTy != Idx0->getType()) {
02044             Type *Int64Ty = Type::getInt64Ty(IdxTy->getContext());
02045             Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Int64Ty);
02046             Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, Int64Ty);
02047             Combined = ConstantExpr::get(Instruction::Add, C1, C2);
02048           } else {
02049             Combined =
02050               ConstantExpr::get(Instruction::Add, Idx0, Combined);
02051           }
02052         }
02053 
02054         NewIndices.push_back(Combined);
02055         NewIndices.append(Idxs.begin() + 1, Idxs.end());
02056         return
02057           ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices,
02058                                          inBounds &&
02059                                            cast<GEPOperator>(CE)->isInBounds());
02060       }
02061     }
02062 
02063     // Attempt to fold casts to the same type away.  For example, folding:
02064     //
02065     //   i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
02066     //                       i64 0, i64 0)
02067     // into:
02068     //
02069     //   i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
02070     //
02071     // Don't fold if the cast is changing address spaces.
02072     if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) {
02073       PointerType *SrcPtrTy =
02074         dyn_cast<PointerType>(CE->getOperand(0)->getType());
02075       PointerType *DstPtrTy = dyn_cast<PointerType>(CE->getType());
02076       if (SrcPtrTy && DstPtrTy) {
02077         ArrayType *SrcArrayTy =
02078           dyn_cast<ArrayType>(SrcPtrTy->getElementType());
02079         ArrayType *DstArrayTy =
02080           dyn_cast<ArrayType>(DstPtrTy->getElementType());
02081         if (SrcArrayTy && DstArrayTy
02082             && SrcArrayTy->getElementType() == DstArrayTy->getElementType()
02083             && SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
02084           return ConstantExpr::getGetElementPtr((Constant*)CE->getOperand(0),
02085                                                 Idxs, inBounds);
02086       }
02087     }
02088   }
02089 
02090   // Check to see if any array indices are not within the corresponding
02091   // notional array or vector bounds. If so, try to determine if they can be
02092   // factored out into preceding dimensions.
02093   bool Unknown = false;
02094   SmallVector<Constant *, 8> NewIdxs;
02095   Type *Ty = C->getType();
02096   Type *Prev = nullptr;
02097   for (unsigned i = 0, e = Idxs.size(); i != e;
02098        Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
02099     if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
02100       if (isa<ArrayType>(Ty) || isa<VectorType>(Ty))
02101         if (CI->getSExtValue() > 0 &&
02102             !isIndexInRangeOfSequentialType(cast<SequentialType>(Ty), CI)) {
02103           if (isa<SequentialType>(Prev)) {
02104             // It's out of range, but we can factor it into the prior
02105             // dimension.
02106             NewIdxs.resize(Idxs.size());
02107             uint64_t NumElements = 0;
02108             if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
02109               NumElements = ATy->getNumElements();
02110             else
02111               NumElements = cast<VectorType>(Ty)->getNumElements();
02112 
02113             ConstantInt *Factor = ConstantInt::get(CI->getType(), NumElements);
02114             NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
02115 
02116             Constant *PrevIdx = cast<Constant>(Idxs[i-1]);
02117             Constant *Div = ConstantExpr::getSDiv(CI, Factor);
02118 
02119             // Before adding, extend both operands to i64 to avoid
02120             // overflow trouble.
02121             if (!PrevIdx->getType()->isIntegerTy(64))
02122               PrevIdx = ConstantExpr::getSExt(PrevIdx,
02123                                            Type::getInt64Ty(Div->getContext()));
02124             if (!Div->getType()->isIntegerTy(64))
02125               Div = ConstantExpr::getSExt(Div,
02126                                           Type::getInt64Ty(Div->getContext()));
02127 
02128             NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div);
02129           } else {
02130             // It's out of range, but the prior dimension is a struct
02131             // so we can't do anything about it.
02132             Unknown = true;
02133           }
02134         }
02135     } else {
02136       // We don't know if it's in range or not.
02137       Unknown = true;
02138     }
02139   }
02140 
02141   // If we did any factoring, start over with the adjusted indices.
02142   if (!NewIdxs.empty()) {
02143     for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
02144       if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
02145     return ConstantExpr::getGetElementPtr(C, NewIdxs, inBounds);
02146   }
02147 
02148   // If all indices are known integers and normalized, we can do a simple
02149   // check for the "inbounds" property.
02150   if (!Unknown && !inBounds)
02151     if (auto *GV = dyn_cast<GlobalVariable>(C))
02152       if (!GV->hasExternalWeakLinkage() && isInBoundsIndices(Idxs))
02153         return ConstantExpr::getInBoundsGetElementPtr(C, Idxs);
02154 
02155   return nullptr;
02156 }
02157 
02158 Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
02159                                           bool inBounds,
02160                                           ArrayRef<Constant *> Idxs) {
02161   return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs);
02162 }
02163 
02164 Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
02165                                           bool inBounds,
02166                                           ArrayRef<Value *> Idxs) {
02167   return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs);
02168 }