generated from dannypsnl-fork/racket-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir.rkt
190 lines (177 loc) · 6.64 KB
/
ir.rkt
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
#lang nanopass
(define (op? sym)
(member sym '(+ - * / ^ = >= > <= <)))
(define-language IR
(terminals
(symbol (name))
(op (op))
(integer (int)))
(Program (prog)
(p inst* ...))
(Inst (inst)
(assign name expr) => (name := expr)
(assign-op name op expr0 expr1) => (name := expr0 op expr1)
(condbr cond int) => (if cond jump-to int)
(br int) => (jump-to int))
(Expr (expr cond)
name
int))
(define-pass get-leader* : IR (prog) -> * ()
(definitions
; init with 0 since the first instruction is leader
(define leader* '(0)))
(Program : Program (prog) -> * ()
[(p ,inst* ...)
(for ([inst inst*]
[n (length inst*)])
(update-leader inst n))
(reverse leader*)])
; * the instruction after jump instruction is leader
; * the instruction of jump target is leader
(update-leader : Inst (inst n) -> * ()
[(condbr ,cond ,int)
(set! leader* (cons (+ n 1) leader*))
(set! leader* (cons int leader*))]
[(br ,int)
(set! leader* (cons (+ n 1) leader*))
(set! leader* (cons int leader*))]
[else
(void)]))
; IR-BB add BasicBlock and convert program from inst* to basic-block*
(define-language IR-BB
(extends IR)
(Program (prog)
(- (p inst* ...))
(+ (p bb* ...)))
(BasicBlock (bb)
(+ (block inst* ...))))
(define-pass simplify : (IR-BB Inst) (inst) -> (IR-BB Inst) ()
(Inst : Inst (inst) -> Inst ()
[(assign-op ,name ,op ,expr0 ,expr1)
(match* {op expr0 expr1}
[{'+ x 0} `(assign ,name ,x)]
[{'+ 0 x} `(assign ,name ,x)]
[{'- x 0} `(assign ,name ,x)]
[{'* x 1} `(assign ,name ,x)]
[{'* 1 x} `(assign ,name ,x)]
[{'/ x 1} `(assign ,name ,x)]
; reduction in strength
[{'^ x 2} `(assign-op ,name * ,x ,x)]
[{'* x 2} `(assign-op ,name + ,x ,x)]
[{'* 2 x} `(assign-op ,name + ,x ,x)]
[{_ _ _} inst])]))
(define-pass IR->IR-BB : IR (prog leader*) -> IR-BB ()
(definitions
(define basic-block* '())
(define cur-block '()))
(Program : Program (prog) -> Program ()
[(p ,inst* ...)
(for ([inst inst*]
[n (length inst*)])
(define cur-inst (list (simplify (inst-IR->BBIR inst))))
(if (member n leader*)
(if (empty? cur-block)
(set! cur-block cur-inst)
(set! basic-block* (append basic-block* (list cur-block))))
(set! cur-block (append cur-block cur-inst))))
(set! basic-block* (append basic-block* (list cur-block)))
`(p ,(map inst*->block basic-block*) ...)])
(inst*->block : * (inst*) -> BasicBlock ()
`(block ,inst* ...))
(inst-IR->BBIR : Inst (inst) -> Inst ()))
(define-pass liveness-map : IR-BB (bb) -> * ()
(basic-block : BasicBlock (bb) -> * ()
[(block ,inst* ...)
(define mark* (make-hash))
(define symbol* (make-hash))
(for ([inst (reverse inst*)])
(mark-inst inst mark* symbol*))
mark*])
(mark-inst : Inst (inst mark* symbol*) -> * ()
[(assign ,name ,expr)
(hash-set! mark* inst (hash-copy symbol*))
(hash-set! symbol* name '(inactive dead))
(mark-use expr symbol*)]
[(assign-op ,name ,op ,expr0 ,expr1)
(hash-set! mark* inst (hash-copy symbol*))
(hash-set! symbol* name '(inactive dead))
(mark-use expr0 symbol*)
(mark-use expr1 symbol*)]
[else (void)])
(mark-use : Expr (expr symbol*) -> * ()
[,name
(hash-set! symbol* name 'active)]
[else (void)])
(basic-block bb))
(struct node (name node)
#:transparent)
(define-pass basic-block->DAG : IR-BB (bb) -> * ()
(basic-block : BasicBlock (bb) -> * ()
[(block ,inst* ...)
(define name=>graph (make-hash))
(map (λ (inst)
(inst->node inst name=>graph))
inst*)])
(inst->node : Inst (inst name=>graph) -> * ()
[(assign ,name ,expr)
(define g (node name (expr->node expr name=>graph)))
(hash-set! name=>graph name g)
g]
[(assign-op ,name ,op ,expr0 ,expr1)
(define g (node name `(,op ,(expr->node expr0 name=>graph) ,(expr->node expr1 name=>graph))))
(hash-set! name=>graph name g)
g]
[(condbr ,cond ,int)
`(condbr ,(expr->node cond name=>graph) ,int)]
[(br ,int)
inst])
(expr->node : Expr (expr name=>graph) -> * ()
[,name (if (hash-ref name=>graph name #f)
(hash-ref name=>graph name)
(let ([val (gensym name)])
(hash-set! name=>graph name val)
val))]
[,int int])
(basic-block bb))
(define-pass bb*->DAG* : IR-BB (prog) -> * ()
(Prog : Program (prog) -> * ()
[(p ,bb* ...)
(define inst=>liveness (map liveness-map bb*))
(for/list ([basic-block bb*])
(basic-block->DAG basic-block))]))
(define (all prog)
(define-parser parse IR)
(define p (parse prog))
(define leader* (get-leader* p))
(define block* (IR->IR-BB p leader*))
(bb*->DAG* block*))
(all '(p (assign a 1) ;0
(assign b 2) ;1
(assign-op c + a b) ;2
(assign-op d = c 3) ;3
(condbr d 6) ;4, jump to 6 is c=3
(assign c a) ;5
(assign c b) ;6
(assign-op c + b 0) ; 7
))
(module+ test
(require rackunit)
(with-output-language (IR-BB Inst)
(check-equal? (simplify `(assign-op a + x 0))
`(assign a x))
(check-equal? (simplify `(assign-op a + 0 x))
`(assign a x))
(check-equal? (simplify `(assign-op a - x 0))
`(assign a x))
(check-equal? (simplify `(assign-op a * x 1))
`(assign a x))
(check-equal? (simplify `(assign-op a * 1 x))
`(assign a x))
(check-equal? (simplify `(assign-op a / x 1))
`(assign a x))
(check-equal? (simplify `(assign-op a ^ x 2))
`(assign-op a * x x))
(check-equal? (simplify `(assign-op a * x 2))
`(assign-op a + x x))
(check-equal? (simplify `(assign-op a * 2 x))
`(assign-op a + x x))))