xref: /llvm-project/llvm/test/Transforms/DeadStoreElimination/out-of-bounds-stores.ll (revision f497a00da968b0ff90d8c98caa184d14b9a92495)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -passes=dse -S %s | FileCheck %s
3
4declare void @use_pointer(ptr)
5
6; Out-of-bounds stores can be considered killing any other stores to the same
7; object in the same BB, because they are UB and guaranteed to execute. Note
8; that cases in which the BB is exited through unwinding are handled separately
9; by DSE and the unwinding call will be considered as clobber.
10define i32 @test_out_of_bounds_store_local(i1 %c) {
11; CHECK-LABEL: @test_out_of_bounds_store_local(
12; CHECK-NEXT:    [[D:%.*]] = alloca [1 x i32], align 4
13; CHECK-NEXT:    [[ARRAYIDX_1:%.*]] = getelementptr inbounds [1 x i32], ptr [[D]], i64 0, i64 1
14; CHECK-NEXT:    store i32 20, ptr [[ARRAYIDX_1]], align 4
15; CHECK-NEXT:    call void @use_pointer(ptr [[D]])
16; CHECK-NEXT:    ret i32 0
17;
18  %d = alloca [1 x i32], align 4
19  store i32 10, ptr %d, align 4
20  %arrayidx.1 = getelementptr inbounds [1 x i32], ptr %d, i64 0, i64 1
21  store i32 20, ptr %arrayidx.1, align 4
22  call void @use_pointer(ptr %d)
23  ret i32 0
24}
25
26; Similar to @test_out_of_bounds_store_local, but with multiple in-bounds
27; stores to a larger object, followed by an out-of-bounds store.
28; FIXME: the 2 inbounds stores could be removed by applying the same
29; reasoning as for @test_out_of_bounds_store_local.
30define i32 @test_out_of_bounds_store_local_larger_object(i1 %c) {
31; CHECK-LABEL: @test_out_of_bounds_store_local_larger_object(
32; CHECK-NEXT:    [[D:%.*]] = alloca [2 x i32], align 4
33; CHECK-NEXT:    store i32 10, ptr [[D]], align 4
34; CHECK-NEXT:    [[ARRAYIDX_1:%.*]] = getelementptr inbounds [2 x i32], ptr [[D]], i64 0, i64 1
35; CHECK-NEXT:    store i32 20, ptr [[ARRAYIDX_1]], align 4
36; CHECK-NEXT:    [[ARRAYIDX_2:%.*]] = getelementptr inbounds [2 x i32], ptr [[D]], i64 0, i64 2
37; CHECK-NEXT:    store i32 30, ptr [[ARRAYIDX_2]], align 4
38; CHECK-NEXT:    call void @use_pointer(ptr [[D]])
39; CHECK-NEXT:    ret i32 0
40;
41  %d = alloca [2 x i32], align 4
42  store i32 10, ptr %d, align 4
43  %arrayidx.1 = getelementptr inbounds [2 x i32], ptr %d, i64 0, i64 1
44  store i32 20, ptr %arrayidx.1, align 4
45  %arrayidx.2 = getelementptr inbounds [2 x i32], ptr %d, i64 0, i64 2
46  store i32 30, ptr %arrayidx.2, align 4
47  call void @use_pointer(ptr %d)
48  ret i32 0
49}
50
51; Make sure that out-of-bound stores are not considered killing other stores to
52; the same underlying object, if they are in different basic blocks. The
53; out-of-bounds store may not be executed.
54;
55; Test case from PR48279. FIXME.
56define i32 @test_out_of_bounds_store_nonlocal(i1 %c) {
57; CHECK-LABEL: @test_out_of_bounds_store_nonlocal(
58; CHECK-NEXT:    [[D:%.*]] = alloca [1 x i32], align 4
59; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
60; CHECK:       for.body:
61; CHECK-NEXT:    store i32 10, ptr [[D]], align 4
62; CHECK-NEXT:    br label [[FOR_INC:%.*]]
63; CHECK:       for.inc:
64; CHECK-NEXT:    br i1 [[C:%.*]], label [[FOR_BODY_1:%.*]], label [[FOR_END:%.*]]
65; CHECK:       for.body.1:
66; CHECK-NEXT:    ret i32 1
67; CHECK:       for.end:
68; CHECK-NEXT:    call void @use_pointer(ptr [[D]])
69; CHECK-NEXT:    ret i32 0
70;
71  %d = alloca [1 x i32], align 4
72  br label %for.body
73
74for.body:                                         ; preds = %for.cond
75  store i32 10, ptr %d, align 4
76  br label %for.inc
77
78for.inc:                                          ; preds = %for.body
79  br i1 %c, label %for.body.1, label %for.end
80
81for.body.1:                                       ; preds = %for.inc
82  %arrayidx.1 = getelementptr inbounds [1 x i32], ptr %d, i64 0, i64 1
83  store i32 20, ptr %arrayidx.1, align 4
84  ret i32 1
85
86for.end:                                          ; preds = %for.inc
87  call void @use_pointer(ptr %d)
88  ret i32 0
89}
90