c code refactoring or optimizing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis Yurichev

    c code refactoring or optimizing

    Hi.
    How can I find some ready tool or how can I find a proper way to
    develope tool which will convert such code:

    if (a>b+2+2)
    {
    f1();
    f2(a*3*2);
    } else
    {
    f1();
    f3(a);
    }

    To something like that:

    f1();
    if (a>b+4)
    f2(a*6)
    else
    f3(a);

    In another words, this is what is done by modern compiler at
    optimization phase, right? But I need to have C-source at the input
    and C-source at the output.
    Could please anyone point me to any ideas or ready works.

  • Kenneth Brody

    #2
    Re: c code refactoring or optimizing

    Dennis Yurichev wrote:
    >
    Hi.
    How can I find some ready tool or how can I find a proper way to
    develope tool which will convert such code:
    >
    if (a>b+2+2)
    {
    f1();
    f2(a*3*2);
    } else
    {
    f1();
    f3(a);
    }
    >
    To something like that:
    >
    f1();
    if (a>b+4)
    f2(a*6)
    else
    f3(a);
    >
    In another words, this is what is done by modern compiler at
    optimization phase, right? But I need to have C-source at the input
    and C-source at the output.
    Could please anyone point me to any ideas or ready works.
    It can be done only if you can prove that f1() has no side effects
    which affect a or b. (And, if a and b aren't simple variables, that
    accessing them has no effect on f1() either.)

    Yes, the compiler will probably take "a*3*2" and generate "multiply
    a times 6" in the output. However, whether it will move the call to
    f1(), even if it can prove the "no side effects", is up to the
    writers of the compiler, and may not happen on any compiler.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

    Comment

    • Dennis Yurichev

      #3
      Re: c code refactoring or optimizing

      On 27 ÍÁÒ, 21:46, Kenneth Brody <kenbr...@spamc op.netwrote:
      Dennis Yurichev wrote:
      >
      Hi.
      How can I find some ready tool or how can I find a proper way to
      develope tool which will convert such code:
      >
      if (a>b+2+2)
      {
      f1();
      f2(a*3*2);
      } else
      {
      f1();
      f3(a);
      }
      >
      To something like that:
      >
      f1();
      if (a>b+4)
      f2(a*6)
      else
      f3(a);
      >
      In another words, this is what is done by modern compiler at
      optimization phase, right? But I need to have C-source at the input
      and C-source at the output.
      Could please anyone point me to any ideas or ready works.
      >
      It can be done only if you can prove that f1() has no side effects
      which affect a or b. (And, if a and b aren't simple variables, that
      accessing them has no effect on f1() either.)
      Yes, I forgot to mention that a and b are local variables that are not
      in scope inside of f1();

      Comment

      • Malcolm McLean

        #4
        Re: c code refactoring or optimizing


        "Kenneth Brody" <kenbrody@spamc op.netwrote
        Dennis Yurichev wrote:
        >>
        >Hi.
        >How can I find some ready tool or how can I find a proper way to
        >develope tool which will convert such code:
        >>
        >if (a>b+2+2)
        >{
        > f1();
        > f2(a*3*2);
        >} else
        >{
        > f1();
        > f3(a);
        >}
        >>
        >To something like that:
        >>
        >f1();
        >if (a>b+4)
        > f2(a*6)
        >else
        > f3(a);
        >>
        >In another words, this is what is done by modern compiler at
        >optimization phase, right? But I need to have C-source at the input
        >and C-source at the output.
        >Could please anyone point me to any ideas or ready works.
        >
        It can be done only if you can prove that f1() has no side effects
        which affect a or b. (And, if a and b aren't simple variables, that
        accessing them has no effect on f1() either.)
        >
        Yes, the compiler will probably take "a*3*2" and generate "multiply
        a times 6" in the output. However, whether it will move the call to
        f1(), even if it can prove the "no side effects", is up to the
        writers of the compiler, and may not happen on any compiler.
        >
        The optimisation would save only a few bytes of code space to set up and
        call the function twice. It wouldn't reduce the number of calls. It is
        unlikely that this optimisation would be worthwhile.

        Comment

        • Flash Gordon

          #5
          Re: c code refactoring or optimizing

          Dennis Yurichev wrote, On 27/03/07 19:51:
          On 27 ÍÁÒ, 21:46, Kenneth Brody <kenbr...@spamc op.netwrote:
          >Dennis Yurichev wrote:
          <snip>
          >>In another words, this is what is done by modern compiler at
          >>optimizatio n phase, right? But I need to have C-source at the input
          >>and C-source at the output.
          >>Could please anyone point me to any ideas or ready works.
          >It can be done only if you can prove that f1() has no side effects
          >which affect a or b. (And, if a and b aren't simple variables, that
          >accessing them has no effect on f1() either.)
          >
          Yes, I forgot to mention that a and b are local variables that are not
          in scope inside of f1();
          Basically you will have to implement a significant amount of a compiler
          and optimiser to solve this problem (you won't need to generate
          assembler/machine-code, but you need to go far enough that you have a
          representation you can optimise). Or you could write a backend to gcc
          which emits C code, although how readable it would be is another matter.
          --
          Flash Gordon

          Comment

          • Eric Sosman

            #6
            Re: c code refactoring or optimizing

            Dennis Yurichev wrote On 03/27/07 13:10,:
            Hi.
            How can I find some ready tool or how can I find a proper way to
            develope tool which will convert such code:
            >
            if (a>b+2+2)
            {
            f1();
            f2(a*3*2);
            } else
            {
            f1();
            f3(a);
            }
            >
            To something like that:
            >
            f1();
            if (a>b+4)
            f2(a*6)
            else
            f3(a);
            >
            In another words, this is what is done by modern compiler at
            optimization phase, right? But I need to have C-source at the input
            and C-source at the output.
            Could please anyone point me to any ideas or ready works.
            As others have said, you'll need most of a compiler's machinery
            to get started at this. If you want to build such a tool, you should
            probably begin with an open-source C compiler and start modifying it.

            But why do you want to do this? I can imagine a lot of plausible
            transformations that would be entirely valid on the machine at hand,
            but would completely ruin the code for any other machine. For instance

            if (CHAR_MIN < 0)
            printf ("Signed char\n");
            else
            printf ("Unsigned char\n");

            .... would not be "improved" by expanding CHAR_MIN, evaluating the
            constant expression, eliminating the test, and replacing the whole
            thing with one or the other of the printf() calls! Similarly, the
            messy expression in

            char buff[1+(sizeof(int)* CHAR_BIT+2)/3+1];
            sprintf (buff, "%d", count);

            .... should probably be left as it is and not "economized ." And yet,
            if you simply ignore preprocessor macros you may lack enough information
            to tell whether a transformation is useful, or even possible. It seems
            you'd need a front-end that would "sort of" preprocess the source code
            but "sort of" not do so. Tricky.

            What problem are you trying to solve by applying such a tool?

            --
            Eric.Sosman@sun .com

            Comment

            • Ira Baxter

              #7
              Re: c code refactoring or optimizing

              "Dennis Yurichev" <Dennis.Yuriche v@gmail.comwrot e in message
              news:1175015416 .964426.307210@ b75g2000hsg.goo glegroups.com.. .
              Hi.
              How can I find some ready tool or how can I find a proper way to
              develope tool which will convert such code:
              >
              if (a>b+2+2)
              {
              f1();
              f2(a*3*2);
              } else
              {
              f1();
              f3(a);
              }
              >
              To something like that:
              >
              f1();
              if (a>b+4)
              f2(a*6)
              else
              f3(a);
              >
              In another words, this is what is done by modern compiler at
              optimization phase, right? But I need to have C-source at the input
              and C-source at the output.
              Could please anyone point me to any ideas or ready works.
              The DMS Software Reengineering Toolkit can carry out these
              kind of transformations . It provides a full C parser and symbol
              table and an anti-parser to convert internal trees back into
              legal source source. The transformations operate on internal
              ASTs. You have to provide the transforms
              and analyzers, but there is a lot of machinery for specifying
              the transforms, and there is considerable flow analysis
              machinery for collecting and propagating facts.
              DMS has been used on very large C source code bases.

              See http://www.semanticdesigns.com/Produ...CFrontEnd.html

              --
              Ira Baxter, CTO
              software, analysis, translation, porting, modification, generation, synthesis, reengineering, reverse engineering, testing, Hogan System, PLEX, AS/400, migration, program transformation, rules, parser, domain-specific, legacy, C/C++, Java, XML, COBOL, Ada, Fortran, PL/1, Agile, Testing, refactoring



              Comment

              Working...