A portion of the disclosure of this patent document contains material, which is subject to copyright protection. The owner has no objection to the facsimile reproduction by any one of the patent document or the patent disclosure, as it appears in the Patent and Trademark Office patent file or records, but otherwise reserves all copyrights whatsoever.
Certain marks referenced herein may be common law or registered trademarks of third parties affiliated or unaffiliated with the applicant or the assignee. Use of these marks is for providing an enabling disclosure by way of example and shall not be construed to limit the scope of the claimed subject matter to material associated with such marks.
The claimed subject matter relates generally to program code optimization and, more particularly, to branch elimination in a program code.
The source code of a program may comprise a branch instruction. The branch instruction generally includes a condition expression that is evaluated to return either a “true” or a “false” value. The return value may be represented as a sequence of bits (i.e., a sequence of 0s and 1s). Typically, a bit sequence in which each bit is equal to zero (e.g., 00000) is a “false” value, and any other bit sequence is a “true” value.
The value of the condition expression is used to determine whether the program is executed according to a first control path (i.e., a first set of instructions) or a second control path (i.e., a second set of instructions). For example, if the value is “true,” the program is executed according to the first control path; if the value is “false,” the program is executed according to the second control path.
Execution performance of a program code may be optimized if branching instructions are reduced or eliminated. Improved systems and methods are needed for replacing a branch instruction with one or more instructions that do not require branching.
The present disclosure is directed to systems and corresponding methods that facilitate optimizing program code using branch elimination.
For purposes of summarizing, certain aspects, advantages, and novel features have been described herein. It is to be understood that not all such advantages may be achieved in accordance with any one particular embodiment. Thus, the claimed subject matter may be embodied or carried out in a manner that achieves or optimizes one advantage or group of advantages without achieving all advantages as may be taught or suggested herein.
In accordance with one embodiment, a method for optimizing program code is provided. The method comprises detecting a branch instruction comprising a condition expression, wherein the branch instruction, when executed by a processor, causes the processor to execute either a first set of instructions or a second set of instructions according to a value of the condition expression; and replacing the branch instruction with a third set of instructions, wherein the third set of instructions, when executed by a processor, has a collective effect same as if either the first or second set of instructions were executed according to the value of the condition expression. The third set of instructions comprises a negation instruction to normalize the value of the condition expression.
In accordance with another embodiment, a system comprising one or more logic units is provided. The one or more logic units are configured to perform the functions and operations associated with the above-disclosed methods. In accordance with yet another embodiment, a computer program product comprising a computer useable medium having a computer readable program is provided. The computer readable program when executed on a computer causes the computer to perform the functions and operations associated with the above-disclosed methods.
One or more of the above-disclosed embodiments in addition to certain alternatives are provided in further detail below with reference to the attached figures. The claimed subject matter is not, however, limited to any particular embodiment disclosed.
Embodiments of the claimed subject matter are understood by referring to the figures in the attached drawings, as provided below.
Features, elements, and aspects that are referenced by the same numerals in different figures represent the same, equivalent, or similar features, elements, or aspects, in accordance with one or more embodiments.
In the following, numerous specific details are set forth to provide a thorough description of various embodiments of the claimed subject matter. Certain embodiments may be practiced without these specific details or with some variations in detail. In some instances, certain features are described in less detail so as not to obscure other aspects of the disclosed embodiments. The level of detail associated with each of the elements or features should not be construed to qualify the novelty or importance of one feature over the others.
Branch elimination may be performed on a program code to improve the execution performance of a program when the program is executed. For example, the source code shown in
Referring to
Referring to
The value of the condition expression may be represented as a sequence of bits (i.e., a sequence of 0s and 1s). Normalizing the value of the condition expression causes the value to be represented as a sequence of bits in which each bit in the sequence is equal to 0 or a sequence of bits in which each bit in the sequence is equal to 1. That is, each “true” value is represented as a sequence of 1s, and each “false” value is represented as a sequence of 0s. For example, the “true” value 00001001 may be normalized to 11111111.
It is noteworthy that
Additionally, the above implementation may not be fully supported by high-level programming languages that support overloading of arithmetic operations between more complex types (e.g., classes in C++). In such a scenario, the above implementation is not always possible. Further, object-oriented programming languages may utilize object types for which a subtraction instruction is undefined.
In the following, an implementation is provided for normalizing the value of the condition expression so that x and x1 may be of any real number or object type.
Referring to
It is noteworthy that a negation instruction is supported by most programming languages for any real number or object type. Additionally, the above exemplary program code, once optimized, is executable according to a single control path regardless of the value of the condition expression. Further, the optimized source code retains the original syntax of the source code so that the program is easily understandable and thus easily debuggable.
Referring to
Referring to
The branch instruction, when executed by the processors 230 may cause the processors 230 to execute either a first set of instructions or a second set of instructions according to the value of the condition expression. For example, the branch instruction shown in
Upon detecting the branch instruction, the compiler 220 may replace the branch instruction with one or more non-branching instructions, where the non-branching instructions include a negation instruction to normalize the value of the condition expression (P310). For example, the compiler 220 may replace the branch instruction shown in
The non-branching instructions, when executed by the processors 230 may have a collective effect same as that of executing either the first or second set of instructions according to the value of the condition expression. For example, the non-branching instructions shown in
Advantageously, in a programming or execution environment where branching is not explicitly supported (e.g., gpgpu), the compiler 220 may be utilized to insert the non-branch instructions into the source code to implement branching. In an alternative embodiment, the branch instruction may be detected and replaced by a software developer utilizing the code editor 210, for example.
Once the branch instruction is detected and replaced, the compiler 220 may compile the source code into an executable format for execution by the processors 230 (P330). Replacing the branch instruction with the non-branching instructions may improve the performance of the program during execution, particularly if the branch instruction contributed to poor performance during a prior execution of the program.
Branch elimination, as provided above, is a platform-independent optimization that may be applied to a program by a software developer without utilizing a compiler. This may provide the software developer with more control over the program code execution. Further, branch elimination may be applied to a program that does not utilize compiler optimizations or to a program that utilizes a general-purpose compiler without branch elimination support.
In different embodiments, the claimed subject matter may be implemented either entirely in the form of hardware or entirely in the form of software, or a combination of both hardware and software elements. For example, the software development environment 200 may be included in a controlled computing system environment that may be presented largely in terms of hardware components and software code executed to perform processes that achieve the results contemplated by the system of the claimed subject matter.
Referring to
As provided here, software elements that are executed on the illustrated hardware elements are described in terms of specific logical/functional relationships. It should be noted, however, that the respective methods implemented in software may be also implemented in hardware by way of configured and programmed processors, ASICs (application specific integrated circuits), FPGAs (Field Programmable Gate Arrays) and DSPs (digital signal processors), for example.
Software environment 1120 is divided into two major classes comprising system software 1121 and application software 1122. In one embodiment, the code editor 210 or the compiler 220 may be implemented as system software 1121 or application software 1122 executed on one or more hardware environments to facilitate optimizing program code using branch elimination.
System software 1121 may comprise control programs, such as the operating system (OS) and information management systems that instruct the hardware how to function and process information. Application software 1122 may comprise but is not limited to program code, data structures, firmware, resident software, microcode or any other form of information or routine that may be read, analyzed or executed by a microcontroller.
In an alternative embodiment, the claimed subject matter may be implemented as computer program product accessible from a computer-usable or computer-readable medium providing program code for use by or in connection with a computer or any instruction execution system. For the purposes of this description, a computer-usable or computer-readable medium may be any apparatus that can contain, store, communicate, propagate or transport the program for use by or in connection with the instruction execution system, apparatus or device.
The computer-readable medium may be an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system (or apparatus or device) or a propagation medium. Examples of a computer-readable medium include a semiconductor or solid-state memory, magnetic tape, a removable computer diskette, a random access memory (RAM), a read-only memory (ROM), a rigid magnetic disk and an optical disk. Current examples of optical disks include compact disk read only memory (CD-ROM), compact disk read/write (CD-R/W) and digital video disk (DVD).
Referring to
A user interface device 1105 (e.g., keyboard, pointing device, etc.) and a display screen 1107 can be coupled to the computing system either directly or through an intervening I/O controller 1103, for example. A communication interface unit 1108, such as a network adapter, may be also coupled to the computing system to enable the data processing system to communicate with other data processing systems or remote printers or storage devices through intervening private or public networks. Wired or wireless modems and Ethernet cards are a few of the exemplary types of network adapters.
In one or more embodiments, hardware environment 1110 may not include all the above components, or may comprise other components for additional functionality or utility. For example, hardware environment 1110 can be a laptop computer or other portable computing device embodied in an embedded system such as a set-top box, a personal data assistant (PDA), a mobile communication unit (e.g., a wireless phone), or other similar hardware platforms that have information processing and/or data storage and communication capabilities.
In some embodiments of the system, communication interface 1108 communicates with other systems by sending and receiving electrical, electromagnetic or optical signals that carry digital data streams representing various types of information including program code. The communication may be established by way of a remote network (e.g., the Internet), or alternatively by way of transmission over a carrier wave.
Referring to
Software environment 1120 may also comprise browser software 1126 for accessing data available over local or remote computing networks. Further, software environment 1120 may comprise a user interface 1124 (e.g., a Graphical User Interface (GUI)) for receiving user commands and data. Please note that the hardware and software architectures and environments described above are for purposes of example, and one or more embodiments of the invention may be implemented over any type of system architecture or processing environment.
It should also be understood that the logic code, programs, modules, processes, methods and the order in which the respective processes of each method are performed are purely exemplary. Depending on implementation, the processes can be performed in any order or in parallel, unless indicated otherwise in the present disclosure. Further, the logic code is not related, or limited to any particular programming language, and may comprise of one or more modules that execute on one or more processors in a distributed, non-distributed or multiprocessing environment.
The claimed subject matter has been described above with reference to one or more features or embodiments. Those skilled in the art will recognize, however, that changes and modifications may be made to these embodiments without departing from the scope of the claimed subject matter. These and various other adaptations and combinations of the embodiments disclosed are within the scope of the claimed subject matter as defined by the claims and their full scope of equivalents.