Commit Graph

127 Commits

Author SHA1 Message Date
Roman Levenstein
12b1e88d3f Fix a bug in a peephole for checked_cast_addr_br.
It was producing a checked_cast_add_br, which took a metatype inst as it's argument, which is simply wrong, as it is not an address type. SILVerifier was complaining about it. Now we produce an equivalent peephole, but with a correct instruction sequence, which makes SILVerifier happy.

Swift SVN r27375
2015-04-16 20:28:12 +00:00
Roman Levenstein
7fe82963f0 Bail early on casts involving unbound generic types. It is too early to optimize them.
Swift SVN r27374
2015-04-16 20:28:11 +00:00
Roman Levenstein
a8cf05343f Don't try to optimize non-addr types in simplifyCheckedCastAddrBranchInst.
Sometimes, during intermediate optimization steps, the operands of produced checked_cast_addr_br instructions do not actually have address types.
Don't try to optimize further in such cases. Let the optimizer clean-up those instructions.

Swift SVN r27373
2015-04-16 20:28:10 +00:00
Roman Levenstein
782a4120a4 If kind of the instruction was changed by the optimizeCheckedCast*BranchInst, bail.
This avoids segfaults due to de-referencing of a nullptr.

Swift SVN r27372
2015-04-16 20:28:10 +00:00
Roman Levenstein
185e96ce8c Clean-up of retain/release insertion during bridged casts optimizations.
I noticed under Instruments that the bridged casts opts produced a code that was leaking in some cases. Therefore I took the change to re-visit the code and to clean-up the logic for insertion of retains and releases.

Swift SVN r27371
2015-04-16 20:28:09 +00:00
Roman Levenstein
0b6b907a0a Do not produce an intermediate cast between the same ObjC types as it will get eliminated anyways.
When casts optimizations were lowering bridged casts from ObjC to Swift, they were producing internally an intermediate cast from an ObjC type into the expected ObjC type of a bridged cast, before converting this expected ObjC type into a Swift type. In many cases, this resulted in a useless intermediate cast where both types were the same and such a cast would be eliminated afterwards. E.g.

unconditional_checked_cast A to B  // where A is ObjC type and B is a Swift type

was lowered into:

unconditional_checked_cast A to B. _ObjectiveCType // Often useless as A is equal to B. _ObjectiveCType already.
unconditional_checked_cast B._ObjectiveCType to B

This small inefficiency is fixed now. This problem was no observable from outside the optimization pass, it just required additional processing, therefore there is no test-case.

Swift SVN r27370
2015-04-16 20:28:07 +00:00
Roman Levenstein
d1698ba1cb Use a _bridgeable suffix for newly introduced fast bridging functions. NFC.
Dave explained that stdlib usually uses the suffix notation in such cases. This change follows his advice.

Swift SVN r27177
2015-04-09 20:59:42 +00:00
Roman Levenstein
90e5f932c7 Simplify the lookup of a compiler-known library function. NFC.
Swift SVN r27157
2015-04-09 02:35:27 +00:00
Roman Levenstein
7c5717d3ad Address Joe's comments on my r27102 commit. NFC.
Swift SVN r27154
2015-04-09 01:51:59 +00:00
Roman Levenstein
2dd38eee0e [sil-combine] Teach the optimizer how to optimize bridged casts.
If a conformance to _BridgedToObjectiveC is statically known, generate a more efficient code by using the newly introduced library functions for bridging casts.
This covers the casts resulting from SIL optimizations.

Tests are included. I tried to cover most typical casts from ObjC types into Swift types and vice versa and to check that we always generate something more efficient than a checked_cast or unconditional_checked_cast. But probably even more tests should be written or generated by means of gyb files to make sure that nothing important is missing.

The plan is to make the bridged casts SIL optimization a guaranteed optimization. Once it is done, there is no need to lower the bridged casts in a special way inside Sema, because they all can be handled by the optimizer in a uniform way. This would apply to bridging of Error types too.

With this change, no run-time conformance checks are performed at run-time if conformances are statically known at compile-time.
As a result, the performance of rdar://19081345 is improved by about 15%. In the past, conformance checks in this test took 50% of its execution time, then after some improvements 15% and now it is 0%, as it should be.

Swift SVN r27102
2015-04-07 22:53:57 +00:00
Mark Lacey
fea3321f59 Update the generic specializer to maintain the call graph.
Swift SVN r27024
2015-04-05 19:27:40 +00:00
Mark Lacey
5adf76b4dd Missed one comment update.
Swift SVN r27019
2015-04-05 07:14:45 +00:00
Mark Lacey
8fad304e02 Fix a leak.
swift::clearBlockBody() in Local.cpp was popping instructions rather
than erasing them, resulting in leaking any instructions removed via
this function (which is reached via removeDeadBlock(), called throughout
SimplifyCFG).

Also tweak a couple comments and remove an assert that cannot fire.

Swift SVN r27018
2015-04-05 07:12:35 +00:00
Mark Lacey
730ef41385 Make devirtualizer clients remove old applies.
This makes it feasible for clients to maintain the call graph.

Swift SVN r26997
2015-04-05 02:27:57 +00:00
Roman Levenstein
55ea9ec0e3 [sil-combine] Minor clean-up of casts optimizations. NFC.
Add more checks and logic into emitSuccessfulIndirectUnconditionalCast and emitSuccessfulScalarUnconditionalCast, so that its clients in sil-combine can be simplified by avoiding looking into special cases.

Swift SVN r26885
2015-04-02 19:57:35 +00:00
Roman Levenstein
a7d0eb17ba [sil-combine] casts to/from existentials cannot be further simplified.
This should fix a bug in Adventure reported in rdar://20396204.

Swift SVN r26875
2015-04-02 17:20:23 +00:00
John McCall
6d8fff9c06 Parsing and basic structure of try_apply. Not yet properly
threaded into IRGen; tests to follow when that's done.

I made a preliminary effort to make the inliner do the
right thing with try_apply, but otherwise tried to avoid
touching the optimizer any more than was required by the
removal of ApplyInstBase.

Swift SVN r26747
2015-03-31 02:41:03 +00:00
Roman Levenstein
7011a5f4b4 [generic-specializer] Add support for specializing generic partial_apply instructions.
Use existing machinery of the generic specializer to produce generic specializations of closures referenced by partial_apply instructions. Thanks to the newly introduced ApplyInstBase class, the required changes in the generic specializer are very minimal.

rdar://19290942

Swift SVN r26582
2015-03-26 06:41:30 +00:00
Mark Lacey
1f23ff27bb Remove the transparent bit from apply instructions.
We no longer need or use it since we can always refer to the same bit on
the applied function when deciding whether to inline during mandatory
inlining.

Resolves rdar://problem/19478366.

Swift SVN r26534
2015-03-25 08:36:34 +00:00
Mark Lacey
2cec37e73b Use isTransparent() from the referenced function rather than the apply.
When creating a new apply, we were copying the transparent bit from the
old apply, as well as the referenced function from the old apply. We
should just use the transparent bit from that referenced function.

Swift SVN r26384
2015-03-21 02:06:15 +00:00
Roman Levenstein
f3ae8c1f52 Minor clean-up after my recent re-factoring of cast optimizations. NFC.
Swift SVN r26312
2015-03-19 20:03:44 +00:00
Roman Levenstein
848b9ab488 [sil-simplify-cfg] Fold checked_cast_br if a dynamic type of the operand is statically known from alloc_ref
Transforms :

%1 = alloc_ref $B
checked_cast_br [exact] %1 : $B to $B, bb1, bb2

into

br bb1(%1 : $B)

In case when source and destination types do no match, replace by a branch to the failure bb of the checked_cast_br.

Swift SVN r26275
2015-03-18 20:27:44 +00:00
Chris Lattner
4f708c049b fix const correctness and standardize on names for the successor list of
TerminatorInsts.  Now you can walk over the successor list of a terminator
and actually modify the SILSuccessor directly, allowing better CFG
transformations.  NFC.




Swift SVN r26140
2015-03-14 17:52:27 +00:00
Roman Levenstein
51ad2d964d Move the logic for type casts optimizations into one place.
This is mostly a re-factoring. It creates a new helper class CastOptimizer which contains all the logic for performing type casts optimizations. This logic is a copy of the current logic from sil-combine and simplify-cfg. Those passes will become clients of this new helper class.

Swift SVN r26122
2015-03-14 02:23:05 +00:00
Arnold Schwaighofer
aa24e126e4 ArraySemantic: Move to its own file in SILAnalysis
I want to use it in ARCAnalysis.

Swift SVN r25924
2015-03-10 15:20:20 +00:00
Arnold Schwaighofer
75710bf9c4 Array property functions were enabled by default
No need to conditionalize code anymore. NFC.

Swift SVN r25894
2015-03-09 22:03:06 +00:00
Arnold Schwaighofer
06f7c40c32 ArraySemanticsCall: Handle @guaranteed parameters
Also improve checking of parameter requirements.

This is needed for our move to +0 self.

radar://20039357

Swift SVN r25798
2015-03-06 03:20:47 +00:00
Arnold Schwaighofer
f7c01f2061 Nuke isReadNone function
We can now compute the same result with Inst.mayReadOrWriteMemory(). NFC.

Swift SVN r25743
2015-03-04 05:03:45 +00:00
Arnold Schwaighofer
951dc2875a Nuke isSideEffectFree
We can now compute the same result with Inst.mayHaveSideEffects(). NFC.

Swift SVN r25742
2015-03-04 05:03:44 +00:00
Arnold Schwaighofer
d6cd798de2 stdlib: Add array.props.isNative/needsElementTypeCheck calls
This enables high-level SIL optimizations to speculatively specialize loops into
'fast native swift array' loops vs standard array loops.

This commits adds two semantic calls to Arrays.swift.

   @semantics("array.props.isNative")
   func _getArrayPropertyIsNative() -> Bool

   @semantics("array.props.needsElementTypeCheck")
   func _getArrayPropertyNeedsElementTypeCheck() -> Bool

checkSubscript and getElement get extra arguments to pass in the result of above
function calls. This also allow us to communicate that an array that was made
mutable is always native. The array subscript getter is changed to use the
result of these new calls:

   public subscript(index: Int) -> Element {
     get {
       let isNative = _getArrayPropertyIsNative()
       let needsTypeCheck = _getArrayPropertyNeedsElementTypeCheck()
       _checkSubscript(index, isNative)
       return _getElement(index, isNative, needsTypeCheck)
     }
     mutableAddressWithPinnedNativeOwner {
       _makeMutableAndUniqueOrPinned()
       // When an array was made mutable we know it is a backed by a native
       // array buffer.
       _checkSubscript(index, true)
       return (_getElementAddress(index),
               Builtin.tryPin(Builtin.castToNativeObject(_buffer.owner)))
     }
   }

High-level SIL optimizations will now see a loop like this:

     func f(inout a : A[AClass]) {
         for i in 0..a.count {
           let b = a.props.isNative()
            .. += _getElement(a, i, b)
         }
     }

After proving that ‘a’ does not escape other than to known safe functions, the
optimizer hoists the array property calls out of the loop in a loop versioned
on the array property.

      func f(inout a : A[AClass]) {
        let b2 = a.props.isNative()
        if (!b2) {
          for i in 0..a.count {
             .. += _getElement(a, i, false)
          }
        } else {
          for i in 0..a.count {
            let b = a.props.isNative
            .. += _getElement(a, i, b)
          }
        }
      }

rdar://17955309

Swift SVN r25698
2015-03-03 00:45:59 +00:00
Arnold Schwaighofer
14ddfe1427 Add an API to ArraySemantic call to get the self argument as an operand.
Swift SVN r25619
2015-02-27 22:07:39 +00:00
Arnold Schwaighofer
20b1323da2 ArraySemanticsCall: Don't repeat assertions
SILInstruction::getSelfArgument already performs these.

Swift SVN r25616
2015-02-27 22:07:37 +00:00
Luqman Aden
e1c60464d3 Fold getCmpFunction function into helper method on SILBuilder.
Swift SVN r25416
2015-02-20 04:08:08 +00:00
Luqman Aden
858d8d99df SILCombine: Add cmp_*_T . (zext U->T x, zext U->T y) => cmp_*_T (x, y)
peephole for unsigned/equality comparisons.

Fixes <rdar://problem/19759124>

Swift SVN r25404
2015-02-20 00:39:33 +00:00
Michael Gottesman
669cdc537c Create an entrypoint tryToConcatenateStrings and hide the class StringConcatenationOptimizer in Local.cpp.
In every instance, we were just creating the StringConcatenationOptimizer and
then invoking optimize on it. This is a cleaner solution since the details of
how we perform the string concatenation are hidden in Local.cpp instead of being
in a header.

NFC.

Swift SVN r25341
2015-02-17 01:59:13 +00:00
Arnold Schwaighofer
fdb172e7a3 Only recognize array semantic functions with a self argument when they have a self argument
rdar://19808820

Swift SVN r25242
2015-02-12 17:42:11 +00:00
Michael Gottesman
3b7df81a58 Add in a note. NFC.
Swift SVN r25196
2015-02-11 21:52:28 +00:00
Mark Lacey
03cca73418 Fix naming convention.
Swift SVN r25179
2015-02-11 10:27:19 +00:00
Mark Lacey
c804c50c4d Add a simple utility for determining the lifetime of a value.
This refactors some code out of AllocBoxToStack that computes the
lifetime of a value in the strictess sense, limiting the lifetime to
that value and not anything derived from that value (whether by casting,
projection, etc.).

In the short term this will be used to fix a very rarely hit
optimization in AllocBoxToStack.

Longer term I will replace the other similar code in AllocBoxToStack to
use this instead.

Swift SVN r25176
2015-02-11 10:17:03 +00:00
Michael Gottesman
a5e757c6a2 Improve some comments. NFC.
Swift SVN r25134
2015-02-10 20:25:16 +00:00
Michael Gottesman
436f022867 [local] Add tryDeleteDeadClosures.
This utility attempts to delete dead closures with a set of
post-dominating releases using the infrastructure from
getFinalReleasesForValue.

It currently only will eliminate closures that only have retain, release
uses and a final post-dominating release set.

The reason why we need the final post-dominating release set is so that
we can release any captured variables at the points where we would have
deallocated the release. This is b/c captured variables are passed in at
+1 to partial apply.

Swift SVN r25050
2015-02-06 21:59:52 +00:00
Arnold Schwaighofer
86492b5fa2 ArraySemantics: Use the function type to check for the presence of a self argument
Swift SVN r25027
2015-02-06 01:23:39 +00:00
Arnold Schwaighofer
283a6bee39 Fix users of ArraySemantics::getSelf to correctly handle Array.init
Array.init does not have a self argument (it returns the newly created array
@owned). Passes using the ArraySemantic::getSelf() interface must handle it
specially.

rdar://19724033

Swift SVN r25013
2015-02-05 19:31:23 +00:00
Roman Levenstein
af31899fef Flag -debug-values-propagate-liveness should be always taken into account if provided.
Till now, -debug-values-propagate-liveness was only taken into account if the compiler was built with assertions. This led to some problems with bots, which were testing both assertions-enabled and assertions-disables versions of the compiler. It was agreed that we want this experimental flag to be always taken into account when it is provided on the command-line. The default value of this flag is "disabled".

rdar://19674999

Swift SVN r24977
2015-02-04 21:58:57 +00:00
Arnold Schwaighofer
1e3f67e9c7 global_init functions can have side-effects
We can not remove unused global initializer function calls. They might have
side-effects.

I measured no regressions at -O.

rdar://19464274

Swift SVN r24513
2015-01-19 19:17:14 +00:00
Michael Gottesman
897325b096 Codebase Gardening. NFC.
1. Eliminate unused variable warnings.
2. Change field names to match capitalization of the rest of the field names in the file.
3. Change method names to match rest of the file.
4. Change get,set method for a field to match the field type.

Swift SVN r24501
2015-01-19 00:34:07 +00:00
Adrian Prantl
95eec5a155 Fix Test for: DCE is dropping debug intrinsics, Was: DebugInfo/closure-multivalue test is failing
In order to not completely loose testcoverage while rdar://problem/18709125
is under investiagtion, add a special flag for enabling debug value
liveness.

Patch by Michael Gottesman!

<rdar://problem/19267059>

Swift SVN r24416
2015-01-14 18:37:39 +00:00
Erik Eckstein
31dd17a49f Add array.uninitialized semantics to ArrayCallKind, ArraySemanticsCall and the docs.
For completeness. NFC.



Swift SVN r23955
2014-12-16 13:28:18 +00:00
Arnold Schwaighofer
2f949056d6 SIL utils: Rename array.props.isCocoa to array.props.isNative
The upcoming stdlib patch will also be modified.

NFC

rdar://17955309

Swift SVN r23813
2014-12-09 21:58:16 +00:00
Mark Lacey
9d6e7cddc8 Reapply r23673, which was reverted in r23679.
It had exposed a problem with the MemBehavior on a couple SIL
instructions which resulted in code motion moving a retain across an
instruction that can release (fixed in r23722).

From the original commit message:

    Remove restriction on substituting existentials during mandatory inlining.

    Issues around this have now been resolved, so we should now support
    anything that Sema lets through.

    Fixes rdar://problem/17769717.

Swift SVN r23729
2014-12-05 05:47:42 +00:00