1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use rustc::mir::{Local, Location, Lvalue, Mir};
14 use rustc::mir::visit::{LvalueContext, MutVisitor, Visitor};
15 use rustc_data_structures::indexed_vec::IndexVec;
16 use std::marker::PhantomData;
19 pub struct DefUseAnalysis<'tcx> {
20 info: IndexVec<Local, Info<'tcx>>,
24 pub struct Info<'tcx> {
25 pub defs_and_uses: Vec<Use<'tcx>>,
29 pub struct Use<'tcx> {
30 pub context: LvalueContext<'tcx>,
31 pub location: Location,
34 impl<'tcx> DefUseAnalysis<'tcx> {
35 pub fn new(mir: &Mir<'tcx>) -> DefUseAnalysis<'tcx> {
37 info: IndexVec::from_elem_n(Info::new(), mir.local_decls.len()),
41 pub fn analyze(&mut self, mir: &Mir<'tcx>) {
42 let mut finder = DefUseFinder {
43 info: mem::replace(&mut self.info, IndexVec::new()),
45 finder.visit_mir(mir);
46 self.info = finder.info
49 pub fn local_info(&self, local: Local) -> &Info<'tcx> {
53 fn mutate_defs_and_uses<F>(&self, local: Local, mir: &mut Mir<'tcx>, mut callback: F)
54 where F: for<'a> FnMut(&'a mut Lvalue<'tcx>,
57 for lvalue_use in &self.info[local].defs_and_uses {
58 MutateUseVisitor::new(local,
60 mir).visit_location(mir, lvalue_use.location)
64 /// FIXME(pcwalton): This should update the def-use chains.
65 pub fn replace_all_defs_and_uses_with(&self,
68 new_lvalue: Lvalue<'tcx>) {
69 self.mutate_defs_and_uses(local, mir, |lvalue, _, _| *lvalue = new_lvalue.clone())
73 struct DefUseFinder<'tcx> {
74 info: IndexVec<Local, Info<'tcx>>,
77 impl<'tcx> DefUseFinder<'tcx> {
78 fn lvalue_mut_info(&mut self, lvalue: &Lvalue<'tcx>) -> Option<&mut Info<'tcx>> {
79 let info = &mut self.info;
81 if let Lvalue::Local(local) = *lvalue {
82 Some(&mut info[local])
89 impl<'tcx> Visitor<'tcx> for DefUseFinder<'tcx> {
90 fn visit_lvalue(&mut self,
91 lvalue: &Lvalue<'tcx>,
92 context: LvalueContext<'tcx>,
94 if let Some(ref mut info) = self.lvalue_mut_info(lvalue) {
95 info.defs_and_uses.push(Use {
100 self.super_lvalue(lvalue, context, location)
104 impl<'tcx> Info<'tcx> {
105 fn new() -> Info<'tcx> {
107 defs_and_uses: vec![],
111 pub fn def_count(&self) -> usize {
112 self.defs_and_uses.iter().filter(|lvalue_use| lvalue_use.context.is_mutating_use()).count()
115 pub fn def_count_not_including_drop(&self) -> usize {
116 self.defs_and_uses.iter().filter(|lvalue_use| {
117 lvalue_use.context.is_mutating_use() && !lvalue_use.context.is_drop()
121 pub fn use_count(&self) -> usize {
122 self.defs_and_uses.iter().filter(|lvalue_use| {
123 lvalue_use.context.is_nonmutating_use()
128 struct MutateUseVisitor<'tcx, F> {
131 phantom: PhantomData<&'tcx ()>,
134 impl<'tcx, F> MutateUseVisitor<'tcx, F> {
135 fn new(query: Local, callback: F, _: &Mir<'tcx>)
136 -> MutateUseVisitor<'tcx, F>
137 where F: for<'a> FnMut(&'a mut Lvalue<'tcx>, LvalueContext<'tcx>, Location) {
141 phantom: PhantomData,
146 impl<'tcx, F> MutVisitor<'tcx> for MutateUseVisitor<'tcx, F>
147 where F: for<'a> FnMut(&'a mut Lvalue<'tcx>, LvalueContext<'tcx>, Location) {
148 fn visit_lvalue(&mut self,
149 lvalue: &mut Lvalue<'tcx>,
150 context: LvalueContext<'tcx>,
151 location: Location) {
152 if let Lvalue::Local(local) = *lvalue {
153 if local == self.query {
154 (self.callback)(lvalue, context, location)
157 self.super_lvalue(lvalue, context, location)