add L8.pdf and complement in 11.06
This commit is contained in:
50
notes/7.md
50
notes/7.md
@@ -17,12 +17,10 @@ Keep the top of the stack in a register, so `add` requires only a single memory
|
||||
* `pop`
|
||||
* `add`
|
||||
|
||||
|
||||
### Code Generation From Stack Machine
|
||||
|
||||
Assume that stack grows towards lower addresses.
|
||||
|
||||
|
||||
## MIPS
|
||||
|
||||
32 regs
|
||||
@@ -36,7 +34,6 @@ Assume that stack grows towards lower addresses.
|
||||
* `li`
|
||||
* `mv`
|
||||
|
||||
|
||||
Converting Stack to MIPS ISA
|
||||
|
||||
* `acc <- i`
|
||||
@@ -44,13 +41,56 @@ Converting Stack to MIPS ISA
|
||||
|
||||
### Optimizing
|
||||
|
||||
|
||||
no
|
||||
|
||||
## Branch
|
||||
|
||||
`beq $1 $2 lbl`
|
||||
`b lbl`
|
||||
|
||||
|
||||
## Function
|
||||
|
||||
At Caller Side:
|
||||
1. saves the `$fp` to the stack
|
||||
2. saves the actual params in reverse order
|
||||
3. saves the return address to `$ra`
|
||||
|
||||
At Callee Side:
|
||||
1. set `$fp` to `$sp`
|
||||
2. the callee saves the return address
|
||||
3. ...
|
||||
|
||||
## Temp Var
|
||||
|
||||
Many various intermediate vars should be stored in the AR. But compiler can statically know how many temporary variables there are.
|
||||
|
||||
Let $NT(e)$ is defined recursively by:
|
||||
$$NT(e1 + e2) = \max (NT(e1), NT(e2) + 1)$$
|
||||
|
||||
for example:
|
||||
|
||||
### CG using NT
|
||||
|
||||
add new args to the `cgen(e, nt)`
|
||||
|
||||
reduce number of decrease `$sp` (`addi $sp $sp -4`)
|
||||
|
||||
|
||||
## Intermediate Representation (IR)
|
||||
|
||||
Before:
|
||||
Each Languages need respective, different Optimizer.
|
||||
|
||||
Now:
|
||||
Common IR optimizer.
|
||||
|
||||
|
||||
IR is language-independent and machine-independent optimization.
|
||||
|
||||
### High-Level Assembly
|
||||
|
||||
It uses unlimited number of registers.
|
||||
It uses assembly-like control structures (jmp and lbl).
|
||||
opcodes but some are higher level
|
||||
|
||||
igen(e, t)
|
||||
52
notes/8.md
Normal file
52
notes/8.md
Normal file
@@ -0,0 +1,52 @@
|
||||
# Dataflow Analysis
|
||||
|
||||
Optimization means improving resource utilization not changing what the program computes.
|
||||
Resource utilization means many things:
|
||||
* **Execution time**
|
||||
* Code size
|
||||
* Network messages sent.
|
||||
|
||||
## Basic Block (BB)
|
||||
|
||||
A BB is a maximum sequence of instructions with **no labels**, **no jumps**
|
||||
All instructions in a BB has fixed control flow.
|
||||
|
||||
## Control Flow Graph
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Entry --> A
|
||||
A[BB1] --> B[BB2]
|
||||
A --> C[BB3]
|
||||
B --> D[BB4]
|
||||
C --> D
|
||||
D --> E[BB5]
|
||||
E --> G[BB7]
|
||||
E --> F[BB6]
|
||||
G --> Exit
|
||||
F --> Exit
|
||||
```
|
||||
|
||||
## Local Optimization
|
||||
|
||||
### Algebraic Simplification
|
||||
|
||||
x := x + 0 -> x := 0
|
||||
y := y ** 2 -> y := y * y
|
||||
x := x * 8 -> x := x << 3
|
||||
x := x * 15 -> t := x << 4; x := t - x
|
||||
|
||||
### Constant Folding
|
||||
|
||||
x := 2 + 2 -> x := 4
|
||||
if 2 < 0 jump L -> nop
|
||||
if 2 > 0 jump L -> jump L
|
||||
|
||||
But Constant folding can be dangerous on cross-compilation (in precision).
|
||||
|
||||
|
||||
### Unreachable Code
|
||||
|
||||
### Dead Code Elimination
|
||||
|
||||
## Global Optimization
|
||||
Reference in New Issue
Block a user