Compilation optimization method and device for eliminating global function pointer in loop

By statically or dynamically optimizing the global function pointers in the loop in the compiler and converting them into direct calls, the problem that global function pointers hinder compiler optimization is solved, and the opportunity for function inlining and reduction of instruction overhead are achieved.

CN120631374APending Publication Date: 2025-09-12CHENGDU QUNXIN MICROELECTRONICS TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510950377.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-10
Publication Date
2025-09-12

AI Technical Summary

Technical Problem

In the compiler, global function pointers in loops hinder the compiler's optimization capabilities, resulting in large overhead of push and pop instructions when calling functions. Existing technologies make it difficult to effectively eliminate global function pointers to achieve function inlining optimization.

Method used

The optimization possibility of global function pointers is determined through constant value analysis. If the static analysis is successful, optimization is performed in combination with control flow. If it fails, dynamic inspection and optimization are performed, and direct calls are converted to reduce indirect calls of function pointers.

Benefits of technology

It reduces the instruction overhead during function calls, provides optimization opportunities for subsequent function inlining, and improves the optimization efficiency of the compiler.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120631374A_ABST
    Figure CN120631374A_ABST
Patent Text Reader

Abstract

The invention relates to a global function pointer elimination compiling optimization method and device in a loop. Before inline optimization of the compiler, the global function pointer in the loop is subjected to static optimization or dynamic optimization, and indirect calling performed through the function pointer in the loop is converted into direct calling, so that the number of instructions in the calling process is reduced, opportunities are provided for subsequent function inline, and the calling efficiency is improved. According to the embodiment of the invention, the optimized functions called through the function pointer are all directly called, so that subsequent function inline has an opportunity to replace the direct calling with codes of the function body, and the instruction overhead of pushing and out-of-stack during function calling is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of this specification relate to the field of computer technology, and more particularly to a method and device for compiling and optimizing the elimination of global function pointers in a loop. Background Art

[0002] Function inlining is a crucial compiler optimization technique. Its purpose is to move the function's execution directly to the call site, if appropriate, when a function is called. This reduces function call costs and provides more compiler optimization opportunities. When a function is called within a loop, it may occur multiple times at the same location. In this case, if function inlining offers benefits, the benefits can be greater than a single call.

[0003] In compiled languages, function pointers are used to flexibly call functions. They can be assigned values ​​at different locations in the program, and when using function pointers to call functions, the function pointed to by the current function pointer can be called. This language feature brings many conveniences to program writing, but at the same time hinders some of the compiler's optimization functions. How to eliminate global function pointers in loops in the compiler so that the compiler can inline more loop functions and thus reduce the instruction overhead of pushing and popping the stack when calling functions is a technical problem that needs to be solved urgently. Summary of the Invention

[0004] To solve the problems existing in the prior art, the embodiments of this specification provide a compilation optimization method and device for eliminating global function pointers in a loop. Before the function is inlined, the optimization possibility of the function pointer is judged through constant value analysis. If the static analysis is successful, the control flow optimization is completed through a loop version variant combined with the existing control flow. If the static analysis fails, dynamic inspection and optimization are performed through loop versioning, thereby eliminating the function pointer and providing inlining and other optimization opportunities.

[0005] The specific technical solutions of the embodiments of this specification are as follows:

[0006] On the one hand, an embodiment of this specification provides a compilation optimization method for eliminating global function pointers in a loop, the method comprising:

[0007] The compiler collects each global function pointer that meets the requirements in the global scope of the current file and stores it in a set;

[0008] For each global function pointer in the set, determining whether the global function pointer can take a unique value on a corresponding branch;

[0009] If so, perform static optimization on the global function pointer;

[0010] If not, the global function pointer is dynamically optimized.

[0011] Furthermore, the compiler collects each global function pointer that meets the requirements in the global scope of the current file and stores it in a collection further including:

[0012] Traverse each loop and find the global function pointer used in the loop;

[0013] Determine whether the global function pointer contains an indirect call through the global function pointer in the same loop;

[0014] If so, the global function pointer is stored in the set.

[0015] Furthermore, determining whether the global function pointer can take a unique value on the corresponding branch further includes:

[0016] Obtain a basic block set and corresponding branch block for function assignment to the global function pointer;

[0017] For a branch block, if there is a basic block that dominates the branch block, and all paths from the basic block to the branch block do not pass through other basic blocks that contain assignment operations to the function pointer, then the global function pointer takes a unique value on the branch block.

[0018] Furthermore, the basic block that governs this branch block is represented as:

[0019] All paths that reach this branch block must pass through this basic block.

[0020] Furthermore, static optimization of the global function pointer further includes:

[0021] Duplicate the loop into multiple copies according to the number of branch blocks that call the loop;

[0022] A separate copy of this loop is called for each branch block.

[0023] Furthermore, static optimization of the global function pointer further includes:

[0024] The indirect call through the global function pointer in each copied loop is converted into a direct call of a unique value, where the unique value is the unique value corresponding to the basic block that dominates the corresponding branch block.

[0025] Furthermore, the method further comprises:

[0026] Calculating an optimization cost score based on the size of the loop, the complexity of the function called by the global function pointer, and the number of times the global function pointer is called in the loop;

[0027] If the optimization cost score meets the optimization requirement, the global function pointer is optimized.

[0028] Furthermore, dynamically optimizing the global function pointer further includes:

[0029] Counting multiple different assignments of the global function pointer;

[0030] Duplicate the loop into multiple copies according to the number of different assignments;

[0031] Convert the indirect call through the global function pointer in each copied loop into a direct call of the corresponding assignment;

[0032] Add a conditional judgment statement and use each copied loop as a corresponding branch target of the conditional judgment statement.

[0033] On the other hand, an embodiment of this specification further provides a device for eliminating compilation optimization of global function pointers in a loop, the device comprising:

[0034] The global function pointer collection unit is used by the compiler to collect all global function pointers that meet the requirements in the global scope of the current file and store them in a collection;

[0035] The global function pointer optimization unit is used to determine, for each global function pointer in the set, whether the global function pointer can take a unique value on the corresponding branch; if so, statically optimize the global function pointer; if not, dynamically optimize the global function pointer.

[0036] On the other hand, an embodiment of this specification further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program implements the above method when executed by a processor.

[0037] Through the method of the embodiments of this specification, before the compiler's inline optimization, the global function pointers in the loop are first statically optimized or dynamically optimized, and the indirect calls made through the function pointers in the loop are converted into direct calls, which reduces the number of instructions in the calling process and provides an opportunity for subsequent function inlining. After the optimization of the embodiments of this specification, the functions called through the function pointers are all direct calls, so the subsequent function inlining has the opportunity to replace the direct calls with the code of the function body, thereby reducing the instruction overhead of pushing and popping the stack during function calls. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] In order to more clearly illustrate the embodiments of this specification or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the embodiments of this specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0039] Figure 1 The figure shows a flow chart of a method for eliminating compilation optimization of global function pointers in a loop in an embodiment of this specification;

[0040] Figure 2 The figure shows the position diagram of the function pointer optimization pass in the embodiment of this specification;

[0041] Figure 3 The figure shows a flow chart of the compiler collecting each global function pointer that meets the requirements in the global scope of the current file and storing them in a set in an embodiment of this specification;

[0042] Figure 4 FIG2 is a schematic diagram showing an embodiment of the present specification for determining whether the global function pointer can take a unique value on a corresponding branch;

[0043] Figure 5 Shown is a control flow chart to be optimized in the embodiment of this specification;

[0044] Figure 6 FIG2 is a schematic diagram of statically optimizing the global function pointer in an embodiment of this specification;

[0045] Figure 7 The figure shows a control flow chart after static optimization in the embodiment of this specification;

[0046] Figure 8 The figure shows a flow chart of dynamically optimizing the global function pointer in the embodiment of this specification;

[0047] Figure 9 The embodiment of this specification shows Figure 5 The control flow graph to be optimized shown is a control flow graph after being optimized;

[0048] Figure 10 The figure shows a schematic diagram of the processing flow of the cost model in the embodiment of this specification;

[0049] Figure 11 The figure shows a schematic diagram of the structure of a device for eliminating compilation and optimizing a global function pointer in a loop according to an embodiment of the present specification;

[0050] Figure 12 The figure shows a schematic diagram of the structure of a computer device in an embodiment of this specification.

[0051]

Description of the accompanying drawings

[0052] 1101, global function pointer collection unit;

[0053] 1102. Global function pointer optimization unit;

[0054] 1202. Computer equipment;

[0055] 1204. Processing equipment;

[0056] 1206. Storage resources;

[0057] 1208, drive system;

[0058] 1210, input / output module;

[0059] 1212. Input device;

[0060] 1214. Output device;

[0061] 1216. Presentation equipment;

[0062] 1218. Graphical User Interface;

[0063] 1220, network interface;

[0064] 1222, communication link;

[0065] 1224. Communication bus. DETAILED DESCRIPTION

[0066] The following will be combined with the drawings in the embodiments of this specification to clearly and completely describe the technical solutions in the embodiments of this specification. Obviously, the embodiments described are only part of the embodiments of this specification, not all of them. Based on the embodiments of this specification, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the embodiments of this specification.

[0067] It should be noted that the terms "first", "second", etc. in the description and claims of the embodiments of this specification and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that the numbers used in this way can be interchanged where appropriate, so that the embodiments of the embodiments of this specification described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, apparatus, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0068] It should be noted that the acquisition, storage, use, and processing of data in the technical solutions of the embodiments of this specification comply with the relevant provisions of national laws and regulations.

[0069] It should be noted that in the embodiments of this specification, certain software, components, models and other existing solutions in the industry may be mentioned. They should be regarded as exemplary and their purpose is only to illustrate the feasibility of implementing the technical solution of this application, but it does not mean that the applicant has or will necessarily use the solution.

[0070] In response to the problems existing in the prior art, the embodiments of this specification provide a compilation optimization method for eliminating global function pointers in loops. Before function inlining, the optimization possibility of function pointers is judged through constant value analysis. If the static analysis is successful, the control flow optimization is completed through loop versioning variants combined with the existing control flow. If the static analysis fails, dynamic checking and optimization are performed through loop versioning, thereby eliminating function pointers and providing inlining and other optimization opportunities. Figure 1 The figure shows a flowchart of a compilation optimization method for eliminating global function pointers in a loop in an embodiment of this specification. This figure describes the process of the compiler converting global function pointers in a loop into direct calls before the function is inlined. The order of steps listed in the embodiment is only one way of executing the steps among many, and does not represent the only execution order. When the actual system or device product is executed, it can be executed in sequence or in parallel according to the method shown in the embodiment or the accompanying drawings. Specifically, Figure 1 As shown, the method can be executed by a computer and may include:

[0071] Step 101: The compiler collects all global function pointers that meet the requirements in the global scope of the current file and stores them in a set;

[0072] Step 102: for each global function pointer in the set, determine whether the global function pointer can take a unique value on the corresponding branch;

[0073] Step 103: If yes, then statically optimize the global function pointer;

[0074] Step 104: If not, dynamically optimize the global function pointer.

[0075] Through the method of the embodiments of this specification, before the compiler's inline optimization, the global function pointers in the loop are first statically optimized or dynamically optimized, and the indirect calls made through the function pointers in the loop are converted into direct calls, which reduces the number of instructions in the calling process and provides an opportunity for subsequent function inlining. After the optimization of the embodiments of this specification, the functions called through the function pointers are all direct calls, so the subsequent function inlining has the opportunity to replace the direct calls with the code of the function body, thereby reducing the instruction overhead of pushing and popping the stack during function calls.

[0076] The method of the embodiment of this specification is implemented in the form of a separate function pointer optimization pass in the compiler. The location of the function pointer optimization pass is as follows: Figure 2 As shown, it is located in the local optimization stage of the compiler's mid-stage optimization, before the inline optimization pass.

[0077] The optimization scope of the embodiments of this specification is limited to global function pointers in loops. The main reasons include:

[0078] 1. Function pointers in loops have the greatest optimization value;

[0079] 2. The optimization methods used may increase code length and insert conditional judgment instructions, which may have a certain negative impact on program performance. If the optimization scope is too large, it may cause excessive expansion of code length and affect program performance.

[0080] 3. In complex flow control, constant value analysis may introduce too many optimization processes for absolute rigor.

[0081] Therefore, if Figure 3 As shown, the compiler collects each global function pointer that meets the requirements in the global scope of the current file and stores it into a collection further including:

[0082] Step 301: traverse each loop and find the global function pointer used in the loop;

[0083] Step 302: Determine whether the global function pointer contains an indirect call through the global function pointer in the same loop;

[0084] Step 303: If yes, store the global function pointer into the set.

[0085] In the embodiments of this specification, if a global function pointer is assigned to another pointer variable in a loop body, it means that the global function pointer is used in the loop. Then, it is determined whether the global function pointer contains an indirect call through the global function pointer in the same loop, that is, whether the pointer variable assigned to the global function pointer is called in the loop.

[0086] The following pseudo code:

[0087] FP = Func1;

[0088] / / cycle

[0089] While(1); ......

[0091] FP_1=FP;

[0092] A_1=FP_1(arg0,…,argN); ......

[0094] Here, Func1 is a function, and FP is a global function pointer. In the while loop, the global function pointer FP is assigned to the pointer variable FP_1, and then the pointer variable FP_1 is called. The global function pointer FP is indirectly called in the loop, and the global function pointer FP meets the requirements.

[0095] Then perform value analysis on the global function pointer.

[0096] To determine whether static optimization is feasible, it's not enough to simply determine the possible values ​​of a global function pointer at a certain location; it's also necessary to confirm whether it has a unique value at that location. Constant value analysis is typically performed through an iterative approach. However, iterative analysis focuses more on analyzing the initialization of all variables; it would be much simpler if only a single variable were analyzed. Furthermore, the iterative process operates at the variable level. Since the compiler already provides more control flow information and currently only analyzes global function pointers within loops, some analysis can be elevated to the block and loop levels. In actual compilers, this judgment can be made by combining dominance analysis with basic block reachability analysis.

[0097] Specifically, if Figure 4 As shown, determining whether the global function pointer can take a unique value on the corresponding branch further includes:

[0098] Step 401: Obtain a basic block set and corresponding branch blocks for performing function assignment on the global function pointer;

[0099] Step 402: For a branch block, if there is a basic block that dominates the branch block, and all paths from the basic block to the branch block do not pass through other basic blocks, then the global function pointer takes a unique value on the branch block.

[0100] In the embodiments of this specification, Figure 5 As shown, Figure 5 Each box in is a basic block, where FP is a global function pointer that meets the requirements, LOOP1 is a loop, and the global function pointer FP is indirectly called in the loop.

[0101] Get the basic block set {(func1,bb1),(func2,bb2),(func3,bb3)} for function assignment to the global function pointer FP, where func1, func2 and func3 are function names, and the corresponding basic blocks are bb1, bb2 and bb3.

[0102] Then, the branch blocks in the path of the loop are obtained, namely brbb1 and brbb2, where the branch block brbb1 is the same as the basic block bb1.

[0103] For a branch block (brbb1 or brbb2), if there is a basic block that dominates the branch block, and all paths from the basic block to the branch block do not pass through other basic blocks that contain assignment operations to the function pointer, then the global function pointer takes a unique value on the branch block.

[0104] The basic block that dominates the branch block means that all paths that reach the branch block must pass through the basic block.

[0105] by Figure 5 For example, branch block brbb1 is also basic block bb1. Therefore, the path to branch block brbb1 must pass through basic block bb1, and all paths from basic block bb1 to branch block brbb1 do not pass through other basic blocks. Therefore, the global function pointer FP takes a unique value on branch block brbb1, that is, the function func1 in basic block bb1.

[0106] Branch block brbb2, basic blocks bb2 and bb3 are not the basic blocks that must be passed through in the path to reach branch block brbb2. Therefore, the global function pointer FP cannot take a unique value on branch block brbb2. The global function pointer FP has two possible assignments on branch block brbb2, namely function func2 in basic block bb2 and function func3 in basic block bb3.

[0107] The embodiments of this specification perform static optimization on global function pointers that can take unique values, and perform dynamic optimization on global function pointers that cannot take unique values.

[0108] Both static optimization and dynamic optimization use similar loop versioning ideas, but their specific implementations are very different: the static optimization method in the embodiment of this specification is to separate the original control flow branches, copy the corresponding loops, and put them into the separated branches. The dynamic optimization method in the embodiment of this specification requires inserting dynamically checked conditional statements in the pre-block of the loop, and performing loop versioning in subsequent different branches. Although scenarios that can be completed through static optimization can also be completed through dynamic optimization, static optimization utilizes the original branches in the control flow to avoid inserting other statements, while compressing the code length.

[0109] The embodiment of this specification additionally implements the control flow conversion process under static optimization, combines the idea of ​​loop versioning, and utilizes the existing control flow branches.

[0110] Specifically, if Figure 6 As shown, static optimization of the global function pointer further includes:

[0111] Step 601: Duplicate the loop into multiple copies according to the number of branch blocks that call the loop;

[0112] In this step, if there are multiple branch blocks that call the loop, and multiple branch blocks have unique values ​​for the global function pointer in the loop, the loop is copied into multiple branches, the number of which is the same as the number of branch blocks that have unique values ​​for the global function pointer in the loop. If there is only one branch block that calls the loop, and the branch block has a unique value for the global function pointer in the loop, there is no need to copy the loop.

[0113] Step 602: Call a copy of the loop for each branch block separately;

[0114] For example, Figure 7 In the original control flow shown, bb4 and bb5 are both basic blocks and branch blocks, and LOOP4 is a loop. If the global function pointer FP is indirectly called in loop LOOP4, and the global function pointer FP is assigned values ​​in bb4 and bb5 respectively, then bb4 and bb5 are both branch blocks with unique values ​​of the global function pointer. Therefore, loop LOOP4 is copied into two, namely LOOP4_1 and LOOP4_2. bb4 calls LOOP4_1 and bb5 calls LOOP4_2.

[0115] Step 603: Convert the indirect call through the global function pointer in each copied loop into a direct call of a unique value, where the unique value is the unique value corresponding to the basic block that dominates the corresponding branch block.

[0116] In an embodiment of the present specification, an indirect call of a global function pointer is converted into a direct call of a unique value, so that when a subsequent function is inlined, the compiler can directly replace the call of the global function pointer in the loop with the code of the function body corresponding to the unique value, thereby reducing the overhead of function calls and creating more optimization opportunities.

[0117] For example, the following pseudo code:

[0118] / / Before optimization

[0119] FP = Func1;

[0120] / / cycle

[0121] While(1); ......

[0123] FP_1=FP;

[0124] A_1=FP_1(arg0,…,argN); ......

[0126] / / After optimization

[0127] FP = Func1;

[0128] While(1); ......

[0130] FP_1=FP;

[0131] A_1=Func_1(arg0,…,argN); ......

[0133] Here, Func1 is a function and FP is a global function pointer. In the while loop, the global function pointer FP is assigned to the pointer variable FP_1, which is then called. Converting the indirect call to the global function pointer FP to a direct call to the unique value Func_1 yields A_1 = Func_1(arg0,…,argN).

[0134] According to one embodiment of this specification, Figure 8 As shown, the dynamic optimization of the global function pointer further includes:

[0135] Step 801: Counting multiple different assignments of the global function pointer;

[0136] Step 802: Duplicate the loop into multiple copies according to the number of different assignments;

[0137] Step 803: convert the indirect call through the global function pointer in each copied loop into a direct call with the corresponding assigned value;

[0138] Step 804: Add a conditional statement, and use each copied loop as a corresponding branch target of the conditional statement.

[0139] In the embodiments of this specification, the dynamic optimization method is also to replace the indirect call of the global function pointer in the loop with a direct call. Because the global function pointer during dynamic optimization has multiple possible values ​​on the branch block, the dynamic optimization method of the embodiments of this specification copies the loop into multiple ones according to the number of different assignments of the global function pointer on the branch block, and then converts the indirect call through the global function pointer in each copied loop into a direct call of the corresponding assignment, and finally adds a conditional judgment statement, and uses each copied loop as the corresponding branch target of the conditional judgment statement.

[0140] For example, by static optimization and dynamic optimization Figure 5 The control flow graph shown is optimized as follows Figure 9 As shown, for the branch block brbb1 call, since the branch block brbb1 has only one unique value, the loop LOOP1 does not need to be copied, and the indirect call of the global function pointer FP in the loop LOOP1 is replaced by a direct call of the unique value func1.

[0141] For branch block brbb2, because the global function pointer has two possible values, func2 and func3, loop LOOP1 is copied into two, LOOP2 and LOOP3. The indirect call to the global function pointer FP in loop LOOP2 is replaced with a direct call to one possible value, func2. The indirect call to the global function pointer FP in loop LOOP3 is replaced with a direct call to the other possible value, func3. A conditional statement is added, with the copied loops LOOP2 and LOOP3 serving as the corresponding branch targets.

[0142] In the embodiments of this specification, both static optimization and dynamic optimization may increase the code length. Therefore, the embodiments of this specification introduce a cost model to make a trade-off between control flow optimization and code length growth. The main factors considered are the size of the loop, the complexity of the function called by the global function pointer, and the number of times the global function pointer is called in the loop. Specifically, Figure 10 As shown, the method further includes:

[0143] Step 1001: Calculate an optimization cost score based on the size of the loop, the complexity of the function called by the global function pointer, and the number of times the global function pointer is called in the loop;

[0144] Step 1002: If the optimization cost score meets the optimization requirements, the global function pointer is optimized.

[0145] In the embodiments of this specification, the loop size refers to the loop length (number of instructions). Because loops need to be replicated, if the loop length is too large, many copies will be made, increasing the code length. The complexity of the function called by the global function pointer refers to the function length (number of instructions). If the complexity of the function called by the global function pointer is small, it is more likely to be optimized. The more times the global function pointer is called in the loop, the better the optimization effect.

[0146] Therefore, the embodiments of this specification can use the loop length, the calling complexity of the global function pointer and the number of times the global function pointer is called in the loop to determine whether to optimize. For example, the cost score is calculated by normalization and weighted summation method, and then compared with the threshold. If it is lower than the threshold (or higher than the threshold, which can be set according to actual conditions), the global function pointer is optimized.

[0147] In addition, the loop length, the calling complexity of the global function pointer or the number of times the global function pointer is called in the loop may be used alone to determine whether to perform optimization, and the embodiments of this specification do not impose any restrictions.

[0148] For example, if the loop is small enough, the calling of the global function pointer is simple enough, and the global function pointer repeatedly calls certain functions on multiple paths in the control flow graph, this optimization will be more likely to be implemented.

[0149] Based on the same inventive concept, the embodiment of this specification also provides a global function pointer elimination compilation optimization device in a loop, such as Figure 11 Shown, including:

[0150] The global function pointer collection unit 1101 is used by the compiler to collect all global function pointers that meet the requirements in the global scope of the current file and store them in a collection;

[0151] The global function pointer optimization unit 1102 is used to determine, for each global function pointer in the set, whether the global function pointer can take a unique value on the corresponding branch; if so, statically optimize the global function pointer; if not, dynamically optimize the global function pointer.

[0152] The beneficial effects achieved by the above-mentioned device are consistent with the beneficial effects achieved by the above-mentioned method, and will not be described in detail in the embodiments of this specification.

[0153] like Figure 12 The figure shows a schematic diagram of the structure of a computer device according to an embodiment of the present specification. The computer device according to this embodiment is capable of executing the methods according to the embodiments of the present specification described above. The computer device 1202 may include one or more processing devices 1204, such as one or more central processing units (CPUs), each of which may implement one or more hardware threads. The computer device 1202 may also include any storage resources 1206 for storing any type of information, such as code, settings, data, etc. For example, without limitation, the storage resources 1206 may include any one or more combinations of the following: any type of RAM, any type of ROM, a flash memory device, a hard disk, an optical disk, etc. More generally, any storage resource may use any technology to store information. Furthermore, any storage resource may provide volatile or non-volatile retention of information. Furthermore, any storage resource may represent a fixed or removable component of the computer device 1202. In one embodiment, when the processing device 1204 executes associated instructions stored in any storage resource or combination of storage resources, the computer device 1202 may perform any operation of the associated instructions. The computer device 1202 also includes one or more drive systems 1208 for interacting with any storage resources, such as a hard disk drive system, an optical disk drive system, and the like.

[0154] The computer device 1202 may also include an input / output module 1210 (I / O) for receiving various inputs (via input devices 1212) and for providing various outputs (via output devices 1214). A specific output mechanism may include a presentation device 1216 and an associated graphical user interface (GUI) 1218. In other embodiments, the input / output module 1210 (I / O), input devices 1212, and output devices 1214 may not be included, and the computer device 1202 may simply be a computer device in a network. The computer device 1202 may also include one or more network interfaces 1220 for exchanging data with other devices via one or more communication links 1222. One or more communication buses 1224 couple the components described above together.

[0155] The communication link 1222 may be implemented in any manner, for example, via a local area network, a wide area network (e.g., the Internet), a point-to-point connection, etc., or any combination thereof. The communication link 1222 may include any combination of hardwired links, wireless links, routers, gateway functions, name servers, etc., governed by any protocol or combination of protocols.

[0156] The embodiments of this specification also provide a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program implements the above method when executed by a processor.

[0157] The embodiments of this specification also provide a computer-readable instruction, wherein when a processor executes the instruction, the program therein causes the processor to execute the above method.

[0158] It should be understood that in the various embodiments of the present specification, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present specification.

[0159] It should also be understood that in the embodiments of this specification, the term "and / or" is merely a description of the relationship between associated objects, indicating that three possible relationships exist. For example, "A and / or B" can represent three situations: A exists alone, A and B exist simultaneously, or B exists alone. Furthermore, in the embodiments of this specification, the character " / " generally indicates that the associated objects are in an "or" relationship.

[0160] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed in the embodiments of this specification can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the embodiments of this specification.

[0161] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0162] In the several embodiments provided in the embodiments of this specification, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, or can be electrical, mechanical or other forms of connection.

[0163] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiments of this specification.

[0164] In addition, the functional units in each embodiment of the present specification may be integrated into a single processing unit, each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0165] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of this specification is essentially or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the embodiment of this specification. The aforementioned storage medium includes: various media that can store program codes, such as a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0166] The embodiments of this specification use specific embodiments to illustrate the principles and implementation methods of the embodiments of this specification. The description of the above embodiments is only used to help understand the methods and core ideas of the embodiments of this specification. At the same time, for those skilled in the art, based on the ideas of the embodiments of this specification, there will be changes in the specific implementation methods and application scopes. In summary, the content of this specification should not be understood as a limitation on the embodiments of this specification.

Claims

1. A compilation optimization method for eliminating global function pointers in a loop, characterized in that: The method comprises: The compiler collects each global function pointer that meets the requirements in the global scope of the current file and stores it in a set; For each global function pointer in the set, determining whether the global function pointer can take a unique value on a corresponding branch; If so, perform static optimization on the global function pointer; If not, the global function pointer is dynamically optimized.

2. The method according to claim 1, characterized in that The compiler collects each global function pointer that meets the requirements in the global scope of the current file and stores it in a collection further including: Traverse each loop and find the global function pointer used in the loop; Determine whether the global function pointer contains an indirect call through the global function pointer in the same loop; If so, the global function pointer is stored in the set.

3. The method according to claim 2, characterized in that Determining whether the global function pointer can take a unique value on the corresponding branch further includes: Obtain a basic block set and corresponding branch block for function assignment to the global function pointer; For a branch block, if there is a basic block that dominates the branch block, and all paths from the basic block to the branch block do not pass through other basic blocks that contain assignment operations to the function pointer, then the global function pointer takes a unique value on the branch block.

4. The method according to claim 3, characterized in that The basic block that governs this branch block is represented as: All paths that reach this branch block must pass through this basic block.

5. The method according to claim 3, characterized in that Static optimization of the global function pointer further includes: Duplicate the loop into multiple copies according to the number of branch blocks that call the loop; A separate copy of this loop is called for each branch block.

6. The method according to claim 3, characterized in that Static optimization of the global function pointer also includes: The indirect call through the global function pointer in each copied loop is converted into a direct call of a unique value, where the unique value is the unique value corresponding to the basic block that dominates the corresponding branch block.

7. The method according to claim 2, characterized in that The method further comprises: Calculating an optimization cost score based on the size of the loop, the complexity of the function called by the global function pointer, and the number of times the global function pointer is called in the loop; If the optimization cost score meets the optimization requirement, the global function pointer is optimized.

8. The method according to claim 3, characterized in that Dynamically optimizing the global function pointer further includes: Counting multiple different assignments of the global function pointer; Duplicate the loop into multiple copies according to the number of different assignments; Convert the indirect call through the global function pointer in each copied loop into a direct call of the corresponding assignment; Add a conditional judgment statement and use each copied loop as a corresponding branch target of the conditional judgment statement.

9. A device for eliminating compilation and optimization of global function pointers in a loop, characterized in that: The device comprises: The global function pointer collection unit is used by the compiler to collect all global function pointers that meet the requirements in the global scope of the current file and store them in a collection; The global function pointer optimization unit is used to determine, for each global function pointer in the set, whether the global function pointer can take a unique value on the corresponding branch; if so, statically optimize the global function pointer; if not, dynamically optimize the global function pointer.

10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.