2007-07-26 08:18:32 +00:00
|
|
|
//===-- LowerSubregs.cpp - Subregister Lowering instruction pass ----------===//
|
|
|
|
//
|
|
|
|
// 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-07-26 08:18:32 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-09-24 23:44:12 +00:00
|
|
|
//
|
|
|
|
// This file defines a MachineFunction pass which runs after register
|
|
|
|
// allocation that turns subreg insert/extract instructions into register
|
|
|
|
// copies, as needed. This ensures correct codegen even if the coalescer
|
|
|
|
// isn't able to remove all subreg instructions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-07-26 08:18:32 +00:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "lowersubregs"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2009-08-03 20:08:18 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2008-02-10 18:45:23 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2007-07-26 08:18:32 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-07-26 08:18:32 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct LowerSubregsInstructionPass : public MachineFunctionPass {
|
2009-10-25 07:49:57 +00:00
|
|
|
private:
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
|
|
|
|
public:
|
2007-07-26 08:18:32 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-08-06 18:33:48 +00:00
|
|
|
LowerSubregsInstructionPass() : MachineFunctionPass(ID) {}
|
2007-07-26 08:18:32 +00:00
|
|
|
|
|
|
|
const char *getPassName() const {
|
|
|
|
return "Subregister lowering instruction pass";
|
|
|
|
}
|
|
|
|
|
2008-09-22 20:58:04 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2009-07-31 23:37:33 +00:00
|
|
|
AU.setPreservesCFG();
|
2008-09-22 22:21:38 +00:00
|
|
|
AU.addPreservedID(MachineLoopInfoID);
|
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
2008-09-22 20:58:04 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2007-07-26 08:18:32 +00:00
|
|
|
/// runOnMachineFunction - pass entry point
|
|
|
|
bool runOnMachineFunction(MachineFunction&);
|
2009-10-25 07:49:57 +00:00
|
|
|
|
|
|
|
private:
|
2008-03-16 03:12:01 +00:00
|
|
|
bool LowerSubregToReg(MachineInstr *MI);
|
2010-07-02 22:29:50 +00:00
|
|
|
bool LowerCopy(MachineInstr *MI);
|
2008-12-18 22:14:08 +00:00
|
|
|
|
|
|
|
void TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
|
2009-10-25 07:49:57 +00:00
|
|
|
const TargetRegisterInfo *TRI);
|
2010-06-29 18:42:49 +00:00
|
|
|
void TransferImplicitDefs(MachineInstr *MI);
|
2007-07-26 08:18:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
char LowerSubregsInstructionPass::ID = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createLowerSubregsPass() {
|
|
|
|
return new LowerSubregsInstructionPass();
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:14:08 +00:00
|
|
|
/// TransferDeadFlag - MI is a pseudo-instruction with DstReg dead,
|
|
|
|
/// and the lowered replacement instructions immediately precede it.
|
|
|
|
/// Mark the replacement instructions with the dead flag.
|
|
|
|
void
|
|
|
|
LowerSubregsInstructionPass::TransferDeadFlag(MachineInstr *MI,
|
|
|
|
unsigned DstReg,
|
2009-10-25 07:49:57 +00:00
|
|
|
const TargetRegisterInfo *TRI) {
|
2008-12-18 22:14:08 +00:00
|
|
|
for (MachineBasicBlock::iterator MII =
|
|
|
|
prior(MachineBasicBlock::iterator(MI)); ; --MII) {
|
2009-10-25 07:49:57 +00:00
|
|
|
if (MII->addRegisterDead(DstReg, TRI))
|
2008-12-18 22:14:08 +00:00
|
|
|
break;
|
|
|
|
assert(MII != MI->getParent()->begin() &&
|
2010-07-08 05:01:41 +00:00
|
|
|
"copyPhysReg output doesn't reference destination register!");
|
2008-12-18 22:14:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-29 18:42:49 +00:00
|
|
|
/// TransferImplicitDefs - MI is a pseudo-instruction, and the lowered
|
|
|
|
/// replacement instructions immediately precede it. Copy any implicit-def
|
|
|
|
/// operands from MI to the replacement instruction.
|
|
|
|
void
|
|
|
|
LowerSubregsInstructionPass::TransferImplicitDefs(MachineInstr *MI) {
|
|
|
|
MachineBasicBlock::iterator CopyMI = MI;
|
|
|
|
--CopyMI;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg() || !MO.isImplicit() || MO.isUse())
|
|
|
|
continue;
|
|
|
|
CopyMI->addOperand(MachineOperand::CreateReg(MO.getReg(), true, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
bool LowerSubregsInstructionPass::LowerSubregToReg(MachineInstr *MI) {
|
2007-08-06 16:33:56 +00:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2008-10-03 15:45:36 +00:00
|
|
|
assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
|
|
|
|
MI->getOperand(1).isImm() &&
|
|
|
|
(MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
|
|
|
|
MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
|
2010-06-22 22:11:07 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
|
|
unsigned InsReg = MI->getOperand(2).getReg();
|
2010-06-22 22:11:07 +00:00
|
|
|
assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
|
2009-03-23 07:19:58 +00:00
|
|
|
unsigned SubIdx = MI->getOperand(3).getImm();
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
assert(SubIdx != 0 && "Invalid index for insert_subreg");
|
2009-10-25 07:49:57 +00:00
|
|
|
unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
|
2009-03-23 07:19:58 +00:00
|
|
|
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
|
2007-08-06 16:33:56 +00:00
|
|
|
"Insert destination must be in a physical register");
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
|
2007-08-06 16:33:56 +00:00
|
|
|
"Inserted value must be in a physical register");
|
|
|
|
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
|
2008-03-16 03:12:01 +00:00
|
|
|
|
2010-06-22 22:11:07 +00:00
|
|
|
if (DstSubReg == InsReg) {
|
2008-08-07 02:54:50 +00:00
|
|
|
// No need to insert an identify copy instruction.
|
2009-03-23 07:19:58 +00:00
|
|
|
// Watch out for case like this:
|
2010-06-22 22:11:07 +00:00
|
|
|
// %RAX<def> = SUBREG_TO_REG 0, %EAX<kill>, 3
|
|
|
|
// We must leave %RAX live.
|
|
|
|
if (DstReg != InsReg) {
|
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
|
|
|
MI->RemoveOperand(3); // SubIdx
|
|
|
|
MI->RemoveOperand(1); // Imm
|
|
|
|
DEBUG(dbgs() << "subreg: replace by: " << *MI);
|
|
|
|
return true;
|
|
|
|
}
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "subreg: eliminated!");
|
2008-08-07 02:54:50 +00:00
|
|
|
} else {
|
2010-07-08 05:01:41 +00:00
|
|
|
TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
|
|
|
|
MI->getOperand(2).isKill());
|
2008-12-18 22:14:08 +00:00
|
|
|
// Transfer the kill/dead flags, if needed.
|
|
|
|
if (MI->getOperand(0).isDead())
|
|
|
|
TransferDeadFlag(MI, DstSubReg, TRI);
|
2009-08-22 20:23:49 +00:00
|
|
|
DEBUG({
|
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
2010-01-04 23:06:47 +00:00
|
|
|
dbgs() << "subreg: " << *(--dMI);
|
2009-08-22 20:23:49 +00:00
|
|
|
});
|
2008-08-07 02:54:50 +00:00
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << '\n');
|
2008-07-17 23:49:46 +00:00
|
|
|
MBB->erase(MI);
|
2009-10-24 00:27:00 +00:00
|
|
|
return true;
|
2008-03-16 03:12:01 +00:00
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2010-07-02 22:29:50 +00:00
|
|
|
bool LowerSubregsInstructionPass::LowerCopy(MachineInstr *MI) {
|
|
|
|
MachineOperand &DstMO = MI->getOperand(0);
|
|
|
|
MachineOperand &SrcMO = MI->getOperand(1);
|
|
|
|
|
|
|
|
if (SrcMO.getReg() == DstMO.getReg()) {
|
|
|
|
DEBUG(dbgs() << "identity copy: " << *MI);
|
|
|
|
// No need to insert an identity copy instruction, but replace with a KILL
|
|
|
|
// if liveness is changed.
|
|
|
|
if (DstMO.isDead() || SrcMO.isUndef() || MI->getNumOperands() > 2) {
|
|
|
|
// We must make sure the super-register gets killed. Replace the
|
|
|
|
// instruction with KILL.
|
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
|
|
|
DEBUG(dbgs() << "replaced by: " << *MI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Vanilla identity copy.
|
|
|
|
MI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "real copy: " << *MI);
|
2010-07-08 05:01:41 +00:00
|
|
|
TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
|
|
|
|
DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
|
2010-07-02 22:29:50 +00:00
|
|
|
|
|
|
|
if (DstMO.isDead())
|
|
|
|
TransferDeadFlag(MI, DstMO.getReg(), TRI);
|
|
|
|
if (MI->getNumOperands() > 2)
|
|
|
|
TransferImplicitDefs(MI);
|
|
|
|
DEBUG({
|
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
|
|
|
dbgs() << "replaced by: " << *(--dMI);
|
|
|
|
});
|
|
|
|
MI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-26 08:18:32 +00:00
|
|
|
/// runOnMachineFunction - Reduce subregister inserts and extracts to register
|
|
|
|
/// copies.
|
|
|
|
///
|
|
|
|
bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) {
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "Machine Function\n"
|
2009-08-22 20:23:49 +00:00
|
|
|
<< "********** LOWERING SUBREG INSTRS **********\n"
|
|
|
|
<< "********** Function: "
|
|
|
|
<< MF.getFunction()->getName() << '\n');
|
2009-10-25 07:49:57 +00:00
|
|
|
TRI = MF.getTarget().getRegisterInfo();
|
|
|
|
TII = MF.getTarget().getInstrInfo();
|
2007-07-26 08:18:32 +00:00
|
|
|
|
2009-08-22 20:23:49 +00:00
|
|
|
bool MadeChange = false;
|
2007-07-26 08:18:32 +00:00
|
|
|
|
|
|
|
for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
|
|
|
|
mbbi != mbbe; ++mbbi) {
|
|
|
|
for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
|
2007-08-06 16:33:56 +00:00
|
|
|
mi != me;) {
|
2009-12-03 00:50:42 +00:00
|
|
|
MachineBasicBlock::iterator nmi = llvm::next(mi);
|
2009-10-25 07:49:57 +00:00
|
|
|
MachineInstr *MI = mi;
|
2010-07-08 16:40:15 +00:00
|
|
|
assert(!MI->isInsertSubreg() && "INSERT_SUBREG should no longer appear");
|
2010-07-08 16:40:22 +00:00
|
|
|
assert(MI->getOpcode() != TargetOpcode::EXTRACT_SUBREG &&
|
|
|
|
"EXTRACT_SUBREG should no longer appear");
|
|
|
|
if (MI->isSubregToReg()) {
|
2008-03-16 03:12:01 +00:00
|
|
|
MadeChange |= LowerSubregToReg(MI);
|
2010-07-02 22:29:50 +00:00
|
|
|
} else if (MI->isCopy()) {
|
|
|
|
MadeChange |= LowerCopy(MI);
|
2007-07-26 08:18:32 +00:00
|
|
|
}
|
2009-10-25 07:49:57 +00:00
|
|
|
mi = nmi;
|
2007-07-26 08:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|