@@ -739,6 +739,53 @@ static void fprint_jcc(VgFile *fp, jCC* jcc, AddrPos* curr, AddrPos* last,
739739static AddrCost ccSum [2 ];
740740static int currSum ;
741741
742+ /* Merge two sorted jCC lists.
743+ * Assumes both input lists are sorted by creation_seq.
744+ * Returns a new merged list that is also sorted by creation_seq.
745+ */
746+ static jCC * merge_jcc_lists (jCC * left , jCC * right ) {
747+ jCC dummy ;
748+ dummy .next_from = NULL ;
749+ jCC * tail = & dummy ;
750+
751+ while (left && right ) {
752+ if (left -> creation_seq <= right -> creation_seq ) {
753+ tail -> next_from = left ;
754+ left = left -> next_from ;
755+ } else {
756+ tail -> next_from = right ;
757+ right = right -> next_from ;
758+ }
759+ tail = tail -> next_from ;
760+ }
761+
762+ tail -> next_from = left ? left : right ;
763+ return dummy .next_from ;
764+ }
765+
766+ /* Merge sort for jCC lists to ensure chronological dump order.
767+ * Sorts by creation_seq field to preserve execution order.
768+ */
769+ static jCC * sort_jcc_list (jCC * head ) {
770+ if (!head || !head -> next_from ) return head ;
771+
772+ /* Split list into two halves using slow/fast pointer technique */
773+ jCC * slow = head ;
774+ jCC * fast = head -> next_from ;
775+
776+ while (fast && fast -> next_from ) {
777+ slow = slow -> next_from ;
778+ fast = fast -> next_from -> next_from ;
779+ }
780+
781+ /* Split at midpoint */
782+ jCC * mid = slow -> next_from ;
783+ slow -> next_from = NULL ;
784+
785+ /* Recursively sort both halves and merge */
786+ return merge_jcc_lists (sort_jcc_list (head ), sort_jcc_list (mid ));
787+ }
788+
742789/*
743790 * Print all costs of a BBCC:
744791 * - FCCs of instructions
@@ -818,6 +865,9 @@ static Bool fprint_bbcc(VgFile *fp, BBCC* bbcc, AddrPos* last)
818865 get_debug_pos (bbcc , bb_addr (bb )+ instr_info -> instr_offset , & (currCost -> p ));
819866 fprint_apos (fp , & (currCost -> p ), last , bbcc -> cxt -> fn [0 ]-> file , bbcc );
820867 something_written = True ;
868+
869+ /* Sort jcc_list by creation sequence to ensure chronological order */
870+ bbcc -> jmp [jmp ].jcc_list = sort_jcc_list (bbcc -> jmp [jmp ].jcc_list );
821871 for (jcc = bbcc -> jmp [jmp ].jcc_list ; jcc ; jcc = jcc -> next_from ) {
822872 if (((jcc -> jmpkind != jk_Call ) && (jcc -> call_counter > 0 )) ||
823873 (!CLG_ (is_zero_cost )( CLG_ (sets ).full , jcc -> cost )))
@@ -868,14 +918,17 @@ static Bool fprint_bbcc(VgFile *fp, BBCC* bbcc, AddrPos* last)
868918 fprint_fcost (fp , currCost , last );
869919 }
870920
871- if (jcc_count > 0 )
921+ if (jcc_count > 0 ) {
922+ /* Sort jcc_list by creation sequence to ensure chronological order */
923+ bbcc -> jmp [jmp ].jcc_list = sort_jcc_list (bbcc -> jmp [jmp ].jcc_list );
872924 for (jcc = bbcc -> jmp [jmp ].jcc_list ; jcc ; jcc = jcc -> next_from ) {
873925 CLG_ASSERT (jcc -> jmp == jmp );
874926 if ( ((jcc -> jmpkind != jk_Call ) && (jcc -> call_counter > 0 )) ||
875927 (!CLG_ (is_zero_cost )( CLG_ (sets ).full , jcc -> cost )))
876-
928+
877929 fprint_jcc (fp , jcc , & (currCost -> p ), last , ecounter );
878930 }
931+ }
879932 }
880933
881934 if (CLG_ (clo ).dump_bbs || CLG_ (clo ).dump_bb ) {
0 commit comments