Skip to content

Commit 5e07327

Browse files
authored
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.
1 parent c655b8b commit 5e07327

File tree

4 files changed

+21
-48
lines changed

4 files changed

+21
-48
lines changed

lib/codegen/CreateStaticTerm.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ llvm::Constant *create_static_term::not_injection_case(
8383

8484
std::vector<llvm::Constant *> idxs
8585
= {llvm::ConstantInt::get(llvm::Type::getInt64Ty(ctx_), 0)};
86-
return llvm::ConstantExpr::getBitCast(
87-
llvm::ConstantExpr::getInBoundsGetElementPtr(
88-
block_type, global_var, idxs),
89-
llvm::PointerType::getUnqual(module_->getContext()));
86+
return llvm::ConstantExpr::getInBoundsGetElementPtr(
87+
block_type, global_var, idxs);
9088
}
9189

9290
std::pair<llvm::Constant *, bool>

lib/codegen/CreateTerm.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -974,15 +974,14 @@ llvm::Value *create_term::not_injection_case(
974974
}
975975

976976
auto *block_ptr = llvm::PointerType::getUnqual(module_->getContext());
977-
auto *bitcast = new llvm::BitCastInst(block, block_ptr, "", current_block_);
978977
if (symbol_decl->attributes().contains(attribute_set::key::Binder)) {
979978
auto *call = llvm::CallInst::Create(
980979
get_or_insert_function(module_, "debruijnize", block_ptr, block_ptr),
981-
bitcast, "withIndices", current_block_);
980+
block, "withIndices", current_block_);
982981
set_debug_loc(call);
983982
return call;
984983
}
985-
return bitcast;
984+
return block;
986985
}
987986

988987
// returns a value and a boolean indicating whether that value could be an

lib/codegen/Decision.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ void decision::operator()(decision_node *entry) {
5252
if (fail_pattern_) {
5353
llvm::Value *val = load(std::make_pair(
5454
"_1", getvalue_type({sort_category::Symbol, 0}, module_)));
55-
fail_subject_->addIncoming(
56-
new llvm::BitCastInst(
57-
val, llvm::PointerType::getUnqual(ctx_), "", current_block_),
58-
current_block_);
55+
fail_subject_->addIncoming(val, current_block_);
5956
fail_pattern_->addIncoming(
6057
string_literal("\\bottom{SortGeneratedTopCell{}}()"), current_block_);
6158
fail_sort_->addIncoming(
@@ -71,8 +68,7 @@ llvm::Value *decision::ptr_term(llvm::Value *val) {
7168
if (val->getType()->isIntegerTy()) {
7269
val = allocate_term(val->getType(), current_block_, "kore_alloc_always_gc");
7370
}
74-
return new llvm::BitCastInst(
75-
val, llvm::PointerType::getUnqual(ctx_), "", current_block_);
71+
return val;
7672
}
7773

7874
bool decision_node::begin_node(decision *d, std::string const &name) {
@@ -236,14 +232,13 @@ void switch_node::codegen(decision *d) {
236232
int offset = 0;
237233
llvm::StructType *block_type = get_block_type(
238234
d->module_, d->definition_, switch_case.get_constructor());
239-
auto *cast = new llvm::BitCastInst(val, ptr_ty, "", d->current_block_);
240235
kore_symbol_declaration *symbol_decl
241236
= d->definition_->get_symbol_declarations().at(
242237
switch_case.get_constructor()->get_name());
243238
llvm::Instruction *renamed = nullptr;
244239
for (auto const &binding : switch_case.get_bindings()) {
245240
llvm::Value *child_ptr = llvm::GetElementPtrInst::CreateInBounds(
246-
block_type, cast,
241+
block_type, val,
247242
{llvm::ConstantInt::get(llvm::Type::getInt64Ty(d->ctx_), 0),
248243
llvm::ConstantInt::get(
249244
llvm::Type::getInt32Ty(d->ctx_),
@@ -894,7 +889,7 @@ void abort_when_stuck(
894889
}
895890
new llvm::StoreInst(child_value, child_ptr, current_block);
896891
}
897-
ptr = new llvm::BitCastInst(block, block_ptr, "", current_block);
892+
ptr = block;
898893
}
899894
auto *func = get_or_insert_function(
900895
module, "finish_rewriting", llvm::Type::getVoidTy(ctx), block_ptr,
@@ -971,10 +966,8 @@ static void store_ptrs_for_gc(
971966
{zero, llvm::ConstantInt::get(
972967
llvm::Type::getInt64Ty(module->getContext()), i)},
973968
"", collect);
974-
auto *casted = new llvm::BitCastInst(
975-
ptr, llvm::PointerType::getUnqual(module->getContext()), "", collect);
976-
new llvm::StoreInst(roots[i], casted, collect);
977-
root_ptrs.emplace_back(casted, ptr_types[i]);
969+
new llvm::StoreInst(roots[i], ptr, collect);
970+
root_ptrs.emplace_back(ptr, ptr_types[i]);
978971
}
979972
}
980973

@@ -1113,8 +1106,7 @@ std::pair<std::vector<llvm::Value *>, llvm::BasicBlock *> step_function_header(
11131106
{arr,
11141107
llvm::ConstantInt::get(
11151108
llvm::Type::getInt8Ty(module->getContext()), nroots),
1116-
llvm::ConstantExpr::getBitCast(layout, ptr_ty),
1117-
llvm::ConstantInt::getFalse(module->getContext())},
1109+
layout, llvm::ConstantInt::getFalse(module->getContext())},
11181110
"", collect);
11191111
set_debug_loc(call);
11201112
std::vector<llvm::Value *> phis;

lib/codegen/EmitConfigParser.cpp

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -326,24 +326,19 @@ static llvm::Value *get_arg_value(
326326
case sort_category::Bool:
327327
case sort_category::MInt: {
328328
auto *val_ty = getvalue_type(cat, mod);
329-
auto *cast = new llvm::BitCastInst(arg, ptr_ty, "", case_block);
330-
auto *load = new llvm::LoadInst(val_ty, cast, "", case_block);
329+
auto *load = new llvm::LoadInst(val_ty, arg, "", case_block);
331330
arg = load;
332331
break;
333332
}
334333
case sort_category::Map:
335334
case sort_category::RangeMap:
336335
case sort_category::List:
337336
case sort_category::Set:
338-
arg = new llvm::BitCastInst(arg, ptr_ty, "", case_block);
339-
break;
340337
case sort_category::Int:
341338
case sort_category::Float:
342339
case sort_category::StringBuffer:
343340
case sort_category::Symbol:
344-
case sort_category::Variable:
345-
arg = new llvm::BitCastInst(arg, getvalue_type(cat, mod), "", case_block);
346-
break;
341+
case sort_category::Variable: break;
347342
case sort_category::MapIter:
348343
case sort_category::SetIter:
349344
case sort_category::Uncomputed: abort();
@@ -386,19 +381,15 @@ static std::pair<llvm::Value *, llvm::BasicBlock *> get_eval(
386381
case sort_category::Map:
387382
case sort_category::RangeMap:
388383
case sort_category::List:
389-
case sort_category::Set:
390-
retval = new llvm::BitCastInst(
391-
result, ptr_ty, "", creator.get_current_block());
392-
break;
384+
case sort_category::Set: retval = result; break;
393385
case sort_category::Bool:
394386
case sort_category::MInt: {
395387
auto *malloc = create_malloc(
396388
creator.get_current_block(),
397389
llvm::ConstantExpr::getSizeOf(result->getType()),
398390
get_or_insert_function(mod, "malloc", ptr_ty, ptr_ty));
399391
new llvm::StoreInst(result, malloc, creator.get_current_block());
400-
retval = new llvm::BitCastInst(
401-
malloc, ptr_ty, "", creator.get_current_block());
392+
retval = malloc;
402393
break;
403394
}
404395
case sort_category::MapIter:
@@ -554,8 +545,7 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
554545
case_block, llvm::ConstantExpr::getSizeOf(compare->getType()),
555546
get_or_insert_function(module, "malloc", ptr_ty, ptr_ty));
556547
new llvm::StoreInst(compare, malloc, case_block);
557-
auto *result = new llvm::BitCastInst(malloc, ptr_ty, "", case_block);
558-
phi->addIncoming(result, case_block);
548+
phi->addIncoming(malloc, case_block);
559549
llvm::BranchInst::Create(merge_block, case_block);
560550
break;
561551
}
@@ -568,8 +558,7 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
568558
module, "init_float", llvm::Type::getVoidTy(ctx), ptr_ty, ptr_ty);
569559
llvm::CallInst::Create(
570560
init_float, {term, func->arg_begin() + 2}, "", case_block);
571-
auto *cast = new llvm::BitCastInst(term, ptr_ty, "", case_block);
572-
phi->addIncoming(cast, case_block);
561+
phi->addIncoming(term, case_block);
573562
llvm::BranchInst::Create(merge_block, case_block);
574563
break;
575564
}
@@ -611,9 +600,8 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
611600
*case_block, llvm::CmpInst::ICMP_EQ, call, zero32);
612601
auto *abort_block = llvm::BasicBlock::Create(ctx, "invalid_int", func);
613602
add_abort(abort_block, module);
614-
auto *cast = new llvm::BitCastInst(term, ptr_ty, "", case_block);
615603
llvm::BranchInst::Create(merge_block, abort_block, icmp, case_block);
616-
phi->addIncoming(cast, case_block);
604+
phi->addIncoming(term, case_block);
617605
break;
618606
}
619607
case sort_category::Variable:
@@ -661,9 +649,8 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
661649
llvm::CallInst::Create(
662650
memcpy, {str_ptr, func->arg_begin() + 2, func->arg_begin() + 1}, "",
663651
current_block);
664-
auto *cast = new llvm::BitCastInst(block, ptr_ty, "", current_block);
665652
llvm::BranchInst::Create(merge_block, current_block);
666-
phi->addIncoming(cast, current_block);
653+
phi->addIncoming(block, current_block);
667654
llvm::ReturnInst::Create(ctx, phi, merge_block);
668655
merge_block->insertInto(func);
669656
}
@@ -777,16 +764,14 @@ static void get_store(
777764
llvm::Value *arguments_array = func->arg_begin() + 1;
778765
int idx = 0;
779766
auto *block_type = get_block_type(module, definition, symbol);
780-
auto *cast = new llvm::BitCastInst(
781-
func->arg_begin(), llvm::PointerType::getUnqual(ctx), "", case_block);
782767
for (auto const &sort : symbol->get_arguments()) {
783768
value_type cat = dynamic_cast<kore_composite_sort *>(sort.get())
784769
->get_category(definition);
785770
llvm::Value *arg
786771
= get_arg_value(arguments_array, idx, case_block, cat, module);
787772
llvm::Type *arg_ty = get_arg_type(cat, module);
788773
llvm::Value *child_ptr = llvm::GetElementPtrInst::CreateInBounds(
789-
block_type, cast,
774+
block_type, func->arg_begin(),
790775
{zero, llvm::ConstantInt::get(
791776
llvm::Type::getInt32Ty(ctx),
792777
get_block_offset(definition, symbol, idx++))},
@@ -896,7 +881,6 @@ static void get_visitor(
896881
llvm::Function *func = case_block->getParent();
897882
int idx = 0;
898883
auto *block_type = get_block_type(module, definition, symbol);
899-
auto *cast = new llvm::BitCastInst(func->arg_begin(), ptr_ty, "", case_block);
900884
unsigned i = 0;
901885

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

0 commit comments

Comments
 (0)