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