Skip to content

Commit

Permalink
try to delete some probably unneeded bitcasts (#1163)
Browse files Browse the repository at this point in the history
When we updated to the version of the llvm backend with opaque pointers,
all pointer types become the same type in llvm bitcode. As such, we no
longer need bitcast instructions to cast from one pointer type to
another. These now redundant instructions can be removed.
  • Loading branch information
dwightguth authored Nov 25, 2024
1 parent c655b8b commit 5e07327
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 48 deletions.
6 changes: 2 additions & 4 deletions lib/codegen/CreateStaticTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ llvm::Constant *create_static_term::not_injection_case(

std::vector<llvm::Constant *> idxs
= {llvm::ConstantInt::get(llvm::Type::getInt64Ty(ctx_), 0)};
return llvm::ConstantExpr::getBitCast(
llvm::ConstantExpr::getInBoundsGetElementPtr(
block_type, global_var, idxs),
llvm::PointerType::getUnqual(module_->getContext()));
return llvm::ConstantExpr::getInBoundsGetElementPtr(
block_type, global_var, idxs);
}

std::pair<llvm::Constant *, bool>
Expand Down
5 changes: 2 additions & 3 deletions lib/codegen/CreateTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,15 +974,14 @@ llvm::Value *create_term::not_injection_case(
}

auto *block_ptr = llvm::PointerType::getUnqual(module_->getContext());
auto *bitcast = new llvm::BitCastInst(block, block_ptr, "", current_block_);
if (symbol_decl->attributes().contains(attribute_set::key::Binder)) {
auto *call = llvm::CallInst::Create(
get_or_insert_function(module_, "debruijnize", block_ptr, block_ptr),
bitcast, "withIndices", current_block_);
block, "withIndices", current_block_);
set_debug_loc(call);
return call;
}
return bitcast;
return block;
}

// returns a value and a boolean indicating whether that value could be an
Expand Down
22 changes: 7 additions & 15 deletions lib/codegen/Decision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ void decision::operator()(decision_node *entry) {
if (fail_pattern_) {
llvm::Value *val = load(std::make_pair(
"_1", getvalue_type({sort_category::Symbol, 0}, module_)));
fail_subject_->addIncoming(
new llvm::BitCastInst(
val, llvm::PointerType::getUnqual(ctx_), "", current_block_),
current_block_);
fail_subject_->addIncoming(val, current_block_);
fail_pattern_->addIncoming(
string_literal("\\bottom{SortGeneratedTopCell{}}()"), current_block_);
fail_sort_->addIncoming(
Expand All @@ -71,8 +68,7 @@ llvm::Value *decision::ptr_term(llvm::Value *val) {
if (val->getType()->isIntegerTy()) {
val = allocate_term(val->getType(), current_block_, "kore_alloc_always_gc");
}
return new llvm::BitCastInst(
val, llvm::PointerType::getUnqual(ctx_), "", current_block_);
return val;
}

bool decision_node::begin_node(decision *d, std::string const &name) {
Expand Down Expand Up @@ -236,14 +232,13 @@ void switch_node::codegen(decision *d) {
int offset = 0;
llvm::StructType *block_type = get_block_type(
d->module_, d->definition_, switch_case.get_constructor());
auto *cast = new llvm::BitCastInst(val, ptr_ty, "", d->current_block_);
kore_symbol_declaration *symbol_decl
= d->definition_->get_symbol_declarations().at(
switch_case.get_constructor()->get_name());
llvm::Instruction *renamed = nullptr;
for (auto const &binding : switch_case.get_bindings()) {
llvm::Value *child_ptr = llvm::GetElementPtrInst::CreateInBounds(
block_type, cast,
block_type, val,
{llvm::ConstantInt::get(llvm::Type::getInt64Ty(d->ctx_), 0),
llvm::ConstantInt::get(
llvm::Type::getInt32Ty(d->ctx_),
Expand Down Expand Up @@ -894,7 +889,7 @@ void abort_when_stuck(
}
new llvm::StoreInst(child_value, child_ptr, current_block);
}
ptr = new llvm::BitCastInst(block, block_ptr, "", current_block);
ptr = block;
}
auto *func = get_or_insert_function(
module, "finish_rewriting", llvm::Type::getVoidTy(ctx), block_ptr,
Expand Down Expand Up @@ -971,10 +966,8 @@ static void store_ptrs_for_gc(
{zero, llvm::ConstantInt::get(
llvm::Type::getInt64Ty(module->getContext()), i)},
"", collect);
auto *casted = new llvm::BitCastInst(
ptr, llvm::PointerType::getUnqual(module->getContext()), "", collect);
new llvm::StoreInst(roots[i], casted, collect);
root_ptrs.emplace_back(casted, ptr_types[i]);
new llvm::StoreInst(roots[i], ptr, collect);
root_ptrs.emplace_back(ptr, ptr_types[i]);
}
}

Expand Down Expand Up @@ -1113,8 +1106,7 @@ std::pair<std::vector<llvm::Value *>, llvm::BasicBlock *> step_function_header(
{arr,
llvm::ConstantInt::get(
llvm::Type::getInt8Ty(module->getContext()), nroots),
llvm::ConstantExpr::getBitCast(layout, ptr_ty),
llvm::ConstantInt::getFalse(module->getContext())},
layout, llvm::ConstantInt::getFalse(module->getContext())},
"", collect);
set_debug_loc(call);
std::vector<llvm::Value *> phis;
Expand Down
36 changes: 10 additions & 26 deletions lib/codegen/EmitConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,24 +326,19 @@ static llvm::Value *get_arg_value(
case sort_category::Bool:
case sort_category::MInt: {
auto *val_ty = getvalue_type(cat, mod);
auto *cast = new llvm::BitCastInst(arg, ptr_ty, "", case_block);
auto *load = new llvm::LoadInst(val_ty, cast, "", case_block);
auto *load = new llvm::LoadInst(val_ty, arg, "", case_block);
arg = load;
break;
}
case sort_category::Map:
case sort_category::RangeMap:
case sort_category::List:
case sort_category::Set:
arg = new llvm::BitCastInst(arg, ptr_ty, "", case_block);
break;
case sort_category::Int:
case sort_category::Float:
case sort_category::StringBuffer:
case sort_category::Symbol:
case sort_category::Variable:
arg = new llvm::BitCastInst(arg, getvalue_type(cat, mod), "", case_block);
break;
case sort_category::Variable: break;
case sort_category::MapIter:
case sort_category::SetIter:
case sort_category::Uncomputed: abort();
Expand Down Expand Up @@ -386,19 +381,15 @@ static std::pair<llvm::Value *, llvm::BasicBlock *> get_eval(
case sort_category::Map:
case sort_category::RangeMap:
case sort_category::List:
case sort_category::Set:
retval = new llvm::BitCastInst(
result, ptr_ty, "", creator.get_current_block());
break;
case sort_category::Set: retval = result; break;
case sort_category::Bool:
case sort_category::MInt: {
auto *malloc = create_malloc(
creator.get_current_block(),
llvm::ConstantExpr::getSizeOf(result->getType()),
get_or_insert_function(mod, "malloc", ptr_ty, ptr_ty));
new llvm::StoreInst(result, malloc, creator.get_current_block());
retval = new llvm::BitCastInst(
malloc, ptr_ty, "", creator.get_current_block());
retval = malloc;
break;
}
case sort_category::MapIter:
Expand Down Expand Up @@ -554,8 +545,7 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
case_block, llvm::ConstantExpr::getSizeOf(compare->getType()),
get_or_insert_function(module, "malloc", ptr_ty, ptr_ty));
new llvm::StoreInst(compare, malloc, case_block);
auto *result = new llvm::BitCastInst(malloc, ptr_ty, "", case_block);
phi->addIncoming(result, case_block);
phi->addIncoming(malloc, case_block);
llvm::BranchInst::Create(merge_block, case_block);
break;
}
Expand All @@ -568,8 +558,7 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
module, "init_float", llvm::Type::getVoidTy(ctx), ptr_ty, ptr_ty);
llvm::CallInst::Create(
init_float, {term, func->arg_begin() + 2}, "", case_block);
auto *cast = new llvm::BitCastInst(term, ptr_ty, "", case_block);
phi->addIncoming(cast, case_block);
phi->addIncoming(term, case_block);
llvm::BranchInst::Create(merge_block, case_block);
break;
}
Expand Down Expand Up @@ -611,9 +600,8 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
*case_block, llvm::CmpInst::ICMP_EQ, call, zero32);
auto *abort_block = llvm::BasicBlock::Create(ctx, "invalid_int", func);
add_abort(abort_block, module);
auto *cast = new llvm::BitCastInst(term, ptr_ty, "", case_block);
llvm::BranchInst::Create(merge_block, abort_block, icmp, case_block);
phi->addIncoming(cast, case_block);
phi->addIncoming(term, case_block);
break;
}
case sort_category::Variable:
Expand Down Expand Up @@ -661,9 +649,8 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
llvm::CallInst::Create(
memcpy, {str_ptr, func->arg_begin() + 2, func->arg_begin() + 1}, "",
current_block);
auto *cast = new llvm::BitCastInst(block, ptr_ty, "", current_block);
llvm::BranchInst::Create(merge_block, current_block);
phi->addIncoming(cast, current_block);
phi->addIncoming(block, current_block);
llvm::ReturnInst::Create(ctx, phi, merge_block);
merge_block->insertInto(func);
}
Expand Down Expand Up @@ -777,16 +764,14 @@ static void get_store(
llvm::Value *arguments_array = func->arg_begin() + 1;
int idx = 0;
auto *block_type = get_block_type(module, definition, symbol);
auto *cast = new llvm::BitCastInst(
func->arg_begin(), llvm::PointerType::getUnqual(ctx), "", case_block);
for (auto const &sort : symbol->get_arguments()) {
value_type cat = dynamic_cast<kore_composite_sort *>(sort.get())
->get_category(definition);
llvm::Value *arg
= get_arg_value(arguments_array, idx, case_block, cat, module);
llvm::Type *arg_ty = get_arg_type(cat, module);
llvm::Value *child_ptr = llvm::GetElementPtrInst::CreateInBounds(
block_type, cast,
block_type, func->arg_begin(),
{zero, llvm::ConstantInt::get(
llvm::Type::getInt32Ty(ctx),
get_block_offset(definition, symbol, idx++))},
Expand Down Expand Up @@ -896,7 +881,6 @@ static void get_visitor(
llvm::Function *func = case_block->getParent();
int idx = 0;
auto *block_type = get_block_type(module, definition, symbol);
auto *cast = new llvm::BitCastInst(func->arg_begin(), ptr_ty, "", case_block);
unsigned i = 0;

auto *state_ptr = func->arg_end() - 1;
Expand All @@ -905,7 +889,7 @@ static void get_visitor(
auto *composite_sort = dynamic_cast<kore_composite_sort *>(sort.get());
value_type cat = composite_sort->get_category(definition);
llvm::Value *child_ptr = llvm::GetElementPtrInst::CreateInBounds(
block_type, cast,
block_type, func->arg_begin(),
{zero, llvm::ConstantInt::get(
llvm::Type::getInt32Ty(ctx),
get_block_offset(definition, symbol, idx++))},
Expand Down

0 comments on commit 5e07327

Please sign in to comment.