blob: c531fd32ac45c8f4cdab4e4a249204f95de63c45 [file] [log] [blame]
Chris Lattner753a2b42010-01-05 07:32:13 +00001//===- 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"
Eric Christopher27ceaa12010-03-06 10:50:38 +000019#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chris Lattner753a2b42010-01-05 07:32:13 +000020using namespace llvm;
21
22/// getPromotedType - Return the specified type promoted as it would be to pass
23/// though a va_arg area.
24static const Type *getPromotedType(const Type *Ty) {
25 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
26 if (ITy->getBitWidth() < 32)
27 return Type::getInt32Ty(Ty->getContext());
28 }
29 return Ty;
30}
31
32/// EnforceKnownAlignment - If the specified pointer points to an object that
33/// we control, modify the object's alignment to PrefAlign. This isn't
34/// often possible though. If alignment is important, a more reliable approach
35/// is to simply align all global variables and allocation instructions to
36/// their preferred alignment from the beginning.
37///
38static unsigned EnforceKnownAlignment(Value *V,
39 unsigned Align, unsigned PrefAlign) {
40
41 User *U = dyn_cast<User>(V);
42 if (!U) return Align;
43
44 switch (Operator::getOpcode(U)) {
45 default: break;
46 case Instruction::BitCast:
47 return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
48 case Instruction::GetElementPtr: {
49 // If all indexes are zero, it is just the alignment of the base pointer.
50 bool AllZeroOperands = true;
51 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i)
52 if (!isa<Constant>(*i) ||
53 !cast<Constant>(*i)->isNullValue()) {
54 AllZeroOperands = false;
55 break;
56 }
57
58 if (AllZeroOperands) {
59 // Treat this like a bitcast.
60 return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
61 }
62 break;
63 }
64 }
65
66 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
67 // If there is a large requested alignment and we can, bump up the alignment
68 // of the global.
69 if (!GV->isDeclaration()) {
70 if (GV->getAlignment() >= PrefAlign)
71 Align = GV->getAlignment();
72 else {
73 GV->setAlignment(PrefAlign);
74 Align = PrefAlign;
75 }
76 }
77 } else if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
78 // If there is a requested alignment and if this is an alloca, round up.
79 if (AI->getAlignment() >= PrefAlign)
80 Align = AI->getAlignment();
81 else {
82 AI->setAlignment(PrefAlign);
83 Align = PrefAlign;
84 }
85 }
86
87 return Align;
88}
89
90/// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that
91/// we can determine, return it, otherwise return 0. If PrefAlign is specified,
92/// and it is more than the alignment of the ultimate object, see if we can
93/// increase the alignment of the ultimate object, making this check succeed.
94unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V,
95 unsigned PrefAlign) {
96 unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) :
97 sizeof(PrefAlign) * CHAR_BIT;
98 APInt Mask = APInt::getAllOnesValue(BitWidth);
99 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
100 ComputeMaskedBits(V, Mask, KnownZero, KnownOne);
101 unsigned TrailZ = KnownZero.countTrailingOnes();
102 unsigned Align = 1u << std::min(BitWidth - 1, TrailZ);
103
104 if (PrefAlign > Align)
105 Align = EnforceKnownAlignment(V, Align, PrefAlign);
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000106
Chris Lattner753a2b42010-01-05 07:32:13 +0000107 // We don't need to make any adjustment.
108 return Align;
109}
110
111Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
Gabor Greif2ff961f2010-04-15 20:51:13 +0000112 unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(0));
113 unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
Chris Lattner753a2b42010-01-05 07:32:13 +0000114 unsigned MinAlign = std::min(DstAlign, SrcAlign);
115 unsigned CopyAlign = MI->getAlignment();
116
117 if (CopyAlign < MinAlign) {
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000118 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
Chris Lattner753a2b42010-01-05 07:32:13 +0000119 MinAlign, false));
120 return MI;
121 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000122
Chris Lattner753a2b42010-01-05 07:32:13 +0000123 // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
124 // load/store.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000125 ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(2));
Chris Lattner753a2b42010-01-05 07:32:13 +0000126 if (MemOpLength == 0) return 0;
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000127
Chris Lattner753a2b42010-01-05 07:32:13 +0000128 // Source and destination pointer types are always "i8*" for intrinsic. See
129 // if the size is something we can handle with a single primitive load/store.
130 // A single load+store correctly handles overlapping memory in the memmove
131 // case.
132 unsigned Size = MemOpLength->getZExtValue();
133 if (Size == 0) return MI; // Delete this mem transfer.
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000134
Chris Lattner753a2b42010-01-05 07:32:13 +0000135 if (Size > 8 || (Size&(Size-1)))
136 return 0; // If not 1/2/4/8 bytes, exit.
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000137
Chris Lattner753a2b42010-01-05 07:32:13 +0000138 // Use an integer load+store unless we can find something better.
Mon P Wang20adc9d2010-04-04 03:10:48 +0000139 unsigned SrcAddrSp =
Gabor Greif9ee17202010-04-15 12:46:56 +0000140 cast<PointerType>(MI->getOperand(1)->getType())->getAddressSpace();
Gabor Greif2ff961f2010-04-15 20:51:13 +0000141 unsigned DstAddrSp =
142 cast<PointerType>(MI->getOperand(0)->getType())->getAddressSpace();
Mon P Wang20adc9d2010-04-04 03:10:48 +0000143
144 const IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
145 Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp);
146 Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp);
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000147
Chris Lattner753a2b42010-01-05 07:32:13 +0000148 // Memcpy forces the use of i8* for the source and destination. That means
149 // that if you're using memcpy to move one double around, you'll get a cast
150 // from double* to i8*. We'd much rather use a double load+store rather than
151 // an i64 load+store, here because this improves the odds that the source or
152 // dest address will be promotable. See if we can find a better type than the
153 // integer datatype.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000154 Value *StrippedDest = MI->getOperand(0)->stripPointerCasts();
155 if (StrippedDest != MI->getOperand(0)) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000156 const Type *SrcETy = cast<PointerType>(StrippedDest->getType())
157 ->getElementType();
158 if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
159 // The SrcETy might be something like {{{double}}} or [1 x double]. Rip
160 // down through these levels if so.
161 while (!SrcETy->isSingleValueType()) {
162 if (const StructType *STy = dyn_cast<StructType>(SrcETy)) {
163 if (STy->getNumElements() == 1)
164 SrcETy = STy->getElementType(0);
165 else
166 break;
167 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) {
168 if (ATy->getNumElements() == 1)
169 SrcETy = ATy->getElementType();
170 else
171 break;
172 } else
173 break;
174 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000175
Mon P Wang20adc9d2010-04-04 03:10:48 +0000176 if (SrcETy->isSingleValueType()) {
177 NewSrcPtrTy = PointerType::get(SrcETy, SrcAddrSp);
178 NewDstPtrTy = PointerType::get(SrcETy, DstAddrSp);
179 }
Chris Lattner753a2b42010-01-05 07:32:13 +0000180 }
181 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000182
183
Chris Lattner753a2b42010-01-05 07:32:13 +0000184 // If the memcpy/memmove provides better alignment info than we can
185 // infer, use it.
186 SrcAlign = std::max(SrcAlign, CopyAlign);
187 DstAlign = std::max(DstAlign, CopyAlign);
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000188
Gabor Greif2ff961f2010-04-15 20:51:13 +0000189 Value *Src = Builder->CreateBitCast(MI->getOperand(1), NewSrcPtrTy);
190 Value *Dest = Builder->CreateBitCast(MI->getOperand(0), NewDstPtrTy);
Mon P Wang20adc9d2010-04-04 03:10:48 +0000191 Instruction *L = new LoadInst(Src, "tmp", MI->isVolatile(), SrcAlign);
Chris Lattner753a2b42010-01-05 07:32:13 +0000192 InsertNewInstBefore(L, *MI);
Mon P Wang20adc9d2010-04-04 03:10:48 +0000193 InsertNewInstBefore(new StoreInst(L, Dest, MI->isVolatile(), DstAlign),
194 *MI);
Chris Lattner753a2b42010-01-05 07:32:13 +0000195
196 // Set the size of the copy to 0, it will be deleted on the next iteration.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000197 MI->setOperand(2, Constant::getNullValue(MemOpLength->getType()));
Chris Lattner753a2b42010-01-05 07:32:13 +0000198 return MI;
199}
200
201Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
202 unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest());
203 if (MI->getAlignment() < Alignment) {
204 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
205 Alignment, false));
206 return MI;
207 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000208
Chris Lattner753a2b42010-01-05 07:32:13 +0000209 // Extract the length and alignment and fill if they are constant.
210 ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
211 ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000212 if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8))
Chris Lattner753a2b42010-01-05 07:32:13 +0000213 return 0;
214 uint64_t Len = LenC->getZExtValue();
215 Alignment = MI->getAlignment();
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000216
Chris Lattner753a2b42010-01-05 07:32:13 +0000217 // If the length is zero, this is a no-op
218 if (Len == 0) return MI; // memset(d,c,0,a) -> noop
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000219
Chris Lattner753a2b42010-01-05 07:32:13 +0000220 // memset(s,c,n) -> store s, c (for n=1,2,4,8)
221 if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
222 const Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8.
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000223
Chris Lattner753a2b42010-01-05 07:32:13 +0000224 Value *Dest = MI->getDest();
225 Dest = Builder->CreateBitCast(Dest, PointerType::getUnqual(ITy));
226
227 // Alignment 0 is identity for alignment 1 for memset, but not store.
228 if (Alignment == 0) Alignment = 1;
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000229
Chris Lattner753a2b42010-01-05 07:32:13 +0000230 // Extract the fill value and store.
231 uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
232 InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill),
233 Dest, false, Alignment), *MI);
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000234
Chris Lattner753a2b42010-01-05 07:32:13 +0000235 // Set the size of the copy to 0, it will be deleted on the next iteration.
236 MI->setLength(Constant::getNullValue(LenC->getType()));
237 return MI;
238 }
239
240 return 0;
241}
242
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000243/// visitCallInst - CallInst simplification. This mostly only handles folding
Chris Lattner753a2b42010-01-05 07:32:13 +0000244/// of intrinsic instructions. For normal calls, it allows visitCallSite to do
245/// the heavy lifting.
246///
247Instruction *InstCombiner::visitCallInst(CallInst &CI) {
248 if (isFreeCall(&CI))
249 return visitFree(CI);
250
251 // If the caller function is nounwind, mark the call as nounwind, even if the
252 // callee isn't.
253 if (CI.getParent()->getParent()->doesNotThrow() &&
254 !CI.doesNotThrow()) {
255 CI.setDoesNotThrow();
256 return &CI;
257 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000258
Chris Lattner753a2b42010-01-05 07:32:13 +0000259 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
260 if (!II) return visitCallSite(&CI);
Gabor Greif2ff961f2010-04-15 20:51:13 +0000261
Chris Lattner753a2b42010-01-05 07:32:13 +0000262 // Intrinsics cannot occur in an invoke, so handle them here instead of in
263 // visitCallSite.
264 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
265 bool Changed = false;
266
267 // memmove/cpy/set of zero bytes is a noop.
268 if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
269 if (NumBytes->isNullValue()) return EraseInstFromFunction(CI);
270
271 if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
272 if (CI->getZExtValue() == 1) {
273 // Replace the instruction with just byte operations. We would
274 // transform other cases to loads/stores, but we don't know if
275 // alignment is sufficient.
276 }
277 }
278
279 // If we have a memmove and the source operation is a constant global,
280 // then the source and dest pointers can't alias, so we can change this
281 // into a call to memcpy.
282 if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
283 if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
284 if (GVSrc->isConstant()) {
Gabor Greif2ff961f2010-04-15 20:51:13 +0000285 Module *M = MMI->getParent()->getParent()->getParent();
Chris Lattner753a2b42010-01-05 07:32:13 +0000286 Intrinsic::ID MemCpyID = Intrinsic::memcpy;
Gabor Greif2ff961f2010-04-15 20:51:13 +0000287 const Type *Tys[3] = { CI.getOperand(0)->getType(),
288 CI.getOperand(1)->getType(),
289 CI.getOperand(2)->getType() };
290 MMI->setCalledFunction(
Mon P Wang20adc9d2010-04-04 03:10:48 +0000291 Intrinsic::getDeclaration(M, MemCpyID, Tys, 3));
Chris Lattner753a2b42010-01-05 07:32:13 +0000292 Changed = true;
293 }
294 }
295
296 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
297 // memmove(x,x,size) -> noop.
298 if (MTI->getSource() == MTI->getDest())
299 return EraseInstFromFunction(CI);
Chris Lattner753a2b42010-01-05 07:32:13 +0000300
Gabor Greif2ff961f2010-04-15 20:51:13 +0000301 // If we can determine a pointer alignment that is bigger than currently
302 // set, update the alignment.
303 if (Instruction *I = SimplifyMemTransfer(MTI))
Chris Lattner753a2b42010-01-05 07:32:13 +0000304 return I;
305 } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
306 if (Instruction *I = SimplifyMemSet(MSI))
307 return I;
308 }
Gabor Greif2ff961f2010-04-15 20:51:13 +0000309
Chris Lattner753a2b42010-01-05 07:32:13 +0000310 if (Changed) return II;
311 }
Gabor Greif2ff961f2010-04-15 20:51:13 +0000312
Chris Lattner753a2b42010-01-05 07:32:13 +0000313 switch (II->getIntrinsicID()) {
314 default: break;
Eric Christopher415326b2010-02-09 21:24:27 +0000315 case Intrinsic::objectsize: {
Eric Christopher26d0e892010-02-11 01:48:54 +0000316 // We need target data for just about everything so depend on it.
Eric Christopher415326b2010-02-09 21:24:27 +0000317 if (!TD) break;
Eric Christopher26d0e892010-02-11 01:48:54 +0000318
Evan Chenga8623262010-03-05 20:47:23 +0000319 const Type *ReturnTy = CI.getType();
Gabor Greif2ff961f2010-04-15 20:51:13 +0000320 bool Min = (cast<ConstantInt>(II->getOperand(1))->getZExtValue() == 1);
Evan Chenga8623262010-03-05 20:47:23 +0000321
Eric Christopher26d0e892010-02-11 01:48:54 +0000322 // Get to the real allocated thing and offset as fast as possible.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000323 Value *Op1 = II->getOperand(0)->stripPointerCasts();
Eric Christopher415326b2010-02-09 21:24:27 +0000324
Eric Christopher26d0e892010-02-11 01:48:54 +0000325 // If we've stripped down to a single global variable that we
326 // can know the size of then just return that.
Eric Christopher415326b2010-02-09 21:24:27 +0000327 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op1)) {
328 if (GV->hasDefinitiveInitializer()) {
329 Constant *C = GV->getInitializer();
Evan Chenga8623262010-03-05 20:47:23 +0000330 uint64_t GlobalSize = TD->getTypeAllocSize(C->getType());
331 return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, GlobalSize));
Eric Christopher415326b2010-02-09 21:24:27 +0000332 } else {
Evan Chenga8623262010-03-05 20:47:23 +0000333 // Can't determine size of the GV.
Eric Christopher415326b2010-02-09 21:24:27 +0000334 Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
335 return ReplaceInstUsesWith(CI, RetVal);
336 }
Evan Chenga8623262010-03-05 20:47:23 +0000337 } else if (AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
338 // Get alloca size.
339 if (AI->getAllocatedType()->isSized()) {
340 uint64_t AllocaSize = TD->getTypeAllocSize(AI->getAllocatedType());
341 if (AI->isArrayAllocation()) {
342 const ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize());
343 if (!C) break;
344 AllocaSize *= C->getZExtValue();
345 }
346 return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, AllocaSize));
347 }
Evan Cheng687fed32010-03-08 22:54:36 +0000348 } else if (CallInst *MI = extractMallocCall(Op1)) {
349 const Type* MallocType = getMallocAllocatedType(MI);
350 // Get alloca size.
351 if (MallocType && MallocType->isSized()) {
352 if (Value *NElems = getMallocArraySize(MI, TD, true)) {
353 if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
354 return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy,
355 (NElements->getZExtValue() * TD->getTypeAllocSize(MallocType))));
356 }
357 }
Evan Chenga8623262010-03-05 20:47:23 +0000358 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op1)) {
Eric Christopher26d0e892010-02-11 01:48:54 +0000359 // Only handle constant GEPs here.
360 if (CE->getOpcode() != Instruction::GetElementPtr) break;
361 GEPOperator *GEP = cast<GEPOperator>(CE);
362
Eric Christopherdfdddd82010-02-11 17:44:04 +0000363 // Make sure we're not a constant offset from an external
364 // global.
365 Value *Operand = GEP->getPointerOperand();
Eric Christopher77ffe3b2010-02-13 23:38:01 +0000366 Operand = Operand->stripPointerCasts();
Eric Christopherdfdddd82010-02-11 17:44:04 +0000367 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand))
368 if (!GV->hasDefinitiveInitializer()) break;
Eric Christopher27ceaa12010-03-06 10:50:38 +0000369
Eric Christopher77ffe3b2010-02-13 23:38:01 +0000370 // Get what we're pointing to and its size.
371 const PointerType *BaseType =
Eric Christopherdfdddd82010-02-11 17:44:04 +0000372 cast<PointerType>(Operand->getType());
Evan Cheng6e5dfd42010-02-22 23:34:00 +0000373 uint64_t Size = TD->getTypeAllocSize(BaseType->getElementType());
Eric Christopher26d0e892010-02-11 01:48:54 +0000374
Eric Christopher77ffe3b2010-02-13 23:38:01 +0000375 // Get the current byte offset into the thing. Use the original
376 // operand in case we're looking through a bitcast.
Eric Christopher26d0e892010-02-11 01:48:54 +0000377 SmallVector<Value*, 8> Ops(CE->op_begin()+1, CE->op_end());
Eric Christopher77ffe3b2010-02-13 23:38:01 +0000378 const PointerType *OffsetType =
379 cast<PointerType>(GEP->getPointerOperand()->getType());
Evan Cheng6e5dfd42010-02-22 23:34:00 +0000380 uint64_t Offset = TD->getIndexedOffset(OffsetType, &Ops[0], Ops.size());
Eric Christopher26d0e892010-02-11 01:48:54 +0000381
Evan Cheng6e5dfd42010-02-22 23:34:00 +0000382 if (Size < Offset) {
383 // Out of bound reference? Negative index normalized to large
384 // index? Just return "I don't know".
385 Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
386 return ReplaceInstUsesWith(CI, RetVal);
387 }
Eric Christopher26d0e892010-02-11 01:48:54 +0000388
389 Constant *RetVal = ConstantInt::get(ReturnTy, Size-Offset);
390 return ReplaceInstUsesWith(CI, RetVal);
Eric Christopher27ceaa12010-03-06 10:50:38 +0000391 }
Evan Chenga8623262010-03-05 20:47:23 +0000392
393 // Do not return "I don't know" here. Later optimization passes could
394 // make it possible to evaluate objectsize to a constant.
Evan Chengf79d6242010-03-05 01:22:47 +0000395 break;
Eric Christopher415326b2010-02-09 21:24:27 +0000396 }
Chris Lattner753a2b42010-01-05 07:32:13 +0000397 case Intrinsic::bswap:
398 // bswap(bswap(x)) -> x
Gabor Greif2ff961f2010-04-15 20:51:13 +0000399 if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(0)))
Chris Lattner753a2b42010-01-05 07:32:13 +0000400 if (Operand->getIntrinsicID() == Intrinsic::bswap)
Gabor Greif2ff961f2010-04-15 20:51:13 +0000401 return ReplaceInstUsesWith(CI, Operand->getOperand(0));
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000402
Chris Lattner753a2b42010-01-05 07:32:13 +0000403 // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
Gabor Greif2ff961f2010-04-15 20:51:13 +0000404 if (TruncInst *TI = dyn_cast<TruncInst>(II->getOperand(0))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000405 if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0)))
406 if (Operand->getIntrinsicID() == Intrinsic::bswap) {
407 unsigned C = Operand->getType()->getPrimitiveSizeInBits() -
408 TI->getType()->getPrimitiveSizeInBits();
409 Value *CV = ConstantInt::get(Operand->getType(), C);
Gabor Greif2ff961f2010-04-15 20:51:13 +0000410 Value *V = Builder->CreateLShr(Operand->getOperand(0), CV);
Chris Lattner753a2b42010-01-05 07:32:13 +0000411 return new TruncInst(V, TI->getType());
412 }
413 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000414
Chris Lattner753a2b42010-01-05 07:32:13 +0000415 break;
416 case Intrinsic::powi:
Gabor Greif2ff961f2010-04-15 20:51:13 +0000417 if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getOperand(1))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000418 // powi(x, 0) -> 1.0
419 if (Power->isZero())
420 return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
421 // powi(x, 1) -> x
422 if (Power->isOne())
Gabor Greif2ff961f2010-04-15 20:51:13 +0000423 return ReplaceInstUsesWith(CI, II->getOperand(0));
Chris Lattner753a2b42010-01-05 07:32:13 +0000424 // powi(x, -1) -> 1/x
425 if (Power->isAllOnesValue())
426 return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
Gabor Greif2ff961f2010-04-15 20:51:13 +0000427 II->getOperand(0));
Chris Lattner753a2b42010-01-05 07:32:13 +0000428 }
429 break;
430 case Intrinsic::cttz: {
431 // If all bits below the first known one are known zero,
432 // this value is constant.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000433 const IntegerType *IT = cast<IntegerType>(II->getOperand(0)->getType());
Chris Lattner753a2b42010-01-05 07:32:13 +0000434 uint32_t BitWidth = IT->getBitWidth();
435 APInt KnownZero(BitWidth, 0);
436 APInt KnownOne(BitWidth, 0);
Gabor Greif2ff961f2010-04-15 20:51:13 +0000437 ComputeMaskedBits(II->getOperand(0), APInt::getAllOnesValue(BitWidth),
Chris Lattner753a2b42010-01-05 07:32:13 +0000438 KnownZero, KnownOne);
439 unsigned TrailingZeros = KnownOne.countTrailingZeros();
440 APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros));
441 if ((Mask & KnownZero) == Mask)
442 return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
443 APInt(BitWidth, TrailingZeros)));
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000444
Chris Lattner753a2b42010-01-05 07:32:13 +0000445 }
446 break;
447 case Intrinsic::ctlz: {
448 // If all bits above the first known one are known zero,
449 // this value is constant.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000450 const IntegerType *IT = cast<IntegerType>(II->getOperand(0)->getType());
Chris Lattner753a2b42010-01-05 07:32:13 +0000451 uint32_t BitWidth = IT->getBitWidth();
452 APInt KnownZero(BitWidth, 0);
453 APInt KnownOne(BitWidth, 0);
Gabor Greif2ff961f2010-04-15 20:51:13 +0000454 ComputeMaskedBits(II->getOperand(0), APInt::getAllOnesValue(BitWidth),
Chris Lattner753a2b42010-01-05 07:32:13 +0000455 KnownZero, KnownOne);
456 unsigned LeadingZeros = KnownOne.countLeadingZeros();
457 APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros));
458 if ((Mask & KnownZero) == Mask)
459 return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
460 APInt(BitWidth, LeadingZeros)));
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000461
Chris Lattner753a2b42010-01-05 07:32:13 +0000462 }
463 break;
464 case Intrinsic::uadd_with_overflow: {
Gabor Greif2ff961f2010-04-15 20:51:13 +0000465 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
466 const IntegerType *IT = cast<IntegerType>(II->getOperand(0)->getType());
Chris Lattner753a2b42010-01-05 07:32:13 +0000467 uint32_t BitWidth = IT->getBitWidth();
468 APInt Mask = APInt::getSignBit(BitWidth);
469 APInt LHSKnownZero(BitWidth, 0);
470 APInt LHSKnownOne(BitWidth, 0);
471 ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne);
472 bool LHSKnownNegative = LHSKnownOne[BitWidth - 1];
473 bool LHSKnownPositive = LHSKnownZero[BitWidth - 1];
474
475 if (LHSKnownNegative || LHSKnownPositive) {
476 APInt RHSKnownZero(BitWidth, 0);
477 APInt RHSKnownOne(BitWidth, 0);
478 ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne);
479 bool RHSKnownNegative = RHSKnownOne[BitWidth - 1];
480 bool RHSKnownPositive = RHSKnownZero[BitWidth - 1];
481 if (LHSKnownNegative && RHSKnownNegative) {
482 // The sign bit is set in both cases: this MUST overflow.
483 // Create a simple add instruction, and insert it into the struct.
484 Instruction *Add = BinaryOperator::CreateAdd(LHS, RHS, "", &CI);
485 Worklist.Add(Add);
486 Constant *V[] = {
487 UndefValue::get(LHS->getType()),ConstantInt::getTrue(II->getContext())
488 };
489 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
490 return InsertValueInst::Create(Struct, Add, 0);
491 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000492
Chris Lattner753a2b42010-01-05 07:32:13 +0000493 if (LHSKnownPositive && RHSKnownPositive) {
494 // The sign bit is clear in both cases: this CANNOT overflow.
495 // Create a simple add instruction, and insert it into the struct.
496 Instruction *Add = BinaryOperator::CreateNUWAdd(LHS, RHS, "", &CI);
497 Worklist.Add(Add);
498 Constant *V[] = {
499 UndefValue::get(LHS->getType()),
500 ConstantInt::getFalse(II->getContext())
501 };
502 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
503 return InsertValueInst::Create(Struct, Add, 0);
504 }
505 }
506 }
507 // FALL THROUGH uadd into sadd
508 case Intrinsic::sadd_with_overflow:
509 // Canonicalize constants into the RHS.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000510 if (isa<Constant>(II->getOperand(0)) &&
511 !isa<Constant>(II->getOperand(1))) {
512 Value *LHS = II->getOperand(0);
513 II->setOperand(0, II->getOperand(1));
514 II->setOperand(1, LHS);
Chris Lattner753a2b42010-01-05 07:32:13 +0000515 return II;
516 }
517
518 // X + undef -> undef
Gabor Greif2ff961f2010-04-15 20:51:13 +0000519 if (isa<UndefValue>(II->getOperand(1)))
Chris Lattner753a2b42010-01-05 07:32:13 +0000520 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000521
Gabor Greif2ff961f2010-04-15 20:51:13 +0000522 if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(1))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000523 // X + 0 -> {X, false}
524 if (RHS->isZero()) {
525 Constant *V[] = {
526 UndefValue::get(II->getOperand(0)->getType()),
527 ConstantInt::getFalse(II->getContext())
528 };
529 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
Gabor Greif2ff961f2010-04-15 20:51:13 +0000530 return InsertValueInst::Create(Struct, II->getOperand(0), 0);
Chris Lattner753a2b42010-01-05 07:32:13 +0000531 }
532 }
533 break;
534 case Intrinsic::usub_with_overflow:
535 case Intrinsic::ssub_with_overflow:
536 // undef - X -> undef
537 // X - undef -> undef
Gabor Greif2ff961f2010-04-15 20:51:13 +0000538 if (isa<UndefValue>(II->getOperand(0)) ||
539 isa<UndefValue>(II->getOperand(1)))
Chris Lattner753a2b42010-01-05 07:32:13 +0000540 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000541
Gabor Greif2ff961f2010-04-15 20:51:13 +0000542 if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(1))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000543 // X - 0 -> {X, false}
544 if (RHS->isZero()) {
545 Constant *V[] = {
Gabor Greif2ff961f2010-04-15 20:51:13 +0000546 UndefValue::get(II->getOperand(0)->getType()),
Chris Lattner753a2b42010-01-05 07:32:13 +0000547 ConstantInt::getFalse(II->getContext())
548 };
549 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
Gabor Greif2ff961f2010-04-15 20:51:13 +0000550 return InsertValueInst::Create(Struct, II->getOperand(0), 0);
Chris Lattner753a2b42010-01-05 07:32:13 +0000551 }
552 }
553 break;
554 case Intrinsic::umul_with_overflow:
555 case Intrinsic::smul_with_overflow:
556 // Canonicalize constants into the RHS.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000557 if (isa<Constant>(II->getOperand(0)) &&
558 !isa<Constant>(II->getOperand(1))) {
559 Value *LHS = II->getOperand(0);
560 II->setOperand(0, II->getOperand(1));
561 II->setOperand(1, LHS);
Chris Lattner753a2b42010-01-05 07:32:13 +0000562 return II;
563 }
564
565 // X * undef -> undef
Gabor Greif2ff961f2010-04-15 20:51:13 +0000566 if (isa<UndefValue>(II->getOperand(1)))
Chris Lattner753a2b42010-01-05 07:32:13 +0000567 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000568
Gabor Greif2ff961f2010-04-15 20:51:13 +0000569 if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getOperand(1))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000570 // X*0 -> {0, false}
571 if (RHSI->isZero())
572 return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType()));
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000573
Chris Lattner753a2b42010-01-05 07:32:13 +0000574 // X * 1 -> {X, false}
575 if (RHSI->equalsInt(1)) {
576 Constant *V[] = {
Gabor Greif2ff961f2010-04-15 20:51:13 +0000577 UndefValue::get(II->getOperand(0)->getType()),
Chris Lattner753a2b42010-01-05 07:32:13 +0000578 ConstantInt::getFalse(II->getContext())
579 };
580 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
Gabor Greif2ff961f2010-04-15 20:51:13 +0000581 return InsertValueInst::Create(Struct, II->getOperand(0), 0);
Chris Lattner753a2b42010-01-05 07:32:13 +0000582 }
583 }
584 break;
585 case Intrinsic::ppc_altivec_lvx:
586 case Intrinsic::ppc_altivec_lvxl:
587 case Intrinsic::x86_sse_loadu_ps:
588 case Intrinsic::x86_sse2_loadu_pd:
589 case Intrinsic::x86_sse2_loadu_dq:
590 // Turn PPC lvx -> load if the pointer is known aligned.
591 // Turn X86 loadups -> load if the pointer is known aligned.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000592 if (GetOrEnforceKnownAlignment(II->getOperand(0), 16) >= 16) {
593 Value *Ptr = Builder->CreateBitCast(II->getOperand(0),
Chris Lattner753a2b42010-01-05 07:32:13 +0000594 PointerType::getUnqual(II->getType()));
595 return new LoadInst(Ptr);
596 }
597 break;
598 case Intrinsic::ppc_altivec_stvx:
599 case Intrinsic::ppc_altivec_stvxl:
600 // Turn stvx -> store if the pointer is known aligned.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000601 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000602 const Type *OpPtrTy =
Gabor Greif2ff961f2010-04-15 20:51:13 +0000603 PointerType::getUnqual(II->getOperand(0)->getType());
604 Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy);
605 return new StoreInst(II->getOperand(0), Ptr);
Chris Lattner753a2b42010-01-05 07:32:13 +0000606 }
607 break;
608 case Intrinsic::x86_sse_storeu_ps:
609 case Intrinsic::x86_sse2_storeu_pd:
610 case Intrinsic::x86_sse2_storeu_dq:
611 // Turn X86 storeu -> store if the pointer is known aligned.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000612 if (GetOrEnforceKnownAlignment(II->getOperand(0), 16) >= 16) {
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000613 const Type *OpPtrTy =
Gabor Greif2ff961f2010-04-15 20:51:13 +0000614 PointerType::getUnqual(II->getOperand(1)->getType());
615 Value *Ptr = Builder->CreateBitCast(II->getOperand(0), OpPtrTy);
616 return new StoreInst(II->getOperand(1), Ptr);
Chris Lattner753a2b42010-01-05 07:32:13 +0000617 }
618 break;
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000619
Chris Lattner753a2b42010-01-05 07:32:13 +0000620 case Intrinsic::x86_sse_cvttss2si: {
621 // These intrinsics only demands the 0th element of its input vector. If
622 // we can simplify the input based on that, do so now.
623 unsigned VWidth =
Gabor Greif2ff961f2010-04-15 20:51:13 +0000624 cast<VectorType>(II->getOperand(0)->getType())->getNumElements();
Chris Lattner753a2b42010-01-05 07:32:13 +0000625 APInt DemandedElts(VWidth, 1);
626 APInt UndefElts(VWidth, 0);
Gabor Greif2ff961f2010-04-15 20:51:13 +0000627 if (Value *V = SimplifyDemandedVectorElts(II->getOperand(0), DemandedElts,
Chris Lattner753a2b42010-01-05 07:32:13 +0000628 UndefElts)) {
Gabor Greif2ff961f2010-04-15 20:51:13 +0000629 II->setOperand(0, V);
Chris Lattner753a2b42010-01-05 07:32:13 +0000630 return II;
631 }
632 break;
633 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000634
Chris Lattner753a2b42010-01-05 07:32:13 +0000635 case Intrinsic::ppc_altivec_vperm:
636 // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000637 if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(2))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000638 assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000639
Chris Lattner753a2b42010-01-05 07:32:13 +0000640 // Check that all of the elements are integer constants or undefs.
641 bool AllEltsOk = true;
642 for (unsigned i = 0; i != 16; ++i) {
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000643 if (!isa<ConstantInt>(Mask->getOperand(i)) &&
Chris Lattner753a2b42010-01-05 07:32:13 +0000644 !isa<UndefValue>(Mask->getOperand(i))) {
645 AllEltsOk = false;
646 break;
647 }
648 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000649
Chris Lattner753a2b42010-01-05 07:32:13 +0000650 if (AllEltsOk) {
651 // Cast the input vectors to byte vectors.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000652 Value *Op0 = Builder->CreateBitCast(II->getOperand(0), Mask->getType());
653 Value *Op1 = Builder->CreateBitCast(II->getOperand(1), Mask->getType());
Chris Lattner753a2b42010-01-05 07:32:13 +0000654 Value *Result = UndefValue::get(Op0->getType());
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000655
Chris Lattner753a2b42010-01-05 07:32:13 +0000656 // Only extract each element once.
657 Value *ExtractedElts[32];
658 memset(ExtractedElts, 0, sizeof(ExtractedElts));
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000659
Chris Lattner753a2b42010-01-05 07:32:13 +0000660 for (unsigned i = 0; i != 16; ++i) {
661 if (isa<UndefValue>(Mask->getOperand(i)))
662 continue;
663 unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue();
664 Idx &= 31; // Match the hardware behavior.
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000665
Chris Lattner753a2b42010-01-05 07:32:13 +0000666 if (ExtractedElts[Idx] == 0) {
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000667 ExtractedElts[Idx] =
668 Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1,
Chris Lattner753a2b42010-01-05 07:32:13 +0000669 ConstantInt::get(Type::getInt32Ty(II->getContext()),
670 Idx&15, false), "tmp");
671 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000672
Chris Lattner753a2b42010-01-05 07:32:13 +0000673 // Insert this value into the result vector.
674 Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx],
675 ConstantInt::get(Type::getInt32Ty(II->getContext()),
676 i, false), "tmp");
677 }
678 return CastInst::Create(Instruction::BitCast, Result, CI.getType());
679 }
680 }
681 break;
682
683 case Intrinsic::stackrestore: {
684 // If the save is right next to the restore, remove the restore. This can
685 // happen when variable allocas are DCE'd.
Gabor Greif2ff961f2010-04-15 20:51:13 +0000686 if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(0))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000687 if (SS->getIntrinsicID() == Intrinsic::stacksave) {
688 BasicBlock::iterator BI = SS;
689 if (&*++BI == II)
690 return EraseInstFromFunction(CI);
691 }
692 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000693
Chris Lattner753a2b42010-01-05 07:32:13 +0000694 // Scan down this block to see if there is another stack restore in the
695 // same block without an intervening call/alloca.
696 BasicBlock::iterator BI = II;
697 TerminatorInst *TI = II->getParent()->getTerminator();
698 bool CannotRemove = false;
699 for (++BI; &*BI != TI; ++BI) {
700 if (isa<AllocaInst>(BI) || isMalloc(BI)) {
701 CannotRemove = true;
702 break;
703 }
704 if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
705 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
706 // If there is a stackrestore below this one, remove this one.
707 if (II->getIntrinsicID() == Intrinsic::stackrestore)
708 return EraseInstFromFunction(CI);
709 // Otherwise, ignore the intrinsic.
710 } else {
711 // If we found a non-intrinsic call, we can't remove the stack
712 // restore.
713 CannotRemove = true;
714 break;
715 }
716 }
717 }
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000718
Chris Lattner753a2b42010-01-05 07:32:13 +0000719 // If the stack restore is in a return/unwind block and if there are no
720 // allocas or calls between the restore and the return, nuke the restore.
721 if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI)))
722 return EraseInstFromFunction(CI);
723 break;
724 }
Chris Lattner753a2b42010-01-05 07:32:13 +0000725 }
726
727 return visitCallSite(II);
728}
729
730// InvokeInst simplification
731//
732Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
733 return visitCallSite(&II);
734}
735
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000736/// isSafeToEliminateVarargsCast - If this cast does not affect the value
Chris Lattner753a2b42010-01-05 07:32:13 +0000737/// passed through the varargs area, we can eliminate the use of the cast.
738static bool isSafeToEliminateVarargsCast(const CallSite CS,
739 const CastInst * const CI,
740 const TargetData * const TD,
741 const int ix) {
742 if (!CI->isLosslessCast())
743 return false;
744
745 // The size of ByVal arguments is derived from the type, so we
746 // can't change to a type with a different size. If the size were
747 // passed explicitly we could avoid this check.
748 if (!CS.paramHasAttr(ix, Attribute::ByVal))
749 return true;
750
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000751 const Type* SrcTy =
Chris Lattner753a2b42010-01-05 07:32:13 +0000752 cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
753 const Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
754 if (!SrcTy->isSized() || !DstTy->isSized())
755 return false;
756 if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy))
757 return false;
758 return true;
759}
760
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000761namespace {
762class InstCombineFortifiedLibCalls : public SimplifyFortifiedLibCalls {
763 InstCombiner *IC;
764protected:
765 void replaceCall(Value *With) {
766 NewInstruction = IC->ReplaceInstUsesWith(*CI, With);
767 }
768 bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const {
769 if (ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(SizeCIOp))) {
770 if (SizeCI->isAllOnesValue())
771 return true;
772 if (isString)
773 return SizeCI->getZExtValue() >=
774 GetStringLength(CI->getOperand(SizeArgOp));
775 if (ConstantInt *Arg = dyn_cast<ConstantInt>(CI->getOperand(SizeArgOp)))
Evan Cheng9d8f0022010-03-23 06:06:09 +0000776 return SizeCI->getZExtValue() >= Arg->getZExtValue();
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000777 }
778 return false;
779 }
780public:
781 InstCombineFortifiedLibCalls(InstCombiner *IC) : IC(IC), NewInstruction(0) { }
782 Instruction *NewInstruction;
783};
784} // end anonymous namespace
785
Eric Christopher27ceaa12010-03-06 10:50:38 +0000786// Try to fold some different type of calls here.
787// Currently we're only working with the checking functions, memcpy_chk,
788// mempcpy_chk, memmove_chk, memset_chk, strcpy_chk, stpcpy_chk, strncpy_chk,
789// strcat_chk and strncat_chk.
790Instruction *InstCombiner::tryOptimizeCall(CallInst *CI, const TargetData *TD) {
791 if (CI->getCalledFunction() == 0) return 0;
Eric Christopher27ceaa12010-03-06 10:50:38 +0000792
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000793 InstCombineFortifiedLibCalls Simplifier(this);
794 Simplifier.fold(CI, TD);
795 return Simplifier.NewInstruction;
Eric Christopher27ceaa12010-03-06 10:50:38 +0000796}
797
Chris Lattner753a2b42010-01-05 07:32:13 +0000798// visitCallSite - Improvements for call and invoke instructions.
799//
800Instruction *InstCombiner::visitCallSite(CallSite CS) {
801 bool Changed = false;
802
803 // If the callee is a constexpr cast of a function, attempt to move the cast
804 // to the arguments of the call/invoke.
805 if (transformConstExprCastCall(CS)) return 0;
806
807 Value *Callee = CS.getCalledValue();
808
809 if (Function *CalleeF = dyn_cast<Function>(Callee))
Chris Lattnerd5695612010-02-01 18:11:34 +0000810 // If the call and callee calling conventions don't match, this call must
811 // be unreachable, as the call is undefined.
812 if (CalleeF->getCallingConv() != CS.getCallingConv() &&
813 // Only do this for calls to a function with a body. A prototype may
814 // not actually end up matching the implementation's calling conv for a
815 // variety of reasons (e.g. it may be written in assembly).
816 !CalleeF->isDeclaration()) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000817 Instruction *OldCall = CS.getInstruction();
Chris Lattner753a2b42010-01-05 07:32:13 +0000818 new StoreInst(ConstantInt::getTrue(Callee->getContext()),
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000819 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
Chris Lattner753a2b42010-01-05 07:32:13 +0000820 OldCall);
821 // If OldCall dues not return void then replaceAllUsesWith undef.
822 // This allows ValueHandlers and custom metadata to adjust itself.
823 if (!OldCall->getType()->isVoidTy())
824 OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
Chris Lattner830f3f22010-02-01 18:04:58 +0000825 if (isa<CallInst>(OldCall))
Chris Lattner753a2b42010-01-05 07:32:13 +0000826 return EraseInstFromFunction(*OldCall);
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000827
Chris Lattner830f3f22010-02-01 18:04:58 +0000828 // We cannot remove an invoke, because it would change the CFG, just
829 // change the callee to a null pointer.
Gabor Greif654c06f2010-03-20 21:00:25 +0000830 cast<InvokeInst>(OldCall)->setCalledFunction(
Chris Lattner830f3f22010-02-01 18:04:58 +0000831 Constant::getNullValue(CalleeF->getType()));
Chris Lattner753a2b42010-01-05 07:32:13 +0000832 return 0;
833 }
834
835 if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
836 // This instruction is not reachable, just remove it. We insert a store to
837 // undef so that we know that this code is not reachable, despite the fact
838 // that we can't modify the CFG here.
839 new StoreInst(ConstantInt::getTrue(Callee->getContext()),
840 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
841 CS.getInstruction());
842
Gabor Greif2ff961f2010-04-15 20:51:13 +0000843 // If CS does not return void then replaceAllUsesWith undef.
Chris Lattner753a2b42010-01-05 07:32:13 +0000844 // This allows ValueHandlers and custom metadata to adjust itself.
845 if (!CS.getInstruction()->getType()->isVoidTy())
846 CS.getInstruction()->
847 replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType()));
848
849 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
850 // Don't break the CFG, insert a dummy cond branch.
851 BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
852 ConstantInt::getTrue(Callee->getContext()), II);
853 }
854 return EraseInstFromFunction(*CS.getInstruction());
855 }
856
857 if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee))
858 if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0)))
859 if (In->getIntrinsicID() == Intrinsic::init_trampoline)
860 return transformCallThroughTrampoline(CS);
861
862 const PointerType *PTy = cast<PointerType>(Callee->getType());
863 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
864 if (FTy->isVarArg()) {
865 int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1);
866 // See if we can optimize any arguments passed through the varargs area of
867 // the call.
868 for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
869 E = CS.arg_end(); I != E; ++I, ++ix) {
870 CastInst *CI = dyn_cast<CastInst>(*I);
871 if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
872 *I = CI->getOperand(0);
873 Changed = true;
874 }
875 }
876 }
877
878 if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
879 // Inline asm calls cannot throw - mark them 'nounwind'.
880 CS.setDoesNotThrow();
881 Changed = true;
882 }
883
Eric Christopher27ceaa12010-03-06 10:50:38 +0000884 // Try to optimize the call if possible, we require TargetData for most of
885 // this. None of these calls are seen as possibly dead so go ahead and
886 // delete the instruction now.
887 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
888 Instruction *I = tryOptimizeCall(CI, TD);
Eric Christopher7b323a32010-03-06 10:59:25 +0000889 // If we changed something return the result, etc. Otherwise let
890 // the fallthrough check.
891 if (I) return EraseInstFromFunction(*I);
Eric Christopher27ceaa12010-03-06 10:50:38 +0000892 }
893
Chris Lattner753a2b42010-01-05 07:32:13 +0000894 return Changed ? CS.getInstruction() : 0;
895}
896
897// transformConstExprCastCall - If the callee is a constexpr cast of a function,
898// attempt to move the cast to the arguments of the call/invoke.
899//
900bool InstCombiner::transformConstExprCastCall(CallSite CS) {
901 if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
902 ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000903 if (CE->getOpcode() != Instruction::BitCast ||
Chris Lattner753a2b42010-01-05 07:32:13 +0000904 !isa<Function>(CE->getOperand(0)))
905 return false;
906 Function *Callee = cast<Function>(CE->getOperand(0));
907 Instruction *Caller = CS.getInstruction();
908 const AttrListPtr &CallerPAL = CS.getAttributes();
909
910 // Okay, this is a cast from a function to a different type. Unless doing so
911 // would cause a type conversion of one of our arguments, change this call to
912 // be a direct call with arguments casted to the appropriate types.
913 //
914 const FunctionType *FT = Callee->getFunctionType();
915 const Type *OldRetTy = Caller->getType();
916 const Type *NewRetTy = FT->getReturnType();
917
Duncan Sands1df98592010-02-16 11:11:14 +0000918 if (NewRetTy->isStructTy())
Chris Lattner753a2b42010-01-05 07:32:13 +0000919 return false; // TODO: Handle multiple return values.
920
921 // Check to see if we are changing the return type...
922 if (OldRetTy != NewRetTy) {
923 if (Callee->isDeclaration() &&
924 // Conversion is ok if changing from one pointer type to another or from
925 // a pointer to an integer of the same size.
Duncan Sands1df98592010-02-16 11:11:14 +0000926 !((OldRetTy->isPointerTy() || !TD ||
Chris Lattner753a2b42010-01-05 07:32:13 +0000927 OldRetTy == TD->getIntPtrType(Caller->getContext())) &&
Duncan Sands1df98592010-02-16 11:11:14 +0000928 (NewRetTy->isPointerTy() || !TD ||
Chris Lattner753a2b42010-01-05 07:32:13 +0000929 NewRetTy == TD->getIntPtrType(Caller->getContext()))))
930 return false; // Cannot transform this return value.
931
932 if (!Caller->use_empty() &&
933 // void -> non-void is handled specially
934 !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy))
935 return false; // Cannot transform this return value.
936
937 if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
938 Attributes RAttrs = CallerPAL.getRetAttributes();
939 if (RAttrs & Attribute::typeIncompatible(NewRetTy))
940 return false; // Attribute not compatible with transformed value.
941 }
942
943 // If the callsite is an invoke instruction, and the return value is used by
944 // a PHI node in a successor, we cannot change the return type of the call
945 // because there is no place to put the cast instruction (without breaking
946 // the critical edge). Bail out in this case.
947 if (!Caller->use_empty())
948 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
949 for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
950 UI != E; ++UI)
951 if (PHINode *PN = dyn_cast<PHINode>(*UI))
952 if (PN->getParent() == II->getNormalDest() ||
953 PN->getParent() == II->getUnwindDest())
954 return false;
955 }
956
957 unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
958 unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
959
960 CallSite::arg_iterator AI = CS.arg_begin();
961 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
962 const Type *ParamTy = FT->getParamType(i);
963 const Type *ActTy = (*AI)->getType();
964
965 if (!CastInst::isCastable(ActTy, ParamTy))
966 return false; // Cannot transform this parameter value.
967
Eric Christopher0c6a8f92010-02-03 00:21:58 +0000968 if (CallerPAL.getParamAttributes(i + 1)
Chris Lattner753a2b42010-01-05 07:32:13 +0000969 & Attribute::typeIncompatible(ParamTy))
970 return false; // Attribute not compatible with transformed value.
971
972 // Converting from one pointer type to another or between a pointer and an
973 // integer of the same size is safe even if we do not have a body.
974 bool isConvertible = ActTy == ParamTy ||
Duncan Sands1df98592010-02-16 11:11:14 +0000975 (TD && ((ParamTy->isPointerTy() ||
Chris Lattner753a2b42010-01-05 07:32:13 +0000976 ParamTy == TD->getIntPtrType(Caller->getContext())) &&
Duncan Sands1df98592010-02-16 11:11:14 +0000977 (ActTy->isPointerTy() ||
Chris Lattner753a2b42010-01-05 07:32:13 +0000978 ActTy == TD->getIntPtrType(Caller->getContext()))));
979 if (Callee->isDeclaration() && !isConvertible) return false;
980 }
981
982 if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
983 Callee->isDeclaration())
984 return false; // Do not delete arguments unless we have a function body.
985
986 if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
987 !CallerPAL.isEmpty())
988 // In this case we have more arguments than the new function type, but we
989 // won't be dropping them. Check that these extra arguments have attributes
990 // that are compatible with being a vararg call argument.
991 for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
992 if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams())
993 break;
994 Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs;
995 if (PAttrs & Attribute::VarArgsIncompatible)
996 return false;
997 }
998
999 // Okay, we decided that this is a safe thing to do: go ahead and start
1000 // inserting cast instructions as necessary...
1001 std::vector<Value*> Args;
1002 Args.reserve(NumActualArgs);
1003 SmallVector<AttributeWithIndex, 8> attrVec;
1004 attrVec.reserve(NumCommonArgs);
1005
1006 // Get any return attributes.
1007 Attributes RAttrs = CallerPAL.getRetAttributes();
1008
1009 // If the return value is not being used, the type may not be compatible
1010 // with the existing attributes. Wipe out any problematic attributes.
1011 RAttrs &= ~Attribute::typeIncompatible(NewRetTy);
1012
1013 // Add the new return attributes.
1014 if (RAttrs)
1015 attrVec.push_back(AttributeWithIndex::get(0, RAttrs));
1016
1017 AI = CS.arg_begin();
1018 for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
1019 const Type *ParamTy = FT->getParamType(i);
1020 if ((*AI)->getType() == ParamTy) {
1021 Args.push_back(*AI);
1022 } else {
1023 Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
1024 false, ParamTy, false);
1025 Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy, "tmp"));
1026 }
1027
1028 // Add any parameter attributes.
1029 if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
1030 attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
1031 }
1032
1033 // If the function takes more arguments than the call was taking, add them
1034 // now.
1035 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
1036 Args.push_back(Constant::getNullValue(FT->getParamType(i)));
1037
1038 // If we are removing arguments to the function, emit an obnoxious warning.
1039 if (FT->getNumParams() < NumActualArgs) {
1040 if (!FT->isVarArg()) {
1041 errs() << "WARNING: While resolving call to function '"
1042 << Callee->getName() << "' arguments were dropped!\n";
1043 } else {
1044 // Add all of the arguments in their promoted form to the arg list.
1045 for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
1046 const Type *PTy = getPromotedType((*AI)->getType());
1047 if (PTy != (*AI)->getType()) {
1048 // Must promote to pass through va_arg area!
1049 Instruction::CastOps opcode =
1050 CastInst::getCastOpcode(*AI, false, PTy, false);
1051 Args.push_back(Builder->CreateCast(opcode, *AI, PTy, "tmp"));
1052 } else {
1053 Args.push_back(*AI);
1054 }
1055
1056 // Add any parameter attributes.
1057 if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
1058 attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
1059 }
1060 }
1061 }
1062
1063 if (Attributes FnAttrs = CallerPAL.getFnAttributes())
1064 attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs));
1065
1066 if (NewRetTy->isVoidTy())
1067 Caller->setName(""); // Void type should not have a name.
1068
1069 const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),
1070 attrVec.end());
1071
1072 Instruction *NC;
1073 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1074 NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(),
1075 Args.begin(), Args.end(),
1076 Caller->getName(), Caller);
1077 cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
1078 cast<InvokeInst>(NC)->setAttributes(NewCallerPAL);
1079 } else {
1080 NC = CallInst::Create(Callee, Args.begin(), Args.end(),
1081 Caller->getName(), Caller);
1082 CallInst *CI = cast<CallInst>(Caller);
1083 if (CI->isTailCall())
1084 cast<CallInst>(NC)->setTailCall();
1085 cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
1086 cast<CallInst>(NC)->setAttributes(NewCallerPAL);
1087 }
1088
1089 // Insert a cast of the return type as necessary.
1090 Value *NV = NC;
1091 if (OldRetTy != NV->getType() && !Caller->use_empty()) {
1092 if (!NV->getType()->isVoidTy()) {
Eric Christopher0c6a8f92010-02-03 00:21:58 +00001093 Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false,
Chris Lattner753a2b42010-01-05 07:32:13 +00001094 OldRetTy, false);
1095 NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp");
1096
1097 // If this is an invoke instruction, we should insert it after the first
1098 // non-phi, instruction in the normal successor block.
1099 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1100 BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI();
1101 InsertNewInstBefore(NC, *I);
1102 } else {
1103 // Otherwise, it's a call, just insert cast right after the call instr
1104 InsertNewInstBefore(NC, *Caller);
1105 }
1106 Worklist.AddUsersToWorkList(*Caller);
1107 } else {
1108 NV = UndefValue::get(Caller->getType());
1109 }
1110 }
1111
1112
1113 if (!Caller->use_empty())
1114 Caller->replaceAllUsesWith(NV);
Eric Christopher0c6a8f92010-02-03 00:21:58 +00001115
Chris Lattner753a2b42010-01-05 07:32:13 +00001116 EraseInstFromFunction(*Caller);
1117 return true;
1118}
1119
1120// transformCallThroughTrampoline - Turn a call to a function created by the
1121// init_trampoline intrinsic into a direct call to the underlying function.
1122//
1123Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
1124 Value *Callee = CS.getCalledValue();
1125 const PointerType *PTy = cast<PointerType>(Callee->getType());
1126 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1127 const AttrListPtr &Attrs = CS.getAttributes();
1128
1129 // If the call already has the 'nest' attribute somewhere then give up -
1130 // otherwise 'nest' would occur twice after splicing in the chain.
1131 if (Attrs.hasAttrSomewhere(Attribute::Nest))
1132 return 0;
1133
1134 IntrinsicInst *Tramp =
1135 cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0));
1136
Gabor Greif2ff961f2010-04-15 20:51:13 +00001137 Function *NestF = cast<Function>(Tramp->getOperand(1)->stripPointerCasts());
Chris Lattner753a2b42010-01-05 07:32:13 +00001138 const PointerType *NestFPTy = cast<PointerType>(NestF->getType());
1139 const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
1140
1141 const AttrListPtr &NestAttrs = NestF->getAttributes();
1142 if (!NestAttrs.isEmpty()) {
1143 unsigned NestIdx = 1;
1144 const Type *NestTy = 0;
1145 Attributes NestAttr = Attribute::None;
1146
1147 // Look for a parameter marked with the 'nest' attribute.
1148 for (FunctionType::param_iterator I = NestFTy->param_begin(),
1149 E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
1150 if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) {
1151 // Record the parameter type and any other attributes.
1152 NestTy = *I;
1153 NestAttr = NestAttrs.getParamAttributes(NestIdx);
1154 break;
1155 }
1156
1157 if (NestTy) {
1158 Instruction *Caller = CS.getInstruction();
1159 std::vector<Value*> NewArgs;
1160 NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1);
1161
1162 SmallVector<AttributeWithIndex, 8> NewAttrs;
1163 NewAttrs.reserve(Attrs.getNumSlots() + 1);
1164
1165 // Insert the nest argument into the call argument list, which may
1166 // mean appending it. Likewise for attributes.
1167
1168 // Add any result attributes.
1169 if (Attributes Attr = Attrs.getRetAttributes())
1170 NewAttrs.push_back(AttributeWithIndex::get(0, Attr));
1171
1172 {
1173 unsigned Idx = 1;
1174 CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
1175 do {
1176 if (Idx == NestIdx) {
1177 // Add the chain argument and attributes.
Gabor Greif2ff961f2010-04-15 20:51:13 +00001178 Value *NestVal = Tramp->getOperand(2);
Chris Lattner753a2b42010-01-05 07:32:13 +00001179 if (NestVal->getType() != NestTy)
1180 NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller);
1181 NewArgs.push_back(NestVal);
1182 NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr));
1183 }
1184
1185 if (I == E)
1186 break;
1187
1188 // Add the original argument and attributes.
1189 NewArgs.push_back(*I);
1190 if (Attributes Attr = Attrs.getParamAttributes(Idx))
1191 NewAttrs.push_back
1192 (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr));
1193
1194 ++Idx, ++I;
1195 } while (1);
1196 }
1197
1198 // Add any function attributes.
1199 if (Attributes Attr = Attrs.getFnAttributes())
1200 NewAttrs.push_back(AttributeWithIndex::get(~0, Attr));
1201
1202 // The trampoline may have been bitcast to a bogus type (FTy).
1203 // Handle this by synthesizing a new function type, equal to FTy
1204 // with the chain parameter inserted.
1205
1206 std::vector<const Type*> NewTypes;
1207 NewTypes.reserve(FTy->getNumParams()+1);
1208
1209 // Insert the chain's type into the list of parameter types, which may
1210 // mean appending it.
1211 {
1212 unsigned Idx = 1;
1213 FunctionType::param_iterator I = FTy->param_begin(),
1214 E = FTy->param_end();
1215
1216 do {
1217 if (Idx == NestIdx)
1218 // Add the chain's type.
1219 NewTypes.push_back(NestTy);
1220
1221 if (I == E)
1222 break;
1223
1224 // Add the original type.
1225 NewTypes.push_back(*I);
1226
1227 ++Idx, ++I;
1228 } while (1);
1229 }
1230
1231 // Replace the trampoline call with a direct call. Let the generic
1232 // code sort out any function type mismatches.
Eric Christopher0c6a8f92010-02-03 00:21:58 +00001233 FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
Chris Lattner753a2b42010-01-05 07:32:13 +00001234 FTy->isVarArg());
1235 Constant *NewCallee =
1236 NestF->getType() == PointerType::getUnqual(NewFTy) ?
Eric Christopher0c6a8f92010-02-03 00:21:58 +00001237 NestF : ConstantExpr::getBitCast(NestF,
Chris Lattner753a2b42010-01-05 07:32:13 +00001238 PointerType::getUnqual(NewFTy));
1239 const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),
1240 NewAttrs.end());
1241
1242 Instruction *NewCaller;
1243 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1244 NewCaller = InvokeInst::Create(NewCallee,
1245 II->getNormalDest(), II->getUnwindDest(),
1246 NewArgs.begin(), NewArgs.end(),
1247 Caller->getName(), Caller);
1248 cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
1249 cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
1250 } else {
1251 NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(),
1252 Caller->getName(), Caller);
1253 if (cast<CallInst>(Caller)->isTailCall())
1254 cast<CallInst>(NewCaller)->setTailCall();
1255 cast<CallInst>(NewCaller)->
1256 setCallingConv(cast<CallInst>(Caller)->getCallingConv());
1257 cast<CallInst>(NewCaller)->setAttributes(NewPAL);
1258 }
1259 if (!Caller->getType()->isVoidTy())
1260 Caller->replaceAllUsesWith(NewCaller);
1261 Caller->eraseFromParent();
1262 Worklist.Remove(Caller);
1263 return 0;
1264 }
1265 }
1266
1267 // Replace the trampoline call with a direct call. Since there is no 'nest'
1268 // parameter, there is no need to adjust the argument list. Let the generic
1269 // code sort out any function type mismatches.
1270 Constant *NewCallee =
Eric Christopher0c6a8f92010-02-03 00:21:58 +00001271 NestF->getType() == PTy ? NestF :
Chris Lattner753a2b42010-01-05 07:32:13 +00001272 ConstantExpr::getBitCast(NestF, PTy);
1273 CS.setCalledFunction(NewCallee);
1274 return CS.getInstruction();
1275}
Eric Christopher0c6a8f92010-02-03 00:21:58 +00001276