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/BasicBlock.h"
11 #include "llvm/Support/CFG.h"
12 #include "Support/SetOperations.h"
15 /// BROKEN: Should not include sparc stuff directly into here
16 #include "../../Target/Sparc/SparcInternals.h" // Only for PHI defn
20 static AnnotationID AID(AnnotationManager::getID("Analysis::BBLiveVar"));
22 BBLiveVar *BBLiveVar::CreateOnBB(const BasicBlock *BB, unsigned POID) {
23 BBLiveVar *Result = new BBLiveVar(BB, POID);
24 BB->addAnnotation(Result);
28 BBLiveVar *BBLiveVar::GetFromBB(const BasicBlock *BB) {
29 return (BBLiveVar*)BB->getAnnotation(AID);
32 void BBLiveVar::RemoveFromBB(const BasicBlock *BB) {
33 bool Deleted = BB->deleteAnnotation(AID);
34 assert(Deleted && "BBLiveVar annotation did not exist!");
38 BBLiveVar::BBLiveVar(const BasicBlock *bb, unsigned id)
39 : Annotation(AID), BB(bb), POID(id) {
40 InSetChanged = OutSetChanged = false;
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 //-----------------------------------------------------------------------------
52 void BBLiveVar::calcDefUseSets() {
53 // get the iterator for machine instructions
54 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
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;
61 if (DEBUG_LV >= LV_DEBUG_Verbose) { // debug msg
62 cerr << " *Iterating over machine instr ";
67 // iterate over MI operands to find defs
68 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
70 if (OpI.isDef()) // add to Defs only if this operand is a def
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));
78 // iterate over MI operands to find uses
79 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
81 const Value *Op = *OpI;
83 if (isa<BasicBlock>(Op))
84 continue; // don't process labels
86 if (!OpI.isDef()) { // add to Uses only if this operand is a use
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.
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
101 PredToEdgeInSetMap[PredBB].insert(ArgVal);
103 if (DEBUG_LV >= LV_DEBUG_Verbose)
104 cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
105 << RAV(PredBB) << endl;
108 // It is not a Phi use: add to regular use set and remove later defs.
112 } // for all operands
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);
119 if (Op->getType() == Type::LabelTy) // don't process labels
122 if (!MI->implicitRefIsDefined(i))
125 } // for all machine instructions
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
138 if (DEBUG_LV >= LV_DEBUG_Verbose) cerr << " +Def: " << RAV(Op) << "\n";
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
150 if (DEBUG_LV >= LV_DEBUG_Verbose) cerr << " Use: " << RAV(Op) << "\n";
154 //-----------------------------------------------------------------------------
155 // Applies the transfer function to a basic block to produce the InSet using
157 //-----------------------------------------------------------------------------
159 bool BBLiveVar::applyTransferFunc() {
160 // IMPORTANT: caller should check whether the OutSet changed
161 // (else no point in calling)
163 ValueSet OutMinusDef = set_difference(OutSet, DefSet);
164 InSetChanged = set_union(InSet, OutMinusDef);
166 OutSetChanged = false; // no change to OutSet since transf func applied
171 //-----------------------------------------------------------------------------
172 // calculates Out set using In sets of the successors
173 //-----------------------------------------------------------------------------
175 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet,
176 const BasicBlock *PredBB) {
177 bool Changed = false;
179 // merge all members of InSet into OutSet of the predecessor
180 for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
182 if ((OutSet->insert(*InIt)).second)
186 //**** WARNING: The following code for handling dummy PHI machine
187 // instructions is untested. See explanation above.
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();
193 if ((OutSet->insert(*InIt)).second)
202 //-----------------------------------------------------------------------------
203 // propogates in set to OutSets of PREDECESSORs
204 //-----------------------------------------------------------------------------
206 bool BBLiveVar::applyFlowFunc() {
207 // IMPORTANT: caller should check whether inset changed
208 // (else no point in calling)
210 // If this BB changed any OutSets of preds whose POID is lower, than we need
211 // another iteration...
213 bool needAnotherIt = false;
215 for (pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
217 BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(*PI);
220 if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {
221 PredLVBB->OutSetChanged = true;
223 // if the predec POID is lower than mine
224 if (PredLVBB->getPOId() <= POID)
225 needAnotherIt = true;
229 return needAnotherIt;
234 // ----------------- Methods For Debugging (Printing) -----------------
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";
242 void BBLiveVar::printInOutSets() const {
243 cerr << " In: "; printSet(InSet); cerr << "\n";
244 cerr << " Out: "; printSet(OutSet); cerr << "\n";