Instruction Optimization Method and Device
By compiling the source program of the embedded chip and defining the uncalled functions as empty functions, the memory saving problem in low-cost and low-power application scenarios is solved, and the effective reduction of code segment storage space is achieved.
Patent Information
- Application Number
- CN201911133520.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-11-19
- Publication Date
- 2025-06-13
- Estimated Expiration
- 2039-11-19
AI Technical Summary
In low-cost and low-power application scenarios, the running memory capacity of embedded chips is compressed, resulting in high integration of program instructions and reducing redundancy to save memory space.
By compiling the source program, identifying and marking uninvoked functions as redundant functions, and defining these redundant functions as empty functions in the source program, reducing the storage space occupied by code segments in the executable file.
It effectively reduces the storage space occupied by the code segments in the executable file and meets the memory saving requirements in application scenarios with low cost and low power consumption.
Smart Images

Figure CN112905180B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of embedded development, and in particular, to an instruction optimization method, a computing device, and a system on chip. Background Art
[0002] With the development of network communication technology, today's society has entered the Internet of Things (IoT) era. Embedded technology is the foundation for the realization of the IoT. After developers complete the development and debugging of a program on a PC (Personal Computer) side, the program is burned into the memory of an embedded chip, and the embedded chip is integrated into an intelligent device. When the processor of the embedded chip runs the program, the intelligent device can be started to work and implement corresponding functions.
[0003] There are various intelligent devices, and the hardware resource configurations of embedded chips vary greatly according to different application scenarios of intelligent devices. In application scenarios with low-cost and low-power consumption requirements (such as industrial application scenarios), the capacity of the running memory of the embedded chip is usually compressed. Therefore, the program instructions burned into the memory need to have a high integration degree and minimize redundancy as much as possible to save the memory space occupied during operation. Summary of the Invention
[0004] To this end, the present invention provides an instruction optimization method and device to attempt to solve or at least alleviate the problems above.
[0005] According to a first aspect of the present invention, there is provided an instruction optimization method suitable for reducing the storage space occupied by a code segment in an executable file of a source program. The method includes the steps of: compiling the source program to obtain an executable file of the source program, where the executable file includes storage area information occupied by multiple functions; regarding functions included in the executable file that are not called in the source program as redundant functions; and defining the redundant functions as empty functions in the source program.
[0006] Optionally, in the instruction optimization method according to the present invention, the storage space is a read-only storage space for a system on chip of an IoT device.
[0007] Optionally, in the instruction optimization method according to the present invention, the source program includes at least one debugging function for program debugging. Before the step of compiling the source program, the method further includes the steps of: distinguishing the compilation mode of the source program by defining a debugging constant in the source program, where the compilation mode includes a debugging mode and a release mode; and defining the debugging function as empty in the release mode to delete the debugging function.
[0008] Optionally, in the instruction optimization method according to the present invention, the step of compiling the source program includes: compiling the source program in release mode.
[0009] Optionally, in the instruction optimization method according to the present invention, the step of defining a redundant function as an empty function in the source program includes: determining the definition format of the redundant function, where the definition format includes return value type, function name, and parameter type; and defining the redundant function as an empty function according to the definition format.
[0010] Optionally, in the instruction optimization method according to the present invention, a compiler is used to compile the source program, and the redundant function is a standard library function introduced by the compiler.
[0011] Optionally, in the instruction optimization method according to the present invention, the redundant function is a file input / output class function introduced by the compiler.
[0012] Optionally, in the instruction optimization method according to the present invention, the step of defining a redundant function as an empty function in the source program includes: determining the root function in the file input / output class, where other functions in the file input / output class are sub-functions of the root function; and defining the root function as an empty function.
[0013] Optionally, in the instruction optimization method according to the present invention, the step of defining the root function as an empty function includes: determining the definition format of the root function; and defining the root function as an empty function according to the definition format.
[0014] Optionally, in the instruction optimization method according to the present invention, it further includes the step of: when the executable file includes two functions with the same function but different parameter precisions, adjusting the parameters of the two functions to the same precision.
[0015] Optionally, in the instruction optimization method according to the present invention, after the step of defining a redundant function as an empty function in the source program, it further includes the step of: compiling the source program again to obtain an executable file of the source program.
[0016] According to a second aspect of the present invention, there is provided a computing device, including: at least one processor; and a memory storing program instructions, which when read and executed by the processor, cause the computing device to execute the above instruction optimization method.
[0017] According to a third aspect of the present invention, there is provided a readable storage medium storing program instructions, which when read and executed by a computing device, cause the computing device to execute the above instruction optimization method.
[0018] According to a fourth aspect of the present invention, there is provided a system on chip, comprising: at least one processor; and a read-only memory, wherein the read-only memory stores an executable file of a source program optimized according to the above instruction optimization method.
[0019] According to a fifth aspect of the present invention, there is provided an intelligent device comprising the above-mentioned system on chip.
[0020] According to the technical solution of the present invention, the source program is compiled to obtain an executable file of the source program. By comparing the functions in the executable file with the functions in the source program, the functions included in the executable file but not called in the source program are regarded as redundant functions, and the redundant functions are defined as empty functions in the source program. Redundant functions are usually library functions introduced by the compiler when compiling the source program. By defining the redundant functions as empty functions, the compiler will give priority to calling the custom empty functions instead of calling the functions with the same name in the standard library, thereby reducing the storage space occupied by the code segments in the executable file.
[0021] Furthermore, when the redundant function is a file input and output class function introduced by the compiler, by determining the root function in the file input and output class and defining the root function as an empty function, it is convenient to avoid introducing all functions in the file input and output class.
[0022] In addition, the technical solution of the present invention also realizes the conditional compilation of the source program. In the debug mode, the debug function in the source program is compiled normally to obtain the debug version of the executable file, and the debug version of the executable file includes the code segment of the debug function. In the release mode, the debug function is defined as empty to delete the debug function, so that the executable file of the release version does not include the code segment of the debug function, thereby reducing the storage space occupied by the code segment in the executable file.
[0023] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] To achieve the above and related purposes, certain illustrative aspects are described herein in conjunction with the following description and accompanying drawings, which indicate various ways in which the principles disclosed herein can be practiced, and all aspects and their equivalents are intended to fall within the scope of the claimed subject matter. The above and other purposes, features and advantages of the present disclosure will become more apparent by reading the following detailed description in conjunction with the accompanying drawings. Throughout the present disclosure, the same reference numerals generally refer to the same parts or elements.
[0025] Figure 1 Shows a schematic diagram of a system on chip 100 according to an embodiment of the present invention;
[0026] Figure 2 Shows a schematic diagram of a computing device 200 according to an embodiment of the present invention;
[0027] Figure 3 Shows a schematic diagram of a programming system 300 according to an embodiment of the present invention;
[0028] Figure 4 Shows a flowchart of an instruction optimization method 400 according to an embodiment of the present invention;
[0029] Figure 5 Shows a schematic diagram of a code segment of an executable file generated by compilation according to an embodiment of the present invention;
[0030] Figure 6 Shows a schematic diagram of a code segment of an executable file generated by compilation according to another embodiment of the present invention;
[0031] Figure 7 Shows a flowchart of an instruction optimization method 700 according to another embodiment of the present invention;
[0032] Figure 8 Shows a schematic diagram of two functions with the same function but different parameter precisions according to an embodiment of the present invention. Detailed implementation manners
[0033] Hereinafter, exemplary embodiments of the present disclosure will be described in more detail with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided so that the present disclosure can be more thoroughly understood and the scope of the present disclosure can be fully conveyed to those skilled in the art.
[0034] Figure 1 Shows a schematic diagram of a system on chip (SoC) 100 according to an embodiment of the present invention. As Figure 1 shown, the system on chip 100 includes a processor 110, a bus 120, a read-only memory (ROM) 130, and a random access memory (RAM) 140. The processor 110 is coupled to the read-only memory 130 and the random access memory 140 via the bus 120, respectively.
[0035] The processor 110 can be, for example, a single-core processor, a multi-core processor, a processor core in a multi-core processor, or a processing element in an electronic system, etc., but not limited thereto. The bus 120 includes, for example, an address bus for specifying a storage address and a data bus for data transmission.
[0036] The read-only memory 130 can be, for example, a FLASH memory (i.e., flash memory, including NAND FLASH, NOR FLASH, etc.), a Programmable ROM (PROM), an Erasable Programmable ROM (EPROM), an Electrically Erasable Programmable ROM (EEPROM), etc., but not limited thereto.
[0037] The random access memory 140 can be, for example, a Static RAM (SRAM), a Dynamic RAM (DRAM), etc., but not limited thereto.
[0038] The processor 110 is used to obtain instructions from the read-only memory 130, the random access memory 140, a cache (not shown in the figure), or other sources and execute them, and receive data inputs from the random access memory 140, the cache, or other sources and generate data outputs to them. Figure 1 The executable file of the program is stored in the read-only memory 130. The executable file generally includes a text segment, a data segment, and a bss segment. The text segment is used to store program instructions, including the storage area information occupied by multiple functions; the data segment is used to store data that can be determined at compile time, such as constants and variables with initial values, etc.; the bss segment is used to store variables without initial values. During program execution, it is usually necessary to copy the text segment and the data segment of the executable file to the random access memory 140 (in some cases, only the data segment may be copied to the random access memory 140, without copying the text segment), and clear the storage area corresponding to the bss segment in the random access memory 140. Then the processor 110 obtains and executes the instructions in the text segment from the random access memory 140 (or from the read-only memory 130), and reads and writes the data segment and the bss segment.
[0039] The above-described system-on-chip 100 can be included in an intelligent device to implement corresponding functions in the intelligent device, including but not limited to executing relevant control programs, performing data analysis, arithmetic operations and processing, network communication, controlling peripherals in the intelligent device, etc.
[0040]
[0041] Such intelligent devices include dedicated intelligent devices such as mobile terminals and personal digital terminals, etc. These devices include data processing based on one or more system-on-chips according to the present invention, or controlling peripherals in the devices.
[0042] Such intelligent devices also include dedicated devices constructed to achieve specific functions, such as smart speakers and smart display devices, etc. These devices include controlling speakers and display devices according to the system-on-chip of the present invention, so as to endow the speakers and display devices with additional functions such as communication, perception, and data processing.
[0043] Such intelligent devices also include various IoT and AIoT (AI + IoT, intelligent Internet of Things) devices. These devices include data processing according to the system-on-chip of the present invention, such as performing AI operations, data communication and transmission, etc., thus realizing a more intensive and intelligent device distribution.
[0044] Such intelligent devices can also be used in vehicles, for example, can be implemented as in-vehicle devices or can be embedded in vehicles, so as to provide data processing capabilities for intelligent driving of vehicles.
[0045] Such intelligent devices can also be used in the home and entertainment fields, for example, can be implemented as smart speakers, smart air conditioners, smart refrigerators, smart display devices, etc. These devices include data processing and peripheral control according to the system-on-chip of the present invention, thus realizing the intelligence of home and entertainment devices.
[0046] In addition, such intelligent devices can also be used in the industrial field, for example, can be implemented as industrial control devices, sensing devices, IoT devices, AIoT devices, and braking devices, etc. These devices include data processing and peripheral control according to the system-on-chip of the present invention, thus realizing the intelligence of industrial devices.
[0047] The system-on-chip 100 can be applied to a variety of intelligent devices. Its hardware configuration, such as the selection of the processor 110, the number of bits of the data bus / address bus, the capacity of the read-only memory 130 / random access memory 140, etc., will vary greatly according to the different application scenarios of intelligent devices. In the Internet of Things application scenarios with low-cost and low-power consumption requirements (such as industrial application scenarios), usually the capacity of the memory of the system-on-chip 100, especially the capacity of the random access memory 140, will be compressed. This requires that the program instructions stored in the read-only memory 130 have a high degree of integration, minimizing redundancy as much as possible to save the space of the random access memory 140 occupied during operation.
[0048] In an embodiment of the present invention, an executable file of the source program is stored in the read-only memory 130. The executable file includes a code segment (text segment), a data segment (data segment), and a bss segment. Among them, the data segment and the bss segment are used to store constants and variables involved in the program, and their redundancy levels are relatively low, and the optimization space is not large. However, the text segment may have different degrees of redundancy due to factors such as the developer's coding habits and the compilation efficiency of the compiler, and the optimization space is relatively large. Therefore, the present invention provides an instruction optimization method that can optimize the code instructions in the source program to reduce the storage space occupied by the code segment in the executable file of the source program. The optimized source program is compiled to generate an executable file, and then the executable file is burned into the read-only memory 130 of the system-on-chip 100. As Figure 1 shown, after burning, the read-only memory 130 of the system-on-chip 100 stores the executable file of the optimized source program.
[0049] It should be noted that, in order to make the drawings more concise and facilitate understanding of the connection relationship between the processor and the memory, the program content stored in the memory, and the running process of the program in the present invention, Figure 1 only the processor 110, the bus 120, the read-only memory 130, and the random access memory 140 are shown, and other structures in the system-on-chip are omitted. Those skilled in the art can understand that, in addition to Figure 1 the structures shown in, the system-on-chip 100 also includes other structures, such as a coprocessor, a bus controller, an interface for coupling with external input / output devices, etc. The present invention does not limit the types, quantities, and connection relationships of other structures included in the system-on-chip. Any system-on-chip that stores the executable file of the source program optimized according to the instruction optimization method of the present invention is within the protection scope of the present invention.
[0050] In addition, the above description of the intelligent device is only illustrative. The intelligent device according to the present invention is not limited thereto. All intelligent devices that can perform data processing using the system-on-chip according to the present invention are within the protection scope of the present invention.
[0051] The instruction optimization method of the present invention is executed in a computing device. The computing device can be any device with storage and computing capabilities. For example, it can be a personal configured computer such as a desktop computer or a notebook computer; it can also be a mobile terminal such as a mobile phone, a tablet computer, a multimedia device, a wearable device, etc.; it can also be intelligent devices such as smart speakers, smart air conditioners, smart door locks, industrial control devices, sensing devices, IoT devices, etc. applied to the fields of home, industry, etc., but not limited thereto.
[0052] Figure 2 shows a schematic diagram of a computing device 200 according to an embodiment of the present invention. AsFigure 2 As shown, the computing device 200 includes a processor 210 and a memory 220. Program instructions are stored in the memory 220, and the program instructions stored in the memory 220 include an Integrated Development Environment (IDE) 222. Modules such as a text editor, a compiler, and an interpreter are integrated in the integrated development environment 222, which are used for the development and debugging of embedded programs. For example, a developer can perform corresponding operations in the integrated development environment 222 to write an embedded program and compile the program to generate an executable file.
[0053] The integrated development environment 222 can be, for example, a CDS development environment, a CEVA-X Toolbox development environment, an Xtensa development environment of Tensilica HiFi3, etc., but is not limited thereto.
[0054] In an embodiment of the present invention, the program instructions stored in the memory 220 further include an instruction optimization device for executing the instruction optimization method of the present invention (the instruction optimization device is not shown in Figure 2 . When the instruction optimization device is read and executed by the processor 210, the computing device 200 is caused to execute the instruction optimization method of the present invention to optimize the source program. The instruction optimization device can be implemented as a functional module in the integrated development environment 222 or as an independent software, and the present invention does not limit the implementation manner of the instruction optimization device.
[0055] When the computing device 200 completes the optimization of the source program according to the instruction optimization method of the present invention, the optimized source program is compiled to obtain an executable file of the source program. Subsequently, a burning tool (the burning tool can be integrated in the integrated development environment 222 or implemented as another software independent of the integrated development environment 222) is used to burn the executable file into the read-only memory of the system-on-chip.
[0056] Figure 3 shows a schematic diagram of a burning system 300 according to an embodiment of the present invention. As Figure 3 shown, the burning system 300 includes a system-on-chip 100, a computing device 200, and a debugger 310. The debugger 310 is respectively connected to the computing device 200 and the system-on-chip 100 and is used to form a data transmission channel between the two to achieve data transmission. The debugger 310 can be, for example, a JTAG debugger, which is respectively connected to the computing device 200 and the system-on-chip 100 through a USB interface and a JTAG interface.
[0057] After the computing device 200 optimizes the source program according to the instruction optimization method of the present invention, it compiles the optimized source program to obtain an executable file of the source program. Subsequently, a burning tool is used to burn the executable file into the system-on-chip 100 through the debugger 310. Specifically, as Figure 1 shown, it is burned into the read-only memory 130 of the system-on-chip 100. The processor 110 of the system-on-chip 100 can run the executable file in the read-only memory 130 to implement corresponding functions.
[0058] Figure 4 FIG. shows a flowchart of an instruction optimization method 400 according to an embodiment of the present invention. The method 400 is executed in a computing device (such as the aforementioned computing device 200) and is used to optimize a source program to reduce the storage space occupied by the code segment in the executable file of the source program. As Figure 4 shown, the method 400 starts from step S410.
[0059] In step S410, the source program is compiled to obtain an executable file of the source program, and the executable file includes storage area information occupied by multiple functions.
[0060] The source program refers to an uncompiled text file written according to certain programming language specifications. The source program is usually written in a high-level language and is a computer language instruction that can be read by humans. In the field of embedded development, the source program is usually written in the C language.
[0061] In step S410, a compiler is used to compile the source program, and the compiler is usually integrated in the integrated development environment 222. After compilation, an executable file of the source program can be obtained. As mentioned above, the executable file of the source program includes a code segment, a data segment, and a bss segment, and the code segment further includes storage area information occupied by multiple functions. The storage area information may be, for example, the memory logical address range and the size of the memory area occupied by each function (section), but is not limited thereto.
[0062] It should be noted that the functions included in the code segment of the executable file are usually not exactly the same as the functions defined in the source program. During the compilation of the source program by the compiler, some functions will be introduced, so that the number of functions included in the generated executable file is more than the number of functions defined in the source program.
[0063] According to one embodiment, in the C language, the functions introduced by the compiler during the compilation stage include standard library functions, and some functions generated by the compiler by default may also be introduced.
[0064] The C Standard Library is a library defined by the ANSI C standard and supported by all compilers, such as the stdio library, stdlib library, math library, etc. Each standard library contains several functions, and the functions in the standard library are called standard library functions.
[0065] When developers write source programs, they can call the library functions in the standard library by including the header files of the standard library, without having to define the library functions in the source program. For example, developers can include the stdio library by using the code #include<stdio.h> and call library functions such as printf and fopen in the stdio library. During the compilation stage, the compiler will automatically link the library functions called in the source program to the executable file, so that the executable file includes the code segments corresponding to the library functions.
[0066] During the compilation stage, in addition to the library functions called in the source program, the compiler may also automatically introduce functions that are not called in the source program (which may be library functions or other functions). Whether functions that are not called in the source program will be introduced and the number of such introduced functions are determined by the performance of the compiler itself.
[0067] Figure 5 Shows a schematic diagram of the code segments of an executable file generated by compilation according to an embodiment of the present invention. As Figure 5 shown, the code segments are further divided into multiple code slices (sections), and each code slice has corresponding storage area information, and the storage area information includes the range of the memory logical address occupied by the code slice and the size of the memory area occupied. For example, the memory logical address range occupied by the code slice initConfigRegs_section is bdee~bdff, a total of 0x12 bytes.
[0068] Figure 5 The.text in [ ] is the code of the source program itself, and other sections are the standard library functions called in the source program (such as fopen_section, fprintf_section, etc.), or functions generated by the compiler itself that are not called in the source program (such as initConfigRegs_section, etc.).
[0069] During the compilation process of step S410, the compiler may introduce functions that are not called in the source program. These functions introduced by the compiler increase the content of the code segment in the executable file, thereby increasing the storage space occupied by the executable file. However, since these functions are not called in the source program, the functions introduced by the compiler do not affect the operation of the source program and are redundant for the source program. In order to reduce the storage space occupied by the code segment in the executable file, subsequent steps S420 and S430 need to be executed to minimize or eliminate the storage space occupied by the functions introduced by the compiler and not called in the source program.
[0070] In step S420, the functions included in the executable file that are not called in the source program are regarded as redundant functions.
[0071] As mentioned above, the functions included in the executable file but not called in the source program do not affect the normal operation of the source program and are redundant for the source program. Therefore, in step S420, the functions included in the executable file that are not called in the source program are regarded as redundant functions, and in the subsequent step S430, the redundant functions are processed to minimize or eliminate the storage space occupied by the redundant functions introduced by the compiler.
[0072] In step S430, the redundant functions are defined as empty functions in the source program.
[0073] According to an embodiment, if the redundant function is a standard library function, in step S430, in addition to defining the redundant function as an empty function, it is also necessary to delete the reference to the corresponding standard library, that is, delete the code of the header file used to introduce the standard library in the source program. For example, if the redundant function is a function in the stdio library, in addition to defining the redundant function as an empty function, it is also necessary to delete the code of #include<stdio.h> in the source program.
[0074] According to an embodiment, multiple redundant functions are determined in step S420. In step S430, these multiple redundant functions are defined as empty functions one by one. Specifically, first, the definition format of the redundant function is determined. The definition format includes the return value type, function name, and parameter type. The definition format of the redundant function can be determined, for example, by querying the help documentation of the compiler. Subsequently, according to the determined definition format, the redundant function is defined as an empty function. The redefined redundant function has the same return value type, function name, and parameter type as the original redundant function, only the function body is different. The function body of the original redundant function includes code instructions for implementing the corresponding function, while the function body of the redefined redundant function does not include any functional code instructions and only includes a return instruction for returning a null value, such as return, return 0, or return null.
[0075] For example, in step S420, it is determined that the fprintf library function is a redundant function. By searching the help document, it is determined that the definition format of fprintf is as follows:
[0076] int fprintf(FILE*pStream, const char*pFormat,…){
[0077] Function body;
[0078] }
[0079] That is, the return value type of the function is int, the function name is printf, and the parameter type is (FILE*pStream, const char*pFormat, ...).
[0080] According to the above definition format, fprintf is redefined as an empty function. The redefined empty function is as follows:
[0081] int fprintf(FILE*pStream, const char*pFormat,…){
[0082] return 0;
[0083] }
[0084] In step S430, by redefining the redundant function as an empty function, the compiler will give priority to calling the custom empty function instead of calling the function with the same name in the standard library. Compared with the function with the same name in the standard library, the code instructions in the function body of the custom empty function are greatly reduced, thereby reducing the storage space occupied by the code segment corresponding to the function in the executable file, that is, reducing the storage space occupied by the code segment in the executable file.
[0085] According to another embodiment, the redundant functions determined in step S420 are library functions related to file input and output (file-io) introduced by the compiler, referred to as file input and output class functions. In this case, in step S430, all redundant functions in the introduced file input and output class can be defined as empty functions by defining the root function in the file input and output class as an empty function. Specifically, first, the root function in the file input and output class is determined, and the other functions in the file input and output class are all sub-functions of the root function. The setting of the root function of the file input and output class is related to the compiler, and the root functions of the file input and output classes of different compilers may be different. The root function of the file input and output class can be determined by querying the help document of the corresponding compiler or by experiment.
[0086] After determining the root functions in the input / output library, define the root functions as empty functions. The steps for defining the root functions as empty functions are the same as those for defining a single redundant function as an empty function described above, i.e., first, determine the definition format of the root function. The definition format includes the return value type, function name, and parameter types. The definition format of the root function can be determined, for example, by querying the help documentation of the compiler. Subsequently, according to the determined definition format, redefine the root function as an empty function. The redefined root function has the same return value type, function name, and parameter types as the original root function, only the function body is different. The function body of the original root function includes code instructions for implementing the corresponding functions, while the function body of the redefined root function does not include any functional code instructions, but only includes a return instruction for returning a null value, such as return, return 0, or return null.
[0087] For example, Figure 6 FIG. shows a schematic diagram of a code segment of an executable file compiled using the CEVA-X Toolbox development environment according to an embodiment of the present invention. Figure 6 In, fcloseall, fclose, _stdio_close, _stdout_write, _stdio_seek, _stdio_read, _stdio_write, and fflush are all redundant functions introduced by the compiler and are all functions related to file input / output. Through experiments, it is determined that in the CEVA-X Toolbox development environment, the root functions of the file input / output class are initFileIo and fcloseall, and all other file input / output class functions are sub-functions of these two functions or are generated by calling the sub-functions of these two functions. By searching the help documentation of CEVA-X Toolbox, the definition formats of initFileIo and fcloseall are determined as follows:
[0088]
[0089] According to the above definition formats, redefine initFileIo and fcloseall as empty functions. The redefined empty functions are as follows:
[0090]
[0091]
[0092] By redefining the root function as an empty function, the compiler will preferentially call the custom empty function instead of the function with the same name in the file input / output class. Since other functions in the file input / output class are either sub-functions of the root function or called by sub-functions of the root function, after defining the root function as an empty function, other functions in the file input / output class also lose their calling basis and will no longer be introduced by the compiler, thus reducing the storage space occupied by the code segment in the executable file.
[0093] The above takes the CEVA-X Toolbox development environment as an example to illustrate the optimization method of defining the root function of the file input / output class as an empty function. It should be noted that the optimization method of defining the root function of the file input / output class as an empty function is also applicable in other development environments. However, in other development environments, the definition format of the root function (including return value type, function name, etc.) may be different from that in the CEVA-X Toolbox development environment.
[0094] It should be noted that when the multiple redundant functions determined in step S420 are functions of the file input / output class introduced by the compiler, defining the root function of the file input / output class as an empty function is a shortcut to defining all multiple functions as empty functions. That is, when the multiple redundant functions determined in step S420 are input / output library functions introduced by the compiler, there are two ways to define these multiple redundant functions as empty functions. The first way is to define each of these multiple redundant functions as an empty function; the second way is to define the root function of the file input / output class as an empty function. Those skilled in the art can understand that when the number of redundant functions is large, the second way is more convenient and efficient.
[0095] Figure 7 The flowchart of an instruction optimization method 700 according to another embodiment of the present invention is shown. Method 700 is executed in a computing device (such as the aforementioned computing device 200). As Figure 7 shown, method 700 begins at step S710.
[0096] In step S710, the compilation mode of the source program is distinguished by defining a debug constant in the source program. The compilation mode includes a debug mode and a release mode. In the release mode, the debug function is defined as empty to delete the debug function.
[0097] For debugging convenience, the source program usually includes one or more debugging functions. A relatively common debugging function is the printf function, which is used to print the log information preset by the developer in the code on the default output device (usually the local monitor), facilitating the location of the problem area. In addition, there are differences in the presentation and storage methods of log information. Besides directly outputting the log information to the monitor through the serial port like the printf function, the log information can also be saved to a certain memory area or to a specified file. Correspondingly, the debugging functions can also be file input / output functions such as fopen, fwrite, fprintf, fclose, etc.
[0098] The debugging functions in the source program are only used for program debugging and do not affect the functions of the program itself. In the final release version of the program, there is no need for debugging functions. Therefore, in step S710, the compilation mode of the source program is distinguished by defining debugging constants in the source program. The compilation mode includes the debug mode (Debug) and the release mode (Release). That is, conditional compilation is achieved by defining debugging constants. In the debug mode, the debugging functions in the source program are retained, and the debugging functions are compiled normally to obtain an executable file in the debug version. The executable file in the debug version includes the code segment of the debugging functions. In the release mode, the debugging functions are defined as empty to delete the debugging functions, so that the executable file generated by compilation in the release version does not include the code segment of the debugging functions, thereby reducing the storage space occupied by the code segment in the executable file.
[0099] For example, if the debugging function in the source program is the printf function, the following code can be added to the source program to define the debugging function as empty in the release mode:
[0100]
[0101] In the above code, DEBUG is a debug constant. If the debug constant DEBUG is defined in the source program (for example, add the #define DEBUG statement before the #if DEBUG statement to define the debug constant DEBUG), the compiler will compile the source program in debug mode. In debug mode, the string dbg_printf is used to replace the printf function name, and the printf function is compiled normally. If the debug constant DEBUG is not defined in the source program (for example, there is no #define DEBUG statement before the #if DEBUG statement), the compiler will compile the source program in release mode. In release mode, the string dbg_printf is an empty string and no longer represents the printf function as in debug mode. Therefore, the printf function is deleted, and the code segment of the printf function is not included in the executable file generated by the compilation, thereby reducing the storage space occupied by the code segment in the executable file.
[0102] After defining the debug function in the release mode as empty, step S720 is executed.
[0103] In step S720, the source program is compiled in release mode to obtain the executable file of the source program. The executable file includes the storage area information occupied by multiple functions.
[0104] Since the debug function in the release mode is defined as empty in step S710, in step S720, the executable file obtained by compiling the source program in release mode does not include the code segment of the debug function.
[0105] The specific compilation process can refer to the relevant description of the foregoing step S410 and will not be elaborated here.
[0106] Subsequently, in step S730, the functions included in the executable file that are not called in the source program are regarded as redundant functions.
[0107] Subsequently, in step S740, the redundant functions are defined as empty functions in the source program.
[0108] Steps S730 and S740 are the same as the foregoing steps S420 and S430, and the specific implementation process can refer to the relevant description of the foregoing steps S420 and S430 and will not be elaborated here.
[0109] The following gives an embodiment of method 700:
[0110] The source program is written in C language, and the debug function in the source program is the printf function. The compiler uses the CEVA-XToolbox development environment.
[0111] First, use the DEBUG constant in the source program to distinguish between the Debug and Release compilation modes. In the Debug mode, retain the debug function printf in the source program; in the Release mode, define the debug function printf as empty. The relevant code is as follows:
[0112] #if DEBUG
[0113] #define PRINTF(x)printf x
[0114] #include<stdio.h>
[0115] #include<stdlib.h>
[0116] #else
[0117] #define PRINTF(x)((void)0)
[0118] Subsequently, compile the source program in the Release mode to obtain the executable file of the source program, and the executable file includes the storage area information occupied by multiple functions.
[0119] Subsequently, regard the functions included in the executable file that are not called in the source program as redundant functions. For example, the redundant functions are library functions related to file input and output.
[0120] Subsequently, define the redundant functions as empty functions by defining the root functions of the file input and output classes as empty functions. In the CEVA-X Toolbox development environment, the root functions of the file input and output classes are initFileIo and fcloseall. Therefore, redefine these two functions as empty functions in the source program.
[0121] The relevant code is as follows:
[0122]
[0123] After deleting the debug functions in the Release mode and defining the redundant functions as empty functions, the optimization of the source program is completed. Subsequently, compile the optimized source program in the Release mode to obtain the executable file of the optimized source program, and burn the executable file into the read-only memory of the system-on-chip. Depending on the selected compiler (development environment), the storage space occupied by the executable file of the optimized source program will be reduced to varying degrees compared to the storage space occupied by the executable file before optimization.
[0124] The following table shows the optimization effects of the instruction optimization method 700 of the present invention on different development environments.
[0125]
[0126] As can be seen from the above table, after adopting the instruction optimization method 700 of the present invention, for the above various development environments, the storage space occupied by the code segment of the generated executable file after compilation is reduced to varying degrees.
[0127] According to one embodiment, the instruction optimization method 400 or 700 may further include the following steps (this step is not shown in Figure 4 or Figure 7 ): When the executable file includes two functions with the same function but different parameter precisions, adjust the parameters of the two functions to the same precision.
[0128] For example, as Figure 8 shown, the code segment of the executable file includes the sqrt function and the sqrtf function. These two functions have the same function, both used to calculate the square root, but their parameter precisions are different. The parameter precision of the sqrt function is double-precision floating-point (double), and the parameter precision of the sqrtf function is single-precision floating-point (float). By adjusting the parameters of the two functions to the same precision, the code segment corresponding to one of the functions can be deleted, thereby reducing the storage space occupied by the code segment in the executable file. Those skilled in the art can understand that it is not necessary to adjust the parameters of the two functions to the same precision when the executable file includes two functions with the same function but different parameter precisions. In some cases, it is indeed necessary to keep the two functions with the same function but different parameter precisions at the same time. In this case, it is necessary to keep both functions and there is no need to adjust the parameter precisions of the two functions.
[0129] According to one embodiment, in addition to deleting debug functions in the release mode, defining redundant functions as empty functions, and adjusting the parameters of functions with the same function but different parameter precisions to the same precision, it is also possible to reduce the call of library functions by adopting other methods (such as customizing certain functions) to replace library functions, thereby reducing the storage space occupied by the code segment. For example, malloc is a library function for memory space management. Those skilled in the art can adopt other methods to replace the function of this function, thereby removing the code segment corresponding to the malloc function in the executable file and saving storage space.
[0130] The various technologies described herein can be implemented in combination with hardware or software, or a combination thereof. Thus, the methods and apparatuses of the present invention, or certain aspects or portions of the methods and apparatuses of the present invention, may take the form of program code (i.e., instructions) embedded in a tangible medium, such as a removable hard disk, a USB flash drive, a floppy disk, a CD-ROM, or any other machine-readable storage medium, wherein when the program is loaded into and executed by a machine such as a computer, the machine becomes an apparatus for practicing the present invention.
[0131] In the case where the program code is executed on a programmable computer, the computing device generally includes a processor, a processor-readable storage medium (including volatile and non-volatile memory and / or storage elements), at least one input device, and at least one output device. Among them, the memory is configured to store the program code; the processor is configured to execute the instruction optimization method of the present invention according to the instructions in the program code stored in the memory.
[0132] By way of example and not limitation, the readable medium includes a readable storage medium and a communication medium. The readable storage medium stores information such as computer-readable instructions, data structures, program modules, or other data. The communication medium generally embodies computer-readable instructions, data structures, program modules, or other data in a modulated data signal such as a carrier wave or other transmission mechanism, and includes any information delivery medium. A combination of any of the above is also included within the scope of the readable medium.
[0133] In the specification provided herein, the algorithms and displays are not inherently related to any particular computer, virtual system, or other device. A variety of general-purpose systems can also be used in conjunction with the examples of the present invention. Based on the above description, the structure required to construct such systems is obvious. In addition, the present invention is not directed to any particular programming language. It should be understood that the content of the present invention described herein can be implemented using a variety of programming languages, and the description of a particular language above is for the purpose of disclosing the best mode of the present invention.
[0134] In the specification provided herein, a large number of specific details are set forth. However, it can be understood that the embodiments of the present invention can be practiced without these specific details. In some instances, well-known methods, structures, and techniques have not been shown in detail so as not to obscure the understanding of this specification.
[0135] Similarly, it should be understood that, for the purpose of streamlining the present disclosure and aiding in the understanding of one or more of the various inventive aspects, in the foregoing description of the exemplary embodiments of the present invention, the various features of the present invention are sometimes grouped together in a single embodiment, figure, or description thereof. However, the disclosed method should not be construed as reflecting an intention that the claimed invention requires more features than are expressly recited in each claim. Rather, as reflected in the following claims, the inventive aspects lie in less than all the features of the single foregoing disclosed embodiment. Thus, the claims following the detailed description are hereby expressly incorporated into the detailed description, with each claim standing on its own as a separate embodiment of the present invention.
[0136] Those skilled in the art should understand that the modules, units, or components of the devices in the examples disclosed herein may be arranged in the devices as described in this embodiment, or alternatively may be located in one or more devices different from the devices in this example. The modules in the foregoing examples may be combined into one module or further divided into multiple sub-modules.
[0137] Those skilled in the art can understand that the modules in the devices of the embodiments can be adaptively changed and arranged in one or more devices different from this embodiment. The modules, units, or components in the embodiments can be combined into one module, unit, or component, and furthermore can be divided into multiple sub-modules, sub-units, or sub-components. Except that at least some of such features and / or processes or units are mutually exclusive, any combination can be used to combine all the features disclosed in this specification (including the accompanying claims, abstract, and drawings) and all the processes or units of any method or device so disclosed. Unless otherwise expressly stated, each feature disclosed in this specification (including the accompanying claims, abstract, and drawings) can be replaced by an alternative feature that provides the same, equivalent, or similar purpose.
[0138] In addition, those skilled in the art can understand that although some of the embodiments described herein include certain features included in other embodiments rather than other features, the combination of the features of different embodiments means that it is within the scope of the present invention and forms different embodiments. For example, in the following claims, any one of the claimed embodiments can be used in any combination.
[0139] In addition, some of the embodiments described herein are described as a method or a combination of method elements that can be implemented by a processor of a computer system or by other devices performing the functions. Therefore, a processor having the necessary instructions for implementing the method or method elements forms a device for implementing the method or method elements. In addition, the elements described herein in the device embodiments are examples of the following devices: the device is used to implement the functions performed by the elements for the purpose of implementing the invention.
[0140] As used herein, unless otherwise specified, the use of ordinal numbers "first", "second", "third", etc. to describe ordinary objects only indicates different instances of similar objects, and is not intended to imply that the objects so described must have a given order in time, space, ranking, or in any other way.
[0141] Although the present invention has been described in terms of a limited number of embodiments, those skilled in the art in this technical field will appreciate, upon the benefit of the above description, that other embodiments can be conceived within the scope of the invention thus described. In addition, it should be noted that the language used in this specification is mainly selected for the purpose of readability and teaching, rather than for the purpose of explaining or limiting the subject matter of the invention. Therefore, many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the appended claims. For the scope of the present invention, the disclosure of the present invention is illustrative rather than restrictive, and the scope of the present invention is defined by the appended claims.
Claims
1. An instruction optimization method, suitable for reducing the storage space occupied by the code segment in the executable file of the source program. The method includes the steps of: Compiling the source program using a compiler to obtain the executable file of the source program, where the executable file includes storage area information occupied by multiple functions; Regarding the functions included in the executable file that are not called in the source program as redundant functions, where the redundant functions are standard library functions introduced by the compiler ; and Redefining the redundant functions as empty functions in the source program according to the definition format of the redundant functions; Compiling the source program again to obtain the executable file of the source program.
2. The method according to claim 1, wherein, The storage space is the read-only storage space of the system-on-chip for Internet of Things devices.
3. The method according to claim 1, wherein, The source program includes at least one debugging function for program debugging. Before the step of compiling the source program, it further includes the steps of: Distinguishing the compilation mode of the source program by defining a debugging constant in the source program, where the compilation mode includes a debugging mode and a release mode; In the release mode, defining the debugging function as empty to delete the debugging function.
4. The method according to claim 3, wherein, The step of compiling the source program includes: Compiling the source program in the release mode.
5. The method according to any one of claims 1-4, wherein, The step of redefining the redundant functions as empty functions in the source program according to the definition format of the redundant functions includes: Determining the definition format of the redundant functions, where the definition format includes the return value type, function name, and parameter type; Defining the redundant functions as empty functions according to the definition format.
6. The method according to claim 1, wherein, The redundant functions are file input / output class functions introduced by the compiler.
7. The method according to claim 6, wherein, The step of redefining the redundant functions as empty functions in the source program according to the definition format of the redundant functions includes: Determining the root function in the file input / output class, where other functions in the file input / output class are sub-functions of the root function; Defining the root function as empty.
8. The method according to claim 7, wherein, The step of defining the root function as empty includes: Determining the definition format of the root function; Defining the root function as empty according to the definition format.
9. The method according to any one of claims 1-8, wherein, It further includes the step of: When the executable file includes two functions with the same function but different parameter precisions, adjusting the parameters of the two functions to the same precision.
10. A computing device, including: At least one processor; and A memory storing program instructions, which when read and executed by the processor, cause the computing device to execute the method according to any one of claims 1-9.
11. A readable storage medium storing program instructions, which, when read and executed by a computing device, cause the computing device to execute the method according to any one of claims 1-9.
12. A system-on-chip, comprising: at least one processor; and a read-only memory storing an executable file of a source program optimized according to the instruction optimization method according to any one of claims 1-9.
13. An intelligent device comprising the system-on-chip according to claim 12.
Citation Information
Patent Citations
Method and device for detecting useless codes and computer readable storage medium
CN108132790A
An assembly-level shader program link optimization method
CN109726816A