liveness.rs 19.9 KB
Newer Older
qinsoon's avatar
qinsoon committed
1 2
extern crate nalgebra;

qinsoon's avatar
qinsoon committed
3
use compiler::machine_code::CompiledFunction;
4
use ast::ir::*;
qinsoon's avatar
qinsoon committed
5
use compiler::backend;
6
use utils::vec_utils;
7
use utils::LinkedHashSet;
8 9

use std::collections::LinkedList;
qinsoon's avatar
qinsoon committed
10 11 12 13
use std::collections::{HashMap, HashSet};

use self::nalgebra::DMatrix;

14 15
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Node(usize);
16
#[derive(Clone, Debug, PartialEq)]
qinsoon's avatar
qinsoon committed
17 18
pub struct NodeProperty {
    color: Option<MuID>,
19 20 21
    group: backend::RegGroup,
    temp: MuID,
    spill_cost: f32
qinsoon's avatar
qinsoon committed
22 23 24
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Move{pub from: Node, pub to: Node}
25 26

pub struct InterferenceGraph {
qinsoon's avatar
qinsoon committed
27
    nodes: HashMap<MuID, Node>,
qinsoon's avatar
qinsoon committed
28
    nodes_property: HashMap<Node, NodeProperty>,
qinsoon's avatar
qinsoon committed
29 30 31
    
    matrix: Option<DMatrix<bool>>,
    
qinsoon's avatar
qinsoon committed
32
    moves: HashSet<Move>,
33 34 35 36
}

impl InterferenceGraph {
    fn new() -> InterferenceGraph {
qinsoon's avatar
qinsoon committed
37 38
        InterferenceGraph {
            nodes: HashMap::new(),
qinsoon's avatar
qinsoon committed
39
            nodes_property: HashMap::new(),
qinsoon's avatar
qinsoon committed
40 41 42 43 44
            matrix: None,
            moves: HashSet::new()
        }
    }
    
qinsoon's avatar
qinsoon committed
45 46 47 48
    fn new_node(&mut self, reg_id: MuID, context: &FunctionContext) -> Node {
        let entry = context.get_value(reg_id).unwrap();
        
        if !self.nodes.contains_key(&reg_id) {
qinsoon's avatar
qinsoon committed
49
            let index = self.nodes.len();
50
            let node = Node(index);
qinsoon's avatar
qinsoon committed
51 52 53 54 55
            
            // add the node
            self.nodes.insert(reg_id, node.clone());
            
            // add node property
56
            let group = backend::RegGroup::get(entry.ty());
qinsoon's avatar
qinsoon committed
57 58
            let property = NodeProperty {
                color: None,
59 60 61
                group: group,
                temp: reg_id,
                spill_cost: 0.0f32
qinsoon's avatar
qinsoon committed
62 63
            };
            self.nodes_property.insert(node, property);
64 65 66 67 68 69 70 71 72 73
        } 
        
        
        let node = * self.nodes.get(&reg_id).unwrap();
        
        // increase node spill cost
        let property = self.nodes_property.get_mut(&node).unwrap();
        property.spill_cost += 1.0f32;
        
        node
qinsoon's avatar
qinsoon committed
74 75
    }
    
qinsoon's avatar
qinsoon committed
76
    pub fn get_node(&self, reg: MuID) -> Node {
qinsoon's avatar
qinsoon committed
77
        match self.nodes.get(&reg) {
qinsoon's avatar
qinsoon committed
78
            Some(index) => *index,
qinsoon's avatar
qinsoon committed
79
            None => panic!("do not have a node for {}", reg)
qinsoon's avatar
qinsoon committed
80 81 82
        }
    }
    
83 84 85 86 87 88 89 90
    pub fn temps(&self) -> Vec<MuID>{
        let mut ret = vec![];
        for reg in self.nodes.keys() {
            ret.push(*reg);
        }
        ret
    }
    
qinsoon's avatar
qinsoon committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    pub fn nodes(&self) -> Vec<Node> {
        let mut ret = vec![];
        for node in self.nodes.values() {
            ret.push(node.clone());
        }
        ret
    }
    
    pub fn moves(&self) -> &HashSet<Move> {
        &self.moves
    }
    
    pub fn n_nodes(&self) -> usize {
        self.nodes.len()
    }
    
qinsoon's avatar
qinsoon committed
107 108 109 110 111
    fn init_graph(&mut self) {
        let len = self.nodes.len();
        self.matrix = Some(DMatrix::from_element(len, len, false));
    }
    
qinsoon's avatar
qinsoon committed
112 113
    fn add_move(&mut self, src: Node, dst: Node) {
        self.moves.insert(Move{from: src, to: dst});
114
    }
qinsoon's avatar
qinsoon committed
115 116 117 118 119

    pub fn is_same_group(&self, node1: Node, node2: Node) -> bool {
        self.nodes_property.get(&node1).unwrap().group
            == self.nodes_property.get(&node2).unwrap().group
    }
120
    
qinsoon's avatar
qinsoon committed
121
    pub fn add_interference_edge(&mut self, from: Node, to: Node) {
qinsoon's avatar
qinsoon committed
122 123 124 125 126 127
        self.matrix.as_mut().unwrap()[(from.0, to.0)] = true;
    }

    pub fn is_interferenced_with(&self, node1: Node, node2: Node) -> bool {
        self.matrix.as_ref().unwrap()[(node1.0, node2.0)]
        || self.matrix.as_ref().unwrap()[(node2.0, node1.0)]
qinsoon's avatar
qinsoon committed
128 129
    }
    
130
    pub fn color_node(&mut self, node: Node, color: MuID) {
qinsoon's avatar
qinsoon committed
131
        self.nodes_property.get_mut(&node).unwrap().color = Some(color);
qinsoon's avatar
qinsoon committed
132 133
    }
    
qinsoon's avatar
qinsoon committed
134 135 136 137
    pub fn is_colored(&self, node: Node) -> bool {
        self.nodes_property.get(&node).unwrap().color.is_some()
    }
    
138 139 140 141
    pub fn get_color_of(&self, node: Node) -> Option<MuID> {
        self.nodes_property.get(&node).unwrap().color
    }
    
qinsoon's avatar
qinsoon committed
142 143
    pub fn get_group_of(&self, node: Node) -> backend::RegGroup {
        self.nodes_property.get(&node).unwrap().group
qinsoon's avatar
qinsoon committed
144 145
    }
    
146 147 148 149 150 151 152 153
    pub fn get_temp_of(&self, node: Node) -> MuID {
        self.nodes_property.get(&node).unwrap().temp
    }
    
    pub fn get_spill_cost(&self, node: Node) -> f32 {
        self.nodes_property.get(&node).unwrap().spill_cost
    }
    
154
    fn is_same_node(&self, node1: Node, node2: Node) -> bool {
qinsoon's avatar
qinsoon committed
155
        node1 == node2
qinsoon's avatar
qinsoon committed
156 157
    }
    
qinsoon's avatar
qinsoon committed
158
    pub fn is_adj(&self, from: Node, to: Node) -> bool {
159 160
        let ref matrix = self.matrix.as_ref().unwrap();
        
161
        matrix[(from.0, to.0)] || matrix[(to.0, from.0)]
qinsoon's avatar
qinsoon committed
162 163
    }
    
164 165 166 167 168 169 170 171 172 173 174 175 176
    pub fn outedges_of(&self, node: Node) -> Vec<Node> {
        let mut ret = vec![];
        let matrix = self.matrix.as_ref().unwrap();
        
        for i in 0..self.nodes.len() {
            if matrix[(node.0, i)] {
                ret.push(Node(i));
            }
        }
        
        ret
    }
    
qinsoon's avatar
qinsoon committed
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
    pub fn outdegree_of(&self, node: Node) -> usize {
        let mut count = 0;
        for i in 0..self.nodes.len() {
            if self.matrix.as_ref().unwrap()[(node.0, i)] {
                count += 1;
            }
        }
        
        count
    }
    
    pub fn indegree_of(&self, node: Node) -> usize {
        let mut count = 0;
        for i in 0..self.nodes.len() {
            if self.matrix.as_ref().unwrap()[(i, node.0)] {
                count += 1;
            }
        }
        
        count
    }
    
    pub fn degree_of(&self, node: Node) -> usize {
        self.outdegree_of(node) + self.indegree_of(node)
    }
    
qinsoon's avatar
qinsoon committed
203
    pub fn print(&self, context: &FunctionContext) {
204 205 206
        println!("");
        println!("Interference Graph");

qinsoon's avatar
qinsoon committed
207 208
        println!("nodes:");
        for id in self.nodes.keys() {
qinsoon's avatar
qinsoon committed
209 210
            let val = context.get_value(*id).unwrap().value();
            println!("Reg {} -> {:?}", val, self.nodes.get(&id).unwrap());
qinsoon's avatar
qinsoon committed
211 212
        }

213
        println!("color:");
qinsoon's avatar
qinsoon committed
214 215 216 217
        for (node, color) in self.nodes_property.iter() {
            let node_val = context.get_value(self.get_temp_of(*node)).unwrap().value();
            let color_val = context.get_value(color.temp).unwrap().value();
            println!("Reg {} of {:?} -> Color/Reg {}", node_val, node, color_val);
218 219 220
        }
        println!("moves:");
        for mov in self.moves.iter() {
qinsoon's avatar
qinsoon committed
221
            println!("Move {:?} -> {:?}", mov.from, mov.to);
222 223 224
        }
        println!("graph:");
        {
qinsoon's avatar
qinsoon committed
225 226
            let node_to_reg_id = {
                let mut ret : HashMap<Node, MuID> = HashMap::new();
227
                
qinsoon's avatar
qinsoon committed
228 229
                for reg in self.nodes.keys() {
                    ret.insert(*self.nodes.get(reg).unwrap(), *reg);
230 231 232 233 234 235 236 237 238
                }
                
                ret 
            };
            
            let matrix = self.matrix.as_ref().unwrap();
            for i in 0..matrix.ncols() {
                for j in 0..matrix.nrows() {
                    if matrix[(i, j)] {
239 240
                        let from_node = node_to_reg_id.get(&Node(i)).unwrap();
                        let to_node = node_to_reg_id.get(&Node(j)).unwrap();
qinsoon's avatar
qinsoon committed
241 242 243 244 245

                        let from_val = context.get_value(*from_node).unwrap().value();
                        let to_val = context.get_value(*to_node).unwrap().value();

                        println!("Reg {} -> Reg {}", from_val, to_val);
246 247 248 249 250 251
                    }
                }
            }
        }
        println!("");
    }
qinsoon's avatar
qinsoon committed
252 253
}

254 255
#[allow(unused_variables)]
fn build_live_set(cf: &mut CompiledFunction, func: &MuFunctionVersion) {
qinsoon's avatar
qinsoon committed
256
    let n_insts = cf.mc().number_of_insts();
257
    
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    let mut livein  : Vec<Vec<MuID>> = vec![vec![]; n_insts];
    let mut liveout : Vec<Vec<MuID>> = vec![vec![]; n_insts];    
    
    let mut is_changed = true;
    
    while is_changed {
        // reset
        is_changed = false;
        
        for n in 0..n_insts {
            let in_set_old = livein[n].to_vec(); // copy to new vec
            let out_set_old = liveout[n].to_vec();
            
            // in[n] <- use[n] + (out[n] - def[n])
            // (1) in[n] = use[n]
            let mut in_set_new = vec![];
qinsoon's avatar
qinsoon committed
274
            in_set_new.extend_from_slice(&cf.mc().get_inst_reg_uses(n));
275 276
            // (2) diff = out[n] - def[n]
            let mut diff = liveout[n].to_vec();
qinsoon's avatar
qinsoon committed
277
            for def in cf.mc().get_inst_reg_defines(n) {
qinsoon's avatar
qinsoon committed
278
                vec_utils::remove_value(&mut diff, def);
279 280 281 282 283 284 285 286 287 288
            }
            // (3) in[n] = in[n] + diff
            vec_utils::append_unique(&mut in_set_new, &mut diff);
            
            // update livein[n]
            livein[n].clear();
            livein[n].extend_from_slice(&in_set_new);
            
            // out[n] <- union(in[s] for every successor s of n)
            let mut union = vec![];
qinsoon's avatar
qinsoon committed
289
            for s in cf.mc().get_succs(n) {
290 291 292 293 294 295 296 297 298 299 300 301 302
                vec_utils::append_clone_unique(&mut union, &livein[*s]);
            }
            
            // update liveout[n]
            liveout[n].clear();
            liveout[n].extend_from_slice(&union);
            
            let n_changed = !vec_utils::is_identical_ignore_order(&livein[n], &in_set_old)
                || !vec_utils::is_identical_ignore_order(&liveout[n], &out_set_old);
            is_changed = is_changed || n_changed;
        }
    }
    
qinsoon's avatar
qinsoon committed
303
    for block in cf.mc().get_all_blocks().to_vec() {
304 305 306 307 308
        let start_inst = cf.mc().get_block_range(&block).unwrap().start;
        cf.mc_mut().set_ir_block_livein(&block, livein[start_inst].to_vec());

        let end_inst = cf.mc().get_block_range(&block).unwrap().end;
        cf.mc_mut().set_ir_block_liveout(&block, liveout[end_inst].to_vec());
309
    }
310 311
}

312
// from Tailoring Graph-coloring Register Allocation For Runtime Compilation, Figure 4
313 314 315
pub fn build_chaitin_briggs (cf: &mut CompiledFunction, func: &MuFunctionVersion) -> InterferenceGraph {
    build_live_set(cf, func);
    
316 317 318
    let mut ig = InterferenceGraph::new();
    
    // precolor machine register nodes
319
    for reg in backend::all_regs().values() {
320 321 322 323 324 325
        let reg_id = reg.extract_ssa_id().unwrap();
        let node = ig.new_node(reg_id, &func.context);
        ig.color_node(node, reg_id);
    }
    
    // Initialize and creates nodes for all the involved temps/regs
qinsoon's avatar
qinsoon committed
326 327
    for i in 0..cf.mc().number_of_insts() {
        for reg_id in cf.mc().get_inst_reg_defines(i) {
328 329 330
            ig.new_node(reg_id, &func.context);
        }
        
qinsoon's avatar
qinsoon committed
331
        for reg_id in cf.mc().get_inst_reg_uses(i) {
332 333 334 335 336
            ig.new_node(reg_id, &func.context);
        }
    }
    
    // all nodes has been added, we init graph (create adjacency matrix)
337
    ig.init_graph();
338
    
qinsoon's avatar
qinsoon committed
339
    for block in cf.mc().get_all_blocks() {
340
        // Current_Live(B) = LiveOut(B)
qinsoon's avatar
qinsoon committed
341
        let mut current_live = LinkedHashSet::from_vec(match cf.mc().get_ir_block_liveout(&block) {
342 343 344
            Some(liveout) => liveout.to_vec(),
            None => panic!("cannot find liveout for block {}", block)
        });
qinsoon's avatar
qinsoon committed
345 346 347 348 349 350
        if cfg!(debug_assertions) {
            trace!("Block{}: live out", block);
            for ele in current_live.iter() {
                trace!("{}", func.context.get_temp_display(*ele));
            }
        }
351
        
qinsoon's avatar
qinsoon committed
352
        let range = cf.mc().get_block_range(&block);
353
        if range.is_none() {
qinsoon's avatar
qinsoon committed
354
            warn!("Block{}: has no range (no instructions?)", block);
355 356
            continue;
        }
qinsoon's avatar
qinsoon committed
357
        trace!("Block{}: range = {:?}", block, range.as_ref().unwrap());
358 359 360
        
        // for every inst I in reverse order
        for i in range.unwrap().rev() {
qinsoon's avatar
qinsoon committed
361 362 363 364 365 366 367
            if cfg!(debug_assertions) {
                trace!("Block{}: Inst{}: start. current_live:", block, i);
                for ele in current_live.iter() {
                    trace!("{}", func.context.get_temp_display(*ele));
                }
            }

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

            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));
                }
            }
455 456 457 458 459 460
        }
    }
    
    ig
}

461
// from tony's code src/RegAlloc/Liveness.java
462 463
// this function is no longer used
#[allow(dead_code)]
464
pub fn build (cf: &CompiledFunction, func: &MuFunctionVersion) -> InterferenceGraph {
465 466
    let mut ig = InterferenceGraph::new();
    
qinsoon's avatar
qinsoon committed
467
    // precolor machine register nodes
468
    for reg in backend::all_regs().values() {
qinsoon's avatar
qinsoon committed
469 470 471 472
        let reg_id = reg.extract_ssa_id().unwrap();
        let node = ig.new_node(reg_id, &func.context);
        ig.color_node(node, reg_id);
    }
473 474
    
    // Liveness Analysis
qinsoon's avatar
qinsoon committed
475
    let n_insts = cf.mc().number_of_insts();
476 477 478 479 480 481 482 483 484
    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];
        
qinsoon's avatar
qinsoon committed
485
        for reg_id in cf.mc().get_inst_reg_defines(i) {
qinsoon's avatar
qinsoon committed
486
            ig.new_node(reg_id, &func.context);
487 488
        }
        
qinsoon's avatar
qinsoon committed
489
        for reg_id in cf.mc().get_inst_reg_uses(i) {
qinsoon's avatar
qinsoon committed
490
            ig.new_node(reg_id, &func.context);
qinsoon's avatar
qinsoon committed
491 492
            
            in_set.push(reg_id);
493 494 495 496 497
        }
        
        work_list.push_front(i);
    }
    
qinsoon's avatar
qinsoon committed
498 499 500 501
    // all nodes has been added, we init graph (create adjacency matrix)
    ig.init_graph();
    
    // compute liveIn and liveOut iteratively
502
    trace!("build live outs");
503 504
    while !work_list.is_empty() {
        let n = work_list.pop_front().unwrap();
505
        trace!("build liveout for #{}", n);
506
        let ref mut out_set = live_out[n];
qinsoon's avatar
qinsoon committed
507 508
        
        // out = union(in[succ]) for all succs
qinsoon's avatar
qinsoon committed
509
        for succ in cf.mc().get_succs(n) {
510
            trace!("add successor's livein {:?} to #{}", &live_in[*succ], n); 
511
            vec_utils::add_all(out_set, &live_in[*succ]);
qinsoon's avatar
qinsoon committed
512 513 514 515
        }
        
        // in = use(i.e. live_in) + (out - def) 
        let mut diff = out_set.clone();
qinsoon's avatar
qinsoon committed
516
        for def in cf.mc().get_inst_reg_defines(n) {
qinsoon's avatar
qinsoon committed
517 518
            vec_utils::remove_value(&mut diff, def);
            trace!("removing def: {}", def);
519
            trace!("diff = {:?}", diff);
qinsoon's avatar
qinsoon committed
520
        }
521
        trace!("out - def = {:?}", diff);
qinsoon's avatar
qinsoon committed
522 523 524
        
        if !diff.is_empty() {
            let ref mut in_set = live_in[n];
525
            trace!("in = (use) {:?}", in_set);
qinsoon's avatar
qinsoon committed
526
            
527
            if vec_utils::add_all(in_set, &diff) {
qinsoon's avatar
qinsoon committed
528
                for p in cf.mc().get_preds(n) {
qinsoon's avatar
qinsoon committed
529 530 531 532
                    work_list.push_front(*p);
                }
            }
        }
533
        trace!("in = use + (out - def) = {:?}", live_in[n]);
534 535 536 537 538 539 540 541 542
    }
    
    // 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);
        }
qinsoon's avatar
qinsoon committed
543 544 545 546 547 548 549
    }
    
    // build interference graph
    for n in 0..n_insts {
        let ref mut live = live_out[n];
        
        let src : Option<MuID> = {
qinsoon's avatar
qinsoon committed
550 551 552
            if cf.mc().is_move(n) {
                let src = cf.mc().get_inst_reg_uses(n);
                let dst = cf.mc().get_inst_reg_defines(n);
qinsoon's avatar
qinsoon committed
553
                
554 555
                // src may be an immediate number
                // but dest is definitly a register
qinsoon's avatar
qinsoon committed
556 557
                debug_assert!(dst.len() == 1);
                
558
                if src.len() == 1 {
qinsoon's avatar
qinsoon committed
559 560 561
                    let node1 = ig.get_node(src[0]);
                    let node2 = ig.get_node(dst[0]);
                    ig.add_move(node1, node2);
562 563 564 565 566
                    
                    Some(src[0])
                } else {
                    None
                }
qinsoon's avatar
qinsoon committed
567 568 569 570 571
            } else {
                None
            }
        };
        
qinsoon's avatar
qinsoon committed
572
        for d in cf.mc().get_inst_reg_defines(n) {
qinsoon's avatar
qinsoon committed
573 574
            for t in live.iter() {
                if src.is_none() || (src.is_some() && *t != src.unwrap()) {
qinsoon's avatar
qinsoon committed
575
                    let from = ig.get_node(d);
576
                    let to = ig.get_node(*t);
qinsoon's avatar
qinsoon committed
577 578
                    
                    if !ig.is_same_node(from, to) && !ig.is_adj(from, to) {
qinsoon's avatar
qinsoon committed
579
                        if !ig.is_colored(from) {
qinsoon's avatar
qinsoon committed
580 581
                            ig.add_interference_edge(from, to);
                        }
qinsoon's avatar
qinsoon committed
582
                        if !ig.is_colored(to) {
qinsoon's avatar
qinsoon committed
583 584 585 586 587 588 589
                            ig.add_interference_edge(to, from);
                        }
                    }
                }
            }
        }
        
qinsoon's avatar
qinsoon committed
590
        for d in cf.mc().get_inst_reg_defines(n) {
qinsoon's avatar
qinsoon committed
591
            vec_utils::remove_value(live, d);
qinsoon's avatar
qinsoon committed
592 593
        }
        
qinsoon's avatar
qinsoon committed
594
        for u in cf.mc().get_inst_reg_uses(n) {
qinsoon's avatar
qinsoon committed
595
            live.push(u);
qinsoon's avatar
qinsoon committed
596 597 598 599
        }
    }
    
    ig
600
}