Improvements to the Compiler Lecture 27 Mon, Apr 26, 2004.

Slides:



Advertisements
Similar presentations
Recitation 4: 09/30/02 Outline The Stack! Essential skill for Lab 3 –Out-of-bound array access –Put your code on the stack Annie Luo
Advertisements

C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Peephole Optimization Final pass over generated code: examine a few consecutive instructions: 2 to 4 See if an obvious replacement is possible: store/load.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Accessing parameters from the stack and calling functions.
Lecture 25 Generating Code for Basic Blocks Topics Code Generation Readings: April 19, 2006 CSCE 531 Compiler Construction.
Intermediate Code. Local Optimizations
Compiler Construction
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Y86 Processor State Program Registers
Copyright  Hannu Laine C++-programming Part 2 Hannu Laine.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
5. Assembly Language. Basics of AL Program data Pseudo-ops Array Program structures Data, stack, code segments.
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
CSC 221 Computer Organization and Assembly Language
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005.
The x86 Instruction Set Lecture 16 Mon, Mar 14, 2005.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Paradyn Project Paradyn / Dyninst Week Madison, Wisconsin April 12-14, 2004 Paradyn Project Paradyn / Dyninst Week Madison, Wisconsin April 12-14, 2004.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Instruction Set Architecture
C function call conventions and the stack
CS-401 Computer Architecture & Assembly Language Programming
Conditional Branch Example
Computer Architecture and Assembly Language
143A: Principles of Operating Systems Lecture 4: Calling conventions
Exploiting & Defense Day 2 Recap
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Introduction to Compilers Tim Teitelbaum
Factoring if/else code
Recitation 2 – 2/4/01 Outline Machine Model
Machine-Level Programming 1 Introduction
Computer Architecture and Assembly Language
Y86 Processor State Program Registers
Discussion Section – 11/3/2012
Machine-Level Programming 4 Procedures
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Stack Frames and Advanced Procedures
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Programming 2 Control Flow
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Miscellaneous Topics.
Machine-Level Programming II: Control Flow
X86 Assembly Review.
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
X86 Assembly - Control.
Computer Architecture and System Programming Laboratory
Compiler Construction CS 606 Sohail Aslam Lecture 1.
Presentation transcript:

Improvements to the Compiler Lecture 27 Mon, Apr 26, 2004

Overflowing the Stack Each expression leaves a value on the stack. That is because expressions are typically subexpressions of larger expressions. Their values are used in the larger expression. Example: n = n + 1; However, at some point, the value is no longer needed. If we leave it on the stack, then eventually the stack will overflow.

Example read n; d = 2; while (d < n) { if (n % d == 0) { n = n / d; print d; } else d = d + 1; } This loop will fail after about 500,000 iterations.

Excessive Pushing and Popping Most expressions Begin with a pop operation, and End with a push operation. In most cases, the value being pushed by one expression is popped immediately by the next expression, making the two operations unnecessary.

Example n = n + 1; lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push %eax pop %eax push (%eax) push $1 pop %eax pop %edx add %edx,%eax push %eax pop %eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax mov (%eax),%edx mov $1,%eax add %edx,%eax pop %edx mov %eax,(%edx) push %eax

Example lea n,%eax push %eax lea n,%eax push (%eax) mov $1,%eax pop %edx add %edx,%eax pop %edx mov %eax,(%edx) push %eax lea n,%eax push %eax mov (%eax),%edx mov $1,%eax add %edx,%eax pop %edx mov %eax,(%edx) push %eax

Eliminating Dead Code Code that cannot be reached by any logical path is called dead code. Dead code can be detected by looking for unlabeled statements following unconditional jumps. Such statements are unreachable.

Eliminating Dead Code Make one pass of the assembly language program, compiling a list of all labels that are referenced. Make a second pass, removing all unreferenced labels. Make a third pass removing all code occurring between unconditional jumps and the next label.

Example L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret # Code for LABEL L2: mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

Remove Unreferenced Labels L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret # Code for LABEL L2: mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

Remove Unreferenced Labels L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret # Code for LABEL mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

Find Unconditional Branches L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret # Code for LABEL mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

Remove Unreachable Code L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret # Code for LABEL mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }

Remove Unreachable Code L1: # Code for ID lea 8(%ebp),%eax push %eax # Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret int copy(int a) { return a; }