LLVM insertvalue 优化不佳?

发布于 2024-12-28 04:37:41 字数 1711 浏览 4 评论 0原文

当我发出 LLVM 代码时,是否应该避免将“insertvalue”指令与加载和存储结合使用? 当我使用它时,我总是得到糟糕的优化本机代码。看下面的例子:

; ModuleID = 'mod'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-pc-linux-gnu"

%A = type { i64, i64, i64, i64, i64, i64, i64, i64 }

@aa = external global %A*

define void @func() {
entry:
  %a1 = load %A** @aa
  %a2 = load %A* %a1
  %a3 = insertvalue %A %a2, i64 3, 3
  store %A %a3, %A* %a1
  ret void
}

当我运行“llc -o - -O3 mod.ll”时,我得到这个可怕的代码:

func:                                   # @func
.Ltmp0:
        .cfi_startproc
# BB#0:                                 # %entry
        movq    aa(%rip), %rax
        movq    (%rax), %r8
        movq    8(%rax), %r9
        movq    16(%rax), %r10
        movq    32(%rax), %rdi
        movq    40(%rax), %rcx
        movq    48(%rax), %rdx
        movq    56(%rax), %rsi
        movq    %rsi, 56(%rax)
        movq    %rdx, 48(%rax)
        movq    %rcx, 40(%rax)
        movq    %rdi, 32(%rax)
        movq    %r10, 16(%rax)
        movq    %r9, 8(%rax)
        movq    %r8, (%rax)
        movq    $3, 24(%rax)
        ret

但我想看到的是:

func:                                   # @func
.Ltmp0:
        .cfi_startproc
# BB#0:                                 # %entry
        movq    aa(%rip), %rax
        movq    $3, 24(%rax)
        ret

当然我可以使用 getelementptr 或其他东西,但有时它是更容易生成 insertvalue 和 extractvalue 指令,并且我希望对它们进行优化...

我认为代码生成器很容易看到这样的事情是不好的:

        movq    56(%rax), %rsi
        movq    %rsi, 56(%rax)

Should I avoid using the 'insertvalue' instruction combined with load and store when I emit LLVM code?
I always get bad optimized native code when I use it. Look at the following example:

; ModuleID = 'mod'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-pc-linux-gnu"

%A = type { i64, i64, i64, i64, i64, i64, i64, i64 }

@aa = external global %A*

define void @func() {
entry:
  %a1 = load %A** @aa
  %a2 = load %A* %a1
  %a3 = insertvalue %A %a2, i64 3, 3
  store %A %a3, %A* %a1
  ret void
}

When I run "llc -o - -O3 mod.ll", I get this horrible code:

func:                                   # @func
.Ltmp0:
        .cfi_startproc
# BB#0:                                 # %entry
        movq    aa(%rip), %rax
        movq    (%rax), %r8
        movq    8(%rax), %r9
        movq    16(%rax), %r10
        movq    32(%rax), %rdi
        movq    40(%rax), %rcx
        movq    48(%rax), %rdx
        movq    56(%rax), %rsi
        movq    %rsi, 56(%rax)
        movq    %rdx, 48(%rax)
        movq    %rcx, 40(%rax)
        movq    %rdi, 32(%rax)
        movq    %r10, 16(%rax)
        movq    %r9, 8(%rax)
        movq    %r8, (%rax)
        movq    $3, 24(%rax)
        ret

But what I would like to see is this:

func:                                   # @func
.Ltmp0:
        .cfi_startproc
# BB#0:                                 # %entry
        movq    aa(%rip), %rax
        movq    $3, 24(%rax)
        ret

Of course I can use getelementptr or something, but sometimes it is easier to generate insertvalue and extractvalue instructions, and I want these to be optimized...

I think it would be quite easy for the codegen to see that things like these are bad:

        movq    56(%rax), %rsi
        movq    %rsi, 56(%rax)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶视觉 2025-01-04 04:37:41

首先,请注意 llc 不进行任何 IR 级别的优化。因此,您应该运行 opt 来运行一组 IR 级优化器。

然而,opt 对此没有帮助。我希望标准 IR 级优化器能够以某种方式将这些内容规范化为 gep 。

请提交 LLVM PR,这看起来像是错过了优化!

First, note that llc does not do any IR-level optimizations. So, you should run opt to run the set of IR-level optimizers.

However, opt does not help in this. I'd expect that standard IR-level optimizers canonicalize the stuff into gep somehow.

Please file a LLVM PR, this looks like a missed optimization!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文