4

I have to do some GIMPLE_CALL Statement manipulations. This GIMPLE_CALL will have two arguments, e.g: foo(a,b). My goal is change this method to a different method having THREE arguments e.g. zoo(a,b,c)

In my current approach, GCC crashes during while compiling a sample source program.

My code works when all I do is replace the method name (i.e. not changing the argument numbers).

Also, I was not able to find any methods dedicated to adding/removing argument numbers for a GIMPLE_CALL. Which leads me to believe that it might not be the right approach.

Code:

   //Getting the current number of  Call Arguments from target GIMPLE               
   //statememt
   unsigned num_of_ops = gimple_call_num_args(stmt);

   //Replace the method name to a new Method   
   gimple_call_set_fndecl(stmt, new_method);


   //We need to increment total number of call arguments by 1
   //Total numer of arguments are, Number of CALL Arguments + 3
   //You can confirm this in definitions of gimple_call_num_args() and 
   //gimple_call_set_arg()
   gimple_set_num_ops(stmt,num_of_ops+3+1);


   //Add the new argument
   gimple_call_set_arg(stmt, num_of_ops, third_argument);
   update_stmt (stmt);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ravi Shekhar
  • 78
  • 10

1 Answers1

2

It seems, you can only adjust num_ops with this approach to a lesser value.

gimple_set_num_ops is a simple setter, it does not allocate storage:

static inline void
gimple_set_num_ops (gimple *gs, unsigned num_ops)
{
  gs->num_ops = num_ops;
}

You will have to create another GIMPLE statement.

I think, this usage in the GCC codebase solves exactly the same problem that you have (from gcc/gimple.c):

/* Set the RHS of assignment statement pointed-to by GSI to CODE with
   operands OP1, OP2 and OP3.

   NOTE: The statement pointed-to by GSI may be reallocated if it
   did not have enough operand slots.  */

void
gimple_assign_set_rhs_with_ops (gimple_stmt_iterator *gsi, enum tree_code code,
                tree op1, tree op2, tree op3)
{
  unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
  gimple *stmt = gsi_stmt (*gsi);
  gimple *old_stmt = stmt;

  /* If the new CODE needs more operands, allocate a new statement.  */
  if (gimple_num_ops (stmt) < new_rhs_ops + 1)
    {
      tree lhs = gimple_assign_lhs (old_stmt);
      stmt = gimple_alloc (gimple_code (old_stmt), new_rhs_ops + 1);
      memcpy (stmt, old_stmt, gimple_size (gimple_code (old_stmt)));
      gimple_init_singleton (stmt);

      /* The LHS needs to be reset as this also changes the SSA name
     on the LHS.  */
      gimple_assign_set_lhs (stmt, lhs);
    }

  gimple_set_num_ops (stmt, new_rhs_ops + 1);
  gimple_set_subcode (stmt, code);
  gimple_assign_set_rhs1 (stmt, op1);
  if (new_rhs_ops > 1)
    gimple_assign_set_rhs2 (stmt, op2);
  if (new_rhs_ops > 2)
    gimple_assign_set_rhs3 (stmt, op3);
  if (stmt != old_stmt)
    gsi_replace (gsi, stmt, false);
}
Vladislav Ivanishin
  • 2,092
  • 16
  • 22