aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Scalar/DropUnnecessaryAssumes.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Transforms/Scalar/DropUnnecessaryAssumes.cpp')
-rw-r--r--llvm/lib/Transforms/Scalar/DropUnnecessaryAssumes.cpp50
1 files changed, 43 insertions, 7 deletions
diff --git a/llvm/lib/Transforms/Scalar/DropUnnecessaryAssumes.cpp b/llvm/lib/Transforms/Scalar/DropUnnecessaryAssumes.cpp
index c215228..89980d5 100644
--- a/llvm/lib/Transforms/Scalar/DropUnnecessaryAssumes.cpp
+++ b/llvm/lib/Transforms/Scalar/DropUnnecessaryAssumes.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Scalar/DropUnnecessaryAssumes.h"
+#include "llvm/ADT/SetVector.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/IntrinsicInst.h"
@@ -17,13 +18,48 @@ 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<Instruction *, 32> Worklist;
+ auto AddUsers = [&](Value *V) {
+ for (User *U : V->users()) {
+ // Bail out if we need to inspect too many users.
+ if (Worklist.size() >= 32)
+ return false;
+ Worklist.insert(cast<Instruction>(U));
+ }
+ return true;
+ };
+
+ for (Value *V : Affected) {
+ // Do not handle assumes on globals for now. The use list for them may
+ // contain uses in other functions.
+ if (!isa<Instruction, Argument>(V))
+ return false;
+
+ if (!AddUsers(V))
+ return false;
+ }
+
+ for (unsigned Idx = 0; Idx < Worklist.size(); ++Idx) {
+ Instruction *I = Worklist[Idx];
+
+ // Use in assume is ephemeral.
+ if (isa<AssumeInst>(I))
+ continue;
+
+ // Use in side-effecting instruction is non-ephemeral.
+ if (I->mayHaveSideEffects() || I->isTerminator())
+ return false;
+
+ // Otherwise, recursively look at the users.
+ if (!AddUsers(I))
+ return false;
+ }
+
+ return true;
}
PreservedAnalyses