2003-12-18 13:06:04 +00:00
|
|
|
//===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-01-04 23:09:24 +00:00
|
|
|
// This file implements the TwoAddress instruction pass which is used
|
|
|
|
// by most register allocators. Two-Address instructions are rewritten
|
|
|
|
// from:
|
|
|
|
//
|
|
|
|
// A = B op C
|
|
|
|
//
|
|
|
|
// to:
|
|
|
|
//
|
|
|
|
// A = B
|
2004-02-04 22:17:40 +00:00
|
|
|
// A op= C
|
2003-12-18 13:06:04 +00:00
|
|
|
//
|
2004-02-04 22:17:40 +00:00
|
|
|
// Note that if a register allocator chooses to use this pass, that it
|
|
|
|
// has to be capable of handling the non-SSA nature of these rewritten
|
|
|
|
// virtual registers.
|
|
|
|
//
|
|
|
|
// It is also worth noting that the duplicate operand of the two
|
|
|
|
// address instruction is removed.
|
2004-01-31 21:07:15 +00:00
|
|
|
//
|
2003-12-18 13:06:04 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "twoaddrinstr"
|
2004-02-18 00:35:06 +00:00
|
|
|
#include "llvm/Function.h"
|
2004-01-31 21:07:15 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2003-12-18 13:06:04 +00:00
|
|
|
#include "llvm/CodeGen/LiveVariables.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/SSARegMap.h"
|
|
|
|
#include "llvm/Target/MRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "Support/Debug.h"
|
|
|
|
#include "Support/Statistic.h"
|
2004-02-14 01:18:34 +00:00
|
|
|
#include "Support/STLExtras.h"
|
2004-02-05 05:04:39 +00:00
|
|
|
#include <iostream>
|
2003-12-18 13:06:04 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2004-01-31 21:07:15 +00:00
|
|
|
Statistic<> numTwoAddressInstrs("twoaddressinstruction",
|
|
|
|
"Number of two-address instructions");
|
|
|
|
Statistic<> numInstrsAdded("twoaddressinstruction",
|
|
|
|
"Number of instructions added");
|
|
|
|
|
2004-01-31 21:14:04 +00:00
|
|
|
struct TwoAddressInstructionPass : public MachineFunctionPass
|
2003-12-18 22:40:24 +00:00
|
|
|
{
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
|
|
|
|
/// runOnMachineFunction - pass entry point
|
|
|
|
bool runOnMachineFunction(MachineFunction&);
|
|
|
|
};
|
|
|
|
|
|
|
|
RegisterPass<TwoAddressInstructionPass> X(
|
2003-12-18 13:06:04 +00:00
|
|
|
"twoaddressinstruction", "Two-Address instruction pass");
|
|
|
|
};
|
|
|
|
|
2003-12-18 22:40:24 +00:00
|
|
|
const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
|
|
|
|
|
2003-12-18 13:06:04 +00:00
|
|
|
void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const
|
|
|
|
{
|
|
|
|
AU.addPreserved<LiveVariables>();
|
|
|
|
AU.addPreservedID(PHIEliminationID);
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - Reduce two-address instructions to two
|
2004-01-31 21:14:04 +00:00
|
|
|
/// operands.
|
2003-12-18 13:06:04 +00:00
|
|
|
///
|
2004-01-31 21:14:04 +00:00
|
|
|
bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
|
2003-12-18 13:06:04 +00:00
|
|
|
DEBUG(std::cerr << "Machine Function\n");
|
2004-01-31 21:14:04 +00:00
|
|
|
const TargetMachine &TM = MF.getTarget();
|
|
|
|
const MRegisterInfo &MRI = *TM.getRegisterInfo();
|
|
|
|
const TargetInstrInfo &TII = TM.getInstrInfo();
|
2004-02-15 21:50:32 +00:00
|
|
|
LiveVariables* LV = getAnalysisToUpdate<LiveVariables>();
|
2003-12-18 13:06:04 +00:00
|
|
|
|
2004-01-31 21:14:04 +00:00
|
|
|
bool MadeChange = false;
|
2003-12-18 13:06:04 +00:00
|
|
|
|
2004-02-18 00:35:06 +00:00
|
|
|
DEBUG(std::cerr << "********** REWRITING TWO-ADDR INSTRS **********\n");
|
|
|
|
DEBUG(std::cerr << "********** Function: "
|
|
|
|
<< MF.getFunction()->getName() << '\n');
|
|
|
|
|
2004-01-31 21:14:04 +00:00
|
|
|
for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
|
2003-12-18 13:06:04 +00:00
|
|
|
mbbi != mbbe; ++mbbi) {
|
2004-02-12 02:27:10 +00:00
|
|
|
for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
|
|
|
|
mi != me; ++mi) {
|
2003-12-18 13:06:04 +00:00
|
|
|
unsigned opcode = mi->getOpcode();
|
2004-01-31 21:14:04 +00:00
|
|
|
|
2003-12-18 13:06:04 +00:00
|
|
|
// ignore if it is not a two-address instruction
|
2004-01-31 21:14:04 +00:00
|
|
|
if (!TII.isTwoAddrInstr(opcode))
|
2003-12-18 13:06:04 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
++numTwoAddressInstrs;
|
|
|
|
|
2004-02-18 00:35:06 +00:00
|
|
|
DEBUG(std::cerr << '\t'; mi->print(std::cerr, TM));
|
2003-12-18 13:06:04 +00:00
|
|
|
|
2004-01-31 21:21:43 +00:00
|
|
|
assert(mi->getOperand(1).isRegister() &&
|
2004-02-13 21:01:20 +00:00
|
|
|
mi->getOperand(1).getReg() &&
|
2004-01-31 21:21:43 +00:00
|
|
|
mi->getOperand(1).isUse() &&
|
|
|
|
"two address instruction invalid");
|
|
|
|
|
2004-02-04 22:17:40 +00:00
|
|
|
// if the two operands are the same we just remove the use
|
|
|
|
// and mark the def as def&use
|
2004-02-13 21:01:20 +00:00
|
|
|
if (mi->getOperand(0).getReg() ==
|
|
|
|
mi->getOperand(1).getReg()) {
|
2003-12-18 13:06:04 +00:00
|
|
|
}
|
2004-02-04 22:17:40 +00:00
|
|
|
else {
|
|
|
|
MadeChange = true;
|
|
|
|
|
|
|
|
// rewrite:
|
|
|
|
// a = b op c
|
|
|
|
// to:
|
|
|
|
// a = b
|
|
|
|
// a = a op c
|
2004-02-13 21:01:20 +00:00
|
|
|
unsigned regA = mi->getOperand(0).getReg();
|
|
|
|
unsigned regB = mi->getOperand(1).getReg();
|
2004-02-04 22:17:40 +00:00
|
|
|
|
|
|
|
assert(MRegisterInfo::isVirtualRegister(regA) &&
|
|
|
|
MRegisterInfo::isVirtualRegister(regB) &&
|
|
|
|
"cannot update physical register live information");
|
|
|
|
|
|
|
|
// first make sure we do not have a use of a in the
|
|
|
|
// instruction (a = b + a for example) because our
|
|
|
|
// transformation will not work. This should never occur
|
|
|
|
// because we are in SSA form.
|
|
|
|
for (unsigned i = 1; i != mi->getNumOperands(); ++i)
|
|
|
|
assert(!mi->getOperand(i).isRegister() ||
|
2004-02-13 21:01:20 +00:00
|
|
|
mi->getOperand(i).getReg() != regA);
|
2004-02-04 22:17:40 +00:00
|
|
|
|
|
|
|
const TargetRegisterClass* rc =
|
|
|
|
MF.getSSARegMap()->getRegClass(regA);
|
2004-02-12 02:27:10 +00:00
|
|
|
unsigned Added = MRI.copyRegToReg(*mbbi, mi, regA, regB, rc);
|
2004-02-04 22:17:40 +00:00
|
|
|
numInstrsAdded += Added;
|
|
|
|
|
2004-02-14 01:18:34 +00:00
|
|
|
MachineBasicBlock::iterator prevMi = prior(mi);
|
2004-02-18 00:35:06 +00:00
|
|
|
DEBUG(std::cerr << "\t\tprepend:\t";
|
2004-02-04 22:17:40 +00:00
|
|
|
prevMi->print(std::cerr, TM));
|
|
|
|
|
2004-02-15 21:50:32 +00:00
|
|
|
if (LV) {
|
|
|
|
// update live variables for regA
|
|
|
|
assert(Added == 1 &&
|
|
|
|
"Cannot handle multi-instruction copies yet!");
|
|
|
|
LiveVariables::VarInfo& varInfo = LV->getVarInfo(regA);
|
|
|
|
varInfo.DefInst = prevMi;
|
2004-02-04 22:17:40 +00:00
|
|
|
|
2004-02-15 21:50:32 +00:00
|
|
|
// update live variables for regB
|
|
|
|
if (LV->removeVirtualRegisterKilled(regB, &*mbbi, mi))
|
|
|
|
LV->addVirtualRegisterKilled(regB, &*mbbi, prevMi);
|
2004-02-04 22:17:40 +00:00
|
|
|
|
2004-02-15 21:50:32 +00:00
|
|
|
if (LV->removeVirtualRegisterDead(regB, &*mbbi, mi))
|
|
|
|
LV->addVirtualRegisterDead(regB, &*mbbi, prevMi);
|
|
|
|
}
|
2004-02-04 22:17:40 +00:00
|
|
|
|
|
|
|
// replace all occurences of regB with regA
|
|
|
|
for (unsigned i = 1, e = mi->getNumOperands(); i != e; ++i) {
|
|
|
|
if (mi->getOperand(i).isRegister() &&
|
|
|
|
mi->getOperand(i).getReg() == regB)
|
|
|
|
mi->SetMachineOperandReg(i, regA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(mi->getOperand(0).isDef());
|
|
|
|
mi->getOperand(0).setUse();
|
|
|
|
mi->RemoveOperand(1);
|
|
|
|
|
2004-02-18 00:35:06 +00:00
|
|
|
DEBUG(std::cerr << "\t\trewrite to:\t";
|
2004-01-31 21:14:04 +00:00
|
|
|
mi->print(std::cerr, TM));
|
2003-12-18 13:06:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-31 21:14:04 +00:00
|
|
|
return MadeChange;
|
2003-12-18 13:06:04 +00:00
|
|
|
}
|