-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[ConstantFold] Fold inttoptr, ptrtoaddr to bitcast #161087
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
Conversation
@llvm/pr-subscribers-llvm-ir @llvm/pr-subscribers-llvm-transforms Author: Hongyu Chen (XChy) ChangesFixes #157334. Full diff: https://github.com/llvm/llvm-project/pull/161087.diff 2 Files Affected:
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index daebf447a2107..fb47768974f2b 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -2847,6 +2847,7 @@ unsigned CastInst::isEliminableCastPair(
// FPTRUNC > FloatPt n/a FloatPt n/a
// FPEXT < FloatPt n/a FloatPt n/a
// PTRTOINT n/a Pointer n/a Integral Unsigned
+ // PTRTOADDR n/a Pointer n/a Integral Unsigned
// INTTOPTR n/a Integral Unsigned Pointer n/a
// BITCAST = FirstClass n/a FirstClass n/a
// ADDRSPCST n/a Pointer n/a Pointer n/a
@@ -2878,7 +2879,7 @@ unsigned CastInst::isEliminableCastPair(
{ 99,99,99, 2, 2,99,99, 8, 2,99,99,99, 4, 0}, // FPExt |
{ 1, 0, 0,99,99, 0, 0,99,99,99,99, 7, 3, 0}, // PtrToInt |
{ 1, 0, 0,99,99, 0, 0,99,99,99,99, 0, 3, 0}, // PtrToAddr |
- { 99,99,99,99,99,99,99,99,99,11,99,99,15, 0}, // IntToPtr |
+ { 99,99,99,99,99,99,99,99,99,11, 0,99,15, 0}, // IntToPtr |
{ 5, 5, 5, 0, 0, 5, 5, 0, 0,16,16, 5, 1,14}, // BitCast |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
};
diff --git a/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll b/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll
index 442089eecfcbb..42814cf6c3682 100644
--- a/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll
+++ b/llvm/test/Transforms/InstCombine/constant-expr-datalayout.ll
@@ -32,3 +32,17 @@ define i64 @OpenFilter(i64 %x) {
%r = zext i8 %t to i64
ret i64 %r
}
+
+define i64 @ptr2addr1() {
+; CHECK-LABEL: @ptr2addr1(
+; CHECK-NEXT: ret i64 ptrtoaddr (ptr inttoptr (i64 1 to ptr) to i64)
+;
+ ret i64 ptrtoaddr (ptr getelementptr (i8, ptr null, i64 1) to i64)
+}
+
+define i64 @ptr2addr2() {
+; CHECK-LABEL: @ptr2addr2(
+; CHECK-NEXT: ret i64 ptrtoaddr (ptr inttoptr (i64 123 to ptr) to i64)
+;
+ ret i64 ptrtoaddr (ptr inttoptr (i64 123 to ptr) to i64)
+}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
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.
Could you add a few tests where address size < pointer size? Otherwise this looks good to me.
; CHECK-NEXT: ret i32 ptrtoaddr (ptr addrspace(1) inttoptr (i16 -1 to ptr addrspace(1)) to i32) | ||
; | ||
ret i32 ptrtoaddr (ptr addrspace(1) inttoptr (i16 -1 to ptr addrspace(1)) to i32) | ||
} |
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.
Hm, we can make some of these fold, but we should really change the isEliminableCastPair() API to accept DataLayout for that. Leaving these cases alone is fine for now.
42d9399
to
1101f3b
Compare
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.
LGTM
Fixes #157334.