how to annotate assembler

Hi,

In GCC there is one useful option -dp (or -dP for more verbose output)
to annotate assembler with instruction patterns, that was used when
assembler was generated. For example:

double
test(long long s)
{
  return s;
}

gcc -S -dp -O0 test.c

test:
.LFB0:
  .cfi_startproc
  pushq %rbp # 18 *pushdi2_rex64/1 [length = 1]
  .cfi_def_cfa_offset 16
  movq %rsp, %rbp # 19 *movdi_1_rex64/2 [length = 3]
  .cfi_offset 6, -16
  .cfi_def_cfa_register 6
  movq %rdi, -8(%rbp) # 2 *movdi_1_rex64/4 [length = 4]
  cvtsi2sdq -8(%rbp), %xmm0 # 6 *floatdidf2_sse_interunit/2 [length = 6]
  leave # 24 leave_rex64 [length = 1]
  .cfi_def_cfa 7, 8
  ret # 25 return_internal [length = 1]
  .cfi_endproc

Now I may look into config/i386/i386.md, and look for mentioned
*pushdi2_rex64, movdi_1_rex64, floatdidf2_sse_interunit and other
patterns and study how they work.

How to make the same annotation for clang output assembler code?

test:
.Leh_func_begin1:
  pushq %rbp # ??? what insn in X86InstrInfo.td?
.Llabel1:
  movq %rsp, %rbp # ??? what insn in X86InstrInfo.td?
.Llabel2:
  movq %rdi, -16(%rbp) # ??? what insn in X86InstrInfo.td?
  movq -16(%rbp), %rax # ??? what insn in X86InstrInfo.td?
  cvtsi2sdq %rax, %xmm0 # ??? what insn in X86InstrInfo.td?
  movsd %xmm0, -8(%rbp) # ??? what insn in X86InstrInfo.td?
  movsd -8(%rbp), %xmm0 # ??? what insn in X86InstrInfo.td?
  popq %rbp # ??? what insn in X86InstrInfo.td?
  ret

Hello

How to make the same annotation for clang output assembler code?

There is no such functionality.

Hi,

In GCC there is one useful option -dp (or -dP for more verbose output)
to annotate assembler with instruction patterns, that was used when
assembler was generated. For example:

The internal "-mllvm -show-mc-inst" option is probably as close as you can get.

$ clang -S -O0 test.c -mllvm -show-mc-inst -o -
_test: ## @test
  .cfi_startproc
## BB#0: ## %entry
  pushq %rbp ## <MCInst #2120 PUSH64r
                                        ## <MCOperand Reg:106>>
Ltmp2:
  .cfi_def_cfa_offset 16
Ltmp3:
  .cfi_offset %rbp, -16
  movq %rsp, %rbp ## <MCInst #1491 MOV64rr
                                        ## <MCOperand Reg:106>
                                        ## <MCOperand Reg:114>>
Ltmp4:
  .cfi_def_cfa_register %rbp
  movq %rdi, -8(%rbp) ## <MCInst #1482 MOV64mr
                                        ## <MCOperand Reg:106>
                                        ## <MCOperand Imm:1>
                                        ## <MCOperand Reg:0>
                                        ## <MCOperand Imm:-8>
                                        ## <MCOperand Reg:0>
                                        ## <MCOperand Reg:109>>
  cvtsi2sdq -8(%rbp), %xmm0 ## <MCInst #607 CVTSI2SD64rm
                                        ## <MCOperand Reg:128>
                                        ## <MCOperand Reg:106>
                                        ## <MCOperand Imm:1>
                                        ## <MCOperand Reg:0>
                                        ## <MCOperand Imm:-8>
                                        ## <MCOperand Reg:0>>
  popq %rbp ## <MCInst #2000 POP64r
                                        ## <MCOperand Reg:106>>
  ret ## <MCInst #2227 RET>
  .cfi_endproc

- Ben

Hi,

Thank you, it is just what I need. But... it doesn't work for me:

$ clang -S -O0 test.c -mllvm -show-mc-inst
error: unknown argument: '-show-mc-inst'

$ clang --version
clang version 1.1 (branches/release_27)
Target: x86_64-pc-linux-gnu
Thread model: posix

May be I need LLVM with higher version, or mention something in
configure options?

$ clang --version
clang version 1.1 (branches/release_27)

                               ^^^^^^^^^^

   Looks suspicious. I am not sure MC related thing appears at that time.
Try 3.0 instead.

Target: x86_64-pc-linux-gnu
Thread model: posix

Regards,
chenwj

$ clang --version
clang version 1.1 (branches/release_27)

                              ^^^^^^^^^^

  Looks suspicious. I am not sure MC related thing appears at that time.
Try 3.0 instead.

Right. 2.7 is ancient and predates lots of the MC related stuff.

-Jim