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