Loop Upper Bound Calculation Method Based on High-Precision Static Program Analysis

Through data flow analysis and recursive algorithms, the problem of inaccurate calculation of complex loop dependency expressions and nested loop upper bounds in the existing technology is solved, and high-precision loop upper bounds calculation is realized, and program execution efficiency is optimized.

CN115098162BActive Publication Date: 2025-07-22NANJING UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210655377.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-10
Publication Date
2025-07-22
Estimated Expiration
2042-06-10

AI Technical Summary

Technical Problem

The prior art is difficult to accurately calculate the loop upper bound of complex loop-dependent expressions and nested loops, resulting in the inability to effectively optimize program execution efficiency.

Method used

Data flow analysis and recursive algorithms are used to locate loop statements, analyze the data dependence relationship of loop variables, and recursively calculate the possible values of loop upper bound dependency expressions, and summarize the loop upper bound calculation methods of different loop modes.

Benefits of technology

High-precision loop upper bound calculations for complex loop dependency expressions and nested loops are realized, which can give a relatively accurate number of loop executions and optimize program execution efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115098162B_ABST
    Figure CN115098162B_ABST
Patent Text Reader

Abstract

The present invention discloses a method for calculating the upper bound of a loop based on high-precision static program analysis. Step 1: Locate the loop statements in the program and identify the loop control variables. Step 2: Analyze the data dependence relationship of the loop variables. Step 3: Analyze the possible values of the variables in the loop upper bound dependence expression. Step 4: Recursively calculate the possible values of the loop dependence expression, where for the ordinary variables in the expression, the method of Step 3 is called to analyze the specific values. Step 5: Calculate the final result according to the formula for the loop upper bound summarized by the loop structure. This method can analyze the actual parameters at the call point for the case where the loop-related variables come from formal parameters. This method can calculate the results of complex expressions formed by permutations and combinations of some frequently used basic expressions. This method can calculate the case where the loop-related variables of the inner loop are the outer loop control variables, and can give more accurate results for some nested loops with frequently used patterns.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of software engineering, and relates to static program analysis technology and loop upper bound analysis technology, and particularly relates to a method for calculating the loop upper bound based on high-precision static program analysis. Background Art

[0002] A loop structure is a flow structure that repeatedly executes a certain section of a program under certain conditions, and the program that is repeatedly executed is called the loop body. A loop statement consists of two parts: the loop body and the termination condition of the loop. The loop upper bound refers to the maximum number of executions of the loop statement. Judging the maximum value of the loop upper bound is very important for real-time systems. Excessive loop execution times will cause the program to execute slowly and the system to be unable to respond in real time. Therefore, many application scenarios require constraints on the loop upper bound. By static program analysis, estimating the loop upper bound of a program can analyze the maximum number of loop executions without running the program, and give an alarm for loop statements whose loop upper bound times exceed the threshold, so as to evaluate the running efficiency of the program without running the program or prompt the developer to optimize the loop statement.

[0003] The key technology used in the present invention is data flow analysis. Data flow analysis first needs to construct a control flow graph to reflect the execution flow of all basic blocks, and construct a function call graph to reflect the relationship of function calls. According to how variables propagate on the control flow graph and function call graph of the program, information is further collected, calculated, and analyzed.

[0004] The present invention discloses a flow-sensitive and context-sensitive loop upper bound analysis method based on static program analysis. First, locate the loop statements in the program and identify the loop control variables, then analyze the data dependency relationship of the loop variables, then analyze the possible values of the variables in the loop upper bound calculation dependency expression, then recursively calculate the possible values of the loop upper bound calculation dependency expression, and finally summarize the loop upper bound formula according to the loop structure and calculate the final result according to the value of the expression on which the loop upper bound depends. Summary of the Invention

[0005] Currently, for the analysis of loop statements, many focus on verifying the correctness of the program, such as verifying whether the loop statement is an infinite loop. For the calculation of the loop upper bound, currently, it can only be applied to the case where the loop dependency expression is a constant and the loop structure is a simple loop. For loop statements containing complex expressions and nested loops, it is impossible to calculate a relatively accurate loop upper bound result. To solve the above problems, the present invention uses data flow analysis and recursive algorithms to analyze and calculate the values of complex loop dependency expressions, and summarizes the loop upper bound calculation methods for different loop patterns. The present invention is specifically implemented through the following technical solutions:

[0006] A method for calculating the upper bound of a loop based on high-precision static program analysis, characterized by including the following steps:

[0007] Step 1): Locate the loop statements in the program and identify the loop control variables; Locate the loop statements in the program through static program analysis, identify the patterns of the loop statements, and extract the loop control variables in the loop statements of different patterns according to the syntax patterns of the for statement, while statement, and do-while statement.

[0008] Step 2): Analyze the data dependence relationship of the loop variables; According to the syntax patterns of the for statement, while statement, and do-while statement, analyze the data dependence relationship of the loop variables, and find out all the expressions on which the calculation of the loop upper bound depends, including analyzing the loop initialization expression, loop termination expression, and loop increment expression.

[0009] Step 3): Analyze the possible values of the variables in the loop upper bound dependence expression; Perform intra-procedural and inter-procedural data flow analysis, analyze the possible values of basic type variables, and call the method of the next step, that is, Step 4), to calculate when the value of the expression needs to be calculated during the data flow analysis process.

[0010] Step 4): Recursively calculate the possible values of the loop dependence expression; For ordinary variables in the expression, call the method of Step 3) to analyze the specific values.

[0011] Step 5): Calculate the final result according to the formula for summarizing the loop upper bound based on the loop structure; According to the syntax patterns of the for statement, while statement, and do-while statement, use the method of Step 4) to analyze and calculate the possible values of the loop upper bound dependence expression, and summarize and calculate the formula for the loop upper bound and calculate the final result for simple loops, nested loops, and chained loops respectively.

[0012] The above method for calculating the upper bound of a loop based on high-precision static program analysis, characterized in that the specific steps of Step 1) for locating the loop statements in the program and identifying the loop control variables include the following steps:

[0013] Step 11): Convert the program source code into an abstract syntax tree as the input, and construct the control flow graph and function call graph of the program on the basis of the abstract syntax tree; Traverse the statements in the control flow graph, and perform further analysis when traversing to the for loop statement, while loop, or do-while loop statement.

[0014] Step 12): For a for loop statement in the form of for(initial expression; loop condition; increment expression){loop body;}; if the for statement has an initial expression, extract the initialized variable or the left value of the assignment expression as the loop control variable; if the for statement has a loop condition and the loop condition is a relational expression, extract the left value of the relational expression as the loop control variable; if the for statement has an increment expression, extract the variable with increment or decrement or the left value of the assignment statement as the loop increment expression; if none of them exist, traverse the loop body to find the changing variables, including the variables in increment or decrement statements or the left values in compound assignment statements.

[0015] Step 13): For a while loop statement in the form of while(loop condition){loop body}; if the loop condition is a relational expression, extract the left value of the expression as the loop control variable; if the loop condition is a single variable, then this variable is the loop control variable.

[0016] Step 14): For a do-while loop statement in the form of do{loop body;}while(loop condition); if the loop condition is a relational expression, extract the left value of the expression as the loop control variable; if the loop condition is a single variable, then this variable is the loop control variable.

[0017] The above method for calculating the loop upper bound based on high-precision static program analysis is characterized in that the analysis of the data dependence relationship of the loop variable in step 2) specifically includes the following steps:

[0018] Step 21): For a for loop statement, if the loop control variable is initialized or assigned in the for loop initial expression, directly obtain the right value of the initialization statement or assignment statement as the loop initial expression; otherwise, call the method in step 3) for data flow analysis to obtain the value of the loop control variable before entering the loop; if the for statement has a loop condition and the loop condition is a relational expression, extract the right value of the relational expression as the loop termination expression; otherwise, traverse the loop body to check if there is a break or return statement, and extract the right value in the conditional expression of the if statement where it is located as the loop termination expression; according to the loop increment statement located in step 1), if the increment statement is in the form of an increment or decrement expression, set the loop increment value to 1; if the increment statement is in the form of a compound assignment statement, extract the right value of the assignment statement as the loop increment expression.

[0019] Step 22): For the while loop statement; call the method in step 3) to obtain the value of the loop control variable before entering the loop through data flow analysis, and this value is the initial value of the loop for the loop statement; if the loop condition is a relational expression, extract the right value of the expression as the loop termination expression. If the loop condition is a single variable, traverse the loop body to find whether there is a break or return statement, and extract the right value in the conditional expression of the if statement where it is located as the loop termination expression; analyze the loop body to find statements that update the value of the loop control variable. For increment and decrement statements, set the loop increment value to 1. For compound assignment statements, extract its right value as the loop increment expression;

[0020] Step 23): For the do-while loop statement, the method of extracting the loop initial expression, loop termination expression, and loop increment expression is the same as that for the while statement.

[0021] The above loop upper bound calculation method based on high-precision static program analysis is characterized in that the analysis of the possible values of variables in the loop upper bound dependence expression in step 3) specifically includes the following steps:

[0022] Step 31): Define data flow conversion functions for initialization statements, assignment statements, compound assignment statements, increment and decrement statements, and loop statements respectively; its parameters are the statement to be analyzed and the value of the variable before executing the statement, and the return value of the function is the value of the variable after executing the statement;

[0023] Step 32): For the data flow conversion functions of initialization statements and assignment statements, call the method in step 4) to calculate the right value of the initialization statement or assignment statement, and use this value as the post-value of the execution statement;

[0024] Step 33): For the data flow conversion function of compound assignment statements, call the method in step 4) to calculate the right value of the compound assignment statement, and perform operations of +=, -=, / =, *= on the pre-value according to the type of the operator, and use the operation result as the post-value of the execution statement;

[0025] Step 34): For the data flow conversion function of increment and decrement statements, according to the type of the unary operator, add one or subtract one from the pre-value as the value after the execution statement;

[0026] Step 35): For the data flow conversion function of loop statements, it is necessary to perform data flow analysis on the statements in the loop body, and use the post-value after each execution of the loop body as the pre-value for the next loop, and execute the loop upper bound times of the current loop in this way; since all loop statements are analyzed sequentially, the current loop has already undergone loop upper bound analysis and the loop upper bound result has been recorded;

[0027] Step 36): For the case where a variable is initialized within a function, perform in-procedure data flow analysis; traverse the control flow graph of the function body, analyze whether there is a variable to be analyzed in the statement, and if so, pass it to the corresponding data flow transformation function according to the statement type to analyze the post-value of the variable;

[0028] Step 37): For the case where a variable comes from a formal parameter, perform inter-procedure data flow analysis; according to the function call graph, find all callers of the current function and the function call points, match the actual parameters at the function call points according to the position of the variable in the formal parameter; call the above Step 3) to recursively analyze the possible maximum or minimum value of the actual parameter, and pass the analysis result to the formal parameter; then perform in-procedure data flow analysis on the variable.

[0029] The above method for calculating the loop upper bound based on high-precision static program analysis is characterized in that the step 4) of recursively calculating the possible value of the loop dependence expression specifically includes the following steps:

[0030] Step 41): For ordinary variables, call the method in the above Step 3) to analyze the possible value of the variable;

[0031] Step 42): For a binary operation expression, if the expression is an arithmetic operation, recursively call the above Step 4) to calculate the left value and right value of the expression, and perform corresponding addition, subtraction, multiplication, division, and remainder operations on the left value and right value of the expression according to the operator type; if the expression is a relational operation, recursively call the above Step 4) to calculate the left value and right value of the expression, and perform corresponding equality, inequality, greater than, less than, greater than or equal to, and less than or equal to relational judgments on the left value and right value of the expression to obtain the final result;

[0032] Step 43): For a ternary operation expression in the form of cond? x:y, recursively call the above Step 4) to calculate the condition cond. If the value of cond is true, recursively call the above Step 4) to calculate the value of x and return; otherwise, recursively call the above Step 4) to calculate the value of y and return;

[0033] Step 44): For a function call expression, locate the function body of the function and traverse the control flow graph of the function body to obtain the return expression of the function, and recursively call the above Step 4) to calculate the value of the expression and return.

[0034] The above method for calculating the loop upper bound based on high-precision static program analysis is characterized in that the step 5) of calculating the final result according to the loop structure induction loop upper bound formula specifically includes the following steps:

[0035] Step 51): According to the syntax patterns of the for statement, while statement, and do-while statement, call the method in step 4) to analyze the possible values of the initial expression of the loop control variable and the increment expression of the loop control variable;

[0036] Step 52): For a simple loop, calculate the number of loop iterations of the current loop according to the formula

[0037] loop_upbound = |end_value - init_value| / increment_value; where loop_upbound is the result of the loop upper bound, end_value is the termination value of the loop control variable, init_value is the initial value of the loop control variable, and increment_value is the increment value of the loop control variable;

[0038] Step 53): For nested loops, the total number of iterations is equal to the sum of the number of inner loop iterations for each value of the outer loop variable; considering the case where the initial expression or termination expression of the inner loop is the outer loop control variable, for the case where the initial expression of the inner loop is variable, if the inner loop is incrementing, the nested loop calculation formula is If the inner loop is decrementing, the nested loop calculation formula is where i is the initial expression of the inner loop, out_init is the initial value of the outer loop, out_end is the termination value of the outer loop, and in_end is the termination value of the inner loop; for the case where the termination expression of the inner loop is variable, if the inner loop is incrementing, the nested loop calculation formula is If the inner loop is decrementing, the nested loop calculation formula is where i is the termination expression of the inner loop, and in_init is the initial value of the inner loop;

[0039] Step 54): For chained loops, if there is no dependency between the loops, analyze each loop statement independently according to the method of analyzing simple loops or nested loops; if there is a dependency between the loops, that is, the upper bound of the subsequent loop depends on the variable updated in the previous loop, the method is to analyze the loop statements sequentially and record the upper bound of the loop statements that have been analyzed, so as to obtain the data flow conversion function of the loop statement. Specifically, take the post-value after each execution of the loop body as the pre-value of the next loop, and execute the loop upper bound times of the current loop to obtain the post-value of the loop statement; take this value as the pre-value of the next loop-dependent variable to analyze the upper bound of the next loop;

[0040] Step 55): This method takes into account the basic expressions with high usage frequencies. For cases where the expression status is not considered or there are variables from external inputs and the definite value of the expression cannot be calculated, the expression string and the information obtained during the analysis process are output.

[0041] The above method for calculating the loop upper bound based on high-precision static program analysis is characterized in that: in the specific steps of analyzing the possible values of variables in the loop upper bound dependency expression in step 3), interprocedural data flow analysis is considered, and for the case where loop-related variables come from formal parameters, the actual parameters at the call point can be analyzed.

[0042] The above method for calculating the loop upper bound based on high-precision static program analysis is characterized in that: in the specific steps of recursively calculating the possible values of the loop dependency expression in step 4), the case where the loop-related expression is a complex expression is considered, and the result of a complex expression formed by permutation and combination of some basic expressions with high usage frequencies is calculated.

[0043] The above method for calculating the loop upper bound based on high-precision static program analysis is characterized in that: in the specific steps of calculating the final result according to the loop upper bound formula by summarizing the loop structure in step 5), the calculation method of the loop upper bound where the loop-related variables in the inner loop are the control variables of the outer loop is summarized, and relatively accurate results can be given for some nested loops with high usage frequency patterns; a high-precision method for calculating the loop upper bound is also given for chained loops with dependencies between loops.

[0044] By adopting the above technical solutions, the present invention can specifically obtain the following beneficial effects:

[0045] 1. This method considers interprocedural data flow analysis, and for the case where loop-related variables come from formal parameters, the actual parameters at the call point can be analyzed.

[0046] 2. This method considers the case where the loop-related expression is a complex expression, and the result of a complex expression formed by permutation and combination of some basic expressions with high usage frequencies can be calculated.

[0047] 3. This method considers the case where the loop-related variables in the inner loop are the control variables of the outer loop in nested loops, and relatively accurate results can be given for some nested loops with high usage frequency patterns. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] Figure 1 is the schematic diagram of the method for calculating the loop upper bound based on high-precision static program analysis of the present invention.

[0049] Figure 2 is the flow chart of the method for calculating the loop upper bound based on high-precision static program analysis of the present invention. Detailed implementation manners

[0050] To enable those skilled in the art to better understand the present invention, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0051] The loop upper bound calculation method based on high-precision static program analysis in this embodiment, the schematic diagram and flowchart are as Figure 1 and Figure 2 shown, and the specific steps are as follows:

[0052] Step 1): Locate the loop statements in the program through static program analysis. According to the syntax patterns of the for statement, while statement, and do-while statement, extract the loop control variables in loop statements of different patterns.

[0053] The specific steps of the said Step 1) include the following steps:

[0054] Step 11): Convert the program source code into an abstract syntax tree as the input, and construct the control flow graph and function call graph of the program based on the abstract syntax tree. Traverse the statements in the control flow graph, and perform further analysis when traversing a for loop statement, a while loop, or a do-while loop statement;

[0055] Step 12): For a for loop statement, in the form of for(initial expression; loop condition; increment expression){loop body;}. If the for statement has an initial expression, extract the initialization variable or the left value of the assignment expression as the loop control variable; if the for statement has a loop condition and the loop condition is a relational expression, extract the left value of the relational expression as the loop control variable; if the for statement has an increment expression, extract the variable of self-increment or self-decrement or extract the left value of the assignment statement as the loop increment expression; if none of the three exist, then traverse the loop body to find the changing variables, including the variables in the self-increment and self-decrement statements or the left value in the compound assignment statement.

[0056] Step 13): For a while loop statement, in the form of while(loop condition){loop body}. If the loop condition is a relational expression, extract the left value of the expression as the loop control variable, and if the loop condition is a single variable, then this variable is the loop control variable.

[0057] Step 14): For a do-while loop statement, in the form of do{loop body;}while(loop condition); if the loop condition is a relational expression, extract the left value of the expression as the loop control variable, and if the loop condition is a single variable, then this variable is the loop control variable.

[0058] Step 2): According to the syntax patterns of the for statement, while statement, and do-while statement, analyze the data dependence relationships of variables, and find all the expressions on which the loop upper bound analysis depends, including analyzing the loop initialization expression, loop termination expression, and loop increment expression.

[0059] The specific steps of step 2) are as follows:

[0060] Step 21): For a for loop statement, if the loop control variable is initialized or assigned in the for loop initialization expression, directly obtain the right value of the initialization statement or assignment statement as the loop initialization expression; otherwise, call the method in the next step, i.e., step 3), for data flow analysis to obtain the value of the loop control variable before entering the loop. If the for statement has a loop condition and the loop condition is a relational expression, extract the right value of the relational expression as the loop termination expression; otherwise, traverse the loop body to find whether there is a break or return statement, and extract the right value in the conditional expression of the if statement where it is located as the loop termination expression. According to the loop increment statement located in step 1, if the increment statement form is an increment or decrement expression, set the loop increment value to 1; if the increment statement form is a compound assignment statement, extract the right value of the assignment statement as the loop increment expression.

[0061] Step 22): For a while loop statement. Call the method in step 3) to obtain the value of the loop control variable before entering the loop through data flow analysis, and this value is the loop initial value of the loop statement. If the loop condition is a relational expression, extract the right value of the expression as the loop termination expression; if the loop condition is a single variable, traverse the loop body to find whether there is a break or return statement, and extract the right value in the conditional expression of the if statement where it is located as the loop termination expression. Analyze the loop body to find the statement that updates the value of the loop control variable. For increment and decrement statements, set the loop increment value to 1; for compound assignment statements, extract their right values as the loop increment expressions.

[0062] Step 23): For a do-while loop statement, the method of extracting the loop initialization expression, loop termination expression, and loop increment expression is the same as that of the while statement.

[0063] Step 3): Perform intra-procedural and inter-procedural data flow analysis, analyze the possible values of basic type variables, and call the method in the next step, i.e., step 4), to calculate the value of the expression during the data flow analysis process.

[0064] The specific steps of step 3) are as follows:

[0065] Step 31): Define data flow conversion functions for initialization statements, assignment statements, compound assignment statements, increment and decrement statements, and loop statements respectively. Its parameters are the statement to be analyzed and the value of the variable before the execution of the statement, and the return value of the function is the value of the variable after the execution of the statement.

[0066] Step 32): For the data flow conversion functions of initialization statements and assignment statements, calculate the right value of the initialization statement or assignment statement by calling the method in Step 4), and use this value as the value after the execution of the statement.

[0067] Step 33): For the data flow conversion function of compound assignment statements, calculate the right value of the compound assignment statement by calling the method in Step 4), perform operations of +=, -=, / =, *= on the previous value according to the type of the operator, and use the operation result as the value after the execution of the statement.

[0068] Step 34): For the data flow conversion function of increment and decrement statements, add one or subtract one from the previous value as the value after the execution of the statement according to the type of the unary operator.

[0069] Step 35): For the data flow conversion function of loop statements, it is necessary to perform data flow analysis on the statements in the loop body, use the value after each execution of the loop body as the previous value for the next loop, and execute the loop upper bound times of the current loop in this way. Since this method analyzes all loop statements sequentially, the current loop has already undergone loop upper bound analysis and the loop upper bound result has been recorded.

[0070] Step 36): For the case where a variable is initialized within a function, perform in-procedure data flow analysis. Traverse the control flow graph of the function body, analyze whether there is a variable to be analyzed in the statement, and if so, pass it to the corresponding data flow conversion function according to the statement type to analyze the value of the variable after execution.

[0071] Step 37): For the case where a variable comes from a formal parameter, perform inter-procedure data flow analysis. According to the function call graph, find all callers of the current function and the function call points, match the actual parameters at the function call points according to the position of the variable in the formal parameter. Call Step 3) to recursively analyze the possible maximum or minimum value of the actual parameter, and pass the analysis result to the formal parameter. Then perform in-procedure data flow analysis on this variable.

[0072] Step 4): Recursively analyze and calculate the possible values of the expression. For ordinary variables in the expression, call the method in Step 3) to analyze the specific values.

[0073] The specific steps of Step 4) include the following steps:

[0074] Step 41): For ordinary variables, call the method in Step 3) to analyze the possible values of the variable;

[0075] Step 42): For a binary operation expression, if the expression is an arithmetic operation, recursively call Step 4) to calculate the left value and right value of the expression, and perform corresponding addition, subtraction, multiplication, division, and remainder operations on the left value and right value of the expression according to the operator type; if the expression is a relational operation, recursively call Step 4) to calculate the left value and right value of the expression, and perform corresponding relational judgments of equality, inequality, greater than, less than, greater than or equal to, and less than or equal to on the left value and right value of the expression to obtain the final result;

[0076] Step 43): For a ternary operation expression in the form of cond? x:y, recursively call Step 4) to calculate the condition cond. If the value of cond is true, recursively call Step 4) to calculate the value of x and return it; otherwise, recursively call Step 4) to calculate the value of y and return it;

[0077] Step 44): For a function call expression, locate the function body of the function and traverse the control flow graph of the function body to obtain the return expression of the function, and recursively call Step 4) to calculate the value of the expression and return it.

[0078] Step 5): According to the syntax patterns of the for statement, while statement, and do-while statement, use the method in Step 4) to analyze and calculate the possible values of the loop upper bound dependency expression. Inductively calculate the formulas for the loop upper bounds for simple loops, nested loops, and chained loops respectively and calculate the final results.

[0079] The specific steps of Step 5) include the following steps:

[0080] Step 51): According to the syntax patterns of the for statement, while statement, and do-while statement, call the method in Step 4) to analyze the possible values of the loop control variable initial expression and the loop control variable increment expression;

[0081] Step 52): For a simple loop, according to the formula

[0082] loop_upbound = |end_value - init_value| / increment_value to calculate the number of loop iterations of the current loop. Where loop_upbound is the loop upper bound result, end_value is the loop control variable termination value, init_value is the loop control variable initial value, and increment_value is the loop control variable increment value;

[0083] Step 53): For nested loops, the total number of iterations is equal to the sum of the number of times the inner loop iterates for each value of the outer loop control variable. This method mainly considers the case where the initial expression or termination expression of the inner loop is the outer loop control variable. For the case where the initial expression of the inner loop is variable, if the inner loop is incrementing, the nested loop calculation formula is If the inner loop is decrementing, the nested loop calculation formula is where i is the initial expression of the inner loop, out_init is the initial value of the outer loop, out_end is the termination value of the outer loop, and in_end is the termination value of the inner loop. For the case where the termination expression of the inner loop is variable, if the inner loop is incrementing, the nested loop calculation formula is If the inner loop is decrementing, the nested loop calculation formula is where i is the termination expression of the inner loop, and in_init is the initial value of the inner loop;

[0084] Step 54): For chained loops, if there is no dependency between the loops, each loop statement is analyzed independently according to the method of analyzing simple loops or nested loops. If there is a dependency between the loops, that is, the upper bound of the subsequent loop depends on a variable updated in the previous loop, the method is to analyze the loop statements sequentially and record the upper bounds of the loop statements that have been analyzed, then the data flow transformation function of the loop statement can be obtained. Specifically, the post-value after each execution of the loop body is used as the pre-value for the next loop. By executing the loop body the number of times equal to the upper bound of the current loop, the post-value of the loop statement can be obtained. Using this value as the pre-value of the next loop-dependent variable, the upper bound of the next loop can be analyzed.

[0085] Step 55): This method takes into account basic expressions with a relatively high usage frequency. For cases where the expression state is not considered or there are variables from external inputs and the definite value of the expression cannot be calculated, the expression string and the information obtained during the analysis process are output.

[0086] The above are the embodiments of the present invention, but the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention. Any omissions, modifications, equivalent replacements, etc. made within the scope of the patent application of the present invention without departing from the principle and spirit of the present invention shall be included within the protection scope of this disclosure.

Claims

1. A method for calculating the upper bound of a loop based on high-precision static program analysis, characterized in that, The steps include the following: Step 1): Locate the loop statements in the program and identify the loop control variables; locate the loop statements in the program through static program analysis, identify the patterns of the loop statements, and extract the loop control variables in different pattern loop statements according to the syntax patterns of the for statement, while statement, and do-while statement; Step 2): Analyze the data dependence relationships of the loop variables; analyze the data dependence relationships of the loop variables according to the syntax patterns of the for statement, while statement, and do-while statement, and find out all the expressions on which the calculation of the loop upper bound depends, including analyzing the loop initialization expression, loop termination expression, and loop increment expression; Step 3): Analyze the possible values of the variables in the loop upper bound dependent expressions; respectively define the data flow transformation functions for the initialization statement, assignment statement, compound assignment statement, increment and decrement statement, and loop statement; the parameters of the functions are the statements to be analyzed and the values of the variables before the execution of the statements, and the return values of the functions are the values of the variables after the execution of the statements; perform intra-procedural and inter-procedural data flow analysis on the above 5 data flow transformation functions, analyze the possible values of the basic type variables, and call the method in the next step, i.e., Step 4), to calculate the values of the expressions when the values of the expressions need to be calculated during the data flow analysis process; Step 4): Recursively calculate the possible values of the loop dependent expressions; for the ordinary variables in the expressions, call the method in Step 3) to analyze the specific values; Step 5): Calculate the final result according to the formula for summarizing the loop upper bound based on the loop structure; according to the syntax patterns of the for statement, while statement, and do-while statement, use the method in Step 4) to analyze and calculate the possible values of the loop upper bound dependent expressions, and summarize and calculate the formulas for the loop upper bound and calculate the final result for simple loops, nested loops, and chained loops respectively.

2. The loop upper bound calculation method based on high-precision static program analysis according to claim 1, wherein The specific steps of Step 1) to locate the loop statements in the program and identify the loop control variables include the following steps: Step 11): Convert the program source code into an abstract syntax tree as the input, and construct the control flow graph and function call graph of the program based on the abstract syntax tree; traverse the statements in the control flow graph, and perform further analysis when traversing the for loop statement, while loop, or do-while loop statement; Step 12): For the for loop statement, in the form of for (initial expression; loop condition; increment expression) {loop body;}; if the for statement has an initial expression, extract the initialization variable or the left value of the assignment expression as the loop control variable; if the for statement has a loop condition and the loop condition is a relational expression, extract the left value of the relational expression as the loop control variable; if the for statement has an increment expression, extract the incremented or decremented variable or extract the left value of the assignment statement as the loop increment expression; if none of the three exist, traverse the loop body to find the changing variables, including the variables in the increment and decrement statements or the left value in the compound assignment statement; Step 13): For a while loop statement in the form of while (loop condition) {loop body}; if the loop condition is a relational expression, extract the left value of the expression as the loop control variable; if the loop condition is a single variable, then this variable is the loop control variable. Step 14): For a do-while loop statement in the form of do {loop body;} while (loop condition); if the loop condition is a relational expression, extract the left value of the expression as the loop control variable; if the loop condition is a single variable, then this variable is the loop control variable.

3. The loop upper bound calculation method based on high-precision static program analysis according to claim 1, wherein The specific steps of analyzing the data dependence relationship of the loop variable in step 2) are as follows: Step 21): For a for loop statement, if the loop control variable is initialized or assigned in the for loop initialization expression, directly obtain the right value of the initialization statement or assignment statement as the loop initialization expression; otherwise, call the method in step 3) to perform data flow analysis to obtain the value of the loop control variable before entering the loop. If the for statement has a loop condition and the loop condition is a relational expression, extract the right value of the relational expression as the loop termination expression; otherwise, traverse the loop body to check if there is a break or return statement, and extract the right value in the conditional expression of the if statement where it is located as the loop termination expression. According to the loop increment statement located in step 1), if the increment statement is in the form of an increment or decrement expression, set the loop increment value to 1; if the increment statement is in the form of a compound assignment statement, extract the right value of the assignment statement as the loop increment expression. Step 22): For a while loop statement; call the method in step 3) to perform data flow analysis to obtain the value of the loop control variable before entering the loop, and this value is the loop initial value of the loop statement. If the loop condition is a relational expression, extract the right value of the expression as the loop termination expression; if the loop condition is a single variable, then traverse the loop body to check if there is a break or return statement, and extract the right value in the conditional expression of the if statement where it is located as the loop termination expression. Analyze the loop body to find the statement that updates the value of the loop control variable. For increment and decrement statements, set the loop increment value to 1; for compound assignment statements, extract their right values as the loop increment expressions. Step 23): For a do-while loop statement, the method of extracting the loop initialization expression, loop termination expression, and loop increment expression is the same as that of the while statement.

4. The method for calculating the loop upper bound based on high-precision static program analysis according to claim 1, wherein The specific steps of analyzing the possible values of variables in the loop upper bound dependence expression in step 3) are as follows: Step 31): For the data flow conversion function of the initialization statement and assignment statement, calculate the right value of the initialization statement or assignment statement by calling the method in step 4), and use this value as the post-value of the execution statement. Step 32): For the data flow conversion function of the compound assignment statement, calculate the right value of the compound assignment statement by calling the method in step 4), and perform operations of +=, -=, / =, *= on the pre-value according to the type of the operator, and use the operation result as the post-value of the execution statement. Step 33): For the data flow conversion function of increment and decrement statements, according to the type of the unary operator, add one or subtract one from the previous value as the value after the execution of the statement; Step 34): For the data flow conversion function of loop statements, it is necessary to perform data flow analysis on the statements in the loop body, and use the value after each execution of the loop body as the previous value for the next loop, and execute the loop upper bound times of the current loop in this way; Since all loop statements are analyzed sequentially, the current loop has already undergone loop upper bound analysis and the loop upper bound result has been recorded; Step 35): For the case where a variable is initialized within a function, perform intra-procedural data flow analysis; traverse the control flow graph of the function body, analyze whether there is a variable to be analyzed in the statement, and if so, pass the corresponding data flow conversion function according to the statement type to analyze the value of the variable after the analysis; Step 36): For the case where a variable comes from a formal parameter, perform inter-procedural data flow analysis; according to the function call graph, find all callers of the current function and the function call points, match the actual parameters at the function call points according to the position of the variable in the formal parameter; call Step 3) recursively to analyze the possible maximum or minimum value of the actual parameter, and pass the analysis result to the formal parameter; then perform intra-procedural data flow analysis on the variable.

5. The loop upper bound calculation method based on high-precision static program analysis according to claim 1, wherein The specific steps for the recursive calculation of the possible values of the loop-dependent expression in Step 4) are as follows: Step 41): For ordinary variables, call the method in Step 3) to analyze the possible values of the variable; Step 42): For binary operation expressions, if the expression is an arithmetic operation, recursively call Step 4) to calculate the left value and right value of the expression, and perform corresponding addition, subtraction, multiplication, division, and remainder operations on the left value and right value of the expression according to the operator type; if the expression is a relational operation, recursively call Step 4) to calculate the left value and right value of the expression, and perform corresponding equality, inequality, greater than, less than, greater than or equal to, and less than or equal to relational judgments on the left value and right value of the expression to obtain the final result; Step 43): For ternary operation expressions in the form of cond? x : y, recursively call Step 4) to calculate the condition cond. If the value of cond is true, recursively call Step 4) to calculate the value of x and return; otherwise, recursively call Step 4) to calculate the value of y and return; Step 44): For function call expressions, locate the function body of the function and traverse the control flow graph of the function body to obtain the return expression of the function, and recursively call Step 4) to calculate the value of the expression and return.

6. The method for calculating the loop upper bound based on high-precision static program analysis according to claim 1, wherein The specific steps for calculating the final result according to the loop upper bound formula by induction of the loop structure in Step 5) are as follows: Step 51): According to the syntax patterns of for statements, while statements, and do-while statements, call the method in Step 4) to analyze the possible values of the loop control variable initial expression and the loop control variable increment expression; Step 52): For simple loops, according to the formula Calculate the number of loop iterations in the current loop; where loop_upbound is the loop upper bound result, end_value is the termination value of the loop control variable, init_value is the initial value of the loop control variable, and increment_value is the increment value of the loop control variable; Step 53): For nested loops, the total number of iterations is equal to the sum of the number of times the inner loop iterates for each value of the outer loop control variable; considering the case where the initial expression or termination expression of the inner loop is the outer loop control variable, for the case where the initial expression of the inner loop is variable, if the inner loop increments, the nested loop calculation formula is ; if the inner loop decrements, the nested loop calculation formula is , where i is the initial expression of the inner loop, out_init is the initial value of the outer loop, out_end is the termination value of the outer loop, and in_end is the termination value of the inner loop; for the case where the termination expression of the inner loop is variable, if the inner loop increments, the nested loop calculation formula is , if the inner loop decrements, the nested loop calculation formula is , where i is the termination expression of the inner loop, and in_init is the initial value of the inner loop; Step 54): For chained loops, if there is no dependency between loops, each loop statement is independently analyzed in the same way as a simple loop or a nested loop; if there is a dependency between loops, that is, the upper bound of a subsequent loop depends on a variable updated in a previous loop, the method is to sequentially analyze the loop statements and record the upper bounds of the loop statements that have been analyzed, and then the data flow transformation function of the loop statement can be obtained. Specifically, the post-value after each execution of the loop body is used as the pre-value for the next loop. By executing the loop body the number of times equal to the upper bound of the current loop, the post-value of the loop statement can be obtained; this value is used as the pre-value for the next loop-dependent variable to analyze the upper bound of the next loop. Step 55): This method takes into account basic expressions with a high usage frequency. For expression states that are not considered or cases where variables come from external inputs and the definite value of the expression cannot be calculated, the expression string and the information obtained during the analysis process are output.

7. The method for calculating the loop upper bound based on high-precision static program analysis according to claim 4, wherein: In the specific steps of analyzing the possible values of variables in the loop upper bound dependency expression in step 3), interprocedural data flow analysis is considered, and for cases where loop-related variables come from formal parameters, the actual parameters at the call point are analyzed.

8. The method for calculating the loop upper bound based on high-precision static program analysis according to claim 5, wherein: In the specific steps of recursively calculating the possible values of loop dependency expressions in step 4), for cases where loop-related expressions are complex expressions, the results of complex expressions formed by permutations and combinations of some basic expressions with a high usage frequency are calculated.

9. The method for calculating the upper bound of a loop based on high-precision static program analysis according to claim 6, wherein: In the specific steps of calculating the final result according to the formula for inducing the loop upper bound based on the loop structure in step 5), the calculation method for the loop upper bound where the loop-related variables in the inner loop are the control variables of the outer loop is summarized and induced, and more accurate results can be given for some nested loops with high usage frequency patterns; a high-precision calculation method for the loop upper bound is also given for chained loops with dependencies between loops.

Citation Information

Patent Citations

  • Method for automatically synthesizing commonly-used cyclic abstracts and generating program specifications

    CN106681786A

  • Automatic repairing method for loop problem in program

    CN108228232A