2013-02-15 07:20:15 +08:00
|
|
|
//===-- MipsDelaySlotFiller.cpp - Mips Delay Slot Filler ------------------===//
|
2007-08-18 09:50:47 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-18 09:50:47 +08:00
|
|
|
//
|
2011-04-16 05:51:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-08-18 09:50:47 +08:00
|
|
|
//
|
2013-02-15 07:20:15 +08:00
|
|
|
// Simple pass to fill delay slots with useful instructions.
|
2007-08-18 09:50:47 +08:00
|
|
|
//
|
2011-04-16 05:51:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-08-18 09:50:47 +08:00
|
|
|
|
2014-03-11 04:34:23 +08:00
|
|
|
#include "MCTargetDesc/MipsMCNaCl.h"
|
2007-08-18 09:50:47 +08:00
|
|
|
#include "Mips.h"
|
2013-03-01 10:03:51 +08:00
|
|
|
#include "MipsInstrInfo.h"
|
2007-08-18 09:50:47 +08:00
|
|
|
#include "MipsTargetMachine.h"
|
2013-02-15 07:40:57 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2013-03-01 08:16:31 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-03-01 08:16:31 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2013-03-01 10:03:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
2007-08-18 09:50:47 +08: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
llvm-svn: 210760
2014-06-12 18:44:10 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-03-01 08:16:31 +08:00
|
|
|
#include "llvm/CodeGen/PseudoSourceValue.h"
|
2011-09-30 07:52:13 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2007-08-18 09:50:47 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2011-09-30 07:52:13 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2007-08-18 09:50:47 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "delay-slot-filler"
|
|
|
|
|
2007-08-18 09:50:47 +08:00
|
|
|
STATISTIC(FilledSlots, "Number of delay slots filled");
|
2011-10-05 09:19:13 +08:00
|
|
|
STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
|
2011-10-05 10:22:49 +08:00
|
|
|
" are not NOP.");
|
2007-08-18 09:50:47 +08:00
|
|
|
|
2012-08-22 10:51:28 +08:00
|
|
|
static cl::opt<bool> DisableDelaySlotFiller(
|
|
|
|
"disable-mips-delay-filler",
|
2011-09-30 07:52:13 +08:00
|
|
|
cl::init(false),
|
2013-02-15 07:20:15 +08:00
|
|
|
cl::desc("Fill all delay slots with NOPs."),
|
2011-09-30 07:52:13 +08:00
|
|
|
cl::Hidden);
|
|
|
|
|
2013-03-01 08:50:52 +08: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 09:02:36 +08: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 09:50:47 +08:00
|
|
|
namespace {
|
2013-03-01 10:03:51 +08:00
|
|
|
typedef MachineBasicBlock::iterator Iter;
|
|
|
|
typedef MachineBasicBlock::reverse_iterator ReverseIter;
|
|
|
|
typedef SmallDenseMap<MachineBasicBlock*, MachineInstr*, 2> BB2BrMap;
|
|
|
|
|
2013-02-26 09:30:05 +08:00
|
|
|
class RegDefsUses {
|
|
|
|
public:
|
2015-01-30 07:27:36 +08:00
|
|
|
RegDefsUses(const TargetRegisterInfo &TRI);
|
2013-02-26 09:30:05 +08:00
|
|
|
void init(const MachineInstr &MI);
|
2013-03-01 08:50:52 +08:00
|
|
|
|
|
|
|
/// This function sets all caller-saved registers in Defs.
|
|
|
|
void setCallerSaved(const MachineInstr &MI);
|
|
|
|
|
2013-03-01 10:03:51 +08: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 09:30:05 +08: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 08:50:52 +08:00
|
|
|
/// Base class for inspecting loads and stores.
|
|
|
|
class InspectMemInstr {
|
|
|
|
public:
|
2013-03-01 10:03:51 +08: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 08:50:52 +08:00
|
|
|
virtual ~InspectMemInstr() {}
|
2013-03-01 10:03:51 +08: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 08:50:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// This subclass rejects any memory instructions.
|
|
|
|
class NoMemInstr : public InspectMemInstr {
|
|
|
|
public:
|
2013-03-01 10:03:51 +08:00
|
|
|
NoMemInstr() : InspectMemInstr(true) {}
|
|
|
|
private:
|
2014-04-29 15:58:02 +08:00
|
|
|
bool hasHazard_(const MachineInstr &MI) override { return true; }
|
2013-03-01 10:03:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// This subclass accepts loads from stacks and constant loads.
|
|
|
|
class LoadFromStackOrConst : public InspectMemInstr {
|
|
|
|
public:
|
|
|
|
LoadFromStackOrConst() : InspectMemInstr(false) {}
|
|
|
|
private:
|
2014-04-29 15:58:02 +08:00
|
|
|
bool hasHazard_(const MachineInstr &MI) override;
|
2013-03-01 08:50:52 +08: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 08:16:31 +08:00
|
|
|
public:
|
2015-03-10 10:37:25 +08:00
|
|
|
MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI);
|
2013-03-01 08:16:31 +08:00
|
|
|
|
|
|
|
private:
|
2014-04-15 15:22:52 +08:00
|
|
|
typedef PointerUnion<const Value *, const PseudoSourceValue *> ValueType;
|
|
|
|
|
2014-04-29 15:58:02 +08:00
|
|
|
bool hasHazard_(const MachineInstr &MI) override;
|
2013-03-01 10:03:51 +08:00
|
|
|
|
2013-03-01 08:16:31 +08:00
|
|
|
/// Update Defs and Uses. Return true if there exist dependences that
|
2013-03-01 10:17:02 +08:00
|
|
|
/// disqualify the delay slot candidate between V and values in Uses and
|
|
|
|
/// Defs.
|
2014-04-15 15:22:52 +08:00
|
|
|
bool updateDefsUses(ValueType V, bool MayStore);
|
2013-03-01 08:16:31 +08:00
|
|
|
|
|
|
|
/// Get the list of underlying objects of MI's memory operand.
|
|
|
|
bool getUnderlyingObjects(const MachineInstr &MI,
|
2014-04-15 15:22:52 +08:00
|
|
|
SmallVectorImpl<ValueType> &Objects) const;
|
2013-03-01 08:16:31 +08:00
|
|
|
|
|
|
|
const MachineFrameInfo *MFI;
|
2014-04-15 15:22:52 +08:00
|
|
|
SmallPtrSet<ValueType, 4> Uses, Defs;
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL;
|
2013-03-01 08:16:31 +08:00
|
|
|
|
|
|
|
/// Flags indicating whether loads or stores with no underlying objects have
|
|
|
|
/// been seen.
|
|
|
|
bool SeenNoObjLoad, SeenNoObjStore;
|
|
|
|
};
|
|
|
|
|
2013-02-08 05:32:32 +08:00
|
|
|
class Filler : public MachineFunctionPass {
|
|
|
|
public:
|
2010-12-10 01:31:11 +08:00
|
|
|
Filler(TargetMachine &tm)
|
2013-06-07 15:04:14 +08:00
|
|
|
: MachineFunctionPass(ID), TM(tm) { }
|
2007-08-18 09:50:47 +08:00
|
|
|
|
2014-04-29 15:58:02 +08:00
|
|
|
const char *getPassName() const override {
|
2007-08-18 09:50:47 +08:00
|
|
|
return "Mips Delay Slot Filler";
|
|
|
|
}
|
|
|
|
|
2014-04-29 15:58:02 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override {
|
2007-08-18 09:50:47 +08: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
llvm-svn: 210760
2014-06-12 18:44:10 +08: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 09:50:47 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2014-04-29 15:58:02 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2013-03-01 10:03:51 +08:00
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2013-02-08 05:32:32 +08:00
|
|
|
|
2013-03-01 10:03:51 +08:00
|
|
|
private:
|
2013-02-08 05:32:32 +08:00
|
|
|
bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
|
|
|
|
|
2014-11-22 06:04:35 +08:00
|
|
|
Iter replaceWithCompactBranch(MachineBasicBlock &MBB,
|
|
|
|
Iter Branch, DebugLoc DL);
|
|
|
|
|
2015-02-14 01:51:27 +08:00
|
|
|
Iter replaceWithCompactJump(MachineBasicBlock &MBB,
|
|
|
|
Iter Jump, DebugLoc DL);
|
|
|
|
|
2013-02-15 07:40:57 +08:00
|
|
|
/// This function checks if it is valid to move Candidate to the delay slot
|
2013-03-01 08:16:31 +08: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 08:50:52 +08:00
|
|
|
InspectMemInstr &IM) const;
|
2011-09-30 07:52:13 +08:00
|
|
|
|
2013-03-01 08:26:14 +08: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 20:37:58 +08:00
|
|
|
RegDefsUses &RegDU, InspectMemInstr &IM, Iter Slot,
|
|
|
|
IterTy &Filler) const;
|
2013-03-01 08:26:14 +08:00
|
|
|
|
2013-03-01 08:50:52 +08: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-15 07:11:24 +08:00
|
|
|
|
2013-03-26 04:11:16 +08: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 10:03:51 +08:00
|
|
|
bool searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const;
|
|
|
|
|
2013-03-01 10:17:02 +08: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 10:03:51 +08: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-15 07:11:24 +08:00
|
|
|
bool terminateSearch(const MachineInstr &Candidate) const;
|
2011-09-30 07:52:13 +08:00
|
|
|
|
2013-02-08 05:32:32 +08:00
|
|
|
TargetMachine &TM;
|
2011-09-30 07:52:13 +08:00
|
|
|
|
2013-02-08 05:32:32 +08:00
|
|
|
static char ID;
|
2007-08-18 09:50:47 +08:00
|
|
|
};
|
|
|
|
char Filler::ID = 0;
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2013-03-01 10:03:51 +08: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-30 07:27:36 +08:00
|
|
|
assert(MF.getSubtarget().getRegisterInfo()->getAllocatableSet(MF).test(R) &&
|
2013-03-01 10:03:51 +08:00
|
|
|
"Shouldn't move an instruction with unallocatable registers across "
|
|
|
|
"basic block boundaries.");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!MBB.isLiveIn(R))
|
|
|
|
MBB.addLiveIn(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 07:27:36 +08:00
|
|
|
RegDefsUses::RegDefsUses(const TargetRegisterInfo &TRI)
|
|
|
|
: TRI(TRI), Defs(TRI.getNumRegs(), false), Uses(TRI.getNumRegs(), false) {}
|
2013-02-26 09:30:05 +08: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 08:50:52 +08:00
|
|
|
void RegDefsUses::setCallerSaved(const MachineInstr &MI) {
|
|
|
|
assert(MI.isCall());
|
|
|
|
|
2015-05-14 21:17:56 +08:00
|
|
|
// Add RA/RA_64 to Defs to prevent users of RA/RA_64 from going into
|
|
|
|
// the delay slot. The reason is that RA/RA_64 must not be changed
|
|
|
|
// in the delay slot so that the callee can return to the caller.
|
|
|
|
if (MI.definesRegister(Mips::RA) || MI.definesRegister(Mips::RA_64)) {
|
|
|
|
Defs.set(Mips::RA);
|
|
|
|
Defs.set(Mips::RA_64);
|
|
|
|
}
|
|
|
|
|
2013-03-01 08:50:52 +08:00
|
|
|
// 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);
|
|
|
|
|
2015-03-12 05:41:28 +08:00
|
|
|
for (const MCPhysReg *R = TRI.getCalleeSavedRegs(MI.getParent()->getParent());
|
|
|
|
*R; ++R)
|
2013-03-01 08:50:52 +08:00
|
|
|
for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI)
|
|
|
|
CallerSavedRegs.reset(*AI);
|
|
|
|
|
|
|
|
Defs |= CallerSavedRegs;
|
|
|
|
}
|
|
|
|
|
2013-03-01 10:03:51 +08: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)
|
2015-09-10 02:08:03 +08:00
|
|
|
for (const auto &LI : (*SI)->liveins())
|
|
|
|
Uses.set(LI.PhysReg);
|
2013-03-01 10:03:51 +08:00
|
|
|
}
|
|
|
|
|
2013-02-26 09:30:05 +08: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 10:03:51 +08:00
|
|
|
bool InspectMemInstr::hasHazard(const MachineInstr &MI) {
|
2013-03-01 08:16:31 +08:00
|
|
|
if (!MI.mayStore() && !MI.mayLoad())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ForbidMemInstr)
|
|
|
|
return true;
|
|
|
|
|
2013-03-01 10:03:51 +08:00
|
|
|
OrigSeenLoad = SeenLoad;
|
|
|
|
OrigSeenStore = SeenStore;
|
2013-03-01 08:16:31 +08: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 10:03:51 +08:00
|
|
|
return hasHazard_(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) {
|
|
|
|
if (MI.mayStore())
|
|
|
|
return true;
|
|
|
|
|
2014-04-15 15:22:52 +08:00
|
|
|
if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getPseudoValue())
|
2013-03-01 10:03:51 +08:00
|
|
|
return true;
|
|
|
|
|
2014-04-15 15:22:52 +08:00
|
|
|
if (const PseudoSourceValue *PSV =
|
|
|
|
(*MI.memoperands_begin())->getPseudoValue()) {
|
|
|
|
if (isa<FixedStackPseudoSourceValue>(PSV))
|
|
|
|
return false;
|
2015-08-12 07:09:45 +08:00
|
|
|
return !PSV->isConstant(nullptr) && !PSV->isStack();
|
2014-04-15 15:22:52 +08:00
|
|
|
}
|
2013-03-01 10:03:51 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
MemDefsUses::MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI_)
|
|
|
|
: InspectMemInstr(false), MFI(MFI_), DL(DL), SeenNoObjLoad(false),
|
|
|
|
SeenNoObjStore(false) {}
|
2013-03-01 10:03:51 +08:00
|
|
|
|
|
|
|
bool MemDefsUses::hasHazard_(const MachineInstr &MI) {
|
2013-03-01 08:16:31 +08:00
|
|
|
bool HasHazard = false;
|
2014-04-15 15:22:52 +08:00
|
|
|
SmallVector<ValueType, 4> Objs;
|
2013-03-01 08:16:31 +08:00
|
|
|
|
|
|
|
// Check underlying object list.
|
|
|
|
if (getUnderlyingObjects(MI, Objs)) {
|
2014-04-15 15:22:52 +08:00
|
|
|
for (SmallVectorImpl<ValueType>::const_iterator I = Objs.begin();
|
2013-03-01 08:16:31 +08: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 15:22:52 +08:00
|
|
|
bool MemDefsUses::updateDefsUses(ValueType V, bool MayStore) {
|
2013-03-01 08:16:31 +08:00
|
|
|
if (MayStore)
|
2014-11-19 15:49:26 +08:00
|
|
|
return !Defs.insert(V).second || Uses.count(V) || SeenNoObjStore ||
|
|
|
|
SeenNoObjLoad;
|
2013-03-01 08:16:31 +08:00
|
|
|
|
|
|
|
Uses.insert(V);
|
|
|
|
return Defs.count(V) || SeenNoObjStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemDefsUses::
|
|
|
|
getUnderlyingObjects(const MachineInstr &MI,
|
2014-04-15 15:22:52 +08:00
|
|
|
SmallVectorImpl<ValueType> &Objects) const {
|
|
|
|
if (!MI.hasOneMemOperand() ||
|
|
|
|
(!(*MI.memoperands_begin())->getValue() &&
|
|
|
|
!(*MI.memoperands_begin())->getPseudoValue()))
|
2013-03-01 08:16:31 +08:00
|
|
|
return false;
|
|
|
|
|
2014-04-15 15:22:52 +08:00
|
|
|
if (const PseudoSourceValue *PSV =
|
|
|
|
(*MI.memoperands_begin())->getPseudoValue()) {
|
|
|
|
if (!PSV->isAliased(MFI))
|
|
|
|
return false;
|
|
|
|
Objects.push_back(PSV);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-01 08:16:31 +08:00
|
|
|
const Value *V = (*MI.memoperands_begin())->getValue();
|
|
|
|
|
|
|
|
SmallVector<Value *, 4> Objs;
|
2015-03-10 10:37:25 +08:00
|
|
|
GetUnderlyingObjects(const_cast<Value *>(V), Objs, DL);
|
2013-03-01 08:16:31 +08:00
|
|
|
|
2013-07-03 23:07:05 +08:00
|
|
|
for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), E = Objs.end();
|
2013-03-01 08:16:31 +08:00
|
|
|
I != E; ++I) {
|
2014-04-15 15:22:52 +08:00
|
|
|
if (!isIdentifiedObject(V))
|
2013-03-01 08:16:31 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Objects.push_back(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-22 06:04:35 +08:00
|
|
|
// Replace Branch with the compact branch instruction.
|
|
|
|
Iter Filler::replaceWithCompactBranch(MachineBasicBlock &MBB,
|
|
|
|
Iter Branch, DebugLoc DL) {
|
2015-02-03 07:03:43 +08:00
|
|
|
const MipsInstrInfo *TII =
|
|
|
|
MBB.getParent()->getSubtarget<MipsSubtarget>().getInstrInfo();
|
2014-11-22 06:04:35 +08: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-14 01:51:27 +08: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 18:50:00 +08: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 09:50:47 +08:00
|
|
|
/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
|
2011-09-30 07:52:13 +08:00
|
|
|
/// We assume there is only one delay slot per delayed instruction.
|
2013-02-15 07:20:15 +08:00
|
|
|
bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
|
2007-08-18 09:50:47 +08:00
|
|
|
bool Changed = false;
|
2015-02-03 07:03:43 +08:00
|
|
|
const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
|
2015-01-30 07:27:36 +08:00
|
|
|
bool InMicroMipsMode = STI.inMicroMipsMode();
|
|
|
|
const MipsInstrInfo *TII = STI.getInstrInfo();
|
2011-10-05 09:30:09 +08:00
|
|
|
|
2013-02-15 07:11:24 +08:00
|
|
|
for (Iter I = MBB.begin(); I != MBB.end(); ++I) {
|
2013-03-01 10:03:51 +08:00
|
|
|
if (!hasUnoccupiedSlot(&*I))
|
2013-02-08 05:32:32 +08:00
|
|
|
continue;
|
|
|
|
|
2014-11-25 18:50:00 +08: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-11 01:27:56 +08:00
|
|
|
}
|
2014-11-25 18:50:00 +08:00
|
|
|
} else if (searchForward(MBB, I)) {
|
|
|
|
Filled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Filled) {
|
|
|
|
// Get instruction with delay slot.
|
|
|
|
MachineBasicBlock::instr_iterator DSI(I);
|
|
|
|
|
2015-10-20 08:15:20 +08:00
|
|
|
if (InMicroMipsMode && TII->GetInstSizeInBytes(&*std::next(DSI)) == 2 &&
|
2014-11-25 18:50:00 +08:00
|
|
|
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 10:03:51 +08:00
|
|
|
}
|
|
|
|
}
|
2011-09-30 07:52:13 +08:00
|
|
|
|
2014-11-22 06:04:35 +08: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-14 01:51:27 +08: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-22 06:04:35 +08:00
|
|
|
}
|
2015-02-14 01:51:27 +08: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-08 05:32:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
2007-08-18 09:50:47 +08: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 08:26:14 +08:00
|
|
|
template<typename IterTy>
|
|
|
|
bool Filler::searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
|
2015-03-04 20:37:58 +08:00
|
|
|
RegDefsUses &RegDU, InspectMemInstr& IM, Iter Slot,
|
|
|
|
IterTy &Filler) const {
|
2015-04-17 20:01:02 +08:00
|
|
|
bool IsReverseIter = std::is_convertible<IterTy, ReverseIter>::value;
|
|
|
|
|
|
|
|
for (IterTy I = Begin; I != End;) {
|
|
|
|
IterTy CurrI = I;
|
|
|
|
++I;
|
|
|
|
|
2011-09-30 07:52:13 +08:00
|
|
|
// skip debug value
|
2015-04-17 20:01:02 +08:00
|
|
|
if (CurrI->isDebugValue())
|
2011-09-30 07:52:13 +08:00
|
|
|
continue;
|
|
|
|
|
2015-04-17 20:01:02 +08:00
|
|
|
if (terminateSearch(*CurrI))
|
2011-09-30 07:52:13 +08:00
|
|
|
break;
|
|
|
|
|
2015-04-17 20:01:02 +08:00
|
|
|
assert((!CurrI->isCall() && !CurrI->isReturn() && !CurrI->isBranch()) &&
|
2013-03-01 08:16:31 +08:00
|
|
|
"Cannot put calls, returns or branches in delay slot.");
|
|
|
|
|
2015-04-17 20:01:02 +08:00
|
|
|
if (CurrI->isKill()) {
|
|
|
|
CurrI->eraseFromParent();
|
|
|
|
|
|
|
|
// This special case is needed for reverse iterators, because when we
|
|
|
|
// erase an instruction, the iterators are updated to point to the next
|
|
|
|
// instruction.
|
|
|
|
if (IsReverseIter && I != End)
|
|
|
|
I = CurrI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delayHasHazard(*CurrI, RegDU, IM))
|
2011-09-30 07:52:13 +08:00
|
|
|
continue;
|
|
|
|
|
2015-02-03 07:03:43 +08:00
|
|
|
const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
|
|
|
|
if (STI.isTargetNaCl()) {
|
2014-03-11 04:34:23 +08: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;
|
2015-04-17 20:01:02 +08:00
|
|
|
if ((isBasePlusOffsetMemoryAccess(CurrI->getOpcode(), &AddrIdx) &&
|
|
|
|
baseRegNeedsLoadStoreMask(CurrI->getOperand(AddrIdx).getReg())) ||
|
|
|
|
CurrI->modifiesRegister(Mips::SP, STI.getRegisterInfo()))
|
2014-03-11 04:34:23 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-03 07:03:43 +08:00
|
|
|
bool InMicroMipsMode = STI.inMicroMipsMode();
|
|
|
|
const MipsInstrInfo *TII = STI.getInstrInfo();
|
2015-01-13 23:59:17 +08:00
|
|
|
unsigned Opcode = (*Slot).getOpcode();
|
2015-04-17 20:01:02 +08:00
|
|
|
if (InMicroMipsMode && TII->GetInstSizeInBytes(&(*CurrI)) == 2 &&
|
2015-01-13 23:59:17 +08:00
|
|
|
(Opcode == Mips::JR || Opcode == Mips::PseudoIndirectBranch ||
|
|
|
|
Opcode == Mips::PseudoReturn))
|
|
|
|
continue;
|
|
|
|
|
2015-04-17 20:01:02 +08:00
|
|
|
Filler = CurrI;
|
2013-03-01 08:26:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-01 08:50:52 +08:00
|
|
|
bool Filler::searchBackward(MachineBasicBlock &MBB, Iter Slot) const {
|
2013-03-01 10:03:51 +08:00
|
|
|
if (DisableBackwardSearch)
|
|
|
|
return false;
|
|
|
|
|
2015-07-16 14:11:10 +08:00
|
|
|
auto *Fn = MBB.getParent();
|
|
|
|
RegDefsUses RegDU(*Fn->getSubtarget().getRegisterInfo());
|
|
|
|
MemDefsUses MemDU(Fn->getDataLayout(), Fn->getFrameInfo());
|
2013-03-01 08:50:52 +08:00
|
|
|
ReverseIter Filler;
|
2013-03-01 08:26:14 +08:00
|
|
|
|
|
|
|
RegDU.init(*Slot);
|
|
|
|
|
2015-03-04 20:37:58 +08:00
|
|
|
if (!searchRange(MBB, ReverseIter(Slot), MBB.rend(), RegDU, MemDU, Slot,
|
|
|
|
Filler))
|
2013-10-08 03:33:02 +08:00
|
|
|
return false;
|
2013-03-01 08:50:52 +08:00
|
|
|
|
2014-03-02 20:27:27 +08:00
|
|
|
MBB.splice(std::next(Slot), &MBB, std::next(Filler).base());
|
|
|
|
MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
|
2013-10-08 03:33:02 +08:00
|
|
|
++UsefulSlots;
|
|
|
|
return true;
|
2013-03-01 08:50:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Filler::searchForward(MachineBasicBlock &MBB, Iter Slot) const {
|
|
|
|
// Can handle only calls.
|
2013-03-01 10:03:51 +08:00
|
|
|
if (DisableForwardSearch || !Slot->isCall())
|
2013-03-01 08:50:52 +08:00
|
|
|
return false;
|
|
|
|
|
2015-01-30 07:27:36 +08:00
|
|
|
RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
|
2013-03-01 08:50:52 +08:00
|
|
|
NoMemInstr NM;
|
|
|
|
Iter Filler;
|
|
|
|
|
|
|
|
RegDU.setCallerSaved(*Slot);
|
|
|
|
|
2015-03-04 20:37:58 +08:00
|
|
|
if (!searchRange(MBB, std::next(Slot), MBB.end(), RegDU, NM, Slot, Filler))
|
2013-10-08 03:33:02 +08:00
|
|
|
return false;
|
2011-10-05 09:23:39 +08:00
|
|
|
|
2014-03-02 20:27:27 +08:00
|
|
|
MBB.splice(std::next(Slot), &MBB, Filler);
|
|
|
|
MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
|
2013-10-08 03:33:02 +08:00
|
|
|
++UsefulSlots;
|
|
|
|
return true;
|
2011-09-30 07:52:13 +08:00
|
|
|
}
|
|
|
|
|
2013-03-01 10:03:51 +08:00
|
|
|
bool Filler::searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const {
|
|
|
|
if (DisableSuccBBSearch)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineBasicBlock *SuccBB = selectSuccBB(MBB);
|
|
|
|
|
|
|
|
if (!SuccBB)
|
|
|
|
return false;
|
|
|
|
|
2015-01-30 07:27:36 +08:00
|
|
|
RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
|
2013-03-01 10:03:51 +08:00
|
|
|
bool HasMultipleSuccs = false;
|
|
|
|
BB2BrMap BrMap;
|
2014-04-21 17:34:48 +08:00
|
|
|
std::unique_ptr<InspectMemInstr> IM;
|
2013-03-01 10:03:51 +08:00
|
|
|
Iter Filler;
|
2015-07-16 14:11:10 +08:00
|
|
|
auto *Fn = MBB.getParent();
|
2013-03-01 10:03:51 +08:00
|
|
|
|
|
|
|
// 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.
|
2015-07-16 14:11:10 +08:00
|
|
|
RegDU.setUnallocatableRegs(*Fn);
|
2013-03-01 10:03:51 +08:00
|
|
|
|
|
|
|
// 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 {
|
2015-07-16 14:11:10 +08:00
|
|
|
const MachineFrameInfo *MFI = Fn->getFrameInfo();
|
|
|
|
IM.reset(new MemDefsUses(Fn->getDataLayout(), MFI));
|
2013-03-01 10:03:51 +08:00
|
|
|
}
|
|
|
|
|
2015-03-04 20:37:58 +08:00
|
|
|
if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Slot,
|
|
|
|
Filler))
|
2013-03-01 10:03:51 +08: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 13:30:21 +08:00
|
|
|
return nullptr;
|
2013-03-01 10:03:51 +08:00
|
|
|
|
|
|
|
// Select the successor with the larget edge weight.
|
2014-03-01 19:47:00 +08:00
|
|
|
auto &Prob = getAnalysis<MachineBranchProbabilityInfo>();
|
2015-11-24 16:51:23 +08:00
|
|
|
MachineBasicBlock *S = *std::max_element(
|
|
|
|
B.succ_begin(), B.succ_end(),
|
|
|
|
[&](const MachineBasicBlock *Dst0, const MachineBasicBlock *Dst1) {
|
|
|
|
return Prob.getEdgeProbability(&B, Dst0) <
|
|
|
|
Prob.getEdgeProbability(&B, Dst1);
|
|
|
|
});
|
2015-08-28 07:27:47 +08:00
|
|
|
return S->isEHPad() ? nullptr : S;
|
2013-03-01 10:03:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<MipsInstrInfo::BranchType, MachineInstr *>
|
|
|
|
Filler::getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const {
|
2015-02-03 07:03:43 +08:00
|
|
|
const MipsInstrInfo *TII =
|
|
|
|
MBB.getParent()->getSubtarget<MipsSubtarget>().getInstrInfo();
|
2014-04-25 13:30:21 +08:00
|
|
|
MachineBasicBlock *TrueBB = nullptr, *FalseBB = nullptr;
|
2013-03-01 10:03:51 +08: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 13:30:21 +08:00
|
|
|
return std::make_pair(R, nullptr);
|
2013-03-01 10:03:51 +08:00
|
|
|
|
|
|
|
if (R != MipsInstrInfo::BT_CondUncond) {
|
|
|
|
if (!hasUnoccupiedSlot(BranchInstrs[0]))
|
2014-04-25 13:30:21 +08:00
|
|
|
return std::make_pair(MipsInstrInfo::BT_None, nullptr);
|
2013-03-01 10:03:51 +08: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 13:30:21 +08:00
|
|
|
return std::make_pair(MipsInstrInfo::BT_None, nullptr);
|
2013-03-01 10:03:51 +08: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 08:16:31 +08:00
|
|
|
bool Filler::delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
|
2013-03-01 08:50:52 +08:00
|
|
|
InspectMemInstr &IM) const {
|
2015-04-17 20:01:02 +08:00
|
|
|
assert(!Candidate.isKill() &&
|
|
|
|
"KILL instructions should have been eliminated at this point.");
|
|
|
|
|
|
|
|
bool HasHazard = Candidate.isImplicitDef();
|
2011-09-30 07:52:13 +08:00
|
|
|
|
2013-03-01 08:50:52 +08:00
|
|
|
HasHazard |= IM.hasHazard(Candidate);
|
2013-02-26 09:30:05 +08:00
|
|
|
HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands());
|
2011-09-30 07:52:13 +08:00
|
|
|
|
2013-02-15 07:40:57 +08:00
|
|
|
return HasHazard;
|
2012-11-16 10:39:34 +08:00
|
|
|
}
|
|
|
|
|
2013-02-15 07:11:24 +08:00
|
|
|
bool Filler::terminateSearch(const MachineInstr &Candidate) const {
|
|
|
|
return (Candidate.isTerminator() || Candidate.isCall() ||
|
2014-03-07 14:08:31 +08:00
|
|
|
Candidate.isPosition() || Candidate.isInlineAsm() ||
|
2013-02-15 07:11:24 +08:00
|
|
|
Candidate.hasUnmodeledSideEffects());
|
|
|
|
}
|