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/ArrayRef.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Analysis/TargetLibraryInfo.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/Config/config.h"
29 #include "llvm/IR/Constant.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/GlobalValue.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/InstrTypes.h"
37 #include "llvm/IR/Instruction.h"
38 #include "llvm/IR/Instructions.h"
39 #include "llvm/IR/Operator.h"
40 #include "llvm/IR/Type.h"
41 #include "llvm/IR/Value.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/KnownBits.h"
45 #include "llvm/Support/MathExtras.h"
46 #include <cassert>
47 #include <cerrno>
48 #include <cfenv>
49 #include <cmath>
50 #include <cstddef>
51 #include <cstdint>
52 
53 using namespace llvm;
54 
55 namespace {
56 
57 //===----------------------------------------------------------------------===//
58 // Constant Folding internal helper functions
59 //===----------------------------------------------------------------------===//
60 
61 static Constant *foldConstVectorToAPInt(APInt &Result, Type *DestTy,
62                                         Constant *C, Type *SrcEltTy,
63                                         unsigned NumSrcElts,
64                                         const DataLayout &DL) {
65   // Now that we know that the input value is a vector of integers, just shift
66   // and insert them into our result.
67   unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy);
68   for (unsigned i = 0; i != NumSrcElts; ++i) {
69     Constant *Element;
70     if (DL.isLittleEndian())
71       Element = C->getAggregateElement(NumSrcElts - i - 1);
72     else
73       Element = C->getAggregateElement(i);
74 
75     if (Element && isa<UndefValue>(Element)) {
76       Result <<= BitShift;
77       continue;
78     }
79 
80     auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
81     if (!ElementCI)
82       return ConstantExpr::getBitCast(C, DestTy);
83 
84     Result <<= BitShift;
85     Result |= ElementCI->getValue().zextOrSelf(Result.getBitWidth());
86   }
87 
88   return nullptr;
89 }
90 
91 /// Constant fold bitcast, symbolically evaluating it with DataLayout.
92 /// This always returns a non-null constant, but it may be a
93 /// ConstantExpr if unfoldable.
94 Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
95   // Catch the obvious splat cases.
96   if (C->isNullValue() && !DestTy->isX86_MMXTy())
97     return Constant::getNullValue(DestTy);
98   if (C->isAllOnesValue() && !DestTy->isX86_MMXTy() &&
99       !DestTy->isPtrOrPtrVectorTy()) // Don't get ones for ptr types!
100     return Constant::getAllOnesValue(DestTy);
101 
102   if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
103     // Handle a vector->scalar integer/fp cast.
104     if (isa<IntegerType>(DestTy) || DestTy->isFloatingPointTy()) {
105       unsigned NumSrcElts = VTy->getNumElements();
106       Type *SrcEltTy = VTy->getElementType();
107 
108       // If the vector is a vector of floating point, convert it to vector of int
109       // to simplify things.
110       if (SrcEltTy->isFloatingPointTy()) {
111         unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
112         Type *SrcIVTy =
113           VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElts);
114         // Ask IR to do the conversion now that #elts line up.
115         C = ConstantExpr::getBitCast(C, SrcIVTy);
116       }
117 
118       APInt Result(DL.getTypeSizeInBits(DestTy), 0);
119       if (Constant *CE = foldConstVectorToAPInt(Result, DestTy, C,
120                                                 SrcEltTy, NumSrcElts, DL))
121         return CE;
122 
123       if (isa<IntegerType>(DestTy))
124         return ConstantInt::get(DestTy, Result);
125 
126       APFloat FP(DestTy->getFltSemantics(), Result);
127       return ConstantFP::get(DestTy->getContext(), FP);
128     }
129   }
130 
131   // The code below only handles casts to vectors currently.
132   auto *DestVTy = dyn_cast<VectorType>(DestTy);
133   if (!DestVTy)
134     return ConstantExpr::getBitCast(C, DestTy);
135 
136   // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
137   // vector so the code below can handle it uniformly.
138   if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) {
139     Constant *Ops = C; // don't take the address of C!
140     return FoldBitCast(ConstantVector::get(Ops), DestTy, DL);
141   }
142 
143   // If this is a bitcast from constant vector -> vector, fold it.
144   if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
145     return ConstantExpr::getBitCast(C, DestTy);
146 
147   // If the element types match, IR can fold it.
148   unsigned NumDstElt = DestVTy->getNumElements();
149   unsigned NumSrcElt = C->getType()->getVectorNumElements();
150   if (NumDstElt == NumSrcElt)
151     return ConstantExpr::getBitCast(C, DestTy);
152 
153   Type *SrcEltTy = C->getType()->getVectorElementType();
154   Type *DstEltTy = DestVTy->getElementType();
155 
156   // Otherwise, we're changing the number of elements in a vector, which
157   // requires endianness information to do the right thing.  For example,
158   //    bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
159   // folds to (little endian):
160   //    <4 x i32> <i32 0, i32 0, i32 1, i32 0>
161   // and to (big endian):
162   //    <4 x i32> <i32 0, i32 0, i32 0, i32 1>
163 
164   // First thing is first.  We only want to think about integer here, so if
165   // we have something in FP form, recast it as integer.
166   if (DstEltTy->isFloatingPointTy()) {
167     // Fold to an vector of integers with same size as our FP type.
168     unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
169     Type *DestIVTy =
170       VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt);
171     // Recursively handle this integer conversion, if possible.
172     C = FoldBitCast(C, DestIVTy, DL);
173 
174     // Finally, IR can handle this now that #elts line up.
175     return ConstantExpr::getBitCast(C, DestTy);
176   }
177 
178   // Okay, we know the destination is integer, if the input is FP, convert
179   // it to integer first.
180   if (SrcEltTy->isFloatingPointTy()) {
181     unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
182     Type *SrcIVTy =
183       VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
184     // Ask IR to do the conversion now that #elts line up.
185     C = ConstantExpr::getBitCast(C, SrcIVTy);
186     // If IR wasn't able to fold it, bail out.
187     if (!isa<ConstantVector>(C) &&  // FIXME: Remove ConstantVector.
188         !isa<ConstantDataVector>(C))
189       return C;
190   }
191 
192   // Now we know that the input and output vectors are both integer vectors
193   // of the same size, and that their #elements is not the same.  Do the
194   // conversion here, which depends on whether the input or output has
195   // more elements.
196   bool isLittleEndian = DL.isLittleEndian();
197 
198   SmallVector<Constant*, 32> Result;
199   if (NumDstElt < NumSrcElt) {
200     // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
201     Constant *Zero = Constant::getNullValue(DstEltTy);
202     unsigned Ratio = NumSrcElt/NumDstElt;
203     unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
204     unsigned SrcElt = 0;
205     for (unsigned i = 0; i != NumDstElt; ++i) {
206       // Build each element of the result.
207       Constant *Elt = Zero;
208       unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
209       for (unsigned j = 0; j != Ratio; ++j) {
210         Constant *Src = C->getAggregateElement(SrcElt++);
211         if (Src && isa<UndefValue>(Src))
212           Src = Constant::getNullValue(C->getType()->getVectorElementType());
213         else
214           Src = dyn_cast_or_null<ConstantInt>(Src);
215         if (!Src)  // Reject constantexpr elements.
216           return ConstantExpr::getBitCast(C, DestTy);
217 
218         // Zero extend the element to the right size.
219         Src = ConstantExpr::getZExt(Src, Elt->getType());
220 
221         // Shift it to the right place, depending on endianness.
222         Src = ConstantExpr::getShl(Src,
223                                    ConstantInt::get(Src->getType(), ShiftAmt));
224         ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
225 
226         // Mix it in.
227         Elt = ConstantExpr::getOr(Elt, Src);
228       }
229       Result.push_back(Elt);
230     }
231     return ConstantVector::get(Result);
232   }
233 
234   // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
235   unsigned Ratio = NumDstElt/NumSrcElt;
236   unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy);
237 
238   // Loop over each source value, expanding into multiple results.
239   for (unsigned i = 0; i != NumSrcElt; ++i) {
240     auto *Element = C->getAggregateElement(i);
241 
242     if (!Element) // Reject constantexpr elements.
243       return ConstantExpr::getBitCast(C, DestTy);
244 
245     if (isa<UndefValue>(Element)) {
246       // Correctly Propagate undef values.
247       Result.append(Ratio, UndefValue::get(DstEltTy));
248       continue;
249     }
250 
251     auto *Src = dyn_cast<ConstantInt>(Element);
252     if (!Src)
253       return ConstantExpr::getBitCast(C, DestTy);
254 
255     unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
256     for (unsigned j = 0; j != Ratio; ++j) {
257       // Shift the piece of the value into the right place, depending on
258       // endianness.
259       Constant *Elt = ConstantExpr::getLShr(Src,
260                                   ConstantInt::get(Src->getType(), ShiftAmt));
261       ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
262 
263       // Truncate the element to an integer with the same pointer size and
264       // convert the element back to a pointer using a inttoptr.
265       if (DstEltTy->isPointerTy()) {
266         IntegerType *DstIntTy = Type::getIntNTy(C->getContext(), DstBitSize);
267         Constant *CE = ConstantExpr::getTrunc(Elt, DstIntTy);
268         Result.push_back(ConstantExpr::getIntToPtr(CE, DstEltTy));
269         continue;
270       }
271 
272       // Truncate and remember this piece.
273       Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
274     }
275   }
276 
277   return ConstantVector::get(Result);
278 }
279 
280 } // end anonymous namespace
281 
282 /// If this constant is a constant offset from a global, return the global and
283 /// the constant. Because of constantexprs, this function is recursive.
284 bool llvm::IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
285                                       APInt &Offset, const DataLayout &DL) {
286   // Trivial case, constant is the global.
287   if ((GV = dyn_cast<GlobalValue>(C))) {
288     unsigned BitWidth = DL.getIndexTypeSizeInBits(GV->getType());
289     Offset = APInt(BitWidth, 0);
290     return true;
291   }
292 
293   // Otherwise, if this isn't a constant expr, bail out.
294   auto *CE = dyn_cast<ConstantExpr>(C);
295   if (!CE) return false;
296 
297   // Look through ptr->int and ptr->ptr casts.
298   if (CE->getOpcode() == Instruction::PtrToInt ||
299       CE->getOpcode() == Instruction::BitCast)
300     return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL);
301 
302   // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
303   auto *GEP = dyn_cast<GEPOperator>(CE);
304   if (!GEP)
305     return false;
306 
307   unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
308   APInt TmpOffset(BitWidth, 0);
309 
310   // If the base isn't a global+constant, we aren't either.
311   if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, DL))
312     return false;
313 
314   // Otherwise, add any offset that our operands provide.
315   if (!GEP->accumulateConstantOffset(DL, TmpOffset))
316     return false;
317 
318   Offset = TmpOffset;
319   return true;
320 }
321 
322 Constant *llvm::ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
323                                          const DataLayout &DL) {
324   do {
325     Type *SrcTy = C->getType();
326 
327     // If the type sizes are the same and a cast is legal, just directly
328     // cast the constant.
329     if (DL.getTypeSizeInBits(DestTy) == DL.getTypeSizeInBits(SrcTy)) {
330       Instruction::CastOps Cast = Instruction::BitCast;
331       // If we are going from a pointer to int or vice versa, we spell the cast
332       // differently.
333       if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
334         Cast = Instruction::IntToPtr;
335       else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
336         Cast = Instruction::PtrToInt;
337 
338       if (CastInst::castIsValid(Cast, C, DestTy))
339         return ConstantExpr::getCast(Cast, C, DestTy);
340     }
341 
342     // If this isn't an aggregate type, there is nothing we can do to drill down
343     // and find a bitcastable constant.
344     if (!SrcTy->isAggregateType())
345       return nullptr;
346 
347     // We're simulating a load through a pointer that was bitcast to point to
348     // a different type, so we can try to walk down through the initial
349     // elements of an aggregate to see if some part of the aggregate is
350     // castable to implement the "load" semantic model.
351     if (SrcTy->isStructTy()) {
352       // Struct types might have leading zero-length elements like [0 x i32],
353       // which are certainly not what we are looking for, so skip them.
354       unsigned Elem = 0;
355       Constant *ElemC;
356       do {
357         ElemC = C->getAggregateElement(Elem++);
358       } while (ElemC && DL.getTypeSizeInBits(ElemC->getType()) == 0);
359       C = ElemC;
360     } else {
361       C = C->getAggregateElement(0u);
362     }
363   } while (C);
364 
365   return nullptr;
366 }
367 
368 namespace {
369 
370 /// Recursive helper to read bits out of global. C is the constant being copied
371 /// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy
372 /// results into and BytesLeft is the number of bytes left in
373 /// the CurPtr buffer. DL is the DataLayout.
374 bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr,
375                         unsigned BytesLeft, const DataLayout &DL) {
376   assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) &&
377          "Out of range access");
378 
379   // If this element is zero or undefined, we can just return since *CurPtr is
380   // zero initialized.
381   if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
382     return true;
383 
384   if (auto *CI = dyn_cast<ConstantInt>(C)) {
385     if (CI->getBitWidth() > 64 ||
386         (CI->getBitWidth() & 7) != 0)
387       return false;
388 
389     uint64_t Val = CI->getZExtValue();
390     unsigned IntBytes = unsigned(CI->getBitWidth()/8);
391 
392     for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
393       int n = ByteOffset;
394       if (!DL.isLittleEndian())
395         n = IntBytes - n - 1;
396       CurPtr[i] = (unsigned char)(Val >> (n * 8));
397       ++ByteOffset;
398     }
399     return true;
400   }
401 
402   if (auto *CFP = dyn_cast<ConstantFP>(C)) {
403     if (CFP->getType()->isDoubleTy()) {
404       C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL);
405       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
406     }
407     if (CFP->getType()->isFloatTy()){
408       C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), DL);
409       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
410     }
411     if (CFP->getType()->isHalfTy()){
412       C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), DL);
413       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
414     }
415     return false;
416   }
417 
418   if (auto *CS = dyn_cast<ConstantStruct>(C)) {
419     const StructLayout *SL = DL.getStructLayout(CS->getType());
420     unsigned Index = SL->getElementContainingOffset(ByteOffset);
421     uint64_t CurEltOffset = SL->getElementOffset(Index);
422     ByteOffset -= CurEltOffset;
423 
424     while (true) {
425       // If the element access is to the element itself and not to tail padding,
426       // read the bytes from the element.
427       uint64_t EltSize = DL.getTypeAllocSize(CS->getOperand(Index)->getType());
428 
429       if (ByteOffset < EltSize &&
430           !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
431                               BytesLeft, DL))
432         return false;
433 
434       ++Index;
435 
436       // Check to see if we read from the last struct element, if so we're done.
437       if (Index == CS->getType()->getNumElements())
438         return true;
439 
440       // If we read all of the bytes we needed from this element we're done.
441       uint64_t NextEltOffset = SL->getElementOffset(Index);
442 
443       if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
444         return true;
445 
446       // Move to the next element of the struct.
447       CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
448       BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
449       ByteOffset = 0;
450       CurEltOffset = NextEltOffset;
451     }
452     // not reached.
453   }
454 
455   if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
456       isa<ConstantDataSequential>(C)) {
457     Type *EltTy = C->getType()->getSequentialElementType();
458     uint64_t EltSize = DL.getTypeAllocSize(EltTy);
459     uint64_t Index = ByteOffset / EltSize;
460     uint64_t Offset = ByteOffset - Index * EltSize;
461     uint64_t NumElts;
462     if (auto *AT = dyn_cast<ArrayType>(C->getType()))
463       NumElts = AT->getNumElements();
464     else
465       NumElts = C->getType()->getVectorNumElements();
466 
467     for (; Index != NumElts; ++Index) {
468       if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
469                               BytesLeft, DL))
470         return false;
471 
472       uint64_t BytesWritten = EltSize - Offset;
473       assert(BytesWritten <= EltSize && "Not indexing into this element?");
474       if (BytesWritten >= BytesLeft)
475         return true;
476 
477       Offset = 0;
478       BytesLeft -= BytesWritten;
479       CurPtr += BytesWritten;
480     }
481     return true;
482   }
483 
484   if (auto *CE = dyn_cast<ConstantExpr>(C)) {
485     if (CE->getOpcode() == Instruction::IntToPtr &&
486         CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) {
487       return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
488                                 BytesLeft, DL);
489     }
490   }
491 
492   // Otherwise, unknown initializer type.
493   return false;
494 }
495 
496 Constant *FoldReinterpretLoadFromConstPtr(Constant *C, Type *LoadTy,
497                                           const DataLayout &DL) {
498   auto *PTy = cast<PointerType>(C->getType());
499   auto *IntType = dyn_cast<IntegerType>(LoadTy);
500 
501   // If this isn't an integer load we can't fold it directly.
502   if (!IntType) {
503     unsigned AS = PTy->getAddressSpace();
504 
505     // If this is a float/double load, we can try folding it as an int32/64 load
506     // and then bitcast the result.  This can be useful for union cases.  Note
507     // that address spaces don't matter here since we're not going to result in
508     // an actual new load.
509     Type *MapTy;
510     if (LoadTy->isHalfTy())
511       MapTy = Type::getInt16Ty(C->getContext());
512     else if (LoadTy->isFloatTy())
513       MapTy = Type::getInt32Ty(C->getContext());
514     else if (LoadTy->isDoubleTy())
515       MapTy = Type::getInt64Ty(C->getContext());
516     else if (LoadTy->isVectorTy()) {
517       MapTy = PointerType::getIntNTy(C->getContext(),
518                                      DL.getTypeAllocSizeInBits(LoadTy));
519     } else
520       return nullptr;
521 
522     C = FoldBitCast(C, MapTy->getPointerTo(AS), DL);
523     if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, MapTy, DL))
524       return FoldBitCast(Res, LoadTy, DL);
525     return nullptr;
526   }
527 
528   unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
529   if (BytesLoaded > 32 || BytesLoaded == 0)
530     return nullptr;
531 
532   GlobalValue *GVal;
533   APInt OffsetAI;
534   if (!IsConstantOffsetFromGlobal(C, GVal, OffsetAI, DL))
535     return nullptr;
536 
537   auto *GV = dyn_cast<GlobalVariable>(GVal);
538   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
539       !GV->getInitializer()->getType()->isSized())
540     return nullptr;
541 
542   int64_t Offset = OffsetAI.getSExtValue();
543   int64_t InitializerSize = DL.getTypeAllocSize(GV->getInitializer()->getType());
544 
545   // If we're not accessing anything in this constant, the result is undefined.
546   if (Offset + BytesLoaded <= 0)
547     return UndefValue::get(IntType);
548 
549   // If we're not accessing anything in this constant, the result is undefined.
550   if (Offset >= InitializerSize)
551     return UndefValue::get(IntType);
552 
553   unsigned char RawBytes[32] = {0};
554   unsigned char *CurPtr = RawBytes;
555   unsigned BytesLeft = BytesLoaded;
556 
557   // If we're loading off the beginning of the global, some bytes may be valid.
558   if (Offset < 0) {
559     CurPtr += -Offset;
560     BytesLeft += Offset;
561     Offset = 0;
562   }
563 
564   if (!ReadDataFromGlobal(GV->getInitializer(), Offset, CurPtr, BytesLeft, DL))
565     return nullptr;
566 
567   APInt ResultVal = APInt(IntType->getBitWidth(), 0);
568   if (DL.isLittleEndian()) {
569     ResultVal = RawBytes[BytesLoaded - 1];
570     for (unsigned i = 1; i != BytesLoaded; ++i) {
571       ResultVal <<= 8;
572       ResultVal |= RawBytes[BytesLoaded - 1 - i];
573     }
574   } else {
575     ResultVal = RawBytes[0];
576     for (unsigned i = 1; i != BytesLoaded; ++i) {
577       ResultVal <<= 8;
578       ResultVal |= RawBytes[i];
579     }
580   }
581 
582   return ConstantInt::get(IntType->getContext(), ResultVal);
583 }
584 
585 Constant *ConstantFoldLoadThroughBitcastExpr(ConstantExpr *CE, Type *DestTy,
586                                              const DataLayout &DL) {
587   auto *SrcPtr = CE->getOperand(0);
588   auto *SrcPtrTy = dyn_cast<PointerType>(SrcPtr->getType());
589   if (!SrcPtrTy)
590     return nullptr;
591   Type *SrcTy = SrcPtrTy->getPointerElementType();
592 
593   Constant *C = ConstantFoldLoadFromConstPtr(SrcPtr, SrcTy, DL);
594   if (!C)
595     return nullptr;
596 
597   return llvm::ConstantFoldLoadThroughBitcast(C, DestTy, DL);
598 }
599 
600 } // end anonymous namespace
601 
602 Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
603                                              const DataLayout &DL) {
604   // First, try the easy cases:
605   if (auto *GV = dyn_cast<GlobalVariable>(C))
606     if (GV->isConstant() && GV->hasDefinitiveInitializer())
607       return GV->getInitializer();
608 
609   if (auto *GA = dyn_cast<GlobalAlias>(C))
610     if (GA->getAliasee() && !GA->isInterposable())
611       return ConstantFoldLoadFromConstPtr(GA->getAliasee(), Ty, DL);
612 
613   // If the loaded value isn't a constant expr, we can't handle it.
614   auto *CE = dyn_cast<ConstantExpr>(C);
615   if (!CE)
616     return nullptr;
617 
618   if (CE->getOpcode() == Instruction::GetElementPtr) {
619     if (auto *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
620       if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
621         if (Constant *V =
622              ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
623           return V;
624       }
625     }
626   }
627 
628   if (CE->getOpcode() == Instruction::BitCast)
629     if (Constant *LoadedC = ConstantFoldLoadThroughBitcastExpr(CE, Ty, DL))
630       return LoadedC;
631 
632   // Instead of loading constant c string, use corresponding integer value
633   // directly if string length is small enough.
634   StringRef Str;
635   if (getConstantStringInfo(CE, Str) && !Str.empty()) {
636     size_t StrLen = Str.size();
637     unsigned NumBits = Ty->getPrimitiveSizeInBits();
638     // Replace load with immediate integer if the result is an integer or fp
639     // value.
640     if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
641         (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) {
642       APInt StrVal(NumBits, 0);
643       APInt SingleChar(NumBits, 0);
644       if (DL.isLittleEndian()) {
645         for (unsigned char C : reverse(Str.bytes())) {
646           SingleChar = static_cast<uint64_t>(C);
647           StrVal = (StrVal << 8) | SingleChar;
648         }
649       } else {
650         for (unsigned char C : Str.bytes()) {
651           SingleChar = static_cast<uint64_t>(C);
652           StrVal = (StrVal << 8) | SingleChar;
653         }
654         // Append NULL at the end.
655         SingleChar = 0;
656         StrVal = (StrVal << 8) | SingleChar;
657       }
658 
659       Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
660       if (Ty->isFloatingPointTy())
661         Res = ConstantExpr::getBitCast(Res, Ty);
662       return Res;
663     }
664   }
665 
666   // If this load comes from anywhere in a constant global, and if the global
667   // is all undef or zero, we know what it loads.
668   if (auto *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, DL))) {
669     if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
670       if (GV->getInitializer()->isNullValue())
671         return Constant::getNullValue(Ty);
672       if (isa<UndefValue>(GV->getInitializer()))
673         return UndefValue::get(Ty);
674     }
675   }
676 
677   // Try hard to fold loads from bitcasted strange and non-type-safe things.
678   return FoldReinterpretLoadFromConstPtr(CE, Ty, DL);
679 }
680 
681 namespace {
682 
683 Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout &DL) {
684   if (LI->isVolatile()) return nullptr;
685 
686   if (auto *C = dyn_cast<Constant>(LI->getOperand(0)))
687     return ConstantFoldLoadFromConstPtr(C, LI->getType(), DL);
688 
689   return nullptr;
690 }
691 
692 /// One of Op0/Op1 is a constant expression.
693 /// Attempt to symbolically evaluate the result of a binary operator merging
694 /// these together.  If target data info is available, it is provided as DL,
695 /// otherwise DL is null.
696 Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1,
697                                     const DataLayout &DL) {
698   // SROA
699 
700   // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
701   // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
702   // bits.
703 
704   if (Opc == Instruction::And) {
705     KnownBits Known0 = computeKnownBits(Op0, DL);
706     KnownBits Known1 = computeKnownBits(Op1, DL);
707     if ((Known1.One | Known0.Zero).isAllOnesValue()) {
708       // All the bits of Op0 that the 'and' could be masking are already zero.
709       return Op0;
710     }
711     if ((Known0.One | Known1.Zero).isAllOnesValue()) {
712       // All the bits of Op1 that the 'and' could be masking are already zero.
713       return Op1;
714     }
715 
716     Known0.Zero |= Known1.Zero;
717     Known0.One &= Known1.One;
718     if (Known0.isConstant())
719       return ConstantInt::get(Op0->getType(), Known0.getConstant());
720   }
721 
722   // If the constant expr is something like &A[123] - &A[4].f, fold this into a
723   // constant.  This happens frequently when iterating over a global array.
724   if (Opc == Instruction::Sub) {
725     GlobalValue *GV1, *GV2;
726     APInt Offs1, Offs2;
727 
728     if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, DL))
729       if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, DL) && GV1 == GV2) {
730         unsigned OpSize = DL.getTypeSizeInBits(Op0->getType());
731 
732         // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
733         // PtrToInt may change the bitwidth so we have convert to the right size
734         // first.
735         return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
736                                                 Offs2.zextOrTrunc(OpSize));
737       }
738   }
739 
740   return nullptr;
741 }
742 
743 /// If array indices are not pointer-sized integers, explicitly cast them so
744 /// that they aren't implicitly casted by the getelementptr.
745 Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
746                          Type *ResultTy, Optional<unsigned> InRangeIndex,
747                          const DataLayout &DL, const TargetLibraryInfo *TLI) {
748   Type *IntPtrTy = DL.getIntPtrType(ResultTy);
749   Type *IntPtrScalarTy = IntPtrTy->getScalarType();
750 
751   bool Any = false;
752   SmallVector<Constant*, 32> NewIdxs;
753   for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
754     if ((i == 1 ||
755          !isa<StructType>(GetElementPtrInst::getIndexedType(
756              SrcElemTy, Ops.slice(1, i - 1)))) &&
757         Ops[i]->getType()->getScalarType() != IntPtrScalarTy) {
758       Any = true;
759       Type *NewType = Ops[i]->getType()->isVectorTy()
760                           ? IntPtrTy
761                           : IntPtrTy->getScalarType();
762       NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i],
763                                                                       true,
764                                                                       NewType,
765                                                                       true),
766                                               Ops[i], NewType));
767     } else
768       NewIdxs.push_back(Ops[i]);
769   }
770 
771   if (!Any)
772     return nullptr;
773 
774   Constant *C = ConstantExpr::getGetElementPtr(
775       SrcElemTy, Ops[0], NewIdxs, /*InBounds=*/false, InRangeIndex);
776   if (Constant *Folded = ConstantFoldConstant(C, DL, TLI))
777     C = Folded;
778 
779   return C;
780 }
781 
782 /// Strip the pointer casts, but preserve the address space information.
783 Constant* StripPtrCastKeepAS(Constant* Ptr, Type *&ElemTy) {
784   assert(Ptr->getType()->isPointerTy() && "Not a pointer type");
785   auto *OldPtrTy = cast<PointerType>(Ptr->getType());
786   Ptr = Ptr->stripPointerCasts();
787   auto *NewPtrTy = cast<PointerType>(Ptr->getType());
788 
789   ElemTy = NewPtrTy->getPointerElementType();
790 
791   // Preserve the address space number of the pointer.
792   if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
793     NewPtrTy = ElemTy->getPointerTo(OldPtrTy->getAddressSpace());
794     Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy);
795   }
796   return Ptr;
797 }
798 
799 /// If we can symbolically evaluate the GEP constant expression, do so.
800 Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
801                                   ArrayRef<Constant *> Ops,
802                                   const DataLayout &DL,
803                                   const TargetLibraryInfo *TLI) {
804   const GEPOperator *InnermostGEP = GEP;
805   bool InBounds = GEP->isInBounds();
806 
807   Type *SrcElemTy = GEP->getSourceElementType();
808   Type *ResElemTy = GEP->getResultElementType();
809   Type *ResTy = GEP->getType();
810   if (!SrcElemTy->isSized())
811     return nullptr;
812 
813   if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy,
814                                    GEP->getInRangeIndex(), DL, TLI))
815     return C;
816 
817   Constant *Ptr = Ops[0];
818   if (!Ptr->getType()->isPointerTy())
819     return nullptr;
820 
821   Type *IntPtrTy = DL.getIntPtrType(Ptr->getType());
822 
823   // If this is a constant expr gep that is effectively computing an
824   // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
825   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
826       if (!isa<ConstantInt>(Ops[i])) {
827 
828         // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
829         // "inttoptr (sub (ptrtoint Ptr), V)"
830         if (Ops.size() == 2 && ResElemTy->isIntegerTy(8)) {
831           auto *CE = dyn_cast<ConstantExpr>(Ops[1]);
832           assert((!CE || CE->getType() == IntPtrTy) &&
833                  "CastGEPIndices didn't canonicalize index types!");
834           if (CE && CE->getOpcode() == Instruction::Sub &&
835               CE->getOperand(0)->isNullValue()) {
836             Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
837             Res = ConstantExpr::getSub(Res, CE->getOperand(1));
838             Res = ConstantExpr::getIntToPtr(Res, ResTy);
839             if (auto *FoldedRes = ConstantFoldConstant(Res, DL, TLI))
840               Res = FoldedRes;
841             return Res;
842           }
843         }
844         return nullptr;
845       }
846 
847   unsigned BitWidth = DL.getTypeSizeInBits(IntPtrTy);
848   APInt Offset =
849       APInt(BitWidth,
850             DL.getIndexedOffsetInType(
851                 SrcElemTy,
852                 makeArrayRef((Value * const *)Ops.data() + 1, Ops.size() - 1)));
853   Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
854 
855   // If this is a GEP of a GEP, fold it all into a single GEP.
856   while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
857     InnermostGEP = GEP;
858     InBounds &= GEP->isInBounds();
859 
860     SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end());
861 
862     // Do not try the incorporate the sub-GEP if some index is not a number.
863     bool AllConstantInt = true;
864     for (Value *NestedOp : NestedOps)
865       if (!isa<ConstantInt>(NestedOp)) {
866         AllConstantInt = false;
867         break;
868       }
869     if (!AllConstantInt)
870       break;
871 
872     Ptr = cast<Constant>(GEP->getOperand(0));
873     SrcElemTy = GEP->getSourceElementType();
874     Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps));
875     Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
876   }
877 
878   // If the base value for this address is a literal integer value, fold the
879   // getelementptr to the resulting integer value casted to the pointer type.
880   APInt BasePtr(BitWidth, 0);
881   if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) {
882     if (CE->getOpcode() == Instruction::IntToPtr) {
883       if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
884         BasePtr = Base->getValue().zextOrTrunc(BitWidth);
885     }
886   }
887 
888   auto *PTy = cast<PointerType>(Ptr->getType());
889   if ((Ptr->isNullValue() || BasePtr != 0) &&
890       !DL.isNonIntegralPointerType(PTy)) {
891     Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr);
892     return ConstantExpr::getIntToPtr(C, ResTy);
893   }
894 
895   // Otherwise form a regular getelementptr. Recompute the indices so that
896   // we eliminate over-indexing of the notional static type array bounds.
897   // This makes it easy to determine if the getelementptr is "inbounds".
898   // Also, this helps GlobalOpt do SROA on GlobalVariables.
899   Type *Ty = PTy;
900   SmallVector<Constant *, 32> NewIdxs;
901 
902   do {
903     if (!Ty->isStructTy()) {
904       if (Ty->isPointerTy()) {
905         // The only pointer indexing we'll do is on the first index of the GEP.
906         if (!NewIdxs.empty())
907           break;
908 
909         Ty = SrcElemTy;
910 
911         // Only handle pointers to sized types, not pointers to functions.
912         if (!Ty->isSized())
913           return nullptr;
914       } else if (auto *ATy = dyn_cast<SequentialType>(Ty)) {
915         Ty = ATy->getElementType();
916       } else {
917         // We've reached some non-indexable type.
918         break;
919       }
920 
921       // Determine which element of the array the offset points into.
922       APInt ElemSize(BitWidth, DL.getTypeAllocSize(Ty));
923       if (ElemSize == 0) {
924         // The element size is 0. This may be [0 x Ty]*, so just use a zero
925         // index for this level and proceed to the next level to see if it can
926         // accommodate the offset.
927         NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0));
928       } else {
929         // The element size is non-zero divide the offset by the element
930         // size (rounding down), to compute the index at this level.
931         bool Overflow;
932         APInt NewIdx = Offset.sdiv_ov(ElemSize, Overflow);
933         if (Overflow)
934           break;
935         Offset -= NewIdx * ElemSize;
936         NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
937       }
938     } else {
939       auto *STy = cast<StructType>(Ty);
940       // If we end up with an offset that isn't valid for this struct type, we
941       // can't re-form this GEP in a regular form, so bail out. The pointer
942       // operand likely went through casts that are necessary to make the GEP
943       // sensible.
944       const StructLayout &SL = *DL.getStructLayout(STy);
945       if (Offset.isNegative() || Offset.uge(SL.getSizeInBytes()))
946         break;
947 
948       // Determine which field of the struct the offset points into. The
949       // getZExtValue is fine as we've already ensured that the offset is
950       // within the range representable by the StructLayout API.
951       unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
952       NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
953                                          ElIdx));
954       Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
955       Ty = STy->getTypeAtIndex(ElIdx);
956     }
957   } while (Ty != ResElemTy);
958 
959   // If we haven't used up the entire offset by descending the static
960   // type, then the offset is pointing into the middle of an indivisible
961   // member, so we can't simplify it.
962   if (Offset != 0)
963     return nullptr;
964 
965   // Preserve the inrange index from the innermost GEP if possible. We must
966   // have calculated the same indices up to and including the inrange index.
967   Optional<unsigned> InRangeIndex;
968   if (Optional<unsigned> LastIRIndex = InnermostGEP->getInRangeIndex())
969     if (SrcElemTy == InnermostGEP->getSourceElementType() &&
970         NewIdxs.size() > *LastIRIndex) {
971       InRangeIndex = LastIRIndex;
972       for (unsigned I = 0; I <= *LastIRIndex; ++I)
973         if (NewIdxs[I] != InnermostGEP->getOperand(I + 1))
974           return nullptr;
975     }
976 
977   // Create a GEP.
978   Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs,
979                                                InBounds, InRangeIndex);
980   assert(C->getType()->getPointerElementType() == Ty &&
981          "Computed GetElementPtr has unexpected type!");
982 
983   // If we ended up indexing a member with a type that doesn't match
984   // the type of what the original indices indexed, add a cast.
985   if (Ty != ResElemTy)
986     C = FoldBitCast(C, ResTy, DL);
987 
988   return C;
989 }
990 
991 /// Attempt to constant fold an instruction with the
992 /// specified opcode and operands.  If successful, the constant result is
993 /// returned, if not, null is returned.  Note that this function can fail when
994 /// attempting to fold instructions like loads and stores, which have no
995 /// constant expression form.
996 Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
997                                        ArrayRef<Constant *> Ops,
998                                        const DataLayout &DL,
999                                        const TargetLibraryInfo *TLI) {
1000   Type *DestTy = InstOrCE->getType();
1001 
1002   if (Instruction::isUnaryOp(Opcode))
1003     return ConstantFoldUnaryOpOperand(Opcode, Ops[0], DL);
1004 
1005   if (Instruction::isBinaryOp(Opcode))
1006     return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL);
1007 
1008   if (Instruction::isCast(Opcode))
1009     return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL);
1010 
1011   if (auto *GEP = dyn_cast<GEPOperator>(InstOrCE)) {
1012     if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI))
1013       return C;
1014 
1015     return ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), Ops[0],
1016                                           Ops.slice(1), GEP->isInBounds(),
1017                                           GEP->getInRangeIndex());
1018   }
1019 
1020   if (auto *CE = dyn_cast<ConstantExpr>(InstOrCE))
1021     return CE->getWithOperands(Ops);
1022 
1023   switch (Opcode) {
1024   default: return nullptr;
1025   case Instruction::ICmp:
1026   case Instruction::FCmp: llvm_unreachable("Invalid for compares");
1027   case Instruction::Call:
1028     if (auto *F = dyn_cast<Function>(Ops.back())) {
1029       const auto *Call = cast<CallBase>(InstOrCE);
1030       if (canConstantFoldCallTo(Call, F))
1031         return ConstantFoldCall(Call, F, Ops.slice(0, Ops.size() - 1), TLI);
1032     }
1033     return nullptr;
1034   case Instruction::Select:
1035     return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
1036   case Instruction::ExtractElement:
1037     return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1038   case Instruction::InsertElement:
1039     return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1040   case Instruction::ShuffleVector:
1041     return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
1042   }
1043 }
1044 
1045 } // end anonymous namespace
1046 
1047 //===----------------------------------------------------------------------===//
1048 // Constant Folding public APIs
1049 //===----------------------------------------------------------------------===//
1050 
1051 namespace {
1052 
1053 Constant *
1054 ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL,
1055                          const TargetLibraryInfo *TLI,
1056                          SmallDenseMap<Constant *, Constant *> &FoldedOps) {
1057   if (!isa<ConstantVector>(C) && !isa<ConstantExpr>(C))
1058     return nullptr;
1059 
1060   SmallVector<Constant *, 8> Ops;
1061   for (const Use &NewU : C->operands()) {
1062     auto *NewC = cast<Constant>(&NewU);
1063     // Recursively fold the ConstantExpr's operands. If we have already folded
1064     // a ConstantExpr, we don't have to process it again.
1065     if (isa<ConstantVector>(NewC) || isa<ConstantExpr>(NewC)) {
1066       auto It = FoldedOps.find(NewC);
1067       if (It == FoldedOps.end()) {
1068         if (auto *FoldedC =
1069                 ConstantFoldConstantImpl(NewC, DL, TLI, FoldedOps)) {
1070           FoldedOps.insert({NewC, FoldedC});
1071           NewC = FoldedC;
1072         } else {
1073           FoldedOps.insert({NewC, NewC});
1074         }
1075       } else {
1076         NewC = It->second;
1077       }
1078     }
1079     Ops.push_back(NewC);
1080   }
1081 
1082   if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1083     if (CE->isCompare())
1084       return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
1085                                              DL, TLI);
1086 
1087     return ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI);
1088   }
1089 
1090   assert(isa<ConstantVector>(C));
1091   return ConstantVector::get(Ops);
1092 }
1093 
1094 } // end anonymous namespace
1095 
1096 Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
1097                                         const TargetLibraryInfo *TLI) {
1098   // Handle PHI nodes quickly here...
1099   if (auto *PN = dyn_cast<PHINode>(I)) {
1100     Constant *CommonValue = nullptr;
1101 
1102     SmallDenseMap<Constant *, Constant *> FoldedOps;
1103     for (Value *Incoming : PN->incoming_values()) {
1104       // If the incoming value is undef then skip it.  Note that while we could
1105       // skip the value if it is equal to the phi node itself we choose not to
1106       // because that would break the rule that constant folding only applies if
1107       // all operands are constants.
1108       if (isa<UndefValue>(Incoming))
1109         continue;
1110       // If the incoming value is not a constant, then give up.
1111       auto *C = dyn_cast<Constant>(Incoming);
1112       if (!C)
1113         return nullptr;
1114       // Fold the PHI's operands.
1115       if (auto *FoldedC = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps))
1116         C = FoldedC;
1117       // If the incoming value is a different constant to
1118       // the one we saw previously, then give up.
1119       if (CommonValue && C != CommonValue)
1120         return nullptr;
1121       CommonValue = C;
1122     }
1123 
1124     // If we reach here, all incoming values are the same constant or undef.
1125     return CommonValue ? CommonValue : UndefValue::get(PN->getType());
1126   }
1127 
1128   // Scan the operand list, checking to see if they are all constants, if so,
1129   // hand off to ConstantFoldInstOperandsImpl.
1130   if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); }))
1131     return nullptr;
1132 
1133   SmallDenseMap<Constant *, Constant *> FoldedOps;
1134   SmallVector<Constant *, 8> Ops;
1135   for (const Use &OpU : I->operands()) {
1136     auto *Op = cast<Constant>(&OpU);
1137     // Fold the Instruction's operands.
1138     if (auto *FoldedOp = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps))
1139       Op = FoldedOp;
1140 
1141     Ops.push_back(Op);
1142   }
1143 
1144   if (const auto *CI = dyn_cast<CmpInst>(I))
1145     return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
1146                                            DL, TLI);
1147 
1148   if (const auto *LI = dyn_cast<LoadInst>(I))
1149     return ConstantFoldLoadInst(LI, DL);
1150 
1151   if (auto *IVI = dyn_cast<InsertValueInst>(I)) {
1152     return ConstantExpr::getInsertValue(
1153                                 cast<Constant>(IVI->getAggregateOperand()),
1154                                 cast<Constant>(IVI->getInsertedValueOperand()),
1155                                 IVI->getIndices());
1156   }
1157 
1158   if (auto *EVI = dyn_cast<ExtractValueInst>(I)) {
1159     return ConstantExpr::getExtractValue(
1160                                     cast<Constant>(EVI->getAggregateOperand()),
1161                                     EVI->getIndices());
1162   }
1163 
1164   return ConstantFoldInstOperands(I, Ops, DL, TLI);
1165 }
1166 
1167 Constant *llvm::ConstantFoldConstant(const Constant *C, const DataLayout &DL,
1168                                      const TargetLibraryInfo *TLI) {
1169   SmallDenseMap<Constant *, Constant *> FoldedOps;
1170   return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1171 }
1172 
1173 Constant *llvm::ConstantFoldInstOperands(Instruction *I,
1174                                          ArrayRef<Constant *> Ops,
1175                                          const DataLayout &DL,
1176                                          const TargetLibraryInfo *TLI) {
1177   return ConstantFoldInstOperandsImpl(I, I->getOpcode(), Ops, DL, TLI);
1178 }
1179 
1180 Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
1181                                                 Constant *Ops0, Constant *Ops1,
1182                                                 const DataLayout &DL,
1183                                                 const TargetLibraryInfo *TLI) {
1184   // fold: icmp (inttoptr x), null         -> icmp x, 0
1185   // fold: icmp null, (inttoptr x)         -> icmp 0, x
1186   // fold: icmp (ptrtoint x), 0            -> icmp x, null
1187   // fold: icmp 0, (ptrtoint x)            -> icmp null, x
1188   // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
1189   // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
1190   //
1191   // FIXME: The following comment is out of data and the DataLayout is here now.
1192   // ConstantExpr::getCompare cannot do this, because it doesn't have DL
1193   // around to know if bit truncation is happening.
1194   if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
1195     if (Ops1->isNullValue()) {
1196       if (CE0->getOpcode() == Instruction::IntToPtr) {
1197         Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1198         // Convert the integer value to the right size to ensure we get the
1199         // proper extension or truncation.
1200         Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1201                                                    IntPtrTy, false);
1202         Constant *Null = Constant::getNullValue(C->getType());
1203         return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1204       }
1205 
1206       // Only do this transformation if the int is intptrty in size, otherwise
1207       // there is a truncation or extension that we aren't modeling.
1208       if (CE0->getOpcode() == Instruction::PtrToInt) {
1209         Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1210         if (CE0->getType() == IntPtrTy) {
1211           Constant *C = CE0->getOperand(0);
1212           Constant *Null = Constant::getNullValue(C->getType());
1213           return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1214         }
1215       }
1216     }
1217 
1218     if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
1219       if (CE0->getOpcode() == CE1->getOpcode()) {
1220         if (CE0->getOpcode() == Instruction::IntToPtr) {
1221           Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1222 
1223           // Convert the integer value to the right size to ensure we get the
1224           // proper extension or truncation.
1225           Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1226                                                       IntPtrTy, false);
1227           Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
1228                                                       IntPtrTy, false);
1229           return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
1230         }
1231 
1232         // Only do this transformation if the int is intptrty in size, otherwise
1233         // there is a truncation or extension that we aren't modeling.
1234         if (CE0->getOpcode() == Instruction::PtrToInt) {
1235           Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1236           if (CE0->getType() == IntPtrTy &&
1237               CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1238             return ConstantFoldCompareInstOperands(
1239                 Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI);
1240           }
1241         }
1242       }
1243     }
1244 
1245     // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
1246     // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
1247     if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
1248         CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
1249       Constant *LHS = ConstantFoldCompareInstOperands(
1250           Predicate, CE0->getOperand(0), Ops1, DL, TLI);
1251       Constant *RHS = ConstantFoldCompareInstOperands(
1252           Predicate, CE0->getOperand(1), Ops1, DL, TLI);
1253       unsigned OpC =
1254         Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
1255       return ConstantFoldBinaryOpOperands(OpC, LHS, RHS, DL);
1256     }
1257   } else if (isa<ConstantExpr>(Ops1)) {
1258     // If RHS is a constant expression, but the left side isn't, swap the
1259     // operands and try again.
1260     Predicate = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)Predicate);
1261     return ConstantFoldCompareInstOperands(Predicate, Ops1, Ops0, DL, TLI);
1262   }
1263 
1264   return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
1265 }
1266 
1267 Constant *llvm::ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
1268                                            const DataLayout &DL) {
1269   assert(Instruction::isUnaryOp(Opcode));
1270 
1271   return ConstantExpr::get(Opcode, Op);
1272 }
1273 
1274 Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
1275                                              Constant *RHS,
1276                                              const DataLayout &DL) {
1277   assert(Instruction::isBinaryOp(Opcode));
1278   if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS))
1279     if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL))
1280       return C;
1281 
1282   return ConstantExpr::get(Opcode, LHS, RHS);
1283 }
1284 
1285 Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
1286                                         Type *DestTy, const DataLayout &DL) {
1287   assert(Instruction::isCast(Opcode));
1288   switch (Opcode) {
1289   default:
1290     llvm_unreachable("Missing case");
1291   case Instruction::PtrToInt:
1292     // If the input is a inttoptr, eliminate the pair.  This requires knowing
1293     // the width of a pointer, so it can't be done in ConstantExpr::getCast.
1294     if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1295       if (CE->getOpcode() == Instruction::IntToPtr) {
1296         Constant *Input = CE->getOperand(0);
1297         unsigned InWidth = Input->getType()->getScalarSizeInBits();
1298         unsigned PtrWidth = DL.getPointerTypeSizeInBits(CE->getType());
1299         if (PtrWidth < InWidth) {
1300           Constant *Mask =
1301             ConstantInt::get(CE->getContext(),
1302                              APInt::getLowBitsSet(InWidth, PtrWidth));
1303           Input = ConstantExpr::getAnd(Input, Mask);
1304         }
1305         // Do a zext or trunc to get to the dest size.
1306         return ConstantExpr::getIntegerCast(Input, DestTy, false);
1307       }
1308     }
1309     return ConstantExpr::getCast(Opcode, C, DestTy);
1310   case Instruction::IntToPtr:
1311     // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
1312     // the int size is >= the ptr size and the address spaces are the same.
1313     // This requires knowing the width of a pointer, so it can't be done in
1314     // ConstantExpr::getCast.
1315     if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1316       if (CE->getOpcode() == Instruction::PtrToInt) {
1317         Constant *SrcPtr = CE->getOperand(0);
1318         unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType());
1319         unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1320 
1321         if (MidIntSize >= SrcPtrSize) {
1322           unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
1323           if (SrcAS == DestTy->getPointerAddressSpace())
1324             return FoldBitCast(CE->getOperand(0), DestTy, DL);
1325         }
1326       }
1327     }
1328 
1329     return ConstantExpr::getCast(Opcode, C, DestTy);
1330   case Instruction::Trunc:
1331   case Instruction::ZExt:
1332   case Instruction::SExt:
1333   case Instruction::FPTrunc:
1334   case Instruction::FPExt:
1335   case Instruction::UIToFP:
1336   case Instruction::SIToFP:
1337   case Instruction::FPToUI:
1338   case Instruction::FPToSI:
1339   case Instruction::AddrSpaceCast:
1340       return ConstantExpr::getCast(Opcode, C, DestTy);
1341   case Instruction::BitCast:
1342     return FoldBitCast(C, DestTy, DL);
1343   }
1344 }
1345 
1346 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
1347                                                        ConstantExpr *CE) {
1348   if (!CE->getOperand(1)->isNullValue())
1349     return nullptr;  // Do not allow stepping over the value!
1350 
1351   // Loop over all of the operands, tracking down which value we are
1352   // addressing.
1353   for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) {
1354     C = C->getAggregateElement(CE->getOperand(i));
1355     if (!C)
1356       return nullptr;
1357   }
1358   return C;
1359 }
1360 
1361 Constant *
1362 llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
1363                                         ArrayRef<Constant *> Indices) {
1364   // Loop over all of the operands, tracking down which value we are
1365   // addressing.
1366   for (Constant *Index : Indices) {
1367     C = C->getAggregateElement(Index);
1368     if (!C)
1369       return nullptr;
1370   }
1371   return C;
1372 }
1373 
1374 //===----------------------------------------------------------------------===//
1375 //  Constant Folding for Calls
1376 //
1377 
1378 bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
1379   if (Call->isNoBuiltin() || Call->isStrictFP())
1380     return false;
1381   switch (F->getIntrinsicID()) {
1382   case Intrinsic::fabs:
1383   case Intrinsic::minnum:
1384   case Intrinsic::maxnum:
1385   case Intrinsic::minimum:
1386   case Intrinsic::maximum:
1387   case Intrinsic::log:
1388   case Intrinsic::log2:
1389   case Intrinsic::log10:
1390   case Intrinsic::exp:
1391   case Intrinsic::exp2:
1392   case Intrinsic::floor:
1393   case Intrinsic::ceil:
1394   case Intrinsic::sqrt:
1395   case Intrinsic::sin:
1396   case Intrinsic::cos:
1397   case Intrinsic::trunc:
1398   case Intrinsic::rint:
1399   case Intrinsic::nearbyint:
1400   case Intrinsic::pow:
1401   case Intrinsic::powi:
1402   case Intrinsic::bswap:
1403   case Intrinsic::ctpop:
1404   case Intrinsic::ctlz:
1405   case Intrinsic::cttz:
1406   case Intrinsic::fshl:
1407   case Intrinsic::fshr:
1408   case Intrinsic::fma:
1409   case Intrinsic::fmuladd:
1410   case Intrinsic::copysign:
1411   case Intrinsic::launder_invariant_group:
1412   case Intrinsic::strip_invariant_group:
1413   case Intrinsic::round:
1414   case Intrinsic::masked_load:
1415   case Intrinsic::sadd_with_overflow:
1416   case Intrinsic::uadd_with_overflow:
1417   case Intrinsic::ssub_with_overflow:
1418   case Intrinsic::usub_with_overflow:
1419   case Intrinsic::smul_with_overflow:
1420   case Intrinsic::umul_with_overflow:
1421   case Intrinsic::sadd_sat:
1422   case Intrinsic::uadd_sat:
1423   case Intrinsic::ssub_sat:
1424   case Intrinsic::usub_sat:
1425   case Intrinsic::convert_from_fp16:
1426   case Intrinsic::convert_to_fp16:
1427   case Intrinsic::bitreverse:
1428   case Intrinsic::x86_sse_cvtss2si:
1429   case Intrinsic::x86_sse_cvtss2si64:
1430   case Intrinsic::x86_sse_cvttss2si:
1431   case Intrinsic::x86_sse_cvttss2si64:
1432   case Intrinsic::x86_sse2_cvtsd2si:
1433   case Intrinsic::x86_sse2_cvtsd2si64:
1434   case Intrinsic::x86_sse2_cvttsd2si:
1435   case Intrinsic::x86_sse2_cvttsd2si64:
1436   case Intrinsic::x86_avx512_vcvtss2si32:
1437   case Intrinsic::x86_avx512_vcvtss2si64:
1438   case Intrinsic::x86_avx512_cvttss2si:
1439   case Intrinsic::x86_avx512_cvttss2si64:
1440   case Intrinsic::x86_avx512_vcvtsd2si32:
1441   case Intrinsic::x86_avx512_vcvtsd2si64:
1442   case Intrinsic::x86_avx512_cvttsd2si:
1443   case Intrinsic::x86_avx512_cvttsd2si64:
1444   case Intrinsic::x86_avx512_vcvtss2usi32:
1445   case Intrinsic::x86_avx512_vcvtss2usi64:
1446   case Intrinsic::x86_avx512_cvttss2usi:
1447   case Intrinsic::x86_avx512_cvttss2usi64:
1448   case Intrinsic::x86_avx512_vcvtsd2usi32:
1449   case Intrinsic::x86_avx512_vcvtsd2usi64:
1450   case Intrinsic::x86_avx512_cvttsd2usi:
1451   case Intrinsic::x86_avx512_cvttsd2usi64:
1452   case Intrinsic::is_constant:
1453     return true;
1454   default:
1455     return false;
1456   case Intrinsic::not_intrinsic: break;
1457   }
1458 
1459   if (!F->hasName())
1460     return false;
1461   StringRef Name = F->getName();
1462 
1463   // In these cases, the check of the length is required.  We don't want to
1464   // return true for a name like "cos\0blah" which strcmp would return equal to
1465   // "cos", but has length 8.
1466   switch (Name[0]) {
1467   default:
1468     return false;
1469   case 'a':
1470     return Name == "acos" || Name == "asin" || Name == "atan" ||
1471            Name == "atan2" || Name == "acosf" || Name == "asinf" ||
1472            Name == "atanf" || Name == "atan2f";
1473   case 'c':
1474     return Name == "ceil" || Name == "cos" || Name == "cosh" ||
1475            Name == "ceilf" || Name == "cosf" || Name == "coshf";
1476   case 'e':
1477     return Name == "exp" || Name == "exp2" || Name == "expf" || Name == "exp2f";
1478   case 'f':
1479     return Name == "fabs" || Name == "floor" || Name == "fmod" ||
1480            Name == "fabsf" || Name == "floorf" || Name == "fmodf";
1481   case 'l':
1482     return Name == "log" || Name == "log10" || Name == "logf" ||
1483            Name == "log10f";
1484   case 'p':
1485     return Name == "pow" || Name == "powf";
1486   case 'r':
1487     return Name == "round" || Name == "roundf";
1488   case 's':
1489     return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
1490            Name == "sinf" || Name == "sinhf" || Name == "sqrtf";
1491   case 't':
1492     return Name == "tan" || Name == "tanh" || Name == "tanf" || Name == "tanhf";
1493   case '_':
1494 
1495     // Check for various function names that get used for the math functions
1496     // when the header files are preprocessed with the macro
1497     // __FINITE_MATH_ONLY__ enabled.
1498     // The '12' here is the length of the shortest name that can match.
1499     // We need to check the size before looking at Name[1] and Name[2]
1500     // so we may as well check a limit that will eliminate mismatches.
1501     if (Name.size() < 12 || Name[1] != '_')
1502       return false;
1503     switch (Name[2]) {
1504     default:
1505       return false;
1506     case 'a':
1507       return Name == "__acos_finite" || Name == "__acosf_finite" ||
1508              Name == "__asin_finite" || Name == "__asinf_finite" ||
1509              Name == "__atan2_finite" || Name == "__atan2f_finite";
1510     case 'c':
1511       return Name == "__cosh_finite" || Name == "__coshf_finite";
1512     case 'e':
1513       return Name == "__exp_finite" || Name == "__expf_finite" ||
1514              Name == "__exp2_finite" || Name == "__exp2f_finite";
1515     case 'l':
1516       return Name == "__log_finite" || Name == "__logf_finite" ||
1517              Name == "__log10_finite" || Name == "__log10f_finite";
1518     case 'p':
1519       return Name == "__pow_finite" || Name == "__powf_finite";
1520     case 's':
1521       return Name == "__sinh_finite" || Name == "__sinhf_finite";
1522     }
1523   }
1524 }
1525 
1526 namespace {
1527 
1528 Constant *GetConstantFoldFPValue(double V, Type *Ty) {
1529   if (Ty->isHalfTy() || Ty->isFloatTy()) {
1530     APFloat APF(V);
1531     bool unused;
1532     APF.convert(Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &unused);
1533     return ConstantFP::get(Ty->getContext(), APF);
1534   }
1535   if (Ty->isDoubleTy())
1536     return ConstantFP::get(Ty->getContext(), APFloat(V));
1537   llvm_unreachable("Can only constant fold half/float/double");
1538 }
1539 
1540 /// Clear the floating-point exception state.
1541 inline void llvm_fenv_clearexcept() {
1542 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT
1543   feclearexcept(FE_ALL_EXCEPT);
1544 #endif
1545   errno = 0;
1546 }
1547 
1548 /// Test if a floating-point exception was raised.
1549 inline bool llvm_fenv_testexcept() {
1550   int errno_val = errno;
1551   if (errno_val == ERANGE || errno_val == EDOM)
1552     return true;
1553 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT
1554   if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
1555     return true;
1556 #endif
1557   return false;
1558 }
1559 
1560 Constant *ConstantFoldFP(double (*NativeFP)(double), double V, Type *Ty) {
1561   llvm_fenv_clearexcept();
1562   V = NativeFP(V);
1563   if (llvm_fenv_testexcept()) {
1564     llvm_fenv_clearexcept();
1565     return nullptr;
1566   }
1567 
1568   return GetConstantFoldFPValue(V, Ty);
1569 }
1570 
1571 Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), double V,
1572                                double W, Type *Ty) {
1573   llvm_fenv_clearexcept();
1574   V = NativeFP(V, W);
1575   if (llvm_fenv_testexcept()) {
1576     llvm_fenv_clearexcept();
1577     return nullptr;
1578   }
1579 
1580   return GetConstantFoldFPValue(V, Ty);
1581 }
1582 
1583 /// Attempt to fold an SSE floating point to integer conversion of a constant
1584 /// floating point. If roundTowardZero is false, the default IEEE rounding is
1585 /// used (toward nearest, ties to even). This matches the behavior of the
1586 /// non-truncating SSE instructions in the default rounding mode. The desired
1587 /// integer type Ty is used to select how many bits are available for the
1588 /// result. Returns null if the conversion cannot be performed, otherwise
1589 /// returns the Constant value resulting from the conversion.
1590 Constant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero,
1591                                       Type *Ty, bool IsSigned) {
1592   // All of these conversion intrinsics form an integer of at most 64bits.
1593   unsigned ResultWidth = Ty->getIntegerBitWidth();
1594   assert(ResultWidth <= 64 &&
1595          "Can only constant fold conversions to 64 and 32 bit ints");
1596 
1597   uint64_t UIntVal;
1598   bool isExact = false;
1599   APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
1600                                               : APFloat::rmNearestTiesToEven;
1601   APFloat::opStatus status =
1602       Val.convertToInteger(makeMutableArrayRef(UIntVal), ResultWidth,
1603                            IsSigned, mode, &isExact);
1604   if (status != APFloat::opOK &&
1605       (!roundTowardZero || status != APFloat::opInexact))
1606     return nullptr;
1607   return ConstantInt::get(Ty, UIntVal, IsSigned);
1608 }
1609 
1610 double getValueAsDouble(ConstantFP *Op) {
1611   Type *Ty = Op->getType();
1612 
1613   if (Ty->isFloatTy())
1614     return Op->getValueAPF().convertToFloat();
1615 
1616   if (Ty->isDoubleTy())
1617     return Op->getValueAPF().convertToDouble();
1618 
1619   bool unused;
1620   APFloat APF = Op->getValueAPF();
1621   APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &unused);
1622   return APF.convertToDouble();
1623 }
1624 
1625 static bool isManifestConstant(const Constant *c) {
1626   if (isa<ConstantData>(c)) {
1627     return true;
1628   } else if (isa<ConstantAggregate>(c) || isa<ConstantExpr>(c)) {
1629     for (const Value *subc : c->operand_values()) {
1630       if (!isManifestConstant(cast<Constant>(subc)))
1631         return false;
1632     }
1633     return true;
1634   }
1635   return false;
1636 }
1637 
1638 static bool getConstIntOrUndef(Value *Op, const APInt *&C) {
1639   if (auto *CI = dyn_cast<ConstantInt>(Op)) {
1640     C = &CI->getValue();
1641     return true;
1642   }
1643   if (isa<UndefValue>(Op)) {
1644     C = nullptr;
1645     return true;
1646   }
1647   return false;
1648 }
1649 
1650 static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID,
1651                                         Type *Ty,
1652                                         ArrayRef<Constant *> Operands,
1653                                         const TargetLibraryInfo *TLI,
1654                                         const CallBase *Call) {
1655   if (Operands.size() == 1) {
1656     if (IntrinsicID == Intrinsic::is_constant) {
1657       // We know we have a "Constant" argument. But we want to only
1658       // return true for manifest constants, not those that depend on
1659       // constants with unknowable values, e.g. GlobalValue or BlockAddress.
1660       if (isManifestConstant(Operands[0]))
1661         return ConstantInt::getTrue(Ty->getContext());
1662       return nullptr;
1663     }
1664     if (isa<UndefValue>(Operands[0])) {
1665       // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN.
1666       // ctpop() is between 0 and bitwidth, pick 0 for undef.
1667       if (IntrinsicID == Intrinsic::cos ||
1668           IntrinsicID == Intrinsic::ctpop)
1669         return Constant::getNullValue(Ty);
1670       if (IntrinsicID == Intrinsic::bswap ||
1671           IntrinsicID == Intrinsic::bitreverse ||
1672           IntrinsicID == Intrinsic::launder_invariant_group ||
1673           IntrinsicID == Intrinsic::strip_invariant_group)
1674         return Operands[0];
1675     }
1676 
1677     if (isa<ConstantPointerNull>(Operands[0])) {
1678       // launder(null) == null == strip(null) iff in addrspace 0
1679       if (IntrinsicID == Intrinsic::launder_invariant_group ||
1680           IntrinsicID == Intrinsic::strip_invariant_group) {
1681         // If instruction is not yet put in a basic block (e.g. when cloning
1682         // a function during inlining), Call's caller may not be available.
1683         // So check Call's BB first before querying Call->getCaller.
1684         const Function *Caller =
1685             Call->getParent() ? Call->getCaller() : nullptr;
1686         if (Caller &&
1687             !NullPointerIsDefined(
1688                 Caller, Operands[0]->getType()->getPointerAddressSpace())) {
1689           return Operands[0];
1690         }
1691         return nullptr;
1692       }
1693     }
1694 
1695     if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) {
1696       if (IntrinsicID == Intrinsic::convert_to_fp16) {
1697         APFloat Val(Op->getValueAPF());
1698 
1699         bool lost = false;
1700         Val.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &lost);
1701 
1702         return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt());
1703       }
1704 
1705       if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1706         return nullptr;
1707 
1708       if (IntrinsicID == Intrinsic::round) {
1709         APFloat V = Op->getValueAPF();
1710         V.roundToIntegral(APFloat::rmNearestTiesToAway);
1711         return ConstantFP::get(Ty->getContext(), V);
1712       }
1713 
1714       if (IntrinsicID == Intrinsic::floor) {
1715         APFloat V = Op->getValueAPF();
1716         V.roundToIntegral(APFloat::rmTowardNegative);
1717         return ConstantFP::get(Ty->getContext(), V);
1718       }
1719 
1720       if (IntrinsicID == Intrinsic::ceil) {
1721         APFloat V = Op->getValueAPF();
1722         V.roundToIntegral(APFloat::rmTowardPositive);
1723         return ConstantFP::get(Ty->getContext(), V);
1724       }
1725 
1726       if (IntrinsicID == Intrinsic::trunc) {
1727         APFloat V = Op->getValueAPF();
1728         V.roundToIntegral(APFloat::rmTowardZero);
1729         return ConstantFP::get(Ty->getContext(), V);
1730       }
1731 
1732       if (IntrinsicID == Intrinsic::rint) {
1733         APFloat V = Op->getValueAPF();
1734         V.roundToIntegral(APFloat::rmNearestTiesToEven);
1735         return ConstantFP::get(Ty->getContext(), V);
1736       }
1737 
1738       if (IntrinsicID == Intrinsic::nearbyint) {
1739         APFloat V = Op->getValueAPF();
1740         V.roundToIntegral(APFloat::rmNearestTiesToEven);
1741         return ConstantFP::get(Ty->getContext(), V);
1742       }
1743 
1744       /// We only fold functions with finite arguments. Folding NaN and inf is
1745       /// likely to be aborted with an exception anyway, and some host libms
1746       /// have known errors raising exceptions.
1747       if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity())
1748         return nullptr;
1749 
1750       /// Currently APFloat versions of these functions do not exist, so we use
1751       /// the host native double versions.  Float versions are not called
1752       /// directly but for all these it is true (float)(f((double)arg)) ==
1753       /// f(arg).  Long double not supported yet.
1754       double V = getValueAsDouble(Op);
1755 
1756       switch (IntrinsicID) {
1757         default: break;
1758         case Intrinsic::fabs:
1759           return ConstantFoldFP(fabs, V, Ty);
1760         case Intrinsic::log2:
1761           return ConstantFoldFP(Log2, V, Ty);
1762         case Intrinsic::log:
1763           return ConstantFoldFP(log, V, Ty);
1764         case Intrinsic::log10:
1765           return ConstantFoldFP(log10, V, Ty);
1766         case Intrinsic::exp:
1767           return ConstantFoldFP(exp, V, Ty);
1768         case Intrinsic::exp2:
1769           return ConstantFoldFP(exp2, V, Ty);
1770         case Intrinsic::sin:
1771           return ConstantFoldFP(sin, V, Ty);
1772         case Intrinsic::cos:
1773           return ConstantFoldFP(cos, V, Ty);
1774         case Intrinsic::sqrt:
1775           return ConstantFoldFP(sqrt, V, Ty);
1776       }
1777 
1778       if (!TLI)
1779         return nullptr;
1780 
1781       char NameKeyChar = Name[0];
1782       if (Name[0] == '_' && Name.size() > 2 && Name[1] == '_')
1783         NameKeyChar = Name[2];
1784 
1785       switch (NameKeyChar) {
1786       case 'a':
1787         if ((Name == "acos" && TLI->has(LibFunc_acos)) ||
1788             (Name == "acosf" && TLI->has(LibFunc_acosf)) ||
1789             (Name == "__acos_finite" && TLI->has(LibFunc_acos_finite)) ||
1790             (Name == "__acosf_finite" && TLI->has(LibFunc_acosf_finite)))
1791           return ConstantFoldFP(acos, V, Ty);
1792         else if ((Name == "asin" && TLI->has(LibFunc_asin)) ||
1793                  (Name == "asinf" && TLI->has(LibFunc_asinf)) ||
1794                  (Name == "__asin_finite" && TLI->has(LibFunc_asin_finite)) ||
1795                  (Name == "__asinf_finite" && TLI->has(LibFunc_asinf_finite)))
1796           return ConstantFoldFP(asin, V, Ty);
1797         else if ((Name == "atan" && TLI->has(LibFunc_atan)) ||
1798                  (Name == "atanf" && TLI->has(LibFunc_atanf)))
1799           return ConstantFoldFP(atan, V, Ty);
1800         break;
1801       case 'c':
1802         if ((Name == "ceil" && TLI->has(LibFunc_ceil)) ||
1803             (Name == "ceilf" && TLI->has(LibFunc_ceilf)))
1804           return ConstantFoldFP(ceil, V, Ty);
1805         else if ((Name == "cos" && TLI->has(LibFunc_cos)) ||
1806                  (Name == "cosf" && TLI->has(LibFunc_cosf)))
1807           return ConstantFoldFP(cos, V, Ty);
1808         else if ((Name == "cosh" && TLI->has(LibFunc_cosh)) ||
1809                  (Name == "coshf" && TLI->has(LibFunc_coshf)) ||
1810                  (Name == "__cosh_finite" && TLI->has(LibFunc_cosh_finite)) ||
1811                  (Name == "__coshf_finite" && TLI->has(LibFunc_coshf_finite)))
1812           return ConstantFoldFP(cosh, V, Ty);
1813         break;
1814       case 'e':
1815         if ((Name == "exp" && TLI->has(LibFunc_exp)) ||
1816             (Name == "expf" && TLI->has(LibFunc_expf)) ||
1817             (Name == "__exp_finite" && TLI->has(LibFunc_exp_finite)) ||
1818             (Name == "__expf_finite" && TLI->has(LibFunc_expf_finite)))
1819           return ConstantFoldFP(exp, V, Ty);
1820         if ((Name == "exp2" && TLI->has(LibFunc_exp2)) ||
1821             (Name == "exp2f" && TLI->has(LibFunc_exp2f)) ||
1822             (Name == "__exp2_finite" && TLI->has(LibFunc_exp2_finite)) ||
1823             (Name == "__exp2f_finite" && TLI->has(LibFunc_exp2f_finite)))
1824           // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
1825           // C99 library.
1826           return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
1827         break;
1828       case 'f':
1829         if ((Name == "fabs" && TLI->has(LibFunc_fabs)) ||
1830             (Name == "fabsf" && TLI->has(LibFunc_fabsf)))
1831           return ConstantFoldFP(fabs, V, Ty);
1832         else if ((Name == "floor" && TLI->has(LibFunc_floor)) ||
1833                  (Name == "floorf" && TLI->has(LibFunc_floorf)))
1834           return ConstantFoldFP(floor, V, Ty);
1835         break;
1836       case 'l':
1837         if ((Name == "log" && V > 0 && TLI->has(LibFunc_log)) ||
1838             (Name == "logf" && V > 0 && TLI->has(LibFunc_logf)) ||
1839             (Name == "__log_finite" && V > 0 &&
1840               TLI->has(LibFunc_log_finite)) ||
1841             (Name == "__logf_finite" && V > 0 &&
1842               TLI->has(LibFunc_logf_finite)))
1843           return ConstantFoldFP(log, V, Ty);
1844         else if ((Name == "log10" && V > 0 && TLI->has(LibFunc_log10)) ||
1845                  (Name == "log10f" && V > 0 && TLI->has(LibFunc_log10f)) ||
1846                  (Name == "__log10_finite" && V > 0 &&
1847                    TLI->has(LibFunc_log10_finite)) ||
1848                  (Name == "__log10f_finite" && V > 0 &&
1849                    TLI->has(LibFunc_log10f_finite)))
1850           return ConstantFoldFP(log10, V, Ty);
1851         break;
1852       case 'r':
1853         if ((Name == "round" && TLI->has(LibFunc_round)) ||
1854             (Name == "roundf" && TLI->has(LibFunc_roundf)))
1855           return ConstantFoldFP(round, V, Ty);
1856         break;
1857       case 's':
1858         if ((Name == "sin" && TLI->has(LibFunc_sin)) ||
1859             (Name == "sinf" && TLI->has(LibFunc_sinf)))
1860           return ConstantFoldFP(sin, V, Ty);
1861         else if ((Name == "sinh" && TLI->has(LibFunc_sinh)) ||
1862                  (Name == "sinhf" && TLI->has(LibFunc_sinhf)) ||
1863                  (Name == "__sinh_finite" && TLI->has(LibFunc_sinh_finite)) ||
1864                  (Name == "__sinhf_finite" && TLI->has(LibFunc_sinhf_finite)))
1865           return ConstantFoldFP(sinh, V, Ty);
1866         else if ((Name == "sqrt" && V >= 0 && TLI->has(LibFunc_sqrt)) ||
1867                  (Name == "sqrtf" && V >= 0 && TLI->has(LibFunc_sqrtf)))
1868           return ConstantFoldFP(sqrt, V, Ty);
1869         break;
1870       case 't':
1871         if ((Name == "tan" && TLI->has(LibFunc_tan)) ||
1872             (Name == "tanf" && TLI->has(LibFunc_tanf)))
1873           return ConstantFoldFP(tan, V, Ty);
1874         else if ((Name == "tanh" && TLI->has(LibFunc_tanh)) ||
1875                  (Name == "tanhf" && TLI->has(LibFunc_tanhf)))
1876           return ConstantFoldFP(tanh, V, Ty);
1877         break;
1878       default:
1879         break;
1880       }
1881       return nullptr;
1882     }
1883 
1884     if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
1885       switch (IntrinsicID) {
1886       case Intrinsic::bswap:
1887         return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
1888       case Intrinsic::ctpop:
1889         return ConstantInt::get(Ty, Op->getValue().countPopulation());
1890       case Intrinsic::bitreverse:
1891         return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits());
1892       case Intrinsic::convert_from_fp16: {
1893         APFloat Val(APFloat::IEEEhalf(), Op->getValue());
1894 
1895         bool lost = false;
1896         APFloat::opStatus status = Val.convert(
1897             Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &lost);
1898 
1899         // Conversion is always precise.
1900         (void)status;
1901         assert(status == APFloat::opOK && !lost &&
1902                "Precision lost during fp16 constfolding");
1903 
1904         return ConstantFP::get(Ty->getContext(), Val);
1905       }
1906       default:
1907         return nullptr;
1908       }
1909     }
1910 
1911     // Support ConstantVector in case we have an Undef in the top.
1912     if (isa<ConstantVector>(Operands[0]) ||
1913         isa<ConstantDataVector>(Operands[0])) {
1914       auto *Op = cast<Constant>(Operands[0]);
1915       switch (IntrinsicID) {
1916       default: break;
1917       case Intrinsic::x86_sse_cvtss2si:
1918       case Intrinsic::x86_sse_cvtss2si64:
1919       case Intrinsic::x86_sse2_cvtsd2si:
1920       case Intrinsic::x86_sse2_cvtsd2si64:
1921         if (ConstantFP *FPOp =
1922                 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
1923           return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
1924                                              /*roundTowardZero=*/false, Ty,
1925                                              /*IsSigned*/true);
1926         break;
1927       case Intrinsic::x86_sse_cvttss2si:
1928       case Intrinsic::x86_sse_cvttss2si64:
1929       case Intrinsic::x86_sse2_cvttsd2si:
1930       case Intrinsic::x86_sse2_cvttsd2si64:
1931         if (ConstantFP *FPOp =
1932                 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
1933           return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
1934                                              /*roundTowardZero=*/true, Ty,
1935                                              /*IsSigned*/true);
1936         break;
1937       }
1938     }
1939 
1940     return nullptr;
1941   }
1942 
1943   if (Operands.size() == 2) {
1944     if (auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
1945       if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1946         return nullptr;
1947       double Op1V = getValueAsDouble(Op1);
1948 
1949       if (auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
1950         if (Op2->getType() != Op1->getType())
1951           return nullptr;
1952 
1953         double Op2V = getValueAsDouble(Op2);
1954         if (IntrinsicID == Intrinsic::pow) {
1955           return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
1956         }
1957         if (IntrinsicID == Intrinsic::copysign) {
1958           APFloat V1 = Op1->getValueAPF();
1959           const APFloat &V2 = Op2->getValueAPF();
1960           V1.copySign(V2);
1961           return ConstantFP::get(Ty->getContext(), V1);
1962         }
1963 
1964         if (IntrinsicID == Intrinsic::minnum) {
1965           const APFloat &C1 = Op1->getValueAPF();
1966           const APFloat &C2 = Op2->getValueAPF();
1967           return ConstantFP::get(Ty->getContext(), minnum(C1, C2));
1968         }
1969 
1970         if (IntrinsicID == Intrinsic::maxnum) {
1971           const APFloat &C1 = Op1->getValueAPF();
1972           const APFloat &C2 = Op2->getValueAPF();
1973           return ConstantFP::get(Ty->getContext(), maxnum(C1, C2));
1974         }
1975 
1976         if (IntrinsicID == Intrinsic::minimum) {
1977           const APFloat &C1 = Op1->getValueAPF();
1978           const APFloat &C2 = Op2->getValueAPF();
1979           return ConstantFP::get(Ty->getContext(), minimum(C1, C2));
1980         }
1981 
1982         if (IntrinsicID == Intrinsic::maximum) {
1983           const APFloat &C1 = Op1->getValueAPF();
1984           const APFloat &C2 = Op2->getValueAPF();
1985           return ConstantFP::get(Ty->getContext(), maximum(C1, C2));
1986         }
1987 
1988         if (!TLI)
1989           return nullptr;
1990         if ((Name == "pow" && TLI->has(LibFunc_pow)) ||
1991             (Name == "powf" && TLI->has(LibFunc_powf)) ||
1992             (Name == "__pow_finite" && TLI->has(LibFunc_pow_finite)) ||
1993             (Name == "__powf_finite" && TLI->has(LibFunc_powf_finite)))
1994           return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
1995         if ((Name == "fmod" && TLI->has(LibFunc_fmod)) ||
1996             (Name == "fmodf" && TLI->has(LibFunc_fmodf)))
1997           return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
1998         if ((Name == "atan2" && TLI->has(LibFunc_atan2)) ||
1999             (Name == "atan2f" && TLI->has(LibFunc_atan2f)) ||
2000             (Name == "__atan2_finite" && TLI->has(LibFunc_atan2_finite)) ||
2001             (Name == "__atan2f_finite" && TLI->has(LibFunc_atan2f_finite)))
2002           return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
2003       } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
2004         if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
2005           return ConstantFP::get(Ty->getContext(),
2006                                  APFloat((float)std::pow((float)Op1V,
2007                                                  (int)Op2C->getZExtValue())));
2008         if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy())
2009           return ConstantFP::get(Ty->getContext(),
2010                                  APFloat((float)std::pow((float)Op1V,
2011                                                  (int)Op2C->getZExtValue())));
2012         if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy())
2013           return ConstantFP::get(Ty->getContext(),
2014                                  APFloat((double)std::pow((double)Op1V,
2015                                                    (int)Op2C->getZExtValue())));
2016       }
2017       return nullptr;
2018     }
2019 
2020     if (Operands[0]->getType()->isIntegerTy() &&
2021         Operands[1]->getType()->isIntegerTy()) {
2022       const APInt *C0, *C1;
2023       if (!getConstIntOrUndef(Operands[0], C0) ||
2024           !getConstIntOrUndef(Operands[1], C1))
2025         return nullptr;
2026 
2027       switch (IntrinsicID) {
2028       default: break;
2029       case Intrinsic::smul_with_overflow:
2030       case Intrinsic::umul_with_overflow:
2031         // Even if both operands are undef, we cannot fold muls to undef
2032         // in the general case. For example, on i2 there are no inputs
2033         // that would produce { i2 -1, i1 true } as the result.
2034         if (!C0 || !C1)
2035           return Constant::getNullValue(Ty);
2036         LLVM_FALLTHROUGH;
2037       case Intrinsic::sadd_with_overflow:
2038       case Intrinsic::uadd_with_overflow:
2039       case Intrinsic::ssub_with_overflow:
2040       case Intrinsic::usub_with_overflow: {
2041         if (!C0 || !C1)
2042           return UndefValue::get(Ty);
2043 
2044         APInt Res;
2045         bool Overflow;
2046         switch (IntrinsicID) {
2047         default: llvm_unreachable("Invalid case");
2048         case Intrinsic::sadd_with_overflow:
2049           Res = C0->sadd_ov(*C1, Overflow);
2050           break;
2051         case Intrinsic::uadd_with_overflow:
2052           Res = C0->uadd_ov(*C1, Overflow);
2053           break;
2054         case Intrinsic::ssub_with_overflow:
2055           Res = C0->ssub_ov(*C1, Overflow);
2056           break;
2057         case Intrinsic::usub_with_overflow:
2058           Res = C0->usub_ov(*C1, Overflow);
2059           break;
2060         case Intrinsic::smul_with_overflow:
2061           Res = C0->smul_ov(*C1, Overflow);
2062           break;
2063         case Intrinsic::umul_with_overflow:
2064           Res = C0->umul_ov(*C1, Overflow);
2065           break;
2066         }
2067         Constant *Ops[] = {
2068           ConstantInt::get(Ty->getContext(), Res),
2069           ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow)
2070         };
2071         return ConstantStruct::get(cast<StructType>(Ty), Ops);
2072       }
2073       case Intrinsic::uadd_sat:
2074       case Intrinsic::sadd_sat:
2075         if (!C0 && !C1)
2076           return UndefValue::get(Ty);
2077         if (!C0 || !C1)
2078           return Constant::getAllOnesValue(Ty);
2079         if (IntrinsicID == Intrinsic::uadd_sat)
2080           return ConstantInt::get(Ty, C0->uadd_sat(*C1));
2081         else
2082           return ConstantInt::get(Ty, C0->sadd_sat(*C1));
2083       case Intrinsic::usub_sat:
2084       case Intrinsic::ssub_sat:
2085         if (!C0 && !C1)
2086           return UndefValue::get(Ty);
2087         if (!C0 || !C1)
2088           return Constant::getNullValue(Ty);
2089         if (IntrinsicID == Intrinsic::usub_sat)
2090           return ConstantInt::get(Ty, C0->usub_sat(*C1));
2091         else
2092           return ConstantInt::get(Ty, C0->ssub_sat(*C1));
2093       case Intrinsic::cttz:
2094       case Intrinsic::ctlz:
2095         assert(C1 && "Must be constant int");
2096 
2097         // cttz(0, 1) and ctlz(0, 1) are undef.
2098         if (C1->isOneValue() && (!C0 || C0->isNullValue()))
2099           return UndefValue::get(Ty);
2100         if (!C0)
2101           return Constant::getNullValue(Ty);
2102         if (IntrinsicID == Intrinsic::cttz)
2103           return ConstantInt::get(Ty, C0->countTrailingZeros());
2104         else
2105           return ConstantInt::get(Ty, C0->countLeadingZeros());
2106       }
2107 
2108       return nullptr;
2109     }
2110 
2111     // Support ConstantVector in case we have an Undef in the top.
2112     if ((isa<ConstantVector>(Operands[0]) ||
2113          isa<ConstantDataVector>(Operands[0])) &&
2114         // Check for default rounding mode.
2115         // FIXME: Support other rounding modes?
2116         isa<ConstantInt>(Operands[1]) &&
2117         cast<ConstantInt>(Operands[1])->getValue() == 4) {
2118       auto *Op = cast<Constant>(Operands[0]);
2119       switch (IntrinsicID) {
2120       default: break;
2121       case Intrinsic::x86_avx512_vcvtss2si32:
2122       case Intrinsic::x86_avx512_vcvtss2si64:
2123       case Intrinsic::x86_avx512_vcvtsd2si32:
2124       case Intrinsic::x86_avx512_vcvtsd2si64:
2125         if (ConstantFP *FPOp =
2126                 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2127           return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2128                                              /*roundTowardZero=*/false, Ty,
2129                                              /*IsSigned*/true);
2130         break;
2131       case Intrinsic::x86_avx512_vcvtss2usi32:
2132       case Intrinsic::x86_avx512_vcvtss2usi64:
2133       case Intrinsic::x86_avx512_vcvtsd2usi32:
2134       case Intrinsic::x86_avx512_vcvtsd2usi64:
2135         if (ConstantFP *FPOp =
2136                 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2137           return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2138                                              /*roundTowardZero=*/false, Ty,
2139                                              /*IsSigned*/false);
2140         break;
2141       case Intrinsic::x86_avx512_cvttss2si:
2142       case Intrinsic::x86_avx512_cvttss2si64:
2143       case Intrinsic::x86_avx512_cvttsd2si:
2144       case Intrinsic::x86_avx512_cvttsd2si64:
2145         if (ConstantFP *FPOp =
2146                 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2147           return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2148                                              /*roundTowardZero=*/true, Ty,
2149                                              /*IsSigned*/true);
2150         break;
2151       case Intrinsic::x86_avx512_cvttss2usi:
2152       case Intrinsic::x86_avx512_cvttss2usi64:
2153       case Intrinsic::x86_avx512_cvttsd2usi:
2154       case Intrinsic::x86_avx512_cvttsd2usi64:
2155         if (ConstantFP *FPOp =
2156                 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
2157           return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2158                                              /*roundTowardZero=*/true, Ty,
2159                                              /*IsSigned*/false);
2160         break;
2161       }
2162     }
2163     return nullptr;
2164   }
2165 
2166   if (Operands.size() != 3)
2167     return nullptr;
2168 
2169   if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
2170     if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
2171       if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
2172         switch (IntrinsicID) {
2173         default: break;
2174         case Intrinsic::fma:
2175         case Intrinsic::fmuladd: {
2176           APFloat V = Op1->getValueAPF();
2177           APFloat::opStatus s = V.fusedMultiplyAdd(Op2->getValueAPF(),
2178                                                    Op3->getValueAPF(),
2179                                                    APFloat::rmNearestTiesToEven);
2180           if (s != APFloat::opInvalidOp)
2181             return ConstantFP::get(Ty->getContext(), V);
2182 
2183           return nullptr;
2184         }
2185         }
2186       }
2187     }
2188   }
2189 
2190   if (IntrinsicID == Intrinsic::fshl || IntrinsicID == Intrinsic::fshr) {
2191     const APInt *C0, *C1, *C2;
2192     if (!getConstIntOrUndef(Operands[0], C0) ||
2193         !getConstIntOrUndef(Operands[1], C1) ||
2194         !getConstIntOrUndef(Operands[2], C2))
2195       return nullptr;
2196 
2197     bool IsRight = IntrinsicID == Intrinsic::fshr;
2198     if (!C2)
2199       return Operands[IsRight ? 1 : 0];
2200     if (!C0 && !C1)
2201       return UndefValue::get(Ty);
2202 
2203     // The shift amount is interpreted as modulo the bitwidth. If the shift
2204     // amount is effectively 0, avoid UB due to oversized inverse shift below.
2205     unsigned BitWidth = C2->getBitWidth();
2206     unsigned ShAmt = C2->urem(BitWidth);
2207     if (!ShAmt)
2208       return Operands[IsRight ? 1 : 0];
2209 
2210     // (C0 << ShlAmt) | (C1 >> LshrAmt)
2211     unsigned LshrAmt = IsRight ? ShAmt : BitWidth - ShAmt;
2212     unsigned ShlAmt = !IsRight ? ShAmt : BitWidth - ShAmt;
2213     if (!C0)
2214       return ConstantInt::get(Ty, C1->lshr(LshrAmt));
2215     if (!C1)
2216       return ConstantInt::get(Ty, C0->shl(ShlAmt));
2217     return ConstantInt::get(Ty, C0->shl(ShlAmt) | C1->lshr(LshrAmt));
2218   }
2219 
2220   return nullptr;
2221 }
2222 
2223 static Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID,
2224                                         VectorType *VTy,
2225                                         ArrayRef<Constant *> Operands,
2226                                         const DataLayout &DL,
2227                                         const TargetLibraryInfo *TLI,
2228                                         const CallBase *Call) {
2229   SmallVector<Constant *, 4> Result(VTy->getNumElements());
2230   SmallVector<Constant *, 4> Lane(Operands.size());
2231   Type *Ty = VTy->getElementType();
2232 
2233   if (IntrinsicID == Intrinsic::masked_load) {
2234     auto *SrcPtr = Operands[0];
2235     auto *Mask = Operands[2];
2236     auto *Passthru = Operands[3];
2237 
2238     Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, VTy, DL);
2239 
2240     SmallVector<Constant *, 32> NewElements;
2241     for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
2242       auto *MaskElt = Mask->getAggregateElement(I);
2243       if (!MaskElt)
2244         break;
2245       auto *PassthruElt = Passthru->getAggregateElement(I);
2246       auto *VecElt = VecData ? VecData->getAggregateElement(I) : nullptr;
2247       if (isa<UndefValue>(MaskElt)) {
2248         if (PassthruElt)
2249           NewElements.push_back(PassthruElt);
2250         else if (VecElt)
2251           NewElements.push_back(VecElt);
2252         else
2253           return nullptr;
2254       }
2255       if (MaskElt->isNullValue()) {
2256         if (!PassthruElt)
2257           return nullptr;
2258         NewElements.push_back(PassthruElt);
2259       } else if (MaskElt->isOneValue()) {
2260         if (!VecElt)
2261           return nullptr;
2262         NewElements.push_back(VecElt);
2263       } else {
2264         return nullptr;
2265       }
2266     }
2267     if (NewElements.size() != VTy->getNumElements())
2268       return nullptr;
2269     return ConstantVector::get(NewElements);
2270   }
2271 
2272   for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
2273     // Gather a column of constants.
2274     for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
2275       // These intrinsics use a scalar type for their second argument.
2276       if (J == 1 &&
2277           (IntrinsicID == Intrinsic::cttz || IntrinsicID == Intrinsic::ctlz ||
2278            IntrinsicID == Intrinsic::powi)) {
2279         Lane[J] = Operands[J];
2280         continue;
2281       }
2282 
2283       Constant *Agg = Operands[J]->getAggregateElement(I);
2284       if (!Agg)
2285         return nullptr;
2286 
2287       Lane[J] = Agg;
2288     }
2289 
2290     // Use the regular scalar folding to simplify this column.
2291     Constant *Folded =
2292         ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI, Call);
2293     if (!Folded)
2294       return nullptr;
2295     Result[I] = Folded;
2296   }
2297 
2298   return ConstantVector::get(Result);
2299 }
2300 
2301 } // end anonymous namespace
2302 
2303 Constant *llvm::ConstantFoldCall(const CallBase *Call, Function *F,
2304                                  ArrayRef<Constant *> Operands,
2305                                  const TargetLibraryInfo *TLI) {
2306   if (Call->isNoBuiltin() || Call->isStrictFP())
2307     return nullptr;
2308   if (!F->hasName())
2309     return nullptr;
2310   StringRef Name = F->getName();
2311 
2312   Type *Ty = F->getReturnType();
2313 
2314   if (auto *VTy = dyn_cast<VectorType>(Ty))
2315     return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands,
2316                                   F->getParent()->getDataLayout(), TLI, Call);
2317 
2318   return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI,
2319                                 Call);
2320 }
2321 
2322 bool llvm::isMathLibCallNoop(const CallBase *Call,
2323                              const TargetLibraryInfo *TLI) {
2324   // FIXME: Refactor this code; this duplicates logic in LibCallsShrinkWrap
2325   // (and to some extent ConstantFoldScalarCall).
2326   if (Call->isNoBuiltin() || Call->isStrictFP())
2327     return false;
2328   Function *F = Call->getCalledFunction();
2329   if (!F)
2330     return false;
2331 
2332   LibFunc Func;
2333   if (!TLI || !TLI->getLibFunc(*F, Func))
2334     return false;
2335 
2336   if (Call->getNumArgOperands() == 1) {
2337     if (ConstantFP *OpC = dyn_cast<ConstantFP>(Call->getArgOperand(0))) {
2338       const APFloat &Op = OpC->getValueAPF();
2339       switch (Func) {
2340       case LibFunc_logl:
2341       case LibFunc_log:
2342       case LibFunc_logf:
2343       case LibFunc_log2l:
2344       case LibFunc_log2:
2345       case LibFunc_log2f:
2346       case LibFunc_log10l:
2347       case LibFunc_log10:
2348       case LibFunc_log10f:
2349         return Op.isNaN() || (!Op.isZero() && !Op.isNegative());
2350 
2351       case LibFunc_expl:
2352       case LibFunc_exp:
2353       case LibFunc_expf:
2354         // FIXME: These boundaries are slightly conservative.
2355         if (OpC->getType()->isDoubleTy())
2356           return Op.compare(APFloat(-745.0)) != APFloat::cmpLessThan &&
2357                  Op.compare(APFloat(709.0)) != APFloat::cmpGreaterThan;
2358         if (OpC->getType()->isFloatTy())
2359           return Op.compare(APFloat(-103.0f)) != APFloat::cmpLessThan &&
2360                  Op.compare(APFloat(88.0f)) != APFloat::cmpGreaterThan;
2361         break;
2362 
2363       case LibFunc_exp2l:
2364       case LibFunc_exp2:
2365       case LibFunc_exp2f:
2366         // FIXME: These boundaries are slightly conservative.
2367         if (OpC->getType()->isDoubleTy())
2368           return Op.compare(APFloat(-1074.0)) != APFloat::cmpLessThan &&
2369                  Op.compare(APFloat(1023.0)) != APFloat::cmpGreaterThan;
2370         if (OpC->getType()->isFloatTy())
2371           return Op.compare(APFloat(-149.0f)) != APFloat::cmpLessThan &&
2372                  Op.compare(APFloat(127.0f)) != APFloat::cmpGreaterThan;
2373         break;
2374 
2375       case LibFunc_sinl:
2376       case LibFunc_sin:
2377       case LibFunc_sinf:
2378       case LibFunc_cosl:
2379       case LibFunc_cos:
2380       case LibFunc_cosf:
2381         return !Op.isInfinity();
2382 
2383       case LibFunc_tanl:
2384       case LibFunc_tan:
2385       case LibFunc_tanf: {
2386         // FIXME: Stop using the host math library.
2387         // FIXME: The computation isn't done in the right precision.
2388         Type *Ty = OpC->getType();
2389         if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) {
2390           double OpV = getValueAsDouble(OpC);
2391           return ConstantFoldFP(tan, OpV, Ty) != nullptr;
2392         }
2393         break;
2394       }
2395 
2396       case LibFunc_asinl:
2397       case LibFunc_asin:
2398       case LibFunc_asinf:
2399       case LibFunc_acosl:
2400       case LibFunc_acos:
2401       case LibFunc_acosf:
2402         return Op.compare(APFloat(Op.getSemantics(), "-1")) !=
2403                    APFloat::cmpLessThan &&
2404                Op.compare(APFloat(Op.getSemantics(), "1")) !=
2405                    APFloat::cmpGreaterThan;
2406 
2407       case LibFunc_sinh:
2408       case LibFunc_cosh:
2409       case LibFunc_sinhf:
2410       case LibFunc_coshf:
2411       case LibFunc_sinhl:
2412       case LibFunc_coshl:
2413         // FIXME: These boundaries are slightly conservative.
2414         if (OpC->getType()->isDoubleTy())
2415           return Op.compare(APFloat(-710.0)) != APFloat::cmpLessThan &&
2416                  Op.compare(APFloat(710.0)) != APFloat::cmpGreaterThan;
2417         if (OpC->getType()->isFloatTy())
2418           return Op.compare(APFloat(-89.0f)) != APFloat::cmpLessThan &&
2419                  Op.compare(APFloat(89.0f)) != APFloat::cmpGreaterThan;
2420         break;
2421 
2422       case LibFunc_sqrtl:
2423       case LibFunc_sqrt:
2424       case LibFunc_sqrtf:
2425         return Op.isNaN() || Op.isZero() || !Op.isNegative();
2426 
2427       // FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p,
2428       // maybe others?
2429       default:
2430         break;
2431       }
2432     }
2433   }
2434 
2435   if (Call->getNumArgOperands() == 2) {
2436     ConstantFP *Op0C = dyn_cast<ConstantFP>(Call->getArgOperand(0));
2437     ConstantFP *Op1C = dyn_cast<ConstantFP>(Call->getArgOperand(1));
2438     if (Op0C && Op1C) {
2439       const APFloat &Op0 = Op0C->getValueAPF();
2440       const APFloat &Op1 = Op1C->getValueAPF();
2441 
2442       switch (Func) {
2443       case LibFunc_powl:
2444       case LibFunc_pow:
2445       case LibFunc_powf: {
2446         // FIXME: Stop using the host math library.
2447         // FIXME: The computation isn't done in the right precision.
2448         Type *Ty = Op0C->getType();
2449         if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) {
2450           if (Ty == Op1C->getType()) {
2451             double Op0V = getValueAsDouble(Op0C);
2452             double Op1V = getValueAsDouble(Op1C);
2453             return ConstantFoldBinaryFP(pow, Op0V, Op1V, Ty) != nullptr;
2454           }
2455         }
2456         break;
2457       }
2458 
2459       case LibFunc_fmodl:
2460       case LibFunc_fmod:
2461       case LibFunc_fmodf:
2462         return Op0.isNaN() || Op1.isNaN() ||
2463                (!Op0.isInfinity() && !Op1.isZero());
2464 
2465       default:
2466         break;
2467       }
2468     }
2469   }
2470 
2471   return false;
2472 }
2473