Project Stage 2 part 3 - Compile a program with revised GCC

 

Intro

In this post, we are going to use the GCC that we modified. Then, we will compile a code and see it generates a dump file by the pass we added.


Progress

1. I will first test compiling my test program.

Here is my simple C program.

#include <stdio.h>

int main() {
    int sum = 0;

    // For loop to add numbers from 1 to 5
    for (int i = 1; i <= 5; i++) {
        sum += i; // Add i to sum

        // If statement to check if the number is even or odd
        if (i % 2 == 0) {
            printf("%d is even\n", i);
        } else {
            printf("%d is odd\n", i);
        }
    }

    printf("Sum of numbers from 1 to 5 is: %d\n", sum);
    return 0;
}

2. I compiled the prgoram
gcc -fdump-tree-all test.c -o test
And this generated the following dump file as well as other dump files
test.c.263t.ctyler

3. Check the content of the dump file.
===== Basic block count: 1 =====
----- Statement count: 1 -----
sum_7 = 0;
----- Statement count: 2 -----
i_8 = 1;
===== Basic block count: 2 =====
----- Statement count: 3 -----
sum_12 = sum_3 + i_4;
----- Statement count: 4 -----
i.0_1 = (unsigned int) i_4;
----- Statement count: 5 -----
_2 = i.0_1 & 1;
----- Statement count: 6 -----
if (_2 == 0)
===== Basic block count: 3 =====
----- Statement count: 7 -----
# .MEM_14 = VDEF <.MEM_6>
printf ("%d is even\n", i_4);
===== Basic block count: 4 =====
----- Statement count: 8 -----
# .MEM_13 = VDEF <.MEM_6>
printf ("%d is odd\n", i_4);
===== Basic block count: 5 =====
----- Statement count: 9 -----
i_15 = i_4 + 1;
===== Basic block count: 6 =====
----- Statement count: 10 -----
if (i_4 <= 5)
===== Basic block count: 7 =====
----- Statement count: 11 -----
# .MEM_10 = VDEF <.MEM_6>
printf ("Sum of numbers from 1 to 5 is: %d\n", sum_3);
----- Statement count: 12 -----
_11 = 0;
===== Basic block count: 8 =====
----- Statement count: 13 -----
<L6>:
----- Statement count: 14 -----
# VUSE <.MEM_10>
return _11;
int main ()
{
  int i;
  int sum;
  int D.3259;
  unsigned int i.0_1;
  unsigned int _2;
  int _11;

  <bb 2> :
  sum_7 = 0;
  i_8 = 1;
  goto <bb 7>; [INV]

  <bb 3> :
  sum_12 = sum_3 + i_4;
  i.0_1 = (unsigned int) i_4;
  _2 = i.0_1 & 1;
  if (_2 == 0)
    goto <bb 4>; [INV]
  else
    goto <bb 5>; [INV]

  <bb 4> :
  printf ("%d is even\n", i_4);
  goto <bb 6>; [INV]

  <bb 5> :
  printf ("%d is odd\n", i_4);

  <bb 6> :
  i_15 = i_4 + 1;

  <bb 7> :
  # sum_3 = PHI <sum_7(2), sum_12(6)>
  # i_4 = PHI <i_8(2), i_15(6)>
  if (i_4 <= 5)
    goto <bb 3>; [INV]
  else
    goto <bb 8>; [INV]

  <bb 8> :
  printf ("Sum of numbers from 1 to 5 is: %d\n", sum_3);
  _11 = 0;

  <bb 9> :
<L6>:
  return _11;

}
As you can see here, the Statement counts and basic block count are printed.

4. We have checked our modified GCC compiler works well.
Now let's make more changes to it.

I added some codes to print the function name

FOR_EACH_BB_FN (bb, fun)
      {
        bb_cnt++;
        if (dump_file)
          {
            fprintf (dump_file, "===== Basic block count: %d =====\n", bb_cnt);
	    if (!fun || !fun->decl)
   	      {
        	fprintf(dump_file, "Invalid function object.\n");
    	      }

    	    // Print function name
	    else
	      {
                 const char *func_name = IDENTIFIER_POINTER(DECL_NAME(fun->decl));
    	    	 fprintf(dump_file, "Function Name: %s\n", func_name);
	      }
	  }
        for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
          {
            gimple *g = gsi_stmt (gsi);
            stmt_cnt++;
            if (dump_file)
              {
                fprintf (dump_file, "----- Statement count: %d -----\n", stmt_cnt);
                print_gimple_stmt (dump_file, g, 0, TDF_VOPS|TDF_MEMSYMS);
              }
         }
       }
This change will make function names printed in tree-ctyler dumpfile.
The changes look like the follows:
# result-test.c.263t.ctyler file===== Basic block count: 1 =====
Function Name: scale_samples         # This is the newly added line
----- Statement count: 1 -----
x_17 = 0;
===== Basic block count: 2 =====
Function Name: scale_samples         # This is the newly added line
----- Statement count: 2 -----
_1 = (long unsigned int) x_15;
----- Statement count: 3 -----
_2 = _1 * 2;
----- Statement count: 4 -----
_3 = in_20(D) + _2;
----- Statement count: 5 -----
# VUSE <.MEM_16>
_4 = *_3;
----- Statement count: 6 -----
_5 = (int) _4;
----- Statement count: 7 -----
_6 = volume_21(D) * 32767;

...
# skipped the rest of the result for space
As you can see, the function names are printed.

Recently, we, students, were provided a modified version of pass implementation using cgraph node structrues. Now we are going to test the provided code.
First, I tested the provided code without making any modifications,
and the results are as follows:
### For testing, I used the same c program that I showed above
;; Function calc (calc, funcdef_no=0, decl_uid=3249, cgraph_uid=1, symbol_order=0)

=== Function 1 Name 'printf' ===
=== Function 2 Name 'main' ===
=== Function 3 Name 'calc' ===


#### End ctyler diagnostics, start regular dump of current gimple ####


int calc (int x, int y)
{
  int result;
  int D.3262;
  int _4;

  <bb 2> :
  result_3 = x_1(D) + y_2(D);
  _4 = result_3;

  <bb 3> :
<L0>:
  return _4;

}



;; Function main (main, funcdef_no=1, decl_uid=3252, cgraph_uid=2, symbol_order=1)

=== Function 1 Name 'printf' ===
=== Function 2 Name 'main' ===
=== Function 3 Name 'calc' ===


#### End ctyler diagnostics, start regular dump of current gimple ####


int main ()
{
  int i;
  int total;
  int sum;
  int D.3267;
  unsigned int i.0_1;
  unsigned int _2;
  int _14;

  <bb 2> :
  sum_7 = 0;
  i_8 = 1;
  goto <bb 7>; [INV]

  <bb 3> :
  sum_15 = sum_3 + i_4;
  i.0_1 = (unsigned int) i_4;
  _2 = i.0_1 & 1;
  if (_2 == 0)
    goto <bb 4>; [INV]
  else
    goto <bb 5>; [INV]

  <bb 4> :
  printf ("%d is even\n", i_4);
  goto <bb 6>; [INV]

  <bb 5> :
  printf ("%d is odd\n", i_4);

  <bb 6> :
  i_18 = i_4 + 1;

  <bb 7> :
  # sum_3 = PHI <sum_7(2), sum_15(6)>
  # i_4 = PHI <i_8(2), i_18(6)>
  if (i_4 <= 5)
    goto <bb 3>; [INV]
  else
    goto <bb 8>; [INV]

  <bb 8> :
  total_11 = calc (4, 3);
  printf ("total : %d\n", total_11);
  printf ("Sum of numbers from 1 to 5 is: %d\n", sum_3);
  _14 = 0;

  <bb 9> :
<L6>:
  return _14;

}

Now it prints the function names more clearly than before.

Comments

popular posts in this blog

Project Stage 2 - part 2 : Clone-Pruning Analysis Pass

Project Stage 2 part 4 - Testing clone-test-core.c file with Modified GCC file and making further modification