Commit Graph

168 Commits

Author SHA1 Message Date
Nadav Rotem
d78b376d07 [passes] Replace the old invalidation lattice with a new invalidation scheme.
The old invalidation lattice was incorrect because changes to control flow could cause changes to the
call graph, so we've decided to change the way passes invalidate analysis.  In the new scheme, the lattice
is replaced with a list of traits that passes preserve or invalidate. The current traits are Calls and Branches.
Now, passes report which traits they preserve, which is the opposite of the previous implementation where
passes needed to report what they invalidate.

Node: I tried to limit the changes in this commit to mechanical changes to ease the review. I will cleanup some
of the code in a following commit.

Swift SVN r26449
2015-03-23 21:18:58 +00:00
Andrew Trick
bd882d4ee1 SILCombine: generalize init_enum_data_addr canonicalization.
Make the SILCombine logic aware of memory behavior.

This fixes performance issues exposed by copy forwarding.
By itself, there is one confirmed speedup:
StdlibSort +12.3%

Swift SVN r23607
2014-12-02 01:07:46 +00:00
Ben Langmuir
e9e1666ab0 Update for upstream LLVM changes
* removal of StringMap's GetOrCreateValue
* SmallSet::insert now returns a pair like std::set

Swift SVN r23435
2014-11-19 16:49:30 +00:00
Michael Gottesman
6e485700c6 Move SimplifyInstruction into SILAnalysis since it is an analysis.
This will hopefully make it clearer as we onboard people that
SimplifyInstruction should not add instructions to the IR by making it clearer
that it is an analysis, not a pass.

Swift SVN r21752
2014-09-05 22:56:26 +00:00
Michael Gottesman
6d5094bacb [sil-combine] Split SILCombine into SILCombine.cpp, SILCombiner.h, and SILCombinerVisitors.cpp.
rdar://18164766

Swift SVN r21541
2014-08-28 21:27:54 +00:00
Michael Gottesman
75d8e4d748 [sil-combine] Move isZeroValue out of SILCombine and into ValueTracking.
Swift SVN r21540
2014-08-28 21:27:53 +00:00
Roman Levenstein
97014172b7 [sil-combine] String literal concatenation optimization. Constant-fold concatenation of string literals known at compile-time.
Addresses rdar://17033696.

Swift SVN r21526
2014-08-28 11:33:21 +00:00
Michael Gottesman
6e3c9e64b7 Revert "[sil-combine] Strip off RC identity preserving ops from retain value and release value."
This reverts commit r21480. There are other parts of the compiler which pattern
match on retain_value, release_value but don't strip off RC identity preserving
ops. Since the values no longer naively match up, the optimizations fail.

Swift SVN r21503
2014-08-28 00:51:55 +00:00
Michael Gottesman
03ce13f230 [sil-combine] Strip off RC identity preserving ops from retain value and release value.
This is always good to do since it makes it clearer what the ARC optimizer is
doing. I have not decided how I want to strip off arguments from strong_retain,
strong_release since I may necessarily convert the strong_{retain,release} back
to a {retain,release}_value.

Swift SVN r21480
2014-08-27 09:29:58 +00:00
Michael Gottesman
e99588f8a1 [sil-combine] Simplify and generalize switch_enum_addr of loadable type promotion to switch_enum.
Previously we were attempting to look for a store before the switch_enum_addr
and were performing some sort of simplistic store forwarding. This is
unnecessary and causes us to miss potential opportunities to promote
switch_enum_addr of loadable types.

With this patch we always promote switch_enum_addr of loadable types to a load +
switch_enum. This ensures we canonicalize all switch_enum_addr of loadable types
to switch_enum and leaves the load-store forwarding issue to mem2reg and load
store forwarding.

Swift SVN r21479
2014-08-27 09:29:56 +00:00
Arnold Schwaighofer
c034fd088b Remove dead code introduced by 21470
Swift SVN r21471
2014-08-27 00:20:13 +00:00
Arnold Schwaighofer
ef3adb2186 Dont' trivial bitcast between differently sized structs.
Really we want to only cast between equally sized types. SIL does not really
have a concept of types of the same size so this solution is a workaround
(temporary) for acutal code in the standard library which casts a pointer of an
UTF16 type to a pointer of an UTF8 type.

rdar://18118602

Swift SVN r21470
2014-08-27 00:05:04 +00:00
Arnold Schwaighofer
16e41ada77 Make Builtin.canBeClass return a tri-state
Replace the true/maybe state that Builtin.canBeClass was returning by a
tri-state (yes, no, maybe) allowing the optimizer to use the definite no
answer.  This removes the need of the sizeof check that we had in
isClassOrObjCExistential. It also removes the need to CSE this function since
in most cases we will be able to instantiate canBeClass to yes or no (vs maybe)
at compile time.

benchmark``````````````,``baserun0``,``optrun2``,``delta,``speedup
ClassArrayGetter```````,``988.00````,``337.00```,``644.00``,````````191.7%
DeltaBlue``````````````,``2429.00```,``1927.00``,``460.00``,````````23.9%
Dictionary`````````````,``1374.00```,``1231.00``,``129.00``,````````10.9%
Havlak`````````````````,``1079.00```,``911.00```,``124.00``,````````13.7%
Rectangles`````````````,``924.00````,``541.00```,``379.00``,````````70.1%

radar://16823238

Swift SVN r21331
2014-08-21 00:55:40 +00:00
Michael Gottesman
40760685c3 Revert "Revert "[sil-combine] ({struct_extract,unchecked_enum_data} (unchecked_ref_bit_cast)) -> unchecked_ref_bit_cast when the two instructions are just layout compatible bit casts.""
This reapplies commit r21164 by reverting r21169.

Swift SVN r21224
2014-08-15 03:30:37 +00:00
Dmitri Hrybenko
0441ddb4b3 Revert "[sil-combine] ({struct_extract,unchecked_enum_data} (unchecked_ref_bit_cast)) -> unchecked_ref_bit_cast when the two instructions are just layout compatible bit casts."
This reverts commit r21164.

This commit broke building precommit benchmarks.

Swift SVN r21169
2014-08-13 08:31:29 +00:00
Michael Gottesman
62110526b8 [sil-combine] ({struct_extract,unchecked_enum_data} (unchecked_ref_bit_cast)) -> unchecked_ref_bit_cast when the two instructions are just layout compatible bit casts.
Also added identity cast simplifications for unchecked_*_bit_cast to
SimplifyInstruction.

<rdar://problem/17965742>


Swift SVN r21164
2014-08-13 01:25:55 +00:00
Michael Gottesman
6460f97c87 [sil-combine] Always convert unchecked_addr_cast -> unchecked_*_bit_cast instead of to typed GEPs.
This is easier for the ARC optimizer to stomach.

<rdar://problem/17961131>


Swift SVN r21163
2014-08-13 01:24:08 +00:00
Pete Cooper
8322876162 Sink retains down in the current BB if they cannot be sunk to a successor.
We currently only sink retains to successor BBs.

However, if a retain can't be moved to a successor, we may still want to move it as late in the current BB as
possible.  For example, we want retains after called to _swift_isUniquelyReferenced and not before it.

Here are performance numbers for -O.  Note that i'm about to commit improvements to enum_is_tag which fixes the
regressions here.

Ackermann``````,``````````1413.00```,`````````````````1566.00```,``````````153.00``,`````````````````-9.8%
ArrayLiteral```,``````````834.00````,`````````````````859.00````,``````````25.00```,`````````````````-2.9%
Ary````````````,``````````1110.00```,`````````````````1121.00```,``````````11.00```,`````````````````-1.0%
Ary2```````````,``````````1092.00```,`````````````````1105.00```,``````````13.00```,`````````````````-1.2%
Ary3```````````,``````````1164.00```,`````````````````1193.00```,``````````29.00```,`````````````````-2.4%
DeltaBlue``````,``````````2195.00```,`````````````````2204.00```,``````````9.00````,`````````````````-0.4%
Dictionary`````,``````````502.00````,`````````````````512.00````,``````````10.00```,`````````````````-2.0%
Dictionary2````,``````````884.00````,`````````````````895.00````,``````````11.00```,`````````````````-1.2%
Dictionary3````,``````````859.00````,`````````````````870.00````,``````````11.00```,`````````````````-1.3%
EditDistance```,``````````1344.00```,`````````````````1344.00```,``````````0.00````,`````````````````0.0%
Fibonacci``````,``````````1382.00```,`````````````````1309.00```,``````````73.00```,`````````````````5.6%
ForLoops```````,``````````1318.00```,`````````````````1324.00```,``````````6.00````,`````````````````-0.5%
Forest`````````,``````````757.00````,`````````````````775.00````,``````````18.00```,`````````````````-2.3%
GlobalClass````,``````````1617.00```,`````````````````1620.00```,``````````3.00````,`````````````````-0.2%
Hash```````````,``````````660.00````,`````````````````718.00````,``````````58.00```,`````````````````-8.1%
HeapSort```````,``````````1256.00```,`````````````````1292.00```,``````````36.00```,`````````````````-2.8%
Histogram``````,``````````313.00````,`````````````````296.00````,``````````17.00```,`````````````````5.7%
InsertionSort``,``````````1223.00```,`````````````````1351.00```,``````````128.00``,`````````````````-9.5%
Life```````````,``````````69.00`````,`````````````````70.00`````,``````````1.00````,`````````````````-1.4%
LinkedList`````,``````````1516.00```,`````````````````1529.00```,``````````13.00```,`````````````````-0.9%
MatMul`````````,``````````224.00````,`````````````````239.00````,``````````15.00```,`````````````````-6.3%
Memset`````````,``````````43.00`````,`````````````````43.00`````,``````````0.00````,`````````````````0.0%
MonteCarloE````,``````````905.00````,`````````````````900.00````,``````````5.00````,`````````````````0.6%
MonteCarloPi```,``````````643.00````,`````````````````654.00````,``````````11.00```,`````````````````-1.7%
NBody``````````,``````````42.00`````,`````````````````41.00`````,``````````1.00````,`````````````````2.4%
NestedLoop`````,``````````931.00````,`````````````````876.00````,``````````55.00```,`````````````````6.3%
NopDeinit``````,``````````1119.00```,`````````````````1117.00```,``````````2.00````,`````````````````0.2%
Phonebook``````,``````````1424.00```,`````````````````1395.00```,``````````29.00```,`````````````````2.1%
PrimeNum```````,``````````214.00````,`````````````````219.00````,``````````5.00````,`````````````````-2.3%
Prims``````````,``````````1182.00```,`````````````````1198.00```,``````````16.00```,`````````````````-1.3%
QuickSort``````,``````````1196.00```,`````````````````1166.00```,``````````30.00```,`````````````````2.6%
R17315246``````,``````````798.00````,`````````````````798.00````,``````````0.00````,`````````````````0.0%
RC4````````````,``````````25.00`````,`````````````````25.00`````,``````````0.00````,`````````````````0.0%
RIPEMD`````````,``````````724.00````,`````````````````746.00````,``````````22.00```,`````````````````-2.9%
Random`````````,``````````960.00````,`````````````````963.00````,``````````3.00````,`````````````````-0.3%
Rectangles`````,``````````836.00````,`````````````````849.00````,``````````13.00```,`````````````````-1.5%
Richards```````,``````````220.00````,`````````````````225.00````,``````````5.00````,`````````````````-2.2%
SelectionSort``,``````````931.00````,`````````````````932.00````,``````````1.00````,`````````````````-0.1%
SmallPT````````,``````````1014.00```,`````````````````1039.00```,``````````25.00```,`````````````````-2.4%
StdlibSort`````,``````````1617.00```,`````````````````1602.00```,``````````15.00```,`````````````````0.9%
StrCat`````````,``````````1061.00```,`````````````````1121.00```,``````````60.00```,`````````````````-5.4%
StrSplitter````,``````````1607.00```,`````````````````1533.00```,``````````74.00```,`````````````````4.8%
StrToInt```````,``````````616.00````,`````````````````612.00````,``````````4.00````,`````````````````0.7%
StringBuilder``,``````````701.00````,`````````````````682.00````,``````````19.00```,`````````````````2.8%
StringWalk`````,``````````1165.00```,`````````````````1182.00```,``````````17.00```,`````````````````-1.4%
Totals`````````,``````````42392.00``,`````````````````42778.00``,``````````386.00``,`````````````````-0.9%
TwoSum`````````,``````````581.00````,`````````````````564.00````,``````````17.00```,`````````````````3.0%
Walsh``````````,``````````105.00````,`````````````````104.00````,``````````1.00````,`````````````````1.0%

Swift SVN r21133
2014-08-11 17:48:37 +00:00
Nadav Rotem
4cf6181785 Replace @semantics("readonly") with @effects(readonly).
Swift SVN r21088
2014-08-07 07:22:18 +00:00
Arnold Schwaighofer
80c2dc7ebe SILCombine: Don't optimize away stores to alloc_array if the alloc_array itself is stored
Swift SVN r20893
2014-08-01 16:23:33 +00:00
Nadav Rotem
f41361ca29 Don't release address types when deleting apply instructions.
Swift SVN r20889
2014-08-01 06:21:10 +00:00
Nadav Rotem
c39bcf7736 Fix an obvious typo that snuck in by mistake last commit.
Swift SVN r20888
2014-08-01 06:05:55 +00:00
Nadav Rotem
28022fcc04 Add a silcombine optimization to remove alloc_array instructions that are only written into.
Swift SVN r20887
2014-08-01 05:39:11 +00:00
Nadav Rotem
1408b8c03d When deleting Apply instructions make sure to release consumed arguments to prevent a leak.
Swift SVN r20882
2014-08-01 04:19:26 +00:00
Mark Lacey
2a4a89264b Fix <rdar://problem/17817395> optimize (enum_is_tag (enum v, tag) tag)
I noticed this while looking at something completely unrelated.

Swift SVN r20583
2014-07-26 00:31:59 +00:00
Arnold Schwaighofer
c028336cc1 Also combine strideof_nonzero to index_addr
And we vectorize code again :).

Swift SVN r20523
2014-07-24 22:21:51 +00:00
Joe Groff
623aba1786 Encapsulate Substitution's state.
Expose Substitution's archetype, replacement, and conformances only through getters so we can actually assert invariants about them. To start, require  replacement types to be materializable in order to catch cases where the type-checker tries to bind type variables to lvalue or inout types, and require the conformance array to match the number of protocol conformances required by the archetype. This exposes some latent bugs in the test suite I've marked as failures for now:

- test/Constraints/overload.swift was quietly suffering from <rdar://problem/17507421>, but we didn't notice because we never tried to codegen it.
- test/SIL/Parser/array_roundtrip.swift doesn't correctly roundtrip substitutions, which I filed as <rdar://problem/17781140>.

Swift SVN r20418
2014-07-23 18:00:38 +00:00
Joe Groff
45eec9a2e9 Remove 'interface' from the method names of SILFunctionType.
SILFunctionTypes are always interface types now. NFC.

Swift SVN r19952
2014-07-14 22:03:46 +00:00
Nadav Rotem
e45fb7ccc1 Allocation instructions return non-zero pointers.
Swift SVN r19879
2014-07-12 04:54:52 +00:00
Nadav Rotem
bbfd7ca9ef Teach the code that searches for values that can be zero about
additional instructions. This helps to remove a comparison of
a pointer to zero inside the Array implementation.

This patch improves both NBody and SmallPt by about 30%.



Swift SVN r19878
2014-07-12 04:39:12 +00:00
Nadav Rotem
09be2f362c Optimize the comparison builtin. This peephole allows us to remove the control flow in the string literal constructor.
I did not notice performance changes in our string benchmarks.



Swift SVN r19861
2014-07-11 22:59:40 +00:00
Michael Gottesman
0a15d018cc Rely on the stdlib's runtime assert and undefined behavior to promote 3/4 remaining unchecked_addr_cast types to object bit casts.
This commit enables support in the optimizer for promoting the following
unchecked_addr_cast kinds to object bit casts:

1. (Trivial => Trivial) yields a trivial bit cast.
2. (Non-Trivial => Trivial) yields a trivial bit cast.
3. (Non-Trivial => Non-Trivial) yields a ref bit cast.

We do not promote conversions in between trivial and non-trivial types
since a trivial bit cast must have a trivial output and if we allowed
for ref bit casts in between the two, we would be breaking the rule that
ref bit casts do not change the reference semantics of its input, output
types. Technically, we could lower trivial => trivial as a ref cast and
then simplify later but that is unnecessary currently.

<rdar://problem/17373087>

Swift SVN r19784
2014-07-10 05:24:00 +00:00
Pete Cooper
ac6c8c35af Peephole (apply (thin_to_thick_function f)) to (apply f). <rdar://problem/16860102>
Swift SVN r19743
2014-07-09 21:14:18 +00:00
Andrew Trick
3e7cea232b Fix SILType::getMetatypeInstanceType by lowering the type.
Fixes <rdar://problem/17579272> SILCombine assert: ty->isLegalSILType() constructing SILType with type that should have been eliminated by SIL lowering.

Fix suggested by MikeG.

Swift SVN r19701
2014-07-08 21:43:17 +00:00
Nadav Rotem
0eedfdc53e Rename pure->readonly.
Swift SVN r19676
2014-07-08 03:39:48 +00:00
Nadav Rotem
3f476f2be2 Add a small peephole optimization to remove pure functions with no users.
For example, we want to remove this code:

@semantics("pure") func pure_func() -> Foo {
  return Foo()
}


func baz() {
  let unused = pure_func() // Remove this code.
}



Swift SVN r19656
2014-07-08 00:42:43 +00:00
Mark Lacey
e348535c3c Small clean-up - use hasArchetype() directly in the optimizer.
In a few places we were calling into a function that just returned
T->hasArchetype(). This just changes those places to test it directly.

Swift SVN r19427
2014-07-01 20:02:21 +00:00
Michael Gottesman
6f4caa20fb Combine together 2x unchecked_ref_bit_cast, 2x unchecked_trivial_bit_cast, and (trivial_bit_cast (ref_bit_cast x)) -> (trivial_bit_cast)
<rdar://problem/17493843>

Swift SVN r19324
2014-06-28 05:59:13 +00:00
Michael Gottesman
f66bbd9417 Revert "Revert "Expand the amount of unchecked_addr_casts that we can handle.""
This reverts commit r19279 and adds in the necessary fixes.

Swift SVN r19295
2014-06-27 16:49:08 +00:00
Michael Gottesman
cf896976a0 Revert "Expand the amount of unchecked_addr_casts that we can handle."
This reverts commit r19272 to unblock the submission. I am going to
cherry-pick this later.

Swift SVN r19279
2014-06-27 00:02:05 +00:00
Michael Gottesman
256f1e5515 Expand the amount of unchecked_addr_casts that we can handle.
This is tested by an assertion in IRGen. After Beta3, this code is going
to go away and be replaced by just always promoting the cast. Then the
IRGen assertion will be replaced by propagating undef. The assertion in
the stdlib will still fire in that case since the assertion is based on
the tops not the given value implying that we will not lose any
correctness.

Swift SVN r19272
2014-06-26 23:07:52 +00:00
Michael Gottesman
da93018c16 [sil-combine] Canonicalize (multiple-loads (unchecked_addr_cast x X->Y)) -> (multiple-casts (load x) X->Y) where X and Y are layout compatible.
Swift SVN r19131
2014-06-24 08:02:28 +00:00
Michael Gottesman
2eb7c03063 [sil-combine] Invalidating an enum address is never necessary. Thus it is ok if we just remove the unchecked_take_enum_data once it is dead.
Swift SVN r19127
2014-06-24 05:31:02 +00:00
Michael Gottesman
2749f9f1be [sil-combine] Refactor the SILCombine unchecked_addr_cast -> Typed GEP code to make it clearer what is going on. NFC.
Swift SVN r19126
2014-06-24 05:31:00 +00:00
Pete Cooper
7a2f007dee Peephole (condbr(xor(x, 1)), t, f) -> (cond_br(x), f, t). Includes a test case for PatternMatch behaving differently between SILValue and other matches on Apply arguments. <rdar://problem/17332412>
Swift SVN r19001
2014-06-19 15:59:59 +00:00
Michael Gottesman
735a23a0a3 [sil-combine] Rewrite unchecked_addr_cast to access layout compatible first element fields via typed GEPS.
A first element field of a nominal type is either the first element of a
struct or the first payload of an enum. We currently allow the stdlib to
rappel into struct heirarchies using reinterpretCast. This patch teaches
the optimizer how to rewrite such unchecked_addr_cast into
unchecked_enum_data_addr and struct_element_addr instructions. Then
Mem2Reg and Load Store Forwarding will remove the allocation generated
by such uses of reinterpret cast.

<rdar://problem/16703656>

Swift SVN r18977
2014-06-18 09:34:05 +00:00
Michael Gottesman
b97ce43f35 [sil-combine] Make sure when specializing a function to specialize its return type appropriately.
Swift SVN r18886
2014-06-14 03:58:13 +00:00
Michael Gottesman
90d1b06ef6 [sil-combine] Simplify (apply (partial_apply f)) -> (apply f) when the
partial apply is a partial apply of a generic function with a
non-generic substitution list.

rdar://17303157

Swift SVN r18885
2014-06-14 01:16:55 +00:00
Manman Ren
9a7cfe6a69 Optimize sub(ptrtoint(index_raw_pointer(v, x)), ptrtoint(v)) -> x.
Also Optimize sub(x - x) -> 0.


Swift SVN r18879
2014-06-13 22:15:16 +00:00
Manman Ren
7610c792d6 [SILCombine] optimize unconditional_checked_cast in -Ofast builds.
Reduce unconditional_checked_cast to unchecked_ref_cast or unchecked_addr_cast
when runtime checks are disabled.

rdar://16708688


Swift SVN r18601
2014-05-23 19:42:47 +00:00