aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Analysis/LiveVar/BBLiveVar.cpp
blob: 638e000cc7dd72ad14c8438922202b4e0ea4de89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "llvm/Analysis/LiveVar/BBLiveVar.h"
#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "../../Target/Sparc/SparcInternals.h"  // TODO: FIXME!! Only for PHI defn


/********************* Implementation **************************************/

BBLiveVar::BBLiveVar( const BasicBlock *const  baseBB, unsigned int RdfoId) 
                      : BaseBB(baseBB), DefSet(),  InSet(), 
			OutSet(), PhiArgMap() {  
    BaseBB = baseBB;   
    InSetChanged = OutSetChanged = false;
    POId = RdfoId;
}

// caluculates def and use sets for each BB
// There are two passes over operands of a machine instruction. This is
// because, we can have instructions like V = V + 1, since we no longer
// assume single definition.

void BBLiveVar::calcDefUseSets()  
{
  // get the iterator for machine instructions
  const MachineCodeForBasicBlock& MIVec = BaseBB->getMachineInstrVec();
  MachineCodeForBasicBlock::const_reverse_iterator 
    MInstIterator = MIVec.rbegin();

  // iterate over all the machine instructions in BB
  for( ; MInstIterator != MIVec.rend(); ++MInstIterator) {  

    const MachineInstr * MInst  = *MInstIterator;  // MInst is the machine inst
    assert(MInst);
    
    if( DEBUG_LV > 1) {                            // debug msg
      cout << " *Iterating over machine instr ";
      MInst->dump();
      cout << endl;
    }

    // iterate over  MI operands to find defs
    for( MachineInstr::val_op_const_iterator OpI(MInst); !OpI.done() ; ++OpI) {

      if( OpI.isDef() )      // add to Defs only if this operand is a def
	addDef( *OpI );
    }

    // do for implicit operands as well
    for( unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
      if(  MInst->implicitRefIsDefined(i) )
	addDef( MInst->getImplicitRef(i) );
     }

    
    bool IsPhi = ( MInst->getOpCode() == PHI );

 
    // iterate over  MI operands to find uses
    for(MachineInstr::val_op_const_iterator OpI(MInst); !OpI.done() ;  ++OpI) {
      const Value *Op = *OpI;

      if ( ((Op)->getType())->isLabelType() )    
	continue;             // don't process labels

      if(! OpI.isDef() ) {   // add to Defs only if this operand is a use
	addUse( Op );

	if( IsPhi ) {         // for a phi node
	  // put args into the PhiArgMap (Val -> BB)
	
	  const Value * ArgVal = Op;
	  ++OpI;              // increment to point to BB of value
	  const Value * BBVal = *OpI; 
	  
	  
	  assert( (BBVal)->getValueType() == Value::BasicBlockVal );
	  
	  PhiArgMap[ ArgVal ] = (const BasicBlock *) (BBVal); 
	  assert( PhiArgMap[ ArgVal ] );
	  
	  if( DEBUG_LV > 1) {   // debug msg of level 2
	    cout << "   - phi operand "; 
	    printValue( ArgVal ); 
	    cout  << " came from BB "; 
	    printValue( PhiArgMap[ ArgVal ]); 
	    cout<<endl;
	  }

	} // if( IsPhi )

      } // if a use

    } // for all operands

    // do for implicit operands as well
    for( unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {

      assert( !IsPhi && "Phi cannot have implicit opeands");
      const Value *Op =  MInst->getImplicitRef(i);

      if ( ((Op)->getType())->isLabelType() )    
	continue;             // don't process labels
      if(  ! MInst->implicitRefIsDefined(i) )
	addUse( Op );
     }

  } // for all machine instructions
} 
	

// To add an operand wichi is a def

void  BBLiveVar::addDef(const Value *Op) 
{
  DefSet.add( Op );     // operand is a def - so add to def set
  InSet.remove( Op);    // this definition kills any uses
  InSetChanged = true; 

  if( DEBUG_LV > 1) {   
    cout << "  +Def: "; printValue( Op ); cout << endl;
  }
}

// To add an operand which is a use

void  BBLiveVar::addUse(const Value *Op) 
{
  InSet.add( Op );      // An operand is a use - so add to use set
  OutSet.remove( Op );  // remove if there is a def below this use
  InSetChanged = true; 

  if( DEBUG_LV > 1) {   // debug msg of level 2
    cout << "   Use: "; printValue( Op ); cout << endl;
  }

}



bool BBLiveVar::applyTransferFunc() // calculates the InSet in terms of OutSet 
{

  // IMPORTANT: caller should check whether the OutSet changed 
  //           (else no point in calling)

  LiveVarSet OutMinusDef;     // set to hold (Out[B] - Def[B])
  OutMinusDef.setDifference( &OutSet, &DefSet);
  InSetChanged = InSet.setUnion( &OutMinusDef );
 
  OutSetChanged = false;      // no change to OutSet since transf func applied

  return InSetChanged;
}



// calculates Out set using In sets of the predecessors
bool BBLiveVar::setPropagate( LiveVarSet *const OutSet, 
			      const LiveVarSet *const InSet, 
			      const BasicBlock *const PredBB) {

  LiveVarSet::const_iterator InIt;
  pair<LiveVarSet::iterator, bool> result;
  bool changed = false;
  const BasicBlock *PredBBOfPhiArg;

  // for all all elements in InSet
  for( InIt = InSet->begin() ; InIt != InSet->end(); InIt++) {  
    PredBBOfPhiArg =  PhiArgMap[ *InIt ];

    // if this var is not a phi arg OR 
    // it's a phi arg and the var went down from this BB
    if( !PredBBOfPhiArg || PredBBOfPhiArg == PredBB) {  
      result = OutSet->insert( *InIt );               // insert to this set
      if( result.second == true) changed = true;
    }
  }

  return changed;
} 



// propogates in set to OutSets of PREDECESSORs

bool BBLiveVar::applyFlowFunc(BBToBBLiveVarMapType LVMap) 
{

  // IMPORTANT: caller should check whether inset changed 
  //            (else no point in calling)

  bool needAnotherIt= false;  // did this BB change any OutSets of pred.s 
                              // whose POId is lower


  BasicBlock::pred_const_iterator PredBBI = BaseBB->pred_begin();

  for( ; PredBBI != BaseBB->pred_end() ; PredBBI++) {
    assert( *PredBBI );       // assert that the predecessor is valid
    BBLiveVar  *PredLVBB = LVMap[*PredBBI];

                              // do set union
    if(  setPropagate( &(PredLVBB->OutSet), &InSet, *PredBBI ) == true) {  
      PredLVBB->OutSetChanged = true;

      // if the predec POId is lower than mine
      if( PredLVBB->getPOId() <= POId) 
	needAnotherIt = true;   
    }

  }  // for

  return needAnotherIt;

}



/* ----------------- Methods For Debugging (Printing) ----------------- */

void BBLiveVar::printAllSets() const
{
  cout << "  Defs: ";   DefSet.printSet();  cout << endl;
  cout << "  In: ";   InSet.printSet();  cout << endl;
  cout << "  Out: ";   OutSet.printSet();  cout << endl;
}

void BBLiveVar::printInOutSets() const
{
  cout << "  In: ";   InSet.printSet();  cout << endl;
  cout << "  Out: ";   OutSet.printSet();  cout << endl;
}