Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 1 | /// - Targets that need the hazard recognizer to be run at -O0. |
| 2 | /// - Targets that want to guarantee that hazards at the beginning of |
| 3 | /// scheduling regions are handled correctly. The post-RA scheduler is |
| 4 | /// a top-down scheduler, but when there are multiple scheduling regions |
| 5 | /// in a basic block, it visits the regions in bottom-up order. This |
| 6 | /// makes it impossible for the scheduler to gauranttee it can correctly |
| 7 | /// handle hazards at the beginning of scheduling regions. |
| 8 | /// |
| 9 | /// This pass traverses all the instructions in a program in top-down order. |
| 10 | /// In contrast to the instruction scheduling passes, this pass never resets |
| 11 | /// the hazard recognizer to ensure it can correctly handles noop hazards at |
Hiroshi Inoue | e9dea6e | 2017-07-13 06:48:39 +0000 | [diff] [blame] | 12 | /// the beginning of blocks. |
Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
| 17 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/Passes.h" |
Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 22 | #include "llvm/InitializePasses.h" |
Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | #define DEBUG_TYPE "post-RA-hazard-rec" |
| 29 | |
| 30 | STATISTIC(NumNoops, "Number of noops inserted"); |
| 31 | |
| 32 | namespace { |
| 33 | class PostRAHazardRecognizer : public MachineFunctionPass { |
Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 34 | |
| 35 | public: |
| 36 | static char ID; |
| 37 | PostRAHazardRecognizer() : MachineFunctionPass(ID) {} |
| 38 | |
| 39 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 40 | AU.setPreservesCFG(); |
| 41 | MachineFunctionPass::getAnalysisUsage(AU); |
| 42 | } |
| 43 | |
| 44 | bool runOnMachineFunction(MachineFunction &Fn) override; |
| 45 | |
| 46 | }; |
| 47 | char PostRAHazardRecognizer::ID = 0; |
| 48 | |
| 49 | } |
| 50 | |
| 51 | char &llvm::PostRAHazardRecognizerID = PostRAHazardRecognizer::ID; |
| 52 | |
| 53 | INITIALIZE_PASS(PostRAHazardRecognizer, DEBUG_TYPE, |
| 54 | "Post RA hazard recognizer", false, false) |
| 55 | |
| 56 | bool PostRAHazardRecognizer::runOnMachineFunction(MachineFunction &Fn) { |
| 57 | const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo(); |
| 58 | std::unique_ptr<ScheduleHazardRecognizer> HazardRec( |
| 59 | TII->CreateTargetPostRAHazardRecognizer(Fn)); |
| 60 | |
| 61 | // Return if the target has not implemented a hazard recognizer. |
| 62 | if (!HazardRec.get()) |
| 63 | return false; |
| 64 | |
| 65 | // Loop over all of the basic blocks |
| 66 | for (auto &MBB : Fn) { |
| 67 | // We do not call HazardRec->reset() here to make sure we are handling noop |
| 68 | // hazards at the start of basic blocks. |
Duncan P. N. Exon Smith | 286d948 | 2016-07-01 00:50:29 +0000 | [diff] [blame] | 69 | for (MachineInstr &MI : MBB) { |
Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 70 | // If we need to emit noops prior to this instruction, then do so. |
Duncan P. N. Exon Smith | 286d948 | 2016-07-01 00:50:29 +0000 | [diff] [blame] | 71 | unsigned NumPreNoops = HazardRec->PreEmitNoops(&MI); |
Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 72 | for (unsigned i = 0; i != NumPreNoops; ++i) { |
| 73 | HazardRec->EmitNoop(); |
Duncan P. N. Exon Smith | 286d948 | 2016-07-01 00:50:29 +0000 | [diff] [blame] | 74 | TII->insertNoop(MBB, MachineBasicBlock::iterator(MI)); |
Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 75 | ++NumNoops; |
| 76 | } |
| 77 | |
Duncan P. N. Exon Smith | 286d948 | 2016-07-01 00:50:29 +0000 | [diff] [blame] | 78 | HazardRec->EmitInstruction(&MI); |
Tom Stellard | ee34680 | 2016-04-22 14:43:50 +0000 | [diff] [blame] | 79 | if (HazardRec->atIssueLimit()) { |
| 80 | HazardRec->AdvanceCycle(); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return true; |
| 85 | } |