assembly - 优化 SPARC 程序集中的循环

标签 assembly sparc

我正在尝试以正确的方式优化此代码。我所说的正确的意思是......我想有一种通用的方法来执行这些优化,如果其他人查看代码,他们将能够删除优化。

可读性的 C 代码示例...

int a = 1; // mapped to %l0
int b = 5; // mapped to %l1
int c = 0; // mapped to %l2
int d;     // mapped to %l3

while( a < b ) {
  c += a * b;
  ++a;
}

d = c * b;

SPARC 程序集版本...

  mov    %g0, %l2

  cmp    %l0, %l1
  bge    END_LOOP
  nop

LOOP:
  mov    %l0, %o0
  call   .mul
  mov    %l1, %o1       ! Fill delay slot with second argument
  add    %l2, %o0, %l2
  inc    %l0

  cmp    %l0, %l1
  bl     LOOP
  nop

END_LOOP:
  mov    %l2, %o0
  call   .mul           ! Fill delay sot with second argument
  mov    %l1, %o1

  mov    %o0, %l3

我可以优化第一部分(不确定是否正确),但我不确定如何优化第二部分。

  mov    %g0, %l2

  cmp    %l0, %l1
  bge,a  END_LOOP       ! Annul branch to execute if branch is taken
  mov    %l2, %o0       ! Instruction at target

LOOP:
  mov    %l0, %o0
  call   .mul
  mov    %l1, %o1       ! Fill delay slot with second argument
  add    %l2, %o0, %l2
  inc    %l0

  cmp    %l0, %l1
  bl     LOOP
  nop

  mov    %l2, %o0       ! Move the instruction to above the target

END_LOOP:
  call   .mul           ! Fill delay sot with second argument
  mov    %l1, %o1

  mov    %o0, %l3

如能提供有关如何执行这些优化的任何帮助,我们将不胜感激。

最佳答案

总的来说,你的做法是正确的。假设您在目标之后没有数据依赖或控制转移指令,您通常可以遵循以下约定。

你这样做了,可能没有意识到:

转到分支目标,将指令复制到延迟槽中并取消分支。如您所述,您取消了分支以防止在未采用分支时执行指令。然后将指令移动到标签上方的目标处。

在您的代码中,按照上述步骤,您将执行以下操作:

我删除了您的评论,以便您可以清楚地看到我所做的更改。

  mov    %g0, %l2

  cmp    %l0, %l1
  bge,a  END_LOOP
  mov    %l2, %o0

  mov    %l0, %o0       ! 3. Move the instruction formerly after the loop
                        ! above the label
LOOP:
  [ mov    %l0, %o0 ]   ! Instruction was here

  call   .mul
  mov    %l1, %o1
  add    %l2, %o0, %l2
  inc    %l0

  cmp    %l0, %l1
  bl,a   LOOP           ! 1. Go to the target and copy that instruction into
                        ! they delay slot.
                        ! 2. Annul the branch
  mov    %l0, %o0       ! Instruction formerly after LOOP:

  mov    %l2, %o0

END_LOOP:
  call   .mul
  mov    %l1, %o1

  mov    %o0, %l3

如果仔细检查代码,您会发现逻辑仍然成立,并且有系统的方法来展开优化。

无论您是否进入循环,您的代码仍将正确执行循环之后的代码。

这是优化代码的一般方法,类似于编译器将执行的操作。关键始终是确保不存在数据依赖性。

关于assembly - 优化 SPARC 程序集中的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17382275/

相关文章:

c - Sparc 函数编译对齐

c - Sparc V8比较和交换函数的内联汇编实现

assembly - 跟踪和反汇编与内核镜像文件不匹配的 Linux 内核指令

assembly - 为什么 Linux 上的 NASM 会更改 x86_64 程序集中的寄存器

assembly - TI DSP 编程 - C 足够快还是我需要汇编程序?

linux - 使用 x86 Material 学习 64 位操作系统上的汇编?

c - SUN Sparc 上的堆栈溢出

c - 使用 Eclipse SPARC bare-C 编译器未找到 "windows.h"

C代码调用程序集: infinite loop

c - 为什么 MSVC Debug模式会为一个空的 if() 主体而不是另一个(i++ vs.++i)省略 cmp/jcc?