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