From 5e07327b35acc2ff8b6bfaae26593bc9bce882a8 Mon Sep 17 00:00:00 2001 From: Dwight Guth Date: Mon, 25 Nov 2024 09:57:44 -0600 Subject: [PATCH] try to delete some probably unneeded bitcasts (#1163) 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. --- lib/codegen/CreateStaticTerm.cpp | 6 ++---- lib/codegen/CreateTerm.cpp | 5 ++--- lib/codegen/Decision.cpp | 22 +++++++------------ lib/codegen/EmitConfigParser.cpp | 36 +++++++++----------------------- 4 files changed, 21 insertions(+), 48 deletions(-) diff --git a/lib/codegen/CreateStaticTerm.cpp b/lib/codegen/CreateStaticTerm.cpp index 4a9a7de64..7c69fa411 100644 --- a/lib/codegen/CreateStaticTerm.cpp +++ b/lib/codegen/CreateStaticTerm.cpp @@ -83,10 +83,8 @@ llvm::Constant *create_static_term::not_injection_case( std::vector 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 diff --git a/lib/codegen/CreateTerm.cpp b/lib/codegen/CreateTerm.cpp index 13f95b686..201c615f6 100644 --- a/lib/codegen/CreateTerm.cpp +++ b/lib/codegen/CreateTerm.cpp @@ -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 diff --git a/lib/codegen/Decision.cpp b/lib/codegen/Decision.cpp index 14f8c7964..da4c019b8 100644 --- a/lib/codegen/Decision.cpp +++ b/lib/codegen/Decision.cpp @@ -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( @@ -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) { @@ -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_), @@ -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, @@ -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]); } } @@ -1113,8 +1106,7 @@ std::pair, 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 phis; diff --git a/lib/codegen/EmitConfigParser.cpp b/lib/codegen/EmitConfigParser.cpp index da11ac234..8ee777d53 100644 --- a/lib/codegen/EmitConfigParser.cpp +++ b/lib/codegen/EmitConfigParser.cpp @@ -326,8 +326,7 @@ 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; } @@ -335,15 +334,11 @@ static llvm::Value *get_arg_value( 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(); @@ -386,10 +381,7 @@ static std::pair 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( @@ -397,8 +389,7 @@ static std::pair get_eval( 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: @@ -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; } @@ -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; } @@ -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: @@ -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); } @@ -777,8 +764,6 @@ 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(sort.get()) ->get_category(definition); @@ -786,7 +771,7 @@ static void get_store( = 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++))}, @@ -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; @@ -905,7 +889,7 @@ static void get_visitor( auto *composite_sort = dynamic_cast(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++))},