Very Simple MIPS Implementation

Similar documents
Very Simple MIPS Implementation

COSC 6385 Computer Architecture - Pipelining

Computer Architecture EE 4720 Final Examination

Lecture 7 Pipelining. Peng Liu.

09-1 Multicycle Pipeline Operations

Pipelining. Maurizio Palesi

This Set. Scheduling and Dynamic Execution Definitions From various parts of Chapter 4. Description of Three Dynamic Scheduling Methods

This Set. Scheduling and Dynamic Execution Definitions From various parts of Chapter 4. Description of Two Dynamic Scheduling Methods

Instruction Level Parallelism. Appendix C and Chapter 3, HP5e

Computer Architecture

1 Hazards COMP2611 Fall 2015 Pipelined Processor

Lecture 9. Pipeline Hazards. Christos Kozyrakis Stanford University

Data Hazards Compiler Scheduling Pipeline scheduling or instruction scheduling: Compiler generates code to eliminate hazard

LSU EE 4720 Homework 5 Solution Due: 20 March 2017

3/12/2014. Single Cycle (Review) CSE 2021: Computer Organization. Single Cycle with Jump. Multi-Cycle Implementation. Why Multi-Cycle?

Chapter 4 The Processor 1. Chapter 4A. The Processor

Pipelining Analogy. Pipelined laundry: overlapping execution. Parallelism improves performance. Four loads: Non-stop: Speedup = 8/3.5 = 2.3.

The Processor Pipeline. Chapter 4, Patterson and Hennessy, 4ed. Section 5.3, 5.4: J P Hayes.

These slides do not give detailed coverage of the material. See class notes and solved problems (last page) for more information.

MIPS An ISA for Pipelining

Chapter 4 The Processor 1. Chapter 4B. The Processor

Computer Architecture EE 4720 Midterm Examination

Pipeline Hazards. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Pipeline Overview. Dr. Jiang Li. Adapted from the slides provided by the authors. Jiang Li, Ph.D. Department of Computer Science

Lecture 3: The Processor (Chapter 4 of textbook) Chapter 4.1

Pipeline design. Mehran Rezaei

Pipelining. CSC Friday, November 6, 2015

Notes Interrupts and Exceptions. The book uses exception as a general term for all interrupts...

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 4. The Processor

ECEC 355: Pipelining

Computer Architecture EE 4720 Midterm Examination

ECE154A Introduction to Computer Architecture. Homework 4 solution

LECTURE 3: THE PROCESSOR

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 4. The Processor

Processor (II) - pipelining. Hwansoo Han

Chapter 4. Instruction Execution. Introduction. CPU Overview. Multiplexers. Chapter 4 The Processor 1. The Processor.

EI338: Computer Systems and Engineering (Computer Architecture & Operating Systems)

Outline. A pipelined datapath Pipelined control Data hazards and forwarding Data hazards and stalls Branch (control) hazards Exception

CISC 662 Graduate Computer Architecture Lecture 6 - Hazards

Computer Architecture EE 4720 Midterm Examination

Department of Computer and IT Engineering University of Kurdistan. Computer Architecture Pipelining. By: Dr. Alireza Abdollahpouri

Lecture 3. Pipelining. Dr. Soner Onder CS 4431 Michigan Technological University 9/23/2009 1

Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted from Hennessy & Patterson / 2003 Elsevier

Pipelining. Each step does a small fraction of the job All steps ideally operate concurrently

ECE/CS 552: Pipeline Hazards

Chapter 4. The Processor

MIPS Pipelining. Computer Organization Architectures for Embedded Computing. Wednesday 8 October 14

COSC4201 Pipelining. Prof. Mokhtar Aboelaze York University

Pipelining! Advanced Topics on Heterogeneous System Architectures. Politecnico di Milano! Seminar DEIB! 30 November, 2017!

DLX Unpipelined Implementation

Instruction Pipelining

Instruction Pipelining

Appendix C. Abdullah Muzahid CS 5513

CS61C : Machine Structures

ECE 552 / CPS 550 Advanced Computer Architecture I. Lecture 6 Pipelining Part 1

ECE473 Computer Architecture and Organization. Pipeline: Control Hazard

(Basic) Processor Pipeline

Advanced Parallel Architecture Lessons 5 and 6. Annalisa Massini /2017

Basic Pipelining Concepts

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. 5 th. Edition. Chapter 4. The Processor

Computer Architecture EE 4720 Midterm Examination

Thomas Polzer Institut für Technische Informatik

Computer Architecture Spring 2016

LSU EE 4720 Homework 1 Solution Due: 12 February 2016

Chapter 4. The Processor

Lecture 05: Pipelining: Basic/ Intermediate Concepts and Implementation

Computer and Information Sciences College / Computer Science Department Enhancing Performance with Pipelining

Modern Computer Architecture

CS 61C: Great Ideas in Computer Architecture Pipelining and Hazards

Appendix C: Pipelining: Basic and Intermediate Concepts

COMPUTER ORGANIZATION AND DESIGN

Computer Architecture EE 4720 Midterm Examination

EITF20: Computer Architecture Part2.2.1: Pipeline-1

Full Datapath. Chapter 4 The Processor 2

COMPUTER ORGANIZATION AND DESIGN

Pipelined CPUs. Study Chapter 4 of Text. Where are the registers?

CSE 533: Advanced Computer Architectures. Pipelining. Instructor: Gürhan Küçük. Yeditepe University

ECS 154B Computer Architecture II Spring 2009

CSCI 402: Computer Architectures. Fengguang Song Department of Computer & Information Science IUPUI. Today s Content

Computer Architecture EE 4720 Midterm Examination

CSEE 3827: Fundamentals of Computer Systems

Page 1. Pipelining: Its Natural! Chapter 3. Pipelining. Pipelined Laundry Start work ASAP. Sequential Laundry A B C D. 6 PM Midnight

ELEC 5200/6200 Computer Architecture and Design Spring 2017 Lecture 4: Datapath and Control

Full Datapath. Chapter 4 The Processor 2

CS 152, Spring 2011 Section 2

Computer Architecture ELEC3441

The Processor (3) Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Review: Abstract Implementation View

Minimizing Data hazard Stalls by Forwarding Data Hazard Classification Data Hazards Present in Current MIPS Pipeline

Instr. execution impl. view

EITF20: Computer Architecture Part2.2.1: Pipeline-1

CS 110 Computer Architecture. Pipelining. Guest Lecture: Shu Yin. School of Information Science and Technology SIST

Lecture 2: Processor and Pipelining 1

ECE473 Computer Architecture and Organization. Pipeline: Data Hazards

Computer Organization EE 3755 Final Examination

Computer Architecture EE 4720 Final Examination

Basic Instruction Timings. Pipelining 1. How long would it take to execute the following sequence of instructions?

CAD for VLSI 2 Pro ject - Superscalar Processor Implementation

Pipelining concepts The DLX architecture A simple DLX pipeline Pipeline Hazards and Solution to overcome

ECE260: Fundamentals of Computer Engineering

Transcription:

06 1 MIPS Pipelined Implementation 06 1 line: (In this set.) Unpipelined Implementation. (Diagram only.) Pipelined MIPS Implementations: Hardware, notation, hazards. Dependency Definitions. Hazards: Definitions, stalling, bypassing. Control Hazards: Squashing, one-cycle implementation. line: (Covered in class but not yet in set.) Operation of nonpipelined implementation, elegance and power of pipelined implementation. (See text.) Computation of CPI for program executing a loop. 06 1 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 1

06 2 06 2 Very Simple MIPS Implementation From EE 3755. enpc en epc en PC omem xma op addr data in data eir md en opcode 31:26 func 5:0 xrwr rs 25:21 rt 20:16 rd 15:11 5d31 5d0 imm 15:0 xrws to control logic Reg File addr addr addr data in data data prepare imm rsv uimm simm limm bimm xalu1 32d4 oalu op xalu2 alu Features Avoid duplication of hardware: One ory, One Adder (). Relatively complex control logic needed to re-use, etc. 06 2 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 2

06 3 Unpipelined Implementation 06 3 In this implementation hardware is duplicated. Instruction fetch Instruction decode/ register fetch Execute/ address calculation ory access Write back M u x 4 Add Zero? Branch taken Cond PC Instruction memory Registers A B M u x M u x output memory LMD M u x 16 Sign 32 extend lmm FIGURE 3.1 The implementation of the DLX datapath allows every instruction to be executed in four or five clock cycles. 06 3 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 3

06 4 06 4 Pipelined MIPS Implementation IF ID EX ME WB +1 PC 25:21 20:16 D In rsv D In D MD 2'b0 30 2 15:0 format immed IMM = E Decode dest. reg Note: diagram omits connections for some instructions. 06 4 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 4

06 5 Pipeline Details 06 5 Pipelining Idea Split hardware into n equally sized (in time) stages...... separate the stages using special registers called pipeline latches...... increase the clock frequency by n...... avoid problems due to overlapping of execution. Pipeline Stages Pipeline divided into stages. Each stage occupied by at most one instruction. At any time, each stage can be occupied by its own instruction. Stages given names: IF, ID, EX, ME, WB Sometimes ME written as MEM. 06 5 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 5

06 6 06 6 Pipeline Latches: Registers separating pipeline stages. Written at end of each cycle. To emphasize role shown in diagram as bar separating stages. Registers named using pair of stage names and register name. For example, IF/ID., ID/EX., ID/EX.A (used in text, notes). if id ir, id ex ir, id ex rs val (used in Verilog code). 06 6 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 6

06 7 Pipeline Execution Diagram 06 7 Pipeline Execution Diagram: Diagram showing the pipeline stages that instructions occupy as they execute. Time on horizontal axis, instructions on vertical axis. Diagram shows where instruction is at a particular time. Cycle 0 1 2 3 4 5 6 add r1, r2, r3 IF ID EX ME WB and r4, r5, r6 IF ID EX ME WB lw r7, 8(r9) IF ID EX ME WB A vertical slice (e.g., at cycle 3) shows processor activity at that time. In such a slice a stage should appear at most once...... if it appears more than once execution not correct...... since a stage can only execute one instruction at a time. 06 7 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 7

06 8 Instruction Decoding and Pipeline Control 06 8 Pipeline Control Setting control inputs to devices including...... multiplexor inputs...... function for...... operation for memory...... whether to clock each register...... et cetera. 06 8 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 8

06 9 06 9 Options for controlling pipeline: Decode in ID Determine settings in ID, pass settings along in pipeline latches. Decode in Each Stage Pass opcode portions of instruction along. Decoding performed as needed. Real systems decode in ID. Example given later in this set. 06 9 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 9

06 10 Dependencies and Hazards 06 10 Remember Operands read from registers in ID...... and results written to registers in WB. Consider the following incorrect execution:! Cycle 0 1 2 3 4 5 6 7 add r1, r2, r3 IF ID EX ME WB sub r4, r1, r5 IF ID EX ME WB and r6, r1, r8 IF ID EX ME WB xor r9, r4, r11 IF ID EX ME WB Execution incorrect because...... sub reads r1 before add writes (or even finishes computing) r1,...... and reads r1 before add writes r1, and...... xor reads r4 before sub writes r4. 06 10 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 10

06 11 06 11 Dependencies and Hazards Incorrect execution due to...... dependencies in program...... and hazards in hardware (pipeline). Incorrect execution above is the fault of the hardware...... because the ISA does not forbid dependencies. Dependency: A relationship between two instructions...... indicating that their execution should be (or appear to be) in program order. Hazard: A potential execution problem in an implementation due to overlapping instruction execution. There are several kinds of dependencies and hazards. For each kind of dependence there is a corresponding kind of hazard. 06 11 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 11

06 12 Dependencies 06 12 Dependency: A relationship between two instructions...... indicating that their execution should be, or appear to be, in program order. If B is dependent on A then B should appear to execute after A. Dependency Types: True,, or Flow Dependence (Three different terms used for the same concept.) Name Dependence Control Dependence 06 12 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 12

06 13 Dependence 06 13 Dependence: (a.k.a., True and Flow Dependence) A dependence between two instructions...... indicating data needed by the second is produced by the first. Example: add r1, r2, r3 sub r4, r1, r5 and r6, r4, r7 The sub is dependent on add (via r1). The and is dependent on sub (via r4). The and is dependent add (via sub). Execution may be incorrect if...... a program having a data dependence...... is run on a processor having an uncorrected RAW hazard. 06 13 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 13

06 14 Name Dependencies 06 14 There are two kinds: antidependence and output dependence. Antidependence: A dependence between two instructions...... indicating a value written by the second...... that the first instruction reads. Antidependence Example add r1, r2, r3 sub r2, r4, r5 sub is antidependent on the add. Execution may be incorrect if...... a program having an antidependence...... is run on a processor having an uncorrected WAR hazard. 06 14 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 14

06 15 06 15 put Dependence: A dependence between two instructions...... indicating that both instructions write the same location...... (register or memory address). put Dependence Example add r1, r2, r3 sub r1, r4, r5 The sub is output dependent on add. Execution may be incorrect if...... a program having an output dependence...... is run on a processor having an uncorrected WAW hazard. 06 15 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 15

06 16 06 16 Control Dependence: A dependence between a branch instruction and a second instruction...... indicating that whether the second instruction executes...... depends on the outcome of the branch. beq $1, $0 SKIP nop add $2, $3, $4 SKIP: sub $5, $6, $7 # Delayed branch The add is control dependent on the beq. The sub is not control dependent on the beq. 06 16 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 16

06 17 Pipeline Hazards 06 17 Hazard: A potential execution problem in an implementation due to overlapping instruction execution. Interlock: Hardware that avoids hazards by stalling certain instructions when necessary. Hazard Types: Structural Hazard: Needed resource currently busy. Hazard: Needed value not yet available or overwritten. Control Hazard: Needed instruction not yet available or wrong instruction executing. 06 17 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 17

06 18 Hazards 06 18 Identified by acronym indicating correct operation. RAW: Read after write, akin to data dependency. WAR: Write after read, akin to anti dependency. WAW: Write after write, akin to output dependency. MIPS implementations above only subject to RAW hazards. RAR not a hazard since read order irrelevant (without an intervening write). 06 18 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 18

06 19 Interlocks 06 19 When threatened by a hazard: Stall (Pause a part of the pipeline.) Stalling avoids overlap that would cause error. This does slow things down. Add hardware to avoid the hazards. Details of hardware depend on hazard and pipeline. Several will be covered. 06 19 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 19

06 20 06 20 Structural Hazards Cause: two instructions simultaneously need one resource. Solutions: Stall. Duplicate resource. Pipelines in this section do not have structural hazards. Covered in more detail with floating-point instructions. 06 20 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 20

06 21 Hazards 06 21 Pipelined MIPS Subject to RAW Hazards. Consider the following incorrect execution of code containing data dependencies.! Cycle 0 1 2 3 4 5 6 7 add r1, r2, r3 IF ID EX ME WB sub r4, r1, r5 IF ID EX ME WB and r6, r1, r8 IF ID EX ME WB xor r9, r4, r11 IF ID EX ME WB Execution incorrect because...... sub reads r1 before add writes (or even finishes computing) r1,...... and reads r1 before add writes r1, and...... xor reads r4 before sub writes r4. Problem fixed by stalling the pipeline. 06 21 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 21

06 22 06 22 Stall: To pause execution in a pipeline from IF up to a certain stage. With stalls, code can execute correctly: For code on previous slide, stall until data in register.! Cycle 0 1 2 3 4 5 6 7 8 9 10 add r1, r2, r3 IF ID EX ME WB sub r4, r1, r5 IF ID -----> EX ME WB and r6, r1, r8 IF -----> ID EX ME WB xor r9, r4, r11 IF ID -> EX ME WB Arrow shows that instructions stalled. Stall creates a bubble, stages without valid instructions, in the pipeline. With bubbles present, CPI is greater than its ideal value of 1. 06 22 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 22

06 23 06 23 Stall Implementation Stall implemented by asserting a hold signal...... which inserts a nop (or equivalent) after the stalling instruction...... and disables clocking of pipeline latches before the stalling instruction.! Cycle 0 1 2 3 4 5 6 7 8 9 10 add r1, r2, r3 IF ID EX ME WB sub r4, r1, r5 IF ID -----> EX ME WB and r6, r1, r8 IF -----> ID EX ME WB xor r9, r4, r11 IF ID -> EX ME WB During cycle 3, a nop is in EX. During cycle 4, a nop is in EX and ME. The two adjacent nops are called a bubble...... they move through the pipeline with the other instructions. A third nop is in EX in cycle 7. 06 23 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 23

06 24 Bypassing 06 24 Some stalls are avoidable. Consider again:! Cycle 0 1 2 3 4 5 6 7 8 9 10 add r1, r2, r3 IF ID EX ME WB sub r4, r1, r5 IF ID EX ME WB and r6, r1, r8 IF ID EX ME WB xor r9, r4, r11 IF ID EX ME WB Note that the new value of r1 needed by sub...... has been computed at the end of cycle 2...... and isn t really needed until the beginning of the next cycle, 3. Execution was incorrect because the value had to go around the pipeline to ID. Why not provide a shortcut? Why not call a shortcut a bypass or forwarding path? 06 24 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 24

06 25 Non-Bypassed MIPS 06 25 IF ID EX ME WB +1 PC 25:21 20:16 D In rsv D In D MD 2'b0 30 2 15:0 format immed IMM = E Decode dest. reg 06 25 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 25

06 26 06 26 Bypassed MIPS IF ID EX ME WB +1 PC 25:21 20:16 D In rsv D In D MD 2'b0 30 2 15:0 format immed IMM = E Decode dest. reg 06 26 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 26

06 27 06 27 MIPS Implementation With Some Forwarding Paths IF ID EX ME WB +1 PC 25:21 20:16 D In rsv D In D MD 2'b0 30 2 15:0 format immed IMM = E Decode dest. reg! Cycle 0 1 2 3 4 5 6 7 8 9 10 add r1, r2, r3 IF ID EX ME WB sub r4, r1, r5 IF ID EX ME WB and r6, r1, r8 IF ID EX ME WB xor r9, r4, r11 IF ID EX ME WB It works! 06 27 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 27

06 28 06 28 MIPS Implementation With Some Forwarding Paths Some stalls unavoidable. IF ID EX ME WB +1 PC 25:21 20:16 D In rsv D D In MD 2'b0 30 2 15:0 format immed IMM = E Decode dest. reg! Cycle 0 1 2 3 4 5 6 7 8 9 10 lw r1, 0(r2) IF ID EX ME WB add r1, r1, r4 IF ID -> EX ME WB sw 4(r2), r1 IF -> ID -----> EX ME WB addi r2, r2, 8 IF -----> ID EX ME WB Stall due to lw could not be avoided (data not available in cycle 3). Stall in cycles 5 and 6 could be avoided with a new forwarding path. 06 28 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 28

06 29 Bypass Control Logic for Lower Mux 06 29 Start with logic for, show path of Mux logic. 25:0 + IF ID EX ME WB 15:0 = +1 PC 25:21 20:16 D In rsv D In D MD 0 1 2'b0 30 2 15:0 format immed IMM Design me! Decode dest. reg Me too! 06 29 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 29

06 30 06 30 Logic to determine for register file. 25:0 IF + ID = EX ME WB 15:0 +1 PC 25:21 20:16 D In rsv D In D MD 30 2 msb 2'b0 lsb 15:0 format immed Design me next! IMM ype R is Store is Branch is J is JAL Dest is rd. No dest (use r0). Dest is r31. Dest is rt. rd 15:11 00 5'd0 01 5'd31 10 rt 20:16 11 lsb msb 06 30 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 30

29:26 06 31 06 31 25:0 29:0 Bypass Control Logic for Lower Mux IF + ID = EX ME WB 15:0 +1 PC 25:21 20:16 D In rsv ByME imm ByWB D In D MD 0 1 30 2 msb 2'b0 lsb 15:0 format immed imm mx1 Decode Dest rt 20:16 is Type R ID. signals in purple. ByME 00 01 lsb imm 10 msb ByWB 11 06 31 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 31

06 32 06 32 Bypass Control Logic Control logic not minimized (for clarity). Control Logic Generating. Present in previous implementations, just not shown. Determines which register gets written based on instruction. Instruction categories used in boxes such as = is Store (some instructions omitted): = is Type R : All Type R instructions. = is Store : All store instructions. = is Branch : branches such as beq and bltz. = is JAL, = is J : Matches the exact instruction. 06 32 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 32

06 33 Bypass Control Logic, Continued 06 33 Logic Generating ID/EX.MUX. = box determines if two register numbers are equal. Register number zero is not equal register zero, nor any other register. (The bypassed zero value might not be zero.) 06 33 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 33

06 34 Control Hazards 06 34 Cause: on taken CTI several wrong instructions fetched. Consider: IF ID EX MEM WB +4 PC 25:21 20:16 format immed D In rsv IMM = =0 <0 E Z N In MD Decode dest. reg 06 34 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 34

IF ID EX 06 35 MEM WB 06 35 Example of incorrect execution +4 PC 25:21 20:16 format immed Decode dest. reg!i Adr Cycle 0 1 2 3 4 5 6 7 8 0x100 bgtz r4, TARGET IF ID EX ME WB 0x104 sub r4, r2, r5 IF ID EX ME WB 0x108 sw 0(r2), r1 IF ID EX ME WB 0x10c and r6, r1, r8 IF ID EX ME WB 0x110 or r12, r13, r14... TARGET:! TARGET = 0x200 0x200 xor r9, r4, r11 IF ID EX ME WB D In rsv IMM = =0 <0 E Z N MD In Branch is taken yet two instructions past delay slot (sub) complete execution. Branch target finally fetched in cycle 4. Problem: Two instructions following delay slot. 06 35 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 35

06 36 06 36 Handling Instructions Following a Taken Branch Delay Slot Option 1: Don t fetch them. Possible (with pipelining) because...... fetch starts (sw in cycle 2)...... after branch decoded. (Would be impossible...... for non-delayed branch.) IF ID EX MEM WB +4 PC 25:21 20:16 format immed Decode dest. reg D In rsv IMM = =0 <0 E Z N MD In!I Adr Cycle 0 1 2 3 4 5 6 7 8 0x100 bgtz r4, TARGET IF ID EX ME WB 0x104 sub r4, r2, r5 IF ID EX ME WB 0x108 sw 0(r2), r1 IF ID EX ME WB 0x10c and r6, r1, r8 IF ID EX ME WB 0x110 or r12, r13, r14... TARGET:! TARGET = 0x200 0x200 xor r9, r4, r11 IF ID EX ME WB 06 36 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 36

06 37 06 37 Handling Instructions Following a Taken Branch Option 2: Fetch them, but squash (stop) them in a later stage. This will work if instructions squashed...... before modifying architecturally visible storage (registers and memory). ory modified in ME stage and registers modified in WB stage...... so instructions must be stopped before beginning of ME stage. Can we do it? Depends depends where branch instruction is. In example, need to squash sw before cycle 5. During cycle 3 bgtz in ME...... it has been decoded and the branch condition is available...... so we know whether the branch is taken...... so sw can easily be squashed before cycle 5. Option 2 will be used. 06 37 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 37

06 38 Instruction Squashing 06 38 In-Flight Instruction:: An instruction in the execution pipeline. Later in the semester a more specific definition will be used. Squashing:: [an instruction] preventing an in-flight instruction...... from writing registers, memory or any other visible storage. Squashing also called: nulling, abandoning, and cancelling.. Like an insect, a squashed instruction is still there (in most cases) but can do no harm. 06 38 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 38

06 39 Squashing Instruction in Example MIPS Implementation 06 39 Two ways to squash. Prevent it from writing architecturally visible storage. Replace destination register control bits with zero. (Writing zero doesn t change anything.) Set memory control bits (not shown so far) for no operation. Change Operation to nop. Would require changing many control bits. Squashing shown that way here for brevity. Illustrated by placing a nop in. Why not replace squashed instructions with target instructions? Because there is no straightforward and inexpensive way...... to get the instructions where and when they are needed. (Curvysideways and expensive techniques covered in Chapter 4.) 06 39 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 39

06 40 06 40 MIPS implementation used so far. IF ID EX MEM WB +4 PC 25:21 20:16 format immed D In rsv IMM = =0 <0 E Z N In MD Decode dest. reg 06 40 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 40

06 41 06 41 Example of correct execution!i Adr Cycle 0 1 2 3 4 5 6 7 8 0x100 bgtz r4, TARGET IF ID EX ME WB 0x104 sub r4, r2, r5 IF ID EX ME WB 0x108 sw 0(r2), r1 IF IDx 0x10c and r6, r1, r8 IFx 0x110 or r12, r13, r14... TARGET:! TARGET = 0x200 0x200 xor r9, r4, r11 IF ID EX ME WB Branch outcome known at end of cycle 2...... wait for cycle 3 when doomed instructions (sw and and) in flight...... and squash them so in cycle 4 they act like nops. Two cycles (1, 2, and 3), are lost. Two cycles called a branch penalty. Two cycles is alot of cycles, is there something we can do? 06 41 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 41

06 42 Yes: Zero-Cycle Branch Delay Implementation 06 42 msb 29:26 lsb 25:0 + 29:0 IF ID EX ME WB 15:0 = +1 PC 25:21 20:16 D In rsv D In D MD 0 1 2'b0 30 2 msb lsb 15:0 format immed IMM Decode Compute branch target address in ID stage. 06 42 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 42

06 43 Zero-Cycle Branch Delay Implementation 06 43 Compute branch target and condition in ID stage. Workable because register values not needed to compute branch address and...... branch condition can be computed quickly. Now how fast will code run?!i Adr Cycle 0 1 2 3 4 5 6 7 8 0x100 bgtz r4, TARGET IF ID EX ME WB 0x104 sub r4, r2, r5 IF ID EX ME WB 0x108 sw 0(r2), r1 0x10c and r6, r1, r8 0x110 or r12, r13, r14... TARGET:! TARGET = 0x200 0x200 xor r9, r4, r11 IF ID EX ME WB No penalty, not a cycle wasted!! 06 43 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 43

06 44 Control Logic for some Control Transfers 06 44 is J is B 10 msb rt 20:16 is B msb lsb 29:26 25:0 29:0 + 15:0 is BNE 01 lsb opc 31:26 31:31 =0 = IF ID EX ME WB +1 PC 01 10 00 inc 25:21 20:16 D In rsv D In D MD 0 1 2'b0 30 2 msb lsb 15:0 format immed IMM Decode 06 44 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 44

06 45 06 45 Non-Bypassed MIPS IF ID EX ME WB +1 PC 25:21 20:16 D In rsv D In D MD 2'b0 30 2 15:0 format immed IMM = E Decode 06 45 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 45

06 46 06 46 Bypassed MIPS IF ID EX MEM WB +4 PC 25:21 20:16 format immed D In rsv IMM = =0 <0 E Z N In MD Decode dest. reg 06 46 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 46

06 47 06 47 ID Branch MIPS msb 29:26 lsb 25:0 + 29:0 IF ID EX ME WB 15:0 = +1 PC 25:21 20:16 D In rsv D In D MD 0 1 2'b0 30 2 msb lsb 15:0 format immed IMM Decode 06 47 EE 4720 Lecture Transparency. Formatted 16:49, 16 February 2017 from lsli06. 06 47