Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 1 | //===- MergedLoadStoreMotion.cpp - merge and hoist/sink load/stores -------===// |
| 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 | //! \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | //! This pass performs merges of loads and stores on both sides of a |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 12 | // diamond (hammock). It hoists the loads and sinks the stores. |
| 13 | // |
| 14 | // The algorithm iteratively hoists two loads to the same address out of a |
| 15 | // diamond (hammock) and merges them into a single load in the header. Similar |
| 16 | // it sinks and merges two stores to the tail block (footer). The algorithm |
| 17 | // iterates over the instructions of one side of the diamond and attempts to |
| 18 | // find a matching load/store on the other side. It hoists / sinks when it |
| 19 | // thinks it safe to do so. This optimization helps with eg. hiding load |
| 20 | // latencies, triggering if-conversion, and reducing static code size. |
| 21 | // |
Daniel Berlin | 390dfde | 2017-01-24 19:55:36 +0000 | [diff] [blame] | 22 | // NOTE: This code no longer performs load hoisting, it is subsumed by GVNHoist. |
| 23 | // |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // |
| 26 | // |
| 27 | // Example: |
| 28 | // Diamond shaped code before merge: |
| 29 | // |
| 30 | // header: |
| 31 | // br %cond, label %if.then, label %if.else |
Gerolf Hoflehner | ea96a3d | 2014-08-07 23:19:55 +0000 | [diff] [blame] | 32 | // + + |
| 33 | // + + |
| 34 | // + + |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 35 | // if.then: if.else: |
| 36 | // %lt = load %addr_l %le = load %addr_l |
| 37 | // <use %lt> <use %le> |
| 38 | // <...> <...> |
| 39 | // store %st, %addr_s store %se, %addr_s |
| 40 | // br label %if.end br label %if.end |
Gerolf Hoflehner | ea96a3d | 2014-08-07 23:19:55 +0000 | [diff] [blame] | 41 | // + + |
| 42 | // + + |
| 43 | // + + |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 44 | // if.end ("footer"): |
| 45 | // <...> |
| 46 | // |
| 47 | // Diamond shaped code after merge: |
| 48 | // |
| 49 | // header: |
| 50 | // %l = load %addr_l |
| 51 | // br %cond, label %if.then, label %if.else |
Gerolf Hoflehner | ea96a3d | 2014-08-07 23:19:55 +0000 | [diff] [blame] | 52 | // + + |
| 53 | // + + |
| 54 | // + + |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 55 | // if.then: if.else: |
| 56 | // <use %l> <use %l> |
| 57 | // <...> <...> |
| 58 | // br label %if.end br label %if.end |
Gerolf Hoflehner | ea96a3d | 2014-08-07 23:19:55 +0000 | [diff] [blame] | 59 | // + + |
| 60 | // + + |
| 61 | // + + |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 62 | // if.end ("footer"): |
| 63 | // %s.sink = phi [%st, if.then], [%se, if.else] |
| 64 | // <...> |
| 65 | // store %s.sink, %addr_s |
| 66 | // <...> |
| 67 | // |
| 68 | // |
| 69 | //===----------------------- TODO -----------------------------------------===// |
| 70 | // |
| 71 | // 1) Generalize to regions other than diamonds |
| 72 | // 2) Be more aggressive merging memory operations |
| 73 | // Note that both changes require register pressure control |
| 74 | // |
| 75 | //===----------------------------------------------------------------------===// |
| 76 | |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 77 | #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h" |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 78 | #include "llvm/ADT/Statistic.h" |
| 79 | #include "llvm/Analysis/AliasAnalysis.h" |
| 80 | #include "llvm/Analysis/CFG.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 81 | #include "llvm/Analysis/GlobalsModRef.h" |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 82 | #include "llvm/Analysis/Loads.h" |
Eli Friedman | 9f8031c | 2016-06-12 02:11:20 +0000 | [diff] [blame] | 83 | #include "llvm/Analysis/ValueTracking.h" |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 84 | #include "llvm/IR/Metadata.h" |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 85 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | b85d375 | 2015-03-23 18:45:56 +0000 | [diff] [blame] | 86 | #include "llvm/Support/raw_ostream.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 87 | #include "llvm/Transforms/Scalar.h" |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 88 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 89 | |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 90 | using namespace llvm; |
| 91 | |
| 92 | #define DEBUG_TYPE "mldst-motion" |
| 93 | |
Benjamin Kramer | 4d09892 | 2016-07-10 11:28:51 +0000 | [diff] [blame] | 94 | namespace { |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 95 | //===----------------------------------------------------------------------===// |
| 96 | // MergedLoadStoreMotion Pass |
| 97 | //===----------------------------------------------------------------------===// |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 98 | class MergedLoadStoreMotion { |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 99 | AliasAnalysis *AA = nullptr; |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 100 | |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 101 | // The mergeLoad/Store algorithms could have Size0 * Size1 complexity, |
| 102 | // where Size0 and Size1 are the #instructions on the two sides of |
| 103 | // the diamond. The constant chosen here is arbitrary. Compiler Time |
| 104 | // Control is enforced by the check Size0 * Size1 < MagicCompileTimeControl. |
| 105 | const int MagicCompileTimeControl = 250; |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 106 | |
| 107 | public: |
Bjorn Steinbrink | 983d6c3 | 2018-02-23 10:41:57 +0000 | [diff] [blame] | 108 | bool run(Function &F, AliasAnalysis &AA); |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 109 | |
| 110 | private: |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 111 | BasicBlock *getDiamondTail(BasicBlock *BB); |
| 112 | bool isDiamondHead(BasicBlock *BB); |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 113 | // Routines for sinking stores |
| 114 | StoreInst *canSinkFromBlock(BasicBlock *BB, StoreInst *SI); |
| 115 | PHINode *getPHIOperand(BasicBlock *BB, StoreInst *S0, StoreInst *S1); |
| 116 | bool isStoreSinkBarrierInRange(const Instruction &Start, |
| 117 | const Instruction &End, MemoryLocation Loc); |
| 118 | bool sinkStore(BasicBlock *BB, StoreInst *SinkCand, StoreInst *ElseInst); |
| 119 | bool mergeStores(BasicBlock *BB); |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 120 | }; |
Benjamin Kramer | 4d09892 | 2016-07-10 11:28:51 +0000 | [diff] [blame] | 121 | } // end anonymous namespace |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 122 | |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 123 | /// |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 124 | /// Return tail block of a diamond. |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 125 | /// |
| 126 | BasicBlock *MergedLoadStoreMotion::getDiamondTail(BasicBlock *BB) { |
| 127 | assert(isDiamondHead(BB) && "Basic block is not head of a diamond"); |
| 128 | return BB->getTerminator()->getSuccessor(0)->getSingleSuccessor(); |
| 129 | } |
| 130 | |
| 131 | /// |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 132 | /// True when BB is the head of a diamond (hammock) |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 133 | /// |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 134 | bool MergedLoadStoreMotion::isDiamondHead(BasicBlock *BB) { |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 135 | if (!BB) |
| 136 | return false; |
David Majnemer | 8cce333 | 2016-05-26 05:43:12 +0000 | [diff] [blame] | 137 | auto *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 138 | if (!BI || !BI->isConditional()) |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 139 | return false; |
| 140 | |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 141 | BasicBlock *Succ0 = BI->getSuccessor(0); |
| 142 | BasicBlock *Succ1 = BI->getSuccessor(1); |
| 143 | |
David Majnemer | 8cce333 | 2016-05-26 05:43:12 +0000 | [diff] [blame] | 144 | if (!Succ0->getSinglePredecessor()) |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 145 | return false; |
David Majnemer | 8cce333 | 2016-05-26 05:43:12 +0000 | [diff] [blame] | 146 | if (!Succ1->getSinglePredecessor()) |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 147 | return false; |
| 148 | |
David Majnemer | 8cce333 | 2016-05-26 05:43:12 +0000 | [diff] [blame] | 149 | BasicBlock *Succ0Succ = Succ0->getSingleSuccessor(); |
| 150 | BasicBlock *Succ1Succ = Succ1->getSingleSuccessor(); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 151 | // Ignore triangles. |
David Majnemer | 8cce333 | 2016-05-26 05:43:12 +0000 | [diff] [blame] | 152 | if (!Succ0Succ || !Succ1Succ || Succ0Succ != Succ1Succ) |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 153 | return false; |
| 154 | return true; |
| 155 | } |
| 156 | |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 157 | |
| 158 | /// |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 159 | /// True when instruction is a sink barrier for a store |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 160 | /// located in Loc |
| 161 | /// |
| 162 | /// Whenever an instruction could possibly read or modify the |
| 163 | /// value being stored or protect against the store from |
| 164 | /// happening it is considered a sink barrier. |
| 165 | /// |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 166 | bool MergedLoadStoreMotion::isStoreSinkBarrierInRange(const Instruction &Start, |
| 167 | const Instruction &End, |
| 168 | MemoryLocation Loc) { |
David Majnemer | 4745125 | 2016-05-26 07:11:09 +0000 | [diff] [blame] | 169 | for (const Instruction &Inst : |
| 170 | make_range(Start.getIterator(), End.getIterator())) |
| 171 | if (Inst.mayThrow()) |
| 172 | return true; |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 173 | return AA->canInstructionRangeModRef(Start, End, Loc, ModRefInfo::ModRef); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /// |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 177 | /// Check if \p BB contains a store to the same address as \p SI |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 178 | /// |
| 179 | /// \return The store in \p when it is safe to sink. Otherwise return Null. |
| 180 | /// |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 181 | StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1, |
| 182 | StoreInst *Store0) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 183 | LLVM_DEBUG(dbgs() << "can Sink? : "; Store0->dump(); dbgs() << "\n"); |
Elena Demikhovsky | ef035bb | 2015-02-17 13:10:05 +0000 | [diff] [blame] | 184 | BasicBlock *BB0 = Store0->getParent(); |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 185 | for (Instruction &Inst : reverse(*BB1)) { |
| 186 | auto *Store1 = dyn_cast<StoreInst>(&Inst); |
David Majnemer | 4745125 | 2016-05-26 07:11:09 +0000 | [diff] [blame] | 187 | if (!Store1) |
| 188 | continue; |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 189 | |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 190 | MemoryLocation Loc0 = MemoryLocation::get(Store0); |
| 191 | MemoryLocation Loc1 = MemoryLocation::get(Store1); |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 192 | if (AA->isMustAlias(Loc0, Loc1) && Store0->isSameOperationAs(Store1) && |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 193 | !isStoreSinkBarrierInRange(*Store1->getNextNode(), BB1->back(), Loc1) && |
| 194 | !isStoreSinkBarrierInRange(*Store0->getNextNode(), BB0->back(), Loc0)) { |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 195 | return Store1; |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 198 | return nullptr; |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /// |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 202 | /// Create a PHI node in BB for the operands of S0 and S1 |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 203 | /// |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 204 | PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0, |
| 205 | StoreInst *S1) { |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 206 | // Create a phi if the values mismatch. |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 207 | Value *Opd1 = S0->getValueOperand(); |
| 208 | Value *Opd2 = S1->getValueOperand(); |
David Majnemer | 8cce333 | 2016-05-26 05:43:12 +0000 | [diff] [blame] | 209 | if (Opd1 == Opd2) |
| 210 | return nullptr; |
| 211 | |
| 212 | auto *NewPN = PHINode::Create(Opd1->getType(), 2, Opd2->getName() + ".sink", |
| 213 | &BB->front()); |
| 214 | NewPN->addIncoming(Opd1, S0->getParent()); |
| 215 | NewPN->addIncoming(Opd2, S1->getParent()); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 216 | return NewPN; |
| 217 | } |
| 218 | |
| 219 | /// |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 220 | /// Merge two stores to same address and sink into \p BB |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 221 | /// |
| 222 | /// Also sinks GEP instruction computing the store address |
| 223 | /// |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 224 | bool MergedLoadStoreMotion::sinkStore(BasicBlock *BB, StoreInst *S0, |
| 225 | StoreInst *S1) { |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 226 | // Only one definition? |
David Majnemer | 8cce333 | 2016-05-26 05:43:12 +0000 | [diff] [blame] | 227 | auto *A0 = dyn_cast<Instruction>(S0->getPointerOperand()); |
| 228 | auto *A1 = dyn_cast<Instruction>(S1->getPointerOperand()); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 229 | if (A0 && A1 && A0->isIdenticalTo(A1) && A0->hasOneUse() && |
| 230 | (A0->getParent() == S0->getParent()) && A1->hasOneUse() && |
| 231 | (A1->getParent() == S1->getParent()) && isa<GetElementPtrInst>(A0)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 232 | LLVM_DEBUG(dbgs() << "Sink Instruction into BB \n"; BB->dump(); |
| 233 | dbgs() << "Instruction Left\n"; S0->dump(); dbgs() << "\n"; |
| 234 | dbgs() << "Instruction Right\n"; S1->dump(); dbgs() << "\n"); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 235 | // Hoist the instruction. |
| 236 | BasicBlock::iterator InsertPt = BB->getFirstInsertionPt(); |
| 237 | // Intersect optional metadata. |
Peter Collingbourne | 8f1dd5c | 2016-09-07 23:39:04 +0000 | [diff] [blame] | 238 | S0->andIRFlags(S1); |
Adrian Prantl | cbdfdb7 | 2015-08-20 22:00:30 +0000 | [diff] [blame] | 239 | S0->dropUnknownNonDebugMetadata(); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 240 | |
| 241 | // Create the new store to be inserted at the join point. |
David Majnemer | 8cce333 | 2016-05-26 05:43:12 +0000 | [diff] [blame] | 242 | StoreInst *SNew = cast<StoreInst>(S0->clone()); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 243 | Instruction *ANew = A0->clone(); |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 244 | SNew->insertBefore(&*InsertPt); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 245 | ANew->insertBefore(SNew); |
| 246 | |
| 247 | assert(S0->getParent() == A0->getParent()); |
| 248 | assert(S1->getParent() == A1->getParent()); |
| 249 | |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 250 | // New PHI operand? Use it. |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 251 | if (PHINode *NewPN = getPHIOperand(BB, S0, S1)) |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 252 | SNew->setOperand(0, NewPN); |
Bjorn Steinbrink | 983d6c3 | 2018-02-23 10:41:57 +0000 | [diff] [blame] | 253 | S0->eraseFromParent(); |
| 254 | S1->eraseFromParent(); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 255 | A0->replaceAllUsesWith(ANew); |
Bjorn Steinbrink | 983d6c3 | 2018-02-23 10:41:57 +0000 | [diff] [blame] | 256 | A0->eraseFromParent(); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 257 | A1->replaceAllUsesWith(ANew); |
Bjorn Steinbrink | 983d6c3 | 2018-02-23 10:41:57 +0000 | [diff] [blame] | 258 | A1->eraseFromParent(); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 259 | return true; |
| 260 | } |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | /// |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 265 | /// True when two stores are equivalent and can sink into the footer |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 266 | /// |
| 267 | /// Starting from a diamond tail block, iterate over the instructions in one |
| 268 | /// predecessor block and try to match a store in the second predecessor. |
| 269 | /// |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 270 | bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) { |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 271 | |
| 272 | bool MergedStores = false; |
| 273 | assert(T && "Footer of a diamond cannot be empty"); |
| 274 | |
| 275 | pred_iterator PI = pred_begin(T), E = pred_end(T); |
| 276 | assert(PI != E); |
| 277 | BasicBlock *Pred0 = *PI; |
| 278 | ++PI; |
| 279 | BasicBlock *Pred1 = *PI; |
| 280 | ++PI; |
| 281 | // tail block of a diamond/hammock? |
| 282 | if (Pred0 == Pred1) |
| 283 | return false; // No. |
| 284 | if (PI != E) |
| 285 | return false; // No. More than 2 predecessors. |
| 286 | |
| 287 | // #Instructions in Succ1 for Compile Time Control |
Vedant Kumar | 5a0872c | 2018-05-16 23:20:42 +0000 | [diff] [blame] | 288 | auto InstsNoDbg = Pred1->instructionsWithoutDebug(); |
| 289 | int Size1 = std::distance(InstsNoDbg.begin(), InstsNoDbg.end()); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 290 | int NStores = 0; |
| 291 | |
| 292 | for (BasicBlock::reverse_iterator RBI = Pred0->rbegin(), RBE = Pred0->rend(); |
| 293 | RBI != RBE;) { |
| 294 | |
| 295 | Instruction *I = &*RBI; |
| 296 | ++RBI; |
Elena Demikhovsky | a5599bf | 2014-12-15 14:09:53 +0000 | [diff] [blame] | 297 | |
David Majnemer | 8cce333 | 2016-05-26 05:43:12 +0000 | [diff] [blame] | 298 | // Don't sink non-simple (atomic, volatile) stores. |
| 299 | auto *S0 = dyn_cast<StoreInst>(I); |
| 300 | if (!S0 || !S0->isSimple()) |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 301 | continue; |
| 302 | |
| 303 | ++NStores; |
| 304 | if (NStores * Size1 >= MagicCompileTimeControl) |
| 305 | break; |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 306 | if (StoreInst *S1 = canSinkFromBlock(Pred1, S0)) { |
| 307 | bool Res = sinkStore(T, S0, S1); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 308 | MergedStores |= Res; |
| 309 | // Don't attempt to sink below stores that had to stick around |
| 310 | // But after removal of a store and some of its feeding |
| 311 | // instruction search again from the beginning since the iterator |
| 312 | // is likely stale at this point. |
| 313 | if (!Res) |
| 314 | break; |
David Majnemer | 8cce333 | 2016-05-26 05:43:12 +0000 | [diff] [blame] | 315 | RBI = Pred0->rbegin(); |
| 316 | RBE = Pred0->rend(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 317 | LLVM_DEBUG(dbgs() << "Search again\n"; Instruction *I = &*RBI; I->dump()); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | return MergedStores; |
| 321 | } |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 322 | |
Bjorn Steinbrink | 983d6c3 | 2018-02-23 10:41:57 +0000 | [diff] [blame] | 323 | bool MergedLoadStoreMotion::run(Function &F, AliasAnalysis &AA) { |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 324 | this->AA = &AA; |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 325 | |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 326 | bool Changed = false; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 327 | LLVM_DEBUG(dbgs() << "Instruction Merger\n"); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 328 | |
| 329 | // Merge unconditional branches, allowing PRE to catch more |
| 330 | // optimization opportunities. |
| 331 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE;) { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 332 | BasicBlock *BB = &*FI++; |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 333 | |
| 334 | // Hoist equivalent loads and sink stores |
| 335 | // outside diamonds when possible |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 336 | if (isDiamondHead(BB)) { |
Davide Italiano | 41315f7 | 2016-06-16 17:40:53 +0000 | [diff] [blame] | 337 | Changed |= mergeStores(getDiamondTail(BB)); |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | return Changed; |
| 341 | } |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 342 | |
| 343 | namespace { |
| 344 | class MergedLoadStoreMotionLegacyPass : public FunctionPass { |
| 345 | public: |
| 346 | static char ID; // Pass identification, replacement for typeid |
| 347 | MergedLoadStoreMotionLegacyPass() : FunctionPass(ID) { |
| 348 | initializeMergedLoadStoreMotionLegacyPassPass( |
| 349 | *PassRegistry::getPassRegistry()); |
| 350 | } |
| 351 | |
| 352 | /// |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 353 | /// Run the transformation for each function |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 354 | /// |
| 355 | bool runOnFunction(Function &F) override { |
| 356 | if (skipFunction(F)) |
| 357 | return false; |
| 358 | MergedLoadStoreMotion Impl; |
Bjorn Steinbrink | 983d6c3 | 2018-02-23 10:41:57 +0000 | [diff] [blame] | 359 | return Impl.run(F, getAnalysis<AAResultsWrapperPass>().getAAResults()); |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | private: |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 363 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 364 | AU.setPreservesCFG(); |
| 365 | AU.addRequired<AAResultsWrapperPass>(); |
| 366 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 367 | } |
| 368 | }; |
| 369 | |
| 370 | char MergedLoadStoreMotionLegacyPass::ID = 0; |
| 371 | } // anonymous namespace |
| 372 | |
| 373 | /// |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 374 | /// createMergedLoadStoreMotionPass - The public interface to this file. |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 375 | /// |
| 376 | FunctionPass *llvm::createMergedLoadStoreMotionPass() { |
| 377 | return new MergedLoadStoreMotionLegacyPass(); |
| 378 | } |
| 379 | |
| 380 | INITIALIZE_PASS_BEGIN(MergedLoadStoreMotionLegacyPass, "mldst-motion", |
| 381 | "MergedLoadStoreMotion", false, false) |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 382 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 383 | INITIALIZE_PASS_END(MergedLoadStoreMotionLegacyPass, "mldst-motion", |
| 384 | "MergedLoadStoreMotion", false, false) |
| 385 | |
| 386 | PreservedAnalyses |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 387 | MergedLoadStoreMotionPass::run(Function &F, FunctionAnalysisManager &AM) { |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 388 | MergedLoadStoreMotion Impl; |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 389 | auto &AA = AM.getResult<AAManager>(F); |
Bjorn Steinbrink | 983d6c3 | 2018-02-23 10:41:57 +0000 | [diff] [blame] | 390 | if (!Impl.run(F, AA)) |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 391 | return PreservedAnalyses::all(); |
| 392 | |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 393 | PreservedAnalyses PA; |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 394 | PA.preserveSet<CFGAnalyses>(); |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 395 | PA.preserve<GlobalsAA>(); |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 396 | return PA; |
| 397 | } |