Loop Optimizations in Modern C Compilers

Total Page:16

File Type:pdf, Size:1020Kb

Loop Optimizations in Modern C Compilers Loop Optimizations in Modern C Compilers Chae Jubb [email protected] 10 December 2014 Abstract Many programs spend a significant portion of execution time in loops. Because of this, loop opti- mizations are increasingly important. We see two major types of optimizations affecting loop performance: general and loop-specific. General optimizations help loop performance simply because the loop is repeated many times. Loop-specific optimizations better performance because they alter the structure of the loop or make improvements that require many iterations to be efficient due to a large overhead. We discuss loop optimization strategies and then, using directed test cases, analyze how gcc and clang use those techniques to optimize at different levels. We find clang to be much more aggressive in optimizations at a lower level. At the highest optimization levels, these compilers produce executables that perform similarly. Importantly, at the most common optimization level (-O2), clang equals or exceeds gcc performance. 1 Introduction -O1, -O2, -O3. The binaries are generated for Intel x86 targets: an i7 quad-core, a Xeon 16-core, and a Loop performance is a large contributor to the over- Pentium 4. all performance of many applications. Programs, es- We then analyze each output binary in two ways. pecially mathematical and scientific, spend a large First, we examine the emitted x86 assembly code. majority of their execution in loops. This makes the This allows us to directly verify techniques used by performance especially critical. Small inefficiencies the compiler in transforming the source code to the are magnified with hundreds or thousands of itera- target x86. Second, the binary is performance-tested. tions. Because it's in the compiler's best interests to Because the compiler strives to create binaries that emit high-performing code, many special techniques perform well|not binaries that look as if they per- are used specifically to increase loop performance. form well|this portion is critical. We examine the use of a subset of these technique The disparities in runtimes of each test are drastic; and compare both emitted code and runtime perfor- thus, performance characteristics are used to evaluate mance across multiple optimization levels for multiple the effectiveness of a different optimization level on C compilers. a certain program compiled with a certain compiler. The times ought not be compared across tests. 2 Method 3 Loop Optimizations To examine loop optimization techniques, we first prepare some sample programs. These programs are Before beginning discussion of results, we first in- 1 2 then compiled using clang and gcc . For each com- troduce common loop optimizations. The tech- piler, various optimization levels are examined: -O0, niques described include both machine-independent 1clang version 3.5 and machine-dependent optimizations. As the names 2gcc version 4.8.2 suggest, the former category is used to make gen- 1 1 void bad invariant() f 1 void good invariant() f 2 i n t i, a=4; 2 i n t i, a=4; 3 f o r(i=0; i < 5 ; ++i ) f 3 i n t a2=a ∗ a ; 4 i n t a2=a ∗ a ; 4 f o r(i=0; i < 5 ; ++i ) f 5 /∗ loop body;`a` not touched ∗/ 5 /∗ loop body;`a` not touched ∗/ 6 g 6 g 7 g 7 g Figure 1: Re-calculating a loop invariant each itera- Figure 2: Calculating a loop invariant outside the tion loop. eral loop optimizations more concerned with the 1 void bad induction() f 2 i n t i, j, ∗ array ; algorithm used; whereas, the latter is more con- 3 f o r(i=0; i < 3 2 ; ++i ) f cerned with the implementation and takes into ac- 4 i n tj=4 ∗ i ; count specifics of the target device. 5 /∗ loop body, uses`i` ∗/ Before beginning our examination of loop opti- 6 array[j] = rand(); 7 g mizations, we note that many, many optimizations 8 g will help loop performance. While not loop-specific, optimizations such as moving variables to registers from the stack will help performance, simply because Figure 3: Inefficient redundant induction variables of the gains of the optimization will be realized in each iteration. label variables such as these as \induction variables". More formally, any variable whose value is altered by 3.1 Machine-Independent a fixed amount each loop iteration is an induction We first consider optimizations made independent of variable. the x86 architecture. These include strategies such We can generally apply two types of optimizations as identifying loop invariants, inverting the loop, and to induction variables: reduction of strength and removing induction variables. elimination. Generally, reduction of strength involves replacing an expensive operation (like multiplication) with a less expensive one (such as addition). Some- 3.1.1 Loop Invariants times, however, a compiler may realize an induction One simple technique used to improve the perfor- variable is redundant and completely eliminate it. mance of loops is moving invariant calculations out- Figure 3 shows an example of an inefficient use side the loop. We see a sample program in Figure 1 of two redundant induction variables. We improve that unnecessarily re-calculates a loop invariant on this slightly in Figure 4 when we invoke a reduction each iterations. This will cause wasted cycles, hurting of strength. Finally, Figure 5 shows the redundant performance. Luckily, many compilers will recognize variable completely optimized away. that program as equivalent to the one in Figure 2, and, as such, produce the optimized code. (It will, of 3.1.3 Loop Unrolling course, take into account the differences in scope of that loop-invariant variable.) We next turn our attention to a simple trick some- times employed: loop unrolling. This optimization is extremely straightforward and can only be applied 3.1.2 Induction Variables to loops with a known length. Rather than having Nearly all for loops and some while loops will have a loop with n iterations, the compiler will produce variables that function as a sort of loop counter. We target code that simply repeats n times. This opti- 2 1 void ros induction() f 1 void pre inversion() f 2 i n t i, j, ∗ array ; 2 while(/ ∗ c o n d i t i o n ∗/) f 3 f o r(i=0, j= −4; i < 3 2 ; ++i ) f 3 /∗ loop body ∗/ 4 i n t j += 4; 4 g 5 /∗ loop body, uses`i` ∗/ 5 g 6 array[j] = rand(); 7 g 8 g Figure 6: Example while loop before inversion Figure 4: Reduction of strength with redundant in- 1 void post inversion() f duction variables 2 i f(/ ∗ c o n d i t i o n ∗/) f 3 do f 4 /∗ loop body ∗/ 1 void elim induction() f 5 g while(/ ∗ c o n d i t i o n ∗/); 2 i n t i, ∗ array ; 6 g 3 f o r(i=0; i < 3 2 ; ++i ) f 7 g 4 /∗ loop body, uses`i` ∗/ 5 array [ i ∗ 4 ] = rand ( ) ; 6 g Figure 7: Example while loop after inversion 7 g Figure 5: Elimination of redundant induction vari- We see no gain, but, more importantly, no loss in ables performance in this case. When the condition is true, both execution flows mization may increase performance on some proces- behave as they would for any middle, non-final iter- sors because it eliminates any jump instructions. In ation. fact, minimizing jump instructions is often the goal of many optimizations (even those not loop-specific) because that type of instruction presents the possi- Middle Iteration With a non-final iteration, we bility of a costly branch-misprediction. We do not obviously see the same behavior between the two ver- always use loops, though, because of a major draw- sion. This is due to the well-known behavior of while back: increased binary size. and do-while loops. 3.1.4 Loop Inversion Final Iteration By having the comparison after We now turn our attention to another optimization the loop rather than at the beginning, we can save designed to reduce the number of branch instructions. cycles. The unoptimized version will run the last iter- Loop inversion is a fairly simple transformation: a ation, jump to the beginning to check the condition. while loop is converted to a do-while loop wrapped Seeing the condition is no longer satisfied, we jump by an if statement as shown in Figure 6 and Figure 7. back to outside the loop. To fully analyze the effectiveness of this optimiza- Now we consider the optimized version. We run tion, we consider three cases: first iteration, any mid- the last iteration and then check the loop condition. dle iteration, final iteration. It will not be satisfied, and thus, we do not jump to the beginning and instead fall through. First Iteration We consider the two cases of the This optimization results in a savings of 2 jumps. first iteration. Either the loop is entered or it is not. While this savings may seem trivial, consider nested When the condition is not true before entering the loops. If this optimization were applied to an inner loop, each version produces a single jump instruction. loop, the savings could be quite noticeable. 3 3.2 x86 Optimizations 1 #include <s t d l i b . h> 2 #include <s t d i n t . h> We now turn our attention to the more specialized op- 3 timizations that directly target the x86 ISA's specific 4 i n t loop i n v ( u i n t 8 t l e n ) f features. These optimizations exploit things such as 5 i n t a[256]; 6 i n t i=0; memory addressing, flags, and register number and 7 width, to name a few.
Recommended publications
  • C++ Programming: Program Design Including Data Structures, Fifth Edition
    C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: • Learn about repetition (looping) control structures • Explore how to construct and use count- controlled, sentinel-controlled, flag- controlled, and EOF-controlled repetition structures • Examine break and continue statements • Discover how to form and use nested control structures C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2 Objectives (cont'd.) • Learn how to avoid bugs by avoiding patches • Learn how to debug loops C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3 Why Is Repetition Needed? • Repetition allows you to efficiently use variables • Can input, add, and average multiple numbers using a limited number of variables • For example, to add five numbers: – Declare a variable for each number, input the numbers and add the variables together – Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4 while Looping (Repetition) Structure • The general form of the while statement is: while is a reserved word • Statement can be simple or compound • Expression acts as a decision maker and is usually a logical expression • Statement is called the body of the loop • The parentheses are part of the syntax C++ Programming: From Problem Analysis to Program Design, Fifth Edition 5 while Looping (Repetition)
    [Show full text]
  • Compiler Construction Assignment 3 – Spring 2018
    Compiler Construction Assignment 3 { Spring 2018 Robert van Engelen µc for the JVM µc (micro-C) is a small C-inspired programming language. In this assignment we will implement a compiler in C++ for µc. The compiler compiles µc programs to java class files for execution with the Java virtual machine. To implement the compiler, we can reuse the same concepts in the code-generation parts that were done in programming assignment 1 and reuse parts of the lexical analyzer you implemented in programming assignment 2. We will implement a new parser based on Yacc/Bison. This new parser utilizes translation schemes defined in Yacc grammars to emit Java bytecode. In the next programming assignment (the last assignment following this assignment) we will further extend the capabilities of our µc compiler by adding static semantics such as data types, apply type checking, and implement scoping rules for functions and blocks. Download Download the Pr3.zip file from http://www.cs.fsu.edu/~engelen/courses/COP5621/Pr3.zip. After unzipping you will get the following files Makefile A makefile bytecode.c The bytecode emitter (same as Pr1) bytecode.h The bytecode definitions (same as Pr1) error.c Error reporter global.h Global definitions init.c Symbol table initialization javaclass.c Java class file operations (same as Pr1) javaclass.h Java class file definitions (same as Pr1) mycc.l *) Lex specification mycc.y *) Yacc specification and main program symbol.c *) Symbol table operations test#.uc A number of µc test programs The files marked ∗) are incomplete. For this assignment you are required to complete these files.
    [Show full text]
  • PDF Python 3
    Python for Everybody Exploring Data Using Python 3 Charles R. Severance 5.7. LOOP PATTERNS 61 In Python terms, the variable friends is a list1 of three strings and the for loop goes through the list and executes the body once for each of the three strings in the list resulting in this output: Happy New Year: Joseph Happy New Year: Glenn Happy New Year: Sally Done! Translating this for loop to English is not as direct as the while, but if you think of friends as a set, it goes like this: “Run the statements in the body of the for loop once for each friend in the set named friends.” Looking at the for loop, for and in are reserved Python keywords, and friend and friends are variables. for friend in friends: print('Happy New Year:', friend) In particular, friend is the iteration variable for the for loop. The variable friend changes for each iteration of the loop and controls when the for loop completes. The iteration variable steps successively through the three strings stored in the friends variable. 5.7 Loop patterns Often we use a for or while loop to go through a list of items or the contents of a file and we are looking for something such as the largest or smallest value of the data we scan through. These loops are generally constructed by: • Initializing one or more variables before the loop starts • Performing some computation on each item in the loop body, possibly chang- ing the variables in the body of the loop • Looking at the resulting variables when the loop completes We will use a list of numbers to demonstrate the concepts and construction of these loop patterns.
    [Show full text]
  • 7. Control Flow First?
    Copyright (C) R.A. van Engelen, FSU Department of Computer Science, 2000-2004 Ordering Program Execution: What is Done 7. Control Flow First? Overview Categories for specifying ordering in programming languages: Expressions 1. Sequencing: the execution of statements and evaluation of Evaluation order expressions is usually in the order in which they appear in a Assignments program text Structured and unstructured flow constructs 2. Selection (or alternation): a run-time condition determines the Goto's choice among two or more statements or expressions Sequencing 3. Iteration: a statement is repeated a number of times or until a Selection run-time condition is met Iteration and iterators 4. Procedural abstraction: subroutines encapsulate collections of Recursion statements and subroutine calls can be treated as single Nondeterminacy statements 5. Recursion: subroutines which call themselves directly or indirectly to solve a problem, where the problem is typically defined in terms of simpler versions of itself 6. Concurrency: two or more program fragments executed in parallel, either on separate processors or interleaved on a single processor Note: Study Chapter 6 of the textbook except Section 7. Nondeterminacy: the execution order among alternative 6.6.2. constructs is deliberately left unspecified, indicating that any alternative will lead to a correct result Expression Syntax Expression Evaluation Ordering: Precedence An expression consists of and Associativity An atomic object, e.g. number or variable The use of infix, prefix, and postfix notation leads to ambiguity An operator applied to a collection of operands (or as to what is an operand of what arguments) which are expressions Fortran example: a+b*c**d**e/f Common syntactic forms for operators: The choice among alternative evaluation orders depends on Function call notation, e.g.
    [Show full text]
  • Repetition Structures
    24785_CH06_BRONSON.qrk 11/10/04 9:05 M Page 301 Repetition Structures 6.1 Introduction Goals 6.2 Do While Loops 6.3 Interactive Do While Loops 6.4 For/Next Loops 6.5 Nested Loops 6.6 Exit-Controlled Loops 6.7 Focus on Program Design and Implementation: After-the- Fact Data Validation and Creating Keyboard Shortcuts 6.8 Knowing About: Programming Costs 6.9 Common Programming Errors and Problems 6.10 Chapter Review 24785_CH06_BRONSON.qrk 11/10/04 9:05 M Page 302 302 | Chapter 6: Repetition Structures The applications examined so far have illustrated the programming concepts involved in input, output, assignment, and selection capabilities. By this time you should have gained enough experience to be comfortable with these concepts and the mechanics of implementing them using Visual Basic. However, many problems require a repetition capability, in which the same calculation or sequence of instructions is repeated, over and over, using different sets of data. Examples of such repetition include continual checking of user data entries until an acceptable entry, such as a valid password, is made; counting and accumulating running totals; and recurring acceptance of input data and recalculation of output values that only stop upon entry of a designated value. This chapter explores the different methods that programmers use to construct repeating sections of code and how they can be implemented in Visual Basic. A repeated procedural section of code is commonly called a loop, because after the last statement in the code is executed, the program branches, or loops back to the first statement and starts another repetition.
    [Show full text]
  • Equality Saturation: a New Approach to Optimization
    Logical Methods in Computer Science Vol. 7 (1:10) 2011, pp. 1–37 Submitted Oct. 12, 2009 www.lmcs-online.org Published Mar. 28, 2011 EQUALITY SATURATION: A NEW APPROACH TO OPTIMIZATION ROSS TATE, MICHAEL STEPP, ZACHARY TATLOCK, AND SORIN LERNER Department of Computer Science and Engineering, University of California, San Diego e-mail address: {rtate,mstepp,ztatlock,lerner}@cs.ucsd.edu Abstract. Optimizations in a traditional compiler are applied sequentially, with each optimization destructively modifying the program to produce a transformed program that is then passed to the next optimization. We present a new approach for structuring the optimization phase of a compiler. In our approach, optimizations take the form of equality analyses that add equality information to a common intermediate representation. The op- timizer works by repeatedly applying these analyses to infer equivalences between program fragments, thus saturating the intermediate representation with equalities. Once saturated, the intermediate representation encodes multiple optimized versions of the input program. At this point, a profitability heuristic picks the final optimized program from the various programs represented in the saturated representation. Our proposed way of structuring optimizers has a variety of benefits over previous approaches: our approach obviates the need to worry about optimization ordering, enables the use of a global optimization heuris- tic that selects among fully optimized programs, and can be used to perform translation validation, even on compilers other than our own. We present our approach, formalize it, and describe our choice of intermediate representation. We also present experimental results showing that our approach is practical in terms of time and space overhead, is effective at discovering intricate optimization opportunities, and is effective at performing translation validation for a realistic optimizer.
    [Show full text]
  • Scalable Conditional Induction Variables (CIV) Analysis
    Scalable Conditional Induction Variables (CIV) Analysis ifact Cosmin E. Oancea Lawrence Rauchwerger rt * * Comple A t te n * A te s W i E * s e n l C l o D C O Department of Computer Science Department of Computer Science and Engineering o * * c u e G m s E u e C e n R t v e o d t * y * s E University of Copenhagen Texas A & M University a a l d u e a [email protected] [email protected] t Abstract k = k0 Ind. k = k0 DO i = 1, N DO i =1,N Var. DO i = 1, N IF(cond(b(i)))THEN Subscripts using induction variables that cannot be ex- k = k+2 ) a(k0+2*i)=.. civ = civ+1 )? pressed as a formula in terms of the enclosing-loop indices a(k)=.. Sub. ENDDO a(civ) = ... appear in the low-level implementation of common pro- ENDDO k=k0+MAX(2N,0) ENDIF ENDDO gramming abstractions such as filter, or stack operations and (a) (b) (c) pose significant challenges to automatic parallelization. Be- Figure 1. Loops with affine and CIV array accesses. cause the complexity of such induction variables is often due to their conditional evaluation across the iteration space of its closed-form equivalent k0+2*i, which enables its in- loops we name them Conditional Induction Variables (CIV). dependent evaluation by all iterations. More importantly, This paper presents a flow-sensitive technique that sum- the resulted code, shown in Figure 1(b), allows the com- marizes both such CIV-based and affine subscripts to pro- piler to verify that the set of points written by any dis- gram level, using the same representation.
    [Show full text]
  • An Introduction to Programming in Simula
    An Introduction to Programming in Simula Rob Pooley This document, including all parts below hyperlinked directly to it, is copyright Rob Pooley ([email protected]). You are free to use it for your own non-commercial purposes, but may not copy it or reproduce all or part of it without including this paragraph. If you wish to use it for gain in any manner, you should contact Rob Pooley for terms appropriate to that use. Teachers in publicly funded schools, universities and colleges are free to use it in their normal teaching. Anyone, including vendors of commercial products, may include links to it in any documentation they distribute, so long as the link is to this page, not any sub-part. This is an .pdf version of the book originally published by Blackwell Scientific Publications. The copyright of that book also belongs to Rob Pooley. REMARK: This document is reassembled from the HTML version found on the web: https://web.archive.org/web/20040919031218/http://www.macs.hw.ac.uk/~rjp/bookhtml/ Oslo 20. March 2018 Øystein Myhre Andersen Table of Contents Chapter 1 - Begin at the beginning Basics Chapter 2 - And end at the end Syntax and semantics of basic elements Chapter 3 - Type cast actors Basic arithmetic and other simple types Chapter 4 - If only Conditional statements Chapter 5 - Would you mind repeating that? Texts and while loops Chapter 6 - Correct Procedures Building blocks Chapter 7 - File FOR future reference Simple input and output using InFile, OutFile and PrintFile Chapter 8 - Item by Item Item oriented reading and writing and for loops Chapter 9 - Classes as Records Chapter 10 - Make me a list Lists 1 - Arrays and simple linked lists Reference comparison Chapter 11 - Like parent like child Sub-classes and complex Boolean expressions Chapter 12 - A Language with Character Character handling, switches and jumps Chapter 13 - Let Us See what We Can See Inspection and Remote Accessing Chapter 14 - Side by Side Coroutines Chapter 15 - File For Immediate Use Direct and Byte Files Chapter 16 - With All My Worldly Goods..
    [Show full text]
  • Induction Variable Analysis with Delayed Abstractions1
    Induction Variable Analysis with Delayed Abstractions1 SEBASTIAN POP, and GEORGES-ANDRE´ SILBER CRI, Mines Paris, France and ALBERT COHEN ALCHEMY group, INRIA Futurs, Orsay, France We present the design of an induction variable analyzer suitable for the analysis of typed, low-level, three address representations in SSA form. At the heart of our analyzer stands a new algorithm that recognizes scalar evolutions. We define a representation called trees of recurrences that is able to capture different levels of abstractions: from the finer level that is a subset of the SSA representation restricted to arithmetic operations on scalar variables, to the coarser levels such as the evolution envelopes that abstract sets of possible evolutions in loops. Unlike previous work, our algorithm tracks induction variables without prior classification of a few evolution patterns: different levels of abstraction can be obtained on demand. The low complexity of the algorithm fits the constraints of a production compiler as illustrated by the evaluation of our implementation on standard benchmark programs. Categories and Subject Descriptors: D.3.4 [Programming Languages]: Processors—compilers, interpreters, optimization, retargetable compilers; F.3.2 [Logics and Meanings of Programs]: Semantics of Programming Languages—partial evaluation, program analysis General Terms: Compilers Additional Key Words and Phrases: Scalar evolutions, static analysis, static single assignment representation, assessing compilers heuristics regressions. 1Extension of Conference Paper:
    [Show full text]
  • 6Up with Notes
    Notes CSCE150A Computer Science & Engineering 150A Problem Solving Using Computers Lecture 05 - Loops Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 [email protected] Chapter 5 CSCE150A 5.1 Repetition in Programs 5.2 Counting Loops and the While Statement 5.3 Computing a Sum or a Product in a Loop 5.4 The for Statement 5.5 Conditional Loops 5.6 Loop Design 5.7 Nested Loops 5.8 Do While Statement and Flag-Controlled Loops 5.10 How to Debug and Test 5.11 Common Programming Errors 2 / 1 Repetition in Programs CSCE150A Just as the ability to make decisions (if-else selection statements) is an important programming tool, so too is the ability to specify the repetition of a group of operations. When solving a general problem, it is sometimes helpful to write a solution to a specific case. Once this is done, ask yourself: Were there any steps that I repeated? If so, which ones? Do I know how many times I will have to repeat the steps? If not, how did I know how long to keep repeating the steps? 3 / 1 Notes Counting Loops CSCE150A A counter-controlled loop (or counting loop) is a loop whose repetition is managed by a loop control variable whose value represents a count. Also called a while loop. 1 Set counter to an initial value of 0 2 while counter < someF inalV alue do 3 Block of program code 4 Increase counter by 1 5 end Algorithm 1: Counter-Controlled Loop 4 / 1 The C While Loop CSCE150A This while loop computes and displays the gross pay for seven employees.
    [Show full text]
  • A Short Introduction to Ocaml
    Ecole´ Polytechnique INF549 A Short Introduction to OCaml Jean-Christophe Filli^atre September 11, 2018 Jean-Christophe Filli^atre A Short Introduction to OCaml INF549 1 / 102 overview lecture Jean-Christophe Filli^atre labs St´ephaneLengrand Monday 17 and Tuesday 18, 9h{12h web site for this course http://www.enseignement.polytechnique.fr/profs/ informatique/Jean-Christophe.Filliatre/INF549/ questions ) [email protected] Jean-Christophe Filli^atre A Short Introduction to OCaml INF549 2 / 102 OCaml OCaml is a general-purpose, strongly typed programming language successor of Caml Light (itself successor of Caml), part of the ML family (SML, F#, etc.) designed and implemented at Inria Rocquencourt by Xavier Leroy and others Some applications: symbolic computation and languages (IBM, Intel, Dassault Syst`emes),static analysis (Microsoft, ENS), file synchronization (Unison), peer-to-peer (MLDonkey), finance (LexiFi, Jane Street Capital), teaching Jean-Christophe Filli^atre A Short Introduction to OCaml INF549 3 / 102 first steps with OCaml Jean-Christophe Filli^atre A Short Introduction to OCaml INF549 4 / 102 the first program hello.ml print_string "hello world!\n" compiling % ocamlopt -o hello hello.ml executing % ./hello hello world! Jean-Christophe Filli^atre A Short Introduction to OCaml INF549 5 / 102 the first program hello.ml print_string "hello world!\n" compiling % ocamlopt -o hello hello.ml executing % ./hello hello world! Jean-Christophe Filli^atre A Short Introduction to OCaml INF549 5 / 102 the first program hello.ml
    [Show full text]
  • Strength Reduction of Induction Variables and Pointer Analysis – Induction Variable Elimination
    Loop optimizations • Optimize loops – Loop invariant code motion [last time] Loop Optimizations – Strength reduction of induction variables and Pointer Analysis – Induction variable elimination CS 412/413 Spring 2008 Introduction to Compilers 1 CS 412/413 Spring 2008 Introduction to Compilers 2 Strength Reduction Induction Variables • Basic idea: replace expensive operations (multiplications) with • An induction variable is a variable in a loop, cheaper ones (additions) in definitions of induction variables whose value is a function of the loop iteration s = 3*i+1; number v = f(i) while (i<10) { while (i<10) { j = 3*i+1; //<i,3,1> j = s; • In compilers, this a linear function: a[j] = a[j] –2; a[j] = a[j] –2; i = i+2; i = i+2; f(i) = c*i + d } s= s+6; } •Observation:linear combinations of linear • Benefit: cheaper to compute s = s+6 than j = 3*i functions are linear functions – s = s+6 requires an addition – Consequence: linear combinations of induction – j = 3*i requires a multiplication variables are induction variables CS 412/413 Spring 2008 Introduction to Compilers 3 CS 412/413 Spring 2008 Introduction to Compilers 4 1 Families of Induction Variables Representation • Basic induction variable: a variable whose only definition in the • Representation of induction variables in family i by triples: loop body is of the form – Denote basic induction variable i by <i, 1, 0> i = i + c – Denote induction variable k=i*a+b by triple <i, a, b> where c is a loop-invariant value • Derived induction variables: Each basic induction variable i defines
    [Show full text]