2007-11-12 01:05:09 +00:00
|
|
|
//===--------- BreakCriticalMachineEdges.h - Break critical edges ---------===//
|
2007-11-08 07:55:43 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Fernando Pereira and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
//
|
2007-11-12 01:05:09 +00:00
|
|
|
// Helper function to break a critical machine edge.
|
2007-11-08 07:55:43 +00:00
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
2007-11-12 01:05:09 +00:00
|
|
|
#ifndef LLVM_CODEGEN_ASMPRINTER_H
|
|
|
|
#define LLVM_CODEGEN_ASMPRINTER_H
|
|
|
|
|
2007-11-08 07:55:43 +00:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
|
2007-11-12 01:05:09 +00:00
|
|
|
namespace llvm {
|
2007-11-08 07:55:43 +00:00
|
|
|
|
2007-11-12 01:05:09 +00:00
|
|
|
MachineBasicBlock* SplitCriticalMachineEdge(MachineBasicBlock* src,
|
|
|
|
MachineBasicBlock* dst) {
|
2007-11-08 07:55:43 +00:00
|
|
|
const BasicBlock* srcBB = src->getBasicBlock();
|
|
|
|
|
|
|
|
MachineBasicBlock* crit_mbb = new MachineBasicBlock(srcBB);
|
|
|
|
|
|
|
|
// modify the llvm control flow graph
|
|
|
|
src->removeSuccessor(dst);
|
|
|
|
src->addSuccessor(crit_mbb);
|
|
|
|
crit_mbb->addSuccessor(dst);
|
|
|
|
|
|
|
|
// insert the new block into the machine function.
|
|
|
|
src->getParent()->getBasicBlockList().insert(src->getParent()->end(),
|
|
|
|
crit_mbb);
|
|
|
|
|
|
|
|
// insert a unconditional branch linking the new block to dst
|
|
|
|
const TargetMachine& TM = src->getParent()->getTarget();
|
|
|
|
const TargetInstrInfo* TII = TM.getInstrInfo();
|
|
|
|
std::vector<MachineOperand> emptyConditions;
|
|
|
|
TII->InsertBranch(*crit_mbb, dst, (MachineBasicBlock*)0, emptyConditions);
|
|
|
|
|
|
|
|
// modify every branch in src that points to dst to point to the new
|
|
|
|
// machine basic block instead:
|
|
|
|
MachineBasicBlock::iterator mii = src->end();
|
|
|
|
bool found_branch = false;
|
|
|
|
while (mii != src->begin()) {
|
|
|
|
mii--;
|
|
|
|
// if there are no more branches, finish the loop
|
|
|
|
if (!TII->isTerminatorInstr(mii->getOpcode())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-11-12 02:13:07 +00:00
|
|
|
// Scan the operands of this branch, finding all uses of this MBB
|
|
|
|
std::vector<unsigned> toRemove;
|
|
|
|
unsigned reg = 0;
|
2007-11-08 07:55:43 +00:00
|
|
|
for (unsigned i = 0, e = mii->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand & mo = mii->getOperand(i);
|
|
|
|
if (mo.isMachineBasicBlock() &&
|
2007-11-12 02:13:07 +00:00
|
|
|
mo.getMachineBasicBlock() == dst)
|
|
|
|
reg = mii->getOperand(i-1).getReg();
|
|
|
|
toRemove.push_back(i-1);
|
2007-11-08 07:55:43 +00:00
|
|
|
}
|
2007-11-12 02:13:07 +00:00
|
|
|
|
|
|
|
// Remove all uses of this MBB
|
|
|
|
for (std::vector<unsigned>::reverse_iterator I = toRemove.rbegin(),
|
|
|
|
E = toRemove.rend(); I != E; ++I) {
|
|
|
|
mii->RemoveOperand(*I+1);
|
|
|
|
mii->RemoveOperand(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a single use corresponding to the new MBB
|
|
|
|
mii->addRegOperand(reg, false);
|
|
|
|
mii->addMachineBasicBlockOperand(crit_mbb);
|
2007-11-08 07:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This is tentative. It may be necessary to fix this code. Maybe
|
|
|
|
// I am inserting too many gotos, but I am trusting that the asm printer
|
|
|
|
// will optimize the unnecessary gotos.
|
|
|
|
if(!found_branch) {
|
|
|
|
TII->InsertBranch(*src, crit_mbb, (MachineBasicBlock*)0, emptyConditions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Change all the phi functions in dst, so that the incoming block be
|
|
|
|
/// crit_mbb, instead of src
|
|
|
|
for(mii = dst->begin(); mii != dst->end(); mii++) {
|
|
|
|
/// the first instructions are always phi functions.
|
|
|
|
if(mii->getOpcode() != TargetInstrInfo::PHI)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (unsigned u = 0; u != mii->getNumOperands(); ++u)
|
|
|
|
if (mii->getOperand(u).isMachineBasicBlock() &&
|
|
|
|
mii->getOperand(u).getMachineBasicBlock() == src)
|
|
|
|
mii->getOperand(u).setMachineBasicBlock(crit_mbb);
|
|
|
|
}
|
2007-11-12 01:05:09 +00:00
|
|
|
|
|
|
|
return crit_mbb;
|
2007-11-08 07:55:43 +00:00
|
|
|
}
|
|
|
|
|
2007-11-09 08:30:21 +00:00
|
|
|
}
|
2007-11-12 01:05:09 +00:00
|
|
|
|
|
|
|
#endif
|