Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 1 | //===- MipsHazardSchedule.cpp - Workaround pipeline hazards ---------------===// |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 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 | /// \file |
Simon Dardis | e8af792 | 2016-12-13 11:10:53 +0000 | [diff] [blame] | 10 | /// This pass is used to workaround certain pipeline hazards. For now, this |
| 11 | /// covers compact branch hazards. In future this pass can be extended to other |
| 12 | /// pipeline hazards, such as various MIPS1 hazards, processor errata that |
| 13 | /// require instruction reorganization, etc. |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 14 | /// |
| 15 | /// This pass has to run after the delay slot filler as that pass can introduce |
| 16 | /// pipeline hazards, hence the existing hazard recognizer is not suitable. |
| 17 | /// |
| 18 | /// Hazards handled: forbidden slots for MIPSR6. |
| 19 | /// |
| 20 | /// A forbidden slot hazard occurs when a compact branch instruction is executed |
Simon Dardis | e8af792 | 2016-12-13 11:10:53 +0000 | [diff] [blame] | 21 | /// and the adjacent instruction in memory is a control transfer instruction |
| 22 | /// such as a branch or jump, ERET, ERETNC, DERET, WAIT and PAUSE. |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 23 | /// |
| 24 | /// For example: |
| 25 | /// |
NAKAMURA Takumi | fe1202c | 2016-06-20 00:37:41 +0000 | [diff] [blame] | 26 | /// 0x8004 bnec a1,v0,<P+0x18> |
| 27 | /// 0x8008 beqc a1,a2,<P+0x54> |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 28 | /// |
| 29 | /// In such cases, the processor is required to signal a Reserved Instruction |
| 30 | /// exception. |
| 31 | /// |
| 32 | /// Here, if the instruction at 0x8004 is executed, the processor will raise an |
| 33 | /// exception as there is a control transfer instruction at 0x8008. |
| 34 | /// |
| 35 | /// There are two sources of forbidden slot hazards: |
| 36 | /// |
| 37 | /// A) A previous pass has created a compact branch directly. |
| 38 | /// B) Transforming a delay slot branch into compact branch. This case can be |
Simon Pilgrim | 68168d1 | 2017-03-30 12:59:53 +0000 | [diff] [blame] | 39 | /// difficult to process as lookahead for hazards is insufficient, as |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 40 | /// backwards delay slot fillling can also produce hazards in previously |
| 41 | /// processed instuctions. |
| 42 | /// |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | #include "Mips.h" |
| 46 | #include "MipsInstrInfo.h" |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 47 | #include "MipsSubtarget.h" |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 49 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 50 | #include "llvm/CodeGen/MachineFunction.h" |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 51 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 52 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 53 | #include <algorithm> |
| 54 | #include <iterator> |
| 55 | #include <utility> |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 56 | |
| 57 | using namespace llvm; |
| 58 | |
| 59 | #define DEBUG_TYPE "mips-hazard-schedule" |
| 60 | |
| 61 | STATISTIC(NumInsertedNops, "Number of nops inserted"); |
| 62 | |
| 63 | namespace { |
| 64 | |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 65 | using Iter = MachineBasicBlock::iterator; |
| 66 | using ReverseIter = MachineBasicBlock::reverse_iterator; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 67 | |
| 68 | class MipsHazardSchedule : public MachineFunctionPass { |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 69 | public: |
Chad Rosier | 7a21bb1 | 2016-03-14 18:10:20 +0000 | [diff] [blame] | 70 | MipsHazardSchedule() : MachineFunctionPass(ID) {} |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 71 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 72 | StringRef getPassName() const override { return "Mips Hazard Schedule"; } |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 73 | |
| 74 | bool runOnMachineFunction(MachineFunction &F) override; |
| 75 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 76 | MachineFunctionProperties getRequiredProperties() const override { |
| 77 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 78 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 81 | private: |
| 82 | static char ID; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 85 | } // end of anonymous namespace |
| 86 | |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 87 | char MipsHazardSchedule::ID = 0; |
| 88 | |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 89 | /// Returns a pass that clears pipeline hazards. |
Chad Rosier | 7a21bb1 | 2016-03-14 18:10:20 +0000 | [diff] [blame] | 90 | FunctionPass *llvm::createMipsHazardSchedule() { |
| 91 | return new MipsHazardSchedule(); |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Simon Dardis | 43b5ce4 | 2016-12-13 11:07:51 +0000 | [diff] [blame] | 94 | // Find the next real instruction from the current position in current basic |
| 95 | // block. |
| 96 | static Iter getNextMachineInstrInBB(Iter Position) { |
Simon Dardis | 7383bfd8 | 2016-04-29 16:04:18 +0000 | [diff] [blame] | 97 | Iter I = Position, E = Position->getParent()->end(); |
Simon Dardis | 43b5ce4 | 2016-12-13 11:07:51 +0000 | [diff] [blame] | 98 | I = std::find_if_not(I, E, |
| 99 | [](const Iter &Insn) { return Insn->isTransient(); }); |
| 100 | |
Simon Dardis | 7383bfd8 | 2016-04-29 16:04:18 +0000 | [diff] [blame] | 101 | return I; |
| 102 | } |
| 103 | |
Simon Dardis | 43b5ce4 | 2016-12-13 11:07:51 +0000 | [diff] [blame] | 104 | // Find the next real instruction from the current position, looking through |
| 105 | // basic block boundaries. |
Simon Dardis | 0a47edb | 2017-04-04 11:28:53 +0000 | [diff] [blame] | 106 | static std::pair<Iter, bool> getNextMachineInstr(Iter Position, MachineBasicBlock * Parent) { |
Petar Jovanovic | 8a4e639 | 2016-12-22 19:29:50 +0000 | [diff] [blame] | 107 | if (Position == Parent->end()) { |
Simon Dardis | 0a47edb | 2017-04-04 11:28:53 +0000 | [diff] [blame] | 108 | do { |
| 109 | MachineBasicBlock *Succ = Parent->getNextNode(); |
| 110 | if (Succ != nullptr && Parent->isSuccessor(Succ)) { |
| 111 | Position = Succ->begin(); |
| 112 | Parent = Succ; |
| 113 | } else { |
| 114 | return std::make_pair(Position, true); |
| 115 | } |
| 116 | } while (Parent->empty()); |
Simon Dardis | 43b5ce4 | 2016-12-13 11:07:51 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Petar Jovanovic | 8a4e639 | 2016-12-22 19:29:50 +0000 | [diff] [blame] | 119 | Iter Instr = getNextMachineInstrInBB(Position); |
| 120 | if (Instr == Parent->end()) { |
| 121 | return getNextMachineInstr(Instr, Parent); |
| 122 | } |
Simon Dardis | 0a47edb | 2017-04-04 11:28:53 +0000 | [diff] [blame] | 123 | return std::make_pair(Instr, false); |
Simon Dardis | 43b5ce4 | 2016-12-13 11:07:51 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 126 | bool MipsHazardSchedule::runOnMachineFunction(MachineFunction &MF) { |
| 127 | |
| 128 | const MipsSubtarget *STI = |
| 129 | &static_cast<const MipsSubtarget &>(MF.getSubtarget()); |
| 130 | |
Simon Dardis | 43b5ce4 | 2016-12-13 11:07:51 +0000 | [diff] [blame] | 131 | // Forbidden slot hazards are only defined for MIPSR6 but not microMIPSR6. |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 132 | if (!STI->hasMips32r6() || STI->inMicroMipsMode()) |
| 133 | return false; |
| 134 | |
| 135 | bool Changed = false; |
| 136 | const MipsInstrInfo *TII = STI->getInstrInfo(); |
| 137 | |
| 138 | for (MachineFunction::iterator FI = MF.begin(); FI != MF.end(); ++FI) { |
| 139 | for (Iter I = FI->begin(); I != FI->end(); ++I) { |
| 140 | |
| 141 | // Forbidden slot hazard handling. Use lookahead over state. |
| 142 | if (!TII->HasForbiddenSlot(*I)) |
| 143 | continue; |
| 144 | |
Simon Dardis | 43b5ce4 | 2016-12-13 11:07:51 +0000 | [diff] [blame] | 145 | Iter Inst; |
| 146 | bool LastInstInFunction = |
| 147 | std::next(I) == FI->end() && std::next(FI) == MF.end(); |
| 148 | if (!LastInstInFunction) { |
Simon Dardis | 0a47edb | 2017-04-04 11:28:53 +0000 | [diff] [blame] | 149 | std::pair<Iter, bool> Res = getNextMachineInstr(std::next(I), &*FI); |
| 150 | LastInstInFunction |= Res.second; |
| 151 | Inst = Res.first; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Simon Dardis | 43b5ce4 | 2016-12-13 11:07:51 +0000 | [diff] [blame] | 154 | if (LastInstInFunction || !TII->SafeInForbiddenSlot(*Inst)) { |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 155 | Changed = true; |
Simon Dardis | 43b5ce4 | 2016-12-13 11:07:51 +0000 | [diff] [blame] | 156 | MIBundleBuilder(&*I) |
| 157 | .append(BuildMI(MF, I->getDebugLoc(), TII->get(Mips::NOP))); |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 158 | NumInsertedNops++; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | return Changed; |
| 163 | } |