1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=globalopt < %s -S | FileCheck %s
4 @i = internal unnamed_addr global i32 1, align 4
5 @r = internal global i64 0, align 8
7 ; negative test for store-once-global - the urem constant expression must not be speculated
9 declare dso_local void @use(i32)
11 define i32 @cantrap_constant() {
12 ; CHECK-LABEL: @cantrap_constant(
14 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* @i, align 4
15 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[TMP0]], 0
16 ; CHECK-NEXT: [[NOT_TOBOOL:%.*]] = xor i1 [[TOBOOL]], true
17 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = zext i1 [[NOT_TOBOOL]] to i32
18 ; CHECK-NEXT: tail call void @use(i32 [[SPEC_SELECT]])
19 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_THEN:%.*]], label [[EXIT:%.*]]
21 ; CHECK-NEXT: store i32 trunc (i64 urem (i64 7, i64 zext (i1 icmp eq (i64* inttoptr (i64 1 to i64*), i64* @r) to i64)) to i32), i32* @i, align 4
22 ; CHECK-NEXT: br label [[EXIT]]
24 ; CHECK-NEXT: ret i32 0
27 %0 = load i32, i32* @i, align 4
28 %tobool = icmp eq i32 %0, 0
29 %not.tobool = xor i1 %tobool, true
30 %spec.select = zext i1 %not.tobool to i32
31 tail call void @use(i32 %spec.select)
32 br i1 %tobool, label %if.then, label %exit
35 store i32 trunc (i64 urem (i64 7, i64 zext (i1 icmp eq (i64* inttoptr (i64 1 to i64*), i64* @r) to i64)) to i32), i32* @i, align 4
42 ; negative test for store-once-global - the srem constant expression must not be speculated
44 @b1 = internal global i64* null, align 8
45 @d1 = internal unnamed_addr global i32 0, align 2
47 define void @maytrap() {
48 ; CHECK-LABEL: @maytrap(
49 ; CHECK-NEXT: store i32 srem (i32 7, i32 zext (i1 icmp eq (i64** inttoptr (i64 16 to i64**), i64** @b1) to i32)), i32* @d1, align 2
50 ; CHECK-NEXT: ret void
52 store i32 srem (i32 7, i32 zext (i1 icmp eq (i64** inttoptr (i64 16 to i64**), i64** @b1) to i32)), i32* @d1, align 2
57 ; CHECK-LABEL: @main1(
58 ; CHECK-NEXT: [[T0:%.*]] = load i32, i32* @d1, align 2
59 ; CHECK-NEXT: ret i32 [[T0]]
61 %t0 = load i32, i32* @d1, align 2
65 ; This is fine to optimize as a store-once-global constant because the expression can't trap
67 @b2 = internal global i64* null, align 8
68 @d2 = internal unnamed_addr global i32 0, align 2
70 define void @maynottrap() {
71 ; CHECK-LABEL: @maynottrap(
72 ; CHECK-NEXT: store i1 true, i1* @d2, align 1
73 ; CHECK-NEXT: ret void
75 store i32 mul (i32 7, i32 zext (i1 icmp eq (i64** inttoptr (i64 16 to i64**), i64** @b2) to i32)), i32* @d2, align 2
80 ; CHECK-LABEL: @main2(
81 ; CHECK-NEXT: [[T0_B:%.*]] = load i1, i1* @d2, align 1
82 ; CHECK-NEXT: [[T0:%.*]] = select i1 [[T0_B]], i32 mul (i32 zext (i1 icmp eq (i64** inttoptr (i64 16 to i64**), i64** @b2) to i32), i32 7), i32 0
83 ; CHECK-NEXT: ret i32 [[T0]]
85 %t0 = load i32, i32* @d2, align 2