c02eaa784a9e91a742bef48a8cf3ca8d0a2bfe0b
[oota-llvm.git] / include / llvm / CodeGen / BreakCriticalMachineEdge.h
1 //===--------- BreakCriticalMachineEdges.h - Break critical edges ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Fernando Pereira and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===---------------------------------------------------------------------===//
9 //
10 // Helper function to break a critical machine edge.
11 //
12 //===---------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_ASMPRINTER_H
15 #define LLVM_CODEGEN_ASMPRINTER_H
16
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/MachineJumpTableInfo.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Support/Compiler.h"
22
23 namespace llvm {
24
25 MachineBasicBlock* SplitCriticalMachineEdge(MachineBasicBlock* src,
26                                             MachineBasicBlock* dst) {
27   const BasicBlock* srcBB = src->getBasicBlock();
28
29   MachineBasicBlock* crit_mbb = new MachineBasicBlock(srcBB);
30
31   // modify the llvm control flow graph
32   src->removeSuccessor(dst);
33   src->addSuccessor(crit_mbb);
34   crit_mbb->addSuccessor(dst);
35
36   // insert the new block into the machine function.
37   src->getParent()->getBasicBlockList().insert(src->getParent()->end(),
38                                                crit_mbb);
39
40   // insert a unconditional branch linking the new block to dst
41   const TargetMachine& TM = src->getParent()->getTarget();
42   const TargetInstrInfo* TII = TM.getInstrInfo();
43   std::vector<MachineOperand> emptyConditions;
44   TII->InsertBranch(*crit_mbb, dst, (MachineBasicBlock*)0, emptyConditions);
45
46   // modify every branch in src that points to dst to point to the new
47   // machine basic block instead:
48   MachineBasicBlock::iterator mii = src->end();
49   bool found_branch = false;
50   while (mii != src->begin()) {
51     mii--;
52     // if there are no more branches, finish the loop
53     if (!TII->isTerminatorInstr(mii->getOpcode())) {
54       break;
55     }
56     
57     // Scan the operands of this branch, replacing any uses of dst with
58     // crit_mbb.
59     for (unsigned i = 0, e = mii->getNumOperands(); i != e; ++i) {
60       MachineOperand & mo = mii->getOperand(i);
61       if (mo.isMachineBasicBlock() &&
62           mo.getMachineBasicBlock() == dst) {
63         found_branch = true;
64         mo.setMachineBasicBlock(crit_mbb);
65       }
66     }
67   }
68
69   // TODO: This is tentative. It may be necessary to fix this code. Maybe
70   // I am inserting too many gotos, but I am trusting that the asm printer
71   // will optimize the unnecessary gotos.
72   if(!found_branch) {
73     TII->InsertBranch(*src, crit_mbb, (MachineBasicBlock*)0, emptyConditions);
74   }
75
76   /// Change all the phi functions in dst, so that the incoming block be
77   /// crit_mbb, instead of src
78   for(mii = dst->begin(); mii != dst->end(); mii++) {
79     /// the first instructions are always phi functions.
80     if(mii->getOpcode() != TargetInstrInfo::PHI)
81       break;
82     
83     for (unsigned u = 0; u != mii->getNumOperands(); ++u)
84       if (mii->getOperand(u).isMachineBasicBlock() &&
85           mii->getOperand(u).getMachineBasicBlock() == src)
86         mii->getOperand(u).setMachineBasicBlock(crit_mbb);
87   }
88   
89   return crit_mbb;
90 }
91
92 }
93
94 #endif