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