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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "lowersubregs"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.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"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct VISIBILITY_HIDDEN LowerSubregsInstructionPass
|
|
|
|
: public MachineFunctionPass {
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
LowerSubregsInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
|
|
|
|
|
|
|
|
const char *getPassName() const {
|
|
|
|
return "Subregister lowering instruction pass";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - pass entry point
|
|
|
|
bool runOnMachineFunction(MachineFunction&);
|
2007-08-06 16:33:56 +00:00
|
|
|
|
|
|
|
bool LowerExtract(MachineInstr *MI);
|
|
|
|
bool LowerInsert(MachineInstr *MI);
|
2007-07-26 08:18:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
char LowerSubregsInstructionPass::ID = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createLowerSubregsPass() {
|
|
|
|
return new LowerSubregsInstructionPass();
|
|
|
|
}
|
|
|
|
|
2007-08-06 16:33:56 +00:00
|
|
|
bool LowerSubregsInstructionPass::LowerExtract(MachineInstr *MI) {
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
MachineFunction &MF = *MBB->getParent();
|
2008-02-10 18:45:23 +00:00
|
|
|
const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
|
2007-12-31 06:32:00 +00:00
|
|
|
const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
|
2007-08-06 16:33:56 +00:00
|
|
|
|
|
|
|
assert(MI->getOperand(0).isRegister() && MI->getOperand(0).isDef() &&
|
|
|
|
MI->getOperand(1).isRegister() && MI->getOperand(1).isUse() &&
|
2007-09-14 20:33:02 +00:00
|
|
|
MI->getOperand(2).isImmediate() && "Malformed extract_subreg");
|
2007-08-06 16:33:56 +00:00
|
|
|
|
|
|
|
unsigned SuperReg = MI->getOperand(1).getReg();
|
|
|
|
unsigned SubIdx = MI->getOperand(2).getImm();
|
|
|
|
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(SuperReg) &&
|
2007-08-06 16:33:56 +00:00
|
|
|
"Extract supperg source must be a physical register");
|
2008-02-10 18:45:23 +00:00
|
|
|
unsigned SrcReg = TRI.getSubReg(SuperReg, SubIdx);
|
2007-08-06 16:33:56 +00:00
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
|
|
|
|
|
|
DOUT << "subreg: CONVERTING: " << *MI;
|
|
|
|
|
|
|
|
if (SrcReg != DstReg) {
|
|
|
|
const TargetRegisterClass *TRC = 0;
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
|
2008-03-11 07:55:13 +00:00
|
|
|
TRC = TRI.getPhysicalRegisterRegClass(DstReg);
|
2007-08-06 16:33:56 +00:00
|
|
|
} else {
|
2007-12-31 04:13:23 +00:00
|
|
|
TRC = MF.getRegInfo().getRegClass(DstReg);
|
2007-08-06 16:33:56 +00:00
|
|
|
}
|
2008-03-11 07:55:13 +00:00
|
|
|
assert(TRC == TRI.getPhysicalRegisterRegClass(SrcReg) &&
|
2007-08-06 16:33:56 +00:00
|
|
|
"Extract subreg and Dst must be of same register class");
|
|
|
|
|
2007-12-31 06:32:00 +00:00
|
|
|
TII.copyRegToReg(*MBB, MI, DstReg, SrcReg, TRC, TRC);
|
2007-08-06 16:33:56 +00:00
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
|
|
|
DOUT << "subreg: " << *(--dMI);
|
|
|
|
}
|
|
|
|
|
|
|
|
DOUT << "\n";
|
2007-08-10 21:11:55 +00:00
|
|
|
MBB->remove(MI);
|
2007-08-06 16:33:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LowerSubregsInstructionPass::LowerInsert(MachineInstr *MI) {
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
MachineFunction &MF = *MBB->getParent();
|
2008-02-10 18:45:23 +00:00
|
|
|
const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
|
2007-12-31 06:32:00 +00:00
|
|
|
const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
|
2008-03-11 10:09:17 +00:00
|
|
|
assert((MI->getOperand(0).isRegister() && MI->getOperand(0).isDef()) &&
|
|
|
|
((MI->getOperand(1).isRegister() && MI->getOperand(1).isUse()) ||
|
|
|
|
MI->getOperand(1).isImmediate()) &&
|
|
|
|
(MI->getOperand(2).isRegister() && MI->getOperand(2).isUse()) &&
|
|
|
|
MI->getOperand(3).isImmediate() && "Invalid insert_subreg");
|
|
|
|
|
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
2007-08-06 16:33:56 +00:00
|
|
|
unsigned SrcReg = 0;
|
2008-03-11 10:09:17 +00:00
|
|
|
// Check if we're inserting into an implicit value.
|
|
|
|
if (MI->getOperand(1).isImmediate())
|
2007-08-06 16:33:56 +00:00
|
|
|
SrcReg = DstReg;
|
2008-03-11 10:09:17 +00:00
|
|
|
else
|
2007-08-06 16:33:56 +00:00
|
|
|
SrcReg = MI->getOperand(1).getReg();
|
2008-03-11 10:09:17 +00:00
|
|
|
unsigned InsReg = MI->getOperand(2).getReg();
|
|
|
|
unsigned SubIdx = MI->getOperand(3).getImm();
|
2007-08-06 16:33:56 +00:00
|
|
|
|
|
|
|
assert(SubIdx != 0 && "Invalid index for extract_subreg");
|
2008-02-10 18:45:23 +00:00
|
|
|
unsigned DstSubReg = TRI.getSubReg(DstReg, SubIdx);
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
|
2007-08-06 16:33:56 +00:00
|
|
|
"Insert superreg source must be in a physical register");
|
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");
|
|
|
|
|
|
|
|
DOUT << "subreg: CONVERTING: " << *MI;
|
|
|
|
|
|
|
|
// If the inserted register is already allocated into a subregister
|
|
|
|
// of the destination, we copy the subreg into the source
|
|
|
|
// However, this is only safe if the insert instruction is the kill
|
|
|
|
// of the source register
|
2008-02-10 18:45:23 +00:00
|
|
|
bool revCopyOrder = TRI.isSubRegister(DstReg, InsReg);
|
2007-08-10 21:11:55 +00:00
|
|
|
if (revCopyOrder && InsReg != DstSubReg) {
|
2007-08-06 16:33:56 +00:00
|
|
|
if (MI->getOperand(1).isKill()) {
|
2008-02-10 18:45:23 +00:00
|
|
|
DstSubReg = TRI.getSubReg(SrcReg, SubIdx);
|
2007-08-06 16:33:56 +00:00
|
|
|
// Insert sub-register copy
|
|
|
|
const TargetRegisterClass *TRC1 = 0;
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(InsReg)) {
|
2008-03-11 07:55:13 +00:00
|
|
|
TRC1 = TRI.getPhysicalRegisterRegClass(InsReg);
|
2007-08-06 16:33:56 +00:00
|
|
|
} else {
|
2007-12-31 04:13:23 +00:00
|
|
|
TRC1 = MF.getRegInfo().getRegClass(InsReg);
|
2007-08-06 16:33:56 +00:00
|
|
|
}
|
2007-12-31 06:32:00 +00:00
|
|
|
TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC1, TRC1);
|
2007-08-10 21:11:55 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2007-08-06 16:33:56 +00:00
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
|
|
|
DOUT << "subreg: " << *(--dMI);
|
2007-08-10 21:11:55 +00:00
|
|
|
#endif
|
2007-08-06 16:33:56 +00:00
|
|
|
} else {
|
|
|
|
assert(0 && "Don't know how to convert this insert");
|
|
|
|
}
|
|
|
|
}
|
2007-08-10 21:11:55 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (InsReg == DstSubReg) {
|
|
|
|
DOUT << "subreg: Eliminated subreg copy\n";
|
|
|
|
}
|
|
|
|
#endif
|
2007-08-06 16:33:56 +00:00
|
|
|
|
|
|
|
if (SrcReg != DstReg) {
|
|
|
|
// Insert super-register copy
|
|
|
|
const TargetRegisterClass *TRC0 = 0;
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
|
2008-03-11 07:55:13 +00:00
|
|
|
TRC0 = TRI.getPhysicalRegisterRegClass(DstReg);
|
2007-08-06 16:33:56 +00:00
|
|
|
} else {
|
2007-12-31 04:13:23 +00:00
|
|
|
TRC0 = MF.getRegInfo().getRegClass(DstReg);
|
2007-08-06 16:33:56 +00:00
|
|
|
}
|
2008-03-11 07:55:13 +00:00
|
|
|
assert(TRC0 == TRI.getPhysicalRegisterRegClass(SrcReg) &&
|
2007-08-06 16:33:56 +00:00
|
|
|
"Insert superreg and Dst must be of same register class");
|
|
|
|
|
2007-12-31 06:32:00 +00:00
|
|
|
TII.copyRegToReg(*MBB, MI, DstReg, SrcReg, TRC0, TRC0);
|
2007-08-10 21:11:55 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2007-08-06 16:33:56 +00:00
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
|
|
|
DOUT << "subreg: " << *(--dMI);
|
2007-08-10 21:11:55 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (SrcReg == DstReg) {
|
|
|
|
DOUT << "subreg: Eliminated superreg copy\n";
|
2007-08-06 16:33:56 +00:00
|
|
|
}
|
2007-08-10 21:11:55 +00:00
|
|
|
#endif
|
2007-08-06 16:33:56 +00:00
|
|
|
|
|
|
|
if (!revCopyOrder && InsReg != DstSubReg) {
|
|
|
|
// Insert sub-register copy
|
|
|
|
const TargetRegisterClass *TRC1 = 0;
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(InsReg)) {
|
2008-03-11 07:55:13 +00:00
|
|
|
TRC1 = TRI.getPhysicalRegisterRegClass(InsReg);
|
2007-08-06 16:33:56 +00:00
|
|
|
} else {
|
2007-12-31 04:13:23 +00:00
|
|
|
TRC1 = MF.getRegInfo().getRegClass(InsReg);
|
2007-08-06 16:33:56 +00:00
|
|
|
}
|
2007-12-31 06:32:00 +00:00
|
|
|
TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC1, TRC1);
|
2007-08-10 21:11:55 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2007-08-06 16:33:56 +00:00
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
|
|
|
DOUT << "subreg: " << *(--dMI);
|
2007-08-10 21:11:55 +00:00
|
|
|
#endif
|
2007-08-06 16:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DOUT << "\n";
|
2007-08-10 21:11:55 +00:00
|
|
|
MBB->remove(MI);
|
2007-08-06 16:33:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-07-26 08:18:32 +00:00
|
|
|
|
|
|
|
/// runOnMachineFunction - Reduce subregister inserts and extracts to register
|
|
|
|
/// copies.
|
|
|
|
///
|
|
|
|
bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
DOUT << "Machine Function\n";
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
DOUT << "********** LOWERING SUBREG INSTRS **********\n";
|
|
|
|
DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
|
|
|
|
|
|
|
|
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;) {
|
|
|
|
MachineInstr *MI = mi++;
|
|
|
|
|
|
|
|
if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
|
|
|
|
MadeChange |= LowerExtract(MI);
|
|
|
|
} else if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) {
|
|
|
|
MadeChange |= LowerInsert(MI);
|
2007-07-26 08:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|