默认情况下,用于Risc-V的GCC会在nop指令之后生成call指令:

$ cat test.c
void g();
void f() {
        g();
}
$ riscv64-unknown-elf-gcc -S test.c -o -
[...]
f:
        addi    sp,sp,-16
        sd      ra,8(sp)
        sd      s0,0(sp)
        addi    s0,sp,16
        call    g
        nop #### <-----------here
        ld      ra,8(sp)
        ld      s0,0(sp)
        addi    sp,sp,16
        jr      ra
        .size   f, .-f
        .ident  "GCC: (GNU) 8.3.0"

我希望在针对具有branch delay slot的体系结构时,但我的理解是Risc-V不是这样的体系结构。实际上,当使用nop或更高版本进行编译时,-O1消失了。

只是GCC中的“错误”会发出nop作为具有延迟插槽的体系结构的剩余部分,还是出现此nop指令的实际原因?

最佳答案

这不是一个完整的答案,但至少是试图弄清点头出现的原因。我坚信这是具有延迟插槽的体系结构中的错误/遗留问题(因为它是在第一个RTL传递中添加-扩展)。

在调查中,GCC有两种类型的通行证:Tree和RTL。要查看它们的实际效果,请准备2个文件夹,因为它将有许多文件nooptopt,并使用-fdump-tree-all-raw -fdump-rtl-all查看中间结果。 Tree阶段的最后阶段给出了(noopt情况):

$ cat noopt/test.c.232t.optimized

;; Function f (f, funcdef_no=0, decl_uid=1549, cgraph_uid=0, symbol_order=0)

f ()
{
  <bb 2> :
  gimple_call <g, NULL>
  gimple_return <NULL NULL>

}

opt大小写(-O1)的差异可以忽略不计:
$ diff -u noopt/test.c.232t.optimized opt/test.c.232t.optimized
--- noopt/test.c.232t.optimized 2019-09-03 14:48:02.874071927 +0200
+++ opt/test.c.232t.optimized   2019-09-03 14:48:29.550278667 +0200
@@ -3,7 +3,7 @@

 f ()
 {
-  <bb 2> :
+  <bb 2> [local count: 1073741825]:
   gimple_call <g, NULL>
   gimple_return <NULL NULL>

RTL通过(扩展)的第一阶段是不同的:
$ cat noopt/test.c.234r.expand

;; Function f (f, funcdef_no=0, decl_uid=1549, cgraph_uid=0, symbol_order=0)


;; Generating RTL for gimple basic block 2


try_optimize_cfg iteration 1

Merging block 3 into block 2...
Merged blocks 2 and 3.
Merged 2 and 3 without moving.
Merging block 4 into block 2...
Merged blocks 2 and 4.
Merged 2 and 4 without moving.


try_optimize_cfg iteration 2



;;
;; Full RTL generated for this function:
;;
(note 1 0 3 NOTE_INSN_DELETED)
(note 3 1 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
(note 2 3 5 2 NOTE_INSN_FUNCTION_BEG)
(call_insn 5 2 8 2 (parallel [
            (call (mem:SI (symbol_ref:DI ("g") [flags 0x41] <function_decl 0x7fbc2827a400 g>) [0 g S4 A32])
                (const_int 0 [0]))
            (clobber (reg:SI 1 ra))
        ]) "../test.c":3 -1
     (nil)
    (nil))
(insn 8 5 0 2 (const_int 0 [0]) "../test.c":4 -1
     (nil))
-O1的不同之处在于只是删除了const_int 0 [0],最终将导致nop:
$ diff -u noopt/test.c.234r.expand opt/test.c.234r.expand
--- noopt/test.c.234r.expand    2019-09-03 14:48:02.874071927 +0200
+++ opt/test.c.234r.expand  2019-09-03 14:48:29.550278667 +0200
@@ -25,12 +25,10 @@
 (note 1 0 3 NOTE_INSN_DELETED)
 (note 3 1 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
 (note 2 3 5 2 NOTE_INSN_FUNCTION_BEG)
-(call_insn 5 2 8 2 (parallel [
-            (call (mem:SI (symbol_ref:DI ("g") [flags 0x41] <function_decl 0x7fbc2827a400 g>) [0 g S4 A32])
+(call_insn 5 2 0 2 (parallel [
+            (call (mem:SI (symbol_ref:DI ("g") [flags 0x41] <function_decl 0x7f0bdec1f400 g>) [0 g S4 A32])
                 (const_int 0 [0]))
             (clobber (reg:SI 1 ra))
         ]) "../test.c":3 -1
      (nil)
     (nil))
-(insn 8 5 0 2 (const_int 0 [0]) "../test.c":4 -1
-     (nil))

关于gcc - 为什么调用后GCC for Risc-V会产生nop指令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57695031/

10-11 23:03
查看更多