blob: 638e000cc7dd72ad14c8438922202b4e0ea4de89 [file] [log] [blame]
Ruchira Sasanka683847f2001-07-24 17:14:13 +00001#include "llvm/Analysis/LiveVar/BBLiveVar.h"
Ruchira Sasankae27c3442001-08-20 21:12:49 +00002#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
3#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerf6374bf2001-09-14 03:55:11 +00004#include "../../Target/Sparc/SparcInternals.h" // TODO: FIXME!! Only for PHI defn
Ruchira Sasanka683847f2001-07-24 17:14:13 +00005
6
7/********************* Implementation **************************************/
8
Ruchira Sasankae27c3442001-08-20 21:12:49 +00009BBLiveVar::BBLiveVar( const BasicBlock *const baseBB, unsigned int RdfoId)
10 : BaseBB(baseBB), DefSet(), InSet(),
11 OutSet(), PhiArgMap() {
Ruchira Sasanka683847f2001-07-24 17:14:13 +000012 BaseBB = baseBB;
13 InSetChanged = OutSetChanged = false;
14 POId = RdfoId;
15}
16
Ruchira Sasankae27c3442001-08-20 21:12:49 +000017// caluculates def and use sets for each BB
18// There are two passes over operands of a machine instruction. This is
19// because, we can have instructions like V = V + 1, since we no longer
20// assume single definition.
Ruchira Sasanka683847f2001-07-24 17:14:13 +000021
Ruchira Sasankae27c3442001-08-20 21:12:49 +000022void BBLiveVar::calcDefUseSets()
Ruchira Sasanka683847f2001-07-24 17:14:13 +000023{
Ruchira Sasankae27c3442001-08-20 21:12:49 +000024 // get the iterator for machine instructions
25 const MachineCodeForBasicBlock& MIVec = BaseBB->getMachineInstrVec();
26 MachineCodeForBasicBlock::const_reverse_iterator
27 MInstIterator = MIVec.rbegin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000028
Ruchira Sasankae27c3442001-08-20 21:12:49 +000029 // iterate over all the machine instructions in BB
30 for( ; MInstIterator != MIVec.rend(); ++MInstIterator) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +000031
Ruchira Sasankae27c3442001-08-20 21:12:49 +000032 const MachineInstr * MInst = *MInstIterator; // MInst is the machine inst
33 assert(MInst);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000034
Ruchira Sasankae27c3442001-08-20 21:12:49 +000035 if( DEBUG_LV > 1) { // debug msg
36 cout << " *Iterating over machine instr ";
37 MInst->dump();
38 cout << endl;
39 }
40
41 // iterate over MI operands to find defs
42 for( MachineInstr::val_op_const_iterator OpI(MInst); !OpI.done() ; ++OpI) {
43
Ruchira Sasankac1daae82001-10-12 17:47:23 +000044 if( OpI.isDef() ) // add to Defs only if this operand is a def
45 addDef( *OpI );
Ruchira Sasankae27c3442001-08-20 21:12:49 +000046 }
47
Ruchira Sasankac1daae82001-10-12 17:47:23 +000048 // do for implicit operands as well
49 for( unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
50 if( MInst->implicitRefIsDefined(i) )
51 addDef( MInst->getImplicitRef(i) );
52 }
53
54
Ruchira Sasankae27c3442001-08-20 21:12:49 +000055 bool IsPhi = ( MInst->getOpCode() == PHI );
56
57
58 // iterate over MI operands to find uses
59 for(MachineInstr::val_op_const_iterator OpI(MInst); !OpI.done() ; ++OpI) {
60 const Value *Op = *OpI;
61
62 if ( ((Op)->getType())->isLabelType() )
63 continue; // don't process labels
64
Ruchira Sasankac1daae82001-10-12 17:47:23 +000065 if(! OpI.isDef() ) { // add to Defs only if this operand is a use
66 addUse( Op );
Ruchira Sasankae27c3442001-08-20 21:12:49 +000067
68 if( IsPhi ) { // for a phi node
Ruchira Sasankac1daae82001-10-12 17:47:23 +000069 // put args into the PhiArgMap (Val -> BB)
70
Ruchira Sasankae27c3442001-08-20 21:12:49 +000071 const Value * ArgVal = Op;
72 ++OpI; // increment to point to BB of value
73 const Value * BBVal = *OpI;
Ruchira Sasankac1daae82001-10-12 17:47:23 +000074
75
Ruchira Sasankae27c3442001-08-20 21:12:49 +000076 assert( (BBVal)->getValueType() == Value::BasicBlockVal );
77
78 PhiArgMap[ ArgVal ] = (const BasicBlock *) (BBVal);
79 assert( PhiArgMap[ ArgVal ] );
Ruchira Sasankac1daae82001-10-12 17:47:23 +000080
Ruchira Sasankae27c3442001-08-20 21:12:49 +000081 if( DEBUG_LV > 1) { // debug msg of level 2
82 cout << " - phi operand ";
83 printValue( ArgVal );
84 cout << " came from BB ";
85 printValue( PhiArgMap[ ArgVal ]);
86 cout<<endl;
87 }
88
Ruchira Sasankac1daae82001-10-12 17:47:23 +000089 } // if( IsPhi )
Ruchira Sasankae27c3442001-08-20 21:12:49 +000090
Ruchira Sasankac1daae82001-10-12 17:47:23 +000091 } // if a use
92
93 } // for all operands
94
95 // do for implicit operands as well
96 for( unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
97
98 assert( !IsPhi && "Phi cannot have implicit opeands");
99 const Value *Op = MInst->getImplicitRef(i);
100
101 if ( ((Op)->getType())->isLabelType() )
102 continue; // don't process labels
103 if( ! MInst->implicitRefIsDefined(i) )
104 addUse( Op );
105 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000106
107 } // for all machine instructions
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000108}
109
110
Ruchira Sasankac1daae82001-10-12 17:47:23 +0000111// To add an operand wichi is a def
112
113void BBLiveVar::addDef(const Value *Op)
114{
115 DefSet.add( Op ); // operand is a def - so add to def set
116 InSet.remove( Op); // this definition kills any uses
117 InSetChanged = true;
118
119 if( DEBUG_LV > 1) {
120 cout << " +Def: "; printValue( Op ); cout << endl;
121 }
122}
123
124// To add an operand which is a use
125
126void BBLiveVar::addUse(const Value *Op)
127{
128 InSet.add( Op ); // An operand is a use - so add to use set
129 OutSet.remove( Op ); // remove if there is a def below this use
130 InSetChanged = true;
131
132 if( DEBUG_LV > 1) { // debug msg of level 2
133 cout << " Use: "; printValue( Op ); cout << endl;
134 }
135
136}
137
138
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000139
140bool BBLiveVar::applyTransferFunc() // calculates the InSet in terms of OutSet
141{
142
143 // IMPORTANT: caller should check whether the OutSet changed
144 // (else no point in calling)
145
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000146 LiveVarSet OutMinusDef; // set to hold (Out[B] - Def[B])
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000147 OutMinusDef.setDifference( &OutSet, &DefSet);
148 InSetChanged = InSet.setUnion( &OutMinusDef );
149
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000150 OutSetChanged = false; // no change to OutSet since transf func applied
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000151
152 return InSetChanged;
153}
154
155
156
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000157// calculates Out set using In sets of the predecessors
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000158bool BBLiveVar::setPropagate( LiveVarSet *const OutSet,
159 const LiveVarSet *const InSet,
160 const BasicBlock *const PredBB) {
161
162 LiveVarSet::const_iterator InIt;
163 pair<LiveVarSet::iterator, bool> result;
164 bool changed = false;
165 const BasicBlock *PredBBOfPhiArg;
166
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000167 // for all all elements in InSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000168 for( InIt = InSet->begin() ; InIt != InSet->end(); InIt++) {
169 PredBBOfPhiArg = PhiArgMap[ *InIt ];
170
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000171 // if this var is not a phi arg OR
172 // it's a phi arg and the var went down from this BB
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000173 if( !PredBBOfPhiArg || PredBBOfPhiArg == PredBB) {
174 result = OutSet->insert( *InIt ); // insert to this set
175 if( result.second == true) changed = true;
176 }
177 }
178
179 return changed;
180}
181
182
183
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000184// propogates in set to OutSets of PREDECESSORs
185
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000186bool BBLiveVar::applyFlowFunc(BBToBBLiveVarMapType LVMap)
187{
188
189 // IMPORTANT: caller should check whether inset changed
190 // (else no point in calling)
191
192 bool needAnotherIt= false; // did this BB change any OutSets of pred.s
193 // whose POId is lower
194
195
Chris Lattnerf0604b82001-10-01 13:19:53 +0000196 BasicBlock::pred_const_iterator PredBBI = BaseBB->pred_begin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000197
Chris Lattnerf0604b82001-10-01 13:19:53 +0000198 for( ; PredBBI != BaseBB->pred_end() ; PredBBI++) {
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000199 assert( *PredBBI ); // assert that the predecessor is valid
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000200 BBLiveVar *PredLVBB = LVMap[*PredBBI];
201
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000202 // do set union
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000203 if( setPropagate( &(PredLVBB->OutSet), &InSet, *PredBBI ) == true) {
204 PredLVBB->OutSetChanged = true;
205
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000206 // if the predec POId is lower than mine
207 if( PredLVBB->getPOId() <= POId)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000208 needAnotherIt = true;
209 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000210
211 } // for
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000212
213 return needAnotherIt;
214
215}
216
217
218
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000219/* ----------------- Methods For Debugging (Printing) ----------------- */
220
221void BBLiveVar::printAllSets() const
222{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000223 cout << " Defs: "; DefSet.printSet(); cout << endl;
224 cout << " In: "; InSet.printSet(); cout << endl;
225 cout << " Out: "; OutSet.printSet(); cout << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000226}
227
228void BBLiveVar::printInOutSets() const
229{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000230 cout << " In: "; InSet.printSet(); cout << endl;
231 cout << " Out: "; OutSet.printSet(); cout << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000232}
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000233
234
235
236