2013-02-14 23:20:15 +00:00
|
|
|
//===-- MipsDelaySlotFiller.cpp - Mips Delay Slot Filler ------------------===//
|
2007-08-18 01:50:47 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-18 01:50:47 +00:00
|
|
|
//
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-08-18 01:50:47 +00:00
|
|
|
//
|
2013-02-14 23:20:15 +00:00
|
|
|
// Simple pass to fill delay slots with useful instructions.
|
2007-08-18 01:50:47 +00:00
|
|
|
//
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-08-18 01:50:47 +00:00
|
|
|
|
2014-03-10 20:34:23 +00:00
|
|
|
#include "MCTargetDesc/MipsMCNaCl.h"
|
2007-08-18 01:50:47 +00:00
|
|
|
#include "Mips.h"
|
2013-03-01 02:03:51 +00:00
|
|
|
#include "MipsInstrInfo.h"
|
2007-08-18 01:50:47 +00:00
|
|
|
#include "MipsTargetMachine.h"
|
2013-02-14 23:40:57 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2013-03-01 00:16:31 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-03-01 00:16:31 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2013-03-01 02:03:51 +00:00
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
2007-08-18 01:50:47 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
[mips][mips64r6] Replace m[tf]hi, m[tf]lo, mult, multu, dmult, dmultu, div, ddiv, divu, ddivu for MIPS32r6/MIPS64.
Summary:
The accumulator-based (HI/LO) multiplies and divides from earlier ISA's have
been removed and replaced with GPR-based equivalents. For example:
div $1, $2
mflo $3
is now:
div $3, $1, $2
This patch disables the accumulator-based multiplies and divides for
MIPS32r6/MIPS64r6 and uses the GPR-based equivalents instead.
Renamed expandPseudoDiv to insertDivByZeroTrap to better describe the
behaviour of the function.
MipsDelaySlotFiller now invalidates the liveness information when moving
instructions to the delay slot. Without this, divrem.ll will abort since
%GP ends up used before it is defined.
Reviewers: vmedic, zoran.jovanovic, jkolek
Reviewed By: jkolek
Differential Revision: http://reviews.llvm.org/D3896
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210760 91177308-0d34-0410-b5e6-96231b3b80d8
2014-06-12 10:44:10 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-03-01 00:16:31 +00:00
|
|
|
#include "llvm/CodeGen/PseudoSourceValue.h"
|
2011-09-29 23:52:13 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2007-08-18 01:50:47 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2011-09-29 23:52:13 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2007-08-18 01:50:47 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:41:26 +00:00
|
|
|
#define DEBUG_TYPE "delay-slot-filler"
|
|
|
|
|
2007-08-18 01:50:47 +00:00
|
|
|
STATISTIC(FilledSlots, "Number of delay slots filled");
|
2011-10-05 01:19:13 +00:00
|
|
|
STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
|
2011-10-05 02:22:49 +00:00
|
|
|
" are not NOP.");
|
2007-08-18 01:50:47 +00:00
|
|
|
|
2012-08-22 02:51:28 +00:00
|
|
|
static cl::opt<bool> DisableDelaySlotFiller(
|
|
|
|
"disable-mips-delay-filler",
|
2011-09-29 23:52:13 +00:00
|
|
|
cl::init(false),
|
2013-02-14 23:20:15 +00:00
|
|
|
cl::desc("Fill all delay slots with NOPs."),
|
2011-09-29 23:52:13 +00:00
|
|
|
cl::Hidden);
|
|
|
|
|
2013-03-01 00:50:52 +00:00
|
|
|
static cl::opt<bool> DisableForwardSearch(
|
|
|
|
"disable-mips-df-forward-search",
|
|
|
|
cl::init(true),
|
|
|
|
cl::desc("Disallow MIPS delay filler to search forward."),
|
|
|
|
cl::Hidden);
|
|
|
|
|
2013-03-01 01:02:36 +00:00
|
|
|
static cl::opt<bool> DisableSuccBBSearch(
|
|
|
|
"disable-mips-df-succbb-search",
|
|
|
|
cl::init(true),
|
|
|
|
cl::desc("Disallow MIPS delay filler to search successor basic blocks."),
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<bool> DisableBackwardSearch(
|
|
|
|
"disable-mips-df-backward-search",
|
|
|
|
cl::init(false),
|
|
|
|
cl::desc("Disallow MIPS delay filler to search backward."),
|
|
|
|
cl::Hidden);
|
|
|
|
|
2007-08-18 01:50:47 +00:00
|
|
|
namespace {
|
2013-03-01 02:03:51 +00:00
|
|
|
typedef MachineBasicBlock::iterator Iter;
|
|
|
|
typedef MachineBasicBlock::reverse_iterator ReverseIter;
|
|
|
|
typedef SmallDenseMap<MachineBasicBlock*, MachineInstr*, 2> BB2BrMap;
|
|
|
|
|
2013-02-26 01:30:05 +00:00
|
|
|
class RegDefsUses {
|
|
|
|
public:
|
2015-01-29 23:27:36 +00:00
|
|
|
RegDefsUses(const TargetRegisterInfo &TRI);
|
2013-02-26 01:30:05 +00:00
|
|
|
void init(const MachineInstr &MI);
|
2013-03-01 00:50:52 +00:00
|
|
|
|
|
|
|
/// This function sets all caller-saved registers in Defs.
|
|
|
|
void setCallerSaved(const MachineInstr &MI);
|
|
|
|
|
2013-03-01 02:03:51 +00:00
|
|
|
/// This function sets all unallocatable registers in Defs.
|
|
|
|
void setUnallocatableRegs(const MachineFunction &MF);
|
|
|
|
|
|
|
|
/// Set bits in Uses corresponding to MBB's live-out registers except for
|
|
|
|
/// the registers that are live-in to SuccBB.
|
|
|
|
void addLiveOut(const MachineBasicBlock &MBB,
|
|
|
|
const MachineBasicBlock &SuccBB);
|
|
|
|
|
2013-02-26 01:30:05 +00:00
|
|
|
bool update(const MachineInstr &MI, unsigned Begin, unsigned End);
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, unsigned Reg,
|
|
|
|
bool IsDef) const;
|
|
|
|
|
|
|
|
/// Returns true if Reg or its alias is in RegSet.
|
|
|
|
bool isRegInSet(const BitVector &RegSet, unsigned Reg) const;
|
|
|
|
|
|
|
|
const TargetRegisterInfo &TRI;
|
|
|
|
BitVector Defs, Uses;
|
|
|
|
};
|
|
|
|
|
2013-03-01 00:50:52 +00:00
|
|
|
/// Base class for inspecting loads and stores.
|
|
|
|
class InspectMemInstr {
|
|
|
|
public:
|
2013-03-01 02:03:51 +00:00
|
|
|
InspectMemInstr(bool ForbidMemInstr_)
|
|
|
|
: OrigSeenLoad(false), OrigSeenStore(false), SeenLoad(false),
|
|
|
|
SeenStore(false), ForbidMemInstr(ForbidMemInstr_) {}
|
|
|
|
|
|
|
|
/// Return true if MI cannot be moved to delay slot.
|
|
|
|
bool hasHazard(const MachineInstr &MI);
|
|
|
|
|
2013-03-01 00:50:52 +00:00
|
|
|
virtual ~InspectMemInstr() {}
|
2013-03-01 02:03:51 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Flags indicating whether loads or stores have been seen.
|
|
|
|
bool OrigSeenLoad, OrigSeenStore, SeenLoad, SeenStore;
|
|
|
|
|
|
|
|
/// Memory instructions are not allowed to move to delay slot if this flag
|
|
|
|
/// is true.
|
|
|
|
bool ForbidMemInstr;
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool hasHazard_(const MachineInstr &MI) = 0;
|
2013-03-01 00:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// This subclass rejects any memory instructions.
|
|
|
|
class NoMemInstr : public InspectMemInstr {
|
|
|
|
public:
|
2013-03-01 02:03:51 +00:00
|
|
|
NoMemInstr() : InspectMemInstr(true) {}
|
|
|
|
private:
|
2014-04-29 07:58:02 +00:00
|
|
|
bool hasHazard_(const MachineInstr &MI) override { return true; }
|
2013-03-01 02:03:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// This subclass accepts loads from stacks and constant loads.
|
|
|
|
class LoadFromStackOrConst : public InspectMemInstr {
|
|
|
|
public:
|
|
|
|
LoadFromStackOrConst() : InspectMemInstr(false) {}
|
|
|
|
private:
|
2014-04-29 07:58:02 +00:00
|
|
|
bool hasHazard_(const MachineInstr &MI) override;
|
2013-03-01 00:50:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// This subclass uses memory dependence information to determine whether a
|
|
|
|
/// memory instruction can be moved to a delay slot.
|
|
|
|
class MemDefsUses : public InspectMemInstr {
|
2013-03-01 00:16:31 +00:00
|
|
|
public:
|
2015-03-10 02:37:25 +00:00
|
|
|
MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI);
|
2013-03-01 00:16:31 +00:00
|
|
|
|
|
|
|
private:
|
2014-04-15 07:22:52 +00:00
|
|
|
typedef PointerUnion<const Value *, const PseudoSourceValue *> ValueType;
|
|
|
|
|
2014-04-29 07:58:02 +00:00
|
|
|
bool hasHazard_(const MachineInstr &MI) override;
|
2013-03-01 02:03:51 +00:00
|
|
|
|
2013-03-01 00:16:31 +00:00
|
|
|
/// Update Defs and Uses. Return true if there exist dependences that
|
2013-03-01 02:17:02 +00:00
|
|
|
/// disqualify the delay slot candidate between V and values in Uses and
|
|
|
|
/// Defs.
|
2014-04-15 07:22:52 +00:00
|
|
|
bool updateDefsUses(ValueType V, bool MayStore);
|
2013-03-01 00:16:31 +00:00
|
|
|
|
|
|
|
/// Get the list of underlying objects of MI's memory operand.
|
|
|
|
bool getUnderlyingObjects(const MachineInstr &MI,
|
2014-04-15 07:22:52 +00:00
|
|
|
SmallVectorImpl<ValueType> &Objects) const;
|
2013-03-01 00:16:31 +00:00
|
|
|
|
|
|
|
const MachineFrameInfo *MFI;
|
2014-04-15 07:22:52 +00:00
|
|
|
SmallPtrSet<ValueType, 4> Uses, Defs;
|
2015-03-10 02:37:25 +00:00
|
|
|
const DataLayout &DL;
|
2013-03-01 00:16:31 +00:00
|
|
|
|
|
|
|
/// Flags indicating whether loads or stores with no underlying objects have
|
|
|
|
/// been seen.
|
|
|
|
bool SeenNoObjLoad, SeenNoObjStore;
|
|
|
|
};
|
|
|
|
|
2013-02-07 21:32:32 +00:00
|
|
|
class Filler : public MachineFunctionPass {
|
|
|
|
public:
|
2010-12-09 17:31:11 +00:00
|
|
|
Filler(TargetMachine &tm)
|
2013-06-07 07:04:14 +00:00
|
|
|
: MachineFunctionPass(ID), TM(tm) { }
|
2007-08-18 01:50:47 +00:00
|
|
|
|
2014-04-29 07:58:02 +00:00
|
|
|
const char *getPassName() const override {
|
2007-08-18 01:50:47 +00:00
|
|
|
return "Mips Delay Slot Filler";
|
|
|
|
}
|
|
|
|
|
2014-04-29 07:58:02 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override {
|
2007-08-18 01:50:47 +00:00
|
|
|
bool Changed = false;
|
|
|
|
for (MachineFunction::iterator FI = F.begin(), FE = F.end();
|
|
|
|
FI != FE; ++FI)
|
|
|
|
Changed |= runOnMachineBasicBlock(*FI);
|
[mips][mips64r6] Replace m[tf]hi, m[tf]lo, mult, multu, dmult, dmultu, div, ddiv, divu, ddivu for MIPS32r6/MIPS64.
Summary:
The accumulator-based (HI/LO) multiplies and divides from earlier ISA's have
been removed and replaced with GPR-based equivalents. For example:
div $1, $2
mflo $3
is now:
div $3, $1, $2
This patch disables the accumulator-based multiplies and divides for
MIPS32r6/MIPS64r6 and uses the GPR-based equivalents instead.
Renamed expandPseudoDiv to insertDivByZeroTrap to better describe the
behaviour of the function.
MipsDelaySlotFiller now invalidates the liveness information when moving
instructions to the delay slot. Without this, divrem.ll will abort since
%GP ends up used before it is defined.
Reviewers: vmedic, zoran.jovanovic, jkolek
Reviewed By: jkolek
Differential Revision: http://reviews.llvm.org/D3896
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210760 91177308-0d34-0410-b5e6-96231b3b80d8
2014-06-12 10:44:10 +00:00
|
|
|
|
|
|
|
// This pass invalidates liveness information when it reorders
|
|
|
|
// instructions to fill delay slot. Without this, -verify-machineinstrs
|
|
|
|
// will fail.
|
|
|
|
if (Changed)
|
|
|
|
F.getRegInfo().invalidateLiveness();
|
|
|
|
|
2007-08-18 01:50:47 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2014-04-29 07:58:02 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2013-03-01 02:03:51 +00:00
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2013-02-07 21:32:32 +00:00
|
|
|
|
2013-03-01 02:03:51 +00:00
|
|
|
private:
|
2013-02-07 21:32:32 +00:00
|
|
|
bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
|
|
|
|
|
2014-11-21 22:04:35 +00:00
|
|
|
Iter replaceWithCompactBranch(MachineBasicBlock &MBB,
|
|
|
|
Iter Branch, DebugLoc DL);
|
|
|
|
|
2015-02-13 17:51:27 +00:00
|
|
|
Iter replaceWithCompactJump(MachineBasicBlock &MBB,
|
|
|
|
Iter Jump, DebugLoc DL);
|
|
|
|
|
2013-02-14 23:40:57 +00:00
|
|
|
/// This function checks if it is valid to move Candidate to the delay slot
|
2013-03-01 00:16:31 +00:00
|
|
|
/// and returns true if it isn't. It also updates memory and register
|
|
|
|
/// dependence information.
|
|
|
|
bool delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
|
2013-03-01 00:50:52 +00:00
|
|
|
InspectMemInstr &IM) const;
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2013-03-01 00:26:14 +00:00
|
|
|
/// This function searches range [Begin, End) for an instruction that can be
|
|
|
|
/// moved to the delay slot. Returns true on success.
|
|
|
|
template<typename IterTy>
|
|
|
|
bool searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
|
2015-03-04 12:37:58 +00:00
|
|
|
RegDefsUses &RegDU, InspectMemInstr &IM, Iter Slot,
|
|
|
|
IterTy &Filler) const;
|
2013-03-01 00:26:14 +00:00
|
|
|
|
2013-03-01 00:50:52 +00:00
|
|
|
/// This function searches in the backward direction for an instruction that
|
|
|
|
/// can be moved to the delay slot. Returns true on success.
|
|
|
|
bool searchBackward(MachineBasicBlock &MBB, Iter Slot) const;
|
|
|
|
|
|
|
|
/// This function searches MBB in the forward direction for an instruction
|
|
|
|
/// that can be moved to the delay slot. Returns true on success.
|
|
|
|
bool searchForward(MachineBasicBlock &MBB, Iter Slot) const;
|
2013-02-14 23:11:24 +00:00
|
|
|
|
2013-03-25 20:11:16 +00:00
|
|
|
/// This function searches one of MBB's successor blocks for an instruction
|
|
|
|
/// that can be moved to the delay slot and inserts clones of the
|
|
|
|
/// instruction into the successor's predecessor blocks.
|
2013-03-01 02:03:51 +00:00
|
|
|
bool searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const;
|
|
|
|
|
2013-03-01 02:17:02 +00:00
|
|
|
/// Pick a successor block of MBB. Return NULL if MBB doesn't have a
|
|
|
|
/// successor block that is not a landing pad.
|
2013-03-01 02:03:51 +00:00
|
|
|
MachineBasicBlock *selectSuccBB(MachineBasicBlock &B) const;
|
|
|
|
|
|
|
|
/// This function analyzes MBB and returns an instruction with an unoccupied
|
|
|
|
/// slot that branches to Dst.
|
|
|
|
std::pair<MipsInstrInfo::BranchType, MachineInstr *>
|
|
|
|
getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const;
|
|
|
|
|
|
|
|
/// Examine Pred and see if it is possible to insert an instruction into
|
|
|
|
/// one of its branches delay slot or its end.
|
|
|
|
bool examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
|
|
|
|
RegDefsUses &RegDU, bool &HasMultipleSuccs,
|
|
|
|
BB2BrMap &BrMap) const;
|
|
|
|
|
2013-02-14 23:11:24 +00:00
|
|
|
bool terminateSearch(const MachineInstr &Candidate) const;
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2013-02-07 21:32:32 +00:00
|
|
|
TargetMachine &TM;
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2013-02-07 21:32:32 +00:00
|
|
|
static char ID;
|
2007-08-18 01:50:47 +00:00
|
|
|
};
|
|
|
|
char Filler::ID = 0;
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2013-03-01 02:03:51 +00:00
|
|
|
static bool hasUnoccupiedSlot(const MachineInstr *MI) {
|
|
|
|
return MI->hasDelaySlot() && !MI->isBundledWithSucc();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function inserts clones of Filler into predecessor blocks.
|
|
|
|
static void insertDelayFiller(Iter Filler, const BB2BrMap &BrMap) {
|
|
|
|
MachineFunction *MF = Filler->getParent()->getParent();
|
|
|
|
|
|
|
|
for (BB2BrMap::const_iterator I = BrMap.begin(); I != BrMap.end(); ++I) {
|
|
|
|
if (I->second) {
|
|
|
|
MIBundleBuilder(I->second).append(MF->CloneMachineInstr(&*Filler));
|
|
|
|
++UsefulSlots;
|
|
|
|
} else {
|
|
|
|
I->first->insert(I->first->end(), MF->CloneMachineInstr(&*Filler));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function adds registers Filler defines to MBB's live-in register list.
|
|
|
|
static void addLiveInRegs(Iter Filler, MachineBasicBlock &MBB) {
|
|
|
|
for (unsigned I = 0, E = Filler->getNumOperands(); I != E; ++I) {
|
|
|
|
const MachineOperand &MO = Filler->getOperand(I);
|
|
|
|
unsigned R;
|
|
|
|
|
|
|
|
if (!MO.isReg() || !MO.isDef() || !(R = MO.getReg()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
const MachineFunction &MF = *MBB.getParent();
|
2015-01-29 23:27:36 +00:00
|
|
|
assert(MF.getSubtarget().getRegisterInfo()->getAllocatableSet(MF).test(R) &&
|
2013-03-01 02:03:51 +00:00
|
|
|
"Shouldn't move an instruction with unallocatable registers across "
|
|
|
|
"basic block boundaries.");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!MBB.isLiveIn(R))
|
|
|
|
MBB.addLiveIn(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 23:27:36 +00:00
|
|
|
RegDefsUses::RegDefsUses(const TargetRegisterInfo &TRI)
|
|
|
|
: TRI(TRI), Defs(TRI.getNumRegs(), false), Uses(TRI.getNumRegs(), false) {}
|
2013-02-26 01:30:05 +00:00
|
|
|
|
|
|
|
void RegDefsUses::init(const MachineInstr &MI) {
|
|
|
|
// Add all register operands which are explicit and non-variadic.
|
|
|
|
update(MI, 0, MI.getDesc().getNumOperands());
|
|
|
|
|
|
|
|
// If MI is a call, add RA to Defs to prevent users of RA from going into
|
|
|
|
// delay slot.
|
|
|
|
if (MI.isCall())
|
|
|
|
Defs.set(Mips::RA);
|
|
|
|
|
|
|
|
// Add all implicit register operands of branch instructions except
|
|
|
|
// register AT.
|
|
|
|
if (MI.isBranch()) {
|
|
|
|
update(MI, MI.getDesc().getNumOperands(), MI.getNumOperands());
|
|
|
|
Defs.reset(Mips::AT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 00:50:52 +00:00
|
|
|
void RegDefsUses::setCallerSaved(const MachineInstr &MI) {
|
|
|
|
assert(MI.isCall());
|
|
|
|
|
|
|
|
// If MI is a call, add all caller-saved registers to Defs.
|
|
|
|
BitVector CallerSavedRegs(TRI.getNumRegs(), true);
|
|
|
|
|
|
|
|
CallerSavedRegs.reset(Mips::ZERO);
|
|
|
|
CallerSavedRegs.reset(Mips::ZERO_64);
|
|
|
|
|
|
|
|
for (const MCPhysReg *R = TRI.getCalleeSavedRegs(); *R; ++R)
|
|
|
|
for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI)
|
|
|
|
CallerSavedRegs.reset(*AI);
|
|
|
|
|
|
|
|
Defs |= CallerSavedRegs;
|
|
|
|
}
|
|
|
|
|
2013-03-01 02:03:51 +00:00
|
|
|
void RegDefsUses::setUnallocatableRegs(const MachineFunction &MF) {
|
|
|
|
BitVector AllocSet = TRI.getAllocatableSet(MF);
|
|
|
|
|
|
|
|
for (int R = AllocSet.find_first(); R != -1; R = AllocSet.find_next(R))
|
|
|
|
for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI)
|
|
|
|
AllocSet.set(*AI);
|
|
|
|
|
|
|
|
AllocSet.set(Mips::ZERO);
|
|
|
|
AllocSet.set(Mips::ZERO_64);
|
|
|
|
|
|
|
|
Defs |= AllocSet.flip();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegDefsUses::addLiveOut(const MachineBasicBlock &MBB,
|
|
|
|
const MachineBasicBlock &SuccBB) {
|
|
|
|
for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
|
|
|
|
SE = MBB.succ_end(); SI != SE; ++SI)
|
|
|
|
if (*SI != &SuccBB)
|
|
|
|
for (MachineBasicBlock::livein_iterator LI = (*SI)->livein_begin(),
|
|
|
|
LE = (*SI)->livein_end(); LI != LE; ++LI)
|
|
|
|
Uses.set(*LI);
|
|
|
|
}
|
|
|
|
|
2013-02-26 01:30:05 +00:00
|
|
|
bool RegDefsUses::update(const MachineInstr &MI, unsigned Begin, unsigned End) {
|
|
|
|
BitVector NewDefs(TRI.getNumRegs()), NewUses(TRI.getNumRegs());
|
|
|
|
bool HasHazard = false;
|
|
|
|
|
|
|
|
for (unsigned I = Begin; I != End; ++I) {
|
|
|
|
const MachineOperand &MO = MI.getOperand(I);
|
|
|
|
|
|
|
|
if (MO.isReg() && MO.getReg())
|
|
|
|
HasHazard |= checkRegDefsUses(NewDefs, NewUses, MO.getReg(), MO.isDef());
|
|
|
|
}
|
|
|
|
|
|
|
|
Defs |= NewDefs;
|
|
|
|
Uses |= NewUses;
|
|
|
|
|
|
|
|
return HasHazard;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegDefsUses::checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses,
|
|
|
|
unsigned Reg, bool IsDef) const {
|
|
|
|
if (IsDef) {
|
|
|
|
NewDefs.set(Reg);
|
|
|
|
// check whether Reg has already been defined or used.
|
|
|
|
return (isRegInSet(Defs, Reg) || isRegInSet(Uses, Reg));
|
|
|
|
}
|
|
|
|
|
|
|
|
NewUses.set(Reg);
|
|
|
|
// check whether Reg has already been defined.
|
|
|
|
return isRegInSet(Defs, Reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegDefsUses::isRegInSet(const BitVector &RegSet, unsigned Reg) const {
|
|
|
|
// Check Reg and all aliased Registers.
|
|
|
|
for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
|
|
|
|
if (RegSet.test(*AI))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-01 02:03:51 +00:00
|
|
|
bool InspectMemInstr::hasHazard(const MachineInstr &MI) {
|
2013-03-01 00:16:31 +00:00
|
|
|
if (!MI.mayStore() && !MI.mayLoad())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ForbidMemInstr)
|
|
|
|
return true;
|
|
|
|
|
2013-03-01 02:03:51 +00:00
|
|
|
OrigSeenLoad = SeenLoad;
|
|
|
|
OrigSeenStore = SeenStore;
|
2013-03-01 00:16:31 +00:00
|
|
|
SeenLoad |= MI.mayLoad();
|
|
|
|
SeenStore |= MI.mayStore();
|
|
|
|
|
|
|
|
// If MI is an ordered or volatile memory reference, disallow moving
|
|
|
|
// subsequent loads and stores to delay slot.
|
|
|
|
if (MI.hasOrderedMemoryRef() && (OrigSeenLoad || OrigSeenStore)) {
|
|
|
|
ForbidMemInstr = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-01 02:03:51 +00:00
|
|
|
return hasHazard_(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) {
|
|
|
|
if (MI.mayStore())
|
|
|
|
return true;
|
|
|
|
|
2014-04-15 07:22:52 +00:00
|
|
|
if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getPseudoValue())
|
2013-03-01 02:03:51 +00:00
|
|
|
return true;
|
|
|
|
|
2014-04-15 07:22:52 +00:00
|
|
|
if (const PseudoSourceValue *PSV =
|
|
|
|
(*MI.memoperands_begin())->getPseudoValue()) {
|
|
|
|
if (isa<FixedStackPseudoSourceValue>(PSV))
|
|
|
|
return false;
|
2014-04-25 05:30:21 +00:00
|
|
|
return !PSV->isConstant(nullptr) && PSV != PseudoSourceValue::getStack();
|
2014-04-15 07:22:52 +00:00
|
|
|
}
|
2013-03-01 02:03:51 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-10 02:37:25 +00:00
|
|
|
MemDefsUses::MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI_)
|
|
|
|
: InspectMemInstr(false), MFI(MFI_), DL(DL), SeenNoObjLoad(false),
|
|
|
|
SeenNoObjStore(false) {}
|
2013-03-01 02:03:51 +00:00
|
|
|
|
|
|
|
bool MemDefsUses::hasHazard_(const MachineInstr &MI) {
|
2013-03-01 00:16:31 +00:00
|
|
|
bool HasHazard = false;
|
2014-04-15 07:22:52 +00:00
|
|
|
SmallVector<ValueType, 4> Objs;
|
2013-03-01 00:16:31 +00:00
|
|
|
|
|
|
|
// Check underlying object list.
|
|
|
|
if (getUnderlyingObjects(MI, Objs)) {
|
2014-04-15 07:22:52 +00:00
|
|
|
for (SmallVectorImpl<ValueType>::const_iterator I = Objs.begin();
|
2013-03-01 00:16:31 +00:00
|
|
|
I != Objs.end(); ++I)
|
|
|
|
HasHazard |= updateDefsUses(*I, MI.mayStore());
|
|
|
|
|
|
|
|
return HasHazard;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No underlying objects found.
|
|
|
|
HasHazard = MI.mayStore() && (OrigSeenLoad || OrigSeenStore);
|
|
|
|
HasHazard |= MI.mayLoad() || OrigSeenStore;
|
|
|
|
|
|
|
|
SeenNoObjLoad |= MI.mayLoad();
|
|
|
|
SeenNoObjStore |= MI.mayStore();
|
|
|
|
|
|
|
|
return HasHazard;
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:22:52 +00:00
|
|
|
bool MemDefsUses::updateDefsUses(ValueType V, bool MayStore) {
|
2013-03-01 00:16:31 +00:00
|
|
|
if (MayStore)
|
2014-11-19 07:49:26 +00:00
|
|
|
return !Defs.insert(V).second || Uses.count(V) || SeenNoObjStore ||
|
|
|
|
SeenNoObjLoad;
|
2013-03-01 00:16:31 +00:00
|
|
|
|
|
|
|
Uses.insert(V);
|
|
|
|
return Defs.count(V) || SeenNoObjStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemDefsUses::
|
|
|
|
getUnderlyingObjects(const MachineInstr &MI,
|
2014-04-15 07:22:52 +00:00
|
|
|
SmallVectorImpl<ValueType> &Objects) const {
|
|
|
|
if (!MI.hasOneMemOperand() ||
|
|
|
|
(!(*MI.memoperands_begin())->getValue() &&
|
|
|
|
!(*MI.memoperands_begin())->getPseudoValue()))
|
2013-03-01 00:16:31 +00:00
|
|
|
return false;
|
|
|
|
|
2014-04-15 07:22:52 +00:00
|
|
|
if (const PseudoSourceValue *PSV =
|
|
|
|
(*MI.memoperands_begin())->getPseudoValue()) {
|
|
|
|
if (!PSV->isAliased(MFI))
|
|
|
|
return false;
|
|
|
|
Objects.push_back(PSV);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-01 00:16:31 +00:00
|
|
|
const Value *V = (*MI.memoperands_begin())->getValue();
|
|
|
|
|
|
|
|
SmallVector<Value *, 4> Objs;
|
2015-03-10 02:37:25 +00:00
|
|
|
GetUnderlyingObjects(const_cast<Value *>(V), Objs, DL);
|
2013-03-01 00:16:31 +00:00
|
|
|
|
2013-07-03 15:07:05 +00:00
|
|
|
for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), E = Objs.end();
|
2013-03-01 00:16:31 +00:00
|
|
|
I != E; ++I) {
|
2014-04-15 07:22:52 +00:00
|
|
|
if (!isIdentifiedObject(V))
|
2013-03-01 00:16:31 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Objects.push_back(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:04:35 +00:00
|
|
|
// Replace Branch with the compact branch instruction.
|
|
|
|
Iter Filler::replaceWithCompactBranch(MachineBasicBlock &MBB,
|
|
|
|
Iter Branch, DebugLoc DL) {
|
2015-02-02 23:03:43 +00:00
|
|
|
const MipsInstrInfo *TII =
|
|
|
|
MBB.getParent()->getSubtarget<MipsSubtarget>().getInstrInfo();
|
2014-11-21 22:04:35 +00:00
|
|
|
|
|
|
|
unsigned NewOpcode =
|
|
|
|
(((unsigned) Branch->getOpcode()) == Mips::BEQ) ? Mips::BEQZC_MM
|
|
|
|
: Mips::BNEZC_MM;
|
|
|
|
|
|
|
|
const MCInstrDesc &NewDesc = TII->get(NewOpcode);
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, Branch, DL, NewDesc);
|
|
|
|
|
|
|
|
MIB.addReg(Branch->getOperand(0).getReg());
|
|
|
|
MIB.addMBB(Branch->getOperand(2).getMBB());
|
|
|
|
|
|
|
|
Iter tmpIter = Branch;
|
|
|
|
Branch = std::prev(Branch);
|
|
|
|
MBB.erase(tmpIter);
|
|
|
|
|
|
|
|
return Branch;
|
|
|
|
}
|
|
|
|
|
2015-02-13 17:51:27 +00:00
|
|
|
// Replace Jumps with the compact jump instruction.
|
|
|
|
Iter Filler::replaceWithCompactJump(MachineBasicBlock &MBB,
|
|
|
|
Iter Jump, DebugLoc DL) {
|
|
|
|
const MipsInstrInfo *TII =
|
|
|
|
MBB.getParent()->getSubtarget<MipsSubtarget>().getInstrInfo();
|
|
|
|
|
|
|
|
const MCInstrDesc &NewDesc = TII->get(Mips::JRC16_MM);
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, Jump, DL, NewDesc);
|
|
|
|
|
|
|
|
MIB.addReg(Jump->getOperand(0).getReg());
|
|
|
|
|
|
|
|
Iter tmpIter = Jump;
|
|
|
|
Jump = std::prev(Jump);
|
|
|
|
MBB.erase(tmpIter);
|
|
|
|
|
|
|
|
return Jump;
|
|
|
|
}
|
|
|
|
|
2014-11-25 10:50:00 +00:00
|
|
|
// For given opcode returns opcode of corresponding instruction with short
|
|
|
|
// delay slot.
|
|
|
|
static int getEquivalentCallShort(int Opcode) {
|
|
|
|
switch (Opcode) {
|
|
|
|
case Mips::BGEZAL:
|
|
|
|
return Mips::BGEZALS_MM;
|
|
|
|
case Mips::BLTZAL:
|
|
|
|
return Mips::BLTZALS_MM;
|
|
|
|
case Mips::JAL:
|
|
|
|
return Mips::JALS_MM;
|
|
|
|
case Mips::JALR:
|
|
|
|
return Mips::JALRS_MM;
|
|
|
|
case Mips::JALR16_MM:
|
|
|
|
return Mips::JALRS16_MM;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected call instruction for microMIPS.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-18 01:50:47 +00:00
|
|
|
/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
|
2011-09-29 23:52:13 +00:00
|
|
|
/// We assume there is only one delay slot per delayed instruction.
|
2013-02-14 23:20:15 +00:00
|
|
|
bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
|
2007-08-18 01:50:47 +00:00
|
|
|
bool Changed = false;
|
2015-02-02 23:03:43 +00:00
|
|
|
const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
|
2015-01-29 23:27:36 +00:00
|
|
|
bool InMicroMipsMode = STI.inMicroMipsMode();
|
|
|
|
const MipsInstrInfo *TII = STI.getInstrInfo();
|
2011-10-05 01:30:09 +00:00
|
|
|
|
2013-02-14 23:11:24 +00:00
|
|
|
for (Iter I = MBB.begin(); I != MBB.end(); ++I) {
|
2013-03-01 02:03:51 +00:00
|
|
|
if (!hasUnoccupiedSlot(&*I))
|
2013-02-07 21:32:32 +00:00
|
|
|
continue;
|
|
|
|
|
2014-11-25 10:50:00 +00:00
|
|
|
++FilledSlots;
|
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
// Delay slot filling is disabled at -O0.
|
|
|
|
if (!DisableDelaySlotFiller && (TM.getOptLevel() != CodeGenOpt::None)) {
|
|
|
|
bool Filled = false;
|
|
|
|
|
|
|
|
if (searchBackward(MBB, I)) {
|
|
|
|
Filled = true;
|
|
|
|
} else if (I->isTerminator()) {
|
|
|
|
if (searchSuccBBs(MBB, I)) {
|
|
|
|
Filled = true;
|
2014-11-10 17:27:56 +00:00
|
|
|
}
|
2014-11-25 10:50:00 +00:00
|
|
|
} else if (searchForward(MBB, I)) {
|
|
|
|
Filled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Filled) {
|
|
|
|
// Get instruction with delay slot.
|
|
|
|
MachineBasicBlock::instr_iterator DSI(I);
|
|
|
|
|
|
|
|
if (InMicroMipsMode && TII->GetInstSizeInBytes(std::next(DSI)) == 2 &&
|
|
|
|
DSI->isCall()) {
|
|
|
|
// If instruction in delay slot is 16b change opcode to
|
|
|
|
// corresponding instruction with short delay slot.
|
|
|
|
DSI->setDesc(TII->get(getEquivalentCallShort(DSI->getOpcode())));
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2013-03-01 02:03:51 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2014-11-21 22:04:35 +00:00
|
|
|
// If instruction is BEQ or BNE with one ZERO register, then instead of
|
|
|
|
// adding NOP replace this instruction with the corresponding compact
|
|
|
|
// branch instruction, i.e. BEQZC or BNEZC.
|
|
|
|
unsigned Opcode = I->getOpcode();
|
2015-02-13 17:51:27 +00:00
|
|
|
if (InMicroMipsMode) {
|
|
|
|
switch (Opcode) {
|
|
|
|
case Mips::BEQ:
|
|
|
|
case Mips::BNE:
|
|
|
|
if (((unsigned) I->getOperand(1).getReg()) == Mips::ZERO) {
|
|
|
|
I = replaceWithCompactBranch(MBB, I, I->getDebugLoc());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Mips::JR:
|
|
|
|
case Mips::PseudoReturn:
|
|
|
|
case Mips::PseudoIndirectBranch:
|
|
|
|
// For microMIPS the PseudoReturn and PseudoIndirectBranch are allways
|
|
|
|
// expanded to JR_MM, so they can be replaced with JRC16_MM.
|
|
|
|
I = replaceWithCompactJump(MBB, I, I->getDebugLoc());
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-11-21 22:04:35 +00:00
|
|
|
}
|
2015-02-13 17:51:27 +00:00
|
|
|
// Bundle the NOP to the instruction with the delay slot.
|
|
|
|
BuildMI(MBB, std::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
|
|
|
|
MIBundleBuilder(MBB, I, std::next(I, 2));
|
2013-02-07 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
2007-08-18 01:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
|
|
|
|
/// slots in Mips MachineFunctions
|
|
|
|
FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
|
|
|
|
return new Filler(tm);
|
|
|
|
}
|
|
|
|
|
2013-03-01 00:26:14 +00:00
|
|
|
template<typename IterTy>
|
|
|
|
bool Filler::searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
|
2015-03-04 12:37:58 +00:00
|
|
|
RegDefsUses &RegDU, InspectMemInstr& IM, Iter Slot,
|
|
|
|
IterTy &Filler) const {
|
2013-03-01 00:26:14 +00:00
|
|
|
for (IterTy I = Begin; I != End; ++I) {
|
2011-09-29 23:52:13 +00:00
|
|
|
// skip debug value
|
|
|
|
if (I->isDebugValue())
|
|
|
|
continue;
|
|
|
|
|
2013-02-14 23:11:24 +00:00
|
|
|
if (terminateSearch(*I))
|
2011-09-29 23:52:13 +00:00
|
|
|
break;
|
|
|
|
|
2013-03-01 00:16:31 +00:00
|
|
|
assert((!I->isCall() && !I->isReturn() && !I->isBranch()) &&
|
|
|
|
"Cannot put calls, returns or branches in delay slot.");
|
|
|
|
|
2013-03-01 00:50:52 +00:00
|
|
|
if (delayHasHazard(*I, RegDU, IM))
|
2011-09-29 23:52:13 +00:00
|
|
|
continue;
|
|
|
|
|
2015-02-02 23:03:43 +00:00
|
|
|
const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
|
|
|
|
if (STI.isTargetNaCl()) {
|
2014-03-10 20:34:23 +00:00
|
|
|
// In NaCl, instructions that must be masked are forbidden in delay slots.
|
|
|
|
// We only check for loads, stores and SP changes. Calls, returns and
|
|
|
|
// branches are not checked because non-NaCl targets never put them in
|
|
|
|
// delay slots.
|
|
|
|
unsigned AddrIdx;
|
2014-08-04 21:25:23 +00:00
|
|
|
if ((isBasePlusOffsetMemoryAccess(I->getOpcode(), &AddrIdx) &&
|
|
|
|
baseRegNeedsLoadStoreMask(I->getOperand(AddrIdx).getReg())) ||
|
2015-02-02 23:03:43 +00:00
|
|
|
I->modifiesRegister(Mips::SP, STI.getRegisterInfo()))
|
2014-03-10 20:34:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-02 23:03:43 +00:00
|
|
|
bool InMicroMipsMode = STI.inMicroMipsMode();
|
|
|
|
const MipsInstrInfo *TII = STI.getInstrInfo();
|
2015-01-13 15:59:17 +00:00
|
|
|
unsigned Opcode = (*Slot).getOpcode();
|
|
|
|
if (InMicroMipsMode && TII->GetInstSizeInBytes(&(*I)) == 2 &&
|
|
|
|
(Opcode == Mips::JR || Opcode == Mips::PseudoIndirectBranch ||
|
|
|
|
Opcode == Mips::PseudoReturn))
|
|
|
|
continue;
|
|
|
|
|
2013-03-01 00:26:14 +00:00
|
|
|
Filler = I;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-01 00:50:52 +00:00
|
|
|
bool Filler::searchBackward(MachineBasicBlock &MBB, Iter Slot) const {
|
2013-03-01 02:03:51 +00:00
|
|
|
if (DisableBackwardSearch)
|
|
|
|
return false;
|
|
|
|
|
2015-01-29 23:27:36 +00:00
|
|
|
RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
|
2015-03-10 02:37:25 +00:00
|
|
|
MemDefsUses MemDU(*TM.getDataLayout(), MBB.getParent()->getFrameInfo());
|
2013-03-01 00:50:52 +00:00
|
|
|
ReverseIter Filler;
|
2013-03-01 00:26:14 +00:00
|
|
|
|
|
|
|
RegDU.init(*Slot);
|
|
|
|
|
2015-03-04 12:37:58 +00:00
|
|
|
if (!searchRange(MBB, ReverseIter(Slot), MBB.rend(), RegDU, MemDU, Slot,
|
|
|
|
Filler))
|
2013-10-07 19:33:02 +00:00
|
|
|
return false;
|
2013-03-01 00:50:52 +00:00
|
|
|
|
2014-03-02 12:27:27 +00:00
|
|
|
MBB.splice(std::next(Slot), &MBB, std::next(Filler).base());
|
|
|
|
MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
|
2013-10-07 19:33:02 +00:00
|
|
|
++UsefulSlots;
|
|
|
|
return true;
|
2013-03-01 00:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Filler::searchForward(MachineBasicBlock &MBB, Iter Slot) const {
|
|
|
|
// Can handle only calls.
|
2013-03-01 02:03:51 +00:00
|
|
|
if (DisableForwardSearch || !Slot->isCall())
|
2013-03-01 00:50:52 +00:00
|
|
|
return false;
|
|
|
|
|
2015-01-29 23:27:36 +00:00
|
|
|
RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
|
2013-03-01 00:50:52 +00:00
|
|
|
NoMemInstr NM;
|
|
|
|
Iter Filler;
|
|
|
|
|
|
|
|
RegDU.setCallerSaved(*Slot);
|
|
|
|
|
2015-03-04 12:37:58 +00:00
|
|
|
if (!searchRange(MBB, std::next(Slot), MBB.end(), RegDU, NM, Slot, Filler))
|
2013-10-07 19:33:02 +00:00
|
|
|
return false;
|
2011-10-05 01:23:39 +00:00
|
|
|
|
2014-03-02 12:27:27 +00:00
|
|
|
MBB.splice(std::next(Slot), &MBB, Filler);
|
|
|
|
MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
|
2013-10-07 19:33:02 +00:00
|
|
|
++UsefulSlots;
|
|
|
|
return true;
|
2011-09-29 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
2013-03-01 02:03:51 +00:00
|
|
|
bool Filler::searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const {
|
|
|
|
if (DisableSuccBBSearch)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineBasicBlock *SuccBB = selectSuccBB(MBB);
|
|
|
|
|
|
|
|
if (!SuccBB)
|
|
|
|
return false;
|
|
|
|
|
2015-01-29 23:27:36 +00:00
|
|
|
RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
|
2013-03-01 02:03:51 +00:00
|
|
|
bool HasMultipleSuccs = false;
|
|
|
|
BB2BrMap BrMap;
|
2014-04-21 09:34:48 +00:00
|
|
|
std::unique_ptr<InspectMemInstr> IM;
|
2013-03-01 02:03:51 +00:00
|
|
|
Iter Filler;
|
|
|
|
|
|
|
|
// Iterate over SuccBB's predecessor list.
|
|
|
|
for (MachineBasicBlock::pred_iterator PI = SuccBB->pred_begin(),
|
|
|
|
PE = SuccBB->pred_end(); PI != PE; ++PI)
|
|
|
|
if (!examinePred(**PI, *SuccBB, RegDU, HasMultipleSuccs, BrMap))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Do not allow moving instructions which have unallocatable register operands
|
|
|
|
// across basic block boundaries.
|
|
|
|
RegDU.setUnallocatableRegs(*MBB.getParent());
|
|
|
|
|
|
|
|
// Only allow moving loads from stack or constants if any of the SuccBB's
|
|
|
|
// predecessors have multiple successors.
|
|
|
|
if (HasMultipleSuccs) {
|
|
|
|
IM.reset(new LoadFromStackOrConst());
|
|
|
|
} else {
|
|
|
|
const MachineFrameInfo *MFI = MBB.getParent()->getFrameInfo();
|
2015-03-10 02:37:25 +00:00
|
|
|
IM.reset(new MemDefsUses(*TM.getDataLayout(), MFI));
|
2013-03-01 02:03:51 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 12:37:58 +00:00
|
|
|
if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Slot,
|
|
|
|
Filler))
|
2013-03-01 02:03:51 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
insertDelayFiller(Filler, BrMap);
|
|
|
|
addLiveInRegs(Filler, *SuccBB);
|
|
|
|
Filler->eraseFromParent();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *Filler::selectSuccBB(MachineBasicBlock &B) const {
|
|
|
|
if (B.succ_empty())
|
2014-04-25 05:30:21 +00:00
|
|
|
return nullptr;
|
2013-03-01 02:03:51 +00:00
|
|
|
|
|
|
|
// Select the successor with the larget edge weight.
|
2014-03-01 11:47:00 +00:00
|
|
|
auto &Prob = getAnalysis<MachineBranchProbabilityInfo>();
|
|
|
|
MachineBasicBlock *S = *std::max_element(B.succ_begin(), B.succ_end(),
|
|
|
|
[&](const MachineBasicBlock *Dst0,
|
|
|
|
const MachineBasicBlock *Dst1) {
|
|
|
|
return Prob.getEdgeWeight(&B, Dst0) < Prob.getEdgeWeight(&B, Dst1);
|
|
|
|
});
|
2014-04-25 05:30:21 +00:00
|
|
|
return S->isLandingPad() ? nullptr : S;
|
2013-03-01 02:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<MipsInstrInfo::BranchType, MachineInstr *>
|
|
|
|
Filler::getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const {
|
2015-02-02 23:03:43 +00:00
|
|
|
const MipsInstrInfo *TII =
|
|
|
|
MBB.getParent()->getSubtarget<MipsSubtarget>().getInstrInfo();
|
2014-04-25 05:30:21 +00:00
|
|
|
MachineBasicBlock *TrueBB = nullptr, *FalseBB = nullptr;
|
2013-03-01 02:03:51 +00:00
|
|
|
SmallVector<MachineInstr*, 2> BranchInstrs;
|
|
|
|
SmallVector<MachineOperand, 2> Cond;
|
|
|
|
|
|
|
|
MipsInstrInfo::BranchType R =
|
|
|
|
TII->AnalyzeBranch(MBB, TrueBB, FalseBB, Cond, false, BranchInstrs);
|
|
|
|
|
|
|
|
if ((R == MipsInstrInfo::BT_None) || (R == MipsInstrInfo::BT_NoBranch))
|
2014-04-25 05:30:21 +00:00
|
|
|
return std::make_pair(R, nullptr);
|
2013-03-01 02:03:51 +00:00
|
|
|
|
|
|
|
if (R != MipsInstrInfo::BT_CondUncond) {
|
|
|
|
if (!hasUnoccupiedSlot(BranchInstrs[0]))
|
2014-04-25 05:30:21 +00:00
|
|
|
return std::make_pair(MipsInstrInfo::BT_None, nullptr);
|
2013-03-01 02:03:51 +00:00
|
|
|
|
|
|
|
assert(((R != MipsInstrInfo::BT_Uncond) || (TrueBB == &Dst)));
|
|
|
|
|
|
|
|
return std::make_pair(R, BranchInstrs[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert((TrueBB == &Dst) || (FalseBB == &Dst));
|
|
|
|
|
|
|
|
// Examine the conditional branch. See if its slot is occupied.
|
|
|
|
if (hasUnoccupiedSlot(BranchInstrs[0]))
|
|
|
|
return std::make_pair(MipsInstrInfo::BT_Cond, BranchInstrs[0]);
|
|
|
|
|
|
|
|
// If that fails, try the unconditional branch.
|
|
|
|
if (hasUnoccupiedSlot(BranchInstrs[1]) && (FalseBB == &Dst))
|
|
|
|
return std::make_pair(MipsInstrInfo::BT_Uncond, BranchInstrs[1]);
|
|
|
|
|
2014-04-25 05:30:21 +00:00
|
|
|
return std::make_pair(MipsInstrInfo::BT_None, nullptr);
|
2013-03-01 02:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Filler::examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
|
|
|
|
RegDefsUses &RegDU, bool &HasMultipleSuccs,
|
|
|
|
BB2BrMap &BrMap) const {
|
|
|
|
std::pair<MipsInstrInfo::BranchType, MachineInstr *> P =
|
|
|
|
getBranch(Pred, Succ);
|
|
|
|
|
|
|
|
// Return if either getBranch wasn't able to analyze the branches or there
|
|
|
|
// were no branches with unoccupied slots.
|
|
|
|
if (P.first == MipsInstrInfo::BT_None)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ((P.first != MipsInstrInfo::BT_Uncond) &&
|
|
|
|
(P.first != MipsInstrInfo::BT_NoBranch)) {
|
|
|
|
HasMultipleSuccs = true;
|
|
|
|
RegDU.addLiveOut(Pred, Succ);
|
|
|
|
}
|
|
|
|
|
|
|
|
BrMap[&Pred] = P.second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-01 00:16:31 +00:00
|
|
|
bool Filler::delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
|
2013-03-01 00:50:52 +00:00
|
|
|
InspectMemInstr &IM) const {
|
2013-02-14 23:40:57 +00:00
|
|
|
bool HasHazard = (Candidate.isImplicitDef() || Candidate.isKill());
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2013-03-01 00:50:52 +00:00
|
|
|
HasHazard |= IM.hasHazard(Candidate);
|
2013-02-26 01:30:05 +00:00
|
|
|
HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands());
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2013-02-14 23:40:57 +00:00
|
|
|
return HasHazard;
|
2012-11-16 02:39:34 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 23:11:24 +00:00
|
|
|
bool Filler::terminateSearch(const MachineInstr &Candidate) const {
|
|
|
|
return (Candidate.isTerminator() || Candidate.isCall() ||
|
2014-03-07 06:08:31 +00:00
|
|
|
Candidate.isPosition() || Candidate.isInlineAsm() ||
|
2013-02-14 23:11:24 +00:00
|
|
|
Candidate.hasUnmodeledSideEffects());
|
|
|
|
}
|