-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added peepholes and const folding rules
+ Peepholes and const folding rules for Sub, Shl, Or + Added tests
- Loading branch information
1 parent
49aaa48
commit fa6c9b6
Showing
10 changed files
with
517 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "constant_folding.h" | ||
#include "graph.h" | ||
|
||
namespace compiler { | ||
|
||
bool ConstFoldingBinaryOp(Graph *graph, Inst *inst) { | ||
auto input0 = inst->GetDataInput(0); | ||
auto input1 = inst->GetDataInput(1); | ||
|
||
if (!input0->IsConst() || !input1->IsConst()) { | ||
return false; | ||
} | ||
|
||
Inst *new_const = nullptr; | ||
auto imm0 = input0->CastToConstant()->GetImm(); | ||
auto imm1 = input1->CastToConstant()->GetImm(); | ||
|
||
switch (inst->GetOpcode()) { | ||
case Opcode::Sub: | ||
new_const = graph->CreateConstantInst(imm0 - imm1); | ||
break; | ||
case Opcode::Shl: | ||
new_const = graph->CreateConstantInst(imm0 << imm1); | ||
break; | ||
case Opcode::Or: | ||
new_const = graph->CreateConstantInst(imm0 | imm1); | ||
break; | ||
default: | ||
UNREACHABLE(); | ||
} | ||
new_const->ReplaceDataUsers(inst); | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
namespace compiler { | ||
|
||
class Inst; | ||
class Graph; | ||
|
||
bool ConstFoldingBinaryOp(Graph *graph, Inst *inst); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include <limits> | ||
|
||
#include "peepholes.h" | ||
#include "analysis/rpo.h" | ||
#include "constant_folding.h" | ||
|
||
namespace compiler { | ||
|
||
void Peepholes::Run() { | ||
auto rpo = RpoInsts(graph_); | ||
rpo.Run(); | ||
auto rpo_vector = rpo.GetVector(); | ||
for (auto inst : rpo_vector) { | ||
AnalysisInst(inst); | ||
} | ||
} | ||
|
||
void Peepholes::AnalysisInst(Inst *inst) { | ||
switch (inst->GetOpcode()) { | ||
case Opcode::Sub: | ||
VisitSub(inst); | ||
break; | ||
case Opcode::Shl: | ||
VisitShl(inst); | ||
break; | ||
case Opcode::Or: | ||
VisitOr(inst); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void Peepholes::VisitSub(Inst *inst) { | ||
if (ConstFoldingBinaryOp(graph_, inst)) { | ||
return; | ||
} | ||
|
||
if (TryOptimizeSubZero(inst)) { | ||
return; | ||
} | ||
} | ||
|
||
void Peepholes::VisitShl(Inst *inst) { | ||
if (ConstFoldingBinaryOp(graph_, inst)) { | ||
return; | ||
} | ||
|
||
if (TryOptimizeShlAfterShr(inst)) { | ||
return; | ||
} | ||
} | ||
|
||
void Peepholes::VisitOr(Inst *inst) { | ||
if (ConstFoldingBinaryOp(graph_, inst)) { | ||
return; | ||
} | ||
if (TryOptimizeOrZero(inst)) { | ||
return; | ||
} | ||
} | ||
|
||
bool Peepholes::TryOptimizeSubZero(Inst *inst) { | ||
auto input0 = inst->GetDataInput(0); | ||
auto input1 = inst->GetDataInput(1); | ||
|
||
if (input1->IsConst() && input1->CastToConstant()->GetImm() == 0) { | ||
input0->ReplaceDataUsers(inst); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool Peepholes::TryOptimizeShlAfterShr(Inst *inst) { | ||
auto input0 = inst->GetDataInput(0); | ||
auto input1 = inst->GetDataInput(1); | ||
|
||
if (input1->IsConst() && | ||
input0->GetOpcode() == Opcode::Shr && | ||
input0->GetDataInput(1)->IsConst() && | ||
input0->GetDataInput(1)->CastToConstant()->GetImm() == input1->CastToConstant()->GetImm()) { | ||
|
||
auto input_shr = input0->GetDataInput(0); | ||
auto shift = input1->CastToConstant()->GetImm(); | ||
auto new_const = graph_->CreateConstantInst(std::numeric_limits<ImmType>::max() & (0xffffffffffffffff << shift)); | ||
auto inst_and = graph_->CreateAndInst(input_shr->GetType(), input_shr, new_const); | ||
inst_and->ReplaceDataUsers(inst); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool Peepholes::TryOptimizeOrZero(Inst *inst) { | ||
auto input0 = inst->GetDataInput(0); | ||
auto input1 = inst->GetDataInput(1); | ||
|
||
if (input0->IsConst()) { | ||
std::swap(input0, input1); | ||
} | ||
|
||
if (input1->IsConst() && input1->CastToConstant()->GetImm() == 0) { | ||
input0->ReplaceDataUsers(inst); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include "graph.h" | ||
|
||
namespace compiler { | ||
|
||
class Peepholes { | ||
public: | ||
Peepholes(Graph *graph): | ||
graph_(graph) {} | ||
|
||
void Run(); | ||
|
||
private: | ||
void AnalysisInst(Inst *inst); | ||
|
||
// Process instruction | ||
void VisitSub(Inst *inst); | ||
void VisitShl(Inst *inst); | ||
void VisitOr(Inst *inst); | ||
|
||
// Cases of optimization | ||
bool TryOptimizeSubZero(Inst *inst); | ||
bool TryOptimizeShlAfterShr(Inst *inst); | ||
bool TryOptimizeOrZero(Inst *inst); | ||
|
||
private: | ||
Graph *graph_; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.