Mercurial > hg > CbC > CbC_llvm
comparison lib/CodeGen/MachineLICM.cpp @ 134:3a76565eade5 LLVM5.0.1
update 5.0.1
author | mir3636 |
---|---|
date | Sat, 17 Feb 2018 09:57:20 +0900 |
parents | 803732b1fca8 |
children | c2174574ed3a |
comparison
equal
deleted
inserted
replaced
133:c60214abe0e8 | 134:3a76565eade5 |
---|---|
32 #include "llvm/CodeGen/MachineLoopInfo.h" | 32 #include "llvm/CodeGen/MachineLoopInfo.h" |
33 #include "llvm/CodeGen/MachineMemOperand.h" | 33 #include "llvm/CodeGen/MachineMemOperand.h" |
34 #include "llvm/CodeGen/MachineOperand.h" | 34 #include "llvm/CodeGen/MachineOperand.h" |
35 #include "llvm/CodeGen/MachineRegisterInfo.h" | 35 #include "llvm/CodeGen/MachineRegisterInfo.h" |
36 #include "llvm/CodeGen/PseudoSourceValue.h" | 36 #include "llvm/CodeGen/PseudoSourceValue.h" |
37 #include "llvm/CodeGen/TargetInstrInfo.h" | |
38 #include "llvm/CodeGen/TargetLowering.h" | |
39 #include "llvm/CodeGen/TargetRegisterInfo.h" | |
37 #include "llvm/CodeGen/TargetSchedule.h" | 40 #include "llvm/CodeGen/TargetSchedule.h" |
41 #include "llvm/CodeGen/TargetSubtargetInfo.h" | |
38 #include "llvm/IR/DebugLoc.h" | 42 #include "llvm/IR/DebugLoc.h" |
39 #include "llvm/MC/MCInstrDesc.h" | 43 #include "llvm/MC/MCInstrDesc.h" |
40 #include "llvm/MC/MCRegisterInfo.h" | 44 #include "llvm/MC/MCRegisterInfo.h" |
41 #include "llvm/Pass.h" | 45 #include "llvm/Pass.h" |
42 #include "llvm/Support/Casting.h" | 46 #include "llvm/Support/Casting.h" |
43 #include "llvm/Support/CommandLine.h" | 47 #include "llvm/Support/CommandLine.h" |
44 #include "llvm/Support/Debug.h" | 48 #include "llvm/Support/Debug.h" |
45 #include "llvm/Support/raw_ostream.h" | 49 #include "llvm/Support/raw_ostream.h" |
46 #include "llvm/Target/TargetInstrInfo.h" | |
47 #include "llvm/Target/TargetLowering.h" | |
48 #include "llvm/Target/TargetRegisterInfo.h" | |
49 #include "llvm/Target/TargetSubtargetInfo.h" | |
50 #include <algorithm> | 50 #include <algorithm> |
51 #include <cassert> | 51 #include <cassert> |
52 #include <limits> | 52 #include <limits> |
53 #include <vector> | 53 #include <vector> |
54 | 54 |
83 STATISTIC(NumPostRAHoisted, | 83 STATISTIC(NumPostRAHoisted, |
84 "Number of machine instructions hoisted out of loops post regalloc"); | 84 "Number of machine instructions hoisted out of loops post regalloc"); |
85 | 85 |
86 namespace { | 86 namespace { |
87 | 87 |
88 class MachineLICM : public MachineFunctionPass { | 88 class MachineLICMBase : public MachineFunctionPass { |
89 const TargetInstrInfo *TII; | 89 const TargetInstrInfo *TII; |
90 const TargetLoweringBase *TLI; | 90 const TargetLoweringBase *TLI; |
91 const TargetRegisterInfo *TRI; | 91 const TargetRegisterInfo *TRI; |
92 const MachineFrameInfo *MFI; | 92 const MachineFrameInfo *MFI; |
93 MachineRegisterInfo *MRI; | 93 MachineRegisterInfo *MRI; |
94 TargetSchedModel SchedModel; | 94 TargetSchedModel SchedModel; |
95 bool PreRegAlloc = true; | 95 bool PreRegAlloc; |
96 | 96 |
97 // Various analyses that we use... | 97 // Various analyses that we use... |
98 AliasAnalysis *AA; // Alias analysis info. | 98 AliasAnalysis *AA; // Alias analysis info. |
99 MachineLoopInfo *MLI; // Current MachineLoopInfo | 99 MachineLoopInfo *MLI; // Current MachineLoopInfo |
100 MachineDominatorTree *DT; // Machine dominator tree for the cur loop | 100 MachineDominatorTree *DT; // Machine dominator tree for the cur loop |
136 // to hoist loads from this block. | 136 // to hoist loads from this block. |
137 // Tri-state: 0 - false, 1 - true, 2 - unknown | 137 // Tri-state: 0 - false, 1 - true, 2 - unknown |
138 unsigned SpeculationState; | 138 unsigned SpeculationState; |
139 | 139 |
140 public: | 140 public: |
141 static char ID; // Pass identification, replacement for typeid | 141 MachineLICMBase(char &PassID, bool PreRegAlloc) |
142 | 142 : MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {} |
143 MachineLICM() : MachineFunctionPass(ID) { | |
144 initializeMachineLICMPass(*PassRegistry::getPassRegistry()); | |
145 } | |
146 | |
147 explicit MachineLICM(bool PreRA) | |
148 : MachineFunctionPass(ID), PreRegAlloc(PreRA) { | |
149 initializeMachineLICMPass(*PassRegistry::getPassRegistry()); | |
150 } | |
151 | 143 |
152 bool runOnMachineFunction(MachineFunction &MF) override; | 144 bool runOnMachineFunction(MachineFunction &MF) override; |
153 | 145 |
154 void getAnalysisUsage(AnalysisUsage &AU) const override { | 146 void getAnalysisUsage(AnalysisUsage &AU) const override { |
155 AU.addRequired<MachineLoopInfo>(); | 147 AU.addRequired<MachineLoopInfo>(); |
250 void InitCSEMap(MachineBasicBlock *BB); | 242 void InitCSEMap(MachineBasicBlock *BB); |
251 | 243 |
252 MachineBasicBlock *getCurPreheader(); | 244 MachineBasicBlock *getCurPreheader(); |
253 }; | 245 }; |
254 | 246 |
247 class MachineLICM : public MachineLICMBase { | |
248 public: | |
249 static char ID; | |
250 MachineLICM() : MachineLICMBase(ID, false) { | |
251 initializeMachineLICMPass(*PassRegistry::getPassRegistry()); | |
252 } | |
253 }; | |
254 | |
255 class EarlyMachineLICM : public MachineLICMBase { | |
256 public: | |
257 static char ID; | |
258 EarlyMachineLICM() : MachineLICMBase(ID, true) { | |
259 initializeEarlyMachineLICMPass(*PassRegistry::getPassRegistry()); | |
260 } | |
261 }; | |
262 | |
255 } // end anonymous namespace | 263 } // end anonymous namespace |
256 | 264 |
257 char MachineLICM::ID = 0; | 265 char MachineLICM::ID; |
266 char EarlyMachineLICM::ID; | |
258 | 267 |
259 char &llvm::MachineLICMID = MachineLICM::ID; | 268 char &llvm::MachineLICMID = MachineLICM::ID; |
269 char &llvm::EarlyMachineLICMID = EarlyMachineLICM::ID; | |
260 | 270 |
261 INITIALIZE_PASS_BEGIN(MachineLICM, DEBUG_TYPE, | 271 INITIALIZE_PASS_BEGIN(MachineLICM, DEBUG_TYPE, |
262 "Machine Loop Invariant Code Motion", false, false) | 272 "Machine Loop Invariant Code Motion", false, false) |
263 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) | 273 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
264 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) | 274 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
265 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) | 275 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
266 INITIALIZE_PASS_END(MachineLICM, DEBUG_TYPE, | 276 INITIALIZE_PASS_END(MachineLICM, DEBUG_TYPE, |
267 "Machine Loop Invariant Code Motion", false, false) | 277 "Machine Loop Invariant Code Motion", false, false) |
278 | |
279 INITIALIZE_PASS_BEGIN(EarlyMachineLICM, "early-machinelicm", | |
280 "Early Machine Loop Invariant Code Motion", false, false) | |
281 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) | |
282 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) | |
283 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) | |
284 INITIALIZE_PASS_END(EarlyMachineLICM, "early-machinelicm", | |
285 "Early Machine Loop Invariant Code Motion", false, false) | |
268 | 286 |
269 /// Test if the given loop is the outer-most loop that has a unique predecessor. | 287 /// Test if the given loop is the outer-most loop that has a unique predecessor. |
270 static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) { | 288 static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) { |
271 // Check whether this loop even has a unique predecessor. | 289 // Check whether this loop even has a unique predecessor. |
272 if (!CurLoop->getLoopPredecessor()) | 290 if (!CurLoop->getLoopPredecessor()) |
277 return false; | 295 return false; |
278 // None of them did, so this is the outermost with a unique predecessor. | 296 // None of them did, so this is the outermost with a unique predecessor. |
279 return true; | 297 return true; |
280 } | 298 } |
281 | 299 |
282 bool MachineLICM::runOnMachineFunction(MachineFunction &MF) { | 300 bool MachineLICMBase::runOnMachineFunction(MachineFunction &MF) { |
283 if (skipFunction(*MF.getFunction())) | 301 if (skipFunction(MF.getFunction())) |
284 return false; | 302 return false; |
285 | 303 |
286 Changed = FirstInLoop = false; | 304 Changed = FirstInLoop = false; |
287 const TargetSubtargetInfo &ST = MF.getSubtarget(); | 305 const TargetSubtargetInfo &ST = MF.getSubtarget(); |
288 TII = ST.getInstrInfo(); | 306 TII = ST.getInstrInfo(); |
366 return false; | 384 return false; |
367 } | 385 } |
368 | 386 |
369 /// Examine the instruction for potentai LICM candidate. Also | 387 /// Examine the instruction for potentai LICM candidate. Also |
370 /// gather register def and frame object update information. | 388 /// gather register def and frame object update information. |
371 void MachineLICM::ProcessMI(MachineInstr *MI, | 389 void MachineLICMBase::ProcessMI(MachineInstr *MI, |
372 BitVector &PhysRegDefs, | 390 BitVector &PhysRegDefs, |
373 BitVector &PhysRegClobbers, | 391 BitVector &PhysRegClobbers, |
374 SmallSet<int, 32> &StoredFIs, | 392 SmallSet<int, 32> &StoredFIs, |
375 SmallVectorImpl<CandidateInfo> &Candidates) { | 393 SmallVectorImpl<CandidateInfo> &Candidates) { |
376 bool RuledOut = false; | 394 bool RuledOut = false; |
377 bool HasNonInvariantUse = false; | 395 bool HasNonInvariantUse = false; |
378 unsigned Def = 0; | 396 unsigned Def = 0; |
379 for (const MachineOperand &MO : MI->operands()) { | 397 for (const MachineOperand &MO : MI->operands()) { |
380 if (MO.isFI()) { | 398 if (MO.isFI()) { |
453 } | 471 } |
454 } | 472 } |
455 | 473 |
456 /// Walk the specified region of the CFG and hoist loop invariants out to the | 474 /// Walk the specified region of the CFG and hoist loop invariants out to the |
457 /// preheader. | 475 /// preheader. |
458 void MachineLICM::HoistRegionPostRA() { | 476 void MachineLICMBase::HoistRegionPostRA() { |
459 MachineBasicBlock *Preheader = getCurPreheader(); | 477 MachineBasicBlock *Preheader = getCurPreheader(); |
460 if (!Preheader) | 478 if (!Preheader) |
461 return; | 479 return; |
462 | 480 |
463 unsigned NumRegs = TRI->getNumRegs(); | 481 unsigned NumRegs = TRI->getNumRegs(); |
539 } | 557 } |
540 } | 558 } |
541 | 559 |
542 /// Add register 'Reg' to the livein sets of BBs in the current loop, and make | 560 /// Add register 'Reg' to the livein sets of BBs in the current loop, and make |
543 /// sure it is not killed by any instructions in the loop. | 561 /// sure it is not killed by any instructions in the loop. |
544 void MachineLICM::AddToLiveIns(unsigned Reg) { | 562 void MachineLICMBase::AddToLiveIns(unsigned Reg) { |
545 const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks(); | 563 const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks(); |
546 for (MachineBasicBlock *BB : Blocks) { | 564 for (MachineBasicBlock *BB : Blocks) { |
547 if (!BB->isLiveIn(Reg)) | 565 if (!BB->isLiveIn(Reg)) |
548 BB->addLiveIn(Reg); | 566 BB->addLiveIn(Reg); |
549 for (MachineInstr &MI : *BB) { | 567 for (MachineInstr &MI : *BB) { |
556 } | 574 } |
557 } | 575 } |
558 | 576 |
559 /// When an instruction is found to only use loop invariant operands that is | 577 /// When an instruction is found to only use loop invariant operands that is |
560 /// safe to hoist, this instruction is called to do the dirty work. | 578 /// safe to hoist, this instruction is called to do the dirty work. |
561 void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) { | 579 void MachineLICMBase::HoistPostRA(MachineInstr *MI, unsigned Def) { |
562 MachineBasicBlock *Preheader = getCurPreheader(); | 580 MachineBasicBlock *Preheader = getCurPreheader(); |
563 | 581 |
564 // Now move the instructions to the predecessor, inserting it before any | 582 // Now move the instructions to the predecessor, inserting it before any |
565 // terminator instructions. | 583 // terminator instructions. |
566 DEBUG(dbgs() << "Hoisting to BB#" << Preheader->getNumber() << " from BB#" | 584 DEBUG(dbgs() << "Hoisting to " << printMBBReference(*Preheader) << " from " |
567 << MI->getParent()->getNumber() << ": " << *MI); | 585 << printMBBReference(*MI->getParent()) << ": " << *MI); |
568 | 586 |
569 // Splice the instruction to the preheader. | 587 // Splice the instruction to the preheader. |
570 MachineBasicBlock *MBB = MI->getParent(); | 588 MachineBasicBlock *MBB = MI->getParent(); |
571 Preheader->splice(Preheader->getFirstTerminator(), MBB, MI); | 589 Preheader->splice(Preheader->getFirstTerminator(), MBB, MI); |
572 | 590 |
579 Changed = true; | 597 Changed = true; |
580 } | 598 } |
581 | 599 |
582 /// Check if this mbb is guaranteed to execute. If not then a load from this mbb | 600 /// Check if this mbb is guaranteed to execute. If not then a load from this mbb |
583 /// may not be safe to hoist. | 601 /// may not be safe to hoist. |
584 bool MachineLICM::IsGuaranteedToExecute(MachineBasicBlock *BB) { | 602 bool MachineLICMBase::IsGuaranteedToExecute(MachineBasicBlock *BB) { |
585 if (SpeculationState != SpeculateUnknown) | 603 if (SpeculationState != SpeculateUnknown) |
586 return SpeculationState == SpeculateFalse; | 604 return SpeculationState == SpeculateFalse; |
587 | 605 |
588 if (BB != CurLoop->getHeader()) { | 606 if (BB != CurLoop->getHeader()) { |
589 // Check loop exiting blocks. | 607 // Check loop exiting blocks. |
598 | 616 |
599 SpeculationState = SpeculateFalse; | 617 SpeculationState = SpeculateFalse; |
600 return true; | 618 return true; |
601 } | 619 } |
602 | 620 |
603 void MachineLICM::EnterScope(MachineBasicBlock *MBB) { | 621 void MachineLICMBase::EnterScope(MachineBasicBlock *MBB) { |
604 DEBUG(dbgs() << "Entering BB#" << MBB->getNumber() << '\n'); | 622 DEBUG(dbgs() << "Entering " << printMBBReference(*MBB) << '\n'); |
605 | 623 |
606 // Remember livein register pressure. | 624 // Remember livein register pressure. |
607 BackTrace.push_back(RegPressure); | 625 BackTrace.push_back(RegPressure); |
608 } | 626 } |
609 | 627 |
610 void MachineLICM::ExitScope(MachineBasicBlock *MBB) { | 628 void MachineLICMBase::ExitScope(MachineBasicBlock *MBB) { |
611 DEBUG(dbgs() << "Exiting BB#" << MBB->getNumber() << '\n'); | 629 DEBUG(dbgs() << "Exiting " << printMBBReference(*MBB) << '\n'); |
612 BackTrace.pop_back(); | 630 BackTrace.pop_back(); |
613 } | 631 } |
614 | 632 |
615 /// Destroy scope for the MBB that corresponds to the given dominator tree node | 633 /// Destroy scope for the MBB that corresponds to the given dominator tree node |
616 /// if its a leaf or all of its children are done. Walk up the dominator tree to | 634 /// if its a leaf or all of its children are done. Walk up the dominator tree to |
617 /// destroy ancestors which are now done. | 635 /// destroy ancestors which are now done. |
618 void MachineLICM::ExitScopeIfDone(MachineDomTreeNode *Node, | 636 void MachineLICMBase::ExitScopeIfDone(MachineDomTreeNode *Node, |
619 DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren, | 637 DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren, |
620 DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) { | 638 DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) { |
621 if (OpenChildren[Node]) | 639 if (OpenChildren[Node]) |
622 return; | 640 return; |
623 | 641 |
624 // Pop scope. | 642 // Pop scope. |
625 ExitScope(Node->getBlock()); | 643 ExitScope(Node->getBlock()); |
636 | 654 |
637 /// Walk the specified loop in the CFG (defined by all blocks dominated by the | 655 /// Walk the specified loop in the CFG (defined by all blocks dominated by the |
638 /// specified header block, and that are in the current loop) in depth first | 656 /// specified header block, and that are in the current loop) in depth first |
639 /// order w.r.t the DominatorTree. This allows us to visit definitions before | 657 /// order w.r.t the DominatorTree. This allows us to visit definitions before |
640 /// uses, allowing us to hoist a loop body in one pass without iteration. | 658 /// uses, allowing us to hoist a loop body in one pass without iteration. |
641 void MachineLICM::HoistOutOfLoop(MachineDomTreeNode *HeaderN) { | 659 void MachineLICMBase::HoistOutOfLoop(MachineDomTreeNode *HeaderN) { |
642 MachineBasicBlock *Preheader = getCurPreheader(); | 660 MachineBasicBlock *Preheader = getCurPreheader(); |
643 if (!Preheader) | 661 if (!Preheader) |
644 return; | 662 return; |
645 | 663 |
646 SmallVector<MachineDomTreeNode*, 32> Scopes; | 664 SmallVector<MachineDomTreeNode*, 32> Scopes; |
717 } | 735 } |
718 | 736 |
719 /// Sink instructions into loops if profitable. This especially tries to prevent | 737 /// Sink instructions into loops if profitable. This especially tries to prevent |
720 /// register spills caused by register pressure if there is little to no | 738 /// register spills caused by register pressure if there is little to no |
721 /// overhead moving instructions into loops. | 739 /// overhead moving instructions into loops. |
722 void MachineLICM::SinkIntoLoop() { | 740 void MachineLICMBase::SinkIntoLoop() { |
723 MachineBasicBlock *Preheader = getCurPreheader(); | 741 MachineBasicBlock *Preheader = getCurPreheader(); |
724 if (!Preheader) | 742 if (!Preheader) |
725 return; | 743 return; |
726 | 744 |
727 SmallVector<MachineInstr *, 8> Candidates; | 745 SmallVector<MachineInstr *, 8> Candidates; |
771 } | 789 } |
772 | 790 |
773 /// Find all virtual register references that are liveout of the preheader to | 791 /// Find all virtual register references that are liveout of the preheader to |
774 /// initialize the starting "register pressure". Note this does not count live | 792 /// initialize the starting "register pressure". Note this does not count live |
775 /// through (livein but not used) registers. | 793 /// through (livein but not used) registers. |
776 void MachineLICM::InitRegPressure(MachineBasicBlock *BB) { | 794 void MachineLICMBase::InitRegPressure(MachineBasicBlock *BB) { |
777 std::fill(RegPressure.begin(), RegPressure.end(), 0); | 795 std::fill(RegPressure.begin(), RegPressure.end(), 0); |
778 | 796 |
779 // If the preheader has only a single predecessor and it ends with a | 797 // If the preheader has only a single predecessor and it ends with a |
780 // fallthrough or an unconditional branch, then scan its predecessor for live | 798 // fallthrough or an unconditional branch, then scan its predecessor for live |
781 // defs as well. This happens whenever the preheader is created by splitting | 799 // defs as well. This happens whenever the preheader is created by splitting |
790 for (const MachineInstr &MI : *BB) | 808 for (const MachineInstr &MI : *BB) |
791 UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true); | 809 UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true); |
792 } | 810 } |
793 | 811 |
794 /// Update estimate of register pressure after the specified instruction. | 812 /// Update estimate of register pressure after the specified instruction. |
795 void MachineLICM::UpdateRegPressure(const MachineInstr *MI, | 813 void MachineLICMBase::UpdateRegPressure(const MachineInstr *MI, |
796 bool ConsiderUnseenAsDef) { | 814 bool ConsiderUnseenAsDef) { |
797 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef); | 815 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef); |
798 for (const auto &RPIdAndCost : Cost) { | 816 for (const auto &RPIdAndCost : Cost) { |
799 unsigned Class = RPIdAndCost.first; | 817 unsigned Class = RPIdAndCost.first; |
800 if (static_cast<int>(RegPressure[Class]) < -RPIdAndCost.second) | 818 if (static_cast<int>(RegPressure[Class]) < -RPIdAndCost.second) |
801 RegPressure[Class] = 0; | 819 RegPressure[Class] = 0; |
809 /// | 827 /// |
810 /// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to | 828 /// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to |
811 /// figure out which usages are live-ins. | 829 /// figure out which usages are live-ins. |
812 /// FIXME: Figure out a way to consider 'RegSeen' from all code paths. | 830 /// FIXME: Figure out a way to consider 'RegSeen' from all code paths. |
813 DenseMap<unsigned, int> | 831 DenseMap<unsigned, int> |
814 MachineLICM::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen, | 832 MachineLICMBase::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen, |
815 bool ConsiderUnseenAsDef) { | 833 bool ConsiderUnseenAsDef) { |
816 DenseMap<unsigned, int> Cost; | 834 DenseMap<unsigned, int> Cost; |
817 if (MI->isImplicitDef()) | 835 if (MI->isImplicitDef()) |
818 return Cost; | 836 return Cost; |
819 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { | 837 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { |
820 const MachineOperand &MO = MI->getOperand(i); | 838 const MachineOperand &MO = MI->getOperand(i); |
871 return false; | 889 return false; |
872 } | 890 } |
873 | 891 |
874 /// Returns true if the instruction may be a suitable candidate for LICM. | 892 /// Returns true if the instruction may be a suitable candidate for LICM. |
875 /// e.g. If the instruction is a call, then it's obviously not safe to hoist it. | 893 /// e.g. If the instruction is a call, then it's obviously not safe to hoist it. |
876 bool MachineLICM::IsLICMCandidate(MachineInstr &I) { | 894 bool MachineLICMBase::IsLICMCandidate(MachineInstr &I) { |
877 // Check if it's safe to move the instruction. | 895 // Check if it's safe to move the instruction. |
878 bool DontMoveAcrossStore = true; | 896 bool DontMoveAcrossStore = true; |
879 if (!I.isSafeToMove(AA, DontMoveAcrossStore)) | 897 if (!I.isSafeToMove(AA, DontMoveAcrossStore)) |
880 return false; | 898 return false; |
881 | 899 |
894 | 912 |
895 /// Returns true if the instruction is loop invariant. | 913 /// Returns true if the instruction is loop invariant. |
896 /// I.e., all virtual register operands are defined outside of the loop, | 914 /// I.e., all virtual register operands are defined outside of the loop, |
897 /// physical registers aren't accessed explicitly, and there are no side | 915 /// physical registers aren't accessed explicitly, and there are no side |
898 /// effects that aren't captured by the operands or other flags. | 916 /// effects that aren't captured by the operands or other flags. |
899 bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) { | 917 bool MachineLICMBase::IsLoopInvariantInst(MachineInstr &I) { |
900 if (!IsLICMCandidate(I)) | 918 if (!IsLICMCandidate(I)) |
901 return false; | 919 return false; |
902 | 920 |
903 // The instruction is loop invariant if all of its operands are. | 921 // The instruction is loop invariant if all of its operands are. |
904 for (const MachineOperand &MO : I.operands()) { | 922 for (const MachineOperand &MO : I.operands()) { |
947 return true; | 965 return true; |
948 } | 966 } |
949 | 967 |
950 /// Return true if the specified instruction is used by a phi node and hoisting | 968 /// Return true if the specified instruction is used by a phi node and hoisting |
951 /// it could cause a copy to be inserted. | 969 /// it could cause a copy to be inserted. |
952 bool MachineLICM::HasLoopPHIUse(const MachineInstr *MI) const { | 970 bool MachineLICMBase::HasLoopPHIUse(const MachineInstr *MI) const { |
953 SmallVector<const MachineInstr*, 8> Work(1, MI); | 971 SmallVector<const MachineInstr*, 8> Work(1, MI); |
954 do { | 972 do { |
955 MI = Work.pop_back_val(); | 973 MI = Work.pop_back_val(); |
956 for (const MachineOperand &MO : MI->operands()) { | 974 for (const MachineOperand &MO : MI->operands()) { |
957 if (!MO.isReg() || !MO.isDef()) | 975 if (!MO.isReg() || !MO.isDef()) |
982 return false; | 1000 return false; |
983 } | 1001 } |
984 | 1002 |
985 /// Compute operand latency between a def of 'Reg' and an use in the current | 1003 /// Compute operand latency between a def of 'Reg' and an use in the current |
986 /// loop, return true if the target considered it high. | 1004 /// loop, return true if the target considered it high. |
987 bool MachineLICM::HasHighOperandLatency(MachineInstr &MI, | 1005 bool MachineLICMBase::HasHighOperandLatency(MachineInstr &MI, |
988 unsigned DefIdx, unsigned Reg) const { | 1006 unsigned DefIdx, |
1007 unsigned Reg) const { | |
989 if (MRI->use_nodbg_empty(Reg)) | 1008 if (MRI->use_nodbg_empty(Reg)) |
990 return false; | 1009 return false; |
991 | 1010 |
992 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) { | 1011 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) { |
993 if (UseMI.isCopyLike()) | 1012 if (UseMI.isCopyLike()) |
1013 return false; | 1032 return false; |
1014 } | 1033 } |
1015 | 1034 |
1016 /// Return true if the instruction is marked "cheap" or the operand latency | 1035 /// Return true if the instruction is marked "cheap" or the operand latency |
1017 /// between its def and a use is one or less. | 1036 /// between its def and a use is one or less. |
1018 bool MachineLICM::IsCheapInstruction(MachineInstr &MI) const { | 1037 bool MachineLICMBase::IsCheapInstruction(MachineInstr &MI) const { |
1019 if (TII->isAsCheapAsAMove(MI) || MI.isCopyLike()) | 1038 if (TII->isAsCheapAsAMove(MI) || MI.isCopyLike()) |
1020 return true; | 1039 return true; |
1021 | 1040 |
1022 bool isCheap = false; | 1041 bool isCheap = false; |
1023 unsigned NumDefs = MI.getDesc().getNumDefs(); | 1042 unsigned NumDefs = MI.getDesc().getNumDefs(); |
1038 return isCheap; | 1057 return isCheap; |
1039 } | 1058 } |
1040 | 1059 |
1041 /// Visit BBs from header to current BB, check if hoisting an instruction of the | 1060 /// Visit BBs from header to current BB, check if hoisting an instruction of the |
1042 /// given cost matrix can cause high register pressure. | 1061 /// given cost matrix can cause high register pressure. |
1043 bool MachineLICM::CanCauseHighRegPressure(const DenseMap<unsigned, int>& Cost, | 1062 bool |
1044 bool CheapInstr) { | 1063 MachineLICMBase::CanCauseHighRegPressure(const DenseMap<unsigned, int>& Cost, |
1064 bool CheapInstr) { | |
1045 for (const auto &RPIdAndCost : Cost) { | 1065 for (const auto &RPIdAndCost : Cost) { |
1046 if (RPIdAndCost.second <= 0) | 1066 if (RPIdAndCost.second <= 0) |
1047 continue; | 1067 continue; |
1048 | 1068 |
1049 unsigned Class = RPIdAndCost.first; | 1069 unsigned Class = RPIdAndCost.first; |
1063 } | 1083 } |
1064 | 1084 |
1065 /// Traverse the back trace from header to the current block and update their | 1085 /// Traverse the back trace from header to the current block and update their |
1066 /// register pressures to reflect the effect of hoisting MI from the current | 1086 /// register pressures to reflect the effect of hoisting MI from the current |
1067 /// block to the preheader. | 1087 /// block to the preheader. |
1068 void MachineLICM::UpdateBackTraceRegPressure(const MachineInstr *MI) { | 1088 void MachineLICMBase::UpdateBackTraceRegPressure(const MachineInstr *MI) { |
1069 // First compute the 'cost' of the instruction, i.e. its contribution | 1089 // First compute the 'cost' of the instruction, i.e. its contribution |
1070 // to register pressure. | 1090 // to register pressure. |
1071 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false, | 1091 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false, |
1072 /*ConsiderUnseenAsDef=*/false); | 1092 /*ConsiderUnseenAsDef=*/false); |
1073 | 1093 |
1077 RP[RPIdAndCost.first] += RPIdAndCost.second; | 1097 RP[RPIdAndCost.first] += RPIdAndCost.second; |
1078 } | 1098 } |
1079 | 1099 |
1080 /// Return true if it is potentially profitable to hoist the given loop | 1100 /// Return true if it is potentially profitable to hoist the given loop |
1081 /// invariant. | 1101 /// invariant. |
1082 bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) { | 1102 bool MachineLICMBase::IsProfitableToHoist(MachineInstr &MI) { |
1083 if (MI.isImplicitDef()) | 1103 if (MI.isImplicitDef()) |
1084 return true; | 1104 return true; |
1085 | 1105 |
1086 // Besides removing computation from the loop, hoisting an instruction has | 1106 // Besides removing computation from the loop, hoisting an instruction has |
1087 // these effects: | 1107 // these effects: |
1169 } | 1189 } |
1170 | 1190 |
1171 /// Unfold a load from the given machineinstr if the load itself could be | 1191 /// Unfold a load from the given machineinstr if the load itself could be |
1172 /// hoisted. Return the unfolded and hoistable load, or null if the load | 1192 /// hoisted. Return the unfolded and hoistable load, or null if the load |
1173 /// couldn't be unfolded or if it wouldn't be hoistable. | 1193 /// couldn't be unfolded or if it wouldn't be hoistable. |
1174 MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) { | 1194 MachineInstr *MachineLICMBase::ExtractHoistableLoad(MachineInstr *MI) { |
1175 // Don't unfold simple loads. | 1195 // Don't unfold simple loads. |
1176 if (MI->canFoldAsLoad()) | 1196 if (MI->canFoldAsLoad()) |
1177 return nullptr; | 1197 return nullptr; |
1178 | 1198 |
1179 // If not, we may be able to unfold a load and hoist that. | 1199 // If not, we may be able to unfold a load and hoist that. |
1227 } | 1247 } |
1228 | 1248 |
1229 /// Initialize the CSE map with instructions that are in the current loop | 1249 /// Initialize the CSE map with instructions that are in the current loop |
1230 /// preheader that may become duplicates of instructions that are hoisted | 1250 /// preheader that may become duplicates of instructions that are hoisted |
1231 /// out of the loop. | 1251 /// out of the loop. |
1232 void MachineLICM::InitCSEMap(MachineBasicBlock *BB) { | 1252 void MachineLICMBase::InitCSEMap(MachineBasicBlock *BB) { |
1233 for (MachineInstr &MI : *BB) | 1253 for (MachineInstr &MI : *BB) |
1234 CSEMap[MI.getOpcode()].push_back(&MI); | 1254 CSEMap[MI.getOpcode()].push_back(&MI); |
1235 } | 1255 } |
1236 | 1256 |
1237 /// Find an instruction amount PrevMIs that is a duplicate of MI. | 1257 /// Find an instruction amount PrevMIs that is a duplicate of MI. |
1238 /// Return this instruction if it's found. | 1258 /// Return this instruction if it's found. |
1239 const MachineInstr* | 1259 const MachineInstr* |
1240 MachineLICM::LookForDuplicate(const MachineInstr *MI, | 1260 MachineLICMBase::LookForDuplicate(const MachineInstr *MI, |
1241 std::vector<const MachineInstr*> &PrevMIs) { | 1261 std::vector<const MachineInstr*> &PrevMIs) { |
1242 for (const MachineInstr *PrevMI : PrevMIs) | 1262 for (const MachineInstr *PrevMI : PrevMIs) |
1243 if (TII->produceSameValue(*MI, *PrevMI, (PreRegAlloc ? MRI : nullptr))) | 1263 if (TII->produceSameValue(*MI, *PrevMI, (PreRegAlloc ? MRI : nullptr))) |
1244 return PrevMI; | 1264 return PrevMI; |
1245 | 1265 |
1246 return nullptr; | 1266 return nullptr; |
1248 | 1268 |
1249 /// Given a LICM'ed instruction, look for an instruction on the preheader that | 1269 /// Given a LICM'ed instruction, look for an instruction on the preheader that |
1250 /// computes the same value. If it's found, do a RAU on with the definition of | 1270 /// computes the same value. If it's found, do a RAU on with the definition of |
1251 /// the existing instruction rather than hoisting the instruction to the | 1271 /// the existing instruction rather than hoisting the instruction to the |
1252 /// preheader. | 1272 /// preheader. |
1253 bool MachineLICM::EliminateCSE(MachineInstr *MI, | 1273 bool MachineLICMBase::EliminateCSE(MachineInstr *MI, |
1254 DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator &CI) { | 1274 DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator &CI) { |
1255 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate | 1275 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate |
1256 // the undef property onto uses. | 1276 // the undef property onto uses. |
1257 if (CI == CSEMap.end() || MI->isImplicitDef()) | 1277 if (CI == CSEMap.end() || MI->isImplicitDef()) |
1258 return false; | 1278 return false; |
1259 | 1279 |
1306 return false; | 1326 return false; |
1307 } | 1327 } |
1308 | 1328 |
1309 /// Return true if the given instruction will be CSE'd if it's hoisted out of | 1329 /// Return true if the given instruction will be CSE'd if it's hoisted out of |
1310 /// the loop. | 1330 /// the loop. |
1311 bool MachineLICM::MayCSE(MachineInstr *MI) { | 1331 bool MachineLICMBase::MayCSE(MachineInstr *MI) { |
1312 unsigned Opcode = MI->getOpcode(); | 1332 unsigned Opcode = MI->getOpcode(); |
1313 DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator | 1333 DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator |
1314 CI = CSEMap.find(Opcode); | 1334 CI = CSEMap.find(Opcode); |
1315 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate | 1335 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate |
1316 // the undef property onto uses. | 1336 // the undef property onto uses. |
1321 } | 1341 } |
1322 | 1342 |
1323 /// When an instruction is found to use only loop invariant operands | 1343 /// When an instruction is found to use only loop invariant operands |
1324 /// that are safe to hoist, this instruction is called to do the dirty work. | 1344 /// that are safe to hoist, this instruction is called to do the dirty work. |
1325 /// It returns true if the instruction is hoisted. | 1345 /// It returns true if the instruction is hoisted. |
1326 bool MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) { | 1346 bool MachineLICMBase::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) { |
1327 // First check whether we should hoist this instruction. | 1347 // First check whether we should hoist this instruction. |
1328 if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) { | 1348 if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) { |
1329 // If not, try unfolding a hoistable load. | 1349 // If not, try unfolding a hoistable load. |
1330 MI = ExtractHoistableLoad(MI); | 1350 MI = ExtractHoistableLoad(MI); |
1331 if (!MI) return false; | 1351 if (!MI) return false; |
1334 // Now move the instructions to the predecessor, inserting it before any | 1354 // Now move the instructions to the predecessor, inserting it before any |
1335 // terminator instructions. | 1355 // terminator instructions. |
1336 DEBUG({ | 1356 DEBUG({ |
1337 dbgs() << "Hoisting " << *MI; | 1357 dbgs() << "Hoisting " << *MI; |
1338 if (MI->getParent()->getBasicBlock()) | 1358 if (MI->getParent()->getBasicBlock()) |
1339 dbgs() << " from BB#" << MI->getParent()->getNumber(); | 1359 dbgs() << " from " << printMBBReference(*MI->getParent()); |
1340 if (Preheader->getBasicBlock()) | 1360 if (Preheader->getBasicBlock()) |
1341 dbgs() << " to BB#" << Preheader->getNumber(); | 1361 dbgs() << " to " << printMBBReference(*Preheader); |
1342 dbgs() << "\n"; | 1362 dbgs() << "\n"; |
1343 }); | 1363 }); |
1344 | 1364 |
1345 // If this is the first instruction being hoisted to the preheader, | 1365 // If this is the first instruction being hoisted to the preheader, |
1346 // initialize the CSE map with potential common expressions. | 1366 // initialize the CSE map with potential common expressions. |
1384 | 1404 |
1385 return true; | 1405 return true; |
1386 } | 1406 } |
1387 | 1407 |
1388 /// Get the preheader for the current loop, splitting a critical edge if needed. | 1408 /// Get the preheader for the current loop, splitting a critical edge if needed. |
1389 MachineBasicBlock *MachineLICM::getCurPreheader() { | 1409 MachineBasicBlock *MachineLICMBase::getCurPreheader() { |
1390 // Determine the block to which to hoist instructions. If we can't find a | 1410 // Determine the block to which to hoist instructions. If we can't find a |
1391 // suitable loop predecessor, we can't do any hoisting. | 1411 // suitable loop predecessor, we can't do any hoisting. |
1392 | 1412 |
1393 // If we've tried to get a preheader and failed, don't try again. | 1413 // If we've tried to get a preheader and failed, don't try again. |
1394 if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1)) | 1414 if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1)) |