SlideShare une entreprise Scribd logo
1  sur  14
PRINCIPLES OF COMPILER DESIGN
 LOOP INVARIANT COMPUTATION




                  By- Bharat P. Patil
                  M.Sc. C.S.(Part 1) - 41
Loop Invariant Computation
and Code Motion



   Loop-invariant computation
   Algorithms for code motion
   Summary: 13.1.2 (global CSE), 13.2
Loop-Invariant Computation
and Code Motion
   Loop invariant computation
    ◦ A computation whose value does not change as
      long as control stays within the loop
   Loop invariant code motion (LICM)
    ◦ Move a loop invariant statement within a loop
      to the pre-header of the loop
Example

◦ Which statement is a loop-invariant?
◦ Which statement can we move to the preheader?



                   I        I
                   I
LICM Algorithm
Observations
 ◦ Loop invariant: operands are defined
   outside loop or are defined by loop
   invariants
 ◦ Code motion: not all invariant statements
   can be moved to the pre-header
Algorithm
 ◦ Detect loop invariant computations
 ◦ Check conditions for code motion
 ◦ Code transformation
Detecting loop invariant
computation
Compute reaching definitions
Repeat: mark A=B+C as invariant if
 ◦ All reaching definitions of B are outside of the
   loop, or there is exactly one reaching definition
   for B and it is from a loop-invariant statement
   inside the loop
 ◦ Check similarly for C
Until no changes to the set of loop-
 invariant statements occur
Example
                  I(1)




          I(1)           I(1)




                 I(2)
Conditions for Code Motion
  Correctness: movement does not change the program
   semantics
  Performance: code should not be slowed down




                         OK?
                    I




 Need information on
  Control flow of the loop: dominate all the exits
  Other definitions: no other definitions of A
  Other uses: all uses of A are dominated by block b
Code Motion Algorithm
Given a set of nodes in a loop
 Compute reaching definitions
 Compute loop invariant computation
 Compute dominators
 Find the exits of the loop, which are   nodes whose
  successors are located outside loop
Code Motion Details
 Candidate statement for code motion
  ◦   Loop invariant
  ◦   Located in a block that dominates all the exits of the loop
  ◦   Assign to a variable not assigned to elsewhere in the loop
  ◦   Located in a block that dominate all blocks in the loop
      that contain the use of the variable

 Visit blocks in a reverse-postorder
  ◦ Move the candidate statement to pre-header
  if all the invariant operations it depends on have been
     moved
Examples

           I                 I




               I


           I                 I



               I

                   outside
                   loop
More Aggressive
Optimizations
Gamble on: most loops get executed
 ◦ Can we relax constraint of dominating all
   exits?
    If liveness check and exception check is satisfied
Landing Pads
Code for most loops is generated in a
 test-repeat-until form to simplify
 dominance
Loop invariant computation

Contenu connexe

Tendances

Tendances (20)

Relationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternRelationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & Pattern
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
Artificial Intelligence Searching Techniques
Artificial Intelligence Searching TechniquesArtificial Intelligence Searching Techniques
Artificial Intelligence Searching Techniques
 
Loop optimization
Loop optimizationLoop optimization
Loop optimization
 
Block Cipher and its Design Principles
Block Cipher and its Design PrinciplesBlock Cipher and its Design Principles
Block Cipher and its Design Principles
 
And or graph
And or graphAnd or graph
And or graph
 
Network security & cryptography full notes
Network security & cryptography full notesNetwork security & cryptography full notes
Network security & cryptography full notes
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
A* Search Algorithm
A* Search AlgorithmA* Search Algorithm
A* Search Algorithm
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Three Address code
Three Address code Three Address code
Three Address code
 
Problem Formulation in Artificial Inteligence Projects
Problem Formulation in Artificial Inteligence ProjectsProblem Formulation in Artificial Inteligence Projects
Problem Formulation in Artificial Inteligence Projects
 
Deadlock dbms
Deadlock dbmsDeadlock dbms
Deadlock dbms
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
A* Algorithm
A* AlgorithmA* Algorithm
A* Algorithm
 
Loops in flow
Loops in flowLoops in flow
Loops in flow
 
Air Cargo transport
 Air Cargo transport Air Cargo transport
Air Cargo transport
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 

Similaire à Loop invariant computation

Chapter 8 review questions
Chapter 8 review questionsChapter 8 review questions
Chapter 8 review questions
loayshabaneh
 
White Box Testing And Control Flow & Loop Testing
White Box Testing And Control Flow & Loop TestingWhite Box Testing And Control Flow & Loop Testing
White Box Testing And Control Flow & Loop Testing
Ankit Mulani
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
Vasavi College of Engg
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
Deepak Singh
 

Similaire à Loop invariant computation (20)

Lecture-13.ppt
Lecture-13.pptLecture-13.ppt
Lecture-13.ppt
 
8 statement level
8 statement level8 statement level
8 statement level
 
chapter 6.pptx
chapter 6.pptxchapter 6.pptx
chapter 6.pptx
 
System verilog control flow
System verilog control flowSystem verilog control flow
System verilog control flow
 
Chapter 8 review questions
Chapter 8 review questionsChapter 8 review questions
Chapter 8 review questions
 
Bp150513(compiler)
Bp150513(compiler)Bp150513(compiler)
Bp150513(compiler)
 
Java 2.pptx
Java 2.pptxJava 2.pptx
Java 2.pptx
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
White Box Testing And Control Flow & Loop Testing
White Box Testing And Control Flow & Loop TestingWhite Box Testing And Control Flow & Loop Testing
White Box Testing And Control Flow & Loop Testing
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statements
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Control structure
Control structureControl structure
Control structure
 
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin CoroutinesThreading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Java Control Statements
Java Control StatementsJava Control Statements
Java Control Statements
 
Repetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesRepetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniques
 
Loops in C
Loops in CLoops in C
Loops in C
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow Analysis
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
 

Plus de ReachLocal Services India

Plus de ReachLocal Services India (12)

Excel ppt
Excel pptExcel ppt
Excel ppt
 
Virtual reality
Virtual realityVirtual reality
Virtual reality
 
Digital signatures
Digital signaturesDigital signatures
Digital signatures
 
System security
System securitySystem security
System security
 
Artificial intelligence
Artificial intelligenceArtificial intelligence
Artificial intelligence
 
Distributed database
Distributed databaseDistributed database
Distributed database
 
Distributed dbms
Distributed dbmsDistributed dbms
Distributed dbms
 
Sexual harresment on women
Sexual harresment on womenSexual harresment on women
Sexual harresment on women
 
Digital signal processing
Digital signal processingDigital signal processing
Digital signal processing
 
Mobile network layer (mobile comm.)
Mobile network layer (mobile comm.)Mobile network layer (mobile comm.)
Mobile network layer (mobile comm.)
 
Regular expression (compiler)
Regular expression (compiler)Regular expression (compiler)
Regular expression (compiler)
 
Temporal data mining
Temporal data miningTemporal data mining
Temporal data mining
 

Dernier

Dernier (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 

Loop invariant computation

  • 1. PRINCIPLES OF COMPILER DESIGN LOOP INVARIANT COMPUTATION By- Bharat P. Patil M.Sc. C.S.(Part 1) - 41
  • 2. Loop Invariant Computation and Code Motion  Loop-invariant computation  Algorithms for code motion  Summary: 13.1.2 (global CSE), 13.2
  • 3. Loop-Invariant Computation and Code Motion  Loop invariant computation ◦ A computation whose value does not change as long as control stays within the loop  Loop invariant code motion (LICM) ◦ Move a loop invariant statement within a loop to the pre-header of the loop
  • 4. Example ◦ Which statement is a loop-invariant? ◦ Which statement can we move to the preheader? I I I
  • 5. LICM Algorithm Observations ◦ Loop invariant: operands are defined outside loop or are defined by loop invariants ◦ Code motion: not all invariant statements can be moved to the pre-header Algorithm ◦ Detect loop invariant computations ◦ Check conditions for code motion ◦ Code transformation
  • 6. Detecting loop invariant computation Compute reaching definitions Repeat: mark A=B+C as invariant if ◦ All reaching definitions of B are outside of the loop, or there is exactly one reaching definition for B and it is from a loop-invariant statement inside the loop ◦ Check similarly for C Until no changes to the set of loop- invariant statements occur
  • 7. Example I(1) I(1) I(1) I(2)
  • 8. Conditions for Code Motion  Correctness: movement does not change the program semantics  Performance: code should not be slowed down OK? I Need information on  Control flow of the loop: dominate all the exits  Other definitions: no other definitions of A  Other uses: all uses of A are dominated by block b
  • 9. Code Motion Algorithm Given a set of nodes in a loop  Compute reaching definitions  Compute loop invariant computation  Compute dominators  Find the exits of the loop, which are nodes whose successors are located outside loop
  • 10. Code Motion Details  Candidate statement for code motion ◦ Loop invariant ◦ Located in a block that dominates all the exits of the loop ◦ Assign to a variable not assigned to elsewhere in the loop ◦ Located in a block that dominate all blocks in the loop that contain the use of the variable  Visit blocks in a reverse-postorder ◦ Move the candidate statement to pre-header if all the invariant operations it depends on have been moved
  • 11. Examples I I I I I I outside loop
  • 12. More Aggressive Optimizations Gamble on: most loops get executed ◦ Can we relax constraint of dominating all exits?  If liveness check and exception check is satisfied
  • 13. Landing Pads Code for most loops is generated in a test-repeat-until form to simplify dominance