aboutsummaryrefslogtreecommitdiffstats
path: root/test/Transforms/Reassociate/basictest.ll
blob: 0194ce2e302cafd00e44bfb1a940c197ce0a3262 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
; RUN: opt < %s -reassociate -gvn -instcombine -S | FileCheck %s

define i32 @test1(i32 %arg) {
  %tmp1 = sub i32 -12, %arg
  %tmp2 = add i32 %tmp1, 12
  ret i32 %tmp2

; CHECK-LABEL: @test1
; CHECK-NEXT: sub i32 0, %arg
; CHECK-NEXT: ret i32
}

define i32 @test2(i32 %reg109, i32 %reg1111) {
  %reg115 = add i32 %reg109, -30
  %reg116 = add i32 %reg115, %reg1111
  %reg117 = add i32 %reg116, 30
  ret i32 %reg117

; CHECK-LABEL: @test2
; CHECK-NEXT: %reg117 = add i32 %reg1111, %reg109
; CHECK-NEXT: ret i32 %reg117
}

@e = external global i32
@a = external global i32
@b = external global i32
@c = external global i32
@f = external global i32

define void @test3() {
  %A = load i32* @a
  %B = load i32* @b
  %C = load i32* @c
  %t1 = add i32 %A, %B
  %t2 = add i32 %t1, %C
  %t3 = add i32 %C, %A
  %t4 = add i32 %t3, %B
  ; e = (a+b)+c;
  store i32 %t2, i32* @e
  ; f = (a+c)+b
  store i32 %t4, i32* @f
  ret void

; CHECK-LABEL: @test3
; CHECK: add i32
; CHECK: add i32
; CHECK-NOT: add i32
; CHECK: ret void
}

define void @test4() {
  %A = load i32* @a
  %B = load i32* @b
  %C = load i32* @c
  %t1 = add i32 %A, %B
  %t2 = add i32 %t1, %C
  %t3 = add i32 %C, %A
  %t4 = add i32 %t3, %B
  ; e = c+(a+b)
  store i32 %t2, i32* @e
  ; f = (c+a)+b
  store i32 %t4, i32* @f
  ret void

; CHECK-LABEL: @test4
; CHECK: add i32
; CHECK: add i32
; CHECK-NOT: add i32
; CHECK: ret void
}

define void @test5() {
  %A = load i32* @a
  %B = load i32* @b
  %C = load i32* @c
  %t1 = add i32 %B, %A
  %t2 = add i32 %t1, %C
  %t3 = add i32 %C, %A
  %t4 = add i32 %t3, %B
  ; e = c+(b+a)
  store i32 %t2, i32* @e
  ; f = (c+a)+b
  store i32 %t4, i32* @f
  ret void

; CHECK-LABEL: @test5
; CHECK: add i32
; CHECK: add i32
; CHECK-NOT: add i32
; CHECK: ret void
}

define i32 @test6() {
  %tmp.0 = load i32* @a
  %tmp.1 = load i32* @b
  ; (a+b)
  %tmp.2 = add i32 %tmp.0, %tmp.1
  %tmp.4 = load i32* @c
  ; (a+b)+c
  %tmp.5 = add i32 %tmp.2, %tmp.4
  ; (a+c)
  %tmp.8 = add i32 %tmp.0, %tmp.4
  ; (a+c)+b
  %tmp.11 = add i32 %tmp.8, %tmp.1
  ; X ^ X = 0
  %RV = xor i32 %tmp.5, %tmp.11
  ret i32 %RV

; CHECK-LABEL: @test6
; CHECK: ret i32 0
}

; This should be one add and two multiplies.
define i32 @test7(i32 %A, i32 %B, i32 %C) {
  ; A*A*B + A*C*A
  %aa = mul i32 %A, %A
  %aab = mul i32 %aa, %B
  %ac = mul i32 %A, %C
  %aac = mul i32 %ac, %A
  %r = add i32 %aab, %aac
  ret i32 %r

; CHECK-LABEL: @test7
; CHECK-NEXT: add i32 %C, %B
; CHECK-NEXT: mul i32 
; CHECK-NEXT: mul i32 
; CHECK-NEXT: ret i32 
}

define i32 @test8(i32 %X, i32 %Y, i32 %Z) {
  %A = sub i32 0, %X
  %B = mul i32 %A, %Y
  ; (-X)*Y + Z -> Z-X*Y
  %C = add i32 %B, %Z
  ret i32 %C

; CHECK-LABEL: @test8
; CHECK-NEXT: %A = mul i32 %Y, %X
; CHECK-NEXT: %C = sub i32 %Z, %A
; CHECK-NEXT: ret i32 %C
}

; PR5458
define i32 @test9(i32 %X) {
  %Y = mul i32 %X, 47
  %Z = add i32 %Y, %Y
  ret i32 %Z
; CHECK-LABEL: @test9
; CHECK-NEXT: mul i32 %X, 94
; CHECK-NEXT: ret i32
}

define i32 @test10(i32 %X) {
  %Y = add i32 %X ,%X
  %Z = add i32 %Y, %X
  ret i32 %Z
; CHECK-LABEL: @test10
; CHECK-NEXT: mul i32 %X, 3
; CHECK-NEXT: ret i32
}

define i32 @test11(i32 %W) {
  %X = mul i32 %W, 127
  %Y = add i32 %X ,%X
  %Z = add i32 %Y, %X
  ret i32 %Z
; CHECK-LABEL: @test11
; CHECK-NEXT: mul i32 %W, 381
; CHECK-NEXT: ret i32
}

define i32 @test12(i32 %X) {
  %A = sub i32 1, %X
  %B = sub i32 2, %X
  %C = sub i32 3, %X
  %Y = add i32 %A ,%B
  %Z = add i32 %Y, %C
  ret i32 %Z
; CHECK-LABEL: @test12
; CHECK-NEXT: mul i32 %X, -3
; CHECK-NEXT: add i32{{.*}}, 6
; CHECK-NEXT: ret i32
}

define i32 @test13(i32 %X1, i32 %X2, i32 %X3) {
  %A = sub i32 0, %X1
  %B = mul i32 %A, %X2   ; -X1*X2
  %C = mul i32 %X1, %X3  ; X1*X3
  %D = add i32 %B, %C    ; -X1*X2 + X1*X3 -> X1*(X3-X2)
  ret i32 %D
; CHECK-LABEL: @test13
; CHECK-NEXT: sub i32 %X3, %X2
; CHECK-NEXT: mul i32 {{.*}}, %X1
; CHECK-NEXT: ret i32
}

; PR5359
define i32 @test14(i32 %X1, i32 %X2) {
  %B = mul i32 %X1, 47   ; X1*47
  %C = mul i32 %X2, -47  ; X2*-47
  %D = add i32 %B, %C    ; X1*47 + X2*-47 -> 47*(X1-X2)
  ret i32 %D

; CHECK-LABEL: @test14
; CHECK-NEXT: sub i32 %X1, %X2
; CHECK-NEXT: mul i32 %B2, 47
; CHECK-NEXT: ret i32
}

; Do not reassociate expressions of type i1
define i32 @test15(i32 %X1, i32 %X2, i32 %X3) {
  %A = icmp ne i32 %X1, 0
  %B = icmp slt i32 %X2, %X3
  %C = and i1 %A, %B
  %D = select i1 %C, i32 %X1, i32 0
  ret i32 %D
; CHECK-LABEL: @test15
; CHECK: and i1 %A, %B
}