为什么呼叫后GCC for Risc-V会产生nop指令

Mat*_*Moy 6 gcc riscv

默认情况下,用于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"
Run Code Online (Sandbox Code Playgroud)

我希望当针对具有分支延迟时隙的架构时,但是我的理解是Risc-V不是这样的架构。实际上,nop使用-O1或更高版本进行编译时消失。

只是GCC中的“错误”会nop从具有延迟插槽的体系结构中遗留下来,还是有此nop指令的实际原因?

bor*_*car 7

这不是一个完整的答案,但至少是要尝试弄清点头出现的原因。我坚信这是具有延迟插槽的体系结构中的错误/遗留问题(因为它是在第一个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>

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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))
Run Code Online (Sandbox Code Playgroud)

与的区别-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))
Run Code Online (Sandbox Code Playgroud)