JP3580394B2 - Program conversion device and program conversion method - Google Patents

Program conversion device and program conversion method Download PDF

Info

Publication number
JP3580394B2
JP3580394B2 JP13672796A JP13672796A JP3580394B2 JP 3580394 B2 JP3580394 B2 JP 3580394B2 JP 13672796 A JP13672796 A JP 13672796A JP 13672796 A JP13672796 A JP 13672796A JP 3580394 B2 JP3580394 B2 JP 3580394B2
Authority
JP
Japan
Prior art keywords
optimization
program
factor
processing
execution order
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.)
Expired - Fee Related
Application number
JP13672796A
Other languages
Japanese (ja)
Other versions
JPH09319586A (en
Inventor
賢一 柘植
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Fujitsu Ltd
Original Assignee
Fujitsu Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Fujitsu Ltd filed Critical Fujitsu Ltd
Priority to JP13672796A priority Critical patent/JP3580394B2/en
Publication of JPH09319586A publication Critical patent/JPH09319586A/en
Application granted granted Critical
Publication of JP3580394B2 publication Critical patent/JP3580394B2/en
Anticipated expiration legal-status Critical
Expired - Fee Related legal-status Critical Current

Links

Images

Landscapes

  • Devices For Executing Special Programs (AREA)

Description

【0001】
【発明の属する技術分野】
本発明は、ソースプログラム(原始プログラム)をオブジェクトプログラム(目的プログラム)に変換するプログラム変換装置(コンパイラ)及びプログラム変換方法に関し、特に、最終的に得られるオブジェクトプログラムを高速化したり、又はメモリ使用量を減少させるなどの最適化(optimaization )を行なうプログラム変換装置及びプログラム変換方法に関する。
【0002】
【従来の技術】
近年、種々のアーキテクチャを持つマイクロコンピュータが開発されており、それらのプログラム変換装置については、メモリ使用量の減少や、実行速度の向上などが要求されている。このため、プログラム変換装置では、単にソースプログラムをオブジェクトプログラムに変換するだけでなく、冗長な命令(関数及び演算式等を含む;以下、同じ)を削減したり、メモリ使用量が少ない命令に置き換える等の処理を施すいわゆる最適化が行われることがある。
【0003】
図13は従来のプログラム変換装置を示す模式図である。プログラム変換装置の入力部11に入力されたソースプログラムは、字句解析部12で字句(token )解析が行われる。すなわち、この字句解析部12では、ソースプログラムを読み込んで、意味がある名前、予約語、定数及び区切り記号などの基本単位の字句に分解し、これらを後の処理が効率よく実行できるように記号表等に展開する。また、この字句解析部12では、ソースプログラム中の注釈行の読み飛ばし処理や、開始行と継続行との区分処理などを行なう。更に、この過程でマクロの展開処理等も行なう。
【0004】
構文解析部13は、プログラムの構文解析(syntax analysis )を行なう。すなわち、構文解析部13は、ソースプログラムを構成する文がどのような階層構成になっているのかを調べたり、文法的に正しいか否かを判定する。
情報解析部14はデータの依存関係の解析部14aを含んで構成されており、構文解析によって得られた構文情報や記号表を基にして、プログラム中で使用されているデータの依存関係の解析を行なう。例えば、変数の宣言を認識した場合には、その変数の名前、型、初期値等の情報を記号表に登録するなどの処理を行なう。また、実行文を認識した場合にはどのような実行文であるかを調べ、文中で使われている各種の変数、関数、手続きなどの使われかたのチェックなどを行う。そして、その結果として、データの依存関係が記号表などの各種の表に記入されたり、入力したソースプログラムに対する内部形式(internal form )の形で内部に蓄えられる。
【0005】
このようにして情報の解析を行った後、最適化処理部16で最適化処理を行なう。一般的に、最適化処理部16では、複数種類の最適化方法を予め定められた順番で実行する。
出力部17においては、最適化された内部形式から、アセンブリ言語、絶対番地の機械語、再配置可能(relocatable )な機械語、又は他のプログラミング言語などで表現されたコードを生成して出力する。
【0006】
最適化方法には、例えば以下に示すものがある。
(1)畳み込み(folding )による最適化
定数同士の加減乗除などのように予め演算結果が求められる場合は、演算式を演算結果に置き換える。
(2)共通式の削除による最適化
繰り返し現れるが演算結果が等しい演算式を共通式という。2回目以降に現れる共通式を最初の共通式の結果で置き換える。
【0007】
(3)ループの最適化
ループ内に、ループを回っている間は演算結果が変わらない演算式がある場合は、その演算式をループ内からループの直前に移動する。
(4)レジスタ割付けによる最適化
外部メモリ等のアクセスタイムが長い場所に格納されている変数などのうち、アクセスが頻繁に行われる変数については、その変数をマイクロコンピュータの内部レジスタなどのようにアクセスタイムが短い場所におくことにより、データの転送時間や演算時間が短縮される。
【0008】
(5)式の演算順序の変更による最適化
演算結果を変えない範囲で、演算の順序を変更することで効率化を図る。例えば、ab+acとa(b+c)とは同じ演算結果が得られるが、前者の式では(a×b)の演算結果と(a×c)の演算結果とを加算し、演算回数は3回となる。後者の式では、(b+c)を演算した結果とaとを乗算し、演算回数は2回となる。この場合、演算結果は同じであるが、後者のほうが演算回数が削減され、効率が良いオブジェクトプログラムが得られる。
【0009】
(6)二重定義の削除による最適化
1つの変数が二重に定義されており、初めに定義されてから次に定義されるまでの間にその定数が使用されない場合、初めに変数を定義している式を削除する。
(7)基本ブロックをまたがって移動する命令の削除による最適化
基本ブロックとは、ソースプログラムを構成する文の集合体であり、その初めと終わりとの間には分岐がなく、連続している単位をいう。ある基本ブロックに対して複数の基本ブロックから同じ命令が渡ってくる場合には、その命令を移動先の基本ブロックのみにし、移動元の基本ブロックの命令を削除する。
【0010】
(8)命令の置き換え
同じ処理結果が得られるが演算速度が高速な命令が存在する場合には、その高速な命令に置き換える。
この他にも、種々の最適化方法がある。
【0011】
【発明が解決しようとする課題】
従来のプログラム変換装置においては、予め統計的に決定された最も効率がよくなるであろう順番で複数の最適化方法を実行している。しかし、このように最適化方法とその実行順とが一定である場合には、ある特定のソースプログラムでは最適なオブジェクトを生成できるが、他のソースプログラムでは最適化が十分でないことがある。
【0012】
アーキテクチャに依存する部分での最適化では、入力されるソースプログラムによって最適化方法やその実行順を変えることにより、最適化のかかるパターンが多くなり、よいオブジェクトが生成できる可能性がある。
本発明の目的は、ソースプログラムに応じて最適化方法とその実行順とを自動的に選択し、常に効率良い最適化を行なうことができるプログラム変換装置及びプログラム変換方法を提供することである。
【0013】
【課題を解決するための手段】
上記した課題は、ソースプログラムから、各種最適化方法毎に処理対象となる命令の数を最適化因子として集計する情報解析部と、前記最適化因子に基づいてき最適化方法とその実行順を選択する最適化選択部と、該最適化選択部により選択された実行順で選択された最適化方法を実行する最適化処理部とを有することを特徴とするプログラム変換装置により解決する。
【0014】
また、上記の課題は、ソースプログラムから、各種最適化処理毎に処理対象となる命令の数を最適化因子として集計する工程と、前記最適化因子に応じて最適化方法とその実行順を選択する工程と、選択した最適化方法を選択した実行順で実行する工程とを有することを特徴とするプログラム変換方法により解決する。
なお、本発明において以下に説明する処理ブロックとは、基本ブロックを指す場合と、一連の処理を実行する複数の基本ブロックの集合体を指す場合とがあるが、いずれも場合でも発明の原理は同じである。
【0015】
本発明においては、各種最適化方法毎に最適化処理対象となる命令の数を集計し、それを各最適化処理方法の最適化因子として取得する。例えば、レジスタ割付けによる最適化に関して最適化因子を集計するには、外部メモリ等のアクセスタイムが長い場所に格納されている変数のうち、アクセスが頻繁に行われる変数(アクセス回数が所定数以上の変数)に対してアクセスを行なう命令の数を集計する。その集計した値がレジスタ割付けによる最適化に関する最適化因子となる。
【0016】
また、二重定義の削除による最適化に関して最適化因子を集計するには、二重定義の関係にある命令の数を集計する。その集計した値が二重定義の削除による最適化に関する最適化因子となる。
更に、基本ブロックをまたがって移動する命令の削除による最適化に関する最適化因子を集計するには、基本ブロック間をわたる同じ命令の数を集計する。その集計した値が基本ブロックをまたがって移動する命令の削除による最適化因子となる。
【0017】
このように、各種の最適化方法に対して処理対象となる命令の数を最適化因子として集計することによりソースプログラムの傾向が分かり、どの最適化方法が最も有効であるかを判断することができる。基本的には、最適化因子が大きいものほど最適化の効果が大きいといえる。従って、例えば最適化因子が大きい最適化方法を優先させて最適化を実行することにより、効率よく最適化が行われる。
【0018】
なお、ソースプログラム全体について最適化因子を集計し、その集計結果に基づいて最適化方法とその実行順を選択し、選択した最適化方法をソースプログラム全体について実行してもよい。しかし、ソースプログラムを複数の処理ブロックに分割し、処理ブロック毎に最適化の集計、最適化方法とその実行順の選択及び最適化の実行を行なうことにより、より一層効率がよい最適化が可能である。
【0019】
【発明の実施の形態】
以下、本発明の実施の形態について、添付の図面を参照して説明する。
図1は本発明の実施の形態に係るプログラム変換装置を示す模式図である。プログラム変換装置の入力部11に入力されたソースプログラムは、従来と同様に、字句解析部12で字句解析が行われる。また、構文解析部13は、字句解析後のプログラムの構文を解析して、プログラムを構成する文の階層構成を調べたり、文法的なチェックを行なう。
【0020】
情報解析部14は、データの依存関係の解析部14a及び最適化因子の情報収集部14bを有している。データの依存関係の解析部14aでは、従来と同様に、構文解析によって得られた構文情報や記号表を基にして、データの依存関係の解析を行い、データの依存関係を記号表等の各種の表に記入したり、入力したソースプログラムに対する内部形式の形で蓄える。このデータの依存関係の解析結果により、最適化により他の部分に影響を与えることがない命令を抽出することが可能になる。
【0021】
最適化因子の情報収集部14bでは、データの依存関係の解析結果を使用して各種最適化方法毎に処理対象となる命令の数を最適化因子として集計する。
最適化選択部15では、最適化因子の情報収集部14bで集計した最適化因子の値を基に、適用すべき最適化方法及びその実行順を選択する。そして、最適化処理部16では、最適化選択部15で選択された最適化方法を選択された実行順で実行する。
【0022】
出力部17では、最適化された内部形式から、アセンブリ言語、絶対番地の機械語、再配置可能な機械語又は他のプログラミング言語などで表現されたコードを生成して出力する。
最適化方法の選択は、以下のように行われる。例えば、図2に示すように、最適化方法及びその実行順をパターン化した5種類の最適化パターン1〜5を用意しておく。そして、ソースプログラムを入力し、最適化因子の集計(情報の収集)を行なった後、最適化因子の値を基に最適化パターン1〜5のいずれか一つを選択し、最適化処理部15ではその最適化パターンに定められた最適化方法を定められた実行順で実行して、その結果を出力する。
【0023】
最適化処理の実行は、以下のようにしてもよい。すなわち、図3は、情報収集の結果を基に、最適化方法1〜5のうちから適宜1つを選択して呼び出し、その方法を実行し、その後更に最適化方法1〜5のいずれか1つを選択して呼び出すというようにして、複数回の最適化を実行するものである。
図4は図2の最適化処理例に基づいた最適化の選択処理の一例を示している。この例では、最適化因子をx,yの2つとし、これらの最適化因子x,yの値を直交座標上に示しており、点Pはこのプログラムの最適化因子の状態を示している。この例では、点Pが第1象限(x>0、y>0)にあることから、このプログラムがパターン1で決められた最適化方法と処理順で最適化処理を行なう。なお、点Pが第2象限(x<0、y>0)又は第3象限(x<0、y<0)に位置するときはパターン2で決められた最適化方法と処理順で最適化処理を行ない、第4象限(x>0、y<0)に位置するときはパターン3で決められた最適化方法と処理順で最適化処理を行なう。
【0024】
なお、最適化因子の情報収集部14bは、ある最適化方法の最適化因子をプラスの値とし、別の最適化方法の最適化因子をマイナスの値としてアーキテクチャ毎に決められる初期値から増減し、その集計結果に基づいて最適化方法及びその実行順を選択してもよい。
また、これらの手順はプログラムの処理単位毎に行なえばよいため、一つのソースプログラムでも処理ブロック毎に最適化方法及びその実行順を替えて行なうことができる。これにより、より一層効率が良い最適化がなされる。
【0025】
以下、更に具体的に本発明について説明する。
図5は二重定義の削除による最適化(以下、最適化Aという)を示す図である。図5(a)に示すように、ソースプログラムでは、基本ブロック内において、変数aを、「a=b」と「a=c」とに二重定義している。なお、「a=b」を定義した後、「a=c」を定義するまでの間に変数aは使用されていないとする。この場合、図5(b)に示すように、先の命令「a=b」を削除することにより処理数が削減される。
【0026】
図6は基本ブロックをまたがって移動する命令の削除による最適化(以下、最適化Bという)を示す図である。図6(a)に示すように、基本ブロックBK3に制御が渡ってくる2つの基本ブロックBK1,BK2に共通の命令(「a=b」)がある場合に、図6(b)に示すように、共通の命令をブロックBK3に移動することにより処理数が削減される。
【0027】
図7,8は最適化方法の実行順を変えたときの処理結果を示す例であり、最適化Aと最適化Bとの実行順によってどのように最適化が行われ、結果が異なるかを示している。
図7(a)は、ソースプログラムを構成する基本ブロックBK1,BK2,BK3のうち、ブロックBK1,BK2に命令「a=b」があり、ブロックBK1,BK2から処理が移るブロックBK3に命令「a=c」があるプログラムである。このプログラムに対し、最適化Aを実行した後に最適化Bを実行したとする。この場合、最初に最適化Aを実行しても、同一ブロック内に二重定義がなされているわけではないので、図7(b)に示すように、プログラムは変化しない。その後、最適化Bを実行すると、図7(c)に示すように、ブロックBK1,BK2で共通の命令「a=b」がブロックBK3に移動する。このように最適化A→最適化Bの順番で最適化を実行すると、最適化処理後のブロックBK3に変数aに対する二重定義が存在することになり、最適化が十分であるとはいえない。
【0028】
一方、同じソースプログラムに対し、最適化Bを実行した後に最適化Aを実行したとする。そうすると、図8(a)に示すように、最適化Bを実行することにより、ブロックBK1,BK2で共通する命令「a=b」がブロックBK3に移動する。次に、最適化Aを実行すると、ブロックBK3内で二重定義されている命令「a=b」及び「a=c」とのうち先の命令が削除され、図8(b)に示すように、ブロックBK3には変数aに関する命令が「a=c」のみとなる。
【0029】
すなわち、図7(a)のソースプログラムについては、最適化B→最適化Aの順番で最適化を実行することにより、効率よく最適化することができる。
しかしながら、この順番で最適化を実行すると、最適化が十分でないときがある。図9,10は他のソースプログラムについて最適化方法の実行順を変えたときの処理結果を示す例である。図9(a)は基本ブロックBK1,BK2,BK3で構成されたソースプログラムであり、ブロックBK1では命令「a=b」の後に命令「a=d」があり、ブロックBK2では命令「a=c」の後に命令「a=d」がある。そして、ブロックBK1,BK2からブロックBK3に処理が移る。
【0030】
このソースプログラムに対し、まず、最適化Aを行なうと、図9(b)に示すように、ブロックBK1,BK2では、aについて二重定義している2つの命令のうち先の命令が削除され、ブロックBK1,BK2内にはいずれもaについての命令が「a=d」のみとなる。その後、最適化Bを行なうと、図9(c)に示すように、ブロックBK1,BK2に共通の命令「a=d」がブロックBK3に移動する。
【0031】
一方、図9(a)のソースプログラムに対し、まず最適化Bを実行すると、図10(a)に示すように、ブロックBK1,BK2に共通の命令「a=d」がブロックBK3に移動する。その後、最適化Aを実行しても、図10(b)に示すように、ブロックBK1,BK2にはいずれもaについて二重定義がないため、プログラムは変化しない。
【0032】
このように、図9(a)に示すソースプログラムに対しては、最適化B→最適化Aの順で最適化を行なうよりも最適化A→最適化Bの順番で最適化を実行することにより、効率よく最適化することができる。
以下に、最適化方法の実施順を決定するための最適化因子について説明する。図11(a)は図7(a)と同一のソースプログラムであり、図11(b)はそのソースプログラムにおける最適化因子の計算方法を示す図である。通常、最適化因子は、適用すべき多数の最適化方法毎に用意する必要があるが、ここでは、説明を簡単にするために、最適化方法としては図5に示す二重定義の削除による最適化Aと、図6に示す基本ブロックをまたがって移動する命令の削除による最適化Bとの二つだけであり、最適化因子はX(+X及び−X)のみであるとする。また、最適化因子Xの初期値は0とする。
【0033】
最適化因子Xは最適化A,Bは以下の条件で計算(集計)する。
1.同一ブロック内で同一変数の定義が続く場合、最適化Aにより処理数が1つ減少するので、「X=X+1」とする。
2.基本ブロックにまたがって移動する命令がある場合、最適化Bにより処理数が命令の個数だけ減少するので、「X=X−命令の個数」とする。
【0034】
そして、Xの値に応じて、最適化方法の実行順を下記表1のように選択する。
【0035】
【表1】

Figure 0003580394
【0036】
この条件に沿って、図11(a)に示すソースプログラムの最適化因子Xを計算すると、図11(b)に示すように、最適化因子Xの計算結果は−3となる。従って、この場合は、最適化B→最適化Aの順で処理を行う。そうすると、図8に示すように、効率よく最適化が行われる。
一方、図12(a)に示すソースプログラムの最適化因子を計算すると、図12(b)に示すように、最適化因子Xの計算結果は0となり、最適化A→最適化Bの順番で処理を行なう。この場合、図9に示すように最適化されるので、効率よく最適化が行われる。
【0037】
このように、本実施の形態では、最適化方法毎に処理対象の命令の数を最適化因子として集計し、その結果に基づいて最適化方法及びその実行順を自動的に選択して最適化を行なうため、最適化の効率がよく、プログラムサイズの縮小、処理速度のより一層の向上や、メモリ使用量のより一層の減少が達成される。
なお、上述の実施の形態においては、最適化方法として二重定義の削除による最適化及び基本ブロックをまたがって移動する命令の削除による最適化の場合について説明したが、本発明が適用可能な最適化方法はこれに限定されるものではなく、その他の最適化方法についても適用できることは勿論である。この場合、最適化方法毎にその処理対象となる命令の数を集計して、その値を最適化因子とする。
【0038】
【発明の効果】
以上説明したように、本発明に係るプログラム変換装置によれば、最適化選択部において各種最適化方法毎に処理対象となる命令の数を最適化因子として集計し、適用すべき最適化方法及びその実行順を選択して、選択された最適化方法及び実行順で最適化処理部において最適化処理を実行するので、効率が良い最適化を行なうことができて、プログラムサイズの減少、処理速度の向上及びメモリ使用量の減少などのソースプログラムに応じた最適化がなされる。
【0039】
また、本発明に係るプログラム変換方法によれば、ソースプログラムから各最適化方法毎に処理対象となる命令の数を最適化因子として集計し、その最適化因子の値に応じて最適化方法及びその実行順を決定するので、常にソースプログラムに応じた効率よい最適化がなされる。これにより、プログラムサイズの減少、処理速度の向上及びメモリ使用量の減少等の効果が得られる。
【図面の簡単な説明】
【図1】本発明の実施の形態に係るプログラム変換装置を示す模式図である。
【図2】最適化方法及びその実行順の選択処理の一例を示す模式図である。
【図3】最適化方法及びその実行順の選択処理の他の例を示す模式図である。
【図4】図2に示す選択処理例のより具体的な処理を示す模式図である。
【図5】二重定義の削除による最適化を示す図である。
【図6】基本ブロックをまたがって移動する命令の削除による最適化を示す図である。
【図7】最適化A→最適化Bの順で最適化を行ったときの最適化の例を示す図である。
【図8】最適化B→最適化Aの順で最適化を行ったときの最適化の例を示す図である。
【図9】最適化A→最適化Bの順で最適化を行ったときの最適化の他の例を示す図である。
【図10】最適化B→最適化Aの順で最適化を行ったときの最適化の他の例を示す図である。
【図11】最適化因子の集計の例を示す図である。
【図12】最適化因子の集計の他の例を示す図である。
【図13】従来のプログラム変換装置を示す模式図である。
【符号の説明】
11 入力部
12 字句解析部
13 構文解析部
14 情報解析部
14a データの依存関係の解析部
14b 最適化因子の情報収集部
15 最適化選択部
16 最適化処理部
17 出力部[0001]
TECHNICAL FIELD OF THE INVENTION
The present invention relates to a program conversion device (compiler) and a program conversion method for converting a source program (a source program) into an object program (a target program), and in particular, to speed up the finally obtained object program or to reduce the amount of memory used. The present invention relates to a program conversion apparatus and a program conversion method for performing optimization such as reduction of the program.
[0002]
[Prior art]
In recent years, microcomputers having various architectures have been developed, and these program conversion devices are required to reduce the amount of memory used, improve the execution speed, and the like. For this reason, the program conversion apparatus not only converts a source program into an object program, but also reduces redundant instructions (including functions and arithmetic expressions; the same applies hereinafter) or replaces them with instructions that use less memory. So-called optimization for performing such processing may be performed.
[0003]
FIG. 13 is a schematic diagram showing a conventional program conversion device. The lexical analysis unit 12 analyzes the token of the source program input to the input unit 11 of the program conversion device. That is, the lexical analysis unit 12 reads the source program, decomposes the lexical data into basic lexical characters such as meaningful names, reserved words, constants, and delimiters, and converts them into symbols so that the subsequent processing can be executed efficiently. Expand to a table etc. In addition, the lexical analysis unit 12 performs a process of skipping a comment line in a source program, a process of separating a start line and a continuation line, and the like. Further, in this process, macro expansion processing and the like are also performed.
[0004]
The syntax analyzer 13 performs syntax analysis of the program. That is, the syntax analysis unit 13 checks the hierarchical structure of the statements constituting the source program, and determines whether the statement is grammatically correct.
The information analysis unit 14 includes a data dependency analysis unit 14a, and analyzes the data dependency used in the program based on the syntax information and the symbol table obtained by the syntax analysis. Perform For example, when the declaration of a variable is recognized, processing such as registering information such as the name, type, and initial value of the variable in a symbol table is performed. When an executable statement is recognized, the type of executable statement is checked, and various variables, functions, procedures, and the like used in the statement are checked. As a result, data dependencies are entered in various tables such as a symbol table or stored internally in an internal form (internal form) for the input source program.
[0005]
After analyzing the information in this way, the optimization processing unit 16 performs an optimization process. Generally, the optimization processing unit 16 executes a plurality of types of optimization methods in a predetermined order.
The output unit 17 generates and outputs a code expressed in an assembly language, an absolute address machine language, a relocatable machine language, or another programming language from the optimized internal format. .
[0006]
Examples of the optimization method include the following.
(1) When an operation result is obtained in advance such as addition, subtraction, multiplication, and division of optimization constants by folding, the operation expression is replaced with the operation result.
(2) Optimization by Deletion of Common Expression An operation expression that appears repeatedly but has the same operation result is called a common expression. The common expression appearing after the second time is replaced with the result of the first common expression.
[0007]
(3) Optimization of Loop If there is an operation expression whose operation result does not change during the loop, the operation expression is moved from within the loop to immediately before the loop.
(4) Optimization by register allocation Among variables stored in a place where the access time is long, such as an external memory, for a frequently accessed variable, the variable is accessed like an internal register of a microcomputer. By placing it in a place where the time is short, data transfer time and calculation time are reduced.
[0008]
(5) The efficiency is improved by changing the operation order within a range that does not change the optimization operation result by changing the operation order of the expression. For example, although the same operation result is obtained with ab + ac and a (b + c), the former expression adds the operation result of (a × b) and the operation result of (a × c), and the number of operations is three. Become. In the latter equation, the result of calculating (b + c) is multiplied by a, and the number of calculations is two. In this case, although the calculation results are the same, the latter reduces the number of calculations and provides an efficient object program.
[0009]
(6) Optimization by deleting double definition If one variable is defined twice and its constant is not used between the first definition and the next definition, the variable is defined first. Delete the expression you are using.
(7) Optimization by deleting an instruction that moves across basic blocks A basic block is a set of statements constituting a source program, and has no branch between its beginning and end and is continuous. A unit. When the same instruction is transmitted from a plurality of basic blocks to a certain basic block, the instruction is limited to the destination basic block, and the instruction of the source basic block is deleted.
[0010]
(8) Replacement of Instruction If the same processing result is obtained but an instruction with a high operation speed exists, the instruction is replaced with the high-speed instruction.
There are various other optimization methods.
[0011]
[Problems to be solved by the invention]
In a conventional program conversion device, a plurality of optimization methods are executed in a previously determined statistically determined order in which the efficiency becomes highest. However, when the optimization method and the execution order are constant as described above, an optimal object can be generated in a specific source program, but optimization may not be sufficient in another source program.
[0012]
In the optimization depending on the architecture, by changing the optimization method and the execution order depending on the input source program, the number of patterns to be optimized increases, and there is a possibility that a good object can be generated.
An object of the present invention is to provide a program conversion apparatus and a program conversion method that can automatically select an optimization method and an execution order according to a source program and always perform efficient optimization.
[0013]
[Means for Solving the Problems]
The above-described problem is solved by selecting an information analysis unit that aggregates the number of instructions to be processed for each optimization method as an optimization factor from a source program, and selects an optimization method and an execution order based on the optimization factor. The problem is solved by a program conversion device comprising: an optimization selection unit that performs optimization and an optimization processing unit that executes an optimization method selected in the execution order selected by the optimization selection unit.
[0014]
In addition, the above-mentioned problems include a step of totalizing the number of instructions to be processed for each optimization process as an optimization factor from a source program, and selecting an optimization method and an execution order according to the optimization factor. And a step of executing the selected optimization method in the selected execution order.
In the present invention, a processing block described below may refer to a basic block or may refer to an aggregate of a plurality of basic blocks that execute a series of processes. Is the same.
[0015]
In the present invention, the number of instructions to be subjected to optimization processing is totaled for each optimization method, and this is acquired as an optimization factor for each optimization method. For example, in order to collect optimization factors for optimization by register allocation, among variables stored in a place having a long access time such as an external memory, a variable that is frequently accessed (a variable whose access frequency is equal to or more than a predetermined number) is used. Variable)). The total value is an optimization factor for optimization by register allocation.
[0016]
In addition, in order to total the optimization factors for the optimization by deleting the double definition, the number of instructions having a double definition is totaled. The total value is an optimization factor for the optimization by deleting the double definition.
Furthermore, in order to total the optimization factors related to the optimization by deleting the instructions that move across the basic blocks, the number of the same instructions that cross the basic blocks is totaled. The totalized value becomes an optimization factor due to deletion of an instruction that moves across basic blocks.
[0017]
In this way, by counting the number of instructions to be processed for various optimization methods as optimization factors, it is possible to understand the tendency of the source program and determine which optimization method is most effective. it can. Basically, the larger the optimization factor, the greater the effect of the optimization. Therefore, for example, optimization is performed efficiently by giving priority to an optimization method having a large optimization factor.
[0018]
The optimization factors may be totaled for the entire source program, an optimization method and an execution order may be selected based on the totalization result, and the selected optimization method may be executed for the entire source program. However, even more efficient optimization is possible by dividing the source program into multiple processing blocks, totalizing the optimization for each processing block, selecting the optimization method and its execution order, and executing the optimization. It is.
[0019]
BEST MODE FOR CARRYING OUT THE INVENTION
Hereinafter, embodiments of the present invention will be described with reference to the accompanying drawings.
FIG. 1 is a schematic diagram showing a program conversion device according to an embodiment of the present invention. The lexical analysis of the source program input to the input unit 11 of the program conversion device is performed by the lexical analysis unit 12 as in the related art. Further, the syntax analysis unit 13 analyzes the syntax of the program after the lexical analysis to check the hierarchical structure of the sentences constituting the program and to perform a grammatical check.
[0020]
The information analysis unit 14 includes a data dependence analysis unit 14a and an optimization factor information collection unit 14b. The data dependency analysis unit 14a analyzes the data dependency based on the syntax information and the symbol table obtained by the syntax analysis in the same manner as in the related art, and compares the data dependency with various data such as a symbol table. And store it in the form of the internal format of the input source program. Based on the analysis result of the data dependency, it is possible to extract an instruction that does not affect other parts by the optimization.
[0021]
The optimization factor information collecting unit 14b totalizes the number of instructions to be processed for each optimization method as an optimization factor using the analysis result of the data dependency.
The optimization selecting unit 15 selects an optimization method to be applied and an execution order thereof based on the values of the optimization factors totalized by the optimization factor information collecting unit 14b. Then, the optimization processing unit 16 executes the optimization methods selected by the optimization selection unit 15 in the selected execution order.
[0022]
The output unit 17 generates and outputs a code expressed in an assembly language, a machine language at an absolute address, a relocatable machine language, or another programming language from the optimized internal format.
The selection of the optimization method is performed as follows. For example, as shown in FIG. 2, five types of optimization patterns 1 to 5 in which the optimization method and its execution order are patterned are prepared. Then, after inputting the source program and totalizing the optimization factors (collecting information), one of the optimization patterns 1 to 5 is selected based on the values of the optimization factors, and the optimization processing unit is selected. In step 15, the optimization method defined in the optimization pattern is executed in a predetermined execution order, and the result is output.
[0023]
The execution of the optimization processing may be performed as follows. That is, FIG. 3 shows that one of the optimization methods 1 to 5 is appropriately selected and called based on the result of the information collection, the method is executed, and then any one of the optimization methods 1 to 5 is further executed. One is selected and called, and optimization is performed a plurality of times.
FIG. 4 illustrates an example of an optimization selection process based on the optimization process example of FIG. In this example, the optimization factors are x and y, and the values of these optimization factors x and y are shown on the rectangular coordinates, and point P indicates the state of the optimization factor of this program. . In this example, since the point P is in the first quadrant (x> 0, y> 0), this program performs optimization processing in the optimization method and processing order determined by pattern 1. When the point P is located in the second quadrant (x <0, y> 0) or the third quadrant (x <0, y <0), the optimization is performed in the optimization method and processing order determined in the pattern 2. The processing is performed, and when it is located in the fourth quadrant (x> 0, y <0), the optimization processing is performed according to the optimization method and processing order determined by pattern 3.
[0024]
The optimization factor information collecting unit 14b sets the optimization factor of a certain optimization method as a positive value and sets the optimization factor of another optimization method as a negative value, and increases or decreases from an initial value determined for each architecture. The optimization method and the execution order may be selected based on the result of the aggregation.
In addition, since these procedures may be performed for each processing unit of the program, the optimization method and the execution order can be changed for each processing block even in one source program. This allows for more efficient optimization.
[0025]
Hereinafter, the present invention will be described more specifically.
FIG. 5 is a diagram showing optimization by deleting a double definition (hereinafter, referred to as optimization A). As shown in FIG. 5A, in the source program, the variable a is double-defined as “a = b” and “a = c” in the basic block. It is assumed that the variable “a” is not used after “a = b” is defined and before “a = c” is defined. In this case, as shown in FIG. 5B, the number of processes is reduced by deleting the previous instruction “a = b”.
[0026]
FIG. 6 is a diagram illustrating optimization by deleting an instruction that moves across basic blocks (hereinafter referred to as optimization B). As shown in FIG. 6A, when there is a common instruction (“a = b”) in the two basic blocks BK1 and BK2 whose control is transferred to the basic block BK3, as shown in FIG. Then, the number of processes is reduced by moving the common instruction to the block BK3.
[0027]
7 and 8 show examples of processing results when the execution order of the optimization method is changed, and show how the optimization is performed according to the execution order of the optimization A and the optimization B and the result is different. Is shown.
FIG. 7A shows that among the basic blocks BK1, BK2, and BK3 that constitute the source program, the block BK1 and BK2 have the instruction “a = b”, and the block BK1 and BK2 have the instruction “a” assigned to the block BK3 to which the processing shifts. = C "is a program. It is assumed that optimization B is executed after execution of optimization A for this program. In this case, even if the optimization A is executed first, the program is not changed, as shown in FIG. 7B, since the same block is not defined twice. Thereafter, when the optimization B is executed, the common instruction “a = b” in the blocks BK1 and BK2 moves to the block BK3 as shown in FIG. 7C. When the optimization is performed in the order of optimization A → optimization B in this manner, the block BK3 after the optimization processing has a double definition for the variable a, and it cannot be said that the optimization is sufficient. .
[0028]
On the other hand, assume that optimization A is executed after optimization B is executed for the same source program. Then, as shown in FIG. 8A, by executing the optimization B, the instruction “a = b” common to the blocks BK1 and BK2 moves to the block BK3. Next, when the optimization A is executed, the earlier instruction of the instructions “a = b” and “a = c” which are double-defined in the block BK3 is deleted, and as shown in FIG. In the block BK3, the instruction regarding the variable a is only "a = c".
[0029]
That is, the source program of FIG. 7A can be optimized efficiently by performing optimization in the order of optimization B → optimization A.
However, if the optimization is performed in this order, the optimization may not be sufficient. 9 and 10 show examples of processing results when the execution order of the optimization method is changed for another source program. FIG. 9A shows a source program composed of basic blocks BK1, BK2 and BK3. In the block BK1, an instruction "a = d" is followed by an instruction "a = d", and in the block BK2, an instruction "a = c". Is followed by the instruction "a = d". Then, the processing moves from the blocks BK1 and BK2 to the block BK3.
[0030]
When optimization A is first performed on this source program, as shown in FIG. 9B, in the blocks BK1 and BK2, the earlier instruction of the two instructions that are defined twice for a is deleted. In each of the blocks BK1 and BK2, the instruction for “a” is only “a = d”. Thereafter, when the optimization B is performed, the instruction “a = d” common to the blocks BK1 and BK2 moves to the block BK3 as shown in FIG. 9C.
[0031]
On the other hand, when the optimization B is first performed on the source program of FIG. 9A, the instruction “a = d” common to the blocks BK1 and BK2 moves to the block BK3 as shown in FIG. . Thereafter, even if the optimization A is executed, as shown in FIG. 10B, the program does not change because neither of the blocks BK1 and BK2 has a double definition of a.
[0032]
Thus, for the source program shown in FIG. 9A, optimization is performed in the order of optimization A → optimization B, rather than in the order of optimization B → optimization A. Thereby, optimization can be performed efficiently.
Hereinafter, optimization factors for determining the execution order of the optimization method will be described. FIG. 11A shows the same source program as FIG. 7A, and FIG. 11B shows a method of calculating an optimization factor in the source program. Usually, it is necessary to prepare an optimization factor for each of a number of optimization methods to be applied, but here, for simplicity of explanation, the optimization method is based on the elimination of the double definition shown in FIG. It is assumed that there are only optimization A and optimization B by deleting an instruction that moves across basic blocks shown in FIG. 6, and that the optimization factor is only X (+ X and -X). The initial value of the optimization factor X is set to 0.
[0033]
The optimization factor X is calculated (aggregated) under the following conditions for the optimizations A and B.
1. If the definition of the same variable continues in the same block, the number of processes is reduced by one due to the optimization A, so that “X = X + 1” is set.
2. If there is an instruction that moves over the basic block, the number of instructions is reduced by the number of instructions due to the optimization B, so that “X = X−the number of instructions” is set.
[0034]
Then, according to the value of X, the execution order of the optimization method is selected as shown in Table 1 below.
[0035]
[Table 1]
Figure 0003580394
[0036]
When the optimization factor X of the source program shown in FIG. 11A is calculated according to this condition, the calculation result of the optimization factor X becomes -3 as shown in FIG. 11B. Therefore, in this case, processing is performed in the order of optimization B → optimization A. Then, as shown in FIG. 8, optimization is efficiently performed.
On the other hand, when the optimization factor of the source program shown in FIG. 12A is calculated, as shown in FIG. 12B, the calculation result of the optimization factor X becomes 0, and in the order of optimization A → optimization B, Perform processing. In this case, the optimization is performed as shown in FIG. 9, so that the optimization is performed efficiently.
[0037]
As described above, in the present embodiment, the number of instructions to be processed is totaled as an optimization factor for each optimization method, and the optimization method and the execution order are automatically selected based on the result to perform optimization. Therefore, the efficiency of the optimization is high, and the program size can be reduced, the processing speed can be further improved, and the memory usage can be further reduced.
In the above-described embodiment, the optimization method by elimination of duplicate definitions and the optimization by elimination of instructions that move across basic blocks have been described. The optimization method is not limited to this, and it goes without saying that other optimization methods can be applied. In this case, the number of instructions to be processed is totaled for each optimization method, and the value is used as an optimization factor.
[0038]
【The invention's effect】
As described above, according to the program conversion device of the present invention, the optimization selecting unit counts the number of instructions to be processed for each optimization method as an optimization factor, and optimizes the optimization method to be applied. Since the execution order is selected and the optimization processing unit executes the optimization processing in the selected optimization method and the execution order, efficient optimization can be performed, and the program size is reduced and the processing speed is reduced. Optimization according to the source program, such as improvement of memory and reduction of memory usage, is performed.
[0039]
Further, according to the program conversion method of the present invention, the number of instructions to be processed for each optimization method is counted as an optimization factor from a source program, and the optimization method and the optimization method are performed in accordance with the value of the optimization factor. Since the execution order is determined, efficient optimization according to the source program is always performed. As a result, effects such as a reduction in program size, an improvement in processing speed, and a reduction in memory usage can be obtained.
[Brief description of the drawings]
FIG. 1 is a schematic diagram showing a program conversion device according to an embodiment of the present invention.
FIG. 2 is a schematic diagram illustrating an example of an optimization method and an execution order selection process thereof.
FIG. 3 is a schematic diagram showing another example of an optimization method and a process of selecting an execution order thereof.
FIG. 4 is a schematic diagram showing more specific processing of the selection processing example shown in FIG. 2;
FIG. 5 is a diagram showing optimization by deleting a double definition.
FIG. 6 is a diagram illustrating optimization by deleting an instruction that moves across basic blocks.
FIG. 7 is a diagram illustrating an example of optimization when optimization is performed in the order of optimization A → optimization B;
FIG. 8 is a diagram illustrating an example of optimization when optimization is performed in the order of optimization B → optimization A.
FIG. 9 is a diagram illustrating another example of optimization when optimization is performed in the order of optimization A → optimization B.
FIG. 10 is a diagram illustrating another example of optimization when optimization is performed in the order of optimization B → optimization A.
FIG. 11 is a diagram illustrating an example of aggregation of optimization factors.
FIG. 12 is a diagram illustrating another example of the aggregation of optimization factors.
FIG. 13 is a schematic diagram showing a conventional program conversion device.
[Explanation of symbols]
Reference Signs List 11 Input unit 12 Lexical analysis unit 13 Syntax analysis unit 14 Information analysis unit 14a Data dependency analysis unit 14b Optimization factor information collection unit 15 Optimization selection unit 16 Optimization processing unit 17 Output unit

Claims (4)

ソースプログラムから、複数種類用意された最適化方法毎に、最適化処理の対象となる命令の数を最適化因子として集計する情報解析部と、
前記最適化因子に基づいて採用すべき最適化方法とその実行順を選択する最適化選択部と、
該最適化選択部により選択された実行順で選択された最適化方法を実行する最適化処理部と
を有することを特徴とするプログラム変換装置。
An information analysis unit that aggregates the number of instructions to be optimized as an optimization factor for each of a plurality of types of optimization methods prepared from a source program;
An optimization selection unit that selects an optimization method to be adopted based on the optimization factor and an execution order thereof,
An optimization processing unit that executes an optimization method selected in an execution order selected by the optimization selection unit.
前記情報解析部は入力と出力との間に分岐がない処理ブロック毎に前記最適化因子の集計を行い、前記最適化選択部は前記情報解析部の最適化因子の集計結果に応じて前記処理ブロック毎に前記最適化方法とその実行順を選択し、前記最適化処理部は前記最適化選択部の選択結果に応じて前記処理ブロック毎に前記最適化方法を実行することを特徴とする請求項1に記載のプログラム変換装置。The information analysis unit performs the totalization of the optimization factors for each processing block having no branch between the input and the output, and the optimization selection unit performs the processing according to the totalization result of the optimization factors of the information analysis unit. The optimization method and the execution order are selected for each block, and the optimization processing unit executes the optimization method for each processing block according to a selection result of the optimization selection unit. Item 2. The program conversion device according to Item 1. ソースプログラムから、複数種類用意された最適化方法毎に、最適化処理の対象となる命令の数を最適化因子として集計する工程と、
前記最適化因子に応じて採用すべき最適化方法とその実行順を選択する工程と、
選択した最適化方法を選択した実行順で実行する工程と
を有することを特徴とするプログラム変換方法。
From the source program, for each of a plurality of types of optimization methods, a step of counting the number of instructions to be optimized as an optimization factor;
Selecting an optimization method to be adopted according to the optimization factor and an execution order thereof,
Executing the selected optimization method in the selected execution order.
前記最適化因子の集計、最適化方法の選択及び実行は、入力と出力との間に分岐がない処理ブロック毎に行なうことを特徴とする請求項3に記載のプログラム変換方法。4. The program conversion method according to claim 3, wherein the aggregation of the optimization factors and the selection and execution of the optimization method are performed for each processing block having no branch between the input and the output.
JP13672796A 1996-05-30 1996-05-30 Program conversion device and program conversion method Expired - Fee Related JP3580394B2 (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
JP13672796A JP3580394B2 (en) 1996-05-30 1996-05-30 Program conversion device and program conversion method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
JP13672796A JP3580394B2 (en) 1996-05-30 1996-05-30 Program conversion device and program conversion method

Publications (2)

Publication Number Publication Date
JPH09319586A JPH09319586A (en) 1997-12-12
JP3580394B2 true JP3580394B2 (en) 2004-10-20

Family

ID=15182097

Family Applications (1)

Application Number Title Priority Date Filing Date
JP13672796A Expired - Fee Related JP3580394B2 (en) 1996-05-30 1996-05-30 Program conversion device and program conversion method

Country Status (1)

Country Link
JP (1) JP3580394B2 (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP4757811B2 (en) 2007-02-19 2011-08-24 日本電気株式会社 Apparatus and method for generating job network flow from job control statements described in job control language

Also Published As

Publication number Publication date
JPH09319586A (en) 1997-12-12

Similar Documents

Publication Publication Date Title
JPH07234792A (en) Compile processor
US7784039B2 (en) Compiler, compilation method, and compilation program
US6026241A (en) System, method, and computer program product for partial redundancy elimination based on static single assignment form during compilation
JP3650649B2 (en) Optimization device
US7356813B2 (en) System and method for optimizing a program
US20040128660A1 (en) Efficient dead code elimination
US20100162220A1 (en) Code Motion Based on Live Ranges in an Optimizing Compiler
JPS6325733A (en) Compiler processing system
JPH0738158B2 (en) Code optimization method and compiler system
Hu et al. An accumulative parallel skeleton for all
CN103559069B (en) A kind of optimization method across between file processes based on algebra system
JP3580394B2 (en) Program conversion device and program conversion method
JPH01118931A (en) Program conversion system
JP3550748B2 (en) Compiler unit
Davidson A retargetable instruction reorganizer
JPH0756745A (en) Compiler processing system for language processing program
Ramamoorthy et al. Compilation techniques for recognition of parallel processable tasks in arithmetic expressions
Debois Imperative program optimization by partial evaluation
JPH07105013A (en) Register allocation system
Vasconcellos et al. Practical Type Inference for Polymorphic Recursion: an Implementation in Haskell.
van Engelen et al. Automatic validation of code-improving transformations on low-level program representations
JP3542538B2 (en) Program optimization processing device and program optimization method
JP2004139369A (en) Analysis method for pointer pointing constant address domain
JPH0689187A (en) Inline expansion optimizing method
Richardson et al. Interprocedural analysis useless for code optimization

Legal Events

Date Code Title Description
A977 Report on retrieval

Free format text: JAPANESE INTERMEDIATE CODE: A971007

Effective date: 20040531

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: 20040713

A61 First payment of annual fees (during grant procedure)

Free format text: JAPANESE INTERMEDIATE CODE: A61

Effective date: 20040714

R150 Certificate of patent or registration of utility model

Free format text: JAPANESE INTERMEDIATE CODE: R150

LAPS Cancellation because of no payment of annual fees