blob: 928a43d69e54e0e28896338525dd43b3296fbe3c [file] [log] [blame]
Akira Hatanaka1083eb12013-02-14 23:20:15 +00001//===-- MipsDelaySlotFiller.cpp - Mips Delay Slot Filler ------------------===//
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +00009//
Akira Hatanaka1083eb12013-02-14 23:20:15 +000010// Simple pass to fill delay slots with useful instructions.
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000011//
Akira Hatanakae2489122011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000013
14#define DEBUG_TYPE "delay-slot-filler"
15
16#include "Mips.h"
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000017#include "MipsInstrInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000018#include "MipsTargetMachine.h"
Akira Hatanaka06bd1382013-02-14 23:40:57 +000019#include "llvm/ADT/BitVector.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000020#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/Statistic.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000022#include "llvm/Analysis/AliasAnalysis.h"
23#include "llvm/Analysis/ValueTracking.h"
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000024#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000028#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000029#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Target/TargetMachine.h"
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000031#include "llvm/Target/TargetRegisterInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000032
33using namespace llvm;
34
35STATISTIC(FilledSlots, "Number of delay slots filled");
Akira Hatanaka9e603442011-10-05 01:19:13 +000036STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
Akira Hatanaka02e760a2011-10-05 02:22:49 +000037 " are not NOP.");
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000038
Akira Hatanaka9d957842012-08-22 02:51:28 +000039static cl::opt<bool> DisableDelaySlotFiller(
40 "disable-mips-delay-filler",
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000041 cl::init(false),
Akira Hatanaka1083eb12013-02-14 23:20:15 +000042 cl::desc("Fill all delay slots with NOPs."),
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000043 cl::Hidden);
44
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000045static cl::opt<bool> DisableForwardSearch(
46 "disable-mips-df-forward-search",
47 cl::init(true),
48 cl::desc("Disallow MIPS delay filler to search forward."),
49 cl::Hidden);
50
Akira Hatanakae44e30c2013-03-01 01:02:36 +000051static cl::opt<bool> DisableSuccBBSearch(
52 "disable-mips-df-succbb-search",
53 cl::init(true),
54 cl::desc("Disallow MIPS delay filler to search successor basic blocks."),
55 cl::Hidden);
56
57static cl::opt<bool> DisableBackwardSearch(
58 "disable-mips-df-backward-search",
59 cl::init(false),
60 cl::desc("Disallow MIPS delay filler to search backward."),
61 cl::Hidden);
62
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000063namespace {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000064 typedef MachineBasicBlock::iterator Iter;
65 typedef MachineBasicBlock::reverse_iterator ReverseIter;
66 typedef SmallDenseMap<MachineBasicBlock*, MachineInstr*, 2> BB2BrMap;
67
68 /// \brief A functor comparing edge weight of two blocks.
69 struct CmpWeight {
70 CmpWeight(const MachineBasicBlock &S,
71 const MachineBranchProbabilityInfo &P) : Src(S), Prob(P) {}
72
73 bool operator()(const MachineBasicBlock *Dst0,
74 const MachineBasicBlock *Dst1) const {
75 return Prob.getEdgeWeight(&Src, Dst0) < Prob.getEdgeWeight(&Src, Dst1);
76 }
77
78 const MachineBasicBlock &Src;
79 const MachineBranchProbabilityInfo &Prob;
80 };
81
Akira Hatanaka979899e2013-02-26 01:30:05 +000082 class RegDefsUses {
83 public:
84 RegDefsUses(TargetMachine &TM);
85 void init(const MachineInstr &MI);
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000086
87 /// This function sets all caller-saved registers in Defs.
88 void setCallerSaved(const MachineInstr &MI);
89
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000090 /// This function sets all unallocatable registers in Defs.
91 void setUnallocatableRegs(const MachineFunction &MF);
92
93 /// Set bits in Uses corresponding to MBB's live-out registers except for
94 /// the registers that are live-in to SuccBB.
95 void addLiveOut(const MachineBasicBlock &MBB,
96 const MachineBasicBlock &SuccBB);
97
Akira Hatanaka979899e2013-02-26 01:30:05 +000098 bool update(const MachineInstr &MI, unsigned Begin, unsigned End);
99
100 private:
101 bool checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, unsigned Reg,
102 bool IsDef) const;
103
104 /// Returns true if Reg or its alias is in RegSet.
105 bool isRegInSet(const BitVector &RegSet, unsigned Reg) const;
106
107 const TargetRegisterInfo &TRI;
108 BitVector Defs, Uses;
109 };
110
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000111 /// Base class for inspecting loads and stores.
112 class InspectMemInstr {
113 public:
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000114 InspectMemInstr(bool ForbidMemInstr_)
115 : OrigSeenLoad(false), OrigSeenStore(false), SeenLoad(false),
116 SeenStore(false), ForbidMemInstr(ForbidMemInstr_) {}
117
118 /// Return true if MI cannot be moved to delay slot.
119 bool hasHazard(const MachineInstr &MI);
120
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000121 virtual ~InspectMemInstr() {}
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000122
123 protected:
124 /// Flags indicating whether loads or stores have been seen.
125 bool OrigSeenLoad, OrigSeenStore, SeenLoad, SeenStore;
126
127 /// Memory instructions are not allowed to move to delay slot if this flag
128 /// is true.
129 bool ForbidMemInstr;
130
131 private:
132 virtual bool hasHazard_(const MachineInstr &MI) = 0;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000133 };
134
135 /// This subclass rejects any memory instructions.
136 class NoMemInstr : public InspectMemInstr {
137 public:
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000138 NoMemInstr() : InspectMemInstr(true) {}
139 private:
140 virtual bool hasHazard_(const MachineInstr &MI) { return true; }
141 };
142
143 /// This subclass accepts loads from stacks and constant loads.
144 class LoadFromStackOrConst : public InspectMemInstr {
145 public:
146 LoadFromStackOrConst() : InspectMemInstr(false) {}
147 private:
148 virtual bool hasHazard_(const MachineInstr &MI);
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000149 };
150
151 /// This subclass uses memory dependence information to determine whether a
152 /// memory instruction can be moved to a delay slot.
153 class MemDefsUses : public InspectMemInstr {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000154 public:
155 MemDefsUses(const MachineFrameInfo *MFI);
156
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000157 private:
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000158 virtual bool hasHazard_(const MachineInstr &MI);
159
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000160 /// Update Defs and Uses. Return true if there exist dependences that
Akira Hatanakae9e588d2013-03-01 02:17:02 +0000161 /// disqualify the delay slot candidate between V and values in Uses and
162 /// Defs.
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000163 bool updateDefsUses(const Value *V, bool MayStore);
164
165 /// Get the list of underlying objects of MI's memory operand.
166 bool getUnderlyingObjects(const MachineInstr &MI,
167 SmallVectorImpl<const Value *> &Objects) const;
168
169 const MachineFrameInfo *MFI;
170 SmallPtrSet<const Value*, 4> Uses, Defs;
171
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000172 /// Flags indicating whether loads or stores with no underlying objects have
173 /// been seen.
174 bool SeenNoObjLoad, SeenNoObjStore;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000175 };
176
Akira Hatanakaa0612812013-02-07 21:32:32 +0000177 class Filler : public MachineFunctionPass {
178 public:
Bruno Cardoso Lopesfde21cf2010-12-09 17:31:11 +0000179 Filler(TargetMachine &tm)
Bill Wendlingead89ef2013-06-07 07:04:14 +0000180 : MachineFunctionPass(ID), TM(tm) { }
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000181
182 virtual const char *getPassName() const {
183 return "Mips Delay Slot Filler";
184 }
185
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000186 bool runOnMachineFunction(MachineFunction &F) {
187 bool Changed = false;
188 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
189 FI != FE; ++FI)
190 Changed |= runOnMachineBasicBlock(*FI);
191 return Changed;
192 }
193
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000194 void getAnalysisUsage(AnalysisUsage &AU) const {
195 AU.addRequired<MachineBranchProbabilityInfo>();
196 MachineFunctionPass::getAnalysisUsage(AU);
197 }
Akira Hatanakaa0612812013-02-07 21:32:32 +0000198
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000199 private:
Akira Hatanakaa0612812013-02-07 21:32:32 +0000200 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
201
Akira Hatanaka06bd1382013-02-14 23:40:57 +0000202 /// This function checks if it is valid to move Candidate to the delay slot
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000203 /// and returns true if it isn't. It also updates memory and register
204 /// dependence information.
205 bool delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000206 InspectMemInstr &IM) const;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000207
Akira Hatanakaf815db52013-03-01 00:26:14 +0000208 /// This function searches range [Begin, End) for an instruction that can be
209 /// moved to the delay slot. Returns true on success.
210 template<typename IterTy>
211 bool searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
Akira Hatanakae9e588d2013-03-01 02:17:02 +0000212 RegDefsUses &RegDU, InspectMemInstr &IM,
213 IterTy &Filler) const;
Akira Hatanakaf815db52013-03-01 00:26:14 +0000214
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000215 /// This function searches in the backward direction for an instruction that
216 /// can be moved to the delay slot. Returns true on success.
217 bool searchBackward(MachineBasicBlock &MBB, Iter Slot) const;
218
219 /// This function searches MBB in the forward direction for an instruction
220 /// that can be moved to the delay slot. Returns true on success.
221 bool searchForward(MachineBasicBlock &MBB, Iter Slot) const;
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000222
Akira Hatanaka1ff803f2013-03-25 20:11:16 +0000223 /// This function searches one of MBB's successor blocks for an instruction
224 /// that can be moved to the delay slot and inserts clones of the
225 /// instruction into the successor's predecessor blocks.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000226 bool searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const;
227
Akira Hatanakae9e588d2013-03-01 02:17:02 +0000228 /// Pick a successor block of MBB. Return NULL if MBB doesn't have a
229 /// successor block that is not a landing pad.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000230 MachineBasicBlock *selectSuccBB(MachineBasicBlock &B) const;
231
232 /// This function analyzes MBB and returns an instruction with an unoccupied
233 /// slot that branches to Dst.
234 std::pair<MipsInstrInfo::BranchType, MachineInstr *>
235 getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const;
236
237 /// Examine Pred and see if it is possible to insert an instruction into
238 /// one of its branches delay slot or its end.
239 bool examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
240 RegDefsUses &RegDU, bool &HasMultipleSuccs,
241 BB2BrMap &BrMap) const;
242
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000243 bool terminateSearch(const MachineInstr &Candidate) const;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000244
Akira Hatanakaa0612812013-02-07 21:32:32 +0000245 TargetMachine &TM;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000246
Akira Hatanakaa0612812013-02-07 21:32:32 +0000247 static char ID;
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000248 };
249 char Filler::ID = 0;
250} // end of anonymous namespace
251
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000252static bool hasUnoccupiedSlot(const MachineInstr *MI) {
253 return MI->hasDelaySlot() && !MI->isBundledWithSucc();
254}
255
256/// This function inserts clones of Filler into predecessor blocks.
257static void insertDelayFiller(Iter Filler, const BB2BrMap &BrMap) {
258 MachineFunction *MF = Filler->getParent()->getParent();
259
260 for (BB2BrMap::const_iterator I = BrMap.begin(); I != BrMap.end(); ++I) {
261 if (I->second) {
262 MIBundleBuilder(I->second).append(MF->CloneMachineInstr(&*Filler));
263 ++UsefulSlots;
264 } else {
265 I->first->insert(I->first->end(), MF->CloneMachineInstr(&*Filler));
266 }
267 }
268}
269
270/// This function adds registers Filler defines to MBB's live-in register list.
271static void addLiveInRegs(Iter Filler, MachineBasicBlock &MBB) {
272 for (unsigned I = 0, E = Filler->getNumOperands(); I != E; ++I) {
273 const MachineOperand &MO = Filler->getOperand(I);
274 unsigned R;
275
276 if (!MO.isReg() || !MO.isDef() || !(R = MO.getReg()))
277 continue;
278
279#ifndef NDEBUG
280 const MachineFunction &MF = *MBB.getParent();
281 assert(MF.getTarget().getRegisterInfo()->getAllocatableSet(MF).test(R) &&
282 "Shouldn't move an instruction with unallocatable registers across "
283 "basic block boundaries.");
284#endif
285
286 if (!MBB.isLiveIn(R))
287 MBB.addLiveIn(R);
288 }
289}
290
Akira Hatanaka979899e2013-02-26 01:30:05 +0000291RegDefsUses::RegDefsUses(TargetMachine &TM)
292 : TRI(*TM.getRegisterInfo()), Defs(TRI.getNumRegs(), false),
293 Uses(TRI.getNumRegs(), false) {}
294
295void RegDefsUses::init(const MachineInstr &MI) {
296 // Add all register operands which are explicit and non-variadic.
297 update(MI, 0, MI.getDesc().getNumOperands());
298
299 // If MI is a call, add RA to Defs to prevent users of RA from going into
300 // delay slot.
301 if (MI.isCall())
302 Defs.set(Mips::RA);
303
304 // Add all implicit register operands of branch instructions except
305 // register AT.
306 if (MI.isBranch()) {
307 update(MI, MI.getDesc().getNumOperands(), MI.getNumOperands());
308 Defs.reset(Mips::AT);
309 }
310}
311
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000312void RegDefsUses::setCallerSaved(const MachineInstr &MI) {
313 assert(MI.isCall());
314
315 // If MI is a call, add all caller-saved registers to Defs.
316 BitVector CallerSavedRegs(TRI.getNumRegs(), true);
317
318 CallerSavedRegs.reset(Mips::ZERO);
319 CallerSavedRegs.reset(Mips::ZERO_64);
320
321 for (const MCPhysReg *R = TRI.getCalleeSavedRegs(); *R; ++R)
322 for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI)
323 CallerSavedRegs.reset(*AI);
324
325 Defs |= CallerSavedRegs;
326}
327
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000328void RegDefsUses::setUnallocatableRegs(const MachineFunction &MF) {
329 BitVector AllocSet = TRI.getAllocatableSet(MF);
330
331 for (int R = AllocSet.find_first(); R != -1; R = AllocSet.find_next(R))
332 for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI)
333 AllocSet.set(*AI);
334
335 AllocSet.set(Mips::ZERO);
336 AllocSet.set(Mips::ZERO_64);
337
338 Defs |= AllocSet.flip();
339}
340
341void RegDefsUses::addLiveOut(const MachineBasicBlock &MBB,
342 const MachineBasicBlock &SuccBB) {
343 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
344 SE = MBB.succ_end(); SI != SE; ++SI)
345 if (*SI != &SuccBB)
346 for (MachineBasicBlock::livein_iterator LI = (*SI)->livein_begin(),
347 LE = (*SI)->livein_end(); LI != LE; ++LI)
348 Uses.set(*LI);
349}
350
Akira Hatanaka979899e2013-02-26 01:30:05 +0000351bool RegDefsUses::update(const MachineInstr &MI, unsigned Begin, unsigned End) {
352 BitVector NewDefs(TRI.getNumRegs()), NewUses(TRI.getNumRegs());
353 bool HasHazard = false;
354
355 for (unsigned I = Begin; I != End; ++I) {
356 const MachineOperand &MO = MI.getOperand(I);
357
358 if (MO.isReg() && MO.getReg())
359 HasHazard |= checkRegDefsUses(NewDefs, NewUses, MO.getReg(), MO.isDef());
360 }
361
362 Defs |= NewDefs;
363 Uses |= NewUses;
364
365 return HasHazard;
366}
367
368bool RegDefsUses::checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses,
369 unsigned Reg, bool IsDef) const {
370 if (IsDef) {
371 NewDefs.set(Reg);
372 // check whether Reg has already been defined or used.
373 return (isRegInSet(Defs, Reg) || isRegInSet(Uses, Reg));
374 }
375
376 NewUses.set(Reg);
377 // check whether Reg has already been defined.
378 return isRegInSet(Defs, Reg);
379}
380
381bool RegDefsUses::isRegInSet(const BitVector &RegSet, unsigned Reg) const {
382 // Check Reg and all aliased Registers.
383 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
384 if (RegSet.test(*AI))
385 return true;
386 return false;
387}
388
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000389bool InspectMemInstr::hasHazard(const MachineInstr &MI) {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000390 if (!MI.mayStore() && !MI.mayLoad())
391 return false;
392
393 if (ForbidMemInstr)
394 return true;
395
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000396 OrigSeenLoad = SeenLoad;
397 OrigSeenStore = SeenStore;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000398 SeenLoad |= MI.mayLoad();
399 SeenStore |= MI.mayStore();
400
401 // If MI is an ordered or volatile memory reference, disallow moving
402 // subsequent loads and stores to delay slot.
403 if (MI.hasOrderedMemoryRef() && (OrigSeenLoad || OrigSeenStore)) {
404 ForbidMemInstr = true;
405 return true;
406 }
407
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000408 return hasHazard_(MI);
409}
410
411bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) {
412 if (MI.mayStore())
413 return true;
414
415 if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getValue())
416 return true;
417
418 const Value *V = (*MI.memoperands_begin())->getValue();
419
420 if (isa<FixedStackPseudoSourceValue>(V))
421 return false;
422
423 if (const PseudoSourceValue *PSV = dyn_cast<const PseudoSourceValue>(V))
424 return !PSV->PseudoSourceValue::isConstant(0) &&
425 (V != PseudoSourceValue::getStack());
426
427 return true;
428}
429
430MemDefsUses::MemDefsUses(const MachineFrameInfo *MFI_)
431 : InspectMemInstr(false), MFI(MFI_), SeenNoObjLoad(false),
432 SeenNoObjStore(false) {}
433
434bool MemDefsUses::hasHazard_(const MachineInstr &MI) {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000435 bool HasHazard = false;
436 SmallVector<const Value *, 4> Objs;
437
438 // Check underlying object list.
439 if (getUnderlyingObjects(MI, Objs)) {
440 for (SmallVector<const Value *, 4>::const_iterator I = Objs.begin();
441 I != Objs.end(); ++I)
442 HasHazard |= updateDefsUses(*I, MI.mayStore());
443
444 return HasHazard;
445 }
446
447 // No underlying objects found.
448 HasHazard = MI.mayStore() && (OrigSeenLoad || OrigSeenStore);
449 HasHazard |= MI.mayLoad() || OrigSeenStore;
450
451 SeenNoObjLoad |= MI.mayLoad();
452 SeenNoObjStore |= MI.mayStore();
453
454 return HasHazard;
455}
456
457bool MemDefsUses::updateDefsUses(const Value *V, bool MayStore) {
458 if (MayStore)
459 return !Defs.insert(V) || Uses.count(V) || SeenNoObjStore || SeenNoObjLoad;
460
461 Uses.insert(V);
462 return Defs.count(V) || SeenNoObjStore;
463}
464
465bool MemDefsUses::
466getUnderlyingObjects(const MachineInstr &MI,
467 SmallVectorImpl<const Value *> &Objects) const {
468 if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getValue())
469 return false;
470
471 const Value *V = (*MI.memoperands_begin())->getValue();
472
473 SmallVector<Value *, 4> Objs;
474 GetUnderlyingObjects(const_cast<Value *>(V), Objs);
475
476 for (SmallVector<Value*, 4>::iterator I = Objs.begin(), E = Objs.end();
477 I != E; ++I) {
478 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(*I)) {
479 if (PSV->isAliased(MFI))
480 return false;
481 } else if (!isIdentifiedObject(V))
482 return false;
483
484 Objects.push_back(*I);
485 }
486
487 return true;
488}
489
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000490/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000491/// We assume there is only one delay slot per delayed instruction.
Akira Hatanaka1083eb12013-02-14 23:20:15 +0000492bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000493 bool Changed = false;
Akira Hatanakae7b06972011-10-05 01:30:09 +0000494
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000495 for (Iter I = MBB.begin(); I != MBB.end(); ++I) {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000496 if (!hasUnoccupiedSlot(&*I))
Akira Hatanakaa0612812013-02-07 21:32:32 +0000497 continue;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000498
Akira Hatanakaa0612812013-02-07 21:32:32 +0000499 ++FilledSlots;
500 Changed = true;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000501
Akira Hatanakaa0612812013-02-07 21:32:32 +0000502 // Delay slot filling is disabled at -O0.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000503 if (!DisableDelaySlotFiller && (TM.getOptLevel() != CodeGenOpt::None)) {
504 if (searchBackward(MBB, I))
505 continue;
506
507 if (I->isTerminator()) {
508 if (searchSuccBBs(MBB, I))
509 continue;
510 } else if (searchForward(MBB, I)) {
511 continue;
512 }
513 }
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000514
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000515 // Bundle the NOP to the instruction with the delay slot.
Bill Wendlingead89ef2013-06-07 07:04:14 +0000516 const MipsInstrInfo *TII =
517 static_cast<const MipsInstrInfo*>(TM.getInstrInfo());
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000518 BuildMI(MBB, llvm::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000519 MIBundleBuilder(MBB, I, llvm::next(llvm::next(I)));
Akira Hatanakaa0612812013-02-07 21:32:32 +0000520 }
521
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000522 return Changed;
523}
524
525/// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
526/// slots in Mips MachineFunctions
527FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
528 return new Filler(tm);
529}
530
Akira Hatanakaf815db52013-03-01 00:26:14 +0000531template<typename IterTy>
532bool Filler::searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000533 RegDefsUses &RegDU, InspectMemInstr& IM,
Akira Hatanakaf815db52013-03-01 00:26:14 +0000534 IterTy &Filler) const {
535 for (IterTy I = Begin; I != End; ++I) {
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000536 // skip debug value
537 if (I->isDebugValue())
538 continue;
539
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000540 if (terminateSearch(*I))
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000541 break;
542
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000543 assert((!I->isCall() && !I->isReturn() && !I->isBranch()) &&
544 "Cannot put calls, returns or branches in delay slot.");
545
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000546 if (delayHasHazard(*I, RegDU, IM))
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000547 continue;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000548
Akira Hatanakaf815db52013-03-01 00:26:14 +0000549 Filler = I;
550 return true;
551 }
552
553 return false;
554}
555
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000556bool Filler::searchBackward(MachineBasicBlock &MBB, Iter Slot) const {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000557 if (DisableBackwardSearch)
558 return false;
559
Akira Hatanakaf815db52013-03-01 00:26:14 +0000560 RegDefsUses RegDU(TM);
561 MemDefsUses MemDU(MBB.getParent()->getFrameInfo());
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000562 ReverseIter Filler;
Akira Hatanakaf815db52013-03-01 00:26:14 +0000563
564 RegDU.init(*Slot);
565
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000566 if (searchRange(MBB, ReverseIter(Slot), MBB.rend(), RegDU, MemDU, Filler)) {
567 MBB.splice(llvm::next(Slot), &MBB, llvm::next(Filler).base());
568 MIBundleBuilder(MBB, Slot, llvm::next(llvm::next(Slot)));
569 ++UsefulSlots;
570 return true;
571 }
572
573 return false;
574}
575
576bool Filler::searchForward(MachineBasicBlock &MBB, Iter Slot) const {
577 // Can handle only calls.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000578 if (DisableForwardSearch || !Slot->isCall())
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000579 return false;
580
581 RegDefsUses RegDU(TM);
582 NoMemInstr NM;
583 Iter Filler;
584
585 RegDU.setCallerSaved(*Slot);
586
587 if (searchRange(MBB, llvm::next(Slot), MBB.end(), RegDU, NM, Filler)) {
588 MBB.splice(llvm::next(Slot), &MBB, Filler);
589 MIBundleBuilder(MBB, Slot, llvm::next(llvm::next(Slot)));
590 ++UsefulSlots;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000591 return true;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000592 }
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000593
594 return false;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000595}
596
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000597bool Filler::searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const {
598 if (DisableSuccBBSearch)
599 return false;
600
601 MachineBasicBlock *SuccBB = selectSuccBB(MBB);
602
603 if (!SuccBB)
604 return false;
605
606 RegDefsUses RegDU(TM);
607 bool HasMultipleSuccs = false;
608 BB2BrMap BrMap;
609 OwningPtr<InspectMemInstr> IM;
610 Iter Filler;
611
612 // Iterate over SuccBB's predecessor list.
613 for (MachineBasicBlock::pred_iterator PI = SuccBB->pred_begin(),
614 PE = SuccBB->pred_end(); PI != PE; ++PI)
615 if (!examinePred(**PI, *SuccBB, RegDU, HasMultipleSuccs, BrMap))
616 return false;
617
618 // Do not allow moving instructions which have unallocatable register operands
619 // across basic block boundaries.
620 RegDU.setUnallocatableRegs(*MBB.getParent());
621
622 // Only allow moving loads from stack or constants if any of the SuccBB's
623 // predecessors have multiple successors.
624 if (HasMultipleSuccs) {
625 IM.reset(new LoadFromStackOrConst());
626 } else {
627 const MachineFrameInfo *MFI = MBB.getParent()->getFrameInfo();
628 IM.reset(new MemDefsUses(MFI));
629 }
630
631 if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Filler))
632 return false;
633
634 insertDelayFiller(Filler, BrMap);
635 addLiveInRegs(Filler, *SuccBB);
636 Filler->eraseFromParent();
637
638 return true;
639}
640
641MachineBasicBlock *Filler::selectSuccBB(MachineBasicBlock &B) const {
642 if (B.succ_empty())
643 return NULL;
644
645 // Select the successor with the larget edge weight.
646 CmpWeight Cmp(B, getAnalysis<MachineBranchProbabilityInfo>());
647 MachineBasicBlock *S = *std::max_element(B.succ_begin(), B.succ_end(), Cmp);
648 return S->isLandingPad() ? NULL : S;
649}
650
651std::pair<MipsInstrInfo::BranchType, MachineInstr *>
652Filler::getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const {
653 const MipsInstrInfo *TII =
654 static_cast<const MipsInstrInfo*>(TM.getInstrInfo());
655 MachineBasicBlock *TrueBB = 0, *FalseBB = 0;
656 SmallVector<MachineInstr*, 2> BranchInstrs;
657 SmallVector<MachineOperand, 2> Cond;
658
659 MipsInstrInfo::BranchType R =
660 TII->AnalyzeBranch(MBB, TrueBB, FalseBB, Cond, false, BranchInstrs);
661
662 if ((R == MipsInstrInfo::BT_None) || (R == MipsInstrInfo::BT_NoBranch))
663 return std::make_pair(R, (MachineInstr*)NULL);
664
665 if (R != MipsInstrInfo::BT_CondUncond) {
666 if (!hasUnoccupiedSlot(BranchInstrs[0]))
667 return std::make_pair(MipsInstrInfo::BT_None, (MachineInstr*)NULL);
668
669 assert(((R != MipsInstrInfo::BT_Uncond) || (TrueBB == &Dst)));
670
671 return std::make_pair(R, BranchInstrs[0]);
672 }
673
674 assert((TrueBB == &Dst) || (FalseBB == &Dst));
675
676 // Examine the conditional branch. See if its slot is occupied.
677 if (hasUnoccupiedSlot(BranchInstrs[0]))
678 return std::make_pair(MipsInstrInfo::BT_Cond, BranchInstrs[0]);
679
680 // If that fails, try the unconditional branch.
681 if (hasUnoccupiedSlot(BranchInstrs[1]) && (FalseBB == &Dst))
682 return std::make_pair(MipsInstrInfo::BT_Uncond, BranchInstrs[1]);
683
684 return std::make_pair(MipsInstrInfo::BT_None, (MachineInstr*)NULL);
685}
686
687bool Filler::examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
688 RegDefsUses &RegDU, bool &HasMultipleSuccs,
689 BB2BrMap &BrMap) const {
690 std::pair<MipsInstrInfo::BranchType, MachineInstr *> P =
691 getBranch(Pred, Succ);
692
693 // Return if either getBranch wasn't able to analyze the branches or there
694 // were no branches with unoccupied slots.
695 if (P.first == MipsInstrInfo::BT_None)
696 return false;
697
698 if ((P.first != MipsInstrInfo::BT_Uncond) &&
699 (P.first != MipsInstrInfo::BT_NoBranch)) {
700 HasMultipleSuccs = true;
701 RegDU.addLiveOut(Pred, Succ);
702 }
703
704 BrMap[&Pred] = P.second;
705 return true;
706}
707
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000708bool Filler::delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000709 InspectMemInstr &IM) const {
Akira Hatanaka06bd1382013-02-14 23:40:57 +0000710 bool HasHazard = (Candidate.isImplicitDef() || Candidate.isKill());
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000711
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000712 HasHazard |= IM.hasHazard(Candidate);
Akira Hatanaka979899e2013-02-26 01:30:05 +0000713 HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands());
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000714
Akira Hatanaka06bd1382013-02-14 23:40:57 +0000715 return HasHazard;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000716}
717
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000718bool Filler::terminateSearch(const MachineInstr &Candidate) const {
719 return (Candidate.isTerminator() || Candidate.isCall() ||
720 Candidate.isLabel() || Candidate.isInlineAsm() ||
721 Candidate.hasUnmodeledSideEffects());
722}