Commit Graph

172 Commits

Author SHA1 Message Date
Roman Levenstein
c199905a2f Second part of the cast folding and type-casts re-factoring.
This patch does the following:
- Moves the logic for handling special-case of converting to/from AnyObject.Protocol (and existential.Protocol in general) into DynamicCasts, where all other cases are handled already.
- Moves the peephole which was folding checked_cast_br into an unchecked cast and branch from sil-combine into sil-simplify-cfg, because it is a better place for it, since this peephole affects the CFG. The corresponding test is also moved from sil_combine.sil into simplify_cfg.sil.
- Adds a few checked_cast_br peepholes to sil-combine.   They try to simplify checked_cond_br instructions using existential metatypes by propagating a concrete type whenever it can be determined statically.
- Adds a new test with a lot of test-cases that make sure we are really folding many type-checks at compile-time now.

Swift SVN r25504
2015-02-24 17:31:08 +00:00
Michael Gottesman
6f8b468cb6 Change switches and selects to use NullablePtr<EnumElementDecl> instead of EnumElementDecl * when their API explicitly requires the user to check for nullptr. NFC.
This is just good to do and hopefully will help prevent people from forgetting
to check in the future by annotating the API explicitly as returning a
potentially nullptr.

Swift SVN r25364
2015-02-18 02:11:57 +00:00
Dmitri Hrybenko
61286f0260 Fix warnings produced by a newer version of Clang
Swift SVN r25257
2015-02-12 23:50:47 +00:00
Erik Eckstein
7aa11c82f1 [SimplifyCFG] Small improvement for switch_enum jump threading.
This will optimize a pattern which appears when iterating over a CollectionOfOne.
Only minor changes in the benchmarks.



Swift SVN r25095
2015-02-09 11:24:24 +00:00
Roman Levenstein
d62a86eb00 [sil-simplify-cfg] Fix a few bugs in conversion of switch_enum into select_enum.
New test-case was added to avoid this problem in the future.

rdar://19537435

Swift SVN r24827
2015-01-29 22:53:35 +00:00
Mark Lacey
6170bbb0ed Minor code clean-up.
Primarily replacing getParent()->getParent() with getFunction().

Swift SVN r24596
2015-01-21 06:35:13 +00:00
Roman Levenstein
e056663e6f Remove debug dump that accidentally got committed ;-) NFC.
Swift SVN r24252
2015-01-08 00:30:09 +00:00
Roman Levenstein
9970d78e77 [sil-simplify-cfg] Improve the trampoline elimination code.
Replace cond branches to a trampoline BB without BBargs, by direct cond branches to the destination BB of the trampoline. Apply this transformation only if it does not introduce new critical edges as this may destroy the canonical form of the CFG which is required/assumed by some other optimizations (e.g. LoopRotate, ABCOpts, etc).

rdar://16450429

Swift SVN r24236
2015-01-07 03:48:24 +00:00
Andrew Trick
d84feff62b Add a changeBranchTarget helper.
I refactored, generalized, and cleaned up an existing helper.

I also removed hard-coded assumptions about successor indices.
There's no point giving CondBranch a true/false API if we don't respect it.

Swift SVN r24001
2014-12-18 01:26:45 +00:00
Erik Eckstein
3b9805b246 Disallow cond_br with identical destinations.
It avoids generation of llvm phi nodes with identical predecessors and differing values.
This change replaces my previous fix of this problem in r23580, where I handled it in IRGen.

There were some discussions about it with the conclusion that it's better to just disallow such cond_br instructions in SIL.
It makes the life easier for some SIL optimizations which can't deal with cond_br with identical destinations.

The original radar is <rdar://problem/18568272> Swift compiler fails with "PHI node has multiple entries for the same basic block with different incoming values!"



Swift SVN r23861
2014-12-11 15:03:49 +00:00
Erik Eckstein
e75334036c Refactor in SimplifyCFG: isTrampolineJump -> getTrampolineDest
NFC



Swift SVN r23860
2014-12-11 12:42:37 +00:00
Roman Levenstein
a4a28d849e Add a date to the comment, so that it is easy to detect later how stale it is.
Swift SVN r23594
2014-12-01 18:34:11 +00:00
Roman Levenstein
b192a12db5 Add a comment about BB inline-costs that occur in practice when performing the jump-threading for checked_cast_br.
Swift SVN r23593
2014-12-01 18:06:33 +00:00
Erik Eckstein
88c674161c refactoring: Add operator for comparing OperandValueArrayRefs.
And use it in SimplifyCFG. NFC



Swift SVN r23581
2014-11-25 09:43:35 +00:00
Arnold Schwaighofer
11802de146 SimplifyCFG: Fix a memory invalidation issue
DFSStack.push_back will invalidate the referenced memory if it relocates the
vector. Just increment the iterator earlier.

rdar://19070782

Swift SVN r23570
2014-11-24 14:39:21 +00:00
Michael Gottesman
1afc987739 Refactor the SILArgument API on SILBasicBlock so we can insert bb arguments anywhere in the argument list. Also clean up the API names so that they all match.
Swift SVN r23543
2014-11-22 00:24:40 +00:00
Roman Levenstein
ab7b1c26ca Move stripClassCasts into SILValue. NFC.
Swift SVN r23539
2014-11-21 22:21:52 +00:00
Roman Levenstein
dda599f58e These lines should not have been committed ;-)
Swift SVN r23538
2014-11-21 22:21:51 +00:00
Roman Levenstein
cb2d39b268 This is a re-factored, more modular implementation of jump-threading for checked_cast_br [exact] based on Michael's comments.
The implementation is inside a dedicated class now. Huge methods are split into smaller ones that are much easier for reviewing and understanding.
The whole thing could be moved out of SimplifyCFG into a dedicated file eventually. But currently it is not done as it still has too many dependencies on the internals of SimplifyCFG.

And, BTW, this dominator-based implementation of checked_cast_br jump-threading gives a 5x boost on the relevant PolymorphicCalls benchmark.

Swift SVN r23536
2014-11-21 22:01:31 +00:00
Roman Levenstein
c3f0ccf3d2 Remove the monolithic implementation of jump-threading for checked_cast_br.
It was using a few huge methods which will be replaced by a more modular implementation in the next commit.

Swift SVN r23535
2014-11-21 22:01:31 +00:00
Roman Levenstein
1eae9eb4bd Minor stylistic changes based on Michael's review. NFC.
Swift SVN r23534
2014-11-21 22:01:30 +00:00
Roman Levenstein
08e79adae2 Remove debug output that was only used during development.
Swift SVN r23519
2014-11-21 17:24:09 +00:00
Roman Levenstein
fcadd24381 [sil-simplify-cfg] Implementation of a dominator-based jump-threading for checked_cast_br [exact]
it is very beneficial for code that:
 - references the same object multiple times (e.g. x.f1() + x.f2())
 - and for method invocation chaining (e.g. x.f3().f4().f5())

rdar://18914224

Swift SVN r23515
2014-11-21 16:47:58 +00:00
Roman Levenstein
bf594a31f6 [sil-simplify-cfg] Add a few helper functions to be used by the implementation of dominator-based jump-threading for checked_cast_br [exact]
Swift SVN r23514
2014-11-21 16:47:58 +00:00
Roman Levenstein
49d1edf9f7 [sil-simplify-cfg] Remove the simple checked_cast_br [exact] jump-threading implementation as it will be replaced by a dominator-based implementation.
Swift SVN r23513
2014-11-21 16:47:56 +00:00
Roman Levenstein
8951649a89 [sil-simplify-cfg] Continue re-factoring. Move a few functions closer to the beginning of a file so that they can be used by all functions below them. NFC.
Swift SVN r23456
2014-11-20 00:00:23 +00:00
Roman Levenstein
705d239a52 [sil-simplify-cfg] Small re-factoring as a preparation for the upcoming jump-threading patch. Introduces a new BasicBlockCloner class and moves a common logic of cloners which are used by jump-threading into a new BaseThreadingCloner base class. NFC.
Swift SVN r23455
2014-11-20 00:00:23 +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
Erik Eckstein
07273ff53b SimplifyCFG: create select_value instructions.
Currently this is only done for integer input values and enum output values.
The purpose is to simplify enum initializations from integer raw values.

Together with an llvm patch, which I'm currently commiting, the Enum.init(rawValue: Int) will be compiled
to a range-check + int-cast.



Swift SVN r23320
2014-11-14 14:56:59 +00:00
Erik Eckstein
d44da6ecf0 Enable jump threading to combine two enum instructions.
This is needed to simplify enum construction from integer raw values.



Swift SVN r23302
2014-11-13 10:25:12 +00:00
Roman Levenstein
3910c25da1 Minor re-factoring: Move the logic to decide if a given instruction can be (easily) duplicated into SILInstruction. So far it was mainly used by jump-threading related optimizations. But it could be generally useful, or at least generally important, to optimizations that want to duplicate code.
Swift SVN r23213
2014-11-10 23:15:07 +00:00
Roman Levenstein
c54686f59d [sil-simplify-cfg] When performing a jump-threading, check first if a given BB can be cloned, because not everything can be cloned. For example alloc_stack and dealloc_stack instructions cannot be cloned.
Swift SVN r23204
2014-11-10 18:26:12 +00:00
Roman Levenstein
704d6f8dbd [sil-simplify-cfg] Implement jump-threading for blocks ending with checked_cast_br [exact] instructions. This improves code typically produced by the devirtualizer when it performs a speculative devirtualization. The generated code for simple expressions like o.x + o.y or o.f1() * o.f2() gets completely jump threaded in many cases.
rdar://18463290

Swift SVN r23163
2014-11-07 21:38:40 +00:00
Arnold Schwaighofer
8f5df015f6 Don't jumpthread through loop headers
This can create irreducible loops.

rdar://18863041

Swift SVN r23108
2014-11-05 02:24:20 +00:00
Roman Levenstein
c960a38d16 [sil-simplify-cfg] Generalize simplifySwitchEnumToSelectEnum to simplify arbitrary simple switch_enum diamonds into select_enums. It supports now not only boolean-like enums, but any simple enums with integer case tags. This version of the patch fixes dominator tree related problems that were found in the initial version of the patch.
rdar://18604219

Swift SVN r23046
2014-11-01 00:22:20 +00:00
Roman Levenstein
c8d180e660 Generalize the switch_int instruction into switch_value instruction, which may switch on arguments of builtin integer types or function types. The later is required for implementing a more efficient speculative devirtualizaiton implementation. Implement lowering of switch_value into LLVM code. In case of integer operands, it reuses LLVM's switch optimizations. Support for switching on function types is not yet bullet-proof and will be refined in the subsequent patches.
rdar://18508812

Swift SVN r23042
2014-10-31 22:55:56 +00:00
Erik Eckstein
743d6af4a4 Fix typo
Swift SVN r23004
2014-10-29 16:51:38 +00:00
Erik Eckstein
4df8f66b9a Replace the default-entry with a corresponding case-entry in switch_enum[_addr], if possible.
<rdar://problem/18709388> SimplifyCFG: Canonicalize default cases of covered switches to not use a default case




Swift SVN r23003
2014-10-29 14:56:34 +00:00
Adrian Prantl
c41b30299f Audit all SILPasses to ensure that new instructions are never created
without a valid SILDebugScope. An assertion in IRGenSIL prevents future
optimizations from regressing in this regard.
Introducing SILBuilderWithScope and SILBuilderwithPostprocess to ease the
transition.

This patch is large, but mostly mechanical.
<rdar://problem/18494573> Swift: Debugger is not stopping at the set breakpoint

Swift SVN r22978
2014-10-28 01:49:11 +00:00
Arnold Schwaighofer
7b790e1faa Revert "[sil-simplify-cfg] Generalize simplifySwitchEnumToSelectEnum to simplify arbitrary simple switch_enum diamonds into select_enums."
SimplifyCFG is not maintaining the dom/postdom tree. This code assumes it is and
so is using an potentially invalid domtree.

This caused SIL verifier errors compiling the perf test suite on my machine:

"SIL verification failed: instruction isn't dominated by its operand: Dominance->properlyDominates(valueI, I)
Verifying instruction:
...
->   %639 = select_enum %634 : $Optional<Int -> ()>, default %731 : $Builtin.Word
In function @_TF11DriverUtils14findTestsToRunFT_T_ basic block:"

This reverts commit r22843.

Swift SVN r22850
2014-10-20 20:51:58 +00:00
Roman Levenstein
108f424018 [sil-simplify-cfg] Generalize simplifySwitchEnumToSelectEnum to simplify arbitrary simple switch_enum diamonds into select_enums.
It supports now not only boolean-like enums, but any simple enums with integer case tags.

rdar://18604219

Swift SVN r22843
2014-10-20 17:30:33 +00:00
Arnold Schwaighofer
5f03e4241c SimplifyCFG: Don't early exit before splitting critical edges
This was causing verifier errors because we were not splitting non cond_br
critical edges.

rdar://18703358

Swift SVN r22842
2014-10-20 16:59:25 +00:00
Arnold Schwaighofer
329fa51d3f Reapply "Verifier: Cannonical SIL should have split critical edges for non cond_br
terminators"

This is an assumption that the SSAUpdater makes. Verify that we preserve this
property.

With changes to the test cases, SIL documentation and add a critical edge (non
cond_br only) splitting pass to the mandatory pipeline.

This reapplies commit 22775.

Swift SVN r22803
2014-10-16 21:16:24 +00:00
Dave Abrahams
1007acb683 Revert "Verifier: Cannonical SIL should have split critical edges for non cond_br"
This reverts commit r22775 as it breaks SILPasses/simplify_cfg_args.sil
and SILPasses/split_critical_edges.sil for me with:

Assertion failed: (!isCriticalEdgePred(BB.getTerminator(), Idx) && "non
cond_br critical edges not allowed"), function verify, file
/Users/dave/src/s/swift/lib/SIL/Verifier.cpp, line 2380.

Swift SVN r22780
2014-10-15 22:46:32 +00:00
Arnold Schwaighofer
f4a855590d Verifier: Cannonical SIL should have split critical edges for non cond_br
terminators

This is an assumption that the SSAUpdater makes. Verify that we preserve this
property.

Swift SVN r22775
2014-10-15 21:42:14 +00:00
Arnold Schwaighofer
2af40aee89 Revert "Revert "SimplifyCFG: Don't count 'free' instructions during jump-threading""
Reinstantiate rr22712. Now that updating SSA form is more robust we should not
run into troubles anymore.

Original commit message:

"Use the inliner's heuristic to decide which instructions are 'free' (constants,
etc).

We were not jumpthreading a block because it had four instruction in it - two of
them integer_literals."

rdar://18594600

Swift SVN r22761
2014-10-15 17:07:06 +00:00
Arnold Schwaighofer
088f777f35 SimplifyCFG: Split critical edges before updating SSA form
This way we are guaranteed to be able to build SSA form: there is guarantueed
to be a terminator branch that supports adding basic block arguments at every
join points. We were relying on the assumption that we will never have other
terminators at join points when we want to insert phi nodes. This turns out to
be a very fragile assumption in the presence of aggressive CFG simplification.

Swift SVN r22759
2014-10-15 17:07:05 +00:00
Arnold Schwaighofer
664d718ac1 SimplifyCFG: Add a pass to split all critical edges
Swift SVN r22758
2014-10-15 17:07:03 +00:00
Arnold Schwaighofer
a536a67d12 Revert "SimplifyCFG: Don't count 'free' instructions during jump-threading"
This reverts commit r22712.

The swift-stdlib-release-asserts-noopt breaks because we can't build SSA form
:(.

Swift SVN r22716
2014-10-14 01:18:51 +00:00
Arnold Schwaighofer
5bf693deff SimplifyCFG: Simplify CondBranchInst's condition
-O Performance:
I saw about 30% speedup on Life and maybe 8-10% on Histogram.

Swift SVN r22715
2014-10-14 01:18:50 +00:00