Opt compiler: enhance gvn for commutative ops.
Change-Id: I415b50d58b30cab4ec38077be22373eb9598ec40
diff --git a/compiler/optimizing/gvn.cc b/compiler/optimizing/gvn.cc
index cb448c8..ea65dc0 100644
--- a/compiler/optimizing/gvn.cc
+++ b/compiler/optimizing/gvn.cc
@@ -299,8 +299,17 @@
// Save the next instruction in case `current` is removed from the graph.
HInstruction* next = current->GetNext();
if (current->CanBeMoved()) {
+ if (current->IsBinaryOperation() && current->AsBinaryOperation()->IsCommutative()) {
+ // For commutative ops, (x op y) will be treated the same as (y op x)
+ // after fixed ordering.
+ current->AsBinaryOperation()->OrderInputs();
+ }
HInstruction* existing = set->Lookup(current);
if (existing != nullptr) {
+ // This replacement doesn't make more OrderInputs() necessary since
+ // current is either used by an instruction that it dominates,
+ // which hasn't been visited yet due to the order we visit instructions.
+ // Or current is used by a phi, and we don't do OrderInputs() on a phi anyway.
current->ReplaceWith(existing);
current->GetBlock()->RemoveInstruction(current);
} else {