c code refactoring or optimizing

D

Dennis Yurichev

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.
 
K

Kenneth Brody

Dennis said:
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:[email protected]>
 
D

Dennis Yurichev

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();
 
F

Flash Gordon

Dennis Yurichev wrote, On 27/03/07 19:51:
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.
 
M

Malcolm McLean

Kenneth Brody said:
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.
 
E

Eric Sosman

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?
 
I

Ira Baxter

Dennis Yurichev said:
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/Products/FrontEnds/CFrontEnd.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top