简体   繁体   English

使用 llvm opt 进行源到源转换

[英]Source to source transformations with llvm opt

I'm compiling the following code:我正在编译以下代码:

#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
    unsigned int i=atoi(argv[1]);
    unsigned int n=atoi(argv[2]);
    unsigned int r=0;
    unsigned int iter=0;

    while(i<n)
    {
        iter++;
        if (r>0) { i=0; r--; }
        else     { i++;      }
    }
    return iter;
}

with

$ clang -O3 -c -emit-llvm file.c -o file.bc
$ llvm-dis file.bc -o file.ll

And when I inspect the resulting *.ll file I'm pleasantly surprised, because the entire code shrinks down to:当我检查生成的*.ll文件时,我很惊喜,因为整个代码缩小到:

; Function Attrs: nounwind uwtable
define dso_local i32 @main(i32 %argc, i8** nocapture readonly %argv) local_unnamed_addr #0 {
entry:
  %arrayidx = getelementptr inbounds i8*, i8** %argv, i64 1
  %0 = load i8*, i8** %arrayidx, align 8, !tbaa !2
  %call.i = tail call i64 @strtol(i8* nocapture nonnull %0, i8** null, i32 10) #2
  %conv.i = trunc i64 %call.i to i32
  %arrayidx1 = getelementptr inbounds i8*, i8** %argv, i64 2
  %1 = load i8*, i8** %arrayidx1, align 8, !tbaa !2
  %call.i12 = tail call i64 @strtol(i8* nocapture nonnull %1, i8** null, i32 10) #2
  %conv.i13 = trunc i64 %call.i12 to i32
  %2 = icmp ugt i32 %conv.i13, %conv.i
  %3 = select i1 %2, i32 %conv.i13, i32 %conv.i
  %4 = sub i32 %3, %conv.i
  ret i32 %4
}

Which essentially means: return (n>i)?(ni):0 Is there anyway I can translate optimized bitcode back to C source?这基本上意味着: return (n>i)?(ni):0无论如何我可以将优化的位码翻译回 C 源吗?

You can try llvm-cbe .你可以试试llvm-cbe Maybe it will be suitable for you.也许它会适合你。

Anyway, llvm-cbe was removed from llvm upstream, due it was not working good as I know:D无论如何,llvm-cbe 已从上游 llvm 中删除,因为据我所知,它运行不佳:D

Maybe retdec is usable somehow, due it is based on llvm, but not sure.也许retdec以某种方式可用,因为它基于 llvm,但不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM