mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
The XXOptUtils.h convention is already established and parallels the SIL/XXUtils convention. New: - InstOptUtils.h - CFGOptUtils.h - BasicBlockOptUtils.h - ValueLifetime.h Removed: - Local.h - Two conflicting CFG.h files This reorganization is helpful before I introduce more utilities for block cloning similar to SinkAddressProjections. Move the control flow utilies out of Local.h, which was an unreadable, unprincipled mess. Rename it to InstOptUtils.h, and confine it to small APIs for working with individual instructions. These are the optimizer's additions to /SIL/InstUtils.h. Rename CFG.h to CFGOptUtils.h and remove the one in /Analysis. Now there is only SIL/CFG.h, resolving the naming conflict within the swift project (this has always been a problem for source tools). Limit this header to low-level APIs for working with branches and CFG edges. Add BasicBlockOptUtils.h for block level transforms (it makes me sad that I can't use BBOptUtils.h, but SIL already has BasicBlockUtils.h). These are larger APIs for cloning or removing whole blocks.
113 lines
4.0 KiB
C++
113 lines
4.0 KiB
C++
//===--- GenericCloner.h - Specializes generic functions -------*- C++ -*-===//
|
|
//
|
|
// 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 contains the definition of a cloner class for creating specialized
|
|
// versions of generic functions by substituting concrete types.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_SIL_GENERICCLONER_H
|
|
#define SWIFT_SIL_GENERICCLONER_H
|
|
|
|
#include "swift/AST/Type.h"
|
|
#include "swift/SIL/SILFunction.h"
|
|
#include "swift/SIL/SILInstruction.h"
|
|
#include "swift/SIL/TypeSubstCloner.h"
|
|
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
|
|
#include "swift/SILOptimizer/Utils/Generics.h"
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include <functional>
|
|
|
|
namespace swift {
|
|
|
|
class GenericCloner
|
|
: public TypeSubstCloner<GenericCloner, SILOptFunctionBuilder> {
|
|
using SuperTy = TypeSubstCloner<GenericCloner, SILOptFunctionBuilder>;
|
|
|
|
SILOptFunctionBuilder &FuncBuilder;
|
|
IsSerialized_t Serialized;
|
|
const ReabstractionInfo &ReInfo;
|
|
CloneCollector::CallbackType Callback;
|
|
llvm::SmallDenseMap<const SILDebugScope *, const SILDebugScope *, 8>
|
|
RemappedScopeCache;
|
|
|
|
llvm::SmallVector<AllocStackInst *, 8> AllocStacks;
|
|
AllocStackInst *ReturnValueAddr = nullptr;
|
|
|
|
public:
|
|
friend class SILCloner<GenericCloner>;
|
|
|
|
GenericCloner(SILOptFunctionBuilder &FuncBuilder,
|
|
SILFunction *F,
|
|
const ReabstractionInfo &ReInfo,
|
|
SubstitutionMap ParamSubs,
|
|
StringRef NewName,
|
|
CloneCollector::CallbackType Callback)
|
|
: SuperTy(*initCloned(FuncBuilder, F, ReInfo, NewName), *F,
|
|
ParamSubs), FuncBuilder(FuncBuilder), ReInfo(ReInfo), Callback(Callback) {
|
|
assert(F->getDebugScope()->Parent != getCloned()->getDebugScope()->Parent);
|
|
}
|
|
/// Clone and remap the types in \p F according to the substitution
|
|
/// list in \p Subs. Parameters are re-abstracted (changed from indirect to
|
|
/// direct) according to \p ReInfo.
|
|
static SILFunction *
|
|
cloneFunction(SILOptFunctionBuilder &FuncBuilder,
|
|
SILFunction *F,
|
|
const ReabstractionInfo &ReInfo,
|
|
SubstitutionMap ParamSubs,
|
|
StringRef NewName,
|
|
CloneCollector::CallbackType Callback =nullptr) {
|
|
// Clone and specialize the function.
|
|
GenericCloner SC(FuncBuilder, F, ReInfo, ParamSubs,
|
|
NewName, Callback);
|
|
SC.populateCloned();
|
|
return SC.getCloned();
|
|
}
|
|
|
|
void fixUp(SILFunction *calleeFunction);
|
|
|
|
protected:
|
|
void visitTerminator(SILBasicBlock *BB);
|
|
|
|
// FIXME: We intentionally call SILClonerWithScopes here to ensure
|
|
// the debug scopes are set correctly for cloned
|
|
// functions. TypeSubstCloner, SILClonerWithScopes, and
|
|
// SILCloner desperately need refactoring and/or combining so
|
|
// that the obviously right things are happening for cloning
|
|
// vs. inlining.
|
|
void postProcess(SILInstruction *Orig, SILInstruction *Cloned) {
|
|
// Call client-supplied callback function.
|
|
if (Callback)
|
|
Callback(Orig, Cloned);
|
|
|
|
SILClonerWithScopes<GenericCloner>::postProcess(Orig, Cloned);
|
|
}
|
|
|
|
private:
|
|
static SILFunction *initCloned(SILOptFunctionBuilder &FuncBuilder,
|
|
SILFunction *Orig,
|
|
const ReabstractionInfo &ReInfo,
|
|
StringRef NewName);
|
|
/// Clone the body of the function into the empty function that was created
|
|
/// by initCloned.
|
|
void populateCloned();
|
|
SILFunction *getCloned() { return &getBuilder().getFunction(); }
|
|
|
|
const SILDebugScope *remapScope(const SILDebugScope *DS);
|
|
|
|
};
|
|
|
|
} // end namespace swift
|
|
|
|
#endif
|