Code Shape III Booleans, Relationals, & Control flow Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 4: Control Structures I (Selection)
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,
Selection (decision) control structure Learning objective
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.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
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
ISBN Chapter 8 Statement-Level Control Structures.
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Fundamentals of Python: From First Programs Through Data Structures
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
CIS Computer Programming Logic
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
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 V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Operator Strength Reduction C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
ISBN Chapter 8 Statement-Level Control Structures.
sequence of execution of high-level statements
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,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Control Structures sequence of execution of high-level statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
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.
Boolean & Relational Values Control-flow Constructs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
Copyright ©2005  Department of Computer & Information Science If-Then-Else Structures.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Definition-Use Chains
Introduction to Optimization
Loops in Java.
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
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.
Instruction Scheduling: Beyond Basic Blocks
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
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
Code Shape IV Procedure Calls & Dispatch
Selection Statements.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Introduction to Optimization
Instruction Scheduling: Beyond Basic Blocks
Lexical Analysis - An Introduction
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
Procedure Linkages Standard procedure linkage Procedure has
Presentation transcript:

Code Shape III Booleans, Relationals, & Control flow Copyright 2003, Keith D. Cooper, Ken Kennedy & 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.

Boolean & Relational Values How should the compiler represent them? Answer depends on the target machine Two classic approaches Numerical representation Positional (implicit) representation Correct choice depends on both context and ISA

Boolean & Relational 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

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

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

Boolean & Relational Values The last example actually encodes result in the PC If result is used to control an operation, this may be enough Condition code version does not directly produce (x < y) Boolean version does Still, there is no significant difference in the code produced if (x < y) then a  c + d else a  e + f Example

Boolean & Relational Values Conditional move & predication both simplify this code Both versions avoid the branches Both are shorter than CC s or Boolean-valued compare Are they better? if (x < y) then a  c + d else a  e + f Example

Boolean & Relational Values Consider the assignment x  a  b  c  d Here, the boolean compare produces much better code

Boolean & Relational Values Conditional move & predication help here, too Conditional move is worse than Boolean compares Predication is identical to Boolean compares Context & hardware determine the appropriate choice x  a  b  c  d

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 case base complicates the predicates and makes branches attractive

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

Loop Implementation Code loadI1  r 1 loadI1  r 2 loadI100  r 3 cmp_ GE r 1, r 3  r 4 cbrr 4  L 2, L 1 L 1 : body addr 1, r 2  r 1 cmp_ LT r 1, r 3  r 5 cbrr 5  L 1, L 2 L 2 : next statement for (i = 1; i< 100; i++) { body } next statement Pre-testPost-test Initialization

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

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

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) Surprisingly many compilers do this for all cases!