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
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-11-08 07:55:43 +00:00
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
//
|
2007-11-12 01:05:09 +00:00
|
|
|
// Helper function to break a critical machine edge.
|
2007-11-08 07:55:43 +00:00
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
2008-07-16 16:04:07 +00:00
|
|
|
#ifndef LLVM_CODEGEN_BREAKCRITICALMACHINEEDGES_H
|
|
|
|
#define LLVM_CODEGEN_BREAKCRITICALMACHINEEDGES_H
|
2007-11-12 01:05:09 +00:00
|
|
|
|
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) {
|
2008-07-07 23:14:23 +00:00
|
|
|
MachineFunction &MF = *src->getParent();
|
2007-11-08 07:55:43 +00:00
|
|
|
const BasicBlock* srcBB = src->getBasicBlock();
|
|
|
|
|
2008-07-07 23:14:23 +00:00
|
|
|
MachineBasicBlock* crit_mbb = MF.CreateMachineBasicBlock(srcBB);
|
2007-11-08 07:55:43 +00:00
|
|
|
|
|
|
|
// 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.
|
2008-07-07 23:14:23 +00:00
|
|
|
MF.push_back(crit_mbb);
|
2007-11-08 07:55:43 +00:00
|
|
|
|
|
|
|
// insert a unconditional branch linking the new block to dst
|
2008-07-07 23:14:23 +00:00
|
|
|
const TargetMachine& TM = MF.getTarget();
|
2007-11-08 07:55:43 +00:00
|
|
|
const TargetInstrInfo* TII = TM.getInstrInfo();
|
|
|
|
std::vector<MachineOperand> emptyConditions;
|
2008-01-27 19:51:03 +00:00
|
|
|
TII->InsertBranch(*crit_mbb, dst, (MachineBasicBlock*)0,
|
|
|
|
emptyConditions);
|
2007-11-08 07:55:43 +00:00
|
|
|
|
|
|
|
// 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
|
2008-01-27 19:51:03 +00:00
|
|
|
if (!mii->getDesc().isTerminator()) {
|
2007-11-08 07:55:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-01-27 19:51:03 +00:00
|
|
|
|
2007-11-13 19:56:28 +00:00
|
|
|
// Scan the operands of this branch, replacing any uses of dst with
|
|
|
|
// crit_mbb.
|
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() &&
|
2008-01-27 19:51:03 +00:00
|
|
|
mo.getMBB() == dst) {
|
2007-11-13 19:56:28 +00:00
|
|
|
found_branch = true;
|
2008-01-27 19:51:03 +00:00
|
|
|
mo.setMBB(crit_mbb);
|
2007-11-13 19:56:28 +00:00
|
|
|
}
|
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) {
|
2008-01-27 19:51:03 +00:00
|
|
|
TII->InsertBranch(*src, crit_mbb, (MachineBasicBlock*)0,
|
|
|
|
emptyConditions);
|
2007-11-08 07:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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;
|
2008-01-27 19:51:03 +00:00
|
|
|
|
2007-11-13 19:56:28 +00:00
|
|
|
// Find the operands corresponding to the source block
|
|
|
|
std::vector<unsigned> toRemove;
|
|
|
|
unsigned reg = 0;
|
2007-11-08 07:55:43 +00:00
|
|
|
for (unsigned u = 0; u != mii->getNumOperands(); ++u)
|
|
|
|
if (mii->getOperand(u).isMachineBasicBlock() &&
|
2008-01-27 19:51:03 +00:00
|
|
|
mii->getOperand(u).getMBB() == src) {
|
2007-11-13 19:56:28 +00:00
|
|
|
reg = mii->getOperand(u-1).getReg();
|
|
|
|
toRemove.push_back(u-1);
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
}
|
2008-01-27 19:51:03 +00:00
|
|
|
|
2007-11-13 19:56:28 +00:00
|
|
|
// Add a single use corresponding to the new MBB
|
2008-01-27 19:51:03 +00:00
|
|
|
mii->addOperand(MachineOperand::CreateReg(reg, false));
|
|
|
|
mii->addOperand(MachineOperand::CreateMBB(crit_mbb));
|
2007-11-08 07:55:43 +00:00
|
|
|
}
|
2008-01-27 19:51:03 +00:00
|
|
|
|
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
|