1249259Sdim//===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10249259Sdim// This file implements folding of constants for LLVM.  This implements the
11249259Sdim// (internal) ConstantFold.h interface, which is used by the
12249259Sdim// ConstantExpr::get* methods to automatically fold constants when possible.
13249259Sdim//
14249259Sdim// The current constant folding implementation is implemented in two pieces: the
15249259Sdim// pieces that don't need DataLayout, and the pieces that do. This is to avoid
16249259Sdim// a dependence in IR on Target.
17249259Sdim//
18249259Sdim//===----------------------------------------------------------------------===//
19249259Sdim
20249259Sdim#include "ConstantFold.h"
21249259Sdim#include "llvm/ADT/SmallVector.h"
22249259Sdim#include "llvm/IR/Constants.h"
23249259Sdim#include "llvm/IR/DerivedTypes.h"
24249259Sdim#include "llvm/IR/Function.h"
25276479Sdim#include "llvm/IR/GetElementPtrTypeIterator.h"
26249259Sdim#include "llvm/IR/GlobalAlias.h"
27249259Sdim#include "llvm/IR/GlobalVariable.h"
28249259Sdim#include "llvm/IR/Instructions.h"
29249259Sdim#include "llvm/IR/Operator.h"
30280031Sdim#include "llvm/IR/PatternMatch.h"
31249259Sdim#include "llvm/Support/Compiler.h"
32249259Sdim#include "llvm/Support/ErrorHandling.h"
33249259Sdim#include "llvm/Support/ManagedStatic.h"
34249259Sdim#include "llvm/Support/MathExtras.h"
35249259Sdim#include <limits>
36249259Sdimusing namespace llvm;
37280031Sdimusing namespace llvm::PatternMatch;
38249259Sdim
39249259Sdim//===----------------------------------------------------------------------===//
40249259Sdim//                ConstantFold*Instruction Implementations
41249259Sdim//===----------------------------------------------------------------------===//
42249259Sdim
43249259Sdim/// BitCastConstantVector - Convert the specified vector Constant node to the
44249259Sdim/// specified vector type.  At this point, we know that the elements of the
45249259Sdim/// input vector constant are all simple integer or FP values.
46249259Sdimstatic Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) {
47249259Sdim
48249259Sdim  if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
49249259Sdim  if (CV->isNullValue()) return Constant::getNullValue(DstTy);
50249259Sdim
51249259Sdim  // If this cast changes element count then we can't handle it here:
52249259Sdim  // doing so requires endianness information.  This should be handled by
53249259Sdim  // Analysis/ConstantFolding.cpp
54249259Sdim  unsigned NumElts = DstTy->getNumElements();
55249259Sdim  if (NumElts != CV->getType()->getVectorNumElements())
56276479Sdim    return nullptr;
57249259Sdim
58249259Sdim  Type *DstEltTy = DstTy->getElementType();
59249259Sdim
60249259Sdim  SmallVector<Constant*, 16> Result;
61249259Sdim  Type *Ty = IntegerType::get(CV->getContext(), 32);
62249259Sdim  for (unsigned i = 0; i != NumElts; ++i) {
63249259Sdim    Constant *C =
64249259Sdim      ConstantExpr::getExtractElement(CV, ConstantInt::get(Ty, i));
65249259Sdim    C = ConstantExpr::getBitCast(C, DstEltTy);
66249259Sdim    Result.push_back(C);
67249259Sdim  }
68249259Sdim
69249259Sdim  return ConstantVector::get(Result);
70249259Sdim}
71249259Sdim
72249259Sdim/// This function determines which opcode to use to fold two constant cast
73249259Sdim/// expressions together. It uses CastInst::isEliminableCastPair to determine
74249259Sdim/// the opcode. Consequently its just a wrapper around that function.
75249259Sdim/// @brief Determine if it is valid to fold a cast of a cast
76249259Sdimstatic unsigned
77249259SdimfoldConstantCastPair(
78249259Sdim  unsigned opc,          ///< opcode of the second cast constant expression
79249259Sdim  ConstantExpr *Op,      ///< the first cast constant expression
80261991Sdim  Type *DstTy            ///< destination type of the first cast
81249259Sdim) {
82249259Sdim  assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
83249259Sdim  assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
84249259Sdim  assert(CastInst::isCast(opc) && "Invalid cast opcode");
85249259Sdim
86296417Sdim  // The types and opcodes for the two Cast constant expressions
87249259Sdim  Type *SrcTy = Op->getOperand(0)->getType();
88249259Sdim  Type *MidTy = Op->getType();
89249259Sdim  Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
90249259Sdim  Instruction::CastOps secondOp = Instruction::CastOps(opc);
91249259Sdim
92261991Sdim  // Assume that pointers are never more than 64 bits wide, and only use this
93261991Sdim  // for the middle type. Otherwise we could end up folding away illegal
94261991Sdim  // bitcasts between address spaces with different sizes.
95249259Sdim  IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext());
96249259Sdim
97249259Sdim  // Let CastInst::isEliminableCastPair do the heavy lifting.
98249259Sdim  return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
99276479Sdim                                        nullptr, FakeIntPtrTy, nullptr);
100249259Sdim}
101249259Sdim
102249259Sdimstatic Constant *FoldBitCast(Constant *V, Type *DestTy) {
103249259Sdim  Type *SrcTy = V->getType();
104249259Sdim  if (SrcTy == DestTy)
105249259Sdim    return V; // no-op cast
106249259Sdim
107249259Sdim  // Check to see if we are casting a pointer to an aggregate to a pointer to
108249259Sdim  // the first element.  If so, return the appropriate GEP instruction.
109249259Sdim  if (PointerType *PTy = dyn_cast<PointerType>(V->getType()))
110249259Sdim    if (PointerType *DPTy = dyn_cast<PointerType>(DestTy))
111249259Sdim      if (PTy->getAddressSpace() == DPTy->getAddressSpace()
112296417Sdim          && PTy->getElementType()->isSized()) {
113249259Sdim        SmallVector<Value*, 8> IdxList;
114249259Sdim        Value *Zero =
115249259Sdim          Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));
116249259Sdim        IdxList.push_back(Zero);
117249259Sdim        Type *ElTy = PTy->getElementType();
118249259Sdim        while (ElTy != DPTy->getElementType()) {
119249259Sdim          if (StructType *STy = dyn_cast<StructType>(ElTy)) {
120249259Sdim            if (STy->getNumElements() == 0) break;
121249259Sdim            ElTy = STy->getElementType(0);
122249259Sdim            IdxList.push_back(Zero);
123249259Sdim          } else if (SequentialType *STy =
124249259Sdim                     dyn_cast<SequentialType>(ElTy)) {
125249259Sdim            if (ElTy->isPointerTy()) break;  // Can't index into pointers!
126249259Sdim            ElTy = STy->getElementType();
127249259Sdim            IdxList.push_back(Zero);
128249259Sdim          } else {
129249259Sdim            break;
130249259Sdim          }
131249259Sdim        }
132249259Sdim
133249259Sdim        if (ElTy == DPTy->getElementType())
134249259Sdim          // This GEP is inbounds because all indices are zero.
135288943Sdim          return ConstantExpr::getInBoundsGetElementPtr(PTy->getElementType(),
136288943Sdim                                                        V, IdxList);
137249259Sdim      }
138249259Sdim
139249259Sdim  // Handle casts from one vector constant to another.  We know that the src
140249259Sdim  // and dest type have the same size (otherwise its an illegal cast).
141249259Sdim  if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
142249259Sdim    if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
143249259Sdim      assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
144249259Sdim             "Not cast between same sized vectors!");
145276479Sdim      SrcTy = nullptr;
146249259Sdim      // First, check for null.  Undef is already handled.
147249259Sdim      if (isa<ConstantAggregateZero>(V))
148249259Sdim        return Constant::getNullValue(DestTy);
149249259Sdim
150249259Sdim      // Handle ConstantVector and ConstantAggregateVector.
151249259Sdim      return BitCastConstantVector(V, DestPTy);
152249259Sdim    }
153249259Sdim
154249259Sdim    // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
155249259Sdim    // This allows for other simplifications (although some of them
156249259Sdim    // can only be handled by Analysis/ConstantFolding.cpp).
157249259Sdim    if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
158249259Sdim      return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy);
159249259Sdim  }
160249259Sdim
161249259Sdim  // Finally, implement bitcast folding now.   The code below doesn't handle
162249259Sdim  // bitcast right.
163249259Sdim  if (isa<ConstantPointerNull>(V))  // ptr->ptr cast.
164249259Sdim    return ConstantPointerNull::get(cast<PointerType>(DestTy));
165249259Sdim
166249259Sdim  // Handle integral constant input.
167249259Sdim  if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
168249259Sdim    if (DestTy->isIntegerTy())
169249259Sdim      // Integral -> Integral. This is a no-op because the bit widths must
170249259Sdim      // be the same. Consequently, we just fold to V.
171249259Sdim      return V;
172249259Sdim
173288943Sdim    // See note below regarding the PPC_FP128 restriction.
174288943Sdim    if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty())
175249259Sdim      return ConstantFP::get(DestTy->getContext(),
176249259Sdim                             APFloat(DestTy->getFltSemantics(),
177249259Sdim                                     CI->getValue()));
178249259Sdim
179249259Sdim    // Otherwise, can't fold this (vector?)
180276479Sdim    return nullptr;
181249259Sdim  }
182249259Sdim
183249259Sdim  // Handle ConstantFP input: FP -> Integral.
184288943Sdim  if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
185288943Sdim    // PPC_FP128 is really the sum of two consecutive doubles, where the first
186288943Sdim    // double is always stored first in memory, regardless of the target
187288943Sdim    // endianness. The memory layout of i128, however, depends on the target
188288943Sdim    // endianness, and so we can't fold this without target endianness
189288943Sdim    // information. This should instead be handled by
190288943Sdim    // Analysis/ConstantFolding.cpp
191288943Sdim    if (FP->getType()->isPPC_FP128Ty())
192288943Sdim      return nullptr;
193288943Sdim
194249259Sdim    return ConstantInt::get(FP->getContext(),
195249259Sdim                            FP->getValueAPF().bitcastToAPInt());
196288943Sdim  }
197249259Sdim
198276479Sdim  return nullptr;
199249259Sdim}
200249259Sdim
201249259Sdim
202249259Sdim/// ExtractConstantBytes - V is an integer constant which only has a subset of
203249259Sdim/// its bytes used.  The bytes used are indicated by ByteStart (which is the
204249259Sdim/// first byte used, counting from the least significant byte) and ByteSize,
205249259Sdim/// which is the number of bytes used.
206249259Sdim///
207249259Sdim/// This function analyzes the specified constant to see if the specified byte
208249259Sdim/// range can be returned as a simplified constant.  If so, the constant is
209249259Sdim/// returned, otherwise null is returned.
210249259Sdim///
211249259Sdimstatic Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
212249259Sdim                                      unsigned ByteSize) {
213249259Sdim  assert(C->getType()->isIntegerTy() &&
214249259Sdim         (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
215249259Sdim         "Non-byte sized integer input");
216249259Sdim  unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
217249259Sdim  assert(ByteSize && "Must be accessing some piece");
218249259Sdim  assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
219249259Sdim  assert(ByteSize != CSize && "Should not extract everything");
220249259Sdim
221249259Sdim  // Constant Integers are simple.
222249259Sdim  if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
223249259Sdim    APInt V = CI->getValue();
224249259Sdim    if (ByteStart)
225249259Sdim      V = V.lshr(ByteStart*8);
226249259Sdim    V = V.trunc(ByteSize*8);
227249259Sdim    return ConstantInt::get(CI->getContext(), V);
228249259Sdim  }
229249259Sdim
230249259Sdim  // In the input is a constant expr, we might be able to recursively simplify.
231249259Sdim  // If not, we definitely can't do anything.
232249259Sdim  ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
233276479Sdim  if (!CE) return nullptr;
234276479Sdim
235249259Sdim  switch (CE->getOpcode()) {
236276479Sdim  default: return nullptr;
237249259Sdim  case Instruction::Or: {
238249259Sdim    Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
239276479Sdim    if (!RHS)
240276479Sdim      return nullptr;
241249259Sdim
242249259Sdim    // X | -1 -> -1.
243249259Sdim    if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS))
244249259Sdim      if (RHSC->isAllOnesValue())
245249259Sdim        return RHSC;
246249259Sdim
247249259Sdim    Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
248276479Sdim    if (!LHS)
249276479Sdim      return nullptr;
250249259Sdim    return ConstantExpr::getOr(LHS, RHS);
251249259Sdim  }
252249259Sdim  case Instruction::And: {
253249259Sdim    Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
254276479Sdim    if (!RHS)
255276479Sdim      return nullptr;
256249259Sdim
257249259Sdim    // X & 0 -> 0.
258249259Sdim    if (RHS->isNullValue())
259249259Sdim      return RHS;
260249259Sdim
261249259Sdim    Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
262276479Sdim    if (!LHS)
263276479Sdim      return nullptr;
264249259Sdim    return ConstantExpr::getAnd(LHS, RHS);
265249259Sdim  }
266249259Sdim  case Instruction::LShr: {
267249259Sdim    ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
268276479Sdim    if (!Amt)
269276479Sdim      return nullptr;
270249259Sdim    unsigned ShAmt = Amt->getZExtValue();
271249259Sdim    // Cannot analyze non-byte shifts.
272249259Sdim    if ((ShAmt & 7) != 0)
273276479Sdim      return nullptr;
274249259Sdim    ShAmt >>= 3;
275249259Sdim
276249259Sdim    // If the extract is known to be all zeros, return zero.
277249259Sdim    if (ByteStart >= CSize-ShAmt)
278249259Sdim      return Constant::getNullValue(IntegerType::get(CE->getContext(),
279249259Sdim                                                     ByteSize*8));
280249259Sdim    // If the extract is known to be fully in the input, extract it.
281249259Sdim    if (ByteStart+ByteSize+ShAmt <= CSize)
282249259Sdim      return ExtractConstantBytes(CE->getOperand(0), ByteStart+ShAmt, ByteSize);
283249259Sdim
284249259Sdim    // TODO: Handle the 'partially zero' case.
285276479Sdim    return nullptr;
286249259Sdim  }
287249259Sdim
288249259Sdim  case Instruction::Shl: {
289249259Sdim    ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
290276479Sdim    if (!Amt)
291276479Sdim      return nullptr;
292249259Sdim    unsigned ShAmt = Amt->getZExtValue();
293249259Sdim    // Cannot analyze non-byte shifts.
294249259Sdim    if ((ShAmt & 7) != 0)
295276479Sdim      return nullptr;
296249259Sdim    ShAmt >>= 3;
297249259Sdim
298249259Sdim    // If the extract is known to be all zeros, return zero.
299249259Sdim    if (ByteStart+ByteSize <= ShAmt)
300249259Sdim      return Constant::getNullValue(IntegerType::get(CE->getContext(),
301249259Sdim                                                     ByteSize*8));
302249259Sdim    // If the extract is known to be fully in the input, extract it.
303249259Sdim    if (ByteStart >= ShAmt)
304249259Sdim      return ExtractConstantBytes(CE->getOperand(0), ByteStart-ShAmt, ByteSize);
305249259Sdim
306249259Sdim    // TODO: Handle the 'partially zero' case.
307276479Sdim    return nullptr;
308249259Sdim  }
309249259Sdim
310249259Sdim  case Instruction::ZExt: {
311249259Sdim    unsigned SrcBitSize =
312249259Sdim      cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth();
313249259Sdim
314249259Sdim    // If extracting something that is completely zero, return 0.
315249259Sdim    if (ByteStart*8 >= SrcBitSize)
316249259Sdim      return Constant::getNullValue(IntegerType::get(CE->getContext(),
317249259Sdim                                                     ByteSize*8));
318249259Sdim
319249259Sdim    // If exactly extracting the input, return it.
320249259Sdim    if (ByteStart == 0 && ByteSize*8 == SrcBitSize)
321249259Sdim      return CE->getOperand(0);
322249259Sdim
323249259Sdim    // If extracting something completely in the input, if if the input is a
324249259Sdim    // multiple of 8 bits, recurse.
325249259Sdim    if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize)
326249259Sdim      return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize);
327249259Sdim
328249259Sdim    // Otherwise, if extracting a subset of the input, which is not multiple of
329249259Sdim    // 8 bits, do a shift and trunc to get the bits.
330249259Sdim    if ((ByteStart+ByteSize)*8 < SrcBitSize) {
331249259Sdim      assert((SrcBitSize&7) && "Shouldn't get byte sized case here");
332249259Sdim      Constant *Res = CE->getOperand(0);
333249259Sdim      if (ByteStart)
334249259Sdim        Res = ConstantExpr::getLShr(Res,
335249259Sdim                                 ConstantInt::get(Res->getType(), ByteStart*8));
336249259Sdim      return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(),
337249259Sdim                                                          ByteSize*8));
338249259Sdim    }
339249259Sdim
340249259Sdim    // TODO: Handle the 'partially zero' case.
341276479Sdim    return nullptr;
342249259Sdim  }
343249259Sdim  }
344249259Sdim}
345249259Sdim
346249259Sdim/// getFoldedSizeOf - Return a ConstantExpr with type DestTy for sizeof
347249259Sdim/// on Ty, with any known factors factored out. If Folded is false,
348249259Sdim/// return null if no factoring was possible, to avoid endlessly
349249259Sdim/// bouncing an unfoldable expression back into the top-level folder.
350249259Sdim///
351249259Sdimstatic Constant *getFoldedSizeOf(Type *Ty, Type *DestTy,
352249259Sdim                                 bool Folded) {
353249259Sdim  if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
354249259Sdim    Constant *N = ConstantInt::get(DestTy, ATy->getNumElements());
355249259Sdim    Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
356249259Sdim    return ConstantExpr::getNUWMul(E, N);
357249259Sdim  }
358249259Sdim
359249259Sdim  if (StructType *STy = dyn_cast<StructType>(Ty))
360249259Sdim    if (!STy->isPacked()) {
361249259Sdim      unsigned NumElems = STy->getNumElements();
362249259Sdim      // An empty struct has size zero.
363249259Sdim      if (NumElems == 0)
364249259Sdim        return ConstantExpr::getNullValue(DestTy);
365249259Sdim      // Check for a struct with all members having the same size.
366249259Sdim      Constant *MemberSize =
367249259Sdim        getFoldedSizeOf(STy->getElementType(0), DestTy, true);
368249259Sdim      bool AllSame = true;
369249259Sdim      for (unsigned i = 1; i != NumElems; ++i)
370249259Sdim        if (MemberSize !=
371249259Sdim            getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
372249259Sdim          AllSame = false;
373249259Sdim          break;
374249259Sdim        }
375249259Sdim      if (AllSame) {
376249259Sdim        Constant *N = ConstantInt::get(DestTy, NumElems);
377249259Sdim        return ConstantExpr::getNUWMul(MemberSize, N);
378249259Sdim      }
379249259Sdim    }
380249259Sdim
381249259Sdim  // Pointer size doesn't depend on the pointee type, so canonicalize them
382249259Sdim  // to an arbitrary pointee.
383249259Sdim  if (PointerType *PTy = dyn_cast<PointerType>(Ty))
384249259Sdim    if (!PTy->getElementType()->isIntegerTy(1))
385249259Sdim      return
386249259Sdim        getFoldedSizeOf(PointerType::get(IntegerType::get(PTy->getContext(), 1),
387249259Sdim                                         PTy->getAddressSpace()),
388249259Sdim                        DestTy, true);
389249259Sdim
390249259Sdim  // If there's no interesting folding happening, bail so that we don't create
391249259Sdim  // a constant that looks like it needs folding but really doesn't.
392249259Sdim  if (!Folded)
393276479Sdim    return nullptr;
394249259Sdim
395249259Sdim  // Base case: Get a regular sizeof expression.
396249259Sdim  Constant *C = ConstantExpr::getSizeOf(Ty);
397249259Sdim  C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
398249259Sdim                                                    DestTy, false),
399249259Sdim                            C, DestTy);
400249259Sdim  return C;
401249259Sdim}
402249259Sdim
403249259Sdim/// getFoldedAlignOf - Return a ConstantExpr with type DestTy for alignof
404249259Sdim/// on Ty, with any known factors factored out. If Folded is false,
405249259Sdim/// return null if no factoring was possible, to avoid endlessly
406249259Sdim/// bouncing an unfoldable expression back into the top-level folder.
407249259Sdim///
408249259Sdimstatic Constant *getFoldedAlignOf(Type *Ty, Type *DestTy,
409249259Sdim                                  bool Folded) {
410249259Sdim  // The alignment of an array is equal to the alignment of the
411249259Sdim  // array element. Note that this is not always true for vectors.
412249259Sdim  if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
413249259Sdim    Constant *C = ConstantExpr::getAlignOf(ATy->getElementType());
414249259Sdim    C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
415249259Sdim                                                      DestTy,
416249259Sdim                                                      false),
417249259Sdim                              C, DestTy);
418249259Sdim    return C;
419249259Sdim  }
420249259Sdim
421249259Sdim  if (StructType *STy = dyn_cast<StructType>(Ty)) {
422249259Sdim    // Packed structs always have an alignment of 1.
423249259Sdim    if (STy->isPacked())
424249259Sdim      return ConstantInt::get(DestTy, 1);
425249259Sdim
426249259Sdim    // Otherwise, struct alignment is the maximum alignment of any member.
427249259Sdim    // Without target data, we can't compare much, but we can check to see
428249259Sdim    // if all the members have the same alignment.
429249259Sdim    unsigned NumElems = STy->getNumElements();
430249259Sdim    // An empty struct has minimal alignment.
431249259Sdim    if (NumElems == 0)
432249259Sdim      return ConstantInt::get(DestTy, 1);
433249259Sdim    // Check for a struct with all members having the same alignment.
434249259Sdim    Constant *MemberAlign =
435249259Sdim      getFoldedAlignOf(STy->getElementType(0), DestTy, true);
436249259Sdim    bool AllSame = true;
437249259Sdim    for (unsigned i = 1; i != NumElems; ++i)
438249259Sdim      if (MemberAlign != getFoldedAlignOf(STy->getElementType(i), DestTy, true)) {
439249259Sdim        AllSame = false;
440249259Sdim        break;
441249259Sdim      }
442249259Sdim    if (AllSame)
443249259Sdim      return MemberAlign;
444249259Sdim  }
445249259Sdim
446249259Sdim  // Pointer alignment doesn't depend on the pointee type, so canonicalize them
447249259Sdim  // to an arbitrary pointee.
448249259Sdim  if (PointerType *PTy = dyn_cast<PointerType>(Ty))
449249259Sdim    if (!PTy->getElementType()->isIntegerTy(1))
450249259Sdim      return
451249259Sdim        getFoldedAlignOf(PointerType::get(IntegerType::get(PTy->getContext(),
452249259Sdim                                                           1),
453249259Sdim                                          PTy->getAddressSpace()),
454249259Sdim                         DestTy, true);
455249259Sdim
456249259Sdim  // If there's no interesting folding happening, bail so that we don't create
457249259Sdim  // a constant that looks like it needs folding but really doesn't.
458249259Sdim  if (!Folded)
459276479Sdim    return nullptr;
460249259Sdim
461249259Sdim  // Base case: Get a regular alignof expression.
462249259Sdim  Constant *C = ConstantExpr::getAlignOf(Ty);
463249259Sdim  C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
464249259Sdim                                                    DestTy, false),
465249259Sdim                            C, DestTy);
466249259Sdim  return C;
467249259Sdim}
468249259Sdim
469249259Sdim/// getFoldedOffsetOf - Return a ConstantExpr with type DestTy for offsetof
470249259Sdim/// on Ty and FieldNo, with any known factors factored out. If Folded is false,
471249259Sdim/// return null if no factoring was possible, to avoid endlessly
472249259Sdim/// bouncing an unfoldable expression back into the top-level folder.
473249259Sdim///
474249259Sdimstatic Constant *getFoldedOffsetOf(Type *Ty, Constant *FieldNo,
475249259Sdim                                   Type *DestTy,
476249259Sdim                                   bool Folded) {
477249259Sdim  if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
478249259Sdim    Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, false,
479249259Sdim                                                                DestTy, false),
480249259Sdim                                        FieldNo, DestTy);
481249259Sdim    Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
482249259Sdim    return ConstantExpr::getNUWMul(E, N);
483249259Sdim  }
484249259Sdim
485249259Sdim  if (StructType *STy = dyn_cast<StructType>(Ty))
486249259Sdim    if (!STy->isPacked()) {
487249259Sdim      unsigned NumElems = STy->getNumElements();
488249259Sdim      // An empty struct has no members.
489249259Sdim      if (NumElems == 0)
490276479Sdim        return nullptr;
491249259Sdim      // Check for a struct with all members having the same size.
492249259Sdim      Constant *MemberSize =
493249259Sdim        getFoldedSizeOf(STy->getElementType(0), DestTy, true);
494249259Sdim      bool AllSame = true;
495249259Sdim      for (unsigned i = 1; i != NumElems; ++i)
496249259Sdim        if (MemberSize !=
497249259Sdim            getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
498249259Sdim          AllSame = false;
499249259Sdim          break;
500249259Sdim        }
501249259Sdim      if (AllSame) {
502249259Sdim        Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo,
503249259Sdim                                                                    false,
504249259Sdim                                                                    DestTy,
505249259Sdim                                                                    false),
506249259Sdim                                            FieldNo, DestTy);
507249259Sdim        return ConstantExpr::getNUWMul(MemberSize, N);
508249259Sdim      }
509249259Sdim    }
510249259Sdim
511249259Sdim  // If there's no interesting folding happening, bail so that we don't create
512249259Sdim  // a constant that looks like it needs folding but really doesn't.
513249259Sdim  if (!Folded)
514276479Sdim    return nullptr;
515249259Sdim
516249259Sdim  // Base case: Get a regular offsetof expression.
517249259Sdim  Constant *C = ConstantExpr::getOffsetOf(Ty, FieldNo);
518249259Sdim  C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
519249259Sdim                                                    DestTy, false),
520249259Sdim                            C, DestTy);
521249259Sdim  return C;
522249259Sdim}
523249259Sdim
524249259SdimConstant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
525249259Sdim                                            Type *DestTy) {
526249259Sdim  if (isa<UndefValue>(V)) {
527249259Sdim    // zext(undef) = 0, because the top bits will be zero.
528249259Sdim    // sext(undef) = 0, because the top bits will all be the same.
529249259Sdim    // [us]itofp(undef) = 0, because the result value is bounded.
530249259Sdim    if (opc == Instruction::ZExt || opc == Instruction::SExt ||
531249259Sdim        opc == Instruction::UIToFP || opc == Instruction::SIToFP)
532249259Sdim      return Constant::getNullValue(DestTy);
533249259Sdim    return UndefValue::get(DestTy);
534249259Sdim  }
535249259Sdim
536249259Sdim  if (V->isNullValue() && !DestTy->isX86_MMXTy())
537249259Sdim    return Constant::getNullValue(DestTy);
538249259Sdim
539249259Sdim  // If the cast operand is a constant expression, there's a few things we can
540249259Sdim  // do to try to simplify it.
541249259Sdim  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
542249259Sdim    if (CE->isCast()) {
543249259Sdim      // Try hard to fold cast of cast because they are often eliminable.
544249259Sdim      if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
545249259Sdim        return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
546276479Sdim    } else if (CE->getOpcode() == Instruction::GetElementPtr &&
547276479Sdim               // Do not fold addrspacecast (gep 0, .., 0). It might make the
548276479Sdim               // addrspacecast uncanonicalized.
549276479Sdim               opc != Instruction::AddrSpaceCast) {
550249259Sdim      // If all of the indexes in the GEP are null values, there is no pointer
551249259Sdim      // adjustment going on.  We might as well cast the source pointer.
552249259Sdim      bool isAllNull = true;
553249259Sdim      for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
554249259Sdim        if (!CE->getOperand(i)->isNullValue()) {
555249259Sdim          isAllNull = false;
556249259Sdim          break;
557249259Sdim        }
558249259Sdim      if (isAllNull)
559249259Sdim        // This is casting one pointer type to another, always BitCast
560249259Sdim        return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
561249259Sdim    }
562249259Sdim  }
563249259Sdim
564249259Sdim  // If the cast operand is a constant vector, perform the cast by
565249259Sdim  // operating on each element. In the cast of bitcasts, the element
566249259Sdim  // count may be mismatched; don't attempt to handle that here.
567249259Sdim  if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
568249259Sdim      DestTy->isVectorTy() &&
569249259Sdim      DestTy->getVectorNumElements() == V->getType()->getVectorNumElements()) {
570249259Sdim    SmallVector<Constant*, 16> res;
571249259Sdim    VectorType *DestVecTy = cast<VectorType>(DestTy);
572249259Sdim    Type *DstEltTy = DestVecTy->getElementType();
573249259Sdim    Type *Ty = IntegerType::get(V->getContext(), 32);
574249259Sdim    for (unsigned i = 0, e = V->getType()->getVectorNumElements(); i != e; ++i) {
575249259Sdim      Constant *C =
576249259Sdim        ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
577249259Sdim      res.push_back(ConstantExpr::getCast(opc, C, DstEltTy));
578249259Sdim    }
579249259Sdim    return ConstantVector::get(res);
580249259Sdim  }
581249259Sdim
582249259Sdim  // We actually have to do a cast now. Perform the cast according to the
583249259Sdim  // opcode specified.
584249259Sdim  switch (opc) {
585249259Sdim  default:
586249259Sdim    llvm_unreachable("Failed to cast constant expression");
587249259Sdim  case Instruction::FPTrunc:
588249259Sdim  case Instruction::FPExt:
589249259Sdim    if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
590249259Sdim      bool ignored;
591249259Sdim      APFloat Val = FPC->getValueAPF();
592249259Sdim      Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf :
593249259Sdim                  DestTy->isFloatTy() ? APFloat::IEEEsingle :
594249259Sdim                  DestTy->isDoubleTy() ? APFloat::IEEEdouble :
595249259Sdim                  DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended :
596249259Sdim                  DestTy->isFP128Ty() ? APFloat::IEEEquad :
597249259Sdim                  DestTy->isPPC_FP128Ty() ? APFloat::PPCDoubleDouble :
598249259Sdim                  APFloat::Bogus,
599249259Sdim                  APFloat::rmNearestTiesToEven, &ignored);
600249259Sdim      return ConstantFP::get(V->getContext(), Val);
601249259Sdim    }
602276479Sdim    return nullptr; // Can't fold.
603249259Sdim  case Instruction::FPToUI:
604249259Sdim  case Instruction::FPToSI:
605249259Sdim    if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
606249259Sdim      const APFloat &V = FPC->getValueAPF();
607249259Sdim      bool ignored;
608249259Sdim      uint64_t x[2];
609249259Sdim      uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
610280031Sdim      if (APFloat::opInvalidOp ==
611280031Sdim          V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
612280031Sdim                             APFloat::rmTowardZero, &ignored)) {
613280031Sdim        // Undefined behavior invoked - the destination type can't represent
614280031Sdim        // the input constant.
615280031Sdim        return UndefValue::get(DestTy);
616280031Sdim      }
617249259Sdim      APInt Val(DestBitWidth, x);
618249259Sdim      return ConstantInt::get(FPC->getContext(), Val);
619249259Sdim    }
620276479Sdim    return nullptr; // Can't fold.
621249259Sdim  case Instruction::IntToPtr:   //always treated as unsigned
622249259Sdim    if (V->isNullValue())       // Is it an integral null value?
623249259Sdim      return ConstantPointerNull::get(cast<PointerType>(DestTy));
624276479Sdim    return nullptr;                   // Other pointer types cannot be casted
625249259Sdim  case Instruction::PtrToInt:   // always treated as unsigned
626249259Sdim    // Is it a null pointer value?
627249259Sdim    if (V->isNullValue())
628249259Sdim      return ConstantInt::get(DestTy, 0);
629249259Sdim    // If this is a sizeof-like expression, pull out multiplications by
630249259Sdim    // known factors to expose them to subsequent folding. If it's an
631249259Sdim    // alignof-like expression, factor out known factors.
632249259Sdim    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
633249259Sdim      if (CE->getOpcode() == Instruction::GetElementPtr &&
634249259Sdim          CE->getOperand(0)->isNullValue()) {
635288943Sdim        GEPOperator *GEPO = cast<GEPOperator>(CE);
636288943Sdim        Type *Ty = GEPO->getSourceElementType();
637249259Sdim        if (CE->getNumOperands() == 2) {
638249259Sdim          // Handle a sizeof-like expression.
639249259Sdim          Constant *Idx = CE->getOperand(1);
640249259Sdim          bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne();
641249259Sdim          if (Constant *C = getFoldedSizeOf(Ty, DestTy, !isOne)) {
642249259Sdim            Idx = ConstantExpr::getCast(CastInst::getCastOpcode(Idx, true,
643249259Sdim                                                                DestTy, false),
644249259Sdim                                        Idx, DestTy);
645249259Sdim            return ConstantExpr::getMul(C, Idx);
646249259Sdim          }
647249259Sdim        } else if (CE->getNumOperands() == 3 &&
648249259Sdim                   CE->getOperand(1)->isNullValue()) {
649249259Sdim          // Handle an alignof-like expression.
650249259Sdim          if (StructType *STy = dyn_cast<StructType>(Ty))
651249259Sdim            if (!STy->isPacked()) {
652249259Sdim              ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2));
653249259Sdim              if (CI->isOne() &&
654249259Sdim                  STy->getNumElements() == 2 &&
655249259Sdim                  STy->getElementType(0)->isIntegerTy(1)) {
656249259Sdim                return getFoldedAlignOf(STy->getElementType(1), DestTy, false);
657249259Sdim              }
658249259Sdim            }
659249259Sdim          // Handle an offsetof-like expression.
660249259Sdim          if (Ty->isStructTy() || Ty->isArrayTy()) {
661249259Sdim            if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2),
662249259Sdim                                                DestTy, false))
663249259Sdim              return C;
664249259Sdim          }
665249259Sdim        }
666249259Sdim      }
667249259Sdim    // Other pointer types cannot be casted
668276479Sdim    return nullptr;
669249259Sdim  case Instruction::UIToFP:
670249259Sdim  case Instruction::SIToFP:
671249259Sdim    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
672249259Sdim      APInt api = CI->getValue();
673249259Sdim      APFloat apf(DestTy->getFltSemantics(),
674249259Sdim                  APInt::getNullValue(DestTy->getPrimitiveSizeInBits()));
675280031Sdim      if (APFloat::opOverflow &
676280031Sdim          apf.convertFromAPInt(api, opc==Instruction::SIToFP,
677280031Sdim                              APFloat::rmNearestTiesToEven)) {
678280031Sdim        // Undefined behavior invoked - the destination type can't represent
679280031Sdim        // the input constant.
680280031Sdim        return UndefValue::get(DestTy);
681280031Sdim      }
682249259Sdim      return ConstantFP::get(V->getContext(), apf);
683249259Sdim    }
684276479Sdim    return nullptr;
685249259Sdim  case Instruction::ZExt:
686249259Sdim    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
687249259Sdim      uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
688249259Sdim      return ConstantInt::get(V->getContext(),
689249259Sdim                              CI->getValue().zext(BitWidth));
690249259Sdim    }
691276479Sdim    return nullptr;
692249259Sdim  case Instruction::SExt:
693249259Sdim    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
694249259Sdim      uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
695249259Sdim      return ConstantInt::get(V->getContext(),
696249259Sdim                              CI->getValue().sext(BitWidth));
697249259Sdim    }
698276479Sdim    return nullptr;
699249259Sdim  case Instruction::Trunc: {
700280031Sdim    if (V->getType()->isVectorTy())
701280031Sdim      return nullptr;
702280031Sdim
703249259Sdim    uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
704249259Sdim    if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
705249259Sdim      return ConstantInt::get(V->getContext(),
706249259Sdim                              CI->getValue().trunc(DestBitWidth));
707249259Sdim    }
708249259Sdim
709249259Sdim    // The input must be a constantexpr.  See if we can simplify this based on
710249259Sdim    // the bytes we are demanding.  Only do this if the source and dest are an
711249259Sdim    // even multiple of a byte.
712249259Sdim    if ((DestBitWidth & 7) == 0 &&
713249259Sdim        (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
714249259Sdim      if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
715249259Sdim        return Res;
716249259Sdim
717276479Sdim    return nullptr;
718249259Sdim  }
719249259Sdim  case Instruction::BitCast:
720249259Sdim    return FoldBitCast(V, DestTy);
721261991Sdim  case Instruction::AddrSpaceCast:
722276479Sdim    return nullptr;
723249259Sdim  }
724249259Sdim}
725249259Sdim
726249259SdimConstant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
727249259Sdim                                              Constant *V1, Constant *V2) {
728249259Sdim  // Check for i1 and vector true/false conditions.
729249259Sdim  if (Cond->isNullValue()) return V2;
730249259Sdim  if (Cond->isAllOnesValue()) return V1;
731249259Sdim
732249259Sdim  // If the condition is a vector constant, fold the result elementwise.
733249259Sdim  if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
734249259Sdim    SmallVector<Constant*, 16> Result;
735249259Sdim    Type *Ty = IntegerType::get(CondV->getContext(), 32);
736249259Sdim    for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){
737276479Sdim      Constant *V;
738276479Sdim      Constant *V1Element = ConstantExpr::getExtractElement(V1,
739276479Sdim                                                    ConstantInt::get(Ty, i));
740276479Sdim      Constant *V2Element = ConstantExpr::getExtractElement(V2,
741276479Sdim                                                    ConstantInt::get(Ty, i));
742276479Sdim      Constant *Cond = dyn_cast<Constant>(CondV->getOperand(i));
743276479Sdim      if (V1Element == V2Element) {
744276479Sdim        V = V1Element;
745276479Sdim      } else if (isa<UndefValue>(Cond)) {
746276479Sdim        V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
747276479Sdim      } else {
748276479Sdim        if (!isa<ConstantInt>(Cond)) break;
749276479Sdim        V = Cond->isNullValue() ? V2Element : V1Element;
750276479Sdim      }
751276479Sdim      Result.push_back(V);
752249259Sdim    }
753249259Sdim
754249259Sdim    // If we were able to build the vector, return it.
755249259Sdim    if (Result.size() == V1->getType()->getVectorNumElements())
756249259Sdim      return ConstantVector::get(Result);
757249259Sdim  }
758249259Sdim
759249259Sdim  if (isa<UndefValue>(Cond)) {
760249259Sdim    if (isa<UndefValue>(V1)) return V1;
761249259Sdim    return V2;
762249259Sdim  }
763249259Sdim  if (isa<UndefValue>(V1)) return V2;
764249259Sdim  if (isa<UndefValue>(V2)) return V1;
765249259Sdim  if (V1 == V2) return V1;
766249259Sdim
767249259Sdim  if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) {
768249259Sdim    if (TrueVal->getOpcode() == Instruction::Select)
769249259Sdim      if (TrueVal->getOperand(0) == Cond)
770249259Sdim        return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2);
771249259Sdim  }
772249259Sdim  if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) {
773249259Sdim    if (FalseVal->getOpcode() == Instruction::Select)
774249259Sdim      if (FalseVal->getOperand(0) == Cond)
775249259Sdim        return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2));
776249259Sdim  }
777249259Sdim
778276479Sdim  return nullptr;
779249259Sdim}
780249259Sdim
781249259SdimConstant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
782249259Sdim                                                      Constant *Idx) {
783249259Sdim  if (isa<UndefValue>(Val))  // ee(undef, x) -> undef
784249259Sdim    return UndefValue::get(Val->getType()->getVectorElementType());
785249259Sdim  if (Val->isNullValue())  // ee(zero, x) -> zero
786249259Sdim    return Constant::getNullValue(Val->getType()->getVectorElementType());
787249259Sdim  // ee({w,x,y,z}, undef) -> undef
788249259Sdim  if (isa<UndefValue>(Idx))
789249259Sdim    return UndefValue::get(Val->getType()->getVectorElementType());
790249259Sdim
791249259Sdim  if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
792249259Sdim    // ee({w,x,y,z}, wrong_value) -> undef
793288943Sdim    if (CIdx->uge(Val->getType()->getVectorNumElements()))
794249259Sdim      return UndefValue::get(Val->getType()->getVectorElementType());
795288943Sdim    return Val->getAggregateElement(CIdx->getZExtValue());
796249259Sdim  }
797276479Sdim  return nullptr;
798249259Sdim}
799249259Sdim
800249259SdimConstant *llvm::ConstantFoldInsertElementInstruction(Constant *Val,
801249259Sdim                                                     Constant *Elt,
802249259Sdim                                                     Constant *Idx) {
803288943Sdim  if (isa<UndefValue>(Idx))
804288943Sdim    return UndefValue::get(Val->getType());
805288943Sdim
806249259Sdim  ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
807276479Sdim  if (!CIdx) return nullptr;
808288943Sdim
809288943Sdim  unsigned NumElts = Val->getType()->getVectorNumElements();
810288943Sdim  if (CIdx->uge(NumElts))
811288943Sdim    return UndefValue::get(Val->getType());
812288943Sdim
813249259Sdim  SmallVector<Constant*, 16> Result;
814288943Sdim  Result.reserve(NumElts);
815288943Sdim  auto *Ty = Type::getInt32Ty(Val->getContext());
816288943Sdim  uint64_t IdxVal = CIdx->getZExtValue();
817288943Sdim  for (unsigned i = 0; i != NumElts; ++i) {
818249259Sdim    if (i == IdxVal) {
819249259Sdim      Result.push_back(Elt);
820249259Sdim      continue;
821249259Sdim    }
822249259Sdim
823288943Sdim    Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
824249259Sdim    Result.push_back(C);
825249259Sdim  }
826288943Sdim
827249259Sdim  return ConstantVector::get(Result);
828249259Sdim}
829249259Sdim
830249259SdimConstant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1,
831249259Sdim                                                     Constant *V2,
832249259Sdim                                                     Constant *Mask) {
833249259Sdim  unsigned MaskNumElts = Mask->getType()->getVectorNumElements();
834249259Sdim  Type *EltTy = V1->getType()->getVectorElementType();
835249259Sdim
836249259Sdim  // Undefined shuffle mask -> undefined value.
837249259Sdim  if (isa<UndefValue>(Mask))
838249259Sdim    return UndefValue::get(VectorType::get(EltTy, MaskNumElts));
839249259Sdim
840249259Sdim  // Don't break the bitcode reader hack.
841276479Sdim  if (isa<ConstantExpr>(Mask)) return nullptr;
842249259Sdim
843249259Sdim  unsigned SrcNumElts = V1->getType()->getVectorNumElements();
844249259Sdim
845249259Sdim  // Loop over the shuffle mask, evaluating each element.
846249259Sdim  SmallVector<Constant*, 32> Result;
847249259Sdim  for (unsigned i = 0; i != MaskNumElts; ++i) {
848249259Sdim    int Elt = ShuffleVectorInst::getMaskValue(Mask, i);
849249259Sdim    if (Elt == -1) {
850249259Sdim      Result.push_back(UndefValue::get(EltTy));
851249259Sdim      continue;
852249259Sdim    }
853249259Sdim    Constant *InElt;
854249259Sdim    if (unsigned(Elt) >= SrcNumElts*2)
855249259Sdim      InElt = UndefValue::get(EltTy);
856249259Sdim    else if (unsigned(Elt) >= SrcNumElts) {
857249259Sdim      Type *Ty = IntegerType::get(V2->getContext(), 32);
858249259Sdim      InElt =
859249259Sdim        ConstantExpr::getExtractElement(V2,
860249259Sdim                                        ConstantInt::get(Ty, Elt - SrcNumElts));
861249259Sdim    } else {
862249259Sdim      Type *Ty = IntegerType::get(V1->getContext(), 32);
863249259Sdim      InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt));
864249259Sdim    }
865249259Sdim    Result.push_back(InElt);
866249259Sdim  }
867249259Sdim
868249259Sdim  return ConstantVector::get(Result);
869249259Sdim}
870249259Sdim
871249259SdimConstant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
872249259Sdim                                                    ArrayRef<unsigned> Idxs) {
873249259Sdim  // Base case: no indices, so return the entire value.
874249259Sdim  if (Idxs.empty())
875249259Sdim    return Agg;
876249259Sdim
877249259Sdim  if (Constant *C = Agg->getAggregateElement(Idxs[0]))
878249259Sdim    return ConstantFoldExtractValueInstruction(C, Idxs.slice(1));
879249259Sdim
880276479Sdim  return nullptr;
881249259Sdim}
882249259Sdim
883249259SdimConstant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
884249259Sdim                                                   Constant *Val,
885249259Sdim                                                   ArrayRef<unsigned> Idxs) {
886249259Sdim  // Base case: no indices, so replace the entire value.
887249259Sdim  if (Idxs.empty())
888249259Sdim    return Val;
889249259Sdim
890249259Sdim  unsigned NumElts;
891249259Sdim  if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
892249259Sdim    NumElts = ST->getNumElements();
893249259Sdim  else if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType()))
894249259Sdim    NumElts = AT->getNumElements();
895249259Sdim  else
896249259Sdim    NumElts = Agg->getType()->getVectorNumElements();
897249259Sdim
898249259Sdim  SmallVector<Constant*, 32> Result;
899249259Sdim  for (unsigned i = 0; i != NumElts; ++i) {
900249259Sdim    Constant *C = Agg->getAggregateElement(i);
901276479Sdim    if (!C) return nullptr;
902276479Sdim
903249259Sdim    if (Idxs[0] == i)
904249259Sdim      C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1));
905249259Sdim
906249259Sdim    Result.push_back(C);
907249259Sdim  }
908249259Sdim
909249259Sdim  if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
910249259Sdim    return ConstantStruct::get(ST, Result);
911249259Sdim  if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType()))
912249259Sdim    return ConstantArray::get(AT, Result);
913249259Sdim  return ConstantVector::get(Result);
914249259Sdim}
915249259Sdim
916249259Sdim
917249259SdimConstant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
918249259Sdim                                              Constant *C1, Constant *C2) {
919249259Sdim  // Handle UndefValue up front.
920249259Sdim  if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
921249259Sdim    switch (Opcode) {
922249259Sdim    case Instruction::Xor:
923249259Sdim      if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
924249259Sdim        // Handle undef ^ undef -> 0 special case. This is a common
925249259Sdim        // idiom (misuse).
926249259Sdim        return Constant::getNullValue(C1->getType());
927249259Sdim      // Fallthrough
928249259Sdim    case Instruction::Add:
929249259Sdim    case Instruction::Sub:
930249259Sdim      return UndefValue::get(C1->getType());
931249259Sdim    case Instruction::And:
932249259Sdim      if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
933249259Sdim        return C1;
934249259Sdim      return Constant::getNullValue(C1->getType());   // undef & X -> 0
935249259Sdim    case Instruction::Mul: {
936280031Sdim      // undef * undef -> undef
937280031Sdim      if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
938280031Sdim        return C1;
939280031Sdim      const APInt *CV;
940280031Sdim      // X * undef -> undef   if X is odd
941280031Sdim      if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV)))
942280031Sdim        if ((*CV)[0])
943280031Sdim          return UndefValue::get(C1->getType());
944249259Sdim
945249259Sdim      // X * undef -> 0       otherwise
946249259Sdim      return Constant::getNullValue(C1->getType());
947249259Sdim    }
948280031Sdim    case Instruction::SDiv:
949249259Sdim    case Instruction::UDiv:
950280031Sdim      // X / undef -> undef
951280031Sdim      if (match(C1, m_Zero()))
952280031Sdim        return C2;
953280031Sdim      // undef / 0 -> undef
954249259Sdim      // undef / 1 -> undef
955280031Sdim      if (match(C2, m_Zero()) || match(C2, m_One()))
956280031Sdim        return C1;
957280031Sdim      // undef / X -> 0       otherwise
958280031Sdim      return Constant::getNullValue(C1->getType());
959249259Sdim    case Instruction::URem:
960249259Sdim    case Instruction::SRem:
961280031Sdim      // X % undef -> undef
962280031Sdim      if (match(C2, m_Undef()))
963280031Sdim        return C2;
964280031Sdim      // undef % 0 -> undef
965280031Sdim      if (match(C2, m_Zero()))
966280031Sdim        return C1;
967280031Sdim      // undef % X -> 0       otherwise
968280031Sdim      return Constant::getNullValue(C1->getType());
969249259Sdim    case Instruction::Or:                          // X | undef -> -1
970249259Sdim      if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
971249259Sdim        return C1;
972249259Sdim      return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
973249259Sdim    case Instruction::LShr:
974280031Sdim      // X >>l undef -> undef
975280031Sdim      if (isa<UndefValue>(C2))
976280031Sdim        return C2;
977280031Sdim      // undef >>l 0 -> undef
978280031Sdim      if (match(C2, m_Zero()))
979280031Sdim        return C1;
980280031Sdim      // undef >>l X -> 0
981280031Sdim      return Constant::getNullValue(C1->getType());
982249259Sdim    case Instruction::AShr:
983280031Sdim      // X >>a undef -> undef
984280031Sdim      if (isa<UndefValue>(C2))
985280031Sdim        return C2;
986280031Sdim      // undef >>a 0 -> undef
987280031Sdim      if (match(C2, m_Zero()))
988280031Sdim        return C1;
989280031Sdim      // TODO: undef >>a X -> undef if the shift is exact
990280031Sdim      // undef >>a X -> 0
991280031Sdim      return Constant::getNullValue(C1->getType());
992249259Sdim    case Instruction::Shl:
993280031Sdim      // X << undef -> undef
994280031Sdim      if (isa<UndefValue>(C2))
995280031Sdim        return C2;
996280031Sdim      // undef << 0 -> undef
997280031Sdim      if (match(C2, m_Zero()))
998280031Sdim        return C1;
999280031Sdim      // undef << X -> 0
1000249259Sdim      return Constant::getNullValue(C1->getType());
1001249259Sdim    }
1002249259Sdim  }
1003249259Sdim
1004249259Sdim  // Handle simplifications when the RHS is a constant int.
1005249259Sdim  if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
1006249259Sdim    switch (Opcode) {
1007249259Sdim    case Instruction::Add:
1008249259Sdim      if (CI2->equalsInt(0)) return C1;                         // X + 0 == X
1009249259Sdim      break;
1010249259Sdim    case Instruction::Sub:
1011249259Sdim      if (CI2->equalsInt(0)) return C1;                         // X - 0 == X
1012249259Sdim      break;
1013249259Sdim    case Instruction::Mul:
1014249259Sdim      if (CI2->equalsInt(0)) return C2;                         // X * 0 == 0
1015249259Sdim      if (CI2->equalsInt(1))
1016249259Sdim        return C1;                                              // X * 1 == X
1017249259Sdim      break;
1018249259Sdim    case Instruction::UDiv:
1019249259Sdim    case Instruction::SDiv:
1020249259Sdim      if (CI2->equalsInt(1))
1021249259Sdim        return C1;                                            // X / 1 == X
1022249259Sdim      if (CI2->equalsInt(0))
1023249259Sdim        return UndefValue::get(CI2->getType());               // X / 0 == undef
1024249259Sdim      break;
1025249259Sdim    case Instruction::URem:
1026249259Sdim    case Instruction::SRem:
1027249259Sdim      if (CI2->equalsInt(1))
1028249259Sdim        return Constant::getNullValue(CI2->getType());        // X % 1 == 0
1029249259Sdim      if (CI2->equalsInt(0))
1030249259Sdim        return UndefValue::get(CI2->getType());               // X % 0 == undef
1031249259Sdim      break;
1032249259Sdim    case Instruction::And:
1033249259Sdim      if (CI2->isZero()) return C2;                           // X & 0 == 0
1034249259Sdim      if (CI2->isAllOnesValue())
1035249259Sdim        return C1;                                            // X & -1 == X
1036249259Sdim
1037249259Sdim      if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1038249259Sdim        // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
1039249259Sdim        if (CE1->getOpcode() == Instruction::ZExt) {
1040249259Sdim          unsigned DstWidth = CI2->getType()->getBitWidth();
1041249259Sdim          unsigned SrcWidth =
1042249259Sdim            CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
1043249259Sdim          APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
1044249259Sdim          if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
1045249259Sdim            return C1;
1046249259Sdim        }
1047249259Sdim
1048249259Sdim        // If and'ing the address of a global with a constant, fold it.
1049249259Sdim        if (CE1->getOpcode() == Instruction::PtrToInt &&
1050249259Sdim            isa<GlobalValue>(CE1->getOperand(0))) {
1051249259Sdim          GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
1052249259Sdim
1053249259Sdim          // Functions are at least 4-byte aligned.
1054249259Sdim          unsigned GVAlign = GV->getAlignment();
1055249259Sdim          if (isa<Function>(GV))
1056249259Sdim            GVAlign = std::max(GVAlign, 4U);
1057249259Sdim
1058249259Sdim          if (GVAlign > 1) {
1059249259Sdim            unsigned DstWidth = CI2->getType()->getBitWidth();
1060249259Sdim            unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
1061249259Sdim            APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
1062249259Sdim
1063249259Sdim            // If checking bits we know are clear, return zero.
1064249259Sdim            if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
1065249259Sdim              return Constant::getNullValue(CI2->getType());
1066249259Sdim          }
1067249259Sdim        }
1068249259Sdim      }
1069249259Sdim      break;
1070249259Sdim    case Instruction::Or:
1071249259Sdim      if (CI2->equalsInt(0)) return C1;    // X | 0 == X
1072249259Sdim      if (CI2->isAllOnesValue())
1073249259Sdim        return C2;                         // X | -1 == -1
1074249259Sdim      break;
1075249259Sdim    case Instruction::Xor:
1076249259Sdim      if (CI2->equalsInt(0)) return C1;    // X ^ 0 == X
1077249259Sdim
1078249259Sdim      if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1079249259Sdim        switch (CE1->getOpcode()) {
1080249259Sdim        default: break;
1081249259Sdim        case Instruction::ICmp:
1082249259Sdim        case Instruction::FCmp:
1083249259Sdim          // cmp pred ^ true -> cmp !pred
1084249259Sdim          assert(CI2->equalsInt(1));
1085249259Sdim          CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
1086249259Sdim          pred = CmpInst::getInversePredicate(pred);
1087249259Sdim          return ConstantExpr::getCompare(pred, CE1->getOperand(0),
1088249259Sdim                                          CE1->getOperand(1));
1089249259Sdim        }
1090249259Sdim      }
1091249259Sdim      break;
1092249259Sdim    case Instruction::AShr:
1093249259Sdim      // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
1094249259Sdim      if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
1095249259Sdim        if (CE1->getOpcode() == Instruction::ZExt)  // Top bits known zero.
1096249259Sdim          return ConstantExpr::getLShr(C1, C2);
1097249259Sdim      break;
1098249259Sdim    }
1099249259Sdim  } else if (isa<ConstantInt>(C1)) {
1100249259Sdim    // If C1 is a ConstantInt and C2 is not, swap the operands.
1101249259Sdim    if (Instruction::isCommutative(Opcode))
1102249259Sdim      return ConstantExpr::get(Opcode, C2, C1);
1103249259Sdim  }
1104249259Sdim
1105249259Sdim  // At this point we know neither constant is an UndefValue.
1106249259Sdim  if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
1107249259Sdim    if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
1108249259Sdim      const APInt &C1V = CI1->getValue();
1109249259Sdim      const APInt &C2V = CI2->getValue();
1110249259Sdim      switch (Opcode) {
1111249259Sdim      default:
1112249259Sdim        break;
1113249259Sdim      case Instruction::Add:
1114249259Sdim        return ConstantInt::get(CI1->getContext(), C1V + C2V);
1115249259Sdim      case Instruction::Sub:
1116249259Sdim        return ConstantInt::get(CI1->getContext(), C1V - C2V);
1117249259Sdim      case Instruction::Mul:
1118249259Sdim        return ConstantInt::get(CI1->getContext(), C1V * C2V);
1119249259Sdim      case Instruction::UDiv:
1120249259Sdim        assert(!CI2->isNullValue() && "Div by zero handled above");
1121249259Sdim        return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V));
1122249259Sdim      case Instruction::SDiv:
1123249259Sdim        assert(!CI2->isNullValue() && "Div by zero handled above");
1124249259Sdim        if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
1125249259Sdim          return UndefValue::get(CI1->getType());   // MIN_INT / -1 -> undef
1126249259Sdim        return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V));
1127249259Sdim      case Instruction::URem:
1128249259Sdim        assert(!CI2->isNullValue() && "Div by zero handled above");
1129249259Sdim        return ConstantInt::get(CI1->getContext(), C1V.urem(C2V));
1130249259Sdim      case Instruction::SRem:
1131249259Sdim        assert(!CI2->isNullValue() && "Div by zero handled above");
1132249259Sdim        if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
1133249259Sdim          return UndefValue::get(CI1->getType());   // MIN_INT % -1 -> undef
1134249259Sdim        return ConstantInt::get(CI1->getContext(), C1V.srem(C2V));
1135249259Sdim      case Instruction::And:
1136249259Sdim        return ConstantInt::get(CI1->getContext(), C1V & C2V);
1137249259Sdim      case Instruction::Or:
1138249259Sdim        return ConstantInt::get(CI1->getContext(), C1V | C2V);
1139249259Sdim      case Instruction::Xor:
1140249259Sdim        return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
1141283526Sdim      case Instruction::Shl:
1142283526Sdim        if (C2V.ult(C1V.getBitWidth()))
1143283526Sdim          return ConstantInt::get(CI1->getContext(), C1V.shl(C2V));
1144283526Sdim        return UndefValue::get(C1->getType()); // too big shift is undef
1145283526Sdim      case Instruction::LShr:
1146283526Sdim        if (C2V.ult(C1V.getBitWidth()))
1147283526Sdim          return ConstantInt::get(CI1->getContext(), C1V.lshr(C2V));
1148283526Sdim        return UndefValue::get(C1->getType()); // too big shift is undef
1149283526Sdim      case Instruction::AShr:
1150283526Sdim        if (C2V.ult(C1V.getBitWidth()))
1151283526Sdim          return ConstantInt::get(CI1->getContext(), C1V.ashr(C2V));
1152283526Sdim        return UndefValue::get(C1->getType()); // too big shift is undef
1153249259Sdim      }
1154249259Sdim    }
1155249259Sdim
1156249259Sdim    switch (Opcode) {
1157249259Sdim    case Instruction::SDiv:
1158249259Sdim    case Instruction::UDiv:
1159249259Sdim    case Instruction::URem:
1160249259Sdim    case Instruction::SRem:
1161249259Sdim    case Instruction::LShr:
1162249259Sdim    case Instruction::AShr:
1163249259Sdim    case Instruction::Shl:
1164249259Sdim      if (CI1->equalsInt(0)) return C1;
1165249259Sdim      break;
1166249259Sdim    default:
1167249259Sdim      break;
1168249259Sdim    }
1169249259Sdim  } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
1170249259Sdim    if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
1171249259Sdim      APFloat C1V = CFP1->getValueAPF();
1172249259Sdim      APFloat C2V = CFP2->getValueAPF();
1173249259Sdim      APFloat C3V = C1V;  // copy for modification
1174249259Sdim      switch (Opcode) {
1175249259Sdim      default:
1176249259Sdim        break;
1177249259Sdim      case Instruction::FAdd:
1178249259Sdim        (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
1179249259Sdim        return ConstantFP::get(C1->getContext(), C3V);
1180249259Sdim      case Instruction::FSub:
1181249259Sdim        (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
1182249259Sdim        return ConstantFP::get(C1->getContext(), C3V);
1183249259Sdim      case Instruction::FMul:
1184249259Sdim        (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
1185249259Sdim        return ConstantFP::get(C1->getContext(), C3V);
1186249259Sdim      case Instruction::FDiv:
1187249259Sdim        (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
1188249259Sdim        return ConstantFP::get(C1->getContext(), C3V);
1189249259Sdim      case Instruction::FRem:
1190296417Sdim        (void)C3V.mod(C2V);
1191249259Sdim        return ConstantFP::get(C1->getContext(), C3V);
1192249259Sdim      }
1193249259Sdim    }
1194249259Sdim  } else if (VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
1195249259Sdim    // Perform elementwise folding.
1196249259Sdim    SmallVector<Constant*, 16> Result;
1197249259Sdim    Type *Ty = IntegerType::get(VTy->getContext(), 32);
1198249259Sdim    for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1199249259Sdim      Constant *LHS =
1200249259Sdim        ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i));
1201249259Sdim      Constant *RHS =
1202249259Sdim        ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i));
1203249259Sdim
1204249259Sdim      Result.push_back(ConstantExpr::get(Opcode, LHS, RHS));
1205249259Sdim    }
1206249259Sdim
1207249259Sdim    return ConstantVector::get(Result);
1208249259Sdim  }
1209249259Sdim
1210249259Sdim  if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1211249259Sdim    // There are many possible foldings we could do here.  We should probably
1212249259Sdim    // at least fold add of a pointer with an integer into the appropriate
1213249259Sdim    // getelementptr.  This will improve alias analysis a bit.
1214249259Sdim
1215249259Sdim    // Given ((a + b) + c), if (b + c) folds to something interesting, return
1216249259Sdim    // (a + (b + c)).
1217249259Sdim    if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
1218249259Sdim      Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
1219249259Sdim      if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
1220249259Sdim        return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
1221249259Sdim    }
1222249259Sdim  } else if (isa<ConstantExpr>(C2)) {
1223249259Sdim    // If C2 is a constant expr and C1 isn't, flop them around and fold the
1224249259Sdim    // other way if possible.
1225249259Sdim    if (Instruction::isCommutative(Opcode))
1226249259Sdim      return ConstantFoldBinaryInstruction(Opcode, C2, C1);
1227249259Sdim  }
1228249259Sdim
1229249259Sdim  // i1 can be simplified in many cases.
1230249259Sdim  if (C1->getType()->isIntegerTy(1)) {
1231249259Sdim    switch (Opcode) {
1232249259Sdim    case Instruction::Add:
1233249259Sdim    case Instruction::Sub:
1234249259Sdim      return ConstantExpr::getXor(C1, C2);
1235249259Sdim    case Instruction::Mul:
1236249259Sdim      return ConstantExpr::getAnd(C1, C2);
1237249259Sdim    case Instruction::Shl:
1238249259Sdim    case Instruction::LShr:
1239249259Sdim    case Instruction::AShr:
1240249259Sdim      // We can assume that C2 == 0.  If it were one the result would be
1241249259Sdim      // undefined because the shift value is as large as the bitwidth.
1242249259Sdim      return C1;
1243249259Sdim    case Instruction::SDiv:
1244249259Sdim    case Instruction::UDiv:
1245249259Sdim      // We can assume that C2 == 1.  If it were zero the result would be
1246249259Sdim      // undefined through division by zero.
1247249259Sdim      return C1;
1248249259Sdim    case Instruction::URem:
1249249259Sdim    case Instruction::SRem:
1250249259Sdim      // We can assume that C2 == 1.  If it were zero the result would be
1251249259Sdim      // undefined through division by zero.
1252249259Sdim      return ConstantInt::getFalse(C1->getContext());
1253249259Sdim    default:
1254249259Sdim      break;
1255249259Sdim    }
1256249259Sdim  }
1257249259Sdim
1258249259Sdim  // We don't know how to fold this.
1259276479Sdim  return nullptr;
1260249259Sdim}
1261249259Sdim
1262249259Sdim/// isZeroSizedType - This type is zero sized if its an array or structure of
1263249259Sdim/// zero sized types.  The only leaf zero sized type is an empty structure.
1264249259Sdimstatic bool isMaybeZeroSizedType(Type *Ty) {
1265249259Sdim  if (StructType *STy = dyn_cast<StructType>(Ty)) {
1266249259Sdim    if (STy->isOpaque()) return true;  // Can't say.
1267249259Sdim
1268249259Sdim    // If all of elements have zero size, this does too.
1269249259Sdim    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1270249259Sdim      if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
1271249259Sdim    return true;
1272249259Sdim
1273249259Sdim  } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1274249259Sdim    return isMaybeZeroSizedType(ATy->getElementType());
1275249259Sdim  }
1276249259Sdim  return false;
1277249259Sdim}
1278249259Sdim
1279249259Sdim/// IdxCompare - Compare the two constants as though they were getelementptr
1280296417Sdim/// indices.  This allows coercion of the types to be the same thing.
1281249259Sdim///
1282296417Sdim/// If the two constants are the "same" (after coercion), return 0.  If the
1283249259Sdim/// first is less than the second, return -1, if the second is less than the
1284249259Sdim/// first, return 1.  If the constants are not integral, return -2.
1285249259Sdim///
1286249259Sdimstatic int IdxCompare(Constant *C1, Constant *C2, Type *ElTy) {
1287249259Sdim  if (C1 == C2) return 0;
1288249259Sdim
1289249259Sdim  // Ok, we found a different index.  If they are not ConstantInt, we can't do
1290249259Sdim  // anything with them.
1291249259Sdim  if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1292249259Sdim    return -2; // don't know!
1293249259Sdim
1294288943Sdim  // We cannot compare the indices if they don't fit in an int64_t.
1295288943Sdim  if (cast<ConstantInt>(C1)->getValue().getActiveBits() > 64 ||
1296288943Sdim      cast<ConstantInt>(C2)->getValue().getActiveBits() > 64)
1297288943Sdim    return -2; // don't know!
1298288943Sdim
1299249259Sdim  // Ok, we have two differing integer indices.  Sign extend them to be the same
1300288943Sdim  // type.
1301288943Sdim  int64_t C1Val = cast<ConstantInt>(C1)->getSExtValue();
1302288943Sdim  int64_t C2Val = cast<ConstantInt>(C2)->getSExtValue();
1303249259Sdim
1304288943Sdim  if (C1Val == C2Val) return 0;  // They are equal
1305249259Sdim
1306249259Sdim  // If the type being indexed over is really just a zero sized type, there is
1307249259Sdim  // no pointer difference being made here.
1308249259Sdim  if (isMaybeZeroSizedType(ElTy))
1309249259Sdim    return -2; // dunno.
1310249259Sdim
1311249259Sdim  // If they are really different, now that they are the same type, then we
1312249259Sdim  // found a difference!
1313288943Sdim  if (C1Val < C2Val)
1314249259Sdim    return -1;
1315249259Sdim  else
1316249259Sdim    return 1;
1317249259Sdim}
1318249259Sdim
1319249259Sdim/// evaluateFCmpRelation - This function determines if there is anything we can
1320249259Sdim/// decide about the two constants provided.  This doesn't need to handle simple
1321249259Sdim/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
1322249259Sdim/// If we can determine that the two constants have a particular relation to
1323249259Sdim/// each other, we should return the corresponding FCmpInst predicate,
1324249259Sdim/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
1325249259Sdim/// ConstantFoldCompareInstruction.
1326249259Sdim///
1327249259Sdim/// To simplify this code we canonicalize the relation so that the first
1328249259Sdim/// operand is always the most "complex" of the two.  We consider ConstantFP
1329249259Sdim/// to be the simplest, and ConstantExprs to be the most complex.
1330249259Sdimstatic FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
1331249259Sdim  assert(V1->getType() == V2->getType() &&
1332249259Sdim         "Cannot compare values of different types!");
1333249259Sdim
1334249259Sdim  // Handle degenerate case quickly
1335249259Sdim  if (V1 == V2) return FCmpInst::FCMP_OEQ;
1336249259Sdim
1337249259Sdim  if (!isa<ConstantExpr>(V1)) {
1338249259Sdim    if (!isa<ConstantExpr>(V2)) {
1339288943Sdim      // Simple case, use the standard constant folder.
1340276479Sdim      ConstantInt *R = nullptr;
1341249259Sdim      R = dyn_cast<ConstantInt>(
1342249259Sdim                      ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
1343249259Sdim      if (R && !R->isZero())
1344249259Sdim        return FCmpInst::FCMP_OEQ;
1345249259Sdim      R = dyn_cast<ConstantInt>(
1346249259Sdim                      ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
1347249259Sdim      if (R && !R->isZero())
1348249259Sdim        return FCmpInst::FCMP_OLT;
1349249259Sdim      R = dyn_cast<ConstantInt>(
1350249259Sdim                      ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
1351249259Sdim      if (R && !R->isZero())
1352249259Sdim        return FCmpInst::FCMP_OGT;
1353249259Sdim
1354249259Sdim      // Nothing more we can do
1355249259Sdim      return FCmpInst::BAD_FCMP_PREDICATE;
1356249259Sdim    }
1357249259Sdim
1358249259Sdim    // If the first operand is simple and second is ConstantExpr, swap operands.
1359249259Sdim    FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
1360249259Sdim    if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
1361249259Sdim      return FCmpInst::getSwappedPredicate(SwappedRelation);
1362249259Sdim  } else {
1363249259Sdim    // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
1364249259Sdim    // constantexpr or a simple constant.
1365249259Sdim    ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1366249259Sdim    switch (CE1->getOpcode()) {
1367249259Sdim    case Instruction::FPTrunc:
1368249259Sdim    case Instruction::FPExt:
1369249259Sdim    case Instruction::UIToFP:
1370249259Sdim    case Instruction::SIToFP:
1371249259Sdim      // We might be able to do something with these but we don't right now.
1372249259Sdim      break;
1373249259Sdim    default:
1374249259Sdim      break;
1375249259Sdim    }
1376249259Sdim  }
1377249259Sdim  // There are MANY other foldings that we could perform here.  They will
1378249259Sdim  // probably be added on demand, as they seem needed.
1379249259Sdim  return FCmpInst::BAD_FCMP_PREDICATE;
1380249259Sdim}
1381249259Sdim
1382276479Sdimstatic ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
1383276479Sdim                                                      const GlobalValue *GV2) {
1384280031Sdim  auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
1385280031Sdim    if (GV->hasExternalWeakLinkage() || GV->hasWeakAnyLinkage())
1386280031Sdim      return true;
1387280031Sdim    if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
1388288943Sdim      Type *Ty = GVar->getValueType();
1389280031Sdim      // A global with opaque type might end up being zero sized.
1390280031Sdim      if (!Ty->isSized())
1391280031Sdim        return true;
1392280031Sdim      // A global with an empty type might lie at the address of any other
1393280031Sdim      // global.
1394280031Sdim      if (Ty->isEmptyTy())
1395280031Sdim        return true;
1396280031Sdim    }
1397280031Sdim    return false;
1398280031Sdim  };
1399276479Sdim  // Don't try to decide equality of aliases.
1400276479Sdim  if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
1401280031Sdim    if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
1402276479Sdim      return ICmpInst::ICMP_NE;
1403276479Sdim  return ICmpInst::BAD_ICMP_PREDICATE;
1404276479Sdim}
1405276479Sdim
1406249259Sdim/// evaluateICmpRelation - This function determines if there is anything we can
1407249259Sdim/// decide about the two constants provided.  This doesn't need to handle simple
1408249259Sdim/// things like integer comparisons, but should instead handle ConstantExprs
1409249259Sdim/// and GlobalValues.  If we can determine that the two constants have a
1410249259Sdim/// particular relation to each other, we should return the corresponding ICmp
1411249259Sdim/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
1412249259Sdim///
1413249259Sdim/// To simplify this code we canonicalize the relation so that the first
1414249259Sdim/// operand is always the most "complex" of the two.  We consider simple
1415249259Sdim/// constants (like ConstantInt) to be the simplest, followed by
1416249259Sdim/// GlobalValues, followed by ConstantExpr's (the most complex).
1417249259Sdim///
1418249259Sdimstatic ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
1419249259Sdim                                                bool isSigned) {
1420249259Sdim  assert(V1->getType() == V2->getType() &&
1421249259Sdim         "Cannot compare different types of values!");
1422249259Sdim  if (V1 == V2) return ICmpInst::ICMP_EQ;
1423249259Sdim
1424249259Sdim  if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
1425249259Sdim      !isa<BlockAddress>(V1)) {
1426249259Sdim    if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) &&
1427249259Sdim        !isa<BlockAddress>(V2)) {
1428249259Sdim      // We distilled this down to a simple case, use the standard constant
1429249259Sdim      // folder.
1430276479Sdim      ConstantInt *R = nullptr;
1431249259Sdim      ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
1432249259Sdim      R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
1433249259Sdim      if (R && !R->isZero())
1434249259Sdim        return pred;
1435249259Sdim      pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1436249259Sdim      R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
1437249259Sdim      if (R && !R->isZero())
1438249259Sdim        return pred;
1439249259Sdim      pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1440249259Sdim      R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
1441249259Sdim      if (R && !R->isZero())
1442249259Sdim        return pred;
1443249259Sdim
1444249259Sdim      // If we couldn't figure it out, bail.
1445249259Sdim      return ICmpInst::BAD_ICMP_PREDICATE;
1446249259Sdim    }
1447249259Sdim
1448249259Sdim    // If the first operand is simple, swap operands.
1449249259Sdim    ICmpInst::Predicate SwappedRelation =
1450249259Sdim      evaluateICmpRelation(V2, V1, isSigned);
1451249259Sdim    if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1452249259Sdim      return ICmpInst::getSwappedPredicate(SwappedRelation);
1453249259Sdim
1454249259Sdim  } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1455249259Sdim    if (isa<ConstantExpr>(V2)) {  // Swap as necessary.
1456249259Sdim      ICmpInst::Predicate SwappedRelation =
1457249259Sdim        evaluateICmpRelation(V2, V1, isSigned);
1458249259Sdim      if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1459249259Sdim        return ICmpInst::getSwappedPredicate(SwappedRelation);
1460249259Sdim      return ICmpInst::BAD_ICMP_PREDICATE;
1461249259Sdim    }
1462249259Sdim
1463249259Sdim    // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1464249259Sdim    // constant (which, since the types must match, means that it's a
1465249259Sdim    // ConstantPointerNull).
1466249259Sdim    if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1467276479Sdim      return areGlobalsPotentiallyEqual(GV, GV2);
1468249259Sdim    } else if (isa<BlockAddress>(V2)) {
1469249259Sdim      return ICmpInst::ICMP_NE; // Globals never equal labels.
1470249259Sdim    } else {
1471249259Sdim      assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
1472249259Sdim      // GlobalVals can never be null unless they have external weak linkage.
1473249259Sdim      // We don't try to evaluate aliases here.
1474249259Sdim      if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV))
1475249259Sdim        return ICmpInst::ICMP_NE;
1476249259Sdim    }
1477249259Sdim  } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1478249259Sdim    if (isa<ConstantExpr>(V2)) {  // Swap as necessary.
1479249259Sdim      ICmpInst::Predicate SwappedRelation =
1480249259Sdim        evaluateICmpRelation(V2, V1, isSigned);
1481249259Sdim      if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1482249259Sdim        return ICmpInst::getSwappedPredicate(SwappedRelation);
1483249259Sdim      return ICmpInst::BAD_ICMP_PREDICATE;
1484249259Sdim    }
1485249259Sdim
1486249259Sdim    // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1487249259Sdim    // constant (which, since the types must match, means that it is a
1488249259Sdim    // ConstantPointerNull).
1489249259Sdim    if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1490249259Sdim      // Block address in another function can't equal this one, but block
1491249259Sdim      // addresses in the current function might be the same if blocks are
1492249259Sdim      // empty.
1493249259Sdim      if (BA2->getFunction() != BA->getFunction())
1494249259Sdim        return ICmpInst::ICMP_NE;
1495249259Sdim    } else {
1496249259Sdim      // Block addresses aren't null, don't equal the address of globals.
1497249259Sdim      assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
1498249259Sdim             "Canonicalization guarantee!");
1499249259Sdim      return ICmpInst::ICMP_NE;
1500249259Sdim    }
1501249259Sdim  } else {
1502249259Sdim    // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
1503249259Sdim    // constantexpr, a global, block address, or a simple constant.
1504249259Sdim    ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1505249259Sdim    Constant *CE1Op0 = CE1->getOperand(0);
1506249259Sdim
1507249259Sdim    switch (CE1->getOpcode()) {
1508249259Sdim    case Instruction::Trunc:
1509249259Sdim    case Instruction::FPTrunc:
1510249259Sdim    case Instruction::FPExt:
1511249259Sdim    case Instruction::FPToUI:
1512249259Sdim    case Instruction::FPToSI:
1513249259Sdim      break; // We can't evaluate floating point casts or truncations.
1514249259Sdim
1515249259Sdim    case Instruction::UIToFP:
1516249259Sdim    case Instruction::SIToFP:
1517249259Sdim    case Instruction::BitCast:
1518249259Sdim    case Instruction::ZExt:
1519249259Sdim    case Instruction::SExt:
1520249259Sdim      // If the cast is not actually changing bits, and the second operand is a
1521249259Sdim      // null pointer, do the comparison with the pre-casted value.
1522249259Sdim      if (V2->isNullValue() &&
1523249259Sdim          (CE1->getType()->isPointerTy() || CE1->getType()->isIntegerTy())) {
1524249259Sdim        if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1525249259Sdim        if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
1526249259Sdim        return evaluateICmpRelation(CE1Op0,
1527249259Sdim                                    Constant::getNullValue(CE1Op0->getType()),
1528249259Sdim                                    isSigned);
1529249259Sdim      }
1530249259Sdim      break;
1531249259Sdim
1532276479Sdim    case Instruction::GetElementPtr: {
1533276479Sdim      GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
1534249259Sdim      // Ok, since this is a getelementptr, we know that the constant has a
1535249259Sdim      // pointer type.  Check the various cases.
1536249259Sdim      if (isa<ConstantPointerNull>(V2)) {
1537249259Sdim        // If we are comparing a GEP to a null pointer, check to see if the base
1538249259Sdim        // of the GEP equals the null pointer.
1539249259Sdim        if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1540249259Sdim          if (GV->hasExternalWeakLinkage())
1541249259Sdim            // Weak linkage GVals could be zero or not. We're comparing that
1542249259Sdim            // to null pointer so its greater-or-equal
1543249259Sdim            return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
1544249259Sdim          else
1545249259Sdim            // If its not weak linkage, the GVal must have a non-zero address
1546249259Sdim            // so the result is greater-than
1547249259Sdim            return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1548249259Sdim        } else if (isa<ConstantPointerNull>(CE1Op0)) {
1549249259Sdim          // If we are indexing from a null pointer, check to see if we have any
1550249259Sdim          // non-zero indices.
1551249259Sdim          for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1552249259Sdim            if (!CE1->getOperand(i)->isNullValue())
1553249259Sdim              // Offsetting from null, must not be equal.
1554249259Sdim              return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1555249259Sdim          // Only zero indexes from null, must still be zero.
1556249259Sdim          return ICmpInst::ICMP_EQ;
1557249259Sdim        }
1558249259Sdim        // Otherwise, we can't really say if the first operand is null or not.
1559249259Sdim      } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1560249259Sdim        if (isa<ConstantPointerNull>(CE1Op0)) {
1561249259Sdim          if (GV2->hasExternalWeakLinkage())
1562249259Sdim            // Weak linkage GVals could be zero or not. We're comparing it to
1563249259Sdim            // a null pointer, so its less-or-equal
1564249259Sdim            return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
1565249259Sdim          else
1566249259Sdim            // If its not weak linkage, the GVal must have a non-zero address
1567249259Sdim            // so the result is less-than
1568249259Sdim            return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1569249259Sdim        } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1570249259Sdim          if (GV == GV2) {
1571249259Sdim            // If this is a getelementptr of the same global, then it must be
1572249259Sdim            // different.  Because the types must match, the getelementptr could
1573249259Sdim            // only have at most one index, and because we fold getelementptr's
1574249259Sdim            // with a single zero index, it must be nonzero.
1575249259Sdim            assert(CE1->getNumOperands() == 2 &&
1576249259Sdim                   !CE1->getOperand(1)->isNullValue() &&
1577249259Sdim                   "Surprising getelementptr!");
1578249259Sdim            return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1579249259Sdim          } else {
1580276479Sdim            if (CE1GEP->hasAllZeroIndices())
1581276479Sdim              return areGlobalsPotentiallyEqual(GV, GV2);
1582249259Sdim            return ICmpInst::BAD_ICMP_PREDICATE;
1583249259Sdim          }
1584249259Sdim        }
1585249259Sdim      } else {
1586249259Sdim        ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1587249259Sdim        Constant *CE2Op0 = CE2->getOperand(0);
1588249259Sdim
1589249259Sdim        // There are MANY other foldings that we could perform here.  They will
1590249259Sdim        // probably be added on demand, as they seem needed.
1591249259Sdim        switch (CE2->getOpcode()) {
1592249259Sdim        default: break;
1593249259Sdim        case Instruction::GetElementPtr:
1594249259Sdim          // By far the most common case to handle is when the base pointers are
1595249259Sdim          // obviously to the same global.
1596249259Sdim          if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1597276479Sdim            // Don't know relative ordering, but check for inequality.
1598276479Sdim            if (CE1Op0 != CE2Op0) {
1599276479Sdim              GEPOperator *CE2GEP = cast<GEPOperator>(CE2);
1600276479Sdim              if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
1601276479Sdim                return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0),
1602276479Sdim                                                  cast<GlobalValue>(CE2Op0));
1603249259Sdim              return ICmpInst::BAD_ICMP_PREDICATE;
1604276479Sdim            }
1605249259Sdim            // Ok, we know that both getelementptr instructions are based on the
1606249259Sdim            // same global.  From this, we can precisely determine the relative
1607249259Sdim            // ordering of the resultant pointers.
1608249259Sdim            unsigned i = 1;
1609249259Sdim
1610249259Sdim            // The logic below assumes that the result of the comparison
1611249259Sdim            // can be determined by finding the first index that differs.
1612249259Sdim            // This doesn't work if there is over-indexing in any
1613249259Sdim            // subsequent indices, so check for that case first.
1614249259Sdim            if (!CE1->isGEPWithNoNotionalOverIndexing() ||
1615249259Sdim                !CE2->isGEPWithNoNotionalOverIndexing())
1616249259Sdim               return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1617249259Sdim
1618249259Sdim            // Compare all of the operands the GEP's have in common.
1619249259Sdim            gep_type_iterator GTI = gep_type_begin(CE1);
1620249259Sdim            for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1621249259Sdim                 ++i, ++GTI)
1622249259Sdim              switch (IdxCompare(CE1->getOperand(i),
1623249259Sdim                                 CE2->getOperand(i), GTI.getIndexedType())) {
1624249259Sdim              case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1625249259Sdim              case 1:  return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1626249259Sdim              case -2: return ICmpInst::BAD_ICMP_PREDICATE;
1627249259Sdim              }
1628249259Sdim
1629249259Sdim            // Ok, we ran out of things they have in common.  If any leftovers
1630249259Sdim            // are non-zero then we have a difference, otherwise we are equal.
1631249259Sdim            for (; i < CE1->getNumOperands(); ++i)
1632249259Sdim              if (!CE1->getOperand(i)->isNullValue()) {
1633249259Sdim                if (isa<ConstantInt>(CE1->getOperand(i)))
1634249259Sdim                  return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1635249259Sdim                else
1636249259Sdim                  return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1637249259Sdim              }
1638249259Sdim
1639249259Sdim            for (; i < CE2->getNumOperands(); ++i)
1640249259Sdim              if (!CE2->getOperand(i)->isNullValue()) {
1641249259Sdim                if (isa<ConstantInt>(CE2->getOperand(i)))
1642249259Sdim                  return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1643249259Sdim                else
1644249259Sdim                  return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1645249259Sdim              }
1646249259Sdim            return ICmpInst::ICMP_EQ;
1647249259Sdim          }
1648249259Sdim        }
1649249259Sdim      }
1650276479Sdim    }
1651249259Sdim    default:
1652249259Sdim      break;
1653249259Sdim    }
1654249259Sdim  }
1655249259Sdim
1656249259Sdim  return ICmpInst::BAD_ICMP_PREDICATE;
1657249259Sdim}
1658249259Sdim
1659249259SdimConstant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
1660249259Sdim                                               Constant *C1, Constant *C2) {
1661249259Sdim  Type *ResultTy;
1662249259Sdim  if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
1663249259Sdim    ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
1664249259Sdim                               VT->getNumElements());
1665249259Sdim  else
1666249259Sdim    ResultTy = Type::getInt1Ty(C1->getContext());
1667249259Sdim
1668249259Sdim  // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
1669249259Sdim  if (pred == FCmpInst::FCMP_FALSE)
1670249259Sdim    return Constant::getNullValue(ResultTy);
1671249259Sdim
1672249259Sdim  if (pred == FCmpInst::FCMP_TRUE)
1673249259Sdim    return Constant::getAllOnesValue(ResultTy);
1674249259Sdim
1675249259Sdim  // Handle some degenerate cases first
1676249259Sdim  if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1677288943Sdim    CmpInst::Predicate Predicate = CmpInst::Predicate(pred);
1678288943Sdim    bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
1679249259Sdim    // For EQ and NE, we can always pick a value for the undef to make the
1680249259Sdim    // predicate pass or fail, so we can return undef.
1681288943Sdim    // Also, if both operands are undef, we can return undef for int comparison.
1682288943Sdim    if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2))
1683249259Sdim      return UndefValue::get(ResultTy);
1684288943Sdim
1685288943Sdim    // Otherwise, for integer compare, pick the same value as the non-undef
1686288943Sdim    // operand, and fold it to true or false.
1687288943Sdim    if (isIntegerPredicate)
1688296417Sdim      return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(Predicate));
1689288943Sdim
1690288943Sdim    // Choosing NaN for the undef will always make unordered comparison succeed
1691288943Sdim    // and ordered comparison fails.
1692288943Sdim    return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate));
1693249259Sdim  }
1694249259Sdim
1695249259Sdim  // icmp eq/ne(null,GV) -> false/true
1696249259Sdim  if (C1->isNullValue()) {
1697249259Sdim    if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1698249259Sdim      // Don't try to evaluate aliases.  External weak GV can be null.
1699249259Sdim      if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
1700249259Sdim        if (pred == ICmpInst::ICMP_EQ)
1701249259Sdim          return ConstantInt::getFalse(C1->getContext());
1702249259Sdim        else if (pred == ICmpInst::ICMP_NE)
1703249259Sdim          return ConstantInt::getTrue(C1->getContext());
1704249259Sdim      }
1705249259Sdim  // icmp eq/ne(GV,null) -> false/true
1706249259Sdim  } else if (C2->isNullValue()) {
1707249259Sdim    if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1708249259Sdim      // Don't try to evaluate aliases.  External weak GV can be null.
1709249259Sdim      if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
1710249259Sdim        if (pred == ICmpInst::ICMP_EQ)
1711249259Sdim          return ConstantInt::getFalse(C1->getContext());
1712249259Sdim        else if (pred == ICmpInst::ICMP_NE)
1713249259Sdim          return ConstantInt::getTrue(C1->getContext());
1714249259Sdim      }
1715249259Sdim  }
1716249259Sdim
1717249259Sdim  // If the comparison is a comparison between two i1's, simplify it.
1718249259Sdim  if (C1->getType()->isIntegerTy(1)) {
1719249259Sdim    switch(pred) {
1720249259Sdim    case ICmpInst::ICMP_EQ:
1721249259Sdim      if (isa<ConstantInt>(C2))
1722249259Sdim        return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
1723249259Sdim      return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
1724249259Sdim    case ICmpInst::ICMP_NE:
1725249259Sdim      return ConstantExpr::getXor(C1, C2);
1726249259Sdim    default:
1727249259Sdim      break;
1728249259Sdim    }
1729249259Sdim  }
1730249259Sdim
1731249259Sdim  if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1732249259Sdim    APInt V1 = cast<ConstantInt>(C1)->getValue();
1733249259Sdim    APInt V2 = cast<ConstantInt>(C2)->getValue();
1734249259Sdim    switch (pred) {
1735249259Sdim    default: llvm_unreachable("Invalid ICmp Predicate");
1736249259Sdim    case ICmpInst::ICMP_EQ:  return ConstantInt::get(ResultTy, V1 == V2);
1737249259Sdim    case ICmpInst::ICMP_NE:  return ConstantInt::get(ResultTy, V1 != V2);
1738249259Sdim    case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2));
1739249259Sdim    case ICmpInst::ICMP_SGT: return ConstantInt::get(ResultTy, V1.sgt(V2));
1740249259Sdim    case ICmpInst::ICMP_SLE: return ConstantInt::get(ResultTy, V1.sle(V2));
1741249259Sdim    case ICmpInst::ICMP_SGE: return ConstantInt::get(ResultTy, V1.sge(V2));
1742249259Sdim    case ICmpInst::ICMP_ULT: return ConstantInt::get(ResultTy, V1.ult(V2));
1743249259Sdim    case ICmpInst::ICMP_UGT: return ConstantInt::get(ResultTy, V1.ugt(V2));
1744249259Sdim    case ICmpInst::ICMP_ULE: return ConstantInt::get(ResultTy, V1.ule(V2));
1745249259Sdim    case ICmpInst::ICMP_UGE: return ConstantInt::get(ResultTy, V1.uge(V2));
1746249259Sdim    }
1747249259Sdim  } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1748249259Sdim    APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
1749249259Sdim    APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
1750249259Sdim    APFloat::cmpResult R = C1V.compare(C2V);
1751249259Sdim    switch (pred) {
1752249259Sdim    default: llvm_unreachable("Invalid FCmp Predicate");
1753249259Sdim    case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy);
1754249259Sdim    case FCmpInst::FCMP_TRUE:  return Constant::getAllOnesValue(ResultTy);
1755249259Sdim    case FCmpInst::FCMP_UNO:
1756249259Sdim      return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered);
1757249259Sdim    case FCmpInst::FCMP_ORD:
1758249259Sdim      return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered);
1759249259Sdim    case FCmpInst::FCMP_UEQ:
1760249259Sdim      return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
1761249259Sdim                                        R==APFloat::cmpEqual);
1762249259Sdim    case FCmpInst::FCMP_OEQ:
1763249259Sdim      return ConstantInt::get(ResultTy, R==APFloat::cmpEqual);
1764249259Sdim    case FCmpInst::FCMP_UNE:
1765249259Sdim      return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual);
1766249259Sdim    case FCmpInst::FCMP_ONE:
1767249259Sdim      return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
1768249259Sdim                                        R==APFloat::cmpGreaterThan);
1769249259Sdim    case FCmpInst::FCMP_ULT:
1770249259Sdim      return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
1771249259Sdim                                        R==APFloat::cmpLessThan);
1772249259Sdim    case FCmpInst::FCMP_OLT:
1773249259Sdim      return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan);
1774249259Sdim    case FCmpInst::FCMP_UGT:
1775249259Sdim      return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
1776249259Sdim                                        R==APFloat::cmpGreaterThan);
1777249259Sdim    case FCmpInst::FCMP_OGT:
1778249259Sdim      return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan);
1779249259Sdim    case FCmpInst::FCMP_ULE:
1780249259Sdim      return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan);
1781249259Sdim    case FCmpInst::FCMP_OLE:
1782249259Sdim      return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
1783249259Sdim                                        R==APFloat::cmpEqual);
1784249259Sdim    case FCmpInst::FCMP_UGE:
1785249259Sdim      return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan);
1786249259Sdim    case FCmpInst::FCMP_OGE:
1787249259Sdim      return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan ||
1788249259Sdim                                        R==APFloat::cmpEqual);
1789249259Sdim    }
1790249259Sdim  } else if (C1->getType()->isVectorTy()) {
1791249259Sdim    // If we can constant fold the comparison of each element, constant fold
1792249259Sdim    // the whole vector comparison.
1793249259Sdim    SmallVector<Constant*, 4> ResElts;
1794249259Sdim    Type *Ty = IntegerType::get(C1->getContext(), 32);
1795249259Sdim    // Compare the elements, producing an i1 result or constant expr.
1796249259Sdim    for (unsigned i = 0, e = C1->getType()->getVectorNumElements(); i != e;++i){
1797249259Sdim      Constant *C1E =
1798249259Sdim        ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i));
1799249259Sdim      Constant *C2E =
1800249259Sdim        ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i));
1801249259Sdim
1802249259Sdim      ResElts.push_back(ConstantExpr::getCompare(pred, C1E, C2E));
1803249259Sdim    }
1804249259Sdim
1805249259Sdim    return ConstantVector::get(ResElts);
1806249259Sdim  }
1807249259Sdim
1808288943Sdim  if (C1->getType()->isFloatingPointTy() &&
1809288943Sdim      // Only call evaluateFCmpRelation if we have a constant expr to avoid
1810288943Sdim      // infinite recursive loop
1811288943Sdim      (isa<ConstantExpr>(C1) || isa<ConstantExpr>(C2))) {
1812249259Sdim    int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
1813249259Sdim    switch (evaluateFCmpRelation(C1, C2)) {
1814249259Sdim    default: llvm_unreachable("Unknown relation!");
1815249259Sdim    case FCmpInst::FCMP_UNO:
1816249259Sdim    case FCmpInst::FCMP_ORD:
1817249259Sdim    case FCmpInst::FCMP_UEQ:
1818249259Sdim    case FCmpInst::FCMP_UNE:
1819249259Sdim    case FCmpInst::FCMP_ULT:
1820249259Sdim    case FCmpInst::FCMP_UGT:
1821249259Sdim    case FCmpInst::FCMP_ULE:
1822249259Sdim    case FCmpInst::FCMP_UGE:
1823249259Sdim    case FCmpInst::FCMP_TRUE:
1824249259Sdim    case FCmpInst::FCMP_FALSE:
1825249259Sdim    case FCmpInst::BAD_FCMP_PREDICATE:
1826249259Sdim      break; // Couldn't determine anything about these constants.
1827249259Sdim    case FCmpInst::FCMP_OEQ: // We know that C1 == C2
1828249259Sdim      Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1829249259Sdim                pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1830249259Sdim                pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1831249259Sdim      break;
1832249259Sdim    case FCmpInst::FCMP_OLT: // We know that C1 < C2
1833249259Sdim      Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1834249259Sdim                pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1835249259Sdim                pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1836249259Sdim      break;
1837249259Sdim    case FCmpInst::FCMP_OGT: // We know that C1 > C2
1838249259Sdim      Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1839249259Sdim                pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1840249259Sdim                pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1841249259Sdim      break;
1842249259Sdim    case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1843249259Sdim      // We can only partially decide this relation.
1844249259Sdim      if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
1845249259Sdim        Result = 0;
1846249259Sdim      else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
1847249259Sdim        Result = 1;
1848249259Sdim      break;
1849249259Sdim    case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1850249259Sdim      // We can only partially decide this relation.
1851249259Sdim      if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
1852249259Sdim        Result = 0;
1853249259Sdim      else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
1854249259Sdim        Result = 1;
1855249259Sdim      break;
1856249259Sdim    case FCmpInst::FCMP_ONE: // We know that C1 != C2
1857249259Sdim      // We can only partially decide this relation.
1858249259Sdim      if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
1859249259Sdim        Result = 0;
1860249259Sdim      else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
1861249259Sdim        Result = 1;
1862249259Sdim      break;
1863249259Sdim    }
1864249259Sdim
1865249259Sdim    // If we evaluated the result, return it now.
1866249259Sdim    if (Result != -1)
1867249259Sdim      return ConstantInt::get(ResultTy, Result);
1868249259Sdim
1869249259Sdim  } else {
1870249259Sdim    // Evaluate the relation between the two constants, per the predicate.
1871249259Sdim    int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
1872296417Sdim    switch (evaluateICmpRelation(C1, C2,
1873296417Sdim                                 CmpInst::isSigned((CmpInst::Predicate)pred))) {
1874249259Sdim    default: llvm_unreachable("Unknown relational!");
1875249259Sdim    case ICmpInst::BAD_ICMP_PREDICATE:
1876249259Sdim      break;  // Couldn't determine anything about these constants.
1877249259Sdim    case ICmpInst::ICMP_EQ:   // We know the constants are equal!
1878249259Sdim      // If we know the constants are equal, we can decide the result of this
1879249259Sdim      // computation precisely.
1880249259Sdim      Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred);
1881249259Sdim      break;
1882249259Sdim    case ICmpInst::ICMP_ULT:
1883249259Sdim      switch (pred) {
1884249259Sdim      case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
1885249259Sdim        Result = 1; break;
1886249259Sdim      case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
1887249259Sdim        Result = 0; break;
1888249259Sdim      }
1889249259Sdim      break;
1890249259Sdim    case ICmpInst::ICMP_SLT:
1891249259Sdim      switch (pred) {
1892249259Sdim      case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
1893249259Sdim        Result = 1; break;
1894249259Sdim      case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
1895249259Sdim        Result = 0; break;
1896249259Sdim      }
1897249259Sdim      break;
1898249259Sdim    case ICmpInst::ICMP_UGT:
1899249259Sdim      switch (pred) {
1900249259Sdim      case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
1901249259Sdim        Result = 1; break;
1902249259Sdim      case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
1903249259Sdim        Result = 0; break;
1904249259Sdim      }
1905249259Sdim      break;
1906249259Sdim    case ICmpInst::ICMP_SGT:
1907249259Sdim      switch (pred) {
1908249259Sdim      case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
1909249259Sdim        Result = 1; break;
1910249259Sdim      case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
1911249259Sdim        Result = 0; break;
1912249259Sdim      }
1913249259Sdim      break;
1914249259Sdim    case ICmpInst::ICMP_ULE:
1915249259Sdim      if (pred == ICmpInst::ICMP_UGT) Result = 0;
1916249259Sdim      if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1;
1917249259Sdim      break;
1918249259Sdim    case ICmpInst::ICMP_SLE:
1919249259Sdim      if (pred == ICmpInst::ICMP_SGT) Result = 0;
1920249259Sdim      if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1;
1921249259Sdim      break;
1922249259Sdim    case ICmpInst::ICMP_UGE:
1923249259Sdim      if (pred == ICmpInst::ICMP_ULT) Result = 0;
1924249259Sdim      if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1;
1925249259Sdim      break;
1926249259Sdim    case ICmpInst::ICMP_SGE:
1927249259Sdim      if (pred == ICmpInst::ICMP_SLT) Result = 0;
1928249259Sdim      if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1;
1929249259Sdim      break;
1930249259Sdim    case ICmpInst::ICMP_NE:
1931249259Sdim      if (pred == ICmpInst::ICMP_EQ) Result = 0;
1932249259Sdim      if (pred == ICmpInst::ICMP_NE) Result = 1;
1933249259Sdim      break;
1934249259Sdim    }
1935249259Sdim
1936249259Sdim    // If we evaluated the result, return it now.
1937249259Sdim    if (Result != -1)
1938249259Sdim      return ConstantInt::get(ResultTy, Result);
1939249259Sdim
1940249259Sdim    // If the right hand side is a bitcast, try using its inverse to simplify
1941249259Sdim    // it by moving it to the left hand side.  We can't do this if it would turn
1942249259Sdim    // a vector compare into a scalar compare or visa versa.
1943249259Sdim    if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
1944249259Sdim      Constant *CE2Op0 = CE2->getOperand(0);
1945249259Sdim      if (CE2->getOpcode() == Instruction::BitCast &&
1946249259Sdim          CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy()) {
1947249259Sdim        Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
1948249259Sdim        return ConstantExpr::getICmp(pred, Inverse, CE2Op0);
1949249259Sdim      }
1950249259Sdim    }
1951249259Sdim
1952249259Sdim    // If the left hand side is an extension, try eliminating it.
1953249259Sdim    if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1954296417Sdim      if ((CE1->getOpcode() == Instruction::SExt &&
1955296417Sdim           ICmpInst::isSigned((ICmpInst::Predicate)pred)) ||
1956296417Sdim          (CE1->getOpcode() == Instruction::ZExt &&
1957296417Sdim           !ICmpInst::isSigned((ICmpInst::Predicate)pred))){
1958249259Sdim        Constant *CE1Op0 = CE1->getOperand(0);
1959249259Sdim        Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
1960249259Sdim        if (CE1Inverse == CE1Op0) {
1961249259Sdim          // Check whether we can safely truncate the right hand side.
1962249259Sdim          Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
1963261991Sdim          if (ConstantExpr::getCast(CE1->getOpcode(), C2Inverse,
1964261991Sdim                                    C2->getType()) == C2)
1965249259Sdim            return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse);
1966249259Sdim        }
1967249259Sdim      }
1968249259Sdim    }
1969249259Sdim
1970249259Sdim    if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
1971249259Sdim        (C1->isNullValue() && !C2->isNullValue())) {
1972249259Sdim      // If C2 is a constant expr and C1 isn't, flip them around and fold the
1973249259Sdim      // other way if possible.
1974249259Sdim      // Also, if C1 is null and C2 isn't, flip them around.
1975249259Sdim      pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
1976249259Sdim      return ConstantExpr::getICmp(pred, C2, C1);
1977249259Sdim    }
1978249259Sdim  }
1979276479Sdim  return nullptr;
1980249259Sdim}
1981249259Sdim
1982249259Sdim/// isInBoundsIndices - Test whether the given sequence of *normalized* indices
1983249259Sdim/// is "inbounds".
1984249259Sdimtemplate<typename IndexTy>
1985249259Sdimstatic bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) {
1986249259Sdim  // No indices means nothing that could be out of bounds.
1987249259Sdim  if (Idxs.empty()) return true;
1988249259Sdim
1989249259Sdim  // If the first index is zero, it's in bounds.
1990249259Sdim  if (cast<Constant>(Idxs[0])->isNullValue()) return true;
1991249259Sdim
1992249259Sdim  // If the first index is one and all the rest are zero, it's in bounds,
1993249259Sdim  // by the one-past-the-end rule.
1994249259Sdim  if (!cast<ConstantInt>(Idxs[0])->isOne())
1995249259Sdim    return false;
1996249259Sdim  for (unsigned i = 1, e = Idxs.size(); i != e; ++i)
1997249259Sdim    if (!cast<Constant>(Idxs[i])->isNullValue())
1998249259Sdim      return false;
1999249259Sdim  return true;
2000249259Sdim}
2001249259Sdim
2002261991Sdim/// \brief Test whether a given ConstantInt is in-range for a SequentialType.
2003296417Sdimstatic bool isIndexInRangeOfSequentialType(SequentialType *STy,
2004261991Sdim                                           const ConstantInt *CI) {
2005296417Sdim  // And indices are valid when indexing along a pointer
2006296417Sdim  if (isa<PointerType>(STy))
2007296417Sdim    return true;
2008261991Sdim
2009261991Sdim  uint64_t NumElements = 0;
2010261991Sdim  // Determine the number of elements in our sequential type.
2011296417Sdim  if (auto *ATy = dyn_cast<ArrayType>(STy))
2012261991Sdim    NumElements = ATy->getNumElements();
2013296417Sdim  else if (auto *VTy = dyn_cast<VectorType>(STy))
2014261991Sdim    NumElements = VTy->getNumElements();
2015261991Sdim
2016261991Sdim  assert((isa<ArrayType>(STy) || NumElements > 0) &&
2017261991Sdim         "didn't expect non-array type to have zero elements!");
2018261991Sdim
2019261991Sdim  // We cannot bounds check the index if it doesn't fit in an int64_t.
2020261991Sdim  if (CI->getValue().getActiveBits() > 64)
2021261991Sdim    return false;
2022261991Sdim
2023261991Sdim  // A negative index or an index past the end of our sequential type is
2024261991Sdim  // considered out-of-range.
2025261991Sdim  int64_t IndexVal = CI->getSExtValue();
2026261991Sdim  if (IndexVal < 0 || (NumElements > 0 && (uint64_t)IndexVal >= NumElements))
2027261991Sdim    return false;
2028261991Sdim
2029261991Sdim  // Otherwise, it is in-range.
2030261991Sdim  return true;
2031261991Sdim}
2032261991Sdim
2033249259Sdimtemplate<typename IndexTy>
2034288943Sdimstatic Constant *ConstantFoldGetElementPtrImpl(Type *PointeeTy, Constant *C,
2035249259Sdim                                               bool inBounds,
2036249259Sdim                                               ArrayRef<IndexTy> Idxs) {
2037249259Sdim  if (Idxs.empty()) return C;
2038249259Sdim  Constant *Idx0 = cast<Constant>(Idxs[0]);
2039249259Sdim  if ((Idxs.size() == 1 && Idx0->isNullValue()))
2040249259Sdim    return C;
2041249259Sdim
2042249259Sdim  if (isa<UndefValue>(C)) {
2043249259Sdim    PointerType *Ptr = cast<PointerType>(C->getType());
2044288943Sdim    Type *Ty = GetElementPtrInst::getIndexedType(
2045288943Sdim        cast<PointerType>(Ptr->getScalarType())->getElementType(), Idxs);
2046276479Sdim    assert(Ty && "Invalid indices for GEP!");
2047249259Sdim    return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
2048249259Sdim  }
2049249259Sdim
2050249259Sdim  if (C->isNullValue()) {
2051249259Sdim    bool isNull = true;
2052249259Sdim    for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
2053249259Sdim      if (!cast<Constant>(Idxs[i])->isNullValue()) {
2054249259Sdim        isNull = false;
2055249259Sdim        break;
2056249259Sdim      }
2057249259Sdim    if (isNull) {
2058249259Sdim      PointerType *Ptr = cast<PointerType>(C->getType());
2059288943Sdim      Type *Ty = GetElementPtrInst::getIndexedType(
2060288943Sdim          cast<PointerType>(Ptr->getScalarType())->getElementType(), Idxs);
2061276479Sdim      assert(Ty && "Invalid indices for GEP!");
2062249259Sdim      return ConstantPointerNull::get(PointerType::get(Ty,
2063249259Sdim                                                       Ptr->getAddressSpace()));
2064249259Sdim    }
2065249259Sdim  }
2066249259Sdim
2067249259Sdim  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2068249259Sdim    // Combine Indices - If the source pointer to this getelementptr instruction
2069249259Sdim    // is a getelementptr instruction, combine the indices of the two
2070249259Sdim    // getelementptr instructions into a single instruction.
2071249259Sdim    //
2072249259Sdim    if (CE->getOpcode() == Instruction::GetElementPtr) {
2073276479Sdim      Type *LastTy = nullptr;
2074249259Sdim      for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
2075249259Sdim           I != E; ++I)
2076249259Sdim        LastTy = *I;
2077249259Sdim
2078261991Sdim      // We cannot combine indices if doing so would take us outside of an
2079261991Sdim      // array or vector.  Doing otherwise could trick us if we evaluated such a
2080261991Sdim      // GEP as part of a load.
2081261991Sdim      //
2082261991Sdim      // e.g. Consider if the original GEP was:
2083261991Sdim      // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c,
2084261991Sdim      //                    i32 0, i32 0, i64 0)
2085261991Sdim      //
2086261991Sdim      // If we then tried to offset it by '8' to get to the third element,
2087261991Sdim      // an i8, we should *not* get:
2088261991Sdim      // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c,
2089261991Sdim      //                    i32 0, i32 0, i64 8)
2090261991Sdim      //
2091261991Sdim      // This GEP tries to index array element '8  which runs out-of-bounds.
2092261991Sdim      // Subsequent evaluation would get confused and produce erroneous results.
2093261991Sdim      //
2094261991Sdim      // The following prohibits such a GEP from being formed by checking to see
2095261991Sdim      // if the index is in-range with respect to an array or vector.
2096261991Sdim      bool PerformFold = false;
2097261991Sdim      if (Idx0->isNullValue())
2098261991Sdim        PerformFold = true;
2099261991Sdim      else if (SequentialType *STy = dyn_cast_or_null<SequentialType>(LastTy))
2100261991Sdim        if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx0))
2101261991Sdim          PerformFold = isIndexInRangeOfSequentialType(STy, CI);
2102261991Sdim
2103261991Sdim      if (PerformFold) {
2104249259Sdim        SmallVector<Value*, 16> NewIndices;
2105249259Sdim        NewIndices.reserve(Idxs.size() + CE->getNumOperands());
2106288943Sdim        NewIndices.append(CE->op_begin() + 1, CE->op_end() - 1);
2107249259Sdim
2108249259Sdim        // Add the last index of the source with the first index of the new GEP.
2109249259Sdim        // Make sure to handle the case when they are actually different types.
2110249259Sdim        Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
2111249259Sdim        // Otherwise it must be an array.
2112249259Sdim        if (!Idx0->isNullValue()) {
2113249259Sdim          Type *IdxTy = Combined->getType();
2114249259Sdim          if (IdxTy != Idx0->getType()) {
2115288943Sdim            unsigned CommonExtendedWidth =
2116288943Sdim                std::max(IdxTy->getIntegerBitWidth(),
2117288943Sdim                         Idx0->getType()->getIntegerBitWidth());
2118288943Sdim            CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
2119288943Sdim
2120288943Sdim            Type *CommonTy =
2121288943Sdim                Type::getIntNTy(IdxTy->getContext(), CommonExtendedWidth);
2122288943Sdim            Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, CommonTy);
2123288943Sdim            Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, CommonTy);
2124249259Sdim            Combined = ConstantExpr::get(Instruction::Add, C1, C2);
2125249259Sdim          } else {
2126249259Sdim            Combined =
2127249259Sdim              ConstantExpr::get(Instruction::Add, Idx0, Combined);
2128249259Sdim          }
2129249259Sdim        }
2130249259Sdim
2131249259Sdim        NewIndices.push_back(Combined);
2132249259Sdim        NewIndices.append(Idxs.begin() + 1, Idxs.end());
2133288943Sdim        return ConstantExpr::getGetElementPtr(
2134288943Sdim            cast<GEPOperator>(CE)->getSourceElementType(), CE->getOperand(0),
2135288943Sdim            NewIndices, inBounds && cast<GEPOperator>(CE)->isInBounds());
2136249259Sdim      }
2137249259Sdim    }
2138249259Sdim
2139249259Sdim    // Attempt to fold casts to the same type away.  For example, folding:
2140249259Sdim    //
2141249259Sdim    //   i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
2142249259Sdim    //                       i64 0, i64 0)
2143249259Sdim    // into:
2144249259Sdim    //
2145249259Sdim    //   i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
2146249259Sdim    //
2147249259Sdim    // Don't fold if the cast is changing address spaces.
2148249259Sdim    if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) {
2149249259Sdim      PointerType *SrcPtrTy =
2150249259Sdim        dyn_cast<PointerType>(CE->getOperand(0)->getType());
2151249259Sdim      PointerType *DstPtrTy = dyn_cast<PointerType>(CE->getType());
2152249259Sdim      if (SrcPtrTy && DstPtrTy) {
2153249259Sdim        ArrayType *SrcArrayTy =
2154249259Sdim          dyn_cast<ArrayType>(SrcPtrTy->getElementType());
2155249259Sdim        ArrayType *DstArrayTy =
2156249259Sdim          dyn_cast<ArrayType>(DstPtrTy->getElementType());
2157249259Sdim        if (SrcArrayTy && DstArrayTy
2158249259Sdim            && SrcArrayTy->getElementType() == DstArrayTy->getElementType()
2159249259Sdim            && SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
2160288943Sdim          return ConstantExpr::getGetElementPtr(
2161288943Sdim              SrcArrayTy, (Constant *)CE->getOperand(0), Idxs, inBounds);
2162249259Sdim      }
2163249259Sdim    }
2164249259Sdim  }
2165249259Sdim
2166249259Sdim  // Check to see if any array indices are not within the corresponding
2167261991Sdim  // notional array or vector bounds. If so, try to determine if they can be
2168261991Sdim  // factored out into preceding dimensions.
2169249259Sdim  SmallVector<Constant *, 8> NewIdxs;
2170288943Sdim  Type *Ty = PointeeTy;
2171288943Sdim  Type *Prev = C->getType();
2172288943Sdim  bool Unknown = !isa<ConstantInt>(Idxs[0]);
2173288943Sdim  for (unsigned i = 1, e = Idxs.size(); i != e;
2174249259Sdim       Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
2175249259Sdim    if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
2176261991Sdim      if (isa<ArrayType>(Ty) || isa<VectorType>(Ty))
2177261991Sdim        if (CI->getSExtValue() > 0 &&
2178261991Sdim            !isIndexInRangeOfSequentialType(cast<SequentialType>(Ty), CI)) {
2179249259Sdim          if (isa<SequentialType>(Prev)) {
2180249259Sdim            // It's out of range, but we can factor it into the prior
2181249259Sdim            // dimension.
2182249259Sdim            NewIdxs.resize(Idxs.size());
2183261991Sdim            uint64_t NumElements = 0;
2184296417Sdim            if (auto *ATy = dyn_cast<ArrayType>(Ty))
2185261991Sdim              NumElements = ATy->getNumElements();
2186261991Sdim            else
2187261991Sdim              NumElements = cast<VectorType>(Ty)->getNumElements();
2188261991Sdim
2189261991Sdim            ConstantInt *Factor = ConstantInt::get(CI->getType(), NumElements);
2190249259Sdim            NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
2191249259Sdim
2192249259Sdim            Constant *PrevIdx = cast<Constant>(Idxs[i-1]);
2193249259Sdim            Constant *Div = ConstantExpr::getSDiv(CI, Factor);
2194249259Sdim
2195288943Sdim            unsigned CommonExtendedWidth =
2196288943Sdim                std::max(PrevIdx->getType()->getIntegerBitWidth(),
2197288943Sdim                         Div->getType()->getIntegerBitWidth());
2198288943Sdim            CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
2199288943Sdim
2200249259Sdim            // Before adding, extend both operands to i64 to avoid
2201249259Sdim            // overflow trouble.
2202288943Sdim            if (!PrevIdx->getType()->isIntegerTy(CommonExtendedWidth))
2203288943Sdim              PrevIdx = ConstantExpr::getSExt(
2204288943Sdim                  PrevIdx,
2205288943Sdim                  Type::getIntNTy(Div->getContext(), CommonExtendedWidth));
2206288943Sdim            if (!Div->getType()->isIntegerTy(CommonExtendedWidth))
2207288943Sdim              Div = ConstantExpr::getSExt(
2208288943Sdim                  Div, Type::getIntNTy(Div->getContext(), CommonExtendedWidth));
2209249259Sdim
2210249259Sdim            NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div);
2211249259Sdim          } else {
2212249259Sdim            // It's out of range, but the prior dimension is a struct
2213249259Sdim            // so we can't do anything about it.
2214249259Sdim            Unknown = true;
2215249259Sdim          }
2216249259Sdim        }
2217249259Sdim    } else {
2218249259Sdim      // We don't know if it's in range or not.
2219249259Sdim      Unknown = true;
2220249259Sdim    }
2221249259Sdim  }
2222249259Sdim
2223249259Sdim  // If we did any factoring, start over with the adjusted indices.
2224249259Sdim  if (!NewIdxs.empty()) {
2225249259Sdim    for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
2226249259Sdim      if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
2227288943Sdim    return ConstantExpr::getGetElementPtr(PointeeTy, C, NewIdxs, inBounds);
2228249259Sdim  }
2229249259Sdim
2230249259Sdim  // If all indices are known integers and normalized, we can do a simple
2231249259Sdim  // check for the "inbounds" property.
2232280031Sdim  if (!Unknown && !inBounds)
2233280031Sdim    if (auto *GV = dyn_cast<GlobalVariable>(C))
2234280031Sdim      if (!GV->hasExternalWeakLinkage() && isInBoundsIndices(Idxs))
2235288943Sdim        return ConstantExpr::getInBoundsGetElementPtr(PointeeTy, C, Idxs);
2236249259Sdim
2237276479Sdim  return nullptr;
2238249259Sdim}
2239249259Sdim
2240249259SdimConstant *llvm::ConstantFoldGetElementPtr(Constant *C,
2241249259Sdim                                          bool inBounds,
2242249259Sdim                                          ArrayRef<Constant *> Idxs) {
2243288943Sdim  return ConstantFoldGetElementPtrImpl(
2244288943Sdim      cast<PointerType>(C->getType()->getScalarType())->getElementType(), C,
2245288943Sdim      inBounds, Idxs);
2246249259Sdim}
2247249259Sdim
2248249259SdimConstant *llvm::ConstantFoldGetElementPtr(Constant *C,
2249249259Sdim                                          bool inBounds,
2250249259Sdim                                          ArrayRef<Value *> Idxs) {
2251288943Sdim  return ConstantFoldGetElementPtrImpl(
2252288943Sdim      cast<PointerType>(C->getType()->getScalarType())->getElementType(), C,
2253288943Sdim      inBounds, Idxs);
2254249259Sdim}
2255288943Sdim
2256288943SdimConstant *llvm::ConstantFoldGetElementPtr(Type *Ty, Constant *C,
2257288943Sdim                                          bool inBounds,
2258288943Sdim                                          ArrayRef<Constant *> Idxs) {
2259288943Sdim  return ConstantFoldGetElementPtrImpl(Ty, C, inBounds, Idxs);
2260288943Sdim}
2261288943Sdim
2262288943SdimConstant *llvm::ConstantFoldGetElementPtr(Type *Ty, Constant *C,
2263288943Sdim                                          bool inBounds,
2264288943Sdim                                          ArrayRef<Value *> Idxs) {
2265288943Sdim  return ConstantFoldGetElementPtrImpl(Ty, C, inBounds, Idxs);
2266288943Sdim}
2267