chharvey / counterpoint

A robust programming language.
GNU Affero General Public License v3.0
2 stars 0 forks source link

Compiler Technique: DCE, Statements #31

Open chharvey opened 4 years ago

chharvey commented 4 years ago

Dead code elimination (DCE) is a compiler technique in which output is not produced for code that is “unreachable” — code that is impossible to execute. The compiler will not produce output for statements that are known at compile-time to be unreachable.

Conditional Statements

if ‹condition› then ‹consequent› else ‹alternative›;

For conditional statements, if the compiler can determine that ‹condition› is true, then it will not produce output for ‹alternative›. Likewise, if ‹condition› is false, then no output is produced for ‹consequent›.

No specification for unless conditional statements is needed, since it is syntax sugar for if conditional statements.

Loops

while ‹condition› do ‹body›;
do ‹body› while ‹condition›;

For “while–do” statements, if the compiler can determine that ‹condition› is false, then it will not produce output for ‹body›. There is no compiler optimization for “do–while” statements, since ‹body› will definitely execute at least once.

No specification for until loops is needed, since it is syntax sugar for while loops.

for ‹index› from ‹start› to ‹end› by ‹delta› do ‹body›;

For “for” statements, if the compiler can determine that the range [‹start›, ‹end›) is empty, then it will not produce output for ‹body›.