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