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/MethodLiveVarInfo.h"
9 #include "llvm/CodeGen/MachineInstr.h"
10 #include "llvm/BasicBlock.h"
11 #include "Support/SetOperations.h"
13 /// BROKEN: Should not include sparc stuff directly into here
14 #include "../../Target/Sparc/SparcInternals.h" // Only for PHI defn
18 static AnnotationID AID(AnnotationManager::getID("Analysis::BBLiveVar"));
20 BBLiveVar *BBLiveVar::CreateOnBB(const BasicBlock *BB, unsigned POID) {
21 BBLiveVar *Result = new BBLiveVar(BB, POID);
22 BB->addAnnotation(Result);
26 BBLiveVar *BBLiveVar::GetFromBB(const BasicBlock *BB) {
27 return (BBLiveVar*)BB->getAnnotation(AID);
30 void BBLiveVar::RemoveFromBB(const BasicBlock *BB) {
31 bool Deleted = BB->deleteAnnotation(AID);
32 assert(Deleted && "BBLiveVar annotation did not exist!");
36 BBLiveVar::BBLiveVar(const BasicBlock *bb, unsigned id)
37 : Annotation(AID), BB(bb), POID(id) {
38 InSetChanged = OutSetChanged = false;
43 //-----------------------------------------------------------------------------
44 // calculates def and use sets for each BB
45 // There are two passes over operands of a machine instruction. This is
46 // because, we can have instructions like V = V + 1, since we no longer
47 // assume single definition.
48 //-----------------------------------------------------------------------------
50 void BBLiveVar::calcDefUseSets() {
51 // get the iterator for machine instructions
52 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
54 // iterate over all the machine instructions in BB
55 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
56 MIE = MIVec.rend(); MII != MIE; ++MII) {
57 const MachineInstr *MI = *MII;
59 if (DEBUG_LV > 1) { // debug msg
60 cerr << " *Iterating over machine instr ";
65 // iterate over MI operands to find defs
66 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
68 if (OpI.isDef()) // add to Defs only if this operand is a def
71 // do for implicit operands as well
72 for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i)
73 if (MI->implicitRefIsDefined(i))
74 addDef(MI->getImplicitRef(i));
76 bool IsPhi = MI->getOpCode() == PHI;
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 Defs only if this operand is a use
89 if (IsPhi) { // for a phi node
90 // put args into the PhiArgMap (Val -> BB)
91 const Value *ArgVal = Op;
92 const Value *BBVal = *++OpI; // increment to point to BB of value
94 PhiArgMap[ArgVal] = cast<BasicBlock>(BBVal);
97 cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
98 << RAV(PhiArgMap[ArgVal]) << "\n";
101 } // for all operands
103 // do for implicit operands as well
104 for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
105 assert(!IsPhi && "Phi cannot have implicit opeands");
106 const Value *Op = MI->getImplicitRef(i);
108 if (Op->getType()->isLabelType()) // don't process labels
111 if (!MI->implicitRefIsDefined(i))
114 } // for all machine instructions
119 //-----------------------------------------------------------------------------
120 // To add an operand which is a def
121 //-----------------------------------------------------------------------------
122 void BBLiveVar::addDef(const Value *Op) {
123 DefSet.insert(Op); // operand is a def - so add to def set
124 InSet.erase(Op); // this definition kills any uses
127 if (DEBUG_LV > 1) cerr << " +Def: " << RAV(Op) << "\n";
131 //-----------------------------------------------------------------------------
132 // To add an operand which is a use
133 //-----------------------------------------------------------------------------
134 void BBLiveVar::addUse(const Value *Op) {
135 InSet.insert(Op); // An operand is a use - so add to use set
136 OutSet.erase(Op); // remove if there is a def below this use
139 if (DEBUG_LV > 1) cerr << " Use: " << RAV(Op) << "\n";
143 //-----------------------------------------------------------------------------
144 // Applies the transfer function to a basic block to produce the InSet using
146 //-----------------------------------------------------------------------------
148 bool BBLiveVar::applyTransferFunc() {
149 // IMPORTANT: caller should check whether the OutSet changed
150 // (else no point in calling)
152 ValueSet OutMinusDef = set_difference(OutSet, DefSet);
153 InSetChanged = set_union(InSet, OutMinusDef);
155 OutSetChanged = false; // no change to OutSet since transf func applied
160 //-----------------------------------------------------------------------------
161 // calculates Out set using In sets of the predecessors
162 //-----------------------------------------------------------------------------
164 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet,
165 const BasicBlock *PredBB) {
166 bool Changed = false;
168 // for all all elements in InSet
169 for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
170 InIt != InE; ++InIt) {
171 const BasicBlock *PredBBOfPhiArg = PhiArgMap[*InIt];
173 // Only propogate liveness of the value if it is either not an argument of
174 // a PHI node, or if it IS an argument, AND 'PredBB' is the basic block
175 // that it is coming in from. THIS IS BROKEN because the same value can
176 // come in from multiple predecessors (and it's not a multimap)!
178 if (PredBBOfPhiArg == 0 || PredBBOfPhiArg == PredBB)
179 if (OutSet->insert(*InIt).second)
187 //-----------------------------------------------------------------------------
188 // propogates in set to OutSets of PREDECESSORs
189 //-----------------------------------------------------------------------------
191 bool BBLiveVar::applyFlowFunc() {
192 // IMPORTANT: caller should check whether inset changed
193 // (else no point in calling)
195 // If this BB changed any OutSets of preds whose POID is lower, than we need
196 // another iteration...
198 bool needAnotherIt = false;
200 for (BasicBlock::pred_const_iterator PI = BB->pred_begin(),
201 PE = BB->pred_begin(); PI != PE ; ++PI) {
202 BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(*PI);
205 if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {
206 PredLVBB->OutSetChanged = true;
208 // if the predec POID is lower than mine
209 if (PredLVBB->getPOId() <= POID)
210 needAnotherIt = true;
214 return needAnotherIt;
219 // ----------------- Methods For Debugging (Printing) -----------------
221 void BBLiveVar::printAllSets() const {
222 cerr << " Defs: "; printSet(DefSet); cerr << "\n";
223 cerr << " In: "; printSet(InSet); cerr << "\n";
224 cerr << " Out: "; printSet(OutSet); cerr << "\n";
227 void BBLiveVar::printInOutSets() const {
228 cerr << " In: "; printSet(InSet); cerr << "\n";
229 cerr << " Out: "; printSet(OutSet); cerr << "\n";