@@ -606,59 +606,66 @@ void solve_phi_insertion(void)
606606 basic_block_t * work_list [64 ];
607607 int work_list_idx = 0 ;
608608
609+ /* Initialize work list with blocks that reference this variable */
609610 for (ref_block_t * ref = var -> ref_block_list .head ; ref ;
610611 ref = ref -> next ) {
611612 if (work_list_idx >= 64 ) /* Prevent buffer overflow */
612613 break ;
613614 work_list [work_list_idx ++ ] = ref -> bb ;
614615 }
615616
617+ /* Process work list items */
616618 for (int i = 0 ; i < work_list_idx ; i ++ ) {
617619 basic_block_t * bb = work_list [i ];
620+
618621 for (int j = 0 ; j < bb -> df_idx ; j ++ ) {
619622 basic_block_t * df = bb -> DF [j ];
623+
624+ /* Skip early conditions */
620625 if (!var_check_in_scope (var , df -> scope ))
621626 continue ;
627+ if (df == func -> exit )
628+ continue ;
629+ if (var -> is_global )
630+ continue ;
622631
632+ /* Check if variable already declared in this block */
623633 bool is_decl = false;
624634 for (symbol_t * s = df -> symbol_list .head ; s ; s = s -> next ) {
625635 if (s -> var == var ) {
626636 is_decl = true;
627637 break ;
628638 }
629639 }
630-
631640 if (is_decl )
632641 continue ;
633642
634- if (df == func -> exit )
643+ /* Try to insert phi instruction */
644+ if (!insert_phi_insn (df , var ))
635645 continue ;
636646
637- if (var -> is_global )
647+ /* Restrict phi insertion of ternary operation, and
648+ * logical-and/or operation.
649+ *
650+ * The ternary and logical-and/or operations don't
651+ * create new scope, so prevent temporary variable from
652+ * propagating through the dominance tree.
653+ */
654+ if (var -> is_ternary_ret || var -> is_logical_ret )
638655 continue ;
639656
640- if (insert_phi_insn (df , var )) {
641- bool found = false;
642-
643- /* Restrict phi insertion of ternary operation, and
644- * logical-and/or operation.
645- *
646- * The ternary and logical-and/or operations don't
647- * create new scope, so prevent temporary variable from
648- * propagating through the dominance tree.
649- */
650- if (var -> is_ternary_ret || var -> is_logical_ret )
651- continue ;
652-
653- for (int l = 0 ; l < work_list_idx ; l ++ ) {
654- if (work_list [l ] == df ) {
655- found = true;
656- break ;
657- }
657+ /* Check if already in work list */
658+ bool found = false;
659+ for (int l = 0 ; l < work_list_idx ; l ++ ) {
660+ if (work_list [l ] == df ) {
661+ found = true;
662+ break ;
658663 }
659- if (!found && work_list_idx < 64 ) /* Bounds check */
660- work_list [work_list_idx ++ ] = df ;
661664 }
665+
666+ /* Add to work list if not found and space available */
667+ if (!found && work_list_idx < 64 ) /* Bounds check */
668+ work_list [work_list_idx ++ ] = df ;
662669 }
663670 }
664671 }
0 commit comments