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