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