File: reassoc-mul-nuw.ll

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (188 lines) | stat: -rw-r--r-- 7,736 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt < %s -passes=reassociate -S | FileCheck %s

; We cannot preserve nuw flags for mul
define i4 @nuw_preserve_negative(i4 %a, i4 %b, i4 %c) {
; CHECK-LABEL: define i4 @nuw_preserve_negative(
; CHECK-SAME: i4 [[A:%.*]], i4 [[B:%.*]], i4 [[C:%.*]]) {
; CHECK-NEXT:    [[V0:%.*]] = mul i4 [[B]], [[A]]
; CHECK-NEXT:    [[V1:%.*]] = mul i4 [[V0]], [[C]]
; CHECK-NEXT:    ret i4 [[V1]]
;
  %v0 = mul nuw i4 %a, %c
  %v1 = mul nuw i4 %v0, %b
  ret i4 %v1
}

; TODO: we can add nuw flags if we know all operands are non-zero.
define i4 @nuw_preserve_non_zero(i4 %a, i4 %b, i4 %c) {
; CHECK-LABEL: define i4 @nuw_preserve_non_zero(
; CHECK-SAME: i4 [[A:%.*]], i4 [[B:%.*]], i4 [[C:%.*]]) {
; CHECK-NEXT:    [[A0:%.*]] = add nuw i4 [[A]], 1
; CHECK-NEXT:    [[B0:%.*]] = add nuw i4 [[B]], 1
; CHECK-NEXT:    [[C0:%.*]] = add nuw i4 [[C]], 1
; CHECK-NEXT:    [[V0:%.*]] = mul nuw i4 [[B0]], [[A0]]
; CHECK-NEXT:    [[V1:%.*]] = mul nuw i4 [[V0]], [[C0]]
; CHECK-NEXT:    ret i4 [[V1]]
;
  %a0 = add nuw i4 %a, 1
  %b0 = add nuw i4 %b, 1
  %c0 = add nuw i4 %c, 1
  %v0 = mul nuw i4 %a0, %c0
  %v1 = mul nuw i4 %v0, %b0
  ret i4 %v1
}

define i4 @re_order_mul_nuw(i4 %xx0, i4 %xx1, i4 %xx2, i4 %xx3) {
; CHECK-LABEL: define i4 @re_order_mul_nuw(
; CHECK-SAME: i4 [[XX0:%.*]], i4 [[XX1:%.*]], i4 [[XX2:%.*]], i4 [[XX3:%.*]]) {
; CHECK-NEXT:    [[X0:%.*]] = add nuw i4 [[XX0]], 1
; CHECK-NEXT:    [[X1:%.*]] = add nuw i4 [[XX1]], 1
; CHECK-NEXT:    [[X2:%.*]] = add nuw i4 [[XX2]], 1
; CHECK-NEXT:    [[X3:%.*]] = add nuw i4 [[XX3]], 1
; CHECK-NEXT:    [[MUL_B:%.*]] = mul nuw i4 [[X1]], [[X0]]
; CHECK-NEXT:    [[MUL_A:%.*]] = mul nuw i4 [[MUL_B]], [[X2]]
; CHECK-NEXT:    [[MUL_C:%.*]] = mul nuw i4 [[MUL_A]], [[X3]]
; CHECK-NEXT:    ret i4 [[MUL_C]]
;
  %x0 = add nuw i4 %xx0, 1
  %x1 = add nuw i4 %xx1, 1
  %x2 = add nuw i4 %xx2, 1
  %x3 = add nuw i4 %xx3, 1
  %mul_a = mul nuw i4 %x0, %x1
  %mul_b = mul nuw i4 %x2, %x3
  %mul_c = mul nuw i4 %mul_a, %mul_b
  ret i4 %mul_c
}

define i4 @re_order_mul_nuw_fail_maybe_zero(i4 %xx0, i4 %xx1, i4 %xx2, i4 %xx3) {
; CHECK-LABEL: define i4 @re_order_mul_nuw_fail_maybe_zero(
; CHECK-SAME: i4 [[XX0:%.*]], i4 [[XX1:%.*]], i4 [[XX2:%.*]], i4 [[XX3:%.*]]) {
; CHECK-NEXT:    [[X0:%.*]] = add nsw i4 [[XX0]], 1
; CHECK-NEXT:    [[X1:%.*]] = add nuw i4 [[XX1]], 1
; CHECK-NEXT:    [[X2:%.*]] = add nuw i4 [[XX2]], 1
; CHECK-NEXT:    [[X3:%.*]] = add nuw i4 [[XX3]], 1
; CHECK-NEXT:    [[MUL_B:%.*]] = mul i4 [[X1]], [[X0]]
; CHECK-NEXT:    [[MUL_A:%.*]] = mul i4 [[MUL_B]], [[X2]]
; CHECK-NEXT:    [[MUL_C:%.*]] = mul i4 [[MUL_A]], [[X3]]
; CHECK-NEXT:    ret i4 [[MUL_C]]
;
  %x0 = add nsw i4 %xx0, 1
  %x1 = add nuw i4 %xx1, 1
  %x2 = add nuw i4 %xx2, 1
  %x3 = add nuw i4 %xx3, 1
  %mul_a = mul nuw i4 %x0, %x1
  %mul_b = mul nuw i4 %x2, %x3
  %mul_c = mul nuw i4 %mul_a, %mul_b
  ret i4 %mul_c
}

define i4 @re_order_mul_nsw(i4 %xx0, i4 %xx1, i4 %xx2, i4 %xx3) {
; CHECK-LABEL: define i4 @re_order_mul_nsw(
; CHECK-SAME: i4 [[XX0:%.*]], i4 [[XX1:%.*]], i4 [[XX2:%.*]], i4 [[XX3:%.*]]) {
; CHECK-NEXT:    [[X0_NZ:%.*]] = add nuw i4 [[XX0]], 1
; CHECK-NEXT:    [[X1_NZ:%.*]] = add nuw i4 [[XX1]], 1
; CHECK-NEXT:    [[X2_NZ:%.*]] = add nuw i4 [[XX2]], 1
; CHECK-NEXT:    [[X3_NZ:%.*]] = add nuw i4 [[XX3]], 1
; CHECK-NEXT:    [[X0:%.*]] = call i4 @llvm.smax.i4(i4 [[X0_NZ]], i4 1)
; CHECK-NEXT:    [[X1:%.*]] = call i4 @llvm.smax.i4(i4 [[X1_NZ]], i4 1)
; CHECK-NEXT:    [[X2:%.*]] = call i4 @llvm.smax.i4(i4 [[X2_NZ]], i4 1)
; CHECK-NEXT:    [[X3:%.*]] = call i4 @llvm.smax.i4(i4 [[X3_NZ]], i4 1)
; CHECK-NEXT:    [[MUL_B:%.*]] = mul nsw i4 [[X1]], [[X0]]
; CHECK-NEXT:    [[MUL_A:%.*]] = mul nsw i4 [[MUL_B]], [[X2]]
; CHECK-NEXT:    [[MUL_C:%.*]] = mul nsw i4 [[MUL_A]], [[X3]]
; CHECK-NEXT:    ret i4 [[MUL_C]]
;
  %x0_nz = add nuw i4 %xx0, 1
  %x1_nz = add nuw i4 %xx1, 1
  %x2_nz = add nuw i4 %xx2, 1
  %x3_nz = add nuw i4 %xx3, 1
  %x0 = call i4 @llvm.smax.i4(i4 %x0_nz, i4 1)
  %x1 = call i4 @llvm.smax.i4(i4 %x1_nz, i4 1)
  %x2 = call i4 @llvm.smax.i4(i4 %x2_nz, i4 1)
  %x3 = call i4 @llvm.smax.i4(i4 %x3_nz, i4 1)
  %mul_a = mul nsw i4 %x0, %x1
  %mul_b = mul nsw i4 %x2, %x3
  %mul_c = mul nsw i4 %mul_a, %mul_b
  ret i4 %mul_c
}

define i4 @re_order_mul_nsw_nuw(i4 %xx0, i4 %xx1, i4 %xx2, i4 %xx3) {
; CHECK-LABEL: define i4 @re_order_mul_nsw_nuw(
; CHECK-SAME: i4 [[XX0:%.*]], i4 [[XX1:%.*]], i4 [[XX2:%.*]], i4 [[XX3:%.*]]) {
; CHECK-NEXT:    [[X0:%.*]] = add nuw i4 [[XX0]], 1
; CHECK-NEXT:    [[X1:%.*]] = add nuw i4 [[XX1]], 1
; CHECK-NEXT:    [[X2:%.*]] = add nuw i4 [[XX2]], 1
; CHECK-NEXT:    [[X3:%.*]] = add nuw i4 [[XX3]], 1
; CHECK-NEXT:    [[MUL_B:%.*]] = mul nuw nsw i4 [[X1]], [[X0]]
; CHECK-NEXT:    [[MUL_A:%.*]] = mul nuw nsw i4 [[MUL_B]], [[X2]]
; CHECK-NEXT:    [[MUL_C:%.*]] = mul nuw nsw i4 [[MUL_A]], [[X3]]
; CHECK-NEXT:    ret i4 [[MUL_C]]
;
  %x0 = add nuw i4 %xx0, 1
  %x1 = add nuw i4 %xx1, 1
  %x2 = add nuw i4 %xx2, 1
  %x3 = add nuw i4 %xx3, 1
  %mul_a = mul nuw nsw i4 %x0, %x1
  %mul_b = mul nuw nsw i4 %x2, %x3
  %mul_c = mul nuw nsw i4 %mul_a, %mul_b
  ret i4 %mul_c
}

define i4 @re_order_mul_fail_maybe_neg(i4 %xx0, i4 %xx1, i4 %xx2, i4 %xx3) {
; CHECK-LABEL: define i4 @re_order_mul_fail_maybe_neg(
; CHECK-SAME: i4 [[XX0:%.*]], i4 [[XX1:%.*]], i4 [[XX2:%.*]], i4 [[XX3:%.*]]) {
; CHECK-NEXT:    [[X0_NZ:%.*]] = add nuw i4 [[XX0]], 1
; CHECK-NEXT:    [[X1_NZ:%.*]] = add nuw i4 [[XX1]], 1
; CHECK-NEXT:    [[X2_NZ:%.*]] = add nuw i4 [[XX2]], 1
; CHECK-NEXT:    [[X3:%.*]] = add nuw i4 [[XX3]], 1
; CHECK-NEXT:    [[X0:%.*]] = call i4 @llvm.smax.i4(i4 [[X0_NZ]], i4 1)
; CHECK-NEXT:    [[X1:%.*]] = call i4 @llvm.smax.i4(i4 [[X1_NZ]], i4 1)
; CHECK-NEXT:    [[X2:%.*]] = call i4 @llvm.smax.i4(i4 [[X2_NZ]], i4 1)
; CHECK-NEXT:    [[MUL_B:%.*]] = mul i4 [[X1]], [[X0]]
; CHECK-NEXT:    [[MUL_A:%.*]] = mul i4 [[MUL_B]], [[X3]]
; CHECK-NEXT:    [[MUL_C:%.*]] = mul i4 [[MUL_A]], [[X2]]
; CHECK-NEXT:    ret i4 [[MUL_C]]
;
  %x0_nz = add nuw i4 %xx0, 1
  %x1_nz = add nuw i4 %xx1, 1
  %x2_nz = add nuw i4 %xx2, 1
  %x3 = add nuw i4 %xx3, 1
  %x0 = call i4 @llvm.smax.i4(i4 %x0_nz, i4 1)
  %x1 = call i4 @llvm.smax.i4(i4 %x1_nz, i4 1)
  %x2 = call i4 @llvm.smax.i4(i4 %x2_nz, i4 1)
  %mul_a = mul nsw i4 %x0, %x1
  %mul_b = mul nsw i4 %x2, %x3
  %mul_c = mul nsw i4 %mul_a, %mul_b
  ret i4 %mul_c
}

define i4 @re_order_mul_nsw_fail_maybe_z(i4 %xx0, i4 %xx1, i4 %xx2, i4 %xx3) {
; CHECK-LABEL: define i4 @re_order_mul_nsw_fail_maybe_z(
; CHECK-SAME: i4 [[XX0:%.*]], i4 [[XX1:%.*]], i4 [[XX2:%.*]], i4 [[XX3:%.*]]) {
; CHECK-NEXT:    [[X0_NZ:%.*]] = add nuw i4 [[XX0]], 1
; CHECK-NEXT:    [[X1_NZ:%.*]] = add nuw i4 [[XX1]], 1
; CHECK-NEXT:    [[X2_NZ:%.*]] = add nuw i4 [[XX2]], 1
; CHECK-NEXT:    [[X3_NZ:%.*]] = add nuw i4 [[XX3]], 1
; CHECK-NEXT:    [[X0:%.*]] = call i4 @llvm.smax.i4(i4 [[X0_NZ]], i4 1)
; CHECK-NEXT:    [[X1:%.*]] = call i4 @llvm.smax.i4(i4 [[X1_NZ]], i4 1)
; CHECK-NEXT:    [[X2:%.*]] = call i4 @llvm.smax.i4(i4 [[X2_NZ]], i4 0)
; CHECK-NEXT:    [[X3:%.*]] = call i4 @llvm.smax.i4(i4 [[X3_NZ]], i4 1)
; CHECK-NEXT:    [[MUL_B:%.*]] = mul i4 [[X1]], [[X0]]
; CHECK-NEXT:    [[MUL_A:%.*]] = mul i4 [[MUL_B]], [[X2]]
; CHECK-NEXT:    [[MUL_C:%.*]] = mul i4 [[MUL_A]], [[X3]]
; CHECK-NEXT:    ret i4 [[MUL_C]]
;
  %x0_nz = add nuw i4 %xx0, 1
  %x1_nz = add nuw i4 %xx1, 1
  %x2_nz = add nuw i4 %xx2, 1
  %x3_nz = add nuw i4 %xx3, 1
  %x0 = call i4 @llvm.smax.i4(i4 %x0_nz, i4 1)
  %x1 = call i4 @llvm.smax.i4(i4 %x1_nz, i4 1)
  %x2 = call i4 @llvm.smax.i4(i4 %x2_nz, i4 0)
  %x3 = call i4 @llvm.smax.i4(i4 %x3_nz, i4 1)
  %mul_a = mul nsw i4 %x0, %x1
  %mul_b = mul nsw i4 %x2, %x3
  %mul_c = mul nsw i4 %mul_a, %mul_b
  ret i4 %mul_c
}