Optimize constant operand order.

Constant operands of commutative operations preferably go on the right
hand side to avoid requiring an extra register for two operand
instructions. Also, Subzero assumes constants in pointer arithmetic are
on the right hand side to consider optimizing it into an addressing
mode.

Change-Id: Ife5a471903d5f4bef0c19b6c908d75715f06bfec
Reviewed-on: https://swiftshader-review.googlesource.com/8548
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
Tested-by: Nicolas Capens <capn@google.com>
diff --git a/src/Reactor/SubzeroReactor.cpp b/src/Reactor/SubzeroReactor.cpp
index 21298f8..3187777 100644
--- a/src/Reactor/SubzeroReactor.cpp
+++ b/src/Reactor/SubzeroReactor.cpp
@@ -589,12 +589,31 @@
 		::basicBlock->appendInst(br);
 	}
 
+	static bool isCommutative(Ice::InstArithmetic::OpKind op)
+	{
+		switch(op)
+		{
+		case Ice::InstArithmetic::Add:
+		case Ice::InstArithmetic::Fadd:
+		case Ice::InstArithmetic::Mul:
+		case Ice::InstArithmetic::Fmul:
+		case Ice::InstArithmetic::And:
+		case Ice::InstArithmetic::Or:
+		case Ice::InstArithmetic::Xor:
+			return true;
+		default:
+			return false;
+		}
+	}
+
 	static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs)
 	{
 		assert(lhs->getType() == rhs->getType() || (llvm::isa<Ice::Constant>(rhs) && (op == Ice::InstArithmetic::Shl || Ice::InstArithmetic::Lshr || Ice::InstArithmetic::Ashr)));
 
+		bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op);
+
 		Ice::Variable *result = ::function->makeVariable(lhs->getType());
-		Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, lhs, rhs);
+		Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs);
 		::basicBlock->appendInst(arithmetic);
 
 		return V(result);