2006-02-05 13:50:24 +08:00
|
|
|
//===-- DelaySlotFiller.cpp - SPARC delay slot filler ---------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
2006-02-05 13:50:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-01-20 13:08:26 +08:00
|
|
|
// This is a simple local pass that attempts to fill delay slots with useful
|
|
|
|
// instructions. If no instructions can be moved into the delay slot, then a
|
|
|
|
// NOP is placed.
|
2006-02-05 13:50:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sparc.h"
|
2013-10-06 15:06:44 +08:00
|
|
|
#include "SparcSubtarget.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2006-02-05 13:50:24 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2014-01-12 03:38:03 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2011-01-20 13:08:26 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-02-05 13:50:24 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2011-01-20 13:08:26 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "delay-slot-filler"
|
|
|
|
|
2006-12-20 06:59:26 +08:00
|
|
|
STATISTIC(FilledSlots, "Number of delay slots filled");
|
2006-02-05 13:50:24 +08:00
|
|
|
|
2011-01-20 13:08:26 +08:00
|
|
|
static cl::opt<bool> DisableDelaySlotFiller(
|
|
|
|
"disable-sparc-delay-filler",
|
|
|
|
cl::init(false),
|
|
|
|
cl::desc("Disable the Sparc delay slot filler."),
|
|
|
|
cl::Hidden);
|
|
|
|
|
2006-12-20 06:59:26 +08:00
|
|
|
namespace {
|
2006-02-05 13:50:24 +08:00
|
|
|
struct Filler : public MachineFunctionPass {
|
|
|
|
/// Target machine description which we query for reg. names, data
|
|
|
|
/// layout, etc.
|
|
|
|
///
|
|
|
|
TargetMachine &TM;
|
2013-10-06 15:06:44 +08:00
|
|
|
const SparcSubtarget *Subtarget;
|
2006-02-05 13:50:24 +08:00
|
|
|
|
2007-05-03 09:11:54 +08:00
|
|
|
static char ID;
|
2013-06-05 02:33:25 +08:00
|
|
|
Filler(TargetMachine &tm)
|
2013-10-06 15:06:44 +08:00
|
|
|
: MachineFunctionPass(ID), TM(tm),
|
|
|
|
Subtarget(&TM.getSubtarget<SparcSubtarget>()) {
|
|
|
|
}
|
2006-02-05 13:50:24 +08:00
|
|
|
|
2014-04-29 15:57:13 +08:00
|
|
|
const char *getPassName() const override {
|
2006-02-05 13:50:24 +08:00
|
|
|
return "SPARC Delay Slot Filler";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
|
2014-04-29 15:57:13 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override {
|
2006-02-05 13:50:24 +08:00
|
|
|
bool Changed = false;
|
2014-01-12 03:38:03 +08:00
|
|
|
|
|
|
|
// This pass invalidates liveness information when it reorders
|
|
|
|
// instructions to fill delay slot.
|
|
|
|
F.getRegInfo().invalidateLiveness();
|
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
for (MachineFunction::iterator FI = F.begin(), FE = F.end();
|
|
|
|
FI != FE; ++FI)
|
|
|
|
Changed |= runOnMachineBasicBlock(*FI);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2013-05-17 07:53:29 +08:00
|
|
|
void insertCallDefsUses(MachineBasicBlock::iterator MI,
|
|
|
|
SmallSet<unsigned, 32>& RegDefs,
|
|
|
|
SmallSet<unsigned, 32>& RegUses);
|
2011-01-20 13:08:26 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot);
|
|
|
|
|
2011-02-21 11:42:44 +08:00
|
|
|
bool needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize);
|
2011-01-20 13:08:26 +08:00
|
|
|
|
2013-06-03 05:48:17 +08:00
|
|
|
bool tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI);
|
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
};
|
2007-05-03 09:11:54 +08:00
|
|
|
char Filler::ID = 0;
|
2006-02-05 13:50:24 +08:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
|
|
|
|
/// slots in Sparc MachineFunctions
|
|
|
|
///
|
|
|
|
FunctionPass *llvm::createSparcDelaySlotFillerPass(TargetMachine &tm) {
|
|
|
|
return new Filler(tm);
|
|
|
|
}
|
|
|
|
|
2011-02-21 11:42:44 +08:00
|
|
|
|
2006-02-05 13:50:24 +08:00
|
|
|
/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
|
2011-01-20 13:08:26 +08:00
|
|
|
/// We assume there is only one delay slot per delayed instruction.
|
2006-02-05 13:50:24 +08:00
|
|
|
///
|
|
|
|
bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
|
|
|
|
bool Changed = false;
|
2011-01-20 13:08:26 +08:00
|
|
|
|
2013-10-06 15:06:44 +08:00
|
|
|
const TargetInstrInfo *TII = TM.getInstrInfo();
|
|
|
|
|
2013-06-03 05:48:17 +08:00
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
|
|
|
|
MachineBasicBlock::iterator MI = I;
|
|
|
|
++I;
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// If MI is restore, try combining it with previous inst.
|
2013-06-03 05:48:17 +08:00
|
|
|
if (!DisableDelaySlotFiller &&
|
|
|
|
(MI->getOpcode() == SP::RESTORErr
|
|
|
|
|| MI->getOpcode() == SP::RESTOREri)) {
|
|
|
|
Changed |= tryCombineRestoreWithPrevInst(MBB, MI);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-10-06 15:06:44 +08:00
|
|
|
if (!Subtarget->isV9() &&
|
|
|
|
(MI->getOpcode() == SP::FCMPS || MI->getOpcode() == SP::FCMPD
|
|
|
|
|| MI->getOpcode() == SP::FCMPQ)) {
|
|
|
|
BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
|
|
|
|
Changed = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// If MI has no delay slot, skip.
|
2013-06-03 05:48:17 +08:00
|
|
|
if (!MI->hasDelaySlot())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator D = MBB.end();
|
|
|
|
|
|
|
|
if (!DisableDelaySlotFiller)
|
|
|
|
D = findDelayInstr(MBB, MI);
|
|
|
|
|
|
|
|
++FilledSlots;
|
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
if (D == MBB.end())
|
|
|
|
BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
|
|
|
|
else
|
|
|
|
MBB.splice(I, &MBB, D);
|
|
|
|
|
|
|
|
unsigned structSize = 0;
|
|
|
|
if (needsUnimp(MI, structSize)) {
|
|
|
|
MachineBasicBlock::iterator J = MI;
|
2013-06-05 02:33:25 +08:00
|
|
|
++J; // skip the delay filler.
|
2013-06-03 05:48:17 +08:00
|
|
|
assert (J != MBB.end() && "MI needs a delay instruction.");
|
2013-07-30 10:26:29 +08:00
|
|
|
BuildMI(MBB, ++J, MI->getDebugLoc(),
|
2013-06-03 05:48:17 +08:00
|
|
|
TII->get(SP::UNIMP)).addImm(structSize);
|
2014-01-12 03:38:03 +08:00
|
|
|
// Bundle the delay filler and unimp with the instruction.
|
|
|
|
MIBundleBuilder(MBB, MachineBasicBlock::iterator(MI), J);
|
|
|
|
} else {
|
|
|
|
MIBundleBuilder(MBB, MachineBasicBlock::iterator(MI), I);
|
2006-02-05 13:50:24 +08:00
|
|
|
}
|
2013-06-03 05:48:17 +08:00
|
|
|
}
|
2006-02-05 13:50:24 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
2011-01-20 13:08:26 +08:00
|
|
|
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
Filler::findDelayInstr(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator slot)
|
|
|
|
{
|
|
|
|
SmallSet<unsigned, 32> RegDefs;
|
|
|
|
SmallSet<unsigned, 32> RegUses;
|
|
|
|
bool sawLoad = false;
|
|
|
|
bool sawStore = false;
|
|
|
|
|
2013-05-29 12:46:31 +08:00
|
|
|
if (slot == MBB.begin())
|
|
|
|
return MBB.end();
|
2011-01-20 13:08:26 +08:00
|
|
|
|
2013-10-08 10:50:29 +08:00
|
|
|
if (slot->getOpcode() == SP::RET || slot->getOpcode() == SP::TLS_CALL)
|
2011-01-20 13:08:26 +08:00
|
|
|
return MBB.end();
|
|
|
|
|
|
|
|
if (slot->getOpcode() == SP::RETL) {
|
2013-05-29 12:46:31 +08:00
|
|
|
MachineBasicBlock::iterator J = slot;
|
|
|
|
--J;
|
|
|
|
|
|
|
|
if (J->getOpcode() == SP::RESTORErr
|
|
|
|
|| J->getOpcode() == SP::RESTOREri) {
|
2013-06-05 02:33:25 +08:00
|
|
|
// change retl to ret.
|
2013-06-08 04:35:25 +08:00
|
|
|
slot->setDesc(TM.getInstrInfo()->get(SP::RET));
|
2013-05-29 12:46:31 +08:00
|
|
|
return J;
|
|
|
|
}
|
2011-01-20 13:08:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Call's delay filler can def some of call's uses.
|
2011-12-07 15:15:52 +08:00
|
|
|
if (slot->isCall())
|
2013-05-17 07:53:29 +08:00
|
|
|
insertCallDefsUses(slot, RegDefs, RegUses);
|
2011-01-20 13:08:26 +08:00
|
|
|
else
|
|
|
|
insertDefsUses(slot, RegDefs, RegUses);
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
|
2013-05-29 12:46:31 +08:00
|
|
|
MachineBasicBlock::iterator I = slot;
|
|
|
|
|
2011-01-20 13:08:26 +08:00
|
|
|
while (!done) {
|
|
|
|
done = (I == MBB.begin());
|
|
|
|
|
|
|
|
if (!done)
|
|
|
|
--I;
|
|
|
|
|
|
|
|
// skip debug value
|
|
|
|
if (I->isDebugValue())
|
|
|
|
continue;
|
|
|
|
|
2014-03-07 14:08:31 +08:00
|
|
|
if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() ||
|
|
|
|
I->hasDelaySlot() || I->isBundledWithSucc())
|
2011-01-20 13:08:26 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
|
|
|
|
insertDefsUses(I, RegDefs, RegUses);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
return MBB.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
|
|
|
|
bool &sawLoad,
|
|
|
|
bool &sawStore,
|
|
|
|
SmallSet<unsigned, 32> &RegDefs,
|
|
|
|
SmallSet<unsigned, 32> &RegUses)
|
|
|
|
{
|
|
|
|
|
2011-02-13 03:02:33 +08:00
|
|
|
if (candidate->isImplicitDef() || candidate->isKill())
|
|
|
|
return true;
|
|
|
|
|
2011-12-07 15:15:52 +08:00
|
|
|
if (candidate->mayLoad()) {
|
2011-01-20 13:08:26 +08:00
|
|
|
sawLoad = true;
|
|
|
|
if (sawStore)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-07 15:15:52 +08:00
|
|
|
if (candidate->mayStore()) {
|
2011-01-20 13:08:26 +08:00
|
|
|
if (sawStore)
|
|
|
|
return true;
|
|
|
|
sawStore = true;
|
|
|
|
if (sawLoad)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = candidate->getNumOperands(); i!= e; ++i) {
|
|
|
|
const MachineOperand &MO = candidate->getOperand(i);
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue; // skip
|
|
|
|
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
|
|
|
|
if (MO.isDef()) {
|
2013-06-05 02:33:25 +08:00
|
|
|
// check whether Reg is defined or used before delay slot.
|
2011-01-20 13:08:26 +08:00
|
|
|
if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (MO.isUse()) {
|
2013-06-05 02:33:25 +08:00
|
|
|
// check whether Reg is defined before delay slot.
|
2011-01-20 13:08:26 +08:00
|
|
|
if (IsRegInSet(RegDefs, Reg))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-17 07:53:29 +08:00
|
|
|
void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI,
|
|
|
|
SmallSet<unsigned, 32>& RegDefs,
|
|
|
|
SmallSet<unsigned, 32>& RegUses)
|
2011-01-20 13:08:26 +08:00
|
|
|
{
|
2013-06-05 02:33:25 +08:00
|
|
|
// Call defines o7, which is visible to the instruction in delay slot.
|
2013-05-17 07:53:29 +08:00
|
|
|
RegDefs.insert(SP::O7);
|
2011-01-20 13:08:26 +08:00
|
|
|
|
|
|
|
switch(MI->getOpcode()) {
|
|
|
|
default: llvm_unreachable("Unknown opcode.");
|
|
|
|
case SP::CALL: break;
|
2014-01-10 09:48:17 +08:00
|
|
|
case SP::CALLrr:
|
|
|
|
case SP::CALLri:
|
2011-01-20 13:08:26 +08:00
|
|
|
assert(MI->getNumOperands() >= 2);
|
|
|
|
const MachineOperand &Reg = MI->getOperand(0);
|
2014-01-10 09:48:17 +08:00
|
|
|
assert(Reg.isReg() && "CALL first operand is not a register.");
|
|
|
|
assert(Reg.isUse() && "CALL first operand is not a use.");
|
2011-01-20 13:08:26 +08:00
|
|
|
RegUses.insert(Reg.getReg());
|
|
|
|
|
|
|
|
const MachineOperand &RegOrImm = MI->getOperand(1);
|
|
|
|
if (RegOrImm.isImm())
|
|
|
|
break;
|
2014-01-10 09:48:17 +08:00
|
|
|
assert(RegOrImm.isReg() && "CALLrr second operand is not a register.");
|
|
|
|
assert(RegOrImm.isUse() && "CALLrr second operand is not a use.");
|
2011-01-20 13:08:26 +08:00
|
|
|
RegUses.insert(RegOrImm.getReg());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Insert Defs and Uses of MI into the sets RegDefs and RegUses.
|
2011-01-20 13:08:26 +08:00
|
|
|
void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
|
|
|
|
SmallSet<unsigned, 32>& RegDefs,
|
|
|
|
SmallSet<unsigned, 32>& RegUses)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (Reg == 0)
|
|
|
|
continue;
|
|
|
|
if (MO.isDef())
|
|
|
|
RegDefs.insert(Reg);
|
2013-05-29 12:46:31 +08:00
|
|
|
if (MO.isUse()) {
|
2013-06-05 02:33:25 +08:00
|
|
|
// Implicit register uses of retl are return values and
|
|
|
|
// retl does not use them.
|
2013-05-29 12:46:31 +08:00
|
|
|
if (MO.isImplicit() && MI->getOpcode() == SP::RETL)
|
|
|
|
continue;
|
2011-01-20 13:08:26 +08:00
|
|
|
RegUses.insert(Reg);
|
2013-05-29 12:46:31 +08:00
|
|
|
}
|
2011-01-20 13:08:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// returns true if the Reg or its alias is in the RegSet.
|
2011-01-20 13:08:26 +08:00
|
|
|
bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg)
|
|
|
|
{
|
2012-06-02 04:36:54 +08:00
|
|
|
// Check Reg and all aliased Registers.
|
|
|
|
for (MCRegAliasIterator AI(Reg, TM.getRegisterInfo(), true);
|
|
|
|
AI.isValid(); ++AI)
|
|
|
|
if (RegSet.count(*AI))
|
2011-01-20 13:08:26 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-21 11:42:44 +08:00
|
|
|
bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
|
|
|
|
{
|
2011-12-07 15:15:52 +08:00
|
|
|
if (!I->isCall())
|
2011-02-21 11:42:44 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned structSizeOpNum = 0;
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
default: llvm_unreachable("Unknown call opcode.");
|
|
|
|
case SP::CALL: structSizeOpNum = 1; break;
|
2014-01-10 09:48:17 +08:00
|
|
|
case SP::CALLrr:
|
|
|
|
case SP::CALLri: structSizeOpNum = 2; break;
|
2013-10-08 10:50:29 +08:00
|
|
|
case SP::TLS_CALL: return false;
|
2011-02-21 11:42:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const MachineOperand &MO = I->getOperand(structSizeOpNum);
|
|
|
|
if (!MO.isImm())
|
|
|
|
return false;
|
|
|
|
StructSize = MO.getImm();
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-03 05:48:17 +08:00
|
|
|
|
|
|
|
static bool combineRestoreADD(MachineBasicBlock::iterator RestoreMI,
|
|
|
|
MachineBasicBlock::iterator AddMI,
|
|
|
|
const TargetInstrInfo *TII)
|
|
|
|
{
|
2013-06-05 02:33:25 +08:00
|
|
|
// Before: add <op0>, <op1>, %i[0-7]
|
|
|
|
// restore %g0, %g0, %i[0-7]
|
2013-06-03 05:48:17 +08:00
|
|
|
//
|
2013-06-05 02:33:25 +08:00
|
|
|
// After : restore <op0>, <op1>, %o[0-7]
|
2013-06-03 05:48:17 +08:00
|
|
|
|
|
|
|
unsigned reg = AddMI->getOperand(0).getReg();
|
|
|
|
if (reg < SP::I0 || reg > SP::I7)
|
|
|
|
return false;
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Erase RESTORE.
|
2013-06-03 05:48:17 +08:00
|
|
|
RestoreMI->eraseFromParent();
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Change ADD to RESTORE.
|
2013-06-03 05:48:17 +08:00
|
|
|
AddMI->setDesc(TII->get((AddMI->getOpcode() == SP::ADDrr)
|
|
|
|
? SP::RESTORErr
|
|
|
|
: SP::RESTOREri));
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Map the destination register.
|
2013-06-03 05:48:17 +08:00
|
|
|
AddMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool combineRestoreOR(MachineBasicBlock::iterator RestoreMI,
|
|
|
|
MachineBasicBlock::iterator OrMI,
|
|
|
|
const TargetInstrInfo *TII)
|
|
|
|
{
|
2013-06-05 02:33:25 +08:00
|
|
|
// Before: or <op0>, <op1>, %i[0-7]
|
|
|
|
// restore %g0, %g0, %i[0-7]
|
|
|
|
// and <op0> or <op1> is zero,
|
2013-06-03 05:48:17 +08:00
|
|
|
//
|
2013-06-05 02:33:25 +08:00
|
|
|
// After : restore <op0>, <op1>, %o[0-7]
|
2013-06-03 05:48:17 +08:00
|
|
|
|
|
|
|
unsigned reg = OrMI->getOperand(0).getReg();
|
|
|
|
if (reg < SP::I0 || reg > SP::I7)
|
|
|
|
return false;
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// check whether it is a copy.
|
2013-06-03 05:48:17 +08:00
|
|
|
if (OrMI->getOpcode() == SP::ORrr
|
|
|
|
&& OrMI->getOperand(1).getReg() != SP::G0
|
|
|
|
&& OrMI->getOperand(2).getReg() != SP::G0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (OrMI->getOpcode() == SP::ORri
|
|
|
|
&& OrMI->getOperand(1).getReg() != SP::G0
|
|
|
|
&& (!OrMI->getOperand(2).isImm() || OrMI->getOperand(2).getImm() != 0))
|
|
|
|
return false;
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Erase RESTORE.
|
2013-06-03 05:48:17 +08:00
|
|
|
RestoreMI->eraseFromParent();
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Change OR to RESTORE.
|
2013-06-03 05:48:17 +08:00
|
|
|
OrMI->setDesc(TII->get((OrMI->getOpcode() == SP::ORrr)
|
|
|
|
? SP::RESTORErr
|
|
|
|
: SP::RESTOREri));
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Map the destination register.
|
2013-06-03 05:48:17 +08:00
|
|
|
OrMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI,
|
|
|
|
MachineBasicBlock::iterator SetHiMI,
|
|
|
|
const TargetInstrInfo *TII)
|
|
|
|
{
|
2013-06-05 02:33:25 +08:00
|
|
|
// Before: sethi imm3, %i[0-7]
|
|
|
|
// restore %g0, %g0, %g0
|
2013-06-03 05:48:17 +08:00
|
|
|
//
|
2013-06-05 02:33:25 +08:00
|
|
|
// After : restore %g0, (imm3<<10), %o[0-7]
|
2013-06-03 05:48:17 +08:00
|
|
|
|
|
|
|
unsigned reg = SetHiMI->getOperand(0).getReg();
|
|
|
|
if (reg < SP::I0 || reg > SP::I7)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!SetHiMI->getOperand(1).isImm())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int64_t imm = SetHiMI->getOperand(1).getImm();
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Is it a 3 bit immediate?
|
2013-06-03 05:48:17 +08:00
|
|
|
if (!isInt<3>(imm))
|
|
|
|
return false;
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Make it a 13 bit immediate.
|
2013-06-03 05:48:17 +08:00
|
|
|
imm = (imm << 10) & 0x1FFF;
|
|
|
|
|
|
|
|
assert(RestoreMI->getOpcode() == SP::RESTORErr);
|
|
|
|
|
|
|
|
RestoreMI->setDesc(TII->get(SP::RESTOREri));
|
|
|
|
|
|
|
|
RestoreMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
|
|
|
|
RestoreMI->getOperand(1).setReg(SP::G0);
|
|
|
|
RestoreMI->getOperand(2).ChangeToImmediate(imm);
|
|
|
|
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// Erase the original SETHI.
|
2013-06-03 05:48:17 +08:00
|
|
|
SetHiMI->eraseFromParent();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Filler::tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI)
|
|
|
|
{
|
2013-06-05 02:33:25 +08:00
|
|
|
// No previous instruction.
|
2013-06-03 05:48:17 +08:00
|
|
|
if (MBBI == MBB.begin())
|
|
|
|
return false;
|
|
|
|
|
2013-06-05 02:33:25 +08:00
|
|
|
// assert that MBBI is a "restore %g0, %g0, %g0".
|
2013-06-03 05:48:17 +08:00
|
|
|
assert(MBBI->getOpcode() == SP::RESTORErr
|
|
|
|
&& MBBI->getOperand(0).getReg() == SP::G0
|
|
|
|
&& MBBI->getOperand(1).getReg() == SP::G0
|
|
|
|
&& MBBI->getOperand(2).getReg() == SP::G0);
|
|
|
|
|
2014-03-02 20:27:27 +08:00
|
|
|
MachineBasicBlock::iterator PrevInst = std::prev(MBBI);
|
2013-06-03 05:48:17 +08:00
|
|
|
|
2014-01-12 03:38:03 +08:00
|
|
|
// It cannot be combined with a bundled instruction.
|
|
|
|
if (PrevInst->isBundledWithSucc())
|
2013-06-03 05:48:17 +08:00
|
|
|
return false;
|
|
|
|
|
2013-06-08 04:35:25 +08:00
|
|
|
const TargetInstrInfo *TII = TM.getInstrInfo();
|
|
|
|
|
2013-06-03 05:48:17 +08:00
|
|
|
switch (PrevInst->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case SP::ADDrr:
|
|
|
|
case SP::ADDri: return combineRestoreADD(MBBI, PrevInst, TII); break;
|
|
|
|
case SP::ORrr:
|
|
|
|
case SP::ORri: return combineRestoreOR(MBBI, PrevInst, TII); break;
|
|
|
|
case SP::SETHIi: return combineRestoreSETHIi(MBBI, PrevInst, TII); break;
|
|
|
|
}
|
2013-06-05 02:33:25 +08:00
|
|
|
// It cannot combine with the previous instruction.
|
2013-06-03 05:48:17 +08:00
|
|
|
return false;
|
|
|
|
}
|