Skip to content

Commit 0d68e6e

Browse files
authored
Merge pull request #13932 from gottesmm/pr-f7d51d76d90c593b876ea658427a26fbadd64ba2
2 parents eace034 + dfd40e4 commit 0d68e6e

File tree

8 files changed

+1702
-1688
lines changed

8 files changed

+1702
-1688
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//===--- CastOptimizer.h --------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_SILOPTIMIZER_UTILS_CASTOPTIMIZER_H
14+
#define SWIFT_SILOPTIMIZER_UTILS_CASTOPTIMIZER_H
15+
16+
#include "swift/Basic/ArrayRefView.h"
17+
#include "swift/SIL/SILBuilder.h"
18+
#include "swift/SIL/SILCloner.h"
19+
#include "swift/SIL/SILInstruction.h"
20+
#include "swift/SILOptimizer/Analysis/ARCAnalysis.h"
21+
#include "swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h"
22+
#include "swift/SILOptimizer/Analysis/EpilogueARCAnalysis.h"
23+
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
24+
#include "llvm/ADT/SmallPtrSet.h"
25+
#include "llvm/Support/Allocator.h"
26+
#include <functional>
27+
#include <utility>
28+
29+
namespace swift {
30+
31+
/// \brief This is a helper class used to optimize casts.
32+
class CastOptimizer {
33+
// Callback to be called when uses of an instruction should be replaced.
34+
std::function<void(SingleValueInstruction *I, ValueBase *V)>
35+
ReplaceInstUsesAction;
36+
37+
// Callback to call when an instruction needs to be erased.
38+
std::function<void(SILInstruction *)> EraseInstAction;
39+
40+
// Callback to call after an optimization was performed based on the fact
41+
// that a cast will succeed.
42+
std::function<void()> WillSucceedAction;
43+
44+
// Callback to call after an optimization was performed based on the fact
45+
// that a cast will fail.
46+
std::function<void()> WillFailAction;
47+
48+
/// Optimize a cast from a bridged ObjC type into
49+
/// a corresponding Swift type implementing _ObjectiveCBridgeable.
50+
SILInstruction *optimizeBridgedObjCToSwiftCast(
51+
SILInstruction *Inst, bool isConditional, SILValue Src, SILValue Dest,
52+
CanType Source, CanType Target, Type BridgedSourceTy,
53+
Type BridgedTargetTy, SILBasicBlock *SuccessBB, SILBasicBlock *FailureBB);
54+
55+
/// Optimize a cast from a Swift type implementing _ObjectiveCBridgeable
56+
/// into a bridged ObjC type.
57+
SILInstruction *optimizeBridgedSwiftToObjCCast(
58+
SILInstruction *Inst, CastConsumptionKind ConsumptionKind,
59+
bool isConditional, SILValue Src, SILValue Dest, CanType Source,
60+
CanType Target, Type BridgedSourceTy, Type BridgedTargetTy,
61+
SILBasicBlock *SuccessBB, SILBasicBlock *FailureBB);
62+
63+
void deleteInstructionsAfterUnreachable(SILInstruction *UnreachableInst,
64+
SILInstruction *TrapInst);
65+
66+
public:
67+
CastOptimizer(std::function<void(SingleValueInstruction *I, ValueBase *V)>
68+
ReplaceInstUsesAction,
69+
std::function<void(SILInstruction *)> EraseAction,
70+
std::function<void()> WillSucceedAction,
71+
std::function<void()> WillFailAction = []() {})
72+
: ReplaceInstUsesAction(ReplaceInstUsesAction),
73+
EraseInstAction(EraseAction), WillSucceedAction(WillSucceedAction),
74+
WillFailAction(WillFailAction) {}
75+
76+
// This constructor is used in
77+
// 'SILOptimizer/Mandatory/ConstantPropagation.cpp'. MSVC2015 compiler
78+
// couldn't use the single constructor version which has three default
79+
// arguments. It seems the number of the default argument with lambda is
80+
// limited.
81+
CastOptimizer(std::function<void(SingleValueInstruction *I, ValueBase *V)>
82+
ReplaceInstUsesAction,
83+
std::function<void(SILInstruction *)> EraseAction =
84+
[](SILInstruction *) {})
85+
: CastOptimizer(ReplaceInstUsesAction, EraseAction, []() {}, []() {}) {}
86+
87+
/// Simplify checked_cast_br. It may change the control flow.
88+
SILInstruction *simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst);
89+
90+
/// Simplify checked_cast_value_br. It may change the control flow.
91+
SILInstruction *
92+
simplifyCheckedCastValueBranchInst(CheckedCastValueBranchInst *Inst);
93+
94+
/// Simplify checked_cast_addr_br. It may change the control flow.
95+
SILInstruction *
96+
simplifyCheckedCastAddrBranchInst(CheckedCastAddrBranchInst *Inst);
97+
98+
/// Optimize checked_cast_br. This cannot change the control flow.
99+
SILInstruction *optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst);
100+
101+
/// Optimize checked_cast_value_br. This cannot change the control flow.
102+
SILInstruction *
103+
optimizeCheckedCastValueBranchInst(CheckedCastValueBranchInst *Inst);
104+
105+
/// Optimize checked_cast_addr_br. This cannot change the control flow.
106+
SILInstruction *
107+
optimizeCheckedCastAddrBranchInst(CheckedCastAddrBranchInst *Inst);
108+
109+
/// Optimize unconditional_checked_cast. This cannot change the control flow.
110+
ValueBase *
111+
optimizeUnconditionalCheckedCastInst(UnconditionalCheckedCastInst *Inst);
112+
113+
/// Optimize unconditional_checked_cast_addr. This cannot change the control
114+
/// flow.
115+
SILInstruction *optimizeUnconditionalCheckedCastAddrInst(
116+
UnconditionalCheckedCastAddrInst *Inst);
117+
118+
/// Check if it is a bridged cast and optimize it.
119+
/// May change the control flow.
120+
SILInstruction *optimizeBridgedCasts(SILInstruction *Inst,
121+
CastConsumptionKind ConsumptionKind,
122+
bool isConditional, SILValue Src,
123+
SILValue Dest, CanType Source,
124+
CanType Target, SILBasicBlock *SuccessBB,
125+
SILBasicBlock *FailureBB);
126+
};
127+
128+
} // namespace swift
129+
130+
#endif // SWIFT_SILOPTIMIZER_UTILS_CASTOPTIMIZER_H

include/swift/SILOptimizer/Utils/Local.h

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -432,122 +432,6 @@ void updateSSAAfterCloning(BaseThreadingCloner &Cloner, SILBasicBlock *SrcBB,
432432
SILBasicBlock *DestBB,
433433
bool NeedToSplitCriticalEdges = true);
434434

435-
/// \brief This is a helper class used to optimize casts.
436-
class CastOptimizer {
437-
// Callback to be called when uses of an instruction should be replaced.
438-
std::function<void (SingleValueInstruction *I, ValueBase *V)>
439-
ReplaceInstUsesAction;
440-
441-
// Callback to call when an instruction needs to be erased.
442-
std::function<void (SILInstruction *)> EraseInstAction;
443-
444-
// Callback to call after an optimization was performed based on the fact
445-
// that a cast will succeed.
446-
std::function<void ()> WillSucceedAction;
447-
448-
// Callback to call after an optimization was performed based on the fact
449-
// that a cast will fail.
450-
std::function<void ()> WillFailAction;
451-
452-
/// Optimize a cast from a bridged ObjC type into
453-
/// a corresponding Swift type implementing _ObjectiveCBridgeable.
454-
SILInstruction *
455-
optimizeBridgedObjCToSwiftCast(SILInstruction *Inst,
456-
bool isConditional,
457-
SILValue Src,
458-
SILValue Dest,
459-
CanType Source,
460-
CanType Target,
461-
Type BridgedSourceTy,
462-
Type BridgedTargetTy,
463-
SILBasicBlock *SuccessBB,
464-
SILBasicBlock *FailureBB);
465-
466-
/// Optimize a cast from a Swift type implementing _ObjectiveCBridgeable
467-
/// into a bridged ObjC type.
468-
SILInstruction *
469-
optimizeBridgedSwiftToObjCCast(SILInstruction *Inst,
470-
CastConsumptionKind ConsumptionKind,
471-
bool isConditional,
472-
SILValue Src,
473-
SILValue Dest,
474-
CanType Source,
475-
CanType Target,
476-
Type BridgedSourceTy,
477-
Type BridgedTargetTy,
478-
SILBasicBlock *SuccessBB,
479-
SILBasicBlock *FailureBB);
480-
481-
void deleteInstructionsAfterUnreachable(SILInstruction *UnreachableInst,
482-
SILInstruction *TrapInst);
483-
484-
public:
485-
CastOptimizer(std::function<void (SingleValueInstruction *I, ValueBase *V)> ReplaceInstUsesAction,
486-
std::function<void (SILInstruction *)> EraseAction,
487-
std::function<void ()> WillSucceedAction,
488-
std::function<void ()> WillFailAction = [](){})
489-
: ReplaceInstUsesAction(ReplaceInstUsesAction),
490-
EraseInstAction(EraseAction),
491-
WillSucceedAction(WillSucceedAction),
492-
WillFailAction(WillFailAction) {}
493-
494-
// This constructor is used in
495-
// 'SILOptimizer/Mandatory/ConstantPropagation.cpp'. MSVC2015 compiler
496-
// couldn't use the single constructor version which has three default
497-
// arguments. It seems the number of the default argument with lambda is
498-
// limited.
499-
CastOptimizer(std::function<void (SingleValueInstruction *I, ValueBase *V)> ReplaceInstUsesAction,
500-
std::function<void (SILInstruction *)> EraseAction = [](SILInstruction*){})
501-
: CastOptimizer(ReplaceInstUsesAction, EraseAction, [](){}, [](){}) {}
502-
503-
/// Simplify checked_cast_br. It may change the control flow.
504-
SILInstruction *
505-
simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst);
506-
507-
/// Simplify checked_cast_value_br. It may change the control flow.
508-
SILInstruction *
509-
simplifyCheckedCastValueBranchInst(CheckedCastValueBranchInst *Inst);
510-
511-
/// Simplify checked_cast_addr_br. It may change the control flow.
512-
SILInstruction *
513-
simplifyCheckedCastAddrBranchInst(CheckedCastAddrBranchInst *Inst);
514-
515-
/// Optimize checked_cast_br. This cannot change the control flow.
516-
SILInstruction *
517-
optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst);
518-
519-
/// Optimize checked_cast_value_br. This cannot change the control flow.
520-
SILInstruction *
521-
optimizeCheckedCastValueBranchInst(CheckedCastValueBranchInst *Inst);
522-
523-
/// Optimize checked_cast_addr_br. This cannot change the control flow.
524-
SILInstruction *
525-
optimizeCheckedCastAddrBranchInst(CheckedCastAddrBranchInst *Inst);
526-
527-
/// Optimize unconditional_checked_cast. This cannot change the control flow.
528-
ValueBase *
529-
optimizeUnconditionalCheckedCastInst(UnconditionalCheckedCastInst *Inst);
530-
531-
/// Optimize unconditional_checked_cast_addr. This cannot change the control
532-
/// flow.
533-
SILInstruction *
534-
optimizeUnconditionalCheckedCastAddrInst(UnconditionalCheckedCastAddrInst *Inst);
535-
536-
/// Check if it is a bridged cast and optimize it.
537-
/// May change the control flow.
538-
SILInstruction *
539-
optimizeBridgedCasts(SILInstruction *Inst,
540-
CastConsumptionKind ConsumptionKind,
541-
bool isConditional,
542-
SILValue Src,
543-
SILValue Dest,
544-
CanType Source,
545-
CanType Target,
546-
SILBasicBlock *SuccessBB,
547-
SILBasicBlock *FailureBB);
548-
549-
};
550-
551435
// Helper class that provides a callback that can be used in
552436
// inliners/cloners for collecting new call sites.
553437
class CloneCollector {

lib/SILOptimizer/Mandatory/ConstantPropagation.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#define DEBUG_TYPE "constant-propagation"
14-
#include "swift/SILOptimizer/PassManager/Passes.h"
1514
#include "swift/AST/DiagnosticsSIL.h"
1615
#include "swift/AST/Expr.h"
1716
#include "swift/SIL/PatternMatch.h"
1817
#include "swift/SIL/SILBuilder.h"
1918
#include "swift/SIL/SILInstruction.h"
20-
#include "swift/SILOptimizer/Utils/Local.h"
21-
#include "swift/SILOptimizer/Utils/ConstantFolding.h"
19+
#include "swift/SILOptimizer/PassManager/Passes.h"
2220
#include "swift/SILOptimizer/PassManager/Transforms.h"
21+
#include "swift/SILOptimizer/Utils/CastOptimizer.h"
22+
#include "swift/SILOptimizer/Utils/ConstantFolding.h"
23+
#include "swift/SILOptimizer/Utils/Local.h"
2324
#include "llvm/ADT/Statistic.h"
2425
#include "llvm/ADT/StringSwitch.h"
25-
#include "llvm/Support/Debug.h"
2626
#include "llvm/Support/CommandLine.h"
27+
#include "llvm/Support/Debug.h"
2728
using namespace swift;
2829
using namespace swift::PatternMatch;
2930

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
#ifndef SWIFT_SILOPTIMIZER_PASSMANAGER_SILCOMBINER_H
2222
#define SWIFT_SILOPTIMIZER_PASSMANAGER_SILCOMBINER_H
2323

24+
#include "swift/SIL/SILBuilder.h"
2425
#include "swift/SIL/SILInstruction.h"
2526
#include "swift/SIL/SILValue.h"
26-
#include "swift/SIL/SILBuilder.h"
2727
#include "swift/SIL/SILVisitor.h"
28+
#include "swift/SILOptimizer/Utils/CastOptimizer.h"
2829
#include "swift/SILOptimizer/Utils/Local.h"
29-
#include "llvm/ADT/SmallVector.h"
3030
#include "llvm/ADT/DenseMap.h"
31+
#include "llvm/ADT/SmallVector.h"
3132

3233
namespace swift {
3334

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,28 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#define DEBUG_TYPE "sil-simplify-cfg"
14-
#include "swift/SILOptimizer/PassManager/Passes.h"
14+
#include "swift/SIL/DebugUtils.h"
1515
#include "swift/SIL/Dominance.h"
16+
#include "swift/SIL/InstructionUtils.h"
1617
#include "swift/SIL/Projection.h"
1718
#include "swift/SIL/SILArgument.h"
1819
#include "swift/SIL/SILModule.h"
1920
#include "swift/SIL/SILUndef.h"
20-
#include "swift/SIL/DebugUtils.h"
21-
#include "swift/SIL/InstructionUtils.h"
2221
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
23-
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
2422
#include "swift/SILOptimizer/Analysis/ProgramTerminationAnalysis.h"
23+
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
24+
#include "swift/SILOptimizer/PassManager/Passes.h"
2525
#include "swift/SILOptimizer/PassManager/Transforms.h"
2626
#include "swift/SILOptimizer/Utils/CFG.h"
27+
#include "swift/SILOptimizer/Utils/CastOptimizer.h"
2728
#include "swift/SILOptimizer/Utils/Local.h"
2829
#include "swift/SILOptimizer/Utils/SILInliner.h"
2930
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
3031
#include "llvm/ADT/SmallPtrSet.h"
3132
#include "llvm/ADT/SmallVector.h"
3233
#include "llvm/ADT/Statistic.h"
33-
#include "llvm/Support/Debug.h"
3434
#include "llvm/Support/CommandLine.h"
35+
#include "llvm/Support/Debug.h"
3536
using namespace swift;
3637

3738
STATISTIC(NumBlocksDeleted, "Number of unreachable blocks removed");

lib/SILOptimizer/Utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(UTILS_SOURCES
22
Utils/CFG.cpp
3+
Utils/CastOptimizer.cpp
34
Utils/CheckedCastBrJumpThreading.cpp
45
Utils/ConstantFolding.cpp
56
Utils/Devirtualize.cpp

0 commit comments

Comments
 (0)