[llvm] [DropUnnecessaryAssumes] Make the ephemeral value check more precise (PR #160700)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 26 03:11:03 PDT 2025
================
@@ -17,13 +18,44 @@ using namespace llvm;
using namespace llvm::PatternMatch;
static bool affectedValuesAreEphemeral(ArrayRef<Value *> Affected) {
- // If all the affected uses have only one use (part of the assume), then
- // the assume does not provide useful information. Note that additional
- // users may appear as a result of inlining and CSE, so we should only
- // make this assumption late in the optimization pipeline.
- // TODO: Handle dead cyclic usages.
- // TODO: Handle multiple dead assumes on the same value.
- return all_of(Affected, match_fn(m_OneUse(m_Value())));
+ // Check whether all the uses are ephemeral, i.e. recursively only used
+ // by assumes. In that case, the assume does not provide useful information.
+ // Note that additional users may appear as a result of inlining and CSE,
+ // so we should only make this assumption late in the optimization pipeline.
+ SmallSetVector<User *, 16> Worklist;
+ auto AddUser = [&](User *U) {
+ // Bail out if we need to inspect too many users.
+ if (Worklist.size() >= 32)
+ return false;
+ Worklist.insert(U);
+ return true;
+ };
+
+ for (Value *V : Affected)
+ for (User *U : V->users())
+ if (!AddUser(U))
+ return false;
----------------
nikic wrote:
I realized that I didn't actually have a test for this, so added in affected_value_has_side_effect. And to answer your other question:
> Hm, what if the Affected value isn't an Instruction?
I thought about this a bit, and decided to explicitly limit this code to only handling Instructions/Arguments. For GlobalVariable, doing a use walk is iffy because it includes uses from other functions. We could add checks that the uses are in the right function, but I'm not sure this is worthwhile. For now I decided to just not handle these and added some tests for it.
This also means that we are guaranteed that all users are Instructions, so we can use that as the worklist type without problem.
https://github.com/llvm/llvm-project/pull/160700
More information about the llvm-commits
mailing list