Reduce instruction create and deletion during inlining.
authorSteven Perron <stevenperron@google.com>
Tue, 20 Feb 2018 16:24:08 +0000 (11:24 -0500)
committerSteven Perron <stevenperron@google.com>
Wed, 21 Feb 2018 14:50:47 +0000 (09:50 -0500)
When inlining a function call the instructions in the same basic block
as the call get cloned.  The clone is added to the set of new blocks
containing the inlined code, and the original instructions are deleted.

This PR will change this so that we simply move the instructions to the
new blocks.  This saves on the creation and deletion of the
instructions.

Contributes to #1328.

source/opt/inline_pass.cpp

index b713852..2d5b27c 100644 (file)
@@ -317,8 +317,10 @@ void InlinePass::GenInlineCode(
         if (firstBlock) {
           // Copy contents of original caller block up to call instruction.
           for (auto cii = call_block_itr->begin(); cii != call_inst_itr;
-               ++cii) {
-            std::unique_ptr<ir::Instruction> cp_inst(cii->Clone(context()));
+               cii = call_block_itr->begin()) {
+            ir::Instruction* inst = &*cii;
+            inst->RemoveFromList();
+            std::unique_ptr<ir::Instruction> cp_inst(inst);
             // Remember same-block ops for possible regeneration.
             if (IsSameBlockOp(&*cp_inst)) {
               auto* sb_inst_ptr = cp_inst.get();
@@ -425,9 +427,10 @@ void InlinePass::GenInlineCode(
           AddLoad(calleeTypeId, resId, returnVarId, &new_blk_ptr);
         }
         // Copy remaining instructions from caller block.
-        auto cii = call_inst_itr;
-        for (++cii; cii != call_block_itr->end(); ++cii) {
-          std::unique_ptr<ir::Instruction> cp_inst(cii->Clone(context()));
+        for (ir::Instruction* inst = call_inst_itr->NextNode(); inst;
+             inst = call_inst_itr->NextNode()) {
+          inst->RemoveFromList();
+          std::unique_ptr<ir::Instruction> cp_inst(inst);
           // If multiple blocks generated, regenerate any same-block
           // instruction that has not been seen in this last block.
           if (multiBlocks) {