@@ -138,10 +138,16 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
138
138
&& " cloned instruction dropped debug scope" );
139
139
}
140
140
141
- // / Visitor callback that maps an original value to an existing value. Called
142
- // / whenever the visitor that clones an instruction skips doPostProcess().
141
+ // / Visitor callback that maps an original value to an existing value. If the
142
+ // / original instruction can be folded away in the cloned code, then the
143
+ // / instruction visitor should call this instead of cloning the instruction
144
+ // / and calling doPostProcess(). foldValue() and doPostProcess() are the only
145
+ // / two ways for a visitor to remap an original value that may be used within
146
+ // / the cloned region.
143
147
void foldValue (SILValue origValue, SILValue mappedValue) {
144
- ValueMap.insert ({origValue, mappedValue});
148
+ auto iterAndInserted = ValueMap.insert ({origValue, mappedValue});
149
+ (void )iterAndInserted;
150
+ assert (iterAndInserted.second && " Original value already mapped." );
145
151
}
146
152
147
153
// / Mark a block containing an unreachable instruction for use in the `fixUp`
@@ -316,7 +322,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
316
322
// terminator.
317
323
void visitInstructionsInBlock (SILBasicBlock *BB);
318
324
319
- // Visit a block's terminator. This is called with each block in DFS predorder
325
+ // Visit a block's terminator. This is called with each block in DFS preorder
320
326
// after visiting and mapping all basic blocks and after visiting all
321
327
// non-terminator instructions in the block.
322
328
void visitTerminator (SILBasicBlock *BB) {
@@ -623,18 +629,14 @@ void SILCloner<ImplClass>::visitBlocksDepthFirst(
623
629
continue ;
624
630
625
631
// Map the successor to a new BB.
626
- auto *MappedBB = newF.createBasicBlock ();
632
+ auto *MappedBB = insertBeforeBB
633
+ ? newF.createBasicBlockBefore (insertBeforeBB)
634
+ : newF.createBasicBlock ();
635
+
627
636
BBMap.insert (std::make_pair (Succ.getBB (), MappedBB));
628
637
629
638
clonePhiArgs (Succ);
630
639
631
- // Also, move the new mapped BB to the right position in the caller
632
- if (insertBeforeBB) {
633
- newF.getBlocks ().splice (SILFunction::iterator (insertBeforeBB),
634
- newF.getBlocks (),
635
- SILFunction::iterator (MappedBB));
636
- }
637
-
638
640
dfsWorklist.push_back (Succ);
639
641
}
640
642
// Reverse the worklist to pop the successors in forward order.
0 commit comments