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/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/Target/TargetInstrInfo.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
30 STATISTIC(FilledSlots, "Number of delay slots filled");
33 cl::opt<bool> DisableDelaySlotFiller(
34 "disable-mblaze-delay-filler",
36 cl::desc("Disable the MBlaze delay slot filter."),
41 struct Filler : public MachineFunctionPass {
44 const TargetInstrInfo *TII;
47 Filler(TargetMachine &tm)
48 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
50 virtual const char *getPassName() const {
51 return "MBlaze Delay Slot Filler";
54 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
55 bool runOnMachineFunction(MachineFunction &F) {
57 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
59 Changed |= runOnMachineBasicBlock(*FI);
65 } // end of anonymous namespace
67 static bool hasImmInstruction(MachineBasicBlock::iterator &candidate) {
68 // Any instruction with an immediate mode operand greater than
69 // 16-bits requires an implicit IMM instruction.
70 unsigned numOper = candidate->getNumOperands();
71 for (unsigned op = 0; op < numOper; ++op) {
72 MachineOperand &mop = candidate->getOperand(op);
74 // The operand requires more than 16-bits to represent.
75 if (mop.isImm() && (mop.getImm() < -0x8000 || mop.getImm() > 0x7fff))
78 // We must assume that unknown immediate values require more than
79 // 16-bits to represent.
80 if (mop.isGlobal() || mop.isSymbol() || mop.isJTI() || mop.isCPI())
83 // FIXME: we could probably check to see if the FP value happens
84 // to not need an IMM instruction. For now we just always
85 // assume that FP values do.
93 static unsigned getLastRealOperand(MachineBasicBlock::iterator &instr) {
94 switch (instr->getOpcode()) {
95 default: return instr->getNumOperands();
97 // These instructions have a variable number of operands but the first two
98 // are the "real" operands that we care about during hazard detection.
107 static bool delayHasHazard(MachineBasicBlock::iterator &candidate,
108 MachineBasicBlock::iterator &slot) {
110 MachineBasicBlock::iterator a = candidate;
111 MachineBasicBlock::iterator b = slot;
112 MCInstrDesc desc = candidate->getDesc();
115 // candidate := a0 = operation(a1, a2)
117 // slot := b0 = operation(b1, b2)
119 // Possible hazards:-/
120 // 1. a1 or a2 was written during the middle bit
121 // 2. a0 was read or written during the middle bit
122 // 3. a0 is one or more of {b0, b1, b2}
123 // 4. b0 is one or more of {a1, a2}
124 // 5. a accesses memory, and the middle bit
125 // contains a store operation.
126 bool a_is_memory = desc.mayLoad() || desc.mayStore();
128 // Determine the number of operands in the slot instruction and in the
129 // candidate instruction.
130 const unsigned aend = getLastRealOperand(a);
131 const unsigned bend = getLastRealOperand(b);
133 // Check hazards type 1, 2 and 5 by scanning the middle bit
134 MachineBasicBlock::iterator m = a;
135 for (++m; m != b; ++m) {
136 for (unsigned aop = 0; aop<aend; ++aop) {
137 bool aop_is_reg = a->getOperand(aop).isReg();
138 if (!aop_is_reg) continue;
140 bool aop_is_def = a->getOperand(aop).isDef();
141 unsigned aop_reg = a->getOperand(aop).getReg();
143 const unsigned mend = getLastRealOperand(m);
144 for (unsigned mop = 0; mop<mend; ++mop) {
145 bool mop_is_reg = m->getOperand(mop).isReg();
146 if (!mop_is_reg) continue;
148 bool mop_is_def = m->getOperand(mop).isDef();
149 unsigned mop_reg = m->getOperand(mop).getReg();
151 if (aop_is_def && (mop_reg == aop_reg))
152 return true; // Hazard type 2, because aop = a0
153 else if (mop_is_def && (mop_reg == aop_reg))
154 return true; // Hazard type 1, because aop in {a1, a2}
158 // Check hazard type 5
159 if (a_is_memory && m->getDesc().mayStore())
163 // Check hazard type 3 & 4
164 for (unsigned aop = 0; aop<aend; ++aop) {
165 if (a->getOperand(aop).isReg()) {
166 unsigned aop_reg = a->getOperand(aop).getReg();
168 for (unsigned bop = 0; bop<bend; ++bop) {
169 if (b->getOperand(bop).isReg() && !b->getOperand(bop).isImplicit()) {
170 unsigned bop_reg = b->getOperand(bop).getReg();
171 if (aop_reg == bop_reg)
181 static bool isDelayFiller(MachineBasicBlock &MBB,
182 MachineBasicBlock::iterator candidate) {
183 if (candidate == MBB.begin())
186 MCInstrDesc brdesc = (--candidate)->getDesc();
187 return (brdesc.hasDelaySlot());
190 static bool hasUnknownSideEffects(MachineBasicBlock::iterator &I) {
191 if (!I->hasUnmodeledSideEffects())
194 unsigned op = I->getOpcode();
195 if (op == MBlaze::ADDK || op == MBlaze::ADDIK ||
196 op == MBlaze::ADDC || op == MBlaze::ADDIC ||
197 op == MBlaze::ADDKC || op == MBlaze::ADDIKC ||
198 op == MBlaze::RSUBK || op == MBlaze::RSUBIK ||
199 op == MBlaze::RSUBC || op == MBlaze::RSUBIC ||
200 op == MBlaze::RSUBKC || op == MBlaze::RSUBIKC)
206 static MachineBasicBlock::iterator
207 findDelayInstr(MachineBasicBlock &MBB,MachineBasicBlock::iterator slot) {
208 MachineBasicBlock::iterator I = slot;
210 if (I == MBB.begin())
214 MCInstrDesc desc = I->getDesc();
215 if (desc.hasDelaySlot() || desc.isBranch() || isDelayFiller(MBB,I) ||
216 desc.isCall() || desc.isReturn() || desc.isBarrier() ||
217 hasUnknownSideEffects(I))
220 if (hasImmInstruction(I) || delayHasHazard(I,slot))
229 /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
230 /// Currently, we fill delay slots with NOPs. We assume there is only one
231 /// delay slot per delayed instruction.
232 bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
233 bool Changed = false;
234 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
235 if (I->getDesc().hasDelaySlot()) {
236 MachineBasicBlock::iterator D = MBB.end();
237 MachineBasicBlock::iterator J = I;
239 if (!DisableDelaySlotFiller)
240 D = findDelayInstr(MBB,I);
246 BuildMI(MBB, ++J, I->getDebugLoc(), TII->get(MBlaze::NOP));
248 MBB.splice(++J, &MBB, D);
253 /// createMBlazeDelaySlotFillerPass - Returns a pass that fills in delay
254 /// slots in MBlaze MachineFunctions
255 FunctionPass *llvm::createMBlazeDelaySlotFillerPass(MBlazeTargetMachine &tm) {
256 return new Filler(tm);