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