Skip to content

Commit 9d4aad3

Browse files
committed
Extracted 'generateBooleanOperation'
Generate magic instruction for '&&, ||' in condition as well
1 parent 4279da1 commit 9d4aad3

File tree

5 files changed

+76
-78
lines changed

5 files changed

+76
-78
lines changed

compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetControlFlowProcessor.java

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -335,29 +335,8 @@ public void visitBinaryExpressionVoid(@NotNull JetBinaryExpression expression, C
335335

336336
JetExpression left = expression.getLeft();
337337
JetExpression right = expression.getRight();
338-
if (operationType == ANDAND) {
339-
generateInstructions(left, IN_CONDITION);
340-
Label resultLabel = builder.createUnboundLabel();
341-
builder.jumpOnFalse(resultLabel, expression, builder.getBoundValue(left));
342-
if (right != null) {
343-
generateInstructions(right, IN_CONDITION);
344-
}
345-
builder.bindLabel(resultLabel);
346-
if (!context.inCondition()) {
347-
predefinedOperation(expression, AND);
348-
}
349-
}
350-
else if (operationType == OROR) {
351-
generateInstructions(left, IN_CONDITION);
352-
Label resultLabel = builder.createUnboundLabel();
353-
builder.jumpOnTrue(resultLabel, expression, builder.getBoundValue(left));
354-
if (right != null) {
355-
generateInstructions(right, IN_CONDITION);
356-
}
357-
builder.bindLabel(resultLabel);
358-
if (!context.inCondition()) {
359-
predefinedOperation(expression, OR);
360-
}
338+
if (operationType == ANDAND || operationType == OROR) {
339+
generateBooleanOperation(expression);
361340
}
362341
else if (operationType == EQ) {
363342
visitAssignment(left, getDeferredValue(right), expression);
@@ -402,6 +381,27 @@ else if (operationType == ELVIS) {
402381
}
403382
}
404383

384+
private void generateBooleanOperation(JetBinaryExpression expression) {
385+
IElementType operationType = expression.getOperationReference().getReferencedNameElementType();
386+
JetExpression left = expression.getLeft();
387+
JetExpression right = expression.getRight();
388+
389+
Label resultLabel = builder.createUnboundLabel();
390+
generateInstructions(left, IN_CONDITION);
391+
if (operationType == ANDAND) {
392+
builder.jumpOnFalse(resultLabel, expression, builder.getBoundValue(left));
393+
}
394+
else {
395+
builder.jumpOnTrue(resultLabel, expression, builder.getBoundValue(left));
396+
}
397+
if (right != null) {
398+
generateInstructions(right, IN_CONDITION);
399+
}
400+
builder.bindLabel(resultLabel);
401+
JetControlFlowBuilder.PredefinedOperation operation = operationType == ANDAND ? AND : OR;
402+
builder.predefinedOperation(expression, operation, elementsToValues(Arrays.asList(left, right)));
403+
}
404+
405405
private Function0<PseudoValue> getValueAsFunction(final PseudoValue value) {
406406
return new Function0<PseudoValue>() {
407407
@Override
@@ -421,12 +421,6 @@ public PseudoValue invoke() {
421421
};
422422
}
423423

424-
private void predefinedOperation(JetBinaryExpression expression, JetControlFlowBuilder.PredefinedOperation operation) {
425-
builder.predefinedOperation(
426-
expression, operation, elementsToValues(Arrays.asList(expression.getLeft(), expression.getRight()))
427-
);
428-
}
429-
430424
private void generateBothArgumentsAndMark(JetBinaryExpression expression) {
431425
JetExpression left = JetPsiUtil.deparenthesize(expression.getLeft());
432426
if (left != null) {

compiler/testData/cfg/expressions/LazyBooleans.instructions

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,48 +40,50 @@ L3:
4040
r(3) -> <v6>
4141
mark(if (a && b) 5 else 6)
4242
r(a) -> <v7>
43-
jf(L4|<v7>) NEXT:[jf(L5), r(b) -> <v8>]
43+
jf(L4|<v7>) NEXT:[magic(a && b|<v7>, <v8>) -> <v9>, r(b) -> <v8>]
4444
r(b) -> <v8>
4545
L4:
46-
jf(L5) NEXT:[r(6) -> <v10>, r(5) -> <v9>] PREV:[jf(L4|<v7>), r(b) -> <v8>]
47-
r(5) -> <v9>
48-
jmp(L6) NEXT:[merge(if (a && b) 5 else 6|<v9>, <v10>) -> <v11>]
46+
magic(a && b|<v7>, <v8>) -> <v9> PREV:[jf(L4|<v7>), r(b) -> <v8>]
47+
jf(L5|<v9>) NEXT:[r(6) -> <v11>, r(5) -> <v10>]
48+
r(5) -> <v10>
49+
jmp(L6) NEXT:[merge(if (a && b) 5 else 6|<v10>, <v11>) -> <v12>]
4950
L5:
50-
r(6) -> <v10> PREV:[jf(L5)]
51+
r(6) -> <v11> PREV:[jf(L5|<v9>)]
5152
L6:
52-
merge(if (a && b) 5 else 6|<v9>, <v10>) -> <v11> PREV:[jmp(L6), r(6) -> <v10>]
53-
r(7) -> <v12>
53+
merge(if (a && b) 5 else 6|<v10>, <v11>) -> <v12> PREV:[jmp(L6), r(6) -> <v11>]
54+
r(7) -> <v13>
5455
mark(if (a || b) 8 else 9)
55-
r(a) -> <v13>
56-
jt(L7|<v13>) NEXT:[r(b) -> <v14>, jf(L8)]
57-
r(b) -> <v14>
56+
r(a) -> <v14>
57+
jt(L7|<v14>) NEXT:[r(b) -> <v15>, magic(a || b|<v14>, <v15>) -> <v16>]
58+
r(b) -> <v15>
5859
L7:
59-
jf(L8) NEXT:[r(9) -> <v16>, r(8) -> <v15>] PREV:[jt(L7|<v13>), r(b) -> <v14>]
60-
r(8) -> <v15>
61-
jmp(L9) NEXT:[merge(if (a || b) 8 else 9|<v15>, <v16>) -> <v17>]
60+
magic(a || b|<v14>, <v15>) -> <v16> PREV:[jt(L7|<v14>), r(b) -> <v15>]
61+
jf(L8|<v16>) NEXT:[r(9) -> <v18>, r(8) -> <v17>]
62+
r(8) -> <v17>
63+
jmp(L9) NEXT:[merge(if (a || b) 8 else 9|<v17>, <v18>) -> <v19>]
6264
L8:
63-
r(9) -> <v16> PREV:[jf(L8)]
65+
r(9) -> <v18> PREV:[jf(L8|<v16>)]
6466
L9:
65-
merge(if (a || b) 8 else 9|<v15>, <v16>) -> <v17> PREV:[jmp(L9), r(9) -> <v16>]
66-
r(10) -> <v18>
67+
merge(if (a || b) 8 else 9|<v17>, <v18>) -> <v19> PREV:[jmp(L9), r(9) -> <v18>]
68+
r(10) -> <v20>
6769
mark(if (a) 11)
68-
r(a) -> <v19>
69-
jf(L10|<v19>) NEXT:[read (Unit), r(11) -> <v20>]
70-
r(11) -> <v20>
71-
jmp(L11) NEXT:[r(12) -> <v21>]
70+
r(a) -> <v21>
71+
jf(L10|<v21>) NEXT:[read (Unit), r(11) -> <v22>]
72+
r(11) -> <v22>
73+
jmp(L11) NEXT:[r(12) -> <v23>]
7274
L10:
73-
read (Unit) PREV:[jf(L10|<v19>)]
75+
read (Unit) PREV:[jf(L10|<v21>)]
7476
L11:
75-
r(12) -> <v21> PREV:[jmp(L11), read (Unit)]
77+
r(12) -> <v23> PREV:[jmp(L11), read (Unit)]
7678
mark(if (a) else 13)
77-
r(a) -> <v22>
78-
jf(L12|<v22>) NEXT:[r(13) -> <v23>, read (Unit)]
79+
r(a) -> <v24>
80+
jf(L12|<v24>) NEXT:[r(13) -> <v25>, read (Unit)]
7981
read (Unit)
80-
jmp(L13) NEXT:[r(14) -> <v24>]
82+
jmp(L13) NEXT:[r(14) -> <v26>]
8183
L12:
82-
r(13) -> <v23> PREV:[jf(L12|<v22>)]
84+
r(13) -> <v25> PREV:[jf(L12|<v24>)]
8385
L13:
84-
r(14) -> <v24> PREV:[jmp(L13), r(13) -> <v23>]
86+
r(14) -> <v26> PREV:[jmp(L13), r(13) -> <v25>]
8587
L1:
8688
1 <END> NEXT:[<SINK>]
8789
error:

compiler/testData/cfg/expressions/LazyBooleans.values

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fun lazyBooleans(a : Boolean, b : Boolean) : Unit {
1717
14
1818
}
1919
---------------------
20-
a <v2> NEW()
20+
a <v2> NEW()
2121
1 <v3> NEW()
2222
{ 1 } <v3> COPY
2323
2 <v4> NEW()
@@ -26,23 +26,25 @@ if (a) { 1 } else { 2 }
2626
3 <v6> NEW()
2727
a <v7> NEW()
2828
b <v8> NEW()
29-
5 <v9> NEW()
30-
6 <v10> NEW()
31-
if (a && b) 5 else 6 <v11> NEW(<v9>, <v10>)
32-
7 <v12> NEW()
33-
a <v13> NEW()
34-
b <v14> NEW()
35-
8 <v15> NEW()
36-
9 <v16> NEW()
37-
if (a || b) 8 else 9 <v17> NEW(<v15>, <v16>)
38-
10 <v18> NEW()
39-
a <v19> NEW()
40-
11 <v20> NEW()
41-
if (a) 11 <v20> COPY
42-
12 <v21> NEW()
43-
a <v22> NEW()
44-
13 <v23> NEW()
45-
if (a) else 13 <v23> COPY
46-
14 <v24> NEW()
47-
{ if (a) { 1 } else { 2 } 3 if (a && b) 5 else 6 7 if (a || b) 8 else 9 10 if (a) 11 12 if (a) else 13 14 } <v24> COPY
29+
a && b <v9> NEW(<v7>, <v8>)
30+
5 <v10> NEW()
31+
6 <v11> NEW()
32+
if (a && b) 5 else 6 <v12> NEW(<v10>, <v11>)
33+
7 <v13> NEW()
34+
a <v14> NEW()
35+
b <v15> NEW()
36+
a || b <v16> NEW(<v14>, <v15>)
37+
8 <v17> NEW()
38+
9 <v18> NEW()
39+
if (a || b) 8 else 9 <v19> NEW(<v17>, <v18>)
40+
10 <v20> NEW()
41+
a <v21> NEW()
42+
11 <v22> NEW()
43+
if (a) 11 <v22> COPY
44+
12 <v23> NEW()
45+
a <v24> NEW()
46+
13 <v25> NEW()
47+
if (a) else 13 <v25> COPY
48+
14 <v26> NEW()
49+
{ if (a) { 1 } else { 2 } 3 if (a && b) 5 else 6 7 if (a || b) 8 else 9 10 if (a) 11 12 if (a) else 13 14 } <v26> COPY
4850
=====================

compiler/testData/diagnostics/tests/controlFlowAnalysis/deadCode/deadCodeDifferentExamples.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fun tf() : Int {
138138
}
139139

140140
fun failtest(a : Int) : Int {
141-
if (fail() || <!UNREACHABLE_CODE!>true<!>) <!UNREACHABLE_CODE!>{
141+
if (fail() <!UNREACHABLE_CODE!>|| true<!>) <!UNREACHABLE_CODE!>{
142142

143143
}<!>
144144
<!UNREACHABLE_CODE!>return 1<!>

idea/testData/checker/UnreachableCode.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fun tf() : Int {
136136
}
137137

138138
fun failtest(<warning>a</warning> : Int) : Int {
139-
if (fail() || <warning>true</warning>) <warning>{
139+
if (fail() <warning>|| true</warning>) <warning>{
140140

141141
}</warning>
142142
<warning>return 1</warning>

0 commit comments

Comments
 (0)