[PATCH] D44559: Wrong width of result of mul operation for x86

Andrew V. Tischenko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 16 05:53:48 PDT 2018


avt77 created this revision.
avt77 added reviewers: rjmccall, RKSimon.

GetExprRange in lib/Sema/SemaChecking.cpp returns wrong width for mul operation: for x86 it should be 2N bits for N * N.
This patch will close Bug 35409.


https://reviews.llvm.org/D44559

Files:
  lib/Sema/SemaChecking.cpp
  test/Sema/conversion.c
  test/SemaCXX/conversion.cpp


Index: test/SemaCXX/conversion.cpp
===================================================================
--- test/SemaCXX/conversion.cpp
+++ test/SemaCXX/conversion.cpp
@@ -298,3 +298,15 @@
   conditional_run_13(NULL);
 }
 }
+
+template<typename T>
+T mul_t(T a, T b) {
+  int c = a * b;
+  T d1 = c; // expected-warning{{implicit conversion loses integer precision: 'int' to 'char'}}
+  T d2 = a * b; // expected-warning{{implicit conversion loses integer precision: 'int' to 'char'}}
+  return d1 + d2;
+}
+
+char mul_t_test (char a, char b) {
+    return mul_t(a, b); // expected-note{{in instantiation of function template specialization 'mul_t<char>' requested here}}
+}
Index: test/Sema/conversion.c
===================================================================
--- test/Sema/conversion.c
+++ test/Sema/conversion.c
@@ -302,7 +302,7 @@
 }
 
 void test15(char c) {
-  c = c + 1 + c * 2;
+  c = c + 1 + c * 2; // expected-warning {{implicit conversion loses integer precision: 'int'}}
   c = (short) c + 1 + c * 2; // expected-warning {{implicit conversion loses integer precision}}
 }
 
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -8536,8 +8536,21 @@
       return meet;
     }
 
+    case BO_Mul: {
+      switch (C.getTargetInfo().getTriple().getArch()) {
+      case llvm::Triple::x86:
+      case llvm::Triple::x86_64: {
+        // The result width should be calculated in advance
+        unsigned opWidth = C.getIntWidth(GetExprType(E));
+        return IntRange(opWidth, false);
+      }
+      default:
+        // The default behavior is okay for these.
+        break;
+      }
+    }
+
     // The default behavior is okay for these.
-    case BO_Mul:
     case BO_Add:
     case BO_Xor:
     case BO_Or:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44559.138683.patch
Type: text/x-patch
Size: 1846 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180316/9f6e82dd/attachment.bin>


More information about the llvm-commits mailing list