1 //===-- BBLiveVar.cpp - Live Variable Analysis for a BasicBlock -----------===//
3 // This is a wrapper class for BasicBlock which is used by live var analysis.
5 //===----------------------------------------------------------------------===//
8 #include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
9 #include "llvm/CodeGen/MachineInstr.h"
10 #include "llvm/Support/CFG.h"
11 #include "Support/SetOperations.h"
14 /// BROKEN: Should not include sparc stuff directly into here
15 #include "../../Target/Sparc/SparcInternals.h" // Only for PHI defn
19 static AnnotationID AID(AnnotationManager::getID("Analysis::BBLiveVar"));
21 BBLiveVar *BBLiveVar::CreateOnBB(const BasicBlock *BB, unsigned POID) {
22 BBLiveVar *Result = new BBLiveVar(BB, POID);
23 BB->addAnnotation(Result);
27 BBLiveVar *BBLiveVar::GetFromBB(const BasicBlock *BB) {
28 return (BBLiveVar*)BB->getAnnotation(AID);
31 void BBLiveVar::RemoveFromBB(const BasicBlock *BB) {
32 bool Deleted = BB->deleteAnnotation(AID);
33 assert(Deleted && "BBLiveVar annotation did not exist!");
37 BBLiveVar::BBLiveVar(const BasicBlock *bb, unsigned id)
38 : Annotation(AID), BB(bb), POID(id) {
39 InSetChanged = OutSetChanged = false;
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 //-----------------------------------------------------------------------------
51 void BBLiveVar::calcDefUseSets() {
52 // get the iterator for machine instructions
53 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
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;
60 if (DEBUG_LV >= LV_DEBUG_Verbose) { // debug msg
61 cerr << " *Iterating over machine instr ";
66 // iterate over MI operands to find defs
67 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
69 if (OpI.isDef()) // add to Defs only if this operand is a def
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));
77 // iterate over MI operands to find uses
78 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
80 const Value *Op = *OpI;
82 if (isa<BasicBlock>(Op))
83 continue; // don't process labels
85 if (!OpI.isDef()) { // add to Uses only if this operand is a use
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.
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
100 PredToEdgeInSetMap[PredBB].insert(ArgVal);
102 if (DEBUG_LV >= LV_DEBUG_Verbose)
103 cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
104 << RAV(PredBB) << endl;
107 // It is not a Phi use: add to regular use set and remove later defs.
111 } // for all operands
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);
118 if (Op->getType() == Type::LabelTy) // don't process labels
121 if (!MI->implicitRefIsDefined(i))
124 } // for all machine instructions
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
137 if (DEBUG_LV >= LV_DEBUG_Verbose) cerr << " +Def: " << RAV(Op) << "\n";
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
149 if (DEBUG_LV >= LV_DEBUG_Verbose) cerr << " Use: " << RAV(Op) << "\n";
153 //-----------------------------------------------------------------------------
154 // Applies the transfer function to a basic block to produce the InSet using
156 //-----------------------------------------------------------------------------
158 bool BBLiveVar::applyTransferFunc() {
159 // IMPORTANT: caller should check whether the OutSet changed
160 // (else no point in calling)
162 ValueSet OutMinusDef = set_difference(OutSet, DefSet);
163 InSetChanged = set_union(InSet, OutMinusDef);
165 OutSetChanged = false; // no change to OutSet since transf func applied
170 //-----------------------------------------------------------------------------
171 // calculates Out set using In sets of the successors
172 //-----------------------------------------------------------------------------
174 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet,
175 const BasicBlock *PredBB) {
176 bool Changed = false;
178 // merge all members of InSet into OutSet of the predecessor
179 for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
181 if ((OutSet->insert(*InIt)).second)
185 //**** WARNING: The following code for handling dummy PHI machine
186 // instructions is untested. See explanation above.
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();
192 if ((OutSet->insert(*InIt)).second)
201 //-----------------------------------------------------------------------------
202 // propogates in set to OutSets of PREDECESSORs
203 //-----------------------------------------------------------------------------
205 bool BBLiveVar::applyFlowFunc() {
206 // IMPORTANT: caller should check whether inset changed
207 // (else no point in calling)
209 // If this BB changed any OutSets of preds whose POID is lower, than we need
210 // another iteration...
212 bool needAnotherIt = false;
214 for (pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
216 BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(*PI);
219 if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {
220 PredLVBB->OutSetChanged = true;
222 // if the predec POID is lower than mine
223 if (PredLVBB->getPOId() <= POID)
224 needAnotherIt = true;
228 return needAnotherIt;
233 // ----------------- Methods For Debugging (Printing) -----------------
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";
241 void BBLiveVar::printInOutSets() const {
242 cerr << " In: "; printSet(InSet); cerr << "\n";
243 cerr << " Out: "; printSet(OutSet); cerr << "\n";