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