1 #include "llvm/Analysis/LiveVar/BBLiveVar.h"
2 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
3 #include "llvm/CodeGen/MachineInstr.h"
4 #include "../../Target/Sparc/SparcInternals.h" // Only for PHI defn
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
10 BBLiveVar::BBLiveVar( const BasicBlock *const baseBB, unsigned int RdfoId)
11 : BaseBB(baseBB), DefSet(), InSet(),
12 OutSet(), PhiArgMap() {
14 InSetChanged = OutSetChanged = false;
19 //-----------------------------------------------------------------------------
20 // caluculates def and use sets for each BB
21 // There are two passes over operands of a machine instruction. This is
22 // because, we can have instructions like V = V + 1, since we no longer
23 // assume single definition.
24 //-----------------------------------------------------------------------------
26 void BBLiveVar::calcDefUseSets()
28 // get the iterator for machine instructions
29 const MachineCodeForBasicBlock& MIVec = BaseBB->getMachineInstrVec();
30 MachineCodeForBasicBlock::const_reverse_iterator
31 MInstIterator = MIVec.rbegin();
33 // iterate over all the machine instructions in BB
34 for( ; MInstIterator != MIVec.rend(); ++MInstIterator) {
36 const MachineInstr * MInst = *MInstIterator; // MInst is the machine inst
39 if( DEBUG_LV > 1) { // debug msg
40 cerr << " *Iterating over machine instr ";
45 // iterate over MI operands to find defs
46 for( MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done() ; ++OpI) {
48 if( OpI.isDef() ) // add to Defs only if this operand is a def
52 // do for implicit operands as well
53 for( unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
54 if( MInst->implicitRefIsDefined(i) )
55 addDef( MInst->getImplicitRef(i) );
59 bool IsPhi = ( MInst->getOpCode() == PHI );
62 // iterate over MI operands to find uses
63 for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done() ; ++OpI) {
64 const Value *Op = *OpI;
66 if ( ((Op)->getType())->isLabelType() )
67 continue; // don't process labels
69 if(! OpI.isDef() ) { // add to Defs only if this operand is a use
72 if( IsPhi ) { // for a phi node
73 // put args into the PhiArgMap (Val -> BB)
75 const Value * ArgVal = Op;
76 ++OpI; // increment to point to BB of value
77 const Value * BBVal = *OpI;
80 assert( (BBVal)->getValueType() == Value::BasicBlockVal );
82 PhiArgMap[ ArgVal ] = (const BasicBlock *) (BBVal);
83 assert( PhiArgMap[ ArgVal ] );
85 if( DEBUG_LV > 1) { // debug msg of level 2
86 cerr << " - phi operand ";
88 cerr << " came from BB ";
89 printValue( PhiArgMap[ ArgVal ]);
99 // do for implicit operands as well
100 for( unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
102 assert( !IsPhi && "Phi cannot have implicit opeands");
103 const Value *Op = MInst->getImplicitRef(i);
105 if ( ((Op)->getType())->isLabelType() )
106 continue; // don't process labels
107 if( ! MInst->implicitRefIsDefined(i) )
111 } // for all machine instructions
116 //-----------------------------------------------------------------------------
117 // To add an operand which is a def
118 //-----------------------------------------------------------------------------
119 void BBLiveVar::addDef(const Value *Op)
121 DefSet.add( Op ); // operand is a def - so add to def set
122 InSet.remove( Op); // this definition kills any uses
126 cerr << " +Def: "; printValue( Op ); cerr << endl;
131 //-----------------------------------------------------------------------------
132 // To add an operand which is a use
133 //-----------------------------------------------------------------------------
134 void BBLiveVar::addUse(const Value *Op)
136 InSet.add( Op ); // An operand is a use - so add to use set
137 OutSet.remove( Op ); // remove if there is a def below this use
140 if( DEBUG_LV > 1) { // debug msg of level 2
141 cerr << " Use: "; printValue( Op ); cerr << endl;
147 //-----------------------------------------------------------------------------
148 // Applies the transfer function to a basic block to produce the InSet using
150 //-----------------------------------------------------------------------------
152 bool BBLiveVar::applyTransferFunc() // calculates the InSet in terms of OutSet
155 // IMPORTANT: caller should check whether the OutSet changed
156 // (else no point in calling)
158 LiveVarSet OutMinusDef; // set to hold (Out[B] - Def[B])
159 OutMinusDef.setDifference( &OutSet, &DefSet);
160 InSetChanged = InSet.setUnion( &OutMinusDef );
162 OutSetChanged = false; // no change to OutSet since transf func applied
168 //-----------------------------------------------------------------------------
169 // calculates Out set using In sets of the predecessors
170 //-----------------------------------------------------------------------------
171 bool BBLiveVar::setPropagate( LiveVarSet *const OutSet,
172 const LiveVarSet *const InSet,
173 const BasicBlock *const PredBB) {
175 LiveVarSet::const_iterator InIt;
176 pair<LiveVarSet::iterator, bool> result;
177 bool changed = false;
178 const BasicBlock *PredBBOfPhiArg;
180 // for all all elements in InSet
181 for( InIt = InSet->begin() ; InIt != InSet->end(); InIt++) {
182 PredBBOfPhiArg = PhiArgMap[ *InIt ];
184 // if this var is not a phi arg OR
185 // it's a phi arg and the var went down from this BB
186 if( !PredBBOfPhiArg || PredBBOfPhiArg == PredBB) {
187 result = OutSet->insert( *InIt ); // insert to this set
188 if( result.second == true) changed = true;
196 //-----------------------------------------------------------------------------
197 // propogates in set to OutSets of PREDECESSORs
198 //-----------------------------------------------------------------------------
199 bool BBLiveVar::applyFlowFunc(BBToBBLiveVarMapType LVMap)
202 // IMPORTANT: caller should check whether inset changed
203 // (else no point in calling)
205 bool needAnotherIt= false; // did this BB change any OutSets of pred.s
206 // whose POId is lower
209 BasicBlock::pred_const_iterator PredBBI = BaseBB->pred_begin();
211 for( ; PredBBI != BaseBB->pred_end() ; PredBBI++) {
212 assert( *PredBBI ); // assert that the predecessor is valid
213 BBLiveVar *PredLVBB = LVMap[*PredBBI];
216 if( setPropagate( &(PredLVBB->OutSet), &InSet, *PredBBI ) == true) {
217 PredLVBB->OutSetChanged = true;
219 // if the predec POId is lower than mine
220 if( PredLVBB->getPOId() <= POId)
221 needAnotherIt = true;
226 return needAnotherIt;
232 /* ----------------- Methods For Debugging (Printing) ----------------- */
234 void BBLiveVar::printAllSets() const
236 cerr << " Defs: "; DefSet.printSet(); cerr << endl;
237 cerr << " In: "; InSet.printSet(); cerr << endl;
238 cerr << " Out: "; OutSet.printSet(); cerr << endl;
241 void BBLiveVar::printInOutSets() const
243 cerr << " In: "; InSet.printSet(); cerr << endl;
244 cerr << " Out: "; OutSet.printSet(); cerr << endl;