Boolean & Relational Values Control-flow Constructs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Operator Strength Reduction From Cooper, Simpson, & Vick, “Operator Strength Reduction”, ACM TOPLAS, 23(5), See also § of EaC2e. 1COMP 512,
SSA-Based Constant Propagation, SCP, SCCP, & the Issue of Combining Optimizations 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon,
The Last Lecture Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission.
Lazy Code Motion Comp 512 Spring 2011
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Loop Invariant Code Motion — classical approaches — 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Code Shape III Booleans, Relationals, & Control flow Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Chapter 8 . Sequence Control
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Instruction Scheduling II: Beyond Basic Blocks Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp.
1 Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence.
Fundamentals of Python: From First Programs Through Data Structures
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Code Optimization, Part III Global Methods Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Introduction to Optimization, II Value Numbering & Larger Scopes Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
ISBN Chapter 8 Statement-Level Control Structures.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Building SSA Form, III 1COMP 512, Rice University This lecture presents the problems inherent in out- of-SSA translation and some ways to solve them. Copyright.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
Decision Making and Branching (cont.)
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Definition-Use Chains
Introduction to Optimization
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
Control Structures.
Topics The if Statement The if-else Statement Comparing Strings
Lexical Analysis - An Introduction
Introduction to Optimization
Control Flow Analysis CS 4501 Baishakhi Ray.
Intermediate Representations
Introduction to Code Generation
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
The Procedure Abstraction Part I: Basics
Code Shape III Booleans, Relationals, & Control flow
Intermediate Representations
The Last Lecture COMP 512 Rice University Houston, Texas Fall 2003
Selection Statements.
Statement-Level Control Structures
Introduction to Optimization
Chapter8: Statement-Level Control Structures April 9, 2019
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
Procedure Linkages Standard procedure linkage Procedure has
Presentation transcript:

Boolean & Relational Values Control-flow Constructs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. COMP 412 FALL 2010

Comp 412, Fall Boolean & Relational Values How should the compiler represent them? Answer depends on the target machine Two classic approaches Numerical (explicit) representation Positional (implicit) representation Best choice depends on both context and ISA This material is drawn from §7.4 in EaC, where it is presented in more depth with more examples. You should read that section, along with the rest of Chapter 7. Lecture will not cover all the material in Chapter 7, but you are responsible for it. Implementation of booleans, relational expressions & control flow constructs varies widely with the ISA

Boolean & Relational Expressions First, we need to recognize boolean & relational expressions Comp 412, Fall Expr → Expr ∨ AndTerm |AndTerm → AndTerm ∧ RelExpr |RelExpr → RelExpr < NumExpr |RelExpr ≤ NumExpr |RelExpr = NumExpr |RelExpr ≠ NumExpr |RelExpr ≥ NumExpr |RelExpr > NumExpr NumExpr → NumExpr + Term |NumExpr - Term |Term → Term × Value |Term ÷ Value |Value → ¬ Factor |Factor |( Expr ) | number |Reference Reference derives a name, a subscripted name, a structure reference, a string reference, …

Comp 412, Fall Boolean & Relational Values Next, we need to represent the values Numerical representation Assign values to TRUE and FALSE Use hardware AND, OR, and NOT operations Use comparison to get a boolean from a relational expression Examples

Comp 412, Fall Boolean & Relational Values What if the ISA uses a condition code? Must use a conditional branch to interpret result of compare Necessitates branches in the evaluation Example This “positional representation” is much more complex

Comp 412, Fall Boolean & Relational Values What if the ISA uses a condition code? Must use a conditional branch to interpret result of compare Necessitates branches in the evaluation Example This “positional representation” is much more complex Condition codes are an architect’s hack allow ISA to avoid some comparisons complicates code for simple cases Condition codes are an architect’s hack allow ISA to avoid some comparisons complicates code for simple cases Editorial comment: (KDC) CC’s are an evil, seductive idea Editorial comment: (KDC) CC’s are an evil, seductive idea

Comp 412, Fall Boolean & Relational Values The last example actually encoded result in the PC If result is used to control an operation, that may suffice Condition code version does not directly produce (x < y) Boolean version does Still, there is no significant difference in the code produced

Comp 412, Fall Boolean & Relational Values Other Architectural Variations Conditional move & predication both simplify this code Both versions avoid the branches Both are shorter than cond’n codes or Boolean-valued compare Are they better?

Comp 412, Fall Boolean & Relational Values Consider the assignment x  a < b  c < d Here, Boolean compare produces much better code

Comp 412, Fall Boolean & Relational Values Conditional move & predication help here, too Conditional move is worse than Boolean compare Predication is identical to Boolean compares The bottom line:  Context & hardware determine the appropriate choice x  a < b  c < d Chapter 7 goes into greater depth; read it.

Comp 412, Fall Control Flow If-then-else Follow model for evaluating relationals & booleans with branches Branching versus predication ( e.g., IA-64 ) Frequency of execution —Uneven distribution  do what it takes to speed common case Amount of code in each case —Unequal amounts means predication may waste issue slots Control flow inside the construct —Any branching activity within the construct complicates the predicates and makes branches attractive

Comp 412, Fall Short-circuit Evaluation Optimize boolean expression evaluation Once value is determined, skip rest of the evaluation if (x or y and z) then … —If x is true, need not evaluate y or z  Branch directly to the “then” clause —On a PDP-11 or a VAX, short circuiting saved time Modern architectures may favor evaluating full expression —Rising branch latencies make the short-circuit path expensive —Conditional move and predication may make full path cheaper Past: compilers analyzed code to insert short circuits Future: compilers analyze code to prove legality of full path evaluation where language specifies short circuits

Comp 412, Fall Control Flow Loops Evaluate condition before loop (if needed) Evaluate condition after loop Branch back to the top (if needed) Merges test with last block of loop body while, for, do, & until all fit this basic model Pre-test Loop body Post-test Next block

Comp 412, Fall Implementing Loops for (i = 1; i< 100; i++) { loop body } next statement Pre-test Post-test Initialization

Comp 412, Fall Break statements Many modern programming languages include a break Exits from the innermost control-flow statement —Out of the innermost loop —Out of a case statement Translates into a jump Targets statement outside control- flow construct Creates multiple-exit construct Skip in loop goes to next iteration Only make sense if loop has > 1 block Pre-test Loop head Post-test Next block B 1B 2 Break in B 1 Skip in B 2

Comp 412, Fall Control Flow Case Statements 1 Evaluate the controlling expression 2 Branch to the selected case 3 Execute the code for that case 4 Branch to the statement after the case Parts 1, 3, & 4 are well understood, part 2 is the key

Comp 412, Fall Control Flow Case Statements 1 Evaluate the controlling expression 2 Branch to the selected case 3 Execute the code for that case 4 Branch to the statement after the case ( use break ) Parts 1, 3, & 4 are well understood, part 2 is the key Strategies Linear search (nested if-then-else constructs) Build a table of case expressions & binary search it Directly compute an address (requires dense case set) Case statements are a place where attention to code shape pays off handsomely.