diff --git a/llvm/include/llvm/Analysis/InlineCost.h b/llvm/include/llvm/Analysis/InlineCost.h --- a/llvm/include/llvm/Analysis/InlineCost.h +++ b/llvm/include/llvm/Analysis/InlineCost.h @@ -233,6 +233,15 @@ Optional> GetBFI, ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE); +/// Returns InlineResult::success() if the call site should be always inlined +/// because of user directives, and the inlining is viable. Returns +/// InlineResult::failure() if the inlining may never happen because of user +/// directives or incompatibilities detectable without needing callee traversal. +/// Otherwise returns None, meaning that inlining should be decided based on +/// other criteria (e.g. cost modeling). +Optional getFastInliningDecision(CallBase &Call, Function *Callee, + TargetTransformInfo &CalleeTTI); + /// Minimal filter to detect invalid constructs for inlining. InlineResult isInlineViable(Function &Callee); } // namespace llvm diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp --- a/llvm/lib/Analysis/InlineCost.cpp +++ b/llvm/lib/Analysis/InlineCost.cpp @@ -2112,16 +2112,13 @@ GetAssumptionCache, GetBFI, PSI, ORE); } -InlineCost llvm::getInlineCost( - CallBase &Call, Function *Callee, const InlineParams &Params, - TargetTransformInfo &CalleeTTI, - std::function &GetAssumptionCache, - Optional> GetBFI, - ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) { +Optional +llvm::getFastInliningDecision(CallBase &Call, Function *Callee, + TargetTransformInfo &CalleeTTI) { // Cannot inline indirect calls. if (!Callee) - return llvm::InlineCost::getNever("indirect call"); + return InlineResult::failure("indirect call"); // Never inline calls with byval arguments that does not have the alloca // address space. Since byval arguments can be replaced with a copy to an @@ -2133,8 +2130,8 @@ if (Call.isByValArgument(I)) { PointerType *PTy = cast(Call.getArgOperand(I)->getType()); if (PTy->getAddressSpace() != AllocaAS) - return llvm::InlineCost::getNever("byval arguments without alloca" - " address space"); + return InlineResult::failure("byval arguments without alloca" + " address space"); } // Calls to functions with always-inline attributes should be inlined @@ -2142,39 +2139,58 @@ if (Call.hasFnAttr(Attribute::AlwaysInline)) { auto IsViable = isInlineViable(*Callee); if (IsViable.isSuccess()) - return llvm::InlineCost::getAlways("always inline attribute"); - return llvm::InlineCost::getNever(IsViable.getFailureReason()); + return InlineResult::success(); + return InlineResult::failure(IsViable.getFailureReason()); } // Never inline functions with conflicting attributes (unless callee has // always-inline attribute). Function *Caller = Call.getCaller(); if (!functionsHaveCompatibleAttributes(Caller, Callee, CalleeTTI)) - return llvm::InlineCost::getNever("conflicting attributes"); + return InlineResult::failure("conflicting attributes"); // Don't inline this call if the caller has the optnone attribute. if (Caller->hasOptNone()) - return llvm::InlineCost::getNever("optnone attribute"); + return InlineResult::failure("optnone attribute"); // Don't inline a function that treats null pointer as valid into a caller // that does not have this attribute. if (!Caller->nullPointerIsDefined() && Callee->nullPointerIsDefined()) - return llvm::InlineCost::getNever("nullptr definitions incompatible"); + return InlineResult::failure("nullptr definitions incompatible"); // Don't inline functions which can be interposed at link-time. if (Callee->isInterposable()) - return llvm::InlineCost::getNever("interposable"); + return InlineResult::failure("interposable"); // Don't inline functions marked noinline. if (Callee->hasFnAttribute(Attribute::NoInline)) - return llvm::InlineCost::getNever("noinline function attribute"); + return InlineResult::failure("noinline function attribute"); // Don't inline call sites marked noinline. if (Call.isNoInline()) - return llvm::InlineCost::getNever("noinline call site attribute"); + return InlineResult::failure("noinline call site attribute"); + + return None; +} + +InlineCost llvm::getInlineCost( + CallBase &Call, Function *Callee, const InlineParams &Params, + TargetTransformInfo &CalleeTTI, + std::function &GetAssumptionCache, + Optional> GetBFI, + ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) { + + auto UserDecision = llvm::getFastInliningDecision(Call, Callee, CalleeTTI); + + if (UserDecision.hasValue()) { + if (UserDecision->isSuccess()) + return llvm::InlineCost::getAlways("always inline attribute"); + return llvm::InlineCost::getNever(UserDecision->getFailureReason()); + } LLVM_DEBUG(llvm::dbgs() << " Analyzing call of " << Callee->getName() - << "... (caller:" << Caller->getName() << ")\n"); + << "... (caller:" << Call.getCaller()->getName() + << ")\n"); InlineCostCallAnalyzer CA(CalleeTTI, GetAssumptionCache, GetBFI, PSI, ORE, *Callee, Call, Params);