-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathphi_resolver.cc
More file actions
45 lines (37 loc) · 1.26 KB
/
phi_resolver.cc
File metadata and controls
45 lines (37 loc) · 1.26 KB
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
//
// phi_resolver.cc
// Katara
//
// Created by Arne Philipeit on 2/23/20.
// Copyright © 2020 Arne Philipeit. All rights reserved.
//
#include "phi_resolver.h"
namespace ir_processors {
namespace {
void ResolvePhisInBlock(ir::Func* func, ir::Block* block) {
int phi_count = 0;
for (const auto& instr : block->instrs()) {
if (instr->instr_kind() != ir::InstrKind::kPhi) {
break;
}
phi_count++;
const ir::PhiInstr* phi_instr = static_cast<ir::PhiInstr*>(instr.get());
std::shared_ptr<ir::Computed> destination = phi_instr->result();
for (const auto& inherited_value : phi_instr->args()) {
std::shared_ptr<ir::Value> source = inherited_value->value();
ir::block_num_t origin = inherited_value->origin();
ir::Block* parent = func->GetBlock(origin);
// Insert Mov before last (control flow) instruction:
parent->instrs().insert(parent->instrs().end() - 1,
std::make_unique<ir::MovInstr>(destination, source));
}
}
block->instrs().erase(block->instrs().begin(), block->instrs().begin() + phi_count);
}
} // namespace
void ResolvePhisInFunc(ir::Func* func) {
for (auto& block : func->blocks()) {
ResolvePhisInBlock(func, block.get());
}
}
} // namespace ir_processors