InstCombineCalls.cpp revision 36664bfc7a540f1b45e80c966e36b1aea9d29aa3
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===- InstCombineCalls.cpp -----------------------------------------------===//
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// License. See LICENSE.TXT for details.
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file implements the visitCall and visitInvoke functions.
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "InstCombine.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/IntrinsicInst.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/CallSite.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Target/TargetData.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Analysis/MemoryBuiltins.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using namespace llvm;
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// getPromotedType - Return the specified type promoted as it would be to pass
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// though a va_arg area.
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static const Type *getPromotedType(const Type *Ty) {
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (ITy->getBitWidth() < 32)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return Type::getInt32Ty(Ty->getContext());
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return Ty;
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// EnforceKnownAlignment - If the specified pointer points to an object that
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// we control, modify the object's alignment to PrefAlign. This isn't
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// often possible though. If alignment is important, a more reliable approach
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// is to simply align all global variables and allocation instructions to
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// their preferred alignment from the beginning.
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static unsigned EnforceKnownAlignment(Value *V,
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      unsigned Align, unsigned PrefAlign) {
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  User *U = dyn_cast<User>(V);
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!U) return Align;
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  switch (Operator::getOpcode(U)) {
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  default: break;
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  case Instruction::BitCast:
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  case Instruction::GetElementPtr: {
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If all indexes are zero, it is just the alignment of the base pointer.
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool AllZeroOperands = true;
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!isa<Constant>(*i) ||
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          !cast<Constant>(*i)->isNullValue()) {
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AllZeroOperands = false;
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        break;
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (AllZeroOperands) {
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Treat this like a bitcast.
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
60    }
61    break;
62  }
63  }
64
65  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
66    // If there is a large requested alignment and we can, bump up the alignment
67    // of the global.
68    if (!GV->isDeclaration()) {
69      if (GV->getAlignment() >= PrefAlign)
70        Align = GV->getAlignment();
71      else {
72        GV->setAlignment(PrefAlign);
73        Align = PrefAlign;
74      }
75    }
76  } else if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
77    // If there is a requested alignment and if this is an alloca, round up.
78    if (AI->getAlignment() >= PrefAlign)
79      Align = AI->getAlignment();
80    else {
81      AI->setAlignment(PrefAlign);
82      Align = PrefAlign;
83    }
84  }
85
86  return Align;
87}
88
89/// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that
90/// we can determine, return it, otherwise return 0.  If PrefAlign is specified,
91/// and it is more than the alignment of the ultimate object, see if we can
92/// increase the alignment of the ultimate object, making this check succeed.
93unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V,
94                                                  unsigned PrefAlign) {
95  unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) :
96                      sizeof(PrefAlign) * CHAR_BIT;
97  APInt Mask = APInt::getAllOnesValue(BitWidth);
98  APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
99  ComputeMaskedBits(V, Mask, KnownZero, KnownOne);
100  unsigned TrailZ = KnownZero.countTrailingOnes();
101  unsigned Align = 1u << std::min(BitWidth - 1, TrailZ);
102
103  if (PrefAlign > Align)
104    Align = EnforceKnownAlignment(V, Align, PrefAlign);
105
106    // We don't need to make any adjustment.
107  return Align;
108}
109
110Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
111  unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
112  unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2));
113  unsigned MinAlign = std::min(DstAlign, SrcAlign);
114  unsigned CopyAlign = MI->getAlignment();
115
116  if (CopyAlign < MinAlign) {
117    MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
118                                             MinAlign, false));
119    return MI;
120  }
121
122  // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
123  // load/store.
124  ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3));
125  if (MemOpLength == 0) return 0;
126
127  // Source and destination pointer types are always "i8*" for intrinsic.  See
128  // if the size is something we can handle with a single primitive load/store.
129  // A single load+store correctly handles overlapping memory in the memmove
130  // case.
131  unsigned Size = MemOpLength->getZExtValue();
132  if (Size == 0) return MI;  // Delete this mem transfer.
133
134  if (Size > 8 || (Size&(Size-1)))
135    return 0;  // If not 1/2/4/8 bytes, exit.
136
137  // Use an integer load+store unless we can find something better.
138  Type *NewPtrTy =
139            PointerType::getUnqual(IntegerType::get(MI->getContext(), Size<<3));
140
141  // Memcpy forces the use of i8* for the source and destination.  That means
142  // that if you're using memcpy to move one double around, you'll get a cast
143  // from double* to i8*.  We'd much rather use a double load+store rather than
144  // an i64 load+store, here because this improves the odds that the source or
145  // dest address will be promotable.  See if we can find a better type than the
146  // integer datatype.
147  Value *StrippedDest = MI->getOperand(1)->stripPointerCasts();
148  if (StrippedDest != MI->getOperand(1)) {
149    const Type *SrcETy = cast<PointerType>(StrippedDest->getType())
150                                    ->getElementType();
151    if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
152      // The SrcETy might be something like {{{double}}} or [1 x double].  Rip
153      // down through these levels if so.
154      while (!SrcETy->isSingleValueType()) {
155        if (const StructType *STy = dyn_cast<StructType>(SrcETy)) {
156          if (STy->getNumElements() == 1)
157            SrcETy = STy->getElementType(0);
158          else
159            break;
160        } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) {
161          if (ATy->getNumElements() == 1)
162            SrcETy = ATy->getElementType();
163          else
164            break;
165        } else
166          break;
167      }
168
169      if (SrcETy->isSingleValueType())
170        NewPtrTy = PointerType::getUnqual(SrcETy);
171    }
172  }
173
174
175  // If the memcpy/memmove provides better alignment info than we can
176  // infer, use it.
177  SrcAlign = std::max(SrcAlign, CopyAlign);
178  DstAlign = std::max(DstAlign, CopyAlign);
179
180  Value *Src = Builder->CreateBitCast(MI->getOperand(2), NewPtrTy);
181  Value *Dest = Builder->CreateBitCast(MI->getOperand(1), NewPtrTy);
182  Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign);
183  InsertNewInstBefore(L, *MI);
184  InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI);
185
186  // Set the size of the copy to 0, it will be deleted on the next iteration.
187  MI->setOperand(3, Constant::getNullValue(MemOpLength->getType()));
188  return MI;
189}
190
191Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
192  unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest());
193  if (MI->getAlignment() < Alignment) {
194    MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
195                                             Alignment, false));
196    return MI;
197  }
198
199  // Extract the length and alignment and fill if they are constant.
200  ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
201  ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
202  if (!LenC || !FillC || !FillC->getType()->isInteger(8))
203    return 0;
204  uint64_t Len = LenC->getZExtValue();
205  Alignment = MI->getAlignment();
206
207  // If the length is zero, this is a no-op
208  if (Len == 0) return MI; // memset(d,c,0,a) -> noop
209
210  // memset(s,c,n) -> store s, c (for n=1,2,4,8)
211  if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
212    const Type *ITy = IntegerType::get(MI->getContext(), Len*8);  // n=1 -> i8.
213
214    Value *Dest = MI->getDest();
215    Dest = Builder->CreateBitCast(Dest, PointerType::getUnqual(ITy));
216
217    // Alignment 0 is identity for alignment 1 for memset, but not store.
218    if (Alignment == 0) Alignment = 1;
219
220    // Extract the fill value and store.
221    uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
222    InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill),
223                                      Dest, false, Alignment), *MI);
224
225    // Set the size of the copy to 0, it will be deleted on the next iteration.
226    MI->setLength(Constant::getNullValue(LenC->getType()));
227    return MI;
228  }
229
230  return 0;
231}
232
233
234/// visitCallInst - CallInst simplification.  This mostly only handles folding
235/// of intrinsic instructions.  For normal calls, it allows visitCallSite to do
236/// the heavy lifting.
237///
238Instruction *InstCombiner::visitCallInst(CallInst &CI) {
239  if (isFreeCall(&CI))
240    return visitFree(CI);
241
242  // If the caller function is nounwind, mark the call as nounwind, even if the
243  // callee isn't.
244  if (CI.getParent()->getParent()->doesNotThrow() &&
245      !CI.doesNotThrow()) {
246    CI.setDoesNotThrow();
247    return &CI;
248  }
249
250  IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
251  if (!II) return visitCallSite(&CI);
252
253  // Intrinsics cannot occur in an invoke, so handle them here instead of in
254  // visitCallSite.
255  if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
256    bool Changed = false;
257
258    // memmove/cpy/set of zero bytes is a noop.
259    if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
260      if (NumBytes->isNullValue()) return EraseInstFromFunction(CI);
261
262      if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
263        if (CI->getZExtValue() == 1) {
264          // Replace the instruction with just byte operations.  We would
265          // transform other cases to loads/stores, but we don't know if
266          // alignment is sufficient.
267        }
268    }
269
270    // If we have a memmove and the source operation is a constant global,
271    // then the source and dest pointers can't alias, so we can change this
272    // into a call to memcpy.
273    if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
274      if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
275        if (GVSrc->isConstant()) {
276          Module *M = CI.getParent()->getParent()->getParent();
277          Intrinsic::ID MemCpyID = Intrinsic::memcpy;
278          const Type *Tys[1];
279          Tys[0] = CI.getOperand(3)->getType();
280          CI.setOperand(0,
281                        Intrinsic::getDeclaration(M, MemCpyID, Tys, 1));
282          Changed = true;
283        }
284    }
285
286    if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
287      // memmove(x,x,size) -> noop.
288      if (MTI->getSource() == MTI->getDest())
289        return EraseInstFromFunction(CI);
290    }
291
292    // If we can determine a pointer alignment that is bigger than currently
293    // set, update the alignment.
294    if (isa<MemTransferInst>(MI)) {
295      if (Instruction *I = SimplifyMemTransfer(MI))
296        return I;
297    } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
298      if (Instruction *I = SimplifyMemSet(MSI))
299        return I;
300    }
301
302    if (Changed) return II;
303  }
304
305  switch (II->getIntrinsicID()) {
306  default: break;
307  case Intrinsic::bswap:
308    // bswap(bswap(x)) -> x
309    if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1)))
310      if (Operand->getIntrinsicID() == Intrinsic::bswap)
311        return ReplaceInstUsesWith(CI, Operand->getOperand(1));
312
313    // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
314    if (TruncInst *TI = dyn_cast<TruncInst>(II->getOperand(1))) {
315      if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0)))
316        if (Operand->getIntrinsicID() == Intrinsic::bswap) {
317          unsigned C = Operand->getType()->getPrimitiveSizeInBits() -
318                       TI->getType()->getPrimitiveSizeInBits();
319          Value *CV = ConstantInt::get(Operand->getType(), C);
320          Value *V = Builder->CreateLShr(Operand->getOperand(1), CV);
321          return new TruncInst(V, TI->getType());
322        }
323    }
324
325    break;
326  case Intrinsic::powi:
327    if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getOperand(2))) {
328      // powi(x, 0) -> 1.0
329      if (Power->isZero())
330        return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
331      // powi(x, 1) -> x
332      if (Power->isOne())
333        return ReplaceInstUsesWith(CI, II->getOperand(1));
334      // powi(x, -1) -> 1/x
335      if (Power->isAllOnesValue())
336        return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
337                                          II->getOperand(1));
338    }
339    break;
340  case Intrinsic::cttz: {
341    // If all bits below the first known one are known zero,
342    // this value is constant.
343    const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType());
344    uint32_t BitWidth = IT->getBitWidth();
345    APInt KnownZero(BitWidth, 0);
346    APInt KnownOne(BitWidth, 0);
347    ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth),
348                      KnownZero, KnownOne);
349    unsigned TrailingZeros = KnownOne.countTrailingZeros();
350    APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros));
351    if ((Mask & KnownZero) == Mask)
352      return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
353                                 APInt(BitWidth, TrailingZeros)));
354
355    }
356    break;
357  case Intrinsic::ctlz: {
358    // If all bits above the first known one are known zero,
359    // this value is constant.
360    const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType());
361    uint32_t BitWidth = IT->getBitWidth();
362    APInt KnownZero(BitWidth, 0);
363    APInt KnownOne(BitWidth, 0);
364    ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth),
365                      KnownZero, KnownOne);
366    unsigned LeadingZeros = KnownOne.countLeadingZeros();
367    APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros));
368    if ((Mask & KnownZero) == Mask)
369      return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
370                                 APInt(BitWidth, LeadingZeros)));
371
372    }
373    break;
374  case Intrinsic::uadd_with_overflow: {
375    Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
376    const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType());
377    uint32_t BitWidth = IT->getBitWidth();
378    APInt Mask = APInt::getSignBit(BitWidth);
379    APInt LHSKnownZero(BitWidth, 0);
380    APInt LHSKnownOne(BitWidth, 0);
381    ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne);
382    bool LHSKnownNegative = LHSKnownOne[BitWidth - 1];
383    bool LHSKnownPositive = LHSKnownZero[BitWidth - 1];
384
385    if (LHSKnownNegative || LHSKnownPositive) {
386      APInt RHSKnownZero(BitWidth, 0);
387      APInt RHSKnownOne(BitWidth, 0);
388      ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne);
389      bool RHSKnownNegative = RHSKnownOne[BitWidth - 1];
390      bool RHSKnownPositive = RHSKnownZero[BitWidth - 1];
391      if (LHSKnownNegative && RHSKnownNegative) {
392        // The sign bit is set in both cases: this MUST overflow.
393        // Create a simple add instruction, and insert it into the struct.
394        Instruction *Add = BinaryOperator::CreateAdd(LHS, RHS, "", &CI);
395        Worklist.Add(Add);
396        Constant *V[] = {
397          UndefValue::get(LHS->getType()),ConstantInt::getTrue(II->getContext())
398        };
399        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
400        return InsertValueInst::Create(Struct, Add, 0);
401      }
402
403      if (LHSKnownPositive && RHSKnownPositive) {
404        // The sign bit is clear in both cases: this CANNOT overflow.
405        // Create a simple add instruction, and insert it into the struct.
406        Instruction *Add = BinaryOperator::CreateNUWAdd(LHS, RHS, "", &CI);
407        Worklist.Add(Add);
408        Constant *V[] = {
409          UndefValue::get(LHS->getType()),
410          ConstantInt::getFalse(II->getContext())
411        };
412        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
413        return InsertValueInst::Create(Struct, Add, 0);
414      }
415    }
416  }
417  // FALL THROUGH uadd into sadd
418  case Intrinsic::sadd_with_overflow:
419    // Canonicalize constants into the RHS.
420    if (isa<Constant>(II->getOperand(1)) &&
421        !isa<Constant>(II->getOperand(2))) {
422      Value *LHS = II->getOperand(1);
423      II->setOperand(1, II->getOperand(2));
424      II->setOperand(2, LHS);
425      return II;
426    }
427
428    // X + undef -> undef
429    if (isa<UndefValue>(II->getOperand(2)))
430      return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
431
432    if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) {
433      // X + 0 -> {X, false}
434      if (RHS->isZero()) {
435        Constant *V[] = {
436          UndefValue::get(II->getOperand(0)->getType()),
437          ConstantInt::getFalse(II->getContext())
438        };
439        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
440        return InsertValueInst::Create(Struct, II->getOperand(1), 0);
441      }
442    }
443    break;
444  case Intrinsic::usub_with_overflow:
445  case Intrinsic::ssub_with_overflow:
446    // undef - X -> undef
447    // X - undef -> undef
448    if (isa<UndefValue>(II->getOperand(1)) ||
449        isa<UndefValue>(II->getOperand(2)))
450      return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
451
452    if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) {
453      // X - 0 -> {X, false}
454      if (RHS->isZero()) {
455        Constant *V[] = {
456          UndefValue::get(II->getOperand(1)->getType()),
457          ConstantInt::getFalse(II->getContext())
458        };
459        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
460        return InsertValueInst::Create(Struct, II->getOperand(1), 0);
461      }
462    }
463    break;
464  case Intrinsic::umul_with_overflow:
465  case Intrinsic::smul_with_overflow:
466    // Canonicalize constants into the RHS.
467    if (isa<Constant>(II->getOperand(1)) &&
468        !isa<Constant>(II->getOperand(2))) {
469      Value *LHS = II->getOperand(1);
470      II->setOperand(1, II->getOperand(2));
471      II->setOperand(2, LHS);
472      return II;
473    }
474
475    // X * undef -> undef
476    if (isa<UndefValue>(II->getOperand(2)))
477      return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
478
479    if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getOperand(2))) {
480      // X*0 -> {0, false}
481      if (RHSI->isZero())
482        return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType()));
483
484      // X * 1 -> {X, false}
485      if (RHSI->equalsInt(1)) {
486        Constant *V[] = {
487          UndefValue::get(II->getOperand(1)->getType()),
488          ConstantInt::getFalse(II->getContext())
489        };
490        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
491        return InsertValueInst::Create(Struct, II->getOperand(1), 0);
492      }
493    }
494    break;
495  case Intrinsic::ppc_altivec_lvx:
496  case Intrinsic::ppc_altivec_lvxl:
497  case Intrinsic::x86_sse_loadu_ps:
498  case Intrinsic::x86_sse2_loadu_pd:
499  case Intrinsic::x86_sse2_loadu_dq:
500    // Turn PPC lvx     -> load if the pointer is known aligned.
501    // Turn X86 loadups -> load if the pointer is known aligned.
502    if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
503      Value *Ptr = Builder->CreateBitCast(II->getOperand(1),
504                                         PointerType::getUnqual(II->getType()));
505      return new LoadInst(Ptr);
506    }
507    break;
508  case Intrinsic::ppc_altivec_stvx:
509  case Intrinsic::ppc_altivec_stvxl:
510    // Turn stvx -> store if the pointer is known aligned.
511    if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) {
512      const Type *OpPtrTy =
513        PointerType::getUnqual(II->getOperand(1)->getType());
514      Value *Ptr = Builder->CreateBitCast(II->getOperand(2), OpPtrTy);
515      return new StoreInst(II->getOperand(1), Ptr);
516    }
517    break;
518  case Intrinsic::x86_sse_storeu_ps:
519  case Intrinsic::x86_sse2_storeu_pd:
520  case Intrinsic::x86_sse2_storeu_dq:
521    // Turn X86 storeu -> store if the pointer is known aligned.
522    if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
523      const Type *OpPtrTy =
524        PointerType::getUnqual(II->getOperand(2)->getType());
525      Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy);
526      return new StoreInst(II->getOperand(2), Ptr);
527    }
528    break;
529
530  case Intrinsic::x86_sse_cvttss2si: {
531    // These intrinsics only demands the 0th element of its input vector.  If
532    // we can simplify the input based on that, do so now.
533    unsigned VWidth =
534      cast<VectorType>(II->getOperand(1)->getType())->getNumElements();
535    APInt DemandedElts(VWidth, 1);
536    APInt UndefElts(VWidth, 0);
537    if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
538                                              UndefElts)) {
539      II->setOperand(1, V);
540      return II;
541    }
542    break;
543  }
544
545  case Intrinsic::ppc_altivec_vperm:
546    // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
547    if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) {
548      assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
549
550      // Check that all of the elements are integer constants or undefs.
551      bool AllEltsOk = true;
552      for (unsigned i = 0; i != 16; ++i) {
553        if (!isa<ConstantInt>(Mask->getOperand(i)) &&
554            !isa<UndefValue>(Mask->getOperand(i))) {
555          AllEltsOk = false;
556          break;
557        }
558      }
559
560      if (AllEltsOk) {
561        // Cast the input vectors to byte vectors.
562        Value *Op0 = Builder->CreateBitCast(II->getOperand(1), Mask->getType());
563        Value *Op1 = Builder->CreateBitCast(II->getOperand(2), Mask->getType());
564        Value *Result = UndefValue::get(Op0->getType());
565
566        // Only extract each element once.
567        Value *ExtractedElts[32];
568        memset(ExtractedElts, 0, sizeof(ExtractedElts));
569
570        for (unsigned i = 0; i != 16; ++i) {
571          if (isa<UndefValue>(Mask->getOperand(i)))
572            continue;
573          unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue();
574          Idx &= 31;  // Match the hardware behavior.
575
576          if (ExtractedElts[Idx] == 0) {
577            ExtractedElts[Idx] =
578              Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1,
579                  ConstantInt::get(Type::getInt32Ty(II->getContext()),
580                                   Idx&15, false), "tmp");
581          }
582
583          // Insert this value into the result vector.
584          Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx],
585                         ConstantInt::get(Type::getInt32Ty(II->getContext()),
586                                          i, false), "tmp");
587        }
588        return CastInst::Create(Instruction::BitCast, Result, CI.getType());
589      }
590    }
591    break;
592
593  case Intrinsic::stackrestore: {
594    // If the save is right next to the restore, remove the restore.  This can
595    // happen when variable allocas are DCE'd.
596    if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) {
597      if (SS->getIntrinsicID() == Intrinsic::stacksave) {
598        BasicBlock::iterator BI = SS;
599        if (&*++BI == II)
600          return EraseInstFromFunction(CI);
601      }
602    }
603
604    // Scan down this block to see if there is another stack restore in the
605    // same block without an intervening call/alloca.
606    BasicBlock::iterator BI = II;
607    TerminatorInst *TI = II->getParent()->getTerminator();
608    bool CannotRemove = false;
609    for (++BI; &*BI != TI; ++BI) {
610      if (isa<AllocaInst>(BI) || isMalloc(BI)) {
611        CannotRemove = true;
612        break;
613      }
614      if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
615        if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
616          // If there is a stackrestore below this one, remove this one.
617          if (II->getIntrinsicID() == Intrinsic::stackrestore)
618            return EraseInstFromFunction(CI);
619          // Otherwise, ignore the intrinsic.
620        } else {
621          // If we found a non-intrinsic call, we can't remove the stack
622          // restore.
623          CannotRemove = true;
624          break;
625        }
626      }
627    }
628
629    // If the stack restore is in a return/unwind block and if there are no
630    // allocas or calls between the restore and the return, nuke the restore.
631    if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)))
632      return EraseInstFromFunction(CI);
633    break;
634  }
635  case Intrinsic::objectsize: {
636    ConstantInt *Const = cast<ConstantInt>(II->getOperand(2));
637    const Type *Ty = CI.getType();
638
639    // 0 is maximum number of bytes left, 1 is minimum number of bytes left.
640    // TODO: actually add these values, the current return values are "don't
641    // know".
642    if (Const->getZExtValue() == 0)
643      return ReplaceInstUsesWith(CI, Constant::getAllOnesValue(Ty));
644    else
645      return ReplaceInstUsesWith(CI, ConstantInt::get(Ty, 0));
646  }
647  }
648
649  return visitCallSite(II);
650}
651
652// InvokeInst simplification
653//
654Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
655  return visitCallSite(&II);
656}
657
658/// isSafeToEliminateVarargsCast - If this cast does not affect the value
659/// passed through the varargs area, we can eliminate the use of the cast.
660static bool isSafeToEliminateVarargsCast(const CallSite CS,
661                                         const CastInst * const CI,
662                                         const TargetData * const TD,
663                                         const int ix) {
664  if (!CI->isLosslessCast())
665    return false;
666
667  // The size of ByVal arguments is derived from the type, so we
668  // can't change to a type with a different size.  If the size were
669  // passed explicitly we could avoid this check.
670  if (!CS.paramHasAttr(ix, Attribute::ByVal))
671    return true;
672
673  const Type* SrcTy =
674            cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
675  const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
676  if (!SrcTy->isSized() || !DstTy->isSized())
677    return false;
678  if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy))
679    return false;
680  return true;
681}
682
683// visitCallSite - Improvements for call and invoke instructions.
684//
685Instruction *InstCombiner::visitCallSite(CallSite CS) {
686  bool Changed = false;
687
688  // If the callee is a constexpr cast of a function, attempt to move the cast
689  // to the arguments of the call/invoke.
690  if (transformConstExprCastCall(CS)) return 0;
691
692  Value *Callee = CS.getCalledValue();
693
694  if (Function *CalleeF = dyn_cast<Function>(Callee))
695    if (CalleeF->getCallingConv() != CS.getCallingConv()) {
696      Instruction *OldCall = CS.getInstruction();
697      // If the call and callee calling conventions don't match, this call must
698      // be unreachable, as the call is undefined.
699      new StoreInst(ConstantInt::getTrue(Callee->getContext()),
700                UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
701                                  OldCall);
702      // If OldCall dues not return void then replaceAllUsesWith undef.
703      // This allows ValueHandlers and custom metadata to adjust itself.
704      if (!OldCall->getType()->isVoidTy())
705        OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
706      if (isa<CallInst>(OldCall))   // Not worth removing an invoke here.
707        return EraseInstFromFunction(*OldCall);
708      return 0;
709    }
710
711  if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
712    // This instruction is not reachable, just remove it.  We insert a store to
713    // undef so that we know that this code is not reachable, despite the fact
714    // that we can't modify the CFG here.
715    new StoreInst(ConstantInt::getTrue(Callee->getContext()),
716               UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
717                  CS.getInstruction());
718
719    // If CS dues not return void then replaceAllUsesWith undef.
720    // This allows ValueHandlers and custom metadata to adjust itself.
721    if (!CS.getInstruction()->getType()->isVoidTy())
722      CS.getInstruction()->
723        replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType()));
724
725    if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
726      // Don't break the CFG, insert a dummy cond branch.
727      BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
728                         ConstantInt::getTrue(Callee->getContext()), II);
729    }
730    return EraseInstFromFunction(*CS.getInstruction());
731  }
732
733  if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee))
734    if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0)))
735      if (In->getIntrinsicID() == Intrinsic::init_trampoline)
736        return transformCallThroughTrampoline(CS);
737
738  const PointerType *PTy = cast<PointerType>(Callee->getType());
739  const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
740  if (FTy->isVarArg()) {
741    int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1);
742    // See if we can optimize any arguments passed through the varargs area of
743    // the call.
744    for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
745           E = CS.arg_end(); I != E; ++I, ++ix) {
746      CastInst *CI = dyn_cast<CastInst>(*I);
747      if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
748        *I = CI->getOperand(0);
749        Changed = true;
750      }
751    }
752  }
753
754  if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
755    // Inline asm calls cannot throw - mark them 'nounwind'.
756    CS.setDoesNotThrow();
757    Changed = true;
758  }
759
760  return Changed ? CS.getInstruction() : 0;
761}
762
763// transformConstExprCastCall - If the callee is a constexpr cast of a function,
764// attempt to move the cast to the arguments of the call/invoke.
765//
766bool InstCombiner::transformConstExprCastCall(CallSite CS) {
767  if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
768  ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
769  if (CE->getOpcode() != Instruction::BitCast ||
770      !isa<Function>(CE->getOperand(0)))
771    return false;
772  Function *Callee = cast<Function>(CE->getOperand(0));
773  Instruction *Caller = CS.getInstruction();
774  const AttrListPtr &CallerPAL = CS.getAttributes();
775
776  // Okay, this is a cast from a function to a different type.  Unless doing so
777  // would cause a type conversion of one of our arguments, change this call to
778  // be a direct call with arguments casted to the appropriate types.
779  //
780  const FunctionType *FT = Callee->getFunctionType();
781  const Type *OldRetTy = Caller->getType();
782  const Type *NewRetTy = FT->getReturnType();
783
784  if (isa<StructType>(NewRetTy))
785    return false; // TODO: Handle multiple return values.
786
787  // Check to see if we are changing the return type...
788  if (OldRetTy != NewRetTy) {
789    if (Callee->isDeclaration() &&
790        // Conversion is ok if changing from one pointer type to another or from
791        // a pointer to an integer of the same size.
792        !((isa<PointerType>(OldRetTy) || !TD ||
793           OldRetTy == TD->getIntPtrType(Caller->getContext())) &&
794          (isa<PointerType>(NewRetTy) || !TD ||
795           NewRetTy == TD->getIntPtrType(Caller->getContext()))))
796      return false;   // Cannot transform this return value.
797
798    if (!Caller->use_empty() &&
799        // void -> non-void is handled specially
800        !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy))
801      return false;   // Cannot transform this return value.
802
803    if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
804      Attributes RAttrs = CallerPAL.getRetAttributes();
805      if (RAttrs & Attribute::typeIncompatible(NewRetTy))
806        return false;   // Attribute not compatible with transformed value.
807    }
808
809    // If the callsite is an invoke instruction, and the return value is used by
810    // a PHI node in a successor, we cannot change the return type of the call
811    // because there is no place to put the cast instruction (without breaking
812    // the critical edge).  Bail out in this case.
813    if (!Caller->use_empty())
814      if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
815        for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
816             UI != E; ++UI)
817          if (PHINode *PN = dyn_cast<PHINode>(*UI))
818            if (PN->getParent() == II->getNormalDest() ||
819                PN->getParent() == II->getUnwindDest())
820              return false;
821  }
822
823  unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
824  unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
825
826  CallSite::arg_iterator AI = CS.arg_begin();
827  for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
828    const Type *ParamTy = FT->getParamType(i);
829    const Type *ActTy = (*AI)->getType();
830
831    if (!CastInst::isCastable(ActTy, ParamTy))
832      return false;   // Cannot transform this parameter value.
833
834    if (CallerPAL.getParamAttributes(i + 1)
835        & Attribute::typeIncompatible(ParamTy))
836      return false;   // Attribute not compatible with transformed value.
837
838    // Converting from one pointer type to another or between a pointer and an
839    // integer of the same size is safe even if we do not have a body.
840    bool isConvertible = ActTy == ParamTy ||
841      (TD && ((isa<PointerType>(ParamTy) ||
842      ParamTy == TD->getIntPtrType(Caller->getContext())) &&
843              (isa<PointerType>(ActTy) ||
844              ActTy == TD->getIntPtrType(Caller->getContext()))));
845    if (Callee->isDeclaration() && !isConvertible) return false;
846  }
847
848  if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
849      Callee->isDeclaration())
850    return false;   // Do not delete arguments unless we have a function body.
851
852  if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
853      !CallerPAL.isEmpty())
854    // In this case we have more arguments than the new function type, but we
855    // won't be dropping them.  Check that these extra arguments have attributes
856    // that are compatible with being a vararg call argument.
857    for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
858      if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams())
859        break;
860      Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs;
861      if (PAttrs & Attribute::VarArgsIncompatible)
862        return false;
863    }
864
865  // Okay, we decided that this is a safe thing to do: go ahead and start
866  // inserting cast instructions as necessary...
867  std::vector<Value*> Args;
868  Args.reserve(NumActualArgs);
869  SmallVector<AttributeWithIndex, 8> attrVec;
870  attrVec.reserve(NumCommonArgs);
871
872  // Get any return attributes.
873  Attributes RAttrs = CallerPAL.getRetAttributes();
874
875  // If the return value is not being used, the type may not be compatible
876  // with the existing attributes.  Wipe out any problematic attributes.
877  RAttrs &= ~Attribute::typeIncompatible(NewRetTy);
878
879  // Add the new return attributes.
880  if (RAttrs)
881    attrVec.push_back(AttributeWithIndex::get(0, RAttrs));
882
883  AI = CS.arg_begin();
884  for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
885    const Type *ParamTy = FT->getParamType(i);
886    if ((*AI)->getType() == ParamTy) {
887      Args.push_back(*AI);
888    } else {
889      Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
890          false, ParamTy, false);
891      Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy, "tmp"));
892    }
893
894    // Add any parameter attributes.
895    if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
896      attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
897  }
898
899  // If the function takes more arguments than the call was taking, add them
900  // now.
901  for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
902    Args.push_back(Constant::getNullValue(FT->getParamType(i)));
903
904  // If we are removing arguments to the function, emit an obnoxious warning.
905  if (FT->getNumParams() < NumActualArgs) {
906    if (!FT->isVarArg()) {
907      errs() << "WARNING: While resolving call to function '"
908             << Callee->getName() << "' arguments were dropped!\n";
909    } else {
910      // Add all of the arguments in their promoted form to the arg list.
911      for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
912        const Type *PTy = getPromotedType((*AI)->getType());
913        if (PTy != (*AI)->getType()) {
914          // Must promote to pass through va_arg area!
915          Instruction::CastOps opcode =
916            CastInst::getCastOpcode(*AI, false, PTy, false);
917          Args.push_back(Builder->CreateCast(opcode, *AI, PTy, "tmp"));
918        } else {
919          Args.push_back(*AI);
920        }
921
922        // Add any parameter attributes.
923        if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
924          attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
925      }
926    }
927  }
928
929  if (Attributes FnAttrs =  CallerPAL.getFnAttributes())
930    attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs));
931
932  if (NewRetTy->isVoidTy())
933    Caller->setName("");   // Void type should not have a name.
934
935  const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),
936                                                     attrVec.end());
937
938  Instruction *NC;
939  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
940    NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(),
941                            Args.begin(), Args.end(),
942                            Caller->getName(), Caller);
943    cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
944    cast<InvokeInst>(NC)->setAttributes(NewCallerPAL);
945  } else {
946    NC = CallInst::Create(Callee, Args.begin(), Args.end(),
947                          Caller->getName(), Caller);
948    CallInst *CI = cast<CallInst>(Caller);
949    if (CI->isTailCall())
950      cast<CallInst>(NC)->setTailCall();
951    cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
952    cast<CallInst>(NC)->setAttributes(NewCallerPAL);
953  }
954
955  // Insert a cast of the return type as necessary.
956  Value *NV = NC;
957  if (OldRetTy != NV->getType() && !Caller->use_empty()) {
958    if (!NV->getType()->isVoidTy()) {
959      Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false,
960                                                            OldRetTy, false);
961      NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp");
962
963      // If this is an invoke instruction, we should insert it after the first
964      // non-phi, instruction in the normal successor block.
965      if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
966        BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI();
967        InsertNewInstBefore(NC, *I);
968      } else {
969        // Otherwise, it's a call, just insert cast right after the call instr
970        InsertNewInstBefore(NC, *Caller);
971      }
972      Worklist.AddUsersToWorkList(*Caller);
973    } else {
974      NV = UndefValue::get(Caller->getType());
975    }
976  }
977
978
979  if (!Caller->use_empty())
980    Caller->replaceAllUsesWith(NV);
981
982  EraseInstFromFunction(*Caller);
983  return true;
984}
985
986// transformCallThroughTrampoline - Turn a call to a function created by the
987// init_trampoline intrinsic into a direct call to the underlying function.
988//
989Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
990  Value *Callee = CS.getCalledValue();
991  const PointerType *PTy = cast<PointerType>(Callee->getType());
992  const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
993  const AttrListPtr &Attrs = CS.getAttributes();
994
995  // If the call already has the 'nest' attribute somewhere then give up -
996  // otherwise 'nest' would occur twice after splicing in the chain.
997  if (Attrs.hasAttrSomewhere(Attribute::Nest))
998    return 0;
999
1000  IntrinsicInst *Tramp =
1001    cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0));
1002
1003  Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts());
1004  const PointerType *NestFPTy = cast<PointerType>(NestF->getType());
1005  const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
1006
1007  const AttrListPtr &NestAttrs = NestF->getAttributes();
1008  if (!NestAttrs.isEmpty()) {
1009    unsigned NestIdx = 1;
1010    const Type *NestTy = 0;
1011    Attributes NestAttr = Attribute::None;
1012
1013    // Look for a parameter marked with the 'nest' attribute.
1014    for (FunctionType::param_iterator I = NestFTy->param_begin(),
1015         E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
1016      if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) {
1017        // Record the parameter type and any other attributes.
1018        NestTy = *I;
1019        NestAttr = NestAttrs.getParamAttributes(NestIdx);
1020        break;
1021      }
1022
1023    if (NestTy) {
1024      Instruction *Caller = CS.getInstruction();
1025      std::vector<Value*> NewArgs;
1026      NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1);
1027
1028      SmallVector<AttributeWithIndex, 8> NewAttrs;
1029      NewAttrs.reserve(Attrs.getNumSlots() + 1);
1030
1031      // Insert the nest argument into the call argument list, which may
1032      // mean appending it.  Likewise for attributes.
1033
1034      // Add any result attributes.
1035      if (Attributes Attr = Attrs.getRetAttributes())
1036        NewAttrs.push_back(AttributeWithIndex::get(0, Attr));
1037
1038      {
1039        unsigned Idx = 1;
1040        CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
1041        do {
1042          if (Idx == NestIdx) {
1043            // Add the chain argument and attributes.
1044            Value *NestVal = Tramp->getOperand(3);
1045            if (NestVal->getType() != NestTy)
1046              NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller);
1047            NewArgs.push_back(NestVal);
1048            NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr));
1049          }
1050
1051          if (I == E)
1052            break;
1053
1054          // Add the original argument and attributes.
1055          NewArgs.push_back(*I);
1056          if (Attributes Attr = Attrs.getParamAttributes(Idx))
1057            NewAttrs.push_back
1058              (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr));
1059
1060          ++Idx, ++I;
1061        } while (1);
1062      }
1063
1064      // Add any function attributes.
1065      if (Attributes Attr = Attrs.getFnAttributes())
1066        NewAttrs.push_back(AttributeWithIndex::get(~0, Attr));
1067
1068      // The trampoline may have been bitcast to a bogus type (FTy).
1069      // Handle this by synthesizing a new function type, equal to FTy
1070      // with the chain parameter inserted.
1071
1072      std::vector<const Type*> NewTypes;
1073      NewTypes.reserve(FTy->getNumParams()+1);
1074
1075      // Insert the chain's type into the list of parameter types, which may
1076      // mean appending it.
1077      {
1078        unsigned Idx = 1;
1079        FunctionType::param_iterator I = FTy->param_begin(),
1080          E = FTy->param_end();
1081
1082        do {
1083          if (Idx == NestIdx)
1084            // Add the chain's type.
1085            NewTypes.push_back(NestTy);
1086
1087          if (I == E)
1088            break;
1089
1090          // Add the original type.
1091          NewTypes.push_back(*I);
1092
1093          ++Idx, ++I;
1094        } while (1);
1095      }
1096
1097      // Replace the trampoline call with a direct call.  Let the generic
1098      // code sort out any function type mismatches.
1099      FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
1100                                                FTy->isVarArg());
1101      Constant *NewCallee =
1102        NestF->getType() == PointerType::getUnqual(NewFTy) ?
1103        NestF : ConstantExpr::getBitCast(NestF,
1104                                         PointerType::getUnqual(NewFTy));
1105      const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),
1106                                                   NewAttrs.end());
1107
1108      Instruction *NewCaller;
1109      if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1110        NewCaller = InvokeInst::Create(NewCallee,
1111                                       II->getNormalDest(), II->getUnwindDest(),
1112                                       NewArgs.begin(), NewArgs.end(),
1113                                       Caller->getName(), Caller);
1114        cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
1115        cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
1116      } else {
1117        NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(),
1118                                     Caller->getName(), Caller);
1119        if (cast<CallInst>(Caller)->isTailCall())
1120          cast<CallInst>(NewCaller)->setTailCall();
1121        cast<CallInst>(NewCaller)->
1122          setCallingConv(cast<CallInst>(Caller)->getCallingConv());
1123        cast<CallInst>(NewCaller)->setAttributes(NewPAL);
1124      }
1125      if (!Caller->getType()->isVoidTy())
1126        Caller->replaceAllUsesWith(NewCaller);
1127      Caller->eraseFromParent();
1128      Worklist.Remove(Caller);
1129      return 0;
1130    }
1131  }
1132
1133  // Replace the trampoline call with a direct call.  Since there is no 'nest'
1134  // parameter, there is no need to adjust the argument list.  Let the generic
1135  // code sort out any function type mismatches.
1136  Constant *NewCallee =
1137    NestF->getType() == PTy ? NestF :
1138                              ConstantExpr::getBitCast(NestF, PTy);
1139  CS.setCalledFunction(NewCallee);
1140  return CS.getInstruction();
1141}
1142
1143