第9章代码优化 91代码优化概述 92局部优化 93控制流程分析和循环 94数据流分析举例
第9章 代码优化 9.1 代码优化概述 9.2 局部优化 9.3 控制流程分析和循环 9.4 数据流分析举例
9.1 何谓代码优化:为获得较好性能的代码而进行的变换 宗旨:等价变换 权衡(意图,结果) 阶段: Source front (LR) code—( target code end generator code 用户 中间代码优化 目标代码优化 代码优化的工作基础 数据流分析和控制流分析
9.1 何谓代码优化: 宗旨: 为获得较好性能的代码而进行的变换 等价变换 权衡 ( 意图,结果) 阶段: (source front (I.R) code ( target code) end generator code) 用户 中间代码优化 目标代码优化 代码优化的工作基础: 数据流分析和控制流分析
Diagram of the compilation process Source if(a>=b+1) program Lexical analysis Front end Syntax analysis (analysis) emantic analvsis Intermediate code gen Intermediate tl b+1 t2 representation a t1 if t2 goto LO IR optimization Back end (svnthesIs Object code gen Object code optimization Target 1d[8fp-16],s10 add810,1,10 program
直觉:哪个程序快?优化编译做到 样 int arr[10000] int arr[l0000]i void binkyo i void winky) i int li register int *pi for (l=0; i for (p=arr p< 10000;1++) arr+10000; arr l p++)
直觉:哪个程序快?—优化编译做到 一样. int arr[10000]; void Binky() { int i; for (i=0; i < 10000; i++) arr[i] = 1; } int arr[10000]; void Winky() { register int *p; for (p = arr; p < arr + 10000; p++) *p = 1; }
优化技术简介一常数合并 10*5+6-b; tmp =10i tmp1 =5 tmp tmpo x tmpl i tmp =6 tmp 4 tmp2+ tmp 3 i tmp tmp 4-bi tmp 5 i tmp0=56;
优化技术简介—常数合并 a = 10 * 5 + 6 - b; _tmp0 = 10 ; _tmp1 = 5 ; _tmp2 = _tmp0 * _tmp1 ; _tmp3 = 6 ; _tmp4 = _tmp2 + _tmp3 ; _tmp5 = _tmp4 – b; a = _tmp5 ; _tmp0 = 56 ;