From 2786ab4339261ff92c3a3111229cbce111a39ad4 Mon Sep 17 00:00:00 2001 From: 2317891476 <2317891476@qq.com> Date: Sat, 29 Jun 2024 13:20:42 +0800 Subject: [PATCH 1/9] 2 --- cmake.sh | 2 ++ src/opt/constprop.cc | 53 ++++++++++++++++++++++++++++++++++++++++++-- src/opt/constprop.h | 21 ++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100755 cmake.sh diff --git a/cmake.sh b/cmake.sh new file mode 100755 index 0000000..c81cef4 --- /dev/null +++ b/cmake.sh @@ -0,0 +1,2 @@ +cmake -S . -B build +cmake --build build -j12 \ No newline at end of file diff --git a/src/opt/constprop.cc b/src/opt/constprop.cc index 3cd01da..3958521 100644 --- a/src/opt/constprop.cc +++ b/src/opt/constprop.cc @@ -1,3 +1,52 @@ +#include "constprop.h" +#include "../frontend/IR.h" +#include + namespace transform { - -} \ No newline at end of file + +void ConstProp::functionTransform(sysy::Function *func) { + for (auto &bb : func->getBasicBlocks()) { + this->curBB = bb.get(); + this->constantPropagation(bb.get()); + } +} + +void ConstProp::moduleTransform() { + auto *funcs = this->module->getFunctions(); + for (auto it = funcs->begin(); it != funcs->end(); ++it) { + this->functionTransform(it->second); + } +} + +void ConstProp::constantPropagation(sysy::BasicBlock *bb) { + std::unordered_map constantMap; + for (sysy::RVInst &inst : bb->CoInst) { + if (inst.op == "li" && inst.fields.size() >= 2) { // Assuming "li" is used to load immediate constants. + constantMap[inst.fields[1]] = std::stoi(inst.fields[0]); + } else if (isConstantValue(&inst)) { + continue; // Skip instructions already known to be constants. + } else { + // Attempt to replace uses of constants. + for (size_t i = 1; i < inst.fields.size(); ++i) { + if (constantMap.find(inst.fields[i]) != constantMap.end()) { + replaceConstantUses(&inst, constantMap[inst.fields[i]]); + break; // Assume single replacement per instruction for simplicity. + } + } + } + } +} + +bool ConstProp::isConstantValue(sysy::RVInst *inst) { + // Implement logic to determine if an instruction's output is a known constant. + // This is a placeholder and should be tailored to your IR structure. + return false; // Replace with actual implementation. +} + +void ConstProp::replaceConstantUses(sysy::RVInst *defInst, int constantValue) { + // Replace uses of the defined instruction with the constant value. + // This is a high-level description and requires detailed implementation. + // Ensure to update uses in subsequent instructions and potentially remove defInst. +} + +} // namespace transform \ No newline at end of file diff --git a/src/opt/constprop.h b/src/opt/constprop.h index e69de29..3eb4f05 100644 --- a/src/opt/constprop.h +++ b/src/opt/constprop.h @@ -0,0 +1,21 @@ +#include "../frontend/IR.h" + +namespace transform { + +class ConstProp { +private: + sysy::Module *module; + +public: + sysy::BasicBlock *curBB; + + ConstProp(sysy::Module *module): module(module) {}; + + void moduleTransform(); + void functionTransform(sysy::Function *func); + void constantPropagation(sysy::BasicBlock *bb); + bool isConstantValue(sysy::RVInst *inst); + void replaceConstantUses(sysy::RVInst *defInst, int constantValue); +}; // class ConstProp + +} // namespace transform \ No newline at end of file From 9473e012b45e84fec35e3e90a41061e18b857286 Mon Sep 17 00:00:00 2001 From: 2317891476 <2317891476@qq.com> Date: Sat, 29 Jun 2024 16:54:45 +0800 Subject: [PATCH 2/9] 1 --- src/CMakeLists.txt | 1 + src/opt/constprop.cc | 42 +++++++++++++++++++----------------------- src/opt/constprop.h | 4 ++-- src/sysyc.cc | 6 ++++++ test/17_constprop.sy | 12 ++++++++++++ 5 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 test/17_constprop.sy diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 692c160..17fe54f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable(sysyc backend/llir.cc backend/assembly.cc opt/peephole.cc + opt/constprop.cc ) target_include_directories(sysyc PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) target_link_libraries(sysyc PRIVATE SysYParser) diff --git a/src/opt/constprop.cc b/src/opt/constprop.cc index 3958521..35e7410 100644 --- a/src/opt/constprop.cc +++ b/src/opt/constprop.cc @@ -19,34 +19,30 @@ void ConstProp::moduleTransform() { } void ConstProp::constantPropagation(sysy::BasicBlock *bb) { - std::unordered_map constantMap; - for (sysy::RVInst &inst : bb->CoInst) { - if (inst.op == "li" && inst.fields.size() >= 2) { // Assuming "li" is used to load immediate constants. - constantMap[inst.fields[1]] = std::stoi(inst.fields[0]); - } else if (isConstantValue(&inst)) { - continue; // Skip instructions already known to be constants. - } else { - // Attempt to replace uses of constants. - for (size_t i = 1; i < inst.fields.size(); ++i) { - if (constantMap.find(inst.fields[i]) != constantMap.end()) { - replaceConstantUses(&inst, constantMap[inst.fields[i]]); - break; // Assume single replacement per instruction for simplicity. + std::unordered_map valueMap; + + // First pass: collect and record constant assignments. + for (auto &inst : bb->CoInst) { + if (inst.op == "li" && inst.fields.size() == 3) { // Assuming 'li' is the instruction to load immediate constants. + valueMap[inst.fields[1]] = inst.fields[2]; // Store constant value with its register. + } + } + + // Second pass: substitute known constants. + bool changed = true; + while (changed) { + changed = false; + for (auto &inst : bb->CoInst) { + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end()) { + // Replace uses of constants. + if (inst.op != "li") { + inst.fields[0] = valueMap[inst.fields[0]]; + changed = true; } } } } } -bool ConstProp::isConstantValue(sysy::RVInst *inst) { - // Implement logic to determine if an instruction's output is a known constant. - // This is a placeholder and should be tailored to your IR structure. - return false; // Replace with actual implementation. -} - -void ConstProp::replaceConstantUses(sysy::RVInst *defInst, int constantValue) { - // Replace uses of the defined instruction with the constant value. - // This is a high-level description and requires detailed implementation. - // Ensure to update uses in subsequent instructions and potentially remove defInst. -} } // namespace transform \ No newline at end of file diff --git a/src/opt/constprop.h b/src/opt/constprop.h index 3eb4f05..e2c7fd0 100644 --- a/src/opt/constprop.h +++ b/src/opt/constprop.h @@ -14,8 +14,8 @@ class ConstProp { void moduleTransform(); void functionTransform(sysy::Function *func); void constantPropagation(sysy::BasicBlock *bb); - bool isConstantValue(sysy::RVInst *inst); - void replaceConstantUses(sysy::RVInst *defInst, int constantValue); + //bool isConstantValue(sysy::RVInst *inst); + //void replaceConstantUses(sysy::RVInst *defInst, int constantValue); }; // class ConstProp } // namespace transform \ No newline at end of file diff --git a/src/sysyc.cc b/src/sysyc.cc index 15c3085..6623682 100644 --- a/src/sysyc.cc +++ b/src/sysyc.cc @@ -9,6 +9,7 @@ #include "backend/llir.h" #include "backend/assembly.h" #include "opt/peephole.h" +#include "opt/constprop.h" struct ArgsOptions { std::string srcfile; @@ -29,6 +30,7 @@ class TransformFlags { {"--fpeephole", false}, {"--verbose", false}, {"--fparallel", false}, + {"--fconstprop",false}, {"--opt", false}}; std::string srcfile; void parseFlags(int argc, char** argv) { @@ -57,6 +59,10 @@ class TransformFlags { transform::Hole hole(this->module); hole.moduleTransform(); } + if (this->flags["--fconstprop"]) { + transform::ConstProp constProp(this->module); + constProp.moduleTransform(); + } } void emit() { if (this->flags["--emit-ir"]) { diff --git a/test/17_constprop.sy b/test/17_constprop.sy new file mode 100644 index 0000000..556a4a2 --- /dev/null +++ b/test/17_constprop.sy @@ -0,0 +1,12 @@ +int main (){ + int a = 30; + int b = 9 - a / 5; + int c; + c = b * 4; + if (c > 10) { + c = c - 10; + }else{ + c = 100; + } + return c * (60 / a); +} \ No newline at end of file From 30da0b75823d071b4fdce711ba1212863fda5086 Mon Sep 17 00:00:00 2001 From: 2317891476 <2317891476@qq.com> Date: Sat, 29 Jun 2024 22:16:08 +0800 Subject: [PATCH 3/9] bug1 --- src/opt/constprop.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/opt/constprop.cc b/src/opt/constprop.cc index 35e7410..62c5206 100644 --- a/src/opt/constprop.cc +++ b/src/opt/constprop.cc @@ -23,8 +23,8 @@ void ConstProp::constantPropagation(sysy::BasicBlock *bb) { // First pass: collect and record constant assignments. for (auto &inst : bb->CoInst) { - if (inst.op == "li" && inst.fields.size() == 3) { // Assuming 'li' is the instruction to load immediate constants. - valueMap[inst.fields[1]] = inst.fields[2]; // Store constant value with its register. + if (inst.op == "li" && inst.fields.size() == 1) { // Assuming 'li' is the instruction to load immediate constants. + valueMap[inst.fields[1]] = inst.fields[0]; // Store constant value with its register. } } From 33f9f0bc3ee2f65bbac37f76dbc955c05e69627b Mon Sep 17 00:00:00 2001 From: 2317891476 <2317891476@qq.com> Date: Sat, 29 Jun 2024 22:57:45 +0800 Subject: [PATCH 4/9] bug_fix_low_perform --- src/opt/constprop.cc | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/opt/constprop.cc b/src/opt/constprop.cc index 62c5206..ac8111c 100644 --- a/src/opt/constprop.cc +++ b/src/opt/constprop.cc @@ -20,12 +20,18 @@ void ConstProp::moduleTransform() { void ConstProp::constantPropagation(sysy::BasicBlock *bb) { std::unordered_map valueMap; - + std::unordered_map valuelabel; // First pass: collect and record constant assignments. for (auto &inst : bb->CoInst) { - if (inst.op == "li" && inst.fields.size() == 1) { // Assuming 'li' is the instruction to load immediate constants. - valueMap[inst.fields[1]] = inst.fields[0]; // Store constant value with its register. - } + if (inst.op == "li" && inst.fields.size() == 2) { // Assuming 'li' is the instruction to load immediate constants. + if (valueMap.find(inst.fields[0]) == valueMap.end()){ + valueMap[inst.fields[0]] = inst.fields[1]; // Store constant value with its register. + valuelabel[inst.fields[0]] = "1"; + } + else { + valuelabel[inst.fields[0]] = "2"; + } + } } // Second pass: substitute known constants. @@ -33,9 +39,9 @@ void ConstProp::constantPropagation(sysy::BasicBlock *bb) { while (changed) { changed = false; for (auto &inst : bb->CoInst) { - if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end()) { + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuelabel[inst.fields[0]] == "1") { // Replace uses of constants. - if (inst.op != "li") { + if (inst.op != "li" ) { inst.fields[0] = valueMap[inst.fields[0]]; changed = true; } From 1404f4d25741e0bcacca049f7f4de2c59f6dc3da Mon Sep 17 00:00:00 2001 From: 2317891476 <2317891476@qq.com> Date: Sun, 30 Jun 2024 00:32:06 +0800 Subject: [PATCH 5/9] better perform --- src/opt/constprop.cc | 57 +++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/opt/constprop.cc b/src/opt/constprop.cc index ac8111c..7dfc89b 100644 --- a/src/opt/constprop.cc +++ b/src/opt/constprop.cc @@ -20,34 +20,53 @@ void ConstProp::moduleTransform() { void ConstProp::constantPropagation(sysy::BasicBlock *bb) { std::unordered_map valueMap; - std::unordered_map valuelabel; + // std::unordered_map valuelabel; // First pass: collect and record constant assignments. + // auto curInst = bb->CoInst.begin(); + // auto nextInst = std::next(curInst); + // while (curInst != bb->CoInst.end() && nextInst != bb->CoInst.end()) { + // if (curInst->op == "li" && curInst->fields.size() == 2) { // Assuming 'li' is the instruction to load immediate constants. + // if (valueMap.find(curInst->fields[0]) == valueMap.end()){ + // valueMap[curInst->fields[0]] = curInst->fields[1]; // Store constant value with its register. + // valuelabel[curInst->fields[0]] = "1"; + // } + // curInst = nextInst; + // nextInst = std::next(nextInst); + // } for (auto &inst : bb->CoInst) { if (inst.op == "li" && inst.fields.size() == 2) { // Assuming 'li' is the instruction to load immediate constants. - if (valueMap.find(inst.fields[0]) == valueMap.end()){ - valueMap[inst.fields[0]] = inst.fields[1]; // Store constant value with its register. - valuelabel[inst.fields[0]] = "1"; - } - else { - valuelabel[inst.fields[0]] = "2"; - } - } - } - - // Second pass: substitute known constants. - bool changed = true; - while (changed) { - changed = false; - for (auto &inst : bb->CoInst) { - if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuelabel[inst.fields[0]] == "1") { + // if (valueMap.find(inst.fields[0]) == valueMap.end()){ + valueMap[inst.fields[0]] = inst.fields[1]; // Store constant value with its register. + // //valuelabel[inst.fields[0]] = "1"; + // } + // else { + // //valuelabel[inst.fields[0]] = "2"; + // } + } + else{ + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end()) { // Replace uses of constants. if (inst.op != "li" ) { inst.fields[0] = valueMap[inst.fields[0]]; - changed = true; } } - } + } } + + // // Second pass: substitute known constants. + // bool changed = true; + // while (changed) { + // changed = false; + // for (auto &inst : bb->CoInst) { + // if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuelabel[inst.fields[0]] == "1") { + // // Replace uses of constants. + // if (inst.op != "li" ) { + // inst.fields[0] = valueMap[inst.fields[0]]; + // changed = true; + // } + // } + // } + // } } From b93d0e3b1673bbef2cca924c3cbf33de9866a0c7 Mon Sep 17 00:00:00 2001 From: 2317891476 <2317891476@qq.com> Date: Sun, 30 Jun 2024 01:42:28 +0800 Subject: [PATCH 6/9] v3 --- src/opt/constprop.cc | 51 ++++++++++---------------------------------- 1 file changed, 11 insertions(+), 40 deletions(-) diff --git a/src/opt/constprop.cc b/src/opt/constprop.cc index 7dfc89b..d172c54 100644 --- a/src/opt/constprop.cc +++ b/src/opt/constprop.cc @@ -20,54 +20,25 @@ void ConstProp::moduleTransform() { void ConstProp::constantPropagation(sysy::BasicBlock *bb) { std::unordered_map valueMap; - // std::unordered_map valuelabel; - // First pass: collect and record constant assignments. - // auto curInst = bb->CoInst.begin(); - // auto nextInst = std::next(curInst); - // while (curInst != bb->CoInst.end() && nextInst != bb->CoInst.end()) { - // if (curInst->op == "li" && curInst->fields.size() == 2) { // Assuming 'li' is the instruction to load immediate constants. - // if (valueMap.find(curInst->fields[0]) == valueMap.end()){ - // valueMap[curInst->fields[0]] = curInst->fields[1]; // Store constant value with its register. - // valuelabel[curInst->fields[0]] = "1"; - // } - // curInst = nextInst; - // nextInst = std::next(nextInst); - // } for (auto &inst : bb->CoInst) { - if (inst.op == "li" && inst.fields.size() == 2) { // Assuming 'li' is the instruction to load immediate constants. - // if (valueMap.find(inst.fields[0]) == valueMap.end()){ + if (inst.op == "li") { // Assuming 'li' is the instruction to load immediate constants. valueMap[inst.fields[0]] = inst.fields[1]; // Store constant value with its register. - // //valuelabel[inst.fields[0]] = "1"; - // } - // else { - // //valuelabel[inst.fields[0]] = "2"; - // } } - else{ - if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end()) { + else if(inst.op == "add" || inst.op == "mul" || inst.op == "sub" || inst.op == "div" ){ + //int len = inst.fields.size(); + for (int i=1;i<=2;i++){ + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[i]) != valueMap.end()) { // Replace uses of constants. - if (inst.op != "li" ) { - inst.fields[0] = valueMap[inst.fields[0]]; + inst.fields[i] = valueMap[inst.fields[i]]; } } + //int temp1 = (int*) valueMap[inst.fields[1]]; + //valueMap[inst.fields[0]] = valueMap[inst.fields[1]] + valueMap[inst.fields[2]]; + } + else{ + continue; } } - // // Second pass: substitute known constants. - // bool changed = true; - // while (changed) { - // changed = false; - // for (auto &inst : bb->CoInst) { - // if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuelabel[inst.fields[0]] == "1") { - // // Replace uses of constants. - // if (inst.op != "li" ) { - // inst.fields[0] = valueMap[inst.fields[0]]; - // changed = true; - // } - // } - // } - // } } - - } // namespace transform \ No newline at end of file From 2d461533ecd765b3a8c7c92d3ca6a113d0a4d9cc Mon Sep 17 00:00:00 2001 From: 2317891476 <2317891476@qq.com> Date: Sun, 30 Jun 2024 02:31:23 +0800 Subject: [PATCH 7/9] v4_add,sub,mul,div,sw,lw, --- src/opt/constprop.cc | 94 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 6 deletions(-) diff --git a/src/opt/constprop.cc b/src/opt/constprop.cc index d172c54..4781403 100644 --- a/src/opt/constprop.cc +++ b/src/opt/constprop.cc @@ -1,6 +1,7 @@ #include "constprop.h" #include "../frontend/IR.h" #include +#include namespace transform { @@ -19,21 +20,102 @@ void ConstProp::moduleTransform() { } void ConstProp::constantPropagation(sysy::BasicBlock *bb) { + int flag; std::unordered_map valueMap; + std::unordered_map valuechange; for (auto &inst : bb->CoInst) { if (inst.op == "li") { // Assuming 'li' is the instruction to load immediate constants. valueMap[inst.fields[0]] = inst.fields[1]; // Store constant value with its register. + valuechange[inst.fields[0]] = "0"; } - else if(inst.op == "add" || inst.op == "mul" || inst.op == "sub" || inst.op == "div" ){ - //int len = inst.fields.size(); + else if(inst.op == "add" ){ for (int i=1;i<=2;i++){ - if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[i]) != valueMap.end()) { - // Replace uses of constants. + flag = false; + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[i]) != valueMap.end() && valuechange[inst.fields[i]] == "0") { + inst.fields[i] = valueMap[inst.fields[i]]; + flag = true; + } + else break; + } + if (flag){ + int temp1 = std::stoll(inst.fields[1]); + int temp2 = std::stoll(inst.fields[2]); + valueMap[inst.fields[0]] = std::to_string(temp1 + temp2); + } + else{ + valuechange[inst.fields[0]] = "1"; + } + } + else if(inst.op == "sub" ){ + for (int i=1;i<=2;i++){ + flag = false; + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[i]) != valueMap.end() && valuechange[inst.fields[i]] == "0") { inst.fields[i] = valueMap[inst.fields[i]]; + flag = true; } + else break; + } + if (flag){ + int temp1 = std::stoll(inst.fields[1]); + int temp2 = std::stoll(inst.fields[2]); + valueMap[inst.fields[0]] = std::to_string(temp1 - temp2); + } + else{ + valuechange[inst.fields[0]] = "1"; + } + } + else if(inst.op == "mul" ){ + for (int i=1;i<=2;i++){ + flag = false; + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[i]) != valueMap.end() && valuechange[inst.fields[i]] == "0") { + inst.fields[i] = valueMap[inst.fields[i]]; + flag = true; + } + else break; + } + if (flag){ + int temp1 = std::stoll(inst.fields[1]); + int temp2 = std::stoll(inst.fields[2]); + valueMap[inst.fields[0]] = std::to_string(temp1 * temp2); + } + else{ + valuechange[inst.fields[0]] = "1"; + } + } + else if(inst.op == "div" ){ + for (int i=1;i<=2;i++){ + flag = false; + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[i]) != valueMap.end() && valuechange[inst.fields[i]] == "0") { + inst.fields[i] = valueMap[inst.fields[i]]; + flag = true; + } + else break; + } + if (flag){ + int temp1 = std::stoll(inst.fields[1]); + int temp2 = std::stoll(inst.fields[2]); + valueMap[inst.fields[0]] = std::to_string(temp1 / temp2); + } + else{ + valuechange[inst.fields[0]] = "1"; + } + } + else if (inst.op == "sw" ){ + auto nextInst = std::next(&inst); + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuechange[inst.fields[0]] == "0") { + // Replace uses of constants. + inst.fields[0] = valueMap[inst.fields[0]]; + } + if (nextInst->op == "lw"){ + valueMap[nextInst->fields[0]] = inst.fields[0];// Store constant value with its register. + valuechange[nextInst->fields[0]] = "0"; + } + } + else if (inst.op == "lw" ){ + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuechange[inst.fields[0]] == "0") { + // Replace uses of constants. + inst.fields[0] = valueMap[inst.fields[0]]; } - //int temp1 = (int*) valueMap[inst.fields[1]]; - //valueMap[inst.fields[0]] = valueMap[inst.fields[1]] + valueMap[inst.fields[2]]; } else{ continue; From 460f0725f08c952ce2e458cbd68554b73bce3a09 Mon Sep 17 00:00:00 2001 From: 2317891476 <2317891476@qq.com> Date: Sun, 30 Jun 2024 12:07:30 +0800 Subject: [PATCH 8/9] v4_bugfixed --- src/opt/constprop.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/opt/constprop.cc b/src/opt/constprop.cc index 4781403..581ec75 100644 --- a/src/opt/constprop.cc +++ b/src/opt/constprop.cc @@ -117,6 +117,12 @@ void ConstProp::constantPropagation(sysy::BasicBlock *bb) { inst.fields[0] = valueMap[inst.fields[0]]; } } + else if (inst.op == "addi" && inst.op == "ori" && inst.op == "subi" && inst.op == "slli" && inst.op == "sll" ){ + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuechange[inst.fields[0]] == "0") { + // Replace uses of constants. + valuechange[inst.fields[0]] = "1"; + } + } else{ continue; } From 940355de161b794c5be163f067eb95a112a701b2 Mon Sep 17 00:00:00 2001 From: 2317891476 <2317891476@qq.com> Date: Sun, 30 Jun 2024 21:47:48 +0800 Subject: [PATCH 9/9] sw_lw_bug_fixed --- src/opt/constprop.cc | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/opt/constprop.cc b/src/opt/constprop.cc index 581ec75..eac8ec8 100644 --- a/src/opt/constprop.cc +++ b/src/opt/constprop.cc @@ -101,30 +101,32 @@ void ConstProp::constantPropagation(sysy::BasicBlock *bb) { } } else if (inst.op == "sw" ){ + std::string temp; auto nextInst = std::next(&inst); if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuechange[inst.fields[0]] == "0") { // Replace uses of constants. - inst.fields[0] = valueMap[inst.fields[0]]; + //inst.fields[0] = valueMap[inst.fields[0]]; + temp = valueMap[inst.fields[0]]; } if (nextInst->op == "lw"){ - valueMap[nextInst->fields[0]] = inst.fields[0];// Store constant value with its register. + valueMap[nextInst->fields[0]] = temp;// Store constant value with its register. valuechange[nextInst->fields[0]] = "0"; } } else if (inst.op == "lw" ){ - if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuechange[inst.fields[0]] == "0") { - // Replace uses of constants. - inst.fields[0] = valueMap[inst.fields[0]]; - } + continue; } - else if (inst.op == "addi" && inst.op == "ori" && inst.op == "subi" && inst.op == "slli" && inst.op == "sll" ){ - if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuechange[inst.fields[0]] == "0") { + // else if (inst.op == "addi" && inst.op == "ori" && inst.op == "subi" && inst.op == "slli" && inst.op == "sll" ){ + // if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuechange[inst.fields[0]] == "0") { + // // Replace uses of constants. + // valuechange[inst.fields[0]] = "1"; + // } + // } + else{ + if (inst.valid && !inst.fields.empty() && valueMap.find(inst.fields[0]) != valueMap.end() && valuechange[inst.fields[0]] == "0") { // Replace uses of constants. valuechange[inst.fields[0]] = "1"; } - } - else{ - continue; } }