def insert_op_before(self, new_op: Operation, before_op: Optional[Operation] = None):
        """
        Insert an op right before before_op. If before_op is None,
         then the new op is appended in the end.
        """
        if new_op in self.op_to_node:
            raise ValueError(f"{new_op} already exisits.")

        new_node = OpNode(new_op)

        if before_op is None:
            # If before op is None, the new node is appended in the end.
            if self.start is None:
                self.start = self.end = new_node
            else:
                self.end.next = new_node
                new_node.prev = self.end
                self.end = new_node
        else:
            anchor_node = self.op_to_node[before_op]
            prev_node = anchor_node.prev

            if prev_node is None:
                self.start = new_node
            else:
                prev_node.next = new_node

            new_node.prev = prev_node
            new_node.next = anchor_node
            anchor_node.prev = new_node

        self.op_to_node[new_op] = new_node

        Insert an op right before before_op. If before_op is None,
         then the new op is appended in the end.
        
coremltools/converters/mil/mil/utils.py