Skip to content

Commit d59d567

Browse files
committed
Clean up the newly created Projection. NFC.
- Update comments. - Correct 80 col violations mostly due to renaming NewProjection to Projection. - Remove dead functions
1 parent 050b324 commit d59d567

File tree

2 files changed

+37
-152
lines changed

2 files changed

+37
-152
lines changed

include/swift/SIL/Projection.h

Lines changed: 14 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ constexpr unsigned MaxPointerProjectionKind = ((1 << TypeAlignInBits) - 1);
103103
/// Make sure that our tagged pointer assumptions are true. See comment above
104104
/// the declaration of ProjectionKind.
105105
static_assert(unsigned(ProjectionKind::LastPointerKind) <=
106-
unsigned(MaxPointerProjectionKind),
106+
unsigned(MaxPointerProjectionKind),
107107
"Too many projection kinds to fit in Projection");
108108

109109
static inline bool isCastProjectionKind(ProjectionKind Kind) {
@@ -130,7 +130,7 @@ struct ProjectionIndex {
130130
SILValue Aggregate;
131131
unsigned Index;
132132

133-
explicit ProjectionIndex(SILValue V) :Index(~0U) {
133+
explicit ProjectionIndex(SILValue V) : Index(~0U) {
134134
switch (V->getKind()) {
135135
default:
136136
break;
@@ -281,7 +281,7 @@ class Projection {
281281
return BaseType.getEnumElementType(getEnumElementDecl(BaseType), M);
282282
case ProjectionKind::Box:
283283
return SILType::getPrimitiveAddressType(BaseType.castTo<SILBoxType>()->
284-
getBoxedType());
284+
getBoxedType());
285285
case ProjectionKind::Tuple:
286286
return BaseType.getTupleElementType(getIndex());
287287
case ProjectionKind::Upcast:
@@ -315,24 +315,6 @@ class Projection {
315315
return *Iter;
316316
}
317317

318-
ValueDecl *getValueDecl(SILType BaseType) const {
319-
assert(isValid());
320-
switch (getKind()) {
321-
case ProjectionKind::Enum:
322-
return getEnumElementDecl(BaseType);
323-
case ProjectionKind::Struct:
324-
case ProjectionKind::Class:
325-
return getVarDecl(BaseType);
326-
case ProjectionKind::Upcast:
327-
case ProjectionKind::RefCast:
328-
case ProjectionKind::BitwiseCast:
329-
case ProjectionKind::Index:
330-
case ProjectionKind::Tuple:
331-
case ProjectionKind::Box:
332-
llvm_unreachable("ProjectionKind that does not have a value decl?");
333-
}
334-
}
335-
336318
SILType getCastType(SILType BaseType) const {
337319
assert(isValid());
338320
assert(getKind() == ProjectionKind::Upcast ||
@@ -483,6 +465,7 @@ class ProjectionPath {
483465
private:
484466
SILType BaseType;
485467
SILType MostDerivedType;
468+
/// A path from base to most-derived.
486469
PathTy Path;
487470

488471
public:
@@ -555,7 +538,7 @@ class ProjectionPath {
555538
/// types via ref_element_addr. If Start is an address type though, End will
556539
/// always also be an address type.
557540
static Optional<ProjectionPath> getProjectionPath(SILValue Start,
558-
SILValue End);
541+
SILValue End);
559542

560543
/// Treating a projection path as an ordered set, if RHS is a prefix of LHS,
561544
/// return the projection path with that prefix removed.
@@ -595,27 +578,6 @@ class ProjectionPath {
595578
/// subsequence of the other.
596579
SubSeqRelation_t computeSubSeqRelation(const ProjectionPath &RHS) const;
597580

598-
/// Returns true if this is a projection path that takes an address base type
599-
/// to an address derived type.
600-
bool isAddressProjectionPath() const;
601-
602-
/// Returns true if this is a projection path that takes an object base type
603-
/// to an object derived type.
604-
bool isObjectProjectionPath() const;
605-
606-
/// Find all object projection paths from I that matches this projection
607-
/// path. Return the tails of each extract path in T.
608-
bool
609-
findMatchingObjectProjectionPaths(SILInstruction *I,
610-
SmallVectorImpl<SILInstruction *> &T) const;
611-
612-
/// If this is an address projection path and \p Base is a SILValue with the
613-
/// object version of said type, use \p B and \p Loc to recreate the stored
614-
/// address projection path as an object projection path from \p Base. Return
615-
/// the SILValue at the end of the path.
616-
SILValue createObjectProjections(SILBuilder &B, SILLocation Loc,
617-
SILValue Base);
618-
619581
/// Pushes an element to the path.
620582
void push_back(const Projection &Proj) { Path.push_back(Proj); }
621583

@@ -713,13 +675,8 @@ class ProjectionTreeNode {
713675
/// invalidating our pointers.
714676
unsigned Index;
715677

716-
/// The base type from which this projection tree node is derived via Proj. It
717-
/// is necessary to maintain a separate such entry from BaseValues since we
718-
/// may have projection tree nodes without any BaseValues since we completely
719-
/// explode non-enum scalar values to the leafs of our tree.
720-
///
721-
/// In the root of the tree, this is the actual type.
722-
SILType BaseType;
678+
/// The type this projection tree node represents.
679+
SILType NodeType;
723680

724681
/// The projection that this node represents. None in the root.
725682
llvm::Optional<Projection> Proj;
@@ -755,14 +712,14 @@ class ProjectionTreeNode {
755712
};
756713

757714
/// Constructor for the root of the tree.
758-
ProjectionTreeNode(SILType BaseTy)
759-
: Index(0), BaseType(BaseTy), Proj(), Parent(),
715+
ProjectionTreeNode(SILType NodeTy)
716+
: Index(0), NodeType(NodeTy), Proj(), Parent(),
760717
NonProjUsers(), ChildProjections(), Initialized(false), IsLive(false) {}
761718

762719
// Normal constructor for non-root nodes.
763-
ProjectionTreeNode(ProjectionTreeNode *Parent, unsigned Index, SILType BaseTy,
720+
ProjectionTreeNode(ProjectionTreeNode *Parent, unsigned Index, SILType NodeTy,
764721
Projection P)
765-
: Index(Index), BaseType(BaseTy), Proj(P),
722+
: Index(Index), NodeType(NodeTy), Proj(P),
766723
Parent(Parent->getIndex()), NonProjUsers(), ChildProjections(),
767724
Initialized(false), IsLive(false) {}
768725

@@ -776,9 +733,9 @@ class ProjectionTreeNode {
776733
return llvm::makeArrayRef(ChildProjections);
777734
}
778735

779-
llvm::Optional<Projection> &getProjection() {
780-
return Proj;
781-
}
736+
llvm::Optional<Projection> &getProjection() { return Proj; }
737+
738+
SILType getType() const { return NodeType; }
782739

783740
bool isRoot() const {
784741
// Root does not have a parent. So if we have a parent, we cannot be root.
@@ -794,10 +751,6 @@ class ProjectionTreeNode {
794751
}
795752
}
796753

797-
SILType getType() const {
798-
return BaseType;
799-
}
800-
801754
ProjectionTreeNode *getChildForProjection(ProjectionTree &Tree,
802755
const Projection &P);
803756

@@ -819,9 +772,6 @@ class ProjectionTreeNode {
819772
return getParent(Tree);
820773
}
821774

822-
823-
llvm::Optional<Projection> getProjection() const { return Proj; }
824-
825775
private:
826776
void addNonProjectionUser(Operand *Op) {
827777
IsLive = true;

lib/SIL/Projection.cpp

Lines changed: 23 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ static_assert(std::is_standard_layout<Projection>::value,
3737
// Utility
3838
//===----------------------------------------------------------------------===//
3939

40-
/// We do not support symbolic projections yet, only 32-bit unsigned integers.
40+
/// Extract an integer index from a SILValue.
41+
///
42+
/// Return true if IndexVal is a constant index representable as unsigned
43+
/// int. We do not support symbolic projections yet, only 32-bit unsigned
44+
/// integers.
4145
bool swift::getIntegerIndex(SILValue IndexVal, unsigned &IndexConst) {
4246
if (auto *IndexLiteral = dyn_cast<IntegerLiteralInst>(IndexVal)) {
4347
APInt ConstInt = IndexLiteral->getValue();
@@ -187,7 +191,7 @@ Projection::Projection(SILInstruction *I) : Value() {
187191

188192
NullablePtr<SILInstruction>
189193
Projection::createObjectProjection(SILBuilder &B, SILLocation Loc,
190-
SILValue Base) const {
194+
SILValue Base) const {
191195
SILType BaseTy = Base->getType();
192196

193197
// We can only create a value projection from an object.
@@ -221,7 +225,7 @@ Projection::createObjectProjection(SILBuilder &B, SILLocation Loc,
221225

222226
NullablePtr<SILInstruction>
223227
Projection::createAddressProjection(SILBuilder &B, SILLocation Loc,
224-
SILValue Base) const {
228+
SILValue Base) const {
225229
SILType BaseTy = Base->getType();
226230

227231
// We can only create an address projection from an object, unless we have a
@@ -259,8 +263,8 @@ Projection::createAddressProjection(SILBuilder &B, SILLocation Loc,
259263
}
260264
}
261265

262-
void Projection::getFirstLevelProjections(
263-
SILType Ty, SILModule &Mod, llvm::SmallVectorImpl<Projection> &Out) {
266+
void Projection::getFirstLevelProjections(SILType Ty, SILModule &Mod,
267+
llvm::SmallVectorImpl<Projection> &Out) {
264268
if (auto *S = Ty.getStructOrBoundGenericStruct()) {
265269
unsigned Count = 0;
266270
for (auto *VDecl : S->getStoredProperties()) {
@@ -319,11 +323,11 @@ void Projection::getFirstLevelProjections(
319323
}
320324

321325
//===----------------------------------------------------------------------===//
322-
// New Projection Path
326+
// Projection Path
323327
//===----------------------------------------------------------------------===//
324328

325329
Optional<ProjectionPath> ProjectionPath::getProjectionPath(SILValue Start,
326-
SILValue End) {
330+
SILValue End) {
327331
ProjectionPath P(Start->getType(), End->getType());
328332

329333
// If Start == End, there is a "trivial" projection path in between the
@@ -364,8 +368,8 @@ Optional<ProjectionPath> ProjectionPath::getProjectionPath(SILValue Start,
364368
///
365369
/// This means that the two objects have the same base but access different
366370
/// fields of the base object.
367-
bool ProjectionPath::hasNonEmptySymmetricDifference(
368-
const ProjectionPath &RHS) const {
371+
bool
372+
ProjectionPath::hasNonEmptySymmetricDifference(const ProjectionPath &RHS) const{
369373
// First make sure that both of our base types are the same.
370374
if (BaseType != RHS.BaseType)
371375
return false;
@@ -489,63 +493,9 @@ ProjectionPath::computeSubSeqRelation(const ProjectionPath &RHS) const {
489493
return SubSeqRelation_t::RHSStrictSubSeqOfLHS;
490494
}
491495

492-
bool ProjectionPath::findMatchingObjectProjectionPaths(
493-
SILInstruction *I, SmallVectorImpl<SILInstruction *> &T) const {
494-
// We only support unary instructions.
495-
if (I->getNumOperands() != 1)
496-
return false;
497-
498-
// Check that the base result type of I is equivalent to this types base path.
499-
if (I->getOperand(0)->getType().copyCategory(BaseType) != BaseType)
500-
return false;
501-
502-
// We maintain the head of our worklist so we can use our worklist as a queue
503-
// and work in breadth first order. This makes sense since we want to process
504-
// in levels so we can maintain one tail list and delete the tail list when we
505-
// move to the next level.
506-
unsigned WorkListHead = 0;
507-
llvm::SmallVector<SILInstruction *, 8> WorkList;
508-
WorkList.push_back(I);
509-
510-
// Start at the root of the list.
511-
for (auto PI = rbegin(), PE = rend(); PI != PE; ++PI) {
512-
// When we start a new level, clear the tail list.
513-
T.clear();
514-
515-
// If we have an empty worklist, return false. We have been unable to
516-
// complete the list.
517-
unsigned WorkListSize = WorkList.size();
518-
if (WorkListHead == WorkListSize)
519-
return false;
520-
521-
// Otherwise, process each instruction in the worklist.
522-
for (; WorkListHead != WorkListSize; WorkListHead++) {
523-
SILInstruction *Ext = WorkList[WorkListHead];
524-
525-
// If the current projection does not match I, continue and process the
526-
// next instruction.
527-
if (!PI->matchesObjectProjection(Ext)) {
528-
continue;
529-
}
530-
531-
// Otherwise, we know that Ext matched this projection path and we should
532-
// visit all of its uses and add Ext itself to our tail list.
533-
T.push_back(Ext);
534-
for (auto *Op : Ext->getUses()) {
535-
WorkList.push_back(Op->getUser());
536-
}
537-
}
538-
539-
// Reset the worklist size.
540-
WorkListSize = WorkList.size();
541-
}
542-
543-
return true;
544-
}
545-
546496
Optional<ProjectionPath>
547497
ProjectionPath::removePrefix(const ProjectionPath &Path,
548-
const ProjectionPath &Prefix) {
498+
const ProjectionPath &Prefix) {
549499
// We can only subtract paths that have the same base.
550500
if (Path.BaseType != Prefix.BaseType)
551501
return llvm::NoneType::None;
@@ -575,19 +525,6 @@ ProjectionPath::removePrefix(const ProjectionPath &Path,
575525
return P;
576526
}
577527

578-
SILValue ProjectionPath::createObjectProjections(SILBuilder &B,
579-
SILLocation Loc,
580-
SILValue Base) {
581-
assert(BaseType.isAddress());
582-
assert(Base->getType().isObject());
583-
assert(Base->getType().getAddressType() == BaseType);
584-
SILValue Val = Base;
585-
for (auto iter : Path) {
586-
Val = iter.createObjectProjection(B, Loc, Val).get();
587-
}
588-
return Val;
589-
}
590-
591528
raw_ostream &ProjectionPath::print(raw_ostream &os, SILModule &M) {
592529
// Match how the memlocation print tests expect us to print projection paths.
593530
//
@@ -685,7 +622,7 @@ void ProjectionPath::verify(SILModule &M) {
685622

686623
void
687624
ProjectionPath::expandTypeIntoLeafProjectionPaths(SILType B, SILModule *Mod,
688-
ProjectionPathList &Paths) {
625+
ProjectionPathList &Paths) {
689626
// Perform a BFS to expand the given type into projectionpath each of
690627
// which contains 1 field from the type.
691628
llvm::SmallVector<ProjectionPath, 8> Worklist;
@@ -751,7 +688,7 @@ ProjectionPath::expandTypeIntoLeafProjectionPaths(SILType B, SILModule *Mod,
751688

752689
void
753690
ProjectionPath::expandTypeIntoNodeProjectionPaths(SILType B, SILModule *Mod,
754-
ProjectionPathList &Paths) {
691+
ProjectionPathList &Paths) {
755692
// Perform a BFS to expand the given type into projectionpath each of
756693
// which contains 1 field from the type.
757694
llvm::SmallVector<ProjectionPath, 8> Worklist;
@@ -905,7 +842,6 @@ ProjectionTreeNode::getChildForProjection(ProjectionTree &Tree,
905842
return N;
906843
}
907844
}
908-
909845
return nullptr;
910846
}
911847

@@ -932,10 +868,8 @@ createProjection(SILBuilder &B, SILLocation Loc, SILValue Arg) const {
932868
return Proj->createProjection(B, Loc, Arg);
933869
}
934870

935-
936871
void
937-
ProjectionTreeNode::
938-
processUsersOfValue(ProjectionTree &Tree,
872+
ProjectionTreeNode::processUsersOfValue(ProjectionTree &Tree,
939873
llvm::SmallVectorImpl<ValueNodePair> &Worklist,
940874
SILValue Value) {
941875
DEBUG(llvm::dbgs() << " Looking at Users:\n");
@@ -1232,14 +1166,14 @@ ProjectionTree::computeExplodedArgumentValueInner(SILBuilder &Builder,
12321166
Node->getType(),
12331167
ChildValues);
12341168

1235-
assert(AI.get() && "Failed to get a part of the debug value");
1169+
assert(AI.get() && "Failed to get a part of value");
12361170
return SILValue(AI.get());
12371171
}
12381172

12391173
SILValue
1240-
ProjectionTree::computeExplodedArgumentValue(SILBuilder &Builder,
1241-
SILLocation Loc,
1242-
llvm::SmallVector<SILValue, 8> &LeafValues) {
1174+
ProjectionTree::computeExplodedArgumentValue(
1175+
SILBuilder &Builder, SILLocation Loc,
1176+
llvm::SmallVector<SILValue, 8> &LeafValues) {
12431177
// Construct the leaf index to leaf value map.
12441178
llvm::DenseMap<unsigned, SILValue> LeafIndexToValue;
12451179
for (unsigned i = 0; i < LeafValues.size(); ++i) {
@@ -1388,7 +1322,8 @@ getNextValidNode(llvm::SmallVectorImpl<ProjectionTreeNode *> &Worklist,
13881322

13891323
ProjectionTreeNode *Node = Worklist.back();
13901324

1391-
// If the Node is not complete, then we have reached a dead lock. This should never happen.
1325+
// If the Node is not complete, then we have reached a dead lock. This should
1326+
// never happen.
13921327
//
13931328
// TODO: Prove this and put the proof here.
13941329
if (CheckForDeadLock && !isComplete(Node)) {

0 commit comments

Comments
 (0)