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 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
NAKAMURA Takumi | ec6b1fc | 2015-12-15 09:37:31 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 826bdf8 | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Loads.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/IntrinsicInst.h" |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 21 | #include "llvm/IR/MDBuilder.h" |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 23 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 26 | #define DEBUG_TYPE "instcombine" |
| 27 | |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 28 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 29 | STATISTIC(NumGlobalCopies, "Number of allocas copied from constant global"); |
| 30 | |
| 31 | /// pointsToConstantGlobal - Return true if V (possibly indirectly) points to |
| 32 | /// some part of a constant global variable. This intentionally only accepts |
| 33 | /// constant expressions because we can't rewrite arbitrary instructions. |
| 34 | static bool pointsToConstantGlobal(Value *V) { |
| 35 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 36 | return GV->isConstant(); |
Matt Arsenault | 60728177 | 2014-04-24 00:01:09 +0000 | [diff] [blame] | 37 | |
| 38 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 39 | if (CE->getOpcode() == Instruction::BitCast || |
Matt Arsenault | 60728177 | 2014-04-24 00:01:09 +0000 | [diff] [blame] | 40 | CE->getOpcode() == Instruction::AddrSpaceCast || |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 41 | CE->getOpcode() == Instruction::GetElementPtr) |
| 42 | return pointsToConstantGlobal(CE->getOperand(0)); |
Matt Arsenault | 60728177 | 2014-04-24 00:01:09 +0000 | [diff] [blame] | 43 | } |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
| 47 | /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) |
| 48 | /// pointer to an alloca. Ignore any reads of the pointer, return false if we |
| 49 | /// see any stores or other unknown uses. If we see pointer arithmetic, keep |
| 50 | /// track of whether it moves the pointer (with IsOffset) but otherwise traverse |
| 51 | /// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to |
| 52 | /// the alloca, and if the source pointer is a pointer to a constant global, we |
| 53 | /// can optimize this. |
| 54 | static bool |
| 55 | isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy, |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 56 | SmallVectorImpl<Instruction *> &ToDelete) { |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 57 | // We track lifetime intrinsics as we encounter them. If we decide to go |
| 58 | // ahead and replace the value with the global, this lets the caller quickly |
| 59 | // eliminate the markers. |
| 60 | |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 61 | SmallVector<std::pair<Value *, bool>, 35> ValuesToInspect; |
| 62 | ValuesToInspect.push_back(std::make_pair(V, false)); |
| 63 | while (!ValuesToInspect.empty()) { |
| 64 | auto ValuePair = ValuesToInspect.pop_back_val(); |
| 65 | const bool IsOffset = ValuePair.second; |
| 66 | for (auto &U : ValuePair.first->uses()) { |
| 67 | Instruction *I = cast<Instruction>(U.getUser()); |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 68 | |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 69 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 70 | // Ignore non-volatile loads, they are always ok. |
| 71 | if (!LI->isSimple()) return false; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 72 | continue; |
| 73 | } |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 74 | |
| 75 | if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) { |
| 76 | // If uses of the bitcast are ok, we are ok. |
| 77 | ValuesToInspect.push_back(std::make_pair(I, IsOffset)); |
| 78 | continue; |
| 79 | } |
| 80 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 81 | // If the GEP has all zero indices, it doesn't offset the pointer. If it |
| 82 | // doesn't, it does. |
| 83 | ValuesToInspect.push_back( |
| 84 | std::make_pair(I, IsOffset || !GEP->hasAllZeroIndices())); |
| 85 | continue; |
| 86 | } |
| 87 | |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 88 | if (auto CS = CallSite(I)) { |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 89 | // If this is the function being called then we treat it like a load and |
| 90 | // ignore it. |
| 91 | if (CS.isCallee(&U)) |
| 92 | continue; |
| 93 | |
David Majnemer | 02f4787 | 2015-12-23 09:58:41 +0000 | [diff] [blame] | 94 | unsigned DataOpNo = CS.getDataOperandNo(&U); |
| 95 | bool IsArgOperand = CS.isArgOperand(&U); |
| 96 | |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 97 | // Inalloca arguments are clobbered by the call. |
David Majnemer | 02f4787 | 2015-12-23 09:58:41 +0000 | [diff] [blame] | 98 | if (IsArgOperand && CS.isInAllocaArgument(DataOpNo)) |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 99 | return false; |
| 100 | |
| 101 | // If this is a readonly/readnone call site, then we know it is just a |
| 102 | // load (but one that potentially returns the value itself), so we can |
| 103 | // ignore it if we know that the value isn't captured. |
| 104 | if (CS.onlyReadsMemory() && |
David Majnemer | 02f4787 | 2015-12-23 09:58:41 +0000 | [diff] [blame] | 105 | (CS.getInstruction()->use_empty() || CS.doesNotCapture(DataOpNo))) |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 106 | continue; |
| 107 | |
| 108 | // If this is being passed as a byval argument, the caller is making a |
| 109 | // copy, so it is only a read of the alloca. |
David Majnemer | 02f4787 | 2015-12-23 09:58:41 +0000 | [diff] [blame] | 110 | if (IsArgOperand && CS.isByValArgument(DataOpNo)) |
Reid Kleckner | 813dab2 | 2014-07-01 21:36:20 +0000 | [diff] [blame] | 111 | continue; |
| 112 | } |
| 113 | |
| 114 | // Lifetime intrinsics can be handled by the caller. |
| 115 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 116 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 117 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 118 | assert(II->use_empty() && "Lifetime markers have no result to use!"); |
| 119 | ToDelete.push_back(II); |
| 120 | continue; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // If this is isn't our memcpy/memmove, reject it as something we can't |
| 125 | // handle. |
| 126 | MemTransferInst *MI = dyn_cast<MemTransferInst>(I); |
| 127 | if (!MI) |
| 128 | return false; |
| 129 | |
| 130 | // If the transfer is using the alloca as a source of the transfer, then |
| 131 | // ignore it since it is a load (unless the transfer is volatile). |
| 132 | if (U.getOperandNo() == 1) { |
| 133 | if (MI->isVolatile()) return false; |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | // If we already have seen a copy, reject the second one. |
| 138 | if (TheCopy) return false; |
| 139 | |
| 140 | // If the pointer has been offset from the start of the alloca, we can't |
| 141 | // safely handle this. |
| 142 | if (IsOffset) return false; |
| 143 | |
| 144 | // If the memintrinsic isn't using the alloca as the dest, reject it. |
| 145 | if (U.getOperandNo() != 0) return false; |
| 146 | |
| 147 | // If the source of the memcpy/move is not a constant global, reject it. |
| 148 | if (!pointsToConstantGlobal(MI->getSource())) |
| 149 | return false; |
| 150 | |
| 151 | // Otherwise, the transform is safe. Remember the copy instruction. |
| 152 | TheCopy = MI; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 153 | } |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only |
| 159 | /// modified by a copy from a constant global. If we can prove this, we can |
| 160 | /// replace any uses of the alloca with uses of the global directly. |
| 161 | static MemTransferInst * |
| 162 | isOnlyCopiedFromConstantGlobal(AllocaInst *AI, |
| 163 | SmallVectorImpl<Instruction *> &ToDelete) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 164 | MemTransferInst *TheCopy = nullptr; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 165 | if (isOnlyCopiedFromConstantGlobal(AI, TheCopy, ToDelete)) |
| 166 | return TheCopy; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 167 | return nullptr; |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Duncan P. N. Exon Smith | c6820ec | 2015-03-13 19:22:03 +0000 | [diff] [blame] | 170 | static Instruction *simplifyAllocaArraySize(InstCombiner &IC, AllocaInst &AI) { |
Duncan P. N. Exon Smith | 720762e | 2015-03-13 19:30:44 +0000 | [diff] [blame] | 171 | // Check for array size of 1 (scalar allocation). |
Duncan P. N. Exon Smith | be95b4a | 2015-03-13 19:42:09 +0000 | [diff] [blame] | 172 | if (!AI.isArrayAllocation()) { |
| 173 | // i32 1 is the canonical array size for scalar allocations. |
| 174 | if (AI.getArraySize()->getType()->isIntegerTy(32)) |
| 175 | return nullptr; |
| 176 | |
| 177 | // Canonicalize it. |
| 178 | Value *V = IC.Builder->getInt32(1); |
| 179 | AI.setOperand(0, V); |
| 180 | return &AI; |
| 181 | } |
Duncan P. N. Exon Smith | 720762e | 2015-03-13 19:30:44 +0000 | [diff] [blame] | 182 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 183 | // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1 |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 184 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 185 | Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); |
| 186 | AllocaInst *New = IC.Builder->CreateAlloca(NewTy, nullptr, AI.getName()); |
| 187 | New->setAlignment(AI.getAlignment()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 188 | |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 189 | // Scan to the end of the allocation instructions, to skip over a block of |
| 190 | // allocas if possible...also skip interleaved debug info |
| 191 | // |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 192 | BasicBlock::iterator It(New); |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 193 | while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) |
| 194 | ++It; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 195 | |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 196 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 197 | // insert our getelementptr instruction... |
| 198 | // |
| 199 | Type *IdxTy = IC.getDataLayout().getIntPtrType(AI.getType()); |
| 200 | Value *NullIdx = Constant::getNullValue(IdxTy); |
| 201 | Value *Idx[2] = {NullIdx, NullIdx}; |
| 202 | Instruction *GEP = |
Matt Arsenault | 640ff9d | 2013-08-14 00:24:05 +0000 | [diff] [blame] | 203 | GetElementPtrInst::CreateInBounds(New, Idx, New->getName() + ".sub"); |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 204 | IC.InsertNewInstBefore(GEP, *It); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 205 | |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 206 | // Now make everything use the getelementptr instead of the original |
| 207 | // allocation. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 208 | return IC.replaceInstUsesWith(AI, GEP); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 211 | if (isa<UndefValue>(AI.getArraySize())) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 212 | return IC.replaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); |
Duncan P. N. Exon Smith | bb73013 | 2015-03-13 19:26:33 +0000 | [diff] [blame] | 213 | |
Duncan P. N. Exon Smith | 07ff9b0 | 2015-03-13 19:34:55 +0000 | [diff] [blame] | 214 | // Ensure that the alloca array size argument has type intptr_t, so that |
| 215 | // any casting is exposed early. |
| 216 | Type *IntPtrTy = IC.getDataLayout().getIntPtrType(AI.getType()); |
| 217 | if (AI.getArraySize()->getType() != IntPtrTy) { |
| 218 | Value *V = IC.Builder->CreateIntCast(AI.getArraySize(), IntPtrTy, false); |
| 219 | AI.setOperand(0, V); |
| 220 | return &AI; |
| 221 | } |
| 222 | |
Duncan P. N. Exon Smith | c6820ec | 2015-03-13 19:22:03 +0000 | [diff] [blame] | 223 | return nullptr; |
| 224 | } |
| 225 | |
| 226 | Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) { |
| 227 | if (auto *I = simplifyAllocaArraySize(*this, AI)) |
| 228 | return I; |
| 229 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 230 | if (AI.getAllocatedType()->isSized()) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 231 | // If the alignment is 0 (unspecified), assign it the preferred alignment. |
| 232 | if (AI.getAlignment() == 0) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 233 | AI.setAlignment(DL.getPrefTypeAlignment(AI.getAllocatedType())); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 234 | |
| 235 | // Move all alloca's of zero byte objects to the entry block and merge them |
| 236 | // together. Note that we only do this for alloca's, because malloc should |
| 237 | // allocate and return a unique pointer, even for a zero byte allocation. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 238 | if (DL.getTypeAllocSize(AI.getAllocatedType()) == 0) { |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 239 | // For a zero sized alloca there is no point in doing an array allocation. |
| 240 | // This is helpful if the array size is a complicated expression not used |
| 241 | // elsewhere. |
| 242 | if (AI.isArrayAllocation()) { |
| 243 | AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1)); |
| 244 | return &AI; |
| 245 | } |
| 246 | |
| 247 | // Get the first instruction in the entry block. |
| 248 | BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock(); |
| 249 | Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg(); |
| 250 | if (FirstInst != &AI) { |
| 251 | // If the entry block doesn't start with a zero-size alloca then move |
| 252 | // this one to the start of the entry block. There is no problem with |
| 253 | // dominance as the array size was forced to a constant earlier already. |
| 254 | AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst); |
| 255 | if (!EntryAI || !EntryAI->getAllocatedType()->isSized() || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 256 | DL.getTypeAllocSize(EntryAI->getAllocatedType()) != 0) { |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 257 | AI.moveBefore(FirstInst); |
| 258 | return &AI; |
| 259 | } |
| 260 | |
Richard Osborne | b68053e | 2012-09-18 09:31:44 +0000 | [diff] [blame] | 261 | // If the alignment of the entry block alloca is 0 (unspecified), |
| 262 | // assign it the preferred alignment. |
| 263 | if (EntryAI->getAlignment() == 0) |
| 264 | EntryAI->setAlignment( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 265 | DL.getPrefTypeAlignment(EntryAI->getAllocatedType())); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 266 | // Replace this zero-sized alloca with the one at the start of the entry |
| 267 | // block after ensuring that the address will be aligned enough for both |
| 268 | // types. |
Richard Osborne | b68053e | 2012-09-18 09:31:44 +0000 | [diff] [blame] | 269 | unsigned MaxAlign = std::max(EntryAI->getAlignment(), |
| 270 | AI.getAlignment()); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 271 | EntryAI->setAlignment(MaxAlign); |
| 272 | if (AI.getType() != EntryAI->getType()) |
| 273 | return new BitCastInst(EntryAI, AI.getType()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 274 | return replaceInstUsesWith(AI, EntryAI); |
Duncan Sands | 8bc764a | 2012-06-26 13:39:21 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Eli Friedman | b14873c | 2012-11-26 23:04:53 +0000 | [diff] [blame] | 279 | if (AI.getAlignment()) { |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 280 | // Check to see if this allocation is only modified by a memcpy/memmove from |
| 281 | // a constant global whose alignment is equal to or exceeds that of the |
| 282 | // allocation. If this is the case, we can change all users to use |
| 283 | // the constant global instead. This is commonly produced by the CFE by |
| 284 | // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A' |
| 285 | // is only subsequently read. |
| 286 | SmallVector<Instruction *, 4> ToDelete; |
| 287 | if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 288 | unsigned SourceAlign = getOrEnforceKnownAlignment( |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 289 | Copy->getSource(), AI.getAlignment(), DL, &AI, AC, DT); |
Eli Friedman | b14873c | 2012-11-26 23:04:53 +0000 | [diff] [blame] | 290 | if (AI.getAlignment() <= SourceAlign) { |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 291 | DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n'); |
| 292 | DEBUG(dbgs() << " memcpy = " << *Copy << '\n'); |
| 293 | for (unsigned i = 0, e = ToDelete.size(); i != e; ++i) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 294 | eraseInstFromFunction(*ToDelete[i]); |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 295 | Constant *TheSrc = cast<Constant>(Copy->getSource()); |
Matt Arsenault | bbf18c6 | 2013-12-07 02:58:45 +0000 | [diff] [blame] | 296 | Constant *Cast |
| 297 | = ConstantExpr::getPointerBitCastOrAddrSpaceCast(TheSrc, AI.getType()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 298 | Instruction *NewI = replaceInstUsesWith(AI, Cast); |
| 299 | eraseInstFromFunction(*Copy); |
Richard Osborne | 2fd29bf | 2012-09-24 17:10:03 +0000 | [diff] [blame] | 300 | ++NumGlobalCopies; |
| 301 | return NewI; |
| 302 | } |
Chandler Carruth | c908ca1 | 2012-08-21 08:39:44 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 306 | // At last, use the generic allocation site handler to aggressively remove |
| 307 | // unused allocas. |
| 308 | return visitAllocSite(AI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 311 | /// \brief Helper to combine a load to a new type. |
| 312 | /// |
| 313 | /// This just does the work of combining a load to a new type. It handles |
| 314 | /// metadata, etc., and returns the new instruction. The \c NewTy should be the |
| 315 | /// loaded *value* type. This will convert it to a pointer, cast the operand to |
| 316 | /// that pointer type, load it, etc. |
| 317 | /// |
| 318 | /// Note that this will create all of the instructions with whatever insert |
| 319 | /// point the \c InstCombiner currently is using. |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 320 | static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewTy, |
| 321 | const Twine &Suffix = "") { |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 322 | Value *Ptr = LI.getPointerOperand(); |
| 323 | unsigned AS = LI.getPointerAddressSpace(); |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 324 | SmallVector<std::pair<unsigned, MDNode *>, 8> MD; |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 325 | LI.getAllMetadata(MD); |
| 326 | |
| 327 | LoadInst *NewLoad = IC.Builder->CreateAlignedLoad( |
| 328 | IC.Builder->CreateBitCast(Ptr, NewTy->getPointerTo(AS)), |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 329 | LI.getAlignment(), LI.getName() + Suffix); |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 330 | MDBuilder MDB(NewLoad->getContext()); |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 331 | for (const auto &MDPair : MD) { |
| 332 | unsigned ID = MDPair.first; |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 333 | MDNode *N = MDPair.second; |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 334 | // Note, essentially every kind of metadata should be preserved here! This |
| 335 | // routine is supposed to clone a load instruction changing *only its type*. |
| 336 | // The only metadata it makes sense to drop is metadata which is invalidated |
| 337 | // when the pointer type changes. This should essentially never be the case |
| 338 | // in LLVM, but we explicitly switch over only known metadata to be |
| 339 | // conservatively correct. If you are adding metadata to LLVM which pertains |
| 340 | // to loads, you almost certainly want to add it here. |
| 341 | switch (ID) { |
| 342 | case LLVMContext::MD_dbg: |
| 343 | case LLVMContext::MD_tbaa: |
| 344 | case LLVMContext::MD_prof: |
| 345 | case LLVMContext::MD_fpmath: |
| 346 | case LLVMContext::MD_tbaa_struct: |
| 347 | case LLVMContext::MD_invariant_load: |
| 348 | case LLVMContext::MD_alias_scope: |
| 349 | case LLVMContext::MD_noalias: |
Philip Reames | 5a3f5f7 | 2014-10-21 00:13:20 +0000 | [diff] [blame] | 350 | case LLVMContext::MD_nontemporal: |
| 351 | case LLVMContext::MD_mem_parallel_loop_access: |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 352 | // All of these directly apply. |
| 353 | NewLoad->setMetadata(ID, N); |
| 354 | break; |
| 355 | |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 356 | case LLVMContext::MD_nonnull: |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 357 | // This only directly applies if the new type is also a pointer. |
| 358 | if (NewTy->isPointerTy()) { |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 359 | NewLoad->setMetadata(ID, N); |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 360 | break; |
| 361 | } |
| 362 | // If it's integral now, translate it to !range metadata. |
| 363 | if (NewTy->isIntegerTy()) { |
| 364 | auto *ITy = cast<IntegerType>(NewTy); |
| 365 | auto *NullInt = ConstantExpr::getPtrToInt( |
| 366 | ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy); |
| 367 | auto *NonNullInt = |
| 368 | ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1)); |
| 369 | NewLoad->setMetadata(LLVMContext::MD_range, |
| 370 | MDB.createRange(NonNullInt, NullInt)); |
| 371 | } |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 372 | break; |
Artur Pilipenko | 5c5011d | 2015-11-02 17:53:51 +0000 | [diff] [blame] | 373 | case LLVMContext::MD_align: |
| 374 | case LLVMContext::MD_dereferenceable: |
| 375 | case LLVMContext::MD_dereferenceable_or_null: |
| 376 | // These only directly apply if the new type is also a pointer. |
| 377 | if (NewTy->isPointerTy()) |
| 378 | NewLoad->setMetadata(ID, N); |
| 379 | break; |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 380 | case LLVMContext::MD_range: |
| 381 | // FIXME: It would be nice to propagate this in some way, but the type |
Charles Davis | 33d1dc0 | 2015-02-25 05:10:25 +0000 | [diff] [blame] | 382 | // conversions make it hard. If the new type is a pointer, we could |
| 383 | // translate it to !nonnull metadata. |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 384 | break; |
| 385 | } |
| 386 | } |
Chandler Carruth | bc6378d | 2014-10-19 10:46:46 +0000 | [diff] [blame] | 387 | return NewLoad; |
| 388 | } |
| 389 | |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 390 | /// \brief Combine a store to a new type. |
| 391 | /// |
| 392 | /// Returns the newly created store instruction. |
| 393 | static StoreInst *combineStoreToNewValue(InstCombiner &IC, StoreInst &SI, Value *V) { |
| 394 | Value *Ptr = SI.getPointerOperand(); |
| 395 | unsigned AS = SI.getPointerAddressSpace(); |
| 396 | SmallVector<std::pair<unsigned, MDNode *>, 8> MD; |
| 397 | SI.getAllMetadata(MD); |
| 398 | |
| 399 | StoreInst *NewStore = IC.Builder->CreateAlignedStore( |
| 400 | V, IC.Builder->CreateBitCast(Ptr, V->getType()->getPointerTo(AS)), |
| 401 | SI.getAlignment()); |
| 402 | for (const auto &MDPair : MD) { |
| 403 | unsigned ID = MDPair.first; |
| 404 | MDNode *N = MDPair.second; |
| 405 | // Note, essentially every kind of metadata should be preserved here! This |
| 406 | // routine is supposed to clone a store instruction changing *only its |
| 407 | // type*. The only metadata it makes sense to drop is metadata which is |
| 408 | // invalidated when the pointer type changes. This should essentially |
| 409 | // never be the case in LLVM, but we explicitly switch over only known |
| 410 | // metadata to be conservatively correct. If you are adding metadata to |
| 411 | // LLVM which pertains to stores, you almost certainly want to add it |
| 412 | // here. |
| 413 | switch (ID) { |
| 414 | case LLVMContext::MD_dbg: |
| 415 | case LLVMContext::MD_tbaa: |
| 416 | case LLVMContext::MD_prof: |
| 417 | case LLVMContext::MD_fpmath: |
| 418 | case LLVMContext::MD_tbaa_struct: |
| 419 | case LLVMContext::MD_alias_scope: |
| 420 | case LLVMContext::MD_noalias: |
| 421 | case LLVMContext::MD_nontemporal: |
| 422 | case LLVMContext::MD_mem_parallel_loop_access: |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 423 | // All of these directly apply. |
| 424 | NewStore->setMetadata(ID, N); |
| 425 | break; |
| 426 | |
| 427 | case LLVMContext::MD_invariant_load: |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 428 | case LLVMContext::MD_nonnull: |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 429 | case LLVMContext::MD_range: |
Artur Pilipenko | 5c5011d | 2015-11-02 17:53:51 +0000 | [diff] [blame] | 430 | case LLVMContext::MD_align: |
| 431 | case LLVMContext::MD_dereferenceable: |
| 432 | case LLVMContext::MD_dereferenceable_or_null: |
Chandler Carruth | 87fdafc | 2015-02-13 02:30:01 +0000 | [diff] [blame] | 433 | // These don't apply for stores. |
Chandler Carruth | fa11d83 | 2015-01-22 03:34:54 +0000 | [diff] [blame] | 434 | break; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return NewStore; |
| 439 | } |
| 440 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 441 | /// \brief Combine loads to match the type of value their uses after looking |
| 442 | /// through intervening bitcasts. |
| 443 | /// |
| 444 | /// The core idea here is that if the result of a load is used in an operation, |
| 445 | /// we should load the type most conducive to that operation. For example, when |
| 446 | /// loading an integer and converting that immediately to a pointer, we should |
| 447 | /// instead directly load a pointer. |
| 448 | /// |
| 449 | /// However, this routine must never change the width of a load or the number of |
| 450 | /// loads as that would introduce a semantic change. This combine is expected to |
| 451 | /// be a semantic no-op which just allows loads to more closely model the types |
| 452 | /// of their consuming operations. |
| 453 | /// |
| 454 | /// Currently, we also refuse to change the precise type used for an atomic load |
| 455 | /// or a volatile load. This is debatable, and might be reasonable to change |
| 456 | /// later. However, it is risky in case some backend or other part of LLVM is |
| 457 | /// relying on the exact type loaded to select appropriate atomic operations. |
| 458 | static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) { |
| 459 | // FIXME: We could probably with some care handle both volatile and atomic |
| 460 | // loads here but it isn't clear that this is important. |
| 461 | if (!LI.isSimple()) |
| 462 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 463 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 464 | if (LI.use_empty()) |
| 465 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 466 | |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 467 | Type *Ty = LI.getType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 468 | const DataLayout &DL = IC.getDataLayout(); |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 469 | |
| 470 | // Try to canonicalize loads which are only ever stored to operate over |
| 471 | // integers instead of any other type. We only do this when the loaded type |
| 472 | // is sized and has a size exactly the same as its store size and the store |
| 473 | // size is a legal integer type. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 474 | if (!Ty->isIntegerTy() && Ty->isSized() && |
| 475 | DL.isLegalInteger(DL.getTypeStoreSizeInBits(Ty)) && |
| 476 | DL.getTypeStoreSizeInBits(Ty) == DL.getTypeSizeInBits(Ty)) { |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 477 | if (std::all_of(LI.user_begin(), LI.user_end(), [&LI](User *U) { |
| 478 | auto *SI = dyn_cast<StoreInst>(U); |
| 479 | return SI && SI->getPointerOperand() != &LI; |
| 480 | })) { |
| 481 | LoadInst *NewLoad = combineLoadToNewType( |
| 482 | IC, LI, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 483 | Type::getIntNTy(LI.getContext(), DL.getTypeStoreSizeInBits(Ty))); |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 484 | // Replace all the stores with stores of the newly loaded value. |
| 485 | for (auto UI = LI.user_begin(), UE = LI.user_end(); UI != UE;) { |
| 486 | auto *SI = cast<StoreInst>(*UI++); |
| 487 | IC.Builder->SetInsertPoint(SI); |
| 488 | combineStoreToNewValue(IC, *SI, NewLoad); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 489 | IC.eraseInstFromFunction(*SI); |
Chandler Carruth | cd8522e | 2015-01-22 05:08:12 +0000 | [diff] [blame] | 490 | } |
| 491 | assert(LI.use_empty() && "Failed to remove all users of the load!"); |
| 492 | // Return the old load so the combiner can delete it safely. |
| 493 | return &LI; |
| 494 | } |
| 495 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 496 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 497 | // Fold away bit casts of the loaded value by loading the desired type. |
Quentin Colombet | 490cfbe | 2016-02-11 22:30:41 +0000 | [diff] [blame^] | 498 | // We can do this for BitCastInsts as well as casts from and to pointer types, |
| 499 | // as long as those are noops (i.e., the source or dest type have the same |
| 500 | // bitwidth as the target's pointers). |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 501 | if (LI.hasOneUse()) |
Quentin Colombet | 490cfbe | 2016-02-11 22:30:41 +0000 | [diff] [blame^] | 502 | if (auto* CI = dyn_cast<CastInst>(LI.user_back())) { |
| 503 | if (CI->isNoopCast(DL)) { |
| 504 | LoadInst *NewLoad = combineLoadToNewType(IC, LI, CI->getDestTy()); |
| 505 | CI->replaceAllUsesWith(NewLoad); |
| 506 | IC.eraseInstFromFunction(*CI); |
| 507 | return &LI; |
| 508 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 509 | } |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 510 | |
Chandler Carruth | a7f247e | 2014-12-09 19:21:16 +0000 | [diff] [blame] | 511 | // FIXME: We should also canonicalize loads of vectors when their elements are |
| 512 | // cast to other types. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 513 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 516 | static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) { |
| 517 | // FIXME: We could probably with some care handle both volatile and atomic |
| 518 | // stores here but it isn't clear that this is important. |
| 519 | if (!LI.isSimple()) |
| 520 | return nullptr; |
| 521 | |
| 522 | Type *T = LI.getType(); |
| 523 | if (!T->isAggregateType()) |
| 524 | return nullptr; |
| 525 | |
Bruce Mitchener | e9ffb45 | 2015-09-12 01:17:08 +0000 | [diff] [blame] | 526 | assert(LI.getAlignment() && "Alignment must be set at this point"); |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 527 | |
| 528 | if (auto *ST = dyn_cast<StructType>(T)) { |
| 529 | // If the struct only have one element, we unpack. |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 530 | unsigned Count = ST->getNumElements(); |
| 531 | if (Count == 1) { |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 532 | LoadInst *NewLoad = combineLoadToNewType(IC, LI, ST->getTypeAtIndex(0U), |
| 533 | ".unpack"); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 534 | return IC.replaceInstUsesWith(LI, IC.Builder->CreateInsertValue( |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 535 | UndefValue::get(T), NewLoad, 0, LI.getName())); |
| 536 | } |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 537 | |
| 538 | // We don't want to break loads with padding here as we'd loose |
| 539 | // the knowledge that padding exists for the rest of the pipeline. |
| 540 | const DataLayout &DL = IC.getDataLayout(); |
| 541 | auto *SL = DL.getStructLayout(ST); |
| 542 | if (SL->hasPadding()) |
| 543 | return nullptr; |
| 544 | |
| 545 | auto Name = LI.getName(); |
NAKAMURA Takumi | ec6b1fc | 2015-12-15 09:37:31 +0000 | [diff] [blame] | 546 | SmallString<16> LoadName = Name; |
| 547 | LoadName += ".unpack"; |
| 548 | SmallString<16> EltName = Name; |
| 549 | EltName += ".elt"; |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 550 | auto *Addr = LI.getPointerOperand(); |
| 551 | Value *V = UndefValue::get(T); |
| 552 | auto *IdxType = Type::getInt32Ty(ST->getContext()); |
| 553 | auto *Zero = ConstantInt::get(IdxType, 0); |
| 554 | for (unsigned i = 0; i < Count; i++) { |
| 555 | Value *Indices[2] = { |
| 556 | Zero, |
| 557 | ConstantInt::get(IdxType, i), |
| 558 | }; |
| 559 | auto *Ptr = IC.Builder->CreateInBoundsGEP(ST, Addr, makeArrayRef(Indices), EltName); |
Pete Cooper | 5562c33 | 2016-02-11 21:10:40 +0000 | [diff] [blame] | 560 | auto *L = IC.Builder->CreateAlignedLoad(Ptr, LI.getAlignment(), |
| 561 | LoadName); |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 562 | V = IC.Builder->CreateInsertValue(V, L, i); |
| 563 | } |
| 564 | |
| 565 | V->setName(Name); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 566 | return IC.replaceInstUsesWith(LI, V); |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 567 | } |
| 568 | |
David Majnemer | 58fb038 | 2015-05-11 05:04:22 +0000 | [diff] [blame] | 569 | if (auto *AT = dyn_cast<ArrayType>(T)) { |
| 570 | // If the array only have one element, we unpack. |
| 571 | if (AT->getNumElements() == 1) { |
| 572 | LoadInst *NewLoad = combineLoadToNewType(IC, LI, AT->getElementType(), |
| 573 | ".unpack"); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 574 | return IC.replaceInstUsesWith(LI, IC.Builder->CreateInsertValue( |
David Majnemer | 58fb038 | 2015-05-11 05:04:22 +0000 | [diff] [blame] | 575 | UndefValue::get(T), NewLoad, 0, LI.getName())); |
| 576 | } |
| 577 | } |
| 578 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 579 | return nullptr; |
| 580 | } |
| 581 | |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 582 | // If we can determine that all possible objects pointed to by the provided |
| 583 | // pointer value are, not only dereferenceable, but also definitively less than |
| 584 | // or equal to the provided maximum size, then return true. Otherwise, return |
| 585 | // false (constant global values and allocas fall into this category). |
| 586 | // |
| 587 | // FIXME: This should probably live in ValueTracking (or similar). |
| 588 | static bool isObjectSizeLessThanOrEq(Value *V, uint64_t MaxSize, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 589 | const DataLayout &DL) { |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 590 | SmallPtrSet<Value *, 4> Visited; |
| 591 | SmallVector<Value *, 4> Worklist(1, V); |
| 592 | |
| 593 | do { |
| 594 | Value *P = Worklist.pop_back_val(); |
| 595 | P = P->stripPointerCasts(); |
| 596 | |
| 597 | if (!Visited.insert(P).second) |
| 598 | continue; |
| 599 | |
| 600 | if (SelectInst *SI = dyn_cast<SelectInst>(P)) { |
| 601 | Worklist.push_back(SI->getTrueValue()); |
| 602 | Worklist.push_back(SI->getFalseValue()); |
| 603 | continue; |
| 604 | } |
| 605 | |
| 606 | if (PHINode *PN = dyn_cast<PHINode>(P)) { |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 607 | for (Value *IncValue : PN->incoming_values()) |
| 608 | Worklist.push_back(IncValue); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 609 | continue; |
| 610 | } |
| 611 | |
| 612 | if (GlobalAlias *GA = dyn_cast<GlobalAlias>(P)) { |
| 613 | if (GA->mayBeOverridden()) |
| 614 | return false; |
| 615 | Worklist.push_back(GA->getAliasee()); |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | // If we know how big this object is, and it is less than MaxSize, continue |
| 620 | // searching. Otherwise, return false. |
| 621 | if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) { |
| 622 | if (!AI->getAllocatedType()->isSized()) |
| 623 | return false; |
| 624 | |
| 625 | ConstantInt *CS = dyn_cast<ConstantInt>(AI->getArraySize()); |
| 626 | if (!CS) |
| 627 | return false; |
| 628 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 629 | uint64_t TypeSize = DL.getTypeAllocSize(AI->getAllocatedType()); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 630 | // Make sure that, even if the multiplication below would wrap as an |
| 631 | // uint64_t, we still do the right thing. |
| 632 | if ((CS->getValue().zextOrSelf(128)*APInt(128, TypeSize)).ugt(MaxSize)) |
| 633 | return false; |
| 634 | continue; |
| 635 | } |
| 636 | |
| 637 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) { |
| 638 | if (!GV->hasDefinitiveInitializer() || !GV->isConstant()) |
| 639 | return false; |
| 640 | |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 641 | uint64_t InitSize = DL.getTypeAllocSize(GV->getValueType()); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 642 | if (InitSize > MaxSize) |
| 643 | return false; |
| 644 | continue; |
| 645 | } |
| 646 | |
| 647 | return false; |
| 648 | } while (!Worklist.empty()); |
| 649 | |
| 650 | return true; |
| 651 | } |
| 652 | |
| 653 | // If we're indexing into an object of a known size, and the outer index is |
| 654 | // not a constant, but having any value but zero would lead to undefined |
| 655 | // behavior, replace it with zero. |
| 656 | // |
| 657 | // For example, if we have: |
| 658 | // @f.a = private unnamed_addr constant [1 x i32] [i32 12], align 4 |
| 659 | // ... |
| 660 | // %arrayidx = getelementptr inbounds [1 x i32]* @f.a, i64 0, i64 %x |
| 661 | // ... = load i32* %arrayidx, align 4 |
| 662 | // Then we know that we can replace %x in the GEP with i64 0. |
| 663 | // |
| 664 | // FIXME: We could fold any GEP index to zero that would cause UB if it were |
| 665 | // not zero. Currently, we only handle the first such index. Also, we could |
| 666 | // also search through non-zero constant indices if we kept track of the |
| 667 | // offsets those indices implied. |
| 668 | static bool canReplaceGEPIdxWithZero(InstCombiner &IC, GetElementPtrInst *GEPI, |
| 669 | Instruction *MemI, unsigned &Idx) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 670 | if (GEPI->getNumOperands() < 2) |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 671 | return false; |
| 672 | |
| 673 | // Find the first non-zero index of a GEP. If all indices are zero, return |
| 674 | // one past the last index. |
| 675 | auto FirstNZIdx = [](const GetElementPtrInst *GEPI) { |
| 676 | unsigned I = 1; |
| 677 | for (unsigned IE = GEPI->getNumOperands(); I != IE; ++I) { |
| 678 | Value *V = GEPI->getOperand(I); |
| 679 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 680 | if (CI->isZero()) |
| 681 | continue; |
| 682 | |
| 683 | break; |
| 684 | } |
| 685 | |
| 686 | return I; |
| 687 | }; |
| 688 | |
| 689 | // Skip through initial 'zero' indices, and find the corresponding pointer |
| 690 | // type. See if the next index is not a constant. |
| 691 | Idx = FirstNZIdx(GEPI); |
| 692 | if (Idx == GEPI->getNumOperands()) |
| 693 | return false; |
| 694 | if (isa<Constant>(GEPI->getOperand(Idx))) |
| 695 | return false; |
| 696 | |
| 697 | SmallVector<Value *, 4> Ops(GEPI->idx_begin(), GEPI->idx_begin() + Idx); |
Eduard Burtescu | 19eb031 | 2016-01-19 17:28:00 +0000 | [diff] [blame] | 698 | Type *AllocTy = |
| 699 | GetElementPtrInst::getIndexedType(GEPI->getSourceElementType(), Ops); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 700 | if (!AllocTy || !AllocTy->isSized()) |
| 701 | return false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 702 | const DataLayout &DL = IC.getDataLayout(); |
| 703 | uint64_t TyAllocSize = DL.getTypeAllocSize(AllocTy); |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 704 | |
| 705 | // If there are more indices after the one we might replace with a zero, make |
| 706 | // sure they're all non-negative. If any of them are negative, the overall |
| 707 | // address being computed might be before the base address determined by the |
| 708 | // first non-zero index. |
| 709 | auto IsAllNonNegative = [&]() { |
| 710 | for (unsigned i = Idx+1, e = GEPI->getNumOperands(); i != e; ++i) { |
| 711 | bool KnownNonNegative, KnownNegative; |
| 712 | IC.ComputeSignBit(GEPI->getOperand(i), KnownNonNegative, |
| 713 | KnownNegative, 0, MemI); |
| 714 | if (KnownNonNegative) |
| 715 | continue; |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | return true; |
| 720 | }; |
| 721 | |
| 722 | // FIXME: If the GEP is not inbounds, and there are extra indices after the |
| 723 | // one we'll replace, those could cause the address computation to wrap |
| 724 | // (rendering the IsAllNonNegative() check below insufficient). We can do |
Bruce Mitchener | e9ffb45 | 2015-09-12 01:17:08 +0000 | [diff] [blame] | 725 | // better, ignoring zero indices (and other indices we can prove small |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 726 | // enough not to wrap). |
| 727 | if (Idx+1 != GEPI->getNumOperands() && !GEPI->isInBounds()) |
| 728 | return false; |
| 729 | |
| 730 | // Note that isObjectSizeLessThanOrEq will return true only if the pointer is |
| 731 | // also known to be dereferenceable. |
| 732 | return isObjectSizeLessThanOrEq(GEPI->getOperand(0), TyAllocSize, DL) && |
| 733 | IsAllNonNegative(); |
| 734 | } |
| 735 | |
| 736 | // If we're indexing into an object with a variable index for the memory |
| 737 | // access, but the object has only one element, we can assume that the index |
| 738 | // will always be zero. If we replace the GEP, return it. |
| 739 | template <typename T> |
| 740 | static Instruction *replaceGEPIdxWithZero(InstCombiner &IC, Value *Ptr, |
| 741 | T &MemI) { |
| 742 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 743 | unsigned Idx; |
| 744 | if (canReplaceGEPIdxWithZero(IC, GEPI, &MemI, Idx)) { |
| 745 | Instruction *NewGEPI = GEPI->clone(); |
| 746 | NewGEPI->setOperand(Idx, |
| 747 | ConstantInt::get(GEPI->getOperand(Idx)->getType(), 0)); |
| 748 | NewGEPI->insertBefore(GEPI); |
| 749 | MemI.setOperand(MemI.getPointerOperandIndex(), NewGEPI); |
| 750 | return NewGEPI; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | return nullptr; |
| 755 | } |
| 756 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 757 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 758 | Value *Op = LI.getOperand(0); |
| 759 | |
Chandler Carruth | 2f75fcf | 2014-10-18 06:36:22 +0000 | [diff] [blame] | 760 | // Try to canonicalize the loaded type. |
| 761 | if (Instruction *Res = combineLoadToOperationType(*this, LI)) |
| 762 | return Res; |
| 763 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 764 | // Attempt to improve the alignment. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 765 | unsigned KnownAlign = getOrEnforceKnownAlignment( |
| 766 | Op, DL.getPrefTypeAlignment(LI.getType()), DL, &LI, AC, DT); |
| 767 | unsigned LoadAlign = LI.getAlignment(); |
| 768 | unsigned EffectiveLoadAlign = |
| 769 | LoadAlign != 0 ? LoadAlign : DL.getABITypeAlignment(LI.getType()); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 770 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 771 | if (KnownAlign > EffectiveLoadAlign) |
| 772 | LI.setAlignment(KnownAlign); |
| 773 | else if (LoadAlign == 0) |
| 774 | LI.setAlignment(EffectiveLoadAlign); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 775 | |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 776 | // Replace GEP indices if possible. |
| 777 | if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Op, LI)) { |
| 778 | Worklist.Add(NewGEPI); |
| 779 | return &LI; |
| 780 | } |
| 781 | |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 782 | // None of the following transforms are legal for volatile/atomic loads. |
| 783 | // FIXME: Some of it is okay for atomic loads; needs refactoring. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 784 | if (!LI.isSimple()) return nullptr; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 785 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 786 | if (Instruction *Res = unpackLoadToAggregate(*this, LI)) |
| 787 | return Res; |
| 788 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 789 | // 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] | 790 | // where there are several consecutive memory accesses to the same location, |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 791 | // separated by a few arithmetic operations. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 792 | BasicBlock::iterator BBI(LI); |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 793 | AAMDNodes AATags; |
Larisse Voufo | 532bf71 | 2015-09-18 19:14:35 +0000 | [diff] [blame] | 794 | if (Value *AvailableVal = |
Eduard Burtescu | e2a6917 | 2016-01-22 01:51:51 +0000 | [diff] [blame] | 795 | FindAvailableLoadedValue(&LI, LI.getParent(), BBI, |
Larisse Voufo | 532bf71 | 2015-09-18 19:14:35 +0000 | [diff] [blame] | 796 | DefMaxInstsToScan, AA, &AATags)) { |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 797 | if (LoadInst *NLI = dyn_cast<LoadInst>(AvailableVal)) { |
| 798 | unsigned KnownIDs[] = { |
Artur Pilipenko | 5c5011d | 2015-11-02 17:53:51 +0000 | [diff] [blame] | 799 | LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, |
| 800 | LLVMContext::MD_noalias, LLVMContext::MD_range, |
| 801 | LLVMContext::MD_invariant_load, LLVMContext::MD_nonnull, |
| 802 | LLVMContext::MD_invariant_group, LLVMContext::MD_align, |
| 803 | LLVMContext::MD_dereferenceable, |
| 804 | LLVMContext::MD_dereferenceable_or_null}; |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 805 | combineMetadata(NLI, &LI, KnownIDs); |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 806 | }; |
| 807 | |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 808 | return replaceInstUsesWith( |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 809 | LI, Builder->CreateBitOrPointerCast(AvailableVal, LI.getType(), |
| 810 | LI.getName() + ".cast")); |
Bjorn Steinbrink | a91fd09 | 2015-07-10 06:55:44 +0000 | [diff] [blame] | 811 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 812 | |
| 813 | // load(gep null, ...) -> unreachable |
| 814 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 815 | const Value *GEPI0 = GEPI->getOperand(0); |
| 816 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 817 | if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){ |
| 818 | // Insert a new store to null instruction before the load to indicate |
| 819 | // that this code is not reachable. We do this instead of inserting |
| 820 | // an unreachable instruction directly because we cannot modify the |
| 821 | // CFG. |
| 822 | new StoreInst(UndefValue::get(LI.getType()), |
| 823 | Constant::getNullValue(Op->getType()), &LI); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 824 | return replaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 825 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 826 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 827 | |
| 828 | // load null/undef -> unreachable |
| 829 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 830 | if (isa<UndefValue>(Op) || |
| 831 | (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) { |
| 832 | // Insert a new store to null instruction before the load to indicate that |
| 833 | // this code is not reachable. We do this instead of inserting an |
| 834 | // unreachable instruction directly because we cannot modify the CFG. |
| 835 | new StoreInst(UndefValue::get(LI.getType()), |
| 836 | Constant::getNullValue(Op->getType()), &LI); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 837 | return replaceInstUsesWith(LI, UndefValue::get(LI.getType())); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 840 | if (Op->hasOneUse()) { |
| 841 | // Change select and PHI nodes to select values instead of addresses: this |
| 842 | // helps alias analysis out a lot, allows many others simplifications, and |
| 843 | // exposes redundancy in the code. |
| 844 | // |
| 845 | // Note that we cannot do the transformation unless we know that the |
| 846 | // introduced loads cannot trap! Something like this is valid as long as |
| 847 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 848 | // but it would not be valid if we transformed it to load from null |
| 849 | // unconditionally. |
| 850 | // |
| 851 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 852 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 853 | unsigned Align = LI.getAlignment(); |
Artur Pilipenko | 6dd6969 | 2016-01-15 15:27:46 +0000 | [diff] [blame] | 854 | if (isSafeToLoadUnconditionally(SI->getOperand(1), Align, SI) && |
| 855 | isSafeToLoadUnconditionally(SI->getOperand(2), Align, SI)) { |
Bob Wilson | 4b71b6c | 2010-01-30 00:41:10 +0000 | [diff] [blame] | 856 | LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1), |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 857 | SI->getOperand(1)->getName()+".val"); |
Bob Wilson | 4b71b6c | 2010-01-30 00:41:10 +0000 | [diff] [blame] | 858 | LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2), |
Bob Wilson | 56600a1 | 2010-01-30 04:42:39 +0000 | [diff] [blame] | 859 | SI->getOperand(2)->getName()+".val"); |
| 860 | V1->setAlignment(Align); |
| 861 | V2->setAlignment(Align); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 862 | return SelectInst::Create(SI->getCondition(), V1, V2); |
| 863 | } |
| 864 | |
| 865 | // load (select (cond, null, P)) -> load P |
Larisse Voufo | 532bf71 | 2015-09-18 19:14:35 +0000 | [diff] [blame] | 866 | if (isa<ConstantPointerNull>(SI->getOperand(1)) && |
Philip Reames | 5ad26c3 | 2014-12-29 22:46:21 +0000 | [diff] [blame] | 867 | LI.getPointerAddressSpace() == 0) { |
| 868 | LI.setOperand(0, SI->getOperand(2)); |
| 869 | return &LI; |
| 870 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 871 | |
| 872 | // load (select (cond, P, null)) -> load P |
Philip Reames | 5ad26c3 | 2014-12-29 22:46:21 +0000 | [diff] [blame] | 873 | if (isa<ConstantPointerNull>(SI->getOperand(2)) && |
| 874 | LI.getPointerAddressSpace() == 0) { |
| 875 | LI.setOperand(0, SI->getOperand(1)); |
| 876 | return &LI; |
| 877 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 880 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 883 | /// \brief Combine stores to match the type of value being stored. |
| 884 | /// |
| 885 | /// The core idea here is that the memory does not have any intrinsic type and |
| 886 | /// where we can we should match the type of a store to the type of value being |
| 887 | /// stored. |
| 888 | /// |
| 889 | /// However, this routine must never change the width of a store or the number of |
| 890 | /// stores as that would introduce a semantic change. This combine is expected to |
| 891 | /// be a semantic no-op which just allows stores to more closely model the types |
| 892 | /// of their incoming values. |
| 893 | /// |
| 894 | /// Currently, we also refuse to change the precise type used for an atomic or |
| 895 | /// volatile store. This is debatable, and might be reasonable to change later. |
| 896 | /// However, it is risky in case some backend or other part of LLVM is relying |
| 897 | /// on the exact type stored to select appropriate atomic operations. |
| 898 | /// |
| 899 | /// \returns true if the store was successfully combined away. This indicates |
| 900 | /// the caller must erase the store instruction. We have to let the caller erase |
Bruce Mitchener | e9ffb45 | 2015-09-12 01:17:08 +0000 | [diff] [blame] | 901 | /// the store instruction as otherwise there is no way to signal whether it was |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 902 | /// combined or not: IC.EraseInstFromFunction returns a null pointer. |
| 903 | static bool combineStoreToValueType(InstCombiner &IC, StoreInst &SI) { |
| 904 | // FIXME: We could probably with some care handle both volatile and atomic |
| 905 | // stores here but it isn't clear that this is important. |
| 906 | if (!SI.isSimple()) |
| 907 | return false; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 908 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 909 | Value *V = SI.getValueOperand(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 910 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 911 | // Fold away bit casts of the stored value by storing the original type. |
| 912 | if (auto *BC = dyn_cast<BitCastInst>(V)) { |
Chandler Carruth | a7f247e | 2014-12-09 19:21:16 +0000 | [diff] [blame] | 913 | V = BC->getOperand(0); |
Chandler Carruth | 2135b97 | 2015-01-21 23:45:01 +0000 | [diff] [blame] | 914 | combineStoreToNewValue(IC, SI, V); |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 915 | return true; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 918 | // FIXME: We should also canonicalize loads of vectors when their elements are |
| 919 | // cast to other types. |
| 920 | return false; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 923 | static bool unpackStoreToAggregate(InstCombiner &IC, StoreInst &SI) { |
| 924 | // FIXME: We could probably with some care handle both volatile and atomic |
| 925 | // stores here but it isn't clear that this is important. |
| 926 | if (!SI.isSimple()) |
| 927 | return false; |
| 928 | |
| 929 | Value *V = SI.getValueOperand(); |
| 930 | Type *T = V->getType(); |
| 931 | |
| 932 | if (!T->isAggregateType()) |
| 933 | return false; |
| 934 | |
Mehdi Amini | 2668a48 | 2015-05-07 05:52:40 +0000 | [diff] [blame] | 935 | if (auto *ST = dyn_cast<StructType>(T)) { |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 936 | // If the struct only have one element, we unpack. |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 937 | unsigned Count = ST->getNumElements(); |
| 938 | if (Count == 1) { |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 939 | V = IC.Builder->CreateExtractValue(V, 0); |
| 940 | combineStoreToNewValue(IC, SI, V); |
| 941 | return true; |
| 942 | } |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 943 | |
| 944 | // We don't want to break loads with padding here as we'd loose |
| 945 | // the knowledge that padding exists for the rest of the pipeline. |
| 946 | const DataLayout &DL = IC.getDataLayout(); |
| 947 | auto *SL = DL.getStructLayout(ST); |
| 948 | if (SL->hasPadding()) |
| 949 | return false; |
| 950 | |
NAKAMURA Takumi | ec6b1fc | 2015-12-15 09:37:31 +0000 | [diff] [blame] | 951 | SmallString<16> EltName = V->getName(); |
| 952 | EltName += ".elt"; |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 953 | auto *Addr = SI.getPointerOperand(); |
NAKAMURA Takumi | ec6b1fc | 2015-12-15 09:37:31 +0000 | [diff] [blame] | 954 | SmallString<16> AddrName = Addr->getName(); |
| 955 | AddrName += ".repack"; |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 956 | auto *IdxType = Type::getInt32Ty(ST->getContext()); |
| 957 | auto *Zero = ConstantInt::get(IdxType, 0); |
| 958 | for (unsigned i = 0; i < Count; i++) { |
| 959 | Value *Indices[2] = { |
| 960 | Zero, |
| 961 | ConstantInt::get(IdxType, i), |
| 962 | }; |
| 963 | auto *Ptr = IC.Builder->CreateInBoundsGEP(ST, Addr, makeArrayRef(Indices), AddrName); |
| 964 | auto *Val = IC.Builder->CreateExtractValue(V, i, EltName); |
| 965 | IC.Builder->CreateStore(Val, Ptr); |
| 966 | } |
| 967 | |
| 968 | return true; |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 969 | } |
| 970 | |
David Majnemer | 7536460 | 2015-05-11 05:04:27 +0000 | [diff] [blame] | 971 | if (auto *AT = dyn_cast<ArrayType>(T)) { |
| 972 | // If the array only have one element, we unpack. |
| 973 | if (AT->getNumElements() == 1) { |
| 974 | V = IC.Builder->CreateExtractValue(V, 0); |
| 975 | combineStoreToNewValue(IC, SI, V); |
| 976 | return true; |
| 977 | } |
| 978 | } |
| 979 | |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 980 | return false; |
| 981 | } |
| 982 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 983 | /// equivalentAddressValues - Test if A and B will obviously have the same |
| 984 | /// value. This includes recognizing that %t0 and %t1 will have the same |
| 985 | /// value in code like this: |
| 986 | /// %t0 = getelementptr \@a, 0, 3 |
| 987 | /// store i32 0, i32* %t0 |
| 988 | /// %t1 = getelementptr \@a, 0, 3 |
| 989 | /// %t2 = load i32* %t1 |
| 990 | /// |
| 991 | static bool equivalentAddressValues(Value *A, Value *B) { |
| 992 | // Test if the values are trivially equivalent. |
| 993 | if (A == B) return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 994 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 995 | // Test if the values come form identical arithmetic instructions. |
| 996 | // This uses isIdenticalToWhenDefined instead of isIdenticalTo because |
| 997 | // its only used to compare two uses within the same basic block, which |
| 998 | // means that they'll always either have the same value or one of them |
| 999 | // will have an undefined value. |
| 1000 | if (isa<BinaryOperator>(A) || |
| 1001 | isa<CastInst>(A) || |
| 1002 | isa<PHINode>(A) || |
| 1003 | isa<GetElementPtrInst>(A)) |
| 1004 | if (Instruction *BI = dyn_cast<Instruction>(B)) |
| 1005 | if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) |
| 1006 | return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1007 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1008 | // Otherwise they may not be equivalent. |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1012 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 1013 | Value *Val = SI.getOperand(0); |
| 1014 | Value *Ptr = SI.getOperand(1); |
| 1015 | |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 1016 | // Try to canonicalize the stored type. |
| 1017 | if (combineStoreToValueType(*this, SI)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1018 | return eraseInstFromFunction(SI); |
Chandler Carruth | 816d26f | 2014-11-25 10:09:51 +0000 | [diff] [blame] | 1019 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1020 | // Attempt to improve the alignment. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1021 | unsigned KnownAlign = getOrEnforceKnownAlignment( |
| 1022 | Ptr, DL.getPrefTypeAlignment(Val->getType()), DL, &SI, AC, DT); |
| 1023 | unsigned StoreAlign = SI.getAlignment(); |
| 1024 | unsigned EffectiveStoreAlign = |
| 1025 | StoreAlign != 0 ? StoreAlign : DL.getABITypeAlignment(Val->getType()); |
Dan Gohman | 3619660 | 2010-08-03 18:20:32 +0000 | [diff] [blame] | 1026 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1027 | if (KnownAlign > EffectiveStoreAlign) |
| 1028 | SI.setAlignment(KnownAlign); |
| 1029 | else if (StoreAlign == 0) |
| 1030 | SI.setAlignment(EffectiveStoreAlign); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1031 | |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 1032 | // Try to canonicalize the stored type. |
| 1033 | if (unpackStoreToAggregate(*this, SI)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1034 | return eraseInstFromFunction(SI); |
Mehdi Amini | b344ac9 | 2015-03-14 22:19:33 +0000 | [diff] [blame] | 1035 | |
Hal Finkel | 847e05f | 2015-02-20 03:05:53 +0000 | [diff] [blame] | 1036 | // Replace GEP indices if possible. |
| 1037 | if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Ptr, SI)) { |
| 1038 | Worklist.Add(NewGEPI); |
| 1039 | return &SI; |
| 1040 | } |
| 1041 | |
Philip Reames | d7a6cc8 | 2015-12-17 22:19:27 +0000 | [diff] [blame] | 1042 | // Don't hack volatile/ordered stores. |
| 1043 | // FIXME: Some bits are legal for ordered atomic stores; needs refactoring. |
| 1044 | if (!SI.isUnordered()) return nullptr; |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1045 | |
| 1046 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 1047 | // alloca dead. |
| 1048 | if (Ptr->hasOneUse()) { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1049 | if (isa<AllocaInst>(Ptr)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1050 | return eraseInstFromFunction(SI); |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1051 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 1052 | if (isa<AllocaInst>(GEP->getOperand(0))) { |
| 1053 | if (GEP->getOperand(0)->hasOneUse()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1054 | return eraseInstFromFunction(SI); |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1059 | // Do really simple DSE, to catch cases where there are several consecutive |
| 1060 | // stores to the same location, separated by a few arithmetic operations. This |
| 1061 | // situation often occurs with bitfield accesses. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 1062 | BasicBlock::iterator BBI(SI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1063 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 1064 | --ScanInsts) { |
| 1065 | --BBI; |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 1066 | // Don't count debug info directives, lest they affect codegen, |
| 1067 | // and we skip pointer-to-pointer bitcasts, which are NOPs. |
| 1068 | if (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1069 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1070 | ScanInsts++; |
| 1071 | continue; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1074 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 1075 | // Prev store isn't volatile, and stores to the same location? |
Philip Reames | d7a6cc8 | 2015-12-17 22:19:27 +0000 | [diff] [blame] | 1076 | if (PrevSI->isUnordered() && equivalentAddressValues(PrevSI->getOperand(1), |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1077 | SI.getOperand(1))) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1078 | ++NumDeadStore; |
| 1079 | ++BBI; |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1080 | eraseInstFromFunction(*PrevSI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1081 | continue; |
| 1082 | } |
| 1083 | break; |
| 1084 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1085 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1086 | // If this is a load, we have to stop. However, if the loaded value is from |
| 1087 | // the pointer we're loading and is producing the pointer we're storing, |
| 1088 | // then *this* store is dead (X = load P; store X -> P). |
| 1089 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
Philip Reames | d7a6cc8 | 2015-12-17 22:19:27 +0000 | [diff] [blame] | 1090 | if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr)) { |
| 1091 | assert(SI.isUnordered() && "can't eliminate ordering operation"); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1092 | return eraseInstFromFunction(SI); |
Philip Reames | d7a6cc8 | 2015-12-17 22:19:27 +0000 | [diff] [blame] | 1093 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1094 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1095 | // Otherwise, this is a load from some other location. Stores before it |
| 1096 | // may not be dead. |
| 1097 | break; |
| 1098 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1100 | // Don't skip over loads or things that can modify memory. |
| 1101 | if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory()) |
| 1102 | break; |
| 1103 | } |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1104 | |
| 1105 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| 1106 | if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) { |
| 1107 | if (!isa<UndefValue>(Val)) { |
| 1108 | SI.setOperand(0, UndefValue::get(Val->getType())); |
| 1109 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 1110 | Worklist.Add(U); // Dropped a use. |
| 1111 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1112 | return nullptr; // Do not modify these! |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | // store undef, Ptr -> noop |
| 1116 | if (isa<UndefValue>(Val)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1117 | return eraseInstFromFunction(SI); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1118 | |
Philip Reames | d7a6cc8 | 2015-12-17 22:19:27 +0000 | [diff] [blame] | 1119 | // The code below needs to be audited and adjusted for unordered atomics |
| 1120 | if (!SI.isSimple()) |
| 1121 | return nullptr; |
| 1122 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1123 | // If this store is the last instruction in the basic block (possibly |
Victor Hernandez | 5f5abd5 | 2010-01-21 23:07:15 +0000 | [diff] [blame] | 1124 | // excepting debug info instructions), and if the block ends with an |
| 1125 | // unconditional branch, try to move it to the successor block. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 1126 | BBI = SI.getIterator(); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1127 | do { |
| 1128 | ++BBI; |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 1129 | } while (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1130 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1131 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 1132 | if (BI->isUnconditional()) |
| 1133 | if (SimplifyStoreAtEndOfBlock(SI)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1134 | return nullptr; // xform done! |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1135 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1136 | return nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 1140 | /// if () { *P = v1; } else { *P = v2 } |
| 1141 | /// into a phi node with a store in the successor. |
| 1142 | /// |
| 1143 | /// Simplify things like: |
| 1144 | /// *P = v1; if () { *P = v2; } |
| 1145 | /// into a phi node with a store in the successor. |
| 1146 | /// |
| 1147 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 1148 | BasicBlock *StoreBB = SI.getParent(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1149 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1150 | // Check to see if the successor block has exactly two incoming edges. If |
| 1151 | // so, see if the other predecessor contains a store to the same location. |
| 1152 | // if so, insert a PHI node (if needed) and move the stores down. |
| 1153 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1154 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1155 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 1156 | // the other predecessor. |
| 1157 | pred_iterator PI = pred_begin(DestBB); |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 1158 | BasicBlock *P = *PI; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1159 | BasicBlock *OtherBB = nullptr; |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 1160 | |
| 1161 | if (P != StoreBB) |
| 1162 | OtherBB = P; |
| 1163 | |
| 1164 | if (++PI == pred_end(DestBB)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1165 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1166 | |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 1167 | P = *PI; |
| 1168 | if (P != StoreBB) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1169 | if (OtherBB) |
| 1170 | return false; |
Gabor Greif | 1b787df | 2010-07-12 15:48:26 +0000 | [diff] [blame] | 1171 | OtherBB = P; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1172 | } |
| 1173 | if (++PI != pred_end(DestBB)) |
| 1174 | return false; |
| 1175 | |
| 1176 | // Bail out if all the relevant blocks aren't distinct (this can happen, |
| 1177 | // for example, if SI is in an infinite loop) |
| 1178 | if (StoreBB == DestBB || OtherBB == DestBB) |
| 1179 | return false; |
| 1180 | |
| 1181 | // Verify that the other block ends in a branch and is not otherwise empty. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 1182 | BasicBlock::iterator BBI(OtherBB->getTerminator()); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1183 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
| 1184 | if (!OtherBr || BBI == OtherBB->begin()) |
| 1185 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1186 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1187 | // If the other block ends in an unconditional branch, check for the 'if then |
| 1188 | // else' case. there is an instruction before the branch. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1189 | StoreInst *OtherStore = nullptr; |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1190 | if (OtherBr->isUnconditional()) { |
| 1191 | --BBI; |
| 1192 | // Skip over debugging info. |
Victor Hernandez | 5f8c8c0 | 2010-01-22 19:05:05 +0000 | [diff] [blame] | 1193 | while (isa<DbgInfoIntrinsic>(BBI) || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1194 | (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1195 | if (BBI==OtherBB->begin()) |
| 1196 | return false; |
| 1197 | --BBI; |
| 1198 | } |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1199 | // If this isn't a store, isn't a store to the same location, or is not the |
| 1200 | // right kind of store, bail out. |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1201 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 1202 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) || |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1203 | !SI.isSameOperationAs(OtherStore)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1204 | return false; |
| 1205 | } else { |
| 1206 | // Otherwise, the other block ended with a conditional branch. If one of the |
| 1207 | // destinations is StoreBB, then we have the if/then case. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1208 | if (OtherBr->getSuccessor(0) != StoreBB && |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1209 | OtherBr->getSuccessor(1) != StoreBB) |
| 1210 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1212 | // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an |
| 1213 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 1214 | // lives in OtherBB. |
| 1215 | for (;; --BBI) { |
| 1216 | // Check to see if we find the matching store. |
| 1217 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 1218 | if (OtherStore->getOperand(1) != SI.getOperand(1) || |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1219 | !SI.isSameOperationAs(OtherStore)) |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1220 | return false; |
| 1221 | break; |
| 1222 | } |
| 1223 | // If we find something that may be using or overwriting the stored |
| 1224 | // value, or if we run out of instructions, we can't do the xform. |
| 1225 | if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() || |
| 1226 | BBI == OtherBB->begin()) |
| 1227 | return false; |
| 1228 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1229 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1230 | // In order to eliminate the store in OtherBr, we have to |
| 1231 | // make sure nothing reads or overwrites the stored value in |
| 1232 | // StoreBB. |
| 1233 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 1234 | // FIXME: This should really be AA driven. |
| 1235 | if (I->mayReadFromMemory() || I->mayWriteToMemory()) |
| 1236 | return false; |
| 1237 | } |
| 1238 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1239 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1240 | // Insert a PHI node now if we need it. |
| 1241 | Value *MergedVal = OtherStore->getOperand(0); |
| 1242 | if (MergedVal != SI.getOperand(0)) { |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1243 | PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge"); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1244 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 1245 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 1246 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
| 1247 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1248 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1249 | // Advance to a place where it is safe to insert the new store and |
| 1250 | // insert it. |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 1251 | BBI = DestBB->getFirstInsertionPt(); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 1252 | StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1), |
Eli Friedman | 8bc586e | 2011-08-15 22:09:40 +0000 | [diff] [blame] | 1253 | SI.isVolatile(), |
| 1254 | SI.getAlignment(), |
| 1255 | SI.getOrdering(), |
| 1256 | SI.getSynchScope()); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 1257 | InsertNewInstBefore(NewSI, *BBI); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1258 | NewSI->setDebugLoc(OtherStore->getDebugLoc()); |
Eli Friedman | 35211c6 | 2011-05-27 00:19:40 +0000 | [diff] [blame] | 1259 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1260 | // If the two stores had AA tags, merge them. |
| 1261 | AAMDNodes AATags; |
| 1262 | SI.getAAMetadata(AATags); |
| 1263 | if (AATags) { |
| 1264 | OtherStore->getAAMetadata(AATags, /* Merge = */ true); |
| 1265 | NewSI->setAAMetadata(AATags); |
| 1266 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1268 | // Nuke the old stores. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1269 | eraseInstFromFunction(SI); |
| 1270 | eraseInstFromFunction(*OtherStore); |
Chris Lattner | a65e2f7 | 2010-01-05 05:57:49 +0000 | [diff] [blame] | 1271 | return true; |
| 1272 | } |