forked from NUDT-compiler/nudt-compiler-cpp
parent
e9adbe38c7
commit
7ab465d25b
@ -0,0 +1,83 @@
|
||||
#ifndef IR_PASSES_PASSMANAGER_H_
|
||||
#define IR_PASSES_PASSMANAGER_H_
|
||||
|
||||
#include "ir/IR.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace ir {
|
||||
|
||||
void RunMem2Reg(Module& module);
|
||||
void RunLICM(Module* module);
|
||||
void RunConstFold(Module& module);
|
||||
void RunConstProp(Module& module);
|
||||
void RunDCE(Module& module);
|
||||
void RunCFGSimplify(Module& module);
|
||||
void RunCSE(Module& module);
|
||||
|
||||
class PassManagerModule {
|
||||
public:
|
||||
explicit PassManagerModule(Module* module) : module_(module) {}
|
||||
|
||||
void Run() {
|
||||
if (!module_) {
|
||||
return;
|
||||
}
|
||||
|
||||
RunMem2Reg(*module_);
|
||||
|
||||
RunLICM(module_);
|
||||
|
||||
bool changed = true;
|
||||
int max_iterations = 10;
|
||||
int iterations = 0;
|
||||
|
||||
while (changed && iterations < max_iterations) {
|
||||
changed = false;
|
||||
iterations++;
|
||||
|
||||
auto before = SerializeModule(*module_);
|
||||
|
||||
RunConstFold(*module_);
|
||||
RunConstProp(*module_);
|
||||
RunCFGSimplify(*module_);
|
||||
RunCSE(*module_);
|
||||
RunDCE(*module_);
|
||||
|
||||
auto after = SerializeModule(*module_);
|
||||
changed = (before != after);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::string SerializeModule(const Module& module) {
|
||||
std::ostringstream oss;
|
||||
IRPrinter printer;
|
||||
printer.Print(module, oss);
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
Module* module_;
|
||||
};
|
||||
|
||||
class PassManager {
|
||||
public:
|
||||
PassManager() = default;
|
||||
|
||||
void RunScalarOptimizationPasses(Module* module) {
|
||||
if (!module) return;
|
||||
|
||||
RunMem2Reg(*module);
|
||||
|
||||
RunConstFold(*module);
|
||||
RunDCE(*module);
|
||||
RunCFGSimplify(*module);
|
||||
}
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
} // namespace ir
|
||||
|
||||
#endif // IR_PASSES_PASSMANAGER_H_
|
||||
Loading…
Reference in new issue