On Tue, 12 Apr 2016, Hurugalawadi, Naveen wrote:

+/* Fold A * 10 == B * 10 into A == B.  */
+(for cmp (eq ne)
+ (simplify
+  (cmp (mult:c @0 @1) (mult:c @2 @1))
+  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
+       && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
+       && tree_expr_nonzero_p (@1))
+   (cmp @0 @2))))

(another case we could handle here (but that's for another time) is when TYPE_OVERFLOW_WRAPS and @1 is odd)

+/* Fold A * 10 < B * 10 into A < B.  */
+(for cmp (lt gt le ge)
+ (simplify
+  (cmp (mult:c @0 @1) (mult:c @2 @1))
+  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
+       && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
+       && tree_expr_nonzero_p (@1))
+   (if (tree_expr_nonnegative_p (@1))
+    (cmp @0 @2)

Up to here it looks good to me (maybe someone has a better suggestion
than tree_expr_nonzero_p && tree_expr_nonnegative_p? I think we should
handle at least INTEGER_CST and SSA_NAME with VRP, and it seems natural
to add a VRP check in those 2 functions, expr_not_equal_to in the same
file already has one).

+   (if (!tree_expr_nonnegative_p (@1))
+    (cmp @2 @0))))))

No, same issue as before. !tree_expr_nonnegative_p means that we don't
know for sure that @1 is >=0, so it might be that we know @1 is
negative, or it might be that we simply have no idea, you cannot deduce
anything from it.  Ideally, you would call tree_expr_nonpositive_p,
except that that function doesn't exist yet. So for now, I guess we
could restrict to something like (untested)

if (TREE_CODE (@1) == INTEGER_CST && wi::lt_p (@1, 0))

+int
+f (int a, int b)
+{
+  return a > b;
+}

+int
+f3 (int a, int b)
+{
+  return a * 0 <= b * 0;
+}

Not sure what you are testing with those 2. The second one is optimized
to 'true' before it even reaches this pattern. Multiplying by a third
variable 'c' (where the compiler has no idea what the sign of c is)
could be nicer.

Then you'll need to wait for a real reviewer to show up.

--
Marc Glisse

Reply via email to