LLVM 19.0.0git
ConstantFolding.cpp
Go to the documentation of this file.
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
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"
26#include "llvm/ADT/StringRef.h"
31#include "llvm/Config/config.h"
32#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DataLayout.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/GlobalValue.h"
40#include "llvm/IR/InstrTypes.h"
41#include "llvm/IR/Instruction.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"
57#include <cassert>
58#include <cerrno>
59#include <cfenv>
60#include <cmath>
61#include <cstdint>
62
63using namespace llvm;
64
65namespace {
66
67//===----------------------------------------------------------------------===//
68// Constant Folding internal helper functions
69//===----------------------------------------------------------------------===//
70
71static 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.
104Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
105 assert(CastInst::castIsValid(Instruction::BitCast, C, DestTy) &&
106 "Invalid constantexpr bitcast!");
107
108 // Catch the obvious splat cases.
109 if (Constant *Res = ConstantFoldLoadFromUniformValue(C, DestTy, DL))
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
209 if (NumDstElt < NumSrcElt) {
210 // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
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))
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 = ConstantFoldCastOperand(Instruction::ZExt, Src, Elt->getType(),
231 DL);
232 assert(Src && "Constant folding cannot fail on plain integers");
233
234 // Shift it to the right place, depending on endianness.
236 Instruction::Shl, Src, ConstantInt::get(Src->getType(), ShiftAmt),
237 DL);
238 assert(Src && "Constant folding cannot fail on plain integers");
239
240 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
241
242 // Mix it in.
243 Elt = ConstantFoldBinaryOpOperands(Instruction::Or, Elt, Src, DL);
244 assert(Elt && "Constant folding cannot fail on plain integers");
245 }
246 Result.push_back(Elt);
247 }
248 return ConstantVector::get(Result);
249 }
250
251 // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
252 unsigned Ratio = NumDstElt/NumSrcElt;
253 unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy);
254
255 // Loop over each source value, expanding into multiple results.
256 for (unsigned i = 0; i != NumSrcElt; ++i) {
257 auto *Element = C->getAggregateElement(i);
258
259 if (!Element) // Reject constantexpr elements.
260 return ConstantExpr::getBitCast(C, DestTy);
261
262 if (isa<UndefValue>(Element)) {
263 // Correctly Propagate undef values.
264 Result.append(Ratio, UndefValue::get(DstEltTy));
265 continue;
266 }
267
268 auto *Src = dyn_cast<ConstantInt>(Element);
269 if (!Src)
270 return ConstantExpr::getBitCast(C, DestTy);
271
272 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
273 for (unsigned j = 0; j != Ratio; ++j) {
274 // Shift the piece of the value into the right place, depending on
275 // endianness.
276 APInt Elt = Src->getValue().lshr(ShiftAmt);
277 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
278
279 // Truncate and remember this piece.
280 Result.push_back(ConstantInt::get(DstEltTy, Elt.trunc(DstBitSize)));
281 }
282 }
283
284 return ConstantVector::get(Result);
285}
286
287} // end anonymous namespace
288
289/// If this constant is a constant offset from a global, return the global and
290/// the constant. Because of constantexprs, this function is recursive.
292 APInt &Offset, const DataLayout &DL,
293 DSOLocalEquivalent **DSOEquiv) {
294 if (DSOEquiv)
295 *DSOEquiv = nullptr;
296
297 // Trivial case, constant is the global.
298 if ((GV = dyn_cast<GlobalValue>(C))) {
299 unsigned BitWidth = DL.getIndexTypeSizeInBits(GV->getType());
300 Offset = APInt(BitWidth, 0);
301 return true;
302 }
303
304 if (auto *FoundDSOEquiv = dyn_cast<DSOLocalEquivalent>(C)) {
305 if (DSOEquiv)
306 *DSOEquiv = FoundDSOEquiv;
307 GV = FoundDSOEquiv->getGlobalValue();
308 unsigned BitWidth = DL.getIndexTypeSizeInBits(GV->getType());
309 Offset = APInt(BitWidth, 0);
310 return true;
311 }
312
313 // Otherwise, if this isn't a constant expr, bail out.
314 auto *CE = dyn_cast<ConstantExpr>(C);
315 if (!CE) return false;
316
317 // Look through ptr->int and ptr->ptr casts.
318 if (CE->getOpcode() == Instruction::PtrToInt ||
319 CE->getOpcode() == Instruction::BitCast)
320 return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL,
321 DSOEquiv);
322
323 // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
324 auto *GEP = dyn_cast<GEPOperator>(CE);
325 if (!GEP)
326 return false;
327
328 unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
329 APInt TmpOffset(BitWidth, 0);
330
331 // If the base isn't a global+constant, we aren't either.
332 if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, DL,
333 DSOEquiv))
334 return false;
335
336 // Otherwise, add any offset that our operands provide.
337 if (!GEP->accumulateConstantOffset(DL, TmpOffset))
338 return false;
339
340 Offset = TmpOffset;
341 return true;
342}
343
345 const DataLayout &DL) {
346 do {
347 Type *SrcTy = C->getType();
348 if (SrcTy == DestTy)
349 return C;
350
351 TypeSize DestSize = DL.getTypeSizeInBits(DestTy);
352 TypeSize SrcSize = DL.getTypeSizeInBits(SrcTy);
353 if (!TypeSize::isKnownGE(SrcSize, DestSize))
354 return nullptr;
355
356 // Catch the obvious splat cases (since all-zeros can coerce non-integral
357 // pointers legally).
358 if (Constant *Res = ConstantFoldLoadFromUniformValue(C, DestTy, DL))
359 return Res;
360
361 // If the type sizes are the same and a cast is legal, just directly
362 // cast the constant.
363 // But be careful not to coerce non-integral pointers illegally.
364 if (SrcSize == DestSize &&
365 DL.isNonIntegralPointerType(SrcTy->getScalarType()) ==
366 DL.isNonIntegralPointerType(DestTy->getScalarType())) {
367 Instruction::CastOps Cast = Instruction::BitCast;
368 // If we are going from a pointer to int or vice versa, we spell the cast
369 // differently.
370 if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
371 Cast = Instruction::IntToPtr;
372 else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
373 Cast = Instruction::PtrToInt;
374
375 if (CastInst::castIsValid(Cast, C, DestTy))
376 return ConstantFoldCastOperand(Cast, C, DestTy, DL);
377 }
378
379 // If this isn't an aggregate type, there is nothing we can do to drill down
380 // and find a bitcastable constant.
381 if (!SrcTy->isAggregateType() && !SrcTy->isVectorTy())
382 return nullptr;
383
384 // We're simulating a load through a pointer that was bitcast to point to
385 // a different type, so we can try to walk down through the initial
386 // elements of an aggregate to see if some part of the aggregate is
387 // castable to implement the "load" semantic model.
388 if (SrcTy->isStructTy()) {
389 // Struct types might have leading zero-length elements like [0 x i32],
390 // which are certainly not what we are looking for, so skip them.
391 unsigned Elem = 0;
392 Constant *ElemC;
393 do {
394 ElemC = C->getAggregateElement(Elem++);
395 } while (ElemC && DL.getTypeSizeInBits(ElemC->getType()).isZero());
396 C = ElemC;
397 } else {
398 // For non-byte-sized vector elements, the first element is not
399 // necessarily located at the vector base address.
400 if (auto *VT = dyn_cast<VectorType>(SrcTy))
401 if (!DL.typeSizeEqualsStoreSize(VT->getElementType()))
402 return nullptr;
403
404 C = C->getAggregateElement(0u);
405 }
406 } while (C);
407
408 return nullptr;
409}
410
411namespace {
412
413/// Recursive helper to read bits out of global. C is the constant being copied
414/// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy
415/// results into and BytesLeft is the number of bytes left in
416/// the CurPtr buffer. DL is the DataLayout.
417bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr,
418 unsigned BytesLeft, const DataLayout &DL) {
419 assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) &&
420 "Out of range access");
421
422 // If this element is zero or undefined, we can just return since *CurPtr is
423 // zero initialized.
424 if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
425 return true;
426
427 if (auto *CI = dyn_cast<ConstantInt>(C)) {
428 if ((CI->getBitWidth() & 7) != 0)
429 return false;
430 const APInt &Val = CI->getValue();
431 unsigned IntBytes = unsigned(CI->getBitWidth()/8);
432
433 for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
434 unsigned n = ByteOffset;
435 if (!DL.isLittleEndian())
436 n = IntBytes - n - 1;
437 CurPtr[i] = Val.extractBits(8, n * 8).getZExtValue();
438 ++ByteOffset;
439 }
440 return true;
441 }
442
443 if (auto *CFP = dyn_cast<ConstantFP>(C)) {
444 if (CFP->getType()->isDoubleTy()) {
445 C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL);
446 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
447 }
448 if (CFP->getType()->isFloatTy()){
449 C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), DL);
450 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
451 }
452 if (CFP->getType()->isHalfTy()){
453 C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), DL);
454 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
455 }
456 return false;
457 }
458
459 if (auto *CS = dyn_cast<ConstantStruct>(C)) {
460 const StructLayout *SL = DL.getStructLayout(CS->getType());
461 unsigned Index = SL->getElementContainingOffset(ByteOffset);
462 uint64_t CurEltOffset = SL->getElementOffset(Index);
463 ByteOffset -= CurEltOffset;
464
465 while (true) {
466 // If the element access is to the element itself and not to tail padding,
467 // read the bytes from the element.
468 uint64_t EltSize = DL.getTypeAllocSize(CS->getOperand(Index)->getType());
469
470 if (ByteOffset < EltSize &&
471 !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
472 BytesLeft, DL))
473 return false;
474
475 ++Index;
476
477 // Check to see if we read from the last struct element, if so we're done.
478 if (Index == CS->getType()->getNumElements())
479 return true;
480
481 // If we read all of the bytes we needed from this element we're done.
482 uint64_t NextEltOffset = SL->getElementOffset(Index);
483
484 if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
485 return true;
486
487 // Move to the next element of the struct.
488 CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
489 BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
490 ByteOffset = 0;
491 CurEltOffset = NextEltOffset;
492 }
493 // not reached.
494 }
495
496 if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
497 isa<ConstantDataSequential>(C)) {
498 uint64_t NumElts, EltSize;
499 Type *EltTy;
500 if (auto *AT = dyn_cast<ArrayType>(C->getType())) {
501 NumElts = AT->getNumElements();
502 EltTy = AT->getElementType();
503 EltSize = DL.getTypeAllocSize(EltTy);
504 } else {
505 NumElts = cast<FixedVectorType>(C->getType())->getNumElements();
506 EltTy = cast<FixedVectorType>(C->getType())->getElementType();
507 // TODO: For non-byte-sized vectors, current implementation assumes there is
508 // padding to the next byte boundary between elements.
509 if (!DL.typeSizeEqualsStoreSize(EltTy))
510 return false;
511
512 EltSize = DL.getTypeStoreSize(EltTy);
513 }
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?");
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
546Constant *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::getIntToPtr(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.
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)
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.
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
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, DL))
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
725 const DataLayout &DL) {
726 return ConstantFoldLoadFromConst(C, Ty, APInt(64, 0), DL);
727}
728
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, DL);
749}
750
752 const DataLayout &DL) {
753 APInt Offset(DL.getIndexTypeSizeInBits(C->getType()), 0);
754 return ConstantFoldLoadFromConstPtr(C, Ty, std::move(Offset), DL);
755}
756
758 const DataLayout &DL) {
759 if (isa<PoisonValue>(C))
760 return PoisonValue::get(Ty);
761 if (isa<UndefValue>(C))
762 return UndefValue::get(Ty);
763 // If padding is needed when storing C to memory, then it isn't considered as
764 // uniform.
765 if (!DL.typeSizeEqualsStoreSize(C->getType()))
766 return nullptr;
767 if (C->isNullValue() && !Ty->isX86_MMXTy() && !Ty->isX86_AMXTy())
768 return Constant::getNullValue(Ty);
769 if (C->isAllOnesValue() &&
770 (Ty->isIntOrIntVectorTy() || Ty->isFPOrFPVectorTy()))
771 return Constant::getAllOnesValue(Ty);
772 return nullptr;
773}
774
775namespace {
776
777/// One of Op0/Op1 is a constant expression.
778/// Attempt to symbolically evaluate the result of a binary operator merging
779/// these together. If target data info is available, it is provided as DL,
780/// otherwise DL is null.
781Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1,
782 const DataLayout &DL) {
783 // SROA
784
785 // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
786 // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
787 // bits.
788
789 if (Opc == Instruction::And) {
790 KnownBits Known0 = computeKnownBits(Op0, DL);
791 KnownBits Known1 = computeKnownBits(Op1, DL);
792 if ((Known1.One | Known0.Zero).isAllOnes()) {
793 // All the bits of Op0 that the 'and' could be masking are already zero.
794 return Op0;
795 }
796 if ((Known0.One | Known1.Zero).isAllOnes()) {
797 // All the bits of Op1 that the 'and' could be masking are already zero.
798 return Op1;
799 }
800
801 Known0 &= Known1;
802 if (Known0.isConstant())
803 return ConstantInt::get(Op0->getType(), Known0.getConstant());
804 }
805
806 // If the constant expr is something like &A[123] - &A[4].f, fold this into a
807 // constant. This happens frequently when iterating over a global array.
808 if (Opc == Instruction::Sub) {
809 GlobalValue *GV1, *GV2;
810 APInt Offs1, Offs2;
811
812 if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, DL))
813 if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, DL) && GV1 == GV2) {
814 unsigned OpSize = DL.getTypeSizeInBits(Op0->getType());
815
816 // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
817 // PtrToInt may change the bitwidth so we have convert to the right size
818 // first.
819 return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
820 Offs2.zextOrTrunc(OpSize));
821 }
822 }
823
824 return nullptr;
825}
826
827/// If array indices are not pointer-sized integers, explicitly cast them so
828/// that they aren't implicitly casted by the getelementptr.
829Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
830 Type *ResultTy, bool InBounds,
831 std::optional<ConstantRange> InRange,
832 const DataLayout &DL, const TargetLibraryInfo *TLI) {
833 Type *IntIdxTy = DL.getIndexType(ResultTy);
834 Type *IntIdxScalarTy = IntIdxTy->getScalarType();
835
836 bool Any = false;
838 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
839 if ((i == 1 ||
840 !isa<StructType>(GetElementPtrInst::getIndexedType(
841 SrcElemTy, Ops.slice(1, i - 1)))) &&
842 Ops[i]->getType()->getScalarType() != IntIdxScalarTy) {
843 Any = true;
844 Type *NewType =
845 Ops[i]->getType()->isVectorTy() ? IntIdxTy : IntIdxScalarTy;
847 CastInst::getCastOpcode(Ops[i], true, NewType, true), Ops[i], NewType,
848 DL);
849 if (!NewIdx)
850 return nullptr;
851 NewIdxs.push_back(NewIdx);
852 } else
853 NewIdxs.push_back(Ops[i]);
854 }
855
856 if (!Any)
857 return nullptr;
858
859 Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ops[0], NewIdxs,
860 InBounds, InRange);
861 return ConstantFoldConstant(C, DL, TLI);
862}
863
864/// If we can symbolically evaluate the GEP constant expression, do so.
865Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
867 const DataLayout &DL,
868 const TargetLibraryInfo *TLI) {
869 bool InBounds = GEP->isInBounds();
870
871 Type *SrcElemTy = GEP->getSourceElementType();
872 Type *ResElemTy = GEP->getResultElementType();
873 Type *ResTy = GEP->getType();
874 if (!SrcElemTy->isSized() || isa<ScalableVectorType>(SrcElemTy))
875 return nullptr;
876
877 if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy, GEP->isInBounds(),
878 GEP->getInRange(), DL, TLI))
879 return C;
880
881 Constant *Ptr = Ops[0];
882 if (!Ptr->getType()->isPointerTy())
883 return nullptr;
884
885 Type *IntIdxTy = DL.getIndexType(Ptr->getType());
886
887 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
888 if (!isa<ConstantInt>(Ops[i]))
889 return nullptr;
890
891 unsigned BitWidth = DL.getTypeSizeInBits(IntIdxTy);
893 BitWidth,
894 DL.getIndexedOffsetInType(
895 SrcElemTy, ArrayRef((Value *const *)Ops.data() + 1, Ops.size() - 1)));
896
897 std::optional<ConstantRange> InRange = GEP->getInRange();
898 if (InRange)
899 InRange = InRange->sextOrTrunc(BitWidth);
900
901 // If this is a GEP of a GEP, fold it all into a single GEP.
902 while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
903 InBounds &= GEP->isInBounds();
904
905 SmallVector<Value *, 4> NestedOps(llvm::drop_begin(GEP->operands()));
906
907 // Do not try the incorporate the sub-GEP if some index is not a number.
908 bool AllConstantInt = true;
909 for (Value *NestedOp : NestedOps)
910 if (!isa<ConstantInt>(NestedOp)) {
911 AllConstantInt = false;
912 break;
913 }
914 if (!AllConstantInt)
915 break;
916
917 // TODO: Try to intersect two inrange attributes?
918 if (!InRange) {
919 InRange = GEP->getInRange();
920 if (InRange)
921 // Adjust inrange by offset until now.
922 InRange = InRange->sextOrTrunc(BitWidth).subtract(Offset);
923 }
924
925 Ptr = cast<Constant>(GEP->getOperand(0));
926 SrcElemTy = GEP->getSourceElementType();
927 Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps));
928 }
929
930 // If the base value for this address is a literal integer value, fold the
931 // getelementptr to the resulting integer value casted to the pointer type.
933 if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) {
934 if (CE->getOpcode() == Instruction::IntToPtr) {
935 if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
936 BasePtr = Base->getValue().zextOrTrunc(BitWidth);
937 }
938 }
939
940 auto *PTy = cast<PointerType>(Ptr->getType());
941 if ((Ptr->isNullValue() || BasePtr != 0) &&
942 !DL.isNonIntegralPointerType(PTy)) {
943 Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr);
944 return ConstantExpr::getIntToPtr(C, ResTy);
945 }
946
947 // Otherwise form a regular getelementptr. Recompute the indices so that
948 // we eliminate over-indexing of the notional static type array bounds.
949 // This makes it easy to determine if the getelementptr is "inbounds".
950
951 // For GEPs of GlobalValues, use the value type, otherwise use an i8 GEP.
952 if (auto *GV = dyn_cast<GlobalValue>(Ptr))
953 SrcElemTy = GV->getValueType();
954 else
955 SrcElemTy = Type::getInt8Ty(Ptr->getContext());
956
957 if (!SrcElemTy->isSized())
958 return nullptr;
959
960 Type *ElemTy = SrcElemTy;
961 SmallVector<APInt> Indices = DL.getGEPIndicesForOffset(ElemTy, Offset);
962 if (Offset != 0)
963 return nullptr;
964
965 // Try to add additional zero indices to reach the desired result element
966 // type.
967 // TODO: Should we avoid extra zero indices if ResElemTy can't be reached and
968 // we'll have to insert a bitcast anyway?
969 while (ElemTy != ResElemTy) {
970 Type *NextTy = GetElementPtrInst::getTypeAtIndex(ElemTy, (uint64_t)0);
971 if (!NextTy)
972 break;
973
974 Indices.push_back(APInt::getZero(isa<StructType>(ElemTy) ? 32 : BitWidth));
975 ElemTy = NextTy;
976 }
977
979 for (const APInt &Index : Indices)
980 NewIdxs.push_back(ConstantInt::get(
981 Type::getIntNTy(Ptr->getContext(), Index.getBitWidth()), Index));
982
983 return ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs, InBounds,
984 InRange);
985}
986
987/// Attempt to constant fold an instruction with the
988/// specified opcode and operands. If successful, the constant result is
989/// returned, if not, null is returned. Note that this function can fail when
990/// attempting to fold instructions like loads and stores, which have no
991/// constant expression form.
992Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
994 const DataLayout &DL,
995 const TargetLibraryInfo *TLI) {
996 Type *DestTy = InstOrCE->getType();
997
998 if (Instruction::isUnaryOp(Opcode))
999 return ConstantFoldUnaryOpOperand(Opcode, Ops[0], DL);
1000
1001 if (Instruction::isBinaryOp(Opcode)) {
1002 switch (Opcode) {
1003 default:
1004 break;
1005 case Instruction::FAdd:
1006 case Instruction::FSub:
1007 case Instruction::FMul:
1008 case Instruction::FDiv:
1009 case Instruction::FRem:
1010 // Handle floating point instructions separately to account for denormals
1011 // TODO: If a constant expression is being folded rather than an
1012 // instruction, denormals will not be flushed/treated as zero
1013 if (const auto *I = dyn_cast<Instruction>(InstOrCE)) {
1014 return ConstantFoldFPInstOperands(Opcode, Ops[0], Ops[1], DL, I);
1015 }
1016 }
1017 return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL);
1018 }
1019
1020 if (Instruction::isCast(Opcode))
1021 return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL);
1022
1023 if (auto *GEP = dyn_cast<GEPOperator>(InstOrCE)) {
1024 Type *SrcElemTy = GEP->getSourceElementType();
1026 return nullptr;
1027
1028 if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI))
1029 return C;
1030
1031 return ConstantExpr::getGetElementPtr(SrcElemTy, Ops[0], Ops.slice(1),
1032 GEP->isInBounds(), GEP->getInRange());
1033 }
1034
1035 if (auto *CE = dyn_cast<ConstantExpr>(InstOrCE)) {
1036 if (CE->isCompare())
1037 return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
1038 DL, TLI);
1039 return CE->getWithOperands(Ops);
1040 }
1041
1042 switch (Opcode) {
1043 default: return nullptr;
1044 case Instruction::ICmp:
1045 case Instruction::FCmp: {
1046 auto *C = cast<CmpInst>(InstOrCE);
1047 return ConstantFoldCompareInstOperands(C->getPredicate(), Ops[0], Ops[1],
1048 DL, TLI, C);
1049 }
1050 case Instruction::Freeze:
1051 return isGuaranteedNotToBeUndefOrPoison(Ops[0]) ? Ops[0] : nullptr;
1052 case Instruction::Call:
1053 if (auto *F = dyn_cast<Function>(Ops.back())) {
1054 const auto *Call = cast<CallBase>(InstOrCE);
1055 if (canConstantFoldCallTo(Call, F))
1056 return ConstantFoldCall(Call, F, Ops.slice(0, Ops.size() - 1), TLI);
1057 }
1058 return nullptr;
1059 case Instruction::Select:
1060 return ConstantFoldSelectInstruction(Ops[0], Ops[1], Ops[2]);
1061 case Instruction::ExtractElement:
1062 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1063 case Instruction::ExtractValue:
1065 Ops[0], cast<ExtractValueInst>(InstOrCE)->getIndices());
1066 case Instruction::InsertElement:
1067 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1068 case Instruction::InsertValue:
1070 Ops[0], Ops[1], cast<InsertValueInst>(InstOrCE)->getIndices());
1071 case Instruction::ShuffleVector:
1073 Ops[0], Ops[1], cast<ShuffleVectorInst>(InstOrCE)->getShuffleMask());
1074 case Instruction::Load: {
1075 const auto *LI = dyn_cast<LoadInst>(InstOrCE);
1076 if (LI->isVolatile())
1077 return nullptr;
1078 return ConstantFoldLoadFromConstPtr(Ops[0], LI->getType(), DL);
1079 }
1080 }
1081}
1082
1083} // end anonymous namespace
1084
1085//===----------------------------------------------------------------------===//
1086// Constant Folding public APIs
1087//===----------------------------------------------------------------------===//
1088
1089namespace {
1090
1091Constant *
1092ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL,
1093 const TargetLibraryInfo *TLI,
1095 if (!isa<ConstantVector>(C) && !isa<ConstantExpr>(C))
1096 return const_cast<Constant *>(C);
1097
1099 for (const Use &OldU : C->operands()) {
1100 Constant *OldC = cast<Constant>(&OldU);
1101 Constant *NewC = OldC;
1102 // Recursively fold the ConstantExpr's operands. If we have already folded
1103 // a ConstantExpr, we don't have to process it again.
1104 if (isa<ConstantVector>(OldC) || isa<ConstantExpr>(OldC)) {
1105 auto It = FoldedOps.find(OldC);
1106 if (It == FoldedOps.end()) {
1107 NewC = ConstantFoldConstantImpl(OldC, DL, TLI, FoldedOps);
1108 FoldedOps.insert({OldC, NewC});
1109 } else {
1110 NewC = It->second;
1111 }
1112 }
1113 Ops.push_back(NewC);
1114 }
1115
1116 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1117 if (Constant *Res =
1118 ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI))
1119 return Res;
1120 return const_cast<Constant *>(C);
1121 }
1122
1123 assert(isa<ConstantVector>(C));
1124 return ConstantVector::get(Ops);
1125}
1126
1127} // end anonymous namespace
1128
1130 const TargetLibraryInfo *TLI) {
1131 // Handle PHI nodes quickly here...
1132 if (auto *PN = dyn_cast<PHINode>(I)) {
1133 Constant *CommonValue = nullptr;
1134
1136 for (Value *Incoming : PN->incoming_values()) {
1137 // If the incoming value is undef then skip it. Note that while we could
1138 // skip the value if it is equal to the phi node itself we choose not to
1139 // because that would break the rule that constant folding only applies if
1140 // all operands are constants.
1141 if (isa<UndefValue>(Incoming))
1142 continue;
1143 // If the incoming value is not a constant, then give up.
1144 auto *C = dyn_cast<Constant>(Incoming);
1145 if (!C)
1146 return nullptr;
1147 // Fold the PHI's operands.
1148 C = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1149 // If the incoming value is a different constant to
1150 // the one we saw previously, then give up.
1151 if (CommonValue && C != CommonValue)
1152 return nullptr;
1153 CommonValue = C;
1154 }
1155
1156 // If we reach here, all incoming values are the same constant or undef.
1157 return CommonValue ? CommonValue : UndefValue::get(PN->getType());
1158 }
1159
1160 // Scan the operand list, checking to see if they are all constants, if so,
1161 // hand off to ConstantFoldInstOperandsImpl.
1162 if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); }))
1163 return nullptr;
1164
1167 for (const Use &OpU : I->operands()) {
1168 auto *Op = cast<Constant>(&OpU);
1169 // Fold the Instruction's operands.
1170 Op = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps);
1171 Ops.push_back(Op);
1172 }
1173
1174 return ConstantFoldInstOperands(I, Ops, DL, TLI);
1175}
1176
1178 const TargetLibraryInfo *TLI) {
1180 return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1181}
1182
1185 const DataLayout &DL,
1186 const TargetLibraryInfo *TLI) {
1187 return ConstantFoldInstOperandsImpl(I, I->getOpcode(), Ops, DL, TLI);
1188}
1189
1191 unsigned IntPredicate, Constant *Ops0, Constant *Ops1, const DataLayout &DL,
1192 const TargetLibraryInfo *TLI, const Instruction *I) {
1193 CmpInst::Predicate Predicate = (CmpInst::Predicate)IntPredicate;
1194 // fold: icmp (inttoptr x), null -> icmp x, 0
1195 // fold: icmp null, (inttoptr x) -> icmp 0, x
1196 // fold: icmp (ptrtoint x), 0 -> icmp x, null
1197 // fold: icmp 0, (ptrtoint x) -> icmp null, x
1198 // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
1199 // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
1200 //
1201 // FIXME: The following comment is out of data and the DataLayout is here now.
1202 // ConstantExpr::getCompare cannot do this, because it doesn't have DL
1203 // around to know if bit truncation is happening.
1204 if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
1205 if (Ops1->isNullValue()) {
1206 if (CE0->getOpcode() == Instruction::IntToPtr) {
1207 Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1208 // Convert the integer value to the right size to ensure we get the
1209 // proper extension or truncation.
1210 if (Constant *C = ConstantFoldIntegerCast(CE0->getOperand(0), IntPtrTy,
1211 /*IsSigned*/ false, DL)) {
1212 Constant *Null = Constant::getNullValue(C->getType());
1213 return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1214 }
1215 }
1216
1217 // Only do this transformation if the int is intptrty in size, otherwise
1218 // there is a truncation or extension that we aren't modeling.
1219 if (CE0->getOpcode() == Instruction::PtrToInt) {
1220 Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1221 if (CE0->getType() == IntPtrTy) {
1222 Constant *C = CE0->getOperand(0);
1223 Constant *Null = Constant::getNullValue(C->getType());
1224 return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1225 }
1226 }
1227 }
1228
1229 if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
1230 if (CE0->getOpcode() == CE1->getOpcode()) {
1231 if (CE0->getOpcode() == Instruction::IntToPtr) {
1232 Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1233
1234 // Convert the integer value to the right size to ensure we get the
1235 // proper extension or truncation.
1236 Constant *C0 = ConstantFoldIntegerCast(CE0->getOperand(0), IntPtrTy,
1237 /*IsSigned*/ false, DL);
1238 Constant *C1 = ConstantFoldIntegerCast(CE1->getOperand(0), IntPtrTy,
1239 /*IsSigned*/ false, DL);
1240 if (C0 && C1)
1241 return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
1242 }
1243
1244 // Only do this transformation if the int is intptrty in size, otherwise
1245 // there is a truncation or extension that we aren't modeling.
1246 if (CE0->getOpcode() == Instruction::PtrToInt) {
1247 Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1248 if (CE0->getType() == IntPtrTy &&
1249 CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1251 Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI);
1252 }
1253 }
1254 }
1255 }
1256
1257 // Convert pointer comparison (base+offset1) pred (base+offset2) into
1258 // offset1 pred offset2, for the case where the offset is inbounds. This
1259 // only works for equality and unsigned comparison, as inbounds permits
1260 // crossing the sign boundary. However, the offset comparison itself is
1261 // signed.
1262 if (Ops0->getType()->isPointerTy() && !ICmpInst::isSigned(Predicate)) {
1263 unsigned IndexWidth = DL.getIndexTypeSizeInBits(Ops0->getType());
1264 APInt Offset0(IndexWidth, 0);
1265 Value *Stripped0 =
1267 APInt Offset1(IndexWidth, 0);
1268 Value *Stripped1 =
1270 if (Stripped0 == Stripped1)
1273 ConstantInt::get(CE0->getContext(), Offset0),
1274 ConstantInt::get(CE0->getContext(), Offset1));
1275 }
1276 } else if (isa<ConstantExpr>(Ops1)) {
1277 // If RHS is a constant expression, but the left side isn't, swap the
1278 // operands and try again.
1279 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1280 return ConstantFoldCompareInstOperands(Predicate, Ops1, Ops0, DL, TLI);
1281 }
1282
1283 // Flush any denormal constant float input according to denormal handling
1284 // mode.
1285 Ops0 = FlushFPConstant(Ops0, I, /* IsOutput */ false);
1286 if (!Ops0)
1287 return nullptr;
1288 Ops1 = FlushFPConstant(Ops1, I, /* IsOutput */ false);
1289 if (!Ops1)
1290 return nullptr;
1291
1292 return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
1293}
1294
1296 const DataLayout &DL) {
1298
1299 return ConstantFoldUnaryInstruction(Opcode, Op);
1300}
1301
1303 Constant *RHS,
1304 const DataLayout &DL) {
1306 if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS))
1307 if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL))
1308 return C;
1309
1311 return ConstantExpr::get(Opcode, LHS, RHS);
1312 return ConstantFoldBinaryInstruction(Opcode, LHS, RHS);
1313}
1314
1316 bool IsOutput) {
1317 if (!I || !I->getParent() || !I->getFunction())
1318 return Operand;
1319
1320 ConstantFP *CFP = dyn_cast<ConstantFP>(Operand);
1321 if (!CFP)
1322 return Operand;
1323
1324 const APFloat &APF = CFP->getValueAPF();
1325 // TODO: Should this canonicalize nans?
1326 if (!APF.isDenormal())
1327 return Operand;
1328
1329 Type *Ty = CFP->getType();
1330 DenormalMode DenormMode =
1331 I->getFunction()->getDenormalMode(Ty->getFltSemantics());
1333 IsOutput ? DenormMode.Output : DenormMode.Input;
1334 switch (Mode) {
1335 default:
1336 llvm_unreachable("unknown denormal mode");
1338 return nullptr;
1339 case DenormalMode::IEEE:
1340 return Operand;
1342 if (APF.isDenormal()) {
1343 return ConstantFP::get(
1344 Ty->getContext(),
1346 }
1347 return Operand;
1349 if (APF.isDenormal()) {
1350 return ConstantFP::get(Ty->getContext(),
1351 APFloat::getZero(Ty->getFltSemantics(), false));
1352 }
1353 return Operand;
1354 }
1355 return Operand;
1356}
1357
1359 Constant *RHS, const DataLayout &DL,
1360 const Instruction *I) {
1361 if (Instruction::isBinaryOp(Opcode)) {
1362 // Flush denormal inputs if needed.
1363 Constant *Op0 = FlushFPConstant(LHS, I, /* IsOutput */ false);
1364 if (!Op0)
1365 return nullptr;
1366 Constant *Op1 = FlushFPConstant(RHS, I, /* IsOutput */ false);
1367 if (!Op1)
1368 return nullptr;
1369
1370 // Calculate constant result.
1371 Constant *C = ConstantFoldBinaryOpOperands(Opcode, Op0, Op1, DL);
1372 if (!C)
1373 return nullptr;
1374
1375 // Flush denormal output if needed.
1376 return FlushFPConstant(C, I, /* IsOutput */ true);
1377 }
1378 // If instruction lacks a parent/function and the denormal mode cannot be
1379 // determined, use the default (IEEE).
1380 return ConstantFoldBinaryOpOperands(Opcode, LHS, RHS, DL);
1381}
1382
1384 Type *DestTy, const DataLayout &DL) {
1385 assert(Instruction::isCast(Opcode));
1386 switch (Opcode) {
1387 default:
1388 llvm_unreachable("Missing case");
1389 case Instruction::PtrToInt:
1390 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1391 Constant *FoldedValue = nullptr;
1392 // If the input is a inttoptr, eliminate the pair. This requires knowing
1393 // the width of a pointer, so it can't be done in ConstantExpr::getCast.
1394 if (CE->getOpcode() == Instruction::IntToPtr) {
1395 // zext/trunc the inttoptr to pointer size.
1396 FoldedValue = ConstantFoldIntegerCast(CE->getOperand(0),
1397 DL.getIntPtrType(CE->getType()),
1398 /*IsSigned=*/false, DL);
1399 } else if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
1400 // If we have GEP, we can perform the following folds:
1401 // (ptrtoint (gep null, x)) -> x
1402 // (ptrtoint (gep (gep null, x), y) -> x + y, etc.
1403 unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
1404 APInt BaseOffset(BitWidth, 0);
1405 auto *Base = cast<Constant>(GEP->stripAndAccumulateConstantOffsets(
1406 DL, BaseOffset, /*AllowNonInbounds=*/true));
1407 if (Base->isNullValue()) {
1408 FoldedValue = ConstantInt::get(CE->getContext(), BaseOffset);
1409 } else {
1410 // ptrtoint (gep i8, Ptr, (sub 0, V)) -> sub (ptrtoint Ptr), V
1411 if (GEP->getNumIndices() == 1 &&
1412 GEP->getSourceElementType()->isIntegerTy(8)) {
1413 auto *Ptr = cast<Constant>(GEP->getPointerOperand());
1414 auto *Sub = dyn_cast<ConstantExpr>(GEP->getOperand(1));
1415 Type *IntIdxTy = DL.getIndexType(Ptr->getType());
1416 if (Sub && Sub->getType() == IntIdxTy &&
1417 Sub->getOpcode() == Instruction::Sub &&
1418 Sub->getOperand(0)->isNullValue())
1419 FoldedValue = ConstantExpr::getSub(
1420 ConstantExpr::getPtrToInt(Ptr, IntIdxTy), Sub->getOperand(1));
1421 }
1422 }
1423 }
1424 if (FoldedValue) {
1425 // Do a zext or trunc to get to the ptrtoint dest size.
1426 return ConstantFoldIntegerCast(FoldedValue, DestTy, /*IsSigned=*/false,
1427 DL);
1428 }
1429 }
1430 break;
1431 case Instruction::IntToPtr:
1432 // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
1433 // the int size is >= the ptr size and the address spaces are the same.
1434 // This requires knowing the width of a pointer, so it can't be done in
1435 // ConstantExpr::getCast.
1436 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1437 if (CE->getOpcode() == Instruction::PtrToInt) {
1438 Constant *SrcPtr = CE->getOperand(0);
1439 unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType());
1440 unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1441
1442 if (MidIntSize >= SrcPtrSize) {
1443 unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
1444 if (SrcAS == DestTy->getPointerAddressSpace())
1445 return FoldBitCast(CE->getOperand(0), DestTy, DL);
1446 }
1447 }
1448 }
1449 break;
1450 case Instruction::Trunc:
1451 case Instruction::ZExt:
1452 case Instruction::SExt:
1453 case Instruction::FPTrunc:
1454 case Instruction::FPExt:
1455 case Instruction::UIToFP:
1456 case Instruction::SIToFP:
1457 case Instruction::FPToUI:
1458 case Instruction::FPToSI:
1459 case Instruction::AddrSpaceCast:
1460 break;
1461 case Instruction::BitCast:
1462 return FoldBitCast(C, DestTy, DL);
1463 }
1464
1466 return ConstantExpr::getCast(Opcode, C, DestTy);
1467 return ConstantFoldCastInstruction(Opcode, C, DestTy);
1468}
1469
1471 bool IsSigned, const DataLayout &DL) {
1472 Type *SrcTy = C->getType();
1473 if (SrcTy == DestTy)
1474 return C;
1475 if (SrcTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1476 return ConstantFoldCastOperand(Instruction::Trunc, C, DestTy, DL);
1477 if (IsSigned)
1478 return ConstantFoldCastOperand(Instruction::SExt, C, DestTy, DL);
1479 return ConstantFoldCastOperand(Instruction::ZExt, C, DestTy, DL);
1480}
1481
1482//===----------------------------------------------------------------------===//
1483// Constant Folding for Calls
1484//
1485
1487 if (Call->isNoBuiltin())
1488 return false;
1489 if (Call->getFunctionType() != F->getFunctionType())
1490 return false;
1491 switch (F->getIntrinsicID()) {
1492 // Operations that do not operate floating-point numbers and do not depend on
1493 // FP environment can be folded even in strictfp functions.
1494 case Intrinsic::bswap:
1495 case Intrinsic::ctpop:
1496 case Intrinsic::ctlz:
1497 case Intrinsic::cttz:
1498 case Intrinsic::fshl:
1499 case Intrinsic::fshr:
1500 case Intrinsic::launder_invariant_group:
1501 case Intrinsic::strip_invariant_group:
1502 case Intrinsic::masked_load:
1503 case Intrinsic::get_active_lane_mask:
1504 case Intrinsic::abs:
1505 case Intrinsic::smax:
1506 case Intrinsic::smin:
1507 case Intrinsic::umax:
1508 case Intrinsic::umin:
1509 case Intrinsic::sadd_with_overflow:
1510 case Intrinsic::uadd_with_overflow:
1511 case Intrinsic::ssub_with_overflow:
1512 case Intrinsic::usub_with_overflow:
1513 case Intrinsic::smul_with_overflow:
1514 case Intrinsic::umul_with_overflow:
1515 case Intrinsic::sadd_sat:
1516 case Intrinsic::uadd_sat:
1517 case Intrinsic::ssub_sat:
1518 case Intrinsic::usub_sat:
1519 case Intrinsic::smul_fix:
1520 case Intrinsic::smul_fix_sat:
1521 case Intrinsic::bitreverse:
1522 case Intrinsic::is_constant:
1523 case Intrinsic::vector_reduce_add:
1524 case Intrinsic::vector_reduce_mul:
1525 case Intrinsic::vector_reduce_and:
1526 case Intrinsic::vector_reduce_or:
1527 case Intrinsic::vector_reduce_xor:
1528 case Intrinsic::vector_reduce_smin:
1529 case Intrinsic::vector_reduce_smax:
1530 case Intrinsic::vector_reduce_umin:
1531 case Intrinsic::vector_reduce_umax:
1532 // Target intrinsics
1533 case Intrinsic::amdgcn_perm:
1534 case Intrinsic::amdgcn_wave_reduce_umin:
1535 case Intrinsic::amdgcn_wave_reduce_umax:
1536 case Intrinsic::amdgcn_s_wqm:
1537 case Intrinsic::amdgcn_s_quadmask:
1538 case Intrinsic::amdgcn_s_bitreplicate:
1539 case Intrinsic::arm_mve_vctp8:
1540 case Intrinsic::arm_mve_vctp16:
1541 case Intrinsic::arm_mve_vctp32:
1542 case Intrinsic::arm_mve_vctp64:
1543 case Intrinsic::aarch64_sve_convert_from_svbool:
1544 // WebAssembly float semantics are always known
1545 case Intrinsic::wasm_trunc_signed:
1546 case Intrinsic::wasm_trunc_unsigned:
1547 return true;
1548
1549 // Floating point operations cannot be folded in strictfp functions in
1550 // general case. They can be folded if FP environment is known to compiler.
1551 case Intrinsic::minnum:
1552 case Intrinsic::maxnum:
1553 case Intrinsic::minimum:
1554 case Intrinsic::maximum:
1555 case Intrinsic::log:
1556 case Intrinsic::log2:
1557 case Intrinsic::log10:
1558 case Intrinsic::exp:
1559 case Intrinsic::exp2:
1560 case Intrinsic::exp10:
1561 case Intrinsic::sqrt:
1562 case Intrinsic::sin:
1563 case Intrinsic::cos:
1564 case Intrinsic::pow:
1565 case Intrinsic::powi:
1566 case Intrinsic::ldexp:
1567 case Intrinsic::fma:
1568 case Intrinsic::fmuladd:
1569 case Intrinsic::frexp:
1570 case Intrinsic::fptoui_sat:
1571 case Intrinsic::fptosi_sat:
1572 case Intrinsic::convert_from_fp16:
1573 case Intrinsic::convert_to_fp16:
1574 case Intrinsic::amdgcn_cos:
1575 case Intrinsic::amdgcn_cubeid:
1576 case Intrinsic::amdgcn_cubema:
1577 case Intrinsic::amdgcn_cubesc:
1578 case Intrinsic::amdgcn_cubetc:
1579 case Intrinsic::amdgcn_fmul_legacy:
1580 case Intrinsic::amdgcn_fma_legacy:
1581 case Intrinsic::amdgcn_fract:
1582 case Intrinsic::amdgcn_sin:
1583 // The intrinsics below depend on rounding mode in MXCSR.
1584 case Intrinsic::x86_sse_cvtss2si:
1585 case Intrinsic::x86_sse_cvtss2si64:
1586 case Intrinsic::x86_sse_cvttss2si:
1587 case Intrinsic::x86_sse_cvttss2si64:
1588 case Intrinsic::x86_sse2_cvtsd2si:
1589 case Intrinsic::x86_sse2_cvtsd2si64:
1590 case Intrinsic::x86_sse2_cvttsd2si:
1591 case Intrinsic::x86_sse2_cvttsd2si64:
1592 case Intrinsic::x86_avx512_vcvtss2si32:
1593 case Intrinsic::x86_avx512_vcvtss2si64:
1594 case Intrinsic::x86_avx512_cvttss2si:
1595 case Intrinsic::x86_avx512_cvttss2si64:
1596 case Intrinsic::x86_avx512_vcvtsd2si32:
1597 case Intrinsic::x86_avx512_vcvtsd2si64:
1598 case Intrinsic::x86_avx512_cvttsd2si:
1599 case Intrinsic::x86_avx512_cvttsd2si64:
1600 case Intrinsic::x86_avx512_vcvtss2usi32:
1601 case Intrinsic::x86_avx512_vcvtss2usi64:
1602 case Intrinsic::x86_avx512_cvttss2usi:
1603 case Intrinsic::x86_avx512_cvttss2usi64:
1604 case Intrinsic::x86_avx512_vcvtsd2usi32:
1605 case Intrinsic::x86_avx512_vcvtsd2usi64:
1606 case Intrinsic::x86_avx512_cvttsd2usi:
1607 case Intrinsic::x86_avx512_cvttsd2usi64:
1608 return !Call->isStrictFP();
1609
1610 // Sign operations are actually bitwise operations, they do not raise
1611 // exceptions even for SNANs.
1612 case Intrinsic::fabs:
1613 case Intrinsic::copysign:
1614 case Intrinsic::is_fpclass:
1615 // Non-constrained variants of rounding operations means default FP
1616 // environment, they can be folded in any case.
1617 case Intrinsic::ceil:
1618 case Intrinsic::floor:
1619 case Intrinsic::round:
1620 case Intrinsic::roundeven:
1621 case Intrinsic::trunc:
1622 case Intrinsic::nearbyint:
1623 case Intrinsic::rint:
1624 case Intrinsic::canonicalize:
1625 // Constrained intrinsics can be folded if FP environment is known
1626 // to compiler.
1627 case Intrinsic::experimental_constrained_fma:
1628 case Intrinsic::experimental_constrained_fmuladd:
1629 case Intrinsic::experimental_constrained_fadd:
1630 case Intrinsic::experimental_constrained_fsub:
1631 case Intrinsic::experimental_constrained_fmul:
1632 case Intrinsic::experimental_constrained_fdiv:
1633 case Intrinsic::experimental_constrained_frem:
1634 case Intrinsic::experimental_constrained_ceil:
1635 case Intrinsic::experimental_constrained_floor:
1636 case Intrinsic::experimental_constrained_round:
1637 case Intrinsic::experimental_constrained_roundeven:
1638 case Intrinsic::experimental_constrained_trunc:
1639 case Intrinsic::experimental_constrained_nearbyint:
1640 case Intrinsic::experimental_constrained_rint:
1641 case Intrinsic::experimental_constrained_fcmp:
1642 case Intrinsic::experimental_constrained_fcmps:
1643 return true;
1644 default:
1645 return false;
1646 case Intrinsic::not_intrinsic: break;
1647 }
1648
1649 if (!F->hasName() || Call->isStrictFP())
1650 return false;
1651
1652 // In these cases, the check of the length is required. We don't want to
1653 // return true for a name like "cos\0blah" which strcmp would return equal to
1654 // "cos", but has length 8.
1655 StringRef Name = F->getName();
1656 switch (Name[0]) {
1657 default:
1658 return false;
1659 case 'a':
1660 return Name == "acos" || Name == "acosf" ||
1661 Name == "asin" || Name == "asinf" ||
1662 Name == "atan" || Name == "atanf" ||
1663 Name == "atan2" || Name == "atan2f";
1664 case 'c':
1665 return Name == "ceil" || Name == "ceilf" ||
1666 Name == "cos" || Name == "cosf" ||
1667 Name == "cosh" || Name == "coshf";
1668 case 'e':
1669 return Name == "exp" || Name == "expf" ||
1670 Name == "exp2" || Name == "exp2f";
1671 case 'f':
1672 return Name == "fabs" || Name == "fabsf" ||
1673 Name == "floor" || Name == "floorf" ||
1674 Name == "fmod" || Name == "fmodf";
1675 case 'l':
1676 return Name == "log" || Name == "logf" ||
1677 Name == "log2" || Name == "log2f" ||
1678 Name == "log10" || Name == "log10f";
1679 case 'n':
1680 return Name == "nearbyint" || Name == "nearbyintf";
1681 case 'p':
1682 return Name == "pow" || Name == "powf";
1683 case 'r':
1684 return Name == "remainder" || Name == "remainderf" ||
1685 Name == "rint" || Name == "rintf" ||
1686 Name == "round" || Name == "roundf";
1687 case 's':
1688 return Name == "sin" || Name == "sinf" ||
1689 Name == "sinh" || Name == "sinhf" ||
1690 Name == "sqrt" || Name == "sqrtf";
1691 case 't':
1692 return Name == "tan" || Name == "tanf" ||
1693 Name == "tanh" || Name == "tanhf" ||
1694 Name == "trunc" || Name == "truncf";
1695 case '_':
1696 // Check for various function names that get used for the math functions
1697 // when the header files are preprocessed with the macro
1698 // __FINITE_MATH_ONLY__ enabled.
1699 // The '12' here is the length of the shortest name that can match.
1700 // We need to check the size before looking at Name[1] and Name[2]
1701 // so we may as well check a limit that will eliminate mismatches.
1702 if (Name.size() < 12 || Name[1] != '_')
1703 return false;
1704 switch (Name[2]) {
1705 default:
1706 return false;
1707 case 'a':
1708 return Name == "__acos_finite" || Name == "__acosf_finite" ||
1709 Name == "__asin_finite" || Name == "__asinf_finite" ||
1710 Name == "__atan2_finite" || Name == "__atan2f_finite";
1711 case 'c':
1712 return Name == "__cosh_finite" || Name == "__coshf_finite";
1713 case 'e':
1714 return Name == "__exp_finite" || Name == "__expf_finite" ||
1715 Name == "__exp2_finite" || Name == "__exp2f_finite";
1716 case 'l':
1717 return Name == "__log_finite" || Name == "__logf_finite" ||
1718 Name == "__log10_finite" || Name == "__log10f_finite";
1719 case 'p':
1720 return Name == "__pow_finite" || Name == "__powf_finite";
1721 case 's':
1722 return Name == "__sinh_finite" || Name == "__sinhf_finite";
1723 }
1724 }
1725}
1726
1727namespace {
1728
1729Constant *GetConstantFoldFPValue(double V, Type *Ty) {
1730 if (Ty->isHalfTy() || Ty->isFloatTy()) {
1731 APFloat APF(V);
1732 bool unused;
1733 APF.convert(Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &unused);
1734 return ConstantFP::get(Ty->getContext(), APF);
1735 }
1736 if (Ty->isDoubleTy())
1737 return ConstantFP::get(Ty->getContext(), APFloat(V));
1738 llvm_unreachable("Can only constant fold half/float/double");
1739}
1740
1741/// Clear the floating-point exception state.
1742inline void llvm_fenv_clearexcept() {
1743#if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT
1744 feclearexcept(FE_ALL_EXCEPT);
1745#endif
1746 errno = 0;
1747}
1748
1749/// Test if a floating-point exception was raised.
1750inline bool llvm_fenv_testexcept() {
1751 int errno_val = errno;
1752 if (errno_val == ERANGE || errno_val == EDOM)
1753 return true;
1754#if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT
1755 if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
1756 return true;
1757#endif
1758 return false;
1759}
1760
1761Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
1762 Type *Ty) {
1763 llvm_fenv_clearexcept();
1764 double Result = NativeFP(V.convertToDouble());
1765 if (llvm_fenv_testexcept()) {
1766 llvm_fenv_clearexcept();
1767 return nullptr;
1768 }
1769
1770 return GetConstantFoldFPValue(Result, Ty);
1771}
1772
1773Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
1774 const APFloat &V, const APFloat &W, Type *Ty) {
1775 llvm_fenv_clearexcept();
1776 double Result = NativeFP(V.convertToDouble(), W.convertToDouble());
1777 if (llvm_fenv_testexcept()) {
1778 llvm_fenv_clearexcept();
1779 return nullptr;
1780 }
1781
1782 return GetConstantFoldFPValue(Result, Ty);
1783}
1784
1785Constant *constantFoldVectorReduce(Intrinsic::ID IID, Constant *Op) {
1786 FixedVectorType *VT = dyn_cast<FixedVectorType>(Op->getType());
1787 if (!VT)
1788 return nullptr;
1789
1790 // This isn't strictly necessary, but handle the special/common case of zero:
1791 // all integer reductions of a zero input produce zero.
1792 if (isa<ConstantAggregateZero>(Op))
1793 return ConstantInt::get(VT->getElementType(), 0);
1794
1795 // This is the same as the underlying binops - poison propagates.
1796 if (isa<PoisonValue>(Op) || Op->containsPoisonElement())
1797 return PoisonValue::get(VT->getElementType());
1798
1799 // TODO: Handle undef.
1800 if (!isa<ConstantVector>(Op) && !isa<ConstantDataVector>(Op))
1801 return nullptr;
1802
1803 auto *EltC = dyn_cast<ConstantInt>(Op->getAggregateElement(0U));
1804 if (!EltC)
1805 return nullptr;
1806
1807 APInt Acc = EltC->getValue();
1808 for (unsigned I = 1, E = VT->getNumElements(); I != E; I++) {
1809 if (!(EltC = dyn_cast<ConstantInt>(Op->getAggregateElement(I))))
1810 return nullptr;
1811 const APInt &X = EltC->getValue();
1812 switch (IID) {
1813 case Intrinsic::vector_reduce_add:
1814 Acc = Acc + X;
1815 break;
1816 case Intrinsic::vector_reduce_mul:
1817 Acc = Acc * X;
1818 break;
1819 case Intrinsic::vector_reduce_and:
1820 Acc = Acc & X;
1821 break;
1822 case Intrinsic::vector_reduce_or:
1823 Acc = Acc | X;
1824 break;
1825 case Intrinsic::vector_reduce_xor:
1826 Acc = Acc ^ X;
1827 break;
1828 case Intrinsic::vector_reduce_smin:
1829 Acc = APIntOps::smin(Acc, X);
1830 break;
1831 case Intrinsic::vector_reduce_smax:
1832 Acc = APIntOps::smax(Acc, X);
1833 break;
1834 case Intrinsic::vector_reduce_umin:
1835 Acc = APIntOps::umin(Acc, X);
1836 break;
1837 case Intrinsic::vector_reduce_umax:
1838 Acc = APIntOps::umax(Acc, X);
1839 break;
1840 }
1841 }
1842
1843 return ConstantInt::get(Op->getContext(), Acc);
1844}
1845
1846/// Attempt to fold an SSE floating point to integer conversion of a constant
1847/// floating point. If roundTowardZero is false, the default IEEE rounding is
1848/// used (toward nearest, ties to even). This matches the behavior of the
1849/// non-truncating SSE instructions in the default rounding mode. The desired
1850/// integer type Ty is used to select how many bits are available for the
1851/// result. Returns null if the conversion cannot be performed, otherwise
1852/// returns the Constant value resulting from the conversion.
1853Constant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero,
1854 Type *Ty, bool IsSigned) {
1855 // All of these conversion intrinsics form an integer of at most 64bits.
1856 unsigned ResultWidth = Ty->getIntegerBitWidth();
1857 assert(ResultWidth <= 64 &&
1858 "Can only constant fold conversions to 64 and 32 bit ints");
1859
1860 uint64_t UIntVal;
1861 bool isExact = false;
1862 APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
1863 : APFloat::rmNearestTiesToEven;
1865 Val.convertToInteger(MutableArrayRef(UIntVal), ResultWidth,
1866 IsSigned, mode, &isExact);
1867 if (status != APFloat::opOK &&
1868 (!roundTowardZero || status != APFloat::opInexact))
1869 return nullptr;
1870 return ConstantInt::get(Ty, UIntVal, IsSigned);
1871}
1872
1873double getValueAsDouble(ConstantFP *Op) {
1874 Type *Ty = Op->getType();
1875
1876 if (Ty->isBFloatTy() || Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())
1877 return Op->getValueAPF().convertToDouble();
1878
1879 bool unused;
1880 APFloat APF = Op->getValueAPF();
1881 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &unused);
1882 return APF.convertToDouble();
1883}
1884
1885static bool getConstIntOrUndef(Value *Op, const APInt *&C) {
1886 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
1887 C = &CI->getValue();
1888 return true;
1889 }
1890 if (isa<UndefValue>(Op)) {
1891 C = nullptr;
1892 return true;
1893 }
1894 return false;
1895}
1896
1897/// Checks if the given intrinsic call, which evaluates to constant, is allowed
1898/// to be folded.
1899///
1900/// \param CI Constrained intrinsic call.
1901/// \param St Exception flags raised during constant evaluation.
1902static bool mayFoldConstrained(ConstrainedFPIntrinsic *CI,
1903 APFloat::opStatus St) {
1904 std::optional<RoundingMode> ORM = CI->getRoundingMode();
1905 std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
1906
1907 // If the operation does not change exception status flags, it is safe
1908 // to fold.
1909 if (St == APFloat::opStatus::opOK)
1910 return true;
1911
1912 // If evaluation raised FP exception, the result can depend on rounding
1913 // mode. If the latter is unknown, folding is not possible.
1914 if (ORM && *ORM == RoundingMode::Dynamic)
1915 return false;
1916
1917 // If FP exceptions are ignored, fold the call, even if such exception is
1918 // raised.
1919 if (EB && *EB != fp::ExceptionBehavior::ebStrict)
1920 return true;
1921
1922 // Leave the calculation for runtime so that exception flags be correctly set
1923 // in hardware.
1924 return false;
1925}
1926
1927/// Returns the rounding mode that should be used for constant evaluation.
1928static RoundingMode
1929getEvaluationRoundingMode(const ConstrainedFPIntrinsic *CI) {
1930 std::optional<RoundingMode> ORM = CI->getRoundingMode();
1931 if (!ORM || *ORM == RoundingMode::Dynamic)
1932 // Even if the rounding mode is unknown, try evaluating the operation.
1933 // If it does not raise inexact exception, rounding was not applied,
1934 // so the result is exact and does not depend on rounding mode. Whether
1935 // other FP exceptions are raised, it does not depend on rounding mode.
1936 return RoundingMode::NearestTiesToEven;
1937 return *ORM;
1938}
1939
1940/// Try to constant fold llvm.canonicalize for the given caller and value.
1941static Constant *constantFoldCanonicalize(const Type *Ty, const CallBase *CI,
1942 const APFloat &Src) {
1943 // Zero, positive and negative, is always OK to fold.
1944 if (Src.isZero()) {
1945 // Get a fresh 0, since ppc_fp128 does have non-canonical zeros.
1946 return ConstantFP::get(
1947 CI->getContext(),
1948 APFloat::getZero(Src.getSemantics(), Src.isNegative()));
1949 }
1950
1951 if (!Ty->isIEEELikeFPTy())
1952 return nullptr;
1953
1954 // Zero is always canonical and the sign must be preserved.
1955 //
1956 // Denorms and nans may have special encodings, but it should be OK to fold a
1957 // totally average number.
1958 if (Src.isNormal() || Src.isInfinity())
1959 return ConstantFP::get(CI->getContext(), Src);
1960
1961 if (Src.isDenormal() && CI->getParent() && CI->getFunction()) {
1962 DenormalMode DenormMode =
1963 CI->getFunction()->getDenormalMode(Src.getSemantics());
1964
1965 if (DenormMode == DenormalMode::getIEEE())
1966 return ConstantFP::get(CI->getContext(), Src);
1967
1968 if (DenormMode.Input == DenormalMode::Dynamic)
1969 return nullptr;
1970
1971 // If we know if either input or output is flushed, we can fold.
1972 if ((DenormMode.Input == DenormalMode::Dynamic &&
1973 DenormMode.Output == DenormalMode::IEEE) ||
1974 (DenormMode.Input == DenormalMode::IEEE &&
1975 DenormMode.Output == DenormalMode::Dynamic))
1976 return nullptr;
1977
1978 bool IsPositive =
1979 (!Src.isNegative() || DenormMode.Input == DenormalMode::PositiveZero ||
1980 (DenormMode.Output == DenormalMode::PositiveZero &&
1981 DenormMode.Input == DenormalMode::IEEE));
1982
1983 return ConstantFP::get(CI->getContext(),
1984 APFloat::getZero(Src.getSemantics(), !IsPositive));
1985 }
1986
1987 return nullptr;
1988}
1989
1990static Constant *ConstantFoldScalarCall1(StringRef Name,
1991 Intrinsic::ID IntrinsicID,
1992 Type *Ty,
1994 const TargetLibraryInfo *TLI,
1995 const CallBase *Call) {
1996 assert(Operands.size() == 1 && "Wrong number of operands.");
1997
1998 if (IntrinsicID == Intrinsic::is_constant) {
1999 // We know we have a "Constant" argument. But we want to only
2000 // return true for manifest constants, not those that depend on
2001 // constants with unknowable values, e.g. GlobalValue or BlockAddress.
2002 if (Operands[0]->isManifestConstant())
2003 return ConstantInt::getTrue(Ty->getContext());
2004 return nullptr;
2005 }
2006
2007 if (isa<PoisonValue>(Operands[0])) {
2008 // TODO: All of these operations should probably propagate poison.
2009 if (IntrinsicID == Intrinsic::canonicalize)
2010 return PoisonValue::get(Ty);
2011 }
2012
2013 if (isa<UndefValue>(Operands[0])) {
2014 // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN.
2015 // ctpop() is between 0 and bitwidth, pick 0 for undef.
2016 // fptoui.sat and fptosi.sat can always fold to zero (for a zero input).
2017 if (IntrinsicID == Intrinsic::cos ||
2018 IntrinsicID == Intrinsic::ctpop ||
2019 IntrinsicID == Intrinsic::fptoui_sat ||
2020 IntrinsicID == Intrinsic::fptosi_sat ||
2021 IntrinsicID == Intrinsic::canonicalize)
2022 return Constant::getNullValue(Ty);
2023 if (IntrinsicID == Intrinsic::bswap ||
2024 IntrinsicID == Intrinsic::bitreverse ||
2025 IntrinsicID == Intrinsic::launder_invariant_group ||
2026 IntrinsicID == Intrinsic::strip_invariant_group)
2027 return Operands[0];
2028 }
2029
2030 if (isa<ConstantPointerNull>(Operands[0])) {
2031 // launder(null) == null == strip(null) iff in addrspace 0
2032 if (IntrinsicID == Intrinsic::launder_invariant_group ||
2033 IntrinsicID == Intrinsic::strip_invariant_group) {
2034 // If instruction is not yet put in a basic block (e.g. when cloning
2035 // a function during inlining), Call's caller may not be available.
2036 // So check Call's BB first before querying Call->getCaller.
2037 const Function *Caller =
2038 Call->getParent() ? Call->getCaller() : nullptr;
2039 if (Caller &&
2041 Caller, Operands[0]->getType()->getPointerAddressSpace())) {
2042 return Operands[0];
2043 }
2044 return nullptr;
2045 }
2046 }
2047
2048 if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) {
2049 if (IntrinsicID == Intrinsic::convert_to_fp16) {
2050 APFloat Val(Op->getValueAPF());
2051
2052 bool lost = false;
2053 Val.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &lost);
2054
2055 return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt());
2056 }
2057
2058 APFloat U = Op->getValueAPF();
2059
2060 if (IntrinsicID == Intrinsic::wasm_trunc_signed ||
2061 IntrinsicID == Intrinsic::wasm_trunc_unsigned) {
2062 bool Signed = IntrinsicID == Intrinsic::wasm_trunc_signed;
2063
2064 if (U.isNaN())
2065 return nullptr;
2066
2067 unsigned Width = Ty->getIntegerBitWidth();
2068 APSInt Int(Width, !Signed);
2069 bool IsExact = false;
2071 U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact);
2072
2073 if (Status == APFloat::opOK || Status == APFloat::opInexact)
2074 return ConstantInt::get(Ty, Int);
2075
2076 return nullptr;
2077 }
2078
2079 if (IntrinsicID == Intrinsic::fptoui_sat ||
2080 IntrinsicID == Intrinsic::fptosi_sat) {
2081 // convertToInteger() already has the desired saturation semantics.
2083 IntrinsicID == Intrinsic::fptoui_sat);
2084 bool IsExact;
2085 U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact);
2086 return ConstantInt::get(Ty, Int);
2087 }
2088
2089 if (IntrinsicID == Intrinsic::canonicalize)
2090 return constantFoldCanonicalize(Ty, Call, U);
2091
2092 if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
2093 return nullptr;
2094
2095 // Use internal versions of these intrinsics.
2096
2097 if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint) {
2098 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2099 return ConstantFP::get(Ty->getContext(), U);
2100 }
2101
2102 if (IntrinsicID == Intrinsic::round) {
2103 U.roundToIntegral(APFloat::rmNearestTiesToAway);
2104 return ConstantFP::get(Ty->getContext(), U);
2105 }
2106
2107 if (IntrinsicID == Intrinsic::roundeven) {
2108 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2109 return ConstantFP::get(Ty->getContext(), U);
2110 }
2111
2112 if (IntrinsicID == Intrinsic::ceil) {
2113 U.roundToIntegral(APFloat::rmTowardPositive);
2114 return ConstantFP::get(Ty->getContext(), U);
2115 }
2116
2117 if (IntrinsicID == Intrinsic::floor) {
2118 U.roundToIntegral(APFloat::rmTowardNegative);
2119 return ConstantFP::get(Ty->getContext(), U);
2120 }
2121
2122 if (IntrinsicID == Intrinsic::trunc) {
2123 U.roundToIntegral(APFloat::rmTowardZero);
2124 return ConstantFP::get(Ty->getContext(), U);
2125 }
2126
2127 if (IntrinsicID == Intrinsic::fabs) {
2128 U.clearSign();
2129 return ConstantFP::get(Ty->getContext(), U);
2130 }
2131
2132 if (IntrinsicID == Intrinsic::amdgcn_fract) {
2133 // The v_fract instruction behaves like the OpenCL spec, which defines
2134 // fract(x) as fmin(x - floor(x), 0x1.fffffep-1f): "The min() operator is
2135 // there to prevent fract(-small) from returning 1.0. It returns the
2136 // largest positive floating-point number less than 1.0."
2137 APFloat FloorU(U);
2138 FloorU.roundToIntegral(APFloat::rmTowardNegative);
2139 APFloat FractU(U - FloorU);
2140 APFloat AlmostOne(U.getSemantics(), 1);
2141 AlmostOne.next(/*nextDown*/ true);
2142 return ConstantFP::get(Ty->getContext(), minimum(FractU, AlmostOne));
2143 }
2144
2145 // Rounding operations (floor, trunc, ceil, round and nearbyint) do not
2146 // raise FP exceptions, unless the argument is signaling NaN.
2147
2148 std::optional<APFloat::roundingMode> RM;
2149 switch (IntrinsicID) {
2150 default:
2151 break;
2152 case Intrinsic::experimental_constrained_nearbyint:
2153 case Intrinsic::experimental_constrained_rint: {
2154 auto CI = cast<ConstrainedFPIntrinsic>(Call);
2155 RM = CI->getRoundingMode();
2156 if (!RM || *RM == RoundingMode::Dynamic)
2157 return nullptr;
2158 break;
2159 }
2160 case Intrinsic::experimental_constrained_round:
2161 RM = APFloat::rmNearestTiesToAway;
2162 break;
2163 case Intrinsic::experimental_constrained_ceil:
2164 RM = APFloat::rmTowardPositive;
2165 break;
2166 case Intrinsic::experimental_constrained_floor:
2167 RM = APFloat::rmTowardNegative;
2168 break;
2169 case Intrinsic::experimental_constrained_trunc:
2170 RM = APFloat::rmTowardZero;
2171 break;
2172 }
2173 if (RM) {
2174 auto CI = cast<ConstrainedFPIntrinsic>(Call);
2175 if (U.isFinite()) {
2176 APFloat::opStatus St = U.roundToIntegral(*RM);
2177 if (IntrinsicID == Intrinsic::experimental_constrained_rint &&
2178 St == APFloat::opInexact) {
2179 std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
2180 if (EB && *EB == fp::ebStrict)
2181 return nullptr;
2182 }
2183 } else if (U.isSignaling()) {
2184 std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
2185 if (EB && *EB != fp::ebIgnore)
2186 return nullptr;
2187 U = APFloat::getQNaN(U.getSemantics());
2188 }
2189 return ConstantFP::get(Ty->getContext(), U);
2190 }
2191
2192 /// We only fold functions with finite arguments. Folding NaN and inf is
2193 /// likely to be aborted with an exception anyway, and some host libms
2194 /// have known errors raising exceptions.
2195 if (!U.isFinite())
2196 return nullptr;
2197
2198 /// Currently APFloat versions of these functions do not exist, so we use
2199 /// the host native double versions. Float versions are not called
2200 /// directly but for all these it is true (float)(f((double)arg)) ==
2201 /// f(arg). Long double not supported yet.
2202 const APFloat &APF = Op->getValueAPF();
2203
2204 switch (IntrinsicID) {
2205 default: break;
2206 case Intrinsic::log:
2207 return ConstantFoldFP(log, APF, Ty);
2208 case Intrinsic::log2:
2209 // TODO: What about hosts that lack a C99 library?
2210 return ConstantFoldFP(log2, APF, Ty);
2211 case Intrinsic::log10:
2212 // TODO: What about hosts that lack a C99 library?
2213 return ConstantFoldFP(log10, APF, Ty);
2214 case Intrinsic::exp:
2215 return ConstantFoldFP(exp, APF, Ty);
2216 case Intrinsic::exp2:
2217 // Fold exp2(x) as pow(2, x), in case the host lacks a C99 library.
2218 return ConstantFoldBinaryFP(pow, APFloat(2.0), APF, Ty);
2219 case Intrinsic::exp10:
2220 // Fold exp10(x) as pow(10, x), in case the host lacks a C99 library.
2221 return ConstantFoldBinaryFP(pow, APFloat(10.0), APF, Ty);
2222 case Intrinsic::sin:
2223 return ConstantFoldFP(sin, APF, Ty);
2224 case Intrinsic::cos:
2225 return ConstantFoldFP(cos, APF, Ty);
2226 case Intrinsic::sqrt:
2227 return ConstantFoldFP(sqrt, APF, Ty);
2228 case Intrinsic::amdgcn_cos:
2229 case Intrinsic::amdgcn_sin: {
2230 double V = getValueAsDouble(Op);
2231 if (V < -256.0 || V > 256.0)
2232 // The gfx8 and gfx9 architectures handle arguments outside the range
2233 // [-256, 256] differently. This should be a rare case so bail out
2234 // rather than trying to handle the difference.
2235 return nullptr;
2236 bool IsCos = IntrinsicID == Intrinsic::amdgcn_cos;
2237 double V4 = V * 4.0;
2238 if (V4 == floor(V4)) {
2239 // Force exact results for quarter-integer inputs.
2240 const double SinVals[4] = { 0.0, 1.0, 0.0, -1.0 };
2241 V = SinVals[((int)V4 + (IsCos ? 1 : 0)) & 3];
2242 } else {
2243 if (IsCos)
2244 V = cos(V * 2.0 * numbers::pi);
2245 else
2246 V = sin(V * 2.0 * numbers::pi);
2247 }
2248 return GetConstantFoldFPValue(V, Ty);
2249 }
2250 }
2251
2252 if (!TLI)
2253 return nullptr;
2254
2256 if (!TLI->getLibFunc(Name, Func))
2257 return nullptr;
2258
2259 switch (Func) {
2260 default:
2261 break;
2262 case LibFunc_acos:
2263 case LibFunc_acosf:
2264 case LibFunc_acos_finite:
2265 case LibFunc_acosf_finite:
2266 if (TLI->has(Func))
2267 return ConstantFoldFP(acos, APF, Ty);
2268 break;
2269 case LibFunc_asin:
2270 case LibFunc_asinf:
2271 case LibFunc_asin_finite:
2272 case LibFunc_asinf_finite:
2273 if (TLI->has(Func))
2274 return ConstantFoldFP(asin, APF, Ty);
2275 break;
2276 case LibFunc_atan:
2277 case LibFunc_atanf:
2278 if (TLI->has(Func))
2279 return ConstantFoldFP(atan, APF, Ty);
2280 break;
2281 case LibFunc_ceil:
2282 case LibFunc_ceilf:
2283 if (TLI->has(Func)) {
2284 U.roundToIntegral(APFloat::rmTowardPositive);
2285 return ConstantFP::get(Ty->getContext(), U);
2286 }
2287 break;
2288 case LibFunc_cos:
2289 case LibFunc_cosf:
2290 if (TLI->has(Func))
2291 return ConstantFoldFP(cos, APF, Ty);
2292 break;
2293 case LibFunc_cosh:
2294 case LibFunc_coshf:
2295 case LibFunc_cosh_finite:
2296 case LibFunc_coshf_finite:
2297 if (TLI->has(Func))
2298 return ConstantFoldFP(cosh, APF, Ty);
2299 break;
2300 case LibFunc_exp:
2301 case LibFunc_expf:
2302 case LibFunc_exp_finite:
2303 case LibFunc_expf_finite:
2304 if (TLI->has(Func))
2305 return ConstantFoldFP(exp, APF, Ty);
2306 break;
2307 case LibFunc_exp2:
2308 case LibFunc_exp2f:
2309 case LibFunc_exp2_finite:
2310 case LibFunc_exp2f_finite:
2311 if (TLI->has(Func))
2312 // Fold exp2(x) as pow(2, x), in case the host lacks a C99 library.
2313 return ConstantFoldBinaryFP(pow, APFloat(2.0), APF, Ty);
2314 break;
2315 case LibFunc_fabs:
2316 case LibFunc_fabsf:
2317 if (TLI->has(Func)) {
2318 U.clearSign();
2319 return ConstantFP::get(Ty->getContext(), U);
2320 }
2321 break;
2322 case LibFunc_floor:
2323 case LibFunc_floorf:
2324 if (TLI->has(Func)) {
2325 U.roundToIntegral(APFloat::rmTowardNegative);
2326 return ConstantFP::get(Ty->getContext(), U);
2327 }
2328 break;
2329 case LibFunc_log:
2330 case LibFunc_logf:
2331 case LibFunc_log_finite:
2332 case LibFunc_logf_finite:
2333 if (!APF.isNegative() && !APF.isZero() && TLI->has(Func))
2334 return ConstantFoldFP(log, APF, Ty);
2335 break;
2336 case LibFunc_log2:
2337 case LibFunc_log2f:
2338 case LibFunc_log2_finite:
2339 case LibFunc_log2f_finite:
2340 if (!APF.isNegative() && !APF.isZero() && TLI->has(Func))
2341 // TODO: What about hosts that lack a C99 library?
2342 return ConstantFoldFP(log2, APF, Ty);
2343 break;
2344 case LibFunc_log10:
2345 case LibFunc_log10f:
2346 case LibFunc_log10_finite:
2347 case LibFunc_log10f_finite:
2348 if (!APF.isNegative() && !APF.isZero() && TLI->has(Func))
2349 // TODO: What about hosts that lack a C99 library?
2350 return ConstantFoldFP(log10, APF, Ty);
2351 break;
2352 case LibFunc_nearbyint:
2353 case LibFunc_nearbyintf:
2354 case LibFunc_rint:
2355 case LibFunc_rintf:
2356 if (TLI->has(Func)) {
2357 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2358 return ConstantFP::get(Ty->getContext(), U);
2359 }
2360 break;
2361 case LibFunc_round:
2362 case LibFunc_roundf:
2363 if (TLI->has(Func)) {
2364 U.roundToIntegral(APFloat::rmNearestTiesToAway);
2365 return ConstantFP::get(Ty->getContext(), U);
2366 }
2367 break;
2368 case LibFunc_sin:
2369 case LibFunc_sinf:
2370 if (TLI->has(Func))
2371 return ConstantFoldFP(sin, APF, Ty);
2372 break;
2373 case LibFunc_sinh:
2374 case LibFunc_sinhf:
2375 case LibFunc_sinh_finite:
2376 case LibFunc_sinhf_finite:
2377 if (TLI->has(Func))
2378 return ConstantFoldFP(sinh, APF, Ty);
2379 break;
2380 case LibFunc_sqrt:
2381 case LibFunc_sqrtf:
2382 if (!APF.isNegative() && TLI->has(Func))
2383 return ConstantFoldFP(sqrt, APF, Ty);
2384 break;
2385 case LibFunc_tan:
2386 case LibFunc_tanf:
2387 if (TLI->has(Func))
2388 return ConstantFoldFP(tan, APF, Ty);
2389 break;
2390 case LibFunc_tanh:
2391 case LibFunc_tanhf:
2392 if (TLI->has(Func))
2393 return ConstantFoldFP(tanh, APF, Ty);
2394 break;
2395 case LibFunc_trunc:
2396 case LibFunc_truncf:
2397 if (TLI->has(Func)) {
2398 U.roundToIntegral(APFloat::rmTowardZero);
2399 return ConstantFP::get(Ty->getContext(), U);
2400 }
2401 break;
2402 }
2403 return nullptr;
2404 }
2405
2406 if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
2407 switch (IntrinsicID) {
2408 case Intrinsic::bswap:
2409 return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
2410 case Intrinsic::ctpop:
2411 return ConstantInt::get(Ty, Op->getValue().popcount());
2412 case Intrinsic::bitreverse:
2413 return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits());
2414 case Intrinsic::convert_from_fp16: {
2415 APFloat Val(APFloat::IEEEhalf(), Op->getValue());
2416
2417 bool lost = false;
2419 Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &lost);
2420
2421 // Conversion is always precise.
2422 (void)status;
2423 assert(status != APFloat::opInexact && !lost &&
2424 "Precision lost during fp16 constfolding");
2425
2426 return ConstantFP::get(Ty->getContext(), Val);
2427 }
2428
2429 case Intrinsic::amdgcn_s_wqm: {
2430 uint64_t Val = Op->getZExtValue();
2431 Val |= (Val & 0x5555555555555555ULL) << 1 |
2432 ((Val >> 1) & 0x5555555555555555ULL);
2433 Val |= (Val & 0x3333333333333333ULL) << 2 |
2434 ((Val >> 2) & 0x3333333333333333ULL);
2435 return ConstantInt::get(Ty, Val);
2436 }
2437
2438 case Intrinsic::amdgcn_s_quadmask: {
2439 uint64_t Val = Op->getZExtValue();
2440 uint64_t QuadMask = 0;
2441 for (unsigned I = 0; I < Op->getBitWidth() / 4; ++I, Val >>= 4) {
2442 if (!(Val & 0xF))
2443 continue;
2444
2445 QuadMask |= (1ULL << I);
2446 }
2447 return ConstantInt::get(Ty, QuadMask);
2448 }
2449
2450 case Intrinsic::amdgcn_s_bitreplicate: {
2451 uint64_t Val = Op->getZExtValue();
2452 Val = (Val & 0x000000000000FFFFULL) | (Val & 0x00000000FFFF0000ULL) << 16;
2453 Val = (Val & 0x000000FF000000FFULL) | (Val & 0x0000FF000000FF00ULL) << 8;
2454 Val = (Val & 0x000F000F000F000FULL) | (Val & 0x00F000F000F000F0ULL) << 4;
2455 Val = (Val & 0x0303030303030303ULL) | (Val & 0x0C0C0C0C0C0C0C0CULL) << 2;
2456 Val = (Val & 0x1111111111111111ULL) | (Val & 0x2222222222222222ULL) << 1;
2457 Val = Val | Val << 1;
2458 return ConstantInt::get(Ty, Val);
2459 }
2460
2461 default:
2462 return nullptr;
2463 }
2464 }
2465
2466 switch (IntrinsicID) {
2467 default: break;
2468 case Intrinsic::vector_reduce_add:
2469 case Intrinsic::vector_reduce_mul:
2470 case Intrinsic::vector_reduce_and:
2471 case Intrinsic::vector_reduce_or:
2472 case Intrinsic::vector_reduce_xor:
2473 case Intrinsic::vector_reduce_smin:
2474 case Intrinsic::vector_reduce_smax:
2475 case Intrinsic::vector_reduce_umin:
2476 case Intrinsic::vector_reduce_umax:
2477 if (Constant *C = constantFoldVectorReduce(IntrinsicID, Operands[0]))
2478 return C;
2479 break;
2480 }
2481
2482 // Support ConstantVector in case we have an Undef in the top.
2483 if (isa<ConstantVector>(Operands[0]) ||
2484 isa<ConstantDataVector>(Operands[0])) {
2485 auto *Op = cast<Constant>(Operands[0]);
2486 switch (IntrinsicID) {
2487 default: break;
2488 case Intrinsic::x86_sse_cvtss2si:
2489 case Intrinsic::x86_sse_cvtss2si64:
2490 case Intrinsic::x86_sse2_cvtsd2si:
2491 case Intrinsic::x86_sse2_cvtsd2si64:
2492 if (ConstantFP *FPOp =
2493 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2494 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2495 /*roundTowardZero=*/false, Ty,
2496 /*IsSigned*/true);
2497 break;
2498 case Intrinsic::x86_sse_cvttss2si:
2499 case Intrinsic::x86_sse_cvttss2si64:
2500 case Intrinsic::x86_sse2_cvttsd2si:
2501 case Intrinsic::x86_sse2_cvttsd2si64:
2502 if (ConstantFP *FPOp =
2503 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2504 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2505 /*roundTowardZero=*/true, Ty,
2506 /*IsSigned*/true);
2507 break;
2508 }
2509 }
2510
2511 return nullptr;
2512}
2513
2514static Constant *evaluateCompare(const APFloat &Op1, const APFloat &Op2,
2515 const ConstrainedFPIntrinsic *Call) {
2516 APFloat::opStatus St = APFloat::opOK;
2517 auto *FCmp = cast<ConstrainedFPCmpIntrinsic>(Call);
2518 FCmpInst::Predicate Cond = FCmp->getPredicate();
2519 if (FCmp->isSignaling()) {
2520 if (Op1.isNaN() || Op2.isNaN())
2521 St = APFloat::opInvalidOp;
2522 } else {
2523 if (Op1.isSignaling() || Op2.isSignaling())
2524 St = APFloat::opInvalidOp;
2525 }
2526 bool Result = FCmpInst::compare(Op1, Op2, Cond);
2527 if (mayFoldConstrained(const_cast<ConstrainedFPCmpIntrinsic *>(FCmp), St))
2528 return ConstantInt::get(Call->getType()->getScalarType(), Result);
2529 return nullptr;
2530}
2531
2532static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
2534 const TargetLibraryInfo *TLI) {
2535 if (!TLI)
2536 return nullptr;
2537
2539 if (!TLI->getLibFunc(Name, Func))
2540 return nullptr;
2541
2542 const auto *Op1 = dyn_cast<ConstantFP>(Operands[0]);
2543 if (!Op1)
2544 return nullptr;
2545
2546 const auto *Op2 = dyn_cast<ConstantFP>(Operands[1]);
2547 if (!Op2)
2548 return nullptr;
2549
2550 const APFloat &Op1V = Op1->getValueAPF();
2551 const APFloat &Op2V = Op2->getValueAPF();
2552
2553 switch (Func) {
2554 default:
2555 break;
2556 case LibFunc_pow:
2557 case LibFunc_powf:
2558 case LibFunc_pow_finite:
2559 case LibFunc_powf_finite:
2560 if (TLI->has(Func))
2561 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
2562 break;
2563 case LibFunc_fmod:
2564 case LibFunc_fmodf:
2565 if (TLI->has(Func)) {
2566 APFloat V = Op1->getValueAPF();
2567 if (APFloat::opStatus::opOK == V.mod(Op2->getValueAPF()))
2568 return ConstantFP::get(Ty->getContext(), V);
2569 }
2570 break;
2571 case LibFunc_remainder:
2572 case LibFunc_remainderf:
2573 if (TLI->has(Func)) {
2574 APFloat V = Op1->getValueAPF();
2575 if (APFloat::opStatus::opOK == V.remainder(Op2->getValueAPF()))
2576 return ConstantFP::get(Ty->getContext(), V);
2577 }
2578 break;
2579 case LibFunc_atan2:
2580 case LibFunc_atan2f:
2581 // atan2(+/-0.0, +/-0.0) is known to raise an exception on some libm
2582 // (Solaris), so we do not assume a known result for that.
2583 if (Op1V.isZero() && Op2V.isZero())
2584 return nullptr;
2585 [[fallthrough]];
2586 case LibFunc_atan2_finite:
2587 case LibFunc_atan2f_finite:
2588 if (TLI->has(Func))
2589 return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
2590 break;
2591 }
2592
2593 return nullptr;
2594}
2595
2596static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
2598 const CallBase *Call) {
2599 assert(Operands.size() == 2 && "Wrong number of operands.");
2600
2601 if (Ty->isFloatingPointTy()) {
2602 // TODO: We should have undef handling for all of the FP intrinsics that
2603 // are attempted to be folded in this function.
2604 bool IsOp0Undef = isa<UndefValue>(Operands[0]);
2605 bool IsOp1Undef = isa<UndefValue>(Operands[1]);
2606 switch (IntrinsicID) {
2607 case Intrinsic::maxnum:
2608 case Intrinsic::minnum:
2609 case Intrinsic::maximum:
2610 case Intrinsic::minimum:
2611 // If one argument is undef, return the other argument.
2612 if (IsOp0Undef)
2613 return Operands[1];
2614 if (IsOp1Undef)
2615 return Operands[0];
2616 break;
2617 }
2618 }
2619
2620 if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
2621 const APFloat &Op1V = Op1->getValueAPF();
2622
2623 if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
2624 if (Op2->getType() != Op1->getType())
2625 return nullptr;
2626 const APFloat &Op2V = Op2->getValueAPF();
2627
2628 if (const auto *ConstrIntr =
2629 dyn_cast_if_present<ConstrainedFPIntrinsic>(Call)) {
2630 RoundingMode RM = getEvaluationRoundingMode(ConstrIntr);
2631 APFloat Res = Op1V;
2633 switch (IntrinsicID) {
2634 default:
2635 return nullptr;
2636 case Intrinsic::experimental_constrained_fadd:
2637 St = Res.add(Op2V, RM);
2638 break;
2639 case Intrinsic::experimental_constrained_fsub:
2640 St = Res.subtract(Op2V, RM);
2641 break;
2642 case Intrinsic::experimental_constrained_fmul:
2643 St = Res.multiply(Op2V, RM);
2644 break;
2645 case Intrinsic::experimental_constrained_fdiv:
2646 St = Res.divide(Op2V, RM);
2647 break;
2648 case Intrinsic::experimental_constrained_frem:
2649 St = Res.mod(Op2V);
2650 break;
2651 case Intrinsic::experimental_constrained_fcmp:
2652 case Intrinsic::experimental_constrained_fcmps:
2653 return evaluateCompare(Op1V, Op2V, ConstrIntr);
2654 }
2655 if (mayFoldConstrained(const_cast<ConstrainedFPIntrinsic *>(ConstrIntr),
2656 St))
2657 return ConstantFP::get(Ty->getContext(), Res);
2658 return nullptr;
2659 }
2660
2661 switch (IntrinsicID) {
2662 default:
2663 break;
2664 case Intrinsic::copysign:
2665 return ConstantFP::get(Ty->getContext(), APFloat::copySign(Op1V, Op2V));
2666 case Intrinsic::minnum:
2667 return ConstantFP::get(Ty->getContext(), minnum(Op1V, Op2V));
2668 case Intrinsic::maxnum:
2669 return ConstantFP::get(Ty->getContext(), maxnum(Op1V, Op2V));
2670 case Intrinsic::minimum:
2671 return ConstantFP::get(Ty->getContext(), minimum(Op1V, Op2V));
2672 case Intrinsic::maximum:
2673 return ConstantFP::get(Ty->getContext(), maximum(Op1V, Op2V));
2674 }
2675
2676 if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
2677 return nullptr;
2678
2679 switch (IntrinsicID) {
2680 default:
2681 break;
2682 case Intrinsic::pow:
2683 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
2684 case Intrinsic::amdgcn_fmul_legacy:
2685 // The legacy behaviour is that multiplying +/- 0.0 by anything, even
2686 // NaN or infinity, gives +0.0.
2687 if (Op1V.isZero() || Op2V.isZero())
2688 return ConstantFP::getZero(Ty);
2689 return ConstantFP::get(Ty->getContext(), Op1V * Op2V);
2690 }
2691
2692 } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
2693 switch (IntrinsicID) {
2694 case Intrinsic::ldexp: {
2695 return ConstantFP::get(
2696 Ty->getContext(),
2697 scalbn(Op1V, Op2C->getSExtValue(), APFloat::rmNearestTiesToEven));
2698 }
2699 case Intrinsic::is_fpclass: {
2700 FPClassTest Mask = static_cast<FPClassTest>(Op2C->getZExtValue());
2701 bool Result =
2702 ((Mask & fcSNan) && Op1V.isNaN() && Op1V.isSignaling()) ||
2703 ((Mask & fcQNan) && Op1V.isNaN() && !Op1V.isSignaling()) ||
2704 ((Mask & fcNegInf) && Op1V.isNegInfinity()) ||
2705 ((Mask & fcNegNormal) && Op1V.isNormal() && Op1V.isNegative()) ||
2706 ((Mask & fcNegSubnormal) && Op1V.isDenormal() && Op1V.isNegative()) ||
2707 ((Mask & fcNegZero) && Op1V.isZero() && Op1V.isNegative()) ||
2708 ((Mask & fcPosZero) && Op1V.isZero() && !Op1V.isNegative()) ||
2709 ((Mask & fcPosSubnormal) && Op1V.isDenormal() && !Op1V.isNegative()) ||
2710 ((Mask & fcPosNormal) && Op1V.isNormal() && !Op1V.isNegative()) ||
2711 ((Mask & fcPosInf) && Op1V.isPosInfinity());
2712 return ConstantInt::get(Ty, Result);
2713 }
2714 default:
2715 break;
2716 }
2717
2718 if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
2719 return nullptr;
2720 if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
2721 return ConstantFP::get(
2722 Ty->getContext(),
2723 APFloat((float)std::pow((float)Op1V.convertToDouble(),
2724 (int)Op2C->getZExtValue())));
2725 if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy())
2726 return ConstantFP::get(
2727 Ty->getContext(),
2728 APFloat((float)std::pow((float)Op1V.convertToDouble(),
2729 (int)Op2C->getZExtValue())));
2730 if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy())
2731 return ConstantFP::get(
2732 Ty->getContext(),
2733 APFloat((double)std::pow(Op1V.convertToDouble(),
2734 (int)Op2C->getZExtValue())));
2735 }
2736 return nullptr;
2737 }
2738
2739 if (Operands[0]->getType()->isIntegerTy() &&
2740 Operands[1]->getType()->isIntegerTy()) {
2741 const APInt *C0, *C1;
2742 if (!getConstIntOrUndef(Operands[0], C0) ||
2743 !getConstIntOrUndef(Operands[1], C1))
2744 return nullptr;
2745
2746 switch (IntrinsicID) {
2747 default: break;
2748 case Intrinsic::smax:
2749 case Intrinsic::smin:
2750 case Intrinsic::umax:
2751 case Intrinsic::umin:
2752 // This is the same as for binary ops - poison propagates.
2753 // TODO: Poison handling should be consolidated.
2754 if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
2755 return PoisonValue::get(Ty);
2756
2757 if (!C0 && !C1)
2758 return UndefValue::get(Ty);
2759 if (!C0 || !C1)
2760 return MinMaxIntrinsic::getSaturationPoint(IntrinsicID, Ty);
2761 return ConstantInt::get(
2762 Ty, ICmpInst::compare(*C0, *C1,
2763 MinMaxIntrinsic::getPredicate(IntrinsicID))
2764 ? *C0
2765 : *C1);
2766
2767 case Intrinsic::usub_with_overflow:
2768 case Intrinsic::ssub_with_overflow:
2769 // X - undef -> { 0, false }
2770 // undef - X -> { 0, false }
2771 if (!C0 || !C1)
2772 return Constant::getNullValue(Ty);
2773 [[fallthrough]];
2774 case Intrinsic::uadd_with_overflow:
2775 case Intrinsic::sadd_with_overflow:
2776 // X + undef -> { -1, false }
2777 // undef + x -> { -1, false }
2778 if (!C0 || !C1) {
2779 return ConstantStruct::get(
2780 cast<StructType>(Ty),
2783 }
2784 [[fallthrough]];
2785 case Intrinsic::smul_with_overflow:
2786 case Intrinsic::umul_with_overflow: {
2787 // undef * X -> { 0, false }
2788 // X * undef -> { 0, false }
2789 if (!C0 || !C1)
2790 return Constant::getNullValue(Ty);
2791
2792 APInt Res;
2793 bool Overflow;
2794 switch (IntrinsicID) {
2795 default: llvm_unreachable("Invalid case");
2796 case Intrinsic::sadd_with_overflow:
2797 Res = C0->sadd_ov(*C1, Overflow);
2798 break;
2799 case Intrinsic::uadd_with_overflow:
2800 Res = C0->uadd_ov(*C1, Overflow);
2801 break;
2802 case Intrinsic::ssub_with_overflow:
2803 Res = C0->ssub_ov(*C1, Overflow);
2804 break;
2805 case Intrinsic::usub_with_overflow:
2806 Res = C0->usub_ov(*C1, Overflow);
2807 break;
2808 case Intrinsic::smul_with_overflow:
2809 Res = C0->smul_ov(*C1, Overflow);
2810 break;
2811 case Intrinsic::umul_with_overflow:
2812 Res = C0->umul_ov(*C1, Overflow);
2813 break;
2814 }
2815 Constant *Ops[] = {
2816 ConstantInt::get(Ty->getContext(), Res),
2817 ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow)
2818 };
2819 return ConstantStruct::get(cast<StructType>(Ty), Ops);
2820 }
2821 case Intrinsic::uadd_sat:
2822 case Intrinsic::sadd_sat:
2823 // This is the same as for binary ops - poison propagates.
2824 // TODO: Poison handling should be consolidated.
2825 if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
2826 return PoisonValue::get(Ty);
2827
2828 if (!C0 && !C1)
2829 return UndefValue::get(Ty);
2830 if (!C0 || !C1)
2831 return Constant::getAllOnesValue(Ty);
2832 if (IntrinsicID == Intrinsic::uadd_sat)
2833 return ConstantInt::get(Ty, C0->uadd_sat(*C1));
2834 else
2835 return ConstantInt::get(Ty, C0->sadd_sat(*C1));
2836 case Intrinsic::usub_sat:
2837 case Intrinsic::ssub_sat:
2838 // This is the same as for binary ops - poison propagates.
2839 // TODO: Poison handling should be consolidated.
2840 if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
2841 return PoisonValue::get(Ty);
2842
2843 if (!C0 && !C1)
2844 return UndefValue::get(Ty);
2845 if (!C0 || !C1)
2846 return Constant::getNullValue(Ty);
2847 if (IntrinsicID == Intrinsic::usub_sat)
2848 return ConstantInt::get(Ty, C0->usub_sat(*C1));
2849 else
2850 return ConstantInt::get(Ty, C0->ssub_sat(*C1));
2851 case Intrinsic::cttz:
2852 case Intrinsic::ctlz:
2853 assert(C1 && "Must be constant int");
2854
2855 // cttz(0, 1) and ctlz(0, 1) are poison.
2856 if (C1->isOne() && (!C0 || C0->isZero()))
2857 return PoisonValue::get(Ty);
2858 if (!C0)
2859 return Constant::getNullValue(Ty);
2860 if (IntrinsicID == Intrinsic::cttz)
2861 return ConstantInt::get(Ty, C0->countr_zero());
2862 else
2863 return ConstantInt::get(Ty, C0->countl_zero());
2864
2865 case Intrinsic::abs:
2866 assert(C1 && "Must be constant int");
2867 assert((C1->isOne() || C1->isZero()) && "Must be 0 or 1");
2868
2869 // Undef or minimum val operand with poison min --> undef
2870 if (C1->isOne() && (!C0 || C0->isMinSignedValue()))
2871 return UndefValue::get(Ty);
2872
2873 // Undef operand with no poison min --> 0 (sign bit must be clear)
2874 if (!C0)
2875 return Constant::getNullValue(Ty);
2876
2877 return ConstantInt::get(Ty, C0->abs());
2878 case Intrinsic::amdgcn_wave_reduce_umin:
2879 case Intrinsic::amdgcn_wave_reduce_umax:
2880 return dyn_cast<Constant>(Operands[0]);
2881 }
2882
2883 return nullptr;
2884 }
2885
2886 // Support ConstantVector in case we have an Undef in the top.
2887 if ((isa<ConstantVector>(Operands[0]) ||
2888 isa<ConstantDataVector>(Operands[0])) &&
2889 // Check for default rounding mode.
2890 // FIXME: Support other rounding modes?
2891 isa<ConstantInt>(Operands[1]) &&
2892 cast<ConstantInt>(Operands[1])->getValue() == 4) {
2893 auto *Op = cast<Constant>(Operands[0]);
2894 switch (IntrinsicID) {
2895 default: break;
2896 case Intrinsic::x86_avx512_vcvtss2si32:
2897 case Intrinsic::x86_avx512_vcvtss2si64:
2898 case Intrinsic::x86_avx512_vcvtsd2si32:
2899 case Intrinsic::x86_avx512_vcvtsd2si64:
2900 if (ConstantFP *FPOp =
2901 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2902 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2903 /*roundTowardZero=*/false, Ty,
2904 /*IsSigned*/true);
2905 break;
2906 case Intrinsic::x86_avx512_vcvtss2usi32:
2907 case Intrinsic::x86_avx512_vcvtss2usi64:
2908 case Intrinsic::x86_avx512_vcvtsd2usi32:
2909 case Intrinsic::x86_avx512_vcvtsd2usi64:
2910 if (ConstantFP *FPOp =
2911 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2912 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2913 /*roundTowardZero=*/false, Ty,
2914 /*IsSigned*/false);
2915 break;
2916 case Intrinsic::x86_avx512_cvttss2si:
2917 case Intrinsic::x86_avx512_cvttss2si64:
2918 case Intrinsic::x86_avx512_cvttsd2si:
2919 case Intrinsic::x86_avx512_cvttsd2si64:
2920 if (ConstantFP *FPOp =
2921 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2922 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2923 /*roundTowardZero=*/true, Ty,
2924 /*IsSigned*/true);
2925 break;
2926 case Intrinsic::x86_avx512_cvttss2usi:
2927 case Intrinsic::x86_avx512_cvttss2usi64:
2928 case Intrinsic::x86_avx512_cvttsd2usi:
2929 case Intrinsic::x86_avx512_cvttsd2usi64:
2930 if (ConstantFP *FPOp =
2931 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2932 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2933 /*roundTowardZero=*/true, Ty,
2934 /*IsSigned*/false);
2935 break;
2936 }
2937 }
2938 return nullptr;
2939}
2940
2941static APFloat ConstantFoldAMDGCNCubeIntrinsic(Intrinsic::ID IntrinsicID,
2942 const APFloat &S0,
2943 const APFloat &S1,
2944 const APFloat &S2) {
2945 unsigned ID;
2946 const fltSemantics &Sem = S0.getSemantics();
2947 APFloat MA(Sem), SC(Sem), TC(Sem);
2948 if (abs(S2) >= abs(S0) && abs(S2) >= abs(S1)) {
2949 if (S2.isNegative() && S2.isNonZero() && !S2.isNaN()) {
2950 // S2 < 0
2951 ID = 5;
2952 SC = -S0;
2953 } else {
2954 ID = 4;
2955 SC = S0;
2956 }
2957 MA = S2;
2958 TC = -S1;
2959 } else if (abs(S1) >= abs(S0)) {
2960 if (S1.isNegative() && S1.isNonZero() && !S1.isNaN()) {
2961 // S1 < 0
2962 ID = 3;
2963 TC = -S2;
2964 } else {
2965 ID = 2;
2966 TC = S2;
2967 }
2968 MA = S1;
2969 SC = S0;
2970 } else {
2971 if (S0.isNegative() && S0.isNonZero() && !S0.isNaN()) {
2972 // S0 < 0
2973 ID = 1;
2974 SC = S2;
2975 } else {
2976 ID = 0;
2977 SC = -S2;
2978 }
2979 MA = S0;
2980 TC = -S1;
2981 }
2982 switch (IntrinsicID) {
2983 default:
2984 llvm_unreachable("unhandled amdgcn cube intrinsic");
2985 case Intrinsic::amdgcn_cubeid:
2986 return APFloat(Sem, ID);
2987 case Intrinsic::amdgcn_cubema:
2988 return MA + MA;
2989 case Intrinsic::amdgcn_cubesc:
2990 return SC;
2991 case Intrinsic::amdgcn_cubetc:
2992 return TC;
2993 }
2994}
2995
2996static Constant *ConstantFoldAMDGCNPermIntrinsic(ArrayRef<Constant *> Operands,
2997 Type *Ty) {
2998 const APInt *C0, *C1, *C2;
2999 if (!getConstIntOrUndef(Operands[0], C0) ||
3000 !getConstIntOrUndef(Operands[1], C1) ||
3001 !getConstIntOrUndef(Operands[2], C2))
3002 return nullptr;
3003
3004 if (!C2)
3005 return UndefValue::get(Ty);
3006
3007 APInt Val(32, 0);
3008 unsigned NumUndefBytes = 0;
3009 for (unsigned I = 0; I < 32; I += 8) {
3010 unsigned Sel = C2->extractBitsAsZExtValue(8, I);
3011 unsigned B = 0;
3012
3013 if (Sel >= 13)
3014 B = 0xff;
3015 else if (Sel == 12)
3016 B = 0x00;
3017 else {
3018 const APInt *Src = ((Sel & 10) == 10 || (Sel & 12) == 4) ? C0 : C1;
3019 if (!Src)
3020 ++NumUndefBytes;
3021 else if (Sel < 8)
3022 B = Src->extractBitsAsZExtValue(8, (Sel & 3) * 8);
3023 else
3024 B = Src->extractBitsAsZExtValue(1, (Sel & 1) ? 31 : 15) * 0xff;
3025 }
3026
3027 Val.insertBits(B, I, 8);
3028 }
3029
3030 if (NumUndefBytes == 4)
3031 return UndefValue::get(Ty);
3032
3033 return ConstantInt::get(Ty, Val);
3034}
3035
3036static Constant *ConstantFoldScalarCall3(StringRef Name,
3037 Intrinsic::ID IntrinsicID,
3038 Type *Ty,
3040 const TargetLibraryInfo *TLI,
3041 const CallBase *Call) {
3042 assert(Operands.size() == 3 && "Wrong number of operands.");
3043
3044 if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
3045 if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
3046 if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
3047 const APFloat &C1 = Op1->getValueAPF();
3048 const APFloat &C2 = Op2->getValueAPF();
3049 const APFloat &C3 = Op3->getValueAPF();
3050
3051 if (const auto *ConstrIntr = dyn_cast<ConstrainedFPIntrinsic>(Call)) {
3052 RoundingMode RM = getEvaluationRoundingMode(ConstrIntr);
3053 APFloat Res = C1;
3055 switch (IntrinsicID) {
3056 default:
3057 return nullptr;
3058 case Intrinsic::experimental_constrained_fma:
3059 case Intrinsic::experimental_constrained_fmuladd:
3060 St = Res.fusedMultiplyAdd(C2, C3, RM);
3061 break;
3062 }
3063 if (mayFoldConstrained(
3064 const_cast<ConstrainedFPIntrinsic *>(ConstrIntr), St))
3065 return ConstantFP::get(Ty->getContext(), Res);
3066 return nullptr;
3067 }
3068
3069 switch (IntrinsicID) {
3070 default: break;
3071 case Intrinsic::amdgcn_fma_legacy: {
3072 // The legacy behaviour is that multiplying +/- 0.0 by anything, even
3073 // NaN or infinity, gives +0.0.
3074 if (C1.isZero() || C2.isZero()) {
3075 // It's tempting to just return C3 here, but that would give the
3076 // wrong result if C3 was -0.0.
3077 return ConstantFP::get(Ty->getContext(), APFloat(0.0f) + C3);
3078 }
3079 [[fallthrough]];
3080 }
3081 case Intrinsic::fma:
3082 case Intrinsic::fmuladd: {
3083 APFloat V = C1;
3084 V.fusedMultiplyAdd(C2, C3, APFloat::rmNearestTiesToEven);
3085 return ConstantFP::get(Ty->getContext(), V);
3086 }
3087 case Intrinsic::amdgcn_cubeid:
3088 case Intrinsic::amdgcn_cubema:
3089 case Intrinsic::amdgcn_cubesc:
3090 case Intrinsic::amdgcn_cubetc: {
3091 APFloat V = ConstantFoldAMDGCNCubeIntrinsic(IntrinsicID, C1, C2, C3);
3092 return ConstantFP::get(Ty->getContext(), V);
3093 }
3094 }
3095 }
3096 }
3097 }
3098
3099 if (IntrinsicID == Intrinsic::smul_fix ||
3100 IntrinsicID == Intrinsic::smul_fix_sat) {
3101 // poison * C -> poison
3102 // C * poison -> poison
3103 if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
3104 return PoisonValue::get(Ty);
3105
3106 const APInt *C0, *C1;
3107 if (!getConstIntOrUndef(Operands[0], C0) ||
3108 !getConstIntOrUndef(Operands[1], C1))
3109 return nullptr;
3110
3111 // undef * C -> 0
3112 // C * undef -> 0
3113 if (!C0 || !C1)
3114 return Constant::getNullValue(Ty);
3115
3116 // This code performs rounding towards negative infinity in case the result
3117 // cannot be represented exactly for the given scale. Targets that do care
3118 // about rounding should use a target hook for specifying how rounding
3119 // should be done, and provide their own folding to be consistent with
3120 // rounding. This is the same approach as used by
3121 // DAGTypeLegalizer::ExpandIntRes_MULFIX.
3122 unsigned Scale = cast<ConstantInt>(Operands[2])->getZExtValue();
3123 unsigned Width = C0->getBitWidth();
3124 assert(Scale < Width && "Illegal scale.");
3125 unsigned ExtendedWidth = Width * 2;
3126 APInt Product =
3127 (C0->sext(ExtendedWidth) * C1->sext(ExtendedWidth)).ashr(Scale);
3128 if (IntrinsicID == Intrinsic::smul_fix_sat) {
3129 APInt Max = APInt::getSignedMaxValue(Width).sext(ExtendedWidth);
3130 APInt Min = APInt::getSignedMinValue(Width).sext(ExtendedWidth);
3131 Product = APIntOps::smin(Product, Max);
3132 Product = APIntOps::smax(Product, Min);
3133 }
3134 return ConstantInt::get(Ty->getContext(), Product.sextOrTrunc(Width));
3135 }
3136
3137 if (IntrinsicID == Intrinsic::fshl || IntrinsicID == Intrinsic::fshr) {
3138 const APInt *C0, *C1, *C2;
3139 if (!getConstIntOrUndef(Operands[0], C0) ||
3140 !getConstIntOrUndef(Operands[1], C1) ||
3141 !getConstIntOrUndef(Operands[2], C2))
3142 return nullptr;
3143
3144 bool IsRight = IntrinsicID == Intrinsic::fshr;
3145 if (!C2)
3146 return Operands[IsRight ? 1 : 0];
3147 if (!C0 && !C1)
3148 return UndefValue::get(Ty);
3149
3150 // The shift amount is interpreted as modulo the bitwidth. If the shift
3151 // amount is effectively 0, avoid UB due to oversized inverse shift below.
3152 unsigned BitWidth = C2->getBitWidth();
3153 unsigned ShAmt = C2->urem(BitWidth);
3154 if (!ShAmt)
3155 return Operands[IsRight ? 1 : 0];
3156
3157 // (C0 << ShlAmt) | (C1 >> LshrAmt)
3158 unsigned LshrAmt = IsRight ? ShAmt : BitWidth - ShAmt;
3159 unsigned ShlAmt = !IsRight ? ShAmt : BitWidth - ShAmt;
3160 if (!C0)
3161 return ConstantInt::get(Ty, C1->lshr(LshrAmt));
3162 if (!C1)
3163 return ConstantInt::get(Ty, C0->shl(ShlAmt));
3164 return ConstantInt::get(Ty, C0->shl(ShlAmt) | C1->lshr(LshrAmt));
3165 }
3166
3167 if (IntrinsicID == Intrinsic::amdgcn_perm)
3168 return ConstantFoldAMDGCNPermIntrinsic(Operands, Ty);
3169
3170 return nullptr;
3171}
3172
3173static Constant *ConstantFoldScalarCall(StringRef Name,
3174 Intrinsic::ID IntrinsicID,
3175 Type *Ty,
3177 const TargetLibraryInfo *TLI,
3178 const CallBase *Call) {
3179 if (Operands.size() == 1)
3180 return ConstantFoldScalarCall1(Name, IntrinsicID, Ty, Operands, TLI, Call);
3181
3182 if (Operands.size() == 2) {
3183 if (Constant *FoldedLibCall =
3184 ConstantFoldLibCall2(Name, Ty, Operands, TLI)) {
3185 return FoldedLibCall;
3186 }
3187 return ConstantFoldIntrinsicCall2(IntrinsicID, Ty, Operands, Call);
3188 }
3189
3190 if (Operands.size() == 3)
3191 return ConstantFoldScalarCall3(Name, IntrinsicID, Ty, Operands, TLI, Call);
3192
3193 return nullptr;
3194}
3195
3196static Constant *ConstantFoldFixedVectorCall(
3197 StringRef Name, Intrinsic::ID IntrinsicID, FixedVectorType *FVTy,
3199 const TargetLibraryInfo *TLI, const CallBase *Call) {
3202 Type *Ty = FVTy->getElementType();
3203
3204 switch (IntrinsicID) {
3205 case Intrinsic::masked_load: {
3206 auto *SrcPtr = Operands[0];
3207 auto *Mask = Operands[2];
3208 auto *Passthru = Operands[3];
3209
3210 Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, FVTy, DL);
3211
3212 SmallVector<Constant *, 32> NewElements;
3213 for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
3214 auto *MaskElt = Mask->getAggregateElement(I);
3215 if (!MaskElt)
3216 break;
3217 auto *PassthruElt = Passthru->getAggregateElement(I);
3218 auto *VecElt = VecData ? VecData->getAggregateElement(I) : nullptr;
3219 if (isa<UndefValue>(MaskElt)) {
3220 if (PassthruElt)
3221 NewElements.push_back(PassthruElt);
3222 else if (VecElt)
3223 NewElements.push_back(VecElt);
3224 else
3225 return nullptr;
3226 }
3227 if (MaskElt->isNullValue()) {
3228 if (!PassthruElt)
3229 return nullptr;
3230 NewElements.push_back(PassthruElt);
3231 } else if (MaskElt->isOneValue()) {
3232 if (!VecElt)
3233 return nullptr;
3234 NewElements.push_back(VecElt);
3235 } else {
3236 return nullptr;
3237 }
3238 }
3239 if (NewElements.size() != FVTy->getNumElements())
3240 return nullptr;
3241 return ConstantVector::get(NewElements);
3242 }
3243 case Intrinsic::arm_mve_vctp8:
3244 case Intrinsic::arm_mve_vctp16:
3245 case Intrinsic::arm_mve_vctp32:
3246 case Intrinsic::arm_mve_vctp64: {
3247 if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
3248 unsigned Lanes = FVTy->getNumElements();
3249 uint64_t Limit = Op->getZExtValue();
3250
3252 for (unsigned i = 0; i < Lanes; i++) {
3253 if (i < Limit)
3255 else
3257 }
3258 return ConstantVector::get(NCs);
3259 }
3260 return nullptr;
3261 }
3262 case Intrinsic::get_active_lane_mask: {
3263 auto *Op0 = dyn_cast<ConstantInt>(Operands[0]);
3264 auto *Op1 = dyn_cast<ConstantInt>(Operands[1]);
3265 if (Op0 && Op1) {
3266 unsigned Lanes = FVTy->getNumElements();
3267 uint64_t Base = Op0->getZExtValue();
3268 uint64_t Limit = Op1->getZExtValue();
3269
3271 for (unsigned i = 0; i < Lanes; i++) {
3272 if (Base + i < Limit)
3274 else
3276 }
3277 return ConstantVector::get(NCs);
3278 }
3279 return nullptr;
3280 }
3281 default:
3282 break;
3283 }
3284
3285 for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
3286 // Gather a column of constants.
3287 for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
3288 // Some intrinsics use a scalar type for certain arguments.
3289 if (isVectorIntrinsicWithScalarOpAtArg(IntrinsicID, J)) {
3290 Lane[J] = Operands[J];
3291 continue;
3292 }
3293
3294 Constant *Agg = Operands[J]->getAggregateElement(I);
3295 if (!Agg)
3296 return nullptr;
3297
3298 Lane[J] = Agg;
3299 }
3300
3301 // Use the regular scalar folding to simplify this column.
3302 Constant *Folded =
3303 ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI, Call);
3304 if (!Folded)
3305 return nullptr;
3306 Result[I] = Folded;
3307 }
3308
3309 return ConstantVector::get(Result);
3310}
3311
3312static Constant *ConstantFoldScalableVectorCall(
3313 StringRef Name, Intrinsic::ID IntrinsicID, ScalableVectorType *SVTy,
3315 const TargetLibraryInfo *TLI, const CallBase *Call) {
3316 switch (IntrinsicID) {
3317 case Intrinsic::aarch64_sve_convert_from_svbool: {
3318 auto *Src = dyn_cast<Constant>(Operands[0]);
3319 if (!Src || !Src->isNullValue())
3320 break;
3321
3322 return ConstantInt::getFalse(SVTy);
3323 }
3324 default:
3325 break;
3326 }
3327 return nullptr;
3328}
3329
3330static std::pair<Constant *, Constant *>
3331ConstantFoldScalarFrexpCall(Constant *Op, Type *IntTy) {
3332 if (isa<PoisonValue>(Op))
3333 return {Op, PoisonValue::get(IntTy)};
3334
3335 auto *ConstFP = dyn_cast<ConstantFP>(Op);
3336 if (!ConstFP)
3337 return {};
3338
3339 const APFloat &U = ConstFP->getValueAPF();
3340 int FrexpExp;
3341 APFloat FrexpMant = frexp(U, FrexpExp, APFloat::rmNearestTiesToEven);
3342 Constant *Result0 = ConstantFP::get(ConstFP->getType(), FrexpMant);
3343
3344 // The exponent is an "unspecified value" for inf/nan. We use zero to avoid
3345 // using undef.
3346 Constant *Result1 = FrexpMant.isFinite() ? ConstantInt::get(IntTy, FrexpExp)
3347 : ConstantInt::getNullValue(IntTy);
3348 return {Result0, Result1};
3349}
3350
3351/// Handle intrinsics that return tuples, which may be tuples of vectors.
3352static Constant *
3353ConstantFoldStructCall(StringRef Name, Intrinsic::ID IntrinsicID,
3355 const DataLayout &DL, const TargetLibraryInfo *TLI,
3356 const CallBase *Call) {
3357
3358 switch (IntrinsicID) {
3359 case Intrinsic::frexp: {
3360 Type *Ty0 = StTy->getContainedType(0);
3361 Type *Ty1 = StTy->getContainedType(1)->getScalarType();
3362
3363 if (auto *FVTy0 = dyn_cast<FixedVectorType>(Ty0)) {
3364 SmallVector<Constant *, 4> Results0(FVTy0->getNumElements());
3365 SmallVector<Constant *, 4> Results1(FVTy0->getNumElements());
3366
3367 for (unsigned I = 0, E = FVTy0->getNumElements(); I != E; ++I) {
3368 Constant *Lane = Operands[0]->getAggregateElement(I);
3369 std::tie(Results0[I], Results1[I]) =
3370 ConstantFoldScalarFrexpCall(Lane, Ty1);
3371 if (!Results0[I])
3372 return nullptr;
3373 }
3374
3375 return ConstantStruct::get(StTy, ConstantVector::get(Results0),
3376 ConstantVector::get(Results1));
3377 }
3378
3379 auto [Result0, Result1] = ConstantFoldScalarFrexpCall(Operands[0], Ty1);
3380 if (!Result0)
3381 return nullptr;
3382 return ConstantStruct::get(StTy, Result0, Result1);
3383 }
3384 default:
3385 // TODO: Constant folding of vector intrinsics that fall through here does
3386 // not work (e.g. overflow intrinsics)
3387 return ConstantFoldScalarCall(Name, IntrinsicID, StTy, Operands, TLI, Call);
3388 }
3389
3390 return nullptr;
3391}
3392
3393} // end anonymous namespace
3394
3396 Constant *RHS, Type *Ty,
3397 Instruction *FMFSource) {
3398 return ConstantFoldIntrinsicCall2(ID, Ty, {LHS, RHS},
3399 dyn_cast_if_present<CallBase>(FMFSource));
3400}
3401
3404 const TargetLibraryInfo *TLI) {
3405 if (Call->isNoBuiltin())
3406 return nullptr;
3407 if (!F->hasName())
3408 return nullptr;
3409
3410 // If this is not an intrinsic and not recognized as a library call, bail out.
3411 Intrinsic::ID IID = F->getIntrinsicID();
3412 if (IID == Intrinsic::not_intrinsic) {
3413 if (!TLI)
3414 return nullptr;
3415 LibFunc LibF;
3416 if (!TLI->getLibFunc(*F, LibF))
3417 return nullptr;
3418 }
3419
3420 StringRef Name = F->getName();
3421 Type *Ty = F->getReturnType();
3422 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty))
3423 return ConstantFoldFixedVectorCall(
3424 Name, IID, FVTy, Operands, F->getParent()->getDataLayout(), TLI, Call);
3425
3426 if (auto *SVTy = dyn_cast<ScalableVectorType>(Ty))
3427 return ConstantFoldScalableVectorCall(
3428 Name, IID, SVTy, Operands, F->getParent()->getDataLayout(), TLI, Call);
3429
3430 if (auto *StTy = dyn_cast<StructType>(Ty))
3431 return ConstantFoldStructCall(Name, IID, StTy, Operands,
3432 F->getParent()->getDataLayout(), TLI, Call);
3433
3434 // TODO: If this is a library function, we already discovered that above,
3435 // so we should pass the LibFunc, not the name (and it might be better
3436 // still to separate intrinsic handling from libcalls).
3437 return ConstantFoldScalarCall(Name, IID, Ty, Operands, TLI, Call);
3438}
3439
3441 const TargetLibraryInfo *TLI) {
3442 // FIXME: Refactor this code; this duplicates logic in LibCallsShrinkWrap
3443 // (and to some extent ConstantFoldScalarCall).
3444 if (Call->isNoBuiltin() || Call->isStrictFP())
3445 return false;
3446 Function *F = Call->getCalledFunction();
3447 if (!F)
3448 return false;
3449
3450 LibFunc Func;
3451 if (!TLI || !TLI->getLibFunc(*F, Func))
3452 return false;
3453
3454 if (Call->arg_size() == 1) {
3455 if (ConstantFP *OpC = dyn_cast<ConstantFP>(Call->getArgOperand(0))) {
3456 const APFloat &Op = OpC->getValueAPF();
3457 switch (Func) {
3458 case LibFunc_logl:
3459 case LibFunc_log:
3460 case LibFunc_logf:
3461 case LibFunc_log2l:
3462 case LibFunc_log2:
3463 case LibFunc_log2f:
3464 case LibFunc_log10l:
3465 case LibFunc_log10:
3466 case LibFunc_log10f:
3467 return Op.isNaN() || (!Op.isZero() && !Op.isNegative());
3468
3469 case LibFunc_expl:
3470 case LibFunc_exp:
3471 case LibFunc_expf:
3472 // FIXME: These boundaries are slightly conservative.
3473 if (OpC->getType()->isDoubleTy())
3474 return !(Op < APFloat(-745.0) || Op > APFloat(709.0));
3475 if (OpC->getType()->isFloatTy())
3476 return !(Op < APFloat(-103.0f) || Op > APFloat(88.0f));
3477 break;
3478
3479 case LibFunc_exp2l:
3480 case LibFunc_exp2:
3481 case LibFunc_exp2f:
3482 // FIXME: These boundaries are slightly conservative.
3483 if (OpC->getType()->isDoubleTy())
3484 return !(Op < APFloat(-1074.0) || Op > APFloat(1023.0));
3485 if (OpC->getType()->isFloatTy())
3486 return !(Op < APFloat(-149.0f) || Op > APFloat(127.0f));
3487 break;
3488
3489 case LibFunc_sinl:
3490 case LibFunc_sin:
3491 case LibFunc_sinf:
3492 case LibFunc_cosl:
3493 case LibFunc_cos:
3494 case LibFunc_cosf:
3495 return !Op.isInfinity();
3496
3497 case LibFunc_tanl:
3498 case LibFunc_tan:
3499 case LibFunc_tanf: {
3500 // FIXME: Stop using the host math library.
3501 // FIXME: The computation isn't done in the right precision.
3502 Type *Ty = OpC->getType();
3503 if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy())
3504 return ConstantFoldFP(tan, OpC->getValueAPF(), Ty) != nullptr;
3505 break;
3506 }
3507
3508 case LibFunc_atan:
3509 case LibFunc_atanf:
3510 case LibFunc_atanl:
3511 // Per POSIX, this MAY fail if Op is denormal. We choose not failing.
3512 return true;
3513
3514
3515 case LibFunc_asinl:
3516 case LibFunc_asin:
3517 case LibFunc_asinf:
3518 case LibFunc_acosl:
3519 case LibFunc_acos:
3520 case LibFunc_acosf:
3521 return !(Op < APFloat(Op.getSemantics(), "-1") ||
3522 Op > APFloat(Op.getSemantics(), "1"));
3523
3524 case LibFunc_sinh:
3525 case LibFunc_cosh:
3526 case LibFunc_sinhf:
3527 case LibFunc_coshf:
3528 case LibFunc_sinhl:
3529 case LibFunc_coshl:
3530 // FIXME: These boundaries are slightly conservative.
3531 if (OpC->getType()->isDoubleTy())
3532 return !(Op < APFloat(-710.0) || Op > APFloat(710.0));
3533 if (OpC->getType()->isFloatTy())
3534 return !(Op < APFloat(-89.0f) || Op > APFloat(89.0f));
3535 break;
3536
3537 case LibFunc_sqrtl:
3538 case LibFunc_sqrt:
3539 case LibFunc_sqrtf:
3540 return Op.isNaN() || Op.isZero() || !Op.isNegative();
3541
3542 // FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p,
3543 // maybe others?
3544 default:
3545 break;
3546 }
3547 }
3548 }
3549
3550 if (Call->arg_size() == 2) {
3551 ConstantFP *Op0C = dyn_cast<ConstantFP>(Call->getArgOperand(0));
3552 ConstantFP *Op1C = dyn_cast<ConstantFP>(Call->getArgOperand(1));
3553 if (Op0C && Op1C) {
3554 const APFloat &Op0 = Op0C->getValueAPF();
3555 const APFloat &Op1 = Op1C->getValueAPF();
3556
3557 switch (Func) {
3558 case LibFunc_powl:
3559 case LibFunc_pow:
3560 case LibFunc_powf: {
3561 // FIXME: Stop using the host math library.
3562 // FIXME: The computation isn't done in the right precision.
3563 Type *Ty = Op0C->getType();
3564 if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) {
3565 if (Ty == Op1C->getType())
3566 return ConstantFoldBinaryFP(pow, Op0, Op1, Ty) != nullptr;
3567 }
3568 break;
3569 }
3570
3571 case LibFunc_fmodl:
3572 case LibFunc_fmod:
3573 case LibFunc_fmodf:
3574 case LibFunc_remainderl:
3575 case LibFunc_remainder:
3576 case LibFunc_remainderf:
3577 return Op0.isNaN() || Op1.isNaN() ||
3578 (!Op0.isInfinity() && !Op1.isZero());
3579
3580 case LibFunc_atan2:
3581 case LibFunc_atan2f:
3582 case LibFunc_atan2l:
3583 // Although IEEE-754 says atan2(+/-0.0, +/-0.0) are well-defined, and
3584 // GLIBC and MSVC do not appear to raise an error on those, we
3585 // cannot rely on that behavior. POSIX and C11 say that a domain error
3586 // may occur, so allow for that possibility.
3587 return !Op0.isZero() || !Op1.isZero();
3588
3589 default:
3590 break;
3591 }
3592 }
3593 }
3594
3595 return false;
3596}
3597
3598void TargetFolder::anchor() {}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const LLT S1
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static Constant * FoldBitCast(Constant *V, Type *DestTy)
Constant * getConstantAtOffset(Constant *Base, APInt Offset, const DataLayout &DL)
If this Offset points exactly to the start of an aggregate element, return that element,...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
std::string Name
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Hexagon Common GEP
amode Optimize addressing mode
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:40
Value * RHS
Value * LHS
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:988
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1069
void copySign(const APFloat &RHS)
Definition: APFloat.h:1163
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5196
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1051
bool isNegative() const
Definition: APFloat.h:1295
double convertToDouble() const
Converts this APFloat to host double value.
Definition: APFloat.cpp:5255
bool isPosInfinity() const
Definition: APFloat.h:1308
bool isNormal() const
Definition: APFloat.h:1299
bool isDenormal() const
Definition: APFloat.h:1296
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1042
const fltSemantics & getSemantics() const
Definition: APFloat.h:1303
bool isNonZero() const
Definition: APFloat.h:1304
bool isFinite() const
Definition: APFloat.h:1300
bool isNaN() const
Definition: APFloat.h:1293
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1060
bool isSignaling() const
Definition: APFloat.h:1297
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1096
bool isZero() const
Definition: APFloat.h:1291
APInt bitcastToAPInt() const
Definition: APFloat.h:1210
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1185
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1087
bool isNegInfinity() const
Definition: APFloat.h:1309
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:957
bool isInfinity() const
Definition: APFloat.h:1292
Class for arbitrary precision integers.
Definition: APInt.h:76
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1977
APInt usub_sat(const APInt &RHS) const
Definition: APInt.cpp:2061
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:401
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1491
uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
Definition: APInt.cpp:489
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1002
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:906
APInt abs() const
Get the absolute value.
Definition: APInt.h:1737
APInt sadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2032
APInt usub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1954
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1672
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1439
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:187
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1934
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1589
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1548
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:197
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1010
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2042
APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1966
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:954
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:851
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition: APInt.cpp:453
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1947
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:367
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:829
APInt ssub_sat(const APInt &RHS) const
Definition: APInt.cpp:2051
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
Definition: Any.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:174
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
const T * data() const
Definition: ArrayRef.h:162
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:195
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1461
static Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Returns the opcode necessary to cast Val into Ty using usual casting rules.
static bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy)
This method can be used to determine if a cast from SrcTy to DstTy using Opcode op is valid or not.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:960
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
Definition: Constants.h:705
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2126
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2452
static bool isDesirableCastOp(unsigned Opcode)
Whether creating a constant expression for this cast is desirable.
Definition: Constants.cpp:2261
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:2041
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2542
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2474
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2112
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1200
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2497
static bool isSupportedGetElementPtr(const Type *SrcElemTy)
Whether creating a constant expression for this getelementptr type is supported.
Definition: Constants.h:1315
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible.
Definition: Constants.cpp:2159
static bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
Definition: Constants.cpp:2207
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2140
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:2328
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:268
const APFloat & getValueAPF() const
Definition: Constants.h:311
static Constant * getZero(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1037
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:849
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:856
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1356
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1398
This is an important base class in LLVM.
Definition: Constant.h:41
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:417
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:432
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
Constrained floating point compare intrinsics.
This is the common base class for constrained floating point intrinsics.
std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
std::optional< RoundingMode > getRoundingMode() const
Wrapper for a function that represents a value that functionally represents the original function.
Definition: Constants.h:935
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
static bool compare(const APFloat &LHS, const APFloat &RHS, FCmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
Class to represent fixed width SIMD vectors.
Definition: DerivedTypes.h:539
unsigned getNumElements() const
Definition: DerivedTypes.h:582
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:692
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition: Function.cpp:740
static Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
static Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:655
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:294
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
bool isCast() const
Definition: Instruction.h:260
bool isBinaryOp() const
Definition: Instruction.h:257
const BasicBlock * getParent() const
Definition: Instruction.h:152
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:84
bool isUnaryOp() const
Definition: Instruction.h:256
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:278
static APInt getSaturationPoint(Intrinsic::ID ID, unsigned numBits)
Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values, so there is a certain thre...
ICmpInst::Predicate getPredicate() const
Returns the comparison predicate underlying the intrinsic.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.h:287
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1827
Class to represent scalable SIMD vectors.
Definition: DerivedTypes.h:586
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:426
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:622
unsigned getElementContainingOffset(uint64_t FixedOffset) const
Given a valid byte offset into the structure, returns the structure index that contains it.
Definition: DataLayout.cpp:92
TypeSize getElementOffset(unsigned Idx) const
Definition: DataLayout.h:651
Class to represent struct types.
Definition: DerivedTypes.h:216
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getIntegerBitWidth() const
Type * getStructElementType(unsigned N) const
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:234
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:255
static IntegerType * getInt1Ty(LLVMContext &C)
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:154
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition: Type.h:146
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool isX86_MMXTy() const
Return true if this is X86 MMX.
Definition: Type.h:201
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isStructTy() const
True if this is an instance of StructType.
Definition: Type.h:249
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:302
static IntegerType * getInt16Ty(LLVMContext &C)
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition: Type.h:295
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:143
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:129
static IntegerType * getInt8Ty(LLVMContext &C)
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:157
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition: Type.h:185
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:262
bool isX86_AMXTy() const
Return true if this is X86 AMX.
Definition: Type.h:204
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:228
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition: Type.h:216
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Type * getContainedType(unsigned i) const
This method is used to implement the type iterator (defined at the end of the file).
Definition: Type.h:377
bool isIEEELikeFPTy() const
Return true if this is a well-behaved IEEE-like type, which has a IEEE compatible layout as defined b...
Definition: Type.h:171
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:348
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1808
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
Value * getOperand(unsigned i) const
Definition: User.h:169
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition: Value.h:736
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
Type * getElementType() const
Definition: DerivedTypes.h:436
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:187
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition: APInt.h:2178
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition: APInt.h:2183
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition: APInt.h:2188
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition: APInt.h:2193
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:121
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
@ CE
Windows NT (Windows on ARM)
@ ebStrict
This corresponds to "fpexcept.strict".
Definition: FPEnv.h:41
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition: FPEnv.h:39
constexpr double pi
Definition: MathExtras.h:37
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329
@ Offset
Definition: DWP.cpp:456
Constant * ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS, Constant *RHS, Type *Ty, Instruction *FMFSource)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1731
Constant * ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, const DataLayout &DL)
ConstantFoldLoadThroughBitcast - try to cast constant to destination type returning null if unsuccess...
static double log2(double V)
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
unsigned getPointerAddressSpace(const Type *T)
Definition: SPIRVUtils.h:122
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1381
Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I)
Attempt to constant fold a floating point binary operation with the specified operands,...
Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL, DSOLocalEquivalent **DSOEquiv=nullptr)
If this constant is a constant offset from a global, return the global and the constant.
bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition: APFloat.h:1436
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value,...
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition: APFloat.h:1373
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 maximumNumber semantics.
Definition: APFloat.h:1410
Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, const DataLayout &DL)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
Constant * FlushFPConstant(Constant *Operand, const Instruction *I, bool IsOutput)
Attempt to flush float point constant according to denormal mode set in the instruction's parent func...
Constant * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Definition: APFloat.h:1361
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:2022
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 minimumNumber semantics.
Definition: APFloat.h:1396
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
RoundingMode
Rounding mode.
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
bool isVectorIntrinsicWithScalarOpAtArg(Intrinsic::ID ID, unsigned ScalarOpdIdx)
Identifies if the vector form of the intrinsic has a scalar operand.
Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition: APFloat.h:1423
Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:246
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
DenormalModeKind
Represent handled modes for denormal (aka subnormal) modes in the floating point environment.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ Dynamic
Denormals have unknown treatment.
@ IEEE
IEEE-754 denormal numbers preserved.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getIEEE()
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:50
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition: KnownBits.h:57