Files
swift-mirror/lib/SIL/SILBasicBlock.cpp
Mark Lacey 48b0e18e23 Hook instruction insertion & removal.
Call back to the SILModule to notify of instructions being inserted or
removed from basic blocks.

The intent is to use this to notify the call graph (if one exists and
the module knows about it). We can then use these notifications to
automatically update the call graph.

Swift SVN r32359
2015-10-01 02:43:33 +00:00

183 lines
5.7 KiB
C++

//===--- SILBasicBlock.cpp - Basic blocks for high-level SIL code ----------==//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the high-level BasicBlocks used for Swift SIL code.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/STLExtras.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILDebugScope.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILModule.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// SILBasicBlock Implementation
//===----------------------------------------------------------------------===//
SILBasicBlock::SILBasicBlock(SILFunction *parent, SILBasicBlock *afterBB)
: Parent(parent), PredList(0) {
if (afterBB) {
parent->getBlocks().insertAfter(afterBB, this);
} else {
parent->getBlocks().push_back(this);
}
}
SILBasicBlock::~SILBasicBlock() {
// Notify the module of the removal of the instructions before
// deleting the list.
for (auto I = begin(), E = end(); I != E; ++I)
getModule().instructionRemoved(&*I);
// iplist's destructor is going to destroy the InstList.
}
int SILBasicBlock::getDebugID() {
if (!getParent())
return -1;
int idx = 0;
for (const SILBasicBlock &B : *getParent()) {
if (&B == this)
return idx;
idx++;
}
llvm_unreachable("block not in function's block list");
}
SILModule &SILBasicBlock::getModule() const {
return getParent()->getModule();
}
void SILBasicBlock::insert(iterator InsertPt, SILInstruction *I) {
InstList.insert(InsertPt, I);
// Notify module after insertion.
getModule().instructionAdded(I);
}
void SILBasicBlock::push_back(SILInstruction *I) {
InstList.push_back(I);
// Notify module after insertion.
getModule().instructionAdded(I);
}
void SILBasicBlock::push_front(SILInstruction *I) {
InstList.push_front(I);
// Notify module after insertion.
getModule().instructionAdded(I);
}
void SILBasicBlock::remove(SILInstruction *I) {
// Notify module before removal.
getModule().instructionRemoved(I);
InstList.remove(I);
}
void SILBasicBlock::erase(SILInstruction *I) {
// Notify module before removal.
getModule().instructionRemoved(I);
InstList.erase(I);
}
/// This method unlinks 'self' from the containing SILFunction and deletes it.
void SILBasicBlock::eraseFromParent() {
getParent()->getBlocks().erase(this);
}
/// This method unlinks 'self' from the containing SILFunction.
void SILBasicBlock::removeFromParent() {
getParent()->getBlocks().remove(this);
}
/// Replace the ith BB argument with a new one with type Ty (and optional
/// ValueDecl D).
SILArgument *SILBasicBlock::replaceBBArg(unsigned i, SILType Ty,
const ValueDecl *D) {
SILModule &M = getParent()->getModule();
assert(BBArgList[i]->use_empty() && "Expected no uses of the old BB arg!");
auto *NewArg = new (M) SILArgument(Ty, D);
NewArg->setParent(this);
BBArgList[i] = NewArg;
return NewArg;
}
SILArgument *SILBasicBlock::createBBArg(SILType Ty, const ValueDecl *D) {
return new (getModule()) SILArgument(this, Ty, D);
}
SILArgument *SILBasicBlock::insertBBArg(bbarg_iterator Iter, SILType Ty,
const ValueDecl *D) {
return new (getModule()) SILArgument(this, Iter, Ty, D);
}
/// \brief Splits a basic block into two at the specified instruction.
///
/// Note that all the instructions BEFORE the specified iterator
/// stay as part of the original basic block. The old basic block is left
/// without a terminator.
SILBasicBlock *SILBasicBlock::splitBasicBlock(iterator I) {
SILBasicBlock *New = new (Parent->getModule()) SILBasicBlock(Parent);
SILFunction::iterator Where = std::next(SILFunction::iterator(this));
SILFunction::iterator First = SILFunction::iterator(New);
if (Where != First)
Parent->getBlocks().splice(Where, Parent->getBlocks(), First);
// Move all of the specified instructions from the original basic block into
// the new basic block.
New->InstList.splice(New->end(), InstList, I, end());
return New;
}
/// \brief Move the basic block to after the specified basic block in the IR.
void SILBasicBlock::moveAfter(SILBasicBlock *After) {
assert(getParent() && getParent() == After->getParent() &&
"Blocks must be in the same function");
auto InsertPt = std::next(SILFunction::iterator(After));
auto &BlkList = getParent()->getBlocks();
BlkList.splice(InsertPt, BlkList, this);
}
void
llvm::ilist_traits<swift::SILBasicBlock>::
transferNodesFromList(llvm::ilist_traits<SILBasicBlock> &SrcTraits,
llvm::ilist_iterator<SILBasicBlock> First,
llvm::ilist_iterator<SILBasicBlock> Last) {
assert(&Parent->getModule() == &SrcTraits.Parent->getModule() &&
"Module mismatch!");
// If we are asked to splice into the same function, don't update parent
// pointers.
if (Parent == SrcTraits.Parent) return;
ScopeCloner ScopeCloner(*Parent);
// If splicing blocks not in the same function, update the parent pointers.
for (; First != Last; ++First) {
First->Parent = Parent;
for (auto &II : *First)
II.setDebugScope(ScopeCloner.getOrCreateClonedScope(II.getDebugScope()));
}
}
bool SILBasicBlock::isEntry() const {
return this == &*getParent()->begin();
}