[CodeGen][ARM] Error when writing to specific reserved registers in inline asm
authorVictor Campos <victor.campos@arm.com>
Wed, 25 Mar 2020 10:52:43 +0000 (10:52 +0000)
committerVictor Campos <victor.campos@arm.com>
Wed, 15 Apr 2020 13:40:42 +0000 (14:40 +0100)
commitd85b3877dcd283feb6075162765664173cf1488f
treea685d37e25ad452055ee2daa76e63b8c617019f4
parent01bcc3e9371470e1974f066ced353df15e10056d
[CodeGen][ARM] Error when writing to specific reserved registers in inline asm

Summary:
No error or warning is emitted when specific reserved registers are
written to in inline assembly. Therefore, writes to the program counter
or to the frame pointer, for instance, were permitted, which could have
led to undesirable behaviour.

Example:
  int foo() {
    register int a __asm__("r7"); // r7 = frame-pointer in M-class ARM
    __asm__ __volatile__("mov %0, r1" : "=r"(a) : : );
    return a;
  }

In contrast, GCC issues an error in the same scenario.

This patch detects writes to specific reserved registers in inline
assembly for ARM and emits an error in such case. The detection works
for output and input operands. Clobber operands are not handled here:
they are already covered at a later point in
AsmPrinter::emitInlineAsm(const MachineInstr *MI). The registers
covered are: program counter, frame pointer and base pointer.

This is ARM only. Therefore the implementation of other targets'
counterparts remain open to do.

Reviewers: efriedma

Reviewed By: efriedma

Subscribers: kristof.beyls, hiraditya, danielkiss, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D76848
llvm/include/llvm/CodeGen/TargetRegisterInfo.h
llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
llvm/lib/Target/ARM/ARMBaseRegisterInfo.cpp
llvm/lib/Target/ARM/ARMBaseRegisterInfo.h
llvm/test/CodeGen/ARM/inline-asm-reserved-registers.ll [new file with mode: 0644]