Files
swift-mirror/lib/SILOptimizer/UtilityPasses/SimplifyUnreachableContainingBlocks.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

62 lines
2.1 KiB
C++

//===--- SimplifyUnreachableContainingBlocks.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
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// This file contains a simple utility pass that simplifies blocks that contain
/// unreachables by eliminating all other instructions. This includes
/// instructions with side-effects and no-return functions. It is only intended
/// to be used to simplify IR for testing or exploratory purposes.
///
//===----------------------------------------------------------------------===//
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
using namespace swift;
namespace {
class SimplifyUnreachableContainingBlocks : public SILFunctionTransform {
void run() override {
// For each block...
for (auto &BB : *getFunction()) {
// If the block does not contain an unreachable, just continue. There is
// no further work to do.
auto *UI = dyn_cast<UnreachableInst>(BB.getTerminator());
if (!UI)
continue;
// Otherwise, eliminate all other instructions in the block.
for (auto II = BB.begin(); &*II != UI;) {
// Avoid iterator invalidation.
auto *I = &*II;
++II;
I->replaceAllUsesOfAllResultsWithUndef();
I->eraseFromParent();
}
}
}
};
} // end anonymous namespace
//===----------------------------------------------------------------------===//
// Top Level Entry Point
//===----------------------------------------------------------------------===//
SILTransform *swift::createSimplifyUnreachableContainingBlocks() {
return new SimplifyUnreachableContainingBlocks();
}