diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp index 7bd1f18004580..fe1ceb74429c9 100644 --- a/llvm/lib/Analysis/InlineCost.cpp +++ b/llvm/lib/Analysis/InlineCost.cpp @@ -391,7 +391,8 @@ class CallAnalyzer : public InstVisitor { /// likely simplifications post-inlining. The most important aspect we track /// is CFG altering simplifications -- when we prove a basic block dead, that /// can cause dramatic shifts in the cost of inlining a function. - DenseMap SimplifiedValues; + /// Note: The simplified Value may be owned by the caller function. + DenseMap SimplifiedValues; /// Keep track of the values which map back (through function arguments) to /// allocas on the caller stack which could be simplified through SROA. @@ -432,7 +433,7 @@ class CallAnalyzer : public InstVisitor { template T *getDirectOrSimplifiedValue(Value *V) const { if (auto *Direct = dyn_cast(V)) return Direct; - return dyn_cast_if_present(SimplifiedValues.lookup(V)); + return getSimplifiedValue(V); } // Custom simplification helper routines. @@ -525,11 +526,33 @@ class CallAnalyzer : public InstVisitor { InlineResult analyze(); - std::optional getSimplifiedValue(Instruction *I) { - auto It = SimplifiedValues.find(I); - if (It != SimplifiedValues.end()) - return It->second; - return std::nullopt; + /// Lookup simplified Value. May return a value owned by the caller. + Value *getSimplifiedValueUnchecked(Value *V) const { + return SimplifiedValues.lookup(V); + } + + /// Lookup simplified Value, but return nullptr if the simplified value is + /// owned by the caller. + template T *getSimplifiedValue(Value *V) const { + Value *SimpleV = SimplifiedValues.lookup(V); + if (!SimpleV) + return nullptr; + + // Skip checks if we know T is a global. This has a small, but measurable + // impact on compile-time. + if constexpr (std::is_base_of_v) + return dyn_cast(SimpleV); + + // Make sure the simplified Value is owned by this function + if (auto *I = dyn_cast(SimpleV)) { + if (I->getFunction() != &F) + return nullptr; + } else if (auto *Arg = dyn_cast(SimpleV)) { + if (Arg->getParent() != &F) + return nullptr; + } else if (!isa(SimpleV)) + return nullptr; + return dyn_cast(SimpleV); } // Keep a bunch of stats about the cost savings found so we can print them @@ -921,12 +944,11 @@ class InlineCostCallAnalyzer final : public CallAnalyzer { if (BranchInst *BI = dyn_cast(&I)) { // Count a conditional branch as savings if it becomes unconditional. if (BI->isConditional() && - isa_and_nonnull( - SimplifiedValues.lookup(BI->getCondition()))) { + getSimplifiedValue(BI->getCondition())) { CurrentSavings += InstrCost; } } else if (SwitchInst *SI = dyn_cast(&I)) { - if (isa_and_present(SimplifiedValues.lookup(SI->getCondition()))) + if (getSimplifiedValue(SI->getCondition())) CurrentSavings += InstrCost; } else if (Value *V = dyn_cast(&I)) { // Count an instruction as savings if we can fold it. @@ -1423,10 +1445,17 @@ void InlineCostAnnotationWriter::emitInstructionAnnot( if (Record->hasThresholdChanged()) OS << ", threshold delta = " << Record->getThresholdDelta(); } - auto C = ICCA->getSimplifiedValue(const_cast(I)); - if (C) { + auto *V = ICCA->getSimplifiedValueUnchecked(const_cast(I)); + if (V) { OS << ", simplified to "; - (*C)->print(OS, true); + V->print(OS, true); + if (auto *VI = dyn_cast(V)) { + if (VI->getFunction() != I->getFunction()) + OS << " (caller instruction)"; + } else if (auto *VArg = dyn_cast(V)) { + if (VArg->getParent() != I->getFunction()) + OS << " (caller argument)"; + } } OS << "\n"; } @@ -1483,7 +1512,7 @@ bool CallAnalyzer::isGEPFree(GetElementPtrInst &GEP) { SmallVector Operands; Operands.push_back(GEP.getOperand(0)); for (const Use &Op : GEP.indices()) - if (Constant *SimpleOp = SimplifiedValues.lookup(Op)) + if (Constant *SimpleOp = getSimplifiedValue(Op)) Operands.push_back(SimpleOp); else Operands.push_back(Op); @@ -1498,7 +1527,7 @@ bool CallAnalyzer::visitAlloca(AllocaInst &I) { // Check whether inlining will turn a dynamic alloca into a static // alloca and handle that case. if (I.isArrayAllocation()) { - Constant *Size = SimplifiedValues.lookup(I.getArraySize()); + Constant *Size = getSimplifiedValue(I.getArraySize()); if (auto *AllocSize = dyn_cast_or_null(Size)) { // Sometimes a dynamic alloca could be converted into a static alloca // after this constant prop, and become a huge static alloca on an @@ -2388,7 +2417,7 @@ bool CallAnalyzer::visitCallBase(CallBase &Call) { // Check if this happens to be an indirect function call to a known function // in this inline context. If not, we've done all we can. Value *Callee = Call.getCalledOperand(); - F = dyn_cast_or_null(SimplifiedValues.lookup(Callee)); + F = getSimplifiedValue(Callee); if (!F || F->getFunctionType() != Call.getFunctionType()) { onCallArgumentSetup(Call); @@ -2483,8 +2512,7 @@ bool CallAnalyzer::visitSelectInst(SelectInst &SI) { Constant *TrueC = getDirectOrSimplifiedValue(TrueVal); Constant *FalseC = getDirectOrSimplifiedValue(FalseVal); - Constant *CondC = - dyn_cast_or_null(SimplifiedValues.lookup(SI.getCondition())); + Constant *CondC = getSimplifiedValue(SI.getCondition()); if (!CondC) { // Select C, X, X => X @@ -2833,8 +2861,9 @@ InlineResult CallAnalyzer::analyze() { auto CAI = CandidateCall.arg_begin(); for (Argument &FAI : F.args()) { assert(CAI != CandidateCall.arg_end()); - if (Constant *C = dyn_cast(CAI)) - SimplifiedValues[&FAI] = C; + SimplifiedValues[&FAI] = *CAI; + if (isa(*CAI)) + ++NumConstantArgs; Value *PtrArg = *CAI; if (ConstantInt *C = stripAndComputeInBoundsConstantOffsets(PtrArg)) { @@ -2849,7 +2878,6 @@ InlineResult CallAnalyzer::analyze() { } ++CAI; } - NumConstantArgs = SimplifiedValues.size(); NumConstantOffsetPtrArgs = ConstantOffsetPtrs.size(); NumAllocaArgs = SROAArgValues.size(); @@ -2911,8 +2939,7 @@ InlineResult CallAnalyzer::analyze() { if (BranchInst *BI = dyn_cast(TI)) { if (BI->isConditional()) { Value *Cond = BI->getCondition(); - if (ConstantInt *SimpleCond = - dyn_cast_or_null(SimplifiedValues.lookup(Cond))) { + if (ConstantInt *SimpleCond = getSimplifiedValue(Cond)) { BasicBlock *NextBB = BI->getSuccessor(SimpleCond->isZero() ? 1 : 0); BBWorklist.insert(NextBB); KnownSuccessors[BB] = NextBB; @@ -2922,8 +2949,7 @@ InlineResult CallAnalyzer::analyze() { } } else if (SwitchInst *SI = dyn_cast(TI)) { Value *Cond = SI->getCondition(); - if (ConstantInt *SimpleCond = - dyn_cast_or_null(SimplifiedValues.lookup(Cond))) { + if (ConstantInt *SimpleCond = getSimplifiedValue(Cond)) { BasicBlock *NextBB = SI->findCaseValue(SimpleCond)->getCaseSuccessor(); BBWorklist.insert(NextBB); KnownSuccessors[BB] = NextBB;