llvm.org GIT mirror llvm / bd3ba46
- Fix SelectionDAG to generate correct CFGs. - Add a basic machine-level dead block eliminator. These two have to go together, since many other parts of the code generator are unable to handle the unreachable blocks otherwise created. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54333 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 11 years ago
7 changed file(s) with 128 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
246246 assert(Removed && "Register is not defined by this instruction!");
247247 return true;
248248 }
249
250 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
251 AU.setPreservesAll();
252 }
249
250 void getAnalysisUsage(AnalysisUsage &AU) const;
253251
254252 virtual void releaseMemory() {
255253 VirtRegInfo.clear();
6969 /// use two operands. This destroys SSA information but it is desired by
7070 /// register allocators.
7171 extern const PassInfo *const TwoAddressInstructionPassID;
72
73 /// UnreachableMachineBlockElimination pass - This pass removes unreachable
74 /// machine basic blocks.
75 extern const PassInfo *const UnreachableMachineBlockElimID;
7276
7377 /// Creates a register allocator as the user specified on the command line.
7478 ///
100100 /// DisableJumpTables - This flag indicates jump tables should not be
101101 /// generated.
102102 extern bool DisableJumpTables;
103
104 /// DisableCorrectBranchFolding - This flag indicates whether the instruction
105 /// selector should take care to update the CFG properly when
106 /// folding branches.
107 extern bool DisableCorrectBranchFolding;
103108 } // End llvm namespace
104109
105110 #endif
2828 #include "llvm/CodeGen/LiveVariables.h"
2929 #include "llvm/CodeGen/MachineInstr.h"
3030 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/Passes.h"
3132 #include "llvm/Target/TargetRegisterInfo.h"
3233 #include "llvm/Target/TargetInstrInfo.h"
3334 #include "llvm/Target/TargetMachine.h"
4142
4243 char LiveVariables::ID = 0;
4344 static RegisterPass X("livevars", "Live Variable Analysis");
45
46
47 void LiveVariables::getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.addRequiredID(UnreachableMachineBlockElimID);
49 AU.setPreservesAll();
50 }
4451
4552 void LiveVariables::VarInfo::dump() const {
4653 cerr << " Alive in blocks: ";
16311631 }
16321632 SDValue BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(), Cond,
16331633 DAG.getBasicBlock(CB.TrueBB));
1634 if (CB.FalseBB == NextBlock)
1634
1635 // If the branch was constant folded, fix up the CFG.
1636 if (BrCond.getOpcode() == ISD::BR) {
1637 if (!DisableCorrectBranchFolding)
1638 CurMBB->removeSuccessor(CB.FalseBB);
16351639 DAG.setRoot(BrCond);
1636 else
1637 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond,
1638 DAG.getBasicBlock(CB.FalseBB)));
1640 } else {
1641 // Otherwise, go ahead and insert the false branch.
1642 if (BrCond == getControlRoot())
1643 if (!DisableCorrectBranchFolding)
1644 CurMBB->removeSuccessor(CB.TrueBB);
1645
1646 if (CB.FalseBB == NextBlock)
1647 DAG.setRoot(BrCond);
1648 else
1649 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond,
1650 DAG.getBasicBlock(CB.FalseBB)));
1651 }
16391652 }
16401653
16411654 /// visitJumpTable - Emit JumpTable node in the current MBB
2525 #include "llvm/Function.h"
2626 #include "llvm/Pass.h"
2727 #include "llvm/Type.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
2829 #include "llvm/Support/CFG.h"
2930 #include "llvm/Support/Compiler.h"
31 #include "llvm/Target/TargetInstrInfo.h"
3032 #include "llvm/ADT/DepthFirstIterator.h"
3133 using namespace llvm;
3234
7072 BB->dropAllReferences();
7173 }
7274
73 if (DeadBlocks.empty()) return false;
75 // Actually remove the blocks now.
76 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
77 DeadBlocks[i]->eraseFromParent();
78
79 return DeadBlocks.size();
80 }
81
82
83 namespace {
84 class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
85 public MachineFunctionPass {
86 virtual bool runOnMachineFunction(MachineFunction &F);
87 bool iterateOnFunction(MachineFunction& F);
88
89 public:
90 static char ID; // Pass identification, replacement for typeid
91 UnreachableMachineBlockElim() : MachineFunctionPass((intptr_t)&ID) {}
92 };
93 }
94 char UnreachableMachineBlockElim::ID = 0;
95
96 static RegisterPass
97 Y("unreachable-mbb-elimination",
98 "Remove unreachable machine basic blocks");
99
100 const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
101
102 bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
103 bool changed = true;
104 bool result = false;
105
106 while (changed) {
107 changed = iterateOnFunction(F);
108 result |= changed;
109 }
110
111 if (result)
112 F.RenumberBlocks();
113
114 return result;
115 }
116
117 bool UnreachableMachineBlockElim::iterateOnFunction(MachineFunction &F) {
118 std::set Reachable;
119
120 // Mark all reachable blocks.
121 for (df_ext_iterator I = df_ext_begin(&F, Reachable),
122 E = df_ext_end(&F, Reachable); I != E; ++I)
123 /* Mark all reachable blocks */;
124
125 // Loop over all dead blocks, remembering them and deleting all instructions
126 // in them.
127 std::vector DeadBlocks;
128 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I)
129 if (!Reachable.count(I)) {
130 MachineBasicBlock *BB = I;
131 DeadBlocks.push_back(BB);
132
133 while (BB->succ_begin() != BB->succ_end()) {
134 MachineBasicBlock* succ = *BB->succ_begin();
135
136 MachineBasicBlock::iterator start = succ->begin();
137 while (start != succ->end() &&
138 start->getOpcode() == TargetInstrInfo::PHI) {
139 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
140 if (start->getOperand(i).isMBB() &&
141 start->getOperand(i).getMBB() == BB) {
142 start->RemoveOperand(i);
143 start->RemoveOperand(i-1);
144 }
145
146 if (start->getNumOperands() == 1) {
147 MachineInstr* phi = start;
148 start++;
149 phi->eraseFromParent();
150 } else
151 start++;
152 }
153
154 BB->removeSuccessor(BB->succ_begin());
155 }
156 }
74157
75158 // Actually remove the blocks now.
76159 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
77 F.getBasicBlockList().erase(DeadBlocks[i]);
160 DeadBlocks[i]->eraseFromParent();
78161
79 return true;
162 return DeadBlocks.size();
80163 }
164
3939 bool RealignStack;
4040 bool VerboseAsm;
4141 bool DisableJumpTables;
42 bool DisableCorrectBranchFolding;
4243 }
4344
4445 static cl::opt PrintCode("print-machineinstrs",
162163 cl::location(DisableJumpTables),
163164 cl::init(false));
164165
166 static cl::opt
167 DisableCorrectISelFolding(cl::Hidden, "disable-correct-folding",
168 cl::desc("Do not perform correct branch folding in the instruction selector."),
169 cl::location(DisableCorrectBranchFolding), cl::init(false));
170
165171 //---------------------------------------------------------------------------
166172 // TargetMachine Class
167173 //