如题所述
ä¸å½±åè¯ä¹çåæä¸ç¼è¯å¨å¯ä»¥ä»»æéæ代ç 顺åºï¼
å¨ä¹±åºæ§è¡ï¼Out-of-Orderï¼çCPUéï¼æºå¨ç çæ§è¡ä¹å¯ä»¥ä¸æç §ä½ å¨âæ±ç¼âå±é¢ä¸çå°ç顺åºæ§è¡ï¼åªè¦ä¸å½±åè¯ä¹ã
æ以说è¿äºä¸é´æ¥éª¤ç顺åºï¼ä½ä¸ºåºå±ç»èå¹³æ¶ä¸éè¦é£ä¹å¨æââå®ä»¬å¤åè·åå§æºç ç顺åºæ¯ä¸ä¸æ ·çã
ç°ä»£ä¼åç¼è¯å¨ä¼åçæè·¯ä¹ä¸æ¯âåºäºä¾èµçä¼åâï¼dependence-based optimizationï¼ãé¢ä¸»å¼ç¨çCSAPPçä¾åï¼
int arith(int x, int y, int z) {
int t1 = x + y;
int t2 = z * 48;
int t3 = t1 & 0xFFFF;
int t4 = t2 * t3;
return t4;
}
æææ¶åè¿ç®çå¼é½æ¯å±é¨æ éåéï¼local scalar variableï¼ï¼è¿æ¯æ便äºç¼è¯å¨ååæçæ åµï¼ææä¾èµé½å¯ä»¥æ¾å¼åæã
ç±äºæ´ä¸ªå½æ°æ²¡æåæ¯ï¼è¿éä¹ä¸éè¦è®¨è®ºæ§å¶ä¾èµï¼control dependenceï¼ï¼åªè¦è®¨è®ºæ°æ®ä¾èµï¼data dependenceï¼å°±å¥½ã
ææ°æ®ä¾èµå¾ç»åºæ¥æ¯ä¸ªDAGï¼è¿éæ£å¥½æ¯æ£µæ ï¼ç¹ä¾äºï¼ï¼
x y z 48
\ / \ /
t1 0xFFFF t2
\ / /
t3 /
\ /
t4
ä¼åå¿ é¡»è¦æ»¡è¶³ç约ææ¯ï¼æ¯ä¸ªèç¹æ±å¼ä¹åï¼å ¶åèç¹ï¼ä¾èµçæ°æ®æºï¼å¿ é¡»è¦å æ±äºå¼ã
æ¾ç¶ï¼t1åt2ä¹é´æ²¡æä¾èµå ³ç³»ï¼å®ä»¬çç¸å¯¹æ±å¼é¡ºåºææ ·éæé½æ²¡å ³ç³»ã
ææ¬æå¾å欢ç书ï¼éé¢è®²çæ¯åç§åºäºä¾èµçä¼åï¼Optimizing Compilers for Modern Architectures - A Dependence-based Approach
以ä¸æ¯ç论é¨åã
================================================================
ä¸é¢æ¥çä¾åã
æ们å¯ä»¥ç¨ä¸ä¸ªå®é ç¼è¯å¨æ¥ççCSAPPçä¾åç¼è¯åºæ¥çç»æï¼
.text
# -- Begin arith
.p2align 4,,15
.globl arith
.type arith, @function
arith:
.p2align 4,,7
/*.L0:*/ /* Block BB[54:2] preds: none, freq: 1.000 */
movl 8(%esp), %edx /* ia32_Load T[139:10] -:1:22 */
addl 4(%esp), %edx /* ia32_Add Iu[141:12] -:2:14 */
movzwl %dx, %edx /* ia32_Conv_I2I Iu[142:13] -:4:15 */
imull 12(%esp), %edx /* ia32_IMul Iu[143:14] -:5:15 */
leal (%edx,%edx,2), %eax /* ia32_Lea Iu[144:15] -:5:15 */
shll $0x4, %eax /* ia32_Shl Iu[146:17] -:5:15 */
ret /* ia32_Return X[152:23] -:6:3 */
.size arith, .-arith
# -- End arith
è¿éç¨çæ¯libFirmãå¯è§å®è·CSAPP书éæ说çæ±ç¼ç顺åºåææä¸åãè¿ä¹æ¯å®å ¨åççã
è¿ä¸ªç¼è¯ç»æç顺åºæ¯ï¼
edx = y;
edx += x;
edx = zeroextend dx; // edx = edx & 0xFFFF
edx *= z;
eax = edx * 3;
eax <<= 4; // eax = eax * 16
ä¹æ¯å®å ¨ç¬¦åä¾èµå ³ç³»ç约æçä¸ç§é¡ºåºã
ä¹æ以ç¨libFirm举ä¾æ¯å 为å®çä¸é´è¡¨ç¤ºï¼Intermediate Representationï¼æ¯ä¸ç§ç¨åºä¾èµå¾ï¼Program Dependence Graphï¼ï¼å¯ä»¥å¾æ¹ä¾¿ççåºæ§å¶ä¸æ°æ®ä¾èµãæCSAPPé£éä¾å对åºçlibFirm IRç»åºæ¥ï¼æ¯è¿ä¸ªæ ·åçï¼
ï¼è¿å¼ å¾è·æåé¢ç»çæ°æ®ä¾èµå¾æ£å¥½æ¯å·¦å³ç¿»è½¬çï¼ä¸è¿ææä¸æ ·ãï¼è¿å¼ å¾è·æåé¢ç»çæ°æ®ä¾èµå¾æ£å¥½æ¯å·¦å³ç¿»è½¬çï¼ä¸è¿ææä¸æ ·ã
Arg 0ã1ã2åå«ä»£è¡¨xãyãzãç½è²æ¹åæ¯æ®éæ°æ®èç¹ï¼é»è²æ¹åæ¯å¸¸éèç¹ï¼èè²æ¹åæ¯å åç¸å ³èç¹ï¼çº¢è²æ¹åæ¯æ§å¶æµèç¹ï¼ç²çº¢è²æ¹åæ¯ç¹æ®çå¼å§/ç»æèç¹ãï¼
æçLLVMçæç代ç ï¼
; ModuleID = '/tmp/webcompile/_16355_0.bc'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-ellcc-linux"
; Function Attrs: nounwind readnone
define i32 @arith(i32 %x, i32 %y, i32 %z) #0 {
entry:
%add = add nsw i32 %y, %x
%mul = mul nsw i32 %z, 48
%and = and i32 %add, 65535
%mul1 = mul nsw i32 %mul, %and
ret i32 %mul1
}
attributes #0 = { nounwind readnone "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.ident = !{!0}
!0 = !{!"ecc 0.1.10 based on clang version 3.7.0 (trunk) (based on LLVM 3.7.0svn)"}
æç»çæçx86æ±ç¼ï¼
.text
.file "/tmp/webcompile/_15964_0.c"
.globl arith
.align 16, 0x90
.type arith,@function
arith: # @arith
# BB#0: # %entry
movl 8(%esp), %eax
addl 4(%esp), %eax
movzwl %ax, %eax
imull 12(%esp), %eax
shll $4, %eax
leal (%eax,%eax,2), %eax
retl
.Ltmp0:
.size arith, .Ltmp0-arith
.ident "ecc 0.1.10 based on clang version 3.7.0 (trunk) (based on LLVM 3.7.0svn)"
.section ".note.GNU-stack","",@progbits
GCC 4.9.2 x86-64ï¼
arith(int, int, int):
leal (%rdx,%rdx,2), %eax
addl %edi, %esi
movzwl %si, %esi
sall $4, %eax
imull %esi, %eax
ret
Zing VM Server Compiler x86-64ï¼
# edi: x
# esi: y
# edx: z
movl %edx, %eax
shll $0x4, %eax
leal (%rsi, %rdi, 1), %ecx
shll $0x5, %edx
addl %edx, $eax
movzwl %ecx, %edx
imull %edx, %eax
å¨ä¹±åºæ§è¡ï¼Out-of-Orderï¼çCPUéï¼æºå¨ç çæ§è¡ä¹å¯ä»¥ä¸æç §ä½ å¨âæ±ç¼âå±é¢ä¸çå°ç顺åºæ§è¡ï¼åªè¦ä¸å½±åè¯ä¹ã
æ以说è¿äºä¸é´æ¥éª¤ç顺åºï¼ä½ä¸ºåºå±ç»èå¹³æ¶ä¸éè¦é£ä¹å¨æââå®ä»¬å¤åè·åå§æºç ç顺åºæ¯ä¸ä¸æ ·çã
ç°ä»£ä¼åç¼è¯å¨ä¼åçæè·¯ä¹ä¸æ¯âåºäºä¾èµçä¼åâï¼dependence-based optimizationï¼ãé¢ä¸»å¼ç¨çCSAPPçä¾åï¼
int arith(int x, int y, int z) {
int t1 = x + y;
int t2 = z * 48;
int t3 = t1 & 0xFFFF;
int t4 = t2 * t3;
return t4;
}
æææ¶åè¿ç®çå¼é½æ¯å±é¨æ éåéï¼local scalar variableï¼ï¼è¿æ¯æ便äºç¼è¯å¨ååæçæ åµï¼ææä¾èµé½å¯ä»¥æ¾å¼åæã
ç±äºæ´ä¸ªå½æ°æ²¡æåæ¯ï¼è¿éä¹ä¸éè¦è®¨è®ºæ§å¶ä¾èµï¼control dependenceï¼ï¼åªè¦è®¨è®ºæ°æ®ä¾èµï¼data dependenceï¼å°±å¥½ã
ææ°æ®ä¾èµå¾ç»åºæ¥æ¯ä¸ªDAGï¼è¿éæ£å¥½æ¯æ£µæ ï¼ç¹ä¾äºï¼ï¼
x y z 48
\ / \ /
t1 0xFFFF t2
\ / /
t3 /
\ /
t4
ä¼åå¿ é¡»è¦æ»¡è¶³ç约ææ¯ï¼æ¯ä¸ªèç¹æ±å¼ä¹åï¼å ¶åèç¹ï¼ä¾èµçæ°æ®æºï¼å¿ é¡»è¦å æ±äºå¼ã
æ¾ç¶ï¼t1åt2ä¹é´æ²¡æä¾èµå ³ç³»ï¼å®ä»¬çç¸å¯¹æ±å¼é¡ºåºææ ·éæé½æ²¡å ³ç³»ã
ææ¬æå¾å欢ç书ï¼éé¢è®²çæ¯åç§åºäºä¾èµçä¼åï¼Optimizing Compilers for Modern Architectures - A Dependence-based Approach
以ä¸æ¯ç论é¨åã
================================================================
ä¸é¢æ¥çä¾åã
æ们å¯ä»¥ç¨ä¸ä¸ªå®é ç¼è¯å¨æ¥ççCSAPPçä¾åç¼è¯åºæ¥çç»æï¼
.text
# -- Begin arith
.p2align 4,,15
.globl arith
.type arith, @function
arith:
.p2align 4,,7
/*.L0:*/ /* Block BB[54:2] preds: none, freq: 1.000 */
movl 8(%esp), %edx /* ia32_Load T[139:10] -:1:22 */
addl 4(%esp), %edx /* ia32_Add Iu[141:12] -:2:14 */
movzwl %dx, %edx /* ia32_Conv_I2I Iu[142:13] -:4:15 */
imull 12(%esp), %edx /* ia32_IMul Iu[143:14] -:5:15 */
leal (%edx,%edx,2), %eax /* ia32_Lea Iu[144:15] -:5:15 */
shll $0x4, %eax /* ia32_Shl Iu[146:17] -:5:15 */
ret /* ia32_Return X[152:23] -:6:3 */
.size arith, .-arith
# -- End arith
è¿éç¨çæ¯libFirmãå¯è§å®è·CSAPP书éæ说çæ±ç¼ç顺åºåææä¸åãè¿ä¹æ¯å®å ¨åççã
è¿ä¸ªç¼è¯ç»æç顺åºæ¯ï¼
edx = y;
edx += x;
edx = zeroextend dx; // edx = edx & 0xFFFF
edx *= z;
eax = edx * 3;
eax <<= 4; // eax = eax * 16
ä¹æ¯å®å ¨ç¬¦åä¾èµå ³ç³»ç约æçä¸ç§é¡ºåºã
ä¹æ以ç¨libFirm举ä¾æ¯å 为å®çä¸é´è¡¨ç¤ºï¼Intermediate Representationï¼æ¯ä¸ç§ç¨åºä¾èµå¾ï¼Program Dependence Graphï¼ï¼å¯ä»¥å¾æ¹ä¾¿ççåºæ§å¶ä¸æ°æ®ä¾èµãæCSAPPé£éä¾å对åºçlibFirm IRç»åºæ¥ï¼æ¯è¿ä¸ªæ ·åçï¼
ï¼è¿å¼ å¾è·æåé¢ç»çæ°æ®ä¾èµå¾æ£å¥½æ¯å·¦å³ç¿»è½¬çï¼ä¸è¿ææä¸æ ·ãï¼è¿å¼ å¾è·æåé¢ç»çæ°æ®ä¾èµå¾æ£å¥½æ¯å·¦å³ç¿»è½¬çï¼ä¸è¿ææä¸æ ·ã
Arg 0ã1ã2åå«ä»£è¡¨xãyãzãç½è²æ¹åæ¯æ®éæ°æ®èç¹ï¼é»è²æ¹åæ¯å¸¸éèç¹ï¼èè²æ¹åæ¯å åç¸å ³èç¹ï¼çº¢è²æ¹åæ¯æ§å¶æµèç¹ï¼ç²çº¢è²æ¹åæ¯ç¹æ®çå¼å§/ç»æèç¹ãï¼
æçLLVMçæç代ç ï¼
; ModuleID = '/tmp/webcompile/_16355_0.bc'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-ellcc-linux"
; Function Attrs: nounwind readnone
define i32 @arith(i32 %x, i32 %y, i32 %z) #0 {
entry:
%add = add nsw i32 %y, %x
%mul = mul nsw i32 %z, 48
%and = and i32 %add, 65535
%mul1 = mul nsw i32 %mul, %and
ret i32 %mul1
}
attributes #0 = { nounwind readnone "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.ident = !{!0}
!0 = !{!"ecc 0.1.10 based on clang version 3.7.0 (trunk) (based on LLVM 3.7.0svn)"}
æç»çæçx86æ±ç¼ï¼
.text
.file "/tmp/webcompile/_15964_0.c"
.globl arith
.align 16, 0x90
.type arith,@function
arith: # @arith
# BB#0: # %entry
movl 8(%esp), %eax
addl 4(%esp), %eax
movzwl %ax, %eax
imull 12(%esp), %eax
shll $4, %eax
leal (%eax,%eax,2), %eax
retl
.Ltmp0:
.size arith, .Ltmp0-arith
.ident "ecc 0.1.10 based on clang version 3.7.0 (trunk) (based on LLVM 3.7.0svn)"
.section ".note.GNU-stack","",@progbits
GCC 4.9.2 x86-64ï¼
arith(int, int, int):
leal (%rdx,%rdx,2), %eax
addl %edi, %esi
movzwl %si, %esi
sall $4, %eax
imull %esi, %eax
ret
Zing VM Server Compiler x86-64ï¼
# edi: x
# esi: y
# edx: z
movl %edx, %eax
shll $0x4, %eax
leal (%rsi, %rdi, 1), %ecx
shll $0x5, %edx
addl %edx, $eax
movzwl %ecx, %edx
imull %edx, %eax
温馨提示:答案为网友推荐,仅供参考