Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1 | //===- InstCombineCalls.cpp -----------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the visitCall and visitInvoke functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "InstCombine.h" |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 15 | #include "llvm/Support/CallSite.h" |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 16 | #include "llvm/DataLayout.h" |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/MemoryBuiltins.h" |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | /// getPromotedType - Return the specified type promoted as it would be to pass |
| 23 | /// though a va_arg area. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 24 | static Type *getPromotedType(Type *Ty) { |
| 25 | if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 26 | if (ITy->getBitWidth() < 32) |
| 27 | return Type::getInt32Ty(Ty->getContext()); |
| 28 | } |
| 29 | return Ty; |
| 30 | } |
| 31 | |
Dan Gohman | ce52bc5 | 2012-09-13 18:19:06 +0000 | [diff] [blame] | 32 | /// reduceToSingleValueType - Given an aggregate type which ultimately holds a |
| 33 | /// single scalar element, like {{{type}}} or [1 x type], return type. |
| 34 | static 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 Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 52 | |
| 53 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 54 | unsigned DstAlign = getKnownAlignment(MI->getArgOperand(0), TD); |
| 55 | unsigned SrcAlign = getKnownAlignment(MI->getArgOperand(1), TD); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 56 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| 57 | unsigned CopyAlign = MI->getAlignment(); |
| 58 | |
| 59 | if (CopyAlign < MinAlign) { |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 60 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 61 | MinAlign, false)); |
| 62 | return MI; |
| 63 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 65 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 66 | // load/store. |
Gabor Greif | bcda85c | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 67 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getArgOperand(2)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 68 | if (MemOpLength == 0) return 0; |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 70 | // 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 Liao | 9441ad0 | 2012-08-15 03:49:59 +0000 | [diff] [blame] | 74 | uint64_t Size = MemOpLength->getLimitedValue(); |
| 75 | assert(Size && "0-sized memory transfering should be removed already."); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 77 | if (Size > 8 || (Size&(Size-1))) |
| 78 | return 0; // If not 1/2/4/8 bytes, exit. |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 80 | // Use an integer load+store unless we can find something better. |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 81 | unsigned SrcAddrSp = |
Gabor Greif | bcda85c | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 82 | cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace(); |
Gabor Greif | 4ec2258 | 2010-04-16 15:33:14 +0000 | [diff] [blame] | 83 | unsigned DstAddrSp = |
Gabor Greif | bcda85c | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 84 | cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 85 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 86 | IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 87 | Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp); |
| 88 | Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 90 | // 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 Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 96 | Value *StrippedDest = MI->getArgOperand(0)->stripPointerCasts(); |
Dan Gohman | b998913 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 97 | MDNode *CopyMD = 0; |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 98 | if (StrippedDest != MI->getArgOperand(0)) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 99 | Type *SrcETy = cast<PointerType>(StrippedDest->getType()) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 100 | ->getElementType(); |
| 101 | if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
| 102 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 103 | // down through these levels if so. |
Dan Gohman | ce52bc5 | 2012-09-13 18:19:06 +0000 | [diff] [blame] | 104 | SrcETy = reduceToSingleValueType(SrcETy); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 105 | |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 106 | if (SrcETy->isSingleValueType()) { |
| 107 | NewSrcPtrTy = PointerType::get(SrcETy, SrcAddrSp); |
| 108 | NewDstPtrTy = PointerType::get(SrcETy, DstAddrSp); |
Dan Gohman | b998913 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 109 | |
| 110 | // If the memcpy has metadata describing the members, see if we can |
| 111 | // get the TBAA tag describing our copy. |
| 112 | if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) { |
| 113 | if (M->getNumOperands() == 3 && |
| 114 | isa<ConstantInt>(M->getOperand(0)) && |
| 115 | cast<ConstantInt>(M->getOperand(0))->isNullValue() && |
| 116 | isa<ConstantInt>(M->getOperand(1)) && |
| 117 | cast<ConstantInt>(M->getOperand(1))->getValue() == Size && |
| 118 | isa<MDNode>(M->getOperand(2))) |
| 119 | CopyMD = cast<MDNode>(M->getOperand(2)); |
| 120 | } |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 121 | } |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 125 | // If the memcpy/memmove provides better alignment info than we can |
| 126 | // infer, use it. |
| 127 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 128 | DstAlign = std::max(DstAlign, CopyAlign); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 129 | |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 130 | Value *Src = Builder->CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy); |
| 131 | Value *Dest = Builder->CreateBitCast(MI->getArgOperand(0), NewDstPtrTy); |
Eli Friedman | 59f1591 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 132 | LoadInst *L = Builder->CreateLoad(Src, MI->isVolatile()); |
| 133 | L->setAlignment(SrcAlign); |
Dan Gohman | b998913 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 134 | if (CopyMD) |
| 135 | L->setMetadata(LLVMContext::MD_tbaa, CopyMD); |
Eli Friedman | 59f1591 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 136 | StoreInst *S = Builder->CreateStore(L, Dest, MI->isVolatile()); |
| 137 | S->setAlignment(DstAlign); |
Dan Gohman | b998913 | 2012-09-13 21:51:01 +0000 | [diff] [blame] | 138 | if (CopyMD) |
| 139 | S->setMetadata(LLVMContext::MD_tbaa, CopyMD); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 140 | |
| 141 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
Gabor Greif | a90c5c7 | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 142 | MI->setArgOperand(2, Constant::getNullValue(MemOpLength->getType())); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 143 | return MI; |
| 144 | } |
| 145 | |
| 146 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
Chris Lattner | ae47be1 | 2010-12-25 20:52:04 +0000 | [diff] [blame] | 147 | unsigned Alignment = getKnownAlignment(MI->getDest(), TD); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 148 | if (MI->getAlignment() < Alignment) { |
| 149 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), |
| 150 | Alignment, false)); |
| 151 | return MI; |
| 152 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 154 | // Extract the length and alignment and fill if they are constant. |
| 155 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); |
| 156 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 157 | if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8)) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 158 | return 0; |
Michael Liao | 9441ad0 | 2012-08-15 03:49:59 +0000 | [diff] [blame] | 159 | uint64_t Len = LenC->getLimitedValue(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 160 | Alignment = MI->getAlignment(); |
Michael Liao | 9441ad0 | 2012-08-15 03:49:59 +0000 | [diff] [blame] | 161 | assert(Len && "0-sized memory setting should be removed already."); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 163 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) |
| 164 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 165 | Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8. |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 167 | Value *Dest = MI->getDest(); |
Mon P Wang | 55fb9b0 | 2010-12-20 01:05:30 +0000 | [diff] [blame] | 168 | unsigned DstAddrSp = cast<PointerType>(Dest->getType())->getAddressSpace(); |
| 169 | Type *NewDstPtrTy = PointerType::get(ITy, DstAddrSp); |
| 170 | Dest = Builder->CreateBitCast(Dest, NewDstPtrTy); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 171 | |
| 172 | // Alignment 0 is identity for alignment 1 for memset, but not store. |
| 173 | if (Alignment == 0) Alignment = 1; |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 175 | // Extract the fill value and store. |
| 176 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; |
Eli Friedman | 59f1591 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 177 | StoreInst *S = Builder->CreateStore(ConstantInt::get(ITy, Fill), Dest, |
| 178 | MI->isVolatile()); |
| 179 | S->setAlignment(Alignment); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 181 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 182 | MI->setLength(Constant::getNullValue(LenC->getType())); |
| 183 | return MI; |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 189 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 190 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 191 | /// the heavy lifting. |
| 192 | /// |
| 193 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 194 | if (isFreeCall(&CI, TLI)) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 195 | return visitFree(CI); |
| 196 | |
| 197 | // If the caller function is nounwind, mark the call as nounwind, even if the |
| 198 | // callee isn't. |
| 199 | if (CI.getParent()->getParent()->doesNotThrow() && |
| 200 | !CI.doesNotThrow()) { |
| 201 | CI.setDoesNotThrow(); |
| 202 | return &CI; |
| 203 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 205 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 206 | if (!II) return visitCallSite(&CI); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 208 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 209 | // visitCallSite. |
| 210 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
| 211 | bool Changed = false; |
| 212 | |
| 213 | // memmove/cpy/set of zero bytes is a noop. |
| 214 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
Chris Lattner | 6eff751 | 2010-10-01 05:51:02 +0000 | [diff] [blame] | 215 | if (NumBytes->isNullValue()) |
| 216 | return EraseInstFromFunction(CI); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 217 | |
| 218 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 219 | if (CI->getZExtValue() == 1) { |
| 220 | // Replace the instruction with just byte operations. We would |
| 221 | // transform other cases to loads/stores, but we don't know if |
| 222 | // alignment is sufficient. |
| 223 | } |
| 224 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 6eff751 | 2010-10-01 05:51:02 +0000 | [diff] [blame] | 226 | // No other transformations apply to volatile transfers. |
| 227 | if (MI->isVolatile()) |
| 228 | return 0; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 229 | |
| 230 | // If we have a memmove and the source operation is a constant global, |
| 231 | // then the source and dest pointers can't alias, so we can change this |
| 232 | // into a call to memcpy. |
| 233 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
| 234 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 235 | if (GVSrc->isConstant()) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 236 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 237 | Intrinsic::ID MemCpyID = Intrinsic::memcpy; |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 238 | Type *Tys[3] = { CI.getArgOperand(0)->getType(), |
| 239 | CI.getArgOperand(1)->getType(), |
| 240 | CI.getArgOperand(2)->getType() }; |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 241 | CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 242 | Changed = true; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { |
| 247 | // memmove(x,x,size) -> noop. |
| 248 | if (MTI->getSource() == MTI->getDest()) |
| 249 | return EraseInstFromFunction(CI); |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 250 | } |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 251 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 252 | // If we can determine a pointer alignment that is bigger than currently |
| 253 | // set, update the alignment. |
| 254 | if (isa<MemTransferInst>(MI)) { |
| 255 | if (Instruction *I = SimplifyMemTransfer(MI)) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 256 | return I; |
| 257 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
| 258 | if (Instruction *I = SimplifyMemSet(MSI)) |
| 259 | return I; |
| 260 | } |
Gabor Greif | c310fcc | 2010-06-24 13:42:49 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 262 | if (Changed) return II; |
| 263 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 265 | switch (II->getIntrinsicID()) { |
| 266 | default: break; |
Eric Christopher | 415326b | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 267 | case Intrinsic::objectsize: { |
Nuno Lopes | 9e72a79 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 268 | uint64_t Size; |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 269 | if (getObjectSize(II->getArgOperand(0), Size, TD, TLI)) |
Nuno Lopes | 9e72a79 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 270 | return ReplaceInstUsesWith(CI, ConstantInt::get(CI.getType(), Size)); |
| 271 | return 0; |
Eric Christopher | 415326b | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 272 | } |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 273 | case Intrinsic::bswap: |
| 274 | // bswap(bswap(x)) -> x |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 275 | if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 276 | if (Operand->getIntrinsicID() == Intrinsic::bswap) |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 277 | return ReplaceInstUsesWith(CI, Operand->getArgOperand(0)); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 279 | // bswap(trunc(bswap(x))) -> trunc(lshr(x, c)) |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 280 | if (TruncInst *TI = dyn_cast<TruncInst>(II->getArgOperand(0))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 281 | if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0))) |
| 282 | if (Operand->getIntrinsicID() == Intrinsic::bswap) { |
| 283 | unsigned C = Operand->getType()->getPrimitiveSizeInBits() - |
| 284 | TI->getType()->getPrimitiveSizeInBits(); |
| 285 | Value *CV = ConstantInt::get(Operand->getType(), C); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 286 | Value *V = Builder->CreateLShr(Operand->getArgOperand(0), CV); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 287 | return new TruncInst(V, TI->getType()); |
| 288 | } |
| 289 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 290 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 291 | break; |
| 292 | case Intrinsic::powi: |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 293 | if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 294 | // powi(x, 0) -> 1.0 |
| 295 | if (Power->isZero()) |
| 296 | return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0)); |
| 297 | // powi(x, 1) -> x |
| 298 | if (Power->isOne()) |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 299 | return ReplaceInstUsesWith(CI, II->getArgOperand(0)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 300 | // powi(x, -1) -> 1/x |
| 301 | if (Power->isAllOnesValue()) |
| 302 | return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0), |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 303 | II->getArgOperand(0)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 304 | } |
| 305 | break; |
| 306 | case Intrinsic::cttz: { |
| 307 | // If all bits below the first known one are known zero, |
| 308 | // this value is constant. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 309 | IntegerType *IT = dyn_cast<IntegerType>(II->getArgOperand(0)->getType()); |
Owen Anderson | f1ac465 | 2011-07-01 21:52:38 +0000 | [diff] [blame] | 310 | // FIXME: Try to simplify vectors of integers. |
| 311 | if (!IT) break; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 312 | uint32_t BitWidth = IT->getBitWidth(); |
| 313 | APInt KnownZero(BitWidth, 0); |
| 314 | APInt KnownOne(BitWidth, 0); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 315 | ComputeMaskedBits(II->getArgOperand(0), KnownZero, KnownOne); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 316 | unsigned TrailingZeros = KnownOne.countTrailingZeros(); |
| 317 | APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros)); |
| 318 | if ((Mask & KnownZero) == Mask) |
| 319 | return ReplaceInstUsesWith(CI, ConstantInt::get(IT, |
| 320 | APInt(BitWidth, TrailingZeros))); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 322 | } |
| 323 | break; |
| 324 | case Intrinsic::ctlz: { |
| 325 | // If all bits above the first known one are known zero, |
| 326 | // this value is constant. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 327 | IntegerType *IT = dyn_cast<IntegerType>(II->getArgOperand(0)->getType()); |
Owen Anderson | f1ac465 | 2011-07-01 21:52:38 +0000 | [diff] [blame] | 328 | // FIXME: Try to simplify vectors of integers. |
| 329 | if (!IT) break; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 330 | uint32_t BitWidth = IT->getBitWidth(); |
| 331 | APInt KnownZero(BitWidth, 0); |
| 332 | APInt KnownOne(BitWidth, 0); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 333 | ComputeMaskedBits(II->getArgOperand(0), KnownZero, KnownOne); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 334 | unsigned LeadingZeros = KnownOne.countLeadingZeros(); |
| 335 | APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros)); |
| 336 | if ((Mask & KnownZero) == Mask) |
| 337 | return ReplaceInstUsesWith(CI, ConstantInt::get(IT, |
| 338 | APInt(BitWidth, LeadingZeros))); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 340 | } |
| 341 | break; |
| 342 | case Intrinsic::uadd_with_overflow: { |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 343 | Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 344 | IntegerType *IT = cast<IntegerType>(II->getArgOperand(0)->getType()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 345 | uint32_t BitWidth = IT->getBitWidth(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 346 | APInt LHSKnownZero(BitWidth, 0); |
| 347 | APInt LHSKnownOne(BitWidth, 0); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 348 | ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 349 | bool LHSKnownNegative = LHSKnownOne[BitWidth - 1]; |
| 350 | bool LHSKnownPositive = LHSKnownZero[BitWidth - 1]; |
| 351 | |
| 352 | if (LHSKnownNegative || LHSKnownPositive) { |
| 353 | APInt RHSKnownZero(BitWidth, 0); |
| 354 | APInt RHSKnownOne(BitWidth, 0); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 355 | ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 356 | bool RHSKnownNegative = RHSKnownOne[BitWidth - 1]; |
| 357 | bool RHSKnownPositive = RHSKnownZero[BitWidth - 1]; |
| 358 | if (LHSKnownNegative && RHSKnownNegative) { |
| 359 | // The sign bit is set in both cases: this MUST overflow. |
| 360 | // Create a simple add instruction, and insert it into the struct. |
Eli Friedman | 59f1591 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 361 | Value *Add = Builder->CreateAdd(LHS, RHS); |
| 362 | Add->takeName(&CI); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 363 | Constant *V[] = { |
Eli Friedman | 59f1591 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 364 | UndefValue::get(LHS->getType()), |
| 365 | ConstantInt::getTrue(II->getContext()) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 366 | }; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 367 | StructType *ST = cast<StructType>(II->getType()); |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 368 | Constant *Struct = ConstantStruct::get(ST, V); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 369 | return InsertValueInst::Create(Struct, Add, 0); |
| 370 | } |
Eli Friedman | 59f1591 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 372 | if (LHSKnownPositive && RHSKnownPositive) { |
| 373 | // The sign bit is clear in both cases: this CANNOT overflow. |
| 374 | // Create a simple add instruction, and insert it into the struct. |
Eli Friedman | 59f1591 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 375 | Value *Add = Builder->CreateNUWAdd(LHS, RHS); |
| 376 | Add->takeName(&CI); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 377 | Constant *V[] = { |
| 378 | UndefValue::get(LHS->getType()), |
| 379 | ConstantInt::getFalse(II->getContext()) |
| 380 | }; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 381 | StructType *ST = cast<StructType>(II->getType()); |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 382 | Constant *Struct = ConstantStruct::get(ST, V); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 383 | return InsertValueInst::Create(Struct, Add, 0); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | // FALL THROUGH uadd into sadd |
| 388 | case Intrinsic::sadd_with_overflow: |
| 389 | // Canonicalize constants into the RHS. |
Gabor Greif | a90c5c7 | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 390 | if (isa<Constant>(II->getArgOperand(0)) && |
| 391 | !isa<Constant>(II->getArgOperand(1))) { |
| 392 | Value *LHS = II->getArgOperand(0); |
| 393 | II->setArgOperand(0, II->getArgOperand(1)); |
| 394 | II->setArgOperand(1, LHS); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 395 | return II; |
| 396 | } |
| 397 | |
| 398 | // X + undef -> undef |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 399 | if (isa<UndefValue>(II->getArgOperand(1))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 400 | return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 401 | |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 402 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 403 | // X + 0 -> {X, false} |
| 404 | if (RHS->isZero()) { |
| 405 | Constant *V[] = { |
Eli Friedman | 4fffb34 | 2010-08-09 20:49:43 +0000 | [diff] [blame] | 406 | UndefValue::get(II->getArgOperand(0)->getType()), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 407 | ConstantInt::getFalse(II->getContext()) |
| 408 | }; |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 409 | Constant *Struct = |
| 410 | ConstantStruct::get(cast<StructType>(II->getType()), V); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 411 | return InsertValueInst::Create(Struct, II->getArgOperand(0), 0); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | break; |
| 415 | case Intrinsic::usub_with_overflow: |
| 416 | case Intrinsic::ssub_with_overflow: |
| 417 | // undef - X -> undef |
| 418 | // X - undef -> undef |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 419 | if (isa<UndefValue>(II->getArgOperand(0)) || |
| 420 | isa<UndefValue>(II->getArgOperand(1))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 421 | return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 422 | |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 423 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 424 | // X - 0 -> {X, false} |
| 425 | if (RHS->isZero()) { |
| 426 | Constant *V[] = { |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 427 | UndefValue::get(II->getArgOperand(0)->getType()), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 428 | ConstantInt::getFalse(II->getContext()) |
| 429 | }; |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 430 | Constant *Struct = |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 431 | ConstantStruct::get(cast<StructType>(II->getType()), V); |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 432 | return InsertValueInst::Create(Struct, II->getArgOperand(0), 0); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | break; |
Benjamin Kramer | 6b96fe7 | 2011-03-10 18:40:14 +0000 | [diff] [blame] | 436 | case Intrinsic::umul_with_overflow: { |
| 437 | Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1); |
| 438 | unsigned BitWidth = cast<IntegerType>(LHS->getType())->getBitWidth(); |
Benjamin Kramer | 6b96fe7 | 2011-03-10 18:40:14 +0000 | [diff] [blame] | 439 | |
| 440 | APInt LHSKnownZero(BitWidth, 0); |
| 441 | APInt LHSKnownOne(BitWidth, 0); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 442 | ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne); |
Benjamin Kramer | 6b96fe7 | 2011-03-10 18:40:14 +0000 | [diff] [blame] | 443 | APInt RHSKnownZero(BitWidth, 0); |
| 444 | APInt RHSKnownOne(BitWidth, 0); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 445 | ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne); |
Benjamin Kramer | 6b96fe7 | 2011-03-10 18:40:14 +0000 | [diff] [blame] | 446 | |
Benjamin Kramer | d655e6e | 2011-03-27 15:04:38 +0000 | [diff] [blame] | 447 | // Get the largest possible values for each operand. |
| 448 | APInt LHSMax = ~LHSKnownZero; |
| 449 | APInt RHSMax = ~RHSKnownZero; |
Benjamin Kramer | 6b96fe7 | 2011-03-10 18:40:14 +0000 | [diff] [blame] | 450 | |
| 451 | // If multiplying the maximum values does not overflow then we can turn |
| 452 | // this into a plain NUW mul. |
Benjamin Kramer | d655e6e | 2011-03-27 15:04:38 +0000 | [diff] [blame] | 453 | bool Overflow; |
| 454 | LHSMax.umul_ov(RHSMax, Overflow); |
| 455 | if (!Overflow) { |
Benjamin Kramer | 6b96fe7 | 2011-03-10 18:40:14 +0000 | [diff] [blame] | 456 | Value *Mul = Builder->CreateNUWMul(LHS, RHS, "umul_with_overflow"); |
| 457 | Constant *V[] = { |
| 458 | UndefValue::get(LHS->getType()), |
| 459 | Builder->getFalse() |
| 460 | }; |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 461 | Constant *Struct = ConstantStruct::get(cast<StructType>(II->getType()),V); |
Benjamin Kramer | 6b96fe7 | 2011-03-10 18:40:14 +0000 | [diff] [blame] | 462 | return InsertValueInst::Create(Struct, Mul, 0); |
| 463 | } |
| 464 | } // FALL THROUGH |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 465 | case Intrinsic::smul_with_overflow: |
| 466 | // Canonicalize constants into the RHS. |
Gabor Greif | a90c5c7 | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 467 | if (isa<Constant>(II->getArgOperand(0)) && |
| 468 | !isa<Constant>(II->getArgOperand(1))) { |
| 469 | Value *LHS = II->getArgOperand(0); |
| 470 | II->setArgOperand(0, II->getArgOperand(1)); |
| 471 | II->setArgOperand(1, LHS); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 472 | return II; |
| 473 | } |
| 474 | |
| 475 | // X * undef -> undef |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 476 | if (isa<UndefValue>(II->getArgOperand(1))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 477 | return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 478 | |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 479 | if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getArgOperand(1))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 480 | // X*0 -> {0, false} |
| 481 | if (RHSI->isZero()) |
| 482 | return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType())); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 484 | // X * 1 -> {X, false} |
| 485 | if (RHSI->equalsInt(1)) { |
| 486 | Constant *V[] = { |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 487 | UndefValue::get(II->getArgOperand(0)->getType()), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 488 | ConstantInt::getFalse(II->getContext()) |
| 489 | }; |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 490 | Constant *Struct = |
Chris Lattner | b065b06 | 2011-06-20 04:01:31 +0000 | [diff] [blame] | 491 | ConstantStruct::get(cast<StructType>(II->getType()), V); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 492 | return InsertValueInst::Create(Struct, II->getArgOperand(0), 0); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | break; |
| 496 | case Intrinsic::ppc_altivec_lvx: |
| 497 | case Intrinsic::ppc_altivec_lvxl: |
Bill Wendling | f93f7b2 | 2011-04-13 00:36:11 +0000 | [diff] [blame] | 498 | // Turn PPC lvx -> load if the pointer is known aligned. |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 499 | if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, TD) >= 16) { |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 500 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 501 | PointerType::getUnqual(II->getType())); |
| 502 | return new LoadInst(Ptr); |
| 503 | } |
| 504 | break; |
| 505 | case Intrinsic::ppc_altivec_stvx: |
| 506 | case Intrinsic::ppc_altivec_stvxl: |
| 507 | // Turn stvx -> store if the pointer is known aligned. |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 508 | if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, TD) >= 16) { |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 509 | Type *OpPtrTy = |
Gabor Greif | 2f1ab74 | 2010-06-24 15:51:11 +0000 | [diff] [blame] | 510 | PointerType::getUnqual(II->getArgOperand(0)->getType()); |
| 511 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy); |
| 512 | return new StoreInst(II->getArgOperand(0), Ptr); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 513 | } |
| 514 | break; |
| 515 | case Intrinsic::x86_sse_storeu_ps: |
| 516 | case Intrinsic::x86_sse2_storeu_pd: |
| 517 | case Intrinsic::x86_sse2_storeu_dq: |
| 518 | // Turn X86 storeu -> store if the pointer is known aligned. |
Chris Lattner | 687140c | 2010-12-25 20:37:57 +0000 | [diff] [blame] | 519 | if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, TD) >= 16) { |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 520 | Type *OpPtrTy = |
Gabor Greif | 2f1ab74 | 2010-06-24 15:51:11 +0000 | [diff] [blame] | 521 | PointerType::getUnqual(II->getArgOperand(1)->getType()); |
| 522 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), OpPtrTy); |
| 523 | return new StoreInst(II->getArgOperand(1), Ptr); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 524 | } |
| 525 | break; |
Chandler Carruth | 9cc9f50 | 2011-01-10 07:19:37 +0000 | [diff] [blame] | 526 | |
| 527 | case Intrinsic::x86_sse_cvtss2si: |
| 528 | case Intrinsic::x86_sse_cvtss2si64: |
| 529 | case Intrinsic::x86_sse_cvttss2si: |
| 530 | case Intrinsic::x86_sse_cvttss2si64: |
| 531 | case Intrinsic::x86_sse2_cvtsd2si: |
| 532 | case Intrinsic::x86_sse2_cvtsd2si64: |
| 533 | case Intrinsic::x86_sse2_cvttsd2si: |
| 534 | case Intrinsic::x86_sse2_cvttsd2si64: { |
| 535 | // These intrinsics only demand the 0th element of their input vectors. If |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 536 | // we can simplify the input based on that, do so now. |
| 537 | unsigned VWidth = |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 538 | cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 539 | APInt DemandedElts(VWidth, 1); |
| 540 | APInt UndefElts(VWidth, 0); |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 541 | if (Value *V = SimplifyDemandedVectorElts(II->getArgOperand(0), |
| 542 | DemandedElts, UndefElts)) { |
Gabor Greif | a90c5c7 | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 543 | II->setArgOperand(0, V); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 544 | return II; |
| 545 | } |
| 546 | break; |
| 547 | } |
Chandler Carruth | 9cc9f50 | 2011-01-10 07:19:37 +0000 | [diff] [blame] | 548 | |
Stuart Hastings | ca1ef48 | 2011-05-17 22:13:31 +0000 | [diff] [blame] | 549 | |
| 550 | case Intrinsic::x86_sse41_pmovsxbw: |
| 551 | case Intrinsic::x86_sse41_pmovsxwd: |
| 552 | case Intrinsic::x86_sse41_pmovsxdq: |
| 553 | case Intrinsic::x86_sse41_pmovzxbw: |
| 554 | case Intrinsic::x86_sse41_pmovzxwd: |
| 555 | case Intrinsic::x86_sse41_pmovzxdq: { |
Evan Cheng | aaa7f49 | 2011-05-19 18:18:39 +0000 | [diff] [blame] | 556 | // pmov{s|z}x ignores the upper half of their input vectors. |
Stuart Hastings | ca1ef48 | 2011-05-17 22:13:31 +0000 | [diff] [blame] | 557 | unsigned VWidth = |
| 558 | cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements(); |
| 559 | unsigned LowHalfElts = VWidth / 2; |
Stuart Hastings | d116611 | 2011-05-18 15:54:26 +0000 | [diff] [blame] | 560 | APInt InputDemandedElts(APInt::getBitsSet(VWidth, 0, LowHalfElts)); |
Stuart Hastings | ca1ef48 | 2011-05-17 22:13:31 +0000 | [diff] [blame] | 561 | APInt UndefElts(VWidth, 0); |
| 562 | if (Value *TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0), |
| 563 | InputDemandedElts, |
| 564 | UndefElts)) { |
| 565 | II->setArgOperand(0, TmpV); |
| 566 | return II; |
| 567 | } |
| 568 | break; |
| 569 | } |
| 570 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 571 | case Intrinsic::ppc_altivec_vperm: |
| 572 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 573 | if (Constant *Mask = dyn_cast<Constant>(II->getArgOperand(2))) { |
| 574 | assert(Mask->getType()->getVectorNumElements() == 16 && |
| 575 | "Bad type for intrinsic!"); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 577 | // Check that all of the elements are integer constants or undefs. |
| 578 | bool AllEltsOk = true; |
| 579 | for (unsigned i = 0; i != 16; ++i) { |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 580 | Constant *Elt = Mask->getAggregateElement(i); |
| 581 | if (Elt == 0 || |
| 582 | !(isa<ConstantInt>(Elt) || isa<UndefValue>(Elt))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 583 | AllEltsOk = false; |
| 584 | break; |
| 585 | } |
| 586 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 588 | if (AllEltsOk) { |
| 589 | // Cast the input vectors to byte vectors. |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 590 | Value *Op0 = Builder->CreateBitCast(II->getArgOperand(0), |
| 591 | Mask->getType()); |
| 592 | Value *Op1 = Builder->CreateBitCast(II->getArgOperand(1), |
| 593 | Mask->getType()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 594 | Value *Result = UndefValue::get(Op0->getType()); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 595 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 596 | // Only extract each element once. |
| 597 | Value *ExtractedElts[32]; |
| 598 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 600 | for (unsigned i = 0; i != 16; ++i) { |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 601 | if (isa<UndefValue>(Mask->getAggregateElement(i))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 602 | continue; |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 603 | unsigned Idx = |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 604 | cast<ConstantInt>(Mask->getAggregateElement(i))->getZExtValue(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 605 | Idx &= 31; // Match the hardware behavior. |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 607 | if (ExtractedElts[Idx] == 0) { |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 608 | ExtractedElts[Idx] = |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 609 | Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1, |
| 610 | Builder->getInt32(Idx&15)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 611 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 612 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 613 | // Insert this value into the result vector. |
| 614 | Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx], |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 615 | Builder->getInt32(i)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 616 | } |
| 617 | return CastInst::Create(Instruction::BitCast, Result, CI.getType()); |
| 618 | } |
| 619 | } |
| 620 | break; |
| 621 | |
Bob Wilson | 364f17c | 2010-10-22 21:41:48 +0000 | [diff] [blame] | 622 | case Intrinsic::arm_neon_vld1: |
| 623 | case Intrinsic::arm_neon_vld2: |
| 624 | case Intrinsic::arm_neon_vld3: |
| 625 | case Intrinsic::arm_neon_vld4: |
| 626 | case Intrinsic::arm_neon_vld2lane: |
| 627 | case Intrinsic::arm_neon_vld3lane: |
| 628 | case Intrinsic::arm_neon_vld4lane: |
| 629 | case Intrinsic::arm_neon_vst1: |
| 630 | case Intrinsic::arm_neon_vst2: |
| 631 | case Intrinsic::arm_neon_vst3: |
| 632 | case Intrinsic::arm_neon_vst4: |
| 633 | case Intrinsic::arm_neon_vst2lane: |
| 634 | case Intrinsic::arm_neon_vst3lane: |
| 635 | case Intrinsic::arm_neon_vst4lane: { |
Chris Lattner | ae47be1 | 2010-12-25 20:52:04 +0000 | [diff] [blame] | 636 | unsigned MemAlign = getKnownAlignment(II->getArgOperand(0), TD); |
Bob Wilson | 364f17c | 2010-10-22 21:41:48 +0000 | [diff] [blame] | 637 | unsigned AlignArg = II->getNumArgOperands() - 1; |
| 638 | ConstantInt *IntrAlign = dyn_cast<ConstantInt>(II->getArgOperand(AlignArg)); |
| 639 | if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) { |
| 640 | II->setArgOperand(AlignArg, |
| 641 | ConstantInt::get(Type::getInt32Ty(II->getContext()), |
| 642 | MemAlign, false)); |
| 643 | return II; |
| 644 | } |
| 645 | break; |
| 646 | } |
| 647 | |
Lang Hames | 973f72a | 2012-05-01 00:20:38 +0000 | [diff] [blame] | 648 | case Intrinsic::arm_neon_vmulls: |
| 649 | case Intrinsic::arm_neon_vmullu: { |
| 650 | Value *Arg0 = II->getArgOperand(0); |
| 651 | Value *Arg1 = II->getArgOperand(1); |
| 652 | |
| 653 | // Handle mul by zero first: |
| 654 | if (isa<ConstantAggregateZero>(Arg0) || isa<ConstantAggregateZero>(Arg1)) { |
| 655 | return ReplaceInstUsesWith(CI, ConstantAggregateZero::get(II->getType())); |
| 656 | } |
| 657 | |
| 658 | // Check for constant LHS & RHS - in this case we just simplify. |
| 659 | bool Zext = (II->getIntrinsicID() == Intrinsic::arm_neon_vmullu); |
| 660 | VectorType *NewVT = cast<VectorType>(II->getType()); |
| 661 | unsigned NewWidth = NewVT->getElementType()->getIntegerBitWidth(); |
| 662 | if (ConstantDataVector *CV0 = dyn_cast<ConstantDataVector>(Arg0)) { |
| 663 | if (ConstantDataVector *CV1 = dyn_cast<ConstantDataVector>(Arg1)) { |
| 664 | VectorType* VT = cast<VectorType>(CV0->getType()); |
| 665 | SmallVector<Constant*, 4> NewElems; |
| 666 | for (unsigned i = 0; i < VT->getNumElements(); ++i) { |
| 667 | APInt CV0E = |
| 668 | (cast<ConstantInt>(CV0->getAggregateElement(i)))->getValue(); |
| 669 | CV0E = Zext ? CV0E.zext(NewWidth) : CV0E.sext(NewWidth); |
| 670 | APInt CV1E = |
| 671 | (cast<ConstantInt>(CV1->getAggregateElement(i)))->getValue(); |
| 672 | CV1E = Zext ? CV1E.zext(NewWidth) : CV1E.sext(NewWidth); |
| 673 | NewElems.push_back( |
| 674 | ConstantInt::get(NewVT->getElementType(), CV0E * CV1E)); |
| 675 | } |
| 676 | return ReplaceInstUsesWith(CI, ConstantVector::get(NewElems)); |
| 677 | } |
| 678 | |
| 679 | // Couldn't simplify - cannonicalize constant to the RHS. |
| 680 | std::swap(Arg0, Arg1); |
| 681 | } |
| 682 | |
| 683 | // Handle mul by one: |
| 684 | if (ConstantDataVector *CV1 = dyn_cast<ConstantDataVector>(Arg1)) { |
| 685 | if (ConstantInt *Splat = |
| 686 | dyn_cast_or_null<ConstantInt>(CV1->getSplatValue())) { |
| 687 | if (Splat->isOne()) { |
| 688 | if (Zext) |
| 689 | return CastInst::CreateZExtOrBitCast(Arg0, II->getType()); |
| 690 | // else |
| 691 | return CastInst::CreateSExtOrBitCast(Arg0, II->getType()); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | break; |
| 697 | } |
| 698 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 699 | case Intrinsic::stackrestore: { |
| 700 | // If the save is right next to the restore, remove the restore. This can |
| 701 | // happen when variable allocas are DCE'd. |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 702 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 703 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 704 | BasicBlock::iterator BI = SS; |
| 705 | if (&*++BI == II) |
| 706 | return EraseInstFromFunction(CI); |
| 707 | } |
| 708 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 709 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 710 | // Scan down this block to see if there is another stack restore in the |
| 711 | // same block without an intervening call/alloca. |
| 712 | BasicBlock::iterator BI = II; |
| 713 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 714 | bool CannotRemove = false; |
| 715 | for (++BI; &*BI != TI; ++BI) { |
Nuno Lopes | 9e72a79 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 716 | if (isa<AllocaInst>(BI)) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 717 | CannotRemove = true; |
| 718 | break; |
| 719 | } |
| 720 | if (CallInst *BCI = dyn_cast<CallInst>(BI)) { |
| 721 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { |
| 722 | // If there is a stackrestore below this one, remove this one. |
| 723 | if (II->getIntrinsicID() == Intrinsic::stackrestore) |
| 724 | return EraseInstFromFunction(CI); |
| 725 | // Otherwise, ignore the intrinsic. |
| 726 | } else { |
| 727 | // If we found a non-intrinsic call, we can't remove the stack |
| 728 | // restore. |
| 729 | CannotRemove = true; |
| 730 | break; |
| 731 | } |
| 732 | } |
| 733 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 734 | |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 735 | // If the stack restore is in a return, resume, or unwind block and if there |
| 736 | // are no allocas or calls between the restore and the return, nuke the |
| 737 | // restore. |
Bill Wendling | aa5abe8 | 2012-02-06 21:16:41 +0000 | [diff] [blame] | 738 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 739 | return EraseInstFromFunction(CI); |
| 740 | break; |
| 741 | } |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | return visitCallSite(II); |
| 745 | } |
| 746 | |
| 747 | // InvokeInst simplification |
| 748 | // |
| 749 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
| 750 | return visitCallSite(&II); |
| 751 | } |
| 752 | |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 753 | /// isSafeToEliminateVarargsCast - If this cast does not affect the value |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 754 | /// passed through the varargs area, we can eliminate the use of the cast. |
| 755 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
| 756 | const CastInst * const CI, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 757 | const DataLayout * const TD, |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 758 | const int ix) { |
| 759 | if (!CI->isLosslessCast()) |
| 760 | return false; |
| 761 | |
| 762 | // The size of ByVal arguments is derived from the type, so we |
| 763 | // can't change to a type with a different size. If the size were |
| 764 | // passed explicitly we could avoid this check. |
Nick Lewycky | 173862e | 2011-11-20 19:09:04 +0000 | [diff] [blame] | 765 | if (!CS.isByValArgument(ix)) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 766 | return true; |
| 767 | |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 768 | Type* SrcTy = |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 769 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 770 | Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 771 | if (!SrcTy->isSized() || !DstTy->isSized()) |
| 772 | return false; |
| 773 | if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy)) |
| 774 | return false; |
| 775 | return true; |
| 776 | } |
| 777 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 778 | namespace { |
| 779 | class InstCombineFortifiedLibCalls : public SimplifyFortifiedLibCalls { |
| 780 | InstCombiner *IC; |
| 781 | protected: |
| 782 | void replaceCall(Value *With) { |
| 783 | NewInstruction = IC->ReplaceInstUsesWith(*CI, With); |
| 784 | } |
| 785 | bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const { |
Benjamin Kramer | 8143a84 | 2011-01-06 14:22:52 +0000 | [diff] [blame] | 786 | if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp)) |
| 787 | return true; |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 788 | if (ConstantInt *SizeCI = |
| 789 | dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) { |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 790 | if (SizeCI->isAllOnesValue()) |
| 791 | return true; |
Eric Christopher | b9b80c3 | 2011-03-15 00:25:41 +0000 | [diff] [blame] | 792 | if (isString) { |
| 793 | uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp)); |
| 794 | // If the length is 0 we don't know how long it is and so we can't |
| 795 | // remove the check. |
| 796 | if (Len == 0) return false; |
| 797 | return SizeCI->getZExtValue() >= Len; |
| 798 | } |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 799 | if (ConstantInt *Arg = dyn_cast<ConstantInt>( |
| 800 | CI->getArgOperand(SizeArgOp))) |
Evan Cheng | 9d8f002 | 2010-03-23 06:06:09 +0000 | [diff] [blame] | 801 | return SizeCI->getZExtValue() >= Arg->getZExtValue(); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 802 | } |
| 803 | return false; |
| 804 | } |
| 805 | public: |
| 806 | InstCombineFortifiedLibCalls(InstCombiner *IC) : IC(IC), NewInstruction(0) { } |
| 807 | Instruction *NewInstruction; |
| 808 | }; |
| 809 | } // end anonymous namespace |
| 810 | |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 811 | // Try to fold some different type of calls here. |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 812 | // Currently we're only working with the checking functions, memcpy_chk, |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 813 | // mempcpy_chk, memmove_chk, memset_chk, strcpy_chk, stpcpy_chk, strncpy_chk, |
| 814 | // strcat_chk and strncat_chk. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 815 | Instruction *InstCombiner::tryOptimizeCall(CallInst *CI, const DataLayout *TD) { |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 816 | if (CI->getCalledFunction() == 0) return 0; |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 817 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 818 | InstCombineFortifiedLibCalls Simplifier(this); |
Nuno Lopes | 51004df | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 819 | Simplifier.fold(CI, TD, TLI); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 820 | return Simplifier.NewInstruction; |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 823 | static IntrinsicInst *FindInitTrampolineFromAlloca(Value *TrampMem) { |
| 824 | // Strip off at most one level of pointer casts, looking for an alloca. This |
| 825 | // is good enough in practice and simpler than handling any number of casts. |
| 826 | Value *Underlying = TrampMem->stripPointerCasts(); |
| 827 | if (Underlying != TrampMem && |
| 828 | (!Underlying->hasOneUse() || *Underlying->use_begin() != TrampMem)) |
| 829 | return 0; |
| 830 | if (!isa<AllocaInst>(Underlying)) |
| 831 | return 0; |
| 832 | |
| 833 | IntrinsicInst *InitTrampoline = 0; |
| 834 | for (Value::use_iterator I = TrampMem->use_begin(), E = TrampMem->use_end(); |
| 835 | I != E; I++) { |
| 836 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(*I); |
| 837 | if (!II) |
| 838 | return 0; |
| 839 | if (II->getIntrinsicID() == Intrinsic::init_trampoline) { |
| 840 | if (InitTrampoline) |
| 841 | // More than one init_trampoline writes to this value. Give up. |
| 842 | return 0; |
| 843 | InitTrampoline = II; |
| 844 | continue; |
| 845 | } |
| 846 | if (II->getIntrinsicID() == Intrinsic::adjust_trampoline) |
| 847 | // Allow any number of calls to adjust.trampoline. |
| 848 | continue; |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | // No call to init.trampoline found. |
| 853 | if (!InitTrampoline) |
| 854 | return 0; |
| 855 | |
| 856 | // Check that the alloca is being used in the expected way. |
| 857 | if (InitTrampoline->getOperand(0) != TrampMem) |
| 858 | return 0; |
| 859 | |
| 860 | return InitTrampoline; |
| 861 | } |
| 862 | |
| 863 | static IntrinsicInst *FindInitTrampolineFromBB(IntrinsicInst *AdjustTramp, |
| 864 | Value *TrampMem) { |
| 865 | // Visit all the previous instructions in the basic block, and try to find a |
| 866 | // init.trampoline which has a direct path to the adjust.trampoline. |
| 867 | for (BasicBlock::iterator I = AdjustTramp, |
| 868 | E = AdjustTramp->getParent()->begin(); I != E; ) { |
| 869 | Instruction *Inst = --I; |
| 870 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) |
| 871 | if (II->getIntrinsicID() == Intrinsic::init_trampoline && |
| 872 | II->getOperand(0) == TrampMem) |
| 873 | return II; |
| 874 | if (Inst->mayWriteToMemory()) |
| 875 | return 0; |
| 876 | } |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | // Given a call to llvm.adjust.trampoline, find and return the corresponding |
| 881 | // call to llvm.init.trampoline if the call to the trampoline can be optimized |
| 882 | // to a direct call to a function. Otherwise return NULL. |
| 883 | // |
| 884 | static IntrinsicInst *FindInitTrampoline(Value *Callee) { |
| 885 | Callee = Callee->stripPointerCasts(); |
| 886 | IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee); |
| 887 | if (!AdjustTramp || |
| 888 | AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline) |
| 889 | return 0; |
| 890 | |
| 891 | Value *TrampMem = AdjustTramp->getOperand(0); |
| 892 | |
| 893 | if (IntrinsicInst *IT = FindInitTrampolineFromAlloca(TrampMem)) |
| 894 | return IT; |
| 895 | if (IntrinsicInst *IT = FindInitTrampolineFromBB(AdjustTramp, TrampMem)) |
| 896 | return IT; |
| 897 | return 0; |
| 898 | } |
| 899 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 900 | // visitCallSite - Improvements for call and invoke instructions. |
| 901 | // |
| 902 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 903 | if (isAllocLikeFn(CS.getInstruction(), TLI)) |
Nuno Lopes | 78f8ef4 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 904 | return visitAllocSite(*CS.getInstruction()); |
Nuno Lopes | 2b3e958 | 2012-06-21 21:25:05 +0000 | [diff] [blame] | 905 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 906 | bool Changed = false; |
| 907 | |
Chris Lattner | ab215bc | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 908 | // If the callee is a pointer to a function, attempt to move any casts to the |
| 909 | // arguments of the call/invoke. |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 910 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | ab215bc | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 911 | if (!isa<Function>(Callee) && transformConstExprCastCall(CS)) |
| 912 | return 0; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 913 | |
| 914 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
Chris Lattner | d569561 | 2010-02-01 18:11:34 +0000 | [diff] [blame] | 915 | // If the call and callee calling conventions don't match, this call must |
| 916 | // be unreachable, as the call is undefined. |
| 917 | if (CalleeF->getCallingConv() != CS.getCallingConv() && |
| 918 | // Only do this for calls to a function with a body. A prototype may |
| 919 | // not actually end up matching the implementation's calling conv for a |
| 920 | // variety of reasons (e.g. it may be written in assembly). |
| 921 | !CalleeF->isDeclaration()) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 922 | Instruction *OldCall = CS.getInstruction(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 923 | new StoreInst(ConstantInt::getTrue(Callee->getContext()), |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 924 | UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 925 | OldCall); |
| 926 | // If OldCall dues not return void then replaceAllUsesWith undef. |
| 927 | // This allows ValueHandlers and custom metadata to adjust itself. |
| 928 | if (!OldCall->getType()->isVoidTy()) |
Eli Friedman | 3e22cb9 | 2011-05-18 00:32:01 +0000 | [diff] [blame] | 929 | ReplaceInstUsesWith(*OldCall, UndefValue::get(OldCall->getType())); |
Chris Lattner | 830f3f2 | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 930 | if (isa<CallInst>(OldCall)) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 931 | return EraseInstFromFunction(*OldCall); |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 830f3f2 | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 933 | // We cannot remove an invoke, because it would change the CFG, just |
| 934 | // change the callee to a null pointer. |
Gabor Greif | 654c06f | 2010-03-20 21:00:25 +0000 | [diff] [blame] | 935 | cast<InvokeInst>(OldCall)->setCalledFunction( |
Chris Lattner | 830f3f2 | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 936 | Constant::getNullValue(CalleeF->getType())); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 941 | // If CS does not return void then replaceAllUsesWith undef. |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 942 | // This allows ValueHandlers and custom metadata to adjust itself. |
| 943 | if (!CS.getInstruction()->getType()->isVoidTy()) |
Eli Friedman | 3e22cb9 | 2011-05-18 00:32:01 +0000 | [diff] [blame] | 944 | ReplaceInstUsesWith(*CS.getInstruction(), |
| 945 | UndefValue::get(CS.getInstruction()->getType())); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 946 | |
Nuno Lopes | f1fb6c8 | 2012-06-21 23:52:14 +0000 | [diff] [blame] | 947 | if (isa<InvokeInst>(CS.getInstruction())) { |
| 948 | // Can't remove an invoke because we cannot change the CFG. |
| 949 | return 0; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 950 | } |
Nuno Lopes | f1fb6c8 | 2012-06-21 23:52:14 +0000 | [diff] [blame] | 951 | |
| 952 | // This instruction is not reachable, just remove it. We insert a store to |
| 953 | // undef so that we know that this code is not reachable, despite the fact |
| 954 | // that we can't modify the CFG here. |
| 955 | new StoreInst(ConstantInt::getTrue(Callee->getContext()), |
| 956 | UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), |
| 957 | CS.getInstruction()); |
| 958 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 959 | return EraseInstFromFunction(*CS.getInstruction()); |
| 960 | } |
| 961 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 962 | if (IntrinsicInst *II = FindInitTrampoline(Callee)) |
| 963 | return transformCallThroughTrampoline(CS, II); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 964 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 965 | PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 966 | FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 967 | if (FTy->isVarArg()) { |
Eli Friedman | ba78c88 | 2011-11-29 01:18:23 +0000 | [diff] [blame] | 968 | int ix = FTy->getNumParams(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 969 | // See if we can optimize any arguments passed through the varargs area of |
| 970 | // the call. |
| 971 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 972 | E = CS.arg_end(); I != E; ++I, ++ix) { |
| 973 | CastInst *CI = dyn_cast<CastInst>(*I); |
| 974 | if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { |
| 975 | *I = CI->getOperand(0); |
| 976 | Changed = true; |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
| 982 | // Inline asm calls cannot throw - mark them 'nounwind'. |
| 983 | CS.setDoesNotThrow(); |
| 984 | Changed = true; |
| 985 | } |
| 986 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 987 | // Try to optimize the call if possible, we require DataLayout for most of |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 988 | // this. None of these calls are seen as possibly dead so go ahead and |
| 989 | // delete the instruction now. |
| 990 | if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) { |
| 991 | Instruction *I = tryOptimizeCall(CI, TD); |
Eric Christopher | 7b323a3 | 2010-03-06 10:59:25 +0000 | [diff] [blame] | 992 | // If we changed something return the result, etc. Otherwise let |
| 993 | // the fallthrough check. |
| 994 | if (I) return EraseInstFromFunction(*I); |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 997 | return Changed ? CS.getInstruction() : 0; |
| 998 | } |
| 999 | |
| 1000 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 1001 | // attempt to move the cast to the arguments of the call/invoke. |
| 1002 | // |
| 1003 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
Chris Lattner | ab215bc | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 1004 | Function *Callee = |
| 1005 | dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts()); |
| 1006 | if (Callee == 0) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1007 | return false; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1008 | Instruction *Caller = CS.getInstruction(); |
| 1009 | const AttrListPtr &CallerPAL = CS.getAttributes(); |
| 1010 | |
| 1011 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 1012 | // would cause a type conversion of one of our arguments, change this call to |
| 1013 | // be a direct call with arguments casted to the appropriate types. |
| 1014 | // |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1015 | FunctionType *FT = Callee->getFunctionType(); |
| 1016 | Type *OldRetTy = Caller->getType(); |
| 1017 | Type *NewRetTy = FT->getReturnType(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1018 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1019 | if (NewRetTy->isStructTy()) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1020 | return false; // TODO: Handle multiple return values. |
| 1021 | |
| 1022 | // Check to see if we are changing the return type... |
| 1023 | if (OldRetTy != NewRetTy) { |
| 1024 | if (Callee->isDeclaration() && |
| 1025 | // Conversion is ok if changing from one pointer type to another or from |
| 1026 | // a pointer to an integer of the same size. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1027 | !((OldRetTy->isPointerTy() || !TD || |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1028 | OldRetTy == TD->getIntPtrType(Caller->getContext())) && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1029 | (NewRetTy->isPointerTy() || !TD || |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1030 | NewRetTy == TD->getIntPtrType(Caller->getContext())))) |
| 1031 | return false; // Cannot transform this return value. |
| 1032 | |
| 1033 | if (!Caller->use_empty() && |
| 1034 | // void -> non-void is handled specially |
| 1035 | !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy)) |
| 1036 | return false; // Cannot transform this return value. |
| 1037 | |
| 1038 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1039 | Attributes::Builder RAttrs = CallerPAL.getRetAttributes(); |
| 1040 | if (RAttrs.hasAttributes(Attributes::typeIncompatible(NewRetTy))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1041 | return false; // Attribute not compatible with transformed value. |
| 1042 | } |
| 1043 | |
| 1044 | // If the callsite is an invoke instruction, and the return value is used by |
| 1045 | // a PHI node in a successor, we cannot change the return type of the call |
| 1046 | // because there is no place to put the cast instruction (without breaking |
| 1047 | // the critical edge). Bail out in this case. |
| 1048 | if (!Caller->use_empty()) |
| 1049 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 1050 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 1051 | UI != E; ++UI) |
| 1052 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 1053 | if (PN->getParent() == II->getNormalDest() || |
| 1054 | PN->getParent() == II->getUnwindDest()) |
| 1055 | return false; |
| 1056 | } |
| 1057 | |
| 1058 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 1059 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
| 1060 | |
| 1061 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 1062 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1063 | Type *ParamTy = FT->getParamType(i); |
| 1064 | Type *ActTy = (*AI)->getType(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1065 | |
| 1066 | if (!CastInst::isCastable(ActTy, ParamTy)) |
| 1067 | return false; // Cannot transform this parameter value. |
| 1068 | |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 1069 | Attributes Attrs = CallerPAL.getParamAttributes(i + 1); |
Bill Wendling | 853a8c5 | 2012-09-25 20:38:59 +0000 | [diff] [blame] | 1070 | if (Attrs & Attributes::typeIncompatible(ParamTy)) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1071 | return false; // Attribute not compatible with transformed value. |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1072 | |
Chris Lattner | 2b9375e | 2010-12-20 08:36:38 +0000 | [diff] [blame] | 1073 | // If the parameter is passed as a byval argument, then we have to have a |
| 1074 | // sized type and the sized type has to have the same size as the old type. |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame^] | 1075 | if (ParamTy != ActTy && Attrs.hasAttribute(Attributes::ByVal)) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1076 | PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy); |
Chris Lattner | 2b9375e | 2010-12-20 08:36:38 +0000 | [diff] [blame] | 1077 | if (ParamPTy == 0 || !ParamPTy->getElementType()->isSized() || TD == 0) |
| 1078 | return false; |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1080 | Type *CurElTy = cast<PointerType>(ActTy)->getElementType(); |
Chris Lattner | 2b9375e | 2010-12-20 08:36:38 +0000 | [diff] [blame] | 1081 | if (TD->getTypeAllocSize(CurElTy) != |
| 1082 | TD->getTypeAllocSize(ParamPTy->getElementType())) |
| 1083 | return false; |
| 1084 | } |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1085 | |
| 1086 | // Converting from one pointer type to another or between a pointer and an |
| 1087 | // integer of the same size is safe even if we do not have a body. |
| 1088 | bool isConvertible = ActTy == ParamTy || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1089 | (TD && ((ParamTy->isPointerTy() || |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1090 | ParamTy == TD->getIntPtrType(Caller->getContext())) && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1091 | (ActTy->isPointerTy() || |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1092 | ActTy == TD->getIntPtrType(Caller->getContext())))); |
| 1093 | if (Callee->isDeclaration() && !isConvertible) return false; |
| 1094 | } |
| 1095 | |
Chris Lattner | 091b1e3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 1096 | if (Callee->isDeclaration()) { |
| 1097 | // Do not delete arguments unless we have a function body. |
| 1098 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg()) |
| 1099 | return false; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1100 | |
Chris Lattner | 091b1e3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 1101 | // If the callee is just a declaration, don't change the varargsness of the |
| 1102 | // call. We don't want to introduce a varargs call where one doesn't |
| 1103 | // already exist. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1104 | PointerType *APTy = cast<PointerType>(CS.getCalledValue()->getType()); |
Chris Lattner | 091b1e3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 1105 | if (FT->isVarArg()!=cast<FunctionType>(APTy->getElementType())->isVarArg()) |
| 1106 | return false; |
Jim Grosbach | f374486 | 2012-02-03 00:00:55 +0000 | [diff] [blame] | 1107 | |
| 1108 | // If both the callee and the cast type are varargs, we still have to make |
| 1109 | // sure the number of fixed parameters are the same or we have the same |
| 1110 | // ABI issues as if we introduce a varargs call. |
Jim Grosbach | 871a205 | 2012-02-03 00:26:07 +0000 | [diff] [blame] | 1111 | if (FT->isVarArg() && |
| 1112 | cast<FunctionType>(APTy->getElementType())->isVarArg() && |
| 1113 | FT->getNumParams() != |
Jim Grosbach | f374486 | 2012-02-03 00:00:55 +0000 | [diff] [blame] | 1114 | cast<FunctionType>(APTy->getElementType())->getNumParams()) |
| 1115 | return false; |
Chris Lattner | 091b1e3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 1116 | } |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1117 | |
Jim Grosbach | d5917f0 | 2012-02-03 00:00:50 +0000 | [diff] [blame] | 1118 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 1119 | !CallerPAL.isEmpty()) |
| 1120 | // In this case we have more arguments than the new function type, but we |
| 1121 | // won't be dropping them. Check that these extra arguments have attributes |
| 1122 | // that are compatible with being a vararg call argument. |
| 1123 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
| 1124 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) |
| 1125 | break; |
| 1126 | Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1127 | if (PAttrs.hasIncompatibleWithVarArgsAttrs()) |
Jim Grosbach | d5917f0 | 2012-02-03 00:00:50 +0000 | [diff] [blame] | 1128 | return false; |
| 1129 | } |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1130 | |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1131 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1132 | // Okay, we decided that this is a safe thing to do: go ahead and start |
Chris Lattner | 091b1e3 | 2011-02-24 05:10:56 +0000 | [diff] [blame] | 1133 | // inserting cast instructions as necessary. |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1134 | std::vector<Value*> Args; |
| 1135 | Args.reserve(NumActualArgs); |
| 1136 | SmallVector<AttributeWithIndex, 8> attrVec; |
| 1137 | attrVec.reserve(NumCommonArgs); |
| 1138 | |
| 1139 | // Get any return attributes. |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1140 | Attributes::Builder RAttrs = CallerPAL.getRetAttributes(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1141 | |
| 1142 | // If the return value is not being used, the type may not be compatible |
| 1143 | // with the existing attributes. Wipe out any problematic attributes. |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1144 | RAttrs.removeAttributes(Attributes::typeIncompatible(NewRetTy)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1145 | |
| 1146 | // Add the new return attributes. |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1147 | if (RAttrs.hasAttributes()) |
| 1148 | attrVec.push_back(AttributeWithIndex::get(0, Attributes::get(RAttrs))); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1149 | |
| 1150 | AI = CS.arg_begin(); |
| 1151 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1152 | Type *ParamTy = FT->getParamType(i); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1153 | if ((*AI)->getType() == ParamTy) { |
| 1154 | Args.push_back(*AI); |
| 1155 | } else { |
| 1156 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
| 1157 | false, ParamTy, false); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1158 | Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | // Add any parameter attributes. |
| 1162 | if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) |
| 1163 | attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); |
| 1164 | } |
| 1165 | |
| 1166 | // If the function takes more arguments than the call was taking, add them |
| 1167 | // now. |
| 1168 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 1169 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 1170 | |
| 1171 | // If we are removing arguments to the function, emit an obnoxious warning. |
| 1172 | if (FT->getNumParams() < NumActualArgs) { |
| 1173 | if (!FT->isVarArg()) { |
| 1174 | errs() << "WARNING: While resolving call to function '" |
| 1175 | << Callee->getName() << "' arguments were dropped!\n"; |
| 1176 | } else { |
| 1177 | // Add all of the arguments in their promoted form to the arg list. |
| 1178 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1179 | Type *PTy = getPromotedType((*AI)->getType()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1180 | if (PTy != (*AI)->getType()) { |
| 1181 | // Must promote to pass through va_arg area! |
| 1182 | Instruction::CastOps opcode = |
| 1183 | CastInst::getCastOpcode(*AI, false, PTy, false); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1184 | Args.push_back(Builder->CreateCast(opcode, *AI, PTy)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1185 | } else { |
| 1186 | Args.push_back(*AI); |
| 1187 | } |
| 1188 | |
| 1189 | // Add any parameter attributes. |
| 1190 | if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) |
| 1191 | attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | if (Attributes FnAttrs = CallerPAL.getFnAttributes()) |
| 1197 | attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs)); |
| 1198 | |
| 1199 | if (NewRetTy->isVoidTy()) |
| 1200 | Caller->setName(""); // Void type should not have a name. |
| 1201 | |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 1202 | const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1203 | |
| 1204 | Instruction *NC; |
| 1205 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Eli Friedman | ef819d0 | 2011-05-18 01:28:27 +0000 | [diff] [blame] | 1206 | NC = Builder->CreateInvoke(Callee, II->getNormalDest(), |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1207 | II->getUnwindDest(), Args); |
Eli Friedman | ef819d0 | 2011-05-18 01:28:27 +0000 | [diff] [blame] | 1208 | NC->takeName(II); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1209 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
| 1210 | cast<InvokeInst>(NC)->setAttributes(NewCallerPAL); |
| 1211 | } else { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1212 | CallInst *CI = cast<CallInst>(Caller); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1213 | NC = Builder->CreateCall(Callee, Args); |
Eli Friedman | ef819d0 | 2011-05-18 01:28:27 +0000 | [diff] [blame] | 1214 | NC->takeName(CI); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1215 | if (CI->isTailCall()) |
| 1216 | cast<CallInst>(NC)->setTailCall(); |
| 1217 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
| 1218 | cast<CallInst>(NC)->setAttributes(NewCallerPAL); |
| 1219 | } |
| 1220 | |
| 1221 | // Insert a cast of the return type as necessary. |
| 1222 | Value *NV = NC; |
| 1223 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
| 1224 | if (!NV->getType()->isVoidTy()) { |
Chris Lattner | ab215bc | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 1225 | Instruction::CastOps opcode = |
| 1226 | CastInst::getCastOpcode(NC, false, OldRetTy, false); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1227 | NV = NC = CastInst::Create(opcode, NC, OldRetTy); |
Eli Friedman | a311c34 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 1228 | NC->setDebugLoc(Caller->getDebugLoc()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1229 | |
| 1230 | // If this is an invoke instruction, we should insert it after the first |
| 1231 | // non-phi, instruction in the normal successor block. |
| 1232 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
Bill Wendling | 89d4411 | 2011-08-25 01:08:34 +0000 | [diff] [blame] | 1233 | BasicBlock::iterator I = II->getNormalDest()->getFirstInsertionPt(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1234 | InsertNewInstBefore(NC, *I); |
| 1235 | } else { |
Chris Lattner | ab215bc | 2010-12-20 08:25:06 +0000 | [diff] [blame] | 1236 | // Otherwise, it's a call, just insert cast right after the call. |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1237 | InsertNewInstBefore(NC, *Caller); |
| 1238 | } |
| 1239 | Worklist.AddUsersToWorkList(*Caller); |
| 1240 | } else { |
| 1241 | NV = UndefValue::get(Caller->getType()); |
| 1242 | } |
| 1243 | } |
| 1244 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1245 | if (!Caller->use_empty()) |
Eli Friedman | 3e22cb9 | 2011-05-18 00:32:01 +0000 | [diff] [blame] | 1246 | ReplaceInstUsesWith(*Caller, NV); |
| 1247 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1248 | EraseInstFromFunction(*Caller); |
| 1249 | return true; |
| 1250 | } |
| 1251 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 1252 | // transformCallThroughTrampoline - Turn a call to a function created by |
| 1253 | // init_trampoline / adjust_trampoline intrinsic pair into a direct call to the |
| 1254 | // underlying function. |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1255 | // |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 1256 | Instruction * |
| 1257 | InstCombiner::transformCallThroughTrampoline(CallSite CS, |
| 1258 | IntrinsicInst *Tramp) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1259 | Value *Callee = CS.getCalledValue(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1260 | PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 1261 | FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1262 | const AttrListPtr &Attrs = CS.getAttributes(); |
| 1263 | |
| 1264 | // If the call already has the 'nest' attribute somewhere then give up - |
| 1265 | // otherwise 'nest' would occur twice after splicing in the chain. |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1266 | for (unsigned I = 0, E = Attrs.getNumAttrs(); I != E; ++I) |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame^] | 1267 | if (Attrs.getAttributesAtIndex(I).hasAttribute(Attributes::Nest)) |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1268 | return 0; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1269 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 1270 | assert(Tramp && |
| 1271 | "transformCallThroughTrampoline called with incorrect CallSite."); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1272 | |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 1273 | Function *NestF =cast<Function>(Tramp->getArgOperand(1)->stripPointerCasts()); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1274 | PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 1275 | FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1276 | |
| 1277 | const AttrListPtr &NestAttrs = NestF->getAttributes(); |
| 1278 | if (!NestAttrs.isEmpty()) { |
| 1279 | unsigned NestIdx = 1; |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1280 | Type *NestTy = 0; |
Bill Wendling | 8831c06 | 2012-10-09 00:01:21 +0000 | [diff] [blame] | 1281 | Attributes NestAttr; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1282 | |
| 1283 | // Look for a parameter marked with the 'nest' attribute. |
| 1284 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 1285 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame^] | 1286 | if (NestAttrs.getParamAttributes(NestIdx).hasAttribute(Attributes::Nest)){ |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1287 | // Record the parameter type and any other attributes. |
| 1288 | NestTy = *I; |
| 1289 | NestAttr = NestAttrs.getParamAttributes(NestIdx); |
| 1290 | break; |
| 1291 | } |
| 1292 | |
| 1293 | if (NestTy) { |
| 1294 | Instruction *Caller = CS.getInstruction(); |
| 1295 | std::vector<Value*> NewArgs; |
| 1296 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 1297 | |
| 1298 | SmallVector<AttributeWithIndex, 8> NewAttrs; |
| 1299 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
| 1300 | |
| 1301 | // Insert the nest argument into the call argument list, which may |
| 1302 | // mean appending it. Likewise for attributes. |
| 1303 | |
| 1304 | // Add any result attributes. |
| 1305 | if (Attributes Attr = Attrs.getRetAttributes()) |
| 1306 | NewAttrs.push_back(AttributeWithIndex::get(0, Attr)); |
| 1307 | |
| 1308 | { |
| 1309 | unsigned Idx = 1; |
| 1310 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 1311 | do { |
| 1312 | if (Idx == NestIdx) { |
| 1313 | // Add the chain argument and attributes. |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 1314 | Value *NestVal = Tramp->getArgOperand(2); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1315 | if (NestVal->getType() != NestTy) |
Eli Friedman | e6f364b | 2011-05-18 23:58:37 +0000 | [diff] [blame] | 1316 | NestVal = Builder->CreateBitCast(NestVal, NestTy, "nest"); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1317 | NewArgs.push_back(NestVal); |
| 1318 | NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr)); |
| 1319 | } |
| 1320 | |
| 1321 | if (I == E) |
| 1322 | break; |
| 1323 | |
| 1324 | // Add the original argument and attributes. |
| 1325 | NewArgs.push_back(*I); |
| 1326 | if (Attributes Attr = Attrs.getParamAttributes(Idx)) |
| 1327 | NewAttrs.push_back |
| 1328 | (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
| 1329 | |
| 1330 | ++Idx, ++I; |
| 1331 | } while (1); |
| 1332 | } |
| 1333 | |
| 1334 | // Add any function attributes. |
| 1335 | if (Attributes Attr = Attrs.getFnAttributes()) |
| 1336 | NewAttrs.push_back(AttributeWithIndex::get(~0, Attr)); |
| 1337 | |
| 1338 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 1339 | // Handle this by synthesizing a new function type, equal to FTy |
| 1340 | // with the chain parameter inserted. |
| 1341 | |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1342 | std::vector<Type*> NewTypes; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1343 | NewTypes.reserve(FTy->getNumParams()+1); |
| 1344 | |
| 1345 | // Insert the chain's type into the list of parameter types, which may |
| 1346 | // mean appending it. |
| 1347 | { |
| 1348 | unsigned Idx = 1; |
| 1349 | FunctionType::param_iterator I = FTy->param_begin(), |
| 1350 | E = FTy->param_end(); |
| 1351 | |
| 1352 | do { |
| 1353 | if (Idx == NestIdx) |
| 1354 | // Add the chain's type. |
| 1355 | NewTypes.push_back(NestTy); |
| 1356 | |
| 1357 | if (I == E) |
| 1358 | break; |
| 1359 | |
| 1360 | // Add the original type. |
| 1361 | NewTypes.push_back(*I); |
| 1362 | |
| 1363 | ++Idx, ++I; |
| 1364 | } while (1); |
| 1365 | } |
| 1366 | |
| 1367 | // Replace the trampoline call with a direct call. Let the generic |
| 1368 | // code sort out any function type mismatches. |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1369 | FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes, |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1370 | FTy->isVarArg()); |
| 1371 | Constant *NewCallee = |
| 1372 | NestF->getType() == PointerType::getUnqual(NewFTy) ? |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1373 | NestF : ConstantExpr::getBitCast(NestF, |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1374 | PointerType::getUnqual(NewFTy)); |
Chris Lattner | d509d0b | 2012-05-28 01:47:44 +0000 | [diff] [blame] | 1375 | const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1376 | |
| 1377 | Instruction *NewCaller; |
| 1378 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 1379 | NewCaller = InvokeInst::Create(NewCallee, |
| 1380 | II->getNormalDest(), II->getUnwindDest(), |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1381 | NewArgs); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1382 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
| 1383 | cast<InvokeInst>(NewCaller)->setAttributes(NewPAL); |
| 1384 | } else { |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 1385 | NewCaller = CallInst::Create(NewCallee, NewArgs); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1386 | if (cast<CallInst>(Caller)->isTailCall()) |
| 1387 | cast<CallInst>(NewCaller)->setTailCall(); |
| 1388 | cast<CallInst>(NewCaller)-> |
| 1389 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
| 1390 | cast<CallInst>(NewCaller)->setAttributes(NewPAL); |
| 1391 | } |
Eli Friedman | 59f1591 | 2011-05-18 19:57:14 +0000 | [diff] [blame] | 1392 | |
| 1393 | return NewCaller; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 1398 | // parameter, there is no need to adjust the argument list. Let the generic |
| 1399 | // code sort out any function type mismatches. |
| 1400 | Constant *NewCallee = |
Jim Grosbach | 00e403a | 2012-02-03 00:07:04 +0000 | [diff] [blame] | 1401 | NestF->getType() == PTy ? NestF : |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1402 | ConstantExpr::getBitCast(NestF, PTy); |
| 1403 | CS.setCalledFunction(NewCallee); |
| 1404 | return CS.getInstruction(); |
| 1405 | } |