blob: f38f90b8f80f1c913f2f1a6993dfd73135349759 [file] [log] [blame]
Wesley Pecka70f28c2010-02-23 19:15:24 +00001//===-- DelaySlotFiller.cpp - MBlaze delay slot filler --------------------===//
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//
Wesley Pecka0603832010-10-27 00:23:01 +000010// 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.
Wesley Pecka70f28c2010-02-23 19:15:24 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "delay-slot-filler"
16
17#include "MBlaze.h"
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"
Wesley Peck4e9141f2010-10-21 03:57:26 +000023#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/raw_ostream.h"
Wesley Pecka70f28c2010-02-23 19:15:24 +000027
28using namespace llvm;
29
30STATISTIC(FilledSlots, "Number of delay slots filled");
31
Wesley Peck1e8cdd52010-12-06 21:11:01 +000032namespace llvm {
33cl::opt<bool> DisableDelaySlotFiller(
34 "disable-mblaze-delay-filler",
35 cl::init(false),
36 cl::desc("Disable the MBlaze delay slot filter."),
37 cl::Hidden);
38}
39
Wesley Pecka70f28c2010-02-23 19:15:24 +000040namespace {
41 struct Filler : public MachineFunctionPass {
42
43 TargetMachine &TM;
44 const TargetInstrInfo *TII;
45
46 static char ID;
Wesley Peck0a67d922010-11-08 19:40:01 +000047 Filler(TargetMachine &tm)
Owen Anderson90c579d2010-08-06 18:33:48 +000048 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
Wesley Pecka70f28c2010-02-23 19:15:24 +000049
50 virtual const char *getPassName() const {
51 return "MBlaze Delay Slot Filler";
52 }
53
54 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
55 bool runOnMachineFunction(MachineFunction &F) {
56 bool Changed = false;
57 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
58 FI != FE; ++FI)
59 Changed |= runOnMachineBasicBlock(*FI);
60 return Changed;
61 }
62
63 };
64 char Filler::ID = 0;
65} // end of anonymous namespace
66
Wesley Peck0a67d922010-11-08 19:40:01 +000067static bool hasImmInstruction(MachineBasicBlock::iterator &candidate) {
Wesley Peck4e9141f2010-10-21 03:57:26 +000068 // Any instruction with an immediate mode operand greater than
69 // 16-bits requires an implicit IMM instruction.
70 unsigned numOper = candidate->getNumOperands();
Wesley Peck0a67d922010-11-08 19:40:01 +000071 for (unsigned op = 0; op < numOper; ++op) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +000072 MachineOperand &mop = candidate->getOperand(op);
73
74 // The operand requires more than 16-bits to represent.
75 if (mop.isImm() && (mop.getImm() < -0x8000 || mop.getImm() > 0x7fff))
76 return true;
77
78 // We must assume that unknown immediate values require more than
79 // 16-bits to represent.
80 if (mop.isGlobal() || mop.isSymbol())
81 return true;
Wesley Peck4e9141f2010-10-21 03:57:26 +000082
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
Wesley Peck1e8cdd52010-12-06 21:11:01 +000085 // assume that FP values do.
86 if (mop.isFPImm())
87 return true;
Wesley Peck4e9141f2010-10-21 03:57:26 +000088 }
89
90 return false;
91}
92
Wesley Peck025c4582010-12-22 00:22:59 +000093static unsigned getLastRealOperand(MachineBasicBlock::iterator &instr) {
94 switch (instr->getOpcode()) {
95 default: return instr->getNumOperands();
96
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.
99 case MBlaze::BRLID:
100 case MBlaze::BRALID:
101 case MBlaze::BRLD:
102 case MBlaze::BRALD:
103 return 2;
104 }
105}
106
Wesley Peck0a67d922010-11-08 19:40:01 +0000107static bool delayHasHazard(MachineBasicBlock::iterator &candidate,
108 MachineBasicBlock::iterator &slot) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000109 // Hazard check
110 MachineBasicBlock::iterator a = candidate;
111 MachineBasicBlock::iterator b = slot;
112 TargetInstrDesc desc = candidate->getDesc();
Wesley Peck4e9141f2010-10-21 03:57:26 +0000113
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000114 // MBB layout:-
115 // candidate := a0 = operation(a1, a2)
116 // ...middle bit...
117 // slot := b0 = operation(b1, b2)
118
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();
127
Wesley Peck025c4582010-12-22 00:22:59 +0000128 // 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);
132
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000133 // Check hazards type 1, 2 and 5 by scanning the middle bit
134 MachineBasicBlock::iterator m = a;
135 for (++m; m != b; ++m) {
Wesley Peck025c4582010-12-22 00:22:59 +0000136 for (unsigned aop = 0; aop<aend; ++aop) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000137 bool aop_is_reg = a->getOperand(aop).isReg();
138 if (!aop_is_reg) continue;
139
140 bool aop_is_def = a->getOperand(aop).isDef();
141 unsigned aop_reg = a->getOperand(aop).getReg();
142
Wesley Peck025c4582010-12-22 00:22:59 +0000143 const unsigned mend = getLastRealOperand(m);
144 for (unsigned mop = 0; mop<mend; ++mop) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000145 bool mop_is_reg = m->getOperand(mop).isReg();
146 if (!mop_is_reg) continue;
147
148 bool mop_is_def = m->getOperand(mop).isDef();
149 unsigned mop_reg = m->getOperand(mop).getReg();
150
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}
155 }
Wesley Peck4e9141f2010-10-21 03:57:26 +0000156 }
157
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000158 // Check hazard type 5
159 if (a_is_memory && m->getDesc().mayStore())
160 return true;
161 }
Wesley Peck4e9141f2010-10-21 03:57:26 +0000162
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000163 // Check hazard type 3 & 4
Wesley Peck025c4582010-12-22 00:22:59 +0000164 for (unsigned aop = 0; aop<aend; ++aop) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000165 if (a->getOperand(aop).isReg()) {
166 unsigned aop_reg = a->getOperand(aop).getReg();
167
Wesley Peck025c4582010-12-22 00:22:59 +0000168 for (unsigned bop = 0; bop<bend; ++bop) {
169 if (b->getOperand(bop).isReg() && !b->getOperand(bop).isImplicit()) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000170 unsigned bop_reg = b->getOperand(bop).getReg();
171 if (aop_reg == bop_reg)
172 return true;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000173 }
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000174 }
175 }
Wesley Peck4e9141f2010-10-21 03:57:26 +0000176 }
177
178 return false;
179}
180
Wesley Peck5437ba42010-11-21 21:36:12 +0000181static bool isDelayFiller(MachineBasicBlock &MBB,
182 MachineBasicBlock::iterator candidate) {
183 if (candidate == MBB.begin())
184 return false;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000185
Wesley Peck5437ba42010-11-21 21:36:12 +0000186 TargetInstrDesc brdesc = (--candidate)->getDesc();
187 return (brdesc.hasDelaySlot());
188}
189
Wesley Peck8f40b242010-12-12 22:22:49 +0000190static bool hasUnknownSideEffects(MachineBasicBlock::iterator &I,
191 TargetInstrDesc &desc) {
192 if (!desc.hasUnmodeledSideEffects())
193 return false;
194
195 unsigned op = I->getOpcode();
196 if (op == MBlaze::ADDK || op == MBlaze::ADDIK ||
197 op == MBlaze::ADDC || op == MBlaze::ADDIC ||
198 op == MBlaze::ADDKC || op == MBlaze::ADDIKC ||
199 op == MBlaze::RSUBK || op == MBlaze::RSUBIK ||
200 op == MBlaze::RSUBC || op == MBlaze::RSUBIC ||
201 op == MBlaze::RSUBKC || op == MBlaze::RSUBIKC)
202 return false;
203
204 return true;
205}
206
Wesley Peck5437ba42010-11-21 21:36:12 +0000207static MachineBasicBlock::iterator
208findDelayInstr(MachineBasicBlock &MBB,MachineBasicBlock::iterator slot) {
209 MachineBasicBlock::iterator I = slot;
210 while (true) {
211 if (I == MBB.begin())
212 break;
213
214 --I;
215 TargetInstrDesc desc = I->getDesc();
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000216 if (desc.hasDelaySlot() || desc.isBranch() || isDelayFiller(MBB,I) ||
217 desc.isCall() || desc.isReturn() || desc.isBarrier() ||
Wesley Peck8f40b242010-12-12 22:22:49 +0000218 hasUnknownSideEffects(I,desc))
Wesley Peck5437ba42010-11-21 21:36:12 +0000219 break;
220
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000221 if (hasImmInstruction(I) || delayHasHazard(I,slot))
Wesley Peck5437ba42010-11-21 21:36:12 +0000222 continue;
223
224 return I;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000225 }
226
Wesley Peck5437ba42010-11-21 21:36:12 +0000227 return MBB.end();
Wesley Peck4e9141f2010-10-21 03:57:26 +0000228}
229
Wesley Pecka70f28c2010-02-23 19:15:24 +0000230/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
231/// Currently, we fill delay slots with NOPs. We assume there is only one
232/// delay slot per delayed instruction.
233bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
234 bool Changed = false;
235 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
236 if (I->getDesc().hasDelaySlot()) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000237 MachineBasicBlock::iterator D = MBB.end();
Wesley Peck5437ba42010-11-21 21:36:12 +0000238 MachineBasicBlock::iterator J = I;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000239
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000240 if (!DisableDelaySlotFiller)
241 D = findDelayInstr(MBB,I);
242
Wesley Pecka70f28c2010-02-23 19:15:24 +0000243 ++FilledSlots;
244 Changed = true;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000245
Wesley Peck0a67d922010-11-08 19:40:01 +0000246 if (D == MBB.end())
Wesley Peck5437ba42010-11-21 21:36:12 +0000247 BuildMI(MBB, ++J, I->getDebugLoc(), TII->get(MBlaze::NOP));
Wesley Peck4e9141f2010-10-21 03:57:26 +0000248 else
Wesley Peck5437ba42010-11-21 21:36:12 +0000249 MBB.splice(++J, &MBB, D);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000250 }
251 return Changed;
252}
253
254/// createMBlazeDelaySlotFillerPass - Returns a pass that fills in delay
255/// slots in MBlaze MachineFunctions
256FunctionPass *llvm::createMBlazeDelaySlotFillerPass(MBlazeTargetMachine &tm) {
257 return new Filler(tm);
258}
259