@@ -142,6 +142,55 @@ void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB) {
142
142
NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;
143
143
}
144
144
145
+ // CountBonusForConstant - Figure out an approximation for how much per-call
146
+ // performance boost we can expect if the specified value is constant.
147
+ unsigned CodeMetrics::CountBonusForConstant (Value *V) {
148
+ unsigned Bonus = 0 ;
149
+ for (Value::use_iterator UI = V->use_begin (), E = V->use_end (); UI != E;++UI){
150
+ User *U = *UI;
151
+ if (CallInst *CI = dyn_cast<CallInst>(U)) {
152
+ // Turning an indirect call into a direct call is a BIG win
153
+ if (CI->getCalledValue () == V)
154
+ Bonus += InlineConstants::IndirectCallBonus;
155
+ }
156
+ else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) {
157
+ // Turning an indirect call into a direct call is a BIG win
158
+ if (II->getCalledValue () == V)
159
+ Bonus += InlineConstants::IndirectCallBonus;
160
+ }
161
+ // FIXME: Eliminating conditional branches and switches should
162
+ // also yield a per-call performance boost.
163
+ else {
164
+ // Figure out the bonuses that wll accrue due to simple constant
165
+ // propagation.
166
+ Instruction &Inst = cast<Instruction>(*U);
167
+
168
+ // We can't constant propagate instructions which have effects or
169
+ // read memory.
170
+ //
171
+ // FIXME: It would be nice to capture the fact that a load from a
172
+ // pointer-to-constant-global is actually a *really* good thing to zap.
173
+ // Unfortunately, we don't know the pointer that may get propagated here,
174
+ // so we can't make this decision.
175
+ if (Inst.mayReadFromMemory () || Inst.mayHaveSideEffects () ||
176
+ isa<AllocaInst>(Inst))
177
+ continue ;
178
+
179
+ bool AllOperandsConstant = true ;
180
+ for (unsigned i = 0 , e = Inst.getNumOperands (); i != e; ++i)
181
+ if (!isa<Constant>(Inst.getOperand (i)) && Inst.getOperand (i) != V) {
182
+ AllOperandsConstant = false ;
183
+ break ;
184
+ }
185
+
186
+ if (AllOperandsConstant)
187
+ Bonus += CountBonusForConstant (&Inst);
188
+ }
189
+ }
190
+ return Bonus;
191
+ }
192
+
193
+
145
194
// CountCodeReductionForConstant - Figure out an approximation for how many
146
195
// instructions will be constant folded if the specified value is constant.
147
196
//
@@ -158,14 +207,6 @@ unsigned CodeMetrics::CountCodeReductionForConstant(Value *V) {
158
207
Instrs += NumBBInsts[TI.getSuccessor (I)];
159
208
// We don't know which blocks will be eliminated, so use the average size.
160
209
Reduction += InlineConstants::InstrCost*Instrs*(NumSucc-1 )/NumSucc;
161
- } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
162
- // Turning an indirect call into a direct call is a BIG win
163
- if (CI->getCalledValue () == V)
164
- Reduction += InlineConstants::IndirectCallBonus;
165
- } else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) {
166
- // Turning an indirect call into a direct call is a BIG win
167
- if (II->getCalledValue () == V)
168
- Reduction += InlineConstants::IndirectCallBonus;
169
210
} else {
170
211
// Figure out if this instruction will be removed due to simple constant
171
212
// propagation.
@@ -259,7 +300,8 @@ void InlineCostAnalyzer::FunctionInfo::analyzeFunction(Function *F) {
259
300
ArgumentWeights.reserve (F->arg_size ());
260
301
for (Function::arg_iterator I = F->arg_begin (), E = F->arg_end (); I != E; ++I)
261
302
ArgumentWeights.push_back (ArgInfo (Metrics.CountCodeReductionForConstant (I),
262
- Metrics.CountCodeReductionForAlloca (I)));
303
+ Metrics.CountCodeReductionForAlloca (I),
304
+ Metrics.CountBonusForConstant (I)));
263
305
}
264
306
265
307
// / NeverInline - returns true if the function should never be inlined into
@@ -383,7 +425,8 @@ InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS,
383
425
// away with this information.
384
426
} else if (isa<Constant>(I)) {
385
427
if (ArgNo < CalleeFI->ArgumentWeights .size ())
386
- InlineCost -= CalleeFI->ArgumentWeights [ArgNo].ConstantWeight ;
428
+ InlineCost -= (CalleeFI->ArgumentWeights [ArgNo].ConstantWeight +
429
+ CalleeFI->ArgumentWeights [ArgNo].ConstantBonus );
387
430
}
388
431
}
389
432
0 commit comments