Skip to content

Commit

Permalink
Merge commit '9733fe8c09a6c86ea1a12c610d13dd6fe31c37b7'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ranganhar committed Aug 16, 2024
2 parents 8e4ac8a + 9733fe8 commit 50d7edd
Show file tree
Hide file tree
Showing 16 changed files with 360 additions and 17 deletions.
11 changes: 11 additions & 0 deletions backend/RISCVISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,17 @@ void RISCVISel::InstLowering(BinaryInst* inst){
else assert(0&&"Illegal!");
break;
}
case BinaryInst::Op_Xor:
{
if(inst->GetType()!=FloatType::NewFloatTypeGet()) {
if(ConstIRInt* constint = dynamic_cast<ConstIRInt*>(inst->GetOperand(1)))
ctx(Builder(RISCVMIR::_xori,inst));
else
ctx(Builder(RISCVMIR::_xor,inst));
}
else assert(0&&"Illegal!");
break;
}
default:
assert(0&&"Invalid Opcode");
}
Expand Down
4 changes: 2 additions & 2 deletions include/ir/opt/ConstantFold.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ConstantFolding
public:
Value* ConstantFoldInst(User* inst);
static ConstantData* ConstantFoldLoadFromConstPtr(ConstantData* Ptr, Type* tp);
static ConstantData* ConstFoldBinary(BinaryInst::Operation Opcode, ConstantData* LHS, ConstantData* RHS);
static ConstantData* ConstFoldBinary(BinaryInst* inst, BinaryInst::Operation Opcode, ConstantData* LHS, ConstantData* RHS);
static bool ReversedSubOperand(BinaryInst* inst);
private:
// Handle PhiInst
Expand All @@ -27,11 +27,11 @@ Value* ConstFoldFloatCmp(BinaryInst::Operation Opcode, float LVal, float RVal);
Value* ConstFoldCmp(BinaryInst::Operation Opcode, bool LVal, bool RVal);
Value* ConstFoldMaxInst(Value* LHS, Value* RHS);
Value* ConstFoldMinInst(Value* LHS, Value* RHS);
Value* ConstFoldSelectInst(Value* cond, Value* TrueVal, Value* FalseVal);
// Handle SITFP
Value* ConstantFoldSITFPInst(SITFP* inst);
// Handle FPTSI
Value* ConstantFoldFPTSIInst(FPTSI* inst);
// Handle ZextInst
Value* ConstantFoldZextInst(ZextInst* inst);

};
45 changes: 45 additions & 0 deletions include/ir/opt/ConstantHoist.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once
#include "../../../util/my_stl.hpp"
#include "../../lib/CFG.hpp"
#include "../Analysis/dominant.hpp"
#include "New_passManager.hpp"
#include "PassManagerBase.hpp"
#include <unordered_set>

class _AnalysisManager;

class ConstantHoist : public _PassManagerBase<ConstantHoist, Function>
{
struct HoistNode
{
User *LHS_Inst;
Value *LHS;
User *RHS_Inst;
Value *RHS;
int index;
HoistNode(User *LHS_Inst_, Value *LHS_, User *RHS_Inst_, Value *RHS_, int index_)
: LHS_Inst(LHS_Inst_), LHS(LHS_), RHS_Inst(RHS_Inst_), RHS(RHS_), index(index_)
{
}
};

private:
Function *func;
dominance *DomTree;
_AnalysisManager &AM;
std::vector<HoistNode *> HoistList;
std::unordered_map<BasicBlock *, std::unordered_map<User *, int>> InstIndex;

bool RunOnBlock(BasicBlock *bb);
bool HoistInstInBlock(BasicBlock *TrueBlock, BasicBlock *FalseBlock);

public:
ConstantHoist(Function *func_, _AnalysisManager &AM_) : func(func_), AM(AM_)
{
}
~ConstantHoist()
{
HoistList.clear();
}
bool Run();
};
4 changes: 2 additions & 2 deletions include/ir/opt/InstructionSimplify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include "../../lib/CFG.hpp"
#include "ConstantProp.hpp"

Value* SimplifyBinOp(BinaryInst::Operation Opcode, Value* LHS, Value* RHS);
Value* SimplifyBinOp(BinaryInst* inst, BinaryInst::Operation Opcode, Value* LHS, Value* RHS);
Value* SimplifyAddInst(Value* LHS, Value* RHS);
Value* SimplifySubInst(Value* LHS, Value* RHS);
Value* SimplifyMulInst(Value* LHS, Value* RHS);
Value* SimplifyDivInst(Value* LHS, Value* RHS);
Value* SimplifyModInst(Value* LHS, Value* RHS);
Value* SimplifyIcmpInst(BinaryInst::Operation Opcode, Value* LHS, Value* RHS);
Value* SimplifyIcmpInst(BinaryInst* inst, BinaryInst::Operation Opcode, Value* LHS, Value* RHS);
bool MatchAddModInst(BasicBlock* cur);
5 changes: 4 additions & 1 deletion include/ir/opt/New_passManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "../../include/ir/opt/LoadElimination.hpp"
#include "../../include/ir/opt/SelfStoreElimination.hpp"
#include "../../include/ir/opt/ScalarStrengthReduce.hpp"
#include "../../include/ir/opt/ConstantHoist.hpp"
#include <any>
#include <getopt.h>
#include <memory>
Expand Down Expand Up @@ -72,7 +73,8 @@ enum PassName {
loadeliminaion,
selfstoreelimination,
cachelookup,
scalarstrengthreduce
scalarstrengthreduce,
consthoist
};

static struct option long_options[] = {
Expand Down Expand Up @@ -106,6 +108,7 @@ static struct option long_options[] = {
{"SelfStoreElimination", no_argument, 0, 31},
{"CacheLookUp", no_argument, 0, 32},
{"ScalarStrengthReduce", no_argument, 0, 33},
{"consthoist", no_argument, 0, 34},
{"O0", no_argument, 0, 0},
{"O1", no_argument, 0, 1},
{"O2", no_argument, 0, 2},
Expand Down
3 changes: 2 additions & 1 deletion include/lib/BaseCFG.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ class User:public Value,public list_node<BasicBlock,User>
SI2FP,
BinaryUnknown,
Max,
Min
Min,
Select
};
OpID id;
using UsePtr=std::unique_ptr<Use>;
Expand Down
9 changes: 9 additions & 0 deletions include/lib/CFG.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ class MinInst:public User
void print()final;
};

class SelectInst : public User
{
public:
SelectInst(Type *ty) : User{ty} {id = OpID::Select;}
SelectInst(Operand _Cond, Operand _TrueVal, Operand _FalseVal);
SelectInst* clone(std::unordered_map<Operand,Operand>&)override;
void print() final;
};

class PhiInst : public User {
public:
PhiInst(Type *ty) :oprandNum(0), User{ty} {id = OpID::Phi;}
Expand Down
2 changes: 1 addition & 1 deletion ir/opt/CSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool CSE::ProcessNode(CSENode *node)
if (auto binary = dynamic_cast<BinaryInst *>(inst))
{
node->DelStoreValue(binary, CurGeneration);
if (Value *val = SimplifyBinOp(binary->getopration(), binary->GetOperand(0), binary->GetOperand(1)))
if (Value *val = SimplifyBinOp(binary, binary->getopration(), binary->GetOperand(0), binary->GetOperand(1)))
{
_DEBUG(std::cerr << "Can Handle BinaryInst: " << inst->GetName() << " as Value:" << val->GetName()
<< std::endl;)
Expand Down
21 changes: 17 additions & 4 deletions ir/opt/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Value *ConstantFolding::ConstantFoldInst(User *inst)
return ConstFoldMaxInst(inst->GetOperand(0), inst->GetOperand(1));
else if(auto mininst = dynamic_cast<MinInst*>(inst))
return ConstFoldMinInst(inst->GetOperand(0), inst->GetOperand(1));
else if(auto select = dynamic_cast<SelectInst*>(inst))
return ConstFoldSelectInst(select->GetOperand(0), select->GetOperand(1), select->GetOperand(2));
return nullptr;
}

Expand Down Expand Up @@ -44,7 +46,7 @@ Value *ConstantFolding::ConstantFoldBinaryInst(BinaryInst *inst)

if (LHS->isConst() && RHS->isConst())
{
Value *Simplify = SimplifyBinOp(inst->getopration(), LHS, RHS);
Value *Simplify = SimplifyBinOp(inst, inst->getopration(), LHS, RHS);
if (Simplify)
{
if (dynamic_cast<UndefValue *>(Simplify))
Expand All @@ -64,7 +66,7 @@ Value *ConstantFolding::ConstantFoldBinaryInst(BinaryInst *inst)
if (type == InnerDataType::IR_Value_Float)
return ConstantFoldBinaryFloat(inst, LHS, RHS);
}
Value *Simplify = SimplifyBinOp(inst->getopration(), LHS, RHS);
Value *Simplify = SimplifyBinOp(inst, inst->getopration(), LHS, RHS);
if (Simplify)
return Simplify;
if (inst->getopration() == BinaryInst::Op_Sub)
Expand Down Expand Up @@ -351,9 +353,9 @@ Value *ConstantFolding::ConstFoldCmp(BinaryInst::Operation Opcode, bool LVal, bo
return ConstIRBoolean::GetNewConstant(Result);
}

ConstantData *ConstantFolding::ConstFoldBinary(BinaryInst::Operation Opcode, ConstantData *LHS, ConstantData *RHS)
ConstantData *ConstantFolding::ConstFoldBinary(BinaryInst* inst, BinaryInst::Operation Opcode, ConstantData *LHS, ConstantData *RHS)
{
Value *Simplify = SimplifyBinOp(Opcode, LHS, RHS);
Value *Simplify = SimplifyBinOp(inst, Opcode, LHS, RHS);
ConstantData *Simplify_ = static_cast<ConstantData *>(Simplify);
if (Simplify_)
{
Expand Down Expand Up @@ -568,4 +570,15 @@ Value* ConstantFolding::ConstFoldMinInst(Value* LHS, Value* RHS)
return ConstIRFloat::GetNewConstant(std::min(LVal, RVal));
}
return nullptr;
}

Value* ConstantFolding::ConstFoldSelectInst(Value* cond, Value* TrueVal, Value* FalseVal)
{
auto Cond = dynamic_cast<ConstIRBoolean*>(cond);
if(!Cond)
return nullptr;
if(Cond->GetVal())
return TrueVal;
else
return FalseVal;
}
Loading

0 comments on commit 50d7edd

Please sign in to comment.