On Mon 2026-10-26 15:55:37, Filip Kastl wrote:
> I'm currently working on a patch where ginsert_iterator would be helpful (the
> sprintf patch we discussed offlist).  I'll post a preview of the relevant part
> of the patch written using the ginsert_iterator (I still have to finish that
> part of the patch first).

Here is the relevant portion of my patch.  The first attachment patch.cc.orig
contains my original code using gimple_build(&gsi, ...) API where possible, the
second attachment patch.cc is rewritten (by Claude) to use the ginsert_iterator
API.

Doing `wc -b` shows I save ~5% characters.  That's mostly because I don't have
to specify `false, GSI_NEW_STMT, ` all the time.  Just by looking at the two
versions, I think the readability improvement isn't a giant one.  Inserting
large ammount of GIMPLE statements is a mess either way.  But using
ginsert_iterator certainly makes this code a bit more readable.

Filip
  gimple_stmt_iterator gsi;
  gphi *phi_i, *phi_str;

  // bb2
  gsi = gsi_start_bb (bb2);
  tree i_1 = get_or_create_ssa_default_def (cfun, i_param_decl);
  gcond *bb2_cond =
    gimple_build_cond (LT_EXPR, i_1, build_zero_cst (integer_type_node),
        NULL, NULL);
  gsi_insert_after (&gsi, bb2_cond, GSI_NEW_STMT);

  // bb3
  gsi = gsi_start_bb (bb3);
  tree str_1 = get_or_create_ssa_default_def (cfun, str_param_decl);
  tree str_2 =
    gimple_build (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
        POINTER_PLUS_EXPR, str_type, str_1, build_one_cst (size_type_node));
  tree str_1_deref = build_simple_mem_ref (str_1);
  gassign *minus_write =
    gimple_build_assign (str_1_deref, build_int_cst (char_type_node, 45));
  gsi_insert_after (&gsi, minus_write, GSI_NEW_STMT);

  // bb4
  tree str_3 = make_ssa_name (str_param_decl);
  phi_str = create_phi_node (str_3, bb4);
  add_phi_arg (phi_str, str_1, e24, UNKNOWN_LOCATION);
  add_phi_arg (phi_str, str_2, single_succ_edge (bb3), UNKNOWN_LOCATION);

  gsi = gsi_start_bb (bb4);
  tree i_3 = gimple_build (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      ABSU_EXPR, unsigned_type_node, i_1);

  // bb5
  tree str_4 = make_ssa_name (str_param_decl);
  tree i_4 = make_ssa_name (unsigned_type_node);
  phi_str = create_phi_node (str_4, bb5);
  phi_i = create_phi_node (i_4, bb5);

  gsi = gsi_start_bb (bb5);
  tree i_5 = gimple_build (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      TRUNC_DIV_EXPR, unsigned_type_node, i_4, build_int_cst 
(unsigned_type_node,
        10));
  tree str_5 = gimple_build (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      POINTER_PLUS_EXPR, str_type, str_4, build_one_cst (size_type_node));
  gcond *bb5_cond =
    gimple_build_cond (GT_EXPR, i_4, build_int_cst (unsigned_type_node, 9),
        NULL, NULL);
  gsi_insert_after (&gsi, bb5_cond, GSI_NEW_STMT);

  add_phi_arg (phi_str, str_3, single_succ_edge (bb4), UNKNOWN_LOCATION);
  add_phi_arg (phi_str, str_5, e55, UNKNOWN_LOCATION);
  add_phi_arg (phi_i, i_3, single_succ_edge (bb4), UNKNOWN_LOCATION);
  add_phi_arg (phi_i, i_5, e55, UNKNOWN_LOCATION);

  // bb6
  gsi = gsi_start_bb (bb6);
  tree written_1 = gimple_build (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      POINTER_DIFF_EXPR, long_integer_type_node, str_5, str_1);
  tree written_2 = gimple_convert (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      integer_type_node, written_1);
  tree written_3 = gimple_build (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      PLUS_EXPR, integer_type_node, written_2,
      build_minus_one_cst (integer_type_node));

  // bb7
  tree str_6 = make_ssa_name (str_param_decl);
  tree i_6 = make_ssa_name (unsigned_type_node);
  phi_str = create_phi_node (str_6, bb7);
  phi_i = create_phi_node (i_6, bb7);

  gsi = gsi_start_bb (bb7);
  tree i_7 = gimple_build (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      TRUNC_DIV_EXPR, unsigned_type_node, i_6, build_int_cst 
(unsigned_type_node,
        10));
  tree str_7 = gimple_build (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      POINTER_PLUS_EXPR, str_type, str_6, build_int_cst(size_type_node, -1));
  tree i_8 = gimple_build (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      TRUNC_MOD_EXPR, unsigned_type_node, i_6,
      build_int_cst (unsigned_type_node, 10));
  tree i_9 = gimple_convert (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      unsigned_char_type_node, i_8);
  tree i_10 = gimple_build (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      PLUS_EXPR, unsigned_char_type_node, i_9,
      build_int_cst (unsigned_char_type_node, 48));
  tree i_11 = gimple_convert (&gsi, false, GSI_NEW_STMT, UNKNOWN_LOCATION,
      char_type_node, i_10);
  tree str_7_deref = build_simple_mem_ref (str_7);
  gassign *char_write =
    gimple_build_assign (str_7_deref, i_11);
  gsi_insert_after (&gsi, char_write, GSI_NEW_STMT);
  gcond *bb7_cond =
    gimple_build_cond (GT_EXPR, i_6, build_int_cst (unsigned_type_node, 9),
        NULL, NULL);
  gsi_insert_after (&gsi, bb7_cond, GSI_NEW_STMT);

  add_phi_arg (phi_str, str_5, single_succ_edge (bb6), UNKNOWN_LOCATION);
  add_phi_arg (phi_str, str_7, e77, UNKNOWN_LOCATION);
  add_phi_arg (phi_i, i_3, single_succ_edge (bb6), UNKNOWN_LOCATION);
  add_phi_arg (phi_i, i_7, e77, UNKNOWN_LOCATION);

  // bb8
  gsi = gsi_start_bb (bb8);
  greturn *return_stmt = gimple_build_return (written_3);
  gsi_insert_after (&gsi, return_stmt, GSI_NEW_STMT);
  ginsert_iterator gsi;
  gphi *phi_i, *phi_str;

  // bb2
  gsi = ginsert_iterator::after (gsi_start_bb (bb2));
  tree i_1 = get_or_create_ssa_default_def (cfun, i_param_decl);
  gcond *bb2_cond =
    gimple_build_cond (LT_EXPR, i_1, build_zero_cst (integer_type_node),
	NULL, NULL);
  gsi.insert (bb2_cond);

  // bb3
  gsi = ginsert_iterator::after (gsi_start_bb (bb3));
  tree str_1 = get_or_create_ssa_default_def (cfun, str_param_decl);
  tree str_2 =
    gimple_build (&gsi, UNKNOWN_LOCATION,
	POINTER_PLUS_EXPR, str_type, str_1, build_one_cst (size_type_node));
  tree str_1_deref = build_simple_mem_ref (str_1);
  gassign *minus_write =
    gimple_build_assign (str_1_deref, build_int_cst (char_type_node, 45));
  gsi.insert (minus_write);

  // bb4
  tree str_3 = make_ssa_name (str_param_decl);
  phi_str = create_phi_node (str_3, bb4);
  add_phi_arg (phi_str, str_1, e24, UNKNOWN_LOCATION);
  add_phi_arg (phi_str, str_2, single_succ_edge (bb3), UNKNOWN_LOCATION);

  gsi = ginsert_iterator::after (gsi_start_bb (bb4));
  tree i_3 = gimple_build (&gsi, UNKNOWN_LOCATION,
      ABSU_EXPR, unsigned_type_node, i_1);

  // bb5
  tree str_4 = make_ssa_name (str_param_decl);
  tree i_4 = make_ssa_name (unsigned_type_node);
  phi_str = create_phi_node (str_4, bb5);
  phi_i = create_phi_node (i_4, bb5);

  gsi = ginsert_iterator::after (gsi_start_bb (bb5));
  tree i_5 = gimple_build (&gsi, UNKNOWN_LOCATION,
      TRUNC_DIV_EXPR, unsigned_type_node, i_4, build_int_cst (unsigned_type_node,
	10));
  tree str_5 = gimple_build (&gsi, UNKNOWN_LOCATION,
      POINTER_PLUS_EXPR, str_type, str_4, build_one_cst (size_type_node));
  gcond *bb5_cond =
    gimple_build_cond (GT_EXPR, i_4, build_int_cst (unsigned_type_node, 9),
	NULL, NULL);
  gsi.insert (bb5_cond);

  add_phi_arg (phi_str, str_3, single_succ_edge (bb4), UNKNOWN_LOCATION);
  add_phi_arg (phi_str, str_5, e55, UNKNOWN_LOCATION);
  add_phi_arg (phi_i, i_3, single_succ_edge (bb4), UNKNOWN_LOCATION);
  add_phi_arg (phi_i, i_5, e55, UNKNOWN_LOCATION);

  // bb6
  gsi = ginsert_iterator::after (gsi_start_bb (bb6));
  tree written_1 = gimple_build (&gsi, UNKNOWN_LOCATION,
      POINTER_DIFF_EXPR, long_integer_type_node, str_5, str_1);
  tree written_2 = gimple_convert (&gsi, UNKNOWN_LOCATION,
      integer_type_node, written_1);
  tree written_3 = gimple_build (&gsi, UNKNOWN_LOCATION,
      PLUS_EXPR, integer_type_node, written_2,
      build_minus_one_cst (integer_type_node));

  // bb7
  tree str_6 = make_ssa_name (str_param_decl);
  tree i_6 = make_ssa_name (unsigned_type_node);
  phi_str = create_phi_node (str_6, bb7);
  phi_i = create_phi_node (i_6, bb7);

  gsi = ginsert_iterator::after (gsi_start_bb (bb7));
  tree i_7 = gimple_build (&gsi, UNKNOWN_LOCATION,
      TRUNC_DIV_EXPR, unsigned_type_node, i_6, build_int_cst (unsigned_type_node,
	10));
  tree str_7 = gimple_build (&gsi, UNKNOWN_LOCATION,
      POINTER_PLUS_EXPR, str_type, str_6, build_int_cst(size_type_node, -1));
  tree i_8 = gimple_build (&gsi, UNKNOWN_LOCATION,
      TRUNC_MOD_EXPR, unsigned_type_node, i_6,
      build_int_cst (unsigned_type_node, 10));
  tree i_9 = gimple_convert (&gsi, UNKNOWN_LOCATION,
      unsigned_char_type_node, i_8);
  tree i_10 = gimple_build (&gsi, UNKNOWN_LOCATION,
      PLUS_EXPR, unsigned_char_type_node, i_9,
      build_int_cst (unsigned_char_type_node, 48));
  tree i_11 = gimple_convert (&gsi, UNKNOWN_LOCATION,
      char_type_node, i_10);
  tree str_7_deref = build_simple_mem_ref (str_7);
  gassign *char_write =
    gimple_build_assign (str_7_deref, i_11);
  gsi.insert (char_write);
  gcond *bb7_cond =
    gimple_build_cond (GT_EXPR, i_6, build_int_cst (unsigned_type_node, 9),
	NULL, NULL);
  gsi.insert (bb7_cond);

  add_phi_arg (phi_str, str_5, single_succ_edge (bb6), UNKNOWN_LOCATION);
  add_phi_arg (phi_str, str_7, e77, UNKNOWN_LOCATION);
  add_phi_arg (phi_i, i_3, single_succ_edge (bb6), UNKNOWN_LOCATION);
  add_phi_arg (phi_i, i_7, e77, UNKNOWN_LOCATION);

  // bb8
  gsi = ginsert_iterator::after (gsi_start_bb (bb8));
  greturn *return_stmt = gimple_build_return (written_3);
  gsi.insert (return_stmt);

Reply via email to