[PATCH] D132516: [NVPTX] SHL.64 $r, 31 cannot be converted to a mulwide.s32

Dmitry Vassiliev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 23 16:30:56 PDT 2022


slydiman created this revision.
slydiman added reviewers: tra, jchlanda.
slydiman added a project: LLVM.
Herald added subscribers: mattd, gchakrabarti, asavonic, hiraditya.
Herald added a project: All.
slydiman requested review of this revision.
Herald added subscribers: llvm-commits, jholewinski.

In order to convert to mulwide.s32, we compute the 2nd operand as MulWide.32 $r, (1 << 31).
(1 << 31) is interpreted as a negative number, and is not equivalent to the original instruction.
The code `int64_t r = (int64_t)a << 31;` incorrectly compiled to `mul.wide.s32 %rd7, %r1, -2147483648;`


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132516

Files:
  llvm/lib/Target/NVPTX/NVPTXInstrInfo.td
  llvm/test/CodeGen/NVPTX/mulwide.ll


Index: llvm/test/CodeGen/NVPTX/mulwide.ll
===================================================================
--- llvm/test/CodeGen/NVPTX/mulwide.ll
+++ llvm/test/CodeGen/NVPTX/mulwide.ll
@@ -90,3 +90,23 @@
   %val2 = mul i64 %val0, %val1
   ret i64 %val2
 }
+
+; OPT-LABEL: @shl30
+; NOOPT-LABEL: @shl30
+define i64 @shl30(i32 %a) {
+; OPT: mul.wide
+; NOOPT: shl.b64
+  %conv = sext i32 %a to i64
+  %shl = shl i64 %conv, 30
+  ret i64 %shl
+}
+
+; OPT-LABEL: @shl31
+; NOOPT-LABEL: @shl31
+define i64 @shl31(i32 %a) {
+; OPT-NOT: mul.wide
+; NOOPT-NOT: mul.wide
+  %conv = sext i32 %a to i64
+  %shl = shl i64 %conv, 31
+  ret i64 %shl
+}
Index: llvm/lib/Target/NVPTX/NVPTXInstrInfo.td
===================================================================
--- llvm/lib/Target/NVPTX/NVPTXInstrInfo.td
+++ llvm/lib/Target/NVPTX/NVPTXInstrInfo.td
@@ -732,15 +732,15 @@
 }]>;
 
 def Int5Const : PatLeaf<(imm), [{
-  // Check if 0 <= v < 32; only then will the result of (x << v) be an int32.
+  // Check if 0 <= v < 31; only then will the result of (x << v) be an int32.
   const APInt &v = N->getAPIntValue();
-  return v.sge(0) && v.slt(32);
+  return v.sge(0) && v.slt(31);
 }]>;
 
 def Int4Const : PatLeaf<(imm), [{
-  // Check if 0 <= v < 16; only then will the result of (x << v) be an int16.
+  // Check if 0 <= v < 15; only then will the result of (x << v) be an int16.
   const APInt &v = N->getAPIntValue();
-  return v.sge(0) && v.slt(16);
+  return v.sge(0) && v.slt(15);
 }]>;
 
 def SHL2MUL32 : SDNodeXForm<imm, [{


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132516.455006.patch
Type: text/x-patch
Size: 1526 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220823/831d0dda/attachment.bin>


More information about the llvm-commits mailing list