1*9880d681SAndroid Build Coastguard Worker //===-- ConstantFolding.cpp - Fold instructions into constants ------------===//
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 defines routines for folding instructions into constants.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker // Also, to supplement the basic IR ConstantExpr simplifications,
13*9880d681SAndroid Build Coastguard Worker // this file defines some additional folding routines that can make use of
14*9880d681SAndroid Build Coastguard Worker // DataLayout information. These functions cannot go in IR due to library
15*9880d681SAndroid Build Coastguard Worker // dependency issues.
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ConstantFolding.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringMap.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Config/config.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/GetElementPtrTypeIterator.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/GlobalVariable.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Intrinsics.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Operator.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MathExtras.h"
38*9880d681SAndroid Build Coastguard Worker #include <cassert>
39*9880d681SAndroid Build Coastguard Worker #include <cerrno>
40*9880d681SAndroid Build Coastguard Worker #include <cfenv>
41*9880d681SAndroid Build Coastguard Worker #include <cmath>
42*9880d681SAndroid Build Coastguard Worker #include <limits>
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker using namespace llvm;
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker namespace {
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
49*9880d681SAndroid Build Coastguard Worker // Constant Folding internal helper functions
50*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker /// Constant fold bitcast, symbolically evaluating it with DataLayout.
53*9880d681SAndroid Build Coastguard Worker /// This always returns a non-null constant, but it may be a
54*9880d681SAndroid Build Coastguard Worker /// ConstantExpr if unfoldable.
FoldBitCast(Constant * C,Type * DestTy,const DataLayout & DL)55*9880d681SAndroid Build Coastguard Worker Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
56*9880d681SAndroid Build Coastguard Worker // Catch the obvious splat cases.
57*9880d681SAndroid Build Coastguard Worker if (C->isNullValue() && !DestTy->isX86_MMXTy())
58*9880d681SAndroid Build Coastguard Worker return Constant::getNullValue(DestTy);
59*9880d681SAndroid Build Coastguard Worker if (C->isAllOnesValue() && !DestTy->isX86_MMXTy() &&
60*9880d681SAndroid Build Coastguard Worker !DestTy->isPtrOrPtrVectorTy()) // Don't get ones for ptr types!
61*9880d681SAndroid Build Coastguard Worker return Constant::getAllOnesValue(DestTy);
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker // Handle a vector->integer cast.
64*9880d681SAndroid Build Coastguard Worker if (auto *IT = dyn_cast<IntegerType>(DestTy)) {
65*9880d681SAndroid Build Coastguard Worker auto *VTy = dyn_cast<VectorType>(C->getType());
66*9880d681SAndroid Build Coastguard Worker if (!VTy)
67*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getBitCast(C, DestTy);
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker unsigned NumSrcElts = VTy->getNumElements();
70*9880d681SAndroid Build Coastguard Worker Type *SrcEltTy = VTy->getElementType();
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker // If the vector is a vector of floating point, convert it to vector of int
73*9880d681SAndroid Build Coastguard Worker // to simplify things.
74*9880d681SAndroid Build Coastguard Worker if (SrcEltTy->isFloatingPointTy()) {
75*9880d681SAndroid Build Coastguard Worker unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
76*9880d681SAndroid Build Coastguard Worker Type *SrcIVTy =
77*9880d681SAndroid Build Coastguard Worker VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElts);
78*9880d681SAndroid Build Coastguard Worker // Ask IR to do the conversion now that #elts line up.
79*9880d681SAndroid Build Coastguard Worker C = ConstantExpr::getBitCast(C, SrcIVTy);
80*9880d681SAndroid Build Coastguard Worker }
81*9880d681SAndroid Build Coastguard Worker
82*9880d681SAndroid Build Coastguard Worker // Now that we know that the input value is a vector of integers, just shift
83*9880d681SAndroid Build Coastguard Worker // and insert them into our result.
84*9880d681SAndroid Build Coastguard Worker unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy);
85*9880d681SAndroid Build Coastguard Worker APInt Result(IT->getBitWidth(), 0);
86*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumSrcElts; ++i) {
87*9880d681SAndroid Build Coastguard Worker Constant *Element;
88*9880d681SAndroid Build Coastguard Worker if (DL.isLittleEndian())
89*9880d681SAndroid Build Coastguard Worker Element = C->getAggregateElement(NumSrcElts-i-1);
90*9880d681SAndroid Build Coastguard Worker else
91*9880d681SAndroid Build Coastguard Worker Element = C->getAggregateElement(i);
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
94*9880d681SAndroid Build Coastguard Worker if (!ElementCI)
95*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getBitCast(C, DestTy);
96*9880d681SAndroid Build Coastguard Worker
97*9880d681SAndroid Build Coastguard Worker Result <<= BitShift;
98*9880d681SAndroid Build Coastguard Worker Result |= ElementCI->getValue().zextOrSelf(IT->getBitWidth());
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(IT, Result);
102*9880d681SAndroid Build Coastguard Worker }
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker // The code below only handles casts to vectors currently.
105*9880d681SAndroid Build Coastguard Worker auto *DestVTy = dyn_cast<VectorType>(DestTy);
106*9880d681SAndroid Build Coastguard Worker if (!DestVTy)
107*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getBitCast(C, DestTy);
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
110*9880d681SAndroid Build Coastguard Worker // vector so the code below can handle it uniformly.
111*9880d681SAndroid Build Coastguard Worker if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) {
112*9880d681SAndroid Build Coastguard Worker Constant *Ops = C; // don't take the address of C!
113*9880d681SAndroid Build Coastguard Worker return FoldBitCast(ConstantVector::get(Ops), DestTy, DL);
114*9880d681SAndroid Build Coastguard Worker }
115*9880d681SAndroid Build Coastguard Worker
116*9880d681SAndroid Build Coastguard Worker // If this is a bitcast from constant vector -> vector, fold it.
117*9880d681SAndroid Build Coastguard Worker if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
118*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getBitCast(C, DestTy);
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Worker // If the element types match, IR can fold it.
121*9880d681SAndroid Build Coastguard Worker unsigned NumDstElt = DestVTy->getNumElements();
122*9880d681SAndroid Build Coastguard Worker unsigned NumSrcElt = C->getType()->getVectorNumElements();
123*9880d681SAndroid Build Coastguard Worker if (NumDstElt == NumSrcElt)
124*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getBitCast(C, DestTy);
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard Worker Type *SrcEltTy = C->getType()->getVectorElementType();
127*9880d681SAndroid Build Coastguard Worker Type *DstEltTy = DestVTy->getElementType();
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker // Otherwise, we're changing the number of elements in a vector, which
130*9880d681SAndroid Build Coastguard Worker // requires endianness information to do the right thing. For example,
131*9880d681SAndroid Build Coastguard Worker // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
132*9880d681SAndroid Build Coastguard Worker // folds to (little endian):
133*9880d681SAndroid Build Coastguard Worker // <4 x i32> <i32 0, i32 0, i32 1, i32 0>
134*9880d681SAndroid Build Coastguard Worker // and to (big endian):
135*9880d681SAndroid Build Coastguard Worker // <4 x i32> <i32 0, i32 0, i32 0, i32 1>
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker // First thing is first. We only want to think about integer here, so if
138*9880d681SAndroid Build Coastguard Worker // we have something in FP form, recast it as integer.
139*9880d681SAndroid Build Coastguard Worker if (DstEltTy->isFloatingPointTy()) {
140*9880d681SAndroid Build Coastguard Worker // Fold to an vector of integers with same size as our FP type.
141*9880d681SAndroid Build Coastguard Worker unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
142*9880d681SAndroid Build Coastguard Worker Type *DestIVTy =
143*9880d681SAndroid Build Coastguard Worker VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt);
144*9880d681SAndroid Build Coastguard Worker // Recursively handle this integer conversion, if possible.
145*9880d681SAndroid Build Coastguard Worker C = FoldBitCast(C, DestIVTy, DL);
146*9880d681SAndroid Build Coastguard Worker
147*9880d681SAndroid Build Coastguard Worker // Finally, IR can handle this now that #elts line up.
148*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getBitCast(C, DestTy);
149*9880d681SAndroid Build Coastguard Worker }
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Worker // Okay, we know the destination is integer, if the input is FP, convert
152*9880d681SAndroid Build Coastguard Worker // it to integer first.
153*9880d681SAndroid Build Coastguard Worker if (SrcEltTy->isFloatingPointTy()) {
154*9880d681SAndroid Build Coastguard Worker unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
155*9880d681SAndroid Build Coastguard Worker Type *SrcIVTy =
156*9880d681SAndroid Build Coastguard Worker VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
157*9880d681SAndroid Build Coastguard Worker // Ask IR to do the conversion now that #elts line up.
158*9880d681SAndroid Build Coastguard Worker C = ConstantExpr::getBitCast(C, SrcIVTy);
159*9880d681SAndroid Build Coastguard Worker // If IR wasn't able to fold it, bail out.
160*9880d681SAndroid Build Coastguard Worker if (!isa<ConstantVector>(C) && // FIXME: Remove ConstantVector.
161*9880d681SAndroid Build Coastguard Worker !isa<ConstantDataVector>(C))
162*9880d681SAndroid Build Coastguard Worker return C;
163*9880d681SAndroid Build Coastguard Worker }
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker // Now we know that the input and output vectors are both integer vectors
166*9880d681SAndroid Build Coastguard Worker // of the same size, and that their #elements is not the same. Do the
167*9880d681SAndroid Build Coastguard Worker // conversion here, which depends on whether the input or output has
168*9880d681SAndroid Build Coastguard Worker // more elements.
169*9880d681SAndroid Build Coastguard Worker bool isLittleEndian = DL.isLittleEndian();
170*9880d681SAndroid Build Coastguard Worker
171*9880d681SAndroid Build Coastguard Worker SmallVector<Constant*, 32> Result;
172*9880d681SAndroid Build Coastguard Worker if (NumDstElt < NumSrcElt) {
173*9880d681SAndroid Build Coastguard Worker // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
174*9880d681SAndroid Build Coastguard Worker Constant *Zero = Constant::getNullValue(DstEltTy);
175*9880d681SAndroid Build Coastguard Worker unsigned Ratio = NumSrcElt/NumDstElt;
176*9880d681SAndroid Build Coastguard Worker unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
177*9880d681SAndroid Build Coastguard Worker unsigned SrcElt = 0;
178*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumDstElt; ++i) {
179*9880d681SAndroid Build Coastguard Worker // Build each element of the result.
180*9880d681SAndroid Build Coastguard Worker Constant *Elt = Zero;
181*9880d681SAndroid Build Coastguard Worker unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
182*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0; j != Ratio; ++j) {
183*9880d681SAndroid Build Coastguard Worker Constant *Src = dyn_cast<ConstantInt>(C->getAggregateElement(SrcElt++));
184*9880d681SAndroid Build Coastguard Worker if (!Src) // Reject constantexpr elements.
185*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getBitCast(C, DestTy);
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Worker // Zero extend the element to the right size.
188*9880d681SAndroid Build Coastguard Worker Src = ConstantExpr::getZExt(Src, Elt->getType());
189*9880d681SAndroid Build Coastguard Worker
190*9880d681SAndroid Build Coastguard Worker // Shift it to the right place, depending on endianness.
191*9880d681SAndroid Build Coastguard Worker Src = ConstantExpr::getShl(Src,
192*9880d681SAndroid Build Coastguard Worker ConstantInt::get(Src->getType(), ShiftAmt));
193*9880d681SAndroid Build Coastguard Worker ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
194*9880d681SAndroid Build Coastguard Worker
195*9880d681SAndroid Build Coastguard Worker // Mix it in.
196*9880d681SAndroid Build Coastguard Worker Elt = ConstantExpr::getOr(Elt, Src);
197*9880d681SAndroid Build Coastguard Worker }
198*9880d681SAndroid Build Coastguard Worker Result.push_back(Elt);
199*9880d681SAndroid Build Coastguard Worker }
200*9880d681SAndroid Build Coastguard Worker return ConstantVector::get(Result);
201*9880d681SAndroid Build Coastguard Worker }
202*9880d681SAndroid Build Coastguard Worker
203*9880d681SAndroid Build Coastguard Worker // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
204*9880d681SAndroid Build Coastguard Worker unsigned Ratio = NumDstElt/NumSrcElt;
205*9880d681SAndroid Build Coastguard Worker unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy);
206*9880d681SAndroid Build Coastguard Worker
207*9880d681SAndroid Build Coastguard Worker // Loop over each source value, expanding into multiple results.
208*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumSrcElt; ++i) {
209*9880d681SAndroid Build Coastguard Worker auto *Src = dyn_cast<ConstantInt>(C->getAggregateElement(i));
210*9880d681SAndroid Build Coastguard Worker if (!Src) // Reject constantexpr elements.
211*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getBitCast(C, DestTy);
212*9880d681SAndroid Build Coastguard Worker
213*9880d681SAndroid Build Coastguard Worker unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
214*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0; j != Ratio; ++j) {
215*9880d681SAndroid Build Coastguard Worker // Shift the piece of the value into the right place, depending on
216*9880d681SAndroid Build Coastguard Worker // endianness.
217*9880d681SAndroid Build Coastguard Worker Constant *Elt = ConstantExpr::getLShr(Src,
218*9880d681SAndroid Build Coastguard Worker ConstantInt::get(Src->getType(), ShiftAmt));
219*9880d681SAndroid Build Coastguard Worker ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker // Truncate the element to an integer with the same pointer size and
222*9880d681SAndroid Build Coastguard Worker // convert the element back to a pointer using a inttoptr.
223*9880d681SAndroid Build Coastguard Worker if (DstEltTy->isPointerTy()) {
224*9880d681SAndroid Build Coastguard Worker IntegerType *DstIntTy = Type::getIntNTy(C->getContext(), DstBitSize);
225*9880d681SAndroid Build Coastguard Worker Constant *CE = ConstantExpr::getTrunc(Elt, DstIntTy);
226*9880d681SAndroid Build Coastguard Worker Result.push_back(ConstantExpr::getIntToPtr(CE, DstEltTy));
227*9880d681SAndroid Build Coastguard Worker continue;
228*9880d681SAndroid Build Coastguard Worker }
229*9880d681SAndroid Build Coastguard Worker
230*9880d681SAndroid Build Coastguard Worker // Truncate and remember this piece.
231*9880d681SAndroid Build Coastguard Worker Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
232*9880d681SAndroid Build Coastguard Worker }
233*9880d681SAndroid Build Coastguard Worker }
234*9880d681SAndroid Build Coastguard Worker
235*9880d681SAndroid Build Coastguard Worker return ConstantVector::get(Result);
236*9880d681SAndroid Build Coastguard Worker }
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker /// If this constant is a constant offset from a global, return the global and
241*9880d681SAndroid Build Coastguard Worker /// the constant. Because of constantexprs, this function is recursive.
IsConstantOffsetFromGlobal(Constant * C,GlobalValue * & GV,APInt & Offset,const DataLayout & DL)242*9880d681SAndroid Build Coastguard Worker bool llvm::IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
243*9880d681SAndroid Build Coastguard Worker APInt &Offset, const DataLayout &DL) {
244*9880d681SAndroid Build Coastguard Worker // Trivial case, constant is the global.
245*9880d681SAndroid Build Coastguard Worker if ((GV = dyn_cast<GlobalValue>(C))) {
246*9880d681SAndroid Build Coastguard Worker unsigned BitWidth = DL.getPointerTypeSizeInBits(GV->getType());
247*9880d681SAndroid Build Coastguard Worker Offset = APInt(BitWidth, 0);
248*9880d681SAndroid Build Coastguard Worker return true;
249*9880d681SAndroid Build Coastguard Worker }
250*9880d681SAndroid Build Coastguard Worker
251*9880d681SAndroid Build Coastguard Worker // Otherwise, if this isn't a constant expr, bail out.
252*9880d681SAndroid Build Coastguard Worker auto *CE = dyn_cast<ConstantExpr>(C);
253*9880d681SAndroid Build Coastguard Worker if (!CE) return false;
254*9880d681SAndroid Build Coastguard Worker
255*9880d681SAndroid Build Coastguard Worker // Look through ptr->int and ptr->ptr casts.
256*9880d681SAndroid Build Coastguard Worker if (CE->getOpcode() == Instruction::PtrToInt ||
257*9880d681SAndroid Build Coastguard Worker CE->getOpcode() == Instruction::BitCast)
258*9880d681SAndroid Build Coastguard Worker return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL);
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard Worker // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
261*9880d681SAndroid Build Coastguard Worker auto *GEP = dyn_cast<GEPOperator>(CE);
262*9880d681SAndroid Build Coastguard Worker if (!GEP)
263*9880d681SAndroid Build Coastguard Worker return false;
264*9880d681SAndroid Build Coastguard Worker
265*9880d681SAndroid Build Coastguard Worker unsigned BitWidth = DL.getPointerTypeSizeInBits(GEP->getType());
266*9880d681SAndroid Build Coastguard Worker APInt TmpOffset(BitWidth, 0);
267*9880d681SAndroid Build Coastguard Worker
268*9880d681SAndroid Build Coastguard Worker // If the base isn't a global+constant, we aren't either.
269*9880d681SAndroid Build Coastguard Worker if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, DL))
270*9880d681SAndroid Build Coastguard Worker return false;
271*9880d681SAndroid Build Coastguard Worker
272*9880d681SAndroid Build Coastguard Worker // Otherwise, add any offset that our operands provide.
273*9880d681SAndroid Build Coastguard Worker if (!GEP->accumulateConstantOffset(DL, TmpOffset))
274*9880d681SAndroid Build Coastguard Worker return false;
275*9880d681SAndroid Build Coastguard Worker
276*9880d681SAndroid Build Coastguard Worker Offset = TmpOffset;
277*9880d681SAndroid Build Coastguard Worker return true;
278*9880d681SAndroid Build Coastguard Worker }
279*9880d681SAndroid Build Coastguard Worker
280*9880d681SAndroid Build Coastguard Worker namespace {
281*9880d681SAndroid Build Coastguard Worker
282*9880d681SAndroid Build Coastguard Worker /// Recursive helper to read bits out of global. C is the constant being copied
283*9880d681SAndroid Build Coastguard Worker /// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy
284*9880d681SAndroid Build Coastguard Worker /// results into and BytesLeft is the number of bytes left in
285*9880d681SAndroid Build Coastguard Worker /// the CurPtr buffer. DL is the DataLayout.
ReadDataFromGlobal(Constant * C,uint64_t ByteOffset,unsigned char * CurPtr,unsigned BytesLeft,const DataLayout & DL)286*9880d681SAndroid Build Coastguard Worker bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr,
287*9880d681SAndroid Build Coastguard Worker unsigned BytesLeft, const DataLayout &DL) {
288*9880d681SAndroid Build Coastguard Worker assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) &&
289*9880d681SAndroid Build Coastguard Worker "Out of range access");
290*9880d681SAndroid Build Coastguard Worker
291*9880d681SAndroid Build Coastguard Worker // If this element is zero or undefined, we can just return since *CurPtr is
292*9880d681SAndroid Build Coastguard Worker // zero initialized.
293*9880d681SAndroid Build Coastguard Worker if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
294*9880d681SAndroid Build Coastguard Worker return true;
295*9880d681SAndroid Build Coastguard Worker
296*9880d681SAndroid Build Coastguard Worker if (auto *CI = dyn_cast<ConstantInt>(C)) {
297*9880d681SAndroid Build Coastguard Worker if (CI->getBitWidth() > 64 ||
298*9880d681SAndroid Build Coastguard Worker (CI->getBitWidth() & 7) != 0)
299*9880d681SAndroid Build Coastguard Worker return false;
300*9880d681SAndroid Build Coastguard Worker
301*9880d681SAndroid Build Coastguard Worker uint64_t Val = CI->getZExtValue();
302*9880d681SAndroid Build Coastguard Worker unsigned IntBytes = unsigned(CI->getBitWidth()/8);
303*9880d681SAndroid Build Coastguard Worker
304*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
305*9880d681SAndroid Build Coastguard Worker int n = ByteOffset;
306*9880d681SAndroid Build Coastguard Worker if (!DL.isLittleEndian())
307*9880d681SAndroid Build Coastguard Worker n = IntBytes - n - 1;
308*9880d681SAndroid Build Coastguard Worker CurPtr[i] = (unsigned char)(Val >> (n * 8));
309*9880d681SAndroid Build Coastguard Worker ++ByteOffset;
310*9880d681SAndroid Build Coastguard Worker }
311*9880d681SAndroid Build Coastguard Worker return true;
312*9880d681SAndroid Build Coastguard Worker }
313*9880d681SAndroid Build Coastguard Worker
314*9880d681SAndroid Build Coastguard Worker if (auto *CFP = dyn_cast<ConstantFP>(C)) {
315*9880d681SAndroid Build Coastguard Worker if (CFP->getType()->isDoubleTy()) {
316*9880d681SAndroid Build Coastguard Worker C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL);
317*9880d681SAndroid Build Coastguard Worker return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
318*9880d681SAndroid Build Coastguard Worker }
319*9880d681SAndroid Build Coastguard Worker if (CFP->getType()->isFloatTy()){
320*9880d681SAndroid Build Coastguard Worker C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), DL);
321*9880d681SAndroid Build Coastguard Worker return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
322*9880d681SAndroid Build Coastguard Worker }
323*9880d681SAndroid Build Coastguard Worker if (CFP->getType()->isHalfTy()){
324*9880d681SAndroid Build Coastguard Worker C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), DL);
325*9880d681SAndroid Build Coastguard Worker return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
326*9880d681SAndroid Build Coastguard Worker }
327*9880d681SAndroid Build Coastguard Worker return false;
328*9880d681SAndroid Build Coastguard Worker }
329*9880d681SAndroid Build Coastguard Worker
330*9880d681SAndroid Build Coastguard Worker if (auto *CS = dyn_cast<ConstantStruct>(C)) {
331*9880d681SAndroid Build Coastguard Worker const StructLayout *SL = DL.getStructLayout(CS->getType());
332*9880d681SAndroid Build Coastguard Worker unsigned Index = SL->getElementContainingOffset(ByteOffset);
333*9880d681SAndroid Build Coastguard Worker uint64_t CurEltOffset = SL->getElementOffset(Index);
334*9880d681SAndroid Build Coastguard Worker ByteOffset -= CurEltOffset;
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker while (1) {
337*9880d681SAndroid Build Coastguard Worker // If the element access is to the element itself and not to tail padding,
338*9880d681SAndroid Build Coastguard Worker // read the bytes from the element.
339*9880d681SAndroid Build Coastguard Worker uint64_t EltSize = DL.getTypeAllocSize(CS->getOperand(Index)->getType());
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard Worker if (ByteOffset < EltSize &&
342*9880d681SAndroid Build Coastguard Worker !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
343*9880d681SAndroid Build Coastguard Worker BytesLeft, DL))
344*9880d681SAndroid Build Coastguard Worker return false;
345*9880d681SAndroid Build Coastguard Worker
346*9880d681SAndroid Build Coastguard Worker ++Index;
347*9880d681SAndroid Build Coastguard Worker
348*9880d681SAndroid Build Coastguard Worker // Check to see if we read from the last struct element, if so we're done.
349*9880d681SAndroid Build Coastguard Worker if (Index == CS->getType()->getNumElements())
350*9880d681SAndroid Build Coastguard Worker return true;
351*9880d681SAndroid Build Coastguard Worker
352*9880d681SAndroid Build Coastguard Worker // If we read all of the bytes we needed from this element we're done.
353*9880d681SAndroid Build Coastguard Worker uint64_t NextEltOffset = SL->getElementOffset(Index);
354*9880d681SAndroid Build Coastguard Worker
355*9880d681SAndroid Build Coastguard Worker if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
356*9880d681SAndroid Build Coastguard Worker return true;
357*9880d681SAndroid Build Coastguard Worker
358*9880d681SAndroid Build Coastguard Worker // Move to the next element of the struct.
359*9880d681SAndroid Build Coastguard Worker CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
360*9880d681SAndroid Build Coastguard Worker BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
361*9880d681SAndroid Build Coastguard Worker ByteOffset = 0;
362*9880d681SAndroid Build Coastguard Worker CurEltOffset = NextEltOffset;
363*9880d681SAndroid Build Coastguard Worker }
364*9880d681SAndroid Build Coastguard Worker // not reached.
365*9880d681SAndroid Build Coastguard Worker }
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard Worker if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
368*9880d681SAndroid Build Coastguard Worker isa<ConstantDataSequential>(C)) {
369*9880d681SAndroid Build Coastguard Worker Type *EltTy = C->getType()->getSequentialElementType();
370*9880d681SAndroid Build Coastguard Worker uint64_t EltSize = DL.getTypeAllocSize(EltTy);
371*9880d681SAndroid Build Coastguard Worker uint64_t Index = ByteOffset / EltSize;
372*9880d681SAndroid Build Coastguard Worker uint64_t Offset = ByteOffset - Index * EltSize;
373*9880d681SAndroid Build Coastguard Worker uint64_t NumElts;
374*9880d681SAndroid Build Coastguard Worker if (auto *AT = dyn_cast<ArrayType>(C->getType()))
375*9880d681SAndroid Build Coastguard Worker NumElts = AT->getNumElements();
376*9880d681SAndroid Build Coastguard Worker else
377*9880d681SAndroid Build Coastguard Worker NumElts = C->getType()->getVectorNumElements();
378*9880d681SAndroid Build Coastguard Worker
379*9880d681SAndroid Build Coastguard Worker for (; Index != NumElts; ++Index) {
380*9880d681SAndroid Build Coastguard Worker if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
381*9880d681SAndroid Build Coastguard Worker BytesLeft, DL))
382*9880d681SAndroid Build Coastguard Worker return false;
383*9880d681SAndroid Build Coastguard Worker
384*9880d681SAndroid Build Coastguard Worker uint64_t BytesWritten = EltSize - Offset;
385*9880d681SAndroid Build Coastguard Worker assert(BytesWritten <= EltSize && "Not indexing into this element?");
386*9880d681SAndroid Build Coastguard Worker if (BytesWritten >= BytesLeft)
387*9880d681SAndroid Build Coastguard Worker return true;
388*9880d681SAndroid Build Coastguard Worker
389*9880d681SAndroid Build Coastguard Worker Offset = 0;
390*9880d681SAndroid Build Coastguard Worker BytesLeft -= BytesWritten;
391*9880d681SAndroid Build Coastguard Worker CurPtr += BytesWritten;
392*9880d681SAndroid Build Coastguard Worker }
393*9880d681SAndroid Build Coastguard Worker return true;
394*9880d681SAndroid Build Coastguard Worker }
395*9880d681SAndroid Build Coastguard Worker
396*9880d681SAndroid Build Coastguard Worker if (auto *CE = dyn_cast<ConstantExpr>(C)) {
397*9880d681SAndroid Build Coastguard Worker if (CE->getOpcode() == Instruction::IntToPtr &&
398*9880d681SAndroid Build Coastguard Worker CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) {
399*9880d681SAndroid Build Coastguard Worker return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
400*9880d681SAndroid Build Coastguard Worker BytesLeft, DL);
401*9880d681SAndroid Build Coastguard Worker }
402*9880d681SAndroid Build Coastguard Worker }
403*9880d681SAndroid Build Coastguard Worker
404*9880d681SAndroid Build Coastguard Worker // Otherwise, unknown initializer type.
405*9880d681SAndroid Build Coastguard Worker return false;
406*9880d681SAndroid Build Coastguard Worker }
407*9880d681SAndroid Build Coastguard Worker
FoldReinterpretLoadFromConstPtr(Constant * C,Type * LoadTy,const DataLayout & DL)408*9880d681SAndroid Build Coastguard Worker Constant *FoldReinterpretLoadFromConstPtr(Constant *C, Type *LoadTy,
409*9880d681SAndroid Build Coastguard Worker const DataLayout &DL) {
410*9880d681SAndroid Build Coastguard Worker auto *PTy = cast<PointerType>(C->getType());
411*9880d681SAndroid Build Coastguard Worker auto *IntType = dyn_cast<IntegerType>(LoadTy);
412*9880d681SAndroid Build Coastguard Worker
413*9880d681SAndroid Build Coastguard Worker // If this isn't an integer load we can't fold it directly.
414*9880d681SAndroid Build Coastguard Worker if (!IntType) {
415*9880d681SAndroid Build Coastguard Worker unsigned AS = PTy->getAddressSpace();
416*9880d681SAndroid Build Coastguard Worker
417*9880d681SAndroid Build Coastguard Worker // If this is a float/double load, we can try folding it as an int32/64 load
418*9880d681SAndroid Build Coastguard Worker // and then bitcast the result. This can be useful for union cases. Note
419*9880d681SAndroid Build Coastguard Worker // that address spaces don't matter here since we're not going to result in
420*9880d681SAndroid Build Coastguard Worker // an actual new load.
421*9880d681SAndroid Build Coastguard Worker Type *MapTy;
422*9880d681SAndroid Build Coastguard Worker if (LoadTy->isHalfTy())
423*9880d681SAndroid Build Coastguard Worker MapTy = Type::getInt16Ty(C->getContext());
424*9880d681SAndroid Build Coastguard Worker else if (LoadTy->isFloatTy())
425*9880d681SAndroid Build Coastguard Worker MapTy = Type::getInt32Ty(C->getContext());
426*9880d681SAndroid Build Coastguard Worker else if (LoadTy->isDoubleTy())
427*9880d681SAndroid Build Coastguard Worker MapTy = Type::getInt64Ty(C->getContext());
428*9880d681SAndroid Build Coastguard Worker else if (LoadTy->isVectorTy()) {
429*9880d681SAndroid Build Coastguard Worker MapTy = PointerType::getIntNTy(C->getContext(),
430*9880d681SAndroid Build Coastguard Worker DL.getTypeAllocSizeInBits(LoadTy));
431*9880d681SAndroid Build Coastguard Worker } else
432*9880d681SAndroid Build Coastguard Worker return nullptr;
433*9880d681SAndroid Build Coastguard Worker
434*9880d681SAndroid Build Coastguard Worker C = FoldBitCast(C, MapTy->getPointerTo(AS), DL);
435*9880d681SAndroid Build Coastguard Worker if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, MapTy, DL))
436*9880d681SAndroid Build Coastguard Worker return FoldBitCast(Res, LoadTy, DL);
437*9880d681SAndroid Build Coastguard Worker return nullptr;
438*9880d681SAndroid Build Coastguard Worker }
439*9880d681SAndroid Build Coastguard Worker
440*9880d681SAndroid Build Coastguard Worker unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
441*9880d681SAndroid Build Coastguard Worker if (BytesLoaded > 32 || BytesLoaded == 0)
442*9880d681SAndroid Build Coastguard Worker return nullptr;
443*9880d681SAndroid Build Coastguard Worker
444*9880d681SAndroid Build Coastguard Worker GlobalValue *GVal;
445*9880d681SAndroid Build Coastguard Worker APInt OffsetAI;
446*9880d681SAndroid Build Coastguard Worker if (!IsConstantOffsetFromGlobal(C, GVal, OffsetAI, DL))
447*9880d681SAndroid Build Coastguard Worker return nullptr;
448*9880d681SAndroid Build Coastguard Worker
449*9880d681SAndroid Build Coastguard Worker auto *GV = dyn_cast<GlobalVariable>(GVal);
450*9880d681SAndroid Build Coastguard Worker if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
451*9880d681SAndroid Build Coastguard Worker !GV->getInitializer()->getType()->isSized())
452*9880d681SAndroid Build Coastguard Worker return nullptr;
453*9880d681SAndroid Build Coastguard Worker
454*9880d681SAndroid Build Coastguard Worker int64_t Offset = OffsetAI.getSExtValue();
455*9880d681SAndroid Build Coastguard Worker int64_t InitializerSize = DL.getTypeAllocSize(GV->getInitializer()->getType());
456*9880d681SAndroid Build Coastguard Worker
457*9880d681SAndroid Build Coastguard Worker // If we're not accessing anything in this constant, the result is undefined.
458*9880d681SAndroid Build Coastguard Worker if (Offset + BytesLoaded <= 0)
459*9880d681SAndroid Build Coastguard Worker return UndefValue::get(IntType);
460*9880d681SAndroid Build Coastguard Worker
461*9880d681SAndroid Build Coastguard Worker // If we're not accessing anything in this constant, the result is undefined.
462*9880d681SAndroid Build Coastguard Worker if (Offset >= InitializerSize)
463*9880d681SAndroid Build Coastguard Worker return UndefValue::get(IntType);
464*9880d681SAndroid Build Coastguard Worker
465*9880d681SAndroid Build Coastguard Worker unsigned char RawBytes[32] = {0};
466*9880d681SAndroid Build Coastguard Worker unsigned char *CurPtr = RawBytes;
467*9880d681SAndroid Build Coastguard Worker unsigned BytesLeft = BytesLoaded;
468*9880d681SAndroid Build Coastguard Worker
469*9880d681SAndroid Build Coastguard Worker // If we're loading off the beginning of the global, some bytes may be valid.
470*9880d681SAndroid Build Coastguard Worker if (Offset < 0) {
471*9880d681SAndroid Build Coastguard Worker CurPtr += -Offset;
472*9880d681SAndroid Build Coastguard Worker BytesLeft += Offset;
473*9880d681SAndroid Build Coastguard Worker Offset = 0;
474*9880d681SAndroid Build Coastguard Worker }
475*9880d681SAndroid Build Coastguard Worker
476*9880d681SAndroid Build Coastguard Worker if (!ReadDataFromGlobal(GV->getInitializer(), Offset, CurPtr, BytesLeft, DL))
477*9880d681SAndroid Build Coastguard Worker return nullptr;
478*9880d681SAndroid Build Coastguard Worker
479*9880d681SAndroid Build Coastguard Worker APInt ResultVal = APInt(IntType->getBitWidth(), 0);
480*9880d681SAndroid Build Coastguard Worker if (DL.isLittleEndian()) {
481*9880d681SAndroid Build Coastguard Worker ResultVal = RawBytes[BytesLoaded - 1];
482*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1; i != BytesLoaded; ++i) {
483*9880d681SAndroid Build Coastguard Worker ResultVal <<= 8;
484*9880d681SAndroid Build Coastguard Worker ResultVal |= RawBytes[BytesLoaded - 1 - i];
485*9880d681SAndroid Build Coastguard Worker }
486*9880d681SAndroid Build Coastguard Worker } else {
487*9880d681SAndroid Build Coastguard Worker ResultVal = RawBytes[0];
488*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1; i != BytesLoaded; ++i) {
489*9880d681SAndroid Build Coastguard Worker ResultVal <<= 8;
490*9880d681SAndroid Build Coastguard Worker ResultVal |= RawBytes[i];
491*9880d681SAndroid Build Coastguard Worker }
492*9880d681SAndroid Build Coastguard Worker }
493*9880d681SAndroid Build Coastguard Worker
494*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(IntType->getContext(), ResultVal);
495*9880d681SAndroid Build Coastguard Worker }
496*9880d681SAndroid Build Coastguard Worker
ConstantFoldLoadThroughBitcast(ConstantExpr * CE,Type * DestTy,const DataLayout & DL)497*9880d681SAndroid Build Coastguard Worker Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE, Type *DestTy,
498*9880d681SAndroid Build Coastguard Worker const DataLayout &DL) {
499*9880d681SAndroid Build Coastguard Worker auto *SrcPtr = CE->getOperand(0);
500*9880d681SAndroid Build Coastguard Worker auto *SrcPtrTy = dyn_cast<PointerType>(SrcPtr->getType());
501*9880d681SAndroid Build Coastguard Worker if (!SrcPtrTy)
502*9880d681SAndroid Build Coastguard Worker return nullptr;
503*9880d681SAndroid Build Coastguard Worker Type *SrcTy = SrcPtrTy->getPointerElementType();
504*9880d681SAndroid Build Coastguard Worker
505*9880d681SAndroid Build Coastguard Worker Constant *C = ConstantFoldLoadFromConstPtr(SrcPtr, SrcTy, DL);
506*9880d681SAndroid Build Coastguard Worker if (!C)
507*9880d681SAndroid Build Coastguard Worker return nullptr;
508*9880d681SAndroid Build Coastguard Worker
509*9880d681SAndroid Build Coastguard Worker do {
510*9880d681SAndroid Build Coastguard Worker Type *SrcTy = C->getType();
511*9880d681SAndroid Build Coastguard Worker
512*9880d681SAndroid Build Coastguard Worker // If the type sizes are the same and a cast is legal, just directly
513*9880d681SAndroid Build Coastguard Worker // cast the constant.
514*9880d681SAndroid Build Coastguard Worker if (DL.getTypeSizeInBits(DestTy) == DL.getTypeSizeInBits(SrcTy)) {
515*9880d681SAndroid Build Coastguard Worker Instruction::CastOps Cast = Instruction::BitCast;
516*9880d681SAndroid Build Coastguard Worker // If we are going from a pointer to int or vice versa, we spell the cast
517*9880d681SAndroid Build Coastguard Worker // differently.
518*9880d681SAndroid Build Coastguard Worker if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
519*9880d681SAndroid Build Coastguard Worker Cast = Instruction::IntToPtr;
520*9880d681SAndroid Build Coastguard Worker else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
521*9880d681SAndroid Build Coastguard Worker Cast = Instruction::PtrToInt;
522*9880d681SAndroid Build Coastguard Worker
523*9880d681SAndroid Build Coastguard Worker if (CastInst::castIsValid(Cast, C, DestTy))
524*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getCast(Cast, C, DestTy);
525*9880d681SAndroid Build Coastguard Worker }
526*9880d681SAndroid Build Coastguard Worker
527*9880d681SAndroid Build Coastguard Worker // If this isn't an aggregate type, there is nothing we can do to drill down
528*9880d681SAndroid Build Coastguard Worker // and find a bitcastable constant.
529*9880d681SAndroid Build Coastguard Worker if (!SrcTy->isAggregateType())
530*9880d681SAndroid Build Coastguard Worker return nullptr;
531*9880d681SAndroid Build Coastguard Worker
532*9880d681SAndroid Build Coastguard Worker // We're simulating a load through a pointer that was bitcast to point to
533*9880d681SAndroid Build Coastguard Worker // a different type, so we can try to walk down through the initial
534*9880d681SAndroid Build Coastguard Worker // elements of an aggregate to see if some part of th e aggregate is
535*9880d681SAndroid Build Coastguard Worker // castable to implement the "load" semantic model.
536*9880d681SAndroid Build Coastguard Worker C = C->getAggregateElement(0u);
537*9880d681SAndroid Build Coastguard Worker } while (C);
538*9880d681SAndroid Build Coastguard Worker
539*9880d681SAndroid Build Coastguard Worker return nullptr;
540*9880d681SAndroid Build Coastguard Worker }
541*9880d681SAndroid Build Coastguard Worker
542*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
543*9880d681SAndroid Build Coastguard Worker
ConstantFoldLoadFromConstPtr(Constant * C,Type * Ty,const DataLayout & DL)544*9880d681SAndroid Build Coastguard Worker Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
545*9880d681SAndroid Build Coastguard Worker const DataLayout &DL) {
546*9880d681SAndroid Build Coastguard Worker // First, try the easy cases:
547*9880d681SAndroid Build Coastguard Worker if (auto *GV = dyn_cast<GlobalVariable>(C))
548*9880d681SAndroid Build Coastguard Worker if (GV->isConstant() && GV->hasDefinitiveInitializer())
549*9880d681SAndroid Build Coastguard Worker return GV->getInitializer();
550*9880d681SAndroid Build Coastguard Worker
551*9880d681SAndroid Build Coastguard Worker if (auto *GA = dyn_cast<GlobalAlias>(C))
552*9880d681SAndroid Build Coastguard Worker if (GA->getAliasee() && !GA->isInterposable())
553*9880d681SAndroid Build Coastguard Worker return ConstantFoldLoadFromConstPtr(GA->getAliasee(), Ty, DL);
554*9880d681SAndroid Build Coastguard Worker
555*9880d681SAndroid Build Coastguard Worker // If the loaded value isn't a constant expr, we can't handle it.
556*9880d681SAndroid Build Coastguard Worker auto *CE = dyn_cast<ConstantExpr>(C);
557*9880d681SAndroid Build Coastguard Worker if (!CE)
558*9880d681SAndroid Build Coastguard Worker return nullptr;
559*9880d681SAndroid Build Coastguard Worker
560*9880d681SAndroid Build Coastguard Worker if (CE->getOpcode() == Instruction::GetElementPtr) {
561*9880d681SAndroid Build Coastguard Worker if (auto *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
562*9880d681SAndroid Build Coastguard Worker if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
563*9880d681SAndroid Build Coastguard Worker if (Constant *V =
564*9880d681SAndroid Build Coastguard Worker ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
565*9880d681SAndroid Build Coastguard Worker return V;
566*9880d681SAndroid Build Coastguard Worker }
567*9880d681SAndroid Build Coastguard Worker }
568*9880d681SAndroid Build Coastguard Worker }
569*9880d681SAndroid Build Coastguard Worker
570*9880d681SAndroid Build Coastguard Worker if (CE->getOpcode() == Instruction::BitCast)
571*9880d681SAndroid Build Coastguard Worker if (Constant *LoadedC = ConstantFoldLoadThroughBitcast(CE, Ty, DL))
572*9880d681SAndroid Build Coastguard Worker return LoadedC;
573*9880d681SAndroid Build Coastguard Worker
574*9880d681SAndroid Build Coastguard Worker // Instead of loading constant c string, use corresponding integer value
575*9880d681SAndroid Build Coastguard Worker // directly if string length is small enough.
576*9880d681SAndroid Build Coastguard Worker StringRef Str;
577*9880d681SAndroid Build Coastguard Worker if (getConstantStringInfo(CE, Str) && !Str.empty()) {
578*9880d681SAndroid Build Coastguard Worker size_t StrLen = Str.size();
579*9880d681SAndroid Build Coastguard Worker unsigned NumBits = Ty->getPrimitiveSizeInBits();
580*9880d681SAndroid Build Coastguard Worker // Replace load with immediate integer if the result is an integer or fp
581*9880d681SAndroid Build Coastguard Worker // value.
582*9880d681SAndroid Build Coastguard Worker if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
583*9880d681SAndroid Build Coastguard Worker (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) {
584*9880d681SAndroid Build Coastguard Worker APInt StrVal(NumBits, 0);
585*9880d681SAndroid Build Coastguard Worker APInt SingleChar(NumBits, 0);
586*9880d681SAndroid Build Coastguard Worker if (DL.isLittleEndian()) {
587*9880d681SAndroid Build Coastguard Worker for (unsigned char C : reverse(Str.bytes())) {
588*9880d681SAndroid Build Coastguard Worker SingleChar = static_cast<uint64_t>(C);
589*9880d681SAndroid Build Coastguard Worker StrVal = (StrVal << 8) | SingleChar;
590*9880d681SAndroid Build Coastguard Worker }
591*9880d681SAndroid Build Coastguard Worker } else {
592*9880d681SAndroid Build Coastguard Worker for (unsigned char C : Str.bytes()) {
593*9880d681SAndroid Build Coastguard Worker SingleChar = static_cast<uint64_t>(C);
594*9880d681SAndroid Build Coastguard Worker StrVal = (StrVal << 8) | SingleChar;
595*9880d681SAndroid Build Coastguard Worker }
596*9880d681SAndroid Build Coastguard Worker // Append NULL at the end.
597*9880d681SAndroid Build Coastguard Worker SingleChar = 0;
598*9880d681SAndroid Build Coastguard Worker StrVal = (StrVal << 8) | SingleChar;
599*9880d681SAndroid Build Coastguard Worker }
600*9880d681SAndroid Build Coastguard Worker
601*9880d681SAndroid Build Coastguard Worker Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
602*9880d681SAndroid Build Coastguard Worker if (Ty->isFloatingPointTy())
603*9880d681SAndroid Build Coastguard Worker Res = ConstantExpr::getBitCast(Res, Ty);
604*9880d681SAndroid Build Coastguard Worker return Res;
605*9880d681SAndroid Build Coastguard Worker }
606*9880d681SAndroid Build Coastguard Worker }
607*9880d681SAndroid Build Coastguard Worker
608*9880d681SAndroid Build Coastguard Worker // If this load comes from anywhere in a constant global, and if the global
609*9880d681SAndroid Build Coastguard Worker // is all undef or zero, we know what it loads.
610*9880d681SAndroid Build Coastguard Worker if (auto *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, DL))) {
611*9880d681SAndroid Build Coastguard Worker if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
612*9880d681SAndroid Build Coastguard Worker if (GV->getInitializer()->isNullValue())
613*9880d681SAndroid Build Coastguard Worker return Constant::getNullValue(Ty);
614*9880d681SAndroid Build Coastguard Worker if (isa<UndefValue>(GV->getInitializer()))
615*9880d681SAndroid Build Coastguard Worker return UndefValue::get(Ty);
616*9880d681SAndroid Build Coastguard Worker }
617*9880d681SAndroid Build Coastguard Worker }
618*9880d681SAndroid Build Coastguard Worker
619*9880d681SAndroid Build Coastguard Worker // Try hard to fold loads from bitcasted strange and non-type-safe things.
620*9880d681SAndroid Build Coastguard Worker return FoldReinterpretLoadFromConstPtr(CE, Ty, DL);
621*9880d681SAndroid Build Coastguard Worker }
622*9880d681SAndroid Build Coastguard Worker
623*9880d681SAndroid Build Coastguard Worker namespace {
624*9880d681SAndroid Build Coastguard Worker
ConstantFoldLoadInst(const LoadInst * LI,const DataLayout & DL)625*9880d681SAndroid Build Coastguard Worker Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout &DL) {
626*9880d681SAndroid Build Coastguard Worker if (LI->isVolatile()) return nullptr;
627*9880d681SAndroid Build Coastguard Worker
628*9880d681SAndroid Build Coastguard Worker if (auto *C = dyn_cast<Constant>(LI->getOperand(0)))
629*9880d681SAndroid Build Coastguard Worker return ConstantFoldLoadFromConstPtr(C, LI->getType(), DL);
630*9880d681SAndroid Build Coastguard Worker
631*9880d681SAndroid Build Coastguard Worker return nullptr;
632*9880d681SAndroid Build Coastguard Worker }
633*9880d681SAndroid Build Coastguard Worker
634*9880d681SAndroid Build Coastguard Worker /// One of Op0/Op1 is a constant expression.
635*9880d681SAndroid Build Coastguard Worker /// Attempt to symbolically evaluate the result of a binary operator merging
636*9880d681SAndroid Build Coastguard Worker /// these together. If target data info is available, it is provided as DL,
637*9880d681SAndroid Build Coastguard Worker /// otherwise DL is null.
SymbolicallyEvaluateBinop(unsigned Opc,Constant * Op0,Constant * Op1,const DataLayout & DL)638*9880d681SAndroid Build Coastguard Worker Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1,
639*9880d681SAndroid Build Coastguard Worker const DataLayout &DL) {
640*9880d681SAndroid Build Coastguard Worker // SROA
641*9880d681SAndroid Build Coastguard Worker
642*9880d681SAndroid Build Coastguard Worker // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
643*9880d681SAndroid Build Coastguard Worker // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
644*9880d681SAndroid Build Coastguard Worker // bits.
645*9880d681SAndroid Build Coastguard Worker
646*9880d681SAndroid Build Coastguard Worker if (Opc == Instruction::And) {
647*9880d681SAndroid Build Coastguard Worker unsigned BitWidth = DL.getTypeSizeInBits(Op0->getType()->getScalarType());
648*9880d681SAndroid Build Coastguard Worker APInt KnownZero0(BitWidth, 0), KnownOne0(BitWidth, 0);
649*9880d681SAndroid Build Coastguard Worker APInt KnownZero1(BitWidth, 0), KnownOne1(BitWidth, 0);
650*9880d681SAndroid Build Coastguard Worker computeKnownBits(Op0, KnownZero0, KnownOne0, DL);
651*9880d681SAndroid Build Coastguard Worker computeKnownBits(Op1, KnownZero1, KnownOne1, DL);
652*9880d681SAndroid Build Coastguard Worker if ((KnownOne1 | KnownZero0).isAllOnesValue()) {
653*9880d681SAndroid Build Coastguard Worker // All the bits of Op0 that the 'and' could be masking are already zero.
654*9880d681SAndroid Build Coastguard Worker return Op0;
655*9880d681SAndroid Build Coastguard Worker }
656*9880d681SAndroid Build Coastguard Worker if ((KnownOne0 | KnownZero1).isAllOnesValue()) {
657*9880d681SAndroid Build Coastguard Worker // All the bits of Op1 that the 'and' could be masking are already zero.
658*9880d681SAndroid Build Coastguard Worker return Op1;
659*9880d681SAndroid Build Coastguard Worker }
660*9880d681SAndroid Build Coastguard Worker
661*9880d681SAndroid Build Coastguard Worker APInt KnownZero = KnownZero0 | KnownZero1;
662*9880d681SAndroid Build Coastguard Worker APInt KnownOne = KnownOne0 & KnownOne1;
663*9880d681SAndroid Build Coastguard Worker if ((KnownZero | KnownOne).isAllOnesValue()) {
664*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(Op0->getType(), KnownOne);
665*9880d681SAndroid Build Coastguard Worker }
666*9880d681SAndroid Build Coastguard Worker }
667*9880d681SAndroid Build Coastguard Worker
668*9880d681SAndroid Build Coastguard Worker // If the constant expr is something like &A[123] - &A[4].f, fold this into a
669*9880d681SAndroid Build Coastguard Worker // constant. This happens frequently when iterating over a global array.
670*9880d681SAndroid Build Coastguard Worker if (Opc == Instruction::Sub) {
671*9880d681SAndroid Build Coastguard Worker GlobalValue *GV1, *GV2;
672*9880d681SAndroid Build Coastguard Worker APInt Offs1, Offs2;
673*9880d681SAndroid Build Coastguard Worker
674*9880d681SAndroid Build Coastguard Worker if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, DL))
675*9880d681SAndroid Build Coastguard Worker if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, DL) && GV1 == GV2) {
676*9880d681SAndroid Build Coastguard Worker unsigned OpSize = DL.getTypeSizeInBits(Op0->getType());
677*9880d681SAndroid Build Coastguard Worker
678*9880d681SAndroid Build Coastguard Worker // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
679*9880d681SAndroid Build Coastguard Worker // PtrToInt may change the bitwidth so we have convert to the right size
680*9880d681SAndroid Build Coastguard Worker // first.
681*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
682*9880d681SAndroid Build Coastguard Worker Offs2.zextOrTrunc(OpSize));
683*9880d681SAndroid Build Coastguard Worker }
684*9880d681SAndroid Build Coastguard Worker }
685*9880d681SAndroid Build Coastguard Worker
686*9880d681SAndroid Build Coastguard Worker return nullptr;
687*9880d681SAndroid Build Coastguard Worker }
688*9880d681SAndroid Build Coastguard Worker
689*9880d681SAndroid Build Coastguard Worker /// If array indices are not pointer-sized integers, explicitly cast them so
690*9880d681SAndroid Build Coastguard Worker /// that they aren't implicitly casted by the getelementptr.
CastGEPIndices(Type * SrcElemTy,ArrayRef<Constant * > Ops,Type * ResultTy,const DataLayout & DL,const TargetLibraryInfo * TLI)691*9880d681SAndroid Build Coastguard Worker Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
692*9880d681SAndroid Build Coastguard Worker Type *ResultTy, const DataLayout &DL,
693*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
694*9880d681SAndroid Build Coastguard Worker Type *IntPtrTy = DL.getIntPtrType(ResultTy);
695*9880d681SAndroid Build Coastguard Worker
696*9880d681SAndroid Build Coastguard Worker bool Any = false;
697*9880d681SAndroid Build Coastguard Worker SmallVector<Constant*, 32> NewIdxs;
698*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
699*9880d681SAndroid Build Coastguard Worker if ((i == 1 ||
700*9880d681SAndroid Build Coastguard Worker !isa<StructType>(GetElementPtrInst::getIndexedType(SrcElemTy,
701*9880d681SAndroid Build Coastguard Worker Ops.slice(1, i - 1)))) &&
702*9880d681SAndroid Build Coastguard Worker Ops[i]->getType() != IntPtrTy) {
703*9880d681SAndroid Build Coastguard Worker Any = true;
704*9880d681SAndroid Build Coastguard Worker NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i],
705*9880d681SAndroid Build Coastguard Worker true,
706*9880d681SAndroid Build Coastguard Worker IntPtrTy,
707*9880d681SAndroid Build Coastguard Worker true),
708*9880d681SAndroid Build Coastguard Worker Ops[i], IntPtrTy));
709*9880d681SAndroid Build Coastguard Worker } else
710*9880d681SAndroid Build Coastguard Worker NewIdxs.push_back(Ops[i]);
711*9880d681SAndroid Build Coastguard Worker }
712*9880d681SAndroid Build Coastguard Worker
713*9880d681SAndroid Build Coastguard Worker if (!Any)
714*9880d681SAndroid Build Coastguard Worker return nullptr;
715*9880d681SAndroid Build Coastguard Worker
716*9880d681SAndroid Build Coastguard Worker Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ops[0], NewIdxs);
717*9880d681SAndroid Build Coastguard Worker if (auto *CE = dyn_cast<ConstantExpr>(C)) {
718*9880d681SAndroid Build Coastguard Worker if (Constant *Folded = ConstantFoldConstantExpression(CE, DL, TLI))
719*9880d681SAndroid Build Coastguard Worker C = Folded;
720*9880d681SAndroid Build Coastguard Worker }
721*9880d681SAndroid Build Coastguard Worker
722*9880d681SAndroid Build Coastguard Worker return C;
723*9880d681SAndroid Build Coastguard Worker }
724*9880d681SAndroid Build Coastguard Worker
725*9880d681SAndroid Build Coastguard Worker /// Strip the pointer casts, but preserve the address space information.
StripPtrCastKeepAS(Constant * Ptr,Type * & ElemTy)726*9880d681SAndroid Build Coastguard Worker Constant* StripPtrCastKeepAS(Constant* Ptr, Type *&ElemTy) {
727*9880d681SAndroid Build Coastguard Worker assert(Ptr->getType()->isPointerTy() && "Not a pointer type");
728*9880d681SAndroid Build Coastguard Worker auto *OldPtrTy = cast<PointerType>(Ptr->getType());
729*9880d681SAndroid Build Coastguard Worker Ptr = Ptr->stripPointerCasts();
730*9880d681SAndroid Build Coastguard Worker auto *NewPtrTy = cast<PointerType>(Ptr->getType());
731*9880d681SAndroid Build Coastguard Worker
732*9880d681SAndroid Build Coastguard Worker ElemTy = NewPtrTy->getPointerElementType();
733*9880d681SAndroid Build Coastguard Worker
734*9880d681SAndroid Build Coastguard Worker // Preserve the address space number of the pointer.
735*9880d681SAndroid Build Coastguard Worker if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
736*9880d681SAndroid Build Coastguard Worker NewPtrTy = ElemTy->getPointerTo(OldPtrTy->getAddressSpace());
737*9880d681SAndroid Build Coastguard Worker Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy);
738*9880d681SAndroid Build Coastguard Worker }
739*9880d681SAndroid Build Coastguard Worker return Ptr;
740*9880d681SAndroid Build Coastguard Worker }
741*9880d681SAndroid Build Coastguard Worker
742*9880d681SAndroid Build Coastguard Worker /// If we can symbolically evaluate the GEP constant expression, do so.
SymbolicallyEvaluateGEP(const GEPOperator * GEP,ArrayRef<Constant * > Ops,const DataLayout & DL,const TargetLibraryInfo * TLI)743*9880d681SAndroid Build Coastguard Worker Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
744*9880d681SAndroid Build Coastguard Worker ArrayRef<Constant *> Ops,
745*9880d681SAndroid Build Coastguard Worker const DataLayout &DL,
746*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
747*9880d681SAndroid Build Coastguard Worker Type *SrcElemTy = GEP->getSourceElementType();
748*9880d681SAndroid Build Coastguard Worker Type *ResElemTy = GEP->getResultElementType();
749*9880d681SAndroid Build Coastguard Worker Type *ResTy = GEP->getType();
750*9880d681SAndroid Build Coastguard Worker if (!SrcElemTy->isSized())
751*9880d681SAndroid Build Coastguard Worker return nullptr;
752*9880d681SAndroid Build Coastguard Worker
753*9880d681SAndroid Build Coastguard Worker if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy, DL, TLI))
754*9880d681SAndroid Build Coastguard Worker return C;
755*9880d681SAndroid Build Coastguard Worker
756*9880d681SAndroid Build Coastguard Worker Constant *Ptr = Ops[0];
757*9880d681SAndroid Build Coastguard Worker if (!Ptr->getType()->isPointerTy())
758*9880d681SAndroid Build Coastguard Worker return nullptr;
759*9880d681SAndroid Build Coastguard Worker
760*9880d681SAndroid Build Coastguard Worker Type *IntPtrTy = DL.getIntPtrType(Ptr->getType());
761*9880d681SAndroid Build Coastguard Worker
762*9880d681SAndroid Build Coastguard Worker // If this is a constant expr gep that is effectively computing an
763*9880d681SAndroid Build Coastguard Worker // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
764*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = Ops.size(); i != e; ++i)
765*9880d681SAndroid Build Coastguard Worker if (!isa<ConstantInt>(Ops[i])) {
766*9880d681SAndroid Build Coastguard Worker
767*9880d681SAndroid Build Coastguard Worker // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
768*9880d681SAndroid Build Coastguard Worker // "inttoptr (sub (ptrtoint Ptr), V)"
769*9880d681SAndroid Build Coastguard Worker if (Ops.size() == 2 && ResElemTy->isIntegerTy(8)) {
770*9880d681SAndroid Build Coastguard Worker auto *CE = dyn_cast<ConstantExpr>(Ops[1]);
771*9880d681SAndroid Build Coastguard Worker assert((!CE || CE->getType() == IntPtrTy) &&
772*9880d681SAndroid Build Coastguard Worker "CastGEPIndices didn't canonicalize index types!");
773*9880d681SAndroid Build Coastguard Worker if (CE && CE->getOpcode() == Instruction::Sub &&
774*9880d681SAndroid Build Coastguard Worker CE->getOperand(0)->isNullValue()) {
775*9880d681SAndroid Build Coastguard Worker Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
776*9880d681SAndroid Build Coastguard Worker Res = ConstantExpr::getSub(Res, CE->getOperand(1));
777*9880d681SAndroid Build Coastguard Worker Res = ConstantExpr::getIntToPtr(Res, ResTy);
778*9880d681SAndroid Build Coastguard Worker if (auto *ResCE = dyn_cast<ConstantExpr>(Res))
779*9880d681SAndroid Build Coastguard Worker Res = ConstantFoldConstantExpression(ResCE, DL, TLI);
780*9880d681SAndroid Build Coastguard Worker return Res;
781*9880d681SAndroid Build Coastguard Worker }
782*9880d681SAndroid Build Coastguard Worker }
783*9880d681SAndroid Build Coastguard Worker return nullptr;
784*9880d681SAndroid Build Coastguard Worker }
785*9880d681SAndroid Build Coastguard Worker
786*9880d681SAndroid Build Coastguard Worker unsigned BitWidth = DL.getTypeSizeInBits(IntPtrTy);
787*9880d681SAndroid Build Coastguard Worker APInt Offset =
788*9880d681SAndroid Build Coastguard Worker APInt(BitWidth,
789*9880d681SAndroid Build Coastguard Worker DL.getIndexedOffsetInType(
790*9880d681SAndroid Build Coastguard Worker SrcElemTy,
791*9880d681SAndroid Build Coastguard Worker makeArrayRef((Value * const *)Ops.data() + 1, Ops.size() - 1)));
792*9880d681SAndroid Build Coastguard Worker Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
793*9880d681SAndroid Build Coastguard Worker
794*9880d681SAndroid Build Coastguard Worker // If this is a GEP of a GEP, fold it all into a single GEP.
795*9880d681SAndroid Build Coastguard Worker while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
796*9880d681SAndroid Build Coastguard Worker SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end());
797*9880d681SAndroid Build Coastguard Worker
798*9880d681SAndroid Build Coastguard Worker // Do not try the incorporate the sub-GEP if some index is not a number.
799*9880d681SAndroid Build Coastguard Worker bool AllConstantInt = true;
800*9880d681SAndroid Build Coastguard Worker for (Value *NestedOp : NestedOps)
801*9880d681SAndroid Build Coastguard Worker if (!isa<ConstantInt>(NestedOp)) {
802*9880d681SAndroid Build Coastguard Worker AllConstantInt = false;
803*9880d681SAndroid Build Coastguard Worker break;
804*9880d681SAndroid Build Coastguard Worker }
805*9880d681SAndroid Build Coastguard Worker if (!AllConstantInt)
806*9880d681SAndroid Build Coastguard Worker break;
807*9880d681SAndroid Build Coastguard Worker
808*9880d681SAndroid Build Coastguard Worker Ptr = cast<Constant>(GEP->getOperand(0));
809*9880d681SAndroid Build Coastguard Worker SrcElemTy = GEP->getSourceElementType();
810*9880d681SAndroid Build Coastguard Worker Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps));
811*9880d681SAndroid Build Coastguard Worker Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
812*9880d681SAndroid Build Coastguard Worker }
813*9880d681SAndroid Build Coastguard Worker
814*9880d681SAndroid Build Coastguard Worker // If the base value for this address is a literal integer value, fold the
815*9880d681SAndroid Build Coastguard Worker // getelementptr to the resulting integer value casted to the pointer type.
816*9880d681SAndroid Build Coastguard Worker APInt BasePtr(BitWidth, 0);
817*9880d681SAndroid Build Coastguard Worker if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) {
818*9880d681SAndroid Build Coastguard Worker if (CE->getOpcode() == Instruction::IntToPtr) {
819*9880d681SAndroid Build Coastguard Worker if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
820*9880d681SAndroid Build Coastguard Worker BasePtr = Base->getValue().zextOrTrunc(BitWidth);
821*9880d681SAndroid Build Coastguard Worker }
822*9880d681SAndroid Build Coastguard Worker }
823*9880d681SAndroid Build Coastguard Worker
824*9880d681SAndroid Build Coastguard Worker if (Ptr->isNullValue() || BasePtr != 0) {
825*9880d681SAndroid Build Coastguard Worker Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr);
826*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getIntToPtr(C, ResTy);
827*9880d681SAndroid Build Coastguard Worker }
828*9880d681SAndroid Build Coastguard Worker
829*9880d681SAndroid Build Coastguard Worker // Otherwise form a regular getelementptr. Recompute the indices so that
830*9880d681SAndroid Build Coastguard Worker // we eliminate over-indexing of the notional static type array bounds.
831*9880d681SAndroid Build Coastguard Worker // This makes it easy to determine if the getelementptr is "inbounds".
832*9880d681SAndroid Build Coastguard Worker // Also, this helps GlobalOpt do SROA on GlobalVariables.
833*9880d681SAndroid Build Coastguard Worker Type *Ty = Ptr->getType();
834*9880d681SAndroid Build Coastguard Worker assert(Ty->isPointerTy() && "Forming regular GEP of non-pointer type");
835*9880d681SAndroid Build Coastguard Worker SmallVector<Constant *, 32> NewIdxs;
836*9880d681SAndroid Build Coastguard Worker
837*9880d681SAndroid Build Coastguard Worker do {
838*9880d681SAndroid Build Coastguard Worker if (!Ty->isStructTy()) {
839*9880d681SAndroid Build Coastguard Worker if (Ty->isPointerTy()) {
840*9880d681SAndroid Build Coastguard Worker // The only pointer indexing we'll do is on the first index of the GEP.
841*9880d681SAndroid Build Coastguard Worker if (!NewIdxs.empty())
842*9880d681SAndroid Build Coastguard Worker break;
843*9880d681SAndroid Build Coastguard Worker
844*9880d681SAndroid Build Coastguard Worker Ty = SrcElemTy;
845*9880d681SAndroid Build Coastguard Worker
846*9880d681SAndroid Build Coastguard Worker // Only handle pointers to sized types, not pointers to functions.
847*9880d681SAndroid Build Coastguard Worker if (!Ty->isSized())
848*9880d681SAndroid Build Coastguard Worker return nullptr;
849*9880d681SAndroid Build Coastguard Worker } else if (auto *ATy = dyn_cast<SequentialType>(Ty)) {
850*9880d681SAndroid Build Coastguard Worker Ty = ATy->getElementType();
851*9880d681SAndroid Build Coastguard Worker } else {
852*9880d681SAndroid Build Coastguard Worker // We've reached some non-indexable type.
853*9880d681SAndroid Build Coastguard Worker break;
854*9880d681SAndroid Build Coastguard Worker }
855*9880d681SAndroid Build Coastguard Worker
856*9880d681SAndroid Build Coastguard Worker // Determine which element of the array the offset points into.
857*9880d681SAndroid Build Coastguard Worker APInt ElemSize(BitWidth, DL.getTypeAllocSize(Ty));
858*9880d681SAndroid Build Coastguard Worker if (ElemSize == 0) {
859*9880d681SAndroid Build Coastguard Worker // The element size is 0. This may be [0 x Ty]*, so just use a zero
860*9880d681SAndroid Build Coastguard Worker // index for this level and proceed to the next level to see if it can
861*9880d681SAndroid Build Coastguard Worker // accommodate the offset.
862*9880d681SAndroid Build Coastguard Worker NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0));
863*9880d681SAndroid Build Coastguard Worker } else {
864*9880d681SAndroid Build Coastguard Worker // The element size is non-zero divide the offset by the element
865*9880d681SAndroid Build Coastguard Worker // size (rounding down), to compute the index at this level.
866*9880d681SAndroid Build Coastguard Worker bool Overflow;
867*9880d681SAndroid Build Coastguard Worker APInt NewIdx = Offset.sdiv_ov(ElemSize, Overflow);
868*9880d681SAndroid Build Coastguard Worker if (Overflow)
869*9880d681SAndroid Build Coastguard Worker break;
870*9880d681SAndroid Build Coastguard Worker Offset -= NewIdx * ElemSize;
871*9880d681SAndroid Build Coastguard Worker NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
872*9880d681SAndroid Build Coastguard Worker }
873*9880d681SAndroid Build Coastguard Worker } else {
874*9880d681SAndroid Build Coastguard Worker auto *STy = cast<StructType>(Ty);
875*9880d681SAndroid Build Coastguard Worker // If we end up with an offset that isn't valid for this struct type, we
876*9880d681SAndroid Build Coastguard Worker // can't re-form this GEP in a regular form, so bail out. The pointer
877*9880d681SAndroid Build Coastguard Worker // operand likely went through casts that are necessary to make the GEP
878*9880d681SAndroid Build Coastguard Worker // sensible.
879*9880d681SAndroid Build Coastguard Worker const StructLayout &SL = *DL.getStructLayout(STy);
880*9880d681SAndroid Build Coastguard Worker if (Offset.isNegative() || Offset.uge(SL.getSizeInBytes()))
881*9880d681SAndroid Build Coastguard Worker break;
882*9880d681SAndroid Build Coastguard Worker
883*9880d681SAndroid Build Coastguard Worker // Determine which field of the struct the offset points into. The
884*9880d681SAndroid Build Coastguard Worker // getZExtValue is fine as we've already ensured that the offset is
885*9880d681SAndroid Build Coastguard Worker // within the range representable by the StructLayout API.
886*9880d681SAndroid Build Coastguard Worker unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
887*9880d681SAndroid Build Coastguard Worker NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
888*9880d681SAndroid Build Coastguard Worker ElIdx));
889*9880d681SAndroid Build Coastguard Worker Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
890*9880d681SAndroid Build Coastguard Worker Ty = STy->getTypeAtIndex(ElIdx);
891*9880d681SAndroid Build Coastguard Worker }
892*9880d681SAndroid Build Coastguard Worker } while (Ty != ResElemTy);
893*9880d681SAndroid Build Coastguard Worker
894*9880d681SAndroid Build Coastguard Worker // If we haven't used up the entire offset by descending the static
895*9880d681SAndroid Build Coastguard Worker // type, then the offset is pointing into the middle of an indivisible
896*9880d681SAndroid Build Coastguard Worker // member, so we can't simplify it.
897*9880d681SAndroid Build Coastguard Worker if (Offset != 0)
898*9880d681SAndroid Build Coastguard Worker return nullptr;
899*9880d681SAndroid Build Coastguard Worker
900*9880d681SAndroid Build Coastguard Worker // Create a GEP.
901*9880d681SAndroid Build Coastguard Worker Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs);
902*9880d681SAndroid Build Coastguard Worker assert(C->getType()->getPointerElementType() == Ty &&
903*9880d681SAndroid Build Coastguard Worker "Computed GetElementPtr has unexpected type!");
904*9880d681SAndroid Build Coastguard Worker
905*9880d681SAndroid Build Coastguard Worker // If we ended up indexing a member with a type that doesn't match
906*9880d681SAndroid Build Coastguard Worker // the type of what the original indices indexed, add a cast.
907*9880d681SAndroid Build Coastguard Worker if (Ty != ResElemTy)
908*9880d681SAndroid Build Coastguard Worker C = FoldBitCast(C, ResTy, DL);
909*9880d681SAndroid Build Coastguard Worker
910*9880d681SAndroid Build Coastguard Worker return C;
911*9880d681SAndroid Build Coastguard Worker }
912*9880d681SAndroid Build Coastguard Worker
913*9880d681SAndroid Build Coastguard Worker /// Attempt to constant fold an instruction with the
914*9880d681SAndroid Build Coastguard Worker /// specified opcode and operands. If successful, the constant result is
915*9880d681SAndroid Build Coastguard Worker /// returned, if not, null is returned. Note that this function can fail when
916*9880d681SAndroid Build Coastguard Worker /// attempting to fold instructions like loads and stores, which have no
917*9880d681SAndroid Build Coastguard Worker /// constant expression form.
918*9880d681SAndroid Build Coastguard Worker ///
919*9880d681SAndroid Build Coastguard Worker /// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/etc
920*9880d681SAndroid Build Coastguard Worker /// information, due to only being passed an opcode and operands. Constant
921*9880d681SAndroid Build Coastguard Worker /// folding using this function strips this information.
922*9880d681SAndroid Build Coastguard Worker ///
ConstantFoldInstOperandsImpl(const Value * InstOrCE,Type * DestTy,unsigned Opcode,ArrayRef<Constant * > Ops,const DataLayout & DL,const TargetLibraryInfo * TLI)923*9880d681SAndroid Build Coastguard Worker Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, Type *DestTy,
924*9880d681SAndroid Build Coastguard Worker unsigned Opcode,
925*9880d681SAndroid Build Coastguard Worker ArrayRef<Constant *> Ops,
926*9880d681SAndroid Build Coastguard Worker const DataLayout &DL,
927*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
928*9880d681SAndroid Build Coastguard Worker // Handle easy binops first.
929*9880d681SAndroid Build Coastguard Worker if (Instruction::isBinaryOp(Opcode))
930*9880d681SAndroid Build Coastguard Worker return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL);
931*9880d681SAndroid Build Coastguard Worker
932*9880d681SAndroid Build Coastguard Worker if (Instruction::isCast(Opcode))
933*9880d681SAndroid Build Coastguard Worker return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL);
934*9880d681SAndroid Build Coastguard Worker
935*9880d681SAndroid Build Coastguard Worker if (auto *GEP = dyn_cast<GEPOperator>(InstOrCE)) {
936*9880d681SAndroid Build Coastguard Worker if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI))
937*9880d681SAndroid Build Coastguard Worker return C;
938*9880d681SAndroid Build Coastguard Worker
939*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getGetElementPtr(GEP->getSourceElementType(),
940*9880d681SAndroid Build Coastguard Worker Ops[0], Ops.slice(1));
941*9880d681SAndroid Build Coastguard Worker }
942*9880d681SAndroid Build Coastguard Worker
943*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
944*9880d681SAndroid Build Coastguard Worker default: return nullptr;
945*9880d681SAndroid Build Coastguard Worker case Instruction::ICmp:
946*9880d681SAndroid Build Coastguard Worker case Instruction::FCmp: llvm_unreachable("Invalid for compares");
947*9880d681SAndroid Build Coastguard Worker case Instruction::Call:
948*9880d681SAndroid Build Coastguard Worker if (auto *F = dyn_cast<Function>(Ops.back()))
949*9880d681SAndroid Build Coastguard Worker if (canConstantFoldCallTo(F))
950*9880d681SAndroid Build Coastguard Worker return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI);
951*9880d681SAndroid Build Coastguard Worker return nullptr;
952*9880d681SAndroid Build Coastguard Worker case Instruction::Select:
953*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
954*9880d681SAndroid Build Coastguard Worker case Instruction::ExtractElement:
955*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
956*9880d681SAndroid Build Coastguard Worker case Instruction::InsertElement:
957*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
958*9880d681SAndroid Build Coastguard Worker case Instruction::ShuffleVector:
959*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
960*9880d681SAndroid Build Coastguard Worker }
961*9880d681SAndroid Build Coastguard Worker }
962*9880d681SAndroid Build Coastguard Worker
963*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
964*9880d681SAndroid Build Coastguard Worker
965*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
966*9880d681SAndroid Build Coastguard Worker // Constant Folding public APIs
967*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
968*9880d681SAndroid Build Coastguard Worker
ConstantFoldInstruction(Instruction * I,const DataLayout & DL,const TargetLibraryInfo * TLI)969*9880d681SAndroid Build Coastguard Worker Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
970*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
971*9880d681SAndroid Build Coastguard Worker // Handle PHI nodes quickly here...
972*9880d681SAndroid Build Coastguard Worker if (auto *PN = dyn_cast<PHINode>(I)) {
973*9880d681SAndroid Build Coastguard Worker Constant *CommonValue = nullptr;
974*9880d681SAndroid Build Coastguard Worker
975*9880d681SAndroid Build Coastguard Worker for (Value *Incoming : PN->incoming_values()) {
976*9880d681SAndroid Build Coastguard Worker // If the incoming value is undef then skip it. Note that while we could
977*9880d681SAndroid Build Coastguard Worker // skip the value if it is equal to the phi node itself we choose not to
978*9880d681SAndroid Build Coastguard Worker // because that would break the rule that constant folding only applies if
979*9880d681SAndroid Build Coastguard Worker // all operands are constants.
980*9880d681SAndroid Build Coastguard Worker if (isa<UndefValue>(Incoming))
981*9880d681SAndroid Build Coastguard Worker continue;
982*9880d681SAndroid Build Coastguard Worker // If the incoming value is not a constant, then give up.
983*9880d681SAndroid Build Coastguard Worker auto *C = dyn_cast<Constant>(Incoming);
984*9880d681SAndroid Build Coastguard Worker if (!C)
985*9880d681SAndroid Build Coastguard Worker return nullptr;
986*9880d681SAndroid Build Coastguard Worker // Fold the PHI's operands.
987*9880d681SAndroid Build Coastguard Worker if (auto *NewC = dyn_cast<ConstantExpr>(C))
988*9880d681SAndroid Build Coastguard Worker C = ConstantFoldConstantExpression(NewC, DL, TLI);
989*9880d681SAndroid Build Coastguard Worker // If the incoming value is a different constant to
990*9880d681SAndroid Build Coastguard Worker // the one we saw previously, then give up.
991*9880d681SAndroid Build Coastguard Worker if (CommonValue && C != CommonValue)
992*9880d681SAndroid Build Coastguard Worker return nullptr;
993*9880d681SAndroid Build Coastguard Worker CommonValue = C;
994*9880d681SAndroid Build Coastguard Worker }
995*9880d681SAndroid Build Coastguard Worker
996*9880d681SAndroid Build Coastguard Worker
997*9880d681SAndroid Build Coastguard Worker // If we reach here, all incoming values are the same constant or undef.
998*9880d681SAndroid Build Coastguard Worker return CommonValue ? CommonValue : UndefValue::get(PN->getType());
999*9880d681SAndroid Build Coastguard Worker }
1000*9880d681SAndroid Build Coastguard Worker
1001*9880d681SAndroid Build Coastguard Worker // Scan the operand list, checking to see if they are all constants, if so,
1002*9880d681SAndroid Build Coastguard Worker // hand off to ConstantFoldInstOperandsImpl.
1003*9880d681SAndroid Build Coastguard Worker if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); }))
1004*9880d681SAndroid Build Coastguard Worker return nullptr;
1005*9880d681SAndroid Build Coastguard Worker
1006*9880d681SAndroid Build Coastguard Worker SmallVector<Constant *, 8> Ops;
1007*9880d681SAndroid Build Coastguard Worker for (const Use &OpU : I->operands()) {
1008*9880d681SAndroid Build Coastguard Worker auto *Op = cast<Constant>(&OpU);
1009*9880d681SAndroid Build Coastguard Worker // Fold the Instruction's operands.
1010*9880d681SAndroid Build Coastguard Worker if (auto *NewCE = dyn_cast<ConstantExpr>(Op))
1011*9880d681SAndroid Build Coastguard Worker Op = ConstantFoldConstantExpression(NewCE, DL, TLI);
1012*9880d681SAndroid Build Coastguard Worker
1013*9880d681SAndroid Build Coastguard Worker Ops.push_back(Op);
1014*9880d681SAndroid Build Coastguard Worker }
1015*9880d681SAndroid Build Coastguard Worker
1016*9880d681SAndroid Build Coastguard Worker if (const auto *CI = dyn_cast<CmpInst>(I))
1017*9880d681SAndroid Build Coastguard Worker return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
1018*9880d681SAndroid Build Coastguard Worker DL, TLI);
1019*9880d681SAndroid Build Coastguard Worker
1020*9880d681SAndroid Build Coastguard Worker if (const auto *LI = dyn_cast<LoadInst>(I))
1021*9880d681SAndroid Build Coastguard Worker return ConstantFoldLoadInst(LI, DL);
1022*9880d681SAndroid Build Coastguard Worker
1023*9880d681SAndroid Build Coastguard Worker if (auto *IVI = dyn_cast<InsertValueInst>(I)) {
1024*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getInsertValue(
1025*9880d681SAndroid Build Coastguard Worker cast<Constant>(IVI->getAggregateOperand()),
1026*9880d681SAndroid Build Coastguard Worker cast<Constant>(IVI->getInsertedValueOperand()),
1027*9880d681SAndroid Build Coastguard Worker IVI->getIndices());
1028*9880d681SAndroid Build Coastguard Worker }
1029*9880d681SAndroid Build Coastguard Worker
1030*9880d681SAndroid Build Coastguard Worker if (auto *EVI = dyn_cast<ExtractValueInst>(I)) {
1031*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getExtractValue(
1032*9880d681SAndroid Build Coastguard Worker cast<Constant>(EVI->getAggregateOperand()),
1033*9880d681SAndroid Build Coastguard Worker EVI->getIndices());
1034*9880d681SAndroid Build Coastguard Worker }
1035*9880d681SAndroid Build Coastguard Worker
1036*9880d681SAndroid Build Coastguard Worker return ConstantFoldInstOperands(I, Ops, DL, TLI);
1037*9880d681SAndroid Build Coastguard Worker }
1038*9880d681SAndroid Build Coastguard Worker
1039*9880d681SAndroid Build Coastguard Worker namespace {
1040*9880d681SAndroid Build Coastguard Worker
1041*9880d681SAndroid Build Coastguard Worker Constant *
ConstantFoldConstantExpressionImpl(const ConstantExpr * CE,const DataLayout & DL,const TargetLibraryInfo * TLI,SmallPtrSetImpl<ConstantExpr * > & FoldedOps)1042*9880d681SAndroid Build Coastguard Worker ConstantFoldConstantExpressionImpl(const ConstantExpr *CE, const DataLayout &DL,
1043*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI,
1044*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<ConstantExpr *> &FoldedOps) {
1045*9880d681SAndroid Build Coastguard Worker SmallVector<Constant *, 8> Ops;
1046*9880d681SAndroid Build Coastguard Worker for (const Use &NewU : CE->operands()) {
1047*9880d681SAndroid Build Coastguard Worker auto *NewC = cast<Constant>(&NewU);
1048*9880d681SAndroid Build Coastguard Worker // Recursively fold the ConstantExpr's operands. If we have already folded
1049*9880d681SAndroid Build Coastguard Worker // a ConstantExpr, we don't have to process it again.
1050*9880d681SAndroid Build Coastguard Worker if (auto *NewCE = dyn_cast<ConstantExpr>(NewC)) {
1051*9880d681SAndroid Build Coastguard Worker if (FoldedOps.insert(NewCE).second)
1052*9880d681SAndroid Build Coastguard Worker NewC = ConstantFoldConstantExpressionImpl(NewCE, DL, TLI, FoldedOps);
1053*9880d681SAndroid Build Coastguard Worker }
1054*9880d681SAndroid Build Coastguard Worker Ops.push_back(NewC);
1055*9880d681SAndroid Build Coastguard Worker }
1056*9880d681SAndroid Build Coastguard Worker
1057*9880d681SAndroid Build Coastguard Worker if (CE->isCompare())
1058*9880d681SAndroid Build Coastguard Worker return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
1059*9880d681SAndroid Build Coastguard Worker DL, TLI);
1060*9880d681SAndroid Build Coastguard Worker
1061*9880d681SAndroid Build Coastguard Worker return ConstantFoldInstOperandsImpl(CE, CE->getType(), CE->getOpcode(), Ops,
1062*9880d681SAndroid Build Coastguard Worker DL, TLI);
1063*9880d681SAndroid Build Coastguard Worker }
1064*9880d681SAndroid Build Coastguard Worker
1065*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
1066*9880d681SAndroid Build Coastguard Worker
ConstantFoldConstantExpression(const ConstantExpr * CE,const DataLayout & DL,const TargetLibraryInfo * TLI)1067*9880d681SAndroid Build Coastguard Worker Constant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE,
1068*9880d681SAndroid Build Coastguard Worker const DataLayout &DL,
1069*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
1070*9880d681SAndroid Build Coastguard Worker SmallPtrSet<ConstantExpr *, 4> FoldedOps;
1071*9880d681SAndroid Build Coastguard Worker return ConstantFoldConstantExpressionImpl(CE, DL, TLI, FoldedOps);
1072*9880d681SAndroid Build Coastguard Worker }
1073*9880d681SAndroid Build Coastguard Worker
ConstantFoldInstOperands(Instruction * I,ArrayRef<Constant * > Ops,const DataLayout & DL,const TargetLibraryInfo * TLI)1074*9880d681SAndroid Build Coastguard Worker Constant *llvm::ConstantFoldInstOperands(Instruction *I,
1075*9880d681SAndroid Build Coastguard Worker ArrayRef<Constant *> Ops,
1076*9880d681SAndroid Build Coastguard Worker const DataLayout &DL,
1077*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
1078*9880d681SAndroid Build Coastguard Worker return ConstantFoldInstOperandsImpl(I, I->getType(), I->getOpcode(), Ops, DL,
1079*9880d681SAndroid Build Coastguard Worker TLI);
1080*9880d681SAndroid Build Coastguard Worker }
1081*9880d681SAndroid Build Coastguard Worker
ConstantFoldInstOperands(unsigned Opcode,Type * DestTy,ArrayRef<Constant * > Ops,const DataLayout & DL,const TargetLibraryInfo * TLI)1082*9880d681SAndroid Build Coastguard Worker Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
1083*9880d681SAndroid Build Coastguard Worker ArrayRef<Constant *> Ops,
1084*9880d681SAndroid Build Coastguard Worker const DataLayout &DL,
1085*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
1086*9880d681SAndroid Build Coastguard Worker assert(Opcode != Instruction::GetElementPtr && "Invalid for GEPs");
1087*9880d681SAndroid Build Coastguard Worker return ConstantFoldInstOperandsImpl(nullptr, DestTy, Opcode, Ops, DL, TLI);
1088*9880d681SAndroid Build Coastguard Worker }
1089*9880d681SAndroid Build Coastguard Worker
ConstantFoldCompareInstOperands(unsigned Predicate,Constant * Ops0,Constant * Ops1,const DataLayout & DL,const TargetLibraryInfo * TLI)1090*9880d681SAndroid Build Coastguard Worker Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
1091*9880d681SAndroid Build Coastguard Worker Constant *Ops0, Constant *Ops1,
1092*9880d681SAndroid Build Coastguard Worker const DataLayout &DL,
1093*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
1094*9880d681SAndroid Build Coastguard Worker // fold: icmp (inttoptr x), null -> icmp x, 0
1095*9880d681SAndroid Build Coastguard Worker // fold: icmp (ptrtoint x), 0 -> icmp x, null
1096*9880d681SAndroid Build Coastguard Worker // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
1097*9880d681SAndroid Build Coastguard Worker // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
1098*9880d681SAndroid Build Coastguard Worker //
1099*9880d681SAndroid Build Coastguard Worker // FIXME: The following comment is out of data and the DataLayout is here now.
1100*9880d681SAndroid Build Coastguard Worker // ConstantExpr::getCompare cannot do this, because it doesn't have DL
1101*9880d681SAndroid Build Coastguard Worker // around to know if bit truncation is happening.
1102*9880d681SAndroid Build Coastguard Worker if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
1103*9880d681SAndroid Build Coastguard Worker if (Ops1->isNullValue()) {
1104*9880d681SAndroid Build Coastguard Worker if (CE0->getOpcode() == Instruction::IntToPtr) {
1105*9880d681SAndroid Build Coastguard Worker Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1106*9880d681SAndroid Build Coastguard Worker // Convert the integer value to the right size to ensure we get the
1107*9880d681SAndroid Build Coastguard Worker // proper extension or truncation.
1108*9880d681SAndroid Build Coastguard Worker Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1109*9880d681SAndroid Build Coastguard Worker IntPtrTy, false);
1110*9880d681SAndroid Build Coastguard Worker Constant *Null = Constant::getNullValue(C->getType());
1111*9880d681SAndroid Build Coastguard Worker return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1112*9880d681SAndroid Build Coastguard Worker }
1113*9880d681SAndroid Build Coastguard Worker
1114*9880d681SAndroid Build Coastguard Worker // Only do this transformation if the int is intptrty in size, otherwise
1115*9880d681SAndroid Build Coastguard Worker // there is a truncation or extension that we aren't modeling.
1116*9880d681SAndroid Build Coastguard Worker if (CE0->getOpcode() == Instruction::PtrToInt) {
1117*9880d681SAndroid Build Coastguard Worker Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1118*9880d681SAndroid Build Coastguard Worker if (CE0->getType() == IntPtrTy) {
1119*9880d681SAndroid Build Coastguard Worker Constant *C = CE0->getOperand(0);
1120*9880d681SAndroid Build Coastguard Worker Constant *Null = Constant::getNullValue(C->getType());
1121*9880d681SAndroid Build Coastguard Worker return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1122*9880d681SAndroid Build Coastguard Worker }
1123*9880d681SAndroid Build Coastguard Worker }
1124*9880d681SAndroid Build Coastguard Worker }
1125*9880d681SAndroid Build Coastguard Worker
1126*9880d681SAndroid Build Coastguard Worker if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
1127*9880d681SAndroid Build Coastguard Worker if (CE0->getOpcode() == CE1->getOpcode()) {
1128*9880d681SAndroid Build Coastguard Worker if (CE0->getOpcode() == Instruction::IntToPtr) {
1129*9880d681SAndroid Build Coastguard Worker Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1130*9880d681SAndroid Build Coastguard Worker
1131*9880d681SAndroid Build Coastguard Worker // Convert the integer value to the right size to ensure we get the
1132*9880d681SAndroid Build Coastguard Worker // proper extension or truncation.
1133*9880d681SAndroid Build Coastguard Worker Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1134*9880d681SAndroid Build Coastguard Worker IntPtrTy, false);
1135*9880d681SAndroid Build Coastguard Worker Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
1136*9880d681SAndroid Build Coastguard Worker IntPtrTy, false);
1137*9880d681SAndroid Build Coastguard Worker return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
1138*9880d681SAndroid Build Coastguard Worker }
1139*9880d681SAndroid Build Coastguard Worker
1140*9880d681SAndroid Build Coastguard Worker // Only do this transformation if the int is intptrty in size, otherwise
1141*9880d681SAndroid Build Coastguard Worker // there is a truncation or extension that we aren't modeling.
1142*9880d681SAndroid Build Coastguard Worker if (CE0->getOpcode() == Instruction::PtrToInt) {
1143*9880d681SAndroid Build Coastguard Worker Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1144*9880d681SAndroid Build Coastguard Worker if (CE0->getType() == IntPtrTy &&
1145*9880d681SAndroid Build Coastguard Worker CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1146*9880d681SAndroid Build Coastguard Worker return ConstantFoldCompareInstOperands(
1147*9880d681SAndroid Build Coastguard Worker Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI);
1148*9880d681SAndroid Build Coastguard Worker }
1149*9880d681SAndroid Build Coastguard Worker }
1150*9880d681SAndroid Build Coastguard Worker }
1151*9880d681SAndroid Build Coastguard Worker }
1152*9880d681SAndroid Build Coastguard Worker
1153*9880d681SAndroid Build Coastguard Worker // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
1154*9880d681SAndroid Build Coastguard Worker // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
1155*9880d681SAndroid Build Coastguard Worker if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
1156*9880d681SAndroid Build Coastguard Worker CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
1157*9880d681SAndroid Build Coastguard Worker Constant *LHS = ConstantFoldCompareInstOperands(
1158*9880d681SAndroid Build Coastguard Worker Predicate, CE0->getOperand(0), Ops1, DL, TLI);
1159*9880d681SAndroid Build Coastguard Worker Constant *RHS = ConstantFoldCompareInstOperands(
1160*9880d681SAndroid Build Coastguard Worker Predicate, CE0->getOperand(1), Ops1, DL, TLI);
1161*9880d681SAndroid Build Coastguard Worker unsigned OpC =
1162*9880d681SAndroid Build Coastguard Worker Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
1163*9880d681SAndroid Build Coastguard Worker return ConstantFoldBinaryOpOperands(OpC, LHS, RHS, DL);
1164*9880d681SAndroid Build Coastguard Worker }
1165*9880d681SAndroid Build Coastguard Worker }
1166*9880d681SAndroid Build Coastguard Worker
1167*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
1168*9880d681SAndroid Build Coastguard Worker }
1169*9880d681SAndroid Build Coastguard Worker
ConstantFoldBinaryOpOperands(unsigned Opcode,Constant * LHS,Constant * RHS,const DataLayout & DL)1170*9880d681SAndroid Build Coastguard Worker Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
1171*9880d681SAndroid Build Coastguard Worker Constant *RHS,
1172*9880d681SAndroid Build Coastguard Worker const DataLayout &DL) {
1173*9880d681SAndroid Build Coastguard Worker assert(Instruction::isBinaryOp(Opcode));
1174*9880d681SAndroid Build Coastguard Worker if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS))
1175*9880d681SAndroid Build Coastguard Worker if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL))
1176*9880d681SAndroid Build Coastguard Worker return C;
1177*9880d681SAndroid Build Coastguard Worker
1178*9880d681SAndroid Build Coastguard Worker return ConstantExpr::get(Opcode, LHS, RHS);
1179*9880d681SAndroid Build Coastguard Worker }
1180*9880d681SAndroid Build Coastguard Worker
ConstantFoldCastOperand(unsigned Opcode,Constant * C,Type * DestTy,const DataLayout & DL)1181*9880d681SAndroid Build Coastguard Worker Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
1182*9880d681SAndroid Build Coastguard Worker Type *DestTy, const DataLayout &DL) {
1183*9880d681SAndroid Build Coastguard Worker assert(Instruction::isCast(Opcode));
1184*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
1185*9880d681SAndroid Build Coastguard Worker default:
1186*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Missing case");
1187*9880d681SAndroid Build Coastguard Worker case Instruction::PtrToInt:
1188*9880d681SAndroid Build Coastguard Worker // If the input is a inttoptr, eliminate the pair. This requires knowing
1189*9880d681SAndroid Build Coastguard Worker // the width of a pointer, so it can't be done in ConstantExpr::getCast.
1190*9880d681SAndroid Build Coastguard Worker if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1191*9880d681SAndroid Build Coastguard Worker if (CE->getOpcode() == Instruction::IntToPtr) {
1192*9880d681SAndroid Build Coastguard Worker Constant *Input = CE->getOperand(0);
1193*9880d681SAndroid Build Coastguard Worker unsigned InWidth = Input->getType()->getScalarSizeInBits();
1194*9880d681SAndroid Build Coastguard Worker unsigned PtrWidth = DL.getPointerTypeSizeInBits(CE->getType());
1195*9880d681SAndroid Build Coastguard Worker if (PtrWidth < InWidth) {
1196*9880d681SAndroid Build Coastguard Worker Constant *Mask =
1197*9880d681SAndroid Build Coastguard Worker ConstantInt::get(CE->getContext(),
1198*9880d681SAndroid Build Coastguard Worker APInt::getLowBitsSet(InWidth, PtrWidth));
1199*9880d681SAndroid Build Coastguard Worker Input = ConstantExpr::getAnd(Input, Mask);
1200*9880d681SAndroid Build Coastguard Worker }
1201*9880d681SAndroid Build Coastguard Worker // Do a zext or trunc to get to the dest size.
1202*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getIntegerCast(Input, DestTy, false);
1203*9880d681SAndroid Build Coastguard Worker }
1204*9880d681SAndroid Build Coastguard Worker }
1205*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getCast(Opcode, C, DestTy);
1206*9880d681SAndroid Build Coastguard Worker case Instruction::IntToPtr:
1207*9880d681SAndroid Build Coastguard Worker // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
1208*9880d681SAndroid Build Coastguard Worker // the int size is >= the ptr size and the address spaces are the same.
1209*9880d681SAndroid Build Coastguard Worker // This requires knowing the width of a pointer, so it can't be done in
1210*9880d681SAndroid Build Coastguard Worker // ConstantExpr::getCast.
1211*9880d681SAndroid Build Coastguard Worker if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1212*9880d681SAndroid Build Coastguard Worker if (CE->getOpcode() == Instruction::PtrToInt) {
1213*9880d681SAndroid Build Coastguard Worker Constant *SrcPtr = CE->getOperand(0);
1214*9880d681SAndroid Build Coastguard Worker unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType());
1215*9880d681SAndroid Build Coastguard Worker unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1216*9880d681SAndroid Build Coastguard Worker
1217*9880d681SAndroid Build Coastguard Worker if (MidIntSize >= SrcPtrSize) {
1218*9880d681SAndroid Build Coastguard Worker unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
1219*9880d681SAndroid Build Coastguard Worker if (SrcAS == DestTy->getPointerAddressSpace())
1220*9880d681SAndroid Build Coastguard Worker return FoldBitCast(CE->getOperand(0), DestTy, DL);
1221*9880d681SAndroid Build Coastguard Worker }
1222*9880d681SAndroid Build Coastguard Worker }
1223*9880d681SAndroid Build Coastguard Worker }
1224*9880d681SAndroid Build Coastguard Worker
1225*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getCast(Opcode, C, DestTy);
1226*9880d681SAndroid Build Coastguard Worker case Instruction::Trunc:
1227*9880d681SAndroid Build Coastguard Worker case Instruction::ZExt:
1228*9880d681SAndroid Build Coastguard Worker case Instruction::SExt:
1229*9880d681SAndroid Build Coastguard Worker case Instruction::FPTrunc:
1230*9880d681SAndroid Build Coastguard Worker case Instruction::FPExt:
1231*9880d681SAndroid Build Coastguard Worker case Instruction::UIToFP:
1232*9880d681SAndroid Build Coastguard Worker case Instruction::SIToFP:
1233*9880d681SAndroid Build Coastguard Worker case Instruction::FPToUI:
1234*9880d681SAndroid Build Coastguard Worker case Instruction::FPToSI:
1235*9880d681SAndroid Build Coastguard Worker case Instruction::AddrSpaceCast:
1236*9880d681SAndroid Build Coastguard Worker return ConstantExpr::getCast(Opcode, C, DestTy);
1237*9880d681SAndroid Build Coastguard Worker case Instruction::BitCast:
1238*9880d681SAndroid Build Coastguard Worker return FoldBitCast(C, DestTy, DL);
1239*9880d681SAndroid Build Coastguard Worker }
1240*9880d681SAndroid Build Coastguard Worker }
1241*9880d681SAndroid Build Coastguard Worker
ConstantFoldLoadThroughGEPConstantExpr(Constant * C,ConstantExpr * CE)1242*9880d681SAndroid Build Coastguard Worker Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
1243*9880d681SAndroid Build Coastguard Worker ConstantExpr *CE) {
1244*9880d681SAndroid Build Coastguard Worker if (!CE->getOperand(1)->isNullValue())
1245*9880d681SAndroid Build Coastguard Worker return nullptr; // Do not allow stepping over the value!
1246*9880d681SAndroid Build Coastguard Worker
1247*9880d681SAndroid Build Coastguard Worker // Loop over all of the operands, tracking down which value we are
1248*9880d681SAndroid Build Coastguard Worker // addressing.
1249*9880d681SAndroid Build Coastguard Worker for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) {
1250*9880d681SAndroid Build Coastguard Worker C = C->getAggregateElement(CE->getOperand(i));
1251*9880d681SAndroid Build Coastguard Worker if (!C)
1252*9880d681SAndroid Build Coastguard Worker return nullptr;
1253*9880d681SAndroid Build Coastguard Worker }
1254*9880d681SAndroid Build Coastguard Worker return C;
1255*9880d681SAndroid Build Coastguard Worker }
1256*9880d681SAndroid Build Coastguard Worker
1257*9880d681SAndroid Build Coastguard Worker Constant *
ConstantFoldLoadThroughGEPIndices(Constant * C,ArrayRef<Constant * > Indices)1258*9880d681SAndroid Build Coastguard Worker llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
1259*9880d681SAndroid Build Coastguard Worker ArrayRef<Constant *> Indices) {
1260*9880d681SAndroid Build Coastguard Worker // Loop over all of the operands, tracking down which value we are
1261*9880d681SAndroid Build Coastguard Worker // addressing.
1262*9880d681SAndroid Build Coastguard Worker for (Constant *Index : Indices) {
1263*9880d681SAndroid Build Coastguard Worker C = C->getAggregateElement(Index);
1264*9880d681SAndroid Build Coastguard Worker if (!C)
1265*9880d681SAndroid Build Coastguard Worker return nullptr;
1266*9880d681SAndroid Build Coastguard Worker }
1267*9880d681SAndroid Build Coastguard Worker return C;
1268*9880d681SAndroid Build Coastguard Worker }
1269*9880d681SAndroid Build Coastguard Worker
1270*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
1271*9880d681SAndroid Build Coastguard Worker // Constant Folding for Calls
1272*9880d681SAndroid Build Coastguard Worker //
1273*9880d681SAndroid Build Coastguard Worker
canConstantFoldCallTo(const Function * F)1274*9880d681SAndroid Build Coastguard Worker bool llvm::canConstantFoldCallTo(const Function *F) {
1275*9880d681SAndroid Build Coastguard Worker switch (F->getIntrinsicID()) {
1276*9880d681SAndroid Build Coastguard Worker case Intrinsic::fabs:
1277*9880d681SAndroid Build Coastguard Worker case Intrinsic::minnum:
1278*9880d681SAndroid Build Coastguard Worker case Intrinsic::maxnum:
1279*9880d681SAndroid Build Coastguard Worker case Intrinsic::log:
1280*9880d681SAndroid Build Coastguard Worker case Intrinsic::log2:
1281*9880d681SAndroid Build Coastguard Worker case Intrinsic::log10:
1282*9880d681SAndroid Build Coastguard Worker case Intrinsic::exp:
1283*9880d681SAndroid Build Coastguard Worker case Intrinsic::exp2:
1284*9880d681SAndroid Build Coastguard Worker case Intrinsic::floor:
1285*9880d681SAndroid Build Coastguard Worker case Intrinsic::ceil:
1286*9880d681SAndroid Build Coastguard Worker case Intrinsic::sqrt:
1287*9880d681SAndroid Build Coastguard Worker case Intrinsic::sin:
1288*9880d681SAndroid Build Coastguard Worker case Intrinsic::cos:
1289*9880d681SAndroid Build Coastguard Worker case Intrinsic::trunc:
1290*9880d681SAndroid Build Coastguard Worker case Intrinsic::rint:
1291*9880d681SAndroid Build Coastguard Worker case Intrinsic::nearbyint:
1292*9880d681SAndroid Build Coastguard Worker case Intrinsic::pow:
1293*9880d681SAndroid Build Coastguard Worker case Intrinsic::powi:
1294*9880d681SAndroid Build Coastguard Worker case Intrinsic::bswap:
1295*9880d681SAndroid Build Coastguard Worker case Intrinsic::ctpop:
1296*9880d681SAndroid Build Coastguard Worker case Intrinsic::ctlz:
1297*9880d681SAndroid Build Coastguard Worker case Intrinsic::cttz:
1298*9880d681SAndroid Build Coastguard Worker case Intrinsic::fma:
1299*9880d681SAndroid Build Coastguard Worker case Intrinsic::fmuladd:
1300*9880d681SAndroid Build Coastguard Worker case Intrinsic::copysign:
1301*9880d681SAndroid Build Coastguard Worker case Intrinsic::round:
1302*9880d681SAndroid Build Coastguard Worker case Intrinsic::masked_load:
1303*9880d681SAndroid Build Coastguard Worker case Intrinsic::sadd_with_overflow:
1304*9880d681SAndroid Build Coastguard Worker case Intrinsic::uadd_with_overflow:
1305*9880d681SAndroid Build Coastguard Worker case Intrinsic::ssub_with_overflow:
1306*9880d681SAndroid Build Coastguard Worker case Intrinsic::usub_with_overflow:
1307*9880d681SAndroid Build Coastguard Worker case Intrinsic::smul_with_overflow:
1308*9880d681SAndroid Build Coastguard Worker case Intrinsic::umul_with_overflow:
1309*9880d681SAndroid Build Coastguard Worker case Intrinsic::convert_from_fp16:
1310*9880d681SAndroid Build Coastguard Worker case Intrinsic::convert_to_fp16:
1311*9880d681SAndroid Build Coastguard Worker case Intrinsic::bitreverse:
1312*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse_cvtss2si:
1313*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse_cvtss2si64:
1314*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse_cvttss2si:
1315*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse_cvttss2si64:
1316*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse2_cvtsd2si:
1317*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse2_cvtsd2si64:
1318*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse2_cvttsd2si:
1319*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse2_cvttsd2si64:
1320*9880d681SAndroid Build Coastguard Worker return true;
1321*9880d681SAndroid Build Coastguard Worker default:
1322*9880d681SAndroid Build Coastguard Worker return false;
1323*9880d681SAndroid Build Coastguard Worker case 0: break;
1324*9880d681SAndroid Build Coastguard Worker }
1325*9880d681SAndroid Build Coastguard Worker
1326*9880d681SAndroid Build Coastguard Worker if (!F->hasName())
1327*9880d681SAndroid Build Coastguard Worker return false;
1328*9880d681SAndroid Build Coastguard Worker StringRef Name = F->getName();
1329*9880d681SAndroid Build Coastguard Worker
1330*9880d681SAndroid Build Coastguard Worker // In these cases, the check of the length is required. We don't want to
1331*9880d681SAndroid Build Coastguard Worker // return true for a name like "cos\0blah" which strcmp would return equal to
1332*9880d681SAndroid Build Coastguard Worker // "cos", but has length 8.
1333*9880d681SAndroid Build Coastguard Worker switch (Name[0]) {
1334*9880d681SAndroid Build Coastguard Worker default:
1335*9880d681SAndroid Build Coastguard Worker return false;
1336*9880d681SAndroid Build Coastguard Worker case 'a':
1337*9880d681SAndroid Build Coastguard Worker return Name == "acos" || Name == "asin" || Name == "atan" ||
1338*9880d681SAndroid Build Coastguard Worker Name == "atan2" || Name == "acosf" || Name == "asinf" ||
1339*9880d681SAndroid Build Coastguard Worker Name == "atanf" || Name == "atan2f";
1340*9880d681SAndroid Build Coastguard Worker case 'c':
1341*9880d681SAndroid Build Coastguard Worker return Name == "ceil" || Name == "cos" || Name == "cosh" ||
1342*9880d681SAndroid Build Coastguard Worker Name == "ceilf" || Name == "cosf" || Name == "coshf";
1343*9880d681SAndroid Build Coastguard Worker case 'e':
1344*9880d681SAndroid Build Coastguard Worker return Name == "exp" || Name == "exp2" || Name == "expf" || Name == "exp2f";
1345*9880d681SAndroid Build Coastguard Worker case 'f':
1346*9880d681SAndroid Build Coastguard Worker return Name == "fabs" || Name == "floor" || Name == "fmod" ||
1347*9880d681SAndroid Build Coastguard Worker Name == "fabsf" || Name == "floorf" || Name == "fmodf";
1348*9880d681SAndroid Build Coastguard Worker case 'l':
1349*9880d681SAndroid Build Coastguard Worker return Name == "log" || Name == "log10" || Name == "logf" ||
1350*9880d681SAndroid Build Coastguard Worker Name == "log10f";
1351*9880d681SAndroid Build Coastguard Worker case 'p':
1352*9880d681SAndroid Build Coastguard Worker return Name == "pow" || Name == "powf";
1353*9880d681SAndroid Build Coastguard Worker case 's':
1354*9880d681SAndroid Build Coastguard Worker return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
1355*9880d681SAndroid Build Coastguard Worker Name == "sinf" || Name == "sinhf" || Name == "sqrtf";
1356*9880d681SAndroid Build Coastguard Worker case 't':
1357*9880d681SAndroid Build Coastguard Worker return Name == "tan" || Name == "tanh" || Name == "tanf" || Name == "tanhf";
1358*9880d681SAndroid Build Coastguard Worker }
1359*9880d681SAndroid Build Coastguard Worker }
1360*9880d681SAndroid Build Coastguard Worker
1361*9880d681SAndroid Build Coastguard Worker namespace {
1362*9880d681SAndroid Build Coastguard Worker
GetConstantFoldFPValue(double V,Type * Ty)1363*9880d681SAndroid Build Coastguard Worker Constant *GetConstantFoldFPValue(double V, Type *Ty) {
1364*9880d681SAndroid Build Coastguard Worker if (Ty->isHalfTy()) {
1365*9880d681SAndroid Build Coastguard Worker APFloat APF(V);
1366*9880d681SAndroid Build Coastguard Worker bool unused;
1367*9880d681SAndroid Build Coastguard Worker APF.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &unused);
1368*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), APF);
1369*9880d681SAndroid Build Coastguard Worker }
1370*9880d681SAndroid Build Coastguard Worker if (Ty->isFloatTy())
1371*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), APFloat((float)V));
1372*9880d681SAndroid Build Coastguard Worker if (Ty->isDoubleTy())
1373*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), APFloat(V));
1374*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Can only constant fold half/float/double");
1375*9880d681SAndroid Build Coastguard Worker }
1376*9880d681SAndroid Build Coastguard Worker
1377*9880d681SAndroid Build Coastguard Worker /// Clear the floating-point exception state.
llvm_fenv_clearexcept()1378*9880d681SAndroid Build Coastguard Worker inline void llvm_fenv_clearexcept() {
1379*9880d681SAndroid Build Coastguard Worker #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT
1380*9880d681SAndroid Build Coastguard Worker feclearexcept(FE_ALL_EXCEPT);
1381*9880d681SAndroid Build Coastguard Worker #endif
1382*9880d681SAndroid Build Coastguard Worker errno = 0;
1383*9880d681SAndroid Build Coastguard Worker }
1384*9880d681SAndroid Build Coastguard Worker
1385*9880d681SAndroid Build Coastguard Worker /// Test if a floating-point exception was raised.
llvm_fenv_testexcept()1386*9880d681SAndroid Build Coastguard Worker inline bool llvm_fenv_testexcept() {
1387*9880d681SAndroid Build Coastguard Worker int errno_val = errno;
1388*9880d681SAndroid Build Coastguard Worker if (errno_val == ERANGE || errno_val == EDOM)
1389*9880d681SAndroid Build Coastguard Worker return true;
1390*9880d681SAndroid Build Coastguard Worker #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT
1391*9880d681SAndroid Build Coastguard Worker if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
1392*9880d681SAndroid Build Coastguard Worker return true;
1393*9880d681SAndroid Build Coastguard Worker #endif
1394*9880d681SAndroid Build Coastguard Worker return false;
1395*9880d681SAndroid Build Coastguard Worker }
1396*9880d681SAndroid Build Coastguard Worker
ConstantFoldFP(double (* NativeFP)(double),double V,Type * Ty)1397*9880d681SAndroid Build Coastguard Worker Constant *ConstantFoldFP(double (*NativeFP)(double), double V, Type *Ty) {
1398*9880d681SAndroid Build Coastguard Worker llvm_fenv_clearexcept();
1399*9880d681SAndroid Build Coastguard Worker V = NativeFP(V);
1400*9880d681SAndroid Build Coastguard Worker if (llvm_fenv_testexcept()) {
1401*9880d681SAndroid Build Coastguard Worker llvm_fenv_clearexcept();
1402*9880d681SAndroid Build Coastguard Worker return nullptr;
1403*9880d681SAndroid Build Coastguard Worker }
1404*9880d681SAndroid Build Coastguard Worker
1405*9880d681SAndroid Build Coastguard Worker return GetConstantFoldFPValue(V, Ty);
1406*9880d681SAndroid Build Coastguard Worker }
1407*9880d681SAndroid Build Coastguard Worker
ConstantFoldBinaryFP(double (* NativeFP)(double,double),double V,double W,Type * Ty)1408*9880d681SAndroid Build Coastguard Worker Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), double V,
1409*9880d681SAndroid Build Coastguard Worker double W, Type *Ty) {
1410*9880d681SAndroid Build Coastguard Worker llvm_fenv_clearexcept();
1411*9880d681SAndroid Build Coastguard Worker V = NativeFP(V, W);
1412*9880d681SAndroid Build Coastguard Worker if (llvm_fenv_testexcept()) {
1413*9880d681SAndroid Build Coastguard Worker llvm_fenv_clearexcept();
1414*9880d681SAndroid Build Coastguard Worker return nullptr;
1415*9880d681SAndroid Build Coastguard Worker }
1416*9880d681SAndroid Build Coastguard Worker
1417*9880d681SAndroid Build Coastguard Worker return GetConstantFoldFPValue(V, Ty);
1418*9880d681SAndroid Build Coastguard Worker }
1419*9880d681SAndroid Build Coastguard Worker
1420*9880d681SAndroid Build Coastguard Worker /// Attempt to fold an SSE floating point to integer conversion of a constant
1421*9880d681SAndroid Build Coastguard Worker /// floating point. If roundTowardZero is false, the default IEEE rounding is
1422*9880d681SAndroid Build Coastguard Worker /// used (toward nearest, ties to even). This matches the behavior of the
1423*9880d681SAndroid Build Coastguard Worker /// non-truncating SSE instructions in the default rounding mode. The desired
1424*9880d681SAndroid Build Coastguard Worker /// integer type Ty is used to select how many bits are available for the
1425*9880d681SAndroid Build Coastguard Worker /// result. Returns null if the conversion cannot be performed, otherwise
1426*9880d681SAndroid Build Coastguard Worker /// returns the Constant value resulting from the conversion.
ConstantFoldConvertToInt(const APFloat & Val,bool roundTowardZero,Type * Ty)1427*9880d681SAndroid Build Coastguard Worker Constant *ConstantFoldConvertToInt(const APFloat &Val, bool roundTowardZero,
1428*9880d681SAndroid Build Coastguard Worker Type *Ty) {
1429*9880d681SAndroid Build Coastguard Worker // All of these conversion intrinsics form an integer of at most 64bits.
1430*9880d681SAndroid Build Coastguard Worker unsigned ResultWidth = Ty->getIntegerBitWidth();
1431*9880d681SAndroid Build Coastguard Worker assert(ResultWidth <= 64 &&
1432*9880d681SAndroid Build Coastguard Worker "Can only constant fold conversions to 64 and 32 bit ints");
1433*9880d681SAndroid Build Coastguard Worker
1434*9880d681SAndroid Build Coastguard Worker uint64_t UIntVal;
1435*9880d681SAndroid Build Coastguard Worker bool isExact = false;
1436*9880d681SAndroid Build Coastguard Worker APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
1437*9880d681SAndroid Build Coastguard Worker : APFloat::rmNearestTiesToEven;
1438*9880d681SAndroid Build Coastguard Worker APFloat::opStatus status = Val.convertToInteger(&UIntVal, ResultWidth,
1439*9880d681SAndroid Build Coastguard Worker /*isSigned=*/true, mode,
1440*9880d681SAndroid Build Coastguard Worker &isExact);
1441*9880d681SAndroid Build Coastguard Worker if (status != APFloat::opOK && status != APFloat::opInexact)
1442*9880d681SAndroid Build Coastguard Worker return nullptr;
1443*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(Ty, UIntVal, /*isSigned=*/true);
1444*9880d681SAndroid Build Coastguard Worker }
1445*9880d681SAndroid Build Coastguard Worker
getValueAsDouble(ConstantFP * Op)1446*9880d681SAndroid Build Coastguard Worker double getValueAsDouble(ConstantFP *Op) {
1447*9880d681SAndroid Build Coastguard Worker Type *Ty = Op->getType();
1448*9880d681SAndroid Build Coastguard Worker
1449*9880d681SAndroid Build Coastguard Worker if (Ty->isFloatTy())
1450*9880d681SAndroid Build Coastguard Worker return Op->getValueAPF().convertToFloat();
1451*9880d681SAndroid Build Coastguard Worker
1452*9880d681SAndroid Build Coastguard Worker if (Ty->isDoubleTy())
1453*9880d681SAndroid Build Coastguard Worker return Op->getValueAPF().convertToDouble();
1454*9880d681SAndroid Build Coastguard Worker
1455*9880d681SAndroid Build Coastguard Worker bool unused;
1456*9880d681SAndroid Build Coastguard Worker APFloat APF = Op->getValueAPF();
1457*9880d681SAndroid Build Coastguard Worker APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &unused);
1458*9880d681SAndroid Build Coastguard Worker return APF.convertToDouble();
1459*9880d681SAndroid Build Coastguard Worker }
1460*9880d681SAndroid Build Coastguard Worker
ConstantFoldScalarCall(StringRef Name,unsigned IntrinsicID,Type * Ty,ArrayRef<Constant * > Operands,const TargetLibraryInfo * TLI)1461*9880d681SAndroid Build Coastguard Worker Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, Type *Ty,
1462*9880d681SAndroid Build Coastguard Worker ArrayRef<Constant *> Operands,
1463*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
1464*9880d681SAndroid Build Coastguard Worker if (Operands.size() == 1) {
1465*9880d681SAndroid Build Coastguard Worker if (isa<UndefValue>(Operands[0])) {
1466*9880d681SAndroid Build Coastguard Worker // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN
1467*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::cos)
1468*9880d681SAndroid Build Coastguard Worker return Constant::getNullValue(Ty);
1469*9880d681SAndroid Build Coastguard Worker }
1470*9880d681SAndroid Build Coastguard Worker if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) {
1471*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::convert_to_fp16) {
1472*9880d681SAndroid Build Coastguard Worker APFloat Val(Op->getValueAPF());
1473*9880d681SAndroid Build Coastguard Worker
1474*9880d681SAndroid Build Coastguard Worker bool lost = false;
1475*9880d681SAndroid Build Coastguard Worker Val.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &lost);
1476*9880d681SAndroid Build Coastguard Worker
1477*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt());
1478*9880d681SAndroid Build Coastguard Worker }
1479*9880d681SAndroid Build Coastguard Worker
1480*9880d681SAndroid Build Coastguard Worker if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1481*9880d681SAndroid Build Coastguard Worker return nullptr;
1482*9880d681SAndroid Build Coastguard Worker
1483*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::round) {
1484*9880d681SAndroid Build Coastguard Worker APFloat V = Op->getValueAPF();
1485*9880d681SAndroid Build Coastguard Worker V.roundToIntegral(APFloat::rmNearestTiesToAway);
1486*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), V);
1487*9880d681SAndroid Build Coastguard Worker }
1488*9880d681SAndroid Build Coastguard Worker
1489*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::floor) {
1490*9880d681SAndroid Build Coastguard Worker APFloat V = Op->getValueAPF();
1491*9880d681SAndroid Build Coastguard Worker V.roundToIntegral(APFloat::rmTowardNegative);
1492*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), V);
1493*9880d681SAndroid Build Coastguard Worker }
1494*9880d681SAndroid Build Coastguard Worker
1495*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::ceil) {
1496*9880d681SAndroid Build Coastguard Worker APFloat V = Op->getValueAPF();
1497*9880d681SAndroid Build Coastguard Worker V.roundToIntegral(APFloat::rmTowardPositive);
1498*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), V);
1499*9880d681SAndroid Build Coastguard Worker }
1500*9880d681SAndroid Build Coastguard Worker
1501*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::trunc) {
1502*9880d681SAndroid Build Coastguard Worker APFloat V = Op->getValueAPF();
1503*9880d681SAndroid Build Coastguard Worker V.roundToIntegral(APFloat::rmTowardZero);
1504*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), V);
1505*9880d681SAndroid Build Coastguard Worker }
1506*9880d681SAndroid Build Coastguard Worker
1507*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::rint) {
1508*9880d681SAndroid Build Coastguard Worker APFloat V = Op->getValueAPF();
1509*9880d681SAndroid Build Coastguard Worker V.roundToIntegral(APFloat::rmNearestTiesToEven);
1510*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), V);
1511*9880d681SAndroid Build Coastguard Worker }
1512*9880d681SAndroid Build Coastguard Worker
1513*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::nearbyint) {
1514*9880d681SAndroid Build Coastguard Worker APFloat V = Op->getValueAPF();
1515*9880d681SAndroid Build Coastguard Worker V.roundToIntegral(APFloat::rmNearestTiesToEven);
1516*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), V);
1517*9880d681SAndroid Build Coastguard Worker }
1518*9880d681SAndroid Build Coastguard Worker
1519*9880d681SAndroid Build Coastguard Worker /// We only fold functions with finite arguments. Folding NaN and inf is
1520*9880d681SAndroid Build Coastguard Worker /// likely to be aborted with an exception anyway, and some host libms
1521*9880d681SAndroid Build Coastguard Worker /// have known errors raising exceptions.
1522*9880d681SAndroid Build Coastguard Worker if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity())
1523*9880d681SAndroid Build Coastguard Worker return nullptr;
1524*9880d681SAndroid Build Coastguard Worker
1525*9880d681SAndroid Build Coastguard Worker /// Currently APFloat versions of these functions do not exist, so we use
1526*9880d681SAndroid Build Coastguard Worker /// the host native double versions. Float versions are not called
1527*9880d681SAndroid Build Coastguard Worker /// directly but for all these it is true (float)(f((double)arg)) ==
1528*9880d681SAndroid Build Coastguard Worker /// f(arg). Long double not supported yet.
1529*9880d681SAndroid Build Coastguard Worker double V = getValueAsDouble(Op);
1530*9880d681SAndroid Build Coastguard Worker
1531*9880d681SAndroid Build Coastguard Worker switch (IntrinsicID) {
1532*9880d681SAndroid Build Coastguard Worker default: break;
1533*9880d681SAndroid Build Coastguard Worker case Intrinsic::fabs:
1534*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(fabs, V, Ty);
1535*9880d681SAndroid Build Coastguard Worker case Intrinsic::log2:
1536*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(Log2, V, Ty);
1537*9880d681SAndroid Build Coastguard Worker case Intrinsic::log:
1538*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(log, V, Ty);
1539*9880d681SAndroid Build Coastguard Worker case Intrinsic::log10:
1540*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(log10, V, Ty);
1541*9880d681SAndroid Build Coastguard Worker case Intrinsic::exp:
1542*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(exp, V, Ty);
1543*9880d681SAndroid Build Coastguard Worker case Intrinsic::exp2:
1544*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(exp2, V, Ty);
1545*9880d681SAndroid Build Coastguard Worker case Intrinsic::sin:
1546*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(sin, V, Ty);
1547*9880d681SAndroid Build Coastguard Worker case Intrinsic::cos:
1548*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(cos, V, Ty);
1549*9880d681SAndroid Build Coastguard Worker }
1550*9880d681SAndroid Build Coastguard Worker
1551*9880d681SAndroid Build Coastguard Worker if (!TLI)
1552*9880d681SAndroid Build Coastguard Worker return nullptr;
1553*9880d681SAndroid Build Coastguard Worker
1554*9880d681SAndroid Build Coastguard Worker switch (Name[0]) {
1555*9880d681SAndroid Build Coastguard Worker case 'a':
1556*9880d681SAndroid Build Coastguard Worker if ((Name == "acos" && TLI->has(LibFunc::acos)) ||
1557*9880d681SAndroid Build Coastguard Worker (Name == "acosf" && TLI->has(LibFunc::acosf)))
1558*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(acos, V, Ty);
1559*9880d681SAndroid Build Coastguard Worker else if ((Name == "asin" && TLI->has(LibFunc::asin)) ||
1560*9880d681SAndroid Build Coastguard Worker (Name == "asinf" && TLI->has(LibFunc::asinf)))
1561*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(asin, V, Ty);
1562*9880d681SAndroid Build Coastguard Worker else if ((Name == "atan" && TLI->has(LibFunc::atan)) ||
1563*9880d681SAndroid Build Coastguard Worker (Name == "atanf" && TLI->has(LibFunc::atanf)))
1564*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(atan, V, Ty);
1565*9880d681SAndroid Build Coastguard Worker break;
1566*9880d681SAndroid Build Coastguard Worker case 'c':
1567*9880d681SAndroid Build Coastguard Worker if ((Name == "ceil" && TLI->has(LibFunc::ceil)) ||
1568*9880d681SAndroid Build Coastguard Worker (Name == "ceilf" && TLI->has(LibFunc::ceilf)))
1569*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(ceil, V, Ty);
1570*9880d681SAndroid Build Coastguard Worker else if ((Name == "cos" && TLI->has(LibFunc::cos)) ||
1571*9880d681SAndroid Build Coastguard Worker (Name == "cosf" && TLI->has(LibFunc::cosf)))
1572*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(cos, V, Ty);
1573*9880d681SAndroid Build Coastguard Worker else if ((Name == "cosh" && TLI->has(LibFunc::cosh)) ||
1574*9880d681SAndroid Build Coastguard Worker (Name == "coshf" && TLI->has(LibFunc::coshf)))
1575*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(cosh, V, Ty);
1576*9880d681SAndroid Build Coastguard Worker break;
1577*9880d681SAndroid Build Coastguard Worker case 'e':
1578*9880d681SAndroid Build Coastguard Worker if ((Name == "exp" && TLI->has(LibFunc::exp)) ||
1579*9880d681SAndroid Build Coastguard Worker (Name == "expf" && TLI->has(LibFunc::expf)))
1580*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(exp, V, Ty);
1581*9880d681SAndroid Build Coastguard Worker if ((Name == "exp2" && TLI->has(LibFunc::exp2)) ||
1582*9880d681SAndroid Build Coastguard Worker (Name == "exp2f" && TLI->has(LibFunc::exp2f)))
1583*9880d681SAndroid Build Coastguard Worker // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
1584*9880d681SAndroid Build Coastguard Worker // C99 library.
1585*9880d681SAndroid Build Coastguard Worker return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
1586*9880d681SAndroid Build Coastguard Worker break;
1587*9880d681SAndroid Build Coastguard Worker case 'f':
1588*9880d681SAndroid Build Coastguard Worker if ((Name == "fabs" && TLI->has(LibFunc::fabs)) ||
1589*9880d681SAndroid Build Coastguard Worker (Name == "fabsf" && TLI->has(LibFunc::fabsf)))
1590*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(fabs, V, Ty);
1591*9880d681SAndroid Build Coastguard Worker else if ((Name == "floor" && TLI->has(LibFunc::floor)) ||
1592*9880d681SAndroid Build Coastguard Worker (Name == "floorf" && TLI->has(LibFunc::floorf)))
1593*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(floor, V, Ty);
1594*9880d681SAndroid Build Coastguard Worker break;
1595*9880d681SAndroid Build Coastguard Worker case 'l':
1596*9880d681SAndroid Build Coastguard Worker if ((Name == "log" && V > 0 && TLI->has(LibFunc::log)) ||
1597*9880d681SAndroid Build Coastguard Worker (Name == "logf" && V > 0 && TLI->has(LibFunc::logf)))
1598*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(log, V, Ty);
1599*9880d681SAndroid Build Coastguard Worker else if ((Name == "log10" && V > 0 && TLI->has(LibFunc::log10)) ||
1600*9880d681SAndroid Build Coastguard Worker (Name == "log10f" && V > 0 && TLI->has(LibFunc::log10f)))
1601*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(log10, V, Ty);
1602*9880d681SAndroid Build Coastguard Worker else if (IntrinsicID == Intrinsic::sqrt &&
1603*9880d681SAndroid Build Coastguard Worker (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) {
1604*9880d681SAndroid Build Coastguard Worker if (V >= -0.0)
1605*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(sqrt, V, Ty);
1606*9880d681SAndroid Build Coastguard Worker else {
1607*9880d681SAndroid Build Coastguard Worker // Unlike the sqrt definitions in C/C++, POSIX, and IEEE-754 - which
1608*9880d681SAndroid Build Coastguard Worker // all guarantee or favor returning NaN - the square root of a
1609*9880d681SAndroid Build Coastguard Worker // negative number is not defined for the LLVM sqrt intrinsic.
1610*9880d681SAndroid Build Coastguard Worker // This is because the intrinsic should only be emitted in place of
1611*9880d681SAndroid Build Coastguard Worker // libm's sqrt function when using "no-nans-fp-math".
1612*9880d681SAndroid Build Coastguard Worker return UndefValue::get(Ty);
1613*9880d681SAndroid Build Coastguard Worker }
1614*9880d681SAndroid Build Coastguard Worker }
1615*9880d681SAndroid Build Coastguard Worker break;
1616*9880d681SAndroid Build Coastguard Worker case 's':
1617*9880d681SAndroid Build Coastguard Worker if ((Name == "sin" && TLI->has(LibFunc::sin)) ||
1618*9880d681SAndroid Build Coastguard Worker (Name == "sinf" && TLI->has(LibFunc::sinf)))
1619*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(sin, V, Ty);
1620*9880d681SAndroid Build Coastguard Worker else if ((Name == "sinh" && TLI->has(LibFunc::sinh)) ||
1621*9880d681SAndroid Build Coastguard Worker (Name == "sinhf" && TLI->has(LibFunc::sinhf)))
1622*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(sinh, V, Ty);
1623*9880d681SAndroid Build Coastguard Worker else if ((Name == "sqrt" && V >= 0 && TLI->has(LibFunc::sqrt)) ||
1624*9880d681SAndroid Build Coastguard Worker (Name == "sqrtf" && V >= 0 && TLI->has(LibFunc::sqrtf)))
1625*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(sqrt, V, Ty);
1626*9880d681SAndroid Build Coastguard Worker break;
1627*9880d681SAndroid Build Coastguard Worker case 't':
1628*9880d681SAndroid Build Coastguard Worker if ((Name == "tan" && TLI->has(LibFunc::tan)) ||
1629*9880d681SAndroid Build Coastguard Worker (Name == "tanf" && TLI->has(LibFunc::tanf)))
1630*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(tan, V, Ty);
1631*9880d681SAndroid Build Coastguard Worker else if ((Name == "tanh" && TLI->has(LibFunc::tanh)) ||
1632*9880d681SAndroid Build Coastguard Worker (Name == "tanhf" && TLI->has(LibFunc::tanhf)))
1633*9880d681SAndroid Build Coastguard Worker return ConstantFoldFP(tanh, V, Ty);
1634*9880d681SAndroid Build Coastguard Worker break;
1635*9880d681SAndroid Build Coastguard Worker default:
1636*9880d681SAndroid Build Coastguard Worker break;
1637*9880d681SAndroid Build Coastguard Worker }
1638*9880d681SAndroid Build Coastguard Worker return nullptr;
1639*9880d681SAndroid Build Coastguard Worker }
1640*9880d681SAndroid Build Coastguard Worker
1641*9880d681SAndroid Build Coastguard Worker if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
1642*9880d681SAndroid Build Coastguard Worker switch (IntrinsicID) {
1643*9880d681SAndroid Build Coastguard Worker case Intrinsic::bswap:
1644*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
1645*9880d681SAndroid Build Coastguard Worker case Intrinsic::ctpop:
1646*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(Ty, Op->getValue().countPopulation());
1647*9880d681SAndroid Build Coastguard Worker case Intrinsic::bitreverse:
1648*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits());
1649*9880d681SAndroid Build Coastguard Worker case Intrinsic::convert_from_fp16: {
1650*9880d681SAndroid Build Coastguard Worker APFloat Val(APFloat::IEEEhalf, Op->getValue());
1651*9880d681SAndroid Build Coastguard Worker
1652*9880d681SAndroid Build Coastguard Worker bool lost = false;
1653*9880d681SAndroid Build Coastguard Worker APFloat::opStatus status = Val.convert(
1654*9880d681SAndroid Build Coastguard Worker Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &lost);
1655*9880d681SAndroid Build Coastguard Worker
1656*9880d681SAndroid Build Coastguard Worker // Conversion is always precise.
1657*9880d681SAndroid Build Coastguard Worker (void)status;
1658*9880d681SAndroid Build Coastguard Worker assert(status == APFloat::opOK && !lost &&
1659*9880d681SAndroid Build Coastguard Worker "Precision lost during fp16 constfolding");
1660*9880d681SAndroid Build Coastguard Worker
1661*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), Val);
1662*9880d681SAndroid Build Coastguard Worker }
1663*9880d681SAndroid Build Coastguard Worker default:
1664*9880d681SAndroid Build Coastguard Worker return nullptr;
1665*9880d681SAndroid Build Coastguard Worker }
1666*9880d681SAndroid Build Coastguard Worker }
1667*9880d681SAndroid Build Coastguard Worker
1668*9880d681SAndroid Build Coastguard Worker // Support ConstantVector in case we have an Undef in the top.
1669*9880d681SAndroid Build Coastguard Worker if (isa<ConstantVector>(Operands[0]) ||
1670*9880d681SAndroid Build Coastguard Worker isa<ConstantDataVector>(Operands[0])) {
1671*9880d681SAndroid Build Coastguard Worker auto *Op = cast<Constant>(Operands[0]);
1672*9880d681SAndroid Build Coastguard Worker switch (IntrinsicID) {
1673*9880d681SAndroid Build Coastguard Worker default: break;
1674*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse_cvtss2si:
1675*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse_cvtss2si64:
1676*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse2_cvtsd2si:
1677*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse2_cvtsd2si64:
1678*9880d681SAndroid Build Coastguard Worker if (ConstantFP *FPOp =
1679*9880d681SAndroid Build Coastguard Worker dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
1680*9880d681SAndroid Build Coastguard Worker return ConstantFoldConvertToInt(FPOp->getValueAPF(),
1681*9880d681SAndroid Build Coastguard Worker /*roundTowardZero=*/false, Ty);
1682*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse_cvttss2si:
1683*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse_cvttss2si64:
1684*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse2_cvttsd2si:
1685*9880d681SAndroid Build Coastguard Worker case Intrinsic::x86_sse2_cvttsd2si64:
1686*9880d681SAndroid Build Coastguard Worker if (ConstantFP *FPOp =
1687*9880d681SAndroid Build Coastguard Worker dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
1688*9880d681SAndroid Build Coastguard Worker return ConstantFoldConvertToInt(FPOp->getValueAPF(),
1689*9880d681SAndroid Build Coastguard Worker /*roundTowardZero=*/true, Ty);
1690*9880d681SAndroid Build Coastguard Worker }
1691*9880d681SAndroid Build Coastguard Worker }
1692*9880d681SAndroid Build Coastguard Worker
1693*9880d681SAndroid Build Coastguard Worker if (isa<UndefValue>(Operands[0])) {
1694*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::bswap)
1695*9880d681SAndroid Build Coastguard Worker return Operands[0];
1696*9880d681SAndroid Build Coastguard Worker return nullptr;
1697*9880d681SAndroid Build Coastguard Worker }
1698*9880d681SAndroid Build Coastguard Worker
1699*9880d681SAndroid Build Coastguard Worker return nullptr;
1700*9880d681SAndroid Build Coastguard Worker }
1701*9880d681SAndroid Build Coastguard Worker
1702*9880d681SAndroid Build Coastguard Worker if (Operands.size() == 2) {
1703*9880d681SAndroid Build Coastguard Worker if (auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
1704*9880d681SAndroid Build Coastguard Worker if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1705*9880d681SAndroid Build Coastguard Worker return nullptr;
1706*9880d681SAndroid Build Coastguard Worker double Op1V = getValueAsDouble(Op1);
1707*9880d681SAndroid Build Coastguard Worker
1708*9880d681SAndroid Build Coastguard Worker if (auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
1709*9880d681SAndroid Build Coastguard Worker if (Op2->getType() != Op1->getType())
1710*9880d681SAndroid Build Coastguard Worker return nullptr;
1711*9880d681SAndroid Build Coastguard Worker
1712*9880d681SAndroid Build Coastguard Worker double Op2V = getValueAsDouble(Op2);
1713*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::pow) {
1714*9880d681SAndroid Build Coastguard Worker return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
1715*9880d681SAndroid Build Coastguard Worker }
1716*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::copysign) {
1717*9880d681SAndroid Build Coastguard Worker APFloat V1 = Op1->getValueAPF();
1718*9880d681SAndroid Build Coastguard Worker const APFloat &V2 = Op2->getValueAPF();
1719*9880d681SAndroid Build Coastguard Worker V1.copySign(V2);
1720*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), V1);
1721*9880d681SAndroid Build Coastguard Worker }
1722*9880d681SAndroid Build Coastguard Worker
1723*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::minnum) {
1724*9880d681SAndroid Build Coastguard Worker const APFloat &C1 = Op1->getValueAPF();
1725*9880d681SAndroid Build Coastguard Worker const APFloat &C2 = Op2->getValueAPF();
1726*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), minnum(C1, C2));
1727*9880d681SAndroid Build Coastguard Worker }
1728*9880d681SAndroid Build Coastguard Worker
1729*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::maxnum) {
1730*9880d681SAndroid Build Coastguard Worker const APFloat &C1 = Op1->getValueAPF();
1731*9880d681SAndroid Build Coastguard Worker const APFloat &C2 = Op2->getValueAPF();
1732*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), maxnum(C1, C2));
1733*9880d681SAndroid Build Coastguard Worker }
1734*9880d681SAndroid Build Coastguard Worker
1735*9880d681SAndroid Build Coastguard Worker if (!TLI)
1736*9880d681SAndroid Build Coastguard Worker return nullptr;
1737*9880d681SAndroid Build Coastguard Worker if ((Name == "pow" && TLI->has(LibFunc::pow)) ||
1738*9880d681SAndroid Build Coastguard Worker (Name == "powf" && TLI->has(LibFunc::powf)))
1739*9880d681SAndroid Build Coastguard Worker return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
1740*9880d681SAndroid Build Coastguard Worker if ((Name == "fmod" && TLI->has(LibFunc::fmod)) ||
1741*9880d681SAndroid Build Coastguard Worker (Name == "fmodf" && TLI->has(LibFunc::fmodf)))
1742*9880d681SAndroid Build Coastguard Worker return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
1743*9880d681SAndroid Build Coastguard Worker if ((Name == "atan2" && TLI->has(LibFunc::atan2)) ||
1744*9880d681SAndroid Build Coastguard Worker (Name == "atan2f" && TLI->has(LibFunc::atan2f)))
1745*9880d681SAndroid Build Coastguard Worker return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
1746*9880d681SAndroid Build Coastguard Worker } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
1747*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
1748*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(),
1749*9880d681SAndroid Build Coastguard Worker APFloat((float)std::pow((float)Op1V,
1750*9880d681SAndroid Build Coastguard Worker (int)Op2C->getZExtValue())));
1751*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy())
1752*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(),
1753*9880d681SAndroid Build Coastguard Worker APFloat((float)std::pow((float)Op1V,
1754*9880d681SAndroid Build Coastguard Worker (int)Op2C->getZExtValue())));
1755*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy())
1756*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(),
1757*9880d681SAndroid Build Coastguard Worker APFloat((double)std::pow((double)Op1V,
1758*9880d681SAndroid Build Coastguard Worker (int)Op2C->getZExtValue())));
1759*9880d681SAndroid Build Coastguard Worker }
1760*9880d681SAndroid Build Coastguard Worker return nullptr;
1761*9880d681SAndroid Build Coastguard Worker }
1762*9880d681SAndroid Build Coastguard Worker
1763*9880d681SAndroid Build Coastguard Worker if (auto *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
1764*9880d681SAndroid Build Coastguard Worker if (auto *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
1765*9880d681SAndroid Build Coastguard Worker switch (IntrinsicID) {
1766*9880d681SAndroid Build Coastguard Worker default: break;
1767*9880d681SAndroid Build Coastguard Worker case Intrinsic::sadd_with_overflow:
1768*9880d681SAndroid Build Coastguard Worker case Intrinsic::uadd_with_overflow:
1769*9880d681SAndroid Build Coastguard Worker case Intrinsic::ssub_with_overflow:
1770*9880d681SAndroid Build Coastguard Worker case Intrinsic::usub_with_overflow:
1771*9880d681SAndroid Build Coastguard Worker case Intrinsic::smul_with_overflow:
1772*9880d681SAndroid Build Coastguard Worker case Intrinsic::umul_with_overflow: {
1773*9880d681SAndroid Build Coastguard Worker APInt Res;
1774*9880d681SAndroid Build Coastguard Worker bool Overflow;
1775*9880d681SAndroid Build Coastguard Worker switch (IntrinsicID) {
1776*9880d681SAndroid Build Coastguard Worker default: llvm_unreachable("Invalid case");
1777*9880d681SAndroid Build Coastguard Worker case Intrinsic::sadd_with_overflow:
1778*9880d681SAndroid Build Coastguard Worker Res = Op1->getValue().sadd_ov(Op2->getValue(), Overflow);
1779*9880d681SAndroid Build Coastguard Worker break;
1780*9880d681SAndroid Build Coastguard Worker case Intrinsic::uadd_with_overflow:
1781*9880d681SAndroid Build Coastguard Worker Res = Op1->getValue().uadd_ov(Op2->getValue(), Overflow);
1782*9880d681SAndroid Build Coastguard Worker break;
1783*9880d681SAndroid Build Coastguard Worker case Intrinsic::ssub_with_overflow:
1784*9880d681SAndroid Build Coastguard Worker Res = Op1->getValue().ssub_ov(Op2->getValue(), Overflow);
1785*9880d681SAndroid Build Coastguard Worker break;
1786*9880d681SAndroid Build Coastguard Worker case Intrinsic::usub_with_overflow:
1787*9880d681SAndroid Build Coastguard Worker Res = Op1->getValue().usub_ov(Op2->getValue(), Overflow);
1788*9880d681SAndroid Build Coastguard Worker break;
1789*9880d681SAndroid Build Coastguard Worker case Intrinsic::smul_with_overflow:
1790*9880d681SAndroid Build Coastguard Worker Res = Op1->getValue().smul_ov(Op2->getValue(), Overflow);
1791*9880d681SAndroid Build Coastguard Worker break;
1792*9880d681SAndroid Build Coastguard Worker case Intrinsic::umul_with_overflow:
1793*9880d681SAndroid Build Coastguard Worker Res = Op1->getValue().umul_ov(Op2->getValue(), Overflow);
1794*9880d681SAndroid Build Coastguard Worker break;
1795*9880d681SAndroid Build Coastguard Worker }
1796*9880d681SAndroid Build Coastguard Worker Constant *Ops[] = {
1797*9880d681SAndroid Build Coastguard Worker ConstantInt::get(Ty->getContext(), Res),
1798*9880d681SAndroid Build Coastguard Worker ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow)
1799*9880d681SAndroid Build Coastguard Worker };
1800*9880d681SAndroid Build Coastguard Worker return ConstantStruct::get(cast<StructType>(Ty), Ops);
1801*9880d681SAndroid Build Coastguard Worker }
1802*9880d681SAndroid Build Coastguard Worker case Intrinsic::cttz:
1803*9880d681SAndroid Build Coastguard Worker if (Op2->isOne() && Op1->isZero()) // cttz(0, 1) is undef.
1804*9880d681SAndroid Build Coastguard Worker return UndefValue::get(Ty);
1805*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(Ty, Op1->getValue().countTrailingZeros());
1806*9880d681SAndroid Build Coastguard Worker case Intrinsic::ctlz:
1807*9880d681SAndroid Build Coastguard Worker if (Op2->isOne() && Op1->isZero()) // ctlz(0, 1) is undef.
1808*9880d681SAndroid Build Coastguard Worker return UndefValue::get(Ty);
1809*9880d681SAndroid Build Coastguard Worker return ConstantInt::get(Ty, Op1->getValue().countLeadingZeros());
1810*9880d681SAndroid Build Coastguard Worker }
1811*9880d681SAndroid Build Coastguard Worker }
1812*9880d681SAndroid Build Coastguard Worker
1813*9880d681SAndroid Build Coastguard Worker return nullptr;
1814*9880d681SAndroid Build Coastguard Worker }
1815*9880d681SAndroid Build Coastguard Worker return nullptr;
1816*9880d681SAndroid Build Coastguard Worker }
1817*9880d681SAndroid Build Coastguard Worker
1818*9880d681SAndroid Build Coastguard Worker if (Operands.size() != 3)
1819*9880d681SAndroid Build Coastguard Worker return nullptr;
1820*9880d681SAndroid Build Coastguard Worker
1821*9880d681SAndroid Build Coastguard Worker if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
1822*9880d681SAndroid Build Coastguard Worker if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
1823*9880d681SAndroid Build Coastguard Worker if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
1824*9880d681SAndroid Build Coastguard Worker switch (IntrinsicID) {
1825*9880d681SAndroid Build Coastguard Worker default: break;
1826*9880d681SAndroid Build Coastguard Worker case Intrinsic::fma:
1827*9880d681SAndroid Build Coastguard Worker case Intrinsic::fmuladd: {
1828*9880d681SAndroid Build Coastguard Worker APFloat V = Op1->getValueAPF();
1829*9880d681SAndroid Build Coastguard Worker APFloat::opStatus s = V.fusedMultiplyAdd(Op2->getValueAPF(),
1830*9880d681SAndroid Build Coastguard Worker Op3->getValueAPF(),
1831*9880d681SAndroid Build Coastguard Worker APFloat::rmNearestTiesToEven);
1832*9880d681SAndroid Build Coastguard Worker if (s != APFloat::opInvalidOp)
1833*9880d681SAndroid Build Coastguard Worker return ConstantFP::get(Ty->getContext(), V);
1834*9880d681SAndroid Build Coastguard Worker
1835*9880d681SAndroid Build Coastguard Worker return nullptr;
1836*9880d681SAndroid Build Coastguard Worker }
1837*9880d681SAndroid Build Coastguard Worker }
1838*9880d681SAndroid Build Coastguard Worker }
1839*9880d681SAndroid Build Coastguard Worker }
1840*9880d681SAndroid Build Coastguard Worker }
1841*9880d681SAndroid Build Coastguard Worker
1842*9880d681SAndroid Build Coastguard Worker return nullptr;
1843*9880d681SAndroid Build Coastguard Worker }
1844*9880d681SAndroid Build Coastguard Worker
ConstantFoldVectorCall(StringRef Name,unsigned IntrinsicID,VectorType * VTy,ArrayRef<Constant * > Operands,const DataLayout & DL,const TargetLibraryInfo * TLI)1845*9880d681SAndroid Build Coastguard Worker Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID,
1846*9880d681SAndroid Build Coastguard Worker VectorType *VTy, ArrayRef<Constant *> Operands,
1847*9880d681SAndroid Build Coastguard Worker const DataLayout &DL,
1848*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
1849*9880d681SAndroid Build Coastguard Worker SmallVector<Constant *, 4> Result(VTy->getNumElements());
1850*9880d681SAndroid Build Coastguard Worker SmallVector<Constant *, 4> Lane(Operands.size());
1851*9880d681SAndroid Build Coastguard Worker Type *Ty = VTy->getElementType();
1852*9880d681SAndroid Build Coastguard Worker
1853*9880d681SAndroid Build Coastguard Worker if (IntrinsicID == Intrinsic::masked_load) {
1854*9880d681SAndroid Build Coastguard Worker auto *SrcPtr = Operands[0];
1855*9880d681SAndroid Build Coastguard Worker auto *Mask = Operands[2];
1856*9880d681SAndroid Build Coastguard Worker auto *Passthru = Operands[3];
1857*9880d681SAndroid Build Coastguard Worker
1858*9880d681SAndroid Build Coastguard Worker Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, VTy, DL);
1859*9880d681SAndroid Build Coastguard Worker
1860*9880d681SAndroid Build Coastguard Worker SmallVector<Constant *, 32> NewElements;
1861*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
1862*9880d681SAndroid Build Coastguard Worker auto *MaskElt = Mask->getAggregateElement(I);
1863*9880d681SAndroid Build Coastguard Worker if (!MaskElt)
1864*9880d681SAndroid Build Coastguard Worker break;
1865*9880d681SAndroid Build Coastguard Worker auto *PassthruElt = Passthru->getAggregateElement(I);
1866*9880d681SAndroid Build Coastguard Worker auto *VecElt = VecData ? VecData->getAggregateElement(I) : nullptr;
1867*9880d681SAndroid Build Coastguard Worker if (isa<UndefValue>(MaskElt)) {
1868*9880d681SAndroid Build Coastguard Worker if (PassthruElt)
1869*9880d681SAndroid Build Coastguard Worker NewElements.push_back(PassthruElt);
1870*9880d681SAndroid Build Coastguard Worker else if (VecElt)
1871*9880d681SAndroid Build Coastguard Worker NewElements.push_back(VecElt);
1872*9880d681SAndroid Build Coastguard Worker else
1873*9880d681SAndroid Build Coastguard Worker return nullptr;
1874*9880d681SAndroid Build Coastguard Worker }
1875*9880d681SAndroid Build Coastguard Worker if (MaskElt->isNullValue()) {
1876*9880d681SAndroid Build Coastguard Worker if (!PassthruElt)
1877*9880d681SAndroid Build Coastguard Worker return nullptr;
1878*9880d681SAndroid Build Coastguard Worker NewElements.push_back(PassthruElt);
1879*9880d681SAndroid Build Coastguard Worker } else if (MaskElt->isOneValue()) {
1880*9880d681SAndroid Build Coastguard Worker if (!VecElt)
1881*9880d681SAndroid Build Coastguard Worker return nullptr;
1882*9880d681SAndroid Build Coastguard Worker NewElements.push_back(VecElt);
1883*9880d681SAndroid Build Coastguard Worker } else {
1884*9880d681SAndroid Build Coastguard Worker return nullptr;
1885*9880d681SAndroid Build Coastguard Worker }
1886*9880d681SAndroid Build Coastguard Worker }
1887*9880d681SAndroid Build Coastguard Worker if (NewElements.size() != VTy->getNumElements())
1888*9880d681SAndroid Build Coastguard Worker return nullptr;
1889*9880d681SAndroid Build Coastguard Worker return ConstantVector::get(NewElements);
1890*9880d681SAndroid Build Coastguard Worker }
1891*9880d681SAndroid Build Coastguard Worker
1892*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
1893*9880d681SAndroid Build Coastguard Worker // Gather a column of constants.
1894*9880d681SAndroid Build Coastguard Worker for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
1895*9880d681SAndroid Build Coastguard Worker Constant *Agg = Operands[J]->getAggregateElement(I);
1896*9880d681SAndroid Build Coastguard Worker if (!Agg)
1897*9880d681SAndroid Build Coastguard Worker return nullptr;
1898*9880d681SAndroid Build Coastguard Worker
1899*9880d681SAndroid Build Coastguard Worker Lane[J] = Agg;
1900*9880d681SAndroid Build Coastguard Worker }
1901*9880d681SAndroid Build Coastguard Worker
1902*9880d681SAndroid Build Coastguard Worker // Use the regular scalar folding to simplify this column.
1903*9880d681SAndroid Build Coastguard Worker Constant *Folded = ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI);
1904*9880d681SAndroid Build Coastguard Worker if (!Folded)
1905*9880d681SAndroid Build Coastguard Worker return nullptr;
1906*9880d681SAndroid Build Coastguard Worker Result[I] = Folded;
1907*9880d681SAndroid Build Coastguard Worker }
1908*9880d681SAndroid Build Coastguard Worker
1909*9880d681SAndroid Build Coastguard Worker return ConstantVector::get(Result);
1910*9880d681SAndroid Build Coastguard Worker }
1911*9880d681SAndroid Build Coastguard Worker
1912*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
1913*9880d681SAndroid Build Coastguard Worker
1914*9880d681SAndroid Build Coastguard Worker Constant *
ConstantFoldCall(Function * F,ArrayRef<Constant * > Operands,const TargetLibraryInfo * TLI)1915*9880d681SAndroid Build Coastguard Worker llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
1916*9880d681SAndroid Build Coastguard Worker const TargetLibraryInfo *TLI) {
1917*9880d681SAndroid Build Coastguard Worker if (!F->hasName())
1918*9880d681SAndroid Build Coastguard Worker return nullptr;
1919*9880d681SAndroid Build Coastguard Worker StringRef Name = F->getName();
1920*9880d681SAndroid Build Coastguard Worker
1921*9880d681SAndroid Build Coastguard Worker Type *Ty = F->getReturnType();
1922*9880d681SAndroid Build Coastguard Worker
1923*9880d681SAndroid Build Coastguard Worker if (auto *VTy = dyn_cast<VectorType>(Ty))
1924*9880d681SAndroid Build Coastguard Worker return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands,
1925*9880d681SAndroid Build Coastguard Worker F->getParent()->getDataLayout(), TLI);
1926*9880d681SAndroid Build Coastguard Worker
1927*9880d681SAndroid Build Coastguard Worker return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI);
1928*9880d681SAndroid Build Coastguard Worker }
1929