Skip to content

Commit 891f53d

Browse files
committed
libswift: bridge the MemoryBehavior enum instead of all the mayRead/Write instruction functions
1 parent 7f08508 commit 891f53d

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

include/swift/SIL/SILBridging.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ typedef struct {
119119
SwiftObject obj;
120120
} BridgedMultiValueResult;
121121

122+
// Must be in sync with SILInstruction::MemoryBehavior
123+
// TODO: do this less hacky.
124+
typedef enum {
125+
NoneBehavior,
126+
MayReadBehavior,
127+
MayWriteBehavior,
128+
MayReadWriteBehavior,
129+
MayHaveSideEffectsBehavior
130+
} BridgedMemoryBehavior;
131+
122132
typedef long SwiftInt;
123133

124134
void registerBridgedClass(BridgedStringRef className, SwiftMetatype metatype);
@@ -166,10 +176,7 @@ OptionalBridgedInstruction SILInstruction_previous(BridgedInstruction inst);
166176
BridgedBasicBlock SILInstruction_getParent(BridgedInstruction inst);
167177
BridgedOperandArray SILInstruction_getOperands(BridgedInstruction inst);
168178
BridgedLocation SILInstruction_getLocation(BridgedInstruction inst);
169-
int SILInstruction_mayHaveSideEffects(BridgedInstruction inst);
170-
int SILInstruction_mayReadFromMemory(BridgedInstruction inst);
171-
int SILInstruction_mayWriteToMemory(BridgedInstruction inst);
172-
int SILInstruction_mayReadOrWriteMemory(BridgedInstruction inst);
179+
BridgedMemoryBehavior SILInstruction_getMemBehavior(BridgedInstruction inst);
173180

174181
BridgedInstruction MultiValueInstResult_getParent(BridgedMultiValueResult result);
175182
SwiftInt MultipleValueInstruction_getNumResults(BridgedInstruction inst);

lib/SIL/Utils/SILBridging.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,8 @@ BridgedLocation SILInstruction_getLocation(BridgedInstruction inst) {
326326
return *reinterpret_cast<BridgedLocation *>(&loc);
327327
}
328328

329-
int SILInstruction_mayHaveSideEffects(BridgedInstruction inst) {
330-
return castToInst(inst)->mayHaveSideEffects();
331-
}
332-
int SILInstruction_mayReadFromMemory(BridgedInstruction inst) {
333-
return castToInst(inst)->mayReadFromMemory();
334-
}
335-
int SILInstruction_mayWriteToMemory(BridgedInstruction inst) {
336-
return castToInst(inst)->mayWriteToMemory();
337-
}
338-
int SILInstruction_mayReadOrWriteMemory(BridgedInstruction inst) {
339-
return castToInst(inst)->mayReadOrWriteMemory();
329+
BridgedMemoryBehavior SILInstruction_getMemBehavior(BridgedInstruction inst) {
330+
return (BridgedMemoryBehavior)castToInst(inst)->getMemoryBehavior();
340331
}
341332

342333
BridgedInstruction MultiValueInstResult_getParent(BridgedMultiValueResult result) {

libswift/Sources/SIL/Instruction.swift

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,38 @@ public class Instruction : ListNode, CustomStringConvertible {
4646
return Location(bridgedLocation: SILInstruction_getLocation(bridged))
4747
}
4848

49+
public var mayTrap: Bool { false }
50+
4951
final public var mayHaveSideEffects: Bool {
50-
return SILInstruction_mayHaveSideEffects(bridged) != 0
52+
return mayTrap || mayWriteToMemory
5153
}
54+
5255
final public var mayReadFromMemory: Bool {
53-
return SILInstruction_mayReadFromMemory(bridged) != 0
56+
switch SILInstruction_getMemBehavior(bridged) {
57+
case MayReadBehavior, MayReadWriteBehavior, MayHaveSideEffectsBehavior:
58+
return true
59+
default:
60+
return false
61+
}
5462
}
63+
5564
final public var mayWriteToMemory: Bool {
56-
return SILInstruction_mayWriteToMemory(bridged) != 0
65+
switch SILInstruction_getMemBehavior(bridged) {
66+
case MayWriteBehavior, MayReadWriteBehavior, MayHaveSideEffectsBehavior:
67+
return true
68+
default:
69+
return false
70+
}
5771
}
72+
5873
final public var mayReadOrWriteMemory: Bool {
59-
return SILInstruction_mayReadOrWriteMemory(bridged) != 0
74+
switch SILInstruction_getMemBehavior(bridged) {
75+
case MayReadBehavior, MayWriteBehavior, MayReadWriteBehavior,
76+
MayHaveSideEffectsBehavior:
77+
return true
78+
default:
79+
return false
80+
}
6081
}
6182

6283
public var bridged: BridgedInstruction {
@@ -143,6 +164,8 @@ final public class UnimplementedInstruction : Instruction {
143164
final public class CondFailInst : Instruction {
144165
public var condition: Value { operands[0].value }
145166

167+
public override var mayTrap: Bool { true }
168+
146169
public var message: String { CondFailInst_getMessage(bridged).string }
147170
}
148171

0 commit comments

Comments
 (0)