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, |
Rafael Espindola | 689d7d5 | 2011-06-09 23:22:56 +0000 | [diff] [blame] | 85 | const DenseSet<unsigned> &UsedByPhi, |
| 86 | bool Remove); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 87 | void DuplicateInstruction(MachineInstr *MI, |
| 88 | MachineBasicBlock *TailBB, |
| 89 | MachineBasicBlock *PredBB, |
| 90 | MachineFunction &MF, |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame] | 91 | DenseMap<unsigned, unsigned> &LocalVRMap, |
| 92 | const DenseSet<unsigned> &UsedByPhi); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 93 | void UpdateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead, |
| 94 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
| 95 | SmallSetVector<MachineBasicBlock*, 8> &Succs); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 96 | bool TailDuplicateBlocks(MachineFunction &MF); |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 97 | bool shouldTailDuplicate(const MachineFunction &MF, |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 98 | bool IsSimple, MachineBasicBlock &TailBB); |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 99 | bool isSimpleBB(MachineBasicBlock *TailBB); |
Rafael Espindola | 40179bf | 2011-06-24 15:50:56 +0000 | [diff] [blame] | 100 | bool canCompletelyDuplicateBB(MachineBasicBlock &BB); |
Rafael Espindola | d7f35fa | 2011-06-24 15:47:41 +0000 | [diff] [blame] | 101 | bool duplicateSimpleBB(MachineBasicBlock *TailBB, |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 102 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
| 103 | const DenseSet<unsigned> &RegsUsedByPhi, |
| 104 | SmallVector<MachineInstr*, 16> &Copies); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 105 | bool TailDuplicate(MachineBasicBlock *TailBB, MachineFunction &MF, |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 106 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
| 107 | SmallVector<MachineInstr*, 16> &Copies); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 108 | void RemoveDeadBlock(MachineBasicBlock *MBB); |
| 109 | }; |
| 110 | |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 111 | char TailDuplicatePass::ID = 0; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 114 | FunctionPass *llvm::createTailDuplicatePass(bool PreRegAlloc) { |
| 115 | return new TailDuplicatePass(PreRegAlloc); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 118 | bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 119 | TII = MF.getTarget().getInstrInfo(); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 120 | MRI = &MF.getRegInfo(); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 121 | MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 122 | |
| 123 | bool MadeChange = false; |
Jakob Stoklund Olesen | 057d539 | 2010-01-15 19:59:57 +0000 | [diff] [blame] | 124 | while (TailDuplicateBlocks(MF)) |
| 125 | MadeChange = true; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 126 | |
| 127 | return MadeChange; |
| 128 | } |
| 129 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 130 | static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) { |
| 131 | for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ++I) { |
| 132 | MachineBasicBlock *MBB = I; |
| 133 | SmallSetVector<MachineBasicBlock*, 8> Preds(MBB->pred_begin(), |
| 134 | MBB->pred_end()); |
| 135 | MachineBasicBlock::iterator MI = MBB->begin(); |
| 136 | while (MI != MBB->end()) { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 137 | if (!MI->isPHI()) |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 138 | break; |
| 139 | for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(), |
| 140 | PE = Preds.end(); PI != PE; ++PI) { |
| 141 | MachineBasicBlock *PredBB = *PI; |
| 142 | bool Found = false; |
| 143 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { |
| 144 | MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB(); |
| 145 | if (PHIBB == PredBB) { |
| 146 | Found = true; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | if (!Found) { |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 151 | dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI; |
| 152 | dbgs() << " missing input from predecessor BB#" |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 153 | << PredBB->getNumber() << '\n'; |
| 154 | llvm_unreachable(0); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { |
| 159 | MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB(); |
| 160 | if (CheckExtra && !Preds.count(PHIBB)) { |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 161 | dbgs() << "Warning: malformed PHI in BB#" << MBB->getNumber() |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 162 | << ": " << *MI; |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 163 | dbgs() << " extra input from predecessor BB#" |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 164 | << PHIBB->getNumber() << '\n'; |
Rafael Espindola | d3f4eea | 2011-06-09 23:55:56 +0000 | [diff] [blame] | 165 | llvm_unreachable(0); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 166 | } |
| 167 | if (PHIBB->getNumber() < 0) { |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 168 | dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI; |
| 169 | dbgs() << " non-existing BB#" << PHIBB->getNumber() << '\n'; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 170 | llvm_unreachable(0); |
| 171 | } |
| 172 | } |
| 173 | ++MI; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 178 | /// TailDuplicateBlocks - Look for small blocks that are unconditionally |
| 179 | /// branched to and do not fall through. Tail-duplicate their instructions |
| 180 | /// into their predecessors to eliminate (dynamic) branches. |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 181 | bool TailDuplicatePass::TailDuplicateBlocks(MachineFunction &MF) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 182 | bool MadeChange = false; |
| 183 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 184 | if (PreRegAlloc && TailDupVerify) { |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 185 | DEBUG(dbgs() << "\n*** Before tail-duplicating\n"); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 186 | VerifyPHIs(MF, true); |
| 187 | } |
| 188 | |
| 189 | SmallVector<MachineInstr*, 8> NewPHIs; |
| 190 | MachineSSAUpdater SSAUpdate(MF, &NewPHIs); |
| 191 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 192 | for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) { |
| 193 | MachineBasicBlock *MBB = I++; |
| 194 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 195 | if (NumTails == TailDupLimit) |
| 196 | break; |
| 197 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 198 | // Save the successors list. |
| 199 | SmallSetVector<MachineBasicBlock*, 8> Succs(MBB->succ_begin(), |
| 200 | MBB->succ_end()); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 201 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 202 | SmallVector<MachineBasicBlock*, 8> TDBBs; |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 203 | SmallVector<MachineInstr*, 16> Copies; |
Rafael Espindola | c0af352 | 2011-07-04 00:13:36 +0000 | [diff] [blame^] | 204 | if (!TailDuplicate(MBB, MF, TDBBs, Copies)) |
| 205 | continue; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 206 | |
Rafael Espindola | c0af352 | 2011-07-04 00:13:36 +0000 | [diff] [blame^] | 207 | ++NumTails; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 208 | |
Rafael Espindola | c0af352 | 2011-07-04 00:13:36 +0000 | [diff] [blame^] | 209 | // TailBB's immediate successors are now successors of those predecessors |
| 210 | // which duplicated TailBB. Add the predecessors as sources to the PHI |
| 211 | // instructions. |
| 212 | bool isDead = MBB->pred_empty() && !MBB->hasAddressTaken(); |
| 213 | if (PreRegAlloc) |
| 214 | UpdateSuccessorsPHIs(MBB, isDead, TDBBs, Succs); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 215 | |
Rafael Espindola | c0af352 | 2011-07-04 00:13:36 +0000 | [diff] [blame^] | 216 | // If it is dead, remove it. |
| 217 | if (isDead) { |
| 218 | NumInstrDups -= MBB->size(); |
| 219 | RemoveDeadBlock(MBB); |
| 220 | ++NumDeadBlocks; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 221 | } |
Rafael Espindola | c0af352 | 2011-07-04 00:13:36 +0000 | [diff] [blame^] | 222 | |
| 223 | // Update SSA form. |
| 224 | if (!SSAUpdateVRs.empty()) { |
| 225 | NewPHIs.clear(); |
| 226 | for (unsigned i = 0, e = SSAUpdateVRs.size(); i != e; ++i) { |
| 227 | unsigned VReg = SSAUpdateVRs[i]; |
| 228 | SSAUpdate.Initialize(VReg); |
| 229 | |
| 230 | // If the original definition is still around, add it as an available |
| 231 | // value. |
| 232 | MachineInstr *DefMI = MRI->getVRegDef(VReg); |
| 233 | MachineBasicBlock *DefBB = 0; |
| 234 | if (DefMI) { |
| 235 | DefBB = DefMI->getParent(); |
| 236 | SSAUpdate.AddAvailableValue(DefBB, VReg); |
| 237 | } |
| 238 | |
| 239 | // Add the new vregs as available values. |
| 240 | DenseMap<unsigned, AvailableValsTy>::iterator LI = |
| 241 | SSAUpdateVals.find(VReg); |
| 242 | for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) { |
| 243 | MachineBasicBlock *SrcBB = LI->second[j].first; |
| 244 | unsigned SrcReg = LI->second[j].second; |
| 245 | SSAUpdate.AddAvailableValue(SrcBB, SrcReg); |
| 246 | } |
| 247 | |
| 248 | // Rewrite uses that are outside of the original def's block. |
| 249 | MachineRegisterInfo::use_iterator UI = MRI->use_begin(VReg); |
| 250 | while (UI != MRI->use_end()) { |
| 251 | MachineOperand &UseMO = UI.getOperand(); |
| 252 | MachineInstr *UseMI = &*UI; |
| 253 | ++UI; |
| 254 | if (UseMI->isDebugValue()) { |
| 255 | // SSAUpdate can replace the use with an undef. That creates |
| 256 | // a debug instruction that is a kill. |
| 257 | // FIXME: Should it SSAUpdate job to delete debug instructions |
| 258 | // instead of replacing the use with undef? |
| 259 | UseMI->eraseFromParent(); |
| 260 | continue; |
| 261 | } |
| 262 | if (UseMI->getParent() == DefBB && !UseMI->isPHI()) |
| 263 | continue; |
| 264 | SSAUpdate.RewriteUse(UseMO); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | SSAUpdateVRs.clear(); |
| 269 | SSAUpdateVals.clear(); |
| 270 | } |
| 271 | |
| 272 | // Eliminate some of the copies inserted by tail duplication to maintain |
| 273 | // SSA form. |
| 274 | for (unsigned i = 0, e = Copies.size(); i != e; ++i) { |
| 275 | MachineInstr *Copy = Copies[i]; |
| 276 | if (!Copy->isCopy()) |
| 277 | continue; |
| 278 | unsigned Dst = Copy->getOperand(0).getReg(); |
| 279 | unsigned Src = Copy->getOperand(1).getReg(); |
| 280 | MachineRegisterInfo::use_iterator UI = MRI->use_begin(Src); |
| 281 | if (++UI == MRI->use_end()) { |
| 282 | // Copy is the only use. Do trivial copy propagation here. |
| 283 | MRI->replaceRegWith(Dst, Src); |
| 284 | Copy->eraseFromParent(); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (PreRegAlloc && TailDupVerify) |
| 289 | VerifyPHIs(MF, false); |
| 290 | MadeChange = true; |
| 291 | |
| 292 | if (NewPHIs.size()) |
| 293 | NumAddedPHIs += NewPHIs.size(); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 294 | } |
Rafael Espindola | c0af352 | 2011-07-04 00:13:36 +0000 | [diff] [blame^] | 295 | |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 296 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 297 | return MadeChange; |
| 298 | } |
| 299 | |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 300 | static bool isDefLiveOut(unsigned Reg, MachineBasicBlock *BB, |
| 301 | const MachineRegisterInfo *MRI) { |
| 302 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg), |
| 303 | UE = MRI->use_end(); UI != UE; ++UI) { |
| 304 | MachineInstr *UseMI = &*UI; |
Rafael Espindola | db3983b | 2011-06-17 13:59:43 +0000 | [diff] [blame] | 305 | if (UseMI->isDebugValue()) |
| 306 | continue; |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 307 | if (UseMI->getParent() != BB) |
| 308 | return true; |
| 309 | } |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB) { |
| 314 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) |
| 315 | if (MI->getOperand(i+1).getMBB() == SrcBB) |
| 316 | return i; |
| 317 | return 0; |
| 318 | } |
| 319 | |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame] | 320 | |
| 321 | // Remember which registers are used by phis in this block. This is |
| 322 | // used to determine which registers are liveout while modifying the |
| 323 | // block (which is why we need to copy the information). |
| 324 | static void getRegsUsedByPHIs(const MachineBasicBlock &BB, |
Rafael Espindola | 33b4658 | 2011-06-10 21:01:53 +0000 | [diff] [blame] | 325 | DenseSet<unsigned> *UsedByPhi) { |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame] | 326 | for(MachineBasicBlock::const_iterator I = BB.begin(), E = BB.end(); |
| 327 | I != E; ++I) { |
| 328 | const MachineInstr &MI = *I; |
| 329 | if (!MI.isPHI()) |
| 330 | break; |
| 331 | for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { |
| 332 | unsigned SrcReg = MI.getOperand(i).getReg(); |
| 333 | UsedByPhi->insert(SrcReg); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 338 | /// AddSSAUpdateEntry - Add a definition and source virtual registers pair for |
| 339 | /// SSA update. |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 340 | void TailDuplicatePass::AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg, |
| 341 | MachineBasicBlock *BB) { |
| 342 | DenseMap<unsigned, AvailableValsTy>::iterator LI= SSAUpdateVals.find(OrigReg); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 343 | if (LI != SSAUpdateVals.end()) |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 344 | LI->second.push_back(std::make_pair(BB, NewReg)); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 345 | else { |
| 346 | AvailableValsTy Vals; |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 347 | Vals.push_back(std::make_pair(BB, NewReg)); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 348 | SSAUpdateVals.insert(std::make_pair(OrigReg, Vals)); |
| 349 | SSAUpdateVRs.push_back(OrigReg); |
| 350 | } |
| 351 | } |
| 352 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 353 | /// ProcessPHI - Process PHI node in TailBB by turning it into a copy in PredBB. |
| 354 | /// Remember the source register that's contributed by PredBB and update SSA |
| 355 | /// update map. |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 356 | void TailDuplicatePass::ProcessPHI(MachineInstr *MI, |
| 357 | MachineBasicBlock *TailBB, |
| 358 | MachineBasicBlock *PredBB, |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 359 | DenseMap<unsigned, unsigned> &LocalVRMap, |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame] | 360 | SmallVector<std::pair<unsigned,unsigned>, 4> &Copies, |
Rafael Espindola | 33b4658 | 2011-06-10 21:01:53 +0000 | [diff] [blame] | 361 | const DenseSet<unsigned> &RegsUsedByPhi, |
Rafael Espindola | 689d7d5 | 2011-06-09 23:22:56 +0000 | [diff] [blame] | 362 | bool Remove) { |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 363 | unsigned DefReg = MI->getOperand(0).getReg(); |
| 364 | unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB); |
| 365 | assert(SrcOpIdx && "Unable to find matching PHI source?"); |
| 366 | unsigned SrcReg = MI->getOperand(SrcOpIdx).getReg(); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 367 | const TargetRegisterClass *RC = MRI->getRegClass(DefReg); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 368 | LocalVRMap.insert(std::make_pair(DefReg, SrcReg)); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 369 | |
| 370 | // Insert a copy from source to the end of the block. The def register is the |
| 371 | // available value liveout of the block. |
| 372 | unsigned NewDef = MRI->createVirtualRegister(RC); |
| 373 | Copies.push_back(std::make_pair(NewDef, SrcReg)); |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame] | 374 | if (isDefLiveOut(DefReg, TailBB, MRI) || RegsUsedByPhi.count(DefReg)) |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 375 | AddSSAUpdateEntry(DefReg, NewDef, PredBB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 376 | |
Rafael Espindola | 689d7d5 | 2011-06-09 23:22:56 +0000 | [diff] [blame] | 377 | if (!Remove) |
| 378 | return; |
| 379 | |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 380 | // Remove PredBB from the PHI node. |
| 381 | MI->RemoveOperand(SrcOpIdx+1); |
| 382 | MI->RemoveOperand(SrcOpIdx); |
| 383 | if (MI->getNumOperands() == 1) |
| 384 | MI->eraseFromParent(); |
| 385 | } |
| 386 | |
| 387 | /// DuplicateInstruction - Duplicate a TailBB instruction to PredBB and update |
| 388 | /// the source operands due to earlier PHI translation. |
| 389 | void TailDuplicatePass::DuplicateInstruction(MachineInstr *MI, |
| 390 | MachineBasicBlock *TailBB, |
| 391 | MachineBasicBlock *PredBB, |
| 392 | MachineFunction &MF, |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame] | 393 | DenseMap<unsigned, unsigned> &LocalVRMap, |
| 394 | const DenseSet<unsigned> &UsedByPhi) { |
Jakob Stoklund Olesen | 30ac046 | 2010-01-06 23:47:07 +0000 | [diff] [blame] | 395 | MachineInstr *NewMI = TII->duplicate(MI, MF); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 396 | for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) { |
| 397 | MachineOperand &MO = NewMI->getOperand(i); |
| 398 | if (!MO.isReg()) |
| 399 | continue; |
| 400 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 401 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 402 | continue; |
| 403 | if (MO.isDef()) { |
| 404 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| 405 | unsigned NewReg = MRI->createVirtualRegister(RC); |
| 406 | MO.setReg(NewReg); |
| 407 | LocalVRMap.insert(std::make_pair(Reg, NewReg)); |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame] | 408 | if (isDefLiveOut(Reg, TailBB, MRI) || UsedByPhi.count(Reg)) |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 409 | AddSSAUpdateEntry(Reg, NewReg, PredBB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 410 | } else { |
| 411 | DenseMap<unsigned, unsigned>::iterator VI = LocalVRMap.find(Reg); |
| 412 | if (VI != LocalVRMap.end()) |
| 413 | MO.setReg(VI->second); |
| 414 | } |
| 415 | } |
| 416 | PredBB->insert(PredBB->end(), NewMI); |
| 417 | } |
| 418 | |
| 419 | /// UpdateSuccessorsPHIs - After FromBB is tail duplicated into its predecessor |
| 420 | /// blocks, the successors have gained new predecessors. Update the PHI |
| 421 | /// instructions in them accordingly. |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 422 | void |
| 423 | TailDuplicatePass::UpdateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead, |
| 424 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 425 | SmallSetVector<MachineBasicBlock*,8> &Succs) { |
| 426 | for (SmallSetVector<MachineBasicBlock*, 8>::iterator SI = Succs.begin(), |
| 427 | SE = Succs.end(); SI != SE; ++SI) { |
| 428 | MachineBasicBlock *SuccBB = *SI; |
| 429 | for (MachineBasicBlock::iterator II = SuccBB->begin(), EE = SuccBB->end(); |
| 430 | II != EE; ++II) { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 431 | if (!II->isPHI()) |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 432 | break; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 433 | unsigned Idx = 0; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 434 | for (unsigned i = 1, e = II->getNumOperands(); i != e; i += 2) { |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 435 | MachineOperand &MO = II->getOperand(i+1); |
| 436 | if (MO.getMBB() == FromBB) { |
| 437 | Idx = i; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 438 | break; |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | assert(Idx != 0); |
| 443 | MachineOperand &MO0 = II->getOperand(Idx); |
| 444 | unsigned Reg = MO0.getReg(); |
| 445 | if (isDead) { |
| 446 | // Folded into the previous BB. |
| 447 | // There could be duplicate phi source entries. FIXME: Should sdisel |
| 448 | // or earlier pass fixed this? |
| 449 | for (unsigned i = II->getNumOperands()-2; i != Idx; i -= 2) { |
| 450 | MachineOperand &MO = II->getOperand(i+1); |
| 451 | if (MO.getMBB() == FromBB) { |
| 452 | II->RemoveOperand(i+1); |
| 453 | II->RemoveOperand(i); |
| 454 | } |
| 455 | } |
Jakob Stoklund Olesen | 09eeac9 | 2010-02-11 00:34:33 +0000 | [diff] [blame] | 456 | } else |
| 457 | Idx = 0; |
| 458 | |
| 459 | // If Idx is set, the operands at Idx and Idx+1 must be removed. |
| 460 | // We reuse the location to avoid expensive RemoveOperand calls. |
| 461 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 462 | DenseMap<unsigned,AvailableValsTy>::iterator LI=SSAUpdateVals.find(Reg); |
| 463 | if (LI != SSAUpdateVals.end()) { |
| 464 | // This register is defined in the tail block. |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 465 | for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) { |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 466 | MachineBasicBlock *SrcBB = LI->second[j].first; |
Rafael Espindola | d3f4eea | 2011-06-09 23:55:56 +0000 | [diff] [blame] | 467 | // If we didn't duplicate a bb into a particular predecessor, we |
| 468 | // might still have added an entry to SSAUpdateVals to correcly |
| 469 | // recompute SSA. If that case, avoid adding a dummy extra argument |
| 470 | // this PHI. |
| 471 | if (!SrcBB->isSuccessor(SuccBB)) |
| 472 | continue; |
| 473 | |
Evan Cheng | 11572ba | 2009-12-04 19:09:10 +0000 | [diff] [blame] | 474 | unsigned SrcReg = LI->second[j].second; |
Jakob Stoklund Olesen | 09eeac9 | 2010-02-11 00:34:33 +0000 | [diff] [blame] | 475 | if (Idx != 0) { |
| 476 | II->getOperand(Idx).setReg(SrcReg); |
| 477 | II->getOperand(Idx+1).setMBB(SrcBB); |
| 478 | Idx = 0; |
| 479 | } else { |
| 480 | II->addOperand(MachineOperand::CreateReg(SrcReg, false)); |
| 481 | II->addOperand(MachineOperand::CreateMBB(SrcBB)); |
| 482 | } |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 483 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 484 | } else { |
| 485 | // Live in tail block, must also be live in predecessors. |
| 486 | for (unsigned j = 0, ee = TDBBs.size(); j != ee; ++j) { |
| 487 | MachineBasicBlock *SrcBB = TDBBs[j]; |
Jakob Stoklund Olesen | 09eeac9 | 2010-02-11 00:34:33 +0000 | [diff] [blame] | 488 | if (Idx != 0) { |
| 489 | II->getOperand(Idx).setReg(Reg); |
| 490 | II->getOperand(Idx+1).setMBB(SrcBB); |
| 491 | Idx = 0; |
| 492 | } else { |
| 493 | II->addOperand(MachineOperand::CreateReg(Reg, false)); |
| 494 | II->addOperand(MachineOperand::CreateMBB(SrcBB)); |
| 495 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 496 | } |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 497 | } |
Jakob Stoklund Olesen | 09eeac9 | 2010-02-11 00:34:33 +0000 | [diff] [blame] | 498 | if (Idx != 0) { |
| 499 | II->RemoveOperand(Idx+1); |
| 500 | II->RemoveOperand(Idx); |
| 501 | } |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 506 | /// shouldTailDuplicate - Determine if it is profitable to duplicate this block. |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 507 | bool |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 508 | TailDuplicatePass::shouldTailDuplicate(const MachineFunction &MF, |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 509 | bool IsSimple, |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 510 | MachineBasicBlock &TailBB) { |
| 511 | // Only duplicate blocks that end with unconditional branches. |
| 512 | if (TailBB.canFallThrough()) |
| 513 | return false; |
| 514 | |
Rafael Espindola | ec324e5 | 2011-06-17 05:54:50 +0000 | [diff] [blame] | 515 | // Don't try to tail-duplicate single-block loops. |
| 516 | if (TailBB.isSuccessor(&TailBB)) |
| 517 | return false; |
| 518 | |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 519 | // Set the limit on the cost to duplicate. When optimizing for size, |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 520 | // duplicate only one, because one branch instruction can be eliminated to |
| 521 | // compensate for the duplication. |
| 522 | unsigned MaxDuplicateCount; |
Jakob Stoklund Olesen | 8352062 | 2011-01-30 20:38:12 +0000 | [diff] [blame] | 523 | if (TailDuplicateSize.getNumOccurrences() == 0 && |
| 524 | MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize)) |
Bob Wilson | 3858225 | 2009-11-30 18:56:45 +0000 | [diff] [blame] | 525 | MaxDuplicateCount = 1; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 526 | else |
| 527 | MaxDuplicateCount = TailDuplicateSize; |
| 528 | |
Rafael Espindola | ec324e5 | 2011-06-17 05:54:50 +0000 | [diff] [blame] | 529 | // If the target has hardware branch prediction that can handle indirect |
| 530 | // branches, duplicating them can often make them predictable when there |
| 531 | // are common paths through the code. The limit needs to be high enough |
| 532 | // to allow undoing the effects of tail merging and other optimizations |
| 533 | // that rearrange the predecessors of the indirect branch. |
Bob Wilson | cb44b28 | 2010-01-16 00:42:25 +0000 | [diff] [blame] | 534 | |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 535 | bool hasIndirectBR = false; |
Rafael Espindola | ec324e5 | 2011-06-17 05:54:50 +0000 | [diff] [blame] | 536 | if (PreRegAlloc && !TailBB.empty()) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 537 | const MCInstrDesc &MCID = TailBB.back().getDesc(); |
| 538 | if (MCID.isIndirectBranch()) { |
Rafael Espindola | ec324e5 | 2011-06-17 05:54:50 +0000 | [diff] [blame] | 539 | MaxDuplicateCount = 20; |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 540 | hasIndirectBR = true; |
| 541 | } |
Rafael Espindola | ec324e5 | 2011-06-17 05:54:50 +0000 | [diff] [blame] | 542 | } |
Bob Wilson | bfdcf3b | 2010-01-15 06:29:17 +0000 | [diff] [blame] | 543 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 544 | // Check the instructions in the block to determine whether tail-duplication |
| 545 | // is invalid or unlikely to be profitable. |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame] | 546 | unsigned InstrCount = 0; |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 547 | for (MachineBasicBlock::const_iterator I = TailBB.begin(); I != TailBB.end(); |
| 548 | ++I) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 549 | // Non-duplicable things shouldn't be tail-duplicated. |
Rafael Espindola | ec324e5 | 2011-06-17 05:54:50 +0000 | [diff] [blame] | 550 | if (I->getDesc().isNotDuplicable()) |
| 551 | return false; |
| 552 | |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 553 | // Do not duplicate 'return' instructions if this is a pre-regalloc run. |
| 554 | // A return may expand into a lot more instructions (e.g. reload of callee |
| 555 | // saved registers) after PEI. |
Rafael Espindola | ec324e5 | 2011-06-17 05:54:50 +0000 | [diff] [blame] | 556 | if (PreRegAlloc && I->getDesc().isReturn()) |
| 557 | return false; |
| 558 | |
| 559 | // Avoid duplicating calls before register allocation. Calls presents a |
| 560 | // barrier to register allocation so duplicating them may end up increasing |
| 561 | // spills. |
| 562 | if (PreRegAlloc && I->getDesc().isCall()) |
| 563 | return false; |
| 564 | |
Devang Patel | cbe1e31 | 2010-03-16 21:02:07 +0000 | [diff] [blame] | 565 | if (!I->isPHI() && !I->isDebugValue()) |
Bob Wilson | f1e01dc | 2009-12-02 17:15:24 +0000 | [diff] [blame] | 566 | InstrCount += 1; |
Rafael Espindola | ec324e5 | 2011-06-17 05:54:50 +0000 | [diff] [blame] | 567 | |
| 568 | if (InstrCount > MaxDuplicateCount) |
| 569 | return false; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 570 | } |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 571 | |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 572 | if (hasIndirectBR) |
| 573 | return true; |
| 574 | |
| 575 | if (IsSimple) |
Rafael Espindola | d7f35fa | 2011-06-24 15:47:41 +0000 | [diff] [blame] | 576 | return true; |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 577 | |
| 578 | if (!PreRegAlloc) |
| 579 | return true; |
| 580 | |
Rafael Espindola | 40179bf | 2011-06-24 15:50:56 +0000 | [diff] [blame] | 581 | return canCompletelyDuplicateBB(TailBB); |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 584 | /// isSimpleBB - True if this BB has only one unconditional jump. |
| 585 | bool |
| 586 | TailDuplicatePass::isSimpleBB(MachineBasicBlock *TailBB) { |
| 587 | if (TailBB->succ_size() != 1) |
| 588 | return false; |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 589 | if (TailBB->pred_empty()) |
| 590 | return false; |
Rafael Espindola | d6379a9 | 2011-06-22 22:31:57 +0000 | [diff] [blame] | 591 | MachineBasicBlock::iterator I = TailBB->begin(); |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 592 | MachineBasicBlock::iterator E = TailBB->end(); |
Rafael Espindola | d6379a9 | 2011-06-22 22:31:57 +0000 | [diff] [blame] | 593 | while (I != E && I->isDebugValue()) |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 594 | ++I; |
| 595 | if (I == E) |
| 596 | return true; |
| 597 | return I->getDesc().isUnconditionalBranch(); |
| 598 | } |
| 599 | |
| 600 | static bool |
| 601 | bothUsedInPHI(const MachineBasicBlock &A, |
| 602 | SmallPtrSet<MachineBasicBlock*, 8> SuccsB) { |
| 603 | for (MachineBasicBlock::const_succ_iterator SI = A.succ_begin(), |
| 604 | SE = A.succ_end(); SI != SE; ++SI) { |
| 605 | MachineBasicBlock *BB = *SI; |
| 606 | if (SuccsB.count(BB) && !BB->empty() && BB->begin()->isPHI()) |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | bool |
Rafael Espindola | 40179bf | 2011-06-24 15:50:56 +0000 | [diff] [blame] | 614 | TailDuplicatePass::canCompletelyDuplicateBB(MachineBasicBlock &BB) { |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 615 | SmallPtrSet<MachineBasicBlock*, 8> Succs(BB.succ_begin(), BB.succ_end()); |
| 616 | |
| 617 | for (MachineBasicBlock::pred_iterator PI = BB.pred_begin(), |
| 618 | PE = BB.pred_end(); PI != PE; ++PI) { |
| 619 | MachineBasicBlock *PredBB = *PI; |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 620 | |
Rafael Espindola | 40179bf | 2011-06-24 15:50:56 +0000 | [diff] [blame] | 621 | if (PredBB->succ_size() > 1) |
| 622 | return false; |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 623 | |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 624 | MachineBasicBlock *PredTBB = NULL, *PredFBB = NULL; |
| 625 | SmallVector<MachineOperand, 4> PredCond; |
| 626 | if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)) |
| 627 | return false; |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 628 | |
Rafael Espindola | 40179bf | 2011-06-24 15:50:56 +0000 | [diff] [blame] | 629 | if (!PredCond.empty()) |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 630 | return false; |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 631 | } |
| 632 | return true; |
| 633 | } |
| 634 | |
Rafael Espindola | d7f35fa | 2011-06-24 15:47:41 +0000 | [diff] [blame] | 635 | bool |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 636 | TailDuplicatePass::duplicateSimpleBB(MachineBasicBlock *TailBB, |
| 637 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
| 638 | const DenseSet<unsigned> &UsedByPhi, |
| 639 | SmallVector<MachineInstr*, 16> &Copies) { |
Rafael Espindola | d7f35fa | 2011-06-24 15:47:41 +0000 | [diff] [blame] | 640 | SmallPtrSet<MachineBasicBlock*, 8> Succs(TailBB->succ_begin(), |
| 641 | TailBB->succ_end()); |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 642 | SmallVector<MachineBasicBlock*, 8> Preds(TailBB->pred_begin(), |
| 643 | TailBB->pred_end()); |
Rafael Espindola | d7f35fa | 2011-06-24 15:47:41 +0000 | [diff] [blame] | 644 | bool Changed = false; |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 645 | for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(), |
| 646 | PE = Preds.end(); PI != PE; ++PI) { |
| 647 | MachineBasicBlock *PredBB = *PI; |
| 648 | |
Rafael Espindola | d7f35fa | 2011-06-24 15:47:41 +0000 | [diff] [blame] | 649 | if (PredBB->getLandingPadSuccessor()) |
| 650 | continue; |
| 651 | |
| 652 | if (bothUsedInPHI(*PredBB, Succs)) |
| 653 | continue; |
| 654 | |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 655 | MachineBasicBlock *PredTBB = NULL, *PredFBB = NULL; |
| 656 | SmallVector<MachineOperand, 4> PredCond; |
Rafael Espindola | d7f35fa | 2011-06-24 15:47:41 +0000 | [diff] [blame] | 657 | if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)) |
| 658 | continue; |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 659 | |
Rafael Espindola | d7f35fa | 2011-06-24 15:47:41 +0000 | [diff] [blame] | 660 | Changed = true; |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 661 | DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB |
| 662 | << "From simple Succ: " << *TailBB); |
| 663 | |
| 664 | MachineBasicBlock *NewTarget = *TailBB->succ_begin(); |
Francois Pichet | 289a279 | 2011-06-20 05:19:37 +0000 | [diff] [blame] | 665 | MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(PredBB)); |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 666 | |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 667 | // Make PredFBB explicit. |
| 668 | if (PredCond.empty()) |
| 669 | PredFBB = PredTBB; |
| 670 | |
| 671 | // Make fall through explicit. |
| 672 | if (!PredTBB) |
| 673 | PredTBB = NextBB; |
| 674 | if (!PredFBB) |
| 675 | PredFBB = NextBB; |
| 676 | |
| 677 | // Redirect |
| 678 | if (PredFBB == TailBB) |
| 679 | PredFBB = NewTarget; |
| 680 | if (PredTBB == TailBB) |
| 681 | PredTBB = NewTarget; |
| 682 | |
| 683 | // Make the branch unconditional if possible |
Rafael Espindola | 689c247 | 2011-06-20 14:11:42 +0000 | [diff] [blame] | 684 | if (PredTBB == PredFBB) { |
| 685 | PredCond.clear(); |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 686 | PredFBB = NULL; |
Rafael Espindola | 689c247 | 2011-06-20 14:11:42 +0000 | [diff] [blame] | 687 | } |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 688 | |
| 689 | // Avoid adding fall through branches. |
| 690 | if (PredFBB == NextBB) |
| 691 | PredFBB = NULL; |
| 692 | if (PredTBB == NextBB && PredFBB == NULL) |
| 693 | PredTBB = NULL; |
| 694 | |
| 695 | TII->RemoveBranch(*PredBB); |
| 696 | |
| 697 | if (PredTBB) |
| 698 | TII->InsertBranch(*PredBB, PredTBB, PredFBB, PredCond, DebugLoc()); |
| 699 | |
| 700 | PredBB->removeSuccessor(TailBB); |
Rafael Espindola | 689c247 | 2011-06-20 14:11:42 +0000 | [diff] [blame] | 701 | unsigned NumSuccessors = PredBB->succ_size(); |
| 702 | assert(NumSuccessors <= 1); |
| 703 | if (NumSuccessors == 0 || *PredBB->succ_begin() != NewTarget) |
| 704 | PredBB->addSuccessor(NewTarget); |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 705 | |
| 706 | TDBBs.push_back(PredBB); |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 707 | } |
Rafael Espindola | d7f35fa | 2011-06-24 15:47:41 +0000 | [diff] [blame] | 708 | return Changed; |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 711 | /// TailDuplicate - If it is profitable, duplicate TailBB's contents in each |
| 712 | /// of its predecessors. |
| 713 | bool |
| 714 | TailDuplicatePass::TailDuplicate(MachineBasicBlock *TailBB, MachineFunction &MF, |
| 715 | SmallVector<MachineBasicBlock*, 8> &TDBBs, |
| 716 | SmallVector<MachineInstr*, 16> &Copies) { |
Rafael Espindola | 9dbbd87 | 2011-06-23 03:41:29 +0000 | [diff] [blame] | 717 | bool IsSimple = isSimpleBB(TailBB); |
| 718 | |
| 719 | if (!shouldTailDuplicate(MF, IsSimple, *TailBB)) |
Rafael Espindola | 54c2562 | 2011-06-09 19:54:42 +0000 | [diff] [blame] | 720 | return false; |
| 721 | |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 722 | DEBUG(dbgs() << "\n*** Tail-duplicating BB#" << TailBB->getNumber() << '\n'); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 723 | |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 724 | DenseSet<unsigned> UsedByPhi; |
| 725 | getRegsUsedByPHIs(*TailBB, &UsedByPhi); |
| 726 | |
Rafael Espindola | d7f35fa | 2011-06-24 15:47:41 +0000 | [diff] [blame] | 727 | if (IsSimple) |
| 728 | return duplicateSimpleBB(TailBB, TDBBs, UsedByPhi, Copies); |
Rafael Espindola | 275c1f9 | 2011-06-20 04:16:35 +0000 | [diff] [blame] | 729 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 730 | // Iterate through all the unique predecessors and tail-duplicate this |
| 731 | // block into them, if possible. Copying the list ahead of time also |
| 732 | // avoids trouble with the predecessor list reallocating. |
| 733 | bool Changed = false; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 734 | SmallSetVector<MachineBasicBlock*, 8> Preds(TailBB->pred_begin(), |
| 735 | TailBB->pred_end()); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 736 | for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(), |
| 737 | PE = Preds.end(); PI != PE; ++PI) { |
| 738 | MachineBasicBlock *PredBB = *PI; |
| 739 | |
| 740 | assert(TailBB != PredBB && |
| 741 | "Single-block loop should have been rejected earlier!"); |
Rafael Espindola | 9a9a3a5 | 2011-06-10 20:08:23 +0000 | [diff] [blame] | 742 | // EH edges are ignored by AnalyzeBranch. |
| 743 | if (PredBB->succ_size() > 1) |
| 744 | continue; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 745 | |
| 746 | MachineBasicBlock *PredTBB, *PredFBB; |
| 747 | SmallVector<MachineOperand, 4> PredCond; |
| 748 | if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)) |
| 749 | continue; |
| 750 | if (!PredCond.empty()) |
| 751 | continue; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 752 | // Don't duplicate into a fall-through predecessor (at least for now). |
| 753 | if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough()) |
| 754 | continue; |
| 755 | |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 756 | DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 757 | << "From Succ: " << *TailBB); |
| 758 | |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 759 | TDBBs.push_back(PredBB); |
| 760 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 761 | // Remove PredBB's unconditional branch. |
| 762 | TII->RemoveBranch(*PredBB); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 763 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 764 | // Clone the contents of TailBB into PredBB. |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 765 | DenseMap<unsigned, unsigned> LocalVRMap; |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 766 | SmallVector<std::pair<unsigned,unsigned>, 4> CopyInfos; |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 767 | MachineBasicBlock::iterator I = TailBB->begin(); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 768 | while (I != TailBB->end()) { |
| 769 | MachineInstr *MI = &*I; |
| 770 | ++I; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 771 | if (MI->isPHI()) { |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 772 | // Replace the uses of the def of the PHI with the register coming |
| 773 | // from PredBB. |
Rafael Espindola | 689d7d5 | 2011-06-09 23:22:56 +0000 | [diff] [blame] | 774 | ProcessPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, true); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 775 | } else { |
| 776 | // Replace def of virtual registers with new registers, and update |
| 777 | // uses with PHI source register or the new registers. |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame] | 778 | DuplicateInstruction(MI, TailBB, PredBB, MF, LocalVRMap, UsedByPhi); |
Evan Cheng | 111e762 | 2009-12-03 08:43:53 +0000 | [diff] [blame] | 779 | } |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 780 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 781 | MachineBasicBlock::iterator Loc = PredBB->getFirstTerminator(); |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 782 | for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) { |
Jakob Stoklund Olesen | 1e1098c | 2010-07-10 22:42:59 +0000 | [diff] [blame] | 783 | Copies.push_back(BuildMI(*PredBB, Loc, DebugLoc(), |
| 784 | TII->get(TargetOpcode::COPY), |
| 785 | CopyInfos[i].first).addReg(CopyInfos[i].second)); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 786 | } |
Rafael Espindola | ec324e5 | 2011-06-17 05:54:50 +0000 | [diff] [blame] | 787 | |
| 788 | // Simplify |
| 789 | TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true); |
| 790 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 791 | NumInstrDups += TailBB->size() - 1; // subtract one for removed branch |
| 792 | |
| 793 | // Update the CFG. |
| 794 | PredBB->removeSuccessor(PredBB->succ_begin()); |
| 795 | assert(PredBB->succ_empty() && |
| 796 | "TailDuplicate called on block with multiple successors!"); |
| 797 | for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(), |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 798 | E = TailBB->succ_end(); I != E; ++I) |
| 799 | PredBB->addSuccessor(*I); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 800 | |
| 801 | Changed = true; |
| 802 | ++NumTailDups; |
| 803 | } |
| 804 | |
| 805 | // If TailBB was duplicated into all its predecessors except for the prior |
| 806 | // block, which falls through unconditionally, move the contents of this |
| 807 | // block into the prior block. |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 808 | MachineBasicBlock *PrevBB = prior(MachineFunction::iterator(TailBB)); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 809 | MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0; |
| 810 | SmallVector<MachineOperand, 4> PriorCond; |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 811 | // This has to check PrevBB->succ_size() because EH edges are ignored by |
| 812 | // AnalyzeBranch. |
Rafael Espindola | a899b22 | 2011-06-09 21:43:25 +0000 | [diff] [blame] | 813 | if (PrevBB->succ_size() == 1 && |
| 814 | !TII->AnalyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond, true) && |
| 815 | PriorCond.empty() && !PriorTBB && TailBB->pred_size() == 1 && |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 816 | !TailBB->hasAddressTaken()) { |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 817 | DEBUG(dbgs() << "\nMerging into block: " << *PrevBB |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 818 | << "From MBB: " << *TailBB); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 819 | if (PreRegAlloc) { |
| 820 | DenseMap<unsigned, unsigned> LocalVRMap; |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 821 | SmallVector<std::pair<unsigned,unsigned>, 4> CopyInfos; |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 822 | MachineBasicBlock::iterator I = TailBB->begin(); |
| 823 | // Process PHI instructions first. |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 824 | while (I != TailBB->end() && I->isPHI()) { |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 825 | // Replace the uses of the def of the PHI with the register coming |
| 826 | // from PredBB. |
| 827 | MachineInstr *MI = &*I++; |
Rafael Espindola | 689d7d5 | 2011-06-09 23:22:56 +0000 | [diff] [blame] | 828 | ProcessPHI(MI, TailBB, PrevBB, LocalVRMap, CopyInfos, UsedByPhi, true); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 829 | if (MI->getParent()) |
| 830 | MI->eraseFromParent(); |
| 831 | } |
| 832 | |
| 833 | // Now copy the non-PHI instructions. |
| 834 | while (I != TailBB->end()) { |
| 835 | // Replace def of virtual registers with new registers, and update |
| 836 | // uses with PHI source register or the new registers. |
| 837 | MachineInstr *MI = &*I++; |
Rafael Espindola | 0f28c3f | 2011-06-09 22:53:47 +0000 | [diff] [blame] | 838 | DuplicateInstruction(MI, TailBB, PrevBB, MF, LocalVRMap, UsedByPhi); |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 839 | MI->eraseFromParent(); |
| 840 | } |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 841 | MachineBasicBlock::iterator Loc = PrevBB->getFirstTerminator(); |
Evan Cheng | 3466f13 | 2009-12-15 01:44:10 +0000 | [diff] [blame] | 842 | for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) { |
Jakob Stoklund Olesen | 1e1098c | 2010-07-10 22:42:59 +0000 | [diff] [blame] | 843 | Copies.push_back(BuildMI(*PrevBB, Loc, DebugLoc(), |
| 844 | TII->get(TargetOpcode::COPY), |
| 845 | CopyInfos[i].first) |
| 846 | .addReg(CopyInfos[i].second)); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 847 | } |
Evan Cheng | 79fc6f4 | 2009-12-04 09:42:45 +0000 | [diff] [blame] | 848 | } else { |
| 849 | // No PHIs to worry about, just splice the instructions over. |
| 850 | PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end()); |
| 851 | } |
| 852 | PrevBB->removeSuccessor(PrevBB->succ_begin()); |
| 853 | assert(PrevBB->succ_empty()); |
| 854 | PrevBB->transferSuccessors(TailBB); |
Evan Cheng | 75eb535 | 2009-12-07 10:15:19 +0000 | [diff] [blame] | 855 | TDBBs.push_back(PrevBB); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 856 | Changed = true; |
| 857 | } |
| 858 | |
Rafael Espindola | 689d7d5 | 2011-06-09 23:22:56 +0000 | [diff] [blame] | 859 | // If this is after register allocation, there are no phis to fix. |
| 860 | if (!PreRegAlloc) |
| 861 | return Changed; |
| 862 | |
| 863 | // If we made no changes so far, we are safe. |
| 864 | if (!Changed) |
| 865 | return Changed; |
| 866 | |
| 867 | |
| 868 | // Handle the nasty case in that we duplicated a block that is part of a loop |
| 869 | // into some but not all of its predecessors. For example: |
Rafael Espindola | 4d7b457 | 2011-06-09 23:51:45 +0000 | [diff] [blame] | 870 | // 1 -> 2 <-> 3 | |
| 871 | // \ | |
| 872 | // \---> rest | |
Rafael Espindola | 689d7d5 | 2011-06-09 23:22:56 +0000 | [diff] [blame] | 873 | // if we duplicate 2 into 1 but not into 3, we end up with |
Rafael Espindola | 4d7b457 | 2011-06-09 23:51:45 +0000 | [diff] [blame] | 874 | // 12 -> 3 <-> 2 -> rest | |
| 875 | // \ / | |
| 876 | // \----->-----/ | |
Rafael Espindola | 689d7d5 | 2011-06-09 23:22:56 +0000 | [diff] [blame] | 877 | // If there was a "var = phi(1, 3)" in 2, it has to be ultimately replaced |
| 878 | // with a phi in 3 (which now dominates 2). |
| 879 | // What we do here is introduce a copy in 3 of the register defined by the |
| 880 | // phi, just like when we are duplicating 2 into 3, but we don't copy any |
| 881 | // real instructions or remove the 3 -> 2 edge from the phi in 2. |
| 882 | for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(), |
| 883 | PE = Preds.end(); PI != PE; ++PI) { |
| 884 | MachineBasicBlock *PredBB = *PI; |
| 885 | if (std::find(TDBBs.begin(), TDBBs.end(), PredBB) != TDBBs.end()) |
| 886 | continue; |
| 887 | |
| 888 | // EH edges |
| 889 | if (PredBB->succ_size() != 1) |
| 890 | continue; |
| 891 | |
| 892 | DenseMap<unsigned, unsigned> LocalVRMap; |
| 893 | SmallVector<std::pair<unsigned,unsigned>, 4> CopyInfos; |
| 894 | MachineBasicBlock::iterator I = TailBB->begin(); |
| 895 | // Process PHI instructions first. |
| 896 | while (I != TailBB->end() && I->isPHI()) { |
| 897 | // Replace the uses of the def of the PHI with the register coming |
| 898 | // from PredBB. |
| 899 | MachineInstr *MI = &*I++; |
| 900 | ProcessPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, false); |
| 901 | } |
| 902 | MachineBasicBlock::iterator Loc = PredBB->getFirstTerminator(); |
| 903 | for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) { |
| 904 | Copies.push_back(BuildMI(*PredBB, Loc, DebugLoc(), |
| 905 | TII->get(TargetOpcode::COPY), |
| 906 | CopyInfos[i].first).addReg(CopyInfos[i].second)); |
| 907 | } |
| 908 | } |
| 909 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 910 | return Changed; |
| 911 | } |
| 912 | |
| 913 | /// RemoveDeadBlock - Remove the specified dead machine basic block from the |
| 914 | /// function, updating the CFG. |
Bob Wilson | 2d521e5 | 2009-11-26 21:38:41 +0000 | [diff] [blame] | 915 | void TailDuplicatePass::RemoveDeadBlock(MachineBasicBlock *MBB) { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 916 | assert(MBB->pred_empty() && "MBB must be dead!"); |
David Greene | 00dec1b | 2010-01-05 01:25:15 +0000 | [diff] [blame] | 917 | DEBUG(dbgs() << "\nRemoving MBB: " << *MBB); |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 918 | |
| 919 | // Remove all successors. |
| 920 | while (!MBB->succ_empty()) |
| 921 | MBB->removeSuccessor(MBB->succ_end()-1); |
| 922 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 923 | // Remove the block. |
| 924 | MBB->eraseFromParent(); |
| 925 | } |