Cnsideratin fr Optimizatin: Optimizatin is a prgram transfrmatin technique, which tries t imprve the cde by making it cnsume fewer resurces (i.e. CPU, Memry) and deliver high speed. In ptimizatin, high-level general prgramming cnstructs are replaced by very efficient lw-level prgramming cdes. A cde ptimizing prcess must fllw the three rules given belw: The utput cde must nt, in any way, change the meaning f the prgram. Optimizatin shuld increase the speed f the prgram and if pssible, the prgram shuld demand less number f resurces. Optimizatin shuld itself be fast and shuld nt delay the verall cmpiling prcess. Optimizatin can be categrized bradly int tw types: machine independent and machine dependent. Machine-Independent Optimizatin Intermediate cde generatin prcess intrduces many inefficiencies: Extra cpies f variables, using variables instead f cnstants, repeated evaluatin f expressins, etc. Cde ptimizatin remves such inefficiencies and imprves cde. Imprvement may be time, space, r pwer cnsumptin. It changes the structure f prgrams, smetimes f beynd recgnitin. Inlines functins, unrlls lps, eliminates sme prgrammer-defined variables, etc. Cde ptimizatin cnsists f a bunch f heuristics and percentage f imprvement depends n prgrams (may be zer als) Machine-dependent Optimizatin www.sakshieducatin.cm Machine-dependent ptimizatin is dne after the target cde has been generated and when the cde is transfrmed accrding t the target machine architecture. It invlves CPU registers and may have abslute memry references rather than relative references. Machinedependent ptimizers put effrts t take maximum advantage f memry hierarchy. www.sakshieducatin.cm Basic Blcks www.sakshieducatin.cm
Surce cdes generally have a number f instructins, which are always executed in sequence and are cnsidered as the basic blcks f the cde. These basic blcks d nt have any jump statements amng them, i.e., when the first instructin is executed, all the instructins in the same basic blck will be executed in their sequence f appearance withut lsing the flw cntrl f the prgram. A prgram can have varius cnstructs as basic blcks, like IF-THEN-ELSE, SWITCH- CASE cnditinal statements and lps such as DO-WHILE, FOR, and REPEAT-UNTIL, etc. Basic Blck Identificatin: We may use the fllwing algrithm t find the basic blcks in a prgram: Search header statements f all the basic blcks frm where a basic blck starts: First statement f a prgram. Statements that are target f any branch (cnditinal/uncnditinal). Statements that fllw any branch statement. Header statements and the statements fllwing them frm a basic blck. A basic blck des nt include any header statement f any ther basic blck. Basic blcks are imprtant cncepts frm bth cde generatin and ptimizatin pint f view. www.sakshieducatin.cm www.sakshieducatin.cm www.sakshieducatin.cm
www.sakshieducatin.cm Basic blcks play an imprtant rle in identifying variables, which are being used mre than nce in a single basic blck. If any variable is being used mre than nce, the register memry allcated t that variable need nt be emptied unless the blck finishes executin. Cntrl Flw Graph Basic blcks in a prgram can be represented by means f cntrl flw graphs. A cntrl flw graph depicts hw the prgram cntrl is being passed amng the blcks. It is a useful tl that helps in ptimizatin by help lcating any unwanted lps in the prgram. Lcal Optimizatin: Optimizatins perfrmed exclusively within a basic blck are called "lcal ptimizatins". These are typically the easiest t perfrm since we d nt cnsider any cntrl flw infrmatin; we just wrk with the statements within the blck. Many f the lcal www.sakshieducatin.cm ptimizatins we will discuss have crrespnding glbal ptimizatins that perate n the same principle, but require additinal analysis t perfrm. Lp Optimizatin Mst prgrams run as a lp in the system. It becmes necessary t ptimize the lps in rder t save CPU cycles and memry. Lps can be ptimized by the fllwing techniques: www.sakshieducatin.cm
Invariant cde: A fragment f cde that resides in the lp and cmputes the same value at each iteratin is called a lp-invariant cde. This cde can be mved ut f the lp by saving it t be cmputed nly nce, rather than with each iteratin. Inductin analysis: A variable is called an inductin variable if its value is altered within the lp by a lp-invariant value. Strength reductin: There are expressins that cnsume mre CPU cycles, time, and memry. These expressins shuld be replaced with cheaper expressins withut cmprmising the utput f expressin. Fr example, multiplicatin (x * 2) is expensive in terms f CPU cycles than (x << 1) and yields the same result. Dead-cde Eliminatin Dead cde is ne r mre than ne cde statements, which are: Either never executed r unreachable, Or if executed, their utput is never used. Thus, dead cde plays n rle in any prgram peratin and therefre it can simply be eliminated. Partially dead cde There are sme cde statements whse cmputed values are used nly under certain circumstances, i.e., smetimes the values are used and smetimes they are nt. Such cdes are knwn as partially dead-cde. www.sakshieducatin.cm www.sakshieducatin.cm The abve cntrl flw graph depicts a chunk f prgram where variable a is used t assign the utput f expressin x * y. Let us assume that the value assigned t a is never used inside the lp. Immediately after the cntrl leaves the lp, a is assigned the value f variable z, which wuld be used later in the prgram. We cnclude here that the assignment cde f a is never used anywhere, therefre it is eligible t be eliminated. www.sakshieducatin.cm
www.sakshieducatin.cm Likewise, the picture abve depicts that the cnditinal statement is always false, implying that the cde, written in true case, will never be executed, hence it can be remved. Partial Redundancy Redundant expressins are cmputed mre than nce in parallel path, withut any change in perands whereas partial-redundant expressins are cmputed mre than nce in a path, withut any change in perands. Fr example, www.sakshieducatin.cm [redundant expressin] [partially redundant expressin] Lp-invariant cde is partially redundant and can be eliminated by using a cde-mtin technique. Anther example f a partially redundant cde can be: www.sakshieducatin.cm
If (cnditin) a = y OP z; else c = y OP z; We assume that the values f perands (y and z) are nt changed frm assignment f variable a t variable c. Here, if the cnditin statement is true, then y OP z is cmputed twice, therwise nce. Cde mtin can be used t eliminate this redundancy, as shwn belw: If (cnditin) else tmp = y OP z; a = tmp; tmp = y OP z; c = tmp; www.sakshieducatin.cm Here, whether the cnditin is true r false; y OP z shuld be cmputed nly nce. Directed Acyclic Graph Directed Acyclic Graph (DAG) is a tl that depicts the structure f basic blcks, helps t see the flw f values flwing amng the basic blcks, and ffers ptimizatin t. DAG prvides easy transfrmatin n basic blcks. DAG can be understd here: Leaf ndes represent identifiers, names r cnstants. www.sakshieducatin.cm www.sakshieducatin.cm
Interir ndes represent peratrs. Interir ndes als represent the results f expressins r the identifiers/name where the values are t be stred r assigned. Example: t 0 = a + b t 1 = t 0 + c d = t 0 + t 1 [t 0 = a + b] www.sakshieducatin.cm [t 1 = t 0 + c] [d = t 0 + t 1 ] www.sakshieducatin.cm www.sakshieducatin.cm