blob: 1b389b92e8daf612d540ac1ff46ea41ba076bc93 [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"
Chris Lattner753a2b42010-01-05 07:32:13 +000015#include "llvm/Support/CallSite.h"
16#include "llvm/Target/TargetData.h"
17#include "llvm/Analysis/MemoryBuiltins.h"
Eric Christopher27ceaa12010-03-06 10:50:38 +000018#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chris Lattner687140c2010-12-25 20:37:57 +000019#include "llvm/Transforms/Utils/Local.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.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000024static Type *getPromotedType(Type *Ty) {
25 if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
Chris Lattner753a2b42010-01-05 07:32:13 +000026 if (ITy->getBitWidth() < 32)
27 return Type::getInt32Ty(Ty->getContext());
28 }
29 return Ty;
30}
31
Dan Gohmance52bc52012-09-13 18:19:06 +000032/// reduceToSingleValueType - Given an aggregate type which ultimately holds a
33/// single scalar element, like {{{type}}} or [1 x type], return type.
34static Type *reduceToSingleValueType(Type *T) {
35 while (!T->isSingleValueType()) {
36 if (StructType *STy = dyn_cast<StructType>(T)) {
37 if (STy->getNumElements() == 1)
38 T = STy->getElementType(0);
39 else
40 break;
41 } else if (ArrayType *ATy = dyn_cast<ArrayType>(T)) {
42 if (ATy->getNumElements() == 1)
43 T = ATy->getElementType();
44 else
45 break;
46 } else
47 break;
48 }
49
50 return T;
51}
Chris Lattner753a2b42010-01-05 07:32:13 +000052
53Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
Chris Lattner687140c2010-12-25 20:37:57 +000054 unsigned DstAlign = getKnownAlignment(MI->getArgOperand(0), TD);
55 unsigned SrcAlign = getKnownAlignment(MI->getArgOperand(1), TD);
Chris Lattner753a2b42010-01-05 07:32:13 +000056 unsigned MinAlign = std::min(DstAlign, SrcAlign);
57 unsigned CopyAlign = MI->getAlignment();
58
59 if (CopyAlign < MinAlign) {
Jim Grosbach00e403a2012-02-03 00:07:04 +000060 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
Chris Lattner753a2b42010-01-05 07:32:13 +000061 MinAlign, false));
62 return MI;
63 }
Jim Grosbach00e403a2012-02-03 00:07:04 +000064
Chris Lattner753a2b42010-01-05 07:32:13 +000065 // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
66 // load/store.
Gabor Greifbcda85c2010-06-24 13:54:33 +000067 ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getArgOperand(2));
Chris Lattner753a2b42010-01-05 07:32:13 +000068 if (MemOpLength == 0) return 0;
Jim Grosbach00e403a2012-02-03 00:07:04 +000069
Chris Lattner753a2b42010-01-05 07:32:13 +000070 // Source and destination pointer types are always "i8*" for intrinsic. See
71 // if the size is something we can handle with a single primitive load/store.
72 // A single load+store correctly handles overlapping memory in the memmove
73 // case.
Michael Liao9441ad02012-08-15 03:49:59 +000074 uint64_t Size = MemOpLength->getLimitedValue();
75 assert(Size && "0-sized memory transfering should be removed already.");
Jim Grosbach00e403a2012-02-03 00:07:04 +000076
Chris Lattner753a2b42010-01-05 07:32:13 +000077 if (Size > 8 || (Size&(Size-1)))
78 return 0; // If not 1/2/4/8 bytes, exit.
Jim Grosbach00e403a2012-02-03 00:07:04 +000079
Chris Lattner753a2b42010-01-05 07:32:13 +000080 // Use an integer load+store unless we can find something better.
Mon P Wang20adc9d2010-04-04 03:10:48 +000081 unsigned SrcAddrSp =
Gabor Greifbcda85c2010-06-24 13:54:33 +000082 cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace();
Gabor Greif4ec22582010-04-16 15:33:14 +000083 unsigned DstAddrSp =
Gabor Greifbcda85c2010-06-24 13:54:33 +000084 cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace();
Mon P Wang20adc9d2010-04-04 03:10:48 +000085
Chris Lattnerdb125cf2011-07-18 04:54:35 +000086 IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
Mon P Wang20adc9d2010-04-04 03:10:48 +000087 Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp);
88 Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp);
Jim Grosbach00e403a2012-02-03 00:07:04 +000089
Chris Lattner753a2b42010-01-05 07:32:13 +000090 // Memcpy forces the use of i8* for the source and destination. That means
91 // that if you're using memcpy to move one double around, you'll get a cast
92 // from double* to i8*. We'd much rather use a double load+store rather than
93 // an i64 load+store, here because this improves the odds that the source or
94 // dest address will be promotable. See if we can find a better type than the
95 // integer datatype.
Gabor Greifcea7ac72010-06-24 12:58:35 +000096 Value *StrippedDest = MI->getArgOperand(0)->stripPointerCasts();
97 if (StrippedDest != MI->getArgOperand(0)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000098 Type *SrcETy = cast<PointerType>(StrippedDest->getType())
Chris Lattner753a2b42010-01-05 07:32:13 +000099 ->getElementType();
100 if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
101 // The SrcETy might be something like {{{double}}} or [1 x double]. Rip
102 // down through these levels if so.
Dan Gohmance52bc52012-09-13 18:19:06 +0000103 SrcETy = reduceToSingleValueType(SrcETy);
Jim Grosbach00e403a2012-02-03 00:07:04 +0000104
Mon P Wang20adc9d2010-04-04 03:10:48 +0000105 if (SrcETy->isSingleValueType()) {
106 NewSrcPtrTy = PointerType::get(SrcETy, SrcAddrSp);
107 NewDstPtrTy = PointerType::get(SrcETy, DstAddrSp);
108 }
Chris Lattner753a2b42010-01-05 07:32:13 +0000109 }
110 }
Jim Grosbach00e403a2012-02-03 00:07:04 +0000111
Chris Lattner753a2b42010-01-05 07:32:13 +0000112 // If the memcpy/memmove provides better alignment info than we can
113 // infer, use it.
114 SrcAlign = std::max(SrcAlign, CopyAlign);
115 DstAlign = std::max(DstAlign, CopyAlign);
Jim Grosbach00e403a2012-02-03 00:07:04 +0000116
Gabor Greif9c68a7b2010-06-25 07:57:14 +0000117 Value *Src = Builder->CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy);
118 Value *Dest = Builder->CreateBitCast(MI->getArgOperand(0), NewDstPtrTy);
Eli Friedman59f15912011-05-18 19:57:14 +0000119 LoadInst *L = Builder->CreateLoad(Src, MI->isVolatile());
120 L->setAlignment(SrcAlign);
121 StoreInst *S = Builder->CreateStore(L, Dest, MI->isVolatile());
122 S->setAlignment(DstAlign);
Chris Lattner753a2b42010-01-05 07:32:13 +0000123
124 // Set the size of the copy to 0, it will be deleted on the next iteration.
Gabor Greifa90c5c72010-06-28 16:50:57 +0000125 MI->setArgOperand(2, Constant::getNullValue(MemOpLength->getType()));
Chris Lattner753a2b42010-01-05 07:32:13 +0000126 return MI;
127}
128
129Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
Chris Lattnerae47be12010-12-25 20:52:04 +0000130 unsigned Alignment = getKnownAlignment(MI->getDest(), TD);
Chris Lattner753a2b42010-01-05 07:32:13 +0000131 if (MI->getAlignment() < Alignment) {
132 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
133 Alignment, false));
134 return MI;
135 }
Jim Grosbach00e403a2012-02-03 00:07:04 +0000136
Chris Lattner753a2b42010-01-05 07:32:13 +0000137 // Extract the length and alignment and fill if they are constant.
138 ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
139 ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000140 if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8))
Chris Lattner753a2b42010-01-05 07:32:13 +0000141 return 0;
Michael Liao9441ad02012-08-15 03:49:59 +0000142 uint64_t Len = LenC->getLimitedValue();
Chris Lattner753a2b42010-01-05 07:32:13 +0000143 Alignment = MI->getAlignment();
Michael Liao9441ad02012-08-15 03:49:59 +0000144 assert(Len && "0-sized memory setting should be removed already.");
Jim Grosbach00e403a2012-02-03 00:07:04 +0000145
Chris Lattner753a2b42010-01-05 07:32:13 +0000146 // memset(s,c,n) -> store s, c (for n=1,2,4,8)
147 if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000148 Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8.
Jim Grosbach00e403a2012-02-03 00:07:04 +0000149
Chris Lattner753a2b42010-01-05 07:32:13 +0000150 Value *Dest = MI->getDest();
Mon P Wang55fb9b02010-12-20 01:05:30 +0000151 unsigned DstAddrSp = cast<PointerType>(Dest->getType())->getAddressSpace();
152 Type *NewDstPtrTy = PointerType::get(ITy, DstAddrSp);
153 Dest = Builder->CreateBitCast(Dest, NewDstPtrTy);
Chris Lattner753a2b42010-01-05 07:32:13 +0000154
155 // Alignment 0 is identity for alignment 1 for memset, but not store.
156 if (Alignment == 0) Alignment = 1;
Jim Grosbach00e403a2012-02-03 00:07:04 +0000157
Chris Lattner753a2b42010-01-05 07:32:13 +0000158 // Extract the fill value and store.
159 uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
Eli Friedman59f15912011-05-18 19:57:14 +0000160 StoreInst *S = Builder->CreateStore(ConstantInt::get(ITy, Fill), Dest,
161 MI->isVolatile());
162 S->setAlignment(Alignment);
Jim Grosbach00e403a2012-02-03 00:07:04 +0000163
Chris Lattner753a2b42010-01-05 07:32:13 +0000164 // Set the size of the copy to 0, it will be deleted on the next iteration.
165 MI->setLength(Constant::getNullValue(LenC->getType()));
166 return MI;
167 }
168
169 return 0;
170}
171
Jim Grosbach00e403a2012-02-03 00:07:04 +0000172/// visitCallInst - CallInst simplification. This mostly only handles folding
Chris Lattner753a2b42010-01-05 07:32:13 +0000173/// of intrinsic instructions. For normal calls, it allows visitCallSite to do
174/// the heavy lifting.
175///
176Instruction *InstCombiner::visitCallInst(CallInst &CI) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000177 if (isFreeCall(&CI, TLI))
Chris Lattner753a2b42010-01-05 07:32:13 +0000178 return visitFree(CI);
179
180 // If the caller function is nounwind, mark the call as nounwind, even if the
181 // callee isn't.
182 if (CI.getParent()->getParent()->doesNotThrow() &&
183 !CI.doesNotThrow()) {
184 CI.setDoesNotThrow();
185 return &CI;
186 }
Jim Grosbach00e403a2012-02-03 00:07:04 +0000187
Chris Lattner753a2b42010-01-05 07:32:13 +0000188 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
189 if (!II) return visitCallSite(&CI);
Gabor Greifcea7ac72010-06-24 12:58:35 +0000190
Chris Lattner753a2b42010-01-05 07:32:13 +0000191 // Intrinsics cannot occur in an invoke, so handle them here instead of in
192 // visitCallSite.
193 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
194 bool Changed = false;
195
196 // memmove/cpy/set of zero bytes is a noop.
197 if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
Chris Lattner6eff7512010-10-01 05:51:02 +0000198 if (NumBytes->isNullValue())
199 return EraseInstFromFunction(CI);
Chris Lattner753a2b42010-01-05 07:32:13 +0000200
201 if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
202 if (CI->getZExtValue() == 1) {
203 // Replace the instruction with just byte operations. We would
204 // transform other cases to loads/stores, but we don't know if
205 // alignment is sufficient.
206 }
207 }
Jim Grosbach00e403a2012-02-03 00:07:04 +0000208
Chris Lattner6eff7512010-10-01 05:51:02 +0000209 // No other transformations apply to volatile transfers.
210 if (MI->isVolatile())
211 return 0;
Chris Lattner753a2b42010-01-05 07:32:13 +0000212
213 // If we have a memmove and the source operation is a constant global,
214 // then the source and dest pointers can't alias, so we can change this
215 // into a call to memcpy.
216 if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
217 if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
218 if (GVSrc->isConstant()) {
Eric Christopher551754c2010-04-16 23:37:20 +0000219 Module *M = CI.getParent()->getParent()->getParent();
Chris Lattner753a2b42010-01-05 07:32:13 +0000220 Intrinsic::ID MemCpyID = Intrinsic::memcpy;
Jay Foad5fdd6c82011-07-12 14:06:48 +0000221 Type *Tys[3] = { CI.getArgOperand(0)->getType(),
222 CI.getArgOperand(1)->getType(),
223 CI.getArgOperand(2)->getType() };
Benjamin Kramereb9a85f2011-07-14 17:45:39 +0000224 CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys));
Chris Lattner753a2b42010-01-05 07:32:13 +0000225 Changed = true;
226 }
227 }
228
229 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
230 // memmove(x,x,size) -> noop.
231 if (MTI->getSource() == MTI->getDest())
232 return EraseInstFromFunction(CI);
Eric Christopher551754c2010-04-16 23:37:20 +0000233 }
Chris Lattner753a2b42010-01-05 07:32:13 +0000234
Eric Christopher551754c2010-04-16 23:37:20 +0000235 // If we can determine a pointer alignment that is bigger than currently
236 // set, update the alignment.
237 if (isa<MemTransferInst>(MI)) {
238 if (Instruction *I = SimplifyMemTransfer(MI))
Chris Lattner753a2b42010-01-05 07:32:13 +0000239 return I;
240 } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
241 if (Instruction *I = SimplifyMemSet(MSI))
242 return I;
243 }
Gabor Greifc310fcc2010-06-24 13:42:49 +0000244
Chris Lattner753a2b42010-01-05 07:32:13 +0000245 if (Changed) return II;
246 }
Jim Grosbach00e403a2012-02-03 00:07:04 +0000247
Chris Lattner753a2b42010-01-05 07:32:13 +0000248 switch (II->getIntrinsicID()) {
249 default: break;
Eric Christopher415326b2010-02-09 21:24:27 +0000250 case Intrinsic::objectsize: {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000251 uint64_t Size;
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000252 if (getObjectSize(II->getArgOperand(0), Size, TD, TLI))
Nuno Lopes9e72a792012-06-21 15:45:28 +0000253 return ReplaceInstUsesWith(CI, ConstantInt::get(CI.getType(), Size));
254 return 0;
Eric Christopher415326b2010-02-09 21:24:27 +0000255 }
Chris Lattner753a2b42010-01-05 07:32:13 +0000256 case Intrinsic::bswap:
257 // bswap(bswap(x)) -> x
Gabor Greifcea7ac72010-06-24 12:58:35 +0000258 if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getArgOperand(0)))
Chris Lattner753a2b42010-01-05 07:32:13 +0000259 if (Operand->getIntrinsicID() == Intrinsic::bswap)
Gabor Greifcea7ac72010-06-24 12:58:35 +0000260 return ReplaceInstUsesWith(CI, Operand->getArgOperand(0));
Jim Grosbach00e403a2012-02-03 00:07:04 +0000261
Chris Lattner753a2b42010-01-05 07:32:13 +0000262 // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
Gabor Greifcea7ac72010-06-24 12:58:35 +0000263 if (TruncInst *TI = dyn_cast<TruncInst>(II->getArgOperand(0))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000264 if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0)))
265 if (Operand->getIntrinsicID() == Intrinsic::bswap) {
266 unsigned C = Operand->getType()->getPrimitiveSizeInBits() -
267 TI->getType()->getPrimitiveSizeInBits();
268 Value *CV = ConstantInt::get(Operand->getType(), C);
Gabor Greifcea7ac72010-06-24 12:58:35 +0000269 Value *V = Builder->CreateLShr(Operand->getArgOperand(0), CV);
Chris Lattner753a2b42010-01-05 07:32:13 +0000270 return new TruncInst(V, TI->getType());
271 }
272 }
Jim Grosbach00e403a2012-02-03 00:07:04 +0000273
Chris Lattner753a2b42010-01-05 07:32:13 +0000274 break;
275 case Intrinsic::powi:
Gabor Greifcea7ac72010-06-24 12:58:35 +0000276 if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000277 // powi(x, 0) -> 1.0
278 if (Power->isZero())
279 return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
280 // powi(x, 1) -> x
281 if (Power->isOne())
Gabor Greifcea7ac72010-06-24 12:58:35 +0000282 return ReplaceInstUsesWith(CI, II->getArgOperand(0));
Chris Lattner753a2b42010-01-05 07:32:13 +0000283 // powi(x, -1) -> 1/x
284 if (Power->isAllOnesValue())
285 return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
Gabor Greifcea7ac72010-06-24 12:58:35 +0000286 II->getArgOperand(0));
Chris Lattner753a2b42010-01-05 07:32:13 +0000287 }
288 break;
289 case Intrinsic::cttz: {
290 // If all bits below the first known one are known zero,
291 // this value is constant.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000292 IntegerType *IT = dyn_cast<IntegerType>(II->getArgOperand(0)->getType());
Owen Andersonf1ac4652011-07-01 21:52:38 +0000293 // FIXME: Try to simplify vectors of integers.
294 if (!IT) break;
Chris Lattner753a2b42010-01-05 07:32:13 +0000295 uint32_t BitWidth = IT->getBitWidth();
296 APInt KnownZero(BitWidth, 0);
297 APInt KnownOne(BitWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000298 ComputeMaskedBits(II->getArgOperand(0), KnownZero, KnownOne);
Chris Lattner753a2b42010-01-05 07:32:13 +0000299 unsigned TrailingZeros = KnownOne.countTrailingZeros();
300 APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros));
301 if ((Mask & KnownZero) == Mask)
302 return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
303 APInt(BitWidth, TrailingZeros)));
Jim Grosbach00e403a2012-02-03 00:07:04 +0000304
Chris Lattner753a2b42010-01-05 07:32:13 +0000305 }
306 break;
307 case Intrinsic::ctlz: {
308 // If all bits above the first known one are known zero,
309 // this value is constant.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000310 IntegerType *IT = dyn_cast<IntegerType>(II->getArgOperand(0)->getType());
Owen Andersonf1ac4652011-07-01 21:52:38 +0000311 // FIXME: Try to simplify vectors of integers.
312 if (!IT) break;
Chris Lattner753a2b42010-01-05 07:32:13 +0000313 uint32_t BitWidth = IT->getBitWidth();
314 APInt KnownZero(BitWidth, 0);
315 APInt KnownOne(BitWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000316 ComputeMaskedBits(II->getArgOperand(0), KnownZero, KnownOne);
Chris Lattner753a2b42010-01-05 07:32:13 +0000317 unsigned LeadingZeros = KnownOne.countLeadingZeros();
318 APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros));
319 if ((Mask & KnownZero) == Mask)
320 return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
321 APInt(BitWidth, LeadingZeros)));
Jim Grosbach00e403a2012-02-03 00:07:04 +0000322
Chris Lattner753a2b42010-01-05 07:32:13 +0000323 }
324 break;
325 case Intrinsic::uadd_with_overflow: {
Gabor Greifcea7ac72010-06-24 12:58:35 +0000326 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000327 IntegerType *IT = cast<IntegerType>(II->getArgOperand(0)->getType());
Chris Lattner753a2b42010-01-05 07:32:13 +0000328 uint32_t BitWidth = IT->getBitWidth();
Chris Lattner753a2b42010-01-05 07:32:13 +0000329 APInt LHSKnownZero(BitWidth, 0);
330 APInt LHSKnownOne(BitWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000331 ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne);
Chris Lattner753a2b42010-01-05 07:32:13 +0000332 bool LHSKnownNegative = LHSKnownOne[BitWidth - 1];
333 bool LHSKnownPositive = LHSKnownZero[BitWidth - 1];
334
335 if (LHSKnownNegative || LHSKnownPositive) {
336 APInt RHSKnownZero(BitWidth, 0);
337 APInt RHSKnownOne(BitWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000338 ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne);
Chris Lattner753a2b42010-01-05 07:32:13 +0000339 bool RHSKnownNegative = RHSKnownOne[BitWidth - 1];
340 bool RHSKnownPositive = RHSKnownZero[BitWidth - 1];
341 if (LHSKnownNegative && RHSKnownNegative) {
342 // The sign bit is set in both cases: this MUST overflow.
343 // Create a simple add instruction, and insert it into the struct.
Eli Friedman59f15912011-05-18 19:57:14 +0000344 Value *Add = Builder->CreateAdd(LHS, RHS);
345 Add->takeName(&CI);
Chris Lattner753a2b42010-01-05 07:32:13 +0000346 Constant *V[] = {
Eli Friedman59f15912011-05-18 19:57:14 +0000347 UndefValue::get(LHS->getType()),
348 ConstantInt::getTrue(II->getContext())
Chris Lattner753a2b42010-01-05 07:32:13 +0000349 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000350 StructType *ST = cast<StructType>(II->getType());
Chris Lattnerb065b062011-06-20 04:01:31 +0000351 Constant *Struct = ConstantStruct::get(ST, V);
Chris Lattner753a2b42010-01-05 07:32:13 +0000352 return InsertValueInst::Create(Struct, Add, 0);
353 }
Eli Friedman59f15912011-05-18 19:57:14 +0000354
Chris Lattner753a2b42010-01-05 07:32:13 +0000355 if (LHSKnownPositive && RHSKnownPositive) {
356 // The sign bit is clear in both cases: this CANNOT overflow.
357 // Create a simple add instruction, and insert it into the struct.
Eli Friedman59f15912011-05-18 19:57:14 +0000358 Value *Add = Builder->CreateNUWAdd(LHS, RHS);
359 Add->takeName(&CI);
Chris Lattner753a2b42010-01-05 07:32:13 +0000360 Constant *V[] = {
361 UndefValue::get(LHS->getType()),
362 ConstantInt::getFalse(II->getContext())
363 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000364 StructType *ST = cast<StructType>(II->getType());
Chris Lattnerb065b062011-06-20 04:01:31 +0000365 Constant *Struct = ConstantStruct::get(ST, V);
Chris Lattner753a2b42010-01-05 07:32:13 +0000366 return InsertValueInst::Create(Struct, Add, 0);
367 }
368 }
369 }
370 // FALL THROUGH uadd into sadd
371 case Intrinsic::sadd_with_overflow:
372 // Canonicalize constants into the RHS.
Gabor Greifa90c5c72010-06-28 16:50:57 +0000373 if (isa<Constant>(II->getArgOperand(0)) &&
374 !isa<Constant>(II->getArgOperand(1))) {
375 Value *LHS = II->getArgOperand(0);
376 II->setArgOperand(0, II->getArgOperand(1));
377 II->setArgOperand(1, LHS);
Chris Lattner753a2b42010-01-05 07:32:13 +0000378 return II;
379 }
380
381 // X + undef -> undef
Gabor Greif9c68a7b2010-06-25 07:57:14 +0000382 if (isa<UndefValue>(II->getArgOperand(1)))
Chris Lattner753a2b42010-01-05 07:32:13 +0000383 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
Jim Grosbach00e403a2012-02-03 00:07:04 +0000384
Gabor Greif9c68a7b2010-06-25 07:57:14 +0000385 if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000386 // X + 0 -> {X, false}
387 if (RHS->isZero()) {
388 Constant *V[] = {
Eli Friedman4fffb342010-08-09 20:49:43 +0000389 UndefValue::get(II->getArgOperand(0)->getType()),
Chris Lattner753a2b42010-01-05 07:32:13 +0000390 ConstantInt::getFalse(II->getContext())
391 };
Chris Lattnerb065b062011-06-20 04:01:31 +0000392 Constant *Struct =
393 ConstantStruct::get(cast<StructType>(II->getType()), V);
Gabor Greifcea7ac72010-06-24 12:58:35 +0000394 return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
Chris Lattner753a2b42010-01-05 07:32:13 +0000395 }
396 }
397 break;
398 case Intrinsic::usub_with_overflow:
399 case Intrinsic::ssub_with_overflow:
400 // undef - X -> undef
401 // X - undef -> undef
Gabor Greif9c68a7b2010-06-25 07:57:14 +0000402 if (isa<UndefValue>(II->getArgOperand(0)) ||
403 isa<UndefValue>(II->getArgOperand(1)))
Chris Lattner753a2b42010-01-05 07:32:13 +0000404 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
Jim Grosbach00e403a2012-02-03 00:07:04 +0000405
Gabor Greif9c68a7b2010-06-25 07:57:14 +0000406 if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000407 // X - 0 -> {X, false}
408 if (RHS->isZero()) {
409 Constant *V[] = {
Gabor Greif9c68a7b2010-06-25 07:57:14 +0000410 UndefValue::get(II->getArgOperand(0)->getType()),
Chris Lattner753a2b42010-01-05 07:32:13 +0000411 ConstantInt::getFalse(II->getContext())
412 };
Jim Grosbach00e403a2012-02-03 00:07:04 +0000413 Constant *Struct =
Chris Lattnerb065b062011-06-20 04:01:31 +0000414 ConstantStruct::get(cast<StructType>(II->getType()), V);
Gabor Greif9c68a7b2010-06-25 07:57:14 +0000415 return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
Chris Lattner753a2b42010-01-05 07:32:13 +0000416 }
417 }
418 break;
Benjamin Kramer6b96fe72011-03-10 18:40:14 +0000419 case Intrinsic::umul_with_overflow: {
420 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
421 unsigned BitWidth = cast<IntegerType>(LHS->getType())->getBitWidth();
Benjamin Kramer6b96fe72011-03-10 18:40:14 +0000422
423 APInt LHSKnownZero(BitWidth, 0);
424 APInt LHSKnownOne(BitWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000425 ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne);
Benjamin Kramer6b96fe72011-03-10 18:40:14 +0000426 APInt RHSKnownZero(BitWidth, 0);
427 APInt RHSKnownOne(BitWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000428 ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne);
Benjamin Kramer6b96fe72011-03-10 18:40:14 +0000429
Benjamin Kramerd655e6e2011-03-27 15:04:38 +0000430 // Get the largest possible values for each operand.
431 APInt LHSMax = ~LHSKnownZero;
432 APInt RHSMax = ~RHSKnownZero;
Benjamin Kramer6b96fe72011-03-10 18:40:14 +0000433
434 // If multiplying the maximum values does not overflow then we can turn
435 // this into a plain NUW mul.
Benjamin Kramerd655e6e2011-03-27 15:04:38 +0000436 bool Overflow;
437 LHSMax.umul_ov(RHSMax, Overflow);
438 if (!Overflow) {
Benjamin Kramer6b96fe72011-03-10 18:40:14 +0000439 Value *Mul = Builder->CreateNUWMul(LHS, RHS, "umul_with_overflow");
440 Constant *V[] = {
441 UndefValue::get(LHS->getType()),
442 Builder->getFalse()
443 };
Chris Lattnerb065b062011-06-20 04:01:31 +0000444 Constant *Struct = ConstantStruct::get(cast<StructType>(II->getType()),V);
Benjamin Kramer6b96fe72011-03-10 18:40:14 +0000445 return InsertValueInst::Create(Struct, Mul, 0);
446 }
447 } // FALL THROUGH
Chris Lattner753a2b42010-01-05 07:32:13 +0000448 case Intrinsic::smul_with_overflow:
449 // Canonicalize constants into the RHS.
Gabor Greifa90c5c72010-06-28 16:50:57 +0000450 if (isa<Constant>(II->getArgOperand(0)) &&
451 !isa<Constant>(II->getArgOperand(1))) {
452 Value *LHS = II->getArgOperand(0);
453 II->setArgOperand(0, II->getArgOperand(1));
454 II->setArgOperand(1, LHS);
Chris Lattner753a2b42010-01-05 07:32:13 +0000455 return II;
456 }
457
458 // X * undef -> undef
Gabor Greif9c68a7b2010-06-25 07:57:14 +0000459 if (isa<UndefValue>(II->getArgOperand(1)))
Chris Lattner753a2b42010-01-05 07:32:13 +0000460 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
Jim Grosbach00e403a2012-02-03 00:07:04 +0000461
Gabor Greif9c68a7b2010-06-25 07:57:14 +0000462 if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000463 // X*0 -> {0, false}
464 if (RHSI->isZero())
465 return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType()));
Jim Grosbach00e403a2012-02-03 00:07:04 +0000466
Chris Lattner753a2b42010-01-05 07:32:13 +0000467 // X * 1 -> {X, false}
468 if (RHSI->equalsInt(1)) {
469 Constant *V[] = {
Gabor Greifcea7ac72010-06-24 12:58:35 +0000470 UndefValue::get(II->getArgOperand(0)->getType()),
Chris Lattner753a2b42010-01-05 07:32:13 +0000471 ConstantInt::getFalse(II->getContext())
472 };
Jim Grosbach00e403a2012-02-03 00:07:04 +0000473 Constant *Struct =
Chris Lattnerb065b062011-06-20 04:01:31 +0000474 ConstantStruct::get(cast<StructType>(II->getType()), V);
Gabor Greifcea7ac72010-06-24 12:58:35 +0000475 return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
Chris Lattner753a2b42010-01-05 07:32:13 +0000476 }
477 }
478 break;
479 case Intrinsic::ppc_altivec_lvx:
480 case Intrinsic::ppc_altivec_lvxl:
Bill Wendlingf93f7b22011-04-13 00:36:11 +0000481 // Turn PPC lvx -> load if the pointer is known aligned.
Chris Lattner687140c2010-12-25 20:37:57 +0000482 if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, TD) >= 16) {
Gabor Greifcea7ac72010-06-24 12:58:35 +0000483 Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0),
Chris Lattner753a2b42010-01-05 07:32:13 +0000484 PointerType::getUnqual(II->getType()));
485 return new LoadInst(Ptr);
486 }
487 break;
488 case Intrinsic::ppc_altivec_stvx:
489 case Intrinsic::ppc_altivec_stvxl:
490 // Turn stvx -> store if the pointer is known aligned.
Chris Lattner687140c2010-12-25 20:37:57 +0000491 if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, TD) >= 16) {
Jim Grosbach00e403a2012-02-03 00:07:04 +0000492 Type *OpPtrTy =
Gabor Greif2f1ab742010-06-24 15:51:11 +0000493 PointerType::getUnqual(II->getArgOperand(0)->getType());
494 Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy);
495 return new StoreInst(II->getArgOperand(0), Ptr);
Chris Lattner753a2b42010-01-05 07:32:13 +0000496 }
497 break;
498 case Intrinsic::x86_sse_storeu_ps:
499 case Intrinsic::x86_sse2_storeu_pd:
500 case Intrinsic::x86_sse2_storeu_dq:
501 // Turn X86 storeu -> store if the pointer is known aligned.
Chris Lattner687140c2010-12-25 20:37:57 +0000502 if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, TD) >= 16) {
Jim Grosbach00e403a2012-02-03 00:07:04 +0000503 Type *OpPtrTy =
Gabor Greif2f1ab742010-06-24 15:51:11 +0000504 PointerType::getUnqual(II->getArgOperand(1)->getType());
505 Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), OpPtrTy);
506 return new StoreInst(II->getArgOperand(1), Ptr);
Chris Lattner753a2b42010-01-05 07:32:13 +0000507 }
508 break;
Chandler Carruth9cc9f502011-01-10 07:19:37 +0000509
510 case Intrinsic::x86_sse_cvtss2si:
511 case Intrinsic::x86_sse_cvtss2si64:
512 case Intrinsic::x86_sse_cvttss2si:
513 case Intrinsic::x86_sse_cvttss2si64:
514 case Intrinsic::x86_sse2_cvtsd2si:
515 case Intrinsic::x86_sse2_cvtsd2si64:
516 case Intrinsic::x86_sse2_cvttsd2si:
517 case Intrinsic::x86_sse2_cvttsd2si64: {
518 // These intrinsics only demand the 0th element of their input vectors. If
Chris Lattner753a2b42010-01-05 07:32:13 +0000519 // we can simplify the input based on that, do so now.
520 unsigned VWidth =
Gabor Greif9c68a7b2010-06-25 07:57:14 +0000521 cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements();
Chris Lattner753a2b42010-01-05 07:32:13 +0000522 APInt DemandedElts(VWidth, 1);
523 APInt UndefElts(VWidth, 0);
Gabor Greifa3997812010-07-22 10:37:47 +0000524 if (Value *V = SimplifyDemandedVectorElts(II->getArgOperand(0),
525 DemandedElts, UndefElts)) {
Gabor Greifa90c5c72010-06-28 16:50:57 +0000526 II->setArgOperand(0, V);
Chris Lattner753a2b42010-01-05 07:32:13 +0000527 return II;
528 }
529 break;
530 }
Chandler Carruth9cc9f502011-01-10 07:19:37 +0000531
Stuart Hastingsca1ef482011-05-17 22:13:31 +0000532
533 case Intrinsic::x86_sse41_pmovsxbw:
534 case Intrinsic::x86_sse41_pmovsxwd:
535 case Intrinsic::x86_sse41_pmovsxdq:
536 case Intrinsic::x86_sse41_pmovzxbw:
537 case Intrinsic::x86_sse41_pmovzxwd:
538 case Intrinsic::x86_sse41_pmovzxdq: {
Evan Chengaaa7f492011-05-19 18:18:39 +0000539 // pmov{s|z}x ignores the upper half of their input vectors.
Stuart Hastingsca1ef482011-05-17 22:13:31 +0000540 unsigned VWidth =
541 cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements();
542 unsigned LowHalfElts = VWidth / 2;
Stuart Hastingsd1166112011-05-18 15:54:26 +0000543 APInt InputDemandedElts(APInt::getBitsSet(VWidth, 0, LowHalfElts));
Stuart Hastingsca1ef482011-05-17 22:13:31 +0000544 APInt UndefElts(VWidth, 0);
545 if (Value *TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0),
546 InputDemandedElts,
547 UndefElts)) {
548 II->setArgOperand(0, TmpV);
549 return II;
550 }
551 break;
552 }
553
Chris Lattner753a2b42010-01-05 07:32:13 +0000554 case Intrinsic::ppc_altivec_vperm:
555 // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
Chris Lattnera78fa8c2012-01-27 03:08:05 +0000556 if (Constant *Mask = dyn_cast<Constant>(II->getArgOperand(2))) {
557 assert(Mask->getType()->getVectorNumElements() == 16 &&
558 "Bad type for intrinsic!");
Jim Grosbach00e403a2012-02-03 00:07:04 +0000559
Chris Lattner753a2b42010-01-05 07:32:13 +0000560 // Check that all of the elements are integer constants or undefs.
561 bool AllEltsOk = true;
562 for (unsigned i = 0; i != 16; ++i) {
Chris Lattnera78fa8c2012-01-27 03:08:05 +0000563 Constant *Elt = Mask->getAggregateElement(i);
564 if (Elt == 0 ||
565 !(isa<ConstantInt>(Elt) || isa<UndefValue>(Elt))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000566 AllEltsOk = false;
567 break;
568 }
569 }
Jim Grosbach00e403a2012-02-03 00:07:04 +0000570
Chris Lattner753a2b42010-01-05 07:32:13 +0000571 if (AllEltsOk) {
572 // Cast the input vectors to byte vectors.
Gabor Greifa3997812010-07-22 10:37:47 +0000573 Value *Op0 = Builder->CreateBitCast(II->getArgOperand(0),
574 Mask->getType());
575 Value *Op1 = Builder->CreateBitCast(II->getArgOperand(1),
576 Mask->getType());
Chris Lattner753a2b42010-01-05 07:32:13 +0000577 Value *Result = UndefValue::get(Op0->getType());
Jim Grosbach00e403a2012-02-03 00:07:04 +0000578
Chris Lattner753a2b42010-01-05 07:32:13 +0000579 // Only extract each element once.
580 Value *ExtractedElts[32];
581 memset(ExtractedElts, 0, sizeof(ExtractedElts));
Jim Grosbach00e403a2012-02-03 00:07:04 +0000582
Chris Lattner753a2b42010-01-05 07:32:13 +0000583 for (unsigned i = 0; i != 16; ++i) {
Chris Lattnera78fa8c2012-01-27 03:08:05 +0000584 if (isa<UndefValue>(Mask->getAggregateElement(i)))
Chris Lattner753a2b42010-01-05 07:32:13 +0000585 continue;
Jim Grosbach00e403a2012-02-03 00:07:04 +0000586 unsigned Idx =
Chris Lattnera78fa8c2012-01-27 03:08:05 +0000587 cast<ConstantInt>(Mask->getAggregateElement(i))->getZExtValue();
Chris Lattner753a2b42010-01-05 07:32:13 +0000588 Idx &= 31; // Match the hardware behavior.
Jim Grosbach00e403a2012-02-03 00:07:04 +0000589
Chris Lattner753a2b42010-01-05 07:32:13 +0000590 if (ExtractedElts[Idx] == 0) {
Jim Grosbach00e403a2012-02-03 00:07:04 +0000591 ExtractedElts[Idx] =
Benjamin Kramera9390a42011-09-27 20:39:19 +0000592 Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1,
593 Builder->getInt32(Idx&15));
Chris Lattner753a2b42010-01-05 07:32:13 +0000594 }
Jim Grosbach00e403a2012-02-03 00:07:04 +0000595
Chris Lattner753a2b42010-01-05 07:32:13 +0000596 // Insert this value into the result vector.
597 Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx],
Benjamin Kramera9390a42011-09-27 20:39:19 +0000598 Builder->getInt32(i));
Chris Lattner753a2b42010-01-05 07:32:13 +0000599 }
600 return CastInst::Create(Instruction::BitCast, Result, CI.getType());
601 }
602 }
603 break;
604
Bob Wilson364f17c2010-10-22 21:41:48 +0000605 case Intrinsic::arm_neon_vld1:
606 case Intrinsic::arm_neon_vld2:
607 case Intrinsic::arm_neon_vld3:
608 case Intrinsic::arm_neon_vld4:
609 case Intrinsic::arm_neon_vld2lane:
610 case Intrinsic::arm_neon_vld3lane:
611 case Intrinsic::arm_neon_vld4lane:
612 case Intrinsic::arm_neon_vst1:
613 case Intrinsic::arm_neon_vst2:
614 case Intrinsic::arm_neon_vst3:
615 case Intrinsic::arm_neon_vst4:
616 case Intrinsic::arm_neon_vst2lane:
617 case Intrinsic::arm_neon_vst3lane:
618 case Intrinsic::arm_neon_vst4lane: {
Chris Lattnerae47be12010-12-25 20:52:04 +0000619 unsigned MemAlign = getKnownAlignment(II->getArgOperand(0), TD);
Bob Wilson364f17c2010-10-22 21:41:48 +0000620 unsigned AlignArg = II->getNumArgOperands() - 1;
621 ConstantInt *IntrAlign = dyn_cast<ConstantInt>(II->getArgOperand(AlignArg));
622 if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) {
623 II->setArgOperand(AlignArg,
624 ConstantInt::get(Type::getInt32Ty(II->getContext()),
625 MemAlign, false));
626 return II;
627 }
628 break;
629 }
630
Lang Hames973f72a2012-05-01 00:20:38 +0000631 case Intrinsic::arm_neon_vmulls:
632 case Intrinsic::arm_neon_vmullu: {
633 Value *Arg0 = II->getArgOperand(0);
634 Value *Arg1 = II->getArgOperand(1);
635
636 // Handle mul by zero first:
637 if (isa<ConstantAggregateZero>(Arg0) || isa<ConstantAggregateZero>(Arg1)) {
638 return ReplaceInstUsesWith(CI, ConstantAggregateZero::get(II->getType()));
639 }
640
641 // Check for constant LHS & RHS - in this case we just simplify.
642 bool Zext = (II->getIntrinsicID() == Intrinsic::arm_neon_vmullu);
643 VectorType *NewVT = cast<VectorType>(II->getType());
644 unsigned NewWidth = NewVT->getElementType()->getIntegerBitWidth();
645 if (ConstantDataVector *CV0 = dyn_cast<ConstantDataVector>(Arg0)) {
646 if (ConstantDataVector *CV1 = dyn_cast<ConstantDataVector>(Arg1)) {
647 VectorType* VT = cast<VectorType>(CV0->getType());
648 SmallVector<Constant*, 4> NewElems;
649 for (unsigned i = 0; i < VT->getNumElements(); ++i) {
650 APInt CV0E =
651 (cast<ConstantInt>(CV0->getAggregateElement(i)))->getValue();
652 CV0E = Zext ? CV0E.zext(NewWidth) : CV0E.sext(NewWidth);
653 APInt CV1E =
654 (cast<ConstantInt>(CV1->getAggregateElement(i)))->getValue();
655 CV1E = Zext ? CV1E.zext(NewWidth) : CV1E.sext(NewWidth);
656 NewElems.push_back(
657 ConstantInt::get(NewVT->getElementType(), CV0E * CV1E));
658 }
659 return ReplaceInstUsesWith(CI, ConstantVector::get(NewElems));
660 }
661
662 // Couldn't simplify - cannonicalize constant to the RHS.
663 std::swap(Arg0, Arg1);
664 }
665
666 // Handle mul by one:
667 if (ConstantDataVector *CV1 = dyn_cast<ConstantDataVector>(Arg1)) {
668 if (ConstantInt *Splat =
669 dyn_cast_or_null<ConstantInt>(CV1->getSplatValue())) {
670 if (Splat->isOne()) {
671 if (Zext)
672 return CastInst::CreateZExtOrBitCast(Arg0, II->getType());
673 // else
674 return CastInst::CreateSExtOrBitCast(Arg0, II->getType());
675 }
676 }
677 }
678
679 break;
680 }
681
Chris Lattner753a2b42010-01-05 07:32:13 +0000682 case Intrinsic::stackrestore: {
683 // If the save is right next to the restore, remove the restore. This can
684 // happen when variable allocas are DCE'd.
Gabor Greifcea7ac72010-06-24 12:58:35 +0000685 if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000686 if (SS->getIntrinsicID() == Intrinsic::stacksave) {
687 BasicBlock::iterator BI = SS;
688 if (&*++BI == II)
689 return EraseInstFromFunction(CI);
690 }
691 }
Jim Grosbach00e403a2012-02-03 00:07:04 +0000692
Chris Lattner753a2b42010-01-05 07:32:13 +0000693 // Scan down this block to see if there is another stack restore in the
694 // same block without an intervening call/alloca.
695 BasicBlock::iterator BI = II;
696 TerminatorInst *TI = II->getParent()->getTerminator();
697 bool CannotRemove = false;
698 for (++BI; &*BI != TI; ++BI) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000699 if (isa<AllocaInst>(BI)) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000700 CannotRemove = true;
701 break;
702 }
703 if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
704 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
705 // If there is a stackrestore below this one, remove this one.
706 if (II->getIntrinsicID() == Intrinsic::stackrestore)
707 return EraseInstFromFunction(CI);
708 // Otherwise, ignore the intrinsic.
709 } else {
710 // If we found a non-intrinsic call, we can't remove the stack
711 // restore.
712 CannotRemove = true;
713 break;
714 }
715 }
716 }
Jim Grosbach00e403a2012-02-03 00:07:04 +0000717
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000718 // If the stack restore is in a return, resume, or unwind block and if there
719 // are no allocas or calls between the restore and the return, nuke the
720 // restore.
Bill Wendlingaa5abe82012-02-06 21:16:41 +0000721 if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI)))
Chris Lattner753a2b42010-01-05 07:32:13 +0000722 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
Jim Grosbach00e403a2012-02-03 00:07:04 +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.
Nick Lewycky173862e2011-11-20 19:09:04 +0000748 if (!CS.isByValArgument(ix))
Chris Lattner753a2b42010-01-05 07:32:13 +0000749 return true;
750
Jim Grosbach00e403a2012-02-03 00:07:04 +0000751 Type* SrcTy =
Chris Lattner753a2b42010-01-05 07:32:13 +0000752 cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000753 Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
Chris Lattner753a2b42010-01-05 07:32:13 +0000754 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 {
Benjamin Kramer8143a842011-01-06 14:22:52 +0000769 if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
770 return true;
Gabor Greifa3997812010-07-22 10:37:47 +0000771 if (ConstantInt *SizeCI =
772 dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000773 if (SizeCI->isAllOnesValue())
774 return true;
Eric Christopherb9b80c32011-03-15 00:25:41 +0000775 if (isString) {
776 uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
777 // If the length is 0 we don't know how long it is and so we can't
778 // remove the check.
779 if (Len == 0) return false;
780 return SizeCI->getZExtValue() >= Len;
781 }
Gabor Greifa3997812010-07-22 10:37:47 +0000782 if (ConstantInt *Arg = dyn_cast<ConstantInt>(
783 CI->getArgOperand(SizeArgOp)))
Evan Cheng9d8f0022010-03-23 06:06:09 +0000784 return SizeCI->getZExtValue() >= Arg->getZExtValue();
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000785 }
786 return false;
787 }
788public:
789 InstCombineFortifiedLibCalls(InstCombiner *IC) : IC(IC), NewInstruction(0) { }
790 Instruction *NewInstruction;
791};
792} // end anonymous namespace
793
Eric Christopher27ceaa12010-03-06 10:50:38 +0000794// Try to fold some different type of calls here.
Jim Grosbach00e403a2012-02-03 00:07:04 +0000795// Currently we're only working with the checking functions, memcpy_chk,
Eric Christopher27ceaa12010-03-06 10:50:38 +0000796// mempcpy_chk, memmove_chk, memset_chk, strcpy_chk, stpcpy_chk, strncpy_chk,
797// strcat_chk and strncat_chk.
798Instruction *InstCombiner::tryOptimizeCall(CallInst *CI, const TargetData *TD) {
799 if (CI->getCalledFunction() == 0) return 0;
Eric Christopher27ceaa12010-03-06 10:50:38 +0000800
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000801 InstCombineFortifiedLibCalls Simplifier(this);
Nuno Lopes51004df2012-07-25 16:46:31 +0000802 Simplifier.fold(CI, TD, TLI);
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000803 return Simplifier.NewInstruction;
Eric Christopher27ceaa12010-03-06 10:50:38 +0000804}
805
Duncan Sands4a544a72011-09-06 13:37:06 +0000806static IntrinsicInst *FindInitTrampolineFromAlloca(Value *TrampMem) {
807 // Strip off at most one level of pointer casts, looking for an alloca. This
808 // is good enough in practice and simpler than handling any number of casts.
809 Value *Underlying = TrampMem->stripPointerCasts();
810 if (Underlying != TrampMem &&
811 (!Underlying->hasOneUse() || *Underlying->use_begin() != TrampMem))
812 return 0;
813 if (!isa<AllocaInst>(Underlying))
814 return 0;
815
816 IntrinsicInst *InitTrampoline = 0;
817 for (Value::use_iterator I = TrampMem->use_begin(), E = TrampMem->use_end();
818 I != E; I++) {
819 IntrinsicInst *II = dyn_cast<IntrinsicInst>(*I);
820 if (!II)
821 return 0;
822 if (II->getIntrinsicID() == Intrinsic::init_trampoline) {
823 if (InitTrampoline)
824 // More than one init_trampoline writes to this value. Give up.
825 return 0;
826 InitTrampoline = II;
827 continue;
828 }
829 if (II->getIntrinsicID() == Intrinsic::adjust_trampoline)
830 // Allow any number of calls to adjust.trampoline.
831 continue;
832 return 0;
833 }
834
835 // No call to init.trampoline found.
836 if (!InitTrampoline)
837 return 0;
838
839 // Check that the alloca is being used in the expected way.
840 if (InitTrampoline->getOperand(0) != TrampMem)
841 return 0;
842
843 return InitTrampoline;
844}
845
846static IntrinsicInst *FindInitTrampolineFromBB(IntrinsicInst *AdjustTramp,
847 Value *TrampMem) {
848 // Visit all the previous instructions in the basic block, and try to find a
849 // init.trampoline which has a direct path to the adjust.trampoline.
850 for (BasicBlock::iterator I = AdjustTramp,
851 E = AdjustTramp->getParent()->begin(); I != E; ) {
852 Instruction *Inst = --I;
853 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
854 if (II->getIntrinsicID() == Intrinsic::init_trampoline &&
855 II->getOperand(0) == TrampMem)
856 return II;
857 if (Inst->mayWriteToMemory())
858 return 0;
859 }
860 return 0;
861}
862
863// Given a call to llvm.adjust.trampoline, find and return the corresponding
864// call to llvm.init.trampoline if the call to the trampoline can be optimized
865// to a direct call to a function. Otherwise return NULL.
866//
867static IntrinsicInst *FindInitTrampoline(Value *Callee) {
868 Callee = Callee->stripPointerCasts();
869 IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee);
870 if (!AdjustTramp ||
871 AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline)
872 return 0;
873
874 Value *TrampMem = AdjustTramp->getOperand(0);
875
876 if (IntrinsicInst *IT = FindInitTrampolineFromAlloca(TrampMem))
877 return IT;
878 if (IntrinsicInst *IT = FindInitTrampolineFromBB(AdjustTramp, TrampMem))
879 return IT;
880 return 0;
881}
882
Chris Lattner753a2b42010-01-05 07:32:13 +0000883// visitCallSite - Improvements for call and invoke instructions.
884//
885Instruction *InstCombiner::visitCallSite(CallSite CS) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000886 if (isAllocLikeFn(CS.getInstruction(), TLI))
Nuno Lopes78f8ef42012-07-09 18:38:20 +0000887 return visitAllocSite(*CS.getInstruction());
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000888
Chris Lattner753a2b42010-01-05 07:32:13 +0000889 bool Changed = false;
890
Chris Lattnerab215bc2010-12-20 08:25:06 +0000891 // If the callee is a pointer to a function, attempt to move any casts to the
892 // arguments of the call/invoke.
Chris Lattner753a2b42010-01-05 07:32:13 +0000893 Value *Callee = CS.getCalledValue();
Chris Lattnerab215bc2010-12-20 08:25:06 +0000894 if (!isa<Function>(Callee) && transformConstExprCastCall(CS))
895 return 0;
Chris Lattner753a2b42010-01-05 07:32:13 +0000896
897 if (Function *CalleeF = dyn_cast<Function>(Callee))
Chris Lattnerd5695612010-02-01 18:11:34 +0000898 // If the call and callee calling conventions don't match, this call must
899 // be unreachable, as the call is undefined.
900 if (CalleeF->getCallingConv() != CS.getCallingConv() &&
901 // Only do this for calls to a function with a body. A prototype may
902 // not actually end up matching the implementation's calling conv for a
903 // variety of reasons (e.g. it may be written in assembly).
904 !CalleeF->isDeclaration()) {
Chris Lattner753a2b42010-01-05 07:32:13 +0000905 Instruction *OldCall = CS.getInstruction();
Chris Lattner753a2b42010-01-05 07:32:13 +0000906 new StoreInst(ConstantInt::getTrue(Callee->getContext()),
Jim Grosbach00e403a2012-02-03 00:07:04 +0000907 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
Chris Lattner753a2b42010-01-05 07:32:13 +0000908 OldCall);
909 // If OldCall dues not return void then replaceAllUsesWith undef.
910 // This allows ValueHandlers and custom metadata to adjust itself.
911 if (!OldCall->getType()->isVoidTy())
Eli Friedman3e22cb92011-05-18 00:32:01 +0000912 ReplaceInstUsesWith(*OldCall, UndefValue::get(OldCall->getType()));
Chris Lattner830f3f22010-02-01 18:04:58 +0000913 if (isa<CallInst>(OldCall))
Chris Lattner753a2b42010-01-05 07:32:13 +0000914 return EraseInstFromFunction(*OldCall);
Jim Grosbach00e403a2012-02-03 00:07:04 +0000915
Chris Lattner830f3f22010-02-01 18:04:58 +0000916 // We cannot remove an invoke, because it would change the CFG, just
917 // change the callee to a null pointer.
Gabor Greif654c06f2010-03-20 21:00:25 +0000918 cast<InvokeInst>(OldCall)->setCalledFunction(
Chris Lattner830f3f22010-02-01 18:04:58 +0000919 Constant::getNullValue(CalleeF->getType()));
Chris Lattner753a2b42010-01-05 07:32:13 +0000920 return 0;
921 }
922
923 if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
Gabor Greifcea7ac72010-06-24 12:58:35 +0000924 // If CS does not return void then replaceAllUsesWith undef.
Chris Lattner753a2b42010-01-05 07:32:13 +0000925 // This allows ValueHandlers and custom metadata to adjust itself.
926 if (!CS.getInstruction()->getType()->isVoidTy())
Eli Friedman3e22cb92011-05-18 00:32:01 +0000927 ReplaceInstUsesWith(*CS.getInstruction(),
928 UndefValue::get(CS.getInstruction()->getType()));
Chris Lattner753a2b42010-01-05 07:32:13 +0000929
Nuno Lopesf1fb6c82012-06-21 23:52:14 +0000930 if (isa<InvokeInst>(CS.getInstruction())) {
931 // Can't remove an invoke because we cannot change the CFG.
932 return 0;
Chris Lattner753a2b42010-01-05 07:32:13 +0000933 }
Nuno Lopesf1fb6c82012-06-21 23:52:14 +0000934
935 // This instruction is not reachable, just remove it. We insert a store to
936 // undef so that we know that this code is not reachable, despite the fact
937 // that we can't modify the CFG here.
938 new StoreInst(ConstantInt::getTrue(Callee->getContext()),
939 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
940 CS.getInstruction());
941
Chris Lattner753a2b42010-01-05 07:32:13 +0000942 return EraseInstFromFunction(*CS.getInstruction());
943 }
944
Duncan Sands4a544a72011-09-06 13:37:06 +0000945 if (IntrinsicInst *II = FindInitTrampoline(Callee))
946 return transformCallThroughTrampoline(CS, II);
Chris Lattner753a2b42010-01-05 07:32:13 +0000947
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000948 PointerType *PTy = cast<PointerType>(Callee->getType());
949 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattner753a2b42010-01-05 07:32:13 +0000950 if (FTy->isVarArg()) {
Eli Friedmanba78c882011-11-29 01:18:23 +0000951 int ix = FTy->getNumParams();
Chris Lattner753a2b42010-01-05 07:32:13 +0000952 // See if we can optimize any arguments passed through the varargs area of
953 // the call.
954 for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
955 E = CS.arg_end(); I != E; ++I, ++ix) {
956 CastInst *CI = dyn_cast<CastInst>(*I);
957 if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
958 *I = CI->getOperand(0);
959 Changed = true;
960 }
961 }
962 }
963
964 if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
965 // Inline asm calls cannot throw - mark them 'nounwind'.
966 CS.setDoesNotThrow();
967 Changed = true;
968 }
969
Eric Christopher27ceaa12010-03-06 10:50:38 +0000970 // Try to optimize the call if possible, we require TargetData for most of
971 // this. None of these calls are seen as possibly dead so go ahead and
972 // delete the instruction now.
973 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
974 Instruction *I = tryOptimizeCall(CI, TD);
Eric Christopher7b323a32010-03-06 10:59:25 +0000975 // If we changed something return the result, etc. Otherwise let
976 // the fallthrough check.
977 if (I) return EraseInstFromFunction(*I);
Eric Christopher27ceaa12010-03-06 10:50:38 +0000978 }
979
Chris Lattner753a2b42010-01-05 07:32:13 +0000980 return Changed ? CS.getInstruction() : 0;
981}
982
983// transformConstExprCastCall - If the callee is a constexpr cast of a function,
984// attempt to move the cast to the arguments of the call/invoke.
985//
986bool InstCombiner::transformConstExprCastCall(CallSite CS) {
Chris Lattnerab215bc2010-12-20 08:25:06 +0000987 Function *Callee =
988 dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts());
989 if (Callee == 0)
Chris Lattner753a2b42010-01-05 07:32:13 +0000990 return false;
Chris Lattner753a2b42010-01-05 07:32:13 +0000991 Instruction *Caller = CS.getInstruction();
992 const AttrListPtr &CallerPAL = CS.getAttributes();
993
994 // Okay, this is a cast from a function to a different type. Unless doing so
995 // would cause a type conversion of one of our arguments, change this call to
996 // be a direct call with arguments casted to the appropriate types.
997 //
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000998 FunctionType *FT = Callee->getFunctionType();
999 Type *OldRetTy = Caller->getType();
1000 Type *NewRetTy = FT->getReturnType();
Chris Lattner753a2b42010-01-05 07:32:13 +00001001
Duncan Sands1df98592010-02-16 11:11:14 +00001002 if (NewRetTy->isStructTy())
Chris Lattner753a2b42010-01-05 07:32:13 +00001003 return false; // TODO: Handle multiple return values.
1004
1005 // Check to see if we are changing the return type...
1006 if (OldRetTy != NewRetTy) {
1007 if (Callee->isDeclaration() &&
1008 // Conversion is ok if changing from one pointer type to another or from
1009 // a pointer to an integer of the same size.
Duncan Sands1df98592010-02-16 11:11:14 +00001010 !((OldRetTy->isPointerTy() || !TD ||
Chris Lattner753a2b42010-01-05 07:32:13 +00001011 OldRetTy == TD->getIntPtrType(Caller->getContext())) &&
Duncan Sands1df98592010-02-16 11:11:14 +00001012 (NewRetTy->isPointerTy() || !TD ||
Chris Lattner753a2b42010-01-05 07:32:13 +00001013 NewRetTy == TD->getIntPtrType(Caller->getContext()))))
1014 return false; // Cannot transform this return value.
1015
1016 if (!Caller->use_empty() &&
1017 // void -> non-void is handled specially
1018 !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy))
1019 return false; // Cannot transform this return value.
1020
1021 if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
1022 Attributes RAttrs = CallerPAL.getRetAttributes();
1023 if (RAttrs & Attribute::typeIncompatible(NewRetTy))
1024 return false; // Attribute not compatible with transformed value.
1025 }
1026
1027 // If the callsite is an invoke instruction, and the return value is used by
1028 // a PHI node in a successor, we cannot change the return type of the call
1029 // because there is no place to put the cast instruction (without breaking
1030 // the critical edge). Bail out in this case.
1031 if (!Caller->use_empty())
1032 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
1033 for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
1034 UI != E; ++UI)
1035 if (PHINode *PN = dyn_cast<PHINode>(*UI))
1036 if (PN->getParent() == II->getNormalDest() ||
1037 PN->getParent() == II->getUnwindDest())
1038 return false;
1039 }
1040
1041 unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
1042 unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
1043
1044 CallSite::arg_iterator AI = CS.arg_begin();
1045 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001046 Type *ParamTy = FT->getParamType(i);
1047 Type *ActTy = (*AI)->getType();
Chris Lattner753a2b42010-01-05 07:32:13 +00001048
1049 if (!CastInst::isCastable(ActTy, ParamTy))
1050 return false; // Cannot transform this parameter value.
1051
Kostya Serebryany164b86b2012-01-20 17:56:17 +00001052 Attributes Attrs = CallerPAL.getParamAttributes(i + 1);
Chris Lattner2b9375e2010-12-20 08:36:38 +00001053 if (Attrs & Attribute::typeIncompatible(ParamTy))
Chris Lattner753a2b42010-01-05 07:32:13 +00001054 return false; // Attribute not compatible with transformed value.
Jim Grosbach00e403a2012-02-03 00:07:04 +00001055
Chris Lattner2b9375e2010-12-20 08:36:38 +00001056 // If the parameter is passed as a byval argument, then we have to have a
1057 // sized type and the sized type has to have the same size as the old type.
1058 if (ParamTy != ActTy && (Attrs & Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001059 PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy);
Chris Lattner2b9375e2010-12-20 08:36:38 +00001060 if (ParamPTy == 0 || !ParamPTy->getElementType()->isSized() || TD == 0)
1061 return false;
Jim Grosbach00e403a2012-02-03 00:07:04 +00001062
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001063 Type *CurElTy = cast<PointerType>(ActTy)->getElementType();
Chris Lattner2b9375e2010-12-20 08:36:38 +00001064 if (TD->getTypeAllocSize(CurElTy) !=
1065 TD->getTypeAllocSize(ParamPTy->getElementType()))
1066 return false;
1067 }
Chris Lattner753a2b42010-01-05 07:32:13 +00001068
1069 // Converting from one pointer type to another or between a pointer and an
1070 // integer of the same size is safe even if we do not have a body.
1071 bool isConvertible = ActTy == ParamTy ||
Duncan Sands1df98592010-02-16 11:11:14 +00001072 (TD && ((ParamTy->isPointerTy() ||
Chris Lattner753a2b42010-01-05 07:32:13 +00001073 ParamTy == TD->getIntPtrType(Caller->getContext())) &&
Duncan Sands1df98592010-02-16 11:11:14 +00001074 (ActTy->isPointerTy() ||
Chris Lattner753a2b42010-01-05 07:32:13 +00001075 ActTy == TD->getIntPtrType(Caller->getContext()))));
1076 if (Callee->isDeclaration() && !isConvertible) return false;
1077 }
1078
Chris Lattner091b1e32011-02-24 05:10:56 +00001079 if (Callee->isDeclaration()) {
1080 // Do not delete arguments unless we have a function body.
1081 if (FT->getNumParams() < NumActualArgs && !FT->isVarArg())
1082 return false;
Chris Lattner753a2b42010-01-05 07:32:13 +00001083
Chris Lattner091b1e32011-02-24 05:10:56 +00001084 // If the callee is just a declaration, don't change the varargsness of the
1085 // call. We don't want to introduce a varargs call where one doesn't
1086 // already exist.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001087 PointerType *APTy = cast<PointerType>(CS.getCalledValue()->getType());
Chris Lattner091b1e32011-02-24 05:10:56 +00001088 if (FT->isVarArg()!=cast<FunctionType>(APTy->getElementType())->isVarArg())
1089 return false;
Jim Grosbachf3744862012-02-03 00:00:55 +00001090
1091 // If both the callee and the cast type are varargs, we still have to make
1092 // sure the number of fixed parameters are the same or we have the same
1093 // ABI issues as if we introduce a varargs call.
Jim Grosbach871a2052012-02-03 00:26:07 +00001094 if (FT->isVarArg() &&
1095 cast<FunctionType>(APTy->getElementType())->isVarArg() &&
1096 FT->getNumParams() !=
Jim Grosbachf3744862012-02-03 00:00:55 +00001097 cast<FunctionType>(APTy->getElementType())->getNumParams())
1098 return false;
Chris Lattner091b1e32011-02-24 05:10:56 +00001099 }
Jim Grosbach00e403a2012-02-03 00:07:04 +00001100
Jim Grosbachd5917f02012-02-03 00:00:50 +00001101 if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
1102 !CallerPAL.isEmpty())
1103 // In this case we have more arguments than the new function type, but we
1104 // won't be dropping them. Check that these extra arguments have attributes
1105 // that are compatible with being a vararg call argument.
1106 for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
1107 if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams())
1108 break;
1109 Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs;
1110 if (PAttrs & Attribute::VarArgsIncompatible)
1111 return false;
1112 }
Chris Lattner753a2b42010-01-05 07:32:13 +00001113
Jim Grosbach00e403a2012-02-03 00:07:04 +00001114
Chris Lattner753a2b42010-01-05 07:32:13 +00001115 // Okay, we decided that this is a safe thing to do: go ahead and start
Chris Lattner091b1e32011-02-24 05:10:56 +00001116 // inserting cast instructions as necessary.
Chris Lattner753a2b42010-01-05 07:32:13 +00001117 std::vector<Value*> Args;
1118 Args.reserve(NumActualArgs);
1119 SmallVector<AttributeWithIndex, 8> attrVec;
1120 attrVec.reserve(NumCommonArgs);
1121
1122 // Get any return attributes.
1123 Attributes RAttrs = CallerPAL.getRetAttributes();
1124
1125 // If the return value is not being used, the type may not be compatible
1126 // with the existing attributes. Wipe out any problematic attributes.
1127 RAttrs &= ~Attribute::typeIncompatible(NewRetTy);
1128
1129 // Add the new return attributes.
1130 if (RAttrs)
1131 attrVec.push_back(AttributeWithIndex::get(0, RAttrs));
1132
1133 AI = CS.arg_begin();
1134 for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001135 Type *ParamTy = FT->getParamType(i);
Chris Lattner753a2b42010-01-05 07:32:13 +00001136 if ((*AI)->getType() == ParamTy) {
1137 Args.push_back(*AI);
1138 } else {
1139 Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
1140 false, ParamTy, false);
Benjamin Kramera9390a42011-09-27 20:39:19 +00001141 Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy));
Chris Lattner753a2b42010-01-05 07:32:13 +00001142 }
1143
1144 // Add any parameter attributes.
1145 if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
1146 attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
1147 }
1148
1149 // If the function takes more arguments than the call was taking, add them
1150 // now.
1151 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
1152 Args.push_back(Constant::getNullValue(FT->getParamType(i)));
1153
1154 // If we are removing arguments to the function, emit an obnoxious warning.
1155 if (FT->getNumParams() < NumActualArgs) {
1156 if (!FT->isVarArg()) {
1157 errs() << "WARNING: While resolving call to function '"
1158 << Callee->getName() << "' arguments were dropped!\n";
1159 } else {
1160 // Add all of the arguments in their promoted form to the arg list.
1161 for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001162 Type *PTy = getPromotedType((*AI)->getType());
Chris Lattner753a2b42010-01-05 07:32:13 +00001163 if (PTy != (*AI)->getType()) {
1164 // Must promote to pass through va_arg area!
1165 Instruction::CastOps opcode =
1166 CastInst::getCastOpcode(*AI, false, PTy, false);
Benjamin Kramera9390a42011-09-27 20:39:19 +00001167 Args.push_back(Builder->CreateCast(opcode, *AI, PTy));
Chris Lattner753a2b42010-01-05 07:32:13 +00001168 } else {
1169 Args.push_back(*AI);
1170 }
1171
1172 // Add any parameter attributes.
1173 if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
1174 attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
1175 }
1176 }
1177 }
1178
1179 if (Attributes FnAttrs = CallerPAL.getFnAttributes())
1180 attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs));
1181
1182 if (NewRetTy->isVoidTy())
1183 Caller->setName(""); // Void type should not have a name.
1184
Chris Lattnerd509d0b2012-05-28 01:47:44 +00001185 const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec);
Chris Lattner753a2b42010-01-05 07:32:13 +00001186
1187 Instruction *NC;
1188 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
Eli Friedmanef819d02011-05-18 01:28:27 +00001189 NC = Builder->CreateInvoke(Callee, II->getNormalDest(),
Jay Foada3efbb12011-07-15 08:37:34 +00001190 II->getUnwindDest(), Args);
Eli Friedmanef819d02011-05-18 01:28:27 +00001191 NC->takeName(II);
Chris Lattner753a2b42010-01-05 07:32:13 +00001192 cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
1193 cast<InvokeInst>(NC)->setAttributes(NewCallerPAL);
1194 } else {
Chris Lattner753a2b42010-01-05 07:32:13 +00001195 CallInst *CI = cast<CallInst>(Caller);
Jay Foada3efbb12011-07-15 08:37:34 +00001196 NC = Builder->CreateCall(Callee, Args);
Eli Friedmanef819d02011-05-18 01:28:27 +00001197 NC->takeName(CI);
Chris Lattner753a2b42010-01-05 07:32:13 +00001198 if (CI->isTailCall())
1199 cast<CallInst>(NC)->setTailCall();
1200 cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
1201 cast<CallInst>(NC)->setAttributes(NewCallerPAL);
1202 }
1203
1204 // Insert a cast of the return type as necessary.
1205 Value *NV = NC;
1206 if (OldRetTy != NV->getType() && !Caller->use_empty()) {
1207 if (!NV->getType()->isVoidTy()) {
Chris Lattnerab215bc2010-12-20 08:25:06 +00001208 Instruction::CastOps opcode =
1209 CastInst::getCastOpcode(NC, false, OldRetTy, false);
Benjamin Kramera9390a42011-09-27 20:39:19 +00001210 NV = NC = CastInst::Create(opcode, NC, OldRetTy);
Eli Friedmana311c342011-05-27 00:19:40 +00001211 NC->setDebugLoc(Caller->getDebugLoc());
Chris Lattner753a2b42010-01-05 07:32:13 +00001212
1213 // If this is an invoke instruction, we should insert it after the first
1214 // non-phi, instruction in the normal successor block.
1215 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
Bill Wendling89d44112011-08-25 01:08:34 +00001216 BasicBlock::iterator I = II->getNormalDest()->getFirstInsertionPt();
Chris Lattner753a2b42010-01-05 07:32:13 +00001217 InsertNewInstBefore(NC, *I);
1218 } else {
Chris Lattnerab215bc2010-12-20 08:25:06 +00001219 // Otherwise, it's a call, just insert cast right after the call.
Chris Lattner753a2b42010-01-05 07:32:13 +00001220 InsertNewInstBefore(NC, *Caller);
1221 }
1222 Worklist.AddUsersToWorkList(*Caller);
1223 } else {
1224 NV = UndefValue::get(Caller->getType());
1225 }
1226 }
1227
Chris Lattner753a2b42010-01-05 07:32:13 +00001228 if (!Caller->use_empty())
Eli Friedman3e22cb92011-05-18 00:32:01 +00001229 ReplaceInstUsesWith(*Caller, NV);
1230
Chris Lattner753a2b42010-01-05 07:32:13 +00001231 EraseInstFromFunction(*Caller);
1232 return true;
1233}
1234
Duncan Sands4a544a72011-09-06 13:37:06 +00001235// transformCallThroughTrampoline - Turn a call to a function created by
1236// init_trampoline / adjust_trampoline intrinsic pair into a direct call to the
1237// underlying function.
Chris Lattner753a2b42010-01-05 07:32:13 +00001238//
Duncan Sands4a544a72011-09-06 13:37:06 +00001239Instruction *
1240InstCombiner::transformCallThroughTrampoline(CallSite CS,
1241 IntrinsicInst *Tramp) {
Chris Lattner753a2b42010-01-05 07:32:13 +00001242 Value *Callee = CS.getCalledValue();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001243 PointerType *PTy = cast<PointerType>(Callee->getType());
1244 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattner753a2b42010-01-05 07:32:13 +00001245 const AttrListPtr &Attrs = CS.getAttributes();
1246
1247 // If the call already has the 'nest' attribute somewhere then give up -
1248 // otherwise 'nest' would occur twice after splicing in the chain.
1249 if (Attrs.hasAttrSomewhere(Attribute::Nest))
1250 return 0;
1251
Duncan Sands4a544a72011-09-06 13:37:06 +00001252 assert(Tramp &&
1253 "transformCallThroughTrampoline called with incorrect CallSite.");
Chris Lattner753a2b42010-01-05 07:32:13 +00001254
Gabor Greifa3997812010-07-22 10:37:47 +00001255 Function *NestF =cast<Function>(Tramp->getArgOperand(1)->stripPointerCasts());
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001256 PointerType *NestFPTy = cast<PointerType>(NestF->getType());
1257 FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
Chris Lattner753a2b42010-01-05 07:32:13 +00001258
1259 const AttrListPtr &NestAttrs = NestF->getAttributes();
1260 if (!NestAttrs.isEmpty()) {
1261 unsigned NestIdx = 1;
Jay Foad5fdd6c82011-07-12 14:06:48 +00001262 Type *NestTy = 0;
Chris Lattner753a2b42010-01-05 07:32:13 +00001263 Attributes NestAttr = Attribute::None;
1264
1265 // Look for a parameter marked with the 'nest' attribute.
1266 for (FunctionType::param_iterator I = NestFTy->param_begin(),
1267 E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
1268 if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) {
1269 // Record the parameter type and any other attributes.
1270 NestTy = *I;
1271 NestAttr = NestAttrs.getParamAttributes(NestIdx);
1272 break;
1273 }
1274
1275 if (NestTy) {
1276 Instruction *Caller = CS.getInstruction();
1277 std::vector<Value*> NewArgs;
1278 NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1);
1279
1280 SmallVector<AttributeWithIndex, 8> NewAttrs;
1281 NewAttrs.reserve(Attrs.getNumSlots() + 1);
1282
1283 // Insert the nest argument into the call argument list, which may
1284 // mean appending it. Likewise for attributes.
1285
1286 // Add any result attributes.
1287 if (Attributes Attr = Attrs.getRetAttributes())
1288 NewAttrs.push_back(AttributeWithIndex::get(0, Attr));
1289
1290 {
1291 unsigned Idx = 1;
1292 CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
1293 do {
1294 if (Idx == NestIdx) {
1295 // Add the chain argument and attributes.
Gabor Greifcea7ac72010-06-24 12:58:35 +00001296 Value *NestVal = Tramp->getArgOperand(2);
Chris Lattner753a2b42010-01-05 07:32:13 +00001297 if (NestVal->getType() != NestTy)
Eli Friedmane6f364b2011-05-18 23:58:37 +00001298 NestVal = Builder->CreateBitCast(NestVal, NestTy, "nest");
Chris Lattner753a2b42010-01-05 07:32:13 +00001299 NewArgs.push_back(NestVal);
1300 NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr));
1301 }
1302
1303 if (I == E)
1304 break;
1305
1306 // Add the original argument and attributes.
1307 NewArgs.push_back(*I);
1308 if (Attributes Attr = Attrs.getParamAttributes(Idx))
1309 NewAttrs.push_back
1310 (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr));
1311
1312 ++Idx, ++I;
1313 } while (1);
1314 }
1315
1316 // Add any function attributes.
1317 if (Attributes Attr = Attrs.getFnAttributes())
1318 NewAttrs.push_back(AttributeWithIndex::get(~0, Attr));
1319
1320 // The trampoline may have been bitcast to a bogus type (FTy).
1321 // Handle this by synthesizing a new function type, equal to FTy
1322 // with the chain parameter inserted.
1323
Jay Foad5fdd6c82011-07-12 14:06:48 +00001324 std::vector<Type*> NewTypes;
Chris Lattner753a2b42010-01-05 07:32:13 +00001325 NewTypes.reserve(FTy->getNumParams()+1);
1326
1327 // Insert the chain's type into the list of parameter types, which may
1328 // mean appending it.
1329 {
1330 unsigned Idx = 1;
1331 FunctionType::param_iterator I = FTy->param_begin(),
1332 E = FTy->param_end();
1333
1334 do {
1335 if (Idx == NestIdx)
1336 // Add the chain's type.
1337 NewTypes.push_back(NestTy);
1338
1339 if (I == E)
1340 break;
1341
1342 // Add the original type.
1343 NewTypes.push_back(*I);
1344
1345 ++Idx, ++I;
1346 } while (1);
1347 }
1348
1349 // Replace the trampoline call with a direct call. Let the generic
1350 // code sort out any function type mismatches.
Jim Grosbach00e403a2012-02-03 00:07:04 +00001351 FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
Chris Lattner753a2b42010-01-05 07:32:13 +00001352 FTy->isVarArg());
1353 Constant *NewCallee =
1354 NestF->getType() == PointerType::getUnqual(NewFTy) ?
Jim Grosbach00e403a2012-02-03 00:07:04 +00001355 NestF : ConstantExpr::getBitCast(NestF,
Chris Lattner753a2b42010-01-05 07:32:13 +00001356 PointerType::getUnqual(NewFTy));
Chris Lattnerd509d0b2012-05-28 01:47:44 +00001357 const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs);
Chris Lattner753a2b42010-01-05 07:32:13 +00001358
1359 Instruction *NewCaller;
1360 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1361 NewCaller = InvokeInst::Create(NewCallee,
1362 II->getNormalDest(), II->getUnwindDest(),
Jay Foada3efbb12011-07-15 08:37:34 +00001363 NewArgs);
Chris Lattner753a2b42010-01-05 07:32:13 +00001364 cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
1365 cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
1366 } else {
Jay Foada3efbb12011-07-15 08:37:34 +00001367 NewCaller = CallInst::Create(NewCallee, NewArgs);
Chris Lattner753a2b42010-01-05 07:32:13 +00001368 if (cast<CallInst>(Caller)->isTailCall())
1369 cast<CallInst>(NewCaller)->setTailCall();
1370 cast<CallInst>(NewCaller)->
1371 setCallingConv(cast<CallInst>(Caller)->getCallingConv());
1372 cast<CallInst>(NewCaller)->setAttributes(NewPAL);
1373 }
Eli Friedman59f15912011-05-18 19:57:14 +00001374
1375 return NewCaller;
Chris Lattner753a2b42010-01-05 07:32:13 +00001376 }
1377 }
1378
1379 // Replace the trampoline call with a direct call. Since there is no 'nest'
1380 // parameter, there is no need to adjust the argument list. Let the generic
1381 // code sort out any function type mismatches.
1382 Constant *NewCallee =
Jim Grosbach00e403a2012-02-03 00:07:04 +00001383 NestF->getType() == PTy ? NestF :
Chris Lattner753a2b42010-01-05 07:32:13 +00001384 ConstantExpr::getBitCast(NestF, PTy);
1385 CS.setCalledFunction(NewCallee);
1386 return CS.getInstruction();
1387}