JP6366033B2 - Optimization method of IF statement in program - Google Patents
Optimization method of IF statement in program Download PDFInfo
- Publication number
- JP6366033B2 JP6366033B2 JP2014098069A JP2014098069A JP6366033B2 JP 6366033 B2 JP6366033 B2 JP 6366033B2 JP 2014098069 A JP2014098069 A JP 2014098069A JP 2014098069 A JP2014098069 A JP 2014098069A JP 6366033 B2 JP6366033 B2 JP 6366033B2
- Authority
- JP
- Japan
- Prior art keywords
- conditional expression
- conditional
- program
- cost
- condition
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
- G06F8/41—Compilation
- G06F8/44—Encoding
- G06F8/443—Optimisation
- G06F8/4441—Reducing the execution time required by the program code
- G06F8/4442—Reducing the number of cache misses; Data prefetching
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
- G06F8/41—Compilation
- G06F8/44—Encoding
- G06F8/443—Optimisation
- G06F8/4441—Reducing the execution time required by the program code
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/36—Preventing errors by testing or debugging software
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/36—Preventing errors by testing or debugging software
- G06F11/3604—Software analysis for verifying properties of programs
- G06F11/3612—Software analysis for verifying properties of programs by runtime analysis
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/36—Preventing errors by testing or debugging software
- G06F11/362—Software debugging
- G06F11/3628—Software debugging of optimised code
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
- G06F8/41—Compilation
- G06F8/44—Encoding
- G06F8/443—Optimisation
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
- G06F8/41—Compilation
- G06F8/44—Encoding
- G06F8/443—Optimisation
- G06F8/4434—Reducing the memory space required by the program code
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Software Systems (AREA)
- Computer Hardware Design (AREA)
- Quality & Reliability (AREA)
- Devices For Executing Special Programs (AREA)
Description
本発明は、プログラム中のIF文を最適化するための方法に関し、より具体的には、プログラム中のIF文の条件式の包含関係を考慮して条件式の変形(移動、変数へのキャッシュ等)を含むIF文の最適化を図るための方法に関する。 The present invention relates to a method for optimizing an IF statement in a program, and more specifically, transformation of conditional expressions (movement, cache to variables) in consideration of the inclusion relation of conditional expressions in IF statements in a program. The present invention relates to a method for optimizing IF statements including
プログラムが機械生成される場合等において同じIF文が繰り返し生成される場合がある。その場合、同じIF文が繰り返し実行される分だけプログラムの処理パフォーマンス(速度)が遅くなってしまう。したがって、何らかの方法でIF文を最適化して、プログラム中の同じIF文が繰り返し実行されるような事態を避けることが望まれる。 In some cases, such as when a program is machine-generated, the same IF statement is repeatedly generated. In that case, the processing performance (speed) of the program is slowed by the amount that the same IF statement is repeatedly executed. Therefore, it is desirable to optimize the IF statement by some method to avoid a situation where the same IF statement in the program is repeatedly executed.
IF文の最適化に関連して、特許文献1には、自動作成されたコードの第1の中間表現を最適化手法により第2の中間表現に変換する方法において、コードの部分をそのコードが実行される位置に移動させること、複数のループ(IF文等)を単一のループに結合させることにより第1の中間表現の最適化を行って、ループの実行回数(メモリアクセス回数など)を低減することが開示されている。
In relation to the optimization of the IF statement,
特許文献2には、オリジナルコードから抽出された関数コール箇所にif文のブロックが存在し、そのif文の条件式に関数コールが使用されている場合に、その条件式をif文の前に移動して“local_cond1”に仮代入し、if文の条件式内では仮代入した“local_cond1”に置換し、if文内の条件式から関数コールを除去することを特徴とするプログラムコードの構造変換装置が開示されている。
In
特許文献1、2の方法では、IF文の条件式に包含関係がある場合についてIF分の最適化を図るための対応が採られておらず、また、IF文の条件式の移動に伴う利益(コスト)予測も十分に考慮されていない。
したがって、本発明の目的は、プログラムのIF文の条件式に包含関係がある場合について、その条件式の移動に伴う利益(コスト)予測をも行いながら、IF文の最適化によりプログラムの処理パフォーマンス(速度)を向上させることである。
In the methods of
Therefore, the object of the present invention is to optimize the IF statement and to improve the processing performance of the program while predicting the profit (cost) associated with the movement of the conditional expression when the conditional expression of the IF statement of the program has an inclusive relationship. (Speed) is to improve.
本発明は、プログラム中のIF文を最適化するための方法を提供する。その方法は、(a)プログラム中の複数のIF文の各条件式について、包含関係にある条件式の集合を求めるステップと、(b)集合の各々について、包含関係にある条件式の移動先として、プログラム中の実行頻度が小さい位置を当該条件式が含まれる集合の情報を用いて算出するステップと、(c)算出された条件式の移動先に当該条件式を移動させるステップとを含む。 The present invention provides a method for optimizing IF statements in a program. The method includes: (a) obtaining a set of conditional expressions in an inclusive relationship for each conditional expression of a plurality of IF statements in the program; and (b) a destination of the conditional expression in the inclusive relationship for each set. As follows: a step of calculating a position with a low execution frequency in the program using information on a set including the conditional expression; and (c) a step of moving the conditional expression to a destination of the calculated conditional expression. .
本発明の一態様では、集合を求めるステップ(a)は、包含関係にある条件式の各々において、肯定条件と否定条件を設定し、各条件について、部分集合である条件式の数と条件式が成立する確率とからコストを求めるステップを含む。そのコストは、例えば、各条件式について条件式の数と条件式が成立する確率との積として計算することができる。 In one aspect of the present invention, in the step (a) for obtaining a set, an affirmative condition and a negative condition are set for each conditional expression in an inclusive relationship, and the number of conditional expressions that are a subset and the conditional expression are set for each condition. A cost is obtained from the probability that is satisfied. The cost can be calculated as, for example, the product of the number of conditional expressions and the probability that the conditional expression is satisfied for each conditional expression.
本発明の一態様では、条件式を移動させるステップ(c)は、(c1)副作用を起こさない条件式を前記プログラム中の当該条件式が実行される可能性がある領域中の実行頻度が最小となる位置に移動させるステップと、(c2)副作用を起こし得る条件式をプログラム中の当該条件式が必ず実行されかつ移動による副作用が起こらない領域中の実行頻度が最小となる位置に移動させるステップと、を含む。 In one aspect of the present invention, the step (c) of moving the conditional expression includes (c1) executing a conditional expression that does not cause a side effect in a region where the conditional expression in the program may be executed at a minimum frequency. And (c2) a step of moving a conditional expression that may cause a side effect to a position where the conditional expression in the program is always executed and the frequency of execution in a region where the side effect due to the movement does not occur is minimized. And including.
本発明の一態様では、条件式を移動させるステップ(c)は、(c3)条件式の結果が変数にキャッシュされていない場合、包含関係にある条件式に関するIF文を生成しその結果を新たな変数にキャッシュするステップと、(c4)条件式の結果が変数にキャッシュされている場合、包含関係にある条件式の条件演算子を論理演算子に置き換えた後に当該条件式の結果を新たな変数にキャッシュするステップとを含む。 In one aspect of the present invention, in the step (c) of moving the conditional expression, (c3) if the result of the conditional expression is not cached in a variable, an IF statement relating to the conditional expression in the inclusive relation is generated and the result is newly updated. (C4) When the result of the conditional expression is cached in the variable, after replacing the conditional operator of the conditional expression in the inclusive relation with the logical operator, the result of the conditional expression is updated. Caching to variables.
本発明の一態様では、包含関係にある条件式に関するIF文を生成しその結果を新たな変数にキャッシュするステップ(c3)は、肯定条件と否定条件のコストの合計値が小さい順に当該IF文を生成しその結果を新たな変数にキャッシュすることを含む。 In one aspect of the present invention, the step (c3) of generating an IF statement relating to a conditional expression having an inclusive relationship and caching the result in a new variable includes the IF statement in ascending order of the total cost of the positive condition and the negative condition. And caching the result in a new variable.
本発明の一態様では、プログラム中の実行頻度が小さい位置を当該条件式が含まれる集合の情報を用いて算出するステップ(b)は、集合の情報として、肯定条件と否定条件を用いてデータフロー解析を行うことを含む。 In one aspect of the present invention, the step (b) of calculating a position with a low execution frequency in the program using information on the set including the conditional expression includes data using a positive condition and a negative condition as the set information. Including performing flow analysis.
本発明の一態様では、条件式を移動させるステップ(c)は、移動前の条件式の実行コストと移動後に設定される条件式の実行コストとを比較した結果、実行速度が向上する場合に実施される。 In one aspect of the present invention, the step (c) of moving the conditional expression is performed when the execution speed is improved as a result of comparing the execution cost of the conditional expression before the movement with the execution cost of the conditional expression set after the movement. To be implemented.
図面を参照しながら本発明の実施の形態を説明する。図1は、本発明の方法を実施するコンピュータの構成例を示すブロック図である。コンピュータ100は、バス30を介して相互に接続された演算処理装置(CPU)10、記憶手段20、各種I/F40を含む。各種I/F40は、入力I/F、出力I/F、外部記憶I/F、外部通信I/F等を含む総称として用いられ、各I/Fが、それぞれ対応するキーボード、マウス等の入力手段50、CRT、LCD等の表示手段60、USB接続の半導体メモリやHDD等の外部記憶手段70等に接続する。記憶手段20は、RAM、ROM等の半導体メモリ、HDD等を含むことができる。本発明の方法は、コンピュータ100が例えば記憶手段20、70に格納された所定のソフトウェア呼び出して実行することにより実施される。本発明の方法は、プログラムのデバッグ時あるいはコンパイル時に実行される。
Embodiments of the present invention will be described with reference to the drawings. FIG. 1 is a block diagram showing a configuration example of a computer that implements the method of the present invention. The
図2を参照しながら本発明の方法のフローについて説明する。図2は、本発明の方法の基本的なフローを示す図である。図2のステップS11において、編集対象となるプログラム中の複数のIF文の各条件式について、包含関係にある条件式の集合を求める。この集合を求める際に、包含関係にある条件式の各々において、肯定条件と否定条件を設定し、各条件について、部分集合である条件式の数と条件式が成立する確率とからコストを求める。 The flow of the method of the present invention will be described with reference to FIG. FIG. 2 is a diagram showing a basic flow of the method of the present invention. In step S11 of FIG. 2, a set of conditional expressions having an inclusion relationship is obtained for each conditional expression of a plurality of IF statements in the program to be edited. When determining this set, set an affirmative condition and a negative condition for each conditional expression in an inclusive relationship, and calculate the cost from the number of conditional expressions that are a subset and the probability that the conditional expression is satisfied for each condition. .
図3と図4に、ステップS11で求める集合の例を示す。図3の例では、肯定条件として、I=5、I=20、I=40の3つの等号(=)を含む条件式を設定し、それらの否定条件として、I!=5、I!=20、I!=40の3つの条件式を設定している。ここで、例えば“I!=5”は、Iが5に等しくないことを意味する。ここでの条件式の包含関係は、肯定条件の式同志の間では存在せず、肯定条件と否定条件の式間において存在する。例えば、否定条件のI!=5は、肯定条件のI=20とI=40の2式を包含する関係となる。同様に、否定条件のI!=20、I!=40は、それぞれ肯定条件のI=5及びI=40の2式、I=5及びI=20の2式を包含する関係となる。図3中の否定条件の行下の“部分集合である式の数”は、各否定条件式が包含する条件式の数である2を示している。上述したように、各肯定条件には包含関係が存在しないので肯定条件の行下の“部分集合である式の数”はいずれもゼロになる。 3 and 4 show examples of sets obtained in step S11. In the example of FIG. 3, a conditional expression including three equal signs (=) of I = 5, I = 20, and I = 40 is set as an affirmative condition, and I! = 5, I! = 20, I! = 3 conditional expressions are set. Here, for example, “I! = 5” means that I is not equal to 5. The inclusive relation of the conditional expressions here does not exist between the positive condition expressions but exists between the positive condition and negative condition expressions. For example, I! = 5 is a relationship including two expressions of I = 20 and I = 40 which are positive conditions. Similarly, the negative condition I! = 20, I! = 40 is a relationship including two expressions of positive conditions I = 5 and I = 40 and two expressions of I = 5 and I = 20, respectively. The “number of expressions as a subset” below the negative condition line in FIG. 3 indicates 2 which is the number of conditional expressions included in each negative conditional expression. As described above, since there is no inclusion relationship for each positive condition, the “number of expressions that are a subset” under the line of the positive condition is all zero.
図3中の仮コストは、“条件式が成り立つ確率”Pに“部分集合である式の数”Nを乗算して(P*Nとして)求めることができる。例えば、否定条件I!=5の仮コストは、0.96*2=1.92となる。他の条件式についても同様に計算して求めることができる。仮コスト合計は、肯定条件と否定条件のセットにおける合算値として求めることができる。例えば、I=5とI!=5の仮コスト合計は、0+1.92=1.92となる。他の条件式のセットについても同様に計算して求めることができる。なお、後述するように、この仮コスト合計値は、コード(IF文)の移動先においてIF文を生成する際のその生成順序(優先度)の判断基準となる。 The provisional cost in FIG. 3 can be obtained by multiplying “the probability that the conditional expression is satisfied” P by “the number of expressions as a subset” N (as P * N). For example, negative condition I! The provisional cost of = 5 is 0.96 * 2 = 1.92. Other conditional expressions can be calculated and obtained in the same manner. The provisional cost total can be obtained as a sum value in a set of positive conditions and negative conditions. For example, I = 5 and I! = 5, the total provisional cost is 0 + 1.92 = 1.92. Other sets of conditional expressions can be similarly calculated and obtained. As will be described later, this temporary cost total value is a criterion for determining the generation order (priority) when generating an IF sentence at the destination of the code (IF sentence).
図4を参照すると、ここでは各条件式が不等号を含む式となっている点を除いて、基本的に図3の等号を含む条件式の場合と同様な手順で集合を求めることができる。例えば、肯定条件のDD>29は、肯定条件のDD>30とDD>31の2式を包含する関係となるので、その“部分集合である式の数”は2となる。同様に、例えば、否定条件のDD≦29は、否定条件のDD≦28の1式を包含する関係となるので、その“部分集合である式の数”は1となる。他の条件式についても同様にして求めることができる。仮コスト及び仮コスト合計も図3の場合と同様に求めることができ、例示した肯定条件DD>29と否定条件DD≦29のセットの場合は、2つの仮コストを合算して仮コスト合計は、0.12+0.94=1.06となる。 Referring to FIG. 4, a set can be obtained basically in the same procedure as in the case of the conditional expression including the equal sign in FIG. 3, except that each conditional expression is an expression including an inequality sign. . For example, since the positive condition DD> 29 is a relationship including two expressions of positive conditions DD> 30 and DD> 31, the “number of expressions as a subset” is two. Similarly, for example, the negative condition DD ≦ 29 is a relationship including one expression of the negative condition DD ≦ 28, and therefore, the “number of expressions as a subset” is 1. Other conditional expressions can be obtained in the same manner. The provisional cost and the total provisional cost can also be obtained in the same manner as in FIG. 3. In the case of the set of the exemplified positive condition DD> 29 and negative condition DD ≦ 29, the total provisional cost is obtained by adding the two provisional costs. 0.12 + 0.94 = 1.06.
図2に戻って、次のステップS12において、ステップS11で取得された集合の各々について、包含関係にある条件式の移動先として、プログラム中の実行頻度が小さい位置を当該条件式が含まれる集合の情報を用いて算出する。その際、集合の情報として、上述した肯定条件と否定条件の各式を用いて、データフロー解析を行うことによりプログラム中の実行頻度が小さい位置を求めることができる。例えば、従来からあるデータフロー解析のアルゴリズム(“Lazy Code Motion”等)を用いて求めることができる。また、同じ実行頻度の中で最後の位置を求めるときに、従来は各条件式をデータフロー方程式で使うGEN集合に登録していたところを、当該条件式に対応する上述した肯定条件と否定条件の集合をGEN集合に組み入れて計算する。なお、この同じ実行頻度の中で最後の位置は、後述するコード(IF文、条件式)の移動先の候補となる。 Returning to FIG. 2, in the next step S12, for each set acquired in step S11, as a destination of the conditional expression in the inclusive relationship, a set including the conditional expression at a position with a low execution frequency in the program It calculates using the information. At that time, a position with a low execution frequency in the program can be obtained by performing a data flow analysis using the above-described expressions of the positive condition and the negative condition as set information. For example, it can be obtained using a conventional data flow analysis algorithm (such as “Lazy Code Motion”). In addition, when obtaining the last position within the same execution frequency, the previous positive condition and negative condition corresponding to the conditional expression described above are registered in the GEN set that each conditional expression is used in the data flow equation. The set of is incorporated into the GEN set and calculated. Note that the last position in the same execution frequency is a candidate for a movement destination of a code (IF statement, conditional expression) described later.
次のステップS13において、算出された条件式の移動先に当該条件式を移動させる。この移動させるステップでは、プログラムの処理パフォーマンス(速度)が実際に向上するように、下記の(a)〜(c)に示すように、各場合(条件)において対応する適切な処理を行う。 In the next step S13, the conditional expression is moved to the destination of the calculated conditional expression. In this moving step, appropriate processing corresponding to each case (condition) is performed as shown in the following (a) to (c) so that the processing performance (speed) of the program is actually improved.
(a)移動させるステップにおいて、条件式が副作用を起こさない条件式である場合、その条件式をプログラム中の当該条件式が実行される可能性がある領域中の実行頻度が最小となる位置に移動させる。その後、条件式を実行してその結果(0か1)を変数にキャッシュ(保管)する。実行頻度が最小となる位置は、上述したデータフロー方程式を用いた計算によって求める。ここで、副作用とは、一般に、プログラム中のある機能がコンピュータの論理的な状態を変化させ、それ以降で得られる結果に影響を与えることを意味する。代表的な例は、変数への値の代入におおいてその値が変化してしまう場合が該当する。条件式が副作用を起こし得る場合は、その条件式をプログラム中の当該条件式が必ず実行されかつ移動による副作用が起こらない領域中の実行頻度が最小となる位置に移動させる。その後、条件式を実行してその結果(0か1)を変数にキャッシュ(保管)する。 (A) In the moving step, if the conditional expression is a conditional expression that does not cause a side effect, the conditional expression is positioned at a position where the execution frequency in the area where the conditional expression is likely to be executed in the program is minimized. Move. Thereafter, the conditional expression is executed, and the result (0 or 1) is cached (stored) in a variable. The position where the execution frequency is minimized is obtained by calculation using the data flow equation described above. Here, a side effect generally means that a certain function in a program changes a logical state of a computer and affects a result obtained thereafter. A typical example corresponds to a case where the value changes when a value is assigned to a variable. If the conditional expression may cause a side effect, the conditional expression is moved to a position where the conditional expression in the program is always executed and the frequency of execution in the region where the side effect due to movement does not occur is minimized. Thereafter, the conditional expression is executed, and the result (0 or 1) is cached (stored) in a variable.
(b)条件式を移動させるステップにおいて、移動する条件式の結果が変数にキャッシュされていない場合において、その条件式が包含関係にある条件式を持っているときは、その包含関係にある条件式に関するIF文を生成しその結果を新たな変数にキャッシュする。その際、集合中の肯定条件と前記否定条件の仮コスト合計値が小さい順に当該IF文を生成しその結果を新たな変数にキャッシュする。一方、条件式の結果が変数にキャッシュされている場合、包含関係にある条件式の条件演算子(Conditional AND、 OR、 XOR)を論理演算子(Logical AND、 OR、 XOR)に置き換えた後に当該条件式の結果を新たな変数にキャッシュする。 (B) In the step of moving the conditional expression, when the result of the moving conditional expression is not cached in a variable, if the conditional expression has a conditional expression in an inclusive relation, the condition in the inclusive relation Generate an IF statement for the expression and cache the result in a new variable. At that time, the IF statements are generated in ascending order of the total provisional cost of the positive condition and the negative condition in the set, and the result is cached in a new variable. On the other hand, if the result of a conditional expression is cached in a variable, the conditional operator (Conditional AND, OR, XOR) in the conditional expression in the inclusive relation is replaced with a logical operator (Logical AND, OR, XOR). Cache the result of the conditional expression in a new variable.
(c)条件式を移動させるステップは、移動前の条件式の実行コストと移動後に設定される条件式の実行コストとを比較した結果、実行速度が向上する場合に実施することができる。すなわち、移動に伴い最適化が行われる条件式について、コスト利益(Cost-Benefit)計算を行い、ある利益(Benefit)が所定の閾値よりも大きい場合に、その条件式を計算で求めた変数またはキャッシュした変数で置き換える。そうでなければ、上記(a)及び(b)における変形(条件式の移動、作成、変数へのキャッシュ)は行わない。したがって、上記(a)及び(b)における変形は、実際にはコスト計算及び評価が行われるまでは実行せずに、その変形の情報をメモリに記憶しておくという方法を採用することができる。 (C) The step of moving the conditional expression can be performed when the execution speed is improved as a result of comparing the execution cost of the conditional expression before the movement with the execution cost of the conditional expression set after the movement. That is, the cost-benefit calculation is performed on the conditional expression that is optimized along with the movement, and when a certain profit (Benefit) is greater than a predetermined threshold, the conditional expression Replace with cached variables. Otherwise, the transformations in (a) and (b) above (movement and creation of conditional expressions, cache to variables) are not performed. Therefore, the deformation in the above (a) and (b) is not executed until the cost calculation and evaluation are actually performed, and a method of storing the deformation information in the memory can be adopted. .
コスト利益(Cost-Benefit)計算が必要な理由は以下のように説明することができる。すなわち、計算式の最適化と違いIF文の最適化は、最適化後も完全に条件式を削除できない場合が多く、遅くなる場合があるため、コストを計算する必要がある。速くなるかどうかは、最適化された条件式の合計の実行頻度、条件式のコスト、挿入する条件式の合計の実行頻度、キャッシュされた変数のアクセスコスト、によって決まる。例えば、次に示すように計算式の最適化と違いIF文の最適化で違いが生じる。 The reason why the cost-benefit calculation is necessary can be explained as follows. That is, unlike the optimization of the calculation formula, the optimization of the IF statement is often unable to completely delete the conditional formula even after the optimization, and may be slow. Therefore, it is necessary to calculate the cost. Whether it is faster depends on the total execution frequency of the optimized conditional expressions, the cost of the conditional expressions, the total execution frequency of the inserted conditional expressions, and the access cost of the cached variables. For example, as shown below, there is a difference between the optimization of the calculation formula and the optimization of the IF statement.
(A)計算式の最適化の場合:
(1) 元のプログラム(2) 最適化後
T = b + c
a = b + c a = T
: :
d = b + c d = T
(B)IF文の最適化の場合:
(1) 元のプログラム (2) 最適化後
T = (cond)
if cond IF T
: :
if cond IF T
条件式condのコストをC1、TのメモリアクセスコストをC2としたとき、(1)のコストはC1*2、(2)のコストはC1+C2*3、利益はC1−C2*3となる。その利益が所定の閾値以上のときに最適化を行う。
(A) For optimization of calculation formula:
(1) Original program (2) After optimization
T = b + c
a = b + ca = T
::
d = b + cd = T
(B) For IF statement optimization:
(1) Original program (2) After optimization
T = (cond)
if cond IF T
::
if cond IF T
When the cost of the conditional expression cond is C1, and the memory access cost of T is C2, the cost of (1) is C1 * 2, the cost of (2) is C1 + C2 * 3, and the profit is C1-
図5と図6において、上述した図2の処理フローに基づく条件式の包含関係を考慮したコード(条件式)移動の実施例を示す。図5は、条件式A<1及びA<5に対する集合を示す。集合内の各パラメータの意味は、図3と図4を参照しながら説明した通りである。図6は、図5の集合についてのコード移動の例を示す。図6の(a)は、従来のコード移動アルゴリズムによるコード移動の例であり、(b)は、本発明によるコード移動の例である。(a)の従来例では、2つの条件式(A<1及びA<5)は最適化されない、すなわちその処理量(速度)が低減されない。一方、(b)の本発明の例では、新たなコード(IF文)の作成(1)〜(3)により、2つの条件式(A<1及びA<5)を最適化することができる。 5 and 6 show examples of code (conditional expression) movement in consideration of the inclusion relation of conditional expressions based on the processing flow of FIG. 2 described above. FIG. 5 shows a set for conditional expressions A <1 and A <5. The meaning of each parameter in the set is as described with reference to FIGS. FIG. 6 shows an example of code movement for the set of FIG. 6A shows an example of code movement by a conventional code movement algorithm, and FIG. 6B shows an example of code movement according to the present invention. In the conventional example of (a), the two conditional expressions (A <1 and A <5) are not optimized, that is, the processing amount (speed) is not reduced. On the other hand, in the example of the present invention of (b), two conditional expressions (A <1 and A <5) can be optimized by creating new codes (IF statements) (1) to (3). .
なお、図6の例では、AへのメモリアクセスコストをC1、TへメモリアクセスコストをC2、A<1が成り立つときの確率を50%、 左のパスが実行される確率を50%としたとき、
元のコスト:1.5*C1
最適化後のコスト:1.25*C1+2*C2
最適化による利益:0.25*C1−2*C2
となる。その最適化による利益(0.25*C1−2*C2)の値が所定の閾値よりも大きい場合に変形を行うようにすることができる。
In the example of FIG. 6, the memory access cost to A is C1, the memory access cost to T is C2, the probability that A <1 holds is 50%, and the probability that the left pass is executed is 50%. When
Original cost: 1.5 * C1
Cost after optimization: 1.25 * C1 + 2 * C2
Benefit from optimization: 0.25 * C1-2 * C2
It becomes. When the value of the profit (0.25 * C1-2 * C2) by the optimization is larger than a predetermined threshold value, the transformation can be performed.
図7に上述した図2の処理フローに基づく、包含関係があるIF文の作成例を示す。図7(a)は、元のプログラムにおけるコードであり、(b)は本発明による最適化(移動、変形)後のコードである。この場合も、TXNFL―DATE―DDとの比較コストをC1、 一時変数のメモリアクセスコストをC2とした場合、
(a)の場合のコスト:4*C1
(b)の場合のコスト:1.11*c1+8*C2
最適化による利益:2.89*C1−8*C2
となる。したがって、その最適化による利益(2.89*C1−8*C2)の値が所定の閾値よりも大きい場合に変形(コード移動)を行うようにすることができる。
FIG. 7 shows an example of creating an IF statement having an inclusion relationship based on the processing flow of FIG. 2 described above. FIG. 7A shows the code in the original program, and FIG. 7B shows the code after optimization (movement, deformation) according to the present invention. Also in this case, when the comparison cost with TXNFL-DATE-DD is C1, and the memory access cost of the temporary variable is C2,
Cost in case of (a): 4 * C1
Cost in case of (b): 1.11 * c1 + 8 * C2
Benefit from optimization: 2.89 * C1-8 * C2
It becomes. Therefore, when the value of the profit (2.89 * C1-8 * C2) by the optimization is larger than a predetermined threshold value, the transformation (code movement) can be performed.
1.副作用を起こさない条件式の最適化の例
次のIF文が4つある場合の最適化例は下記のようになる。
IF R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5=SPACE
OR R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5=LOW-VALUE
上記のIF文は次のような命令列が生成されている。この場合、メモリアクセスが多くそのコストが高い。
L 4,312(0,9)
CLC 304(12,4),12(12)
BC 8,2612(0,11)
CLI 304(4),X'00'
BC 7,2620(0,11)
CLC 305(11,4),304(4)
BC 7,2620(0,11)
これに含まれる2つの条件式は副作用を起こさず、IF文間で値は変わらないものとする。本発明の方法では、次のように変形される。
Cond1 = (R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5 = SPACE)
Cond2 = (R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5 = LOW-VALUE)
Cond3 = Cond1 | Cond2 // Conditional ORをLogical ORで高速化
この場合の最適化による利益が所定の閾値よりも大きいと判定され、4つのIF文を上記のCond3の条件式で置き換える。
IF Cond3 処理1
処理2
IF Cond3 処理3
処理4
IF Cond3 処理5
処理6
IF Cond3 処理7
1. Example of optimizing conditional expressions that cause no side effects An example of optimization when there are four IF statements is as follows.
IF R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5 = SPACE
OR R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5 = LOW-VALUE
In the above IF statement, the following instruction sequence is generated. In this case, there are many memory accesses and the cost is high.
L 4,312 (0,9)
CLC 304 (12,4), 12 (12)
BC 8,2612 (0,11)
CLI 304 (4), X'00 '
BC 7,2620 (0,11)
CLC 305 (11,4), 304 (4)
BC 7,2620 (0,11)
The two conditional expressions included in this do not cause side effects, and the values do not change between IF statements. The method of the present invention is modified as follows.
Cond1 = (R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5 = SPACE)
Cond2 = (R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5 = LOW-VALUE)
Cond3 = Cond1 | Cond2 // Speed up Conditional OR with Logical OR
In this case, it is determined that the benefit of optimization is greater than a predetermined threshold, and the four IF statements are replaced with the above Condition3 conditional expression.
IF
IF
IF
Process 6
IF Cond3 treatment 7
2.副作用を起こし得る条件式の最適化の例
上記1.のIF文の2つ目の条件式(R−RESTOCK・・・)がアクセス例外を起こし得るとする。その場合、上記(b)の移動ステップでは次のように変形される。
Cond1 = (R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5 = SPACE)
IF Cond1 OR
R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5 = LOW-VALUE
Cond3 = 1
ELSE
Cond3 = 0
END-IF
2. Examples of optimization of conditional expressions that can cause side effects Suppose that the second conditional expression (R-RESTOCK...) Of the IF statement can cause an access exception. In this case, the movement step (b) is modified as follows.
Cond1 = (R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5 = SPACE)
IF Cond1 OR
R-RESTOCK-01 OF REPORT-RESTOCK OF REPORT-BMO5 = LOW-VALUE
Cond3 = 1
ELSE
Cond3 = 0
END-IF
図7の包含関係があるIF文の作成例について、元のプログラムと最適化後のプログラムをソースコードレベルで作成し、両者の処理(実行)速度を調べた結果、最適化後のプログラムは元のプログラムよりも約40%程度の速度を向上させることができることを確認した。 As for the example of creating the IF statement having the inclusion relationship shown in FIG. 7, the original program and the optimized program are created at the source code level, and the processing (execution) speed of both is examined. It was confirmed that the speed of about 40% can be improved as compared with the program.
本発明の実施形態について、図を参照しながら説明をした。しかし、本発明はこれらの実施形態に限られるものではない。本発明はその趣旨を逸脱しない範囲で当業者の知識に基づき種々なる改良、修正、変形を加えた態様で実施できるものである。 Embodiments of the present invention have been described with reference to the drawings. However, the present invention is not limited to these embodiments. The present invention can be implemented in variously modified, modified, and modified embodiments based on the knowledge of those skilled in the art without departing from the spirit of the present invention.
30 バス
100 コンピュータ
30
Claims (9)
プログラム中の複数のIF文の各条件式について、包含関係にある条件式の集合を求めるステップと、
前記集合の各々について、包含関係にある条件式の移動先として、前記プログラム中の実行頻度が小さい位置を当該条件式が含まれる前記集合の情報を用いて算出するステップと、
算出された前記条件式の移動先に当該条件式を移動させるステップと、を含む方法。 A method for optimizing an IF statement in a program, executed by a computer,
Obtaining a set of conditional expressions in an inclusive relationship for each conditional expression of a plurality of IF statements in the program;
For each of the sets, as a destination of conditional expressions in an inclusive relationship, calculating a position with a low execution frequency in the program using information on the set including the conditional expressions;
Moving the conditional expression to the calculated destination of the conditional expression.
前記包含関係にある条件式の各々において、肯定条件と否定条件を設定し、各条件について、部分集合である条件式の数と条件式が成立する確率とからコストを求めるステップを含む、請求項1に記載の方法。 The step of obtaining the set includes:
A positive condition and a negative condition are set in each of the conditional expressions in the inclusion relationship, and a cost is obtained for each condition from the number of conditional expressions that are a subset and the probability that the conditional expression is satisfied. The method according to 1.
副作用を起こさない条件式を前記プログラム中の当該条件式が実行される可能性がある領域中の実行頻度が最小となる位置に移動させるステップと、
副作用を起こし得る条件式を前記プログラム中の当該条件式が必ず実行されかつ移動による副作用が起こらない領域中の実行頻度が最小となる位置に移動させるステップと、を含む請求項1に記載の方法。 The step of moving the conditional expression includes:
Moving a conditional expression that does not cause a side effect to a position where the execution frequency in the region where the conditional expression in the program may be executed is minimized;
The method according to claim 1, further comprising: moving a conditional expression that may cause a side effect to a position where the conditional expression in the program is always executed and no side effect due to movement occurs in a region where the execution frequency is minimized. .
前記条件式の結果が変数にキャッシュされていない場合、前記包含関係にある条件式に関するIF文を生成しその結果を新たな変数にキャッシュするステップと、
前記条件式の結果が変数にキャッシュされている場合、前記包含関係にある条件式の条件演算子を論理演算子に置き換えた後に当該条件式の結果を新たな変数にキャッシュするステップと、を含む請求項2に記載の方法。 The step of moving the conditional expression includes:
If the result of the conditional expression is not cached in a variable, generating an IF statement related to the conditional expression in the inclusion relationship and caching the result in a new variable;
If the result of the conditional expression is cached in a variable, the condition operator of the conditional expression in the inclusion relation is replaced with a logical operator, and then the result of the conditional expression is cached in a new variable. The method of claim 2 .
Priority Applications (5)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
JP2014098069A JP6366033B2 (en) | 2014-05-09 | 2014-05-09 | Optimization method of IF statement in program |
US14/695,274 US9298434B2 (en) | 2014-05-09 | 2015-04-24 | Optimizing if statements in computer programming |
US14/748,305 US9298435B2 (en) | 2014-05-09 | 2015-06-24 | Optimizing if statements in computer programming |
US15/045,731 US9495140B2 (en) | 2014-05-09 | 2016-02-17 | Optimizing if statements in computer programming |
US15/054,589 US9507576B2 (en) | 2014-05-09 | 2016-02-26 | Optimizing if statements in computer programming |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
JP2014098069A JP6366033B2 (en) | 2014-05-09 | 2014-05-09 | Optimization method of IF statement in program |
Publications (2)
Publication Number | Publication Date |
---|---|
JP2015215756A JP2015215756A (en) | 2015-12-03 |
JP6366033B2 true JP6366033B2 (en) | 2018-08-01 |
Family
ID=54367906
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
JP2014098069A Active JP6366033B2 (en) | 2014-05-09 | 2014-05-09 | Optimization method of IF statement in program |
Country Status (2)
Country | Link |
---|---|
US (4) | US9298434B2 (en) |
JP (1) | JP6366033B2 (en) |
Families Citing this family (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
JP6366033B2 (en) | 2014-05-09 | 2018-08-01 | インターナショナル・ビジネス・マシーンズ・コーポレーションInternational Business Machines Corporation | Optimization method of IF statement in program |
US10795682B2 (en) | 2016-12-28 | 2020-10-06 | Intel Corporation | Generating vector based selection control statements |
US11093224B2 (en) | 2019-04-24 | 2021-08-17 | International Business Machines Corporation | Compilation to reduce number of instructions for deep learning processor |
US20230030132A1 (en) * | 2021-08-02 | 2023-02-02 | Samsung Electronics Co., Ltd. | Application optimization method and apparatus supporting the same |
US11561778B1 (en) | 2021-11-23 | 2023-01-24 | International Business Machines Corporation | Instrumentation for nested conditional checks |
Family Cites Families (25)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
JPH02151973A (en) * | 1988-12-05 | 1990-06-11 | Hitachi Ltd | Rapid method of collating condition |
JPH06314203A (en) * | 1993-04-28 | 1994-11-08 | Fujitsu Ltd | Method and device for optimizing compiler |
JPH07160499A (en) * | 1993-12-09 | 1995-06-23 | Nec Corp | Optimization system for multidirectional branch instruction |
AUPM704494A0 (en) * | 1994-07-25 | 1994-08-18 | Canon Information Systems Research Australia Pty Ltd | Efficient methods for the interpretation of a graphical programming language |
JP3234552B2 (en) * | 1997-09-30 | 2001-12-04 | 松下電器産業株式会社 | Optimization device and computer readable recording medium |
US20020100031A1 (en) * | 2000-01-14 | 2002-07-25 | Miguel Miranda | System and method for optimizing source code |
JP4626032B2 (en) * | 2000-08-28 | 2011-02-02 | 株式会社デンソー | Program conversion apparatus, program conversion method, and recording medium |
CA2321018A1 (en) * | 2000-09-27 | 2002-03-27 | Ibm Canada Limited-Ibm Canada Limitee | Optimizing compilation by forward store movement |
US20050071827A1 (en) * | 2003-09-29 | 2005-03-31 | Lai Michael Y. | Method and apparatus for bit field optimization |
JP4754909B2 (en) * | 2004-09-22 | 2011-08-24 | パナソニック株式会社 | Compiler device, compiling method, compiler program |
JP2006135465A (en) * | 2004-11-04 | 2006-05-25 | Fuji Xerox Co Ltd | Document management apparatus, document management method, and computer program |
US7966610B2 (en) | 2005-11-17 | 2011-06-21 | The Mathworks, Inc. | Application of optimization techniques to intermediate representations for code generation |
ATE432507T1 (en) * | 2006-12-21 | 2009-06-15 | Ericsson Telefon Ab L M | OCCASIONING COMPUTER PROGRAM CODES |
US8285656B1 (en) * | 2007-03-30 | 2012-10-09 | Consumerinfo.Com, Inc. | Systems and methods for data verification |
US20090192880A1 (en) * | 2008-01-21 | 2009-07-30 | Michael Hood | Method of Providing Leads From a Trustworthy |
US8516468B2 (en) * | 2008-06-30 | 2013-08-20 | Intel Corporation | Multiversioning if statement merging and loop fusion |
KR20120058455A (en) * | 2009-04-28 | 2012-06-07 | 인터내셔널 비지네스 머신즈 코포레이션 | Method for converting program code of program running in multithreaded environment to program code having fewer lock conflicts, and computer program and computer system therefor |
US20110078424A1 (en) * | 2009-09-30 | 2011-03-31 | International Business Machines Corporation | Optimizing program code using branch elimination |
JP2012014526A (en) | 2010-07-01 | 2012-01-19 | Hitachi Ltd | Structure conversion apparatus for program code, and code structure conversion program |
WO2012031165A2 (en) * | 2010-09-02 | 2012-03-08 | Zaretsky, Howard | System and method of cost oriented software profiling |
US8700542B2 (en) * | 2010-12-15 | 2014-04-15 | International Business Machines Corporation | Rule set management |
WO2012164738A1 (en) * | 2011-06-03 | 2012-12-06 | 株式会社日立製作所 | Database management system, device, and method |
JP5458065B2 (en) * | 2011-07-25 | 2014-04-02 | 伸一 石田 | Device and program for supporting structural analysis of module |
US8966643B2 (en) * | 2011-10-08 | 2015-02-24 | Broadcom Corporation | Content security in a social network |
JP6366033B2 (en) | 2014-05-09 | 2018-08-01 | インターナショナル・ビジネス・マシーンズ・コーポレーションInternational Business Machines Corporation | Optimization method of IF statement in program |
-
2014
- 2014-05-09 JP JP2014098069A patent/JP6366033B2/en active Active
-
2015
- 2015-04-24 US US14/695,274 patent/US9298434B2/en not_active Expired - Fee Related
- 2015-06-24 US US14/748,305 patent/US9298435B2/en not_active Expired - Fee Related
-
2016
- 2016-02-17 US US15/045,731 patent/US9495140B2/en not_active Expired - Fee Related
- 2016-02-26 US US15/054,589 patent/US9507576B2/en not_active Expired - Fee Related
Also Published As
Publication number | Publication date |
---|---|
US20160162267A1 (en) | 2016-06-09 |
US9495140B2 (en) | 2016-11-15 |
JP2015215756A (en) | 2015-12-03 |
US9298434B2 (en) | 2016-03-29 |
US20150324176A1 (en) | 2015-11-12 |
US9507576B2 (en) | 2016-11-29 |
US20150324177A1 (en) | 2015-11-12 |
US9298435B2 (en) | 2016-03-29 |
US20160179491A1 (en) | 2016-06-23 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN111602151B (en) | Automated optimization of large-scale quantum circuits with continuous parameters | |
JP6366033B2 (en) | Optimization method of IF statement in program | |
CN103765402B (en) | What use mixed code signature tracing program calls context | |
US10146531B2 (en) | Method and apparatus for generating a refactored code | |
Carmona et al. | New region-based algorithms for deriving bounded Petri nets | |
US20130268921A1 (en) | Optimization of an application to reduce local memory usage | |
JP6245573B2 (en) | Method for obtaining execution frequency information of execution path on control flow graph, computer for obtaining the information, and computer program thereof | |
JP6111543B2 (en) | Method and apparatus for extracting similar sub time series | |
US20120311550A1 (en) | Method for optimizing binary codes in language having access to zoned decimal type variable, optimization apparatus and computer program for the same | |
WO2014136611A1 (en) | Cost computation device, cost computation method, and computer-readable recording medium | |
US11226798B2 (en) | Information processing device and information processing method | |
JP6319739B2 (en) | Method for inlining, computer program thereof, and computer for executing inlining | |
WO2020230043A1 (en) | Feature vector feasibilty estimation | |
JP6471615B2 (en) | Performance information generation program, performance information generation method, and information processing apparatus | |
JP2017224038A (en) | Cache miss estimation program, cache miss estimation method and information processing device | |
US11176473B2 (en) | Partially observed Markov decision process model and its use | |
US20170344351A1 (en) | Information processing apparatus, compiling management method, and recording medium | |
WO2017204139A1 (en) | Data processing apparatus, data processing method, and program recording medium | |
EP4227864A1 (en) | Evaluation method, evaluation device, and evaluation program | |
JP7035751B2 (en) | Code conversion device, code conversion method, and code conversion program | |
WO2015045091A1 (en) | Method and program for extraction of super-structure in structural learning of bayesian network | |
WO2020044413A1 (en) | Hypothesis inference device, hypothesis inference method, and computer-readable recording medium | |
WO2021245726A1 (en) | Information processing device, information processing method, and computer-readable recording medium | |
WO2020008631A1 (en) | Observation event determination device, observation event determination method, and computer-readable recording medium | |
JP6908134B2 (en) | Hypothesis generator, hypothesis generation method, and program |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
A621 | Written request for application examination |
Free format text: JAPANESE INTERMEDIATE CODE: A621 Effective date: 20170411 |
|
A977 | Report on retrieval |
Free format text: JAPANESE INTERMEDIATE CODE: A971007 Effective date: 20180427 |
|
A131 | Notification of reasons for refusal |
Free format text: JAPANESE INTERMEDIATE CODE: A131 Effective date: 20180524 |
|
A521 | Request for written amendment filed |
Free format text: JAPANESE INTERMEDIATE CODE: A523 Effective date: 20180526 |
|
TRDD | Decision of grant or rejection written | ||
A01 | Written decision to grant a patent or to grant a registration (utility model) |
Free format text: JAPANESE INTERMEDIATE CODE: A01 Effective date: 20180607 |
|
RD14 | Notification of resignation of power of sub attorney |
Free format text: JAPANESE INTERMEDIATE CODE: A7434 Effective date: 20180607 |
|
A61 | First payment of annual fees (during grant procedure) |
Free format text: JAPANESE INTERMEDIATE CODE: A61 Effective date: 20180626 |
|
R150 | Certificate of patent or registration of utility model |
Ref document number: 6366033 Country of ref document: JP Free format text: JAPANESE INTERMEDIATE CODE: R150 |