llvm 使用库函数进行优化 [英] llvm optimizes with library functions

查看:53
本文介绍了llvm 使用库函数进行优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这样的代码开始

void lib_memset( unsigned char *dest, unsigned char c, unsigned int n)
{
    while(n--)
    {
        *dest=c;
        dest++;
    }
}

使用 llvm 作为交叉编译器

using llvm as a cross compiler

clang -Wall -m32 -emit-llvm -fno-builtin --target=arm-none-eabi  -c lib_memset.c -o lib_memset.bc
opt -std-compile-opts -strip-debug -march=arm -mcpu=mpcore -mtriple=arm-none-eabi lib_memset.bc -o lib_memset.opt.bc
llc -march=arm -mcpu=mpcore -disable-simplify-libcalls  lib_memset.opt.bc -o lib_memset.opt.s
llc -march=arm -mcpu=mpcore -disable-simplify-libcalls  lib_memset.bc -o lib_memset.s

并在使用优化器时检测并用真实的 memset 替换它

and it detects and replaces it with a real memset when the optimizer is used

lib_memset:
    push    {r11, lr}
    mov r3, r1
    mov r11, sp
    cmp r2, #0
    beq .LBB0_2
    mov r1, r2
    mov r2, r3
    bl  __aeabi_memset
.LBB0_2:                                @ %while.end
    pop {r11, pc}

但是没有实现它.

我不希望它编译我提供给它的目标代码,而不是使用库调用.我以为 -disable-simplify-libcalls 会做到,但它没有.

I don't want that I want it to compile the code I gave it for the target I gave it rather than use library calls. I thought the -disable-simplify-libcalls would do it but it doesn't.

我以为我以前已经想通了,但不知道如何去做.我需要优化器,我不想要实现库的循环依赖问题,它需要库等.可以在 asm 中做到这一点以使编译器退出循环,但不应该这样做.

I thought I had figured this out before, but cant find out how to do it. I need the optimizer, I don't want this circular dependency problem of implementing the library and it needing the library, etc. Could do it in asm to take the compiler out of the loop, but shouldn't have to.

推荐答案

clang -Wall -m32 -emit-llvm -fno-builtin --target=arm-none-eabi -ffreestanding -c lib_memset.c -o lib_memset.bc
opt -std-compile-opts -strip-debug -march=arm -mcpu=mpcore -mtriple=arm-none-eabi -disable-simplify-libcalls  lib_memset.bc -o lib_memset.opt.bc
llc -march=arm -mcpu=mpcore -disable-simplify-libcalls -mtriple=arm-none-eabi  lib_memset.opt.bc -o lib_memset.opt.s
llc -march=arm -mcpu=mpcore -disable-simplify-libcalls -mtriple=arm-none-eabi  lib_memset.bc -o lib_memset.s

感谢在添加 -ffreestanding 时的巧妙噪音,我决定重新阅读 llc 和 opt 的所有 --help 选项,并发现 -disable-simpilfy-libcalls 是 opt 和 llc 的一个选项,将其添加到选择修复了问题.

thanks to artless noise while adding -ffreestanding, I decided to re-read all the --help options for llc and opt, and found the -disable-simpilfy-libcalls is an option for both opt and llc, adding it to opt fixed the problem.

lib_memset:
    cmp r2, #0
    bxeq    lr
.LBB0_1:
    strb    r1, [r0], #1
    subs    r2, r2, #1
    bne .LBB0_1
    bx  lr

我不喜欢回答我自己的问题,可以在这里坐一会儿,以便下次我可以找到答案,或者如果 SO 大神决定把它留在这里就好了...

I dont like answering my own question, can sit here for a bit so I can maybe find the answer next time or if the SO gods decide to leave it here that is fine...

这篇关于llvm 使用库函数进行优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆