Coverage Report

Created: 2017-10-03 07:32

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