blob: 19e787d8622d136c4ef3a3d7c6f4491c2cf0b47e [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
Benjamin Kramer0861f572011-11-26 23:01:57 +000032static cl::opt<bool> MBDisableDelaySlotFiller(
Wesley Peck1e8cdd52010-12-06 21:11:01 +000033 "disable-mblaze-delay-filler",
34 cl::init(false),
35 cl::desc("Disable the MBlaze delay slot filter."),
36 cl::Hidden);
Wesley Peck1e8cdd52010-12-06 21:11:01 +000037
Wesley Pecka70f28c2010-02-23 19:15:24 +000038namespace {
39 struct Filler : public MachineFunctionPass {
40
41 TargetMachine &TM;
42 const TargetInstrInfo *TII;
43
44 static char ID;
Wesley Peck0a67d922010-11-08 19:40:01 +000045 Filler(TargetMachine &tm)
Owen Anderson90c579d2010-08-06 18:33:48 +000046 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
Wesley Pecka70f28c2010-02-23 19:15:24 +000047
48 virtual const char *getPassName() const {
49 return "MBlaze Delay Slot Filler";
50 }
51
52 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
53 bool runOnMachineFunction(MachineFunction &F) {
54 bool Changed = false;
55 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
56 FI != FE; ++FI)
57 Changed |= runOnMachineBasicBlock(*FI);
58 return Changed;
59 }
60
61 };
62 char Filler::ID = 0;
63} // end of anonymous namespace
64
Wesley Peck0a67d922010-11-08 19:40:01 +000065static bool hasImmInstruction(MachineBasicBlock::iterator &candidate) {
Wesley Peck4e9141f2010-10-21 03:57:26 +000066 // Any instruction with an immediate mode operand greater than
67 // 16-bits requires an implicit IMM instruction.
68 unsigned numOper = candidate->getNumOperands();
Wesley Peck0a67d922010-11-08 19:40:01 +000069 for (unsigned op = 0; op < numOper; ++op) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +000070 MachineOperand &mop = candidate->getOperand(op);
71
72 // The operand requires more than 16-bits to represent.
73 if (mop.isImm() && (mop.getImm() < -0x8000 || mop.getImm() > 0x7fff))
74 return true;
75
76 // We must assume that unknown immediate values require more than
77 // 16-bits to represent.
Wesley Peckef9d9fd2011-04-11 22:45:02 +000078 if (mop.isGlobal() || mop.isSymbol() || mop.isJTI() || mop.isCPI())
Wesley Peck1e8cdd52010-12-06 21:11:01 +000079 return true;
Wesley Peck4e9141f2010-10-21 03:57:26 +000080
81 // FIXME: we could probably check to see if the FP value happens
82 // to not need an IMM instruction. For now we just always
Wesley Peck1e8cdd52010-12-06 21:11:01 +000083 // assume that FP values do.
84 if (mop.isFPImm())
85 return true;
Wesley Peck4e9141f2010-10-21 03:57:26 +000086 }
87
88 return false;
89}
90
Wesley Peck025c4582010-12-22 00:22:59 +000091static unsigned getLastRealOperand(MachineBasicBlock::iterator &instr) {
92 switch (instr->getOpcode()) {
93 default: return instr->getNumOperands();
94
95 // These instructions have a variable number of operands but the first two
96 // are the "real" operands that we care about during hazard detection.
97 case MBlaze::BRLID:
98 case MBlaze::BRALID:
99 case MBlaze::BRLD:
100 case MBlaze::BRALD:
101 return 2;
102 }
103}
104
Wesley Peck0a67d922010-11-08 19:40:01 +0000105static bool delayHasHazard(MachineBasicBlock::iterator &candidate,
106 MachineBasicBlock::iterator &slot) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000107 // Hazard check
108 MachineBasicBlock::iterator a = candidate;
109 MachineBasicBlock::iterator b = slot;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000110
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000111 // MBB layout:-
112 // candidate := a0 = operation(a1, a2)
113 // ...middle bit...
114 // slot := b0 = operation(b1, b2)
115
116 // Possible hazards:-/
117 // 1. a1 or a2 was written during the middle bit
118 // 2. a0 was read or written during the middle bit
119 // 3. a0 is one or more of {b0, b1, b2}
120 // 4. b0 is one or more of {a1, a2}
121 // 5. a accesses memory, and the middle bit
122 // contains a store operation.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000123 bool a_is_memory = candidate->mayLoad() || candidate->mayStore();
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000124
Wesley Peck025c4582010-12-22 00:22:59 +0000125 // Determine the number of operands in the slot instruction and in the
126 // candidate instruction.
127 const unsigned aend = getLastRealOperand(a);
128 const unsigned bend = getLastRealOperand(b);
129
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000130 // Check hazards type 1, 2 and 5 by scanning the middle bit
131 MachineBasicBlock::iterator m = a;
132 for (++m; m != b; ++m) {
Wesley Peck025c4582010-12-22 00:22:59 +0000133 for (unsigned aop = 0; aop<aend; ++aop) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000134 bool aop_is_reg = a->getOperand(aop).isReg();
135 if (!aop_is_reg) continue;
136
137 bool aop_is_def = a->getOperand(aop).isDef();
138 unsigned aop_reg = a->getOperand(aop).getReg();
139
Wesley Peck025c4582010-12-22 00:22:59 +0000140 const unsigned mend = getLastRealOperand(m);
141 for (unsigned mop = 0; mop<mend; ++mop) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000142 bool mop_is_reg = m->getOperand(mop).isReg();
143 if (!mop_is_reg) continue;
144
145 bool mop_is_def = m->getOperand(mop).isDef();
146 unsigned mop_reg = m->getOperand(mop).getReg();
147
148 if (aop_is_def && (mop_reg == aop_reg))
149 return true; // Hazard type 2, because aop = a0
150 else if (mop_is_def && (mop_reg == aop_reg))
151 return true; // Hazard type 1, because aop in {a1, a2}
152 }
Wesley Peck4e9141f2010-10-21 03:57:26 +0000153 }
154
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000155 // Check hazard type 5
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000156 if (a_is_memory && m->mayStore())
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000157 return true;
158 }
Wesley Peck4e9141f2010-10-21 03:57:26 +0000159
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000160 // Check hazard type 3 & 4
Wesley Peck025c4582010-12-22 00:22:59 +0000161 for (unsigned aop = 0; aop<aend; ++aop) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000162 if (a->getOperand(aop).isReg()) {
163 unsigned aop_reg = a->getOperand(aop).getReg();
164
Wesley Peck025c4582010-12-22 00:22:59 +0000165 for (unsigned bop = 0; bop<bend; ++bop) {
166 if (b->getOperand(bop).isReg() && !b->getOperand(bop).isImplicit()) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000167 unsigned bop_reg = b->getOperand(bop).getReg();
168 if (aop_reg == bop_reg)
169 return true;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000170 }
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000171 }
172 }
Wesley Peck4e9141f2010-10-21 03:57:26 +0000173 }
174
175 return false;
176}
177
Wesley Peck5437ba42010-11-21 21:36:12 +0000178static bool isDelayFiller(MachineBasicBlock &MBB,
179 MachineBasicBlock::iterator candidate) {
180 if (candidate == MBB.begin())
181 return false;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000182
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000183 --candidate;
184 return (candidate->hasDelaySlot());
Wesley Peck5437ba42010-11-21 21:36:12 +0000185}
186
Evan Chengc36b7062011-01-07 23:50:32 +0000187static bool hasUnknownSideEffects(MachineBasicBlock::iterator &I) {
188 if (!I->hasUnmodeledSideEffects())
Wesley Peck8f40b242010-12-12 22:22:49 +0000189 return false;
190
191 unsigned op = I->getOpcode();
192 if (op == MBlaze::ADDK || op == MBlaze::ADDIK ||
193 op == MBlaze::ADDC || op == MBlaze::ADDIC ||
194 op == MBlaze::ADDKC || op == MBlaze::ADDIKC ||
195 op == MBlaze::RSUBK || op == MBlaze::RSUBIK ||
196 op == MBlaze::RSUBC || op == MBlaze::RSUBIC ||
197 op == MBlaze::RSUBKC || op == MBlaze::RSUBIKC)
198 return false;
199
200 return true;
201}
202
Wesley Peck5437ba42010-11-21 21:36:12 +0000203static MachineBasicBlock::iterator
204findDelayInstr(MachineBasicBlock &MBB,MachineBasicBlock::iterator slot) {
205 MachineBasicBlock::iterator I = slot;
206 while (true) {
207 if (I == MBB.begin())
208 break;
209
210 --I;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000211 if (I->hasDelaySlot() || I->isBranch() || isDelayFiller(MBB,I) ||
212 I->isCall() || I->isReturn() || I->isBarrier() ||
Evan Chengc36b7062011-01-07 23:50:32 +0000213 hasUnknownSideEffects(I))
Wesley Peck5437ba42010-11-21 21:36:12 +0000214 break;
215
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000216 if (hasImmInstruction(I) || delayHasHazard(I,slot))
Wesley Peck5437ba42010-11-21 21:36:12 +0000217 continue;
218
219 return I;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000220 }
221
Wesley Peck5437ba42010-11-21 21:36:12 +0000222 return MBB.end();
Wesley Peck4e9141f2010-10-21 03:57:26 +0000223}
224
Wesley Pecka70f28c2010-02-23 19:15:24 +0000225/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
226/// Currently, we fill delay slots with NOPs. We assume there is only one
227/// delay slot per delayed instruction.
228bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
229 bool Changed = false;
230 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000231 if (I->hasDelaySlot()) {
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000232 MachineBasicBlock::iterator D = MBB.end();
Wesley Peck5437ba42010-11-21 21:36:12 +0000233 MachineBasicBlock::iterator J = I;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000234
Wesley Pecka18f0832011-11-26 21:50:38 +0000235 if (!MBDisableDelaySlotFiller)
Wesley Peck1e8cdd52010-12-06 21:11:01 +0000236 D = findDelayInstr(MBB,I);
237
Wesley Pecka70f28c2010-02-23 19:15:24 +0000238 ++FilledSlots;
239 Changed = true;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000240
Wesley Peck0a67d922010-11-08 19:40:01 +0000241 if (D == MBB.end())
Wesley Peck5437ba42010-11-21 21:36:12 +0000242 BuildMI(MBB, ++J, I->getDebugLoc(), TII->get(MBlaze::NOP));
Wesley Peck4e9141f2010-10-21 03:57:26 +0000243 else
Wesley Peck5437ba42010-11-21 21:36:12 +0000244 MBB.splice(++J, &MBB, D);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000245 }
246 return Changed;
247}
248
249/// createMBlazeDelaySlotFillerPass - Returns a pass that fills in delay
250/// slots in MBlaze MachineFunctions
251FunctionPass *llvm::createMBlazeDelaySlotFillerPass(MBlazeTargetMachine &tm) {
252 return new Filler(tm);
253}
254