From 341d1e9ad24418f1af594ef430ba56fbebd954c7 Mon Sep 17 00:00:00 2001 From: lyh552506 <1661650252@qq.com> Date: Fri, 19 Apr 2024 23:55:42 +0800 Subject: [PATCH] [PRE] no WA --- include/backend/RegAlloc.hpp | 20 +++ include/ir/opt/SSAPRE.hpp | 94 ++++++-------- include/ir/opt/dominant.hpp | 5 + ir/opt/ADCE.cpp | 5 +- ir/opt/LoopInfo.cpp | 2 + ir/opt/SSAPRE.cpp | 240 ++++++++++++++++++++--------------- ir/opt/passManager.cpp | 13 +- 7 files changed, 223 insertions(+), 156 deletions(-) create mode 100644 include/backend/RegAlloc.hpp diff --git a/include/backend/RegAlloc.hpp b/include/backend/RegAlloc.hpp new file mode 100644 index 00000000..457d7400 --- /dev/null +++ b/include/backend/RegAlloc.hpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "MachineCode.hpp" +#include "PassManagerBase.hpp" +#include "CFG.hpp" + +class RegAlloc:public PassManagerBase{ + public: + RegAlloc(Function* func):m_func(func){} + void Run(); + void PrintPass(); + private: + Function* m_func; +}; \ No newline at end of file diff --git a/include/ir/opt/SSAPRE.hpp b/include/ir/opt/SSAPRE.hpp index 3b8859ee..7df46d2e 100644 --- a/include/ir/opt/SSAPRE.hpp +++ b/include/ir/opt/SSAPRE.hpp @@ -4,10 +4,13 @@ the details can be found in "Value-Based Partial Redundancy Elimination" written by Thomas VanDrunen and Antony L. Hosking ----------------------------------------------------------------------------------------------------*/ #pragma once +#include + +#include "BaseCFG.hpp" +#include "DealCriticalEdges.hpp" #include "IDF.hpp" -#include "dominant.hpp" #include "PassManagerBase.hpp" -#include "DealCriticalEdges.hpp" +#include "dominant.hpp" enum RetStats { Delay, Changed, Unchanged }; @@ -41,29 +44,22 @@ struct Expression { //因为后续使用find寻找相同的exp,这里需要增加一个判断两个exp是否相同 bool operator==(Expression &other) { - if (firVal != other.firVal) - return false; - if (SecVal != other.SecVal) - return false; - if (ThirdVal != other.ThirdVal) - return false; - if (type != other.type) - return false; - if (op != other.op) - return false; - if (args.size() != other.args.size()) - return false; + if (firVal != other.firVal) return false; + if (SecVal != other.SecVal) return false; + if (ThirdVal != other.ThirdVal) return false; + if (type != other.type) return false; + if (op != other.op) return false; + if (args.size() != other.args.size()) return false; for (int i = 0; i < args.size(); i++) - if (args[i] != other.args[i]) - return false; + if (args[i] != other.args[i]) return false; return true; } }; struct ValueTable { - std::map ValueNumber; // value* to kind number + std::map ValueNumber; // value* to kind number std::vector> - ExpNumber; // expression to kind number + ExpNumber; // expression to kind number int valuekinds = 0; int LookupOrAdd(Value *val); @@ -71,30 +67,27 @@ struct ValueTable { Expression CreatExp(BinaryInst *bin); Expression CreatExp(GetElementPtrInst *gep); - void Add(Value *val) { - if (ValueNumber.find(val) != ValueNumber.end()) - ValueNumber.erase(val); - ValueNumber.insert(std::make_pair(val, LookupOrAdd(val))); + void Add(Value *val, int num) { + if (ValueNumber.find(val) != ValueNumber.end()) ValueNumber.erase(val); + ValueNumber.insert(std::make_pair(val, num)); } ValueTable() : ValueNumber{} {} }; struct ValueNumberedSet { - std::vector Record; //判断是否插入 + std::vector Record; //判断是否插入 std::set contents; void insert_val(Value *val) { contents.insert(val); } void erase_val(Value *val) { contents.erase(val); } void set_hash(int hash) { - if (Record.size() < hash+1) - Record.resize(hash + 5); + if (Record.size() < hash + 1) Record.resize(hash + 5); Record[hash] = 1; } void clear_hash(int hash) { - if (Record.size() < hash+1) + if (Record.size() < hash + 1) assert(0 && "hash can't bigger than Record size!"); - if (Record[hash] == 0) - return; + if (Record[hash] == 0) return; Record[hash] = 0; } @@ -105,45 +98,42 @@ struct ValueNumberedSet { contents.clear(); } - void operator=(const ValueNumberedSet& other){ - Record=other.Record; - contents=other.contents; + void operator=(const ValueNumberedSet &other) { + Record = other.Record; + contents = other.contents; } /// @brief return true if is inserted bool IsAlreadyInsert(int hash) { - if (Record.size() < hash+1) - return false; + if (Record.size() < hash + 1) return false; return Record[hash] == 0 ? false : true; } }; -class PRE :public PassManagerBase{ -public: +class PRE : public PassManagerBase { + public: void RunOnFunction(); - void PrintPass(){ + void PrintPass() { std::cout << "--------pre--------" << std::endl; Singleton().Test(); } void init_pass(); - /// @brief void BuildSets(); - /// @brief void Insert(); - /// @brief + void Elimination(); + void check(const Value *val); + bool CanBeElim(Value *val); void DfsDT(int pos); void PostOrderCFG(BasicBlock *root); void BuidTopuSet(ValueNumberedSet &set); BasicBlock *GetChild(BasicBlock *BB, int flag); /// @brief 传入一个BasicBlock,统计他的所有前驱 void CalculatePredBB(BasicBlock *bb); - RetStats - IdentyPartilRedundancy(BasicBlock *cur, - std::map &insert_set); - void - FixPartialRedundancy(BasicBlock *cur, - std::map &predAvail, - std::map &insert_set); + RetStats IdentyPartilRedundancy( + BasicBlock *cur, std::map &insert_set); + bool FixPartialRedundancy( + Value *val, BasicBlock *cur, std::map &predAvail, + std::map &insert_set); void CalculateAvailOut(User *inst, ValueNumberedSet &avail, ValueNumberedSet &genexp, ValueNumberedSet &gentemp, ValueNumberedSet &genphis); @@ -161,20 +151,20 @@ class PRE :public PassManagerBase{ PRE(dominance *dom, Function *func) : m_dom(dom), m_func(func) { VN = new ValueTable(); } - ~PRE(){ - delete VN; - } -private: + ~PRE() { delete VN; } + + private: dominance *m_dom; Function *m_func; ValueTable *VN; std::map AvailOut; std::map AnticipatedIn; std::map GeneratedPhis; - std::vector Dfs; //记录深度遍历支配树信息 + std::vector Dfs; //记录深度遍历支配树信息 std::vector PostOrder; std::vector TopuOrder; std::vector Preds; - std::vector gen_exp; //原来的block中没有,属于是自己创建的exp + std::vector> + gen_exp; //原来的block中没有,属于是自己创建的exp std::map GeneratedTemp; }; diff --git a/include/ir/opt/dominant.hpp b/include/ir/opt/dominant.hpp index 33189975..308ed21c 100644 --- a/include/ir/opt/dominant.hpp +++ b/include/ir/opt/dominant.hpp @@ -24,6 +24,10 @@ class dominance : public PassManagerBase { public: int ancestor; int min_sdom; + void init(){ + ancestor=0; + min_sdom=0; + } }; class Node { public: @@ -130,6 +134,7 @@ class dominance : public PassManagerBase { count=0; std::fill_n(vertex, 91000, 0); bucket->clear(); + std::for_each(dsu.begin(), dsu.end(), [](DSU &x) { x.init(); }); init(); for (int i = 1; i <= block_num; i++) { dsu[i].ancestor = i; diff --git a/ir/opt/ADCE.cpp b/ir/opt/ADCE.cpp index fd79c3d6..8065469c 100644 --- a/ir/opt/ADCE.cpp +++ b/ir/opt/ADCE.cpp @@ -33,8 +33,9 @@ void ADCE::RunOnFunction() User* inst = *inst_; if(!inst->Alive) { - inst->ClearRelation(); - inst->EraseFromParent(); + // inst->ClearRelation(); + // inst->EraseFromParent(); + delete inst; } } } diff --git a/ir/opt/LoopInfo.cpp b/ir/opt/LoopInfo.cpp index df47e5df..c38cb31f 100644 --- a/ir/opt/LoopInfo.cpp +++ b/ir/opt/LoopInfo.cpp @@ -99,6 +99,7 @@ void LoopAnalysis::CalculateLoopDepth(LoopInfo *loop, int depth) { // num of loops // each loop nodes void LoopAnalysis::PrintPass() { + #ifdef DEBUG std::cout << "---------------------Loop Analysis-----------------------\n"; std::cout << "Num Of Loops:" << LoopRecord.size() << "\n"; for (int i = 0; i < LoopRecord.size(); i++) { @@ -111,4 +112,5 @@ void LoopAnalysis::PrintPass() { std::cout << x->GetName() << " "; std::cout << "\n\r"; } + #endif } \ No newline at end of file diff --git a/ir/opt/SSAPRE.cpp b/ir/opt/SSAPRE.cpp index a0e0a407..5bb54a9b 100644 --- a/ir/opt/SSAPRE.cpp +++ b/ir/opt/SSAPRE.cpp @@ -1,5 +1,16 @@ #include "SSAPRE.hpp" +#include +#include +#include +#include +#include +#include + +#include "BaseCFG.hpp" +#include "CFG.hpp" +#include "Singleton.hpp" + /// @brief flag=1 -->GetLeftChild /// flag=0 -->GetRightChild BasicBlock *PRE::GetChild(BasicBlock *BB, int flag) { @@ -28,21 +39,25 @@ void PRE::CalculatePredBB(BasicBlock *bb) { } void PRE::CleanMemory() { - for(int i=0;i(tmp); - delete tmp; + if (tmp.second == false) delete tmp.first; } } void PRE::init_pass() { BuildSets(); - + // if (m_func->GetName() == "main") Singleton().Test(); Insert(); + Elimination(); + // if (m_func->GetName() == "main") { + // Singleton().Test(); + // exit(0); + // } CleanMemory(); } @@ -55,16 +70,15 @@ void PRE::BuildSets() { auto &genPhis = GeneratedPhis[bb]; auto &genExps = GeneratedExps[bb]; auto &genTemp = GeneratedTemp[bb]; - auto node = m_dom->GetNode(bb->num); // Since the value leader set for the dominator // will have already been determined, we can conveniently build the leader // set for the current block by initializing it to the leader set of the // dominator BasicBlock *idomBB = m_dom->GetNode(node.idom).thisBlock; - if (idomBB != bb) //当前BB在支配树上没有父亲 + if (idomBB != bb) //当前BB在支配树上没有父亲 availout = AvailOut[idomBB]; - + for (auto it = bb->begin(); it != bb->end(); ++it) CalculateAvailOut(*it, availout, genExps, genTemp, genPhis); } @@ -82,23 +96,21 @@ void PRE::BuildSets() { //此处采用后续遍历CFG,因为没有采用后支配树的遍历,所以添加Delay标志 for (BasicBlock *post : PostOrder) { auto it = CalculateAntic.find(post); - if (it == CalculateAntic.end()) - continue; + if (it == CalculateAntic.end()) continue; RetStats stat = CalculateAnticIn(post, AnticOut, visited, GeneratedExps[post]); //如果为delay代表当前需要延迟求这个block,循环需要继续进行 if (stat == Delay) { changed = true; continue; - } else if (stat == Changed) { //对于change的block,因为数据流有更新, - changed = true; //所以需要重新将他的前驱块加入进来 + } else if (stat == Changed) { //对于change的block,因为数据流有更新, + changed = true; //所以需要重新将他的前驱块加入进来 visited.insert(post); m_func->init_visited_block(); Preds.clear(); CalculatePredBB(post); - for (BasicBlock *bb : Preds) - CalculateAntic.insert(bb); - } else { // stat == unchanged + for (BasicBlock *bb : Preds) CalculateAntic.insert(bb); + } else { // stat == unchanged visited.insert(post); CalculateAntic.erase(it); } @@ -149,10 +161,36 @@ void PRE::Insert() { //此时处理D的AnticIn集合,并选择能提升(hoist)到他的所有前驱的exp BuidTopuSet(AnticipatedIn[bb]); RetStats stat = IdentyPartilRedundancy(bb, insert_set); - if (stat == Changed) - changed = true; + if (stat == Changed) changed = true; + } + } + } +} + +void PRE::Elimination() { + std::vector> replace; + for (auto bb : Dfs) + for (auto iter = bb->begin(); iter != bb->end(); ++iter) { + User *tmp = *iter; + if (dynamic_cast(tmp) || + dynamic_cast(tmp)) { + if (AvailOut[bb].IsAlreadyInsert(VN->LookupOrAdd(tmp)) && + !AvailOut[bb].contents.count(tmp)) { + Value *leader = Find_Leader(AvailOut[bb], tmp); + auto user = dynamic_cast(leader); + if (user && user->GetParent() != nullptr) + replace.push_back(std::make_pair(tmp, user)); + } } } + while (replace.size() > 0) { + auto &val = replace.back(); + replace.pop_back(); + #ifdef DEBUG + std::cerr << "Erase Value: " << val.first->GetName() << std::endl; + #endif + val.first->RAUW(val.second); + val.first->EraseFromParent(); } } @@ -184,17 +222,21 @@ RetStats PRE::IdentyPartilRedundancy( //如果在其中一个前驱没有该表达式,则在此处不是available //代表后续我们需要在这个块添加一条指令,在当前块添加phi if (v == nullptr) { + if (predAvail.find(pred) != predAvail.end()) predAvail.erase(pred); AllPathSame = false; predAvail.insert(std::make_pair(pred, newval)); } else { + if (predAvail.find(pred) != predAvail.end()) predAvail.erase(pred); SomePathSame = true; predAvail.insert(std::make_pair(pred, v)); } } //存在至少一条路径可用并且不是所有路径可用时,识别为部分冗余 if (!AllPathSame && SomePathSame && - !GeneratedPhis[cur].IsAlreadyInsert(VN->LookupOrAdd(val))) { - FixPartialRedundancy(cur, predAvail, insert_set); + !GeneratedPhis[cur].IsAlreadyInsert(VN->LookupOrAdd(val)) && + CanBeElim(val)) { + if (FixPartialRedundancy(val, cur, predAvail, insert_set)) + return Unchanged; return Changed; } } @@ -202,12 +244,24 @@ RetStats PRE::IdentyPartilRedundancy( return Unchanged; } +bool PRE::CanBeElim(Value *val) { + if (auto user = dynamic_cast(val)) { + for (auto &use : user->Getuselist()) { + if (dynamic_cast(use->GetValue())||dynamic_cast(use->GetValue())) { + return false; + } + } + } + return true; +} + /// @brief 将识别出的部分冗余改写 -void PRE::FixPartialRedundancy( - BasicBlock *cur, std::map &predAvail, +bool PRE::FixPartialRedundancy( + Value *val, BasicBlock *cur, std::map &predAvail, std::map &insert_set) { Type *ty = nullptr; User *ready2Relp = nullptr; + int nul = 0; //初始为0,无效为2 for (auto p : m_dom->GetNode(cur->num).rev) { BasicBlock *pred = m_dom->GetNode(p).thisBlock; Value *v = predAvail[pred]; @@ -216,18 +270,22 @@ void PRE::FixPartialRedundancy( User *inst = dynamic_cast(v); Value *op_1 = nullptr, *op_2 = nullptr; ready2Relp = inst; + if (inst->GetUserlist().is_empty() && inst->GetParent() == nullptr) + return true; if (dynamic_cast(GetOperand(inst, 0)) || dynamic_cast(GetOperand(inst, 0))) op_1 = Find_Leader(AvailOut[pred], GetOperand(inst, 0)); else op_1 = GetOperand(inst, 0); - - if (dynamic_cast(inst)) + if (op_1 == nullptr) nul = 2; + if (dynamic_cast(inst)) { if (dynamic_cast(GetOperand(inst, 1)) || dynamic_cast(GetOperand(inst, 1))) op_2 = Find_Leader(AvailOut[pred], GetOperand(inst, 1)); else op_2 = GetOperand(inst, 1); + if (op_2 == nullptr) nul = 2; + } std::vector args; if (dynamic_cast(inst)) for (int i = 1; i < inst->Getuselist().size(); i++) { @@ -238,6 +296,16 @@ void PRE::FixPartialRedundancy( else args.push_back(arg); } + for (auto arg : args) { + if (arg == nullptr) { + nul = 2; + break; + } + } + if (nul == 2) { + int a = 0; + return nul; + } User *newval = nullptr; if (auto bin = dynamic_cast(inst)) newval = new BinaryInst(op_1, bin->getopration(), op_2); @@ -246,34 +314,31 @@ void PRE::FixPartialRedundancy( else assert(0); - auto tmp = pred->begin(); - for (auto iter = pred->begin();; ++iter) { - if (iter == pred->end()) - break; - tmp = iter; - } + auto tmp = pred->rbegin(); tmp.insert_before(newval); - VN->Add(newval); + #ifdef DEBUG + std::cerr << "insert:" << newval->GetName() << " at " + << newval->GetParent()->GetName() << std::endl; + #endif + VN->Add(newval, VN->LookupOrAdd(inst)); if (ty == nullptr && dynamic_cast(newval)) ty = dynamic_cast(newval->GetType()); else if (ty == nullptr && dynamic_cast(newval)) - ty = dynamic_cast(newval->GetType())->GetSubType(); + ty = dynamic_cast(newval->GetType()); // replace val in leader set ValueNumberedSet &newi = insert_set[pred]; ValueNumberedSet &oldi = AvailOut[pred]; if (!newi.contents.count(newval)) { Value *lead = Find_Leader(newi, newval); - if (lead != nullptr) - newi.erase_val(lead); + if (lead != nullptr) newi.erase_val(lead); newi.insert_val(newval); newi.set_hash(VN->LookupOrAdd(newval)); } if (!oldi.contents.count(newval)) { Value *lead = Find_Leader(oldi, newval); - if (lead != nullptr) - oldi.erase_val(lead); + if (lead != nullptr) oldi.erase_val(lead); oldi.insert_val(newval); oldi.set_hash(VN->LookupOrAdd(newval)); } @@ -283,31 +348,29 @@ void PRE::FixPartialRedundancy( //在此处创建phiNode PhiInst *phi = PhiInst::NewPhiNode(cur->front(), cur, ty); + #ifdef DEBUG + std::cerr << "insert phi:" << phi->GetName() << " at " + << phi->GetParent()->GetName() << std::endl; + #endif for (auto p : m_dom->GetNode(cur->num).rev) { BasicBlock *pred = m_dom->GetNode(p).thisBlock; - if(ready2Relp!=predAvail[pred]) - phi->updateIncoming(predAvail[pred], pred); + phi->updateIncoming(predAvail[pred], pred); } - if(phi->oprandNum==1){ - Value* v=(*(phi->PhiRecord.begin())).second.first; - ready2Relp->RAUW(v); - ready2Relp->ClearRelation(); - ready2Relp->EraseFromParent(); - phi->ClearRelation(); - phi->EraseFromParent(); - } - else { - ready2Relp->RAUW(phi); - ready2Relp->ClearRelation(); - ready2Relp->EraseFromParent(); - } - VN->Add(phi); + VN->Add(phi, VN->LookupOrAdd(val)); AvailOut[cur].insert_val(phi); AvailOut[cur].set_hash(VN->LookupOrAdd(phi)); GeneratedPhis[cur].insert_val(phi); GeneratedPhis[cur].set_hash(VN->LookupOrAdd(phi)); insert_set[cur].insert_val(phi); insert_set[cur].set_hash(VN->LookupOrAdd(phi)); + return nul; +} + +void PRE::check(const Value *val) { + auto iter = std::find_if( + gen_exp.begin(), gen_exp.end(), + [val](const std::pair &ele) { return ele.first == val; }); + if (iter != gen_exp.end()) iter->second = true; } // ANTIC IN[b] = clean(canone(ANTIC OUT[b] ∪ EXP GEN[b]−TMP GEN(b))) @@ -317,12 +380,12 @@ RetStats PRE::CalculateAnticIn(BasicBlock *bb, ValueNumberedSet &AnticOut, //要计算Antic_In set首先需要求出Antic_Out auto &gen_tmp = GeneratedTemp[bb]; auto &anticin = AnticipatedIn[bb]; - int o_size = anticin.contents.size(); //记录下原来的size大小,看后续是否有变化 + int o_size = + anticin.contents.size(); //记录下原来的size大小,看后续是否有变化 anticin.init(); RetStats stat = CalculateAnticOut(bb, AnticOut, visited); - if (stat == Delay) - return stat; + if (stat == Delay) return stat; // ANTIC OUT[b] ,其中已经保证了Anticout的元素都是leader for (auto it = AnticOut.contents.begin(); it != AnticOut.contents.end(); ++it) { @@ -345,6 +408,7 @@ RetStats PRE::CalculateAnticIn(BasicBlock *bb, ValueNumberedSet &AnticOut, } clean(anticin); + AnticOut.init(); if (anticin.contents.size() == o_size) return Unchanged; else @@ -377,7 +441,7 @@ RetStats PRE::CalculateAnticOut(BasicBlock *bb, ValueNumberedSet &AnticOut, } } } - } else if (SuccNum > 1) { // the back of BB must be CondInst + } else if (SuccNum > 1) { // the back of BB must be CondInst // ANTIC_IN[succ0(b)] BasicBlock *succ0 = dynamic_cast(bb->back()->Getuselist()[1]->GetValue()); @@ -409,7 +473,7 @@ RetStats PRE::CalculateAnticOut(BasicBlock *bb, ValueNumberedSet &AnticOut, //逐个遍历指令加入各个value leader void PRE::CalculateAvailOut(User *inst, ValueNumberedSet &avail, ValueNumberedSet &genexp, ValueNumberedSet &gentemp, - ValueNumberedSet &genphis) { + ValueNumberedSet &genphis) { if (auto phi = dynamic_cast(inst)) { int hash = VN->LookupOrAdd(phi); @@ -461,7 +525,7 @@ void PRE::CalculateAvailOut(User *inst, ValueNumberedSet &avail, } } } - } else if (!inst->IsTerminateInst()) { /// @note 这里为什么会这样? + } else if (!inst->IsTerminateInst()) { /// @note 这里为什么会这样? VN->LookupOrAdd(inst); gentemp.insert_val(inst); } @@ -487,8 +551,7 @@ void PRE::DfsDT(int pos) { int ValueTable::LookupOrAdd(Value *val) { //首先查找VN是否有传入的目标value auto iter = ValueNumber.find(val); - if (iter != ValueNumber.end()) - return iter->second; + if (iter != ValueNumber.end()) return iter->second; //在VN中没有找到目标value,依次枚举Value*,创建Exp if (auto bin = dynamic_cast(val)) { @@ -518,7 +581,7 @@ int ValueTable::LookupOrAdd(Value *val) { ExpNumber.push_back(std::make_pair(e, valuekinds)); return valuekinds++; } - } else { //不是任何语句 + } else { //不是任何语句 ValueNumber.insert(std::make_pair(val, valuekinds)); return valuekinds++; } @@ -549,8 +612,7 @@ Expression ValueTable::CreatExp(GetElementPtrInst *gep) { } void PRE::PostOrderCFG(BasicBlock *root) { - if (root->visited) - return; + if (root->visited) return; root->visited = true; for (int tmp : m_dom->GetNode(root->num).des) { PostOrderCFG(m_dom->GetNode(tmp).thisBlock); @@ -561,51 +623,40 @@ void PRE::PostOrderCFG(BasicBlock *root) { Value *PRE::phi_translate(BasicBlock *pred, BasicBlock *succ, Value *val) { //对于phi函数,返回来自pred块的数据流 if (auto phi = dynamic_cast(val)) { - if (phi->GetParent() == succ) - return phi->ReturnValIn(pred); + if (phi->GetParent() == succ) return phi->ReturnValIn(pred); } else if (auto bin = dynamic_cast(val)) { Value *op_1 = bin->Getuselist()[0]->GetValue(); Value *op_2 = bin->Getuselist()[1]->GetValue(); - if(bin->GetName()==".194"){ - int a=0; - } - if (dynamic_cast(op_1)) - op_1 = phi_translate(pred, succ, op_1); - if (dynamic_cast(op_2)) - op_2 = phi_translate(pred, succ, op_2); + if (dynamic_cast(op_1)) op_1 = phi_translate(pred, succ, op_1); + if (dynamic_cast(op_2)) op_2 = phi_translate(pred, succ, op_2); //如果操作数中出现phi函数,会将phi转换为对应的数据流,此时我们检查是否有转换 if (op_1 != bin->Getuselist()[0]->GetValue() || op_2 != bin->Getuselist()[1]->GetValue()) { BinaryInst *newbin = new BinaryInst(op_1, bin->getopration(), op_2); - int hash = VN->LookupOrAdd(newbin); - if (!AvailOut[pred].IsAlreadyInsert(hash)) { - gen_exp.push_back(newbin); + gen_exp.push_back(std::make_pair(newbin, false)); return newbin; } else { VN->erase(newbin); newbin->ClearRelation(); + check(newbin); delete newbin; for (auto x = AvailOut[pred].contents.begin(); x != AvailOut[pred].contents.end(); x++) - if (hash == VN->LookupOrAdd(*x)) - return *x; + if (hash == VN->LookupOrAdd(*x)) return *x; } } } else if (auto gep = dynamic_cast(val)) { bool change = false; std::vector args; Value *ptr = gep->Getuselist()[0]->GetValue(); - if (dynamic_cast(ptr)) - ptr = phi_translate(pred, succ, ptr); + if (dynamic_cast(ptr)) ptr = phi_translate(pred, succ, ptr); //接下来处理args for (int i = 1; i < gep->Getuselist().size(); i++) { Value *arg = gep->Getuselist()[i]->GetValue(); - if (dynamic_cast(arg)) - arg = phi_translate(pred, succ, arg); - if (arg != gep->Getuselist()[i]->GetValue()) - change = true; + if (dynamic_cast(arg)) arg = phi_translate(pred, succ, arg); + if (arg != gep->Getuselist()[i]->GetValue()) change = true; args.push_back(arg); } @@ -613,18 +664,14 @@ Value *PRE::phi_translate(BasicBlock *pred, BasicBlock *succ, Value *val) { GetElementPtrInst *newgep = new GetElementPtrInst(ptr, args); int hash = VN->LookupOrAdd(newgep); if (!AvailOut[pred].IsAlreadyInsert(hash)) { - // if(newgep->GetName()==".307"){ - // int x=0; - // } - gen_exp.push_back(newgep); + gen_exp.push_back(std::make_pair(newgep, false)); return newgep; } else { VN->erase(newgep); delete newgep; for (auto x = AvailOut[pred].contents.begin(); x != AvailOut[pred].contents.end(); x++) - if (hash == VN->LookupOrAdd(*x)) - return *x; + if (hash == VN->LookupOrAdd(*x)) return *x; } } } @@ -633,10 +680,6 @@ Value *PRE::phi_translate(BasicBlock *pred, BasicBlock *succ, Value *val) { } void PRE::clean(ValueNumberedSet &val) { - // BuidTopuSet(val); - // for (auto inst : TopuOrder) { - - // } } void PRE::BuidTopuSet(ValueNumberedSet &set) { @@ -645,8 +688,7 @@ void PRE::BuidTopuSet(ValueNumberedSet &set) { std::vector topuSt; for (auto it = set.contents.begin(); it != set.contents.end(); it++) { //此处不能直接插入,还需要看他的操作数是否是inst,递归调用 - if (!visited.count(*it)) - topuSt.push_back(*it); + if (!visited.count(*it)) topuSt.push_back(*it); while (!topuSt.empty()) { Value *val = topuSt.back(); @@ -661,7 +703,7 @@ void PRE::BuidTopuSet(ValueNumberedSet &set) { else if (op2 != nullptr && dynamic_cast(op2) && visited.count(op2) == 0) topuSt.push_back(op2); - else { //来到边缘点,加入到topu排序栈 + else { //来到边缘点,加入到topu排序栈 TopuOrder.push_back(bin); visited.insert(bin); topuSt.pop_back(); @@ -699,10 +741,8 @@ void PRE::BuidTopuSet(ValueNumberedSet &set) { } Value *PRE::Find_Leader(ValueNumberedSet &set, Value *val) { - if (!set.IsAlreadyInsert(VN->LookupOrAdd(val))) - return nullptr; + if (!set.IsAlreadyInsert(VN->LookupOrAdd(val))) return nullptr; int hash = VN->LookupOrAdd(val); for (auto tmp = set.contents.begin(); tmp != set.contents.end(); tmp++) - if (hash == VN->LookupOrAdd(*tmp)) - return *tmp; + if (hash == VN->LookupOrAdd(*tmp)) return *tmp; } \ No newline at end of file diff --git a/ir/opt/passManager.cpp b/ir/opt/passManager.cpp index d1c80536..28f91f66 100644 --- a/ir/opt/passManager.cpp +++ b/ir/opt/passManager.cpp @@ -1,4 +1,7 @@ #include "passManager.hpp" +#include "CFG.hpp" +#include "Singleton.hpp" +#include "dominant.hpp" void PassManager::InitPass() { for (int i = 0; i < Singleton().GetFuncTion().size(); i++) { @@ -7,7 +10,7 @@ void PassManager::InitPass() { m_eliedg = std::make_unique(m_func); m_dom = std::make_unique(m_func, BList.size()); - m_pre = std::make_unique
(m_dom.get(), m_func);
+    // m_pre = std::make_unique
(m_dom.get(), m_func);
     m_cfgsimple = std::make_unique(m_func, m_dom.get());
     m_liveness = std::make_unique(m_func);
     m_adce = std::make_unique(m_func);
@@ -37,14 +40,20 @@ void PassManager::RunOnFunction() {
   if (InitpassRecorder[0]) {
     m_dom->RunOnFunction();
     // m_dom->PrintPass();
+    // if(m_func->GetName()=="main")
+    //   Singleton().Test();
   }
   if (InitpassRecorder[8]) {
     m_eliedg->RunOnFunction();
     PreWork(func);
     m_dom->update();
-    // m_eliedg->PrintPass();
+    // if(m_func->GetName()=="main")
+    //   Singleton().Test();
   }
   if (InitpassRecorder[1]) {
+    dominance* d=new dominance(m_func, BList.size());
+    d->RunOnFunction();
+    m_pre = std::make_unique
(d, m_func);
     m_pre->RunOnFunction();
     // m_pre->PrintPass();
   }