@@ -78,11 +78,6 @@ static cl::opt<unsigned> MaxClones(
7878 " The maximum number of clones allowed for a single function "
7979 " specialization" ));
8080
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-
8681static cl::opt<unsigned > MinFunctionSize (
8782 " funcspec-min-function-size" , cl::init(100 ), cl::Hidden, cl::desc(
8883 " Don't specialize functions that have less than this number of "
@@ -109,7 +104,6 @@ static cl::opt<bool> SpecializeLiteralConstant(
109104// the combination of size and latency savings in comparison to the non
110105// specialized version of the function.
111106static Cost estimateBasicBlocks (SmallVectorImpl<BasicBlock *> &WorkList,
112- DenseSet<BasicBlock *> &DeadBlocks,
113107 ConstMap &KnownConstants, SCCPSolver &Solver,
114108 BlockFrequencyInfo &BFI,
115109 TargetTransformInfo &TTI) {
@@ -124,12 +118,6 @@ static Cost estimateBasicBlocks(SmallVectorImpl<BasicBlock *> &WorkList,
124118 if (!Weight)
125119 continue ;
126120
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-
133121 for (Instruction &I : *BB) {
134122 // Disregard SSA copies.
135123 if (auto *II = dyn_cast<IntrinsicInst>(&I))
@@ -164,19 +152,9 @@ static Constant *findConstantFor(Value *V, ConstMap &KnownConstants) {
164152 return nullptr ;
165153}
166154
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-
176155Cost InstCostVisitor::getUserBonus (Instruction *User, Value *Use, Constant *C) {
177156 // Cache the iterator before visiting.
178- LastVisited = Use ? KnownConstants.insert ({Use, C}).first
179- : KnownConstants.end ();
157+ LastVisited = KnownConstants.insert ({Use, C}).first ;
180158
181159 if (auto *I = dyn_cast<SwitchInst>(User))
182160 return estimateSwitchInst (*I);
@@ -203,15 +181,13 @@ Cost InstCostVisitor::getUserBonus(Instruction *User, Value *Use, Constant *C) {
203181
204182 for (auto *U : User->users ())
205183 if (auto *UI = dyn_cast<Instruction>(U))
206- if (UI != User && Solver.isBlockExecutable (UI->getParent ()))
184+ if (Solver.isBlockExecutable (UI->getParent ()))
207185 Bonus += getUserBonus (UI, User, C);
208186
209187 return Bonus;
210188}
211189
212190Cost InstCostVisitor::estimateSwitchInst (SwitchInst &I) {
213- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
214-
215191 if (I.getCondition () != LastVisited->first )
216192 return 0 ;
217193
@@ -232,13 +208,10 @@ Cost InstCostVisitor::estimateSwitchInst(SwitchInst &I) {
232208 WorkList.push_back (BB);
233209 }
234210
235- return estimateBasicBlocks (WorkList, DeadBlocks, KnownConstants, Solver, BFI,
236- TTI);
211+ return estimateBasicBlocks (WorkList, KnownConstants, Solver, BFI, TTI);
237212}
238213
239214Cost InstCostVisitor::estimateBranchInst (BranchInst &I) {
240- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
241-
242215 if (I.getCondition () != LastVisited->first )
243216 return 0 ;
244217
@@ -250,39 +223,10 @@ Cost InstCostVisitor::estimateBranchInst(BranchInst &I) {
250223 Succ->getUniquePredecessor () == I.getParent ())
251224 WorkList.push_back (Succ);
252225
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);
281227}
282228
283229Constant *InstCostVisitor::visitFreezeInst (FreezeInst &I) {
284- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
285-
286230 if (isGuaranteedNotToBeUndefOrPoison (LastVisited->second ))
287231 return LastVisited->second ;
288232 return nullptr ;
@@ -309,8 +253,6 @@ Constant *InstCostVisitor::visitCallBase(CallBase &I) {
309253}
310254
311255Constant *InstCostVisitor::visitLoadInst (LoadInst &I) {
312- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
313-
314256 if (isa<ConstantPointerNull>(LastVisited->second ))
315257 return nullptr ;
316258 return ConstantFoldLoadFromConstPtr (LastVisited->second , I.getType (), DL);
@@ -333,8 +275,6 @@ Constant *InstCostVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
333275}
334276
335277Constant *InstCostVisitor::visitSelectInst (SelectInst &I) {
336- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
337-
338278 if (I.getCondition () != LastVisited->first )
339279 return nullptr ;
340280
@@ -350,8 +290,6 @@ Constant *InstCostVisitor::visitCastInst(CastInst &I) {
350290}
351291
352292Constant *InstCostVisitor::visitCmpInst (CmpInst &I) {
353- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
354-
355293 bool Swap = I.getOperand (1 ) == LastVisited->first ;
356294 Value *V = Swap ? I.getOperand (0 ) : I.getOperand (1 );
357295 Constant *Other = findConstantFor (V, KnownConstants);
@@ -365,14 +303,10 @@ Constant *InstCostVisitor::visitCmpInst(CmpInst &I) {
365303}
366304
367305Constant *InstCostVisitor::visitUnaryOperator (UnaryOperator &I) {
368- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
369-
370306 return ConstantFoldUnaryOpOperand (I.getOpcode (), LastVisited->second , DL);
371307}
372308
373309Constant *InstCostVisitor::visitBinaryOperator (BinaryOperator &I) {
374- assert (LastVisited != KnownConstants.end () && " Invalid iterator!" );
375-
376310 bool Swap = I.getOperand (1 ) == LastVisited->first ;
377311 Value *V = Swap ? I.getOperand (0 ) : I.getOperand (1 );
378312 Constant *Other = findConstantFor (V, KnownConstants);
@@ -779,17 +713,13 @@ bool FunctionSpecializer::findSpecializations(Function *F, Cost SpecCost,
779713 AllSpecs[Index].CallSites .push_back (&CS);
780714 } else {
781715 // Calculate the specialisation gain.
782- Cost Score = 0 ;
716+ Cost Score = 0 - SpecCost ;
783717 InstCostVisitor Visitor = getInstCostVisitorFor (F);
784718 for (ArgInfo &A : S.Args )
785719 Score += getSpecializationBonus (A.Formal , A.Actual , Visitor);
786- Score += Visitor.getBonusFromPendingPHIs ();
787-
788- LLVM_DEBUG (dbgs () << " FnSpecialization: Specialization score = "
789- << Score << " \n " );
790720
791721 // Discard unprofitable specialisations.
792- if (!ForceSpecialization && Score <= SpecCost )
722+ if (!ForceSpecialization && Score <= 0 )
793723 continue ;
794724
795725 // Create a new specialisation entry.
0 commit comments