blob: e15424e9fb7af0d53185fce7179a7f3a6851c693 [file] [log] [blame]
Chad Rosier7a21bb12016-03-14 18:10:20 +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
10/// This pass is used to workaround certain pipeline hazards. For now, this covers
11/// compact branch hazards. In future this pass can be extended to other pipeline
12/// hazards, such as various MIPS1 hazards, processor errata that require
13/// instruction reorganization, etc.
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
21/// and the adjacent instruction in memory is a control transfer instruction such
22/// as a branch or jump, ERET, ERETNC, DERET, WAIT and PAUSE.
23///
24/// For example:
25///
26/// 0x8004 bnec a1,v0,<P+0x18>
27/// 0x8008 beqc a1,a2,<P+0x54>
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
39/// difficult to process as lookahead for hazards is insufficent, as
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"
47#include "MipsSEInstrInfo.h"
48#include "MipsTargetMachine.h"
49#include "llvm/IR/Function.h"
50#include "llvm/ADT/Statistic.h"
51#include "llvm/CodeGen/MachineFunctionPass.h"
52#include "llvm/CodeGen/MachineInstrBuilder.h"
53#include "llvm/Support/CommandLine.h"
54#include "llvm/Target/TargetInstrInfo.h"
55#include "llvm/Target/TargetMachine.h"
56#include "llvm/Target/TargetRegisterInfo.h"
57
58using namespace llvm;
59
60#define DEBUG_TYPE "mips-hazard-schedule"
61
62STATISTIC(NumInsertedNops, "Number of nops inserted");
63
64namespace {
65
66typedef MachineBasicBlock::iterator Iter;
67typedef MachineBasicBlock::reverse_iterator ReverseIter;
68
69class MipsHazardSchedule : public MachineFunctionPass {
70
71public:
Chad Rosier7a21bb12016-03-14 18:10:20 +000072 MipsHazardSchedule() : MachineFunctionPass(ID) {}
Daniel Sanderse8efff32016-03-14 16:24:05 +000073
74 const char *getPassName() const override { return "Mips Hazard Schedule"; }
75
76 bool runOnMachineFunction(MachineFunction &F) override;
77
Derek Schuff1dbf7a52016-04-04 17:09:25 +000078 MachineFunctionProperties getRequiredProperties() const override {
79 return MachineFunctionProperties().set(
80 MachineFunctionProperties::Property::AllVRegsAllocated);
81 }
82
Daniel Sanderse8efff32016-03-14 16:24:05 +000083private:
84 static char ID;
Daniel Sanderse8efff32016-03-14 16:24:05 +000085};
86
87char MipsHazardSchedule::ID = 0;
88} // end of anonymous namespace
89
90/// Returns a pass that clears pipeline hazards.
Chad Rosier7a21bb12016-03-14 18:10:20 +000091FunctionPass *llvm::createMipsHazardSchedule() {
92 return new MipsHazardSchedule();
Daniel Sanderse8efff32016-03-14 16:24:05 +000093}
94
95bool MipsHazardSchedule::runOnMachineFunction(MachineFunction &MF) {
96
97 const MipsSubtarget *STI =
98 &static_cast<const MipsSubtarget &>(MF.getSubtarget());
99
100 // Forbidden slot hazards are only defined for MIPSR6.
101 if (!STI->hasMips32r6() || STI->inMicroMipsMode())
102 return false;
103
104 bool Changed = false;
105 const MipsInstrInfo *TII = STI->getInstrInfo();
106
107 for (MachineFunction::iterator FI = MF.begin(); FI != MF.end(); ++FI) {
108 for (Iter I = FI->begin(); I != FI->end(); ++I) {
109
110 // Forbidden slot hazard handling. Use lookahead over state.
111 if (!TII->HasForbiddenSlot(*I))
112 continue;
113
114 bool InsertNop = false;
115 // Next instruction in the basic block.
116 if (std::next(I) != FI->end() &&
117 !TII->SafeInForbiddenSlot(*std::next(I))) {
118 InsertNop = true;
119 } else {
120 // Next instruction in the physical successor basic block.
121 for (auto *Succ : FI->successors()) {
122 if (FI->isLayoutSuccessor(Succ) &&
123 Succ->getFirstNonDebugInstr() != Succ->end() &&
124 !TII->SafeInForbiddenSlot(*Succ->getFirstNonDebugInstr())) {
125 InsertNop = true;
126 break;
127 }
128 }
129 }
130
131 if (InsertNop) {
132 Changed = true;
133 MIBundleBuilder(I)
134 .append(BuildMI(MF, I->getDebugLoc(), TII->get(Mips::NOP)));
135 NumInsertedNops++;
136 }
137 }
138 }
139 return Changed;
140}