Update IRMetadata upon calling BasicBlock.add()

Change-Id: If51bc8d05607a2cfbc83cb22e9abdea76169a78e
diff --git a/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java b/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java
index cf29fdc..661371c 100644
--- a/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java
+++ b/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java
@@ -652,9 +652,14 @@
         : "Attempt to remove Phi " + phi + " which is present in currentDefinitions";
   }
 
-  public void add(Instruction next) {
+  public void add(Instruction next, IRCode code) {
+    add(next, code.metadata());
+  }
+
+  public void add(Instruction next, IRMetadata metadata) {
     assert !isFilled();
     instructions.add(next);
+    metadata.record(next);
     next.setBlock(this);
   }
 
@@ -1280,8 +1285,9 @@
    * @param blockNumber the block number of the goto block
    * @param target the target of the goto block
    */
-  public static BasicBlock createGotoBlock(int blockNumber, Position position, BasicBlock target) {
-    BasicBlock block = createGotoBlock(blockNumber, position);
+  public static BasicBlock createGotoBlock(
+      int blockNumber, Position position, IRMetadata metadata, BasicBlock target) {
+    BasicBlock block = createGotoBlock(blockNumber, position, metadata);
     block.getMutableSuccessors().add(target);
     return block;
   }
@@ -1293,9 +1299,10 @@
    *
    * @param blockNumber the block number of the goto block
    */
-  public static BasicBlock createGotoBlock(int blockNumber, Position position) {
+  public static BasicBlock createGotoBlock(
+      int blockNumber, Position position, IRMetadata metadata) {
     BasicBlock block = new BasicBlock();
-    block.add(new Goto());
+    block.add(new Goto(), metadata);
     block.close(null);
     block.setNumber(blockNumber);
     block.entry().setPosition(position);
@@ -1310,9 +1317,9 @@
    * @param blockNumber the block number of the block
    * @param theIf the if instruction
    */
-  public static BasicBlock createIfBlock(int blockNumber, If theIf) {
+  public static BasicBlock createIfBlock(int blockNumber, If theIf, IRMetadata metadata) {
     BasicBlock block = new BasicBlock();
-    block.add(theIf);
+    block.add(theIf, metadata);
     block.close(null);
     block.setNumber(blockNumber);
     return block;
@@ -1327,20 +1334,22 @@
    * @param theIf the if instruction
    * @param instructions the instructions to place before the if instruction
    */
-  public static BasicBlock createIfBlock(int blockNumber, If theIf, Instruction... instructions) {
+  public static BasicBlock createIfBlock(
+      int blockNumber, If theIf, IRMetadata metadata, Instruction... instructions) {
     BasicBlock block = new BasicBlock();
     for (Instruction instruction : instructions) {
-      block.add(instruction);
+      block.add(instruction, metadata);
     }
-    block.add(theIf);
+    block.add(theIf, metadata);
     block.close(null);
     block.setNumber(blockNumber);
     return block;
   }
 
-  public static BasicBlock createSwitchBlock(int blockNumber, IntSwitch theSwitch) {
+  public static BasicBlock createSwitchBlock(
+      int blockNumber, IntSwitch theSwitch, IRMetadata metadata) {
     BasicBlock block = new BasicBlock();
-    block.add(theSwitch);
+    block.add(theSwitch, metadata);
     block.close(null);
     block.setNumber(blockNumber);
     return block;
@@ -1359,8 +1368,8 @@
     moveException.setPosition(position);
     Throw throwInstruction = new Throw(moveException.outValue);
     throwInstruction.setPosition(position);
-    block.add(moveException);
-    block.add(throwInstruction);
+    block.add(moveException, code);
+    block.add(throwInstruction, code);
     block.close(null);
     block.setNumber(code.getHighestBlockNumber() + 1);
     return block;
@@ -1624,32 +1633,26 @@
     // one new phi to merge the two exception values, and all other phis don't need
     // to be changed.
     for (BasicBlock catchSuccessor : catchSuccessors) {
-      catchSuccessor.splitCriticalExceptionEdges(
-          code.getHighestBlockNumber() + 1,
-          code.valueNumberGenerator,
-          blockIterator::add,
-          options);
+      catchSuccessor.splitCriticalExceptionEdges(code, blockIterator::add, options);
     }
   }
 
   /**
-   * Assumes that `this` block is a catch handler target (note that it does not have to
-   * start with MoveException instruction, since the instruction can be removed by
-   * optimizations like dead code remover.
+   * Assumes that `this` block is a catch handler target (note that it does not have to start with
+   * MoveException instruction, since the instruction can be removed by optimizations like dead code
+   * remover.
    *
-   * Introduces new blocks on all incoming edges and clones MoveException instruction to
-   * these blocks if it exists. All exception values introduced in newly created blocks
-   * are combined in a phi added to `this` block.
+   * <p>Introduces new blocks on all incoming edges and clones MoveException instruction to these
+   * blocks if it exists. All exception values introduced in newly created blocks are combined in a
+   * phi added to `this` block.
    *
-   * Note that if there are any other phis defined on this block, they remain valid, since
-   * this method does not affect incoming edges in any way, and just adds new blocks with
-   * MoveException and Goto.
+   * <p>Note that if there are any other phis defined on this block, they remain valid, since this
+   * method does not affect incoming edges in any way, and just adds new blocks with MoveException
+   * and Goto.
    */
   public int splitCriticalExceptionEdges(
-      int nextBlockNumber,
-      ValueNumberGenerator valueNumberGenerator,
-      Consumer<BasicBlock> onNewBlock,
-      InternalOptions options) {
+      IRCode code, Consumer<BasicBlock> onNewBlock, InternalOptions options) {
+    int nextBlockNumber = code.getHighestBlockNumber() + 1;
     List<BasicBlock> predecessors = getMutablePredecessors();
     boolean hasMoveException = entry().isMoveException();
     TypeLatticeElement exceptionTypeLattice = null;
@@ -1676,18 +1679,16 @@
       newBlock.setNumber(nextBlockNumber++);
       newPredecessors.add(newBlock);
       if (hasMoveException) {
-        Value value = new Value(
-            valueNumberGenerator.next(),
-            exceptionTypeLattice,
-            move.getLocalInfo());
+        Value value =
+            new Value(code.valueNumberGenerator.next(), exceptionTypeLattice, move.getLocalInfo());
         values.add(value);
         MoveException newMove = new MoveException(value, exceptionType, options);
-        newBlock.add(newMove);
+        newBlock.add(newMove, code);
         newMove.setPosition(position);
       }
       Goto next = new Goto();
       next.setPosition(position);
-      newBlock.add(next);
+      newBlock.add(next, code);
       newBlock.close(null);
       newBlock.getMutableSuccessors().add(this);
       newBlock.getMutablePredecessors().add(predecessor);
@@ -1702,7 +1703,7 @@
     if (hasMoveException) {
       Phi phi =
           new Phi(
-              valueNumberGenerator.next(),
+              code.valueNumberGenerator.next(),
               this,
               exceptionTypeLattice,
               move.getLocalInfo(),
diff --git a/src/main/java/com/android/tools/r8/ir/code/BasicBlockInstructionListIterator.java b/src/main/java/com/android/tools/r8/ir/code/BasicBlockInstructionListIterator.java
index 90c1569..70c4406 100644
--- a/src/main/java/com/android/tools/r8/ir/code/BasicBlockInstructionListIterator.java
+++ b/src/main/java/com/android/tools/r8/ir/code/BasicBlockInstructionListIterator.java
@@ -121,6 +121,7 @@
     instruction.setBlock(block);
     assert instruction.getBlock() == block;
     listIterator.set(instruction);
+    metadata.record(instruction);
   }
 
   /**
@@ -501,7 +502,7 @@
         entryBlock = entryBlock.listIterator(code).split(inlinee);
         entryBlockIterator = entryBlock.listIterator(code);
         // Insert cast instruction into the new block.
-        inlineEntry.getInstructions().addFirst(castInstruction);
+        inlineEntry.listIterator(code).add(castInstruction);
         castInstruction.setBlock(inlineEntry);
         assert castInstruction.getBlock().getInstructions().size() == 2;
       } else {
@@ -628,6 +629,7 @@
         || IteratorUtils.anyRemainingMatch(
             blocksIterator, remaining -> remaining == finalInvokeSuccessor);
 
+    code.metadata().merge(inlinee.metadata());
     return invokeSuccessor;
   }
 
@@ -672,7 +674,7 @@
     }
     // The newly constructed return will be eliminated as part of inlining so we set position none.
     newReturn.setPosition(Position.none());
-    newExitBlock.add(newReturn);
+    newExitBlock.add(newReturn, metadata);
     for (BasicBlock exitBlock : normalExits) {
       InstructionListIterator it = exitBlock.listIterator(code, exitBlock.getInstructions().size());
       Instruction oldExit = it.previous();
diff --git a/src/main/java/com/android/tools/r8/ir/code/IRCode.java b/src/main/java/com/android/tools/r8/ir/code/IRCode.java
index 9198796..78b7dd6 100644
--- a/src/main/java/com/android/tools/r8/ir/code/IRCode.java
+++ b/src/main/java/com/android/tools/r8/ir/code/IRCode.java
@@ -309,7 +309,8 @@
           // at the end of the list of blocks disregarding branching
           // structure.
           BasicBlock newBlock =
-              BasicBlock.createGotoBlock(nextBlockNumber++, pred.exit().getPosition(), block);
+              BasicBlock.createGotoBlock(
+                  nextBlockNumber++, pred.exit().getPosition(), metadata, block);
           newBlocks.add(newBlock);
           pred.replaceSuccessor(block, newBlock);
           newBlock.getMutablePredecessors().add(pred);
@@ -367,7 +368,7 @@
         if (fallthrough != null) {
           BasicBlock newFallthrough =
               BasicBlock.createGotoBlock(
-                  nextBlockNumber++, current.exit().getPosition(), fallthrough);
+                  nextBlockNumber++, current.exit().getPosition(), metadata, fallthrough);
           current.exit().setFallthroughBlock(newFallthrough);
           newFallthrough.getMutablePredecessors().add(current);
           fallthrough.replacePredecessor(current, newFallthrough);
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/DexBuilder.java b/src/main/java/com/android/tools/r8/ir/conversion/DexBuilder.java
index 75c2a15..a8b0e60 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/DexBuilder.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/DexBuilder.java
@@ -497,7 +497,8 @@
         If theIf = block.exit().asIf();
         BasicBlock trueTarget = theIf.getTrueTarget();
         BasicBlock newBlock =
-            BasicBlock.createGotoBlock(ir.blocks.size(), theIf.getPosition(), trueTarget);
+            BasicBlock.createGotoBlock(
+                ir.blocks.size(), theIf.getPosition(), ir.metadata(), trueTarget);
         theIf.setTrueTarget(newBlock);
         theIf.invert();
         it.add(newBlock);
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/IRBuilder.java b/src/main/java/com/android/tools/r8/ir/conversion/IRBuilder.java
index 04bcfa8..4cf7094 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/IRBuilder.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/IRBuilder.java
@@ -783,7 +783,7 @@
       MoveException moveException =
           new MoveException(out, moveExceptionItem.guard, appView.options());
       moveException.setPosition(position);
-      currentBlock.add(moveException);
+      currentBlock.add(moveException, metadata);
     }
     // The block-transfer for exceptional edges needs to inform that this is an exceptional transfer
     // so that local ends become implicit. The reason for this issue is that the "split block" for
@@ -2208,7 +2208,6 @@
   // Private instruction helpers.
   private void addInstruction(Instruction ir) {
     addInstruction(ir, source.getCurrentPosition());
-    metadata.record(ir);
   }
 
   private void addInstruction(Instruction ir, Position position) {
@@ -2216,7 +2215,7 @@
     hasImpreciseValues |= ir.outValue() != null && !ir.outValue().getTypeLattice().isPreciseType();
     ir.setPosition(position);
     attachLocalValues(ir);
-    currentBlock.add(ir);
+    currentBlock.add(ir, metadata);
     if (ir.instructionTypeCanThrow()) {
       assert source.verifyCurrentInstructionCanThrow();
       CatchHandlers<Integer> catchHandlers = source.getCurrentCatchHandlers(this);
@@ -2519,7 +2518,7 @@
             if (joinBlock == null) {
               joinBlock =
                   BasicBlock.createGotoBlock(
-                      blocks.size() + blocksToAdd.size(), block.getPosition(), block);
+                      blocks.size() + blocksToAdd.size(), block.getPosition(), metadata, block);
               joinBlocks.put(otherPredecessorIndex, joinBlock);
               blocksToAdd.add(joinBlock);
               BasicBlock otherPredecessor = block.getPredecessors().get(otherPredecessorIndex);
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/StringSwitchRemover.java b/src/main/java/com/android/tools/r8/ir/conversion/StringSwitchRemover.java
index d163c21..7868d7b 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/StringSwitchRemover.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/StringSwitchRemover.java
@@ -105,7 +105,11 @@
 
       BasicBlock newBlock =
           BasicBlock.createIfBlock(
-              code.blocks.size(), ifInstruction, constStringInstruction, invokeInstruction);
+              code.blocks.size(),
+              ifInstruction,
+              code.metadata(),
+              constStringInstruction,
+              invokeInstruction);
       newBlock.link(entry.getValue());
       blockIterator.add(newBlock);
       newBlocks.add(newBlock);
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
index 2831de7..6479c7f 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
@@ -54,6 +54,7 @@
 import com.android.tools.r8.ir.code.DominatorTree;
 import com.android.tools.r8.ir.code.Goto;
 import com.android.tools.r8.ir.code.IRCode;
+import com.android.tools.r8.ir.code.IRMetadata;
 import com.android.tools.r8.ir.code.If;
 import com.android.tools.r8.ir.code.If.Type;
 import com.android.tools.r8.ir.code.InstanceOf;
@@ -491,7 +492,7 @@
       return this;
     }
 
-    public BasicBlock build() {
+    public BasicBlock build(IRMetadata metadata) {
       final int NOT_FOUND = -1;
       Object2IntMap<BasicBlock> targetToSuccessorIndex = new Object2IntLinkedOpenHashMap<>();
       targetToSuccessorIndex.defaultReturnValue(NOT_FOUND);
@@ -514,7 +515,7 @@
           targetToSuccessorIndex.computeIfAbsent(fallthrough, b -> targetToSuccessorIndex.size());
       IntSwitch newSwitch = new IntSwitch(value, keys, targetBlockIndices, fallthroughIndex);
       newSwitch.setPosition(position);
-      BasicBlock newSwitchBlock = BasicBlock.createSwitchBlock(blockNumber, newSwitch);
+      BasicBlock newSwitchBlock = BasicBlock.createSwitchBlock(blockNumber, newSwitch, metadata);
       for (BasicBlock successor : targetToSuccessorIndex.keySet()) {
         newSwitchBlock.link(successor);
       }
@@ -568,10 +569,10 @@
         ConstNumber rightConst = code.createIntConstant(right);
         rightConst.setPosition(position);
         newIf = new If(Type.EQ, ImmutableList.of(left, rightConst.dest()));
-        ifBlock = BasicBlock.createIfBlock(blockNumber, newIf, rightConst);
+        ifBlock = BasicBlock.createIfBlock(blockNumber, newIf, code.metadata(), rightConst);
       } else {
         newIf = new If(Type.EQ, left);
-        ifBlock = BasicBlock.createIfBlock(blockNumber, newIf);
+        ifBlock = BasicBlock.createIfBlock(blockNumber, newIf, code.metadata());
       }
       newIf.setPosition(position);
       ifBlock.link(target);
@@ -631,7 +632,7 @@
       switchBuilder
           .setFallthrough(fallthroughBlock)
           .setBlockNumber(nextBlockNumber++);
-      BasicBlock newSwitchBlock = switchBuilder.build();
+      BasicBlock newSwitchBlock = switchBuilder.build(code.metadata());
       newBlocks.addFirst(newSwitchBlock);
       fallthroughBlock = newSwitchBlock;
     }
@@ -4052,7 +4053,7 @@
       iterator.add(new InvokeVirtual(print, null, ImmutableList.of(out, indent)));
 
       // Add a block for end-of-line printing.
-      BasicBlock eol = BasicBlock.createGotoBlock(code.blocks.size(), position);
+      BasicBlock eol = BasicBlock.createGotoBlock(code.blocks.size(), position, code.metadata());
       code.blocks.add(eol);
 
       BasicBlock successor = block.unlinkSingleSuccessor();
@@ -4067,12 +4068,14 @@
         successor = block.unlinkSingleSuccessor();
         If theIf = new If(Type.NE, argument);
         theIf.setPosition(position);
-        BasicBlock ifBlock = BasicBlock.createIfBlock(code.blocks.size(), theIf);
+        BasicBlock ifBlock = BasicBlock.createIfBlock(code.blocks.size(), theIf, code.metadata());
         code.blocks.add(ifBlock);
         // Fallthrough block must be added right after the if.
-        BasicBlock isNullBlock = BasicBlock.createGotoBlock(code.blocks.size(), position);
+        BasicBlock isNullBlock =
+            BasicBlock.createGotoBlock(code.blocks.size(), position, code.metadata());
         code.blocks.add(isNullBlock);
-        BasicBlock isNotNullBlock = BasicBlock.createGotoBlock(code.blocks.size(), position);
+        BasicBlock isNotNullBlock =
+            BasicBlock.createGotoBlock(code.blocks.size(), position, code.metadata());
         code.blocks.add(isNotNullBlock);
 
         // Link the added blocks together.
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java b/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java
index b1f231e..d616c87 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java
@@ -874,7 +874,6 @@
               }
 
               context.copyMetadata(target);
-              code.mergeMetadataFromInlinee(inlinee.code);
             }
           }
         } else if (current.isAssumeDynamicType()) {
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/classinliner/FieldValueHelper.java b/src/main/java/com/android/tools/r8/ir/optimize/classinliner/FieldValueHelper.java
index 43784de..a16e9a3 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/classinliner/FieldValueHelper.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/classinliner/FieldValueHelper.java
@@ -19,7 +19,6 @@
 import com.android.tools.r8.ir.code.Value;
 import java.util.ArrayList;
 import java.util.IdentityHashMap;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -146,9 +145,7 @@
           code.createValue(TypeLatticeElement.fromDexType(field.type, maybeNull(), appView));
       ConstNumber defaultValueInsn = new ConstNumber(defaultValue, 0);
       defaultValueInsn.setPosition(root.getPosition());
-      LinkedList<Instruction> instructions = block.getInstructions();
-      instructions.add(instructions.indexOf(root) + 1, defaultValueInsn);
-      defaultValueInsn.setBlock(block);
+      block.listIterator(code, root).add(defaultValueInsn);
     }
     return defaultValue;
   }
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/classinliner/InlineCandidateProcessor.java b/src/main/java/com/android/tools/r8/ir/optimize/classinliner/InlineCandidateProcessor.java
index ac4754a..1b05311 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/classinliner/InlineCandidateProcessor.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/classinliner/InlineCandidateProcessor.java
@@ -47,7 +47,6 @@
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.IdentityHashMap;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -381,9 +380,8 @@
 
       ConstNumber nullValue = code.createConstNull();
       nullValue.setPosition(invoke.getPosition());
-      LinkedList<Instruction> instructions = block.getInstructions();
-      instructions.add(instructions.indexOf(invoke), nullValue);
-      nullValue.setBlock(block);
+      block.listIterator(code, invoke).add(nullValue);
+      assert nullValue.getBlock() == block;
 
       int argIndex = unusedArgument.getSecond() + (invoke.isInvokeMethodWithReceiver() ? 1 : 0);
       invoke.replaceValue(argIndex, nullValue.outValue());
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/ConstantRemovalTest.java b/src/test/java/com/android/tools/r8/ir/optimize/ConstantRemovalTest.java
index e97277f..2d626a2 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/ConstantRemovalTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/ConstantRemovalTest.java
@@ -31,7 +31,7 @@
 public class ConstantRemovalTest {
 
   private static class MockLinearScanRegisterAllocator extends LinearScanRegisterAllocator {
-    public MockLinearScanRegisterAllocator(AppView<?> appView, IRCode code) {
+    MockLinearScanRegisterAllocator(AppView<?> appView, IRCode code) {
       super(appView, code);
     }
 
@@ -42,7 +42,7 @@
   }
 
   private static class MockLiveIntervals extends LiveIntervals {
-    public MockLiveIntervals(Value value) {
+    MockLiveIntervals(Value value) {
       super(value);
     }
 
@@ -72,6 +72,8 @@
     // is needed and the value 10 is *not* still in register 0 at that point.
     BasicBlock block = new BasicBlock();
     block.setNumber(0);
+
+    IRMetadata metadata = IRMetadata.unknown();
     Position position = Position.testingPosition();
 
     Value v3 = new Value(3, TypeLatticeElement.LONG, null);
@@ -79,51 +81,51 @@
     new MockLiveIntervals(v3);
     Instruction instruction = new ConstNumber(v3, 0);
     instruction.setPosition(position);
-    block.add(instruction);
+    block.add(instruction, metadata);
 
     Value v0 = new Value(0, TypeLatticeElement.LONG, null);
     v0.setNeedsRegister(true);
     new MockLiveIntervals(v0);
     instruction = new ConstNumber(v0, 10);
     instruction.setPosition(position);
-    block.add(instruction);
+    block.add(instruction, metadata);
 
     instruction = new Div(NumericType.LONG, v3, v3, v0);
     instruction.setPosition(position);
-    block.add(instruction);
+    block.add(instruction, metadata);
 
     Value v2 = new Value(2, TypeLatticeElement.INT, null);
     v2.setNeedsRegister(true);
     new MockLiveIntervals(v2);
     instruction = new ConstNumber(v2, 10);
     instruction.setPosition(position);
-    block.add(instruction);
+    block.add(instruction, metadata);
 
     Value v1 = new Value(1, TypeLatticeElement.INT, null);
     v1.setNeedsRegister(true);
     new MockLiveIntervals(v1);
     instruction = new Move(v1 ,v2);
     instruction.setPosition(position);
-    block.add(instruction);
+    block.add(instruction, metadata);
 
     instruction = new Div(NumericType.INT, v1, v1, v1);
     instruction.setPosition(position);
-    block.add(instruction);
+    block.add(instruction, metadata);
 
     Value v0_2 = new Value(0, TypeLatticeElement.LONG, null);
     v0_2.setNeedsRegister(true);
     new MockLiveIntervals(v0_2);
     instruction = new ConstNumber(v0_2, 10);
     instruction.setPosition(position);
-    block.add(instruction);
+    block.add(instruction, metadata);
 
     instruction = new Div(NumericType.LONG, v3, v3, v0_2);
     instruction.setPosition(position);
-    block.add(instruction);
+    block.add(instruction, metadata);
 
     Instruction ret = new Return();
     ret.setPosition(position);
-    block.add(ret);
+    block.add(ret, metadata);
     block.setFilledForTesting();
 
     LinkedList<BasicBlock> blocks = new LinkedList<>();
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/TrivialGotoEliminationTest.java b/src/test/java/com/android/tools/r8/ir/optimize/TrivialGotoEliminationTest.java
index 1007ea6..fdb982f 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/TrivialGotoEliminationTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/TrivialGotoEliminationTest.java
@@ -33,6 +33,9 @@
 import org.junit.Test;
 
 public class TrivialGotoEliminationTest {
+
+  private final IRMetadata metadata = IRMetadata.unknown();
+
   @Test
   public void trivialGotoInEntryBlock() {
     // Setup silly block structure:
@@ -47,22 +50,22 @@
     Position position = Position.testingPosition();
     BasicBlock block2 = new BasicBlock();
     block2.setNumber(2);
-    BasicBlock block0 = BasicBlock.createGotoBlock(0, position, block2);
+    BasicBlock block0 = BasicBlock.createGotoBlock(0, position, metadata, block2);
     block0.setFilledForTesting();
     block2.getMutablePredecessors().add(block0);
     Instruction ret = new Return();
     ret.setPosition(position);
-    block2.add(ret);
+    block2.add(ret, metadata);
     block2.setFilledForTesting();
     BasicBlock block1 = new BasicBlock();
     block1.setNumber(1);
     Value value = new Value(0, TypeLatticeElement.INT, null);
     Instruction number = new ConstNumber(value, 0);
     number.setPosition(position);
-    block1.add(number);
+    block1.add(number, metadata);
     Instruction throwing = new Throw(value);
     throwing.setPosition(position);
-    block1.add(throwing);
+    block1.add(throwing, metadata);
     block1.setFilledForTesting();
     LinkedList<BasicBlock> blocks = new LinkedList<>();
     blocks.add(block0);
@@ -111,18 +114,18 @@
     block2.setNumber(2);
     Instruction ret = new Return();
     ret.setPosition(position);
-    block2.add(ret);
+    block2.add(ret, metadata);
     block2.setFilledForTesting();
 
     BasicBlock block3 = new BasicBlock();
     block3.setNumber(3);
     Instruction instruction = new Goto();
     instruction.setPosition(position);
-    block3.add(instruction);
+    block3.add(instruction, metadata);
     block3.setFilledForTesting();
     block3.getMutableSuccessors().add(block3);
 
-    BasicBlock block1 = BasicBlock.createGotoBlock(1, position);
+    BasicBlock block1 = BasicBlock.createGotoBlock(1, position, metadata);
     block1.getMutableSuccessors().add(block3);
     block1.setFilledForTesting();
 
@@ -136,10 +139,10 @@
             null);
     instruction = new Argument(value, false);
     instruction.setPosition(position);
-    block0.add(instruction);
+    block0.add(instruction, metadata);
     instruction = new If(Type.EQ, value);
     instruction.setPosition(position);
-    block0.add(instruction);
+    block0.add(instruction, metadata);
     block0.getMutableSuccessors().add(block2);
     block0.getMutableSuccessors().add(block1);
     block0.setFilledForTesting();