Files
swift-mirror/lib/SIL/Projection.cpp
Michael Gottesman 596f406f31 [aa] Refactor all projection path logic into a class called ProjectionPath.
rdar://18831605

Swift SVN r23060
2014-11-02 00:41:13 +00:00

139 lines
4.8 KiB
C++

//===--- Projection.cpp ---------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-projection"
#include "swift/SIL/Projection.h"
#include "llvm/ADT/None.h"
#include "llvm/Support/Debug.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// Projection
//===----------------------------------------------------------------------===//
/// Returns true if we are accessing different fields.
static bool areProjectionsToDifferentFields(const Projection &P1,
const Projection &P2) {
// If operands have the same type and we are accessing different fields,
// returns true. Operand's type is not saved in Projection. Instead we check
// Decl's context.
return P1.getDecl() && P2.getDecl() &&
P1.getDecl()->getDeclContext() == P2.getDecl()->getDeclContext() &&
P1 != P2;
}
//===----------------------------------------------------------------------===//
// Projection Path
//===----------------------------------------------------------------------===//
Optional<ProjectionPath>
ProjectionPath::getAddressProjectionPathBetweenValues(SILValue Start,
SILValue End,
bool IgnoreCasts) {
// Do not inspect the body of structs with unreferenced types such as
// bitfields and unions.
if (Start.getType().aggregateHasUnreferenceableStorage() ||
End.getType().aggregateHasUnreferenceableStorage()) {
return llvm::NoneType::None;
}
ProjectionPath P;
// If Start == End, there is a "trivial" address projection in between the
// two. This is represented by returning an empty ProjectionPath.
if (Start == End)
return std::move(P);
// Otherwise see if End can be projection extracted from Start. First see if
// End is a projection at all.
auto Iter = End;
if (IgnoreCasts)
Iter = Iter.stripCasts();
while (Projection::isAddressProjection(Iter) && Start != Iter) {
if (auto *SEA = dyn_cast<StructElementAddrInst>(Iter))
P.Path.push_back(Projection(SEA));
else if (auto *TEA = dyn_cast<TupleElementAddrInst>(Iter))
P.Path.push_back(Projection(TEA));
else if (auto *REA = dyn_cast<RefElementAddrInst>(Iter))
P.Path.push_back(Projection(REA));
else
P.Path.push_back(Projection(cast<UncheckedTakeEnumDataAddrInst>(Iter)));
Iter = cast<SILInstruction>(*Iter).getOperand(0);
if (IgnoreCasts)
Iter = Iter.stripCasts();
}
// Return None if we have an empty projection list or if Start == Iter.
if (P.empty() || Start != Iter)
return llvm::NoneType::None;
// Otherwise, return P.
return std::move(P);
}
bool
ProjectionPath::operator==(const ProjectionPath &RHS) const {
if (size() != RHS.size())
return false;
for (unsigned i = 0, e = size(); i != e; ++i)
if (Path[i] != RHS.Path[i])
return false;
return true;
}
/// Returns true if the two paths have a non-empty symmetric difference.
///
/// This means that the two objects have the same base but access different
/// fields of the base object.
bool
ProjectionPath::
hasNonEmptySymmetricDifference(const ProjectionPath &RHS) const {
// If either the LHS or RHS is empty, there is no common base class. Return
// false.
if (empty() || RHS.empty())
return false;
// We reverse the projection path to scan from the common object.
auto LHSReverseIter = Path.rbegin();
auto RHSReverseIter = RHS.Path.rbegin();
// For each index i until min path size...
for (unsigned i = 0, e = std::min(size(), RHS.size()); i != e; ++i) {
// Grab the current projections.
const Projection &LHSProj = *LHSReverseIter;
const Projection &RHSProj = *RHSReverseIter;
// If we are accessing different fields of a common object, return
// false. The two projection paths must have a non-empty symmetric
// difference.
if (areProjectionsToDifferentFields(LHSProj, RHSProj)) {
DEBUG(llvm::dbgs() << " Path different at index: " << i << '\n');
return true;
}
// Otherwise, if the two projections equal exactly, they have no symmetric
// difference.
if (LHSProj == RHSProj)
return false;
// Continue if we are accessing the same field.
LHSReverseIter++;
RHSReverseIter++;
}
// We checked
return false;
}