Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 1 | //===-- Sink.cpp - Code Sinking -------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass moves instructions into successor blocks, when possible, so that |
| 10 | // they aren't executed on paths where their results aren't needed. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/Scalar/Sink.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
| 16 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopInfo.h" |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 19 | #include "llvm/IR/CFG.h" |
Hal Finkel | 511fea7 | 2014-07-10 16:07:11 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/IntrinsicInst.h" |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Scalar.h" |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "sink" |
| 30 | |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 31 | STATISTIC(NumSunk, "Number of instructions sunk"); |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 32 | STATISTIC(NumSinkIter, "Number of sinking iterations"); |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 33 | |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 34 | /// AllUsesDominatedByBlock - Return true if all uses of the specified value |
| 35 | /// occur in blocks dominated by the specified block. |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 36 | static bool AllUsesDominatedByBlock(Instruction *Inst, BasicBlock *BB, |
| 37 | DominatorTree &DT) { |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 38 | // Ignoring debug uses is necessary so debug info doesn't affect the code. |
| 39 | // This may leave a referencing dbg_value in the original block, before |
| 40 | // the definition of the vreg. Dwarf generator handles this although the |
| 41 | // user might not get the right info at runtime. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 42 | for (Use &U : Inst->uses()) { |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 43 | // Determine the block of the use. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 44 | Instruction *UseInst = cast<Instruction>(U.getUser()); |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 45 | BasicBlock *UseBlock = UseInst->getParent(); |
| 46 | if (PHINode *PN = dyn_cast<PHINode>(UseInst)) { |
| 47 | // PHI nodes use the operand in the predecessor block, not the block with |
| 48 | // the PHI. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 49 | unsigned Num = PHINode::getIncomingValueNumForOperand(U.getOperandNo()); |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 50 | UseBlock = PN->getIncomingBlock(Num); |
| 51 | } |
| 52 | // Check that it dominates. |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 53 | if (!DT.dominates(BB, UseBlock)) |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | return true; |
| 57 | } |
| 58 | |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 59 | static bool isSafeToMove(Instruction *Inst, AliasAnalysis &AA, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 60 | SmallPtrSetImpl<Instruction *> &Stores) { |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 61 | |
Eli Friedman | 71f5c2f | 2011-09-01 21:21:24 +0000 | [diff] [blame] | 62 | if (Inst->mayWriteToMemory()) { |
| 63 | Stores.insert(Inst); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if (LoadInst *L = dyn_cast<LoadInst>(Inst)) { |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 68 | MemoryLocation Loc = MemoryLocation::get(L); |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 69 | for (Instruction *S : Stores) |
Alina Sbirlea | 63d2250 | 2017-12-05 20:12:23 +0000 | [diff] [blame] | 70 | if (isModSet(AA.getModRefInfo(S, Loc))) |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
Chandler Carruth | 9ae926b | 2018-08-26 09:51:22 +0000 | [diff] [blame] | 74 | if (Inst->isTerminator() || isa<PHINode>(Inst) || Inst->isEHPad() || |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 75 | Inst->mayThrow()) |
Dan Gohman | c3b4ea7 | 2010-11-11 16:20:28 +0000 | [diff] [blame] | 76 | return false; |
| 77 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 78 | if (auto *Call = dyn_cast<CallBase>(Inst)) { |
Nicolai Haehnle | 889a20c | 2016-07-11 14:11:51 +0000 | [diff] [blame] | 79 | // Convergent operations cannot be made control-dependent on additional |
| 80 | // values. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 81 | if (Call->hasFnAttr(Attribute::Convergent)) |
Owen Anderson | 15d1805 | 2015-06-01 17:20:31 +0000 | [diff] [blame] | 82 | return false; |
Nicolai Haehnle | 889a20c | 2016-07-11 14:11:51 +0000 | [diff] [blame] | 83 | |
| 84 | for (Instruction *S : Stores) |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 85 | if (isModSet(AA.getModRefInfo(S, Call))) |
Nicolai Haehnle | 889a20c | 2016-07-11 14:11:51 +0000 | [diff] [blame] | 86 | return false; |
Owen Anderson | 15d1805 | 2015-06-01 17:20:31 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Dan Gohman | c3b4ea7 | 2010-11-11 16:20:28 +0000 | [diff] [blame] | 89 | return true; |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 92 | /// IsAcceptableTarget - Return true if it is possible to sink the instruction |
| 93 | /// in the specified basic block. |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 94 | static bool IsAcceptableTarget(Instruction *Inst, BasicBlock *SuccToSinkTo, |
| 95 | DominatorTree &DT, LoopInfo &LI) { |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 96 | assert(Inst && "Instruction to be sunk is null"); |
| 97 | assert(SuccToSinkTo && "Candidate sink target is null"); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 98 | |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 99 | // It is not possible to sink an instruction into its own block. This can |
| 100 | // happen with loops. |
| 101 | if (Inst->getParent() == SuccToSinkTo) |
| 102 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 103 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 104 | // It's never legal to sink an instruction into a block which terminates in an |
| 105 | // EH-pad. |
Chandler Carruth | 698fbe7 | 2018-08-26 08:56:42 +0000 | [diff] [blame] | 106 | if (SuccToSinkTo->getTerminator()->isExceptionalTerminator()) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 107 | return false; |
| 108 | |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 109 | // If the block has multiple predecessors, this would introduce computation |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 110 | // on different code paths. We could split the critical edge, but for now we |
| 111 | // just punt. |
| 112 | // FIXME: Split critical edges if not backedges. |
| 113 | if (SuccToSinkTo->getUniquePredecessor() != Inst->getParent()) { |
| 114 | // We cannot sink a load across a critical edge - there may be stores in |
| 115 | // other code paths. |
Fiona Glaser | efe6a84 | 2018-01-11 21:28:57 +0000 | [diff] [blame] | 116 | if (Inst->mayReadFromMemory()) |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 117 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 118 | |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 119 | // We don't want to sink across a critical edge if we don't dominate the |
| 120 | // successor. We could be introducing calculations to new code paths. |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 121 | if (!DT.dominates(Inst->getParent(), SuccToSinkTo)) |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 122 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 123 | |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 124 | // Don't sink instructions into a loop. |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 125 | Loop *succ = LI.getLoopFor(SuccToSinkTo); |
| 126 | Loop *cur = LI.getLoopFor(Inst->getParent()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 127 | if (succ != nullptr && succ != cur) |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 128 | return false; |
| 129 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 130 | |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 131 | // Finally, check that all the uses of the instruction are actually |
| 132 | // dominated by the candidate |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 133 | return AllUsesDominatedByBlock(Inst, SuccToSinkTo, DT); |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 136 | /// SinkInstruction - Determine whether it is safe to sink the specified machine |
| 137 | /// instruction out of its current block into a successor. |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 138 | static bool SinkInstruction(Instruction *Inst, |
| 139 | SmallPtrSetImpl<Instruction *> &Stores, |
| 140 | DominatorTree &DT, LoopInfo &LI, AAResults &AA) { |
Tom Stellard | edfd81d | 2014-03-21 15:51:51 +0000 | [diff] [blame] | 141 | |
| 142 | // Don't sink static alloca instructions. CodeGen assumes allocas outside the |
| 143 | // entry block are dynamically sized stack objects. |
| 144 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst)) |
| 145 | if (AI->isStaticAlloca()) |
| 146 | return false; |
| 147 | |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 148 | // Check if it's safe to move the instruction. |
| 149 | if (!isSafeToMove(Inst, AA, Stores)) |
| 150 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 151 | |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 152 | // FIXME: This should include support for sinking instructions within the |
| 153 | // block they are currently in to shorten the live ranges. We often get |
| 154 | // instructions sunk into the top of a large block, but it would be better to |
| 155 | // also sink them down before their first use in the block. This xform has to |
| 156 | // be careful not to *increase* register pressure though, e.g. sinking |
| 157 | // "x = y + z" down if it kills y and z would increase the live ranges of y |
| 158 | // and z and only shrink the live range of x. |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 159 | |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 160 | // SuccToSinkTo - This is the successor to sink this instruction to, once we |
| 161 | // decide. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 162 | BasicBlock *SuccToSinkTo = nullptr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 163 | |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 164 | // Instructions can only be sunk if all their uses are in blocks |
| 165 | // dominated by one of the successors. |
Xin Tong | cbf04d9 | 2017-03-20 00:30:19 +0000 | [diff] [blame] | 166 | // Look at all the dominated blocks and see if we can sink it in one. |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 167 | DomTreeNode *DTN = DT.getNode(Inst->getParent()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 168 | for (DomTreeNode::iterator I = DTN->begin(), E = DTN->end(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 169 | I != E && SuccToSinkTo == nullptr; ++I) { |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 170 | BasicBlock *Candidate = (*I)->getBlock(); |
Xin Tong | cbf04d9 | 2017-03-20 00:30:19 +0000 | [diff] [blame] | 171 | // A node always immediate-dominates its children on the dominator |
| 172 | // tree. |
| 173 | if (IsAcceptableTarget(Inst, Candidate, DT, LI)) |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 174 | SuccToSinkTo = Candidate; |
| 175 | } |
| 176 | |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 177 | // If no suitable postdominator was found, look at all the successors and |
Duncan Sands | 339bb61 | 2012-05-31 08:09:49 +0000 | [diff] [blame] | 178 | // decide which one we should sink to, if any. |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 179 | for (succ_iterator I = succ_begin(Inst->getParent()), |
| 180 | E = succ_end(Inst->getParent()); I != E && !SuccToSinkTo; ++I) { |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 181 | if (IsAcceptableTarget(Inst, *I, DT, LI)) |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 182 | SuccToSinkTo = *I; |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 183 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 184 | |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 185 | // If we couldn't find a block to sink to, ignore this instruction. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 186 | if (!SuccToSinkTo) |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 187 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 188 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 189 | LLVM_DEBUG(dbgs() << "Sink" << *Inst << " ("; |
| 190 | Inst->getParent()->printAsOperand(dbgs(), false); dbgs() << " -> "; |
| 191 | SuccToSinkTo->printAsOperand(dbgs(), false); dbgs() << ")\n"); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 192 | |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 193 | // Move the instruction. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 194 | Inst->moveBefore(&*SuccToSinkTo->getFirstInsertionPt()); |
Dan Gohman | 5d5b8b1 | 2010-05-07 15:40:13 +0000 | [diff] [blame] | 195 | return true; |
| 196 | } |
Justin Bogner | 82077c4 | 2016-04-22 19:54:04 +0000 | [diff] [blame] | 197 | |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 198 | static bool ProcessBlock(BasicBlock &BB, DominatorTree &DT, LoopInfo &LI, |
| 199 | AAResults &AA) { |
Justin Bogner | 82077c4 | 2016-04-22 19:54:04 +0000 | [diff] [blame] | 200 | // Can't sink anything out of a block that has less than two successors. |
| 201 | if (BB.getTerminator()->getNumSuccessors() <= 1) return false; |
| 202 | |
| 203 | // Don't bother sinking code out of unreachable blocks. In addition to being |
| 204 | // unprofitable, it can also lead to infinite looping, because in an |
| 205 | // unreachable loop there may be nowhere to stop. |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 206 | if (!DT.isReachableFromEntry(&BB)) return false; |
Justin Bogner | 82077c4 | 2016-04-22 19:54:04 +0000 | [diff] [blame] | 207 | |
| 208 | bool MadeChange = false; |
| 209 | |
| 210 | // Walk the basic block bottom-up. Remember if we saw a store. |
| 211 | BasicBlock::iterator I = BB.end(); |
| 212 | --I; |
| 213 | bool ProcessedBegin = false; |
| 214 | SmallPtrSet<Instruction *, 8> Stores; |
| 215 | do { |
| 216 | Instruction *Inst = &*I; // The instruction to sink. |
| 217 | |
| 218 | // Predecrement I (if it's not begin) so that it isn't invalidated by |
| 219 | // sinking. |
| 220 | ProcessedBegin = I == BB.begin(); |
| 221 | if (!ProcessedBegin) |
| 222 | --I; |
| 223 | |
| 224 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 225 | continue; |
| 226 | |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 227 | if (SinkInstruction(Inst, Stores, DT, LI, AA)) { |
Justin Bogner | 82077c4 | 2016-04-22 19:54:04 +0000 | [diff] [blame] | 228 | ++NumSunk; |
| 229 | MadeChange = true; |
| 230 | } |
| 231 | |
| 232 | // If we just processed the first instruction in the block, we're done. |
| 233 | } while (!ProcessedBegin); |
| 234 | |
| 235 | return MadeChange; |
| 236 | } |
| 237 | |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 238 | static bool iterativelySinkInstructions(Function &F, DominatorTree &DT, |
| 239 | LoopInfo &LI, AAResults &AA) { |
Justin Bogner | 82077c4 | 2016-04-22 19:54:04 +0000 | [diff] [blame] | 240 | bool MadeChange, EverMadeChange = false; |
| 241 | |
| 242 | do { |
| 243 | MadeChange = false; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 244 | LLVM_DEBUG(dbgs() << "Sinking iteration " << NumSinkIter << "\n"); |
Justin Bogner | 82077c4 | 2016-04-22 19:54:04 +0000 | [diff] [blame] | 245 | // Process all basic blocks. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 246 | for (BasicBlock &I : F) |
| 247 | MadeChange |= ProcessBlock(I, DT, LI, AA); |
Justin Bogner | 82077c4 | 2016-04-22 19:54:04 +0000 | [diff] [blame] | 248 | EverMadeChange |= MadeChange; |
| 249 | NumSinkIter++; |
| 250 | } while (MadeChange); |
| 251 | |
| 252 | return EverMadeChange; |
| 253 | } |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 254 | |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 255 | PreservedAnalyses SinkingPass::run(Function &F, FunctionAnalysisManager &AM) { |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 256 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 257 | auto &LI = AM.getResult<LoopAnalysis>(F); |
| 258 | auto &AA = AM.getResult<AAManager>(F); |
| 259 | |
| 260 | if (!iterativelySinkInstructions(F, DT, LI, AA)) |
| 261 | return PreservedAnalyses::all(); |
| 262 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 263 | PreservedAnalyses PA; |
| 264 | PA.preserveSet<CFGAnalyses>(); |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 265 | return PA; |
| 266 | } |
| 267 | |
| 268 | namespace { |
| 269 | class SinkingLegacyPass : public FunctionPass { |
| 270 | public: |
| 271 | static char ID; // Pass identification |
| 272 | SinkingLegacyPass() : FunctionPass(ID) { |
| 273 | initializeSinkingLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 274 | } |
| 275 | |
| 276 | bool runOnFunction(Function &F) override { |
| 277 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 278 | auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 279 | auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 280 | |
| 281 | return iterativelySinkInstructions(F, DT, LI, AA); |
| 282 | } |
| 283 | |
| 284 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 285 | AU.setPreservesCFG(); |
| 286 | FunctionPass::getAnalysisUsage(AU); |
| 287 | AU.addRequired<AAResultsWrapperPass>(); |
| 288 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 289 | AU.addRequired<LoopInfoWrapperPass>(); |
| 290 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 291 | AU.addPreserved<LoopInfoWrapperPass>(); |
| 292 | } |
| 293 | }; |
| 294 | } // end anonymous namespace |
| 295 | |
| 296 | char SinkingLegacyPass::ID = 0; |
| 297 | INITIALIZE_PASS_BEGIN(SinkingLegacyPass, "sink", "Code sinking", false, false) |
| 298 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 299 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 300 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 301 | INITIALIZE_PASS_END(SinkingLegacyPass, "sink", "Code sinking", false, false) |
| 302 | |
| 303 | FunctionPass *llvm::createSinkingPass() { return new SinkingLegacyPass(); } |