mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-13 00:21:28 +00:00
JVM_IR: Generate better code for null checks.
Simplify ifs when branches have condition true/false.
Simplify blocks containing only a variable declaration
and a variable get of the same variable. Simplify to
just the condition.
Do not introduce temporary variables for constants for
null checks. Constants have no side-effects and can be
reloaded freely instead of going through a local.
This simplifies code such as "42.toLong()!!" so that the
resulting code has no branches and uses no locals. The
simplifications happen as follows:
```
block
temp = 42.toLong()
when
(temp == null) throw NPE
(true) load temp
---> null test simplification
block
temp = 42.toLong()
when
(false) throw NPE
(true) load temp
---> when simplification
block
temp = 42.toLong()
load temp
---> block simplification
42.toLong()
```
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
fun test() {
|
||||
if (false) {
|
||||
try {
|
||||
|
||||
9
compiler/testData/codegen/bytecodeText/exclExcl/primitive.kt
vendored
Normal file
9
compiler/testData/codegen/bytecodeText/exclExcl/primitive.kt
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
fun box(): String {
|
||||
42!!
|
||||
42.toLong()!!
|
||||
return "OK"!!
|
||||
}
|
||||
|
||||
// 0 LOAD
|
||||
// 0 STORE
|
||||
// 0 IF
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
fun test(a: Any?, b: Any?, c: Any?) {
|
||||
when (null) {
|
||||
a -> throw IllegalArgumentException("a is null")
|
||||
|
||||
Reference in New Issue
Block a user