@@ -944,6 +944,24 @@ static bool is_branch(const MachineInstr &minst,
944944 }) != minst.operands_end ();
945945}
946946
947+ static Value *cast_value_to (IRBuilder<> &builder, Value *val, Type *ty) {
948+ auto from_id = val->getType ()->getTypeID ();
949+ auto to_id = ty->getTypeID ();
950+ if (from_id != Type::TypeID::IntegerTyID)
951+ throw std::invalid_argument (
952+ " currently casting is only supported for integral types" );
953+ if (to_id != Type::TypeID::IntegerTyID)
954+ throw std::invalid_argument (
955+ " currently casting is only supported for integral types" );
956+ auto *from_int = dyn_cast<IntegerType>(val->getType ());
957+ auto *to_int = dyn_cast<IntegerType>(ty);
958+ auto from_width = from_int->getBitWidth ();
959+ auto to_width = to_int->getBitWidth ();
960+ if (from_width == to_width)
961+ return val;
962+ return builder.CreateSExtOrTrunc (val, ty);
963+ }
964+
947965auto generate_instruction (const MachineInstr &minst, BasicBlock &bb,
948966 IRBuilder<> &builder, reg2vals &rmap,
949967 const instr_impl &instrs,
@@ -999,15 +1017,26 @@ auto generate_instruction(const MachineInstr &minst, BasicBlock &bb,
9991017 return generate_stack_pointer_modification (
10001018 minst, builder, bb, target_machine, rmap, instrs, reg_stats);
10011019 }
1002- auto op_to_val = [&](auto &mop) {
1003- return operand_to_value (mop, bb, target_machine, rmap, reg_stats, instrs);
1004- };
1005- auto values = minst.uses () | views::filter ([](auto &mi) {
1006- return !mi.isReg () || (!mi.isDef () && !mi.isImplicit ());
1007- }) |
1008- views::transform (op_to_val);
1009- std::vector<Value *> args (values.begin (), values.end ());
1010- auto *call = builder.CreateCall (func->getFunctionType (), func, args);
1020+ auto *func_type = func->getFunctionType ();
1021+ assert (func_type);
1022+ std::vector<Type *> param_types (func_type->param_begin (),
1023+ func_type->param_end ());
1024+ auto args = minst.uses () | views::filter ([](auto &mi) {
1025+ return !mi.isReg () || (!mi.isDef () && !mi.isImplicit ());
1026+ }) |
1027+ views::transform ([&](auto &mop) -> Value * {
1028+ return operand_to_value (mop, bb, target_machine, rmap,
1029+ reg_stats, instrs);
1030+ }) |
1031+ ranges::to<std::vector>();
1032+ auto casted_args = views::zip (args, param_types) |
1033+ views::transform ([&](auto &&arg) -> Value * {
1034+ auto &&[val, ty] = arg;
1035+ return cast_value_to (builder, val, ty);
1036+ }) |
1037+ ranges::to<std::vector>();
1038+
1039+ auto *call = builder.CreateCall (func->getFunctionType (), func, casted_args);
10111040 for (auto &def : minst.defs ()) {
10121041 assert (def.isDef ());
10131042 if (!def.isReg ())
0 commit comments