@@ -3957,7 +3957,8 @@ void Compiler::optAssertionProp_RangeProperties(ASSERT_VALARG_TP assertions,
3957
3957
}
3958
3958
3959
3959
// First, analyze possible X ==/!= CNS assertions.
3960
- if (curAssertion->IsConstantInt32Assertion () && (curAssertion->op1 .vn == treeVN))
3960
+ if (curAssertion->IsConstantInt32Assertion () && (curAssertion->op1 .kind == O1K_LCLVAR) &&
3961
+ (curAssertion->op1 .vn == treeVN))
3961
3962
{
3962
3963
if ((curAssertion->assertionKind == OAK_NOT_EQUAL) && (curAssertion->op2 .u1 .iconVal == 0 ))
3963
3964
{
@@ -4295,6 +4296,44 @@ GenTree* Compiler::optAssertionPropGlobal_RelOp(ASSERT_VALARG_TP assertions, Gen
4295
4296
GenTree* op1 = tree->AsOp ()->gtOp1 ;
4296
4297
GenTree* op2 = tree->AsOp ()->gtOp2 ;
4297
4298
4299
+ // Can we fold "X relop 0" based on assertions?
4300
+ if (op2->IsIntegralConst (0 ) && tree->OperIsCmpCompare ())
4301
+ {
4302
+ bool isNonZero, isNeverNegative;
4303
+ optAssertionProp_RangeProperties (assertions, op1, &isNonZero, &isNeverNegative);
4304
+
4305
+ if (tree->OperIs (GT_GE, GT_LT) && isNeverNegative)
4306
+ {
4307
+ // Assertions: X >= 0
4308
+ //
4309
+ // X >= 0 --> true
4310
+ // X < 0 --> false
4311
+ newTree = tree->OperIs (GT_GE) ? gtNewTrue () : gtNewFalse ();
4312
+ }
4313
+ else if (tree->OperIs (GT_GT, GT_LE) && isNeverNegative && isNonZero)
4314
+ {
4315
+ // Assertions: X > 0
4316
+ //
4317
+ // X > 0 --> true
4318
+ // X <= 0 --> false
4319
+ newTree = tree->OperIs (GT_GT) ? gtNewTrue () : gtNewFalse ();
4320
+ }
4321
+ else if (tree->OperIs (GT_EQ, GT_NE) && isNonZero)
4322
+ {
4323
+ // Assertions: X != 0
4324
+ //
4325
+ // X != 0 --> true
4326
+ // X == 0 --> false
4327
+ newTree = tree->OperIs (GT_NE) ? gtNewTrue () : gtNewFalse ();
4328
+ }
4329
+
4330
+ if (newTree != tree)
4331
+ {
4332
+ newTree = gtWrapWithSideEffects (newTree, tree, GTF_ALL_EFFECT);
4333
+ return optAssertionProp_Update (newTree, tree, stmt);
4334
+ }
4335
+ }
4336
+
4298
4337
// Look for assertions of the form (tree EQ/NE 0)
4299
4338
AssertionIndex index = optGlobalAssertionIsEqualOrNotEqualZero (assertions, tree);
4300
4339
@@ -4315,7 +4354,6 @@ GenTree* Compiler::optAssertionPropGlobal_RelOp(ASSERT_VALARG_TP assertions, Gen
4315
4354
4316
4355
newTree = curAssertion->assertionKind == OAK_EQUAL ? gtNewIconNode (0 ) : gtNewIconNode (1 );
4317
4356
newTree = gtWrapWithSideEffects (newTree, tree, GTF_ALL_EFFECT);
4318
- newTree = fgMorphTree (newTree);
4319
4357
DISPTREE (newTree);
4320
4358
return optAssertionProp_Update (newTree, tree, stmt);
4321
4359
}
0 commit comments