public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/pheeck/heads/insert-api)] insert api: implementing calls, stuck on mem corruption bug
@ 2023-07-06  8:46 Filip Kastl
  0 siblings, 0 replies; only message in thread
From: Filip Kastl @ 2023-07-06  8:46 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:269f84fde764b83cc0d4c20c134a64cb0840f94c

commit 269f84fde764b83cc0d4c20c134a64cb0840f94c
Author: Filip Kastl <filip.kastl@gmail.com>
Date:   Wed Jul 5 15:34:18 2023 +0200

    insert api: implementing calls, stuck on mem corruption bug

Diff:
---
 gcc/insert-gimple-ssa.cc | 107 ++++++++++++++++++++++++++++++++++++++++++++++-
 gcc/insert-gimple-ssa.h  |  42 +++++++++++++++++--
 gcc/tree-into-ssa.cc     |  37 ++++++++++++----
 3 files changed, 174 insertions(+), 12 deletions(-)

diff --git a/gcc/insert-gimple-ssa.cc b/gcc/insert-gimple-ssa.cc
index 0994560934d..58210e8c2ab 100644
--- a/gcc/insert-gimple-ssa.cc
+++ b/gcc/insert-gimple-ssa.cc
@@ -93,6 +93,19 @@ hstmt_cond::replace_op_by (hstmt_with_lhs *op, hstmt_with_lhs *replace_by)
     rhs = replace_by;
 }
 
+/* Replace hack call operand.  */
+
+void
+hstmt_call::replace_op_by (hstmt_with_lhs *op, hstmt_with_lhs *replace_by)
+{
+  unsigned i;
+  for (i = 0; i < val->num_ops; i++)
+    {
+      if (val->op[i] == op)
+	val->op[i] = replace_by;
+    }
+}
+
 gimple *
 hstmt_assign::to_gimple (void)
 {
@@ -150,6 +163,24 @@ hstmt_return::to_gimple (void)
     return gimple_build_return (retval->ssa);
 }
 
+gimple *
+hstmt_call::to_gimple (void)
+{
+  vec<tree> ssa_args = vNULL; /* TODO We know how much to allocate.  */
+  unsigned i;
+  for (i = 0; i < val->num_ops; i++)
+    {
+      ssa_args.safe_push (val->op[i]->ssa);
+    }
+
+  gimple *call = gimple_build_call_vec (val->fn, ssa_args);
+  if (var != NULL)
+    gimple_call_set_lhs (call, ssa);
+
+  ssa_args.release ();
+  return call;
+}
+
 /* Create a new LOCAL hack variable.
 
    Accepts VAR_DECL, PARM_DECL and type trees.
@@ -268,6 +299,10 @@ hack_ssa_builder::append_outvar (basic_block bb, hvar *local)
   append_stmt (bb, stmt);
 
   allocated_hvars.safe_push (outvar);
+
+  /* Update uses list of appropriate stmts.  */
+  stmt->rhs->uses.safe_push (stmt);
+  // TODO Neměl bych kontrolovat, jestli není killed?
   
   return outvar;
 }
@@ -290,6 +325,46 @@ hack_ssa_builder::append_return (basic_block bb, hvar *retval)
   hstmt_return *stmt = new hstmt_return (read_variable (bb, retval));
   append_stmt (bb, stmt);
   set_block_filled (bb);
+
+  /* Update uses list of appropriate stmts.  */
+  stmt->retval->uses.safe_push (stmt);
+  // TODO Neměl bych kontrolovat, jestli není killed?
+}
+
+/* Build and append hack call to a basic block.  */
+
+void hack_ssa_builder::append_call_vec (basic_block bb, tree fn, hvar *left,
+					const vec<hvar *> &args)
+{
+  unsigned num_ops = args.length ();
+  hack_tuple_fn *val = tuple_alloc_fn (fn, num_ops);
+
+  unsigned i;
+  for (i = 0; i < num_ops; i++)
+    {
+      hvar *arg = args[i];
+      gcc_checking_assert (arg->code != OUTVAR);
+      tuple_set_operand_fn (i, val, read_variable (bb, arg));
+    }
+
+  hstmt_call *call = new hstmt_call (left, val);
+  append_stmt (bb, call);
+
+  /* Update uses list of appropriate stmts.  */
+  for (i = 0; i < val->num_ops; i++)
+    {
+      // TODO Neměl bych kontrolovat, jestli není killed?
+      val->op[i]->uses.safe_push (call);
+    }
+
+  write_variable (bb, left, call);
+}
+
+void
+hack_ssa_builder::append_call_vec (basic_block bb, tree fn,
+				   const vec<hvar *> &args)
+{
+  append_call_vec (bb, fn, NULL, args);
 }
 
 /* See the Braun alg paper for what 'sealed' means.  */
@@ -403,6 +478,8 @@ hack_ssa_builder::release (void)
     XDELETE (v);
   for (hack_tuple_internal *t : allocated_internal)
     XDELETE (t);
+  for (hack_tuple_fn *t : allocated_tuples_fn)
+    XDELETE (t);
 }
 
 /* Extracts SSA name from outvar. Outvars represent places where user wants SSA
@@ -495,6 +572,23 @@ hack_ssa_builder::tuple_alloc (enum tree_code code, unsigned num_ops)
   return result;
 }
 
+hack_tuple_fn *
+hack_ssa_builder::tuple_alloc_fn (tree fn, unsigned num_ops)
+{
+  gcc_checking_assert (num_ops >= 1 && "Tuples of size <1 not allowed");
+
+  size_t size = sizeof (hack_tuple_fn) +
+    (num_ops - 1) * sizeof (struct hstmt_with_lhs *);
+  hack_tuple_fn *result = XNEWVAR (struct hack_tuple_fn, size);
+
+  result->num_ops = num_ops;
+  result->fn = fn;
+
+  allocated_tuples_fn.safe_push (result);
+
+  return result;
+}
+
 void
 hack_ssa_builder::tuple_set_operand (unsigned op_num,
 				     hack_tuple_internal *tuple,
@@ -503,6 +597,14 @@ hack_ssa_builder::tuple_set_operand (unsigned op_num,
   tuple->op[op_num] = op;
 }
 
+void
+hack_ssa_builder::tuple_set_operand_fn (unsigned op_num,
+					hack_tuple_fn *tuple,
+					hstmt_with_lhs *op)
+{
+  tuple->op[op_num] = op;
+}
+
 void
 hack_ssa_builder::append_stmt (basic_block bb, hstmt *stmt)
 {
@@ -528,6 +630,9 @@ hack_ssa_builder::add_empty_phi (basic_block bb, hvar *var)
 void
 hack_ssa_builder::commit_ssa_name (hstmt_with_lhs *s)
 {
+  if (s->var == NULL) /* This statement doesn't have lhs.  */
+    return;
+
   switch (s->var->code)
     {
       case LOCAL:
@@ -725,7 +830,7 @@ hack_ssa_builder::read_variable_recursive (basic_block bb, hvar *var)
     }
   else
     {
-      /* Reached a bb without predecesors.  */
+      /* Reached a bb without predecessors.  */
       if (var->code == PARAM)
 	{
 	  return var->default_def;
diff --git a/gcc/insert-gimple-ssa.h b/gcc/insert-gimple-ssa.h
index 8a1070a0817..64d12b2ba88 100644
--- a/gcc/insert-gimple-ssa.h
+++ b/gcc/insert-gimple-ssa.h
@@ -31,7 +31,9 @@ class hstmt_with_lhs;
 class hphi;
 class hstmt_assign;
 class hstmt_const;
+class hstmt_call;
 struct hack_tuple_internal;
+struct hack_tuple_fn;
 class hack_bb;
 struct hvar;
 class hack_ssa_builder;
@@ -85,7 +87,8 @@ enum hstmt_code
   HSTMT_CONST,
   HSTMT_COND,
   HSTMT_OUTVAR,
-  HSTMT_RETURN
+  HSTMT_RETURN,
+  HSTMT_CALL
 };
 
 /* Hack statement
@@ -192,7 +195,8 @@ class hstmt_assign : public hstmt_with_lhs
       }
 
     virtual gimple *to_gimple (void) override;
-    virtual void replace_op_by (hstmt_with_lhs *op, hstmt_with_lhs *replace_by) override;
+    virtual void replace_op_by (hstmt_with_lhs *op, hstmt_with_lhs *replace_by)
+      override;
 };
 
 /* Hack const stmt (will rename this to hack invar stmt)
@@ -254,7 +258,7 @@ class hstmt_return : public hstmt
   public:
     hstmt_with_lhs *retval;
 
-    hstmt_return () :
+    hstmt_return (void) :
       hstmt (HSTMT_RETURN), retval (NULL) { }
 
     hstmt_return (hstmt_with_lhs *retval) :
@@ -263,6 +267,21 @@ class hstmt_return : public hstmt
     virtual gimple *to_gimple (void) override;
 };
 
+/* TODO Description.  */
+
+class hstmt_call : public hstmt_with_lhs
+{
+  public:
+    hack_tuple_fn *val;
+
+    hstmt_call (hvar *var, hack_tuple_fn *val)
+      : hstmt_with_lhs (HSTMT_CALL, var), val (val) { }
+
+    virtual gimple *to_gimple (void) override;
+    virtual void replace_op_by (hstmt_with_lhs *op, hstmt_with_lhs *replace_by)
+      override;
+};
+
 /* Hack internal tuple
 
    Right side of assign statements. I may merge this into 'hstmt_assign'
@@ -275,6 +294,15 @@ struct hack_tuple_internal
   hstmt_with_lhs *op[1];  /* Trailing array idiom.  */
 };
 
+/* TODO Describe  */
+
+struct hack_tuple_fn
+{
+  tree fn;
+  unsigned num_ops;
+  hstmt_with_lhs *op[1];  /* Trailing array idiom.  */
+};
+
 template<>
 struct default_hash_traits<hack_tuple_internal>
   : typed_noop_remove <hack_tuple_internal>
@@ -363,6 +391,9 @@ class hack_ssa_builder
 		    hvar *left, hvar *right);
   void append_return (basic_block bb);
   void append_return (basic_block bb, hvar *retval);
+  void append_call_vec (basic_block bb, tree fn, hvar *left,
+		 	const vec<hvar *> &args);
+  void append_call_vec (basic_block bb, tree fn, const vec<hvar *> &args);
   hvar *append_outvar (basic_block bb, hvar *local);
 
   void set_block_sealed (basic_block bb);
@@ -383,6 +414,7 @@ class hack_ssa_builder
      Will become obsolete when allocation on obstack is implemented.  */
   vec<hvar *> allocated_hvars = vNULL;
   vec<hack_tuple_internal *> allocated_internal = vNULL;
+  vec<hack_tuple_fn *> allocated_tuples_fn = vNULL;
 
   hash_set<basic_block> seen_bbs;
   hash_map<basic_block, hack_bb *> bb_record_map;
@@ -395,8 +427,11 @@ class hack_ssa_builder
 
   hack_bb *get_bb_record (basic_block bb);
   hack_tuple_internal *tuple_alloc (enum tree_code code, unsigned num_ops);
+  hack_tuple_fn *tuple_alloc_fn (tree fn, unsigned num_ops);
   void tuple_set_operand (unsigned op_num, hack_tuple_internal *tuple,
 			  hstmt_with_lhs *op);
+  void tuple_set_operand_fn (unsigned op_num, hack_tuple_fn *tuple,
+			     hstmt_with_lhs *op);
   void append_stmt (basic_block bb, hstmt *stmt);
   hphi *add_empty_phi (basic_block bb, hvar *var);
 
@@ -431,6 +466,7 @@ struct is_a_helper<hstmt_with_lhs *> : static_is_a_helper<hstmt_with_lhs *>
 	case HSTMT_ASSIGN:
 	case HPHI:
 	case HSTMT_CONST:
+	case HSTMT_CALL:
 	  return true;
 	default:
 	  return false;
diff --git a/gcc/tree-into-ssa.cc b/gcc/tree-into-ssa.cc
index b5c3cf9749f..2bc56a8df7c 100644
--- a/gcc/tree-into-ssa.cc
+++ b/gcc/tree-into-ssa.cc
@@ -2502,7 +2502,6 @@ new_intossa ()
       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
 	{
 	  gimple *stmt = gsi_stmt (gsi);
-	  tree retval;
 	  switch (gimple_code (stmt))
 	    {
 	    case GIMPLE_ASSIGN:
@@ -2539,13 +2538,35 @@ new_intossa ()
 	      gsi_remove (&gsi, true);
 	      break;
 	    case GIMPLE_RETURN:
-	      update_stmt (stmt);
-	      retval = gimple_return_retval (as_a <greturn *>(stmt));
-	      if (retval == NULL_TREE)
-		builder.append_return (bb);
-	      else
-		builder.append_return (bb, get_op (builder, retval));
-	      gsi_remove (&gsi, true);
+	      {
+		update_stmt (stmt);
+		tree retval = gimple_return_retval (as_a <greturn *>(stmt));
+		if (retval == NULL_TREE)
+		  builder.append_return (bb);
+		else
+		  builder.append_return (bb, get_op (builder, retval));
+		gsi_remove (&gsi, true);
+	      }
+	      break;
+	    case GIMPLE_CALL:
+	      {
+		unsigned num_args = gimple_call_num_args (stmt);
+		vec<hvar *> args = vNULL; /* TODO We know how much to allocate.  */
+		hvar *lhs = get_op (builder, gimple_call_lhs (stmt));
+		tree fn = gimple_call_fn (stmt);
+
+		unsigned i;
+		for (i = 0; i < num_args; i++)
+		  {
+		    tree arg = gimple_call_arg (stmt, i);
+		    args.safe_push (get_op (builder, arg));
+		  }
+
+		builder.append_call_vec (bb, fn, lhs, args);
+		gsi_remove (&gsi, true);
+
+		args.release ();
+	      }
 	      break;
 	    default:
 	      debug_gimple_stmt (stmt);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-07-06  8:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-06  8:46 [gcc(refs/users/pheeck/heads/insert-api)] insert api: implementing calls, stuck on mem corruption bug Filip Kastl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).