What is an ISA Hardwaresoftware interface m Instruction

  • Slides: 26
Download presentation
What is an ISA? Hardware-software interface m Instruction Set Architecture (ISA) defines: m u

What is an ISA? Hardware-software interface m Instruction Set Architecture (ISA) defines: m u STATE OF THE PROGRAM (processor registers, memory) u WHAT INSTRUCTIONS DO: Semantics of instructions, how they update state u HOW INSTRUCTIONS ARE REPRESENTED: Syntax (bit encodings) m …selected so that implications of the above on hardware design/compiler design are optimal u Example: register specifier moves around between different instructions-- need multiple lines and a mux before the register file. ISA-1

Why is the ISA important? m Fixed h/w-s/w interface for a generation of processors

Why is the ISA important? m Fixed h/w-s/w interface for a generation of processors u IBM realized early the value of a fixed ISA u But: “stuck” with bad decisions for long time u Recent developments mitigate ISA problems (e. g. , x 86 microops, Transmeta, virtual machines) m ISA decisions affect: (Revisit RISC vs. CISC…) 1. Memory cost of the machine Ø Short vs. long bit encodings Ø high vs. low semantic meaning per instruction 2. Hardware design Ø Simple, uniform-complexity ops => efficient pipeline Ø Don’t build hardware for instructions that never get used 3. Compiler and programming language issues Ø How much can compiler exploit ISA to optimize perf. Ø How well does ISA support high-level lang. constructs Ø Choice for hand coding vs. compiler generated code: semantics are easy to use vs. easy to generate code for ISA-2

ISA Design Decisions & Outline: Style of operand specification: stack, accumulator, registers, etc. m

ISA Design Decisions & Outline: Style of operand specification: stack, accumulator, registers, etc. m Operand access limitations m Addressing modes for operands m Semantics: m u Mix of operations supported u Control transfers Encoding tradeoffs m Compiler influence m Example: MIPS m ISA-3

Styles of ISAs ISA-4

Styles of ISAs ISA-4

Styles of ISAs All implement: C=A+B ISA-5

Styles of ISAs All implement: C=A+B ISA-5

Why stacks, accumulators m Stacks: u Very compact format à All calculation operations take

Why stacks, accumulators m Stacks: u Very compact format à All calculation operations take zero operands à Example use: Java bytecode (low network b/w) u Theoretically shortest code for implementing arithmetic expressions à All HP calculator fanatics know this m Accumulator: u Also a very compact format u Less dependence on memory than stack-based m For both: u Compact implies memory efficient u Good if memory is expensive ISA-6

Why registers? 1. Faster than memory u Latency: raw access time (once address is

Why registers? 1. Faster than memory u Latency: raw access time (once address is known) à Cache access: 2 -3 cycles (typical) à Register access: 1 cycle à Register file typically smaller than data cache à Register file doesn’t need tag check logic u Bandwidth: more practical to multiport a register file à ILP requires large number of operand ports u ILP requirements à High-performance scheduling (ILP) requires detecting data dependent/independent operations early in pipeline à Register “addresses” are known at instruction decode time à Memory addresses are known quite late due to address computation ISA-7

Why Registers? (cont. ) 2. Less memory traffic if values are in registers u

Why Registers? (cont. ) 2. Less memory traffic if values are in registers u Program runs faster if variables are inside registers (compiler does “register allocation”) u Bus can be used for other things (e. g. , I/O) 3. More flexible for compiler/hardware scheduling u (A*B) - (C*D) - (E*F) u A*B in R 1, -C*D in R 2, -E*F in R 3: can easily rearrange ADD instructions u A to F on the stack: less flexible à Need to add swaps/rotates or completely rewrite code ISA-8

How many registers? m Depends on: u Compiler ability u Program characteristics m Lots-o-registers

How many registers? m Depends on: u Compiler ability u Program characteristics m Lots-o-registers enable two important optimizations: u Register allocation (more variables can be in registers) u Limiting reuse of registers improves parallelism m Reuse example: Load R 2, A; Load R 3, B; Load R 4, C; Load R 5, D Add R 1, R 2, R 3 Add R 2, R 5, R 4 (reuse of R 2) vs. Conflict artificially serializes the two instructions Add R 1, R 2, R 3 Add R 6, R 4, R 5 (no reuse: had R 6) u Without reuse Adds are “parallelizable” if there are two adders m Instruction level parallelism (ILP) u ILP ~ Average (CPI)-1 ~ Number of registers ISA-9

Operand access limitations m Load/store (0, 3) u u m Register/memory (1, 2) u

Operand access limitations m Load/store (0, 3) u u m Register/memory (1, 2) u u m (+) Fixed-length instructions possible: easy fetch/decode (+) Simpler h/w: efficient pipeline & potentially lower CT (-) Higher instruction count (IC) (-) Fixed-length instructions are wasteful Good code density (+) No need for extra loads (+) “A few lengths” better uses bits (-) Destroys source operand (e. g. , Add R 1, R 2) (-) May impact CPI Memory/memory u (+) Most compact (code density) u (-) High memory traffic (memory bottleneck) ISA-10

Alignment m Byte alignment u Any access is accommodated m Word alignment memory (bytes)

Alignment m Byte alignment u Any access is accommodated m Word alignment memory (bytes) 0 1 2 3 4 5 6 7 Unaligned access u Only accesses that are aligned at natural word boundaries are accommodated due to DRAM/SRAM organization u Reduces number of reads/writes to memory u Eliminates hardware for alignment (typically expensive) u Often handle misalignment via software: à Compiler detects & generates appropriate instructions à …or O/S detects and runs “fixit” routine Word size = 4 bytes Asking for words beginning at 0 or 4 is OK Asking for other words requires two reads (e. g. , ask for word starting at 2) 0 4 1 5 2 6 3 7 4 5 2 3 read #1 read #2 reorder 2 3 4 5 ISA-11

Endian-ness m Where is the most-significant byte (MSB) in a word? u Little-endian (e.

Endian-ness m Where is the most-significant byte (MSB) in a word? u Little-endian (e. g. , x 86) Byte address LSB 0 1 2 MSB 3 4 5 6 7 à “little”-endian comes from interpreting byte address 0 as the “least”-significant byte u Big-endian (e. g. , IBM Power. PC) Byte address MSB 0 1 2 LSB 3 4 5 6 7 à “big”-endian comes from interpreting byte address 0 as the “most”-significant byte ISA-12

Common addressing modes m Register u Add R 4, R 3 u R 4

Common addressing modes m Register u Add R 4, R 3 u R 4 = R 4 + R 3 u Used when value is in a register m Immediate u Add R 4, #3 u R 4 = R 4 + 3 u Useful for small constants, which occur frequently m Displacement u Add R 4, 100(R 1) u R 4 = R 4 + Mem[100+R 1] u Accesses the frame (arguments, local variables) u Accesses the global data segment u Accesses fields of a data struct ISA-13

Advertisement
Addressing modes (cont. ) m Register deferred/Register indirect u Add R 3, (R 1)

Addressing modes (cont. ) m Register deferred/Register indirect u Add R 3, (R 1) u R 3 = R 3 + Mem[R 1] u Access using a computed address m Indexed u Add R 3, (R 1 + R 2) u R 3 = R 3 + Mem[R 1 + R 2] u Array accesses à R 1 = base, R 2 = index m Direct/Absolute u Add R 1, (1001) u R 1 = R 1 + M[1001] u Accessing global (“static”) data ISA-14

Addressing modes (cont. ) m Memory indirect/Memory deferred u Add R 1, @(R 3)

Addressing modes (cont. ) m Memory indirect/Memory deferred u Add R 1, @(R 3) u R 1 = R 1 + Mem[R 3]] u Pointer dereferencing: x = *p; (if p is not register-allocated) m Autoincrement/Postincrement u Add R 1, (R 2)+ u R 1 = R 1 + Mem[R 2]; R 2 = R 2 + d (d is size of operation) u Looping through arrays, stack pop m Autodecrement/Predecrement u Add R 1, -(R 2) u R 2 = R 2 - d; R 1 = R 1 + Mem[R 2] (d is size of operation) u Same uses as autoincrement, stack push m Scaled u Add R 1, 100(R 2)[R 3] u R 1 = R 1 + Mem[100+R 2+R 3*d] (d is size of operation) u Array accesses for non-byte-sized elements ISA-15

Wisdom about modes m Need: u Register, Displacement, Immediate and optionally Indexed (indexed simplifies

Wisdom about modes m Need: u Register, Displacement, Immediate and optionally Indexed (indexed simplifies array accesses) u Displacement size 12 -16 bits (empirical) u Immediate: 8 to 16 bits (empirical) u Can synthesize the rest from simpler instructions u Example-- MIPS architecture: à Register, displacement, Immediate modes only à both immediate and displacement: 16 bits m Choice depends on workload! u For example, floating-point codes might require larger immediates, or 64 bit wordsize machines might also require larger immediates (for *p++ kind of operations) ISA-16

Control transfer semantics m Types of branches u Conditional u Unconditional à Normal à

Control transfer semantics m Types of branches u Conditional u Unconditional à Normal à Call à Return m PC Relative (Branch) vs. Absolute (Jump) u Branch allows relocatable (“position independent”) code u Jump allows branching further than PC relative ISA-17

Parts of a control transfer m WHERE u Determine target address m WHETHER u

Parts of a control transfer m WHERE u Determine target address m WHETHER u Determine if transfer should occur or not m WHEN u Determine when in time the transfer should occur m Each of the three decisions can be decoupled ISA-18

Types of control transfer (cont). m All three together: Compare and branch instruction u

Types of control transfer (cont). m All three together: Compare and branch instruction u Br (R 1 = R 2), destination u (+) A single instruction u (-) Heavy hardware requirement, inflexible scheduling m WHETHER separate from WHERE/WHEN: u Condition code register (CMP R 1, R 2 … BEQ dest) à (+) Sometimes test happens “for free” à (-) Hard for compiler to figure out which instructions depend on CC register u Condition register (SUB R 1, R 2 … BEQ R 1, dest) à (+) Simple to implement, dependencies between instructions are obvious to compiler à (-) Uses a register (“register pressure”) ISA-19

Prepare-to-branch Decouple all three of WHERE / WHETHER / WHEN m WHERE: PBR BTR

Prepare-to-branch Decouple all three of WHERE / WHETHER / WHEN m WHERE: PBR BTR 1 = destination m u BTR 1 = “Branch target register #1” m WHETHER: CMP PR 2 = (R 1 = R 2) u PR 2 = “Predicate register #2” WHEN BR BTR 1 if PR 2 m (+) Schedule each instruction so it happens during “free time” when hardware is idle m (-) Three instructions: higher IC m From the HP Labs Play. Doh architecture m ISA-20

Instruction Encoding tradeoffs m Variable width u Common instructions are short (1 -2 bytes),

Instruction Encoding tradeoffs m Variable width u Common instructions are short (1 -2 bytes), less common or more complex instructions are long (>2 bytes) u (+) Very versatile, uses memory efficiently u (-) Instruction words must be decoded before number of instructions is known m Fixed width u Typically 1 instruction per 32 -bit word (Alpha is 2 instructions per word) u (+) Every instruction word is an instruction, Easier to fetch/decode u (-) Uses memory inefficiently ISA-21

Addressing mode encoding m Each operand has a “mode” field u Also called “address

Addressing mode encoding m Each operand has a “mode” field u Also called “address specifiers” u VAX, 68000 u (+) Very versatile u (-) Encourages variable-width instructions (hard decode) m Opcode specifies addressing mode u Most RISCs u (+) Encourages fixed-width instructions (easy decode) u (+) “Natural” for a load/store ISA u (-) Limits what every instruction can do à But only matters for loads and stores ISA-22

Compiler impact m Parse u Use a “virtual source level” representation u Loop interchange,

Compiler impact m Parse u Use a “virtual source level” representation u Loop interchange, etc. High-level intermediate language High-level Optimize Low-level intermediate language m Low-level intermediate language Allocate, Schedule translate Low-level opt: u Clean up parser refuse u Each “optimization pass” runs as a filter u Enhance parallelism Low-level Optimize Code generation: High-level opt: m Code generation: u Allocate registers u Schedule code for high performance u More later on this Assembly code ISA-23

Example: MIPS A load/store, fixed-encoding architecture with a “condition register” architecture I-type instruction 6

Example: MIPS A load/store, fixed-encoding architecture with a “condition register” architecture I-type instruction 6 5 5 16 Opcode rs 1 rd Immediate Load, store, all immediate operations, conditional branches (rd unused) Jump through register, call through register (“jump and link register”) Opcode is in the same place for every instruction R-type instruction 6 5 5 Opcode rs 1 rs 2 rd Shamt 6 Func Register-register ALU operations “Func” is an opcode extension J-type instruction 6 Opcode 26 Offset added to PC Jump, call (“jump and link”), trap and return from exception ISA-24

ISA of MIPS 64 0 R 1 R 2. . . R 31 R

ISA of MIPS 64 0 R 1 R 2. . . R 31 R 0 is permanent 0 PC 64 0 F 1 F 2. . . F 31 Use one half of Fi for single precision ops Load/store architecture Transfer sizes: B (byte), H (halfword), W (word), D (double word) No unaligned accesses allowed Only 3 addressing modes: register, immediate, displacement ISA-25

MIPS example code Loop DADDI LD L. D ADD. D S. D DADDI BNE

MIPS example code Loop DADDI LD L. D ADD. D S. D DADDI BNE R 1, R 0, 10 R 2, A F 0, 0(R 2) F 4, F 0, F 2 0(R 2), F 4 R 1, -1 R 2, 8 R 1, Loop Put 10 into R 1 (R 0 = 0) Put A in R 2 Load double FP value into F 0 Add F 2 to F 0 Store result back to memory Decrement I Increment loop pointer ISA-26