1 //===-- DelaySlotFiller.cpp - MBlaze delay slot filler --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // A pass that attempts to fill instructions with delay slots. If no
11 // instructions can be moved into the delay slot then a NOP is placed there.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "delay-slot-filler"
18 #include "MBlazeTargetMachine.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetInstrInfo.h"
30 STATISTIC(FilledSlots, "Number of delay slots filled");
32 static cl::opt<bool> MBDisableDelaySlotFiller(
33 "disable-mblaze-delay-filler",
35 cl::desc("Disable the MBlaze delay slot filter."),
39 struct Filler : public MachineFunctionPass {
43 Filler(TargetMachine &tm)
44 : MachineFunctionPass(ID), TM(tm) { }
46 virtual const char *getPassName() const {
47 return "MBlaze Delay Slot Filler";
50 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
51 bool runOnMachineFunction(MachineFunction &F) {
53 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
55 Changed |= runOnMachineBasicBlock(*FI);
61 } // end of anonymous namespace
63 static bool hasImmInstruction(MachineBasicBlock::iterator &candidate) {
64 // Any instruction with an immediate mode operand greater than
65 // 16-bits requires an implicit IMM instruction.
66 unsigned numOper = candidate->getNumOperands();
67 for (unsigned op = 0; op < numOper; ++op) {
68 MachineOperand &mop = candidate->getOperand(op);
70 // The operand requires more than 16-bits to represent.
71 if (mop.isImm() && (mop.getImm() < -0x8000 || mop.getImm() > 0x7fff))
74 // We must assume that unknown immediate values require more than
75 // 16-bits to represent.
76 if (mop.isGlobal() || mop.isSymbol() || mop.isJTI() || mop.isCPI())
79 // FIXME: we could probably check to see if the FP value happens
80 // to not need an IMM instruction. For now we just always
81 // assume that FP values do.
89 static unsigned getLastRealOperand(MachineBasicBlock::iterator &instr) {
90 switch (instr->getOpcode()) {
91 default: return instr->getNumOperands();
93 // These instructions have a variable number of operands but the first two
94 // are the "real" operands that we care about during hazard detection.
103 static bool delayHasHazard(MachineBasicBlock::iterator &candidate,
104 MachineBasicBlock::iterator &slot) {
106 MachineBasicBlock::iterator a = candidate;
107 MachineBasicBlock::iterator b = slot;
110 // candidate := a0 = operation(a1, a2)
112 // slot := b0 = operation(b1, b2)
114 // Possible hazards:-/
115 // 1. a1 or a2 was written during the middle bit
116 // 2. a0 was read or written during the middle bit
117 // 3. a0 is one or more of {b0, b1, b2}
118 // 4. b0 is one or more of {a1, a2}
119 // 5. a accesses memory, and the middle bit
120 // contains a store operation.
121 bool a_is_memory = candidate->mayLoad() || candidate->mayStore();
123 // Determine the number of operands in the slot instruction and in the
124 // candidate instruction.
125 const unsigned aend = getLastRealOperand(a);
126 const unsigned bend = getLastRealOperand(b);
128 // Check hazards type 1, 2 and 5 by scanning the middle bit
129 MachineBasicBlock::iterator m = a;
130 for (++m; m != b; ++m) {
131 for (unsigned aop = 0; aop<aend; ++aop) {
132 bool aop_is_reg = a->getOperand(aop).isReg();
133 if (!aop_is_reg) continue;
135 bool aop_is_def = a->getOperand(aop).isDef();
136 unsigned aop_reg = a->getOperand(aop).getReg();
138 const unsigned mend = getLastRealOperand(m);
139 for (unsigned mop = 0; mop<mend; ++mop) {
140 bool mop_is_reg = m->getOperand(mop).isReg();
141 if (!mop_is_reg) continue;
143 bool mop_is_def = m->getOperand(mop).isDef();
144 unsigned mop_reg = m->getOperand(mop).getReg();
146 if (aop_is_def && (mop_reg == aop_reg))
147 return true; // Hazard type 2, because aop = a0
148 else if (mop_is_def && (mop_reg == aop_reg))
149 return true; // Hazard type 1, because aop in {a1, a2}
153 // Check hazard type 5
154 if (a_is_memory && m->mayStore())
158 // Check hazard type 3 & 4
159 for (unsigned aop = 0; aop<aend; ++aop) {
160 if (a->getOperand(aop).isReg()) {
161 unsigned aop_reg = a->getOperand(aop).getReg();
163 for (unsigned bop = 0; bop<bend; ++bop) {
164 if (b->getOperand(bop).isReg() && !b->getOperand(bop).isImplicit()) {
165 unsigned bop_reg = b->getOperand(bop).getReg();
166 if (aop_reg == bop_reg)
176 static bool isDelayFiller(MachineBasicBlock &MBB,
177 MachineBasicBlock::iterator candidate) {
178 if (candidate == MBB.begin())
182 return (candidate->hasDelaySlot());
185 static bool hasUnknownSideEffects(MachineBasicBlock::iterator &I) {
186 if (!I->hasUnmodeledSideEffects())
189 unsigned op = I->getOpcode();
190 if (op == MBlaze::ADDK || op == MBlaze::ADDIK ||
191 op == MBlaze::ADDC || op == MBlaze::ADDIC ||
192 op == MBlaze::ADDKC || op == MBlaze::ADDIKC ||
193 op == MBlaze::RSUBK || op == MBlaze::RSUBIK ||
194 op == MBlaze::RSUBC || op == MBlaze::RSUBIC ||
195 op == MBlaze::RSUBKC || op == MBlaze::RSUBIKC)
201 static MachineBasicBlock::iterator
202 findDelayInstr(MachineBasicBlock &MBB,MachineBasicBlock::iterator slot) {
203 MachineBasicBlock::iterator I = slot;
205 if (I == MBB.begin())
209 if (I->hasDelaySlot() || I->isBranch() || isDelayFiller(MBB,I) ||
210 I->isCall() || I->isReturn() || I->isBarrier() ||
211 hasUnknownSideEffects(I))
214 if (hasImmInstruction(I) || delayHasHazard(I,slot))
223 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
224 /// Currently, we fill delay slots with NOPs. We assume there is only one
225 /// delay slot per delayed instruction.
226 bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
227 bool Changed = false;
228 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
229 if (I->hasDelaySlot()) {
230 MachineBasicBlock::iterator D = MBB.end();
231 MachineBasicBlock::iterator J = I;
233 if (!MBDisableDelaySlotFiller)
234 D = findDelayInstr(MBB,I);
240 BuildMI(MBB, ++J, I->getDebugLoc(),TM.getInstrInfo()->get(MBlaze::NOP));
242 MBB.splice(++J, &MBB, D);
247 /// createMBlazeDelaySlotFillerPass - Returns a pass that fills in delay
248 /// slots in MBlaze MachineFunctions
249 FunctionPass *llvm::createMBlazeDelaySlotFillerPass(MBlazeTargetMachine &tm) {
250 return new Filler(tm);