1 //===-- BBLiveVar.cpp - Live Variable Analysis for a BasicBlock -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a wrapper class for BasicBlock which is used by live var analysis.
12 //===----------------------------------------------------------------------===//
14 #include "BBLiveVar.h"
15 #include "FunctionLiveVarInfo.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/Support/CFG.h"
19 #include "Support/SetOperations.h"
20 #include "../SparcV9Internals.h"
25 BBLiveVar::BBLiveVar(const BasicBlock &bb,
26 const MachineBasicBlock &mbb,
28 : BB(bb), MBB(mbb), POID(id) {
29 InSetChanged = OutSetChanged = false;
34 //-----------------------------------------------------------------------------
35 // calculates def and use sets for each BB
36 // There are two passes over operands of a machine instruction. This is
37 // because, we can have instructions like V = V + 1, since we no longer
38 // assume single definition.
39 //-----------------------------------------------------------------------------
41 void BBLiveVar::calcDefUseSets() {
42 // iterate over all the machine instructions in BB
43 for (MachineBasicBlock::const_reverse_iterator MII = MBB.rbegin(),
44 MIE = MBB.rend(); MII != MIE; ++MII) {
45 const MachineInstr *MI = &*MII;
47 if (DEBUG_LV >= LV_DEBUG_Verbose) {
48 std::cerr << " *Iterating over machine instr ";
53 // iterate over MI operands to find defs
54 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
56 if (OpI.isDef()) // add to Defs if this operand is a def
59 // do for implicit operands as well
60 for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i)
61 if (MI->getImplicitOp(i).isDef())
62 addDef(MI->getImplicitRef(i));
64 // iterate over MI operands to find uses
65 for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
67 const Value *Op = *OpI;
69 if (isa<BasicBlock>(Op))
70 continue; // don't process labels
72 if (OpI.isUse()) { // add to Uses only if this operand is a use
74 // *** WARNING: The following code for handling dummy PHI machine
75 // instructions is untested. The previous code was broken and I
76 // fixed it, but it turned out to be unused as long as Phi
77 // elimination is performed during instruction selection.
79 // Put Phi operands in UseSet for the incoming edge, not node.
80 // They must not "hide" later defs, and must be handled specially
81 // during set propagation over the CFG.
82 if (MI->getOpcode() == V9::PHI) { // for a phi node
83 const Value *ArgVal = Op;
84 const BasicBlock *PredBB = cast<BasicBlock>(*++OpI); // next ptr is BB
86 PredToEdgeInSetMap[PredBB].insert(ArgVal);
88 if (DEBUG_LV >= LV_DEBUG_Verbose)
89 std::cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
90 << RAV(PredBB) << "\n";
93 // It is not a Phi use: add to regular use set and remove later defs.
99 // do for implicit operands as well
100 for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
101 assert(MI->getOpcode() != V9::PHI && "Phi cannot have implicit operands");
102 const Value *Op = MI->getImplicitRef(i);
104 if (Op->getType() == Type::LabelTy) // don't process labels
107 if (MI->getImplicitOp(i).isUse())
110 } // for all machine instructions
115 //-----------------------------------------------------------------------------
116 // To add an operand which is a def
117 //-----------------------------------------------------------------------------
118 void BBLiveVar::addDef(const Value *Op) {
119 DefSet.insert(Op); // operand is a def - so add to def set
120 InSet.erase(Op); // this definition kills any later uses
123 if (DEBUG_LV >= LV_DEBUG_Verbose) std::cerr << " +Def: " << RAV(Op) << "\n";
127 //-----------------------------------------------------------------------------
128 // To add an operand which is a use
129 //-----------------------------------------------------------------------------
130 void BBLiveVar::addUse(const Value *Op) {
131 InSet.insert(Op); // An operand is a use - so add to use set
132 DefSet.erase(Op); // remove if there is a def below this use
135 if (DEBUG_LV >= LV_DEBUG_Verbose) std::cerr << " Use: " << RAV(Op) << "\n";
139 //-----------------------------------------------------------------------------
140 // Applies the transfer function to a basic block to produce the InSet using
142 //-----------------------------------------------------------------------------
144 bool BBLiveVar::applyTransferFunc() {
145 // IMPORTANT: caller should check whether the OutSet changed
146 // (else no point in calling)
148 ValueSet OutMinusDef = set_difference(OutSet, DefSet);
149 InSetChanged = set_union(InSet, OutMinusDef);
151 OutSetChanged = false; // no change to OutSet since transf func applied
156 //-----------------------------------------------------------------------------
157 // calculates Out set using In sets of the successors
158 //-----------------------------------------------------------------------------
160 bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet,
161 const BasicBlock *PredBB) {
162 bool Changed = false;
164 // merge all members of InSet into OutSet of the predecessor
165 for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
167 if ((OutSet->insert(*InIt)).second)
171 //**** WARNING: The following code for handling dummy PHI machine
172 // instructions is untested. See explanation above.
174 // then merge all members of the EdgeInSet for the predecessor into the OutSet
175 const ValueSet& EdgeInSet = PredToEdgeInSetMap[PredBB];
176 for (ValueSet::const_iterator InIt = EdgeInSet.begin(), InE = EdgeInSet.end();
178 if ((OutSet->insert(*InIt)).second)
187 //-----------------------------------------------------------------------------
188 // propagates in set to OutSets of PREDECESSORs
189 //-----------------------------------------------------------------------------
191 bool BBLiveVar::applyFlowFunc(hash_map<const BasicBlock*,
192 BBLiveVar*> &BBLiveVarInfo) {
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_end(&BB);
203 BBLiveVar *PredLVBB = BBLiveVarInfo[*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 std::cerr << " Defs: "; printSet(DefSet); std::cerr << "\n";
224 std::cerr << " In: "; printSet(InSet); std::cerr << "\n";
225 std::cerr << " Out: "; printSet(OutSet); std::cerr << "\n";
228 void BBLiveVar::printInOutSets() const {
229 std::cerr << " In: "; printSet(InSet); std::cerr << "\n";
230 std::cerr << " Out: "; printSet(OutSet); std::cerr << "\n";
233 } // End llvm namespace