jit: Stop emitting some unnecessary instructions
authorHeikki Linnakangas <[email protected]>
Fri, 15 Nov 2024 08:06:36 +0000 (10:06 +0200)
committerHeikki Linnakangas <[email protected]>
Fri, 15 Nov 2024 08:06:36 +0000 (10:06 +0200)
In EEOP_BOOL_AND_STEP* and EEOP_BOOL_OR_STEP*, we emitted pointlesss
store instructions to store to resnull/resvalue values that were just
loaded from the same fields in the previous instructions. They will
surely get optimized away by LLVM if any optimizations are enabled,
but it's better to not emit them in the first place. In
EEOP_BOOL_NOT_STEP, similar story with resnull.

In EEOP_NULLIF, when it returns NULL, there was also a redundant store
to resvalue just after storing a 0 to it. The value of resvalue
doesn't matter when resnull is set, so in fact even storing the 0 is
unnecessary, but I kept that because we tend to do that for general
tidiness.

Author: Xing Guo <[email protected]>
Reviewed-by: Andreas Karlsson <[email protected]>
Discussion: https://www.postgresql.org/message-id/CACpMh%2BC%[email protected]

src/backend/jit/llvm/llvmjit_expr.c

index 0b3b5748ea28dc0baf976ae1279a7a8a46b75433..9cec17544b80667fb89c39d8672a0ff2c5713dbf 100644 (file)
@@ -720,11 +720,6 @@ llvm_compile_expr(ExprState *state)
                    v_boolnull = l_load(b, TypeStorageBool, v_resnullp, "");
                    v_boolvalue = l_load(b, TypeSizeT, v_resvaluep, "");
 
-                   /* set resnull to boolnull */
-                   LLVMBuildStore(b, v_boolnull, v_resnullp);
-                   /* set revalue to boolvalue */
-                   LLVMBuildStore(b, v_boolvalue, v_resvaluep);
-
                    /* check if current input is NULL */
                    LLVMBuildCondBr(b,
                                    LLVMBuildICmp(b, LLVMIntEQ, v_boolnull,
@@ -816,11 +811,6 @@ llvm_compile_expr(ExprState *state)
                    v_boolnull = l_load(b, TypeStorageBool, v_resnullp, "");
                    v_boolvalue = l_load(b, TypeSizeT, v_resvaluep, "");
 
-                   /* set resnull to boolnull */
-                   LLVMBuildStore(b, v_boolnull, v_resnullp);
-                   /* set revalue to boolvalue */
-                   LLVMBuildStore(b, v_boolvalue, v_resvaluep);
-
                    LLVMBuildCondBr(b,
                                    LLVMBuildICmp(b, LLVMIntEQ, v_boolnull,
                                                  l_sbool_const(1), ""),
@@ -875,21 +865,22 @@ llvm_compile_expr(ExprState *state)
            case EEOP_BOOL_NOT_STEP:
                {
                    LLVMValueRef v_boolvalue;
-                   LLVMValueRef v_boolnull;
                    LLVMValueRef v_negbool;
 
-                   v_boolnull = l_load(b, TypeStorageBool, v_resnullp, "");
+                   /* compute !boolvalue */
                    v_boolvalue = l_load(b, TypeSizeT, v_resvaluep, "");
-
                    v_negbool = LLVMBuildZExt(b,
                                              LLVMBuildICmp(b, LLVMIntEQ,
                                                            v_boolvalue,
                                                            l_sizet_const(0),
                                                            ""),
                                              TypeSizeT, "");
-                   /* set resnull to boolnull */
-                   LLVMBuildStore(b, v_boolnull, v_resnullp);
-                   /* set revalue to !boolvalue */
+
+                   /*
+                    * Store it back in resvalue.  We can ignore resnull here;
+                    * if it was true, it stays true, and the value we store
+                    * in resvalue doesn't matter.
+                    */
                    LLVMBuildStore(b, v_negbool, v_resvaluep);
 
                    LLVMBuildBr(b, opblocks[opno + 1]);
@@ -1615,7 +1606,6 @@ llvm_compile_expr(ExprState *state)
                    LLVMPositionBuilderAtEnd(b, b_argsequal);
                    LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
                    LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
-                   LLVMBuildStore(b, v_retval, v_resvaluep);
 
                    LLVMBuildBr(b, opblocks[opno + 1]);
                    break;