Commit Graph

172 Commits

Author SHA1 Message Date
Arnold Schwaighofer
1e06048408 SimplifyCFG: Don't count 'free' instructions during jump-threading
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 r22712
2014-10-13 22:47:43 +00:00
Mark Lacey
a0e161dcfb Resolve regression resulting from enum_is_tag to select_enum transition.
Unwrap the argument to select_enum to determine if it is the same
condition as is used in the terminator we are looking to optimize.

This particular code has become a bit unwieldy and needs larger
generalization and refactoring, but this restores the previous
optimization for now.

Resolves rdar://problem/18592542.

Swift SVN r22655
2014-10-10 05:31:12 +00:00
Joe Groff
a124a20368 Clean up debug dumps.
Swift SVN r22617
2014-10-09 05:03:45 +00:00
Joe Groff
9205bf64cf SIL: Remove enum_is_tag.
Swift SVN r22616
2014-10-09 05:03:43 +00:00
Joe Groff
c2fc9f58b8 SIL passes: Work with select_enum instead of enum_is_tag.
Simplify binary switches to select_enum instead of enum_is_tag, and make a first attempt at changing passes over to recognize limited forms of select_enum instead of enum_is_tag. There is one case in test/SILPasses/simplify_cfg.sil I wasn't able to figure out, and there are a lot more general passes we could define in terms of select_enum.

Swift SVN r22615
2014-10-09 03:49:31 +00:00
Michael Gottesman
269c372d97 [func-sig-opts] Extract out utility method removeDeadBlocks() from simplifyCFG so I canuse it in FunctionSignatureOpts.
I also extracted out an additional method clearBlockBody() from the body
of removeDeadBlocks() since I need that functionality exposed in
Function Signature Opts.

Swift SVN r22560
2014-10-07 00:53:58 +00:00
Joe Groff
a3126706da SIL: Eliminate the dead 'alloc_array' insn.
Swift SVN r22292
2014-09-26 02:28:10 +00:00
Michael Gottesman
44a9935076 [simplify-cfg] Refactor simplifyConditional and teach it how to simplify (cond_br (enum_is_tag)) when there is a dominating enum_is_tag on the same value.
This pattern occurs a lot in the following benchmark:

let     loopcount     = 1000000000
let     arraysize     = 100000
let     range          = 100..<200
let     prtrange     = (range.startIndex-2)..<(range.startIndex+2)
var vector          = [Double]( count: 5000, repeatedValue: 0.0 )
let     alfa          = 1.0
for var i=0;i<loopcount;i++ {
  vector[range] = vector[range].map() { $0 + alfa }
}

Swift SVN r22273
2014-09-25 01:24:52 +00:00
Michael Gottesman
561ab6980e [simplify-cfg] Canonicalize enum_is_tag on Optionals to always be on Some by swapping cond_br arguments if we have a .None check.
Swift SVN r22158
2014-09-21 10:56:30 +00:00
Arnold Schwaighofer
b1ae8073f6 SimplifyCFG: Only skip jump-threading for MethodInsts that get lowered to ObjC calls
Swift SVN r21788
2014-09-08 21:56:47 +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
Arnold Schwaighofer
78945346f6 SimplifyCFG: Don't jumpthread blocks that have method instructions
We don't support creating phi nodes with function value'd arguments.

rdar://18232001

Swift SVN r21743
2014-09-05 21:23:06 +00:00
Michael Gottesman
73434aa4fa Rename SwitchEnumInst::getBBCase => getUniqueCaseForDestination.
This matches the name for SwitchEnumInst::getCaseDestination() and includes the word
'unique' so that the name self documents.

I also removed a local function doing the same work in SimplifyCFG and changed
its user to use getUniqueCaseForDestination instead.

Swift SVN r21339
2014-08-21 04:06:19 +00:00
Michael Gottesman
0f7053bd01 Remove SILBasicBlock::getBBArgIndex(SILArgument *) in favor of SILArgument::getIndex().
The index is a property of the argument. There is no reason from a modeling
perspective to go through the BB to get it.

Swift SVN r21338
2014-08-21 04:06:19 +00:00
Mark Lacey
f5a9eabe10 Fix crash when a block has two incoming edges from the same terminator.
I noticed this while reviewing some code. It's possible to have a single
conditional branch where both the true and false destination are the
same block. The argument simplification code would crash in that case
due to removeArgumentFromTerminator assuming this couldn't happen, and
because the loop over blocks would attempt to visit the same terminator
twice, with the first visit invalidating the iterator over
SILSuccessors.

Swift SVN r21245
2014-08-16 00:10:15 +00:00
Arnold Schwaighofer
cd6b046e0c SimplifyCFG: We only change the CFG of the current function
No need to invalidate all functions.

Swift SVN r21215
2014-08-14 18:37:01 +00:00
Pete Cooper
27a88974c8 Optimize enum_is_tag branching to switch_enum when the enum is not of the given tag. Only currently supports case where the switch has 2 possible tags. rdar://problem/18010012
Swift SVN r21201
2014-08-14 16:49:42 +00:00
Mark Lacey
8cc0953ca8 Fix issue with switch_enum simplification.
I hit this building with another change that I have.

In cases where we have a switch_enum that is dominated by another
switch_enum, where the dominating switch_enum branches to a block with
no block argument, but the dominated switch_enum branches to a block
with a block argument, we need to synthesize the block argument to pass
by using unchecked_enum_data.

Swift SVN r21167
2014-08-13 02:02:29 +00:00
Arnold Schwaighofer
24d4d9d904 SimplifyCFG: Implement general jumpthreading using the SSAUpdater
simplifyInstruction conciously does not create constants (AFAIK) so we need to
run instruction combine after simplify-cfg to enable more cfg simplification
exposed by jumpthreading. We can revisit this decision in a follow-up commit if
necessary (I believe it to be useful for simplifyInstruction to be able to
create constants and for simplifycfg to use simplifyInstruction on the branch
condition).

O:
benchmark      ,  baserun0  ,  optrun0  ,  delta,  speedup
Fibonacci      ,  1473.00   ,  1317.00  ,  75.00   ,        5.8%
Histogram      ,  407.00    ,  390.00   ,  23.00   ,        6.1%
InsertionSort  ,  1273.00   ,  1200.00  ,  79.00   ,        6.7%
Life           ,  74.00     ,  69.00    ,  4.00    ,        5.8%
NestedLoop     ,  937.00    ,  883.00   ,  56.00   ,        6.4%
R17315246      ,  8.00      ,  801.00   ,  793.00  ,        -99.0%
SelectionSort  ,  1150.00   ,  921.00   ,  226.00  ,        24.5%

Ounchecked:
Histogram      ,  394.00    ,  342.00   ,  51.00   ,        15.0%
InsertionSort  ,  1122.00   ,  1024.00  ,  85.00   ,        8.3%
Life           ,  57.00     ,  44.00    ,  9.00    ,        20.9%
SelectionSort  ,  1312.00   ,  1060.00  ,  246.00  ,        23.3%

The R17315246 regression is somewhat bad. We dependent on a loop form such that
LLVM transforms the loop into an inner loop that just iterates from x to y and
this is unrolled (good version) the slow version has a loop with cond_fail
control flow and is not unrolled. The loop does nothing more than count up.
I have not being able to narrow this down further.

rdar://16821595

Swift SVN r21124
2014-08-09 00:05:30 +00:00
Pete Cooper
a7cebecf83 Handle simple argument SSA case in jump threading. This is only the case where the BB we are jumping to has a single predecessor which is our threaded block
Swift SVN r20798
2014-07-31 01:12:48 +00:00
Pete Cooper
a3677cb75f Simplify the case where an enum_is_tag inst dominates a switch_enum and so we can replace the switch with a branch
<rdar://problem/17849729>

2Sum           ,        632.00    ,          642.00    ,        10.00   ,          -1.6%
Ackermann      ,        1554.00   ,          1644.00   ,        90.00   ,          -5.5%
ArrayLiteral   ,        857.00    ,          868.00    ,        11.00   ,          -1.3%
Ary            ,        1290.00   ,          1304.00   ,        14.00   ,          -1.1%
Ary2           ,        1641.00   ,          1657.00   ,        16.00   ,          -1.0%
Ary3           ,        1378.00   ,          1416.00   ,        38.00   ,          -2.7%
DeltaBlue      ,        2429.00   ,          2504.00   ,        75.00   ,          -3.0%
Dictionary     ,        400.00    ,          407.00    ,        7.00    ,          -1.7%
Dictionary2    ,        889.00    ,          846.00    ,        43.00   ,          5.1%
Dictionary3    ,        787.00    ,          746.00    ,        41.00   ,          5.5%
EditDistance   ,        1337.00   ,          1359.00   ,        22.00   ,          -1.6%
Fibonacci      ,        1380.00   ,          1379.00   ,        1.00    ,          0.1%
ForLoops       ,        1408.00   ,          1248.00   ,        160.00  ,          12.8%
Forest         ,        1018.00   ,          976.00    ,        42.00   ,          4.3%
GlobalClass    ,        1598.00   ,          1600.00   ,        2.00    ,          -0.1%
Hash           ,        882.00    ,          950.00    ,        68.00   ,          -7.2%
HeapSort       ,        1234.00   ,          1220.00   ,        14.00   ,          1.1%
Histogram      ,        414.00    ,          415.00    ,        1.00    ,          -0.2%
Life           ,        64.00     ,          64.00     ,        0.00    ,          0.0%
LinkedList     ,        1523.00   ,          1523.00   ,        0.00    ,          0.0%
MatMul         ,        225.00    ,          226.00    ,        1.00    ,          -0.4%
Memset         ,        74.00     ,          42.00     ,        32.00   ,          76.2%
MonteCarloE    ,        923.00    ,          868.00    ,        55.00   ,          6.3%
MonteCarloPi   ,        655.00    ,          598.00    ,        57.00   ,          9.5%
NBody          ,        43.00     ,          42.00     ,        1.00    ,          2.4%
NestedLoop     ,        937.00    ,          936.00    ,        1.00    ,          0.1%
Phonebook      ,        1290.00   ,          1219.00   ,        71.00   ,          5.8%
PrimeNum       ,        336.00    ,          325.00    ,        11.00   ,          3.4%
Prims          ,        1248.00   ,          1258.00   ,        10.00   ,          -0.8%
QuickSort      ,        86.00     ,          83.00     ,        3.00    ,          3.6%
R17315246      ,        1065.00   ,          1067.00   ,        2.00    ,          -0.2%
RC4            ,        25.00     ,          25.00     ,        0.00    ,          0.0%
RIPEMD         ,        770.00    ,          785.00    ,        15.00   ,          -1.9%
Random         ,        982.00    ,          991.00    ,        9.00    ,          -0.9%
Rectangles     ,        959.00    ,          958.00    ,        1.00    ,          0.1%
SmallPT        ,        819.00    ,          791.00    ,        28.00   ,          3.5%
StrCat         ,        1285.00   ,          1466.00   ,        181.00  ,          -12.3%
StrToInt       ,        818.00    ,          832.00    ,        14.00   ,          -1.7%
StringBuilder  ,        779.00    ,          803.00    ,        24.00   ,          -3.0%
StringWalk     ,        1201.00   ,          1195.00   ,        6.00    ,          0.5%
Totals         ,        37369.00  ,          37403.00  ,        34.00   ,          -0.1%
Walsh          ,        134.00    ,          125.00    ,        9.00    ,          7.2%

Swift SVN r20797
2014-07-31 01:12:47 +00:00
Pete Cooper
6cc4b4fd7f Perform jump threading on enum inst feeding enum_is_tag
Swift SVN r20796
2014-07-31 01:12:45 +00:00
Pete Cooper
31cf2c4bf9 Add optimisation for switch_enum -> enum_is_tag.
This is currently done when a single branch generates a true, and all other branches generate false.

Swift SVN r20362
2014-07-23 02:53:43 +00:00
Mark Lacey
37b597f111 Remove redundant checked_cast_br by looking up the dominator tree.
As we do for conditional branches, try to optimized checked_cast_br to
branch when we have an identical dominating checked_cast_br.

<rdar://problem/17346767>

Swift SVN r19558
2014-07-04 06:38:23 +00:00
Nadav Rotem
d5948f9321 Fix a bug in simplifyLoopStructure to unbreak the build.
The bug was exposed by the pass order change.



Swift SVN r19175
2014-06-25 20:30:49 +00:00
Adrian Prantl
17e3604b43 Make sure all non-inlining uses of SILCloner also clone the SILDebugScope
hierarchy. I still need to figure out a reliable way to write testcases
for this. For now it's ensured via an assertion in SILCloner::postprocess.

Swift SVN r18917
2014-06-15 20:50:49 +00:00
Arnold Schwaighofer
7fb33bbbde Simplify loop header structures
Get rid of the switch optional enum construct that we use in counting loops.

Swift SVN r18716
2014-06-05 22:25:41 +00:00
Arnold Schwaighofer
c72961d5ac SimplifyCFG: Add dominator based simplify of conditional branches
Swift SVN r18715
2014-06-05 22:25:37 +00:00
Mark Lacey
07fedc728a Defer erasing instructions in unreachable blocks.
Instead of erasing instructions immediately, keep track of them until
we've found all the ones we'll erase, and then erase them.

It's not clear to me why the code was hitting an assert as it was
written, but this works around the problem.

Fixes <rdar://problem/16805316>.

Swift SVN r17372
2014-05-04 07:26:46 +00:00
Mark Lacey
2bf96448ab Minor style tweak.
Swift SVN r17313
2014-05-03 09:23:05 +00:00
Mark Lacey
7640b67504 Optimize switch_enum to unchecked_enum_data.
In cases where there is only one successor to a switch_enum that does
not end with "unreachable", replace the switch_enum with an
unchecked_enum_data extracting the data for that one valid successor.

Swift SVN r17312
2014-05-03 09:17:39 +00:00
Mark Lacey
91ddbeb291 Switch on instruction kind rather than using cascading ifs.
Swift SVN r17301
2014-05-03 01:20:39 +00:00
Mark Lacey
d867b5a845 Grab BB args from the correct block.
We do not necessarily want to grab the BB arg that we will use in the
branch we are creating from the same block that the switch_enum we are
eliminating is in.

There is still a latent bug here for the case where the block we are
creating a branch to expects an argument but the block we would grab the
argument from doesn't have an argument. This will be fixed when I land
changes that enforce that payload producing ops like switch_enum target
branches that always have a BB arg.

Swift SVN r17239
2014-05-02 06:35:31 +00:00
Mark Lacey
ae7b356540 Don't create branches with arguments to blocks that do not expect them.
I am planning on doing a larger change to ensure that blocks
consistently have a BB arg if they are targeted by a conditional branch
that has a payload (e.g. a switch_enum) so that we do not have to
special case like this.

Fix <rdar://problem/16761996>.

Swift SVN r17238
2014-05-02 06:35:30 +00:00
Mark Lacey
fee6979366 Handle the default case properly in switch_enum_inst simplification.
If we reach a switch_enum_inst via the default case of another
switch_enum_inst that is switching on the same value, we should keep
looking up the dominator tree rather than asserting.

Fixes <rdar://problem/16744848>.

Swift SVN r17020
2014-04-29 05:30:08 +00:00
Mark Lacey
86a434ab4d Use dominance to remove redundant conditional branching.
This updates the CFG optimization pass to use the dominator tree to
eliminate redundant switch_enum instructions. It can be enhanced at a
later time to handle other conditional branching instructions.

Because jump threading can expose opportunitites for this, we run a pass
of CFG simplifications prior to running this, and since this exposes
more opportunities for those CFG simplifications, we run them again
after. Despite this, there is no measurable compile-time impact building
the stdlib with a release build of the compiler.

This in concert with the two commits that precede it reduce the runtime
of the string sort benchmark by 9%, RC4 by around 2-3%, and now the
generated SIL for optionals look slightly more sane.

Swift SVN r16510
2014-04-18 07:19:34 +00:00
Mark Lacey
924e30ea61 Rewrite BB args whose only use is in struct/tuple extract.
If we have BB args that are only used in a struct/tuple extract, and
that are generated in each predecessor with a struct/tuple instruction,
retype the BB arg and replace the argument with what would have been the
extracted value.

This provides more opportunties for jump threading to kick in.

Swift SVN r16509
2014-04-18 07:19:33 +00:00
Michael Gottesman
267b4169dd [simplify-cfg] Refactor 2x methods to fit LLVM style. NFC.
Generally in LLVM we prefer early returns to large if () { return true; } return false;

Swift SVN r16469
2014-04-17 21:58:00 +00:00
Mark Lacey
2d014c0acd A couple small clean-ups.
Swift SVN r16420
2014-04-16 19:51:48 +00:00
Mark Lacey
a054675589 Remove unreachable code in SimplifyCFG.
Add a simple pass to remove blocks that are not reachable from the entry
of the function. Run it once before simplifying blocks, and then if any
blocks are simplified run it again in case those simplifications
resulted in an unreachable loop.

Swift SVN r16364
2014-04-15 07:14:51 +00:00
Mark Lacey
5ba256fb0b Do not try to merge a block with itself.
When merging a two blocks, where the source block has a single successor
and the sink block a single predecessor, do not attempt the merge if the
two blocks are the same, i.e. a self-loop.

<rdar://problem/16605102>

Swift SVN r16335
2014-04-14 22:13:17 +00:00
Joe Groff
4e4bea6dde SimplifyCFG: Don't pass an argument when simplifying switch_enum to br if the destination branch doesn't want one.
Fixes a crash building the stdlib with optimizations.

Swift SVN r15995
2014-04-06 03:04:19 +00:00
Mark Lacey
19ad5097f0 When simplifying switch_enum to a branch, don't forget the payload.
The optimization that was simplifying:
  %e = enum $T, #T.E, %payload
  switch_enum %e : $T, case #T.E: bb1
to an unconditional branch was not including the payload as a block
argument.

Fixes <rdar://problem/16509020>.

Swift SVN r15860
2014-04-03 09:20:55 +00:00
John McCall
c5d8e1280b Don't remove BB args after a conditional cast terminator.
Swift SVN r15421
2014-03-24 18:29:49 +00:00
Nadav Rotem
03daa6c83e Document the function. NFC.
Swift SVN r15129
2014-03-17 05:57:01 +00:00
Nadav Rotem
5f35d44ecf Remove unused arguments from the block argument list.
Swift SVN r15006
2014-03-13 21:29:56 +00:00
Chris Lattner
001c1890e5 put all the SIL*Transform classes in anonymous namespaces, there is
no need for their symbols to be exported out of their implementation
file.


Swift SVN r14714
2014-03-06 01:49:53 +00:00
Andrew Trick
0825258b80 SIL transforms should only invalidate when things change.
-sil-print-all shows a nice readable evolution now.

Oh yeah, and we don't unnecessarilly rerun passes.

Swift SVN r13677
2014-02-08 08:20:45 +00:00
Andrew Trick
731000b4cd Added -sil-print-all and -sil-verify-all options.
Swift SVN r13662
2014-02-07 23:07:11 +00:00
Nadav Rotem
27a1a63134 Remove unneeded empty virtual destructors.
Swift SVN r13599
2014-02-06 22:24:33 +00:00