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