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 "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 > 1) { // 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 bool IsPhi = MI->getOpCode() == PHI;
80 // iterate over MI operands to find uses
81 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
83 const Value *Op = *OpI;
85 if (isa<BasicBlock>(Op))
86 continue; // don't process labels
88 if (!OpI.isDef()) { // add to Defs only if this operand is a use
91 if (IsPhi) { // for a phi node
92 // put args into the PhiArgMap (Val -> BB)
93 const Value *ArgVal = Op;
94 const Value *BBVal = *++OpI; // increment to point to BB of value
96 PhiArgMap[ArgVal] = cast<BasicBlock>(BBVal);
99 cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
100 << RAV(PhiArgMap[ArgVal]) << "\n";
103 } // for all operands
105 // do for implicit operands as well
106 for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
107 assert(!IsPhi && "Phi cannot have implicit opeands");
108 const Value *Op = MI->getImplicitRef(i);
110 if (Op->getType()->isLabelType()) // don't process labels
113 if (!MI->implicitRefIsDefined(i))
116 } // for all machine instructions
121 //-----------------------------------------------------------------------------
122 // To add an operand which is a def
123 //-----------------------------------------------------------------------------
124 void BBLiveVar::addDef(const Value *Op) {
125 DefSet.insert(Op); // operand is a def - so add to def set
126 InSet.erase(Op); // this definition kills any uses
129 if (DEBUG_LV > 1) cerr << " +Def: " << RAV(Op) << "\n";
133 //-----------------------------------------------------------------------------
134 // To add an operand which is a use
135 //-----------------------------------------------------------------------------
136 void BBLiveVar::addUse(const Value *Op) {
137 InSet.insert(Op); // An operand is a use - so add to use set
138 OutSet.erase(Op); // remove if there is a def below this use
141 if (DEBUG_LV > 1) cerr << " Use: " << RAV(Op) << "\n";
145 //-----------------------------------------------------------------------------
146 // Applies the transfer function to a basic block to produce the InSet using
148 //-----------------------------------------------------------------------------
150 bool BBLiveVar::applyTransferFunc() {
151 // IMPORTANT: caller should check whether the OutSet changed
152 // (else no point in calling)
154 ValueSet OutMinusDef = set_difference(OutSet, DefSet);
155 InSetChanged = set_union(InSet, OutMinusDef);
157 OutSetChanged = false; // no change to OutSet since transf func applied
162 //-----------------------------------------------------------------------------
163 // calculates Out set using In sets of the predecessors
164 //-----------------------------------------------------------------------------
166 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet,
167 const BasicBlock *PredBB) {
168 bool Changed = false;
170 // for all all elements in InSet
171 for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
172 InIt != InE; ++InIt) {
173 const BasicBlock *PredBBOfPhiArg = PhiArgMap[*InIt];
175 // Only propogate liveness of the value if it is either not an argument of
176 // a PHI node, or if it IS an argument, AND 'PredBB' is the basic block
177 // that it is coming in from. THIS IS BROKEN because the same value can
178 // come in from multiple predecessors (and it's not a multimap)!
180 if (PredBBOfPhiArg == 0 || PredBBOfPhiArg == PredBB)
181 if (OutSet->insert(*InIt).second)
189 //-----------------------------------------------------------------------------
190 // propogates in set to OutSets of PREDECESSORs
191 //-----------------------------------------------------------------------------
193 bool BBLiveVar::applyFlowFunc() {
194 // IMPORTANT: caller should check whether inset changed
195 // (else no point in calling)
197 // If this BB changed any OutSets of preds whose POID is lower, than we need
198 // another iteration...
200 bool needAnotherIt = false;
202 for (pred_const_iterator PI = pred_begin(BB), PE = pred_begin(BB);
204 BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(*PI);
207 if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {
208 PredLVBB->OutSetChanged = true;
210 // if the predec POID is lower than mine
211 if (PredLVBB->getPOId() <= POID)
212 needAnotherIt = true;
216 return needAnotherIt;
221 // ----------------- Methods For Debugging (Printing) -----------------
223 void BBLiveVar::printAllSets() const {
224 cerr << " Defs: "; printSet(DefSet); cerr << "\n";
225 cerr << " In: "; printSet(InSet); cerr << "\n";
226 cerr << " Out: "; printSet(OutSet); cerr << "\n";
229 void BBLiveVar::printInOutSets() const {
230 cerr << " In: "; printSet(InSet); cerr << "\n";
231 cerr << " Out: "; printSet(OutSet); cerr << "\n";