File: | build/source/llvm/lib/Analysis/ConstantFolding.cpp |
Warning: | line 1102, column 9 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- ConstantFolding.cpp - Fold instructions into constants ------------===// | ||||
2 | // | ||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
6 | // | ||||
7 | //===----------------------------------------------------------------------===// | ||||
8 | // | ||||
9 | // This file defines routines for folding instructions into constants. | ||||
10 | // | ||||
11 | // Also, to supplement the basic IR ConstantExpr simplifications, | ||||
12 | // this file defines some additional folding routines that can make use of | ||||
13 | // DataLayout information. These functions cannot go in IR due to library | ||||
14 | // dependency issues. | ||||
15 | // | ||||
16 | //===----------------------------------------------------------------------===// | ||||
17 | |||||
18 | #include "llvm/Analysis/ConstantFolding.h" | ||||
19 | #include "llvm/ADT/APFloat.h" | ||||
20 | #include "llvm/ADT/APInt.h" | ||||
21 | #include "llvm/ADT/APSInt.h" | ||||
22 | #include "llvm/ADT/ArrayRef.h" | ||||
23 | #include "llvm/ADT/DenseMap.h" | ||||
24 | #include "llvm/ADT/STLExtras.h" | ||||
25 | #include "llvm/ADT/SmallVector.h" | ||||
26 | #include "llvm/ADT/StringRef.h" | ||||
27 | #include "llvm/Analysis/TargetFolder.h" | ||||
28 | #include "llvm/Analysis/TargetLibraryInfo.h" | ||||
29 | #include "llvm/Analysis/ValueTracking.h" | ||||
30 | #include "llvm/Analysis/VectorUtils.h" | ||||
31 | #include "llvm/Config/config.h" | ||||
32 | #include "llvm/IR/Constant.h" | ||||
33 | #include "llvm/IR/ConstantFold.h" | ||||
34 | #include "llvm/IR/Constants.h" | ||||
35 | #include "llvm/IR/DataLayout.h" | ||||
36 | #include "llvm/IR/DerivedTypes.h" | ||||
37 | #include "llvm/IR/Function.h" | ||||
38 | #include "llvm/IR/GlobalValue.h" | ||||
39 | #include "llvm/IR/GlobalVariable.h" | ||||
40 | #include "llvm/IR/InstrTypes.h" | ||||
41 | #include "llvm/IR/Instruction.h" | ||||
42 | #include "llvm/IR/Instructions.h" | ||||
43 | #include "llvm/IR/IntrinsicInst.h" | ||||
44 | #include "llvm/IR/Intrinsics.h" | ||||
45 | #include "llvm/IR/IntrinsicsAArch64.h" | ||||
46 | #include "llvm/IR/IntrinsicsAMDGPU.h" | ||||
47 | #include "llvm/IR/IntrinsicsARM.h" | ||||
48 | #include "llvm/IR/IntrinsicsWebAssembly.h" | ||||
49 | #include "llvm/IR/IntrinsicsX86.h" | ||||
50 | #include "llvm/IR/Operator.h" | ||||
51 | #include "llvm/IR/Type.h" | ||||
52 | #include "llvm/IR/Value.h" | ||||
53 | #include "llvm/Support/Casting.h" | ||||
54 | #include "llvm/Support/ErrorHandling.h" | ||||
55 | #include "llvm/Support/KnownBits.h" | ||||
56 | #include "llvm/Support/MathExtras.h" | ||||
57 | #include <cassert> | ||||
58 | #include <cerrno> | ||||
59 | #include <cfenv> | ||||
60 | #include <cmath> | ||||
61 | #include <cstdint> | ||||
62 | |||||
63 | using namespace llvm; | ||||
64 | |||||
65 | namespace { | ||||
66 | |||||
67 | //===----------------------------------------------------------------------===// | ||||
68 | // Constant Folding internal helper functions | ||||
69 | //===----------------------------------------------------------------------===// | ||||
70 | |||||
71 | static Constant *foldConstVectorToAPInt(APInt &Result, Type *DestTy, | ||||
72 | Constant *C, Type *SrcEltTy, | ||||
73 | unsigned NumSrcElts, | ||||
74 | const DataLayout &DL) { | ||||
75 | // Now that we know that the input value is a vector of integers, just shift | ||||
76 | // and insert them into our result. | ||||
77 | unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy); | ||||
78 | for (unsigned i = 0; i != NumSrcElts; ++i) { | ||||
79 | Constant *Element; | ||||
80 | if (DL.isLittleEndian()) | ||||
81 | Element = C->getAggregateElement(NumSrcElts - i - 1); | ||||
82 | else | ||||
83 | Element = C->getAggregateElement(i); | ||||
84 | |||||
85 | if (Element && isa<UndefValue>(Element)) { | ||||
86 | Result <<= BitShift; | ||||
87 | continue; | ||||
88 | } | ||||
89 | |||||
90 | auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element); | ||||
91 | if (!ElementCI) | ||||
92 | return ConstantExpr::getBitCast(C, DestTy); | ||||
93 | |||||
94 | Result <<= BitShift; | ||||
95 | Result |= ElementCI->getValue().zext(Result.getBitWidth()); | ||||
96 | } | ||||
97 | |||||
98 | return nullptr; | ||||
99 | } | ||||
100 | |||||
101 | /// Constant fold bitcast, symbolically evaluating it with DataLayout. | ||||
102 | /// This always returns a non-null constant, but it may be a | ||||
103 | /// ConstantExpr if unfoldable. | ||||
104 | Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { | ||||
105 | assert(CastInst::castIsValid(Instruction::BitCast, C, DestTy) &&(static_cast <bool> (CastInst::castIsValid(Instruction:: BitCast, C, DestTy) && "Invalid constantexpr bitcast!" ) ? void (0) : __assert_fail ("CastInst::castIsValid(Instruction::BitCast, C, DestTy) && \"Invalid constantexpr bitcast!\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 106, __extension__ __PRETTY_FUNCTION__)) | ||||
106 | "Invalid constantexpr bitcast!")(static_cast <bool> (CastInst::castIsValid(Instruction:: BitCast, C, DestTy) && "Invalid constantexpr bitcast!" ) ? void (0) : __assert_fail ("CastInst::castIsValid(Instruction::BitCast, C, DestTy) && \"Invalid constantexpr bitcast!\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 106, __extension__ __PRETTY_FUNCTION__)); | ||||
107 | |||||
108 | // Catch the obvious splat cases. | ||||
109 | if (Constant *Res = ConstantFoldLoadFromUniformValue(C, DestTy)) | ||||
110 | return Res; | ||||
111 | |||||
112 | if (auto *VTy = dyn_cast<VectorType>(C->getType())) { | ||||
113 | // Handle a vector->scalar integer/fp cast. | ||||
114 | if (isa<IntegerType>(DestTy) || DestTy->isFloatingPointTy()) { | ||||
115 | unsigned NumSrcElts = cast<FixedVectorType>(VTy)->getNumElements(); | ||||
116 | Type *SrcEltTy = VTy->getElementType(); | ||||
117 | |||||
118 | // If the vector is a vector of floating point, convert it to vector of int | ||||
119 | // to simplify things. | ||||
120 | if (SrcEltTy->isFloatingPointTy()) { | ||||
121 | unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits(); | ||||
122 | auto *SrcIVTy = FixedVectorType::get( | ||||
123 | IntegerType::get(C->getContext(), FPWidth), NumSrcElts); | ||||
124 | // Ask IR to do the conversion now that #elts line up. | ||||
125 | C = ConstantExpr::getBitCast(C, SrcIVTy); | ||||
126 | } | ||||
127 | |||||
128 | APInt Result(DL.getTypeSizeInBits(DestTy), 0); | ||||
129 | if (Constant *CE = foldConstVectorToAPInt(Result, DestTy, C, | ||||
130 | SrcEltTy, NumSrcElts, DL)) | ||||
131 | return CE; | ||||
132 | |||||
133 | if (isa<IntegerType>(DestTy)) | ||||
134 | return ConstantInt::get(DestTy, Result); | ||||
135 | |||||
136 | APFloat FP(DestTy->getFltSemantics(), Result); | ||||
137 | return ConstantFP::get(DestTy->getContext(), FP); | ||||
138 | } | ||||
139 | } | ||||
140 | |||||
141 | // The code below only handles casts to vectors currently. | ||||
142 | auto *DestVTy = dyn_cast<VectorType>(DestTy); | ||||
143 | if (!DestVTy) | ||||
144 | return ConstantExpr::getBitCast(C, DestTy); | ||||
145 | |||||
146 | // If this is a scalar -> vector cast, convert the input into a <1 x scalar> | ||||
147 | // vector so the code below can handle it uniformly. | ||||
148 | if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) { | ||||
149 | Constant *Ops = C; // don't take the address of C! | ||||
150 | return FoldBitCast(ConstantVector::get(Ops), DestTy, DL); | ||||
151 | } | ||||
152 | |||||
153 | // If this is a bitcast from constant vector -> vector, fold it. | ||||
154 | if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C)) | ||||
155 | return ConstantExpr::getBitCast(C, DestTy); | ||||
156 | |||||
157 | // If the element types match, IR can fold it. | ||||
158 | unsigned NumDstElt = cast<FixedVectorType>(DestVTy)->getNumElements(); | ||||
159 | unsigned NumSrcElt = cast<FixedVectorType>(C->getType())->getNumElements(); | ||||
160 | if (NumDstElt == NumSrcElt) | ||||
161 | return ConstantExpr::getBitCast(C, DestTy); | ||||
162 | |||||
163 | Type *SrcEltTy = cast<VectorType>(C->getType())->getElementType(); | ||||
164 | Type *DstEltTy = DestVTy->getElementType(); | ||||
165 | |||||
166 | // Otherwise, we're changing the number of elements in a vector, which | ||||
167 | // requires endianness information to do the right thing. For example, | ||||
168 | // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) | ||||
169 | // folds to (little endian): | ||||
170 | // <4 x i32> <i32 0, i32 0, i32 1, i32 0> | ||||
171 | // and to (big endian): | ||||
172 | // <4 x i32> <i32 0, i32 0, i32 0, i32 1> | ||||
173 | |||||
174 | // First thing is first. We only want to think about integer here, so if | ||||
175 | // we have something in FP form, recast it as integer. | ||||
176 | if (DstEltTy->isFloatingPointTy()) { | ||||
177 | // Fold to an vector of integers with same size as our FP type. | ||||
178 | unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits(); | ||||
179 | auto *DestIVTy = FixedVectorType::get( | ||||
180 | IntegerType::get(C->getContext(), FPWidth), NumDstElt); | ||||
181 | // Recursively handle this integer conversion, if possible. | ||||
182 | C = FoldBitCast(C, DestIVTy, DL); | ||||
183 | |||||
184 | // Finally, IR can handle this now that #elts line up. | ||||
185 | return ConstantExpr::getBitCast(C, DestTy); | ||||
186 | } | ||||
187 | |||||
188 | // Okay, we know the destination is integer, if the input is FP, convert | ||||
189 | // it to integer first. | ||||
190 | if (SrcEltTy->isFloatingPointTy()) { | ||||
191 | unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits(); | ||||
192 | auto *SrcIVTy = FixedVectorType::get( | ||||
193 | IntegerType::get(C->getContext(), FPWidth), NumSrcElt); | ||||
194 | // Ask IR to do the conversion now that #elts line up. | ||||
195 | C = ConstantExpr::getBitCast(C, SrcIVTy); | ||||
196 | // If IR wasn't able to fold it, bail out. | ||||
197 | if (!isa<ConstantVector>(C) && // FIXME: Remove ConstantVector. | ||||
198 | !isa<ConstantDataVector>(C)) | ||||
199 | return C; | ||||
200 | } | ||||
201 | |||||
202 | // Now we know that the input and output vectors are both integer vectors | ||||
203 | // of the same size, and that their #elements is not the same. Do the | ||||
204 | // conversion here, which depends on whether the input or output has | ||||
205 | // more elements. | ||||
206 | bool isLittleEndian = DL.isLittleEndian(); | ||||
207 | |||||
208 | SmallVector<Constant*, 32> Result; | ||||
209 | if (NumDstElt < NumSrcElt) { | ||||
210 | // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>) | ||||
211 | Constant *Zero = Constant::getNullValue(DstEltTy); | ||||
212 | unsigned Ratio = NumSrcElt/NumDstElt; | ||||
213 | unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits(); | ||||
214 | unsigned SrcElt = 0; | ||||
215 | for (unsigned i = 0; i != NumDstElt; ++i) { | ||||
216 | // Build each element of the result. | ||||
217 | Constant *Elt = Zero; | ||||
218 | unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1); | ||||
219 | for (unsigned j = 0; j != Ratio; ++j) { | ||||
220 | Constant *Src = C->getAggregateElement(SrcElt++); | ||||
221 | if (Src && isa<UndefValue>(Src)) | ||||
222 | Src = Constant::getNullValue( | ||||
223 | cast<VectorType>(C->getType())->getElementType()); | ||||
224 | else | ||||
225 | Src = dyn_cast_or_null<ConstantInt>(Src); | ||||
226 | if (!Src) // Reject constantexpr elements. | ||||
227 | return ConstantExpr::getBitCast(C, DestTy); | ||||
228 | |||||
229 | // Zero extend the element to the right size. | ||||
230 | Src = ConstantExpr::getZExt(Src, Elt->getType()); | ||||
231 | |||||
232 | // Shift it to the right place, depending on endianness. | ||||
233 | Src = ConstantExpr::getShl(Src, | ||||
234 | ConstantInt::get(Src->getType(), ShiftAmt)); | ||||
235 | ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize; | ||||
236 | |||||
237 | // Mix it in. | ||||
238 | Elt = ConstantExpr::getOr(Elt, Src); | ||||
239 | } | ||||
240 | Result.push_back(Elt); | ||||
241 | } | ||||
242 | return ConstantVector::get(Result); | ||||
243 | } | ||||
244 | |||||
245 | // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) | ||||
246 | unsigned Ratio = NumDstElt/NumSrcElt; | ||||
247 | unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy); | ||||
248 | |||||
249 | // Loop over each source value, expanding into multiple results. | ||||
250 | for (unsigned i = 0; i != NumSrcElt; ++i) { | ||||
251 | auto *Element = C->getAggregateElement(i); | ||||
252 | |||||
253 | if (!Element) // Reject constantexpr elements. | ||||
254 | return ConstantExpr::getBitCast(C, DestTy); | ||||
255 | |||||
256 | if (isa<UndefValue>(Element)) { | ||||
257 | // Correctly Propagate undef values. | ||||
258 | Result.append(Ratio, UndefValue::get(DstEltTy)); | ||||
259 | continue; | ||||
260 | } | ||||
261 | |||||
262 | auto *Src = dyn_cast<ConstantInt>(Element); | ||||
263 | if (!Src) | ||||
264 | return ConstantExpr::getBitCast(C, DestTy); | ||||
265 | |||||
266 | unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1); | ||||
267 | for (unsigned j = 0; j != Ratio; ++j) { | ||||
268 | // Shift the piece of the value into the right place, depending on | ||||
269 | // endianness. | ||||
270 | Constant *Elt = ConstantExpr::getLShr(Src, | ||||
271 | ConstantInt::get(Src->getType(), ShiftAmt)); | ||||
272 | ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize; | ||||
273 | |||||
274 | // Truncate the element to an integer with the same pointer size and | ||||
275 | // convert the element back to a pointer using a inttoptr. | ||||
276 | if (DstEltTy->isPointerTy()) { | ||||
277 | IntegerType *DstIntTy = Type::getIntNTy(C->getContext(), DstBitSize); | ||||
278 | Constant *CE = ConstantExpr::getTrunc(Elt, DstIntTy); | ||||
279 | Result.push_back(ConstantExpr::getIntToPtr(CE, DstEltTy)); | ||||
280 | continue; | ||||
281 | } | ||||
282 | |||||
283 | // Truncate and remember this piece. | ||||
284 | Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy)); | ||||
285 | } | ||||
286 | } | ||||
287 | |||||
288 | return ConstantVector::get(Result); | ||||
289 | } | ||||
290 | |||||
291 | } // end anonymous namespace | ||||
292 | |||||
293 | /// If this constant is a constant offset from a global, return the global and | ||||
294 | /// the constant. Because of constantexprs, this function is recursive. | ||||
295 | bool llvm::IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, | ||||
296 | APInt &Offset, const DataLayout &DL, | ||||
297 | DSOLocalEquivalent **DSOEquiv) { | ||||
298 | if (DSOEquiv) | ||||
299 | *DSOEquiv = nullptr; | ||||
300 | |||||
301 | // Trivial case, constant is the global. | ||||
302 | if ((GV = dyn_cast<GlobalValue>(C))) { | ||||
303 | unsigned BitWidth = DL.getIndexTypeSizeInBits(GV->getType()); | ||||
304 | Offset = APInt(BitWidth, 0); | ||||
305 | return true; | ||||
306 | } | ||||
307 | |||||
308 | if (auto *FoundDSOEquiv = dyn_cast<DSOLocalEquivalent>(C)) { | ||||
309 | if (DSOEquiv) | ||||
310 | *DSOEquiv = FoundDSOEquiv; | ||||
311 | GV = FoundDSOEquiv->getGlobalValue(); | ||||
312 | unsigned BitWidth = DL.getIndexTypeSizeInBits(GV->getType()); | ||||
313 | Offset = APInt(BitWidth, 0); | ||||
314 | return true; | ||||
315 | } | ||||
316 | |||||
317 | // Otherwise, if this isn't a constant expr, bail out. | ||||
318 | auto *CE = dyn_cast<ConstantExpr>(C); | ||||
319 | if (!CE) return false; | ||||
320 | |||||
321 | // Look through ptr->int and ptr->ptr casts. | ||||
322 | if (CE->getOpcode() == Instruction::PtrToInt || | ||||
323 | CE->getOpcode() == Instruction::BitCast) | ||||
324 | return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL, | ||||
325 | DSOEquiv); | ||||
326 | |||||
327 | // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) | ||||
328 | auto *GEP = dyn_cast<GEPOperator>(CE); | ||||
329 | if (!GEP) | ||||
330 | return false; | ||||
331 | |||||
332 | unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType()); | ||||
333 | APInt TmpOffset(BitWidth, 0); | ||||
334 | |||||
335 | // If the base isn't a global+constant, we aren't either. | ||||
336 | if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, DL, | ||||
337 | DSOEquiv)) | ||||
338 | return false; | ||||
339 | |||||
340 | // Otherwise, add any offset that our operands provide. | ||||
341 | if (!GEP->accumulateConstantOffset(DL, TmpOffset)) | ||||
342 | return false; | ||||
343 | |||||
344 | Offset = TmpOffset; | ||||
345 | return true; | ||||
346 | } | ||||
347 | |||||
348 | Constant *llvm::ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, | ||||
349 | const DataLayout &DL) { | ||||
350 | do { | ||||
351 | Type *SrcTy = C->getType(); | ||||
352 | if (SrcTy == DestTy) | ||||
353 | return C; | ||||
354 | |||||
355 | TypeSize DestSize = DL.getTypeSizeInBits(DestTy); | ||||
356 | TypeSize SrcSize = DL.getTypeSizeInBits(SrcTy); | ||||
357 | if (!TypeSize::isKnownGE(SrcSize, DestSize)) | ||||
358 | return nullptr; | ||||
359 | |||||
360 | // Catch the obvious splat cases (since all-zeros can coerce non-integral | ||||
361 | // pointers legally). | ||||
362 | if (Constant *Res = ConstantFoldLoadFromUniformValue(C, DestTy)) | ||||
363 | return Res; | ||||
364 | |||||
365 | // If the type sizes are the same and a cast is legal, just directly | ||||
366 | // cast the constant. | ||||
367 | // But be careful not to coerce non-integral pointers illegally. | ||||
368 | if (SrcSize == DestSize && | ||||
369 | DL.isNonIntegralPointerType(SrcTy->getScalarType()) == | ||||
370 | DL.isNonIntegralPointerType(DestTy->getScalarType())) { | ||||
371 | Instruction::CastOps Cast = Instruction::BitCast; | ||||
372 | // If we are going from a pointer to int or vice versa, we spell the cast | ||||
373 | // differently. | ||||
374 | if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) | ||||
375 | Cast = Instruction::IntToPtr; | ||||
376 | else if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) | ||||
377 | Cast = Instruction::PtrToInt; | ||||
378 | |||||
379 | if (CastInst::castIsValid(Cast, C, DestTy)) | ||||
380 | return ConstantExpr::getCast(Cast, C, DestTy); | ||||
381 | } | ||||
382 | |||||
383 | // If this isn't an aggregate type, there is nothing we can do to drill down | ||||
384 | // and find a bitcastable constant. | ||||
385 | if (!SrcTy->isAggregateType() && !SrcTy->isVectorTy()) | ||||
386 | return nullptr; | ||||
387 | |||||
388 | // We're simulating a load through a pointer that was bitcast to point to | ||||
389 | // a different type, so we can try to walk down through the initial | ||||
390 | // elements of an aggregate to see if some part of the aggregate is | ||||
391 | // castable to implement the "load" semantic model. | ||||
392 | if (SrcTy->isStructTy()) { | ||||
393 | // Struct types might have leading zero-length elements like [0 x i32], | ||||
394 | // which are certainly not what we are looking for, so skip them. | ||||
395 | unsigned Elem = 0; | ||||
396 | Constant *ElemC; | ||||
397 | do { | ||||
398 | ElemC = C->getAggregateElement(Elem++); | ||||
399 | } while (ElemC && DL.getTypeSizeInBits(ElemC->getType()).isZero()); | ||||
400 | C = ElemC; | ||||
401 | } else { | ||||
402 | // For non-byte-sized vector elements, the first element is not | ||||
403 | // necessarily located at the vector base address. | ||||
404 | if (auto *VT = dyn_cast<VectorType>(SrcTy)) | ||||
405 | if (!DL.typeSizeEqualsStoreSize(VT->getElementType())) | ||||
406 | return nullptr; | ||||
407 | |||||
408 | C = C->getAggregateElement(0u); | ||||
409 | } | ||||
410 | } while (C); | ||||
411 | |||||
412 | return nullptr; | ||||
413 | } | ||||
414 | |||||
415 | namespace { | ||||
416 | |||||
417 | /// Recursive helper to read bits out of global. C is the constant being copied | ||||
418 | /// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy | ||||
419 | /// results into and BytesLeft is the number of bytes left in | ||||
420 | /// the CurPtr buffer. DL is the DataLayout. | ||||
421 | bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr, | ||||
422 | unsigned BytesLeft, const DataLayout &DL) { | ||||
423 | assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) &&(static_cast <bool> (ByteOffset <= DL.getTypeAllocSize (C->getType()) && "Out of range access") ? void (0 ) : __assert_fail ("ByteOffset <= DL.getTypeAllocSize(C->getType()) && \"Out of range access\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 424, __extension__ __PRETTY_FUNCTION__)) | ||||
424 | "Out of range access")(static_cast <bool> (ByteOffset <= DL.getTypeAllocSize (C->getType()) && "Out of range access") ? void (0 ) : __assert_fail ("ByteOffset <= DL.getTypeAllocSize(C->getType()) && \"Out of range access\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 424, __extension__ __PRETTY_FUNCTION__)); | ||||
425 | |||||
426 | // If this element is zero or undefined, we can just return since *CurPtr is | ||||
427 | // zero initialized. | ||||
428 | if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) | ||||
429 | return true; | ||||
430 | |||||
431 | if (auto *CI = dyn_cast<ConstantInt>(C)) { | ||||
432 | if (CI->getBitWidth() > 64 || | ||||
433 | (CI->getBitWidth() & 7) != 0) | ||||
434 | return false; | ||||
435 | |||||
436 | uint64_t Val = CI->getZExtValue(); | ||||
437 | unsigned IntBytes = unsigned(CI->getBitWidth()/8); | ||||
438 | |||||
439 | for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) { | ||||
440 | int n = ByteOffset; | ||||
441 | if (!DL.isLittleEndian()) | ||||
442 | n = IntBytes - n - 1; | ||||
443 | CurPtr[i] = (unsigned char)(Val >> (n * 8)); | ||||
444 | ++ByteOffset; | ||||
445 | } | ||||
446 | return true; | ||||
447 | } | ||||
448 | |||||
449 | if (auto *CFP = dyn_cast<ConstantFP>(C)) { | ||||
450 | if (CFP->getType()->isDoubleTy()) { | ||||
451 | C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL); | ||||
452 | return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL); | ||||
453 | } | ||||
454 | if (CFP->getType()->isFloatTy()){ | ||||
455 | C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), DL); | ||||
456 | return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL); | ||||
457 | } | ||||
458 | if (CFP->getType()->isHalfTy()){ | ||||
459 | C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), DL); | ||||
460 | return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL); | ||||
461 | } | ||||
462 | return false; | ||||
463 | } | ||||
464 | |||||
465 | if (auto *CS = dyn_cast<ConstantStruct>(C)) { | ||||
466 | const StructLayout *SL = DL.getStructLayout(CS->getType()); | ||||
467 | unsigned Index = SL->getElementContainingOffset(ByteOffset); | ||||
468 | uint64_t CurEltOffset = SL->getElementOffset(Index); | ||||
469 | ByteOffset -= CurEltOffset; | ||||
470 | |||||
471 | while (true) { | ||||
472 | // If the element access is to the element itself and not to tail padding, | ||||
473 | // read the bytes from the element. | ||||
474 | uint64_t EltSize = DL.getTypeAllocSize(CS->getOperand(Index)->getType()); | ||||
475 | |||||
476 | if (ByteOffset < EltSize && | ||||
477 | !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr, | ||||
478 | BytesLeft, DL)) | ||||
479 | return false; | ||||
480 | |||||
481 | ++Index; | ||||
482 | |||||
483 | // Check to see if we read from the last struct element, if so we're done. | ||||
484 | if (Index == CS->getType()->getNumElements()) | ||||
485 | return true; | ||||
486 | |||||
487 | // If we read all of the bytes we needed from this element we're done. | ||||
488 | uint64_t NextEltOffset = SL->getElementOffset(Index); | ||||
489 | |||||
490 | if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset) | ||||
491 | return true; | ||||
492 | |||||
493 | // Move to the next element of the struct. | ||||
494 | CurPtr += NextEltOffset - CurEltOffset - ByteOffset; | ||||
495 | BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset; | ||||
496 | ByteOffset = 0; | ||||
497 | CurEltOffset = NextEltOffset; | ||||
498 | } | ||||
499 | // not reached. | ||||
500 | } | ||||
501 | |||||
502 | if (isa<ConstantArray>(C) || isa<ConstantVector>(C) || | ||||
503 | isa<ConstantDataSequential>(C)) { | ||||
504 | uint64_t NumElts; | ||||
505 | Type *EltTy; | ||||
506 | if (auto *AT = dyn_cast<ArrayType>(C->getType())) { | ||||
507 | NumElts = AT->getNumElements(); | ||||
508 | EltTy = AT->getElementType(); | ||||
509 | } else { | ||||
510 | NumElts = cast<FixedVectorType>(C->getType())->getNumElements(); | ||||
511 | EltTy = cast<FixedVectorType>(C->getType())->getElementType(); | ||||
512 | } | ||||
513 | uint64_t EltSize = DL.getTypeAllocSize(EltTy); | ||||
514 | uint64_t Index = ByteOffset / EltSize; | ||||
515 | uint64_t Offset = ByteOffset - Index * EltSize; | ||||
516 | |||||
517 | for (; Index != NumElts; ++Index) { | ||||
518 | if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr, | ||||
519 | BytesLeft, DL)) | ||||
520 | return false; | ||||
521 | |||||
522 | uint64_t BytesWritten = EltSize - Offset; | ||||
523 | assert(BytesWritten <= EltSize && "Not indexing into this element?")(static_cast <bool> (BytesWritten <= EltSize && "Not indexing into this element?") ? void (0) : __assert_fail ("BytesWritten <= EltSize && \"Not indexing into this element?\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 523, __extension__ __PRETTY_FUNCTION__)); | ||||
524 | if (BytesWritten >= BytesLeft) | ||||
525 | return true; | ||||
526 | |||||
527 | Offset = 0; | ||||
528 | BytesLeft -= BytesWritten; | ||||
529 | CurPtr += BytesWritten; | ||||
530 | } | ||||
531 | return true; | ||||
532 | } | ||||
533 | |||||
534 | if (auto *CE = dyn_cast<ConstantExpr>(C)) { | ||||
535 | if (CE->getOpcode() == Instruction::IntToPtr && | ||||
536 | CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) { | ||||
537 | return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr, | ||||
538 | BytesLeft, DL); | ||||
539 | } | ||||
540 | } | ||||
541 | |||||
542 | // Otherwise, unknown initializer type. | ||||
543 | return false; | ||||
544 | } | ||||
545 | |||||
546 | Constant *FoldReinterpretLoadFromConst(Constant *C, Type *LoadTy, | ||||
547 | int64_t Offset, const DataLayout &DL) { | ||||
548 | // Bail out early. Not expect to load from scalable global variable. | ||||
549 | if (isa<ScalableVectorType>(LoadTy)) | ||||
550 | return nullptr; | ||||
551 | |||||
552 | auto *IntType = dyn_cast<IntegerType>(LoadTy); | ||||
553 | |||||
554 | // If this isn't an integer load we can't fold it directly. | ||||
555 | if (!IntType) { | ||||
556 | // If this is a non-integer load, we can try folding it as an int load and | ||||
557 | // then bitcast the result. This can be useful for union cases. Note | ||||
558 | // that address spaces don't matter here since we're not going to result in | ||||
559 | // an actual new load. | ||||
560 | if (!LoadTy->isFloatingPointTy() && !LoadTy->isPointerTy() && | ||||
561 | !LoadTy->isVectorTy()) | ||||
562 | return nullptr; | ||||
563 | |||||
564 | Type *MapTy = Type::getIntNTy(C->getContext(), | ||||
565 | DL.getTypeSizeInBits(LoadTy).getFixedValue()); | ||||
566 | if (Constant *Res = FoldReinterpretLoadFromConst(C, MapTy, Offset, DL)) { | ||||
567 | if (Res->isNullValue() && !LoadTy->isX86_MMXTy() && | ||||
568 | !LoadTy->isX86_AMXTy()) | ||||
569 | // Materializing a zero can be done trivially without a bitcast | ||||
570 | return Constant::getNullValue(LoadTy); | ||||
571 | Type *CastTy = LoadTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(LoadTy) : LoadTy; | ||||
572 | Res = FoldBitCast(Res, CastTy, DL); | ||||
573 | if (LoadTy->isPtrOrPtrVectorTy()) { | ||||
574 | // For vector of pointer, we needed to first convert to a vector of integer, then do vector inttoptr | ||||
575 | if (Res->isNullValue() && !LoadTy->isX86_MMXTy() && | ||||
576 | !LoadTy->isX86_AMXTy()) | ||||
577 | return Constant::getNullValue(LoadTy); | ||||
578 | if (DL.isNonIntegralPointerType(LoadTy->getScalarType())) | ||||
579 | // Be careful not to replace a load of an addrspace value with an inttoptr here | ||||
580 | return nullptr; | ||||
581 | Res = ConstantExpr::getCast(Instruction::IntToPtr, Res, LoadTy); | ||||
582 | } | ||||
583 | return Res; | ||||
584 | } | ||||
585 | return nullptr; | ||||
586 | } | ||||
587 | |||||
588 | unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8; | ||||
589 | if (BytesLoaded > 32 || BytesLoaded == 0) | ||||
590 | return nullptr; | ||||
591 | |||||
592 | // If we're not accessing anything in this constant, the result is undefined. | ||||
593 | if (Offset <= -1 * static_cast<int64_t>(BytesLoaded)) | ||||
594 | return PoisonValue::get(IntType); | ||||
595 | |||||
596 | // TODO: We should be able to support scalable types. | ||||
597 | TypeSize InitializerSize = DL.getTypeAllocSize(C->getType()); | ||||
598 | if (InitializerSize.isScalable()) | ||||
599 | return nullptr; | ||||
600 | |||||
601 | // If we're not accessing anything in this constant, the result is undefined. | ||||
602 | if (Offset >= (int64_t)InitializerSize.getFixedValue()) | ||||
603 | return PoisonValue::get(IntType); | ||||
604 | |||||
605 | unsigned char RawBytes[32] = {0}; | ||||
606 | unsigned char *CurPtr = RawBytes; | ||||
607 | unsigned BytesLeft = BytesLoaded; | ||||
608 | |||||
609 | // If we're loading off the beginning of the global, some bytes may be valid. | ||||
610 | if (Offset < 0) { | ||||
611 | CurPtr += -Offset; | ||||
612 | BytesLeft += Offset; | ||||
613 | Offset = 0; | ||||
614 | } | ||||
615 | |||||
616 | if (!ReadDataFromGlobal(C, Offset, CurPtr, BytesLeft, DL)) | ||||
617 | return nullptr; | ||||
618 | |||||
619 | APInt ResultVal = APInt(IntType->getBitWidth(), 0); | ||||
620 | if (DL.isLittleEndian()) { | ||||
621 | ResultVal = RawBytes[BytesLoaded - 1]; | ||||
622 | for (unsigned i = 1; i != BytesLoaded; ++i) { | ||||
623 | ResultVal <<= 8; | ||||
624 | ResultVal |= RawBytes[BytesLoaded - 1 - i]; | ||||
625 | } | ||||
626 | } else { | ||||
627 | ResultVal = RawBytes[0]; | ||||
628 | for (unsigned i = 1; i != BytesLoaded; ++i) { | ||||
629 | ResultVal <<= 8; | ||||
630 | ResultVal |= RawBytes[i]; | ||||
631 | } | ||||
632 | } | ||||
633 | |||||
634 | return ConstantInt::get(IntType->getContext(), ResultVal); | ||||
635 | } | ||||
636 | |||||
637 | } // anonymous namespace | ||||
638 | |||||
639 | // If GV is a constant with an initializer read its representation starting | ||||
640 | // at Offset and return it as a constant array of unsigned char. Otherwise | ||||
641 | // return null. | ||||
642 | Constant *llvm::ReadByteArrayFromGlobal(const GlobalVariable *GV, | ||||
643 | uint64_t Offset) { | ||||
644 | if (!GV->isConstant() || !GV->hasDefinitiveInitializer()) | ||||
645 | return nullptr; | ||||
646 | |||||
647 | const DataLayout &DL = GV->getParent()->getDataLayout(); | ||||
648 | Constant *Init = const_cast<Constant *>(GV->getInitializer()); | ||||
649 | TypeSize InitSize = DL.getTypeAllocSize(Init->getType()); | ||||
650 | if (InitSize < Offset) | ||||
651 | return nullptr; | ||||
652 | |||||
653 | uint64_t NBytes = InitSize - Offset; | ||||
654 | if (NBytes > UINT16_MAX(65535)) | ||||
655 | // Bail for large initializers in excess of 64K to avoid allocating | ||||
656 | // too much memory. | ||||
657 | // Offset is assumed to be less than or equal than InitSize (this | ||||
658 | // is enforced in ReadDataFromGlobal). | ||||
659 | return nullptr; | ||||
660 | |||||
661 | SmallVector<unsigned char, 256> RawBytes(static_cast<size_t>(NBytes)); | ||||
662 | unsigned char *CurPtr = RawBytes.data(); | ||||
663 | |||||
664 | if (!ReadDataFromGlobal(Init, Offset, CurPtr, NBytes, DL)) | ||||
665 | return nullptr; | ||||
666 | |||||
667 | return ConstantDataArray::get(GV->getContext(), RawBytes); | ||||
668 | } | ||||
669 | |||||
670 | /// If this Offset points exactly to the start of an aggregate element, return | ||||
671 | /// that element, otherwise return nullptr. | ||||
672 | Constant *getConstantAtOffset(Constant *Base, APInt Offset, | ||||
673 | const DataLayout &DL) { | ||||
674 | if (Offset.isZero()) | ||||
675 | return Base; | ||||
676 | |||||
677 | if (!isa<ConstantAggregate>(Base) && !isa<ConstantDataSequential>(Base)) | ||||
678 | return nullptr; | ||||
679 | |||||
680 | Type *ElemTy = Base->getType(); | ||||
681 | SmallVector<APInt> Indices = DL.getGEPIndicesForOffset(ElemTy, Offset); | ||||
682 | if (!Offset.isZero() || !Indices[0].isZero()) | ||||
683 | return nullptr; | ||||
684 | |||||
685 | Constant *C = Base; | ||||
686 | for (const APInt &Index : drop_begin(Indices)) { | ||||
687 | if (Index.isNegative() || Index.getActiveBits() >= 32) | ||||
688 | return nullptr; | ||||
689 | |||||
690 | C = C->getAggregateElement(Index.getZExtValue()); | ||||
691 | if (!C) | ||||
692 | return nullptr; | ||||
693 | } | ||||
694 | |||||
695 | return C; | ||||
696 | } | ||||
697 | |||||
698 | Constant *llvm::ConstantFoldLoadFromConst(Constant *C, Type *Ty, | ||||
699 | const APInt &Offset, | ||||
700 | const DataLayout &DL) { | ||||
701 | if (Constant *AtOffset = getConstantAtOffset(C, Offset, DL)) | ||||
702 | if (Constant *Result = ConstantFoldLoadThroughBitcast(AtOffset, Ty, DL)) | ||||
703 | return Result; | ||||
704 | |||||
705 | // Explicitly check for out-of-bounds access, so we return poison even if the | ||||
706 | // constant is a uniform value. | ||||
707 | TypeSize Size = DL.getTypeAllocSize(C->getType()); | ||||
708 | if (!Size.isScalable() && Offset.sge(Size.getFixedValue())) | ||||
709 | return PoisonValue::get(Ty); | ||||
710 | |||||
711 | // Try an offset-independent fold of a uniform value. | ||||
712 | if (Constant *Result = ConstantFoldLoadFromUniformValue(C, Ty)) | ||||
713 | return Result; | ||||
714 | |||||
715 | // Try hard to fold loads from bitcasted strange and non-type-safe things. | ||||
716 | if (Offset.getSignificantBits() <= 64) | ||||
717 | if (Constant *Result = | ||||
718 | FoldReinterpretLoadFromConst(C, Ty, Offset.getSExtValue(), DL)) | ||||
719 | return Result; | ||||
720 | |||||
721 | return nullptr; | ||||
722 | } | ||||
723 | |||||
724 | Constant *llvm::ConstantFoldLoadFromConst(Constant *C, Type *Ty, | ||||
725 | const DataLayout &DL) { | ||||
726 | return ConstantFoldLoadFromConst(C, Ty, APInt(64, 0), DL); | ||||
727 | } | ||||
728 | |||||
729 | Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, | ||||
730 | APInt Offset, | ||||
731 | const DataLayout &DL) { | ||||
732 | // We can only fold loads from constant globals with a definitive initializer. | ||||
733 | // Check this upfront, to skip expensive offset calculations. | ||||
734 | auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(C)); | ||||
735 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) | ||||
736 | return nullptr; | ||||
737 | |||||
738 | C = cast<Constant>(C->stripAndAccumulateConstantOffsets( | ||||
739 | DL, Offset, /* AllowNonInbounds */ true)); | ||||
740 | |||||
741 | if (C == GV) | ||||
742 | if (Constant *Result = ConstantFoldLoadFromConst(GV->getInitializer(), Ty, | ||||
743 | Offset, DL)) | ||||
744 | return Result; | ||||
745 | |||||
746 | // If this load comes from anywhere in a uniform constant global, the value | ||||
747 | // is always the same, regardless of the loaded offset. | ||||
748 | return ConstantFoldLoadFromUniformValue(GV->getInitializer(), Ty); | ||||
749 | } | ||||
750 | |||||
751 | Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, | ||||
752 | const DataLayout &DL) { | ||||
753 | APInt Offset(DL.getIndexTypeSizeInBits(C->getType()), 0); | ||||
754 | return ConstantFoldLoadFromConstPtr(C, Ty, Offset, DL); | ||||
755 | } | ||||
756 | |||||
757 | Constant *llvm::ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty) { | ||||
758 | if (isa<PoisonValue>(C)) | ||||
759 | return PoisonValue::get(Ty); | ||||
760 | if (isa<UndefValue>(C)) | ||||
761 | return UndefValue::get(Ty); | ||||
762 | if (C->isNullValue() && !Ty->isX86_MMXTy() && !Ty->isX86_AMXTy()) | ||||
763 | return Constant::getNullValue(Ty); | ||||
764 | if (C->isAllOnesValue() && | ||||
765 | (Ty->isIntOrIntVectorTy() || Ty->isFPOrFPVectorTy())) | ||||
766 | return Constant::getAllOnesValue(Ty); | ||||
767 | return nullptr; | ||||
768 | } | ||||
769 | |||||
770 | namespace { | ||||
771 | |||||
772 | /// One of Op0/Op1 is a constant expression. | ||||
773 | /// Attempt to symbolically evaluate the result of a binary operator merging | ||||
774 | /// these together. If target data info is available, it is provided as DL, | ||||
775 | /// otherwise DL is null. | ||||
776 | Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1, | ||||
777 | const DataLayout &DL) { | ||||
778 | // SROA | ||||
779 | |||||
780 | // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl. | ||||
781 | // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute | ||||
782 | // bits. | ||||
783 | |||||
784 | if (Opc == Instruction::And) { | ||||
785 | KnownBits Known0 = computeKnownBits(Op0, DL); | ||||
786 | KnownBits Known1 = computeKnownBits(Op1, DL); | ||||
787 | if ((Known1.One | Known0.Zero).isAllOnes()) { | ||||
788 | // All the bits of Op0 that the 'and' could be masking are already zero. | ||||
789 | return Op0; | ||||
790 | } | ||||
791 | if ((Known0.One | Known1.Zero).isAllOnes()) { | ||||
792 | // All the bits of Op1 that the 'and' could be masking are already zero. | ||||
793 | return Op1; | ||||
794 | } | ||||
795 | |||||
796 | Known0 &= Known1; | ||||
797 | if (Known0.isConstant()) | ||||
798 | return ConstantInt::get(Op0->getType(), Known0.getConstant()); | ||||
799 | } | ||||
800 | |||||
801 | // If the constant expr is something like &A[123] - &A[4].f, fold this into a | ||||
802 | // constant. This happens frequently when iterating over a global array. | ||||
803 | if (Opc == Instruction::Sub) { | ||||
804 | GlobalValue *GV1, *GV2; | ||||
805 | APInt Offs1, Offs2; | ||||
806 | |||||
807 | if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, DL)) | ||||
808 | if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, DL) && GV1 == GV2) { | ||||
809 | unsigned OpSize = DL.getTypeSizeInBits(Op0->getType()); | ||||
810 | |||||
811 | // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow. | ||||
812 | // PtrToInt may change the bitwidth so we have convert to the right size | ||||
813 | // first. | ||||
814 | return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) - | ||||
815 | Offs2.zextOrTrunc(OpSize)); | ||||
816 | } | ||||
817 | } | ||||
818 | |||||
819 | return nullptr; | ||||
820 | } | ||||
821 | |||||
822 | /// If array indices are not pointer-sized integers, explicitly cast them so | ||||
823 | /// that they aren't implicitly casted by the getelementptr. | ||||
824 | Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops, | ||||
825 | Type *ResultTy, std::optional<unsigned> InRangeIndex, | ||||
826 | const DataLayout &DL, const TargetLibraryInfo *TLI) { | ||||
827 | Type *IntIdxTy = DL.getIndexType(ResultTy); | ||||
828 | Type *IntIdxScalarTy = IntIdxTy->getScalarType(); | ||||
829 | |||||
830 | bool Any = false; | ||||
831 | SmallVector<Constant*, 32> NewIdxs; | ||||
832 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) { | ||||
833 | if ((i == 1 || | ||||
834 | !isa<StructType>(GetElementPtrInst::getIndexedType( | ||||
835 | SrcElemTy, Ops.slice(1, i - 1)))) && | ||||
836 | Ops[i]->getType()->getScalarType() != IntIdxScalarTy) { | ||||
837 | Any = true; | ||||
838 | Type *NewType = Ops[i]->getType()->isVectorTy() | ||||
839 | ? IntIdxTy | ||||
840 | : IntIdxScalarTy; | ||||
841 | NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i], | ||||
842 | true, | ||||
843 | NewType, | ||||
844 | true), | ||||
845 | Ops[i], NewType)); | ||||
846 | } else | ||||
847 | NewIdxs.push_back(Ops[i]); | ||||
848 | } | ||||
849 | |||||
850 | if (!Any) | ||||
851 | return nullptr; | ||||
852 | |||||
853 | Constant *C = ConstantExpr::getGetElementPtr( | ||||
854 | SrcElemTy, Ops[0], NewIdxs, /*InBounds=*/false, InRangeIndex); | ||||
855 | return ConstantFoldConstant(C, DL, TLI); | ||||
856 | } | ||||
857 | |||||
858 | /// Strip the pointer casts, but preserve the address space information. | ||||
859 | Constant *StripPtrCastKeepAS(Constant *Ptr) { | ||||
860 | assert(Ptr->getType()->isPointerTy() && "Not a pointer type")(static_cast <bool> (Ptr->getType()->isPointerTy( ) && "Not a pointer type") ? void (0) : __assert_fail ("Ptr->getType()->isPointerTy() && \"Not a pointer type\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 860, __extension__ __PRETTY_FUNCTION__)); | ||||
861 | auto *OldPtrTy = cast<PointerType>(Ptr->getType()); | ||||
862 | Ptr = cast<Constant>(Ptr->stripPointerCasts()); | ||||
863 | auto *NewPtrTy = cast<PointerType>(Ptr->getType()); | ||||
864 | |||||
865 | // Preserve the address space number of the pointer. | ||||
866 | if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) { | ||||
867 | Ptr = ConstantExpr::getPointerCast( | ||||
868 | Ptr, PointerType::getWithSamePointeeType(NewPtrTy, | ||||
869 | OldPtrTy->getAddressSpace())); | ||||
870 | } | ||||
871 | return Ptr; | ||||
872 | } | ||||
873 | |||||
874 | /// If we can symbolically evaluate the GEP constant expression, do so. | ||||
875 | Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP, | ||||
876 | ArrayRef<Constant *> Ops, | ||||
877 | const DataLayout &DL, | ||||
878 | const TargetLibraryInfo *TLI) { | ||||
879 | const GEPOperator *InnermostGEP = GEP; | ||||
880 | bool InBounds = GEP->isInBounds(); | ||||
881 | |||||
882 | Type *SrcElemTy = GEP->getSourceElementType(); | ||||
883 | Type *ResElemTy = GEP->getResultElementType(); | ||||
884 | Type *ResTy = GEP->getType(); | ||||
885 | if (!SrcElemTy->isSized() || isa<ScalableVectorType>(SrcElemTy)) | ||||
886 | return nullptr; | ||||
887 | |||||
888 | if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy, | ||||
889 | GEP->getInRangeIndex(), DL, TLI)) | ||||
890 | return C; | ||||
891 | |||||
892 | Constant *Ptr = Ops[0]; | ||||
893 | if (!Ptr->getType()->isPointerTy()) | ||||
894 | return nullptr; | ||||
895 | |||||
896 | Type *IntIdxTy = DL.getIndexType(Ptr->getType()); | ||||
897 | |||||
898 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) | ||||
899 | if (!isa<ConstantInt>(Ops[i])) | ||||
900 | return nullptr; | ||||
901 | |||||
902 | unsigned BitWidth = DL.getTypeSizeInBits(IntIdxTy); | ||||
903 | APInt Offset = APInt( | ||||
904 | BitWidth, | ||||
905 | DL.getIndexedOffsetInType( | ||||
906 | SrcElemTy, ArrayRef((Value *const *)Ops.data() + 1, Ops.size() - 1))); | ||||
907 | Ptr = StripPtrCastKeepAS(Ptr); | ||||
908 | |||||
909 | // If this is a GEP of a GEP, fold it all into a single GEP. | ||||
910 | while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) { | ||||
911 | InnermostGEP = GEP; | ||||
912 | InBounds &= GEP->isInBounds(); | ||||
913 | |||||
914 | SmallVector<Value *, 4> NestedOps(llvm::drop_begin(GEP->operands())); | ||||
915 | |||||
916 | // Do not try the incorporate the sub-GEP if some index is not a number. | ||||
917 | bool AllConstantInt = true; | ||||
918 | for (Value *NestedOp : NestedOps) | ||||
919 | if (!isa<ConstantInt>(NestedOp)) { | ||||
920 | AllConstantInt = false; | ||||
921 | break; | ||||
922 | } | ||||
923 | if (!AllConstantInt) | ||||
924 | break; | ||||
925 | |||||
926 | Ptr = cast<Constant>(GEP->getOperand(0)); | ||||
927 | SrcElemTy = GEP->getSourceElementType(); | ||||
928 | Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps)); | ||||
929 | Ptr = StripPtrCastKeepAS(Ptr); | ||||
930 | } | ||||
931 | |||||
932 | // If the base value for this address is a literal integer value, fold the | ||||
933 | // getelementptr to the resulting integer value casted to the pointer type. | ||||
934 | APInt BasePtr(BitWidth, 0); | ||||
935 | if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) { | ||||
936 | if (CE->getOpcode() == Instruction::IntToPtr) { | ||||
937 | if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0))) | ||||
938 | BasePtr = Base->getValue().zextOrTrunc(BitWidth); | ||||
939 | } | ||||
940 | } | ||||
941 | |||||
942 | auto *PTy = cast<PointerType>(Ptr->getType()); | ||||
943 | if ((Ptr->isNullValue() || BasePtr != 0) && | ||||
944 | !DL.isNonIntegralPointerType(PTy)) { | ||||
945 | Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr); | ||||
946 | return ConstantExpr::getIntToPtr(C, ResTy); | ||||
947 | } | ||||
948 | |||||
949 | // Otherwise form a regular getelementptr. Recompute the indices so that | ||||
950 | // we eliminate over-indexing of the notional static type array bounds. | ||||
951 | // This makes it easy to determine if the getelementptr is "inbounds". | ||||
952 | // Also, this helps GlobalOpt do SROA on GlobalVariables. | ||||
953 | |||||
954 | // For GEPs of GlobalValues, use the value type even for opaque pointers. | ||||
955 | // Otherwise use an i8 GEP. | ||||
956 | if (auto *GV = dyn_cast<GlobalValue>(Ptr)) | ||||
957 | SrcElemTy = GV->getValueType(); | ||||
958 | else if (!PTy->isOpaque()) | ||||
959 | SrcElemTy = PTy->getNonOpaquePointerElementType(); | ||||
960 | else | ||||
961 | SrcElemTy = Type::getInt8Ty(Ptr->getContext()); | ||||
962 | |||||
963 | if (!SrcElemTy->isSized()) | ||||
964 | return nullptr; | ||||
965 | |||||
966 | Type *ElemTy = SrcElemTy; | ||||
967 | SmallVector<APInt> Indices = DL.getGEPIndicesForOffset(ElemTy, Offset); | ||||
968 | if (Offset != 0) | ||||
969 | return nullptr; | ||||
970 | |||||
971 | // Try to add additional zero indices to reach the desired result element | ||||
972 | // type. | ||||
973 | // TODO: Should we avoid extra zero indices if ResElemTy can't be reached and | ||||
974 | // we'll have to insert a bitcast anyway? | ||||
975 | while (ElemTy != ResElemTy) { | ||||
976 | Type *NextTy = GetElementPtrInst::getTypeAtIndex(ElemTy, (uint64_t)0); | ||||
977 | if (!NextTy) | ||||
978 | break; | ||||
979 | |||||
980 | Indices.push_back(APInt::getZero(isa<StructType>(ElemTy) ? 32 : BitWidth)); | ||||
981 | ElemTy = NextTy; | ||||
982 | } | ||||
983 | |||||
984 | SmallVector<Constant *, 32> NewIdxs; | ||||
985 | for (const APInt &Index : Indices) | ||||
986 | NewIdxs.push_back(ConstantInt::get( | ||||
987 | Type::getIntNTy(Ptr->getContext(), Index.getBitWidth()), Index)); | ||||
988 | |||||
989 | // Preserve the inrange index from the innermost GEP if possible. We must | ||||
990 | // have calculated the same indices up to and including the inrange index. | ||||
991 | std::optional<unsigned> InRangeIndex; | ||||
992 | if (std::optional<unsigned> LastIRIndex = InnermostGEP->getInRangeIndex()) | ||||
993 | if (SrcElemTy == InnermostGEP->getSourceElementType() && | ||||
994 | NewIdxs.size() > *LastIRIndex) { | ||||
995 | InRangeIndex = LastIRIndex; | ||||
996 | for (unsigned I = 0; I <= *LastIRIndex; ++I) | ||||
997 | if (NewIdxs[I] != InnermostGEP->getOperand(I + 1)) | ||||
998 | return nullptr; | ||||
999 | } | ||||
1000 | |||||
1001 | // Create a GEP. | ||||
1002 | Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs, | ||||
1003 | InBounds, InRangeIndex); | ||||
1004 | assert((static_cast <bool> (cast<PointerType>(C->getType ())->isOpaqueOrPointeeTypeMatches(ElemTy) && "Computed GetElementPtr has unexpected type!" ) ? void (0) : __assert_fail ("cast<PointerType>(C->getType())->isOpaqueOrPointeeTypeMatches(ElemTy) && \"Computed GetElementPtr has unexpected type!\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 1006, __extension__ __PRETTY_FUNCTION__)) | ||||
1005 | cast<PointerType>(C->getType())->isOpaqueOrPointeeTypeMatches(ElemTy) &&(static_cast <bool> (cast<PointerType>(C->getType ())->isOpaqueOrPointeeTypeMatches(ElemTy) && "Computed GetElementPtr has unexpected type!" ) ? void (0) : __assert_fail ("cast<PointerType>(C->getType())->isOpaqueOrPointeeTypeMatches(ElemTy) && \"Computed GetElementPtr has unexpected type!\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 1006, __extension__ __PRETTY_FUNCTION__)) | ||||
1006 | "Computed GetElementPtr has unexpected type!")(static_cast <bool> (cast<PointerType>(C->getType ())->isOpaqueOrPointeeTypeMatches(ElemTy) && "Computed GetElementPtr has unexpected type!" ) ? void (0) : __assert_fail ("cast<PointerType>(C->getType())->isOpaqueOrPointeeTypeMatches(ElemTy) && \"Computed GetElementPtr has unexpected type!\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 1006, __extension__ __PRETTY_FUNCTION__)); | ||||
1007 | |||||
1008 | // If we ended up indexing a member with a type that doesn't match | ||||
1009 | // the type of what the original indices indexed, add a cast. | ||||
1010 | if (C->getType() != ResTy) | ||||
1011 | C = FoldBitCast(C, ResTy, DL); | ||||
1012 | |||||
1013 | return C; | ||||
1014 | } | ||||
1015 | |||||
1016 | /// Attempt to constant fold an instruction with the | ||||
1017 | /// specified opcode and operands. If successful, the constant result is | ||||
1018 | /// returned, if not, null is returned. Note that this function can fail when | ||||
1019 | /// attempting to fold instructions like loads and stores, which have no | ||||
1020 | /// constant expression form. | ||||
1021 | Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode, | ||||
1022 | ArrayRef<Constant *> Ops, | ||||
1023 | const DataLayout &DL, | ||||
1024 | const TargetLibraryInfo *TLI) { | ||||
1025 | Type *DestTy = InstOrCE->getType(); | ||||
1026 | |||||
1027 | if (Instruction::isUnaryOp(Opcode)) | ||||
1028 | return ConstantFoldUnaryOpOperand(Opcode, Ops[0], DL); | ||||
1029 | |||||
1030 | if (Instruction::isBinaryOp(Opcode)) { | ||||
1031 | switch (Opcode) { | ||||
1032 | default: | ||||
1033 | break; | ||||
1034 | case Instruction::FAdd: | ||||
1035 | case Instruction::FSub: | ||||
1036 | case Instruction::FMul: | ||||
1037 | case Instruction::FDiv: | ||||
1038 | case Instruction::FRem: | ||||
1039 | // Handle floating point instructions separately to account for denormals | ||||
1040 | // TODO: If a constant expression is being folded rather than an | ||||
1041 | // instruction, denormals will not be flushed/treated as zero | ||||
1042 | if (const auto *I = dyn_cast<Instruction>(InstOrCE)) { | ||||
1043 | return ConstantFoldFPInstOperands(Opcode, Ops[0], Ops[1], DL, I); | ||||
1044 | } | ||||
1045 | } | ||||
1046 | return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL); | ||||
1047 | } | ||||
1048 | |||||
1049 | if (Instruction::isCast(Opcode)) | ||||
1050 | return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL); | ||||
1051 | |||||
1052 | if (auto *GEP
| ||||
1053 | if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI)) | ||||
1054 | return C; | ||||
1055 | |||||
1056 | return ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), Ops[0], | ||||
1057 | Ops.slice(1), GEP->isInBounds(), | ||||
1058 | GEP->getInRangeIndex()); | ||||
1059 | } | ||||
1060 | |||||
1061 | if (auto *CE
| ||||
1062 | if (CE->isCompare()) | ||||
1063 | return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1], | ||||
1064 | DL, TLI); | ||||
1065 | return CE->getWithOperands(Ops); | ||||
1066 | } | ||||
1067 | |||||
1068 | switch (Opcode) { | ||||
1069 | default: return nullptr; | ||||
1070 | case Instruction::ICmp: | ||||
1071 | case Instruction::FCmp: { | ||||
1072 | auto *C = cast<CmpInst>(InstOrCE); | ||||
1073 | return ConstantFoldCompareInstOperands(C->getPredicate(), Ops[0], Ops[1], | ||||
1074 | DL, TLI, C); | ||||
1075 | } | ||||
1076 | case Instruction::Freeze: | ||||
1077 | return isGuaranteedNotToBeUndefOrPoison(Ops[0]) ? Ops[0] : nullptr; | ||||
1078 | case Instruction::Call: | ||||
1079 | if (auto *F = dyn_cast<Function>(Ops.back())) { | ||||
1080 | const auto *Call = cast<CallBase>(InstOrCE); | ||||
1081 | if (canConstantFoldCallTo(Call, F)) | ||||
1082 | return ConstantFoldCall(Call, F, Ops.slice(0, Ops.size() - 1), TLI); | ||||
1083 | } | ||||
1084 | return nullptr; | ||||
1085 | case Instruction::Select: | ||||
1086 | return ConstantFoldSelectInstruction(Ops[0], Ops[1], Ops[2]); | ||||
1087 | case Instruction::ExtractElement: | ||||
1088 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); | ||||
1089 | case Instruction::ExtractValue: | ||||
1090 | return ConstantFoldExtractValueInstruction( | ||||
1091 | Ops[0], cast<ExtractValueInst>(InstOrCE)->getIndices()); | ||||
1092 | case Instruction::InsertElement: | ||||
1093 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); | ||||
1094 | case Instruction::InsertValue: | ||||
1095 | return ConstantFoldInsertValueInstruction( | ||||
1096 | Ops[0], Ops[1], cast<InsertValueInst>(InstOrCE)->getIndices()); | ||||
1097 | case Instruction::ShuffleVector: | ||||
1098 | return ConstantExpr::getShuffleVector( | ||||
1099 | Ops[0], Ops[1], cast<ShuffleVectorInst>(InstOrCE)->getShuffleMask()); | ||||
1100 | case Instruction::Load: { | ||||
1101 | const auto *LI = dyn_cast<LoadInst>(InstOrCE); | ||||
1102 | if (LI->isVolatile()) | ||||
| |||||
1103 | return nullptr; | ||||
1104 | return ConstantFoldLoadFromConstPtr(Ops[0], LI->getType(), DL); | ||||
1105 | } | ||||
1106 | } | ||||
1107 | } | ||||
1108 | |||||
1109 | } // end anonymous namespace | ||||
1110 | |||||
1111 | //===----------------------------------------------------------------------===// | ||||
1112 | // Constant Folding public APIs | ||||
1113 | //===----------------------------------------------------------------------===// | ||||
1114 | |||||
1115 | namespace { | ||||
1116 | |||||
1117 | Constant * | ||||
1118 | ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL, | ||||
1119 | const TargetLibraryInfo *TLI, | ||||
1120 | SmallDenseMap<Constant *, Constant *> &FoldedOps) { | ||||
1121 | if (!isa<ConstantVector>(C) && !isa<ConstantExpr>(C)) | ||||
1122 | return const_cast<Constant *>(C); | ||||
1123 | |||||
1124 | SmallVector<Constant *, 8> Ops; | ||||
1125 | for (const Use &OldU : C->operands()) { | ||||
1126 | Constant *OldC = cast<Constant>(&OldU); | ||||
1127 | Constant *NewC = OldC; | ||||
1128 | // Recursively fold the ConstantExpr's operands. If we have already folded | ||||
1129 | // a ConstantExpr, we don't have to process it again. | ||||
1130 | if (isa<ConstantVector>(OldC) || isa<ConstantExpr>(OldC)) { | ||||
1131 | auto It = FoldedOps.find(OldC); | ||||
1132 | if (It == FoldedOps.end()) { | ||||
1133 | NewC = ConstantFoldConstantImpl(OldC, DL, TLI, FoldedOps); | ||||
1134 | FoldedOps.insert({OldC, NewC}); | ||||
1135 | } else { | ||||
1136 | NewC = It->second; | ||||
1137 | } | ||||
1138 | } | ||||
1139 | Ops.push_back(NewC); | ||||
1140 | } | ||||
1141 | |||||
1142 | if (auto *CE
| ||||
1143 | if (Constant *Res = | ||||
1144 | ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI)) | ||||
1145 | return Res; | ||||
1146 | return const_cast<Constant *>(C); | ||||
1147 | } | ||||
1148 | |||||
1149 | assert(isa<ConstantVector>(C))(static_cast <bool> (isa<ConstantVector>(C)) ? void (0) : __assert_fail ("isa<ConstantVector>(C)", "llvm/lib/Analysis/ConstantFolding.cpp" , 1149, __extension__ __PRETTY_FUNCTION__)); | ||||
1150 | return ConstantVector::get(Ops); | ||||
1151 | } | ||||
1152 | |||||
1153 | } // end anonymous namespace | ||||
1154 | |||||
1155 | Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL, | ||||
1156 | const TargetLibraryInfo *TLI) { | ||||
1157 | // Handle PHI nodes quickly here... | ||||
1158 | if (auto *PN = dyn_cast<PHINode>(I)) { | ||||
1159 | Constant *CommonValue = nullptr; | ||||
1160 | |||||
1161 | SmallDenseMap<Constant *, Constant *> FoldedOps; | ||||
1162 | for (Value *Incoming : PN->incoming_values()) { | ||||
1163 | // If the incoming value is undef then skip it. Note that while we could | ||||
1164 | // skip the value if it is equal to the phi node itself we choose not to | ||||
1165 | // because that would break the rule that constant folding only applies if | ||||
1166 | // all operands are constants. | ||||
1167 | if (isa<UndefValue>(Incoming)) | ||||
1168 | continue; | ||||
1169 | // If the incoming value is not a constant, then give up. | ||||
1170 | auto *C = dyn_cast<Constant>(Incoming); | ||||
1171 | if (!C) | ||||
1172 | return nullptr; | ||||
1173 | // Fold the PHI's operands. | ||||
1174 | C = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps); | ||||
1175 | // If the incoming value is a different constant to | ||||
1176 | // the one we saw previously, then give up. | ||||
1177 | if (CommonValue && C != CommonValue) | ||||
1178 | return nullptr; | ||||
1179 | CommonValue = C; | ||||
1180 | } | ||||
1181 | |||||
1182 | // If we reach here, all incoming values are the same constant or undef. | ||||
1183 | return CommonValue ? CommonValue : UndefValue::get(PN->getType()); | ||||
1184 | } | ||||
1185 | |||||
1186 | // Scan the operand list, checking to see if they are all constants, if so, | ||||
1187 | // hand off to ConstantFoldInstOperandsImpl. | ||||
1188 | if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); })) | ||||
1189 | return nullptr; | ||||
1190 | |||||
1191 | SmallDenseMap<Constant *, Constant *> FoldedOps; | ||||
1192 | SmallVector<Constant *, 8> Ops; | ||||
1193 | for (const Use &OpU : I->operands()) { | ||||
1194 | auto *Op = cast<Constant>(&OpU); | ||||
1195 | // Fold the Instruction's operands. | ||||
1196 | Op = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps); | ||||
1197 | Ops.push_back(Op); | ||||
1198 | } | ||||
1199 | |||||
1200 | return ConstantFoldInstOperands(I, Ops, DL, TLI); | ||||
1201 | } | ||||
1202 | |||||
1203 | Constant *llvm::ConstantFoldConstant(const Constant *C, const DataLayout &DL, | ||||
1204 | const TargetLibraryInfo *TLI) { | ||||
1205 | SmallDenseMap<Constant *, Constant *> FoldedOps; | ||||
1206 | return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps); | ||||
| |||||
1207 | } | ||||
1208 | |||||
1209 | Constant *llvm::ConstantFoldInstOperands(Instruction *I, | ||||
1210 | ArrayRef<Constant *> Ops, | ||||
1211 | const DataLayout &DL, | ||||
1212 | const TargetLibraryInfo *TLI) { | ||||
1213 | return ConstantFoldInstOperandsImpl(I, I->getOpcode(), Ops, DL, TLI); | ||||
1214 | } | ||||
1215 | |||||
1216 | Constant *llvm::ConstantFoldCompareInstOperands( | ||||
1217 | unsigned IntPredicate, Constant *Ops0, Constant *Ops1, const DataLayout &DL, | ||||
1218 | const TargetLibraryInfo *TLI, const Instruction *I) { | ||||
1219 | CmpInst::Predicate Predicate = (CmpInst::Predicate)IntPredicate; | ||||
1220 | // fold: icmp (inttoptr x), null -> icmp x, 0 | ||||
1221 | // fold: icmp null, (inttoptr x) -> icmp 0, x | ||||
1222 | // fold: icmp (ptrtoint x), 0 -> icmp x, null | ||||
1223 | // fold: icmp 0, (ptrtoint x) -> icmp null, x | ||||
1224 | // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y | ||||
1225 | // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y | ||||
1226 | // | ||||
1227 | // FIXME: The following comment is out of data and the DataLayout is here now. | ||||
1228 | // ConstantExpr::getCompare cannot do this, because it doesn't have DL | ||||
1229 | // around to know if bit truncation is happening. | ||||
1230 | if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) { | ||||
1231 | if (Ops1->isNullValue()) { | ||||
1232 | if (CE0->getOpcode() == Instruction::IntToPtr) { | ||||
1233 | Type *IntPtrTy = DL.getIntPtrType(CE0->getType()); | ||||
1234 | // Convert the integer value to the right size to ensure we get the | ||||
1235 | // proper extension or truncation. | ||||
1236 | Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0), | ||||
1237 | IntPtrTy, false); | ||||
1238 | Constant *Null = Constant::getNullValue(C->getType()); | ||||
1239 | return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI); | ||||
1240 | } | ||||
1241 | |||||
1242 | // Only do this transformation if the int is intptrty in size, otherwise | ||||
1243 | // there is a truncation or extension that we aren't modeling. | ||||
1244 | if (CE0->getOpcode() == Instruction::PtrToInt) { | ||||
1245 | Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType()); | ||||
1246 | if (CE0->getType() == IntPtrTy) { | ||||
1247 | Constant *C = CE0->getOperand(0); | ||||
1248 | Constant *Null = Constant::getNullValue(C->getType()); | ||||
1249 | return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI); | ||||
1250 | } | ||||
1251 | } | ||||
1252 | } | ||||
1253 | |||||
1254 | if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) { | ||||
1255 | if (CE0->getOpcode() == CE1->getOpcode()) { | ||||
1256 | if (CE0->getOpcode() == Instruction::IntToPtr) { | ||||
1257 | Type *IntPtrTy = DL.getIntPtrType(CE0->getType()); | ||||
1258 | |||||
1259 | // Convert the integer value to the right size to ensure we get the | ||||
1260 | // proper extension or truncation. | ||||
1261 | Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0), | ||||
1262 | IntPtrTy, false); | ||||
1263 | Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0), | ||||
1264 | IntPtrTy, false); | ||||
1265 | return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI); | ||||
1266 | } | ||||
1267 | |||||
1268 | // Only do this transformation if the int is intptrty in size, otherwise | ||||
1269 | // there is a truncation or extension that we aren't modeling. | ||||
1270 | if (CE0->getOpcode() == Instruction::PtrToInt) { | ||||
1271 | Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType()); | ||||
1272 | if (CE0->getType() == IntPtrTy && | ||||
1273 | CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) { | ||||
1274 | return ConstantFoldCompareInstOperands( | ||||
1275 | Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI); | ||||
1276 | } | ||||
1277 | } | ||||
1278 | } | ||||
1279 | } | ||||
1280 | |||||
1281 | // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0) | ||||
1282 | // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0) | ||||
1283 | if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) && | ||||
1284 | CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) { | ||||
1285 | Constant *LHS = ConstantFoldCompareInstOperands( | ||||
1286 | Predicate, CE0->getOperand(0), Ops1, DL, TLI); | ||||
1287 | Constant *RHS = ConstantFoldCompareInstOperands( | ||||
1288 | Predicate, CE0->getOperand(1), Ops1, DL, TLI); | ||||
1289 | unsigned OpC = | ||||
1290 | Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; | ||||
1291 | return ConstantFoldBinaryOpOperands(OpC, LHS, RHS, DL); | ||||
1292 | } | ||||
1293 | |||||
1294 | // Convert pointer comparison (base+offset1) pred (base+offset2) into | ||||
1295 | // offset1 pred offset2, for the case where the offset is inbounds. This | ||||
1296 | // only works for equality and unsigned comparison, as inbounds permits | ||||
1297 | // crossing the sign boundary. However, the offset comparison itself is | ||||
1298 | // signed. | ||||
1299 | if (Ops0->getType()->isPointerTy() && !ICmpInst::isSigned(Predicate)) { | ||||
1300 | unsigned IndexWidth = DL.getIndexTypeSizeInBits(Ops0->getType()); | ||||
1301 | APInt Offset0(IndexWidth, 0); | ||||
1302 | Value *Stripped0 = | ||||
1303 | Ops0->stripAndAccumulateInBoundsConstantOffsets(DL, Offset0); | ||||
1304 | APInt Offset1(IndexWidth, 0); | ||||
1305 | Value *Stripped1 = | ||||
1306 | Ops1->stripAndAccumulateInBoundsConstantOffsets(DL, Offset1); | ||||
1307 | if (Stripped0 == Stripped1) | ||||
1308 | return ConstantExpr::getCompare( | ||||
1309 | ICmpInst::getSignedPredicate(Predicate), | ||||
1310 | ConstantInt::get(CE0->getContext(), Offset0), | ||||
1311 | ConstantInt::get(CE0->getContext(), Offset1)); | ||||
1312 | } | ||||
1313 | } else if (isa<ConstantExpr>(Ops1)) { | ||||
1314 | // If RHS is a constant expression, but the left side isn't, swap the | ||||
1315 | // operands and try again. | ||||
1316 | Predicate = ICmpInst::getSwappedPredicate(Predicate); | ||||
1317 | return ConstantFoldCompareInstOperands(Predicate, Ops1, Ops0, DL, TLI); | ||||
1318 | } | ||||
1319 | |||||
1320 | // Flush any denormal constant float input according to denormal handling | ||||
1321 | // mode. | ||||
1322 | Ops0 = FlushFPConstant(Ops0, I, /* IsOutput */ false); | ||||
1323 | Ops1 = FlushFPConstant(Ops1, I, /* IsOutput */ false); | ||||
1324 | |||||
1325 | return ConstantExpr::getCompare(Predicate, Ops0, Ops1); | ||||
1326 | } | ||||
1327 | |||||
1328 | Constant *llvm::ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, | ||||
1329 | const DataLayout &DL) { | ||||
1330 | assert(Instruction::isUnaryOp(Opcode))(static_cast <bool> (Instruction::isUnaryOp(Opcode)) ? void (0) : __assert_fail ("Instruction::isUnaryOp(Opcode)", "llvm/lib/Analysis/ConstantFolding.cpp" , 1330, __extension__ __PRETTY_FUNCTION__)); | ||||
1331 | |||||
1332 | return ConstantFoldUnaryInstruction(Opcode, Op); | ||||
1333 | } | ||||
1334 | |||||
1335 | Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, | ||||
1336 | Constant *RHS, | ||||
1337 | const DataLayout &DL) { | ||||
1338 | assert(Instruction::isBinaryOp(Opcode))(static_cast <bool> (Instruction::isBinaryOp(Opcode)) ? void (0) : __assert_fail ("Instruction::isBinaryOp(Opcode)", "llvm/lib/Analysis/ConstantFolding.cpp", 1338, __extension__ __PRETTY_FUNCTION__)); | ||||
1339 | if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS)) | ||||
1340 | if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL)) | ||||
1341 | return C; | ||||
1342 | |||||
1343 | if (ConstantExpr::isDesirableBinOp(Opcode)) | ||||
1344 | return ConstantExpr::get(Opcode, LHS, RHS); | ||||
1345 | return ConstantFoldBinaryInstruction(Opcode, LHS, RHS); | ||||
1346 | } | ||||
1347 | |||||
1348 | Constant *llvm::FlushFPConstant(Constant *Operand, const Instruction *I, | ||||
1349 | bool IsOutput) { | ||||
1350 | if (!I || !I->getParent() || !I->getFunction()) | ||||
1351 | return Operand; | ||||
1352 | |||||
1353 | ConstantFP *CFP = dyn_cast<ConstantFP>(Operand); | ||||
1354 | if (!CFP) | ||||
1355 | return Operand; | ||||
1356 | |||||
1357 | const APFloat &APF = CFP->getValueAPF(); | ||||
1358 | Type *Ty = CFP->getType(); | ||||
1359 | DenormalMode DenormMode = | ||||
1360 | I->getFunction()->getDenormalMode(Ty->getFltSemantics()); | ||||
1361 | DenormalMode::DenormalModeKind Mode = | ||||
1362 | IsOutput ? DenormMode.Output : DenormMode.Input; | ||||
1363 | switch (Mode) { | ||||
1364 | default: | ||||
1365 | llvm_unreachable("unknown denormal mode")::llvm::llvm_unreachable_internal("unknown denormal mode", "llvm/lib/Analysis/ConstantFolding.cpp" , 1365); | ||||
1366 | return Operand; | ||||
1367 | case DenormalMode::IEEE: | ||||
1368 | return Operand; | ||||
1369 | case DenormalMode::PreserveSign: | ||||
1370 | if (APF.isDenormal()) { | ||||
1371 | return ConstantFP::get( | ||||
1372 | Ty->getContext(), | ||||
1373 | APFloat::getZero(Ty->getFltSemantics(), APF.isNegative())); | ||||
1374 | } | ||||
1375 | return Operand; | ||||
1376 | case DenormalMode::PositiveZero: | ||||
1377 | if (APF.isDenormal()) { | ||||
1378 | return ConstantFP::get(Ty->getContext(), | ||||
1379 | APFloat::getZero(Ty->getFltSemantics(), false)); | ||||
1380 | } | ||||
1381 | return Operand; | ||||
1382 | } | ||||
1383 | return Operand; | ||||
1384 | } | ||||
1385 | |||||
1386 | Constant *llvm::ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, | ||||
1387 | Constant *RHS, const DataLayout &DL, | ||||
1388 | const Instruction *I) { | ||||
1389 | if (Instruction::isBinaryOp(Opcode)) { | ||||
1390 | // Flush denormal inputs if needed. | ||||
1391 | Constant *Op0 = FlushFPConstant(LHS, I, /* IsOutput */ false); | ||||
1392 | Constant *Op1 = FlushFPConstant(RHS, I, /* IsOutput */ false); | ||||
1393 | |||||
1394 | // Calculate constant result. | ||||
1395 | Constant *C = ConstantFoldBinaryOpOperands(Opcode, Op0, Op1, DL); | ||||
1396 | if (!C) | ||||
1397 | return nullptr; | ||||
1398 | |||||
1399 | // Flush denormal output if needed. | ||||
1400 | return FlushFPConstant(C, I, /* IsOutput */ true); | ||||
1401 | } | ||||
1402 | // If instruction lacks a parent/function and the denormal mode cannot be | ||||
1403 | // determined, use the default (IEEE). | ||||
1404 | return ConstantFoldBinaryOpOperands(Opcode, LHS, RHS, DL); | ||||
1405 | } | ||||
1406 | |||||
1407 | Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C, | ||||
1408 | Type *DestTy, const DataLayout &DL) { | ||||
1409 | assert(Instruction::isCast(Opcode))(static_cast <bool> (Instruction::isCast(Opcode)) ? void (0) : __assert_fail ("Instruction::isCast(Opcode)", "llvm/lib/Analysis/ConstantFolding.cpp" , 1409, __extension__ __PRETTY_FUNCTION__)); | ||||
1410 | switch (Opcode) { | ||||
1411 | default: | ||||
1412 | llvm_unreachable("Missing case")::llvm::llvm_unreachable_internal("Missing case", "llvm/lib/Analysis/ConstantFolding.cpp" , 1412); | ||||
1413 | case Instruction::PtrToInt: | ||||
1414 | if (auto *CE = dyn_cast<ConstantExpr>(C)) { | ||||
1415 | Constant *FoldedValue = nullptr; | ||||
1416 | // If the input is a inttoptr, eliminate the pair. This requires knowing | ||||
1417 | // the width of a pointer, so it can't be done in ConstantExpr::getCast. | ||||
1418 | if (CE->getOpcode() == Instruction::IntToPtr) { | ||||
1419 | // zext/trunc the inttoptr to pointer size. | ||||
1420 | FoldedValue = ConstantExpr::getIntegerCast( | ||||
1421 | CE->getOperand(0), DL.getIntPtrType(CE->getType()), | ||||
1422 | /*IsSigned=*/false); | ||||
1423 | } else if (auto *GEP = dyn_cast<GEPOperator>(CE)) { | ||||
1424 | // If we have GEP, we can perform the following folds: | ||||
1425 | // (ptrtoint (gep null, x)) -> x | ||||
1426 | // (ptrtoint (gep (gep null, x), y) -> x + y, etc. | ||||
1427 | unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType()); | ||||
1428 | APInt BaseOffset(BitWidth, 0); | ||||
1429 | auto *Base = cast<Constant>(GEP->stripAndAccumulateConstantOffsets( | ||||
1430 | DL, BaseOffset, /*AllowNonInbounds=*/true)); | ||||
1431 | if (Base->isNullValue()) { | ||||
1432 | FoldedValue = ConstantInt::get(CE->getContext(), BaseOffset); | ||||
1433 | } else { | ||||
1434 | // ptrtoint (gep i8, Ptr, (sub 0, V)) -> sub (ptrtoint Ptr), V | ||||
1435 | if (GEP->getNumIndices() == 1 && | ||||
1436 | GEP->getSourceElementType()->isIntegerTy(8)) { | ||||
1437 | auto *Ptr = cast<Constant>(GEP->getPointerOperand()); | ||||
1438 | auto *Sub = dyn_cast<ConstantExpr>(GEP->getOperand(1)); | ||||
1439 | Type *IntIdxTy = DL.getIndexType(Ptr->getType()); | ||||
1440 | if (Sub && Sub->getType() == IntIdxTy && | ||||
1441 | Sub->getOpcode() == Instruction::Sub && | ||||
1442 | Sub->getOperand(0)->isNullValue()) | ||||
1443 | FoldedValue = ConstantExpr::getSub( | ||||
1444 | ConstantExpr::getPtrToInt(Ptr, IntIdxTy), Sub->getOperand(1)); | ||||
1445 | } | ||||
1446 | } | ||||
1447 | } | ||||
1448 | if (FoldedValue) { | ||||
1449 | // Do a zext or trunc to get to the ptrtoint dest size. | ||||
1450 | return ConstantExpr::getIntegerCast(FoldedValue, DestTy, | ||||
1451 | /*IsSigned=*/false); | ||||
1452 | } | ||||
1453 | } | ||||
1454 | return ConstantExpr::getCast(Opcode, C, DestTy); | ||||
1455 | case Instruction::IntToPtr: | ||||
1456 | // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if | ||||
1457 | // the int size is >= the ptr size and the address spaces are the same. | ||||
1458 | // This requires knowing the width of a pointer, so it can't be done in | ||||
1459 | // ConstantExpr::getCast. | ||||
1460 | if (auto *CE = dyn_cast<ConstantExpr>(C)) { | ||||
1461 | if (CE->getOpcode() == Instruction::PtrToInt) { | ||||
1462 | Constant *SrcPtr = CE->getOperand(0); | ||||
1463 | unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType()); | ||||
1464 | unsigned MidIntSize = CE->getType()->getScalarSizeInBits(); | ||||
1465 | |||||
1466 | if (MidIntSize >= SrcPtrSize) { | ||||
1467 | unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace(); | ||||
1468 | if (SrcAS == DestTy->getPointerAddressSpace()) | ||||
1469 | return FoldBitCast(CE->getOperand(0), DestTy, DL); | ||||
1470 | } | ||||
1471 | } | ||||
1472 | } | ||||
1473 | |||||
1474 | return ConstantExpr::getCast(Opcode, C, DestTy); | ||||
1475 | case Instruction::Trunc: | ||||
1476 | case Instruction::ZExt: | ||||
1477 | case Instruction::SExt: | ||||
1478 | case Instruction::FPTrunc: | ||||
1479 | case Instruction::FPExt: | ||||
1480 | case Instruction::UIToFP: | ||||
1481 | case Instruction::SIToFP: | ||||
1482 | case Instruction::FPToUI: | ||||
1483 | case Instruction::FPToSI: | ||||
1484 | case Instruction::AddrSpaceCast: | ||||
1485 | return ConstantExpr::getCast(Opcode, C, DestTy); | ||||
1486 | case Instruction::BitCast: | ||||
1487 | return FoldBitCast(C, DestTy, DL); | ||||
1488 | } | ||||
1489 | } | ||||
1490 | |||||
1491 | //===----------------------------------------------------------------------===// | ||||
1492 | // Constant Folding for Calls | ||||
1493 | // | ||||
1494 | |||||
1495 | bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) { | ||||
1496 | if (Call->isNoBuiltin()) | ||||
1497 | return false; | ||||
1498 | if (Call->getFunctionType() != F->getFunctionType()) | ||||
1499 | return false; | ||||
1500 | switch (F->getIntrinsicID()) { | ||||
1501 | // Operations that do not operate floating-point numbers and do not depend on | ||||
1502 | // FP environment can be folded even in strictfp functions. | ||||
1503 | case Intrinsic::bswap: | ||||
1504 | case Intrinsic::ctpop: | ||||
1505 | case Intrinsic::ctlz: | ||||
1506 | case Intrinsic::cttz: | ||||
1507 | case Intrinsic::fshl: | ||||
1508 | case Intrinsic::fshr: | ||||
1509 | case Intrinsic::launder_invariant_group: | ||||
1510 | case Intrinsic::strip_invariant_group: | ||||
1511 | case Intrinsic::masked_load: | ||||
1512 | case Intrinsic::get_active_lane_mask: | ||||
1513 | case Intrinsic::abs: | ||||
1514 | case Intrinsic::smax: | ||||
1515 | case Intrinsic::smin: | ||||
1516 | case Intrinsic::umax: | ||||
1517 | case Intrinsic::umin: | ||||
1518 | case Intrinsic::sadd_with_overflow: | ||||
1519 | case Intrinsic::uadd_with_overflow: | ||||
1520 | case Intrinsic::ssub_with_overflow: | ||||
1521 | case Intrinsic::usub_with_overflow: | ||||
1522 | case Intrinsic::smul_with_overflow: | ||||
1523 | case Intrinsic::umul_with_overflow: | ||||
1524 | case Intrinsic::sadd_sat: | ||||
1525 | case Intrinsic::uadd_sat: | ||||
1526 | case Intrinsic::ssub_sat: | ||||
1527 | case Intrinsic::usub_sat: | ||||
1528 | case Intrinsic::smul_fix: | ||||
1529 | case Intrinsic::smul_fix_sat: | ||||
1530 | case Intrinsic::bitreverse: | ||||
1531 | case Intrinsic::is_constant: | ||||
1532 | case Intrinsic::vector_reduce_add: | ||||
1533 | case Intrinsic::vector_reduce_mul: | ||||
1534 | case Intrinsic::vector_reduce_and: | ||||
1535 | case Intrinsic::vector_reduce_or: | ||||
1536 | case Intrinsic::vector_reduce_xor: | ||||
1537 | case Intrinsic::vector_reduce_smin: | ||||
1538 | case Intrinsic::vector_reduce_smax: | ||||
1539 | case Intrinsic::vector_reduce_umin: | ||||
1540 | case Intrinsic::vector_reduce_umax: | ||||
1541 | // Target intrinsics | ||||
1542 | case Intrinsic::amdgcn_perm: | ||||
1543 | case Intrinsic::arm_mve_vctp8: | ||||
1544 | case Intrinsic::arm_mve_vctp16: | ||||
1545 | case Intrinsic::arm_mve_vctp32: | ||||
1546 | case Intrinsic::arm_mve_vctp64: | ||||
1547 | case Intrinsic::aarch64_sve_convert_from_svbool: | ||||
1548 | // WebAssembly float semantics are always known | ||||
1549 | case Intrinsic::wasm_trunc_signed: | ||||
1550 | case Intrinsic::wasm_trunc_unsigned: | ||||
1551 | return true; | ||||
1552 | |||||
1553 | // Floating point operations cannot be folded in strictfp functions in | ||||
1554 | // general case. They can be folded if FP environment is known to compiler. | ||||
1555 | case Intrinsic::minnum: | ||||
1556 | case Intrinsic::maxnum: | ||||
1557 | case Intrinsic::minimum: | ||||
1558 | case Intrinsic::maximum: | ||||
1559 | case Intrinsic::log: | ||||
1560 | case Intrinsic::log2: | ||||
1561 | case Intrinsic::log10: | ||||
1562 | case Intrinsic::exp: | ||||
1563 | case Intrinsic::exp2: | ||||
1564 | case Intrinsic::sqrt: | ||||
1565 | case Intrinsic::sin: | ||||
1566 | case Intrinsic::cos: | ||||
1567 | case Intrinsic::pow: | ||||
1568 | case Intrinsic::powi: | ||||
1569 | case Intrinsic::fma: | ||||
1570 | case Intrinsic::fmuladd: | ||||
1571 | case Intrinsic::fptoui_sat: | ||||
1572 | case Intrinsic::fptosi_sat: | ||||
1573 | case Intrinsic::convert_from_fp16: | ||||
1574 | case Intrinsic::convert_to_fp16: | ||||
1575 | case Intrinsic::amdgcn_cos: | ||||
1576 | case Intrinsic::amdgcn_cubeid: | ||||
1577 | case Intrinsic::amdgcn_cubema: | ||||
1578 | case Intrinsic::amdgcn_cubesc: | ||||
1579 | case Intrinsic::amdgcn_cubetc: | ||||
1580 | case Intrinsic::amdgcn_fmul_legacy: | ||||
1581 | case Intrinsic::amdgcn_fma_legacy: | ||||
1582 | case Intrinsic::amdgcn_fract: | ||||
1583 | case Intrinsic::amdgcn_ldexp: | ||||
1584 | case Intrinsic::amdgcn_sin: | ||||
1585 | // The intrinsics below depend on rounding mode in MXCSR. | ||||
1586 | case Intrinsic::x86_sse_cvtss2si: | ||||
1587 | case Intrinsic::x86_sse_cvtss2si64: | ||||
1588 | case Intrinsic::x86_sse_cvttss2si: | ||||
1589 | case Intrinsic::x86_sse_cvttss2si64: | ||||
1590 | case Intrinsic::x86_sse2_cvtsd2si: | ||||
1591 | case Intrinsic::x86_sse2_cvtsd2si64: | ||||
1592 | case Intrinsic::x86_sse2_cvttsd2si: | ||||
1593 | case Intrinsic::x86_sse2_cvttsd2si64: | ||||
1594 | case Intrinsic::x86_avx512_vcvtss2si32: | ||||
1595 | case Intrinsic::x86_avx512_vcvtss2si64: | ||||
1596 | case Intrinsic::x86_avx512_cvttss2si: | ||||
1597 | case Intrinsic::x86_avx512_cvttss2si64: | ||||
1598 | case Intrinsic::x86_avx512_vcvtsd2si32: | ||||
1599 | case Intrinsic::x86_avx512_vcvtsd2si64: | ||||
1600 | case Intrinsic::x86_avx512_cvttsd2si: | ||||
1601 | case Intrinsic::x86_avx512_cvttsd2si64: | ||||
1602 | case Intrinsic::x86_avx512_vcvtss2usi32: | ||||
1603 | case Intrinsic::x86_avx512_vcvtss2usi64: | ||||
1604 | case Intrinsic::x86_avx512_cvttss2usi: | ||||
1605 | case Intrinsic::x86_avx512_cvttss2usi64: | ||||
1606 | case Intrinsic::x86_avx512_vcvtsd2usi32: | ||||
1607 | case Intrinsic::x86_avx512_vcvtsd2usi64: | ||||
1608 | case Intrinsic::x86_avx512_cvttsd2usi: | ||||
1609 | case Intrinsic::x86_avx512_cvttsd2usi64: | ||||
1610 | return !Call->isStrictFP(); | ||||
1611 | |||||
1612 | // Sign operations are actually bitwise operations, they do not raise | ||||
1613 | // exceptions even for SNANs. | ||||
1614 | case Intrinsic::fabs: | ||||
1615 | case Intrinsic::copysign: | ||||
1616 | case Intrinsic::is_fpclass: | ||||
1617 | // Non-constrained variants of rounding operations means default FP | ||||
1618 | // environment, they can be folded in any case. | ||||
1619 | case Intrinsic::ceil: | ||||
1620 | case Intrinsic::floor: | ||||
1621 | case Intrinsic::round: | ||||
1622 | case Intrinsic::roundeven: | ||||
1623 | case Intrinsic::trunc: | ||||
1624 | case Intrinsic::nearbyint: | ||||
1625 | case Intrinsic::rint: | ||||
1626 | case Intrinsic::canonicalize: | ||||
1627 | // Constrained intrinsics can be folded if FP environment is known | ||||
1628 | // to compiler. | ||||
1629 | case Intrinsic::experimental_constrained_fma: | ||||
1630 | case Intrinsic::experimental_constrained_fmuladd: | ||||
1631 | case Intrinsic::experimental_constrained_fadd: | ||||
1632 | case Intrinsic::experimental_constrained_fsub: | ||||
1633 | case Intrinsic::experimental_constrained_fmul: | ||||
1634 | case Intrinsic::experimental_constrained_fdiv: | ||||
1635 | case Intrinsic::experimental_constrained_frem: | ||||
1636 | case Intrinsic::experimental_constrained_ceil: | ||||
1637 | case Intrinsic::experimental_constrained_floor: | ||||
1638 | case Intrinsic::experimental_constrained_round: | ||||
1639 | case Intrinsic::experimental_constrained_roundeven: | ||||
1640 | case Intrinsic::experimental_constrained_trunc: | ||||
1641 | case Intrinsic::experimental_constrained_nearbyint: | ||||
1642 | case Intrinsic::experimental_constrained_rint: | ||||
1643 | case Intrinsic::experimental_constrained_fcmp: | ||||
1644 | case Intrinsic::experimental_constrained_fcmps: | ||||
1645 | return true; | ||||
1646 | default: | ||||
1647 | return false; | ||||
1648 | case Intrinsic::not_intrinsic: break; | ||||
1649 | } | ||||
1650 | |||||
1651 | if (!F->hasName() || Call->isStrictFP()) | ||||
1652 | return false; | ||||
1653 | |||||
1654 | // In these cases, the check of the length is required. We don't want to | ||||
1655 | // return true for a name like "cos\0blah" which strcmp would return equal to | ||||
1656 | // "cos", but has length 8. | ||||
1657 | StringRef Name = F->getName(); | ||||
1658 | switch (Name[0]) { | ||||
1659 | default: | ||||
1660 | return false; | ||||
1661 | case 'a': | ||||
1662 | return Name == "acos" || Name == "acosf" || | ||||
1663 | Name == "asin" || Name == "asinf" || | ||||
1664 | Name == "atan" || Name == "atanf" || | ||||
1665 | Name == "atan2" || Name == "atan2f"; | ||||
1666 | case 'c': | ||||
1667 | return Name == "ceil" || Name == "ceilf" || | ||||
1668 | Name == "cos" || Name == "cosf" || | ||||
1669 | Name == "cosh" || Name == "coshf"; | ||||
1670 | case 'e': | ||||
1671 | return Name == "exp" || Name == "expf" || | ||||
1672 | Name == "exp2" || Name == "exp2f"; | ||||
1673 | case 'f': | ||||
1674 | return Name == "fabs" || Name == "fabsf" || | ||||
1675 | Name == "floor" || Name == "floorf" || | ||||
1676 | Name == "fmod" || Name == "fmodf"; | ||||
1677 | case 'l': | ||||
1678 | return Name == "log" || Name == "logf" || | ||||
1679 | Name == "log2" || Name == "log2f" || | ||||
1680 | Name == "log10" || Name == "log10f"; | ||||
1681 | case 'n': | ||||
1682 | return Name == "nearbyint" || Name == "nearbyintf"; | ||||
1683 | case 'p': | ||||
1684 | return Name == "pow" || Name == "powf"; | ||||
1685 | case 'r': | ||||
1686 | return Name == "remainder" || Name == "remainderf" || | ||||
1687 | Name == "rint" || Name == "rintf" || | ||||
1688 | Name == "round" || Name == "roundf"; | ||||
1689 | case 's': | ||||
1690 | return Name == "sin" || Name == "sinf" || | ||||
1691 | Name == "sinh" || Name == "sinhf" || | ||||
1692 | Name == "sqrt" || Name == "sqrtf"; | ||||
1693 | case 't': | ||||
1694 | return Name == "tan" || Name == "tanf" || | ||||
1695 | Name == "tanh" || Name == "tanhf" || | ||||
1696 | Name == "trunc" || Name == "truncf"; | ||||
1697 | case '_': | ||||
1698 | // Check for various function names that get used for the math functions | ||||
1699 | // when the header files are preprocessed with the macro | ||||
1700 | // __FINITE_MATH_ONLY__ enabled. | ||||
1701 | // The '12' here is the length of the shortest name that can match. | ||||
1702 | // We need to check the size before looking at Name[1] and Name[2] | ||||
1703 | // so we may as well check a limit that will eliminate mismatches. | ||||
1704 | if (Name.size() < 12 || Name[1] != '_') | ||||
1705 | return false; | ||||
1706 | switch (Name[2]) { | ||||
1707 | default: | ||||
1708 | return false; | ||||
1709 | case 'a': | ||||
1710 | return Name == "__acos_finite" || Name == "__acosf_finite" || | ||||
1711 | Name == "__asin_finite" || Name == "__asinf_finite" || | ||||
1712 | Name == "__atan2_finite" || Name == "__atan2f_finite"; | ||||
1713 | case 'c': | ||||
1714 | return Name == "__cosh_finite" || Name == "__coshf_finite"; | ||||
1715 | case 'e': | ||||
1716 | return Name == "__exp_finite" || Name == "__expf_finite" || | ||||
1717 | Name == "__exp2_finite" || Name == "__exp2f_finite"; | ||||
1718 | case 'l': | ||||
1719 | return Name == "__log_finite" || Name == "__logf_finite" || | ||||
1720 | Name == "__log10_finite" || Name == "__log10f_finite"; | ||||
1721 | case 'p': | ||||
1722 | return Name == "__pow_finite" || Name == "__powf_finite"; | ||||
1723 | case 's': | ||||
1724 | return Name == "__sinh_finite" || Name == "__sinhf_finite"; | ||||
1725 | } | ||||
1726 | } | ||||
1727 | } | ||||
1728 | |||||
1729 | namespace { | ||||
1730 | |||||
1731 | Constant *GetConstantFoldFPValue(double V, Type *Ty) { | ||||
1732 | if (Ty->isHalfTy() || Ty->isFloatTy()) { | ||||
1733 | APFloat APF(V); | ||||
1734 | bool unused; | ||||
1735 | APF.convert(Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &unused); | ||||
1736 | return ConstantFP::get(Ty->getContext(), APF); | ||||
1737 | } | ||||
1738 | if (Ty->isDoubleTy()) | ||||
1739 | return ConstantFP::get(Ty->getContext(), APFloat(V)); | ||||
1740 | llvm_unreachable("Can only constant fold half/float/double")::llvm::llvm_unreachable_internal("Can only constant fold half/float/double" , "llvm/lib/Analysis/ConstantFolding.cpp", 1740); | ||||
1741 | } | ||||
1742 | |||||
1743 | /// Clear the floating-point exception state. | ||||
1744 | inline void llvm_fenv_clearexcept() { | ||||
1745 | #if defined(HAVE_FENV_H1) && HAVE_DECL_FE_ALL_EXCEPT1 | ||||
1746 | feclearexcept(FE_ALL_EXCEPT(0x20 | 0x04 | 0x10 | 0x08 | 0x01)); | ||||
1747 | #endif | ||||
1748 | errno(*__errno_location ()) = 0; | ||||
1749 | } | ||||
1750 | |||||
1751 | /// Test if a floating-point exception was raised. | ||||
1752 | inline bool llvm_fenv_testexcept() { | ||||
1753 | int errno_val = errno(*__errno_location ()); | ||||
1754 | if (errno_val == ERANGE34 || errno_val == EDOM33) | ||||
1755 | return true; | ||||
1756 | #if defined(HAVE_FENV_H1) && HAVE_DECL_FE_ALL_EXCEPT1 && HAVE_DECL_FE_INEXACT1 | ||||
1757 | if (fetestexcept(FE_ALL_EXCEPT(0x20 | 0x04 | 0x10 | 0x08 | 0x01) & ~FE_INEXACT0x20)) | ||||
1758 | return true; | ||||
1759 | #endif | ||||
1760 | return false; | ||||
1761 | } | ||||
1762 | |||||
1763 | Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V, | ||||
1764 | Type *Ty) { | ||||
1765 | llvm_fenv_clearexcept(); | ||||
1766 | double Result = NativeFP(V.convertToDouble()); | ||||
1767 | if (llvm_fenv_testexcept()) { | ||||
1768 | llvm_fenv_clearexcept(); | ||||
1769 | return nullptr; | ||||
1770 | } | ||||
1771 | |||||
1772 | return GetConstantFoldFPValue(Result, Ty); | ||||
1773 | } | ||||
1774 | |||||
1775 | Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), | ||||
1776 | const APFloat &V, const APFloat &W, Type *Ty) { | ||||
1777 | llvm_fenv_clearexcept(); | ||||
1778 | double Result = NativeFP(V.convertToDouble(), W.convertToDouble()); | ||||
1779 | if (llvm_fenv_testexcept()) { | ||||
1780 | llvm_fenv_clearexcept(); | ||||
1781 | return nullptr; | ||||
1782 | } | ||||
1783 | |||||
1784 | return GetConstantFoldFPValue(Result, Ty); | ||||
1785 | } | ||||
1786 | |||||
1787 | Constant *constantFoldVectorReduce(Intrinsic::ID IID, Constant *Op) { | ||||
1788 | FixedVectorType *VT = dyn_cast<FixedVectorType>(Op->getType()); | ||||
1789 | if (!VT) | ||||
1790 | return nullptr; | ||||
1791 | |||||
1792 | // This isn't strictly necessary, but handle the special/common case of zero: | ||||
1793 | // all integer reductions of a zero input produce zero. | ||||
1794 | if (isa<ConstantAggregateZero>(Op)) | ||||
1795 | return ConstantInt::get(VT->getElementType(), 0); | ||||
1796 | |||||
1797 | // This is the same as the underlying binops - poison propagates. | ||||
1798 | if (isa<PoisonValue>(Op) || Op->containsPoisonElement()) | ||||
1799 | return PoisonValue::get(VT->getElementType()); | ||||
1800 | |||||
1801 | // TODO: Handle undef. | ||||
1802 | if (!isa<ConstantVector>(Op) && !isa<ConstantDataVector>(Op)) | ||||
1803 | return nullptr; | ||||
1804 | |||||
1805 | auto *EltC = dyn_cast<ConstantInt>(Op->getAggregateElement(0U)); | ||||
1806 | if (!EltC) | ||||
1807 | return nullptr; | ||||
1808 | |||||
1809 | APInt Acc = EltC->getValue(); | ||||
1810 | for (unsigned I = 1, E = VT->getNumElements(); I != E; I++) { | ||||
1811 | if (!(EltC = dyn_cast<ConstantInt>(Op->getAggregateElement(I)))) | ||||
1812 | return nullptr; | ||||
1813 | const APInt &X = EltC->getValue(); | ||||
1814 | switch (IID) { | ||||
1815 | case Intrinsic::vector_reduce_add: | ||||
1816 | Acc = Acc + X; | ||||
1817 | break; | ||||
1818 | case Intrinsic::vector_reduce_mul: | ||||
1819 | Acc = Acc * X; | ||||
1820 | break; | ||||
1821 | case Intrinsic::vector_reduce_and: | ||||
1822 | Acc = Acc & X; | ||||
1823 | break; | ||||
1824 | case Intrinsic::vector_reduce_or: | ||||
1825 | Acc = Acc | X; | ||||
1826 | break; | ||||
1827 | case Intrinsic::vector_reduce_xor: | ||||
1828 | Acc = Acc ^ X; | ||||
1829 | break; | ||||
1830 | case Intrinsic::vector_reduce_smin: | ||||
1831 | Acc = APIntOps::smin(Acc, X); | ||||
1832 | break; | ||||
1833 | case Intrinsic::vector_reduce_smax: | ||||
1834 | Acc = APIntOps::smax(Acc, X); | ||||
1835 | break; | ||||
1836 | case Intrinsic::vector_reduce_umin: | ||||
1837 | Acc = APIntOps::umin(Acc, X); | ||||
1838 | break; | ||||
1839 | case Intrinsic::vector_reduce_umax: | ||||
1840 | Acc = APIntOps::umax(Acc, X); | ||||
1841 | break; | ||||
1842 | } | ||||
1843 | } | ||||
1844 | |||||
1845 | return ConstantInt::get(Op->getContext(), Acc); | ||||
1846 | } | ||||
1847 | |||||
1848 | /// Attempt to fold an SSE floating point to integer conversion of a constant | ||||
1849 | /// floating point. If roundTowardZero is false, the default IEEE rounding is | ||||
1850 | /// used (toward nearest, ties to even). This matches the behavior of the | ||||
1851 | /// non-truncating SSE instructions in the default rounding mode. The desired | ||||
1852 | /// integer type Ty is used to select how many bits are available for the | ||||
1853 | /// result. Returns null if the conversion cannot be performed, otherwise | ||||
1854 | /// returns the Constant value resulting from the conversion. | ||||
1855 | Constant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero, | ||||
1856 | Type *Ty, bool IsSigned) { | ||||
1857 | // All of these conversion intrinsics form an integer of at most 64bits. | ||||
1858 | unsigned ResultWidth = Ty->getIntegerBitWidth(); | ||||
1859 | assert(ResultWidth <= 64 &&(static_cast <bool> (ResultWidth <= 64 && "Can only constant fold conversions to 64 and 32 bit ints" ) ? void (0) : __assert_fail ("ResultWidth <= 64 && \"Can only constant fold conversions to 64 and 32 bit ints\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 1860, __extension__ __PRETTY_FUNCTION__)) | ||||
1860 | "Can only constant fold conversions to 64 and 32 bit ints")(static_cast <bool> (ResultWidth <= 64 && "Can only constant fold conversions to 64 and 32 bit ints" ) ? void (0) : __assert_fail ("ResultWidth <= 64 && \"Can only constant fold conversions to 64 and 32 bit ints\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 1860, __extension__ __PRETTY_FUNCTION__)); | ||||
1861 | |||||
1862 | uint64_t UIntVal; | ||||
1863 | bool isExact = false; | ||||
1864 | APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero | ||||
1865 | : APFloat::rmNearestTiesToEven; | ||||
1866 | APFloat::opStatus status = | ||||
1867 | Val.convertToInteger(MutableArrayRef(UIntVal), ResultWidth, | ||||
1868 | IsSigned, mode, &isExact); | ||||
1869 | if (status != APFloat::opOK && | ||||
1870 | (!roundTowardZero || status != APFloat::opInexact)) | ||||
1871 | return nullptr; | ||||
1872 | return ConstantInt::get(Ty, UIntVal, IsSigned); | ||||
1873 | } | ||||
1874 | |||||
1875 | double getValueAsDouble(ConstantFP *Op) { | ||||
1876 | Type *Ty = Op->getType(); | ||||
1877 | |||||
1878 | if (Ty->isBFloatTy() || Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) | ||||
1879 | return Op->getValueAPF().convertToDouble(); | ||||
1880 | |||||
1881 | bool unused; | ||||
1882 | APFloat APF = Op->getValueAPF(); | ||||
1883 | APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &unused); | ||||
1884 | return APF.convertToDouble(); | ||||
1885 | } | ||||
1886 | |||||
1887 | static bool getConstIntOrUndef(Value *Op, const APInt *&C) { | ||||
1888 | if (auto *CI = dyn_cast<ConstantInt>(Op)) { | ||||
1889 | C = &CI->getValue(); | ||||
1890 | return true; | ||||
1891 | } | ||||
1892 | if (isa<UndefValue>(Op)) { | ||||
1893 | C = nullptr; | ||||
1894 | return true; | ||||
1895 | } | ||||
1896 | return false; | ||||
1897 | } | ||||
1898 | |||||
1899 | /// Checks if the given intrinsic call, which evaluates to constant, is allowed | ||||
1900 | /// to be folded. | ||||
1901 | /// | ||||
1902 | /// \param CI Constrained intrinsic call. | ||||
1903 | /// \param St Exception flags raised during constant evaluation. | ||||
1904 | static bool mayFoldConstrained(ConstrainedFPIntrinsic *CI, | ||||
1905 | APFloat::opStatus St) { | ||||
1906 | std::optional<RoundingMode> ORM = CI->getRoundingMode(); | ||||
1907 | std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior(); | ||||
1908 | |||||
1909 | // If the operation does not change exception status flags, it is safe | ||||
1910 | // to fold. | ||||
1911 | if (St == APFloat::opStatus::opOK) | ||||
1912 | return true; | ||||
1913 | |||||
1914 | // If evaluation raised FP exception, the result can depend on rounding | ||||
1915 | // mode. If the latter is unknown, folding is not possible. | ||||
1916 | if (ORM && *ORM == RoundingMode::Dynamic) | ||||
1917 | return false; | ||||
1918 | |||||
1919 | // If FP exceptions are ignored, fold the call, even if such exception is | ||||
1920 | // raised. | ||||
1921 | if (EB && *EB != fp::ExceptionBehavior::ebStrict) | ||||
1922 | return true; | ||||
1923 | |||||
1924 | // Leave the calculation for runtime so that exception flags be correctly set | ||||
1925 | // in hardware. | ||||
1926 | return false; | ||||
1927 | } | ||||
1928 | |||||
1929 | /// Returns the rounding mode that should be used for constant evaluation. | ||||
1930 | static RoundingMode | ||||
1931 | getEvaluationRoundingMode(const ConstrainedFPIntrinsic *CI) { | ||||
1932 | std::optional<RoundingMode> ORM = CI->getRoundingMode(); | ||||
1933 | if (!ORM || *ORM == RoundingMode::Dynamic) | ||||
1934 | // Even if the rounding mode is unknown, try evaluating the operation. | ||||
1935 | // If it does not raise inexact exception, rounding was not applied, | ||||
1936 | // so the result is exact and does not depend on rounding mode. Whether | ||||
1937 | // other FP exceptions are raised, it does not depend on rounding mode. | ||||
1938 | return RoundingMode::NearestTiesToEven; | ||||
1939 | return *ORM; | ||||
1940 | } | ||||
1941 | |||||
1942 | /// Try to constant fold llvm.canonicalize for the given caller and value. | ||||
1943 | static Constant *constantFoldCanonicalize(const Type *Ty, const CallBase *CI, | ||||
1944 | const APFloat &Src) { | ||||
1945 | // Zero, positive and negative, is always OK to fold. | ||||
1946 | if (Src.isZero()) { | ||||
1947 | // Get a fresh 0, since ppc_fp128 does have non-canonical zeros. | ||||
1948 | return ConstantFP::get( | ||||
1949 | CI->getContext(), | ||||
1950 | APFloat::getZero(Src.getSemantics(), Src.isNegative())); | ||||
1951 | } | ||||
1952 | |||||
1953 | if (!Ty->isIEEELikeFPTy()) | ||||
1954 | return nullptr; | ||||
1955 | |||||
1956 | // Zero is always canonical and the sign must be preserved. | ||||
1957 | // | ||||
1958 | // Denorms and nans may have special encodings, but it should be OK to fold a | ||||
1959 | // totally average number. | ||||
1960 | if (Src.isNormal() || Src.isInfinity()) | ||||
1961 | return ConstantFP::get(CI->getContext(), Src); | ||||
1962 | |||||
1963 | if (Src.isDenormal() && CI->getParent() && CI->getFunction()) { | ||||
1964 | DenormalMode DenormMode = | ||||
1965 | CI->getFunction()->getDenormalMode(Src.getSemantics()); | ||||
1966 | if (DenormMode == DenormalMode::getIEEE()) | ||||
1967 | return nullptr; | ||||
1968 | |||||
1969 | bool IsPositive = | ||||
1970 | (!Src.isNegative() || DenormMode.Input == DenormalMode::PositiveZero || | ||||
1971 | (DenormMode.Output == DenormalMode::PositiveZero && | ||||
1972 | DenormMode.Input == DenormalMode::IEEE)); | ||||
1973 | return ConstantFP::get(CI->getContext(), | ||||
1974 | APFloat::getZero(Src.getSemantics(), !IsPositive)); | ||||
1975 | } | ||||
1976 | |||||
1977 | return nullptr; | ||||
1978 | } | ||||
1979 | |||||
1980 | static Constant *ConstantFoldScalarCall1(StringRef Name, | ||||
1981 | Intrinsic::ID IntrinsicID, | ||||
1982 | Type *Ty, | ||||
1983 | ArrayRef<Constant *> Operands, | ||||
1984 | const TargetLibraryInfo *TLI, | ||||
1985 | const CallBase *Call) { | ||||
1986 | assert(Operands.size() == 1 && "Wrong number of operands.")(static_cast <bool> (Operands.size() == 1 && "Wrong number of operands." ) ? void (0) : __assert_fail ("Operands.size() == 1 && \"Wrong number of operands.\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 1986, __extension__ __PRETTY_FUNCTION__)); | ||||
1987 | |||||
1988 | if (IntrinsicID == Intrinsic::is_constant) { | ||||
1989 | // We know we have a "Constant" argument. But we want to only | ||||
1990 | // return true for manifest constants, not those that depend on | ||||
1991 | // constants with unknowable values, e.g. GlobalValue or BlockAddress. | ||||
1992 | if (Operands[0]->isManifestConstant()) | ||||
1993 | return ConstantInt::getTrue(Ty->getContext()); | ||||
1994 | return nullptr; | ||||
1995 | } | ||||
1996 | |||||
1997 | if (isa<PoisonValue>(Operands[0])) { | ||||
1998 | // TODO: All of these operations should probably propagate poison. | ||||
1999 | if (IntrinsicID == Intrinsic::canonicalize) | ||||
2000 | return PoisonValue::get(Ty); | ||||
2001 | } | ||||
2002 | |||||
2003 | if (isa<UndefValue>(Operands[0])) { | ||||
2004 | // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN. | ||||
2005 | // ctpop() is between 0 and bitwidth, pick 0 for undef. | ||||
2006 | // fptoui.sat and fptosi.sat can always fold to zero (for a zero input). | ||||
2007 | if (IntrinsicID == Intrinsic::cos || | ||||
2008 | IntrinsicID == Intrinsic::ctpop || | ||||
2009 | IntrinsicID == Intrinsic::fptoui_sat || | ||||
2010 | IntrinsicID == Intrinsic::fptosi_sat || | ||||
2011 | IntrinsicID == Intrinsic::canonicalize) | ||||
2012 | return Constant::getNullValue(Ty); | ||||
2013 | if (IntrinsicID == Intrinsic::bswap || | ||||
2014 | IntrinsicID == Intrinsic::bitreverse || | ||||
2015 | IntrinsicID == Intrinsic::launder_invariant_group || | ||||
2016 | IntrinsicID == Intrinsic::strip_invariant_group) | ||||
2017 | return Operands[0]; | ||||
2018 | } | ||||
2019 | |||||
2020 | if (isa<ConstantPointerNull>(Operands[0])) { | ||||
2021 | // launder(null) == null == strip(null) iff in addrspace 0 | ||||
2022 | if (IntrinsicID == Intrinsic::launder_invariant_group || | ||||
2023 | IntrinsicID == Intrinsic::strip_invariant_group) { | ||||
2024 | // If instruction is not yet put in a basic block (e.g. when cloning | ||||
2025 | // a function during inlining), Call's caller may not be available. | ||||
2026 | // So check Call's BB first before querying Call->getCaller. | ||||
2027 | const Function *Caller = | ||||
2028 | Call->getParent() ? Call->getCaller() : nullptr; | ||||
2029 | if (Caller && | ||||
2030 | !NullPointerIsDefined( | ||||
2031 | Caller, Operands[0]->getType()->getPointerAddressSpace())) { | ||||
2032 | return Operands[0]; | ||||
2033 | } | ||||
2034 | return nullptr; | ||||
2035 | } | ||||
2036 | } | ||||
2037 | |||||
2038 | if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) { | ||||
2039 | if (IntrinsicID == Intrinsic::convert_to_fp16) { | ||||
2040 | APFloat Val(Op->getValueAPF()); | ||||
2041 | |||||
2042 | bool lost = false; | ||||
2043 | Val.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &lost); | ||||
2044 | |||||
2045 | return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt()); | ||||
2046 | } | ||||
2047 | |||||
2048 | APFloat U = Op->getValueAPF(); | ||||
2049 | |||||
2050 | if (IntrinsicID == Intrinsic::wasm_trunc_signed || | ||||
2051 | IntrinsicID == Intrinsic::wasm_trunc_unsigned) { | ||||
2052 | bool Signed = IntrinsicID == Intrinsic::wasm_trunc_signed; | ||||
2053 | |||||
2054 | if (U.isNaN()) | ||||
2055 | return nullptr; | ||||
2056 | |||||
2057 | unsigned Width = Ty->getIntegerBitWidth(); | ||||
2058 | APSInt Int(Width, !Signed); | ||||
2059 | bool IsExact = false; | ||||
2060 | APFloat::opStatus Status = | ||||
2061 | U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact); | ||||
2062 | |||||
2063 | if (Status == APFloat::opOK || Status == APFloat::opInexact) | ||||
2064 | return ConstantInt::get(Ty, Int); | ||||
2065 | |||||
2066 | return nullptr; | ||||
2067 | } | ||||
2068 | |||||
2069 | if (IntrinsicID == Intrinsic::fptoui_sat || | ||||
2070 | IntrinsicID == Intrinsic::fptosi_sat) { | ||||
2071 | // convertToInteger() already has the desired saturation semantics. | ||||
2072 | APSInt Int(Ty->getIntegerBitWidth(), | ||||
2073 | IntrinsicID == Intrinsic::fptoui_sat); | ||||
2074 | bool IsExact; | ||||
2075 | U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact); | ||||
2076 | return ConstantInt::get(Ty, Int); | ||||
2077 | } | ||||
2078 | |||||
2079 | if (IntrinsicID == Intrinsic::canonicalize) | ||||
2080 | return constantFoldCanonicalize(Ty, Call, U); | ||||
2081 | |||||
2082 | if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy()) | ||||
2083 | return nullptr; | ||||
2084 | |||||
2085 | // Use internal versions of these intrinsics. | ||||
2086 | |||||
2087 | if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint) { | ||||
2088 | U.roundToIntegral(APFloat::rmNearestTiesToEven); | ||||
2089 | return ConstantFP::get(Ty->getContext(), U); | ||||
2090 | } | ||||
2091 | |||||
2092 | if (IntrinsicID == Intrinsic::round) { | ||||
2093 | U.roundToIntegral(APFloat::rmNearestTiesToAway); | ||||
2094 | return ConstantFP::get(Ty->getContext(), U); | ||||
2095 | } | ||||
2096 | |||||
2097 | if (IntrinsicID == Intrinsic::roundeven) { | ||||
2098 | U.roundToIntegral(APFloat::rmNearestTiesToEven); | ||||
2099 | return ConstantFP::get(Ty->getContext(), U); | ||||
2100 | } | ||||
2101 | |||||
2102 | if (IntrinsicID == Intrinsic::ceil) { | ||||
2103 | U.roundToIntegral(APFloat::rmTowardPositive); | ||||
2104 | return ConstantFP::get(Ty->getContext(), U); | ||||
2105 | } | ||||
2106 | |||||
2107 | if (IntrinsicID == Intrinsic::floor) { | ||||
2108 | U.roundToIntegral(APFloat::rmTowardNegative); | ||||
2109 | return ConstantFP::get(Ty->getContext(), U); | ||||
2110 | } | ||||
2111 | |||||
2112 | if (IntrinsicID == Intrinsic::trunc) { | ||||
2113 | U.roundToIntegral(APFloat::rmTowardZero); | ||||
2114 | return ConstantFP::get(Ty->getContext(), U); | ||||
2115 | } | ||||
2116 | |||||
2117 | if (IntrinsicID == Intrinsic::fabs) { | ||||
2118 | U.clearSign(); | ||||
2119 | return ConstantFP::get(Ty->getContext(), U); | ||||
2120 | } | ||||
2121 | |||||
2122 | if (IntrinsicID == Intrinsic::amdgcn_fract) { | ||||
2123 | // The v_fract instruction behaves like the OpenCL spec, which defines | ||||
2124 | // fract(x) as fmin(x - floor(x), 0x1.fffffep-1f): "The min() operator is | ||||
2125 | // there to prevent fract(-small) from returning 1.0. It returns the | ||||
2126 | // largest positive floating-point number less than 1.0." | ||||
2127 | APFloat FloorU(U); | ||||
2128 | FloorU.roundToIntegral(APFloat::rmTowardNegative); | ||||
2129 | APFloat FractU(U - FloorU); | ||||
2130 | APFloat AlmostOne(U.getSemantics(), 1); | ||||
2131 | AlmostOne.next(/*nextDown*/ true); | ||||
2132 | return ConstantFP::get(Ty->getContext(), minimum(FractU, AlmostOne)); | ||||
2133 | } | ||||
2134 | |||||
2135 | // Rounding operations (floor, trunc, ceil, round and nearbyint) do not | ||||
2136 | // raise FP exceptions, unless the argument is signaling NaN. | ||||
2137 | |||||
2138 | std::optional<APFloat::roundingMode> RM; | ||||
2139 | switch (IntrinsicID) { | ||||
2140 | default: | ||||
2141 | break; | ||||
2142 | case Intrinsic::experimental_constrained_nearbyint: | ||||
2143 | case Intrinsic::experimental_constrained_rint: { | ||||
2144 | auto CI = cast<ConstrainedFPIntrinsic>(Call); | ||||
2145 | RM = CI->getRoundingMode(); | ||||
2146 | if (!RM || *RM == RoundingMode::Dynamic) | ||||
2147 | return nullptr; | ||||
2148 | break; | ||||
2149 | } | ||||
2150 | case Intrinsic::experimental_constrained_round: | ||||
2151 | RM = APFloat::rmNearestTiesToAway; | ||||
2152 | break; | ||||
2153 | case Intrinsic::experimental_constrained_ceil: | ||||
2154 | RM = APFloat::rmTowardPositive; | ||||
2155 | break; | ||||
2156 | case Intrinsic::experimental_constrained_floor: | ||||
2157 | RM = APFloat::rmTowardNegative; | ||||
2158 | break; | ||||
2159 | case Intrinsic::experimental_constrained_trunc: | ||||
2160 | RM = APFloat::rmTowardZero; | ||||
2161 | break; | ||||
2162 | } | ||||
2163 | if (RM) { | ||||
2164 | auto CI = cast<ConstrainedFPIntrinsic>(Call); | ||||
2165 | if (U.isFinite()) { | ||||
2166 | APFloat::opStatus St = U.roundToIntegral(*RM); | ||||
2167 | if (IntrinsicID == Intrinsic::experimental_constrained_rint && | ||||
2168 | St == APFloat::opInexact) { | ||||
2169 | std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior(); | ||||
2170 | if (EB && *EB == fp::ebStrict) | ||||
2171 | return nullptr; | ||||
2172 | } | ||||
2173 | } else if (U.isSignaling()) { | ||||
2174 | std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior(); | ||||
2175 | if (EB && *EB != fp::ebIgnore) | ||||
2176 | return nullptr; | ||||
2177 | U = APFloat::getQNaN(U.getSemantics()); | ||||
2178 | } | ||||
2179 | return ConstantFP::get(Ty->getContext(), U); | ||||
2180 | } | ||||
2181 | |||||
2182 | /// We only fold functions with finite arguments. Folding NaN and inf is | ||||
2183 | /// likely to be aborted with an exception anyway, and some host libms | ||||
2184 | /// have known errors raising exceptions. | ||||
2185 | if (!U.isFinite()) | ||||
2186 | return nullptr; | ||||
2187 | |||||
2188 | /// Currently APFloat versions of these functions do not exist, so we use | ||||
2189 | /// the host native double versions. Float versions are not called | ||||
2190 | /// directly but for all these it is true (float)(f((double)arg)) == | ||||
2191 | /// f(arg). Long double not supported yet. | ||||
2192 | const APFloat &APF = Op->getValueAPF(); | ||||
2193 | |||||
2194 | switch (IntrinsicID) { | ||||
2195 | default: break; | ||||
2196 | case Intrinsic::log: | ||||
2197 | return ConstantFoldFP(log, APF, Ty); | ||||
2198 | case Intrinsic::log2: | ||||
2199 | // TODO: What about hosts that lack a C99 library? | ||||
2200 | return ConstantFoldFP(log2, APF, Ty); | ||||
2201 | case Intrinsic::log10: | ||||
2202 | // TODO: What about hosts that lack a C99 library? | ||||
2203 | return ConstantFoldFP(log10, APF, Ty); | ||||
2204 | case Intrinsic::exp: | ||||
2205 | return ConstantFoldFP(exp, APF, Ty); | ||||
2206 | case Intrinsic::exp2: | ||||
2207 | // Fold exp2(x) as pow(2, x), in case the host lacks a C99 library. | ||||
2208 | return ConstantFoldBinaryFP(pow, APFloat(2.0), APF, Ty); | ||||
2209 | case Intrinsic::sin: | ||||
2210 | return ConstantFoldFP(sin, APF, Ty); | ||||
2211 | case Intrinsic::cos: | ||||
2212 | return ConstantFoldFP(cos, APF, Ty); | ||||
2213 | case Intrinsic::sqrt: | ||||
2214 | return ConstantFoldFP(sqrt, APF, Ty); | ||||
2215 | case Intrinsic::amdgcn_cos: | ||||
2216 | case Intrinsic::amdgcn_sin: { | ||||
2217 | double V = getValueAsDouble(Op); | ||||
2218 | if (V < -256.0 || V > 256.0) | ||||
2219 | // The gfx8 and gfx9 architectures handle arguments outside the range | ||||
2220 | // [-256, 256] differently. This should be a rare case so bail out | ||||
2221 | // rather than trying to handle the difference. | ||||
2222 | return nullptr; | ||||
2223 | bool IsCos = IntrinsicID == Intrinsic::amdgcn_cos; | ||||
2224 | double V4 = V * 4.0; | ||||
2225 | if (V4 == floor(V4)) { | ||||
2226 | // Force exact results for quarter-integer inputs. | ||||
2227 | const double SinVals[4] = { 0.0, 1.0, 0.0, -1.0 }; | ||||
2228 | V = SinVals[((int)V4 + (IsCos ? 1 : 0)) & 3]; | ||||
2229 | } else { | ||||
2230 | if (IsCos) | ||||
2231 | V = cos(V * 2.0 * numbers::pi); | ||||
2232 | else | ||||
2233 | V = sin(V * 2.0 * numbers::pi); | ||||
2234 | } | ||||
2235 | return GetConstantFoldFPValue(V, Ty); | ||||
2236 | } | ||||
2237 | } | ||||
2238 | |||||
2239 | if (!TLI) | ||||
2240 | return nullptr; | ||||
2241 | |||||
2242 | LibFunc Func = NotLibFunc; | ||||
2243 | if (!TLI->getLibFunc(Name, Func)) | ||||
2244 | return nullptr; | ||||
2245 | |||||
2246 | switch (Func) { | ||||
2247 | default: | ||||
2248 | break; | ||||
2249 | case LibFunc_acos: | ||||
2250 | case LibFunc_acosf: | ||||
2251 | case LibFunc_acos_finite: | ||||
2252 | case LibFunc_acosf_finite: | ||||
2253 | if (TLI->has(Func)) | ||||
2254 | return ConstantFoldFP(acos, APF, Ty); | ||||
2255 | break; | ||||
2256 | case LibFunc_asin: | ||||
2257 | case LibFunc_asinf: | ||||
2258 | case LibFunc_asin_finite: | ||||
2259 | case LibFunc_asinf_finite: | ||||
2260 | if (TLI->has(Func)) | ||||
2261 | return ConstantFoldFP(asin, APF, Ty); | ||||
2262 | break; | ||||
2263 | case LibFunc_atan: | ||||
2264 | case LibFunc_atanf: | ||||
2265 | if (TLI->has(Func)) | ||||
2266 | return ConstantFoldFP(atan, APF, Ty); | ||||
2267 | break; | ||||
2268 | case LibFunc_ceil: | ||||
2269 | case LibFunc_ceilf: | ||||
2270 | if (TLI->has(Func)) { | ||||
2271 | U.roundToIntegral(APFloat::rmTowardPositive); | ||||
2272 | return ConstantFP::get(Ty->getContext(), U); | ||||
2273 | } | ||||
2274 | break; | ||||
2275 | case LibFunc_cos: | ||||
2276 | case LibFunc_cosf: | ||||
2277 | if (TLI->has(Func)) | ||||
2278 | return ConstantFoldFP(cos, APF, Ty); | ||||
2279 | break; | ||||
2280 | case LibFunc_cosh: | ||||
2281 | case LibFunc_coshf: | ||||
2282 | case LibFunc_cosh_finite: | ||||
2283 | case LibFunc_coshf_finite: | ||||
2284 | if (TLI->has(Func)) | ||||
2285 | return ConstantFoldFP(cosh, APF, Ty); | ||||
2286 | break; | ||||
2287 | case LibFunc_exp: | ||||
2288 | case LibFunc_expf: | ||||
2289 | case LibFunc_exp_finite: | ||||
2290 | case LibFunc_expf_finite: | ||||
2291 | if (TLI->has(Func)) | ||||
2292 | return ConstantFoldFP(exp, APF, Ty); | ||||
2293 | break; | ||||
2294 | case LibFunc_exp2: | ||||
2295 | case LibFunc_exp2f: | ||||
2296 | case LibFunc_exp2_finite: | ||||
2297 | case LibFunc_exp2f_finite: | ||||
2298 | if (TLI->has(Func)) | ||||
2299 | // Fold exp2(x) as pow(2, x), in case the host lacks a C99 library. | ||||
2300 | return ConstantFoldBinaryFP(pow, APFloat(2.0), APF, Ty); | ||||
2301 | break; | ||||
2302 | case LibFunc_fabs: | ||||
2303 | case LibFunc_fabsf: | ||||
2304 | if (TLI->has(Func)) { | ||||
2305 | U.clearSign(); | ||||
2306 | return ConstantFP::get(Ty->getContext(), U); | ||||
2307 | } | ||||
2308 | break; | ||||
2309 | case LibFunc_floor: | ||||
2310 | case LibFunc_floorf: | ||||
2311 | if (TLI->has(Func)) { | ||||
2312 | U.roundToIntegral(APFloat::rmTowardNegative); | ||||
2313 | return ConstantFP::get(Ty->getContext(), U); | ||||
2314 | } | ||||
2315 | break; | ||||
2316 | case LibFunc_log: | ||||
2317 | case LibFunc_logf: | ||||
2318 | case LibFunc_log_finite: | ||||
2319 | case LibFunc_logf_finite: | ||||
2320 | if (!APF.isNegative() && !APF.isZero() && TLI->has(Func)) | ||||
2321 | return ConstantFoldFP(log, APF, Ty); | ||||
2322 | break; | ||||
2323 | case LibFunc_log2: | ||||
2324 | case LibFunc_log2f: | ||||
2325 | case LibFunc_log2_finite: | ||||
2326 | case LibFunc_log2f_finite: | ||||
2327 | if (!APF.isNegative() && !APF.isZero() && TLI->has(Func)) | ||||
2328 | // TODO: What about hosts that lack a C99 library? | ||||
2329 | return ConstantFoldFP(log2, APF, Ty); | ||||
2330 | break; | ||||
2331 | case LibFunc_log10: | ||||
2332 | case LibFunc_log10f: | ||||
2333 | case LibFunc_log10_finite: | ||||
2334 | case LibFunc_log10f_finite: | ||||
2335 | if (!APF.isNegative() && !APF.isZero() && TLI->has(Func)) | ||||
2336 | // TODO: What about hosts that lack a C99 library? | ||||
2337 | return ConstantFoldFP(log10, APF, Ty); | ||||
2338 | break; | ||||
2339 | case LibFunc_nearbyint: | ||||
2340 | case LibFunc_nearbyintf: | ||||
2341 | case LibFunc_rint: | ||||
2342 | case LibFunc_rintf: | ||||
2343 | if (TLI->has(Func)) { | ||||
2344 | U.roundToIntegral(APFloat::rmNearestTiesToEven); | ||||
2345 | return ConstantFP::get(Ty->getContext(), U); | ||||
2346 | } | ||||
2347 | break; | ||||
2348 | case LibFunc_round: | ||||
2349 | case LibFunc_roundf: | ||||
2350 | if (TLI->has(Func)) { | ||||
2351 | U.roundToIntegral(APFloat::rmNearestTiesToAway); | ||||
2352 | return ConstantFP::get(Ty->getContext(), U); | ||||
2353 | } | ||||
2354 | break; | ||||
2355 | case LibFunc_sin: | ||||
2356 | case LibFunc_sinf: | ||||
2357 | if (TLI->has(Func)) | ||||
2358 | return ConstantFoldFP(sin, APF, Ty); | ||||
2359 | break; | ||||
2360 | case LibFunc_sinh: | ||||
2361 | case LibFunc_sinhf: | ||||
2362 | case LibFunc_sinh_finite: | ||||
2363 | case LibFunc_sinhf_finite: | ||||
2364 | if (TLI->has(Func)) | ||||
2365 | return ConstantFoldFP(sinh, APF, Ty); | ||||
2366 | break; | ||||
2367 | case LibFunc_sqrt: | ||||
2368 | case LibFunc_sqrtf: | ||||
2369 | if (!APF.isNegative() && TLI->has(Func)) | ||||
2370 | return ConstantFoldFP(sqrt, APF, Ty); | ||||
2371 | break; | ||||
2372 | case LibFunc_tan: | ||||
2373 | case LibFunc_tanf: | ||||
2374 | if (TLI->has(Func)) | ||||
2375 | return ConstantFoldFP(tan, APF, Ty); | ||||
2376 | break; | ||||
2377 | case LibFunc_tanh: | ||||
2378 | case LibFunc_tanhf: | ||||
2379 | if (TLI->has(Func)) | ||||
2380 | return ConstantFoldFP(tanh, APF, Ty); | ||||
2381 | break; | ||||
2382 | case LibFunc_trunc: | ||||
2383 | case LibFunc_truncf: | ||||
2384 | if (TLI->has(Func)) { | ||||
2385 | U.roundToIntegral(APFloat::rmTowardZero); | ||||
2386 | return ConstantFP::get(Ty->getContext(), U); | ||||
2387 | } | ||||
2388 | break; | ||||
2389 | } | ||||
2390 | return nullptr; | ||||
2391 | } | ||||
2392 | |||||
2393 | if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) { | ||||
2394 | switch (IntrinsicID) { | ||||
2395 | case Intrinsic::bswap: | ||||
2396 | return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap()); | ||||
2397 | case Intrinsic::ctpop: | ||||
2398 | return ConstantInt::get(Ty, Op->getValue().popcount()); | ||||
2399 | case Intrinsic::bitreverse: | ||||
2400 | return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits()); | ||||
2401 | case Intrinsic::convert_from_fp16: { | ||||
2402 | APFloat Val(APFloat::IEEEhalf(), Op->getValue()); | ||||
2403 | |||||
2404 | bool lost = false; | ||||
2405 | APFloat::opStatus status = Val.convert( | ||||
2406 | Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &lost); | ||||
2407 | |||||
2408 | // Conversion is always precise. | ||||
2409 | (void)status; | ||||
2410 | assert(status != APFloat::opInexact && !lost &&(static_cast <bool> (status != APFloat::opInexact && !lost && "Precision lost during fp16 constfolding") ? void (0) : __assert_fail ("status != APFloat::opInexact && !lost && \"Precision lost during fp16 constfolding\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 2411, __extension__ __PRETTY_FUNCTION__)) | ||||
2411 | "Precision lost during fp16 constfolding")(static_cast <bool> (status != APFloat::opInexact && !lost && "Precision lost during fp16 constfolding") ? void (0) : __assert_fail ("status != APFloat::opInexact && !lost && \"Precision lost during fp16 constfolding\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 2411, __extension__ __PRETTY_FUNCTION__)); | ||||
2412 | |||||
2413 | return ConstantFP::get(Ty->getContext(), Val); | ||||
2414 | } | ||||
2415 | default: | ||||
2416 | return nullptr; | ||||
2417 | } | ||||
2418 | } | ||||
2419 | |||||
2420 | switch (IntrinsicID) { | ||||
2421 | default: break; | ||||
2422 | case Intrinsic::vector_reduce_add: | ||||
2423 | case Intrinsic::vector_reduce_mul: | ||||
2424 | case Intrinsic::vector_reduce_and: | ||||
2425 | case Intrinsic::vector_reduce_or: | ||||
2426 | case Intrinsic::vector_reduce_xor: | ||||
2427 | case Intrinsic::vector_reduce_smin: | ||||
2428 | case Intrinsic::vector_reduce_smax: | ||||
2429 | case Intrinsic::vector_reduce_umin: | ||||
2430 | case Intrinsic::vector_reduce_umax: | ||||
2431 | if (Constant *C = constantFoldVectorReduce(IntrinsicID, Operands[0])) | ||||
2432 | return C; | ||||
2433 | break; | ||||
2434 | } | ||||
2435 | |||||
2436 | // Support ConstantVector in case we have an Undef in the top. | ||||
2437 | if (isa<ConstantVector>(Operands[0]) || | ||||
2438 | isa<ConstantDataVector>(Operands[0])) { | ||||
2439 | auto *Op = cast<Constant>(Operands[0]); | ||||
2440 | switch (IntrinsicID) { | ||||
2441 | default: break; | ||||
2442 | case Intrinsic::x86_sse_cvtss2si: | ||||
2443 | case Intrinsic::x86_sse_cvtss2si64: | ||||
2444 | case Intrinsic::x86_sse2_cvtsd2si: | ||||
2445 | case Intrinsic::x86_sse2_cvtsd2si64: | ||||
2446 | if (ConstantFP *FPOp = | ||||
2447 | dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) | ||||
2448 | return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), | ||||
2449 | /*roundTowardZero=*/false, Ty, | ||||
2450 | /*IsSigned*/true); | ||||
2451 | break; | ||||
2452 | case Intrinsic::x86_sse_cvttss2si: | ||||
2453 | case Intrinsic::x86_sse_cvttss2si64: | ||||
2454 | case Intrinsic::x86_sse2_cvttsd2si: | ||||
2455 | case Intrinsic::x86_sse2_cvttsd2si64: | ||||
2456 | if (ConstantFP *FPOp = | ||||
2457 | dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) | ||||
2458 | return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), | ||||
2459 | /*roundTowardZero=*/true, Ty, | ||||
2460 | /*IsSigned*/true); | ||||
2461 | break; | ||||
2462 | } | ||||
2463 | } | ||||
2464 | |||||
2465 | return nullptr; | ||||
2466 | } | ||||
2467 | |||||
2468 | static Constant *evaluateCompare(const APFloat &Op1, const APFloat &Op2, | ||||
2469 | const ConstrainedFPIntrinsic *Call) { | ||||
2470 | APFloat::opStatus St = APFloat::opOK; | ||||
2471 | auto *FCmp = cast<ConstrainedFPCmpIntrinsic>(Call); | ||||
2472 | FCmpInst::Predicate Cond = FCmp->getPredicate(); | ||||
2473 | if (FCmp->isSignaling()) { | ||||
2474 | if (Op1.isNaN() || Op2.isNaN()) | ||||
2475 | St = APFloat::opInvalidOp; | ||||
2476 | } else { | ||||
2477 | if (Op1.isSignaling() || Op2.isSignaling()) | ||||
2478 | St = APFloat::opInvalidOp; | ||||
2479 | } | ||||
2480 | bool Result = FCmpInst::compare(Op1, Op2, Cond); | ||||
2481 | if (mayFoldConstrained(const_cast<ConstrainedFPCmpIntrinsic *>(FCmp), St)) | ||||
2482 | return ConstantInt::get(Call->getType()->getScalarType(), Result); | ||||
2483 | return nullptr; | ||||
2484 | } | ||||
2485 | |||||
2486 | static Constant *ConstantFoldScalarCall2(StringRef Name, | ||||
2487 | Intrinsic::ID IntrinsicID, | ||||
2488 | Type *Ty, | ||||
2489 | ArrayRef<Constant *> Operands, | ||||
2490 | const TargetLibraryInfo *TLI, | ||||
2491 | const CallBase *Call) { | ||||
2492 | assert(Operands.size() == 2 && "Wrong number of operands.")(static_cast <bool> (Operands.size() == 2 && "Wrong number of operands." ) ? void (0) : __assert_fail ("Operands.size() == 2 && \"Wrong number of operands.\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 2492, __extension__ __PRETTY_FUNCTION__)); | ||||
2493 | |||||
2494 | if (Ty->isFloatingPointTy()) { | ||||
2495 | // TODO: We should have undef handling for all of the FP intrinsics that | ||||
2496 | // are attempted to be folded in this function. | ||||
2497 | bool IsOp0Undef = isa<UndefValue>(Operands[0]); | ||||
2498 | bool IsOp1Undef = isa<UndefValue>(Operands[1]); | ||||
2499 | switch (IntrinsicID) { | ||||
2500 | case Intrinsic::maxnum: | ||||
2501 | case Intrinsic::minnum: | ||||
2502 | case Intrinsic::maximum: | ||||
2503 | case Intrinsic::minimum: | ||||
2504 | // If one argument is undef, return the other argument. | ||||
2505 | if (IsOp0Undef) | ||||
2506 | return Operands[1]; | ||||
2507 | if (IsOp1Undef) | ||||
2508 | return Operands[0]; | ||||
2509 | break; | ||||
2510 | } | ||||
2511 | } | ||||
2512 | |||||
2513 | if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) { | ||||
2514 | const APFloat &Op1V = Op1->getValueAPF(); | ||||
2515 | |||||
2516 | if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) { | ||||
2517 | if (Op2->getType() != Op1->getType()) | ||||
2518 | return nullptr; | ||||
2519 | const APFloat &Op2V = Op2->getValueAPF(); | ||||
2520 | |||||
2521 | if (const auto *ConstrIntr = dyn_cast<ConstrainedFPIntrinsic>(Call)) { | ||||
2522 | RoundingMode RM = getEvaluationRoundingMode(ConstrIntr); | ||||
2523 | APFloat Res = Op1V; | ||||
2524 | APFloat::opStatus St; | ||||
2525 | switch (IntrinsicID) { | ||||
2526 | default: | ||||
2527 | return nullptr; | ||||
2528 | case Intrinsic::experimental_constrained_fadd: | ||||
2529 | St = Res.add(Op2V, RM); | ||||
2530 | break; | ||||
2531 | case Intrinsic::experimental_constrained_fsub: | ||||
2532 | St = Res.subtract(Op2V, RM); | ||||
2533 | break; | ||||
2534 | case Intrinsic::experimental_constrained_fmul: | ||||
2535 | St = Res.multiply(Op2V, RM); | ||||
2536 | break; | ||||
2537 | case Intrinsic::experimental_constrained_fdiv: | ||||
2538 | St = Res.divide(Op2V, RM); | ||||
2539 | break; | ||||
2540 | case Intrinsic::experimental_constrained_frem: | ||||
2541 | St = Res.mod(Op2V); | ||||
2542 | break; | ||||
2543 | case Intrinsic::experimental_constrained_fcmp: | ||||
2544 | case Intrinsic::experimental_constrained_fcmps: | ||||
2545 | return evaluateCompare(Op1V, Op2V, ConstrIntr); | ||||
2546 | } | ||||
2547 | if (mayFoldConstrained(const_cast<ConstrainedFPIntrinsic *>(ConstrIntr), | ||||
2548 | St)) | ||||
2549 | return ConstantFP::get(Ty->getContext(), Res); | ||||
2550 | return nullptr; | ||||
2551 | } | ||||
2552 | |||||
2553 | switch (IntrinsicID) { | ||||
2554 | default: | ||||
2555 | break; | ||||
2556 | case Intrinsic::copysign: | ||||
2557 | return ConstantFP::get(Ty->getContext(), APFloat::copySign(Op1V, Op2V)); | ||||
2558 | case Intrinsic::minnum: | ||||
2559 | return ConstantFP::get(Ty->getContext(), minnum(Op1V, Op2V)); | ||||
2560 | case Intrinsic::maxnum: | ||||
2561 | return ConstantFP::get(Ty->getContext(), maxnum(Op1V, Op2V)); | ||||
2562 | case Intrinsic::minimum: | ||||
2563 | return ConstantFP::get(Ty->getContext(), minimum(Op1V, Op2V)); | ||||
2564 | case Intrinsic::maximum: | ||||
2565 | return ConstantFP::get(Ty->getContext(), maximum(Op1V, Op2V)); | ||||
2566 | } | ||||
2567 | |||||
2568 | if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy()) | ||||
2569 | return nullptr; | ||||
2570 | |||||
2571 | switch (IntrinsicID) { | ||||
2572 | default: | ||||
2573 | break; | ||||
2574 | case Intrinsic::pow: | ||||
2575 | return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty); | ||||
2576 | case Intrinsic::amdgcn_fmul_legacy: | ||||
2577 | // The legacy behaviour is that multiplying +/- 0.0 by anything, even | ||||
2578 | // NaN or infinity, gives +0.0. | ||||
2579 | if (Op1V.isZero() || Op2V.isZero()) | ||||
2580 | return ConstantFP::getNullValue(Ty); | ||||
2581 | return ConstantFP::get(Ty->getContext(), Op1V * Op2V); | ||||
2582 | } | ||||
2583 | |||||
2584 | if (!TLI) | ||||
2585 | return nullptr; | ||||
2586 | |||||
2587 | LibFunc Func = NotLibFunc; | ||||
2588 | if (!TLI->getLibFunc(Name, Func)) | ||||
2589 | return nullptr; | ||||
2590 | |||||
2591 | switch (Func) { | ||||
2592 | default: | ||||
2593 | break; | ||||
2594 | case LibFunc_pow: | ||||
2595 | case LibFunc_powf: | ||||
2596 | case LibFunc_pow_finite: | ||||
2597 | case LibFunc_powf_finite: | ||||
2598 | if (TLI->has(Func)) | ||||
2599 | return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty); | ||||
2600 | break; | ||||
2601 | case LibFunc_fmod: | ||||
2602 | case LibFunc_fmodf: | ||||
2603 | if (TLI->has(Func)) { | ||||
2604 | APFloat V = Op1->getValueAPF(); | ||||
2605 | if (APFloat::opStatus::opOK == V.mod(Op2->getValueAPF())) | ||||
2606 | return ConstantFP::get(Ty->getContext(), V); | ||||
2607 | } | ||||
2608 | break; | ||||
2609 | case LibFunc_remainder: | ||||
2610 | case LibFunc_remainderf: | ||||
2611 | if (TLI->has(Func)) { | ||||
2612 | APFloat V = Op1->getValueAPF(); | ||||
2613 | if (APFloat::opStatus::opOK == V.remainder(Op2->getValueAPF())) | ||||
2614 | return ConstantFP::get(Ty->getContext(), V); | ||||
2615 | } | ||||
2616 | break; | ||||
2617 | case LibFunc_atan2: | ||||
2618 | case LibFunc_atan2f: | ||||
2619 | // atan2(+/-0.0, +/-0.0) is known to raise an exception on some libm | ||||
2620 | // (Solaris), so we do not assume a known result for that. | ||||
2621 | if (Op1V.isZero() && Op2V.isZero()) | ||||
2622 | return nullptr; | ||||
2623 | [[fallthrough]]; | ||||
2624 | case LibFunc_atan2_finite: | ||||
2625 | case LibFunc_atan2f_finite: | ||||
2626 | if (TLI->has(Func)) | ||||
2627 | return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty); | ||||
2628 | break; | ||||
2629 | } | ||||
2630 | } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) { | ||||
2631 | switch (IntrinsicID) { | ||||
2632 | case Intrinsic::is_fpclass: { | ||||
2633 | uint32_t Mask = Op2C->getZExtValue(); | ||||
2634 | bool Result = | ||||
2635 | ((Mask & fcSNan) && Op1V.isNaN() && Op1V.isSignaling()) || | ||||
2636 | ((Mask & fcQNan) && Op1V.isNaN() && !Op1V.isSignaling()) || | ||||
2637 | ((Mask & fcNegInf) && Op1V.isInfinity() && Op1V.isNegative()) || | ||||
2638 | ((Mask & fcNegNormal) && Op1V.isNormal() && Op1V.isNegative()) || | ||||
2639 | ((Mask & fcNegSubnormal) && Op1V.isDenormal() && Op1V.isNegative()) || | ||||
2640 | ((Mask & fcNegZero) && Op1V.isZero() && Op1V.isNegative()) || | ||||
2641 | ((Mask & fcPosZero) && Op1V.isZero() && !Op1V.isNegative()) || | ||||
2642 | ((Mask & fcPosSubnormal) && Op1V.isDenormal() && !Op1V.isNegative()) || | ||||
2643 | ((Mask & fcPosNormal) && Op1V.isNormal() && !Op1V.isNegative()) || | ||||
2644 | ((Mask & fcPosInf) && Op1V.isInfinity() && !Op1V.isNegative()); | ||||
2645 | return ConstantInt::get(Ty, Result); | ||||
2646 | } | ||||
2647 | default: | ||||
2648 | break; | ||||
2649 | } | ||||
2650 | |||||
2651 | if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy()) | ||||
2652 | return nullptr; | ||||
2653 | if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy()) | ||||
2654 | return ConstantFP::get( | ||||
2655 | Ty->getContext(), | ||||
2656 | APFloat((float)std::pow((float)Op1V.convertToDouble(), | ||||
2657 | (int)Op2C->getZExtValue()))); | ||||
2658 | if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy()) | ||||
2659 | return ConstantFP::get( | ||||
2660 | Ty->getContext(), | ||||
2661 | APFloat((float)std::pow((float)Op1V.convertToDouble(), | ||||
2662 | (int)Op2C->getZExtValue()))); | ||||
2663 | if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy()) | ||||
2664 | return ConstantFP::get( | ||||
2665 | Ty->getContext(), | ||||
2666 | APFloat((double)std::pow(Op1V.convertToDouble(), | ||||
2667 | (int)Op2C->getZExtValue()))); | ||||
2668 | |||||
2669 | if (IntrinsicID == Intrinsic::amdgcn_ldexp) { | ||||
2670 | // FIXME: Should flush denorms depending on FP mode, but that's ignored | ||||
2671 | // everywhere else. | ||||
2672 | |||||
2673 | // scalbn is equivalent to ldexp with float radix 2 | ||||
2674 | APFloat Result = scalbn(Op1->getValueAPF(), Op2C->getSExtValue(), | ||||
2675 | APFloat::rmNearestTiesToEven); | ||||
2676 | return ConstantFP::get(Ty->getContext(), Result); | ||||
2677 | } | ||||
2678 | } | ||||
2679 | return nullptr; | ||||
2680 | } | ||||
2681 | |||||
2682 | if (Operands[0]->getType()->isIntegerTy() && | ||||
2683 | Operands[1]->getType()->isIntegerTy()) { | ||||
2684 | const APInt *C0, *C1; | ||||
2685 | if (!getConstIntOrUndef(Operands[0], C0) || | ||||
2686 | !getConstIntOrUndef(Operands[1], C1)) | ||||
2687 | return nullptr; | ||||
2688 | |||||
2689 | switch (IntrinsicID) { | ||||
2690 | default: break; | ||||
2691 | case Intrinsic::smax: | ||||
2692 | case Intrinsic::smin: | ||||
2693 | case Intrinsic::umax: | ||||
2694 | case Intrinsic::umin: | ||||
2695 | // This is the same as for binary ops - poison propagates. | ||||
2696 | // TODO: Poison handling should be consolidated. | ||||
2697 | if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1])) | ||||
2698 | return PoisonValue::get(Ty); | ||||
2699 | |||||
2700 | if (!C0 && !C1) | ||||
2701 | return UndefValue::get(Ty); | ||||
2702 | if (!C0 || !C1) | ||||
2703 | return MinMaxIntrinsic::getSaturationPoint(IntrinsicID, Ty); | ||||
2704 | return ConstantInt::get( | ||||
2705 | Ty, ICmpInst::compare(*C0, *C1, | ||||
2706 | MinMaxIntrinsic::getPredicate(IntrinsicID)) | ||||
2707 | ? *C0 | ||||
2708 | : *C1); | ||||
2709 | |||||
2710 | case Intrinsic::usub_with_overflow: | ||||
2711 | case Intrinsic::ssub_with_overflow: | ||||
2712 | // X - undef -> { 0, false } | ||||
2713 | // undef - X -> { 0, false } | ||||
2714 | if (!C0 || !C1) | ||||
2715 | return Constant::getNullValue(Ty); | ||||
2716 | [[fallthrough]]; | ||||
2717 | case Intrinsic::uadd_with_overflow: | ||||
2718 | case Intrinsic::sadd_with_overflow: | ||||
2719 | // X + undef -> { -1, false } | ||||
2720 | // undef + x -> { -1, false } | ||||
2721 | if (!C0 || !C1) { | ||||
2722 | return ConstantStruct::get( | ||||
2723 | cast<StructType>(Ty), | ||||
2724 | {Constant::getAllOnesValue(Ty->getStructElementType(0)), | ||||
2725 | Constant::getNullValue(Ty->getStructElementType(1))}); | ||||
2726 | } | ||||
2727 | [[fallthrough]]; | ||||
2728 | case Intrinsic::smul_with_overflow: | ||||
2729 | case Intrinsic::umul_with_overflow: { | ||||
2730 | // undef * X -> { 0, false } | ||||
2731 | // X * undef -> { 0, false } | ||||
2732 | if (!C0 || !C1) | ||||
2733 | return Constant::getNullValue(Ty); | ||||
2734 | |||||
2735 | APInt Res; | ||||
2736 | bool Overflow; | ||||
2737 | switch (IntrinsicID) { | ||||
2738 | default: llvm_unreachable("Invalid case")::llvm::llvm_unreachable_internal("Invalid case", "llvm/lib/Analysis/ConstantFolding.cpp" , 2738); | ||||
2739 | case Intrinsic::sadd_with_overflow: | ||||
2740 | Res = C0->sadd_ov(*C1, Overflow); | ||||
2741 | break; | ||||
2742 | case Intrinsic::uadd_with_overflow: | ||||
2743 | Res = C0->uadd_ov(*C1, Overflow); | ||||
2744 | break; | ||||
2745 | case Intrinsic::ssub_with_overflow: | ||||
2746 | Res = C0->ssub_ov(*C1, Overflow); | ||||
2747 | break; | ||||
2748 | case Intrinsic::usub_with_overflow: | ||||
2749 | Res = C0->usub_ov(*C1, Overflow); | ||||
2750 | break; | ||||
2751 | case Intrinsic::smul_with_overflow: | ||||
2752 | Res = C0->smul_ov(*C1, Overflow); | ||||
2753 | break; | ||||
2754 | case Intrinsic::umul_with_overflow: | ||||
2755 | Res = C0->umul_ov(*C1, Overflow); | ||||
2756 | break; | ||||
2757 | } | ||||
2758 | Constant *Ops[] = { | ||||
2759 | ConstantInt::get(Ty->getContext(), Res), | ||||
2760 | ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow) | ||||
2761 | }; | ||||
2762 | return ConstantStruct::get(cast<StructType>(Ty), Ops); | ||||
2763 | } | ||||
2764 | case Intrinsic::uadd_sat: | ||||
2765 | case Intrinsic::sadd_sat: | ||||
2766 | // This is the same as for binary ops - poison propagates. | ||||
2767 | // TODO: Poison handling should be consolidated. | ||||
2768 | if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1])) | ||||
2769 | return PoisonValue::get(Ty); | ||||
2770 | |||||
2771 | if (!C0 && !C1) | ||||
2772 | return UndefValue::get(Ty); | ||||
2773 | if (!C0 || !C1) | ||||
2774 | return Constant::getAllOnesValue(Ty); | ||||
2775 | if (IntrinsicID == Intrinsic::uadd_sat) | ||||
2776 | return ConstantInt::get(Ty, C0->uadd_sat(*C1)); | ||||
2777 | else | ||||
2778 | return ConstantInt::get(Ty, C0->sadd_sat(*C1)); | ||||
2779 | case Intrinsic::usub_sat: | ||||
2780 | case Intrinsic::ssub_sat: | ||||
2781 | // This is the same as for binary ops - poison propagates. | ||||
2782 | // TODO: Poison handling should be consolidated. | ||||
2783 | if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1])) | ||||
2784 | return PoisonValue::get(Ty); | ||||
2785 | |||||
2786 | if (!C0 && !C1) | ||||
2787 | return UndefValue::get(Ty); | ||||
2788 | if (!C0 || !C1) | ||||
2789 | return Constant::getNullValue(Ty); | ||||
2790 | if (IntrinsicID == Intrinsic::usub_sat) | ||||
2791 | return ConstantInt::get(Ty, C0->usub_sat(*C1)); | ||||
2792 | else | ||||
2793 | return ConstantInt::get(Ty, C0->ssub_sat(*C1)); | ||||
2794 | case Intrinsic::cttz: | ||||
2795 | case Intrinsic::ctlz: | ||||
2796 | assert(C1 && "Must be constant int")(static_cast <bool> (C1 && "Must be constant int" ) ? void (0) : __assert_fail ("C1 && \"Must be constant int\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 2796, __extension__ __PRETTY_FUNCTION__)); | ||||
2797 | |||||
2798 | // cttz(0, 1) and ctlz(0, 1) are poison. | ||||
2799 | if (C1->isOne() && (!C0 || C0->isZero())) | ||||
2800 | return PoisonValue::get(Ty); | ||||
2801 | if (!C0) | ||||
2802 | return Constant::getNullValue(Ty); | ||||
2803 | if (IntrinsicID == Intrinsic::cttz) | ||||
2804 | return ConstantInt::get(Ty, C0->countr_zero()); | ||||
2805 | else | ||||
2806 | return ConstantInt::get(Ty, C0->countl_zero()); | ||||
2807 | |||||
2808 | case Intrinsic::abs: | ||||
2809 | assert(C1 && "Must be constant int")(static_cast <bool> (C1 && "Must be constant int" ) ? void (0) : __assert_fail ("C1 && \"Must be constant int\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 2809, __extension__ __PRETTY_FUNCTION__)); | ||||
2810 | assert((C1->isOne() || C1->isZero()) && "Must be 0 or 1")(static_cast <bool> ((C1->isOne() || C1->isZero() ) && "Must be 0 or 1") ? void (0) : __assert_fail ("(C1->isOne() || C1->isZero()) && \"Must be 0 or 1\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 2810, __extension__ __PRETTY_FUNCTION__)); | ||||
2811 | |||||
2812 | // Undef or minimum val operand with poison min --> undef | ||||
2813 | if (C1->isOne() && (!C0 || C0->isMinSignedValue())) | ||||
2814 | return UndefValue::get(Ty); | ||||
2815 | |||||
2816 | // Undef operand with no poison min --> 0 (sign bit must be clear) | ||||
2817 | if (!C0) | ||||
2818 | return Constant::getNullValue(Ty); | ||||
2819 | |||||
2820 | return ConstantInt::get(Ty, C0->abs()); | ||||
2821 | } | ||||
2822 | |||||
2823 | return nullptr; | ||||
2824 | } | ||||
2825 | |||||
2826 | // Support ConstantVector in case we have an Undef in the top. | ||||
2827 | if ((isa<ConstantVector>(Operands[0]) || | ||||
2828 | isa<ConstantDataVector>(Operands[0])) && | ||||
2829 | // Check for default rounding mode. | ||||
2830 | // FIXME: Support other rounding modes? | ||||
2831 | isa<ConstantInt>(Operands[1]) && | ||||
2832 | cast<ConstantInt>(Operands[1])->getValue() == 4) { | ||||
2833 | auto *Op = cast<Constant>(Operands[0]); | ||||
2834 | switch (IntrinsicID) { | ||||
2835 | default: break; | ||||
2836 | case Intrinsic::x86_avx512_vcvtss2si32: | ||||
2837 | case Intrinsic::x86_avx512_vcvtss2si64: | ||||
2838 | case Intrinsic::x86_avx512_vcvtsd2si32: | ||||
2839 | case Intrinsic::x86_avx512_vcvtsd2si64: | ||||
2840 | if (ConstantFP *FPOp = | ||||
2841 | dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) | ||||
2842 | return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), | ||||
2843 | /*roundTowardZero=*/false, Ty, | ||||
2844 | /*IsSigned*/true); | ||||
2845 | break; | ||||
2846 | case Intrinsic::x86_avx512_vcvtss2usi32: | ||||
2847 | case Intrinsic::x86_avx512_vcvtss2usi64: | ||||
2848 | case Intrinsic::x86_avx512_vcvtsd2usi32: | ||||
2849 | case Intrinsic::x86_avx512_vcvtsd2usi64: | ||||
2850 | if (ConstantFP *FPOp = | ||||
2851 | dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) | ||||
2852 | return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), | ||||
2853 | /*roundTowardZero=*/false, Ty, | ||||
2854 | /*IsSigned*/false); | ||||
2855 | break; | ||||
2856 | case Intrinsic::x86_avx512_cvttss2si: | ||||
2857 | case Intrinsic::x86_avx512_cvttss2si64: | ||||
2858 | case Intrinsic::x86_avx512_cvttsd2si: | ||||
2859 | case Intrinsic::x86_avx512_cvttsd2si64: | ||||
2860 | if (ConstantFP *FPOp = | ||||
2861 | dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) | ||||
2862 | return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), | ||||
2863 | /*roundTowardZero=*/true, Ty, | ||||
2864 | /*IsSigned*/true); | ||||
2865 | break; | ||||
2866 | case Intrinsic::x86_avx512_cvttss2usi: | ||||
2867 | case Intrinsic::x86_avx512_cvttss2usi64: | ||||
2868 | case Intrinsic::x86_avx512_cvttsd2usi: | ||||
2869 | case Intrinsic::x86_avx512_cvttsd2usi64: | ||||
2870 | if (ConstantFP *FPOp = | ||||
2871 | dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U))) | ||||
2872 | return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(), | ||||
2873 | /*roundTowardZero=*/true, Ty, | ||||
2874 | /*IsSigned*/false); | ||||
2875 | break; | ||||
2876 | } | ||||
2877 | } | ||||
2878 | return nullptr; | ||||
2879 | } | ||||
2880 | |||||
2881 | static APFloat ConstantFoldAMDGCNCubeIntrinsic(Intrinsic::ID IntrinsicID, | ||||
2882 | const APFloat &S0, | ||||
2883 | const APFloat &S1, | ||||
2884 | const APFloat &S2) { | ||||
2885 | unsigned ID; | ||||
2886 | const fltSemantics &Sem = S0.getSemantics(); | ||||
2887 | APFloat MA(Sem), SC(Sem), TC(Sem); | ||||
2888 | if (abs(S2) >= abs(S0) && abs(S2) >= abs(S1)) { | ||||
2889 | if (S2.isNegative() && S2.isNonZero() && !S2.isNaN()) { | ||||
2890 | // S2 < 0 | ||||
2891 | ID = 5; | ||||
2892 | SC = -S0; | ||||
2893 | } else { | ||||
2894 | ID = 4; | ||||
2895 | SC = S0; | ||||
2896 | } | ||||
2897 | MA = S2; | ||||
2898 | TC = -S1; | ||||
2899 | } else if (abs(S1) >= abs(S0)) { | ||||
2900 | if (S1.isNegative() && S1.isNonZero() && !S1.isNaN()) { | ||||
2901 | // S1 < 0 | ||||
2902 | ID = 3; | ||||
2903 | TC = -S2; | ||||
2904 | } else { | ||||
2905 | ID = 2; | ||||
2906 | TC = S2; | ||||
2907 | } | ||||
2908 | MA = S1; | ||||
2909 | SC = S0; | ||||
2910 | } else { | ||||
2911 | if (S0.isNegative() && S0.isNonZero() && !S0.isNaN()) { | ||||
2912 | // S0 < 0 | ||||
2913 | ID = 1; | ||||
2914 | SC = S2; | ||||
2915 | } else { | ||||
2916 | ID = 0; | ||||
2917 | SC = -S2; | ||||
2918 | } | ||||
2919 | MA = S0; | ||||
2920 | TC = -S1; | ||||
2921 | } | ||||
2922 | switch (IntrinsicID) { | ||||
2923 | default: | ||||
2924 | llvm_unreachable("unhandled amdgcn cube intrinsic")::llvm::llvm_unreachable_internal("unhandled amdgcn cube intrinsic" , "llvm/lib/Analysis/ConstantFolding.cpp", 2924); | ||||
2925 | case Intrinsic::amdgcn_cubeid: | ||||
2926 | return APFloat(Sem, ID); | ||||
2927 | case Intrinsic::amdgcn_cubema: | ||||
2928 | return MA + MA; | ||||
2929 | case Intrinsic::amdgcn_cubesc: | ||||
2930 | return SC; | ||||
2931 | case Intrinsic::amdgcn_cubetc: | ||||
2932 | return TC; | ||||
2933 | } | ||||
2934 | } | ||||
2935 | |||||
2936 | static Constant *ConstantFoldAMDGCNPermIntrinsic(ArrayRef<Constant *> Operands, | ||||
2937 | Type *Ty) { | ||||
2938 | const APInt *C0, *C1, *C2; | ||||
2939 | if (!getConstIntOrUndef(Operands[0], C0) || | ||||
2940 | !getConstIntOrUndef(Operands[1], C1) || | ||||
2941 | !getConstIntOrUndef(Operands[2], C2)) | ||||
2942 | return nullptr; | ||||
2943 | |||||
2944 | if (!C2) | ||||
2945 | return UndefValue::get(Ty); | ||||
2946 | |||||
2947 | APInt Val(32, 0); | ||||
2948 | unsigned NumUndefBytes = 0; | ||||
2949 | for (unsigned I = 0; I < 32; I += 8) { | ||||
2950 | unsigned Sel = C2->extractBitsAsZExtValue(8, I); | ||||
2951 | unsigned B = 0; | ||||
2952 | |||||
2953 | if (Sel >= 13) | ||||
2954 | B = 0xff; | ||||
2955 | else if (Sel == 12) | ||||
2956 | B = 0x00; | ||||
2957 | else { | ||||
2958 | const APInt *Src = ((Sel & 10) == 10 || (Sel & 12) == 4) ? C0 : C1; | ||||
2959 | if (!Src) | ||||
2960 | ++NumUndefBytes; | ||||
2961 | else if (Sel < 8) | ||||
2962 | B = Src->extractBitsAsZExtValue(8, (Sel & 3) * 8); | ||||
2963 | else | ||||
2964 | B = Src->extractBitsAsZExtValue(1, (Sel & 1) ? 31 : 15) * 0xff; | ||||
2965 | } | ||||
2966 | |||||
2967 | Val.insertBits(B, I, 8); | ||||
2968 | } | ||||
2969 | |||||
2970 | if (NumUndefBytes == 4) | ||||
2971 | return UndefValue::get(Ty); | ||||
2972 | |||||
2973 | return ConstantInt::get(Ty, Val); | ||||
2974 | } | ||||
2975 | |||||
2976 | static Constant *ConstantFoldScalarCall3(StringRef Name, | ||||
2977 | Intrinsic::ID IntrinsicID, | ||||
2978 | Type *Ty, | ||||
2979 | ArrayRef<Constant *> Operands, | ||||
2980 | const TargetLibraryInfo *TLI, | ||||
2981 | const CallBase *Call) { | ||||
2982 | assert(Operands.size() == 3 && "Wrong number of operands.")(static_cast <bool> (Operands.size() == 3 && "Wrong number of operands." ) ? void (0) : __assert_fail ("Operands.size() == 3 && \"Wrong number of operands.\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 2982, __extension__ __PRETTY_FUNCTION__)); | ||||
2983 | |||||
2984 | if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) { | ||||
2985 | if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) { | ||||
2986 | if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) { | ||||
2987 | const APFloat &C1 = Op1->getValueAPF(); | ||||
2988 | const APFloat &C2 = Op2->getValueAPF(); | ||||
2989 | const APFloat &C3 = Op3->getValueAPF(); | ||||
2990 | |||||
2991 | if (const auto *ConstrIntr = dyn_cast<ConstrainedFPIntrinsic>(Call)) { | ||||
2992 | RoundingMode RM = getEvaluationRoundingMode(ConstrIntr); | ||||
2993 | APFloat Res = C1; | ||||
2994 | APFloat::opStatus St; | ||||
2995 | switch (IntrinsicID) { | ||||
2996 | default: | ||||
2997 | return nullptr; | ||||
2998 | case Intrinsic::experimental_constrained_fma: | ||||
2999 | case Intrinsic::experimental_constrained_fmuladd: | ||||
3000 | St = Res.fusedMultiplyAdd(C2, C3, RM); | ||||
3001 | break; | ||||
3002 | } | ||||
3003 | if (mayFoldConstrained( | ||||
3004 | const_cast<ConstrainedFPIntrinsic *>(ConstrIntr), St)) | ||||
3005 | return ConstantFP::get(Ty->getContext(), Res); | ||||
3006 | return nullptr; | ||||
3007 | } | ||||
3008 | |||||
3009 | switch (IntrinsicID) { | ||||
3010 | default: break; | ||||
3011 | case Intrinsic::amdgcn_fma_legacy: { | ||||
3012 | // The legacy behaviour is that multiplying +/- 0.0 by anything, even | ||||
3013 | // NaN or infinity, gives +0.0. | ||||
3014 | if (C1.isZero() || C2.isZero()) { | ||||
3015 | // It's tempting to just return C3 here, but that would give the | ||||
3016 | // wrong result if C3 was -0.0. | ||||
3017 | return ConstantFP::get(Ty->getContext(), APFloat(0.0f) + C3); | ||||
3018 | } | ||||
3019 | [[fallthrough]]; | ||||
3020 | } | ||||
3021 | case Intrinsic::fma: | ||||
3022 | case Intrinsic::fmuladd: { | ||||
3023 | APFloat V = C1; | ||||
3024 | V.fusedMultiplyAdd(C2, C3, APFloat::rmNearestTiesToEven); | ||||
3025 | return ConstantFP::get(Ty->getContext(), V); | ||||
3026 | } | ||||
3027 | case Intrinsic::amdgcn_cubeid: | ||||
3028 | case Intrinsic::amdgcn_cubema: | ||||
3029 | case Intrinsic::amdgcn_cubesc: | ||||
3030 | case Intrinsic::amdgcn_cubetc: { | ||||
3031 | APFloat V = ConstantFoldAMDGCNCubeIntrinsic(IntrinsicID, C1, C2, C3); | ||||
3032 | return ConstantFP::get(Ty->getContext(), V); | ||||
3033 | } | ||||
3034 | } | ||||
3035 | } | ||||
3036 | } | ||||
3037 | } | ||||
3038 | |||||
3039 | if (IntrinsicID == Intrinsic::smul_fix || | ||||
3040 | IntrinsicID == Intrinsic::smul_fix_sat) { | ||||
3041 | // poison * C -> poison | ||||
3042 | // C * poison -> poison | ||||
3043 | if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1])) | ||||
3044 | return PoisonValue::get(Ty); | ||||
3045 | |||||
3046 | const APInt *C0, *C1; | ||||
3047 | if (!getConstIntOrUndef(Operands[0], C0) || | ||||
3048 | !getConstIntOrUndef(Operands[1], C1)) | ||||
3049 | return nullptr; | ||||
3050 | |||||
3051 | // undef * C -> 0 | ||||
3052 | // C * undef -> 0 | ||||
3053 | if (!C0 || !C1) | ||||
3054 | return Constant::getNullValue(Ty); | ||||
3055 | |||||
3056 | // This code performs rounding towards negative infinity in case the result | ||||
3057 | // cannot be represented exactly for the given scale. Targets that do care | ||||
3058 | // about rounding should use a target hook for specifying how rounding | ||||
3059 | // should be done, and provide their own folding to be consistent with | ||||
3060 | // rounding. This is the same approach as used by | ||||
3061 | // DAGTypeLegalizer::ExpandIntRes_MULFIX. | ||||
3062 | unsigned Scale = cast<ConstantInt>(Operands[2])->getZExtValue(); | ||||
3063 | unsigned Width = C0->getBitWidth(); | ||||
3064 | assert(Scale < Width && "Illegal scale.")(static_cast <bool> (Scale < Width && "Illegal scale." ) ? void (0) : __assert_fail ("Scale < Width && \"Illegal scale.\"" , "llvm/lib/Analysis/ConstantFolding.cpp", 3064, __extension__ __PRETTY_FUNCTION__)); | ||||
3065 | unsigned ExtendedWidth = Width * 2; | ||||
3066 | APInt Product = | ||||
3067 | (C0->sext(ExtendedWidth) * C1->sext(ExtendedWidth)).ashr(Scale); | ||||
3068 | if (IntrinsicID == Intrinsic::smul_fix_sat) { | ||||
3069 | APInt Max = APInt::getSignedMaxValue(Width).sext(ExtendedWidth); | ||||
3070 | APInt Min = APInt::getSignedMinValue(Width).sext(ExtendedWidth); | ||||
3071 | Product = APIntOps::smin(Product, Max); | ||||
3072 | Product = APIntOps::smax(Product, Min); | ||||
3073 | } | ||||
3074 | return ConstantInt::get(Ty->getContext(), Product.sextOrTrunc(Width)); | ||||
3075 | } | ||||
3076 | |||||
3077 | if (IntrinsicID == Intrinsic::fshl || IntrinsicID == Intrinsic::fshr) { | ||||
3078 | const APInt *C0, *C1, *C2; | ||||
3079 | if (!getConstIntOrUndef(Operands[0], C0) || | ||||
3080 | !getConstIntOrUndef(Operands[1], C1) || | ||||
3081 | !getConstIntOrUndef(Operands[2], C2)) | ||||
3082 | return nullptr; | ||||
3083 | |||||
3084 | bool IsRight = IntrinsicID == Intrinsic::fshr; | ||||
3085 | if (!C2) | ||||
3086 | return Operands[IsRight ? 1 : 0]; | ||||
3087 | if (!C0 && !C1) | ||||
3088 | return UndefValue::get(Ty); | ||||
3089 | |||||
3090 | // The shift amount is interpreted as modulo the bitwidth. If the shift | ||||
3091 | // amount is effectively 0, avoid UB due to oversized inverse shift below. | ||||
3092 | unsigned BitWidth = C2->getBitWidth(); | ||||
3093 | unsigned ShAmt = C2->urem(BitWidth); | ||||
3094 | if (!ShAmt) | ||||
3095 | return Operands[IsRight ? 1 : 0]; | ||||
3096 | |||||
3097 | // (C0 << ShlAmt) | (C1 >> LshrAmt) | ||||
3098 | unsigned LshrAmt = IsRight ? ShAmt : BitWidth - ShAmt; | ||||
3099 | unsigned ShlAmt = !IsRight ? ShAmt : BitWidth - ShAmt; | ||||
3100 | if (!C0) | ||||
3101 | return ConstantInt::get(Ty, C1->lshr(LshrAmt)); | ||||
3102 | if (!C1) | ||||
3103 | return ConstantInt::get(Ty, C0->shl(ShlAmt)); | ||||
3104 | return ConstantInt::get(Ty, C0->shl(ShlAmt) | C1->lshr(LshrAmt)); | ||||
3105 | } | ||||
3106 | |||||
3107 | if (IntrinsicID == Intrinsic::amdgcn_perm) | ||||
3108 | return ConstantFoldAMDGCNPermIntrinsic(Operands, Ty); | ||||
3109 | |||||
3110 | return nullptr; | ||||
3111 | } | ||||
3112 | |||||
3113 | static Constant *ConstantFoldScalarCall(StringRef Name, | ||||
3114 | Intrinsic::ID IntrinsicID, | ||||
3115 | Type *Ty, | ||||
3116 | ArrayRef<Constant *> Operands, | ||||
3117 | const TargetLibraryInfo *TLI, | ||||
3118 | const CallBase *Call) { | ||||
3119 | if (Operands.size() == 1) | ||||
3120 | return ConstantFoldScalarCall1(Name, IntrinsicID, Ty, Operands, TLI, Call); | ||||
3121 | |||||
3122 | if (Operands.size() == 2) | ||||
3123 | return ConstantFoldScalarCall2(Name, IntrinsicID, Ty, Operands, TLI, Call); | ||||
3124 | |||||
3125 | if (Operands.size() == 3) | ||||
3126 | return ConstantFoldScalarCall3(Name, IntrinsicID, Ty, Operands, TLI, Call); | ||||
3127 | |||||
3128 | return nullptr; | ||||
3129 | } | ||||
3130 | |||||
3131 | static Constant *ConstantFoldFixedVectorCall( | ||||
3132 | StringRef Name, Intrinsic::ID IntrinsicID, FixedVectorType *FVTy, | ||||
3133 | ArrayRef<Constant *> Operands, const DataLayout &DL, | ||||
3134 | const TargetLibraryInfo *TLI, const CallBase *Call) { | ||||
3135 | SmallVector<Constant *, 4> Result(FVTy->getNumElements()); | ||||
3136 | SmallVector<Constant *, 4> Lane(Operands.size()); | ||||
3137 | Type *Ty = FVTy->getElementType(); | ||||
3138 | |||||
3139 | switch (IntrinsicID) { | ||||
3140 | case Intrinsic::masked_load: { | ||||
3141 | auto *SrcPtr = Operands[0]; | ||||
3142 | auto *Mask = Operands[2]; | ||||
3143 | auto *Passthru = Operands[3]; | ||||
3144 | |||||
3145 | Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, FVTy, DL); | ||||
3146 | |||||
3147 | SmallVector<Constant *, 32> NewElements; | ||||
3148 | for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) { | ||||
3149 | auto *MaskElt = Mask->getAggregateElement(I); | ||||
3150 | if (!MaskElt) | ||||
3151 | break; | ||||
3152 | auto *PassthruElt = Passthru->getAggregateElement(I); | ||||
3153 | auto *VecElt = VecData ? VecData->getAggregateElement(I) : nullptr; | ||||
3154 | if (isa<UndefValue>(MaskElt)) { | ||||
3155 | if (PassthruElt) | ||||
3156 | NewElements.push_back(PassthruElt); | ||||
3157 | else if (VecElt) | ||||
3158 | NewElements.push_back(VecElt); | ||||
3159 | else | ||||
3160 | return nullptr; | ||||
3161 | } | ||||
3162 | if (MaskElt->isNullValue()) { | ||||
3163 | if (!PassthruElt) | ||||
3164 | return nullptr; | ||||
3165 | NewElements.push_back(PassthruElt); | ||||
3166 | } else if (MaskElt->isOneValue()) { | ||||
3167 | if (!VecElt) | ||||
3168 | return nullptr; | ||||
3169 | NewElements.push_back(VecElt); | ||||
3170 | } else { | ||||
3171 | return nullptr; | ||||
3172 | } | ||||
3173 | } | ||||
3174 | if (NewElements.size() != FVTy->getNumElements()) | ||||
3175 | return nullptr; | ||||
3176 | return ConstantVector::get(NewElements); | ||||
3177 | } | ||||
3178 | case Intrinsic::arm_mve_vctp8: | ||||
3179 | case Intrinsic::arm_mve_vctp16: | ||||
3180 | case Intrinsic::arm_mve_vctp32: | ||||
3181 | case Intrinsic::arm_mve_vctp64: { | ||||
3182 | if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) { | ||||
3183 | unsigned Lanes = FVTy->getNumElements(); | ||||
3184 | uint64_t Limit = Op->getZExtValue(); | ||||
3185 | |||||
3186 | SmallVector<Constant *, 16> NCs; | ||||
3187 | for (unsigned i = 0; i < Lanes; i++) { | ||||
3188 | if (i < Limit) | ||||
3189 | NCs.push_back(ConstantInt::getTrue(Ty)); | ||||
3190 | else | ||||
3191 | NCs.push_back(ConstantInt::getFalse(Ty)); | ||||
3192 | } | ||||
3193 | return ConstantVector::get(NCs); | ||||
3194 | } | ||||
3195 | return nullptr; | ||||
3196 | } | ||||
3197 | case Intrinsic::get_active_lane_mask: { | ||||
3198 | auto *Op0 = dyn_cast<ConstantInt>(Operands[0]); | ||||
3199 | auto *Op1 = dyn_cast<ConstantInt>(Operands[1]); | ||||
3200 | if (Op0 && Op1) { | ||||
3201 | unsigned Lanes = FVTy->getNumElements(); | ||||
3202 | uint64_t Base = Op0->getZExtValue(); | ||||
3203 | uint64_t Limit = Op1->getZExtValue(); | ||||
3204 | |||||
3205 | SmallVector<Constant *, 16> NCs; | ||||
3206 | for (unsigned i = 0; i < Lanes; i++) { | ||||
3207 | if (Base + i < Limit) | ||||
3208 | NCs.push_back(ConstantInt::getTrue(Ty)); | ||||
3209 | else | ||||
3210 | NCs.push_back(ConstantInt::getFalse(Ty)); | ||||
3211 | } | ||||
3212 | return ConstantVector::get(NCs); | ||||
3213 | } | ||||
3214 | return nullptr; | ||||
3215 | } | ||||
3216 | default: | ||||
3217 | break; | ||||
3218 | } | ||||
3219 | |||||
3220 | for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) { | ||||
3221 | // Gather a column of constants. | ||||
3222 | for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) { | ||||
3223 | // Some intrinsics use a scalar type for certain arguments. | ||||
3224 | if (isVectorIntrinsicWithScalarOpAtArg(IntrinsicID, J)) { | ||||
3225 | Lane[J] = Operands[J]; | ||||
3226 | continue; | ||||
3227 | } | ||||
3228 | |||||
3229 | Constant *Agg = Operands[J]->getAggregateElement(I); | ||||
3230 | if (!Agg) | ||||
3231 | return nullptr; | ||||
3232 | |||||
3233 | Lane[J] = Agg; | ||||
3234 | } | ||||
3235 | |||||
3236 | // Use the regular scalar folding to simplify this column. | ||||
3237 | Constant *Folded = | ||||
3238 | ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI, Call); | ||||
3239 | if (!Folded) | ||||
3240 | return nullptr; | ||||
3241 | Result[I] = Folded; | ||||
3242 | } | ||||
3243 | |||||
3244 | return ConstantVector::get(Result); | ||||
3245 | } | ||||
3246 | |||||
3247 | static Constant *ConstantFoldScalableVectorCall( | ||||
3248 | StringRef Name, Intrinsic::ID IntrinsicID, ScalableVectorType *SVTy, | ||||
3249 | ArrayRef<Constant *> Operands, const DataLayout &DL, | ||||
3250 | const TargetLibraryInfo *TLI, const CallBase *Call) { | ||||
3251 | switch (IntrinsicID) { | ||||
3252 | case Intrinsic::aarch64_sve_convert_from_svbool: { | ||||
3253 | auto *Src = dyn_cast<Constant>(Operands[0]); | ||||
3254 | if (!Src || !Src->isNullValue()) | ||||
3255 | break; | ||||
3256 | |||||
3257 | return ConstantInt::getFalse(SVTy); | ||||
3258 | } | ||||
3259 | default: | ||||
3260 | break; | ||||
3261 | } | ||||
3262 | return nullptr; | ||||
3263 | } | ||||
3264 | |||||
3265 | } // end anonymous namespace | ||||
3266 | |||||
3267 | Constant *llvm::ConstantFoldCall(const CallBase *Call, Function *F, | ||||
3268 | ArrayRef<Constant *> Operands, | ||||
3269 | const TargetLibraryInfo *TLI) { | ||||
3270 | if (Call->isNoBuiltin()) | ||||
3271 | return nullptr; | ||||
3272 | if (!F->hasName()) | ||||
3273 | return nullptr; | ||||
3274 | |||||
3275 | // If this is not an intrinsic and not recognized as a library call, bail out. | ||||
3276 | if (F->getIntrinsicID() == Intrinsic::not_intrinsic) { | ||||
3277 | if (!TLI) | ||||
3278 | return nullptr; | ||||
3279 | LibFunc LibF; | ||||
3280 | if (!TLI->getLibFunc(*F, LibF)) | ||||
3281 | return nullptr; | ||||
3282 | } | ||||
3283 | |||||
3284 | StringRef Name = F->getName(); | ||||
3285 | Type *Ty = F->getReturnType(); | ||||
3286 | if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) | ||||
3287 | return ConstantFoldFixedVectorCall( | ||||
3288 | Name, F->getIntrinsicID(), FVTy, Operands, | ||||
3289 | F->getParent()->getDataLayout(), TLI, Call); | ||||
3290 | |||||
3291 | if (auto *SVTy = dyn_cast<ScalableVectorType>(Ty)) | ||||
3292 | return ConstantFoldScalableVectorCall( | ||||
3293 | Name, F->getIntrinsicID(), SVTy, Operands, | ||||
3294 | F->getParent()->getDataLayout(), TLI, Call); | ||||
3295 | |||||
3296 | // TODO: If this is a library function, we already discovered that above, | ||||
3297 | // so we should pass the LibFunc, not the name (and it might be better | ||||
3298 | // still to separate intrinsic handling from libcalls). | ||||
3299 | return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI, | ||||
3300 | Call); | ||||
3301 | } | ||||
3302 | |||||
3303 | bool llvm::isMathLibCallNoop(const CallBase *Call, | ||||
3304 | const TargetLibraryInfo *TLI) { | ||||
3305 | // FIXME: Refactor this code; this duplicates logic in LibCallsShrinkWrap | ||||
3306 | // (and to some extent ConstantFoldScalarCall). | ||||
3307 | if (Call->isNoBuiltin() || Call->isStrictFP()) | ||||
3308 | return false; | ||||
3309 | Function *F = Call->getCalledFunction(); | ||||
3310 | if (!F) | ||||
3311 | return false; | ||||
3312 | |||||
3313 | LibFunc Func; | ||||
3314 | if (!TLI || !TLI->getLibFunc(*F, Func)) | ||||
3315 | return false; | ||||
3316 | |||||
3317 | if (Call->arg_size() == 1) { | ||||
3318 | if (ConstantFP *OpC = dyn_cast<ConstantFP>(Call->getArgOperand(0))) { | ||||
3319 | const APFloat &Op = OpC->getValueAPF(); | ||||
3320 | switch (Func) { | ||||
3321 | case LibFunc_logl: | ||||
3322 | case LibFunc_log: | ||||
3323 | case LibFunc_logf: | ||||
3324 | case LibFunc_log2l: | ||||
3325 | case LibFunc_log2: | ||||
3326 | case LibFunc_log2f: | ||||
3327 | case LibFunc_log10l: | ||||
3328 | case LibFunc_log10: | ||||
3329 | case LibFunc_log10f: | ||||
3330 | return Op.isNaN() || (!Op.isZero() && !Op.isNegative()); | ||||
3331 | |||||
3332 | case LibFunc_expl: | ||||
3333 | case LibFunc_exp: | ||||
3334 | case LibFunc_expf: | ||||
3335 | // FIXME: These boundaries are slightly conservative. | ||||
3336 | if (OpC->getType()->isDoubleTy()) | ||||
3337 | return !(Op < APFloat(-745.0) || Op > APFloat(709.0)); | ||||
3338 | if (OpC->getType()->isFloatTy()) | ||||
3339 | return !(Op < APFloat(-103.0f) || Op > APFloat(88.0f)); | ||||
3340 | break; | ||||
3341 | |||||
3342 | case LibFunc_exp2l: | ||||
3343 | case LibFunc_exp2: | ||||
3344 | case LibFunc_exp2f: | ||||
3345 | // FIXME: These boundaries are slightly conservative. | ||||
3346 | if (OpC->getType()->isDoubleTy()) | ||||
3347 | return !(Op < APFloat(-1074.0) || Op > APFloat(1023.0)); | ||||
3348 | if (OpC->getType()->isFloatTy()) | ||||
3349 | return !(Op < APFloat(-149.0f) || Op > APFloat(127.0f)); | ||||
3350 | break; | ||||
3351 | |||||
3352 | case LibFunc_sinl: | ||||
3353 | case LibFunc_sin: | ||||
3354 | case LibFunc_sinf: | ||||
3355 | case LibFunc_cosl: | ||||
3356 | case LibFunc_cos: | ||||
3357 | case LibFunc_cosf: | ||||
3358 | return !Op.isInfinity(); | ||||
3359 | |||||
3360 | case LibFunc_tanl: | ||||
3361 | case LibFunc_tan: | ||||
3362 | case LibFunc_tanf: { | ||||
3363 | // FIXME: Stop using the host math library. | ||||
3364 | // FIXME: The computation isn't done in the right precision. | ||||
3365 | Type *Ty = OpC->getType(); | ||||
3366 | if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) | ||||
3367 | return ConstantFoldFP(tan, OpC->getValueAPF(), Ty) != nullptr; | ||||
3368 | break; | ||||
3369 | } | ||||
3370 | |||||
3371 | case LibFunc_atan: | ||||
3372 | case LibFunc_atanf: | ||||
3373 | case LibFunc_atanl: | ||||
3374 | // Per POSIX, this MAY fail if Op is denormal. We choose not failing. | ||||
3375 | return true; | ||||
3376 | |||||
3377 | |||||
3378 | case LibFunc_asinl: | ||||
3379 | case LibFunc_asin: | ||||
3380 | case LibFunc_asinf: | ||||
3381 | case LibFunc_acosl: | ||||
3382 | case LibFunc_acos: | ||||
3383 | case LibFunc_acosf: | ||||
3384 | return !(Op < APFloat(Op.getSemantics(), "-1") || | ||||
3385 | Op > APFloat(Op.getSemantics(), "1")); | ||||
3386 | |||||
3387 | case LibFunc_sinh: | ||||
3388 | case LibFunc_cosh: | ||||
3389 | case LibFunc_sinhf: | ||||
3390 | case LibFunc_coshf: | ||||
3391 | case LibFunc_sinhl: | ||||
3392 | case LibFunc_coshl: | ||||
3393 | // FIXME: These boundaries are slightly conservative. | ||||
3394 | if (OpC->getType()->isDoubleTy()) | ||||
3395 | return !(Op < APFloat(-710.0) || Op > APFloat(710.0)); | ||||
3396 | if (OpC->getType()->isFloatTy()) | ||||
3397 | return !(Op < APFloat(-89.0f) || Op > APFloat(89.0f)); | ||||
3398 | break; | ||||
3399 | |||||
3400 | case LibFunc_sqrtl: | ||||
3401 | case LibFunc_sqrt: | ||||
3402 | case LibFunc_sqrtf: | ||||
3403 | return Op.isNaN() || Op.isZero() || !Op.isNegative(); | ||||
3404 | |||||
3405 | // FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p, | ||||
3406 | // maybe others? | ||||
3407 | default: | ||||
3408 | break; | ||||
3409 | } | ||||
3410 | } | ||||
3411 | } | ||||
3412 | |||||
3413 | if (Call->arg_size() == 2) { | ||||
3414 | ConstantFP *Op0C = dyn_cast<ConstantFP>(Call->getArgOperand(0)); | ||||
3415 | ConstantFP *Op1C = dyn_cast<ConstantFP>(Call->getArgOperand(1)); | ||||
3416 | if (Op0C && Op1C) { | ||||
3417 | const APFloat &Op0 = Op0C->getValueAPF(); | ||||
3418 | const APFloat &Op1 = Op1C->getValueAPF(); | ||||
3419 | |||||
3420 | switch (Func) { | ||||
3421 | case LibFunc_powl: | ||||
3422 | case LibFunc_pow: | ||||
3423 | case LibFunc_powf: { | ||||
3424 | // FIXME: Stop using the host math library. | ||||
3425 | // FIXME: The computation isn't done in the right precision. | ||||
3426 | Type *Ty = Op0C->getType(); | ||||
3427 | if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) { | ||||
3428 | if (Ty == Op1C->getType()) | ||||
3429 | return ConstantFoldBinaryFP(pow, Op0, Op1, Ty) != nullptr; | ||||
3430 | } | ||||
3431 | break; | ||||
3432 | } | ||||
3433 | |||||
3434 | case LibFunc_fmodl: | ||||
3435 | case LibFunc_fmod: | ||||
3436 | case LibFunc_fmodf: | ||||
3437 | case LibFunc_remainderl: | ||||
3438 | case LibFunc_remainder: | ||||
3439 | case LibFunc_remainderf: | ||||
3440 | return Op0.isNaN() || Op1.isNaN() || | ||||
3441 | (!Op0.isInfinity() && !Op1.isZero()); | ||||
3442 | |||||
3443 | case LibFunc_atan2: | ||||
3444 | case LibFunc_atan2f: | ||||
3445 | case LibFunc_atan2l: | ||||
3446 | // Although IEEE-754 says atan2(+/-0.0, +/-0.0) are well-defined, and | ||||
3447 | // GLIBC and MSVC do not appear to raise an error on those, we | ||||
3448 | // cannot rely on that behavior. POSIX and C11 say that a domain error | ||||
3449 | // may occur, so allow for that possibility. | ||||
3450 | return !Op0.isZero() || !Op1.isZero(); | ||||
3451 | |||||
3452 | default: | ||||
3453 | break; | ||||
3454 | } | ||||
3455 | } | ||||
3456 | } | ||||
3457 | |||||
3458 | return false; | ||||
3459 | } | ||||
3460 | |||||
3461 | void TargetFolder::anchor() {} |
1 | //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===// | ||||
2 | // | ||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
6 | // | ||||
7 | //===----------------------------------------------------------------------===// | ||||
8 | // | ||||
9 | // This file contains the declaration of the Instruction class, which is the | ||||
10 | // base class for all of the LLVM instructions. | ||||
11 | // | ||||
12 | //===----------------------------------------------------------------------===// | ||||
13 | |||||
14 | #ifndef LLVM_IR_INSTRUCTION_H | ||||
15 | #define LLVM_IR_INSTRUCTION_H | ||||
16 | |||||
17 | #include "llvm/ADT/ArrayRef.h" | ||||
18 | #include "llvm/ADT/Bitfields.h" | ||||
19 | #include "llvm/ADT/StringRef.h" | ||||
20 | #include "llvm/ADT/ilist_node.h" | ||||
21 | #include "llvm/IR/DebugLoc.h" | ||||
22 | #include "llvm/IR/SymbolTableListTraits.h" | ||||
23 | #include "llvm/IR/User.h" | ||||
24 | #include "llvm/IR/Value.h" | ||||
25 | #include "llvm/Support/AtomicOrdering.h" | ||||
26 | #include <cstdint> | ||||
27 | #include <utility> | ||||
28 | |||||
29 | namespace llvm { | ||||
30 | |||||
31 | class BasicBlock; | ||||
32 | class FastMathFlags; | ||||
33 | class MDNode; | ||||
34 | class Module; | ||||
35 | struct AAMDNodes; | ||||
36 | |||||
37 | template <> struct ilist_alloc_traits<Instruction> { | ||||
38 | static inline void deleteNode(Instruction *V); | ||||
39 | }; | ||||
40 | |||||
41 | class Instruction : public User, | ||||
42 | public ilist_node_with_parent<Instruction, BasicBlock> { | ||||
43 | BasicBlock *Parent; | ||||
44 | DebugLoc DbgLoc; // 'dbg' Metadata cache. | ||||
45 | |||||
46 | /// Relative order of this instruction in its parent basic block. Used for | ||||
47 | /// O(1) local dominance checks between instructions. | ||||
48 | mutable unsigned Order = 0; | ||||
49 | |||||
50 | protected: | ||||
51 | // The 15 first bits of `Value::SubclassData` are available for subclasses of | ||||
52 | // `Instruction` to use. | ||||
53 | using OpaqueField = Bitfield::Element<uint16_t, 0, 15>; | ||||
54 | |||||
55 | // Template alias so that all Instruction storing alignment use the same | ||||
56 | // definiton. | ||||
57 | // Valid alignments are powers of two from 2^0 to 2^MaxAlignmentExponent = | ||||
58 | // 2^32. We store them as Log2(Alignment), so we need 6 bits to encode the 33 | ||||
59 | // possible values. | ||||
60 | template <unsigned Offset> | ||||
61 | using AlignmentBitfieldElementT = | ||||
62 | typename Bitfield::Element<unsigned, Offset, 6, | ||||
63 | Value::MaxAlignmentExponent>; | ||||
64 | |||||
65 | template <unsigned Offset> | ||||
66 | using BoolBitfieldElementT = typename Bitfield::Element<bool, Offset, 1>; | ||||
67 | |||||
68 | template <unsigned Offset> | ||||
69 | using AtomicOrderingBitfieldElementT = | ||||
70 | typename Bitfield::Element<AtomicOrdering, Offset, 3, | ||||
71 | AtomicOrdering::LAST>; | ||||
72 | |||||
73 | private: | ||||
74 | // The last bit is used to store whether the instruction has metadata attached | ||||
75 | // or not. | ||||
76 | using HasMetadataField = Bitfield::Element<bool, 15, 1>; | ||||
77 | |||||
78 | protected: | ||||
79 | ~Instruction(); // Use deleteValue() to delete a generic Instruction. | ||||
80 | |||||
81 | public: | ||||
82 | Instruction(const Instruction &) = delete; | ||||
83 | Instruction &operator=(const Instruction &) = delete; | ||||
84 | |||||
85 | /// Specialize the methods defined in Value, as we know that an instruction | ||||
86 | /// can only be used by other instructions. | ||||
87 | Instruction *user_back() { return cast<Instruction>(*user_begin());} | ||||
88 | const Instruction *user_back() const { return cast<Instruction>(*user_begin());} | ||||
89 | |||||
90 | inline const BasicBlock *getParent() const { return Parent; } | ||||
91 | inline BasicBlock *getParent() { return Parent; } | ||||
92 | |||||
93 | /// Return the module owning the function this instruction belongs to | ||||
94 | /// or nullptr it the function does not have a module. | ||||
95 | /// | ||||
96 | /// Note: this is undefined behavior if the instruction does not have a | ||||
97 | /// parent, or the parent basic block does not have a parent function. | ||||
98 | const Module *getModule() const; | ||||
99 | Module *getModule() { | ||||
100 | return const_cast<Module *>( | ||||
101 | static_cast<const Instruction *>(this)->getModule()); | ||||
102 | } | ||||
103 | |||||
104 | /// Return the function this instruction belongs to. | ||||
105 | /// | ||||
106 | /// Note: it is undefined behavior to call this on an instruction not | ||||
107 | /// currently inserted into a function. | ||||
108 | const Function *getFunction() const; | ||||
109 | Function *getFunction() { | ||||
110 | return const_cast<Function *>( | ||||
111 | static_cast<const Instruction *>(this)->getFunction()); | ||||
112 | } | ||||
113 | |||||
114 | /// This method unlinks 'this' from the containing basic block, but does not | ||||
115 | /// delete it. | ||||
116 | void removeFromParent(); | ||||
117 | |||||
118 | /// This method unlinks 'this' from the containing basic block and deletes it. | ||||
119 | /// | ||||
120 | /// \returns an iterator pointing to the element after the erased one | ||||
121 | SymbolTableList<Instruction>::iterator eraseFromParent(); | ||||
122 | |||||
123 | /// Insert an unlinked instruction into a basic block immediately before | ||||
124 | /// the specified instruction. | ||||
125 | void insertBefore(Instruction *InsertPos); | ||||
126 | |||||
127 | /// Insert an unlinked instruction into a basic block immediately after the | ||||
128 | /// specified instruction. | ||||
129 | void insertAfter(Instruction *InsertPos); | ||||
130 | |||||
131 | /// Inserts an unlinked instruction into \p ParentBB at position \p It and | ||||
132 | /// returns the iterator of the inserted instruction. | ||||
133 | SymbolTableList<Instruction>::iterator | ||||
134 | insertInto(BasicBlock *ParentBB, SymbolTableList<Instruction>::iterator It); | ||||
135 | |||||
136 | /// Unlink this instruction from its current basic block and insert it into | ||||
137 | /// the basic block that MovePos lives in, right before MovePos. | ||||
138 | void moveBefore(Instruction *MovePos); | ||||
139 | |||||
140 | /// Unlink this instruction and insert into BB before I. | ||||
141 | /// | ||||
142 | /// \pre I is a valid iterator into BB. | ||||
143 | void moveBefore(BasicBlock &BB, SymbolTableList<Instruction>::iterator I); | ||||
144 | |||||
145 | /// Unlink this instruction from its current basic block and insert it into | ||||
146 | /// the basic block that MovePos lives in, right after MovePos. | ||||
147 | void moveAfter(Instruction *MovePos); | ||||
148 | |||||
149 | /// Given an instruction Other in the same basic block as this instruction, | ||||
150 | /// return true if this instruction comes before Other. In this worst case, | ||||
151 | /// this takes linear time in the number of instructions in the block. The | ||||
152 | /// results are cached, so in common cases when the block remains unmodified, | ||||
153 | /// it takes constant time. | ||||
154 | bool comesBefore(const Instruction *Other) const; | ||||
155 | |||||
156 | /// Get the first insertion point at which the result of this instruction | ||||
157 | /// is defined. This is *not* the directly following instruction in a number | ||||
158 | /// of cases, e.g. phi nodes or terminators that return values. This function | ||||
159 | /// may return null if the insertion after the definition is not possible, | ||||
160 | /// e.g. due to a catchswitch terminator. | ||||
161 | Instruction *getInsertionPointAfterDef(); | ||||
162 | |||||
163 | //===--------------------------------------------------------------------===// | ||||
164 | // Subclass classification. | ||||
165 | //===--------------------------------------------------------------------===// | ||||
166 | |||||
167 | /// Returns a member of one of the enums like Instruction::Add. | ||||
168 | unsigned getOpcode() const { return getValueID() - InstructionVal; } | ||||
169 | |||||
170 | const char *getOpcodeName() const { return getOpcodeName(getOpcode()); } | ||||
171 | bool isTerminator() const { return isTerminator(getOpcode()); } | ||||
172 | bool isUnaryOp() const { return isUnaryOp(getOpcode()); } | ||||
173 | bool isBinaryOp() const { return isBinaryOp(getOpcode()); } | ||||
174 | bool isIntDivRem() const { return isIntDivRem(getOpcode()); } | ||||
175 | bool isShift() const { return isShift(getOpcode()); } | ||||
176 | bool isCast() const { return isCast(getOpcode()); } | ||||
177 | bool isFuncletPad() const { return isFuncletPad(getOpcode()); } | ||||
178 | bool isExceptionalTerminator() const { | ||||
179 | return isExceptionalTerminator(getOpcode()); | ||||
180 | } | ||||
181 | |||||
182 | /// It checks if this instruction is the only user of at least one of | ||||
183 | /// its operands. | ||||
184 | bool isOnlyUserOfAnyOperand(); | ||||
185 | |||||
186 | static const char *getOpcodeName(unsigned Opcode); | ||||
187 | |||||
188 | static inline bool isTerminator(unsigned Opcode) { | ||||
189 | return Opcode >= TermOpsBegin && Opcode < TermOpsEnd; | ||||
190 | } | ||||
191 | |||||
192 | static inline bool isUnaryOp(unsigned Opcode) { | ||||
193 | return Opcode >= UnaryOpsBegin && Opcode < UnaryOpsEnd; | ||||
194 | } | ||||
195 | static inline bool isBinaryOp(unsigned Opcode) { | ||||
196 | return Opcode
| ||||
197 | } | ||||
198 | |||||
199 | static inline bool isIntDivRem(unsigned Opcode) { | ||||
200 | return Opcode == UDiv || Opcode == SDiv || Opcode == URem || Opcode == SRem; | ||||
201 | } | ||||
202 | |||||
203 | /// Determine if the Opcode is one of the shift instructions. | ||||
204 | static inline bool isShift(unsigned Opcode) { | ||||
205 | return Opcode >= Shl && Opcode <= AShr; | ||||
206 | } | ||||
207 | |||||
208 | /// Return true if this is a logical shift left or a logical shift right. | ||||
209 | inline bool isLogicalShift() const { | ||||
210 | return getOpcode() == Shl || getOpcode() == LShr; | ||||
211 | } | ||||
212 | |||||
213 | /// Return true if this is an arithmetic shift right. | ||||
214 | inline bool isArithmeticShift() const { | ||||
215 | return getOpcode() == AShr; | ||||
216 | } | ||||
217 | |||||
218 | /// Determine if the Opcode is and/or/xor. | ||||
219 | static inline bool isBitwiseLogicOp(unsigned Opcode) { | ||||
220 | return Opcode == And || Opcode == Or || Opcode == Xor; | ||||
221 | } | ||||
222 | |||||
223 | /// Return true if this is and/or/xor. | ||||
224 | inline bool isBitwiseLogicOp() const { | ||||
225 | return isBitwiseLogicOp(getOpcode()); | ||||
226 | } | ||||
227 | |||||
228 | /// Determine if the Opcode is one of the CastInst instructions. | ||||
229 | static inline bool isCast(unsigned Opcode) { | ||||
230 | return Opcode >= CastOpsBegin && Opcode < CastOpsEnd; | ||||
231 | } | ||||
232 | |||||
233 | /// Determine if the Opcode is one of the FuncletPadInst instructions. | ||||
234 | static inline bool isFuncletPad(unsigned Opcode) { | ||||
235 | return Opcode >= FuncletPadOpsBegin && Opcode < FuncletPadOpsEnd; | ||||
236 | } | ||||
237 | |||||
238 | /// Returns true if the Opcode is a terminator related to exception handling. | ||||
239 | static inline bool isExceptionalTerminator(unsigned Opcode) { | ||||
240 | switch (Opcode) { | ||||
241 | case Instruction::CatchSwitch: | ||||
242 | case Instruction::CatchRet: | ||||
243 | case Instruction::CleanupRet: | ||||
244 | case Instruction::Invoke: | ||||
245 | case Instruction::Resume: | ||||
246 | return true; | ||||
247 | default: | ||||
248 | return false; | ||||
249 | } | ||||
250 | } | ||||
251 | |||||
252 | //===--------------------------------------------------------------------===// | ||||
253 | // Metadata manipulation. | ||||
254 | //===--------------------------------------------------------------------===// | ||||
255 | |||||
256 | /// Return true if this instruction has any metadata attached to it. | ||||
257 | bool hasMetadata() const { return DbgLoc || Value::hasMetadata(); } | ||||
258 | |||||
259 | /// Return true if this instruction has metadata attached to it other than a | ||||
260 | /// debug location. | ||||
261 | bool hasMetadataOtherThanDebugLoc() const { return Value::hasMetadata(); } | ||||
262 | |||||
263 | /// Return true if this instruction has the given type of metadata attached. | ||||
264 | bool hasMetadata(unsigned KindID) const { | ||||
265 | return getMetadata(KindID) != nullptr; | ||||
266 | } | ||||
267 | |||||
268 | /// Return true if this instruction has the given type of metadata attached. | ||||
269 | bool hasMetadata(StringRef Kind) const { | ||||
270 | return getMetadata(Kind) != nullptr; | ||||
271 | } | ||||
272 | |||||
273 | /// Get the metadata of given kind attached to this Instruction. | ||||
274 | /// If the metadata is not found then return null. | ||||
275 | MDNode *getMetadata(unsigned KindID) const { | ||||
276 | if (!hasMetadata()) return nullptr; | ||||
277 | return getMetadataImpl(KindID); | ||||
278 | } | ||||
279 | |||||
280 | /// Get the metadata of given kind attached to this Instruction. | ||||
281 | /// If the metadata is not found then return null. | ||||
282 | MDNode *getMetadata(StringRef Kind) const { | ||||
283 | if (!hasMetadata()) return nullptr; | ||||
284 | return getMetadataImpl(Kind); | ||||
285 | } | ||||
286 | |||||
287 | /// Get all metadata attached to this Instruction. The first element of each | ||||
288 | /// pair returned is the KindID, the second element is the metadata value. | ||||
289 | /// This list is returned sorted by the KindID. | ||||
290 | void | ||||
291 | getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const { | ||||
292 | if (hasMetadata()) | ||||
293 | getAllMetadataImpl(MDs); | ||||
294 | } | ||||
295 | |||||
296 | /// This does the same thing as getAllMetadata, except that it filters out the | ||||
297 | /// debug location. | ||||
298 | void getAllMetadataOtherThanDebugLoc( | ||||
299 | SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const { | ||||
300 | Value::getAllMetadata(MDs); | ||||
301 | } | ||||
302 | |||||
303 | /// Set the metadata of the specified kind to the specified node. This updates | ||||
304 | /// or replaces metadata if already present, or removes it if Node is null. | ||||
305 | void setMetadata(unsigned KindID, MDNode *Node); | ||||
306 | void setMetadata(StringRef Kind, MDNode *Node); | ||||
307 | |||||
308 | /// Copy metadata from \p SrcInst to this instruction. \p WL, if not empty, | ||||
309 | /// specifies the list of meta data that needs to be copied. If \p WL is | ||||
310 | /// empty, all meta data will be copied. | ||||
311 | void copyMetadata(const Instruction &SrcInst, | ||||
312 | ArrayRef<unsigned> WL = ArrayRef<unsigned>()); | ||||
313 | |||||
314 | /// If the instruction has "branch_weights" MD_prof metadata and the MDNode | ||||
315 | /// has three operands (including name string), swap the order of the | ||||
316 | /// metadata. | ||||
317 | void swapProfMetadata(); | ||||
318 | |||||
319 | /// Drop all unknown metadata except for debug locations. | ||||
320 | /// @{ | ||||
321 | /// Passes are required to drop metadata they don't understand. This is a | ||||
322 | /// convenience method for passes to do so. | ||||
323 | /// dropUndefImplyingAttrsAndUnknownMetadata should be used instead of | ||||
324 | /// this API if the Instruction being modified is a call. | ||||
325 | void dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs); | ||||
326 | void dropUnknownNonDebugMetadata() { | ||||
327 | return dropUnknownNonDebugMetadata(std::nullopt); | ||||
328 | } | ||||
329 | void dropUnknownNonDebugMetadata(unsigned ID1) { | ||||
330 | return dropUnknownNonDebugMetadata(ArrayRef(ID1)); | ||||
331 | } | ||||
332 | void dropUnknownNonDebugMetadata(unsigned ID1, unsigned ID2) { | ||||
333 | unsigned IDs[] = {ID1, ID2}; | ||||
334 | return dropUnknownNonDebugMetadata(IDs); | ||||
335 | } | ||||
336 | /// @} | ||||
337 | |||||
338 | /// Adds an !annotation metadata node with \p Annotation to this instruction. | ||||
339 | /// If this instruction already has !annotation metadata, append \p Annotation | ||||
340 | /// to the existing node. | ||||
341 | void addAnnotationMetadata(StringRef Annotation); | ||||
342 | |||||
343 | /// Returns the AA metadata for this instruction. | ||||
344 | AAMDNodes getAAMetadata() const; | ||||
345 | |||||
346 | /// Sets the AA metadata on this instruction from the AAMDNodes structure. | ||||
347 | void setAAMetadata(const AAMDNodes &N); | ||||
348 | |||||
349 | /// Retrieve total raw weight values of a branch. | ||||
350 | /// Returns true on success with profile total weights filled in. | ||||
351 | /// Returns false if no metadata was found. | ||||
352 | bool extractProfTotalWeight(uint64_t &TotalVal) const; | ||||
353 | |||||
354 | /// Set the debug location information for this instruction. | ||||
355 | void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); } | ||||
356 | |||||
357 | /// Return the debug location for this node as a DebugLoc. | ||||
358 | const DebugLoc &getDebugLoc() const { return DbgLoc; } | ||||
359 | |||||
360 | /// Set or clear the nuw flag on this instruction, which must be an operator | ||||
361 | /// which supports this flag. See LangRef.html for the meaning of this flag. | ||||
362 | void setHasNoUnsignedWrap(bool b = true); | ||||
363 | |||||
364 | /// Set or clear the nsw flag on this instruction, which must be an operator | ||||
365 | /// which supports this flag. See LangRef.html for the meaning of this flag. | ||||
366 | void setHasNoSignedWrap(bool b = true); | ||||
367 | |||||
368 | /// Set or clear the exact flag on this instruction, which must be an operator | ||||
369 | /// which supports this flag. See LangRef.html for the meaning of this flag. | ||||
370 | void setIsExact(bool b = true); | ||||
371 | |||||
372 | /// Determine whether the no unsigned wrap flag is set. | ||||
373 | bool hasNoUnsignedWrap() const LLVM_READONLY__attribute__((__pure__)); | ||||
374 | |||||
375 | /// Determine whether the no signed wrap flag is set. | ||||
376 | bool hasNoSignedWrap() const LLVM_READONLY__attribute__((__pure__)); | ||||
377 | |||||
378 | /// Return true if this operator has flags which may cause this instruction | ||||
379 | /// to evaluate to poison despite having non-poison inputs. | ||||
380 | bool hasPoisonGeneratingFlags() const LLVM_READONLY__attribute__((__pure__)); | ||||
381 | |||||
382 | /// Drops flags that may cause this instruction to evaluate to poison despite | ||||
383 | /// having non-poison inputs. | ||||
384 | void dropPoisonGeneratingFlags(); | ||||
385 | |||||
386 | /// Return true if this instruction has poison-generating metadata. | ||||
387 | bool hasPoisonGeneratingMetadata() const LLVM_READONLY__attribute__((__pure__)); | ||||
388 | |||||
389 | /// Drops metadata that may generate poison. | ||||
390 | void dropPoisonGeneratingMetadata(); | ||||
391 | |||||
392 | /// Return true if this instruction has poison-generating flags or metadata. | ||||
393 | bool hasPoisonGeneratingFlagsOrMetadata() const { | ||||
394 | return hasPoisonGeneratingFlags() || hasPoisonGeneratingMetadata(); | ||||
395 | } | ||||
396 | |||||
397 | /// Drops flags and metadata that may generate poison. | ||||
398 | void dropPoisonGeneratingFlagsAndMetadata() { | ||||
399 | dropPoisonGeneratingFlags(); | ||||
400 | dropPoisonGeneratingMetadata(); | ||||
401 | } | ||||
402 | |||||
403 | /// This function drops non-debug unknown metadata (through | ||||
404 | /// dropUnknownNonDebugMetadata). For calls, it also drops parameter and | ||||
405 | /// return attributes that can cause undefined behaviour. Both of these should | ||||
406 | /// be done by passes which move instructions in IR. | ||||
407 | void | ||||
408 | dropUndefImplyingAttrsAndUnknownMetadata(ArrayRef<unsigned> KnownIDs = {}); | ||||
409 | |||||
410 | /// Determine whether the exact flag is set. | ||||
411 | bool isExact() const LLVM_READONLY__attribute__((__pure__)); | ||||
412 | |||||
413 | /// Set or clear all fast-math-flags on this instruction, which must be an | ||||
414 | /// operator which supports this flag. See LangRef.html for the meaning of | ||||
415 | /// this flag. | ||||
416 | void setFast(bool B); | ||||
417 | |||||
418 | /// Set or clear the reassociation flag on this instruction, which must be | ||||
419 | /// an operator which supports this flag. See LangRef.html for the meaning of | ||||
420 | /// this flag. | ||||
421 | void setHasAllowReassoc(bool B); | ||||
422 | |||||
423 | /// Set or clear the no-nans flag on this instruction, which must be an | ||||
424 | /// operator which supports this flag. See LangRef.html for the meaning of | ||||
425 | /// this flag. | ||||
426 | void setHasNoNaNs(bool B); | ||||
427 | |||||
428 | /// Set or clear the no-infs flag on this instruction, which must be an | ||||
429 | /// operator which supports this flag. See LangRef.html for the meaning of | ||||
430 | /// this flag. | ||||
431 | void setHasNoInfs(bool B); | ||||
432 | |||||
433 | /// Set or clear the no-signed-zeros flag on this instruction, which must be | ||||
434 | /// an operator which supports this flag. See LangRef.html for the meaning of | ||||
435 | /// this flag. | ||||
436 | void setHasNoSignedZeros(bool B); | ||||
437 | |||||
438 | /// Set or clear the allow-reciprocal flag on this instruction, which must be | ||||
439 | /// an operator which supports this flag. See LangRef.html for the meaning of | ||||
440 | /// this flag. | ||||
441 | void setHasAllowReciprocal(bool B); | ||||
442 | |||||
443 | /// Set or clear the allow-contract flag on this instruction, which must be | ||||
444 | /// an operator which supports this flag. See LangRef.html for the meaning of | ||||
445 | /// this flag. | ||||
446 | void setHasAllowContract(bool B); | ||||
447 | |||||
448 | /// Set or clear the approximate-math-functions flag on this instruction, | ||||
449 | /// which must be an operator which supports this flag. See LangRef.html for | ||||
450 | /// the meaning of this flag. | ||||
451 | void setHasApproxFunc(bool B); | ||||
452 | |||||
453 | /// Convenience function for setting multiple fast-math flags on this | ||||
454 | /// instruction, which must be an operator which supports these flags. See | ||||
455 | /// LangRef.html for the meaning of these flags. | ||||
456 | void setFastMathFlags(FastMathFlags FMF); | ||||
457 | |||||
458 | /// Convenience function for transferring all fast-math flag values to this | ||||
459 | /// instruction, which must be an operator which supports these flags. See | ||||
460 | /// LangRef.html for the meaning of these flags. | ||||
461 | void copyFastMathFlags(FastMathFlags FMF); | ||||
462 | |||||
463 | /// Determine whether all fast-math-flags are set. | ||||
464 | bool isFast() const LLVM_READONLY__attribute__((__pure__)); | ||||
465 | |||||
466 | /// Determine whether the allow-reassociation flag is set. | ||||
467 | bool hasAllowReassoc() const LLVM_READONLY__attribute__((__pure__)); | ||||
468 | |||||
469 | /// Determine whether the no-NaNs flag is set. | ||||
470 | bool hasNoNaNs() const LLVM_READONLY__attribute__((__pure__)); | ||||
471 | |||||
472 | /// Determine whether the no-infs flag is set. | ||||
473 | bool hasNoInfs() const LLVM_READONLY__attribute__((__pure__)); | ||||
474 | |||||
475 | /// Determine whether the no-signed-zeros flag is set. | ||||
476 | bool hasNoSignedZeros() const LLVM_READONLY__attribute__((__pure__)); | ||||
477 | |||||
478 | /// Determine whether the allow-reciprocal flag is set. | ||||
479 | bool hasAllowReciprocal() const LLVM_READONLY__attribute__((__pure__)); | ||||
480 | |||||
481 | /// Determine whether the allow-contract flag is set. | ||||
482 | bool hasAllowContract() const LLVM_READONLY__attribute__((__pure__)); | ||||
483 | |||||
484 | /// Determine whether the approximate-math-functions flag is set. | ||||
485 | bool hasApproxFunc() const LLVM_READONLY__attribute__((__pure__)); | ||||
486 | |||||
487 | /// Convenience function for getting all the fast-math flags, which must be an | ||||
488 | /// operator which supports these flags. See LangRef.html for the meaning of | ||||
489 | /// these flags. | ||||
490 | FastMathFlags getFastMathFlags() const LLVM_READONLY__attribute__((__pure__)); | ||||
491 | |||||
492 | /// Copy I's fast-math flags | ||||
493 | void copyFastMathFlags(const Instruction *I); | ||||
494 | |||||
495 | /// Convenience method to copy supported exact, fast-math, and (optionally) | ||||
496 | /// wrapping flags from V to this instruction. | ||||
497 | void copyIRFlags(const Value *V, bool IncludeWrapFlags = true); | ||||
498 | |||||
499 | /// Logical 'and' of any supported wrapping, exact, and fast-math flags of | ||||
500 | /// V and this instruction. | ||||
501 | void andIRFlags(const Value *V); | ||||
502 | |||||
503 | /// Merge 2 debug locations and apply it to the Instruction. If the | ||||
504 | /// instruction is a CallIns, we need to traverse the inline chain to find | ||||
505 | /// the common scope. This is not efficient for N-way merging as each time | ||||
506 | /// you merge 2 iterations, you need to rebuild the hashmap to find the | ||||
507 | /// common scope. However, we still choose this API because: | ||||
508 | /// 1) Simplicity: it takes 2 locations instead of a list of locations. | ||||
509 | /// 2) In worst case, it increases the complexity from O(N*I) to | ||||
510 | /// O(2*N*I), where N is # of Instructions to merge, and I is the | ||||
511 | /// maximum level of inline stack. So it is still linear. | ||||
512 | /// 3) Merging of call instructions should be extremely rare in real | ||||
513 | /// applications, thus the N-way merging should be in code path. | ||||
514 | /// The DebugLoc attached to this instruction will be overwritten by the | ||||
515 | /// merged DebugLoc. | ||||
516 | void applyMergedLocation(const DILocation *LocA, const DILocation *LocB); | ||||
517 | |||||
518 | /// Updates the debug location given that the instruction has been hoisted | ||||
519 | /// from a block to a predecessor of that block. | ||||
520 | /// Note: it is undefined behavior to call this on an instruction not | ||||
521 | /// currently inserted into a function. | ||||
522 | void updateLocationAfterHoist(); | ||||
523 | |||||
524 | /// Drop the instruction's debug location. This does not guarantee removal | ||||
525 | /// of the !dbg source location attachment, as it must set a line 0 location | ||||
526 | /// with scope information attached on call instructions. To guarantee | ||||
527 | /// removal of the !dbg attachment, use the \ref setDebugLoc() API. | ||||
528 | /// Note: it is undefined behavior to call this on an instruction not | ||||
529 | /// currently inserted into a function. | ||||
530 | void dropLocation(); | ||||
531 | |||||
532 | /// Merge the DIAssignID metadata from this instruction and those attached to | ||||
533 | /// instructions in \p SourceInstructions. This process performs a RAUW on | ||||
534 | /// the MetadataAsValue uses of the merged DIAssignID nodes. Not every | ||||
535 | /// instruction in \p SourceInstructions needs to have DIAssignID | ||||
536 | /// metadata. If none of them do then nothing happens. If this instruction | ||||
537 | /// does not have a DIAssignID attachment but at least one in \p | ||||
538 | /// SourceInstructions does then the merged one will be attached to | ||||
539 | /// it. However, instructions without attachments in \p SourceInstructions | ||||
540 | /// are not modified. | ||||
541 | void mergeDIAssignID(ArrayRef<const Instruction *> SourceInstructions); | ||||
542 | |||||
543 | private: | ||||
544 | // These are all implemented in Metadata.cpp. | ||||
545 | MDNode *getMetadataImpl(unsigned KindID) const; | ||||
546 | MDNode *getMetadataImpl(StringRef Kind) const; | ||||
547 | void | ||||
548 | getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const; | ||||
549 | |||||
550 | /// Update the LLVMContext ID-to-Instruction(s) mapping. If \p ID is nullptr | ||||
551 | /// then clear the mapping for this instruction. | ||||
552 | void updateDIAssignIDMapping(DIAssignID *ID); | ||||
553 | |||||
554 | public: | ||||
555 | //===--------------------------------------------------------------------===// | ||||
556 | // Predicates and helper methods. | ||||
557 | //===--------------------------------------------------------------------===// | ||||
558 | |||||
559 | /// Return true if the instruction is associative: | ||||
560 | /// | ||||
561 | /// Associative operators satisfy: x op (y op z) === (x op y) op z | ||||
562 | /// | ||||
563 | /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative. | ||||
564 | /// | ||||
565 | bool isAssociative() const LLVM_READONLY__attribute__((__pure__)); | ||||
566 | static bool isAssociative(unsigned Opcode) { | ||||
567 | return Opcode == And || Opcode == Or || Opcode == Xor || | ||||
568 | Opcode == Add || Opcode == Mul; | ||||
569 | } | ||||
570 | |||||
571 | /// Return true if the instruction is commutative: | ||||
572 | /// | ||||
573 | /// Commutative operators satisfy: (x op y) === (y op x) | ||||
574 | /// | ||||
575 | /// In LLVM, these are the commutative operators, plus SetEQ and SetNE, when | ||||
576 | /// applied to any type. | ||||
577 | /// | ||||
578 | bool isCommutative() const LLVM_READONLY__attribute__((__pure__)); | ||||
579 | static bool isCommutative(unsigned Opcode) { | ||||
580 | switch (Opcode) { | ||||
581 | case Add: case FAdd: | ||||
582 | case Mul: case FMul: | ||||
583 | case And: case Or: case Xor: | ||||
584 | return true; | ||||
585 | default: | ||||
586 | return false; | ||||
587 | } | ||||
588 | } | ||||
589 | |||||
590 | /// Return true if the instruction is idempotent: | ||||
591 | /// | ||||
592 | /// Idempotent operators satisfy: x op x === x | ||||
593 | /// | ||||
594 | /// In LLVM, the And and Or operators are idempotent. | ||||
595 | /// | ||||
596 | bool isIdempotent() const { return isIdempotent(getOpcode()); } | ||||
597 | static bool isIdempotent(unsigned Opcode) { | ||||
598 | return Opcode == And || Opcode == Or; | ||||
599 | } | ||||
600 | |||||
601 | /// Return true if the instruction is nilpotent: | ||||
602 | /// | ||||
603 | /// Nilpotent operators satisfy: x op x === Id, | ||||
604 | /// | ||||
605 | /// where Id is the identity for the operator, i.e. a constant such that | ||||
606 | /// x op Id === x and Id op x === x for all x. | ||||
607 | /// | ||||
608 | /// In LLVM, the Xor operator is nilpotent. | ||||
609 | /// | ||||
610 | bool isNilpotent() const { return isNilpotent(getOpcode()); } | ||||
611 | static bool isNilpotent(unsigned Opcode) { | ||||
612 | return Opcode == Xor; | ||||
613 | } | ||||
614 | |||||
615 | /// Return true if this instruction may modify memory. | ||||
616 | bool mayWriteToMemory() const LLVM_READONLY__attribute__((__pure__)); | ||||
617 | |||||
618 | /// Return true if this instruction may read memory. | ||||
619 | bool mayReadFromMemory() const LLVM_READONLY__attribute__((__pure__)); | ||||
620 | |||||
621 | /// Return true if this instruction may read or write memory. | ||||
622 | bool mayReadOrWriteMemory() const { | ||||
623 | return mayReadFromMemory() || mayWriteToMemory(); | ||||
624 | } | ||||
625 | |||||
626 | /// Return true if this instruction has an AtomicOrdering of unordered or | ||||
627 | /// higher. | ||||
628 | bool isAtomic() const LLVM_READONLY__attribute__((__pure__)); | ||||
629 | |||||
630 | /// Return true if this atomic instruction loads from memory. | ||||
631 | bool hasAtomicLoad() const LLVM_READONLY__attribute__((__pure__)); | ||||
632 | |||||
633 | /// Return true if this atomic instruction stores to memory. | ||||
634 | bool hasAtomicStore() const LLVM_READONLY__attribute__((__pure__)); | ||||
635 | |||||
636 | /// Return true if this instruction has a volatile memory access. | ||||
637 | bool isVolatile() const LLVM_READONLY__attribute__((__pure__)); | ||||
638 | |||||
639 | /// Return true if this instruction may throw an exception. | ||||
640 | bool mayThrow() const LLVM_READONLY__attribute__((__pure__)); | ||||
641 | |||||
642 | /// Return true if this instruction behaves like a memory fence: it can load | ||||
643 | /// or store to memory location without being given a memory location. | ||||
644 | bool isFenceLike() const { | ||||
645 | switch (getOpcode()) { | ||||
646 | default: | ||||
647 | return false; | ||||
648 | // This list should be kept in sync with the list in mayWriteToMemory for | ||||
649 | // all opcodes which don't have a memory location. | ||||
650 | case Instruction::Fence: | ||||
651 | case Instruction::CatchPad: | ||||
652 | case Instruction::CatchRet: | ||||
653 | case Instruction::Call: | ||||
654 | case Instruction::Invoke: | ||||
655 | return true; | ||||
656 | } | ||||
657 | } | ||||
658 | |||||
659 | /// Return true if the instruction may have side effects. | ||||
660 | /// | ||||
661 | /// Side effects are: | ||||
662 | /// * Writing to memory. | ||||
663 | /// * Unwinding. | ||||
664 | /// * Not returning (e.g. an infinite loop). | ||||
665 | /// | ||||
666 | /// Note that this does not consider malloc and alloca to have side | ||||
667 | /// effects because the newly allocated memory is completely invisible to | ||||
668 | /// instructions which don't use the returned value. For cases where this | ||||
669 | /// matters, isSafeToSpeculativelyExecute may be more appropriate. | ||||
670 | bool mayHaveSideEffects() const LLVM_READONLY__attribute__((__pure__)); | ||||
671 | |||||
672 | /// Return true if the instruction can be removed if the result is unused. | ||||
673 | /// | ||||
674 | /// When constant folding some instructions cannot be removed even if their | ||||
675 | /// results are unused. Specifically terminator instructions and calls that | ||||
676 | /// may have side effects cannot be removed without semantically changing the | ||||
677 | /// generated program. | ||||
678 | bool isSafeToRemove() const LLVM_READONLY__attribute__((__pure__)); | ||||
679 | |||||
680 | /// Return true if the instruction will return (unwinding is considered as | ||||
681 | /// a form of returning control flow here). | ||||
682 | bool willReturn() const LLVM_READONLY__attribute__((__pure__)); | ||||
683 | |||||
684 | /// Return true if the instruction is a variety of EH-block. | ||||
685 | bool isEHPad() const { | ||||
686 | switch (getOpcode()) { | ||||
687 | case Instruction::CatchSwitch: | ||||
688 | case Instruction::CatchPad: | ||||
689 | case Instruction::CleanupPad: | ||||
690 | case Instruction::LandingPad: | ||||
691 | return true; | ||||
692 | default: | ||||
693 | return false; | ||||
694 | } | ||||
695 | } | ||||
696 | |||||
697 | /// Return true if the instruction is a llvm.lifetime.start or | ||||
698 | /// llvm.lifetime.end marker. | ||||
699 | bool isLifetimeStartOrEnd() const LLVM_READONLY__attribute__((__pure__)); | ||||
700 | |||||
701 | /// Return true if the instruction is a llvm.launder.invariant.group or | ||||
702 | /// llvm.strip.invariant.group. | ||||
703 | bool isLaunderOrStripInvariantGroup() const LLVM_READONLY__attribute__((__pure__)); | ||||
704 | |||||
705 | /// Return true if the instruction is a DbgInfoIntrinsic or PseudoProbeInst. | ||||
706 | bool isDebugOrPseudoInst() const LLVM_READONLY__attribute__((__pure__)); | ||||
707 | |||||
708 | /// Return a pointer to the next non-debug instruction in the same basic | ||||
709 | /// block as 'this', or nullptr if no such instruction exists. Skip any pseudo | ||||
710 | /// operations if \c SkipPseudoOp is true. | ||||
711 | const Instruction * | ||||
712 | getNextNonDebugInstruction(bool SkipPseudoOp = false) const; | ||||
713 | Instruction *getNextNonDebugInstruction(bool SkipPseudoOp = false) { | ||||
714 | return const_cast<Instruction *>( | ||||
715 | static_cast<const Instruction *>(this)->getNextNonDebugInstruction( | ||||
716 | SkipPseudoOp)); | ||||
717 | } | ||||
718 | |||||
719 | /// Return a pointer to the previous non-debug instruction in the same basic | ||||
720 | /// block as 'this', or nullptr if no such instruction exists. Skip any pseudo | ||||
721 | /// operations if \c SkipPseudoOp is true. | ||||
722 | const Instruction * | ||||
723 | getPrevNonDebugInstruction(bool SkipPseudoOp = false) const; | ||||
724 | Instruction *getPrevNonDebugInstruction(bool SkipPseudoOp = false) { | ||||
725 | return const_cast<Instruction *>( | ||||
726 | static_cast<const Instruction *>(this)->getPrevNonDebugInstruction( | ||||
727 | SkipPseudoOp)); | ||||
728 | } | ||||
729 | |||||
730 | /// Create a copy of 'this' instruction that is identical in all ways except | ||||
731 | /// the following: | ||||
732 | /// * The instruction has no parent | ||||
733 | /// * The instruction has no name | ||||
734 | /// | ||||
735 | Instruction *clone() const; | ||||
736 | |||||
737 | /// Return true if the specified instruction is exactly identical to the | ||||
738 | /// current one. This means that all operands match and any extra information | ||||
739 | /// (e.g. load is volatile) agree. | ||||
740 | bool isIdenticalTo(const Instruction *I) const LLVM_READONLY__attribute__((__pure__)); | ||||
741 | |||||
742 | /// This is like isIdenticalTo, except that it ignores the | ||||
743 | /// SubclassOptionalData flags, which may specify conditions under which the | ||||
744 | /// instruction's result is undefined. | ||||
745 | bool isIdenticalToWhenDefined(const Instruction *I) const LLVM_READONLY__attribute__((__pure__)); | ||||
746 | |||||
747 | /// When checking for operation equivalence (using isSameOperationAs) it is | ||||
748 | /// sometimes useful to ignore certain attributes. | ||||
749 | enum OperationEquivalenceFlags { | ||||
750 | /// Check for equivalence ignoring load/store alignment. | ||||
751 | CompareIgnoringAlignment = 1<<0, | ||||
752 | /// Check for equivalence treating a type and a vector of that type | ||||
753 | /// as equivalent. | ||||
754 | CompareUsingScalarTypes = 1<<1 | ||||
755 | }; | ||||
756 | |||||
757 | /// This function determines if the specified instruction executes the same | ||||
758 | /// operation as the current one. This means that the opcodes, type, operand | ||||
759 | /// types and any other factors affecting the operation must be the same. This | ||||
760 | /// is similar to isIdenticalTo except the operands themselves don't have to | ||||
761 | /// be identical. | ||||
762 | /// @returns true if the specified instruction is the same operation as | ||||
763 | /// the current one. | ||||
764 | /// Determine if one instruction is the same operation as another. | ||||
765 | bool isSameOperationAs(const Instruction *I, unsigned flags = 0) const LLVM_READONLY__attribute__((__pure__)); | ||||
766 | |||||
767 | /// Return true if there are any uses of this instruction in blocks other than | ||||
768 | /// the specified block. Note that PHI nodes are considered to evaluate their | ||||
769 | /// operands in the corresponding predecessor block. | ||||
770 | bool isUsedOutsideOfBlock(const BasicBlock *BB) const LLVM_READONLY__attribute__((__pure__)); | ||||
771 | |||||
772 | /// Return the number of successors that this instruction has. The instruction | ||||
773 | /// must be a terminator. | ||||
774 | unsigned getNumSuccessors() const LLVM_READONLY__attribute__((__pure__)); | ||||
775 | |||||
776 | /// Return the specified successor. This instruction must be a terminator. | ||||
777 | BasicBlock *getSuccessor(unsigned Idx) const LLVM_READONLY__attribute__((__pure__)); | ||||
778 | |||||
779 | /// Update the specified successor to point at the provided block. This | ||||
780 | /// instruction must be a terminator. | ||||
781 | void setSuccessor(unsigned Idx, BasicBlock *BB); | ||||
782 | |||||
783 | /// Replace specified successor OldBB to point at the provided block. | ||||
784 | /// This instruction must be a terminator. | ||||
785 | void replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB); | ||||
786 | |||||
787 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | ||||
788 | static bool classof(const Value *V) { | ||||
789 | return V->getValueID() >= Value::InstructionVal; | ||||
790 | } | ||||
791 | |||||
792 | //---------------------------------------------------------------------- | ||||
793 | // Exported enumerations. | ||||
794 | // | ||||
795 | enum TermOps { // These terminate basic blocks | ||||
796 | #define FIRST_TERM_INST(N) TermOpsBegin = N, | ||||
797 | #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N, | ||||
798 | #define LAST_TERM_INST(N) TermOpsEnd = N+1 | ||||
799 | #include "llvm/IR/Instruction.def" | ||||
800 | }; | ||||
801 | |||||
802 | enum UnaryOps { | ||||
803 | #define FIRST_UNARY_INST(N) UnaryOpsBegin = N, | ||||
804 | #define HANDLE_UNARY_INST(N, OPC, CLASS) OPC = N, | ||||
805 | #define LAST_UNARY_INST(N) UnaryOpsEnd = N+1 | ||||
806 | #include "llvm/IR/Instruction.def" | ||||
807 | }; | ||||
808 | |||||
809 | enum BinaryOps { | ||||
810 | #define FIRST_BINARY_INST(N) BinaryOpsBegin = N, | ||||
811 | #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N, | ||||
812 | #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1 | ||||
813 | #include "llvm/IR/Instruction.def" | ||||
814 | }; | ||||
815 | |||||
816 | enum MemoryOps { | ||||
817 | #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N, | ||||
818 | #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N, | ||||
819 | #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1 | ||||
820 | #include "llvm/IR/Instruction.def" | ||||
821 | }; | ||||
822 | |||||
823 | enum CastOps { | ||||
824 | #define FIRST_CAST_INST(N) CastOpsBegin = N, | ||||
825 | #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N, | ||||
826 | #define LAST_CAST_INST(N) CastOpsEnd = N+1 | ||||
827 | #include "llvm/IR/Instruction.def" | ||||
828 | }; | ||||
829 | |||||
830 | enum FuncletPadOps { | ||||
831 | #define FIRST_FUNCLETPAD_INST(N) FuncletPadOpsBegin = N, | ||||
832 | #define HANDLE_FUNCLETPAD_INST(N, OPC, CLASS) OPC = N, | ||||
833 | #define LAST_FUNCLETPAD_INST(N) FuncletPadOpsEnd = N+1 | ||||
834 | #include "llvm/IR/Instruction.def" | ||||
835 | }; | ||||
836 | |||||
837 | enum OtherOps { | ||||
838 | #define FIRST_OTHER_INST(N) OtherOpsBegin = N, | ||||
839 | #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N, | ||||
840 | #define LAST_OTHER_INST(N) OtherOpsEnd = N+1 | ||||
841 | #include "llvm/IR/Instruction.def" | ||||
842 | }; | ||||
843 | |||||
844 | private: | ||||
845 | friend class SymbolTableListTraits<Instruction>; | ||||
846 | friend class BasicBlock; // For renumbering. | ||||
847 | |||||
848 | // Shadow Value::setValueSubclassData with a private forwarding method so that | ||||
849 | // subclasses cannot accidentally use it. | ||||
850 | void setValueSubclassData(unsigned short D) { | ||||
851 | Value::setValueSubclassData(D); | ||||
852 | } | ||||
853 | |||||
854 | unsigned short getSubclassDataFromValue() const { | ||||
855 | return Value::getSubclassDataFromValue(); | ||||
856 | } | ||||
857 | |||||
858 | void setParent(BasicBlock *P); | ||||
859 | |||||
860 | protected: | ||||
861 | // Instruction subclasses can stick up to 15 bits of stuff into the | ||||
862 | // SubclassData field of instruction with these members. | ||||
863 | |||||
864 | template <typename BitfieldElement> | ||||
865 | typename BitfieldElement::Type getSubclassData() const { | ||||
866 | static_assert( | ||||
867 | std::is_same<BitfieldElement, HasMetadataField>::value || | ||||
868 | !Bitfield::isOverlapping<BitfieldElement, HasMetadataField>(), | ||||
869 | "Must not overlap with the metadata bit"); | ||||
870 | return Bitfield::get<BitfieldElement>(getSubclassDataFromValue()); | ||||
871 | } | ||||
872 | |||||
873 | template <typename BitfieldElement> | ||||
874 | void setSubclassData(typename BitfieldElement::Type Value) { | ||||
875 | static_assert( | ||||
876 | std::is_same<BitfieldElement, HasMetadataField>::value || | ||||
877 | !Bitfield::isOverlapping<BitfieldElement, HasMetadataField>(), | ||||
878 | "Must not overlap with the metadata bit"); | ||||
879 | auto Storage = getSubclassDataFromValue(); | ||||
880 | Bitfield::set<BitfieldElement>(Storage, Value); | ||||
881 | setValueSubclassData(Storage); | ||||
882 | } | ||||
883 | |||||
884 | Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, | ||||
885 | Instruction *InsertBefore = nullptr); | ||||
886 | Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps, | ||||
887 | BasicBlock *InsertAtEnd); | ||||
888 | |||||
889 | private: | ||||
890 | /// Create a copy of this instruction. | ||||
891 | Instruction *cloneImpl() const; | ||||
892 | }; | ||||
893 | |||||
894 | inline void ilist_alloc_traits<Instruction>::deleteNode(Instruction *V) { | ||||
895 | V->deleteValue(); | ||||
896 | } | ||||
897 | |||||
898 | } // end namespace llvm | ||||
899 | |||||
900 | #endif // LLVM_IR_INSTRUCTION_H |