blob: eef9f38daf8a5c983c1917fc970476e80182cd23 [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
Sasa Stankovic5fddf612014-03-10 20:34:23 +000016#include "MCTargetDesc/MipsMCNaCl.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000017#include "Mips.h"
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000018#include "MipsInstrInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000019#include "MipsTargetMachine.h"
Akira Hatanaka06bd1382013-02-14 23:40:57 +000020#include "llvm/ADT/BitVector.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000021#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/Statistic.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000023#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/Analysis/ValueTracking.h"
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000025#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000028#include "llvm/CodeGen/PseudoSourceValue.h"
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000029#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000030#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Target/TargetMachine.h"
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000032#include "llvm/Target/TargetRegisterInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000033
34using namespace llvm;
35
36STATISTIC(FilledSlots, "Number of delay slots filled");
Akira Hatanaka9e603442011-10-05 01:19:13 +000037STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
Akira Hatanaka02e760a2011-10-05 02:22:49 +000038 " are not NOP.");
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000039
Akira Hatanaka9d957842012-08-22 02:51:28 +000040static cl::opt<bool> DisableDelaySlotFiller(
41 "disable-mips-delay-filler",
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000042 cl::init(false),
Akira Hatanaka1083eb12013-02-14 23:20:15 +000043 cl::desc("Fill all delay slots with NOPs."),
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000044 cl::Hidden);
45
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000046static cl::opt<bool> DisableForwardSearch(
47 "disable-mips-df-forward-search",
48 cl::init(true),
49 cl::desc("Disallow MIPS delay filler to search forward."),
50 cl::Hidden);
51
Akira Hatanakae44e30c2013-03-01 01:02:36 +000052static cl::opt<bool> DisableSuccBBSearch(
53 "disable-mips-df-succbb-search",
54 cl::init(true),
55 cl::desc("Disallow MIPS delay filler to search successor basic blocks."),
56 cl::Hidden);
57
58static cl::opt<bool> DisableBackwardSearch(
59 "disable-mips-df-backward-search",
60 cl::init(false),
61 cl::desc("Disallow MIPS delay filler to search backward."),
62 cl::Hidden);
63
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000064namespace {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000065 typedef MachineBasicBlock::iterator Iter;
66 typedef MachineBasicBlock::reverse_iterator ReverseIter;
67 typedef SmallDenseMap<MachineBasicBlock*, MachineInstr*, 2> BB2BrMap;
68
Akira Hatanaka979899e2013-02-26 01:30:05 +000069 class RegDefsUses {
70 public:
71 RegDefsUses(TargetMachine &TM);
72 void init(const MachineInstr &MI);
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000073
74 /// This function sets all caller-saved registers in Defs.
75 void setCallerSaved(const MachineInstr &MI);
76
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000077 /// This function sets all unallocatable registers in Defs.
78 void setUnallocatableRegs(const MachineFunction &MF);
79
80 /// Set bits in Uses corresponding to MBB's live-out registers except for
81 /// the registers that are live-in to SuccBB.
82 void addLiveOut(const MachineBasicBlock &MBB,
83 const MachineBasicBlock &SuccBB);
84
Akira Hatanaka979899e2013-02-26 01:30:05 +000085 bool update(const MachineInstr &MI, unsigned Begin, unsigned End);
86
87 private:
88 bool checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, unsigned Reg,
89 bool IsDef) const;
90
91 /// Returns true if Reg or its alias is in RegSet.
92 bool isRegInSet(const BitVector &RegSet, unsigned Reg) const;
93
94 const TargetRegisterInfo &TRI;
95 BitVector Defs, Uses;
96 };
97
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000098 /// Base class for inspecting loads and stores.
99 class InspectMemInstr {
100 public:
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000101 InspectMemInstr(bool ForbidMemInstr_)
102 : OrigSeenLoad(false), OrigSeenStore(false), SeenLoad(false),
103 SeenStore(false), ForbidMemInstr(ForbidMemInstr_) {}
104
105 /// Return true if MI cannot be moved to delay slot.
106 bool hasHazard(const MachineInstr &MI);
107
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000108 virtual ~InspectMemInstr() {}
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000109
110 protected:
111 /// Flags indicating whether loads or stores have been seen.
112 bool OrigSeenLoad, OrigSeenStore, SeenLoad, SeenStore;
113
114 /// Memory instructions are not allowed to move to delay slot if this flag
115 /// is true.
116 bool ForbidMemInstr;
117
118 private:
119 virtual bool hasHazard_(const MachineInstr &MI) = 0;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000120 };
121
122 /// This subclass rejects any memory instructions.
123 class NoMemInstr : public InspectMemInstr {
124 public:
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000125 NoMemInstr() : InspectMemInstr(true) {}
126 private:
127 virtual bool hasHazard_(const MachineInstr &MI) { return true; }
128 };
129
130 /// This subclass accepts loads from stacks and constant loads.
131 class LoadFromStackOrConst : public InspectMemInstr {
132 public:
133 LoadFromStackOrConst() : InspectMemInstr(false) {}
134 private:
135 virtual bool hasHazard_(const MachineInstr &MI);
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000136 };
137
138 /// This subclass uses memory dependence information to determine whether a
139 /// memory instruction can be moved to a delay slot.
140 class MemDefsUses : public InspectMemInstr {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000141 public:
142 MemDefsUses(const MachineFrameInfo *MFI);
143
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000144 private:
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000145 virtual bool hasHazard_(const MachineInstr &MI);
146
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000147 /// Update Defs and Uses. Return true if there exist dependences that
Akira Hatanakae9e588d2013-03-01 02:17:02 +0000148 /// disqualify the delay slot candidate between V and values in Uses and
149 /// Defs.
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000150 bool updateDefsUses(const Value *V, bool MayStore);
151
152 /// Get the list of underlying objects of MI's memory operand.
153 bool getUnderlyingObjects(const MachineInstr &MI,
154 SmallVectorImpl<const Value *> &Objects) const;
155
156 const MachineFrameInfo *MFI;
157 SmallPtrSet<const Value*, 4> Uses, Defs;
158
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000159 /// Flags indicating whether loads or stores with no underlying objects have
160 /// been seen.
161 bool SeenNoObjLoad, SeenNoObjStore;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000162 };
163
Akira Hatanakaa0612812013-02-07 21:32:32 +0000164 class Filler : public MachineFunctionPass {
165 public:
Bruno Cardoso Lopesfde21cf2010-12-09 17:31:11 +0000166 Filler(TargetMachine &tm)
Bill Wendlingead89ef2013-06-07 07:04:14 +0000167 : MachineFunctionPass(ID), TM(tm) { }
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000168
169 virtual const char *getPassName() const {
170 return "Mips Delay Slot Filler";
171 }
172
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000173 bool runOnMachineFunction(MachineFunction &F) {
174 bool Changed = false;
175 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
176 FI != FE; ++FI)
177 Changed |= runOnMachineBasicBlock(*FI);
178 return Changed;
179 }
180
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000181 void getAnalysisUsage(AnalysisUsage &AU) const {
182 AU.addRequired<MachineBranchProbabilityInfo>();
183 MachineFunctionPass::getAnalysisUsage(AU);
184 }
Akira Hatanakaa0612812013-02-07 21:32:32 +0000185
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000186 private:
Akira Hatanakaa0612812013-02-07 21:32:32 +0000187 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
188
Akira Hatanaka06bd1382013-02-14 23:40:57 +0000189 /// This function checks if it is valid to move Candidate to the delay slot
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000190 /// and returns true if it isn't. It also updates memory and register
191 /// dependence information.
192 bool delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000193 InspectMemInstr &IM) const;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000194
Akira Hatanakaf815db52013-03-01 00:26:14 +0000195 /// This function searches range [Begin, End) for an instruction that can be
196 /// moved to the delay slot. Returns true on success.
197 template<typename IterTy>
198 bool searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
Akira Hatanakae9e588d2013-03-01 02:17:02 +0000199 RegDefsUses &RegDU, InspectMemInstr &IM,
200 IterTy &Filler) const;
Akira Hatanakaf815db52013-03-01 00:26:14 +0000201
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000202 /// This function searches in the backward direction for an instruction that
203 /// can be moved to the delay slot. Returns true on success.
204 bool searchBackward(MachineBasicBlock &MBB, Iter Slot) const;
205
206 /// This function searches MBB in the forward direction for an instruction
207 /// that can be moved to the delay slot. Returns true on success.
208 bool searchForward(MachineBasicBlock &MBB, Iter Slot) const;
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000209
Akira Hatanaka1ff803f2013-03-25 20:11:16 +0000210 /// This function searches one of MBB's successor blocks for an instruction
211 /// that can be moved to the delay slot and inserts clones of the
212 /// instruction into the successor's predecessor blocks.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000213 bool searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const;
214
Akira Hatanakae9e588d2013-03-01 02:17:02 +0000215 /// Pick a successor block of MBB. Return NULL if MBB doesn't have a
216 /// successor block that is not a landing pad.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000217 MachineBasicBlock *selectSuccBB(MachineBasicBlock &B) const;
218
219 /// This function analyzes MBB and returns an instruction with an unoccupied
220 /// slot that branches to Dst.
221 std::pair<MipsInstrInfo::BranchType, MachineInstr *>
222 getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const;
223
224 /// Examine Pred and see if it is possible to insert an instruction into
225 /// one of its branches delay slot or its end.
226 bool examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
227 RegDefsUses &RegDU, bool &HasMultipleSuccs,
228 BB2BrMap &BrMap) const;
229
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000230 bool terminateSearch(const MachineInstr &Candidate) const;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000231
Akira Hatanakaa0612812013-02-07 21:32:32 +0000232 TargetMachine &TM;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000233
Akira Hatanakaa0612812013-02-07 21:32:32 +0000234 static char ID;
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000235 };
236 char Filler::ID = 0;
237} // end of anonymous namespace
238
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000239static bool hasUnoccupiedSlot(const MachineInstr *MI) {
240 return MI->hasDelaySlot() && !MI->isBundledWithSucc();
241}
242
243/// This function inserts clones of Filler into predecessor blocks.
244static void insertDelayFiller(Iter Filler, const BB2BrMap &BrMap) {
245 MachineFunction *MF = Filler->getParent()->getParent();
246
247 for (BB2BrMap::const_iterator I = BrMap.begin(); I != BrMap.end(); ++I) {
248 if (I->second) {
249 MIBundleBuilder(I->second).append(MF->CloneMachineInstr(&*Filler));
250 ++UsefulSlots;
251 } else {
252 I->first->insert(I->first->end(), MF->CloneMachineInstr(&*Filler));
253 }
254 }
255}
256
257/// This function adds registers Filler defines to MBB's live-in register list.
258static void addLiveInRegs(Iter Filler, MachineBasicBlock &MBB) {
259 for (unsigned I = 0, E = Filler->getNumOperands(); I != E; ++I) {
260 const MachineOperand &MO = Filler->getOperand(I);
261 unsigned R;
262
263 if (!MO.isReg() || !MO.isDef() || !(R = MO.getReg()))
264 continue;
265
266#ifndef NDEBUG
267 const MachineFunction &MF = *MBB.getParent();
268 assert(MF.getTarget().getRegisterInfo()->getAllocatableSet(MF).test(R) &&
269 "Shouldn't move an instruction with unallocatable registers across "
270 "basic block boundaries.");
271#endif
272
273 if (!MBB.isLiveIn(R))
274 MBB.addLiveIn(R);
275 }
276}
277
Akira Hatanaka979899e2013-02-26 01:30:05 +0000278RegDefsUses::RegDefsUses(TargetMachine &TM)
279 : TRI(*TM.getRegisterInfo()), Defs(TRI.getNumRegs(), false),
280 Uses(TRI.getNumRegs(), false) {}
281
282void RegDefsUses::init(const MachineInstr &MI) {
283 // Add all register operands which are explicit and non-variadic.
284 update(MI, 0, MI.getDesc().getNumOperands());
285
286 // If MI is a call, add RA to Defs to prevent users of RA from going into
287 // delay slot.
288 if (MI.isCall())
289 Defs.set(Mips::RA);
290
291 // Add all implicit register operands of branch instructions except
292 // register AT.
293 if (MI.isBranch()) {
294 update(MI, MI.getDesc().getNumOperands(), MI.getNumOperands());
295 Defs.reset(Mips::AT);
296 }
297}
298
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000299void RegDefsUses::setCallerSaved(const MachineInstr &MI) {
300 assert(MI.isCall());
301
302 // If MI is a call, add all caller-saved registers to Defs.
303 BitVector CallerSavedRegs(TRI.getNumRegs(), true);
304
305 CallerSavedRegs.reset(Mips::ZERO);
306 CallerSavedRegs.reset(Mips::ZERO_64);
307
308 for (const MCPhysReg *R = TRI.getCalleeSavedRegs(); *R; ++R)
309 for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI)
310 CallerSavedRegs.reset(*AI);
311
312 Defs |= CallerSavedRegs;
313}
314
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000315void RegDefsUses::setUnallocatableRegs(const MachineFunction &MF) {
316 BitVector AllocSet = TRI.getAllocatableSet(MF);
317
318 for (int R = AllocSet.find_first(); R != -1; R = AllocSet.find_next(R))
319 for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI)
320 AllocSet.set(*AI);
321
322 AllocSet.set(Mips::ZERO);
323 AllocSet.set(Mips::ZERO_64);
324
325 Defs |= AllocSet.flip();
326}
327
328void RegDefsUses::addLiveOut(const MachineBasicBlock &MBB,
329 const MachineBasicBlock &SuccBB) {
330 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
331 SE = MBB.succ_end(); SI != SE; ++SI)
332 if (*SI != &SuccBB)
333 for (MachineBasicBlock::livein_iterator LI = (*SI)->livein_begin(),
334 LE = (*SI)->livein_end(); LI != LE; ++LI)
335 Uses.set(*LI);
336}
337
Akira Hatanaka979899e2013-02-26 01:30:05 +0000338bool RegDefsUses::update(const MachineInstr &MI, unsigned Begin, unsigned End) {
339 BitVector NewDefs(TRI.getNumRegs()), NewUses(TRI.getNumRegs());
340 bool HasHazard = false;
341
342 for (unsigned I = Begin; I != End; ++I) {
343 const MachineOperand &MO = MI.getOperand(I);
344
345 if (MO.isReg() && MO.getReg())
346 HasHazard |= checkRegDefsUses(NewDefs, NewUses, MO.getReg(), MO.isDef());
347 }
348
349 Defs |= NewDefs;
350 Uses |= NewUses;
351
352 return HasHazard;
353}
354
355bool RegDefsUses::checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses,
356 unsigned Reg, bool IsDef) const {
357 if (IsDef) {
358 NewDefs.set(Reg);
359 // check whether Reg has already been defined or used.
360 return (isRegInSet(Defs, Reg) || isRegInSet(Uses, Reg));
361 }
362
363 NewUses.set(Reg);
364 // check whether Reg has already been defined.
365 return isRegInSet(Defs, Reg);
366}
367
368bool RegDefsUses::isRegInSet(const BitVector &RegSet, unsigned Reg) const {
369 // Check Reg and all aliased Registers.
370 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
371 if (RegSet.test(*AI))
372 return true;
373 return false;
374}
375
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000376bool InspectMemInstr::hasHazard(const MachineInstr &MI) {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000377 if (!MI.mayStore() && !MI.mayLoad())
378 return false;
379
380 if (ForbidMemInstr)
381 return true;
382
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000383 OrigSeenLoad = SeenLoad;
384 OrigSeenStore = SeenStore;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000385 SeenLoad |= MI.mayLoad();
386 SeenStore |= MI.mayStore();
387
388 // If MI is an ordered or volatile memory reference, disallow moving
389 // subsequent loads and stores to delay slot.
390 if (MI.hasOrderedMemoryRef() && (OrigSeenLoad || OrigSeenStore)) {
391 ForbidMemInstr = true;
392 return true;
393 }
394
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000395 return hasHazard_(MI);
396}
397
398bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) {
399 if (MI.mayStore())
400 return true;
401
402 if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getValue())
403 return true;
404
405 const Value *V = (*MI.memoperands_begin())->getValue();
406
407 if (isa<FixedStackPseudoSourceValue>(V))
408 return false;
409
410 if (const PseudoSourceValue *PSV = dyn_cast<const PseudoSourceValue>(V))
Akira Hatanakaaf4211a2013-09-28 00:12:32 +0000411 return !PSV->isConstant(0) && V != PseudoSourceValue::getStack();
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000412
413 return true;
414}
415
416MemDefsUses::MemDefsUses(const MachineFrameInfo *MFI_)
417 : InspectMemInstr(false), MFI(MFI_), SeenNoObjLoad(false),
418 SeenNoObjStore(false) {}
419
420bool MemDefsUses::hasHazard_(const MachineInstr &MI) {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000421 bool HasHazard = false;
422 SmallVector<const Value *, 4> Objs;
423
424 // Check underlying object list.
425 if (getUnderlyingObjects(MI, Objs)) {
Craig Topper31ee5862013-07-03 15:07:05 +0000426 for (SmallVectorImpl<const Value *>::const_iterator I = Objs.begin();
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000427 I != Objs.end(); ++I)
428 HasHazard |= updateDefsUses(*I, MI.mayStore());
429
430 return HasHazard;
431 }
432
433 // No underlying objects found.
434 HasHazard = MI.mayStore() && (OrigSeenLoad || OrigSeenStore);
435 HasHazard |= MI.mayLoad() || OrigSeenStore;
436
437 SeenNoObjLoad |= MI.mayLoad();
438 SeenNoObjStore |= MI.mayStore();
439
440 return HasHazard;
441}
442
443bool MemDefsUses::updateDefsUses(const Value *V, bool MayStore) {
444 if (MayStore)
445 return !Defs.insert(V) || Uses.count(V) || SeenNoObjStore || SeenNoObjLoad;
446
447 Uses.insert(V);
448 return Defs.count(V) || SeenNoObjStore;
449}
450
451bool MemDefsUses::
452getUnderlyingObjects(const MachineInstr &MI,
453 SmallVectorImpl<const Value *> &Objects) const {
454 if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getValue())
455 return false;
456
457 const Value *V = (*MI.memoperands_begin())->getValue();
458
459 SmallVector<Value *, 4> Objs;
460 GetUnderlyingObjects(const_cast<Value *>(V), Objs);
461
Craig Topper31ee5862013-07-03 15:07:05 +0000462 for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), E = Objs.end();
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000463 I != E; ++I) {
464 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(*I)) {
465 if (PSV->isAliased(MFI))
466 return false;
467 } else if (!isIdentifiedObject(V))
468 return false;
469
470 Objects.push_back(*I);
471 }
472
473 return true;
474}
475
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000476/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000477/// We assume there is only one delay slot per delayed instruction.
Akira Hatanaka1083eb12013-02-14 23:20:15 +0000478bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000479 bool Changed = false;
Akira Hatanakae7b06972011-10-05 01:30:09 +0000480
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000481 for (Iter I = MBB.begin(); I != MBB.end(); ++I) {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000482 if (!hasUnoccupiedSlot(&*I))
Akira Hatanakaa0612812013-02-07 21:32:32 +0000483 continue;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000484
Akira Hatanakaa0612812013-02-07 21:32:32 +0000485 ++FilledSlots;
486 Changed = true;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000487
Akira Hatanakaa0612812013-02-07 21:32:32 +0000488 // Delay slot filling is disabled at -O0.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000489 if (!DisableDelaySlotFiller && (TM.getOptLevel() != CodeGenOpt::None)) {
490 if (searchBackward(MBB, I))
491 continue;
492
493 if (I->isTerminator()) {
494 if (searchSuccBBs(MBB, I))
495 continue;
496 } else if (searchForward(MBB, I)) {
497 continue;
498 }
499 }
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000500
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000501 // Bundle the NOP to the instruction with the delay slot.
Bill Wendlingead89ef2013-06-07 07:04:14 +0000502 const MipsInstrInfo *TII =
503 static_cast<const MipsInstrInfo*>(TM.getInstrInfo());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000504 BuildMI(MBB, std::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
505 MIBundleBuilder(MBB, I, std::next(I, 2));
Akira Hatanakaa0612812013-02-07 21:32:32 +0000506 }
507
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000508 return Changed;
509}
510
511/// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
512/// slots in Mips MachineFunctions
513FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
514 return new Filler(tm);
515}
516
Akira Hatanakaf815db52013-03-01 00:26:14 +0000517template<typename IterTy>
518bool Filler::searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000519 RegDefsUses &RegDU, InspectMemInstr& IM,
Akira Hatanakaf815db52013-03-01 00:26:14 +0000520 IterTy &Filler) const {
521 for (IterTy I = Begin; I != End; ++I) {
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000522 // skip debug value
523 if (I->isDebugValue())
524 continue;
525
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000526 if (terminateSearch(*I))
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000527 break;
528
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000529 assert((!I->isCall() && !I->isReturn() && !I->isBranch()) &&
530 "Cannot put calls, returns or branches in delay slot.");
531
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000532 if (delayHasHazard(*I, RegDU, IM))
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000533 continue;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000534
Sasa Stankovic5fddf612014-03-10 20:34:23 +0000535 if (TM.getSubtarget<MipsSubtarget>().isTargetNaCl()) {
536 // In NaCl, instructions that must be masked are forbidden in delay slots.
537 // We only check for loads, stores and SP changes. Calls, returns and
538 // branches are not checked because non-NaCl targets never put them in
539 // delay slots.
540 unsigned AddrIdx;
541 if ((isBasePlusOffsetMemoryAccess(I->getOpcode(), &AddrIdx)
542 && baseRegNeedsLoadStoreMask(I->getOperand(AddrIdx).getReg()))
543 || I->modifiesRegister(Mips::SP, TM.getRegisterInfo()))
544 continue;
545 }
546
Akira Hatanakaf815db52013-03-01 00:26:14 +0000547 Filler = I;
548 return true;
549 }
550
551 return false;
552}
553
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000554bool Filler::searchBackward(MachineBasicBlock &MBB, Iter Slot) const {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000555 if (DisableBackwardSearch)
556 return false;
557
Akira Hatanakaf815db52013-03-01 00:26:14 +0000558 RegDefsUses RegDU(TM);
559 MemDefsUses MemDU(MBB.getParent()->getFrameInfo());
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000560 ReverseIter Filler;
Akira Hatanakaf815db52013-03-01 00:26:14 +0000561
562 RegDU.init(*Slot);
563
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000564 if (!searchRange(MBB, ReverseIter(Slot), MBB.rend(), RegDU, MemDU, Filler))
565 return false;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000566
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000567 MBB.splice(std::next(Slot), &MBB, std::next(Filler).base());
568 MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000569 ++UsefulSlots;
570 return true;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000571}
572
573bool Filler::searchForward(MachineBasicBlock &MBB, Iter Slot) const {
574 // Can handle only calls.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000575 if (DisableForwardSearch || !Slot->isCall())
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000576 return false;
577
578 RegDefsUses RegDU(TM);
579 NoMemInstr NM;
580 Iter Filler;
581
582 RegDU.setCallerSaved(*Slot);
583
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000584 if (!searchRange(MBB, std::next(Slot), MBB.end(), RegDU, NM, Filler))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000585 return false;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000586
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000587 MBB.splice(std::next(Slot), &MBB, Filler);
588 MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000589 ++UsefulSlots;
590 return true;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000591}
592
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000593bool Filler::searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const {
594 if (DisableSuccBBSearch)
595 return false;
596
597 MachineBasicBlock *SuccBB = selectSuccBB(MBB);
598
599 if (!SuccBB)
600 return false;
601
602 RegDefsUses RegDU(TM);
603 bool HasMultipleSuccs = false;
604 BB2BrMap BrMap;
605 OwningPtr<InspectMemInstr> IM;
606 Iter Filler;
607
608 // Iterate over SuccBB's predecessor list.
609 for (MachineBasicBlock::pred_iterator PI = SuccBB->pred_begin(),
610 PE = SuccBB->pred_end(); PI != PE; ++PI)
611 if (!examinePred(**PI, *SuccBB, RegDU, HasMultipleSuccs, BrMap))
612 return false;
613
614 // Do not allow moving instructions which have unallocatable register operands
615 // across basic block boundaries.
616 RegDU.setUnallocatableRegs(*MBB.getParent());
617
618 // Only allow moving loads from stack or constants if any of the SuccBB's
619 // predecessors have multiple successors.
620 if (HasMultipleSuccs) {
621 IM.reset(new LoadFromStackOrConst());
622 } else {
623 const MachineFrameInfo *MFI = MBB.getParent()->getFrameInfo();
624 IM.reset(new MemDefsUses(MFI));
625 }
626
627 if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Filler))
628 return false;
629
630 insertDelayFiller(Filler, BrMap);
631 addLiveInRegs(Filler, *SuccBB);
632 Filler->eraseFromParent();
633
634 return true;
635}
636
637MachineBasicBlock *Filler::selectSuccBB(MachineBasicBlock &B) const {
638 if (B.succ_empty())
639 return NULL;
640
641 // Select the successor with the larget edge weight.
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000642 auto &Prob = getAnalysis<MachineBranchProbabilityInfo>();
643 MachineBasicBlock *S = *std::max_element(B.succ_begin(), B.succ_end(),
644 [&](const MachineBasicBlock *Dst0,
645 const MachineBasicBlock *Dst1) {
646 return Prob.getEdgeWeight(&B, Dst0) < Prob.getEdgeWeight(&B, Dst1);
647 });
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000648 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() ||
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000720 Candidate.isPosition() || Candidate.isInlineAsm() ||
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000721 Candidate.hasUnmodeledSideEffects());
722}