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
|
|
|
|
|
|
|
#define DEBUG_TYPE "delay-slot-filler"
|
|
|
|
|
|
|
|
#include "Mips.h"
|
|
|
|
#include "MipsTargetMachine.h"
|
2013-02-14 23:40:57 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-08-18 01:50:47 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-05-14 23:59:17 +00:00
|
|
|
// This option can be used to silence complaints by machine verifier passes.
|
|
|
|
static cl::opt<bool> SkipDelaySlotFiller(
|
|
|
|
"skip-mips-delay-filler",
|
|
|
|
cl::init(false),
|
|
|
|
cl::desc("Skip MIPS' delay slot filling pass."),
|
|
|
|
cl::Hidden);
|
|
|
|
|
2007-08-18 01:50:47 +00:00
|
|
|
namespace {
|
2013-02-26 01:30:05 +00:00
|
|
|
class RegDefsUses {
|
|
|
|
public:
|
|
|
|
RegDefsUses(TargetMachine &TM);
|
|
|
|
void init(const MachineInstr &MI);
|
|
|
|
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-02-07 21:32:32 +00:00
|
|
|
class Filler : public MachineFunctionPass {
|
|
|
|
public:
|
2010-12-09 17:31:11 +00:00
|
|
|
Filler(TargetMachine &tm)
|
2010-08-06 18:33:48 +00:00
|
|
|
: MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
|
2007-08-18 01:50:47 +00:00
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Mips Delay Slot Filler";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &F) {
|
2012-05-14 23:59:17 +00:00
|
|
|
if (SkipDelaySlotFiller)
|
|
|
|
return false;
|
|
|
|
|
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);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2013-02-07 21:32:32 +00:00
|
|
|
private:
|
2013-02-14 23:11:24 +00:00
|
|
|
typedef MachineBasicBlock::iterator Iter;
|
|
|
|
typedef MachineBasicBlock::reverse_iterator ReverseIter;
|
2013-02-07 21:32:32 +00:00
|
|
|
|
|
|
|
bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
|
|
|
|
|
2013-02-14 23:40:57 +00:00
|
|
|
/// This function checks if it is valid to move Candidate to the delay slot
|
|
|
|
/// and returns true if it isn't. It also updates load and store flags and
|
|
|
|
/// register defs and uses.
|
2013-02-14 23:20:15 +00:00
|
|
|
bool delayHasHazard(const MachineInstr &Candidate, bool &SawLoad,
|
2013-02-26 01:30:05 +00:00
|
|
|
bool &SawStore, RegDefsUses &RegDU) const;
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2013-02-14 23:20:15 +00:00
|
|
|
bool findDelayInstr(MachineBasicBlock &MBB, Iter slot, Iter &Filler) 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;
|
|
|
|
const TargetInstrInfo *TII;
|
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-02-26 01:30:05 +00:00
|
|
|
RegDefsUses::RegDefsUses(TargetMachine &TM)
|
|
|
|
: TRI(*TM.getRegisterInfo()), Defs(TRI.getNumRegs(), false),
|
|
|
|
Uses(TRI.getNumRegs(), false) {}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
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-02-07 21:32:32 +00:00
|
|
|
if (!I->hasDelaySlot())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
++FilledSlots;
|
|
|
|
Changed = true;
|
2013-02-14 23:11:24 +00:00
|
|
|
Iter D;
|
2013-02-07 21:32:32 +00:00
|
|
|
|
|
|
|
// Delay slot filling is disabled at -O0.
|
|
|
|
if (!DisableDelaySlotFiller && (TM.getOptLevel() != CodeGenOpt::None) &&
|
|
|
|
findDelayInstr(MBB, I, D)) {
|
|
|
|
MBB.splice(llvm::next(I), &MBB, D);
|
|
|
|
++UsefulSlots;
|
|
|
|
} else
|
|
|
|
BuildMI(MBB, llvm::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2013-02-14 23:11:24 +00:00
|
|
|
// Bundle the delay slot filler to the instruction with the delay slot.
|
|
|
|
MIBundleBuilder(MBB, I, llvm::next(llvm::next(I)));
|
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-02-14 23:20:15 +00:00
|
|
|
bool Filler::findDelayInstr(MachineBasicBlock &MBB, Iter Slot,
|
|
|
|
Iter &Filler) const {
|
2013-02-26 01:30:05 +00:00
|
|
|
RegDefsUses RegDU(TM);
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2013-02-26 01:30:05 +00:00
|
|
|
RegDU.init(*Slot);
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2013-02-14 23:20:15 +00:00
|
|
|
bool SawLoad = false;
|
|
|
|
bool SawStore = false;
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2013-02-14 23:20:15 +00:00
|
|
|
for (ReverseIter I(Slot); I != MBB.rend(); ++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-02-26 01:30:05 +00:00
|
|
|
if (delayHasHazard(*I, SawLoad, SawStore, RegDU))
|
2011-09-29 23:52:13 +00:00
|
|
|
continue;
|
|
|
|
|
2013-02-14 23:20:15 +00:00
|
|
|
Filler = llvm::next(I).base();
|
2011-10-05 01:23:39 +00:00
|
|
|
return true;
|
2011-09-29 23:52:13 +00:00
|
|
|
}
|
2011-10-05 01:23:39 +00:00
|
|
|
|
|
|
|
return false;
|
2011-09-29 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 23:20:15 +00:00
|
|
|
bool Filler::delayHasHazard(const MachineInstr &Candidate, bool &SawLoad,
|
2013-02-26 01:30:05 +00:00
|
|
|
bool &SawStore, RegDefsUses &RegDU) const {
|
2013-02-14 23:40:57 +00:00
|
|
|
bool HasHazard = (Candidate.isImplicitDef() || Candidate.isKill());
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2011-10-05 01:09:37 +00:00
|
|
|
// Loads or stores cannot be moved past a store to the delay slot
|
2012-02-28 07:46:26 +00:00
|
|
|
// and stores cannot be moved past a load.
|
2013-02-14 23:54:40 +00:00
|
|
|
if (Candidate.mayStore() || Candidate.hasOrderedMemoryRef()) {
|
2013-02-14 23:40:57 +00:00
|
|
|
HasHazard |= SawStore | SawLoad;
|
2013-02-14 23:20:15 +00:00
|
|
|
SawStore = true;
|
2013-02-14 23:40:57 +00:00
|
|
|
} else if (Candidate.mayLoad()) {
|
|
|
|
HasHazard |= SawStore;
|
|
|
|
SawLoad = true;
|
2011-09-29 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 23:20:15 +00:00
|
|
|
assert((!Candidate.isCall() && !Candidate.isReturn()) &&
|
2011-10-05 18:17:49 +00:00
|
|
|
"Cannot put calls or returns in delay slot.");
|
2011-10-05 02:18:58 +00:00
|
|
|
|
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() ||
|
|
|
|
Candidate.isLabel() || Candidate.isInlineAsm() ||
|
|
|
|
Candidate.hasUnmodeledSideEffects());
|
|
|
|
}
|