File: ra-iteratedCoalescing.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (796 lines) | stat: -rw-r--r-- 34,905 bytes parent folder | download | duplicates (5)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
(** Graph coloring register allocation. graph
 ** Implements the 'iterated register coalescing' scheme described 
 ** in POPL'96, and TOPLAS v18 #3, pp 325-353. 
 **)
(*
 * This is a reorganization of the old iterated coalescing 
 * register allocator using a more modular implementation.
 * 
 * --- Allen
 *)
functor OldRegAllocator
    (structure RaArch : RA_ARCH_PARAMS)
    (structure RaUser : RA_USER_PARAMS
       where I = RaArch.I
       where B = RaArch.Liveness.F.B
    ) : RA =
struct

   structure F = RaArch.Liveness.F
   structure Core = OldRACore
   structure G = Core.G
   structure A = Array
   structure I = RaArch.I
   structure C = I.C
   structure P = RaArch.InsnProps
   structure SL = SortedList

   datatype mode = REGISTER_ALLOCATION | COPY_PROPAGATION

   open G

   fun error msg = MLRiscErrorMsg.error("IteratedCoalescing",msg)

   (*
    * Debugging flags
    *)
   val cfg_before_ra   = MLRiscControl.getFlag "dump-cfg-before-ra"
   val cfg_after_ra    = MLRiscControl.getFlag "dump-cfg-after-ra"
   val cfg_after_spill = MLRiscControl.getFlag "dump-cfg-after-spilling"
   val dump_graph      = MLRiscControl.getFlag "dump-interference-graph"
   val ra_count        = MLRiscControl.getCounter "ra-count"
   val rewrite         = MLRiscControl.getCounter "ra-rewrites"

   (*  
    * Set of dedicated registers.
    * Note: I'm using an array for testing for dedicated registers.
    * Hopefully this is now a bit faster than before. -- Allen
    *)
   val spillRegSentinel = ~1 (* what is this? *)
   val dedicated     = SL.uniq(RaUser.dedicated)
   val firstPseudoR  = RaArch.firstPseudoR
   val dedicatedRegs = A.array(firstPseudoR,false)
   val _  = app (fn r => if r >= 0 andalso r < firstPseudoR then
                         A.update(dedicatedRegs,r,true) else ()) dedicated
   fun isDedicated r = r < 0 orelse r < firstPseudoR andalso 
                                    Unsafe.Array.sub(dedicatedRegs,r) 
   (* Note: The following is no long necessary!
    * Note: This function maintains the order of members in rset
    * which is important when dealing with parallel copies.
    *)
   fun rmvDedicated rset = 
   let fun loop(x::xs, xs') = loop(xs, if isDedicated x then xs' else x::xs')
         | loop([], xs') = xs'
   in  loop(rset,[])
   end


   (* register mapping functions *)
   fun uniqMap(f, l) = SL.uniq(map f l)

   fun prList (l:int list,msg:string) = 
   let fun pr [] = print "\n"
         | pr (x::xs) = (print (Int.toString x ^ " "); pr xs)
   in  print msg; pr l
   end

   (* debugging *)
   fun printBlocks(blks, regmap, annotations) = 
   let val regmap = C.lookup regmap
       val RaArch.AsmEmitter.S.STREAM{emit,...} =
              AsmStream.withStream TextIO.stdOut 
                 RaArch.AsmEmitter.makeStream annotations
        val emit = emit regmap
        fun prBlks([]) = print"\n"
          | prBlks(F.BBLOCK{blknum,insns,liveOut,liveIn,
                            succ,pred,...}::blocks)=
            let
              fun regset cellset = map regmap (RaArch.regSet(cellset))
              fun pr [] = prList(regset(!liveOut), "liveOut: ")
                | pr (instr::rest) = (emit instr; pr rest)
              fun blkNum(F.BBLOCK{blknum, ...},_) = blknum
                | blkNum(F.ENTRY{blknum, ...},_) = blknum
                | blkNum(F.EXIT{blknum, ...},_) = blknum
                | blkNum _ = error "printBlocks.prBlks.blkNum"
            in
              print("BLOCK" ^ Int.toString blknum ^ "\n");
              prList(regset (!liveIn), "LiveIn :");
              prList(map blkNum (!pred),"predecessors: ");
              case !insns of [] => print "empty instruction sequence\n"
                           |  l  => pr(rev l)
              (*esac*);
              prList(map blkNum (!succ),"successors: ");
              prBlks(blocks)
            end
          | prBlks(F.LABEL lab::blocks) =
              (print(Label.nameOf lab^":\n"); prBlks(blocks))
          | prBlks(F.PSEUDO pOp::blocks) = (print (F.P.toString pOp); prBlks(blocks))
          | prBlks(_::blocks) = prBlks(blocks)
   in prBlks blks
   end

   fun debug(flag, msg, blocks, regmap, annotations) =
   if !flag then
      (print ("------------------" ^ msg ^ " ----------------\n");
       printBlocks(blocks,regmap,annotations))
   else ()


   (* Utility functions *)
   fun newNode(num, col) =
       NODE{number=num,
            color=ref col,
            degree=ref 0,
            adj=ref [],
            movecnt = ref 0,
            movelist = ref []}

   fun nodeNumber(NODE{number, ...}) = number

   fun chase(NODE{color=ref(ALIASED r), ...}) = chase r
     | chase x = x

   fun nodeMember(_, []) = false
     | nodeMember(node as NODE{number=x, ...}, NODE{number=y,...}::rest) =
        x = y orelse nodeMember(node, rest)


   fun isMoveRelated(NODE{movecnt=ref 0, ...}) = false
     | isMoveRelated _ = true

   exception PrevSpills 
   val prevSpills      = Intmap.new(32,PrevSpills) : bool Intmap.intmap
   val isSpilled       = Intmap.mapWithDefault(prevSpills,false)
   val enterSpilled    = Intmap.add prevSpills 
   fun markAsSpilled r = enterSpilled(r,true)

   (* 
    * This is the new register allocator!
    *)
   fun ra mode prohibit 
       (cluster as F.CLUSTER{regmap,blocks,annotations=ref an,...}) =
   if RaArch.numRegs() = 0 then cluster
   else
   let
       val _ = Intmap.clear prevSpills
       val _ = app (fn (i,j) => 
                    let fun loop(i) = 
                            if i <= j then (markAsSpilled i; loop(i+1)) else ()
                    in  loop i end) prohibit

          (* number of blocks *)
       val numBlocks = foldr (fn (F.BBLOCK _,n) => n + 1 | (_,n) => n) 0 blocks

       val blockDU = A.array(numBlocks,[] : (node list * node list) list)
       val cblocks = A.array(numBlocks,F.LABEL(Label.newLabel ""))
       val numOfBlocks = A.length cblocks

           (* remainInfo: blocks where spill nodes are defined and used. *)
       type info  = int list Intmap.intmap
       val remainInfo : (info * info) option ref   = ref NONE

       fun cleanupSpillInfo() = remainInfo := NONE

       (** 
        ** Build blockDU and cblocks.
        ** This is done once per RA.
        **)
       fun initialize() = 
       let val nodes = Intmap.new(32,Nodes)
           fun mkNode i =
                newNode(i, if i < firstPseudoR then COLORED(i) else PSEUDO)
           val lookupNodes = Intmap.map nodes
           val enterNodes = Intmap.add nodes
           fun newnode n =
                 lookupNodes n
                 handle _ =>
                   let val node = mkNode n
                   in  enterNodes (n, node); node
                   end
           fun blockDefUse((b as F.BBLOCK{insns,liveOut,succ, ...})::blks, n) = 
               let fun insnDefUse insn = 
                   let val (d,u) = RaArch.defUse insn
                       fun rmv [] = []
                         | rmv (l as [x]) =
                             if isDedicated x then [] else [newnode x]
                         | rmv set = map newnode (rmvDedicated set)
                   in (rmv d, rmv u) end
               in  Unsafe.Array.update(cblocks, n, b);
                   Unsafe.Array.update(blockDU, n, map insnDefUse (!insns));
                   case !succ of 
                      [(F.EXIT _,_)] =>
                         app (fn i => (newnode i; ()))
                             (rmvDedicated(RaArch.regSet(!liveOut)))
                     | _  => ();
                   blockDefUse(blks, n+1)
               end
             | blockDefUse(_::blks, n) = blockDefUse(blks, n)
             | blockDefUse([], _) = ()

            (* if copy propagation was done prior to register allocation
             * then some nodes may already be aliased. 
             *)
           fun updateAliases() = 
           let val alias = Intmap.mapInt regmap
               fun fixup(num, NODE{color, ...}) =
                   if num < firstPseudoR then ()
                   else let val reg = alias num
                        in  if reg=num then () else 
                            color := ALIASED(newnode reg)
                        end 
           in  Intmap.app fixup nodes end
       in  blockDefUse(blocks,0);
           updateAliases();
           nodes
       end

       (** 
        ** Run liveness analysis 
        **)
       fun liveness(nodes,blocks) = 
       let val getnode = Intmap.map nodes
           fun regmap i = 
           let val node = getnode i
           in  case node
                of NODE{color= ref (COLORED r), ...} => r
                 | NODE{color=ref PSEUDO, ...} => nodeNumber node
                 | NODE{color=ref(ALIASED r), ...} => nodeNumber(chase node)
                 | _ => error "liveness.regmap"
           end handle _ => i                 (* XXX *)
       in  RaArch.Liveness.liveness(blocks, regmap)
       end

       (* 
        * Given a set of registers, remove all spilled and dedicated nodes.
        * NOTE: we assume that dedicated registers are NEVER entered into
        *       nodes Intmap. 
        *)
       fun collectNodes(getnode,regs) =  
       let fun loop([],xs) = xs
             | loop(r::rs,xs) = 
               (case chase(getnode r) of
                  NODE{color=ref(COLORED ~1),...} => loop(rs,xs) 
                | x => loop(rs,x::xs)
               ) handle _ => loop(rs,xs) (* dedicated *)
       in  loop(regs,[]) end


       (** 
        ** Builds the interference graph and initialMove list 
        **)
       fun build(graph as GRAPH{bitMatrix,nodes,...}) = 
       let (* The movecnt field is used to (lazily) record members in the 
            * live set. Deleted members are removed during an 
            * addEdgeForallLive operation.
            *)
           val getnode = Intmap.map nodes
           val chaseReg = chase o getnode
           val chaseRegs = map chaseReg
           val addEdge = Core.addEdge graph
           val member = BM.member bitMatrix
           fun memBitMatrix(NODE{number=x,...}, NODE{number=y,...}) =
               member (if x<y then (x, y) else (y, x))

           fun delete(NODE{movecnt, ...}) = movecnt:=0
           fun insert((node as NODE{movecnt as ref 0, ...})::rest, live) = 
                (movecnt:=1; insert(rest, node::live))
             | insert(_::rest, live) = insert(rest, live)
             | insert([], live) = live
           fun addEdgeForallLive([], live) = live
             | addEdgeForallLive(d::ds, live) = 
               let fun f ([], pruned) = pruned
                     | f ((n as NODE{movecnt as ref 1, ...})::rest, pruned) =
                         (addEdge(d, n); f(rest, n::pruned))
                     | f (_::rest, pruned) = f(rest, pruned)
               in addEdgeForallLive(ds, f(live, []))
               end
           fun forallBlocks(~1, mvs) = mvs
             | forallBlocks(n, mvs) = 
               let val F.BBLOCK{insns, liveOut, ...} = A.sub(cblocks, n)
                   val bdu = A.sub(blockDU, n)
                   fun doBlock([], _, live, mvs) = 
                         (app (fn NODE{movecnt, ...} => movecnt := 0) live;
                          forallBlocks(n-1, mvs))
                     | doBlock(instr::rest, (def',use')::bdu, live', mvs) = 
                       let val def = map chase def'
                           val use = map chase use'
                          (* move instructions are treated specially *)
                          (* There  is a subtle interaction between parallel
                              moves and interference graph construction. When we
                              have {d1, ... dn} <- {s1, ... sn} and liveOut we 
                              should make di interfere with:
        
                                  liveOut U {d1, ... dn} U ({s1, ... sn} \ {si})

                              This is not currently done.
                           *)
                          fun zip(d::defs, u::uses) = 
                              if isDedicated d orelse 
                                 isDedicated u then zip(defs, uses) 
                              else 
                              let val d as NODE{number=x,...} = chaseReg d
                                  val u as NODE{number=y,...} = chaseReg u 
                              in  if x = y then zip(defs,uses)
                                  else
                                   MV{dst=d, src=u, status=ref WORKLIST}::
                                    zip(defs, uses)
                              end 
                            | zip([],[]) = mvs

                         (* Assumes that the move temporary 
                          * if present is always the
                          * first thing on the definition list.
                          *)
                         val moves = 
                           if P.moveInstr instr then 
                           let val (defs,uses) = RaArch.defUse instr
                               val defs =
                                   case defs of 
                                     [] => []
                                   | _::rest => case P.moveTmpR instr of
                                                   SOME _ => rest
                                                |  NONE => defs
                           in  zip(defs,uses)
                           end
                           else mvs
                        val live = 
                           if length def > 1 then
                             addEdgeForallLive(def, insert(def, live'))
                           else addEdgeForallLive(def, live')
                     in app delete def;
                        doBlock(rest, bdu, insert(use,live), moves)
                     end  
                   val lout = collectNodes(getnode,RaArch.regSet(!liveOut))
                in doBlock(!insns, bdu, insert(lout, []), mvs)
                end
             (* Filter moves that already have an interference.
              * Also initialize the movelist and movecnt fields at this time.
              *)
             fun filter [] = []
               | filter (MV{src=NODE{color=ref(COLORED _), ...}, 
                            dst=NODE{color=ref(COLORED _), ...}, ...}::rest) = 
                   filter rest
               | filter ((mv as MV{src, dst, ...})::rest) = 
                 if memBitMatrix(src, dst) then filter rest
                 else let 
                  fun info(u as NODE{color=ref PSEUDO, movecnt, movelist,...}) =
                      (movelist := mv :: !movelist;   movecnt := 1 + !movecnt)
                    | info _ = ()
                in info src;  info dst;  mv::filter rest
                end
        in filter(forallBlocks(numOfBlocks-1, []))
        end (* build *)

        (** 
         ** select a spill node 
         **)
        fun selectSpill (GRAPH{nodes,spillFlag,K,...}, 
              {simplifyWkl, spillWkl, stack, moveWkl, freezeWkl}) =
        let (* duCount: compute the def/use points of spilled nodes. *)
            val getnode  = Intmap.map nodes
            val chaseReg = chase o getnode
            fun duCount spillable = 
            let val size = length spillable
                exception Info
                val defInfo : info = Intmap.new(size,Info)
                val useInfo : info = Intmap.new(size,Info)
                val addDef = Intmap.add defInfo 
                val addUse = Intmap.add useInfo
                val getDefs = Intmap.mapWithDefault (defInfo,[]) 
                val getUses = Intmap.mapWithDefault (useInfo,[])

                (* doblocks --- 
                 * updates the defInfo and useInfo tables to indicate
                 * the blocks where spillable live ranges are defined and used.
                 *)
                fun doblocks ~1 = ()
                  | doblocks blknum = 
                    let val bdu = A.sub(blockDU,blknum)
                        fun iter [] = ()
                          | iter((def',use')::rest) = 
                            let val def = uniqMap(nodeNumber o chase, def')
                                val use = uniqMap(nodeNumber o chase, use')
                                fun updateDef n = addDef(n, blknum::getDefs n)
                                fun updateUse n = addUse(n, blknum::getUses n)
                            in  app updateDef (SL.intersect(def,spillable));  
                                app updateUse (SL.intersect(use,spillable));   
                                iter rest
                            end
                    in iter(bdu);
                       doblocks(blknum-1)
                    end

                (* If a node is live going out of an block terminated by 
                 * an escaping branch, it may be necessary to reload the
                 * the node just prior to taking the branch. We will therefore
                 * record this as a definition of the node.
                 *)
                fun doBBlocks n = 
                let val F.BBLOCK{blknum,liveIn,liveOut,succ,...} = 
                         A.sub(cblocks,n)
                    val liveout = 
                     uniqMap (nodeNumber,
                               collectNodes(getnode,RaArch.regSet(!liveOut)))
                in  case !succ of 
                      [(F.EXIT _,_)] => 
                       (case SL.intersect(spillable,liveout) 
                          of [] => doBBlocks(n+1)
                           | some =>
                             (app (fn n => addDef(n, blknum::getDefs n)) some;
                              doBBlocks (n+1))
                       (*esac*))
                    | _ => doBBlocks(n+1)
                    (*esac*)
                end (* doBBlocks *) 
            in  doblocks (numOfBlocks - 1);
                doBBlocks 0 handle _ => ();
                (defInfo,useInfo)
            end (* duCount *)

            (* Since the spillWkl is not actively maintained, the set of
             * spillable nodes for which def/use info is needed is a subset
             * of spillWkl.
             *)
            fun remainingNodes() = 
            let fun prune [] = []
                  | prune((n as NODE{color=ref PSEUDO, ...}) ::ns) =  
                      n::prune ns
                  | prune((n as NODE{color=ref(ALIASED _), ...})::ns) = 
                      prune(chase n::ns)
                  | prune(_::ns) = prune ns
            in  case !remainInfo of 
                  SOME info => prune spillWkl
                | NONE => 
                  let (* first time spilling *)
                      val spillable = prune ( spillWkl)
                  in remainInfo := 
                       (case spillable 
                         of [] => NONE
                          | _ => SOME(duCount(uniqMap(nodeNumber, spillable)))
                        (*esac*));
                     spillable
                  end
            end

            (** apply the Chaitin heuristic to find the spill node **)
            fun chaitinHeuristic(spillable) = 
            let val infinity = 1000000.0
                val infinityi= 1000000
                val SOME(dinfo,uinfo) = !remainInfo
                val getdInfo = Intmap.map dinfo
                val getuInfo = Intmap.map uinfo
                fun coreDump [] = ()
                  | coreDump ((node as NODE{number, degree, adj, ...})::rest) = 
                    (print(concat
                      ["number =", Int.toString number,
                       " node =", Int.toString(nodeNumber (chase node)),
                       " degree = ", Int.toString (!degree),
                       " adj = "]);
                     prList(map (nodeNumber o chase) (!adj), "");
                     print "\n";
                     coreDump rest)
                fun iter([],node,cmin) = 
                       if node <> ~1 then 
                          (if !cfg_after_spill then 
                              print("Spilling node "^Int.toString node^
                                    " cost="^Real.toString cmin^"\n") else ();
                           getnode node
                          ) 
                       else (coreDump spillable; 
                             prList(Intmap.keys prevSpills,"PrevSpills: ");
                             error "chaitinHeuristic.iter")
                  | iter((node as NODE{number, degree, ...})::rest,cnode,cmin) =
                    let
                       (* An exeception will be raised if the node is defined
                        * but not used. This is not a suitable node to spill.
                        *)
                        val cost = 
                          (length(getdInfo number) handle _ => 0) +
                              (length(getuInfo number) handle _ => infinityi)
                        val heuristic = real cost / real (!degree)
                    in
                        if heuristic < cmin andalso not(isSpilled number)
                        then iter(rest, number, heuristic)
                        else iter(rest, cnode, cmin)
                    end
            in iter(spillable, ~1, infinity)
            end
       in case mode of 
            COPY_PROPAGATION =>
              {spillWkl=[], simplifyWkl=[], stack=[], moveWkl=[], freezeWkl=[]}
          | REGISTER_ALLOCATION => 
            (case remainingNodes() of 
               [] => {spillWkl=[], simplifyWkl=simplifyWkl, 
                      stack=stack, moveWkl=moveWkl, freezeWkl=freezeWkl}
             | spillWkl => 
               let val spillNode = chaitinHeuristic(spillWkl)
                   val simpWkl = 
                       if isMoveRelated spillNode
                       then spillNode::Core.wklFromFrozen(K,spillNode)
                       else [spillNode]
               in  spillFlag:=true;
                   {simplifyWkl=simpWkl,
                    spillWkl = spillWkl,
                    freezeWkl = freezeWkl,
                    stack = stack,
                    moveWkl = moveWkl}
               end
               (*esac*))
       end (* selectSpill *)


       (** rewriteGraph(spillList) - 
        **   an unsuccessful round of coloring has taken
        **   place with nodes in spillList having been spilled. The
        **   flowgraph must be updated and the entire process repeated. 
        **)
       fun rewriteGraph (graph as GRAPH{nodes,...}, spillList) = 
       let val _       = rewrite := !rewrite + 1
           val SOME(dInfo,uInfo) = !remainInfo
           val getnode = Intmap.map nodes
           val enternode = Intmap.add nodes
           val chaseReg = chase o getnode
           val chaseRegs = map chaseReg
          
           fun newdu (d, u) = 
           let fun rmv([],nodes) = nodes
                 | rmv(r::rs,nodes) = 
                   let val node = chase(getnode r) handle _ => 
                                  let val n = newNode(r, PSEUDO)
                                  in  enternode (r, n); n
                                  end
                   in rmv(rs,node::nodes) end
               fun rmv' rs = rmv(rmvDedicated rs,[])
           in (rmv' d, rmv' u)
           end (* newdu *)

           val defUse = newdu o RaArch.defUse

           (* blocks where spill code is required for node n *)
           fun affectedBlocks node = 
           let val n = nodeNumber node
           in  SL.merge(SL.uniq(Intmap.mapWithDefault (dInfo,[]) n),
                        SL.uniq(Intmap.mapWithDefault (uInfo,[]) n))
           end

           val mapr = C.lookup regmap
           val markProh = app markAsSpilled

           (* Insert spill code into the affected blocks *)
           fun doBlocks([], _) = ()
             | doBlocks(blknum::rest, node) = 
           let val F.BBLOCK{insns, liveOut, name, ...} = 
                     A.sub(cblocks, blknum)
               val bdu = A.sub(blockDU, blknum)
               val liveOut = collectNodes(getnode,RaArch.regSet(!liveOut))
               val spillReg = nodeNumber node

               (* note: the instruction list start out in reverse order. *)
               fun doInstrs([], [], newI, newBDU) =
                     (rev newI, rev newBDU)
                 | doInstrs(instr::rest, (du as (d,u))::bDU, newI, newBDU) = 
                   let val defs=map chase d
                       val uses=map chase u

                       fun outputInstrs(instrs, I, bDU) = 
                           {newI=instrs @ I, 
                            newBDU=(map defUse instrs) @ bDU}

                       fun newReloadCopy(rds, rss) = 
                       let fun f(rd::rds, rs::rss, rds', rss') = 
                               if mapr rs = spillReg 
                               then(([rd], [rs]), (rds@rds', rss@rss'))
                               else f(rds, rss, rd::rds', rs::rss')
                             | f([], [], _, _) = error "newReloadCopy.f"
                       in f(rds, rss, [], []) end
            
                       (* insert reloading code and continue *)
                       fun reloadInstr(instr, du, newI, newBDU)=
                       let val {code, proh} = 
                               RaUser.reload{regmap=mapr, instr=instr, 
                                             reg=spillReg, id=name}
                           val _ = markProh proh
                           val {newI, newBDU} = 
                               outputInstrs(code, newI, newBDU)
                       in doInstrs(rest, bDU, newI, newBDU) end

                       (* insert reload code for copies. *)
                       fun reloadCopy(du, instr, newI, newBDU) =
                           if nodeMember(node, #2 du) then 
                           (case (P.moveDstSrc(instr)) 
                            of ([d], [u]) => 
                              reloadInstr(instr,du,newI,newBDU)
                            | (defs, uses) => 
                              let val (mv, cpy) = newReloadCopy(defs, uses)
                                  val cpyInstr = RaUser.copyInstr(cpy, instr)
                                  val duCpy = defUse cpyInstr
                                  val {code, proh} =
                                    RaUser.reload
                                     {regmap=mapr, 
                                      instr=RaUser.copyInstr(mv, instr), 
                                      reg=spillReg, id=name}
                                  val _ = markProh proh
                                  val {newI, newBDU} = 
                                      outputInstrs(code, newI, newBDU)
                             in  (* recurse to deal with multiple uses *)
                                 reloadCopy(duCpy, cpyInstr, newI, newBDU) 
                             end
                             (*esac*))
                          else
                             doInstrs(rest, bDU, instr::newI, du::newBDU)

                      (* insert reload code *)
                      fun reload(du as (d,u), instr, newI, newBDU) = 
                        if P.moveInstr(instr) then 
                           reloadCopy(du, instr, newI, newBDU)
                        else if nodeMember(node, u) then 
                          let val {code, proh} = 
                                RaUser.reload{regmap=mapr, instr=instr, 
                                              reg=spillReg, id=name}
                              val {newI, newBDU} = 
                                     outputInstrs(code, newI, newBDU)
                              val _ = markProh proh
                          in doInstrs(rest, bDU, newI, newBDU)
                          end
                       else
                         doInstrs(rest, bDU, instr::newI, du::newBDU)


                      fun spillInstr(instr, newI, newBDU) = 
                      let val {code, instr, proh} = 
                           RaUser.spill{regmap=mapr,  instr=instr, reg=spillReg, id=name}
                          val _ = markProh proh
                          val {newI, newBDU} = outputInstrs(code, newI, newBDU)
                      in case instr
                          of NONE => doInstrs(rest, bDU, newI, newBDU)
                           | SOME instr => 
                               reload(defUse instr, instr, newI, newBDU)
                      end
            
                      fun spillCopy() = 
                      let (* Note:: There is a guarantee that the node 
                           * will never be aliased to another register.
                           *)
                          fun newSpillCopy(rds, rss) = 
                          let fun f(rd::rds, rs::rss, rds', rss') = 
                              if mapr rd = spillReg then 
                                 (([rd], [rs]), (rds@rds', rss@rss'))
                              else f(rds, rss, rd::rds', rs::rss')
                                | f([], [], _, _) = error "newSpillCopy"
                          in f(rds, rss, [], []) end

                          fun spillCpyDst() = 
                          let val (mv, cpy) = newSpillCopy(P.moveDstSrc(instr))
                              val (newI, newBDU) = 
                              (case cpy
                               of ([],[]) => (newI, newBDU)
                                | _ => let val cpyInstr = RaUser.copyInstr(cpy, instr)
                                       in (cpyInstr::newI, defUse cpyInstr::newBDU)
                                       end
                              (*esac*))
                              val instr = RaUser.copyInstr(mv, instr)
                          in spillInstr(instr, newI, newBDU)
                          end
                      in case P.moveTmpR instr
                          of NONE => spillCpyDst()
                           | SOME r => 
                             if mapr r=spillReg 
                             then spillInstr(instr, newI, newBDU)
                             else spillCpyDst()
                         (*esac*)
                      end (* spillCopy *)
                   in (* insert spill code *)
                      if nodeMember(node, defs) then 
                         if P.moveInstr instr then spillCopy() 
                         else spillInstr(instr, newI, newBDU)
                      else
                        reload((defs,uses), instr, newI, newBDU)
                   end

               (* special action if the last instruction is an escaping
                * branch and the node is live across the branch.
                * We discover if the node needs to be spilled or reloaded.
                *)
               fun blockEnd(instrs as instr::rest, bDU as du::bdu) = 
               let fun escapes [] = false
                     | escapes (P.ESCAPES::_) = true
                     | escapes (_::targets) = escapes targets
               in if nodeMember(node, liveOut) then
                  (case P.instrKind instr
                   of P.IK_JUMP =>
                       if escapes(P.branchTargets instr) then let
                           val {code,...} = 
                             RaUser.reload{regmap=mapr, instr=instr, reg=spillReg, id=name}
                           val reloadDU = map defUse code
                         in (rev code@rest, rev reloadDU@bdu)
                         end
                       else (instrs, bDU)
                    | _ => (instrs, bDU)
                  (*esac*))
                  else (instrs, bDU)
               end
                 | blockEnd([],[]) = ([], [])

              val (newInstrs, newBdu) = 
                   doInstrs(!insns, bdu, [], [])
              val (newInstrs, newBdu) = blockEnd(newInstrs, newBdu)
          in  insns := newInstrs;
              A.update(blockDU, blknum, newBdu);
              doBlocks(rest, node)
          end (* doBlocks *)

          (* The optimistic coloring selection may come up with a node
           * that has already been spilled. Must be careful not to spill
           * it twice.
           *)
          fun glue [] = ()
            | glue((node as NODE{number, color, ...})::rest) =
               (if not(isSpilled number) then 
                  (doBlocks(affectedBlocks node, node)
                   before color := COLORED(spillRegSentinel)
                  )
                else ();
                glue rest
               )

          (* redoAlgorithm
           *  -- rerun graph coloring but note that spilling may 
           *     have introduced new registers.
           *)
          fun redoAlgorithm(spillList) = 
          let val _ = app (markAsSpilled o nodeNumber) spillList
              fun init(_, NODE{color=ref PSEUDO, degree, adj,  
                               movecnt, movelist, ...}) =
                     (degree:=0; adj := []; movecnt:=0; movelist:=[])
                | init _ = ()
          in Intmap.app init nodes
          end
       in glue(spillList);
          redoAlgorithm(spillList);
          debug(cfg_after_spill,"after spilling",blocks,regmap,an)
       end (* rewriteGraph *)

       (** 
        ** The main driver 
        **) 
       fun graphColoring(nodes) =
       let (* Create an empty interference graph *) 
           val graph = newGraph
                       {nodes=nodes,
                        K=RaUser.nFreeRegs,
                        numRegs=RaArch.numRegs(),
                        regmap=regmap,
                        getreg=RaUser.getreg,
                        firstPseudoR=firstPseudoR
                       }
           val moves      = build graph        (* build interference graph *)
           val worklists  = Core.makeWorkLists graph moves 
           val simpCoalFz = Core.simplifyCoalesceFreeze graph

           (* Note: freezeWkl or spillWkl are maintained lazily. *)
           fun iterate wl =  
               case simpCoalFz wl of
                 wl as {spillWkl= _::_, ...} => iterate(selectSpill(graph,wl))
               | wl =>
                 (case mode of
                   COPY_PROPAGATION => Core.finishCP graph
                 | REGISTER_ALLOCATION =>
                   (case Core.optimisticSpilling graph wl of
                     [] => Core.finishRA graph
                   | spills => (rewriteGraph(graph,spills); 
                                graphColoring(nodes))
                   )
                 )
       in  if !dump_graph then Core.dumpGraph graph else ();
           debug(cfg_before_ra,"before register allocation",blocks,regmap,an);
           iterate worklists
       end
       val nodes = initialize()
   in  liveness(nodes,blocks);   (* run liveness analysis *)
       graphColoring(nodes);
       debug(cfg_after_ra,"after register allocation",blocks,regmap,an);
       ra_count := !ra_count + 1;
       cluster
   end

end