OPtimization of constants with keil

In my code I have many of constants have same value. I cannot use same name of constant due to logic.

On optimization will keil take different memory of each or same memory.

I am using three setting of optimization:
1. O0
2. Cross + microlib + O2
3. Cross + Time + O3

Parents
  • Just to be clear, are you saying constant folding can be applied to constant variables, in addition to constant literals?

    For example:

    void func(void)
    {
       int a = 5;
       int b = a * 3;
       //...
    }
    

    I would expect the optimizer to do the math and assign b = 15;

    Where as:

    const int a = 5;
    
    void func(void)
    {
       int b = a * 3;
       //...
    }
    

    I would expect the expression "a * 3" to be evaluated at runtime.

    The wikipedia article doesn't mention the second example, with constant variables.

Reply
  • Just to be clear, are you saying constant folding can be applied to constant variables, in addition to constant literals?

    For example:

    void func(void)
    {
       int a = 5;
       int b = a * 3;
       //...
    }
    

    I would expect the optimizer to do the math and assign b = 15;

    Where as:

    const int a = 5;
    
    void func(void)
    {
       int b = a * 3;
       //...
    }
    

    I would expect the expression "a * 3" to be evaluated at runtime.

    The wikipedia article doesn't mention the second example, with constant variables.

Children
More questions in this forum