Commit Graph

29 Commits

Author SHA1 Message Date
Dmitry Petrov
f678db2f89 KT-36813 Generate optimizable null checks in JVM_IR 2020-03-03 22:38:21 +03:00
pyos
ed83e3ccef Optimize more redundant kotlin/jvm/internal/Refs
The number of initializations of the `value` field before the live range
begins does not really matter so long as we insert a write of a default
value to the local if there were none.
2020-03-02 20:20:55 +03:00
Dmitry Petrov
0b9b1b6945 Update bytecode text tests for JVM_IR 2020-02-18 22:51:29 +03:00
Dmitry Petrov
ee020ef290 KT-36336 @EnhancedNullability and null checks
Don't insert implicit null check on a value of @EnhancedNullability type
used where @EnhancedNullability type is expected.

This uncovers a bunch of other problems in FE and BE.
KT-36343 and KT-36347 are bugs in StrictJavaNullabilityAssertions
implementation which should most likely be fixed in next major language
version (with proper breaking change notice).
KT-36344 is a design problem which should be addressed after 1.4 issues
are resolved.
2020-02-05 11:15:41 +03:00
Steven Schäfer
cf3e4608f3 JVM IR: Support -Xno-call-assertions 2020-01-30 14:43:23 +03:00
Alexander Udalov
6a90dc2efe Fix bytecode text tests for language version 1.4
New null check assertions are generated a bit more efficiently, with a
call to `checkNotNull` instead of IFNONNULL+jump.
2020-01-20 19:12:59 +01:00
Mikhail Zarechenskiy
5c5635ce20 Fix codegen & bytecode tests after unifying exceptions in JVM backend
See KT-22275 for details
2020-01-20 16:36:03 +03:00
Dmitry Petrov
1d5370a56a Fix rationale for test muted in JVM_IR (see KT-35656) 2019-12-26 12:44:54 +03:00
Dmitry Petrov
f6b99955f8 Update testData in bytecodeText (argument nullability assertions) 2019-12-06 11:03:07 +03:00
Alexander Udalov
9d257a1ed8 JVM IR: generate call to checkNotNull in IrCheckNotNull since 1.4
#KT-22275
2019-09-16 12:33:01 +02:00
Dmitry Petrov
7d49b72b79 JVM_IR: Support 'CHECK_NOT_NULL' intrinsic
TODO: migrate to changes related to KT-22275
2019-08-14 11:16:10 +03:00
Alexander Udalov
2baddb029c Use Intrinsics.checkNotNullParameter to throw NPE in parameter null checks
Similarly to previous commits, this method was unused, so we're changing
its semantics in API version >= 1.4.

 #KT-22275 In Progress
2019-08-12 16:09:23 +02:00
Alexander Udalov
480313210a Use Intrinsics.checkNotNullExpressionValue to throw NPE in Java null checks
Similarly to previous commit, this method was unused since its
introduction before 1.0, so we're changing its semantics to throw NPE
and starting to use it with API version >= 1.4.

 #KT-22275 In Progress
2019-08-12 16:09:23 +02:00
Alexander Udalov
a7c8fdcbe2 Use Intrinsics.checkNotNull and throw NPE in !! operator generation
This method was introduced in c204e8fc67 "just in case" and was never
used. Therefore we're free to change its semantics and use it in all new
generated code (with API version >= 1.4), without even worrying that the
newly used API will leak from inline functions in stdlib when used with
an older API version. Since we agreed to change the type of thrown
exceptions to java.lang.NPE in KT-22275, invoke a new method
throwJavaNpe now which throws that exception instead of KNPE.

Note that the additional method that takes an exception message is still
unused and exists just in case we need to use it in the future. The new
method throwJavaNpe is public also "just in case" we need to invoke it
in the future; currently it's not invoked from the bytecode.

 #KT-22275 In Progress
2019-08-12 16:09:23 +02:00
pyos
6b2d874ccc JVM_IR: generate non-null assertions for arguments 2019-05-21 18:30:27 +03:00
Mikhael Bogdanov
4c59d161d4 Support basic reification in IR 2019-04-08 13:10:22 +02:00
Mads Ager
057bdb3954 Generate better code for branches based on comparisons.
For comparison intrinsics and for instanceof checks, make
it possible to get the the stack value produced and branch
on that directly instead of materializing a boolean to
branch on from it.

That reduces code such as

```
    IF_CMPEQ L1
    CONST_0
    GOTO L2
L1: CONST_1
L2: IFEQ L3
```

to just one IF_CMP instruction.
2019-02-19 15:46:52 +01:00
Mads Ager
690b8e0ac9 JVM_IR: Optimize null checks.
Introduce lowering to remove null checks for primitive type
expressions and replace them with true/false. Side-effects
are preserved.

Generate ifnull/ifnonnull instructions for null checks instead
of materializing a null literal for an equality check.
2019-01-19 09:43:43 +01:00
Mads Ager
3a11322506 Enable bytecode text tests for the JVM_IR backend. 2018-12-21 16:20:45 +01:00
Alexander Udalov
f5ff3d2fa9 Remove directives that have no effect from bytecode text tests
All bytecode text tests are run with stdlib in the classpath and only
for JVM backend, therefore directives WITH_RUNTIME, TARGET_BACKEND,
IGNORE_BACKEND are not needed
2018-12-20 12:53:24 +01:00
Alexander Udalov
5b58eb8491 Remove LANGUAGE_VERSION from non-coroutine codegen tests
Most of these tests used this directive as a way to opt in to a new
language feature, and most of those features are already stable for a
long time, so no opt-in is needed. Some other tests used the directive
to opt out from a language feature, replace those by the `LANGUAGE`
directive. One test used the directive to test behavior that actually
depended on the API version; use `API_VERSION` directive there instead.
2018-12-20 12:53:23 +01:00
Alexander Udalov
f59963ef73 Simplify test case on deterministic null-checks 2018-05-02 14:40:15 +02:00
Denis Vnukov
c5373c9029 Ensure deterministic insertion of checkExpressionValueIsNotNull.
Kotlinc source’s file DescriptorUtils.kt demonstarted non-deterministic
insertion of checkExpressionValueIsNotNull for value returned by
CallableDescriptor::getOriginal(). It was difficult to reproduce
this behavior on ф smaller example, but I added a test which was
failing once in 5-10 times while I was testing manually.

I believe this bug is close to KT-23704.

This PR addresses non-determinism to a degree when I can run 120
Compilations with './gradlew dist' and get same classes in all
jars in 'dist'.

NOTE that thew fact that insertion of checkExpressionValueIsNotNull may
depend on order of the types seems suspicios. This CL only addresses
non-determinism part, but I believe it’s worth looking into this more
from semantics point of view.
2018-05-02 14:36:04 +02:00
Dmitry Petrov
78b69cad77 Support lateinit local vars in redundant null check elimination
Lateinit local vars are guaranteed to be non-null after store.
So we mark such stores as storing non-null value
(could be useful for some other constructs, too),
and optimize null checks accordingly.
2017-08-31 11:28:08 +03:00
Dmitry Petrov
08885e273b Support additional intrinsics in null check elimination
1. checkExpressionValueIsNotNull implies checked value is non-null

2. throwNpe never returns

 #KT-18162 Fixed Target versions 1.1.4
 #KT-18164 Fixed Target versions 1.1.4
2017-05-31 16:48:14 +03:00
Dmitry Petrov
d559212d70 Optimize out trivial INSTANCEOF checks
#KT-18157 Fixed Target versions 1.1.4
2017-05-31 16:48:14 +03:00
Dmitry Petrov
1378b0cf05 Fix bytecode tests after new optimizations
- Turn some const conditions into non-const conditions
- Make sure inlined const values are used where required
(otherwise they are eliminated by POP backward propagation)
2017-05-16 17:28:43 +03:00
Dmitry Petrov
ec403bfdbc KT-16245 Redundant null-check generated for a cast of already non-nullable value
KT-16194 Code with unnecessary safe call contains redundant boxing/unboxing for primitive values
KT-12839 Two null checks are generated when manually null checking platform type

Recognize some additional cases of trivial null checks and trivial instance-of checks.

A variable is "checked for null", if it is:
- a function parameter checked with 'INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull'
- checked for nullability with 'IFNULL/IFNONNULL'
- checked for nullability with 'INSTANCEOF'
  (if objectref is instance-of T, then objectref is non-null)

Before analyzing nullability, introduce synthetic assumptions for execution branches
where a variable is guaranteed to be null or not null. For example, the following bytecode:

     ALOAD 1 // Ljava/lang/String;
     IFNULL L
     <non-null branch>
  L:
     <null branch>

is transformed to

     ALOAD 1
     IFNULL L1
     NEW java/lang/String
     ASTORE 1            // tells analyzer that variable 1 is non-null
     <non-null branch>
  L:
     <null branch>
  L1:
     ACONST_NULL
     ASTORE 1            // tells analyzer that variable 1 is null
     GOTO L

After the analysis is performed on a preprocessed method,
remember the results for "interesting" instructions
and revert the preprocessing transformations.

After that, perform bytecode transformations as usual.

Do not transform INSTANCEOF to-be-reified, because reification at call site
can introduce null checks. E.g.,

    inline fun <reified T> isNullable() = null is T
    ...
    assert(isNullable<String?>())
2017-03-13 09:04:31 +03:00
Dmitry Petrov
3fc106572e Make redundant null check optimization independent of boxing optimization algorithm.
Run DCE after each single redundant null check optimization pass.
2017-03-13 09:04:31 +03:00