This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Compiler optimisation

Hello,

1.when i use printf with no var_args then the compiler should call puts instead of printf.

ex1:


#include <REGX51.H>
#include <stdio.h>

void main(void)
{
        printf("This must call puts instead of printf");
}


Program Size: data=30.1 xdata=0 code=1103

ex2:


#include <REGX51.H>
#include <stdio.h>

void main(void)
{
        puts("This must call puts instead of printf");
}


Program Size: data=9.0 xdata=0 code=168

The above code links the printf function from the library which is huge(produces 1103 bytes).But the compiler can use puts when there is no var_args given which is much smaller than printf(produces 168 bytes).

2.The Compiler must find and remove the duplicate constant strings

ex3:


#include <REGX51.H>
#include <stdio.h>

void main(void)
{
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
        puts("This string gets duplicated as many time as i use it");
}


Program Size: data=9.0 xdata=0 code=334

ex4:


#include <REGX51.H>
#include <stdio.h>

void main(void)
{
        puts("This string gets duplicated as many time as i use it");
}


Program Size: data=9.0 xdata=0 code=183

3.Bit Test instructions are not used when i actually test for the bit

ex5:


#include <REGX51.H>
#include <stdio.h>


void main(void)
{
        if(P0^1)
        {
                P1 = 10;
        }

}


ASSEMBLY LISTING OF GENERATED OBJECT CODE

             ; FUNCTION main (BEGIN)
                                           ; SOURCE LINE # 6
                                           ; SOURCE LINE # 7
                                           ; SOURCE LINE # 8
0000 E580              MOV     A,P0
0002 6401              XRL     A,#01H
0004 6003              JZ      ?C0002
                                           ; SOURCE LINE # 9
                                           ; SOURCE LINE # 10
0006 75900A            MOV     P1,#0AH
                                           ; SOURCE LINE # 11
                                           ; SOURCE LINE # 13
0009         ?C0002:
0009 22                RET
             ; FUNCTION main (END)

In the above assembly output it should have used a single instruction JNB instead of three MOV,XRL and JZ.This is very basic anybody would object the assembly code produced.

I have not used the compiler much.But the compiler needs a look by the programmers at keil.

The above programs were all compiled with compiler optimisation level set to 9 & favour speed.

About 5 years back i compiled a c51 source code using keil.
Now i recompiled the same source code with the latest compiler from keil and compared the two output .hex files.
Unfortunately it produced exactly the same output.Here i was expecting some code and data size reduction as the compiler must be capable of optimising more.

It seems there was no improvement on the compiler side.

It is not a complaint but in the interest of improving the compiler.

regards,

S.Sheik mohamed

Parents
  • Not at all.

    Who are relying on a bit test operation being implemented as a byte read or just a single bit read?

    Who are relying on a bit set being implemented as a byte read followed by a byte write?

    The Keil code is most definitely not relying on that. It is doing what you ask it - a byte access when the source specifies a byte access, and a bit access when the code specifies a bit access. It leaves it up to you to decide if you want a bit instruction or a byte instruction.

    In the case I mentioned earlier - some chip manufacturer implementing a pin change interrupt, with acknowledge of the pin interrupts by reading specific bits, such a feature would obviously have to be documented.

    But a chip manufacturer who "just" implements a 8051 core functionality would not have any real reason to document how the bit operations are implemented.

    It's just when you combine two things - a peripherial device that behaves differently for a bit read or a byte read, with a core that does not implement the bit read as a byte read - that things gets interesting.

    Without a peripherial that behaves differently, the bit operations are opaque black boxes. You can't know if they do bit or byte accesses and there is no reason to document what they do. But having a soft core and combining it with own SFR devices have the potential for surprises if the compiler manufacturer decides to be "clever".

Reply
  • Not at all.

    Who are relying on a bit test operation being implemented as a byte read or just a single bit read?

    Who are relying on a bit set being implemented as a byte read followed by a byte write?

    The Keil code is most definitely not relying on that. It is doing what you ask it - a byte access when the source specifies a byte access, and a bit access when the code specifies a bit access. It leaves it up to you to decide if you want a bit instruction or a byte instruction.

    In the case I mentioned earlier - some chip manufacturer implementing a pin change interrupt, with acknowledge of the pin interrupts by reading specific bits, such a feature would obviously have to be documented.

    But a chip manufacturer who "just" implements a 8051 core functionality would not have any real reason to document how the bit operations are implemented.

    It's just when you combine two things - a peripherial device that behaves differently for a bit read or a byte read, with a core that does not implement the bit read as a byte read - that things gets interesting.

    Without a peripherial that behaves differently, the bit operations are opaque black boxes. You can't know if they do bit or byte accesses and there is no reason to document what they do. But having a soft core and combining it with own SFR devices have the potential for surprises if the compiler manufacturer decides to be "clever".

Children
  • Well Keil support multiple data pointers so that processors that have them can have compiled code that uses them.

    The programmer can choose to use (or ignore) the possible optimisation opportunity.

    So theoretically, the same thing could be done with bit access.

    It's a simple matter-of-fact.

    Keil already have (by necessity) a number of extensions for the '51, so adding another wouldn't really be so naughty.

    As to whether it would really be worth it is another matter.

    But there is no doubt that it could be done.