@@ -13435,28 +13435,6 @@ GenTree* Compiler::fgOptimizeCommutativeArithmetic(GenTreeOp* tree)
13435
13435
std::swap(tree->gtOp1, tree->gtOp2);
13436
13436
}
13437
13437
13438
- if (!optValnumCSE_phase)
13439
- {
13440
- GenTree* optimizedTree = nullptr;
13441
- if (tree->OperIs(GT_ADD))
13442
- {
13443
- optimizedTree = fgOptimizeAddition(tree);
13444
- }
13445
- else if (tree->OperIs(GT_MUL))
13446
- {
13447
- optimizedTree = fgOptimizeMultiply(tree);
13448
- }
13449
- else if (tree->OperIs(GT_AND))
13450
- {
13451
- optimizedTree = fgOptimizeBitwiseAnd(tree);
13452
- }
13453
-
13454
- if (optimizedTree != nullptr)
13455
- {
13456
- return optimizedTree;
13457
- }
13458
- }
13459
-
13460
13438
if (fgOperIsBitwiseRotationRoot(tree->OperGet()))
13461
13439
{
13462
13440
GenTree* rotationTree = fgRecognizeAndMorphBitwiseRotation(tree);
@@ -13477,7 +13455,36 @@ GenTree* Compiler::fgOptimizeCommutativeArithmetic(GenTreeOp* tree)
13477
13455
13478
13456
if (varTypeIsIntegralOrI(tree))
13479
13457
{
13458
+ genTreeOps oldTreeOper = tree->OperGet();
13480
13459
GenTreeOp* optimizedTree = fgMorphCommutative(tree->AsOp());
13460
+ if (optimizedTree != nullptr)
13461
+ {
13462
+ if (!optimizedTree->OperIs(oldTreeOper))
13463
+ {
13464
+ // "optimizedTree" could end up being a COMMA.
13465
+ return optimizedTree;
13466
+ }
13467
+
13468
+ tree = optimizedTree;
13469
+ }
13470
+ }
13471
+
13472
+ if (!optValnumCSE_phase)
13473
+ {
13474
+ GenTree* optimizedTree = nullptr;
13475
+ if (tree->OperIs(GT_ADD))
13476
+ {
13477
+ optimizedTree = fgOptimizeAddition(tree);
13478
+ }
13479
+ else if (tree->OperIs(GT_MUL))
13480
+ {
13481
+ optimizedTree = fgOptimizeMultiply(tree);
13482
+ }
13483
+ else if (tree->OperIs(GT_AND))
13484
+ {
13485
+ optimizedTree = fgOptimizeBitwiseAnd(tree);
13486
+ }
13487
+
13481
13488
if (optimizedTree != nullptr)
13482
13489
{
13483
13490
return optimizedTree;
@@ -13529,8 +13536,7 @@ GenTree* Compiler::fgOptimizeAddition(GenTreeOp* add)
13529
13536
13530
13537
// Fold (x + 0) - given it won't change the tree type to TYP_REF.
13531
13538
// TODO-Bug: this code will lose the GC-ness of a tree like "native int + byref(0)".
13532
- if (op2->IsCnsIntOrI() && (op2->AsIntCon()->IconValue() == 0) &&
13533
- ((add->TypeGet() == op1->TypeGet()) || !op1->TypeIs(TYP_REF)))
13539
+ if (op2->IsIntegralConst(0) && ((add->TypeGet() == op1->TypeGet()) || !op1->TypeIs(TYP_REF)))
13534
13540
{
13535
13541
if (op2->IsCnsIntOrI() && (op2->AsIntCon()->gtFieldSeq != nullptr) &&
13536
13542
(op2->AsIntCon()->gtFieldSeq != FieldSeqStore::NotAField()))
@@ -13544,9 +13550,8 @@ GenTree* Compiler::fgOptimizeAddition(GenTreeOp* add)
13544
13550
return op1;
13545
13551
}
13546
13552
13547
- // TODO-CQ: this transform preserves VNs and can be enabled outside global morph.
13548
13553
// Note that these transformations are legal for floating-point ADDs as well.
13549
- if (opts.OptimizationEnabled() && fgGlobalMorph )
13554
+ if (opts.OptimizationEnabled())
13550
13555
{
13551
13556
// - a + b = > b - a
13552
13557
// ADD((NEG(a), b) => SUB(b, a)
@@ -13655,6 +13660,13 @@ GenTree* Compiler::fgOptimizeMultiply(GenTreeOp* mul)
13655
13660
return mul;
13656
13661
}
13657
13662
13663
+ #ifdef TARGET_XARCH
13664
+ // Should we try to replace integer multiplication with lea/add/shift sequences?
13665
+ bool mulShiftOpt = compCodeOpt() != SMALL_CODE;
13666
+ #else // !TARGET_XARCH
13667
+ bool mulShiftOpt = false;
13668
+ #endif // !TARGET_XARCH
13669
+
13658
13670
size_t abs_mult = (mult >= 0) ? mult : -mult;
13659
13671
size_t lowestBit = genFindLowestBit(abs_mult);
13660
13672
bool changeToShift = false;
@@ -13697,7 +13709,7 @@ GenTree* Compiler::fgOptimizeMultiply(GenTreeOp* mul)
13697
13709
op2->AsIntConCommon()->SetIconValue(genLog2(abs_mult));
13698
13710
changeToShift = true;
13699
13711
}
13700
- else if ((lowestBit > 1) && jitIsScaleIndexMul(lowestBit) && optAvoidIntMult( ))
13712
+ else if (mulShiftOpt && (lowestBit > 1) && jitIsScaleIndexMul(lowestBit))
13701
13713
{
13702
13714
int shift = genLog2(lowestBit);
13703
13715
ssize_t factor = abs_mult >> shift;
0 commit comments