Skip to content

Commit

Permalink
Change the output in local value numbering to integrate with the bril…
Browse files Browse the repository at this point in the history
… tools
  • Loading branch information
chuang0221 committed Sep 4, 2024
1 parent ba5c795 commit 1ae4aaa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
mkdir build
cd build
cmake ..
make
2 changes: 2 additions & 0 deletions include/buildBlocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ bool isTerminator(json instr);
std::vector<std::vector<json>> buildBlocks(json instrs);
void printBlock(std::vector<json> block, bool withLabel);
void printBlocks(std::vector<std::vector<json>> blocks, bool withLabel);
json flattenBlock(std::vector<json> block);
json flattenBlocks(std::vector<std::vector<json>> blocks);

#endif
10 changes: 10 additions & 0 deletions src/buildBlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ std::vector<std::vector<json>> buildBlocks(json instrs) {
return blocks;
}

json flattenBlocks(std::vector<std::vector<json>> blocks) {
json flat;
for (auto block: blocks) {
for (auto instr: block) {
flat.push_back(instr);
}
}
return flat;
}

void printBlock(std::vector<json> block, bool withLabel) {
for (int i = 0; i < block.size(); i++) {
if (withLabel) {
Expand Down
16 changes: 9 additions & 7 deletions src/localValueNumberingMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
int main(int argc, char* argv[]) {
try {
Logger::getInstance().setLogLevel(LogLevel::INFO);
const json program = parseJsonFromStdin();
LOG_INFO("Starting local value numbering");
json program = parseJsonFromStdin();
LOG_DEBUG("Starting local value numbering");
for (auto& func : program["functions"]) {
std::vector<std::vector<json>> blocks = buildBlocks(func["instrs"]);
LOG_INFO("Before local value numbering");
printBlocks(blocks, true);
LOG_DEBUG("Before local value numbering");
//printBlocks(blocks, true);
std::vector<std::string> args(argv + 1, argv + argc);
Config config(false, false, false);
for (auto& arg : args) {
Expand All @@ -29,10 +29,12 @@ int main(int argc, char* argv[]) {
}
}
localValueNumbering(blocks, config);
LOG_INFO("After local value numbering");
printBlocks(blocks, true);
LOG_DEBUG("After local value numbering");
//printBlocks(blocks, false);
func["instrs"] = flattenBlocks(blocks);
}
LOG_INFO("Local value numbering finished");
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;
Expand Down
6 changes: 3 additions & 3 deletions test/lvn/invalid_example.bril
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
x: int = const 1;
y: int = const 2;
a: int = add x y;
b: int = add x y;
b: int = add y x;
a: int = const 17;
c: int = add x y;
d: int = add x y;
c: int = add y x;
d: int = add y x;
}

0 comments on commit 1ae4aaa

Please sign in to comment.