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