LLVM库函数优化 [英] llvm optimizes with library functions

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

问题描述

与code这样

启动

 无效lib_memset(无符号字符* DEST,unsigned char型C,无符号整数N)
{
    而(N--)
    {
        * DEST = C;
        DEST ++;
    }
}

使用LLVM作为一个交叉编译器

 铛-Wall -m32 -emit-LLVM -fno-内置--target =臂无 -  EABI -c lib_memset.c -o lib_memset.bc
选择-std-编译OPTS -strip调试-march =手臂-mcpu = MPCore的-mtriple =臂无 - EABI lib_memset.bc -o lib_memset.opt.bc
LLC -march =手臂-mcpu = MPCore的-disable-简化-的libcall lib_memset.opt.bc -o lib_memset.opt.s
LLC -march =手臂-mcpu = MPCore的-disable-简化-的libcall lib_memset.bc -o lib_memset.s

和它检测并当使用优化与真正的memset替换它

  lib_memset:
    推{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}

但实现它没有。

我不希望我想它来编译code我给它,因为我给它的目标,而不是使用库的调用。我以为-disable-简化-的libcall会做,但事实并非如此。

我想我之前就想通了这一点,但不能找出如何做到这一点。我需要优化,我不希望实施库,它需要的库等能做到这一点的ASM采取编译圈外的这种循环依赖问题,但应该不是必须的。


解决方案

 铛-Wall -m32 -emit-LLVM -fno-内置--target =臂无 -  EABI -ffreestanding -c lib_memset.c -o lib_memset.bc
选择-std-编译OPTS -strip调试-march =手臂-mcpu = MPCore的-mtriple =臂无 - EABI -disable-简化-的libcall lib_memset.bc -o lib_memset.opt.bc
LLC -march =手臂-mcpu = MPCore的-disable-简化-的libcall -mtriple =臂无 - EABI lib_memset.opt.bc -o lib_memset.opt.s
LLC -march =手臂-mcpu = MPCore的-disable-简化-的libcall -mtriple =臂无 - EABI lib_memset.bc -o lib_memset.s

由于烂漫的噪音,同时增加-ffreestanding,我决定重新读取所有的LLC方案--help和选择,并找到了-disable-simpilfy-的libcall是为选择和有限责任公司的一个选项,将其添加到齐齐解决了这一问题。

  lib_memset:
    CMP R2,#0
    bxeq LR
.LBB0_1:
    STRB R1,[R0],#1
    潜艇R2,R2,#1
    BNE .LBB0_1
    BX LR

我不喜欢回答我的问题,可以在这里坐了一下,所以我也许可以找到答案下一次,或者如果SO众神决定离开这里是罚款......

Starting with code like this

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

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

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}

but implements it without.

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.

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

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

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天全站免登陆