If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ...

2025/05/2712:01:34 technology 1285

In various programming languages, logic operator requires to short-circuit .

Take C language as an example:

if (p && p-x 0) p-x++;

When the pointer p is NULL, reading and writing the member variable p-x will cause a segfault, so if the condition p before && is NULL, you must skip the subsequent p-x.

can ensure the normal operation of the code.

If the language itself does not support short circuits of logical operators, then the code can only be written like this:

if (p) {

if (p-x 0) p-x++;

}

need to write one more if condition. The result of the

logical operator is only 0 or 1.

In programming, the logical operator can not only as the conditional expression of if/while/for, can also participate in normal operation .

if (i 0 && j 1 && k 2) return -1; // Conditional expression

n = i 0 && j 1 && k 2; // Normal operation

When optimizing to for logical operators, these and two situations must be considered at the same time.

The second case of . If short-circuits when calculating the first two conditions, the calculation result must be written to temporary variable corresponding to the last logical operator of . The calculation result of the last logical operator of

is the result of the entire expression and the result of assignment of n.

If it is just used as the conditional expression in the if statement, the result of the logical operation does not need to be written to the temporary variable , and it is enough to jump to the corresponding branch based on the comparison result. The syntax tree for

If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ... - DayDayNews

assignment operation

The syntax tree for the second case is shown in the figure. The temporary variable corresponding to the second && of is involved in the assignment operation.

If the short-circuit is short-circuited at i 0 or j 1, they set the value of the first && of : the compiler must set the operation result for the second && when short-circuits and jumps to , otherwise the result of n = ... will be wrong.

When gives logic operator an intermediate code , in fact, does not know how the result of will be used in the future [cover face]

can also be seen from the above figure that the = sign is above (if is also above). In fact, when processing the && operator, can only see 's two conditional expressions, cannot see the program logic of 's higher-level. If you have to look at higher-level logic, it will destroy the encapsulation of the module , making the code that traverses the syntax tree to generate intermediate code complex and ugly. The

scf framework here is:

If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ... - DayDayNews. First, set the result to temporary variable of all logical operators of , and put further optimization into in the future. The case of

If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ... - DayDayNews

if

's initial intermediate code is like this, you can see that each cmp or teq operation is followed by setcc.

where cmp is the size, the result can be, ==, =, =, =,!=.

teq is a comparison of whether it is 0 or not, and there are only 2 kinds of results: == 0, != 0. When

If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ... - DayDayNews

is assigned a value

. If there is a logical operator, after cmmp, it must follow teq to determine whether the logical result is 0.

For example, in the figure above:

cmmp i, 0

setgt v_8_16/&&

jle: teq v_8_16/&&

jz : assign v_8_6/n; v_8_25/&&

number represents row and column number , and v_8_16/&& represents temporary variable corresponding to the && operator in row 8 and column 16.

can be seen that in the initial intermediate code, the logical operator did not have "really short-circuit" : although the second comparison condition was skipped, the logical operation was not skipped. The reason why

is like this is because (when generating intermediate code) cannot determine whether the logical result is useful next.Since

cannot be determined, the compiler can only first. : When encountering this situation in the code, it will definitely be conservative.

So, whether it is programmer or mathematician , they are generally pessimist [cover face]

If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ... - DayDayNews, and further optimizes this problem when jumps to optimize .

If a jump target position is an absolute jump , it can be optimized to jump to , which is absolutely jumped.

if a jcc's destination is a jmp, then optimize its destination to the jmp's.

This is the basic optimization for jumps.

In fact, if false is set to the logical result before jumping, and the target of jumping is to detect this result , then detection result is also false [grin] This is another rule for jump optimization.

So, when cmp triggers jle (=), then setgt () between them is sure that is 0:

and then causes teq in the target position to be also 0,

and then causes setnz in the target position to be also 0,

and then causes jz in the target position to be triggered,

so, jle's address is the address of jz!

But before jle, you should also set the target variable of setnz. The setting command used is setgt: it is consistent with the condition that triggered the jump. The intermediate code after

If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ... - DayDayNews

jump optimization

can be seen from the figure above that after jump optimization , the teq operation of the test logic results , and the jz jump corresponding to teq also disappeared: the

logic operator was short-circuited by to the next assignment operation of .

assignment operation only uses the last temporary variable v_8_25.

sets the intermediate temporary variable v_8_16 to redundant . The next question is how to eliminate it.

If the logical operation is just a condition for if, then the last logical result is also an unnecessary .

If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ... - DayDayNews, analyzes the optimization of based on the active variable.

arithmetic operation will use the temporary variable of the last logical result. If condition does not use any temporary variable of the logical result. This can be reflected in the activity of the temporary variable of .

Just remove temporary variable from DAG.

If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ... - DayDayNews

final optimization result

only uses the temporary variable v_8_25. It can be seen that it is the exit active variable of the previous basic block and is retained.

If it is an if condition, because all temporary variables are not active , after DAG optimization , only is left to compare and jump [grin]

as shown in the figure below:

If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ... - DayDayNews

if final figure

above is the final figure of if condition, and the figure below is the figure after jump optimization and before DAG optimization.

can be seen that all setgts have been removed. Picture before

If the short circuit is short circuited when i > 0 or j > 1, they set the value of the first &&: the compiler must set the operation result for the second && when the short circuit jumps, otherwise the following n = ... - DayDayNews

if

technology Category Latest News