@@ -4574,19 +4574,31 @@ export class Compiler extends DiagnosticEmitter {
4574
4574
}
4575
4575
leftExpr = this . convertExpression ( leftExpr , leftType , commonType , false , left ) ;
4576
4576
leftType = commonType ;
4577
+
4578
+ // This is sometimes needed to make the left trivial
4579
+ let leftPrecompExpr = module . runExpression ( leftExpr , ExpressionRunnerFlags . PreserveSideeffects ) ;
4580
+ if ( leftPrecompExpr ) leftExpr = leftPrecompExpr ;
4581
+
4577
4582
rightExpr = this . convertExpression ( rightExpr , rightType , commonType , false , right ) ;
4578
4583
rightType = commonType ;
4579
4584
4580
- // simplify if copying left is trivial
4581
- if ( expr = module . tryCopyTrivialExpression ( leftExpr ) ) {
4585
+ let condExpr = this . makeIsTrueish ( leftExpr , this . currentType , left ) ;
4586
+ let condKind = this . evaluateCondition ( condExpr ) ;
4587
+
4588
+ if ( condKind != ConditionKind . Unknown ) {
4589
+ // simplify if left is a constant
4590
+ expr = condKind == ConditionKind . True
4591
+ ? rightExpr
4592
+ : leftExpr ;
4593
+ } else if ( expr = module . tryCopyTrivialExpression ( leftExpr ) ) {
4594
+ // simplify if copying left is trivial
4582
4595
expr = module . if (
4583
- this . makeIsTrueish ( leftExpr , this . currentType , left ) ,
4596
+ condExpr ,
4584
4597
rightExpr ,
4585
4598
expr
4586
4599
) ;
4587
-
4588
- // if not possible, tee left to a temp
4589
4600
} else {
4601
+ // if not possible, tee left to a temp
4590
4602
let tempLocal = flow . getTempLocal ( leftType ) ;
4591
4603
if ( ! flow . canOverflow ( leftExpr , leftType ) ) flow . setLocalFlag ( tempLocal . index , LocalFlags . Wrapped ) ;
4592
4604
if ( flow . isNonnull ( leftExpr , leftType ) ) flow . setLocalFlag ( tempLocal . index , LocalFlags . NonNull ) ;
@@ -4654,19 +4666,31 @@ export class Compiler extends DiagnosticEmitter {
4654
4666
let possiblyNull = leftType . is ( TypeFlags . Nullable ) && rightType . is ( TypeFlags . Nullable ) ;
4655
4667
leftExpr = this . convertExpression ( leftExpr , leftType , commonType , false , left ) ;
4656
4668
leftType = commonType ;
4669
+
4670
+ // This is sometimes needed to make the left trivial
4671
+ let leftPrecompExpr = module . runExpression ( leftExpr , ExpressionRunnerFlags . PreserveSideeffects ) ;
4672
+ if ( leftPrecompExpr ) leftExpr = leftPrecompExpr ;
4673
+
4657
4674
rightExpr = this . convertExpression ( rightExpr , rightType , commonType , false , right ) ;
4658
4675
rightType = commonType ;
4659
4676
4660
- // simplify if copying left is trivial
4661
- if ( expr = module . tryCopyTrivialExpression ( leftExpr ) ) {
4677
+ let condExpr = this . makeIsTrueish ( leftExpr , this . currentType , left ) ;
4678
+ let condKind = this . evaluateCondition ( condExpr ) ;
4679
+
4680
+ if ( condKind != ConditionKind . Unknown ) {
4681
+ // simplify if left is a constant
4682
+ expr = condKind == ConditionKind . True
4683
+ ? leftExpr
4684
+ : rightExpr ;
4685
+ } else if ( expr = module . tryCopyTrivialExpression ( leftExpr ) ) {
4686
+ // otherwise, simplify if copying left is trivial
4662
4687
expr = module . if (
4663
- this . makeIsTrueish ( leftExpr , leftType , left ) ,
4688
+ condExpr ,
4664
4689
expr ,
4665
4690
rightExpr
4666
4691
) ;
4667
-
4668
- // if not possible, tee left to a temp. local
4669
4692
} else {
4693
+ // if not possible, tee left to a temp. local
4670
4694
let temp = flow . getTempLocal ( leftType ) ;
4671
4695
let tempIndex = temp . index ;
4672
4696
if ( ! flow . canOverflow ( leftExpr , leftType ) ) flow . setLocalFlag ( tempIndex , LocalFlags . Wrapped ) ;
0 commit comments