[llvm] b382619 - [InstCombine] Add additional tests for gep of minus ptrtoint (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 29 08:11:07 PDT 2022


Author: Nikita Popov
Date: 2022-04-29T17:10:58+02:00
New Revision: b3826192fb6e3f7f05ff21911f5f948ad5eabcdc

URL: https://github.com/llvm/llvm-project/commit/b3826192fb6e3f7f05ff21911f5f948ad5eabcdc
DIFF: https://github.com/llvm/llvm-project/commit/b3826192fb6e3f7f05ff21911f5f948ad5eabcdc.diff

LOG: [InstCombine] Add additional tests for gep of minus ptrtoint (NFC)

Added: 
    

Modified: 
    llvm/test/Transforms/InstCombine/constant-fold-gep.ll

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/InstCombine/constant-fold-gep.ll b/llvm/test/Transforms/InstCombine/constant-fold-gep.ll
index 53fe95afc2d0..6bee7bb79446 100644
--- a/llvm/test/Transforms/InstCombine/constant-fold-gep.ll
+++ b/llvm/test/Transforms/InstCombine/constant-fold-gep.ll
@@ -117,3 +117,87 @@ define i32 @test_gep_in_struct(i64 %idx) {
   %res = load i32, i32* %NS7, align 1
   ret i32 %res
 }
+
+ at g = external global i8
+ at g2 = external global i8
+
+declare i64 @get.i64()
+declare void @use.ptr(i8*)
+
+define i8* @gep_sub_self() {
+; CHECK-LABEL: @gep_sub_self(
+; CHECK-NEXT:    ret i8* null
+;
+  %p.int = ptrtoint i8* @g to i64
+  %p.int.neg = sub i64 0, %p.int
+  %p1 = getelementptr i8, i8* @g, i64 %p.int.neg
+  ret i8* %p1
+}
+
+define i8* @gep_sub_self_plus_addr(i64 %addr) {
+; CHECK-LABEL: @gep_sub_self_plus_addr(
+; CHECK-NEXT:    [[P2:%.*]] = getelementptr i8, i8* null, i64 [[ADDR:%.*]]
+; CHECK-NEXT:    ret i8* [[P2]]
+;
+  %p.int = ptrtoint i8* @g to i64
+  %p.int.neg = sub i64 0, %p.int
+  %p1 = getelementptr i8, i8* @g, i64 %p.int.neg
+  %p2 = getelementptr i8, i8* %p1, i64 %addr
+  ret i8* %p2
+}
+
+define i8* @gep_plus_addr_sub_self(i64 %addr) {
+; CHECK-LABEL: @gep_plus_addr_sub_self(
+; CHECK-NEXT:    [[P1:%.*]] = getelementptr i8, i8* @g, i64 [[ADDR:%.*]]
+; CHECK-NEXT:    [[P2:%.*]] = getelementptr i8, i8* [[P1]], i64 sub (i64 0, i64 ptrtoint (i8* @g to i64))
+; CHECK-NEXT:    ret i8* [[P2]]
+;
+  %p.int = ptrtoint i8* @g to i64
+  %p.int.neg = sub i64 0, %p.int
+  %p1 = getelementptr i8, i8* @g, i64 %addr
+  %p2 = getelementptr i8, i8* %p1, i64 %p.int.neg
+  ret i8* %p2
+}
+
+define i8* @gep_plus_addr_sub_self_in_loop() {
+; CHECK-LABEL: @gep_plus_addr_sub_self_in_loop(
+; CHECK-NEXT:    br label [[LOOP:%.*]]
+; CHECK:       loop:
+; CHECK-NEXT:    [[ADDR:%.*]] = call i64 @get.i64()
+; CHECK-NEXT:    [[P1:%.*]] = getelementptr i8, i8* @g, i64 [[ADDR]]
+; CHECK-NEXT:    [[P2:%.*]] = getelementptr i8, i8* [[P1]], i64 sub (i64 0, i64 ptrtoint (i8* @g to i64))
+; CHECK-NEXT:    call void @use.ptr(i8* [[P2]])
+; CHECK-NEXT:    br label [[LOOP]]
+;
+  %p.int = ptrtoint i8* @g to i64
+  %p.int.neg = sub i64 0, %p.int
+  br label %loop
+
+loop:
+  %addr = call i64 @get.i64()
+  %p1 = getelementptr i8, i8* @g, i64 %addr
+  %p2 = getelementptr i8, i8* %p1, i64 %p.int.neg
+  call void @use.ptr(i8* %p2)
+  br label %loop
+}
+
+define i8* @gep_sub_other() {
+; CHECK-LABEL: @gep_sub_other(
+; CHECK-NEXT:    ret i8* inttoptr (i64 sub (i64 ptrtoint (i8* @g to i64), i64 ptrtoint (i8* @g2 to i64)) to i8*)
+;
+  %p.int = ptrtoint i8* @g2 to i64
+  %p.int.neg = sub i64 0, %p.int
+  %p1 = getelementptr i8, i8* @g, i64 %p.int.neg
+  ret i8* %p1
+}
+
+define i64 @gep_sub_other_to_int() {
+; CHECK-LABEL: @gep_sub_other_to_int(
+; CHECK-NEXT:    ret i64 sub (i64 ptrtoint (i8* @g to i64), i64 ptrtoint (i8* @g2 to i64))
+;
+  %p.int = ptrtoint i8* @g2 to i64
+  %p.int.neg = sub i64 0, %p.int
+  %p1 = getelementptr i8, i8* @g, i64 %p.int.neg
+  %p1.int = ptrtoint i8* %p1 to i64
+  ret i64 %p1.int
+}


        


More information about the llvm-commits mailing list