8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
authorroland
Fri, 29 Jan 2016 17:18:35 +0100
changeset 35838 a6e58eeeaa2e
parent 35837 7edbab7f12d0
child 35839 7149ce4e3c01
8137049: Code quality: reducing an trivial integer loop does not produce an optimal code Summary: canonicalized if shape not recognized by empty loop detection code Reviewed-by: kvn, shade
hotspot/src/share/vm/opto/loopTransform.cpp
--- a/hotspot/src/share/vm/opto/loopTransform.cpp	Mon Feb 08 18:52:03 2016 +0100
+++ b/hotspot/src/share/vm/opto/loopTransform.cpp	Fri Jan 29 17:18:35 2016 +0100
@@ -2404,15 +2404,25 @@
   if (needs_guard) {
     // Check for an obvious zero trip guard.
     Node* inctrl = PhaseIdealLoop::skip_loop_predicates(cl->in(LoopNode::EntryControl));
-    if (inctrl->Opcode() == Op_IfTrue) {
+    if (inctrl->Opcode() == Op_IfTrue || inctrl->Opcode() == Op_IfFalse) {
+      bool maybe_swapped = (inctrl->Opcode() == Op_IfFalse);
       // The test should look like just the backedge of a CountedLoop
       Node* iff = inctrl->in(0);
       if (iff->is_If()) {
         Node* bol = iff->in(1);
-        if (bol->is_Bool() && bol->as_Bool()->_test._test == cl->loopexit()->test_trip()) {
-          Node* cmp = bol->in(1);
-          if (cmp->is_Cmp() && cmp->in(1) == cl->init_trip() && cmp->in(2) == cl->limit()) {
-            needs_guard = false;
+        if (bol->is_Bool()) {
+          BoolTest test = bol->as_Bool()->_test;
+          if (maybe_swapped) {
+            test._test = test.commute();
+            test._test = test.negate();
+          }
+          if (test._test == cl->loopexit()->test_trip()) {
+            Node* cmp = bol->in(1);
+            int init_idx = maybe_swapped ? 2 : 1;
+            int limit_idx = maybe_swapped ? 1 : 2;
+            if (cmp->is_Cmp() && cmp->in(init_idx) == cl->init_trip() && cmp->in(limit_idx) == cl->limit()) {
+              needs_guard = false;
+            }
           }
         }
       }