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