@@ -5,11 +5,11 @@ use syntax_pos::Span;
5
5
6
6
use rustc:: ty:: { self , TyCtxt } ;
7
7
use rustc:: hir:: def_id:: DefId ;
8
- use rustc:: mir:: { self , Body , Location } ;
8
+ use rustc:: mir:: { self , Body , Location , Local } ;
9
9
use rustc_data_structures:: bit_set:: BitSet ;
10
10
use crate :: transform:: { MirPass , MirSource } ;
11
11
12
- use crate :: dataflow:: { do_dataflow, DebugFormatted } ;
12
+ use crate :: dataflow:: { self , do_dataflow, DebugFormatted } ;
13
13
use crate :: dataflow:: MoveDataParamEnv ;
14
14
use crate :: dataflow:: BitDenotation ;
15
15
use crate :: dataflow:: DataflowResults ;
@@ -92,147 +92,114 @@ pub fn sanity_check_via_rustc_peek<'tcx, O>(
92
92
O : BitDenotation < ' tcx , Idx = MovePathIndex > + HasMoveData < ' tcx > ,
93
93
{
94
94
debug ! ( "sanity_check_via_rustc_peek def_id: {:?}" , def_id) ;
95
- // FIXME: this is not DRY. Figure out way to abstract this and
96
- // `dataflow::build_sets`. (But note it is doing non-standard
97
- // stuff, so such generalization may not be realistic.)
98
-
99
- for bb in body. basic_blocks ( ) . indices ( ) {
100
- each_block ( tcx, body, results, bb) ;
101
- }
102
- }
103
-
104
- fn each_block < ' tcx , O > (
105
- tcx : TyCtxt < ' tcx > ,
106
- body : & Body < ' tcx > ,
107
- results : & DataflowResults < ' tcx , O > ,
108
- bb : mir:: BasicBlock ,
109
- ) where
110
- O : BitDenotation < ' tcx , Idx = MovePathIndex > + HasMoveData < ' tcx > ,
111
- {
112
- let move_data = results. 0 . operator . move_data ( ) ;
113
- let mir:: BasicBlockData { ref statements, ref terminator, is_cleanup : _ } = body[ bb] ;
114
-
115
- let ( args, span) = match is_rustc_peek ( tcx, terminator) {
116
- Some ( args_and_span) => args_and_span,
117
- None => return ,
118
- } ;
119
- assert ! ( args. len( ) == 1 ) ;
120
- let peek_arg_place = match args[ 0 ] {
121
- mir:: Operand :: Copy ( ref place @ mir:: Place {
122
- base : mir:: PlaceBase :: Local ( _) ,
123
- projection : None ,
124
- } ) |
125
- mir:: Operand :: Move ( ref place @ mir:: Place {
126
- base : mir:: PlaceBase :: Local ( _) ,
127
- projection : None ,
128
- } ) => Some ( place) ,
129
- _ => None ,
130
- } ;
131
-
132
- let peek_arg_place = match peek_arg_place {
133
- Some ( arg) => arg,
134
- None => {
135
- tcx. sess . diagnostic ( ) . span_err (
136
- span, "dataflow::sanity_check cannot feed a non-temp to rustc_peek." ) ;
137
- return ;
138
- }
139
- } ;
140
-
141
- let mut on_entry = results. 0 . sets . entry_set_for ( bb. index ( ) ) . to_owned ( ) ;
142
- let mut trans = results. 0 . sets . trans_for ( bb. index ( ) ) . clone ( ) ;
143
-
144
- // Emulate effect of all statements in the block up to (but not
145
- // including) the borrow within `peek_arg_place`. Do *not* include
146
- // call to `peek_arg_place` itself (since we are peeking the state
147
- // of the argument at time immediate preceding Call to
148
- // `rustc_peek`).
149
-
150
- for ( j, stmt) in statements. iter ( ) . enumerate ( ) {
151
- debug ! ( "rustc_peek: ({:?},{}) {:?}" , bb, j, stmt) ;
152
- let ( place, rvalue) = match stmt. kind {
153
- mir:: StatementKind :: Assign ( ref place, ref rvalue) => {
154
- ( place, rvalue)
155
- }
156
- mir:: StatementKind :: FakeRead ( ..) |
157
- mir:: StatementKind :: StorageLive ( _) |
158
- mir:: StatementKind :: StorageDead ( _) |
159
- mir:: StatementKind :: InlineAsm { .. } |
160
- mir:: StatementKind :: Retag { .. } |
161
- mir:: StatementKind :: AscribeUserType ( ..) |
162
- mir:: StatementKind :: Nop => continue ,
163
- mir:: StatementKind :: SetDiscriminant { .. } =>
164
- span_bug ! ( stmt. source_info. span,
165
- "sanity_check should run before Deaggregator inserts SetDiscriminant" ) ,
166
- } ;
167
-
168
- if place == peek_arg_place {
169
- if let mir:: Rvalue :: Ref ( _, mir:: BorrowKind :: Shared , ref peeking_at_place) = * * rvalue {
170
- // Okay, our search is over.
171
- match move_data. rev_lookup . find ( peeking_at_place. as_ref ( ) ) {
172
- LookupResult :: Exact ( peek_mpi) => {
173
- let bit_state = on_entry. contains ( peek_mpi) ;
174
- debug ! ( "rustc_peek({:?} = &{:?}) bit_state: {}" ,
175
- place, peeking_at_place, bit_state) ;
176
- if !bit_state {
177
- tcx. sess . span_err ( span, "rustc_peek: bit not set" ) ;
178
- }
179
- }
180
- LookupResult :: Parent ( ..) => {
181
- tcx. sess . span_err ( span, "rustc_peek: argument untracked" ) ;
95
+ let move_data = results. operator ( ) . move_data ( ) ;
96
+
97
+ let peek_calls = body
98
+ . basic_blocks ( )
99
+ . iter_enumerated ( )
100
+ . filter_map ( |( bb, data) | {
101
+ data. terminator
102
+ . as_ref ( )
103
+ . and_then ( |term| PeekCall :: from_terminator ( tcx, term) )
104
+ . map ( |call| ( bb, data, call) )
105
+ } ) ;
106
+
107
+ for ( bb, data, call) in peek_calls {
108
+ // Look for a sequence like the following to indicate that we should be peeking at `_1`:
109
+ // _2 = &_1
110
+ // rustc_peek(_2)
111
+ let ( statement_index, peek_rval) = data. statements
112
+ . iter ( )
113
+ . map ( |stmt| value_assigned_to_local ( stmt, call. arg ) )
114
+ . enumerate ( )
115
+ . filter_map ( |( idx, rval) | rval. map ( |r| ( idx, r) ) )
116
+ . next ( )
117
+ . expect ( "call to rustc_peek should be preceded by \
118
+ assignment to temporary holding its argument") ;
119
+
120
+ if let mir:: Rvalue :: Ref ( _, mir:: BorrowKind :: Shared , peeking_at_place) = peek_rval {
121
+ let loc = Location { block : bb, statement_index } ;
122
+ let flow_state = dataflow:: state_for_location ( loc, results. operator ( ) , results, body) ;
123
+
124
+ match move_data. rev_lookup . find ( peeking_at_place) {
125
+ LookupResult :: Exact ( peek_mpi) => {
126
+ let bit_state = flow_state. contains ( peek_mpi) ;
127
+ debug ! ( "rustc_peek({:?} = &{:?}) bit_state: {}" ,
128
+ call. arg, peeking_at_place, bit_state) ;
129
+ if !bit_state {
130
+ tcx. sess . span_err ( call. span , "rustc_peek: bit not set" ) ;
182
131
}
183
132
}
184
- return ;
185
- } else {
186
- // Our search should have been over, but the input
187
- // does not match expectations of `rustc_peek` for
188
- // this sanity_check.
189
- let msg = "rustc_peek: argument expression \
190
- must be immediate borrow of form `&expr`";
191
- tcx. sess . span_err ( span, msg) ;
133
+ LookupResult :: Parent ( ..) => {
134
+ tcx. sess . span_err ( call. span , "rustc_peek: argument untracked" ) ;
135
+ }
192
136
}
137
+ } else {
138
+ let msg = "rustc_peek: argument expression \
139
+ must be immediate borrow of form `&expr`";
140
+ tcx. sess . span_err ( call. span , msg) ;
193
141
}
142
+ }
143
+ }
194
144
195
- let lhs_mpi = move_data. rev_lookup . find ( place. as_ref ( ) ) ;
196
-
197
- debug ! ( "rustc_peek: computing effect on place: {:?} ({:?}) in stmt: {:?}" ,
198
- place, lhs_mpi, stmt) ;
199
- // reset GEN and KILL sets before emulating their effect.
200
- trans. clear ( ) ;
201
- results. 0 . operator . before_statement_effect (
202
- & mut trans,
203
- Location { block : bb, statement_index : j } ) ;
204
- results. 0 . operator . statement_effect (
205
- & mut trans,
206
- Location { block : bb, statement_index : j } ) ;
207
- trans. apply ( & mut on_entry) ;
145
+ /// If `stmt` is an assignment where the LHS is the given local (with no projections), returns the
146
+ /// RHS of the assignment.
147
+ fn value_assigned_to_local < ' a , ' tcx > (
148
+ stmt : & ' a mir:: Statement < ' tcx > ,
149
+ local : Local ,
150
+ ) -> Option < & ' a mir:: Rvalue < ' tcx > > {
151
+ if let mir:: StatementKind :: Assign ( place, rvalue) = & stmt. kind {
152
+ if let mir:: Place :: Base ( mir:: PlaceBase :: Local ( l) ) = place {
153
+ if local == * l {
154
+ return Some ( & * rvalue) ;
155
+ }
156
+ }
208
157
}
209
158
210
- results. 0 . operator . before_terminator_effect (
211
- & mut trans,
212
- Location { block : bb, statement_index : statements. len ( ) } ) ;
159
+ None
160
+ }
213
161
214
- tcx. sess . span_err ( span, & format ! ( "rustc_peek: MIR did not match \
215
- anticipated pattern; note that \
216
- rustc_peek expects input of \
217
- form `&expr`") ) ;
162
+ struct PeekCall {
163
+ arg : Local ,
164
+ span : Span ,
218
165
}
219
166
220
- fn is_rustc_peek < ' a , ' tcx > (
221
- tcx : TyCtxt < ' tcx > ,
222
- terminator : & ' a Option < mir:: Terminator < ' tcx > > ,
223
- ) -> Option < ( & ' a [ mir:: Operand < ' tcx > ] , Span ) > {
224
- if let Some ( mir:: Terminator { ref kind, source_info, .. } ) = * terminator {
225
- if let mir:: TerminatorKind :: Call { func : ref oper, ref args, .. } = * kind {
226
- if let mir:: Operand :: Constant ( ref func) = * oper {
227
- if let ty:: FnDef ( def_id, _) = func. ty . sty {
228
- let abi = tcx. fn_sig ( def_id) . abi ( ) ;
229
- let name = tcx. item_name ( def_id) ;
230
- if abi == Abi :: RustIntrinsic && name == sym:: rustc_peek {
231
- return Some ( ( args, source_info. span ) ) ;
232
- }
167
+ impl PeekCall {
168
+ fn from_terminator < ' tcx > (
169
+ tcx : TyCtxt < ' tcx > ,
170
+ terminator : & mir:: Terminator < ' tcx > ,
171
+ ) -> Option < Self > {
172
+ let span = terminator. source_info . span ;
173
+ if let mir:: TerminatorKind :: Call { func : mir:: Operand :: Constant ( func) , args, .. } =
174
+ & terminator. kind
175
+ {
176
+ if let ty:: FnDef ( def_id, _) = func. ty . sty {
177
+ let abi = tcx. fn_sig ( def_id) . abi ( ) ;
178
+ let name = tcx. item_name ( def_id) ;
179
+ if abi != Abi :: RustIntrinsic || name != sym:: rustc_peek {
180
+ return None ;
233
181
}
182
+
183
+ assert_eq ! ( args. len( ) , 1 ) ;
184
+ let arg = match args[ 0 ] {
185
+ | mir:: Operand :: Copy ( mir:: Place :: Base ( mir:: PlaceBase :: Local ( local) ) )
186
+ | mir:: Operand :: Move ( mir:: Place :: Base ( mir:: PlaceBase :: Local ( local) ) )
187
+ => local,
188
+
189
+ _ => {
190
+ tcx. sess . diagnostic ( ) . span_err (
191
+ span, "dataflow::sanity_check cannot feed a non-temp to rustc_peek." ) ;
192
+ return None ;
193
+ }
194
+ } ;
195
+
196
+ return Some ( PeekCall {
197
+ arg,
198
+ span,
199
+ } ) ;
234
200
}
235
201
}
202
+
203
+ None
236
204
}
237
- return None ;
238
205
}
0 commit comments