llvm.org GIT mirror llvm / fe0c882
As Chris and Evan pointed out, BreakCriticalMachineEdges doesn't really need to be a pass of its own. Instead, move it out into a helper method. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44002 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 12 years ago
5 changed file(s) with 95 addition(s) and 136 deletion(s). Raw diff Collapse all Expand all
0 //===--------- BreakCriticalMachineEdges.h - 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 // Helper function to break a critical machine edge.
10 //
11 //===---------------------------------------------------------------------===//
12
13 #ifndef LLVM_CODEGEN_ASMPRINTER_H
14 #define LLVM_CODEGEN_ASMPRINTER_H
15
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineJumpTableInfo.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Support/Compiler.h"
21
22 namespace llvm {
23
24 MachineBasicBlock* SplitCriticalMachineEdge(MachineBasicBlock* src,
25 MachineBasicBlock* dst) {
26 const BasicBlock* srcBB = src->getBasicBlock();
27
28 MachineBasicBlock* crit_mbb = new MachineBasicBlock(srcBB);
29
30 // modify the llvm control flow graph
31 src->removeSuccessor(dst);
32 src->addSuccessor(crit_mbb);
33 crit_mbb->addSuccessor(dst);
34
35 // insert the new block into the machine function.
36 src->getParent()->getBasicBlockList().insert(src->getParent()->end(),
37 crit_mbb);
38
39 // insert a unconditional branch linking the new block to dst
40 const TargetMachine& TM = src->getParent()->getTarget();
41 const TargetInstrInfo* TII = TM.getInstrInfo();
42 std::vector emptyConditions;
43 TII->InsertBranch(*crit_mbb, dst, (MachineBasicBlock*)0, emptyConditions);
44
45 // modify every branch in src that points to dst to point to the new
46 // machine basic block instead:
47 MachineBasicBlock::iterator mii = src->end();
48 bool found_branch = false;
49 while (mii != src->begin()) {
50 mii--;
51 // if there are no more branches, finish the loop
52 if (!TII->isTerminatorInstr(mii->getOpcode())) {
53 break;
54 }
55
56 // Scan the operands of this branch, replacing any uses of dst with
57 // crit_mbb.
58 for (unsigned i = 0, e = mii->getNumOperands(); i != e; ++i) {
59 MachineOperand & mo = mii->getOperand(i);
60 if (mo.isMachineBasicBlock() &&
61 mo.getMachineBasicBlock() == dst) {
62 found_branch = true;
63 mo.setMachineBasicBlock(crit_mbb);
64 }
65 }
66 }
67
68 // TODO: This is tentative. It may be necessary to fix this code. Maybe
69 // I am inserting too many gotos, but I am trusting that the asm printer
70 // will optimize the unnecessary gotos.
71 if(!found_branch) {
72 TII->InsertBranch(*src, crit_mbb, (MachineBasicBlock*)0, emptyConditions);
73 }
74
75 /// Change all the phi functions in dst, so that the incoming block be
76 /// crit_mbb, instead of src
77 for(mii = dst->begin(); mii != dst->end(); mii++) {
78 /// the first instructions are always phi functions.
79 if(mii->getOpcode() != TargetInstrInfo::PHI)
80 break;
81
82 for (unsigned u = 0; u != mii->getNumOperands(); ++u)
83 if (mii->getOperand(u).isMachineBasicBlock() &&
84 mii->getOperand(u).getMachineBasicBlock() == src)
85 mii->getOperand(u).setMachineBasicBlock(crit_mbb);
86 }
87
88 return crit_mbb;
89 }
90
91 }
92
93 #endif
5656 /// copy it can.
5757 ///
5858 extern const PassInfo *SimpleRegisterCoalescingID;
59
60 /// BreakCriticalMachineEdges pass. Breaks critical edges between
61 /// machine basic blocks.
62 extern const PassInfo *BreakCriticalMachineEdgesID;
6359
6460 /// TwoAddressInstruction pass - This pass reduces two-address instructions to
6561 /// use two operands. This destroys SSA information but it is desired by
+0
-131
lib/CodeGen/BreakCriticalMachineEdges.cpp less more
None //===----------- 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(); u++)
127 splitCriticalEdge(SourceBlocks[u], DestBlocks[u]);
128
129 return false;
130 }
2020
2121 #define DEBUG_TYPE "strongphielim"
2222 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/CodeGen/BreakCriticalMachineEdge.h"
2324 #include "llvm/CodeGen/LiveVariables.h"
2425 #include "llvm/CodeGen/MachineDominators.h"
2526 #include "llvm/CodeGen/MachineFunctionPass.h"
6969 AU.addRequired();
7070 AU.addPreserved();
7171 AU.addPreservedID(PHIEliminationID);
72 AU.addPreservedID(BreakCriticalMachineEdgesID);
7372 MachineFunctionPass::getAnalysisUsage(AU);
7473 }
7574