Minor change: Methods that return ValueSet's that are guaranteed to be valid
[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::val_const_op_iterator OpI(MI); !OpI.done(); ++OpI)
49       if (OpI.isDef())      // add to Defs only if this operand is a def
50         addDef(*OpI);
51
52     // do for implicit operands as well
53     for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i)
54       if (MI->implicitRefIsDefined(i))
55         addDef(MI->getImplicitRef(i));
56     
57     bool IsPhi = MI->getOpCode() == PHI;
58  
59     // iterate over MI operands to find uses
60     for (MachineInstr::val_const_op_iterator OpI(MI); !OpI.done(); ++OpI) {
61       const Value *Op = *OpI;
62
63       if (Op->getType()->isLabelType())    
64         continue;             // don't process labels
65
66       if (!OpI.isDef()) {   // add to Defs only if this operand is a use
67         addUse(Op);
68
69         if (IsPhi) {         // for a phi node
70           // put args into the PhiArgMap (Val -> BB)
71           const Value *ArgVal = Op;
72           const Value *BBVal = *++OpI; // increment to point to BB of value
73           
74           PhiArgMap[ArgVal] = cast<BasicBlock>(BBVal); 
75           
76           if (DEBUG_LV > 1)
77             cerr << "   - phi operand " << RAV(ArgVal) << " came from BB "
78                  << RAV(PhiArgMap[ArgVal]) << "\n";
79         } // if( IsPhi )
80       } // if a use
81     } // for all operands
82
83     // do for implicit operands as well
84     for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
85       assert(!IsPhi && "Phi cannot have implicit opeands");
86       const Value *Op = MI->getImplicitRef(i);
87
88       if (Op->getType()->isLabelType())             // don't process labels
89         continue;
90
91       if (!MI->implicitRefIsDefined(i))
92         addUse(Op);
93     }
94   } // for all machine instructions
95
96
97
98         
99 //-----------------------------------------------------------------------------
100 // To add an operand which is a def
101 //-----------------------------------------------------------------------------
102 void  BBLiveVar::addDef(const Value *Op) {
103   DefSet.insert(Op);     // operand is a def - so add to def set
104   InSet.erase(Op);       // this definition kills any uses
105   InSetChanged = true; 
106
107   if (DEBUG_LV > 1) cerr << "  +Def: " << RAV(Op) << "\n";
108 }
109
110
111 //-----------------------------------------------------------------------------
112 // To add an operand which is a use
113 //-----------------------------------------------------------------------------
114 void  BBLiveVar::addUse(const Value *Op) {
115   InSet.insert(Op);   // An operand is a use - so add to use set
116   OutSet.erase(Op);   // remove if there is a def below this use
117   InSetChanged = true; 
118
119   if (DEBUG_LV > 1) cerr << "   Use: " << RAV(Op) << "\n";
120 }
121
122
123 //-----------------------------------------------------------------------------
124 // Applies the transfer function to a basic block to produce the InSet using
125 // the outset. 
126 //-----------------------------------------------------------------------------
127
128 bool BBLiveVar::applyTransferFunc() {
129   // IMPORTANT: caller should check whether the OutSet changed 
130   //           (else no point in calling)
131
132   ValueSet OutMinusDef = set_difference(OutSet, DefSet);
133   InSetChanged = set_union(InSet, OutMinusDef);
134  
135   OutSetChanged = false;      // no change to OutSet since transf func applied
136   return InSetChanged;
137 }
138
139
140 //-----------------------------------------------------------------------------
141 // calculates Out set using In sets of the predecessors
142 //-----------------------------------------------------------------------------
143
144 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet, 
145                              const BasicBlock *PredBB) {
146   bool Changed = false;
147
148   // for all all elements in InSet
149   for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
150        InIt != InE; ++InIt) {  
151     const BasicBlock *PredBBOfPhiArg = PhiArgMap[*InIt];
152
153     // if this var is not a phi arg OR 
154     // it's a phi arg and the var went down from this BB
155     if (!PredBBOfPhiArg || PredBBOfPhiArg == PredBB)
156       if (OutSet->insert(*InIt).second)
157         Changed = true;
158   }
159
160   return Changed;
161
162
163
164 //-----------------------------------------------------------------------------
165 // propogates in set to OutSets of PREDECESSORs
166 //-----------------------------------------------------------------------------
167
168 bool BBLiveVar::applyFlowFunc(std::map<const BasicBlock *, BBLiveVar *> &LVMap){
169   // IMPORTANT: caller should check whether inset changed 
170   //            (else no point in calling)
171
172   // If this BB changed any OutSets of preds whose POID is lower, than we need
173   // another iteration...
174   //
175   bool needAnotherIt = false;  
176
177   for (BasicBlock::pred_const_iterator PI = BB->pred_begin(),
178          PE = BB->pred_begin(); PI != PE ; ++PI) {
179     BBLiveVar *PredLVBB = LVMap[*PI];
180
181     // do set union
182     if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {  
183       PredLVBB->OutSetChanged = true;
184
185       // if the predec POID is lower than mine
186       if (PredLVBB->getPOId() <= POID)
187         needAnotherIt = true;   
188     }
189   }  // for
190
191   return needAnotherIt;
192 }
193
194
195
196 // ----------------- Methods For Debugging (Printing) -----------------
197
198 void BBLiveVar::printAllSets() const {
199   cerr << "  Defs: "; printSet(DefSet);  cerr << "\n";
200   cerr << "  In: ";  printSet(InSet);  cerr << "\n";
201   cerr << "  Out: "; printSet(OutSet);  cerr << "\n";
202 }
203
204 void BBLiveVar::printInOutSets() const {
205   cerr << "  In: ";   printSet(InSet);  cerr << "\n";
206   cerr << "  Out: ";  printSet(OutSet);  cerr << "\n";
207 }
208
209
210
211