minor change in removing endl
[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/FunctionLiveVarInfo.h"
9 #include "llvm/CodeGen/MachineInstr.h"
10 #include "llvm/Support/CFG.h"
11 #include "Support/SetOperations.h"
12 #include <iostream>
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 >= LV_DEBUG_Verbose) {                            // 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     // iterate over MI operands to find uses
78     for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
79          OpI != OpE; ++OpI) {
80       const Value *Op = *OpI;
81
82       if (isa<BasicBlock>(Op))
83         continue;             // don't process labels
84
85       if (!OpI.isDef()) {   // add to Uses only if this operand is a use
86
87         //
88         // *** WARNING: The following code for handling dummy PHI machine
89         //     instructions is untested.  The previous code was broken and I
90         //     fixed it, but it turned out to be unused as long as Phi elimination
91         //     is performed during instruction selection.
92         // 
93         // Put Phi operands in UseSet for the incoming edge, not node.
94         // They must not "hide" later defs, and must be handled specially
95         // during set propagation over the CFG.
96         if (MI->getOpCode() == PHI) {         // for a phi node
97           const Value *ArgVal = Op;
98           const BasicBlock *PredBB = cast<BasicBlock>(*++OpI); // next ptr is BB
99           
100           PredToEdgeInSetMap[PredBB].insert(ArgVal); 
101           
102           if (DEBUG_LV >= LV_DEBUG_Verbose)
103             cerr << "   - phi operand " << RAV(ArgVal) << " came from BB "
104                  << RAV(PredBB) << "\n";
105         } // if( IsPhi )
106         else {
107           // It is not a Phi use: add to regular use set and remove later defs.
108           addUse(Op);
109         }
110       } // if a use
111     } // for all operands
112
113     // do for implicit operands as well
114     for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
115       assert(MI->getOpCode() != PHI && "Phi cannot have implicit opeands");
116       const Value *Op = MI->getImplicitRef(i);
117
118       if (Op->getType() == Type::LabelTy)             // don't process labels
119         continue;
120
121       if (!MI->implicitRefIsDefined(i))
122         addUse(Op);
123     }
124   } // for all machine instructions
125
126
127
128         
129 //-----------------------------------------------------------------------------
130 // To add an operand which is a def
131 //-----------------------------------------------------------------------------
132 void BBLiveVar::addDef(const Value *Op) {
133   DefSet.insert(Op);     // operand is a def - so add to def set
134   InSet.erase(Op);       // this definition kills any later uses
135   InSetChanged = true; 
136
137   if (DEBUG_LV >= LV_DEBUG_Verbose) cerr << "  +Def: " << RAV(Op) << "\n";
138 }
139
140
141 //-----------------------------------------------------------------------------
142 // To add an operand which is a use
143 //-----------------------------------------------------------------------------
144 void  BBLiveVar::addUse(const Value *Op) {
145   InSet.insert(Op);   // An operand is a use - so add to use set
146   DefSet.erase(Op);   // remove if there is a def below this use
147   InSetChanged = true; 
148
149   if (DEBUG_LV >= LV_DEBUG_Verbose) cerr << "   Use: " << RAV(Op) << "\n";
150 }
151
152
153 //-----------------------------------------------------------------------------
154 // Applies the transfer function to a basic block to produce the InSet using
155 // the OutSet. 
156 //-----------------------------------------------------------------------------
157
158 bool BBLiveVar::applyTransferFunc() {
159   // IMPORTANT: caller should check whether the OutSet changed 
160   //           (else no point in calling)
161
162   ValueSet OutMinusDef = set_difference(OutSet, DefSet);
163   InSetChanged = set_union(InSet, OutMinusDef);
164  
165   OutSetChanged = false;      // no change to OutSet since transf func applied
166   return InSetChanged;
167 }
168
169
170 //-----------------------------------------------------------------------------
171 // calculates Out set using In sets of the successors
172 //-----------------------------------------------------------------------------
173
174 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet, 
175                              const BasicBlock *PredBB) {
176   bool Changed = false;
177   
178   // merge all members of InSet into OutSet of the predecessor
179   for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
180        InIt != InE; ++InIt)
181     if ((OutSet->insert(*InIt)).second)
182       Changed = true;
183   
184   // 
185   //**** WARNING: The following code for handling dummy PHI machine
186   //     instructions is untested.  See explanation above.
187   // 
188   // then merge all members of the EdgeInSet for the predecessor into the OutSet
189   const ValueSet& EdgeInSet = PredToEdgeInSetMap[PredBB];
190   for (ValueSet::const_iterator InIt = EdgeInSet.begin(), InE = EdgeInSet.end();
191        InIt != InE; ++InIt)
192     if ((OutSet->insert(*InIt)).second)
193       Changed = true;
194   // 
195   //****
196   
197   return Changed;
198
199
200
201 //-----------------------------------------------------------------------------
202 // propogates in set to OutSets of PREDECESSORs
203 //-----------------------------------------------------------------------------
204
205 bool BBLiveVar::applyFlowFunc() {
206   // IMPORTANT: caller should check whether inset changed 
207   //            (else no point in calling)
208   
209   // If this BB changed any OutSets of preds whose POID is lower, than we need
210   // another iteration...
211   //
212   bool needAnotherIt = false;  
213
214   for (pred_const_iterator PI = pred_begin(&BB), PE = pred_end(&BB);
215        PI != PE ; ++PI) {
216     BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(**PI);
217
218     // do set union
219     if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {  
220       PredLVBB->OutSetChanged = true;
221
222       // if the predec POID is lower than mine
223       if (PredLVBB->getPOId() <= POID)
224         needAnotherIt = true;   
225     }
226   }  // for
227
228   return needAnotherIt;
229 }
230
231
232
233 // ----------------- Methods For Debugging (Printing) -----------------
234
235 void BBLiveVar::printAllSets() const {
236   cerr << "  Defs: "; printSet(DefSet);  cerr << "\n";
237   cerr << "  In: ";  printSet(InSet);  cerr << "\n";
238   cerr << "  Out: "; printSet(OutSet);  cerr << "\n";
239 }
240
241 void BBLiveVar::printInOutSets() const {
242   cerr << "  In: ";   printSet(InSet);  cerr << "\n";
243   cerr << "  Out: ";  printSet(OutSet);  cerr << "\n";
244 }
245
246
247
248