Adding constant optimizations to Clang AST

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding constant optimizations to Clang AST

dmanatunga
I am working on implementing a constant reassociation optimization to the AST which will take an expression, such as:
x = 10 + a + 5
and reassociate it to some form of:
x = 10 + 5 + a.

Where would be the best place to apply this optimization? Should it be done through a plugin using a visitor (can plugins edit the AST before final compilation)? Or do I need to manually edit certain locations in the source code.

-Dilan
Reply | Threaded
Open this post in threaded view
|

Re: Adding constant optimizations to Clang AST

David Blaikie
On Wed, Feb 6, 2013 at 12:17 PM, dmanatunga <[hidden email]> wrote:
> I am working on implementing a constant reassociation optimization to the AST
> which will take an expression, such as:
> x = 10 + a + 5
> and reassociate it to some form of:
> x = 10 + 5 + a.

Why? LLVM optimizations will already do the right thing with this sort of code.

> Where would be the best place to apply this optimization? Should it be done
> through a plugin using a visitor (can plugins edit the AST before final
> compilation)? Or do I need to manually edit certain locations in the source
> code.

Are you trying to transform the user's code for them? (ie: do you
expect to modify the original source files or otherwise give this
source back to the user in some form) Or just produce better binaries?
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Adding constant optimizations to Clang AST

dmanatunga
David Blaikie wrote
Why? LLVM optimizations will already do the right thing with this sort of code.
It is for an assignment that has been given to me. The Professor wants it done it for the Clang AST as opposed to the llvm IR.


David Blaikie wrote
Are you trying to transform the user's code for them? (ie: do you
expect to modify the original source files or otherwise give this
source back to the user in some form) Or just produce better binaries?
I am not modifying the original source file, or expect to give the source back to the user in some form. Just producing a better binary. I understand Clang tries to retain as much of the original code specification in the AST as possible, but I am basically ignoring that.

-Dilan
Reply | Threaded
Open this post in threaded view
|

Re: Adding constant optimizations to Clang AST

David Blaikie
On Wed, Feb 6, 2013 at 2:18 PM, dmanatunga <[hidden email]> wrote:

> David Blaikie wrote
>> Why? LLVM optimizations will already do the right thing with this sort of
>> code.
>
> It is for an assignment that has been given to me. The Professor wants it
> done it for the Clang AST as opposed to the llvm IR.
>
> David Blaikie wrote
>> Are you trying to transform the user's code for them? (ie: do you
>> expect to modify the original source files or otherwise give this
>> source back to the user in some form) Or just produce better binaries?
>
> I am not modifying the original source file, or expect to give the source
> back to the user in some form. Just producing a better binary. I understand
> Clang tries to retain as much of the original code specification in the AST
> as possible, but I am basically ignoring that.

In short: the AST is not designed to be mutated so this may not be an
easy task. I don't really have any particularly useful pointers on how
you might achieve it.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Adding constant optimizations to Clang AST

jahanian

On Feb 6, 2013, at 2:23 PM, David Blaikie <[hidden email]> wrote:

> On Wed, Feb 6, 2013 at 2:18 PM, dmanatunga <[hidden email]> wrote:
>> David Blaikie wrote
>>> Why? LLVM optimizations will already do the right thing with this sort of
>>> code.
>>
>> It is for an assignment that has been given to me. The Professor wants it
>> done it for the Clang AST as opposed to the llvm IR.
>>
>> David Blaikie wrote
>>> Are you trying to transform the user's code for them? (ie: do you
>>> expect to modify the original source files or otherwise give this
>>> source back to the user in some form) Or just produce better binaries?
>>
>> I am not modifying the original source file, or expect to give the source
>> back to the user in some form. Just producing a better binary. I understand
>> Clang tries to retain as much of the original code specification in the AST
>> as possible, but I am basically ignoring that.
>
> In short: the AST is not designed to be mutated so this may not be an
> easy task. I don't really have any particularly useful pointers on how
> you might achieve it.

One place to look at is the objective-C rewriter which walks the AST tree, makes local AST changes and
writes it back out as source. This is good way to visualize things.

 - fariborz
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Adding constant optimizations to Clang AST

John McCall
In reply to this post by David Blaikie
On Feb 6, 2013, at 2:23 PM, David Blaikie <[hidden email]> wrote:

> On Wed, Feb 6, 2013 at 2:18 PM, dmanatunga <[hidden email]> wrote:
>> David Blaikie wrote
>>> Why? LLVM optimizations will already do the right thing with this sort of
>>> code.
>>
>> It is for an assignment that has been given to me. The Professor wants it
>> done it for the Clang AST as opposed to the llvm IR.
>>
>> David Blaikie wrote
>>> Are you trying to transform the user's code for them? (ie: do you
>>> expect to modify the original source files or otherwise give this
>>> source back to the user in some form) Or just produce better binaries?
>>
>> I am not modifying the original source file, or expect to give the source
>> back to the user in some form. Just producing a better binary. I understand
>> Clang tries to retain as much of the original code specification in the AST
>> as possible, but I am basically ignoring that.
>
> In short: the AST is not designed to be mutated so this may not be an
> easy task. I don't really have any particularly useful pointers on how
> you might achieve it.

A TreeTransform would be the ridiculously expensive way of doing it.

BinaryOperator will let you see the LHS and RHS expressions, and
reassociation doesn't really disrupt any particularly serious invariants
for IR-generation, so for an assignment, this isn't too evil.  It is, of
course, absolutely not how we would approach this problem for real.

John.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev