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