[ARM][Thumb2] Fix ADD/SUB invalid writes to SP
authorDiogo Sampaio <diogo.sampaio@arm.com>
Wed, 8 Jan 2020 10:26:28 +0000 (10:26 +0000)
committerDiogo Sampaio <diogo.sampaio@arm.com>
Fri, 10 Jan 2020 11:25:44 +0000 (11:25 +0000)
commit8c12769f3046029e2a9b4e48e1645b1a77d28650
tree358071d0bcd70bce130a2335b343497ef185e996
parent1ccee0e86386762bd742fd067391b6c4be089806
[ARM][Thumb2] Fix ADD/SUB invalid writes to SP

Summary:
This patch fixes pr23772  [ARM] r226200 can emit illegal thumb2 instruction: "sub sp, r12, #80".
The violation was that SUB and ADD (reg, immediate) instructions can only write to SP if the source register is also SP. So the above instructions was unpredictable.
To enforce that the instruction t2(ADD|SUB)ri does not write to SP we now enforce the destination register to be rGPR (That exclude PC and SP).
Different than the ARM specification, that defines one instruction that can read from SP, and one that can't, here we inserted one that can't write to SP, and other that can only write to SP as to reuse most of the hard-coded size optimizations.
When performing this change, it uncovered that emitting Thumb2 Reg plus Immediate could not emit all variants of ADD SP, SP #imm instructions before so it was refactored to be able to. (see test/CodeGen/Thumb2/mve-stacksplot.mir where we use a subw sp, sp, Imm12 variant )
It also uncovered a disassembly issue of adr.w instructions, that were only written as SUBW instructions (see llvm/test/MC/Disassembler/ARM/thumb2.txt).

Reviewers: eli.friedman, dmgreen, carwil, olista01, efriedma

Reviewed By: efriedma

Subscribers: john.brawn, efriedma, ostannard, kristof.beyls, hiraditya, dmgreen, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D70680
24 files changed:
llvm/lib/Target/ARM/ARMAsmPrinter.cpp
llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
llvm/lib/Target/ARM/ARMInstrThumb2.td
llvm/lib/Target/ARM/ARMLoadStoreOptimizer.cpp
llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
llvm/lib/Target/ARM/Disassembler/ARMDisassembler.cpp
llvm/lib/Target/ARM/Thumb2InstrInfo.cpp
llvm/test/CodeGen/ARM/GlobalISel/thumb-select-arithmetic-ops.mir
llvm/test/CodeGen/ARM/GlobalISel/thumb-select-load-store.mir
llvm/test/CodeGen/MIR/ARM/thumb2-sub-sp-t3.mir
llvm/test/CodeGen/Thumb2/bug-subw.ll [new file with mode: 0644]
llvm/test/CodeGen/Thumb2/fp16-stacksplot.mir
llvm/test/CodeGen/Thumb2/mve-stacksplot.mir
llvm/test/CodeGen/Thumb2/peephole-addsub.mir
llvm/test/CodeGen/Thumb2/peephole-cmp.mir
llvm/test/MC/ARM/basic-thumb2-instructions.s
llvm/test/MC/ARM/invalid-addsub.s
llvm/test/MC/ARM/negative-immediates.s
llvm/test/MC/ARM/register-token-source-loc.s
llvm/test/MC/ARM/thumb-diagnostics.s
llvm/test/MC/Disassembler/ARM/invalid-thumbv7.txt
llvm/test/MC/Disassembler/ARM/thumb-tests.txt
llvm/test/MC/Disassembler/ARM/thumb2-v8.txt
llvm/test/MC/Disassembler/ARM/thumb2.txt