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