llvm.org GIT mirror llvm / 01f43a4
Add the majority of machine-level critical edge breaking pass. Most of this was written by Fernando, cleanup and updating to TOT by me. This still needs a bit of work, particularly to handle jump tables properly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43885 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 12 years ago
1 changed file(s) with 131 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===----------- BreakCriticalMachineEdges - Break critical edges ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Fernando Pereira and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===---------------------------------------------------------------------===//
8 //
9 // Break all of the critical edges in the CFG by inserting a dummy basic block.
10 // This pass may be "required" by passes that cannot deal with critical edges.
11 // Notice that this pass invalidates the CFG, because the same BasicBlock is
12 // used as parameter for the src MachineBasicBlock and the new dummy
13 // MachineBasicBlock.
14 //
15 //===---------------------------------------------------------------------===//
16
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineJumpTableInfo.h"
21 #include "llvm/Target/TargetInstrInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Support/Compiler.h"
25
26 using namespace llvm;
27
28 namespace {
29 struct VISIBILITY_HIDDEN BreakCriticalMachineEdges :
30 public MachineFunctionPass {
31 static char ID; // Pass identification
32 BreakCriticalMachineEdges() : MachineFunctionPass((intptr_t)&ID) {}
33
34 bool runOnMachineFunction(MachineFunction& Fn);
35 void splitCriticalEdge(MachineBasicBlock* A, MachineBasicBlock* B);
36 };
37
38 char BreakCriticalMachineEdges::ID = 0;
39 RegisterPass X("critical-machine-edges",
40 "Break critical machine code edges");
41 }
42
43 //const PassInfo *llvm::BreakCriticalMachineEdgesID = X.getPassInfo();
44
45 void BreakCriticalMachineEdges::splitCriticalEdge(MachineBasicBlock* src,
46 MachineBasicBlock* dst) {
47 const BasicBlock* srcBB = src->getBasicBlock();
48
49 MachineBasicBlock* crit_mbb = new MachineBasicBlock(srcBB);
50
51 // modify the llvm control flow graph
52 src->removeSuccessor(dst);
53 src->addSuccessor(crit_mbb);
54 crit_mbb->addSuccessor(dst);
55
56 // insert the new block into the machine function.
57 src->getParent()->getBasicBlockList().insert(src->getParent()->end(),
58 crit_mbb);
59
60 // insert a unconditional branch linking the new block to dst
61 const TargetMachine& TM = src->getParent()->getTarget();
62 const TargetInstrInfo* TII = TM.getInstrInfo();
63 std::vector emptyConditions;
64 TII->InsertBranch(*crit_mbb, dst, (MachineBasicBlock*)0, emptyConditions);
65
66 // modify every branch in src that points to dst to point to the new
67 // machine basic block instead:
68 MachineBasicBlock::iterator mii = src->end();
69 bool found_branch = false;
70 while (mii != src->begin()) {
71 mii--;
72 // if there are no more branches, finish the loop
73 if (!TII->isTerminatorInstr(mii->getOpcode())) {
74 break;
75 }
76
77 // Scan the operands of this branch, replacing any uses of dst with
78 // crit_mbb.
79 for (unsigned i = 0, e = mii->getNumOperands(); i != e; ++i) {
80 MachineOperand & mo = mii->getOperand(i);
81 if (mo.isMachineBasicBlock() &&
82 mo.getMachineBasicBlock() == dst) {
83 found_branch = true;
84 mo.setMachineBasicBlock(crit_mbb);
85 }
86 }
87 }
88
89 // TODO: This is tentative. It may be necessary to fix this code. Maybe
90 // I am inserting too many gotos, but I am trusting that the asm printer
91 // will optimize the unnecessary gotos.
92 if(!found_branch) {
93 TII->InsertBranch(*src, crit_mbb, (MachineBasicBlock*)0, emptyConditions);
94 }
95
96 /// Change all the phi functions in dst, so that the incoming block be
97 /// crit_mbb, instead of src
98 for(mii = dst->begin(); mii != dst->end(); mii++) {
99 /// the first instructions are always phi functions.
100 if(mii->getOpcode() != TargetInstrInfo::PHI)
101 break;
102
103 for (unsigned u = 0; u != mii->getNumOperands(); ++u)
104 if (mii->getOperand(u).isMachineBasicBlock() &&
105 mii->getOperand(u).getMachineBasicBlock() == src)
106 mii->getOperand(u).setMachineBasicBlock(crit_mbb);
107 }
108 }
109
110 bool BreakCriticalMachineEdges::runOnMachineFunction(MachineFunction& F) {
111 std::vector SourceBlocks;
112 std::vector DestBlocks;
113
114 for(MachineFunction::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
115 for(MachineBasicBlock::succ_iterator SI = FI->succ_begin(),
116 SE = FI->succ_end(); SI != SE; ++SI) {
117 // predecessor with multiple successors, successor with multiple
118 // predecessors.
119 if (FI->succ_size() > 1 && (*SI)->pred_size() > 1) {
120 SourceBlocks.push_back(FI);
121 DestBlocks.push_back(*SI);
122 }
123 }
124 }
125
126 for(unsigned u = 0; u < SourceBlocks.size() > 0; u++)
127 splitCriticalEdge(SourceBlocks[u], DestBlocks[u]);
128
129 return false;
130 }