From 677bddb49c66987005264c262e36c40438dbf82a Mon Sep 17 00:00:00 2001 From: Artyom Skrobov Date: Wed, 22 Mar 2017 15:09:30 +0000 Subject: [PATCH] [ARM] t2_so_imm_neg had a subtle bug in the conversion, and could trigger UB by negating (int)-2147483648. By pure luck, none of the pre-existing tests triggered this; so I'm adding one. Summary: Thanks to Vitaly Buka for helping catch this. Reviewers: rengolin, jmolloy, efriedma, vitalybuka Subscribers: llvm-commits, aemerson Differential Revision: https://reviews.llvm.org/D31242 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298512 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/ARM/ARMInstrThumb2.td | 4 +++- test/CodeGen/Thumb/ispositive.ll | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/Target/ARM/ARMInstrThumb2.td b/lib/Target/ARM/ARMInstrThumb2.td index 557099590c3..45ab65580d4 100644 --- a/lib/Target/ARM/ARMInstrThumb2.td +++ b/lib/Target/ARM/ARMInstrThumb2.td @@ -111,7 +111,9 @@ def t2_so_imm_notSext : Operand, PatLeaf<(imm), [{ // t2_so_imm_neg - Match an immediate that is a negation of a t2_so_imm. def t2_so_imm_neg_asmoperand : AsmOperandClass { let Name = "T2SOImmNeg"; } def t2_so_imm_neg : Operand, PatLeaf<(imm), [{ - int64_t Value = -(int)N->getZExtValue(); + int64_t Value = N->getZExtValue(); + if (Value == 1LL<<31) return false; // INT_MIN cannot be negated + Value = -(int)Value; return Value && ARM_AM::getT2SOImmVal(Value) != -1; }], t2_so_imm_neg_XFORM> { let ParserMatchClass = t2_so_imm_neg_asmoperand; diff --git a/test/CodeGen/Thumb/ispositive.ll b/test/CodeGen/Thumb/ispositive.ll index 8d396878932..a9b2c139797 100644 --- a/test/CodeGen/Thumb/ispositive.ll +++ b/test/CodeGen/Thumb/ispositive.ll @@ -9,3 +9,12 @@ entry: ret i32 %1 } +define i32 @test2(i32 %X) { +entry: +; CHECK-LABEL: test2: +; CHECK: lsls r1, r1, #31 +; CHECK-NEXT: adds + %tmp1 = sub i32 %X, 2147483648 + ret i32 %tmp1 +} + -- 2.40.0