optimizer: don't replace for loop with repeat loop (the loop variable might be used elsewhere!)

This commit is contained in:
Irmen de Jong 2023-07-05 21:14:42 +02:00
parent e705a8bd89
commit 07c606bfc9
3 changed files with 4 additions and 11 deletions

View File

@ -176,16 +176,6 @@ class StatementOptimizer(private val program: Program,
}
}
val iterationCount = forLoop.constIterationCount(program)
if(iterationCount!=null) {
val loopName = forLoop.loopVar.nameInSource
if(!forLoop.iterable.referencesIdentifier(loopName) && !forLoop.body.referencesIdentifier(loopName)) {
errors.warn("for loop can be replaced with repeat loop", forLoop.position)
val repeat = RepeatLoop(NumericLiteral.optimalNumeric(iterationCount, forLoop.position), forLoop.body, forLoop.position)
return listOf(IAstModification.ReplaceNode(forLoop, repeat, parent))
}
}
return noModifications
}

View File

@ -210,7 +210,7 @@ main {
var result = compileText(target, true, src, writeAssembly = true)!!
var virtfile = result.compilationOptions.outputDir.resolve(result.compilerAst.name + ".p8ir")
VmRunner().runAndTestProgram(virtfile.readText()) { vm ->
vm.stepCount shouldBe 36
vm.stepCount shouldBe 48
}
result = compileText(target, false, src, writeAssembly = true)!!

View File

@ -1,6 +1,9 @@
TODO
====
- check for name clash: variable vs block name
...