Commit Graph

59 Commits

Author SHA1 Message Date
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
Nadav Rotem
1ef0d157ca PassManager: Inject the function/module into the Transformation.
Now the pass does not need to know about the pass manager. We also don't have
runOnFunction or runOnModule anymore because the trnasformation knows
which module it is processing. The Pass itself knows how to invalidate the
analysis, based on the injected pass manager that is internal to the
transformation.

Now our DCE transformation looks like this:

class DCE : public SILModuleTransform {
  void run() {
    performSILDeadCodeElimination(getModule());
    invalidateAnalysis(SILAnalysis::InvalidationKind::All);
  }
};





Swift SVN r13598
2014-02-06 22:11:21 +00:00
Nadav Rotem
99b075c32a Rename SILFunctionTrans -> SILFunctionTransform
Swift SVN r13536
2014-02-06 01:32:10 +00:00
Nadav Rotem
f8c7b54d28 Delete the unused performXXX() functions.
Swift SVN r13531
2014-02-06 00:57:28 +00:00
Michael Gottesman
631f9326ab [PM] Change enum => enum class everywhere in the PM code. Additionally fix some typos.
Swift SVN r13507
2014-02-05 21:25:15 +00:00
Joe Groff
7bb68490a1 Text files end with newlines.
Swift SVN r13500
2014-02-05 20:23:32 +00:00
Nadav Rotem
baa1d1679a Teach some of the passes about the new PM.
Swift SVN r13494
2014-02-05 18:58:23 +00:00
Nadav Rotem
654ce61c8f Reapply r13275 in hope it won't break the build again.
CFG trampoline optimizations and cond_br optimization.



Swift SVN r13369
2014-02-03 21:34:58 +00:00
Michael Gottesman
6fa09aa153 Revert "Reapply the cfg trampoline optimization and add another cond_br optimization:"
This reverts commit r13275. Fixing the build.

Swift SVN r13280
2014-02-01 02:19:11 +00:00
Nadav Rotem
ca27474139 Reapply the cfg trampoline optimization and add another cond_br optimization:
Same target:

 cond_br %0, bb1(%1 : $Int64), bb2(%1 : $Int64)

And trampolines:

bb0(%0 : $Builtin.Int1, %1 : $Int64, %2 : $Int64):
  cond_br %0, bb1(%1 : $Int64), bb2(%2 : $Int64)

bb1(%3 : $Int64):
  br bb3(%3 : $Int64)

bb2(%4 : $Int64):
  br bb3(%4 : $Int64)





Swift SVN r13275
2014-02-01 01:32:27 +00:00
Jordan Rose
a610743064 Revert "Remove simple trampoline blocks."
This seems to break building the Foundation overlay.

This reverts r13242.

Swift SVN r13246
2014-01-31 19:58:01 +00:00
Nadav Rotem
4dfecb243e Remove simple trampoline blocks.
Like this one:

bb2(%A : $Int64):
  br bb3(%A : $Int64)



Swift SVN r13242
2014-01-31 19:38:46 +00:00
Nadav Rotem
9b19675fb9 SimplifyCFG: Don't drop the branch arguments when simplifying conditional branch into a non conditional branch.
Swift SVN r11990
2014-01-07 07:40:48 +00:00
Michael Gottesman
4379283013 Remove inclusion of SILPasses/Passes.h into Subsystems.h and update all relevant files.
Swift SVN r10880
2013-12-05 19:58:21 +00:00