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