1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room...

14
1 CS 201 Compiler Construction Introduction

Transcript of 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room...

Page 1: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

1

CS 201Compiler Construction

Introduction

Page 2: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

2

Instructor Information

Rajiv GuptaOffice: WCH Room 408E-mail: [email protected]: (951) 827-2558Office Hours: T, Th 10:00 am -11:00 am

Page 3: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

3

Course Requirements

Grading:Test 1: 35 pointsTest 2: 35 pointsProject: 30 points

Page 4: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

4

Course Overview

CS 152

CS 201

Page 5: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

Control Flow Graph Representation- Control Flow Analysis- Data Flow Analysis- Selected Code Optimizations Static Single Assignment Representation- Control Flow Analysis- Selected Code Optimizations

5

Exam I

Page 6: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

Bidirectional Data Flow Analysis- Partial Redundancy Elimination- Partial Dead-code EliminationInterprocedural Data Flow AnalysisMachine Code Generation- Register Allocation- Instruction Scheduling

6

Exam II

Page 7: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

Project

• LLVM infrastructure• Code instrumentation• Implement path profiling

7

Page 8: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

Three Address Intermediate Code

Arithmetic Operationsdst = src1 op src2

where op in {+, -, *, /, %}Relational Operators

dst = src1 relop src2where relop in {<,<=,!

=,==,>=,>}Logical Operations

dst = src lop src2, where lop in {||,&&}dst = ! src

8

Page 9: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

Three Address Intermediate Code

Array Accessesdst = src[index]dst[index] = src

Pointersdst = & src* dst = src

Copy Assignmentdst = src

9

Page 10: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

Three Address Intermediate Code

Branchesunconditional:

goto labelconditional:

if predicate goto labelor

if src1 relop src2 goto labellabels:

declared or instruction numbers

10

Page 11: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

11

Control Flow Graph (CFG)

Intermediate Code can be transformed from linear representation to a directed graph form called Control flow Graph:

Nodes – Basic Blocks: Basic block consists of a sequence of intermediate code statements that must be entered at the top and exited at the bottom, i.e. once the block is entered, all intermediate code statements will be executed.

Edges: directed edges connect basic blocks according to control flow.

CFG representation will be used for program analysis and optimization.

Page 12: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

CFG Construction Algorithm

• Identify Leaders: the first instruction in a basic block is a leader.– First instruction in the program– Target instruction of a conditional or

unconditional branch– Instruction immediately following a

conditional or unconditional branch

• Construct Basic Blocks: – Starting from the leader append subsequent

instructions up to, but not including, the next leader or the end of the program

12

Page 13: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

CFG Construction Algorithm

• Add Edges: add a directed edge from basic block B1 to basic block B2 if:– There is a conditional or unconditional

branch from the last statement in B1 to the leader of B2; or

– B2 immediately follows B1 in program order and B1 does not end in an unconditional branch.

13

Page 14: 1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room 408 E-mail: gupta@cs.ucr.edu Tel: (951) 827-2558 Office.

Example

14

1. A = 42. T1 = A * B3. T2 = T1 / C4. If T2<W goto 75. M = T1 * K6. T3 = M + I7. H = I8. M = T3 – H9. If T3>= 0 goto 1110. goto 311. Halt