Commit Graph

890 Commits

Author SHA1 Message Date
Manman Ren
d0068877f5 [PGO] Add SILMetadata for branch weights.
SILMetadata is the base class with a single enum member (MDKind).
SILBranchNode is the derived class with additional members:
  unsigned NumOperands
  an array of uint32_t

A static member function SILBranchNode::get is implemented to get or create
SILBranchNode. All SILMetadata created are uniqued and saved in SILModule's
member variable:
  llvm::FoldingSet<SILMetadata> Metadatas

Usage of SILMetadta by SILInstruction is captured in SILModule's member variable:
  llvm::DenseMap<const SILInstruction *, SILMetadata *> MetadataStore
This is similar to LLVM's Metadata. Another option is to add a SILMetadata* to
SILInstruction. The disadvantage is the waste of space when we don't have PGO on.

This commit also enables parsing and printing of SILMetadata.

We add keyword sil_metadata to define SILMetadata:
  sil_metadata !0 = {"branch_weights", 3, 5}

For parsing, we add a map in SILModule
  llvm::DenseMap<unsigned, SILMetadata *> NumberedMetadata
that maps from ID to SILMetadata* to help matching usage of "!id" in SILFunction
with definition of "!id" in sil_metadata section.

For printing, we assign IDs to SILMetadata at SILModule scope, we then pass in
an optional argument of
  llvm::DenseMap<const SILMetadata *, unsigned> *MetadataMap
to SILFunction::print in order to get the ID of SILMetadata used in
SILInstruction.

Post-commit review will be appreciated.

rdar://18269754


Swift SVN r23713
2014-12-05 01:47:11 +00:00
John McCall
dd07c8ca10 Add 'mark_dependence', which indicates that an address
or pointer depends on another for validity in a
non-obvious way.

Also, document some basic value-propagation rules
based roughly on the optimization rules for ARC.

Swift SVN r23695
2014-12-04 22:38:09 +00:00
Arnold Schwaighofer
c322b3592d Add a data dependence between opened existential values and method_inst that 'use' them.
Before this patch there was no dependence visible to the optimizer between a
open_existential and the witness_method allowing the optimizer to reorder the
two instruction. The dependence was implicit in the opened archetype but this
is not a concept model by the SIL optimizer.

  %2 = open_existential %0 : $*FooProto to $*@opened("...") FooProto
  %3 = witness_method $@opened("...") FooProto,
                      #FooProto.bar!1 : $@cc(...)
  %4 = apply %3<...>(%2)

This patch changes the SIL representation such that witness_methods on opened
archetypes take the open_existential (or the producer of the opened existential)
as an operand preventing the optimizer from reordering them.

  %2 = open_existential %0 : $*FooProto to $*@opened("...") FooProto
  %3 = witness_method $@opened("...") FooProto,
                      #FooProto.bar!1,
                      %2 : $*@opened("...") FooProto : $@cc(...)
  %4 = apply %3<...>(%2)

rdar://18984526

Swift SVN r23438
2014-11-19 17:22:22 +00:00
Michael Gottesman
fae31cbaa2 [dead-object-elim] Add SILInstruction::mayTrap(), change SILInstruction::mayHaveSideEffects() to call it, and change dead object elimination to call mayHaveSideEffects().
Note that I did not change any actual memory behavior. That will come via a
later cleanup phase. Since this is a correctness fix I wanted to only make
things more conservative.

rdar://18568601

Swift SVN r23311
2014-11-14 00:05:15 +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
Erik Eckstein
da9f08aec8 Replace the default-entry with a corresponding case-entry in select_enum[ _addr], if possible.
This is exactly the same thing as I did for switch_enum.



Swift SVN r23116
2014-11-05 12:48:11 +00:00
Roman Levenstein
03a5d72a35 Get rid of virtual functions. Use CRTP instead.
Swift SVN r23057
2014-11-01 06:34:30 +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
Roman Levenstein
f016754ef9 Add a new select_value instruction. This instruction should be the equivalent of select_enum, just for builtin int types. Such an instruction is needed e.g. to efficiently implement conversions of Int raw values to C-like enums.
rdar://18812325

Swift SVN r23036
2014-10-31 20:44:11 +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
Michael Gottesman
af10399976 [rc-id] Teach RCId how to strip off StructInst, TupleInst, and TupleExtractInst.
Specifically:

1. Given a struct literal with only one stored non-trivial field, a ref count
operation on the struct is equivalent to a ref count operation on the field.

2. Given a tuple literal with only one non-trivial elt , a ref count
operation on the tuple is equivalent to a ref count operation on the elt.

3. Given a tuple_extract, if the tuple_extract is extracting the only
non-trivial element of the tuple, a ref count on the tuple_extract is equivalent
to a ref count on the original type.

rdar://18499023

Swift SVN r22902
2014-10-23 23:38:26 +00:00
Joe Groff
5a2f48e3be Add a Builtin.BridgeObject type.
This is a type that has ownership of a reference while allowing access to the
spare bits inside the pointer, but which can also safely hold an ObjC tagged pointer
reference (with no spare bits of course). It additionally blesses one
Foundation-coordinated bit with the meaning of "has swift refcounting" in order
to get a faster short-circuit to native refcounting. It supports the following
builtin operations:

- Builtin.castToBridgeObject<T>(ref: T, bits: Builtin.Word) ->
  Builtin.BridgeObject

  Creates a BridgeObject that contains the bitwise-OR of the bit patterns of
  "ref" and "bits". It is the user's responsibility to ensure "bits" doesn't
  interfere with the reference identity of the resulting value. In other words,
  it is undefined behavior unless:

    castReferenceFromBridgeObject(castToBridgeObject(ref, bits)) === ref

  This means "bits" must be zero if "ref" is a tagged pointer. If "ref" is a real
  object pointer, "bits" must not have any non-spare bits set (unless they're
  already set in the pointer value). The native discriminator bit may only be set
  if the object is Swift-refcounted.

- Builtin.castReferenceFromBridgeObject<T>(bo: Builtin.BridgeObject) -> T

  Extracts the reference from a BridgeObject.

- Builtin.castBitPatternFromBridgeObject(bo: Builtin.BridgeObject) -> Builtin.Word

  Presents the bit pattern of a BridgeObject as a Word.

BridgeObject's bits are set up as follows on the various platforms:

i386, armv7:

  No ObjC tagged pointers
  Swift native refcounting flag bit: 0x0000_0001
  Other available spare bits:        0x0000_0002

x86_64:

  Reserved for ObjC tagged pointers: 0x8000_0000_0000_0001
  Swift native refcounting flag bit: 0x0000_0000_0000_0002
  Other available spare bits:        0x7F00_0000_0000_0004

arm64:

  Reserved for ObjC tagged pointers: 0x8000_0000_0000_0000
  Swift native refcounting flag bit: 0x4000_0000_0000_0000
  Other available spare bits:        0x3F00_0000_0000_0007

TODO: BridgeObject doesn't present any extra inhabitants. It ought to at least provide null as an extra inhabitant for Optional.

Swift SVN r22880
2014-10-23 00:09:23 +00:00
Joe Groff
3f23b82e6d SIL: Rename SILGlobalAddr to GlobalAddr.
All globals are SIL globals now.

Swift SVN r22827
2014-10-18 17:08:28 +00:00
Joe Groff
ea65d1e60b SIL: Remove the builtin_function_ref instruction.
Swift SVN r22797
2014-10-16 16:18:40 +00:00
Arnold Schwaighofer
ad0e20a316 Make isa work with SwitchEnumInstBase.
Swift SVN r22755
2014-10-15 17:07:02 +00:00
Joe Groff
bb46f4bbd9 SIL: Remove the global_addr instruction.
It's no longer needed now that we always lower to SIL globals.

Swift SVN r22693
2014-10-12 17:19:06 +00:00
Joe Groff
a60a52d72e SIL: Add a "builtin" instruction to represent builtin invocations.
Modeling builtins as first-class function values doesn't really make sense because there's no real function value to emit, and modeling them this way complicates passes that work with builtins because they have to invent function types for builtin invocations. It's much more straightforward to have a single instruction that references the builtin by ID, along with the type information for the necessary values, type parameters, and results, so add a new "builtin" instruction that directly represents a builtin invocation. NFC yet.

Swift SVN r22690
2014-10-11 20:34:24 +00:00
Mark Lacey
0542dc421b Fix comment.
Swift SVN r22654
2014-10-10 04:58:28 +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
John McCall
89e60f31aa Add protocol witness tables to existential metatype
layouts.  Introduce new SIL instructions to initialize
and open existential metatype values.

Don't actually, y'know, lift any of the restriction on
existential metatypes; just pointlessly burn extra
memory storing them.

Swift SVN r22592
2014-10-08 01:20:13 +00:00
Joe Groff
ca8b168188 SIL: Add select_enum and select_enum_addr insns.
Similar to LLVM's "select" instruction, the instruction picks one of its operands based on the case tag of an enum value.

Swift SVN r22578
2014-10-07 21:45:08 +00:00
Michael Gottesman
b4c06632e2 Add a helper method SILInstruction::replaceAllUsesWithUndef().
This should really be on ValueBase, but currently ValueBase does not
have access to a SILModule.

Swift SVN r22559
2014-10-07 00:53:56 +00:00
Andrew Trick
43b928bc7c Added ApplyInst::getArgumentOperandNumber to avoid hard-coding this in many places.
Swift SVN r22516
2014-10-04 01:23:10 +00:00
Andrew Trick
89a53d1205 Make StoreInst operand position enum public for the same reason as CopyAddrInst.
Swift SVN r22515
2014-10-04 01:23:10 +00:00
Andrew Trick
245097d4d1 Add a UnaryInstructionBase::getOperandRef helper so I can safely get
to the operand.

Swift SVN r22514
2014-10-04 01:23:09 +00:00
Andrew Trick
66b75ba820 Make CopyAddrInst operand enum public.
I think all the enums that identify operand position should be public.
I've noticed a number of places where we hard-code operand positions
which seems pretty horrible to me.

Swift SVN r22513
2014-10-04 01:23:08 +00:00
Joe Groff
782833f054 SIL: Remove the project_existential* instructions.
Swift SVN r22457
2014-10-02 04:06:10 +00:00
Joe Groff
3a606b9eb8 SIL: Drop the protocol_method instruction.
Swift SVN r22446
2014-10-01 23:35:41 +00:00
Joe Groff
be45322668 SIL: Drop the upcast_existential* instructions.
Swift SVN r22388
2014-09-30 16:11:54 +00:00
Joe Groff
c098645f34 SIL: Conformances relate to formal types, so change witness_method's lookup type to a CanType.
Should be NFC in practice, since only nominal types can currently conform to protocols anyway, but improves modeling of the system.

Swift SVN r22360
2014-09-29 20:44:00 +00:00
Joe Groff
152aa9e244 Revert "SIL: Drop the upcast_existential* instructions."
This reverts commit r22345.

Swift SVN r22353
2014-09-29 13:46:41 +00:00
Joe Groff
1e343fb430 SIL: Drop the upcast_existential* instructions.
Swift SVN r22345
2014-09-28 19:24:33 +00:00
Joe Groff
0518f2067f Revert "SIL: Drop the upcast_existential* instructions."
This reverts commit r22333.

Swift SVN r22337
2014-09-28 18:41:05 +00:00
Joe Groff
727c9b5ed7 SIL: Drop the upcast_existential* instructions.
Swift SVN r22333
2014-09-28 16:38:13 +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
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
Michael Gottesman
a30dbd1fc0 Add some helper methods to CondBranchInst for getting True, False operands and a swapSuccessor method.
Swift SVN r22157
2014-09-21 10:56:28 +00:00
Michael Gottesman
8d9e358ad4 [closure-spec] Use the signature of the closure's function_ref instead of the
arguments of the closure since if the closure is an external declaration, it
wont have a first BB.

Swift SVN r22114
2014-09-19 02:33:58 +00:00
Arnold Schwaighofer
73fd579edf SILInstruction: Add setArgument and setSelfArgument to ApplyInst
Swift SVN r21924
2014-09-12 21:05:20 +00:00
Joe Groff
e004ad7df4 SIL: Carry AST types through init_existential instructions.
This is necessary to be able to properly stash values with nontrivial lowerings, such as metatypes and functions, inside existential containers. Modify SILGen to lower values to the proper abstraction level before storing them in an existential container. Part of the fix for rdar://problem/18189508, though runtime problems still remain when trying to actually dynamicCast out a metatype from an Any container.

Swift SVN r21830
2014-09-10 05:56:36 +00:00
Doug Gregor
c504086266 Revert r21707 "Remove the SIL is_nonnull instruction. It's no longer useful."
We want to be able to work around problems with non-failable
Objective-C initializers actually failing, which can happen when the
API audit data incorrectly marks an initializer as non-failable.



Swift SVN r21711
2014-09-04 17:26:34 +00:00
Doug Gregor
cdf94885ae Remove the SIL is_nonnull instruction. It's no longer useful.
Swift SVN r21707
2014-09-04 15:56:12 +00:00
Michael Gottesman
1ba4322412 Make a small fix for LLVM style by inverting an if (condition) {return} into an if (!condition) {continue} return. NFC.
Swift SVN r21615
2014-08-31 18:18:38 +00:00
Michael Gottesman
ec7cc211cb [g-arc-opts] A struct_extract of the only non-trivial stored field of a struct is always a ref count identity preserving identity.
rdar://18189325

Swift SVN r21614
2014-08-31 18:08:30 +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
Mark Lacey
51e710170a Fix typo in comment.
Swift SVN r21461
2014-08-26 19:49:46 +00:00
Erik Eckstein
17c4a50766 Let the switch_enum instruction return a unqiue enum case for the default block, if possible.
This enables removal of more redundant switch_enum instructions, like

  switch_enum %0 : ..., default bb2
bb2:
  switch_enum %0 : ...




Swift SVN r21418
2014-08-22 18:51:56 +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