[H8-ML(2024)] Re: 右辺値のvolatile宣言(GCC で検証)
From: Keiji Matsuzono <k-zono@xxxxxxxxxxxxxxxx>
Date: 2002年06月28日(金)05時57分35秒
まつぞの@自宅から です

代入文の右辺にだけ現れる変数に volatile 指定をしても
最適化(コードが削除される)ことがありうる、ということだったので、
GCC で試してみました。

結論を先にいうと、GCC だと最適化されないようです。H8 用と AVR 用で
試してみました。

コンパイラ: GCC 3.0.3(for H8), 3.04(for AVR)   on Debian(Linux)
# 自宅からなので前回の報告とは OS が異なります

テストしたソース:
-----------------------------------------------------------------------
// io_read.c
// volatile 指定で最適化がされる場合があるかどうかテストする

#ifdef VOLATILE_DEFINED
volatile int count;
#else
int count;
#endif

int main(){
        int a;

        a = count;
        a = count;
        a = count;

        return 0;
}
-----------------------------------------------------------------------

(a-1) H8, volatile 指定あり、最適化レベル9(最大)
(a-2) H8, volatile 指定なし、最適化レベル2
(b-1) AVR, volatile 指定あり、最適化レベル9(最大)
(b-2) AVR, volatile 指定なし、最適化レベル2

(a-1) H8, volatile 指定あり、最適化レベル9(最大)
// 右辺値ですが、volatile 指定をしてあると最適化されません
 $ h8300-hms-gcc -o H8_volatile_O9.S -S -O9 -DVOLATILE_DEFINED io_read.c
 ---------- H8_volatile_O9.S ------------------------------------------
;       GCC For the Hitachi H8/300
;       By Hitachi America Ltd and Cygnus Support
; -O9


        .file   "io_read.c"
        .comm _count,2
        .section .text
        .align 1
        .global _main
_main:
        push    r6
        mov.w   r7,r6
        mov.w   @_count,r2
        mov.w   @_count,r2
        mov.w   @_count,r2
        sub.w   r0,r0
        pop     r6
        rts
        .end
        .ident
"GCC: (GNU) 3.0.3"

-----------------------------------------------------------------------

(a-2) H8, volatile 指定なし、最適化レベル2
// volatile 指定をしないと最適化レベル2でも最適化されてしまいます
// まあ、左辺値でもそうだったのであたりまえですが(^^;
 $ h8300-hms-gcc -o H8_no_volatile_O2.S -S -O2  io_read.c
---------- H8_no_volatile_O2.S ------------------------------------------
;       GCC For the Hitachi H8/300
;       By Hitachi America Ltd and Cygnus Support
; -O2


        .file   "io_read.c"
        .section .text
        .align 1
        .global _main
_main:
        push    r6
        mov.w   r7,r6
        sub.w   r0,r0
        pop     r6
        rts
        .comm _count,2
        .end
        .ident
"GCC: (GNU) 3.0.3"

-----------------------------------------------------------------------

(b-1) AVR, volatile 指定あり、最適化レベル9(最大)
// H8 と同様、右辺値ですが、volatile 指定をしてあると最適化されません
 $ avr-gcc -o AVR_volatile_O9.S -S -O9 -DVOLATILE_DEFINED io_read.c
---------- AVR_volatile_O9.S ------------------------------------------
        .file   "io_read.c"
        .arch avr2
__SREG__ = 0x3f
__SP_H__ = 0x3e
__SP_L__ = 0x3d
__tmp_reg__ = 0
__zero_reg__ = 1
_PC_ = 2
        .text
.global main
        .type   main,@function
main:
/* prologue: frame size=0 */
        ldi r28,lo8(__stack - 0)
        ldi r29,hi8(__stack - 0)
        out __SP_H__,r29
        out __SP_L__,r28
/* prologue end (size=4) */
        lds r24,count
        lds r25,(count)+1
        lds r24,count
        lds r25,(count)+1
        lds r24,count
        lds r25,(count)+1
        ldi r24,lo8(0)
        ldi r25,hi8(0)
/* epilogue: frame size=0 */
__stop_progIi__:
        rjmp __stop_progIi__
/* epilogue end (size=1) */
/* function main size 19 (14) */
.Lfe1:
        .size   main,.Lfe1-main
        .comm count,2,1
/* File io_read.c: code   19 = 0x0013 (  14), prologues   4, epilogues   1 */

-----------------------------------------------------------------------

(b-2) AVR, volatile 指定なし、最適化レベル2
// AVR でも volatile 指定なしだと最適化されます
// AVR で最適化された場合、こんなコードになります
 $ avr-gcc -o AVR_no_volatile_O2.S -O2 -S io_read.c 
--------  AVR_no_volatile_O2.S --------------------------------------
        .file   "io_read.c"
        .arch avr2
__SREG__ = 0x3f
__SP_H__ = 0x3e
__SP_L__ = 0x3d
__tmp_reg__ = 0
__zero_reg__ = 1
_PC_ = 2
        .text
.global main
        .type   main,@function
main:
/* prologue: frame size=0 */
        ldi r28,lo8(__stack - 0)
        ldi r29,hi8(__stack - 0)
        out __SP_H__,r29
        out __SP_L__,r28
/* prologue end (size=4) */
        ldi r24,lo8(0)
        ldi r25,hi8(0)
/* epilogue: frame size=0 */
__stop_progIi__:
        rjmp __stop_progIi__
/* epilogue end (size=1) */
/* function main size 7 (2) */
.Lfe1:
        .size   main,.Lfe1-main
        .comm count,2,1
/* File ../io_read.c: code    7 = 0x0007 (   2), prologues   4, epilogues   1 */
-----------------------------------------------------------------------



スレッド概略
[1992(R)](起点)
 └[2020(U)]
   └[表示中]
     └[2026(1)]


投稿順に移動
[←前の記事へ(P)]
[→次の記事へ(N)]


リスト表示へ
[このスレッド(T)]
[本記事の前後(L)]