Changes For Bug 352
[oota-llvm.git] / lib / Target / SparcV9 / LiveVar / BBLiveVar.cpp
1 //===-- BBLiveVar.cpp - Live Variable Analysis for a BasicBlock -----------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This is a wrapper class for BasicBlock which is used by live var analysis.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "BBLiveVar.h"
15 #include "FunctionLiveVarInfo.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/Support/CFG.h"
19 #include "llvm/ADT/SetOperations.h"
20 #include "../SparcV9Internals.h"
21 #include <iostream>
22
23 namespace llvm {
24
25 BBLiveVar::BBLiveVar(const BasicBlock &bb,
26                      const MachineBasicBlock &mbb,
27                      unsigned id)
28   : BB(bb), MBB(mbb), POID(id) {
29   InSetChanged = OutSetChanged = false;
30
31   calcDefUseSets();
32 }
33
34 //-----------------------------------------------------------------------------
35 // calculates def and use sets for each BB
36 // There are two passes over operands of a machine instruction. This is
37 // because, we can have instructions like V = V + 1, since we no longer
38 // assume single definition.
39 //-----------------------------------------------------------------------------
40
41 void BBLiveVar::calcDefUseSets() {
42   // iterate over all the machine instructions in BB
43   for (MachineBasicBlock::const_reverse_iterator MII = MBB.rbegin(),
44          MIE = MBB.rend(); MII != MIE; ++MII) {
45     const MachineInstr *MI = &*MII;
46     
47     if (DEBUG_LV >= LV_DEBUG_Verbose) {
48       std::cerr << " *Iterating over machine instr ";
49       MI->dump();
50       std::cerr << "\n";
51     }
52
53     // iterate over  MI operands to find defs
54     for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
55          OpI != OpE; ++OpI)
56       if (OpI.isDef()) // add to Defs if this operand is a def
57         addDef(*OpI);
58
59     // do for implicit operands as well
60     for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i)
61       if (MI->getImplicitOp(i).isDef())
62         addDef(MI->getImplicitRef(i));
63     
64     // iterate over MI operands to find uses
65     for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
66          OpI != OpE; ++OpI) {
67       const Value *Op = *OpI;
68
69       if (isa<BasicBlock>(Op))
70         continue;             // don't process labels
71
72       if (OpI.isUse()) { // add to Uses only if this operand is a use
73         //
74         // *** WARNING: The following code for handling dummy PHI machine
75         //     instructions is untested.  The previous code was broken and I
76         //     fixed it, but it turned out to be unused as long as Phi
77         //     elimination is performed during instruction selection.
78         // 
79         // Put Phi operands in UseSet for the incoming edge, not node.
80         // They must not "hide" later defs, and must be handled specially
81         // during set propagation over the CFG.
82         if (MI->getOpcode() == V9::PHI) {         // for a phi node
83           const Value *ArgVal = Op;
84           const BasicBlock *PredBB = cast<BasicBlock>(*++OpI); // next ptr is BB
85           
86           PredToEdgeInSetMap[PredBB].insert(ArgVal); 
87           
88           if (DEBUG_LV >= LV_DEBUG_Verbose)
89             std::cerr << "   - phi operand " << RAV(ArgVal) << " came from BB "
90                       << RAV(PredBB) << "\n";
91         } // if( IsPhi )
92         else {
93           // It is not a Phi use: add to regular use set and remove later defs.
94           addUse(Op);
95         }
96       } // if a use
97     } // for all operands
98
99     // do for implicit operands as well
100     for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
101       assert(MI->getOpcode() != V9::PHI && "Phi cannot have implicit operands");
102       const Value *Op = MI->getImplicitRef(i);
103
104       if (Op->getType() == Type::LabelTy)             // don't process labels
105         continue;
106
107       if (MI->getImplicitOp(i).isUse())
108         addUse(Op);
109     }
110   } // for all machine instructions
111
112
113
114         
115 //-----------------------------------------------------------------------------
116 // To add an operand which is a def
117 //-----------------------------------------------------------------------------
118 void BBLiveVar::addDef(const Value *Op) {
119   DefSet.insert(Op);     // operand is a def - so add to def set
120   InSet.erase(Op);       // this definition kills any later uses
121   InSetChanged = true; 
122
123   if (DEBUG_LV >= LV_DEBUG_Verbose) std::cerr << "  +Def: " << RAV(Op) << "\n";
124 }
125
126
127 //-----------------------------------------------------------------------------
128 // To add an operand which is a use
129 //-----------------------------------------------------------------------------
130 void  BBLiveVar::addUse(const Value *Op) {
131   InSet.insert(Op);   // An operand is a use - so add to use set
132   DefSet.erase(Op);   // remove if there is a def below this use
133   InSetChanged = true; 
134
135   if (DEBUG_LV >= LV_DEBUG_Verbose) std::cerr << "   Use: " << RAV(Op) << "\n";
136 }
137
138
139 //-----------------------------------------------------------------------------
140 // Applies the transfer function to a basic block to produce the InSet using
141 // the OutSet. 
142 //-----------------------------------------------------------------------------
143
144 bool BBLiveVar::applyTransferFunc() {
145   // IMPORTANT: caller should check whether the OutSet changed 
146   //           (else no point in calling)
147
148   ValueSet OutMinusDef = set_difference(OutSet, DefSet);
149   InSetChanged = set_union(InSet, OutMinusDef);
150  
151   OutSetChanged = false;      // no change to OutSet since transf func applied
152   return InSetChanged;
153 }
154
155
156 //-----------------------------------------------------------------------------
157 // calculates Out set using In sets of the successors
158 //-----------------------------------------------------------------------------
159
160 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet, 
161                              const BasicBlock *PredBB) {
162   bool Changed = false;
163   
164   // merge all members of InSet into OutSet of the predecessor
165   for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
166        InIt != InE; ++InIt)
167     if ((OutSet->insert(*InIt)).second)
168       Changed = true;
169   
170   // 
171   //**** WARNING: The following code for handling dummy PHI machine
172   //     instructions is untested.  See explanation above.
173   // 
174   // then merge all members of the EdgeInSet for the predecessor into the OutSet
175   const ValueSet& EdgeInSet = PredToEdgeInSetMap[PredBB];
176   for (ValueSet::const_iterator InIt = EdgeInSet.begin(), InE = EdgeInSet.end();
177        InIt != InE; ++InIt)
178     if ((OutSet->insert(*InIt)).second)
179       Changed = true;
180   // 
181   //****
182   
183   return Changed;
184
185
186
187 //-----------------------------------------------------------------------------
188 // propagates in set to OutSets of PREDECESSORs
189 //-----------------------------------------------------------------------------
190
191 bool BBLiveVar::applyFlowFunc(hash_map<const BasicBlock*,
192                                        BBLiveVar*> &BBLiveVarInfo) {
193   // IMPORTANT: caller should check whether inset changed 
194   //            (else no point in calling)
195   
196   // If this BB changed any OutSets of preds whose POID is lower, than we need
197   // another iteration...
198   //
199   bool needAnotherIt = false;  
200
201   for (pred_const_iterator PI = pred_begin(&BB), PE = pred_end(&BB);
202        PI != PE ; ++PI) {
203     BBLiveVar *PredLVBB = BBLiveVarInfo[*PI];
204
205     // do set union
206     if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {  
207       PredLVBB->OutSetChanged = true;
208
209       // if the predec POID is lower than mine
210       if (PredLVBB->getPOId() <= POID)
211         needAnotherIt = true;   
212     }
213   }  // for
214
215   return needAnotherIt;
216 }
217
218
219
220 // ----------------- Methods For Debugging (Printing) -----------------
221
222 void BBLiveVar::printAllSets() const {
223   std::cerr << "  Defs: "; printSet(DefSet);  std::cerr << "\n";
224   std::cerr << "  In: ";  printSet(InSet);  std::cerr << "\n";
225   std::cerr << "  Out: "; printSet(OutSet);  std::cerr << "\n";
226 }
227
228 void BBLiveVar::printInOutSets() const {
229   std::cerr << "  In: ";   printSet(InSet);  std::cerr << "\n";
230   std::cerr << "  Out: ";  printSet(OutSet);  std::cerr << "\n";
231 }
232
233 } // End llvm namespace