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