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 BBLiveVar::BBLiveVar(const BasicBlock *bb, unsigned id)
20 InSetChanged = OutSetChanged = false;
25 //-----------------------------------------------------------------------------
26 // calculates def and use sets for each BB
27 // There are two passes over operands of a machine instruction. This is
28 // because, we can have instructions like V = V + 1, since we no longer
29 // assume single definition.
30 //-----------------------------------------------------------------------------
32 void BBLiveVar::calcDefUseSets() {
33 // get the iterator for machine instructions
34 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
36 // iterate over all the machine instructions in BB
37 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
38 MIE = MIVec.rend(); MII != MIE; ++MII) {
39 const MachineInstr *MI = *MII;
41 if (DEBUG_LV > 1) { // debug msg
42 cerr << " *Iterating over machine instr ";
47 // iterate over MI operands to find defs
48 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
50 if (OpI.isDef()) // add to Defs only if this operand is a def
53 // do for implicit operands as well
54 for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i)
55 if (MI->implicitRefIsDefined(i))
56 addDef(MI->getImplicitRef(i));
58 bool IsPhi = MI->getOpCode() == PHI;
60 // iterate over MI operands to find uses
61 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
63 const Value *Op = *OpI;
65 if (isa<BasicBlock>(Op))
66 continue; // don't process labels
68 if (!OpI.isDef()) { // add to Defs only if this operand is a use
71 if (IsPhi) { // for a phi node
72 // put args into the PhiArgMap (Val -> BB)
73 const Value *ArgVal = Op;
74 const Value *BBVal = *++OpI; // increment to point to BB of value
76 PhiArgMap[ArgVal] = cast<BasicBlock>(BBVal);
79 cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
80 << RAV(PhiArgMap[ArgVal]) << "\n";
85 // do for implicit operands as well
86 for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
87 assert(!IsPhi && "Phi cannot have implicit opeands");
88 const Value *Op = MI->getImplicitRef(i);
90 if (Op->getType()->isLabelType()) // don't process labels
93 if (!MI->implicitRefIsDefined(i))
96 } // for all machine instructions
101 //-----------------------------------------------------------------------------
102 // To add an operand which is a def
103 //-----------------------------------------------------------------------------
104 void BBLiveVar::addDef(const Value *Op) {
105 DefSet.insert(Op); // operand is a def - so add to def set
106 InSet.erase(Op); // this definition kills any uses
109 if (DEBUG_LV > 1) cerr << " +Def: " << RAV(Op) << "\n";
113 //-----------------------------------------------------------------------------
114 // To add an operand which is a use
115 //-----------------------------------------------------------------------------
116 void BBLiveVar::addUse(const Value *Op) {
117 InSet.insert(Op); // An operand is a use - so add to use set
118 OutSet.erase(Op); // remove if there is a def below this use
121 if (DEBUG_LV > 1) cerr << " Use: " << RAV(Op) << "\n";
125 //-----------------------------------------------------------------------------
126 // Applies the transfer function to a basic block to produce the InSet using
128 //-----------------------------------------------------------------------------
130 bool BBLiveVar::applyTransferFunc() {
131 // IMPORTANT: caller should check whether the OutSet changed
132 // (else no point in calling)
134 ValueSet OutMinusDef = set_difference(OutSet, DefSet);
135 InSetChanged = set_union(InSet, OutMinusDef);
137 OutSetChanged = false; // no change to OutSet since transf func applied
142 //-----------------------------------------------------------------------------
143 // calculates Out set using In sets of the predecessors
144 //-----------------------------------------------------------------------------
146 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet,
147 const BasicBlock *PredBB) {
148 bool Changed = false;
150 // for all all elements in InSet
151 for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
152 InIt != InE; ++InIt) {
153 const BasicBlock *PredBBOfPhiArg = PhiArgMap[*InIt];
155 // if this var is not a phi arg OR
156 // it's a phi arg and the var went down from this BB
157 if (!PredBBOfPhiArg || PredBBOfPhiArg == PredBB)
158 if (OutSet->insert(*InIt).second)
166 //-----------------------------------------------------------------------------
167 // propogates in set to OutSets of PREDECESSORs
168 //-----------------------------------------------------------------------------
170 bool BBLiveVar::applyFlowFunc(std::map<const BasicBlock *, BBLiveVar *> &LVMap){
171 // IMPORTANT: caller should check whether inset changed
172 // (else no point in calling)
174 // If this BB changed any OutSets of preds whose POID is lower, than we need
175 // another iteration...
177 bool needAnotherIt = false;
179 for (BasicBlock::pred_const_iterator PI = BB->pred_begin(),
180 PE = BB->pred_begin(); PI != PE ; ++PI) {
181 BBLiveVar *PredLVBB = LVMap[*PI];
184 if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {
185 PredLVBB->OutSetChanged = true;
187 // if the predec POID is lower than mine
188 if (PredLVBB->getPOId() <= POID)
189 needAnotherIt = true;
193 return needAnotherIt;
198 // ----------------- Methods For Debugging (Printing) -----------------
200 void BBLiveVar::printAllSets() const {
201 cerr << " Defs: "; printSet(DefSet); cerr << "\n";
202 cerr << " In: "; printSet(InSet); cerr << "\n";
203 cerr << " Out: "; printSet(OutSet); cerr << "\n";
206 void BBLiveVar::printInOutSets() const {
207 cerr << " In: "; printSet(InSet); cerr << "\n";
208 cerr << " Out: "; printSet(OutSet); cerr << "\n";