1 ;; address after which memory is allocated by the user, therefore not used for
3 ;; in programs, located in the SIXPIC_MEMORY_DIVIDE variable
4 (define memory-divide #f)
6 (define (interference-graph cfg)
8 (define (analyze-liveness cfg)
10 (define (instr-analyze-liveness instr live-after)
14 (let ((def-proc (call-instr-def-proc instr)))
15 (if (not (set-empty? live-after))
18 (set-union! (def-procedure-live-after-calls def-proc)
23 (map (lambda (def-var)
25 (value-bytes (def-variable-value def-var))))
26 (def-procedure-params def-proc)))
30 (value-bytes (def-procedure-value def-proc)))))))
31 (if (bb? (def-procedure-entry def-proc))
33 (bb-live-before (def-procedure-entry def-proc))
36 ((return-instr? instr)
37 (let ((def-proc (return-instr-def-proc instr)))
39 (if (def-procedure? def-proc)
40 (def-procedure-live-after-calls def-proc)
41 (list->set (value-bytes def-proc)))))
42 (set! live-after live)
45 (let* ((src1 (instr-src1 instr))
46 (src2 (instr-src2 instr))
47 (dst (instr-dst instr))
48 (use (if (byte-cell? src1)
50 (set-add (new-set src1) src2)
55 (def (if (byte-cell? dst)
59 ;; (and (byte-cell? dst) ; dead instruction?
60 ;; (not (set-member? live-after dst))
61 ;; (not (and (byte-cell? dst) (byte-cell-adr dst))))
64 (set-diff live-after def))))))))
65 (instr-live-before-set! instr live-before)
66 (instr-live-after-set! instr live-after)
68 (define (bb-analyze-liveness bb)
69 (let loop ((rev-instrs (bb-rev-instrs bb))
70 (live-after (set-union-multi
71 (map bb-live-before (bb-succs bb)))))
72 (if (null? rev-instrs)
73 (if (not (set-equal? live-after (bb-live-before bb)))
74 (begin (set! changed? #t)
75 (bb-live-before-set! bb live-after)))
76 (let* ((instr (car rev-instrs))
77 (live-before (instr-analyze-liveness instr live-after)))
78 (loop (cdr rev-instrs)
82 (begin (set! changed? #f)
83 (for-each bb-analyze-liveness (cfg-bbs cfg))
86 (define all-live (new-empty-set))
87 (define (bb-interference-graph bb)
88 (define (interfere x y)
89 (if (not (set-member? (byte-cell-interferes-with y) x))
90 (begin (set-add! (byte-cell-interferes-with x) y)
91 (set-add! (byte-cell-interferes-with y) x))))
92 (define (interfere-pairwise live)
93 (set-union! all-live live) ;; TODO build the live set only once, if not a set already
94 (set-for-each ;; TODO for each cell in live, do the union of live, diff itself, if live is not a set, we win so we can iterate on something better than a hash table
95 ;; TODO since all true variables will be in the low numbers and all temps in the high numbers, we can have sparse bit vectors (if all below n is 0, don't store it) and save (or even have a bit vector that can store data by chunks, leaving empty space)
97 (set-for-each (lambda (y) (if (not (eq? x y)) (interfere x y)))
100 (define (instr-interference-graph instr)
101 (let ((dst (instr-dst instr)))
103 (let ((src1 (instr-src1 instr))
104 (src2 (instr-src2 instr)))
105 (if (byte-cell? src1)
106 (begin (set-add! (byte-cell-coalesceable-with dst) src1)
107 (set-add! (byte-cell-coalesceable-with src1) dst)))
108 (if (byte-cell? src2)
109 (begin (set-add! (byte-cell-coalesceable-with dst) src2)
110 (set-add! (byte-cell-coalesceable-with src2) dst)))
111 (interfere-pairwise (set-add (instr-live-after instr) dst)))))
112 (interfere-pairwise (instr-live-before instr)))
113 (for-each instr-interference-graph (bb-rev-instrs bb)))
115 (pp analyse-liveness:)
116 (time (analyze-liveness cfg))
118 (pp interference-graph:)
119 (time (for-each bb-interference-graph (cfg-bbs cfg)))
123 ;;-----------------------------------------------------------------------------
125 (define (allocate-registers cfg)
126 (let ((all-live (interference-graph cfg))
127 (max-adr 0)) ; to know how much ram we need
129 (define (color byte-cell)
130 (let (;; (coalesce-candidates ; TODO right now, no coalescing is done
131 ;; (set-filter byte-cell-adr
132 ;; (set-diff (byte-cell-coalesceable-with byte-cell)
133 ;; (byte-cell-interferes-with byte-cell))))
136 ;; (pp (list byte-cell: byte-cell;;;;;;;;;;;;;;;
137 ;; coalesce-candidates
138 ;; ; interferes-with: (byte-cell-interferes-with byte-cell)
139 ;; ; coalesceable-with: (byte-cell-coalesceable-with byte-cell)
142 (if #f #;(not (null? coalesce-candidates))
143 (let ((adr (byte-cell-adr (car (set->list coalesce-candidates))))) ;; TODO have as a set all along
144 (byte-cell-adr-set! byte-cell adr))
145 (let ((neighbours (byte-cell-interferes-with byte-cell)))
147 (if (and memory-divide ; the user wants his own zone
148 (>= adr memory-divide)) ; and we'd use it
149 (error "register allocation would cross the memory divide") ;; TODO fallback ?
150 (let loop2 ((lst (set->list neighbours))) ;; TODO keep using sets, but not urgent, it's not a bottleneck
152 (byte-cell-adr-set! byte-cell adr)
154 (if (= adr (byte-cell-adr x))
156 (loop2 (cdr lst)))))))
157 (set! max-adr (max max-adr adr)))))))
159 (define (delete byte-cell1 neighbours)
160 (set-for-each (lambda (byte-cell2)
161 (set-remove! (byte-cell-interferes-with byte-cell2)
165 (define (undelete byte-cell1 neighbours)
166 (set-for-each (lambda (byte-cell2)
167 (set-add! (byte-cell-interferes-with byte-cell2)
171 (define (find-min-neighbours graph)
172 (let loop ((lst graph) (m #f) (byte-cell #f))
176 (n (set-length (byte-cell-interferes-with x))))
177 (if (or (not m) (< n m))
179 (loop (cdr lst) m byte-cell))))))
181 (define (alloc-reg graph)
182 (if (not (null? graph))
183 (let* ((byte-cell (find-min-neighbours graph))
184 (neighbours (byte-cell-interferes-with byte-cell)))
185 (let ((new-graph (remove byte-cell graph)))
186 (delete byte-cell neighbours)
187 (alloc-reg new-graph)
188 (undelete byte-cell neighbours))
189 (if (not (byte-cell-adr byte-cell))
190 (color byte-cell)))))
192 (pp register-allocation:)
193 (time (alloc-reg (set->list all-live))) ;; TODO convert find-min-neighbors and alloc-reg to use tables, not urgent since it's not a bottleneck
194 (display (string-append (number->string (+ max-adr 1)) " RAM bytes\n"))))