Coverage Report

Created: 2019-07-24 05:18

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