LLVM OPT not giving optimised file as output.

孤街浪徒 提交于 2020-07-21 03:28:28

问题


The man page for opt says: "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".

My Goal: To use the inbuilt optimisation pass -dce available in opt. This pass does Dead Code Elimination

My Source file foo.c:

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

Here is what I did:
1. clang-7.0 -S -emit-llvm foo.c -o foo.ll
2. opt -dce -S foo.ll -o fooOpt.ll

What I expect : A .ll file in which the dead code (in source code with the comment) part is eliminated.

What I get: fooOpt.ll is the same as non optimised code foo.ll

I have already seen this SO answer, but I didn't get optimised code.
Am I missing something here? Can someone please guide me on the right path.
Thank you.


回答1:


If you look at the .ll file generated by clang, it will contain a line like this:

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

You should remove the optnone attribute here. Whenever a function has the optnone attribute, opt won't touch that function at all.

Now if you try again, you'll notice ... nothing. It still does not work.

This time the problem is that the code is working on memory, not registers. What we need to do is to convert the allocas to registers using -mem2reg. In fact doing this will already optimize away b, so you don't even need the -dce flag.



来源:https://stackoverflow.com/questions/49092340/llvm-opt-not-giving-optimised-file-as-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!