]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Analysis/ConstantFolding.cpp
MFV r317781:
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Analysis / ConstantFolding.cpp
1 //===-- ConstantFolding.cpp - Fold instructions into constants ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines routines for folding instructions into constants.
11 //
12 // Also, to supplement the basic IR ConstantExpr simplifications,
13 // this file defines some additional folding routines that can make use of
14 // DataLayout information. These functions cannot go in IR due to library
15 // dependency issues.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Analysis/ConstantFolding.h"
20 #include "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/APInt.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Analysis/TargetLibraryInfo.h"
28 #include "llvm/Analysis/ValueTracking.h"
29 #include "llvm/Config/config.h"
30 #include "llvm/IR/Constant.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/GlobalValue.h"
36 #include "llvm/IR/GlobalVariable.h"
37 #include "llvm/IR/InstrTypes.h"
38 #include "llvm/IR/Instruction.h"
39 #include "llvm/IR/Instructions.h"
40 #include "llvm/IR/Operator.h"
41 #include "llvm/IR/Type.h"
42 #include "llvm/IR/Value.h"
43 #include "llvm/Support/Casting.h"
44 #include "llvm/Support/ErrorHandling.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.getPointerTypeSizeInBits(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.getPointerTypeSizeInBits(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 namespace {
323
324 /// Recursive helper to read bits out of global. C is the constant being copied
325 /// out of. ByteOffset is an offset into C. CurPtr is the pointer to copy
326 /// results into and BytesLeft is the number of bytes left in
327 /// the CurPtr buffer. DL is the DataLayout.
328 bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr,
329                         unsigned BytesLeft, const DataLayout &DL) {
330   assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) &&
331          "Out of range access");
332
333   // If this element is zero or undefined, we can just return since *CurPtr is
334   // zero initialized.
335   if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
336     return true;
337
338   if (auto *CI = dyn_cast<ConstantInt>(C)) {
339     if (CI->getBitWidth() > 64 ||
340         (CI->getBitWidth() & 7) != 0)
341       return false;
342
343     uint64_t Val = CI->getZExtValue();
344     unsigned IntBytes = unsigned(CI->getBitWidth()/8);
345
346     for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
347       int n = ByteOffset;
348       if (!DL.isLittleEndian())
349         n = IntBytes - n - 1;
350       CurPtr[i] = (unsigned char)(Val >> (n * 8));
351       ++ByteOffset;
352     }
353     return true;
354   }
355
356   if (auto *CFP = dyn_cast<ConstantFP>(C)) {
357     if (CFP->getType()->isDoubleTy()) {
358       C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL);
359       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
360     }
361     if (CFP->getType()->isFloatTy()){
362       C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), DL);
363       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
364     }
365     if (CFP->getType()->isHalfTy()){
366       C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), DL);
367       return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
368     }
369     return false;
370   }
371
372   if (auto *CS = dyn_cast<ConstantStruct>(C)) {
373     const StructLayout *SL = DL.getStructLayout(CS->getType());
374     unsigned Index = SL->getElementContainingOffset(ByteOffset);
375     uint64_t CurEltOffset = SL->getElementOffset(Index);
376     ByteOffset -= CurEltOffset;
377
378     while (true) {
379       // If the element access is to the element itself and not to tail padding,
380       // read the bytes from the element.
381       uint64_t EltSize = DL.getTypeAllocSize(CS->getOperand(Index)->getType());
382
383       if (ByteOffset < EltSize &&
384           !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
385                               BytesLeft, DL))
386         return false;
387
388       ++Index;
389
390       // Check to see if we read from the last struct element, if so we're done.
391       if (Index == CS->getType()->getNumElements())
392         return true;
393
394       // If we read all of the bytes we needed from this element we're done.
395       uint64_t NextEltOffset = SL->getElementOffset(Index);
396
397       if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
398         return true;
399
400       // Move to the next element of the struct.
401       CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
402       BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
403       ByteOffset = 0;
404       CurEltOffset = NextEltOffset;
405     }
406     // not reached.
407   }
408
409   if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
410       isa<ConstantDataSequential>(C)) {
411     Type *EltTy = C->getType()->getSequentialElementType();
412     uint64_t EltSize = DL.getTypeAllocSize(EltTy);
413     uint64_t Index = ByteOffset / EltSize;
414     uint64_t Offset = ByteOffset - Index * EltSize;
415     uint64_t NumElts;
416     if (auto *AT = dyn_cast<ArrayType>(C->getType()))
417       NumElts = AT->getNumElements();
418     else
419       NumElts = C->getType()->getVectorNumElements();
420
421     for (; Index != NumElts; ++Index) {
422       if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
423                               BytesLeft, DL))
424         return false;
425
426       uint64_t BytesWritten = EltSize - Offset;
427       assert(BytesWritten <= EltSize && "Not indexing into this element?");
428       if (BytesWritten >= BytesLeft)
429         return true;
430
431       Offset = 0;
432       BytesLeft -= BytesWritten;
433       CurPtr += BytesWritten;
434     }
435     return true;
436   }
437
438   if (auto *CE = dyn_cast<ConstantExpr>(C)) {
439     if (CE->getOpcode() == Instruction::IntToPtr &&
440         CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) {
441       return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
442                                 BytesLeft, DL);
443     }
444   }
445
446   // Otherwise, unknown initializer type.
447   return false;
448 }
449
450 Constant *FoldReinterpretLoadFromConstPtr(Constant *C, Type *LoadTy,
451                                           const DataLayout &DL) {
452   auto *PTy = cast<PointerType>(C->getType());
453   auto *IntType = dyn_cast<IntegerType>(LoadTy);
454
455   // If this isn't an integer load we can't fold it directly.
456   if (!IntType) {
457     unsigned AS = PTy->getAddressSpace();
458
459     // If this is a float/double load, we can try folding it as an int32/64 load
460     // and then bitcast the result.  This can be useful for union cases.  Note
461     // that address spaces don't matter here since we're not going to result in
462     // an actual new load.
463     Type *MapTy;
464     if (LoadTy->isHalfTy())
465       MapTy = Type::getInt16Ty(C->getContext());
466     else if (LoadTy->isFloatTy())
467       MapTy = Type::getInt32Ty(C->getContext());
468     else if (LoadTy->isDoubleTy())
469       MapTy = Type::getInt64Ty(C->getContext());
470     else if (LoadTy->isVectorTy()) {
471       MapTy = PointerType::getIntNTy(C->getContext(),
472                                      DL.getTypeAllocSizeInBits(LoadTy));
473     } else
474       return nullptr;
475
476     C = FoldBitCast(C, MapTy->getPointerTo(AS), DL);
477     if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, MapTy, DL))
478       return FoldBitCast(Res, LoadTy, DL);
479     return nullptr;
480   }
481
482   unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
483   if (BytesLoaded > 32 || BytesLoaded == 0)
484     return nullptr;
485
486   GlobalValue *GVal;
487   APInt OffsetAI;
488   if (!IsConstantOffsetFromGlobal(C, GVal, OffsetAI, DL))
489     return nullptr;
490
491   auto *GV = dyn_cast<GlobalVariable>(GVal);
492   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
493       !GV->getInitializer()->getType()->isSized())
494     return nullptr;
495
496   int64_t Offset = OffsetAI.getSExtValue();
497   int64_t InitializerSize = DL.getTypeAllocSize(GV->getInitializer()->getType());
498
499   // If we're not accessing anything in this constant, the result is undefined.
500   if (Offset + BytesLoaded <= 0)
501     return UndefValue::get(IntType);
502
503   // If we're not accessing anything in this constant, the result is undefined.
504   if (Offset >= InitializerSize)
505     return UndefValue::get(IntType);
506
507   unsigned char RawBytes[32] = {0};
508   unsigned char *CurPtr = RawBytes;
509   unsigned BytesLeft = BytesLoaded;
510
511   // If we're loading off the beginning of the global, some bytes may be valid.
512   if (Offset < 0) {
513     CurPtr += -Offset;
514     BytesLeft += Offset;
515     Offset = 0;
516   }
517
518   if (!ReadDataFromGlobal(GV->getInitializer(), Offset, CurPtr, BytesLeft, DL))
519     return nullptr;
520
521   APInt ResultVal = APInt(IntType->getBitWidth(), 0);
522   if (DL.isLittleEndian()) {
523     ResultVal = RawBytes[BytesLoaded - 1];
524     for (unsigned i = 1; i != BytesLoaded; ++i) {
525       ResultVal <<= 8;
526       ResultVal |= RawBytes[BytesLoaded - 1 - i];
527     }
528   } else {
529     ResultVal = RawBytes[0];
530     for (unsigned i = 1; i != BytesLoaded; ++i) {
531       ResultVal <<= 8;
532       ResultVal |= RawBytes[i];
533     }
534   }
535
536   return ConstantInt::get(IntType->getContext(), ResultVal);
537 }
538
539 Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE, Type *DestTy,
540                                          const DataLayout &DL) {
541   auto *SrcPtr = CE->getOperand(0);
542   auto *SrcPtrTy = dyn_cast<PointerType>(SrcPtr->getType());
543   if (!SrcPtrTy)
544     return nullptr;
545   Type *SrcTy = SrcPtrTy->getPointerElementType();
546
547   Constant *C = ConstantFoldLoadFromConstPtr(SrcPtr, SrcTy, DL);
548   if (!C)
549     return nullptr;
550
551   do {
552     Type *SrcTy = C->getType();
553
554     // If the type sizes are the same and a cast is legal, just directly
555     // cast the constant.
556     if (DL.getTypeSizeInBits(DestTy) == DL.getTypeSizeInBits(SrcTy)) {
557       Instruction::CastOps Cast = Instruction::BitCast;
558       // If we are going from a pointer to int or vice versa, we spell the cast
559       // differently.
560       if (SrcTy->isIntegerTy() && DestTy->isPointerTy())
561         Cast = Instruction::IntToPtr;
562       else if (SrcTy->isPointerTy() && DestTy->isIntegerTy())
563         Cast = Instruction::PtrToInt;
564
565       if (CastInst::castIsValid(Cast, C, DestTy))
566         return ConstantExpr::getCast(Cast, C, DestTy);
567     }
568
569     // If this isn't an aggregate type, there is nothing we can do to drill down
570     // and find a bitcastable constant.
571     if (!SrcTy->isAggregateType())
572       return nullptr;
573
574     // We're simulating a load through a pointer that was bitcast to point to
575     // a different type, so we can try to walk down through the initial
576     // elements of an aggregate to see if some part of th e aggregate is
577     // castable to implement the "load" semantic model.
578     C = C->getAggregateElement(0u);
579   } while (C);
580
581   return nullptr;
582 }
583
584 } // end anonymous namespace
585
586 Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
587                                              const DataLayout &DL) {
588   // First, try the easy cases:
589   if (auto *GV = dyn_cast<GlobalVariable>(C))
590     if (GV->isConstant() && GV->hasDefinitiveInitializer())
591       return GV->getInitializer();
592
593   if (auto *GA = dyn_cast<GlobalAlias>(C))
594     if (GA->getAliasee() && !GA->isInterposable())
595       return ConstantFoldLoadFromConstPtr(GA->getAliasee(), Ty, DL);
596
597   // If the loaded value isn't a constant expr, we can't handle it.
598   auto *CE = dyn_cast<ConstantExpr>(C);
599   if (!CE)
600     return nullptr;
601
602   if (CE->getOpcode() == Instruction::GetElementPtr) {
603     if (auto *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
604       if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
605         if (Constant *V =
606              ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
607           return V;
608       }
609     }
610   }
611
612   if (CE->getOpcode() == Instruction::BitCast)
613     if (Constant *LoadedC = ConstantFoldLoadThroughBitcast(CE, Ty, DL))
614       return LoadedC;
615
616   // Instead of loading constant c string, use corresponding integer value
617   // directly if string length is small enough.
618   StringRef Str;
619   if (getConstantStringInfo(CE, Str) && !Str.empty()) {
620     size_t StrLen = Str.size();
621     unsigned NumBits = Ty->getPrimitiveSizeInBits();
622     // Replace load with immediate integer if the result is an integer or fp
623     // value.
624     if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
625         (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) {
626       APInt StrVal(NumBits, 0);
627       APInt SingleChar(NumBits, 0);
628       if (DL.isLittleEndian()) {
629         for (unsigned char C : reverse(Str.bytes())) {
630           SingleChar = static_cast<uint64_t>(C);
631           StrVal = (StrVal << 8) | SingleChar;
632         }
633       } else {
634         for (unsigned char C : Str.bytes()) {
635           SingleChar = static_cast<uint64_t>(C);
636           StrVal = (StrVal << 8) | SingleChar;
637         }
638         // Append NULL at the end.
639         SingleChar = 0;
640         StrVal = (StrVal << 8) | SingleChar;
641       }
642
643       Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
644       if (Ty->isFloatingPointTy())
645         Res = ConstantExpr::getBitCast(Res, Ty);
646       return Res;
647     }
648   }
649
650   // If this load comes from anywhere in a constant global, and if the global
651   // is all undef or zero, we know what it loads.
652   if (auto *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, DL))) {
653     if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
654       if (GV->getInitializer()->isNullValue())
655         return Constant::getNullValue(Ty);
656       if (isa<UndefValue>(GV->getInitializer()))
657         return UndefValue::get(Ty);
658     }
659   }
660
661   // Try hard to fold loads from bitcasted strange and non-type-safe things.
662   return FoldReinterpretLoadFromConstPtr(CE, Ty, DL);
663 }
664
665 namespace {
666
667 Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout &DL) {
668   if (LI->isVolatile()) return nullptr;
669
670   if (auto *C = dyn_cast<Constant>(LI->getOperand(0)))
671     return ConstantFoldLoadFromConstPtr(C, LI->getType(), DL);
672
673   return nullptr;
674 }
675
676 /// One of Op0/Op1 is a constant expression.
677 /// Attempt to symbolically evaluate the result of a binary operator merging
678 /// these together.  If target data info is available, it is provided as DL,
679 /// otherwise DL is null.
680 Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1,
681                                     const DataLayout &DL) {
682   // SROA
683
684   // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
685   // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
686   // bits.
687
688   if (Opc == Instruction::And) {
689     unsigned BitWidth = DL.getTypeSizeInBits(Op0->getType()->getScalarType());
690     APInt KnownZero0(BitWidth, 0), KnownOne0(BitWidth, 0);
691     APInt KnownZero1(BitWidth, 0), KnownOne1(BitWidth, 0);
692     computeKnownBits(Op0, KnownZero0, KnownOne0, DL);
693     computeKnownBits(Op1, KnownZero1, KnownOne1, DL);
694     if ((KnownOne1 | KnownZero0).isAllOnesValue()) {
695       // All the bits of Op0 that the 'and' could be masking are already zero.
696       return Op0;
697     }
698     if ((KnownOne0 | KnownZero1).isAllOnesValue()) {
699       // All the bits of Op1 that the 'and' could be masking are already zero.
700       return Op1;
701     }
702
703     APInt KnownZero = KnownZero0 | KnownZero1;
704     APInt KnownOne = KnownOne0 & KnownOne1;
705     if ((KnownZero | KnownOne).isAllOnesValue()) {
706       return ConstantInt::get(Op0->getType(), KnownOne);
707     }
708   }
709
710   // If the constant expr is something like &A[123] - &A[4].f, fold this into a
711   // constant.  This happens frequently when iterating over a global array.
712   if (Opc == Instruction::Sub) {
713     GlobalValue *GV1, *GV2;
714     APInt Offs1, Offs2;
715
716     if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, DL))
717       if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, DL) && GV1 == GV2) {
718         unsigned OpSize = DL.getTypeSizeInBits(Op0->getType());
719
720         // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
721         // PtrToInt may change the bitwidth so we have convert to the right size
722         // first.
723         return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
724                                                 Offs2.zextOrTrunc(OpSize));
725       }
726   }
727
728   return nullptr;
729 }
730
731 /// If array indices are not pointer-sized integers, explicitly cast them so
732 /// that they aren't implicitly casted by the getelementptr.
733 Constant *CastGEPIndices(Type *SrcElemTy, ArrayRef<Constant *> Ops,
734                          Type *ResultTy, Optional<unsigned> InRangeIndex,
735                          const DataLayout &DL, const TargetLibraryInfo *TLI) {
736   Type *IntPtrTy = DL.getIntPtrType(ResultTy);
737   Type *IntPtrScalarTy = IntPtrTy->getScalarType();
738
739   bool Any = false;
740   SmallVector<Constant*, 32> NewIdxs;
741   for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
742     if ((i == 1 ||
743          !isa<StructType>(GetElementPtrInst::getIndexedType(
744              SrcElemTy, Ops.slice(1, i - 1)))) &&
745         Ops[i]->getType()->getScalarType() != IntPtrScalarTy) {
746       Any = true;
747       Type *NewType = Ops[i]->getType()->isVectorTy()
748                           ? IntPtrTy
749                           : IntPtrTy->getScalarType();
750       NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i],
751                                                                       true,
752                                                                       NewType,
753                                                                       true),
754                                               Ops[i], NewType));
755     } else
756       NewIdxs.push_back(Ops[i]);
757   }
758
759   if (!Any)
760     return nullptr;
761
762   Constant *C = ConstantExpr::getGetElementPtr(
763       SrcElemTy, Ops[0], NewIdxs, /*InBounds=*/false, InRangeIndex);
764   if (Constant *Folded = ConstantFoldConstant(C, DL, TLI))
765     C = Folded;
766
767   return C;
768 }
769
770 /// Strip the pointer casts, but preserve the address space information.
771 Constant* StripPtrCastKeepAS(Constant* Ptr, Type *&ElemTy) {
772   assert(Ptr->getType()->isPointerTy() && "Not a pointer type");
773   auto *OldPtrTy = cast<PointerType>(Ptr->getType());
774   Ptr = Ptr->stripPointerCasts();
775   auto *NewPtrTy = cast<PointerType>(Ptr->getType());
776
777   ElemTy = NewPtrTy->getPointerElementType();
778
779   // Preserve the address space number of the pointer.
780   if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
781     NewPtrTy = ElemTy->getPointerTo(OldPtrTy->getAddressSpace());
782     Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy);
783   }
784   return Ptr;
785 }
786
787 /// If we can symbolically evaluate the GEP constant expression, do so.
788 Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
789                                   ArrayRef<Constant *> Ops,
790                                   const DataLayout &DL,
791                                   const TargetLibraryInfo *TLI) {
792   const GEPOperator *InnermostGEP = GEP;
793   bool InBounds = GEP->isInBounds();
794
795   Type *SrcElemTy = GEP->getSourceElementType();
796   Type *ResElemTy = GEP->getResultElementType();
797   Type *ResTy = GEP->getType();
798   if (!SrcElemTy->isSized())
799     return nullptr;
800
801   if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy,
802                                    GEP->getInRangeIndex(), DL, TLI))
803     return C;
804
805   Constant *Ptr = Ops[0];
806   if (!Ptr->getType()->isPointerTy())
807     return nullptr;
808
809   Type *IntPtrTy = DL.getIntPtrType(Ptr->getType());
810
811   // If this is a constant expr gep that is effectively computing an
812   // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
813   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
814     if (!isa<ConstantInt>(Ops[i])) {
815
816       // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
817       // "inttoptr (sub (ptrtoint Ptr), V)"
818       if (Ops.size() == 2 && ResElemTy->isIntegerTy(8)) {
819         auto *CE = dyn_cast<ConstantExpr>(Ops[1]);
820         assert((!CE || CE->getType() == IntPtrTy) &&
821                "CastGEPIndices didn't canonicalize index types!");
822         if (CE && CE->getOpcode() == Instruction::Sub &&
823             CE->getOperand(0)->isNullValue()) {
824           Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
825           Res = ConstantExpr::getSub(Res, CE->getOperand(1));
826           Res = ConstantExpr::getIntToPtr(Res, ResTy);
827           if (auto *FoldedRes = ConstantFoldConstant(Res, DL, TLI))
828             Res = FoldedRes;
829           return Res;
830         }
831       }
832       return nullptr;
833     }
834
835   unsigned BitWidth = DL.getTypeSizeInBits(IntPtrTy);
836   APInt Offset =
837       APInt(BitWidth,
838             DL.getIndexedOffsetInType(
839                 SrcElemTy,
840                 makeArrayRef((Value * const *)Ops.data() + 1, Ops.size() - 1)));
841   Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
842
843   // If this is a GEP of a GEP, fold it all into a single GEP.
844   while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
845     InnermostGEP = GEP;
846     InBounds &= GEP->isInBounds();
847
848     SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end());
849
850     // Do not try the incorporate the sub-GEP if some index is not a number.
851     bool AllConstantInt = true;
852     for (Value *NestedOp : NestedOps)
853       if (!isa<ConstantInt>(NestedOp)) {
854         AllConstantInt = false;
855         break;
856       }
857     if (!AllConstantInt)
858       break;
859
860     Ptr = cast<Constant>(GEP->getOperand(0));
861     SrcElemTy = GEP->getSourceElementType();
862     Offset += APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps));
863     Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
864   }
865
866   // If the base value for this address is a literal integer value, fold the
867   // getelementptr to the resulting integer value casted to the pointer type.
868   APInt BasePtr(BitWidth, 0);
869   if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) {
870     if (CE->getOpcode() == Instruction::IntToPtr) {
871       if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
872         BasePtr = Base->getValue().zextOrTrunc(BitWidth);
873     }
874   }
875
876   auto *PTy = cast<PointerType>(Ptr->getType());
877   if ((Ptr->isNullValue() || BasePtr != 0) &&
878       !DL.isNonIntegralPointerType(PTy)) {
879     Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr);
880     return ConstantExpr::getIntToPtr(C, ResTy);
881   }
882
883   // Otherwise form a regular getelementptr. Recompute the indices so that
884   // we eliminate over-indexing of the notional static type array bounds.
885   // This makes it easy to determine if the getelementptr is "inbounds".
886   // Also, this helps GlobalOpt do SROA on GlobalVariables.
887   Type *Ty = PTy;
888   SmallVector<Constant *, 32> NewIdxs;
889
890   do {
891     if (!Ty->isStructTy()) {
892       if (Ty->isPointerTy()) {
893         // The only pointer indexing we'll do is on the first index of the GEP.
894         if (!NewIdxs.empty())
895           break;
896
897         Ty = SrcElemTy;
898
899         // Only handle pointers to sized types, not pointers to functions.
900         if (!Ty->isSized())
901           return nullptr;
902       } else if (auto *ATy = dyn_cast<SequentialType>(Ty)) {
903         Ty = ATy->getElementType();
904       } else {
905         // We've reached some non-indexable type.
906         break;
907       }
908
909       // Determine which element of the array the offset points into.
910       APInt ElemSize(BitWidth, DL.getTypeAllocSize(Ty));
911       if (ElemSize == 0) {
912         // The element size is 0. This may be [0 x Ty]*, so just use a zero
913         // index for this level and proceed to the next level to see if it can
914         // accommodate the offset.
915         NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0));
916       } else {
917         // The element size is non-zero divide the offset by the element
918         // size (rounding down), to compute the index at this level.
919         bool Overflow;
920         APInt NewIdx = Offset.sdiv_ov(ElemSize, Overflow);
921         if (Overflow)
922           break;
923         Offset -= NewIdx * ElemSize;
924         NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
925       }
926     } else {
927       auto *STy = cast<StructType>(Ty);
928       // If we end up with an offset that isn't valid for this struct type, we
929       // can't re-form this GEP in a regular form, so bail out. The pointer
930       // operand likely went through casts that are necessary to make the GEP
931       // sensible.
932       const StructLayout &SL = *DL.getStructLayout(STy);
933       if (Offset.isNegative() || Offset.uge(SL.getSizeInBytes()))
934         break;
935
936       // Determine which field of the struct the offset points into. The
937       // getZExtValue is fine as we've already ensured that the offset is
938       // within the range representable by the StructLayout API.
939       unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
940       NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
941                                          ElIdx));
942       Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
943       Ty = STy->getTypeAtIndex(ElIdx);
944     }
945   } while (Ty != ResElemTy);
946
947   // If we haven't used up the entire offset by descending the static
948   // type, then the offset is pointing into the middle of an indivisible
949   // member, so we can't simplify it.
950   if (Offset != 0)
951     return nullptr;
952
953   // Preserve the inrange index from the innermost GEP if possible. We must
954   // have calculated the same indices up to and including the inrange index.
955   Optional<unsigned> InRangeIndex;
956   if (Optional<unsigned> LastIRIndex = InnermostGEP->getInRangeIndex())
957     if (SrcElemTy == InnermostGEP->getSourceElementType() &&
958         NewIdxs.size() > *LastIRIndex) {
959       InRangeIndex = LastIRIndex;
960       for (unsigned I = 0; I <= *LastIRIndex; ++I)
961         if (NewIdxs[I] != InnermostGEP->getOperand(I + 1)) {
962           InRangeIndex = None;
963           break;
964         }
965     }
966
967   // Create a GEP.
968   Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs,
969                                                InBounds, InRangeIndex);
970   assert(C->getType()->getPointerElementType() == Ty &&
971          "Computed GetElementPtr has unexpected type!");
972
973   // If we ended up indexing a member with a type that doesn't match
974   // the type of what the original indices indexed, add a cast.
975   if (Ty != ResElemTy)
976     C = FoldBitCast(C, ResTy, DL);
977
978   return C;
979 }
980
981 /// Attempt to constant fold an instruction with the
982 /// specified opcode and operands.  If successful, the constant result is
983 /// returned, if not, null is returned.  Note that this function can fail when
984 /// attempting to fold instructions like loads and stores, which have no
985 /// constant expression form.
986 ///
987 /// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/inrange
988 /// etc information, due to only being passed an opcode and operands. Constant
989 /// folding using this function strips this information.
990 ///
991 Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
992                                        ArrayRef<Constant *> Ops,
993                                        const DataLayout &DL,
994                                        const TargetLibraryInfo *TLI) {
995   Type *DestTy = InstOrCE->getType();
996
997   // Handle easy binops first.
998   if (Instruction::isBinaryOp(Opcode))
999     return ConstantFoldBinaryOpOperands(Opcode, Ops[0], Ops[1], DL);
1000
1001   if (Instruction::isCast(Opcode))
1002     return ConstantFoldCastOperand(Opcode, Ops[0], DestTy, DL);
1003
1004   if (auto *GEP = dyn_cast<GEPOperator>(InstOrCE)) {
1005     if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI))
1006       return C;
1007
1008     return ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), Ops[0],
1009                                           Ops.slice(1), GEP->isInBounds(),
1010                                           GEP->getInRangeIndex());
1011   }
1012
1013   if (auto *CE = dyn_cast<ConstantExpr>(InstOrCE))
1014     return CE->getWithOperands(Ops);
1015
1016   switch (Opcode) {
1017   default: return nullptr;
1018   case Instruction::ICmp:
1019   case Instruction::FCmp: llvm_unreachable("Invalid for compares");
1020   case Instruction::Call:
1021     if (auto *F = dyn_cast<Function>(Ops.back()))
1022       if (canConstantFoldCallTo(F))
1023         return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI);
1024     return nullptr;
1025   case Instruction::Select:
1026     return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
1027   case Instruction::ExtractElement:
1028     return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1029   case Instruction::InsertElement:
1030     return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1031   case Instruction::ShuffleVector:
1032     return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
1033   }
1034 }
1035
1036 } // end anonymous namespace
1037
1038 //===----------------------------------------------------------------------===//
1039 // Constant Folding public APIs
1040 //===----------------------------------------------------------------------===//
1041
1042 namespace {
1043
1044 Constant *
1045 ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL,
1046                          const TargetLibraryInfo *TLI,
1047                          SmallDenseMap<Constant *, Constant *> &FoldedOps) {
1048   if (!isa<ConstantVector>(C) && !isa<ConstantExpr>(C))
1049     return nullptr;
1050
1051   SmallVector<Constant *, 8> Ops;
1052   for (const Use &NewU : C->operands()) {
1053     auto *NewC = cast<Constant>(&NewU);
1054     // Recursively fold the ConstantExpr's operands. If we have already folded
1055     // a ConstantExpr, we don't have to process it again.
1056     if (isa<ConstantVector>(NewC) || isa<ConstantExpr>(NewC)) {
1057       auto It = FoldedOps.find(NewC);
1058       if (It == FoldedOps.end()) {
1059         if (auto *FoldedC =
1060                 ConstantFoldConstantImpl(NewC, DL, TLI, FoldedOps)) {
1061           NewC = FoldedC;
1062           FoldedOps.insert({NewC, FoldedC});
1063         } else {
1064           FoldedOps.insert({NewC, NewC});
1065         }
1066       } else {
1067         NewC = It->second;
1068       }
1069     }
1070     Ops.push_back(NewC);
1071   }
1072
1073   if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1074     if (CE->isCompare())
1075       return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
1076                                              DL, TLI);
1077
1078     return ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI);
1079   }
1080
1081   assert(isa<ConstantVector>(C));
1082   return ConstantVector::get(Ops);
1083 }
1084
1085 } // end anonymous namespace
1086
1087 Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
1088                                         const TargetLibraryInfo *TLI) {
1089   // Handle PHI nodes quickly here...
1090   if (auto *PN = dyn_cast<PHINode>(I)) {
1091     Constant *CommonValue = nullptr;
1092
1093     SmallDenseMap<Constant *, Constant *> FoldedOps;
1094     for (Value *Incoming : PN->incoming_values()) {
1095       // If the incoming value is undef then skip it.  Note that while we could
1096       // skip the value if it is equal to the phi node itself we choose not to
1097       // because that would break the rule that constant folding only applies if
1098       // all operands are constants.
1099       if (isa<UndefValue>(Incoming))
1100         continue;
1101       // If the incoming value is not a constant, then give up.
1102       auto *C = dyn_cast<Constant>(Incoming);
1103       if (!C)
1104         return nullptr;
1105       // Fold the PHI's operands.
1106       if (auto *FoldedC = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps))
1107         C = FoldedC;
1108       // If the incoming value is a different constant to
1109       // the one we saw previously, then give up.
1110       if (CommonValue && C != CommonValue)
1111         return nullptr;
1112       CommonValue = C;
1113     }
1114
1115     // If we reach here, all incoming values are the same constant or undef.
1116     return CommonValue ? CommonValue : UndefValue::get(PN->getType());
1117   }
1118
1119   // Scan the operand list, checking to see if they are all constants, if so,
1120   // hand off to ConstantFoldInstOperandsImpl.
1121   if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); }))
1122     return nullptr;
1123
1124   SmallDenseMap<Constant *, Constant *> FoldedOps;
1125   SmallVector<Constant *, 8> Ops;
1126   for (const Use &OpU : I->operands()) {
1127     auto *Op = cast<Constant>(&OpU);
1128     // Fold the Instruction's operands.
1129     if (auto *FoldedOp = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps))
1130       Op = FoldedOp;
1131
1132     Ops.push_back(Op);
1133   }
1134
1135   if (const auto *CI = dyn_cast<CmpInst>(I))
1136     return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
1137                                            DL, TLI);
1138
1139   if (const auto *LI = dyn_cast<LoadInst>(I))
1140     return ConstantFoldLoadInst(LI, DL);
1141
1142   if (auto *IVI = dyn_cast<InsertValueInst>(I)) {
1143     return ConstantExpr::getInsertValue(
1144                                 cast<Constant>(IVI->getAggregateOperand()),
1145                                 cast<Constant>(IVI->getInsertedValueOperand()),
1146                                 IVI->getIndices());
1147   }
1148
1149   if (auto *EVI = dyn_cast<ExtractValueInst>(I)) {
1150     return ConstantExpr::getExtractValue(
1151                                     cast<Constant>(EVI->getAggregateOperand()),
1152                                     EVI->getIndices());
1153   }
1154
1155   return ConstantFoldInstOperands(I, Ops, DL, TLI);
1156 }
1157
1158 Constant *llvm::ConstantFoldConstant(const Constant *C, const DataLayout &DL,
1159                                      const TargetLibraryInfo *TLI) {
1160   SmallDenseMap<Constant *, Constant *> FoldedOps;
1161   return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1162 }
1163
1164 Constant *llvm::ConstantFoldInstOperands(Instruction *I,
1165                                          ArrayRef<Constant *> Ops,
1166                                          const DataLayout &DL,
1167                                          const TargetLibraryInfo *TLI) {
1168   return ConstantFoldInstOperandsImpl(I, I->getOpcode(), Ops, DL, TLI);
1169 }
1170
1171 Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
1172                                                 Constant *Ops0, Constant *Ops1,
1173                                                 const DataLayout &DL,
1174                                                 const TargetLibraryInfo *TLI) {
1175   // fold: icmp (inttoptr x), null         -> icmp x, 0
1176   // fold: icmp (ptrtoint x), 0            -> icmp x, null
1177   // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
1178   // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
1179   //
1180   // FIXME: The following comment is out of data and the DataLayout is here now.
1181   // ConstantExpr::getCompare cannot do this, because it doesn't have DL
1182   // around to know if bit truncation is happening.
1183   if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
1184     if (Ops1->isNullValue()) {
1185       if (CE0->getOpcode() == Instruction::IntToPtr) {
1186         Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1187         // Convert the integer value to the right size to ensure we get the
1188         // proper extension or truncation.
1189         Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1190                                                    IntPtrTy, false);
1191         Constant *Null = Constant::getNullValue(C->getType());
1192         return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1193       }
1194
1195       // Only do this transformation if the int is intptrty in size, otherwise
1196       // there is a truncation or extension that we aren't modeling.
1197       if (CE0->getOpcode() == Instruction::PtrToInt) {
1198         Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1199         if (CE0->getType() == IntPtrTy) {
1200           Constant *C = CE0->getOperand(0);
1201           Constant *Null = Constant::getNullValue(C->getType());
1202           return ConstantFoldCompareInstOperands(Predicate, C, Null, DL, TLI);
1203         }
1204       }
1205     }
1206
1207     if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
1208       if (CE0->getOpcode() == CE1->getOpcode()) {
1209         if (CE0->getOpcode() == Instruction::IntToPtr) {
1210           Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1211
1212           // Convert the integer value to the right size to ensure we get the
1213           // proper extension or truncation.
1214           Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1215                                                       IntPtrTy, false);
1216           Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
1217                                                       IntPtrTy, false);
1218           return ConstantFoldCompareInstOperands(Predicate, C0, C1, DL, TLI);
1219         }
1220
1221         // Only do this transformation if the int is intptrty in size, otherwise
1222         // there is a truncation or extension that we aren't modeling.
1223         if (CE0->getOpcode() == Instruction::PtrToInt) {
1224           Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1225           if (CE0->getType() == IntPtrTy &&
1226               CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1227             return ConstantFoldCompareInstOperands(
1228                 Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI);
1229           }
1230         }
1231       }
1232     }
1233
1234     // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
1235     // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
1236     if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
1237         CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
1238       Constant *LHS = ConstantFoldCompareInstOperands(
1239           Predicate, CE0->getOperand(0), Ops1, DL, TLI);
1240       Constant *RHS = ConstantFoldCompareInstOperands(
1241           Predicate, CE0->getOperand(1), Ops1, DL, TLI);
1242       unsigned OpC =
1243         Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
1244       return ConstantFoldBinaryOpOperands(OpC, LHS, RHS, DL);
1245     }
1246   }
1247
1248   return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
1249 }
1250
1251 Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
1252                                              Constant *RHS,
1253                                              const DataLayout &DL) {
1254   assert(Instruction::isBinaryOp(Opcode));
1255   if (isa<ConstantExpr>(LHS) || isa<ConstantExpr>(RHS))
1256     if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL))
1257       return C;
1258
1259   return ConstantExpr::get(Opcode, LHS, RHS);
1260 }
1261
1262 Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
1263                                         Type *DestTy, const DataLayout &DL) {
1264   assert(Instruction::isCast(Opcode));
1265   switch (Opcode) {
1266   default:
1267     llvm_unreachable("Missing case");
1268   case Instruction::PtrToInt:
1269     // If the input is a inttoptr, eliminate the pair.  This requires knowing
1270     // the width of a pointer, so it can't be done in ConstantExpr::getCast.
1271     if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1272       if (CE->getOpcode() == Instruction::IntToPtr) {
1273         Constant *Input = CE->getOperand(0);
1274         unsigned InWidth = Input->getType()->getScalarSizeInBits();
1275         unsigned PtrWidth = DL.getPointerTypeSizeInBits(CE->getType());
1276         if (PtrWidth < InWidth) {
1277           Constant *Mask =
1278             ConstantInt::get(CE->getContext(),
1279                              APInt::getLowBitsSet(InWidth, PtrWidth));
1280           Input = ConstantExpr::getAnd(Input, Mask);
1281         }
1282         // Do a zext or trunc to get to the dest size.
1283         return ConstantExpr::getIntegerCast(Input, DestTy, false);
1284       }
1285     }
1286     return ConstantExpr::getCast(Opcode, C, DestTy);
1287   case Instruction::IntToPtr:
1288     // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
1289     // the int size is >= the ptr size and the address spaces are the same.
1290     // This requires knowing the width of a pointer, so it can't be done in
1291     // ConstantExpr::getCast.
1292     if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1293       if (CE->getOpcode() == Instruction::PtrToInt) {
1294         Constant *SrcPtr = CE->getOperand(0);
1295         unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType());
1296         unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1297
1298         if (MidIntSize >= SrcPtrSize) {
1299           unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
1300           if (SrcAS == DestTy->getPointerAddressSpace())
1301             return FoldBitCast(CE->getOperand(0), DestTy, DL);
1302         }
1303       }
1304     }
1305
1306     return ConstantExpr::getCast(Opcode, C, DestTy);
1307   case Instruction::Trunc:
1308   case Instruction::ZExt:
1309   case Instruction::SExt:
1310   case Instruction::FPTrunc:
1311   case Instruction::FPExt:
1312   case Instruction::UIToFP:
1313   case Instruction::SIToFP:
1314   case Instruction::FPToUI:
1315   case Instruction::FPToSI:
1316   case Instruction::AddrSpaceCast:
1317       return ConstantExpr::getCast(Opcode, C, DestTy);
1318   case Instruction::BitCast:
1319     return FoldBitCast(C, DestTy, DL);
1320   }
1321 }
1322
1323 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
1324                                                        ConstantExpr *CE) {
1325   if (!CE->getOperand(1)->isNullValue())
1326     return nullptr;  // Do not allow stepping over the value!
1327
1328   // Loop over all of the operands, tracking down which value we are
1329   // addressing.
1330   for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) {
1331     C = C->getAggregateElement(CE->getOperand(i));
1332     if (!C)
1333       return nullptr;
1334   }
1335   return C;
1336 }
1337
1338 Constant *
1339 llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
1340                                         ArrayRef<Constant *> Indices) {
1341   // Loop over all of the operands, tracking down which value we are
1342   // addressing.
1343   for (Constant *Index : Indices) {
1344     C = C->getAggregateElement(Index);
1345     if (!C)
1346       return nullptr;
1347   }
1348   return C;
1349 }
1350
1351 //===----------------------------------------------------------------------===//
1352 //  Constant Folding for Calls
1353 //
1354
1355 bool llvm::canConstantFoldCallTo(const Function *F) {
1356   switch (F->getIntrinsicID()) {
1357   case Intrinsic::fabs:
1358   case Intrinsic::minnum:
1359   case Intrinsic::maxnum:
1360   case Intrinsic::log:
1361   case Intrinsic::log2:
1362   case Intrinsic::log10:
1363   case Intrinsic::exp:
1364   case Intrinsic::exp2:
1365   case Intrinsic::floor:
1366   case Intrinsic::ceil:
1367   case Intrinsic::sqrt:
1368   case Intrinsic::sin:
1369   case Intrinsic::cos:
1370   case Intrinsic::trunc:
1371   case Intrinsic::rint:
1372   case Intrinsic::nearbyint:
1373   case Intrinsic::pow:
1374   case Intrinsic::powi:
1375   case Intrinsic::bswap:
1376   case Intrinsic::ctpop:
1377   case Intrinsic::ctlz:
1378   case Intrinsic::cttz:
1379   case Intrinsic::fma:
1380   case Intrinsic::fmuladd:
1381   case Intrinsic::copysign:
1382   case Intrinsic::round:
1383   case Intrinsic::masked_load:
1384   case Intrinsic::sadd_with_overflow:
1385   case Intrinsic::uadd_with_overflow:
1386   case Intrinsic::ssub_with_overflow:
1387   case Intrinsic::usub_with_overflow:
1388   case Intrinsic::smul_with_overflow:
1389   case Intrinsic::umul_with_overflow:
1390   case Intrinsic::convert_from_fp16:
1391   case Intrinsic::convert_to_fp16:
1392   case Intrinsic::bitreverse:
1393   case Intrinsic::x86_sse_cvtss2si:
1394   case Intrinsic::x86_sse_cvtss2si64:
1395   case Intrinsic::x86_sse_cvttss2si:
1396   case Intrinsic::x86_sse_cvttss2si64:
1397   case Intrinsic::x86_sse2_cvtsd2si:
1398   case Intrinsic::x86_sse2_cvtsd2si64:
1399   case Intrinsic::x86_sse2_cvttsd2si:
1400   case Intrinsic::x86_sse2_cvttsd2si64:
1401     return true;
1402   default:
1403     return false;
1404   case 0: break;
1405   }
1406
1407   if (!F->hasName())
1408     return false;
1409   StringRef Name = F->getName();
1410
1411   // In these cases, the check of the length is required.  We don't want to
1412   // return true for a name like "cos\0blah" which strcmp would return equal to
1413   // "cos", but has length 8.
1414   switch (Name[0]) {
1415   default:
1416     return false;
1417   case 'a':
1418     return Name == "acos" || Name == "asin" || Name == "atan" ||
1419            Name == "atan2" || Name == "acosf" || Name == "asinf" ||
1420            Name == "atanf" || Name == "atan2f";
1421   case 'c':
1422     return Name == "ceil" || Name == "cos" || Name == "cosh" ||
1423            Name == "ceilf" || Name == "cosf" || Name == "coshf";
1424   case 'e':
1425     return Name == "exp" || Name == "exp2" || Name == "expf" || Name == "exp2f";
1426   case 'f':
1427     return Name == "fabs" || Name == "floor" || Name == "fmod" ||
1428            Name == "fabsf" || Name == "floorf" || Name == "fmodf";
1429   case 'l':
1430     return Name == "log" || Name == "log10" || Name == "logf" ||
1431            Name == "log10f";
1432   case 'p':
1433     return Name == "pow" || Name == "powf";
1434   case 'r':
1435     return Name == "round" || Name == "roundf";
1436   case 's':
1437     return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
1438            Name == "sinf" || Name == "sinhf" || Name == "sqrtf";
1439   case 't':
1440     return Name == "tan" || Name == "tanh" || Name == "tanf" || Name == "tanhf";
1441   }
1442 }
1443
1444 namespace {
1445
1446 Constant *GetConstantFoldFPValue(double V, Type *Ty) {
1447   if (Ty->isHalfTy()) {
1448     APFloat APF(V);
1449     bool unused;
1450     APF.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &unused);
1451     return ConstantFP::get(Ty->getContext(), APF);
1452   }
1453   if (Ty->isFloatTy())
1454     return ConstantFP::get(Ty->getContext(), APFloat((float)V));
1455   if (Ty->isDoubleTy())
1456     return ConstantFP::get(Ty->getContext(), APFloat(V));
1457   llvm_unreachable("Can only constant fold half/float/double");
1458 }
1459
1460 /// Clear the floating-point exception state.
1461 inline void llvm_fenv_clearexcept() {
1462 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT
1463   feclearexcept(FE_ALL_EXCEPT);
1464 #endif
1465   errno = 0;
1466 }
1467
1468 /// Test if a floating-point exception was raised.
1469 inline bool llvm_fenv_testexcept() {
1470   int errno_val = errno;
1471   if (errno_val == ERANGE || errno_val == EDOM)
1472     return true;
1473 #if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT
1474   if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
1475     return true;
1476 #endif
1477   return false;
1478 }
1479
1480 Constant *ConstantFoldFP(double (*NativeFP)(double), double V, Type *Ty) {
1481   llvm_fenv_clearexcept();
1482   V = NativeFP(V);
1483   if (llvm_fenv_testexcept()) {
1484     llvm_fenv_clearexcept();
1485     return nullptr;
1486   }
1487
1488   return GetConstantFoldFPValue(V, Ty);
1489 }
1490
1491 Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), double V,
1492                                double W, Type *Ty) {
1493   llvm_fenv_clearexcept();
1494   V = NativeFP(V, W);
1495   if (llvm_fenv_testexcept()) {
1496     llvm_fenv_clearexcept();
1497     return nullptr;
1498   }
1499
1500   return GetConstantFoldFPValue(V, Ty);
1501 }
1502
1503 /// Attempt to fold an SSE floating point to integer conversion of a constant
1504 /// floating point. If roundTowardZero is false, the default IEEE rounding is
1505 /// used (toward nearest, ties to even). This matches the behavior of the
1506 /// non-truncating SSE instructions in the default rounding mode. The desired
1507 /// integer type Ty is used to select how many bits are available for the
1508 /// result. Returns null if the conversion cannot be performed, otherwise
1509 /// returns the Constant value resulting from the conversion.
1510 Constant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero,
1511                                       Type *Ty) {
1512   // All of these conversion intrinsics form an integer of at most 64bits.
1513   unsigned ResultWidth = Ty->getIntegerBitWidth();
1514   assert(ResultWidth <= 64 &&
1515          "Can only constant fold conversions to 64 and 32 bit ints");
1516
1517   uint64_t UIntVal;
1518   bool isExact = false;
1519   APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
1520                                               : APFloat::rmNearestTiesToEven;
1521   APFloat::opStatus status = Val.convertToInteger(&UIntVal, ResultWidth,
1522                                                   /*isSigned=*/true, mode,
1523                                                   &isExact);
1524   if (status != APFloat::opOK &&
1525       (!roundTowardZero || status != APFloat::opInexact))
1526     return nullptr;
1527   return ConstantInt::get(Ty, UIntVal, /*isSigned=*/true);
1528 }
1529
1530 double getValueAsDouble(ConstantFP *Op) {
1531   Type *Ty = Op->getType();
1532
1533   if (Ty->isFloatTy())
1534     return Op->getValueAPF().convertToFloat();
1535
1536   if (Ty->isDoubleTy())
1537     return Op->getValueAPF().convertToDouble();
1538
1539   bool unused;
1540   APFloat APF = Op->getValueAPF();
1541   APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &unused);
1542   return APF.convertToDouble();
1543 }
1544
1545 Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, Type *Ty,
1546                                  ArrayRef<Constant *> Operands,
1547                                  const TargetLibraryInfo *TLI) {
1548   if (Operands.size() == 1) {
1549     if (isa<UndefValue>(Operands[0])) {
1550       // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN
1551       if (IntrinsicID == Intrinsic::cos)
1552         return Constant::getNullValue(Ty);
1553     }
1554     if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) {
1555       if (IntrinsicID == Intrinsic::convert_to_fp16) {
1556         APFloat Val(Op->getValueAPF());
1557
1558         bool lost = false;
1559         Val.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &lost);
1560
1561         return ConstantInt::get(Ty->getContext(), Val.bitcastToAPInt());
1562       }
1563
1564       if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1565         return nullptr;
1566
1567       if (IntrinsicID == Intrinsic::round) {
1568         APFloat V = Op->getValueAPF();
1569         V.roundToIntegral(APFloat::rmNearestTiesToAway);
1570         return ConstantFP::get(Ty->getContext(), V);
1571       }
1572
1573       if (IntrinsicID == Intrinsic::floor) {
1574         APFloat V = Op->getValueAPF();
1575         V.roundToIntegral(APFloat::rmTowardNegative);
1576         return ConstantFP::get(Ty->getContext(), V);
1577       }
1578
1579       if (IntrinsicID == Intrinsic::ceil) {
1580         APFloat V = Op->getValueAPF();
1581         V.roundToIntegral(APFloat::rmTowardPositive);
1582         return ConstantFP::get(Ty->getContext(), V);
1583       }
1584
1585       if (IntrinsicID == Intrinsic::trunc) {
1586         APFloat V = Op->getValueAPF();
1587         V.roundToIntegral(APFloat::rmTowardZero);
1588         return ConstantFP::get(Ty->getContext(), V);
1589       }
1590
1591       if (IntrinsicID == Intrinsic::rint) {
1592         APFloat V = Op->getValueAPF();
1593         V.roundToIntegral(APFloat::rmNearestTiesToEven);
1594         return ConstantFP::get(Ty->getContext(), V);
1595       }
1596
1597       if (IntrinsicID == Intrinsic::nearbyint) {
1598         APFloat V = Op->getValueAPF();
1599         V.roundToIntegral(APFloat::rmNearestTiesToEven);
1600         return ConstantFP::get(Ty->getContext(), V);
1601       }
1602
1603       /// We only fold functions with finite arguments. Folding NaN and inf is
1604       /// likely to be aborted with an exception anyway, and some host libms
1605       /// have known errors raising exceptions.
1606       if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity())
1607         return nullptr;
1608
1609       /// Currently APFloat versions of these functions do not exist, so we use
1610       /// the host native double versions.  Float versions are not called
1611       /// directly but for all these it is true (float)(f((double)arg)) ==
1612       /// f(arg).  Long double not supported yet.
1613       double V = getValueAsDouble(Op);
1614
1615       switch (IntrinsicID) {
1616         default: break;
1617         case Intrinsic::fabs:
1618           return ConstantFoldFP(fabs, V, Ty);
1619         case Intrinsic::log2:
1620           return ConstantFoldFP(Log2, V, Ty);
1621         case Intrinsic::log:
1622           return ConstantFoldFP(log, V, Ty);
1623         case Intrinsic::log10:
1624           return ConstantFoldFP(log10, V, Ty);
1625         case Intrinsic::exp:
1626           return ConstantFoldFP(exp, V, Ty);
1627         case Intrinsic::exp2:
1628           return ConstantFoldFP(exp2, V, Ty);
1629         case Intrinsic::sin:
1630           return ConstantFoldFP(sin, V, Ty);
1631         case Intrinsic::cos:
1632           return ConstantFoldFP(cos, V, Ty);
1633       }
1634
1635       if (!TLI)
1636         return nullptr;
1637
1638       switch (Name[0]) {
1639       case 'a':
1640         if ((Name == "acos" && TLI->has(LibFunc::acos)) ||
1641             (Name == "acosf" && TLI->has(LibFunc::acosf)))
1642           return ConstantFoldFP(acos, V, Ty);
1643         else if ((Name == "asin" && TLI->has(LibFunc::asin)) ||
1644                  (Name == "asinf" && TLI->has(LibFunc::asinf)))
1645           return ConstantFoldFP(asin, V, Ty);
1646         else if ((Name == "atan" && TLI->has(LibFunc::atan)) ||
1647                  (Name == "atanf" && TLI->has(LibFunc::atanf)))
1648           return ConstantFoldFP(atan, V, Ty);
1649         break;
1650       case 'c':
1651         if ((Name == "ceil" && TLI->has(LibFunc::ceil)) ||
1652             (Name == "ceilf" && TLI->has(LibFunc::ceilf)))
1653           return ConstantFoldFP(ceil, V, Ty);
1654         else if ((Name == "cos" && TLI->has(LibFunc::cos)) ||
1655                  (Name == "cosf" && TLI->has(LibFunc::cosf)))
1656           return ConstantFoldFP(cos, V, Ty);
1657         else if ((Name == "cosh" && TLI->has(LibFunc::cosh)) ||
1658                  (Name == "coshf" && TLI->has(LibFunc::coshf)))
1659           return ConstantFoldFP(cosh, V, Ty);
1660         break;
1661       case 'e':
1662         if ((Name == "exp" && TLI->has(LibFunc::exp)) ||
1663             (Name == "expf" && TLI->has(LibFunc::expf)))
1664           return ConstantFoldFP(exp, V, Ty);
1665         if ((Name == "exp2" && TLI->has(LibFunc::exp2)) ||
1666             (Name == "exp2f" && TLI->has(LibFunc::exp2f)))
1667           // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
1668           // C99 library.
1669           return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
1670         break;
1671       case 'f':
1672         if ((Name == "fabs" && TLI->has(LibFunc::fabs)) ||
1673             (Name == "fabsf" && TLI->has(LibFunc::fabsf)))
1674           return ConstantFoldFP(fabs, V, Ty);
1675         else if ((Name == "floor" && TLI->has(LibFunc::floor)) ||
1676                  (Name == "floorf" && TLI->has(LibFunc::floorf)))
1677           return ConstantFoldFP(floor, V, Ty);
1678         break;
1679       case 'l':
1680         if ((Name == "log" && V > 0 && TLI->has(LibFunc::log)) ||
1681             (Name == "logf" && V > 0 && TLI->has(LibFunc::logf)))
1682           return ConstantFoldFP(log, V, Ty);
1683         else if ((Name == "log10" && V > 0 && TLI->has(LibFunc::log10)) ||
1684                  (Name == "log10f" && V > 0 && TLI->has(LibFunc::log10f)))
1685           return ConstantFoldFP(log10, V, Ty);
1686         else if (IntrinsicID == Intrinsic::sqrt &&
1687                  (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) {
1688           if (V >= -0.0)
1689             return ConstantFoldFP(sqrt, V, Ty);
1690           else {
1691             // Unlike the sqrt definitions in C/C++, POSIX, and IEEE-754 - which
1692             // all guarantee or favor returning NaN - the square root of a
1693             // negative number is not defined for the LLVM sqrt intrinsic.
1694             // This is because the intrinsic should only be emitted in place of
1695             // libm's sqrt function when using "no-nans-fp-math".
1696             return UndefValue::get(Ty);
1697           }
1698         }
1699         break;
1700       case 'r':
1701         if ((Name == "round" && TLI->has(LibFunc::round)) ||
1702             (Name == "roundf" && TLI->has(LibFunc::roundf)))
1703           return ConstantFoldFP(round, V, Ty);
1704       case 's':
1705         if ((Name == "sin" && TLI->has(LibFunc::sin)) ||
1706             (Name == "sinf" && TLI->has(LibFunc::sinf)))
1707           return ConstantFoldFP(sin, V, Ty);
1708         else if ((Name == "sinh" && TLI->has(LibFunc::sinh)) ||
1709                  (Name == "sinhf" && TLI->has(LibFunc::sinhf)))
1710           return ConstantFoldFP(sinh, V, Ty);
1711         else if ((Name == "sqrt" && V >= 0 && TLI->has(LibFunc::sqrt)) ||
1712                  (Name == "sqrtf" && V >= 0 && TLI->has(LibFunc::sqrtf)))
1713           return ConstantFoldFP(sqrt, V, Ty);
1714         break;
1715       case 't':
1716         if ((Name == "tan" && TLI->has(LibFunc::tan)) ||
1717             (Name == "tanf" && TLI->has(LibFunc::tanf)))
1718           return ConstantFoldFP(tan, V, Ty);
1719         else if ((Name == "tanh" && TLI->has(LibFunc::tanh)) ||
1720                  (Name == "tanhf" && TLI->has(LibFunc::tanhf)))
1721           return ConstantFoldFP(tanh, V, Ty);
1722         break;
1723       default:
1724         break;
1725       }
1726       return nullptr;
1727     }
1728
1729     if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
1730       switch (IntrinsicID) {
1731       case Intrinsic::bswap:
1732         return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
1733       case Intrinsic::ctpop:
1734         return ConstantInt::get(Ty, Op->getValue().countPopulation());
1735       case Intrinsic::bitreverse:
1736         return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits());
1737       case Intrinsic::convert_from_fp16: {
1738         APFloat Val(APFloat::IEEEhalf(), Op->getValue());
1739
1740         bool lost = false;
1741         APFloat::opStatus status = Val.convert(
1742             Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &lost);
1743
1744         // Conversion is always precise.
1745         (void)status;
1746         assert(status == APFloat::opOK && !lost &&
1747                "Precision lost during fp16 constfolding");
1748
1749         return ConstantFP::get(Ty->getContext(), Val);
1750       }
1751       default:
1752         return nullptr;
1753       }
1754     }
1755
1756     // Support ConstantVector in case we have an Undef in the top.
1757     if (isa<ConstantVector>(Operands[0]) ||
1758         isa<ConstantDataVector>(Operands[0])) {
1759       auto *Op = cast<Constant>(Operands[0]);
1760       switch (IntrinsicID) {
1761       default: break;
1762       case Intrinsic::x86_sse_cvtss2si:
1763       case Intrinsic::x86_sse_cvtss2si64:
1764       case Intrinsic::x86_sse2_cvtsd2si:
1765       case Intrinsic::x86_sse2_cvtsd2si64:
1766         if (ConstantFP *FPOp =
1767                 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
1768           return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
1769                                              /*roundTowardZero=*/false, Ty);
1770       case Intrinsic::x86_sse_cvttss2si:
1771       case Intrinsic::x86_sse_cvttss2si64:
1772       case Intrinsic::x86_sse2_cvttsd2si:
1773       case Intrinsic::x86_sse2_cvttsd2si64:
1774         if (ConstantFP *FPOp =
1775                 dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
1776           return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
1777                                              /*roundTowardZero=*/true, Ty);
1778       }
1779     }
1780
1781     if (isa<UndefValue>(Operands[0])) {
1782       if (IntrinsicID == Intrinsic::bswap)
1783         return Operands[0];
1784       return nullptr;
1785     }
1786
1787     return nullptr;
1788   }
1789
1790   if (Operands.size() == 2) {
1791     if (auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
1792       if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1793         return nullptr;
1794       double Op1V = getValueAsDouble(Op1);
1795
1796       if (auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
1797         if (Op2->getType() != Op1->getType())
1798           return nullptr;
1799
1800         double Op2V = getValueAsDouble(Op2);
1801         if (IntrinsicID == Intrinsic::pow) {
1802           return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
1803         }
1804         if (IntrinsicID == Intrinsic::copysign) {
1805           APFloat V1 = Op1->getValueAPF();
1806           const APFloat &V2 = Op2->getValueAPF();
1807           V1.copySign(V2);
1808           return ConstantFP::get(Ty->getContext(), V1);
1809         }
1810
1811         if (IntrinsicID == Intrinsic::minnum) {
1812           const APFloat &C1 = Op1->getValueAPF();
1813           const APFloat &C2 = Op2->getValueAPF();
1814           return ConstantFP::get(Ty->getContext(), minnum(C1, C2));
1815         }
1816
1817         if (IntrinsicID == Intrinsic::maxnum) {
1818           const APFloat &C1 = Op1->getValueAPF();
1819           const APFloat &C2 = Op2->getValueAPF();
1820           return ConstantFP::get(Ty->getContext(), maxnum(C1, C2));
1821         }
1822
1823         if (!TLI)
1824           return nullptr;
1825         if ((Name == "pow" && TLI->has(LibFunc::pow)) ||
1826             (Name == "powf" && TLI->has(LibFunc::powf)))
1827           return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
1828         if ((Name == "fmod" && TLI->has(LibFunc::fmod)) ||
1829             (Name == "fmodf" && TLI->has(LibFunc::fmodf)))
1830           return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
1831         if ((Name == "atan2" && TLI->has(LibFunc::atan2)) ||
1832             (Name == "atan2f" && TLI->has(LibFunc::atan2f)))
1833           return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
1834       } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
1835         if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
1836           return ConstantFP::get(Ty->getContext(),
1837                                  APFloat((float)std::pow((float)Op1V,
1838                                                  (int)Op2C->getZExtValue())));
1839         if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy())
1840           return ConstantFP::get(Ty->getContext(),
1841                                  APFloat((float)std::pow((float)Op1V,
1842                                                  (int)Op2C->getZExtValue())));
1843         if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy())
1844           return ConstantFP::get(Ty->getContext(),
1845                                  APFloat((double)std::pow((double)Op1V,
1846                                                    (int)Op2C->getZExtValue())));
1847       }
1848       return nullptr;
1849     }
1850
1851     if (auto *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
1852       if (auto *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
1853         switch (IntrinsicID) {
1854         default: break;
1855         case Intrinsic::sadd_with_overflow:
1856         case Intrinsic::uadd_with_overflow:
1857         case Intrinsic::ssub_with_overflow:
1858         case Intrinsic::usub_with_overflow:
1859         case Intrinsic::smul_with_overflow:
1860         case Intrinsic::umul_with_overflow: {
1861           APInt Res;
1862           bool Overflow;
1863           switch (IntrinsicID) {
1864           default: llvm_unreachable("Invalid case");
1865           case Intrinsic::sadd_with_overflow:
1866             Res = Op1->getValue().sadd_ov(Op2->getValue(), Overflow);
1867             break;
1868           case Intrinsic::uadd_with_overflow:
1869             Res = Op1->getValue().uadd_ov(Op2->getValue(), Overflow);
1870             break;
1871           case Intrinsic::ssub_with_overflow:
1872             Res = Op1->getValue().ssub_ov(Op2->getValue(), Overflow);
1873             break;
1874           case Intrinsic::usub_with_overflow:
1875             Res = Op1->getValue().usub_ov(Op2->getValue(), Overflow);
1876             break;
1877           case Intrinsic::smul_with_overflow:
1878             Res = Op1->getValue().smul_ov(Op2->getValue(), Overflow);
1879             break;
1880           case Intrinsic::umul_with_overflow:
1881             Res = Op1->getValue().umul_ov(Op2->getValue(), Overflow);
1882             break;
1883           }
1884           Constant *Ops[] = {
1885             ConstantInt::get(Ty->getContext(), Res),
1886             ConstantInt::get(Type::getInt1Ty(Ty->getContext()), Overflow)
1887           };
1888           return ConstantStruct::get(cast<StructType>(Ty), Ops);
1889         }
1890         case Intrinsic::cttz:
1891           if (Op2->isOne() && Op1->isZero()) // cttz(0, 1) is undef.
1892             return UndefValue::get(Ty);
1893           return ConstantInt::get(Ty, Op1->getValue().countTrailingZeros());
1894         case Intrinsic::ctlz:
1895           if (Op2->isOne() && Op1->isZero()) // ctlz(0, 1) is undef.
1896             return UndefValue::get(Ty);
1897           return ConstantInt::get(Ty, Op1->getValue().countLeadingZeros());
1898         }
1899       }
1900
1901       return nullptr;
1902     }
1903     return nullptr;
1904   }
1905
1906   if (Operands.size() != 3)
1907     return nullptr;
1908
1909   if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
1910     if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
1911       if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
1912         switch (IntrinsicID) {
1913         default: break;
1914         case Intrinsic::fma:
1915         case Intrinsic::fmuladd: {
1916           APFloat V = Op1->getValueAPF();
1917           APFloat::opStatus s = V.fusedMultiplyAdd(Op2->getValueAPF(),
1918                                                    Op3->getValueAPF(),
1919                                                    APFloat::rmNearestTiesToEven);
1920           if (s != APFloat::opInvalidOp)
1921             return ConstantFP::get(Ty->getContext(), V);
1922
1923           return nullptr;
1924         }
1925         }
1926       }
1927     }
1928   }
1929
1930   return nullptr;
1931 }
1932
1933 Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID,
1934                                  VectorType *VTy, ArrayRef<Constant *> Operands,
1935                                  const DataLayout &DL,
1936                                  const TargetLibraryInfo *TLI) {
1937   SmallVector<Constant *, 4> Result(VTy->getNumElements());
1938   SmallVector<Constant *, 4> Lane(Operands.size());
1939   Type *Ty = VTy->getElementType();
1940
1941   if (IntrinsicID == Intrinsic::masked_load) {
1942     auto *SrcPtr = Operands[0];
1943     auto *Mask = Operands[2];
1944     auto *Passthru = Operands[3];
1945
1946     Constant *VecData = ConstantFoldLoadFromConstPtr(SrcPtr, VTy, DL);
1947
1948     SmallVector<Constant *, 32> NewElements;
1949     for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
1950       auto *MaskElt = Mask->getAggregateElement(I);
1951       if (!MaskElt)
1952         break;
1953       auto *PassthruElt = Passthru->getAggregateElement(I);
1954       auto *VecElt = VecData ? VecData->getAggregateElement(I) : nullptr;
1955       if (isa<UndefValue>(MaskElt)) {
1956         if (PassthruElt)
1957           NewElements.push_back(PassthruElt);
1958         else if (VecElt)
1959           NewElements.push_back(VecElt);
1960         else
1961           return nullptr;
1962       }
1963       if (MaskElt->isNullValue()) {
1964         if (!PassthruElt)
1965           return nullptr;
1966         NewElements.push_back(PassthruElt);
1967       } else if (MaskElt->isOneValue()) {
1968         if (!VecElt)
1969           return nullptr;
1970         NewElements.push_back(VecElt);
1971       } else {
1972         return nullptr;
1973       }
1974     }
1975     if (NewElements.size() != VTy->getNumElements())
1976       return nullptr;
1977     return ConstantVector::get(NewElements);
1978   }
1979
1980   for (unsigned I = 0, E = VTy->getNumElements(); I != E; ++I) {
1981     // Gather a column of constants.
1982     for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
1983       Constant *Agg = Operands[J]->getAggregateElement(I);
1984       if (!Agg)
1985         return nullptr;
1986
1987       Lane[J] = Agg;
1988     }
1989
1990     // Use the regular scalar folding to simplify this column.
1991     Constant *Folded = ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI);
1992     if (!Folded)
1993       return nullptr;
1994     Result[I] = Folded;
1995   }
1996
1997   return ConstantVector::get(Result);
1998 }
1999
2000 } // end anonymous namespace
2001
2002 Constant *
2003 llvm::ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
2004                        const TargetLibraryInfo *TLI) {
2005   if (!F->hasName())
2006     return nullptr;
2007   StringRef Name = F->getName();
2008
2009   Type *Ty = F->getReturnType();
2010
2011   if (auto *VTy = dyn_cast<VectorType>(Ty))
2012     return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands,
2013                                   F->getParent()->getDataLayout(), TLI);
2014
2015   return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI);
2016 }
2017
2018 bool llvm::isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI) {
2019   // FIXME: Refactor this code; this duplicates logic in LibCallsShrinkWrap
2020   // (and to some extent ConstantFoldScalarCall).
2021   Function *F = CS.getCalledFunction();
2022   if (!F)
2023     return false;
2024
2025   LibFunc::Func Func;
2026   if (!TLI || !TLI->getLibFunc(*F, Func))
2027     return false;
2028
2029   if (CS.getNumArgOperands() == 1) {
2030     if (ConstantFP *OpC = dyn_cast<ConstantFP>(CS.getArgOperand(0))) {
2031       const APFloat &Op = OpC->getValueAPF();
2032       switch (Func) {
2033       case LibFunc::logl:
2034       case LibFunc::log:
2035       case LibFunc::logf:
2036       case LibFunc::log2l:
2037       case LibFunc::log2:
2038       case LibFunc::log2f:
2039       case LibFunc::log10l:
2040       case LibFunc::log10:
2041       case LibFunc::log10f:
2042         return Op.isNaN() || (!Op.isZero() && !Op.isNegative());
2043
2044       case LibFunc::expl:
2045       case LibFunc::exp:
2046       case LibFunc::expf:
2047         // FIXME: These boundaries are slightly conservative.
2048         if (OpC->getType()->isDoubleTy())
2049           return Op.compare(APFloat(-745.0)) != APFloat::cmpLessThan &&
2050                  Op.compare(APFloat(709.0)) != APFloat::cmpGreaterThan;
2051         if (OpC->getType()->isFloatTy())
2052           return Op.compare(APFloat(-103.0f)) != APFloat::cmpLessThan &&
2053                  Op.compare(APFloat(88.0f)) != APFloat::cmpGreaterThan;
2054         break;
2055
2056       case LibFunc::exp2l:
2057       case LibFunc::exp2:
2058       case LibFunc::exp2f:
2059         // FIXME: These boundaries are slightly conservative.
2060         if (OpC->getType()->isDoubleTy())
2061           return Op.compare(APFloat(-1074.0)) != APFloat::cmpLessThan &&
2062                  Op.compare(APFloat(1023.0)) != APFloat::cmpGreaterThan;
2063         if (OpC->getType()->isFloatTy())
2064           return Op.compare(APFloat(-149.0f)) != APFloat::cmpLessThan &&
2065                  Op.compare(APFloat(127.0f)) != APFloat::cmpGreaterThan;
2066         break;
2067
2068       case LibFunc::sinl:
2069       case LibFunc::sin:
2070       case LibFunc::sinf:
2071       case LibFunc::cosl:
2072       case LibFunc::cos:
2073       case LibFunc::cosf:
2074         return !Op.isInfinity();
2075
2076       case LibFunc::tanl:
2077       case LibFunc::tan:
2078       case LibFunc::tanf: {
2079         // FIXME: Stop using the host math library.
2080         // FIXME: The computation isn't done in the right precision.
2081         Type *Ty = OpC->getType();
2082         if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) {
2083           double OpV = getValueAsDouble(OpC);
2084           return ConstantFoldFP(tan, OpV, Ty) != nullptr;
2085         }
2086         break;
2087       }
2088
2089       case LibFunc::asinl:
2090       case LibFunc::asin:
2091       case LibFunc::asinf:
2092       case LibFunc::acosl:
2093       case LibFunc::acos:
2094       case LibFunc::acosf:
2095         return Op.compare(APFloat(Op.getSemantics(), "-1")) !=
2096                    APFloat::cmpLessThan &&
2097                Op.compare(APFloat(Op.getSemantics(), "1")) !=
2098                    APFloat::cmpGreaterThan;
2099
2100       case LibFunc::sinh:
2101       case LibFunc::cosh:
2102       case LibFunc::sinhf:
2103       case LibFunc::coshf:
2104       case LibFunc::sinhl:
2105       case LibFunc::coshl:
2106         // FIXME: These boundaries are slightly conservative.
2107         if (OpC->getType()->isDoubleTy())
2108           return Op.compare(APFloat(-710.0)) != APFloat::cmpLessThan &&
2109                  Op.compare(APFloat(710.0)) != APFloat::cmpGreaterThan;
2110         if (OpC->getType()->isFloatTy())
2111           return Op.compare(APFloat(-89.0f)) != APFloat::cmpLessThan &&
2112                  Op.compare(APFloat(89.0f)) != APFloat::cmpGreaterThan;
2113         break;
2114
2115       case LibFunc::sqrtl:
2116       case LibFunc::sqrt:
2117       case LibFunc::sqrtf:
2118         return Op.isNaN() || Op.isZero() || !Op.isNegative();
2119
2120       // FIXME: Add more functions: sqrt_finite, atanh, expm1, log1p,
2121       // maybe others?
2122       default:
2123         break;
2124       }
2125     }
2126   }
2127
2128   if (CS.getNumArgOperands() == 2) {
2129     ConstantFP *Op0C = dyn_cast<ConstantFP>(CS.getArgOperand(0));
2130     ConstantFP *Op1C = dyn_cast<ConstantFP>(CS.getArgOperand(1));
2131     if (Op0C && Op1C) {
2132       const APFloat &Op0 = Op0C->getValueAPF();
2133       const APFloat &Op1 = Op1C->getValueAPF();
2134
2135       switch (Func) {
2136       case LibFunc::powl:
2137       case LibFunc::pow:
2138       case LibFunc::powf: {
2139         // FIXME: Stop using the host math library.
2140         // FIXME: The computation isn't done in the right precision.
2141         Type *Ty = Op0C->getType();
2142         if (Ty->isDoubleTy() || Ty->isFloatTy() || Ty->isHalfTy()) {
2143           if (Ty == Op1C->getType()) {
2144             double Op0V = getValueAsDouble(Op0C);
2145             double Op1V = getValueAsDouble(Op1C);
2146             return ConstantFoldBinaryFP(pow, Op0V, Op1V, Ty) != nullptr;
2147           }
2148         }
2149         break;
2150       }
2151
2152       case LibFunc::fmodl:
2153       case LibFunc::fmod:
2154       case LibFunc::fmodf:
2155         return Op0.isNaN() || Op1.isNaN() ||
2156                (!Op0.isInfinity() && !Op1.isZero());
2157
2158       default:
2159         break;
2160       }
2161     }
2162   }
2163
2164   return false;
2165 }