compiler: Place phi nodes in stores during SSA.
authorTyler J. Stachecki <stachecki.tyler@gmail.com>
Sat, 22 Apr 2017 16:34:51 +0000 (12:34 -0400)
committerTyler J. Stachecki <stachecki.tyler@gmail.com>
Sat, 22 Apr 2017 16:34:51 +0000 (12:34 -0400)
Signed-off-by: Tyler J. Stachecki <stachecki.tyler@gmail.com>
compiler/passes/ssa_conversion.c
compiler/printer.c

index c68c81c..ad35203 100644 (file)
@@ -131,6 +131,7 @@ static void insert_phis_into_df(struct cen64_compiler *compiler,
 
   if (!(compiler->bb_flag_list[d->value] & CEN64_ALREADY_HAS_PHI)) {
     struct cen64_compiler_node *bb = compiler->bb_list[d->value];
+    struct cen64_compiler_node *store;
     struct cen64_compiler_node *phi;
 
     compiler->bb_flag_list[d->value] |= CEN64_ALREADY_HAS_PHI;
@@ -139,14 +140,18 @@ static void insert_phis_into_df(struct cen64_compiler *compiler,
       cen64_compiler_set_add(work_list, d->value);
     }
 
-    phi = cen64_node_alloc(&compiler->node_alloc, kPhi, 0);
+    phi = cen64_node_alloc(&compiler->node_alloc, kPhi, ~0U);
     phi->node.phi.variable = var;
 
-    phi->next = bb->node.basic_block.head;
-    phi->next->prev = phi;
-    phi->prev = NULL;
+    store = cen64_node_alloc(&compiler->node_alloc, kStore, ~0U);
+    store->node.store.name = var->node.variable.name;
+    store->node.store.value = phi;
 
-    bb->node.basic_block.head = phi;
+    store->next = bb->node.basic_block.head;
+    store->next->prev = store;
+    store->prev = NULL;
+
+    bb->node.basic_block.head = store;
   }
 }
 
index 01e8a71..adfe6be 100644 (file)
@@ -36,7 +36,12 @@ static void print_program_child_node(FILE *stream,
 static void print_primary_expression(FILE *stream,
     struct cen64_compiler_node *node, unsigned level);
 
-static void print_bblist(FILE *stream,
+void cen64_compiler_print_program(FILE *stream, struct cen64_compiler *compiler,
+    const struct cen64_compiler_program_node *program) {
+  print_program_child_node(stream, compiler, program->head->next);
+}
+
+void print_bblist(FILE *stream,
     struct cen64_compiler_bblist *bblist) {
   unsigned i;
 
@@ -204,12 +209,6 @@ void print_function_body(FILE *stream, struct cen64_compiler *compiler,
 
           break;
 
-        case kPhi:
-          fprintf(stream, "Phi [%s]\n", temp->node.phi.variable->
-              node.variable.name);
-
-          break;
-
         case kStore:
           inner = temp->node.store.value;
 
@@ -307,11 +306,6 @@ void print_object_or_primitive_type(FILE *stream,
     : fprintf(stream, "%s", primitive_type);
 }
 
-void cen64_compiler_print_program(FILE *stream, struct cen64_compiler *compiler,
-    const struct cen64_compiler_program_node *program) {
-  print_program_child_node(stream, compiler, program->head->next);
-}
-
 void print_program_child_node(FILE *stream, struct cen64_compiler *compiler,
     struct cen64_compiler_node *node) {
   struct cen64_compiler_function_node *fn;
@@ -411,6 +405,10 @@ void print_primary_expression(FILE *stream,
           node->node.load.name, node->node.load.ssa_ref);
       break;
 
+    case kPhi:
+      fprintf(stream, "Phi []");
+      break;
+
     default:
       fprintf(stream, "???");
       break;