+unsigned OptTransfers (CodeSeg* S)
+/* Remove transfers from one register to another and back */
+{
+ unsigned Changes = 0;
+
+ /* Walk over the entries */
+ unsigned I = 0;
+ while (I < CS_GetEntryCount (S)) {
+
+ CodeEntry* N;
+ CodeEntry* X;
+ CodeEntry* P;
+
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ /* Check if it is a store instruction followed by a load from the
+ * same address which is itself not followed by a conditional branch.
+ */
+ if ((E->Info & OF_XFR) != 0 &&
+ (N = CS_GetNextEntry (S, I)) != 0 &&
+ !CE_HasLabel (N) &&
+ (N->Info & OF_XFR) != 0) {
+
+ /* Check if it's a transfer and back */
+ if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
+ (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
+ (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
+ (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
+
+ /* If the next insn is a conditional branch, check if the insn
+ * preceeding the first xfr will set the flags right, otherwise we
+ * may not remove the sequence.
+ */
+ if ((X = CS_GetNextEntry (S, I+1)) == 0) {
+ goto NextEntry;
+ }
+ if (CE_UseLoadFlags (X)) {
+ if (I == 0) {
+ /* No preceeding entry */
+ goto NextEntry;
+ }
+ P = CS_GetEntry (S, I-1);
+ if ((P->Info & OF_SETF) == 0) {
+ /* Does not set the flags */
+ goto NextEntry;
+ }
+ }
+
+ /* Remove both transfers */
+ CS_DelEntry (S, I+1);
+ CS_DelEntry (S, I);
+
+ /* Remember, we had changes */
+ ++Changes;
+ }
+ }
+
+NextEntry:
+ /* Next entry */
+ ++I;
+
+ }
+
+ /* Return the number of changes made */
+ return Changes;
+}
+
+
+
+unsigned OptPushPop (CodeSeg* S)
+/* Remove a PHA/PLA sequence were A is not used later */
+{
+ unsigned Changes = 0;
+ unsigned Push = 0; /* Index of push insn */
+ unsigned Pop = 0; /* Index of pop insn */
+ enum {
+ Searching,
+ FoundPush,
+ FoundPop
+ } State = Searching;
+
+ /* Walk over the entries. Look for a push instruction that is followed by
+ * a pop later, where the pop is not followed by an conditional branch,
+ * and where the value of the A register is not used later on.
+ * Look out for the following problems:
+ *
+ * - There may be another PHA/PLA inside the sequence: Restart it.
+ * - If the PLA has a label, all jumps to this label must be inside
+ * the sequence, otherwise we cannot remove the PHA/PLA.
+ */
+ unsigned I = 0;
+ while (I < CS_GetEntryCount (S)) {
+
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ switch (State) {
+
+ case Searching:
+ if (E->OPC == OP65_PHA) {
+ /* Found start of sequence */
+ Push = I;
+ State = FoundPush;
+ }
+ break;
+
+ case FoundPush:
+ if (E->OPC == OP65_PHA) {
+ /* Inner push/pop, restart */
+ Push = I;
+ } else if (E->OPC == OP65_PLA) {
+ /* Found a matching pop */
+ Pop = I;
+ State = FoundPop;
+ }
+ break;
+
+ case FoundPop:
+ /* Next insn, just check if it is no conditional branch and
+ * that A is not used later. Check also that the range we have
+ * found now is a basic block, which means that the PHA is the
+ * only entrance and the PLA the only exit.
+ */
+ if ((E->Info & OF_CBRA) == 0 &&
+ !RegAUsed (S, I) &&
+ CS_IsBasicBlock (S, Push, Pop)) {
+ /* We can remove the PHA and PLA instructions */
+ CS_DelEntry (S, Pop);
+ CS_DelEntry (S, Push);
+ /* Correct I so we continue with the next insn */
+ I -= 2;
+ /* Remember we had changes */
+ ++Changes;
+ }
+ /* Go into search mode again */
+ State = Searching;
+ break;
+
+ }
+
+ /* Next entry */
+ ++I;
+ }
+
+ /* Return the number of changes made */
+ return Changes;
+}
+
+
+