1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
139
140
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* mir/optimise.cpp
* - MIR Optimisations
*
*/
#include "main_bindings.hpp"
#include "mir.hpp"
#include <hir/visitor.hpp>
#include <hir_typeck/static.hpp>
#include <mir/helpers.hpp>
#include <mir/operations.hpp>
#include <mir/visit_crate_mir.hpp>
namespace {
::MIR::BasicBlockId get_new_target(const ::MIR::TypeResolve& state, ::MIR::BasicBlockId bb)
{
const auto& target = state.get_block(bb);
if( target.statements.size() != 0 )
{
return bb;
}
else if( !target.terminator.is_Goto() )
{
return bb;
}
else
{
// Make sure we don't infinite loop
if( bb == target.terminator.as_Goto() )
return bb;
auto rv = get_new_target(state, target.terminator.as_Goto());
DEBUG(bb << " => " << rv);
return rv;
}
}
bool visit_mir_lvalue_mut(::MIR::LValue& lv, bool is_write, ::std::function<bool(::MIR::LValue& , bool)> cb)
{
if( cb(lv, is_write) )
return true;
TU_MATCHA( (lv), (e),
(Variable,
),
(Argument,
),
(Temporary,
),
(Static,
),
(Return,
),
(Field,
return visit_mir_lvalue_mut(*e.val, is_write, cb);
),
(Deref,
return visit_mir_lvalue_mut(*e.val, is_write, cb);
),
(Index,
if( visit_mir_lvalue_mut(*e.val, is_write, cb) )
return true;
return visit_mir_lvalue_mut(*e.idx, false, cb);
),
(Downcast,
return visit_mir_lvalue_mut(*e.val, is_write, cb);
)
)
return false;
}
bool visit_mir_lvalue(const ::MIR::LValue& lv, bool is_write, ::std::function<bool(const ::MIR::LValue& , bool)> cb)
{
return visit_mir_lvalue_mut( const_cast<::MIR::LValue&>(lv), is_write, [&](auto& v, bool im) { return cb(v,im); } );
}
bool visit_mir_lvalues_mut(::MIR::RValue& rval, ::std::function<bool(::MIR::LValue& , bool)> cb)
{
bool rv = false;
TU_MATCHA( (rval), (se),
(Use,
rv |= visit_mir_lvalue_mut(se, false, cb);
),
(Constant,
),
(SizedArray,
rv |= visit_mir_lvalue_mut(se.val, false, cb);
),
(Borrow,
rv |= visit_mir_lvalue_mut(se.val, (se.type != ::HIR::BorrowType::Shared), cb);
),
(Cast,
rv |= visit_mir_lvalue_mut(se.val, false, cb);
),
(BinOp,
rv |= visit_mir_lvalue_mut(se.val_l, false, cb);
rv |= visit_mir_lvalue_mut(se.val_r, false, cb);
),
(UniOp,
rv |= visit_mir_lvalue_mut(se.val, false, cb);
),
(DstMeta,
rv |= visit_mir_lvalue_mut(se.val, false, cb);
),
(DstPtr,
rv |= visit_mir_lvalue_mut(se.val, false, cb);
),
(MakeDst,
// TODO: Would prefer a flag to indicate "move"
rv |= visit_mir_lvalue_mut(se.ptr_val, false, cb);
rv |= visit_mir_lvalue_mut(se.meta_val, false, cb);
),
(Tuple,
for(auto& v : se.vals)
rv |= visit_mir_lvalue_mut(v, false, cb);
),
(Array,
for(auto& v : se.vals)
rv |= visit_mir_lvalue_mut(v, false, cb);
),
(Variant,
rv |= visit_mir_lvalue_mut(se.val, false, cb);
),
(Struct,
for(auto& v : se.vals)
rv |= visit_mir_lvalue_mut(v, false, cb);
)
)
return rv;
}
//bool visit_mir_lvalues(const ::MIR::RValue& rval, ::std::function<bool(const ::MIR::LValue& , bool)> cb)
//{
// return visit_mir_lvalues_mut(const_cast<::MIR::RValue&>(rval), [&](auto& lv, bool im){ return cb(lv, im); });
//}
bool visit_mir_lvalues_mut(::MIR::Statement& stmt, ::std::function<bool(::MIR::LValue& , bool)> cb)
{
bool rv = false;
TU_MATCHA( (stmt), (e),
(Assign,
rv |= visit_mir_lvalues_mut(e.src, cb);
rv |= visit_mir_lvalue_mut(e.dst, true, cb);
),
(Asm,
for(auto& v : e.inputs)
rv |= visit_mir_lvalue_mut(v.second, false, cb);
for(auto& v : e.outputs)
rv |= visit_mir_lvalue_mut(v.second, true, cb);
),
(Drop,
rv |= visit_mir_lvalue_mut(e.slot, false, cb);
)
)
return rv;
}
bool visit_mir_lvalues(const ::MIR::Statement& stmt, ::std::function<bool(const ::MIR::LValue& , bool)> cb)
{
return visit_mir_lvalues_mut(const_cast<::MIR::Statement&>(stmt), [&](auto& lv, bool im){ return cb(lv, im); });
}
void visit_mir_lvalues_mut(::MIR::TypeResolve& state, ::MIR::Function& fcn, ::std::function<bool(::MIR::LValue& , bool)> cb)
{
for(unsigned int block_idx = 0; block_idx < fcn.blocks.size(); block_idx ++)
{
auto& block = fcn.blocks[block_idx];
if( block.terminator.tag() == ::MIR::Terminator::TAGDEAD )
continue ;
for(auto& stmt : block.statements)
{
state.set_cur_stmt(block_idx, (&stmt - &block.statements.front()));
visit_mir_lvalues_mut(stmt, cb);
}
state.set_cur_stmt_term(block_idx);
TU_MATCHA( (block.terminator), (e),
(Incomplete,
),
(Return,
),
(Diverge,
),
(Goto,
),
(Panic,
),
(If,
visit_mir_lvalue_mut(e.cond, false, cb);
),
(Switch,
visit_mir_lvalue_mut(e.val, false, cb);
),
(Call,
if( e.fcn.is_Value() ) {
visit_mir_lvalue_mut(e.fcn.as_Value(), false, cb);
}
for(auto& v : e.args)
visit_mir_lvalue_mut(v, false, cb);
visit_mir_lvalue_mut(e.ret_val, true, cb);
)
)
}
}
void visit_mir_lvalues(::MIR::TypeResolve& state, const ::MIR::Function& fcn, ::std::function<bool(const ::MIR::LValue& , bool)> cb)
{
visit_mir_lvalues_mut(state, const_cast<::MIR::Function&>(fcn), [&](auto& lv, bool im){ return cb(lv, im); });
}
}
void MIR_Optimise(const StaticTraitResolve& resolve, const ::HIR::ItemPath& path, ::MIR::Function& fcn, const ::HIR::Function::args_t& args, const ::HIR::TypeRef& ret_type)
{
static Span sp;
TRACE_FUNCTION_F(path);
::MIR::TypeResolve state { sp, resolve, FMT_CB(ss, ss << path;), ret_type, args, fcn };
// >> Replace targets that point to a block that is just a goto
for(auto& block : fcn.blocks)
{
TU_MATCHA( (block.terminator), (e),
(Incomplete,
),
(Return,
),
(Diverge,
),
(Goto,
e = get_new_target(state, e);
),
(Panic,
),
(If,
e.bb0 = get_new_target(state, e.bb0);
e.bb1 = get_new_target(state, e.bb1);
),
(Switch,
for(auto& target : e.targets)
target = get_new_target(state, target);
),
(Call,
e.ret_block = get_new_target(state, e.ret_block);
e.panic_block = get_new_target(state, e.panic_block);
)
)
}
// >> Merge blocks where a block goto-s to a single-use block.
{
::std::vector<unsigned int> uses( fcn.blocks.size() );
for(auto& block : fcn.blocks)
{
TU_MATCHA( (block.terminator), (e),
(Incomplete,
),
(Return,
),
(Diverge,
),
(Goto,
uses[e] ++;
),
(Panic,
),
(If,
uses[e.bb0] ++;
uses[e.bb1] ++;
),
(Switch,
for(auto& target : e.targets)
uses[target] ++;
),
(Call,
uses[e.ret_block] ++;
uses[e.panic_block] ++;
)
)
}
unsigned int i = 0;
for(auto& block : fcn.blocks)
{
while( block.terminator.is_Goto() )
{
auto tgt = block.terminator.as_Goto();
if( uses[tgt] != 1 )
break ;
DEBUG("Append bb " << tgt << " to bb" << i);
assert( &fcn.blocks[tgt] != &block );
auto src_block = mv$(fcn.blocks[tgt]);
for(auto& stmt : src_block.statements)
block.statements.push_back( mv$(stmt) );
block.terminator = mv$( src_block.terminator );
}
i ++;
}
}
// >> Combine Duplicate Blocks
// TODO:
// >> Propagate dead assignments
// TODO: This requires kowing that doing so has no effect.
// - Can use little heristics like a Call pointing to an assignment of its RV
// - Count the read/write count of a variable, if it's 1,1 then this optimisation is correct.
// - If the count is read=*,write=1 and the write is of an argument, replace with the argument.
struct ValUse {
unsigned int read = 0;
unsigned int write = 0;
};
struct {
::std::vector<ValUse> var_uses;
::std::vector<ValUse> tmp_uses;
void use_lvalue(const ::MIR::LValue& lv, bool is_write) {
TU_MATCHA( (lv), (e),
(Variable,
auto& vu = var_uses[e];
if( is_write )
vu.write += 1;
else
vu.read += 1;
),
(Argument,
),
(Temporary,
auto& vu = tmp_uses[e.idx];
if( is_write )
vu.write += 1;
else
vu.read += 1;
),
(Static,
),
(Return,
),
(Field,
use_lvalue(*e.val, is_write);
),
(Deref,
use_lvalue(*e.val, is_write);
),
(Index,
use_lvalue(*e.val, is_write);
use_lvalue(*e.idx, false);
),
(Downcast,
use_lvalue(*e.val, is_write);
)
)
}
void read_lvalue(const ::MIR::LValue& lv) {
use_lvalue(lv, false);
}
void write_lvalue(const ::MIR::LValue& lv) {
use_lvalue(lv, true);
}
} val_uses = {
::std::vector<ValUse>(fcn.named_variables.size()),
::std::vector<ValUse>(fcn.temporaries.size())
};
visit_mir_lvalues(state, fcn, [&](const auto& lv, bool im){ val_uses.use_lvalue(lv, im); return false; });
// Rules:
// - Find an assignment `tmp = Use(...)` where the temporary is only written and read once
// - Stop on any conditional terminator
// - Any lvalues in the source lvalue must not be mutated between the source assignment and the usage.
// > This includes mutation, borrowing, or moving.
{
::std::map< unsigned int, ::MIR::LValue> replacements;
for(auto& block : fcn.blocks)
{
if( block.terminator.tag() == ::MIR::Terminator::TAGDEAD )
continue ;
//FOREACH_IDX(stmt_idx, block.statements)
for(unsigned int stmt_idx = 0; stmt_idx < block.statements.size(); stmt_idx ++)
{
const auto& stmt = block.statements[stmt_idx];
// > Assignment
if( ! stmt.is_Assign() )
continue ;
const auto& e = stmt.as_Assign();
// > Of a temporary from with a RValue::Use
// TODO: Variables too.
// TODO: Allow any rvalue type (if the usage is a RValue::Use)
if( !( e.dst.is_Temporary() && e.src.is_Use() ) )
continue ;
auto idx = e.dst.as_Temporary().idx;
const auto& vu = val_uses.tmp_uses[idx];
// > Where the temporary is written once and read once
if( !( vu.read == 1 && vu.write == 1 ) )
continue ;
// Get the root value(s) of the source
// TODO: Handle more complex values. (but don't bother for really complex values?)
const auto& src = e.src.as_Use();
if( !( src.is_Temporary() || src.is_Variable() ) )
continue ;
bool src_is_lvalue = true;
auto is_lvalue_usage = [&](const auto& lv, bool ){ return lv == e.dst; };
auto is_lvalue_in_val = [&](const auto& lv) {
return visit_mir_lvalue(src, true, [&](const auto& slv, bool ) { return lv == slv; });
};
// Eligable for replacement
// Find where this value is used
// - Stop on a conditional block terminator
// - Stop if any value mentioned in the source is mutated/invalidated
bool stop = false;
bool found = false;
for(unsigned int si2 = stmt_idx+1; si2 < block.statements.size(); si2 ++)
{
const auto& stmt2 = block.statements[si2];
// Usage found.
if( visit_mir_lvalues(stmt2, is_lvalue_usage) )
{
// TODO: If the source isn't a Use, ensure that this is a Use
if( !src_is_lvalue )
{
if( stmt2.is_Assign() && stmt2.as_Assign().src.is_Use() ) {
// Good
}
else {
// Bad, this has to stay a temporary
stop = true;
break;
}
}
found = true;
stop = true;
break;
}
// Determine if source is mutated.
// > Assume that any mutating access of the root value counts (over-cautious)
if( visit_mir_lvalues(block.statements[si2], [&](const auto& lv, bool is_write){ return is_write && is_lvalue_in_val(lv); }) )
{
stop = true;
break;
}
}
if( !stop )
{
TU_MATCHA( (block.terminator), (e),
(Incomplete,
),
(Return,
),
(Diverge,
),
(Goto,
// TODO: Chain.
),
(Panic,
),
(If,
if( src_is_lvalue && visit_mir_lvalue(e.cond, false, is_lvalue_usage) )
found = true;
stop = true;
),
(Switch,
if( src_is_lvalue && visit_mir_lvalue(e.val, false, is_lvalue_usage) )
found = true;
stop = true;
),
(Call,
if( e.fcn.is_Value() )
if( src_is_lvalue && visit_mir_lvalue(e.fcn.as_Value(), false, is_lvalue_usage) )
found = true;
for(const auto& v : e.args)
if( src_is_lvalue && visit_mir_lvalue(v, false, is_lvalue_usage) )
found = true;
stop = true;
)
)
}
// Schedule a replacement in a future pass
if( found )
{
DEBUG("> Replace " << e.dst << " with " << e.src.as_Use());
replacements.insert( ::std::make_pair(idx, e.src.as_Use().clone()) );
}
else
{
DEBUG("- Single-write/read " << e.dst << " not replaced - couldn't find usage");
}
} // for(stmt : block.statements)
// If the terminator is a call that writes to a 1:1 value, replace the destination value with the eventual destination (if that value isn't used in the meantime)
if( block.terminator.is_Call() )
{
auto& e = block.terminator.as_Call();
// TODO: Support variables too?
if( !e.ret_val.is_Temporary() )
continue ;
const auto& vu = val_uses.tmp_uses[e.ret_val.as_Temporary().idx];
if( !( vu.read == 1 && vu.write == 1 ) )
continue ;
// Iterate the target block, looking for where this value is used.
const ::MIR::LValue* new_dst = nullptr;
auto& blk2 = fcn.blocks.at(e.ret_block);
for(const auto& stmt : blk2.statements)
{
if( stmt.is_Assign() && stmt.as_Assign().src.is_Use() && stmt.as_Assign().src.as_Use() == e.ret_val ) {
new_dst = &stmt.as_Assign().dst;
break;
}
}
// Ensure that the new destination value isn't used before assignment
if( new_dst )
{
auto lvalue_impacts_dst = [&](const ::MIR::LValue& lv) {
return visit_mir_lvalue(*new_dst, true, [&](const auto& slv, bool ) { return lv == slv; });
};
for(auto it = blk2.statements.begin(); it != blk2.statements.end(); ++ it)
{
const auto& stmt = *it;
if( stmt.is_Assign() && stmt.as_Assign().src.is_Use() && stmt.as_Assign().src.as_Use() == e.ret_val )
{
DEBUG("- Replace function return " << e.ret_val << " with " << *new_dst);
e.ret_val = new_dst->clone();
it = blk2.statements.erase(it);
break;
}
if( visit_mir_lvalues(stmt, [&](const auto& lv, bool is_write){ return lv == *new_dst || (is_write && lvalue_impacts_dst(lv)); }) )
{
break;
}
}
}
}
}
for(;;)
{
unsigned int inner_replaced_count = 0;
for(auto& r : replacements)
{
visit_mir_lvalue_mut(r.second, false, [&](auto& lv, bool is_write) {
if( lv.is_Temporary() && !is_write )
{
auto idx = lv.as_Temporary().idx;
auto it = replacements.find(idx);
if( it != replacements.end() )
{
lv = it->second.clone();
inner_replaced_count ++;
}
}
return false;
});
}
if( inner_replaced_count == 0 )
break;
}
// Apply replacements
unsigned int replaced = 0;
while( replaced < replacements.size() )
{
auto old_replaced = replaced;
visit_mir_lvalues_mut(state, fcn, [&](auto& lv, bool is_write){
if( lv.is_Temporary() && !is_write )
{
auto idx = lv.as_Temporary().idx;
auto it = replacements.find(idx);
if( it != replacements.end() )
{
MIR_ASSERT(state, it->second.tag() != ::MIR::LValue::TAGDEAD, "Replacement of " << lv << " fired twice");
lv = ::std::move(it->second);
replaced += 1;
}
}
return false;
});
MIR_ASSERT(state, replaced > old_replaced, "Temporary eliminations didn't advance");
}
// Remove assignments of replaced values
for(auto& block : fcn.blocks)
{
for(auto it = block.statements.begin(); it != block.statements.end(); )
{
// If the statement was an assign of a replaced temporary, remove it.
if( it->is_Assign() && it->as_Assign().dst.is_Temporary() && replacements.count( it->as_Assign().dst.as_Temporary().idx ) > 0 )
it = block.statements.erase(it);
else
++it;
}
}
}
// GC pass on blocks and variables
// - Find unused blocks, then delete and rewrite all references.
{
::std::vector<bool> used_temps( fcn.temporaries.size() );
::std::vector<bool> visited( fcn.blocks.size() );
::std::vector< ::MIR::BasicBlockId> to_visit;
to_visit.push_back( 0 );
while( to_visit.size() > 0 )
{
auto bb = to_visit.back(); to_visit.pop_back();
visited[bb] = true;
const auto& block = fcn.blocks[bb];
auto assigned_lval = [&](const ::MIR::LValue& lv) {
if( lv.is_Temporary() )
used_temps[lv.as_Temporary().idx] = true;
};
for(const auto& stmt : block.statements)
{
TU_IFLET( ::MIR::Statement, stmt, Assign, e,
assigned_lval(e.dst);
)
}
TU_MATCHA( (block.terminator), (e),
(Incomplete,
),
(Return,
),
(Diverge,
),
(Goto,
if( !visited[e] )
to_visit.push_back(e);
),
(Panic,
),
(If,
if( !visited[e.bb0] )
to_visit.push_back(e.bb0);
if( !visited[e.bb1] )
to_visit.push_back(e.bb1);
),
(Switch,
for(auto& target : e.targets)
if( !visited[target] )
to_visit.push_back(target);
),
(Call,
if( !visited[e.ret_block] )
to_visit.push_back(e.ret_block);
if( !visited[e.panic_block] )
to_visit.push_back(e.panic_block);
assigned_lval(e.ret_val);
)
)
}
::std::vector<unsigned int> block_rewrite_table;
for(unsigned int i = 0, j = 0; i < fcn.blocks.size(); i ++)
{
block_rewrite_table.push_back( visited[i] ? j ++ : ~0u );
}
::std::vector<unsigned int> temp_rewrite_table;
unsigned int n_temp = fcn.temporaries.size();
for(unsigned int i = 0, j = 0; i < n_temp; i ++)
{
if( !used_temps[i] )
{
DEBUG("GC Temporary(" << i << ")");
fcn.temporaries.erase(fcn.temporaries.begin() + j);
}
temp_rewrite_table.push_back( used_temps[i] ? j ++ : ~0u );
}
auto it = fcn.blocks.begin();
for(unsigned int i = 0; i < visited.size(); i ++)
{
if( !visited[i] )
{
// Delete
DEBUG("GC bb" << i);
it = fcn.blocks.erase(it);
}
else
{
auto lvalue_cb = [&](auto& lv, bool ) {
if( lv.is_Temporary() ) {
auto& e = lv.as_Temporary();
MIR_ASSERT(state, e.idx < temp_rewrite_table.size(), "Temporary out of range - " << lv);
MIR_ASSERT(state, temp_rewrite_table.at(e.idx) != ~0u, "LValue " << lv << " incorrectly marked as unused");
e.idx = temp_rewrite_table.at(e.idx);
}
return false;
};
unsigned int stmt_idx = 0;
for(auto& stmt : it->statements)
{
state.set_cur_stmt(i, stmt_idx);
visit_mir_lvalues_mut(stmt, lvalue_cb);
stmt_idx ++;
}
state.set_cur_stmt_term(i);
// Rewrite and advance
TU_MATCHA( (it->terminator), (e),
(Incomplete,
),
(Return,
),
(Diverge,
),
(Goto,
e = block_rewrite_table[e];
),
(Panic,
),
(If,
visit_mir_lvalue_mut(e.cond, false, lvalue_cb);
e.bb0 = block_rewrite_table[e.bb0];
e.bb1 = block_rewrite_table[e.bb1];
),
(Switch,
visit_mir_lvalue_mut(e.val, false, lvalue_cb);
for(auto& target : e.targets)
target = block_rewrite_table[target];
),
(Call,
if( e.fcn.is_Value() ) {
visit_mir_lvalue_mut(e.fcn.as_Value(), false, lvalue_cb);
}
for(auto& v : e.args)
visit_mir_lvalue_mut(v, false, lvalue_cb);
visit_mir_lvalue_mut(e.ret_val, true, lvalue_cb);
e.ret_block = block_rewrite_table[e.ret_block];
e.panic_block = block_rewrite_table[e.panic_block];
)
)
++it;
}
}
}
}
void MIR_OptimiseCrate(::HIR::Crate& crate)
{
::MIR::OuterVisitor ov { crate, [](const auto& res, const auto& p, auto& expr, const auto& args, const auto& ty)
{
MIR_Optimise(res, p, *expr.m_mir, args, ty);
}
};
ov.visit_crate(crate);
}
|