Bug List: (This bug is not in your last search results)   Show last search results      Search page      Enter new bug
Bug#: 29560
Product:  
Component:  
Status: NEW
Resolution:
Assigned To: Not yet assigned to anyone <unassigned@gcc.gnu.org>
Host:
Reported against  
Priority:  
Severity:  
Target Milestone:  
 
 
Target:
Reporter: r.leitgeb@x-pin.com <r.leitgeb@x-pin.com>
Add CC:
CC:
Remove selected CCs
Build:
URL:
Summary:
Keywords:
Known to work:
Known to fail:

Attachment Description Type Created Size Actions
Create a New Attachment (proposed patch, testcase, etc.) View All

Bug 29560 depends on: Show dependency tree
Show dependency graph
Bug 29560 blocks:

Additional Comments:





Mark bug as waiting for feedback
Mark bug as suspended




View Bug Activity   |   Format For Printing   |   Clone This Bug


Description:   Last confirmed: 2009-08-24 17:18 Opened: 2006-10-23 11:39
        For setting individual bits of a register, the construct
        unsigned char BitLocation = Whatever;
        REG |= (1<<BitLocation); 
        is commonly used. avr-gcc fails to realize that the target register
        is only 8 bits wide and performs an unnecessary 16-Bit shift of 1.
        Even if this code snippet is replaced by the following:
        unsigned char BitLocation = Whatever;
        const unsigned char one = 1;
        REG |= (one << BitLocation);
        The inefficient code will be generated, even with the -Os flag.
        I suppose this is because the << operator is not defined for 8-bit
        wide data types.

Environment:
System: Darwin variable.local 7.9.0 Darwin Kernel Version 7.9.0: Wed Mar 30
20:11:17 PST 2005; root:xnu/xnu-517.12.7.obj~1/RELEASE_PPC Power Macintosh
powerpc



host: powerpc-apple-darwin7.9.0
build: powerpc-apple-darwin7.9.0
target: avr-unknown-none
configured with: ../configure --target=avr --prefix=/usr/local/atmel
--enable-languages=c --disable-libssp --enable-__cxa_atexit
--enable-clocale=gnu --disable-nls : (reconfigured) ../configure --target=avr
--prefix=/usr/local/avr --enable-languages=c --disable-libssp
--enable-__cxa_atexit --enable-clocale=gnu --disable-nls

How-To-Repeat:
        Use the following test program, compile with -Os -S to see assembler 
        code generated by avr-gcc:
        #include <avr/io.h>

        void setpin(unsigned char pin, unsigned char val) {
                if (val == 1) PORTC |= (1<<pin);
                else PORTC &= ~(1<<pin);
        }

        void setpin1(unsigned char pin, unsigned char val) {
                const unsigned char one = 1;
                if (val == 1) PORTC |= (one<<pin);
                else PORTC &= ~(one<<pin);
        }

        int main(void) {
                setpin(3, 1);
                setpin1(3, 1);
                return 0;
        }

------- Comment #1 From r.leitgeb@x-pin.com 2006-10-23 11:39 -------
Fix:
        Define shift operators for 8 bit wide data types, use if possible.
        I'm not a compiler expert but I know that the multiply operator
        detects the data types involved, so it should be doable with the
        shift operators as well.

------- Comment #2 From r.leitgeb@x-pin.com 2006-10-24 07:31 -------
Here's an excerpt of the assembly code obtained with -Os -S
with some comments from me:

----------------------------------------------------
setpin:
/* prologue: frame size=0 */
/* prologue end (size=0) */
        mov r20,r24
        clr r21
        cpi r22,lo8(1)    <--- The compare is done byte wide
        brne .L2
        in r18,53-0x20
        ldi r24,lo8(1)    <--- Here we load 1 sixteen bit wide
        ldi r25,hi8(1)
        rjmp 2f
1:      lsl r24           <--- Here we shift 1 sixteen bit wide
        rol r25
2:      dec r20
        brpl 1b
        or r18,r24        <--- r25 is not used. Why would we compute it ?
        rjmp .L6
.L2:
        in r18,53-0x20
----------------------------------------------------

Cheers

Rudi

------- Comment #3 From Eric Weddington 2009-08-24 17:18 -------
Confirmed on 4.3.2.

Bug List: (This bug is not in your last search results)   Show last search results      Search page      Enter new bug