-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[DropUnnecessaryAssumes] Add pass for dropping assumes #159403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
llvm/include/llvm/Transforms/Scalar/DropUnnecessaryAssumes.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===------------------------------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Pass that drops assumes that are unlikely to be useful. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TRANSFORMS_SCALAR_DROPUNNECESSARYASSUMES_H | ||
#define LLVM_TRANSFORMS_SCALAR_DROPUNNECESSARYASSUMES_H | ||
|
||
#include "llvm/IR/PassManager.h" | ||
|
||
namespace llvm { | ||
|
||
struct DropUnnecessaryAssumesPass | ||
: public PassInfoMixin<DropUnnecessaryAssumesPass> { | ||
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); | ||
}; | ||
|
||
} // end namespace llvm | ||
|
||
#endif // LLVM_TRANSFORMS_SCALAR_DROPUNNECESSARYASSUMES_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//===------------------------------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Transforms/Scalar/DropUnnecessaryAssumes.h" | ||
#include "llvm/Analysis/AssumptionCache.h" | ||
#include "llvm/Analysis/ValueTracking.h" | ||
#include "llvm/IR/IntrinsicInst.h" | ||
#include "llvm/IR/PatternMatch.h" | ||
#include "llvm/Transforms/Utils/Local.h" | ||
|
||
using namespace llvm; | ||
using namespace llvm::PatternMatch; | ||
|
||
PreservedAnalyses | ||
DropUnnecessaryAssumesPass::run(Function &F, FunctionAnalysisManager &FAM) { | ||
AssumptionCache &AC = FAM.getResult<AssumptionAnalysis>(F); | ||
bool Changed = false; | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (AssumptionCache::ResultElem &Elem : AC.assumptions()) { | ||
auto *Assume = cast_or_null<AssumeInst>(Elem.Assume); | ||
if (!Assume) | ||
continue; | ||
|
||
// TODO: Handle assumes with operand bundles. | ||
if (Assume->hasOperandBundles()) | ||
continue; | ||
|
||
Value *Cond = Assume->getArgOperand(0); | ||
// Don't drop type tests, which have special semantics. | ||
if (match(Cond, m_Intrinsic<Intrinsic::type_test>())) | ||
continue; | ||
|
||
SmallPtrSet<Value *, 8> Affected; | ||
findValuesAffectedByCondition(Cond, /*IsAssume=*/true, | ||
[&](Value *A) { Affected.insert(A); }); | ||
|
||
// 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. | ||
if (!all_of(Affected, match_fn(m_OneUse(m_Value())))) | ||
continue; | ||
|
||
Assume->eraseFromParent(); | ||
RecursivelyDeleteTriviallyDeadInstructions(Cond); | ||
Changed = true; | ||
} | ||
|
||
if (Changed) { | ||
PreservedAnalyses PA; | ||
PA.preserveSet<CFGAnalyses>(); | ||
return PA; | ||
} | ||
return PreservedAnalyses::all(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6 | ||
; RUN: opt -S -passes=drop-unnecessary-assumes < %s | FileCheck %s | ||
|
||
define void @basic_dead(i32 %x) { | ||
; CHECK-LABEL: define void @basic_dead( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: ret void | ||
; | ||
%cond = icmp sge i32 %x, 0 | ||
call void @llvm.assume(i1 %cond) | ||
ret void | ||
} | ||
|
||
define i32 @basic_live(i32 %x) { | ||
; CHECK-LABEL: define i32 @basic_live( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[COND:%.*]] = icmp sge i32 [[X]], 0 | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[COND]]) | ||
; CHECK-NEXT: ret i32 [[X]] | ||
; | ||
%cond = icmp sge i32 %x, 0 | ||
call void @llvm.assume(i1 %cond) | ||
ret i32 %x | ||
} | ||
|
||
; Affected value is not direct operand of the condition. | ||
define i32 @complex_live(i32 %x) { | ||
; CHECK-LABEL: define i32 @complex_live( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[AND:%.*]] = and i32 [[X]], 1 | ||
; CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[AND]], 0 | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[COND]]) | ||
; CHECK-NEXT: ret i32 [[X]] | ||
; | ||
%and = and i32 %x, 1 | ||
%cond = icmp ne i32 %and, 0 | ||
call void @llvm.assume(i1 %cond) | ||
ret i32 %x | ||
} | ||
|
||
; There are multiple affected values, and not all are one-use. | ||
define i32 @multiple_live1(i32 %x, i32 %y) { | ||
; CHECK-LABEL: define i32 @multiple_live1( | ||
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[X]], [[Y]] | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[COND]]) | ||
; CHECK-NEXT: ret i32 [[X]] | ||
; | ||
%cond = icmp eq i32 %x, %y | ||
call void @llvm.assume(i1 %cond) | ||
ret i32 %x | ||
} | ||
|
||
define i32 @multiple_live2(i32 %x, i32 %y) { | ||
; CHECK-LABEL: define i32 @multiple_live2( | ||
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[X]], [[Y]] | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[COND]]) | ||
; CHECK-NEXT: ret i32 [[Y]] | ||
; | ||
%cond = icmp eq i32 %x, %y | ||
call void @llvm.assume(i1 %cond) | ||
ret i32 %y | ||
} | ||
|
||
define void @operand_bundle_dead(ptr %x) { | ||
; CHECK-LABEL: define void @operand_bundle_dead( | ||
; CHECK-SAME: ptr [[X:%.*]]) { | ||
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[X]], i64 8) ] | ||
; CHECK-NEXT: ret void | ||
; | ||
call void @llvm.assume(i1 true) ["align"(ptr %x, i64 8)] | ||
ret void | ||
} | ||
|
||
define ptr @operand_bundle_live(ptr %x) { | ||
; CHECK-LABEL: define ptr @operand_bundle_live( | ||
; CHECK-SAME: ptr [[X:%.*]]) { | ||
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[X]], i64 8) ] | ||
; CHECK-NEXT: ret ptr [[X]] | ||
; | ||
call void @llvm.assume(i1 true) ["align"(ptr %x, i64 8)] | ||
ret ptr %x | ||
} | ||
|
||
define void @type_test(ptr %x) { | ||
; CHECK-LABEL: define void @type_test( | ||
; CHECK-SAME: ptr [[X:%.*]]) { | ||
; CHECK-NEXT: [[TEST:%.*]] = call i1 @llvm.type.test(ptr [[X]], metadata !"typeid") | ||
; CHECK-NEXT: call void @llvm.assume(i1 [[TEST]]) | ||
; CHECK-NEXT: ret void | ||
; | ||
%test = call i1 @llvm.type.test(ptr %x, metadata !"typeid") | ||
call void @llvm.assume(i1 %test) | ||
ret void | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It deserves a comment here to explain why it only runs at post-inline and post-link.