Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1 | //===- InstCombineLoadStoreAlloca.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 visit functions for load, store and alloca. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "InstCombine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/Loads.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DataLayout.h" |
| 18 | #include "llvm/IR/IntrinsicInst.h" |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 20 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 23 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 24 | STATISTIC(NumGlobalCopies, "Number of allocas copied from constant global"); |
| 25 | |
| 26 | /// pointsToConstantGlobal - Return true if V (possibly indirectly) points to |
| 27 | /// some part of a constant global variable. This intentionally only accepts |
| 28 | /// constant expressions because we can't rewrite arbitrary instructions. |
| 29 | static bool pointsToConstantGlobal(Value *V) { |
| 30 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 31 | return GV->isConstant(); |
| 32 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 33 | if (CE->getOpcode() == Instruction::BitCast || |
| 34 | CE->getOpcode() == Instruction::GetElementPtr) |
| 35 | return pointsToConstantGlobal(CE->getOperand(0)); |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) |
| 40 | /// pointer to an alloca. Ignore any reads of the pointer, return false if we |
| 41 | /// see any stores or other unknown uses. If we see pointer arithmetic, keep |
| 42 | /// track of whether it moves the pointer (with IsOffset) but otherwise traverse |
| 43 | /// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to |
| 44 | /// the alloca, and if the source pointer is a pointer to a constant global, we |
| 45 | /// can optimize this. |
| 46 | static bool |
| 47 | isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy, |
| 48 | SmallVectorImpl<Instruction *> &ToDelete, |
| 49 | bool IsOffset = false) { |
| 50 | // We track lifetime intrinsics as we encounter them. If we decide to go |
| 51 | // ahead and replace the value with the global, this lets the caller quickly |
| 52 | // eliminate the markers. |
| 53 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 54 | for (Use &U : V->uses()) { |
| 55 | Instruction *I = cast<Instruction>(U.getUser()); |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 56 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 57 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 58 | // Ignore non-volatile loads, they are always ok. |
| 59 | if (!LI->isSimple()) return false; |
| 60 | continue; |
| 61 | } |
| 62 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 63 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(I)) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 64 | // If uses of the bitcast are ok, we are ok. |
| 65 | if (!isOnlyCopiedFromConstantGlobal(BCI, TheCopy, ToDelete, IsOffset)) |
| 66 | return false; |
| 67 | continue; |
| 68 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 69 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 70 | // If the GEP has all zero indices, it doesn't offset the pointer. If it |
| 71 | // doesn't, it does. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 72 | if (!isOnlyCopiedFromConstantGlobal( |
| 73 | GEP, TheCopy, ToDelete, IsOffset || !GEP->hasAllZeroIndices())) |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 74 | return false; |
| 75 | continue; |
| 76 | } |
| 77 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 78 | if (CallSite CS = I) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 79 | // If this is the function being called then we treat it like a load and |
| 80 | // ignore it. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 81 | if (CS.isCallee(&U)) |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 82 | continue; |
| 83 | |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 84 | // Inalloca arguments are clobbered by the call. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 85 | unsigned ArgNo = CS.getArgumentNo(&U); |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 86 | if (CS.isInAllocaArgument(ArgNo)) |
| 87 | return false; |
| 88 | |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 89 | // If this is a readonly/readnone call site, then we know it is just a |
| 90 | // load (but one that potentially returns the value itself), so we can |
| 91 | // ignore it if we know that the value isn't captured. |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 92 | if (CS.onlyReadsMemory() && |
| 93 | (CS.getInstruction()->use_empty() || CS.doesNotCapture(ArgNo))) |
| 94 | continue; |
| 95 | |
| 96 | // If this is being passed as a byval argument, the caller is making a |
| 97 | // copy, so it is only a read of the alloca. |
| 98 | if (CS.isByValArgument(ArgNo)) |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | // Lifetime intrinsics can be handled by the caller. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 103 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 104 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 105 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 106 | assert(II->use_empty() && "Lifetime markers have no result to use!"); |
| 107 | ToDelete.push_back(II); |
| 108 | continue; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // If this is isn't our memcpy/memmove, reject it as something we can't |
| 113 | // handle. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 114 | MemTransferInst *MI = dyn_cast<MemTransferInst>(I); |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 115 | if (MI == 0) |
| 116 | return false; |
| 117 | |
| 118 | // If the transfer is using the alloca as a source of the transfer, then |
| 119 | // ignore it since it is a load (unless the transfer is volatile). |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 120 | if (U.getOperandNo() == 1) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 121 | if (MI->isVolatile()) return false; |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | // If we already have seen a copy, reject the second one. |
| 126 | if (TheCopy) return false; |
| 127 | |
| 128 | // If the pointer has been offset from the start of the alloca, we can't |
| 129 | // safely handle this. |
| 130 | if (IsOffset) return false; |
| 131 | |
| 132 | // If the memintrinsic isn't using the alloca as the dest, reject it. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 133 | if (U.getOperandNo() != 0) return false; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 134 | |
| 135 | // If the source of the memcpy/move is not a constant global, reject it. |
| 136 | if (!pointsToConstantGlobal(MI->getSource())) |
| 137 | return false; |
| 138 | |
| 139 | // Otherwise, the transform is safe. Remember the copy instruction. |
| 140 | TheCopy = MI; |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only |
| 146 | /// modified by a copy from a constant global. If we can prove this, we can |
| 147 | /// replace any uses of the alloca with uses of the global directly. |
| 148 | static MemTransferInst * |
| 149 | isOnlyCopiedFromConstantGlobal(AllocaInst *AI, |
| 150 | SmallVectorImpl<Instruction *> &ToDelete) { |
| 151 | MemTransferInst *TheCopy = 0; |
| 152 | if (isOnlyCopiedFromConstantGlobal(AI, TheCopy, ToDelete)) |
| 153 | return TheCopy; |
| 154 | return 0; |
| 155 | } |
| 156 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 157 | Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) { |
Dan Gohman | df5d7dc | 2010-05-28 15:09:00 +0000 | [diff] [blame] | 158 | // Ensure that the alloca array size argument has type intptr_t, so that |
| 159 | // any casting is exposed early. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 160 | if (DL) { |
| 161 | Type *IntPtrTy = DL->getIntPtrType(AI.getType()); |
Dan Gohman | df5d7dc | 2010-05-28 15:09:00 +0000 | [diff] [blame] | 162 | if (AI.getArraySize()->getType() != IntPtrTy) { |
| 163 | Value *V = Builder->CreateIntCast(AI.getArraySize(), |
| 164 | IntPtrTy, false); |
| 165 | AI.setOperand(0, V); |
| 166 | return &AI; |
| 167 | } |
| 168 | } |
| 169 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 170 | // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1 |
| 171 | if (AI.isArrayAllocation()) { // Check C != 1 |
| 172 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 173 | Type *NewTy = |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 174 | ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 175 | AllocaInst *New = Builder->CreateAlloca(NewTy, 0, AI.getName()); |
| 176 | New->setAlignment(AI.getAlignment()); |
| 177 | |
| 178 | // Scan to the end of the allocation instructions, to skip over a block of |
| 179 | // allocas if possible...also skip interleaved debug info |
| 180 | // |
| 181 | BasicBlock::iterator It = New; |
| 182 | while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It; |
| 183 | |
| 184 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 185 | // insert our getelementptr instruction... |
| 186 | // |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 187 | Type *IdxTy = DL |
| 188 | ? DL->getIntPtrType(AI.getType()) |
Matt Arsenault | 9e3a6ca | 2013-08-14 00:24:38 +0000 | [diff] [blame] | 189 | : Type::getInt64Ty(AI.getContext()); |
| 190 | Value *NullIdx = Constant::getNullValue(IdxTy); |
Matt Arsenault | 640ff9d | 2013-08-14 00:24:05 +0000 | [diff] [blame] | 191 | Value *Idx[2] = { NullIdx, NullIdx }; |
Eli Friedman | 41e509a | 2011-05-18 23:58:37 +0000 | [diff] [blame] | 192 | Instruction *GEP = |
Matt Arsenault | 640ff9d | 2013-08-14 00:24:05 +0000 | [diff] [blame] | 193 | GetElementPtrInst::CreateInBounds(New, Idx, New->getName() + ".sub"); |
Eli Friedman | 41e509a | 2011-05-18 23:58:37 +0000 | [diff] [blame] | 194 | InsertNewInstBefore(GEP, *It); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 195 | |
| 196 | // Now make everything use the getelementptr instead of the original |
| 197 | // allocation. |
Eli Friedman | 41e509a | 2011-05-18 23:58:37 +0000 | [diff] [blame] | 198 | return ReplaceInstUsesWith(AI, GEP); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 199 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| 200 | return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
| 201 | } |
| 202 | } |
| 203 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 204 | if (DL && AI.getAllocatedType()->isSized()) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 205 | // If the alignment is 0 (unspecified), assign it the preferred alignment. |
| 206 | if (AI.getAlignment() == 0) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 207 | AI.setAlignment(DL->getPrefTypeAlignment(AI.getAllocatedType())); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 208 | |
| 209 | // Move all alloca's of zero byte objects to the entry block and merge them |
| 210 | // together. Note that we only do this for alloca's, because malloc should |
| 211 | // allocate and return a unique pointer, even for a zero byte allocation. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 212 | if (DL->getTypeAllocSize(AI.getAllocatedType()) == 0) { |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 213 | // For a zero sized alloca there is no point in doing an array allocation. |
| 214 | // This is helpful if the array size is a complicated expression not used |
| 215 | // elsewhere. |
| 216 | if (AI.isArrayAllocation()) { |
| 217 | AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1)); |
| 218 | return &AI; |
| 219 | } |
| 220 | |
| 221 | // Get the first instruction in the entry block. |
| 222 | BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock(); |
| 223 | Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg(); |
| 224 | if (FirstInst != &AI) { |
| 225 | // If the entry block doesn't start with a zero-size alloca then move |
| 226 | // this one to the start of the entry block. There is no problem with |
| 227 | // dominance as the array size was forced to a constant earlier already. |
| 228 | AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst); |
| 229 | if (!EntryAI || !EntryAI->getAllocatedType()->isSized() || |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 230 | DL->getTypeAllocSize(EntryAI->getAllocatedType()) != 0) { |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 231 | AI.moveBefore(FirstInst); |
| 232 | return &AI; |
| 233 | } |
| 234 | |
Richard Osborne | b68053e | 2012-09-18 09:31:44 +0000 | [diff] [blame] | 235 | // If the alignment of the entry block alloca is 0 (unspecified), |
| 236 | // assign it the preferred alignment. |
| 237 | if (EntryAI->getAlignment() == 0) |
| 238 | EntryAI->setAlignment( |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 239 | DL->getPrefTypeAlignment(EntryAI->getAllocatedType())); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 240 | // Replace this zero-sized alloca with the one at the start of the entry |
| 241 | // block after ensuring that the address will be aligned enough for both |
| 242 | // types. |
Richard Osborne | b68053e | 2012-09-18 09:31:44 +0000 | [diff] [blame] | 243 | unsigned MaxAlign = std::max(EntryAI->getAlignment(), |
| 244 | AI.getAlignment()); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 245 | EntryAI->setAlignment(MaxAlign); |
| 246 | if (AI.getType() != EntryAI->getType()) |
| 247 | return new BitCastInst(EntryAI, AI.getType()); |
| 248 | return ReplaceInstUsesWith(AI, EntryAI); |
| 249 | } |
| 250 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Eli Friedman | b14873c | 2012-11-26 23:04:53 +0000 | [diff] [blame] | 253 | if (AI.getAlignment()) { |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 254 | // Check to see if this allocation is only modified by a memcpy/memmove from |
| 255 | // a constant global whose alignment is equal to or exceeds that of the |
| 256 | // allocation. If this is the case, we can change all users to use |
| 257 | // the constant global instead. This is commonly produced by the CFE by |
| 258 | // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A' |
| 259 | // is only subsequently read. |
| 260 | SmallVector<Instruction *, 4> ToDelete; |
| 261 | if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) { |
Eli Friedman | b14873c | 2012-11-26 23:04:53 +0000 | [diff] [blame] | 262 | unsigned SourceAlign = getOrEnforceKnownAlignment(Copy->getSource(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 263 | AI.getAlignment(), DL); |
Eli Friedman | b14873c | 2012-11-26 23:04:53 +0000 | [diff] [blame] | 264 | if (AI.getAlignment() <= SourceAlign) { |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 265 | DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n'); |
| 266 | DEBUG(dbgs() << " memcpy = " << *Copy << '\n'); |
| 267 | for (unsigned i = 0, e = ToDelete.size(); i != e; ++i) |
| 268 | EraseInstFromFunction(*ToDelete[i]); |
| 269 | Constant *TheSrc = cast<Constant>(Copy->getSource()); |
Matt Arsenault | bbf18c6 | 2013-12-07 02:58:45 +0000 | [diff] [blame] | 270 | Constant *Cast |
| 271 | = ConstantExpr::getPointerBitCastOrAddrSpaceCast(TheSrc, AI.getType()); |
| 272 | Instruction *NewI = ReplaceInstUsesWith(AI, Cast); |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 273 | EraseInstFromFunction(*Copy); |
| 274 | ++NumGlobalCopies; |
| 275 | return NewI; |
| 276 | } |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 280 | // At last, use the generic allocation site handler to aggressively remove |
| 281 | // unused allocas. |
| 282 | return visitAllocSite(AI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
| 286 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
| 287 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 288 | const DataLayout *DL) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 289 | User *CI = cast<User>(LI.getOperand(0)); |
| 290 | Value *CastOp = CI->getOperand(0); |
| 291 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 292 | PointerType *DestTy = cast<PointerType>(CI->getType()); |
| 293 | Type *DestPTy = DestTy->getElementType(); |
| 294 | if (PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 295 | |
| 296 | // If the address spaces don't match, don't eliminate the cast. |
| 297 | if (DestTy->getAddressSpace() != SrcTy->getAddressSpace()) |
| 298 | return 0; |
| 299 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 300 | Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 301 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 302 | if (DestPTy->isIntegerTy() || DestPTy->isPointerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 303 | DestPTy->isVectorTy()) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 304 | // If the source is an array, the code below will not succeed. Check to |
| 305 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 306 | // constants. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 307 | if (ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 308 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 309 | if (ASrcTy->getNumElements() != 0) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 310 | Type *IdxTy = DL |
| 311 | ? DL->getIntPtrType(SrcTy) |
Matt Arsenault | 3dfe54e | 2013-09-03 21:05:48 +0000 | [diff] [blame] | 312 | : Type::getInt64Ty(SrcTy->getContext()); |
Matt Arsenault | 9e3a6ca | 2013-08-14 00:24:38 +0000 | [diff] [blame] | 313 | Value *Idx = Constant::getNullValue(IdxTy); |
| 314 | Value *Idxs[2] = { Idx, Idx }; |
Jay Foad | 71f19ac | 2011-07-22 07:54:01 +0000 | [diff] [blame] | 315 | CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 316 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 317 | SrcPTy = SrcTy->getElementType(); |
| 318 | } |
| 319 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 320 | if (IC.getDataLayout() && |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 321 | (SrcPTy->isIntegerTy() || SrcPTy->isPointerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 322 | SrcPTy->isVectorTy()) && |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 323 | // Do not allow turning this into a load of an integer, which is then |
| 324 | // casted to a pointer, this pessimizes pointer analysis a lot. |
Benjamin Kramer | 0b37cdf | 2013-09-19 20:59:04 +0000 | [diff] [blame] | 325 | (SrcPTy->isPtrOrPtrVectorTy() == |
| 326 | LI.getType()->isPtrOrPtrVectorTy()) && |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 327 | IC.getDataLayout()->getTypeSizeInBits(SrcPTy) == |
| 328 | IC.getDataLayout()->getTypeSizeInBits(DestPTy)) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 329 | |
| 330 | // Okay, we are casting from one integer or pointer type to another of |
| 331 | // the same size. Instead of casting the pointer before the load, cast |
| 332 | // the result of the loaded value. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 333 | LoadInst *NewLoad = |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 334 | IC.Builder->CreateLoad(CastOp, LI.isVolatile(), CI->getName()); |
Bob Wilson | 4b71b6c | 2010-01-30 00:41:10 +0000 | [diff] [blame] | 335 | NewLoad->setAlignment(LI.getAlignment()); |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 336 | NewLoad->setAtomic(LI.getOrdering(), LI.getSynchScope()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 337 | // Now cast the result of the load. |
Owen Anderson | 9b8f9c3 | 2014-03-13 22:51:43 +0000 | [diff] [blame] | 338 | PointerType *OldTy = dyn_cast<PointerType>(NewLoad->getType()); |
| 339 | PointerType *NewTy = dyn_cast<PointerType>(LI.getType()); |
| 340 | if (OldTy && NewTy && |
| 341 | OldTy->getAddressSpace() != NewTy->getAddressSpace()) { |
| 342 | return new AddrSpaceCastInst(NewLoad, LI.getType()); |
| 343 | } |
| 344 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 345 | return new BitCastInst(NewLoad, LI.getType()); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 353 | Value *Op = LI.getOperand(0); |
| 354 | |
| 355 | // Attempt to improve the alignment. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 356 | if (DL) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 357 | unsigned KnownAlign = |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 358 | getOrEnforceKnownAlignment(Op, DL->getPrefTypeAlignment(LI.getType()),DL); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 359 | unsigned LoadAlign = LI.getAlignment(); |
| 360 | unsigned EffectiveLoadAlign = LoadAlign != 0 ? LoadAlign : |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 361 | DL->getABITypeAlignment(LI.getType()); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 362 | |
| 363 | if (KnownAlign > EffectiveLoadAlign) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 364 | LI.setAlignment(KnownAlign); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 365 | else if (LoadAlign == 0) |
| 366 | LI.setAlignment(EffectiveLoadAlign); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 369 | // load (cast X) --> cast (load X) iff safe. |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 370 | if (isa<CastInst>(Op)) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 371 | if (Instruction *Res = InstCombineLoadCast(*this, LI, DL)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 372 | return Res; |
| 373 | |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 374 | // None of the following transforms are legal for volatile/atomic loads. |
| 375 | // FIXME: Some of it is okay for atomic loads; needs refactoring. |
| 376 | if (!LI.isSimple()) return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 377 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 378 | // Do really simple store-to-load forwarding and load CSE, to catch cases |
Duncan Sands | 75b5d27 | 2011-02-15 09:23:02 +0000 | [diff] [blame] | 379 | // where there are several consecutive memory accesses to the same location, |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 380 | // separated by a few arithmetic operations. |
| 381 | BasicBlock::iterator BBI = &LI; |
| 382 | if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6)) |
| 383 | return ReplaceInstUsesWith(LI, AvailableVal); |
| 384 | |
| 385 | // load(gep null, ...) -> unreachable |
| 386 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 387 | const Value *GEPI0 = GEPI->getOperand(0); |
| 388 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 389 | if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){ |
| 390 | // Insert a new store to null instruction before the load to indicate |
| 391 | // that this code is not reachable. We do this instead of inserting |
| 392 | // an unreachable instruction directly because we cannot modify the |
| 393 | // CFG. |
| 394 | new StoreInst(UndefValue::get(LI.getType()), |
| 395 | Constant::getNullValue(Op->getType()), &LI); |
| 396 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 397 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 398 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 399 | |
| 400 | // load null/undef -> unreachable |
| 401 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 402 | if (isa<UndefValue>(Op) || |
| 403 | (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) { |
| 404 | // Insert a new store to null instruction before the load to indicate that |
| 405 | // this code is not reachable. We do this instead of inserting an |
| 406 | // unreachable instruction directly because we cannot modify the CFG. |
| 407 | new StoreInst(UndefValue::get(LI.getType()), |
| 408 | Constant::getNullValue(Op->getType()), &LI); |
| 409 | return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
| 410 | } |
| 411 | |
| 412 | // Instcombine load (constantexpr_cast global) -> cast (load global) |
| 413 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) |
| 414 | if (CE->isCast()) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 415 | if (Instruction *Res = InstCombineLoadCast(*this, LI, DL)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 416 | return Res; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 417 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 418 | if (Op->hasOneUse()) { |
| 419 | // Change select and PHI nodes to select values instead of addresses: this |
| 420 | // helps alias analysis out a lot, allows many others simplifications, and |
| 421 | // exposes redundancy in the code. |
| 422 | // |
| 423 | // Note that we cannot do the transformation unless we know that the |
| 424 | // introduced loads cannot trap! Something like this is valid as long as |
| 425 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 426 | // but it would not be valid if we transformed it to load from null |
| 427 | // unconditionally. |
| 428 | // |
| 429 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 430 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 431 | unsigned Align = LI.getAlignment(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 432 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI, Align, DL) && |
| 433 | isSafeToLoadUnconditionally(SI->getOperand(2), SI, Align, DL)) { |
Bob Wilson | 4b71b6c | 2010-01-30 00:41:10 +0000 | [diff] [blame] | 434 | LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1), |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 435 | SI->getOperand(1)->getName()+".val"); |
Bob Wilson | 4b71b6c | 2010-01-30 00:41:10 +0000 | [diff] [blame] | 436 | LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2), |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 437 | SI->getOperand(2)->getName()+".val"); |
| 438 | V1->setAlignment(Align); |
| 439 | V2->setAlignment(Align); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 440 | return SelectInst::Create(SI->getCondition(), V1, V2); |
| 441 | } |
| 442 | |
| 443 | // load (select (cond, null, P)) -> load P |
| 444 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 445 | if (C->isNullValue()) { |
| 446 | LI.setOperand(0, SI->getOperand(2)); |
| 447 | return &LI; |
| 448 | } |
| 449 | |
| 450 | // load (select (cond, P, null)) -> load P |
| 451 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 452 | if (C->isNullValue()) { |
| 453 | LI.setOperand(0, SI->getOperand(1)); |
| 454 | return &LI; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
| 462 | /// when possible. This makes it generally easy to do alias analysis and/or |
| 463 | /// SROA/mem2reg of the memory object. |
| 464 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 465 | User *CI = cast<User>(SI.getOperand(1)); |
| 466 | Value *CastOp = CI->getOperand(0); |
| 467 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 468 | Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| 469 | PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 470 | if (SrcTy == 0) return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 471 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 472 | Type *SrcPTy = SrcTy->getElementType(); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 473 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 474 | if (!DestPTy->isIntegerTy() && !DestPTy->isPointerTy()) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 475 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 476 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 477 | /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep" |
| 478 | /// to its first element. This allows us to handle things like: |
| 479 | /// store i32 xxx, (bitcast {foo*, float}* %P to i32*) |
| 480 | /// on 32-bit hosts. |
| 481 | SmallVector<Value*, 4> NewGEPIndices; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 482 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 483 | // If the source is an array, the code below will not succeed. Check to |
| 484 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 485 | // constants. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 486 | if (SrcPTy->isArrayTy() || SrcPTy->isStructTy()) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 487 | // Index through pointer. |
| 488 | Constant *Zero = Constant::getNullValue(Type::getInt32Ty(SI.getContext())); |
| 489 | NewGEPIndices.push_back(Zero); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 490 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 491 | while (1) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 492 | if (StructType *STy = dyn_cast<StructType>(SrcPTy)) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 493 | if (!STy->getNumElements()) /* Struct can be empty {} */ |
| 494 | break; |
| 495 | NewGEPIndices.push_back(Zero); |
| 496 | SrcPTy = STy->getElementType(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 497 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 498 | NewGEPIndices.push_back(Zero); |
| 499 | SrcPTy = ATy->getElementType(); |
| 500 | } else { |
| 501 | break; |
| 502 | } |
| 503 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 504 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 505 | SrcTy = PointerType::get(SrcPTy, SrcTy->getAddressSpace()); |
| 506 | } |
| 507 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 508 | if (!SrcPTy->isIntegerTy() && !SrcPTy->isPointerTy()) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 509 | return 0; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 510 | |
Richard Osborne | 0af4aa9 | 2014-03-25 17:21:41 +0000 | [diff] [blame^] | 511 | // If the pointers point into different address spaces don't do the |
| 512 | // transformation. |
| 513 | if (SrcTy->getAddressSpace() != |
| 514 | cast<PointerType>(CI->getType())->getAddressSpace()) |
| 515 | return 0; |
| 516 | |
| 517 | // If the pointers point to values of different sizes don't do the |
| 518 | // transformation. |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 519 | if (!IC.getDataLayout() || |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 520 | IC.getDataLayout()->getTypeSizeInBits(SrcPTy) != |
| 521 | IC.getDataLayout()->getTypeSizeInBits(DestPTy)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 522 | return 0; |
| 523 | |
Richard Osborne | 0af4aa9 | 2014-03-25 17:21:41 +0000 | [diff] [blame^] | 524 | // If the pointers point to pointers to different address spaces don't do the |
| 525 | // transformation. It is not safe to introduce an addrspacecast instruction in |
| 526 | // this case since, depending on the target, addrspacecast may not be a no-op |
| 527 | // cast. |
| 528 | if (SrcPTy->isPointerTy() && DestPTy->isPointerTy() && |
| 529 | SrcPTy->getPointerAddressSpace() != DestPTy->getPointerAddressSpace()) |
| 530 | return 0; |
| 531 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 532 | // Okay, we are casting from one integer or pointer type to another of |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 533 | // the same size. Instead of casting the pointer before |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 534 | // the store, cast the value to be stored. |
| 535 | Value *NewCast; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 536 | Instruction::CastOps opcode = Instruction::BitCast; |
Richard Osborne | 9805ec4 | 2014-03-25 17:21:35 +0000 | [diff] [blame] | 537 | Type* CastSrcTy = DestPTy; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 538 | Type* CastDstTy = SrcPTy; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 539 | if (CastDstTy->isPointerTy()) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 540 | if (CastSrcTy->isIntegerTy()) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 541 | opcode = Instruction::IntToPtr; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 542 | } else if (CastDstTy->isIntegerTy()) { |
Richard Osborne | 9805ec4 | 2014-03-25 17:21:35 +0000 | [diff] [blame] | 543 | if (CastSrcTy->isPointerTy()) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 544 | opcode = Instruction::PtrToInt; |
| 545 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 546 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 547 | // SIOp0 is a pointer to aggregate and this is a store to the first field, |
| 548 | // emit a GEP to index into its first field. |
| 549 | if (!NewGEPIndices.empty()) |
Jay Foad | 040dd82 | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 550 | CastOp = IC.Builder->CreateInBoundsGEP(CastOp, NewGEPIndices); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 551 | |
Richard Osborne | 9805ec4 | 2014-03-25 17:21:35 +0000 | [diff] [blame] | 552 | Value *SIOp0 = SI.getOperand(0); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 553 | NewCast = IC.Builder->CreateCast(opcode, SIOp0, CastDstTy, |
| 554 | SIOp0->getName()+".c"); |
Dan Gohman | 2e20dfb | 2010-10-25 16:16:27 +0000 | [diff] [blame] | 555 | SI.setOperand(0, NewCast); |
| 556 | SI.setOperand(1, CastOp); |
| 557 | return &SI; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /// equivalentAddressValues - Test if A and B will obviously have the same |
| 561 | /// value. This includes recognizing that %t0 and %t1 will have the same |
| 562 | /// value in code like this: |
| 563 | /// %t0 = getelementptr \@a, 0, 3 |
| 564 | /// store i32 0, i32* %t0 |
| 565 | /// %t1 = getelementptr \@a, 0, 3 |
| 566 | /// %t2 = load i32* %t1 |
| 567 | /// |
| 568 | static bool equivalentAddressValues(Value *A, Value *B) { |
| 569 | // Test if the values are trivially equivalent. |
| 570 | if (A == B) return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 571 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 572 | // Test if the values come form identical arithmetic instructions. |
| 573 | // This uses isIdenticalToWhenDefined instead of isIdenticalTo because |
| 574 | // its only used to compare two uses within the same basic block, which |
| 575 | // means that they'll always either have the same value or one of them |
| 576 | // will have an undefined value. |
| 577 | if (isa<BinaryOperator>(A) || |
| 578 | isa<CastInst>(A) || |
| 579 | isa<PHINode>(A) || |
| 580 | isa<GetElementPtrInst>(A)) |
| 581 | if (Instruction *BI = dyn_cast<Instruction>(B)) |
| 582 | if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) |
| 583 | return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 584 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 585 | // Otherwise they may not be equivalent. |
| 586 | return false; |
| 587 | } |
| 588 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 589 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 590 | Value *Val = SI.getOperand(0); |
| 591 | Value *Ptr = SI.getOperand(1); |
| 592 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 593 | // Attempt to improve the alignment. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 594 | if (DL) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 595 | unsigned KnownAlign = |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 596 | getOrEnforceKnownAlignment(Ptr, DL->getPrefTypeAlignment(Val->getType()), |
| 597 | DL); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 598 | unsigned StoreAlign = SI.getAlignment(); |
| 599 | unsigned EffectiveStoreAlign = StoreAlign != 0 ? StoreAlign : |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 600 | DL->getABITypeAlignment(Val->getType()); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 601 | |
Bill Wendling | 55b6b2b | 2012-03-16 18:20:54 +0000 | [diff] [blame] | 602 | if (KnownAlign > EffectiveStoreAlign) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 603 | SI.setAlignment(KnownAlign); |
Bill Wendling | 55b6b2b | 2012-03-16 18:20:54 +0000 | [diff] [blame] | 604 | else if (StoreAlign == 0) |
| 605 | SI.setAlignment(EffectiveStoreAlign); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 608 | // Don't hack volatile/atomic stores. |
| 609 | // FIXME: Some bits are legal for atomic stores; needs refactoring. |
| 610 | if (!SI.isSimple()) return 0; |
| 611 | |
| 612 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 613 | // alloca dead. |
| 614 | if (Ptr->hasOneUse()) { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 615 | if (isa<AllocaInst>(Ptr)) |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 616 | return EraseInstFromFunction(SI); |
| 617 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 618 | if (isa<AllocaInst>(GEP->getOperand(0))) { |
| 619 | if (GEP->getOperand(0)->hasOneUse()) |
| 620 | return EraseInstFromFunction(SI); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 625 | // Do really simple DSE, to catch cases where there are several consecutive |
| 626 | // stores to the same location, separated by a few arithmetic operations. This |
| 627 | // situation often occurs with bitfield accesses. |
| 628 | BasicBlock::iterator BBI = &SI; |
| 629 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 630 | --ScanInsts) { |
| 631 | --BBI; |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 632 | // Don't count debug info directives, lest they affect codegen, |
| 633 | // and we skip pointer-to-pointer bitcasts, which are NOPs. |
| 634 | if (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 635 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 636 | ScanInsts++; |
| 637 | continue; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 640 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 641 | // Prev store isn't volatile, and stores to the same location? |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 642 | if (PrevSI->isSimple() && equivalentAddressValues(PrevSI->getOperand(1), |
| 643 | SI.getOperand(1))) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 644 | ++NumDeadStore; |
| 645 | ++BBI; |
| 646 | EraseInstFromFunction(*PrevSI); |
| 647 | continue; |
| 648 | } |
| 649 | break; |
| 650 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 651 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 652 | // If this is a load, we have to stop. However, if the loaded value is from |
| 653 | // the pointer we're loading and is producing the pointer we're storing, |
| 654 | // then *this* store is dead (X = load P; store X -> P). |
| 655 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Jin-Gu Kang | b452db0 | 2011-03-14 01:21:00 +0000 | [diff] [blame] | 656 | if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) && |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 657 | LI->isSimple()) |
Jin-Gu Kang | b452db0 | 2011-03-14 01:21:00 +0000 | [diff] [blame] | 658 | return EraseInstFromFunction(SI); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 659 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 660 | // Otherwise, this is a load from some other location. Stores before it |
| 661 | // may not be dead. |
| 662 | break; |
| 663 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 664 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 665 | // Don't skip over loads or things that can modify memory. |
| 666 | if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory()) |
| 667 | break; |
| 668 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 669 | |
| 670 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 671 | if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) { |
| 672 | if (!isa<UndefValue>(Val)) { |
| 673 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 674 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 675 | Worklist.Add(U); // Dropped a use. |
| 676 | } |
| 677 | return 0; // Do not modify these! |
| 678 | } |
| 679 | |
| 680 | // store undef, Ptr -> noop |
| 681 | if (isa<UndefValue>(Val)) |
| 682 | return EraseInstFromFunction(SI); |
| 683 | |
| 684 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 685 | // source instead. |
| 686 | if (isa<CastInst>(Ptr)) |
| 687 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 688 | return Res; |
| 689 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 690 | if (CE->isCast()) |
| 691 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 692 | return Res; |
| 693 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 694 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 695 | // If this store is the last instruction in the basic block (possibly |
Victor Hernandez | 5f5abd5 | 2010-01-21 23:07:15 +0000 | [diff] [blame] | 696 | // excepting debug info instructions), and if the block ends with an |
| 697 | // unconditional branch, try to move it to the successor block. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 698 | BBI = &SI; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 699 | do { |
| 700 | ++BBI; |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 701 | } while (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 702 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 703 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 704 | if (BI->isUnconditional()) |
| 705 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 706 | return 0; // xform done! |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 707 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 712 | /// if () { *P = v1; } else { *P = v2 } |
| 713 | /// into a phi node with a store in the successor. |
| 714 | /// |
| 715 | /// Simplify things like: |
| 716 | /// *P = v1; if () { *P = v2; } |
| 717 | /// into a phi node with a store in the successor. |
| 718 | /// |
| 719 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 720 | BasicBlock *StoreBB = SI.getParent(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 721 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 722 | // Check to see if the successor block has exactly two incoming edges. If |
| 723 | // so, see if the other predecessor contains a store to the same location. |
| 724 | // if so, insert a PHI node (if needed) and move the stores down. |
| 725 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 726 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 727 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 728 | // the other predecessor. |
| 729 | pred_iterator PI = pred_begin(DestBB); |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 730 | BasicBlock *P = *PI; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 731 | BasicBlock *OtherBB = 0; |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 732 | |
| 733 | if (P != StoreBB) |
| 734 | OtherBB = P; |
| 735 | |
| 736 | if (++PI == pred_end(DestBB)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 737 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 738 | |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 739 | P = *PI; |
| 740 | if (P != StoreBB) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 741 | if (OtherBB) |
| 742 | return false; |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 743 | OtherBB = P; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 744 | } |
| 745 | if (++PI != pred_end(DestBB)) |
| 746 | return false; |
| 747 | |
| 748 | // Bail out if all the relevant blocks aren't distinct (this can happen, |
| 749 | // for example, if SI is in an infinite loop) |
| 750 | if (StoreBB == DestBB || OtherBB == DestBB) |
| 751 | return false; |
| 752 | |
| 753 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 754 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
| 755 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
| 756 | if (!OtherBr || BBI == OtherBB->begin()) |
| 757 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 758 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 759 | // If the other block ends in an unconditional branch, check for the 'if then |
| 760 | // else' case. there is an instruction before the branch. |
| 761 | StoreInst *OtherStore = 0; |
| 762 | if (OtherBr->isUnconditional()) { |
| 763 | --BBI; |
| 764 | // Skip over debugging info. |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 765 | while (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 766 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 767 | if (BBI==OtherBB->begin()) |
| 768 | return false; |
| 769 | --BBI; |
| 770 | } |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 771 | // If this isn't a store, isn't a store to the same location, or is not the |
| 772 | // right kind of store, bail out. |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 773 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 774 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) || |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 775 | !SI.isSameOperationAs(OtherStore)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 776 | return false; |
| 777 | } else { |
| 778 | // Otherwise, the other block ended with a conditional branch. If one of the |
| 779 | // destinations is StoreBB, then we have the if/then case. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 780 | if (OtherBr->getSuccessor(0) != StoreBB && |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 781 | OtherBr->getSuccessor(1) != StoreBB) |
| 782 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 783 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 784 | // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an |
| 785 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 786 | // lives in OtherBB. |
| 787 | for (;; --BBI) { |
| 788 | // Check to see if we find the matching store. |
| 789 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 790 | if (OtherStore->getOperand(1) != SI.getOperand(1) || |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 791 | !SI.isSameOperationAs(OtherStore)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 792 | return false; |
| 793 | break; |
| 794 | } |
| 795 | // If we find something that may be using or overwriting the stored |
| 796 | // value, or if we run out of instructions, we can't do the xform. |
| 797 | if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() || |
| 798 | BBI == OtherBB->begin()) |
| 799 | return false; |
| 800 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 801 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 802 | // In order to eliminate the store in OtherBr, we have to |
| 803 | // make sure nothing reads or overwrites the stored value in |
| 804 | // StoreBB. |
| 805 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 806 | // FIXME: This should really be AA driven. |
| 807 | if (I->mayReadFromMemory() || I->mayWriteToMemory()) |
| 808 | return false; |
| 809 | } |
| 810 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 811 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 812 | // Insert a PHI node now if we need it. |
| 813 | Value *MergedVal = OtherStore->getOperand(0); |
| 814 | if (MergedVal != SI.getOperand(0)) { |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 815 | PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge"); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 816 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 817 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 818 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
| 819 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 820 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 821 | // Advance to a place where it is safe to insert the new store and |
| 822 | // insert it. |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 823 | BBI = DestBB->getFirstInsertionPt(); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 824 | StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1), |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 825 | SI.isVolatile(), |
| 826 | SI.getAlignment(), |
| 827 | SI.getOrdering(), |
| 828 | SI.getSynchScope()); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 829 | InsertNewInstBefore(NewSI, *BBI); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 830 | NewSI->setDebugLoc(OtherStore->getDebugLoc()); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 831 | |
Chris Lattner | eeefe1b | 2012-12-31 08:10:58 +0000 | [diff] [blame] | 832 | // If the two stores had the same TBAA tag, preserve it. |
Chris Lattner | 473988c | 2013-01-05 16:44:07 +0000 | [diff] [blame] | 833 | if (MDNode *TBAATag = SI.getMetadata(LLVMContext::MD_tbaa)) |
| 834 | if ((TBAATag = MDNode::getMostGenericTBAA(TBAATag, |
| 835 | OtherStore->getMetadata(LLVMContext::MD_tbaa)))) |
| 836 | NewSI->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
Chris Lattner | eeefe1b | 2012-12-31 08:10:58 +0000 | [diff] [blame] | 837 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 838 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 839 | // Nuke the old stores. |
| 840 | EraseInstFromFunction(SI); |
| 841 | EraseInstFromFunction(*OtherStore); |
| 842 | return true; |
| 843 | } |