alive_entry.rs 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2017 The Australian National University
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16 17 18 19 20 21 22 23 24
#![allow(dead_code)]

use utils::LinkedHashMap;
use utils::vec_utils;
use ast::ir::*;
use ast::ptr::*;
use std::fmt;

type EntryID = usize;

25
#[derive(Clone)]
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
pub struct AliveEntries {
    index: EntryID,

    inner: LinkedHashMap<EntryID, RegisterEntry>
}

impl fmt::Display for AliveEntries {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{} entries", self.inner.len()).unwrap();
        writeln!(f, "| {:20} | {:20} | {:20} |", "ssa", "registers", "stack slots").unwrap();
        for entry in self.inner.values() {
            writeln!(f, "{}", entry).unwrap()
        }

        Ok(())
    }
}

impl AliveEntries {
    pub fn new() -> AliveEntries {
        AliveEntries {
            index: 0,
            inner: LinkedHashMap::new()
        }
    }

    fn new_index(&mut self) -> EntryID {
        let ret = self.index;
        self.index += 1;

        ret
    }

qinsoon's avatar
qinsoon committed
59
    pub fn has_entries_for_temp(&self, temp: MuID) -> bool {
60 61
        for entry in self.inner.values() {
            if entry.match_temp(temp) {
qinsoon's avatar
qinsoon committed
62
                return true;
63 64 65
            }
        }

qinsoon's avatar
qinsoon committed
66
        false
67
    }
qinsoon's avatar
qinsoon committed
68 69 70 71 72 73 74 75 76 77 78
    pub fn find_entries_for_temp(&self, temp: MuID) -> Vec<&RegisterEntry> {
        let mut ret = vec![];
        for entry in self.inner.values() {
            if entry.match_temp(temp) {
                ret.push(entry);
            }
        }
        ret
    }
    pub fn find_entries_for_temp_mut(&mut self, temp: MuID) -> Vec<&mut RegisterEntry> {
        let mut ret = vec![];
79 80
        for entry in self.inner.values_mut() {
            if entry.match_temp(temp) {
qinsoon's avatar
qinsoon committed
81
                ret.push(entry);
82 83
            }
        }
qinsoon's avatar
qinsoon committed
84
        ret
85 86
    }

qinsoon's avatar
qinsoon committed
87
    pub fn has_entries_for_reg(&self, reg: MuID) -> bool {
88 89
        for entry in self.inner.values() {
            if entry.match_reg(reg) {
qinsoon's avatar
qinsoon committed
90
                return true;
91 92
            }
        }
qinsoon's avatar
qinsoon committed
93
        false
94
    }
qinsoon's avatar
qinsoon committed
95 96 97 98 99 100 101 102 103 104 105
    pub fn find_entries_for_reg(&self, reg: MuID) -> Vec<&RegisterEntry> {
        let mut ret = vec![];
        for entry in self.inner.values() {
            if entry.match_reg(reg) {
                ret.push(entry);
            }
        }
        ret
    }
    pub fn find_entries_for_reg_mut(&mut self, reg: MuID) -> Vec<&mut RegisterEntry> {
        let mut ret = vec![];
106 107
        for entry in self.inner.values_mut() {
            if entry.match_reg(reg) {
qinsoon's avatar
qinsoon committed
108
                ret.push(entry)
109 110
            }
        }
qinsoon's avatar
qinsoon committed
111 112
        ret
    }
113

qinsoon's avatar
qinsoon committed
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
    pub fn has_entries_for_mem(&self, mem: P<Value>) -> bool {
        for entry in self.inner.values() {
            if entry.match_stack_loc(mem.clone()) {
                return true;
            }
        }
        false
    }
    pub fn find_entries_for_mem(&self, mem: P<Value>) -> Vec<&RegisterEntry> {
        let mut ret = vec![];
        for entry in self.inner.values() {
            if entry.match_stack_loc(mem.clone()) {
                ret.push(entry)
            }
        }
        ret
    }
    pub fn find_entries_for_mem_mut(&mut self, mem: P<Value>) -> Vec<&mut RegisterEntry> {
        let mut ret = vec![];
        for entry in self.inner.values_mut() {
            if entry.match_stack_loc(mem.clone()) {
                ret.push(entry)
            }
        }
        ret
139 140
    }

qinsoon's avatar
qinsoon committed
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
    pub fn new_alive_reg(&mut self, reg: MuID) {
        debug!("adding alive reg: {}", reg);

        let id = self.new_index();
        let entry = RegisterEntry {
            temp : None,
            real : vec![reg],
            stack: vec![]
        };

        self.inner.insert(id, entry);
    }

    pub fn new_alive_mem(&mut self, mem: P<Value>) {
        debug!("adding alive mem: {}", mem);

        let id = self.new_index();
        let entry = RegisterEntry {
            temp : None,
            real : vec![],
            stack: vec![mem]
        };

        self.inner.insert(id, entry);
    }

    pub fn add_temp_in_reg(&mut self, temp: MuID, reg: MuID) {
        debug!("adding alive temp in reg: {} in {}", temp, reg);

qinsoon's avatar
qinsoon committed
171
        let entry_exists = self.has_entries_for_temp(temp);
172 173

        if entry_exists {
qinsoon's avatar
qinsoon committed
174
            let entries = self.find_entries_for_temp_mut(temp);
qinsoon's avatar
qinsoon committed
175 176 177
            for entry in entries {
                entry.add_real_reg(reg);
            }
178 179 180 181 182 183 184 185 186 187 188 189
        } else {
            let id = self.new_index();
            let entry = RegisterEntry {
                temp: Some(temp),
                real: vec![reg],
                stack: vec![]
            };

            self.inner.insert(id, entry);
        }
    }

qinsoon's avatar
qinsoon committed
190
    pub fn add_temp_in_mem(&mut self, temp: MuID, mem: P<Value>) {
qinsoon's avatar
qinsoon committed
191
        debug!("adding alive temp in mem: {} in {}", temp, mem);
qinsoon's avatar
qinsoon committed
192 193 194 195

        let entry_exists = self.has_entries_for_temp(temp);

        if entry_exists {
qinsoon's avatar
qinsoon committed
196
            let entries = self.find_entries_for_temp_mut(temp);
qinsoon's avatar
qinsoon committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
            for entry in entries {
                entry.add_stack_loc(mem.clone());
            }
        } else {
            let id = self.new_index();
            let entry = RegisterEntry {
                temp: Some(temp),
                real: vec![],
                stack: vec![mem]
            };

            self.inner.insert(id, entry);
        }
    }

212 213 214 215
    pub fn remove_reg(&mut self, reg: MuID) {
        debug!("removing alive reg: {}", reg);
        let mut indices = vec![];

216
        for (index, entry) in self.inner.iter_mut() {
217
            if entry.match_reg(reg) {
218 219 220 221 222
                entry.remove_real(reg);

                if entry.is_empty() {
                    indices.push(*index);
                }
223 224 225 226 227 228 229 230
            }
        }

        for index in indices {
            self.inner.remove(&index);
        }
    }

231 232
    pub fn remove_temp(&mut self, temp: MuID) {
        debug!("removing alive temp: {}", temp);
233

234
        let mut ret = vec![];
235

236 237 238
        for (index, entry) in self.inner.iter() {
            if entry.match_temp(temp) {
                ret.push(*index);
239
            }
240
        }
241

242 243 244 245 246 247 248
        if ret.len() == 0 {
            return;
        } else if ret.len() == 1 {
            self.inner.remove(&ret[0]);
        } else {
            panic!("Temp{} has more than one entry in AliveEntries");
        }
249
    }
250 251 252 253

    pub fn intersect(&mut self, another: &Self) -> bool {
        let mut changed = false;

qinsoon's avatar
qinsoon committed
254
        for (_, entry) in self.inner.iter_mut() {
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
            if entry.has_temp() {
                let temp = entry.get_temp().unwrap();

                // find entry with the same temp in the other set, and do intersect
                for another_entry in another.find_entries_for_temp(temp) {
                    if entry.intersect(another_entry) {
                        changed = true;
                    }
                }
            } else {
                // find entry without a temp in the other set and do intersect
                for another_entry in another.inner.values() {
                    if !another_entry.has_temp() {
                        if entry.intersect(another_entry) {
                            changed = true;
                        }
                    }
                }
            }
        }

        changed
    }
qinsoon's avatar
qinsoon committed
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

    pub fn preserve_list(&mut self, list: &Vec<MuID>) {
        let mut indices_to_delete : Vec<EntryID> = vec![];

        for (index, entry) in self.inner.iter() {
            if !entry.has_temp() {
                if list.iter().any(|x| entry.match_reg(*x)) {

                } else {
                    indices_to_delete.push(*index);
                }
            } else {
                let temp = entry.get_temp().unwrap();
                if !vec_utils::find_value(list, temp).is_some() {
                    indices_to_delete.push(*index);
                }
            }
        }

        // always preserve entries with spill location
        let mut indices_to_really_delete : Vec<EntryID> = vec![];
        for index in indices_to_delete {
            let entry = self.inner.get(&index).unwrap();

            if !entry.has_stack_slots() {
                indices_to_really_delete.push(index);
            }
        }

        // delete
        for index in indices_to_really_delete {
            self.inner.remove(&index);
        }
    }
312 313
}

314
#[derive(Clone)]
315 316 317 318 319 320 321
pub struct RegisterEntry {
    temp  : Option<MuID>,
    real  : Vec<MuID>,
    stack : Vec<P<Value>>
}

impl RegisterEntry {
322 323 324 325
    pub fn is_empty(&self) -> bool {
        !self.has_real() && !self.has_stack_slots()
    }

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    pub fn has_temp(&self) -> bool {
        self.temp.is_some()
    }
    pub fn has_real(&self) -> bool {
        !self.real.is_empty()
    }
    pub fn has_stack_slots(&self) -> bool {
        !self.stack.is_empty()
    }

    pub fn set_temp(&mut self, temp: MuID) {
        self.temp = Some(temp);
    }
    pub fn get_temp(&self) -> Option<MuID> {
        self.temp.clone()
    }

qinsoon's avatar
qinsoon committed
343 344 345 346 347 348 349 350 351 352 353 354
    pub fn remove_real(&mut self, reg: MuID) {
        if let Some(index) = vec_utils::find_value(&self.real, reg) {
            self.real.remove(index);
        }
    }

    pub fn remove_stack_loc(&mut self, mem: P<Value>) {
        if let Some(index) = vec_utils::find_value(&self.stack, mem) {
            self.stack.remove(index);
        }
    }

355 356 357 358 359 360 361 362 363 364 365 366
    pub fn match_temp(&self, temp: MuID) -> bool {
        if self.temp.is_some() && self.temp.unwrap() == temp {
            true
        } else {
            false
        }
    }

    pub fn match_reg(&self, reg: MuID) -> bool {
        vec_utils::find_value(&self.real, reg).is_some()
    }

qinsoon's avatar
qinsoon committed
367 368 369 370
    pub fn match_stack_loc(&self, mem: P<Value>) -> bool {
        vec_utils::find_value(&self.stack, mem).is_some()
    }

371 372 373 374 375
    pub fn add_real_reg(&mut self, reg: MuID) {
        if vec_utils::find_value(&mut self.real, reg).is_none() {
            self.real.push(reg);
        }
    }
qinsoon's avatar
qinsoon committed
376 377 378 379 380 381

    pub fn add_stack_loc(&mut self, mem: P<Value>) {
        if vec_utils::find_value(&mut self.stack, mem.clone()).is_none() {
            self.stack.push(mem)
        }
    }
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

    // two entries can intersect only when they have the same temp, or they do not have temps
    pub fn intersect(&mut self, another: &Self) -> bool {
        assert!(
            (!self.has_temp() && !another.has_temp()
            || (self.has_temp() && another.has_temp() && self.get_temp().unwrap() == another.get_temp().unwrap()))
        );

        let mut changed = false;

        // intersect real registers
        if vec_utils::intersect(&mut self.real, &another.real) {
            changed = true;
        }

        // intersect memory
        if vec_utils::intersect(&mut self.stack, &another.stack) {
            changed = true;
        }

        changed
    }
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
}

impl fmt::Display for RegisterEntry {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let temp = match self.temp {
            Some(id) => format!("{}", id),
            None     => "_".to_string()
        };

        let real = format!("{:?}", self.real);
        let stack = format!("{:?}", self.stack);

        write!(f, "| {:20} | {:20} | {:20} |", temp, real, stack)
    }
}