Skip to content

Commit

Permalink
ret terminator
Browse files Browse the repository at this point in the history
  • Loading branch information
vla5924 committed Oct 1, 2024
1 parent 714a6c0 commit f58eb19
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <deque>
#include <string>
#include <string_view>
#include <unordered_map>
Expand Down Expand Up @@ -34,11 +35,13 @@ class LLVMIRGenerator {
std::unordered_map<llvm::Value *, llvm::Type *> typedValues;
std::unordered_map<std::string, llvm::Value *> globalStrings;
std::unordered_map<std::string_view, llvm::FunctionCallee> externalFunctions;
std::deque<llvm::BasicBlock *> basicBlocks;

llvm::Value *findValue(const Value::Ptr &value) const;
void saveValue(const Value::Ptr &value, llvm::Value *llvmValue);
llvm::Type *convertType(const Type::Ptr &type);
llvm::BasicBlock *createBlock();
void eraseDeadBlocks();
llvm::Value *normalizePredicate(const Value::Ptr &cond);
llvm::Value *getGlobalString(const std::string &str);
llvm::FunctionCallee getExternalFunction(std::string_view name);
Expand Down
22 changes: 20 additions & 2 deletions compiler/lib/codegen/optree_to_llvmir/llvmir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <system_error>
#include <vector>

#include <llvm/IR/CFG.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Type.h>
Expand Down Expand Up @@ -83,7 +84,20 @@ llvm::Type *LLVMIRGenerator::convertType(const Type::Ptr &type) {
}

llvm::BasicBlock *LLVMIRGenerator::createBlock() {
return llvm::BasicBlock::Create(context, "bb", currentFunction);
auto *bb = llvm::BasicBlock::Create(context, "bb", currentFunction);
basicBlocks.push_back(bb);
return bb;
}

void LLVMIRGenerator::eraseDeadBlocks() {
for (auto *&bb : basicBlocks) {
if (bb == nullptr)
continue;
if (llvm::pred_empty(bb) && !bb->isEntryBlock()) {
bb->eraseFromParent();
bb = nullptr;
}
}
}

llvm::Value *LLVMIRGenerator::normalizePredicate(const Value::Ptr &cond) {
Expand Down Expand Up @@ -208,6 +222,8 @@ void LLVMIRGenerator::visit(const ReturnOp &op) {
builder.CreateRetVoid();
else
builder.CreateRet(findValue(op.value()));
auto *bb = createBlock();
builder.SetInsertPoint(bb);
}

void LLVMIRGenerator::visit(const ConstantOp &op) {
Expand Down Expand Up @@ -347,6 +363,7 @@ void LLVMIRGenerator::visit(const IfOp &op) {
auto *thenBlock = createBlock();
builder.SetInsertPoint(thenBlock);
visit(op.thenOp());
auto *newThenBlock = builder.GetInsertBlock();
auto *elseBlock = createBlock();
auto *nextBlock = elseBlock;
if (auto elseOp = op.elseOp()) {
Expand All @@ -355,7 +372,7 @@ void LLVMIRGenerator::visit(const IfOp &op) {
nextBlock = createBlock();
builder.CreateBr(nextBlock);
}
builder.SetInsertPoint(thenBlock);
builder.SetInsertPoint(newThenBlock);
builder.CreateBr(nextBlock);
builder.SetInsertPoint(prevBlock);
builder.CreateCondBr(normalizePredicate(op.cond()), thenBlock, elseBlock);
Expand Down Expand Up @@ -431,6 +448,7 @@ void LLVMIRGenerator::visit(const PrintOp &op) {

void LLVMIRGenerator::process(const Program &program) {
visit(program.root);
eraseDeadBlocks();
}

std::string LLVMIRGenerator::dump() const {
Expand Down

0 comments on commit f58eb19

Please sign in to comment.