Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of def-use chain #156

Merged
merged 1 commit into from
Nov 7, 2024
Merged

Conversation

nosba0957
Copy link
Collaborator

Implement a new structure for def-use chain called uc_node_t. uc_node_t is stored within struct var, includes insn element that records the instruction using the variable which the use chain belongs to.

Simplify CSE procedure with use chain information. We build the def-use chain information by build_use_chain function before the optimizing phase. In addtion, When the instructions are eliminated by CSE, we delete its use chain nodes from both variables(rs1, rs2) at the same time.

The CSE method has been modified. When the first pair of instructions is found, the use chain is utilized to search for identical ADD instructions. Subsequently, the next instruction is verified to ensure it match our target. After removing the instruction, utilize the use chain to find the next target instruction.

resolve #155

@jserv jserv requested a review from vacantron November 4, 2024 17:47
Copy link
Collaborator

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebase the latest master branch to resolve CI breakage.

@jserv
Copy link
Collaborator

jserv commented Nov 4, 2024

I would like to ask @fennecJ for reviewing.

src/defs.h Outdated Show resolved Hide resolved
src/defs.h Outdated Show resolved Hide resolved
@jserv
Copy link
Collaborator

jserv commented Nov 5, 2024

Can you provide test cases accordingly?

src/ssa.c Outdated Show resolved Hide resolved
src/defs.h Outdated Show resolved Hide resolved
@nosba0957
Copy link
Collaborator Author

nosba0957 commented Nov 5, 2024

Can you provide test cases accordingly?

Do you mean adding test cases in driver.sh? But I don't think that would demonstrate the effect of CSE. Or should I provide the differences in assembly before and after implementing CSE?

@jserv
Copy link
Collaborator

jserv commented Nov 5, 2024

should I provide the differences in assembly before and after implementing CSE?

Yes, it makes sense.

@nosba0957 nosba0957 force-pushed the master branch 2 times, most recently from 99d2dac to d9e48c9 Compare November 6, 2024 11:39
src/ssa.c Outdated Show resolved Hide resolved
src/ssa.c Outdated Show resolved Hide resolved
Implement a new structure for def-use chain called "use_chain_t".
"use_chain_t" is stored within "struct var", includes "insn" element
that records the instruction using the variable which the use chain
belongs to.

Simplify CSE procedure with use chain information. We build the def-use
chain information by "build_users" function before the optimizing phase.
In addtion, When the instructions are eliminated by CSE, we delete its
use chain nodes from both variables("rs1", "rs2") at the same time.

The CSE method has been modified. When the first pair of instructions is
found, the use chain is utilized to search for identical ADD
instructions. Subsequently, the next instruction is verified to ensure
it match our target. After removing the instruction, utilize the use
chain to find the next target instruction.

Use the following code as an example. Note that it should be compiled
with option "--no-libc" and the DCE should be disabled.
```
int main()
{
    char arr[1];
    int i, t, pos = 0;
    for (i = 0; i < 10000000; i++)
        t = arr[pos] + arr[pos];
    return 0;
}
```
The difference between before and after CSE:

```
 ldr  r0, [sp, sysprog21#4]
 ldr  r1, [sp, sysprog21#9]
 add  r2, r0, r1
 ldrb r3, [r2]
-add  r2, r0, r1
-ldrb r4, [r2]
+mov  r2, r3
```
@jserv jserv requested a review from fennecJ November 6, 2024 14:35
@jserv jserv merged commit 005956a into sysprog21:master Nov 7, 2024
4 checks passed
@jserv
Copy link
Collaborator

jserv commented Nov 7, 2024

Thank @nosba0957 for contributing!

@fennecJ
Copy link
Collaborator

fennecJ commented Nov 7, 2024

Apologies, I just reviewed the PR and was preparing my response.

The functionality looks correct, and the generated binary code matches the original implementation based on my offline tests. However, I have a concern about the simplification—it doesn’t seem to achieve the expected level of simplification. Nevertheless, I believe the DU-chain implementation can still be useful in the future for further optimizations.

Additionally, I think it’s important to properly clean up the allocated memory when constructing the DU-chain at the end of the compilation process.

@jserv
Copy link
Collaborator

jserv commented Nov 7, 2024

I have a concern about the simplification—it doesn’t seem to achieve the expected level of simplification. Nevertheless, I believe the DU-chain implementation can still be useful in the future for further optimizations.
Additionally, I think it’s important to properly clean up the allocated memory when constructing the DU-chain at the end of the compilation process.

No problem. Create another GitHub issue for tracking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implementation of def-use-chain
3 participants