InstCombineCalls.cpp revision f79d6246e6b6a83e31a1360e80f828707a51f98e
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/IntrinsicInst.h"
16#include "llvm/Support/CallSite.h"
17#include "llvm/Target/TargetData.h"
18#include "llvm/Analysis/MemoryBuiltins.h"
19using namespace llvm;
20
21/// getPromotedType - Return the specified type promoted as it would be to pass
22/// though a va_arg area.
23static const Type *getPromotedType(const Type *Ty) {
24  if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
25    if (ITy->getBitWidth() < 32)
26      return Type::getInt32Ty(Ty->getContext());
27  }
28  return Ty;
29}
30
31/// EnforceKnownAlignment - If the specified pointer points to an object that
32/// we control, modify the object's alignment to PrefAlign. This isn't
33/// often possible though. If alignment is important, a more reliable approach
34/// is to simply align all global variables and allocation instructions to
35/// their preferred alignment from the beginning.
36///
37static unsigned EnforceKnownAlignment(Value *V,
38                                      unsigned Align, unsigned PrefAlign) {
39
40  User *U = dyn_cast<User>(V);
41  if (!U) return Align;
42
43  switch (Operator::getOpcode(U)) {
44  default: break;
45  case Instruction::BitCast:
46    return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
47  case Instruction::GetElementPtr: {
48    // If all indexes are zero, it is just the alignment of the base pointer.
49    bool AllZeroOperands = true;
50    for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i)
51      if (!isa<Constant>(*i) ||
52          !cast<Constant>(*i)->isNullValue()) {
53        AllZeroOperands = false;
54        break;
55      }
56
57    if (AllZeroOperands) {
58      // Treat this like a bitcast.
59      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()->isIntegerTy(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/// visitCallInst - CallInst simplification.  This mostly only handles folding
234/// of intrinsic instructions.  For normal calls, it allows visitCallSite to do
235/// the heavy lifting.
236///
237Instruction *InstCombiner::visitCallInst(CallInst &CI) {
238  if (isFreeCall(&CI))
239    return visitFree(CI);
240
241  // If the caller function is nounwind, mark the call as nounwind, even if the
242  // callee isn't.
243  if (CI.getParent()->getParent()->doesNotThrow() &&
244      !CI.doesNotThrow()) {
245    CI.setDoesNotThrow();
246    return &CI;
247  }
248
249  IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
250  if (!II) return visitCallSite(&CI);
251
252  // Intrinsics cannot occur in an invoke, so handle them here instead of in
253  // visitCallSite.
254  if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
255    bool Changed = false;
256
257    // memmove/cpy/set of zero bytes is a noop.
258    if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
259      if (NumBytes->isNullValue()) return EraseInstFromFunction(CI);
260
261      if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
262        if (CI->getZExtValue() == 1) {
263          // Replace the instruction with just byte operations.  We would
264          // transform other cases to loads/stores, but we don't know if
265          // alignment is sufficient.
266        }
267    }
268
269    // If we have a memmove and the source operation is a constant global,
270    // then the source and dest pointers can't alias, so we can change this
271    // into a call to memcpy.
272    if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
273      if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
274        if (GVSrc->isConstant()) {
275          Module *M = CI.getParent()->getParent()->getParent();
276          Intrinsic::ID MemCpyID = Intrinsic::memcpy;
277          const Type *Tys[1];
278          Tys[0] = CI.getOperand(3)->getType();
279          CI.setOperand(0,
280                        Intrinsic::getDeclaration(M, MemCpyID, Tys, 1));
281          Changed = true;
282        }
283    }
284
285    if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
286      // memmove(x,x,size) -> noop.
287      if (MTI->getSource() == MTI->getDest())
288        return EraseInstFromFunction(CI);
289    }
290
291    // If we can determine a pointer alignment that is bigger than currently
292    // set, update the alignment.
293    if (isa<MemTransferInst>(MI)) {
294      if (Instruction *I = SimplifyMemTransfer(MI))
295        return I;
296    } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
297      if (Instruction *I = SimplifyMemSet(MSI))
298        return I;
299    }
300
301    if (Changed) return II;
302  }
303
304  switch (II->getIntrinsicID()) {
305  default: break;
306  case Intrinsic::objectsize: {
307    const Type *ReturnTy = CI.getType();
308    Value *Op1 = II->getOperand(1);
309    bool Min = (cast<ConstantInt>(II->getOperand(2))->getZExtValue() == 1);
310
311    // We need target data for just about everything so depend on it.
312    if (!TD) break;
313
314    // Get to the real allocated thing and offset as fast as possible.
315    Op1 = Op1->stripPointerCasts();
316
317    // If we've stripped down to a single global variable that we
318    // can know the size of then just return that.
319    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op1)) {
320      if (GV->hasDefinitiveInitializer()) {
321        Constant *C = GV->getInitializer();
322        uint64_t globalSize = TD->getTypeAllocSize(C->getType());
323        return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, globalSize));
324      } else {
325        Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
326        return ReplaceInstUsesWith(CI, RetVal);
327      }
328    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op1)) {
329
330      // Only handle constant GEPs here.
331      if (CE->getOpcode() != Instruction::GetElementPtr) break;
332      GEPOperator *GEP = cast<GEPOperator>(CE);
333
334      // Make sure we're not a constant offset from an external
335      // global.
336      Value *Operand = GEP->getPointerOperand();
337      Operand = Operand->stripPointerCasts();
338      if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand))
339        if (!GV->hasDefinitiveInitializer()) break;
340
341      // Get what we're pointing to and its size.
342      const PointerType *BaseType =
343        cast<PointerType>(Operand->getType());
344      uint64_t Size = TD->getTypeAllocSize(BaseType->getElementType());
345
346      // Get the current byte offset into the thing. Use the original
347      // operand in case we're looking through a bitcast.
348      SmallVector<Value*, 8> Ops(CE->op_begin()+1, CE->op_end());
349      const PointerType *OffsetType =
350        cast<PointerType>(GEP->getPointerOperand()->getType());
351      uint64_t Offset = TD->getIndexedOffset(OffsetType, &Ops[0], Ops.size());
352
353      if (Size < Offset) {
354        // Out of bound reference? Negative index normalized to large
355        // index? Just return "I don't know".
356        Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
357        return ReplaceInstUsesWith(CI, RetVal);
358      }
359
360      Constant *RetVal = ConstantInt::get(ReturnTy, Size-Offset);
361      return ReplaceInstUsesWith(CI, RetVal);
362
363    }
364    break;
365  }
366  case Intrinsic::bswap:
367    // bswap(bswap(x)) -> x
368    if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1)))
369      if (Operand->getIntrinsicID() == Intrinsic::bswap)
370        return ReplaceInstUsesWith(CI, Operand->getOperand(1));
371
372    // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
373    if (TruncInst *TI = dyn_cast<TruncInst>(II->getOperand(1))) {
374      if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0)))
375        if (Operand->getIntrinsicID() == Intrinsic::bswap) {
376          unsigned C = Operand->getType()->getPrimitiveSizeInBits() -
377                       TI->getType()->getPrimitiveSizeInBits();
378          Value *CV = ConstantInt::get(Operand->getType(), C);
379          Value *V = Builder->CreateLShr(Operand->getOperand(1), CV);
380          return new TruncInst(V, TI->getType());
381        }
382    }
383
384    break;
385  case Intrinsic::powi:
386    if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getOperand(2))) {
387      // powi(x, 0) -> 1.0
388      if (Power->isZero())
389        return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
390      // powi(x, 1) -> x
391      if (Power->isOne())
392        return ReplaceInstUsesWith(CI, II->getOperand(1));
393      // powi(x, -1) -> 1/x
394      if (Power->isAllOnesValue())
395        return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
396                                          II->getOperand(1));
397    }
398    break;
399  case Intrinsic::cttz: {
400    // If all bits below the first known one are known zero,
401    // this value is constant.
402    const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType());
403    uint32_t BitWidth = IT->getBitWidth();
404    APInt KnownZero(BitWidth, 0);
405    APInt KnownOne(BitWidth, 0);
406    ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth),
407                      KnownZero, KnownOne);
408    unsigned TrailingZeros = KnownOne.countTrailingZeros();
409    APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros));
410    if ((Mask & KnownZero) == Mask)
411      return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
412                                 APInt(BitWidth, TrailingZeros)));
413
414    }
415    break;
416  case Intrinsic::ctlz: {
417    // If all bits above the first known one are known zero,
418    // this value is constant.
419    const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType());
420    uint32_t BitWidth = IT->getBitWidth();
421    APInt KnownZero(BitWidth, 0);
422    APInt KnownOne(BitWidth, 0);
423    ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth),
424                      KnownZero, KnownOne);
425    unsigned LeadingZeros = KnownOne.countLeadingZeros();
426    APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros));
427    if ((Mask & KnownZero) == Mask)
428      return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
429                                 APInt(BitWidth, LeadingZeros)));
430
431    }
432    break;
433  case Intrinsic::uadd_with_overflow: {
434    Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
435    const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType());
436    uint32_t BitWidth = IT->getBitWidth();
437    APInt Mask = APInt::getSignBit(BitWidth);
438    APInt LHSKnownZero(BitWidth, 0);
439    APInt LHSKnownOne(BitWidth, 0);
440    ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne);
441    bool LHSKnownNegative = LHSKnownOne[BitWidth - 1];
442    bool LHSKnownPositive = LHSKnownZero[BitWidth - 1];
443
444    if (LHSKnownNegative || LHSKnownPositive) {
445      APInt RHSKnownZero(BitWidth, 0);
446      APInt RHSKnownOne(BitWidth, 0);
447      ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne);
448      bool RHSKnownNegative = RHSKnownOne[BitWidth - 1];
449      bool RHSKnownPositive = RHSKnownZero[BitWidth - 1];
450      if (LHSKnownNegative && RHSKnownNegative) {
451        // The sign bit is set in both cases: this MUST overflow.
452        // Create a simple add instruction, and insert it into the struct.
453        Instruction *Add = BinaryOperator::CreateAdd(LHS, RHS, "", &CI);
454        Worklist.Add(Add);
455        Constant *V[] = {
456          UndefValue::get(LHS->getType()),ConstantInt::getTrue(II->getContext())
457        };
458        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
459        return InsertValueInst::Create(Struct, Add, 0);
460      }
461
462      if (LHSKnownPositive && RHSKnownPositive) {
463        // The sign bit is clear in both cases: this CANNOT overflow.
464        // Create a simple add instruction, and insert it into the struct.
465        Instruction *Add = BinaryOperator::CreateNUWAdd(LHS, RHS, "", &CI);
466        Worklist.Add(Add);
467        Constant *V[] = {
468          UndefValue::get(LHS->getType()),
469          ConstantInt::getFalse(II->getContext())
470        };
471        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
472        return InsertValueInst::Create(Struct, Add, 0);
473      }
474    }
475  }
476  // FALL THROUGH uadd into sadd
477  case Intrinsic::sadd_with_overflow:
478    // Canonicalize constants into the RHS.
479    if (isa<Constant>(II->getOperand(1)) &&
480        !isa<Constant>(II->getOperand(2))) {
481      Value *LHS = II->getOperand(1);
482      II->setOperand(1, II->getOperand(2));
483      II->setOperand(2, LHS);
484      return II;
485    }
486
487    // X + undef -> undef
488    if (isa<UndefValue>(II->getOperand(2)))
489      return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
490
491    if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) {
492      // X + 0 -> {X, false}
493      if (RHS->isZero()) {
494        Constant *V[] = {
495          UndefValue::get(II->getOperand(0)->getType()),
496          ConstantInt::getFalse(II->getContext())
497        };
498        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
499        return InsertValueInst::Create(Struct, II->getOperand(1), 0);
500      }
501    }
502    break;
503  case Intrinsic::usub_with_overflow:
504  case Intrinsic::ssub_with_overflow:
505    // undef - X -> undef
506    // X - undef -> undef
507    if (isa<UndefValue>(II->getOperand(1)) ||
508        isa<UndefValue>(II->getOperand(2)))
509      return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
510
511    if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) {
512      // X - 0 -> {X, false}
513      if (RHS->isZero()) {
514        Constant *V[] = {
515          UndefValue::get(II->getOperand(1)->getType()),
516          ConstantInt::getFalse(II->getContext())
517        };
518        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
519        return InsertValueInst::Create(Struct, II->getOperand(1), 0);
520      }
521    }
522    break;
523  case Intrinsic::umul_with_overflow:
524  case Intrinsic::smul_with_overflow:
525    // Canonicalize constants into the RHS.
526    if (isa<Constant>(II->getOperand(1)) &&
527        !isa<Constant>(II->getOperand(2))) {
528      Value *LHS = II->getOperand(1);
529      II->setOperand(1, II->getOperand(2));
530      II->setOperand(2, LHS);
531      return II;
532    }
533
534    // X * undef -> undef
535    if (isa<UndefValue>(II->getOperand(2)))
536      return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
537
538    if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getOperand(2))) {
539      // X*0 -> {0, false}
540      if (RHSI->isZero())
541        return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType()));
542
543      // X * 1 -> {X, false}
544      if (RHSI->equalsInt(1)) {
545        Constant *V[] = {
546          UndefValue::get(II->getOperand(1)->getType()),
547          ConstantInt::getFalse(II->getContext())
548        };
549        Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
550        return InsertValueInst::Create(Struct, II->getOperand(1), 0);
551      }
552    }
553    break;
554  case Intrinsic::ppc_altivec_lvx:
555  case Intrinsic::ppc_altivec_lvxl:
556  case Intrinsic::x86_sse_loadu_ps:
557  case Intrinsic::x86_sse2_loadu_pd:
558  case Intrinsic::x86_sse2_loadu_dq:
559    // Turn PPC lvx     -> load if the pointer is known aligned.
560    // Turn X86 loadups -> load if the pointer is known aligned.
561    if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
562      Value *Ptr = Builder->CreateBitCast(II->getOperand(1),
563                                         PointerType::getUnqual(II->getType()));
564      return new LoadInst(Ptr);
565    }
566    break;
567  case Intrinsic::ppc_altivec_stvx:
568  case Intrinsic::ppc_altivec_stvxl:
569    // Turn stvx -> store if the pointer is known aligned.
570    if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) {
571      const Type *OpPtrTy =
572        PointerType::getUnqual(II->getOperand(1)->getType());
573      Value *Ptr = Builder->CreateBitCast(II->getOperand(2), OpPtrTy);
574      return new StoreInst(II->getOperand(1), Ptr);
575    }
576    break;
577  case Intrinsic::x86_sse_storeu_ps:
578  case Intrinsic::x86_sse2_storeu_pd:
579  case Intrinsic::x86_sse2_storeu_dq:
580    // Turn X86 storeu -> store if the pointer is known aligned.
581    if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
582      const Type *OpPtrTy =
583        PointerType::getUnqual(II->getOperand(2)->getType());
584      Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy);
585      return new StoreInst(II->getOperand(2), Ptr);
586    }
587    break;
588
589  case Intrinsic::x86_sse_cvttss2si: {
590    // These intrinsics only demands the 0th element of its input vector.  If
591    // we can simplify the input based on that, do so now.
592    unsigned VWidth =
593      cast<VectorType>(II->getOperand(1)->getType())->getNumElements();
594    APInt DemandedElts(VWidth, 1);
595    APInt UndefElts(VWidth, 0);
596    if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
597                                              UndefElts)) {
598      II->setOperand(1, V);
599      return II;
600    }
601    break;
602  }
603
604  case Intrinsic::ppc_altivec_vperm:
605    // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
606    if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) {
607      assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
608
609      // Check that all of the elements are integer constants or undefs.
610      bool AllEltsOk = true;
611      for (unsigned i = 0; i != 16; ++i) {
612        if (!isa<ConstantInt>(Mask->getOperand(i)) &&
613            !isa<UndefValue>(Mask->getOperand(i))) {
614          AllEltsOk = false;
615          break;
616        }
617      }
618
619      if (AllEltsOk) {
620        // Cast the input vectors to byte vectors.
621        Value *Op0 = Builder->CreateBitCast(II->getOperand(1), Mask->getType());
622        Value *Op1 = Builder->CreateBitCast(II->getOperand(2), Mask->getType());
623        Value *Result = UndefValue::get(Op0->getType());
624
625        // Only extract each element once.
626        Value *ExtractedElts[32];
627        memset(ExtractedElts, 0, sizeof(ExtractedElts));
628
629        for (unsigned i = 0; i != 16; ++i) {
630          if (isa<UndefValue>(Mask->getOperand(i)))
631            continue;
632          unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue();
633          Idx &= 31;  // Match the hardware behavior.
634
635          if (ExtractedElts[Idx] == 0) {
636            ExtractedElts[Idx] =
637              Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1,
638                  ConstantInt::get(Type::getInt32Ty(II->getContext()),
639                                   Idx&15, false), "tmp");
640          }
641
642          // Insert this value into the result vector.
643          Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx],
644                         ConstantInt::get(Type::getInt32Ty(II->getContext()),
645                                          i, false), "tmp");
646        }
647        return CastInst::Create(Instruction::BitCast, Result, CI.getType());
648      }
649    }
650    break;
651
652  case Intrinsic::stackrestore: {
653    // If the save is right next to the restore, remove the restore.  This can
654    // happen when variable allocas are DCE'd.
655    if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) {
656      if (SS->getIntrinsicID() == Intrinsic::stacksave) {
657        BasicBlock::iterator BI = SS;
658        if (&*++BI == II)
659          return EraseInstFromFunction(CI);
660      }
661    }
662
663    // Scan down this block to see if there is another stack restore in the
664    // same block without an intervening call/alloca.
665    BasicBlock::iterator BI = II;
666    TerminatorInst *TI = II->getParent()->getTerminator();
667    bool CannotRemove = false;
668    for (++BI; &*BI != TI; ++BI) {
669      if (isa<AllocaInst>(BI) || isMalloc(BI)) {
670        CannotRemove = true;
671        break;
672      }
673      if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
674        if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
675          // If there is a stackrestore below this one, remove this one.
676          if (II->getIntrinsicID() == Intrinsic::stackrestore)
677            return EraseInstFromFunction(CI);
678          // Otherwise, ignore the intrinsic.
679        } else {
680          // If we found a non-intrinsic call, we can't remove the stack
681          // restore.
682          CannotRemove = true;
683          break;
684        }
685      }
686    }
687
688    // If the stack restore is in a return/unwind block and if there are no
689    // allocas or calls between the restore and the return, nuke the restore.
690    if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)))
691      return EraseInstFromFunction(CI);
692    break;
693  }
694  }
695
696  return visitCallSite(II);
697}
698
699// InvokeInst simplification
700//
701Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
702  return visitCallSite(&II);
703}
704
705/// isSafeToEliminateVarargsCast - If this cast does not affect the value
706/// passed through the varargs area, we can eliminate the use of the cast.
707static bool isSafeToEliminateVarargsCast(const CallSite CS,
708                                         const CastInst * const CI,
709                                         const TargetData * const TD,
710                                         const int ix) {
711  if (!CI->isLosslessCast())
712    return false;
713
714  // The size of ByVal arguments is derived from the type, so we
715  // can't change to a type with a different size.  If the size were
716  // passed explicitly we could avoid this check.
717  if (!CS.paramHasAttr(ix, Attribute::ByVal))
718    return true;
719
720  const Type* SrcTy =
721            cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
722  const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
723  if (!SrcTy->isSized() || !DstTy->isSized())
724    return false;
725  if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy))
726    return false;
727  return true;
728}
729
730// visitCallSite - Improvements for call and invoke instructions.
731//
732Instruction *InstCombiner::visitCallSite(CallSite CS) {
733  bool Changed = false;
734
735  // If the callee is a constexpr cast of a function, attempt to move the cast
736  // to the arguments of the call/invoke.
737  if (transformConstExprCastCall(CS)) return 0;
738
739  Value *Callee = CS.getCalledValue();
740
741  if (Function *CalleeF = dyn_cast<Function>(Callee))
742    // If the call and callee calling conventions don't match, this call must
743    // be unreachable, as the call is undefined.
744    if (CalleeF->getCallingConv() != CS.getCallingConv() &&
745        // Only do this for calls to a function with a body.  A prototype may
746        // not actually end up matching the implementation's calling conv for a
747        // variety of reasons (e.g. it may be written in assembly).
748        !CalleeF->isDeclaration()) {
749      Instruction *OldCall = CS.getInstruction();
750      new StoreInst(ConstantInt::getTrue(Callee->getContext()),
751                UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
752                                  OldCall);
753      // If OldCall dues not return void then replaceAllUsesWith undef.
754      // This allows ValueHandlers and custom metadata to adjust itself.
755      if (!OldCall->getType()->isVoidTy())
756        OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
757      if (isa<CallInst>(OldCall))
758        return EraseInstFromFunction(*OldCall);
759
760      // We cannot remove an invoke, because it would change the CFG, just
761      // change the callee to a null pointer.
762      cast<InvokeInst>(OldCall)->setOperand(0,
763                                    Constant::getNullValue(CalleeF->getType()));
764      return 0;
765    }
766
767  if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
768    // This instruction is not reachable, just remove it.  We insert a store to
769    // undef so that we know that this code is not reachable, despite the fact
770    // that we can't modify the CFG here.
771    new StoreInst(ConstantInt::getTrue(Callee->getContext()),
772               UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
773                  CS.getInstruction());
774
775    // If CS dues not return void then replaceAllUsesWith undef.
776    // This allows ValueHandlers and custom metadata to adjust itself.
777    if (!CS.getInstruction()->getType()->isVoidTy())
778      CS.getInstruction()->
779        replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType()));
780
781    if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
782      // Don't break the CFG, insert a dummy cond branch.
783      BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
784                         ConstantInt::getTrue(Callee->getContext()), II);
785    }
786    return EraseInstFromFunction(*CS.getInstruction());
787  }
788
789  if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee))
790    if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0)))
791      if (In->getIntrinsicID() == Intrinsic::init_trampoline)
792        return transformCallThroughTrampoline(CS);
793
794  const PointerType *PTy = cast<PointerType>(Callee->getType());
795  const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
796  if (FTy->isVarArg()) {
797    int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1);
798    // See if we can optimize any arguments passed through the varargs area of
799    // the call.
800    for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
801           E = CS.arg_end(); I != E; ++I, ++ix) {
802      CastInst *CI = dyn_cast<CastInst>(*I);
803      if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
804        *I = CI->getOperand(0);
805        Changed = true;
806      }
807    }
808  }
809
810  if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
811    // Inline asm calls cannot throw - mark them 'nounwind'.
812    CS.setDoesNotThrow();
813    Changed = true;
814  }
815
816  return Changed ? CS.getInstruction() : 0;
817}
818
819// transformConstExprCastCall - If the callee is a constexpr cast of a function,
820// attempt to move the cast to the arguments of the call/invoke.
821//
822bool InstCombiner::transformConstExprCastCall(CallSite CS) {
823  if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
824  ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
825  if (CE->getOpcode() != Instruction::BitCast ||
826      !isa<Function>(CE->getOperand(0)))
827    return false;
828  Function *Callee = cast<Function>(CE->getOperand(0));
829  Instruction *Caller = CS.getInstruction();
830  const AttrListPtr &CallerPAL = CS.getAttributes();
831
832  // Okay, this is a cast from a function to a different type.  Unless doing so
833  // would cause a type conversion of one of our arguments, change this call to
834  // be a direct call with arguments casted to the appropriate types.
835  //
836  const FunctionType *FT = Callee->getFunctionType();
837  const Type *OldRetTy = Caller->getType();
838  const Type *NewRetTy = FT->getReturnType();
839
840  if (NewRetTy->isStructTy())
841    return false; // TODO: Handle multiple return values.
842
843  // Check to see if we are changing the return type...
844  if (OldRetTy != NewRetTy) {
845    if (Callee->isDeclaration() &&
846        // Conversion is ok if changing from one pointer type to another or from
847        // a pointer to an integer of the same size.
848        !((OldRetTy->isPointerTy() || !TD ||
849           OldRetTy == TD->getIntPtrType(Caller->getContext())) &&
850          (NewRetTy->isPointerTy() || !TD ||
851           NewRetTy == TD->getIntPtrType(Caller->getContext()))))
852      return false;   // Cannot transform this return value.
853
854    if (!Caller->use_empty() &&
855        // void -> non-void is handled specially
856        !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy))
857      return false;   // Cannot transform this return value.
858
859    if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
860      Attributes RAttrs = CallerPAL.getRetAttributes();
861      if (RAttrs & Attribute::typeIncompatible(NewRetTy))
862        return false;   // Attribute not compatible with transformed value.
863    }
864
865    // If the callsite is an invoke instruction, and the return value is used by
866    // a PHI node in a successor, we cannot change the return type of the call
867    // because there is no place to put the cast instruction (without breaking
868    // the critical edge).  Bail out in this case.
869    if (!Caller->use_empty())
870      if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
871        for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
872             UI != E; ++UI)
873          if (PHINode *PN = dyn_cast<PHINode>(*UI))
874            if (PN->getParent() == II->getNormalDest() ||
875                PN->getParent() == II->getUnwindDest())
876              return false;
877  }
878
879  unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
880  unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
881
882  CallSite::arg_iterator AI = CS.arg_begin();
883  for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
884    const Type *ParamTy = FT->getParamType(i);
885    const Type *ActTy = (*AI)->getType();
886
887    if (!CastInst::isCastable(ActTy, ParamTy))
888      return false;   // Cannot transform this parameter value.
889
890    if (CallerPAL.getParamAttributes(i + 1)
891        & Attribute::typeIncompatible(ParamTy))
892      return false;   // Attribute not compatible with transformed value.
893
894    // Converting from one pointer type to another or between a pointer and an
895    // integer of the same size is safe even if we do not have a body.
896    bool isConvertible = ActTy == ParamTy ||
897      (TD && ((ParamTy->isPointerTy() ||
898      ParamTy == TD->getIntPtrType(Caller->getContext())) &&
899              (ActTy->isPointerTy() ||
900              ActTy == TD->getIntPtrType(Caller->getContext()))));
901    if (Callee->isDeclaration() && !isConvertible) return false;
902  }
903
904  if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
905      Callee->isDeclaration())
906    return false;   // Do not delete arguments unless we have a function body.
907
908  if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
909      !CallerPAL.isEmpty())
910    // In this case we have more arguments than the new function type, but we
911    // won't be dropping them.  Check that these extra arguments have attributes
912    // that are compatible with being a vararg call argument.
913    for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
914      if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams())
915        break;
916      Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs;
917      if (PAttrs & Attribute::VarArgsIncompatible)
918        return false;
919    }
920
921  // Okay, we decided that this is a safe thing to do: go ahead and start
922  // inserting cast instructions as necessary...
923  std::vector<Value*> Args;
924  Args.reserve(NumActualArgs);
925  SmallVector<AttributeWithIndex, 8> attrVec;
926  attrVec.reserve(NumCommonArgs);
927
928  // Get any return attributes.
929  Attributes RAttrs = CallerPAL.getRetAttributes();
930
931  // If the return value is not being used, the type may not be compatible
932  // with the existing attributes.  Wipe out any problematic attributes.
933  RAttrs &= ~Attribute::typeIncompatible(NewRetTy);
934
935  // Add the new return attributes.
936  if (RAttrs)
937    attrVec.push_back(AttributeWithIndex::get(0, RAttrs));
938
939  AI = CS.arg_begin();
940  for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
941    const Type *ParamTy = FT->getParamType(i);
942    if ((*AI)->getType() == ParamTy) {
943      Args.push_back(*AI);
944    } else {
945      Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
946          false, ParamTy, false);
947      Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy, "tmp"));
948    }
949
950    // Add any parameter attributes.
951    if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
952      attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
953  }
954
955  // If the function takes more arguments than the call was taking, add them
956  // now.
957  for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
958    Args.push_back(Constant::getNullValue(FT->getParamType(i)));
959
960  // If we are removing arguments to the function, emit an obnoxious warning.
961  if (FT->getNumParams() < NumActualArgs) {
962    if (!FT->isVarArg()) {
963      errs() << "WARNING: While resolving call to function '"
964             << Callee->getName() << "' arguments were dropped!\n";
965    } else {
966      // Add all of the arguments in their promoted form to the arg list.
967      for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
968        const Type *PTy = getPromotedType((*AI)->getType());
969        if (PTy != (*AI)->getType()) {
970          // Must promote to pass through va_arg area!
971          Instruction::CastOps opcode =
972            CastInst::getCastOpcode(*AI, false, PTy, false);
973          Args.push_back(Builder->CreateCast(opcode, *AI, PTy, "tmp"));
974        } else {
975          Args.push_back(*AI);
976        }
977
978        // Add any parameter attributes.
979        if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
980          attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
981      }
982    }
983  }
984
985  if (Attributes FnAttrs =  CallerPAL.getFnAttributes())
986    attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs));
987
988  if (NewRetTy->isVoidTy())
989    Caller->setName("");   // Void type should not have a name.
990
991  const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),
992                                                     attrVec.end());
993
994  Instruction *NC;
995  if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
996    NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(),
997                            Args.begin(), Args.end(),
998                            Caller->getName(), Caller);
999    cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
1000    cast<InvokeInst>(NC)->setAttributes(NewCallerPAL);
1001  } else {
1002    NC = CallInst::Create(Callee, Args.begin(), Args.end(),
1003                          Caller->getName(), Caller);
1004    CallInst *CI = cast<CallInst>(Caller);
1005    if (CI->isTailCall())
1006      cast<CallInst>(NC)->setTailCall();
1007    cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
1008    cast<CallInst>(NC)->setAttributes(NewCallerPAL);
1009  }
1010
1011  // Insert a cast of the return type as necessary.
1012  Value *NV = NC;
1013  if (OldRetTy != NV->getType() && !Caller->use_empty()) {
1014    if (!NV->getType()->isVoidTy()) {
1015      Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false,
1016                                                            OldRetTy, false);
1017      NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp");
1018
1019      // If this is an invoke instruction, we should insert it after the first
1020      // non-phi, instruction in the normal successor block.
1021      if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1022        BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI();
1023        InsertNewInstBefore(NC, *I);
1024      } else {
1025        // Otherwise, it's a call, just insert cast right after the call instr
1026        InsertNewInstBefore(NC, *Caller);
1027      }
1028      Worklist.AddUsersToWorkList(*Caller);
1029    } else {
1030      NV = UndefValue::get(Caller->getType());
1031    }
1032  }
1033
1034
1035  if (!Caller->use_empty())
1036    Caller->replaceAllUsesWith(NV);
1037
1038  EraseInstFromFunction(*Caller);
1039  return true;
1040}
1041
1042// transformCallThroughTrampoline - Turn a call to a function created by the
1043// init_trampoline intrinsic into a direct call to the underlying function.
1044//
1045Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
1046  Value *Callee = CS.getCalledValue();
1047  const PointerType *PTy = cast<PointerType>(Callee->getType());
1048  const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1049  const AttrListPtr &Attrs = CS.getAttributes();
1050
1051  // If the call already has the 'nest' attribute somewhere then give up -
1052  // otherwise 'nest' would occur twice after splicing in the chain.
1053  if (Attrs.hasAttrSomewhere(Attribute::Nest))
1054    return 0;
1055
1056  IntrinsicInst *Tramp =
1057    cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0));
1058
1059  Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts());
1060  const PointerType *NestFPTy = cast<PointerType>(NestF->getType());
1061  const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
1062
1063  const AttrListPtr &NestAttrs = NestF->getAttributes();
1064  if (!NestAttrs.isEmpty()) {
1065    unsigned NestIdx = 1;
1066    const Type *NestTy = 0;
1067    Attributes NestAttr = Attribute::None;
1068
1069    // Look for a parameter marked with the 'nest' attribute.
1070    for (FunctionType::param_iterator I = NestFTy->param_begin(),
1071         E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
1072      if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) {
1073        // Record the parameter type and any other attributes.
1074        NestTy = *I;
1075        NestAttr = NestAttrs.getParamAttributes(NestIdx);
1076        break;
1077      }
1078
1079    if (NestTy) {
1080      Instruction *Caller = CS.getInstruction();
1081      std::vector<Value*> NewArgs;
1082      NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1);
1083
1084      SmallVector<AttributeWithIndex, 8> NewAttrs;
1085      NewAttrs.reserve(Attrs.getNumSlots() + 1);
1086
1087      // Insert the nest argument into the call argument list, which may
1088      // mean appending it.  Likewise for attributes.
1089
1090      // Add any result attributes.
1091      if (Attributes Attr = Attrs.getRetAttributes())
1092        NewAttrs.push_back(AttributeWithIndex::get(0, Attr));
1093
1094      {
1095        unsigned Idx = 1;
1096        CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
1097        do {
1098          if (Idx == NestIdx) {
1099            // Add the chain argument and attributes.
1100            Value *NestVal = Tramp->getOperand(3);
1101            if (NestVal->getType() != NestTy)
1102              NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller);
1103            NewArgs.push_back(NestVal);
1104            NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr));
1105          }
1106
1107          if (I == E)
1108            break;
1109
1110          // Add the original argument and attributes.
1111          NewArgs.push_back(*I);
1112          if (Attributes Attr = Attrs.getParamAttributes(Idx))
1113            NewAttrs.push_back
1114              (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr));
1115
1116          ++Idx, ++I;
1117        } while (1);
1118      }
1119
1120      // Add any function attributes.
1121      if (Attributes Attr = Attrs.getFnAttributes())
1122        NewAttrs.push_back(AttributeWithIndex::get(~0, Attr));
1123
1124      // The trampoline may have been bitcast to a bogus type (FTy).
1125      // Handle this by synthesizing a new function type, equal to FTy
1126      // with the chain parameter inserted.
1127
1128      std::vector<const Type*> NewTypes;
1129      NewTypes.reserve(FTy->getNumParams()+1);
1130
1131      // Insert the chain's type into the list of parameter types, which may
1132      // mean appending it.
1133      {
1134        unsigned Idx = 1;
1135        FunctionType::param_iterator I = FTy->param_begin(),
1136          E = FTy->param_end();
1137
1138        do {
1139          if (Idx == NestIdx)
1140            // Add the chain's type.
1141            NewTypes.push_back(NestTy);
1142
1143          if (I == E)
1144            break;
1145
1146          // Add the original type.
1147          NewTypes.push_back(*I);
1148
1149          ++Idx, ++I;
1150        } while (1);
1151      }
1152
1153      // Replace the trampoline call with a direct call.  Let the generic
1154      // code sort out any function type mismatches.
1155      FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
1156                                                FTy->isVarArg());
1157      Constant *NewCallee =
1158        NestF->getType() == PointerType::getUnqual(NewFTy) ?
1159        NestF : ConstantExpr::getBitCast(NestF,
1160                                         PointerType::getUnqual(NewFTy));
1161      const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),
1162                                                   NewAttrs.end());
1163
1164      Instruction *NewCaller;
1165      if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1166        NewCaller = InvokeInst::Create(NewCallee,
1167                                       II->getNormalDest(), II->getUnwindDest(),
1168                                       NewArgs.begin(), NewArgs.end(),
1169                                       Caller->getName(), Caller);
1170        cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
1171        cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
1172      } else {
1173        NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(),
1174                                     Caller->getName(), Caller);
1175        if (cast<CallInst>(Caller)->isTailCall())
1176          cast<CallInst>(NewCaller)->setTailCall();
1177        cast<CallInst>(NewCaller)->
1178          setCallingConv(cast<CallInst>(Caller)->getCallingConv());
1179        cast<CallInst>(NewCaller)->setAttributes(NewPAL);
1180      }
1181      if (!Caller->getType()->isVoidTy())
1182        Caller->replaceAllUsesWith(NewCaller);
1183      Caller->eraseFromParent();
1184      Worklist.Remove(Caller);
1185      return 0;
1186    }
1187  }
1188
1189  // Replace the trampoline call with a direct call.  Since there is no 'nest'
1190  // parameter, there is no need to adjust the argument list.  Let the generic
1191  // code sort out any function type mismatches.
1192  Constant *NewCallee =
1193    NestF->getType() == PTy ? NestF :
1194                              ConstantExpr::getBitCast(NestF, PTy);
1195  CS.setCalledFunction(NewCallee);
1196  return CS.getInstruction();
1197}
1198
1199