LLVM OPT 不提供优化文件作为输出。

LLVM OPT not giving optimised file as output.

opt 的手册页说:"It takes LLVM source files as input, runs the specified optimizations or analyses on it, and then outputs the optimized file or the analysis results"。

我的目标:使用 opt 中可用的内置优化通道 -dce。此通行证 Dead Code Elimination

我的源文件foo.c

int foo(void)
 {
   int a = 24;
   int b = 25; /* Assignment to dead variable -- dead code */
   int c;
   c = a * 4;
   return c;
}

这是我所做的:
1. clang-7.0 -S -emit-llvm foo.c -o foo.ll
2. opt -dce -S foo.ll -o fooOpt.ll

我所期望的 : 一个 .ll 文件,其中删除了死代码(在带有注释的源代码中)部分。

我得到的:fooOpt.ll与非优化代码相同foo.ll

我已经看过 this SO 答案,但我没有得到优化代码。
我在这里错过了什么吗?有人可以指导我走正确的道路吗?
谢谢。

如果你查看 clang 生成的 .ll 文件,它会包含这样一行:

attributes #0 = { noinline nounwind optnone sspstrong uwtable ...}

您应该在此处删除 optnone 属性。每当函数具有 optnone 属性时,opt 根本不会触及该函数。

现在,如果您再试一次,您会发现……什么都没有。还是不行。

这次的问题是代码在内存上工作,而不是寄存器。我们需要做的是将 alloca 转换为使用 -mem2reg 的寄存器。事实上这样做已经优化掉 b,所以你甚至不需要 -dce 标志。