1 //===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements folding of constants for LLVM.  This implements the
10 // (internal) ConstantFold.h interface, which is used by the
11 // ConstantExpr::get* methods to automatically fold constants when possible.
12 //
13 // The current constant folding implementation is implemented in two pieces: the
14 // pieces that don't need DataLayout, and the pieces that do. This is to avoid
15 // a dependence in IR on Target.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "ConstantFold.h"
20 #include "llvm/ADT/APSInt.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GetElementPtrTypeIterator.h"
26 #include "llvm/IR/GlobalAlias.h"
27 #include "llvm/IR/GlobalVariable.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Operator.h"
31 #include "llvm/IR/PatternMatch.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/MathExtras.h"
35 using namespace llvm;
36 using namespace llvm::PatternMatch;
37 
38 //===----------------------------------------------------------------------===//
39 //                ConstantFold*Instruction Implementations
40 //===----------------------------------------------------------------------===//
41 
42 /// Convert the specified vector Constant node to the specified vector type.
43 /// At this point, we know that the elements of the input vector constant are
44 /// all simple integer or FP values.
45 static Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) {
46 
47   if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
48   if (CV->isNullValue()) return Constant::getNullValue(DstTy);
49 
50   // Do not iterate on scalable vector. The num of elements is unknown at
51   // compile-time.
52   if (isa<ScalableVectorType>(DstTy))
53     return nullptr;
54 
55   // If this cast changes element count then we can't handle it here:
56   // doing so requires endianness information.  This should be handled by
57   // Analysis/ConstantFolding.cpp
58   unsigned NumElts = cast<FixedVectorType>(DstTy)->getNumElements();
59   if (NumElts != cast<FixedVectorType>(CV->getType())->getNumElements())
60     return nullptr;
61 
62   Type *DstEltTy = DstTy->getElementType();
63   // Fast path for splatted constants.
64   if (Constant *Splat = CV->getSplatValue()) {
65     return ConstantVector::getSplat(DstTy->getElementCount(),
66                                     ConstantExpr::getBitCast(Splat, DstEltTy));
67   }
68 
69   SmallVector<Constant*, 16> Result;
70   Type *Ty = IntegerType::get(CV->getContext(), 32);
71   for (unsigned i = 0; i != NumElts; ++i) {
72     Constant *C =
73       ConstantExpr::getExtractElement(CV, ConstantInt::get(Ty, i));
74     C = ConstantExpr::getBitCast(C, DstEltTy);
75     Result.push_back(C);
76   }
77 
78   return ConstantVector::get(Result);
79 }
80 
81 /// This function determines which opcode to use to fold two constant cast
82 /// expressions together. It uses CastInst::isEliminableCastPair to determine
83 /// the opcode. Consequently its just a wrapper around that function.
84 /// Determine if it is valid to fold a cast of a cast
85 static unsigned
86 foldConstantCastPair(
87   unsigned opc,          ///< opcode of the second cast constant expression
88   ConstantExpr *Op,      ///< the first cast constant expression
89   Type *DstTy            ///< destination type of the first cast
90 ) {
91   assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
92   assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
93   assert(CastInst::isCast(opc) && "Invalid cast opcode");
94 
95   // The types and opcodes for the two Cast constant expressions
96   Type *SrcTy = Op->getOperand(0)->getType();
97   Type *MidTy = Op->getType();
98   Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
99   Instruction::CastOps secondOp = Instruction::CastOps(opc);
100 
101   // Assume that pointers are never more than 64 bits wide, and only use this
102   // for the middle type. Otherwise we could end up folding away illegal
103   // bitcasts between address spaces with different sizes.
104   IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext());
105 
106   // Let CastInst::isEliminableCastPair do the heavy lifting.
107   return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
108                                         nullptr, FakeIntPtrTy, nullptr);
109 }
110 
111 static Constant *FoldBitCast(Constant *V, Type *DestTy) {
112   Type *SrcTy = V->getType();
113   if (SrcTy == DestTy)
114     return V; // no-op cast
115 
116   // Check to see if we are casting a pointer to an aggregate to a pointer to
117   // the first element.  If so, return the appropriate GEP instruction.
118   if (PointerType *PTy = dyn_cast<PointerType>(V->getType()))
119     if (PointerType *DPTy = dyn_cast<PointerType>(DestTy))
120       if (PTy->getAddressSpace() == DPTy->getAddressSpace() &&
121           !PTy->isOpaque() && !DPTy->isOpaque() &&
122           PTy->getElementType()->isSized()) {
123         SmallVector<Value*, 8> IdxList;
124         Value *Zero =
125           Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));
126         IdxList.push_back(Zero);
127         Type *ElTy = PTy->getElementType();
128         while (ElTy && ElTy != DPTy->getElementType()) {
129           ElTy = GetElementPtrInst::getTypeAtIndex(ElTy, (uint64_t)0);
130           IdxList.push_back(Zero);
131         }
132 
133         if (ElTy == DPTy->getElementType())
134           // This GEP is inbounds because all indices are zero.
135           return ConstantExpr::getInBoundsGetElementPtr(PTy->getElementType(),
136                                                         V, IdxList);
137       }
138 
139   // Handle casts from one vector constant to another.  We know that the src
140   // and dest type have the same size (otherwise its an illegal cast).
141   if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
142     if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
143       assert(DestPTy->getPrimitiveSizeInBits() ==
144                  SrcTy->getPrimitiveSizeInBits() &&
145              "Not cast between same sized vectors!");
146       SrcTy = nullptr;
147       // First, check for null.  Undef is already handled.
148       if (isa<ConstantAggregateZero>(V))
149         return Constant::getNullValue(DestTy);
150 
151       // Handle ConstantVector and ConstantAggregateVector.
152       return BitCastConstantVector(V, DestPTy);
153     }
154 
155     // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
156     // This allows for other simplifications (although some of them
157     // can only be handled by Analysis/ConstantFolding.cpp).
158     if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
159       return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy);
160   }
161 
162   // Finally, implement bitcast folding now.   The code below doesn't handle
163   // bitcast right.
164   if (isa<ConstantPointerNull>(V))  // ptr->ptr cast.
165     return ConstantPointerNull::get(cast<PointerType>(DestTy));
166 
167   // Handle integral constant input.
168   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
169     if (DestTy->isIntegerTy())
170       // Integral -> Integral. This is a no-op because the bit widths must
171       // be the same. Consequently, we just fold to V.
172       return V;
173 
174     // See note below regarding the PPC_FP128 restriction.
175     if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty())
176       return ConstantFP::get(DestTy->getContext(),
177                              APFloat(DestTy->getFltSemantics(),
178                                      CI->getValue()));
179 
180     // Otherwise, can't fold this (vector?)
181     return nullptr;
182   }
183 
184   // Handle ConstantFP input: FP -> Integral.
185   if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
186     // PPC_FP128 is really the sum of two consecutive doubles, where the first
187     // double is always stored first in memory, regardless of the target
188     // endianness. The memory layout of i128, however, depends on the target
189     // endianness, and so we can't fold this without target endianness
190     // information. This should instead be handled by
191     // Analysis/ConstantFolding.cpp
192     if (FP->getType()->isPPC_FP128Ty())
193       return nullptr;
194 
195     // Make sure dest type is compatible with the folded integer constant.
196     if (!DestTy->isIntegerTy())
197       return nullptr;
198 
199     return ConstantInt::get(FP->getContext(),
200                             FP->getValueAPF().bitcastToAPInt());
201   }
202 
203   return nullptr;
204 }
205 
206 
207 /// V is an integer constant which only has a subset of its bytes used.
208 /// The bytes used are indicated by ByteStart (which is the first byte used,
209 /// counting from the least significant byte) and ByteSize, which is the number
210 /// of bytes used.
211 ///
212 /// This function analyzes the specified constant to see if the specified byte
213 /// range can be returned as a simplified constant.  If so, the constant is
214 /// returned, otherwise null is returned.
215 static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
216                                       unsigned ByteSize) {
217   assert(C->getType()->isIntegerTy() &&
218          (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
219          "Non-byte sized integer input");
220   unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
221   assert(ByteSize && "Must be accessing some piece");
222   assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
223   assert(ByteSize != CSize && "Should not extract everything");
224 
225   // Constant Integers are simple.
226   if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
227     APInt V = CI->getValue();
228     if (ByteStart)
229       V.lshrInPlace(ByteStart*8);
230     V = V.trunc(ByteSize*8);
231     return ConstantInt::get(CI->getContext(), V);
232   }
233 
234   // In the input is a constant expr, we might be able to recursively simplify.
235   // If not, we definitely can't do anything.
236   ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
237   if (!CE) return nullptr;
238 
239   switch (CE->getOpcode()) {
240   default: return nullptr;
241   case Instruction::Or: {
242     Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
243     if (!RHS)
244       return nullptr;
245 
246     // X | -1 -> -1.
247     if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS))
248       if (RHSC->isMinusOne())
249         return RHSC;
250 
251     Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
252     if (!LHS)
253       return nullptr;
254     return ConstantExpr::getOr(LHS, RHS);
255   }
256   case Instruction::And: {
257     Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
258     if (!RHS)
259       return nullptr;
260 
261     // X & 0 -> 0.
262     if (RHS->isNullValue())
263       return RHS;
264 
265     Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
266     if (!LHS)
267       return nullptr;
268     return ConstantExpr::getAnd(LHS, RHS);
269   }
270   case Instruction::LShr: {
271     ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
272     if (!Amt)
273       return nullptr;
274     APInt ShAmt = Amt->getValue();
275     // Cannot analyze non-byte shifts.
276     if ((ShAmt & 7) != 0)
277       return nullptr;
278     ShAmt.lshrInPlace(3);
279 
280     // If the extract is known to be all zeros, return zero.
281     if (ShAmt.uge(CSize - ByteStart))
282       return Constant::getNullValue(
283           IntegerType::get(CE->getContext(), ByteSize * 8));
284     // If the extract is known to be fully in the input, extract it.
285     if (ShAmt.ule(CSize - (ByteStart + ByteSize)))
286       return ExtractConstantBytes(CE->getOperand(0),
287                                   ByteStart + ShAmt.getZExtValue(), ByteSize);
288 
289     // TODO: Handle the 'partially zero' case.
290     return nullptr;
291   }
292 
293   case Instruction::Shl: {
294     ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
295     if (!Amt)
296       return nullptr;
297     APInt ShAmt = Amt->getValue();
298     // Cannot analyze non-byte shifts.
299     if ((ShAmt & 7) != 0)
300       return nullptr;
301     ShAmt.lshrInPlace(3);
302 
303     // If the extract is known to be all zeros, return zero.
304     if (ShAmt.uge(ByteStart + ByteSize))
305       return Constant::getNullValue(
306           IntegerType::get(CE->getContext(), ByteSize * 8));
307     // If the extract is known to be fully in the input, extract it.
308     if (ShAmt.ule(ByteStart))
309       return ExtractConstantBytes(CE->getOperand(0),
310                                   ByteStart - ShAmt.getZExtValue(), ByteSize);
311 
312     // TODO: Handle the 'partially zero' case.
313     return nullptr;
314   }
315 
316   case Instruction::ZExt: {
317     unsigned SrcBitSize =
318       cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth();
319 
320     // If extracting something that is completely zero, return 0.
321     if (ByteStart*8 >= SrcBitSize)
322       return Constant::getNullValue(IntegerType::get(CE->getContext(),
323                                                      ByteSize*8));
324 
325     // If exactly extracting the input, return it.
326     if (ByteStart == 0 && ByteSize*8 == SrcBitSize)
327       return CE->getOperand(0);
328 
329     // If extracting something completely in the input, if the input is a
330     // multiple of 8 bits, recurse.
331     if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize)
332       return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize);
333 
334     // Otherwise, if extracting a subset of the input, which is not multiple of
335     // 8 bits, do a shift and trunc to get the bits.
336     if ((ByteStart+ByteSize)*8 < SrcBitSize) {
337       assert((SrcBitSize&7) && "Shouldn't get byte sized case here");
338       Constant *Res = CE->getOperand(0);
339       if (ByteStart)
340         Res = ConstantExpr::getLShr(Res,
341                                  ConstantInt::get(Res->getType(), ByteStart*8));
342       return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(),
343                                                           ByteSize*8));
344     }
345 
346     // TODO: Handle the 'partially zero' case.
347     return nullptr;
348   }
349   }
350 }
351 
352 /// Wrapper around getFoldedSizeOfImpl() that adds caching.
353 static Constant *getFoldedSizeOf(Type *Ty, Type *DestTy, bool Folded,
354                                  DenseMap<Type *, Constant *> &Cache);
355 
356 /// Return a ConstantExpr with type DestTy for sizeof on Ty, with any known
357 /// factors factored out. If Folded is false, return null if no factoring was
358 /// possible, to avoid endlessly bouncing an unfoldable expression back into the
359 /// top-level folder.
360 static Constant *getFoldedSizeOfImpl(Type *Ty, Type *DestTy, bool Folded,
361                                      DenseMap<Type *, Constant *> &Cache) {
362   // This is the actual implementation of getFoldedSizeOf(). To get the caching
363   // behavior, we need to call getFoldedSizeOf() when we recurse.
364 
365   if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
366     Constant *N = ConstantInt::get(DestTy, ATy->getNumElements());
367     Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true, Cache);
368     return ConstantExpr::getNUWMul(E, N);
369   }
370 
371   if (StructType *STy = dyn_cast<StructType>(Ty))
372     if (!STy->isPacked()) {
373       unsigned NumElems = STy->getNumElements();
374       // An empty struct has size zero.
375       if (NumElems == 0)
376         return ConstantExpr::getNullValue(DestTy);
377       // Check for a struct with all members having the same size.
378       Constant *MemberSize =
379           getFoldedSizeOf(STy->getElementType(0), DestTy, true, Cache);
380       bool AllSame = true;
381       for (unsigned i = 1; i != NumElems; ++i)
382         if (MemberSize !=
383             getFoldedSizeOf(STy->getElementType(i), DestTy, true, Cache)) {
384           AllSame = false;
385           break;
386         }
387       if (AllSame) {
388         Constant *N = ConstantInt::get(DestTy, NumElems);
389         return ConstantExpr::getNUWMul(MemberSize, N);
390       }
391     }
392 
393   // Pointer size doesn't depend on the pointee type, so canonicalize them
394   // to an arbitrary pointee.
395   if (PointerType *PTy = dyn_cast<PointerType>(Ty))
396     if (!PTy->getElementType()->isIntegerTy(1))
397       return getFoldedSizeOf(
398           PointerType::get(IntegerType::get(PTy->getContext(), 1),
399                            PTy->getAddressSpace()),
400           DestTy, true, Cache);
401 
402   // If there's no interesting folding happening, bail so that we don't create
403   // a constant that looks like it needs folding but really doesn't.
404   if (!Folded)
405     return nullptr;
406 
407   // Base case: Get a regular sizeof expression.
408   Constant *C = ConstantExpr::getSizeOf(Ty);
409   C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
410                                                     DestTy, false),
411                             C, DestTy);
412   return C;
413 }
414 
415 static Constant *getFoldedSizeOf(Type *Ty, Type *DestTy, bool Folded,
416                                  DenseMap<Type *, Constant *> &Cache) {
417   // Check for previously generated folded size constant.
418   auto It = Cache.find(Ty);
419   if (It != Cache.end())
420     return It->second;
421   return Cache[Ty] = getFoldedSizeOfImpl(Ty, DestTy, Folded, Cache);
422 }
423 
424 static Constant *getFoldedSizeOf(Type *Ty, Type *DestTy, bool Folded) {
425   DenseMap<Type *, Constant *> Cache;
426   return getFoldedSizeOf(Ty, DestTy, Folded, Cache);
427 }
428 
429 /// Return a ConstantExpr with type DestTy for alignof on Ty, with any known
430 /// factors factored out. If Folded is false, return null if no factoring was
431 /// possible, to avoid endlessly bouncing an unfoldable expression back into the
432 /// top-level folder.
433 static Constant *getFoldedAlignOf(Type *Ty, Type *DestTy, bool Folded) {
434   // The alignment of an array is equal to the alignment of the
435   // array element. Note that this is not always true for vectors.
436   if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
437     Constant *C = ConstantExpr::getAlignOf(ATy->getElementType());
438     C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
439                                                       DestTy,
440                                                       false),
441                               C, DestTy);
442     return C;
443   }
444 
445   if (StructType *STy = dyn_cast<StructType>(Ty)) {
446     // Packed structs always have an alignment of 1.
447     if (STy->isPacked())
448       return ConstantInt::get(DestTy, 1);
449 
450     // Otherwise, struct alignment is the maximum alignment of any member.
451     // Without target data, we can't compare much, but we can check to see
452     // if all the members have the same alignment.
453     unsigned NumElems = STy->getNumElements();
454     // An empty struct has minimal alignment.
455     if (NumElems == 0)
456       return ConstantInt::get(DestTy, 1);
457     // Check for a struct with all members having the same alignment.
458     Constant *MemberAlign =
459       getFoldedAlignOf(STy->getElementType(0), DestTy, true);
460     bool AllSame = true;
461     for (unsigned i = 1; i != NumElems; ++i)
462       if (MemberAlign != getFoldedAlignOf(STy->getElementType(i), DestTy, true)) {
463         AllSame = false;
464         break;
465       }
466     if (AllSame)
467       return MemberAlign;
468   }
469 
470   // Pointer alignment doesn't depend on the pointee type, so canonicalize them
471   // to an arbitrary pointee.
472   if (PointerType *PTy = dyn_cast<PointerType>(Ty))
473     if (!PTy->getElementType()->isIntegerTy(1))
474       return
475         getFoldedAlignOf(PointerType::get(IntegerType::get(PTy->getContext(),
476                                                            1),
477                                           PTy->getAddressSpace()),
478                          DestTy, true);
479 
480   // If there's no interesting folding happening, bail so that we don't create
481   // a constant that looks like it needs folding but really doesn't.
482   if (!Folded)
483     return nullptr;
484 
485   // Base case: Get a regular alignof expression.
486   Constant *C = ConstantExpr::getAlignOf(Ty);
487   C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
488                                                     DestTy, false),
489                             C, DestTy);
490   return C;
491 }
492 
493 /// Return a ConstantExpr with type DestTy for offsetof on Ty and FieldNo, with
494 /// any known factors factored out. If Folded is false, return null if no
495 /// factoring was possible, to avoid endlessly bouncing an unfoldable expression
496 /// back into the top-level folder.
497 static Constant *getFoldedOffsetOf(Type *Ty, Constant *FieldNo, Type *DestTy,
498                                    bool Folded) {
499   if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
500     Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, false,
501                                                                 DestTy, false),
502                                         FieldNo, DestTy);
503     Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
504     return ConstantExpr::getNUWMul(E, N);
505   }
506 
507   if (StructType *STy = dyn_cast<StructType>(Ty))
508     if (!STy->isPacked()) {
509       unsigned NumElems = STy->getNumElements();
510       // An empty struct has no members.
511       if (NumElems == 0)
512         return nullptr;
513       // Check for a struct with all members having the same size.
514       Constant *MemberSize =
515         getFoldedSizeOf(STy->getElementType(0), DestTy, true);
516       bool AllSame = true;
517       for (unsigned i = 1; i != NumElems; ++i)
518         if (MemberSize !=
519             getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
520           AllSame = false;
521           break;
522         }
523       if (AllSame) {
524         Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo,
525                                                                     false,
526                                                                     DestTy,
527                                                                     false),
528                                             FieldNo, DestTy);
529         return ConstantExpr::getNUWMul(MemberSize, N);
530       }
531     }
532 
533   // If there's no interesting folding happening, bail so that we don't create
534   // a constant that looks like it needs folding but really doesn't.
535   if (!Folded)
536     return nullptr;
537 
538   // Base case: Get a regular offsetof expression.
539   Constant *C = ConstantExpr::getOffsetOf(Ty, FieldNo);
540   C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
541                                                     DestTy, false),
542                             C, DestTy);
543   return C;
544 }
545 
546 Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
547                                             Type *DestTy) {
548   if (isa<PoisonValue>(V))
549     return PoisonValue::get(DestTy);
550 
551   if (isa<UndefValue>(V)) {
552     // zext(undef) = 0, because the top bits will be zero.
553     // sext(undef) = 0, because the top bits will all be the same.
554     // [us]itofp(undef) = 0, because the result value is bounded.
555     if (opc == Instruction::ZExt || opc == Instruction::SExt ||
556         opc == Instruction::UIToFP || opc == Instruction::SIToFP)
557       return Constant::getNullValue(DestTy);
558     return UndefValue::get(DestTy);
559   }
560 
561   if (V->isNullValue() && !DestTy->isX86_MMXTy() && !DestTy->isX86_AMXTy() &&
562       opc != Instruction::AddrSpaceCast)
563     return Constant::getNullValue(DestTy);
564 
565   // If the cast operand is a constant expression, there's a few things we can
566   // do to try to simplify it.
567   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
568     if (CE->isCast()) {
569       // Try hard to fold cast of cast because they are often eliminable.
570       if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
571         return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
572     } else if (CE->getOpcode() == Instruction::GetElementPtr &&
573                // Do not fold addrspacecast (gep 0, .., 0). It might make the
574                // addrspacecast uncanonicalized.
575                opc != Instruction::AddrSpaceCast &&
576                // Do not fold bitcast (gep) with inrange index, as this loses
577                // information.
578                !cast<GEPOperator>(CE)->getInRangeIndex().hasValue() &&
579                // Do not fold if the gep type is a vector, as bitcasting
580                // operand 0 of a vector gep will result in a bitcast between
581                // different sizes.
582                !CE->getType()->isVectorTy()) {
583       // If all of the indexes in the GEP are null values, there is no pointer
584       // adjustment going on.  We might as well cast the source pointer.
585       bool isAllNull = true;
586       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
587         if (!CE->getOperand(i)->isNullValue()) {
588           isAllNull = false;
589           break;
590         }
591       if (isAllNull)
592         // This is casting one pointer type to another, always BitCast
593         return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
594     }
595   }
596 
597   // If the cast operand is a constant vector, perform the cast by
598   // operating on each element. In the cast of bitcasts, the element
599   // count may be mismatched; don't attempt to handle that here.
600   if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
601       DestTy->isVectorTy() &&
602       cast<FixedVectorType>(DestTy)->getNumElements() ==
603           cast<FixedVectorType>(V->getType())->getNumElements()) {
604     VectorType *DestVecTy = cast<VectorType>(DestTy);
605     Type *DstEltTy = DestVecTy->getElementType();
606     // Fast path for splatted constants.
607     if (Constant *Splat = V->getSplatValue()) {
608       return ConstantVector::getSplat(
609           cast<VectorType>(DestTy)->getElementCount(),
610           ConstantExpr::getCast(opc, Splat, DstEltTy));
611     }
612     SmallVector<Constant *, 16> res;
613     Type *Ty = IntegerType::get(V->getContext(), 32);
614     for (unsigned i = 0,
615                   e = cast<FixedVectorType>(V->getType())->getNumElements();
616          i != e; ++i) {
617       Constant *C =
618         ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
619       res.push_back(ConstantExpr::getCast(opc, C, DstEltTy));
620     }
621     return ConstantVector::get(res);
622   }
623 
624   // We actually have to do a cast now. Perform the cast according to the
625   // opcode specified.
626   switch (opc) {
627   default:
628     llvm_unreachable("Failed to cast constant expression");
629   case Instruction::FPTrunc:
630   case Instruction::FPExt:
631     if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
632       bool ignored;
633       APFloat Val = FPC->getValueAPF();
634       Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf() :
635                   DestTy->isFloatTy() ? APFloat::IEEEsingle() :
636                   DestTy->isDoubleTy() ? APFloat::IEEEdouble() :
637                   DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended() :
638                   DestTy->isFP128Ty() ? APFloat::IEEEquad() :
639                   DestTy->isPPC_FP128Ty() ? APFloat::PPCDoubleDouble() :
640                   APFloat::Bogus(),
641                   APFloat::rmNearestTiesToEven, &ignored);
642       return ConstantFP::get(V->getContext(), Val);
643     }
644     return nullptr; // Can't fold.
645   case Instruction::FPToUI:
646   case Instruction::FPToSI:
647     if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
648       const APFloat &V = FPC->getValueAPF();
649       bool ignored;
650       uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
651       APSInt IntVal(DestBitWidth, opc == Instruction::FPToUI);
652       if (APFloat::opInvalidOp ==
653           V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) {
654         // Undefined behavior invoked - the destination type can't represent
655         // the input constant.
656         return PoisonValue::get(DestTy);
657       }
658       return ConstantInt::get(FPC->getContext(), IntVal);
659     }
660     return nullptr; // Can't fold.
661   case Instruction::IntToPtr:   //always treated as unsigned
662     if (V->isNullValue())       // Is it an integral null value?
663       return ConstantPointerNull::get(cast<PointerType>(DestTy));
664     return nullptr;                   // Other pointer types cannot be casted
665   case Instruction::PtrToInt:   // always treated as unsigned
666     // Is it a null pointer value?
667     if (V->isNullValue())
668       return ConstantInt::get(DestTy, 0);
669     // If this is a sizeof-like expression, pull out multiplications by
670     // known factors to expose them to subsequent folding. If it's an
671     // alignof-like expression, factor out known factors.
672     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
673       if (CE->getOpcode() == Instruction::GetElementPtr &&
674           CE->getOperand(0)->isNullValue()) {
675         // FIXME: Looks like getFoldedSizeOf(), getFoldedOffsetOf() and
676         // getFoldedAlignOf() don't handle the case when DestTy is a vector of
677         // pointers yet. We end up in asserts in CastInst::getCastOpcode (see
678         // test/Analysis/ConstantFolding/cast-vector.ll). I've only seen this
679         // happen in one "real" C-code test case, so it does not seem to be an
680         // important optimization to handle vectors here. For now, simply bail
681         // out.
682         if (DestTy->isVectorTy())
683           return nullptr;
684         GEPOperator *GEPO = cast<GEPOperator>(CE);
685         Type *Ty = GEPO->getSourceElementType();
686         if (CE->getNumOperands() == 2) {
687           // Handle a sizeof-like expression.
688           Constant *Idx = CE->getOperand(1);
689           bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne();
690           if (Constant *C = getFoldedSizeOf(Ty, DestTy, !isOne)) {
691             Idx = ConstantExpr::getCast(CastInst::getCastOpcode(Idx, true,
692                                                                 DestTy, false),
693                                         Idx, DestTy);
694             return ConstantExpr::getMul(C, Idx);
695           }
696         } else if (CE->getNumOperands() == 3 &&
697                    CE->getOperand(1)->isNullValue()) {
698           // Handle an alignof-like expression.
699           if (StructType *STy = dyn_cast<StructType>(Ty))
700             if (!STy->isPacked()) {
701               ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2));
702               if (CI->isOne() &&
703                   STy->getNumElements() == 2 &&
704                   STy->getElementType(0)->isIntegerTy(1)) {
705                 return getFoldedAlignOf(STy->getElementType(1), DestTy, false);
706               }
707             }
708           // Handle an offsetof-like expression.
709           if (Ty->isStructTy() || Ty->isArrayTy()) {
710             if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2),
711                                                 DestTy, false))
712               return C;
713           }
714         }
715       }
716     // Other pointer types cannot be casted
717     return nullptr;
718   case Instruction::UIToFP:
719   case Instruction::SIToFP:
720     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
721       const APInt &api = CI->getValue();
722       APFloat apf(DestTy->getFltSemantics(),
723                   APInt::getNullValue(DestTy->getPrimitiveSizeInBits()));
724       apf.convertFromAPInt(api, opc==Instruction::SIToFP,
725                            APFloat::rmNearestTiesToEven);
726       return ConstantFP::get(V->getContext(), apf);
727     }
728     return nullptr;
729   case Instruction::ZExt:
730     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
731       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
732       return ConstantInt::get(V->getContext(),
733                               CI->getValue().zext(BitWidth));
734     }
735     return nullptr;
736   case Instruction::SExt:
737     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
738       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
739       return ConstantInt::get(V->getContext(),
740                               CI->getValue().sext(BitWidth));
741     }
742     return nullptr;
743   case Instruction::Trunc: {
744     if (V->getType()->isVectorTy())
745       return nullptr;
746 
747     uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
748     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
749       return ConstantInt::get(V->getContext(),
750                               CI->getValue().trunc(DestBitWidth));
751     }
752 
753     // The input must be a constantexpr.  See if we can simplify this based on
754     // the bytes we are demanding.  Only do this if the source and dest are an
755     // even multiple of a byte.
756     if ((DestBitWidth & 7) == 0 &&
757         (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
758       if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
759         return Res;
760 
761     return nullptr;
762   }
763   case Instruction::BitCast:
764     return FoldBitCast(V, DestTy);
765   case Instruction::AddrSpaceCast:
766     return nullptr;
767   }
768 }
769 
770 Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
771                                               Constant *V1, Constant *V2) {
772   // Check for i1 and vector true/false conditions.
773   if (Cond->isNullValue()) return V2;
774   if (Cond->isAllOnesValue()) return V1;
775 
776   // If the condition is a vector constant, fold the result elementwise.
777   if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
778     auto *V1VTy = CondV->getType();
779     SmallVector<Constant*, 16> Result;
780     Type *Ty = IntegerType::get(CondV->getContext(), 32);
781     for (unsigned i = 0, e = V1VTy->getNumElements(); i != e; ++i) {
782       Constant *V;
783       Constant *V1Element = ConstantExpr::getExtractElement(V1,
784                                                     ConstantInt::get(Ty, i));
785       Constant *V2Element = ConstantExpr::getExtractElement(V2,
786                                                     ConstantInt::get(Ty, i));
787       auto *Cond = cast<Constant>(CondV->getOperand(i));
788       if (isa<PoisonValue>(Cond)) {
789         V = PoisonValue::get(V1Element->getType());
790       } else if (V1Element == V2Element) {
791         V = V1Element;
792       } else if (isa<UndefValue>(Cond)) {
793         V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
794       } else {
795         if (!isa<ConstantInt>(Cond)) break;
796         V = Cond->isNullValue() ? V2Element : V1Element;
797       }
798       Result.push_back(V);
799     }
800 
801     // If we were able to build the vector, return it.
802     if (Result.size() == V1VTy->getNumElements())
803       return ConstantVector::get(Result);
804   }
805 
806   if (isa<PoisonValue>(Cond))
807     return PoisonValue::get(V1->getType());
808 
809   if (isa<UndefValue>(Cond)) {
810     if (isa<UndefValue>(V1)) return V1;
811     return V2;
812   }
813 
814   if (V1 == V2) return V1;
815 
816   if (isa<PoisonValue>(V1))
817     return V2;
818   if (isa<PoisonValue>(V2))
819     return V1;
820 
821   // If the true or false value is undef, we can fold to the other value as
822   // long as the other value isn't poison.
823   auto NotPoison = [](Constant *C) {
824     if (isa<PoisonValue>(C))
825       return false;
826 
827     // TODO: We can analyze ConstExpr by opcode to determine if there is any
828     //       possibility of poison.
829     if (isa<ConstantExpr>(C))
830       return false;
831 
832     if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(C) ||
833         isa<ConstantPointerNull>(C) || isa<Function>(C))
834       return true;
835 
836     if (C->getType()->isVectorTy())
837       return !C->containsPoisonElement() && !C->containsConstantExpression();
838 
839     // TODO: Recursively analyze aggregates or other constants.
840     return false;
841   };
842   if (isa<UndefValue>(V1) && NotPoison(V2)) return V2;
843   if (isa<UndefValue>(V2) && NotPoison(V1)) return V1;
844 
845   if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) {
846     if (TrueVal->getOpcode() == Instruction::Select)
847       if (TrueVal->getOperand(0) == Cond)
848         return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2);
849   }
850   if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) {
851     if (FalseVal->getOpcode() == Instruction::Select)
852       if (FalseVal->getOperand(0) == Cond)
853         return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2));
854   }
855 
856   return nullptr;
857 }
858 
859 Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
860                                                       Constant *Idx) {
861   auto *ValVTy = cast<VectorType>(Val->getType());
862 
863   // extractelt poison, C -> poison
864   // extractelt C, undef -> poison
865   if (isa<PoisonValue>(Val) || isa<UndefValue>(Idx))
866     return PoisonValue::get(ValVTy->getElementType());
867 
868   // extractelt undef, C -> undef
869   if (isa<UndefValue>(Val))
870     return UndefValue::get(ValVTy->getElementType());
871 
872   auto *CIdx = dyn_cast<ConstantInt>(Idx);
873   if (!CIdx)
874     return nullptr;
875 
876   if (auto *ValFVTy = dyn_cast<FixedVectorType>(Val->getType())) {
877     // ee({w,x,y,z}, wrong_value) -> poison
878     if (CIdx->uge(ValFVTy->getNumElements()))
879       return PoisonValue::get(ValFVTy->getElementType());
880   }
881 
882   // ee (gep (ptr, idx0, ...), idx) -> gep (ee (ptr, idx), ee (idx0, idx), ...)
883   if (auto *CE = dyn_cast<ConstantExpr>(Val)) {
884     if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
885       SmallVector<Constant *, 8> Ops;
886       Ops.reserve(CE->getNumOperands());
887       for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
888         Constant *Op = CE->getOperand(i);
889         if (Op->getType()->isVectorTy()) {
890           Constant *ScalarOp = ConstantExpr::getExtractElement(Op, Idx);
891           if (!ScalarOp)
892             return nullptr;
893           Ops.push_back(ScalarOp);
894         } else
895           Ops.push_back(Op);
896       }
897       return CE->getWithOperands(Ops, ValVTy->getElementType(), false,
898                                  GEP->getSourceElementType());
899     } else if (CE->getOpcode() == Instruction::InsertElement) {
900       if (const auto *IEIdx = dyn_cast<ConstantInt>(CE->getOperand(2))) {
901         if (APSInt::isSameValue(APSInt(IEIdx->getValue()),
902                                 APSInt(CIdx->getValue()))) {
903           return CE->getOperand(1);
904         } else {
905           return ConstantExpr::getExtractElement(CE->getOperand(0), CIdx);
906         }
907       }
908     }
909   }
910 
911   // Lane < Splat minimum vector width => extractelt Splat(x), Lane -> x
912   if (CIdx->getValue().ult(ValVTy->getElementCount().getKnownMinValue())) {
913     if (Constant *SplatVal = Val->getSplatValue())
914       return SplatVal;
915   }
916 
917   return Val->getAggregateElement(CIdx);
918 }
919 
920 Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val,
921                                                      Constant *Elt,
922                                                      Constant *Idx) {
923   if (isa<UndefValue>(Idx))
924     return PoisonValue::get(Val->getType());
925 
926   ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
927   if (!CIdx) return nullptr;
928 
929   // Do not iterate on scalable vector. The num of elements is unknown at
930   // compile-time.
931   if (isa<ScalableVectorType>(Val->getType()))
932     return nullptr;
933 
934   auto *ValTy = cast<FixedVectorType>(Val->getType());
935 
936   unsigned NumElts = ValTy->getNumElements();
937   if (CIdx->uge(NumElts))
938     return PoisonValue::get(Val->getType());
939 
940   SmallVector<Constant*, 16> Result;
941   Result.reserve(NumElts);
942   auto *Ty = Type::getInt32Ty(Val->getContext());
943   uint64_t IdxVal = CIdx->getZExtValue();
944   for (unsigned i = 0; i != NumElts; ++i) {
945     if (i == IdxVal) {
946       Result.push_back(Elt);
947       continue;
948     }
949 
950     Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
951     Result.push_back(C);
952   }
953 
954   return ConstantVector::get(Result);
955 }
956 
957 Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
958                                                      ArrayRef<int> Mask) {
959   auto *V1VTy = cast<VectorType>(V1->getType());
960   unsigned MaskNumElts = Mask.size();
961   auto MaskEltCount =
962       ElementCount::get(MaskNumElts, isa<ScalableVectorType>(V1VTy));
963   Type *EltTy = V1VTy->getElementType();
964 
965   // Undefined shuffle mask -> undefined value.
966   if (all_of(Mask, [](int Elt) { return Elt == UndefMaskElem; })) {
967     return UndefValue::get(FixedVectorType::get(EltTy, MaskNumElts));
968   }
969 
970   // If the mask is all zeros this is a splat, no need to go through all
971   // elements.
972   if (all_of(Mask, [](int Elt) { return Elt == 0; }) &&
973       !MaskEltCount.isScalable()) {
974     Type *Ty = IntegerType::get(V1->getContext(), 32);
975     Constant *Elt =
976         ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, 0));
977     return ConstantVector::getSplat(MaskEltCount, Elt);
978   }
979   // Do not iterate on scalable vector. The num of elements is unknown at
980   // compile-time.
981   if (isa<ScalableVectorType>(V1VTy))
982     return nullptr;
983 
984   unsigned SrcNumElts = V1VTy->getElementCount().getKnownMinValue();
985 
986   // Loop over the shuffle mask, evaluating each element.
987   SmallVector<Constant*, 32> Result;
988   for (unsigned i = 0; i != MaskNumElts; ++i) {
989     int Elt = Mask[i];
990     if (Elt == -1) {
991       Result.push_back(UndefValue::get(EltTy));
992       continue;
993     }
994     Constant *InElt;
995     if (unsigned(Elt) >= SrcNumElts*2)
996       InElt = UndefValue::get(EltTy);
997     else if (unsigned(Elt) >= SrcNumElts) {
998       Type *Ty = IntegerType::get(V2->getContext(), 32);
999       InElt =
1000         ConstantExpr::getExtractElement(V2,
1001                                         ConstantInt::get(Ty, Elt - SrcNumElts));
1002     } else {
1003       Type *Ty = IntegerType::get(V1->getContext(), 32);
1004       InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt));
1005     }
1006     Result.push_back(InElt);
1007   }
1008 
1009   return ConstantVector::get(Result);
1010 }
1011 
1012 Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
1013                                                     ArrayRef<unsigned> Idxs) {
1014   // Base case: no indices, so return the entire value.
1015   if (Idxs.empty())
1016     return Agg;
1017 
1018   if (Constant *C = Agg->getAggregateElement(Idxs[0]))
1019     return ConstantFoldExtractValueInstruction(C, Idxs.slice(1));
1020 
1021   return nullptr;
1022 }
1023 
1024 Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
1025                                                    Constant *Val,
1026                                                    ArrayRef<unsigned> Idxs) {
1027   // Base case: no indices, so replace the entire value.
1028   if (Idxs.empty())
1029     return Val;
1030 
1031   unsigned NumElts;
1032   if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
1033     NumElts = ST->getNumElements();
1034   else
1035     NumElts = cast<ArrayType>(Agg->getType())->getNumElements();
1036 
1037   SmallVector<Constant*, 32> Result;
1038   for (unsigned i = 0; i != NumElts; ++i) {
1039     Constant *C = Agg->getAggregateElement(i);
1040     if (!C) return nullptr;
1041 
1042     if (Idxs[0] == i)
1043       C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1));
1044 
1045     Result.push_back(C);
1046   }
1047 
1048   if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
1049     return ConstantStruct::get(ST, Result);
1050   return ConstantArray::get(cast<ArrayType>(Agg->getType()), Result);
1051 }
1052 
1053 Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) {
1054   assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected");
1055 
1056   // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length
1057   // vectors are always evaluated per element.
1058   bool IsScalableVector = isa<ScalableVectorType>(C->getType());
1059   bool HasScalarUndefOrScalableVectorUndef =
1060       (!C->getType()->isVectorTy() || IsScalableVector) && isa<UndefValue>(C);
1061 
1062   if (HasScalarUndefOrScalableVectorUndef) {
1063     switch (static_cast<Instruction::UnaryOps>(Opcode)) {
1064     case Instruction::FNeg:
1065       return C; // -undef -> undef
1066     case Instruction::UnaryOpsEnd:
1067       llvm_unreachable("Invalid UnaryOp");
1068     }
1069   }
1070 
1071   // Constant should not be UndefValue, unless these are vector constants.
1072   assert(!HasScalarUndefOrScalableVectorUndef && "Unexpected UndefValue");
1073   // We only have FP UnaryOps right now.
1074   assert(!isa<ConstantInt>(C) && "Unexpected Integer UnaryOp");
1075 
1076   if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
1077     const APFloat &CV = CFP->getValueAPF();
1078     switch (Opcode) {
1079     default:
1080       break;
1081     case Instruction::FNeg:
1082       return ConstantFP::get(C->getContext(), neg(CV));
1083     }
1084   } else if (auto *VTy = dyn_cast<FixedVectorType>(C->getType())) {
1085 
1086     Type *Ty = IntegerType::get(VTy->getContext(), 32);
1087     // Fast path for splatted constants.
1088     if (Constant *Splat = C->getSplatValue()) {
1089       Constant *Elt = ConstantExpr::get(Opcode, Splat);
1090       return ConstantVector::getSplat(VTy->getElementCount(), Elt);
1091     }
1092 
1093     // Fold each element and create a vector constant from those constants.
1094     SmallVector<Constant *, 16> Result;
1095     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1096       Constant *ExtractIdx = ConstantInt::get(Ty, i);
1097       Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx);
1098 
1099       Result.push_back(ConstantExpr::get(Opcode, Elt));
1100     }
1101 
1102     return ConstantVector::get(Result);
1103   }
1104 
1105   // We don't know how to fold this.
1106   return nullptr;
1107 }
1108 
1109 Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
1110                                               Constant *C2) {
1111   assert(Instruction::isBinaryOp(Opcode) && "Non-binary instruction detected");
1112 
1113   // Simplify BinOps with their identity values first. They are no-ops and we
1114   // can always return the other value, including undef or poison values.
1115   // FIXME: remove unnecessary duplicated identity patterns below.
1116   // FIXME: Use AllowRHSConstant with getBinOpIdentity to handle additional ops,
1117   //        like X << 0 = X.
1118   Constant *Identity = ConstantExpr::getBinOpIdentity(Opcode, C1->getType());
1119   if (Identity) {
1120     if (C1 == Identity)
1121       return C2;
1122     if (C2 == Identity)
1123       return C1;
1124   }
1125 
1126   // Binary operations propagate poison.
1127   if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1128     return PoisonValue::get(C1->getType());
1129 
1130   // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length
1131   // vectors are always evaluated per element.
1132   bool IsScalableVector = isa<ScalableVectorType>(C1->getType());
1133   bool HasScalarUndefOrScalableVectorUndef =
1134       (!C1->getType()->isVectorTy() || IsScalableVector) &&
1135       (isa<UndefValue>(C1) || isa<UndefValue>(C2));
1136   if (HasScalarUndefOrScalableVectorUndef) {
1137     switch (static_cast<Instruction::BinaryOps>(Opcode)) {
1138     case Instruction::Xor:
1139       if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1140         // Handle undef ^ undef -> 0 special case. This is a common
1141         // idiom (misuse).
1142         return Constant::getNullValue(C1->getType());
1143       LLVM_FALLTHROUGH;
1144     case Instruction::Add:
1145     case Instruction::Sub:
1146       return UndefValue::get(C1->getType());
1147     case Instruction::And:
1148       if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
1149         return C1;
1150       return Constant::getNullValue(C1->getType());   // undef & X -> 0
1151     case Instruction::Mul: {
1152       // undef * undef -> undef
1153       if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1154         return C1;
1155       const APInt *CV;
1156       // X * undef -> undef   if X is odd
1157       if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV)))
1158         if ((*CV)[0])
1159           return UndefValue::get(C1->getType());
1160 
1161       // X * undef -> 0       otherwise
1162       return Constant::getNullValue(C1->getType());
1163     }
1164     case Instruction::SDiv:
1165     case Instruction::UDiv:
1166       // X / undef -> poison
1167       // X / 0 -> poison
1168       if (match(C2, m_CombineOr(m_Undef(), m_Zero())))
1169         return PoisonValue::get(C2->getType());
1170       // undef / 1 -> undef
1171       if (match(C2, m_One()))
1172         return C1;
1173       // undef / X -> 0       otherwise
1174       return Constant::getNullValue(C1->getType());
1175     case Instruction::URem:
1176     case Instruction::SRem:
1177       // X % undef -> poison
1178       // X % 0 -> poison
1179       if (match(C2, m_CombineOr(m_Undef(), m_Zero())))
1180         return PoisonValue::get(C2->getType());
1181       // undef % X -> 0       otherwise
1182       return Constant::getNullValue(C1->getType());
1183     case Instruction::Or:                          // X | undef -> -1
1184       if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
1185         return C1;
1186       return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
1187     case Instruction::LShr:
1188       // X >>l undef -> poison
1189       if (isa<UndefValue>(C2))
1190         return PoisonValue::get(C2->getType());
1191       // undef >>l 0 -> undef
1192       if (match(C2, m_Zero()))
1193         return C1;
1194       // undef >>l X -> 0
1195       return Constant::getNullValue(C1->getType());
1196     case Instruction::AShr:
1197       // X >>a undef -> poison
1198       if (isa<UndefValue>(C2))
1199         return PoisonValue::get(C2->getType());
1200       // undef >>a 0 -> undef
1201       if (match(C2, m_Zero()))
1202         return C1;
1203       // TODO: undef >>a X -> poison if the shift is exact
1204       // undef >>a X -> 0
1205       return Constant::getNullValue(C1->getType());
1206     case Instruction::Shl:
1207       // X << undef -> undef
1208       if (isa<UndefValue>(C2))
1209         return PoisonValue::get(C2->getType());
1210       // undef << 0 -> undef
1211       if (match(C2, m_Zero()))
1212         return C1;
1213       // undef << X -> 0
1214       return Constant::getNullValue(C1->getType());
1215     case Instruction::FSub:
1216       // -0.0 - undef --> undef (consistent with "fneg undef")
1217       if (match(C1, m_NegZeroFP()) && isa<UndefValue>(C2))
1218         return C2;
1219       LLVM_FALLTHROUGH;
1220     case Instruction::FAdd:
1221     case Instruction::FMul:
1222     case Instruction::FDiv:
1223     case Instruction::FRem:
1224       // [any flop] undef, undef -> undef
1225       if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1226         return C1;
1227       // [any flop] C, undef -> NaN
1228       // [any flop] undef, C -> NaN
1229       // We could potentially specialize NaN/Inf constants vs. 'normal'
1230       // constants (possibly differently depending on opcode and operand). This
1231       // would allow returning undef sometimes. But it is always safe to fold to
1232       // NaN because we can choose the undef operand as NaN, and any FP opcode
1233       // with a NaN operand will propagate NaN.
1234       return ConstantFP::getNaN(C1->getType());
1235     case Instruction::BinaryOpsEnd:
1236       llvm_unreachable("Invalid BinaryOp");
1237     }
1238   }
1239 
1240   // Neither constant should be UndefValue, unless these are vector constants.
1241   assert((!HasScalarUndefOrScalableVectorUndef) && "Unexpected UndefValue");
1242 
1243   // Handle simplifications when the RHS is a constant int.
1244   if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
1245     switch (Opcode) {
1246     case Instruction::Add:
1247       if (CI2->isZero()) return C1;                             // X + 0 == X
1248       break;
1249     case Instruction::Sub:
1250       if (CI2->isZero()) return C1;                             // X - 0 == X
1251       break;
1252     case Instruction::Mul:
1253       if (CI2->isZero()) return C2;                             // X * 0 == 0
1254       if (CI2->isOne())
1255         return C1;                                              // X * 1 == X
1256       break;
1257     case Instruction::UDiv:
1258     case Instruction::SDiv:
1259       if (CI2->isOne())
1260         return C1;                                            // X / 1 == X
1261       if (CI2->isZero())
1262         return PoisonValue::get(CI2->getType());              // X / 0 == poison
1263       break;
1264     case Instruction::URem:
1265     case Instruction::SRem:
1266       if (CI2->isOne())
1267         return Constant::getNullValue(CI2->getType());        // X % 1 == 0
1268       if (CI2->isZero())
1269         return PoisonValue::get(CI2->getType());              // X % 0 == poison
1270       break;
1271     case Instruction::And:
1272       if (CI2->isZero()) return C2;                           // X & 0 == 0
1273       if (CI2->isMinusOne())
1274         return C1;                                            // X & -1 == X
1275 
1276       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1277         // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
1278         if (CE1->getOpcode() == Instruction::ZExt) {
1279           unsigned DstWidth = CI2->getType()->getBitWidth();
1280           unsigned SrcWidth =
1281             CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
1282           APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
1283           if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
1284             return C1;
1285         }
1286 
1287         // If and'ing the address of a global with a constant, fold it.
1288         if (CE1->getOpcode() == Instruction::PtrToInt &&
1289             isa<GlobalValue>(CE1->getOperand(0))) {
1290           GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
1291 
1292           MaybeAlign GVAlign;
1293 
1294           if (Module *TheModule = GV->getParent()) {
1295             const DataLayout &DL = TheModule->getDataLayout();
1296             GVAlign = GV->getPointerAlignment(DL);
1297 
1298             // If the function alignment is not specified then assume that it
1299             // is 4.
1300             // This is dangerous; on x86, the alignment of the pointer
1301             // corresponds to the alignment of the function, but might be less
1302             // than 4 if it isn't explicitly specified.
1303             // However, a fix for this behaviour was reverted because it
1304             // increased code size (see https://reviews.llvm.org/D55115)
1305             // FIXME: This code should be deleted once existing targets have
1306             // appropriate defaults
1307             if (isa<Function>(GV) && !DL.getFunctionPtrAlign())
1308               GVAlign = Align(4);
1309           } else if (isa<Function>(GV)) {
1310             // Without a datalayout we have to assume the worst case: that the
1311             // function pointer isn't aligned at all.
1312             GVAlign = llvm::None;
1313           } else if (isa<GlobalVariable>(GV)) {
1314             GVAlign = cast<GlobalVariable>(GV)->getAlign();
1315           }
1316 
1317           if (GVAlign && *GVAlign > 1) {
1318             unsigned DstWidth = CI2->getType()->getBitWidth();
1319             unsigned SrcWidth = std::min(DstWidth, Log2(*GVAlign));
1320             APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
1321 
1322             // If checking bits we know are clear, return zero.
1323             if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
1324               return Constant::getNullValue(CI2->getType());
1325           }
1326         }
1327       }
1328       break;
1329     case Instruction::Or:
1330       if (CI2->isZero()) return C1;        // X | 0 == X
1331       if (CI2->isMinusOne())
1332         return C2;                         // X | -1 == -1
1333       break;
1334     case Instruction::Xor:
1335       if (CI2->isZero()) return C1;        // X ^ 0 == X
1336 
1337       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1338         switch (CE1->getOpcode()) {
1339         default: break;
1340         case Instruction::ICmp:
1341         case Instruction::FCmp:
1342           // cmp pred ^ true -> cmp !pred
1343           assert(CI2->isOne());
1344           CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
1345           pred = CmpInst::getInversePredicate(pred);
1346           return ConstantExpr::getCompare(pred, CE1->getOperand(0),
1347                                           CE1->getOperand(1));
1348         }
1349       }
1350       break;
1351     case Instruction::AShr:
1352       // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
1353       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
1354         if (CE1->getOpcode() == Instruction::ZExt)  // Top bits known zero.
1355           return ConstantExpr::getLShr(C1, C2);
1356       break;
1357     }
1358   } else if (isa<ConstantInt>(C1)) {
1359     // If C1 is a ConstantInt and C2 is not, swap the operands.
1360     if (Instruction::isCommutative(Opcode))
1361       return ConstantExpr::get(Opcode, C2, C1);
1362   }
1363 
1364   if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
1365     if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
1366       const APInt &C1V = CI1->getValue();
1367       const APInt &C2V = CI2->getValue();
1368       switch (Opcode) {
1369       default:
1370         break;
1371       case Instruction::Add:
1372         return ConstantInt::get(CI1->getContext(), C1V + C2V);
1373       case Instruction::Sub:
1374         return ConstantInt::get(CI1->getContext(), C1V - C2V);
1375       case Instruction::Mul:
1376         return ConstantInt::get(CI1->getContext(), C1V * C2V);
1377       case Instruction::UDiv:
1378         assert(!CI2->isZero() && "Div by zero handled above");
1379         return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V));
1380       case Instruction::SDiv:
1381         assert(!CI2->isZero() && "Div by zero handled above");
1382         if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
1383           return PoisonValue::get(CI1->getType());   // MIN_INT / -1 -> poison
1384         return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V));
1385       case Instruction::URem:
1386         assert(!CI2->isZero() && "Div by zero handled above");
1387         return ConstantInt::get(CI1->getContext(), C1V.urem(C2V));
1388       case Instruction::SRem:
1389         assert(!CI2->isZero() && "Div by zero handled above");
1390         if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
1391           return PoisonValue::get(CI1->getType());   // MIN_INT % -1 -> poison
1392         return ConstantInt::get(CI1->getContext(), C1V.srem(C2V));
1393       case Instruction::And:
1394         return ConstantInt::get(CI1->getContext(), C1V & C2V);
1395       case Instruction::Or:
1396         return ConstantInt::get(CI1->getContext(), C1V | C2V);
1397       case Instruction::Xor:
1398         return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
1399       case Instruction::Shl:
1400         if (C2V.ult(C1V.getBitWidth()))
1401           return ConstantInt::get(CI1->getContext(), C1V.shl(C2V));
1402         return PoisonValue::get(C1->getType()); // too big shift is poison
1403       case Instruction::LShr:
1404         if (C2V.ult(C1V.getBitWidth()))
1405           return ConstantInt::get(CI1->getContext(), C1V.lshr(C2V));
1406         return PoisonValue::get(C1->getType()); // too big shift is poison
1407       case Instruction::AShr:
1408         if (C2V.ult(C1V.getBitWidth()))
1409           return ConstantInt::get(CI1->getContext(), C1V.ashr(C2V));
1410         return PoisonValue::get(C1->getType()); // too big shift is poison
1411       }
1412     }
1413 
1414     switch (Opcode) {
1415     case Instruction::SDiv:
1416     case Instruction::UDiv:
1417     case Instruction::URem:
1418     case Instruction::SRem:
1419     case Instruction::LShr:
1420     case Instruction::AShr:
1421     case Instruction::Shl:
1422       if (CI1->isZero()) return C1;
1423       break;
1424     default:
1425       break;
1426     }
1427   } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
1428     if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
1429       const APFloat &C1V = CFP1->getValueAPF();
1430       const APFloat &C2V = CFP2->getValueAPF();
1431       APFloat C3V = C1V;  // copy for modification
1432       switch (Opcode) {
1433       default:
1434         break;
1435       case Instruction::FAdd:
1436         (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
1437         return ConstantFP::get(C1->getContext(), C3V);
1438       case Instruction::FSub:
1439         (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
1440         return ConstantFP::get(C1->getContext(), C3V);
1441       case Instruction::FMul:
1442         (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
1443         return ConstantFP::get(C1->getContext(), C3V);
1444       case Instruction::FDiv:
1445         (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
1446         return ConstantFP::get(C1->getContext(), C3V);
1447       case Instruction::FRem:
1448         (void)C3V.mod(C2V);
1449         return ConstantFP::get(C1->getContext(), C3V);
1450       }
1451     }
1452   } else if (auto *VTy = dyn_cast<VectorType>(C1->getType())) {
1453     // Fast path for splatted constants.
1454     if (Constant *C2Splat = C2->getSplatValue()) {
1455       if (Instruction::isIntDivRem(Opcode) && C2Splat->isNullValue())
1456         return PoisonValue::get(VTy);
1457       if (Constant *C1Splat = C1->getSplatValue()) {
1458         return ConstantVector::getSplat(
1459             VTy->getElementCount(),
1460             ConstantExpr::get(Opcode, C1Splat, C2Splat));
1461       }
1462     }
1463 
1464     if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
1465       // Fold each element and create a vector constant from those constants.
1466       SmallVector<Constant*, 16> Result;
1467       Type *Ty = IntegerType::get(FVTy->getContext(), 32);
1468       for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
1469         Constant *ExtractIdx = ConstantInt::get(Ty, i);
1470         Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx);
1471         Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx);
1472 
1473         // If any element of a divisor vector is zero, the whole op is poison.
1474         if (Instruction::isIntDivRem(Opcode) && RHS->isNullValue())
1475           return PoisonValue::get(VTy);
1476 
1477         Result.push_back(ConstantExpr::get(Opcode, LHS, RHS));
1478       }
1479 
1480       return ConstantVector::get(Result);
1481     }
1482   }
1483 
1484   if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1485     // There are many possible foldings we could do here.  We should probably
1486     // at least fold add of a pointer with an integer into the appropriate
1487     // getelementptr.  This will improve alias analysis a bit.
1488 
1489     // Given ((a + b) + c), if (b + c) folds to something interesting, return
1490     // (a + (b + c)).
1491     if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
1492       Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
1493       if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
1494         return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
1495     }
1496   } else if (isa<ConstantExpr>(C2)) {
1497     // If C2 is a constant expr and C1 isn't, flop them around and fold the
1498     // other way if possible.
1499     if (Instruction::isCommutative(Opcode))
1500       return ConstantFoldBinaryInstruction(Opcode, C2, C1);
1501   }
1502 
1503   // i1 can be simplified in many cases.
1504   if (C1->getType()->isIntegerTy(1)) {
1505     switch (Opcode) {
1506     case Instruction::Add:
1507     case Instruction::Sub:
1508       return ConstantExpr::getXor(C1, C2);
1509     case Instruction::Mul:
1510       return ConstantExpr::getAnd(C1, C2);
1511     case Instruction::Shl:
1512     case Instruction::LShr:
1513     case Instruction::AShr:
1514       // We can assume that C2 == 0.  If it were one the result would be
1515       // undefined because the shift value is as large as the bitwidth.
1516       return C1;
1517     case Instruction::SDiv:
1518     case Instruction::UDiv:
1519       // We can assume that C2 == 1.  If it were zero the result would be
1520       // undefined through division by zero.
1521       return C1;
1522     case Instruction::URem:
1523     case Instruction::SRem:
1524       // We can assume that C2 == 1.  If it were zero the result would be
1525       // undefined through division by zero.
1526       return ConstantInt::getFalse(C1->getContext());
1527     default:
1528       break;
1529     }
1530   }
1531 
1532   // We don't know how to fold this.
1533   return nullptr;
1534 }
1535 
1536 /// This type is zero-sized if it's an array or structure of zero-sized types.
1537 /// The only leaf zero-sized type is an empty structure.
1538 static bool isMaybeZeroSizedType(Type *Ty) {
1539   if (StructType *STy = dyn_cast<StructType>(Ty)) {
1540     if (STy->isOpaque()) return true;  // Can't say.
1541 
1542     // If all of elements have zero size, this does too.
1543     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1544       if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
1545     return true;
1546 
1547   } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1548     return isMaybeZeroSizedType(ATy->getElementType());
1549   }
1550   return false;
1551 }
1552 
1553 /// Compare the two constants as though they were getelementptr indices.
1554 /// This allows coercion of the types to be the same thing.
1555 ///
1556 /// If the two constants are the "same" (after coercion), return 0.  If the
1557 /// first is less than the second, return -1, if the second is less than the
1558 /// first, return 1.  If the constants are not integral, return -2.
1559 ///
1560 static int IdxCompare(Constant *C1, Constant *C2, Type *ElTy) {
1561   if (C1 == C2) return 0;
1562 
1563   // Ok, we found a different index.  If they are not ConstantInt, we can't do
1564   // anything with them.
1565   if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1566     return -2; // don't know!
1567 
1568   // We cannot compare the indices if they don't fit in an int64_t.
1569   if (cast<ConstantInt>(C1)->getValue().getActiveBits() > 64 ||
1570       cast<ConstantInt>(C2)->getValue().getActiveBits() > 64)
1571     return -2; // don't know!
1572 
1573   // Ok, we have two differing integer indices.  Sign extend them to be the same
1574   // type.
1575   int64_t C1Val = cast<ConstantInt>(C1)->getSExtValue();
1576   int64_t C2Val = cast<ConstantInt>(C2)->getSExtValue();
1577 
1578   if (C1Val == C2Val) return 0;  // They are equal
1579 
1580   // If the type being indexed over is really just a zero sized type, there is
1581   // no pointer difference being made here.
1582   if (isMaybeZeroSizedType(ElTy))
1583     return -2; // dunno.
1584 
1585   // If they are really different, now that they are the same type, then we
1586   // found a difference!
1587   if (C1Val < C2Val)
1588     return -1;
1589   else
1590     return 1;
1591 }
1592 
1593 /// This function determines if there is anything we can decide about the two
1594 /// constants provided. This doesn't need to handle simple things like
1595 /// ConstantFP comparisons, but should instead handle ConstantExprs.
1596 /// If we can determine that the two constants have a particular relation to
1597 /// each other, we should return the corresponding FCmpInst predicate,
1598 /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
1599 /// ConstantFoldCompareInstruction.
1600 ///
1601 /// To simplify this code we canonicalize the relation so that the first
1602 /// operand is always the most "complex" of the two.  We consider ConstantFP
1603 /// to be the simplest, and ConstantExprs to be the most complex.
1604 static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
1605   assert(V1->getType() == V2->getType() &&
1606          "Cannot compare values of different types!");
1607 
1608   // We do not know if a constant expression will evaluate to a number or NaN.
1609   // Therefore, we can only say that the relation is unordered or equal.
1610   if (V1 == V2) return FCmpInst::FCMP_UEQ;
1611 
1612   if (!isa<ConstantExpr>(V1)) {
1613     if (!isa<ConstantExpr>(V2)) {
1614       // Simple case, use the standard constant folder.
1615       ConstantInt *R = nullptr;
1616       R = dyn_cast<ConstantInt>(
1617                       ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
1618       if (R && !R->isZero())
1619         return FCmpInst::FCMP_OEQ;
1620       R = dyn_cast<ConstantInt>(
1621                       ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
1622       if (R && !R->isZero())
1623         return FCmpInst::FCMP_OLT;
1624       R = dyn_cast<ConstantInt>(
1625                       ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
1626       if (R && !R->isZero())
1627         return FCmpInst::FCMP_OGT;
1628 
1629       // Nothing more we can do
1630       return FCmpInst::BAD_FCMP_PREDICATE;
1631     }
1632 
1633     // If the first operand is simple and second is ConstantExpr, swap operands.
1634     FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
1635     if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
1636       return FCmpInst::getSwappedPredicate(SwappedRelation);
1637   } else {
1638     // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
1639     // constantexpr or a simple constant.
1640     ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1641     switch (CE1->getOpcode()) {
1642     case Instruction::FPTrunc:
1643     case Instruction::FPExt:
1644     case Instruction::UIToFP:
1645     case Instruction::SIToFP:
1646       // We might be able to do something with these but we don't right now.
1647       break;
1648     default:
1649       break;
1650     }
1651   }
1652   // There are MANY other foldings that we could perform here.  They will
1653   // probably be added on demand, as they seem needed.
1654   return FCmpInst::BAD_FCMP_PREDICATE;
1655 }
1656 
1657 static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
1658                                                       const GlobalValue *GV2) {
1659   auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
1660     if (GV->isInterposable() || GV->hasGlobalUnnamedAddr())
1661       return true;
1662     if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
1663       Type *Ty = GVar->getValueType();
1664       // A global with opaque type might end up being zero sized.
1665       if (!Ty->isSized())
1666         return true;
1667       // A global with an empty type might lie at the address of any other
1668       // global.
1669       if (Ty->isEmptyTy())
1670         return true;
1671     }
1672     return false;
1673   };
1674   // Don't try to decide equality of aliases.
1675   if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
1676     if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
1677       return ICmpInst::ICMP_NE;
1678   return ICmpInst::BAD_ICMP_PREDICATE;
1679 }
1680 
1681 /// This function determines if there is anything we can decide about the two
1682 /// constants provided. This doesn't need to handle simple things like integer
1683 /// comparisons, but should instead handle ConstantExprs and GlobalValues.
1684 /// If we can determine that the two constants have a particular relation to
1685 /// each other, we should return the corresponding ICmp predicate, otherwise
1686 /// return ICmpInst::BAD_ICMP_PREDICATE.
1687 ///
1688 /// To simplify this code we canonicalize the relation so that the first
1689 /// operand is always the most "complex" of the two.  We consider simple
1690 /// constants (like ConstantInt) to be the simplest, followed by
1691 /// GlobalValues, followed by ConstantExpr's (the most complex).
1692 ///
1693 static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
1694                                                 bool isSigned) {
1695   assert(V1->getType() == V2->getType() &&
1696          "Cannot compare different types of values!");
1697   if (V1 == V2) return ICmpInst::ICMP_EQ;
1698 
1699   if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
1700       !isa<BlockAddress>(V1)) {
1701     if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) &&
1702         !isa<BlockAddress>(V2)) {
1703       // We distilled this down to a simple case, use the standard constant
1704       // folder.
1705       ConstantInt *R = nullptr;
1706       ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
1707       R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
1708       if (R && !R->isZero())
1709         return pred;
1710       pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1711       R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
1712       if (R && !R->isZero())
1713         return pred;
1714       pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1715       R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
1716       if (R && !R->isZero())
1717         return pred;
1718 
1719       // If we couldn't figure it out, bail.
1720       return ICmpInst::BAD_ICMP_PREDICATE;
1721     }
1722 
1723     // If the first operand is simple, swap operands.
1724     ICmpInst::Predicate SwappedRelation =
1725       evaluateICmpRelation(V2, V1, isSigned);
1726     if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1727       return ICmpInst::getSwappedPredicate(SwappedRelation);
1728 
1729   } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1730     if (isa<ConstantExpr>(V2)) {  // Swap as necessary.
1731       ICmpInst::Predicate SwappedRelation =
1732         evaluateICmpRelation(V2, V1, isSigned);
1733       if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1734         return ICmpInst::getSwappedPredicate(SwappedRelation);
1735       return ICmpInst::BAD_ICMP_PREDICATE;
1736     }
1737 
1738     // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1739     // constant (which, since the types must match, means that it's a
1740     // ConstantPointerNull).
1741     if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1742       return areGlobalsPotentiallyEqual(GV, GV2);
1743     } else if (isa<BlockAddress>(V2)) {
1744       return ICmpInst::ICMP_NE; // Globals never equal labels.
1745     } else {
1746       assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
1747       // GlobalVals can never be null unless they have external weak linkage.
1748       // We don't try to evaluate aliases here.
1749       // NOTE: We should not be doing this constant folding if null pointer
1750       // is considered valid for the function. But currently there is no way to
1751       // query it from the Constant type.
1752       if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1753           !NullPointerIsDefined(nullptr /* F */,
1754                                 GV->getType()->getAddressSpace()))
1755         return ICmpInst::ICMP_UGT;
1756     }
1757   } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1758     if (isa<ConstantExpr>(V2)) {  // Swap as necessary.
1759       ICmpInst::Predicate SwappedRelation =
1760         evaluateICmpRelation(V2, V1, isSigned);
1761       if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1762         return ICmpInst::getSwappedPredicate(SwappedRelation);
1763       return ICmpInst::BAD_ICMP_PREDICATE;
1764     }
1765 
1766     // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1767     // constant (which, since the types must match, means that it is a
1768     // ConstantPointerNull).
1769     if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1770       // Block address in another function can't equal this one, but block
1771       // addresses in the current function might be the same if blocks are
1772       // empty.
1773       if (BA2->getFunction() != BA->getFunction())
1774         return ICmpInst::ICMP_NE;
1775     } else {
1776       // Block addresses aren't null, don't equal the address of globals.
1777       assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
1778              "Canonicalization guarantee!");
1779       return ICmpInst::ICMP_NE;
1780     }
1781   } else {
1782     // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
1783     // constantexpr, a global, block address, or a simple constant.
1784     ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1785     Constant *CE1Op0 = CE1->getOperand(0);
1786 
1787     switch (CE1->getOpcode()) {
1788     case Instruction::Trunc:
1789     case Instruction::FPTrunc:
1790     case Instruction::FPExt:
1791     case Instruction::FPToUI:
1792     case Instruction::FPToSI:
1793       break; // We can't evaluate floating point casts or truncations.
1794 
1795     case Instruction::BitCast:
1796       // If this is a global value cast, check to see if the RHS is also a
1797       // GlobalValue.
1798       if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0))
1799         if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2))
1800           return areGlobalsPotentiallyEqual(GV, GV2);
1801       LLVM_FALLTHROUGH;
1802     case Instruction::UIToFP:
1803     case Instruction::SIToFP:
1804     case Instruction::ZExt:
1805     case Instruction::SExt:
1806       // We can't evaluate floating point casts or truncations.
1807       if (CE1Op0->getType()->isFPOrFPVectorTy())
1808         break;
1809 
1810       // If the cast is not actually changing bits, and the second operand is a
1811       // null pointer, do the comparison with the pre-casted value.
1812       if (V2->isNullValue() && CE1->getType()->isIntOrPtrTy()) {
1813         if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1814         if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
1815         return evaluateICmpRelation(CE1Op0,
1816                                     Constant::getNullValue(CE1Op0->getType()),
1817                                     isSigned);
1818       }
1819       break;
1820 
1821     case Instruction::GetElementPtr: {
1822       GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
1823       // Ok, since this is a getelementptr, we know that the constant has a
1824       // pointer type.  Check the various cases.
1825       if (isa<ConstantPointerNull>(V2)) {
1826         // If we are comparing a GEP to a null pointer, check to see if the base
1827         // of the GEP equals the null pointer.
1828         if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1829           // If its not weak linkage, the GVal must have a non-zero address
1830           // so the result is greater-than
1831           if (!GV->hasExternalWeakLinkage())
1832             return ICmpInst::ICMP_UGT;
1833         } else if (isa<ConstantPointerNull>(CE1Op0)) {
1834           // If we are indexing from a null pointer, check to see if we have any
1835           // non-zero indices.
1836           for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1837             if (!CE1->getOperand(i)->isNullValue())
1838               // Offsetting from null, must not be equal.
1839               return ICmpInst::ICMP_UGT;
1840           // Only zero indexes from null, must still be zero.
1841           return ICmpInst::ICMP_EQ;
1842         }
1843         // Otherwise, we can't really say if the first operand is null or not.
1844       } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1845         if (isa<ConstantPointerNull>(CE1Op0)) {
1846           // If its not weak linkage, the GVal must have a non-zero address
1847           // so the result is less-than
1848           if (!GV2->hasExternalWeakLinkage())
1849             return ICmpInst::ICMP_ULT;
1850         } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1851           if (GV == GV2) {
1852             // If this is a getelementptr of the same global, then it must be
1853             // different.  Because the types must match, the getelementptr could
1854             // only have at most one index, and because we fold getelementptr's
1855             // with a single zero index, it must be nonzero.
1856             assert(CE1->getNumOperands() == 2 &&
1857                    !CE1->getOperand(1)->isNullValue() &&
1858                    "Surprising getelementptr!");
1859             return ICmpInst::ICMP_UGT;
1860           } else {
1861             if (CE1GEP->hasAllZeroIndices())
1862               return areGlobalsPotentiallyEqual(GV, GV2);
1863             return ICmpInst::BAD_ICMP_PREDICATE;
1864           }
1865         }
1866       } else {
1867         ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1868         Constant *CE2Op0 = CE2->getOperand(0);
1869 
1870         // There are MANY other foldings that we could perform here.  They will
1871         // probably be added on demand, as they seem needed.
1872         switch (CE2->getOpcode()) {
1873         default: break;
1874         case Instruction::GetElementPtr:
1875           // By far the most common case to handle is when the base pointers are
1876           // obviously to the same global.
1877           if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1878             // Don't know relative ordering, but check for inequality.
1879             if (CE1Op0 != CE2Op0) {
1880               GEPOperator *CE2GEP = cast<GEPOperator>(CE2);
1881               if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
1882                 return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0),
1883                                                   cast<GlobalValue>(CE2Op0));
1884               return ICmpInst::BAD_ICMP_PREDICATE;
1885             }
1886             // Ok, we know that both getelementptr instructions are based on the
1887             // same global.  From this, we can precisely determine the relative
1888             // ordering of the resultant pointers.
1889             unsigned i = 1;
1890 
1891             // The logic below assumes that the result of the comparison
1892             // can be determined by finding the first index that differs.
1893             // This doesn't work if there is over-indexing in any
1894             // subsequent indices, so check for that case first.
1895             if (!CE1->isGEPWithNoNotionalOverIndexing() ||
1896                 !CE2->isGEPWithNoNotionalOverIndexing())
1897                return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1898 
1899             // Compare all of the operands the GEP's have in common.
1900             gep_type_iterator GTI = gep_type_begin(CE1);
1901             for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1902                  ++i, ++GTI)
1903               switch (IdxCompare(CE1->getOperand(i),
1904                                  CE2->getOperand(i), GTI.getIndexedType())) {
1905               case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1906               case 1:  return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1907               case -2: return ICmpInst::BAD_ICMP_PREDICATE;
1908               }
1909 
1910             // Ok, we ran out of things they have in common.  If any leftovers
1911             // are non-zero then we have a difference, otherwise we are equal.
1912             for (; i < CE1->getNumOperands(); ++i)
1913               if (!CE1->getOperand(i)->isNullValue()) {
1914                 if (isa<ConstantInt>(CE1->getOperand(i)))
1915                   return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1916                 else
1917                   return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1918               }
1919 
1920             for (; i < CE2->getNumOperands(); ++i)
1921               if (!CE2->getOperand(i)->isNullValue()) {
1922                 if (isa<ConstantInt>(CE2->getOperand(i)))
1923                   return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1924                 else
1925                   return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1926               }
1927             return ICmpInst::ICMP_EQ;
1928           }
1929         }
1930       }
1931       break;
1932     }
1933     default:
1934       break;
1935     }
1936   }
1937 
1938   return ICmpInst::BAD_ICMP_PREDICATE;
1939 }
1940 
1941 Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
1942                                                Constant *C1, Constant *C2) {
1943   Type *ResultTy;
1944   if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
1945     ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
1946                                VT->getElementCount());
1947   else
1948     ResultTy = Type::getInt1Ty(C1->getContext());
1949 
1950   // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
1951   if (pred == FCmpInst::FCMP_FALSE)
1952     return Constant::getNullValue(ResultTy);
1953 
1954   if (pred == FCmpInst::FCMP_TRUE)
1955     return Constant::getAllOnesValue(ResultTy);
1956 
1957   // Handle some degenerate cases first
1958   if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1959     return PoisonValue::get(ResultTy);
1960 
1961   if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1962     CmpInst::Predicate Predicate = CmpInst::Predicate(pred);
1963     bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
1964     // For EQ and NE, we can always pick a value for the undef to make the
1965     // predicate pass or fail, so we can return undef.
1966     // Also, if both operands are undef, we can return undef for int comparison.
1967     if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2))
1968       return UndefValue::get(ResultTy);
1969 
1970     // Otherwise, for integer compare, pick the same value as the non-undef
1971     // operand, and fold it to true or false.
1972     if (isIntegerPredicate)
1973       return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(Predicate));
1974 
1975     // Choosing NaN for the undef will always make unordered comparison succeed
1976     // and ordered comparison fails.
1977     return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate));
1978   }
1979 
1980   // icmp eq/ne(null,GV) -> false/true
1981   if (C1->isNullValue()) {
1982     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1983       // Don't try to evaluate aliases.  External weak GV can be null.
1984       if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
1985           !NullPointerIsDefined(nullptr /* F */,
1986                                 GV->getType()->getAddressSpace())) {
1987         if (pred == ICmpInst::ICMP_EQ)
1988           return ConstantInt::getFalse(C1->getContext());
1989         else if (pred == ICmpInst::ICMP_NE)
1990           return ConstantInt::getTrue(C1->getContext());
1991       }
1992   // icmp eq/ne(GV,null) -> false/true
1993   } else if (C2->isNullValue()) {
1994     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1)) {
1995       // Don't try to evaluate aliases.  External weak GV can be null.
1996       if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
1997           !NullPointerIsDefined(nullptr /* F */,
1998                                 GV->getType()->getAddressSpace())) {
1999         if (pred == ICmpInst::ICMP_EQ)
2000           return ConstantInt::getFalse(C1->getContext());
2001         else if (pred == ICmpInst::ICMP_NE)
2002           return ConstantInt::getTrue(C1->getContext());
2003       }
2004     }
2005 
2006     // The caller is expected to commute the operands if the constant expression
2007     // is C2.
2008     // C1 >= 0 --> true
2009     if (pred == ICmpInst::ICMP_UGE)
2010       return Constant::getAllOnesValue(ResultTy);
2011     // C1 < 0 --> false
2012     if (pred == ICmpInst::ICMP_ULT)
2013       return Constant::getNullValue(ResultTy);
2014   }
2015 
2016   // If the comparison is a comparison between two i1's, simplify it.
2017   if (C1->getType()->isIntegerTy(1)) {
2018     switch(pred) {
2019     case ICmpInst::ICMP_EQ:
2020       if (isa<ConstantInt>(C2))
2021         return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
2022       return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
2023     case ICmpInst::ICMP_NE:
2024       return ConstantExpr::getXor(C1, C2);
2025     default:
2026       break;
2027     }
2028   }
2029 
2030   if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
2031     const APInt &V1 = cast<ConstantInt>(C1)->getValue();
2032     const APInt &V2 = cast<ConstantInt>(C2)->getValue();
2033     switch (pred) {
2034     default: llvm_unreachable("Invalid ICmp Predicate");
2035     case ICmpInst::ICMP_EQ:  return ConstantInt::get(ResultTy, V1 == V2);
2036     case ICmpInst::ICMP_NE:  return ConstantInt::get(ResultTy, V1 != V2);
2037     case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2));
2038     case ICmpInst::ICMP_SGT: return ConstantInt::get(ResultTy, V1.sgt(V2));
2039     case ICmpInst::ICMP_SLE: return ConstantInt::get(ResultTy, V1.sle(V2));
2040     case ICmpInst::ICMP_SGE: return ConstantInt::get(ResultTy, V1.sge(V2));
2041     case ICmpInst::ICMP_ULT: return ConstantInt::get(ResultTy, V1.ult(V2));
2042     case ICmpInst::ICMP_UGT: return ConstantInt::get(ResultTy, V1.ugt(V2));
2043     case ICmpInst::ICMP_ULE: return ConstantInt::get(ResultTy, V1.ule(V2));
2044     case ICmpInst::ICMP_UGE: return ConstantInt::get(ResultTy, V1.uge(V2));
2045     }
2046   } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
2047     const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
2048     const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
2049     APFloat::cmpResult R = C1V.compare(C2V);
2050     switch (pred) {
2051     default: llvm_unreachable("Invalid FCmp Predicate");
2052     case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy);
2053     case FCmpInst::FCMP_TRUE:  return Constant::getAllOnesValue(ResultTy);
2054     case FCmpInst::FCMP_UNO:
2055       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered);
2056     case FCmpInst::FCMP_ORD:
2057       return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered);
2058     case FCmpInst::FCMP_UEQ:
2059       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
2060                                         R==APFloat::cmpEqual);
2061     case FCmpInst::FCMP_OEQ:
2062       return ConstantInt::get(ResultTy, R==APFloat::cmpEqual);
2063     case FCmpInst::FCMP_UNE:
2064       return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual);
2065     case FCmpInst::FCMP_ONE:
2066       return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
2067                                         R==APFloat::cmpGreaterThan);
2068     case FCmpInst::FCMP_ULT:
2069       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
2070                                         R==APFloat::cmpLessThan);
2071     case FCmpInst::FCMP_OLT:
2072       return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan);
2073     case FCmpInst::FCMP_UGT:
2074       return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
2075                                         R==APFloat::cmpGreaterThan);
2076     case FCmpInst::FCMP_OGT:
2077       return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan);
2078     case FCmpInst::FCMP_ULE:
2079       return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan);
2080     case FCmpInst::FCMP_OLE:
2081       return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
2082                                         R==APFloat::cmpEqual);
2083     case FCmpInst::FCMP_UGE:
2084       return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan);
2085     case FCmpInst::FCMP_OGE:
2086       return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan ||
2087                                         R==APFloat::cmpEqual);
2088     }
2089   } else if (auto *C1VTy = dyn_cast<VectorType>(C1->getType())) {
2090 
2091     // Fast path for splatted constants.
2092     if (Constant *C1Splat = C1->getSplatValue())
2093       if (Constant *C2Splat = C2->getSplatValue())
2094         return ConstantVector::getSplat(
2095             C1VTy->getElementCount(),
2096             ConstantExpr::getCompare(pred, C1Splat, C2Splat));
2097 
2098     // Do not iterate on scalable vector. The number of elements is unknown at
2099     // compile-time.
2100     if (isa<ScalableVectorType>(C1VTy))
2101       return nullptr;
2102 
2103     // If we can constant fold the comparison of each element, constant fold
2104     // the whole vector comparison.
2105     SmallVector<Constant*, 4> ResElts;
2106     Type *Ty = IntegerType::get(C1->getContext(), 32);
2107     // Compare the elements, producing an i1 result or constant expr.
2108     for (unsigned I = 0, E = C1VTy->getElementCount().getKnownMinValue();
2109          I != E; ++I) {
2110       Constant *C1E =
2111           ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, I));
2112       Constant *C2E =
2113           ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, I));
2114 
2115       ResElts.push_back(ConstantExpr::getCompare(pred, C1E, C2E));
2116     }
2117 
2118     return ConstantVector::get(ResElts);
2119   }
2120 
2121   if (C1->getType()->isFloatingPointTy() &&
2122       // Only call evaluateFCmpRelation if we have a constant expr to avoid
2123       // infinite recursive loop
2124       (isa<ConstantExpr>(C1) || isa<ConstantExpr>(C2))) {
2125     int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
2126     switch (evaluateFCmpRelation(C1, C2)) {
2127     default: llvm_unreachable("Unknown relation!");
2128     case FCmpInst::FCMP_UNO:
2129     case FCmpInst::FCMP_ORD:
2130     case FCmpInst::FCMP_UNE:
2131     case FCmpInst::FCMP_ULT:
2132     case FCmpInst::FCMP_UGT:
2133     case FCmpInst::FCMP_ULE:
2134     case FCmpInst::FCMP_UGE:
2135     case FCmpInst::FCMP_TRUE:
2136     case FCmpInst::FCMP_FALSE:
2137     case FCmpInst::BAD_FCMP_PREDICATE:
2138       break; // Couldn't determine anything about these constants.
2139     case FCmpInst::FCMP_OEQ: // We know that C1 == C2
2140       Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
2141                 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
2142                 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
2143       break;
2144     case FCmpInst::FCMP_OLT: // We know that C1 < C2
2145       Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
2146                 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
2147                 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
2148       break;
2149     case FCmpInst::FCMP_OGT: // We know that C1 > C2
2150       Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
2151                 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
2152                 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
2153       break;
2154     case FCmpInst::FCMP_OLE: // We know that C1 <= C2
2155       // We can only partially decide this relation.
2156       if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
2157         Result = 0;
2158       else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
2159         Result = 1;
2160       break;
2161     case FCmpInst::FCMP_OGE: // We known that C1 >= C2
2162       // We can only partially decide this relation.
2163       if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
2164         Result = 0;
2165       else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
2166         Result = 1;
2167       break;
2168     case FCmpInst::FCMP_ONE: // We know that C1 != C2
2169       // We can only partially decide this relation.
2170       if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
2171         Result = 0;
2172       else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
2173         Result = 1;
2174       break;
2175     case FCmpInst::FCMP_UEQ: // We know that C1 == C2 || isUnordered(C1, C2).
2176       // We can only partially decide this relation.
2177       if (pred == FCmpInst::FCMP_ONE)
2178         Result = 0;
2179       else if (pred == FCmpInst::FCMP_UEQ)
2180         Result = 1;
2181       break;
2182     }
2183 
2184     // If we evaluated the result, return it now.
2185     if (Result != -1)
2186       return ConstantInt::get(ResultTy, Result);
2187 
2188   } else {
2189     // Evaluate the relation between the two constants, per the predicate.
2190     int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
2191     switch (evaluateICmpRelation(C1, C2,
2192                                  CmpInst::isSigned((CmpInst::Predicate)pred))) {
2193     default: llvm_unreachable("Unknown relational!");
2194     case ICmpInst::BAD_ICMP_PREDICATE:
2195       break;  // Couldn't determine anything about these constants.
2196     case ICmpInst::ICMP_EQ:   // We know the constants are equal!
2197       // If we know the constants are equal, we can decide the result of this
2198       // computation precisely.
2199       Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred);
2200       break;
2201     case ICmpInst::ICMP_ULT:
2202       switch (pred) {
2203       case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
2204         Result = 1; break;
2205       case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
2206         Result = 0; break;
2207       }
2208       break;
2209     case ICmpInst::ICMP_SLT:
2210       switch (pred) {
2211       case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
2212         Result = 1; break;
2213       case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
2214         Result = 0; break;
2215       }
2216       break;
2217     case ICmpInst::ICMP_UGT:
2218       switch (pred) {
2219       case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
2220         Result = 1; break;
2221       case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
2222         Result = 0; break;
2223       }
2224       break;
2225     case ICmpInst::ICMP_SGT:
2226       switch (pred) {
2227       case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
2228         Result = 1; break;
2229       case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
2230         Result = 0; break;
2231       }
2232       break;
2233     case ICmpInst::ICMP_ULE:
2234       if (pred == ICmpInst::ICMP_UGT) Result = 0;
2235       if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1;
2236       break;
2237     case ICmpInst::ICMP_SLE:
2238       if (pred == ICmpInst::ICMP_SGT) Result = 0;
2239       if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1;
2240       break;
2241     case ICmpInst::ICMP_UGE:
2242       if (pred == ICmpInst::ICMP_ULT) Result = 0;
2243       if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1;
2244       break;
2245     case ICmpInst::ICMP_SGE:
2246       if (pred == ICmpInst::ICMP_SLT) Result = 0;
2247       if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1;
2248       break;
2249     case ICmpInst::ICMP_NE:
2250       if (pred == ICmpInst::ICMP_EQ) Result = 0;
2251       if (pred == ICmpInst::ICMP_NE) Result = 1;
2252       break;
2253     }
2254 
2255     // If we evaluated the result, return it now.
2256     if (Result != -1)
2257       return ConstantInt::get(ResultTy, Result);
2258 
2259     // If the right hand side is a bitcast, try using its inverse to simplify
2260     // it by moving it to the left hand side.  We can't do this if it would turn
2261     // a vector compare into a scalar compare or visa versa, or if it would turn
2262     // the operands into FP values.
2263     if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
2264       Constant *CE2Op0 = CE2->getOperand(0);
2265       if (CE2->getOpcode() == Instruction::BitCast &&
2266           CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy() &&
2267           !CE2Op0->getType()->isFPOrFPVectorTy()) {
2268         Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
2269         return ConstantExpr::getICmp(pred, Inverse, CE2Op0);
2270       }
2271     }
2272 
2273     // If the left hand side is an extension, try eliminating it.
2274     if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
2275       if ((CE1->getOpcode() == Instruction::SExt &&
2276            ICmpInst::isSigned((ICmpInst::Predicate)pred)) ||
2277           (CE1->getOpcode() == Instruction::ZExt &&
2278            !ICmpInst::isSigned((ICmpInst::Predicate)pred))){
2279         Constant *CE1Op0 = CE1->getOperand(0);
2280         Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
2281         if (CE1Inverse == CE1Op0) {
2282           // Check whether we can safely truncate the right hand side.
2283           Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
2284           if (ConstantExpr::getCast(CE1->getOpcode(), C2Inverse,
2285                                     C2->getType()) == C2)
2286             return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse);
2287         }
2288       }
2289     }
2290 
2291     if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
2292         (C1->isNullValue() && !C2->isNullValue())) {
2293       // If C2 is a constant expr and C1 isn't, flip them around and fold the
2294       // other way if possible.
2295       // Also, if C1 is null and C2 isn't, flip them around.
2296       pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
2297       return ConstantExpr::getICmp(pred, C2, C1);
2298     }
2299   }
2300   return nullptr;
2301 }
2302 
2303 /// Test whether the given sequence of *normalized* indices is "inbounds".
2304 template<typename IndexTy>
2305 static bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) {
2306   // No indices means nothing that could be out of bounds.
2307   if (Idxs.empty()) return true;
2308 
2309   // If the first index is zero, it's in bounds.
2310   if (cast<Constant>(Idxs[0])->isNullValue()) return true;
2311 
2312   // If the first index is one and all the rest are zero, it's in bounds,
2313   // by the one-past-the-end rule.
2314   if (auto *CI = dyn_cast<ConstantInt>(Idxs[0])) {
2315     if (!CI->isOne())
2316       return false;
2317   } else {
2318     auto *CV = cast<ConstantDataVector>(Idxs[0]);
2319     CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
2320     if (!CI || !CI->isOne())
2321       return false;
2322   }
2323 
2324   for (unsigned i = 1, e = Idxs.size(); i != e; ++i)
2325     if (!cast<Constant>(Idxs[i])->isNullValue())
2326       return false;
2327   return true;
2328 }
2329 
2330 /// Test whether a given ConstantInt is in-range for a SequentialType.
2331 static bool isIndexInRangeOfArrayType(uint64_t NumElements,
2332                                       const ConstantInt *CI) {
2333   // We cannot bounds check the index if it doesn't fit in an int64_t.
2334   if (CI->getValue().getMinSignedBits() > 64)
2335     return false;
2336 
2337   // A negative index or an index past the end of our sequential type is
2338   // considered out-of-range.
2339   int64_t IndexVal = CI->getSExtValue();
2340   if (IndexVal < 0 || (NumElements > 0 && (uint64_t)IndexVal >= NumElements))
2341     return false;
2342 
2343   // Otherwise, it is in-range.
2344   return true;
2345 }
2346 
2347 Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
2348                                           bool InBounds,
2349                                           Optional<unsigned> InRangeIndex,
2350                                           ArrayRef<Value *> Idxs) {
2351   if (Idxs.empty()) return C;
2352 
2353   Type *GEPTy = GetElementPtrInst::getGEPReturnType(
2354       PointeeTy, C, makeArrayRef((Value *const *)Idxs.data(), Idxs.size()));
2355 
2356   if (isa<PoisonValue>(C))
2357     return PoisonValue::get(GEPTy);
2358 
2359   if (isa<UndefValue>(C))
2360     // If inbounds, we can choose an out-of-bounds pointer as a base pointer.
2361     return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy);
2362 
2363   Constant *Idx0 = cast<Constant>(Idxs[0]);
2364   if (Idxs.size() == 1 && (Idx0->isNullValue() || isa<UndefValue>(Idx0)))
2365     return GEPTy->isVectorTy() && !C->getType()->isVectorTy()
2366                ? ConstantVector::getSplat(
2367                      cast<VectorType>(GEPTy)->getElementCount(), C)
2368                : C;
2369 
2370   if (C->isNullValue()) {
2371     bool isNull = true;
2372     for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
2373       if (!isa<UndefValue>(Idxs[i]) &&
2374           !cast<Constant>(Idxs[i])->isNullValue()) {
2375         isNull = false;
2376         break;
2377       }
2378     if (isNull) {
2379       PointerType *PtrTy = cast<PointerType>(C->getType()->getScalarType());
2380       Type *Ty = GetElementPtrInst::getIndexedType(PointeeTy, Idxs);
2381 
2382       assert(Ty && "Invalid indices for GEP!");
2383       Type *OrigGEPTy = PointerType::get(Ty, PtrTy->getAddressSpace());
2384       Type *GEPTy = PointerType::get(Ty, PtrTy->getAddressSpace());
2385       if (VectorType *VT = dyn_cast<VectorType>(C->getType()))
2386         GEPTy = VectorType::get(OrigGEPTy, VT->getElementCount());
2387 
2388       // The GEP returns a vector of pointers when one of more of
2389       // its arguments is a vector.
2390       for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
2391         if (auto *VT = dyn_cast<VectorType>(Idxs[i]->getType())) {
2392           assert((!isa<VectorType>(GEPTy) || isa<ScalableVectorType>(GEPTy) ==
2393                                                  isa<ScalableVectorType>(VT)) &&
2394                  "Mismatched GEPTy vector types");
2395           GEPTy = VectorType::get(OrigGEPTy, VT->getElementCount());
2396           break;
2397         }
2398       }
2399 
2400       return Constant::getNullValue(GEPTy);
2401     }
2402   }
2403 
2404   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2405     // Combine Indices - If the source pointer to this getelementptr instruction
2406     // is a getelementptr instruction, combine the indices of the two
2407     // getelementptr instructions into a single instruction.
2408     //
2409     if (CE->getOpcode() == Instruction::GetElementPtr) {
2410       gep_type_iterator LastI = gep_type_end(CE);
2411       for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
2412            I != E; ++I)
2413         LastI = I;
2414 
2415       // We cannot combine indices if doing so would take us outside of an
2416       // array or vector.  Doing otherwise could trick us if we evaluated such a
2417       // GEP as part of a load.
2418       //
2419       // e.g. Consider if the original GEP was:
2420       // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c,
2421       //                    i32 0, i32 0, i64 0)
2422       //
2423       // If we then tried to offset it by '8' to get to the third element,
2424       // an i8, we should *not* get:
2425       // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c,
2426       //                    i32 0, i32 0, i64 8)
2427       //
2428       // This GEP tries to index array element '8  which runs out-of-bounds.
2429       // Subsequent evaluation would get confused and produce erroneous results.
2430       //
2431       // The following prohibits such a GEP from being formed by checking to see
2432       // if the index is in-range with respect to an array.
2433       // TODO: This code may be extended to handle vectors as well.
2434       bool PerformFold = false;
2435       if (Idx0->isNullValue())
2436         PerformFold = true;
2437       else if (LastI.isSequential())
2438         if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx0))
2439           PerformFold = (!LastI.isBoundedSequential() ||
2440                          isIndexInRangeOfArrayType(
2441                              LastI.getSequentialNumElements(), CI)) &&
2442                         !CE->getOperand(CE->getNumOperands() - 1)
2443                              ->getType()
2444                              ->isVectorTy();
2445 
2446       if (PerformFold) {
2447         SmallVector<Value*, 16> NewIndices;
2448         NewIndices.reserve(Idxs.size() + CE->getNumOperands());
2449         NewIndices.append(CE->op_begin() + 1, CE->op_end() - 1);
2450 
2451         // Add the last index of the source with the first index of the new GEP.
2452         // Make sure to handle the case when they are actually different types.
2453         Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
2454         // Otherwise it must be an array.
2455         if (!Idx0->isNullValue()) {
2456           Type *IdxTy = Combined->getType();
2457           if (IdxTy != Idx0->getType()) {
2458             unsigned CommonExtendedWidth =
2459                 std::max(IdxTy->getIntegerBitWidth(),
2460                          Idx0->getType()->getIntegerBitWidth());
2461             CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
2462 
2463             Type *CommonTy =
2464                 Type::getIntNTy(IdxTy->getContext(), CommonExtendedWidth);
2465             Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, CommonTy);
2466             Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, CommonTy);
2467             Combined = ConstantExpr::get(Instruction::Add, C1, C2);
2468           } else {
2469             Combined =
2470               ConstantExpr::get(Instruction::Add, Idx0, Combined);
2471           }
2472         }
2473 
2474         NewIndices.push_back(Combined);
2475         NewIndices.append(Idxs.begin() + 1, Idxs.end());
2476 
2477         // The combined GEP normally inherits its index inrange attribute from
2478         // the inner GEP, but if the inner GEP's last index was adjusted by the
2479         // outer GEP, any inbounds attribute on that index is invalidated.
2480         Optional<unsigned> IRIndex = cast<GEPOperator>(CE)->getInRangeIndex();
2481         if (IRIndex && *IRIndex == CE->getNumOperands() - 2 && !Idx0->isNullValue())
2482           IRIndex = None;
2483 
2484         return ConstantExpr::getGetElementPtr(
2485             cast<GEPOperator>(CE)->getSourceElementType(), CE->getOperand(0),
2486             NewIndices, InBounds && cast<GEPOperator>(CE)->isInBounds(),
2487             IRIndex);
2488       }
2489     }
2490 
2491     // Attempt to fold casts to the same type away.  For example, folding:
2492     //
2493     //   i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
2494     //                       i64 0, i64 0)
2495     // into:
2496     //
2497     //   i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
2498     //
2499     // Don't fold if the cast is changing address spaces.
2500     if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) {
2501       PointerType *SrcPtrTy =
2502         dyn_cast<PointerType>(CE->getOperand(0)->getType());
2503       PointerType *DstPtrTy = dyn_cast<PointerType>(CE->getType());
2504       if (SrcPtrTy && DstPtrTy) {
2505         ArrayType *SrcArrayTy =
2506           dyn_cast<ArrayType>(SrcPtrTy->getElementType());
2507         ArrayType *DstArrayTy =
2508           dyn_cast<ArrayType>(DstPtrTy->getElementType());
2509         if (SrcArrayTy && DstArrayTy
2510             && SrcArrayTy->getElementType() == DstArrayTy->getElementType()
2511             && SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
2512           return ConstantExpr::getGetElementPtr(SrcArrayTy,
2513                                                 (Constant *)CE->getOperand(0),
2514                                                 Idxs, InBounds, InRangeIndex);
2515       }
2516     }
2517   }
2518 
2519   // Check to see if any array indices are not within the corresponding
2520   // notional array or vector bounds. If so, try to determine if they can be
2521   // factored out into preceding dimensions.
2522   SmallVector<Constant *, 8> NewIdxs;
2523   Type *Ty = PointeeTy;
2524   Type *Prev = C->getType();
2525   auto GEPIter = gep_type_begin(PointeeTy, Idxs);
2526   bool Unknown =
2527       !isa<ConstantInt>(Idxs[0]) && !isa<ConstantDataVector>(Idxs[0]);
2528   for (unsigned i = 1, e = Idxs.size(); i != e;
2529        Prev = Ty, Ty = (++GEPIter).getIndexedType(), ++i) {
2530     if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) {
2531       // We don't know if it's in range or not.
2532       Unknown = true;
2533       continue;
2534     }
2535     if (!isa<ConstantInt>(Idxs[i - 1]) && !isa<ConstantDataVector>(Idxs[i - 1]))
2536       // Skip if the type of the previous index is not supported.
2537       continue;
2538     if (InRangeIndex && i == *InRangeIndex + 1) {
2539       // If an index is marked inrange, we cannot apply this canonicalization to
2540       // the following index, as that will cause the inrange index to point to
2541       // the wrong element.
2542       continue;
2543     }
2544     if (isa<StructType>(Ty)) {
2545       // The verify makes sure that GEPs into a struct are in range.
2546       continue;
2547     }
2548     if (isa<VectorType>(Ty)) {
2549       // There can be awkward padding in after a non-power of two vector.
2550       Unknown = true;
2551       continue;
2552     }
2553     auto *STy = cast<ArrayType>(Ty);
2554     if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
2555       if (isIndexInRangeOfArrayType(STy->getNumElements(), CI))
2556         // It's in range, skip to the next index.
2557         continue;
2558       if (CI->getSExtValue() < 0) {
2559         // It's out of range and negative, don't try to factor it.
2560         Unknown = true;
2561         continue;
2562       }
2563     } else {
2564       auto *CV = cast<ConstantDataVector>(Idxs[i]);
2565       bool InRange = true;
2566       for (unsigned I = 0, E = CV->getNumElements(); I != E; ++I) {
2567         auto *CI = cast<ConstantInt>(CV->getElementAsConstant(I));
2568         InRange &= isIndexInRangeOfArrayType(STy->getNumElements(), CI);
2569         if (CI->getSExtValue() < 0) {
2570           Unknown = true;
2571           break;
2572         }
2573       }
2574       if (InRange || Unknown)
2575         // It's in range, skip to the next index.
2576         // It's out of range and negative, don't try to factor it.
2577         continue;
2578     }
2579     if (isa<StructType>(Prev)) {
2580       // It's out of range, but the prior dimension is a struct
2581       // so we can't do anything about it.
2582       Unknown = true;
2583       continue;
2584     }
2585     // It's out of range, but we can factor it into the prior
2586     // dimension.
2587     NewIdxs.resize(Idxs.size());
2588     // Determine the number of elements in our sequential type.
2589     uint64_t NumElements = STy->getArrayNumElements();
2590 
2591     // Expand the current index or the previous index to a vector from a scalar
2592     // if necessary.
2593     Constant *CurrIdx = cast<Constant>(Idxs[i]);
2594     auto *PrevIdx =
2595         NewIdxs[i - 1] ? NewIdxs[i - 1] : cast<Constant>(Idxs[i - 1]);
2596     bool IsCurrIdxVector = CurrIdx->getType()->isVectorTy();
2597     bool IsPrevIdxVector = PrevIdx->getType()->isVectorTy();
2598     bool UseVector = IsCurrIdxVector || IsPrevIdxVector;
2599 
2600     if (!IsCurrIdxVector && IsPrevIdxVector)
2601       CurrIdx = ConstantDataVector::getSplat(
2602           cast<FixedVectorType>(PrevIdx->getType())->getNumElements(), CurrIdx);
2603 
2604     if (!IsPrevIdxVector && IsCurrIdxVector)
2605       PrevIdx = ConstantDataVector::getSplat(
2606           cast<FixedVectorType>(CurrIdx->getType())->getNumElements(), PrevIdx);
2607 
2608     Constant *Factor =
2609         ConstantInt::get(CurrIdx->getType()->getScalarType(), NumElements);
2610     if (UseVector)
2611       Factor = ConstantDataVector::getSplat(
2612           IsPrevIdxVector
2613               ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements()
2614               : cast<FixedVectorType>(CurrIdx->getType())->getNumElements(),
2615           Factor);
2616 
2617     NewIdxs[i] = ConstantExpr::getSRem(CurrIdx, Factor);
2618 
2619     Constant *Div = ConstantExpr::getSDiv(CurrIdx, Factor);
2620 
2621     unsigned CommonExtendedWidth =
2622         std::max(PrevIdx->getType()->getScalarSizeInBits(),
2623                  Div->getType()->getScalarSizeInBits());
2624     CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
2625 
2626     // Before adding, extend both operands to i64 to avoid
2627     // overflow trouble.
2628     Type *ExtendedTy = Type::getIntNTy(Div->getContext(), CommonExtendedWidth);
2629     if (UseVector)
2630       ExtendedTy = FixedVectorType::get(
2631           ExtendedTy,
2632           IsPrevIdxVector
2633               ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements()
2634               : cast<FixedVectorType>(CurrIdx->getType())->getNumElements());
2635 
2636     if (!PrevIdx->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
2637       PrevIdx = ConstantExpr::getSExt(PrevIdx, ExtendedTy);
2638 
2639     if (!Div->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
2640       Div = ConstantExpr::getSExt(Div, ExtendedTy);
2641 
2642     NewIdxs[i - 1] = ConstantExpr::getAdd(PrevIdx, Div);
2643   }
2644 
2645   // If we did any factoring, start over with the adjusted indices.
2646   if (!NewIdxs.empty()) {
2647     for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
2648       if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
2649     return ConstantExpr::getGetElementPtr(PointeeTy, C, NewIdxs, InBounds,
2650                                           InRangeIndex);
2651   }
2652 
2653   // If all indices are known integers and normalized, we can do a simple
2654   // check for the "inbounds" property.
2655   if (!Unknown && !InBounds)
2656     if (auto *GV = dyn_cast<GlobalVariable>(C))
2657       if (!GV->hasExternalWeakLinkage() && isInBoundsIndices(Idxs))
2658         return ConstantExpr::getGetElementPtr(PointeeTy, C, Idxs,
2659                                               /*InBounds=*/true, InRangeIndex);
2660 
2661   return nullptr;
2662 }
2663