2022-09-19 17:41:43 +00:00
|
|
|
package prog8.codegen.intermediate
|
2022-08-21 15:21:29 +00:00
|
|
|
|
2022-09-19 17:41:43 +00:00
|
|
|
import prog8.intermediate.*
|
|
|
|
|
|
|
|
internal class IRPeepholeOptimizer(private val irprog: IRProgram) {
|
2022-08-21 15:21:29 +00:00
|
|
|
fun optimize() {
|
2022-11-22 01:04:24 +00:00
|
|
|
irprog.blocks.asSequence().flatMap { it.children.filterIsInstance<IRSubroutine>() }.forEach { sub ->
|
2022-10-12 22:56:44 +00:00
|
|
|
removeEmptyChunks(sub)
|
2022-09-30 00:47:33 +00:00
|
|
|
joinChunks(sub)
|
2022-10-16 16:30:14 +00:00
|
|
|
sub.chunks.withIndex().forEach { (index, chunk1) ->
|
2022-09-08 20:59:13 +00:00
|
|
|
// we don't optimize Inline Asm chunks here.
|
2022-10-16 16:30:14 +00:00
|
|
|
val chunk2 = if(index<sub.chunks.size-1) sub.chunks[index+1] else null
|
|
|
|
if(chunk1 is IRCodeChunk) {
|
2022-09-08 20:59:13 +00:00
|
|
|
do {
|
2022-10-16 16:30:14 +00:00
|
|
|
val indexedInstructions = chunk1.instructions.withIndex()
|
|
|
|
.map { IndexedValue(it.index, it.value) }
|
|
|
|
val changed = removeNops(chunk1, indexedInstructions)
|
|
|
|
|| removeDoubleLoadsAndStores(chunk1, indexedInstructions) // TODO not yet implemented
|
|
|
|
|| removeUselessArithmetic(chunk1, indexedInstructions)
|
|
|
|
|| removeWeirdBranches(chunk1, chunk2, indexedInstructions)
|
|
|
|
|| removeDoubleSecClc(chunk1, indexedInstructions)
|
|
|
|
|| cleanupPushPop(chunk1, indexedInstructions)
|
2022-09-08 20:59:13 +00:00
|
|
|
// TODO other optimizations:
|
|
|
|
// more complex optimizations such as unused registers
|
|
|
|
} while (changed)
|
|
|
|
}
|
2022-08-25 19:02:18 +00:00
|
|
|
}
|
2022-10-16 16:30:14 +00:00
|
|
|
removeEmptyChunks(sub)
|
2022-08-21 15:21:29 +00:00
|
|
|
}
|
2022-10-31 23:15:39 +00:00
|
|
|
|
|
|
|
irprog.linkChunks() // re-link
|
2022-08-21 15:21:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 22:56:44 +00:00
|
|
|
private fun removeEmptyChunks(sub: IRSubroutine) {
|
|
|
|
if(sub.chunks.isEmpty())
|
|
|
|
return
|
|
|
|
|
|
|
|
/*
|
|
|
|
Empty Code chunk with label ->
|
|
|
|
If next chunk has no label -> move label to next chunk, remove original
|
|
|
|
If next chunk has label -> label name should be the same, remove original. Otherwise FOR NOW leave it in place. (TODO: consolidate labels into 1)
|
2022-10-16 16:30:14 +00:00
|
|
|
If is last chunk -> keep chunk in place because of the label.
|
2022-10-12 22:56:44 +00:00
|
|
|
Empty Code chunk without label ->
|
|
|
|
should not have been generated! ERROR.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
val relabelChunks = mutableListOf<Pair<Int, String>>()
|
|
|
|
val removeChunks = mutableListOf<Int>()
|
|
|
|
|
|
|
|
sub.chunks.withIndex().forEach { (index, chunk) ->
|
2022-10-16 16:30:14 +00:00
|
|
|
if(chunk is IRCodeChunk && chunk.instructions.isEmpty()) {
|
|
|
|
if(chunk.label==null) {
|
2022-10-12 22:56:44 +00:00
|
|
|
removeChunks += index
|
2022-10-16 16:30:14 +00:00
|
|
|
} else {
|
|
|
|
if (index < sub.chunks.size - 1) {
|
|
|
|
val nextchunk = sub.chunks[index + 1]
|
|
|
|
if (nextchunk.label == null) {
|
|
|
|
// can transplant label to next chunk and remove this empty one.
|
|
|
|
relabelChunks += Pair(index + 1, chunk.label!!)
|
|
|
|
removeChunks += index
|
|
|
|
} else {
|
|
|
|
if (chunk.label == nextchunk.label)
|
|
|
|
removeChunks += index
|
|
|
|
else {
|
|
|
|
// TODO: consolidate labels on same chunk
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 22:56:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
relabelChunks.forEach { (index, label) ->
|
2022-11-02 21:12:42 +00:00
|
|
|
val chunk = IRCodeChunk(label, null)
|
2022-10-12 22:56:44 +00:00
|
|
|
chunk.instructions += sub.chunks[index].instructions
|
|
|
|
sub.chunks[index] = chunk
|
|
|
|
}
|
|
|
|
removeChunks.reversed().forEach { index -> sub.chunks.removeAt(index) }
|
|
|
|
}
|
|
|
|
|
2022-09-30 00:47:33 +00:00
|
|
|
private fun joinChunks(sub: IRSubroutine) {
|
2022-10-25 20:57:04 +00:00
|
|
|
// Subroutine contains a list of chunks. Some can be joined into one.
|
2022-09-30 00:47:33 +00:00
|
|
|
|
2022-10-12 22:56:44 +00:00
|
|
|
if(sub.chunks.isEmpty())
|
2022-09-30 00:47:33 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
fun mayJoin(previous: IRCodeChunkBase, chunk: IRCodeChunkBase): Boolean {
|
2022-10-25 20:57:04 +00:00
|
|
|
if(chunk.label!=null)
|
|
|
|
return false
|
2022-09-30 00:47:33 +00:00
|
|
|
if(previous is IRCodeChunk && chunk is IRCodeChunk) {
|
2022-10-25 20:57:04 +00:00
|
|
|
// if the previous chunk doesn't end in a jump or a return, flow continues into the next chunk
|
|
|
|
val lastInstruction = previous.instructions.lastOrNull()
|
|
|
|
if(lastInstruction!=null)
|
|
|
|
return lastInstruction.opcode !in OpcodesThatJump
|
2022-09-30 00:47:33 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
val chunks = mutableListOf<IRCodeChunkBase>()
|
|
|
|
chunks += sub.chunks[0]
|
|
|
|
for(ix in 1 until sub.chunks.size) {
|
2022-10-30 22:42:41 +00:00
|
|
|
val lastChunk = chunks.last()
|
|
|
|
if(mayJoin(lastChunk, sub.chunks[ix])) {
|
|
|
|
lastChunk.instructions += sub.chunks[ix].instructions
|
|
|
|
lastChunk.next = sub.chunks[ix].next
|
|
|
|
}
|
2022-09-30 00:47:33 +00:00
|
|
|
else
|
|
|
|
chunks += sub.chunks[ix]
|
|
|
|
}
|
|
|
|
sub.chunks.clear()
|
2022-10-25 20:57:04 +00:00
|
|
|
sub.chunks += chunks
|
2022-09-30 00:47:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 17:46:44 +00:00
|
|
|
private fun cleanupPushPop(chunk: IRCodeChunk, indexedInstructions: List<IndexedValue<IRInstruction>>): Boolean {
|
2022-08-21 15:21:29 +00:00
|
|
|
// push followed by pop to same target, or different target->replace with load
|
|
|
|
var changed = false
|
|
|
|
indexedInstructions.reversed().forEach { (idx, ins) ->
|
2022-09-19 17:41:43 +00:00
|
|
|
if(ins.opcode== Opcode.PUSH) {
|
2022-10-04 20:54:14 +00:00
|
|
|
if(idx < chunk.instructions.size-1) {
|
|
|
|
val insAfter = chunk.instructions[idx+1] as? IRInstruction
|
2022-09-26 17:46:44 +00:00
|
|
|
if(insAfter!=null && insAfter.opcode == Opcode.POP) {
|
|
|
|
if(ins.reg1==insAfter.reg1) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
} else {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions[idx] = IRInstruction(Opcode.LOADR, ins.type, reg1=insAfter.reg1, reg2=ins.reg1)
|
|
|
|
chunk.instructions.removeAt(idx+1)
|
2022-08-21 15:21:29 +00:00
|
|
|
}
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return changed
|
|
|
|
}
|
|
|
|
|
2022-09-26 17:46:44 +00:00
|
|
|
private fun removeDoubleSecClc(chunk: IRCodeChunk, indexedInstructions: List<IndexedValue<IRInstruction>>): Boolean {
|
2022-08-21 15:21:29 +00:00
|
|
|
// double sec, clc
|
|
|
|
// sec+clc or clc+sec
|
|
|
|
var changed = false
|
|
|
|
indexedInstructions.reversed().forEach { (idx, ins) ->
|
2022-09-19 17:41:43 +00:00
|
|
|
if(ins.opcode== Opcode.SEC || ins.opcode== Opcode.CLC) {
|
2022-10-04 20:54:14 +00:00
|
|
|
if(idx < chunk.instructions.size-1) {
|
|
|
|
val insAfter = chunk.instructions[idx+1] as? IRInstruction
|
2022-09-26 17:46:44 +00:00
|
|
|
if(insAfter?.opcode == ins.opcode) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
|
|
|
}
|
2022-09-26 17:46:44 +00:00
|
|
|
else if(ins.opcode== Opcode.SEC && insAfter?.opcode== Opcode.CLC) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
|
|
|
}
|
2022-09-26 17:46:44 +00:00
|
|
|
else if(ins.opcode== Opcode.CLC && insAfter?.opcode== Opcode.SEC) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return changed
|
|
|
|
}
|
|
|
|
|
2022-10-16 16:30:14 +00:00
|
|
|
private fun removeWeirdBranches(chunk: IRCodeChunk, nextChunk: IRCodeChunkBase?, indexedInstructions: List<IndexedValue<IRInstruction>>): Boolean {
|
2022-08-21 15:21:29 +00:00
|
|
|
var changed = false
|
|
|
|
indexedInstructions.reversed().forEach { (idx, ins) ->
|
2022-09-19 17:41:43 +00:00
|
|
|
val labelSymbol = ins.labelSymbol
|
2022-10-06 22:34:56 +00:00
|
|
|
|
2022-10-16 16:30:14 +00:00
|
|
|
// remove jump/branch to label immediately below (= next chunk if it has that label)
|
2022-09-19 17:41:43 +00:00
|
|
|
if(ins.opcode== Opcode.JUMP && labelSymbol!=null) {
|
2022-10-16 16:30:14 +00:00
|
|
|
if(idx==chunk.instructions.size-1 && ins.branchTarget===nextChunk) {
|
|
|
|
chunk.instructions.removeAt(idx)
|
|
|
|
changed = true
|
|
|
|
}
|
2022-08-21 15:21:29 +00:00
|
|
|
}
|
2022-09-30 13:27:03 +00:00
|
|
|
// remove useless RETURN
|
2023-03-12 20:54:59 +00:00
|
|
|
if(idx>0 && (ins.opcode == Opcode.RETURN || ins.opcode==Opcode.RETURNREG)) {
|
2022-10-04 20:54:14 +00:00
|
|
|
val previous = chunk.instructions[idx-1] as? IRInstruction
|
2022-10-25 20:57:04 +00:00
|
|
|
if(previous?.opcode in OpcodesThatJump) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-09-30 13:27:03 +00:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
}
|
2022-08-21 15:21:29 +00:00
|
|
|
}
|
|
|
|
return changed
|
|
|
|
}
|
|
|
|
|
2022-09-26 17:46:44 +00:00
|
|
|
private fun removeUselessArithmetic(chunk: IRCodeChunk, indexedInstructions: List<IndexedValue<IRInstruction>>): Boolean {
|
2022-08-21 15:21:29 +00:00
|
|
|
// note: this is hard to solve for the non-immediate instructions atm because the values are loaded into registers first
|
|
|
|
var changed = false
|
|
|
|
indexedInstructions.reversed().forEach { (idx, ins) ->
|
|
|
|
when (ins.opcode) {
|
|
|
|
Opcode.DIV, Opcode.DIVS, Opcode.MUL, Opcode.MOD -> {
|
|
|
|
if (ins.value == 1) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Opcode.ADD, Opcode.SUB -> {
|
|
|
|
if (ins.value == 1) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions[idx] = IRInstruction(
|
2022-08-21 15:21:29 +00:00
|
|
|
if (ins.opcode == Opcode.ADD) Opcode.INC else Opcode.DEC,
|
|
|
|
ins.type,
|
|
|
|
ins.reg1
|
|
|
|
)
|
|
|
|
changed = true
|
|
|
|
} else if (ins.value == 0) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Opcode.AND -> {
|
|
|
|
if (ins.value == 0) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions[idx] = IRInstruction(Opcode.LOAD, ins.type, reg1 = ins.reg1, value = 0)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
2022-09-30 13:27:03 +00:00
|
|
|
} else if (ins.value == 255 && ins.type == IRDataType.BYTE) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
2022-09-30 13:27:03 +00:00
|
|
|
} else if (ins.value == 65535 && ins.type == IRDataType.WORD) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Opcode.OR -> {
|
|
|
|
if (ins.value == 0) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
2022-09-30 13:27:03 +00:00
|
|
|
} else if ((ins.value == 255 && ins.type == IRDataType.BYTE) || (ins.value == 65535 && ins.type == IRDataType.WORD)) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions[idx] = IRInstruction(Opcode.LOAD, ins.type, reg1 = ins.reg1, value = ins.value)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Opcode.XOR -> {
|
|
|
|
if (ins.value == 0) {
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else -> {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return changed
|
|
|
|
}
|
|
|
|
|
2022-09-26 17:46:44 +00:00
|
|
|
private fun removeNops(chunk: IRCodeChunk, indexedInstructions: List<IndexedValue<IRInstruction>>): Boolean {
|
2022-08-21 15:21:29 +00:00
|
|
|
var changed = false
|
|
|
|
indexedInstructions.reversed().forEach { (idx, ins) ->
|
|
|
|
if (ins.opcode == Opcode.NOP) {
|
|
|
|
changed = true
|
2022-10-04 20:54:14 +00:00
|
|
|
chunk.instructions.removeAt(idx)
|
2022-08-21 15:21:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return changed
|
|
|
|
}
|
|
|
|
|
2022-09-26 17:46:44 +00:00
|
|
|
private fun removeDoubleLoadsAndStores(chunk: IRCodeChunk, indexedInstructions: List<IndexedValue<IRInstruction>>): Boolean {
|
2022-08-21 15:21:29 +00:00
|
|
|
var changed = false
|
|
|
|
indexedInstructions.forEach { (idx, ins) ->
|
|
|
|
|
|
|
|
// TODO: detect multiple loads to the same target registers, only keep first (if source is not I/O memory)
|
|
|
|
// TODO: detect multiple stores to the same target, only keep first (if target is not I/O memory)
|
|
|
|
// TODO: detect multiple float ffrom/fto to the same target, only keep first
|
|
|
|
// TODO: detect multiple sequential rnd with same reg1, only keep one
|
|
|
|
// TODO: detect subsequent same xors/nots/negs, remove the pairs completely as they cancel out
|
|
|
|
// TODO: detect multiple same ands, ors; only keep first
|
|
|
|
// TODO: (hard) detect multiple registers being assigned the same value (and not changed) - use only 1 of them
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
return changed
|
|
|
|
}
|
|
|
|
}
|