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