/
pass_constprop.go
280 lines (247 loc) · 10.7 KB
/
pass_constprop.go
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ssa
import (
`fmt`
`math/bits`
`unsafe`
`github.com/cloudwego/frugal/internal/atm/abi`
)
// ConstProp propagates constant through the expression tree.
type ConstProp struct{}
func (ConstProp) unary(v int64, op IrUnaryOp) int64 {
switch op {
case IrOpNegate : return -v
case IrOpSwap16 : return int64(bits.ReverseBytes16(uint16(v)))
case IrOpSwap32 : return int64(bits.ReverseBytes32(uint32(v)))
case IrOpSwap64 : return int64(bits.ReverseBytes64(uint64(v)))
case IrOpSx32to64 : return int64(int32(v))
default : panic(fmt.Sprintf("constprop: invalid unary operator: %d", op))
}
}
func (ConstProp) binary(x int64, y int64, op IrBinaryOp) int64 {
switch op {
case IrOpAdd : return x + y
case IrOpSub : return x - y
case IrOpMul : return x * y
case IrOpAnd : return x & y
case IrOpOr : return x | y
case IrOpXor : return x ^ y
case IrOpShr : return x >> y
case IrCmpEq : if x == y { return 1 } else { return 0 }
case IrCmpNe : if x != y { return 1 } else { return 0 }
case IrCmpLt : if x < y { return 1 } else { return 0 }
case IrCmpLtu : if uint64(x) < uint64(y) { return 1 } else { return 0 }
case IrCmpGeu : if uint64(x) >= uint64(y) { return 1 } else { return 0 }
default : panic(fmt.Sprintf("constprop: invalid binary operator: %d", op))
}
}
func (ConstProp) testandset(x int64, y int64) (int64, int64) {
if bv := int64(1 << y); x & bv == 0 {
return 0, x | bv
} else {
return 1, x | bv
}
}
func (self ConstProp) Apply(cfg *CFG) {
done := false
consts := make(map[Reg]_ConstData)
/* constant zero registers */
consts[Rz] = constint(0)
consts[Pn] = constptr(nil, Const)
/* const adder */
addconst := func(r Reg, v _ConstData) {
if _, ok := consts[r]; !ok {
done = false
consts[r] = v
}
}
/* evaluate const expression until no modifications were made */
for !done {
done = true
for _, bb := range cfg.PostOrder().Reversed() {
phi := make([]*IrPhi, 0, len(bb.Phi))
ins := make([]IrNode, 0, len(bb.Ins))
isconst := false
/* check every Phi node */
for _, p := range bb.Phi {
var first bool
var cdata _ConstData
/* assume it's a const */
first = true
isconst = true
/* a Phi node is a const iff all it's arguments are the same const */
for _, r := range p.V {
if *r != p.R {
if cc, ok := consts[*r]; !ok {
isconst = false
break
} else if first {
cdata = cc
first = false
} else if cdata != cc {
isconst = false
break
}
}
}
/* not constant, keep it as is */
if !isconst {
phi = append(phi, p)
continue
}
/* registers declared by Phi nodes can never be zero registers */
if p.R.Kind() == K_zero {
panic("constprop: assignment to zero registers in Phi node: " + p.String())
}
/* replace the Phi node with a Const node */
if cdata.i {
ins = append(ins, &IrConstInt { R: p.R, V: cdata.v })
} else {
ins = append(ins, &IrConstPtr { R: p.R, P: cdata.p, M: cdata.c })
}
/* mark as constant */
done = false
consts[p.R] = cdata
}
/* check every instructions */
for _, v := range bb.Ins {
switch p := v.(type) {
default: {
ins = append(ins, p)
}
/* value loads */
case *IrLoad: {
var ok bool
var iv int64
var cc _ConstData
var pv unsafe.Pointer
/* must be constant memory address */
if cc, ok = consts[p.Mem]; !ok {
ins = append(ins, p)
break
}
/* address must be a pointer */
if cc.i {
panic(fmt.Sprintf("constprop: value load on integer value %#x: %s", cc.v, p))
}
/* do not load a volatile pointer */
if cc.c != Const {
ins = append(ins, p)
break
}
/* constant pointer */
if p.R.Ptr() {
if p.Size != abi.PtrSize {
panic("constprop: pointer load of invalid size: " + p.String())
} else {
pv = *(*unsafe.Pointer)(cc.p)
ins = append(ins, &IrConstPtr { R: p.R, P: pv, M: Volatile })
addconst(p.R, constptr(pv, Volatile))
break
}
}
/* read the integer */
switch p.Size {
case 1 : iv = int64(*(*uint8)(cc.p))
case 2 : iv = int64(*(*uint16)(cc.p))
case 4 : iv = int64(*(*uint32)(cc.p))
case 8 : iv = int64(*(*uint64)(cc.p))
default : panic("constprop: integer load of invalid size: " + p.String())
}
/* replace the instruction */
ins = append(ins, &IrConstInt { R: p.R, V: iv })
addconst(p.R, constint(iv))
}
/* integer constant */
case *IrConstInt: {
ins = append(ins, p)
addconst(p.R, constint(p.V))
}
/* pointer constant */
case *IrConstPtr: {
ins = append(ins, p)
addconst(p.R, constptr(p.P, p.M))
}
/* pointer arithmetics */
case *IrLEA: {
if mem, ok := consts[p.Mem]; !ok {
ins = append(ins, p)
} else if off, ok := consts[p.Off]; !ok {
ins = append(ins, p)
} else if mem.i {
panic(fmt.Sprintf("constprop: pointer operation on integer value %#x: %s", mem.v, p))
} else if !off.i {
panic(fmt.Sprintf("constprop: pointer operation with pointer offset %p: %s", off.p, p))
} else {
r := addptr(mem.p, off.v)
ins = append(ins, &IrConstPtr { R: p.R, P: r, M: Volatile })
addconst(p.R, constptr(r, mem.c))
}
}
/* unary expressions */
case *IrUnaryExpr: {
if cc, ok := consts[p.V]; !ok {
ins = append(ins, p)
} else if !cc.i {
panic(fmt.Sprintf("constprop: integer operation on pointer value %p: %s", cc.p, p))
} else {
r := self.unary(cc.v, p.Op)
ins = append(ins, &IrConstInt { R: p.R, V: r })
addconst(p.R, constint(r))
}
}
/* binary expressions */
case *IrBinaryExpr: {
if x, ok := consts[p.X]; !ok {
ins = append(ins, p)
} else if y, ok := consts[p.Y]; !ok {
ins = append(ins, p)
} else if !x.i {
panic(fmt.Sprintf("constprop: integer operation on pointer value %p: %s", x.p, p))
} else if !y.i {
panic(fmt.Sprintf("constprop: integer operation on pointer value %p: %s", y.p, p))
} else {
r := self.binary(x.v, y.v, p.Op)
ins = append(ins, &IrConstInt { R: p.R, V: r })
addconst(p.R, constint(r))
}
}
/* bit test and set operation */
case *IrBitTestSet: {
if x, ok := consts[p.X]; !ok {
ins = append(ins, p)
} else if y, ok := consts[p.Y]; !ok {
ins = append(ins, p)
} else if !x.i {
panic(fmt.Sprintf("constprop: integer operation on pointer value %p: %s", x.p, p))
} else if !y.i {
panic(fmt.Sprintf("constprop: integer operation on pointer value %p: %s", y.p, p))
} else {
t, s := self.testandset(x.v, y.v)
ins = append(ins, &IrConstInt { R: p.T, V: t }, &IrConstInt { R: p.S, V: s })
addconst(p.T, constint(t))
addconst(p.S, constint(s))
}
}
}
}
/* rebuild the basic block */
bb.Phi = phi
bb.Ins = ins
}
}
}