Skip to content

Commit

Permalink
[CodeMove] Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Ranganhar committed Aug 20, 2024
1 parent 2be7836 commit 581a9d4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 39 deletions.
2 changes: 1 addition & 1 deletion include/ir/opt/CodeMove.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CodeMove : public _PassManagerBase<CodeMove, Function>
_AnalysisManager &AM;
void init();
bool RunMotion();
bool CanHandle(User* inst);
bool CanHandle(User* inst, std::unordered_set<Value*> CountSet, BasicBlock* father);
public:
CodeMove(Function *func_, _AnalysisManager &AM_) : func(func_), AM(AM_)
{
Expand Down
71 changes: 33 additions & 38 deletions ir/opt/CodeMove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,27 @@ bool CodeMove::RunMotion()
{
if (block == func->front())
continue;
std::vector<User *> MoveOut;
std::unordered_set<Value *> MoveOutSet;
std::vector<User *> HandleList;
std::unordered_set<Value *> CountSet;
for (User *inst : *block)
{
if (!CanHandle(inst))
if (!CanHandle(inst, CountSet, block))
continue;

bool flag = true;
for (auto &use : inst->Getuselist())
{
if (auto user = dynamic_cast<User *>(use->usee))
{
if (user->GetParent() == block)
{
if (!MoveOutSet.count(user))
{
flag = false;
break;
}
}
}
}
if (flag)
{
MoveOut.push_back(inst);
MoveOutSet.insert(inst);
}
HandleList.push_back(inst);
CountSet.insert(inst);
}
if(HandleList.empty())
continue;
std::vector<std::pair<User *, BasicBlock *>> MoveTo;
std::unordered_map<User *, BasicBlock *> SetTarget;
for (User *inst : MoveOut)
std::unordered_map<User *, BasicBlock *> MoveMapping;
for (User *inst : HandleList)
{
BasicBlock *TargetBlock = func->front();
auto UpdateTargetBlock = [&](BasicBlock *block) {
if (block == TargetBlock)
BasicBlock *MappingBB = func->front();
auto Update = [&](BasicBlock *block) {
if (block == MappingBB)
return;
if (DomTree->dominates(TargetBlock, block))
TargetBlock = block;
if (DomTree->dominates(MappingBB, block))
MappingBB = block;
return;
};
bool flag = true;
Expand All @@ -66,34 +49,34 @@ bool CodeMove::RunMotion()
{
if (user->GetParent() == block)
{
if (SetTarget.count(user))
UpdateTargetBlock(SetTarget[user]);
if (MoveMapping.count(user))
Update(MoveMapping[user]);
else
{
flag = false;
break;
}
}
else if (auto parent = user->GetParent())
UpdateTargetBlock(parent);
Update(parent);
}
else
flag = false;
}
if (!flag)
continue;
User *br = TargetBlock->back();
User *br = MappingBB->back();
BasicBlock::mylist<BasicBlock, User>::iterator Pos(br);
inst->EraseFromParent();
Pos.insert_before(inst);
SetTarget.emplace(inst, TargetBlock);
MoveMapping.emplace(inst, MappingBB);
modified = true;
}
}
return modified;
}

bool CodeMove::CanHandle(User *inst)
bool CodeMove::CanHandle(User *inst, std::unordered_set<Value *> CountSet, BasicBlock *father)
{
if (inst->HasSideEffect())
return false;
Expand All @@ -112,7 +95,19 @@ bool CodeMove::CanHandle(User *inst)
if (inst->GetOperand(0)->isGlobal())
return false;
}
default:
default: {
for (auto &use : inst->Getuselist())
{
if (auto user = dynamic_cast<User *>(use->usee))
{
if (user->GetParent() == father)
{
if (!CountSet.count(user))
return false;
}
}
}
return true;
}
}
}
62 changes: 62 additions & 0 deletions ir/opt/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,48 @@ Value *SimplifyIcmpInst(BinaryInst *inst, BinaryInst::Operation Opcode, Value *L
}
}

// (x > a) || (x < b) -> true (a < b)
if (Opcode == BinaryInst::Op_Or)
{
auto LHS_Cmp = dynamic_cast<BinaryInst *>(LHS);
auto RHS_Cmp = dynamic_cast<BinaryInst *>(RHS);
if (LHS_Cmp && RHS_Cmp && LHS_Cmp->getopration() == BinaryInst::Op_G &&
RHS_Cmp->getopration() == BinaryInst::Op_L)
{
Value *lhs_l = LHS_Cmp->GetOperand(0);
Value *lhs_r = LHS_Cmp->GetOperand(1);
Value *rhs_l = RHS_Cmp->GetOperand(0);
Value *rhs_r = RHS_Cmp->GetOperand(1);
if (lhs_l == rhs_l)
{
if (dynamic_cast<ConstIRInt *>(rhs_r) && dynamic_cast<ConstIRInt *>(lhs_r))
{
if (dynamic_cast<ConstIRInt *>(rhs_r)->GetVal() > dynamic_cast<ConstIRInt *>(lhs_r)->GetVal())
{
return ConstIRBoolean::GetNewConstant(true);
}
}
}
}
else if (LHS_Cmp && RHS_Cmp && LHS_Cmp->getopration() == BinaryInst::Op_L &&
RHS_Cmp->getopration() == BinaryInst::Op_G)
{
Value *lhs_l = LHS_Cmp->GetOperand(0);
Value *lhs_r = LHS_Cmp->GetOperand(1);
Value *rhs_l = RHS_Cmp->GetOperand(0);
Value *rhs_r = RHS_Cmp->GetOperand(1);
if (lhs_l == rhs_l)
{
if (dynamic_cast<ConstIRInt *>(rhs_r) && dynamic_cast<ConstIRInt *>(lhs_r))
{
if (dynamic_cast<ConstIRInt *>(rhs_r)->GetVal() < dynamic_cast<ConstIRInt *>(lhs_r)->GetVal()){
return ConstIRBoolean::GetNewConstant(true);
}

}
}
}
}
// true or X -> true
if (Opcode == BinaryInst::Op_Or)
{
Expand Down Expand Up @@ -453,5 +495,25 @@ Value *SimplifySelectInst(SelectInst *inst)
}
}
}
else if (auto false_bool = dynamic_cast<ConstIRBoolean *>(FalseVal))
{
if (false_bool->GetVal() == false)
{
auto AndInst = new BinaryInst(Cond, BinaryInst::Op_And, TrueVal);
BasicBlock::mylist<BasicBlock, User>::iterator Pos(inst);
Pos.insert_before(AndInst);
return AndInst;
}
}
else if (auto true_bool = dynamic_cast<ConstIRBoolean *>(TrueVal))
{
if (true_bool->GetVal() == true)
{
auto OrInst = new BinaryInst(Cond, BinaryInst::Op_Or, FalseVal);
BasicBlock::mylist<BasicBlock, User>::iterator Pos(inst);
Pos.insert_before(OrInst);
return OrInst;
}
}
return nullptr;
}

0 comments on commit 581a9d4

Please sign in to comment.