Skip to content

Commit 27dc5af

Browse files
committed
libswift: add a few utilities
* add the IntegerLiteralInst + the Builder function to create it * add Value.nonDebugUses * add a general utility Sequence.isEmpty * add PassContext.replaceAllUses * add a function to erase all `debug_value` uses in PassContext.erase
1 parent 97424b7 commit 27dc5af

File tree

11 files changed

+86
-3
lines changed

11 files changed

+86
-3
lines changed

include/swift/SIL/SILBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ BridgedInstruction SILBuilder_createBuiltinBinaryFunction(
237237
BridgedType operandType, BridgedType resultType, BridgedValueArray arguments);
238238
BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
239239
BridgedLocation loc, BridgedValue condition, BridgedStringRef messge);
240+
BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionPoint,
241+
BridgedLocation loc, BridgedType type, SwiftInt value);
240242

241243
SWIFT_END_NULLABILITY_ANNOTATIONS
242244

include/swift/SIL/SILNodes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
469469
LiteralInst, None, DoesNotRelease)
470470
BRIDGED_SINGLE_VALUE_INST(GlobalValueInst, global_value,
471471
LiteralInst, None, DoesNotRelease)
472-
SINGLE_VALUE_INST(IntegerLiteralInst, integer_literal,
472+
BRIDGED_SINGLE_VALUE_INST(IntegerLiteralInst, integer_literal,
473473
LiteralInst, None, DoesNotRelease)
474474
SINGLE_VALUE_INST(FloatLiteralInst, float_literal,
475475
LiteralInst, None, DoesNotRelease)

lib/SIL/Utils/SILBridging.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,11 @@ BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
513513
return {builder.createCondFail(getRegularLocation(loc),
514514
castToSILValue(condition), getStringRef(messge))};
515515
}
516+
517+
BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionPoint,
518+
BridgedLocation loc, BridgedType type, SwiftInt value) {
519+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
520+
return {builder.createIntegerLiteral(getRegularLocation(loc),
521+
getSILType(type), value)};
522+
}
523+

libswift/Sources/Optimizer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ add_subdirectory(Analysis)
1313
add_subdirectory(InstructionPasses)
1414
add_subdirectory(PassManager)
1515
add_subdirectory(FunctionPasses)
16+
add_subdirectory(Utilities)

libswift/Sources/Optimizer/PassManager/PassUtils.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,23 @@ struct PassContext {
3636
return CalleeAnalysis(bridged: bridgeCA)
3737
}
3838

39-
func erase(instruction: Instruction) {
39+
enum EraseMode {
40+
case onlyInstruction, includingDebugUses
41+
}
42+
43+
func erase(instruction: Instruction, _ mode: EraseMode = .onlyInstruction) {
44+
switch mode {
45+
case .onlyInstruction:
46+
break
47+
case .includingDebugUses:
48+
for result in instruction.results {
49+
for use in result.uses {
50+
assert(use.instruction is DebugValueInst)
51+
PassContext_eraseInstruction(passContext, use.instruction.bridged)
52+
}
53+
}
54+
}
55+
4056
if instruction is FullApplySite {
4157
PassContext_notifyChanges(passContext, callsChanged)
4258
}
@@ -47,7 +63,13 @@ struct PassContext {
4763

4864
PassContext_eraseInstruction(passContext, instruction.bridged)
4965
}
50-
66+
67+
func replaceAllUses(of value: Value, with replacement: Value) {
68+
for use in value.uses {
69+
setOperand(of: use.instruction, at: use.index, to: replacement)
70+
}
71+
}
72+
5173
func setOperand(of instruction: Instruction, at index : Int, to value: Value) {
5274
if instruction is FullApplySite && index == ApplyOperands.calleeOperandIndex {
5375
PassContext_notifyChanges(passContext, callsChanged)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
libswift_sources(Optimizer
10+
OptUtils.swift)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===--- OptUtils.swift - Utilities for optimzations ----------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 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+
import SIL
14+
15+
extension Value {
16+
var nonDebugUses: LazyFilterSequence<UseList> {
17+
uses.lazy.filter { !($0.instruction is DebugValueInst) }
18+
}
19+
}

libswift/Sources/SIL/Builder.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,11 @@ public struct Builder {
6060
return cf.getAs(CondFailInst.self)
6161
}
6262
}
63+
64+
public func createIntegerLiteral(_ value: Int, type: Type) -> IntegerLiteralInst {
65+
notifyInstructionsChanged()
66+
let literal = SILBuilder_createIntegerLiteral(
67+
bridgedInsPoint, location.bridgedLocation, type.bridged, value)
68+
return literal.getAs(IntegerLiteralInst.self)
69+
}
6370
}

libswift/Sources/SIL/Instruction.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ final public class GlobalAddrInst : GlobalAccessInst {}
326326

327327
final public class GlobalValueInst : GlobalAccessInst {}
328328

329+
final public class IntegerLiteralInst : SingleValueInstruction {}
330+
329331
final public class TupleInst : SingleValueInstruction {
330332
}
331333

libswift/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public func registerSILClasses() {
7777
register(ExistentialMetatypeInst.self)
7878
register(GlobalAddrInst.self)
7979
register(GlobalValueInst.self)
80+
register(IntegerLiteralInst.self)
8081
register(TupleInst.self)
8182
register(TupleExtractInst.self)
8283
register(TupleElementAddrInst.self)

0 commit comments

Comments
 (0)