SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
rawan_z
 

Mais procurados (20)

Distance Vector & Link state Routing Algorithm
Distance Vector & Link state Routing AlgorithmDistance Vector & Link state Routing Algorithm
Distance Vector & Link state Routing Algorithm
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
 
Mobile Network Layer
Mobile Network LayerMobile Network Layer
Mobile Network Layer
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Informed and Uninformed search Strategies
Informed and Uninformed search StrategiesInformed and Uninformed search Strategies
Informed and Uninformed search Strategies
 
Artificial Intelligence Notes Unit 4
Artificial Intelligence Notes Unit 4Artificial Intelligence Notes Unit 4
Artificial Intelligence Notes Unit 4
 
Semaphores
SemaphoresSemaphores
Semaphores
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
 
Dynamic storage allocation techniques in Compiler design
Dynamic storage allocation techniques in Compiler designDynamic storage allocation techniques in Compiler design
Dynamic storage allocation techniques in Compiler design
 
Relationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternRelationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & Pattern
 
Multiple Access Protocal
Multiple Access ProtocalMultiple Access Protocal
Multiple Access Protocal
 
Distance vector routing
Distance vector routingDistance vector routing
Distance vector routing
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing MachineMulti Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
 
Planning in AI(Partial order planning)
Planning in AI(Partial order planning)Planning in AI(Partial order planning)
Planning in AI(Partial order planning)
 

Semelhante a 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
 

Semelhante a 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
 

Mais de ReachLocal Services India

Mais 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
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Último (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
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
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.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