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" |
| 15 | #include "llvm/IntrinsicInst.h" |
| 16 | #include "llvm/Support/CallSite.h" |
| 17 | #include "llvm/Target/TargetData.h" |
| 18 | #include "llvm/Analysis/MemoryBuiltins.h" |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/BuildLibCalls.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. |
| 24 | static const Type *getPromotedType(const Type *Ty) { |
| 25 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 26 | if (ITy->getBitWidth() < 32) |
| 27 | return Type::getInt32Ty(Ty->getContext()); |
| 28 | } |
| 29 | return Ty; |
| 30 | } |
| 31 | |
| 32 | /// EnforceKnownAlignment - If the specified pointer points to an object that |
| 33 | /// we control, modify the object's alignment to PrefAlign. This isn't |
| 34 | /// often possible though. If alignment is important, a more reliable approach |
| 35 | /// is to simply align all global variables and allocation instructions to |
| 36 | /// their preferred alignment from the beginning. |
| 37 | /// |
| 38 | static unsigned EnforceKnownAlignment(Value *V, |
| 39 | unsigned Align, unsigned PrefAlign) { |
| 40 | |
| 41 | User *U = dyn_cast<User>(V); |
| 42 | if (!U) return Align; |
| 43 | |
| 44 | switch (Operator::getOpcode(U)) { |
| 45 | default: break; |
| 46 | case Instruction::BitCast: |
| 47 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
| 48 | case Instruction::GetElementPtr: { |
| 49 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 50 | bool AllZeroOperands = true; |
| 51 | for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i) |
| 52 | if (!isa<Constant>(*i) || |
| 53 | !cast<Constant>(*i)->isNullValue()) { |
| 54 | AllZeroOperands = false; |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | if (AllZeroOperands) { |
| 59 | // Treat this like a bitcast. |
| 60 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
| 61 | } |
Chris Lattner | 2d4b8ee | 2010-04-28 00:31:12 +0000 | [diff] [blame] | 62 | return Align; |
| 63 | } |
| 64 | case Instruction::Alloca: { |
| 65 | AllocaInst *AI = cast<AllocaInst>(V); |
| 66 | // If there is a requested alignment and if this is an alloca, round up. |
| 67 | if (AI->getAlignment() >= PrefAlign) |
| 68 | return AI->getAlignment(); |
| 69 | AI->setAlignment(PrefAlign); |
| 70 | return PrefAlign; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 75 | // If there is a large requested alignment and we can, bump up the alignment |
| 76 | // of the global. |
Chris Lattner | 2d4b8ee | 2010-04-28 00:31:12 +0000 | [diff] [blame] | 77 | if (GV->isDeclaration()) return Align; |
| 78 | |
| 79 | if (GV->getAlignment() >= PrefAlign) |
| 80 | return GV->getAlignment(); |
| 81 | // We can only increase the alignment of the global if it has no alignment |
| 82 | // specified or if it is not assigned a section. If it is assigned a |
| 83 | // section, the global could be densely packed with other objects in the |
| 84 | // section, increasing the alignment could cause padding issues. |
| 85 | if (!GV->hasSection() || GV->getAlignment() == 0) |
| 86 | GV->setAlignment(PrefAlign); |
| 87 | return GV->getAlignment(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return Align; |
| 91 | } |
| 92 | |
| 93 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 94 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 95 | /// and it is more than the alignment of the ultimate object, see if we can |
| 96 | /// increase the alignment of the ultimate object, making this check succeed. |
| 97 | unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, |
| 98 | unsigned PrefAlign) { |
Dan Gohman | 33591af | 2010-07-28 17:14:23 +0000 | [diff] [blame] | 99 | assert(V->getType()->isPointerTy() && |
| 100 | "GetOrEnforceKnownAlignment expects a pointer!"); |
| 101 | unsigned BitWidth = TD ? TD->getPointerSizeInBits() : 64; |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 102 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 103 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 104 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne); |
| 105 | unsigned TrailZ = KnownZero.countTrailingOnes(); |
Dan Gohman | 33591af | 2010-07-28 17:14:23 +0000 | [diff] [blame] | 106 | |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 107 | // Avoid trouble with rediculously large TrailZ values, such as |
| 108 | // those computed from a null pointer. |
Dan Gohman | 33591af | 2010-07-28 17:14:23 +0000 | [diff] [blame] | 109 | TrailZ = std::min(TrailZ, unsigned(sizeof(unsigned) * CHAR_BIT - 1)); |
| 110 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 111 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); |
| 112 | |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 113 | // LLVM doesn't support alignments larger than this currently. |
Dan Gohman | 795e70e | 2010-08-03 16:15:50 +0000 | [diff] [blame] | 114 | Align = std::min(Align, +Value::MaximumAlignment); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 116 | if (PrefAlign > Align) |
| 117 | Align = EnforceKnownAlignment(V, Align, PrefAlign); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 119 | // We don't need to make any adjustment. |
| 120 | return Align; |
| 121 | } |
| 122 | |
| 123 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
Gabor Greif | bcda85c | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 124 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getArgOperand(0)); |
| 125 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getArgOperand(1)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 126 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| 127 | unsigned CopyAlign = MI->getAlignment(); |
| 128 | |
| 129 | if (CopyAlign < MinAlign) { |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 130 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 131 | MinAlign, false)); |
| 132 | return MI; |
| 133 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 135 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 136 | // load/store. |
Gabor Greif | bcda85c | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 137 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getArgOperand(2)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 138 | if (MemOpLength == 0) return 0; |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 140 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 141 | // if the size is something we can handle with a single primitive load/store. |
| 142 | // A single load+store correctly handles overlapping memory in the memmove |
| 143 | // case. |
| 144 | unsigned Size = MemOpLength->getZExtValue(); |
| 145 | if (Size == 0) return MI; // Delete this mem transfer. |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 147 | if (Size > 8 || (Size&(Size-1))) |
| 148 | return 0; // If not 1/2/4/8 bytes, exit. |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 150 | // Use an integer load+store unless we can find something better. |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 151 | unsigned SrcAddrSp = |
Gabor Greif | bcda85c | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 152 | cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace(); |
Gabor Greif | 4ec2258 | 2010-04-16 15:33:14 +0000 | [diff] [blame] | 153 | unsigned DstAddrSp = |
Gabor Greif | bcda85c | 2010-06-24 13:54:33 +0000 | [diff] [blame] | 154 | cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 155 | |
| 156 | const IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3); |
| 157 | Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp); |
| 158 | Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 160 | // Memcpy forces the use of i8* for the source and destination. That means |
| 161 | // that if you're using memcpy to move one double around, you'll get a cast |
| 162 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 163 | // an i64 load+store, here because this improves the odds that the source or |
| 164 | // dest address will be promotable. See if we can find a better type than the |
| 165 | // integer datatype. |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 166 | Value *StrippedDest = MI->getArgOperand(0)->stripPointerCasts(); |
| 167 | if (StrippedDest != MI->getArgOperand(0)) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 168 | const Type *SrcETy = cast<PointerType>(StrippedDest->getType()) |
| 169 | ->getElementType(); |
| 170 | if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
| 171 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 172 | // down through these levels if so. |
| 173 | while (!SrcETy->isSingleValueType()) { |
| 174 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
| 175 | if (STy->getNumElements() == 1) |
| 176 | SrcETy = STy->getElementType(0); |
| 177 | else |
| 178 | break; |
| 179 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { |
| 180 | if (ATy->getNumElements() == 1) |
| 181 | SrcETy = ATy->getElementType(); |
| 182 | else |
| 183 | break; |
| 184 | } else |
| 185 | break; |
| 186 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 187 | |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 188 | if (SrcETy->isSingleValueType()) { |
| 189 | NewSrcPtrTy = PointerType::get(SrcETy, SrcAddrSp); |
| 190 | NewDstPtrTy = PointerType::get(SrcETy, DstAddrSp); |
| 191 | } |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 194 | |
| 195 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 196 | // If the memcpy/memmove provides better alignment info than we can |
| 197 | // infer, use it. |
| 198 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 199 | DstAlign = std::max(DstAlign, CopyAlign); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 200 | |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 201 | Value *Src = Builder->CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy); |
| 202 | Value *Dest = Builder->CreateBitCast(MI->getArgOperand(0), NewDstPtrTy); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 203 | Instruction *L = new LoadInst(Src, "tmp", MI->isVolatile(), SrcAlign); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 204 | InsertNewInstBefore(L, *MI); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 205 | InsertNewInstBefore(new StoreInst(L, Dest, MI->isVolatile(), DstAlign), |
| 206 | *MI); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 207 | |
| 208 | // 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] | 209 | MI->setArgOperand(2, Constant::getNullValue(MemOpLength->getType())); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 210 | return MI; |
| 211 | } |
| 212 | |
| 213 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
| 214 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); |
| 215 | if (MI->getAlignment() < Alignment) { |
| 216 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), |
| 217 | Alignment, false)); |
| 218 | return MI; |
| 219 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 221 | // Extract the length and alignment and fill if they are constant. |
| 222 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); |
| 223 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 224 | if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8)) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | uint64_t Len = LenC->getZExtValue(); |
| 227 | Alignment = MI->getAlignment(); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 229 | // If the length is zero, this is a no-op |
| 230 | if (Len == 0) return MI; // memset(d,c,0,a) -> noop |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 232 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) |
| 233 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { |
| 234 | const Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8. |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 236 | Value *Dest = MI->getDest(); |
| 237 | Dest = Builder->CreateBitCast(Dest, PointerType::getUnqual(ITy)); |
| 238 | |
| 239 | // Alignment 0 is identity for alignment 1 for memset, but not store. |
| 240 | if (Alignment == 0) Alignment = 1; |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 242 | // Extract the fill value and store. |
| 243 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; |
| 244 | InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), |
| 245 | Dest, false, Alignment), *MI); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 247 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| 248 | MI->setLength(Constant::getNullValue(LenC->getType())); |
| 249 | return MI; |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 255 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 256 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 257 | /// the heavy lifting. |
| 258 | /// |
| 259 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
| 260 | if (isFreeCall(&CI)) |
| 261 | return visitFree(CI); |
Duncan Sands | 1d9b973 | 2010-05-27 19:09:06 +0000 | [diff] [blame] | 262 | if (isMalloc(&CI)) |
| 263 | return visitMalloc(CI); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 264 | |
| 265 | // If the caller function is nounwind, mark the call as nounwind, even if the |
| 266 | // callee isn't. |
| 267 | if (CI.getParent()->getParent()->doesNotThrow() && |
| 268 | !CI.doesNotThrow()) { |
| 269 | CI.setDoesNotThrow(); |
| 270 | return &CI; |
| 271 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 273 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 274 | if (!II) return visitCallSite(&CI); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 276 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 277 | // visitCallSite. |
| 278 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
| 279 | bool Changed = false; |
| 280 | |
| 281 | // memmove/cpy/set of zero bytes is a noop. |
| 282 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 283 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 284 | |
| 285 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 286 | if (CI->getZExtValue() == 1) { |
| 287 | // Replace the instruction with just byte operations. We would |
| 288 | // transform other cases to loads/stores, but we don't know if |
| 289 | // alignment is sufficient. |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // If we have a memmove and the source operation is a constant global, |
| 294 | // then the source and dest pointers can't alias, so we can change this |
| 295 | // into a call to memcpy. |
| 296 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
| 297 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 298 | if (GVSrc->isConstant()) { |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 299 | Module *M = CI.getParent()->getParent()->getParent(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 300 | Intrinsic::ID MemCpyID = Intrinsic::memcpy; |
Gabor Greif | c310fcc | 2010-06-24 13:42:49 +0000 | [diff] [blame] | 301 | const Type *Tys[3] = { CI.getArgOperand(0)->getType(), |
| 302 | CI.getArgOperand(1)->getType(), |
| 303 | CI.getArgOperand(2)->getType() }; |
| 304 | CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys, 3)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 305 | Changed = true; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { |
| 310 | // memmove(x,x,size) -> noop. |
| 311 | if (MTI->getSource() == MTI->getDest()) |
| 312 | return EraseInstFromFunction(CI); |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 314 | |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 315 | // If we can determine a pointer alignment that is bigger than currently |
| 316 | // set, update the alignment. |
| 317 | if (isa<MemTransferInst>(MI)) { |
| 318 | if (Instruction *I = SimplifyMemTransfer(MI)) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 319 | return I; |
| 320 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
| 321 | if (Instruction *I = SimplifyMemSet(MSI)) |
| 322 | return I; |
| 323 | } |
Gabor Greif | c310fcc | 2010-06-24 13:42:49 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 325 | if (Changed) return II; |
| 326 | } |
Eric Christopher | 551754c | 2010-04-16 23:37:20 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 328 | switch (II->getIntrinsicID()) { |
| 329 | default: break; |
Eric Christopher | 415326b | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 330 | case Intrinsic::objectsize: { |
Eric Christopher | 26d0e89 | 2010-02-11 01:48:54 +0000 | [diff] [blame] | 331 | // We need target data for just about everything so depend on it. |
Eric Christopher | 415326b | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 332 | if (!TD) break; |
Eric Christopher | 26d0e89 | 2010-02-11 01:48:54 +0000 | [diff] [blame] | 333 | |
Evan Cheng | a862326 | 2010-03-05 20:47:23 +0000 | [diff] [blame] | 334 | const Type *ReturnTy = CI.getType(); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 335 | bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1); |
Evan Cheng | a862326 | 2010-03-05 20:47:23 +0000 | [diff] [blame] | 336 | |
Eric Christopher | 26d0e89 | 2010-02-11 01:48:54 +0000 | [diff] [blame] | 337 | // Get to the real allocated thing and offset as fast as possible. |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 338 | Value *Op1 = II->getArgOperand(0)->stripPointerCasts(); |
Eric Christopher | 415326b | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 339 | |
Eric Christopher | 26d0e89 | 2010-02-11 01:48:54 +0000 | [diff] [blame] | 340 | // If we've stripped down to a single global variable that we |
| 341 | // can know the size of then just return that. |
Eric Christopher | 415326b | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 342 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op1)) { |
| 343 | if (GV->hasDefinitiveInitializer()) { |
| 344 | Constant *C = GV->getInitializer(); |
Evan Cheng | a862326 | 2010-03-05 20:47:23 +0000 | [diff] [blame] | 345 | uint64_t GlobalSize = TD->getTypeAllocSize(C->getType()); |
| 346 | return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, GlobalSize)); |
Eric Christopher | 415326b | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 347 | } else { |
Evan Cheng | a862326 | 2010-03-05 20:47:23 +0000 | [diff] [blame] | 348 | // Can't determine size of the GV. |
Eric Christopher | 415326b | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 349 | Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); |
| 350 | return ReplaceInstUsesWith(CI, RetVal); |
| 351 | } |
Evan Cheng | a862326 | 2010-03-05 20:47:23 +0000 | [diff] [blame] | 352 | } else if (AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) { |
| 353 | // Get alloca size. |
| 354 | if (AI->getAllocatedType()->isSized()) { |
| 355 | uint64_t AllocaSize = TD->getTypeAllocSize(AI->getAllocatedType()); |
| 356 | if (AI->isArrayAllocation()) { |
| 357 | const ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize()); |
| 358 | if (!C) break; |
| 359 | AllocaSize *= C->getZExtValue(); |
| 360 | } |
| 361 | return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, AllocaSize)); |
| 362 | } |
Evan Cheng | 687fed3 | 2010-03-08 22:54:36 +0000 | [diff] [blame] | 363 | } else if (CallInst *MI = extractMallocCall(Op1)) { |
| 364 | const Type* MallocType = getMallocAllocatedType(MI); |
| 365 | // Get alloca size. |
| 366 | if (MallocType && MallocType->isSized()) { |
| 367 | if (Value *NElems = getMallocArraySize(MI, TD, true)) { |
| 368 | if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems)) |
| 369 | return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, |
| 370 | (NElements->getZExtValue() * TD->getTypeAllocSize(MallocType)))); |
| 371 | } |
| 372 | } |
Evan Cheng | a862326 | 2010-03-05 20:47:23 +0000 | [diff] [blame] | 373 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op1)) { |
Eric Christopher | 26d0e89 | 2010-02-11 01:48:54 +0000 | [diff] [blame] | 374 | // Only handle constant GEPs here. |
| 375 | if (CE->getOpcode() != Instruction::GetElementPtr) break; |
| 376 | GEPOperator *GEP = cast<GEPOperator>(CE); |
| 377 | |
Eric Christopher | dfdddd8 | 2010-02-11 17:44:04 +0000 | [diff] [blame] | 378 | // Make sure we're not a constant offset from an external |
| 379 | // global. |
| 380 | Value *Operand = GEP->getPointerOperand(); |
Eric Christopher | 77ffe3b | 2010-02-13 23:38:01 +0000 | [diff] [blame] | 381 | Operand = Operand->stripPointerCasts(); |
Eric Christopher | dfdddd8 | 2010-02-11 17:44:04 +0000 | [diff] [blame] | 382 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) |
| 383 | if (!GV->hasDefinitiveInitializer()) break; |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 384 | |
Eric Christopher | 77ffe3b | 2010-02-13 23:38:01 +0000 | [diff] [blame] | 385 | // Get what we're pointing to and its size. |
| 386 | const PointerType *BaseType = |
Eric Christopher | dfdddd8 | 2010-02-11 17:44:04 +0000 | [diff] [blame] | 387 | cast<PointerType>(Operand->getType()); |
Evan Cheng | 6e5dfd4 | 2010-02-22 23:34:00 +0000 | [diff] [blame] | 388 | uint64_t Size = TD->getTypeAllocSize(BaseType->getElementType()); |
Eric Christopher | 26d0e89 | 2010-02-11 01:48:54 +0000 | [diff] [blame] | 389 | |
Eric Christopher | 77ffe3b | 2010-02-13 23:38:01 +0000 | [diff] [blame] | 390 | // Get the current byte offset into the thing. Use the original |
| 391 | // operand in case we're looking through a bitcast. |
Eric Christopher | 26d0e89 | 2010-02-11 01:48:54 +0000 | [diff] [blame] | 392 | SmallVector<Value*, 8> Ops(CE->op_begin()+1, CE->op_end()); |
Eric Christopher | 77ffe3b | 2010-02-13 23:38:01 +0000 | [diff] [blame] | 393 | const PointerType *OffsetType = |
| 394 | cast<PointerType>(GEP->getPointerOperand()->getType()); |
Evan Cheng | 6e5dfd4 | 2010-02-22 23:34:00 +0000 | [diff] [blame] | 395 | uint64_t Offset = TD->getIndexedOffset(OffsetType, &Ops[0], Ops.size()); |
Eric Christopher | 26d0e89 | 2010-02-11 01:48:54 +0000 | [diff] [blame] | 396 | |
Evan Cheng | 6e5dfd4 | 2010-02-22 23:34:00 +0000 | [diff] [blame] | 397 | if (Size < Offset) { |
| 398 | // Out of bound reference? Negative index normalized to large |
| 399 | // index? Just return "I don't know". |
| 400 | Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); |
| 401 | return ReplaceInstUsesWith(CI, RetVal); |
| 402 | } |
Eric Christopher | 26d0e89 | 2010-02-11 01:48:54 +0000 | [diff] [blame] | 403 | |
| 404 | Constant *RetVal = ConstantInt::get(ReturnTy, Size-Offset); |
| 405 | return ReplaceInstUsesWith(CI, RetVal); |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 406 | } |
Evan Cheng | a862326 | 2010-03-05 20:47:23 +0000 | [diff] [blame] | 407 | |
| 408 | // Do not return "I don't know" here. Later optimization passes could |
| 409 | // make it possible to evaluate objectsize to a constant. |
Evan Cheng | f79d624 | 2010-03-05 01:22:47 +0000 | [diff] [blame] | 410 | break; |
Eric Christopher | 415326b | 2010-02-09 21:24:27 +0000 | [diff] [blame] | 411 | } |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 412 | case Intrinsic::bswap: |
| 413 | // bswap(bswap(x)) -> x |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 414 | if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 415 | if (Operand->getIntrinsicID() == Intrinsic::bswap) |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 416 | return ReplaceInstUsesWith(CI, Operand->getArgOperand(0)); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 418 | // bswap(trunc(bswap(x))) -> trunc(lshr(x, c)) |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 419 | if (TruncInst *TI = dyn_cast<TruncInst>(II->getArgOperand(0))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 420 | if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0))) |
| 421 | if (Operand->getIntrinsicID() == Intrinsic::bswap) { |
| 422 | unsigned C = Operand->getType()->getPrimitiveSizeInBits() - |
| 423 | TI->getType()->getPrimitiveSizeInBits(); |
| 424 | Value *CV = ConstantInt::get(Operand->getType(), C); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 425 | Value *V = Builder->CreateLShr(Operand->getArgOperand(0), CV); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 426 | return new TruncInst(V, TI->getType()); |
| 427 | } |
| 428 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 430 | break; |
| 431 | case Intrinsic::powi: |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 432 | if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 433 | // powi(x, 0) -> 1.0 |
| 434 | if (Power->isZero()) |
| 435 | return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0)); |
| 436 | // powi(x, 1) -> x |
| 437 | if (Power->isOne()) |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 438 | return ReplaceInstUsesWith(CI, II->getArgOperand(0)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 439 | // powi(x, -1) -> 1/x |
| 440 | if (Power->isAllOnesValue()) |
| 441 | return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0), |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 442 | II->getArgOperand(0)); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 443 | } |
| 444 | break; |
| 445 | case Intrinsic::cttz: { |
| 446 | // If all bits below the first known one are known zero, |
| 447 | // this value is constant. |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 448 | const IntegerType *IT = cast<IntegerType>(II->getArgOperand(0)->getType()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 449 | uint32_t BitWidth = IT->getBitWidth(); |
| 450 | APInt KnownZero(BitWidth, 0); |
| 451 | APInt KnownOne(BitWidth, 0); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 452 | ComputeMaskedBits(II->getArgOperand(0), APInt::getAllOnesValue(BitWidth), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 453 | KnownZero, KnownOne); |
| 454 | unsigned TrailingZeros = KnownOne.countTrailingZeros(); |
| 455 | APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros)); |
| 456 | if ((Mask & KnownZero) == Mask) |
| 457 | return ReplaceInstUsesWith(CI, ConstantInt::get(IT, |
| 458 | APInt(BitWidth, TrailingZeros))); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 460 | } |
| 461 | break; |
| 462 | case Intrinsic::ctlz: { |
| 463 | // If all bits above the first known one are known zero, |
| 464 | // this value is constant. |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 465 | const IntegerType *IT = cast<IntegerType>(II->getArgOperand(0)->getType()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 466 | uint32_t BitWidth = IT->getBitWidth(); |
| 467 | APInt KnownZero(BitWidth, 0); |
| 468 | APInt KnownOne(BitWidth, 0); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 469 | ComputeMaskedBits(II->getArgOperand(0), APInt::getAllOnesValue(BitWidth), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 470 | KnownZero, KnownOne); |
| 471 | unsigned LeadingZeros = KnownOne.countLeadingZeros(); |
| 472 | APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros)); |
| 473 | if ((Mask & KnownZero) == Mask) |
| 474 | return ReplaceInstUsesWith(CI, ConstantInt::get(IT, |
| 475 | APInt(BitWidth, LeadingZeros))); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 476 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 477 | } |
| 478 | break; |
| 479 | case Intrinsic::uadd_with_overflow: { |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 480 | Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1); |
| 481 | const IntegerType *IT = cast<IntegerType>(II->getArgOperand(0)->getType()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 482 | uint32_t BitWidth = IT->getBitWidth(); |
| 483 | APInt Mask = APInt::getSignBit(BitWidth); |
| 484 | APInt LHSKnownZero(BitWidth, 0); |
| 485 | APInt LHSKnownOne(BitWidth, 0); |
| 486 | ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne); |
| 487 | bool LHSKnownNegative = LHSKnownOne[BitWidth - 1]; |
| 488 | bool LHSKnownPositive = LHSKnownZero[BitWidth - 1]; |
| 489 | |
| 490 | if (LHSKnownNegative || LHSKnownPositive) { |
| 491 | APInt RHSKnownZero(BitWidth, 0); |
| 492 | APInt RHSKnownOne(BitWidth, 0); |
| 493 | ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne); |
| 494 | bool RHSKnownNegative = RHSKnownOne[BitWidth - 1]; |
| 495 | bool RHSKnownPositive = RHSKnownZero[BitWidth - 1]; |
| 496 | if (LHSKnownNegative && RHSKnownNegative) { |
| 497 | // The sign bit is set in both cases: this MUST overflow. |
| 498 | // Create a simple add instruction, and insert it into the struct. |
| 499 | Instruction *Add = BinaryOperator::CreateAdd(LHS, RHS, "", &CI); |
| 500 | Worklist.Add(Add); |
| 501 | Constant *V[] = { |
| 502 | UndefValue::get(LHS->getType()),ConstantInt::getTrue(II->getContext()) |
| 503 | }; |
| 504 | Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); |
| 505 | return InsertValueInst::Create(Struct, Add, 0); |
| 506 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 508 | if (LHSKnownPositive && RHSKnownPositive) { |
| 509 | // The sign bit is clear in both cases: this CANNOT overflow. |
| 510 | // Create a simple add instruction, and insert it into the struct. |
| 511 | Instruction *Add = BinaryOperator::CreateNUWAdd(LHS, RHS, "", &CI); |
| 512 | Worklist.Add(Add); |
| 513 | Constant *V[] = { |
| 514 | UndefValue::get(LHS->getType()), |
| 515 | ConstantInt::getFalse(II->getContext()) |
| 516 | }; |
| 517 | Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); |
| 518 | return InsertValueInst::Create(Struct, Add, 0); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | // FALL THROUGH uadd into sadd |
| 523 | case Intrinsic::sadd_with_overflow: |
| 524 | // Canonicalize constants into the RHS. |
Gabor Greif | a90c5c7 | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 525 | if (isa<Constant>(II->getArgOperand(0)) && |
| 526 | !isa<Constant>(II->getArgOperand(1))) { |
| 527 | Value *LHS = II->getArgOperand(0); |
| 528 | II->setArgOperand(0, II->getArgOperand(1)); |
| 529 | II->setArgOperand(1, LHS); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 530 | return II; |
| 531 | } |
| 532 | |
| 533 | // X + undef -> undef |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 534 | if (isa<UndefValue>(II->getArgOperand(1))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 535 | return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 536 | |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 537 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 538 | // X + 0 -> {X, false} |
| 539 | if (RHS->isZero()) { |
| 540 | Constant *V[] = { |
Eli Friedman | 4fffb34 | 2010-08-09 20:49:43 +0000 | [diff] [blame] | 541 | UndefValue::get(II->getArgOperand(0)->getType()), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 542 | ConstantInt::getFalse(II->getContext()) |
| 543 | }; |
| 544 | Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 545 | return InsertValueInst::Create(Struct, II->getArgOperand(0), 0); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | break; |
| 549 | case Intrinsic::usub_with_overflow: |
| 550 | case Intrinsic::ssub_with_overflow: |
| 551 | // undef - X -> undef |
| 552 | // X - undef -> undef |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 553 | if (isa<UndefValue>(II->getArgOperand(0)) || |
| 554 | isa<UndefValue>(II->getArgOperand(1))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 555 | return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 556 | |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 557 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 558 | // X - 0 -> {X, false} |
| 559 | if (RHS->isZero()) { |
| 560 | Constant *V[] = { |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 561 | UndefValue::get(II->getArgOperand(0)->getType()), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 562 | ConstantInt::getFalse(II->getContext()) |
| 563 | }; |
| 564 | Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 565 | return InsertValueInst::Create(Struct, II->getArgOperand(0), 0); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | break; |
| 569 | case Intrinsic::umul_with_overflow: |
| 570 | case Intrinsic::smul_with_overflow: |
| 571 | // Canonicalize constants into the RHS. |
Gabor Greif | a90c5c7 | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 572 | if (isa<Constant>(II->getArgOperand(0)) && |
| 573 | !isa<Constant>(II->getArgOperand(1))) { |
| 574 | Value *LHS = II->getArgOperand(0); |
| 575 | II->setArgOperand(0, II->getArgOperand(1)); |
| 576 | II->setArgOperand(1, LHS); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 577 | return II; |
| 578 | } |
| 579 | |
| 580 | // X * undef -> undef |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 581 | if (isa<UndefValue>(II->getArgOperand(1))) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 582 | return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 583 | |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 584 | if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getArgOperand(1))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 585 | // X*0 -> {0, false} |
| 586 | if (RHSI->isZero()) |
| 587 | return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType())); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 588 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 589 | // X * 1 -> {X, false} |
| 590 | if (RHSI->equalsInt(1)) { |
| 591 | Constant *V[] = { |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 592 | UndefValue::get(II->getArgOperand(0)->getType()), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 593 | ConstantInt::getFalse(II->getContext()) |
| 594 | }; |
| 595 | Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 596 | return InsertValueInst::Create(Struct, II->getArgOperand(0), 0); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | break; |
| 600 | case Intrinsic::ppc_altivec_lvx: |
| 601 | case Intrinsic::ppc_altivec_lvxl: |
| 602 | case Intrinsic::x86_sse_loadu_ps: |
| 603 | case Intrinsic::x86_sse2_loadu_pd: |
| 604 | case Intrinsic::x86_sse2_loadu_dq: |
| 605 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 606 | // Turn X86 loadups -> load if the pointer is known aligned. |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 607 | if (GetOrEnforceKnownAlignment(II->getArgOperand(0), 16) >= 16) { |
| 608 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 609 | PointerType::getUnqual(II->getType())); |
| 610 | return new LoadInst(Ptr); |
| 611 | } |
| 612 | break; |
| 613 | case Intrinsic::ppc_altivec_stvx: |
| 614 | case Intrinsic::ppc_altivec_stvxl: |
| 615 | // Turn stvx -> store if the pointer is known aligned. |
Gabor Greif | 2f1ab74 | 2010-06-24 15:51:11 +0000 | [diff] [blame] | 616 | if (GetOrEnforceKnownAlignment(II->getArgOperand(1), 16) >= 16) { |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 617 | const Type *OpPtrTy = |
Gabor Greif | 2f1ab74 | 2010-06-24 15:51:11 +0000 | [diff] [blame] | 618 | PointerType::getUnqual(II->getArgOperand(0)->getType()); |
| 619 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy); |
| 620 | return new StoreInst(II->getArgOperand(0), Ptr); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 621 | } |
| 622 | break; |
| 623 | case Intrinsic::x86_sse_storeu_ps: |
| 624 | case Intrinsic::x86_sse2_storeu_pd: |
| 625 | case Intrinsic::x86_sse2_storeu_dq: |
| 626 | // Turn X86 storeu -> store if the pointer is known aligned. |
Gabor Greif | 2f1ab74 | 2010-06-24 15:51:11 +0000 | [diff] [blame] | 627 | if (GetOrEnforceKnownAlignment(II->getArgOperand(0), 16) >= 16) { |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 628 | const Type *OpPtrTy = |
Gabor Greif | 2f1ab74 | 2010-06-24 15:51:11 +0000 | [diff] [blame] | 629 | PointerType::getUnqual(II->getArgOperand(1)->getType()); |
| 630 | Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), OpPtrTy); |
| 631 | return new StoreInst(II->getArgOperand(1), Ptr); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 632 | } |
| 633 | break; |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 634 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 635 | case Intrinsic::x86_sse_cvttss2si: { |
| 636 | // These intrinsics only demands the 0th element of its input vector. If |
| 637 | // we can simplify the input based on that, do so now. |
| 638 | unsigned VWidth = |
Gabor Greif | 9c68a7b | 2010-06-25 07:57:14 +0000 | [diff] [blame] | 639 | cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 640 | APInt DemandedElts(VWidth, 1); |
| 641 | APInt UndefElts(VWidth, 0); |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 642 | if (Value *V = SimplifyDemandedVectorElts(II->getArgOperand(0), |
| 643 | DemandedElts, UndefElts)) { |
Gabor Greif | a90c5c7 | 2010-06-28 16:50:57 +0000 | [diff] [blame] | 644 | II->setArgOperand(0, V); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 645 | return II; |
| 646 | } |
| 647 | break; |
| 648 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 650 | case Intrinsic::ppc_altivec_vperm: |
| 651 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 652 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getArgOperand(2))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 653 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 655 | // Check that all of the elements are integer constants or undefs. |
| 656 | bool AllEltsOk = true; |
| 657 | for (unsigned i = 0; i != 16; ++i) { |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 658 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 659 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 660 | AllEltsOk = false; |
| 661 | break; |
| 662 | } |
| 663 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 664 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 665 | if (AllEltsOk) { |
| 666 | // Cast the input vectors to byte vectors. |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 667 | Value *Op0 = Builder->CreateBitCast(II->getArgOperand(0), |
| 668 | Mask->getType()); |
| 669 | Value *Op1 = Builder->CreateBitCast(II->getArgOperand(1), |
| 670 | Mask->getType()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 671 | Value *Result = UndefValue::get(Op0->getType()); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 673 | // Only extract each element once. |
| 674 | Value *ExtractedElts[32]; |
| 675 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 677 | for (unsigned i = 0; i != 16; ++i) { |
| 678 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 679 | continue; |
| 680 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
| 681 | Idx &= 31; // Match the hardware behavior. |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 683 | if (ExtractedElts[Idx] == 0) { |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 684 | ExtractedElts[Idx] = |
| 685 | Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1, |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 686 | ConstantInt::get(Type::getInt32Ty(II->getContext()), |
| 687 | Idx&15, false), "tmp"); |
| 688 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 689 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 690 | // Insert this value into the result vector. |
| 691 | Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx], |
| 692 | ConstantInt::get(Type::getInt32Ty(II->getContext()), |
| 693 | i, false), "tmp"); |
| 694 | } |
| 695 | return CastInst::Create(Instruction::BitCast, Result, CI.getType()); |
| 696 | } |
| 697 | } |
| 698 | break; |
| 699 | |
| 700 | case Intrinsic::stackrestore: { |
| 701 | // If the save is right next to the restore, remove the restore. This can |
| 702 | // happen when variable allocas are DCE'd. |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 703 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 704 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 705 | BasicBlock::iterator BI = SS; |
| 706 | if (&*++BI == II) |
| 707 | return EraseInstFromFunction(CI); |
| 708 | } |
| 709 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 711 | // Scan down this block to see if there is another stack restore in the |
| 712 | // same block without an intervening call/alloca. |
| 713 | BasicBlock::iterator BI = II; |
| 714 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 715 | bool CannotRemove = false; |
| 716 | for (++BI; &*BI != TI; ++BI) { |
| 717 | if (isa<AllocaInst>(BI) || isMalloc(BI)) { |
| 718 | CannotRemove = true; |
| 719 | break; |
| 720 | } |
| 721 | if (CallInst *BCI = dyn_cast<CallInst>(BI)) { |
| 722 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { |
| 723 | // If there is a stackrestore below this one, remove this one. |
| 724 | if (II->getIntrinsicID() == Intrinsic::stackrestore) |
| 725 | return EraseInstFromFunction(CI); |
| 726 | // Otherwise, ignore the intrinsic. |
| 727 | } else { |
| 728 | // If we found a non-intrinsic call, we can't remove the stack |
| 729 | // restore. |
| 730 | CannotRemove = true; |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 736 | // If the stack restore is in a return/unwind block and if there are no |
| 737 | // allocas or calls between the restore and the return, nuke the restore. |
| 738 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) |
| 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 | |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +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, |
| 757 | const TargetData * const TD, |
| 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. |
| 765 | if (!CS.paramHasAttr(ix, Attribute::ByVal)) |
| 766 | return true; |
| 767 | |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 768 | const Type* SrcTy = |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 769 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); |
| 770 | const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); |
| 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 { |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 786 | if (ConstantInt *SizeCI = |
| 787 | dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) { |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 788 | if (SizeCI->isAllOnesValue()) |
| 789 | return true; |
| 790 | if (isString) |
| 791 | return SizeCI->getZExtValue() >= |
Gabor Greif | a6aac4c | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 792 | GetStringLength(CI->getArgOperand(SizeArgOp)); |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 793 | if (ConstantInt *Arg = dyn_cast<ConstantInt>( |
| 794 | CI->getArgOperand(SizeArgOp))) |
Evan Cheng | 9d8f002 | 2010-03-23 06:06:09 +0000 | [diff] [blame] | 795 | return SizeCI->getZExtValue() >= Arg->getZExtValue(); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 796 | } |
| 797 | return false; |
| 798 | } |
| 799 | public: |
| 800 | InstCombineFortifiedLibCalls(InstCombiner *IC) : IC(IC), NewInstruction(0) { } |
| 801 | Instruction *NewInstruction; |
| 802 | }; |
| 803 | } // end anonymous namespace |
| 804 | |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 805 | // Try to fold some different type of calls here. |
| 806 | // Currently we're only working with the checking functions, memcpy_chk, |
| 807 | // mempcpy_chk, memmove_chk, memset_chk, strcpy_chk, stpcpy_chk, strncpy_chk, |
| 808 | // strcat_chk and strncat_chk. |
| 809 | Instruction *InstCombiner::tryOptimizeCall(CallInst *CI, const TargetData *TD) { |
| 810 | if (CI->getCalledFunction() == 0) return 0; |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 811 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 812 | InstCombineFortifiedLibCalls Simplifier(this); |
| 813 | Simplifier.fold(CI, TD); |
| 814 | return Simplifier.NewInstruction; |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 817 | // visitCallSite - Improvements for call and invoke instructions. |
| 818 | // |
| 819 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
| 820 | bool Changed = false; |
| 821 | |
| 822 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 823 | // to the arguments of the call/invoke. |
| 824 | if (transformConstExprCastCall(CS)) return 0; |
| 825 | |
| 826 | Value *Callee = CS.getCalledValue(); |
| 827 | |
| 828 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
Chris Lattner | d569561 | 2010-02-01 18:11:34 +0000 | [diff] [blame] | 829 | // If the call and callee calling conventions don't match, this call must |
| 830 | // be unreachable, as the call is undefined. |
| 831 | if (CalleeF->getCallingConv() != CS.getCallingConv() && |
| 832 | // Only do this for calls to a function with a body. A prototype may |
| 833 | // not actually end up matching the implementation's calling conv for a |
| 834 | // variety of reasons (e.g. it may be written in assembly). |
| 835 | !CalleeF->isDeclaration()) { |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 836 | Instruction *OldCall = CS.getInstruction(); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 837 | new StoreInst(ConstantInt::getTrue(Callee->getContext()), |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 838 | UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 839 | OldCall); |
| 840 | // If OldCall dues not return void then replaceAllUsesWith undef. |
| 841 | // This allows ValueHandlers and custom metadata to adjust itself. |
| 842 | if (!OldCall->getType()->isVoidTy()) |
| 843 | OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); |
Chris Lattner | 830f3f2 | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 844 | if (isa<CallInst>(OldCall)) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 845 | return EraseInstFromFunction(*OldCall); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 846 | |
Chris Lattner | 830f3f2 | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 847 | // We cannot remove an invoke, because it would change the CFG, just |
| 848 | // change the callee to a null pointer. |
Gabor Greif | 654c06f | 2010-03-20 21:00:25 +0000 | [diff] [blame] | 849 | cast<InvokeInst>(OldCall)->setCalledFunction( |
Chris Lattner | 830f3f2 | 2010-02-01 18:04:58 +0000 | [diff] [blame] | 850 | Constant::getNullValue(CalleeF->getType())); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 855 | // This instruction is not reachable, just remove it. We insert a store to |
| 856 | // undef so that we know that this code is not reachable, despite the fact |
| 857 | // that we can't modify the CFG here. |
| 858 | new StoreInst(ConstantInt::getTrue(Callee->getContext()), |
| 859 | UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), |
| 860 | CS.getInstruction()); |
| 861 | |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 862 | // If CS does not return void then replaceAllUsesWith undef. |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 863 | // This allows ValueHandlers and custom metadata to adjust itself. |
| 864 | if (!CS.getInstruction()->getType()->isVoidTy()) |
| 865 | CS.getInstruction()-> |
| 866 | replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); |
| 867 | |
| 868 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 869 | // Don't break the CFG, insert a dummy cond branch. |
| 870 | BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), |
| 871 | ConstantInt::getTrue(Callee->getContext()), II); |
| 872 | } |
| 873 | return EraseInstFromFunction(*CS.getInstruction()); |
| 874 | } |
| 875 | |
| 876 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
| 877 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) |
| 878 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) |
| 879 | return transformCallThroughTrampoline(CS); |
| 880 | |
| 881 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 882 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 883 | if (FTy->isVarArg()) { |
| 884 | int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); |
| 885 | // See if we can optimize any arguments passed through the varargs area of |
| 886 | // the call. |
| 887 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| 888 | E = CS.arg_end(); I != E; ++I, ++ix) { |
| 889 | CastInst *CI = dyn_cast<CastInst>(*I); |
| 890 | if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { |
| 891 | *I = CI->getOperand(0); |
| 892 | Changed = true; |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
| 898 | // Inline asm calls cannot throw - mark them 'nounwind'. |
| 899 | CS.setDoesNotThrow(); |
| 900 | Changed = true; |
| 901 | } |
| 902 | |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 903 | // Try to optimize the call if possible, we require TargetData for most of |
| 904 | // this. None of these calls are seen as possibly dead so go ahead and |
| 905 | // delete the instruction now. |
| 906 | if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) { |
| 907 | Instruction *I = tryOptimizeCall(CI, TD); |
Eric Christopher | 7b323a3 | 2010-03-06 10:59:25 +0000 | [diff] [blame] | 908 | // If we changed something return the result, etc. Otherwise let |
| 909 | // the fallthrough check. |
| 910 | if (I) return EraseInstFromFunction(*I); |
Eric Christopher | 27ceaa1 | 2010-03-06 10:50:38 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 913 | return Changed ? CS.getInstruction() : 0; |
| 914 | } |
| 915 | |
| 916 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 917 | // attempt to move the cast to the arguments of the call/invoke. |
| 918 | // |
| 919 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 920 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 921 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 922 | if (CE->getOpcode() != Instruction::BitCast || |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 923 | !isa<Function>(CE->getOperand(0))) |
| 924 | return false; |
| 925 | Function *Callee = cast<Function>(CE->getOperand(0)); |
| 926 | Instruction *Caller = CS.getInstruction(); |
| 927 | const AttrListPtr &CallerPAL = CS.getAttributes(); |
| 928 | |
| 929 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 930 | // would cause a type conversion of one of our arguments, change this call to |
| 931 | // be a direct call with arguments casted to the appropriate types. |
| 932 | // |
| 933 | const FunctionType *FT = Callee->getFunctionType(); |
| 934 | const Type *OldRetTy = Caller->getType(); |
| 935 | const Type *NewRetTy = FT->getReturnType(); |
| 936 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 937 | if (NewRetTy->isStructTy()) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 938 | return false; // TODO: Handle multiple return values. |
| 939 | |
| 940 | // Check to see if we are changing the return type... |
| 941 | if (OldRetTy != NewRetTy) { |
| 942 | if (Callee->isDeclaration() && |
| 943 | // Conversion is ok if changing from one pointer type to another or from |
| 944 | // a pointer to an integer of the same size. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 945 | !((OldRetTy->isPointerTy() || !TD || |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 946 | OldRetTy == TD->getIntPtrType(Caller->getContext())) && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 947 | (NewRetTy->isPointerTy() || !TD || |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 948 | NewRetTy == TD->getIntPtrType(Caller->getContext())))) |
| 949 | return false; // Cannot transform this return value. |
| 950 | |
| 951 | if (!Caller->use_empty() && |
| 952 | // void -> non-void is handled specially |
| 953 | !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy)) |
| 954 | return false; // Cannot transform this return value. |
| 955 | |
| 956 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
| 957 | Attributes RAttrs = CallerPAL.getRetAttributes(); |
| 958 | if (RAttrs & Attribute::typeIncompatible(NewRetTy)) |
| 959 | return false; // Attribute not compatible with transformed value. |
| 960 | } |
| 961 | |
| 962 | // If the callsite is an invoke instruction, and the return value is used by |
| 963 | // a PHI node in a successor, we cannot change the return type of the call |
| 964 | // because there is no place to put the cast instruction (without breaking |
| 965 | // the critical edge). Bail out in this case. |
| 966 | if (!Caller->use_empty()) |
| 967 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 968 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 969 | UI != E; ++UI) |
| 970 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 971 | if (PN->getParent() == II->getNormalDest() || |
| 972 | PN->getParent() == II->getUnwindDest()) |
| 973 | return false; |
| 974 | } |
| 975 | |
| 976 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 977 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
| 978 | |
| 979 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 980 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 981 | const Type *ParamTy = FT->getParamType(i); |
| 982 | const Type *ActTy = (*AI)->getType(); |
| 983 | |
| 984 | if (!CastInst::isCastable(ActTy, ParamTy)) |
| 985 | return false; // Cannot transform this parameter value. |
| 986 | |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 987 | if (CallerPAL.getParamAttributes(i + 1) |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 988 | & Attribute::typeIncompatible(ParamTy)) |
| 989 | return false; // Attribute not compatible with transformed value. |
| 990 | |
| 991 | // Converting from one pointer type to another or between a pointer and an |
| 992 | // integer of the same size is safe even if we do not have a body. |
| 993 | bool isConvertible = ActTy == ParamTy || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 994 | (TD && ((ParamTy->isPointerTy() || |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 995 | ParamTy == TD->getIntPtrType(Caller->getContext())) && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 996 | (ActTy->isPointerTy() || |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 997 | ActTy == TD->getIntPtrType(Caller->getContext())))); |
| 998 | if (Callee->isDeclaration() && !isConvertible) return false; |
| 999 | } |
| 1000 | |
| 1001 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 1002 | Callee->isDeclaration()) |
| 1003 | return false; // Do not delete arguments unless we have a function body. |
| 1004 | |
| 1005 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 1006 | !CallerPAL.isEmpty()) |
| 1007 | // In this case we have more arguments than the new function type, but we |
| 1008 | // won't be dropping them. Check that these extra arguments have attributes |
| 1009 | // that are compatible with being a vararg call argument. |
| 1010 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
| 1011 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) |
| 1012 | break; |
| 1013 | Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
| 1014 | if (PAttrs & Attribute::VarArgsIncompatible) |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
| 1018 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 1019 | // inserting cast instructions as necessary... |
| 1020 | std::vector<Value*> Args; |
| 1021 | Args.reserve(NumActualArgs); |
| 1022 | SmallVector<AttributeWithIndex, 8> attrVec; |
| 1023 | attrVec.reserve(NumCommonArgs); |
| 1024 | |
| 1025 | // Get any return attributes. |
| 1026 | Attributes RAttrs = CallerPAL.getRetAttributes(); |
| 1027 | |
| 1028 | // If the return value is not being used, the type may not be compatible |
| 1029 | // with the existing attributes. Wipe out any problematic attributes. |
| 1030 | RAttrs &= ~Attribute::typeIncompatible(NewRetTy); |
| 1031 | |
| 1032 | // Add the new return attributes. |
| 1033 | if (RAttrs) |
| 1034 | attrVec.push_back(AttributeWithIndex::get(0, RAttrs)); |
| 1035 | |
| 1036 | AI = CS.arg_begin(); |
| 1037 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 1038 | const Type *ParamTy = FT->getParamType(i); |
| 1039 | if ((*AI)->getType() == ParamTy) { |
| 1040 | Args.push_back(*AI); |
| 1041 | } else { |
| 1042 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
| 1043 | false, ParamTy, false); |
| 1044 | Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy, "tmp")); |
| 1045 | } |
| 1046 | |
| 1047 | // Add any parameter attributes. |
| 1048 | if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) |
| 1049 | attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); |
| 1050 | } |
| 1051 | |
| 1052 | // If the function takes more arguments than the call was taking, add them |
| 1053 | // now. |
| 1054 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| 1055 | Args.push_back(Constant::getNullValue(FT->getParamType(i))); |
| 1056 | |
| 1057 | // If we are removing arguments to the function, emit an obnoxious warning. |
| 1058 | if (FT->getNumParams() < NumActualArgs) { |
| 1059 | if (!FT->isVarArg()) { |
| 1060 | errs() << "WARNING: While resolving call to function '" |
| 1061 | << Callee->getName() << "' arguments were dropped!\n"; |
| 1062 | } else { |
| 1063 | // Add all of the arguments in their promoted form to the arg list. |
| 1064 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 1065 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 1066 | if (PTy != (*AI)->getType()) { |
| 1067 | // Must promote to pass through va_arg area! |
| 1068 | Instruction::CastOps opcode = |
| 1069 | CastInst::getCastOpcode(*AI, false, PTy, false); |
| 1070 | Args.push_back(Builder->CreateCast(opcode, *AI, PTy, "tmp")); |
| 1071 | } else { |
| 1072 | Args.push_back(*AI); |
| 1073 | } |
| 1074 | |
| 1075 | // Add any parameter attributes. |
| 1076 | if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) |
| 1077 | attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | if (Attributes FnAttrs = CallerPAL.getFnAttributes()) |
| 1083 | attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs)); |
| 1084 | |
| 1085 | if (NewRetTy->isVoidTy()) |
| 1086 | Caller->setName(""); // Void type should not have a name. |
| 1087 | |
| 1088 | const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(), |
| 1089 | attrVec.end()); |
| 1090 | |
| 1091 | Instruction *NC; |
| 1092 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 1093 | NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), |
| 1094 | Args.begin(), Args.end(), |
| 1095 | Caller->getName(), Caller); |
| 1096 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
| 1097 | cast<InvokeInst>(NC)->setAttributes(NewCallerPAL); |
| 1098 | } else { |
| 1099 | NC = CallInst::Create(Callee, Args.begin(), Args.end(), |
| 1100 | Caller->getName(), Caller); |
| 1101 | CallInst *CI = cast<CallInst>(Caller); |
| 1102 | if (CI->isTailCall()) |
| 1103 | cast<CallInst>(NC)->setTailCall(); |
| 1104 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
| 1105 | cast<CallInst>(NC)->setAttributes(NewCallerPAL); |
| 1106 | } |
| 1107 | |
| 1108 | // Insert a cast of the return type as necessary. |
| 1109 | Value *NV = NC; |
| 1110 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
| 1111 | if (!NV->getType()->isVoidTy()) { |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 1112 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1113 | OldRetTy, false); |
| 1114 | NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp"); |
| 1115 | |
| 1116 | // If this is an invoke instruction, we should insert it after the first |
| 1117 | // non-phi, instruction in the normal successor block. |
| 1118 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 1119 | BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI(); |
| 1120 | InsertNewInstBefore(NC, *I); |
| 1121 | } else { |
| 1122 | // Otherwise, it's a call, just insert cast right after the call instr |
| 1123 | InsertNewInstBefore(NC, *Caller); |
| 1124 | } |
| 1125 | Worklist.AddUsersToWorkList(*Caller); |
| 1126 | } else { |
| 1127 | NV = UndefValue::get(Caller->getType()); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | if (!Caller->use_empty()) |
| 1133 | Caller->replaceAllUsesWith(NV); |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 1134 | |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1135 | EraseInstFromFunction(*Caller); |
| 1136 | return true; |
| 1137 | } |
| 1138 | |
| 1139 | // transformCallThroughTrampoline - Turn a call to a function created by the |
| 1140 | // init_trampoline intrinsic into a direct call to the underlying function. |
| 1141 | // |
| 1142 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { |
| 1143 | Value *Callee = CS.getCalledValue(); |
| 1144 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 1145 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1146 | const AttrListPtr &Attrs = CS.getAttributes(); |
| 1147 | |
| 1148 | // If the call already has the 'nest' attribute somewhere then give up - |
| 1149 | // otherwise 'nest' would occur twice after splicing in the chain. |
| 1150 | if (Attrs.hasAttrSomewhere(Attribute::Nest)) |
| 1151 | return 0; |
| 1152 | |
| 1153 | IntrinsicInst *Tramp = |
| 1154 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); |
| 1155 | |
Gabor Greif | a399781 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 1156 | Function *NestF =cast<Function>(Tramp->getArgOperand(1)->stripPointerCasts()); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1157 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 1158 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
| 1159 | |
| 1160 | const AttrListPtr &NestAttrs = NestF->getAttributes(); |
| 1161 | if (!NestAttrs.isEmpty()) { |
| 1162 | unsigned NestIdx = 1; |
| 1163 | const Type *NestTy = 0; |
| 1164 | Attributes NestAttr = Attribute::None; |
| 1165 | |
| 1166 | // Look for a parameter marked with the 'nest' attribute. |
| 1167 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 1168 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
| 1169 | if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) { |
| 1170 | // Record the parameter type and any other attributes. |
| 1171 | NestTy = *I; |
| 1172 | NestAttr = NestAttrs.getParamAttributes(NestIdx); |
| 1173 | break; |
| 1174 | } |
| 1175 | |
| 1176 | if (NestTy) { |
| 1177 | Instruction *Caller = CS.getInstruction(); |
| 1178 | std::vector<Value*> NewArgs; |
| 1179 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 1180 | |
| 1181 | SmallVector<AttributeWithIndex, 8> NewAttrs; |
| 1182 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
| 1183 | |
| 1184 | // Insert the nest argument into the call argument list, which may |
| 1185 | // mean appending it. Likewise for attributes. |
| 1186 | |
| 1187 | // Add any result attributes. |
| 1188 | if (Attributes Attr = Attrs.getRetAttributes()) |
| 1189 | NewAttrs.push_back(AttributeWithIndex::get(0, Attr)); |
| 1190 | |
| 1191 | { |
| 1192 | unsigned Idx = 1; |
| 1193 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 1194 | do { |
| 1195 | if (Idx == NestIdx) { |
| 1196 | // Add the chain argument and attributes. |
Gabor Greif | cea7ac7 | 2010-06-24 12:58:35 +0000 | [diff] [blame] | 1197 | Value *NestVal = Tramp->getArgOperand(2); |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1198 | if (NestVal->getType() != NestTy) |
| 1199 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); |
| 1200 | NewArgs.push_back(NestVal); |
| 1201 | NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr)); |
| 1202 | } |
| 1203 | |
| 1204 | if (I == E) |
| 1205 | break; |
| 1206 | |
| 1207 | // Add the original argument and attributes. |
| 1208 | NewArgs.push_back(*I); |
| 1209 | if (Attributes Attr = Attrs.getParamAttributes(Idx)) |
| 1210 | NewAttrs.push_back |
| 1211 | (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
| 1212 | |
| 1213 | ++Idx, ++I; |
| 1214 | } while (1); |
| 1215 | } |
| 1216 | |
| 1217 | // Add any function attributes. |
| 1218 | if (Attributes Attr = Attrs.getFnAttributes()) |
| 1219 | NewAttrs.push_back(AttributeWithIndex::get(~0, Attr)); |
| 1220 | |
| 1221 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 1222 | // Handle this by synthesizing a new function type, equal to FTy |
| 1223 | // with the chain parameter inserted. |
| 1224 | |
| 1225 | std::vector<const Type*> NewTypes; |
| 1226 | NewTypes.reserve(FTy->getNumParams()+1); |
| 1227 | |
| 1228 | // Insert the chain's type into the list of parameter types, which may |
| 1229 | // mean appending it. |
| 1230 | { |
| 1231 | unsigned Idx = 1; |
| 1232 | FunctionType::param_iterator I = FTy->param_begin(), |
| 1233 | E = FTy->param_end(); |
| 1234 | |
| 1235 | do { |
| 1236 | if (Idx == NestIdx) |
| 1237 | // Add the chain's type. |
| 1238 | NewTypes.push_back(NestTy); |
| 1239 | |
| 1240 | if (I == E) |
| 1241 | break; |
| 1242 | |
| 1243 | // Add the original type. |
| 1244 | NewTypes.push_back(*I); |
| 1245 | |
| 1246 | ++Idx, ++I; |
| 1247 | } while (1); |
| 1248 | } |
| 1249 | |
| 1250 | // Replace the trampoline call with a direct call. Let the generic |
| 1251 | // code sort out any function type mismatches. |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 1252 | FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes, |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1253 | FTy->isVarArg()); |
| 1254 | Constant *NewCallee = |
| 1255 | NestF->getType() == PointerType::getUnqual(NewFTy) ? |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 1256 | NestF : ConstantExpr::getBitCast(NestF, |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1257 | PointerType::getUnqual(NewFTy)); |
| 1258 | const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(), |
| 1259 | NewAttrs.end()); |
| 1260 | |
| 1261 | Instruction *NewCaller; |
| 1262 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| 1263 | NewCaller = InvokeInst::Create(NewCallee, |
| 1264 | II->getNormalDest(), II->getUnwindDest(), |
| 1265 | NewArgs.begin(), NewArgs.end(), |
| 1266 | Caller->getName(), Caller); |
| 1267 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
| 1268 | cast<InvokeInst>(NewCaller)->setAttributes(NewPAL); |
| 1269 | } else { |
| 1270 | NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), |
| 1271 | Caller->getName(), Caller); |
| 1272 | if (cast<CallInst>(Caller)->isTailCall()) |
| 1273 | cast<CallInst>(NewCaller)->setTailCall(); |
| 1274 | cast<CallInst>(NewCaller)-> |
| 1275 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
| 1276 | cast<CallInst>(NewCaller)->setAttributes(NewPAL); |
| 1277 | } |
| 1278 | if (!Caller->getType()->isVoidTy()) |
| 1279 | Caller->replaceAllUsesWith(NewCaller); |
| 1280 | Caller->eraseFromParent(); |
| 1281 | Worklist.Remove(Caller); |
| 1282 | return 0; |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 1287 | // parameter, there is no need to adjust the argument list. Let the generic |
| 1288 | // code sort out any function type mismatches. |
| 1289 | Constant *NewCallee = |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 1290 | NestF->getType() == PTy ? NestF : |
Chris Lattner | 753a2b4 | 2010-01-05 07:32:13 +0000 | [diff] [blame] | 1291 | ConstantExpr::getBitCast(NestF, PTy); |
| 1292 | CS.setCalledFunction(NewCallee); |
| 1293 | return CS.getInstruction(); |
| 1294 | } |
Eric Christopher | 0c6a8f9 | 2010-02-03 00:21:58 +0000 | [diff] [blame] | 1295 | |