AVR-GCC no longer generating SBI/CBI instructions?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The sbi() and cbi() intrinsics were moved into the "deprecated" area, and we were told that the compiler would generate sbi and cbi instructions for us when we use |= and &= on I/O registers.

However, with 4.7.1, it doesn't seem to actually generate these instructions. Here is a test program:

#include 
#include 


int main() {
    DDRB |= (1 << 4);
    while (true) {
#if SHOULD_BE_GENERATED
        asm(" sbi 0x18, 4\r\n");
        asm(" cbi 0x18, 4\r\n");
#else
        PORTB |= (1 << 4);
        PORTB &= ~(1 << 4);
#endif
    }
}

If I define SHOULD_BE_GENERATED, the inline assembly works fine and does what I want it to. However, if it's not defined, the compiler generates a slower and bigger load-or-store-and-store sequence. This seems like a bug to me!

Compile command:

avr-gcc -mmcu=attiny85 -Wall -Werror -Wno-switch -Os -O3 -mcall-prologues -std=gnu++0x -flto -ffunction-sections -c avr/test85/main.cpp -o bld/avrobj/test85/main.o -MMD -I/usr/lib/avr/include

Link command:

avr-gcc -o bld/avrbin/test85 bld/avrobj/test85/main.o -lavr_attiny85 -Lbld/avrbin -lc -flto -Wl,-flto -mmcu=attiny85

Generated code for main():

00000030 
: 30: a0 e0 ldi r26, 0x00 ; 0 32: b0 e0 ldi r27, 0x00 ; 0 34: ed e1 ldi r30, 0x1D ; 29 36: f0 e0 ldi r31, 0x00 ; 0 38: 20 c0 rjmp .+64 ; 0x7a <__prologue_saves__+0x20> 3a: 80 91 37 00 lds r24, 0x0037 3e: 80 61 ori r24, 0x10 ; 16 40: 80 93 37 00 sts 0x0037, r24 44: 80 91 38 00 lds r24, 0x0038 48: 80 61 ori r24, 0x10 ; 16 4a: 80 93 38 00 sts 0x0038, r24 4e: 80 91 38 00 lds r24, 0x0038 52: 8f 7e andi r24, 0xEF ; 239 54: 80 93 38 00 sts 0x0038, r24 58: f5 cf rjmp .-22 ; 0x44 <__SREG__+0x5>

Curiouser, though, main.o actually contains the right code:

00000000 
: 0: bc 9a sbi 0x17, 4 ; 23 2: c4 9a sbi 0x18, 4 ; 24 4: c4 98 cbi 0x18, 4 ; 24 6: 00 c0 rjmp .+0 ; 0x8 <__zero_reg__+0x7>

Thus, the problem is with -flto! I wonder how this could happen?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I really don't know if this makes a difference or not but I noticed that in the compile command you have -Os followed by -O3.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

with 4.7.1

Since this is on GNU/Linux: Where does your tool chain come from? Did you build it yourself? Or is it a "Bingo build" (the builds that fellow freak "bingo600" supplies)?

Or is it from one of the GNU/Linux distributions repository? (If so, I'd ditch it. According to many posts here they have a history of not getting it right, missing patches etc.)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jwatte wrote:
Thus, the problem is with -flto! I wonder how this could happen?
The problem is your understanding of LTO, what it does and what it does not.

Is DOES NOT optimize on the generated object code you see in .o or on assembler code you see in .s. It optimizes on the code in the lto code in the .gnu.lto sections and calls back the compiler at link time with lto as front end: Just as the compiler can understand C, C++, Fortran, Ada, Go or whatever, it can understand lto.

You effectively compile with -O0.

Thus, everytime you set -flto, you also want to turn on optimizaion -- except you don't want optimized code.

avrfreaks does not support Opera. Profile inactive.

Last Edited: Sat. Jul 14, 2012 - 10:03 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The post by SprinterSB made me curious, so I went over to the 4.7.1 documentation. It seems to me that this piece of text is well worth a read, since it tells you how to use -flto and give a good explanation of the inner workings of it: http://gcc.gnu.org/onlinedocs/gc...

(Slightly OT: Now I need to dig into this "linker plugin" stuff, and see if avr-gcc supports it.. LTO with modules pulled out of a library - interesting!)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
LTO with modules pulled out of a library - interesting!
Yes. You could build AVR-Libc with LTO for example and optimize against or inline code from AVR-Libc.

For example memcpy_P is a library function. WIth LTO it could be inlined, avoiding the overhead of a function call (provided it's not implemented in assembler).

avrfreaks does not support Opera. Profile inactive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So as not to derail the this thread, I've started a new thread for another question about LTO: https://www.avrfreaks.net/index.p... .

Care to have a look over there, SprinterSB?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]