2017-01-24 20 views
0

私はLLVMコードの最適化について読んでいます。多くの例でoptコマンドオプションを適用しようとしましたが効果がありません。 example.HereについてdeadCode.cppと呼ばれるC++コードです:私はこのような打ち鳴らすとLLVM IRを生成llvmコードの最適化オプションが機能しない

#include<stdio.h> 

int square(int x){ 
return x*x; 
} 

int main(){ 

    int a=2; 
    int b=3; 
    int c=4; 

    int result =square(a); 
    printf("%d\n",b); 

} 

打ち鳴らすの-emit-LLVM -S deadCode.cpp -o deadCodeBefore

と結果ファイルdeadCodeBefore内容は次のとおりです。

; ModuleID = 'deadCode.cpp' 
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" 
target triple = "x86_64-pc-linux-gnu" 

@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1 

; Function Attrs: nounwind uwtable 
define i32 @_Z6squarei(i32 %x) #0 { 
    %1 = alloca i32, align 4 
    store i32 %x, i32* %1, align 4 
    %2 = load i32, i32* %1, align 4 
    %3 = load i32, i32* %1, align 4 
    %4 = mul nsw i32 %2, %3 
    ret i32 %4 
} 

; Function Attrs: norecurse uwtable 
define i32 @main() #1 { 
    %a = alloca i32, align 4 
    %b = alloca i32, align 4 
    %c = alloca i32, align 4 
    %result = alloca i32, align 4 
    store i32 2, i32* %a, align 4 
    store i32 3, i32* %b, align 4 
    store i32 4, i32* %c, align 4 
    %1 = load i32, i32* %a, align 4 
    %2 = call i32 @_Z6squarei(i32 %1) 
    store i32 %2, i32* %result, align 4 
    %3 = load i32, i32* %b, align 4 
    %4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %3) 
    ret i32 0 
} 

declare i32 @printf(i8*, ...) #2 

attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } 
attributes #1 = { norecurse uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } 
attributes #2 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } 

!llvm.ident = !{!0} 

!0 = !{!"clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)"} 

最適化のコmmand私が使用:

オプト-S -adce deadCodeBefore -o deadCodeAfter1

私は、彼らは何の効果もありませんので、それは二乗関数ともC変数の宣言への呼び出しを削除する必要が読んでいます。しかし結果は同じです。ここにdeadCodeBeforeと同じdeadCodeAfter1があります:

; ModuleID = 'deadCodeBefore' 
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" 
target triple = "x86_64-pc-linux-gnu" 

@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1 

; Function Attrs: nounwind uwtable 
define i32 @_Z6squarei(i32 %x) #0 { 
    %1 = alloca i32, align 4 
    store i32 %x, i32* %1, align 4 
    %2 = load i32, i32* %1, align 4 
    %3 = load i32, i32* %1, align 4 
    %4 = mul nsw i32 %2, %3 
    ret i32 %4 
} 

; Function Attrs: norecurse uwtable 
define i32 @main() #1 { 
    %a = alloca i32, align 4 
    %b = alloca i32, align 4 
    %c = alloca i32, align 4 
    %result = alloca i32, align 4 
    store i32 2, i32* %a, align 4 
    store i32 3, i32* %b, align 4 
    store i32 4, i32* %c, align 4 
    %1 = load i32, i32* %a, align 4 
    %2 = call i32 @_Z6squarei(i32 %1) 
    store i32 %2, i32* %result, align 4 
    %3 = load i32, i32* %b, align 4 
    %4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %3) 
    ret i32 0 
} 

declare i32 @printf(i8*, ...) #2 

attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } 
attributes #1 = { norecurse uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } 
attributes #2 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } 

!llvm.ident = !{!0} 

!0 = !{!"clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)"} 

答えて

0

これはやっている通りです。他の命令によって命令が使用されているかどうかをIRでチェックします。それだけでなく、それはそれを削除します。あなただけの変数を宣言し、それに任意の値を割り当てられていなかった場合は、あなたのコードの例では、(%a = alloca i32, align 4%a変数の宣言はstore命令にstore i32 2, i32* %a, align 4

を使用している、そしてadceパスは、それを排除しているだろう。 int e;のような変数を宣言するだけでそれを見ることができ、最適化を実行します。

通常、LLVMでのパスは、有効にするために、他のパスの出力に依存します。個々のパスを使用すると、それが提供すると予想される結果が得られない場合があります。

関連する問題