[llvm] a991d53 - [InstCombine] add tests for gep math; NFC
    Sanjay Patel via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Tue Nov 24 10:56:40 PST 2020
    
    
  
Author: Sanjay Patel
Date: 2020-11-24T13:56:30-05:00
New Revision: a991d5334045d1e7f3c7e292245aecbce66184b0
URL: https://github.com/llvm/llvm-project/commit/a991d5334045d1e7f3c7e292245aecbce66184b0
DIFF: https://github.com/llvm/llvm-project/commit/a991d5334045d1e7f3c7e292245aecbce66184b0.diff
LOG: [InstCombine] add tests for gep math; NFC
Added: 
    
Modified: 
    llvm/test/Transforms/InstCombine/sub-gep.ll
Removed: 
    
################################################################################
diff  --git a/llvm/test/Transforms/InstCombine/sub-gep.ll b/llvm/test/Transforms/InstCombine/sub-gep.ll
index 2389b70c3452..d6ace8275c10 100644
--- a/llvm/test/Transforms/InstCombine/sub-gep.ll
+++ b/llvm/test/Transforms/InstCombine/sub-gep.ll
@@ -123,7 +123,36 @@ define i64 @test_inbounds2_nuw_swapped([0 x i32]* %base, i64 %idx) {
   ret i64 %d
 }
 
-; The sub and shl here could be nuw, but this is harder to handle.
+define i64 @test_inbounds_two_gep([0 x i32]* %base, i64 %idx, i64 %idx2) {
+; CHECK-LABEL: @test_inbounds_two_gep(
+; CHECK-NEXT:    [[P2_IDX:%.*]] = shl nsw i64 [[IDX2:%.*]], 2
+; CHECK-NEXT:    [[P1_IDX_NEG:%.*]] = mul i64 [[IDX:%.*]], -4
+; CHECK-NEXT:    [[GEPDIFF:%.*]] = add i64 [[P1_IDX_NEG]], [[P2_IDX]]
+; CHECK-NEXT:    ret i64 [[GEPDIFF]]
+;
+  %p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
+  %p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx2
+  %i1 = ptrtoint i32* %p1 to i64
+  %i2 = ptrtoint i32* %p2 to i64
+  %d = sub i64 %i2, %i1
+  ret i64 %d
+}
+
+define i64 @test_inbounds_nsw_two_gep([0 x i32]* %base, i64 %idx, i64 %idx2) {
+; CHECK-LABEL: @test_inbounds_nsw_two_gep(
+; CHECK-NEXT:    [[P2_IDX:%.*]] = shl nsw i64 [[IDX2:%.*]], 2
+; CHECK-NEXT:    [[P1_IDX_NEG:%.*]] = mul i64 [[IDX:%.*]], -4
+; CHECK-NEXT:    [[GEPDIFF:%.*]] = add i64 [[P1_IDX_NEG]], [[P2_IDX]]
+; CHECK-NEXT:    ret i64 [[GEPDIFF]]
+;
+  %p1 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx
+  %p2 = getelementptr inbounds [0 x i32], [0 x i32]* %base, i64 0, i64 %idx2
+  %i1 = ptrtoint i32* %p1 to i64
+  %i2 = ptrtoint i32* %p2 to i64
+  %d = sub nsw i64 %i2, %i1
+  ret i64 %d
+}
+
 define i64 @test_inbounds_nuw_two_gep([0 x i32]* %base, i64 %idx, i64 %idx2) {
 ; CHECK-LABEL: @test_inbounds_nuw_two_gep(
 ; CHECK-NEXT:    [[P2_IDX:%.*]] = shl nsw i64 [[IDX2:%.*]], 2
        
    
    
More information about the llvm-commits
mailing list