-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[ConstantFolding] Avoid use of isNonIntegralPointerType() #159959
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
arichardson
merged 8 commits into
main
from
users/arichardson/spr/constantfolding-avoid-use-of-isnonintegralpointertype
Sep 23, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
acd5695
[𝘀𝗽𝗿] changes to main this commit is based on
arichardson cccf6db
[𝘀𝗽𝗿] initial version
arichardson e866587
[𝘀𝗽𝗿] changes introduced through rebase
arichardson 7005664
fix unused variable
arichardson 690f308
fix typo in comment
arichardson d18eb5e
spelling
arichardson 6dd1ea4
[𝘀𝗽𝗿] changes introduced through rebase
arichardson f705aa7
add vector test cases
arichardson 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
16 changes: 0 additions & 16 deletions
16
llvm/test/Transforms/InstSimplify/ConstProp/inttoptr-gep-index-width.ll
This file was deleted.
Oops, something went wrong.
145 changes: 145 additions & 0 deletions
145
llvm/test/Transforms/InstSimplify/ConstProp/inttoptr-gep-nonintegral.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,145 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
| ; RUN: opt -S -passes=instsimplify < %s | FileCheck %s | ||
| ;; Check that we do not create new inttoptr instructions for unstable pointers | ||
| ;; or pointers with external state (even if the values are all constants). | ||
| ;; NOTE: for all but the zero address space, the GEP should only modify the | ||
| ;; low 8 bits of the pointer. | ||
| target datalayout = "p:16:16:16:16-p1:16:16:16:8-pu2:16:16:16:8-pe3:16:16:16:8" | ||
|
|
||
| define ptr @test_null_base_normal() { | ||
| ; CHECK-LABEL: define ptr @test_null_base_normal() { | ||
| ; CHECK-NEXT: ret ptr inttoptr (i16 -2 to ptr) | ||
| ; | ||
| %gep = getelementptr i8, ptr null, i8 -2 | ||
| ret ptr %gep | ||
| } | ||
| define ptr @test_inttoptr_base_normal() { | ||
| ; CHECK-LABEL: define ptr @test_inttoptr_base_normal() { | ||
| ; CHECK-NEXT: ret ptr null | ||
| ; | ||
| %base = inttoptr i16 -1 to ptr | ||
| %gep = getelementptr i8, ptr %base, i8 1 | ||
| ret ptr %gep | ||
| } | ||
|
|
||
| ;; Transformation is fine for non-integral address space, but we can only change | ||
| ;; the index bits: (i8 -2 == i16 254) | ||
| define ptr addrspace(1) @test_null_base_nonintegral() { | ||
| ; CHECK-LABEL: define ptr addrspace(1) @test_null_base_nonintegral() { | ||
| ; CHECK-NEXT: ret ptr addrspace(1) inttoptr (i16 254 to ptr addrspace(1)) | ||
| ; | ||
| %gep = getelementptr i8, ptr addrspace(1) null, i8 -2 | ||
| ret ptr addrspace(1) %gep | ||
| } | ||
| define ptr addrspace(1) @test_inttoptr_base_nonintegral() { | ||
| ; CHECK-LABEL: define ptr addrspace(1) @test_inttoptr_base_nonintegral() { | ||
| ; CHECK-NEXT: ret ptr addrspace(1) inttoptr (i16 -256 to ptr addrspace(1)) | ||
| ; | ||
| %base = inttoptr i16 -1 to ptr addrspace(1) | ||
| %gep = getelementptr i8, ptr addrspace(1) %base, i8 1 | ||
| ret ptr addrspace(1) %gep | ||
| } | ||
|
|
||
| ;; For unstable pointers we should avoid any introduction of inttoptr | ||
| define ptr addrspace(2) @test_null_base_unstable() { | ||
| ; CHECK-LABEL: define ptr addrspace(2) @test_null_base_unstable() { | ||
| ; CHECK-NEXT: ret ptr addrspace(2) getelementptr (i8, ptr addrspace(2) null, i8 -2) | ||
| ; | ||
| %gep = getelementptr i8, ptr addrspace(2) null, i8 -2 | ||
| ret ptr addrspace(2) %gep | ||
| } | ||
| define ptr addrspace(2) @test_inttoptr_base_unstable() { | ||
| ; CHECK-LABEL: define ptr addrspace(2) @test_inttoptr_base_unstable() { | ||
| ; CHECK-NEXT: ret ptr addrspace(2) getelementptr (i8, ptr addrspace(2) inttoptr (i16 -1 to ptr addrspace(2)), i8 1) | ||
| ; | ||
| %base = inttoptr i16 -1 to ptr addrspace(2) | ||
| %gep = getelementptr i8, ptr addrspace(2) %base, i8 1 | ||
| ret ptr addrspace(2) %gep | ||
| } | ||
|
|
||
| ;; The same is true for pointers with external state: no new inttoptr | ||
| define ptr addrspace(3) @test_null_base_external() { | ||
| ; CHECK-LABEL: define ptr addrspace(3) @test_null_base_external() { | ||
| ; CHECK-NEXT: ret ptr addrspace(3) getelementptr (i8, ptr addrspace(3) null, i8 -2) | ||
| ; | ||
| %gep = getelementptr i8, ptr addrspace(3) null, i8 -2 | ||
| ret ptr addrspace(3) %gep | ||
| } | ||
|
|
||
| define ptr addrspace(3) @test_inttoptr_base_external() { | ||
| ; CHECK-LABEL: define ptr addrspace(3) @test_inttoptr_base_external() { | ||
| ; CHECK-NEXT: ret ptr addrspace(3) getelementptr (i8, ptr addrspace(3) inttoptr (i16 -1 to ptr addrspace(3)), i8 1) | ||
| ; | ||
| %base = inttoptr i16 -1 to ptr addrspace(3) | ||
| %gep = getelementptr i8, ptr addrspace(3) %base, i8 1 | ||
| ret ptr addrspace(3) %gep | ||
| } | ||
arichardson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| define <2 x ptr> @test_vec_null_base_normal() { | ||
| ; CHECK-LABEL: define <2 x ptr> @test_vec_null_base_normal() { | ||
| ; CHECK-NEXT: ret <2 x ptr> getelementptr (i8, <2 x ptr> zeroinitializer, <2 x i16> <i16 -2, i16 -3>) | ||
| ; | ||
| %gep = getelementptr i8, <2 x ptr> <ptr null, ptr null>, <2 x i8> <i8 -2, i8 -3> | ||
| ret <2 x ptr> %gep | ||
| } | ||
| define <2 x ptr> @test_vec_inttoptr_base_normal() { | ||
| ; CHECK-LABEL: define <2 x ptr> @test_vec_inttoptr_base_normal() { | ||
| ; CHECK-NEXT: ret <2 x ptr> getelementptr (i8, <2 x ptr> <ptr inttoptr (i16 -1 to ptr), ptr inttoptr (i16 -2 to ptr)>, <2 x i16> <i16 1, i16 2>) | ||
| ; | ||
| %base = inttoptr <2 x i16> <i16 -1, i16 -2> to <2 x ptr> | ||
| %gep = getelementptr i8, <2 x ptr> %base, <2 x i8> <i8 1, i8 2> | ||
| ret <2 x ptr> %gep | ||
| } | ||
|
|
||
| ;; Transformation is fine for non-integral address space, but we can only change | ||
| ;; the index bits: (i8 -2 == i16 254) | ||
| define <2 x ptr addrspace(1)> @test_vec_null_base_nonintegral() { | ||
| ; CHECK-LABEL: define <2 x ptr addrspace(1)> @test_vec_null_base_nonintegral() { | ||
| ; CHECK-NEXT: ret <2 x ptr addrspace(1)> getelementptr (i8, <2 x ptr addrspace(1)> zeroinitializer, <2 x i8> <i8 -2, i8 -3>) | ||
| ; | ||
| %gep = getelementptr i8, <2 x ptr addrspace(1)> <ptr addrspace(1) null, ptr addrspace(1) null>, <2 x i8> <i8 -2, i8 -3> | ||
| ret <2 x ptr addrspace(1)> %gep | ||
| } | ||
| define <2 x ptr addrspace(1)> @test_vec_inttoptr_base_nonintegral() { | ||
| ; CHECK-LABEL: define <2 x ptr addrspace(1)> @test_vec_inttoptr_base_nonintegral() { | ||
| ; CHECK-NEXT: ret <2 x ptr addrspace(1)> getelementptr (i8, <2 x ptr addrspace(1)> <ptr addrspace(1) inttoptr (i16 -1 to ptr addrspace(1)), ptr addrspace(1) inttoptr (i16 -2 to ptr addrspace(1))>, <2 x i8> <i8 1, i8 2>) | ||
| ; | ||
| %base = inttoptr <2 x i16> <i16 -1, i16 -2> to <2 x ptr addrspace(1)> | ||
| %gep = getelementptr i8, <2 x ptr addrspace(1)> %base, <2 x i8> <i8 1, i8 2> | ||
| ret <2 x ptr addrspace(1)> %gep | ||
| } | ||
|
|
||
| ;; For unstable pointers we should avoid any introduction of inttoptr | ||
| define <2 x ptr addrspace(2)> @test_vec_null_base_unstable() { | ||
| ; CHECK-LABEL: define <2 x ptr addrspace(2)> @test_vec_null_base_unstable() { | ||
| ; CHECK-NEXT: ret <2 x ptr addrspace(2)> getelementptr (i8, <2 x ptr addrspace(2)> zeroinitializer, <2 x i8> <i8 -2, i8 -3>) | ||
| ; | ||
| %gep = getelementptr i8, <2 x ptr addrspace(2)> <ptr addrspace(2) null, ptr addrspace(2) null>, <2 x i8> <i8 -2, i8 -3> | ||
| ret <2 x ptr addrspace(2)> %gep | ||
| } | ||
| define <2 x ptr addrspace(2)> @test_vec_inttoptr_base_unstable() { | ||
| ; CHECK-LABEL: define <2 x ptr addrspace(2)> @test_vec_inttoptr_base_unstable() { | ||
| ; CHECK-NEXT: ret <2 x ptr addrspace(2)> getelementptr (i8, <2 x ptr addrspace(2)> <ptr addrspace(2) inttoptr (i16 -1 to ptr addrspace(2)), ptr addrspace(2) inttoptr (i16 -2 to ptr addrspace(2))>, <2 x i8> <i8 1, i8 2>) | ||
| ; | ||
| %base = inttoptr <2 x i16> <i16 -1, i16 -2> to <2 x ptr addrspace(2)> | ||
| %gep = getelementptr i8, <2 x ptr addrspace(2)> %base, <2 x i8> <i8 1, i8 2> | ||
| ret <2 x ptr addrspace(2)> %gep | ||
| } | ||
|
|
||
| ;; The same is true for pointers with external state: no new inttoptr | ||
| define <2 x ptr addrspace(3)> @test_vec_null_base_external() { | ||
| ; CHECK-LABEL: define <2 x ptr addrspace(3)> @test_vec_null_base_external() { | ||
| ; CHECK-NEXT: ret <2 x ptr addrspace(3)> getelementptr (i8, <2 x ptr addrspace(3)> zeroinitializer, <2 x i8> <i8 -2, i8 -3>) | ||
| ; | ||
| %gep = getelementptr i8, <2 x ptr addrspace(3)> <ptr addrspace(3) null, ptr addrspace(3) null>, <2 x i8> <i8 -2, i8 -3> | ||
| ret <2 x ptr addrspace(3)> %gep | ||
| } | ||
|
|
||
| define <2 x ptr addrspace(3)> @test_vec_inttoptr_base_external() { | ||
| ; CHECK-LABEL: define <2 x ptr addrspace(3)> @test_vec_inttoptr_base_external() { | ||
| ; CHECK-NEXT: ret <2 x ptr addrspace(3)> getelementptr (i8, <2 x ptr addrspace(3)> <ptr addrspace(3) inttoptr (i16 -1 to ptr addrspace(3)), ptr addrspace(3) inttoptr (i16 -2 to ptr addrspace(3))>, <2 x i8> <i8 1, i8 2>) | ||
| ; | ||
| %base = inttoptr <2 x i16> <i16 -1, i16 -2> to <2 x ptr addrspace(3)> | ||
| %gep = getelementptr i8, <2 x ptr addrspace(3)> %base, <2 x i8> <i8 1, i8 2> | ||
| ret <2 x ptr addrspace(3)> %gep | ||
| } | ||
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'm not sure this really matters for CHERI, we could also emit the
inttoptr (i16 -256 to ptr addrspace(1))directly, but maybe it's cleaner to not attempt to perform any inttoptr based transforms?