-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[BasicAA] Use nuw attribute of GEPs #98608
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
7 commits
Select commit
Hold shift + click to select a range
ac8d810
[BasicAA] Use nuw attribute of GEPs
hazzlim e83875b
Reorder checks (Addressing review comment)
hazzlim 452c8de
Drop nuw flag in some cases when subtracting GEPs
hazzlim 81ce23d
Swap order of GEPs to simplify things
hazzlim bbaf121
Address review comments
hazzlim cf861f3
Initialize DecomposedGEP.NWFlags to GEPNoWrapFlags::all()
hazzlim d171546
Address final review comments
hazzlim 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
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,214 @@ | ||
; RUN: opt < %s -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s | ||
|
||
; CHECK-LABEL: test_no_lower_bound | ||
; | ||
; CHECK-DAG: MayAlias: i32* %a, i32* %b | ||
define void @test_no_lower_bound(ptr %p, i64 %i) { | ||
%a = getelementptr i8, ptr %p, i64 4 | ||
%b = getelementptr nuw i8, ptr %p, i64 %i | ||
|
||
load i32, ptr %a | ||
load i32, ptr %b | ||
|
||
ret void | ||
} | ||
|
||
; CHECK-LABEL: test_lower_bound_lt_size | ||
; | ||
; CHECK-DAG: MayAlias: i32* %a, i32* %b | ||
define void @test_lower_bound_lt_size(ptr %p, i64 %i) { | ||
%a = getelementptr i8, ptr %p | ||
%add = getelementptr nuw i8, ptr %p, i64 2 | ||
%b = getelementptr nuw i8, ptr %add, i64 %i | ||
|
||
load i32, ptr %a | ||
load i32, ptr %b | ||
|
||
ret void | ||
} | ||
|
||
; CHECK-LABEL: test_lower_bound_ge_size | ||
; | ||
; CHECK-DAG: NoAlias: i32* %a, i32* %b | ||
define void @test_lower_bound_ge_size(ptr %p, i64 %i) { | ||
%a = getelementptr i8, ptr %p | ||
%add = getelementptr nuw i8, ptr %p, i64 4 | ||
%b = getelementptr nuw i8, ptr %add, i64 %i | ||
|
||
load i32, ptr %a | ||
load i32, ptr %b | ||
|
||
ret void | ||
} | ||
|
||
; CHECK-LABEL: test_not_all_nuw | ||
; | ||
; If part of the addressing is done with non-nuw GEPs, we can't use properties | ||
; implied by the last GEP with the whole offset. In this case, the calculation | ||
; of %add (%p + 4) could wrap the pointer index type, such that %add +<nuw> %i | ||
; could still alias with %p. | ||
; | ||
; CHECK-DAG: MayAlias: i32* %a, i32* %b | ||
define void @test_not_all_nuw(ptr %p, i64 %i) { | ||
%a = getelementptr i8, ptr %p | ||
%add = getelementptr i8, ptr %p, i64 4 | ||
%b = getelementptr nuw i8, ptr %add, i64 %i | ||
|
||
load i32, ptr %a | ||
load i32, ptr %b | ||
|
||
ret void | ||
} | ||
|
||
; CHECK-LABEL: test_multi_step_not_all_nuw | ||
; | ||
; CHECK-DAG: MayAlias: i32* %a, i32* %b | ||
define void @test_multi_step_not_all_nuw(ptr %p, i64 %i, i64 %j, i64 %k) { | ||
%a = getelementptr i8, ptr %p | ||
%add = getelementptr i8, ptr %p, i64 4 | ||
%step1 = getelementptr i8, ptr %add, i64 %i | ||
%step2 = getelementptr i8, ptr %step1, i64 %j | ||
%b = getelementptr nuw i8, ptr %step2, i64 %k | ||
|
||
load i32, ptr %a | ||
load i32, ptr %b | ||
|
||
ret void | ||
} | ||
|
||
; CHECK-LABEL: test_multi_step_all_nuw | ||
; | ||
; CHECK-DAG: NoAlias: i32* %a, i32* %b | ||
define void @test_multi_step_all_nuw(ptr %p, i64 %i, i64 %j, i64 %k) { | ||
%a = getelementptr i8, ptr %p | ||
%add = getelementptr nuw i8, ptr %p, i64 4 | ||
%step1 = getelementptr nuw i8, ptr %add, i64 %i | ||
%step2 = getelementptr nuw i8, ptr %step1, i64 %j | ||
%b = getelementptr nuw i8, ptr %step2, i64 %k | ||
|
||
load i32, ptr %a | ||
load i32, ptr %b | ||
|
||
ret void | ||
} | ||
|
||
%struct = type { i64, [2 x i32], i64 } | ||
|
||
; CHECK-LABEL: test_struct_no_nuw | ||
; | ||
; The array access may alias with the struct elements before and after, because | ||
; we cannot prove that (%arr + %i) does not alias with the base pointer %p. | ||
; | ||
; CHECK-DAG: MayAlias: i32* %arrayidx, i64* %st | ||
; CHECK-DAG: NoAlias: i64* %after, i64* %st | ||
; CHECK-DAG: MayAlias: i64* %after, i32* %arrayidx | ||
|
||
define void @test_struct_no_nuw(ptr %st, i64 %i) { | ||
%arr = getelementptr i8, ptr %st, i64 8 | ||
%arrayidx = getelementptr [2 x i32], ptr %arr, i64 0, i64 %i | ||
%after = getelementptr i8, ptr %st, i64 16 | ||
|
||
load i64, ptr %st | ||
load i32, ptr %arrayidx | ||
load i64, ptr %after | ||
|
||
ret void | ||
} | ||
|
||
; CHECK-LABEL: test_struct_nuw | ||
; | ||
; We can prove that the array access does not alias with struct element before, | ||
; because we can prove that (%arr +<nuw> %i) does not wrap the pointer index | ||
; type (add nuw). The array access may still alias with the struct element | ||
; after, as the add nuw property does not preclude this. | ||
; | ||
; CHECK-DAG: NoAlias: i32* %arrayidx, i64* %st | ||
; CHECK-DAG: NoAlias: i64* %after, i64* %st | ||
; CHECK-DAG: MayAlias: i64* %after, i32* %arrayidx | ||
|
||
define void @test_struct_nuw(ptr %st, i64 %i) { | ||
%arr = getelementptr nuw i8, ptr %st, i64 8 | ||
%arrayidx = getelementptr nuw [2 x i32], ptr %arr, i64 0, i64 %i | ||
%after = getelementptr nuw i8, ptr %st, i64 16 | ||
|
||
load i64, ptr %st | ||
load i32, ptr %arrayidx | ||
load i64, ptr %after | ||
|
||
ret void | ||
} | ||
|
||
; CHECK-LABEL: constant_offset_overflow | ||
; | ||
; If subtraction of constant offsets could overflow in an unsigned sense, we | ||
; cannot prove the lower bound between the GEPs and so they may still alias. | ||
; | ||
; CHECK-DAG: MayAlias: i32* %a, i32* %b | ||
|
||
define void @constant_offset_overflow(ptr %p, i64 %i) { | ||
%a = getelementptr i8, ptr %p, i64 -8 | ||
%add = getelementptr nuw i8, ptr %p, i64 4 | ||
%b = getelementptr nuw i8, ptr %add, i64 %i | ||
|
||
load i32, ptr %a | ||
load i32, ptr %b | ||
|
||
ret void | ||
} | ||
|
||
; CHECK-LABEL: equal_var_idx_noalias | ||
; | ||
; If GEPs have equal variable indices, we can prove NoAlias when the Scale of | ||
; the RHS GEP is greater, as in this scenario the constant lower bound holds. | ||
; | ||
; CHECK-DAG: NoAlias: i32* %a, i32* %b | ||
|
||
define void @equal_var_idx_noalias(ptr %p, i64 %i) { | ||
%a = getelementptr i8, ptr %p, i64 %i | ||
|
||
%add = getelementptr nuw i8, ptr %p, i64 4 | ||
%b = getelementptr nuw i16, ptr %add, i64 %i | ||
|
||
load i32, ptr %a | ||
load i32, ptr %b | ||
|
||
ret void | ||
} | ||
|
||
; CHECK-LABEL: equal_var_idx_alias | ||
; | ||
; If GEPs have equal variable indices, we cannot prove NoAlias when the Scale of | ||
; the RHS GEP is ult Scale of the LHS GEP. | ||
; | ||
; CHECK-DAG: MayAlias: i32* %a, i32* %b | ||
|
||
define void @equal_var_idx_alias(ptr %p, i64 %i) { | ||
%a = getelementptr i32, ptr %p, i64 %i | ||
|
||
%add = getelementptr nuw i8, ptr %p, i64 4 | ||
%b = getelementptr nuw i16, ptr %add, i64 %i | ||
|
||
load i32, ptr %b | ||
load i32, ptr %a | ||
|
||
ret void | ||
} | ||
|
||
; CHECK-LABEL: both_var_idx | ||
; | ||
; If the RHS GEP has unmatched variable indices, we cannot prove a constant | ||
; lower bound between GEPs. | ||
; | ||
; CHECK-DAG: MayAlias: i32* %a, i32* %b | ||
|
||
define void @both_var_idx(ptr %p, i64 %i, i64 %j) { | ||
%a = getelementptr i8, ptr %p, i64 %i | ||
|
||
%add = getelementptr nuw i8, ptr %p, i64 4 | ||
%b = getelementptr nuw i8, ptr %add, i64 %j | ||
|
||
load i32, ptr %a | ||
load i32, ptr %b | ||
|
||
ret void | ||
} |
Oops, something went wrong.
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.
I'd move these before Base as well, to match
getelementptr
syntax.