[PATCH] D31242: [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.

Phabricator via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 22 08:21:56 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL298512: [ARM] t2_so_imm_neg had a subtle bug in the conversion, and could trigger UB by… (authored by askrobov).

Changed prior to commit:
  https://reviews.llvm.org/D31242?vs=92639&id=92641#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D31242

Files:
  llvm/trunk/lib/Target/ARM/ARMInstrThumb2.td
  llvm/trunk/test/CodeGen/Thumb/ispositive.ll


Index: llvm/trunk/test/CodeGen/Thumb/ispositive.ll
===================================================================
--- llvm/trunk/test/CodeGen/Thumb/ispositive.ll
+++ llvm/trunk/test/CodeGen/Thumb/ispositive.ll
@@ -9,3 +9,12 @@
         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
+}
+
Index: llvm/trunk/lib/Target/ARM/ARMInstrThumb2.td
===================================================================
--- llvm/trunk/lib/Target/ARM/ARMInstrThumb2.td
+++ llvm/trunk/lib/Target/ARM/ARMInstrThumb2.td
@@ -111,7 +111,9 @@
 // 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<i32>, 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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31242.92641.patch
Type: text/x-patch
Size: 1180 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170322/4979cd63/attachment.bin>


More information about the llvm-commits mailing list