LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InstCombineCalls.cpp
Go to the documentation of this file.
1 //===- InstCombineCalls.cpp -----------------------------------------------===//
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 implements the visitCall and visitInvoke functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "InstCombine.h"
15 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/Support/CallSite.h"
22 using namespace llvm;
23 using namespace PatternMatch;
24 
25 STATISTIC(NumSimplified, "Number of library calls simplified");
26 
27 /// getPromotedType - Return the specified type promoted as it would be to pass
28 /// though a va_arg area.
29 static Type *getPromotedType(Type *Ty) {
30  if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
31  if (ITy->getBitWidth() < 32)
32  return Type::getInt32Ty(Ty->getContext());
33  }
34  return Ty;
35 }
36 
37 /// reduceToSingleValueType - Given an aggregate type which ultimately holds a
38 /// single scalar element, like {{{type}}} or [1 x type], return type.
40  while (!T->isSingleValueType()) {
41  if (StructType *STy = dyn_cast<StructType>(T)) {
42  if (STy->getNumElements() == 1)
43  T = STy->getElementType(0);
44  else
45  break;
46  } else if (ArrayType *ATy = dyn_cast<ArrayType>(T)) {
47  if (ATy->getNumElements() == 1)
48  T = ATy->getElementType();
49  else
50  break;
51  } else
52  break;
53  }
54 
55  return T;
56 }
57 
58 Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
59  unsigned DstAlign = getKnownAlignment(MI->getArgOperand(0), TD);
60  unsigned SrcAlign = getKnownAlignment(MI->getArgOperand(1), TD);
61  unsigned MinAlign = std::min(DstAlign, SrcAlign);
62  unsigned CopyAlign = MI->getAlignment();
63 
64  if (CopyAlign < MinAlign) {
66  MinAlign, false));
67  return MI;
68  }
69 
70  // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
71  // load/store.
72  ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getArgOperand(2));
73  if (MemOpLength == 0) return 0;
74 
75  // Source and destination pointer types are always "i8*" for intrinsic. See
76  // if the size is something we can handle with a single primitive load/store.
77  // A single load+store correctly handles overlapping memory in the memmove
78  // case.
79  uint64_t Size = MemOpLength->getLimitedValue();
80  assert(Size && "0-sized memory transfering should be removed already.");
81 
82  if (Size > 8 || (Size&(Size-1)))
83  return 0; // If not 1/2/4/8 bytes, exit.
84 
85  // Use an integer load+store unless we can find something better.
86  unsigned SrcAddrSp =
87  cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace();
88  unsigned DstAddrSp =
89  cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace();
90 
91  IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
92  Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp);
93  Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp);
94 
95  // Memcpy forces the use of i8* for the source and destination. That means
96  // that if you're using memcpy to move one double around, you'll get a cast
97  // from double* to i8*. We'd much rather use a double load+store rather than
98  // an i64 load+store, here because this improves the odds that the source or
99  // dest address will be promotable. See if we can find a better type than the
100  // integer datatype.
101  Value *StrippedDest = MI->getArgOperand(0)->stripPointerCasts();
102  MDNode *CopyMD = 0;
103  if (StrippedDest != MI->getArgOperand(0)) {
104  Type *SrcETy = cast<PointerType>(StrippedDest->getType())
105  ->getElementType();
106  if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
107  // The SrcETy might be something like {{{double}}} or [1 x double]. Rip
108  // down through these levels if so.
109  SrcETy = reduceToSingleValueType(SrcETy);
110 
111  if (SrcETy->isSingleValueType()) {
112  NewSrcPtrTy = PointerType::get(SrcETy, SrcAddrSp);
113  NewDstPtrTy = PointerType::get(SrcETy, DstAddrSp);
114 
115  // If the memcpy has metadata describing the members, see if we can
116  // get the TBAA tag describing our copy.
118  if (M->getNumOperands() == 3 &&
119  M->getOperand(0) &&
120  isa<ConstantInt>(M->getOperand(0)) &&
121  cast<ConstantInt>(M->getOperand(0))->isNullValue() &&
122  M->getOperand(1) &&
123  isa<ConstantInt>(M->getOperand(1)) &&
124  cast<ConstantInt>(M->getOperand(1))->getValue() == Size &&
125  M->getOperand(2) &&
126  isa<MDNode>(M->getOperand(2)))
127  CopyMD = cast<MDNode>(M->getOperand(2));
128  }
129  }
130  }
131  }
132 
133  // If the memcpy/memmove provides better alignment info than we can
134  // infer, use it.
135  SrcAlign = std::max(SrcAlign, CopyAlign);
136  DstAlign = std::max(DstAlign, CopyAlign);
137 
138  Value *Src = Builder->CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy);
139  Value *Dest = Builder->CreateBitCast(MI->getArgOperand(0), NewDstPtrTy);
140  LoadInst *L = Builder->CreateLoad(Src, MI->isVolatile());
141  L->setAlignment(SrcAlign);
142  if (CopyMD)
143  L->setMetadata(LLVMContext::MD_tbaa, CopyMD);
144  StoreInst *S = Builder->CreateStore(L, Dest, MI->isVolatile());
145  S->setAlignment(DstAlign);
146  if (CopyMD)
147  S->setMetadata(LLVMContext::MD_tbaa, CopyMD);
148 
149  // Set the size of the copy to 0, it will be deleted on the next iteration.
150  MI->setArgOperand(2, Constant::getNullValue(MemOpLength->getType()));
151  return MI;
152 }
153 
154 Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
155  unsigned Alignment = getKnownAlignment(MI->getDest(), TD);
156  if (MI->getAlignment() < Alignment) {
158  Alignment, false));
159  return MI;
160  }
161 
162  // Extract the length and alignment and fill if they are constant.
163  ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
164  ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
165  if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8))
166  return 0;
167  uint64_t Len = LenC->getLimitedValue();
168  Alignment = MI->getAlignment();
169  assert(Len && "0-sized memory setting should be removed already.");
170 
171  // memset(s,c,n) -> store s, c (for n=1,2,4,8)
172  if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
173  Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8.
174 
175  Value *Dest = MI->getDest();
176  unsigned DstAddrSp = cast<PointerType>(Dest->getType())->getAddressSpace();
177  Type *NewDstPtrTy = PointerType::get(ITy, DstAddrSp);
178  Dest = Builder->CreateBitCast(Dest, NewDstPtrTy);
179 
180  // Alignment 0 is identity for alignment 1 for memset, but not store.
181  if (Alignment == 0) Alignment = 1;
182 
183  // Extract the fill value and store.
184  uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
185  StoreInst *S = Builder->CreateStore(ConstantInt::get(ITy, Fill), Dest,
186  MI->isVolatile());
187  S->setAlignment(Alignment);
188 
189  // Set the size of the copy to 0, it will be deleted on the next iteration.
191  return MI;
192  }
193 
194  return 0;
195 }
196 
197 /// visitCallInst - CallInst simplification. This mostly only handles folding
198 /// of intrinsic instructions. For normal calls, it allows visitCallSite to do
199 /// the heavy lifting.
200 ///
202  if (isFreeCall(&CI, TLI))
203  return visitFree(CI);
204 
205  // If the caller function is nounwind, mark the call as nounwind, even if the
206  // callee isn't.
207  if (CI.getParent()->getParent()->doesNotThrow() &&
208  !CI.doesNotThrow()) {
209  CI.setDoesNotThrow();
210  return &CI;
211  }
212 
214  if (!II) return visitCallSite(&CI);
215 
216  // Intrinsics cannot occur in an invoke, so handle them here instead of in
217  // visitCallSite.
218  if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
219  bool Changed = false;
220 
221  // memmove/cpy/set of zero bytes is a noop.
222  if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
223  if (NumBytes->isNullValue())
224  return EraseInstFromFunction(CI);
225 
226  if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
227  if (CI->getZExtValue() == 1) {
228  // Replace the instruction with just byte operations. We would
229  // transform other cases to loads/stores, but we don't know if
230  // alignment is sufficient.
231  }
232  }
233 
234  // No other transformations apply to volatile transfers.
235  if (MI->isVolatile())
236  return 0;
237 
238  // If we have a memmove and the source operation is a constant global,
239  // then the source and dest pointers can't alias, so we can change this
240  // into a call to memcpy.
241  if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
242  if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
243  if (GVSrc->isConstant()) {
244  Module *M = CI.getParent()->getParent()->getParent();
245  Intrinsic::ID MemCpyID = Intrinsic::memcpy;
246  Type *Tys[3] = { CI.getArgOperand(0)->getType(),
247  CI.getArgOperand(1)->getType(),
248  CI.getArgOperand(2)->getType() };
249  CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys));
250  Changed = true;
251  }
252  }
253 
254  if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
255  // memmove(x,x,size) -> noop.
256  if (MTI->getSource() == MTI->getDest())
257  return EraseInstFromFunction(CI);
258  }
259 
260  // If we can determine a pointer alignment that is bigger than currently
261  // set, update the alignment.
262  if (isa<MemTransferInst>(MI)) {
263  if (Instruction *I = SimplifyMemTransfer(MI))
264  return I;
265  } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
266  if (Instruction *I = SimplifyMemSet(MSI))
267  return I;
268  }
269 
270  if (Changed) return II;
271  }
272 
273  switch (II->getIntrinsicID()) {
274  default: break;
275  case Intrinsic::objectsize: {
276  uint64_t Size;
277  if (getObjectSize(II->getArgOperand(0), Size, TD, TLI))
278  return ReplaceInstUsesWith(CI, ConstantInt::get(CI.getType(), Size));
279  return 0;
280  }
281  case Intrinsic::bswap: {
282  Value *IIOperand = II->getArgOperand(0);
283  Value *X = 0;
284 
285  // bswap(bswap(x)) -> x
286  if (match(IIOperand, m_BSwap(m_Value(X))))
287  return ReplaceInstUsesWith(CI, X);
288 
289  // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
290  if (match(IIOperand, m_Trunc(m_BSwap(m_Value(X))))) {
291  unsigned C = X->getType()->getPrimitiveSizeInBits() -
292  IIOperand->getType()->getPrimitiveSizeInBits();
293  Value *CV = ConstantInt::get(X->getType(), C);
294  Value *V = Builder->CreateLShr(X, CV);
295  return new TruncInst(V, IIOperand->getType());
296  }
297  break;
298  }
299 
300  case Intrinsic::powi:
301  if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
302  // powi(x, 0) -> 1.0
303  if (Power->isZero())
304  return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
305  // powi(x, 1) -> x
306  if (Power->isOne())
307  return ReplaceInstUsesWith(CI, II->getArgOperand(0));
308  // powi(x, -1) -> 1/x
309  if (Power->isAllOnesValue())
310  return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
311  II->getArgOperand(0));
312  }
313  break;
314  case Intrinsic::cttz: {
315  // If all bits below the first known one are known zero,
316  // this value is constant.
318  // FIXME: Try to simplify vectors of integers.
319  if (!IT) break;
320  uint32_t BitWidth = IT->getBitWidth();
321  APInt KnownZero(BitWidth, 0);
322  APInt KnownOne(BitWidth, 0);
323  ComputeMaskedBits(II->getArgOperand(0), KnownZero, KnownOne);
324  unsigned TrailingZeros = KnownOne.countTrailingZeros();
325  APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros));
326  if ((Mask & KnownZero) == Mask)
327  return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
328  APInt(BitWidth, TrailingZeros)));
329 
330  }
331  break;
332  case Intrinsic::ctlz: {
333  // If all bits above the first known one are known zero,
334  // this value is constant.
336  // FIXME: Try to simplify vectors of integers.
337  if (!IT) break;
338  uint32_t BitWidth = IT->getBitWidth();
339  APInt KnownZero(BitWidth, 0);
340  APInt KnownOne(BitWidth, 0);
341  ComputeMaskedBits(II->getArgOperand(0), KnownZero, KnownOne);
342  unsigned LeadingZeros = KnownOne.countLeadingZeros();
343  APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros));
344  if ((Mask & KnownZero) == Mask)
345  return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
346  APInt(BitWidth, LeadingZeros)));
347 
348  }
349  break;
351  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
352  IntegerType *IT = cast<IntegerType>(II->getArgOperand(0)->getType());
353  uint32_t BitWidth = IT->getBitWidth();
354  APInt LHSKnownZero(BitWidth, 0);
355  APInt LHSKnownOne(BitWidth, 0);
356  ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne);
357  bool LHSKnownNegative = LHSKnownOne[BitWidth - 1];
358  bool LHSKnownPositive = LHSKnownZero[BitWidth - 1];
359 
360  if (LHSKnownNegative || LHSKnownPositive) {
361  APInt RHSKnownZero(BitWidth, 0);
362  APInt RHSKnownOne(BitWidth, 0);
363  ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne);
364  bool RHSKnownNegative = RHSKnownOne[BitWidth - 1];
365  bool RHSKnownPositive = RHSKnownZero[BitWidth - 1];
366  if (LHSKnownNegative && RHSKnownNegative) {
367  // The sign bit is set in both cases: this MUST overflow.
368  // Create a simple add instruction, and insert it into the struct.
369  Value *Add = Builder->CreateAdd(LHS, RHS);
370  Add->takeName(&CI);
371  Constant *V[] = {
372  UndefValue::get(LHS->getType()),
374  };
375  StructType *ST = cast<StructType>(II->getType());
376  Constant *Struct = ConstantStruct::get(ST, V);
377  return InsertValueInst::Create(Struct, Add, 0);
378  }
379 
380  if (LHSKnownPositive && RHSKnownPositive) {
381  // The sign bit is clear in both cases: this CANNOT overflow.
382  // Create a simple add instruction, and insert it into the struct.
383  Value *Add = Builder->CreateNUWAdd(LHS, RHS);
384  Add->takeName(&CI);
385  Constant *V[] = {
386  UndefValue::get(LHS->getType()),
388  };
389  StructType *ST = cast<StructType>(II->getType());
390  Constant *Struct = ConstantStruct::get(ST, V);
391  return InsertValueInst::Create(Struct, Add, 0);
392  }
393  }
394  }
395  // FALL THROUGH uadd into sadd
397  // Canonicalize constants into the RHS.
398  if (isa<Constant>(II->getArgOperand(0)) &&
399  !isa<Constant>(II->getArgOperand(1))) {
400  Value *LHS = II->getArgOperand(0);
401  II->setArgOperand(0, II->getArgOperand(1));
402  II->setArgOperand(1, LHS);
403  return II;
404  }
405 
406  // X + undef -> undef
407  if (isa<UndefValue>(II->getArgOperand(1)))
408  return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
409 
410  if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
411  // X + 0 -> {X, false}
412  if (RHS->isZero()) {
413  Constant *V[] = {
416  };
417  Constant *Struct =
418  ConstantStruct::get(cast<StructType>(II->getType()), V);
419  return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
420  }
421  }
422  break;
425  // undef - X -> undef
426  // X - undef -> undef
427  if (isa<UndefValue>(II->getArgOperand(0)) ||
428  isa<UndefValue>(II->getArgOperand(1)))
429  return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
430 
431  if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
432  // X - 0 -> {X, false}
433  if (RHS->isZero()) {
434  Constant *V[] = {
437  };
438  Constant *Struct =
439  ConstantStruct::get(cast<StructType>(II->getType()), V);
440  return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
441  }
442  }
443  break;
445  Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
446  unsigned BitWidth = cast<IntegerType>(LHS->getType())->getBitWidth();
447 
448  APInt LHSKnownZero(BitWidth, 0);
449  APInt LHSKnownOne(BitWidth, 0);
450  ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne);
451  APInt RHSKnownZero(BitWidth, 0);
452  APInt RHSKnownOne(BitWidth, 0);
453  ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne);
454 
455  // Get the largest possible values for each operand.
456  APInt LHSMax = ~LHSKnownZero;
457  APInt RHSMax = ~RHSKnownZero;
458 
459  // If multiplying the maximum values does not overflow then we can turn
460  // this into a plain NUW mul.
461  bool Overflow;
462  LHSMax.umul_ov(RHSMax, Overflow);
463  if (!Overflow) {
464  Value *Mul = Builder->CreateNUWMul(LHS, RHS, "umul_with_overflow");
465  Constant *V[] = {
466  UndefValue::get(LHS->getType()),
467  Builder->getFalse()
468  };
469  Constant *Struct = ConstantStruct::get(cast<StructType>(II->getType()),V);
470  return InsertValueInst::Create(Struct, Mul, 0);
471  }
472  } // FALL THROUGH
474  // Canonicalize constants into the RHS.
475  if (isa<Constant>(II->getArgOperand(0)) &&
476  !isa<Constant>(II->getArgOperand(1))) {
477  Value *LHS = II->getArgOperand(0);
478  II->setArgOperand(0, II->getArgOperand(1));
479  II->setArgOperand(1, LHS);
480  return II;
481  }
482 
483  // X * undef -> undef
484  if (isa<UndefValue>(II->getArgOperand(1)))
485  return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
486 
487  if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
488  // X*0 -> {0, false}
489  if (RHSI->isZero())
490  return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType()));
491 
492  // X * 1 -> {X, false}
493  if (RHSI->equalsInt(1)) {
494  Constant *V[] = {
497  };
498  Constant *Struct =
499  ConstantStruct::get(cast<StructType>(II->getType()), V);
500  return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
501  }
502  }
503  break;
506  // Turn PPC lvx -> load if the pointer is known aligned.
507  if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, TD) >= 16) {
508  Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0),
510  return new LoadInst(Ptr);
511  }
512  break;
515  // Turn stvx -> store if the pointer is known aligned.
516  if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, TD) >= 16) {
517  Type *OpPtrTy =
519  Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy);
520  return new StoreInst(II->getArgOperand(0), Ptr);
521  }
522  break;
526  // Turn X86 storeu -> store if the pointer is known aligned.
527  if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, TD) >= 16) {
528  Type *OpPtrTy =
530  Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), OpPtrTy);
531  return new StoreInst(II->getArgOperand(1), Ptr);
532  }
533  break;
534 
543  // These intrinsics only demand the 0th element of their input vectors. If
544  // we can simplify the input based on that, do so now.
545  unsigned VWidth =
546  cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements();
547  APInt DemandedElts(VWidth, 1);
548  APInt UndefElts(VWidth, 0);
549  if (Value *V = SimplifyDemandedVectorElts(II->getArgOperand(0),
550  DemandedElts, UndefElts)) {
551  II->setArgOperand(0, V);
552  return II;
553  }
554  break;
555  }
556 
557 
564  // pmov{s|z}x ignores the upper half of their input vectors.
565  unsigned VWidth =
566  cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements();
567  unsigned LowHalfElts = VWidth / 2;
568  APInt InputDemandedElts(APInt::getBitsSet(VWidth, 0, LowHalfElts));
569  APInt UndefElts(VWidth, 0);
570  if (Value *TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0),
571  InputDemandedElts,
572  UndefElts)) {
573  II->setArgOperand(0, TmpV);
574  return II;
575  }
576  break;
577  }
578 
580  // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
581  if (Constant *Mask = dyn_cast<Constant>(II->getArgOperand(2))) {
582  assert(Mask->getType()->getVectorNumElements() == 16 &&
583  "Bad type for intrinsic!");
584 
585  // Check that all of the elements are integer constants or undefs.
586  bool AllEltsOk = true;
587  for (unsigned i = 0; i != 16; ++i) {
588  Constant *Elt = Mask->getAggregateElement(i);
589  if (Elt == 0 ||
590  !(isa<ConstantInt>(Elt) || isa<UndefValue>(Elt))) {
591  AllEltsOk = false;
592  break;
593  }
594  }
595 
596  if (AllEltsOk) {
597  // Cast the input vectors to byte vectors.
598  Value *Op0 = Builder->CreateBitCast(II->getArgOperand(0),
599  Mask->getType());
600  Value *Op1 = Builder->CreateBitCast(II->getArgOperand(1),
601  Mask->getType());
602  Value *Result = UndefValue::get(Op0->getType());
603 
604  // Only extract each element once.
605  Value *ExtractedElts[32];
606  memset(ExtractedElts, 0, sizeof(ExtractedElts));
607 
608  for (unsigned i = 0; i != 16; ++i) {
609  if (isa<UndefValue>(Mask->getAggregateElement(i)))
610  continue;
611  unsigned Idx =
612  cast<ConstantInt>(Mask->getAggregateElement(i))->getZExtValue();
613  Idx &= 31; // Match the hardware behavior.
614 
615  if (ExtractedElts[Idx] == 0) {
616  ExtractedElts[Idx] =
617  Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1,
618  Builder->getInt32(Idx&15));
619  }
620 
621  // Insert this value into the result vector.
622  Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx],
623  Builder->getInt32(i));
624  }
625  return CastInst::Create(Instruction::BitCast, Result, CI.getType());
626  }
627  }
628  break;
629 
644  unsigned MemAlign = getKnownAlignment(II->getArgOperand(0), TD);
645  unsigned AlignArg = II->getNumArgOperands() - 1;
646  ConstantInt *IntrAlign = dyn_cast<ConstantInt>(II->getArgOperand(AlignArg));
647  if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) {
648  II->setArgOperand(AlignArg,
650  MemAlign, false));
651  return II;
652  }
653  break;
654  }
655 
658  Value *Arg0 = II->getArgOperand(0);
659  Value *Arg1 = II->getArgOperand(1);
660 
661  // Handle mul by zero first:
662  if (isa<ConstantAggregateZero>(Arg0) || isa<ConstantAggregateZero>(Arg1)) {
663  return ReplaceInstUsesWith(CI, ConstantAggregateZero::get(II->getType()));
664  }
665 
666  // Check for constant LHS & RHS - in this case we just simplify.
667  bool Zext = (II->getIntrinsicID() == Intrinsic::arm_neon_vmullu);
668  VectorType *NewVT = cast<VectorType>(II->getType());
669  unsigned NewWidth = NewVT->getElementType()->getIntegerBitWidth();
670  if (ConstantDataVector *CV0 = dyn_cast<ConstantDataVector>(Arg0)) {
671  if (ConstantDataVector *CV1 = dyn_cast<ConstantDataVector>(Arg1)) {
672  VectorType* VT = cast<VectorType>(CV0->getType());
673  SmallVector<Constant*, 4> NewElems;
674  for (unsigned i = 0; i < VT->getNumElements(); ++i) {
675  APInt CV0E =
676  (cast<ConstantInt>(CV0->getAggregateElement(i)))->getValue();
677  CV0E = Zext ? CV0E.zext(NewWidth) : CV0E.sext(NewWidth);
678  APInt CV1E =
679  (cast<ConstantInt>(CV1->getAggregateElement(i)))->getValue();
680  CV1E = Zext ? CV1E.zext(NewWidth) : CV1E.sext(NewWidth);
681  NewElems.push_back(
682  ConstantInt::get(NewVT->getElementType(), CV0E * CV1E));
683  }
684  return ReplaceInstUsesWith(CI, ConstantVector::get(NewElems));
685  }
686 
687  // Couldn't simplify - cannonicalize constant to the RHS.
688  std::swap(Arg0, Arg1);
689  }
690 
691  // Handle mul by one:
692  if (ConstantDataVector *CV1 = dyn_cast<ConstantDataVector>(Arg1)) {
693  if (ConstantInt *Splat =
694  dyn_cast_or_null<ConstantInt>(CV1->getSplatValue())) {
695  if (Splat->isOne()) {
696  if (Zext)
697  return CastInst::CreateZExtOrBitCast(Arg0, II->getType());
698  // else
699  return CastInst::CreateSExtOrBitCast(Arg0, II->getType());
700  }
701  }
702  }
703 
704  break;
705  }
706 
708  // If the save is right next to the restore, remove the restore. This can
709  // happen when variable allocas are DCE'd.
710  if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
711  if (SS->getIntrinsicID() == Intrinsic::stacksave) {
712  BasicBlock::iterator BI = SS;
713  if (&*++BI == II)
714  return EraseInstFromFunction(CI);
715  }
716  }
717 
718  // Scan down this block to see if there is another stack restore in the
719  // same block without an intervening call/alloca.
720  BasicBlock::iterator BI = II;
721  TerminatorInst *TI = II->getParent()->getTerminator();
722  bool CannotRemove = false;
723  for (++BI; &*BI != TI; ++BI) {
724  if (isa<AllocaInst>(BI)) {
725  CannotRemove = true;
726  break;
727  }
728  if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
729  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
730  // If there is a stackrestore below this one, remove this one.
732  return EraseInstFromFunction(CI);
733  // Otherwise, ignore the intrinsic.
734  } else {
735  // If we found a non-intrinsic call, we can't remove the stack
736  // restore.
737  CannotRemove = true;
738  break;
739  }
740  }
741  }
742 
743  // If the stack restore is in a return, resume, or unwind block and if there
744  // are no allocas or calls between the restore and the return, nuke the
745  // restore.
746  if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI)))
747  return EraseInstFromFunction(CI);
748  break;
749  }
750  }
751 
752  return visitCallSite(II);
753 }
754 
755 // InvokeInst simplification
756 //
758  return visitCallSite(&II);
759 }
760 
761 /// isSafeToEliminateVarargsCast - If this cast does not affect the value
762 /// passed through the varargs area, we can eliminate the use of the cast.
764  const CastInst * const CI,
765  const DataLayout * const TD,
766  const int ix) {
767  if (!CI->isLosslessCast())
768  return false;
769 
770  // The size of ByVal arguments is derived from the type, so we
771  // can't change to a type with a different size. If the size were
772  // passed explicitly we could avoid this check.
773  if (!CS.isByValArgument(ix))
774  return true;
775 
776  Type* SrcTy =
777  cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
778  Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
779  if (!SrcTy->isSized() || !DstTy->isSized())
780  return false;
781  if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy))
782  return false;
783  return true;
784 }
785 
786 // Try to fold some different type of calls here.
787 // Currently we're only working with the checking functions, memcpy_chk,
788 // mempcpy_chk, memmove_chk, memset_chk, strcpy_chk, stpcpy_chk, strncpy_chk,
789 // strcat_chk and strncat_chk.
790 Instruction *InstCombiner::tryOptimizeCall(CallInst *CI, const DataLayout *TD) {
791  if (CI->getCalledFunction() == 0) return 0;
792 
793  if (Value *With = Simplifier->optimizeCall(CI)) {
794  ++NumSimplified;
795  return CI->use_empty() ? CI : ReplaceInstUsesWith(*CI, With);
796  }
797 
798  return 0;
799 }
800 
802  // Strip off at most one level of pointer casts, looking for an alloca. This
803  // is good enough in practice and simpler than handling any number of casts.
804  Value *Underlying = TrampMem->stripPointerCasts();
805  if (Underlying != TrampMem &&
806  (!Underlying->hasOneUse() || *Underlying->use_begin() != TrampMem))
807  return 0;
808  if (!isa<AllocaInst>(Underlying))
809  return 0;
810 
811  IntrinsicInst *InitTrampoline = 0;
812  for (Value::use_iterator I = TrampMem->use_begin(), E = TrampMem->use_end();
813  I != E; I++) {
815  if (!II)
816  return 0;
818  if (InitTrampoline)
819  // More than one init_trampoline writes to this value. Give up.
820  return 0;
821  InitTrampoline = II;
822  continue;
823  }
825  // Allow any number of calls to adjust.trampoline.
826  continue;
827  return 0;
828  }
829 
830  // No call to init.trampoline found.
831  if (!InitTrampoline)
832  return 0;
833 
834  // Check that the alloca is being used in the expected way.
835  if (InitTrampoline->getOperand(0) != TrampMem)
836  return 0;
837 
838  return InitTrampoline;
839 }
840 
842  Value *TrampMem) {
843  // Visit all the previous instructions in the basic block, and try to find a
844  // init.trampoline which has a direct path to the adjust.trampoline.
845  for (BasicBlock::iterator I = AdjustTramp,
846  E = AdjustTramp->getParent()->begin(); I != E; ) {
847  Instruction *Inst = --I;
848  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
850  II->getOperand(0) == TrampMem)
851  return II;
852  if (Inst->mayWriteToMemory())
853  return 0;
854  }
855  return 0;
856 }
857 
858 // Given a call to llvm.adjust.trampoline, find and return the corresponding
859 // call to llvm.init.trampoline if the call to the trampoline can be optimized
860 // to a direct call to a function. Otherwise return NULL.
861 //
863  Callee = Callee->stripPointerCasts();
864  IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee);
865  if (!AdjustTramp ||
867  return 0;
868 
869  Value *TrampMem = AdjustTramp->getOperand(0);
870 
871  if (IntrinsicInst *IT = FindInitTrampolineFromAlloca(TrampMem))
872  return IT;
873  if (IntrinsicInst *IT = FindInitTrampolineFromBB(AdjustTramp, TrampMem))
874  return IT;
875  return 0;
876 }
877 
878 // visitCallSite - Improvements for call and invoke instructions.
879 //
880 Instruction *InstCombiner::visitCallSite(CallSite CS) {
881  if (isAllocLikeFn(CS.getInstruction(), TLI))
882  return visitAllocSite(*CS.getInstruction());
883 
884  bool Changed = false;
885 
886  // If the callee is a pointer to a function, attempt to move any casts to the
887  // arguments of the call/invoke.
888  Value *Callee = CS.getCalledValue();
889  if (!isa<Function>(Callee) && transformConstExprCastCall(CS))
890  return 0;
891 
892  if (Function *CalleeF = dyn_cast<Function>(Callee))
893  // If the call and callee calling conventions don't match, this call must
894  // be unreachable, as the call is undefined.
895  if (CalleeF->getCallingConv() != CS.getCallingConv() &&
896  // Only do this for calls to a function with a body. A prototype may
897  // not actually end up matching the implementation's calling conv for a
898  // variety of reasons (e.g. it may be written in assembly).
899  !CalleeF->isDeclaration()) {
900  Instruction *OldCall = CS.getInstruction();
903  OldCall);
904  // If OldCall does not return void then replaceAllUsesWith undef.
905  // This allows ValueHandlers and custom metadata to adjust itself.
906  if (!OldCall->getType()->isVoidTy())
907  ReplaceInstUsesWith(*OldCall, UndefValue::get(OldCall->getType()));
908  if (isa<CallInst>(OldCall))
909  return EraseInstFromFunction(*OldCall);
910 
911  // We cannot remove an invoke, because it would change the CFG, just
912  // change the callee to a null pointer.
913  cast<InvokeInst>(OldCall)->setCalledFunction(
914  Constant::getNullValue(CalleeF->getType()));
915  return 0;
916  }
917 
918  if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
919  // If CS does not return void then replaceAllUsesWith undef.
920  // This allows ValueHandlers and custom metadata to adjust itself.
921  if (!CS.getInstruction()->getType()->isVoidTy())
922  ReplaceInstUsesWith(*CS.getInstruction(),
924 
925  if (isa<InvokeInst>(CS.getInstruction())) {
926  // Can't remove an invoke because we cannot change the CFG.
927  return 0;
928  }
929 
930  // This instruction is not reachable, just remove it. We insert a store to
931  // undef so that we know that this code is not reachable, despite the fact
932  // that we can't modify the CFG here.
935  CS.getInstruction());
936 
937  return EraseInstFromFunction(*CS.getInstruction());
938  }
939 
940  if (IntrinsicInst *II = FindInitTrampoline(Callee))
941  return transformCallThroughTrampoline(CS, II);
942 
943  PointerType *PTy = cast<PointerType>(Callee->getType());
944  FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
945  if (FTy->isVarArg()) {
946  int ix = FTy->getNumParams();
947  // See if we can optimize any arguments passed through the varargs area of
948  // the call.
949  for (CallSite::arg_iterator I = CS.arg_begin() + FTy->getNumParams(),
950  E = CS.arg_end(); I != E; ++I, ++ix) {
951  CastInst *CI = dyn_cast<CastInst>(*I);
952  if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
953  *I = CI->getOperand(0);
954  Changed = true;
955  }
956  }
957  }
958 
959  if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
960  // Inline asm calls cannot throw - mark them 'nounwind'.
961  CS.setDoesNotThrow();
962  Changed = true;
963  }
964 
965  // Try to optimize the call if possible, we require DataLayout for most of
966  // this. None of these calls are seen as possibly dead so go ahead and
967  // delete the instruction now.
968  if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
969  Instruction *I = tryOptimizeCall(CI, TD);
970  // If we changed something return the result, etc. Otherwise let
971  // the fallthrough check.
972  if (I) return EraseInstFromFunction(*I);
973  }
974 
975  return Changed ? CS.getInstruction() : 0;
976 }
977 
978 // transformConstExprCastCall - If the callee is a constexpr cast of a function,
979 // attempt to move the cast to the arguments of the call/invoke.
980 //
981 bool InstCombiner::transformConstExprCastCall(CallSite CS) {
982  Function *Callee =
984  if (Callee == 0)
985  return false;
986  Instruction *Caller = CS.getInstruction();
987  const AttributeSet &CallerPAL = CS.getAttributes();
988 
989  // Okay, this is a cast from a function to a different type. Unless doing so
990  // would cause a type conversion of one of our arguments, change this call to
991  // be a direct call with arguments casted to the appropriate types.
992  //
993  FunctionType *FT = Callee->getFunctionType();
994  Type *OldRetTy = Caller->getType();
995  Type *NewRetTy = FT->getReturnType();
996 
997  if (NewRetTy->isStructTy())
998  return false; // TODO: Handle multiple return values.
999 
1000  // Check to see if we are changing the return type...
1001  if (OldRetTy != NewRetTy) {
1002  if (!CastInst::isBitCastable(NewRetTy, OldRetTy)) {
1003  if (Callee->isDeclaration())
1004  return false; // Cannot transform this return value.
1005 
1006  if (!Caller->use_empty() &&
1007  // void -> non-void is handled specially
1008  !NewRetTy->isVoidTy())
1009  return false; // Cannot transform this return value.
1010  }
1011 
1012  if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
1013  AttrBuilder RAttrs(CallerPAL, AttributeSet::ReturnIndex);
1014  if (RAttrs.
1015  hasAttributes(AttributeFuncs::
1016  typeIncompatible(NewRetTy, AttributeSet::ReturnIndex),
1017  AttributeSet::ReturnIndex))
1018  return false; // Attribute not compatible with transformed value.
1019  }
1020 
1021  // If the callsite is an invoke instruction, and the return value is used by
1022  // a PHI node in a successor, we cannot change the return type of the call
1023  // because there is no place to put the cast instruction (without breaking
1024  // the critical edge). Bail out in this case.
1025  if (!Caller->use_empty())
1026  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
1027  for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
1028  UI != E; ++UI)
1029  if (PHINode *PN = dyn_cast<PHINode>(*UI))
1030  if (PN->getParent() == II->getNormalDest() ||
1031  PN->getParent() == II->getUnwindDest())
1032  return false;
1033  }
1034 
1035  unsigned NumActualArgs = CS.arg_size();
1036  unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
1037 
1039  for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
1040  Type *ParamTy = FT->getParamType(i);
1041  Type *ActTy = (*AI)->getType();
1042 
1043  if (!CastInst::isBitCastable(ActTy, ParamTy))
1044  return false; // Cannot transform this parameter value.
1045 
1046  if (AttrBuilder(CallerPAL.getParamAttributes(i + 1), i + 1).
1047  hasAttributes(AttributeFuncs::
1048  typeIncompatible(ParamTy, i + 1), i + 1))
1049  return false; // Attribute not compatible with transformed value.
1050 
1051  // If the parameter is passed as a byval argument, then we have to have a
1052  // sized type and the sized type has to have the same size as the old type.
1053  if (ParamTy != ActTy &&
1054  CallerPAL.getParamAttributes(i + 1).hasAttribute(i + 1,
1055  Attribute::ByVal)) {
1056  PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy);
1057  if (ParamPTy == 0 || !ParamPTy->getElementType()->isSized() || TD == 0)
1058  return false;
1059 
1060  Type *CurElTy = ActTy->getPointerElementType();
1061  if (TD->getTypeAllocSize(CurElTy) !=
1062  TD->getTypeAllocSize(ParamPTy->getElementType()))
1063  return false;
1064  }
1065  }
1066 
1067  if (Callee->isDeclaration()) {
1068  // Do not delete arguments unless we have a function body.
1069  if (FT->getNumParams() < NumActualArgs && !FT->isVarArg())
1070  return false;
1071 
1072  // If the callee is just a declaration, don't change the varargsness of the
1073  // call. We don't want to introduce a varargs call where one doesn't
1074  // already exist.
1075  PointerType *APTy = cast<PointerType>(CS.getCalledValue()->getType());
1076  if (FT->isVarArg()!=cast<FunctionType>(APTy->getElementType())->isVarArg())
1077  return false;
1078 
1079  // If both the callee and the cast type are varargs, we still have to make
1080  // sure the number of fixed parameters are the same or we have the same
1081  // ABI issues as if we introduce a varargs call.
1082  if (FT->isVarArg() &&
1083  cast<FunctionType>(APTy->getElementType())->isVarArg() &&
1084  FT->getNumParams() !=
1085  cast<FunctionType>(APTy->getElementType())->getNumParams())
1086  return false;
1087  }
1088 
1089  if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
1090  !CallerPAL.isEmpty())
1091  // In this case we have more arguments than the new function type, but we
1092  // won't be dropping them. Check that these extra arguments have attributes
1093  // that are compatible with being a vararg call argument.
1094  for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
1095  unsigned Index = CallerPAL.getSlotIndex(i - 1);
1096  if (Index <= FT->getNumParams())
1097  break;
1098 
1099  // Check if it has an attribute that's incompatible with varargs.
1100  AttributeSet PAttrs = CallerPAL.getSlotAttributes(i - 1);
1101  if (PAttrs.hasAttribute(Index, Attribute::StructRet))
1102  return false;
1103  }
1104 
1105 
1106  // Okay, we decided that this is a safe thing to do: go ahead and start
1107  // inserting cast instructions as necessary.
1108  std::vector<Value*> Args;
1109  Args.reserve(NumActualArgs);
1111  attrVec.reserve(NumCommonArgs);
1112 
1113  // Get any return attributes.
1114  AttrBuilder RAttrs(CallerPAL, AttributeSet::ReturnIndex);
1115 
1116  // If the return value is not being used, the type may not be compatible
1117  // with the existing attributes. Wipe out any problematic attributes.
1118  RAttrs.
1119  removeAttributes(AttributeFuncs::
1120  typeIncompatible(NewRetTy, AttributeSet::ReturnIndex),
1121  AttributeSet::ReturnIndex);
1122 
1123  // Add the new return attributes.
1124  if (RAttrs.hasAttributes())
1125  attrVec.push_back(AttributeSet::get(Caller->getContext(),
1126  AttributeSet::ReturnIndex, RAttrs));
1127 
1128  AI = CS.arg_begin();
1129  for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
1130  Type *ParamTy = FT->getParamType(i);
1131 
1132  if ((*AI)->getType() == ParamTy) {
1133  Args.push_back(*AI);
1134  } else {
1135  Args.push_back(Builder->CreateBitCast(*AI, ParamTy));
1136  }
1137 
1138  // Add any parameter attributes.
1139  AttrBuilder PAttrs(CallerPAL.getParamAttributes(i + 1), i + 1);
1140  if (PAttrs.hasAttributes())
1141  attrVec.push_back(AttributeSet::get(Caller->getContext(), i + 1,
1142  PAttrs));
1143  }
1144 
1145  // If the function takes more arguments than the call was taking, add them
1146  // now.
1147  for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
1148  Args.push_back(Constant::getNullValue(FT->getParamType(i)));
1149 
1150  // If we are removing arguments to the function, emit an obnoxious warning.
1151  if (FT->getNumParams() < NumActualArgs) {
1152  // TODO: if (!FT->isVarArg()) this call may be unreachable. PR14722
1153  if (FT->isVarArg()) {
1154  // Add all of the arguments in their promoted form to the arg list.
1155  for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
1156  Type *PTy = getPromotedType((*AI)->getType());
1157  if (PTy != (*AI)->getType()) {
1158  // Must promote to pass through va_arg area!
1159  Instruction::CastOps opcode =
1160  CastInst::getCastOpcode(*AI, false, PTy, false);
1161  Args.push_back(Builder->CreateCast(opcode, *AI, PTy));
1162  } else {
1163  Args.push_back(*AI);
1164  }
1165 
1166  // Add any parameter attributes.
1167  AttrBuilder PAttrs(CallerPAL.getParamAttributes(i + 1), i + 1);
1168  if (PAttrs.hasAttributes())
1169  attrVec.push_back(AttributeSet::get(FT->getContext(), i + 1,
1170  PAttrs));
1171  }
1172  }
1173  }
1174 
1175  AttributeSet FnAttrs = CallerPAL.getFnAttributes();
1176  if (CallerPAL.hasAttributes(AttributeSet::FunctionIndex))
1177  attrVec.push_back(AttributeSet::get(Callee->getContext(), FnAttrs));
1178 
1179  if (NewRetTy->isVoidTy())
1180  Caller->setName(""); // Void type should not have a name.
1181 
1182  const AttributeSet &NewCallerPAL = AttributeSet::get(Callee->getContext(),
1183  attrVec);
1184 
1185  Instruction *NC;
1186  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1187  NC = Builder->CreateInvoke(Callee, II->getNormalDest(),
1188  II->getUnwindDest(), Args);
1189  NC->takeName(II);
1190  cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
1191  cast<InvokeInst>(NC)->setAttributes(NewCallerPAL);
1192  } else {
1193  CallInst *CI = cast<CallInst>(Caller);
1194  NC = Builder->CreateCall(Callee, Args);
1195  NC->takeName(CI);
1196  if (CI->isTailCall())
1197  cast<CallInst>(NC)->setTailCall();
1198  cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
1199  cast<CallInst>(NC)->setAttributes(NewCallerPAL);
1200  }
1201 
1202  // Insert a cast of the return type as necessary.
1203  Value *NV = NC;
1204  if (OldRetTy != NV->getType() && !Caller->use_empty()) {
1205  if (!NV->getType()->isVoidTy()) {
1206  NV = NC = CastInst::Create(CastInst::BitCast, NC, OldRetTy);
1207  NC->setDebugLoc(Caller->getDebugLoc());
1208 
1209  // If this is an invoke instruction, we should insert it after the first
1210  // non-phi, instruction in the normal successor block.
1211  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1212  BasicBlock::iterator I = II->getNormalDest()->getFirstInsertionPt();
1213  InsertNewInstBefore(NC, *I);
1214  } else {
1215  // Otherwise, it's a call, just insert cast right after the call.
1216  InsertNewInstBefore(NC, *Caller);
1217  }
1218  Worklist.AddUsersToWorkList(*Caller);
1219  } else {
1220  NV = UndefValue::get(Caller->getType());
1221  }
1222  }
1223 
1224  if (!Caller->use_empty())
1225  ReplaceInstUsesWith(*Caller, NV);
1226 
1227  EraseInstFromFunction(*Caller);
1228  return true;
1229 }
1230 
1231 // transformCallThroughTrampoline - Turn a call to a function created by
1232 // init_trampoline / adjust_trampoline intrinsic pair into a direct call to the
1233 // underlying function.
1234 //
1235 Instruction *
1236 InstCombiner::transformCallThroughTrampoline(CallSite CS,
1237  IntrinsicInst *Tramp) {
1238  Value *Callee = CS.getCalledValue();
1239  PointerType *PTy = cast<PointerType>(Callee->getType());
1240  FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1241  const AttributeSet &Attrs = CS.getAttributes();
1242 
1243  // If the call already has the 'nest' attribute somewhere then give up -
1244  // otherwise 'nest' would occur twice after splicing in the chain.
1245  if (Attrs.hasAttrSomewhere(Attribute::Nest))
1246  return 0;
1247 
1248  assert(Tramp &&
1249  "transformCallThroughTrampoline called with incorrect CallSite.");
1250 
1251  Function *NestF =cast<Function>(Tramp->getArgOperand(1)->stripPointerCasts());
1252  PointerType *NestFPTy = cast<PointerType>(NestF->getType());
1253  FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
1254 
1255  const AttributeSet &NestAttrs = NestF->getAttributes();
1256  if (!NestAttrs.isEmpty()) {
1257  unsigned NestIdx = 1;
1258  Type *NestTy = 0;
1259  AttributeSet NestAttr;
1260 
1261  // Look for a parameter marked with the 'nest' attribute.
1262  for (FunctionType::param_iterator I = NestFTy->param_begin(),
1263  E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
1264  if (NestAttrs.hasAttribute(NestIdx, Attribute::Nest)) {
1265  // Record the parameter type and any other attributes.
1266  NestTy = *I;
1267  NestAttr = NestAttrs.getParamAttributes(NestIdx);
1268  break;
1269  }
1270 
1271  if (NestTy) {
1272  Instruction *Caller = CS.getInstruction();
1273  std::vector<Value*> NewArgs;
1274  NewArgs.reserve(CS.arg_size() + 1);
1275 
1277  NewAttrs.reserve(Attrs.getNumSlots() + 1);
1278 
1279  // Insert the nest argument into the call argument list, which may
1280  // mean appending it. Likewise for attributes.
1281 
1282  // Add any result attributes.
1283  if (Attrs.hasAttributes(AttributeSet::ReturnIndex))
1284  NewAttrs.push_back(AttributeSet::get(Caller->getContext(),
1285  Attrs.getRetAttributes()));
1286 
1287  {
1288  unsigned Idx = 1;
1289  CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
1290  do {
1291  if (Idx == NestIdx) {
1292  // Add the chain argument and attributes.
1293  Value *NestVal = Tramp->getArgOperand(2);
1294  if (NestVal->getType() != NestTy)
1295  NestVal = Builder->CreateBitCast(NestVal, NestTy, "nest");
1296  NewArgs.push_back(NestVal);
1297  NewAttrs.push_back(AttributeSet::get(Caller->getContext(),
1298  NestAttr));
1299  }
1300 
1301  if (I == E)
1302  break;
1303 
1304  // Add the original argument and attributes.
1305  NewArgs.push_back(*I);
1306  AttributeSet Attr = Attrs.getParamAttributes(Idx);
1307  if (Attr.hasAttributes(Idx)) {
1308  AttrBuilder B(Attr, Idx);
1309  NewAttrs.push_back(AttributeSet::get(Caller->getContext(),
1310  Idx + (Idx >= NestIdx), B));
1311  }
1312 
1313  ++Idx, ++I;
1314  } while (1);
1315  }
1316 
1317  // Add any function attributes.
1318  if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
1319  NewAttrs.push_back(AttributeSet::get(FTy->getContext(),
1320  Attrs.getFnAttributes()));
1321 
1322  // The trampoline may have been bitcast to a bogus type (FTy).
1323  // Handle this by synthesizing a new function type, equal to FTy
1324  // with the chain parameter inserted.
1325 
1326  std::vector<Type*> NewTypes;
1327  NewTypes.reserve(FTy->getNumParams()+1);
1328 
1329  // Insert the chain's type into the list of parameter types, which may
1330  // mean appending it.
1331  {
1332  unsigned Idx = 1;
1333  FunctionType::param_iterator I = FTy->param_begin(),
1334  E = FTy->param_end();
1335 
1336  do {
1337  if (Idx == NestIdx)
1338  // Add the chain's type.
1339  NewTypes.push_back(NestTy);
1340 
1341  if (I == E)
1342  break;
1343 
1344  // Add the original type.
1345  NewTypes.push_back(*I);
1346 
1347  ++Idx, ++I;
1348  } while (1);
1349  }
1350 
1351  // Replace the trampoline call with a direct call. Let the generic
1352  // code sort out any function type mismatches.
1353  FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
1354  FTy->isVarArg());
1355  Constant *NewCallee =
1356  NestF->getType() == PointerType::getUnqual(NewFTy) ?
1357  NestF : ConstantExpr::getBitCast(NestF,
1358  PointerType::getUnqual(NewFTy));
1359  const AttributeSet &NewPAL =
1360  AttributeSet::get(FTy->getContext(), NewAttrs);
1361 
1362  Instruction *NewCaller;
1363  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1364  NewCaller = InvokeInst::Create(NewCallee,
1365  II->getNormalDest(), II->getUnwindDest(),
1366  NewArgs);
1367  cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
1368  cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
1369  } else {
1370  NewCaller = CallInst::Create(NewCallee, NewArgs);
1371  if (cast<CallInst>(Caller)->isTailCall())
1372  cast<CallInst>(NewCaller)->setTailCall();
1373  cast<CallInst>(NewCaller)->
1374  setCallingConv(cast<CallInst>(Caller)->getCallingConv());
1375  cast<CallInst>(NewCaller)->setAttributes(NewPAL);
1376  }
1377 
1378  return NewCaller;
1379  }
1380  }
1381 
1382  // Replace the trampoline call with a direct call. Since there is no 'nest'
1383  // parameter, there is no need to adjust the argument list. Let the generic
1384  // code sort out any function type mismatches.
1385  Constant *NewCallee =
1386  NestF->getType() == PTy ? NestF :
1387  ConstantExpr::getBitCast(NestF, PTy);
1388  CS.setCalledFunction(NewCallee);
1389  return CS.getInstruction();
1390 }
unsigned getAlignment() const
use_iterator use_end()
Definition: Value.h:152
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:445
void setDoesNotThrow()
IntegerType * getType() const
Definition: Constants.h:139
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Definition: PatternMatch.h:70
void reserve(unsigned N)
Definition: SmallVector.h:425
LLVMContext & getContext() const
Definition: Function.cpp:167
const AttributeSet & getAttributes() const
Definition: CallSite.h:179
AttributeSet getParamAttributes(unsigned Index) const
The attributes for the specified index are returned.
Definition: Attributes.cpp:792
bool isVolatile() const
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
unsigned getNumParams() const
Definition: DerivedTypes.h:133
Instruction * visitCallInst(CallInst &CI)
IterTy arg_end() const
Definition: CallSite.h:143
Intrinsic::ID getIntrinsicID() const
Definition: IntrinsicInst.h:43
unsigned arg_size() const
Definition: CallSite.h:145
Value * getValue() const
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
Nested function static chain.
Definition: Attributes.h:79
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1231
Type::subtype_iterator param_iterator
Definition: DerivedTypes.h:123
void setCalledFunction(Value *V)
Definition: CallSite.h:99
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
Definition: APInt.h:528
static PointerType * get(Type *ElementType, unsigned AddressSpace)
Definition: Type.cpp:730
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:116
MDNode - a tuple of other values.
Definition: Metadata.h:69
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
Definition: Attributes.cpp:818
void setAlignment(Constant *A)
Type * getPointerElementType() const
Definition: Type.h:373
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:111
iterator begin()
Definition: BasicBlock.h:193
bool isSingleValueType() const
Definition: Type.h:259
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:42
Instruction * visitInvokeInst(InvokeInst &II)
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
bool doesNotThrow() const
Determine if the function cannot unwind.
Definition: Function.h:265
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))
Base class of casting instructions.
Definition: InstrTypes.h:387
unsigned getNumArgOperands() const
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
m_Trunc
Definition: PatternMatch.h:678
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:923
Type * getAlignmentType() const
void setName(const Twine &Name)
Definition: Value.cpp:175
AttributeSet typeIncompatible(Type *Ty, uint64_t Index)
Which attributes cannot be applied to a type.
Hidden pointer to structure to return.
Definition: Attributes.h:105
static IntrinsicInst * FindInitTrampolineFromBB(IntrinsicInst *AdjustTramp, Value *TrampMem)
bool doesNotThrow() const
Determine if the call cannot unwind.
uint64_t getZExtValue() const
Return the zero extended value.
Definition: Constants.h:116
static IntrinsicInst * FindInitTrampoline(Value *Callee)
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
#define T
CallingConv::ID getCallingConv() const
Definition: CallSite.h:170
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Get the constant's value with a saturation limit.
Definition: Constants.h:218
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
Definition: Type.cpp:361
Pass structure by value.
Definition: Attributes.h:73
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:683
ValTy * getCalledValue() const
Definition: CallSite.h:85
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
void takeName(Value *V)
Definition: Value.cpp:239
Type * getElementType() const
Definition: DerivedTypes.h:319
This class represents a truncation of integer types.
void ComputeMaskedBits(Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout *TD=0, unsigned Depth=0)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=0)
Construct any of the CastInst subclasses.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
Definition: APInt.h:510
unsigned getNumSlots() const
Return the number of slots used in this attribute list. This is the number of arguments that have an ...
Definition: Attributes.cpp:906
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:128
bool isLosslessCast() const
Determine if this is a lossless cast.
STATISTIC(NumSimplified,"Number of library calls simplified")
InstrTy * getInstruction() const
Definition: CallSite.h:79
AttributeSet getSlotAttributes(unsigned Slot) const
Return the attributes at the given slot.
Definition: Attributes.cpp:916
static Type * reduceToSingleValueType(Type *T)
LLVM Constant Representation.
Definition: Constant.h:41
static InvokeInst * Create(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=0)
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:178
APInt LLVM_ATTRIBUTE_UNUSED_RESULT sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:942
static bool isBitCastable(Type *SrcTy, Type *DestTy)
Check whether a bitcast between these types is valid.
Value * getOperand(unsigned i) const
Definition: User.h:88
Integer representation type.
Definition: DerivedTypes.h:37
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:874
Constant * getAggregateElement(unsigned Elt) const
Definition: Constants.cpp:183
void setAlignment(unsigned Align)
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=0)
Create a ZExt or BitCast cast instruction.
static UndefValue * get(Type *T)
Definition: Constants.cpp:1334
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:517
void setMetadata(unsigned KindID, MDNode *Node)
Definition: Metadata.cpp:589
bool mayWriteToMemory() const
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=0)
static CastInst * CreateSExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=0)
Create a SExt or BitCast cast instruction.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
Definition: APInt.cpp:736
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
static PointerType * getInt1PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:280
unsigned getIntegerBitWidth() const
Definition: Type.cpp:178
bool doesNotThrow() const
Determine if the call cannot unwind.
Definition: CallSite.h:240
static PointerType * getUnqual(Type *ElementType)
Definition: DerivedTypes.h:436
Class for constant integers.
Definition: Constants.h:51
bool isByValArgument(unsigned ArgNo) const
Determine whether this argument is passed by value.
Definition: CallSite.h:256
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:326
Value * getDest() const
Type * getType() const
Definition: Value.h:111
MDNode * getMetadata(unsigned KindID) const
Definition: Instruction.h:140
static IntrinsicInst * FindInitTrampolineFromAlloca(Value *TrampMem)
Value * getLength() const
Value * stripPointerCasts()
Strips off any unneeded pointer casts, all-zero GEPs and aliases from the specified value...
Definition: Value.cpp:385
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:492
Function * getCalledFunction() const
static bool isSafeToEliminateVarargsCast(const CallSite CS, const CastInst *const CI, const DataLayout *const TD, const int ix)
static Constant * get(Type *Ty, double V)
Definition: Constants.cpp:557
#define NC
Definition: regutils.h:39
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:438
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:591
AttributeSet getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:170
Value * getArgOperand(unsigned i) const
Class for arbitrary precision integers.
Definition: APInt.h:75
bool isIntegerTy() const
Definition: Type.h:196
void setDoesNotThrow()
Definition: CallSite.h:243
void setLength(Value *L)
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition: APInt.h:495
bool isStructTy() const
Definition: Type.h:212
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=0)
use_iterator use_begin()
Definition: Value.h:150
uint64_t MinAlign(uint64_t A, uint64_t B)
Definition: MathExtras.h:535
unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, const DataLayout *TD=0)
Definition: Local.cpp:922
PointerType * getType() const
getType - Global values are always pointers.
Definition: GlobalValue.h:107
void setCalledFunction(Value *Fn)
setCalledFunction - Set the function called.
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:241
bool isDeclaration() const
Definition: Globals.cpp:66
unsigned getSlotIndex(unsigned Slot) const
Return the index for the given slot.
Definition: Attributes.cpp:910
bool hasAttributes(unsigned Index) const
Return true if attribute exists at the given index.
Definition: Attributes.cpp:828
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
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:120
FunctionType * getFunctionType() const
Definition: Function.cpp:171
bool hasOneUse() const
Definition: Value.h:161
void setArgOperand(unsigned i, Value *v)
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc...
bool isTailCall() const
unsigned getPrimitiveSizeInBits() const
Definition: Type.cpp:117
IterTy arg_begin() const
Definition: CallSite.h:137
bool isVarArg() const
Definition: DerivedTypes.h:120
bool use_empty() const
Definition: Value.h:149
Type * getReturnType() const
Definition: DerivedTypes.h:121
Module * getParent()
Definition: GlobalValue.h:286
LLVM Value Representation.
Definition: Value.h:66
void setAlignment(unsigned Align)
bool isSized() const
Definition: Type.h:278
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
Definition: APInt.h:1340
bool isPowerOf2_32(uint32_t Value)
Definition: MathExtras.h:354
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:983
CallingConv::ID getCallingConv() const
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *DL, const TargetLibraryInfo *TLI, bool RoundToAlign=false)
Compute the size of the object pointed by Ptr. Returns true and the object size in Size if successful...
bool isEmpty() const
Return true if there are no attributes.
Definition: Attributes.h:345
static Type * getPromotedType(Type *Ty)
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
const BasicBlock * getParent() const
Definition: Instruction.h:52
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
Definition: Type.h:140
static unsigned getKnownAlignment(Value *V, const DataLayout *TD=0)
getKnownAlignment - Try to infer an alignment for the specified pointer.
Definition: Local.h:177
AttributeSet getFnAttributes() const
The function attributes are returned.
Definition: Attributes.cpp:809