//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "twoaddrinstr"
+#include "llvm/Function.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/LiveVariables.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/Target/TargetMachine.h"
#include "Support/Debug.h"
#include "Support/Statistic.h"
+#include "Support/STLExtras.h"
#include <iostream>
using namespace llvm;
void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const
{
AU.addPreserved<LiveVariables>();
- AU.addRequired<LiveVariables>();
AU.addPreservedID(PHIEliminationID);
- AU.addRequiredID(PHIEliminationID);
MachineFunctionPass::getAnalysisUsage(AU);
}
const TargetMachine &TM = MF.getTarget();
const MRegisterInfo &MRI = *TM.getRegisterInfo();
const TargetInstrInfo &TII = TM.getInstrInfo();
- LiveVariables &LV = getAnalysis<LiveVariables>();
+ LiveVariables* LV = getAnalysisToUpdate<LiveVariables>();
bool MadeChange = false;
+ DEBUG(std::cerr << "********** REWRITING TWO-ADDR INSTRS **********\n");
+ DEBUG(std::cerr << "********** 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();
++numTwoAddressInstrs;
- DEBUG(std::cerr << "\tinstruction: "; mi->print(std::cerr, TM));
+ DEBUG(std::cerr << '\t'; mi->print(std::cerr, TM));
assert(mi->getOperand(1).isRegister() &&
mi->getOperand(1).getReg() &&
unsigned Added = MRI.copyRegToReg(*mbbi, mi, regA, regB, rc);
numInstrsAdded += Added;
- MachineBasicBlock::iterator prevMi = mi;
- --prevMi;
- DEBUG(std::cerr << "\t\tadded instruction: ";
+ MachineBasicBlock::iterator prevMi = prior(mi);
+ DEBUG(std::cerr << "\t\tprepend:\t";
prevMi->print(std::cerr, TM));
- // update live variables for regA
- assert(Added == 1 &&
- "Cannot handle multi-instruction copies yet!");
- LiveVariables::VarInfo& varInfo = LV.getVarInfo(regA);
- varInfo.DefInst = prevMi;
+ 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;
- // update live variables for regB
- if (LV.removeVirtualRegisterKilled(regB, &*mbbi, mi))
- LV.addVirtualRegisterKilled(regB, &*mbbi, prevMi);
+ // update live variables for regB
+ if (LV->removeVirtualRegisterKilled(regB, &*mbbi, mi))
+ LV->addVirtualRegisterKilled(regB, &*mbbi, prevMi);
- if (LV.removeVirtualRegisterDead(regB, &*mbbi, mi))
- LV.addVirtualRegisterDead(regB, &*mbbi, prevMi);
+ if (LV->removeVirtualRegisterDead(regB, &*mbbi, mi))
+ LV->addVirtualRegisterDead(regB, &*mbbi, prevMi);
+ }
// replace all occurences of regB with regA
for (unsigned i = 1, e = mi->getNumOperands(); i != e; ++i) {
mi->getOperand(0).setUse();
mi->RemoveOperand(1);
- DEBUG(std::cerr << "\t\tmodified original to: ";
+ DEBUG(std::cerr << "\t\trewrite to:\t";
mi->print(std::cerr, TM));
}
}