XC8アセンブラ理解/最適化レベルの変更
https://gyazo.com/b14edd7a8ae5a4ffc37abeb357d07f94
プロジェクトのプロパティを開き、Conf > XC8 Global Options > XC8 Compiler の画面の Option categories で「Optimizations」を選ぶ
Optimization level から好きな最適化レベルを設定する
O3に設定しても「::: advisory: (2051) the current license does not permit the selected optimization level, using optimization level 2」というメッセージが出て、O2でコンパイルされる模様。
O2でも、メモリサイズと動作速度のトレードオフがない最適化はほとんど全部実行されるらしい。
XC8の最適化レベルの概要
MPLABのプロジェクト設定で表示される説明
0 - Do not optimize. The compiler’s goal is to reduce the cost of compilation and to make debugging produce the expected results.
1 - Optimize. Optimizing compilation takes somewhat longer, and a lot more host memory for a large function. The compiler tries to reduce code size and execution time.
2 - Optimize even more. The compiler performs nearly all supported optimizations that do not involve a space-speed trade-off.
3 - Optimize yet more favoring speed (superset of O2).
s - Optimize yet more favoring size (superset of O2).
無料版でも有効な最適化
O0 - Ensures that your code is in its pristine state
O1 - Invokes all optimizations that won't affect debugging
O2 - Invokes a balanced set of speed and size optimizations
PROライセンスで有効化される最適化
Os - Gives maximum code size reductions
O3 - Gives the best speed optimizations
mpa (Procedural Abstraction) - Reduces code size even further
最適化レベルを変えるとコードがどう変わるか
O3
Data Used: 5
Program Used: 39
code:xc8compiller-test.X.production.lst
1690 07E9 _main:
1691
1692 ;main.c: 14: LATA = div_uint8_value(LATA);
1693
1694 ;incstack = 0
1696 07E9 0816 movf 22,w ;volatile
1697 07EA 3187 27DD 3187 fcall _div_uint8_value
1698 07ED 0096 movwf 22 ;volatile
1699
1700 ;main.c: 15: LATB = add_uint8_values(2, 9);
1701 07EE 3009 movlw 9
1702 07EF 00F2 movwf ??_main
1703 07F0 0872 movf ??_main,w
1704 07F1 00F0 movwf add_uint8_values@b
1705 07F2 3002 movlw 2
1706 07F3 3187 27E0 3187 fcall _add_uint8_values
1707 07F6 0097 movwf 23 ;volatile
1708
1709 ;main.c: 16: LATC = get_uint16_value() >> 8;
1710 07F7 3187 27E4 fcall _get_uint16_value
1711 07F9 0871 movf ?_get_uint16_value+1,w
1712 07FA 0098 movwf 24 ;volatile
1713 07FB 3180 2802 ljmp start
1714 07FD __end_of_main:
O0
Data Used: 5
Program Used: 44
code:xc8compiller-test.X.production.lst
1690 07E4 _main:
1691 07E4 l658:
1692 ;incstack = 0
1694
1695
1696 ;main.c: 14: LATA = div_uint8_value(LATA);
1697 07E4 0140 movlb 0 ; select bank0
1698 07E5 0816 movf 22,w ;volatile
1699 07E6 3187 27D8 3187 fcall _div_uint8_value
1700 07E9 0140 movlb 0 ; select bank0
1701 07EA 0096 movwf 22 ;volatile
1702
1703 ;main.c: 15: LATB = add_uint8_values(2, 9);
1704 07EB 3009 movlw 9
1705 07EC 00F2 movwf ??_main
1706 07ED 0872 movf ??_main,w
1707 07EE 00F0 movwf add_uint8_values@b
1708 07EF 3002 movlw 2
1709 07F0 3187 27DB 3187 fcall _add_uint8_values
1710 07F3 0140 movlb 0 ; select bank0
1711 07F4 0097 movwf 23 ;volatile
1712 07F5 l660:
1713
1714 ;main.c: 16: LATC = get_uint16_value() >> 8;
1715 07F5 3187 27DF 3187 fcall _get_uint16_value
1716 07F8 0871 movf ?_get_uint16_value+1,w
1717 07F9 0140 movlb 0 ; select bank0
1718 07FA 0098 movwf 24 ;volatile
1719 07FB l19:
1720 07FB 3180 2802 ljmp start
1721 07FD __end_of_main:
実質的な差はmovlb 0 ; select bank0の有無