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