@@ -78,11 +78,6 @@ static cl::opt<unsigned> MaxClones(
78
78
" The maximum number of clones allowed for a single function "
79
79
" specialization" ));
80
80
81
- static cl::opt<unsigned > MaxIncomingPhiValues (
82
- " funcspec-max-incoming-phi-values" , cl::init(4 ), cl::Hidden, cl::desc(
83
- " The maximum number of incoming values a PHI node can have to be "
84
- " considered during the specialization bonus estimation" ));
85
-
86
81
static cl::opt<unsigned > MinFunctionSize (
87
82
" funcspec-min-function-size" , cl::init(100 ), cl::Hidden, cl::desc(
88
83
" Don't specialize functions that have less than this number of "
@@ -109,7 +104,6 @@ static cl::opt<bool> SpecializeLiteralConstant(
109
104
// the combination of size and latency savings in comparison to the non
110
105
// specialized version of the function.
111
106
static Cost estimateBasicBlocks (SmallVectorImpl<BasicBlock *> &WorkList,
112
- DenseSet<BasicBlock *> &DeadBlocks,
113
107
ConstMap &KnownConstants, SCCPSolver &Solver,
114
108
BlockFrequencyInfo &BFI,
115
109
TargetTransformInfo &TTI) {
@@ -124,12 +118,6 @@ static Cost estimateBasicBlocks(SmallVectorImpl<BasicBlock *> &WorkList,
124
118
if (!Weight)
125
119
continue ;
126
120
127
- // These blocks are considered dead as far as the InstCostVisitor is
128
- // concerned. They haven't been proven dead yet by the Solver, but
129
- // may become if we propagate the constant specialization arguments.
130
- if (!DeadBlocks.insert (BB).second )
131
- continue ;
132
-
133
121
for (Instruction &I : *BB) {
134
122
// Disregard SSA copies.
135
123
if (auto *II = dyn_cast<IntrinsicInst>(&I))
@@ -164,19 +152,9 @@ static Constant *findConstantFor(Value *V, ConstMap &KnownConstants) {
164
152
return nullptr ;
165
153
}
166
154
167
- Cost InstCostVisitor::getBonusFromPendingPHIs () {
168
- Cost Bonus = 0 ;
169
- while (!PendingPHIs.empty ()) {
170
- Instruction *Phi = PendingPHIs.pop_back_val ();
171
- Bonus += getUserBonus (Phi);
172
- }
173
- return Bonus;
174
- }
175
-
176
155
Cost InstCostVisitor::getUserBonus (Instruction *User, Value *Use, Constant *C) {
177
156
// Cache the iterator before visiting.
178
- LastVisited = Use ? KnownConstants.insert ({Use, C}).first
179
- : KnownConstants.end ();
157
+ LastVisited = KnownConstants.insert ({Use, C}).first ;
180
158
181
159
if (auto *I = dyn_cast<SwitchInst>(User))
182
160
return estimateSwitchInst (*I);
@@ -203,15 +181,13 @@ Cost InstCostVisitor::getUserBonus(Instruction *User, Value *Use, Constant *C) {
203
181
204
182
for (auto *U : User->users ())
205
183
if (auto *UI = dyn_cast<Instruction>(U))
206
- if (UI != User && Solver.isBlockExecutable (UI->getParent ()))
184
+ if (Solver.isBlockExecutable (UI->getParent ()))
207
185
Bonus += getUserBonus (UI, User, C);
208
186
209
187
return Bonus;
210
188
}
211
189
212
190
Cost InstCostVisitor::estimateSwitchInst (SwitchInst &I) {
213
- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
214
-
215
191
if (I.getCondition () != LastVisited->first )
216
192
return 0 ;
217
193
@@ -232,13 +208,10 @@ Cost InstCostVisitor::estimateSwitchInst(SwitchInst &I) {
232
208
WorkList.push_back (BB);
233
209
}
234
210
235
- return estimateBasicBlocks (WorkList, DeadBlocks, KnownConstants, Solver, BFI,
236
- TTI);
211
+ return estimateBasicBlocks (WorkList, KnownConstants, Solver, BFI, TTI);
237
212
}
238
213
239
214
Cost InstCostVisitor::estimateBranchInst (BranchInst &I) {
240
- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
241
-
242
215
if (I.getCondition () != LastVisited->first )
243
216
return 0 ;
244
217
@@ -250,39 +223,10 @@ Cost InstCostVisitor::estimateBranchInst(BranchInst &I) {
250
223
Succ->getUniquePredecessor () == I.getParent ())
251
224
WorkList.push_back (Succ);
252
225
253
- return estimateBasicBlocks (WorkList, DeadBlocks, KnownConstants, Solver, BFI,
254
- TTI);
255
- }
256
-
257
- Constant *InstCostVisitor::visitPHINode (PHINode &I) {
258
- if (I.getNumIncomingValues () > MaxIncomingPhiValues)
259
- return nullptr ;
260
-
261
- bool Inserted = VisitedPHIs.insert (&I).second ;
262
- Constant *Const = nullptr ;
263
-
264
- for (unsigned Idx = 0 , E = I.getNumIncomingValues (); Idx != E; ++Idx) {
265
- Value *V = I.getIncomingValue (Idx);
266
- if (auto *Inst = dyn_cast<Instruction>(V))
267
- if (Inst == &I || DeadBlocks.contains (I.getIncomingBlock (Idx)))
268
- continue ;
269
- Constant *C = findConstantFor (V, KnownConstants);
270
- if (!C) {
271
- if (Inserted)
272
- PendingPHIs.push_back (&I);
273
- return nullptr ;
274
- }
275
- if (!Const)
276
- Const = C;
277
- else if (C != Const)
278
- return nullptr ;
279
- }
280
- return Const;
226
+ return estimateBasicBlocks (WorkList, KnownConstants, Solver, BFI, TTI);
281
227
}
282
228
283
229
Constant *InstCostVisitor::visitFreezeInst (FreezeInst &I) {
284
- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
285
-
286
230
if (isGuaranteedNotToBeUndefOrPoison (LastVisited->second ))
287
231
return LastVisited->second ;
288
232
return nullptr ;
@@ -309,8 +253,6 @@ Constant *InstCostVisitor::visitCallBase(CallBase &I) {
309
253
}
310
254
311
255
Constant *InstCostVisitor::visitLoadInst (LoadInst &I) {
312
- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
313
-
314
256
if (isa<ConstantPointerNull>(LastVisited->second ))
315
257
return nullptr ;
316
258
return ConstantFoldLoadFromConstPtr (LastVisited->second , I.getType (), DL);
@@ -333,8 +275,6 @@ Constant *InstCostVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
333
275
}
334
276
335
277
Constant *InstCostVisitor::visitSelectInst (SelectInst &I) {
336
- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
337
-
338
278
if (I.getCondition () != LastVisited->first )
339
279
return nullptr ;
340
280
@@ -350,8 +290,6 @@ Constant *InstCostVisitor::visitCastInst(CastInst &I) {
350
290
}
351
291
352
292
Constant *InstCostVisitor::visitCmpInst (CmpInst &I) {
353
- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
354
-
355
293
bool Swap = I.getOperand (1 ) == LastVisited->first ;
356
294
Value *V = Swap ? I.getOperand (0 ) : I.getOperand (1 );
357
295
Constant *Other = findConstantFor (V, KnownConstants);
@@ -365,14 +303,10 @@ Constant *InstCostVisitor::visitCmpInst(CmpInst &I) {
365
303
}
366
304
367
305
Constant *InstCostVisitor::visitUnaryOperator (UnaryOperator &I) {
368
- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
369
-
370
306
return ConstantFoldUnaryOpOperand (I.getOpcode (), LastVisited->second , DL);
371
307
}
372
308
373
309
Constant *InstCostVisitor::visitBinaryOperator (BinaryOperator &I) {
374
- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
375
-
376
310
bool Swap = I.getOperand (1 ) == LastVisited->first ;
377
311
Value *V = Swap ? I.getOperand (0 ) : I.getOperand (1 );
378
312
Constant *Other = findConstantFor (V, KnownConstants);
@@ -779,17 +713,13 @@ bool FunctionSpecializer::findSpecializations(Function *F, Cost SpecCost,
779
713
AllSpecs[Index].CallSites .push_back (&CS);
780
714
} else {
781
715
// Calculate the specialisation gain.
782
- Cost Score = 0 ;
716
+ Cost Score = 0 - SpecCost ;
783
717
InstCostVisitor Visitor = getInstCostVisitorFor (F);
784
718
for (ArgInfo &A : S.Args )
785
719
Score += getSpecializationBonus (A.Formal , A.Actual , Visitor);
786
- Score += Visitor.getBonusFromPendingPHIs ();
787
-
788
- LLVM_DEBUG (dbgs () << " FnSpecialization: Specialization score = "
789
- << Score << " \n " );
790
720
791
721
// Discard unprofitable specialisations.
792
- if (!ForceSpecialization && Score <= SpecCost )
722
+ if (!ForceSpecialization && Score <= 0 )
793
723
continue ;
794
724
795
725
// Create a new specialisation entry.
0 commit comments