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