LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConstantFolding.cpp
Go to the documentation of this file.
1 //===-- ConstantFolding.cpp - Fold instructions into constants ------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines routines for folding instructions into constants.
11 //
12 // Also, to supplement the basic IR ConstantExpr simplifications,
13 // this file defines some additional folding routines that can make use of
14 // DataLayout information. These functions cannot go in IR due to library
15 // dependency issues.
16 //
17 //===----------------------------------------------------------------------===//
18 
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringMap.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/Intrinsics.h"
31 #include "llvm/IR/Operator.h"
33 #include "llvm/Support/FEnv.h"
37 #include <cerrno>
38 #include <cmath>
39 using namespace llvm;
40 
41 //===----------------------------------------------------------------------===//
42 // Constant Folding internal helper functions
43 //===----------------------------------------------------------------------===//
44 
45 /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with
46 /// DataLayout. This always returns a non-null constant, but it may be a
47 /// ConstantExpr if unfoldable.
48 static Constant *FoldBitCast(Constant *C, Type *DestTy,
49  const DataLayout &TD) {
50  // Catch the obvious splat cases.
51  if (C->isNullValue() && !DestTy->isX86_MMXTy())
52  return Constant::getNullValue(DestTy);
53  if (C->isAllOnesValue() && !DestTy->isX86_MMXTy())
54  return Constant::getAllOnesValue(DestTy);
55 
56  // Handle a vector->integer cast.
57  if (IntegerType *IT = dyn_cast<IntegerType>(DestTy)) {
58  VectorType *VTy = dyn_cast<VectorType>(C->getType());
59  if (VTy == 0)
60  return ConstantExpr::getBitCast(C, DestTy);
61 
62  unsigned NumSrcElts = VTy->getNumElements();
63  Type *SrcEltTy = VTy->getElementType();
64 
65  // If the vector is a vector of floating point, convert it to vector of int
66  // to simplify things.
67  if (SrcEltTy->isFloatingPointTy()) {
68  unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
69  Type *SrcIVTy =
70  VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElts);
71  // Ask IR to do the conversion now that #elts line up.
72  C = ConstantExpr::getBitCast(C, SrcIVTy);
73  }
74 
76  if (CDV == 0)
77  return ConstantExpr::getBitCast(C, DestTy);
78 
79  // Now that we know that the input value is a vector of integers, just shift
80  // and insert them into our result.
81  unsigned BitShift = TD.getTypeAllocSizeInBits(SrcEltTy);
82  APInt Result(IT->getBitWidth(), 0);
83  for (unsigned i = 0; i != NumSrcElts; ++i) {
84  Result <<= BitShift;
85  if (TD.isLittleEndian())
86  Result |= CDV->getElementAsInteger(NumSrcElts-i-1);
87  else
88  Result |= CDV->getElementAsInteger(i);
89  }
90 
91  return ConstantInt::get(IT, Result);
92  }
93 
94  // The code below only handles casts to vectors currently.
95  VectorType *DestVTy = dyn_cast<VectorType>(DestTy);
96  if (DestVTy == 0)
97  return ConstantExpr::getBitCast(C, DestTy);
98 
99  // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
100  // vector so the code below can handle it uniformly.
101  if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) {
102  Constant *Ops = C; // don't take the address of C!
103  return FoldBitCast(ConstantVector::get(Ops), DestTy, TD);
104  }
105 
106  // If this is a bitcast from constant vector -> vector, fold it.
107  if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
108  return ConstantExpr::getBitCast(C, DestTy);
109 
110  // If the element types match, IR can fold it.
111  unsigned NumDstElt = DestVTy->getNumElements();
112  unsigned NumSrcElt = C->getType()->getVectorNumElements();
113  if (NumDstElt == NumSrcElt)
114  return ConstantExpr::getBitCast(C, DestTy);
115 
116  Type *SrcEltTy = C->getType()->getVectorElementType();
117  Type *DstEltTy = DestVTy->getElementType();
118 
119  // Otherwise, we're changing the number of elements in a vector, which
120  // requires endianness information to do the right thing. For example,
121  // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
122  // folds to (little endian):
123  // <4 x i32> <i32 0, i32 0, i32 1, i32 0>
124  // and to (big endian):
125  // <4 x i32> <i32 0, i32 0, i32 0, i32 1>
126 
127  // First thing is first. We only want to think about integer here, so if
128  // we have something in FP form, recast it as integer.
129  if (DstEltTy->isFloatingPointTy()) {
130  // Fold to an vector of integers with same size as our FP type.
131  unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
132  Type *DestIVTy =
133  VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt);
134  // Recursively handle this integer conversion, if possible.
135  C = FoldBitCast(C, DestIVTy, TD);
136 
137  // Finally, IR can handle this now that #elts line up.
138  return ConstantExpr::getBitCast(C, DestTy);
139  }
140 
141  // Okay, we know the destination is integer, if the input is FP, convert
142  // it to integer first.
143  if (SrcEltTy->isFloatingPointTy()) {
144  unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
145  Type *SrcIVTy =
146  VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
147  // Ask IR to do the conversion now that #elts line up.
148  C = ConstantExpr::getBitCast(C, SrcIVTy);
149  // If IR wasn't able to fold it, bail out.
150  if (!isa<ConstantVector>(C) && // FIXME: Remove ConstantVector.
151  !isa<ConstantDataVector>(C))
152  return C;
153  }
154 
155  // Now we know that the input and output vectors are both integer vectors
156  // of the same size, and that their #elements is not the same. Do the
157  // conversion here, which depends on whether the input or output has
158  // more elements.
159  bool isLittleEndian = TD.isLittleEndian();
160 
162  if (NumDstElt < NumSrcElt) {
163  // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
164  Constant *Zero = Constant::getNullValue(DstEltTy);
165  unsigned Ratio = NumSrcElt/NumDstElt;
166  unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
167  unsigned SrcElt = 0;
168  for (unsigned i = 0; i != NumDstElt; ++i) {
169  // Build each element of the result.
170  Constant *Elt = Zero;
171  unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
172  for (unsigned j = 0; j != Ratio; ++j) {
173  Constant *Src =dyn_cast<ConstantInt>(C->getAggregateElement(SrcElt++));
174  if (!Src) // Reject constantexpr elements.
175  return ConstantExpr::getBitCast(C, DestTy);
176 
177  // Zero extend the element to the right size.
178  Src = ConstantExpr::getZExt(Src, Elt->getType());
179 
180  // Shift it to the right place, depending on endianness.
181  Src = ConstantExpr::getShl(Src,
182  ConstantInt::get(Src->getType(), ShiftAmt));
183  ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
184 
185  // Mix it in.
186  Elt = ConstantExpr::getOr(Elt, Src);
187  }
188  Result.push_back(Elt);
189  }
190  return ConstantVector::get(Result);
191  }
192 
193  // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
194  unsigned Ratio = NumDstElt/NumSrcElt;
195  unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits();
196 
197  // Loop over each source value, expanding into multiple results.
198  for (unsigned i = 0; i != NumSrcElt; ++i) {
200  if (!Src) // Reject constantexpr elements.
201  return ConstantExpr::getBitCast(C, DestTy);
202 
203  unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
204  for (unsigned j = 0; j != Ratio; ++j) {
205  // Shift the piece of the value into the right place, depending on
206  // endianness.
207  Constant *Elt = ConstantExpr::getLShr(Src,
208  ConstantInt::get(Src->getType(), ShiftAmt));
209  ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
210 
211  // Truncate and remember this piece.
212  Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
213  }
214  }
215 
216  return ConstantVector::get(Result);
217 }
218 
219 
220 /// IsConstantOffsetFromGlobal - If this constant is actually a constant offset
221 /// from a global, return the global and the constant. Because of
222 /// constantexprs, this function is recursive.
224  APInt &Offset, const DataLayout &TD) {
225  // Trivial case, constant is the global.
226  if ((GV = dyn_cast<GlobalValue>(C))) {
227  unsigned BitWidth = TD.getPointerTypeSizeInBits(GV->getType());
228  Offset = APInt(BitWidth, 0);
229  return true;
230  }
231 
232  // Otherwise, if this isn't a constant expr, bail out.
234  if (!CE) return false;
235 
236  // Look through ptr->int and ptr->ptr casts.
237  if (CE->getOpcode() == Instruction::PtrToInt ||
238  CE->getOpcode() == Instruction::BitCast)
239  return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD);
240 
241  // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
242  GEPOperator *GEP = dyn_cast<GEPOperator>(CE);
243  if (!GEP)
244  return false;
245 
246  unsigned BitWidth = TD.getPointerTypeSizeInBits(GEP->getType());
247  APInt TmpOffset(BitWidth, 0);
248 
249  // If the base isn't a global+constant, we aren't either.
250  if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, TmpOffset, TD))
251  return false;
252 
253  // Otherwise, add any offset that our operands provide.
254  if (!GEP->accumulateConstantOffset(TD, TmpOffset))
255  return false;
256 
257  Offset = TmpOffset;
258  return true;
259 }
260 
261 /// ReadDataFromGlobal - Recursive helper to read bits out of global. C is the
262 /// constant being copied out of. ByteOffset is an offset into C. CurPtr is the
263 /// pointer to copy results into and BytesLeft is the number of bytes left in
264 /// the CurPtr buffer. TD is the target data.
265 static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset,
266  unsigned char *CurPtr, unsigned BytesLeft,
267  const DataLayout &TD) {
268  assert(ByteOffset <= TD.getTypeAllocSize(C->getType()) &&
269  "Out of range access");
270 
271  // If this element is zero or undefined, we can just return since *CurPtr is
272  // zero initialized.
273  if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
274  return true;
275 
276  if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
277  if (CI->getBitWidth() > 64 ||
278  (CI->getBitWidth() & 7) != 0)
279  return false;
280 
281  uint64_t Val = CI->getZExtValue();
282  unsigned IntBytes = unsigned(CI->getBitWidth()/8);
283 
284  for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
285  int n = ByteOffset;
286  if (!TD.isLittleEndian())
287  n = IntBytes - n - 1;
288  CurPtr[i] = (unsigned char)(Val >> (n * 8));
289  ++ByteOffset;
290  }
291  return true;
292  }
293 
294  if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
295  if (CFP->getType()->isDoubleTy()) {
296  C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), TD);
297  return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
298  }
299  if (CFP->getType()->isFloatTy()){
300  C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), TD);
301  return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
302  }
303  if (CFP->getType()->isHalfTy()){
304  C = FoldBitCast(C, Type::getInt16Ty(C->getContext()), TD);
305  return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
306  }
307  return false;
308  }
309 
310  if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
311  const StructLayout *SL = TD.getStructLayout(CS->getType());
312  unsigned Index = SL->getElementContainingOffset(ByteOffset);
313  uint64_t CurEltOffset = SL->getElementOffset(Index);
314  ByteOffset -= CurEltOffset;
315 
316  while (1) {
317  // If the element access is to the element itself and not to tail padding,
318  // read the bytes from the element.
319  uint64_t EltSize = TD.getTypeAllocSize(CS->getOperand(Index)->getType());
320 
321  if (ByteOffset < EltSize &&
322  !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
323  BytesLeft, TD))
324  return false;
325 
326  ++Index;
327 
328  // Check to see if we read from the last struct element, if so we're done.
329  if (Index == CS->getType()->getNumElements())
330  return true;
331 
332  // If we read all of the bytes we needed from this element we're done.
333  uint64_t NextEltOffset = SL->getElementOffset(Index);
334 
335  if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
336  return true;
337 
338  // Move to the next element of the struct.
339  CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
340  BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
341  ByteOffset = 0;
342  CurEltOffset = NextEltOffset;
343  }
344  // not reached.
345  }
346 
347  if (isa<ConstantArray>(C) || isa<ConstantVector>(C) ||
348  isa<ConstantDataSequential>(C)) {
349  Type *EltTy = C->getType()->getSequentialElementType();
350  uint64_t EltSize = TD.getTypeAllocSize(EltTy);
351  uint64_t Index = ByteOffset / EltSize;
352  uint64_t Offset = ByteOffset - Index * EltSize;
353  uint64_t NumElts;
354  if (ArrayType *AT = dyn_cast<ArrayType>(C->getType()))
355  NumElts = AT->getNumElements();
356  else
357  NumElts = C->getType()->getVectorNumElements();
358 
359  for (; Index != NumElts; ++Index) {
360  if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
361  BytesLeft, TD))
362  return false;
363 
364  uint64_t BytesWritten = EltSize - Offset;
365  assert(BytesWritten <= EltSize && "Not indexing into this element?");
366  if (BytesWritten >= BytesLeft)
367  return true;
368 
369  Offset = 0;
370  BytesLeft -= BytesWritten;
371  CurPtr += BytesWritten;
372  }
373  return true;
374  }
375 
376  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
377  if (CE->getOpcode() == Instruction::IntToPtr &&
378  CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getType())) {
379  return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
380  BytesLeft, TD);
381  }
382  }
383 
384  // Otherwise, unknown initializer type.
385  return false;
386 }
387 
389  const DataLayout &TD) {
390  PointerType *PTy = cast<PointerType>(C->getType());
391  Type *LoadTy = PTy->getElementType();
392  IntegerType *IntType = dyn_cast<IntegerType>(LoadTy);
393 
394  // If this isn't an integer load we can't fold it directly.
395  if (!IntType) {
396  unsigned AS = PTy->getAddressSpace();
397 
398  // If this is a float/double load, we can try folding it as an int32/64 load
399  // and then bitcast the result. This can be useful for union cases. Note
400  // that address spaces don't matter here since we're not going to result in
401  // an actual new load.
402  Type *MapTy;
403  if (LoadTy->isHalfTy())
404  MapTy = Type::getInt16PtrTy(C->getContext(), AS);
405  else if (LoadTy->isFloatTy())
406  MapTy = Type::getInt32PtrTy(C->getContext(), AS);
407  else if (LoadTy->isDoubleTy())
408  MapTy = Type::getInt64PtrTy(C->getContext(), AS);
409  else if (LoadTy->isVectorTy()) {
411  TD.getTypeAllocSizeInBits(LoadTy),
412  AS);
413  } else
414  return 0;
415 
416  C = FoldBitCast(C, MapTy, TD);
417  if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, TD))
418  return FoldBitCast(Res, LoadTy, TD);
419  return 0;
420  }
421 
422  unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
423  if (BytesLoaded > 32 || BytesLoaded == 0)
424  return 0;
425 
426  GlobalValue *GVal;
427  APInt Offset;
428  if (!IsConstantOffsetFromGlobal(C, GVal, Offset, TD))
429  return 0;
430 
432  if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
433  !GV->getInitializer()->getType()->isSized())
434  return 0;
435 
436  // If we're loading off the beginning of the global, some bytes may be valid,
437  // but we don't try to handle this.
438  if (Offset.isNegative())
439  return 0;
440 
441  // If we're not accessing anything in this constant, the result is undefined.
442  if (Offset.getZExtValue() >=
444  return UndefValue::get(IntType);
445 
446  unsigned char RawBytes[32] = {0};
447  if (!ReadDataFromGlobal(GV->getInitializer(), Offset.getZExtValue(), RawBytes,
448  BytesLoaded, TD))
449  return 0;
450 
451  APInt ResultVal = APInt(IntType->getBitWidth(), 0);
452  if (TD.isLittleEndian()) {
453  ResultVal = RawBytes[BytesLoaded - 1];
454  for (unsigned i = 1; i != BytesLoaded; ++i) {
455  ResultVal <<= 8;
456  ResultVal |= RawBytes[BytesLoaded - 1 - i];
457  }
458  } else {
459  ResultVal = RawBytes[0];
460  for (unsigned i = 1; i != BytesLoaded; ++i) {
461  ResultVal <<= 8;
462  ResultVal |= RawBytes[i];
463  }
464  }
465 
466  return ConstantInt::get(IntType->getContext(), ResultVal);
467 }
468 
469 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
470 /// produce if it is constant and determinable. If this is not determinable,
471 /// return null.
473  const DataLayout *TD) {
474  // First, try the easy cases:
475  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
476  if (GV->isConstant() && GV->hasDefinitiveInitializer())
477  return GV->getInitializer();
478 
479  // If the loaded value isn't a constant expr, we can't handle it.
481  if (!CE)
482  return 0;
483 
484  if (CE->getOpcode() == Instruction::GetElementPtr) {
485  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
486  if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
487  if (Constant *V =
488  ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
489  return V;
490  }
491  }
492  }
493 
494  // Instead of loading constant c string, use corresponding integer value
495  // directly if string length is small enough.
496  StringRef Str;
497  if (TD && getConstantStringInfo(CE, Str) && !Str.empty()) {
498  unsigned StrLen = Str.size();
499  Type *Ty = cast<PointerType>(CE->getType())->getElementType();
500  unsigned NumBits = Ty->getPrimitiveSizeInBits();
501  // Replace load with immediate integer if the result is an integer or fp
502  // value.
503  if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
504  (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) {
505  APInt StrVal(NumBits, 0);
506  APInt SingleChar(NumBits, 0);
507  if (TD->isLittleEndian()) {
508  for (signed i = StrLen-1; i >= 0; i--) {
509  SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
510  StrVal = (StrVal << 8) | SingleChar;
511  }
512  } else {
513  for (unsigned i = 0; i < StrLen; i++) {
514  SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
515  StrVal = (StrVal << 8) | SingleChar;
516  }
517  // Append NULL at the end.
518  SingleChar = 0;
519  StrVal = (StrVal << 8) | SingleChar;
520  }
521 
523  if (Ty->isFloatingPointTy())
524  Res = ConstantExpr::getBitCast(Res, Ty);
525  return Res;
526  }
527  }
528 
529  // If this load comes from anywhere in a constant global, and if the global
530  // is all undef or zero, we know what it loads.
531  if (GlobalVariable *GV =
532  dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, TD))) {
533  if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
534  Type *ResTy = cast<PointerType>(C->getType())->getElementType();
535  if (GV->getInitializer()->isNullValue())
536  return Constant::getNullValue(ResTy);
537  if (isa<UndefValue>(GV->getInitializer()))
538  return UndefValue::get(ResTy);
539  }
540  }
541 
542  // Try hard to fold loads from bitcasted strange and non-type-safe things.
543  if (TD)
544  return FoldReinterpretLoadFromConstPtr(CE, *TD);
545  return 0;
546 }
547 
549  if (LI->isVolatile()) return 0;
550 
551  if (Constant *C = dyn_cast<Constant>(LI->getOperand(0)))
552  return ConstantFoldLoadFromConstPtr(C, TD);
553 
554  return 0;
555 }
556 
557 /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression.
558 /// Attempt to symbolically evaluate the result of a binary operator merging
559 /// these together. If target data info is available, it is provided as DL,
560 /// otherwise DL is null.
561 static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
562  Constant *Op1, const DataLayout *DL){
563  // SROA
564 
565  // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
566  // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
567  // bits.
568 
569 
570  if (Opc == Instruction::And && DL) {
571  unsigned BitWidth = DL->getTypeSizeInBits(Op0->getType()->getScalarType());
572  APInt KnownZero0(BitWidth, 0), KnownOne0(BitWidth, 0);
573  APInt KnownZero1(BitWidth, 0), KnownOne1(BitWidth, 0);
574  ComputeMaskedBits(Op0, KnownZero0, KnownOne0, DL);
575  ComputeMaskedBits(Op1, KnownZero1, KnownOne1, DL);
576  if ((KnownOne1 | KnownZero0).isAllOnesValue()) {
577  // All the bits of Op0 that the 'and' could be masking are already zero.
578  return Op0;
579  }
580  if ((KnownOne0 | KnownZero1).isAllOnesValue()) {
581  // All the bits of Op1 that the 'and' could be masking are already zero.
582  return Op1;
583  }
584 
585  APInt KnownZero = KnownZero0 | KnownZero1;
586  APInt KnownOne = KnownOne0 & KnownOne1;
587  if ((KnownZero | KnownOne).isAllOnesValue()) {
588  return ConstantInt::get(Op0->getType(), KnownOne);
589  }
590  }
591 
592  // If the constant expr is something like &A[123] - &A[4].f, fold this into a
593  // constant. This happens frequently when iterating over a global array.
594  if (Opc == Instruction::Sub && DL) {
595  GlobalValue *GV1, *GV2;
596  APInt Offs1, Offs2;
597 
598  if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *DL))
599  if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *DL) &&
600  GV1 == GV2) {
601  unsigned OpSize = DL->getTypeSizeInBits(Op0->getType());
602 
603  // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
604  // PtrToInt may change the bitwidth so we have convert to the right size
605  // first.
606  return ConstantInt::get(Op0->getType(), Offs1.zextOrTrunc(OpSize) -
607  Offs2.zextOrTrunc(OpSize));
608  }
609  }
610 
611  return 0;
612 }
613 
614 /// CastGEPIndices - If array indices are not pointer-sized integers,
615 /// explicitly cast them so that they aren't implicitly casted by the
616 /// getelementptr.
618  Type *ResultTy, const DataLayout *TD,
619  const TargetLibraryInfo *TLI) {
620  if (!TD)
621  return 0;
622 
623  Type *IntPtrTy = TD->getIntPtrType(ResultTy);
624 
625  bool Any = false;
627  for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
628  if ((i == 1 ||
629  !isa<StructType>(GetElementPtrInst::getIndexedType(
630  Ops[0]->getType(),
631  Ops.slice(1, i - 1)))) &&
632  Ops[i]->getType() != IntPtrTy) {
633  Any = true;
635  true,
636  IntPtrTy,
637  true),
638  Ops[i], IntPtrTy));
639  } else
640  NewIdxs.push_back(Ops[i]);
641  }
642 
643  if (!Any)
644  return 0;
645 
646  Constant *C = ConstantExpr::getGetElementPtr(Ops[0], NewIdxs);
647  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
648  if (Constant *Folded = ConstantFoldConstantExpression(CE, TD, TLI))
649  C = Folded;
650  }
651 
652  return C;
653 }
654 
655 /// Strip the pointer casts, but preserve the address space information.
657  assert(Ptr->getType()->isPointerTy() && "Not a pointer type");
658  PointerType *OldPtrTy = cast<PointerType>(Ptr->getType());
659  Ptr = cast<Constant>(Ptr->stripPointerCasts());
660  PointerType *NewPtrTy = cast<PointerType>(Ptr->getType());
661 
662  // Preserve the address space number of the pointer.
663  if (NewPtrTy->getAddressSpace() != OldPtrTy->getAddressSpace()) {
664  NewPtrTy = NewPtrTy->getElementType()->getPointerTo(
665  OldPtrTy->getAddressSpace());
666  Ptr = ConstantExpr::getPointerCast(Ptr, NewPtrTy);
667  }
668  return Ptr;
669 }
670 
671 /// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP
672 /// constant expression, do so.
674  Type *ResultTy, const DataLayout *TD,
675  const TargetLibraryInfo *TLI) {
676  Constant *Ptr = Ops[0];
677  if (!TD || !Ptr->getType()->getPointerElementType()->isSized() ||
678  !Ptr->getType()->isPointerTy())
679  return 0;
680 
681  Type *IntPtrTy = TD->getIntPtrType(Ptr->getType());
682  Type *ResultElementTy = ResultTy->getPointerElementType();
683 
684  // If this is a constant expr gep that is effectively computing an
685  // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
686  for (unsigned i = 1, e = Ops.size(); i != e; ++i)
687  if (!isa<ConstantInt>(Ops[i])) {
688 
689  // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
690  // "inttoptr (sub (ptrtoint Ptr), V)"
691  if (Ops.size() == 2 && ResultElementTy->isIntegerTy(8)) {
692  ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[1]);
693  assert((CE == 0 || CE->getType() == IntPtrTy) &&
694  "CastGEPIndices didn't canonicalize index types!");
695  if (CE && CE->getOpcode() == Instruction::Sub &&
696  CE->getOperand(0)->isNullValue()) {
697  Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
698  Res = ConstantExpr::getSub(Res, CE->getOperand(1));
699  Res = ConstantExpr::getIntToPtr(Res, ResultTy);
700  if (ConstantExpr *ResCE = dyn_cast<ConstantExpr>(Res))
701  Res = ConstantFoldConstantExpression(ResCE, TD, TLI);
702  return Res;
703  }
704  }
705  return 0;
706  }
707 
708  unsigned BitWidth = TD->getTypeSizeInBits(IntPtrTy);
709  APInt Offset =
710  APInt(BitWidth, TD->getIndexedOffset(Ptr->getType(),
711  makeArrayRef((Value *const*)
712  Ops.data() + 1,
713  Ops.size() - 1)));
714  Ptr = StripPtrCastKeepAS(Ptr);
715 
716  // If this is a GEP of a GEP, fold it all into a single GEP.
717  while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
718  SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end());
719 
720  // Do not try the incorporate the sub-GEP if some index is not a number.
721  bool AllConstantInt = true;
722  for (unsigned i = 0, e = NestedOps.size(); i != e; ++i)
723  if (!isa<ConstantInt>(NestedOps[i])) {
724  AllConstantInt = false;
725  break;
726  }
727  if (!AllConstantInt)
728  break;
729 
730  Ptr = cast<Constant>(GEP->getOperand(0));
731  Offset += APInt(BitWidth,
732  TD->getIndexedOffset(Ptr->getType(), NestedOps));
733  Ptr = StripPtrCastKeepAS(Ptr);
734  }
735 
736  // If the base value for this address is a literal integer value, fold the
737  // getelementptr to the resulting integer value casted to the pointer type.
738  APInt BasePtr(BitWidth, 0);
739  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
740  if (CE->getOpcode() == Instruction::IntToPtr) {
741  if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
742  BasePtr = Base->getValue().zextOrTrunc(BitWidth);
743  }
744  }
745 
746  if (Ptr->isNullValue() || BasePtr != 0) {
747  Constant *C = ConstantInt::get(Ptr->getContext(), Offset + BasePtr);
748  return ConstantExpr::getIntToPtr(C, ResultTy);
749  }
750 
751  // Otherwise form a regular getelementptr. Recompute the indices so that
752  // we eliminate over-indexing of the notional static type array bounds.
753  // This makes it easy to determine if the getelementptr is "inbounds".
754  // Also, this helps GlobalOpt do SROA on GlobalVariables.
755  Type *Ty = Ptr->getType();
756  assert(Ty->isPointerTy() && "Forming regular GEP of non-pointer type");
758 
759  do {
760  if (SequentialType *ATy = dyn_cast<SequentialType>(Ty)) {
761  if (ATy->isPointerTy()) {
762  // The only pointer indexing we'll do is on the first index of the GEP.
763  if (!NewIdxs.empty())
764  break;
765 
766  // Only handle pointers to sized types, not pointers to functions.
767  if (!ATy->getElementType()->isSized())
768  return 0;
769  }
770 
771  // Determine which element of the array the offset points into.
772  APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType()));
773  if (ElemSize == 0)
774  // The element size is 0. This may be [0 x Ty]*, so just use a zero
775  // index for this level and proceed to the next level to see if it can
776  // accommodate the offset.
777  NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0));
778  else {
779  // The element size is non-zero divide the offset by the element
780  // size (rounding down), to compute the index at this level.
781  APInt NewIdx = Offset.udiv(ElemSize);
782  Offset -= NewIdx * ElemSize;
783  NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
784  }
785  Ty = ATy->getElementType();
786  } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
787  // If we end up with an offset that isn't valid for this struct type, we
788  // can't re-form this GEP in a regular form, so bail out. The pointer
789  // operand likely went through casts that are necessary to make the GEP
790  // sensible.
791  const StructLayout &SL = *TD->getStructLayout(STy);
792  if (Offset.uge(SL.getSizeInBytes()))
793  break;
794 
795  // Determine which field of the struct the offset points into. The
796  // getZExtValue is fine as we've already ensured that the offset is
797  // within the range representable by the StructLayout API.
798  unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
799  NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
800  ElIdx));
801  Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
802  Ty = STy->getTypeAtIndex(ElIdx);
803  } else {
804  // We've reached some non-indexable type.
805  break;
806  }
807  } while (Ty != ResultElementTy);
808 
809  // If we haven't used up the entire offset by descending the static
810  // type, then the offset is pointing into the middle of an indivisible
811  // member, so we can't simplify it.
812  if (Offset != 0)
813  return 0;
814 
815  // Create a GEP.
816  Constant *C = ConstantExpr::getGetElementPtr(Ptr, NewIdxs);
817  assert(C->getType()->getPointerElementType() == Ty &&
818  "Computed GetElementPtr has unexpected type!");
819 
820  // If we ended up indexing a member with a type that doesn't match
821  // the type of what the original indices indexed, add a cast.
822  if (Ty != ResultElementTy)
823  C = FoldBitCast(C, ResultTy, *TD);
824 
825  return C;
826 }
827 
828 
829 
830 //===----------------------------------------------------------------------===//
831 // Constant Folding public APIs
832 //===----------------------------------------------------------------------===//
833 
834 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
835 /// If successful, the constant result is returned, if not, null is returned.
836 /// Note that this fails if not all of the operands are constant. Otherwise,
837 /// this function can only fail when attempting to fold instructions like loads
838 /// and stores, which have no constant expression form.
840  const DataLayout *TD,
841  const TargetLibraryInfo *TLI) {
842  // Handle PHI nodes quickly here...
843  if (PHINode *PN = dyn_cast<PHINode>(I)) {
844  Constant *CommonValue = 0;
845 
846  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
847  Value *Incoming = PN->getIncomingValue(i);
848  // If the incoming value is undef then skip it. Note that while we could
849  // skip the value if it is equal to the phi node itself we choose not to
850  // because that would break the rule that constant folding only applies if
851  // all operands are constants.
852  if (isa<UndefValue>(Incoming))
853  continue;
854  // If the incoming value is not a constant, then give up.
855  Constant *C = dyn_cast<Constant>(Incoming);
856  if (!C)
857  return 0;
858  // Fold the PHI's operands.
859  if (ConstantExpr *NewC = dyn_cast<ConstantExpr>(C))
860  C = ConstantFoldConstantExpression(NewC, TD, TLI);
861  // If the incoming value is a different constant to
862  // the one we saw previously, then give up.
863  if (CommonValue && C != CommonValue)
864  return 0;
865  CommonValue = C;
866  }
867 
868 
869  // If we reach here, all incoming values are the same constant or undef.
870  return CommonValue ? CommonValue : UndefValue::get(PN->getType());
871  }
872 
873  // Scan the operand list, checking to see if they are all constants, if so,
874  // hand off to ConstantFoldInstOperands.
876  for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
877  Constant *Op = dyn_cast<Constant>(*i);
878  if (!Op)
879  return 0; // All operands not constant!
880 
881  // Fold the Instruction's operands.
882  if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(Op))
883  Op = ConstantFoldConstantExpression(NewCE, TD, TLI);
884 
885  Ops.push_back(Op);
886  }
887 
888  if (const CmpInst *CI = dyn_cast<CmpInst>(I))
889  return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
890  TD, TLI);
891 
892  if (const LoadInst *LI = dyn_cast<LoadInst>(I))
893  return ConstantFoldLoadInst(LI, TD);
894 
895  if (InsertValueInst *IVI = dyn_cast<InsertValueInst>(I)) {
897  cast<Constant>(IVI->getAggregateOperand()),
898  cast<Constant>(IVI->getInsertedValueOperand()),
899  IVI->getIndices());
900  }
901 
902  if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I)) {
904  cast<Constant>(EVI->getAggregateOperand()),
905  EVI->getIndices());
906  }
907 
908  return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Ops, TD, TLI);
909 }
910 
911 static Constant *
913  const TargetLibraryInfo *TLI,
914  SmallPtrSet<ConstantExpr *, 4> &FoldedOps) {
916  for (User::const_op_iterator i = CE->op_begin(), e = CE->op_end(); i != e;
917  ++i) {
918  Constant *NewC = cast<Constant>(*i);
919  // Recursively fold the ConstantExpr's operands. If we have already folded
920  // a ConstantExpr, we don't have to process it again.
921  if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC)) {
922  if (FoldedOps.insert(NewCE))
923  NewC = ConstantFoldConstantExpressionImpl(NewCE, TD, TLI, FoldedOps);
924  }
925  Ops.push_back(NewC);
926  }
927 
928  if (CE->isCompare())
929  return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
930  TD, TLI);
931  return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), Ops, TD, TLI);
932 }
933 
934 /// ConstantFoldConstantExpression - Attempt to fold the constant expression
935 /// using the specified DataLayout. If successful, the constant result is
936 /// result is returned, if not, null is returned.
938  const DataLayout *TD,
939  const TargetLibraryInfo *TLI) {
941  return ConstantFoldConstantExpressionImpl(CE, TD, TLI, FoldedOps);
942 }
943 
944 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
945 /// specified opcode and operands. If successful, the constant result is
946 /// returned, if not, null is returned. Note that this function can fail when
947 /// attempting to fold instructions like loads and stores, which have no
948 /// constant expression form.
949 ///
950 /// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/etc
951 /// information, due to only being passed an opcode and operands. Constant
952 /// folding using this function strips this information.
953 ///
954 Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
956  const DataLayout *TD,
957  const TargetLibraryInfo *TLI) {
958  // Handle easy binops first.
959  if (Instruction::isBinaryOp(Opcode)) {
960  if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1])) {
961  if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD))
962  return C;
963  }
964 
965  return ConstantExpr::get(Opcode, Ops[0], Ops[1]);
966  }
967 
968  switch (Opcode) {
969  default: return 0;
970  case Instruction::ICmp:
971  case Instruction::FCmp: llvm_unreachable("Invalid for compares");
972  case Instruction::Call:
973  if (Function *F = dyn_cast<Function>(Ops.back()))
975  return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI);
976  return 0;
977  case Instruction::PtrToInt:
978  // If the input is a inttoptr, eliminate the pair. This requires knowing
979  // the width of a pointer, so it can't be done in ConstantExpr::getCast.
980  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
981  if (TD && CE->getOpcode() == Instruction::IntToPtr) {
982  Constant *Input = CE->getOperand(0);
983  unsigned InWidth = Input->getType()->getScalarSizeInBits();
984  unsigned PtrWidth = TD->getPointerTypeSizeInBits(CE->getType());
985  if (PtrWidth < InWidth) {
986  Constant *Mask =
987  ConstantInt::get(CE->getContext(),
988  APInt::getLowBitsSet(InWidth, PtrWidth));
989  Input = ConstantExpr::getAnd(Input, Mask);
990  }
991  // Do a zext or trunc to get to the dest size.
992  return ConstantExpr::getIntegerCast(Input, DestTy, false);
993  }
994  }
995  return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
996  case Instruction::IntToPtr:
997  // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
998  // the int size is >= the ptr size and the address spaces are the same.
999  // This requires knowing the width of a pointer, so it can't be done in
1000  // ConstantExpr::getCast.
1001  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
1002  if (TD && CE->getOpcode() == Instruction::PtrToInt) {
1003  Constant *SrcPtr = CE->getOperand(0);
1004  unsigned SrcPtrSize = TD->getPointerTypeSizeInBits(SrcPtr->getType());
1005  unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1006 
1007  if (MidIntSize >= SrcPtrSize) {
1008  unsigned SrcAS = SrcPtr->getType()->getPointerAddressSpace();
1009  if (SrcAS == DestTy->getPointerAddressSpace())
1010  return FoldBitCast(CE->getOperand(0), DestTy, *TD);
1011  }
1012  }
1013  }
1014 
1015  return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
1016  case Instruction::Trunc:
1017  case Instruction::ZExt:
1018  case Instruction::SExt:
1019  case Instruction::FPTrunc:
1020  case Instruction::FPExt:
1021  case Instruction::UIToFP:
1022  case Instruction::SIToFP:
1023  case Instruction::FPToUI:
1024  case Instruction::FPToSI:
1025  case Instruction::AddrSpaceCast:
1026  return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
1027  case Instruction::BitCast:
1028  if (TD)
1029  return FoldBitCast(Ops[0], DestTy, *TD);
1030  return ConstantExpr::getBitCast(Ops[0], DestTy);
1031  case Instruction::Select:
1032  return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
1034  return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
1035  case Instruction::InsertElement:
1036  return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
1037  case Instruction::ShuffleVector:
1038  return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
1039  case Instruction::GetElementPtr:
1040  if (Constant *C = CastGEPIndices(Ops, DestTy, TD, TLI))
1041  return C;
1042  if (Constant *C = SymbolicallyEvaluateGEP(Ops, DestTy, TD, TLI))
1043  return C;
1044 
1045  return ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1));
1046  }
1047 }
1048 
1049 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
1050 /// instruction (icmp/fcmp) with the specified operands. If it fails, it
1051 /// returns a constant expression of the specified operands.
1052 ///
1054  Constant *Ops0, Constant *Ops1,
1055  const DataLayout *TD,
1056  const TargetLibraryInfo *TLI) {
1057  // fold: icmp (inttoptr x), null -> icmp x, 0
1058  // fold: icmp (ptrtoint x), 0 -> icmp x, null
1059  // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
1060  // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
1061  //
1062  // ConstantExpr::getCompare cannot do this, because it doesn't have TD
1063  // around to know if bit truncation is happening.
1064  if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
1065  if (TD && Ops1->isNullValue()) {
1066  if (CE0->getOpcode() == Instruction::IntToPtr) {
1067  Type *IntPtrTy = TD->getIntPtrType(CE0->getType());
1068  // Convert the integer value to the right size to ensure we get the
1069  // proper extension or truncation.
1070  Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1071  IntPtrTy, false);
1072  Constant *Null = Constant::getNullValue(C->getType());
1073  return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
1074  }
1075 
1076  // Only do this transformation if the int is intptrty in size, otherwise
1077  // there is a truncation or extension that we aren't modeling.
1078  if (CE0->getOpcode() == Instruction::PtrToInt) {
1079  Type *IntPtrTy = TD->getIntPtrType(CE0->getOperand(0)->getType());
1080  if (CE0->getType() == IntPtrTy) {
1081  Constant *C = CE0->getOperand(0);
1082  Constant *Null = Constant::getNullValue(C->getType());
1083  return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
1084  }
1085  }
1086  }
1087 
1088  if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
1089  if (TD && CE0->getOpcode() == CE1->getOpcode()) {
1090  if (CE0->getOpcode() == Instruction::IntToPtr) {
1091  Type *IntPtrTy = TD->getIntPtrType(CE0->getType());
1092 
1093  // Convert the integer value to the right size to ensure we get the
1094  // proper extension or truncation.
1095  Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
1096  IntPtrTy, false);
1097  Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
1098  IntPtrTy, false);
1099  return ConstantFoldCompareInstOperands(Predicate, C0, C1, TD, TLI);
1100  }
1101 
1102  // Only do this transformation if the int is intptrty in size, otherwise
1103  // there is a truncation or extension that we aren't modeling.
1104  if (CE0->getOpcode() == Instruction::PtrToInt) {
1105  Type *IntPtrTy = TD->getIntPtrType(CE0->getOperand(0)->getType());
1106  if (CE0->getType() == IntPtrTy &&
1107  CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1108  return ConstantFoldCompareInstOperands(Predicate,
1109  CE0->getOperand(0),
1110  CE1->getOperand(0),
1111  TD,
1112  TLI);
1113  }
1114  }
1115  }
1116  }
1117 
1118  // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
1119  // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
1120  if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
1121  CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
1122  Constant *LHS =
1123  ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0), Ops1,
1124  TD, TLI);
1125  Constant *RHS =
1126  ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(1), Ops1,
1127  TD, TLI);
1128  unsigned OpC =
1130  Constant *Ops[] = { LHS, RHS };
1131  return ConstantFoldInstOperands(OpC, LHS->getType(), Ops, TD, TLI);
1132  }
1133  }
1134 
1135  return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
1136 }
1137 
1138 
1139 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
1140 /// getelementptr constantexpr, return the constant value being addressed by the
1141 /// constant expression, or null if something is funny and we can't decide.
1143  ConstantExpr *CE) {
1144  if (!CE->getOperand(1)->isNullValue())
1145  return 0; // Do not allow stepping over the value!
1146 
1147  // Loop over all of the operands, tracking down which value we are
1148  // addressing.
1149  for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) {
1150  C = C->getAggregateElement(CE->getOperand(i));
1151  if (C == 0)
1152  return 0;
1153  }
1154  return C;
1155 }
1156 
1157 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
1158 /// indices (with an *implied* zero pointer index that is not in the list),
1159 /// return the constant value being addressed by a virtual load, or null if
1160 /// something is funny and we can't decide.
1162  ArrayRef<Constant*> Indices) {
1163  // Loop over all of the operands, tracking down which value we are
1164  // addressing.
1165  for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1166  C = C->getAggregateElement(Indices[i]);
1167  if (C == 0)
1168  return 0;
1169  }
1170  return C;
1171 }
1172 
1173 
1174 //===----------------------------------------------------------------------===//
1175 // Constant Folding for Calls
1176 //
1177 
1178 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
1179 /// the specified function.
1181  switch (F->getIntrinsicID()) {
1182  case Intrinsic::fabs:
1183  case Intrinsic::log:
1184  case Intrinsic::log2:
1185  case Intrinsic::log10:
1186  case Intrinsic::exp:
1187  case Intrinsic::exp2:
1188  case Intrinsic::floor:
1189  case Intrinsic::sqrt:
1190  case Intrinsic::pow:
1191  case Intrinsic::powi:
1192  case Intrinsic::bswap:
1193  case Intrinsic::ctpop:
1194  case Intrinsic::ctlz:
1195  case Intrinsic::cttz:
1212  return true;
1213  default:
1214  return false;
1215  case 0: break;
1216  }
1217 
1218  if (!F->hasName())
1219  return false;
1220  StringRef Name = F->getName();
1221 
1222  // In these cases, the check of the length is required. We don't want to
1223  // return true for a name like "cos\0blah" which strcmp would return equal to
1224  // "cos", but has length 8.
1225  switch (Name[0]) {
1226  default: return false;
1227  case 'a':
1228  return Name == "acos" || Name == "asin" || Name == "atan" || Name =="atan2";
1229  case 'c':
1230  return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh";
1231  case 'e':
1232  return Name == "exp" || Name == "exp2";
1233  case 'f':
1234  return Name == "fabs" || Name == "fmod" || Name == "floor";
1235  case 'l':
1236  return Name == "log" || Name == "log10";
1237  case 'p':
1238  return Name == "pow";
1239  case 's':
1240  return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
1241  Name == "sinf" || Name == "sqrtf";
1242  case 't':
1243  return Name == "tan" || Name == "tanh";
1244  }
1245 }
1246 
1247 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
1248  Type *Ty) {
1250  V = NativeFP(V);
1251  if (sys::llvm_fenv_testexcept()) {
1253  return 0;
1254  }
1255 
1256  if (Ty->isHalfTy()) {
1257  APFloat APF(V);
1258  bool unused;
1260  return ConstantFP::get(Ty->getContext(), APF);
1261  }
1262  if (Ty->isFloatTy())
1263  return ConstantFP::get(Ty->getContext(), APFloat((float)V));
1264  if (Ty->isDoubleTy())
1265  return ConstantFP::get(Ty->getContext(), APFloat(V));
1266  llvm_unreachable("Can only constant fold half/float/double");
1267 }
1268 
1269 static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
1270  double V, double W, Type *Ty) {
1272  V = NativeFP(V, W);
1273  if (sys::llvm_fenv_testexcept()) {
1275  return 0;
1276  }
1277 
1278  if (Ty->isHalfTy()) {
1279  APFloat APF(V);
1280  bool unused;
1282  return ConstantFP::get(Ty->getContext(), APF);
1283  }
1284  if (Ty->isFloatTy())
1285  return ConstantFP::get(Ty->getContext(), APFloat((float)V));
1286  if (Ty->isDoubleTy())
1287  return ConstantFP::get(Ty->getContext(), APFloat(V));
1288  llvm_unreachable("Can only constant fold half/float/double");
1289 }
1290 
1291 /// ConstantFoldConvertToInt - Attempt to an SSE floating point to integer
1292 /// conversion of a constant floating point. If roundTowardZero is false, the
1293 /// default IEEE rounding is used (toward nearest, ties to even). This matches
1294 /// the behavior of the non-truncating SSE instructions in the default rounding
1295 /// mode. The desired integer type Ty is used to select how many bits are
1296 /// available for the result. Returns null if the conversion cannot be
1297 /// performed, otherwise returns the Constant value resulting from the
1298 /// conversion.
1300  bool roundTowardZero, Type *Ty) {
1301  // All of these conversion intrinsics form an integer of at most 64bits.
1302  unsigned ResultWidth = Ty->getIntegerBitWidth();
1303  assert(ResultWidth <= 64 &&
1304  "Can only constant fold conversions to 64 and 32 bit ints");
1305 
1306  uint64_t UIntVal;
1307  bool isExact = false;
1308  APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
1310  APFloat::opStatus status = Val.convertToInteger(&UIntVal, ResultWidth,
1311  /*isSigned=*/true, mode,
1312  &isExact);
1313  if (status != APFloat::opOK && status != APFloat::opInexact)
1314  return 0;
1315  return ConstantInt::get(Ty, UIntVal, /*isSigned=*/true);
1316 }
1317 
1318 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
1319 /// with the specified arguments, returning null if unsuccessful.
1320 Constant *
1322  const TargetLibraryInfo *TLI) {
1323  if (!F->hasName())
1324  return 0;
1325  StringRef Name = F->getName();
1326 
1327  Type *Ty = F->getReturnType();
1328  if (Operands.size() == 1) {
1329  if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
1331  APFloat Val(Op->getValueAPF());
1332 
1333  bool lost = false;
1335 
1336  return ConstantInt::get(F->getContext(), Val.bitcastToAPInt());
1337  }
1338  if (!TLI)
1339  return 0;
1340 
1341  if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1342  return 0;
1343 
1344  /// We only fold functions with finite arguments. Folding NaN and inf is
1345  /// likely to be aborted with an exception anyway, and some host libms
1346  /// have known errors raising exceptions.
1347  if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity())
1348  return 0;
1349 
1350  /// Currently APFloat versions of these functions do not exist, so we use
1351  /// the host native double versions. Float versions are not called
1352  /// directly but for all these it is true (float)(f((double)arg)) ==
1353  /// f(arg). Long double not supported yet.
1354  double V;
1355  if (Ty->isFloatTy())
1356  V = Op->getValueAPF().convertToFloat();
1357  else if (Ty->isDoubleTy())
1358  V = Op->getValueAPF().convertToDouble();
1359  else {
1360  bool unused;
1361  APFloat APF = Op->getValueAPF();
1363  V = APF.convertToDouble();
1364  }
1365 
1366  switch (F->getIntrinsicID()) {
1367  default: break;
1368  case Intrinsic::fabs:
1369  return ConstantFoldFP(fabs, V, Ty);
1370 #if HAVE_LOG2
1371  case Intrinsic::log2:
1372  return ConstantFoldFP(log2, V, Ty);
1373 #endif
1374 #if HAVE_LOG
1375  case Intrinsic::log:
1376  return ConstantFoldFP(log, V, Ty);
1377 #endif
1378 #if HAVE_LOG10
1379  case Intrinsic::log10:
1380  return ConstantFoldFP(log10, V, Ty);
1381 #endif
1382 #if HAVE_EXP
1383  case Intrinsic::exp:
1384  return ConstantFoldFP(exp, V, Ty);
1385 #endif
1386 #if HAVE_EXP2
1387  case Intrinsic::exp2:
1388  return ConstantFoldFP(exp2, V, Ty);
1389 #endif
1390  case Intrinsic::floor:
1391  return ConstantFoldFP(floor, V, Ty);
1392  }
1393 
1394  switch (Name[0]) {
1395  case 'a':
1396  if (Name == "acos" && TLI->has(LibFunc::acos))
1397  return ConstantFoldFP(acos, V, Ty);
1398  else if (Name == "asin" && TLI->has(LibFunc::asin))
1399  return ConstantFoldFP(asin, V, Ty);
1400  else if (Name == "atan" && TLI->has(LibFunc::atan))
1401  return ConstantFoldFP(atan, V, Ty);
1402  break;
1403  case 'c':
1404  if (Name == "ceil" && TLI->has(LibFunc::ceil))
1405  return ConstantFoldFP(ceil, V, Ty);
1406  else if (Name == "cos" && TLI->has(LibFunc::cos))
1407  return ConstantFoldFP(cos, V, Ty);
1408  else if (Name == "cosh" && TLI->has(LibFunc::cosh))
1409  return ConstantFoldFP(cosh, V, Ty);
1410  else if (Name == "cosf" && TLI->has(LibFunc::cosf))
1411  return ConstantFoldFP(cos, V, Ty);
1412  break;
1413  case 'e':
1414  if (Name == "exp" && TLI->has(LibFunc::exp))
1415  return ConstantFoldFP(exp, V, Ty);
1416 
1417  if (Name == "exp2" && TLI->has(LibFunc::exp2)) {
1418  // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
1419  // C99 library.
1420  return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
1421  }
1422  break;
1423  case 'f':
1424  if (Name == "fabs" && TLI->has(LibFunc::fabs))
1425  return ConstantFoldFP(fabs, V, Ty);
1426  else if (Name == "floor" && TLI->has(LibFunc::floor))
1427  return ConstantFoldFP(floor, V, Ty);
1428  break;
1429  case 'l':
1430  if (Name == "log" && V > 0 && TLI->has(LibFunc::log))
1431  return ConstantFoldFP(log, V, Ty);
1432  else if (Name == "log10" && V > 0 && TLI->has(LibFunc::log10))
1433  return ConstantFoldFP(log10, V, Ty);
1434  else if (F->getIntrinsicID() == Intrinsic::sqrt &&
1435  (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) {
1436  if (V >= -0.0)
1437  return ConstantFoldFP(sqrt, V, Ty);
1438  else // Undefined
1439  return Constant::getNullValue(Ty);
1440  }
1441  break;
1442  case 's':
1443  if (Name == "sin" && TLI->has(LibFunc::sin))
1444  return ConstantFoldFP(sin, V, Ty);
1445  else if (Name == "sinh" && TLI->has(LibFunc::sinh))
1446  return ConstantFoldFP(sinh, V, Ty);
1447  else if (Name == "sqrt" && V >= 0 && TLI->has(LibFunc::sqrt))
1448  return ConstantFoldFP(sqrt, V, Ty);
1449  else if (Name == "sqrtf" && V >= 0 && TLI->has(LibFunc::sqrtf))
1450  return ConstantFoldFP(sqrt, V, Ty);
1451  else if (Name == "sinf" && TLI->has(LibFunc::sinf))
1452  return ConstantFoldFP(sin, V, Ty);
1453  break;
1454  case 't':
1455  if (Name == "tan" && TLI->has(LibFunc::tan))
1456  return ConstantFoldFP(tan, V, Ty);
1457  else if (Name == "tanh" && TLI->has(LibFunc::tanh))
1458  return ConstantFoldFP(tanh, V, Ty);
1459  break;
1460  default:
1461  break;
1462  }
1463  return 0;
1464  }
1465 
1466  if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
1467  switch (F->getIntrinsicID()) {
1468  case Intrinsic::bswap:
1469  return ConstantInt::get(F->getContext(), Op->getValue().byteSwap());
1470  case Intrinsic::ctpop:
1471  return ConstantInt::get(Ty, Op->getValue().countPopulation());
1473  APFloat Val(APFloat::IEEEhalf, Op->getValue());
1474 
1475  bool lost = false;
1478 
1479  // Conversion is always precise.
1480  (void)status;
1481  assert(status == APFloat::opOK && !lost &&
1482  "Precision lost during fp16 constfolding");
1483 
1484  return ConstantFP::get(F->getContext(), Val);
1485  }
1486  default:
1487  return 0;
1488  }
1489  }
1490 
1491  // Support ConstantVector in case we have an Undef in the top.
1492  if (isa<ConstantVector>(Operands[0]) ||
1493  isa<ConstantDataVector>(Operands[0])) {
1494  Constant *Op = cast<Constant>(Operands[0]);
1495  switch (F->getIntrinsicID()) {
1496  default: break;
1501  if (ConstantFP *FPOp =
1502  dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
1503  return ConstantFoldConvertToInt(FPOp->getValueAPF(),
1504  /*roundTowardZero=*/false, Ty);
1509  if (ConstantFP *FPOp =
1510  dyn_cast_or_null<ConstantFP>(Op->getAggregateElement(0U)))
1511  return ConstantFoldConvertToInt(FPOp->getValueAPF(),
1512  /*roundTowardZero=*/true, Ty);
1513  }
1514  }
1515 
1516  if (isa<UndefValue>(Operands[0])) {
1517  if (F->getIntrinsicID() == Intrinsic::bswap)
1518  return Operands[0];
1519  return 0;
1520  }
1521 
1522  return 0;
1523  }
1524 
1525  if (Operands.size() == 2) {
1526  if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
1527  if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
1528  return 0;
1529  double Op1V;
1530  if (Ty->isFloatTy())
1531  Op1V = Op1->getValueAPF().convertToFloat();
1532  else if (Ty->isDoubleTy())
1533  Op1V = Op1->getValueAPF().convertToDouble();
1534  else {
1535  bool unused;
1536  APFloat APF = Op1->getValueAPF();
1538  Op1V = APF.convertToDouble();
1539  }
1540 
1541  if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
1542  if (Op2->getType() != Op1->getType())
1543  return 0;
1544 
1545  double Op2V;
1546  if (Ty->isFloatTy())
1547  Op2V = Op2->getValueAPF().convertToFloat();
1548  else if (Ty->isDoubleTy())
1549  Op2V = Op2->getValueAPF().convertToDouble();
1550  else {
1551  bool unused;
1552  APFloat APF = Op2->getValueAPF();
1554  Op2V = APF.convertToDouble();
1555  }
1556 
1557  if (F->getIntrinsicID() == Intrinsic::pow) {
1558  return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
1559  }
1560  if (!TLI)
1561  return 0;
1562  if (Name == "pow" && TLI->has(LibFunc::pow))
1563  return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
1564  if (Name == "fmod" && TLI->has(LibFunc::fmod))
1565  return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
1566  if (Name == "atan2" && TLI->has(LibFunc::atan2))
1567  return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
1568  } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
1569  if (F->getIntrinsicID() == Intrinsic::powi && Ty->isHalfTy())
1570  return ConstantFP::get(F->getContext(),
1571  APFloat((float)std::pow((float)Op1V,
1572  (int)Op2C->getZExtValue())));
1573  if (F->getIntrinsicID() == Intrinsic::powi && Ty->isFloatTy())
1574  return ConstantFP::get(F->getContext(),
1575  APFloat((float)std::pow((float)Op1V,
1576  (int)Op2C->getZExtValue())));
1577  if (F->getIntrinsicID() == Intrinsic::powi && Ty->isDoubleTy())
1578  return ConstantFP::get(F->getContext(),
1579  APFloat((double)std::pow((double)Op1V,
1580  (int)Op2C->getZExtValue())));
1581  }
1582  return 0;
1583  }
1584 
1585  if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
1586  if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
1587  switch (F->getIntrinsicID()) {
1588  default: break;
1595  APInt Res;
1596  bool Overflow;
1597  switch (F->getIntrinsicID()) {
1598  default: llvm_unreachable("Invalid case");
1600  Res = Op1->getValue().sadd_ov(Op2->getValue(), Overflow);
1601  break;
1603  Res = Op1->getValue().uadd_ov(Op2->getValue(), Overflow);
1604  break;
1606  Res = Op1->getValue().ssub_ov(Op2->getValue(), Overflow);
1607  break;
1609  Res = Op1->getValue().usub_ov(Op2->getValue(), Overflow);
1610  break;
1612  Res = Op1->getValue().smul_ov(Op2->getValue(), Overflow);
1613  break;
1615  Res = Op1->getValue().umul_ov(Op2->getValue(), Overflow);
1616  break;
1617  }
1618  Constant *Ops[] = {
1619  ConstantInt::get(F->getContext(), Res),
1621  };
1622  return ConstantStruct::get(cast<StructType>(F->getReturnType()), Ops);
1623  }
1624  case Intrinsic::cttz:
1625  if (Op2->isOne() && Op1->isZero()) // cttz(0, 1) is undef.
1626  return UndefValue::get(Ty);
1627  return ConstantInt::get(Ty, Op1->getValue().countTrailingZeros());
1628  case Intrinsic::ctlz:
1629  if (Op2->isOne() && Op1->isZero()) // ctlz(0, 1) is undef.
1630  return UndefValue::get(Ty);
1631  return ConstantInt::get(Ty, Op1->getValue().countLeadingZeros());
1632  }
1633  }
1634 
1635  return 0;
1636  }
1637  return 0;
1638  }
1639  return 0;
1640 }
Abstract base class of comparison instructions.
Definition: InstrTypes.h:633
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:238
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask)
Definition: Constants.cpp:1949
LLVMContext & getContext() const
Definition: Function.cpp:167
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1306
double sinh(double x);
Type * getSequentialElementType() const
Definition: Type.cpp:206
bool hasName() const
Definition: Value.h:117
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
Constant * ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE)
unsigned getScalarSizeInBits()
Definition: Type.cpp:135
static const fltSemantics IEEEdouble
Definition: APFloat.h:133
bool canConstantFoldCallTo(const Function *F)
static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &TD)
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2)
Definition: Constants.cpp:1820
double tanh(double x);
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
unsigned getNumOperands() const
Definition: User.h:108
unsigned getPointerTypeSizeInBits(Type *) const
Definition: DataLayout.cpp:510
static Constant * getGetElementPtr(Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false)
Definition: Constants.h:1004
double cos(double x);
APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:2028
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:528
static PointerType * getInt32PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:292
bool insert(PtrType Ptr)
Definition: SmallPtrSet.h:253
static Constant * getExtractElement(Constant *Vec, Constant *Idx)
Definition: Constants.cpp:1912
static Constant * ConstantFoldConvertToInt(const APFloat &Val, bool roundTowardZero, Type *Ty)
bool isDoubleTy() const
isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:149
Type * getReturnType() const
Definition: Function.cpp:179
F(f)
static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr, unsigned BytesLeft, const DataLayout &TD)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:445
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:242
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:657
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2040
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: Type.cpp:218
static IntegerType * getInt16Ty(LLVMContext &C)
Definition: Type.cpp:240
const Constant * getInitializer() const
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1002
unsigned getOpcode() const
getOpcode - Return the opcode at the root of this constant expression
Definition: Constants.h:1049
op_iterator op_begin()
Definition: User.h:116
static Constant * ConstantFoldBinaryFP(double(*NativeFP)(double, double), double V, double W, Type *Ty)
static PointerType * getInt64PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:296
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:2009
LoopInfoBase< BlockT, LoopT > * LI
Definition: LoopInfoImpl.h:411
Type * getPointerElementType() const
Definition: Type.h:373
uint64_t getTypeAllocSizeInBits(Type *Ty) const
Definition: DataLayout.h:335
float sqrtf(float x);
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:111
StringRef getName() const
Definition: Value.cpp:167
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:322
Value * GetUnderlyingObject(Value *V, const DataLayout *TD=0, unsigned MaxLookup=6)
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1502
const StructLayout * getStructLayout(StructType *Ty) const
Definition: DataLayout.cpp:445
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT,"arm-default-it","Generate IT block based on arch"), clEnumValN(RestrictedIT,"arm-restrict-it","Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT,"arm-no-restrict-it","Allow IT blocks based on ARMv7"), clEnumValEnd))
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0)
Definition: Constants.cpp:1679
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:261
bool has(LibFunc::Func F) const
opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode, bool *) const
Definition: APFloat.cpp:2157
#define llvm_unreachable(msg)
Definition: Use.h:60
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2107
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:923
static bool llvm_fenv_testexcept()
llvm_fenv_testexcept - Test if a floating-point exception was raised.
Definition: FEnv.h:42
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs)
Definition: Constants.cpp:1989
double log10(double x);
static PointerType * getInt16PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:288
uint64_t getIndexedOffset(Type *Ty, ArrayRef< Value * > Indices) const
Definition: DataLayout.cpp:639
static ConstantInt * ExtractElement(Constant *V, Constant *Idx)
Type * getVectorElementType() const
Definition: Type.h:371
static Constant * StripPtrCastKeepAS(Constant *Ptr)
Strip the pointer casts, but preserve the address space information.
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
Definition: Operator.h:444
APInt usub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:2016
bool isHalfTy() const
isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:143
ArrayRef< T > slice(unsigned N) const
slice(n) - Chop off the first N elements of the array.
Definition: ArrayRef.h:134
Constant * ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
static Constant * getPtrToInt(Constant *C, Type *Ty)
Definition: Constants.cpp:1637
double sqrt(double x);
double fmod(double x, double y);
double convertToDouble() const
Definition: APFloat.cpp:3082
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
bool isFloatingPointTy() const
Definition: Type.h:162
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:195
bool hasDefinitiveInitializer() const
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:2038
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:408
static Constant * getIntToPtr(Constant *C, Type *Ty)
Definition: Constants.cpp:1649
Type * getElementType() const
Definition: DerivedTypes.h:319
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:109
void ComputeMaskedBits(Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout *TD=0, unsigned Depth=0)
static Constant * ConstantFoldLoadInst(const LoadInst *LI, const DataLayout *TD)
Constant * ConstantFoldCall(Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=0)
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:442
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition: APFloat.h:122
bool isX86_MMXTy() const
isX86_MMXTy - Return true if this is X86 MMX.
Definition: Type.h:182
error_code status(const Twine &path, file_status &result)
Get file status as if by POSIX stat().
static Constant * SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1, const DataLayout *DL)
unsigned getIntrinsicID() const LLVM_READONLY
Definition: Function.cpp:371
double cosh(double x);
LLVM Constant Representation.
Definition: Constant.h:41
bool isFloatTy() const
isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:146
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2088
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1845
float cosf(float x);
static Constant * CastGEPIndices(ArrayRef< Constant * > Ops, Type *ResultTy, const DataLayout *TD, const TargetLibraryInfo *TLI)
op_iterator op_end()
Definition: User.h:118
static Constant * ConstantFoldFP(double(*NativeFP)(double), double V, Type *Ty)
opStatus convert(const fltSemantics &, roundingMode, bool *)
Definition: APFloat.cpp:1938
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1116
double log(double x);
double pow(double x, double y);
static Constant * FoldReinterpretLoadFromConstPtr(Constant *C, const DataLayout &TD)
Value * getOperand(unsigned i) const
Definition: User.h:88
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
Integer representation type.
Definition: DerivedTypes.h:37
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:874
Constant * ConstantFoldLoadFromConstPtr(Constant *C, const DataLayout *TD=0)
Constant * getAggregateElement(unsigned Elt) const
Definition: Constants.cpp:183
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
Definition: Constants.cpp:163
unsigned getPredicate() const
Definition: Constants.cpp:1082
uint64_t getElementAsInteger(unsigned i) const
Definition: Constants.cpp:2443
bool isPointerTy() const
Definition: Type.h:220
static UndefValue * get(Type *T)
Definition: Constants.cpp:1334
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:517
double atan(double x);
PointerType * getPointerTo(unsigned AddrSpace=0)
Definition: Type.cpp:756
double exp(double x);
static const fltSemantics IEEEhalf
Definition: APFloat.h:131
const T & back() const
back - Get the last element.
Definition: ArrayRef.h:118
static Constant * SymbolicallyEvaluateGEP(ArrayRef< Constant * > Ops, Type *ResultTy, const DataLayout *TD, const TargetLibraryInfo *TLI)
bool isCompare() const
Return true if this is a compare constant expression.
Definition: Constants.cpp:1040
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Definition: DataLayout.cpp:610
double acos(double x);
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast or a PtrToInt cast constant expression.
Definition: Constants.cpp:1487
static IntegerType * get(LLVMContext &C, unsigned NumBits)
Get or create an IntegerType instance.
Definition: Type.cpp:305
static Constant * getBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1661
roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
Definition: APFloat.h:155
static void llvm_fenv_clearexcept()
llvm_fenv_clearexcept - Clear the floating-point exception state.
Definition: FEnv.h:34
unsigned getIntegerBitWidth() const
Definition: Type.cpp:178
Class for constant integers.
Definition: Constants.h:51
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:326
unsigned getVectorNumElements() const
Definition: Type.cpp:214
static Constant * ConstantFoldConstantExpressionImpl(const ConstantExpr *CE, const DataLayout *TD, const TargetLibraryInfo *TLI, SmallPtrSet< ConstantExpr *, 4 > &FoldedOps)
Type * getType() const
Definition: Value.h:111
bool isVolatile() const
Definition: Instructions.h:170
uint64_t getSizeInBytes() const
Definition: DataLayout.h:425
unsigned getElementContainingOffset(uint64_t Offset) const
Definition: DataLayout.cpp:78
double asin(double x);
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:492
double fabs(double x);
static Constant * getTrunc(Constant *C, Type *Ty)
Definition: Constants.cpp:1527
static Constant * get(Type *Ty, double V)
Definition: Constants.cpp:557
bool isNullValue() const
Definition: Constants.cpp:75
bool isAllOnesValue() const
Definition: Constants.cpp:88
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isConstant() const
bool isIntegerTy() const
Definition: Type.h:196
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:2003
double sin(double x);
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1840
PointerType * getType() const
getType - Global values are always pointers.
Definition: GlobalValue.h:107
static const fltSemantics IEEEsingle
Definition: APFloat.h:132
double ceil(double x);
APInt LLVM_ATTRIBUTE_UNUSED_RESULT udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1842
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:241
static Constant * getZExt(Constant *C, Type *Ty)
Definition: Constants.cpp:1555
double floor(double x);
bool isBinaryOp() const
Definition: Instruction.h:87
static Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Infer the opcode for cast operand and type.
#define I(x, y, z)
Definition: MD5.cpp:54
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2092
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2100
const Type * getScalarType() const
Definition: Type.cpp:51
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1996
unsigned getPrimitiveSizeInBits() const
Definition: Type.cpp:117
static Constant * getInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
Definition: Constants.cpp:1969
static PointerType * getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS=0)
Definition: Type.cpp:276
bool getConstantStringInfo(const Value *V, StringRef &Str, uint64_t Offset=0, bool TrimAtNul=true)
float sinf(float x);
static Type * getIndexedType(Type *Ptr, ArrayRef< Value * > IdxList)
double atan2(double y, double x);
LLVM Value Representation.
Definition: Value.h:66
static StrLenOpt StrLen
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:83
static VectorType * get(Type *ElementType, unsigned NumElements)
Definition: Type.cpp:706
Constant * ConstantFoldInstOperands(unsigned Opcode, Type *DestTy, ArrayRef< Constant * > Ops, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
bool isSized() const
Definition: Type.h:278
uint64_t getTypeSizeInBits(Type *Ty) const
Definition: DataLayout.h:459
double exp2(double x);
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:1798
Constant * ConstantFoldLoadThroughGEPIndices(Constant *C, ArrayRef< Constant * > Indices)
static Constant * FoldBitCast(Constant *C, Type *DestTy, const DataLayout &TD)
double tan(double x);
static Constant * getCast(unsigned ops, Constant *C, Type *Ty)
Definition: Constants.cpp:1444
const T * data() const
Definition: ArrayRef.h:106
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx)
Definition: Constants.cpp:1930
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110