Skip to content

Commit

Permalink
Updated testing system in loop analysis and GCM
Browse files Browse the repository at this point in the history
+ Fixed bug in loop analysis
  • Loading branch information
Mikhail Kaskov authored and techie-mike committed Dec 18, 2023
1 parent f2fd196 commit a7be06b
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 36 deletions.
4 changes: 0 additions & 4 deletions src/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ void Graph::DumpPlacedInsts(std::ostream &out) {
for (Inst *inst = region->GetFirst(); inst != nullptr; inst = inst->GetNext()) {
inst->Dump(out);
}
if (region->GetOpcode() != Opcode::End) {
ASSERT(region->GetExitFromRegion());
region->GetExitFromRegion()->Dump(out);
}
out << "----------------------------\n";
first = false;
}
Expand Down
12 changes: 12 additions & 0 deletions src/inst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ void RegionInst::PushBackInst(Inst *inst) {
last_ = inst;
}

void RegionInst::PushFrontInst(Inst *inst) {
inst->SetPlaced();
if (first_ == nullptr) {
AddFirstInst(inst);
return;
}

first_->SetPrev(inst);
inst->SetNext(first_);
first_ = inst;
}

RegionInst *Inst::CastToRegion() {
ASSERT(IsRegion());
return static_cast<RegionInst *>(this);
Expand Down
10 changes: 1 addition & 9 deletions src/inst.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ class RegionInst : public ControlProp<DynamicInputs>

bool IsLoopHeader();
void PushBackInst(Inst *inst);
void PushFrontInst(Inst *inst);

Inst *GetFirst() {
return first_;
Expand All @@ -448,15 +449,6 @@ class RegionInst : public ControlProp<DynamicInputs>
return last_;
}

Inst *GetExitFromRegion() {
return exit_region_;
}

void SetExitRegion(Inst *inst) {
ASSERT(inst->GetOpcode() == Opcode::If || inst->GetOpcode() == Opcode::Jump);
exit_region_ = inst;
}

private:
void AddFirstInst(Inst *inst);

Expand Down
14 changes: 14 additions & 0 deletions src/ir_constructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ class IrConstructor {
return *this;
}

Inst *GetInst(id_t index) {
auto inst = graph_->GetInstByIndex(index);
current_inst_ = inst;
return inst;
}

RegionInst *GetRegion(id_t index) {
auto inst = graph_->GetInstByIndex(index);
if (!inst->IsRegion()) {
UNREACHABLE();
}
return inst->CastToRegion();
}

IrConstructor &Imm(int64_t imm) {
ASSERT(current_inst_ != nullptr);

Expand Down
14 changes: 7 additions & 7 deletions src/optimizations/analysis/liveness_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void LivenessAnalyzer::DumpLifeLinearData(std::ostream &out) {
PrintLifeLinearData(inst, out);
}
if (region->GetOpcode() != Opcode::End) {
ASSERT(region->GetExitFromRegion());
auto inst = region->GetExitFromRegion();
ASSERT(region->GetLast());
auto inst = region->GetLast();
inst->Dump(out);
if (inst->GetOpcode() != Opcode::Jump) {
PrintLifeLinearData(inst, out);
Expand All @@ -81,16 +81,16 @@ void LivenessAnalyzer::PrintLifeLinearData(Inst *inst, std::ostream &out) {
}

void LivenessAnalyzer::BuildLifeIfJump(RegionInst *region, LinearNumber &linear_number, LifeNumber &life_number) {
if (region->GetExitFromRegion() == nullptr) {
if (region->GetLast() == nullptr) {
return;
}

if (region->GetExitFromRegion()->GetOpcode() == Opcode::If) {
region->GetExitFromRegion()->SetLinearNumber(linear_number++);
if (region->GetLast()->GetOpcode() == Opcode::If) {
region->GetLast()->SetLinearNumber(linear_number++);
}
ASSERT(region->GetExitFromRegion()->GetOpcode() == Opcode::Jump);
ASSERT(region->GetLast()->GetOpcode() == Opcode::Jump);
life_number += 2;
region->GetExitFromRegion()->SetLifeNumber(life_number);
region->GetLast()->SetLifeNumber(life_number);
}

}
1 change: 1 addition & 0 deletions src/optimizations/analysis/loop_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Loop *LoopAnalysis::CreateLoop(RegionInst *region) {
region->SetLoop(loop);
loop->SetId(graph_->GetNumLoops());
loop->SetHeader(region);
loop->AddRegion(region);
return loop;
}

Expand Down
10 changes: 9 additions & 1 deletion src/optimizations/analysis/loop_analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,25 @@ class Loop
return backedge_;
}

bool LoopContaine(RegionInst *region) {
return std::find(body_.begin(), body_.end(), region) != body_.end();
}

void AddBackedge(RegionInst *region) {
ASSERT(std::find(backedge_.begin(), backedge_.end(), region) == backedge_.end())
backedge_.push_back(region);
}

void AddRegion(RegionInst *region) {
ASSERT(std::find(body_.begin(), body_.end(), region) == body_.end());
ASSERT(!LoopContaine(region));
region->SetLoop(this);
body_.push_back(region);
}

const std::vector<RegionInst *> &GetBody() {
return body_;
}

void SetOuterLoop(Loop *loop) {
outer_loop_ = loop;
}
Expand Down
6 changes: 3 additions & 3 deletions src/optimizations/gcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void GCM::PlacingDataInst(Inst *inst, RegionInst *region) {

auto opc = inst->GetOpcode();
if (opc == Opcode::Constant || opc == Opcode::Parameter) {
graph_->GetStartRegion()->PushBackInst(inst);
graph_->GetStartRegion()->PushFrontInst(inst);
inst->SetPlaced();
return;
}
Expand All @@ -51,13 +51,13 @@ void GCM::PlacingExitFromRegion(Inst *inst, RegionInst *region) {
ASSERT(opc == Opcode::Jump || opc == Opcode::If);
if (opc == Opcode::Jump) {
// TODO: Find more good place for this fill
region->SetExitRegion(inst);
region->PushBackInst(inst);
return;
}

if (opc == Opcode::If) {
// TODO: Find more good place for this fill
region->SetExitRegion(inst);
region->PushBackInst(inst);
PlacingDataInst(inst->GetDataInput(0), region);
return;
}
Expand Down
Loading

0 comments on commit a7be06b

Please sign in to comment.