2017-08-04 06:12:30 +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"
|
2017-08-04 06:12:30 +08:00
|
|
|
#include "MipsRegisterInfo.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "MipsSubtarget.h"
|
2013-02-15 07:40:57 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/PointerUnion.h"
|
2013-03-01 08:16:31 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-03-01 08:16:31 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2013-03-01 10:03:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2007-08-18 09:50:47 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2007-08-18 09:50:47 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/CodeGen/MachineOperand.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"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/MC/MCInstrDesc.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/CodeGen.h"
|
2011-09-30 07:52:13 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2017-02-01 09:22:51 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
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);
|
|
|
|
|
2016-05-17 18:21:43 +08:00
|
|
|
enum CompactBranchPolicy {
|
|
|
|
CB_Never, ///< The policy 'never' may in some circumstances or for some
|
|
|
|
///< ISAs not be absolutely adhered to.
|
|
|
|
CB_Optimal, ///< Optimal is the default and will produce compact branches
|
|
|
|
///< when delay slots cannot be filled.
|
|
|
|
CB_Always ///< 'always' may in some circumstances may not be
|
|
|
|
///< absolutely adhered to there may not be a corresponding
|
|
|
|
///< compact form of a branch.
|
|
|
|
};
|
|
|
|
|
|
|
|
static cl::opt<CompactBranchPolicy> MipsCompactBranchPolicy(
|
|
|
|
"mips-compact-branches",cl::Optional,
|
|
|
|
cl::init(CB_Optimal),
|
|
|
|
cl::desc("MIPS Specific: Compact branch policy."),
|
|
|
|
cl::values(
|
|
|
|
clEnumValN(CB_Never, "never", "Do not use compact branches if possible."),
|
|
|
|
clEnumValN(CB_Optimal, "optimal", "Use compact branches where appropiate (default)."),
|
2016-10-09 03:41:06 +08:00
|
|
|
clEnumValN(CB_Always, "always", "Always use compact branches if possible.")
|
2016-05-17 18:21:43 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2007-08-18 09:50:47 +08:00
|
|
|
namespace {
|
2017-02-01 09:22:51 +08:00
|
|
|
|
2017-08-04 06:12:30 +08:00
|
|
|
using Iter = MachineBasicBlock::iterator;
|
|
|
|
using ReverseIter = MachineBasicBlock::reverse_iterator;
|
|
|
|
using BB2BrMap = SmallDenseMap<MachineBasicBlock *, MachineInstr *, 2>;
|
2013-03-01 10:03:51 +08:00
|
|
|
|
2013-02-26 09:30:05 +08:00
|
|
|
class RegDefsUses {
|
|
|
|
public:
|
2015-01-30 07:27:36 +08:00
|
|
|
RegDefsUses(const TargetRegisterInfo &TRI);
|
2017-02-01 09:22:51 +08:00
|
|
|
|
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:
|
2017-02-01 09:22:51 +08:00
|
|
|
InspectMemInstr(bool ForbidMemInstr_) : ForbidMemInstr(ForbidMemInstr_) {}
|
|
|
|
virtual ~InspectMemInstr() = default;
|
2013-03-01 10:03:51 +08:00
|
|
|
|
|
|
|
/// Return true if MI cannot be moved to delay slot.
|
|
|
|
bool hasHazard(const MachineInstr &MI);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Flags indicating whether loads or stores have been seen.
|
2017-02-01 09:22:51 +08:00
|
|
|
bool OrigSeenLoad = false;
|
|
|
|
bool OrigSeenStore = false;
|
|
|
|
bool SeenLoad = false;
|
|
|
|
bool SeenStore = false;
|
2013-03-01 10:03:51 +08:00
|
|
|
|
|
|
|
/// 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) {}
|
2017-02-01 09:22:51 +08:00
|
|
|
|
2013-03-01 10:03:51 +08:00
|
|
|
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) {}
|
2017-02-01 09:22:51 +08:00
|
|
|
|
2013-03-01 10:03:51 +08:00
|
|
|
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:
|
2017-08-04 06:12:30 +08:00
|
|
|
using ValueType = PointerUnion<const Value *, const PseudoSourceValue *>;
|
2014-04-15 15:22:52 +08:00
|
|
|
|
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.
|
2017-02-01 09:22:51 +08:00
|
|
|
bool SeenNoObjLoad = false;
|
|
|
|
bool SeenNoObjStore = false;
|
2013-03-01 08:16:31 +08:00
|
|
|
};
|
|
|
|
|
2013-02-08 05:32:32 +08:00
|
|
|
class Filler : public MachineFunctionPass {
|
|
|
|
public:
|
2017-08-04 06:12:30 +08:00
|
|
|
Filler() : MachineFunctionPass(ID) {}
|
2007-08-18 09:50:47 +08:00
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "Mips Delay Slot Filler"; }
|
2007-08-18 09:50:47 +08:00
|
|
|
|
2014-04-29 15:58:02 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override {
|
2017-05-19 01:21:13 +08:00
|
|
|
TM = &F.getTarget();
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-05 01:09:25 +08:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 09:27:13 +08:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-04-05 01:09:25 +08:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2016-06-12 23:39:02 +08:00
|
|
|
Iter replaceWithCompactBranch(MachineBasicBlock &MBB, Iter Branch,
|
|
|
|
const DebugLoc &DL);
|
2014-11-22 06:04:35 +08:00
|
|
|
|
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.
|
2016-09-12 02:51:28 +08:00
|
|
|
bool searchBackward(MachineBasicBlock &MBB, MachineInstr &Slot) const;
|
2013-03-01 08:50:52 +08:00
|
|
|
|
|
|
|
/// 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
|
|
|
|
2017-08-04 06:12:30 +08:00
|
|
|
const TargetMachine *TM = nullptr;
|
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
|
|
|
};
|
2017-02-01 09:22:51 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2007-08-18 09:50:47 +08:00
|
|
|
|
2017-08-04 06:12:30 +08:00
|
|
|
char Filler::ID = 0;
|
|
|
|
|
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);
|
|
|
|
|
2017-05-17 09:07:53 +08:00
|
|
|
for (unsigned R : AllocSet.set_bits())
|
2013-03-01 10:03:51 +08:00
|
|
|
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_)
|
2017-02-01 09:22:51 +08:00
|
|
|
: InspectMemInstr(false), MFI(MFI_), DL(DL) {}
|
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.
|
2016-06-12 23:39:02 +08:00
|
|
|
Iter Filler::replaceWithCompactBranch(MachineBasicBlock &MBB, Iter Branch,
|
|
|
|
const DebugLoc &DL) {
|
2016-03-15 00:24:05 +08:00
|
|
|
const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
|
|
|
|
const MipsInstrInfo *TII = STI.getInstrInfo();
|
2014-11-22 06:04:35 +08:00
|
|
|
|
2016-03-15 00:24:05 +08:00
|
|
|
unsigned NewOpcode = TII->getEquivalentCompactForm(Branch);
|
|
|
|
Branch = TII->genInstrWithNewOpc(NewOpcode, Branch);
|
2014-11-22 06:04:35 +08:00
|
|
|
|
2016-03-15 00:24:05 +08:00
|
|
|
std::next(Branch)->eraseFromParent();
|
2014-11-22 06:04:35 +08:00
|
|
|
return Branch;
|
|
|
|
}
|
|
|
|
|
2014-11-25 18:50:00 +08:00
|
|
|
// For given opcode returns opcode of corresponding instruction with short
|
|
|
|
// delay slot.
|
2017-06-30 17:11:50 +08:00
|
|
|
// For the pseudo TAILCALL*_MM instructions return the short delay slot
|
2016-08-04 17:17:07 +08:00
|
|
|
// form. Unfortunately, TAILCALL<->b16 is denied as b16 has a limited range
|
|
|
|
// that is too short to make use of for tail calls.
|
2014-11-25 18:50:00 +08:00
|
|
|
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;
|
2016-08-04 17:17:07 +08:00
|
|
|
case Mips::TAILCALL_MM:
|
|
|
|
llvm_unreachable("Attempting to shorten the TAILCALL_MM pseudo!");
|
2016-08-18 21:22:43 +08:00
|
|
|
case Mips::TAILCALLREG:
|
2016-08-04 17:17:07 +08:00
|
|
|
return Mips::JR16_MM;
|
2014-11-25 18:50:00 +08:00
|
|
|
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;
|
|
|
|
|
2017-11-23 20:38:04 +08:00
|
|
|
// Delay slot filling is disabled at -O0, or in microMIPS32R6.
|
|
|
|
if (!DisableDelaySlotFiller && (TM->getOptLevel() != CodeGenOpt::None) &&
|
|
|
|
!(InMicroMipsMode && STI.hasMips32r6())) {
|
2014-11-25 18:50:00 +08:00
|
|
|
|
|
|
|
bool Filled = false;
|
|
|
|
|
2016-05-17 18:21:43 +08:00
|
|
|
if (MipsCompactBranchPolicy.getValue() != CB_Always ||
|
|
|
|
!TII->getEquivalentCompactForm(I)) {
|
2016-09-12 02:51:28 +08:00
|
|
|
if (searchBackward(MBB, *I)) {
|
2016-05-17 18:21:43 +08:00
|
|
|
Filled = true;
|
|
|
|
} else if (I->isTerminator()) {
|
|
|
|
if (searchSuccBBs(MBB, I)) {
|
|
|
|
Filled = true;
|
|
|
|
}
|
|
|
|
} else if (searchForward(MBB, I)) {
|
2014-11-25 18:50:00 +08:00
|
|
|
Filled = true;
|
2014-11-11 01:27:56 +08:00
|
|
|
}
|
2014-11-25 18:50:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Filled) {
|
|
|
|
// Get instruction with delay slot.
|
2016-07-16 07:09:47 +08:00
|
|
|
MachineBasicBlock::instr_iterator DSI = I.getInstrIterator();
|
2014-11-25 18:50:00 +08:00
|
|
|
|
2016-07-29 00:32:22 +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.
|
2016-08-04 17:17:07 +08:00
|
|
|
|
|
|
|
// TODO: Implement an instruction mapping table of 16bit opcodes to
|
|
|
|
// 32bit opcodes so that an instruction can be expanded. This would
|
|
|
|
// save 16 bits as a TAILCALL_MM pseudo requires a fullsized nop.
|
2018-01-22 13:54:46 +08:00
|
|
|
// TODO: Permit b16 when branching backwards to the same function
|
2016-08-04 17:17:07 +08:00
|
|
|
// if it is in range.
|
2014-11-25 18:50:00 +08:00
|
|
|
DSI->setDesc(TII->get(getEquivalentCallShort(DSI->getOpcode())));
|
|
|
|
}
|
2017-11-23 20:38:04 +08:00
|
|
|
++FilledSlots;
|
|
|
|
Changed = true;
|
2014-11-25 18:50:00 +08:00
|
|
|
continue;
|
2013-03-01 10:03:51 +08:00
|
|
|
}
|
|
|
|
}
|
2011-09-30 07:52:13 +08:00
|
|
|
|
2016-04-05 20:50:29 +08:00
|
|
|
// For microMIPS 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. Additionally
|
|
|
|
// PseudoReturn and PseudoIndirectBranch are expanded to JR_MM, so they can
|
|
|
|
// be replaced with JRC16_MM.
|
2016-03-15 00:24:05 +08:00
|
|
|
|
|
|
|
// For MIPSR6 attempt to produce the corresponding compact (no delay slot)
|
2016-04-05 20:50:29 +08:00
|
|
|
// form of the CTI. For indirect jumps this will not require inserting a
|
|
|
|
// NOP and for branches will hopefully avoid requiring a NOP.
|
2016-05-17 18:21:43 +08:00
|
|
|
if ((InMicroMipsMode ||
|
|
|
|
(STI.hasMips32r6() && MipsCompactBranchPolicy != CB_Never)) &&
|
|
|
|
TII->getEquivalentCompactForm(I)) {
|
2016-03-15 00:24:05 +08:00
|
|
|
I = replaceWithCompactBranch(MBB, I, I->getDebugLoc());
|
2017-11-23 20:38:04 +08:00
|
|
|
Changed = true;
|
2016-03-15 00:24:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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));
|
2017-11-23 20:38:04 +08:00
|
|
|
++FilledSlots;
|
|
|
|
Changed = true;
|
2013-02-08 05:32:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
2007-08-18 09:50:47 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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();
|
|
|
|
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();
|
2016-08-04 17:17:07 +08:00
|
|
|
// This is complicated by the tail call optimization. For non-PIC code
|
|
|
|
// there is only a 32bit sized unconditional branch which can be assumed
|
|
|
|
// to be able to reach the target. b16 only has a range of +/- 1 KB.
|
|
|
|
// It's entirely possible that the target function is reachable with b16
|
|
|
|
// but we don't have enough information to make that decision.
|
|
|
|
if (InMicroMipsMode && TII->getInstSizeInBytes(*CurrI) == 2 &&
|
2015-01-13 23:59:17 +08:00
|
|
|
(Opcode == Mips::JR || Opcode == Mips::PseudoIndirectBranch ||
|
2016-08-04 17:17:07 +08:00
|
|
|
Opcode == Mips::PseudoReturn || Opcode == Mips::TAILCALL))
|
2015-01-13 23:59:17 +08:00
|
|
|
continue;
|
|
|
|
|
2015-04-17 20:01:02 +08:00
|
|
|
Filler = CurrI;
|
2013-03-01 08:26:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-12 02:51:28 +08:00
|
|
|
bool Filler::searchBackward(MachineBasicBlock &MBB, MachineInstr &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());
|
2016-07-29 02:40:00 +08:00
|
|
|
MemDefsUses MemDU(Fn->getDataLayout(), &Fn->getFrameInfo());
|
2013-03-01 08:50:52 +08:00
|
|
|
ReverseIter Filler;
|
2013-03-01 08:26:14 +08:00
|
|
|
|
2016-09-12 02:51:28 +08:00
|
|
|
RegDU.init(Slot);
|
2013-03-01 08:26:14 +08:00
|
|
|
|
2016-09-12 02:51:28 +08:00
|
|
|
MachineBasicBlock::iterator SlotI = Slot;
|
|
|
|
if (!searchRange(MBB, ++SlotI.getReverse(), MBB.rend(), RegDU, MemDU, Slot,
|
2015-03-04 20:37:58 +08:00
|
|
|
Filler))
|
2013-10-08 03:33:02 +08:00
|
|
|
return false;
|
2013-03-01 08:50:52 +08:00
|
|
|
|
2016-09-12 02:51:28 +08:00
|
|
|
MBB.splice(std::next(SlotI), &MBB, Filler.getReverse());
|
|
|
|
MIBundleBuilder(MBB, SlotI, std::next(SlotI, 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 {
|
2016-07-29 02:40:00 +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 =
|
2016-07-15 22:41:04 +08:00
|
|
|
TII->analyzeBranch(MBB, TrueBB, FalseBB, Cond, false, BranchInstrs);
|
2013-03-01 10:03:51 +08:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
2017-02-01 09:22:51 +08:00
|
|
|
|
|
|
|
/// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
|
|
|
|
/// slots in Mips MachineFunctions
|
2017-05-19 01:21:13 +08:00
|
|
|
FunctionPass *llvm::createMipsDelaySlotFillerPass() { return new Filler(); }
|