Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b5b78d

Browse files
fix: simplify && and || when the LHS is constant (#2947)
1 parent 9664c5b commit 9b5b78d

File tree

5 files changed

+82
-239
lines changed

5 files changed

+82
-239
lines changed

‎src/compiler.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4574,19 +4574,31 @@ export class Compiler extends DiagnosticEmitter {
45744574
}
45754575
leftExpr = this.convertExpression(leftExpr, leftType, commonType, false, left);
45764576
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+
45774582
rightExpr = this.convertExpression(rightExpr, rightType, commonType, false, right);
45784583
rightType = commonType;
45794584

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
45824595
expr = module.if(
4583-
this.makeIsTrueish(leftExpr,this.currentType,left),
4596+
condExpr,
45844597
rightExpr,
45854598
expr
45864599
);
4587-
4588-
// if not possible, tee left to a temp
45894600
} else {
4601+
// if not possible, tee left to a temp
45904602
let tempLocal = flow.getTempLocal(leftType);
45914603
if (!flow.canOverflow(leftExpr, leftType)) flow.setLocalFlag(tempLocal.index, LocalFlags.Wrapped);
45924604
if (flow.isNonnull(leftExpr, leftType)) flow.setLocalFlag(tempLocal.index, LocalFlags.NonNull);
@@ -4654,19 +4666,31 @@ export class Compiler extends DiagnosticEmitter {
46544666
let possiblyNull = leftType.is(TypeFlags.Nullable) && rightType.is(TypeFlags.Nullable);
46554667
leftExpr = this.convertExpression(leftExpr, leftType, commonType, false, left);
46564668
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+
46574674
rightExpr = this.convertExpression(rightExpr, rightType, commonType, false, right);
46584675
rightType = commonType;
46594676

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
46624687
expr = module.if(
4663-
this.makeIsTrueish(leftExpr,leftType,left),
4688+
condExpr,
46644689
expr,
46654690
rightExpr
46664691
);
4667-
4668-
// if not possible, tee left to a temp. local
46694692
} else {
4693+
// if not possible, tee left to a temp. local
46704694
let temp = flow.getTempLocal(leftType);
46714695
let tempIndex = temp.index;
46724696
if (!flow.canOverflow(leftExpr, leftType)) flow.setLocalFlag(tempIndex, LocalFlags.Wrapped);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /