@@ -149,49 +149,46 @@ pub fn run_passes(
149
149
tcx : TyCtxt < ' tcx > ,
150
150
body : & mut Body < ' tcx > ,
151
151
instance : InstanceDef < ' tcx > ,
152
+ promoted : Option < Promoted > ,
152
153
mir_phase : MirPhase ,
153
154
passes : & [ & dyn MirPass < ' tcx > ] ,
154
155
) {
155
156
let phase_index = mir_phase. phase_index ( ) ;
156
157
157
- let run_passes = |body : & mut Body < ' tcx > , promoted| {
158
- if body. phase >= mir_phase {
159
- return ;
160
- }
158
+ if body. phase >= mir_phase {
159
+ return ;
160
+ }
161
161
162
- let source = MirSource {
163
- instance,
164
- promoted,
165
- } ;
166
- let mut index = 0 ;
167
- let mut run_pass = |pass : & dyn MirPass < ' tcx > | {
168
- let run_hooks = |body : & _ , index, is_after| {
169
- dump_mir:: on_mir_pass ( tcx, & format_args ! ( "{:03}-{:03}" , phase_index, index) ,
170
- & pass. name ( ) , source, body, is_after) ;
171
- } ;
172
- run_hooks ( body, index, false ) ;
173
- pass. run_pass ( tcx, source, body) ;
174
- run_hooks ( body, index, true ) ;
175
-
176
- index += 1 ;
162
+ let source = MirSource {
163
+ instance,
164
+ promoted,
165
+ } ;
166
+ let mut index = 0 ;
167
+ let mut run_pass = |pass : & dyn MirPass < ' tcx > | {
168
+ let run_hooks = |body : & _ , index, is_after| {
169
+ dump_mir:: on_mir_pass ( tcx, & format_args ! ( "{:03}-{:03}" , phase_index, index) ,
170
+ & pass. name ( ) , source, body, is_after) ;
177
171
} ;
172
+ run_hooks ( body, index, false ) ;
173
+ pass. run_pass ( tcx, source, body) ;
174
+ run_hooks ( body, index, true ) ;
178
175
179
- for pass in passes {
180
- run_pass ( * pass) ;
181
- }
182
-
183
- body. phase = mir_phase;
176
+ index += 1 ;
184
177
} ;
185
178
186
- run_passes ( body, None ) ;
179
+ for pass in passes {
180
+ run_pass ( * pass) ;
181
+ }
182
+
183
+ body. phase = mir_phase;
187
184
}
188
185
189
186
fn mir_const ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> & Steal < Body < ' _ > > {
190
187
// Unsafety check uses the raw mir, so make sure it is run
191
188
let _ = tcx. unsafety_check_result ( def_id) ;
192
189
193
190
let mut body = tcx. mir_built ( def_id) . steal ( ) ;
194
- run_passes ( tcx, & mut body, InstanceDef :: Item ( def_id) , MirPhase :: Const , & [
191
+ run_passes ( tcx, & mut body, InstanceDef :: Item ( def_id) , None , MirPhase :: Const , & [
195
192
// What we need to do constant evaluation.
196
193
& simplify:: SimplifyCfg :: new ( "initial" ) ,
197
194
& rustc_peek:: SanityCheck ,
@@ -213,7 +210,7 @@ fn mir_validated(
213
210
214
211
let mut body = tcx. mir_const ( def_id) . steal ( ) ;
215
212
let qualify_and_promote_pass = qualify_consts:: QualifyAndPromoteConstants :: default ( ) ;
216
- run_passes ( tcx, & mut body, InstanceDef :: Item ( def_id) , MirPhase :: Validated , & [
213
+ run_passes ( tcx, & mut body, InstanceDef :: Item ( def_id) , None , MirPhase :: Validated , & [
217
214
// What we need to run borrowck etc.
218
215
& qualify_and_promote_pass,
219
216
& simplify:: SimplifyCfg :: new ( "qualify-consts" ) ,
@@ -222,26 +219,13 @@ fn mir_validated(
222
219
( tcx. alloc_steal_mir ( body) , tcx. alloc_steal_promoted ( promoted) )
223
220
}
224
221
225
- fn optimized_mir ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> & Body < ' _ > {
226
- if tcx. is_constructor ( def_id) {
227
- // There's no reason to run all of the MIR passes on constructors when
228
- // we can just output the MIR we want directly. This also saves const
229
- // qualification and borrow checking the trouble of special casing
230
- // constructors.
231
- return shim:: build_adt_ctor ( tcx, def_id) ;
232
- }
233
-
234
- // (Mir-)Borrowck uses `mir_validated`, so we have to force it to
235
- // execute before we can steal.
236
- tcx. ensure ( ) . mir_borrowck ( def_id) ;
237
-
238
- if tcx. use_ast_borrowck ( ) {
239
- tcx. ensure ( ) . borrowck ( def_id) ;
240
- }
241
-
242
- let ( body, _) = tcx. mir_validated ( def_id) ;
243
- let mut body = body. steal ( ) ;
244
- run_passes ( tcx, & mut body, InstanceDef :: Item ( def_id) , MirPhase :: Optimized , & [
222
+ fn run_optimization_passes < ' tcx > (
223
+ tcx : TyCtxt < ' tcx > ,
224
+ body : & mut Body < ' tcx > ,
225
+ def_id : DefId ,
226
+ promoted : Option < Promoted > ,
227
+ ) {
228
+ run_passes ( tcx, body, InstanceDef :: Item ( def_id) , promoted, MirPhase :: Optimized , & [
245
229
// Remove all things only needed by analysis
246
230
& no_landing_pads:: NoLandingPads ,
247
231
& simplify_branches:: SimplifyBranches :: new ( "initial" ) ,
@@ -292,6 +276,28 @@ fn optimized_mir(tcx: TyCtxt<'_>, def_id: DefId) -> &Body<'_> {
292
276
& add_call_guards:: CriticalCallEdges ,
293
277
& dump_mir:: Marker ( "PreCodegen" ) ,
294
278
] ) ;
279
+ }
280
+
281
+ fn optimized_mir ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> & Body < ' _ > {
282
+ if tcx. is_constructor ( def_id) {
283
+ // There's no reason to run all of the MIR passes on constructors when
284
+ // we can just output the MIR we want directly. This also saves const
285
+ // qualification and borrow checking the trouble of special casing
286
+ // constructors.
287
+ return shim:: build_adt_ctor ( tcx, def_id) ;
288
+ }
289
+
290
+ // (Mir-)Borrowck uses `mir_validated`, so we have to force it to
291
+ // execute before we can steal.
292
+ tcx. ensure ( ) . mir_borrowck ( def_id) ;
293
+
294
+ if tcx. use_ast_borrowck ( ) {
295
+ tcx. ensure ( ) . borrowck ( def_id) ;
296
+ }
297
+
298
+ let ( body, _) = tcx. mir_validated ( def_id) ;
299
+ let mut body = body. steal ( ) ;
300
+ run_optimization_passes ( tcx, & mut body, def_id, None ) ;
295
301
tcx. arena . alloc ( body)
296
302
}
297
303
@@ -304,57 +310,8 @@ fn promoted_mir<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx IndexVec<Promot
304
310
let ( _, promoted) = tcx. mir_validated ( def_id) ;
305
311
let mut promoted = promoted. steal ( ) ;
306
312
307
- for mut body in promoted. iter_mut ( ) {
308
- run_passes ( tcx, & mut body, InstanceDef :: Item ( def_id) , MirPhase :: Optimized , & [
309
- // Remove all things only needed by analysis
310
- & no_landing_pads:: NoLandingPads ,
311
- & simplify_branches:: SimplifyBranches :: new ( "initial" ) ,
312
- & remove_noop_landing_pads:: RemoveNoopLandingPads ,
313
- & cleanup_post_borrowck:: CleanupNonCodegenStatements ,
314
-
315
- & simplify:: SimplifyCfg :: new ( "early-opt" ) ,
316
-
317
- // These next passes must be executed together
318
- & add_call_guards:: CriticalCallEdges ,
319
- & elaborate_drops:: ElaborateDrops ,
320
- & no_landing_pads:: NoLandingPads ,
321
- // AddMovesForPackedDrops needs to run after drop
322
- // elaboration.
323
- & add_moves_for_packed_drops:: AddMovesForPackedDrops ,
324
- // AddRetag needs to run after ElaborateDrops, and it needs
325
- // an AllCallEdges pass right before it. Otherwise it should
326
- // run fairly late, but before optimizations begin.
327
- & add_call_guards:: AllCallEdges ,
328
- & add_retag:: AddRetag ,
329
-
330
- & simplify:: SimplifyCfg :: new ( "elaborate-drops" ) ,
331
-
332
- // No lifetime analysis based on borrowing can be done from here on out.
333
-
334
- // From here on out, regions are gone.
335
- & erase_regions:: EraseRegions ,
336
-
337
- // Optimizations begin.
338
- & uniform_array_move_out:: RestoreSubsliceArrayMoveOut ,
339
- & inline:: Inline ,
340
-
341
- // Lowering generator control-flow and variables
342
- // has to happen before we do anything else to them.
343
- & generator:: StateTransform ,
344
-
345
- & instcombine:: InstCombine ,
346
- & const_prop:: ConstProp ,
347
- & simplify_branches:: SimplifyBranches :: new ( "after-const-prop" ) ,
348
- & deaggregator:: Deaggregator ,
349
- & copy_prop:: CopyPropagation ,
350
- & simplify_branches:: SimplifyBranches :: new ( "after-copy-prop" ) ,
351
- & remove_noop_landing_pads:: RemoveNoopLandingPads ,
352
- & simplify:: SimplifyCfg :: new ( "final" ) ,
353
- & simplify:: SimplifyLocals ,
354
-
355
- & add_call_guards:: CriticalCallEdges ,
356
- & dump_mir:: Marker ( "PreCodegen" ) ,
357
- ] ) ;
313
+ for ( p, mut body) in promoted. iter_enumerated_mut ( ) {
314
+ run_optimization_passes ( tcx, & mut body, def_id, Some ( p) ) ;
358
315
}
359
316
360
317
tcx. intern_promoted ( promoted)
0 commit comments