willi19 / swpp202301-compiler-team6

MIT License
0 stars 0 forks source link

[Sprint 1] Add SimplifyCFGPass #8

Closed germanium32 closed 1 year ago

germanium32 commented 1 year ago

Overview

Add SimplifyCFG pass, which canonicalizes the control flow graph of the given ll. The SimplifyCFGPass does several tasks, including:

  1. Dead Branch Elimination: Removing branches that are guaranteed to never be taken.
  2. Merge Block: Merging to basic blocks when one ends with an unconditional branch an the other starts with the same branch.
  3. Switch Conversion: Converting statements with few cases and constant conditions into a sequence of conditional branches(if -> switch)
  4. PHI Node Elimination: Eliminating unnecessary PHI nodes that are introduced by previous passes.
  5. Remove Unnecessary Control Flow: If a basic block contains a branch that always leads to another block, and the instructions can be moved to the other block, remove the current block and redirect to the branch. and more.

Implementation

Include llvm/Transforms/Scalar/SimplifyCFG.h into the opt.cpp file, and add it to the FunctionAnalysisManager.

Unit tests (SimplifyCFG-Test#.ll)

  1. Merge basic blocks + Remove Unreachable Code
  2. Switch conversion
  3. Remove unnecessary control flow
sharaelong commented 1 year ago

I can see unnecessary tab or spaces in opt.cpp. I have a question for test case 2. What DEF:% .* responsible for in this test? Other things LGTM.

germanium32 commented 1 year ago

I can see unnecessary tab or spaces in opt.cpp. I have a question for test case 2. What DEF:% .* responsible for in this test? Other things LGTM.

Ah, for the first comment, I will check and remove redundant tabs and spaces.

For the second comment, the [DEF:%.*] is the name tag for the default block of switch-case argument. If it caused confusion from the ambiguity of the abbreviation "def," I'll try to use more explicit names.

germanium32 commented 1 year ago

Clang-formatted files and moved testcases' directory!