Replies: 1 comment
-
Optimizing jumps with comparison operationsThe current optimizations performed on language constructs that use jumps are not generic, nor always consistent due to how some constructs have edge cases different than the others e.g how The idea to solve this is to use a control flow graph to represent loops and conditions, and apply optimizations whenever certain patterns are found within the control flow graph. Here we define two types of instructions that handle the control flow: Let's see how this script would be translated: const a = Math.rand(10);
const b = Math.rand(10);
if(a < b) {
print("a < b");
} else {
print("a >= b");
}
printFlush();
wait(2); %0:
a = call Math.rand [10]
b = call Math.rand [10]
_t0 = a < b
break_if _t0 %1 %2
%1:
call print ["a < b"]
break %3
%2:
call print ["a >= b"]
break %3
%3:
call printFlush []
call wait [2]
exit Now, if we were to translate this intermediate representation as-is, we would obtain this code:
However, because
To make sure the generated code can be easily mapped to the source code by the user, the second option will be used. Although at first glance it might seem less efficient because it creates additional instructions, they can be trivially optimized away with the following steps: Invert the operationThis step is not exclusive to jump optimizations, it is instead used every time there is a comparison operation (except %0:
a = call Math.rand [10]
b = call Math.rand [10]
- _t0 = a < b
- _t1 = _t0 == false
- break_if _t1 %2 %1
+ _t0 = a >= b
+ break_if _t0 %2 %1 Merge the operation with the underlying jump when generating the mlog codeIf the input value of op rand a 10
op rand b 10
- op greaterThanEq _t0 a b
- jump %2 notEqual _t0 0
+ jump %2 greaterThanEq a b
%1:
print "a < b"
jump %3 always
%2:
print "a >= b"
%3:
printflush message1
wait 2 And the best part of this procedure is that it also works with |
Beta Was this translation helpful? Give feedback.
-
Prior work:
for
loops with constant test values #150if
statements that containbreak
orcontinue
#149This discussion contains ideas about the future intermediate representation, more specifically in this case, about how it will handle language constructs that are translated into conditional branches such as
if
,switch
,for
andwhile
.Beta Was this translation helpful? Give feedback.
All reactions