Reviewers: Sven Panne,

Description:
[turbofan] Use appropriate type for NodeId.

Up until now we used int32_t for NodeId, but that was not ideal because
negative values are invalid for NodeId and we use it as an array index
for example in the NodeMarker class, where C++ compilers on x64 have to
generate code that does proper sign extension for the indices, which is
completely unnecessary.

[email protected]

Please review this at https://codereview.chromium.org/1178403004/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+38, -4 lines):
  M include/v8config.h
  M src/base/bits.h
  M src/compiler/graph.h
  M src/compiler/graph.cc
  M src/compiler/graph-reducer.h
  M src/compiler/node.h
  M test/unittests/base/bits-unittest.cc


Index: include/v8config.h
diff --git a/include/v8config.h b/include/v8config.h
index a8b54c18d83c83b6ccb7702d570799dabdd9b7f0..24b0e5ff3ef64d7ff11f30d495ff7953c0bc4f61 100644
--- a/include/v8config.h
+++ b/include/v8config.h
@@ -183,6 +183,7 @@
 //  V8_HAS_BUILTIN_POPCOUNT             - __builtin_popcount() supported
// V8_HAS_BUILTIN_SADD_OVERFLOW - __builtin_sadd_overflow() supported // V8_HAS_BUILTIN_SSUB_OVERFLOW - __builtin_ssub_overflow() supported +// V8_HAS_BUILTIN_UADD_OVERFLOW - __builtin_uadd_overflow() supported
 //  V8_HAS_DECLSPEC_ALIGN               - __declspec(align(n)) supported
 //  V8_HAS_DECLSPEC_DEPRECATED          - __declspec(deprecated) supported
 //  V8_HAS_DECLSPEC_NOINLINE            - __declspec(noinline) supported
@@ -221,6 +222,7 @@
 # define V8_HAS_BUILTIN_POPCOUNT (__has_builtin(__builtin_popcount))
# define V8_HAS_BUILTIN_SADD_OVERFLOW (__has_builtin(__builtin_sadd_overflow)) # define V8_HAS_BUILTIN_SSUB_OVERFLOW (__has_builtin(__builtin_ssub_overflow)) +# define V8_HAS_BUILTIN_UADD_OVERFLOW (__has_builtin(__builtin_uadd_overflow))

 # define V8_HAS_CXX11_ALIGNAS (__has_feature(cxx_alignas))
 # define V8_HAS_CXX11_STATIC_ASSERT (__has_feature(cxx_static_assert))
Index: src/base/bits.h
diff --git a/src/base/bits.h b/src/base/bits.h
index b237d62bcb1d6f47500a42ffe76bef510e2006d6..abcfd9a9eddce4a45671712d7abbb5da6f0d5fbf 100644
--- a/src/base/bits.h
+++ b/src/base/bits.h
@@ -236,6 +236,19 @@ int32_t SignedDiv32(int32_t lhs, int32_t rhs);
 int32_t SignedMod32(int32_t lhs, int32_t rhs);


+// UnsignedAddOverflow32(lhs,rhs,val) performs an unsigned summation of | lhs| +// and |rhs| and stores the result into the variable pointed to by |val| and
+// returns true if the unsigned summation resulted in an overflow.
+inline bool UnsignedAddOverflow32(uint32_t lhs, uint32_t rhs, uint32_t* val) {
+#if V8_HAS_BUILTIN_SADD_OVERFLOW
+  return __builtin_uadd_overflow(lhs, rhs, val);
+#else
+  *val = lhs + rhs;
+  return *val < (lhs | rhs);
+#endif
+}
+
+
 // UnsignedDiv32(lhs, rhs) divides |lhs| by |rhs| and returns the quotient
 // truncated to uint32. If |rhs| is zero, then zero is returned.
 inline uint32_t UnsignedDiv32(uint32_t lhs, uint32_t rhs) {
Index: src/compiler/graph-reducer.h
diff --git a/src/compiler/graph-reducer.h b/src/compiler/graph-reducer.h
index 21e0a84b99c86021556848e316a66f7c0e6d15c9..a8a907fa12731b29b0834d9fa207a565d371e6b2 100644
--- a/src/compiler/graph-reducer.h
+++ b/src/compiler/graph-reducer.h
@@ -19,7 +19,7 @@ class Node;

// NodeIds are identifying numbers for nodes that can be used to index auxiliary
 // out-of-line data associated with each node.
-typedef int32_t NodeId;
+typedef uint32_t NodeId;


 // Represents the result of trying to reduce a node in the graph.
Index: src/compiler/graph.cc
diff --git a/src/compiler/graph.cc b/src/compiler/graph.cc
index 193861187ba1f8aeec349c6787a7c4e133f55a5b..07bf2ba44eb7cdb068796d0ed179f628172c444c 100644
--- a/src/compiler/graph.cc
+++ b/src/compiler/graph.cc
@@ -53,7 +53,7 @@ Node* Graph::NewNode(const Operator* op, int input_count, Node** inputs,

 NodeId Graph::NextNodeId() {
   NodeId const id = next_node_id_;
-  CHECK(!base::bits::SignedAddOverflow32(id, 1, &next_node_id_));
+  CHECK(!base::bits::UnsignedAddOverflow32(id, 1, &next_node_id_));
   return id;
 }

Index: src/compiler/graph.h
diff --git a/src/compiler/graph.h b/src/compiler/graph.h
index 0b1277ca1dcb1ff0d16d3edafc40bffffc214d28..6b4c8a1bce18648e9674a9ebc9db0f8f9dd9c98e 100644
--- a/src/compiler/graph.h
+++ b/src/compiler/graph.h
@@ -26,7 +26,7 @@ typedef uint32_t Mark;

// NodeIds are identifying numbers for nodes that can be used to index auxiliary
 // out-of-line data associated with each node.
-typedef int32_t NodeId;
+typedef uint32_t NodeId;


 class Graph : public ZoneObject {
Index: src/compiler/node.h
diff --git a/src/compiler/node.h b/src/compiler/node.h
index 16b2aeeb1cc2b12e982ace299453f63645c5e8f7..08b775c1fff688376140e1316c236b141eca09b8 100644
--- a/src/compiler/node.h
+++ b/src/compiler/node.h
@@ -27,7 +27,7 @@ typedef uint32_t Mark;

// NodeIds are identifying numbers for nodes that can be used to index auxiliary
 // out-of-line data associated with each node.
-typedef int32_t NodeId;
+typedef uint32_t NodeId;


 // A Node is the basic primitive of graphs. Nodes are chained together by
Index: test/unittests/base/bits-unittest.cc
diff --git a/test/unittests/base/bits-unittest.cc b/test/unittests/base/bits-unittest.cc index 9caba8484eee0de05833a2aff1d4edcadc777435..3d17a050dbdef9dcb1618a564ad335c8227c0a24 100644
--- a/test/unittests/base/bits-unittest.cc
+++ b/test/unittests/base/bits-unittest.cc
@@ -255,6 +255,25 @@ TEST(Bits, SignedMod32) {
 }


+TEST(Bits, UnsignedAddOverflow32) {
+  uint32_t val = 0;
+  EXPECT_FALSE(UnsignedAddOverflow32(0, 0, &val));
+  EXPECT_EQ(0u, val);
+  EXPECT_TRUE(
+ UnsignedAddOverflow32(std::numeric_limits<uint32_t>::max(), 1u, &val));
+  EXPECT_EQ(std::numeric_limits<uint32_t>::min(), val);
+  EXPECT_TRUE(UnsignedAddOverflow32(std::numeric_limits<uint32_t>::max(),
+                                    std::numeric_limits<uint32_t>::max(),
+                                    &val));
+  TRACED_FORRANGE(uint32_t, i, 1, 50) {
+    TRACED_FORRANGE(uint32_t, j, 1, i) {
+      EXPECT_FALSE(UnsignedAddOverflow32(i, j, &val));
+      EXPECT_EQ(i + j, val);
+    }
+  }
+}
+
+
 TEST(Bits, UnsignedDiv32) {
   TRACED_FORRANGE(uint32_t, i, 0, 50) {
     EXPECT_EQ(0u, UnsignedDiv32(i, 0));


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to