diff --git a/backend/armopt.cpp b/backend/armopt.cpp index ee2e3ef43d823ececcc2cfa5df6cb11fd301c26a..55f4775da17a0cd0623d90b3f6e5e2a254640ac4 100644 --- a/backend/armopt.cpp +++ b/backend/armopt.cpp @@ -3483,35 +3483,28 @@ void _Peephole() ldr rn,[x] str rn,[x] */ - // for (int i = 1; i < _arm_text.size(); i++) - // { - // armIR = _arm_text[i]; - // cout<<"1:" <<_arm_text[i].substr(1, 4) <<endl; - // if (_arm_text[i].substr(1, 4) == "ldr") - // { - // if(_arm_text[i-1].substr(1,4)=="str") - // { - // cout<<_arm_text[i-1].substr(7) <<endl; - // cout<<_arm_text[i].substr(7) <<endl; - // if(_arm_text[i-1].substr(7)==_arm_text[i].substr(7)) - // { - // _arm_text[i].erase(); + for (int i = 1; i < _arm_text.size(); i++) + { + if (_arm_text[i].size()>8&&_arm_text[i].substr(1, 3) == "ldr") + { + if(_arm_text[i-1].size()>8&&_arm_text[i-1].substr(1,3)=="str") + { + if(_arm_text[i-1].find(_arm_text[i].substr(5))!=-1) + { + _arm_text[i].erase(); - // } - // } - // } - // else if (_arm_text[i].substr(1, 4) == "str") - // { - // if(_arm_text[i-1].substr(1,4)=="ldr") - // { - // cout<<_arm_text[i-1].substr(7) <<endl; - // cout<<_arm_text[i].substr(7) <<endl; - // if(_arm_text[i-1].substr(7)==_arm_text[i].substr(7)) - // { - // _arm_text[i].erase(); - - // } - // } - // } - // } + } + } + } + else if (_arm_text[i].size()>8&&_arm_text[i].substr(1,3 ) == "str") + { + if(_arm_text[i-1].size()>8&&_arm_text[i-1].substr(1,3)=="ldr") + { + if(_arm_text[i-1].find(_arm_text[i].substr(5))!=-1) + { + _arm_text[i].erase(); + } + } + } + } } diff --git a/backend/optimizerIR.cpp b/backend/optimizerIR.cpp index 435b28dd092d5271eb444790daa3488faed1706a..347ee7cd0067c93a51b2f4b59d9196133b839397 100644 --- a/backend/optimizerIR.cpp +++ b/backend/optimizerIR.cpp @@ -139,7 +139,7 @@ void CreateBB() } CreateDAG(VisitBB); }*/ - ShowBB(); + // ShowBB(); CreateDOMandLoop(); } @@ -1959,12 +1959,28 @@ void CodeMotion(int i) while (visitIR->op == _OP_EMPTY || !visitIR->isInvar) { if (visitIR == LoopList[i]->loopseq[j]->BlockEndIR || visitIR->op == _OP_BR) + { + if(visitIR->op==_OP_BR) + { + BrIR* brIR = (BrIR*)visitIR; + if(brIR->isIF&&visitIR->arg2=="") + movecount -= CheckIF(LoopList[i]->loopseq[j]); + } break; + } visitIR = visitIR->nextIR; } couldmove = true; if (visitIR == LoopList[i]->loopseq[j]->BlockEndIR || visitIR->op == _OP_BR) + { + if(visitIR->op==_OP_BR) + { + BrIR* brIR = (BrIR*)visitIR; + if(brIR->isIF&&visitIR->arg2=="") + movecount = movecount - CheckIF(LoopList[i]->loopseq[j]); + } break; + } if (visitIR->op == _OP_PARAM || visitIR->op == _OP_CMP_EQ || visitIR->op == _OP_CMP_GE || visitIR->op == _OP_CMP_GT || visitIR->op == _OP_CMP_LE || visitIR->op == _OP_CMP_LT || visitIR->op == _OP_CMP_NE) { if (visitIR->isInvar && visitIR->op == _OP_PARAM) @@ -2333,6 +2349,27 @@ void CodeMotion(int i) } } +int CheckIF(BasicBlock* currentblock) +{ + IR* visitIR = currentblock->BlockStartIR; + int subcount=0; + while (true) + { + if (visitIR == currentblock->BlockEndIR || visitIR->op == _OP_BR) + { + break; + } + if(visitIR->op==_OP_ASSIGN && visitIR->couldmove) + { + visitIR->couldmove=false; + subcount++; + } + visitIR = visitIR->nextIR; + } + return subcount; +} + + void GetUD(BasicBlock *block, string var, IR *varIR) { int num = atoi(var.substr(1).c_str()); diff --git a/compiler b/compiler index 7a0c54655c11c892fbbc425b58606b600188d06f..91e779b1b50731f55f5cf5e4dad0cc1bb1814c0d 100755 Binary files a/compiler and b/compiler differ diff --git a/frontend/semantic.cpp b/frontend/semantic.cpp index 9b7464b4169000897445e0a9e1eeebb45dbc72fb..ec94e1cb46c5a305e79313233ff0bf7c5aa79467 100644 --- a/frontend/semantic.cpp +++ b/frontend/semantic.cpp @@ -824,6 +824,7 @@ void IfNode(ast *node) // 建立IR br arg1:true arg2:next result:cond_value IRcount++; BrIR *br = new BrIR(IRcount, _OP_BR, to_string(IRcount + 1), "", "%" + to_string(CondResult->Id)); + br->isIF=true; if (CondResult->valueType != _IntImm) { br->LinkIR(CurrentIR); @@ -876,6 +877,7 @@ void IfNode(ast *node) //{ IRcount++; BrIR *br_next_t = new BrIR(IRcount, _OP_BR, to_string(IRcount + 1), "", ""); + br_next_t->isIF=true; br_next_t->LinkIR(CurrentIR); CurrentIR = br_next_t; // } @@ -941,6 +943,7 @@ void IfElseNode(ast *node) // 建立IR br arg1:true arg2:next result:cond_value IRcount++; BrIR *br = new BrIR(IRcount, _OP_BR, to_string(IRcount + 1), "", "%" + to_string(CondResult->Id)); + br->isIF=true; if (CondResult->valueType != _IntImm) { br->LinkIR(CurrentIR); @@ -982,6 +985,7 @@ void IfElseNode(ast *node) // if (!(IRTable[IRcount - 1]->op == _OP_BR && (IRTable[IRcount - 2]->op == _OP_RET_EXP || IRTable[IRcount - 2]->op == _OP_RET_EXP))) // { br_next_t = new BrIR(++IRcount, _OP_BR, "", "", ""); + br_next_t->isIF=true; br_next_t->LinkIR(CurrentIR); CurrentIR = br_next_t; //} @@ -1020,6 +1024,7 @@ void IfElseNode(ast *node) { IRcount++; BrIR *br_next_f = new BrIR(IRcount, _OP_BR, to_string(IRcount + 1), "", ""); + br_next_f->isIF=true; br_next_f->LinkIR(CurrentIR); CurrentIR = br_next_f; } diff --git a/include/IR.hpp b/include/IR.hpp index a16cc6b11556fdc5ffb585890b0b160eebb4b93d..823468c45c712a7733ae3c17597c95c2f1447e65 100644 --- a/include/IR.hpp +++ b/include/IR.hpp @@ -63,6 +63,7 @@ class BrIR : public IR public: IR *BrArg1 = nullptr; IR *BrArg2 = nullptr; + bool isIF = false; int tothis = 0; // 跳转到br的次数 BrIR(int _id, int _op, string _arg1 = "", string _arg2 = "", string _result = "", IR *br_arg1 = nullptr, IR *br_arg2 = nullptr) : IR(_id, _op, _arg1, _arg2, _result) { diff --git a/include/optimizerIR.hpp b/include/optimizerIR.hpp index 879b9e7d0d357386591756f244906dddcb097177..0449b140473bbc6b2e9d370e7f7d1d8f9c8e1036 100644 --- a/include/optimizerIR.hpp +++ b/include/optimizerIR.hpp @@ -20,6 +20,7 @@ void DataFlowAnalysis(); void LoopOpt(); void FindInvariant(int i); void CodeMotion(int i); +int CheckIF(BasicBlock* currentblock); void GetUD(BasicBlock *block, string var, IR *varIR); void GetDU(BasicBlock *block, string var, IR *varIR); void DelNegIR(); diff --git a/main.cpp b/main.cpp index 87558cb26445c13abe5b37f057e2e41e18eee9f4..d0c9e80b9369acff700e15e2cdb1bcb4bd1f87fb 100644 --- a/main.cpp +++ b/main.cpp @@ -29,10 +29,10 @@ int main(int argc, char *argv[]) if (argc == 6) { - ShowIDTable(); - ShowIRTable(); + //ShowIDTable(); + //ShowIRTable(); Optimizer(); - ShowIRTable(); + //ShowIRTable(); _VisitIRTable2(); _Peephole(); _ShowArm(argv[3]); @@ -40,6 +40,7 @@ int main(int argc, char *argv[]) else { _VisitIRTable2(); + _Peephole(); _ShowArm(argv[3]); } diff --git a/testcase.s b/testcase.s index de340da9c07727090b0bd3c91c96f33c719fc2b6..0aae668b1d207d2238212871c11feb1404a7388e 100644 --- a/testcase.s +++ b/testcase.s @@ -6,94 +6,65 @@ .data .text main: - push {fp, lr} - add fp, sp, #4 - sub sp, sp, #140 + push {fp} + add fp, sp, #0 + sub sp, sp, #108 push {r5,r6,r7} - movw r4, #893 - movt r4, #0 + mov r4, #0 str r4, [fp, #-24] - mov r4, #716 + mov r4, #0 str r4, [fp, #-20] - movw r4, #837 - movt r4, #0 + ldr r5, [fp, #-24] + add r5, r5, #-2 + str r5, [fp, #-36] + ldr r5, [fp, #-20] + ldr r6, [fp, #-36] + add r5, r5, r6 + mov r4, #2 str r4, [fp, #-16] - ldr r5, [fp, #-16] - add r5, r5, #128 - mov r4, #241 - str r4, [fp, #-12] - ldr r6, [fp, #-12] + ldr r6, [fp, #-16] + add r6, r6, #1 + str r6, [fp, #-48] + ldr r6, [fp, #-48] add r5, r5, r6 - sub r5, r5, #412 - movw r4, #771 - movt r4, #0 - str r4, [fp, #-8] + mov r4, #3 + str r4, [fp, #-12] + mov r6, #2 + str r6, [fp, #-8] ldr r6, [fp, #-12] ldr r7, [fp, #-8] add r6, r6, r7 - sub r6, r6, #18 - add r6, r6, r5 - sub r6, r6, #33 + str r6, [fp, #-60] + ldr r6, [fp, #-60] + add r5, r5, r6 + mov r4, #2 + str r4, [fp, #-8] + ldr r6, [fp, #-8] + add r6, r6, #-3 str r6, [fp, #-72] - ldr r5, [fp, #-72] - sub r5, r5, #55 ldr r6, [fp, #-72] add r5, r5, r6 - mov r1, #21 - mov r0, r5 - bl __aeabi_idivmod - mov r5, r1 - mov r0, r5 - b .L0 - ldr r5, [fp, #-20] - ldr r6, [fp, #-16] + ldr r6, [fp, #-72] + add r6, r6, #-3 + str r6, [fp, #-80] + ldr r6, [fp, #-80] + str r6, [fp, #-8] + ldr r6, [fp, #-80] add r5, r5, r6 - sub r5, r5, #97 - str r5, [fp, #-92] - ldr r5, [fp, #-20] - ldr r6, [fp, #-92] - sub r5, r5, r6 - mov r1, #62 - mov r0, r5 - bl __aeabi_idivmod - mov r5, r1 - ldr r6, [fp, #-92] - add r6, r6, r5 - mov r1, #17 - mov r0, r6 - bl __aeabi_idivmod - mov r5, r1 - mov r0, r5 - b .L0 - ldr r5, [fp, #-16] - ldr r6, [fp, #-20] + ldr r6, [fp, #-60] + add r6, r6, #1 add r5, r5, r6 - mov r1, #13 - mov r0, r5 - bl __aeabi_idivmod - mov r5, r1 - mov r0, r5 - b .L0 - ldr r5, [fp, #-20] - ldr r6, [fp, #-24] - mul r5, r5, r6 - mov r1, #83 - mov r0, r5 - bl __aeabi_idivmod - mov r5, r1 - ldr r6, [fp, #-20] + ldr r6, [fp, #-48] + ldr r7, [fp, #-8] + add r6, r6, r7 + add r5, r5, r6 + ldr r6, [fp, #-36] + add r6, r6, #-5 add r5, r5, r6 - movw r4, #771 - movt r4, #0 - sub r5, r5, r4 - str r-1, [fp, #-124] - add r-1, r-1, r5 - mov r1, #11 - mov r0, r-1 - bl __aeabi_idivmod - mov r5, r1 + str r5, [fp, #-20] + ldr r5, [fp, #-20] mov r0, r5 -.L0: pop {r5,r6,r7} - sub sp, fp, #4 - pop {fp, pc} + sub sp, fp, #0 + pop {fp} + bx lr diff --git a/testcase.sy b/testcase.sy index 8ced64623882c7ba4cc8d8ad5487cbf4d9ec7113..e99562bd2e5312508692d9b4af0979280435db13 100644 --- a/testcase.sy +++ b/testcase.sy @@ -1,64 +1,29 @@ -int n; -int meanless_calculation(int x, int y){ - int i = 0; - int ret = 0; - while (i < x && i < y){ - i = i + 1; - ret = ret + x + i; - } - return ret; -} - -int swap(int arr[], int l, int r){ - int curr = arr[l]; - arr[l] = arr[r]; - arr[r] = curr; - return meanless_calculation(l, r); -} - -int median(int arr[], int begin, int end, int pos){ - int pivot = arr[begin]; - - int l = begin; - int r = end + 1; - int xx = 0; - while (1 == 1){ - while (l < r){ - r = r - 1; - if(arr[r] < pivot){ - break; +int main() { + int a = 0, sum = 0; + a = a + -2; + sum = sum + a; + { + int a = 2; + a = a + 1; + sum = sum + a; + { + int a = 3; + a = a + 2; + sum = sum + a; + { + int a = 2; + a = a + -3; + sum = sum + a; + a = a + -3; + sum = sum + a; } - xx = xx + 1; - } - while (l < r){ - l = l + 1; - if(arr[l] >= pivot){ - break; - } - xx = xx - 1; + a = a + 1; + sum = sum + a; } - - if (l == r) break; - else { - swap(arr, l, r); - } - } - arr[begin] = pivot; - swap(arr, begin, l); - - if (l > pos) return median(arr, begin, l, pos); - if (l < pos) return median(arr, l + 1, end, pos); - - return xx; -} - -int a[10000000]; - -int main(){ - n = getarray(a); - starttime(); - median(a, 0, n-1, n/2); - stoptime(); - putarray(n, a); - return a[n/2] % 256; + a = a + 2; + sum = sum + a; + } + a = a + -5; + sum = sum + a; + return sum; }