2

简短的问题:如何在clang没有代码优化的情况下进行编译?-O0不管用。

长问题: 我正在学习代码优化,特别是 LLVM。我正在编写小示例,编译它们,然后一次只运行一个优化,以分析它的变化。例如,为了测试死代码消除,我尝试了这个:

int main() {
    int a = 20 + 30;
    int b = 25; /* Assignment to dead variable */
    int c;
    c = a << 2;
    return c;
    b = 24; /* Unreachable code */
    return 0;
}

但是,当我编译它时

clang -S -O0 -emit-llvm foo.c

我的 C 代码的最后两行没有出现在 IR 代码中(如下)。此外,20 + 30已经计算到 50。所以这里有一些优化,即使我使用的是-O0.

; ModuleID = 'hello.c'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define i32 @main() #0 {
entry:
  %retval = alloca i32, align 4
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  %c = alloca i32, align 4
  store i32 0, i32* %retval
  store i32 50, i32* %a, align 4
  store i32 25, i32* %b, align 4
  %0 = load i32* %a, align 4
  %shl = shl i32 %0, 2
  store i32 %shl, i32* %c, align 4
  %1 = load i32* %c, align 4
  ret i32 %1
}

attributes #0 = { nounwind uwtable "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" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = metadata !{metadata !"clang version 3.4 (trunk 192936)"}
4

0 回答 0