Files
swift-mirror/lib/SILOptimizer/UtilityPasses/ValueOwnershipKindDumper.cpp
John McCall ab3f77baf2 Make SILInstruction no longer a subclass of ValueBase and
introduce a common superclass, SILNode.

This is in preparation for allowing instructions to have multiple
results.  It is also a somewhat more elegant representation for
instructions that have zero results.  Instructions that are known
to have exactly one result inherit from a class, SingleValueInstruction,
that subclasses both ValueBase and SILInstruction.  Some care must be
taken when working with SILNode pointers and testing for equality;
please see the comment on SILNode for more information.

A number of SIL passes needed to be updated in order to handle this
new distinction between SIL values and SIL instructions.

Note that the SIL parser is now stricter about not trying to assign
a result value from an instruction (like 'return' or 'strong_retain')
that does not produce any.
2017-09-25 02:06:26 -04:00

86 lines
2.6 KiB
C++

//===--- ValueOwnershipKindDumper.cpp -------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// This is a simple utility pass that dumps the ValueOwnershipKind of all
/// SILValue in a module. It is meant to trigger assertions and verification of
/// these values.
///
//===----------------------------------------------------------------------===//
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
using namespace swift;
static void checkEnumInstIsTrivial(EnumInst *EI) {
if (!EI->hasOperand())
return;
if (EI->getOperand()->getType().isTrivial(EI->getModule()))
return;
llvm_unreachable("Found enum with non-trivial operand but trivial ownership?!");
}
namespace {
class ValueOwnershipKindDumper : public SILFunctionTransform {
void run() override {
SILFunction *F = getFunction();
SILModule &M = F->getModule();
for (auto &BB : *F) {
// We only verify instructions right now.
for (auto &II : BB) {
// If the instruction doesn't have any results, bail.
auto results = II.getResults();
if (results.empty())
continue;
llvm::outs() << "Visiting: " << II;
for (auto V : results) {
auto Kind = V.getOwnershipKind();
llvm::outs() << " " << Kind << "\n";
if (Kind == ValueOwnershipKind::Any)
continue;
if (Kind == ValueOwnershipKind::Trivial) {
if (auto *EI = dyn_cast<EnumInst>(V)) {
checkEnumInstIsTrivial(EI);
continue;
}
SILType Ty = V->getType();
if (!Ty.isTrivial(M) && !Ty.isAddress()) {
llvm_unreachable("Error! Trivial ownership without trivial type\n");
}
} else {
if (V->getType().isTrivial(M)) {
llvm_unreachable(
"Error! Non Trivial ownership with trivial type\n");
}
}
}
}
}
}
};
} // end anonymous namespace
SILTransform *swift::createValueOwnershipKindDumper() {
return new ValueOwnershipKindDumper();
}