Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 1 | //===-- TailDuplication.cpp - Duplicate blocks into predecessors' tails ---===// |
| 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 pass duplicates basic blocks ending in unconditional branches into |
| 11 | // the tails of their predecessors. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "tailduplication" |
| 16 | #include "llvm/Function.h" |
| 17 | #include "llvm/CodeGen/Passes.h" |
| 18 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 20 | #include "llvm/Target/TargetInstrInfo.h" |
| 21 | #include "llvm/Support/CommandLine.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include "llvm/ADT/SmallSet.h" |
| 25 | #include "llvm/ADT/SetVector.h" |
| 26 | #include "llvm/ADT/Statistic.h" |
| 27 | using namespace llvm; |
| 28 | |
| 29 | STATISTIC(NumTailDups , "Number of tail duplicated blocks"); |
| 30 | STATISTIC(NumInstrDups , "Additional instructions due to tail duplication"); |
| 31 | STATISTIC(NumDeadBlocks, "Number of dead blocks removed"); |
| 32 | |
| 33 | // Heuristic for tail duplication. |
| 34 | static cl::opt<unsigned> |
| 35 | TailDuplicateSize("tail-dup-size", |
| 36 | cl::desc("Maximum instructions to consider tail duplicating"), |
| 37 | cl::init(2), cl::Hidden); |
| 38 | |
| 39 | namespace { |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 40 | /// TailDuplicatePass - Perform tail duplication. |
| 41 | class TailDuplicatePass : public MachineFunctionPass { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 42 | const TargetInstrInfo *TII; |
| 43 | MachineModuleInfo *MMI; |
| 44 | |
| 45 | public: |
| 46 | static char ID; |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 47 | explicit TailDuplicatePass() : MachineFunctionPass(&ID) {} |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 48 | |
| 49 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 50 | virtual const char *getPassName() const { return "Tail Duplication"; } |
| 51 | |
| 52 | private: |
| 53 | bool TailDuplicateBlocks(MachineFunction &MF); |
| 54 | bool TailDuplicate(MachineBasicBlock *TailBB, MachineFunction &MF); |
| 55 | void RemoveDeadBlock(MachineBasicBlock *MBB); |
| 56 | }; |
| 57 | |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 58 | char TailDuplicatePass::ID = 0; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 61 | FunctionPass *llvm::createTailDuplicatePass() { |
| 62 | return new TailDuplicatePass(); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 65 | bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 66 | TII = MF.getTarget().getInstrInfo(); |
| 67 | MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 68 | |
| 69 | bool MadeChange = false; |
| 70 | bool MadeChangeThisIteration = true; |
| 71 | while (MadeChangeThisIteration) { |
| 72 | MadeChangeThisIteration = false; |
| 73 | MadeChangeThisIteration |= TailDuplicateBlocks(MF); |
| 74 | MadeChange |= MadeChangeThisIteration; |
| 75 | } |
| 76 | |
| 77 | return MadeChange; |
| 78 | } |
| 79 | |
| 80 | /// TailDuplicateBlocks - Look for small blocks that are unconditionally |
| 81 | /// branched to and do not fall through. Tail-duplicate their instructions |
| 82 | /// into their predecessors to eliminate (dynamic) branches. |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 83 | bool TailDuplicatePass::TailDuplicateBlocks(MachineFunction &MF) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 84 | bool MadeChange = false; |
| 85 | |
| 86 | for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) { |
| 87 | MachineBasicBlock *MBB = I++; |
| 88 | |
| 89 | // Only duplicate blocks that end with unconditional branches. |
| 90 | if (MBB->canFallThrough()) |
| 91 | continue; |
| 92 | |
| 93 | MadeChange |= TailDuplicate(MBB, MF); |
| 94 | |
| 95 | // If it is dead, remove it. |
| 96 | if (MBB->pred_empty()) { |
| 97 | NumInstrDups -= MBB->size(); |
| 98 | RemoveDeadBlock(MBB); |
| 99 | MadeChange = true; |
| 100 | ++NumDeadBlocks; |
| 101 | } |
| 102 | } |
| 103 | return MadeChange; |
| 104 | } |
| 105 | |
| 106 | /// TailDuplicate - If it is profitable, duplicate TailBB's contents in each |
| 107 | /// of its predecessors. |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 108 | bool TailDuplicatePass::TailDuplicate(MachineBasicBlock *TailBB, |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 109 | MachineFunction &MF) { |
| 110 | // Don't try to tail-duplicate single-block loops. |
| 111 | if (TailBB->isSuccessor(TailBB)) |
| 112 | return false; |
| 113 | |
| 114 | // Set the limit on the number of instructions to duplicate, with a default |
| 115 | // of one less than the tail-merge threshold. When optimizing for size, |
| 116 | // duplicate only one, because one branch instruction can be eliminated to |
| 117 | // compensate for the duplication. |
| 118 | unsigned MaxDuplicateCount; |
Bob Wilson | 3858225 | 2009-11-30 18:56:45 +0000 | [diff] [blame] | 119 | if (!TailBB->empty() && TailBB->back().getDesc().isIndirectBranch()) |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 120 | // If the target has hardware branch prediction that can handle indirect |
| 121 | // branches, duplicating them can often make them predictable when there |
| 122 | // are common paths through the code. The limit needs to be high enough |
| 123 | // to allow undoing the effects of tail merging. |
| 124 | MaxDuplicateCount = 20; |
Bob Wilson | 3858225 | 2009-11-30 18:56:45 +0000 | [diff] [blame] | 125 | else if (MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize)) |
| 126 | MaxDuplicateCount = 1; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 127 | else |
| 128 | MaxDuplicateCount = TailDuplicateSize; |
| 129 | |
| 130 | // Check the instructions in the block to determine whether tail-duplication |
| 131 | // is invalid or unlikely to be profitable. |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame^] | 132 | unsigned InstrCount = 0; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 133 | bool HasCall = false; |
| 134 | for (MachineBasicBlock::iterator I = TailBB->begin(); |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame^] | 135 | I != TailBB->end(); ++I) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 136 | // Non-duplicable things shouldn't be tail-duplicated. |
| 137 | if (I->getDesc().isNotDuplicable()) return false; |
| 138 | // Don't duplicate more than the threshold. |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame^] | 139 | if (InstrCount == MaxDuplicateCount) return false; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 140 | // Remember if we saw a call. |
| 141 | if (I->getDesc().isCall()) HasCall = true; |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame^] | 142 | if (I->getOpcode() != TargetInstrInfo::PHI) |
| 143 | InstrCount += 1; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 144 | } |
| 145 | // Heuristically, don't tail-duplicate calls if it would expand code size, |
| 146 | // as it's less likely to be worth the extra cost. |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame^] | 147 | if (InstrCount > 1 && HasCall) |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 148 | return false; |
| 149 | |
| 150 | // Iterate through all the unique predecessors and tail-duplicate this |
| 151 | // block into them, if possible. Copying the list ahead of time also |
| 152 | // avoids trouble with the predecessor list reallocating. |
| 153 | bool Changed = false; |
| 154 | SmallSetVector<MachineBasicBlock *, 8> Preds(TailBB->pred_begin(), |
| 155 | TailBB->pred_end()); |
| 156 | for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(), |
| 157 | PE = Preds.end(); PI != PE; ++PI) { |
| 158 | MachineBasicBlock *PredBB = *PI; |
| 159 | |
| 160 | assert(TailBB != PredBB && |
| 161 | "Single-block loop should have been rejected earlier!"); |
| 162 | if (PredBB->succ_size() > 1) continue; |
| 163 | |
| 164 | MachineBasicBlock *PredTBB, *PredFBB; |
| 165 | SmallVector<MachineOperand, 4> PredCond; |
| 166 | if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)) |
| 167 | continue; |
| 168 | if (!PredCond.empty()) |
| 169 | continue; |
| 170 | // EH edges are ignored by AnalyzeBranch. |
| 171 | if (PredBB->succ_size() != 1) |
| 172 | continue; |
| 173 | // Don't duplicate into a fall-through predecessor (at least for now). |
| 174 | if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough()) |
| 175 | continue; |
| 176 | |
| 177 | DEBUG(errs() << "\nTail-duplicating into PredBB: " << *PredBB |
| 178 | << "From Succ: " << *TailBB); |
| 179 | |
| 180 | // Remove PredBB's unconditional branch. |
| 181 | TII->RemoveBranch(*PredBB); |
| 182 | // Clone the contents of TailBB into PredBB. |
| 183 | for (MachineBasicBlock::iterator I = TailBB->begin(), E = TailBB->end(); |
| 184 | I != E; ++I) { |
| 185 | MachineInstr *NewMI = MF.CloneMachineInstr(I); |
| 186 | PredBB->insert(PredBB->end(), NewMI); |
| 187 | } |
| 188 | NumInstrDups += TailBB->size() - 1; // subtract one for removed branch |
| 189 | |
| 190 | // Update the CFG. |
| 191 | PredBB->removeSuccessor(PredBB->succ_begin()); |
| 192 | assert(PredBB->succ_empty() && |
| 193 | "TailDuplicate called on block with multiple successors!"); |
| 194 | for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(), |
| 195 | E = TailBB->succ_end(); I != E; ++I) |
| 196 | PredBB->addSuccessor(*I); |
| 197 | |
| 198 | Changed = true; |
| 199 | ++NumTailDups; |
| 200 | } |
| 201 | |
| 202 | // If TailBB was duplicated into all its predecessors except for the prior |
| 203 | // block, which falls through unconditionally, move the contents of this |
| 204 | // block into the prior block. |
| 205 | MachineBasicBlock &PrevBB = *prior(MachineFunction::iterator(TailBB)); |
| 206 | MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0; |
| 207 | SmallVector<MachineOperand, 4> PriorCond; |
| 208 | bool PriorUnAnalyzable = |
| 209 | TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, true); |
| 210 | // This has to check PrevBB->succ_size() because EH edges are ignored by |
| 211 | // AnalyzeBranch. |
| 212 | if (!PriorUnAnalyzable && PriorCond.empty() && !PriorTBB && |
| 213 | TailBB->pred_size() == 1 && PrevBB.succ_size() == 1 && |
| 214 | !TailBB->hasAddressTaken()) { |
| 215 | DEBUG(errs() << "\nMerging into block: " << PrevBB |
| 216 | << "From MBB: " << *TailBB); |
| 217 | PrevBB.splice(PrevBB.end(), TailBB, TailBB->begin(), TailBB->end()); |
| 218 | PrevBB.removeSuccessor(PrevBB.succ_begin());; |
| 219 | assert(PrevBB.succ_empty()); |
| 220 | PrevBB.transferSuccessors(TailBB); |
| 221 | Changed = true; |
| 222 | } |
| 223 | |
| 224 | return Changed; |
| 225 | } |
| 226 | |
| 227 | /// RemoveDeadBlock - Remove the specified dead machine basic block from the |
| 228 | /// function, updating the CFG. |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 229 | void TailDuplicatePass::RemoveDeadBlock(MachineBasicBlock *MBB) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 230 | assert(MBB->pred_empty() && "MBB must be dead!"); |
| 231 | DEBUG(errs() << "\nRemoving MBB: " << *MBB); |
| 232 | |
| 233 | // Remove all successors. |
| 234 | while (!MBB->succ_empty()) |
| 235 | MBB->removeSuccessor(MBB->succ_end()-1); |
| 236 | |
| 237 | // If there are any labels in the basic block, unregister them from |
| 238 | // MachineModuleInfo. |
| 239 | if (MMI && !MBB->empty()) { |
| 240 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 241 | I != E; ++I) { |
| 242 | if (I->isLabel()) |
| 243 | // The label ID # is always operand #0, an immediate. |
| 244 | MMI->InvalidateLabel(I->getOperand(0).getImm()); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // Remove the block. |
| 249 | MBB->eraseFromParent(); |
| 250 | } |
| 251 | |