2010-02-23 19:15:24 +00:00
|
|
|
//===-- DelaySlotFiller.cpp - MBlaze delay slot filler --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-10-27 00:23:01 +00:00
|
|
|
// A pass that attempts to fill instructions with delay slots. If no
|
|
|
|
// instructions can be moved into the delay slot then a NOP is placed there.
|
2010-02-23 19:15:24 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "delay-slot-filler"
|
|
|
|
|
|
|
|
#include "MBlaze.h"
|
|
|
|
#include "MBlazeTargetMachine.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2010-10-21 03:57:26 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-02-23 19:15:24 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(FilledSlots, "Number of delay slots filled");
|
|
|
|
|
2011-11-26 23:01:57 +00:00
|
|
|
static cl::opt<bool> MBDisableDelaySlotFiller(
|
2010-12-06 21:11:01 +00:00
|
|
|
"disable-mblaze-delay-filler",
|
|
|
|
cl::init(false),
|
|
|
|
cl::desc("Disable the MBlaze delay slot filter."),
|
|
|
|
cl::Hidden);
|
|
|
|
|
2010-02-23 19:15:24 +00:00
|
|
|
namespace {
|
|
|
|
struct Filler : public MachineFunctionPass {
|
|
|
|
|
|
|
|
TargetMachine &TM;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
|
|
|
|
static char ID;
|
2010-11-08 19:40:01 +00:00
|
|
|
Filler(TargetMachine &tm)
|
2010-08-06 18:33:48 +00:00
|
|
|
: MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
|
2010-02-23 19:15:24 +00:00
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "MBlaze 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
char Filler::ID = 0;
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
static bool hasImmInstruction(MachineBasicBlock::iterator &candidate) {
|
2010-10-21 03:57:26 +00:00
|
|
|
// Any instruction with an immediate mode operand greater than
|
|
|
|
// 16-bits requires an implicit IMM instruction.
|
|
|
|
unsigned numOper = candidate->getNumOperands();
|
2010-11-08 19:40:01 +00:00
|
|
|
for (unsigned op = 0; op < numOper; ++op) {
|
2010-12-06 21:11:01 +00:00
|
|
|
MachineOperand &mop = candidate->getOperand(op);
|
|
|
|
|
|
|
|
// The operand requires more than 16-bits to represent.
|
|
|
|
if (mop.isImm() && (mop.getImm() < -0x8000 || mop.getImm() > 0x7fff))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// We must assume that unknown immediate values require more than
|
|
|
|
// 16-bits to represent.
|
2011-04-11 22:45:02 +00:00
|
|
|
if (mop.isGlobal() || mop.isSymbol() || mop.isJTI() || mop.isCPI())
|
2010-12-06 21:11:01 +00:00
|
|
|
return true;
|
2010-10-21 03:57:26 +00:00
|
|
|
|
|
|
|
// FIXME: we could probably check to see if the FP value happens
|
|
|
|
// to not need an IMM instruction. For now we just always
|
2010-12-06 21:11:01 +00:00
|
|
|
// assume that FP values do.
|
|
|
|
if (mop.isFPImm())
|
|
|
|
return true;
|
2010-10-21 03:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-22 00:22:59 +00:00
|
|
|
static unsigned getLastRealOperand(MachineBasicBlock::iterator &instr) {
|
|
|
|
switch (instr->getOpcode()) {
|
|
|
|
default: return instr->getNumOperands();
|
|
|
|
|
|
|
|
// These instructions have a variable number of operands but the first two
|
|
|
|
// are the "real" operands that we care about during hazard detection.
|
|
|
|
case MBlaze::BRLID:
|
|
|
|
case MBlaze::BRALID:
|
|
|
|
case MBlaze::BRLD:
|
|
|
|
case MBlaze::BRALD:
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
static bool delayHasHazard(MachineBasicBlock::iterator &candidate,
|
|
|
|
MachineBasicBlock::iterator &slot) {
|
2010-12-06 21:11:01 +00:00
|
|
|
// Hazard check
|
|
|
|
MachineBasicBlock::iterator a = candidate;
|
|
|
|
MachineBasicBlock::iterator b = slot;
|
|
|
|
|
|
|
|
// MBB layout:-
|
|
|
|
// candidate := a0 = operation(a1, a2)
|
|
|
|
// ...middle bit...
|
|
|
|
// slot := b0 = operation(b1, b2)
|
|
|
|
|
|
|
|
// Possible hazards:-/
|
|
|
|
// 1. a1 or a2 was written during the middle bit
|
|
|
|
// 2. a0 was read or written during the middle bit
|
|
|
|
// 3. a0 is one or more of {b0, b1, b2}
|
|
|
|
// 4. b0 is one or more of {a1, a2}
|
|
|
|
// 5. a accesses memory, and the middle bit
|
|
|
|
// contains a store operation.
|
2011-12-07 07:15:52 +00:00
|
|
|
bool a_is_memory = candidate->mayLoad() || candidate->mayStore();
|
2010-12-06 21:11:01 +00:00
|
|
|
|
2010-12-22 00:22:59 +00:00
|
|
|
// Determine the number of operands in the slot instruction and in the
|
|
|
|
// candidate instruction.
|
|
|
|
const unsigned aend = getLastRealOperand(a);
|
|
|
|
const unsigned bend = getLastRealOperand(b);
|
|
|
|
|
2010-12-06 21:11:01 +00:00
|
|
|
// Check hazards type 1, 2 and 5 by scanning the middle bit
|
|
|
|
MachineBasicBlock::iterator m = a;
|
|
|
|
for (++m; m != b; ++m) {
|
2010-12-22 00:22:59 +00:00
|
|
|
for (unsigned aop = 0; aop<aend; ++aop) {
|
2010-12-06 21:11:01 +00:00
|
|
|
bool aop_is_reg = a->getOperand(aop).isReg();
|
|
|
|
if (!aop_is_reg) continue;
|
|
|
|
|
|
|
|
bool aop_is_def = a->getOperand(aop).isDef();
|
|
|
|
unsigned aop_reg = a->getOperand(aop).getReg();
|
|
|
|
|
2010-12-22 00:22:59 +00:00
|
|
|
const unsigned mend = getLastRealOperand(m);
|
|
|
|
for (unsigned mop = 0; mop<mend; ++mop) {
|
2010-12-06 21:11:01 +00:00
|
|
|
bool mop_is_reg = m->getOperand(mop).isReg();
|
|
|
|
if (!mop_is_reg) continue;
|
|
|
|
|
|
|
|
bool mop_is_def = m->getOperand(mop).isDef();
|
|
|
|
unsigned mop_reg = m->getOperand(mop).getReg();
|
|
|
|
|
|
|
|
if (aop_is_def && (mop_reg == aop_reg))
|
|
|
|
return true; // Hazard type 2, because aop = a0
|
|
|
|
else if (mop_is_def && (mop_reg == aop_reg))
|
|
|
|
return true; // Hazard type 1, because aop in {a1, a2}
|
|
|
|
}
|
2010-10-21 03:57:26 +00:00
|
|
|
}
|
|
|
|
|
2010-12-06 21:11:01 +00:00
|
|
|
// Check hazard type 5
|
2011-12-07 07:15:52 +00:00
|
|
|
if (a_is_memory && m->mayStore())
|
2010-12-06 21:11:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check hazard type 3 & 4
|
2010-12-22 00:22:59 +00:00
|
|
|
for (unsigned aop = 0; aop<aend; ++aop) {
|
2010-12-06 21:11:01 +00:00
|
|
|
if (a->getOperand(aop).isReg()) {
|
|
|
|
unsigned aop_reg = a->getOperand(aop).getReg();
|
2010-10-21 03:57:26 +00:00
|
|
|
|
2010-12-22 00:22:59 +00:00
|
|
|
for (unsigned bop = 0; bop<bend; ++bop) {
|
|
|
|
if (b->getOperand(bop).isReg() && !b->getOperand(bop).isImplicit()) {
|
2010-12-06 21:11:01 +00:00
|
|
|
unsigned bop_reg = b->getOperand(bop).getReg();
|
|
|
|
if (aop_reg == bop_reg)
|
|
|
|
return true;
|
2010-10-21 03:57:26 +00:00
|
|
|
}
|
2010-12-06 21:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-21 03:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-21 21:36:12 +00:00
|
|
|
static bool isDelayFiller(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator candidate) {
|
|
|
|
if (candidate == MBB.begin())
|
|
|
|
return false;
|
|
|
|
|
2011-12-07 07:15:52 +00:00
|
|
|
--candidate;
|
|
|
|
return (candidate->hasDelaySlot());
|
2010-11-21 21:36:12 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 23:50:32 +00:00
|
|
|
static bool hasUnknownSideEffects(MachineBasicBlock::iterator &I) {
|
|
|
|
if (!I->hasUnmodeledSideEffects())
|
2010-12-12 22:22:49 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned op = I->getOpcode();
|
|
|
|
if (op == MBlaze::ADDK || op == MBlaze::ADDIK ||
|
|
|
|
op == MBlaze::ADDC || op == MBlaze::ADDIC ||
|
|
|
|
op == MBlaze::ADDKC || op == MBlaze::ADDIKC ||
|
|
|
|
op == MBlaze::RSUBK || op == MBlaze::RSUBIK ||
|
|
|
|
op == MBlaze::RSUBC || op == MBlaze::RSUBIC ||
|
|
|
|
op == MBlaze::RSUBKC || op == MBlaze::RSUBIKC)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-21 03:57:26 +00:00
|
|
|
static MachineBasicBlock::iterator
|
2010-11-21 21:36:12 +00:00
|
|
|
findDelayInstr(MachineBasicBlock &MBB,MachineBasicBlock::iterator slot) {
|
|
|
|
MachineBasicBlock::iterator I = slot;
|
|
|
|
while (true) {
|
|
|
|
if (I == MBB.begin())
|
|
|
|
break;
|
|
|
|
|
|
|
|
--I;
|
2011-12-07 07:15:52 +00:00
|
|
|
if (I->hasDelaySlot() || I->isBranch() || isDelayFiller(MBB,I) ||
|
|
|
|
I->isCall() || I->isReturn() || I->isBarrier() ||
|
2011-01-07 23:50:32 +00:00
|
|
|
hasUnknownSideEffects(I))
|
2010-11-21 21:36:12 +00:00
|
|
|
break;
|
|
|
|
|
2010-12-06 21:11:01 +00:00
|
|
|
if (hasImmInstruction(I) || delayHasHazard(I,slot))
|
2010-11-21 21:36:12 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
return I;
|
2010-10-21 03:57:26 +00:00
|
|
|
}
|
|
|
|
|
2010-11-21 21:36:12 +00:00
|
|
|
return MBB.end();
|
2010-10-21 03:57:26 +00:00
|
|
|
}
|
|
|
|
|
2010-02-23 19:15:24 +00:00
|
|
|
/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
|
|
|
|
/// Currently, we fill delay slots with NOPs. We assume there is only one
|
|
|
|
/// delay slot per delayed instruction.
|
|
|
|
bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
|
2011-12-07 07:15:52 +00:00
|
|
|
if (I->hasDelaySlot()) {
|
2010-12-06 21:11:01 +00:00
|
|
|
MachineBasicBlock::iterator D = MBB.end();
|
2010-11-21 21:36:12 +00:00
|
|
|
MachineBasicBlock::iterator J = I;
|
2010-10-21 03:57:26 +00:00
|
|
|
|
2011-11-26 21:50:38 +00:00
|
|
|
if (!MBDisableDelaySlotFiller)
|
2010-12-06 21:11:01 +00:00
|
|
|
D = findDelayInstr(MBB,I);
|
|
|
|
|
2010-02-23 19:15:24 +00:00
|
|
|
++FilledSlots;
|
|
|
|
Changed = true;
|
2010-10-21 03:57:26 +00:00
|
|
|
|
2010-11-08 19:40:01 +00:00
|
|
|
if (D == MBB.end())
|
2010-11-21 21:36:12 +00:00
|
|
|
BuildMI(MBB, ++J, I->getDebugLoc(), TII->get(MBlaze::NOP));
|
2010-10-21 03:57:26 +00:00
|
|
|
else
|
2010-11-21 21:36:12 +00:00
|
|
|
MBB.splice(++J, &MBB, D);
|
2010-02-23 19:15:24 +00:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// createMBlazeDelaySlotFillerPass - Returns a pass that fills in delay
|
|
|
|
/// slots in MBlaze MachineFunctions
|
|
|
|
FunctionPass *llvm::createMBlazeDelaySlotFillerPass(MBlazeTargetMachine &tm) {
|
|
|
|
return new Filler(tm);
|
|
|
|
}
|
|
|
|
|