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"
|
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
|
2008-09-04 17:05:41 +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 {
|
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&);
|
2007-08-06 16:33:56 +00:00
|
|
|
|
|
|
|
bool LowerExtract(MachineInstr *MI);
|
|
|
|
bool LowerInsert(MachineInstr *MI);
|
2008-03-16 03:12:01 +00:00
|
|
|
bool LowerSubregToReg(MachineInstr *MI);
|
2008-12-18 22:14:08 +00:00
|
|
|
|
|
|
|
void TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
|
|
|
|
const TargetRegisterInfo &TRI);
|
|
|
|
void TransferKillFlag(MachineInstr *MI, unsigned SrcReg,
|
|
|
|
const TargetRegisterInfo &TRI);
|
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,
|
|
|
|
const TargetRegisterInfo &TRI) {
|
|
|
|
for (MachineBasicBlock::iterator MII =
|
|
|
|
prior(MachineBasicBlock::iterator(MI)); ; --MII) {
|
|
|
|
if (MII->addRegisterDead(DstReg, &TRI))
|
|
|
|
break;
|
|
|
|
assert(MII != MI->getParent()->begin() &&
|
|
|
|
"copyRegToReg output doesn't reference destination register!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TransferKillFlag - MI is a pseudo-instruction with SrcReg killed,
|
|
|
|
/// and the lowered replacement instructions immediately precede it.
|
|
|
|
/// Mark the replacement instructions with the kill flag.
|
|
|
|
void
|
|
|
|
LowerSubregsInstructionPass::TransferKillFlag(MachineInstr *MI,
|
|
|
|
unsigned SrcReg,
|
|
|
|
const TargetRegisterInfo &TRI) {
|
|
|
|
for (MachineBasicBlock::iterator MII =
|
|
|
|
prior(MachineBasicBlock::iterator(MI)); ; --MII) {
|
|
|
|
if (MII->addRegisterKilled(SrcReg, &TRI))
|
|
|
|
break;
|
|
|
|
assert(MII != MI->getParent()->begin() &&
|
|
|
|
"copyRegToReg output doesn't reference source register!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-06 16:33:56 +00:00
|
|
|
bool LowerSubregsInstructionPass::LowerExtract(MachineInstr *MI) {
|
2008-12-18 22:06:01 +00:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
MachineFunction &MF = *MBB->getParent();
|
|
|
|
const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
|
|
|
|
const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
|
|
|
|
|
|
|
|
assert(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
|
|
|
|
MI->getOperand(1).isReg() && MI->getOperand(1).isUse() &&
|
|
|
|
MI->getOperand(2).isImm() && "Malformed extract_subreg");
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-12-18 22:06:01 +00:00
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
|
|
unsigned SuperReg = MI->getOperand(1).getReg();
|
|
|
|
unsigned SubIdx = MI->getOperand(2).getImm();
|
|
|
|
unsigned SrcReg = TRI.getSubReg(SuperReg, SubIdx);
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-12-18 22:06:01 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(SuperReg) &&
|
|
|
|
"Extract supperg source must be a physical register");
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
|
2008-12-18 22:07:25 +00:00
|
|
|
"Extract destination must be in a physical register");
|
2008-12-18 22:06:01 +00:00
|
|
|
|
|
|
|
DOUT << "subreg: CONVERTING: " << *MI;
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-12-18 22:11:34 +00:00
|
|
|
if (SrcReg == DstReg) {
|
|
|
|
// No need to insert an identify copy instruction.
|
|
|
|
DOUT << "subreg: eliminated!";
|
2008-12-18 22:14:08 +00:00
|
|
|
// Find the kill of the destination register's live range, and insert
|
|
|
|
// a kill of the source register at that point.
|
|
|
|
if (MI->getOperand(1).isKill() && !MI->getOperand(0).isDead())
|
|
|
|
for (MachineBasicBlock::iterator MII =
|
|
|
|
next(MachineBasicBlock::iterator(MI));
|
|
|
|
MII != MBB->end(); ++MII)
|
|
|
|
if (MII->killsRegister(DstReg, &TRI)) {
|
|
|
|
MII->addRegisterKilled(SuperReg, &TRI, /*AddIfNotFound=*/true);
|
|
|
|
break;
|
|
|
|
}
|
2008-12-18 22:11:34 +00:00
|
|
|
} else {
|
|
|
|
// Insert copy
|
2008-12-18 22:06:01 +00:00
|
|
|
const TargetRegisterClass *TRC = TRI.getPhysicalRegisterRegClass(DstReg);
|
|
|
|
assert(TRC == TRI.getPhysicalRegisterRegClass(SrcReg) &&
|
|
|
|
"Extract subreg and Dst must be of same register class");
|
|
|
|
TII.copyRegToReg(*MBB, MI, DstReg, SrcReg, TRC, TRC);
|
2008-12-18 22:14:08 +00:00
|
|
|
// Transfer the kill/dead flags, if needed.
|
|
|
|
if (MI->getOperand(0).isDead())
|
|
|
|
TransferDeadFlag(MI, DstReg, TRI);
|
|
|
|
if (MI->getOperand(1).isKill())
|
|
|
|
TransferKillFlag(MI, SrcReg, TRI);
|
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
#ifndef NDEBUG
|
2008-12-18 22:06:01 +00:00
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
|
|
|
DOUT << "subreg: " << *(--dMI);
|
2008-03-16 03:12:01 +00:00
|
|
|
#endif
|
2008-12-18 22:06:01 +00:00
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-12-18 22:06:01 +00:00
|
|
|
DOUT << "\n";
|
|
|
|
MBB->erase(MI);
|
|
|
|
return true;
|
2007-08-06 16:33:56 +00:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
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-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");
|
2008-03-11 10:09:17 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
|
|
unsigned InsReg = MI->getOperand(2).getReg();
|
2009-03-23 07:19:58 +00:00
|
|
|
unsigned InsSIdx = MI->getOperand(2).getSubReg();
|
|
|
|
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");
|
2008-02-10 18:45:23 +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");
|
|
|
|
|
|
|
|
DOUT << "subreg: CONVERTING: " << *MI;
|
2008-03-16 03:12:01 +00:00
|
|
|
|
2009-03-23 07:19:58 +00:00
|
|
|
if (DstSubReg == InsReg && InsSIdx == 0) {
|
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:
|
|
|
|
// %RAX<def> = ...
|
|
|
|
// %RAX<def> = SUBREG_TO_REG 0, %EAX:3<kill>, 3
|
|
|
|
// The first def is defining RAX, not EAX so the top bits were not
|
|
|
|
// zero extended.
|
2008-08-07 02:54:50 +00:00
|
|
|
DOUT << "subreg: eliminated!";
|
|
|
|
} else {
|
|
|
|
// Insert sub-register copy
|
|
|
|
const TargetRegisterClass *TRC0= TRI.getPhysicalRegisterRegClass(DstSubReg);
|
|
|
|
const TargetRegisterClass *TRC1= TRI.getPhysicalRegisterRegClass(InsReg);
|
|
|
|
TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1);
|
2008-12-18 22:14:08 +00:00
|
|
|
// Transfer the kill/dead flags, if needed.
|
|
|
|
if (MI->getOperand(0).isDead())
|
|
|
|
TransferDeadFlag(MI, DstSubReg, TRI);
|
|
|
|
if (MI->getOperand(2).isKill())
|
|
|
|
TransferKillFlag(MI, InsReg, TRI);
|
2007-08-10 21:11:55 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2008-08-20 13:50:12 +00:00
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
|
|
|
DOUT << "subreg: " << *(--dMI);
|
2007-08-10 21:11:55 +00:00
|
|
|
#endif
|
2008-08-07 02:54:50 +00:00
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
DOUT << "\n";
|
2008-07-17 23:49:46 +00:00
|
|
|
MBB->erase(MI);
|
2008-03-16 03:12:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
bool LowerSubregsInstructionPass::LowerInsert(MachineInstr *MI) {
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
MachineFunction &MF = *MBB->getParent();
|
|
|
|
const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
|
|
|
|
const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
|
2008-10-03 15:45:36 +00:00
|
|
|
assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
|
|
|
|
(MI->getOperand(1).isReg() && MI->getOperand(1).isUse()) &&
|
|
|
|
(MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
|
|
|
|
MI->getOperand(3).isImm() && "Invalid insert_subreg");
|
2008-03-16 03:12:01 +00:00
|
|
|
|
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
2008-11-21 20:00:59 +00:00
|
|
|
#ifndef NDEBUG
|
2008-03-16 03:12:01 +00:00
|
|
|
unsigned SrcReg = MI->getOperand(1).getReg();
|
2008-11-21 20:00:59 +00:00
|
|
|
#endif
|
2008-03-16 03:12:01 +00:00
|
|
|
unsigned InsReg = MI->getOperand(2).getReg();
|
|
|
|
unsigned SubIdx = MI->getOperand(3).getImm();
|
2007-08-10 21:11:55 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
assert(DstReg == SrcReg && "insert_subreg not a two-address instruction?");
|
|
|
|
assert(SubIdx != 0 && "Invalid index for insert_subreg");
|
|
|
|
unsigned DstSubReg = TRI.getSubReg(DstReg, SubIdx);
|
2007-08-10 21:11:55 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
|
|
|
|
"Insert superreg source must be in a physical register");
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
|
|
|
|
"Inserted value must be in a physical register");
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
DOUT << "subreg: CONVERTING: " << *MI;
|
|
|
|
|
2008-06-16 22:52:53 +00:00
|
|
|
if (DstSubReg == InsReg) {
|
|
|
|
// No need to insert an identify copy instruction.
|
|
|
|
DOUT << "subreg: eliminated!";
|
|
|
|
} else {
|
|
|
|
// Insert sub-register copy
|
|
|
|
const TargetRegisterClass *TRC0= TRI.getPhysicalRegisterRegClass(DstSubReg);
|
|
|
|
const TargetRegisterClass *TRC1= TRI.getPhysicalRegisterRegClass(InsReg);
|
|
|
|
TII.copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1);
|
2008-12-18 22:14:08 +00:00
|
|
|
// Transfer the kill/dead flags, if needed.
|
|
|
|
if (MI->getOperand(0).isDead())
|
|
|
|
TransferDeadFlag(MI, DstSubReg, TRI);
|
|
|
|
if (MI->getOperand(1).isKill())
|
|
|
|
TransferKillFlag(MI, InsReg, TRI);
|
2008-12-18 22:11:34 +00:00
|
|
|
|
2007-08-10 21:11:55 +00:00
|
|
|
#ifndef NDEBUG
|
2008-06-16 22:52:53 +00:00
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
|
|
|
DOUT << "subreg: " << *(--dMI);
|
2007-08-10 21:11:55 +00:00
|
|
|
#endif
|
2008-06-16 22:52:53 +00:00
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
|
|
|
DOUT << "\n";
|
2008-07-17 23:49:46 +00:00
|
|
|
MBB->erase(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);
|
2008-03-16 03:12:01 +00:00
|
|
|
} else if (MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) {
|
|
|
|
MadeChange |= LowerSubregToReg(MI);
|
2007-07-26 08:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|