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" |
Jakob Stoklund Olesen | 1e1098c | 2010-07-10 22:42:59 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 22 | #include "llvm/CodeGen/MachineSSAUpdater.h" |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetInstrInfo.h" |
| 24 | #include "llvm/Support/CommandLine.h" |
| 25 | #include "llvm/Support/Debug.h" |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include "llvm/ADT/SmallSet.h" |
| 29 | #include "llvm/ADT/SetVector.h" |
| 30 | #include "llvm/ADT/Statistic.h" |
| 31 | using namespace llvm; |
| 32 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 33 | STATISTIC(NumTails , "Number of tails duplicated"); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 34 | STATISTIC(NumTailDups , "Number of tail duplicated blocks"); |
| 35 | STATISTIC(NumInstrDups , "Additional instructions due to tail duplication"); |
| 36 | STATISTIC(NumDeadBlocks, "Number of dead blocks removed"); |
Rafael Espindola | 0cdca08 | 2011-06-08 14:13:31 +0000 | [diff] [blame] | 37 | STATISTIC(NumAddedPHIs , "Number of phis added"); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 38 | |
| 39 | // Heuristic for tail duplication. |
| 40 | static cl::opt<unsigned> |
| 41 | TailDuplicateSize("tail-dup-size", |
| 42 | cl::desc("Maximum instructions to consider tail duplicating"), |
| 43 | cl::init(2), cl::Hidden); |
| 44 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 45 | static cl::opt<bool> |
| 46 | TailDupVerify("tail-dup-verify", |
| 47 | cl::desc("Verify sanity of PHI instructions during taildup"), |
| 48 | cl::init(false), cl::Hidden); |
| 49 | |
| 50 | static cl::opt<unsigned> |
| 51 | TailDupLimit("tail-dup-limit", cl::init(~0U), cl::Hidden); |
| 52 | |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 53 | typedef std::vector<std::pair<MachineBasicBlock*,unsigned> > AvailableValsTy; |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 54 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 55 | namespace { |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 56 | /// TailDuplicatePass - Perform tail duplication. |
| 57 | class TailDuplicatePass : public MachineFunctionPass { |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 58 | bool PreRegAlloc; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 59 | const TargetInstrInfo *TII; |
| 60 | MachineModuleInfo *MMI; |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 61 | MachineRegisterInfo *MRI; |
| 62 | |
| 63 | // SSAUpdateVRs - A list of virtual registers for which to update SSA form. |
| 64 | SmallVector<unsigned, 16> SSAUpdateVRs; |
| 65 | |
| 66 | // SSAUpdateVals - For each virtual register in SSAUpdateVals keep a list of |
| 67 | // source virtual registers. |
| 68 | DenseMap<unsigned, AvailableValsTy> SSAUpdateVals; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 69 | |
| 70 | public: |
| 71 | static char ID; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 72 | explicit TailDuplicatePass(bool PreRA) : |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 73 | MachineFunctionPass(ID), PreRegAlloc(PreRA) {} |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 74 | |
| 75 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 76 | virtual const char *getPassName() const { return "Tail Duplication"; } |
| 77 | |
| 78 | private: |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 79 | void AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg, |
| 80 | MachineBasicBlock *BB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 81 | void ProcessPHI(MachineInstr *MI, MachineBasicBlock *TailBB, |
| 82 | MachineBasicBlock *PredBB, |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 83 | DenseMap<unsigned, unsigned> &LocalVRMap, |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 84 | SmallVector<std::pair<unsigned,unsigned>, 4> &Copies, |
| 85 | const DenseSet<unsigned> &UsedByPhi); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 86 | void DuplicateInstruction(MachineInstr *MI, |
| 87 | MachineBasicBlock *TailBB, |
| 88 | MachineBasicBlock *PredBB, |
| 89 | MachineFunction &MF, |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 90 | DenseMap<unsigned, unsigned> &LocalVRMap, |
| 91 | const DenseSet<unsigned> &UsedByPhi); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 92 | void UpdateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead, |
| 93 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
| 94 | SmallSetVector<MachineBasicBlock*, 8> &Succs); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 95 | bool TailDuplicateBlocks(MachineFunction &MF); |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 96 | bool shouldTailDuplicate(const MachineFunction &MF, |
| 97 | MachineBasicBlock &TailBB); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 98 | bool TailDuplicate(MachineBasicBlock *TailBB, MachineFunction &MF, |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 99 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
| 100 | SmallVector<MachineInstr*, 16> &Copies); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 101 | void RemoveDeadBlock(MachineBasicBlock *MBB); |
| 102 | }; |
| 103 | |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 104 | char TailDuplicatePass::ID = 0; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 107 | FunctionPass *llvm::createTailDuplicatePass(bool PreRegAlloc) { |
| 108 | return new TailDuplicatePass(PreRegAlloc); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 111 | bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 112 | TII = MF.getTarget().getInstrInfo(); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 113 | MRI = &MF.getRegInfo(); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 114 | MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 115 | |
| 116 | bool MadeChange = false; |
Jakob Stoklund Olesen | 057d539 | 2010-01-15 19:59:57 +0000 | [diff] [blame] | 117 | while (TailDuplicateBlocks(MF)) |
| 118 | MadeChange = true; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 119 | |
| 120 | return MadeChange; |
| 121 | } |
| 122 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 123 | static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) { |
| 124 | for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ++I) { |
| 125 | MachineBasicBlock *MBB = I; |
| 126 | SmallSetVector<MachineBasicBlock*, 8> Preds(MBB->pred_begin(), |
| 127 | MBB->pred_end()); |
| 128 | MachineBasicBlock::iterator MI = MBB->begin(); |
| 129 | while (MI != MBB->end()) { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 130 | if (!MI->isPHI()) |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 131 | break; |
| 132 | for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(), |
| 133 | PE = Preds.end(); PI != PE; ++PI) { |
| 134 | MachineBasicBlock *PredBB = *PI; |
| 135 | bool Found = false; |
| 136 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { |
| 137 | MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB(); |
| 138 | if (PHIBB == PredBB) { |
| 139 | Found = true; |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | if (!Found) { |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 144 | dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI; |
| 145 | dbgs() << " missing input from predecessor BB#" |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 146 | << PredBB->getNumber() << '\n'; |
| 147 | llvm_unreachable(0); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { |
| 152 | MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB(); |
| 153 | if (CheckExtra && !Preds.count(PHIBB)) { |
| 154 | // This is not a hard error. |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 155 | dbgs() << "Warning: malformed PHI in BB#" << MBB->getNumber() |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 156 | << ": " << *MI; |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 157 | dbgs() << " extra input from predecessor BB#" |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 158 | << PHIBB->getNumber() << '\n'; |
| 159 | } |
| 160 | if (PHIBB->getNumber() < 0) { |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 161 | dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI; |
| 162 | dbgs() << " non-existing BB#" << PHIBB->getNumber() << '\n'; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 163 | llvm_unreachable(0); |
| 164 | } |
| 165 | } |
| 166 | ++MI; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 171 | /// TailDuplicateBlocks - Look for small blocks that are unconditionally |
| 172 | /// branched to and do not fall through. Tail-duplicate their instructions |
| 173 | /// into their predecessors to eliminate (dynamic) branches. |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 174 | bool TailDuplicatePass::TailDuplicateBlocks(MachineFunction &MF) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 175 | bool MadeChange = false; |
| 176 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 177 | if (PreRegAlloc && TailDupVerify) { |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 178 | DEBUG(dbgs() << "\n*** Before tail-duplicating\n"); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 179 | VerifyPHIs(MF, true); |
| 180 | } |
| 181 | |
| 182 | SmallVector<MachineInstr*, 8> NewPHIs; |
| 183 | MachineSSAUpdater SSAUpdate(MF, &NewPHIs); |
| 184 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 185 | for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) { |
| 186 | MachineBasicBlock *MBB = I++; |
| 187 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 188 | if (NumTails == TailDupLimit) |
| 189 | break; |
| 190 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 191 | // Save the successors list. |
| 192 | SmallSetVector<MachineBasicBlock*, 8> Succs(MBB->succ_begin(), |
| 193 | MBB->succ_end()); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 194 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 195 | SmallVector<MachineBasicBlock*, 8> TDBBs; |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 196 | SmallVector<MachineInstr*, 16> Copies; |
| 197 | if (TailDuplicate(MBB, MF, TDBBs, Copies)) { |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 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; |
Rafael Espindola | c2e9a50 | 2011-06-09 20:55:41 +0000 | [diff] [blame] | 244 | if (UseMI->getParent() == DefBB && !UseMI->isPHI()) |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 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 | |
Bob Wilson | bfdcf3b | 2010-01-15 06:29:17 +0000 | [diff] [blame] | 254 | // Eliminate some of the copies inserted by tail duplication to maintain |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 255 | // SSA form. |
| 256 | for (unsigned i = 0, e = Copies.size(); i != e; ++i) { |
| 257 | MachineInstr *Copy = Copies[i]; |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 258 | if (!Copy->isCopy()) |
| 259 | continue; |
| 260 | unsigned Dst = Copy->getOperand(0).getReg(); |
| 261 | unsigned Src = Copy->getOperand(1).getReg(); |
| 262 | MachineRegisterInfo::use_iterator UI = MRI->use_begin(Src); |
| 263 | if (++UI == MRI->use_end()) { |
| 264 | // Copy is the only use. Do trivial copy propagation here. |
| 265 | MRI->replaceRegWith(Dst, Src); |
| 266 | Copy->eraseFromParent(); |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 270 | if (PreRegAlloc && TailDupVerify) |
| 271 | VerifyPHIs(MF, false); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 272 | MadeChange = true; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
Rafael Espindola | d69f85e | 2011-06-08 14:23:19 +0000 | [diff] [blame] | 275 | NumAddedPHIs += NewPHIs.size(); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 276 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 277 | return MadeChange; |
| 278 | } |
| 279 | |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 280 | static bool isDefLiveOut(unsigned Reg, MachineBasicBlock *BB, |
| 281 | const MachineRegisterInfo *MRI) { |
| 282 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg), |
| 283 | UE = MRI->use_end(); UI != UE; ++UI) { |
| 284 | MachineInstr *UseMI = &*UI; |
| 285 | if (UseMI->getParent() != BB) |
| 286 | return true; |
| 287 | } |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB) { |
| 292 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) |
| 293 | if (MI->getOperand(i+1).getMBB() == SrcBB) |
| 294 | return i; |
| 295 | return 0; |
| 296 | } |
| 297 | |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 298 | |
| 299 | // Remember which registers are used by phis in this block. This is |
| 300 | // used to determine which registers are liveout while modifying the |
| 301 | // block (which is why we need to copy the information). |
| 302 | static void getRegsUsedByPHIs(const MachineBasicBlock &BB, |
| 303 | DenseSet<unsigned> *UsedByPhi) { |
| 304 | for(MachineBasicBlock::const_iterator I = BB.begin(), E = BB.end(); |
| 305 | I != E; ++I) { |
| 306 | const MachineInstr &MI = *I; |
| 307 | if (!MI.isPHI()) |
| 308 | break; |
| 309 | for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { |
| 310 | unsigned SrcReg = MI.getOperand(i).getReg(); |
| 311 | UsedByPhi->insert(SrcReg); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 316 | /// AddSSAUpdateEntry - Add a definition and source virtual registers pair for |
| 317 | /// SSA update. |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 318 | void TailDuplicatePass::AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg, |
| 319 | MachineBasicBlock *BB) { |
| 320 | DenseMap<unsigned, AvailableValsTy>::iterator LI= SSAUpdateVals.find(OrigReg); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 321 | if (LI != SSAUpdateVals.end()) |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 322 | LI->second.push_back(std::make_pair(BB, NewReg)); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 323 | else { |
| 324 | AvailableValsTy Vals; |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 325 | Vals.push_back(std::make_pair(BB, NewReg)); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 326 | SSAUpdateVals.insert(std::make_pair(OrigReg, Vals)); |
| 327 | SSAUpdateVRs.push_back(OrigReg); |
| 328 | } |
| 329 | } |
| 330 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 331 | /// ProcessPHI - Process PHI node in TailBB by turning it into a copy in PredBB. |
| 332 | /// Remember the source register that's contributed by PredBB and update SSA |
| 333 | /// update map. |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 334 | void TailDuplicatePass::ProcessPHI(MachineInstr *MI, |
| 335 | MachineBasicBlock *TailBB, |
| 336 | MachineBasicBlock *PredBB, |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 337 | DenseMap<unsigned, unsigned> &LocalVRMap, |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 338 | SmallVector<std::pair<unsigned,unsigned>, 4> &Copies, |
| 339 | const DenseSet<unsigned> &RegsUsedByPhi) { |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 340 | unsigned DefReg = MI->getOperand(0).getReg(); |
| 341 | unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB); |
| 342 | assert(SrcOpIdx && "Unable to find matching PHI source?"); |
| 343 | unsigned SrcReg = MI->getOperand(SrcOpIdx).getReg(); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 344 | const TargetRegisterClass *RC = MRI->getRegClass(DefReg); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 345 | LocalVRMap.insert(std::make_pair(DefReg, SrcReg)); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 346 | |
| 347 | // Insert a copy from source to the end of the block. The def register is the |
| 348 | // available value liveout of the block. |
| 349 | unsigned NewDef = MRI->createVirtualRegister(RC); |
| 350 | Copies.push_back(std::make_pair(NewDef, SrcReg)); |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 351 | if (isDefLiveOut(DefReg, TailBB, MRI) || RegsUsedByPhi.count(DefReg)) |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 352 | AddSSAUpdateEntry(DefReg, NewDef, PredBB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 353 | |
| 354 | // Remove PredBB from the PHI node. |
| 355 | MI->RemoveOperand(SrcOpIdx+1); |
| 356 | MI->RemoveOperand(SrcOpIdx); |
| 357 | if (MI->getNumOperands() == 1) |
| 358 | MI->eraseFromParent(); |
| 359 | } |
| 360 | |
| 361 | /// DuplicateInstruction - Duplicate a TailBB instruction to PredBB and update |
| 362 | /// the source operands due to earlier PHI translation. |
| 363 | void TailDuplicatePass::DuplicateInstruction(MachineInstr *MI, |
| 364 | MachineBasicBlock *TailBB, |
| 365 | MachineBasicBlock *PredBB, |
| 366 | MachineFunction &MF, |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 367 | DenseMap<unsigned, unsigned> &LocalVRMap, |
| 368 | const DenseSet<unsigned> &UsedByPhi) { |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 369 | MachineInstr *NewMI = TII->duplicate(MI, MF); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 370 | for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) { |
| 371 | MachineOperand &MO = NewMI->getOperand(i); |
| 372 | if (!MO.isReg()) |
| 373 | continue; |
| 374 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 375 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 376 | continue; |
| 377 | if (MO.isDef()) { |
| 378 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| 379 | unsigned NewReg = MRI->createVirtualRegister(RC); |
| 380 | MO.setReg(NewReg); |
| 381 | LocalVRMap.insert(std::make_pair(Reg, NewReg)); |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 382 | if (isDefLiveOut(Reg, TailBB, MRI) || UsedByPhi.count(Reg)) |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 383 | AddSSAUpdateEntry(Reg, NewReg, PredBB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 384 | } else { |
| 385 | DenseMap<unsigned, unsigned>::iterator VI = LocalVRMap.find(Reg); |
| 386 | if (VI != LocalVRMap.end()) |
| 387 | MO.setReg(VI->second); |
| 388 | } |
| 389 | } |
| 390 | PredBB->insert(PredBB->end(), NewMI); |
| 391 | } |
| 392 | |
| 393 | /// UpdateSuccessorsPHIs - After FromBB is tail duplicated into its predecessor |
| 394 | /// blocks, the successors have gained new predecessors. Update the PHI |
| 395 | /// instructions in them accordingly. |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 396 | void |
| 397 | TailDuplicatePass::UpdateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead, |
| 398 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 399 | SmallSetVector<MachineBasicBlock*,8> &Succs) { |
| 400 | for (SmallSetVector<MachineBasicBlock*, 8>::iterator SI = Succs.begin(), |
| 401 | SE = Succs.end(); SI != SE; ++SI) { |
| 402 | MachineBasicBlock *SuccBB = *SI; |
| 403 | for (MachineBasicBlock::iterator II = SuccBB->begin(), EE = SuccBB->end(); |
| 404 | II != EE; ++II) { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 405 | if (!II->isPHI()) |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 406 | break; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 407 | unsigned Idx = 0; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 408 | for (unsigned i = 1, e = II->getNumOperands(); i != e; i += 2) { |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 409 | MachineOperand &MO = II->getOperand(i+1); |
| 410 | if (MO.getMBB() == FromBB) { |
| 411 | Idx = i; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 412 | break; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | assert(Idx != 0); |
| 417 | MachineOperand &MO0 = II->getOperand(Idx); |
| 418 | unsigned Reg = MO0.getReg(); |
| 419 | if (isDead) { |
| 420 | // Folded into the previous BB. |
| 421 | // There could be duplicate phi source entries. FIXME: Should sdisel |
| 422 | // or earlier pass fixed this? |
| 423 | for (unsigned i = II->getNumOperands()-2; i != Idx; i -= 2) { |
| 424 | MachineOperand &MO = II->getOperand(i+1); |
| 425 | if (MO.getMBB() == FromBB) { |
| 426 | II->RemoveOperand(i+1); |
| 427 | II->RemoveOperand(i); |
| 428 | } |
| 429 | } |
Jakob Stoklund Olesen | 09eeac9 | 2010-02-11 00:34:33 +0000 | [diff] [blame] | 430 | } else |
| 431 | Idx = 0; |
| 432 | |
| 433 | // If Idx is set, the operands at Idx and Idx+1 must be removed. |
| 434 | // We reuse the location to avoid expensive RemoveOperand calls. |
| 435 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 436 | DenseMap<unsigned,AvailableValsTy>::iterator LI=SSAUpdateVals.find(Reg); |
| 437 | if (LI != SSAUpdateVals.end()) { |
| 438 | // This register is defined in the tail block. |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 439 | for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) { |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 440 | MachineBasicBlock *SrcBB = LI->second[j].first; |
| 441 | unsigned SrcReg = LI->second[j].second; |
Jakob Stoklund Olesen | 09eeac9 | 2010-02-11 00:34:33 +0000 | [diff] [blame] | 442 | if (Idx != 0) { |
| 443 | II->getOperand(Idx).setReg(SrcReg); |
| 444 | II->getOperand(Idx+1).setMBB(SrcBB); |
| 445 | Idx = 0; |
| 446 | } else { |
| 447 | II->addOperand(MachineOperand::CreateReg(SrcReg, false)); |
| 448 | II->addOperand(MachineOperand::CreateMBB(SrcBB)); |
| 449 | } |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 450 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 451 | } else { |
| 452 | // Live in tail block, must also be live in predecessors. |
| 453 | for (unsigned j = 0, ee = TDBBs.size(); j != ee; ++j) { |
| 454 | MachineBasicBlock *SrcBB = TDBBs[j]; |
Jakob Stoklund Olesen | 09eeac9 | 2010-02-11 00:34:33 +0000 | [diff] [blame] | 455 | if (Idx != 0) { |
| 456 | II->getOperand(Idx).setReg(Reg); |
| 457 | II->getOperand(Idx+1).setMBB(SrcBB); |
| 458 | Idx = 0; |
| 459 | } else { |
| 460 | II->addOperand(MachineOperand::CreateReg(Reg, false)); |
| 461 | II->addOperand(MachineOperand::CreateMBB(SrcBB)); |
| 462 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 463 | } |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 464 | } |
Jakob Stoklund Olesen | 09eeac9 | 2010-02-11 00:34:33 +0000 | [diff] [blame] | 465 | if (Idx != 0) { |
| 466 | II->RemoveOperand(Idx+1); |
| 467 | II->RemoveOperand(Idx); |
| 468 | } |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 473 | /// shouldTailDuplicate - Determine if it is profitable to duplicate this block. |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 474 | bool |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 475 | TailDuplicatePass::shouldTailDuplicate(const MachineFunction &MF, |
| 476 | MachineBasicBlock &TailBB) { |
| 477 | // Only duplicate blocks that end with unconditional branches. |
| 478 | if (TailBB.canFallThrough()) |
| 479 | return false; |
| 480 | |
| 481 | // Set the limit on the cost to duplicate. When optimizing for size, |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 482 | // duplicate only one, because one branch instruction can be eliminated to |
| 483 | // compensate for the duplication. |
| 484 | unsigned MaxDuplicateCount; |
Jakob Stoklund Olesen | 8352062 | 2011-01-30 20:38:12 +0000 | [diff] [blame] | 485 | if (TailDuplicateSize.getNumOccurrences() == 0 && |
| 486 | MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize)) |
Bob Wilson | 3858225 | 2009-11-30 18:56:45 +0000 | [diff] [blame] | 487 | MaxDuplicateCount = 1; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 488 | else |
| 489 | MaxDuplicateCount = TailDuplicateSize; |
| 490 | |
Bob Wilson | cb44b28 | 2010-01-16 00:42:25 +0000 | [diff] [blame] | 491 | if (PreRegAlloc) { |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 492 | if (TailBB.empty()) |
Evan Cheng | c3f507f | 2011-01-29 04:46:23 +0000 | [diff] [blame] | 493 | return false; |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 494 | const TargetInstrDesc &TID = TailBB.back().getDesc(); |
Evan Cheng | c3f507f | 2011-01-29 04:46:23 +0000 | [diff] [blame] | 495 | // Pre-regalloc tail duplication hurts compile time and doesn't help |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 496 | // much except for indirect branches. |
| 497 | if (!TID.isIndirectBranch()) |
Bob Wilson | cb44b28 | 2010-01-16 00:42:25 +0000 | [diff] [blame] | 498 | return false; |
| 499 | // If the target has hardware branch prediction that can handle indirect |
| 500 | // branches, duplicating them can often make them predictable when there |
| 501 | // are common paths through the code. The limit needs to be high enough |
| 502 | // to allow undoing the effects of tail merging and other optimizations |
| 503 | // that rearrange the predecessors of the indirect branch. |
| 504 | MaxDuplicateCount = 20; |
| 505 | } |
| 506 | |
Bob Wilson | bfdcf3b | 2010-01-15 06:29:17 +0000 | [diff] [blame] | 507 | // Don't try to tail-duplicate single-block loops. |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 508 | if (TailBB.isSuccessor(&TailBB)) |
Bob Wilson | bfdcf3b | 2010-01-15 06:29:17 +0000 | [diff] [blame] | 509 | return false; |
| 510 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 511 | // Check the instructions in the block to determine whether tail-duplication |
| 512 | // is invalid or unlikely to be profitable. |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame] | 513 | unsigned InstrCount = 0; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 514 | bool HasCall = false; |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 515 | for (MachineBasicBlock::const_iterator I = TailBB.begin(); I != TailBB.end(); |
| 516 | ++I) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 517 | // Non-duplicable things shouldn't be tail-duplicated. |
| 518 | if (I->getDesc().isNotDuplicable()) return false; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 519 | // Do not duplicate 'return' instructions if this is a pre-regalloc run. |
| 520 | // A return may expand into a lot more instructions (e.g. reload of callee |
| 521 | // saved registers) after PEI. |
| 522 | if (PreRegAlloc && I->getDesc().isReturn()) return false; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 523 | // Don't duplicate more than the threshold. |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame] | 524 | if (InstrCount == MaxDuplicateCount) return false; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 525 | // Remember if we saw a call. |
| 526 | if (I->getDesc().isCall()) HasCall = true; |
Devang Patel | cbe1e31 | 2010-03-16 21:02:07 +0000 | [diff] [blame] | 527 | if (!I->isPHI() && !I->isDebugValue()) |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame] | 528 | InstrCount += 1; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 529 | } |
Evan Cheng | c8b90e2 | 2011-02-04 01:10:12 +0000 | [diff] [blame] | 530 | // Don't tail-duplicate calls before register allocation. Calls presents a |
| 531 | // barrier to register allocation so duplicating them may end up increasing |
| 532 | // spills. |
Evan Cheng | c3f507f | 2011-01-29 04:46:23 +0000 | [diff] [blame] | 533 | if (InstrCount > 1 && (PreRegAlloc && HasCall)) |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 534 | return false; |
| 535 | |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 536 | return true; |
| 537 | } |
| 538 | |
| 539 | /// TailDuplicate - If it is profitable, duplicate TailBB's contents in each |
| 540 | /// of its predecessors. |
| 541 | bool |
| 542 | TailDuplicatePass::TailDuplicate(MachineBasicBlock *TailBB, MachineFunction &MF, |
| 543 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
| 544 | SmallVector<MachineInstr*, 16> &Copies) { |
| 545 | if (!shouldTailDuplicate(MF, *TailBB)) |
| 546 | return false; |
| 547 | |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 548 | DEBUG(dbgs() << "\n*** Tail-duplicating BB#" << TailBB->getNumber() << '\n'); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 549 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 550 | // Iterate through all the unique predecessors and tail-duplicate this |
| 551 | // block into them, if possible. Copying the list ahead of time also |
| 552 | // avoids trouble with the predecessor list reallocating. |
| 553 | bool Changed = false; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 554 | SmallSetVector<MachineBasicBlock*, 8> Preds(TailBB->pred_begin(), |
| 555 | TailBB->pred_end()); |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 556 | DenseSet<unsigned> UsedByPhi; |
| 557 | getRegsUsedByPHIs(*TailBB, &UsedByPhi); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 558 | for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(), |
| 559 | PE = Preds.end(); PI != PE; ++PI) { |
| 560 | MachineBasicBlock *PredBB = *PI; |
| 561 | |
| 562 | assert(TailBB != PredBB && |
| 563 | "Single-block loop should have been rejected earlier!"); |
| 564 | if (PredBB->succ_size() > 1) continue; |
| 565 | |
| 566 | MachineBasicBlock *PredTBB, *PredFBB; |
| 567 | SmallVector<MachineOperand, 4> PredCond; |
Rafael Espindola | a899b22 | 2011-06-09 21:43:25 +0000 | [diff] [blame] | 568 | // EH edges are ignored by AnalyzeBranch. |
| 569 | if (PredBB->succ_size() != 1) |
| 570 | continue; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 571 | if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)) |
| 572 | continue; |
| 573 | if (!PredCond.empty()) |
| 574 | continue; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 575 | // Don't duplicate into a fall-through predecessor (at least for now). |
| 576 | if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough()) |
| 577 | continue; |
| 578 | |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 579 | DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 580 | << "From Succ: " << *TailBB); |
| 581 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 582 | TDBBs.push_back(PredBB); |
| 583 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 584 | // Remove PredBB's unconditional branch. |
| 585 | TII->RemoveBranch(*PredBB); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 586 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 587 | // Clone the contents of TailBB into PredBB. |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 588 | DenseMap<unsigned, unsigned> LocalVRMap; |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 589 | SmallVector<std::pair<unsigned,unsigned>, 4> CopyInfos; |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 590 | MachineBasicBlock::iterator I = TailBB->begin(); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 591 | while (I != TailBB->end()) { |
| 592 | MachineInstr *MI = &*I; |
| 593 | ++I; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 594 | if (MI->isPHI()) { |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 595 | // Replace the uses of the def of the PHI with the register coming |
| 596 | // from PredBB. |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 597 | ProcessPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 598 | } else { |
| 599 | // Replace def of virtual registers with new registers, and update |
| 600 | // uses with PHI source register or the new registers. |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 601 | DuplicateInstruction(MI, TailBB, PredBB, MF, LocalVRMap, UsedByPhi); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 602 | } |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 603 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 604 | MachineBasicBlock::iterator Loc = PredBB->getFirstTerminator(); |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 605 | for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) { |
Jakob Stoklund Olesen | 1e1098c | 2010-07-10 22:42:59 +0000 | [diff] [blame] | 606 | Copies.push_back(BuildMI(*PredBB, Loc, DebugLoc(), |
| 607 | TII->get(TargetOpcode::COPY), |
| 608 | CopyInfos[i].first).addReg(CopyInfos[i].second)); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 609 | } |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 610 | NumInstrDups += TailBB->size() - 1; // subtract one for removed branch |
| 611 | |
| 612 | // Update the CFG. |
| 613 | PredBB->removeSuccessor(PredBB->succ_begin()); |
| 614 | assert(PredBB->succ_empty() && |
| 615 | "TailDuplicate called on block with multiple successors!"); |
| 616 | for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(), |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 617 | E = TailBB->succ_end(); I != E; ++I) |
| 618 | PredBB->addSuccessor(*I); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 619 | |
| 620 | Changed = true; |
| 621 | ++NumTailDups; |
| 622 | } |
| 623 | |
| 624 | // If TailBB was duplicated into all its predecessors except for the prior |
| 625 | // block, which falls through unconditionally, move the contents of this |
| 626 | // block into the prior block. |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 627 | MachineBasicBlock *PrevBB = prior(MachineFunction::iterator(TailBB)); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 628 | MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0; |
| 629 | SmallVector<MachineOperand, 4> PriorCond; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 630 | // This has to check PrevBB->succ_size() because EH edges are ignored by |
| 631 | // AnalyzeBranch. |
Rafael Espindola | a899b22 | 2011-06-09 21:43:25 +0000 | [diff] [blame] | 632 | if (PrevBB->succ_size() == 1 && |
| 633 | !TII->AnalyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond, true) && |
| 634 | PriorCond.empty() && !PriorTBB && TailBB->pred_size() == 1 && |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 635 | !TailBB->hasAddressTaken()) { |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 636 | DEBUG(dbgs() << "\nMerging into block: " << *PrevBB |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 637 | << "From MBB: " << *TailBB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 638 | if (PreRegAlloc) { |
| 639 | DenseMap<unsigned, unsigned> LocalVRMap; |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 640 | SmallVector<std::pair<unsigned,unsigned>, 4> CopyInfos; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 641 | MachineBasicBlock::iterator I = TailBB->begin(); |
| 642 | // Process PHI instructions first. |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 643 | while (I != TailBB->end() && I->isPHI()) { |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 644 | // Replace the uses of the def of the PHI with the register coming |
| 645 | // from PredBB. |
| 646 | MachineInstr *MI = &*I++; |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 647 | ProcessPHI(MI, TailBB, PrevBB, LocalVRMap, CopyInfos, UsedByPhi); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 648 | if (MI->getParent()) |
| 649 | MI->eraseFromParent(); |
| 650 | } |
| 651 | |
| 652 | // Now copy the non-PHI instructions. |
| 653 | while (I != TailBB->end()) { |
| 654 | // Replace def of virtual registers with new registers, and update |
| 655 | // uses with PHI source register or the new registers. |
| 656 | MachineInstr *MI = &*I++; |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame^] | 657 | DuplicateInstruction(MI, TailBB, PrevBB, MF, LocalVRMap, UsedByPhi); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 658 | MI->eraseFromParent(); |
| 659 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 660 | MachineBasicBlock::iterator Loc = PrevBB->getFirstTerminator(); |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 661 | for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) { |
Jakob Stoklund Olesen | 1e1098c | 2010-07-10 22:42:59 +0000 | [diff] [blame] | 662 | Copies.push_back(BuildMI(*PrevBB, Loc, DebugLoc(), |
| 663 | TII->get(TargetOpcode::COPY), |
| 664 | CopyInfos[i].first) |
| 665 | .addReg(CopyInfos[i].second)); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 666 | } |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 667 | } else { |
| 668 | // No PHIs to worry about, just splice the instructions over. |
| 669 | PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end()); |
| 670 | } |
| 671 | PrevBB->removeSuccessor(PrevBB->succ_begin()); |
| 672 | assert(PrevBB->succ_empty()); |
| 673 | PrevBB->transferSuccessors(TailBB); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 674 | TDBBs.push_back(PrevBB); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 675 | Changed = true; |
| 676 | } |
| 677 | |
| 678 | return Changed; |
| 679 | } |
| 680 | |
| 681 | /// RemoveDeadBlock - Remove the specified dead machine basic block from the |
| 682 | /// function, updating the CFG. |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 683 | void TailDuplicatePass::RemoveDeadBlock(MachineBasicBlock *MBB) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 684 | assert(MBB->pred_empty() && "MBB must be dead!"); |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 685 | DEBUG(dbgs() << "\nRemoving MBB: " << *MBB); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 686 | |
| 687 | // Remove all successors. |
| 688 | while (!MBB->succ_empty()) |
| 689 | MBB->removeSuccessor(MBB->succ_end()-1); |
| 690 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 691 | // Remove the block. |
| 692 | MBB->eraseFromParent(); |
| 693 | } |
| 694 | |