-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Teach backend to emit
iinc
instructions
The backend is now able to turn `x += 42` into an `iinc 42` instruction. The optimization only applies to `+=` and `-=`, provided the the net increment fits inside a signed 16-bit value (the ASM library handles choosing `iinc` or `wide iinc` as is appropriate). Ported from scala/scala#9713
- Loading branch information
1 parent
461b30f
commit 29de10a
Showing
5 changed files
with
93 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package dotty.tools.backend.jvm | ||
|
||
import org.junit.Test | ||
import org.junit.Assert._ | ||
|
||
import scala.tools.asm.Opcodes._ | ||
|
||
class IincTest extends DottyBytecodeTest { | ||
import ASMConverters._ | ||
|
||
@Test def increment = test( | ||
"""{ | ||
| var i = x | ||
| i += 1 | ||
| i += 54 | ||
| i += 127 | ||
| i -= 1 | ||
| i -= 54 | ||
| i -= 128 | ||
| i | ||
|}""".stripMargin, | ||
List(1, 54, 127, -1, -54, -128) | ||
) | ||
|
||
@Test def wideIncrement = test( | ||
"""{ | ||
| var i = x | ||
| i += 128 | ||
| i += 8765 | ||
| i += 32767 | ||
| i -= 129 | ||
| i -= 8765 | ||
| i -= 32768 | ||
| i | ||
|}""".stripMargin, | ||
List(128, 8765, 32767, -129, -8765, -32768) | ||
) | ||
|
||
@Test def tooBigForIinc = test( | ||
"""{ | ||
| var i = x | ||
| i += 32768 | ||
| i += 56789 | ||
| i += 2147483647 | ||
| i -= 32769 | ||
| i -= 56789 | ||
| i -= 2147483647 | ||
| i | ||
|}""".stripMargin, | ||
Nil | ||
) | ||
|
||
private def test(code: String, expectedIincs: List[Int])= { | ||
val source = | ||
s"""class Increment { | ||
| def test(x: Int): Int = $code | ||
|} | ||
""".stripMargin | ||
|
||
checkBCode(source) { dir => | ||
val clsIn = dir.lookupName("Increment.class", directory = false).input | ||
val clsNode = loadClassNode(clsIn) | ||
val meth = getMethod(clsNode, "test") | ||
|
||
val foundIincs = instructionsFromMethod(meth).collect { case iinc: Incr => iinc.incr } | ||
|
||
assertEquals(expectedIincs, foundIincs) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters