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 "llvm/IR/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 using namespace llvm;
34 using namespace llvm::PatternMatch;
35 
36 //===----------------------------------------------------------------------===//
37 //                ConstantFold*Instruction Implementations
38 //===----------------------------------------------------------------------===//
39 
40 /// This function determines which opcode to use to fold two constant cast
41 /// expressions together. It uses CastInst::isEliminableCastPair to determine
42 /// the opcode. Consequently its just a wrapper around that function.
43 /// Determine if it is valid to fold a cast of a cast
44 static unsigned
foldConstantCastPair(unsigned opc,ConstantExpr * Op,Type * DstTy)45 foldConstantCastPair(
46   unsigned opc,          ///< opcode of the second cast constant expression
47   ConstantExpr *Op,      ///< the first cast constant expression
48   Type *DstTy            ///< destination type of the first cast
49 ) {
50   assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
51   assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
52   assert(CastInst::isCast(opc) && "Invalid cast opcode");
53 
54   // The types and opcodes for the two Cast constant expressions
55   Type *SrcTy = Op->getOperand(0)->getType();
56   Type *MidTy = Op->getType();
57   Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
58   Instruction::CastOps secondOp = Instruction::CastOps(opc);
59 
60   // Assume that pointers are never more than 64 bits wide, and only use this
61   // for the middle type. Otherwise we could end up folding away illegal
62   // bitcasts between address spaces with different sizes.
63   IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext());
64 
65   // Let CastInst::isEliminableCastPair do the heavy lifting.
66   return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
67                                         nullptr, FakeIntPtrTy, nullptr);
68 }
69 
FoldBitCast(Constant * V,Type * DestTy)70 static Constant *FoldBitCast(Constant *V, Type *DestTy) {
71   Type *SrcTy = V->getType();
72   if (SrcTy == DestTy)
73     return V; // no-op cast
74 
75   // Handle casts from one vector constant to another.  We know that the src
76   // and dest type have the same size (otherwise its an illegal cast).
77   if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
78     if (V->isAllOnesValue())
79       return Constant::getAllOnesValue(DestTy);
80 
81     // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
82     // This allows for other simplifications (although some of them
83     // can only be handled by Analysis/ConstantFolding.cpp).
84     if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
85       return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy);
86     return nullptr;
87   }
88 
89   // Handle integral constant input.
90   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
91     // See note below regarding the PPC_FP128 restriction.
92     if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty())
93       return ConstantFP::get(DestTy->getContext(),
94                              APFloat(DestTy->getFltSemantics(),
95                                      CI->getValue()));
96 
97     // Otherwise, can't fold this (vector?)
98     return nullptr;
99   }
100 
101   // Handle ConstantFP input: FP -> Integral.
102   if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
103     // PPC_FP128 is really the sum of two consecutive doubles, where the first
104     // double is always stored first in memory, regardless of the target
105     // endianness. The memory layout of i128, however, depends on the target
106     // endianness, and so we can't fold this without target endianness
107     // information. This should instead be handled by
108     // Analysis/ConstantFolding.cpp
109     if (FP->getType()->isPPC_FP128Ty())
110       return nullptr;
111 
112     // Make sure dest type is compatible with the folded integer constant.
113     if (!DestTy->isIntegerTy())
114       return nullptr;
115 
116     return ConstantInt::get(FP->getContext(),
117                             FP->getValueAPF().bitcastToAPInt());
118   }
119 
120   return nullptr;
121 }
122 
123 
124 /// V is an integer constant which only has a subset of its bytes used.
125 /// The bytes used are indicated by ByteStart (which is the first byte used,
126 /// counting from the least significant byte) and ByteSize, which is the number
127 /// of bytes used.
128 ///
129 /// This function analyzes the specified constant to see if the specified byte
130 /// range can be returned as a simplified constant.  If so, the constant is
131 /// returned, otherwise null is returned.
ExtractConstantBytes(Constant * C,unsigned ByteStart,unsigned ByteSize)132 static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
133                                       unsigned ByteSize) {
134   assert(C->getType()->isIntegerTy() &&
135          (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
136          "Non-byte sized integer input");
137   [[maybe_unused]] unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
138   assert(ByteSize && "Must be accessing some piece");
139   assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
140   assert(ByteSize != CSize && "Should not extract everything");
141 
142   // Constant Integers are simple.
143   if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
144     APInt V = CI->getValue();
145     if (ByteStart)
146       V.lshrInPlace(ByteStart*8);
147     V = V.trunc(ByteSize*8);
148     return ConstantInt::get(CI->getContext(), V);
149   }
150 
151   // In the input is a constant expr, we might be able to recursively simplify.
152   // If not, we definitely can't do anything.
153   ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
154   if (!CE) return nullptr;
155 
156   switch (CE->getOpcode()) {
157   default: return nullptr;
158   case Instruction::Shl: {
159     ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
160     if (!Amt)
161       return nullptr;
162     APInt ShAmt = Amt->getValue();
163     // Cannot analyze non-byte shifts.
164     if ((ShAmt & 7) != 0)
165       return nullptr;
166     ShAmt.lshrInPlace(3);
167 
168     // If the extract is known to be all zeros, return zero.
169     if (ShAmt.uge(ByteStart + ByteSize))
170       return Constant::getNullValue(
171           IntegerType::get(CE->getContext(), ByteSize * 8));
172     // If the extract is known to be fully in the input, extract it.
173     if (ShAmt.ule(ByteStart))
174       return ExtractConstantBytes(CE->getOperand(0),
175                                   ByteStart - ShAmt.getZExtValue(), ByteSize);
176 
177     // TODO: Handle the 'partially zero' case.
178     return nullptr;
179   }
180   }
181 }
182 
foldMaybeUndesirableCast(unsigned opc,Constant * V,Type * DestTy)183 static Constant *foldMaybeUndesirableCast(unsigned opc, Constant *V,
184                                           Type *DestTy) {
185   return ConstantExpr::isDesirableCastOp(opc)
186              ? ConstantExpr::getCast(opc, V, DestTy)
187              : ConstantFoldCastInstruction(opc, V, DestTy);
188 }
189 
ConstantFoldCastInstruction(unsigned opc,Constant * V,Type * DestTy)190 Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
191                                             Type *DestTy) {
192   if (isa<PoisonValue>(V))
193     return PoisonValue::get(DestTy);
194 
195   if (isa<UndefValue>(V)) {
196     // zext(undef) = 0, because the top bits will be zero.
197     // sext(undef) = 0, because the top bits will all be the same.
198     // [us]itofp(undef) = 0, because the result value is bounded.
199     if (opc == Instruction::ZExt || opc == Instruction::SExt ||
200         opc == Instruction::UIToFP || opc == Instruction::SIToFP)
201       return Constant::getNullValue(DestTy);
202     return UndefValue::get(DestTy);
203   }
204 
205   if (V->isNullValue() && !DestTy->isX86_MMXTy() && !DestTy->isX86_AMXTy() &&
206       opc != Instruction::AddrSpaceCast)
207     return Constant::getNullValue(DestTy);
208 
209   // If the cast operand is a constant expression, there's a few things we can
210   // do to try to simplify it.
211   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
212     if (CE->isCast()) {
213       // Try hard to fold cast of cast because they are often eliminable.
214       if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
215         return foldMaybeUndesirableCast(newOpc, CE->getOperand(0), DestTy);
216     }
217   }
218 
219   // If the cast operand is a constant vector, perform the cast by
220   // operating on each element. In the cast of bitcasts, the element
221   // count may be mismatched; don't attempt to handle that here.
222   if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
223       DestTy->isVectorTy() &&
224       cast<FixedVectorType>(DestTy)->getNumElements() ==
225           cast<FixedVectorType>(V->getType())->getNumElements()) {
226     VectorType *DestVecTy = cast<VectorType>(DestTy);
227     Type *DstEltTy = DestVecTy->getElementType();
228     // Fast path for splatted constants.
229     if (Constant *Splat = V->getSplatValue()) {
230       Constant *Res = foldMaybeUndesirableCast(opc, Splat, DstEltTy);
231       if (!Res)
232         return nullptr;
233       return ConstantVector::getSplat(
234           cast<VectorType>(DestTy)->getElementCount(), Res);
235     }
236     SmallVector<Constant *, 16> res;
237     Type *Ty = IntegerType::get(V->getContext(), 32);
238     for (unsigned i = 0,
239                   e = cast<FixedVectorType>(V->getType())->getNumElements();
240          i != e; ++i) {
241       Constant *C = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
242       Constant *Casted = foldMaybeUndesirableCast(opc, C, DstEltTy);
243       if (!Casted)
244         return nullptr;
245       res.push_back(Casted);
246     }
247     return ConstantVector::get(res);
248   }
249 
250   // We actually have to do a cast now. Perform the cast according to the
251   // opcode specified.
252   switch (opc) {
253   default:
254     llvm_unreachable("Failed to cast constant expression");
255   case Instruction::FPTrunc:
256   case Instruction::FPExt:
257     if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
258       bool ignored;
259       APFloat Val = FPC->getValueAPF();
260       Val.convert(DestTy->getFltSemantics(), APFloat::rmNearestTiesToEven,
261                   &ignored);
262       return ConstantFP::get(V->getContext(), Val);
263     }
264     return nullptr; // Can't fold.
265   case Instruction::FPToUI:
266   case Instruction::FPToSI:
267     if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
268       const APFloat &V = FPC->getValueAPF();
269       bool ignored;
270       uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
271       APSInt IntVal(DestBitWidth, opc == Instruction::FPToUI);
272       if (APFloat::opInvalidOp ==
273           V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) {
274         // Undefined behavior invoked - the destination type can't represent
275         // the input constant.
276         return PoisonValue::get(DestTy);
277       }
278       return ConstantInt::get(FPC->getContext(), IntVal);
279     }
280     return nullptr; // Can't fold.
281   case Instruction::UIToFP:
282   case Instruction::SIToFP:
283     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
284       const APInt &api = CI->getValue();
285       APFloat apf(DestTy->getFltSemantics(),
286                   APInt::getZero(DestTy->getPrimitiveSizeInBits()));
287       apf.convertFromAPInt(api, opc==Instruction::SIToFP,
288                            APFloat::rmNearestTiesToEven);
289       return ConstantFP::get(V->getContext(), apf);
290     }
291     return nullptr;
292   case Instruction::ZExt:
293     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
294       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
295       return ConstantInt::get(V->getContext(),
296                               CI->getValue().zext(BitWidth));
297     }
298     return nullptr;
299   case Instruction::SExt:
300     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
301       uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
302       return ConstantInt::get(V->getContext(),
303                               CI->getValue().sext(BitWidth));
304     }
305     return nullptr;
306   case Instruction::Trunc: {
307     if (V->getType()->isVectorTy())
308       return nullptr;
309 
310     uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
311     if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
312       return ConstantInt::get(V->getContext(),
313                               CI->getValue().trunc(DestBitWidth));
314     }
315 
316     // The input must be a constantexpr.  See if we can simplify this based on
317     // the bytes we are demanding.  Only do this if the source and dest are an
318     // even multiple of a byte.
319     if ((DestBitWidth & 7) == 0 &&
320         (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
321       if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
322         return Res;
323 
324     return nullptr;
325   }
326   case Instruction::BitCast:
327     return FoldBitCast(V, DestTy);
328   case Instruction::AddrSpaceCast:
329   case Instruction::IntToPtr:
330   case Instruction::PtrToInt:
331     return nullptr;
332   }
333 }
334 
ConstantFoldSelectInstruction(Constant * Cond,Constant * V1,Constant * V2)335 Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
336                                               Constant *V1, Constant *V2) {
337   // Check for i1 and vector true/false conditions.
338   if (Cond->isNullValue()) return V2;
339   if (Cond->isAllOnesValue()) return V1;
340 
341   // If the condition is a vector constant, fold the result elementwise.
342   if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
343     auto *V1VTy = CondV->getType();
344     SmallVector<Constant*, 16> Result;
345     Type *Ty = IntegerType::get(CondV->getContext(), 32);
346     for (unsigned i = 0, e = V1VTy->getNumElements(); i != e; ++i) {
347       Constant *V;
348       Constant *V1Element = ConstantExpr::getExtractElement(V1,
349                                                     ConstantInt::get(Ty, i));
350       Constant *V2Element = ConstantExpr::getExtractElement(V2,
351                                                     ConstantInt::get(Ty, i));
352       auto *Cond = cast<Constant>(CondV->getOperand(i));
353       if (isa<PoisonValue>(Cond)) {
354         V = PoisonValue::get(V1Element->getType());
355       } else if (V1Element == V2Element) {
356         V = V1Element;
357       } else if (isa<UndefValue>(Cond)) {
358         V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
359       } else {
360         if (!isa<ConstantInt>(Cond)) break;
361         V = Cond->isNullValue() ? V2Element : V1Element;
362       }
363       Result.push_back(V);
364     }
365 
366     // If we were able to build the vector, return it.
367     if (Result.size() == V1VTy->getNumElements())
368       return ConstantVector::get(Result);
369   }
370 
371   if (isa<PoisonValue>(Cond))
372     return PoisonValue::get(V1->getType());
373 
374   if (isa<UndefValue>(Cond)) {
375     if (isa<UndefValue>(V1)) return V1;
376     return V2;
377   }
378 
379   if (V1 == V2) return V1;
380 
381   if (isa<PoisonValue>(V1))
382     return V2;
383   if (isa<PoisonValue>(V2))
384     return V1;
385 
386   // If the true or false value is undef, we can fold to the other value as
387   // long as the other value isn't poison.
388   auto NotPoison = [](Constant *C) {
389     if (isa<PoisonValue>(C))
390       return false;
391 
392     // TODO: We can analyze ConstExpr by opcode to determine if there is any
393     //       possibility of poison.
394     if (isa<ConstantExpr>(C))
395       return false;
396 
397     if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(C) ||
398         isa<ConstantPointerNull>(C) || isa<Function>(C))
399       return true;
400 
401     if (C->getType()->isVectorTy())
402       return !C->containsPoisonElement() && !C->containsConstantExpression();
403 
404     // TODO: Recursively analyze aggregates or other constants.
405     return false;
406   };
407   if (isa<UndefValue>(V1) && NotPoison(V2)) return V2;
408   if (isa<UndefValue>(V2) && NotPoison(V1)) return V1;
409 
410   return nullptr;
411 }
412 
ConstantFoldExtractElementInstruction(Constant * Val,Constant * Idx)413 Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
414                                                       Constant *Idx) {
415   auto *ValVTy = cast<VectorType>(Val->getType());
416 
417   // extractelt poison, C -> poison
418   // extractelt C, undef -> poison
419   if (isa<PoisonValue>(Val) || isa<UndefValue>(Idx))
420     return PoisonValue::get(ValVTy->getElementType());
421 
422   // extractelt undef, C -> undef
423   if (isa<UndefValue>(Val))
424     return UndefValue::get(ValVTy->getElementType());
425 
426   auto *CIdx = dyn_cast<ConstantInt>(Idx);
427   if (!CIdx)
428     return nullptr;
429 
430   if (auto *ValFVTy = dyn_cast<FixedVectorType>(Val->getType())) {
431     // ee({w,x,y,z}, wrong_value) -> poison
432     if (CIdx->uge(ValFVTy->getNumElements()))
433       return PoisonValue::get(ValFVTy->getElementType());
434   }
435 
436   // ee (gep (ptr, idx0, ...), idx) -> gep (ee (ptr, idx), ee (idx0, idx), ...)
437   if (auto *CE = dyn_cast<ConstantExpr>(Val)) {
438     if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
439       SmallVector<Constant *, 8> Ops;
440       Ops.reserve(CE->getNumOperands());
441       for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
442         Constant *Op = CE->getOperand(i);
443         if (Op->getType()->isVectorTy()) {
444           Constant *ScalarOp = ConstantExpr::getExtractElement(Op, Idx);
445           if (!ScalarOp)
446             return nullptr;
447           Ops.push_back(ScalarOp);
448         } else
449           Ops.push_back(Op);
450       }
451       return CE->getWithOperands(Ops, ValVTy->getElementType(), false,
452                                  GEP->getSourceElementType());
453     } else if (CE->getOpcode() == Instruction::InsertElement) {
454       if (const auto *IEIdx = dyn_cast<ConstantInt>(CE->getOperand(2))) {
455         if (APSInt::isSameValue(APSInt(IEIdx->getValue()),
456                                 APSInt(CIdx->getValue()))) {
457           return CE->getOperand(1);
458         } else {
459           return ConstantExpr::getExtractElement(CE->getOperand(0), CIdx);
460         }
461       }
462     }
463   }
464 
465   if (Constant *C = Val->getAggregateElement(CIdx))
466     return C;
467 
468   // Lane < Splat minimum vector width => extractelt Splat(x), Lane -> x
469   if (CIdx->getValue().ult(ValVTy->getElementCount().getKnownMinValue())) {
470     if (Constant *SplatVal = Val->getSplatValue())
471       return SplatVal;
472   }
473 
474   return nullptr;
475 }
476 
ConstantFoldInsertElementInstruction(Constant * Val,Constant * Elt,Constant * Idx)477 Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val,
478                                                      Constant *Elt,
479                                                      Constant *Idx) {
480   if (isa<UndefValue>(Idx))
481     return PoisonValue::get(Val->getType());
482 
483   // Inserting null into all zeros is still all zeros.
484   // TODO: This is true for undef and poison splats too.
485   if (isa<ConstantAggregateZero>(Val) && Elt->isNullValue())
486     return Val;
487 
488   ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
489   if (!CIdx) return nullptr;
490 
491   // Do not iterate on scalable vector. The num of elements is unknown at
492   // compile-time.
493   if (isa<ScalableVectorType>(Val->getType()))
494     return nullptr;
495 
496   auto *ValTy = cast<FixedVectorType>(Val->getType());
497 
498   unsigned NumElts = ValTy->getNumElements();
499   if (CIdx->uge(NumElts))
500     return PoisonValue::get(Val->getType());
501 
502   SmallVector<Constant*, 16> Result;
503   Result.reserve(NumElts);
504   auto *Ty = Type::getInt32Ty(Val->getContext());
505   uint64_t IdxVal = CIdx->getZExtValue();
506   for (unsigned i = 0; i != NumElts; ++i) {
507     if (i == IdxVal) {
508       Result.push_back(Elt);
509       continue;
510     }
511 
512     Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
513     Result.push_back(C);
514   }
515 
516   return ConstantVector::get(Result);
517 }
518 
ConstantFoldShuffleVectorInstruction(Constant * V1,Constant * V2,ArrayRef<int> Mask)519 Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
520                                                      ArrayRef<int> Mask) {
521   auto *V1VTy = cast<VectorType>(V1->getType());
522   unsigned MaskNumElts = Mask.size();
523   auto MaskEltCount =
524       ElementCount::get(MaskNumElts, isa<ScalableVectorType>(V1VTy));
525   Type *EltTy = V1VTy->getElementType();
526 
527   // Poison shuffle mask -> poison value.
528   if (all_of(Mask, [](int Elt) { return Elt == PoisonMaskElem; })) {
529     return PoisonValue::get(VectorType::get(EltTy, MaskEltCount));
530   }
531 
532   // If the mask is all zeros this is a splat, no need to go through all
533   // elements.
534   if (all_of(Mask, [](int Elt) { return Elt == 0; })) {
535     Type *Ty = IntegerType::get(V1->getContext(), 32);
536     Constant *Elt =
537         ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, 0));
538 
539     if (Elt->isNullValue()) {
540       auto *VTy = VectorType::get(EltTy, MaskEltCount);
541       return ConstantAggregateZero::get(VTy);
542     } else if (!MaskEltCount.isScalable())
543       return ConstantVector::getSplat(MaskEltCount, Elt);
544   }
545   // Do not iterate on scalable vector. The num of elements is unknown at
546   // compile-time.
547   if (isa<ScalableVectorType>(V1VTy))
548     return nullptr;
549 
550   unsigned SrcNumElts = V1VTy->getElementCount().getKnownMinValue();
551 
552   // Loop over the shuffle mask, evaluating each element.
553   SmallVector<Constant*, 32> Result;
554   for (unsigned i = 0; i != MaskNumElts; ++i) {
555     int Elt = Mask[i];
556     if (Elt == -1) {
557       Result.push_back(UndefValue::get(EltTy));
558       continue;
559     }
560     Constant *InElt;
561     if (unsigned(Elt) >= SrcNumElts*2)
562       InElt = UndefValue::get(EltTy);
563     else if (unsigned(Elt) >= SrcNumElts) {
564       Type *Ty = IntegerType::get(V2->getContext(), 32);
565       InElt =
566         ConstantExpr::getExtractElement(V2,
567                                         ConstantInt::get(Ty, Elt - SrcNumElts));
568     } else {
569       Type *Ty = IntegerType::get(V1->getContext(), 32);
570       InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt));
571     }
572     Result.push_back(InElt);
573   }
574 
575   return ConstantVector::get(Result);
576 }
577 
ConstantFoldExtractValueInstruction(Constant * Agg,ArrayRef<unsigned> Idxs)578 Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
579                                                     ArrayRef<unsigned> Idxs) {
580   // Base case: no indices, so return the entire value.
581   if (Idxs.empty())
582     return Agg;
583 
584   if (Constant *C = Agg->getAggregateElement(Idxs[0]))
585     return ConstantFoldExtractValueInstruction(C, Idxs.slice(1));
586 
587   return nullptr;
588 }
589 
ConstantFoldInsertValueInstruction(Constant * Agg,Constant * Val,ArrayRef<unsigned> Idxs)590 Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
591                                                    Constant *Val,
592                                                    ArrayRef<unsigned> Idxs) {
593   // Base case: no indices, so replace the entire value.
594   if (Idxs.empty())
595     return Val;
596 
597   unsigned NumElts;
598   if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
599     NumElts = ST->getNumElements();
600   else
601     NumElts = cast<ArrayType>(Agg->getType())->getNumElements();
602 
603   SmallVector<Constant*, 32> Result;
604   for (unsigned i = 0; i != NumElts; ++i) {
605     Constant *C = Agg->getAggregateElement(i);
606     if (!C) return nullptr;
607 
608     if (Idxs[0] == i)
609       C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1));
610 
611     Result.push_back(C);
612   }
613 
614   if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
615     return ConstantStruct::get(ST, Result);
616   return ConstantArray::get(cast<ArrayType>(Agg->getType()), Result);
617 }
618 
ConstantFoldUnaryInstruction(unsigned Opcode,Constant * C)619 Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) {
620   assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected");
621 
622   // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length
623   // vectors are always evaluated per element.
624   bool IsScalableVector = isa<ScalableVectorType>(C->getType());
625   bool HasScalarUndefOrScalableVectorUndef =
626       (!C->getType()->isVectorTy() || IsScalableVector) && isa<UndefValue>(C);
627 
628   if (HasScalarUndefOrScalableVectorUndef) {
629     switch (static_cast<Instruction::UnaryOps>(Opcode)) {
630     case Instruction::FNeg:
631       return C; // -undef -> undef
632     case Instruction::UnaryOpsEnd:
633       llvm_unreachable("Invalid UnaryOp");
634     }
635   }
636 
637   // Constant should not be UndefValue, unless these are vector constants.
638   assert(!HasScalarUndefOrScalableVectorUndef && "Unexpected UndefValue");
639   // We only have FP UnaryOps right now.
640   assert(!isa<ConstantInt>(C) && "Unexpected Integer UnaryOp");
641 
642   if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
643     const APFloat &CV = CFP->getValueAPF();
644     switch (Opcode) {
645     default:
646       break;
647     case Instruction::FNeg:
648       return ConstantFP::get(C->getContext(), neg(CV));
649     }
650   } else if (auto *VTy = dyn_cast<FixedVectorType>(C->getType())) {
651 
652     Type *Ty = IntegerType::get(VTy->getContext(), 32);
653     // Fast path for splatted constants.
654     if (Constant *Splat = C->getSplatValue())
655       if (Constant *Elt = ConstantFoldUnaryInstruction(Opcode, Splat))
656         return ConstantVector::getSplat(VTy->getElementCount(), Elt);
657 
658     // Fold each element and create a vector constant from those constants.
659     SmallVector<Constant *, 16> Result;
660     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
661       Constant *ExtractIdx = ConstantInt::get(Ty, i);
662       Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx);
663       Constant *Res = ConstantFoldUnaryInstruction(Opcode, Elt);
664       if (!Res)
665         return nullptr;
666       Result.push_back(Res);
667     }
668 
669     return ConstantVector::get(Result);
670   }
671 
672   // We don't know how to fold this.
673   return nullptr;
674 }
675 
ConstantFoldBinaryInstruction(unsigned Opcode,Constant * C1,Constant * C2)676 Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
677                                               Constant *C2) {
678   assert(Instruction::isBinaryOp(Opcode) && "Non-binary instruction detected");
679 
680   // Simplify BinOps with their identity values first. They are no-ops and we
681   // can always return the other value, including undef or poison values.
682   if (Constant *Identity = ConstantExpr::getBinOpIdentity(
683           Opcode, C1->getType(), /*AllowRHSIdentity*/ false)) {
684     if (C1 == Identity)
685       return C2;
686     if (C2 == Identity)
687       return C1;
688   } else if (Constant *Identity = ConstantExpr::getBinOpIdentity(
689                  Opcode, C1->getType(), /*AllowRHSIdentity*/ true)) {
690     if (C2 == Identity)
691       return C1;
692   }
693 
694   // Binary operations propagate poison.
695   if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
696     return PoisonValue::get(C1->getType());
697 
698   // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length
699   // vectors are always evaluated per element.
700   bool IsScalableVector = isa<ScalableVectorType>(C1->getType());
701   bool HasScalarUndefOrScalableVectorUndef =
702       (!C1->getType()->isVectorTy() || IsScalableVector) &&
703       (isa<UndefValue>(C1) || isa<UndefValue>(C2));
704   if (HasScalarUndefOrScalableVectorUndef) {
705     switch (static_cast<Instruction::BinaryOps>(Opcode)) {
706     case Instruction::Xor:
707       if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
708         // Handle undef ^ undef -> 0 special case. This is a common
709         // idiom (misuse).
710         return Constant::getNullValue(C1->getType());
711       [[fallthrough]];
712     case Instruction::Add:
713     case Instruction::Sub:
714       return UndefValue::get(C1->getType());
715     case Instruction::And:
716       if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
717         return C1;
718       return Constant::getNullValue(C1->getType());   // undef & X -> 0
719     case Instruction::Mul: {
720       // undef * undef -> undef
721       if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
722         return C1;
723       const APInt *CV;
724       // X * undef -> undef   if X is odd
725       if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV)))
726         if ((*CV)[0])
727           return UndefValue::get(C1->getType());
728 
729       // X * undef -> 0       otherwise
730       return Constant::getNullValue(C1->getType());
731     }
732     case Instruction::SDiv:
733     case Instruction::UDiv:
734       // X / undef -> poison
735       // X / 0 -> poison
736       if (match(C2, m_CombineOr(m_Undef(), m_Zero())))
737         return PoisonValue::get(C2->getType());
738       // undef / X -> 0       otherwise
739       return Constant::getNullValue(C1->getType());
740     case Instruction::URem:
741     case Instruction::SRem:
742       // X % undef -> poison
743       // X % 0 -> poison
744       if (match(C2, m_CombineOr(m_Undef(), m_Zero())))
745         return PoisonValue::get(C2->getType());
746       // undef % X -> 0       otherwise
747       return Constant::getNullValue(C1->getType());
748     case Instruction::Or:                          // X | undef -> -1
749       if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
750         return C1;
751       return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
752     case Instruction::LShr:
753       // X >>l undef -> poison
754       if (isa<UndefValue>(C2))
755         return PoisonValue::get(C2->getType());
756       // undef >>l X -> 0
757       return Constant::getNullValue(C1->getType());
758     case Instruction::AShr:
759       // X >>a undef -> poison
760       if (isa<UndefValue>(C2))
761         return PoisonValue::get(C2->getType());
762       // TODO: undef >>a X -> poison if the shift is exact
763       // undef >>a X -> 0
764       return Constant::getNullValue(C1->getType());
765     case Instruction::Shl:
766       // X << undef -> undef
767       if (isa<UndefValue>(C2))
768         return PoisonValue::get(C2->getType());
769       // undef << X -> 0
770       return Constant::getNullValue(C1->getType());
771     case Instruction::FSub:
772       // -0.0 - undef --> undef (consistent with "fneg undef")
773       if (match(C1, m_NegZeroFP()) && isa<UndefValue>(C2))
774         return C2;
775       [[fallthrough]];
776     case Instruction::FAdd:
777     case Instruction::FMul:
778     case Instruction::FDiv:
779     case Instruction::FRem:
780       // [any flop] undef, undef -> undef
781       if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
782         return C1;
783       // [any flop] C, undef -> NaN
784       // [any flop] undef, C -> NaN
785       // We could potentially specialize NaN/Inf constants vs. 'normal'
786       // constants (possibly differently depending on opcode and operand). This
787       // would allow returning undef sometimes. But it is always safe to fold to
788       // NaN because we can choose the undef operand as NaN, and any FP opcode
789       // with a NaN operand will propagate NaN.
790       return ConstantFP::getNaN(C1->getType());
791     case Instruction::BinaryOpsEnd:
792       llvm_unreachable("Invalid BinaryOp");
793     }
794   }
795 
796   // Neither constant should be UndefValue, unless these are vector constants.
797   assert((!HasScalarUndefOrScalableVectorUndef) && "Unexpected UndefValue");
798 
799   // Handle simplifications when the RHS is a constant int.
800   if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
801     switch (Opcode) {
802     case Instruction::Mul:
803       if (CI2->isZero())
804         return C2; // X * 0 == 0
805       break;
806     case Instruction::UDiv:
807     case Instruction::SDiv:
808       if (CI2->isZero())
809         return PoisonValue::get(CI2->getType());              // X / 0 == poison
810       break;
811     case Instruction::URem:
812     case Instruction::SRem:
813       if (CI2->isOne())
814         return Constant::getNullValue(CI2->getType());        // X % 1 == 0
815       if (CI2->isZero())
816         return PoisonValue::get(CI2->getType());              // X % 0 == poison
817       break;
818     case Instruction::And:
819       if (CI2->isZero())
820         return C2; // X & 0 == 0
821 
822       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
823         // If and'ing the address of a global with a constant, fold it.
824         if (CE1->getOpcode() == Instruction::PtrToInt &&
825             isa<GlobalValue>(CE1->getOperand(0))) {
826           GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
827 
828           Align GVAlign; // defaults to 1
829 
830           if (Module *TheModule = GV->getParent()) {
831             const DataLayout &DL = TheModule->getDataLayout();
832             GVAlign = GV->getPointerAlignment(DL);
833 
834             // If the function alignment is not specified then assume that it
835             // is 4.
836             // This is dangerous; on x86, the alignment of the pointer
837             // corresponds to the alignment of the function, but might be less
838             // than 4 if it isn't explicitly specified.
839             // However, a fix for this behaviour was reverted because it
840             // increased code size (see https://reviews.llvm.org/D55115)
841             // FIXME: This code should be deleted once existing targets have
842             // appropriate defaults
843             if (isa<Function>(GV) && !DL.getFunctionPtrAlign())
844               GVAlign = Align(4);
845           } else if (isa<GlobalVariable>(GV)) {
846             GVAlign = cast<GlobalVariable>(GV)->getAlign().valueOrOne();
847           }
848 
849           if (GVAlign > 1) {
850             unsigned DstWidth = CI2->getBitWidth();
851             unsigned SrcWidth = std::min(DstWidth, Log2(GVAlign));
852             APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
853 
854             // If checking bits we know are clear, return zero.
855             if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
856               return Constant::getNullValue(CI2->getType());
857           }
858         }
859       }
860       break;
861     case Instruction::Or:
862       if (CI2->isMinusOne())
863         return C2; // X | -1 == -1
864       break;
865     case Instruction::Xor:
866       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
867         switch (CE1->getOpcode()) {
868         default:
869           break;
870         case Instruction::ICmp:
871         case Instruction::FCmp:
872           // cmp pred ^ true -> cmp !pred
873           assert(CI2->isOne());
874           CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
875           pred = CmpInst::getInversePredicate(pred);
876           return ConstantExpr::getCompare(pred, CE1->getOperand(0),
877                                           CE1->getOperand(1));
878         }
879       }
880       break;
881     }
882   } else if (isa<ConstantInt>(C1)) {
883     // If C1 is a ConstantInt and C2 is not, swap the operands.
884     if (Instruction::isCommutative(Opcode))
885       return ConstantExpr::isDesirableBinOp(Opcode)
886                  ? ConstantExpr::get(Opcode, C2, C1)
887                  : ConstantFoldBinaryInstruction(Opcode, C2, C1);
888   }
889 
890   if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
891     if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
892       const APInt &C1V = CI1->getValue();
893       const APInt &C2V = CI2->getValue();
894       switch (Opcode) {
895       default:
896         break;
897       case Instruction::Add:
898         return ConstantInt::get(CI1->getContext(), C1V + C2V);
899       case Instruction::Sub:
900         return ConstantInt::get(CI1->getContext(), C1V - C2V);
901       case Instruction::Mul:
902         return ConstantInt::get(CI1->getContext(), C1V * C2V);
903       case Instruction::UDiv:
904         assert(!CI2->isZero() && "Div by zero handled above");
905         return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V));
906       case Instruction::SDiv:
907         assert(!CI2->isZero() && "Div by zero handled above");
908         if (C2V.isAllOnes() && C1V.isMinSignedValue())
909           return PoisonValue::get(CI1->getType());   // MIN_INT / -1 -> poison
910         return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V));
911       case Instruction::URem:
912         assert(!CI2->isZero() && "Div by zero handled above");
913         return ConstantInt::get(CI1->getContext(), C1V.urem(C2V));
914       case Instruction::SRem:
915         assert(!CI2->isZero() && "Div by zero handled above");
916         if (C2V.isAllOnes() && C1V.isMinSignedValue())
917           return PoisonValue::get(CI1->getType());   // MIN_INT % -1 -> poison
918         return ConstantInt::get(CI1->getContext(), C1V.srem(C2V));
919       case Instruction::And:
920         return ConstantInt::get(CI1->getContext(), C1V & C2V);
921       case Instruction::Or:
922         return ConstantInt::get(CI1->getContext(), C1V | C2V);
923       case Instruction::Xor:
924         return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
925       case Instruction::Shl:
926         if (C2V.ult(C1V.getBitWidth()))
927           return ConstantInt::get(CI1->getContext(), C1V.shl(C2V));
928         return PoisonValue::get(C1->getType()); // too big shift is poison
929       case Instruction::LShr:
930         if (C2V.ult(C1V.getBitWidth()))
931           return ConstantInt::get(CI1->getContext(), C1V.lshr(C2V));
932         return PoisonValue::get(C1->getType()); // too big shift is poison
933       case Instruction::AShr:
934         if (C2V.ult(C1V.getBitWidth()))
935           return ConstantInt::get(CI1->getContext(), C1V.ashr(C2V));
936         return PoisonValue::get(C1->getType()); // too big shift is poison
937       }
938     }
939 
940     switch (Opcode) {
941     case Instruction::SDiv:
942     case Instruction::UDiv:
943     case Instruction::URem:
944     case Instruction::SRem:
945     case Instruction::LShr:
946     case Instruction::AShr:
947     case Instruction::Shl:
948       if (CI1->isZero()) return C1;
949       break;
950     default:
951       break;
952     }
953   } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
954     if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
955       const APFloat &C1V = CFP1->getValueAPF();
956       const APFloat &C2V = CFP2->getValueAPF();
957       APFloat C3V = C1V;  // copy for modification
958       switch (Opcode) {
959       default:
960         break;
961       case Instruction::FAdd:
962         (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
963         return ConstantFP::get(C1->getContext(), C3V);
964       case Instruction::FSub:
965         (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
966         return ConstantFP::get(C1->getContext(), C3V);
967       case Instruction::FMul:
968         (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
969         return ConstantFP::get(C1->getContext(), C3V);
970       case Instruction::FDiv:
971         (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
972         return ConstantFP::get(C1->getContext(), C3V);
973       case Instruction::FRem:
974         (void)C3V.mod(C2V);
975         return ConstantFP::get(C1->getContext(), C3V);
976       }
977     }
978   } else if (auto *VTy = dyn_cast<VectorType>(C1->getType())) {
979     // Fast path for splatted constants.
980     if (Constant *C2Splat = C2->getSplatValue()) {
981       if (Instruction::isIntDivRem(Opcode) && C2Splat->isNullValue())
982         return PoisonValue::get(VTy);
983       if (Constant *C1Splat = C1->getSplatValue()) {
984         Constant *Res =
985             ConstantExpr::isDesirableBinOp(Opcode)
986                 ? ConstantExpr::get(Opcode, C1Splat, C2Splat)
987                 : ConstantFoldBinaryInstruction(Opcode, C1Splat, C2Splat);
988         if (!Res)
989           return nullptr;
990         return ConstantVector::getSplat(VTy->getElementCount(), Res);
991       }
992     }
993 
994     if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
995       // Fold each element and create a vector constant from those constants.
996       SmallVector<Constant*, 16> Result;
997       Type *Ty = IntegerType::get(FVTy->getContext(), 32);
998       for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
999         Constant *ExtractIdx = ConstantInt::get(Ty, i);
1000         Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx);
1001         Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx);
1002 
1003         // If any element of a divisor vector is zero, the whole op is poison.
1004         if (Instruction::isIntDivRem(Opcode) && RHS->isNullValue())
1005           return PoisonValue::get(VTy);
1006 
1007         Constant *Res = ConstantExpr::isDesirableBinOp(Opcode)
1008                             ? ConstantExpr::get(Opcode, LHS, RHS)
1009                             : ConstantFoldBinaryInstruction(Opcode, LHS, RHS);
1010         if (!Res)
1011           return nullptr;
1012         Result.push_back(Res);
1013       }
1014 
1015       return ConstantVector::get(Result);
1016     }
1017   }
1018 
1019   if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1020     // There are many possible foldings we could do here.  We should probably
1021     // at least fold add of a pointer with an integer into the appropriate
1022     // getelementptr.  This will improve alias analysis a bit.
1023 
1024     // Given ((a + b) + c), if (b + c) folds to something interesting, return
1025     // (a + (b + c)).
1026     if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
1027       Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
1028       if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
1029         return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
1030     }
1031   } else if (isa<ConstantExpr>(C2)) {
1032     // If C2 is a constant expr and C1 isn't, flop them around and fold the
1033     // other way if possible.
1034     if (Instruction::isCommutative(Opcode))
1035       return ConstantFoldBinaryInstruction(Opcode, C2, C1);
1036   }
1037 
1038   // i1 can be simplified in many cases.
1039   if (C1->getType()->isIntegerTy(1)) {
1040     switch (Opcode) {
1041     case Instruction::Add:
1042     case Instruction::Sub:
1043       return ConstantExpr::getXor(C1, C2);
1044     case Instruction::Shl:
1045     case Instruction::LShr:
1046     case Instruction::AShr:
1047       // We can assume that C2 == 0.  If it were one the result would be
1048       // undefined because the shift value is as large as the bitwidth.
1049       return C1;
1050     case Instruction::SDiv:
1051     case Instruction::UDiv:
1052       // We can assume that C2 == 1.  If it were zero the result would be
1053       // undefined through division by zero.
1054       return C1;
1055     case Instruction::URem:
1056     case Instruction::SRem:
1057       // We can assume that C2 == 1.  If it were zero the result would be
1058       // undefined through division by zero.
1059       return ConstantInt::getFalse(C1->getContext());
1060     default:
1061       break;
1062     }
1063   }
1064 
1065   // We don't know how to fold this.
1066   return nullptr;
1067 }
1068 
areGlobalsPotentiallyEqual(const GlobalValue * GV1,const GlobalValue * GV2)1069 static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
1070                                                       const GlobalValue *GV2) {
1071   auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
1072     if (GV->isInterposable() || GV->hasGlobalUnnamedAddr())
1073       return true;
1074     if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
1075       Type *Ty = GVar->getValueType();
1076       // A global with opaque type might end up being zero sized.
1077       if (!Ty->isSized())
1078         return true;
1079       // A global with an empty type might lie at the address of any other
1080       // global.
1081       if (Ty->isEmptyTy())
1082         return true;
1083     }
1084     return false;
1085   };
1086   // Don't try to decide equality of aliases.
1087   if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
1088     if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
1089       return ICmpInst::ICMP_NE;
1090   return ICmpInst::BAD_ICMP_PREDICATE;
1091 }
1092 
1093 /// This function determines if there is anything we can decide about the two
1094 /// constants provided. This doesn't need to handle simple things like integer
1095 /// comparisons, but should instead handle ConstantExprs and GlobalValues.
1096 /// If we can determine that the two constants have a particular relation to
1097 /// each other, we should return the corresponding ICmp predicate, otherwise
1098 /// return ICmpInst::BAD_ICMP_PREDICATE.
evaluateICmpRelation(Constant * V1,Constant * V2)1099 static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2) {
1100   assert(V1->getType() == V2->getType() &&
1101          "Cannot compare different types of values!");
1102   if (V1 == V2) return ICmpInst::ICMP_EQ;
1103 
1104   // The following folds only apply to pointers.
1105   if (!V1->getType()->isPointerTy())
1106     return ICmpInst::BAD_ICMP_PREDICATE;
1107 
1108   // To simplify this code we canonicalize the relation so that the first
1109   // operand is always the most "complex" of the two.  We consider simple
1110   // constants (like ConstantPointerNull) to be the simplest, followed by
1111   // BlockAddress, GlobalValues, and ConstantExpr's (the most complex).
1112   auto GetComplexity = [](Constant *V) {
1113     if (isa<ConstantExpr>(V))
1114       return 3;
1115     if (isa<GlobalValue>(V))
1116       return 2;
1117     if (isa<BlockAddress>(V))
1118       return 1;
1119     return 0;
1120   };
1121   if (GetComplexity(V1) < GetComplexity(V2)) {
1122     ICmpInst::Predicate SwappedRelation = evaluateICmpRelation(V2, V1);
1123     if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1124       return ICmpInst::getSwappedPredicate(SwappedRelation);
1125     return ICmpInst::BAD_ICMP_PREDICATE;
1126   }
1127 
1128   if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1129     // Now we know that the RHS is a BlockAddress or simple constant.
1130     if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1131       // Block address in another function can't equal this one, but block
1132       // addresses in the current function might be the same if blocks are
1133       // empty.
1134       if (BA2->getFunction() != BA->getFunction())
1135         return ICmpInst::ICMP_NE;
1136     } else if (isa<ConstantPointerNull>(V2)) {
1137       return ICmpInst::ICMP_NE;
1138     }
1139   } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1140     // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1141     // constant.
1142     if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1143       return areGlobalsPotentiallyEqual(GV, GV2);
1144     } else if (isa<BlockAddress>(V2)) {
1145       return ICmpInst::ICMP_NE; // Globals never equal labels.
1146     } else if (isa<ConstantPointerNull>(V2)) {
1147       // GlobalVals can never be null unless they have external weak linkage.
1148       // We don't try to evaluate aliases here.
1149       // NOTE: We should not be doing this constant folding if null pointer
1150       // is considered valid for the function. But currently there is no way to
1151       // query it from the Constant type.
1152       if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1153           !NullPointerIsDefined(nullptr /* F */,
1154                                 GV->getType()->getAddressSpace()))
1155         return ICmpInst::ICMP_UGT;
1156     }
1157   } else {
1158     // Ok, the LHS is known to be a constantexpr.  The RHS can be any of a
1159     // constantexpr, a global, block address, or a simple constant.
1160     ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1161     Constant *CE1Op0 = CE1->getOperand(0);
1162 
1163     switch (CE1->getOpcode()) {
1164     case Instruction::GetElementPtr: {
1165       GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
1166       // Ok, since this is a getelementptr, we know that the constant has a
1167       // pointer type.  Check the various cases.
1168       if (isa<ConstantPointerNull>(V2)) {
1169         // If we are comparing a GEP to a null pointer, check to see if the base
1170         // of the GEP equals the null pointer.
1171         if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1172           // If its not weak linkage, the GVal must have a non-zero address
1173           // so the result is greater-than
1174           if (!GV->hasExternalWeakLinkage() && CE1GEP->isInBounds())
1175             return ICmpInst::ICMP_UGT;
1176         }
1177       } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1178         if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1179           if (GV != GV2) {
1180             if (CE1GEP->hasAllZeroIndices())
1181               return areGlobalsPotentiallyEqual(GV, GV2);
1182             return ICmpInst::BAD_ICMP_PREDICATE;
1183           }
1184         }
1185       } else if (const auto *CE2GEP = dyn_cast<GEPOperator>(V2)) {
1186         // By far the most common case to handle is when the base pointers are
1187         // obviously to the same global.
1188         const Constant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand());
1189         if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1190           // Don't know relative ordering, but check for inequality.
1191           if (CE1Op0 != CE2Op0) {
1192             if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
1193               return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0),
1194                                                 cast<GlobalValue>(CE2Op0));
1195             return ICmpInst::BAD_ICMP_PREDICATE;
1196           }
1197         }
1198       }
1199       break;
1200     }
1201     default:
1202       break;
1203     }
1204   }
1205 
1206   return ICmpInst::BAD_ICMP_PREDICATE;
1207 }
1208 
ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,Constant * C1,Constant * C2)1209 Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
1210                                                Constant *C1, Constant *C2) {
1211   Type *ResultTy;
1212   if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
1213     ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
1214                                VT->getElementCount());
1215   else
1216     ResultTy = Type::getInt1Ty(C1->getContext());
1217 
1218   // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
1219   if (Predicate == FCmpInst::FCMP_FALSE)
1220     return Constant::getNullValue(ResultTy);
1221 
1222   if (Predicate == FCmpInst::FCMP_TRUE)
1223     return Constant::getAllOnesValue(ResultTy);
1224 
1225   // Handle some degenerate cases first
1226   if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1227     return PoisonValue::get(ResultTy);
1228 
1229   if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1230     bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
1231     // For EQ and NE, we can always pick a value for the undef to make the
1232     // predicate pass or fail, so we can return undef.
1233     // Also, if both operands are undef, we can return undef for int comparison.
1234     if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2))
1235       return UndefValue::get(ResultTy);
1236 
1237     // Otherwise, for integer compare, pick the same value as the non-undef
1238     // operand, and fold it to true or false.
1239     if (isIntegerPredicate)
1240       return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(Predicate));
1241 
1242     // Choosing NaN for the undef will always make unordered comparison succeed
1243     // and ordered comparison fails.
1244     return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate));
1245   }
1246 
1247   if (C2->isNullValue()) {
1248     // The caller is expected to commute the operands if the constant expression
1249     // is C2.
1250     // C1 >= 0 --> true
1251     if (Predicate == ICmpInst::ICMP_UGE)
1252       return Constant::getAllOnesValue(ResultTy);
1253     // C1 < 0 --> false
1254     if (Predicate == ICmpInst::ICMP_ULT)
1255       return Constant::getNullValue(ResultTy);
1256   }
1257 
1258   // If the comparison is a comparison between two i1's, simplify it.
1259   if (C1->getType()->isIntegerTy(1)) {
1260     switch (Predicate) {
1261     case ICmpInst::ICMP_EQ:
1262       if (isa<ConstantInt>(C2))
1263         return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
1264       return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
1265     case ICmpInst::ICMP_NE:
1266       return ConstantExpr::getXor(C1, C2);
1267     default:
1268       break;
1269     }
1270   }
1271 
1272   if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1273     const APInt &V1 = cast<ConstantInt>(C1)->getValue();
1274     const APInt &V2 = cast<ConstantInt>(C2)->getValue();
1275     return ConstantInt::get(ResultTy, ICmpInst::compare(V1, V2, Predicate));
1276   } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1277     const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
1278     const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
1279     return ConstantInt::get(ResultTy, FCmpInst::compare(C1V, C2V, Predicate));
1280   } else if (auto *C1VTy = dyn_cast<VectorType>(C1->getType())) {
1281 
1282     // Fast path for splatted constants.
1283     if (Constant *C1Splat = C1->getSplatValue())
1284       if (Constant *C2Splat = C2->getSplatValue())
1285         return ConstantVector::getSplat(
1286             C1VTy->getElementCount(),
1287             ConstantExpr::getCompare(Predicate, C1Splat, C2Splat));
1288 
1289     // Do not iterate on scalable vector. The number of elements is unknown at
1290     // compile-time.
1291     if (isa<ScalableVectorType>(C1VTy))
1292       return nullptr;
1293 
1294     // If we can constant fold the comparison of each element, constant fold
1295     // the whole vector comparison.
1296     SmallVector<Constant*, 4> ResElts;
1297     Type *Ty = IntegerType::get(C1->getContext(), 32);
1298     // Compare the elements, producing an i1 result or constant expr.
1299     for (unsigned I = 0, E = C1VTy->getElementCount().getKnownMinValue();
1300          I != E; ++I) {
1301       Constant *C1E =
1302           ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, I));
1303       Constant *C2E =
1304           ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, I));
1305 
1306       ResElts.push_back(ConstantExpr::getCompare(Predicate, C1E, C2E));
1307     }
1308 
1309     return ConstantVector::get(ResElts);
1310   }
1311 
1312   if (C1->getType()->isFPOrFPVectorTy()) {
1313     if (C1 == C2) {
1314       // We know that C1 == C2 || isUnordered(C1, C2).
1315       if (Predicate == FCmpInst::FCMP_ONE)
1316         return ConstantInt::getFalse(ResultTy);
1317       else if (Predicate == FCmpInst::FCMP_UEQ)
1318         return ConstantInt::getTrue(ResultTy);
1319     }
1320   } else {
1321     // Evaluate the relation between the two constants, per the predicate.
1322     int Result = -1;  // -1 = unknown, 0 = known false, 1 = known true.
1323     switch (evaluateICmpRelation(C1, C2)) {
1324     default: llvm_unreachable("Unknown relational!");
1325     case ICmpInst::BAD_ICMP_PREDICATE:
1326       break;  // Couldn't determine anything about these constants.
1327     case ICmpInst::ICMP_EQ:   // We know the constants are equal!
1328       // If we know the constants are equal, we can decide the result of this
1329       // computation precisely.
1330       Result = ICmpInst::isTrueWhenEqual(Predicate);
1331       break;
1332     case ICmpInst::ICMP_ULT:
1333       switch (Predicate) {
1334       case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
1335         Result = 1; break;
1336       case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
1337         Result = 0; break;
1338       default:
1339         break;
1340       }
1341       break;
1342     case ICmpInst::ICMP_SLT:
1343       switch (Predicate) {
1344       case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
1345         Result = 1; break;
1346       case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
1347         Result = 0; break;
1348       default:
1349         break;
1350       }
1351       break;
1352     case ICmpInst::ICMP_UGT:
1353       switch (Predicate) {
1354       case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
1355         Result = 1; break;
1356       case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
1357         Result = 0; break;
1358       default:
1359         break;
1360       }
1361       break;
1362     case ICmpInst::ICMP_SGT:
1363       switch (Predicate) {
1364       case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
1365         Result = 1; break;
1366       case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
1367         Result = 0; break;
1368       default:
1369         break;
1370       }
1371       break;
1372     case ICmpInst::ICMP_ULE:
1373       if (Predicate == ICmpInst::ICMP_UGT)
1374         Result = 0;
1375       if (Predicate == ICmpInst::ICMP_ULT || Predicate == ICmpInst::ICMP_ULE)
1376         Result = 1;
1377       break;
1378     case ICmpInst::ICMP_SLE:
1379       if (Predicate == ICmpInst::ICMP_SGT)
1380         Result = 0;
1381       if (Predicate == ICmpInst::ICMP_SLT || Predicate == ICmpInst::ICMP_SLE)
1382         Result = 1;
1383       break;
1384     case ICmpInst::ICMP_UGE:
1385       if (Predicate == ICmpInst::ICMP_ULT)
1386         Result = 0;
1387       if (Predicate == ICmpInst::ICMP_UGT || Predicate == ICmpInst::ICMP_UGE)
1388         Result = 1;
1389       break;
1390     case ICmpInst::ICMP_SGE:
1391       if (Predicate == ICmpInst::ICMP_SLT)
1392         Result = 0;
1393       if (Predicate == ICmpInst::ICMP_SGT || Predicate == ICmpInst::ICMP_SGE)
1394         Result = 1;
1395       break;
1396     case ICmpInst::ICMP_NE:
1397       if (Predicate == ICmpInst::ICMP_EQ)
1398         Result = 0;
1399       if (Predicate == ICmpInst::ICMP_NE)
1400         Result = 1;
1401       break;
1402     }
1403 
1404     // If we evaluated the result, return it now.
1405     if (Result != -1)
1406       return ConstantInt::get(ResultTy, Result);
1407 
1408     if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
1409         (C1->isNullValue() && !C2->isNullValue())) {
1410       // If C2 is a constant expr and C1 isn't, flip them around and fold the
1411       // other way if possible.
1412       // Also, if C1 is null and C2 isn't, flip them around.
1413       Predicate = ICmpInst::getSwappedPredicate(Predicate);
1414       return ConstantExpr::getICmp(Predicate, C2, C1);
1415     }
1416   }
1417   return nullptr;
1418 }
1419 
1420 /// Test whether the given sequence of *normalized* indices is "inbounds".
1421 template<typename IndexTy>
isInBoundsIndices(ArrayRef<IndexTy> Idxs)1422 static bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) {
1423   // No indices means nothing that could be out of bounds.
1424   if (Idxs.empty()) return true;
1425 
1426   // If the first index is zero, it's in bounds.
1427   if (cast<Constant>(Idxs[0])->isNullValue()) return true;
1428 
1429   // If the first index is one and all the rest are zero, it's in bounds,
1430   // by the one-past-the-end rule.
1431   if (auto *CI = dyn_cast<ConstantInt>(Idxs[0])) {
1432     if (!CI->isOne())
1433       return false;
1434   } else {
1435     auto *CV = cast<ConstantDataVector>(Idxs[0]);
1436     CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
1437     if (!CI || !CI->isOne())
1438       return false;
1439   }
1440 
1441   for (unsigned i = 1, e = Idxs.size(); i != e; ++i)
1442     if (!cast<Constant>(Idxs[i])->isNullValue())
1443       return false;
1444   return true;
1445 }
1446 
1447 /// Test whether a given ConstantInt is in-range for a SequentialType.
isIndexInRangeOfArrayType(uint64_t NumElements,const ConstantInt * CI)1448 static bool isIndexInRangeOfArrayType(uint64_t NumElements,
1449                                       const ConstantInt *CI) {
1450   // We cannot bounds check the index if it doesn't fit in an int64_t.
1451   if (CI->getValue().getSignificantBits() > 64)
1452     return false;
1453 
1454   // A negative index or an index past the end of our sequential type is
1455   // considered out-of-range.
1456   int64_t IndexVal = CI->getSExtValue();
1457   if (IndexVal < 0 || (IndexVal != 0 && (uint64_t)IndexVal >= NumElements))
1458     return false;
1459 
1460   // Otherwise, it is in-range.
1461   return true;
1462 }
1463 
1464 // Combine Indices - If the source pointer to this getelementptr instruction
1465 // is a getelementptr instruction, combine the indices of the two
1466 // getelementptr instructions into a single instruction.
foldGEPOfGEP(GEPOperator * GEP,Type * PointeeTy,bool InBounds,ArrayRef<Value * > Idxs)1467 static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds,
1468                               ArrayRef<Value *> Idxs) {
1469   if (PointeeTy != GEP->getResultElementType())
1470     return nullptr;
1471 
1472   Constant *Idx0 = cast<Constant>(Idxs[0]);
1473   if (Idx0->isNullValue()) {
1474     // Handle the simple case of a zero index.
1475     SmallVector<Value*, 16> NewIndices;
1476     NewIndices.reserve(Idxs.size() + GEP->getNumIndices());
1477     NewIndices.append(GEP->idx_begin(), GEP->idx_end());
1478     NewIndices.append(Idxs.begin() + 1, Idxs.end());
1479     return ConstantExpr::getGetElementPtr(
1480         GEP->getSourceElementType(), cast<Constant>(GEP->getPointerOperand()),
1481         NewIndices, InBounds && GEP->isInBounds(), GEP->getInRangeIndex());
1482   }
1483 
1484   gep_type_iterator LastI = gep_type_end(GEP);
1485   for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
1486        I != E; ++I)
1487     LastI = I;
1488 
1489   // We can't combine GEPs if the last index is a struct type.
1490   if (!LastI.isSequential())
1491     return nullptr;
1492   // We could perform the transform with non-constant index, but prefer leaving
1493   // it as GEP of GEP rather than GEP of add for now.
1494   ConstantInt *CI = dyn_cast<ConstantInt>(Idx0);
1495   if (!CI)
1496     return nullptr;
1497 
1498   // TODO: This code may be extended to handle vectors as well.
1499   auto *LastIdx = cast<Constant>(GEP->getOperand(GEP->getNumOperands()-1));
1500   Type *LastIdxTy = LastIdx->getType();
1501   if (LastIdxTy->isVectorTy())
1502     return nullptr;
1503 
1504   SmallVector<Value*, 16> NewIndices;
1505   NewIndices.reserve(Idxs.size() + GEP->getNumIndices());
1506   NewIndices.append(GEP->idx_begin(), GEP->idx_end() - 1);
1507 
1508   // Add the last index of the source with the first index of the new GEP.
1509   // Make sure to handle the case when they are actually different types.
1510   if (LastIdxTy != Idx0->getType()) {
1511     unsigned CommonExtendedWidth =
1512         std::max(LastIdxTy->getIntegerBitWidth(),
1513                  Idx0->getType()->getIntegerBitWidth());
1514     CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
1515 
1516     Type *CommonTy =
1517         Type::getIntNTy(LastIdxTy->getContext(), CommonExtendedWidth);
1518     if (Idx0->getType() != CommonTy)
1519       Idx0 = ConstantFoldCastInstruction(Instruction::SExt, Idx0, CommonTy);
1520     if (LastIdx->getType() != CommonTy)
1521       LastIdx =
1522           ConstantFoldCastInstruction(Instruction::SExt, LastIdx, CommonTy);
1523     if (!Idx0 || !LastIdx)
1524       return nullptr;
1525   }
1526 
1527   NewIndices.push_back(ConstantExpr::get(Instruction::Add, Idx0, LastIdx));
1528   NewIndices.append(Idxs.begin() + 1, Idxs.end());
1529 
1530   // The combined GEP normally inherits its index inrange attribute from
1531   // the inner GEP, but if the inner GEP's last index was adjusted by the
1532   // outer GEP, any inbounds attribute on that index is invalidated.
1533   std::optional<unsigned> IRIndex = GEP->getInRangeIndex();
1534   if (IRIndex && *IRIndex == GEP->getNumIndices() - 1)
1535     IRIndex = std::nullopt;
1536 
1537   return ConstantExpr::getGetElementPtr(
1538       GEP->getSourceElementType(), cast<Constant>(GEP->getPointerOperand()),
1539       NewIndices, InBounds && GEP->isInBounds(), IRIndex);
1540 }
1541 
ConstantFoldGetElementPtr(Type * PointeeTy,Constant * C,bool InBounds,std::optional<unsigned> InRangeIndex,ArrayRef<Value * > Idxs)1542 Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
1543                                           bool InBounds,
1544                                           std::optional<unsigned> InRangeIndex,
1545                                           ArrayRef<Value *> Idxs) {
1546   if (Idxs.empty()) return C;
1547 
1548   Type *GEPTy = GetElementPtrInst::getGEPReturnType(
1549       C, ArrayRef((Value *const *)Idxs.data(), Idxs.size()));
1550 
1551   if (isa<PoisonValue>(C))
1552     return PoisonValue::get(GEPTy);
1553 
1554   if (isa<UndefValue>(C))
1555     // If inbounds, we can choose an out-of-bounds pointer as a base pointer.
1556     return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy);
1557 
1558   auto IsNoOp = [&]() {
1559     // Avoid losing inrange information.
1560     if (InRangeIndex)
1561       return false;
1562 
1563     return all_of(Idxs, [](Value *Idx) {
1564       Constant *IdxC = cast<Constant>(Idx);
1565       return IdxC->isNullValue() || isa<UndefValue>(IdxC);
1566     });
1567   };
1568   if (IsNoOp())
1569     return GEPTy->isVectorTy() && !C->getType()->isVectorTy()
1570                ? ConstantVector::getSplat(
1571                      cast<VectorType>(GEPTy)->getElementCount(), C)
1572                : C;
1573 
1574   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
1575     if (auto *GEP = dyn_cast<GEPOperator>(CE))
1576       if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs))
1577         return C;
1578 
1579   // Check to see if any array indices are not within the corresponding
1580   // notional array or vector bounds. If so, try to determine if they can be
1581   // factored out into preceding dimensions.
1582   SmallVector<Constant *, 8> NewIdxs;
1583   Type *Ty = PointeeTy;
1584   Type *Prev = C->getType();
1585   auto GEPIter = gep_type_begin(PointeeTy, Idxs);
1586   bool Unknown =
1587       !isa<ConstantInt>(Idxs[0]) && !isa<ConstantDataVector>(Idxs[0]);
1588   for (unsigned i = 1, e = Idxs.size(); i != e;
1589        Prev = Ty, Ty = (++GEPIter).getIndexedType(), ++i) {
1590     if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) {
1591       // We don't know if it's in range or not.
1592       Unknown = true;
1593       continue;
1594     }
1595     if (!isa<ConstantInt>(Idxs[i - 1]) && !isa<ConstantDataVector>(Idxs[i - 1]))
1596       // Skip if the type of the previous index is not supported.
1597       continue;
1598     if (InRangeIndex && i == *InRangeIndex + 1) {
1599       // If an index is marked inrange, we cannot apply this canonicalization to
1600       // the following index, as that will cause the inrange index to point to
1601       // the wrong element.
1602       continue;
1603     }
1604     if (isa<StructType>(Ty)) {
1605       // The verify makes sure that GEPs into a struct are in range.
1606       continue;
1607     }
1608     if (isa<VectorType>(Ty)) {
1609       // There can be awkward padding in after a non-power of two vector.
1610       Unknown = true;
1611       continue;
1612     }
1613     auto *STy = cast<ArrayType>(Ty);
1614     if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
1615       if (isIndexInRangeOfArrayType(STy->getNumElements(), CI))
1616         // It's in range, skip to the next index.
1617         continue;
1618       if (CI->isNegative()) {
1619         // It's out of range and negative, don't try to factor it.
1620         Unknown = true;
1621         continue;
1622       }
1623     } else {
1624       auto *CV = cast<ConstantDataVector>(Idxs[i]);
1625       bool InRange = true;
1626       for (unsigned I = 0, E = CV->getNumElements(); I != E; ++I) {
1627         auto *CI = cast<ConstantInt>(CV->getElementAsConstant(I));
1628         InRange &= isIndexInRangeOfArrayType(STy->getNumElements(), CI);
1629         if (CI->isNegative()) {
1630           Unknown = true;
1631           break;
1632         }
1633       }
1634       if (InRange || Unknown)
1635         // It's in range, skip to the next index.
1636         // It's out of range and negative, don't try to factor it.
1637         continue;
1638     }
1639     if (isa<StructType>(Prev)) {
1640       // It's out of range, but the prior dimension is a struct
1641       // so we can't do anything about it.
1642       Unknown = true;
1643       continue;
1644     }
1645 
1646     // Determine the number of elements in our sequential type.
1647     uint64_t NumElements = STy->getArrayNumElements();
1648     if (!NumElements) {
1649       Unknown = true;
1650       continue;
1651     }
1652 
1653     // It's out of range, but we can factor it into the prior
1654     // dimension.
1655     NewIdxs.resize(Idxs.size());
1656 
1657     // Expand the current index or the previous index to a vector from a scalar
1658     // if necessary.
1659     Constant *CurrIdx = cast<Constant>(Idxs[i]);
1660     auto *PrevIdx =
1661         NewIdxs[i - 1] ? NewIdxs[i - 1] : cast<Constant>(Idxs[i - 1]);
1662     bool IsCurrIdxVector = CurrIdx->getType()->isVectorTy();
1663     bool IsPrevIdxVector = PrevIdx->getType()->isVectorTy();
1664     bool UseVector = IsCurrIdxVector || IsPrevIdxVector;
1665 
1666     if (!IsCurrIdxVector && IsPrevIdxVector)
1667       CurrIdx = ConstantDataVector::getSplat(
1668           cast<FixedVectorType>(PrevIdx->getType())->getNumElements(), CurrIdx);
1669 
1670     if (!IsPrevIdxVector && IsCurrIdxVector)
1671       PrevIdx = ConstantDataVector::getSplat(
1672           cast<FixedVectorType>(CurrIdx->getType())->getNumElements(), PrevIdx);
1673 
1674     Constant *Factor =
1675         ConstantInt::get(CurrIdx->getType()->getScalarType(), NumElements);
1676     if (UseVector)
1677       Factor = ConstantDataVector::getSplat(
1678           IsPrevIdxVector
1679               ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements()
1680               : cast<FixedVectorType>(CurrIdx->getType())->getNumElements(),
1681           Factor);
1682 
1683     NewIdxs[i] =
1684         ConstantFoldBinaryInstruction(Instruction::SRem, CurrIdx, Factor);
1685 
1686     Constant *Div =
1687         ConstantFoldBinaryInstruction(Instruction::SDiv, CurrIdx, Factor);
1688 
1689     // We're working on either ConstantInt or vectors of ConstantInt,
1690     // so these should always fold.
1691     assert(NewIdxs[i] != nullptr && Div != nullptr && "Should have folded");
1692 
1693     unsigned CommonExtendedWidth =
1694         std::max(PrevIdx->getType()->getScalarSizeInBits(),
1695                  Div->getType()->getScalarSizeInBits());
1696     CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
1697 
1698     // Before adding, extend both operands to i64 to avoid
1699     // overflow trouble.
1700     Type *ExtendedTy = Type::getIntNTy(Div->getContext(), CommonExtendedWidth);
1701     if (UseVector)
1702       ExtendedTy = FixedVectorType::get(
1703           ExtendedTy,
1704           IsPrevIdxVector
1705               ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements()
1706               : cast<FixedVectorType>(CurrIdx->getType())->getNumElements());
1707 
1708     if (!PrevIdx->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
1709       PrevIdx =
1710           ConstantFoldCastInstruction(Instruction::SExt, PrevIdx, ExtendedTy);
1711 
1712     if (!Div->getType()->isIntOrIntVectorTy(CommonExtendedWidth))
1713       Div = ConstantFoldCastInstruction(Instruction::SExt, Div, ExtendedTy);
1714 
1715     assert(PrevIdx && Div && "Should have folded");
1716     NewIdxs[i - 1] = ConstantExpr::getAdd(PrevIdx, Div);
1717   }
1718 
1719   // If we did any factoring, start over with the adjusted indices.
1720   if (!NewIdxs.empty()) {
1721     for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
1722       if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
1723     return ConstantExpr::getGetElementPtr(PointeeTy, C, NewIdxs, InBounds,
1724                                           InRangeIndex);
1725   }
1726 
1727   // If all indices are known integers and normalized, we can do a simple
1728   // check for the "inbounds" property.
1729   if (!Unknown && !InBounds)
1730     if (auto *GV = dyn_cast<GlobalVariable>(C))
1731       if (!GV->hasExternalWeakLinkage() && GV->getValueType() == PointeeTy &&
1732           isInBoundsIndices(Idxs))
1733         return ConstantExpr::getGetElementPtr(PointeeTy, C, Idxs,
1734                                               /*InBounds=*/true, InRangeIndex);
1735 
1736   return nullptr;
1737 }
1738