protected final Phi addPhiInternal(Identifier var, Value oldVal, Value newVal)
        throws Exception
    {
	// search for phi instruction for this variable
        for(Instruction i : instructions) {
            if(i instanceof Phi) {
                Phi p = (Phi)i;
                if(p.getVariable().equals(var)) {
                    // update the phi since there is already one for
                    // this variable, throw if the old value isn't
                    // part of the phi
                    p.updateArgument(oldVal, new VariableReference(var, newVal),
                                     currentBranch);
                    // we are now done, didn't add a new Phi
                    return null;
                }
            } else {
                // all phis must occur at the begining of the basic
                // block, if we get to an instruction that is not a
                // phi, we can stop our search
                break;
            }
        }
        // if we are here, we need to add a new phi. Wrap the new
        // value in VariableReference to the correct variable, the old
        // value will be a variable reference
        assert (oldVal instanceof VariableReference);
        Phi p = null;

        if(currentBranch == 1) {
            p = new Phi(var, new VariableReference(var, newVal), oldVal);
        } else if (currentBranch == 2) {
            p = new Phi(var, oldVal, new VariableReference(var, newVal));
        } else {
            throw new Exception("Invalid Phi position");
        }

        if(instructions.isEmpty()) {
            // if this is the first instruction, make it the most recent dominating
            p.setDominating(mostRecentDominating);
            mostRecentDominating = p;
        } else {
            // else add it into the linked list of dominating
            // instructions
            Instruction i = instructions.get(0);
            p.setDominating(i.getDominating());
            i.setDominating(p);
        }
        instructions.add(0, p);
        p.setContainingBB(this);
        // return the new Phi
        return p;
    }