[ARM][Thumb2] Fix ADD/SUB invalid writes to SP
authorDiogo Sampaio <diogo.sampaio@arm.com>
Mon, 13 Jan 2020 11:36:02 +0000 (11:36 +0000)
committerDiogo Sampaio <diogo.sampaio@arm.com>
Tue, 14 Jan 2020 11:47:19 +0000 (11:47 +0000)
commitd94d079a6a5b12156e4b818c8ba46eb143f335b9
tree7524c45ac0f99e736d45f891263e872ede29d1ac
parentfd42a4ac7a69adb92f87c7fa927509f177dcc6ca
[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, andreadb

Reviewed By: efriedma

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

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D70680
26 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/CodeGen/Thumb2/t2peephole-t2ADDrr-to-t2ADDri.ll [new file with mode: 0644]
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
llvm/test/tools/llvm-mca/ARM/simple-cortex-m33.s [new file with mode: 0644]