〔リスト1〕関数の境界揃えを行う例(test207.c)
|
#include
/*
*関数の境界揃え
*/
int test1();
int test2();
int test21();
int main(int argc, char* argv[])
{
printf("%d\n",test1());
printf("%d\n",test2());
return 0;
}
int test1()
{
return 100;
}
int test2()
{
printf("%d\n",test21());
return 200;
}
int test21()
{
return 300;
}
|
|
〔リスト2〕32バイト境界そろえにするオプションを付けて生成されたアセンブラ・ソース(test207a.s)
|
.file "test207.c"
.section .rodata
.LC0:
.string "%d\n"
.text
.p2align 5,,31
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
call test1
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
call test2
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.p2align 5,,31
.globl test1
.type test1, @function
test1:
pushl %ebp
movl %esp, %ebp
movl $100, %eax
popl %ebp
ret
.size test1, .-test1
.p2align 5,,31
.globl test2
.type test2, @function
test2:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
call test21
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $200, %eax
leave
ret
.size test2, .-test2
.p2align 5,,31
.globl test21
.type test21, @function
test21:
pushl %ebp
movl %esp, %ebp
movl $300, %eax
popl %ebp
ret
.size test21, .-test21
.ident "GCC: (GNU) 3.3"
|
|
〔リスト3〕64バイト境界そろえにするオプションを付けて生成されたアセンブラ・ソース(test207b.s)
|
.file "test207.c"
.section .rodata
.LC0:
.string "%d\n"
.text
.p2align 6,,63
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
call test1
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
call test2
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.p2align 6,,63
.globl test1
.type test1, @function
test1:
pushl %ebp
movl %esp, %ebp
movl $100, %eax
popl %ebp
ret
.size test1, .-test1
.p2align 6,,63
.globl test2
.type test2, @function
test2:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
call test21
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $200, %eax
leave
ret
.size test2, .-test2
.p2align 6,,63
.globl test21
.type test21, @function
test21:
pushl %ebp
movl %esp, %ebp
movl $300, %eax
popl %ebp
ret
.size test21, .-test21
.ident "GCC: (GNU) 3.3"
|
|
〔リスト4〕オプションなしで生成されたアセンブラ・ソース(test207c.s)
|
.file "test207.c"
.section .rodata
.LC0:
.string "%d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
call test1
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
call test2
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.globl test1
.type test1, @function
test1:
pushl %ebp
movl %esp, %ebp
movl $100, %eax
popl %ebp
ret
.size test1, .-test1
.globl test2
.type test2, @function
test2:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
call test21
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $200, %eax
leave
ret
.size test2, .-test2
.globl test21
.type test21, @function
test21:
pushl %ebp
movl %esp, %ebp
movl $300, %eax
popl %ebp
ret
.size test21, .-test21
.ident "GCC: (GNU) 3.3"
|
|
〔リスト5〕境界そろえ値を省略したオプションを付けて生成されたアセンブラ・ソース(test207.s)
|
.file "test207.c"
.section .rodata
.LC0:
.string "%d\n"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
call test1
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
call test2
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.p2align 4,,15
.globl test1
.type test1, @function
test1:
pushl %ebp
movl %esp, %ebp
movl $100, %eax
popl %ebp
ret
.size test1, .-test1
.p2align 4,,15
.globl test2
.type test2, @function
test2:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
call test21
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $200, %eax
leave
ret
.size test2, .-test2
.p2align 4,,15
.globl test21
.type test21, @function
test21:
pushl %ebp
movl %esp, %ebp
movl $300, %eax
popl %ebp
ret
.size test21, .-test21
.ident "GCC: (GNU) 3.3"
|
|
〔リスト6〕分岐が起こる経路を推測して行われる最適化例(test208.c)
|
/*
*分岐が起こる経路を推測して行われる最適化
*/
#include
#include
#include
int main(int argc, char* argv[])
{
int Wday;
int Wday_save;
int dummy;
enum Days
{
Sunday = 0,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
Wday = 1;
switch (Wday)
{
case Sunday:
Wday_save = Sunday;
printf("%s\n","Sunday");
dummy++;
dummy++;
dummy++;
dummy++;
dummy = 0;
break;
case Monday:
Wday_save = Monday;
printf("%s\n","Monday");
break;
case Tuesday:
Wday_save = Tuesday;
printf("%s\n","Tuesday");
break;
case Wednesday:
Wday_save = Wednesday;
printf("%s\n","Wednesday");
break;
case Thursday:
Wday_save = Thursday;
printf("%s\n","Thursday");
break;
case Saturday:
Wday_save = Saturday;
printf("%s\n","Saturday");
dummy++;
dummy++;
dummy++;
dummy++;
dummy = 0;
break;
default:
Wday_save = Saturday;
}
printf("%d\n",Wday_save);
return 0;
|
|
〔リスト7〕-fprofile-arcsを付加して生成されたアセンブラ・ソース(test208a.s)
|
.file "test208.c"
.section .rodata
.LC0:
.string "Sunday"
.LC1:
.string "%s\n"
.LC2:
.string "Monday"
.LC3:
.string "Tuesday"
.LC4:
.string "Wednesday"
.LC5:
.string "Thursday"
.LC6:
.string "Saturday"
.LC7:
.string "%d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $5, -4(%ebp)
cmpl $6, -4(%ebp)
ja .L9
movl -4(%ebp), %eax
sall $2, %eax
movl .L10(%eax), %eax
jmp *%eax
.section .rodata
.align 4
.align 4
.L10:
.long .L3
.long .L4
.long .L5
.long .L6
.long .L7
.long .L9
.long .L8
.text
.L3:
movl $0, -8(%ebp)
movl $.LC0, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L4:
movl $1, -8(%ebp)
movl $.LC2, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L5:
movl $2, -8(%ebp)
movl $.LC3, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L6:
movl $3, -8(%ebp)
movl $.LC4, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L7:
movl $4, -8(%ebp)
movl $.LC5, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L8:
movl $6, -8(%ebp)
movl $.LC6, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L9:
movl $6, -8(%ebp)
.L2:
movl -8(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC7, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 3.3"
|
|
〔リスト8〕-fbranch-probabilitiesを付加して生成されたアセンブラ・ソース(test208b.s)
|
.file "test208.c"
.section .rodata
.LC0:
.string "Sunday"
.LC1:
.string "%s\n"
.LC2:
.string "Monday"
.LC3:
.string "Tuesday"
.LC4:
.string "Wednesday"
.LC5:
.string "Thursday"
.LC6:
.string "Saturday"
.LC7:
.string "%d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $5, -4(%ebp)
cmpl $6, -4(%ebp)
ja .L9
movl -4(%ebp), %eax
sall $2, %eax
movl .L10(%eax), %eax
jmp *%eax
.section .rodata
.align 4
.align 4
.L10:
.long .L3
.long .L4
.long .L5
.long .L6
.long .L7
.long .L9
.long .L8
.text
.L3:
movl $0, -8(%ebp)
movl $.LC0, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L4:
movl $1, -8(%ebp)
movl $.LC2, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L5:
movl $2, -8(%ebp)
movl $.LC3, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L6:
movl $3, -8(%ebp)
movl $.LC4, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L7:
movl $4, -8(%ebp)
movl $.LC5, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L8:
movl $6, -8(%ebp)
movl $.LC6, 4(%esp)
movl $.LC1, (%esp)
call printf
jmp .L2
.L9:
movl $6, -8(%ebp)
.L2:
movl -8(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC7, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 3.3"
|
|
〔リスト9〕不必要なグローバル・データをbss領域に配置する例(test209.c)
|
//不要なグローバルデータをbss領域に配置
#include
#include
#include
int test1();
int test2();
int test3();
long testdata;
int main(int argc, char* argv[])
{
printf("%d\n",test1());
printf("%d\n",test2());
return 0;
}
int test1()
{
return 100;
}
int test2()
{
return 200;
}
int test3()
{
return 300;
}
|
|
〔リスト10〕オプションを付加しないで生成したアセンブラ・ソース(test209a.s)
|
.file "test209.c"
.section .rodata
.LC0:
.string "%d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
call test1
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
call test2
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.globl test1
.type test1, @function
test1:
pushl %ebp
movl %esp, %ebp
movl $100, %eax
popl %ebp
ret
.size test1, .-test1
.globl test2
.type test2, @function
test2:
pushl %ebp
movl %esp, %ebp
movl $200, %eax
popl %ebp
ret
.size test2, .-test2
.globl test3
.type test3, @function
test3:
pushl %ebp
movl %esp, %ebp
movl $300, %eax
popl %ebp
ret
.size test3, .-test3
.comm testdata,4,4
.ident "GCC: (GNU) 3.3"
|
|
〔リスト11〕オプションを付加して生成したアセンブラ・ソース(test209b.s)
|
.file "test209.c"
.section .rodata
.LC0:
.string "%d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
call test1
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
call test2
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.globl test1
.type test1, @function
test1:
pushl %ebp
movl %esp, %ebp
movl $100, %eax
popl %ebp
ret
.size test1, .-test1
.globl test2
.type test2, @function
test2:
pushl %ebp
movl %esp, %ebp
movl $200, %eax
popl %ebp
ret
.size test2, .-test2
.globl test3
.type test3, @function
test3:
pushl %ebp
movl %esp, %ebp
movl $300, %eax
popl %ebp
ret
.size test3, .-test3
.globl testdata
.section .bss.testdata,"aw",@nobits
.align 4
.type testdata, @object
.size testdata, 4
testdata:
.zero 4
.ident "GCC: (GNU) 3.3"
|
|
〔リスト12〕不要な関数をマッピングしない例(test210.c)
|
//不要な関数をマッピングしない
#include
#include
#include
int test1();
int test2();
int test3();
int main(int argc, char* argv[])
{
printf("%d\n",test1());
printf("%d\n",test2());
return 0;
}
int test1()
{
return 100;
}
int test2()
{
return 200;
}
int test3()
{
return 300;
}
|
|
〔リスト13〕オプションを付加しないで生成したアセンブラ・ソース(test210a.s)
|
.file "test210.c"
.section .rodata
.LC0:
.string "%d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
call test1
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
call test2
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.globl test1
.type test1, @function
test1:
pushl %ebp
movl %esp, %ebp
movl $100, %eax
popl %ebp
ret
.size test1, .-test1
.globl test2
.type test2, @function
test2:
pushl %ebp
movl %esp, %ebp
movl $200, %eax
popl %ebp
ret
.size test2, .-test2
.globl test3
.type test3, @function
test3:
pushl %ebp
movl %esp, %ebp
movl $300, %eax
popl %ebp
ret
.size test3, .-test3
.ident "GCC: (GNU) 3.3"
|
|
〔リスト14〕オプションを付加して生成したアセンブラ・ソース(test210b.s)
|
.file "test210.c"
.section .rodata
.LC0:
.string "%d\n"
.section .text.main,"ax",@progbits
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
call test1
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
call test2
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.section .text.test1,"ax",@progbits
.globl test1
.type test1, @function
test1:
pushl %ebp
movl %esp, %ebp
movl $100, %eax
popl %ebp
ret
.size test1, .-test1
.section .text.test2,"ax",@progbits
.globl test2
.type test2, @function
test2:
pushl %ebp
movl %esp, %ebp
movl $200, %eax
popl %ebp
ret
.size test2, .-test2
.section .text.test3,"ax",@progbits
.globl test3
.type test3, @function
test3:
pushl %ebp
movl %esp, %ebp
movl $300, %eax
popl %ebp
ret
.size test3, .-test3
.ident "GCC: (GNU) 3.3"
|
|
〔リスト15〕不必要なヌル・ポインタ・チェックを削除する例(test211.c)
|
//不要なヌル・ポインタ・チェックを削除する例
#include
int main()
{
int *p = (int *) NULL;
if (p)
{
printf ("p is not NULL\n");
}
else
{
printf ("p is NULL-PO\n");
}
return 0;
}
|
|
〔リスト16〕-Osオプションを付加して生成したアセンブラ・ソース(test211_Os.s)
|
.file "test211.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC1:
.string "p is NULL-PO"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
pushl $.LC1
call puts
popl %eax
xorl %eax, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 3.3"
|
|
〔リスト17〕-O1オプションを付加して生成したアセンブラ・ソース(test211_O1.s)
|
.file "test211.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC1:
.string "p is NULL-PO"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $.LC1, (%esp)
call puts
movl $0, %eax
movl %ebp, %esp
popl %ebp
ret
.size main, .-main
.ident "GCC: (GNU) 3.3"
|
|
〔リスト18〕-O2オプションを付加して生成したアセンブラ・ソース(test211_O2.s)
|
.file "test211.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC1:
.string "p is NULL-PO"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $.LC1, (%esp)
call puts
movl %ebp, %esp
xorl %eax, %eax
popl %ebp
ret
.size main, .-main
.ident "GCC: (GNU) 3.3"
|
|
〔リスト19〕-O3オプションを付加して生成したアセンブラ・ソース(test211_O3.s)
|
.file "test211.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC1:
.string "p is NULL-PO"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $.LC1, (%esp)
call puts
movl %ebp, %esp
xorl %eax, %eax
popl %ebp
ret
.size main, .-main
.ident "GCC: (GNU) 3.3"
|
|
〔リスト20〕-O0オプションを付加して生成したアセンブラ・ソース(test211_O0.s)
|
.file "test211.c"
.section .rodata
.LC0:
.string "p is not NULL\n"
.LC1:
.string "p is NULL-PO\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $0, -4(%ebp)
cmpl $0, -4(%ebp)
je .L2
movl $.LC0, (%esp)
call printf
jmp .L3
.L2:
movl $.LC1, (%esp)
call printf
.L3:
movl $0, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 3.3"
|
|
〔リスト21〕浮動小数点演算の精度が変わってしまうよな最適化を行う例(test212.c)
|
//浮動小数点演算の精度が変わってしまうような最適化をする例
#include
#include
const float f1 = 3.1212312312312312f;
const float f2 = 6.5432165432165432f;
float func(float a)
{
return a * f1 / f2;
|
|
〔リスト22〕生成したアセンブラ・ソース(test211.s)
|
.file "test212.c"
.globl f1
.section .rodata
.align 4
.type f1, @object
.size f1, 4
f1:
.long 1078444609
.globl f2
.align 4
.type f2, @object
.size f2, 4
f2:
.long 1087463944
.section .rodata.cst4,"aM",@progbits,4
.align 4
.LC96:
.long 1056193456
.text
.p2align 4,,15
.globl func
.type func, @function
func:
flds .LC96
fmuls 4(%esp)
ret
.size func, .-func
.ident "GCC: (GNU) 3.3"
|
|
〔リスト23〕最適化して生成したアセンブラ・ソース(test211a.s)
|
. .file "test212.c"
.globl f1
.section .rodata
.align 4
.type f1, @object
.size f1, 4
f1:
.long 1078444609
.globl f2
.align 4
.type f2, @object
.size f2, 4
f2:
.long 1087463944
.text
.globl func
.type func, @function
func:
pushl %ebp
movl %esp, %ebp
flds 8(%ebp)
fmuls f1
fdivs f2
popl %ebp
ret
.size func, .-func
.ident "GCC: (GNU) 3.3"
|
|
 記事内インデックス 連載インデックスはこちら Interfaceのトップ |
|
|
Copyright 2004 岸 哲夫
Copyright 1997-2004 CQ Publishing Co.,Ltd.
|