Skip to content

Commit 093c214

Browse files
committed
SILBuilder: require an insertion point when creating instructions.
This helps to avoid instruction leaks. It's a NFC.
1 parent 9fb7769 commit 093c214

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,15 +2358,11 @@ class SILBuilder {
23582358
}
23592359

23602360
void insertImpl(SILInstruction *TheInst) {
2361-
if (BB == 0)
2362-
return;
2363-
2361+
assert(hasValidInsertionPoint());
23642362
BB->insert(InsertPt, TheInst);
23652363

23662364
C.notifyInserted(TheInst);
23672365

2368-
// TODO: We really shouldn't be creating instructions unless we are going to
2369-
// insert them into a block... This failed in SimplifyCFG.
23702366
#ifndef NDEBUG
23712367
TheInst->verifyOperandOwnership();
23722368
#endif

lib/SIL/IR/SILInstruction.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,14 @@ namespace {
11491149
friend class SILCloner<TrivialCloner>;
11501150
friend class SILInstructionVisitor<TrivialCloner>;
11511151
SILInstruction *Result = nullptr;
1152-
TrivialCloner(SILFunction *F) : SILCloner(*F) {}
1152+
TrivialCloner(SILFunction *F, SILInstruction *InsertPt) : SILCloner(*F) {
1153+
Builder.setInsertionPoint(InsertPt);
1154+
}
1155+
11531156
public:
11541157

1155-
static SILInstruction *doIt(SILInstruction *I) {
1156-
TrivialCloner TC(I->getFunction());
1158+
static SILInstruction *doIt(SILInstruction *I, SILInstruction *InsertPt) {
1159+
TrivialCloner TC(I->getFunction(), InsertPt);
11571160
TC.visit(I);
11581161
return TC.Result;
11591162
}
@@ -1204,10 +1207,7 @@ bool SILInstruction::isDeallocatingStack() const {
12041207
/// then the new instruction is inserted before the specified point, otherwise
12051208
/// the new instruction is returned without a parent.
12061209
SILInstruction *SILInstruction::clone(SILInstruction *InsertPt) {
1207-
SILInstruction *NewInst = TrivialCloner::doIt(this);
1208-
1209-
if (NewInst && InsertPt)
1210-
InsertPt->getParent()->insert(InsertPt, NewInst);
1210+
SILInstruction *NewInst = TrivialCloner::doIt(this, InsertPt);
12111211
return NewInst;
12121212
}
12131213

lib/SILOptimizer/IPO/CrossModuleSerializationSetup.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ class InstructionVisitor : public SILCloner<InstructionVisitor> {
9696
SILInstruction *result = nullptr;
9797

9898
public:
99-
InstructionVisitor(SILFunction *F, CrossModuleSerializationSetup &CMS) :
100-
SILCloner(*F), CMS(CMS) {}
99+
InstructionVisitor(SILInstruction *I, CrossModuleSerializationSetup &CMS) :
100+
SILCloner(*I->getFunction()), CMS(CMS) {
101+
Builder.setInsertionPoint(I);
102+
}
101103

102104
SILType remapType(SILType Ty) {
103105
CMS.makeTypeUsableFromInline(Ty.getASTType());
@@ -124,11 +126,9 @@ class InstructionVisitor : public SILCloner<InstructionVisitor> {
124126
SILBasicBlock *remapBasicBlock(SILBasicBlock *BB) { return BB; }
125127

126128
static void visitInst(SILInstruction *I, CrossModuleSerializationSetup &CMS) {
127-
InstructionVisitor visitor(I->getFunction(), CMS);
129+
InstructionVisitor visitor(I, CMS);
128130
visitor.visit(I);
129-
130-
SILInstruction::destroy(visitor.result);
131-
CMS.M.deallocateInst(visitor.result);
131+
visitor.result->eraseFromParent();
132132
}
133133
};
134134

lib/SILOptimizer/IPO/LetPropertiesOpts.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,12 @@ class InitSequenceCloner : public SILClonerWithScopes<InitSequenceCloner> {
190190
friend class SILCloner<InitSequenceCloner>;
191191

192192
const InitSequence &Init;
193-
SILInstruction *DestIP;
194193

195194
public:
196195
InitSequenceCloner(const InitSequence &init, SILInstruction *destIP)
197-
: SILClonerWithScopes(*destIP->getFunction()), Init(init), DestIP(destIP) {}
196+
: SILClonerWithScopes(*destIP->getFunction()), Init(init) {
197+
Builder.setInsertionPoint(destIP);
198+
}
198199

199200
void process(SILInstruction *I) { visit(I); }
200201

@@ -204,12 +205,6 @@ class InitSequenceCloner : public SILClonerWithScopes<InitSequenceCloner> {
204205
return SILCloner<InitSequenceCloner>::getMappedValue(Value);
205206
}
206207

207-
void postProcess(SILInstruction *orig, SILInstruction *cloned) {
208-
DestIP->getParent()->push_front(cloned);
209-
cloned->moveBefore(DestIP);
210-
SILClonerWithScopes<InitSequenceCloner>::postProcess(orig, cloned);
211-
}
212-
213208
/// Clone all the instructions from Insns into the destination function,
214209
/// immediately before the destination block, and return the value of
215210
/// the result.

0 commit comments

Comments
 (0)