@@ -739,6 +739,50 @@ 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+ static jCC * merge_jcc_lists (jCC * left , jCC * right ) {
744+ jCC dummy ;
745+ dummy .next_from = NULL ;
746+ jCC * tail = & dummy ;
747+
748+ while (left && right ) {
749+ if (left -> creation_seq <= right -> creation_seq ) {
750+ tail -> next_from = left ;
751+ left = left -> next_from ;
752+ } else {
753+ tail -> next_from = right ;
754+ right = right -> next_from ;
755+ }
756+ tail = tail -> next_from ;
757+ }
758+
759+ tail -> next_from = left ? left : right ;
760+ return dummy .next_from ;
761+ }
762+
763+ /* Merge sort for jCC lists to ensure chronological dump order.
764+ * Sorts by creation_seq field to preserve execution order.
765+ */
766+ static jCC * sort_jcc_list (jCC * head ) {
767+ if (!head || !head -> next_from ) return head ;
768+
769+ /* Split list into two halves using slow/fast pointer technique */
770+ jCC * slow = head ;
771+ jCC * fast = head -> next_from ;
772+
773+ while (fast && fast -> next_from ) {
774+ slow = slow -> next_from ;
775+ fast = fast -> next_from -> next_from ;
776+ }
777+
778+ /* Split at midpoint */
779+ jCC * mid = slow -> next_from ;
780+ slow -> next_from = NULL ;
781+
782+ /* Recursively sort both halves and merge */
783+ return merge_jcc_lists (sort_jcc_list (head ), sort_jcc_list (mid ));
784+ }
785+
742786/*
743787 * Print all costs of a BBCC:
744788 * - FCCs of instructions
@@ -818,6 +862,9 @@ static Bool fprint_bbcc(VgFile *fp, BBCC* bbcc, AddrPos* last)
818862 get_debug_pos (bbcc , bb_addr (bb )+ instr_info -> instr_offset , & (currCost -> p ));
819863 fprint_apos (fp , & (currCost -> p ), last , bbcc -> cxt -> fn [0 ]-> file , bbcc );
820864 something_written = True ;
865+
866+ /* Sort jcc_list by creation sequence to ensure chronological order */
867+ bbcc -> jmp [jmp ].jcc_list = sort_jcc_list (bbcc -> jmp [jmp ].jcc_list );
821868 for (jcc = bbcc -> jmp [jmp ].jcc_list ; jcc ; jcc = jcc -> next_from ) {
822869 if (((jcc -> jmpkind != jk_Call ) && (jcc -> call_counter > 0 )) ||
823870 (!CLG_ (is_zero_cost )( CLG_ (sets ).full , jcc -> cost )))
@@ -868,14 +915,17 @@ static Bool fprint_bbcc(VgFile *fp, BBCC* bbcc, AddrPos* last)
868915 fprint_fcost (fp , currCost , last );
869916 }
870917
871- if (jcc_count > 0 )
918+ if (jcc_count > 0 ) {
919+ /* Sort jcc_list by creation sequence to ensure chronological order */
920+ bbcc -> jmp [jmp ].jcc_list = sort_jcc_list (bbcc -> jmp [jmp ].jcc_list );
872921 for (jcc = bbcc -> jmp [jmp ].jcc_list ; jcc ; jcc = jcc -> next_from ) {
873922 CLG_ASSERT (jcc -> jmp == jmp );
874923 if ( ((jcc -> jmpkind != jk_Call ) && (jcc -> call_counter > 0 )) ||
875924 (!CLG_ (is_zero_cost )( CLG_ (sets ).full , jcc -> cost )))
876-
925+
877926 fprint_jcc (fp , jcc , & (currCost -> p ), last , ecounter );
878927 }
928+ }
879929 }
880930
881931 if (CLG_ (clo ).dump_bbs || CLG_ (clo ).dump_bb ) {
0 commit comments