Mercurial > hg > CbC > CbC_llvm
comparison lib/CodeGen/TailDuplication.cpp @ 77:54457678186b LLVM3.6
LLVM 3.6
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 08 Sep 2014 22:06:00 +0900 |
parents | 95c75e76d11b |
children | 60c9769439b8 |
comparison
equal
deleted
inserted
replaced
34:e874dbf0ad9d | 77:54457678186b |
---|---|
10 // This pass duplicates basic blocks ending in unconditional branches into | 10 // This pass duplicates basic blocks ending in unconditional branches into |
11 // the tails of their predecessors. | 11 // the tails of their predecessors. |
12 // | 12 // |
13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
14 | 14 |
15 #define DEBUG_TYPE "tailduplication" | |
16 #include "llvm/CodeGen/Passes.h" | 15 #include "llvm/CodeGen/Passes.h" |
17 #include "llvm/ADT/DenseSet.h" | 16 #include "llvm/ADT/DenseSet.h" |
18 #include "llvm/ADT/OwningPtr.h" | |
19 #include "llvm/ADT/SetVector.h" | 17 #include "llvm/ADT/SetVector.h" |
20 #include "llvm/ADT/SmallSet.h" | 18 #include "llvm/ADT/SmallSet.h" |
21 #include "llvm/ADT/Statistic.h" | 19 #include "llvm/ADT/Statistic.h" |
20 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" | |
22 #include "llvm/CodeGen/MachineFunctionPass.h" | 21 #include "llvm/CodeGen/MachineFunctionPass.h" |
23 #include "llvm/CodeGen/MachineInstrBuilder.h" | 22 #include "llvm/CodeGen/MachineInstrBuilder.h" |
24 #include "llvm/CodeGen/MachineModuleInfo.h" | 23 #include "llvm/CodeGen/MachineModuleInfo.h" |
25 #include "llvm/CodeGen/MachineRegisterInfo.h" | 24 #include "llvm/CodeGen/MachineRegisterInfo.h" |
26 #include "llvm/CodeGen/MachineSSAUpdater.h" | 25 #include "llvm/CodeGen/MachineSSAUpdater.h" |
30 #include "llvm/Support/Debug.h" | 29 #include "llvm/Support/Debug.h" |
31 #include "llvm/Support/ErrorHandling.h" | 30 #include "llvm/Support/ErrorHandling.h" |
32 #include "llvm/Support/raw_ostream.h" | 31 #include "llvm/Support/raw_ostream.h" |
33 #include "llvm/Target/TargetInstrInfo.h" | 32 #include "llvm/Target/TargetInstrInfo.h" |
34 #include "llvm/Target/TargetRegisterInfo.h" | 33 #include "llvm/Target/TargetRegisterInfo.h" |
34 #include "llvm/Target/TargetSubtargetInfo.h" | |
35 using namespace llvm; | 35 using namespace llvm; |
36 | |
37 #define DEBUG_TYPE "tailduplication" | |
36 | 38 |
37 STATISTIC(NumTails , "Number of tails duplicated"); | 39 STATISTIC(NumTails , "Number of tails duplicated"); |
38 STATISTIC(NumTailDups , "Number of tail duplicated blocks"); | 40 STATISTIC(NumTailDups , "Number of tail duplicated blocks"); |
39 STATISTIC(NumInstrDups , "Additional instructions due to tail duplication"); | 41 STATISTIC(NumInstrDups , "Additional instructions due to tail duplication"); |
40 STATISTIC(NumDeadBlocks, "Number of dead blocks removed"); | 42 STATISTIC(NumDeadBlocks, "Number of dead blocks removed"); |
59 namespace { | 61 namespace { |
60 /// TailDuplicatePass - Perform tail duplication. | 62 /// TailDuplicatePass - Perform tail duplication. |
61 class TailDuplicatePass : public MachineFunctionPass { | 63 class TailDuplicatePass : public MachineFunctionPass { |
62 const TargetInstrInfo *TII; | 64 const TargetInstrInfo *TII; |
63 const TargetRegisterInfo *TRI; | 65 const TargetRegisterInfo *TRI; |
66 const MachineBranchProbabilityInfo *MBPI; | |
64 MachineModuleInfo *MMI; | 67 MachineModuleInfo *MMI; |
65 MachineRegisterInfo *MRI; | 68 MachineRegisterInfo *MRI; |
66 OwningPtr<RegScavenger> RS; | 69 std::unique_ptr<RegScavenger> RS; |
67 bool PreRegAlloc; | 70 bool PreRegAlloc; |
68 | 71 |
69 // SSAUpdateVRs - A list of virtual registers for which to update SSA form. | 72 // SSAUpdateVRs - A list of virtual registers for which to update SSA form. |
70 SmallVector<unsigned, 16> SSAUpdateVRs; | 73 SmallVector<unsigned, 16> SSAUpdateVRs; |
71 | 74 |
76 public: | 79 public: |
77 static char ID; | 80 static char ID; |
78 explicit TailDuplicatePass() : | 81 explicit TailDuplicatePass() : |
79 MachineFunctionPass(ID), PreRegAlloc(false) {} | 82 MachineFunctionPass(ID), PreRegAlloc(false) {} |
80 | 83 |
81 virtual bool runOnMachineFunction(MachineFunction &MF); | 84 bool runOnMachineFunction(MachineFunction &MF) override; |
85 | |
86 void getAnalysisUsage(AnalysisUsage &AU) const override; | |
82 | 87 |
83 private: | 88 private: |
84 void AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg, | 89 void AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg, |
85 MachineBasicBlock *BB); | 90 MachineBasicBlock *BB); |
86 void ProcessPHI(MachineInstr *MI, MachineBasicBlock *TailBB, | 91 void ProcessPHI(MachineInstr *MI, MachineBasicBlock *TailBB, |
126 | 131 |
127 INITIALIZE_PASS(TailDuplicatePass, "tailduplication", "Tail Duplication", | 132 INITIALIZE_PASS(TailDuplicatePass, "tailduplication", "Tail Duplication", |
128 false, false) | 133 false, false) |
129 | 134 |
130 bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) { | 135 bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) { |
131 TII = MF.getTarget().getInstrInfo(); | 136 if (skipOptnoneFunction(*MF.getFunction())) |
132 TRI = MF.getTarget().getRegisterInfo(); | 137 return false; |
138 | |
139 TII = MF.getSubtarget().getInstrInfo(); | |
140 TRI = MF.getSubtarget().getRegisterInfo(); | |
133 MRI = &MF.getRegInfo(); | 141 MRI = &MF.getRegInfo(); |
134 MMI = getAnalysisIfAvailable<MachineModuleInfo>(); | 142 MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
143 MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); | |
144 | |
135 PreRegAlloc = MRI->isSSA(); | 145 PreRegAlloc = MRI->isSSA(); |
136 RS.reset(); | 146 RS.reset(); |
137 if (MRI->tracksLiveness() && TRI->trackLivenessAfterRegAlloc(MF)) | 147 if (MRI->tracksLiveness() && TRI->trackLivenessAfterRegAlloc(MF)) |
138 RS.reset(new RegScavenger()); | 148 RS.reset(new RegScavenger()); |
139 | 149 |
140 bool MadeChange = false; | 150 bool MadeChange = false; |
141 while (TailDuplicateBlocks(MF)) | 151 while (TailDuplicateBlocks(MF)) |
142 MadeChange = true; | 152 MadeChange = true; |
143 | 153 |
144 return MadeChange; | 154 return MadeChange; |
155 } | |
156 | |
157 void TailDuplicatePass::getAnalysisUsage(AnalysisUsage &AU) const { | |
158 AU.addRequired<MachineBranchProbabilityInfo>(); | |
159 MachineFunctionPass::getAnalysisUsage(AU); | |
145 } | 160 } |
146 | 161 |
147 static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) { | 162 static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) { |
148 for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ++I) { | 163 for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ++I) { |
149 MachineBasicBlock *MBB = I; | 164 MachineBasicBlock *MBB = I; |
166 } | 181 } |
167 if (!Found) { | 182 if (!Found) { |
168 dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI; | 183 dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI; |
169 dbgs() << " missing input from predecessor BB#" | 184 dbgs() << " missing input from predecessor BB#" |
170 << PredBB->getNumber() << '\n'; | 185 << PredBB->getNumber() << '\n'; |
171 llvm_unreachable(0); | 186 llvm_unreachable(nullptr); |
172 } | 187 } |
173 } | 188 } |
174 | 189 |
175 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { | 190 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { |
176 MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB(); | 191 MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB(); |
177 if (CheckExtra && !Preds.count(PHIBB)) { | 192 if (CheckExtra && !Preds.count(PHIBB)) { |
178 dbgs() << "Warning: malformed PHI in BB#" << MBB->getNumber() | 193 dbgs() << "Warning: malformed PHI in BB#" << MBB->getNumber() |
179 << ": " << *MI; | 194 << ": " << *MI; |
180 dbgs() << " extra input from predecessor BB#" | 195 dbgs() << " extra input from predecessor BB#" |
181 << PHIBB->getNumber() << '\n'; | 196 << PHIBB->getNumber() << '\n'; |
182 llvm_unreachable(0); | 197 llvm_unreachable(nullptr); |
183 } | 198 } |
184 if (PHIBB->getNumber() < 0) { | 199 if (PHIBB->getNumber() < 0) { |
185 dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI; | 200 dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI; |
186 dbgs() << " non-existing BB#" << PHIBB->getNumber() << '\n'; | 201 dbgs() << " non-existing BB#" << PHIBB->getNumber() << '\n'; |
187 llvm_unreachable(0); | 202 llvm_unreachable(nullptr); |
188 } | 203 } |
189 } | 204 } |
190 ++MI; | 205 ++MI; |
191 } | 206 } |
192 } | 207 } |
232 SSAUpdate.Initialize(VReg); | 247 SSAUpdate.Initialize(VReg); |
233 | 248 |
234 // If the original definition is still around, add it as an available | 249 // If the original definition is still around, add it as an available |
235 // value. | 250 // value. |
236 MachineInstr *DefMI = MRI->getVRegDef(VReg); | 251 MachineInstr *DefMI = MRI->getVRegDef(VReg); |
237 MachineBasicBlock *DefBB = 0; | 252 MachineBasicBlock *DefBB = nullptr; |
238 if (DefMI) { | 253 if (DefMI) { |
239 DefBB = DefMI->getParent(); | 254 DefBB = DefMI->getParent(); |
240 SSAUpdate.AddAvailableValue(DefBB, VReg); | 255 SSAUpdate.AddAvailableValue(DefBB, VReg); |
241 } | 256 } |
242 | 257 |
250 } | 265 } |
251 | 266 |
252 // Rewrite uses that are outside of the original def's block. | 267 // Rewrite uses that are outside of the original def's block. |
253 MachineRegisterInfo::use_iterator UI = MRI->use_begin(VReg); | 268 MachineRegisterInfo::use_iterator UI = MRI->use_begin(VReg); |
254 while (UI != MRI->use_end()) { | 269 while (UI != MRI->use_end()) { |
255 MachineOperand &UseMO = UI.getOperand(); | 270 MachineOperand &UseMO = *UI; |
256 MachineInstr *UseMI = &*UI; | 271 MachineInstr *UseMI = UseMO.getParent(); |
257 ++UI; | 272 ++UI; |
258 if (UseMI->isDebugValue()) { | 273 if (UseMI->isDebugValue()) { |
259 // SSAUpdate can replace the use with an undef. That creates | 274 // SSAUpdate can replace the use with an undef. That creates |
260 // a debug instruction that is a kill. | 275 // a debug instruction that is a kill. |
261 // FIXME: Should it SSAUpdate job to delete debug instructions | 276 // FIXME: Should it SSAUpdate job to delete debug instructions |
326 return MadeChange; | 341 return MadeChange; |
327 } | 342 } |
328 | 343 |
329 static bool isDefLiveOut(unsigned Reg, MachineBasicBlock *BB, | 344 static bool isDefLiveOut(unsigned Reg, MachineBasicBlock *BB, |
330 const MachineRegisterInfo *MRI) { | 345 const MachineRegisterInfo *MRI) { |
331 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg), | 346 for (MachineInstr &UseMI : MRI->use_instructions(Reg)) { |
332 UE = MRI->use_end(); UI != UE; ++UI) { | 347 if (UseMI.isDebugValue()) |
333 MachineInstr *UseMI = &*UI; | 348 continue; |
334 if (UseMI->isDebugValue()) | 349 if (UseMI.getParent() != BB) |
335 continue; | |
336 if (UseMI->getParent() != BB) | |
337 return true; | 350 return true; |
338 } | 351 } |
339 return false; | 352 return false; |
340 } | 353 } |
341 | 354 |
350 // Remember which registers are used by phis in this block. This is | 363 // Remember which registers are used by phis in this block. This is |
351 // used to determine which registers are liveout while modifying the | 364 // used to determine which registers are liveout while modifying the |
352 // block (which is why we need to copy the information). | 365 // block (which is why we need to copy the information). |
353 static void getRegsUsedByPHIs(const MachineBasicBlock &BB, | 366 static void getRegsUsedByPHIs(const MachineBasicBlock &BB, |
354 DenseSet<unsigned> *UsedByPhi) { | 367 DenseSet<unsigned> *UsedByPhi) { |
355 for(MachineBasicBlock::const_iterator I = BB.begin(), E = BB.end(); | 368 for (const auto &MI : BB) { |
356 I != E; ++I) { | |
357 const MachineInstr &MI = *I; | |
358 if (!MI.isPHI()) | 369 if (!MI.isPHI()) |
359 break; | 370 break; |
360 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { | 371 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { |
361 unsigned SrcReg = MI.getOperand(i).getReg(); | 372 unsigned SrcReg = MI.getOperand(i).getReg(); |
362 UsedByPhi->insert(SrcReg); | 373 UsedByPhi->insert(SrcReg); |
643 MachineBasicBlock *PredBB = *PI; | 654 MachineBasicBlock *PredBB = *PI; |
644 | 655 |
645 if (PredBB->succ_size() > 1) | 656 if (PredBB->succ_size() > 1) |
646 return false; | 657 return false; |
647 | 658 |
648 MachineBasicBlock *PredTBB = NULL, *PredFBB = NULL; | 659 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr; |
649 SmallVector<MachineOperand, 4> PredCond; | 660 SmallVector<MachineOperand, 4> PredCond; |
650 if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)) | 661 if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)) |
651 return false; | 662 return false; |
652 | 663 |
653 if (!PredCond.empty()) | 664 if (!PredCond.empty()) |
674 continue; | 685 continue; |
675 | 686 |
676 if (bothUsedInPHI(*PredBB, Succs)) | 687 if (bothUsedInPHI(*PredBB, Succs)) |
677 continue; | 688 continue; |
678 | 689 |
679 MachineBasicBlock *PredTBB = NULL, *PredFBB = NULL; | 690 MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr; |
680 SmallVector<MachineOperand, 4> PredCond; | 691 SmallVector<MachineOperand, 4> PredCond; |
681 if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)) | 692 if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true)) |
682 continue; | 693 continue; |
683 | 694 |
684 Changed = true; | 695 Changed = true; |
685 DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB | 696 DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB |
686 << "From simple Succ: " << *TailBB); | 697 << "From simple Succ: " << *TailBB); |
687 | 698 |
688 MachineBasicBlock *NewTarget = *TailBB->succ_begin(); | 699 MachineBasicBlock *NewTarget = *TailBB->succ_begin(); |
689 MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(PredBB)); | 700 MachineBasicBlock *NextBB = std::next(MachineFunction::iterator(PredBB)); |
690 | 701 |
691 // Make PredFBB explicit. | 702 // Make PredFBB explicit. |
692 if (PredCond.empty()) | 703 if (PredCond.empty()) |
693 PredFBB = PredTBB; | 704 PredFBB = PredTBB; |
694 | 705 |
705 PredTBB = NewTarget; | 716 PredTBB = NewTarget; |
706 | 717 |
707 // Make the branch unconditional if possible | 718 // Make the branch unconditional if possible |
708 if (PredTBB == PredFBB) { | 719 if (PredTBB == PredFBB) { |
709 PredCond.clear(); | 720 PredCond.clear(); |
710 PredFBB = NULL; | 721 PredFBB = nullptr; |
711 } | 722 } |
712 | 723 |
713 // Avoid adding fall through branches. | 724 // Avoid adding fall through branches. |
714 if (PredFBB == NextBB) | 725 if (PredFBB == NextBB) |
715 PredFBB = NULL; | 726 PredFBB = nullptr; |
716 if (PredTBB == NextBB && PredFBB == NULL) | 727 if (PredTBB == NextBB && PredFBB == nullptr) |
717 PredTBB = NULL; | 728 PredTBB = nullptr; |
718 | 729 |
719 TII->RemoveBranch(*PredBB); | 730 TII->RemoveBranch(*PredBB); |
720 | 731 |
721 if (PredTBB) | 732 if (PredTBB) |
722 TII->InsertBranch(*PredBB, PredTBB, PredFBB, PredCond, DebugLoc()); | 733 TII->InsertBranch(*PredBB, PredTBB, PredFBB, PredCond, DebugLoc()); |
723 | 734 |
735 uint32_t Weight = MBPI->getEdgeWeight(PredBB, TailBB); | |
724 PredBB->removeSuccessor(TailBB); | 736 PredBB->removeSuccessor(TailBB); |
725 unsigned NumSuccessors = PredBB->succ_size(); | 737 unsigned NumSuccessors = PredBB->succ_size(); |
726 assert(NumSuccessors <= 1); | 738 assert(NumSuccessors <= 1); |
727 if (NumSuccessors == 0 || *PredBB->succ_begin() != NewTarget) | 739 if (NumSuccessors == 0 || *PredBB->succ_begin() != NewTarget) |
728 PredBB->addSuccessor(NewTarget); | 740 PredBB->addSuccessor(NewTarget, Weight); |
729 | 741 |
730 TDBBs.push_back(PredBB); | 742 TDBBs.push_back(PredBB); |
731 } | 743 } |
732 return Changed; | 744 return Changed; |
733 } | 745 } |
784 | 796 |
785 if (RS && !TailBB->livein_empty()) { | 797 if (RS && !TailBB->livein_empty()) { |
786 // Update PredBB livein. | 798 // Update PredBB livein. |
787 RS->enterBasicBlock(PredBB); | 799 RS->enterBasicBlock(PredBB); |
788 if (!PredBB->empty()) | 800 if (!PredBB->empty()) |
789 RS->forward(prior(PredBB->end())); | 801 RS->forward(std::prev(PredBB->end())); |
790 BitVector RegsLiveAtExit(TRI->getNumRegs()); | |
791 RS->getRegsUsed(RegsLiveAtExit, false); | |
792 for (MachineBasicBlock::livein_iterator I = TailBB->livein_begin(), | 802 for (MachineBasicBlock::livein_iterator I = TailBB->livein_begin(), |
793 E = TailBB->livein_end(); I != E; ++I) { | 803 E = TailBB->livein_end(); I != E; ++I) { |
794 if (!RegsLiveAtExit[*I]) | 804 if (!RS->isRegUsed(*I, false)) |
795 // If a register is previously livein to the tail but it's not live | 805 // If a register is previously livein to the tail but it's not live |
796 // at the end of predecessor BB, then it should be added to its | 806 // at the end of predecessor BB, then it should be added to its |
797 // livein list. | 807 // livein list. |
798 PredBB->addLiveIn(*I); | 808 PredBB->addLiveIn(*I); |
799 } | 809 } |
834 PredBB->removeSuccessor(PredBB->succ_begin()); | 844 PredBB->removeSuccessor(PredBB->succ_begin()); |
835 assert(PredBB->succ_empty() && | 845 assert(PredBB->succ_empty() && |
836 "TailDuplicate called on block with multiple successors!"); | 846 "TailDuplicate called on block with multiple successors!"); |
837 for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(), | 847 for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(), |
838 E = TailBB->succ_end(); I != E; ++I) | 848 E = TailBB->succ_end(); I != E; ++I) |
839 PredBB->addSuccessor(*I); | 849 PredBB->addSuccessor(*I, MBPI->getEdgeWeight(TailBB, I)); |
840 | 850 |
841 Changed = true; | 851 Changed = true; |
842 ++NumTailDups; | 852 ++NumTailDups; |
843 } | 853 } |
844 | 854 |
845 // If TailBB was duplicated into all its predecessors except for the prior | 855 // If TailBB was duplicated into all its predecessors except for the prior |
846 // block, which falls through unconditionally, move the contents of this | 856 // block, which falls through unconditionally, move the contents of this |
847 // block into the prior block. | 857 // block into the prior block. |
848 MachineBasicBlock *PrevBB = prior(MachineFunction::iterator(TailBB)); | 858 MachineBasicBlock *PrevBB = std::prev(MachineFunction::iterator(TailBB)); |
849 MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0; | 859 MachineBasicBlock *PriorTBB = nullptr, *PriorFBB = nullptr; |
850 SmallVector<MachineOperand, 4> PriorCond; | 860 SmallVector<MachineOperand, 4> PriorCond; |
851 // This has to check PrevBB->succ_size() because EH edges are ignored by | 861 // This has to check PrevBB->succ_size() because EH edges are ignored by |
852 // AnalyzeBranch. | 862 // AnalyzeBranch. |
853 if (PrevBB->succ_size() == 1 && | 863 if (PrevBB->succ_size() == 1 && |
854 !TII->AnalyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond, true) && | 864 !TII->AnalyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond, true) && |