willi19 / swpp202301-compiler-team6

MIT License
0 stars 0 forks source link

[Sprint 1] Add Mem2Reg Pass #9

Closed germanium32 closed 1 year ago

germanium32 commented 1 year ago

Overview

Add Mem2Reg pass, which transforms the program's SSA form by promoting stack-allocated memory (allocated by alloca instructions) to register-allocated memory. This reduces the costly use of stack memory into the use of rather cheap registers. This may cause a temporary problem; since it increases the register pressure, however, this can be handled by the nature of registers.

Implementation

Include llvm/Transforms/Utils/Mem2Reg.h into the opt.cpp file, and add it to the FunctionAnalysisManager in the form of llvm::PromotePass.

Unit tests (Mem2Reg-Test#.ll)

  1. Function that have alloca with only has one of each load/store operation -> Must Optimize
  2. Function that have alloca, but has function calls -> Must Not Optimize
  3. Function that have alloca with multiple load/store operations, but not else -> Must Optimize
sharaelong commented 1 year ago

In opt.cpp, FPM.addPass() line style is different from your previous PR: SimplifyCFGPass. Your test cases looks good to me. I'm curious about using malloc with small size. This case will be optimized too?

germanium32 commented 1 year ago

In opt.cpp, FPM.addPass() line style is different from your previous PR: SimplifyCFGPass. Your test cases looks good to me. I'm curious about using malloc with small size. This case will be optimized too?

Thanks, I'll consider consistency of code style.

I'm afraid that malloc instructions will not be optimized. Take a look at the mem2reg.cpp file here. This code only checks for alloca instructions.

However, maybe your idea may be an improvement to this. We can promote mallocs to allocas that requires small(within our threshold) memory.

germanium32 commented 1 year ago

Clang-formatted files and moved testcases' directory!