- // Commute floating point binary operators, to canonicalize the order of their
- // operands. This can potentially expose more CSE opportunities, and makes
- // writing other transformations simpler.
- if (I->getType()->isFloatingPointTy() || I->getType()->isVectorTy()) {
-
- // FAdd and FMul can be commuted.
- unsigned Opcode = I->getOpcode();
- if (Opcode == Instruction::FMul || Opcode == Instruction::FAdd) {
- Value *LHS = I->getOperand(0);
- Value *RHS = I->getOperand(1);
- unsigned LHSRank = getRank(LHS);
- unsigned RHSRank = getRank(RHS);
-
- // Sort the operands by rank.
- if (RHSRank < LHSRank) {
- I->setOperand(0, RHS);
- I->setOperand(1, LHS);
- }
- }
+ // Commute binary operators, to canonicalize the order of their operands.
+ // This can potentially expose more CSE opportunities, and makes writing other
+ // transformations simpler.
+ if (I->isCommutative())
+ canonicalizeOperands(I);