Refactor code that finds context for a given die.
[oota-llvm.git] / include / llvm / CodeGen / BreakCriticalMachineEdge.h
1 //===--------- BreakCriticalMachineEdge.h - Break critical edges ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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_BREAKCRITICALMACHINEEDGE_H
15 #define LLVM_CODEGEN_BREAKCRITICALMACHINEEDGE_H
16
17 #include "llvm/CodeGen/MachineJumpTableInfo.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20
21 namespace llvm {
22
23 MachineBasicBlock* SplitCriticalMachineEdge(MachineBasicBlock* src,
24                                             MachineBasicBlock* dst) {
25   MachineFunction &MF = *src->getParent();
26   const BasicBlock* srcBB = src->getBasicBlock();
27
28   MachineBasicBlock* crit_mbb = MF.CreateMachineBasicBlock(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   MF.push_back(crit_mbb);
37
38   // insert a unconditional branch linking the new block to dst
39   const TargetMachine& TM = MF.getTarget();
40   const TargetInstrInfo* TII = TM.getInstrInfo();
41   std::vector<MachineOperand> emptyConditions;
42   TII->InsertBranch(*crit_mbb, dst, (MachineBasicBlock*)0, 
43                     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 (!mii->getDesc().isTerminator()) {
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.isMBB() && mo.getMBB() == dst) {
61         found_branch = true;
62         mo.setMBB(crit_mbb);
63       }
64     }
65   }
66
67   // TODO: This is tentative. It may be necessary to fix this code. Maybe
68   // I am inserting too many gotos, but I am trusting that the asm printer
69   // will optimize the unnecessary gotos.
70   if(!found_branch) {
71     TII->InsertBranch(*src, crit_mbb, (MachineBasicBlock*)0, 
72                       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     // Find the operands corresponding to the source block
83     std::vector<unsigned> toRemove;
84     unsigned reg = 0;
85     for (unsigned u = 0; u != mii->getNumOperands(); ++u)
86       if (mii->getOperand(u).isMBB() &&
87           mii->getOperand(u).getMBB() == src) {
88         reg = mii->getOperand(u-1).getReg();
89         toRemove.push_back(u-1);
90       }
91     // Remove all uses of this MBB
92     for (std::vector<unsigned>::reverse_iterator I = toRemove.rbegin(),
93          E = toRemove.rend(); I != E; ++I) {
94       mii->RemoveOperand(*I+1);
95       mii->RemoveOperand(*I);
96     }
97
98     // Add a single use corresponding to the new MBB
99     mii->addOperand(MachineOperand::CreateReg(reg, false));
100     mii->addOperand(MachineOperand::CreateMBB(crit_mbb));
101   }
102
103   return crit_mbb;
104 }
105
106 }
107
108 #endif