blob: fe53cad56a74d889f02db4aef46220f14990582d [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
Sasa Stankovic5fddf612014-03-10 20:34:23 +000014#include "MCTargetDesc/MipsMCNaCl.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000015#include "Mips.h"
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000016#include "MipsInstrInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000017#include "MipsTargetMachine.h"
Akira Hatanaka06bd1382013-02-14 23:40:57 +000018#include "llvm/ADT/BitVector.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000019#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/Statistic.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000021#include "llvm/Analysis/AliasAnalysis.h"
22#include "llvm/Analysis/ValueTracking.h"
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000023#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
Daniel Sanders308181e2014-06-12 10:44:10 +000026#include "llvm/CodeGen/MachineRegisterInfo.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
Chandler Carruth84e68b22014-04-22 02:41:26 +000035#define DEBUG_TYPE "delay-slot-filler"
36
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000037STATISTIC(FilledSlots, "Number of delay slots filled");
Akira Hatanaka9e603442011-10-05 01:19:13 +000038STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
Akira Hatanaka02e760a2011-10-05 02:22:49 +000039 " are not NOP.");
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000040
Akira Hatanaka9d957842012-08-22 02:51:28 +000041static cl::opt<bool> DisableDelaySlotFiller(
42 "disable-mips-delay-filler",
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000043 cl::init(false),
Akira Hatanaka1083eb12013-02-14 23:20:15 +000044 cl::desc("Fill all delay slots with NOPs."),
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000045 cl::Hidden);
46
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000047static cl::opt<bool> DisableForwardSearch(
48 "disable-mips-df-forward-search",
49 cl::init(true),
50 cl::desc("Disallow MIPS delay filler to search forward."),
51 cl::Hidden);
52
Akira Hatanakae44e30c2013-03-01 01:02:36 +000053static cl::opt<bool> DisableSuccBBSearch(
54 "disable-mips-df-succbb-search",
55 cl::init(true),
56 cl::desc("Disallow MIPS delay filler to search successor basic blocks."),
57 cl::Hidden);
58
59static cl::opt<bool> DisableBackwardSearch(
60 "disable-mips-df-backward-search",
61 cl::init(false),
62 cl::desc("Disallow MIPS delay filler to search backward."),
63 cl::Hidden);
64
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000065namespace {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000066 typedef MachineBasicBlock::iterator Iter;
67 typedef MachineBasicBlock::reverse_iterator ReverseIter;
68 typedef SmallDenseMap<MachineBasicBlock*, MachineInstr*, 2> BB2BrMap;
69
Akira Hatanaka979899e2013-02-26 01:30:05 +000070 class RegDefsUses {
71 public:
Eric Christopher96e72c62015-01-29 23:27:36 +000072 RegDefsUses(const TargetRegisterInfo &TRI);
Akira Hatanaka979899e2013-02-26 01:30:05 +000073 void init(const MachineInstr &MI);
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000074
75 /// This function sets all caller-saved registers in Defs.
76 void setCallerSaved(const MachineInstr &MI);
77
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000078 /// This function sets all unallocatable registers in Defs.
79 void setUnallocatableRegs(const MachineFunction &MF);
80
81 /// Set bits in Uses corresponding to MBB's live-out registers except for
82 /// the registers that are live-in to SuccBB.
83 void addLiveOut(const MachineBasicBlock &MBB,
84 const MachineBasicBlock &SuccBB);
85
Akira Hatanaka979899e2013-02-26 01:30:05 +000086 bool update(const MachineInstr &MI, unsigned Begin, unsigned End);
87
88 private:
89 bool checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, unsigned Reg,
90 bool IsDef) const;
91
92 /// Returns true if Reg or its alias is in RegSet.
93 bool isRegInSet(const BitVector &RegSet, unsigned Reg) const;
94
95 const TargetRegisterInfo &TRI;
96 BitVector Defs, Uses;
97 };
98
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000099 /// Base class for inspecting loads and stores.
100 class InspectMemInstr {
101 public:
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000102 InspectMemInstr(bool ForbidMemInstr_)
103 : OrigSeenLoad(false), OrigSeenStore(false), SeenLoad(false),
104 SeenStore(false), ForbidMemInstr(ForbidMemInstr_) {}
105
106 /// Return true if MI cannot be moved to delay slot.
107 bool hasHazard(const MachineInstr &MI);
108
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000109 virtual ~InspectMemInstr() {}
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000110
111 protected:
112 /// Flags indicating whether loads or stores have been seen.
113 bool OrigSeenLoad, OrigSeenStore, SeenLoad, SeenStore;
114
115 /// Memory instructions are not allowed to move to delay slot if this flag
116 /// is true.
117 bool ForbidMemInstr;
118
119 private:
120 virtual bool hasHazard_(const MachineInstr &MI) = 0;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000121 };
122
123 /// This subclass rejects any memory instructions.
124 class NoMemInstr : public InspectMemInstr {
125 public:
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000126 NoMemInstr() : InspectMemInstr(true) {}
127 private:
Craig Topper56c590a2014-04-29 07:58:02 +0000128 bool hasHazard_(const MachineInstr &MI) override { return true; }
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000129 };
130
131 /// This subclass accepts loads from stacks and constant loads.
132 class LoadFromStackOrConst : public InspectMemInstr {
133 public:
134 LoadFromStackOrConst() : InspectMemInstr(false) {}
135 private:
Craig Topper56c590a2014-04-29 07:58:02 +0000136 bool hasHazard_(const MachineInstr &MI) override;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000137 };
138
139 /// This subclass uses memory dependence information to determine whether a
140 /// memory instruction can be moved to a delay slot.
141 class MemDefsUses : public InspectMemInstr {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000142 public:
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000143 MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI);
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000144
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000145 private:
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000146 typedef PointerUnion<const Value *, const PseudoSourceValue *> ValueType;
147
Craig Topper56c590a2014-04-29 07:58:02 +0000148 bool hasHazard_(const MachineInstr &MI) override;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000149
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000150 /// Update Defs and Uses. Return true if there exist dependences that
Akira Hatanakae9e588d2013-03-01 02:17:02 +0000151 /// disqualify the delay slot candidate between V and values in Uses and
152 /// Defs.
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000153 bool updateDefsUses(ValueType V, bool MayStore);
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000154
155 /// Get the list of underlying objects of MI's memory operand.
156 bool getUnderlyingObjects(const MachineInstr &MI,
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000157 SmallVectorImpl<ValueType> &Objects) const;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000158
159 const MachineFrameInfo *MFI;
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000160 SmallPtrSet<ValueType, 4> Uses, Defs;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000161 const DataLayout &DL;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000162
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000163 /// Flags indicating whether loads or stores with no underlying objects have
164 /// been seen.
165 bool SeenNoObjLoad, SeenNoObjStore;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000166 };
167
Akira Hatanakaa0612812013-02-07 21:32:32 +0000168 class Filler : public MachineFunctionPass {
169 public:
Bruno Cardoso Lopesfde21cf2010-12-09 17:31:11 +0000170 Filler(TargetMachine &tm)
Bill Wendlingead89ef2013-06-07 07:04:14 +0000171 : MachineFunctionPass(ID), TM(tm) { }
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000172
Craig Topper56c590a2014-04-29 07:58:02 +0000173 const char *getPassName() const override {
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000174 return "Mips Delay Slot Filler";
175 }
176
Craig Topper56c590a2014-04-29 07:58:02 +0000177 bool runOnMachineFunction(MachineFunction &F) override {
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000178 bool Changed = false;
179 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
180 FI != FE; ++FI)
181 Changed |= runOnMachineBasicBlock(*FI);
Daniel Sanders308181e2014-06-12 10:44:10 +0000182
183 // This pass invalidates liveness information when it reorders
184 // instructions to fill delay slot. Without this, -verify-machineinstrs
185 // will fail.
186 if (Changed)
187 F.getRegInfo().invalidateLiveness();
188
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000189 return Changed;
190 }
191
Craig Topper56c590a2014-04-29 07:58:02 +0000192 void getAnalysisUsage(AnalysisUsage &AU) const override {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000193 AU.addRequired<MachineBranchProbabilityInfo>();
194 MachineFunctionPass::getAnalysisUsage(AU);
195 }
Akira Hatanakaa0612812013-02-07 21:32:32 +0000196
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000197 private:
Akira Hatanakaa0612812013-02-07 21:32:32 +0000198 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
199
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000200 Iter replaceWithCompactBranch(MachineBasicBlock &MBB,
201 Iter Branch, DebugLoc DL);
202
Jozef Kolek650a61a2015-02-13 17:51:27 +0000203 Iter replaceWithCompactJump(MachineBasicBlock &MBB,
204 Iter Jump, DebugLoc DL);
205
Akira Hatanaka06bd1382013-02-14 23:40:57 +0000206 /// This function checks if it is valid to move Candidate to the delay slot
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000207 /// and returns true if it isn't. It also updates memory and register
208 /// dependence information.
209 bool delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000210 InspectMemInstr &IM) const;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000211
Akira Hatanakaf815db52013-03-01 00:26:14 +0000212 /// This function searches range [Begin, End) for an instruction that can be
213 /// moved to the delay slot. Returns true on success.
214 template<typename IterTy>
215 bool searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
Vasileios Kalintiris87614902015-03-04 12:37:58 +0000216 RegDefsUses &RegDU, InspectMemInstr &IM, Iter Slot,
217 IterTy &Filler) const;
Akira Hatanakaf815db52013-03-01 00:26:14 +0000218
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000219 /// This function searches in the backward direction for an instruction that
220 /// can be moved to the delay slot. Returns true on success.
221 bool searchBackward(MachineBasicBlock &MBB, Iter Slot) const;
222
223 /// This function searches MBB in the forward direction for an instruction
224 /// that can be moved to the delay slot. Returns true on success.
225 bool searchForward(MachineBasicBlock &MBB, Iter Slot) const;
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000226
Akira Hatanaka1ff803f2013-03-25 20:11:16 +0000227 /// This function searches one of MBB's successor blocks for an instruction
228 /// that can be moved to the delay slot and inserts clones of the
229 /// instruction into the successor's predecessor blocks.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000230 bool searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const;
231
Akira Hatanakae9e588d2013-03-01 02:17:02 +0000232 /// Pick a successor block of MBB. Return NULL if MBB doesn't have a
233 /// successor block that is not a landing pad.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000234 MachineBasicBlock *selectSuccBB(MachineBasicBlock &B) const;
235
236 /// This function analyzes MBB and returns an instruction with an unoccupied
237 /// slot that branches to Dst.
238 std::pair<MipsInstrInfo::BranchType, MachineInstr *>
239 getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const;
240
241 /// Examine Pred and see if it is possible to insert an instruction into
242 /// one of its branches delay slot or its end.
243 bool examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
244 RegDefsUses &RegDU, bool &HasMultipleSuccs,
245 BB2BrMap &BrMap) const;
246
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000247 bool terminateSearch(const MachineInstr &Candidate) const;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000248
Akira Hatanakaa0612812013-02-07 21:32:32 +0000249 TargetMachine &TM;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000250
Akira Hatanakaa0612812013-02-07 21:32:32 +0000251 static char ID;
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000252 };
253 char Filler::ID = 0;
254} // end of anonymous namespace
255
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000256static bool hasUnoccupiedSlot(const MachineInstr *MI) {
257 return MI->hasDelaySlot() && !MI->isBundledWithSucc();
258}
259
260/// This function inserts clones of Filler into predecessor blocks.
261static void insertDelayFiller(Iter Filler, const BB2BrMap &BrMap) {
262 MachineFunction *MF = Filler->getParent()->getParent();
263
264 for (BB2BrMap::const_iterator I = BrMap.begin(); I != BrMap.end(); ++I) {
265 if (I->second) {
266 MIBundleBuilder(I->second).append(MF->CloneMachineInstr(&*Filler));
267 ++UsefulSlots;
268 } else {
269 I->first->insert(I->first->end(), MF->CloneMachineInstr(&*Filler));
270 }
271 }
272}
273
274/// This function adds registers Filler defines to MBB's live-in register list.
275static void addLiveInRegs(Iter Filler, MachineBasicBlock &MBB) {
276 for (unsigned I = 0, E = Filler->getNumOperands(); I != E; ++I) {
277 const MachineOperand &MO = Filler->getOperand(I);
278 unsigned R;
279
280 if (!MO.isReg() || !MO.isDef() || !(R = MO.getReg()))
281 continue;
282
283#ifndef NDEBUG
284 const MachineFunction &MF = *MBB.getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +0000285 assert(MF.getSubtarget().getRegisterInfo()->getAllocatableSet(MF).test(R) &&
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000286 "Shouldn't move an instruction with unallocatable registers across "
287 "basic block boundaries.");
288#endif
289
290 if (!MBB.isLiveIn(R))
291 MBB.addLiveIn(R);
292 }
293}
294
Eric Christopher96e72c62015-01-29 23:27:36 +0000295RegDefsUses::RegDefsUses(const TargetRegisterInfo &TRI)
296 : TRI(TRI), Defs(TRI.getNumRegs(), false), Uses(TRI.getNumRegs(), false) {}
Akira Hatanaka979899e2013-02-26 01:30:05 +0000297
298void RegDefsUses::init(const MachineInstr &MI) {
299 // Add all register operands which are explicit and non-variadic.
300 update(MI, 0, MI.getDesc().getNumOperands());
301
302 // If MI is a call, add RA to Defs to prevent users of RA from going into
303 // delay slot.
304 if (MI.isCall())
305 Defs.set(Mips::RA);
306
307 // Add all implicit register operands of branch instructions except
308 // register AT.
309 if (MI.isBranch()) {
310 update(MI, MI.getDesc().getNumOperands(), MI.getNumOperands());
311 Defs.reset(Mips::AT);
312 }
313}
314
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000315void RegDefsUses::setCallerSaved(const MachineInstr &MI) {
316 assert(MI.isCall());
317
Vasileios Kalintiris70b744e2015-05-14 13:17:56 +0000318 // Add RA/RA_64 to Defs to prevent users of RA/RA_64 from going into
319 // the delay slot. The reason is that RA/RA_64 must not be changed
320 // in the delay slot so that the callee can return to the caller.
321 if (MI.definesRegister(Mips::RA) || MI.definesRegister(Mips::RA_64)) {
322 Defs.set(Mips::RA);
323 Defs.set(Mips::RA_64);
324 }
325
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000326 // If MI is a call, add all caller-saved registers to Defs.
327 BitVector CallerSavedRegs(TRI.getNumRegs(), true);
328
329 CallerSavedRegs.reset(Mips::ZERO);
330 CallerSavedRegs.reset(Mips::ZERO_64);
331
Eric Christopher7af952872015-03-11 21:41:28 +0000332 for (const MCPhysReg *R = TRI.getCalleeSavedRegs(MI.getParent()->getParent());
333 *R; ++R)
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000334 for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI)
335 CallerSavedRegs.reset(*AI);
336
337 Defs |= CallerSavedRegs;
338}
339
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000340void RegDefsUses::setUnallocatableRegs(const MachineFunction &MF) {
341 BitVector AllocSet = TRI.getAllocatableSet(MF);
342
343 for (int R = AllocSet.find_first(); R != -1; R = AllocSet.find_next(R))
344 for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI)
345 AllocSet.set(*AI);
346
347 AllocSet.set(Mips::ZERO);
348 AllocSet.set(Mips::ZERO_64);
349
350 Defs |= AllocSet.flip();
351}
352
353void RegDefsUses::addLiveOut(const MachineBasicBlock &MBB,
354 const MachineBasicBlock &SuccBB) {
355 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
356 SE = MBB.succ_end(); SI != SE; ++SI)
357 if (*SI != &SuccBB)
Matthias Braund9da1622015-09-09 18:08:03 +0000358 for (const auto &LI : (*SI)->liveins())
359 Uses.set(LI.PhysReg);
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000360}
361
Akira Hatanaka979899e2013-02-26 01:30:05 +0000362bool RegDefsUses::update(const MachineInstr &MI, unsigned Begin, unsigned End) {
363 BitVector NewDefs(TRI.getNumRegs()), NewUses(TRI.getNumRegs());
364 bool HasHazard = false;
365
366 for (unsigned I = Begin; I != End; ++I) {
367 const MachineOperand &MO = MI.getOperand(I);
368
369 if (MO.isReg() && MO.getReg())
370 HasHazard |= checkRegDefsUses(NewDefs, NewUses, MO.getReg(), MO.isDef());
371 }
372
373 Defs |= NewDefs;
374 Uses |= NewUses;
375
376 return HasHazard;
377}
378
379bool RegDefsUses::checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses,
380 unsigned Reg, bool IsDef) const {
381 if (IsDef) {
382 NewDefs.set(Reg);
383 // check whether Reg has already been defined or used.
384 return (isRegInSet(Defs, Reg) || isRegInSet(Uses, Reg));
385 }
386
387 NewUses.set(Reg);
388 // check whether Reg has already been defined.
389 return isRegInSet(Defs, Reg);
390}
391
392bool RegDefsUses::isRegInSet(const BitVector &RegSet, unsigned Reg) const {
393 // Check Reg and all aliased Registers.
394 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
395 if (RegSet.test(*AI))
396 return true;
397 return false;
398}
399
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000400bool InspectMemInstr::hasHazard(const MachineInstr &MI) {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000401 if (!MI.mayStore() && !MI.mayLoad())
402 return false;
403
404 if (ForbidMemInstr)
405 return true;
406
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000407 OrigSeenLoad = SeenLoad;
408 OrigSeenStore = SeenStore;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000409 SeenLoad |= MI.mayLoad();
410 SeenStore |= MI.mayStore();
411
412 // If MI is an ordered or volatile memory reference, disallow moving
413 // subsequent loads and stores to delay slot.
414 if (MI.hasOrderedMemoryRef() && (OrigSeenLoad || OrigSeenStore)) {
415 ForbidMemInstr = true;
416 return true;
417 }
418
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000419 return hasHazard_(MI);
420}
421
422bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) {
423 if (MI.mayStore())
424 return true;
425
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000426 if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getPseudoValue())
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000427 return true;
428
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000429 if (const PseudoSourceValue *PSV =
430 (*MI.memoperands_begin())->getPseudoValue()) {
431 if (isa<FixedStackPseudoSourceValue>(PSV))
432 return false;
Alex Lorenze40c8a22015-08-11 23:09:45 +0000433 return !PSV->isConstant(nullptr) && !PSV->isStack();
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000434 }
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000435
436 return true;
437}
438
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000439MemDefsUses::MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI_)
440 : InspectMemInstr(false), MFI(MFI_), DL(DL), SeenNoObjLoad(false),
441 SeenNoObjStore(false) {}
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000442
443bool MemDefsUses::hasHazard_(const MachineInstr &MI) {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000444 bool HasHazard = false;
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000445 SmallVector<ValueType, 4> Objs;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000446
447 // Check underlying object list.
448 if (getUnderlyingObjects(MI, Objs)) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000449 for (SmallVectorImpl<ValueType>::const_iterator I = Objs.begin();
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000450 I != Objs.end(); ++I)
451 HasHazard |= updateDefsUses(*I, MI.mayStore());
452
453 return HasHazard;
454 }
455
456 // No underlying objects found.
457 HasHazard = MI.mayStore() && (OrigSeenLoad || OrigSeenStore);
458 HasHazard |= MI.mayLoad() || OrigSeenStore;
459
460 SeenNoObjLoad |= MI.mayLoad();
461 SeenNoObjStore |= MI.mayStore();
462
463 return HasHazard;
464}
465
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000466bool MemDefsUses::updateDefsUses(ValueType V, bool MayStore) {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000467 if (MayStore)
David Blaikie70573dc2014-11-19 07:49:26 +0000468 return !Defs.insert(V).second || Uses.count(V) || SeenNoObjStore ||
469 SeenNoObjLoad;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000470
471 Uses.insert(V);
472 return Defs.count(V) || SeenNoObjStore;
473}
474
475bool MemDefsUses::
476getUnderlyingObjects(const MachineInstr &MI,
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000477 SmallVectorImpl<ValueType> &Objects) const {
478 if (!MI.hasOneMemOperand() ||
479 (!(*MI.memoperands_begin())->getValue() &&
480 !(*MI.memoperands_begin())->getPseudoValue()))
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000481 return false;
482
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000483 if (const PseudoSourceValue *PSV =
484 (*MI.memoperands_begin())->getPseudoValue()) {
485 if (!PSV->isAliased(MFI))
486 return false;
487 Objects.push_back(PSV);
488 return true;
489 }
490
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000491 const Value *V = (*MI.memoperands_begin())->getValue();
492
493 SmallVector<Value *, 4> Objs;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000494 GetUnderlyingObjects(const_cast<Value *>(V), Objs, DL);
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000495
Craig Topper31ee5862013-07-03 15:07:05 +0000496 for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), E = Objs.end();
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000497 I != E; ++I) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000498 if (!isIdentifiedObject(V))
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000499 return false;
500
501 Objects.push_back(*I);
502 }
503
504 return true;
505}
506
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000507// Replace Branch with the compact branch instruction.
508Iter Filler::replaceWithCompactBranch(MachineBasicBlock &MBB,
509 Iter Branch, DebugLoc DL) {
Daniel Sanderse8efff32016-03-14 16:24:05 +0000510 const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
511 const MipsInstrInfo *TII = STI.getInstrInfo();
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000512
Daniel Sanderse8efff32016-03-14 16:24:05 +0000513 unsigned NewOpcode = TII->getEquivalentCompactForm(Branch);
514 Branch = TII->genInstrWithNewOpc(NewOpcode, Branch);
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000515
Daniel Sanderse8efff32016-03-14 16:24:05 +0000516 std::next(Branch)->eraseFromParent();
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000517 return Branch;
518}
519
Jozef Kolek650a61a2015-02-13 17:51:27 +0000520// Replace Jumps with the compact jump instruction.
521Iter Filler::replaceWithCompactJump(MachineBasicBlock &MBB,
522 Iter Jump, DebugLoc DL) {
523 const MipsInstrInfo *TII =
524 MBB.getParent()->getSubtarget<MipsSubtarget>().getInstrInfo();
525
526 const MCInstrDesc &NewDesc = TII->get(Mips::JRC16_MM);
527 MachineInstrBuilder MIB = BuildMI(MBB, Jump, DL, NewDesc);
528
529 MIB.addReg(Jump->getOperand(0).getReg());
530
531 Iter tmpIter = Jump;
532 Jump = std::prev(Jump);
533 MBB.erase(tmpIter);
534
535 return Jump;
536}
537
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000538// For given opcode returns opcode of corresponding instruction with short
539// delay slot.
540static int getEquivalentCallShort(int Opcode) {
541 switch (Opcode) {
542 case Mips::BGEZAL:
543 return Mips::BGEZALS_MM;
544 case Mips::BLTZAL:
545 return Mips::BLTZALS_MM;
546 case Mips::JAL:
547 return Mips::JALS_MM;
548 case Mips::JALR:
549 return Mips::JALRS_MM;
550 case Mips::JALR16_MM:
551 return Mips::JALRS16_MM;
552 default:
553 llvm_unreachable("Unexpected call instruction for microMIPS.");
554 }
555}
556
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000557/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000558/// We assume there is only one delay slot per delayed instruction.
Akira Hatanaka1083eb12013-02-14 23:20:15 +0000559bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000560 bool Changed = false;
Eric Christopher6b6db772015-02-02 23:03:43 +0000561 const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
Eric Christopher96e72c62015-01-29 23:27:36 +0000562 bool InMicroMipsMode = STI.inMicroMipsMode();
563 const MipsInstrInfo *TII = STI.getInstrInfo();
Akira Hatanakae7b06972011-10-05 01:30:09 +0000564
Hrvoje Vargac45baf22016-03-23 10:29:38 +0000565 if (InMicroMipsMode && STI.hasMips32r6()) {
566 // This is microMIPS32r6 or microMIPS64r6 processor. Delay slot for
567 // branching instructions is not needed.
568 return Changed;
569 }
570
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000571 for (Iter I = MBB.begin(); I != MBB.end(); ++I) {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000572 if (!hasUnoccupiedSlot(&*I))
Akira Hatanakaa0612812013-02-07 21:32:32 +0000573 continue;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000574
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000575 ++FilledSlots;
576 Changed = true;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000577
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000578 // Delay slot filling is disabled at -O0.
579 if (!DisableDelaySlotFiller && (TM.getOptLevel() != CodeGenOpt::None)) {
580 bool Filled = false;
Zoran Jovanovic37bca102014-11-10 17:27:56 +0000581
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000582 if (searchBackward(MBB, I)) {
583 Filled = true;
584 } else if (I->isTerminator()) {
585 if (searchSuccBBs(MBB, I)) {
586 Filled = true;
Zoran Jovanovic37bca102014-11-10 17:27:56 +0000587 }
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000588 } else if (searchForward(MBB, I)) {
589 Filled = true;
590 }
591
592 if (Filled) {
593 // Get instruction with delay slot.
594 MachineBasicBlock::instr_iterator DSI(I);
595
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000596 if (InMicroMipsMode && TII->GetInstSizeInBytes(&*std::next(DSI)) == 2 &&
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000597 DSI->isCall()) {
598 // If instruction in delay slot is 16b change opcode to
599 // corresponding instruction with short delay slot.
600 DSI->setDesc(TII->get(getEquivalentCallShort(DSI->getOpcode())));
601 }
602
603 continue;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000604 }
605 }
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000606
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000607 // If instruction is BEQ or BNE with one ZERO register, then instead of
608 // adding NOP replace this instruction with the corresponding compact
609 // branch instruction, i.e. BEQZC or BNEZC.
Jozef Kolek650a61a2015-02-13 17:51:27 +0000610 if (InMicroMipsMode) {
Daniel Sanderse8efff32016-03-14 16:24:05 +0000611 if (TII->getEquivalentCompactForm(I)) {
612 I = replaceWithCompactBranch(MBB, I, I->getDebugLoc());
613 continue;
614 }
615
616 if (I->isIndirectBranch() || I->isReturn()) {
617 // For microMIPS the PseudoReturn and PseudoIndirectBranch are always
618 // expanded to JR_MM, so they can be replaced with JRC16_MM.
619 I = replaceWithCompactJump(MBB, I, I->getDebugLoc());
620 continue;
Jozef Kolek650a61a2015-02-13 17:51:27 +0000621 }
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000622 }
Daniel Sanderse8efff32016-03-14 16:24:05 +0000623
624 // For MIPSR6 attempt to produce the corresponding compact (no delay slot)
625 // form of the branch. This should save putting in a NOP.
626 if ((STI.hasMips32r6()) && TII->getEquivalentCompactForm(I)) {
627 I = replaceWithCompactBranch(MBB, I, I->getDebugLoc());
628 continue;
629 }
630
Jozef Kolek650a61a2015-02-13 17:51:27 +0000631 // Bundle the NOP to the instruction with the delay slot.
632 BuildMI(MBB, std::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
633 MIBundleBuilder(MBB, I, std::next(I, 2));
Akira Hatanakaa0612812013-02-07 21:32:32 +0000634 }
635
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000636 return Changed;
637}
638
639/// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
640/// slots in Mips MachineFunctions
641FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
642 return new Filler(tm);
643}
644
Akira Hatanakaf815db52013-03-01 00:26:14 +0000645template<typename IterTy>
646bool Filler::searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
Vasileios Kalintiris87614902015-03-04 12:37:58 +0000647 RegDefsUses &RegDU, InspectMemInstr& IM, Iter Slot,
648 IterTy &Filler) const {
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000649 bool IsReverseIter = std::is_convertible<IterTy, ReverseIter>::value;
650
651 for (IterTy I = Begin; I != End;) {
652 IterTy CurrI = I;
653 ++I;
654
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000655 // skip debug value
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000656 if (CurrI->isDebugValue())
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000657 continue;
658
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000659 if (terminateSearch(*CurrI))
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000660 break;
661
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000662 assert((!CurrI->isCall() && !CurrI->isReturn() && !CurrI->isBranch()) &&
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000663 "Cannot put calls, returns or branches in delay slot.");
664
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000665 if (CurrI->isKill()) {
666 CurrI->eraseFromParent();
667
668 // This special case is needed for reverse iterators, because when we
669 // erase an instruction, the iterators are updated to point to the next
670 // instruction.
671 if (IsReverseIter && I != End)
672 I = CurrI;
673 continue;
674 }
675
676 if (delayHasHazard(*CurrI, RegDU, IM))
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000677 continue;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000678
Eric Christopher6b6db772015-02-02 23:03:43 +0000679 const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
680 if (STI.isTargetNaCl()) {
Sasa Stankovic5fddf612014-03-10 20:34:23 +0000681 // In NaCl, instructions that must be masked are forbidden in delay slots.
682 // We only check for loads, stores and SP changes. Calls, returns and
683 // branches are not checked because non-NaCl targets never put them in
684 // delay slots.
685 unsigned AddrIdx;
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000686 if ((isBasePlusOffsetMemoryAccess(CurrI->getOpcode(), &AddrIdx) &&
687 baseRegNeedsLoadStoreMask(CurrI->getOperand(AddrIdx).getReg())) ||
688 CurrI->modifiesRegister(Mips::SP, STI.getRegisterInfo()))
Sasa Stankovic5fddf612014-03-10 20:34:23 +0000689 continue;
690 }
691
Eric Christopher6b6db772015-02-02 23:03:43 +0000692 bool InMicroMipsMode = STI.inMicroMipsMode();
693 const MipsInstrInfo *TII = STI.getInstrInfo();
Jozef Koleke7cad7a2015-01-13 15:59:17 +0000694 unsigned Opcode = (*Slot).getOpcode();
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000695 if (InMicroMipsMode && TII->GetInstSizeInBytes(&(*CurrI)) == 2 &&
Jozef Koleke7cad7a2015-01-13 15:59:17 +0000696 (Opcode == Mips::JR || Opcode == Mips::PseudoIndirectBranch ||
697 Opcode == Mips::PseudoReturn))
698 continue;
699
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000700 Filler = CurrI;
Akira Hatanakaf815db52013-03-01 00:26:14 +0000701 return true;
702 }
703
704 return false;
705}
706
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000707bool Filler::searchBackward(MachineBasicBlock &MBB, Iter Slot) const {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000708 if (DisableBackwardSearch)
709 return false;
710
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000711 auto *Fn = MBB.getParent();
712 RegDefsUses RegDU(*Fn->getSubtarget().getRegisterInfo());
713 MemDefsUses MemDU(Fn->getDataLayout(), Fn->getFrameInfo());
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000714 ReverseIter Filler;
Akira Hatanakaf815db52013-03-01 00:26:14 +0000715
716 RegDU.init(*Slot);
717
Vasileios Kalintiris87614902015-03-04 12:37:58 +0000718 if (!searchRange(MBB, ReverseIter(Slot), MBB.rend(), RegDU, MemDU, Slot,
719 Filler))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000720 return false;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000721
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000722 MBB.splice(std::next(Slot), &MBB, std::next(Filler).base());
723 MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000724 ++UsefulSlots;
725 return true;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000726}
727
728bool Filler::searchForward(MachineBasicBlock &MBB, Iter Slot) const {
729 // Can handle only calls.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000730 if (DisableForwardSearch || !Slot->isCall())
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000731 return false;
732
Eric Christopher96e72c62015-01-29 23:27:36 +0000733 RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000734 NoMemInstr NM;
735 Iter Filler;
736
737 RegDU.setCallerSaved(*Slot);
738
Vasileios Kalintiris87614902015-03-04 12:37:58 +0000739 if (!searchRange(MBB, std::next(Slot), MBB.end(), RegDU, NM, Slot, Filler))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000740 return false;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000741
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000742 MBB.splice(std::next(Slot), &MBB, Filler);
743 MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000744 ++UsefulSlots;
745 return true;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000746}
747
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000748bool Filler::searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const {
749 if (DisableSuccBBSearch)
750 return false;
751
752 MachineBasicBlock *SuccBB = selectSuccBB(MBB);
753
754 if (!SuccBB)
755 return false;
756
Eric Christopher96e72c62015-01-29 23:27:36 +0000757 RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000758 bool HasMultipleSuccs = false;
759 BB2BrMap BrMap;
Benjamin Kramerd2da7202014-04-21 09:34:48 +0000760 std::unique_ptr<InspectMemInstr> IM;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000761 Iter Filler;
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000762 auto *Fn = MBB.getParent();
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000763
764 // Iterate over SuccBB's predecessor list.
765 for (MachineBasicBlock::pred_iterator PI = SuccBB->pred_begin(),
766 PE = SuccBB->pred_end(); PI != PE; ++PI)
767 if (!examinePred(**PI, *SuccBB, RegDU, HasMultipleSuccs, BrMap))
768 return false;
769
770 // Do not allow moving instructions which have unallocatable register operands
771 // across basic block boundaries.
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000772 RegDU.setUnallocatableRegs(*Fn);
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000773
774 // Only allow moving loads from stack or constants if any of the SuccBB's
775 // predecessors have multiple successors.
776 if (HasMultipleSuccs) {
777 IM.reset(new LoadFromStackOrConst());
778 } else {
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000779 const MachineFrameInfo *MFI = Fn->getFrameInfo();
780 IM.reset(new MemDefsUses(Fn->getDataLayout(), MFI));
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000781 }
782
Vasileios Kalintiris87614902015-03-04 12:37:58 +0000783 if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Slot,
784 Filler))
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000785 return false;
786
787 insertDelayFiller(Filler, BrMap);
788 addLiveInRegs(Filler, *SuccBB);
789 Filler->eraseFromParent();
790
791 return true;
792}
793
794MachineBasicBlock *Filler::selectSuccBB(MachineBasicBlock &B) const {
795 if (B.succ_empty())
Craig Topper062a2ba2014-04-25 05:30:21 +0000796 return nullptr;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000797
798 // Select the successor with the larget edge weight.
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000799 auto &Prob = getAnalysis<MachineBranchProbabilityInfo>();
Cong Hou1938f2e2015-11-24 08:51:23 +0000800 MachineBasicBlock *S = *std::max_element(
801 B.succ_begin(), B.succ_end(),
802 [&](const MachineBasicBlock *Dst0, const MachineBasicBlock *Dst1) {
803 return Prob.getEdgeProbability(&B, Dst0) <
804 Prob.getEdgeProbability(&B, Dst1);
805 });
Reid Kleckner0e288232015-08-27 23:27:47 +0000806 return S->isEHPad() ? nullptr : S;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000807}
808
809std::pair<MipsInstrInfo::BranchType, MachineInstr *>
810Filler::getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const {
Eric Christopher6b6db772015-02-02 23:03:43 +0000811 const MipsInstrInfo *TII =
812 MBB.getParent()->getSubtarget<MipsSubtarget>().getInstrInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +0000813 MachineBasicBlock *TrueBB = nullptr, *FalseBB = nullptr;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000814 SmallVector<MachineInstr*, 2> BranchInstrs;
815 SmallVector<MachineOperand, 2> Cond;
816
817 MipsInstrInfo::BranchType R =
818 TII->AnalyzeBranch(MBB, TrueBB, FalseBB, Cond, false, BranchInstrs);
819
820 if ((R == MipsInstrInfo::BT_None) || (R == MipsInstrInfo::BT_NoBranch))
Craig Topper062a2ba2014-04-25 05:30:21 +0000821 return std::make_pair(R, nullptr);
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000822
823 if (R != MipsInstrInfo::BT_CondUncond) {
824 if (!hasUnoccupiedSlot(BranchInstrs[0]))
Craig Topper062a2ba2014-04-25 05:30:21 +0000825 return std::make_pair(MipsInstrInfo::BT_None, nullptr);
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000826
827 assert(((R != MipsInstrInfo::BT_Uncond) || (TrueBB == &Dst)));
828
829 return std::make_pair(R, BranchInstrs[0]);
830 }
831
832 assert((TrueBB == &Dst) || (FalseBB == &Dst));
833
834 // Examine the conditional branch. See if its slot is occupied.
835 if (hasUnoccupiedSlot(BranchInstrs[0]))
836 return std::make_pair(MipsInstrInfo::BT_Cond, BranchInstrs[0]);
837
838 // If that fails, try the unconditional branch.
839 if (hasUnoccupiedSlot(BranchInstrs[1]) && (FalseBB == &Dst))
840 return std::make_pair(MipsInstrInfo::BT_Uncond, BranchInstrs[1]);
841
Craig Topper062a2ba2014-04-25 05:30:21 +0000842 return std::make_pair(MipsInstrInfo::BT_None, nullptr);
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000843}
844
845bool Filler::examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
846 RegDefsUses &RegDU, bool &HasMultipleSuccs,
847 BB2BrMap &BrMap) const {
848 std::pair<MipsInstrInfo::BranchType, MachineInstr *> P =
849 getBranch(Pred, Succ);
850
851 // Return if either getBranch wasn't able to analyze the branches or there
852 // were no branches with unoccupied slots.
853 if (P.first == MipsInstrInfo::BT_None)
854 return false;
855
856 if ((P.first != MipsInstrInfo::BT_Uncond) &&
857 (P.first != MipsInstrInfo::BT_NoBranch)) {
858 HasMultipleSuccs = true;
859 RegDU.addLiveOut(Pred, Succ);
860 }
861
862 BrMap[&Pred] = P.second;
863 return true;
864}
865
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000866bool Filler::delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000867 InspectMemInstr &IM) const {
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000868 assert(!Candidate.isKill() &&
869 "KILL instructions should have been eliminated at this point.");
870
871 bool HasHazard = Candidate.isImplicitDef();
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000872
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000873 HasHazard |= IM.hasHazard(Candidate);
Akira Hatanaka979899e2013-02-26 01:30:05 +0000874 HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands());
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000875
Akira Hatanaka06bd1382013-02-14 23:40:57 +0000876 return HasHazard;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000877}
878
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000879bool Filler::terminateSearch(const MachineInstr &Candidate) const {
880 return (Candidate.isTerminator() || Candidate.isCall() ||
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000881 Candidate.isPosition() || Candidate.isInlineAsm() ||
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000882 Candidate.hasUnmodeledSideEffects());
883}