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" |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 21 | #include "llvm/CodeGen/MachineSSAUpdater.h" |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrInfo.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/Debug.h" |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/ADT/SmallSet.h" |
| 28 | #include "llvm/ADT/SetVector.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
| 30 | using namespace llvm; |
| 31 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 32 | STATISTIC(NumTails , "Number of tails duplicated"); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 33 | STATISTIC(NumTailDups , "Number of tail duplicated blocks"); |
| 34 | STATISTIC(NumInstrDups , "Additional instructions due to tail duplication"); |
| 35 | STATISTIC(NumDeadBlocks, "Number of dead blocks removed"); |
| 36 | |
| 37 | // Heuristic for tail duplication. |
| 38 | static cl::opt<unsigned> |
| 39 | TailDuplicateSize("tail-dup-size", |
| 40 | cl::desc("Maximum instructions to consider tail duplicating"), |
| 41 | cl::init(2), cl::Hidden); |
| 42 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 43 | static cl::opt<bool> |
| 44 | TailDupVerify("tail-dup-verify", |
| 45 | cl::desc("Verify sanity of PHI instructions during taildup"), |
| 46 | cl::init(false), cl::Hidden); |
| 47 | |
| 48 | static cl::opt<unsigned> |
| 49 | TailDupLimit("tail-dup-limit", cl::init(~0U), cl::Hidden); |
| 50 | |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 51 | typedef std::vector<std::pair<MachineBasicBlock*,unsigned> > AvailableValsTy; |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 52 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 53 | namespace { |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 54 | /// TailDuplicatePass - Perform tail duplication. |
| 55 | class TailDuplicatePass : public MachineFunctionPass { |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 56 | bool PreRegAlloc; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 57 | const TargetInstrInfo *TII; |
| 58 | MachineModuleInfo *MMI; |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 59 | MachineRegisterInfo *MRI; |
| 60 | |
| 61 | // SSAUpdateVRs - A list of virtual registers for which to update SSA form. |
| 62 | SmallVector<unsigned, 16> SSAUpdateVRs; |
| 63 | |
| 64 | // SSAUpdateVals - For each virtual register in SSAUpdateVals keep a list of |
| 65 | // source virtual registers. |
| 66 | DenseMap<unsigned, AvailableValsTy> SSAUpdateVals; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 67 | |
| 68 | public: |
| 69 | static char ID; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 70 | explicit TailDuplicatePass(bool PreRA) : |
| 71 | MachineFunctionPass(&ID), PreRegAlloc(PreRA) {} |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 72 | |
| 73 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 74 | virtual const char *getPassName() const { return "Tail Duplication"; } |
| 75 | |
| 76 | private: |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 77 | void AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg, |
| 78 | MachineBasicBlock *BB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 79 | void ProcessPHI(MachineInstr *MI, MachineBasicBlock *TailBB, |
| 80 | MachineBasicBlock *PredBB, |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 81 | DenseMap<unsigned, unsigned> &LocalVRMap, |
| 82 | SmallVector<std::pair<unsigned,unsigned>, 4> &Copies); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 83 | void DuplicateInstruction(MachineInstr *MI, |
| 84 | MachineBasicBlock *TailBB, |
| 85 | MachineBasicBlock *PredBB, |
| 86 | MachineFunction &MF, |
| 87 | DenseMap<unsigned, unsigned> &LocalVRMap); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 88 | void UpdateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead, |
| 89 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
| 90 | SmallSetVector<MachineBasicBlock*, 8> &Succs); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 91 | bool TailDuplicateBlocks(MachineFunction &MF); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 92 | bool TailDuplicate(MachineBasicBlock *TailBB, MachineFunction &MF, |
| 93 | SmallVector<MachineBasicBlock*, 8> &TDBBs); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 94 | void RemoveDeadBlock(MachineBasicBlock *MBB); |
| 95 | }; |
| 96 | |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 97 | char TailDuplicatePass::ID = 0; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 100 | FunctionPass *llvm::createTailDuplicatePass(bool PreRegAlloc) { |
| 101 | return new TailDuplicatePass(PreRegAlloc); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 104 | bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 105 | TII = MF.getTarget().getInstrInfo(); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 106 | MRI = &MF.getRegInfo(); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 107 | MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 108 | |
| 109 | bool MadeChange = false; |
| 110 | bool MadeChangeThisIteration = true; |
| 111 | while (MadeChangeThisIteration) { |
| 112 | MadeChangeThisIteration = false; |
| 113 | MadeChangeThisIteration |= TailDuplicateBlocks(MF); |
| 114 | MadeChange |= MadeChangeThisIteration; |
| 115 | } |
| 116 | |
| 117 | return MadeChange; |
| 118 | } |
| 119 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 120 | static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) { |
| 121 | for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ++I) { |
| 122 | MachineBasicBlock *MBB = I; |
| 123 | SmallSetVector<MachineBasicBlock*, 8> Preds(MBB->pred_begin(), |
| 124 | MBB->pred_end()); |
| 125 | MachineBasicBlock::iterator MI = MBB->begin(); |
| 126 | while (MI != MBB->end()) { |
| 127 | if (MI->getOpcode() != TargetInstrInfo::PHI) |
| 128 | break; |
| 129 | for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(), |
| 130 | PE = Preds.end(); PI != PE; ++PI) { |
| 131 | MachineBasicBlock *PredBB = *PI; |
| 132 | bool Found = false; |
| 133 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { |
| 134 | MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB(); |
| 135 | if (PHIBB == PredBB) { |
| 136 | Found = true; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | if (!Found) { |
| 141 | errs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI; |
| 142 | errs() << " missing input from predecessor BB#" |
| 143 | << PredBB->getNumber() << '\n'; |
| 144 | llvm_unreachable(0); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { |
| 149 | MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB(); |
| 150 | if (CheckExtra && !Preds.count(PHIBB)) { |
| 151 | // This is not a hard error. |
| 152 | errs() << "Warning: malformed PHI in BB#" << MBB->getNumber() |
| 153 | << ": " << *MI; |
| 154 | errs() << " extra input from predecessor BB#" |
| 155 | << PHIBB->getNumber() << '\n'; |
| 156 | } |
| 157 | if (PHIBB->getNumber() < 0) { |
| 158 | errs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI; |
| 159 | errs() << " non-existing BB#" << PHIBB->getNumber() << '\n'; |
| 160 | llvm_unreachable(0); |
| 161 | } |
| 162 | } |
| 163 | ++MI; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 168 | /// TailDuplicateBlocks - Look for small blocks that are unconditionally |
| 169 | /// branched to and do not fall through. Tail-duplicate their instructions |
| 170 | /// into their predecessors to eliminate (dynamic) branches. |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 171 | bool TailDuplicatePass::TailDuplicateBlocks(MachineFunction &MF) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 172 | bool MadeChange = false; |
| 173 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 174 | if (PreRegAlloc && TailDupVerify) { |
| 175 | DEBUG(errs() << "\n*** Before tail-duplicating\n"); |
| 176 | VerifyPHIs(MF, true); |
| 177 | } |
| 178 | |
| 179 | SmallVector<MachineInstr*, 8> NewPHIs; |
| 180 | MachineSSAUpdater SSAUpdate(MF, &NewPHIs); |
| 181 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 182 | for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) { |
| 183 | MachineBasicBlock *MBB = I++; |
| 184 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 185 | if (NumTails == TailDupLimit) |
| 186 | break; |
| 187 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 188 | // Only duplicate blocks that end with unconditional branches. |
| 189 | if (MBB->canFallThrough()) |
| 190 | continue; |
| 191 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 192 | // Save the successors list. |
| 193 | SmallSetVector<MachineBasicBlock*, 8> Succs(MBB->succ_begin(), |
| 194 | MBB->succ_end()); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 195 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 196 | SmallVector<MachineBasicBlock*, 8> TDBBs; |
| 197 | if (TailDuplicate(MBB, MF, TDBBs)) { |
| 198 | ++NumTails; |
| 199 | |
| 200 | // TailBB's immediate successors are now successors of those predecessors |
| 201 | // which duplicated TailBB. Add the predecessors as sources to the PHI |
| 202 | // instructions. |
| 203 | bool isDead = MBB->pred_empty(); |
| 204 | if (PreRegAlloc) |
| 205 | UpdateSuccessorsPHIs(MBB, isDead, TDBBs, Succs); |
| 206 | |
| 207 | // If it is dead, remove it. |
| 208 | if (isDead) { |
| 209 | NumInstrDups -= MBB->size(); |
| 210 | RemoveDeadBlock(MBB); |
| 211 | ++NumDeadBlocks; |
| 212 | } |
| 213 | |
| 214 | // Update SSA form. |
| 215 | if (!SSAUpdateVRs.empty()) { |
| 216 | for (unsigned i = 0, e = SSAUpdateVRs.size(); i != e; ++i) { |
| 217 | unsigned VReg = SSAUpdateVRs[i]; |
| 218 | SSAUpdate.Initialize(VReg); |
| 219 | |
| 220 | // If the original definition is still around, add it as an available |
| 221 | // value. |
| 222 | MachineInstr *DefMI = MRI->getVRegDef(VReg); |
| 223 | MachineBasicBlock *DefBB = 0; |
| 224 | if (DefMI) { |
| 225 | DefBB = DefMI->getParent(); |
| 226 | SSAUpdate.AddAvailableValue(DefBB, VReg); |
| 227 | } |
| 228 | |
| 229 | // Add the new vregs as available values. |
| 230 | DenseMap<unsigned, AvailableValsTy>::iterator LI = |
| 231 | SSAUpdateVals.find(VReg); |
| 232 | for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) { |
| 233 | MachineBasicBlock *SrcBB = LI->second[j].first; |
| 234 | unsigned SrcReg = LI->second[j].second; |
| 235 | SSAUpdate.AddAvailableValue(SrcBB, SrcReg); |
| 236 | } |
| 237 | |
| 238 | // Rewrite uses that are outside of the original def's block. |
| 239 | MachineRegisterInfo::use_iterator UI = MRI->use_begin(VReg); |
| 240 | while (UI != MRI->use_end()) { |
| 241 | MachineOperand &UseMO = UI.getOperand(); |
| 242 | MachineInstr *UseMI = &*UI; |
| 243 | ++UI; |
| 244 | if (UseMI->getParent() == DefBB) |
| 245 | continue; |
| 246 | SSAUpdate.RewriteUse(UseMO); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | SSAUpdateVRs.clear(); |
| 251 | SSAUpdateVals.clear(); |
| 252 | } |
| 253 | |
| 254 | if (PreRegAlloc && TailDupVerify) |
| 255 | VerifyPHIs(MF, false); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 256 | MadeChange = true; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 259 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 260 | return MadeChange; |
| 261 | } |
| 262 | |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 263 | static bool isDefLiveOut(unsigned Reg, MachineBasicBlock *BB, |
| 264 | const MachineRegisterInfo *MRI) { |
| 265 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg), |
| 266 | UE = MRI->use_end(); UI != UE; ++UI) { |
| 267 | MachineInstr *UseMI = &*UI; |
| 268 | if (UseMI->getParent() != BB) |
| 269 | return true; |
| 270 | } |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB) { |
| 275 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) |
| 276 | if (MI->getOperand(i+1).getMBB() == SrcBB) |
| 277 | return i; |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | /// AddSSAUpdateEntry - Add a definition and source virtual registers pair for |
| 282 | /// SSA update. |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 283 | void TailDuplicatePass::AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg, |
| 284 | MachineBasicBlock *BB) { |
| 285 | DenseMap<unsigned, AvailableValsTy>::iterator LI= SSAUpdateVals.find(OrigReg); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 286 | if (LI != SSAUpdateVals.end()) |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 287 | LI->second.push_back(std::make_pair(BB, NewReg)); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 288 | else { |
| 289 | AvailableValsTy Vals; |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 290 | Vals.push_back(std::make_pair(BB, NewReg)); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 291 | SSAUpdateVals.insert(std::make_pair(OrigReg, Vals)); |
| 292 | SSAUpdateVRs.push_back(OrigReg); |
| 293 | } |
| 294 | } |
| 295 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 296 | /// ProcessPHI - Process PHI node in TailBB by turning it into a copy in PredBB. |
| 297 | /// Remember the source register that's contributed by PredBB and update SSA |
| 298 | /// update map. |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 299 | void TailDuplicatePass::ProcessPHI(MachineInstr *MI, |
| 300 | MachineBasicBlock *TailBB, |
| 301 | MachineBasicBlock *PredBB, |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 302 | DenseMap<unsigned, unsigned> &LocalVRMap, |
| 303 | SmallVector<std::pair<unsigned,unsigned>, 4> &Copies) { |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 304 | unsigned DefReg = MI->getOperand(0).getReg(); |
| 305 | unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB); |
| 306 | assert(SrcOpIdx && "Unable to find matching PHI source?"); |
| 307 | unsigned SrcReg = MI->getOperand(SrcOpIdx).getReg(); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 308 | const TargetRegisterClass *RC = MRI->getRegClass(DefReg); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 309 | LocalVRMap.insert(std::make_pair(DefReg, SrcReg)); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 310 | |
| 311 | // Insert a copy from source to the end of the block. The def register is the |
| 312 | // available value liveout of the block. |
| 313 | unsigned NewDef = MRI->createVirtualRegister(RC); |
| 314 | Copies.push_back(std::make_pair(NewDef, SrcReg)); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 315 | if (isDefLiveOut(DefReg, TailBB, MRI)) |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 316 | AddSSAUpdateEntry(DefReg, NewDef, PredBB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 317 | |
| 318 | // Remove PredBB from the PHI node. |
| 319 | MI->RemoveOperand(SrcOpIdx+1); |
| 320 | MI->RemoveOperand(SrcOpIdx); |
| 321 | if (MI->getNumOperands() == 1) |
| 322 | MI->eraseFromParent(); |
| 323 | } |
| 324 | |
| 325 | /// DuplicateInstruction - Duplicate a TailBB instruction to PredBB and update |
| 326 | /// the source operands due to earlier PHI translation. |
| 327 | void TailDuplicatePass::DuplicateInstruction(MachineInstr *MI, |
| 328 | MachineBasicBlock *TailBB, |
| 329 | MachineBasicBlock *PredBB, |
| 330 | MachineFunction &MF, |
| 331 | DenseMap<unsigned, unsigned> &LocalVRMap) { |
| 332 | MachineInstr *NewMI = MF.CloneMachineInstr(MI); |
| 333 | for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) { |
| 334 | MachineOperand &MO = NewMI->getOperand(i); |
| 335 | if (!MO.isReg()) |
| 336 | continue; |
| 337 | unsigned Reg = MO.getReg(); |
| 338 | if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 339 | continue; |
| 340 | if (MO.isDef()) { |
| 341 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| 342 | unsigned NewReg = MRI->createVirtualRegister(RC); |
| 343 | MO.setReg(NewReg); |
| 344 | LocalVRMap.insert(std::make_pair(Reg, NewReg)); |
| 345 | if (isDefLiveOut(Reg, TailBB, MRI)) |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 346 | AddSSAUpdateEntry(Reg, NewReg, PredBB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 347 | } else { |
| 348 | DenseMap<unsigned, unsigned>::iterator VI = LocalVRMap.find(Reg); |
| 349 | if (VI != LocalVRMap.end()) |
| 350 | MO.setReg(VI->second); |
| 351 | } |
| 352 | } |
| 353 | PredBB->insert(PredBB->end(), NewMI); |
| 354 | } |
| 355 | |
| 356 | /// UpdateSuccessorsPHIs - After FromBB is tail duplicated into its predecessor |
| 357 | /// blocks, the successors have gained new predecessors. Update the PHI |
| 358 | /// instructions in them accordingly. |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 359 | void |
| 360 | TailDuplicatePass::UpdateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead, |
| 361 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 362 | SmallSetVector<MachineBasicBlock*,8> &Succs) { |
| 363 | for (SmallSetVector<MachineBasicBlock*, 8>::iterator SI = Succs.begin(), |
| 364 | SE = Succs.end(); SI != SE; ++SI) { |
| 365 | MachineBasicBlock *SuccBB = *SI; |
| 366 | for (MachineBasicBlock::iterator II = SuccBB->begin(), EE = SuccBB->end(); |
| 367 | II != EE; ++II) { |
| 368 | if (II->getOpcode() != TargetInstrInfo::PHI) |
| 369 | break; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 370 | unsigned Idx = 0; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 371 | for (unsigned i = 1, e = II->getNumOperands(); i != e; i += 2) { |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 372 | MachineOperand &MO = II->getOperand(i+1); |
| 373 | if (MO.getMBB() == FromBB) { |
| 374 | Idx = i; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 375 | break; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | assert(Idx != 0); |
| 380 | MachineOperand &MO0 = II->getOperand(Idx); |
| 381 | unsigned Reg = MO0.getReg(); |
| 382 | if (isDead) { |
| 383 | // Folded into the previous BB. |
| 384 | // There could be duplicate phi source entries. FIXME: Should sdisel |
| 385 | // or earlier pass fixed this? |
| 386 | for (unsigned i = II->getNumOperands()-2; i != Idx; i -= 2) { |
| 387 | MachineOperand &MO = II->getOperand(i+1); |
| 388 | if (MO.getMBB() == FromBB) { |
| 389 | II->RemoveOperand(i+1); |
| 390 | II->RemoveOperand(i); |
| 391 | } |
| 392 | } |
| 393 | II->RemoveOperand(Idx+1); |
| 394 | II->RemoveOperand(Idx); |
| 395 | } |
| 396 | DenseMap<unsigned,AvailableValsTy>::iterator LI=SSAUpdateVals.find(Reg); |
| 397 | if (LI != SSAUpdateVals.end()) { |
| 398 | // This register is defined in the tail block. |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 399 | for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) { |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 400 | MachineBasicBlock *SrcBB = LI->second[j].first; |
| 401 | unsigned SrcReg = LI->second[j].second; |
| 402 | II->addOperand(MachineOperand::CreateReg(SrcReg, false)); |
| 403 | II->addOperand(MachineOperand::CreateMBB(SrcBB)); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 404 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 405 | } else { |
| 406 | // Live in tail block, must also be live in predecessors. |
| 407 | for (unsigned j = 0, ee = TDBBs.size(); j != ee; ++j) { |
| 408 | MachineBasicBlock *SrcBB = TDBBs[j]; |
| 409 | II->addOperand(MachineOperand::CreateReg(Reg, false)); |
| 410 | II->addOperand(MachineOperand::CreateMBB(SrcBB)); |
| 411 | } |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 417 | /// TailDuplicate - If it is profitable, duplicate TailBB's contents in each |
| 418 | /// of its predecessors. |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 419 | bool |
| 420 | TailDuplicatePass::TailDuplicate(MachineBasicBlock *TailBB, MachineFunction &MF, |
| 421 | SmallVector<MachineBasicBlock*, 8> &TDBBs) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 422 | // Don't try to tail-duplicate single-block loops. |
| 423 | if (TailBB->isSuccessor(TailBB)) |
| 424 | return false; |
| 425 | |
| 426 | // Set the limit on the number of instructions to duplicate, with a default |
| 427 | // of one less than the tail-merge threshold. When optimizing for size, |
| 428 | // duplicate only one, because one branch instruction can be eliminated to |
| 429 | // compensate for the duplication. |
| 430 | unsigned MaxDuplicateCount; |
Bob Wilson | 3858225 | 2009-11-30 18:56:45 +0000 | [diff] [blame] | 431 | if (!TailBB->empty() && TailBB->back().getDesc().isIndirectBranch()) |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 432 | // If the target has hardware branch prediction that can handle indirect |
| 433 | // branches, duplicating them can often make them predictable when there |
| 434 | // are common paths through the code. The limit needs to be high enough |
| 435 | // to allow undoing the effects of tail merging. |
| 436 | MaxDuplicateCount = 20; |
Bob Wilson | 3858225 | 2009-11-30 18:56:45 +0000 | [diff] [blame] | 437 | else if (MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize)) |
| 438 | MaxDuplicateCount = 1; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 439 | else |
| 440 | MaxDuplicateCount = TailDuplicateSize; |
| 441 | |
| 442 | // Check the instructions in the block to determine whether tail-duplication |
| 443 | // is invalid or unlikely to be profitable. |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame] | 444 | unsigned InstrCount = 0; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 445 | bool HasCall = false; |
| 446 | for (MachineBasicBlock::iterator I = TailBB->begin(); |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame] | 447 | I != TailBB->end(); ++I) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 448 | // Non-duplicable things shouldn't be tail-duplicated. |
| 449 | if (I->getDesc().isNotDuplicable()) return false; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 450 | // Do not duplicate 'return' instructions if this is a pre-regalloc run. |
| 451 | // A return may expand into a lot more instructions (e.g. reload of callee |
| 452 | // saved registers) after PEI. |
| 453 | if (PreRegAlloc && I->getDesc().isReturn()) return false; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 454 | // Don't duplicate more than the threshold. |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame] | 455 | if (InstrCount == MaxDuplicateCount) return false; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 456 | // Remember if we saw a call. |
| 457 | if (I->getDesc().isCall()) HasCall = true; |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame] | 458 | if (I->getOpcode() != TargetInstrInfo::PHI) |
| 459 | InstrCount += 1; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 460 | } |
| 461 | // Heuristically, don't tail-duplicate calls if it would expand code size, |
| 462 | // as it's less likely to be worth the extra cost. |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame] | 463 | if (InstrCount > 1 && HasCall) |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 464 | return false; |
| 465 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 466 | DEBUG(errs() << "\n*** Tail-duplicating BB#" << TailBB->getNumber() << '\n'); |
| 467 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 468 | // Iterate through all the unique predecessors and tail-duplicate this |
| 469 | // block into them, if possible. Copying the list ahead of time also |
| 470 | // avoids trouble with the predecessor list reallocating. |
| 471 | bool Changed = false; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 472 | SmallSetVector<MachineBasicBlock*, 8> Preds(TailBB->pred_begin(), |
| 473 | TailBB->pred_end()); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 474 | for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(), |
| 475 | PE = Preds.end(); PI != PE; ++PI) { |
| 476 | MachineBasicBlock *PredBB = *PI; |
| 477 | |
| 478 | assert(TailBB != PredBB && |
| 479 | "Single-block loop should have been rejected earlier!"); |
| 480 | if (PredBB->succ_size() > 1) continue; |
| 481 | |
| 482 | MachineBasicBlock *PredTBB, *PredFBB; |
| 483 | SmallVector<MachineOperand, 4> PredCond; |
| 484 | if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)) |
| 485 | continue; |
| 486 | if (!PredCond.empty()) |
| 487 | continue; |
| 488 | // EH edges are ignored by AnalyzeBranch. |
| 489 | if (PredBB->succ_size() != 1) |
| 490 | continue; |
| 491 | // Don't duplicate into a fall-through predecessor (at least for now). |
| 492 | if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough()) |
| 493 | continue; |
| 494 | |
| 495 | DEBUG(errs() << "\nTail-duplicating into PredBB: " << *PredBB |
| 496 | << "From Succ: " << *TailBB); |
| 497 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 498 | TDBBs.push_back(PredBB); |
| 499 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 500 | // Remove PredBB's unconditional branch. |
| 501 | TII->RemoveBranch(*PredBB); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 502 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 503 | // Clone the contents of TailBB into PredBB. |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 504 | DenseMap<unsigned, unsigned> LocalVRMap; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 505 | SmallVector<std::pair<unsigned,unsigned>, 4> Copies; |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 506 | MachineBasicBlock::iterator I = TailBB->begin(); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 507 | while (I != TailBB->end()) { |
| 508 | MachineInstr *MI = &*I; |
| 509 | ++I; |
| 510 | if (MI->getOpcode() == TargetInstrInfo::PHI) { |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 511 | // Replace the uses of the def of the PHI with the register coming |
| 512 | // from PredBB. |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 513 | ProcessPHI(MI, TailBB, PredBB, LocalVRMap, Copies); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 514 | } else { |
| 515 | // Replace def of virtual registers with new registers, and update |
| 516 | // uses with PHI source register or the new registers. |
| 517 | DuplicateInstruction(MI, TailBB, PredBB, MF, LocalVRMap); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 518 | } |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 519 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 520 | MachineBasicBlock::iterator Loc = PredBB->getFirstTerminator(); |
| 521 | for (unsigned i = 0, e = Copies.size(); i != e; ++i) { |
| 522 | const TargetRegisterClass *RC = MRI->getRegClass(Copies[i].first); |
| 523 | TII->copyRegToReg(*PredBB, Loc, Copies[i].first, Copies[i].second, RC, RC); |
| 524 | } |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 525 | NumInstrDups += TailBB->size() - 1; // subtract one for removed branch |
| 526 | |
| 527 | // Update the CFG. |
| 528 | PredBB->removeSuccessor(PredBB->succ_begin()); |
| 529 | assert(PredBB->succ_empty() && |
| 530 | "TailDuplicate called on block with multiple successors!"); |
| 531 | for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(), |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 532 | E = TailBB->succ_end(); I != E; ++I) |
| 533 | PredBB->addSuccessor(*I); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 534 | |
| 535 | Changed = true; |
| 536 | ++NumTailDups; |
| 537 | } |
| 538 | |
| 539 | // If TailBB was duplicated into all its predecessors except for the prior |
| 540 | // block, which falls through unconditionally, move the contents of this |
| 541 | // block into the prior block. |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 542 | MachineBasicBlock *PrevBB = prior(MachineFunction::iterator(TailBB)); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 543 | MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0; |
| 544 | SmallVector<MachineOperand, 4> PriorCond; |
| 545 | bool PriorUnAnalyzable = |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 546 | TII->AnalyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond, true); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 547 | // This has to check PrevBB->succ_size() because EH edges are ignored by |
| 548 | // AnalyzeBranch. |
| 549 | if (!PriorUnAnalyzable && PriorCond.empty() && !PriorTBB && |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 550 | TailBB->pred_size() == 1 && PrevBB->succ_size() == 1 && |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 551 | !TailBB->hasAddressTaken()) { |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 552 | DEBUG(errs() << "\nMerging into block: " << *PrevBB |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 553 | << "From MBB: " << *TailBB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 554 | if (PreRegAlloc) { |
| 555 | DenseMap<unsigned, unsigned> LocalVRMap; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 556 | SmallVector<std::pair<unsigned,unsigned>, 4> Copies; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 557 | MachineBasicBlock::iterator I = TailBB->begin(); |
| 558 | // Process PHI instructions first. |
| 559 | while (I != TailBB->end() && I->getOpcode() == TargetInstrInfo::PHI) { |
| 560 | // Replace the uses of the def of the PHI with the register coming |
| 561 | // from PredBB. |
| 562 | MachineInstr *MI = &*I++; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 563 | ProcessPHI(MI, TailBB, PrevBB, LocalVRMap, Copies); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 564 | if (MI->getParent()) |
| 565 | MI->eraseFromParent(); |
| 566 | } |
| 567 | |
| 568 | // Now copy the non-PHI instructions. |
| 569 | while (I != TailBB->end()) { |
| 570 | // Replace def of virtual registers with new registers, and update |
| 571 | // uses with PHI source register or the new registers. |
| 572 | MachineInstr *MI = &*I++; |
| 573 | DuplicateInstruction(MI, TailBB, PrevBB, MF, LocalVRMap); |
| 574 | MI->eraseFromParent(); |
| 575 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 576 | MachineBasicBlock::iterator Loc = PrevBB->getFirstTerminator(); |
| 577 | for (unsigned i = 0, e = Copies.size(); i != e; ++i) { |
| 578 | const TargetRegisterClass *RC = MRI->getRegClass(Copies[i].first); |
| 579 | TII->copyRegToReg(*PrevBB, Loc, Copies[i].first, Copies[i].second, RC, RC); |
| 580 | } |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 581 | } else { |
| 582 | // No PHIs to worry about, just splice the instructions over. |
| 583 | PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end()); |
| 584 | } |
| 585 | PrevBB->removeSuccessor(PrevBB->succ_begin()); |
| 586 | assert(PrevBB->succ_empty()); |
| 587 | PrevBB->transferSuccessors(TailBB); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 588 | TDBBs.push_back(PrevBB); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 589 | Changed = true; |
| 590 | } |
| 591 | |
| 592 | return Changed; |
| 593 | } |
| 594 | |
| 595 | /// RemoveDeadBlock - Remove the specified dead machine basic block from the |
| 596 | /// function, updating the CFG. |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 597 | void TailDuplicatePass::RemoveDeadBlock(MachineBasicBlock *MBB) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 598 | assert(MBB->pred_empty() && "MBB must be dead!"); |
| 599 | DEBUG(errs() << "\nRemoving MBB: " << *MBB); |
| 600 | |
| 601 | // Remove all successors. |
| 602 | while (!MBB->succ_empty()) |
| 603 | MBB->removeSuccessor(MBB->succ_end()-1); |
| 604 | |
| 605 | // If there are any labels in the basic block, unregister them from |
| 606 | // MachineModuleInfo. |
| 607 | if (MMI && !MBB->empty()) { |
| 608 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 609 | I != E; ++I) { |
| 610 | if (I->isLabel()) |
| 611 | // The label ID # is always operand #0, an immediate. |
| 612 | MMI->InvalidateLabel(I->getOperand(0).getImm()); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // Remove the block. |
| 617 | MBB->eraseFromParent(); |
| 618 | } |
| 619 | |