diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp --- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -302,6 +302,9 @@ continue; } + LLVM_DEBUG(dbgs() << "FnSpecialization: Specialization cost for " + << F->getName() << " is " << Cost << "\n"); + auto ConstArgs = calculateGains(F, Cost); if (ConstArgs.empty()) { LLVM_DEBUG(dbgs() << "FnSpecialization: no possible constants found\n"); @@ -385,8 +388,6 @@ // argument can take on. If specialization is not profitable, we continue // on to the next argument. for (Argument &FormalArg : F->args()) { - LLVM_DEBUG(dbgs() << "FnSpecialization: Analysing arg: " - << FormalArg.getName() << "\n"); // Determine if this argument is interesting. If we know the argument can // take on any constant values, they are collected in Constants. If the // argument can only ever equal a constant value in Constants, the @@ -396,7 +397,9 @@ bool IsPartial = true; SmallVector ActualArgs; if (!isArgumentInteresting(&FormalArg, ActualArgs, IsPartial)) { - LLVM_DEBUG(dbgs() << "FnSpecialization: Argument is not interesting\n"); + LLVM_DEBUG(dbgs() << "FnSpecialization: Argument " + << FormalArg.getNameOrAsOperand() + << " is not interesting\n"); continue; } @@ -422,10 +425,10 @@ // Truncate the worklist to 'MaxClonesThreshold' candidates if // necessary. if (Worklist.size() > MaxClonesThreshold) { - LLVM_DEBUG(dbgs() << "FnSpecialization: number of candidates exceed " - << "the maximum number of clones threshold.\n" - << "Truncating worklist to " << MaxClonesThreshold - << " candidates.\n"); + LLVM_DEBUG(dbgs() << "FnSpecialization: Number of candidates exceed " + << "the maximum number of clones threshold.\n" + << "FnSpecialization: Truncating worklist to " + << MaxClonesThreshold << " candidates.\n"); Worklist.erase(Worklist.begin() + MaxClonesThreshold, Worklist.end()); } @@ -434,14 +437,16 @@ for (auto &ActualArg : Worklist) ActualArg.Partial = true; - LLVM_DEBUG(dbgs() << "Sorted list of candidates by gain:\n"; - for (auto &C - : Worklist) { - dbgs() << "- Function = " << C.Fn->getName() << ", "; - dbgs() << "FormalArg = " << C.Formal->getName() << ", "; - dbgs() << "ActualArg = " << C.Actual->getName() << ", "; - dbgs() << "Gain = " << C.Gain << "\n"; - }); + LLVM_DEBUG( + dbgs() << "FnSpecialization: Specializations for function " + << F->getName() << "\n"; + for (auto &C : Worklist) { + dbgs() << "FnSpecialization: FormalArg = " + << C.Formal->getNameOrAsOperand() << ", ActualArg = " + << C.Actual->getNameOrAsOperand() << ", Gain = " + << C.Gain << "\n"; + } + ); // FIXME: Only one argument per function. break; @@ -551,13 +556,13 @@ DominatorTree DT(*F); LoopInfo LI(DT); auto &TTI = (GetTTI)(*F); - LLVM_DEBUG(dbgs() << "FnSpecialization: Analysing bonus for: " << *A - << "\n"); + LLVM_DEBUG(dbgs() << "FnSpecialization: Analysing bonus for constant: " + << C->getNameOrAsOperand() << "\n"); InstructionCost TotalCost = 0; for (auto *U : A->users()) { TotalCost += getUserBonus(U, TTI, LI); - LLVM_DEBUG(dbgs() << "FnSpecialization: User cost "; + LLVM_DEBUG(dbgs() << "FnSpecialization: User cost "; TotalCost.print(dbgs()); dbgs() << " for: " << *U << "\n"); } @@ -615,6 +620,9 @@ Bonus += Params.DefaultThreshold; else if (IC.isVariable() && IC.getCostDelta() > 0) Bonus += IC.getCostDelta(); + + LLVM_DEBUG(dbgs() << "FnSpecialization: Inlining bonus " << Bonus + << " for user " << *U << "\n"); } return TotalCost + Bonus; @@ -644,8 +652,9 @@ // If the argument isn't overdefined, there's nothing to do. It should // already be constant. if (!Solver.getLatticeValueFor(A).isOverdefined()) { - LLVM_DEBUG(dbgs() << "FnSpecialization: nothing to do, arg is already " - << "constant?\n"); + LLVM_DEBUG(dbgs() << "FnSpecialization: Nothing to do, argument " + << A->getNameOrAsOperand() + << " is already constant?\n"); return false; } @@ -663,7 +672,8 @@ // TODO 2: this currently does not support constants, i.e. integer ranges. // IsPartial = !getPossibleConstants(A, Constants); - LLVM_DEBUG(dbgs() << "FnSpecialization: interesting arg: " << *A << "\n"); + LLVM_DEBUG(dbgs() << "FnSpecialization: Found interesting argument " + << A->getNameOrAsOperand() << "\n"); return true; } @@ -748,7 +758,15 @@ continue; CallSitesToRewrite.push_back(&CS); } + + LLVM_DEBUG(dbgs() << "FnSpecialization: Replacing call sites of " + << F->getName() << " with " + << Clone->getName() << "\n"); + for (auto *CS : CallSitesToRewrite) { + LLVM_DEBUG(dbgs() << "FnSpecialization: " + << CS->getFunction()->getName() << " ->" + << *CS << "\n"); if ((CS->getFunction() == Clone && CS->getArgOperand(ArgNo) == &Arg) || CS->getArgOperand(ArgNo) == C) { CS->setCalledFunction(Clone); @@ -774,7 +792,7 @@ for (Argument &Arg : F->args()) if (!Arg.use_empty() && tryToReplaceWithConstant(&Arg)) LLVM_DEBUG(dbgs() << "FnSpecialization: Replaced constant argument: " - << Arg.getName() << "\n"); + << Arg.getNameOrAsOperand() << "\n"); } } }; @@ -885,6 +903,7 @@ unsigned I = 0; while (FuncSpecializationMaxIters != I++ && FS.specializeFunctions(FuncDecls, WorkList)) { + LLVM_DEBUG(dbgs() << "FnSpecialization: Finished iteration " << I << "\n"); // Run the solver for the specialized functions. RunSCCPSolver(WorkList); @@ -896,6 +915,9 @@ Changed = true; } + LLVM_DEBUG(dbgs() << "FnSpecialization: Number of specializations = " + << NumFuncSpecialized <<"\n"); + // Clean up the IR by removing dead instructions and ssa_copy intrinsics. FS.removeDeadInstructions(); removeSSACopy(M); diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp --- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp +++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp @@ -538,7 +538,11 @@ // over from the old function. for (Argument *OldArg = F->arg_begin(), *NewArg = A->getParent()->arg_begin(), *End = F->arg_end(); - OldArg != End; ++OldArg, ++NewArg) + OldArg != End; ++OldArg, ++NewArg) { + + LLVM_DEBUG(dbgs() << "SCCP: Marking argument " + << NewArg->getNameOrAsOperand() << "\n"); + if (NewArg != A && ValueState.count(OldArg)) { // Note: This previously looked like this: // ValueState[NewArg] = ValueState[OldArg]; @@ -549,6 +553,7 @@ NewValue = ValueState[OldArg]; pushToWorkList(NewValue, NewArg); } + } } void SCCPInstVisitor::visitInstruction(Instruction &I) {