-
Notifications
You must be signed in to change notification settings - Fork 0
/
arith.go
206 lines (197 loc) · 4.78 KB
/
arith.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
// Package optimize analyzes and optimizes Nebula IR.
//
package optimize // import "github.com/andrewarchi/nebula/ir/optimize"
import (
"fmt"
"math/big"
"github.com/andrewarchi/nebula/internal/bigint"
"github.com/andrewarchi/nebula/ir"
)
// FoldConstArith folds and propagates constant arithmetic expressions
// or identities.
func FoldConstArith(p *ir.Program) {
for _, block := range p.Blocks {
i := 0
for _, node := range block.Nodes {
switch inst := node.(type) {
case *ir.BinaryExpr:
val, isNeg := foldBinaryExpr(p, inst)
if isNeg {
neg := ir.NewUnaryExpr(ir.Neg, val, inst.Pos())
inst.ClearOperands()
inst.ReplaceUsesWith(neg)
node = neg
} else if val != nil {
inst.ClearOperands()
inst.ReplaceUsesWith(val)
continue
}
case *ir.UnaryExpr:
if inst.Op == ir.Neg {
val := inst.Operand(0).Def()
if lhs, ok := val.(*ir.IntConst); ok {
constNeg := ir.NewIntConst(new(big.Int).Neg(lhs.Int()), inst.Pos())
inst.ClearOperands()
inst.ReplaceUsesWith(constNeg)
continue
}
}
}
block.Nodes[i] = node
i++
}
block.Nodes = block.Nodes[:i]
}
}
func foldBinaryExpr(p *ir.Program, bin *ir.BinaryExpr) (ir.Value, bool) {
_, lhsConst := bin.Operand(0).Def().(*ir.IntConst)
_, rhsConst := bin.Operand(1).Def().(*ir.IntConst)
switch {
case lhsConst && rhsConst:
return foldBinaryLR(p, bin)
case lhsConst:
return foldBinaryL(p, bin)
case rhsConst:
return foldBinaryR(p, bin)
default:
return foldBinary(p, bin)
}
}
func foldBinaryLR(p *ir.Program, bin *ir.BinaryExpr) (ir.Value, bool) {
lhs := bin.Operand(0).Def().(*ir.IntConst)
rhs := bin.Operand(1).Def().(*ir.IntConst)
result := new(big.Int)
switch bin.Op {
case ir.Add:
result.Add(lhs.Int(), rhs.Int())
case ir.Sub:
result.Sub(lhs.Int(), rhs.Int())
case ir.Mul:
result.Mul(lhs.Int(), rhs.Int())
case ir.Div:
result.Div(lhs.Int(), rhs.Int())
case ir.Mod:
result.Mod(lhs.Int(), rhs.Int())
case ir.Shl:
s, ok := bigint.ToUint(rhs.Int())
if !ok {
panic(fmt.Sprintf("optimize: shl rhs overflow: %v", rhs.Int()))
}
result.Lsh(lhs.Int(), s)
case ir.LShr:
return nil, false
case ir.AShr:
s, ok := bigint.ToUint(rhs.Int())
if !ok {
panic(fmt.Sprintf("optimize: ashr rhs overflow: %v", rhs.Int()))
}
result.Rsh(lhs.Int(), s)
case ir.And:
result.And(lhs.Int(), rhs.Int())
case ir.Or:
result.Or(lhs.Int(), rhs.Int())
case ir.Xor:
result.Xor(lhs.Int(), rhs.Int())
default:
return nil, false
}
return ir.NewIntConst(result, bin.Pos()), false
}
var (
bigZero = big.NewInt(0)
bigOne = big.NewInt(1)
bigNegOne = big.NewInt(-1)
)
func foldBinaryL(p *ir.Program, bin *ir.BinaryExpr) (ir.Value, bool) {
lhs := bin.Operand(0).Def().(*ir.IntConst)
rhs := bin.Operand(1).Def()
switch lhs.Int().Sign() {
case 0:
switch bin.Op {
case ir.Add:
return rhs, false
case ir.Sub:
return rhs, true
case ir.Mul:
return lhs, false
case ir.Div, ir.Mod:
// TODO trap if RHS zero
return lhs, false
}
case 1:
if bin.Op == ir.Mul && lhs.Int().Cmp(bigOne) == 0 {
return rhs, false
}
case -1:
if bin.Op == ir.Mul && lhs.Int().Cmp(bigNegOne) == 0 {
return rhs, true
}
}
return nil, false
}
func foldBinaryR(p *ir.Program, bin *ir.BinaryExpr) (ir.Value, bool) {
lhs := bin.Operand(0).Def()
rhs := bin.Operand(1).Def().(*ir.IntConst)
switch rhs.Int().Sign() {
case 0:
switch bin.Op {
case ir.Add, ir.Sub:
return lhs, false
case ir.Mul:
return rhs, false
case ir.Div, ir.Mod:
panic("optimize: divide by zero")
}
case 1:
if rhs.Int().Cmp(bigOne) == 0 {
switch bin.Op {
case ir.Mul, ir.Div:
return lhs, false
case ir.Mod:
return ir.NewIntConst(bigZero, bin.Pos()), false
}
} else if ntz := rhs.Int().TrailingZeroBits(); uint(rhs.Int().BitLen()) == ntz+1 {
var r *big.Int
switch bin.Op {
case ir.Mul:
bin.Op = ir.Shl
r = new(big.Int).SetUint64(uint64(ntz))
case ir.Div:
bin.Op = ir.AShr
r = new(big.Int).SetUint64(uint64(ntz))
case ir.Mod:
bin.Op = ir.And
r = new(big.Int).Sub(rhs.Int(), bigOne)
default:
return nil, false
}
bin.Operand(1).SetDef(ir.NewIntConst(r, bin.Pos()))
// overwrite op
}
case -1:
if rhs.Int().Cmp(bigNegOne) == 0 {
switch bin.Op {
case ir.Mul, ir.Div:
return lhs, true
case ir.Mod:
return ir.NewIntConst(bigZero, bin.Pos()), false
}
}
}
return nil, false
}
func foldBinary(p *ir.Program, bin *ir.BinaryExpr) (ir.Value, bool) {
if bin.Operand(0).Def() == bin.Operand(1).Def() {
switch bin.Op {
case ir.Sub:
return ir.NewIntConst(bigZero, bin.Pos()), false
case ir.Div:
// TODO trap if RHS zero
return ir.NewIntConst(bigOne, bin.Pos()), false
case ir.Mod:
// TODO trap if RHS zero
return ir.NewIntConst(bigZero, bin.Pos()), false
}
}
return nil, false
}