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