liveness.rs 20.3 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
use compiler::machine_code::CompiledFunction;
2
use ast::ir::*;
qinsoon's avatar
qinsoon committed
3
use compiler::backend;
4
use utils::LinkedHashSet;
5

qinsoon's avatar
qinsoon committed
6 7
use std::collections::{HashMap, HashSet};

8 9 10
use compiler::backend::reg_alloc::graph_coloring::petgraph;
use compiler::backend::reg_alloc::graph_coloring::petgraph::Graph;
use compiler::backend::reg_alloc::graph_coloring::petgraph::graph::NodeIndex;
qinsoon's avatar
qinsoon committed
11

12 13 14
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GraphNode {
    temp: MuID,
qinsoon's avatar
qinsoon committed
15
    color: Option<MuID>,
16 17
    group: backend::RegGroup,
    spill_cost: f32
qinsoon's avatar
qinsoon committed
18
}
19

qinsoon's avatar
qinsoon committed
20
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
21
pub struct Move{pub from: NodeIndex, pub to: NodeIndex}
22 23

pub struct InterferenceGraph {
24 25
    graph: Graph<GraphNode, (), petgraph::Undirected>,
    nodes: HashMap<MuID, NodeIndex>,
qinsoon's avatar
qinsoon committed
26
    moves: HashSet<Move>,
27 28 29 30
}

impl InterferenceGraph {
    fn new() -> InterferenceGraph {
qinsoon's avatar
qinsoon committed
31
        InterferenceGraph {
32
            graph: Graph::new_undirected(),
qinsoon's avatar
qinsoon committed
33 34 35 36 37
            nodes: HashMap::new(),
            moves: HashSet::new()
        }
    }
    
38
    fn new_node(&mut self, reg_id: MuID, context: &FunctionContext) -> NodeIndex {
qinsoon's avatar
qinsoon committed
39 40 41
        let entry = context.get_value(reg_id).unwrap();
        
        if !self.nodes.contains_key(&reg_id) {
42
            let node = GraphNode {
43
                temp: reg_id,
44 45
                color: None,
                group: backend::RegGroup::get(entry.ty()),
46
                spill_cost: 0.0f32
qinsoon's avatar
qinsoon committed
47
            };
48 49 50 51 52

            let index = self.graph.add_node(node);

            self.nodes.insert(reg_id, index);
        }
53
        
54 55
        let node_index = *self.nodes.get(&reg_id).unwrap();
        let node_mut = self.graph.node_weight_mut(node_index).unwrap();
56 57
        
        // increase node spill cost
58
        node_mut.spill_cost += 1.0f32;
59
        
60
        node_index
qinsoon's avatar
qinsoon committed
61 62
    }
    
63
    pub fn get_node(&self, reg: MuID) -> NodeIndex {
qinsoon's avatar
qinsoon committed
64
        match self.nodes.get(&reg) {
qinsoon's avatar
qinsoon committed
65
            Some(index) => *index,
qinsoon's avatar
qinsoon committed
66
            None => panic!("do not have a node for {}", reg)
qinsoon's avatar
qinsoon committed
67 68 69
        }
    }
    
70 71 72 73 74 75 76 77
    pub fn temps(&self) -> Vec<MuID>{
        let mut ret = vec![];
        for reg in self.nodes.keys() {
            ret.push(*reg);
        }
        ret
    }
    
78
    pub fn nodes(&self) -> Vec<NodeIndex> {
qinsoon's avatar
qinsoon committed
79
        let mut ret = vec![];
80 81
        for index in self.nodes.values() {
            ret.push(*index);
qinsoon's avatar
qinsoon committed
82 83 84 85 86 87 88 89 90 91 92 93
        }
        ret
    }
    
    pub fn moves(&self) -> &HashSet<Move> {
        &self.moves
    }
    
    pub fn n_nodes(&self) -> usize {
        self.nodes.len()
    }
    
94
    fn add_move(&mut self, src: NodeIndex, dst: NodeIndex) {
qinsoon's avatar
qinsoon committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        let src = {
            let temp_src = self.get_temp_of(src);
            if temp_src < MACHINE_ID_END {
                let alias = backend::get_color_for_precolored(self.get_temp_of(src));
                self.get_node(alias)
            } else {
                src
            }
        };


        let dst = {
            let temp_dst = self.get_temp_of(dst);
            if temp_dst < MACHINE_ID_END {
                let alias = backend::get_color_for_precolored(self.get_temp_of(dst));
                self.get_node(alias)
            } else {
                dst
            }
        };

qinsoon's avatar
qinsoon committed
116
        self.moves.insert(Move{from: src, to: dst});
117 118
    }
    
119 120
    pub fn add_interference_edge(&mut self, from: NodeIndex, to: NodeIndex) {
        self.graph.update_edge(from, to, ());
qinsoon's avatar
qinsoon committed
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

        // if one of the node is machine register, we also add
        // interference edge to its alias
        // e.g. if we have %a, %edi interferenced,
        // we also add %a, %rdi interference

        let from_tmp = self.graph.node_weight(from).unwrap().temp;
        let to_tmp   = self.graph.node_weight(to).unwrap().temp;

        if from_tmp < MACHINE_ID_END || to_tmp < MACHINE_ID_END {
            let from_tmp = if from_tmp < MACHINE_ID_END {
                backend::get_color_for_precolored(from_tmp)
            } else {
                from_tmp
            };

            let to_tmp = if to_tmp < MACHINE_ID_END {
                backend::get_color_for_precolored(to_tmp)
            } else {
                to_tmp
            };

            let from_tmp_node = self.get_node(from_tmp);
            let to_tmp_node   = self.get_node(to_tmp);
            self.graph.update_edge(from_tmp_node, to_tmp_node, ());
        }
qinsoon's avatar
qinsoon committed
147 148
    }

149
    pub fn is_interferenced_with(&self, node1: NodeIndex, node2: NodeIndex) -> bool {
150 151 152
        let edge = self.graph.find_edge(node1, node2);

        edge.is_some()
qinsoon's avatar
qinsoon committed
153 154
    }
    
155 156
    pub fn color_node(&mut self, node: NodeIndex, color: MuID) {
        self.graph.node_weight_mut(node).unwrap().color = Some(color);
qinsoon's avatar
qinsoon committed
157 158
    }
    
159 160
    pub fn is_colored(&self, node: NodeIndex) -> bool {
        self.graph.node_weight(node).unwrap().color.is_some()
qinsoon's avatar
qinsoon committed
161 162
    }
    
163 164
    pub fn get_color_of(&self, node: NodeIndex) -> Option<MuID> {
        self.graph.node_weight(node).unwrap().color
165 166
    }
    
167 168
    pub fn get_group_of(&self, node: NodeIndex) -> backend::RegGroup {
        self.graph.node_weight(node).unwrap().group
qinsoon's avatar
qinsoon committed
169 170
    }
    
171 172
    pub fn get_temp_of(&self, node: NodeIndex) -> MuID {
        self.graph.node_weight(node).unwrap().temp
173 174
    }
    
175 176
    pub fn get_spill_cost(&self, node: NodeIndex) -> f32 {
        self.graph.node_weight(node).unwrap().spill_cost
177 178
    }
    
179
    fn is_same_node(&self, node1: NodeIndex, node2: NodeIndex) -> bool {
qinsoon's avatar
qinsoon committed
180
        node1 == node2
qinsoon's avatar
qinsoon committed
181
    }
182 183 184 185 186 187 188

    fn is_same_group(&self, node1: NodeIndex, node2: NodeIndex) -> bool {
        let node1 = self.graph.node_weight(node1).unwrap();
        let node2 = self.graph.node_weight(node2).unwrap();

        node1.group == node2.group
    }
qinsoon's avatar
qinsoon committed
189
    
190 191
    pub fn is_adj(&self, from: NodeIndex, to: NodeIndex) -> bool {
        self.is_interferenced_with(from, to)
qinsoon's avatar
qinsoon committed
192 193
    }
    
194 195
    pub fn outedges_of(&self, node: NodeIndex) -> Vec<NodeIndex> {
        self.graph.neighbors(node).collect()
196 197
    }
    
198 199
    pub fn outdegree_of(&self, node: NodeIndex) -> usize {
        self.outedges_of(node).len()
qinsoon's avatar
qinsoon committed
200 201
    }
    
202 203
    pub fn indegree_of(&self, node: NodeIndex) -> usize {
        self.outdegree_of(node)
qinsoon's avatar
qinsoon committed
204 205
    }
    
206
    pub fn degree_of(&self, node: NodeIndex) -> usize {
qinsoon's avatar
fix  
qinsoon committed
207
        self.outdegree_of(node)
qinsoon's avatar
qinsoon committed
208 209
    }
    
qinsoon's avatar
qinsoon committed
210
    pub fn print(&self, context: &FunctionContext) {
211 212 213
        use compiler::backend::reg_alloc::graph_coloring::petgraph::dot::Dot;
        use compiler::backend::reg_alloc::graph_coloring::petgraph::dot::Config;

qinsoon's avatar
qinsoon committed
214 215
        debug!("");
        debug!("Interference Graph");
216

qinsoon's avatar
qinsoon committed
217
        debug!("nodes:");
qinsoon's avatar
qinsoon committed
218
        for id in self.nodes.keys() {
qinsoon's avatar
qinsoon committed
219
            let val = context.get_value(*id).unwrap().value();
qinsoon's avatar
qinsoon committed
220
            debug!("Reg {} -> {:?}", val, self.nodes.get(&id).unwrap());
qinsoon's avatar
qinsoon committed
221 222
        }

qinsoon's avatar
qinsoon committed
223
        debug!("moves:");
224
        for mov in self.moves.iter() {
qinsoon's avatar
qinsoon committed
225
            debug!("Move {:?} -> {:?}", mov.from, mov.to);
226
        }
qinsoon's avatar
qinsoon committed
227

228
        debug!("graph:");
229
        debug!("\n\n{:?}\n", Dot::with_config(&self.graph, &[Config::EdgeNoLabel]));
qinsoon's avatar
qinsoon committed
230
        debug!("");
231
    }
qinsoon's avatar
qinsoon committed
232 233
}

qinsoon's avatar
[wip]  
qinsoon committed
234 235
fn build_live_set (cf: &mut CompiledFunction, func: &MuFunctionVersion) {
    info!("---start building live set---");
236

qinsoon's avatar
qinsoon committed
237
    let n_insts = cf.mc().number_of_insts();
238 239 240 241

    let mut livein  : Vec<LinkedHashSet<MuID>> = vec![LinkedHashSet::new(); n_insts];
    let mut liveout : Vec<LinkedHashSet<MuID>> = vec![LinkedHashSet::new(); n_insts];

242
    let mut is_changed = true;
243

qinsoon's avatar
[wip]  
qinsoon committed
244
    let mut i = 0;
245
    while is_changed {
qinsoon's avatar
[wip]  
qinsoon committed
246 247 248
        trace!("---iteration {}---", i);
        i += 1;

249 250
        // reset
        is_changed = false;
251

252
        for n in 0..n_insts {
253 254 255 256 257 258 259 260 261
            let in_set_old = livein[n].clone();
            let out_set_old = liveout[n].clone();

            // in[n] <- use[n] + (out[n] - def[n]);
            {
                let ref mut inset = livein[n];

                inset.clear();

qinsoon's avatar
[wip]  
qinsoon committed
262
                // (1) out[n] - def[n]
263 264 265 266
                inset.add_all(liveout[n].clone());
                for def in cf.mc().get_inst_reg_defines(n) {
                    inset.remove(&def);
                }
qinsoon's avatar
[wip]  
qinsoon committed
267 268
                // (2) in[n] + (out[n] - def[n])
                inset.add_from_vec(cf.mc().get_inst_reg_uses(n));
269
            }
270

271
            // out[n] <- union(in[s] for every successor s of n)
272 273 274 275 276 277 278
            {
                let ref mut outset = liveout[n];
                outset.clear();

                for s in cf.mc().get_succs(n) {
                    outset.add_all(livein[*s].clone());
                }
279
            }
280 281 282 283

            // is in/out changed in this iteration?
            let n_changed = !in_set_old.equals(&livein[n]) || !out_set_old.equals(&liveout[n]);

qinsoon's avatar
[wip]  
qinsoon committed
284 285 286 287 288 289
            trace!("inst {}", n);
            trace!("in(old)  = {:?}", in_set_old);
            trace!("in(new)  = {:?}", livein[n]);
            trace!("out(old) = {:?}", out_set_old);
            trace!("out(new) = {:?}", liveout[n]);

290 291 292
            is_changed = is_changed || n_changed;
        }
    }
293

qinsoon's avatar
[wip]  
qinsoon committed
294 295
    info!("---finish building live set---");

qinsoon's avatar
qinsoon committed
296
    for block in cf.mc().get_all_blocks().to_vec() {
297
        let start_inst = cf.mc().get_block_range(&block).unwrap().start;
qinsoon's avatar
[wip]  
qinsoon committed
298 299 300 301 302 303
        let livein = livein[start_inst].clone().to_vec();
        {
            let display_array : Vec<String> =  livein.iter().map(|x| func.context.get_temp_display(*x)).collect();
            trace!("livein  for block {}: {:?}", block, display_array);
        }
        cf.mc_mut().set_ir_block_livein(&block, livein);
304

qinsoon's avatar
[wip]  
qinsoon committed
305 306 307 308 309 310 311
        let end_inst = cf.mc().get_block_range(&block).unwrap().end - 1;
        let liveout = liveout[end_inst].clone().to_vec();
        {
            let display_array : Vec<String> = liveout.iter().map(|x| func.context.get_temp_display(*x)).collect();
            trace!("liveout for block {}: {:?}", block, display_array);
        }
        cf.mc_mut().set_ir_block_liveout(&block, liveout);
312
    }
313 314
}

315
// from Tailoring Graph-coloring Register Allocation For Runtime Compilation, Figure 4
316
pub fn build_chaitin_briggs (cf: &mut CompiledFunction, func: &MuFunctionVersion) -> InterferenceGraph {
qinsoon's avatar
[wip]  
qinsoon committed
317 318 319
    build_live_set(cf, func);

    info!("---start building interference graph---");
320
    
321 322 323
    let mut ig = InterferenceGraph::new();
    
    // precolor machine register nodes
324
    for reg in backend::all_regs().values() {
325 326
        let reg_id = reg.extract_ssa_id().unwrap();
        let node = ig.new_node(reg_id, &func.context);
qinsoon's avatar
qinsoon committed
327

qinsoon's avatar
qinsoon committed
328
        let precolor = backend::get_color_for_precolored(reg_id);
qinsoon's avatar
qinsoon committed
329 330

        ig.color_node(node, precolor);
331 332 333
    }
    
    // Initialize and creates nodes for all the involved temps/regs
qinsoon's avatar
qinsoon committed
334 335
    for i in 0..cf.mc().number_of_insts() {
        for reg_id in cf.mc().get_inst_reg_defines(i) {
336 337 338
            ig.new_node(reg_id, &func.context);
        }
        
qinsoon's avatar
qinsoon committed
339
        for reg_id in cf.mc().get_inst_reg_uses(i) {
340 341 342 343
            ig.new_node(reg_id, &func.context);
        }
    }
    
qinsoon's avatar
qinsoon committed
344
    for block in cf.mc().get_all_blocks() {
345
        // Current_Live(B) = LiveOut(B)
qinsoon's avatar
qinsoon committed
346
        let mut current_live = LinkedHashSet::from_vec(match cf.mc().get_ir_block_liveout(&block) {
347 348 349
            Some(liveout) => liveout.to_vec(),
            None => panic!("cannot find liveout for block {}", block)
        });
qinsoon's avatar
qinsoon committed
350 351 352 353 354 355
        if cfg!(debug_assertions) {
            trace!("Block{}: live out", block);
            for ele in current_live.iter() {
                trace!("{}", func.context.get_temp_display(*ele));
            }
        }
356
        
qinsoon's avatar
qinsoon committed
357
        let range = cf.mc().get_block_range(&block);
358
        if range.is_none() {
qinsoon's avatar
qinsoon committed
359
            warn!("Block{}: has no range (no instructions?)", block);
360 361
            continue;
        }
qinsoon's avatar
qinsoon committed
362
        trace!("Block{}: range = {:?}", block, range.as_ref().unwrap());
363 364 365
        
        // for every inst I in reverse order
        for i in range.unwrap().rev() {
qinsoon's avatar
qinsoon committed
366
            if cfg!(debug_assertions) {
qinsoon's avatar
[wip]  
qinsoon committed
367 368 369
                trace!("Block{}: Inst{}", block, i);
                cf.mc().trace_inst(i);
                trace!("current live: ");
qinsoon's avatar
qinsoon committed
370 371 372 373 374
                for ele in current_live.iter() {
                    trace!("{}", func.context.get_temp_display(*ele));
                }
            }

375
            let src : Option<MuID> = {
qinsoon's avatar
qinsoon committed
376 377 378
                if cf.mc().is_move(i) {
                    let src = cf.mc().get_inst_reg_uses(i);
                    let dst = cf.mc().get_inst_reg_defines(i);
379
                    
qinsoon's avatar
qinsoon committed
380 381 382
                    // src:  reg/imm/mem
                    // dest: reg/mem
                    // we dont care if src/dest is mem
qinsoon's avatar
qinsoon committed
383
                    if cf.mc().is_using_mem_op(i) {
384
                        None
qinsoon's avatar
qinsoon committed
385 386 387 388
                    } else {
                        if src.len() == 1 {
                            let node1 = ig.get_node(src[0]);
                            let node2 = ig.get_node(dst[0]);
qinsoon's avatar
qinsoon committed
389 390 391
                            trace!("add move between {} and {}",
                                   func.context.get_temp_display(src[0]),
                                   func.context.get_temp_display(dst[0]));
qinsoon's avatar
qinsoon committed
392 393 394 395 396 397
                            ig.add_move(node1, node2);
                            
                            Some(src[0])
                        } else {
                            None
                        }
398 399 400 401 402
                    }
                } else {
                    None
                }
            };
qinsoon's avatar
qinsoon committed
403
            trace!("Block{}: Inst{}: src={:?}", block, i, src);
404 405
            
            // for every definition D in I
qinsoon's avatar
qinsoon committed
406
            for d in cf.mc().get_inst_reg_defines(i) {
qinsoon's avatar
qinsoon committed
407
                trace!("Block{}: Inst{}: for definition {}", block, i, func.context.get_temp_display(d));
408 409 410
                // add an interference from D to every element E in Current_Live - {D}
                // creating nodes if necessary
                for e in current_live.iter() {
qinsoon's avatar
qinsoon committed
411 412 413
                    trace!("Block{}: Inst{}: for each live {}",
                           block, i,
                           func.context.get_temp_display(*e));
414
                    if src.is_none() || (src.is_some() && *e != src.unwrap()) {
qinsoon's avatar
qinsoon committed
415
                        let from = ig.get_node(d);
416 417
                        let to = ig.get_node(*e);
                        
qinsoon's avatar
qinsoon committed
418
                        if !ig.is_same_node(from, to) &&ig.is_same_group(from, to) && !ig.is_adj(from, to) {
419
                            if !ig.is_colored(from) {
qinsoon's avatar
qinsoon committed
420 421 422 423
                                trace!("Block{}: Inst{}: add interference between {} and {}",
                                       block, i,
                                       func.context.get_temp_display(d),
                                       func.context.get_temp_display(*e));
424 425 426
                                ig.add_interference_edge(from, to);
                            }
                            if !ig.is_colored(to) {
qinsoon's avatar
qinsoon committed
427 428 429 430
                                trace!("Block{}: Inst{}: add interference between {} and {}",
                                       block, i,
                                       func.context.get_temp_display(*e),
                                       func.context.get_temp_display(d));
431 432 433 434 435 436 437 438
                                ig.add_interference_edge(to, from);
                            }
                        }
                    }
                }
            }
            
            // for every definition D in I
qinsoon's avatar
qinsoon committed
439
            for d in cf.mc().get_inst_reg_defines(i) {
qinsoon's avatar
qinsoon committed
440 441 442
                trace!("Block{}: Inst{}: remove define {} from current_live",
                       block, i,
                       func.context.get_temp_display(d));
443
                // remove D from Current_Live
qinsoon's avatar
qinsoon committed
444
                current_live.remove(&d);
445 446 447
            }
            
            // for every use U in I
qinsoon's avatar
qinsoon committed
448
            for u in cf.mc().get_inst_reg_uses(i) {
qinsoon's avatar
qinsoon committed
449 450 451
                trace!("Block{}: Inst{}: add use {} to current_live",
                       block, i,
                       func.context.get_temp_display(u));
452
                // add U to Current_live
qinsoon's avatar
qinsoon committed
453
                current_live.insert(u);
454
            }
qinsoon's avatar
qinsoon committed
455 456 457 458 459 460 461

            if cfg!(debug_assertions) {
                trace!("Block{}: Inst{}: done. current_live:", block, i);
                for ele in current_live.iter() {
                    trace!("{}", func.context.get_temp_display(*ele));
                }
            }
462 463
        }
    }
qinsoon's avatar
[wip]  
qinsoon committed
464 465

    info!("---finish building interference graph---");
466 467 468
    ig
}

469
// from tony's code src/RegAlloc/Liveness.java
470
// this function is no longer used
qinsoon's avatar
qinsoon committed
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
//#[allow(dead_code)]
//pub fn build (cf: &CompiledFunction, func: &MuFunctionVersion) -> InterferenceGraph {
//    let mut ig = InterferenceGraph::new();
//
//    // precolor machine register nodes
//    for reg in backend::all_regs().values() {
//        let reg_id = reg.extract_ssa_id().unwrap();
//        let node = ig.new_node(reg_id, &func.context);
//        ig.color_node(node, reg_id);
//    }
//
//    // Liveness Analysis
//    let n_insts = cf.mc().number_of_insts();
//    let mut live_in : Vec<Vec<MuID>> = vec![vec![]; n_insts];
//    let mut live_out : Vec<Vec<MuID>> = vec![vec![]; n_insts];
//    let mut work_list : LinkedList<usize> = LinkedList::new();
//
//    // Initialize 'in' sets for each node in the flow graph
//    // and creates nodes for all the involved temps/regs
//    for i in 0..n_insts {
//        let ref mut in_set = live_in[i];
//
//        for reg_id in cf.mc().get_inst_reg_defines(i) {
//            ig.new_node(reg_id, &func.context);
//        }
//
//        for reg_id in cf.mc().get_inst_reg_uses(i) {
//            ig.new_node(reg_id, &func.context);
//
//            in_set.push(reg_id);
//        }
//
//        work_list.push_front(i);
//    }
//
//    // all nodes has been added, we init graph (create adjacency matrix)
//    ig.init_graph();
//
//    // compute liveIn and liveOut iteratively
//    trace!("build live outs");
//    while !work_list.is_empty() {
//        let n = work_list.pop_front().unwrap();
//        trace!("build liveout for #{}", n);
//        let ref mut out_set = live_out[n];
//
//        // out = union(in[succ]) for all succs
//        for succ in cf.mc().get_succs(n) {
//            trace!("add successor's livein {:?} to #{}", &live_in[*succ], n);
//            vec_utils::add_all(out_set, &live_in[*succ]);
//        }
//
//        // in = use(i.e. live_in) + (out - def)
//        let mut diff = out_set.clone();
//        for def in cf.mc().get_inst_reg_defines(n) {
//            vec_utils::remove_value(&mut diff, def);
//            trace!("removing def: {}", def);
//            trace!("diff = {:?}", diff);
//        }
//        trace!("out - def = {:?}", diff);
//
//        if !diff.is_empty() {
//            let ref mut in_set = live_in[n];
//            trace!("in = (use) {:?}", in_set);
//
//            if vec_utils::add_all(in_set, &diff) {
//                for p in cf.mc().get_preds(n) {
//                    work_list.push_front(*p);
//                }
//            }
//        }
//        trace!("in = use + (out - def) = {:?}", live_in[n]);
//    }
//
//    // debug live-outs
//    if cfg!(debug_assertions) {
//        trace!("check live-outs");
//        for n in 0..n_insts {
//            let ref mut live = live_out[n];
//            trace!("#{}\t{:?}", n, live);
//        }
//    }
//
//    // build interference graph
//    for n in 0..n_insts {
//        let ref mut live = live_out[n];
//
//        let src : Option<MuID> = {
//            if cf.mc().is_move(n) {
//                let src = cf.mc().get_inst_reg_uses(n);
//                let dst = cf.mc().get_inst_reg_defines(n);
//
//                // src may be an immediate number
//                // but dest is definitly a register
//                debug_assert!(dst.len() == 1);
//
//                if src.len() == 1 {
//                    let node1 = ig.get_node(src[0]);
//                    let node2 = ig.get_node(dst[0]);
//                    ig.add_move(node1, node2);
//
//                    Some(src[0])
//                } else {
//                    None
//                }
//            } else {
//                None
//            }
//        };
//
//        for d in cf.mc().get_inst_reg_defines(n) {
//            for t in live.iter() {
//                if src.is_none() || (src.is_some() && *t != src.unwrap()) {
//                    let from = ig.get_node(d);
//                    let to = ig.get_node(*t);
//
//                    if !ig.is_same_node(from, to) && !ig.is_adj(from, to) {
//                        if !ig.is_colored(from) {
//                            ig.add_interference_edge(from, to);
//                        }
//                        if !ig.is_colored(to) {
//                            ig.add_interference_edge(to, from);
//                        }
//                    }
//                }
//            }
//        }
//
//        for d in cf.mc().get_inst_reg_defines(n) {
//            vec_utils::remove_value(live, d);
//        }
//
//        for u in cf.mc().get_inst_reg_uses(n) {
//            live.push(u);
//        }
//    }
//
//    ig
//}