variable reusing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

variable reusing

kuldeep dhaka
hello guys,

while coding (embedded devices) i got an idea.

void uart_send_param(uint8_t before, uint8_t after, uint8_t default_value)
{
    uint8_t i = 0;

    uart_send(ASCII_ESCAPE);
    uart_send(before);

    while(before < vt100_param_getcount())
    {
        if(vt100_param_get(i) != default_value)
        {
            uart_send_int(vt100_param_get(i));
        }

        if(++i < vt100_param_getcount())
            uart_send(VT100_PARAM_DELIMITER);
    }

    uart_send(after);
}

in the above code, after sending uart_send(before) , "before" is no more
useful to me.
but for while loop i have to get a new variable "i" for iteration.
in this case the compiler allocates the memory in stack.
since in embedded system, everything is scare , reusing things come handy
and efficient.

but if i try to reuse "before" in loop as replacement of "i" , it looks odd
+ bad coding practise, right?

but if their is something like __reuse__("before", "i")  ( like
__attribute__() or sizeof() ) or uart_send_param(uint8_t before|i, uint8_t
after, uint8_t default_value)
after that  i could use "before" as "i".

if "i" is just finished up with register indexing and no stack allocation,
then that good but still the problem of reuse might arise in some other
case.

in some case union can be used but i think it cannot solve the above
problem.
it can help alot to reduce push and pop operations, making thing efficient.

more or like saving 4 bytes flash and 2(or 1 byte, i dont know :) ) byte of
stack.( msp are 16bit MCU's, msp430G2552 only have 8K flash and 256Byte Ram
, so that comparable).
and when the flash get full or ram overflows it really hurts :)

note: rejected by mail demon of [hidden email] so im trying here.
--
*Kuldeep Singh Dhaka*
Sakul
+91-8791676237
[hidden email]
Programmer, Open Source, Web Developer, Entrepreneur, Animal Lover,
Student, Blogger, Reverse Engineer, Embedded System.

Bitcoins Accepted.
My GnuPG Public
Key<http://pgp.mit.edu:11371/pks/lookup?op=get&fingerprint=on&search=0x23816C5609DC7E26>
Fork Me <https://github.com/kuldeepdhaka>

------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

Hardy Griech
On 14.05.2013 20:01, kuldeep dhaka wrote:
:

> while coding (embedded devices) i got an idea.
>
> void uart_send_param(uint8_t before, uint8_t after, uint8_t default_value)
> {
>      uint8_t i = 0;
>
>      uart_send(ASCII_ESCAPE);
>      uart_send(before);
>
>      while(before < vt100_param_getcount())
>      {
>          if(vt100_param_get(i) != default_value)
>          {
>              uart_send_int(vt100_param_get(i));
>          }
>
>          if(++i < vt100_param_getcount())
>              uart_send(VT100_PARAM_DELIMITER);
>      }
>
>      uart_send(after);
> }
>
> in the above code, after sending uart_send(before) , "before" is no more
> useful to me.

Perhaps I'm mistaken, bit what about the "before" usage one statement later?

Do I understand correctly, that you want recycle variable space?  If so:
don't care, let the compiler handle such situations.

If you are in doubt, generate assember output of your differently
optimized sources and compare them.

Hardy


------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

Paul Sokolovsky
In reply to this post by kuldeep dhaka
Hello,

On Tue, 14 May 2013 23:31:03 +0530
kuldeep dhaka <[hidden email]> wrote:

> hello guys,
>
> while coding (embedded devices) i got an idea.
>

[]

> in the above code, after sending uart_send(before) , "before" is no
> more useful to me.
> but for while loop i have to get a new variable "i" for iteration.
> in this case the compiler allocates the memory in stack.
> since in embedded system, everything is scare , reusing things come
> handy and efficient.
>
> but if i try to reuse "before" in loop as replacement of "i" , it
> looks odd
> + bad coding practise, right?
>
> but if their is something like __reuse__("before", "i")  ( like
> __attribute__() or sizeof() ) or uart_send_param(uint8_t before|i,
> uint8_t after, uint8_t default_value)
> after that  i could use "before" as "i".

You don't need to do anything for this to work as you describe for
*local variables* - compiler is supposed to do this for you (at
relevant optimization levels, like -O2). Google for "live range
analysis" for more info,
http://en.wikipedia.org/wiki/Register_allocation and
http://en.wikipedia.org/wiki/Live_variable_analysis are a good start.

More interesting question is such support for static (if not global)
variables. When trying to do Elliptic Curve Cryptography on MSP430
value line devices which top up at 0.5K RAM, you really wish that
compiler could "pack" together static buffers of functions which cannot
be active at the same time ;-). If anyone know of that to be done by
gcc, would be nice to get some pointers.


--
Best regards,
 Paul                          mailto:[hidden email]

------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

Daniel Beer-3
On Tue, May 14, 2013 at 09:26:46PM +0300, Paul Sokolovsky wrote:
> More interesting question is such support for static (if not global)
> variables. When trying to do Elliptic Curve Cryptography on MSP430
> value line devices which top up at 0.5K RAM, you really wish that
> compiler could "pack" together static buffers of functions which cannot
> be active at the same time ;-). If anyone know of that to be done by
> gcc, would be nice to get some pointers.

If you have static buffers which are never in use simultaneously, why
not keep them together in a union { }?

- Daniel

--
Daniel Beer <[hidden email]>    www.dlbeer.co.nz
IRC: inittab (Freenode)    PGP key: 2048D/160A553B

------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

Paul Sokolovsky
Hello,

On Wed, 15 May 2013 09:35:14 +1200
Daniel Beer <[hidden email]> wrote:

> On Tue, May 14, 2013 at 09:26:46PM +0300, Paul Sokolovsky wrote:
> > More interesting question is such support for static (if not global)
> > variables. When trying to do Elliptic Curve Cryptography on MSP430
> > value line devices which top up at 0.5K RAM, you really wish that
> > compiler could "pack" together static buffers of functions which
> > cannot be active at the same time ;-). If anyone know of that to be
> > done by gcc, would be nice to get some pointers.
>
> If you have static buffers which are never in use simultaneously, why
> not keep them together in a union { }?

Yes, the talk was about offloading the work of proving
non-simultaneous access constraints and creation of that union to the
compiler ;-). Shouldn't be hard for the case of scalar static variables
(function-local dataflow analysis and interprocedural control flow
analysis), so I wondered if someone knows it to be implemented. To be
really useful this should be applied to structural/array vars though,
and that's much harder.

>
> - Daniel


--
Best regards,
 Paul                          mailto:[hidden email]

------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

Wayne Uroda-3
Would either of these solutions work?:

- use a scheme like Malloc/free to allocate the buffers when you need them
- allocate the buffers on the stack (is there a practical limit to doing this?) and that way they can be recycled when not in use

The terrible downside with stack based allocation is the potential for silent and deadly stack overflow. At least a scheme like Malloc/free would give you an error, but you wouldn't be able to do anything useful with it at runtime!

It's an interesting idea and would be very useful for embedded development,

How would the static analysis know when a certain buffer is no longer needed, would you need to insert some kind of "static release" statement/attribute somewhere?

- Wayne

On 15/05/2013, at 8:31, Paul Sokolovsky <[hidden email]> wrote:

> Hello,
>
> On Wed, 15 May 2013 09:35:14 +1200
> Daniel Beer <[hidden email]> wrote:
>
>> On Tue, May 14, 2013 at 09:26:46PM +0300, Paul Sokolovsky wrote:
>>> More interesting question is such support for static (if not global)
>>> variables. When trying to do Elliptic Curve Cryptography on MSP430
>>> value line devices which top up at 0.5K RAM, you really wish that
>>> compiler could "pack" together static buffers of functions which
>>> cannot be active at the same time ;-). If anyone know of that to be
>>> done by gcc, would be nice to get some pointers.
>>
>> If you have static buffers which are never in use simultaneously, why
>> not keep them together in a union { }?
>
> Yes, the talk was about offloading the work of proving
> non-simultaneous access constraints and creation of that union to the
> compiler ;-). Shouldn't be hard for the case of scalar static variables
> (function-local dataflow analysis and interprocedural control flow
> analysis), so I wondered if someone knows it to be implemented. To be
> really useful this should be applied to structural/array vars though,
> and that's much harder.
>
>>
>> - Daniel
>
>
> --
> Best regards,
> Paul                          mailto:[hidden email]
>
> ------------------------------------------------------------------------------
> AlienVault Unified Security Management (USM) platform delivers complete
> security visibility with the essential security capabilities. Easily and
> efficiently configure, manage, and operate all of your security controls
> from a single console and one unified framework. Download a free trial.
> http://p.sf.net/sfu/alienvault_d2d
> _______________________________________________
> Mspgcc-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users

------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

A.P. Horst
In reply to this post by Paul Sokolovsky

On 15-5-2013 0:31, Paul Sokolovsky wrote:

> Hello,
>
> On Wed, 15 May 2013 09:35:14 +1200
> Daniel Beer <[hidden email]> wrote:
>
>> On Tue, May 14, 2013 at 09:26:46PM +0300, Paul Sokolovsky wrote:
>>> More interesting question is such support for static (if not global)
>>> variables. When trying to do Elliptic Curve Cryptography on MSP430
>>> value line devices which top up at 0.5K RAM, you really wish that
>>> compiler could "pack" together static buffers of functions which
>>> cannot be active at the same time ;-). If anyone know of that to be
>>> done by gcc, would be nice to get some pointers.
>> If you have static buffers which are never in use simultaneously, why
>> not keep them together in a union { }?
> Yes, the talk was about offloading the work of proving
> non-simultaneous access constraints and creation of that union to the
> compiler ;-). Shouldn't be hard for the case of scalar static variables
> (function-local dataflow analysis and interprocedural control flow
> analysis), so I wondered if someone knows it to be implemented. To be
> really useful this should be applied to structural/array vars though,
> and that's much harder.
>

This would be absolutely impossible because the compiler is generally
unaware of it's environment. If the application is run on a preemptive
scheduling operating system, then such schemes could go horribly wrong.
Of course, you could protect the memory with a mutex, but this would
defeat the purpose of easy optimization. Anyway, if you really want it I
think it is best done via an explicit way with unions such that it is
clear what is being shared, and it compatible amongst compilers.

Arie


------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

William "Chops" Westfield
>
> you really wish that compiler could "pack" together static buffers of functions which cannot be active at the same time

At some point, you might as well just allocate a global array called "buffer" and use it everywhere.  You could even add some checking code to make sure no one else is using it.

While this violates all sorts of HLL programming paradigms, in some ways it makes me less uncomfortable than using multiple names with local scoping to hide the fact that you're reusing the same buffer…

alloca() is the standard function for allocating dynamic storage on the stack.

WestfW


------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

Matthias Ringwald-3
Hi

C isn't well suited for this kind of reasoning (when is what memory being used). Here's the dissertation of a friend of mine on how to use state charts or so to get there:
http://e-collection.library.ethz.ch/view/eth:29949

(I assume/hope there enough arguments and options listed in there)

Best
 Matthias

On May 15, 2013, at 9:21 AM, William Chops Westfield <[hidden email]> wrote:

>>
>> you really wish that compiler could "pack" together static buffers of functions which cannot be active at the same time
>
> At some point, you might as well just allocate a global array called "buffer" and use it everywhere.  You could even add some checking code to make sure no one else is using it.
>
> While this violates all sorts of HLL programming paradigms, in some ways it makes me less uncomfortable than using multiple names with local scoping to hide the fact that you're reusing the same buffer…
>
> alloca() is the standard function for allocating dynamic storage on the stack.
>
> WestfW
>
>
> ------------------------------------------------------------------------------
> AlienVault Unified Security Management (USM) platform delivers complete
> security visibility with the essential security capabilities. Easily and
> efficiently configure, manage, and operate all of your security controls
> from a single console and one unified framework. Download a free trial.
> http://p.sf.net/sfu/alienvault_d2d
> _______________________________________________
> Mspgcc-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users


------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

David Brown-4
In reply to this post by A.P. Horst
On 15/05/13 08:30, A.P. Horst wrote:

>
> On 15-5-2013 0:31, Paul Sokolovsky wrote:
>> Hello,
>>
>> On Wed, 15 May 2013 09:35:14 +1200
>> Daniel Beer <[hidden email]> wrote:
>>
>>> On Tue, May 14, 2013 at 09:26:46PM +0300, Paul Sokolovsky wrote:
>>>> More interesting question is such support for static (if not global)
>>>> variables. When trying to do Elliptic Curve Cryptography on MSP430
>>>> value line devices which top up at 0.5K RAM, you really wish that
>>>> compiler could "pack" together static buffers of functions which
>>>> cannot be active at the same time ;-). If anyone know of that to be
>>>> done by gcc, would be nice to get some pointers.
>>> If you have static buffers which are never in use simultaneously, why
>>> not keep them together in a union { }?
>> Yes, the talk was about offloading the work of proving
>> non-simultaneous access constraints and creation of that union to the
>> compiler ;-). Shouldn't be hard for the case of scalar static variables
>> (function-local dataflow analysis and interprocedural control flow
>> analysis), so I wondered if someone knows it to be implemented. To be
>> really useful this should be applied to structural/array vars though,
>> and that's much harder.
>>
>
> This would be absolutely impossible because the compiler is generally
> unaware of it's environment. If the application is run on a preemptive
> scheduling operating system, then such schemes could go horribly wrong.
> Of course, you could protect the memory with a mutex, but this would
> defeat the purpose of easy optimization. Anyway, if you really want it I
> think it is best done via an explicit way with unions such that it is
> clear what is being shared, and it compatible amongst compilers.
>
> Arie
>

The general case is impossible (since it reduces to the halting
problem), and even without pre-emption it is impractical to get optimal
results (since it would take exponential time).  However, for many
practical situations it /is/ possible to do quite a good job.  In
particular, whole-program optimisation and heavy use of "static" can
help the compiler determine when different variables are in use.

gcc does not do this sort of analysis and global data reuse, but some
embedded compilers /do/.  In particular, some of the more high-end
compilers for very small microcontrollers (Keil's 8051 compiler and
ByteCraft's compilers being examples) do exactly this sort of analysis.
 For these chips, stacks are impractical due to the lack of useful
addressing modes, so function parameters, return values, and local
variables all end up in fixed locations in ram (unless functions are
re-entrant - in which case a stack is needed giving a huge performance
hit).  This sort of global lifetime analysis is essential to reduce the
ram usage.

For more general code on more sensible processors, such as the msp430,
it would be hard to make such analysis algorithms that always work, run
in a sensible amount of time, and give you a significant benefit for the
effort.



------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

David Brown-4
In reply to this post by kuldeep dhaka

What compiler flags did you use?  It sounds to me that you are compiling
without enabling optimisation - in which case you can't expect good code.

mvh.,

David


On 14/05/13 20:01, kuldeep dhaka wrote:

> hello guys,
>
> while coding (embedded devices) i got an idea.
>
> void uart_send_param(uint8_t before, uint8_t after, uint8_t default_value)
> {
>     uint8_t i = 0;
>
>     uart_send(ASCII_ESCAPE);
>     uart_send(before);
>
>     while(before < vt100_param_getcount())
>     {
>         if(vt100_param_get(i) != default_value)
>         {
>             uart_send_int(vt100_param_get(i));
>         }
>
>         if(++i < vt100_param_getcount())
>             uart_send(VT100_PARAM_DELIMITER);
>     }
>
>     uart_send(after);
> }
>
> in the above code, after sending uart_send(before) , "before" is no more
> useful to me.
> but for while loop i have to get a new variable "i" for iteration.
> in this case the compiler allocates the memory in stack.
> since in embedded system, everything is scare , reusing things come handy
> and efficient.
>
> but if i try to reuse "before" in loop as replacement of "i" , it looks odd
> + bad coding practise, right?
>
> but if their is something like __reuse__("before", "i")  ( like
> __attribute__() or sizeof() ) or uart_send_param(uint8_t before|i, uint8_t
> after, uint8_t default_value)
> after that  i could use "before" as "i".
>
> if "i" is just finished up with register indexing and no stack allocation,
> then that good but still the problem of reuse might arise in some other
> case.
>
> in some case union can be used but i think it cannot solve the above
> problem.
> it can help alot to reduce push and pop operations, making thing efficient.
>
> more or like saving 4 bytes flash and 2(or 1 byte, i dont know :) ) byte of
> stack.( msp are 16bit MCU's, msp430G2552 only have 8K flash and 256Byte Ram
> , so that comparable).
> and when the flash get full or ram overflows it really hurts :)
>
> note: rejected by mail demon of [hidden email] so im trying here.
>


------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

Crazy Casta
In reply to this post by David Brown-4
Isn't that exactly what LTO of the new GCC versions does. I have no idea
whether mspgcc supports it, though I would guess it might. My understanding
is that GCC LTO works by just storing the GIMPLE trees in the object files,
stitching them together at link time and generating code from the stitched
GIMPLE tree instead of regular linking. Of course there's some complexity
in there, but it might be independent of the machine specific code.


On Wed, May 15, 2013 at 5:15 AM, David Brown <[hidden email]> wrote:

> On 15/05/13 08:30, A.P. Horst wrote:
> >
> > On 15-5-2013 0:31, Paul Sokolovsky wrote:
> >> Hello,
> >>
> >> On Wed, 15 May 2013 09:35:14 +1200
> >> Daniel Beer <[hidden email]> wrote:
> >>
> >>> On Tue, May 14, 2013 at 09:26:46PM +0300, Paul Sokolovsky wrote:
> >>>> More interesting question is such support for static (if not global)
> >>>> variables. When trying to do Elliptic Curve Cryptography on MSP430
> >>>> value line devices which top up at 0.5K RAM, you really wish that
> >>>> compiler could "pack" together static buffers of functions which
> >>>> cannot be active at the same time ;-). If anyone know of that to be
> >>>> done by gcc, would be nice to get some pointers.
> >>> If you have static buffers which are never in use simultaneously, why
> >>> not keep them together in a union { }?
> >> Yes, the talk was about offloading the work of proving
> >> non-simultaneous access constraints and creation of that union to the
> >> compiler ;-). Shouldn't be hard for the case of scalar static variables
> >> (function-local dataflow analysis and interprocedural control flow
> >> analysis), so I wondered if someone knows it to be implemented. To be
> >> really useful this should be applied to structural/array vars though,
> >> and that's much harder.
> >>
> >
> > This would be absolutely impossible because the compiler is generally
> > unaware of it's environment. If the application is run on a preemptive
> > scheduling operating system, then such schemes could go horribly wrong.
> > Of course, you could protect the memory with a mutex, but this would
> > defeat the purpose of easy optimization. Anyway, if you really want it I
> > think it is best done via an explicit way with unions such that it is
> > clear what is being shared, and it compatible amongst compilers.
> >
> > Arie
> >
>
> The general case is impossible (since it reduces to the halting
> problem), and even without pre-emption it is impractical to get optimal
> results (since it would take exponential time).  However, for many
> practical situations it /is/ possible to do quite a good job.  In
> particular, whole-program optimisation and heavy use of "static" can
> help the compiler determine when different variables are in use.
>
> gcc does not do this sort of analysis and global data reuse, but some
> embedded compilers /do/.  In particular, some of the more high-end
> compilers for very small microcontrollers (Keil's 8051 compiler and
> ByteCraft's compilers being examples) do exactly this sort of analysis.
>  For these chips, stacks are impractical due to the lack of useful
> addressing modes, so function parameters, return values, and local
> variables all end up in fixed locations in ram (unless functions are
> re-entrant - in which case a stack is needed giving a huge performance
> hit).  This sort of global lifetime analysis is essential to reduce the
> ram usage.
>
> For more general code on more sensible processors, such as the msp430,
> it would be hard to make such analysis algorithms that always work, run
> in a sensible amount of time, and give you a significant benefit for the
> effort.
>
>
>
>
> ------------------------------------------------------------------------------
> AlienVault Unified Security Management (USM) platform delivers complete
> security visibility with the essential security capabilities. Easily and
> efficiently configure, manage, and operate all of your security controls
> from a single console and one unified framework. Download a free trial.
> http://p.sf.net/sfu/alienvault_d2d
> _______________________________________________
> Mspgcc-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users
>

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

David Brown-4
On 22/05/13 19:30, Alex Orange wrote:
> Isn't that exactly what LTO of the new GCC versions does. I have no idea
> whether mspgcc supports it, though I would guess it might. My understanding
> is that GCC LTO works by just storing the GIMPLE trees in the object files,
> stitching them together at link time and generating code from the stitched
> GIMPLE tree instead of regular linking. Of course there's some complexity
> in there, but it might be independent of the machine specific code.


That is pretty much how LTO works (though it is more complex these days,
in order to be scalable for large code bases).  I haven't tried it
myself as yet - it can make debugging somewhat more "interesting".

However, that does not imply that gcc does any sort of analysis for
re-use of static data according to when the data in them is /actually/
used.  It is very difficult to determine when statically allocated data
is no longer needed - and can thus be re-used by something else.  Even
compilers that do re-use global data limit this mainly to data that is
logically local (local variables, parameters, etc.) that are placed in
global areas due to poor stack support on the cpu.


>
>
> On Wed, May 15, 2013 at 5:15 AM, David Brown <[hidden email]> wrote:
>
>> On 15/05/13 08:30, A.P. Horst wrote:
>>>
>>> On 15-5-2013 0:31, Paul Sokolovsky wrote:
>>>> Hello,
>>>>
>>>> On Wed, 15 May 2013 09:35:14 +1200
>>>> Daniel Beer <[hidden email]> wrote:
>>>>
>>>>> On Tue, May 14, 2013 at 09:26:46PM +0300, Paul Sokolovsky wrote:
>>>>>> More interesting question is such support for static (if not global)
>>>>>> variables. When trying to do Elliptic Curve Cryptography on MSP430
>>>>>> value line devices which top up at 0.5K RAM, you really wish that
>>>>>> compiler could "pack" together static buffers of functions which
>>>>>> cannot be active at the same time ;-). If anyone know of that to be
>>>>>> done by gcc, would be nice to get some pointers.
>>>>> If you have static buffers which are never in use simultaneously, why
>>>>> not keep them together in a union { }?
>>>> Yes, the talk was about offloading the work of proving
>>>> non-simultaneous access constraints and creation of that union to the
>>>> compiler ;-). Shouldn't be hard for the case of scalar static variables
>>>> (function-local dataflow analysis and interprocedural control flow
>>>> analysis), so I wondered if someone knows it to be implemented. To be
>>>> really useful this should be applied to structural/array vars though,
>>>> and that's much harder.
>>>>
>>>
>>> This would be absolutely impossible because the compiler is generally
>>> unaware of it's environment. If the application is run on a preemptive
>>> scheduling operating system, then such schemes could go horribly wrong.
>>> Of course, you could protect the memory with a mutex, but this would
>>> defeat the purpose of easy optimization. Anyway, if you really want it I
>>> think it is best done via an explicit way with unions such that it is
>>> clear what is being shared, and it compatible amongst compilers.
>>>
>>> Arie
>>>
>>
>> The general case is impossible (since it reduces to the halting
>> problem), and even without pre-emption it is impractical to get optimal
>> results (since it would take exponential time).  However, for many
>> practical situations it /is/ possible to do quite a good job.  In
>> particular, whole-program optimisation and heavy use of "static" can
>> help the compiler determine when different variables are in use.
>>
>> gcc does not do this sort of analysis and global data reuse, but some
>> embedded compilers /do/.  In particular, some of the more high-end
>> compilers for very small microcontrollers (Keil's 8051 compiler and
>> ByteCraft's compilers being examples) do exactly this sort of analysis.
>>  For these chips, stacks are impractical due to the lack of useful
>> addressing modes, so function parameters, return values, and local
>> variables all end up in fixed locations in ram (unless functions are
>> re-entrant - in which case a stack is needed giving a huge performance
>> hit).  This sort of global lifetime analysis is essential to reduce the
>> ram usage.
>>
>> For more general code on more sensible processors, such as the msp430,
>> it would be hard to make such analysis algorithms that always work, run
>> in a sensible amount of time, and give you a significant benefit for the
>> effort.
>>

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: variable reusing

Crazy Casta
Correct, I am unsure whether gcc does any sort of static re-use analysis or
not. I was just getting the feeling that you were saying that GCC doesn't
do any sort of whole-program analysis and wanted to clarify that part.


On Thu, May 23, 2013 at 2:41 AM, David Brown <[hidden email]> wrote:

> On 22/05/13 19:30, Alex Orange wrote:
> > Isn't that exactly what LTO of the new GCC versions does. I have no idea
> > whether mspgcc supports it, though I would guess it might. My
> understanding
> > is that GCC LTO works by just storing the GIMPLE trees in the object
> files,
> > stitching them together at link time and generating code from the
> stitched
> > GIMPLE tree instead of regular linking. Of course there's some complexity
> > in there, but it might be independent of the machine specific code.
>
>
> That is pretty much how LTO works (though it is more complex these days,
> in order to be scalable for large code bases).  I haven't tried it
> myself as yet - it can make debugging somewhat more "interesting".
>
> However, that does not imply that gcc does any sort of analysis for
> re-use of static data according to when the data in them is /actually/
> used.  It is very difficult to determine when statically allocated data
> is no longer needed - and can thus be re-used by something else.  Even
> compilers that do re-use global data limit this mainly to data that is
> logically local (local variables, parameters, etc.) that are placed in
> global areas due to poor stack support on the cpu.
>
>
> >
> >
> > On Wed, May 15, 2013 at 5:15 AM, David Brown <[hidden email]>
> wrote:
> >
> >> On 15/05/13 08:30, A.P. Horst wrote:
> >>>
> >>> On 15-5-2013 0:31, Paul Sokolovsky wrote:
> >>>> Hello,
> >>>>
> >>>> On Wed, 15 May 2013 09:35:14 +1200
> >>>> Daniel Beer <[hidden email]> wrote:
> >>>>
> >>>>> On Tue, May 14, 2013 at 09:26:46PM +0300, Paul Sokolovsky wrote:
> >>>>>> More interesting question is such support for static (if not global)
> >>>>>> variables. When trying to do Elliptic Curve Cryptography on MSP430
> >>>>>> value line devices which top up at 0.5K RAM, you really wish that
> >>>>>> compiler could "pack" together static buffers of functions which
> >>>>>> cannot be active at the same time ;-). If anyone know of that to be
> >>>>>> done by gcc, would be nice to get some pointers.
> >>>>> If you have static buffers which are never in use simultaneously, why
> >>>>> not keep them together in a union { }?
> >>>> Yes, the talk was about offloading the work of proving
> >>>> non-simultaneous access constraints and creation of that union to the
> >>>> compiler ;-). Shouldn't be hard for the case of scalar static
> variables
> >>>> (function-local dataflow analysis and interprocedural control flow
> >>>> analysis), so I wondered if someone knows it to be implemented. To be
> >>>> really useful this should be applied to structural/array vars though,
> >>>> and that's much harder.
> >>>>
> >>>
> >>> This would be absolutely impossible because the compiler is generally
> >>> unaware of it's environment. If the application is run on a preemptive
> >>> scheduling operating system, then such schemes could go horribly wrong.
> >>> Of course, you could protect the memory with a mutex, but this would
> >>> defeat the purpose of easy optimization. Anyway, if you really want it
> I
> >>> think it is best done via an explicit way with unions such that it is
> >>> clear what is being shared, and it compatible amongst compilers.
> >>>
> >>> Arie
> >>>
> >>
> >> The general case is impossible (since it reduces to the halting
> >> problem), and even without pre-emption it is impractical to get optimal
> >> results (since it would take exponential time).  However, for many
> >> practical situations it /is/ possible to do quite a good job.  In
> >> particular, whole-program optimisation and heavy use of "static" can
> >> help the compiler determine when different variables are in use.
> >>
> >> gcc does not do this sort of analysis and global data reuse, but some
> >> embedded compilers /do/.  In particular, some of the more high-end
> >> compilers for very small microcontrollers (Keil's 8051 compiler and
> >> ByteCraft's compilers being examples) do exactly this sort of analysis.
> >>  For these chips, stacks are impractical due to the lack of useful
> >> addressing modes, so function parameters, return values, and local
> >> variables all end up in fixed locations in ram (unless functions are
> >> re-entrant - in which case a stack is needed giving a huge performance
> >> hit).  This sort of global lifetime analysis is essential to reduce the
> >> ram usage.
> >>
> >> For more general code on more sensible processors, such as the msp430,
> >> it would be hard to make such analysis algorithms that always work, run
> >> in a sensible amount of time, and give you a significant benefit for the
> >> effort.
> >>
>

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users