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

Implement dead code elimination #145

Merged
merged 2 commits into from
Aug 14, 2024
Merged

Implement dead code elimination #145

merged 2 commits into from
Aug 14, 2024

Conversation

nosba0957
Copy link
Collaborator

The creation of RDF is based on reverse CFG. The method of building RDF is similar to building DF, but instead of iterating every prev nodes, it visit next, then_ and else_ nodes. The code appears to extend the for-loop to handle three cases (compared to the method of building DF).
Due to the addition of RDF, several elements were added to the struct basic_block. However, MAX_FILED macro is not large enough, so it has been adjusted to 64.
DCE is implemented using mark-sweep algorithm. dce_insn marks the useful instruction and dce_sweep removes the operations that are not marked "useful". The dce_insn must execute after cse and const_folding are done because dce_insn may mark some instructions that are not handled by cse and const_folding. These instruction might be mistakenly mark "useful".

Related issue: #88

@jserv jserv requested a review from vacantron August 5, 2024 21:32
src/defs.h Outdated Show resolved Hide resolved
src/ssa.c Outdated Show resolved Hide resolved
src/ssa.c Outdated Show resolved Hide resolved
src/ssa.c Outdated Show resolved Hide resolved
src/defs.h Outdated Show resolved Hide resolved
@jserv
Copy link
Collaborator

jserv commented Aug 8, 2024

I defer to @vacantron for confirmation.

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.

Remove incorrect description "51,357 lines instructions." You should explicitly mention 51,357 instructions" for certain ISA.

@jserv
Copy link
Collaborator

jserv commented Aug 9, 2024

Once the DCE is known to work properly, we can close #88 to declare the capability of performing basic optimizations.

This error occurred when a return statement was in for-loop body. After
parsing loop body, "body_" was assigned NULL as the return value,
causing the compiler to failed in correctly connect "inc_" and
"cond_start" basic blocks.

This error can be analyzed by examining the CFG.dot file.
Introduces the creaetion of reverse dominance frontier (RDF) to support
dead code elimination (DCE). Method for implementing RDF is similar to
that of dominance frontier (DF). The key difference is that RDF is
computed on the reverse CFG. In other words, operations were performed
on "prev[]" which in the basic block structure, now switched to operate
on "next", "then_" and "else_".

In the "dce_insn" function, mark useful instructions during the initial
analysis of the current basic block. Continue identifying useful
instructions by tracing back from the last assigned instruction of the
both operands of the current "insn". In the "dce_sweep" function remove
the useless instruction from the current "insn_list". If a branch
instruction is encountered, remove it and reconnect the current basic
block to its reverse immediate dominator.

Before implementing DCE, compiling "src/main.c" resulted in an
executable with 51,357 instructions for ARMv7-A. After DCE, the
executable was 51,330 instructions, reducing the total by 27
instructions.
@jserv jserv merged commit 2b9ad5d into sysprog21:master Aug 14, 2024
4 checks passed
@jserv
Copy link
Collaborator

jserv commented Aug 14, 2024

Thank @nosba0957 for contributing!

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.

3 participants