|
| 1 | +// Copyright 2015 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. |
| 4 | +// |
| 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. |
| 10 | + |
| 11 | +// This pass converts move out from array by Subslice and |
| 12 | +// ConstIndex{.., from_end: true} to ConstIndex move out(s) from begin |
| 13 | +// of array. It allows detect error by mir borrowck and elaborate |
| 14 | +// drops for array without additional work. |
| 15 | +// |
| 16 | +// Example: |
| 17 | +// |
| 18 | +// let a = [ box 1,box 2, box 3]; |
| 19 | +// if b { |
| 20 | +// let [_a.., _] = a; |
| 21 | +// } else { |
| 22 | +// let [.., _b] = a; |
| 23 | +// } |
| 24 | +// |
| 25 | +// mir statement _10 = move _2[:-1]; replaced by: |
| 26 | +// StorageLive(_12); |
| 27 | +// _12 = move _2[0 of 3]; |
| 28 | +// StorageLive(_13); |
| 29 | +// _13 = move _2[1 of 3]; |
| 30 | +// _10 = [move _12, move _13] |
| 31 | +// StorageDead(_12); |
| 32 | +// StorageDead(_13); |
| 33 | +// |
| 34 | +// and mir statement _11 = move _2[-1 of 1]; replaced by: |
| 35 | +// _11 = move _2[2 of 3]; |
| 36 | +// |
| 37 | +// FIXME: convert to Subslice back for performance reason |
| 38 | +// FIXME: integrate this transformation to the mir build |
| 39 | + |
| 40 | +use rustc::ty; |
| 41 | +use rustc::ty::TyCtxt; |
| 42 | +use rustc::mir::*; |
| 43 | +use rustc::mir::visit::Visitor; |
| 44 | +use transform::{MirPass, MirSource}; |
| 45 | +use util::patch::MirPatch; |
| 46 | + |
| 47 | +pub struct UniformArrayMoveOut; |
| 48 | + |
| 49 | +impl MirPass for UniformArrayMoveOut { |
| 50 | + fn run_pass<'a, 'tcx>(&self, |
| 51 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 52 | + _src: MirSource, |
| 53 | + mir: &mut Mir<'tcx>) { |
| 54 | + let mut patch = MirPatch::new(mir); |
| 55 | + { |
| 56 | + let mut visitor = UniformArrayMoveOutVisitor{mir, patch: &mut patch, tcx}; |
| 57 | + visitor.visit_mir(mir); |
| 58 | + } |
| 59 | + patch.apply(mir); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +struct UniformArrayMoveOutVisitor<'a, 'tcx: 'a> { |
| 64 | + mir: &'a Mir<'tcx>, |
| 65 | + patch: &'a mut MirPatch<'tcx>, |
| 66 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 67 | +} |
| 68 | + |
| 69 | +impl<'a, 'tcx> Visitor<'tcx> for UniformArrayMoveOutVisitor<'a, 'tcx> { |
| 70 | + fn visit_statement(&mut self, |
| 71 | + block: BasicBlock, |
| 72 | + statement: &Statement<'tcx>, |
| 73 | + location: Location) { |
| 74 | + if let StatementKind::Assign(ref dst_place, |
| 75 | + Rvalue::Use(Operand::Move(ref src_place))) = statement.kind { |
| 76 | + if let Place::Projection(ref proj) = *src_place { |
| 77 | + if let ProjectionElem::ConstantIndex{offset: _, |
| 78 | + min_length: _, |
| 79 | + from_end: false} = proj.elem { |
| 80 | + // no need to transformation |
| 81 | + } else { |
| 82 | + let place_ty = proj.base.ty(self.mir, self.tcx).to_ty(self.tcx); |
| 83 | + if let ty::TyArray(item_ty, const_size) = place_ty.sty { |
| 84 | + if let Some(size) = const_size.val.to_const_int().and_then(|v| v.to_u64()) { |
| 85 | + assert!(size <= (u32::max_value() as u64), |
| 86 | + "unform array move out doesn't supported |
| 87 | + for array bigger then u32"); |
| 88 | + self.uniform(location, dst_place, proj, item_ty, size as u32); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return self.super_statement(block, statement, location); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl<'a, 'tcx> UniformArrayMoveOutVisitor<'a, 'tcx> { |
| 100 | + fn uniform(&mut self, |
| 101 | + location: Location, |
| 102 | + dst_place: &Place<'tcx>, |
| 103 | + proj: &PlaceProjection<'tcx>, |
| 104 | + item_ty: &'tcx ty::TyS<'tcx>, |
| 105 | + size: u32) { |
| 106 | + match proj.elem { |
| 107 | + // uniform _10 = move _2[:-1]; |
| 108 | + ProjectionElem::Subslice{from, to} => { |
| 109 | + self.patch.make_nop(location); |
| 110 | + let temps : Vec<_> = (from..(size-to)).map(|i| { |
| 111 | + let temp = self.patch.new_temp(item_ty, self.mir.source_info(location).span); |
| 112 | + self.patch.add_statement(location, StatementKind::StorageLive(temp)); |
| 113 | + self.patch.add_assign(location, |
| 114 | + Place::Local(temp), |
| 115 | + Rvalue::Use( |
| 116 | + Operand::Move( |
| 117 | + Place::Projection(box PlaceProjection{ |
| 118 | + base: proj.base.clone(), |
| 119 | + elem: ProjectionElem::ConstantIndex{ |
| 120 | + offset: i, |
| 121 | + min_length: size, |
| 122 | + from_end: false} |
| 123 | + })))); |
| 124 | + temp |
| 125 | + }).collect(); |
| 126 | + self.patch.add_assign(location, |
| 127 | + dst_place.clone(), |
| 128 | + Rvalue::Aggregate(box AggregateKind::Array(item_ty), |
| 129 | + temps.iter().map( |
| 130 | + |x| Operand::Move(Place::Local(*x))).collect() |
| 131 | + )); |
| 132 | + for temp in temps { |
| 133 | + self.patch.add_statement(location, StatementKind::StorageDead(temp)); |
| 134 | + } |
| 135 | + } |
| 136 | + // _11 = move _2[-1 of 1]; |
| 137 | + ProjectionElem::ConstantIndex{offset, min_length: _, from_end: true} => { |
| 138 | + self.patch.make_nop(location); |
| 139 | + self.patch.add_assign(location, |
| 140 | + dst_place.clone(), |
| 141 | + Rvalue::Use( |
| 142 | + Operand::Move( |
| 143 | + Place::Projection(box PlaceProjection{ |
| 144 | + base: proj.base.clone(), |
| 145 | + elem: ProjectionElem::ConstantIndex{ |
| 146 | + offset: size - offset, |
| 147 | + min_length: size, |
| 148 | + from_end: false }})))); |
| 149 | + } |
| 150 | + _ => {} |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments