LLVM 24.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"
27#include "llvm/ADT/StringRef.h"
32#include "llvm/Config/config.h"
33#include "llvm/IR/Constant.h"
35#include "llvm/IR/Constants.h"
36#include "llvm/IR/DataLayout.h"
38#include "llvm/IR/Function.h"
39#include "llvm/IR/GlobalValue.h"
41#include "llvm/IR/InstrTypes.h"
42#include "llvm/IR/Instruction.h"
45#include "llvm/IR/Intrinsics.h"
46#include "llvm/IR/IntrinsicsAArch64.h"
47#include "llvm/IR/IntrinsicsAMDGPU.h"
48#include "llvm/IR/IntrinsicsARM.h"
49#include "llvm/IR/IntrinsicsNVPTX.h"
50#include "llvm/IR/IntrinsicsWebAssembly.h"
51#include "llvm/IR/IntrinsicsX86.h"
53#include "llvm/IR/Operator.h"
54#include "llvm/IR/Type.h"
55#include "llvm/IR/Value.h"
56#include "llvm/Support/CRC.h"
60#include <cassert>
61#include <cerrno>
62#include <cfenv>
63#include <cmath>
64#include <cstdint>
65
66using namespace llvm;
67
69 "disable-fp-call-folding",
70 cl::desc("Disable constant-folding of FP intrinsics and libcalls."),
71 cl::init(false), cl::Hidden);
72
73namespace {
74
75//===----------------------------------------------------------------------===//
76// Constant Folding internal helper functions
77//===----------------------------------------------------------------------===//
78
79static Constant *foldConstVectorToAPInt(APInt &Result, Type *DestTy,
80 Constant *C, Type *SrcEltTy,
81 unsigned NumSrcElts,
82 const DataLayout &DL) {
83 // Now that we know that the input value is a vector of integers, just shift
84 // and insert them into our result.
85 unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy);
86 for (unsigned i = 0; i != NumSrcElts; ++i) {
87 Constant *Element;
88 if (DL.isLittleEndian())
89 Element = C->getAggregateElement(NumSrcElts - i - 1);
90 else
91 Element = C->getAggregateElement(i);
92
93 if (isa_and_nonnull<UndefValue>(Element)) {
94 Result <<= BitShift;
95 continue;
96 }
97
98 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
99 if (!ElementCI)
100 return ConstantExpr::getBitCast(C, DestTy);
101
102 Result <<= BitShift;
103 Result |= ElementCI->getValue().zext(Result.getBitWidth());
104 }
105
106 return nullptr;
107}
108
109/// Check whether folding this bitcast into a byte vector would mix poison and
110/// non-poison bits in the same output lane. While integer types track poison on
111/// a per-value basis, byte types track it on a per-bit basis. However,
112/// `ConstantByte` cannot represent values with both poison and non-poison bits.
113///
114/// Source elements are grouped by the output lane they map to. Returns true if
115/// any group contains both poison and non-poison elements.
116static bool foldMixesPoisonBits(Constant *C, unsigned NumSrcElt,
117 unsigned NumDstElt) {
118 // If element counts don't divide evenly, bail out if a poison source element
119 // might span multiple destination lanes.
120 if (NumSrcElt % NumDstElt != 0)
121 return C->containsPoisonElement();
122 unsigned Ratio = NumSrcElt / NumDstElt;
123 for (unsigned i = 0; i != NumSrcElt; i += Ratio) {
124 bool HasPoison = false;
125 bool HasNonPoison = false;
126 for (unsigned j = 0; j != Ratio; ++j) {
127 Constant *Src = C->getAggregateElement(i + j);
128 // Conservatively bail out.
129 if (!Src)
130 return true;
131 if (isa<PoisonValue>(Src))
132 HasPoison = true;
133 else
134 HasNonPoison = true;
135 }
136 if (HasPoison && HasNonPoison)
137 return true;
138 }
139 return false;
140}
141
142/// Track which destination lanes of a bitcast are produced from poison bytes.
143/// A destination lane is marked if any source element mapped to it is poison.
144/// Returns false if an aggregate element cannot be inspected. The caller should
145/// bail out of folding.
146static bool computePoisonDstLanes(Constant *C, unsigned NumSrcElt,
147 unsigned NumDstElt,
148 SmallBitVector &PoisonDstElts) {
149 // If element counts don't divide evenly, bail out if a poison source element
150 // might span multiple destination lanes.
151 if ((NumDstElt < NumSrcElt ? NumSrcElt % NumDstElt : NumDstElt % NumSrcElt))
152 return !C->containsPoisonElement();
153 if (NumDstElt < NumSrcElt) {
154 unsigned Ratio = NumSrcElt / NumDstElt;
155 for (unsigned i = 0; i != NumDstElt; ++i) {
156 for (unsigned j = 0; j != Ratio; ++j) {
157 Constant *Src = C->getAggregateElement(i * Ratio + j);
158 if (!Src)
159 return false;
160 if (isa<PoisonValue>(Src)) {
161 PoisonDstElts[i] = true;
162 break;
163 }
164 }
165 }
166 } else {
167 unsigned Ratio = NumDstElt / NumSrcElt;
168 for (unsigned i = 0; i != NumSrcElt; ++i) {
169 Constant *Src = C->getAggregateElement(i);
170 if (!Src)
171 return false;
172 if (isa<PoisonValue>(Src))
173 PoisonDstElts.set(i * Ratio, (i + 1) * Ratio);
174 }
175 }
176 return true;
177}
178
179/// Constant fold bitcast, symbolically evaluating it with DataLayout.
180/// This always returns a non-null constant, but it may be a
181/// ConstantExpr if unfoldable.
182Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
183 assert(CastInst::castIsValid(Instruction::BitCast, C, DestTy) &&
184 "Invalid constantexpr bitcast!");
185
186 // Catch the obvious splat cases.
187 if (Constant *Res = ConstantFoldLoadFromUniformValue(C, DestTy, DL))
188 return Res;
189
190 if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
191 // Handle a vector->scalar integer/fp/byte cast.
192 if (isa<IntegerType>(DestTy) || DestTy->isFloatingPointTy() ||
193 DestTy->isByteTy()) {
194 unsigned NumSrcElts = cast<FixedVectorType>(VTy)->getNumElements();
195 Type *SrcEltTy = VTy->getElementType();
196
197 // A byte constant can't represent partial poison or pointer provenance.
198 if (DestTy->isByteTy() &&
199 (C->containsPoisonElement() || SrcEltTy->isPointerTy()))
200 return ConstantExpr::getBitCast(C, DestTy);
201
202 // Bitcasting a byte containing any poison bit to an integer or fp type
203 // yields poison.
204 if (SrcEltTy->isByteTy() && C->containsPoisonElement())
205 return PoisonValue::get(DestTy);
206
207 // If the vector is a vector of floating point or bytes, convert it to a
208 // vector of int to simplify things.
209 Constant *OrigC = C;
210 if (SrcEltTy->isFloatingPointTy() || SrcEltTy->isByteTy()) {
211 unsigned Width = SrcEltTy->getPrimitiveSizeInBits();
212 auto *SrcIVTy = FixedVectorType::get(
213 IntegerType::get(C->getContext(), Width), NumSrcElts);
214 // Ask IR to do the conversion now that #elts line up.
215 C = ConstantExpr::getBitCast(C, SrcIVTy);
216 }
217
218 APInt Result(DL.getTypeSizeInBits(DestTy), 0);
219 // An element that isn't a plain constant, like a pointer cast to a byte,
220 // can't be folded. Byte destinations fall back to a bitcast of the
221 // original vector, as the integer vector C would drop its provenance.
222 if (Constant *CE = foldConstVectorToAPInt(Result, DestTy, C, SrcEltTy,
223 NumSrcElts, DL))
224 return DestTy->isByteTy() ? ConstantExpr::getBitCast(OrigC, DestTy)
225 : CE;
226
227 if (isa<IntegerType>(DestTy))
228 return ConstantInt::get(DestTy, Result);
229
230 if (isa<ByteType>(DestTy))
231 return ConstantByte::get(DestTy->getContext(), Result);
232
233 APFloat FP(DestTy->getFltSemantics(), Result);
234 return ConstantFP::get(DestTy->getContext(), FP);
235 }
236 }
237
238 // The code below only handles casts to vectors currently.
239 auto *DestVTy = dyn_cast<VectorType>(DestTy);
240 if (!DestVTy)
241 return ConstantExpr::getBitCast(C, DestTy);
242
243 // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
244 // vector so the code below can handle it uniformly.
245 if (!isa<VectorType>(C->getType()) &&
247 Constant *Ops = C; // don't take the address of C!
248 return FoldBitCast(ConstantVector::get(Ops), DestTy, DL);
249 }
250
251 // Some of what follows may extend to cover scalable vectors but the current
252 // implementation is fixed length specific.
253 if (!isa<FixedVectorType>(C->getType()))
254 return ConstantExpr::getBitCast(C, DestTy);
255
256 // If this is a bitcast from constant vector -> vector, fold it.
259 return ConstantExpr::getBitCast(C, DestTy);
260
261 // If the element types match, IR can fold it.
262 unsigned NumDstElt = cast<FixedVectorType>(DestVTy)->getNumElements();
263 unsigned NumSrcElt = cast<FixedVectorType>(C->getType())->getNumElements();
264 if (NumDstElt == NumSrcElt)
265 return ConstantExpr::getBitCast(C, DestTy);
266
267 Type *SrcEltTy = cast<VectorType>(C->getType())->getElementType();
268 Type *DstEltTy = DestVTy->getElementType();
269
270 // Otherwise, we're changing the number of elements in a vector, which
271 // requires endianness information to do the right thing. For example,
272 // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
273 // folds to (little endian):
274 // <4 x i32> <i32 0, i32 0, i32 1, i32 0>
275 // and to (big endian):
276 // <4 x i32> <i32 0, i32 0, i32 0, i32 1>
277
278 // First thing is first. We only want to think about integer here, so if
279 // we have something in FP form, recast it as integer.
280 if (DstEltTy->isFloatingPointTy()) {
281 // Fold to an vector of integers with same size as our FP type.
282 unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
283 auto *DestIVTy = FixedVectorType::get(
284 IntegerType::get(C->getContext(), FPWidth), NumDstElt);
285 // Recursively handle this integer conversion, if possible.
286 C = FoldBitCast(C, DestIVTy, DL);
287
288 // Finally, IR can handle this now that #elts line up.
289 return ConstantExpr::getBitCast(C, DestTy);
290 }
291
292 // Handle byte destination type by folding through integers.
293 if (DstEltTy->isByteTy()) {
294 // When combining elements into larger byte values, bail out if the fold
295 // mixes poison and non-poison bits in the same destination element. Byte
296 // types track poison per bit, and no constant value can represent that.
297 if (NumDstElt < NumSrcElt && foldMixesPoisonBits(C, NumSrcElt, NumDstElt))
298 return ConstantExpr::getBitCast(C, DestTy);
299
300 // Fold to a vector of integers with same size as the byte type.
301 unsigned ByteWidth = DstEltTy->getPrimitiveSizeInBits();
302 auto *DestIVTy = FixedVectorType::get(
303 IntegerType::get(C->getContext(), ByteWidth), NumDstElt);
304 C = FoldBitCast(C, DestIVTy, DL);
305 return ConstantExpr::getBitCast(C, DestTy);
306 }
307
308 // Okay, we know the destination is integer, if the input is FP, convert
309 // it to integer first.
310 if (SrcEltTy->isFloatingPointTy()) {
311 unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
312 auto *SrcIVTy = FixedVectorType::get(
313 IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
314 // Ask IR to do the conversion now that #elts line up.
315 C = ConstantExpr::getBitCast(C, SrcIVTy);
316 assert((isa<ConstantVector>(C) || // FIXME: Remove ConstantVector.
318 "Constant folding cannot fail for plain fp->int bitcast!");
319 }
320
321 // Handle byte source type by folding through integers. Byte types track
322 // poison per bit, so any poison bit makes the destination lane poison.
323 // Record which destination lanes contain poison bits, before the generic
324 // fold below refines them to undef/zero, so they can be restored.
325 SmallBitVector PoisonDstElts(NumDstElt);
326 if (SrcEltTy->isByteTy()) {
327 if (!computePoisonDstLanes(C, NumSrcElt, NumDstElt, PoisonDstElts))
328 return ConstantExpr::getBitCast(C, DestTy);
329
330 unsigned ByteWidth = SrcEltTy->getPrimitiveSizeInBits();
331 auto *SrcIVTy = FixedVectorType::get(
332 IntegerType::get(C->getContext(), ByteWidth), NumSrcElt);
333 // Ask IR to do the conversion now that #elts line up.
334 C = ConstantExpr::getBitCast(C, SrcIVTy);
335 assert((isa<ConstantVector>(C) || // FIXME: Remove ConstantVector.
337 "Constant folding cannot fail for plain byte->int bitcast!");
338 }
339
340 // Now we know that the input and output vectors are both integer vectors
341 // of the same size, and that their #elements is not the same.
342 // Use data buffer for easy non-integer element ratio vectors handling,
343 // For example: <4 x i24> to <3 x i32>.
344 bool isLittleEndian = DL.isLittleEndian();
345 unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
346 unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits();
348 unsigned SrcElt = 0;
349
350 APInt Buffer(2 * std::max(SrcBitSize, DstBitSize), 0);
351 APInt UndefMask(Buffer.getBitWidth(), 0);
352 APInt PoisonMask(Buffer.getBitWidth(), 0);
353 unsigned BufferBitSize = 0;
354
355 while (Result.size() != NumDstElt) {
356 // Load SrcElts into Buffer.
357 while (BufferBitSize < DstBitSize) {
358 Constant *Element = C->getAggregateElement(SrcElt++);
359 if (!Element) // Reject constantexpr elements
360 return ConstantExpr::getBitCast(C, DestTy);
361
362 // Shift Buffer & Masks to fit next SrcElt.
363 if (!isLittleEndian) {
364 Buffer <<= SrcBitSize;
365 UndefMask <<= SrcBitSize;
366 PoisonMask <<= SrcBitSize;
367 }
368
369 APInt SrcValue;
370 unsigned BitPosition = isLittleEndian ? BufferBitSize : 0;
371 if (isa<UndefValue>(Element)) {
372 // Set masks fragments bits.
373 UndefMask.setBits(BitPosition, BitPosition + SrcBitSize);
374 if (isa<PoisonValue>(Element))
375 PoisonMask.setBits(BitPosition, BitPosition + SrcBitSize);
376 SrcValue = APInt::getZero(SrcBitSize);
377 } else {
378 auto *Src = dyn_cast<ConstantInt>(Element);
379 if (!Src)
380 return ConstantExpr::getBitCast(C, DestTy);
381 SrcValue = Src->getValue();
382 }
383
384 // Insert src element bits into Buffer on correct position.
385 Buffer.insertBits(SrcValue, BitPosition);
386 BufferBitSize += SrcBitSize;
387 }
388
389 // Create DstElts from Buffer.
390 while (BufferBitSize >= DstBitSize) {
391 unsigned ShiftAmt = isLittleEndian ? 0 : BufferBitSize - DstBitSize;
392 // Emit undef/poison, if all undef mask fragment bits are set.
393 if (UndefMask.extractBits(DstBitSize, ShiftAmt).isAllOnes()) {
394 // Push poison, if any bit in poison mask fragment is set.
395 if (!PoisonMask.extractBits(DstBitSize, ShiftAmt).isZero()) {
396 Result.push_back(PoisonValue::get(DstEltTy));
397 } else {
398 Result.push_back(UndefValue::get(DstEltTy));
399 }
400 } else {
401 // Create and push DstElt.
402 APInt Elt = Buffer.extractBits(DstBitSize, ShiftAmt);
403 Result.push_back(ConstantInt::get(DstEltTy, Elt));
404 }
405
406 // Shift unused Buffer fragment to lower bits.
407 if (isLittleEndian) {
408 Buffer.lshrInPlace(DstBitSize);
409 UndefMask.lshrInPlace(DstBitSize);
410 PoisonMask.lshrInPlace(DstBitSize);
411 }
412 BufferBitSize -= DstBitSize;
413 }
414 }
415
416 // Restore destination lanes whose source bytes contained poison bits.
417 for (unsigned I : PoisonDstElts.set_bits())
418 Result[I] = PoisonValue::get(DstEltTy);
419
420 return ConstantVector::get(Result);
421}
422
423} // end anonymous namespace
424
425/// If this constant is a constant offset from a global, return the global and
426/// the constant. Because of constantexprs, this function is recursive.
428 APInt &Offset, const DataLayout &DL,
429 DSOLocalEquivalent **DSOEquiv) {
430 if (DSOEquiv)
431 *DSOEquiv = nullptr;
432
433 // Trivial case, constant is the global.
434 if ((GV = dyn_cast<GlobalValue>(C))) {
435 unsigned BitWidth = DL.getIndexTypeSizeInBits(GV->getType());
436 Offset = APInt(BitWidth, 0);
437 return true;
438 }
439
440 if (auto *FoundDSOEquiv = dyn_cast<DSOLocalEquivalent>(C)) {
441 if (DSOEquiv)
442 *DSOEquiv = FoundDSOEquiv;
443 GV = FoundDSOEquiv->getGlobalValue();
444 unsigned BitWidth = DL.getIndexTypeSizeInBits(GV->getType());
445 Offset = APInt(BitWidth, 0);
446 return true;
447 }
448
449 // Otherwise, if this isn't a constant expr, bail out.
450 auto *CE = dyn_cast<ConstantExpr>(C);
451 if (!CE) return false;
452
453 // Look through ptr->int and ptr->ptr casts.
454 if (CE->getOpcode() == Instruction::PtrToInt ||
455 CE->getOpcode() == Instruction::PtrToAddr)
456 return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL,
457 DSOEquiv);
458
459 // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
460 auto *GEP = dyn_cast<GEPOperator>(CE);
461 if (!GEP)
462 return false;
463
464 unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
465 APInt TmpOffset(BitWidth, 0);
466
467 // If the base isn't a global+constant, we aren't either.
468 if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, DL,
469 DSOEquiv))
470 return false;
471
472 // Otherwise, add any offset that our operands provide.
473 if (!GEP->accumulateConstantOffset(DL, TmpOffset))
474 return false;
475
476 Offset = TmpOffset;
477 return true;
478}
479
481 const DataLayout &DL) {
482 do {
483 Type *SrcTy = C->getType();
484 if (SrcTy == DestTy)
485 return C;
486
487 TypeSize DestSize = DL.getTypeSizeInBits(DestTy);
488 TypeSize SrcSize = DL.getTypeSizeInBits(SrcTy);
489 if (!TypeSize::isKnownGE(SrcSize, DestSize))
490 return nullptr;
491
492 // Catch the obvious splat cases (since all-zeros can coerce non-integral
493 // pointers legally).
494 if (Constant *Res = ConstantFoldLoadFromUniformValue(C, DestTy, DL))
495 return Res;
496
497 // If the type sizes are the same and a cast is legal, just directly
498 // cast the constant.
499 // But be careful not to coerce non-integral pointers illegally.
500 if (SrcSize == DestSize &&
501 DL.isNonIntegralPointerType(SrcTy->getScalarType()) ==
502 DL.isNonIntegralPointerType(DestTy->getScalarType())) {
503 Instruction::CastOps Cast = Instruction::BitCast;
504 // If we are going from a pointer to int or vice versa, we spell the cast
505 // differently.
506 if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
507 Cast = Instruction::IntToPtr;
508 else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
509 Cast = Instruction::PtrToInt;
510
511 if (CastInst::castIsValid(Cast, C, DestTy))
512 return ConstantFoldCastOperand(Cast, C, DestTy, DL);
513 }
514
515 // If this isn't an aggregate type, there is nothing we can do to drill down
516 // and find a bitcastable constant.
517 if (!SrcTy->isAggregateType() && !SrcTy->isVectorTy())
518 return nullptr;
519
520 // We're simulating a load through a pointer that was bitcast to point to
521 // a different type, so we can try to walk down through the initial
522 // elements of an aggregate to see if some part of the aggregate is
523 // castable to implement the "load" semantic model.
524 if (SrcTy->isStructTy()) {
525 // Struct types might have leading zero-length elements like [0 x i32],
526 // which are certainly not what we are looking for, so skip them.
527 unsigned Elem = 0;
528 Constant *ElemC;
529 do {
530 ElemC = C->getAggregateElement(Elem++);
531 } while (ElemC && DL.getTypeSizeInBits(ElemC->getType()).isZero());
532 C = ElemC;
533 } else {
534 // For non-byte-sized vector elements, the first element is not
535 // necessarily located at the vector base address.
536 if (auto *VT = dyn_cast<VectorType>(SrcTy))
537 if (!DL.typeSizeEqualsStoreSize(VT->getElementType()))
538 return nullptr;
539
540 C = C->getAggregateElement(0u);
541 }
542 } while (C);
543
544 return nullptr;
545}
546
547/// Return true if \p Ty is or contains a byte type.
548static bool containsByteType(Type *Ty) {
549 if (auto *ATy = dyn_cast<ArrayType>(Ty))
550 return containsByteType(ATy->getElementType());
551 if (auto *STy = dyn_cast<StructType>(Ty))
552 return any_of(STy->elements(), containsByteType);
553 return Ty->isByteOrByteVectorTy();
554}
555
556namespace {
557
558/// Recursive helper to read bits out of global. C is the constant being copied
559/// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy
560/// results into and BytesLeft is the number of bytes left in
561/// the CurPtr buffer. DL is the DataLayout. When IsByteLoad is true, do not
562/// unwrap inttoptr constant expressions. The caller would reconstruct those
563/// bits as a ConstantByte, dropping the pointer's provenance.
564bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr,
565 unsigned BytesLeft, const DataLayout &DL,
566 bool IsByteLoad = false) {
567 assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) &&
568 "Out of range access");
569
570 // Reading type padding, return zero.
571 if (ByteOffset >= DL.getTypeStoreSize(C->getType()))
572 return true;
573
574 // If this element is zero or undefined, we can just return since *CurPtr is
575 // zero initialized.
577 return true;
578
579 auto *CI = dyn_cast<ConstantInt>(C);
580 if (CI && CI->getType()->isIntegerTy()) {
581 if ((CI->getBitWidth() & 7) != 0)
582 return false;
583 const APInt &Val = CI->getValue();
584 unsigned IntBytes = unsigned(CI->getBitWidth()/8);
585
586 for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
587 unsigned n = ByteOffset;
588 if (!DL.isLittleEndian())
589 n = IntBytes - n - 1;
590 CurPtr[i] = Val.extractBits(8, n * 8).getZExtValue();
591 ++ByteOffset;
592 }
593 return true;
594 }
595
596 // Read a `ConstantByte` as an integer with the same bits.
597 auto *CB = dyn_cast<ConstantByte>(C);
598 if (CB && CB->getType()->isByteTy())
599 return ReadDataFromGlobal(ConstantInt::get(C->getContext(), CB->getValue()),
600 ByteOffset, CurPtr, BytesLeft, DL, IsByteLoad);
601
602 auto *CFP = dyn_cast<ConstantFP>(C);
603 if (CFP && CFP->getType()->isFloatingPointTy()) {
604 if (CFP->getType()->isDoubleTy()) {
605 C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL);
606 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL,
607 IsByteLoad);
608 }
609 if (CFP->getType()->isFloatTy()){
610 C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), DL);
611 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL,
612 IsByteLoad);
613 }
614 if (CFP->getType()->isHalfTy()){
615 C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), DL);
616 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL,
617 IsByteLoad);
618 }
619 return false;
620 }
621
622 if (auto *CS = dyn_cast<ConstantStruct>(C)) {
623 const StructLayout *SL = DL.getStructLayout(CS->getType());
624 unsigned Index = SL->getElementContainingOffset(ByteOffset);
625 uint64_t CurEltOffset = SL->getElementOffset(Index);
626 ByteOffset -= CurEltOffset;
627
628 while (true) {
629 // If the element access is to the element itself and not to tail padding,
630 // read the bytes from the element.
631 uint64_t EltSize = DL.getTypeAllocSize(CS->getOperand(Index)->getType());
632
633 if (ByteOffset < EltSize &&
634 !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
635 BytesLeft, DL, IsByteLoad))
636 return false;
637
638 ++Index;
639
640 // Check to see if we read from the last struct element, if so we're done.
641 if (Index == CS->getType()->getNumElements())
642 return true;
643
644 // If we read all of the bytes we needed from this element we're done.
645 uint64_t NextEltOffset = SL->getElementOffset(Index);
646
647 if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
648 return true;
649
650 // Move to the next element of the struct.
651 CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
652 BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
653 ByteOffset = 0;
654 CurEltOffset = NextEltOffset;
655 }
656 // not reached.
657 }
658
662 uint64_t NumElts, EltSize;
663 Type *EltTy;
664 if (auto *AT = dyn_cast<ArrayType>(C->getType())) {
665 NumElts = AT->getNumElements();
666 EltTy = AT->getElementType();
667 EltSize = DL.getTypeAllocSize(EltTy);
668 } else {
669 NumElts = cast<FixedVectorType>(C->getType())->getNumElements();
670 EltTy = cast<FixedVectorType>(C->getType())->getElementType();
671 // TODO: For non-byte-sized vectors, current implementation assumes there is
672 // padding to the next byte boundary between elements.
673 if (!DL.typeSizeEqualsStoreSize(EltTy))
674 return false;
675
676 EltSize = DL.getTypeStoreSize(EltTy);
677 }
678 uint64_t Index = ByteOffset / EltSize;
679 uint64_t Offset = ByteOffset - Index * EltSize;
680
681 for (; Index != NumElts; ++Index) {
682 if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
683 BytesLeft, DL, IsByteLoad))
684 return false;
685
686 uint64_t BytesWritten = EltSize - Offset;
687 assert(BytesWritten <= EltSize && "Not indexing into this element?");
688 if (BytesWritten >= BytesLeft)
689 return true;
690
691 Offset = 0;
692 BytesLeft -= BytesWritten;
693 CurPtr += BytesWritten;
694 }
695 return true;
696 }
697
698 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
699 if (CE->getOpcode() == Instruction::IntToPtr &&
700 CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) {
701 // Folding byte loads through the integer operand would rebuild the result
702 // as a `ConstantByte`, dropping the pointer's provenance.
703 if (IsByteLoad)
704 return false;
705 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
706 BytesLeft, DL, IsByteLoad);
707 }
708 }
709
710 // Otherwise, unknown initializer type.
711 return false;
712}
713
714/// OrigLoadTy is the original type being loaded, while LoadTy is the type
715/// currently being folded (which may be integer type mapped from OrigLoadTy).
716Constant *FoldReinterpretLoadFromConst(Constant *C, Type *LoadTy,
717 Type *OrigLoadTy, int64_t Offset,
718 const DataLayout &DL) {
719 // Bail out early. Not expect to load from scalable global variable.
720 if (isa<ScalableVectorType>(LoadTy))
721 return nullptr;
722
723 auto *IntType = dyn_cast<IntegerType>(LoadTy);
724
725 // If this isn't an integer load we can't fold it directly.
726 if (!IntType) {
727 // If this is a non-integer load, we can try folding it as an int load and
728 // then bitcast the result. This can be useful for union cases. Note
729 // that address spaces don't matter here since we're not going to result in
730 // an actual new load.
731 if (!LoadTy->isFloatingPointTy() && !LoadTy->isPointerTy() &&
732 !LoadTy->isByteTy() && !LoadTy->isVectorTy())
733 return nullptr;
734
735 // Reinterpret pointers read from `ConstantByte` as a bitcast, rather than
736 // an inttoptr.
737 if (LoadTy->isPtrOrPtrVectorTy() &&
738 !DL.isNonIntegralPointerType(LoadTy->getScalarType()) &&
739 containsByteType(C->getType())) {
740 Type *ByteTy = DL.getBytePtrType(LoadTy);
741 if (Constant *Res =
742 FoldReinterpretLoadFromConst(C, ByteTy, ByteTy, Offset, DL))
743 return ConstantFoldCastOperand(Instruction::BitCast, Res, LoadTy, DL);
744 }
745
746 Type *MapTy = Type::getIntNTy(C->getContext(),
747 DL.getTypeSizeInBits(LoadTy).getFixedValue());
748 if (Constant *Res =
749 FoldReinterpretLoadFromConst(C, MapTy, OrigLoadTy, Offset, DL)) {
750 if (Res->isNullValue() && !LoadTy->isX86_AMXTy())
751 // Materializing a zero can be done trivially without a bitcast
752 return Constant::getNullValue(LoadTy);
753 Type *CastTy = LoadTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(LoadTy) : LoadTy;
754 Res = FoldBitCast(Res, CastTy, DL);
755 if (LoadTy->isPtrOrPtrVectorTy()) {
756 // For vector of pointer, we needed to first convert to a vector of integer, then do vector inttoptr
757 if (Res->isNullValue() && !LoadTy->isX86_AMXTy())
758 return Constant::getNullValue(LoadTy);
759 if (DL.isNonIntegralPointerType(LoadTy->getScalarType()))
760 // Be careful not to replace a load of an addrspace value with an inttoptr here
761 return nullptr;
762 Res = ConstantExpr::getIntToPtr(Res, LoadTy);
763 }
764 return Res;
765 }
766 return nullptr;
767 }
768
769 unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
770 // Allow folding of large type loads (e.g. <16 x double>).
771 if (BytesLoaded > 128 || BytesLoaded == 0)
772 return nullptr;
773
774 // For scalar integer load, use smaller limit to avoid regression during
775 // memcmp expansion. Codegen may generate inefficient string operations.
776 if (BytesLoaded > 32 && OrigLoadTy->isIntegerTy())
777 return nullptr;
778
779 // If we're not accessing anything in this constant, the result is undefined.
780 if (Offset <= -1 * static_cast<int64_t>(BytesLoaded))
781 return PoisonValue::get(IntType);
782
783 // TODO: We should be able to support scalable types.
784 TypeSize InitializerSize = DL.getTypeAllocSize(C->getType());
785 if (InitializerSize.isScalable())
786 return nullptr;
787
788 // If we're not accessing anything in this constant, the result is undefined.
789 if (Offset >= (int64_t)InitializerSize.getFixedValue())
790 return PoisonValue::get(IntType);
791
792 SmallVector<unsigned char, 64> RawBytes(BytesLoaded);
793 unsigned char *CurPtr = RawBytes.data();
794 unsigned BytesLeft = BytesLoaded;
795
796 // If we're loading off the beginning of the global, some bytes may be valid.
797 if (Offset < 0) {
798 CurPtr += -Offset;
799 BytesLeft += Offset;
800 Offset = 0;
801 }
802
803 if (!ReadDataFromGlobal(C, Offset, CurPtr, BytesLeft, DL,
804 /*IsByteLoad=*/OrigLoadTy->isByteOrByteVectorTy()))
805 return nullptr;
806
807 APInt ResultVal = APInt(IntType->getBitWidth(), 0);
808 if (DL.isLittleEndian()) {
809 ResultVal = RawBytes[BytesLoaded - 1];
810 for (unsigned i = 1; i != BytesLoaded; ++i) {
811 ResultVal <<= 8;
812 ResultVal |= RawBytes[BytesLoaded - 1 - i];
813 }
814 } else {
815 ResultVal = RawBytes[0];
816 for (unsigned i = 1; i != BytesLoaded; ++i) {
817 ResultVal <<= 8;
818 ResultVal |= RawBytes[i];
819 }
820 }
821
822 return ConstantInt::get(IntType->getContext(), ResultVal);
823}
824
825} // anonymous namespace
826
827// If GV is a constant with an initializer read its representation starting
828// at Offset and return it as a constant array of unsigned char. Otherwise
829// return null.
831 uint64_t Offset) {
832 if (!GV->isConstant() || !GV->hasDefinitiveInitializer())
833 return nullptr;
834
835 const DataLayout &DL = GV->getDataLayout();
836 Constant *Init = const_cast<Constant *>(GV->getInitializer());
837 TypeSize InitSize = DL.getTypeAllocSize(Init->getType());
838 if (InitSize < Offset)
839 return nullptr;
840
841 uint64_t NBytes = InitSize - Offset;
842 if (NBytes > UINT16_MAX)
843 // Bail for large initializers in excess of 64K to avoid allocating
844 // too much memory.
845 // Offset is assumed to be less than or equal than InitSize (this
846 // is enforced in ReadDataFromGlobal).
847 return nullptr;
848
849 SmallVector<unsigned char, 256> RawBytes(static_cast<size_t>(NBytes));
850 unsigned char *CurPtr = RawBytes.data();
851
852 if (!ReadDataFromGlobal(Init, Offset, CurPtr, NBytes, DL))
853 return nullptr;
854
855 return ConstantDataArray::get(GV->getContext(), RawBytes);
856}
857
858/// If this Offset points exactly to the start of an aggregate element, return
859/// that element, otherwise return nullptr.
861 const DataLayout &DL) {
862 if (Offset.isZero())
863 return Base;
864
866 return nullptr;
867
868 Type *ElemTy = Base->getType();
869 SmallVector<APInt> Indices = DL.getGEPIndicesForOffset(ElemTy, Offset);
870 if (!Offset.isZero() || !Indices[0].isZero())
871 return nullptr;
872
873 Constant *C = Base;
874 for (const APInt &Index : drop_begin(Indices)) {
875 if (Index.isNegative() || Index.getActiveBits() >= 32)
876 return nullptr;
877
878 C = C->getAggregateElement(Index.getZExtValue());
879 if (!C)
880 return nullptr;
881 }
882
883 return C;
884}
885
887 const APInt &Offset,
888 const DataLayout &DL) {
889 if (Constant *AtOffset = getConstantAtOffset(C, Offset, DL))
890 if (Constant *Result = ConstantFoldLoadThroughBitcast(AtOffset, Ty, DL))
891 return Result;
892
893 // Explicitly check for out-of-bounds access, so we return poison even if the
894 // constant is a uniform value.
895 TypeSize Size = DL.getTypeAllocSize(C->getType());
896 if (!Size.isScalable() && Offset.sge(Size.getFixedValue()))
897 return PoisonValue::get(Ty);
898
899 // Try an offset-independent fold of a uniform value.
900 if (Constant *Result = ConstantFoldLoadFromUniformValue(C, Ty, DL))
901 return Result;
902
903 // Try hard to fold loads from bitcasted strange and non-type-safe things.
904 if (Offset.getSignificantBits() <= 64)
905 if (Constant *Result =
906 FoldReinterpretLoadFromConst(C, Ty, Ty, Offset.getSExtValue(), DL))
907 return Result;
908
909 return nullptr;
910}
911
916
919 const DataLayout &DL) {
920 // We can only fold loads from constant globals with a definitive initializer.
921 // Check this upfront, to skip expensive offset calculations.
923 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
924 return nullptr;
925
926 C = cast<Constant>(C->stripAndAccumulateConstantOffsets(
927 DL, Offset, /* AllowNonInbounds */ true));
928
929 if (C == GV)
930 if (Constant *Result = ConstantFoldLoadFromConst(GV->getInitializer(), Ty,
931 Offset, DL))
932 return Result;
933
934 // If this load comes from anywhere in a uniform constant global, the value
935 // is always the same, regardless of the loaded offset.
936 return ConstantFoldLoadFromUniformValue(GV->getInitializer(), Ty, DL);
937}
938
940 const DataLayout &DL) {
941 APInt Offset(DL.getIndexTypeSizeInBits(C->getType()), 0);
942 return ConstantFoldLoadFromConstPtr(C, Ty, std::move(Offset), DL);
943}
944
946 const DataLayout &DL) {
947 if (isa<PoisonValue>(C))
948 return PoisonValue::get(Ty);
949 if (isa<UndefValue>(C))
950 return UndefValue::get(Ty);
951 // If padding is needed when storing C to memory, then it isn't considered as
952 // uniform.
953 if (!DL.typeSizeEqualsStoreSize(C->getType()))
954 return nullptr;
955 if (C->isNullValue() && !Ty->isX86_AMXTy())
956 return Constant::getNullValue(Ty);
957 if (C->isAllOnesValue() &&
958 (Ty->isIntOrIntVectorTy() || Ty->isByteOrByteVectorTy() ||
959 Ty->isFPOrFPVectorTy()))
960 return Constant::getAllOnesValue(Ty);
961 return nullptr;
962}
963
964namespace {
965
966/// One of Op0/Op1 is a constant expression.
967/// Attempt to symbolically evaluate the result of a binary operator merging
968/// these together. If target data info is available, it is provided as DL,
969/// otherwise DL is null.
970Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1,
971 const DataLayout &DL) {
972 // SROA
973
974 // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
975 // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
976 // bits.
977
978 if (Opc == Instruction::And) {
979 KnownBits Known0 = computeKnownBits(Op0, DL);
980 KnownBits Known1 = computeKnownBits(Op1, DL);
981 if ((Known1.One | Known0.Zero).isAllOnes()) {
982 // All the bits of Op0 that the 'and' could be masking are already zero.
983 return Op0;
984 }
985 if ((Known0.One | Known1.Zero).isAllOnes()) {
986 // All the bits of Op1 that the 'and' could be masking are already zero.
987 return Op1;
988 }
989
990 Known0 &= Known1;
991 if (Known0.isConstant())
992 return ConstantInt::get(Op0->getType(), Known0.getConstant());
993 }
994
995 // If the constant expr is something like &A[123] - &A[4].f, fold this into a
996 // constant. This happens frequently when iterating over a global array.
997 if (Opc == Instruction::Sub) {
998 GlobalValue *GV1, *GV2;
999 APInt Offs1, Offs2;
1000
1001 if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, DL))
1002 if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, DL) && GV1 == GV2) {
1003 unsigned OpSize = DL.getTypeSizeInBits(Op0->getType());
1004
1005 // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
1006 // PtrToInt may change the bitwidth so we have convert to the right size
1007 // first.
1008 return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
1009 Offs2.zextOrTrunc(OpSize));
1010 }
1011 }
1012
1013 return nullptr;
1014}
1015
1016/// If array indices are not pointer-sized integers, explicitly cast them so
1017/// that they aren't implicitly casted by the getelementptr.
1018Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
1019 Type *ResultTy, GEPNoWrapFlags NW,
1020 std::optional<ConstantRange> InRange,
1021 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1022 Type *IntIdxTy = DL.getIndexType(ResultTy);
1023 Type *IntIdxScalarTy = IntIdxTy->getScalarType();
1024
1025 bool Any = false;
1027 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
1028 if ((i == 1 ||
1030 SrcElemTy, Ops.slice(1, i - 1)))) &&
1031 Ops[i]->getType()->getScalarType() != IntIdxScalarTy) {
1032 Any = true;
1033 Type *NewType =
1034 Ops[i]->getType()->isVectorTy() ? IntIdxTy : IntIdxScalarTy;
1036 CastInst::getCastOpcode(Ops[i], true, NewType, true), Ops[i], NewType,
1037 DL);
1038 if (!NewIdx)
1039 return nullptr;
1040 NewIdxs.push_back(NewIdx);
1041 } else
1042 NewIdxs.push_back(Ops[i]);
1043 }
1044
1045 if (!Any)
1046 return nullptr;
1047
1048 Constant *C = ConstantExpr::getGetElementPtr(DL, SrcElemTy, Ops[0], NewIdxs,
1049 NW, InRange);
1050 if (!C)
1051 return nullptr;
1052 return ConstantFoldConstant(C, DL, TLI);
1053}
1054
1055/// If we can symbolically evaluate the GEP constant expression, do so.
1056Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
1058 const DataLayout &DL,
1059 const TargetLibraryInfo *TLI) {
1060 Type *SrcElemTy = GEP->getSourceElementType();
1061 Type *ResTy = GEP->getType();
1062 if (!SrcElemTy->isSized() || isa<ScalableVectorType>(SrcElemTy))
1063 return nullptr;
1064
1065 if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy, GEP->getNoWrapFlags(),
1066 GEP->getInRange(), DL, TLI))
1067 return C;
1068
1069 Constant *Ptr = Ops[0];
1070 if (!Ptr->getType()->isPointerTy())
1071 return nullptr;
1072
1073 Type *IntIdxTy = DL.getIndexType(Ptr->getType());
1074
1075 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1076 if (!isa<ConstantInt>(Ops[i]) || !Ops[i]->getType()->isIntegerTy())
1077 return nullptr;
1078
1079 unsigned BitWidth = DL.getTypeSizeInBits(IntIdxTy);
1080 APInt Offset(BitWidth, 0);
1082 reinterpret_cast<const Value *const *>(Ops.data()) + 1, Ops.size() - 1);
1083 if (!GEPOperator::accumulateConstantOffset(SrcElemTy, Indices, DL, Offset))
1084 return nullptr;
1085
1086 std::optional<ConstantRange> InRange = GEP->getInRange();
1087 if (InRange)
1088 InRange = InRange->sextOrTrunc(BitWidth);
1089
1090 // If this is a GEP of a GEP, fold it all into a single GEP.
1091 GEPNoWrapFlags NW = GEP->getNoWrapFlags();
1092 bool Overflow = false;
1093 while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
1094 NW &= GEP->getNoWrapFlags();
1095
1096 // Do not try to incorporate the sub-GEP if some index is not a number.
1097 APInt NestedOffset(BitWidth, 0);
1098 if (!GEP->accumulateConstantOffset(DL, NestedOffset))
1099 break;
1100
1101 // Adjust inrange offset and intersect inrange attributes
1102 if (auto GEPRange = GEP->getInRange()) {
1103 auto AdjustedGEPRange = GEPRange->sextOrTrunc(BitWidth).subtract(Offset);
1104 InRange =
1105 InRange ? InRange->intersectWith(AdjustedGEPRange) : AdjustedGEPRange;
1106 }
1107
1108 Ptr = cast<Constant>(GEP->getOperand(0));
1109 SrcElemTy = GEP->getSourceElementType();
1110 Offset = Offset.sadd_ov(NestedOffset, Overflow);
1111 }
1112
1113 // Preserving nusw (without inbounds) also requires that the offset
1114 // additions did not overflow.
1115 if (NW.hasNoUnsignedSignedWrap() && !NW.isInBounds() && Overflow)
1117
1118 // If the base value for this address is a literal integer value, fold the
1119 // getelementptr to the resulting integer value casted to the pointer type.
1120 APInt BaseIntVal(DL.getPointerTypeSizeInBits(Ptr->getType()), 0);
1121 if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) {
1122 if (CE->getOpcode() == Instruction::IntToPtr) {
1123 if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
1124 BaseIntVal = Base->getValue().zextOrTrunc(BaseIntVal.getBitWidth());
1125 }
1126 }
1127
1128 if ((Ptr->isNullValue() || BaseIntVal != 0) &&
1129 !DL.mustNotIntroduceIntToPtr(Ptr->getType())) {
1130
1131 // If the index size is smaller than the pointer size, add to the low
1132 // bits only.
1133 BaseIntVal.insertBits(BaseIntVal.trunc(BitWidth) + Offset, 0);
1134 Constant *C = ConstantInt::get(Ptr->getContext(), BaseIntVal);
1135 return ConstantExpr::getIntToPtr(C, ResTy);
1136 }
1137
1138 // Try to infer inbounds for GEPs of globals.
1139 if (!NW.isInBounds() && Offset.isNonNegative()) {
1140 bool CanBeNull;
1141 uint64_t DerefBytes = Ptr->getPointerDereferenceableBytes(
1142 DL, CanBeNull, /*CanBeFreed=*/nullptr);
1143 if (DerefBytes != 0 && !CanBeNull && Offset.sle(DerefBytes))
1145 }
1146
1147 // nusw + nneg -> nuw
1148 if (NW.hasNoUnsignedSignedWrap() && Offset.isNonNegative())
1150
1151 // Otherwise canonicalize this to a single ptradd.
1152 LLVMContext &Ctx = Ptr->getContext();
1153 return ConstantExpr::getPtrAdd(Ptr, ConstantInt::get(Ctx, Offset), NW,
1154 InRange);
1155}
1156
1157/// Attempt to constant fold an instruction with the
1158/// specified opcode and operands. If successful, the constant result is
1159/// returned, if not, null is returned. Note that this function can fail when
1160/// attempting to fold instructions like loads and stores, which have no
1161/// constant expression form.
1162Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
1164 const DataLayout &DL,
1165 const TargetLibraryInfo *TLI,
1166 bool AllowNonDeterministic) {
1167 Type *DestTy = InstOrCE->getType();
1168
1169 if (Instruction::isUnaryOp(Opcode))
1170 return ConstantFoldUnaryOpOperand(Opcode, Ops[0], DL);
1171
1172 if (Instruction::isBinaryOp(Opcode)) {
1173 switch (Opcode) {
1174 default:
1175 break;
1176 case Instruction::FAdd:
1177 case Instruction::FSub:
1178 case Instruction::FMul:
1179 case Instruction::FDiv:
1180 case Instruction::FRem:
1181 // Handle floating point instructions separately to account for denormals
1182 // TODO: If a constant expression is being folded rather than an
1183 // instruction, denormals will not be flushed/treated as zero
1184 if (const auto *I = dyn_cast<Instruction>(InstOrCE)) {
1185 return ConstantFoldFPInstOperands(Opcode, Ops[0], Ops[1], DL, I,
1186 AllowNonDeterministic);
1187 }
1188 }
1189 return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL);
1190 }
1191
1192 if (Instruction::isCast(Opcode))
1193 return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL);
1194
1195 if (auto *GEP = dyn_cast<GEPOperator>(InstOrCE)) {
1196 Type *SrcElemTy = GEP->getSourceElementType();
1198 return nullptr;
1199
1200 if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI))
1201 return C;
1202
1203 return ConstantExpr::getGetElementPtr(DL, SrcElemTy, Ops[0], Ops.slice(1),
1204 GEP->getNoWrapFlags(),
1205 GEP->getInRange());
1206 }
1207
1208 if (auto *CE = dyn_cast<ConstantExpr>(InstOrCE))
1209 return CE->getWithOperands(Ops);
1210
1211 switch (Opcode) {
1212 default: return nullptr;
1213 case Instruction::ICmp:
1214 case Instruction::FCmp: {
1215 auto *C = cast<CmpInst>(InstOrCE);
1216 return ConstantFoldCompareInstOperands(C->getPredicate(), Ops[0], Ops[1],
1217 DL, TLI, C->getFunction());
1218 }
1219 case Instruction::Freeze:
1220 return isGuaranteedNotToBeUndefOrPoison(Ops[0]) ? Ops[0] : nullptr;
1221 case Instruction::Call:
1222 if (auto *F = dyn_cast<Function>(Ops.back())) {
1223 const auto *Call = cast<CallBase>(InstOrCE);
1225 return ConstantFoldCall(Call, F, Ops.slice(0, Ops.size() - 1), TLI,
1226 AllowNonDeterministic);
1227 }
1228 return nullptr;
1229 case Instruction::Select:
1230 return ConstantFoldSelectInstruction(Ops[0], Ops[1], Ops[2]);
1231 case Instruction::ExtractElement:
1233 case Instruction::ExtractValue:
1235 Ops[0], cast<ExtractValueInst>(InstOrCE)->getIndices());
1236 case Instruction::InsertElement:
1237 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1238 case Instruction::InsertValue:
1240 Ops[0], Ops[1], cast<InsertValueInst>(InstOrCE)->getIndices());
1241 case Instruction::ShuffleVector:
1243 Ops[0], Ops[1], cast<ShuffleVectorInst>(InstOrCE)->getShuffleMask());
1244 case Instruction::BitInsert:
1245 return ConstantFoldBitInsertOperands(Ops[0], Ops[1], Ops[2], DL);
1246 case Instruction::BitExtract:
1247 return ConstantFoldBitExtractOperands(InstOrCE->getType(), Ops[0], Ops[1],
1248 DL);
1249 case Instruction::Load: {
1250 const auto *LI = dyn_cast<LoadInst>(InstOrCE);
1251 if (LI->isVolatile())
1252 return nullptr;
1253 return ConstantFoldLoadFromConstPtr(Ops[0], LI->getType(), DL);
1254 }
1255 }
1256}
1257
1258} // end anonymous namespace
1259
1260//===----------------------------------------------------------------------===//
1261// Constant Folding public APIs
1262//===----------------------------------------------------------------------===//
1263
1264namespace {
1265
1266Constant *
1267ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL,
1268 const TargetLibraryInfo *TLI,
1271 return const_cast<Constant *>(C);
1272
1274 for (const Use &OldU : C->operands()) {
1275 Constant *OldC = cast<Constant>(&OldU);
1276 Constant *NewC = OldC;
1277 // Recursively fold the ConstantExpr's operands. If we have already folded
1278 // a ConstantExpr, we don't have to process it again.
1279 if (isa<ConstantVector>(OldC) || isa<ConstantExpr>(OldC)) {
1280 auto It = FoldedOps.find(OldC);
1281 if (It == FoldedOps.end()) {
1282 NewC = ConstantFoldConstantImpl(OldC, DL, TLI, FoldedOps);
1283 FoldedOps.insert({OldC, NewC});
1284 } else {
1285 NewC = It->second;
1286 }
1287 }
1288 Ops.push_back(NewC);
1289 }
1290
1291 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1292 if (Constant *Res = ConstantFoldInstOperandsImpl(
1293 CE, CE->getOpcode(), Ops, DL, TLI, /*AllowNonDeterministic=*/true))
1294 return Res;
1295 return const_cast<Constant *>(C);
1296 }
1297
1299 return ConstantVector::get(Ops);
1300}
1301
1302} // end anonymous namespace
1303
1305 const DataLayout &DL,
1306 const TargetLibraryInfo *TLI) {
1307 // Handle PHI nodes quickly here...
1308 if (auto *PN = dyn_cast<PHINode>(I)) {
1309 Constant *CommonValue = nullptr;
1310
1312 for (Value *Incoming : PN->incoming_values()) {
1313 // If the incoming value is undef then skip it. Note that while we could
1314 // skip the value if it is equal to the phi node itself we choose not to
1315 // because that would break the rule that constant folding only applies if
1316 // all operands are constants.
1317 if (isa<UndefValue>(Incoming))
1318 continue;
1319 // If the incoming value is not a constant, then give up.
1320 auto *C = dyn_cast<Constant>(Incoming);
1321 if (!C)
1322 return nullptr;
1323 // Fold the PHI's operands.
1324 C = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1325 // If the incoming value is a different constant to
1326 // the one we saw previously, then give up.
1327 if (CommonValue && C != CommonValue)
1328 return nullptr;
1329 CommonValue = C;
1330 }
1331
1332 // If we reach here, all incoming values are the same constant or undef.
1333 return CommonValue ? CommonValue : UndefValue::get(PN->getType());
1334 }
1335
1336 // Scan the operand list, checking to see if they are all constants, if so,
1337 // hand off to ConstantFoldInstOperandsImpl.
1338 if (!all_of(I->operands(), [](const Use &U) { return isa<Constant>(U); }))
1339 return nullptr;
1340
1343 for (const Use &OpU : I->operands()) {
1344 auto *Op = cast<Constant>(&OpU);
1345 // Fold the Instruction's operands.
1346 Op = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps);
1347 Ops.push_back(Op);
1348 }
1349
1350 return ConstantFoldInstOperands(I, Ops, DL, TLI);
1351}
1352
1354 const TargetLibraryInfo *TLI) {
1356 return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1357}
1358
1361 const DataLayout &DL,
1362 const TargetLibraryInfo *TLI,
1363 bool AllowNonDeterministic) {
1364 return ConstantFoldInstOperandsImpl(I, I->getOpcode(), Ops, DL, TLI,
1365 AllowNonDeterministic);
1366}
1367
1369 Constant *Ops0, Constant *Ops1,
1370 const DataLayout &DL,
1371 const TargetLibraryInfo *TLI,
1372 const Function *CtxF) {
1373 CmpInst::Predicate Predicate = (CmpInst::Predicate)IntPredicate;
1374 // fold: icmp (inttoptr x), null -> icmp x, 0
1375 // fold: icmp null, (inttoptr x) -> icmp 0, x
1376 // fold: icmp (ptrtoint x), 0 -> icmp x, null
1377 // fold: icmp 0, (ptrtoint x) -> icmp null, x
1378 // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
1379 // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
1380 //
1381 // FIXME: The following comment is out of data and the DataLayout is here now.
1382 // ConstantExpr::getCompare cannot do this, because it doesn't have DL
1383 // around to know if bit truncation is happening.
1384 if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
1385 if (Ops1->isNullValue()) {
1386 if (CE0->getOpcode() == Instruction::IntToPtr) {
1387 Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1388 // Convert the integer value to the right size to ensure we get the
1389 // proper extension or truncation.
1390 if (Constant *C = ConstantFoldIntegerCast(CE0->getOperand(0), IntPtrTy,
1391 /*IsSigned*/ false, DL)) {
1392 Constant *Null = Constant::getNullValue(C->getType());
1393 return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1394 }
1395 }
1396
1397 // icmp only compares the address part of the pointer, so only do this
1398 // transform if the integer size matches the address size.
1399 if (CE0->getOpcode() == Instruction::PtrToInt ||
1400 CE0->getOpcode() == Instruction::PtrToAddr) {
1401 Type *AddrTy = DL.getAddressType(CE0->getOperand(0)->getType());
1402 if (CE0->getType() == AddrTy) {
1403 Constant *C = CE0->getOperand(0);
1404 Constant *Null = Constant::getNullValue(C->getType());
1405 return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1406 }
1407 }
1408 }
1409
1410 if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
1411 if (CE0->getOpcode() == CE1->getOpcode()) {
1412 if (CE0->getOpcode() == Instruction::IntToPtr) {
1413 Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1414
1415 // Convert the integer value to the right size to ensure we get the
1416 // proper extension or truncation.
1417 Constant *C0 = ConstantFoldIntegerCast(CE0->getOperand(0), IntPtrTy,
1418 /*IsSigned*/ false, DL);
1419 Constant *C1 = ConstantFoldIntegerCast(CE1->getOperand(0), IntPtrTy,
1420 /*IsSigned*/ false, DL);
1421 if (C0 && C1)
1422 return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
1423 }
1424
1425 // icmp only compares the address part of the pointer, so only do this
1426 // transform if the integer size matches the address size.
1427 if (CE0->getOpcode() == Instruction::PtrToInt ||
1428 CE0->getOpcode() == Instruction::PtrToAddr) {
1429 Type *AddrTy = DL.getAddressType(CE0->getOperand(0)->getType());
1430 if (CE0->getType() == AddrTy &&
1431 CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1433 Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI);
1434 }
1435 }
1436 }
1437 }
1438
1439 // Convert pointer comparison (base+offset1) pred (base+offset2) into
1440 // offset1 pred offset2, for the case where the offset is inbounds. This
1441 // only works for equality and unsigned comparison, as inbounds permits
1442 // crossing the sign boundary. However, the offset comparison itself is
1443 // signed.
1444 if (Ops0->getType()->isPointerTy() && !ICmpInst::isSigned(Predicate)) {
1445 unsigned IndexWidth = DL.getIndexTypeSizeInBits(Ops0->getType());
1446 APInt Offset0(IndexWidth, 0);
1447 bool IsEqPred = ICmpInst::isEquality(Predicate);
1448 Value *Stripped0 = Ops0->stripAndAccumulateConstantOffsets(
1449 DL, Offset0, /*AllowNonInbounds=*/IsEqPred,
1450 /*AllowInvariantGroup=*/false, /*ExternalAnalysis=*/nullptr,
1451 /*LookThroughIntToPtr=*/IsEqPred);
1452 APInt Offset1(IndexWidth, 0);
1453 Value *Stripped1 = Ops1->stripAndAccumulateConstantOffsets(
1454 DL, Offset1, /*AllowNonInbounds=*/IsEqPred,
1455 /*AllowInvariantGroup=*/false, /*ExternalAnalysis=*/nullptr,
1456 /*LookThroughIntToPtr=*/IsEqPred);
1457 if (Stripped0 == Stripped1)
1458 return ConstantInt::getBool(
1459 Ops0->getContext(),
1460 ICmpInst::compare(Offset0, Offset1,
1461 ICmpInst::getSignedPredicate(Predicate)));
1462 }
1463 } else if (isa<ConstantExpr>(Ops1)) {
1464 // If RHS is a constant expression, but the left side isn't, swap the
1465 // operands and try again.
1466 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1467 return ConstantFoldCompareInstOperands(Predicate, Ops1, Ops0, DL, TLI);
1468 }
1469
1470 if (CmpInst::isFPPredicate(Predicate)) {
1471 // Flush any denormal constant float input according to denormal handling
1472 // mode.
1473 Ops0 = FlushFPConstant(Ops0, CtxF, /*IsOutput=*/false);
1474 if (!Ops0)
1475 return nullptr;
1476 Ops1 = FlushFPConstant(Ops1, CtxF, /*IsOutput=*/false);
1477 if (!Ops1)
1478 return nullptr;
1479 }
1480
1481 return ConstantFoldCompareInstruction(Predicate, Ops0, Ops1);
1482}
1483
1485 const DataLayout &DL) {
1487
1488 return ConstantFoldUnaryInstruction(Opcode, Op);
1489}
1490
1492 Constant *RHS,
1493 const DataLayout &DL) {
1495 if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS))
1496 if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL))
1497 return C;
1498
1500 return ConstantExpr::get(Opcode, LHS, RHS);
1501 return ConstantFoldBinaryInstruction(Opcode, LHS, RHS);
1502}
1503
1506 switch (Mode) {
1508 return nullptr;
1509 case DenormalMode::IEEE:
1510 return ConstantFP::get(Ty, APF);
1512 return ConstantFP::get(
1513 Ty, APFloat::getZero(APF.getSemantics(), APF.isNegative()));
1515 return ConstantFP::get(Ty, APFloat::getZero(APF.getSemantics(), false));
1516 default:
1517 break;
1518 }
1519
1520 llvm_unreachable("unknown denormal mode");
1521}
1522
1523/// Return the denormal mode that can be assumed when executing a floating point
1524/// operation at \p CtxI.
1526 if (!CtxF)
1527 return DenormalMode::getDynamic();
1528 return CtxF->getDenormalMode(Ty->getScalarType()->getFltSemantics());
1529}
1530
1531static ConstantFP *
1532flushDenormalConstantFP(ConstantFP *CFP, const Function *CtxF, bool IsOutput) {
1533 const APFloat &APF = CFP->getValueAPF();
1534 if (!APF.isDenormal())
1535 return CFP;
1536
1538 return flushDenormalConstant(CFP->getType(), APF,
1539 IsOutput ? Mode.Output : Mode.Input);
1540}
1541
1543 bool IsOutput) {
1544 if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operand))
1545 return flushDenormalConstantFP(CFP, CtxF, IsOutput);
1546
1548 return Operand;
1549
1550 Type *Ty = Operand->getType();
1551 VectorType *VecTy = dyn_cast<VectorType>(Ty);
1552 if (VecTy) {
1553 if (auto *Splat = dyn_cast_or_null<ConstantFP>(Operand->getSplatValue())) {
1554 ConstantFP *Folded = flushDenormalConstantFP(Splat, CtxF, IsOutput);
1555 if (!Folded)
1556 return nullptr;
1558 }
1559
1560 Ty = VecTy->getElementType();
1561 }
1562
1563 if (isa<ConstantExpr>(Operand))
1564 return Operand;
1565
1566 if (const auto *CV = dyn_cast<ConstantVector>(Operand)) {
1568 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1569 Constant *Element = CV->getAggregateElement(i);
1570 if (isa<UndefValue>(Element)) {
1571 NewElts.push_back(Element);
1572 continue;
1573 }
1574
1575 ConstantFP *CFP = dyn_cast<ConstantFP>(Element);
1576 if (!CFP)
1577 return nullptr;
1578
1579 ConstantFP *Folded = flushDenormalConstantFP(CFP, CtxF, IsOutput);
1580 if (!Folded)
1581 return nullptr;
1582 NewElts.push_back(Folded);
1583 }
1584
1585 return ConstantVector::get(NewElts);
1586 }
1587
1588 if (const auto *CDV = dyn_cast<ConstantDataVector>(Operand)) {
1590 for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I) {
1591 const APFloat &Elt = CDV->getElementAsAPFloat(I);
1592 if (!Elt.isDenormal()) {
1593 NewElts.push_back(ConstantFP::get(Ty, Elt));
1594 } else {
1595 DenormalMode Mode = getInstrDenormalMode(CtxF, Ty);
1597 flushDenormalConstant(Ty, Elt, IsOutput ? Mode.Output : Mode.Input);
1598 if (!Folded)
1599 return nullptr;
1600 NewElts.push_back(Folded);
1601 }
1602 }
1603
1604 return ConstantVector::get(NewElts);
1605 }
1606
1607 return nullptr;
1608}
1609
1611 Constant *RHS, const DataLayout &DL,
1612 const Instruction *I,
1613 bool AllowNonDeterministic) {
1614 if (Instruction::isBinaryOp(Opcode)) {
1615 // Flush denormal inputs if needed.
1616 Constant *Op0 =
1617 FlushFPConstant(LHS, I->getFunction(), /* IsOutput */ false);
1618 if (!Op0)
1619 return nullptr;
1620 Constant *Op1 =
1621 FlushFPConstant(RHS, I->getFunction(), /* IsOutput */ false);
1622 if (!Op1)
1623 return nullptr;
1624
1625 // If nsz or an algebraic FMF flag is set, the result of the FP operation
1626 // may change due to future optimization. Don't constant fold them if
1627 // non-deterministic results are not allowed.
1628 if (!AllowNonDeterministic)
1630 if (FP->hasNoSignedZeros() || FP->hasAllowReassoc() ||
1631 FP->hasAllowContract() || FP->hasAllowReciprocal())
1632 return nullptr;
1633
1634 // Calculate constant result.
1635 Constant *C = ConstantFoldBinaryOpOperands(Opcode, Op0, Op1, DL);
1636 if (!C)
1637 return nullptr;
1638
1639 // Flush denormal output if needed.
1640 C = FlushFPConstant(C, I->getFunction(), /* IsOutput */ true);
1641 if (!C)
1642 return nullptr;
1643
1644 // The precise NaN value is non-deterministic.
1645 if (!AllowNonDeterministic && C->isNaN())
1646 return nullptr;
1647
1648 return C;
1649 }
1650 // If instruction lacks a parent/function and the denormal mode cannot be
1651 // determined, use the default (IEEE).
1652 return ConstantFoldBinaryOpOperands(Opcode, LHS, RHS, DL);
1653}
1654
1656 Type *DestTy, const DataLayout &DL) {
1657 assert(Instruction::isCast(Opcode));
1658
1659 if (auto *CE = dyn_cast<ConstantExpr>(C))
1660 if (CE->isCast())
1661 if (unsigned NewOp = CastInst::isEliminableCastPair(
1662 Instruction::CastOps(CE->getOpcode()),
1663 Instruction::CastOps(Opcode), CE->getOperand(0)->getType(),
1664 C->getType(), DestTy, &DL))
1665 return ConstantFoldCastOperand(NewOp, CE->getOperand(0), DestTy, DL);
1666
1667 switch (Opcode) {
1668 default:
1669 llvm_unreachable("Missing case");
1670 case Instruction::PtrToAddr:
1671 case Instruction::PtrToInt:
1672 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1673 Constant *FoldedValue = nullptr;
1674 // If the input is an inttoptr, eliminate the pair. This requires knowing
1675 // the width of a pointer, so it can't be done in ConstantExpr::getCast.
1676 if (CE->getOpcode() == Instruction::IntToPtr) {
1677 // zext/trunc the inttoptr to pointer size. ptrtoaddr is truncated to
1678 // the address size below.
1679 FoldedValue = ConstantFoldIntegerCast(CE->getOperand(0),
1680 DL.getIntPtrType(CE->getType()),
1681 /*IsSigned=*/false, DL);
1682 } else if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
1683 // If we have GEP, we can perform the following folds:
1684 // (ptrtoint/ptrtoaddr (gep null, x)) -> x
1685 // (ptrtoint/ptrtoaddr (gep (gep null, x), y) -> x + y, etc.
1686 unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
1687 APInt BaseOffset(BitWidth, 0);
1688 auto *Base = cast<Constant>(GEP->stripAndAccumulateConstantOffsets(
1689 DL, BaseOffset, /*AllowNonInbounds=*/true));
1690 if (Base->isNullValue()) {
1691 FoldedValue = ConstantInt::get(CE->getContext(), BaseOffset);
1692 } else {
1693 // ptrtoint/ptrtoaddr (gep i8, Ptr, (sub 0, V))
1694 // -> sub (ptrtoint/ptrtoaddr Ptr), V
1695 if (GEP->getNumIndices() == 1 &&
1696 GEP->getSourceElementType()->isIntegerTy(8)) {
1697 auto *Ptr = cast<Constant>(GEP->getPointerOperand());
1698 auto *Sub = dyn_cast<ConstantExpr>(GEP->getOperand(1));
1699 Type *IntIdxTy = DL.getIndexType(Ptr->getType());
1700 if (Sub && Sub->getType() == IntIdxTy &&
1701 Sub->getOpcode() == Instruction::Sub &&
1702 Sub->getOperand(0)->isNullValue())
1703 FoldedValue = ConstantExpr::getSub(
1704 ConstantExpr::getCast(Opcode, Ptr, IntIdxTy),
1705 Sub->getOperand(1));
1706 }
1707 }
1708 }
1709 if (FoldedValue) {
1710 // Do a zext or trunc to get to the ptrtoint/ptrtoaddr dest size.
1711 return ConstantFoldIntegerCast(FoldedValue, DestTy, /*IsSigned=*/false,
1712 DL);
1713 }
1714 }
1715 break;
1716 case Instruction::IntToPtr:
1717 case Instruction::Trunc:
1718 case Instruction::ZExt:
1719 case Instruction::SExt:
1720 case Instruction::FPTrunc:
1721 case Instruction::FPExt:
1722 case Instruction::UIToFP:
1723 case Instruction::SIToFP:
1724 case Instruction::FPToUI:
1725 case Instruction::FPToSI:
1726 case Instruction::AddrSpaceCast:
1727 break;
1728 case Instruction::BitCast:
1729 return FoldBitCast(C, DestTy, DL);
1730 }
1731
1733 return ConstantExpr::getCast(Opcode, C, DestTy);
1734 return ConstantFoldCastInstruction(Opcode, C, DestTy);
1735}
1736
1738 bool IsSigned, const DataLayout &DL) {
1739 Type *SrcTy = C->getType();
1740 if (SrcTy == DestTy)
1741 return C;
1742 if (SrcTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1743 return ConstantFoldCastOperand(Instruction::Trunc, C, DestTy, DL);
1744 if (IsSigned)
1745 return ConstantFoldCastOperand(Instruction::SExt, C, DestTy, DL);
1746 return ConstantFoldCastOperand(Instruction::ZExt, C, DestTy, DL);
1747}
1748
1751 const DataLayout &DL) {
1752 // bitinsert C, C, undef -> poison
1754 return PoisonValue::get(Base->getType());
1755
1756 // bitinsert poison, poison, C -> poison
1758 return Base;
1759
1760 if (auto *VTy = dyn_cast<VectorType>(Base->getType())) {
1761 // Fast path for splatted constants.
1762 Constant *BaseSplat = Base->getSplatValue();
1763 Constant *ValSplat = Val->getSplatValue();
1764 Constant *OffsetSplat = Offset->getSplatValue();
1765 if (BaseSplat && ValSplat && OffsetSplat) {
1766 Constant *Res =
1767 ConstantFoldBitInsertOperands(BaseSplat, ValSplat, OffsetSplat, DL);
1768 if (!Res)
1769 return nullptr;
1770 return ConstantVector::getSplat(VTy->getElementCount(), Res);
1771 }
1772
1773 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
1774 if (!FVTy)
1775 return nullptr;
1776
1777 // Fold each element and create a vector constant from those constants.
1779 for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
1780 Constant *BaseElt = Base->getAggregateElement(I);
1781 Constant *ValElt = Val->getAggregateElement(I);
1782 Constant *OffsetElt = Offset->getAggregateElement(I);
1783 if (!BaseElt || !ValElt || !OffsetElt)
1784 return nullptr;
1785 Constant *Res =
1786 ConstantFoldBitInsertOperands(BaseElt, ValElt, OffsetElt, DL);
1787 if (!Res)
1788 return nullptr;
1789 Result.push_back(Res);
1790 }
1791
1792 return ConstantVector::get(Result);
1793 }
1794
1795 auto *COffset = dyn_cast<ConstantInt>(Offset);
1796 if (!COffset)
1797 return nullptr;
1798
1799 // bitinsert C, C, out_of_range -> poison
1800 unsigned BaseBits = DL.getTypeSizeInBits(Base->getType());
1801 unsigned Bits = DL.getTypeSizeInBits(Val->getType());
1802 uint64_t Off = COffset->getZExtValue();
1803 if (Off + Bits > BaseBits)
1804 return PoisonValue::get(Base->getType());
1805
1806 // Overwriting every bit of the base is a bitcast of the value.
1807 if (Bits == BaseBits)
1808 return ConstantFoldCastOperand(Instruction::BitCast, Val, Base->getType(),
1809 DL);
1810
1811 // bitinsert undef, undef, C -> undef
1812 // Any poison bits are refined to undef.
1814 return UndefValue::get(Base->getType());
1815
1816 // A byte constant can't mix poison or undef bits with other bits.
1817 auto *CB = dyn_cast<ConstantByte>(Base);
1818 // The value bits are unknown.
1819 if (!CB || isa<ConstantExpr>(Val) || Val->getType()->isPointerTy())
1820 return nullptr;
1821
1823 ConstantFoldCastOperand(Instruction::BitCast, Val,
1824 IntegerType::get(Base->getContext(), Bits), DL));
1825 if (!CI)
1826 return nullptr;
1827
1828 APInt Res = CB->getValue();
1829 Res.insertBits(CI->getValue(), Off);
1830 return ConstantByte::get(Base->getType(), Res);
1831}
1832
1835 const DataLayout &DL) {
1836 // bitextract poison, C -> poison
1837 // bitextract C, undef -> poison
1839 return PoisonValue::get(Ty);
1840
1841 if (auto *VTy = dyn_cast<VectorType>(Ty)) {
1842 Type *EltTy = VTy->getElementType();
1843 // Fast path for splatted constants.
1844 Constant *SrcSplat = Src->getSplatValue();
1845 Constant *OffsetSplat = Offset->getSplatValue();
1846 if (SrcSplat && OffsetSplat) {
1847 Constant *Res =
1848 ConstantFoldBitExtractOperands(EltTy, SrcSplat, OffsetSplat, DL);
1849 if (!Res)
1850 return nullptr;
1851 return ConstantVector::getSplat(VTy->getElementCount(), Res);
1852 }
1853
1854 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
1855 if (!FVTy)
1856 return nullptr;
1857
1858 // Fold each element and create a vector constant from those constants.
1860 for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
1861 Constant *SrcElt = Src->getAggregateElement(I);
1862 Constant *OffsetElt = Offset->getAggregateElement(I);
1863 if (!SrcElt || !OffsetElt)
1864 return nullptr;
1865 Constant *Res =
1866 ConstantFoldBitExtractOperands(EltTy, SrcElt, OffsetElt, DL);
1867 if (!Res)
1868 return nullptr;
1869 Result.push_back(Res);
1870 }
1871
1872 return ConstantVector::get(Result);
1873 }
1874
1875 auto *COffset = dyn_cast<ConstantInt>(Offset);
1876 if (!COffset)
1877 return nullptr;
1878
1879 // bitextract C, out_of_range -> poison
1880 unsigned Bits = DL.getTypeSizeInBits(Ty);
1881 uint64_t Off = COffset->getZExtValue();
1882 if (Off + Bits > DL.getTypeSizeInBits(Src->getType()))
1883 return PoisonValue::get(Ty);
1884
1885 // bitextract undef, C -> undef
1886 if (isa<UndefValue>(Src))
1887 return UndefValue::get(Ty);
1888
1889 auto *CB = dyn_cast<ConstantByte>(Src);
1890 // An integer can't be bitcast to a pointer.
1891 if (!CB || Ty->isPointerTy())
1892 return nullptr;
1893
1894 APInt Res = CB->getValue().extractBits(Bits, Off);
1896 Instruction::BitCast, ConstantInt::get(Src->getContext(), Res), Ty, DL);
1897}
1898
1899//===----------------------------------------------------------------------===//
1900// Constant Folding for Calls
1901//
1902
1903/// Returns true if the intrinsic can be constant folded, given \p IsStrictFP.
1904static bool canConstantFoldIntrinsic(Intrinsic::ID ID, bool IsStrictFP) {
1905 switch (ID) {
1906 // Operations that do not operate floating-point numbers and do not depend on
1907 // FP environment can be folded even in strictfp functions.
1908 case Intrinsic::bswap:
1909 case Intrinsic::ctpop:
1910 case Intrinsic::ctlz:
1911 case Intrinsic::cttz:
1912 case Intrinsic::fshl:
1913 case Intrinsic::fshr:
1914 case Intrinsic::clmul:
1915 case Intrinsic::pdep:
1916 case Intrinsic::pext:
1917 case Intrinsic::launder_invariant_group:
1918 case Intrinsic::masked_load:
1919 case Intrinsic::get_active_lane_mask:
1920 case Intrinsic::mask_beforefirst:
1921 case Intrinsic::abs:
1922 case Intrinsic::smax:
1923 case Intrinsic::smin:
1924 case Intrinsic::umax:
1925 case Intrinsic::umin:
1926 case Intrinsic::scmp:
1927 case Intrinsic::ucmp:
1928 case Intrinsic::sadd_with_overflow:
1929 case Intrinsic::uadd_with_overflow:
1930 case Intrinsic::ssub_with_overflow:
1931 case Intrinsic::usub_with_overflow:
1932 case Intrinsic::smul_with_overflow:
1933 case Intrinsic::umul_with_overflow:
1934 case Intrinsic::smulh:
1935 case Intrinsic::umulh:
1936 case Intrinsic::sadd_sat:
1937 case Intrinsic::uadd_sat:
1938 case Intrinsic::ssub_sat:
1939 case Intrinsic::usub_sat:
1940 case Intrinsic::sshl_sat:
1941 case Intrinsic::ushl_sat:
1942 case Intrinsic::smul_fix:
1943 case Intrinsic::smul_fix_sat:
1944 case Intrinsic::bitreverse:
1945 case Intrinsic::is_constant:
1946 case Intrinsic::vector_reduce_add:
1947 case Intrinsic::vector_reduce_mul:
1948 case Intrinsic::vector_reduce_and:
1949 case Intrinsic::vector_reduce_or:
1950 case Intrinsic::vector_reduce_xor:
1951 case Intrinsic::vector_reduce_smin:
1952 case Intrinsic::vector_reduce_smax:
1953 case Intrinsic::vector_reduce_umin:
1954 case Intrinsic::vector_reduce_umax:
1955 case Intrinsic::vector_partial_reduce_add:
1956 case Intrinsic::vector_extract:
1957 case Intrinsic::vector_insert:
1958 case Intrinsic::vector_interleave2:
1959 case Intrinsic::vector_interleave3:
1960 case Intrinsic::vector_interleave4:
1961 case Intrinsic::vector_interleave5:
1962 case Intrinsic::vector_interleave6:
1963 case Intrinsic::vector_interleave7:
1964 case Intrinsic::vector_interleave8:
1965 case Intrinsic::vector_deinterleave2:
1966 case Intrinsic::vector_deinterleave3:
1967 case Intrinsic::vector_deinterleave4:
1968 case Intrinsic::vector_deinterleave5:
1969 case Intrinsic::vector_deinterleave6:
1970 case Intrinsic::vector_deinterleave7:
1971 case Intrinsic::vector_deinterleave8:
1972 // Target intrinsics
1973 case Intrinsic::amdgcn_perm:
1974 case Intrinsic::amdgcn_wave_reduce_umin:
1975 case Intrinsic::amdgcn_wave_reduce_umax:
1976 case Intrinsic::amdgcn_wave_reduce_max:
1977 case Intrinsic::amdgcn_wave_reduce_min:
1978 case Intrinsic::amdgcn_wave_reduce_and:
1979 case Intrinsic::amdgcn_wave_reduce_or:
1980 case Intrinsic::amdgcn_wave_reduce_xor:
1981 case Intrinsic::amdgcn_wave_reduce_add:
1982 case Intrinsic::amdgcn_wave_reduce_sub:
1983 case Intrinsic::amdgcn_s_wqm:
1984 case Intrinsic::amdgcn_s_quadmask:
1985 case Intrinsic::amdgcn_s_bitreplicate:
1986 case Intrinsic::arm_crc32b:
1987 case Intrinsic::arm_crc32h:
1988 case Intrinsic::arm_crc32w:
1989 case Intrinsic::arm_crc32cb:
1990 case Intrinsic::arm_crc32ch:
1991 case Intrinsic::arm_crc32cw:
1992 case Intrinsic::arm_mve_vctp8:
1993 case Intrinsic::arm_mve_vctp16:
1994 case Intrinsic::arm_mve_vctp32:
1995 case Intrinsic::arm_mve_vctp64:
1996 case Intrinsic::aarch64_crc32b:
1997 case Intrinsic::aarch64_crc32h:
1998 case Intrinsic::aarch64_crc32w:
1999 case Intrinsic::aarch64_crc32x:
2000 case Intrinsic::aarch64_crc32cb:
2001 case Intrinsic::aarch64_crc32ch:
2002 case Intrinsic::aarch64_crc32cw:
2003 case Intrinsic::aarch64_crc32cx:
2004 case Intrinsic::aarch64_sve_convert_from_svbool:
2005 case Intrinsic::wasm_alltrue:
2006 case Intrinsic::wasm_anytrue:
2007 case Intrinsic::wasm_dot:
2008 // WebAssembly float semantics are always known
2009 case Intrinsic::wasm_trunc_signed:
2010 case Intrinsic::wasm_trunc_unsigned:
2011 case Intrinsic::x86_sse42_crc32_32_8:
2012 case Intrinsic::x86_sse42_crc32_32_16:
2013 case Intrinsic::x86_sse42_crc32_32_32:
2014 case Intrinsic::x86_sse42_crc32_64_64:
2015 return true;
2016
2017 // Floating point operations cannot be folded in strictfp functions in
2018 // general case. They can be folded if FP environment is known to compiler.
2019 case Intrinsic::minnum:
2020 case Intrinsic::maxnum:
2021 case Intrinsic::minimum:
2022 case Intrinsic::maximum:
2023 case Intrinsic::minimumnum:
2024 case Intrinsic::maximumnum:
2025 case Intrinsic::log:
2026 case Intrinsic::log2:
2027 case Intrinsic::log10:
2028 case Intrinsic::exp:
2029 case Intrinsic::exp2:
2030 case Intrinsic::exp10:
2031 case Intrinsic::sqrt:
2032 case Intrinsic::sin:
2033 case Intrinsic::cos:
2034 case Intrinsic::sincos:
2035 case Intrinsic::sinh:
2036 case Intrinsic::cosh:
2037 case Intrinsic::atan:
2038 case Intrinsic::pow:
2039 case Intrinsic::powi:
2040 case Intrinsic::ldexp:
2041 case Intrinsic::fma:
2042 case Intrinsic::fmuladd:
2043 case Intrinsic::frexp:
2044 case Intrinsic::fptoui_sat:
2045 case Intrinsic::fptosi_sat:
2046 case Intrinsic::amdgcn_cos:
2047 case Intrinsic::amdgcn_cubeid:
2048 case Intrinsic::amdgcn_cubema:
2049 case Intrinsic::amdgcn_cubesc:
2050 case Intrinsic::amdgcn_cubetc:
2051 case Intrinsic::amdgcn_fmul_legacy:
2052 case Intrinsic::amdgcn_fma_legacy:
2053 case Intrinsic::amdgcn_fract:
2054 case Intrinsic::amdgcn_sin:
2055 // The intrinsics below depend on rounding mode in MXCSR.
2056 case Intrinsic::x86_sse_cvtss2si:
2057 case Intrinsic::x86_sse_cvtss2si64:
2058 case Intrinsic::x86_sse_cvttss2si:
2059 case Intrinsic::x86_sse_cvttss2si64:
2060 case Intrinsic::x86_sse2_cvtsd2si:
2061 case Intrinsic::x86_sse2_cvtsd2si64:
2062 case Intrinsic::x86_sse2_cvttsd2si:
2063 case Intrinsic::x86_sse2_cvttsd2si64:
2064 case Intrinsic::x86_avx512_vcvtss2si32:
2065 case Intrinsic::x86_avx512_vcvtss2si64:
2066 case Intrinsic::x86_avx512_cvttss2si:
2067 case Intrinsic::x86_avx512_cvttss2si64:
2068 case Intrinsic::x86_avx512_vcvtsd2si32:
2069 case Intrinsic::x86_avx512_vcvtsd2si64:
2070 case Intrinsic::x86_avx512_cvttsd2si:
2071 case Intrinsic::x86_avx512_cvttsd2si64:
2072 case Intrinsic::x86_avx512_vcvtss2usi32:
2073 case Intrinsic::x86_avx512_vcvtss2usi64:
2074 case Intrinsic::x86_avx512_cvttss2usi:
2075 case Intrinsic::x86_avx512_cvttss2usi64:
2076 case Intrinsic::x86_avx512_vcvtsd2usi32:
2077 case Intrinsic::x86_avx512_vcvtsd2usi64:
2078 case Intrinsic::x86_avx512_cvttsd2usi:
2079 case Intrinsic::x86_avx512_cvttsd2usi64:
2080
2081 // NVVM FMax intrinsics
2082 case Intrinsic::nvvm_fmax_d:
2083 case Intrinsic::nvvm_fmax_f:
2084 case Intrinsic::nvvm_fmax_ftz_f:
2085 case Intrinsic::nvvm_fmax_ftz_nan_f:
2086 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
2087 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
2088 case Intrinsic::nvvm_fmax_nan_f:
2089 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
2090 case Intrinsic::nvvm_fmax_xorsign_abs_f:
2091
2092 // NVVM FMin intrinsics
2093 case Intrinsic::nvvm_fmin_d:
2094 case Intrinsic::nvvm_fmin_f:
2095 case Intrinsic::nvvm_fmin_ftz_f:
2096 case Intrinsic::nvvm_fmin_ftz_nan_f:
2097 case Intrinsic::nvvm_fmin_ftz_nan_xorsign_abs_f:
2098 case Intrinsic::nvvm_fmin_ftz_xorsign_abs_f:
2099 case Intrinsic::nvvm_fmin_nan_f:
2100 case Intrinsic::nvvm_fmin_nan_xorsign_abs_f:
2101 case Intrinsic::nvvm_fmin_xorsign_abs_f:
2102
2103 // NVVM float/double to int32/uint32 conversion intrinsics
2104 case Intrinsic::nvvm_f2i_rm:
2105 case Intrinsic::nvvm_f2i_rn:
2106 case Intrinsic::nvvm_f2i_rp:
2107 case Intrinsic::nvvm_f2i_rz:
2108 case Intrinsic::nvvm_f2i_rm_ftz:
2109 case Intrinsic::nvvm_f2i_rn_ftz:
2110 case Intrinsic::nvvm_f2i_rp_ftz:
2111 case Intrinsic::nvvm_f2i_rz_ftz:
2112 case Intrinsic::nvvm_f2ui_rm:
2113 case Intrinsic::nvvm_f2ui_rn:
2114 case Intrinsic::nvvm_f2ui_rp:
2115 case Intrinsic::nvvm_f2ui_rz:
2116 case Intrinsic::nvvm_f2ui_rm_ftz:
2117 case Intrinsic::nvvm_f2ui_rn_ftz:
2118 case Intrinsic::nvvm_f2ui_rp_ftz:
2119 case Intrinsic::nvvm_f2ui_rz_ftz:
2120 case Intrinsic::nvvm_d2i_rm:
2121 case Intrinsic::nvvm_d2i_rn:
2122 case Intrinsic::nvvm_d2i_rp:
2123 case Intrinsic::nvvm_d2i_rz:
2124 case Intrinsic::nvvm_d2ui_rm:
2125 case Intrinsic::nvvm_d2ui_rn:
2126 case Intrinsic::nvvm_d2ui_rp:
2127 case Intrinsic::nvvm_d2ui_rz:
2128
2129 // NVVM float/double to int64/uint64 conversion intrinsics
2130 case Intrinsic::nvvm_f2ll_rm:
2131 case Intrinsic::nvvm_f2ll_rn:
2132 case Intrinsic::nvvm_f2ll_rp:
2133 case Intrinsic::nvvm_f2ll_rz:
2134 case Intrinsic::nvvm_f2ll_rm_ftz:
2135 case Intrinsic::nvvm_f2ll_rn_ftz:
2136 case Intrinsic::nvvm_f2ll_rp_ftz:
2137 case Intrinsic::nvvm_f2ll_rz_ftz:
2138 case Intrinsic::nvvm_f2ull_rm:
2139 case Intrinsic::nvvm_f2ull_rn:
2140 case Intrinsic::nvvm_f2ull_rp:
2141 case Intrinsic::nvvm_f2ull_rz:
2142 case Intrinsic::nvvm_f2ull_rm_ftz:
2143 case Intrinsic::nvvm_f2ull_rn_ftz:
2144 case Intrinsic::nvvm_f2ull_rp_ftz:
2145 case Intrinsic::nvvm_f2ull_rz_ftz:
2146 case Intrinsic::nvvm_d2ll_rm:
2147 case Intrinsic::nvvm_d2ll_rn:
2148 case Intrinsic::nvvm_d2ll_rp:
2149 case Intrinsic::nvvm_d2ll_rz:
2150 case Intrinsic::nvvm_d2ull_rm:
2151 case Intrinsic::nvvm_d2ull_rn:
2152 case Intrinsic::nvvm_d2ull_rp:
2153 case Intrinsic::nvvm_d2ull_rz:
2154
2155 // NVVM math intrinsics:
2156 case Intrinsic::nvvm_ceil_d:
2157 case Intrinsic::nvvm_ceil_f:
2158 case Intrinsic::nvvm_ceil_ftz_f:
2159
2160 case Intrinsic::nvvm_fabs:
2161 case Intrinsic::nvvm_fabs_ftz:
2162
2163 case Intrinsic::nvvm_floor_d:
2164 case Intrinsic::nvvm_floor_f:
2165 case Intrinsic::nvvm_floor_ftz_f:
2166
2167 case Intrinsic::nvvm_rcp_rm_d:
2168 case Intrinsic::nvvm_rcp_rm_f:
2169 case Intrinsic::nvvm_rcp_rm_ftz_f:
2170 case Intrinsic::nvvm_rcp_rn_d:
2171 case Intrinsic::nvvm_rcp_rn_f:
2172 case Intrinsic::nvvm_rcp_rn_ftz_f:
2173 case Intrinsic::nvvm_rcp_rp_d:
2174 case Intrinsic::nvvm_rcp_rp_f:
2175 case Intrinsic::nvvm_rcp_rp_ftz_f:
2176 case Intrinsic::nvvm_rcp_rz_d:
2177 case Intrinsic::nvvm_rcp_rz_f:
2178 case Intrinsic::nvvm_rcp_rz_ftz_f:
2179
2180 case Intrinsic::nvvm_round_d:
2181 case Intrinsic::nvvm_round_f:
2182 case Intrinsic::nvvm_round_ftz_f:
2183
2184 case Intrinsic::nvvm_saturate_d:
2185 case Intrinsic::nvvm_saturate_f:
2186 case Intrinsic::nvvm_saturate_ftz_f:
2187
2188 case Intrinsic::nvvm_sqrt_f:
2189 case Intrinsic::nvvm_sqrt_rn_d:
2190 case Intrinsic::nvvm_sqrt_rn_f:
2191 case Intrinsic::nvvm_sqrt_rn_ftz_f:
2192 return !IsStrictFP;
2193
2194 // NVVM fadd/fmul/fma intrinsics with explicit rounding modes
2195 case Intrinsic::nvvm_fadd:
2196 case Intrinsic::nvvm_fadd_ftz:
2197 case Intrinsic::nvvm_fma:
2198 case Intrinsic::nvvm_fma_ftz:
2199 case Intrinsic::nvvm_fmul:
2200 case Intrinsic::nvvm_fmul_ftz:
2201
2202 // NVVM div intrinsics with explicit rounding modes
2203 case Intrinsic::nvvm_div_rm_d:
2204 case Intrinsic::nvvm_div_rn_d:
2205 case Intrinsic::nvvm_div_rp_d:
2206 case Intrinsic::nvvm_div_rz_d:
2207 case Intrinsic::nvvm_div_rm_f:
2208 case Intrinsic::nvvm_div_rn_f:
2209 case Intrinsic::nvvm_div_rp_f:
2210 case Intrinsic::nvvm_div_rz_f:
2211 case Intrinsic::nvvm_div_rm_ftz_f:
2212 case Intrinsic::nvvm_div_rn_ftz_f:
2213 case Intrinsic::nvvm_div_rp_ftz_f:
2214 case Intrinsic::nvvm_div_rz_ftz_f:
2215
2216 // Sign operations are actually bitwise operations, they do not raise
2217 // exceptions even for SNANs.
2218 case Intrinsic::fabs:
2219 case Intrinsic::copysign:
2220 case Intrinsic::is_fpclass:
2221 // Non-constrained variants of rounding operations means default FP
2222 // environment, they can be folded in any case.
2223 case Intrinsic::ceil:
2224 case Intrinsic::floor:
2225 case Intrinsic::round:
2226 case Intrinsic::roundeven:
2227 case Intrinsic::trunc:
2228 case Intrinsic::nearbyint:
2229 case Intrinsic::rint:
2230 case Intrinsic::canonicalize:
2231
2232 // Constrained intrinsics can be folded if FP environment is known
2233 // to compiler.
2234 case Intrinsic::experimental_constrained_fma:
2235 case Intrinsic::experimental_constrained_fmuladd:
2236 case Intrinsic::experimental_constrained_fadd:
2237 case Intrinsic::experimental_constrained_fsub:
2238 case Intrinsic::experimental_constrained_fmul:
2239 case Intrinsic::experimental_constrained_fdiv:
2240 case Intrinsic::experimental_constrained_frem:
2241 case Intrinsic::experimental_constrained_fpext:
2242 case Intrinsic::experimental_constrained_ceil:
2243 case Intrinsic::experimental_constrained_floor:
2244 case Intrinsic::experimental_constrained_round:
2245 case Intrinsic::experimental_constrained_roundeven:
2246 case Intrinsic::experimental_constrained_trunc:
2247 case Intrinsic::experimental_constrained_nearbyint:
2248 case Intrinsic::experimental_constrained_rint:
2249 case Intrinsic::experimental_constrained_fcmp:
2250 case Intrinsic::experimental_constrained_fcmps:
2251
2252 case Intrinsic::experimental_cttz_elts:
2253 return true;
2254 default:
2255 return false;
2256 }
2257}
2258
2259/// Given a function's return type and its operands, determine if any of them of
2260/// of floating-point type.
2262 return RetTy->isFloatingPointTy() || any_of(Ops, [](Value *V) {
2263 return V->getType()->isFloatingPointTy();
2264 });
2265}
2266
2268 const TargetLibraryInfo *TLI) {
2269 if (Call->isNoBuiltin())
2270 return false;
2271 if (Call->getFunctionType() != F->getFunctionType())
2272 return false;
2273
2274 // Allow FP calls (both libcalls and intrinsics) to avoid being folded.
2275 // This can be useful for GPU targets or in cross-compilation scenarios
2276 // when the exact target FP behaviour is required, and the host compiler's
2277 // behaviour may be slightly different from the device's run-time behaviour.
2280 F->getReturnType(),
2281 ArrayRef<Value *>((Value *const *)(F->arg_begin()), F->arg_size())))
2282 return false;
2283
2284 if (F->getIntrinsicID() != Intrinsic::not_intrinsic)
2285 return canConstantFoldIntrinsic(F->getIntrinsicID(), Call->isStrictFP());
2286
2287 if (!TLI || Call->isStrictFP())
2288 return false;
2289
2290 LibFunc Func = TLI->getLibFunc(*F);
2291 if (Func == NotLibFunc)
2292 return false;
2293
2294 switch (Func) {
2295 case LibFunc_acos:
2296 case LibFunc_acosf:
2297 case LibFunc_acos_finite:
2298 case LibFunc_acosf_finite:
2299 case LibFunc_asin:
2300 case LibFunc_asinf:
2301 case LibFunc_asin_finite:
2302 case LibFunc_asinf_finite:
2303 case LibFunc_atan:
2304 case LibFunc_atanf:
2305 case LibFunc_atan2:
2306 case LibFunc_atan2f:
2307 case LibFunc_atan2_finite:
2308 case LibFunc_atan2f_finite:
2309 case LibFunc_ceil:
2310 case LibFunc_ceilf:
2311 case LibFunc_cosh:
2312 case LibFunc_coshf:
2313 case LibFunc_cosh_finite:
2314 case LibFunc_coshf_finite:
2315 case LibFunc_cos:
2316 case LibFunc_cosf:
2317 case LibFunc_erf:
2318 case LibFunc_erff:
2319 case LibFunc_exp:
2320 case LibFunc_expf:
2321 case LibFunc_exp_finite:
2322 case LibFunc_expf_finite:
2323 case LibFunc_exp2:
2324 case LibFunc_exp2f:
2325 case LibFunc_exp2_finite:
2326 case LibFunc_exp2f_finite:
2327 case LibFunc_fabs:
2328 case LibFunc_fabsf:
2329 case LibFunc_floor:
2330 case LibFunc_floorf:
2331 case LibFunc_fmod:
2332 case LibFunc_fmodf:
2333 case LibFunc_ilogb:
2334 case LibFunc_ilogbf:
2335 case LibFunc_log:
2336 case LibFunc_logf:
2337 case LibFunc_log_finite:
2338 case LibFunc_logf_finite:
2339 case LibFunc_logb:
2340 case LibFunc_logbf:
2341 case LibFunc_logl:
2342 case LibFunc_log2:
2343 case LibFunc_log2f:
2344 case LibFunc_log2_finite:
2345 case LibFunc_log2f_finite:
2346 case LibFunc_log10:
2347 case LibFunc_log10f:
2348 case LibFunc_log10_finite:
2349 case LibFunc_log10f_finite:
2350 case LibFunc_log1p:
2351 case LibFunc_log1pf:
2352 case LibFunc_nearbyint:
2353 case LibFunc_nearbyintf:
2354 case LibFunc_nextafter:
2355 case LibFunc_nextafterf:
2356 case LibFunc_nexttoward:
2357 case LibFunc_nexttowardf:
2358 case LibFunc_pow:
2359 case LibFunc_powf:
2360 case LibFunc_pow_finite:
2361 case LibFunc_powf_finite:
2362 case LibFunc_remainder:
2363 case LibFunc_remainderf:
2364 case LibFunc_rint:
2365 case LibFunc_rintf:
2366 case LibFunc_round:
2367 case LibFunc_roundf:
2368 case LibFunc_roundeven:
2369 case LibFunc_roundevenf:
2370 case LibFunc_sin:
2371 case LibFunc_sinf:
2372 case LibFunc_sinh:
2373 case LibFunc_sinhf:
2374 case LibFunc_sinh_finite:
2375 case LibFunc_sinhf_finite:
2376 case LibFunc_sqrt:
2377 case LibFunc_sqrtf:
2378 case LibFunc_tan:
2379 case LibFunc_tanf:
2380 case LibFunc_tanh:
2381 case LibFunc_tanhf:
2382 case LibFunc_trunc:
2383 case LibFunc_truncf:
2384 return true;
2385 default:
2386 return false;
2387 }
2388}
2389
2390namespace {
2391
2392Constant *GetConstantFoldFPValue(double V, Type *Ty) {
2393 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isBFloatTy()) {
2394 APFloat APF(V);
2395 bool unused;
2396 APF.convert(Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &unused);
2397 return ConstantFP::get(Ty->getContext(), APF);
2398 }
2399 if (Ty->isDoubleTy())
2400 return ConstantFP::get(Ty->getContext(), APFloat(V));
2401 llvm_unreachable("Can only constant fold half/float/double/bfloat");
2402}
2403
2404#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
2405Constant *GetConstantFoldFPValue128(float128 V, Type *Ty) {
2406 if (Ty->isFP128Ty())
2407 return ConstantFP::get(Ty, V);
2408 llvm_unreachable("Can only constant fold fp128");
2409}
2410#endif
2411
2412/// Clear the floating-point exception state.
2413inline void llvm_fenv_clearexcept() {
2414#if defined(FE_ALL_EXCEPT)
2415 feclearexcept(FE_ALL_EXCEPT);
2416#endif
2417 errno = 0;
2418}
2419
2420/// Test if a floating-point exception was raised.
2421inline bool llvm_fenv_testexcept() {
2422 int errno_val = errno;
2423 if (errno_val == ERANGE || errno_val == EDOM)
2424 return true;
2425#if defined(FE_ALL_EXCEPT) && defined(FE_INEXACT)
2426 if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
2427 return true;
2428#endif
2429 return false;
2430}
2431
2432static APFloat FTZPreserveSign(const APFloat &V) {
2433 if (V.isDenormal())
2434 return APFloat::getZero(V.getSemantics(), V.isNegative());
2435 return V;
2436}
2437
2438static APFloat FlushToPositiveZero(const APFloat &V) {
2439 if (V.isDenormal())
2440 return APFloat::getZero(V.getSemantics(), false);
2441 return V;
2442}
2443
2444static APFloat FlushWithDenormKind(const APFloat &V,
2445 DenormalMode::DenormalModeKind DenormKind) {
2448 switch (DenormKind) {
2450 return V;
2452 return FTZPreserveSign(V);
2454 return FlushToPositiveZero(V);
2455 default:
2456 llvm_unreachable("Invalid denormal mode!");
2457 }
2458}
2459
2460#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
2461Constant *ConstantFoldFP128(float128 (*NativeFP)(float128), const APFloat &V,
2462 Type *Ty) {
2463 llvm_fenv_clearexcept();
2464 float128 Result = NativeFP(V.convertToQuad());
2465 if (llvm_fenv_testexcept()) {
2466 llvm_fenv_clearexcept();
2467 return nullptr;
2468 }
2469
2470 return GetConstantFoldFPValue128(Result, Ty);
2471}
2472#endif
2473
2474Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
2475 const APFloat &V, const APFloat &W, Type *Ty) {
2476 llvm_fenv_clearexcept();
2477 double Result = NativeFP(V.convertToDouble(), W.convertToDouble());
2478 if (llvm_fenv_testexcept()) {
2479 llvm_fenv_clearexcept();
2480 return nullptr;
2481 }
2482
2483 return GetConstantFoldFPValue(Result, Ty);
2484}
2485
2486Constant *constantFoldVectorReduce(Intrinsic::ID IID, Constant *Op) {
2487 auto *OpVT = cast<VectorType>(Op->getType());
2488
2489 // This is the same as the underlying binops - poison propagates.
2490 if (Op->containsPoisonElement())
2491 return PoisonValue::get(OpVT->getElementType());
2492
2493 // Shortcut non-accumulating reductions.
2494 if (Constant *SplatVal = Op->getSplatValue()) {
2495 switch (IID) {
2496 case Intrinsic::vector_reduce_and:
2497 case Intrinsic::vector_reduce_or:
2498 case Intrinsic::vector_reduce_smin:
2499 case Intrinsic::vector_reduce_smax:
2500 case Intrinsic::vector_reduce_umin:
2501 case Intrinsic::vector_reduce_umax:
2502 return SplatVal;
2503 case Intrinsic::vector_reduce_add:
2504 if (SplatVal->isNullValue())
2505 return SplatVal;
2506 break;
2507 case Intrinsic::vector_reduce_mul:
2508 if (SplatVal->isNullValue() || SplatVal->isOneValue())
2509 return SplatVal;
2510 break;
2511 case Intrinsic::vector_reduce_xor:
2512 if (SplatVal->isNullValue())
2513 return SplatVal;
2514 if (OpVT->getElementCount().isKnownMultipleOf(2))
2515 return Constant::getNullValue(OpVT->getElementType());
2516 break;
2517 }
2518 }
2519
2521 if (!VT)
2522 return nullptr;
2523
2524 auto *EltC = dyn_cast_or_null<ConstantInt>(Op->getAggregateElement(0U));
2525 if (!EltC)
2526 return nullptr;
2527
2528 APInt Acc = EltC->getValue();
2529 for (unsigned I = 1, E = VT->getNumElements(); I != E; I++) {
2530 if (!(EltC = dyn_cast_or_null<ConstantInt>(Op->getAggregateElement(I))))
2531 return nullptr;
2532 const APInt &X = EltC->getValue();
2533 switch (IID) {
2534 case Intrinsic::vector_reduce_add:
2535 Acc = Acc + X;
2536 break;
2537 case Intrinsic::vector_reduce_mul:
2538 Acc = Acc * X;
2539 break;
2540 case Intrinsic::vector_reduce_and:
2541 Acc = Acc & X;
2542 break;
2543 case Intrinsic::vector_reduce_or:
2544 Acc = Acc | X;
2545 break;
2546 case Intrinsic::vector_reduce_xor:
2547 Acc = Acc ^ X;
2548 break;
2549 case Intrinsic::vector_reduce_smin:
2550 Acc = APIntOps::smin(Acc, X);
2551 break;
2552 case Intrinsic::vector_reduce_smax:
2553 Acc = APIntOps::smax(Acc, X);
2554 break;
2555 case Intrinsic::vector_reduce_umin:
2556 Acc = APIntOps::umin(Acc, X);
2557 break;
2558 case Intrinsic::vector_reduce_umax:
2559 Acc = APIntOps::umax(Acc, X);
2560 break;
2561 }
2562 }
2563
2564 return ConstantInt::get(Op->getContext(), Acc);
2565}
2566
2567/// Fold a vector partial reduction add using the deterministic grouping
2568/// chosen by TargetLowering::expandPartialReduceMLA. Although the
2569/// LangRef leaves the grouping unspecified, input element I is accumulated
2570/// into result lane I % NumAccElts, with each accumulator element seeding
2571/// its corresponding result lane. Returns nullptr if any element cannot be
2572/// folded.
2573static Constant *constantFoldVectorPartialReduceAdd(Constant *Acc,
2574 Constant *Input,
2575 const DataLayout &DL) {
2576 auto *AccTy = cast<FixedVectorType>(Acc->getType());
2577 // A fixed result type does not guarantee a fixed input type.
2578 auto *InputTy = dyn_cast<FixedVectorType>(Input->getType());
2579 if (!InputTy)
2580 return nullptr;
2581
2582 unsigned NumAccElts = AccTy->getNumElements();
2583 unsigned NumInputElts = InputTy->getNumElements();
2584
2585 SmallVector<Constant *> ResultElts(NumAccElts);
2586 for (unsigned I = 0; I < NumAccElts; ++I) {
2587 ResultElts[I] = Acc->getAggregateElement(I);
2588 if (!ResultElts[I])
2589 return nullptr;
2590 }
2591
2592 for (unsigned I = 0; I < NumInputElts; ++I) {
2593 Constant *InputElt = Input->getAggregateElement(I);
2594 if (!InputElt)
2595 return nullptr;
2596
2597 unsigned ResultIdx = I % NumAccElts;
2599 Instruction::Add, ResultElts[ResultIdx], InputElt, DL);
2600 if (!Folded)
2601 return nullptr;
2602
2603 ResultElts[ResultIdx] = Folded;
2604 }
2605
2606 return ConstantVector::get(ResultElts);
2607}
2608
2609/// Attempt to fold an SSE floating point to integer conversion of a constant
2610/// floating point. If roundTowardZero is false, the default IEEE rounding is
2611/// used (toward nearest, ties to even). This matches the behavior of the
2612/// non-truncating SSE instructions in the default rounding mode. The desired
2613/// integer type Ty is used to select how many bits are available for the
2614/// result. Returns null if the conversion cannot be performed, otherwise
2615/// returns the Constant value resulting from the conversion.
2616Constant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero,
2617 Type *Ty, bool IsSigned) {
2618 // All of these conversion intrinsics form an integer of at most 64bits.
2619 unsigned ResultWidth = Ty->getIntegerBitWidth();
2620 assert(ResultWidth <= 64 &&
2621 "Can only constant fold conversions to 64 and 32 bit ints");
2622
2623 uint64_t UIntVal;
2624 bool isExact = false;
2628 Val.convertToInteger(MutableArrayRef(UIntVal), ResultWidth,
2629 IsSigned, mode, &isExact);
2630 if (status != APFloat::opOK &&
2631 (!roundTowardZero || status != APFloat::opInexact))
2632 return nullptr;
2633 return ConstantInt::get(Ty, UIntVal, IsSigned);
2634}
2635
2636double getValueAsDouble(ConstantFP *Op) {
2637 Type *Ty = Op->getType();
2638
2639 if (Ty->isBFloatTy() || Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())
2640 return Op->getValueAPF().convertToDouble();
2641
2642 bool unused;
2643 APFloat APF = Op->getValueAPF();
2645 return APF.convertToDouble();
2646}
2647
2648static bool getConstIntOrUndef(Value *Op, const APInt *&C) {
2649 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
2650 C = &CI->getValue();
2651 return true;
2652 }
2653 if (isa<UndefValue>(Op)) {
2654 C = nullptr;
2655 return true;
2656 }
2657 return false;
2658}
2659
2660/// Checks if the given intrinsic call, which evaluates to constant, is allowed
2661/// to be folded.
2662///
2663/// \param CI Constrained intrinsic call.
2664/// \param St Exception flags raised during constant evaluation.
2665static bool mayFoldConstrained(ConstrainedFPIntrinsic *CI,
2666 APFloat::opStatus St) {
2667 std::optional<RoundingMode> ORM = CI->getRoundingMode();
2668 std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
2669
2670 // If the operation does not change exception status flags, it is safe
2671 // to fold.
2672 if (St == APFloat::opStatus::opOK)
2673 return true;
2674
2675 // If evaluation raised FP exception, the result can depend on rounding
2676 // mode. If the latter is unknown, folding is not possible.
2677 if (ORM == RoundingMode::Dynamic)
2678 return false;
2679
2680 // If FP exceptions are ignored, fold the call, even if such exception is
2681 // raised.
2682 if (EB && *EB != fp::ExceptionBehavior::ebStrict)
2683 return true;
2684
2685 // Leave the calculation for runtime so that exception flags be correctly set
2686 // in hardware.
2687 return false;
2688}
2689
2690/// Returns the rounding mode that should be used for constant evaluation.
2691static RoundingMode
2692getEvaluationRoundingMode(const ConstrainedFPIntrinsic *CI) {
2693 std::optional<RoundingMode> ORM = CI->getRoundingMode();
2694 if (!ORM || *ORM == RoundingMode::Dynamic)
2695 // Even if the rounding mode is unknown, try evaluating the operation.
2696 // If it does not raise inexact exception, rounding was not applied,
2697 // so the result is exact and does not depend on rounding mode. Whether
2698 // other FP exceptions are raised, it does not depend on rounding mode.
2700 return *ORM;
2701}
2702
2703/// Try to constant fold llvm.canonicalize for the given caller and value.
2704static Constant *constantFoldCanonicalize(const Type *Ty, const APFloat &Src,
2705 const Function *CtxF = nullptr) {
2706 // Zero, positive and negative, is always OK to fold.
2707 if (Src.isZero()) {
2708 // Get a fresh 0, since ppc_fp128 does have non-canonical zeros.
2709 return ConstantFP::get(
2710 Ty->getContext(),
2711 APFloat::getZero(Src.getSemantics(), Src.isNegative()));
2712 }
2713
2714 if (!Ty->isIEEELikeFPTy())
2715 return nullptr;
2716
2717 // Zero is always canonical and the sign must be preserved.
2718 //
2719 // Denorms and nans may have special encodings, but it should be OK to fold a
2720 // totally average number.
2721 if (Src.isNormal() || Src.isInfinity())
2722 return ConstantFP::get(Ty->getContext(), Src);
2723
2724 if (Src.isDenormal() && CtxF) {
2725 DenormalMode DenormMode = CtxF->getDenormalMode(Src.getSemantics());
2726
2727 if (DenormMode == DenormalMode::getIEEE())
2728 return ConstantFP::get(Ty->getContext(), Src);
2729
2730 if (DenormMode.Input == DenormalMode::Dynamic)
2731 return nullptr;
2732
2733 // If we know if either input or output is flushed, we can fold.
2734 if ((DenormMode.Input == DenormalMode::Dynamic &&
2735 DenormMode.Output == DenormalMode::IEEE) ||
2736 (DenormMode.Input == DenormalMode::IEEE &&
2737 DenormMode.Output == DenormalMode::Dynamic))
2738 return nullptr;
2739
2740 bool IsPositive =
2741 (!Src.isNegative() || DenormMode.Input == DenormalMode::PositiveZero ||
2742 (DenormMode.Output == DenormalMode::PositiveZero &&
2743 DenormMode.Input == DenormalMode::IEEE));
2744
2745 return ConstantFP::get(Ty->getContext(),
2746 APFloat::getZero(Src.getSemantics(), !IsPositive));
2747 }
2748
2749 return nullptr;
2750}
2751
2752static Constant *ConstantFoldScalarCall1(StringRef Name,
2753 Intrinsic::ID IntrinsicID, Type *Ty,
2755 const TargetLibraryInfo *TLI = nullptr,
2756 const CallBase *Call = nullptr) {
2757 assert(Operands.size() == 1 && "Wrong number of operands.");
2758
2759 if (IntrinsicID == Intrinsic::is_constant) {
2760 // We know we have a "Constant" argument. But we want to only
2761 // return true for manifest constants, not those that depend on
2762 // constants with unknowable values, e.g. GlobalValue or BlockAddress.
2763 if (Operands[0]->isManifestConstant())
2764 return ConstantInt::getTrue(Ty->getContext());
2765 return nullptr;
2766 }
2767
2768 if (isa<UndefValue>(Operands[0])) {
2769 // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN.
2770 // ctpop() is between 0 and bitwidth, pick 0 for undef.
2771 // fptoui.sat and fptosi.sat can always fold to zero (for a zero input).
2772 if (IntrinsicID == Intrinsic::cos ||
2773 IntrinsicID == Intrinsic::ctpop ||
2774 IntrinsicID == Intrinsic::fptoui_sat ||
2775 IntrinsicID == Intrinsic::fptosi_sat ||
2776 IntrinsicID == Intrinsic::canonicalize)
2777 return Constant::getNullValue(Ty);
2778 if (IntrinsicID == Intrinsic::bswap ||
2779 IntrinsicID == Intrinsic::bitreverse ||
2780 IntrinsicID == Intrinsic::launder_invariant_group)
2781 return Operands[0];
2782 }
2783
2785 // launder(null) == null iff in addrspace 0
2786 if (IntrinsicID == Intrinsic::launder_invariant_group) {
2787 // If instruction is not yet put in a basic block (e.g. when cloning
2788 // a function during inlining), Call's caller may not be available.
2789 // So check Call's BB first before querying Call->getCaller.
2790 const Function *Caller =
2791 Call && Call->getParent() ? Call->getCaller() : nullptr;
2792 if (Caller &&
2794 Caller, Operands[0]->getType()->getPointerAddressSpace())) {
2795 return Operands[0];
2796 }
2797 return nullptr;
2798 }
2799 }
2800
2801 if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) {
2802 APFloat U = Op->getValueAPF();
2803
2804 // fpext is exact in every rounding mode, so the only exception it can
2805 // raise is invalid for a signaling NaN input. Handle it before the type
2806 // check below so fp128 and x86_fp80 results are folded too.
2807 if (IntrinsicID == Intrinsic::experimental_constrained_fpext) {
2809 if (!CI)
2810 return nullptr;
2811 bool LosesInfo;
2812 APFloat::opStatus St = U.convert(
2813 Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &LosesInfo);
2814 if (!mayFoldConstrained(const_cast<ConstrainedFPIntrinsic *>(CI), St))
2815 return nullptr;
2816 return ConstantFP::get(Ty->getContext(), U);
2817 }
2818
2819 if (IntrinsicID == Intrinsic::wasm_trunc_signed ||
2820 IntrinsicID == Intrinsic::wasm_trunc_unsigned) {
2821 bool Signed = IntrinsicID == Intrinsic::wasm_trunc_signed;
2822
2823 if (U.isNaN())
2824 return nullptr;
2825
2826 unsigned Width = Ty->getIntegerBitWidth();
2827 APSInt Int(Width, !Signed);
2828 bool IsExact = false;
2830 U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact);
2831
2833 return ConstantInt::get(Ty, Int);
2834
2835 return nullptr;
2836 }
2837
2838 if (IntrinsicID == Intrinsic::fptoui_sat ||
2839 IntrinsicID == Intrinsic::fptosi_sat) {
2840 // convertToInteger() already has the desired saturation semantics.
2841 APSInt Int(Ty->getIntegerBitWidth(),
2842 IntrinsicID == Intrinsic::fptoui_sat);
2843 bool IsExact;
2844 U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact);
2845 return ConstantInt::get(Ty, Int);
2846 }
2847
2848 if (IntrinsicID == Intrinsic::canonicalize) {
2849 const Function *CtxF =
2850 Call && Call->getParent() ? Call->getFunction() : nullptr;
2851 return constantFoldCanonicalize(Ty, U, CtxF);
2852 }
2853
2854#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
2855 if (Ty->isFP128Ty()) {
2856 if (IntrinsicID == Intrinsic::log) {
2857 float128 Result = logf128(Op->getValueAPF().convertToQuad());
2858 return GetConstantFoldFPValue128(Result, Ty);
2859 }
2860
2861 if (TLI && TLI->getLibFunc(Name) == LibFunc_logl &&
2862 TLI->has(LibFunc_logl))
2863 return ConstantFoldFP128(logf128, Op->getValueAPF(), Ty);
2864 }
2865#endif
2866
2867 if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy() &&
2868 !Ty->isIntegerTy() && !Ty->isBFloatTy())
2869 return nullptr;
2870
2871 // Use internal versions of these intrinsics.
2872
2873 if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint ||
2874 IntrinsicID == Intrinsic::roundeven) {
2875 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2876 return ConstantFP::get(Ty, U);
2877 }
2878
2879 if (IntrinsicID == Intrinsic::round) {
2880 U.roundToIntegral(APFloat::rmNearestTiesToAway);
2881 return ConstantFP::get(Ty, U);
2882 }
2883
2884 if (IntrinsicID == Intrinsic::roundeven) {
2885 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2886 return ConstantFP::get(Ty, U);
2887 }
2888
2889 if (IntrinsicID == Intrinsic::ceil) {
2890 U.roundToIntegral(APFloat::rmTowardPositive);
2891 return ConstantFP::get(Ty, U);
2892 }
2893
2894 if (IntrinsicID == Intrinsic::floor) {
2895 U.roundToIntegral(APFloat::rmTowardNegative);
2896 return ConstantFP::get(Ty, U);
2897 }
2898
2899 if (IntrinsicID == Intrinsic::trunc) {
2900 U.roundToIntegral(APFloat::rmTowardZero);
2901 return ConstantFP::get(Ty, U);
2902 }
2903
2904 if (IntrinsicID == Intrinsic::fabs) {
2905 U.clearSign();
2906 return ConstantFP::get(Ty, U);
2907 }
2908
2909 if (IntrinsicID == Intrinsic::amdgcn_fract) {
2910 // The v_fract instruction behaves like the OpenCL spec, which defines
2911 // fract(x) as fmin(x - floor(x), 0x1.fffffep-1f): "The min() operator is
2912 // there to prevent fract(-small) from returning 1.0. It returns the
2913 // largest positive floating-point number less than 1.0."
2914 APFloat FloorU(U);
2915 FloorU.roundToIntegral(APFloat::rmTowardNegative);
2916 APFloat FractU(U - FloorU);
2917 APFloat AlmostOne(U.getSemantics(), 1);
2918 AlmostOne.next(/*nextDown*/ true);
2919 return ConstantFP::get(Ty, minimum(FractU, AlmostOne));
2920 }
2921
2922 // Rounding operations (floor, trunc, ceil, round and nearbyint) do not
2923 // raise FP exceptions, unless the argument is signaling NaN.
2924
2926 std::optional<APFloat::roundingMode> RM;
2927 switch (IntrinsicID) {
2928 default:
2929 break;
2930 case Intrinsic::experimental_constrained_nearbyint:
2931 case Intrinsic::experimental_constrained_rint: {
2932 RM = CI->getRoundingMode();
2933 if (!RM || *RM == RoundingMode::Dynamic)
2934 return nullptr;
2935 break;
2936 }
2937 case Intrinsic::experimental_constrained_round:
2939 break;
2940 case Intrinsic::experimental_constrained_ceil:
2942 break;
2943 case Intrinsic::experimental_constrained_floor:
2945 break;
2946 case Intrinsic::experimental_constrained_trunc:
2948 break;
2949 }
2950 if (RM) {
2951 if (U.isFinite()) {
2952 APFloat::opStatus St = U.roundToIntegral(*RM);
2953 if (IntrinsicID == Intrinsic::experimental_constrained_rint &&
2954 St == APFloat::opInexact) {
2955 std::optional<fp::ExceptionBehavior> EB =
2957 if (EB == fp::ebStrict)
2958 return nullptr;
2959 }
2960 } else if (U.isSignaling()) {
2961 std::optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior();
2962 if (EB && *EB != fp::ebIgnore)
2963 return nullptr;
2964 U = APFloat::getQNaN(U.getSemantics());
2965 }
2966 return ConstantFP::get(Ty, U);
2967 }
2968 }
2969
2970 // NVVM float/double to signed/unsigned int32/int64 conversions:
2971 switch (IntrinsicID) {
2972 // f2i
2973 case Intrinsic::nvvm_f2i_rm:
2974 case Intrinsic::nvvm_f2i_rn:
2975 case Intrinsic::nvvm_f2i_rp:
2976 case Intrinsic::nvvm_f2i_rz:
2977 case Intrinsic::nvvm_f2i_rm_ftz:
2978 case Intrinsic::nvvm_f2i_rn_ftz:
2979 case Intrinsic::nvvm_f2i_rp_ftz:
2980 case Intrinsic::nvvm_f2i_rz_ftz:
2981 // f2ui
2982 case Intrinsic::nvvm_f2ui_rm:
2983 case Intrinsic::nvvm_f2ui_rn:
2984 case Intrinsic::nvvm_f2ui_rp:
2985 case Intrinsic::nvvm_f2ui_rz:
2986 case Intrinsic::nvvm_f2ui_rm_ftz:
2987 case Intrinsic::nvvm_f2ui_rn_ftz:
2988 case Intrinsic::nvvm_f2ui_rp_ftz:
2989 case Intrinsic::nvvm_f2ui_rz_ftz:
2990 // d2i
2991 case Intrinsic::nvvm_d2i_rm:
2992 case Intrinsic::nvvm_d2i_rn:
2993 case Intrinsic::nvvm_d2i_rp:
2994 case Intrinsic::nvvm_d2i_rz:
2995 // d2ui
2996 case Intrinsic::nvvm_d2ui_rm:
2997 case Intrinsic::nvvm_d2ui_rn:
2998 case Intrinsic::nvvm_d2ui_rp:
2999 case Intrinsic::nvvm_d2ui_rz:
3000 // f2ll
3001 case Intrinsic::nvvm_f2ll_rm:
3002 case Intrinsic::nvvm_f2ll_rn:
3003 case Intrinsic::nvvm_f2ll_rp:
3004 case Intrinsic::nvvm_f2ll_rz:
3005 case Intrinsic::nvvm_f2ll_rm_ftz:
3006 case Intrinsic::nvvm_f2ll_rn_ftz:
3007 case Intrinsic::nvvm_f2ll_rp_ftz:
3008 case Intrinsic::nvvm_f2ll_rz_ftz:
3009 // f2ull
3010 case Intrinsic::nvvm_f2ull_rm:
3011 case Intrinsic::nvvm_f2ull_rn:
3012 case Intrinsic::nvvm_f2ull_rp:
3013 case Intrinsic::nvvm_f2ull_rz:
3014 case Intrinsic::nvvm_f2ull_rm_ftz:
3015 case Intrinsic::nvvm_f2ull_rn_ftz:
3016 case Intrinsic::nvvm_f2ull_rp_ftz:
3017 case Intrinsic::nvvm_f2ull_rz_ftz:
3018 // d2ll
3019 case Intrinsic::nvvm_d2ll_rm:
3020 case Intrinsic::nvvm_d2ll_rn:
3021 case Intrinsic::nvvm_d2ll_rp:
3022 case Intrinsic::nvvm_d2ll_rz:
3023 // d2ull
3024 case Intrinsic::nvvm_d2ull_rm:
3025 case Intrinsic::nvvm_d2ull_rn:
3026 case Intrinsic::nvvm_d2ull_rp:
3027 case Intrinsic::nvvm_d2ull_rz: {
3028 // In float-to-integer conversion, NaN inputs are converted to 0.
3029 if (U.isNaN()) {
3030 // In float-to-integer conversion, NaN inputs are converted to 0
3031 // when the source and destination bitwidths are both less than 64.
3032 if (nvvm::FPToIntegerIntrinsicNaNZero(IntrinsicID))
3033 return ConstantInt::get(Ty, 0);
3034
3035 // Otherwise, the most significant bit is set.
3036 unsigned BitWidth = Ty->getIntegerBitWidth();
3037 uint64_t Val = 1ULL << (BitWidth - 1);
3038 return ConstantInt::get(Ty, APInt(BitWidth, Val, /*IsSigned=*/false));
3039 }
3040
3041 APFloat::roundingMode RMode =
3043 bool IsFTZ = nvvm::FPToIntegerIntrinsicShouldFTZ(IntrinsicID);
3044 bool IsSigned = nvvm::FPToIntegerIntrinsicResultIsSigned(IntrinsicID);
3045
3046 APSInt ResInt(Ty->getIntegerBitWidth(), !IsSigned);
3047 auto FloatToRound = IsFTZ ? FTZPreserveSign(U) : U;
3048
3049 // Return max/min value for integers if the result is +/-inf or
3050 // is too large to fit in the result's integer bitwidth.
3051 bool IsExact = false;
3052 FloatToRound.convertToInteger(ResInt, RMode, &IsExact);
3053 return ConstantInt::get(Ty, ResInt);
3054 }
3055 }
3056
3057 /// We only fold functions with finite arguments. Folding NaN and inf is
3058 /// likely to be aborted with an exception anyway, and some host libms
3059 /// have known errors raising exceptions.
3060 if (!U.isFinite())
3061 return nullptr;
3062
3063 /// Currently APFloat versions of these functions do not exist, so we use
3064 /// the host native double versions. Float versions are not called
3065 /// directly but for all these it is true (float)(f((double)arg)) ==
3066 /// f(arg). Long double not supported yet.
3067 const APFloat &APF = Op->getValueAPF();
3068
3069 switch (IntrinsicID) {
3070 default: break;
3071 case Intrinsic::log:
3072 if (U.isZero())
3073 return ConstantFP::getInfinity(Ty, true);
3074 if (U.isNegative())
3075 return ConstantFP::getNaN(Ty);
3076 if (U.isOne())
3077 return ConstantFP::getZero(Ty);
3078 return ConstantFoldFP(log, APF, Ty);
3079 case Intrinsic::log2:
3080 if (U.isZero())
3081 return ConstantFP::getInfinity(Ty, true);
3082 if (U.isNegative())
3083 return ConstantFP::getNaN(Ty);
3084 if (U.isOne())
3085 return ConstantFP::getZero(Ty);
3086 // TODO: What about hosts that lack a C99 library?
3087 return ConstantFoldFP(log2, APF, Ty);
3088 case Intrinsic::log10:
3089 if (U.isZero())
3090 return ConstantFP::getInfinity(Ty, true);
3091 if (U.isNegative())
3092 return ConstantFP::getNaN(Ty);
3093 if (U.isOne())
3094 return ConstantFP::getZero(Ty);
3095 // TODO: What about hosts that lack a C99 library?
3096 return ConstantFoldFP(log10, APF, Ty);
3097 case Intrinsic::exp:
3098 return ConstantFoldFP(exp, APF, Ty);
3099 case Intrinsic::exp2:
3100 // Fold exp2(x) as pow(2, x), in case the host lacks a C99 library.
3101 return ConstantFoldBinaryFP(pow, APFloat(2.0), APF, Ty);
3102 case Intrinsic::exp10:
3103 // Fold exp10(x) as pow(10, x), in case the host lacks a C99 library.
3104 return ConstantFoldBinaryFP(pow, APFloat(10.0), APF, Ty);
3105 case Intrinsic::sin:
3106 return ConstantFoldFP(sin, APF, Ty);
3107 case Intrinsic::cos:
3108 return ConstantFoldFP(cos, APF, Ty);
3109 case Intrinsic::sinh:
3110 return ConstantFoldFP(sinh, APF, Ty);
3111 case Intrinsic::cosh:
3112 return ConstantFoldFP(cosh, APF, Ty);
3113 case Intrinsic::atan:
3114 // Implement optional behavior from C's Annex F for +/-0.0.
3115 if (U.isZero())
3116 return ConstantFP::get(Ty, U);
3117 return ConstantFoldFP(atan, APF, Ty);
3118 case Intrinsic::sqrt:
3119 return ConstantFoldFP(sqrt, APF, Ty);
3120
3121 // NVVM Intrinsics:
3122 case Intrinsic::nvvm_ceil_ftz_f:
3123 case Intrinsic::nvvm_ceil_f:
3124 case Intrinsic::nvvm_ceil_d:
3125 return ConstantFoldFP(
3126 ceil, APF, Ty,
3128 nvvm::UnaryMathIntrinsicShouldFTZ(IntrinsicID)));
3129
3130 case Intrinsic::nvvm_fabs_ftz:
3131 case Intrinsic::nvvm_fabs:
3132 return ConstantFoldFP(
3133 fabs, APF, Ty,
3135 nvvm::UnaryMathIntrinsicShouldFTZ(IntrinsicID)));
3136
3137 case Intrinsic::nvvm_floor_ftz_f:
3138 case Intrinsic::nvvm_floor_f:
3139 case Intrinsic::nvvm_floor_d:
3140 return ConstantFoldFP(
3141 floor, APF, Ty,
3143 nvvm::UnaryMathIntrinsicShouldFTZ(IntrinsicID)));
3144
3145 case Intrinsic::nvvm_rcp_rm_ftz_f:
3146 case Intrinsic::nvvm_rcp_rn_ftz_f:
3147 case Intrinsic::nvvm_rcp_rp_ftz_f:
3148 case Intrinsic::nvvm_rcp_rz_ftz_f:
3149 case Intrinsic::nvvm_rcp_rm_d:
3150 case Intrinsic::nvvm_rcp_rm_f:
3151 case Intrinsic::nvvm_rcp_rn_d:
3152 case Intrinsic::nvvm_rcp_rn_f:
3153 case Intrinsic::nvvm_rcp_rp_d:
3154 case Intrinsic::nvvm_rcp_rp_f:
3155 case Intrinsic::nvvm_rcp_rz_d:
3156 case Intrinsic::nvvm_rcp_rz_f: {
3157 APFloat::roundingMode RoundMode = nvvm::GetRCPRoundingMode(IntrinsicID);
3158 bool IsFTZ = nvvm::RCPShouldFTZ(IntrinsicID);
3159
3160 auto Denominator = IsFTZ ? FTZPreserveSign(APF) : APF;
3162 APFloat::opStatus Status = Res.divide(Denominator, RoundMode);
3163
3165 if (IsFTZ)
3166 Res = FTZPreserveSign(Res);
3167 return ConstantFP::get(Ty, Res);
3168 }
3169 return nullptr;
3170 }
3171
3172 case Intrinsic::nvvm_round_ftz_f:
3173 case Intrinsic::nvvm_round_f:
3174 case Intrinsic::nvvm_round_d: {
3175 // nvvm_round is lowered to PTX cvt.rni, which will round to nearest
3176 // integer, choosing even integer if source is equidistant between two
3177 // integers, so the semantics are closer to "rint" rather than "round".
3178 bool IsFTZ = nvvm::UnaryMathIntrinsicShouldFTZ(IntrinsicID);
3179 auto V = IsFTZ ? FTZPreserveSign(APF) : APF;
3180 V.roundToIntegral(APFloat::rmNearestTiesToEven);
3181 return ConstantFP::get(Ty, V);
3182 }
3183
3184 case Intrinsic::nvvm_saturate_ftz_f:
3185 case Intrinsic::nvvm_saturate_d:
3186 case Intrinsic::nvvm_saturate_f: {
3187 bool IsFTZ = nvvm::UnaryMathIntrinsicShouldFTZ(IntrinsicID);
3188 auto V = IsFTZ ? FTZPreserveSign(APF) : APF;
3189 if (V.isNegative() || V.isZero() || V.isNaN())
3190 return ConstantFP::getZero(Ty);
3192 if (V > One)
3193 return ConstantFP::get(Ty, One);
3194 return ConstantFP::get(Ty, APF);
3195 }
3196
3197 case Intrinsic::nvvm_sqrt_rn_ftz_f:
3198 case Intrinsic::nvvm_sqrt_f:
3199 case Intrinsic::nvvm_sqrt_rn_d:
3200 case Intrinsic::nvvm_sqrt_rn_f:
3201 if (APF.isNegative())
3202 return nullptr;
3203 return ConstantFoldFP(
3204 sqrt, APF, Ty,
3206 nvvm::UnaryMathIntrinsicShouldFTZ(IntrinsicID)));
3207
3208 // AMDGCN Intrinsics:
3209 case Intrinsic::amdgcn_cos:
3210 case Intrinsic::amdgcn_sin: {
3211 double V = getValueAsDouble(Op);
3212 if (V < -256.0 || V > 256.0)
3213 // The gfx8 and gfx9 architectures handle arguments outside the range
3214 // [-256, 256] differently. This should be a rare case so bail out
3215 // rather than trying to handle the difference.
3216 return nullptr;
3217 bool IsCos = IntrinsicID == Intrinsic::amdgcn_cos;
3218 double V4 = V * 4.0;
3219 if (V4 == floor(V4)) {
3220 // Force exact results for quarter-integer inputs.
3221 const double SinVals[4] = { 0.0, 1.0, 0.0, -1.0 };
3222 V = SinVals[((int)V4 + (IsCos ? 1 : 0)) & 3];
3223 } else {
3224 if (IsCos)
3225 V = cos(V * 2.0 * numbers::pi);
3226 else
3227 V = sin(V * 2.0 * numbers::pi);
3228 }
3229 return GetConstantFoldFPValue(V, Ty);
3230 }
3231 }
3232
3233 if (!TLI)
3234 return nullptr;
3235
3236 LibFunc Func = TLI->getLibFunc(Name);
3237 if (Func == NotLibFunc)
3238 return nullptr;
3239
3240 switch (Func) {
3241 default:
3242 break;
3243 case LibFunc_acos:
3244 case LibFunc_acosf:
3245 case LibFunc_acos_finite:
3246 case LibFunc_acosf_finite:
3247 if (TLI->has(Func))
3248 return ConstantFoldFP(acos, APF, Ty);
3249 break;
3250 case LibFunc_asin:
3251 case LibFunc_asinf:
3252 case LibFunc_asin_finite:
3253 case LibFunc_asinf_finite:
3254 if (TLI->has(Func))
3255 return ConstantFoldFP(asin, APF, Ty);
3256 break;
3257 case LibFunc_atan:
3258 case LibFunc_atanf:
3259 // Implement optional behavior from C's Annex F for +/-0.0.
3260 if (U.isZero())
3261 return ConstantFP::get(Ty, U);
3262 if (TLI->has(Func))
3263 return ConstantFoldFP(atan, APF, Ty);
3264 break;
3265 case LibFunc_ceil:
3266 case LibFunc_ceilf:
3267 if (TLI->has(Func)) {
3268 U.roundToIntegral(APFloat::rmTowardPositive);
3269 return ConstantFP::get(Ty, U);
3270 }
3271 break;
3272 case LibFunc_cos:
3273 case LibFunc_cosf:
3274 if (TLI->has(Func))
3275 return ConstantFoldFP(cos, APF, Ty);
3276 break;
3277 case LibFunc_cosh:
3278 case LibFunc_coshf:
3279 case LibFunc_cosh_finite:
3280 case LibFunc_coshf_finite:
3281 if (TLI->has(Func))
3282 return ConstantFoldFP(cosh, APF, Ty);
3283 break;
3284 case LibFunc_exp:
3285 case LibFunc_expf:
3286 case LibFunc_exp_finite:
3287 case LibFunc_expf_finite:
3288 if (TLI->has(Func))
3289 return ConstantFoldFP(exp, APF, Ty);
3290 break;
3291 case LibFunc_exp2:
3292 case LibFunc_exp2f:
3293 case LibFunc_exp2_finite:
3294 case LibFunc_exp2f_finite:
3295 if (TLI->has(Func))
3296 // Fold exp2(x) as pow(2, x), in case the host lacks a C99 library.
3297 return ConstantFoldBinaryFP(pow, APFloat(2.0), APF, Ty);
3298 break;
3299 case LibFunc_fabs:
3300 case LibFunc_fabsf:
3301 if (TLI->has(Func)) {
3302 U.clearSign();
3303 return ConstantFP::get(Ty, U);
3304 }
3305 break;
3306 case LibFunc_floor:
3307 case LibFunc_floorf:
3308 if (TLI->has(Func)) {
3309 U.roundToIntegral(APFloat::rmTowardNegative);
3310 return ConstantFP::get(Ty, U);
3311 }
3312 break;
3313 case LibFunc_log:
3314 case LibFunc_logf:
3315 case LibFunc_log_finite:
3316 case LibFunc_logf_finite:
3317 if (!APF.isNegative() && !APF.isZero() && TLI->has(Func))
3318 return ConstantFoldFP(log, APF, Ty);
3319 break;
3320 case LibFunc_log2:
3321 case LibFunc_log2f:
3322 case LibFunc_log2_finite:
3323 case LibFunc_log2f_finite:
3324 if (!APF.isNegative() && !APF.isZero() && TLI->has(Func))
3325 // TODO: What about hosts that lack a C99 library?
3326 return ConstantFoldFP(log2, APF, Ty);
3327 break;
3328 case LibFunc_log10:
3329 case LibFunc_log10f:
3330 case LibFunc_log10_finite:
3331 case LibFunc_log10f_finite:
3332 if (!APF.isNegative() && !APF.isZero() && TLI->has(Func))
3333 // TODO: What about hosts that lack a C99 library?
3334 return ConstantFoldFP(log10, APF, Ty);
3335 break;
3336 case LibFunc_ilogb:
3337 case LibFunc_ilogbf:
3338 if (!APF.isZero() && TLI->has(Func))
3339 return ConstantInt::get(Ty, ilogb(APF), true);
3340 break;
3341 case LibFunc_logb:
3342 case LibFunc_logbf:
3343 if (!APF.isZero() && TLI->has(Func))
3344 return ConstantFoldFP(logb, APF, Ty);
3345 break;
3346 case LibFunc_log1p:
3347 case LibFunc_log1pf:
3348 // Implement optional behavior from C's Annex F for +/-0.0.
3349 if (U.isZero())
3350 return ConstantFP::get(Ty, U);
3351 if (APF > APFloat::getOne(APF.getSemantics(), true) && TLI->has(Func))
3352 return ConstantFoldFP(log1p, APF, Ty);
3353 break;
3354 case LibFunc_logl:
3355 return nullptr;
3356 case LibFunc_erf:
3357 case LibFunc_erff:
3358 if (TLI->has(Func))
3359 return ConstantFoldFP(erf, APF, Ty);
3360 break;
3361 case LibFunc_nearbyint:
3362 case LibFunc_nearbyintf:
3363 case LibFunc_rint:
3364 case LibFunc_rintf:
3365 case LibFunc_roundeven:
3366 case LibFunc_roundevenf:
3367 if (TLI->has(Func)) {
3368 U.roundToIntegral(APFloat::rmNearestTiesToEven);
3369 return ConstantFP::get(Ty, U);
3370 }
3371 break;
3372 case LibFunc_round:
3373 case LibFunc_roundf:
3374 if (TLI->has(Func)) {
3375 U.roundToIntegral(APFloat::rmNearestTiesToAway);
3376 return ConstantFP::get(Ty, U);
3377 }
3378 break;
3379 case LibFunc_sin:
3380 case LibFunc_sinf:
3381 if (TLI->has(Func))
3382 return ConstantFoldFP(sin, APF, Ty);
3383 break;
3384 case LibFunc_sinh:
3385 case LibFunc_sinhf:
3386 case LibFunc_sinh_finite:
3387 case LibFunc_sinhf_finite:
3388 if (TLI->has(Func))
3389 return ConstantFoldFP(sinh, APF, Ty);
3390 break;
3391 case LibFunc_sqrt:
3392 case LibFunc_sqrtf:
3393 if (!APF.isNegative() && TLI->has(Func))
3394 return ConstantFoldFP(sqrt, APF, Ty);
3395 break;
3396 case LibFunc_tan:
3397 case LibFunc_tanf:
3398 if (TLI->has(Func))
3399 return ConstantFoldFP(tan, APF, Ty);
3400 break;
3401 case LibFunc_tanh:
3402 case LibFunc_tanhf:
3403 if (TLI->has(Func))
3404 return ConstantFoldFP(tanh, APF, Ty);
3405 break;
3406 case LibFunc_trunc:
3407 case LibFunc_truncf:
3408 if (TLI->has(Func)) {
3409 U.roundToIntegral(APFloat::rmTowardZero);
3410 return ConstantFP::get(Ty, U);
3411 }
3412 break;
3413 }
3414 return nullptr;
3415 }
3416
3417 if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
3418 switch (IntrinsicID) {
3419 case Intrinsic::bswap:
3420 return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
3421 case Intrinsic::ctpop:
3422 return ConstantInt::get(Ty, Op->getValue().popcount());
3423 case Intrinsic::bitreverse:
3424 return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits());
3425 case Intrinsic::amdgcn_s_wqm: {
3426 uint64_t Val = Op->getZExtValue();
3427 Val |= (Val & 0x5555555555555555ULL) << 1 |
3428 ((Val >> 1) & 0x5555555555555555ULL);
3429 Val |= (Val & 0x3333333333333333ULL) << 2 |
3430 ((Val >> 2) & 0x3333333333333333ULL);
3431 return ConstantInt::get(Ty, Val);
3432 }
3433
3434 case Intrinsic::amdgcn_s_quadmask: {
3435 uint64_t Val = Op->getZExtValue();
3436 uint64_t QuadMask = 0;
3437 for (unsigned I = 0; I < Op->getBitWidth() / 4; ++I, Val >>= 4) {
3438 if (!(Val & 0xF))
3439 continue;
3440
3441 QuadMask |= (1ULL << I);
3442 }
3443 return ConstantInt::get(Ty, QuadMask);
3444 }
3445
3446 case Intrinsic::amdgcn_s_bitreplicate: {
3447 uint64_t Val = Op->getZExtValue();
3448 Val = (Val & 0x000000000000FFFFULL) | (Val & 0x00000000FFFF0000ULL) << 16;
3449 Val = (Val & 0x000000FF000000FFULL) | (Val & 0x0000FF000000FF00ULL) << 8;
3450 Val = (Val & 0x000F000F000F000FULL) | (Val & 0x00F000F000F000F0ULL) << 4;
3451 Val = (Val & 0x0303030303030303ULL) | (Val & 0x0C0C0C0C0C0C0C0CULL) << 2;
3452 Val = (Val & 0x1111111111111111ULL) | (Val & 0x2222222222222222ULL) << 1;
3453 Val = Val | Val << 1;
3454 return ConstantInt::get(Ty, Val);
3455 }
3456 }
3457 }
3458
3459 if (Operands[0]->getType()->isVectorTy()) {
3460 auto *Op = cast<Constant>(Operands[0]);
3461 switch (IntrinsicID) {
3462 default: break;
3463 case Intrinsic::vector_reduce_add:
3464 case Intrinsic::vector_reduce_mul:
3465 case Intrinsic::vector_reduce_and:
3466 case Intrinsic::vector_reduce_or:
3467 case Intrinsic::vector_reduce_xor:
3468 case Intrinsic::vector_reduce_smin:
3469 case Intrinsic::vector_reduce_smax:
3470 case Intrinsic::vector_reduce_umin:
3471 case Intrinsic::vector_reduce_umax:
3472 if (Constant *C = constantFoldVectorReduce(IntrinsicID, Operands[0]))
3473 return C;
3474 break;
3475 case Intrinsic::x86_sse_cvtss2si:
3476 case Intrinsic::x86_sse_cvtss2si64:
3477 case Intrinsic::x86_sse2_cvtsd2si:
3478 case Intrinsic::x86_sse2_cvtsd2si64:
3479 if (ConstantFP *FPOp =
3480 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
3481 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
3482 /*roundTowardZero=*/false, Ty,
3483 /*IsSigned*/true);
3484 break;
3485 case Intrinsic::x86_sse_cvttss2si:
3486 case Intrinsic::x86_sse_cvttss2si64:
3487 case Intrinsic::x86_sse2_cvttsd2si:
3488 case Intrinsic::x86_sse2_cvttsd2si64:
3489 if (ConstantFP *FPOp =
3490 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
3491 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
3492 /*roundTowardZero=*/true, Ty,
3493 /*IsSigned*/true);
3494 break;
3495
3496 case Intrinsic::wasm_anytrue:
3497 return Op->isNullValue() ? ConstantInt::get(Ty, 0)
3498 : ConstantInt::get(Ty, 1);
3499
3500 case Intrinsic::wasm_alltrue:
3501 // Check each element individually
3502 unsigned E = cast<FixedVectorType>(Op->getType())->getNumElements();
3503 for (unsigned I = 0; I != E; ++I) {
3504 Constant *Elt = Op->getAggregateElement(I);
3505 // Return false as soon as we find a non-true element.
3506 if (Elt && Elt->isNullValue())
3507 return ConstantInt::get(Ty, 0);
3508 // Bail as soon as we find an element we cannot prove to be true.
3509 if (!Elt || !isa<ConstantInt>(Elt))
3510 return nullptr;
3511 }
3512
3513 return ConstantInt::get(Ty, 1);
3514 }
3515 }
3516
3517 return nullptr;
3518}
3519
3520static Constant *evaluateCompare(const APFloat &Op1, const APFloat &Op2,
3524 FCmpInst::Predicate Cond = FCmp->getPredicate();
3525 if (FCmp->isSignaling()) {
3526 if (Op1.isNaN() || Op2.isNaN())
3528 } else {
3529 if (Op1.isSignaling() || Op2.isSignaling())
3531 }
3532 bool Result = FCmpInst::compare(Op1, Op2, Cond);
3533 if (mayFoldConstrained(const_cast<ConstrainedFPCmpIntrinsic *>(FCmp), St))
3534 return ConstantInt::get(Call->getType()->getScalarType(), Result);
3535 return nullptr;
3536}
3537
3538static Constant *ConstantFoldNextToward(const APFloat &Op0, const APFloat &Op1,
3539 const Type *RetTy) {
3540 assert(RetTy != nullptr);
3541 bool LosesInfo;
3542
3543 if (Op1.isSignaling())
3544 return nullptr;
3545 if (Op1.isNaN()) {
3546 APFloat Ret(Op1);
3547 Ret.convert(RetTy->getFltSemantics(), detail::rmNearestTiesToEven,
3548 &LosesInfo);
3549 return ConstantFP::get(RetTy->getContext(), Ret);
3550 }
3551
3552 // Recall that the second argument of nexttoward is always a long double,
3553 // so we may need to promote the first argument for comparisons to be valid.
3554 APFloat PromotedOp0(Op0);
3555 PromotedOp0.convert(Op1.getSemantics(), detail::rmNearestTiesToEven,
3556 &LosesInfo);
3557 assert(!LosesInfo && "Unexpected lossy promotion");
3558 const APFloat::cmpResult Result = PromotedOp0.compare(Op1);
3559
3560 // When equal, the standard says we must return the second argument.
3561 // This allows nice behavior such as nexttoward(0.0, -0.0) = -0.0 and
3562 // nexttoward(-0.0, 0.0) = 0.0
3563 if (Result == detail::cmpEqual) {
3564 APFloat Ret(Op1);
3565 Ret.convert(RetTy->getFltSemantics(), detail::rmNearestTiesToEven,
3566 &LosesInfo);
3567 return ConstantFP::get(RetTy->getContext(), Ret);
3568 }
3569
3570 APFloat Next(Op0);
3571 Next.next(/*nextDown=*/Result == APFloat::cmpGreaterThan);
3572 if (Next.isZero() || Next.isDenormal() || Next.isSignaling())
3573 return nullptr;
3574 return ConstantFP::get(RetTy->getContext(), Next);
3575}
3576
3577static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
3579 const TargetLibraryInfo *TLI = nullptr) {
3580 if (!TLI)
3581 return nullptr;
3582
3583 LibFunc Func = TLI->getLibFunc(Name);
3584 if (Func == NotLibFunc)
3585 return nullptr;
3586
3587 const auto *Op1 = dyn_cast<ConstantFP>(Operands[0]);
3588 if (!Op1)
3589 return nullptr;
3590
3591 const auto *Op2 = dyn_cast<ConstantFP>(Operands[1]);
3592 if (!Op2)
3593 return nullptr;
3594
3595 const APFloat &Op1V = Op1->getValueAPF();
3596 const APFloat &Op2V = Op2->getValueAPF();
3597
3598 switch (Func) {
3599 default:
3600 break;
3601 case LibFunc_pow:
3602 case LibFunc_powf:
3603 case LibFunc_pow_finite:
3604 case LibFunc_powf_finite:
3605 if (TLI->has(Func))
3606 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
3607 break;
3608 case LibFunc_fmod:
3609 case LibFunc_fmodf:
3610 if (TLI->has(Func)) {
3611 APFloat V = Op1->getValueAPF();
3612 if (APFloat::opStatus::opOK == V.mod(Op2->getValueAPF()))
3613 return ConstantFP::get(Ty, V);
3614 }
3615 break;
3616 case LibFunc_remainder:
3617 case LibFunc_remainderf:
3618 if (TLI->has(Func)) {
3619 APFloat V = Op1->getValueAPF();
3620 if (APFloat::opStatus::opOK == V.remainder(Op2->getValueAPF()))
3621 return ConstantFP::get(Ty, V);
3622 }
3623 break;
3624 case LibFunc_atan2:
3625 case LibFunc_atan2f:
3626 // atan2(+/-0.0, +/-0.0) is known to raise an exception on some libm
3627 // (Solaris), so we do not assume a known result for that.
3628 if (Op1V.isZero() && Op2V.isZero())
3629 return nullptr;
3630 [[fallthrough]];
3631 case LibFunc_atan2_finite:
3632 case LibFunc_atan2f_finite:
3633 if (TLI->has(Func))
3634 return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
3635 break;
3636 case LibFunc_nextafter:
3637 case LibFunc_nextafterf:
3638 case LibFunc_nexttoward:
3639 case LibFunc_nexttowardf:
3640 if (TLI->has(Func))
3641 return ConstantFoldNextToward(Op1V, Op2V, Ty);
3642 break;
3643 }
3644
3645 return nullptr;
3646}
3647
3648static Constant *ConstantFoldCRC32(Type *Ty, const APInt *CrcArg,
3649 const APInt *DataArg, unsigned DataBytes,
3650 uint32_t Poly) {
3651 if (!CrcArg || !DataArg)
3652 return nullptr;
3653 uint32_t Crc = CrcArg->getZExtValue();
3654 uint64_t Data = DataArg->getZExtValue();
3655 uint32_t Result = calculateReflectedCRC32(Crc, Data, DataBytes, Poly);
3656 return ConstantInt::get(Ty, Result);
3657}
3658
3659static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
3661 const CallBase *Call = nullptr) {
3662 assert(Operands.size() == 2 && "Wrong number of operands.");
3663
3664 if (Ty->isFloatingPointTy()) {
3665 // TODO: We should have undef handling for all of the FP intrinsics that
3666 // are attempted to be folded in this function.
3667 bool IsOp0Undef = isa<UndefValue>(Operands[0]);
3668 bool IsOp1Undef = isa<UndefValue>(Operands[1]);
3669 switch (IntrinsicID) {
3670 case Intrinsic::maxnum:
3671 case Intrinsic::minnum:
3672 case Intrinsic::maximum:
3673 case Intrinsic::minimum:
3674 case Intrinsic::maximumnum:
3675 case Intrinsic::minimumnum:
3676 case Intrinsic::nvvm_fmax_d:
3677 case Intrinsic::nvvm_fmin_d:
3678 // If one argument is undef, return the other argument.
3679 if (IsOp0Undef)
3680 return Operands[1];
3681 if (IsOp1Undef)
3682 return Operands[0];
3683 break;
3684
3685 case Intrinsic::nvvm_fmax_f:
3686 case Intrinsic::nvvm_fmax_ftz_f:
3687 case Intrinsic::nvvm_fmax_ftz_nan_f:
3688 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
3689 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
3690 case Intrinsic::nvvm_fmax_nan_f:
3691 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
3692 case Intrinsic::nvvm_fmax_xorsign_abs_f:
3693
3694 case Intrinsic::nvvm_fmin_f:
3695 case Intrinsic::nvvm_fmin_ftz_f:
3696 case Intrinsic::nvvm_fmin_ftz_nan_f:
3697 case Intrinsic::nvvm_fmin_ftz_nan_xorsign_abs_f:
3698 case Intrinsic::nvvm_fmin_ftz_xorsign_abs_f:
3699 case Intrinsic::nvvm_fmin_nan_f:
3700 case Intrinsic::nvvm_fmin_nan_xorsign_abs_f:
3701 case Intrinsic::nvvm_fmin_xorsign_abs_f:
3702 // If one arg is undef, the other arg can be returned only if it is
3703 // constant, as we may need to flush it to sign-preserving zero or
3704 // canonicalize the NaN.
3705 if (!IsOp0Undef && !IsOp1Undef)
3706 break;
3707 if (auto *Op = dyn_cast<ConstantFP>(Operands[IsOp0Undef ? 1 : 0])) {
3708 if (Op->isNaN()) {
3709 APInt NVCanonicalNaN(32, 0x7fffffff);
3710 return ConstantFP::get(
3711 Ty, APFloat(Ty->getFltSemantics(), NVCanonicalNaN));
3712 }
3713 if (nvvm::FMinFMaxShouldFTZ(IntrinsicID))
3714 return ConstantFP::get(Ty, FTZPreserveSign(Op->getValueAPF()));
3715 else
3716 return Op;
3717 }
3718 break;
3719 }
3720 }
3721
3722 if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
3723 const APFloat &Op1V = Op1->getValueAPF();
3724
3725 if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
3726 if (Op2->getType() != Op1->getType())
3727 return nullptr;
3728 const APFloat &Op2V = Op2->getValueAPF();
3729
3730 if (const auto *ConstrIntr =
3732 RoundingMode RM = getEvaluationRoundingMode(ConstrIntr);
3733 APFloat Res = Op1V;
3735 switch (IntrinsicID) {
3736 default:
3737 return nullptr;
3738 case Intrinsic::experimental_constrained_fadd:
3739 St = Res.add(Op2V, RM);
3740 break;
3741 case Intrinsic::experimental_constrained_fsub:
3742 St = Res.subtract(Op2V, RM);
3743 break;
3744 case Intrinsic::experimental_constrained_fmul:
3745 St = Res.multiply(Op2V, RM);
3746 break;
3747 case Intrinsic::experimental_constrained_fdiv:
3748 St = Res.divide(Op2V, RM);
3749 break;
3750 case Intrinsic::experimental_constrained_frem:
3751 St = Res.mod(Op2V);
3752 break;
3753 case Intrinsic::experimental_constrained_fcmp:
3754 case Intrinsic::experimental_constrained_fcmps:
3755 return evaluateCompare(Op1V, Op2V, ConstrIntr);
3756 }
3757 if (mayFoldConstrained(const_cast<ConstrainedFPIntrinsic *>(ConstrIntr),
3758 St))
3759 return ConstantFP::get(Ty, Res);
3760 return nullptr;
3761 }
3762
3763 switch (IntrinsicID) {
3764 default:
3765 break;
3766 case Intrinsic::copysign:
3767 return ConstantFP::get(Ty, APFloat::copySign(Op1V, Op2V));
3768 case Intrinsic::minnum:
3769 return ConstantFP::get(Ty, minnum(Op1V, Op2V));
3770 case Intrinsic::maxnum:
3771 return ConstantFP::get(Ty, maxnum(Op1V, Op2V));
3772 case Intrinsic::minimum:
3773 return ConstantFP::get(Ty, minimum(Op1V, Op2V));
3774 case Intrinsic::maximum:
3775 return ConstantFP::get(Ty, maximum(Op1V, Op2V));
3776 case Intrinsic::minimumnum:
3777 return ConstantFP::get(Ty, minimumnum(Op1V, Op2V));
3778 case Intrinsic::maximumnum:
3779 return ConstantFP::get(Ty, maximumnum(Op1V, Op2V));
3780
3781 case Intrinsic::nvvm_fmax_d:
3782 case Intrinsic::nvvm_fmax_f:
3783 case Intrinsic::nvvm_fmax_ftz_f:
3784 case Intrinsic::nvvm_fmax_ftz_nan_f:
3785 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
3786 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
3787 case Intrinsic::nvvm_fmax_nan_f:
3788 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
3789 case Intrinsic::nvvm_fmax_xorsign_abs_f:
3790
3791 case Intrinsic::nvvm_fmin_d:
3792 case Intrinsic::nvvm_fmin_f:
3793 case Intrinsic::nvvm_fmin_ftz_f:
3794 case Intrinsic::nvvm_fmin_ftz_nan_f:
3795 case Intrinsic::nvvm_fmin_ftz_nan_xorsign_abs_f:
3796 case Intrinsic::nvvm_fmin_ftz_xorsign_abs_f:
3797 case Intrinsic::nvvm_fmin_nan_f:
3798 case Intrinsic::nvvm_fmin_nan_xorsign_abs_f:
3799 case Intrinsic::nvvm_fmin_xorsign_abs_f: {
3800
3801 bool ShouldCanonicalizeNaNs = !(IntrinsicID == Intrinsic::nvvm_fmax_d ||
3802 IntrinsicID == Intrinsic::nvvm_fmin_d);
3803 bool IsFTZ = nvvm::FMinFMaxShouldFTZ(IntrinsicID);
3804 bool IsNaNPropagating = nvvm::FMinFMaxPropagatesNaNs(IntrinsicID);
3805 bool IsXorSignAbs = nvvm::FMinFMaxIsXorSignAbs(IntrinsicID);
3806
3807 APFloat A = IsFTZ ? FTZPreserveSign(Op1V) : Op1V;
3808 APFloat B = IsFTZ ? FTZPreserveSign(Op2V) : Op2V;
3809
3810 bool XorSign = false;
3811 if (IsXorSignAbs) {
3812 XorSign = A.isNegative() ^ B.isNegative();
3813 A = abs(A);
3814 B = abs(B);
3815 }
3816
3817 bool IsFMax = false;
3818 switch (IntrinsicID) {
3819 case Intrinsic::nvvm_fmax_d:
3820 case Intrinsic::nvvm_fmax_f:
3821 case Intrinsic::nvvm_fmax_ftz_f:
3822 case Intrinsic::nvvm_fmax_ftz_nan_f:
3823 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
3824 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
3825 case Intrinsic::nvvm_fmax_nan_f:
3826 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
3827 case Intrinsic::nvvm_fmax_xorsign_abs_f:
3828 IsFMax = true;
3829 break;
3830 }
3831 APFloat Res =
3832 IsFMax ? (IsNaNPropagating ? maximum(A, B) : maximumnum(A, B))
3833 : (IsNaNPropagating ? minimum(A, B) : minimumnum(A, B));
3834
3835 if (ShouldCanonicalizeNaNs && Res.isNaN()) {
3836 APFloat NVCanonicalNaN(Res.getSemantics(), APInt(32, 0x7fffffff));
3837 return ConstantFP::get(Ty, NVCanonicalNaN);
3838 }
3839
3840 if (IsXorSignAbs && XorSign != Res.isNegative())
3841 Res.changeSign();
3842
3843 return ConstantFP::get(Ty, Res);
3844 }
3845
3846 case Intrinsic::nvvm_div_rm_f:
3847 case Intrinsic::nvvm_div_rn_f:
3848 case Intrinsic::nvvm_div_rp_f:
3849 case Intrinsic::nvvm_div_rz_f:
3850 case Intrinsic::nvvm_div_rm_d:
3851 case Intrinsic::nvvm_div_rn_d:
3852 case Intrinsic::nvvm_div_rp_d:
3853 case Intrinsic::nvvm_div_rz_d:
3854 case Intrinsic::nvvm_div_rm_ftz_f:
3855 case Intrinsic::nvvm_div_rn_ftz_f:
3856 case Intrinsic::nvvm_div_rp_ftz_f:
3857 case Intrinsic::nvvm_div_rz_ftz_f: {
3858 bool IsFTZ = nvvm::FDivShouldFTZ(IntrinsicID);
3859 APFloat A = IsFTZ ? FTZPreserveSign(Op1V) : Op1V;
3860 APFloat B = IsFTZ ? FTZPreserveSign(Op2V) : Op2V;
3861 APFloat::roundingMode RoundMode =
3862 nvvm::GetFDivRoundingMode(IntrinsicID);
3863
3864 APFloat Res = A;
3865 APFloat::opStatus Status = Res.divide(B, RoundMode);
3866 if (!Res.isNaN() &&
3868 Res = IsFTZ ? FTZPreserveSign(Res) : Res;
3869 return ConstantFP::get(Ty, Res);
3870 }
3871 return nullptr;
3872 }
3873 }
3874
3875 if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
3876 return nullptr;
3877
3878 switch (IntrinsicID) {
3879 default:
3880 break;
3881 case Intrinsic::pow:
3882 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
3883 case Intrinsic::amdgcn_fmul_legacy:
3884 // The legacy behaviour is that multiplying +/- 0.0 by anything, even
3885 // NaN or infinity, gives +0.0.
3886 if (Op1V.isZero() || Op2V.isZero())
3887 return ConstantFP::getZero(Ty);
3888 return ConstantFP::get(Ty, Op1V * Op2V);
3889 }
3890
3891 } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
3892 switch (IntrinsicID) {
3893 case Intrinsic::ldexp: {
3894 // APFloat::scalbn takes the exponent as `int`. Clamp wider integer
3895 // exponents into [INT_MIN, INT_MAX] so values still saturate the
3896 // result to +/-inf or +/-0.
3897 APInt Exp = Op2C->getValue();
3898 Exp = Exp.getBitWidth() < 32 ? Exp.sext(32) : Exp.truncSSat(32);
3899 return ConstantFP::get(
3900 Ty->getContext(),
3901 scalbn(Op1V, Exp.getSExtValue(), APFloat::rmNearestTiesToEven));
3902 }
3903 case Intrinsic::is_fpclass: {
3904 FPClassTest Mask = static_cast<FPClassTest>(Op2C->getZExtValue());
3905 bool Result =
3906 ((Mask & fcSNan) && Op1V.isNaN() && Op1V.isSignaling()) ||
3907 ((Mask & fcQNan) && Op1V.isNaN() && !Op1V.isSignaling()) ||
3908 ((Mask & fcNegInf) && Op1V.isNegInfinity()) ||
3909 ((Mask & fcNegNormal) && Op1V.isNormal() && Op1V.isNegative()) ||
3910 ((Mask & fcNegSubnormal) && Op1V.isDenormal() && Op1V.isNegative()) ||
3911 ((Mask & fcNegZero) && Op1V.isZero() && Op1V.isNegative()) ||
3912 ((Mask & fcPosZero) && Op1V.isZero() && !Op1V.isNegative()) ||
3913 ((Mask & fcPosSubnormal) && Op1V.isDenormal() && !Op1V.isNegative()) ||
3914 ((Mask & fcPosNormal) && Op1V.isNormal() && !Op1V.isNegative()) ||
3915 ((Mask & fcPosInf) && Op1V.isPosInfinity());
3916 return ConstantInt::get(Ty, Result);
3917 }
3918 case Intrinsic::powi: {
3919 // Square-and-multiply using the operand's own semantics, matching
3920 // the multiply sequence ExpandPowI builds in SelectionDAG.
3921 int Exp = static_cast<int>(Op2C->getSExtValue());
3922 unsigned UExp = static_cast<unsigned>(Exp);
3923 if (Exp < 0)
3924 UExp = -UExp;
3925 const fltSemantics &Semantics = Op1V.getSemantics();
3926 APFloat Res = APFloat::getOne(Semantics);
3927 APFloat CurSquare = Op1V;
3928 while (UExp) {
3929 if (UExp & 1)
3930 Res = Res * CurSquare;
3931 CurSquare = CurSquare * CurSquare;
3932 UExp >>= 1;
3933 }
3934 if (Exp < 0)
3935 Res = APFloat::getOne(Semantics) / Res;
3936 return ConstantFP::get(Ty, Res);
3937 }
3938 default:
3939 break;
3940 }
3941 }
3942 return nullptr;
3943 }
3944
3945 if (Operands[0]->getType()->isIntegerTy() &&
3946 Operands[1]->getType()->isIntegerTy()) {
3947 const APInt *C0, *C1;
3948 if (!getConstIntOrUndef(Operands[0], C0) ||
3949 !getConstIntOrUndef(Operands[1], C1))
3950 return nullptr;
3951
3952 switch (IntrinsicID) {
3953 default: break;
3954 case Intrinsic::smax:
3955 case Intrinsic::smin:
3956 case Intrinsic::umax:
3957 case Intrinsic::umin:
3958 if (!C0 || !C1)
3959 return MinMaxIntrinsic::getSaturationPoint(IntrinsicID, Ty);
3960 return ConstantInt::get(
3961 Ty, ICmpInst::compare(*C0, *C1,
3962 MinMaxIntrinsic::getPredicate(IntrinsicID))
3963 ? *C0
3964 : *C1);
3965
3966 case Intrinsic::scmp:
3967 case Intrinsic::ucmp:
3968 if (!C0 || !C1)
3969 return ConstantInt::get(Ty, 0);
3970
3971 int Res;
3972 if (IntrinsicID == Intrinsic::scmp)
3973 Res = C0->sgt(*C1) ? 1 : C0->slt(*C1) ? -1 : 0;
3974 else
3975 Res = C0->ugt(*C1) ? 1 : C0->ult(*C1) ? -1 : 0;
3976 return ConstantInt::get(Ty, Res, /*IsSigned=*/true);
3977
3978 case Intrinsic::usub_with_overflow:
3979 case Intrinsic::ssub_with_overflow:
3980 // X - undef -> { 0, false }
3981 // undef - X -> { 0, false }
3982 if (!C0 || !C1)
3983 return Constant::getNullValue(Ty);
3984 [[fallthrough]];
3985 case Intrinsic::uadd_with_overflow:
3986 case Intrinsic::sadd_with_overflow:
3987 // X + undef -> { -1, false }
3988 // undef + x -> { -1, false }
3989 if (!C0 || !C1) {
3990 return ConstantStruct::get(
3991 cast<StructType>(Ty),
3992 {Constant::getAllOnesValue(Ty->getStructElementType(0)),
3993 Constant::getNullValue(Ty->getStructElementType(1))});
3994 }
3995 [[fallthrough]];
3996 case Intrinsic::smul_with_overflow:
3997 case Intrinsic::umul_with_overflow: {
3998 // undef * X -> { 0, false }
3999 // X * undef -> { 0, false }
4000 if (!C0 || !C1)
4001 return Constant::getNullValue(Ty);
4002
4003 APInt Res;
4004 bool Overflow;
4005 switch (IntrinsicID) {
4006 default: llvm_unreachable("Invalid case");
4007 case Intrinsic::sadd_with_overflow:
4008 Res = C0->sadd_ov(*C1, Overflow);
4009 break;
4010 case Intrinsic::uadd_with_overflow:
4011 Res = C0->uadd_ov(*C1, Overflow);
4012 break;
4013 case Intrinsic::ssub_with_overflow:
4014 Res = C0->ssub_ov(*C1, Overflow);
4015 break;
4016 case Intrinsic::usub_with_overflow:
4017 Res = C0->usub_ov(*C1, Overflow);
4018 break;
4019 case Intrinsic::smul_with_overflow:
4020 Res = C0->smul_ov(*C1, Overflow);
4021 break;
4022 case Intrinsic::umul_with_overflow:
4023 Res = C0->umul_ov(*C1, Overflow);
4024 break;
4025 }
4026 Constant *Ops[] = {
4027 ConstantInt::get(Ty->getContext(), Res),
4028 ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow)
4029 };
4031 }
4032 case Intrinsic::uadd_sat:
4033 case Intrinsic::sadd_sat:
4034 if (!C0 || !C1)
4035 return Constant::getAllOnesValue(Ty);
4036 if (IntrinsicID == Intrinsic::uadd_sat)
4037 return ConstantInt::get(Ty, C0->uadd_sat(*C1));
4038 else
4039 return ConstantInt::get(Ty, C0->sadd_sat(*C1));
4040 case Intrinsic::usub_sat:
4041 case Intrinsic::ssub_sat:
4042 if (!C0 || !C1)
4043 return Constant::getNullValue(Ty);
4044 if (IntrinsicID == Intrinsic::usub_sat)
4045 return ConstantInt::get(Ty, C0->usub_sat(*C1));
4046 else
4047 return ConstantInt::get(Ty, C0->ssub_sat(*C1));
4048 case Intrinsic::ushl_sat:
4049 case Intrinsic::sshl_sat:
4050 if (!C0 || !C1)
4051 return nullptr;
4052 if (C1->uge(C1->getBitWidth()))
4053 return PoisonValue::get(Ty);
4054 if (IntrinsicID == Intrinsic::ushl_sat)
4055 return ConstantInt::get(Ty, C0->ushl_sat(*C1));
4056 return ConstantInt::get(Ty, C0->sshl_sat(*C1));
4057 case Intrinsic::cttz:
4058 case Intrinsic::ctlz:
4059 assert(C1 && "Must be constant int");
4060
4061 // cttz(0, 1) and ctlz(0, 1) are poison.
4062 if (C1->isOne() && (!C0 || C0->isZero()))
4063 return PoisonValue::get(Ty);
4064 if (!C0)
4065 return Constant::getNullValue(Ty);
4066 if (IntrinsicID == Intrinsic::cttz)
4067 return ConstantInt::get(Ty, C0->countr_zero());
4068 else
4069 return ConstantInt::get(Ty, C0->countl_zero());
4070
4071 case Intrinsic::abs:
4072 assert(C1 && "Must be constant int");
4073 assert((C1->isOne() || C1->isZero()) && "Must be 0 or 1");
4074
4075 // Undef or minimum val operand with poison min --> poison
4076 if (C1->isOne() && (!C0 || C0->isMinSignedValue()))
4077 return PoisonValue::get(Ty);
4078
4079 // Undef operand with no poison min --> 0 (sign bit must be clear)
4080 if (!C0)
4081 return Constant::getNullValue(Ty);
4082
4083 return ConstantInt::get(Ty, C0->abs());
4084 case Intrinsic::clmul:
4085 if (!C0 || !C1)
4086 return Constant::getNullValue(Ty);
4087 return ConstantInt::get(Ty, APIntOps::clmul(*C0, *C1));
4088 case Intrinsic::pdep:
4089 if (!C0 || !C1)
4090 return Constant::getNullValue(Ty);
4091 return ConstantInt::get(Ty, APIntOps::pdep(*C0, *C1));
4092 case Intrinsic::pext:
4093 if (!C0 || !C1)
4094 return Constant::getNullValue(Ty);
4095 return ConstantInt::get(Ty, APIntOps::pext(*C0, *C1));
4096 case Intrinsic::smulh:
4097 if (!C0 || !C1)
4098 return Constant::getNullValue(Ty);
4099 return ConstantInt::get(Ty, APIntOps::mulhs(*C0, *C1));
4100 case Intrinsic::umulh:
4101 if (!C0 || !C1)
4102 return Constant::getNullValue(Ty);
4103 return ConstantInt::get(Ty, APIntOps::mulhu(*C0, *C1));
4104 case Intrinsic::amdgcn_wave_reduce_add:
4105 case Intrinsic::amdgcn_wave_reduce_sub:
4106 case Intrinsic::amdgcn_wave_reduce_xor: {
4107 if (C0 && C0->isZero())
4108 return Constant::getNullValue(Ty);
4109 return nullptr;
4110 }
4111 case Intrinsic::amdgcn_wave_reduce_umin:
4112 case Intrinsic::amdgcn_wave_reduce_umax:
4113 case Intrinsic::amdgcn_wave_reduce_max:
4114 case Intrinsic::amdgcn_wave_reduce_min:
4115 case Intrinsic::amdgcn_wave_reduce_and:
4116 case Intrinsic::amdgcn_wave_reduce_or:
4117 return Operands[0];
4118 case Intrinsic::aarch64_crc32b:
4119 case Intrinsic::arm_crc32b:
4120 return ConstantFoldCRC32(Ty, C0, C1, 1, 0xEDB88320);
4121 case Intrinsic::aarch64_crc32h:
4122 case Intrinsic::arm_crc32h:
4123 return ConstantFoldCRC32(Ty, C0, C1, 2, 0xEDB88320);
4124 case Intrinsic::aarch64_crc32w:
4125 case Intrinsic::arm_crc32w:
4126 return ConstantFoldCRC32(Ty, C0, C1, 4, 0xEDB88320);
4127 case Intrinsic::aarch64_crc32x:
4128 return ConstantFoldCRC32(Ty, C0, C1, 8, 0xEDB88320);
4129 case Intrinsic::aarch64_crc32cb:
4130 case Intrinsic::arm_crc32cb:
4131 case Intrinsic::x86_sse42_crc32_32_8:
4132 return ConstantFoldCRC32(Ty, C0, C1, 1, 0x82F63B78);
4133 case Intrinsic::aarch64_crc32ch:
4134 case Intrinsic::arm_crc32ch:
4135 case Intrinsic::x86_sse42_crc32_32_16:
4136 return ConstantFoldCRC32(Ty, C0, C1, 2, 0x82F63B78);
4137 case Intrinsic::aarch64_crc32cw:
4138 case Intrinsic::arm_crc32cw:
4139 case Intrinsic::x86_sse42_crc32_32_32:
4140 return ConstantFoldCRC32(Ty, C0, C1, 4, 0x82F63B78);
4141 case Intrinsic::aarch64_crc32cx:
4142 case Intrinsic::x86_sse42_crc32_64_64:
4143 return ConstantFoldCRC32(Ty, C0, C1, 8, 0x82F63B78);
4144 }
4145
4146 return nullptr;
4147 }
4148
4149 // Support ConstantVector in case we have an Undef in the top.
4150 if ((isa<ConstantVector>(Operands[0]) ||
4152 // Check for default rounding mode.
4153 // FIXME: Support other rounding modes?
4155 cast<ConstantInt>(Operands[1])->getValue() == 4) {
4156 auto *Op = cast<Constant>(Operands[0]);
4157 switch (IntrinsicID) {
4158 default: break;
4159 case Intrinsic::x86_avx512_vcvtss2si32:
4160 case Intrinsic::x86_avx512_vcvtss2si64:
4161 case Intrinsic::x86_avx512_vcvtsd2si32:
4162 case Intrinsic::x86_avx512_vcvtsd2si64:
4163 if (ConstantFP *FPOp =
4164 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
4165 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
4166 /*roundTowardZero=*/false, Ty,
4167 /*IsSigned*/true);
4168 break;
4169 case Intrinsic::x86_avx512_vcvtss2usi32:
4170 case Intrinsic::x86_avx512_vcvtss2usi64:
4171 case Intrinsic::x86_avx512_vcvtsd2usi32:
4172 case Intrinsic::x86_avx512_vcvtsd2usi64:
4173 if (ConstantFP *FPOp =
4174 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
4175 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
4176 /*roundTowardZero=*/false, Ty,
4177 /*IsSigned*/false);
4178 break;
4179 case Intrinsic::x86_avx512_cvttss2si:
4180 case Intrinsic::x86_avx512_cvttss2si64:
4181 case Intrinsic::x86_avx512_cvttsd2si:
4182 case Intrinsic::x86_avx512_cvttsd2si64:
4183 if (ConstantFP *FPOp =
4184 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
4185 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
4186 /*roundTowardZero=*/true, Ty,
4187 /*IsSigned*/true);
4188 break;
4189 case Intrinsic::x86_avx512_cvttss2usi:
4190 case Intrinsic::x86_avx512_cvttss2usi64:
4191 case Intrinsic::x86_avx512_cvttsd2usi:
4192 case Intrinsic::x86_avx512_cvttsd2usi64:
4193 if (ConstantFP *FPOp =
4194 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
4195 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
4196 /*roundTowardZero=*/true, Ty,
4197 /*IsSigned*/false);
4198 break;
4199 }
4200 }
4201
4202 if (IntrinsicID == Intrinsic::experimental_cttz_elts) {
4203 auto *FVTy = dyn_cast<FixedVectorType>(Operands[0]->getType());
4204 bool ZeroIsPoison = cast<ConstantInt>(Operands[1])->isOne();
4205 if (!FVTy)
4206 return nullptr;
4207 unsigned Width = Ty->getIntegerBitWidth();
4208 if (APInt::getMaxValue(Width).ult(FVTy->getNumElements()) ||
4209 Operands[0]->containsPoisonElement())
4210 return PoisonValue::get(Ty);
4211 for (unsigned I = 0; I < FVTy->getNumElements(); ++I) {
4212 Constant *Elt = Operands[0]->getAggregateElement(I);
4213 if (!Elt)
4214 return nullptr;
4215 if (isa<UndefValue>(Elt) || Elt->isNullValue())
4216 continue;
4217 return ConstantInt::get(Ty, I);
4218 }
4219 if (ZeroIsPoison)
4220 return PoisonValue::get(Ty);
4221 return ConstantInt::get(Ty, FVTy->getNumElements());
4222 }
4223 return nullptr;
4224}
4225
4226static APFloat ConstantFoldAMDGCNCubeIntrinsic(Intrinsic::ID IntrinsicID,
4227 const APFloat &S0,
4228 const APFloat &S1,
4229 const APFloat &S2) {
4230 unsigned ID;
4231 const fltSemantics &Sem = S0.getSemantics();
4232 APFloat MA(Sem), SC(Sem), TC(Sem);
4233 if (abs(S2) >= abs(S0) && abs(S2) >= abs(S1)) {
4234 if (S2.isNegative() && S2.isNonZero() && !S2.isNaN()) {
4235 // S2 < 0
4236 ID = 5;
4237 SC = -S0;
4238 } else {
4239 ID = 4;
4240 SC = S0;
4241 }
4242 MA = S2;
4243 TC = -S1;
4244 } else if (abs(S1) >= abs(S0)) {
4245 if (S1.isNegative() && S1.isNonZero() && !S1.isNaN()) {
4246 // S1 < 0
4247 ID = 3;
4248 TC = -S2;
4249 } else {
4250 ID = 2;
4251 TC = S2;
4252 }
4253 MA = S1;
4254 SC = S0;
4255 } else {
4256 if (S0.isNegative() && S0.isNonZero() && !S0.isNaN()) {
4257 // S0 < 0
4258 ID = 1;
4259 SC = S2;
4260 } else {
4261 ID = 0;
4262 SC = -S2;
4263 }
4264 MA = S0;
4265 TC = -S1;
4266 }
4267 switch (IntrinsicID) {
4268 default:
4269 llvm_unreachable("unhandled amdgcn cube intrinsic");
4270 case Intrinsic::amdgcn_cubeid:
4271 return APFloat(Sem, ID);
4272 case Intrinsic::amdgcn_cubema:
4273 return MA + MA;
4274 case Intrinsic::amdgcn_cubesc:
4275 return SC;
4276 case Intrinsic::amdgcn_cubetc:
4277 return TC;
4278 }
4279}
4280
4281static Constant *ConstantFoldAMDGCNPermIntrinsic(ArrayRef<Constant *> Operands,
4282 Type *Ty) {
4283 const APInt *C0, *C1, *C2;
4284 if (!getConstIntOrUndef(Operands[0], C0) ||
4285 !getConstIntOrUndef(Operands[1], C1) ||
4286 !getConstIntOrUndef(Operands[2], C2))
4287 return nullptr;
4288
4289 if (!C2)
4290 return UndefValue::get(Ty);
4291
4292 APInt Val(32, 0);
4293 unsigned NumUndefBytes = 0;
4294 for (unsigned I = 0; I < 32; I += 8) {
4295 unsigned Sel = C2->extractBitsAsZExtValue(8, I);
4296 unsigned B = 0;
4297
4298 if (Sel >= 13)
4299 B = 0xff;
4300 else if (Sel == 12)
4301 B = 0x00;
4302 else {
4303 const APInt *Src = ((Sel & 10) == 10 || (Sel & 12) == 4) ? C0 : C1;
4304 if (!Src)
4305 ++NumUndefBytes;
4306 else if (Sel < 8)
4307 B = Src->extractBitsAsZExtValue(8, (Sel & 3) * 8);
4308 else
4309 B = Src->extractBitsAsZExtValue(1, (Sel & 1) ? 31 : 15) * 0xff;
4310 }
4311
4312 Val.insertBits(B, I, 8);
4313 }
4314
4315 if (NumUndefBytes == 4)
4316 return UndefValue::get(Ty);
4317
4318 return ConstantInt::get(Ty, Val);
4319}
4320
4321static Constant *ConstantFoldScalarCall3(StringRef Name,
4322 Intrinsic::ID IntrinsicID, Type *Ty,
4324 const TargetLibraryInfo *TLI = nullptr,
4325 const CallBase *Call = nullptr) {
4326 assert(Operands.size() == 3 && "Wrong number of operands.");
4327
4328 if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
4329 if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
4330 if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
4331 const APFloat &C1 = Op1->getValueAPF();
4332 const APFloat &C2 = Op2->getValueAPF();
4333 const APFloat &C3 = Op3->getValueAPF();
4334
4335 if (const auto *ConstrIntr =
4337 RoundingMode RM = getEvaluationRoundingMode(ConstrIntr);
4338 APFloat Res = C1;
4340 switch (IntrinsicID) {
4341 default:
4342 return nullptr;
4343 case Intrinsic::experimental_constrained_fma:
4344 case Intrinsic::experimental_constrained_fmuladd:
4345 St = Res.fusedMultiplyAdd(C2, C3, RM);
4346 break;
4347 }
4348 if (mayFoldConstrained(
4349 const_cast<ConstrainedFPIntrinsic *>(ConstrIntr), St))
4350 return ConstantFP::get(Ty, Res);
4351 return nullptr;
4352 }
4353
4354 switch (IntrinsicID) {
4355 default: break;
4356 case Intrinsic::amdgcn_fma_legacy: {
4357 // The legacy behaviour is that multiplying +/- 0.0 by anything, even
4358 // NaN or infinity, gives +0.0.
4359 if (C1.isZero() || C2.isZero()) {
4360 // It's tempting to just return C3 here, but that would give the
4361 // wrong result if C3 was -0.0.
4362 return ConstantFP::get(Ty, APFloat(0.0f) + C3);
4363 }
4364 [[fallthrough]];
4365 }
4366 case Intrinsic::fma:
4367 case Intrinsic::fmuladd: {
4368 APFloat V = C1;
4369 V.fusedMultiplyAdd(C2, C3, APFloat::rmNearestTiesToEven);
4370 return ConstantFP::get(Ty, V);
4371 }
4372
4373 case Intrinsic::amdgcn_cubeid:
4374 case Intrinsic::amdgcn_cubema:
4375 case Intrinsic::amdgcn_cubesc:
4376 case Intrinsic::amdgcn_cubetc: {
4377 APFloat V = ConstantFoldAMDGCNCubeIntrinsic(IntrinsicID, C1, C2, C3);
4378 return ConstantFP::get(Ty, V);
4379 }
4380 }
4381 }
4382
4383 // TODO: Add constant folding for the _sat variants.
4384 const bool IsFAdd = IntrinsicID == Intrinsic::nvvm_fadd ||
4385 IntrinsicID == Intrinsic::nvvm_fadd_ftz;
4386 const bool IsFMul = IntrinsicID == Intrinsic::nvvm_fmul ||
4387 IntrinsicID == Intrinsic::nvvm_fmul_ftz;
4388 if (IsFAdd || IsFMul) {
4389 bool IsFTZ = IntrinsicID == Intrinsic::nvvm_fadd_ftz ||
4390 IntrinsicID == Intrinsic::nvvm_fmul_ftz;
4391 APFloat A =
4392 IsFTZ ? FTZPreserveSign(Op1->getValueAPF()) : Op1->getValueAPF();
4393 APFloat B =
4394 IsFTZ ? FTZPreserveSign(Op2->getValueAPF()) : Op2->getValueAPF();
4395
4396 APFloat::roundingMode RoundMode =
4398
4399 APFloat Res = A;
4401 IsFAdd ? Res.add(B, RoundMode) : Res.multiply(B, RoundMode);
4402
4403 if (!Res.isNaN() &&
4405 Res = IsFTZ ? FTZPreserveSign(Res) : Res;
4406 return ConstantFP::get(Ty, Res);
4407 }
4408 return nullptr;
4409 }
4410 }
4411 }
4412
4413 if (IntrinsicID == Intrinsic::smul_fix ||
4414 IntrinsicID == Intrinsic::smul_fix_sat) {
4415 const APInt *C0, *C1;
4416 if (!getConstIntOrUndef(Operands[0], C0) ||
4417 !getConstIntOrUndef(Operands[1], C1))
4418 return nullptr;
4419
4420 // undef * C -> 0
4421 // C * undef -> 0
4422 if (!C0 || !C1)
4423 return Constant::getNullValue(Ty);
4424
4425 // This code performs rounding towards negative infinity in case the result
4426 // cannot be represented exactly for the given scale. Targets that do care
4427 // about rounding should use a target hook for specifying how rounding
4428 // should be done, and provide their own folding to be consistent with
4429 // rounding. This is the same approach as used by
4430 // DAGTypeLegalizer::ExpandIntRes_MULFIX.
4431 unsigned Scale = cast<ConstantInt>(Operands[2])->getZExtValue();
4432 unsigned Width = C0->getBitWidth();
4433 assert(Scale < Width && "Illegal scale.");
4434 unsigned ExtendedWidth = Width * 2;
4435 APInt Product =
4436 (C0->sext(ExtendedWidth) * C1->sext(ExtendedWidth)).ashr(Scale);
4437 if (IntrinsicID == Intrinsic::smul_fix_sat) {
4438 APInt Max = APInt::getSignedMaxValue(Width).sext(ExtendedWidth);
4439 APInt Min = APInt::getSignedMinValue(Width).sext(ExtendedWidth);
4440 Product = APIntOps::smin(Product, Max);
4441 Product = APIntOps::smax(Product, Min);
4442 }
4443 return ConstantInt::get(Ty->getContext(), Product.sextOrTrunc(Width));
4444 }
4445
4446 if (IntrinsicID == Intrinsic::fshl || IntrinsicID == Intrinsic::fshr) {
4447 const APInt *C0, *C1, *C2;
4448 if (!getConstIntOrUndef(Operands[0], C0) ||
4449 !getConstIntOrUndef(Operands[1], C1) ||
4450 !getConstIntOrUndef(Operands[2], C2))
4451 return nullptr;
4452
4453 bool IsRight = IntrinsicID == Intrinsic::fshr;
4454 if (!C2)
4455 return Operands[IsRight ? 1 : 0];
4456 if (!C0 && !C1)
4457 return UndefValue::get(Ty);
4458
4459 // The shift amount is interpreted as modulo the bitwidth. If the shift
4460 // amount is effectively 0, avoid UB due to oversized inverse shift below.
4461 unsigned BitWidth = C2->getBitWidth();
4462 unsigned ShAmt = C2->urem(BitWidth);
4463 if (!ShAmt)
4464 return Operands[IsRight ? 1 : 0];
4465
4466 // (C0 << ShlAmt) | (C1 >> LshrAmt)
4467 unsigned LshrAmt = IsRight ? ShAmt : BitWidth - ShAmt;
4468 unsigned ShlAmt = !IsRight ? ShAmt : BitWidth - ShAmt;
4469 if (!C0)
4470 return ConstantInt::get(Ty, C1->lshr(LshrAmt));
4471 if (!C1)
4472 return ConstantInt::get(Ty, C0->shl(ShlAmt));
4473 return ConstantInt::get(Ty, C0->shl(ShlAmt) | C1->lshr(LshrAmt));
4474 }
4475
4476 if (IntrinsicID == Intrinsic::amdgcn_perm)
4477 return ConstantFoldAMDGCNPermIntrinsic(Operands, Ty);
4478
4479 return nullptr;
4480}
4481
4482static Constant *ConstantFoldScalarCall4(StringRef Name,
4483 Intrinsic::ID IntrinsicID, Type *Ty,
4485 const TargetLibraryInfo *TLI = nullptr,
4486 const CallBase *Call = nullptr) {
4487 assert(Operands.size() == 4 && "Wrong number of operands.");
4488
4489 // TODO: Add constant folding for the _sat and _relu variants.
4490 if (IntrinsicID == Intrinsic::nvvm_fma ||
4491 IntrinsicID == Intrinsic::nvvm_fma_ftz) {
4492 const auto *Op1 = dyn_cast<ConstantFP>(Operands[0]);
4493 const auto *Op2 = dyn_cast<ConstantFP>(Operands[1]);
4494 const auto *Op3 = dyn_cast<ConstantFP>(Operands[2]);
4495 if (!Op1 || !Op2 || !Op3)
4496 return nullptr;
4497
4498 bool IsFTZ = nvvm::FPArithShouldFTZ(IntrinsicID);
4499 APFloat A =
4500 IsFTZ ? FTZPreserveSign(Op1->getValueAPF()) : Op1->getValueAPF();
4501 APFloat B =
4502 IsFTZ ? FTZPreserveSign(Op2->getValueAPF()) : Op2->getValueAPF();
4503 APFloat C =
4504 IsFTZ ? FTZPreserveSign(Op3->getValueAPF()) : Op3->getValueAPF();
4505
4506 APFloat::roundingMode RoundMode =
4508
4509 APFloat Res = A;
4510 APFloat::opStatus Status = Res.fusedMultiplyAdd(B, C, RoundMode);
4511
4512 if (!Res.isNaN() &&
4514 Res = IsFTZ ? FTZPreserveSign(Res) : Res;
4515 return ConstantFP::get(Ty, Res);
4516 }
4517 return nullptr;
4518 }
4519
4520 return nullptr;
4521}
4522
4523static Constant *ConstantFoldScalarCall(StringRef Name,
4524 Intrinsic::ID IntrinsicID, Type *Ty,
4526 const TargetLibraryInfo *TLI = nullptr,
4527 const CallBase *Call = nullptr) {
4528 if (IntrinsicID != Intrinsic::not_intrinsic &&
4530 intrinsicPropagatesPoison(IntrinsicID))
4531 return PoisonValue::get(Ty);
4532
4533 if (Operands.size() == 1)
4534 return ConstantFoldScalarCall1(Name, IntrinsicID, Ty, Operands, TLI, Call);
4535
4536 if (Operands.size() == 2) {
4537 if (Constant *FoldedLibCall =
4538 ConstantFoldLibCall2(Name, Ty, Operands, TLI)) {
4539 return FoldedLibCall;
4540 }
4541 return ConstantFoldIntrinsicCall2(IntrinsicID, Ty, Operands, Call);
4542 }
4543
4544 if (Operands.size() == 3)
4545 return ConstantFoldScalarCall3(Name, IntrinsicID, Ty, Operands, TLI, Call);
4546
4547 if (Operands.size() == 4)
4548 return ConstantFoldScalarCall4(Name, IntrinsicID, Ty, Operands, TLI, Call);
4549
4550 return nullptr;
4551}
4552
4553static Constant *ConstantFoldFixedVectorCall(
4554 StringRef Name, Intrinsic::ID IntrinsicID, FixedVectorType *FVTy,
4556 const TargetLibraryInfo *TLI = nullptr, const CallBase *Call = nullptr) {
4559 Type *Ty = FVTy->getElementType();
4560
4561 switch (IntrinsicID) {
4562 case Intrinsic::masked_load: {
4563 auto *SrcPtr = Operands[0];
4564 auto *Mask = Operands[1];
4565 auto *Passthru = Operands[2];
4566
4567 Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, FVTy, DL);
4568
4569 SmallVector<Constant *, 32> NewElements;
4570 for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
4571 auto *MaskElt = Mask->getAggregateElement(I);
4572 if (!MaskElt)
4573 break;
4574 auto *PassthruElt = Passthru->getAggregateElement(I);
4575 auto *VecElt = VecData ? VecData->getAggregateElement(I) : nullptr;
4576 if (isa<UndefValue>(MaskElt)) {
4577 if (PassthruElt)
4578 NewElements.push_back(PassthruElt);
4579 else if (VecElt)
4580 NewElements.push_back(VecElt);
4581 else
4582 return nullptr;
4583 }
4584 if (MaskElt->isNullValue()) {
4585 if (!PassthruElt)
4586 return nullptr;
4587 NewElements.push_back(PassthruElt);
4588 } else if (MaskElt->isOneValue()) {
4589 if (!VecElt)
4590 return nullptr;
4591 NewElements.push_back(VecElt);
4592 } else {
4593 return nullptr;
4594 }
4595 }
4596 if (NewElements.size() != FVTy->getNumElements())
4597 return nullptr;
4598 return ConstantVector::get(NewElements);
4599 }
4600 case Intrinsic::arm_mve_vctp8:
4601 case Intrinsic::arm_mve_vctp16:
4602 case Intrinsic::arm_mve_vctp32:
4603 case Intrinsic::arm_mve_vctp64: {
4604 if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
4605 unsigned Lanes = FVTy->getNumElements();
4606 uint64_t Limit = Op->getZExtValue();
4607
4609 for (unsigned i = 0; i < Lanes; i++) {
4610 if (i < Limit)
4612 else
4614 }
4615 return ConstantVector::get(NCs);
4616 }
4617 return nullptr;
4618 }
4619 case Intrinsic::get_active_lane_mask: {
4620 auto *Op0 = dyn_cast<ConstantInt>(Operands[0]);
4621 auto *Op1 = dyn_cast<ConstantInt>(Operands[1]);
4622 if (Op0 && Op1) {
4623 unsigned Lanes = FVTy->getNumElements();
4624 APInt Base = Op0->getValue();
4625 APInt Limit = Op1->getValue();
4626
4628 for (unsigned I = 0; I < Lanes; I++) {
4629 bool Overflow;
4630 if (Base.uadd_ov(APInt(Base.getBitWidth(), I), Overflow).ult(Limit) &&
4631 !Overflow)
4633 else
4635 }
4636 return ConstantVector::get(NCs);
4637 }
4638 return nullptr;
4639 }
4640 case Intrinsic::mask_beforefirst: {
4643 Constant *Current = True;
4644 for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
4645 Constant *Elt = Operands[0]->getAggregateElement(I);
4647 return PoisonValue::get(FVTy);
4648 auto *CI = dyn_cast_or_null<ConstantInt>(Elt);
4649 if (!CI)
4650 return nullptr;
4651 if (CI->isOne())
4652 Current = False;
4653 Result[I] = Current;
4654 }
4655 return ConstantVector::get(Result);
4656 }
4657 case Intrinsic::vector_extract: {
4659 Constant *Vec = Operands[0];
4660 if (!Idx || !isa<FixedVectorType>(Vec->getType()))
4661 return nullptr;
4662
4663 unsigned NumElements = FVTy->getNumElements();
4664 unsigned VecNumElements =
4665 cast<FixedVectorType>(Vec->getType())->getNumElements();
4666 unsigned StartingIndex = Idx->getZExtValue();
4667
4668 // Extracting entire vector is nop
4669 if (NumElements == VecNumElements && StartingIndex == 0)
4670 return Vec;
4671
4672 for (unsigned I = StartingIndex, E = StartingIndex + NumElements; I < E;
4673 ++I) {
4674 Constant *Elt = Vec->getAggregateElement(I);
4675 if (!Elt)
4676 return nullptr;
4677 Result[I - StartingIndex] = Elt;
4678 }
4679
4680 return ConstantVector::get(Result);
4681 }
4682 case Intrinsic::vector_insert: {
4683 Constant *Vec = Operands[0];
4684 Constant *SubVec = Operands[1];
4686 if (!Idx || !isa<FixedVectorType>(Vec->getType()))
4687 return nullptr;
4688
4689 unsigned SubVecNumElements =
4690 cast<FixedVectorType>(SubVec->getType())->getNumElements();
4691 unsigned VecNumElements =
4692 cast<FixedVectorType>(Vec->getType())->getNumElements();
4693 unsigned IdxN = Idx->getZExtValue();
4694 // Replacing entire vector with a subvec is nop
4695 if (SubVecNumElements == VecNumElements && IdxN == 0)
4696 return SubVec;
4697
4698 for (unsigned I = 0; I < VecNumElements; ++I) {
4699 Constant *Elt;
4700 if (I < IdxN + SubVecNumElements)
4701 Elt = SubVec->getAggregateElement(I - IdxN);
4702 else
4703 Elt = Vec->getAggregateElement(I);
4704 if (!Elt)
4705 return nullptr;
4706 Result[I] = Elt;
4707 }
4708 return ConstantVector::get(Result);
4709 }
4710 case Intrinsic::vector_interleave2:
4711 case Intrinsic::vector_interleave3:
4712 case Intrinsic::vector_interleave4:
4713 case Intrinsic::vector_interleave5:
4714 case Intrinsic::vector_interleave6:
4715 case Intrinsic::vector_interleave7:
4716 case Intrinsic::vector_interleave8: {
4717 unsigned NumElements =
4718 cast<FixedVectorType>(Operands[0]->getType())->getNumElements();
4719 unsigned NumOperands = Operands.size();
4720 for (unsigned I = 0; I < NumElements; ++I) {
4721 for (unsigned J = 0; J < NumOperands; ++J) {
4722 Constant *Elt = Operands[J]->getAggregateElement(I);
4723 if (!Elt)
4724 return nullptr;
4725 Result[NumOperands * I + J] = Elt;
4726 }
4727 }
4728 return ConstantVector::get(Result);
4729 }
4730 case Intrinsic::vector_partial_reduce_add:
4731 return constantFoldVectorPartialReduceAdd(Operands[0], Operands[1], DL);
4732 case Intrinsic::wasm_dot: {
4733 unsigned NumElements =
4734 cast<FixedVectorType>(Operands[0]->getType())->getNumElements();
4735
4736 assert(NumElements == 8 && Result.size() == 4 &&
4737 "wasm dot takes i16x8 and produces i32x4");
4738 assert(Ty->isIntegerTy());
4739 int32_t MulVector[8];
4740
4741 for (unsigned I = 0; I < NumElements; ++I) {
4742 ConstantInt *Elt0 =
4743 dyn_cast<ConstantInt>(Operands[0]->getAggregateElement(I));
4744 ConstantInt *Elt1 =
4745 dyn_cast<ConstantInt>(Operands[1]->getAggregateElement(I));
4746
4747 if (!Elt0 || !Elt1)
4748 return nullptr;
4749
4750 MulVector[I] = Elt0->getSExtValue() * Elt1->getSExtValue();
4751 }
4752 for (unsigned I = 0; I < Result.size(); I++) {
4753 int64_t IAdd = (int64_t)MulVector[I * 2] + (int64_t)MulVector[I * 2 + 1];
4754 Result[I] = ConstantInt::getSigned(Ty, IAdd, /*ImplicitTrunc=*/true);
4755 }
4756
4757 return ConstantVector::get(Result);
4758 }
4759 case Intrinsic::nvvm_fadd:
4760 case Intrinsic::nvvm_fadd_ftz:
4761 case Intrinsic::nvvm_fma:
4762 case Intrinsic::nvvm_fma_ftz:
4763 case Intrinsic::nvvm_fmul:
4764 case Intrinsic::nvvm_fmul_ftz:
4765 // The rounding mode operand is a scalar, so the lane-wise folding below
4766 // does not apply.
4767 // TODO: Fold these by passing the rounding mode through to every lane.
4768 return nullptr;
4769 default:
4770 break;
4771 }
4772
4773 for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
4774 // Gather a column of constants.
4775 for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
4776 // Some intrinsics use a scalar type for certain arguments.
4777 if (isVectorIntrinsicWithScalarOpAtArg(IntrinsicID, J, /*TTI=*/nullptr)) {
4778 Lane[J] = Operands[J];
4779 continue;
4780 }
4781
4782 Constant *Agg = Operands[J]->getAggregateElement(I);
4783 if (!Agg)
4784 return nullptr;
4785
4786 Lane[J] = Agg;
4787 }
4788
4789 // Use the regular scalar folding to simplify this column.
4790 Constant *Folded =
4791 ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI, Call);
4792 if (!Folded)
4793 return nullptr;
4794 Result[I] = Folded;
4795 }
4796
4797 return ConstantVector::get(Result);
4798}
4799
4800static Constant *ConstantFoldScalableVectorCall(
4801 StringRef Name, Intrinsic::ID IntrinsicID, ScalableVectorType *SVTy,
4803 const TargetLibraryInfo *TLI, const CallBase *Call) {
4804 switch (IntrinsicID) {
4805 case Intrinsic::aarch64_sve_convert_from_svbool: {
4806 Constant *Src = Operands[0];
4807 if (!Src->isNullValue())
4808 break;
4809
4810 return ConstantInt::getFalse(SVTy);
4811 }
4812 case Intrinsic::get_active_lane_mask: {
4813 auto *Op0 = dyn_cast<ConstantInt>(Operands[0]);
4814 auto *Op1 = dyn_cast<ConstantInt>(Operands[1]);
4815 if (Op0 && Op1 && Op0->getValue().uge(Op1->getValue()))
4816 return ConstantVector::getNullValue(SVTy);
4817 break;
4818 }
4819 case Intrinsic::mask_beforefirst: {
4820 Constant *SplatVal = Operands[0]->getSplatValue();
4821 if (isa_and_nonnull<PoisonValue>(SplatVal))
4822 return PoisonValue::get(SVTy);
4823 if (auto *CI = dyn_cast_if_present<ConstantInt>(SplatVal))
4824 return ConstantInt::getBool(SVTy, CI->isZero());
4825 break;
4826 }
4827 case Intrinsic::vector_interleave2:
4828 case Intrinsic::vector_interleave3:
4829 case Intrinsic::vector_interleave4:
4830 case Intrinsic::vector_interleave5:
4831 case Intrinsic::vector_interleave6:
4832 case Intrinsic::vector_interleave7:
4833 case Intrinsic::vector_interleave8: {
4834 Constant *SplatVal = Operands[0]->getSplatValue();
4835 if (!SplatVal)
4836 return nullptr;
4837
4839 return nullptr;
4840
4841 return ConstantVector::getSplat(SVTy->getElementCount(), SplatVal);
4842 }
4843 default:
4844 break;
4845 }
4846
4847 // If trivially vectorizable, try folding it via the scalar call if all
4848 // operands are splats.
4849
4850 // TODO: ConstantFoldFixedVectorCall should probably check this too?
4851 if (!isTriviallyVectorizable(IntrinsicID))
4852 return nullptr;
4853
4855 for (auto [I, Op] : enumerate(Operands)) {
4856 if (isVectorIntrinsicWithScalarOpAtArg(IntrinsicID, I, /*TTI=*/nullptr)) {
4857 SplatOps.push_back(Op);
4858 continue;
4859 }
4860 Constant *Splat = Op->getSplatValue();
4861 if (!Splat)
4862 return nullptr;
4863 SplatOps.push_back(Splat);
4864 }
4865 Constant *Folded = ConstantFoldScalarCall(
4866 Name, IntrinsicID, SVTy->getElementType(), SplatOps, TLI, Call);
4867 if (!Folded)
4868 return nullptr;
4870}
4871
4872static std::pair<Constant *, Constant *>
4873ConstantFoldScalarFrexpCall(Constant *Op, Type *IntTy) {
4874 auto *ConstFP = dyn_cast<ConstantFP>(Op);
4875 if (!ConstFP)
4876 return {};
4877
4878 const APFloat &U = ConstFP->getValueAPF();
4879 int FrexpExp;
4880 APFloat FrexpMant = frexp(U, FrexpExp, APFloat::rmNearestTiesToEven);
4881 Constant *Result0 = ConstantFP::get(ConstFP->getType(), FrexpMant);
4882
4883 // The exponent is an "unspecified value" for inf/nan. We use zero to avoid
4884 // using undef.
4885 Constant *Result1 = FrexpMant.isFinite()
4886 ? ConstantInt::getSigned(IntTy, FrexpExp)
4887 : ConstantInt::getNullValue(IntTy);
4888 return {Result0, Result1};
4889}
4890
4891/// Handle intrinsics that return tuples, which may be tuples of vectors.
4892static Constant *
4893ConstantFoldStructCall(StringRef Name, Intrinsic::ID IntrinsicID,
4895 const DataLayout &DL, const TargetLibraryInfo *TLI,
4896 const CallBase *Call) {
4897
4898 switch (IntrinsicID) {
4899 case Intrinsic::frexp: {
4900 Type *Ty0 = StTy->getContainedType(0);
4901 Type *Ty1 = StTy->getContainedType(1)->getScalarType();
4902
4903 if (auto *FVTy0 = dyn_cast<FixedVectorType>(Ty0)) {
4904 SmallVector<Constant *, 4> Results0(FVTy0->getNumElements());
4905 SmallVector<Constant *, 4> Results1(FVTy0->getNumElements());
4906
4907 for (unsigned I = 0, E = FVTy0->getNumElements(); I != E; ++I) {
4908 Constant *Lane = Operands[0]->getAggregateElement(I);
4909 std::tie(Results0[I], Results1[I]) =
4910 ConstantFoldScalarFrexpCall(Lane, Ty1);
4911 if (!Results0[I])
4912 return nullptr;
4913 }
4914
4915 return ConstantStruct::get(StTy, ConstantVector::get(Results0),
4916 ConstantVector::get(Results1));
4917 }
4918
4919 auto [Result0, Result1] = ConstantFoldScalarFrexpCall(Operands[0], Ty1);
4920 if (!Result0)
4921 return nullptr;
4922 return ConstantStruct::get(StTy, Result0, Result1);
4923 }
4924 case Intrinsic::sincos: {
4925 Type *Ty = StTy->getContainedType(0);
4926 Type *TyScalar = Ty->getScalarType();
4927
4928 auto ConstantFoldScalarSincosCall =
4929 [&](Constant *Op) -> std::pair<Constant *, Constant *> {
4930 Constant *SinResult =
4931 ConstantFoldScalarCall(Name, Intrinsic::sin, TyScalar, Op, TLI, Call);
4932 Constant *CosResult =
4933 ConstantFoldScalarCall(Name, Intrinsic::cos, TyScalar, Op, TLI, Call);
4934 return std::make_pair(SinResult, CosResult);
4935 };
4936
4937 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4938 SmallVector<Constant *> SinResults(FVTy->getNumElements());
4939 SmallVector<Constant *> CosResults(FVTy->getNumElements());
4940
4941 for (unsigned I = 0, E = FVTy->getNumElements(); I != E; ++I) {
4942 Constant *Lane = Operands[0]->getAggregateElement(I);
4943 std::tie(SinResults[I], CosResults[I]) =
4944 ConstantFoldScalarSincosCall(Lane);
4945 if (!SinResults[I] || !CosResults[I])
4946 return nullptr;
4947 }
4948
4949 return ConstantStruct::get(StTy, ConstantVector::get(SinResults),
4950 ConstantVector::get(CosResults));
4951 }
4952
4953 if (!Ty->isFloatingPointTy())
4954 return nullptr;
4955
4956 auto [SinResult, CosResult] = ConstantFoldScalarSincosCall(Operands[0]);
4957 if (!SinResult || !CosResult)
4958 return nullptr;
4959 return ConstantStruct::get(StTy, SinResult, CosResult);
4960 }
4961 case Intrinsic::vector_deinterleave2:
4962 case Intrinsic::vector_deinterleave3:
4963 case Intrinsic::vector_deinterleave4:
4964 case Intrinsic::vector_deinterleave5:
4965 case Intrinsic::vector_deinterleave6:
4966 case Intrinsic::vector_deinterleave7:
4967 case Intrinsic::vector_deinterleave8: {
4968 unsigned NumResults = StTy->getNumElements();
4969 auto *Vec = Operands[0];
4970 auto *VecTy = cast<VectorType>(Vec->getType());
4971
4972 ElementCount ResultEC =
4973 VecTy->getElementCount().divideCoefficientBy(NumResults);
4974
4975 if (auto *EltC = Vec->getSplatValue()) {
4976 auto *ResultVec = ConstantVector::getSplat(ResultEC, EltC);
4977 SmallVector<Constant *, 8> Results(NumResults, ResultVec);
4978 return ConstantStruct::get(StTy, Results);
4979 }
4980
4981 if (!ResultEC.isFixed())
4982 return nullptr;
4983
4984 unsigned NumElements = ResultEC.getFixedValue();
4986 SmallVector<Constant *> Elements(NumElements);
4987 for (unsigned I = 0; I != NumResults; ++I) {
4988 for (unsigned J = 0; J != NumElements; ++J) {
4989 Constant *Elt = Vec->getAggregateElement(J * NumResults + I);
4990 if (!Elt)
4991 return nullptr;
4992 Elements[J] = Elt;
4993 }
4994 Results[I] = ConstantVector::get(Elements);
4995 }
4996 return ConstantStruct::get(StTy, Results);
4997 }
4998 default:
4999 // TODO: Constant folding of vector intrinsics that fall through here does
5000 // not work (e.g. overflow intrinsics)
5001 return ConstantFoldScalarCall(Name, IntrinsicID, StTy, Operands, TLI, Call);
5002 }
5003
5004 return nullptr;
5005}
5006
5007} // end anonymous namespace
5008
5009Constant *llvm::ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
5010 Type *Ty, DenormalMode DenormMode) {
5011 assert(DenormMode.isValid() && "invalid denormal mode");
5014 return nullptr;
5015
5016 llvm_fenv_clearexcept();
5017 APFloat Input = FlushWithDenormKind(V, DenormMode.Input);
5018 double Result = NativeFP(Input.convertToDouble());
5019 if (llvm_fenv_testexcept()) {
5020 llvm_fenv_clearexcept();
5021 return nullptr;
5022 }
5023
5024 Constant *Output = GetConstantFoldFPValue(Result, Ty);
5026 return Output;
5027 auto *CFP = cast<ConstantFP>(Output);
5028 APFloat Res = FlushWithDenormKind(CFP->getValueAPF(), DenormMode.Output);
5029 return ConstantFP::get(Ty->getContext(), Res);
5030}
5031
5034 const DataLayout &DL,
5035 const Function *CtxF) {
5036 // In the absence of CtxF, assume strictfp conservatively.
5037 if (!canConstantFoldIntrinsic(ID, CtxF ? CtxF->isStrictFP() : true) ||
5040 Ty, ArrayRef<Value *>((Value *const *)Ops.data(), Ops.size()))))
5041 return nullptr;
5042 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty))
5043 return ConstantFoldFixedVectorCall("", ID, FVTy, Ops, DL);
5044 return ConstantFoldScalarCall("", ID, Ty, Ops);
5045}
5046
5049 const TargetLibraryInfo *TLI,
5050 bool AllowNonDeterministic) {
5051 if (Call->isNoBuiltin())
5052 return nullptr;
5053 if (!F->hasName())
5054 return nullptr;
5055
5056 // If this is not an intrinsic and not recognized as a library call, bail out.
5057 Intrinsic::ID IID = F->getIntrinsicID();
5058 if (IID == Intrinsic::not_intrinsic) {
5059 if (!TLI)
5060 return nullptr;
5061 if (TLI->getLibFunc(*F) == NotLibFunc)
5062 return nullptr;
5063 }
5064
5065 // Conservatively assume that floating-point libcalls may be
5066 // non-deterministic.
5067 Type *Ty = F->getReturnType();
5068 if (!AllowNonDeterministic && Ty->isFPOrFPVectorTy())
5069 return nullptr;
5070
5071 StringRef Name = F->getName();
5072 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty))
5073 return ConstantFoldFixedVectorCall(
5074 Name, IID, FVTy, Operands, F->getDataLayout(), TLI, Call);
5075
5076 if (auto *SVTy = dyn_cast<ScalableVectorType>(Ty))
5077 return ConstantFoldScalableVectorCall(
5078 Name, IID, SVTy, Operands, F->getDataLayout(), TLI, Call);
5079
5080 if (auto *StTy = dyn_cast<StructType>(Ty))
5081 return ConstantFoldStructCall(Name, IID, StTy, Operands,
5082 F->getDataLayout(), TLI, Call);
5083
5084 // TODO: If this is a library function, we already discovered that above,
5085 // so we should pass the LibFunc, not the name (and it might be better
5086 // still to separate intrinsic handling from libcalls).
5087 return ConstantFoldScalarCall(Name, IID, Ty, Operands, TLI, Call);
5088}
5089
5091 const TargetLibraryInfo *TLI) {
5092 // FIXME: Refactor this code; this duplicates logic in LibCallsShrinkWrap
5093 // (and to some extent ConstantFoldScalarCall).
5094 if (Call->isNoBuiltin() || Call->isStrictFP())
5095 return false;
5096 Function *F = Call->getCalledFunction();
5097 if (!F)
5098 return false;
5099
5100 if (!TLI)
5101 return false;
5102
5103 LibFunc Func = TLI->getLibFunc(*F);
5104 if (Func == NotLibFunc)
5105 return false;
5106
5107 if (Call->arg_size() == 1) {
5108 if (ConstantFP *OpC = dyn_cast<ConstantFP>(Call->getArgOperand(0))) {
5109 const APFloat &Op = OpC->getValueAPF();
5110 switch (Func) {
5111 case LibFunc_logl:
5112 case LibFunc_log:
5113 case LibFunc_logf:
5114 case LibFunc_log2l:
5115 case LibFunc_log2:
5116 case LibFunc_log2f:
5117 case LibFunc_log10l:
5118 case LibFunc_log10:
5119 case LibFunc_log10f:
5120 return Op.isNaN() || (!Op.isZero() && !Op.isNegative());
5121
5122 case LibFunc_ilogb:
5123 return !Op.isNaN() && !Op.isZero() && !Op.isInfinity();
5124
5125 case LibFunc_expl:
5126 case LibFunc_exp:
5127 case LibFunc_expf:
5128 // FIXME: These boundaries are slightly conservative.
5129 if (OpC->getType()->isDoubleTy())
5130 return !(Op < APFloat(-745.0) || Op > APFloat(709.0));
5131 if (OpC->getType()->isFloatTy())
5132 return !(Op < APFloat(-103.0f) || Op > APFloat(88.0f));
5133 break;
5134
5135 case LibFunc_exp2l:
5136 case LibFunc_exp2:
5137 case LibFunc_exp2f:
5138 // FIXME: These boundaries are slightly conservative.
5139 if (OpC->getType()->isDoubleTy())
5140 return !(Op < APFloat(-1074.0) || Op > APFloat(1023.0));
5141 if (OpC->getType()->isFloatTy())
5142 return !(Op < APFloat(-149.0f) || Op > APFloat(127.0f));
5143 break;
5144
5145 case LibFunc_sinl:
5146 case LibFunc_sin:
5147 case LibFunc_sinf:
5148 case LibFunc_cosl:
5149 case LibFunc_cos:
5150 case LibFunc_cosf:
5151 return !Op.isInfinity();
5152
5153 case LibFunc_tanl:
5154 case LibFunc_tan:
5155 case LibFunc_tanf: {
5156 // FIXME: Stop using the host math library.
5157 // FIXME: The computation isn't done in the right precision.
5158 Type *Ty = OpC->getType();
5159 if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy())
5160 return ConstantFoldFP(tan, OpC->getValueAPF(), Ty) != nullptr;
5161 break;
5162 }
5163
5164 case LibFunc_atan:
5165 case LibFunc_atanf:
5166 case LibFunc_atanl:
5167 // Per POSIX, this MAY fail if Op is denormal. We choose not failing.
5168 return true;
5169
5170 case LibFunc_asinl:
5171 case LibFunc_asin:
5172 case LibFunc_asinf:
5173 case LibFunc_acosl:
5174 case LibFunc_acos:
5175 case LibFunc_acosf:
5176 return !(Op < APFloat::getOne(Op.getSemantics(), true) ||
5177 Op > APFloat::getOne(Op.getSemantics()));
5178
5179 case LibFunc_sinh:
5180 case LibFunc_cosh:
5181 case LibFunc_sinhf:
5182 case LibFunc_coshf:
5183 case LibFunc_sinhl:
5184 case LibFunc_coshl:
5185 // FIXME: These boundaries are slightly conservative.
5186 if (OpC->getType()->isDoubleTy())
5187 return !(Op < APFloat(-710.0) || Op > APFloat(710.0));
5188 if (OpC->getType()->isFloatTy())
5189 return !(Op < APFloat(-89.0f) || Op > APFloat(89.0f));
5190 break;
5191
5192 case LibFunc_sqrtl:
5193 case LibFunc_sqrt:
5194 case LibFunc_sqrtf:
5195 return Op.isNaN() || Op.isZero() || !Op.isNegative();
5196
5197 // FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p,
5198 // maybe others?
5199 default:
5200 break;
5201 }
5202 }
5203 }
5204
5205 if (Call->arg_size() == 2) {
5206 ConstantFP *Op0C = dyn_cast<ConstantFP>(Call->getArgOperand(0));
5207 ConstantFP *Op1C = dyn_cast<ConstantFP>(Call->getArgOperand(1));
5208 if (Op0C && Op1C) {
5209 const APFloat &Op0 = Op0C->getValueAPF();
5210 const APFloat &Op1 = Op1C->getValueAPF();
5211
5212 switch (Func) {
5213 case LibFunc_powl:
5214 case LibFunc_pow:
5215 case LibFunc_powf: {
5216 // FIXME: Stop using the host math library.
5217 // FIXME: The computation isn't done in the right precision.
5218 Type *Ty = Op0C->getType();
5219 if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) {
5220 if (Ty == Op1C->getType())
5221 return ConstantFoldBinaryFP(pow, Op0, Op1, Ty) != nullptr;
5222 }
5223 break;
5224 }
5225
5226 case LibFunc_fmodl:
5227 case LibFunc_fmod:
5228 case LibFunc_fmodf:
5229 case LibFunc_remainderl:
5230 case LibFunc_remainder:
5231 case LibFunc_remainderf:
5232 return Op0.isNaN() || Op1.isNaN() ||
5233 (!Op0.isInfinity() && !Op1.isZero());
5234
5235 case LibFunc_atan2:
5236 case LibFunc_atan2f:
5237 case LibFunc_atan2l:
5238 // Although IEEE-754 says atan2(+/-0.0, +/-0.0) are well-defined, and
5239 // GLIBC and MSVC do not appear to raise an error on those, we
5240 // cannot rely on that behavior. POSIX and C11 say that a domain error
5241 // may occur, so allow for that possibility.
5242 return !Op0.isZero() || !Op1.isZero();
5243
5244 case LibFunc_nextafter:
5245 case LibFunc_nextafterf:
5246 case LibFunc_nextafterl:
5247 case LibFunc_nexttoward:
5248 case LibFunc_nexttowardf:
5249 case LibFunc_nexttowardl: {
5250 return ConstantFoldNextToward(Op0, Op1, F->getReturnType()) != nullptr;
5251 }
5252 default:
5253 break;
5254 }
5255 }
5256 }
5257
5258 return false;
5259}
5260
5262 unsigned CastOp, const DataLayout &DL,
5263 PreservedCastFlags *Flags) {
5264 switch (CastOp) {
5265 case Instruction::BitCast:
5266 // Bitcast is always lossless.
5267 return ConstantFoldCastOperand(Instruction::BitCast, C, InvCastTo, DL);
5268 case Instruction::Trunc: {
5269 auto *ZExtC = ConstantFoldCastOperand(Instruction::ZExt, C, InvCastTo, DL);
5270 if (Flags) {
5271 // Truncation back on ZExt value is always NUW.
5272 Flags->NUW = true;
5273 // Test positivity of C.
5274 auto *SExtC =
5275 ConstantFoldCastOperand(Instruction::SExt, C, InvCastTo, DL);
5276 Flags->NSW = ZExtC == SExtC;
5277 }
5278 return ZExtC;
5279 }
5280 case Instruction::SExt:
5281 case Instruction::ZExt: {
5282 auto *InvC = ConstantExpr::getTrunc(C, InvCastTo);
5283 auto *CastInvC = ConstantFoldCastOperand(CastOp, InvC, C->getType(), DL);
5284 // Must satisfy CastOp(InvC) == C.
5285 if (!CastInvC || CastInvC != C)
5286 return nullptr;
5287 if (Flags && CastOp == Instruction::ZExt) {
5288 auto *SExtInvC =
5289 ConstantFoldCastOperand(Instruction::SExt, InvC, C->getType(), DL);
5290 // Test positivity of InvC.
5291 Flags->NNeg = CastInvC == SExtInvC;
5292 }
5293 return InvC;
5294 }
5295 case Instruction::FPExt: {
5296 Constant *InvC =
5297 ConstantFoldCastOperand(Instruction::FPTrunc, C, InvCastTo, DL);
5298 if (InvC) {
5299 Constant *CastInvC =
5300 ConstantFoldCastOperand(CastOp, InvC, C->getType(), DL);
5301 if (CastInvC == C)
5302 return InvC;
5303 }
5304 return nullptr;
5305 }
5306 default:
5307 return nullptr;
5308 }
5309}
5310
5312 const DataLayout &DL,
5313 PreservedCastFlags *Flags) {
5314 return getLosslessInvCast(C, DestTy, Instruction::ZExt, DL, Flags);
5315}
5316
5318 const DataLayout &DL,
5319 PreservedCastFlags *Flags) {
5320 return getLosslessInvCast(C, DestTy, Instruction::SExt, DL, Flags);
5321}
5322
5323void TargetFolder::anchor() {}
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
constexpr 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...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static Constant * FoldBitCast(Constant *V, Type *DestTy)
static ConstantFP * flushDenormalConstant(Type *Ty, const APFloat &APF, DenormalMode::DenormalModeKind Mode)
Constant * getConstantAtOffset(Constant *Base, APInt Offset, const DataLayout &DL)
If this Offset points exactly to the start of an aggregate element, return that element,...
static cl::opt< bool > DisableFPCallFolding("disable-fp-call-folding", cl::desc("Disable constant-folding of FP intrinsics and libcalls."), cl::init(false), cl::Hidden)
static bool canConstantFoldIntrinsic(Intrinsic::ID ID, bool IsStrictFP)
Returns true if the intrinsic can be constant folded, given IsStrictFP.
static bool containsByteType(Type *Ty)
Return true if Ty is or contains a byte type.
static ConstantFP * flushDenormalConstantFP(ConstantFP *CFP, const Function *CtxF, bool IsOutput)
static bool anyTypeContainsFP(Type *RetTy, ArrayRef< Value * > Ops)
Given a function's return type and its operands, determine if any of them of of floating-point type.
static DenormalMode getInstrDenormalMode(const Function *CtxF, Type *Ty)
Return the denormal mode that can be assumed when executing a floating point operation at CtxI.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
Hexagon Common GEP
amode Optimize addressing mode
static constexpr Value * getValue(Ty &ValueOrUse)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
This file contains the definitions of the enumerations and flags associated with NVVM Intrinsics,...
if(PassOpts->AAPipeline)
MachineBasicBlock * Folded
SmallVector< MachineOperand, 4 > Cond
int64_t C1
int64_t C0
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
SI Fold Operands
This file contains some templates that are useful if you are working with the STL at all.
This file implements the SmallBitVector class.
This file defines the SmallVector class.
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
The Input class is used to parse a yaml document into in-memory structs and vectors.
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:370
static constexpr roundingMode rmTowardZero
Definition APFloat.h:384
llvm::RoundingMode roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
Definition APFloat.h:378
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:383
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:380
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:382
static constexpr const fltSemantics & IEEEdouble()
Definition APFloat.h:306
static constexpr roundingMode rmNearestTiesToAway
Definition APFloat.h:385
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:396
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1253
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1341
void copySign(const APFloat &RHS)
Definition APFloat.h:1435
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6106
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1323
bool isNegative() const
Definition APFloat.h:1612
LLVM_ABI double convertToDouble() const
Converts this APFloat to host double value.
Definition APFloat.cpp:6339
bool isPosInfinity() const
Definition APFloat.h:1625
bool isNormal() const
Definition APFloat.h:1616
bool isDenormal() const
Definition APFloat.h:1613
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1314
const fltSemantics & getSemantics() const
Definition APFloat.h:1620
bool isNonZero() const
Definition APFloat.h:1621
bool isFinite() const
Definition APFloat.h:1617
bool isNaN() const
Definition APFloat.h:1610
static APFloat getOne(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative One.
Definition APFloat.h:1221
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1332
bool isSignaling() const
Definition APFloat.h:1614
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1368
bool isZero() const
Definition APFloat.h:1608
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1465
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1359
bool isNegInfinity() const
Definition APFloat.h:1626
void changeSign()
Definition APFloat.h:1430
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1212
bool isInfinity() const
Definition APFloat.h:1609
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
LLVM_ABI uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
Definition APInt.cpp:517
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1078
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:202
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1695
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:205
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:215
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1086
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:393
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:829
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:1030
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:196
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:478
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
static LLVM_ABI 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 LLVM_ABI unsigned isEliminableCastPair(Instruction::CastOps firstOpcode, Instruction::CastOps secondOpcode, Type *SrcTy, Type *MidTy, Type *DstTy, const DataLayout *DL)
Determine how a pair of casts can be eliminated, if they can be at all.
static LLVM_ABI 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:740
bool isSigned() const
Definition InstrTypes.h:993
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:890
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:833
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:878
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static LLVM_ABI bool isDesirableCastOp(unsigned Opcode)
Whether creating a constant expression for this cast is desirable.
static LLVM_ABI Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getPtrAdd(Constant *Ptr, Constant *Offset, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReduced=nullptr)
Create a getelementptr i8, ptr, offset constant expression.
Definition Constants.h:1518
static LLVM_ABI Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition Constants.h:1477
static LLVM_ABI Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
static bool isSupportedGetElementPtr(const Type *SrcElemTy)
Whether creating a constant expression for this getelementptr type is supported.
Definition Constants.h:1624
static LLVM_ABI 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.
static LLVM_ABI bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
const APFloat & getValueAPF() const
Definition Constants.h:463
static LLVM_ABI ConstantFP * getZero(Type *Ty, bool Negative=false)
static LLVM_ABI ConstantFP * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
static LLVM_ABI ConstantFP * getInfinity(Type *Ty, bool Negative=false)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V, bool ImplicitTrunc=false)
Return a ConstantInt with the specified value for the specified type.
Definition Constants.h:135
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition Constants.h:174
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
static LLVM_ABI Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:44
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constant.h:65
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Constrained floating point compare intrinsics.
This is the common base class for constrained floating point intrinsics.
LLVM_ABI std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
LLVM_ABI std::optional< RoundingMode > getRoundingMode() const
Wrapper for a function that represents a value that functionally represents the original function.
Definition Constants.h:1143
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
iterator end()
Definition DenseMap.h:686
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:827
static LLVM_ABI 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.
unsigned getNumElements() const
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:838
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition Function.cpp:807
bool isStrictFP() const
Determine if the function has strict floating point sematics.
Definition Function.h:637
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags inBounds()
GEPNoWrapFlags withoutNoUnsignedSignedWrap() const
static GEPNoWrapFlags noUnsignedWrap()
bool hasNoUnsignedSignedWrap() const
bool isInBounds() const
LLVM_ABI bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset, function_ref< bool(Value &, APInt &)> ExternalAnalysis=nullptr) const
Accumulate the constant address offset of this GEP if possible.
Definition Operator.cpp:130
static LLVM_ABI Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
PointerType * getType() const
Global values are always pointers.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:205
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 LLVM_ABI 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 isEquality() const
Return true if this predicate is either EQ or NE.
bool isCast() const
bool isBinaryOp() const
bool isUnaryOp() const
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:333
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
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...
static ICmpInst::Predicate getPredicate(Intrinsic::ID ID)
Returns the comparison predicate underlying the intrinsic.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Class to represent scalable SIMD vectors.
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
SmallBitVector & set()
iterator_range< const_set_bits_iterator > set_bits() const
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:746
LLVM_ABI unsigned getElementContainingOffset(uint64_t FixedOffset) const
Given a valid byte offset into the structure, returns the structure index that contains it.
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:777
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:295
bool isByteTy() const
True if this is an instance of ByteType.
Definition Type.h:237
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:283
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:294
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
bool isSized() const
Return true if it makes sense to take the size of this type.
Definition Type.h:321
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.h:354
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:370
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:187
bool isByteOrByteVectorTy() const
Return true if this is a byte type or a vector of byte types.
Definition Type.h:243
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:293
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:291
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:280
bool isX86_AMXTy() const
Return true if this is X86 AMX.
Definition Type.h:202
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:298
Type * getContainedType(unsigned i) const
This method is used to implement the type iterator (defined at the end of the file).
Definition Type.h:399
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:96
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
LLVM_ABI uint64_t getPointerDereferenceableBytes(const DataLayout &DL, bool &CanBeNull, bool *CanBeFreed) const
Returns the number of bytes known to be dereferenceable for the pointer value.
Definition Value.cpp:913
Base class of all SIMD vector types.
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:201
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr bool isFixed() const
Returns true if the quantity is not scaled by vscale.
Definition TypeSize.h:172
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:253
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:238
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3184
LLVM_ABI APInt pext(const APInt &Val, const APInt &Mask)
Perform a "compress" operation, also known as pext or bext.
Definition APInt.cpp:3264
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3176
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition APInt.h:2274
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition APInt.h:2279
LLVM_ABI APInt clmul(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, also known as XOR multiplication, and return low-bits.
Definition APInt.cpp:3244
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition APInt.h:2284
LLVM_ABI APInt pdep(const APInt &Val, const APInt &Mask)
Perform an "expand" operation, also known as pdep or bdep.
Definition APInt.cpp:3274
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2289
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.
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
initializer< Ty > init(const Ty &Val)
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:473
static constexpr cmpResult cmpEqual
Definition APFloat.h:481
@ ebStrict
This corresponds to "fpexcept.strict".
Definition FPEnv.h:42
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
constexpr double pi
APFloat::roundingMode GetRoundingModeFromImmArg(const Value *ImmArgVal)
DenormalMode GetNVVMDenormMode(bool ShouldFTZ)
bool FPToIntegerIntrinsicNaNZero(Intrinsic::ID IntrinsicID)
APFloat::roundingMode GetFDivRoundingMode(Intrinsic::ID IntrinsicID)
bool FPToIntegerIntrinsicResultIsSigned(Intrinsic::ID IntrinsicID)
APFloat::roundingMode GetFPToIntegerRoundingMode(Intrinsic::ID IntrinsicID)
bool RCPShouldFTZ(Intrinsic::ID IntrinsicID)
bool FPToIntegerIntrinsicShouldFTZ(Intrinsic::ID IntrinsicID)
bool FDivShouldFTZ(Intrinsic::ID IntrinsicID)
bool FMinFMaxIsXorSignAbs(Intrinsic::ID IntrinsicID)
bool FPArithShouldFTZ(Intrinsic::ID IntrinsicID)
bool UnaryMathIntrinsicShouldFTZ(Intrinsic::ID IntrinsicID)
bool FMinFMaxShouldFTZ(Intrinsic::ID IntrinsicID)
APFloat::roundingMode GetRCPRoundingMode(Intrinsic::ID IntrinsicID)
bool FMinFMaxPropagatesNaNs(Intrinsic::ID IntrinsicID)
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
LLVM_ABI 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.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:577
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:1755
LLVM_ABI 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)
LLVM_ABI Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
LLVM_ABI Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I, bool AllowNonDeterministic=true)
Attempt to constant fold a floating point binary operation with the specified operands,...
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2575
unsigned getPointerAddressSpace(const Type *T)
Definition SPIRVUtils.h:381
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI Constant * ConstantFoldInstruction(const Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition APFloat.h:1765
LLVM_ABI Constant * ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2)
LLVM_ABI Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
LLVM_ABI 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.
LLVM_ABI bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1845
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI Constant * ConstantFoldBitInsertOperands(Constant *Base, Constant *Val, Constant *Offset, const DataLayout &DL)
Attempt to constant fold a bitinsert instruction with the specified operands.
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1736
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
LLVM_ABI Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
LLVM_ABI bool canConstantFoldCallTo(const CallBase *Call, const Function *F, const TargetLibraryInfo *TLI=nullptr)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1757
LLVM_ABI Constant * ConstantFoldIntrinsic(Intrinsic::ID ID, ArrayRef< Constant * > Ops, Type *Ty, const DataLayout &DL, const Function *CtxF=nullptr)
LLVM_ABI Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Function *CtxF=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
LLVM_ABI Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1800
LLVM_ABI 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...
LLVM_ABI Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
FPOp
The floating-point operations supported by the folding helpers.
Definition FPFold.h:25
LLVM_ABI Constant * getLosslessUnsignedTrunc(Constant *C, Type *DestTy, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
LLVM_READONLY LLVM_ABI std::optional< APFloat > exp(const APFloat &X, RoundingMode RM=APFloat::rmNearestTiesToEven, APFloat::opStatus *Status=nullptr)
Implement IEEE 754-2019 exp functions.
Definition APFloat.cpp:6499
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1831
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Returns: X * 2^Exp for integral exponents.
Definition APFloat.h:1745
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI Constant * getLosslessSignedTrunc(Constant *C, Type *DestTy, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_ABI Constant * FlushFPConstant(Constant *Operand, const Function *CtxF, bool IsOutput)
Attempt to flush float point constant according to denormal mode set in the instruction's parent func...
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
LLVM_ABI Constant * ConstantFoldBitExtractOperands(Type *Ty, Constant *Src, Constant *Offset, const DataLayout &DL)
Attempt to constant fold a bitextract instruction with the specified result type and operands.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1781
@ Sub
Subtraction of integers.
LLVM_ABI bool isVectorIntrinsicWithScalarOpAtArg(Intrinsic::ID ID, unsigned ScalarOpdIdx, const TargetTransformInfo *TTI)
Identifies if the vector form of the intrinsic has a scalar operand.
IntPtrTy
Definition InstrProf.h:82
DWARFExpression::Operation Op
RoundingMode
Rounding mode.
@ NearestTiesToEven
roundTiesToEven.
@ Dynamic
Denotes mode unknown at compile time.
LLVM_ABI 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
LLVM_ABI Constant * getLosslessInvCast(Constant *C, Type *InvCastTo, unsigned CastOp, const DataLayout &DL, PreservedCastFlags *Flags=nullptr)
Try to cast C to InvC losslessly, satisfying CastOp(InvC) equals C, or CastOp(InvC) is a refined valu...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2182
LLVM_ABI Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
LLVM_ABI Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
Attempt to constant fold an insertvalue instruction with the specified operands and indices.
LLVM_ABI 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...
constexpr uint32_t calculateReflectedCRC32(uint32_t Crc, uint64_t Data, unsigned DataBytes, uint32_t Poly)
Definition CRC.h:34
LLVM_ABI Constant * ConstantFoldInstOperands(const Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1818
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1858
LLVM_ABI 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...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:866
LLVM_ABI Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
LLVM_ABI Constant * ConstantFoldFP(double(*NativeFP)(double), const APFloat &V, Type *Ty, DenormalMode DenormMode=DenormalMode::getIEEE())
Evaluate NativeFP on V using the host libm, then convert the result to Ty.
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.
constexpr bool isValid() const
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getDynamic()
static constexpr DenormalMode getIEEE()
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition KnownBits.h:58