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