xref: /llvm-project/llvm/test/Transforms/SeparateConstOffsetFromGEP/split-gep-sub.ll (revision 06f06644efb003e0748e94ac11cb230271a006f8)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S -passes="separate-const-offset-from-gep<lower-gep>" < %s | FileCheck %s
3
4; Check that GEP with an index 'A - B + [ConstantInt]' will be split into two
5; GEPs. eg.
6;     %A = ...
7;     %B = ...
8;     %sub = %A - %B
9;     %idx = %sub + 10
10;     %gep = getelementptr int, ptr %p, %idx
11;  will be transformed into:
12;     %A = ...
13;     %B = ...
14;     %sub = %A - %B
15;     %gep_base = getelementptr int, ptr %p, %sub
16;     %gep = getelementptr int, ptr %gep_base, 10
17
18define void @test_A_sub_B_add_ConstantInt(ptr %p) {
19; CHECK-LABEL: @test_A_sub_B_add_ConstantInt(
20; CHECK-NEXT:  entry:
21; CHECK-NEXT:    [[TMP0:%.*]] = tail call i32 @foo()
22; CHECK-NEXT:    [[REM:%.*]] = srem i32 [[TMP0]], 5
23; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
24; CHECK:       for.body:
25; CHECK-NEXT:    [[K:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[COND_END:%.*]] ]
26; CHECK-NEXT:    [[MUL:%.*]] = mul nuw nsw i32 [[K]], 5
27; CHECK-NEXT:    [[SUB1:%.*]] = sub nsw i32 [[MUL]], [[REM]]
28; CHECK-NEXT:    [[CMP26:%.*]] = icmp ult i32 [[SUB1]], 512
29; CHECK-NEXT:    br i1 [[CMP26]], label [[COND_TRUE:%.*]], label [[COND_END]]
30; CHECK:       cond.true:
31; CHECK-NEXT:    [[TMP1:%.*]] = sext i32 [[MUL]] to i64
32; CHECK-NEXT:    [[TMP2:%.*]] = sext i32 [[REM]] to i64
33; CHECK-NEXT:    [[SUB22:%.*]] = sub i64 [[TMP2]], [[TMP1]]
34; CHECK-NEXT:    [[TMP3:%.*]] = ptrtoint ptr [[P:%.*]] to i64
35; CHECK-NEXT:    [[TMP4:%.*]] = shl i64 [[SUB22]], 2
36; CHECK-NEXT:    [[TMP5:%.*]] = add i64 [[TMP3]], [[TMP4]]
37; CHECK-NEXT:    [[TMP6:%.*]] = add i64 [[TMP5]], 2044
38; CHECK-NEXT:    [[TMP7:%.*]] = inttoptr i64 [[TMP6]] to ptr
39; CHECK-NEXT:    store float 1.000000e+00, ptr [[TMP7]], align 4
40; CHECK-NEXT:    br label [[COND_END]]
41; CHECK:       cond.end:
42; CHECK-NEXT:    [[INC]] = add nuw nsw i32 [[K]], 1
43; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp ne i32 [[INC]], 100
44; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_BODY]], label [[FOR_END:%.*]]
45; CHECK:       for.end:
46; CHECK-NEXT:    ret void
47;
48entry:
49  %0 = tail call i32 @foo()
50  %rem = srem i32 %0, 5
51  %add = add nsw i32 %rem , 511
52  br label %for.body
53
54for.body:
55  %k = phi i32 [ 0, %entry ], [ %inc, %cond.end ]
56  %mul = mul nuw nsw i32 %k, 5
57  %sub1 = sub nsw i32 %mul, %rem
58  %cmp26 = icmp ult i32 %sub1, 512
59  br i1 %cmp26, label %cond.true, label %cond.end
60
61cond.true:
62  %sub2 = sub nsw i32 %add, %mul
63  %idxprom = sext i32 %sub2 to i64
64  %arryidx = getelementptr inbounds float, ptr %p, i64 %idxprom
65  store float 1.0, ptr %arryidx, align 4
66  br label %cond.end
67
68cond.end:
69  %inc = add nuw nsw i32 %k, 1
70  %exitcond = icmp ne i32 %inc, 100
71  br i1 %exitcond, label %for.body, label %for.end
72
73for.end:
74  ret void
75}
76
77declare i32 @foo()
78