blob: da67c1bcea9980f369f1a7959ac4d8a4b9caa42a [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +00001//===- MipsHazardSchedule.cpp - Workaround pipeline hazards ---------------===//
Daniel Sanderse8efff32016-03-14 16:24:05 +00002//
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 Dardise8af7922016-12-13 11:10:53 +000010/// 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 Sanderse8efff32016-03-14 16:24:05 +000014///
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 Dardise8af7922016-12-13 11:10:53 +000021/// 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 Sanderse8efff32016-03-14 16:24:05 +000023///
24/// For example:
25///
NAKAMURA Takumife1202c2016-06-20 00:37:41 +000026/// 0x8004 bnec a1,v0,<P+0x18>
27/// 0x8008 beqc a1,a2,<P+0x54>
Daniel Sanderse8efff32016-03-14 16:24:05 +000028///
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 Pilgrim68168d12017-03-30 12:59:53 +000039/// difficult to process as lookahead for hazards is insufficient, as
Daniel Sanderse8efff32016-03-14 16:24:05 +000040/// 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 Zelenko79220eae2017-08-03 22:12:30 +000047#include "MipsSubtarget.h"
Daniel Sanderse8efff32016-03-14 16:24:05 +000048#include "llvm/ADT/Statistic.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000049#include "llvm/CodeGen/MachineBasicBlock.h"
50#include "llvm/CodeGen/MachineFunction.h"
Daniel Sanderse8efff32016-03-14 16:24:05 +000051#include "llvm/CodeGen/MachineFunctionPass.h"
52#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000053#include <algorithm>
54#include <iterator>
55#include <utility>
Daniel Sanderse8efff32016-03-14 16:24:05 +000056
57using namespace llvm;
58
59#define DEBUG_TYPE "mips-hazard-schedule"
60
61STATISTIC(NumInsertedNops, "Number of nops inserted");
62
63namespace {
64
Eugene Zelenko79220eae2017-08-03 22:12:30 +000065using Iter = MachineBasicBlock::iterator;
66using ReverseIter = MachineBasicBlock::reverse_iterator;
Daniel Sanderse8efff32016-03-14 16:24:05 +000067
68class MipsHazardSchedule : public MachineFunctionPass {
Daniel Sanderse8efff32016-03-14 16:24:05 +000069public:
Chad Rosier7a21bb12016-03-14 18:10:20 +000070 MipsHazardSchedule() : MachineFunctionPass(ID) {}
Daniel Sanderse8efff32016-03-14 16:24:05 +000071
Mehdi Amini117296c2016-10-01 02:56:57 +000072 StringRef getPassName() const override { return "Mips Hazard Schedule"; }
Daniel Sanderse8efff32016-03-14 16:24:05 +000073
74 bool runOnMachineFunction(MachineFunction &F) override;
75
Derek Schuff1dbf7a52016-04-04 17:09:25 +000076 MachineFunctionProperties getRequiredProperties() const override {
77 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000078 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000079 }
80
Daniel Sanderse8efff32016-03-14 16:24:05 +000081private:
82 static char ID;
Daniel Sanderse8efff32016-03-14 16:24:05 +000083};
84
Daniel Sanderse8efff32016-03-14 16:24:05 +000085} // end of anonymous namespace
86
Eugene Zelenko79220eae2017-08-03 22:12:30 +000087char MipsHazardSchedule::ID = 0;
88
Daniel Sanderse8efff32016-03-14 16:24:05 +000089/// Returns a pass that clears pipeline hazards.
Chad Rosier7a21bb12016-03-14 18:10:20 +000090FunctionPass *llvm::createMipsHazardSchedule() {
91 return new MipsHazardSchedule();
Daniel Sanderse8efff32016-03-14 16:24:05 +000092}
93
Simon Dardis43b5ce42016-12-13 11:07:51 +000094// Find the next real instruction from the current position in current basic
95// block.
96static Iter getNextMachineInstrInBB(Iter Position) {
Simon Dardis7383bfd82016-04-29 16:04:18 +000097 Iter I = Position, E = Position->getParent()->end();
Simon Dardis43b5ce42016-12-13 11:07:51 +000098 I = std::find_if_not(I, E,
99 [](const Iter &Insn) { return Insn->isTransient(); });
100
Simon Dardis7383bfd82016-04-29 16:04:18 +0000101 return I;
102}
103
Simon Dardis43b5ce42016-12-13 11:07:51 +0000104// Find the next real instruction from the current position, looking through
105// basic block boundaries.
Simon Dardis0a47edb2017-04-04 11:28:53 +0000106static std::pair<Iter, bool> getNextMachineInstr(Iter Position, MachineBasicBlock * Parent) {
Petar Jovanovic8a4e6392016-12-22 19:29:50 +0000107 if (Position == Parent->end()) {
Simon Dardis0a47edb2017-04-04 11:28:53 +0000108 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 Dardis43b5ce42016-12-13 11:07:51 +0000117 }
118
Petar Jovanovic8a4e6392016-12-22 19:29:50 +0000119 Iter Instr = getNextMachineInstrInBB(Position);
120 if (Instr == Parent->end()) {
121 return getNextMachineInstr(Instr, Parent);
122 }
Simon Dardis0a47edb2017-04-04 11:28:53 +0000123 return std::make_pair(Instr, false);
Simon Dardis43b5ce42016-12-13 11:07:51 +0000124}
125
Daniel Sanderse8efff32016-03-14 16:24:05 +0000126bool MipsHazardSchedule::runOnMachineFunction(MachineFunction &MF) {
127
128 const MipsSubtarget *STI =
129 &static_cast<const MipsSubtarget &>(MF.getSubtarget());
130
Simon Dardis43b5ce42016-12-13 11:07:51 +0000131 // Forbidden slot hazards are only defined for MIPSR6 but not microMIPSR6.
Daniel Sanderse8efff32016-03-14 16:24:05 +0000132 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 Dardis43b5ce42016-12-13 11:07:51 +0000145 Iter Inst;
146 bool LastInstInFunction =
147 std::next(I) == FI->end() && std::next(FI) == MF.end();
148 if (!LastInstInFunction) {
Simon Dardis0a47edb2017-04-04 11:28:53 +0000149 std::pair<Iter, bool> Res = getNextMachineInstr(std::next(I), &*FI);
150 LastInstInFunction |= Res.second;
151 Inst = Res.first;
Daniel Sanderse8efff32016-03-14 16:24:05 +0000152 }
153
Simon Dardis43b5ce42016-12-13 11:07:51 +0000154 if (LastInstInFunction || !TII->SafeInForbiddenSlot(*Inst)) {
Daniel Sanderse8efff32016-03-14 16:24:05 +0000155 Changed = true;
Simon Dardis43b5ce42016-12-13 11:07:51 +0000156 MIBundleBuilder(&*I)
157 .append(BuildMI(MF, I->getDebugLoc(), TII->get(Mips::NOP)));
Daniel Sanderse8efff32016-03-14 16:24:05 +0000158 NumInsertedNops++;
159 }
160 }
161 }
162 return Changed;
163}