-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the trivial dead code elmination algorithm
- Loading branch information
1 parent
c0ab3e1
commit 0874fd7
Showing
15 changed files
with
186 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef DEAD_CODE_ELIMINATION_H | ||
#define DEAD_CODE_ELIMINATION_H | ||
|
||
#include "config.h" | ||
#include <nlohmann/json.hpp> | ||
#include <unordered_set> | ||
#include <unordered_map> | ||
#include <vector> | ||
#include <tuple> | ||
#include <functional> | ||
#include <iostream> | ||
|
||
using json = nlohmann::json; | ||
|
||
void deadCodeElimination(std::vector<std::vector<json>>& blocks, DCEConfig& config); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include "config.h" | ||
|
||
PassConfig* createPassConfig(const std::string& passName) { | ||
if (passName == "LVN") { | ||
return new LVNConfig(false, false, false); | ||
} else if (passName == "DCE") { | ||
return new DCEConfig(false); | ||
} | ||
return nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "deadCodeElimination.h" | ||
#include "logger.h" | ||
|
||
void deadCodeElimination(std::vector<std::vector<json>>& blocks, DCEConfig& config) { | ||
std::unordered_set<std::string> usedVars; | ||
// 1. Traverse the blocks to get all used variables | ||
for (auto &block : blocks) { | ||
for (auto &instr : block) { | ||
for (auto &arg : instr["args"]) { | ||
usedVars.insert(arg.get<std::string>()); | ||
} | ||
} | ||
} | ||
// 2. Traverse the blocks to remove unused variables | ||
bool changed = false; | ||
for (auto &block : blocks) { | ||
std::vector<json> newBlock; | ||
for (auto &instr : block) { | ||
if (instr.find("dest") == instr.end() || usedVars.find(instr["dest"].get<std::string>()) != usedVars.end()) { | ||
LOG_DEBUG(instr.dump()); | ||
newBlock.push_back(instr); | ||
} | ||
} | ||
if (block.size() != newBlock.size()) { | ||
changed = true; | ||
} | ||
block = newBlock; | ||
} | ||
if (changed) { | ||
deadCodeElimination(blocks, config); | ||
} | ||
LOG_DEBUG("\n"); | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "deadCodeElimination.h" | ||
#include "common.h" | ||
#include "buildBlocks.h" | ||
#include "logger.h" | ||
|
||
int main(int argc, char* argv[]) { | ||
try { | ||
Logger::getInstance().setLogLevel(LogLevel::INFO); | ||
json program = parseJsonFromStdin(); | ||
LOG_DEBUG("Starting dead code elimination"); | ||
for (auto& func : program["functions"]) { | ||
std::vector<std::vector<json>> blocks = buildBlocks(func["instrs"]); | ||
std::vector<std::string> args(argv + 1, argv + argc); | ||
DCEConfig* config = static_cast<DCEConfig*>(createPassConfig("DCE")); | ||
for (auto& arg : args) { | ||
if (arg == "-g") { | ||
Logger::getInstance().setLogLevel(LogLevel::DEBUG); | ||
} | ||
if (arg == "-a") { | ||
config->enableAggressiveDCE = true; | ||
} | ||
} | ||
deadCodeElimination(blocks, *config); | ||
delete config; | ||
LOG_DEBUG("After dead code elimination"); | ||
//printBlocks(blocks, false); | ||
func["instrs"] = flattenBlocks(blocks); | ||
} | ||
std::cout << program.dump(2) << std::endl; | ||
LOG_DEBUG("Local value numbering finished"); | ||
} catch (const std::exception& e) { | ||
LOG_ERROR(std::string("Error: ") + e.what()); | ||
return 1; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@main { | ||
num1: int = const 1; | ||
num2: int = const 2; | ||
should_be_removed1: int = const 3; | ||
should_be_removed2: int = const 4; | ||
should_not_be_removed: int = const 5; | ||
|
||
num3: int = add num1 num2; | ||
num4: int = add num3 num3; | ||
num5: int = and num4 should_not_be_removed; | ||
print num4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@main { | ||
num1: int = const 1; | ||
num2: int = const 2; | ||
should_be_removed2: int = add num1 num2; | ||
should_not_be_removed1: int = id should_be_removed2; | ||
num4: int = add num3 num3; | ||
print num4; | ||
print should_not_be_removed1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@main { | ||
num1: int = const 1; | ||
num2: int = const 2; | ||
should_be_removed1: int = const 3; | ||
should_be_removed2: int = add num1 num2; | ||
should_be_removed3: int = add num1 num2; | ||
should_not_be_removed1: int = add num1 num2; | ||
|
||
num4: int = add num3 num3; | ||
print num4; | ||
print should_not_be_removed1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@main { | ||
num1: int = const 1; | ||
num2: int = const 2; | ||
should_not_be_removed1: int = add num1 num2; | ||
num4: int = add num3 num3; | ||
print num4; | ||
print should_not_be_removed1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@main{ | ||
a: int = const 1; | ||
b: int = const 1; | ||
two: int = const 2; | ||
t1: int = mul a two; | ||
t2: int = mul a two; | ||
t3: int = mul t2 b; | ||
result: int = add t1 t3; | ||
print result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@main { | ||
t1: int = mul a two; | ||
t2: int = mul t1 b; | ||
result: int = add t1 t2; | ||
print result; | ||
} |