Commit Graph

266 Commits

Author SHA1 Message Date
Dmitry Petrov
16b7bece46 Fix constant expression inlining logic
Constant expressions are inlined if they do not depend on non-inlineable
vals.
Java constants are always inlined.
Kotlin constants are inlined in LV 1.1+.
2017-10-13 17:01:42 +03:00
Dmitry Petrov
e71090ae4c Spill stack for inline functions only if it's required
Stack should be spilled before inline function call and restored after
call only if one of the following conditions is met:
- inline function is a suspend function
- inline function has try-catch blocks
- inline function has loops (backward jumps)

Note that there're quite some "simple" inline functions in Kotlin stdlib
besides run/let/with/apply. For example, many string operations are
implemented as inline wrappers over Java method calls.
2017-10-10 08:49:19 +03:00
Dmitry Petrov
73724bcdc7 Do not copy immediately created arrays in spread arguments
E.g., 'foo(x = *intArrayOf(42))'.

 #KT-20462 Fixed
2017-09-28 14:55:11 +03:00
Mikhael Bogdanov
81a1bf3319 Recalculate max stack on method emitting: optimizations could change it 2017-09-06 08:20:32 +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
Denis Zharkov
ad9fe53ee2 Avoid local var entry for continuation parameter in suspend function
The primary reason is getting rid of redundant stack spilling, but also
it's not very sensible to have such entry since the parameter
is synthetic
2017-08-22 18:43:31 +03:00
Denis Zharkov
fcd7677a3f Fix compatibility of suspend functions with strict bytecode analyzers
In short, some of the bytecode analyzers assume that there could be
no stores instructions into parameter vars with value of different
types (even when the value type is a subtype)

See the issue for details

 #KT-19713 Fixed
2017-08-22 18:43:26 +03:00
Dmitry Petrov
2ed5a5e368 'when' should use intrinsics for '=='
#KT-19029 Fixed Target versions 1.1.5
 #KT-18818 Fixed Target versions 1.1.5
2017-08-07 10:31:02 +03:00
Dmitry Petrov
c9ad290ad5 Do not store null for temporary in destructuring assignment
#KT-19256 Fixed Target versions 1.1.5
2017-07-27 09:02:26 +03:00
Dmitry Petrov
b867c46f72 Generate for-in-range loop as counter loop when possible
If an upper bound is a compile-time constant != Int.MAX_VALUE,
we can generate 'for (i in x..N)' as 'for (i in x until N+1)'.
2017-07-27 09:02:26 +03:00
Dmitry Petrov
f558b4238c Use counter loop in intrinsic array constructors
#KT-19149 Fixed Target versions 1.1.5
2017-07-24 10:17:30 +03:00
Dmitry Petrov
e1a55e8dec Prefer compact bytecode for primitive/object comparisons with Boolean
'java.lang.Boolean#valueOf' doesn't allocate new objects
(it uses pre-allocated singletons for 'true' and 'false').
2017-07-21 08:52:29 +03:00
Dmitry Petrov
8e9c0294fe Do not box primitives for 'primitive == object'
NB user-defined 'equals' can violate contract for 'Object#equals', e.g.,
it can be asymmetric.
Thus we can't avoid boxing for 'object == primitive'.
2017-07-21 08:52:29 +03:00
Dmitry Petrov
8aacddb9f0 Avoid primitive boxing for 'boxed == primitive' if possible
This makes sense for non-floating-point primitive type
(boolean, char, byte, short, int, long):
floating-point types use specialized versions of 'areEqual'.
2017-07-21 08:52:29 +03:00
Nikolay Krasko
ce4f923ba0 Replace return with nop to avoid merging line instructions (KT-18949)
Dex ignores subsequent line numbers for same instructions and interprets
instruction after inline as if they were inlined. This makes debugger
behaves as if there's nowhere to stop on line with breakpoint.

This also makes stepping through inline function consistent with
non-inline analog. In both context debugger now stops on '}'.

 #KT-18949 Fixed
 #KT-17120 Fixed
2017-07-18 14:39:54 +03:00
Dmitry Petrov
8c7352e668 Generate constant "" for effectively empty string 2017-07-17 09:18:41 +03:00
Dmitry Petrov
8a9707c140 Generate proper initialization for possibly nullable SAM wrapper
#KT-18916 Fixed Target versions 1.1.5
2017-07-12 09:37:16 +03:00
Dmitry Petrov
aa7db727ba Fold I2L with ICONST_n when beneficial
ICONST_0; I2L -> LCONST_0
ICONST_1; I2L -> LCONST_1
2017-07-10 10:51:26 +03:00
Dmitry Petrov
ad80c3cd7f If at least one of the range bounds is "pure", order doesn't matter 2017-07-10 10:51:26 +03:00
Dmitry Petrov
22e12dc139 Minor: use method names in bytecode text tests 2017-07-10 10:51:26 +03:00
Dmitry Petrov
6a3ff5ca46 Add tests for intrinsified in/!in and mismatching range types 2017-07-10 10:51:26 +03:00
Dmitry Petrov
850e1b11fc Minor: test modification after review 2017-07-10 10:51:26 +03:00
Dmitry Petrov
9d1901fc7c Intrinsify some mismatching range/element combinations for in/in!
It's safe to upcast integer types to Long,
floating-point types to Double.
So we don't have to create a range instance for cases such as

fun testLongInInt(x: Long, a: Int, b: Int) =
    x in a .. b

which is equivalent to

fun testLongInInt(x: Long, a: Int, b: Int) =
    x in a.toLong() .. b.toLong()
2017-07-10 10:51:26 +03:00
Dmitry Petrov
ea95f31f99 Simplify some basic instructions using peephole optimization
DUP_X1; POP = SWAP

p1_a; p1_b; SWAP = p1_b; p1_a
where p1_a, p1_b are instructions without side effects pushing value of
size 1 on stack.
E.g.: ACONST_NULL; ALOAD 0; SWAP = ALOAD 0; ACONST_NULL

NOP; NOP = NOP
2017-07-10 10:51:26 +03:00
Dmitry Petrov
224848163d Use InContinuousRangeExpressionGenerator for primitive range intrinsics 2017-07-10 10:51:26 +03:00
Dmitry Petrov
614d90d6ef Skip NOP instructions in fast pop backward propagation 2017-07-10 10:51:26 +03:00
Dmitry Petrov
36cbc0defd Ensure that {Collection, CharSequence}.indices intrinsic works with 'in' 2017-07-10 10:51:26 +03:00
Dmitry Petrov
ec99fa2142 Ensure that 'in/!in x until y' intrinsic works 2017-07-10 10:51:26 +03:00
Dmitry Petrov
05cad83c79 Fix tests for in/in! with primitive range literals
- ClosedFloatingPointRange<T>
- IntRange, etc
- extensions in kotlin.ranges for ranges of Byte/Int/Short
2017-07-10 10:51:26 +03:00
Dmitry Petrov
5decf65d6a Ensure that no range instance is created for 'x in array.indices' 2017-07-10 10:51:26 +03:00
Dmitry Petrov
4320b63922 Ensure that no boolean negation is generated for !in expression
#KT-18777 Fixed Target versions 1.1.4
2017-07-10 10:51:26 +03:00
Dmitry Petrov
b9c0ecf2b1 Ensure that ranges are not created for in-Comparable-range-literal
#KT-6247 Fixed Target versions 1.1.4
2017-07-10 10:51:26 +03:00
Dmitry Petrov
a7071ae7af Provide intrinsic generators for in/!in expression
TODO some tests should fail because range of comparables
(e.g., '"Alpha" .. "Omega"') is currently not implemented
2017-07-10 10:51:26 +03:00
Mikhael Bogdanov
69457ef3f1 Generate optimized hashCode for primitive type with jvmTarget 1.8+
#KT-7571 Fixed
2017-06-28 13:44:07 +02:00
Dmitry Petrov
d5e02f069a Fast version for POP backward propagation (without SourceInterpreter) 2017-06-27 14:28:42 +03:00
Dmitry Petrov
a84c2a6f31 Improve string concatentation & string templates code generation
Reuse StringBuilder instances for nested subexpressions.
(NB StringBuilder instance for string template with a string
concatenation inside an expression entry, such as `"${"a" + "b"}"`,
will not be reused, although that doesn't seem to be a real-life issue).

 #KT-18558 Fixed Target versions 1.1.4
 #KT-13682 Fixed Target versions 1.1.4

Join adjacent strings literals, escaped strings, and constant values
(in a language version that supports const val inlining).
Use StringBuilder#append(char) for single-character constants
(e.g., " " in "$a $b").

 #KT-17280 Fixed Target versions 1.1.4
 #KT-15235 Fixed Target versions 1.1.4
2017-06-27 14:28:42 +03:00
Dmitry Petrov
786ac46fa6 Do not store nulls for captured variables going out of scope
These values can't be read after going out of scope.
JVM implementation can take care of such object references on its own.
Ref objects for captured variables are not different from any other
objects stored in local variables, so there's really no reason to
nullify these references explicitly.

 #KT-18478 Fixed Target versions 1.1.4
2017-06-20 13:31:36 +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
39aa97eebf Use Intrinsics#compare when specializing compareTo for ints 2017-05-22 11:57:20 +03:00
Dmitry Petrov
e1b41eee15 Specialize Comparable#compareTo for boxed primitives
Support Comparable#compareTo for boxed primitive in redundant
boxing/unboxing analysis, along with CHECKCAST to java.lang.Comparable.

Note that we can do that for Float and Double, too, because
Float#compareTo(Float) and Double#compareTo(Double) are delegated to
Float#compare(float, float) and Double#compare(double, double),
respectively.

Fuse specialized comparison for integers with conditional jumps
if possible (both for Comparable#compareTo and Intrinsics#areEqual).

 #KT-11959 Fixed
2017-05-22 11:57:20 +03:00
Dmitry Petrov
899ad7bb53 Generate for-in-indices as a precondition loop
Precondition loops are better optimized by HotSpot
(and, quite likely, by ART).
Also, we generate more compact bytecode that way.

KT-17903 Generate 'for-in-indices' as a precondition loop
2017-05-16 17:28:43 +03:00
Dmitry Petrov
84e54124a2 More aggressive DCE should honor debugger invariants
- A LINENUMEBER node is "dead" if the corresponding instruction interval
 contains at least one "dead" bytecode instruction
 and no live bytecode instructions

- Observable local variable lifetimes should be taken into account
 when determining if a NOP is required for debugger.
2017-05-16 17:28:43 +03:00
Dmitry Petrov
65799a5cb4 Fix debugger-related tests after dead code elimination improvements 2017-05-16 17:28:43 +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
2051355d6a Optimize constant conditions
Using basic constant propagation (only integer constants, no arithmetic
calculations), rewrite conditional jump instructions with constant
arguments.

This covers problem description in KT-17007.
Note that it also works transparently with inline functions.
Partial evaluation is required to cover more "advanced" cases.

As a side effect, this also covers KT-3098:
rewrite IF_ICMP<cmp_op>(x, 0) to IF<cmp0_op>(x).
2017-05-16 17:28:43 +03:00
Dmitry Petrov
495fba43c0 Fuse primitive equality with safe call to avoid boxing
In code like 'a?.b == 42', we can immediately generate equality
comparison result when receiver is null (false for '==', true for '!='),
since the primitive value is definitely non-null.
Otherwise unnecessary boxing/unboxing is generated to handle possibly
null result of 'a?.b'.
2017-05-16 17:28:43 +03:00
Denis Zharkov
d92c403f9e Move helpers for coroutine tests in separate package
It will help to skip their content when rendering bytecode listing
for box tests
2017-05-05 14:01:50 +03:00
Dmitry Petrov
506941e7e0 Optimize range operations for 'until' extension from stdlib (KT-9900)
NB: for-in-until loop is generated as precondition loop, because the
corresponding range is right-exclusive (and thus we have no problems
with integer overflows).
2017-05-04 10:09:42 +03:00
Mikhael Bogdanov
a7c9e14805 Don't generate default arguments for inline call 2017-04-28 12:19:09 +02:00