SlideShare una empresa de 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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligence
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
System testing
System testingSystem testing
System testing
 
Superscalar & superpipeline processor
Superscalar & superpipeline processorSuperscalar & superpipeline processor
Superscalar & superpipeline processor
 
weak slot and filler
weak slot and fillerweak slot and filler
weak slot and filler
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languages
 
A* Search Algorithm
A* Search AlgorithmA* Search Algorithm
A* Search Algorithm
 
Script
ScriptScript
Script
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
 
A* Algorithm
A* AlgorithmA* Algorithm
A* Algorithm
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Recursive Descent Parsing
Recursive Descent Parsing  Recursive Descent Parsing
Recursive Descent Parsing
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
Ch5a
Ch5aCh5a
Ch5a
 
Astar algorithm
Astar algorithmAstar algorithm
Astar algorithm
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 

Similar a Loop invariant computation

System verilog control flow
System verilog control flowSystem verilog control flow
System verilog control flowPushpa Yakkala
 
Chapter 8 review questions
Chapter 8 review questionsChapter 8 review questions
Chapter 8 review questionsloayshabaneh
 
Bp150513(compiler)
Bp150513(compiler)Bp150513(compiler)
Bp150513(compiler)indhu mathi
 
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 TestingAnkit Mulani
 
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 CSowmya Jyothi
 
Operators loops conditional and statements
Operators loops conditional and statementsOperators loops conditional and statements
Operators loops conditional and statementsVladislav Hadzhiyski
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming languageVasavi College of Engg
 
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 CoroutinesLauren Yew
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Deepak Singh
 
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 techniquesJason J Pulikkottil
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow AnalysisEdgar Barbosa
 

Similar 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
 

Más 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

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
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 17Celine George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Último (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
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
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

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