private void insertCopy(TryCatchJoint joint) { Set<Variable> variableSet = new HashSet<>(joint.getSourceVariables()); BasicBlock block = joint.getSource(); DefinitionExtractor defExtractor = new DefinitionExtractor(); for (int i = block.getInstructions().size() - 1; i >= 0; --i) { Instruction insn = block.getInstructions().get(i); insn.acceptVisitor(defExtractor); for (Variable definedVar : defExtractor.getDefinedVariables()) { if (variableSet.remove(definedVar)) { AssignInstruction copyInsn = new AssignInstruction(); copyInsn.setReceiver(joint.getReceiver()); copyInsn.setAssignee(definedVar); block.getInstructions().add(i, copyInsn); } } } for (Variable enteringVar : variableSet) { AssignInstruction copyInsn = new AssignInstruction(); copyInsn.setReceiver(joint.getReceiver()); copyInsn.setAssignee(enteringVar); block.getInstructions().add(0, copyInsn); } }
private void insertCopy(Incoming incoming, Map<BasicBlock, BasicBlock> blockMap) { final Phi phi = incoming.getPhi(); Program program = phi.getBasicBlock().getProgram(); AssignInstruction copyInstruction = new AssignInstruction(); Variable firstCopy = program.createVariable(); copyInstruction.setReceiver(firstCopy); copyInstruction.setAssignee(incoming.getValue()); BasicBlock source = blockMap.get(incoming.getSource()); if (source == null) { source = incoming.getSource(); } else { incoming.setSource(source); } if (!(incoming.getSource().getLastInstruction() instanceof JumpInstruction)) { final BasicBlock copyBlock = program.createBasicBlock(); JumpInstruction jumpInstruction = new JumpInstruction(); jumpInstruction.setTarget(phi.getBasicBlock()); copyBlock.getInstructions().add(jumpInstruction); incoming .getSource() .getLastInstruction() .acceptVisitor( new BasicBlockMapper( block -> block == phi.getBasicBlock().getIndex() ? copyBlock.getIndex() : block)); blockMap.put(source, copyBlock); incoming.setSource(copyBlock); source = copyBlock; } source.getInstructions().add(source.getInstructions().size() - 1, copyInstruction); incoming.setValue(copyInstruction.getReceiver()); }