From 592a96c03b0c587404e78d69bbf072609b1e6417 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 3 Nov 2022 13:08:14 +0100 Subject: [PATCH] [SimplifyCFG] Extract code for tracking ephemeral values (NFC) To allow reusing this in more places in SimplifyCFG. --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 40 +++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index fcdd858..bf0eca5 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -2635,6 +2635,32 @@ static bool MergeCompatibleInvokes(BasicBlock *BB, DomTreeUpdater *DTU) { return Changed; } +namespace { +/// Track ephemeral values, which should be ignored for cost-modelling +/// purposes. Requires walking instructions in reverse order. +class EphemeralValueTracker { + SmallPtrSet EphValues; + + bool isEphemeral(const Instruction *I) { + if (isa(I)) + return true; + return !I->mayHaveSideEffects() && !I->isTerminator() && + all_of(I->users(), [&](const User *U) { + return EphValues.count(cast(U)); + }); + } + +public: + bool track(const Instruction *I) { + if (isEphemeral(I)) { + EphValues.insert(I); + return true; + } + return false; + } +}; +} // namespace + /// Determine if we can hoist sink a sole store instruction out of a /// conditional block. /// @@ -3002,15 +3028,7 @@ bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB, /// Return true if we can thread a branch across this block. static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) { int Size = 0; - - SmallPtrSet EphValues; - auto IsEphemeral = [&](const Instruction *I) { - if (isa(I)) - return true; - return !I->mayHaveSideEffects() && !I->isTerminator() && - all_of(I->users(), - [&](const User *U) { return EphValues.count(U); }); - }; + EphemeralValueTracker EphTracker; // Walk the loop in reverse so that we can identify ephemeral values properly // (values only feeding assumes). @@ -3021,11 +3039,9 @@ static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) { return false; // Ignore ephemeral values which are deleted during codegen. - if (IsEphemeral(&I)) - EphValues.insert(&I); // We will delete Phis while threading, so Phis should not be accounted in // block's size. - else if (!isa(I)) { + if (!EphTracker.track(&I) && !isa(I)) { if (Size++ > MaxSmallBlockSize) return false; // Don't clone large BB's. } -- 2.7.4