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"
12 /// BROKEN: Should not include sparc stuff directly into here
13 #include "../../Target/Sparc/SparcInternals.h" // Only for PHI defn
17 BBLiveVar::BBLiveVar(const BasicBlock *bb, unsigned id)
19 InSetChanged = OutSetChanged = false;
22 //-----------------------------------------------------------------------------
23 // calculates def and use sets for each BB
24 // There are two passes over operands of a machine instruction. This is
25 // because, we can have instructions like V = V + 1, since we no longer
26 // assume single definition.
27 //-----------------------------------------------------------------------------
29 void BBLiveVar::calcDefUseSets() {
30 // get the iterator for machine instructions
31 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
33 // iterate over all the machine instructions in BB
34 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
35 MIE = MIVec.rend(); MII != MIE; ++MII) {
36 const MachineInstr *MI = *MII;
38 if (DEBUG_LV > 1) { // debug msg
39 cerr << " *Iterating over machine instr ";
44 // iterate over MI operands to find defs
45 for (MachineInstr::val_const_op_iterator OpI(MI); !OpI.done(); ++OpI)
46 if (OpI.isDef()) // add to Defs only if this operand is a def
49 // do for implicit operands as well
50 for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i)
51 if (MI->implicitRefIsDefined(i))
52 addDef(MI->getImplicitRef(i));
54 bool IsPhi = MI->getOpCode() == PHI;
56 // iterate over MI operands to find uses
57 for (MachineInstr::val_const_op_iterator OpI(MI); !OpI.done(); ++OpI) {
58 const Value *Op = *OpI;
60 if (Op->getType()->isLabelType())
61 continue; // don't process labels
63 if (!OpI.isDef()) { // add to Defs only if this operand is a use
66 if (IsPhi) { // for a phi node
67 // put args into the PhiArgMap (Val -> BB)
68 const Value *ArgVal = Op;
69 const Value *BBVal = *++OpI; // increment to point to BB of value
71 PhiArgMap[ArgVal] = cast<BasicBlock>(BBVal);
74 cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
75 << RAV(PhiArgMap[ArgVal]) << "\n";
80 // do for implicit operands as well
81 for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
82 assert(!IsPhi && "Phi cannot have implicit opeands");
83 const Value *Op = MI->getImplicitRef(i);
85 if (Op->getType()->isLabelType()) // don't process labels
88 if (!MI->implicitRefIsDefined(i))
91 } // for all machine instructions
96 //-----------------------------------------------------------------------------
97 // To add an operand which is a def
98 //-----------------------------------------------------------------------------
99 void BBLiveVar::addDef(const Value *Op) {
100 DefSet.insert(Op); // operand is a def - so add to def set
101 InSet.erase(Op); // this definition kills any uses
104 if (DEBUG_LV > 1) cerr << " +Def: " << RAV(Op) << "\n";
108 //-----------------------------------------------------------------------------
109 // To add an operand which is a use
110 //-----------------------------------------------------------------------------
111 void BBLiveVar::addUse(const Value *Op) {
112 InSet.insert(Op); // An operand is a use - so add to use set
113 OutSet.erase(Op); // remove if there is a def below this use
116 if (DEBUG_LV > 1) cerr << " Use: " << RAV(Op) << "\n";
120 //-----------------------------------------------------------------------------
121 // Applies the transfer function to a basic block to produce the InSet using
123 //-----------------------------------------------------------------------------
125 bool BBLiveVar::applyTransferFunc() {
126 // IMPORTANT: caller should check whether the OutSet changed
127 // (else no point in calling)
129 LiveVarSet OutMinusDef; // set to hold (Out[B] - Def[B])
130 OutMinusDef.setDifference(&OutSet, &DefSet);
131 InSetChanged = InSet.setUnion(&OutMinusDef);
133 OutSetChanged = false; // no change to OutSet since transf func applied
138 //-----------------------------------------------------------------------------
139 // calculates Out set using In sets of the predecessors
140 //-----------------------------------------------------------------------------
142 bool BBLiveVar::setPropagate(LiveVarSet *OutSet, const LiveVarSet *InSet,
143 const BasicBlock *PredBB) {
144 bool Changed = false;
146 // for all all elements in InSet
147 for (LiveVarSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
148 InIt != InE; ++InIt) {
149 const BasicBlock *PredBBOfPhiArg = PhiArgMap[*InIt];
151 // if this var is not a phi arg OR
152 // it's a phi arg and the var went down from this BB
153 if (!PredBBOfPhiArg || PredBBOfPhiArg == PredBB)
154 if (OutSet->insert(*InIt).second)
162 //-----------------------------------------------------------------------------
163 // propogates in set to OutSets of PREDECESSORs
164 //-----------------------------------------------------------------------------
166 bool BBLiveVar::applyFlowFunc(std::map<const BasicBlock *, BBLiveVar *> &LVMap){
167 // IMPORTANT: caller should check whether inset changed
168 // (else no point in calling)
170 // If this BB changed any OutSets of preds whose POID is lower, than we need
171 // another iteration...
173 bool needAnotherIt = false;
175 for (BasicBlock::pred_const_iterator PI = BB->pred_begin(),
176 PE = BB->pred_begin(); PI != PE ; ++PI) {
177 BBLiveVar *PredLVBB = LVMap[*PI];
180 if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {
181 PredLVBB->OutSetChanged = true;
183 // if the predec POID is lower than mine
184 if (PredLVBB->getPOId() <= POID)
185 needAnotherIt = true;
189 return needAnotherIt;
194 // ----------------- Methods For Debugging (Printing) -----------------
196 void BBLiveVar::printAllSets() const {
197 cerr << " Defs: "; DefSet.printSet(); cerr << "\n";
198 cerr << " In: "; InSet.printSet(); cerr << "\n";
199 cerr << " Out: "; OutSet.printSet(); cerr << "\n";
202 void BBLiveVar::printInOutSets() const {
203 cerr << " In: "; InSet.printSet(); cerr << "\n";
204 cerr << " Out: "; OutSet.printSet(); cerr << "\n";