mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[cast-opt] Allow users to pass in a SILBuilderContext to the CastOptimizer.
NOTE: I changed all places that the CastOptimizer is created to just pass in nullptr for now so this is NFC. ---- Right now the interface of the CastOptimizer is muddled and confused. Sometimes it is returning a value that should be used by the caller, other times it is returning an instruction that is meant to be reprocessed by the caller. This series of patches is attempting to clean this up by switching to the following model: 1. If we are optimizing a cast of a value, we return a SILValue. If the cast fails, we return an empty SILValue(). 2. If we are optimizing a cast of an address, we return a boolean value to show success/failure and require the user to use the SILBuilderContext to get the cast if they need to.
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include "swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h"
|
||||
#include "swift/SILOptimizer/Analysis/EpilogueARCAnalysis.h"
|
||||
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
|
||||
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
|
||||
#include "llvm/ADT/SmallPtrSet.h"
|
||||
#include "llvm/Support/Allocator.h"
|
||||
#include <functional>
|
||||
@@ -34,6 +35,12 @@ class SILOptFunctionBuilder;
|
||||
class CastOptimizer {
|
||||
SILOptFunctionBuilder &FunctionBuilder;
|
||||
|
||||
/// Temporary context for clients that do not provide their own.
|
||||
SILBuilderContext TempBuilderContext;
|
||||
|
||||
/// Reference to the provided SILBuilderContext.
|
||||
SILBuilderContext &BuilderContext;
|
||||
|
||||
/// Callback that replaces the first SILValue's uses with a use of the second
|
||||
/// value.
|
||||
std::function<void(SILValue, SILValue)> ReplaceValueUsesAction;
|
||||
@@ -75,6 +82,7 @@ class CastOptimizer {
|
||||
|
||||
public:
|
||||
CastOptimizer(SILOptFunctionBuilder &FunctionBuilder,
|
||||
SILBuilderContext *BuilderContext,
|
||||
std::function<void(SILValue, SILValue)> ReplaceValueUsesAction,
|
||||
std::function<void(SingleValueInstruction *, ValueBase *)>
|
||||
ReplaceInstUsesAction,
|
||||
@@ -82,6 +90,8 @@ public:
|
||||
std::function<void()> WillSucceedAction,
|
||||
std::function<void()> WillFailAction = []() {})
|
||||
: FunctionBuilder(FunctionBuilder),
|
||||
TempBuilderContext(FunctionBuilder.getModule()),
|
||||
BuilderContext(BuilderContext ? *BuilderContext : TempBuilderContext),
|
||||
ReplaceValueUsesAction(ReplaceValueUsesAction),
|
||||
ReplaceInstUsesAction(ReplaceInstUsesAction),
|
||||
EraseInstAction(EraseAction), WillSucceedAction(WillSucceedAction),
|
||||
@@ -93,12 +103,13 @@ public:
|
||||
// arguments. It seems the number of the default argument with lambda is
|
||||
// limited.
|
||||
CastOptimizer(SILOptFunctionBuilder &FunctionBuilder,
|
||||
SILBuilderContext *BuilderContext,
|
||||
std::function<void(SILValue, SILValue)> ReplaceValueUsesAction,
|
||||
std::function<void(SingleValueInstruction *I, ValueBase *V)>
|
||||
ReplaceInstUsesAction,
|
||||
std::function<void(SILInstruction *)> EraseAction =
|
||||
[](SILInstruction *) {})
|
||||
: CastOptimizer(FunctionBuilder, ReplaceValueUsesAction,
|
||||
: CastOptimizer(FunctionBuilder, BuilderContext, ReplaceValueUsesAction,
|
||||
ReplaceInstUsesAction, EraseAction, []() {}, []() {}) {}
|
||||
|
||||
/// Simplify checked_cast_br. It may change the control flow.
|
||||
|
||||
Reference in New Issue
Block a user