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"
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 > 1) { // 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 bool IsPhi = MI->getOpCode() == PHI;
79 // iterate over MI operands to find uses
80 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
82 const Value *Op = *OpI;
84 if (isa<BasicBlock>(Op))
85 continue; // don't process labels
87 if (!OpI.isDef()) { // add to Defs only if this operand is a use
90 if (IsPhi) { // for a phi node
91 // put args into the PhiArgMap (Val -> BB)
92 const Value *ArgVal = Op;
93 const Value *BBVal = *++OpI; // increment to point to BB of value
95 PhiArgMap[ArgVal] = cast<BasicBlock>(BBVal);
98 cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
99 << RAV(PhiArgMap[ArgVal]) << "\n";
102 } // for all operands
104 // do for implicit operands as well
105 for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
106 assert(!IsPhi && "Phi cannot have implicit opeands");
107 const Value *Op = MI->getImplicitRef(i);
109 if (Op->getType()->isLabelType()) // don't process labels
112 if (!MI->implicitRefIsDefined(i))
115 } // for all machine instructions
120 //-----------------------------------------------------------------------------
121 // To add an operand which is a def
122 //-----------------------------------------------------------------------------
123 void BBLiveVar::addDef(const Value *Op) {
124 DefSet.insert(Op); // operand is a def - so add to def set
125 InSet.erase(Op); // this definition kills any uses
128 if (DEBUG_LV > 1) cerr << " +Def: " << RAV(Op) << "\n";
132 //-----------------------------------------------------------------------------
133 // To add an operand which is a use
134 //-----------------------------------------------------------------------------
135 void BBLiveVar::addUse(const Value *Op) {
136 InSet.insert(Op); // An operand is a use - so add to use set
137 OutSet.erase(Op); // remove if there is a def below this use
140 if (DEBUG_LV > 1) cerr << " Use: " << RAV(Op) << "\n";
144 //-----------------------------------------------------------------------------
145 // Applies the transfer function to a basic block to produce the InSet using
147 //-----------------------------------------------------------------------------
149 bool BBLiveVar::applyTransferFunc() {
150 // IMPORTANT: caller should check whether the OutSet changed
151 // (else no point in calling)
153 ValueSet OutMinusDef = set_difference(OutSet, DefSet);
154 InSetChanged = set_union(InSet, OutMinusDef);
156 OutSetChanged = false; // no change to OutSet since transf func applied
161 //-----------------------------------------------------------------------------
162 // calculates Out set using In sets of the predecessors
163 //-----------------------------------------------------------------------------
165 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet,
166 const BasicBlock *PredBB) {
167 bool Changed = false;
169 // for all all elements in InSet
170 for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
171 InIt != InE; ++InIt) {
172 const BasicBlock *PredBBOfPhiArg = PhiArgMap[*InIt];
174 // Only propogate liveness of the value if it is either not an argument of
175 // a PHI node, or if it IS an argument, AND 'PredBB' is the basic block
176 // that it is coming in from. THIS IS BROKEN because the same value can
177 // come in from multiple predecessors (and it's not a multimap)!
179 if (PredBBOfPhiArg == 0 || PredBBOfPhiArg == PredBB)
180 if (OutSet->insert(*InIt).second)
188 //-----------------------------------------------------------------------------
189 // propogates in set to OutSets of PREDECESSORs
190 //-----------------------------------------------------------------------------
192 bool BBLiveVar::applyFlowFunc() {
193 // IMPORTANT: caller should check whether inset changed
194 // (else no point in calling)
196 // If this BB changed any OutSets of preds whose POID is lower, than we need
197 // another iteration...
199 bool needAnotherIt = false;
201 for (pred_const_iterator PI = pred_begin(BB), PE = pred_begin(BB);
203 BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(*PI);
206 if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {
207 PredLVBB->OutSetChanged = true;
209 // if the predec POID is lower than mine
210 if (PredLVBB->getPOId() <= POID)
211 needAnotherIt = true;
215 return needAnotherIt;
220 // ----------------- Methods For Debugging (Printing) -----------------
222 void BBLiveVar::printAllSets() const {
223 cerr << " Defs: "; printSet(DefSet); cerr << "\n";
224 cerr << " In: "; printSet(InSet); cerr << "\n";
225 cerr << " Out: "; printSet(OutSet); cerr << "\n";
228 void BBLiveVar::printInOutSets() const {
229 cerr << " In: "; printSet(InSet); cerr << "\n";
230 cerr << " Out: "; printSet(OutSet); cerr << "\n";