2007-08-18 01:50:47 +00:00
|
|
|
//===-- DelaySlotFiller.cpp - Mips delay slot filler ---------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
//
|
2011-09-29 23:52:13 +00:00
|
|
|
// Simple pass to fills 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"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2011-09-29 23:52:13 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2007-08-18 01:50:47 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2011-09-29 23:52:13 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2007-08-18 01:50:47 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-09-29 23:52:13 +00:00
|
|
|
static cl::opt<bool> EnableDelaySlotFiller(
|
|
|
|
"enable-mips-delay-filler",
|
|
|
|
cl::init(false),
|
2011-10-05 01:06:57 +00:00
|
|
|
cl::desc("Fill the Mips delay slots useful instructions."),
|
2011-09-29 23:52:13 +00:00
|
|
|
cl::Hidden);
|
|
|
|
|
2007-08-18 01:50:47 +00:00
|
|
|
namespace {
|
|
|
|
struct Filler : public MachineFunctionPass {
|
|
|
|
|
|
|
|
TargetMachine &TM;
|
|
|
|
const TargetInstrInfo *TII;
|
2011-10-05 01:30:09 +00:00
|
|
|
MachineBasicBlock::iterator LastFiller;
|
2007-08-18 01:50:47 +00:00
|
|
|
|
|
|
|
static char ID;
|
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 runOnMachineBasicBlock(MachineBasicBlock &MBB);
|
|
|
|
bool runOnMachineFunction(MachineFunction &F) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (MachineFunction::iterator FI = F.begin(), FE = F.end();
|
|
|
|
FI != FE; ++FI)
|
|
|
|
Changed |= runOnMachineBasicBlock(*FI);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2011-09-29 23:52:13 +00:00
|
|
|
bool isDelayFiller(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator candidate);
|
|
|
|
|
|
|
|
void insertCallUses(MachineBasicBlock::iterator MI,
|
|
|
|
SmallSet<unsigned, 32>& RegDefs,
|
|
|
|
SmallSet<unsigned, 32>& RegUses);
|
|
|
|
|
|
|
|
void insertDefsUses(MachineBasicBlock::iterator MI,
|
|
|
|
SmallSet<unsigned, 32>& RegDefs,
|
|
|
|
SmallSet<unsigned, 32>& RegUses);
|
|
|
|
|
|
|
|
bool IsRegInSet(SmallSet<unsigned, 32>& RegSet,
|
|
|
|
unsigned Reg);
|
|
|
|
|
|
|
|
bool delayHasHazard(MachineBasicBlock::iterator candidate,
|
|
|
|
bool &sawLoad, bool &sawStore,
|
|
|
|
SmallSet<unsigned, 32> &RegDefs,
|
|
|
|
SmallSet<unsigned, 32> &RegUses);
|
|
|
|
|
2011-10-05 01:23:39 +00:00
|
|
|
bool
|
|
|
|
findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot,
|
|
|
|
MachineBasicBlock::iterator &Filler);
|
2011-09-29 23:52:13 +00:00
|
|
|
|
|
|
|
|
2007-08-18 01:50:47 +00:00
|
|
|
};
|
|
|
|
char Filler::ID = 0;
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
/// 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.
|
2007-08-18 01:50:47 +00:00
|
|
|
bool Filler::
|
2011-09-29 23:52:13 +00:00
|
|
|
runOnMachineBasicBlock(MachineBasicBlock &MBB) {
|
2007-08-18 01:50:47 +00:00
|
|
|
bool Changed = false;
|
2011-10-05 01:30:09 +00:00
|
|
|
LastFiller = MBB.end();
|
|
|
|
|
2011-09-29 23:52:13 +00:00
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
|
2011-12-07 07:15:52 +00:00
|
|
|
if (I->hasDelaySlot()) {
|
2007-08-18 01:50:47 +00:00
|
|
|
++FilledSlots;
|
|
|
|
Changed = true;
|
2010-12-09 17:31:11 +00:00
|
|
|
|
2011-10-05 01:23:39 +00:00
|
|
|
MachineBasicBlock::iterator D;
|
|
|
|
|
|
|
|
if (EnableDelaySlotFiller && 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-10-05 01:30:09 +00:00
|
|
|
// Record the filler instruction that filled the delay slot.
|
|
|
|
// The instruction after it will be visited in the next iteration.
|
|
|
|
LastFiller = ++I;
|
2011-09-29 23:52:13 +00:00
|
|
|
}
|
2007-08-18 01:50:47 +00:00
|
|
|
return Changed;
|
2011-09-29 23:52:13 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-10-05 01:23:39 +00:00
|
|
|
bool Filler::findDelayInstr(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator slot,
|
|
|
|
MachineBasicBlock::iterator &Filler) {
|
2011-09-29 23:52:13 +00:00
|
|
|
SmallSet<unsigned, 32> RegDefs;
|
|
|
|
SmallSet<unsigned, 32> RegUses;
|
|
|
|
|
2011-10-05 02:04:17 +00:00
|
|
|
insertDefsUses(slot, RegDefs, RegUses);
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2011-10-05 01:57:46 +00:00
|
|
|
bool sawLoad = false;
|
|
|
|
bool sawStore = false;
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2011-10-05 01:57:46 +00:00
|
|
|
for (MachineBasicBlock::reverse_iterator I(slot); I != MBB.rend(); ++I) {
|
2011-09-29 23:52:13 +00:00
|
|
|
// skip debug value
|
|
|
|
if (I->isDebugValue())
|
|
|
|
continue;
|
|
|
|
|
2011-10-05 01:57:46 +00:00
|
|
|
// Convert to forward iterator.
|
2011-10-05 10:11:02 +00:00
|
|
|
MachineBasicBlock::iterator FI(llvm::next(I).base());
|
2011-10-05 01:57:46 +00:00
|
|
|
|
2011-09-29 23:52:13 +00:00
|
|
|
if (I->hasUnmodeledSideEffects()
|
|
|
|
|| I->isInlineAsm()
|
|
|
|
|| I->isLabel()
|
2011-10-05 01:57:46 +00:00
|
|
|
|| FI == LastFiller
|
2011-12-07 07:15:52 +00:00
|
|
|
|| I->isPseudo()
|
2011-09-29 23:52:13 +00:00
|
|
|
//
|
|
|
|
// Should not allow:
|
|
|
|
// ERET, DERET or WAIT, PAUSE. Need to add these to instruction
|
|
|
|
// list. TBD.
|
|
|
|
)
|
|
|
|
break;
|
|
|
|
|
2011-10-05 01:57:46 +00:00
|
|
|
if (delayHasHazard(FI, sawLoad, sawStore, RegDefs, RegUses)) {
|
|
|
|
insertDefsUses(FI, RegDefs, RegUses);
|
2011-09-29 23:52:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-05 01:57:46 +00:00
|
|
|
Filler = FI;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
|
|
|
|
bool &sawLoad,
|
|
|
|
bool &sawStore,
|
|
|
|
SmallSet<unsigned, 32> &RegDefs,
|
|
|
|
SmallSet<unsigned, 32> &RegUses) {
|
|
|
|
if (candidate->isImplicitDef() || candidate->isKill())
|
|
|
|
return true;
|
|
|
|
|
2011-10-05 01:09:37 +00:00
|
|
|
// Loads or stores cannot be moved past a store to the delay slot
|
|
|
|
// and stores cannot be moved past a load.
|
2011-12-07 07:15:52 +00:00
|
|
|
if (candidate->mayLoad()) {
|
2011-09-29 23:52:13 +00:00
|
|
|
if (sawStore)
|
|
|
|
return true;
|
2011-10-05 01:09:37 +00:00
|
|
|
sawLoad = true;
|
2011-09-29 23:52:13 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 07:15:52 +00:00
|
|
|
if (candidate->mayStore()) {
|
2011-09-29 23:52:13 +00:00
|
|
|
if (sawStore)
|
|
|
|
return true;
|
|
|
|
sawStore = true;
|
|
|
|
if (sawLoad)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-07 07:15:52 +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
|
|
|
|
2011-09-29 23:52:13 +00:00
|
|
|
for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
|
|
|
|
const MachineOperand &MO = candidate->getOperand(i);
|
2011-10-05 02:18:58 +00:00
|
|
|
unsigned Reg;
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2011-10-05 02:18:58 +00:00
|
|
|
if (!MO.isReg() || !(Reg = MO.getReg()))
|
|
|
|
continue; // skip
|
2011-09-29 23:52:13 +00:00
|
|
|
|
|
|
|
if (MO.isDef()) {
|
|
|
|
// check whether Reg is defined or used before delay slot.
|
|
|
|
if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (MO.isUse()) {
|
|
|
|
// check whether Reg is defined before delay slot.
|
|
|
|
if (IsRegInSet(RegDefs, Reg))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert Defs and Uses of MI into the sets RegDefs and RegUses.
|
|
|
|
void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
|
|
|
|
SmallSet<unsigned, 32>& RegDefs,
|
|
|
|
SmallSet<unsigned, 32>& RegUses) {
|
2011-10-05 02:18:58 +00:00
|
|
|
// If MI is a call or return, just examine the explicit non-variadic operands.
|
2011-10-05 02:21:58 +00:00
|
|
|
MCInstrDesc MCID = MI->getDesc();
|
2011-12-07 07:15:52 +00:00
|
|
|
unsigned e = MI->isCall() || MI->isReturn() ? MCID.getNumOperands() :
|
|
|
|
MI->getNumOperands();
|
2011-10-05 18:11:44 +00:00
|
|
|
|
|
|
|
// Add RA to RegDefs to prevent users of RA from going into delay slot.
|
2011-12-07 07:15:52 +00:00
|
|
|
if (MI->isCall())
|
2011-10-05 18:11:44 +00:00
|
|
|
RegDefs.insert(Mips::RA);
|
2011-10-05 02:04:17 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i != e; ++i) {
|
2011-09-29 23:52:13 +00:00
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2011-10-05 02:04:17 +00:00
|
|
|
unsigned Reg;
|
2011-09-29 23:52:13 +00:00
|
|
|
|
2011-10-05 02:04:17 +00:00
|
|
|
if (!MO.isReg() || !(Reg = MO.getReg()))
|
2011-09-29 23:52:13 +00:00
|
|
|
continue;
|
2011-10-05 02:04:17 +00:00
|
|
|
|
2011-09-29 23:52:13 +00:00
|
|
|
if (MO.isDef())
|
|
|
|
RegDefs.insert(Reg);
|
2011-10-05 02:04:17 +00:00
|
|
|
else if (MO.isUse())
|
2011-09-29 23:52:13 +00:00
|
|
|
RegUses.insert(Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//returns true if the Reg or its alias is in the RegSet.
|
|
|
|
bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg) {
|
|
|
|
if (RegSet.count(Reg))
|
|
|
|
return true;
|
|
|
|
// check Aliased Registers
|
|
|
|
for (const unsigned *Alias = TM.getRegisterInfo()->getAliasSet(Reg);
|
|
|
|
*Alias; ++Alias)
|
|
|
|
if (RegSet.count(*Alias))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|