Fix rewriting of PHI nodes.
[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/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   const BasicBlock* srcBB = src->getBasicBlock();
26
27   MachineBasicBlock* crit_mbb = new MachineBasicBlock(srcBB);
28
29   // modify the llvm control flow graph
30   src->removeSuccessor(dst);
31   src->addSuccessor(crit_mbb);
32   crit_mbb->addSuccessor(dst);
33
34   // insert the new block into the machine function.
35   src->getParent()->getBasicBlockList().insert(src->getParent()->end(),
36                                                crit_mbb);
37
38   // insert a unconditional branch linking the new block to dst
39   const TargetMachine& TM = src->getParent()->getTarget();
40   const TargetInstrInfo* TII = TM.getInstrInfo();
41   std::vector<MachineOperand> emptyConditions;
42   TII->InsertBranch(*crit_mbb, dst, (MachineBasicBlock*)0, emptyConditions);
43
44   // modify every branch in src that points to dst to point to the new
45   // machine basic block instead:
46   MachineBasicBlock::iterator mii = src->end();
47   bool found_branch = false;
48   while (mii != src->begin()) {
49     mii--;
50     // if there are no more branches, finish the loop
51     if (!TII->isTerminatorInstr(mii->getOpcode())) {
52       break;
53     }
54     
55     // Scan the operands of this branch, finding all uses of this MBB
56     std::vector<unsigned> toRemove;
57     unsigned reg = 0;
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         reg = mii->getOperand(i-1).getReg();
63         toRemove.push_back(i-1);
64     }
65     
66     // Remove all uses of this MBB
67     for (std::vector<unsigned>::reverse_iterator I = toRemove.rbegin(),
68          E = toRemove.rend(); I != E; ++I) {
69       mii->RemoveOperand(*I+1);
70       mii->RemoveOperand(*I);
71     }
72     
73     // Add a single use corresponding to the new MBB
74     mii->addRegOperand(reg, false);
75     mii->addMachineBasicBlockOperand(crit_mbb);
76   }
77
78   // TODO: This is tentative. It may be necessary to fix this code. Maybe
79   // I am inserting too many gotos, but I am trusting that the asm printer
80   // will optimize the unnecessary gotos.
81   if(!found_branch) {
82     TII->InsertBranch(*src, crit_mbb, (MachineBasicBlock*)0, emptyConditions);
83   }
84
85   /// Change all the phi functions in dst, so that the incoming block be
86   /// crit_mbb, instead of src
87   for(mii = dst->begin(); mii != dst->end(); mii++) {
88     /// the first instructions are always phi functions.
89     if(mii->getOpcode() != TargetInstrInfo::PHI)
90       break;
91     
92     for (unsigned u = 0; u != mii->getNumOperands(); ++u)
93       if (mii->getOperand(u).isMachineBasicBlock() &&
94           mii->getOperand(u).getMachineBasicBlock() == src)
95         mii->getOperand(u).setMachineBasicBlock(crit_mbb);
96   }
97   
98   return crit_mbb;
99 }
100
101 }
102
103 #endif