-
Notifications
You must be signed in to change notification settings - Fork 18.4k
[ConstraintElim] Add NUW flag to sub if x >=u y. #218685
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
Open
fhahn
wants to merge
2
commits into
llvm:main
Choose a base branch
from
fhahn:ce-strenghten-ce-flags-sub
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
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
208 changes: 208 additions & 0 deletions
208
llvm/test/Transforms/ConstraintElimination/materialize-flags-narrow-pointer-index.ll
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,208 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6 | ||
| ; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s | ||
|
|
||
| ; The offsets of a GEP are computed in the pointer index type. With a 16 bit | ||
| ; index type, a stride or struct member offset that does not fit in 15 bits is | ||
| ; negative there, so a non-negative index does not imply a non-negative offset | ||
| ; and nuw must not be added. | ||
|
|
||
| target datalayout = "p:16:16" | ||
|
|
||
| %S.big = type { [40000 x i8], i8 } | ||
| %S.small = type { i16, i8 } | ||
|
|
||
| ; The stride 40000 is negative as an i16, so the offset for a positive index is | ||
| ; negative and the GEP may wrap in the unsigned sense. | ||
| define ptr @gep_no_nuw_stride_negative_in_index_type(ptr %p, i16 %i, i16 %j) { | ||
| ; CHECK-LABEL: define ptr @gep_no_nuw_stride_negative_in_index_type( | ||
| ; CHECK-SAME: ptr [[P:%.*]], i16 [[I:%.*]], i16 [[J:%.*]]) { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] | ||
| ; CHECK-NEXT: [[C_0:%.*]] = icmp sgt i16 [[I]], [[J]] | ||
| ; CHECK-NEXT: br i1 [[C_0]], label %[[BB_1:.*]], label %[[EXIT:.*]] | ||
| ; CHECK: [[BB_1]]: | ||
| ; CHECK-NEXT: [[C_1:%.*]] = icmp sgt i16 [[J]], 0 | ||
| ; CHECK-NEXT: br i1 [[C_1]], label %[[BB_2:.*]], label %[[EXIT]] | ||
| ; CHECK: [[BB_2]]: | ||
| ; CHECK-NEXT: [[GEP:%.*]] = getelementptr nusw [40000 x i8], ptr [[P]], i16 [[I]] | ||
| ; CHECK-NEXT: ret ptr [[GEP]] | ||
| ; CHECK: [[EXIT]]: | ||
| ; CHECK-NEXT: ret ptr null | ||
| ; | ||
| entry: | ||
| %c.0 = icmp sgt i16 %i, %j | ||
| br i1 %c.0, label %bb.1, label %exit | ||
|
|
||
| bb.1: | ||
| %c.1 = icmp sgt i16 %j, 0 | ||
| br i1 %c.1, label %bb.2, label %exit | ||
|
|
||
| bb.2: | ||
| %gep = getelementptr nusw [40000 x i8], ptr %p, i16 %i | ||
| ret ptr %gep | ||
|
|
||
| exit: | ||
| ret ptr null | ||
| } | ||
|
|
||
| ; Same for the offset of a struct member, which is 40000 for the second field. | ||
| define ptr @gep_no_nuw_struct_offset_negative_in_index_type(ptr %p, i16 %i, i16 %j) { | ||
| ; CHECK-LABEL: define ptr @gep_no_nuw_struct_offset_negative_in_index_type( | ||
| ; CHECK-SAME: ptr [[P:%.*]], i16 [[I:%.*]], i16 [[J:%.*]]) { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] | ||
| ; CHECK-NEXT: [[C_0:%.*]] = icmp sgt i16 [[I]], [[J]] | ||
| ; CHECK-NEXT: br i1 [[C_0]], label %[[BB_1:.*]], label %[[EXIT:.*]] | ||
| ; CHECK: [[BB_1]]: | ||
| ; CHECK-NEXT: [[C_1:%.*]] = icmp sgt i16 [[J]], 0 | ||
| ; CHECK-NEXT: br i1 [[C_1]], label %[[BB_2:.*]], label %[[EXIT]] | ||
| ; CHECK: [[BB_2]]: | ||
| ; CHECK-NEXT: [[GEP:%.*]] = getelementptr nusw [[S_BIG:%.*]], ptr [[P]], i16 [[I]], i32 1 | ||
| ; CHECK-NEXT: ret ptr [[GEP]] | ||
| ; CHECK: [[EXIT]]: | ||
| ; CHECK-NEXT: ret ptr null | ||
| ; | ||
| entry: | ||
| %c.0 = icmp sgt i16 %i, %j | ||
| br i1 %c.0, label %bb.1, label %exit | ||
|
|
||
| bb.1: | ||
| %c.1 = icmp sgt i16 %j, 0 | ||
| br i1 %c.1, label %bb.2, label %exit | ||
|
|
||
| bb.2: | ||
| %gep = getelementptr nusw %S.big, ptr %p, i16 %i, i32 1 | ||
| ret ptr %gep | ||
|
|
||
| exit: | ||
| ret ptr null | ||
| } | ||
|
|
||
| ; The stride of the outer array is 160000, which is 28928 and thus non-negative | ||
| ; as an i16, but the stride of the inner array is negative. | ||
| define ptr @gep_no_nuw_inner_stride_negative_in_index_type(ptr %p, i16 %i, i16 %j) { | ||
| ; CHECK-LABEL: define ptr @gep_no_nuw_inner_stride_negative_in_index_type( | ||
| ; CHECK-SAME: ptr [[P:%.*]], i16 [[I:%.*]], i16 [[J:%.*]]) { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] | ||
| ; CHECK-NEXT: [[C_0:%.*]] = icmp sgt i16 [[I]], [[J]] | ||
| ; CHECK-NEXT: br i1 [[C_0]], label %[[BB_1:.*]], label %[[EXIT:.*]] | ||
| ; CHECK: [[BB_1]]: | ||
| ; CHECK-NEXT: [[C_1:%.*]] = icmp sgt i16 [[J]], 0 | ||
| ; CHECK-NEXT: br i1 [[C_1]], label %[[BB_2:.*]], label %[[EXIT]] | ||
| ; CHECK: [[BB_2]]: | ||
| ; CHECK-NEXT: [[GEP:%.*]] = getelementptr nusw [4 x [40000 x i8]], ptr [[P]], i16 [[I]], i16 [[J]] | ||
| ; CHECK-NEXT: ret ptr [[GEP]] | ||
| ; CHECK: [[EXIT]]: | ||
| ; CHECK-NEXT: ret ptr null | ||
| ; | ||
| entry: | ||
| %c.0 = icmp sgt i16 %i, %j | ||
| br i1 %c.0, label %bb.1, label %exit | ||
|
|
||
| bb.1: | ||
| %c.1 = icmp sgt i16 %j, 0 | ||
| br i1 %c.1, label %bb.2, label %exit | ||
|
|
||
| bb.2: | ||
| %gep = getelementptr nusw [4 x [40000 x i8]], ptr %p, i16 %i, i16 %j | ||
| ret ptr %gep | ||
|
|
||
| exit: | ||
| ret ptr null | ||
| } | ||
|
|
||
| ; The stride fits in the index type, so nuw can be added. | ||
| define ptr @gep_nuw_stride_fits_index_type(ptr %p, i16 %i, i16 %j) { | ||
| ; CHECK-LABEL: define ptr @gep_nuw_stride_fits_index_type( | ||
| ; CHECK-SAME: ptr [[P:%.*]], i16 [[I:%.*]], i16 [[J:%.*]]) { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] | ||
| ; CHECK-NEXT: [[C_0:%.*]] = icmp sgt i16 [[I]], [[J]] | ||
| ; CHECK-NEXT: br i1 [[C_0]], label %[[BB_1:.*]], label %[[EXIT:.*]] | ||
| ; CHECK: [[BB_1]]: | ||
| ; CHECK-NEXT: [[C_1:%.*]] = icmp sgt i16 [[J]], 0 | ||
| ; CHECK-NEXT: br i1 [[C_1]], label %[[BB_2:.*]], label %[[EXIT]] | ||
| ; CHECK: [[BB_2]]: | ||
| ; CHECK-NEXT: [[GEP:%.*]] = getelementptr nusw i16, ptr [[P]], i16 [[I]] | ||
| ; CHECK-NEXT: ret ptr [[GEP]] | ||
| ; CHECK: [[EXIT]]: | ||
| ; CHECK-NEXT: ret ptr null | ||
| ; | ||
| entry: | ||
| %c.0 = icmp sgt i16 %i, %j | ||
| br i1 %c.0, label %bb.1, label %exit | ||
|
|
||
| bb.1: | ||
| %c.1 = icmp sgt i16 %j, 0 | ||
| br i1 %c.1, label %bb.2, label %exit | ||
|
|
||
| bb.2: | ||
| %gep = getelementptr nusw i16, ptr %p, i16 %i | ||
| ret ptr %gep | ||
|
|
||
| exit: | ||
| ret ptr null | ||
| } | ||
|
|
||
| ; The struct member offset fits in the index type, so nuw can be added. | ||
| define ptr @gep_nuw_struct_offset_fits_index_type(ptr %p, i16 %i, i16 %j) { | ||
| ; CHECK-LABEL: define ptr @gep_nuw_struct_offset_fits_index_type( | ||
| ; CHECK-SAME: ptr [[P:%.*]], i16 [[I:%.*]], i16 [[J:%.*]]) { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] | ||
| ; CHECK-NEXT: [[C_0:%.*]] = icmp sgt i16 [[I]], [[J]] | ||
| ; CHECK-NEXT: br i1 [[C_0]], label %[[BB_1:.*]], label %[[EXIT:.*]] | ||
| ; CHECK: [[BB_1]]: | ||
| ; CHECK-NEXT: [[C_1:%.*]] = icmp sgt i16 [[J]], 0 | ||
| ; CHECK-NEXT: br i1 [[C_1]], label %[[BB_2:.*]], label %[[EXIT]] | ||
| ; CHECK: [[BB_2]]: | ||
| ; CHECK-NEXT: [[GEP:%.*]] = getelementptr nusw [[S_SMALL:%.*]], ptr [[P]], i16 [[I]], i32 1 | ||
| ; CHECK-NEXT: ret ptr [[GEP]] | ||
| ; CHECK: [[EXIT]]: | ||
| ; CHECK-NEXT: ret ptr null | ||
| ; | ||
| entry: | ||
| %c.0 = icmp sgt i16 %i, %j | ||
| br i1 %c.0, label %bb.1, label %exit | ||
|
|
||
| bb.1: | ||
| %c.1 = icmp sgt i16 %j, 0 | ||
| br i1 %c.1, label %bb.2, label %exit | ||
|
|
||
| bb.2: | ||
| %gep = getelementptr nusw %S.small, ptr %p, i16 %i, i32 1 | ||
| ret ptr %gep | ||
|
|
||
| exit: | ||
| ret ptr null | ||
| } | ||
|
|
||
| ; An index wider than the index type is truncated. nusw guarantees the | ||
| ; truncation preserves the signed value, which together with the index being | ||
| ; non-negative means it also preserves the unsigned value. | ||
| define ptr @gep_nuw_index_wider_than_index_type(ptr %p, i64 %i, i64 %j) { | ||
| ; CHECK-LABEL: define ptr @gep_nuw_index_wider_than_index_type( | ||
| ; CHECK-SAME: ptr [[P:%.*]], i64 [[I:%.*]], i64 [[J:%.*]]) { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] | ||
| ; CHECK-NEXT: [[C_0:%.*]] = icmp sgt i64 [[I]], [[J]] | ||
| ; CHECK-NEXT: br i1 [[C_0]], label %[[BB_1:.*]], label %[[EXIT:.*]] | ||
| ; CHECK: [[BB_1]]: | ||
| ; CHECK-NEXT: [[C_1:%.*]] = icmp sgt i64 [[J]], 0 | ||
| ; CHECK-NEXT: br i1 [[C_1]], label %[[BB_2:.*]], label %[[EXIT]] | ||
| ; CHECK: [[BB_2]]: | ||
| ; CHECK-NEXT: [[GEP:%.*]] = getelementptr nusw i16, ptr [[P]], i64 [[I]] | ||
| ; CHECK-NEXT: ret ptr [[GEP]] | ||
| ; CHECK: [[EXIT]]: | ||
| ; CHECK-NEXT: ret ptr null | ||
| ; | ||
| entry: | ||
| %c.0 = icmp sgt i64 %i, %j | ||
| br i1 %c.0, label %bb.1, label %exit | ||
|
|
||
| bb.1: | ||
| %c.1 = icmp sgt i64 %j, 0 | ||
| br i1 %c.1, label %bb.2, label %exit | ||
|
|
||
| bb.2: | ||
| %gep = getelementptr nusw i16, ptr %p, i64 %i | ||
| ret ptr %gep | ||
|
|
||
| exit: | ||
| ret ptr null | ||
| } |
Oops, something went wrong.
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.
For sub specifically, a constant on the RHS will get canonicalized to add anyway.