* Eliminate the LiveVarSet class, making applyTranferFuncForMInst a static
[oota-llvm.git] / lib / Target / SparcV9 / LiveVar / BBLiveVar.cpp
1 //===-- BBLiveVar.cpp - Live Variable Analysis for a BasicBlock -----------===//
2 //
3 // This is a wrapper class for BasicBlock which is used by live var analysis.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "BBLiveVar.h"
8 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
9 #include "llvm/CodeGen/MachineInstr.h"
10 #include "llvm/BasicBlock.h"
11
12 /// BROKEN: Should not include sparc stuff directly into here
13 #include "../../Target/Sparc/SparcInternals.h"  //  Only for PHI defn
14
15 using std::cerr;
16
17 BBLiveVar::BBLiveVar(const BasicBlock *bb, unsigned id)
18   : BB(bb), POID(id) {
19   InSetChanged = OutSetChanged = false;
20 }
21
22 //-----------------------------------------------------------------------------
23 // calculates def and use sets for each BB
24 // There are two passes over operands of a machine instruction. This is
25 // because, we can have instructions like V = V + 1, since we no longer
26 // assume single definition.
27 //-----------------------------------------------------------------------------
28
29 void BBLiveVar::calcDefUseSets() {
30   // get the iterator for machine instructions
31   const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
32
33   // iterate over all the machine instructions in BB
34   for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
35          MIE = MIVec.rend(); MII != MIE; ++MII) {
36     const MachineInstr *MI = *MII;
37     
38     if (DEBUG_LV > 1) {                            // debug msg
39       cerr << " *Iterating over machine instr ";
40       MI->dump();
41       cerr << "\n";
42     }
43
44     // iterate over  MI operands to find defs
45     for (MachineInstr::val_const_op_iterator OpI(MI); !OpI.done(); ++OpI)
46       if (OpI.isDef())      // add to Defs only if this operand is a def
47         addDef(*OpI);
48
49     // do for implicit operands as well
50     for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i)
51       if (MI->implicitRefIsDefined(i))
52         addDef(MI->getImplicitRef(i));
53     
54     bool IsPhi = MI->getOpCode() == PHI;
55  
56     // iterate over MI operands to find uses
57     for (MachineInstr::val_const_op_iterator OpI(MI); !OpI.done(); ++OpI) {
58       const Value *Op = *OpI;
59
60       if (Op->getType()->isLabelType())    
61         continue;             // don't process labels
62
63       if (!OpI.isDef()) {   // add to Defs only if this operand is a use
64         addUse(Op);
65
66         if (IsPhi) {         // for a phi node
67           // put args into the PhiArgMap (Val -> BB)
68           const Value *ArgVal = Op;
69           const Value *BBVal = *++OpI; // increment to point to BB of value
70           
71           PhiArgMap[ArgVal] = cast<BasicBlock>(BBVal); 
72           
73           if (DEBUG_LV > 1)
74             cerr << "   - phi operand " << RAV(ArgVal) << " came from BB "
75                  << RAV(PhiArgMap[ArgVal]) << "\n";
76         } // if( IsPhi )
77       } // if a use
78     } // for all operands
79
80     // do for implicit operands as well
81     for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
82       assert(!IsPhi && "Phi cannot have implicit opeands");
83       const Value *Op = MI->getImplicitRef(i);
84
85       if (Op->getType()->isLabelType())             // don't process labels
86         continue;
87
88       if (!MI->implicitRefIsDefined(i))
89         addUse(Op);
90     }
91   } // for all machine instructions
92
93
94
95         
96 //-----------------------------------------------------------------------------
97 // To add an operand which is a def
98 //-----------------------------------------------------------------------------
99 void  BBLiveVar::addDef(const Value *Op) {
100   DefSet.insert(Op);     // operand is a def - so add to def set
101   InSet.erase(Op);       // this definition kills any uses
102   InSetChanged = true; 
103
104   if (DEBUG_LV > 1) cerr << "  +Def: " << RAV(Op) << "\n";
105 }
106
107
108 //-----------------------------------------------------------------------------
109 // To add an operand which is a use
110 //-----------------------------------------------------------------------------
111 void  BBLiveVar::addUse(const Value *Op) {
112   InSet.insert(Op);   // An operand is a use - so add to use set
113   OutSet.erase(Op);   // remove if there is a def below this use
114   InSetChanged = true; 
115
116   if (DEBUG_LV > 1) cerr << "   Use: " << RAV(Op) << "\n";
117 }
118
119
120 //-----------------------------------------------------------------------------
121 // Applies the transfer function to a basic block to produce the InSet using
122 // the outset. 
123 //-----------------------------------------------------------------------------
124
125 bool BBLiveVar::applyTransferFunc() {
126   // IMPORTANT: caller should check whether the OutSet changed 
127   //           (else no point in calling)
128
129   ValueSet OutMinusDef = set_difference(OutSet, DefSet);
130   InSetChanged = set_union(InSet, OutMinusDef);
131  
132   OutSetChanged = false;      // no change to OutSet since transf func applied
133   return InSetChanged;
134 }
135
136
137 //-----------------------------------------------------------------------------
138 // calculates Out set using In sets of the predecessors
139 //-----------------------------------------------------------------------------
140
141 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet, 
142                              const BasicBlock *PredBB) {
143   bool Changed = false;
144
145   // for all all elements in InSet
146   for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
147        InIt != InE; ++InIt) {  
148     const BasicBlock *PredBBOfPhiArg = PhiArgMap[*InIt];
149
150     // if this var is not a phi arg OR 
151     // it's a phi arg and the var went down from this BB
152     if (!PredBBOfPhiArg || PredBBOfPhiArg == PredBB)
153       if (OutSet->insert(*InIt).second)
154         Changed = true;
155   }
156
157   return Changed;
158
159
160
161 //-----------------------------------------------------------------------------
162 // propogates in set to OutSets of PREDECESSORs
163 //-----------------------------------------------------------------------------
164
165 bool BBLiveVar::applyFlowFunc(std::map<const BasicBlock *, BBLiveVar *> &LVMap){
166   // IMPORTANT: caller should check whether inset changed 
167   //            (else no point in calling)
168
169   // If this BB changed any OutSets of preds whose POID is lower, than we need
170   // another iteration...
171   //
172   bool needAnotherIt = false;  
173
174   for (BasicBlock::pred_const_iterator PI = BB->pred_begin(),
175          PE = BB->pred_begin(); PI != PE ; ++PI) {
176     BBLiveVar *PredLVBB = LVMap[*PI];
177
178     // do set union
179     if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {  
180       PredLVBB->OutSetChanged = true;
181
182       // if the predec POID is lower than mine
183       if (PredLVBB->getPOId() <= POID)
184         needAnotherIt = true;   
185     }
186   }  // for
187
188   return needAnotherIt;
189 }
190
191
192
193 // ----------------- Methods For Debugging (Printing) -----------------
194
195 void BBLiveVar::printAllSets() const {
196   cerr << "  Defs: "; printSet(DefSet);  cerr << "\n";
197   cerr << "  In: ";  printSet(InSet);  cerr << "\n";
198   cerr << "  Out: "; printSet(OutSet);  cerr << "\n";
199 }
200
201 void BBLiveVar::printInOutSets() const {
202   cerr << "  In: ";   printSet(InSet);  cerr << "\n";
203   cerr << "  Out: ";  printSet(OutSet);  cerr << "\n";
204 }
205
206
207
208