public void optimizePhaseI() {
		// the statement have to have an output line inorder to be able to sort 
		// input pins and refer to the output gate.
		// NOTE that this assignment of outputLine may not be the final one 
		//  (refer to the optimization phase II and circuit writing).
		// 
		outputLine = Program.getLineNumber();

		boolean statementChanged = true;

		do {
			// tracks if there where changes is this AssignmentStatment
			if (rhs.hasUnaryInput()) // if (this has unary exp as input)
			 {
				rhs.combineUnaryInput();
			} else if (rhs.hasSharedInput()) {
				rhs = rhs.combineSharedInput();
			}
			// NOTE:
			// the following two trasformations can reduce the rhs's arity.
			else if (((OperationExpression) rhs).hasConstantInput())
			//if (this has constant input)
			 {
				//combine the input into the gate;
				rhs = rhs.combineConstInput();
			} else if (rhs.hasEqualInputs()) {
				//combine equal inputs into one.
				rhs = rhs.combineEqualInputs();
			} else {
				statementChanged = false;
			}
		} while (statementChanged); //while (there are changes 
		                            //in this AssignmentStatmenet);

		if (rhs.isComplexIDOrNeg()) {
			rhs = rhs.simplify();
		}

		if (rhs.isConstant()) { // if the optimized 
			                    // gate returns a constant result
			rhs = rhs.getConstantOutput();
		}

		rhs.sortInputs();

		if (Optimizer.containsGateNegGate(rhs)) {
			rhs = Optimizer.getReference(rhs);
		} else {
			Optimizer.addGate(rhs, lhs);
		}
	}