--- a/src/ASMText2.cpp +++ b/src/ASMText2.cpp @@ -4952,29 +4952,31 @@ // Look for this: -// LDB {foo} ; {bar} involves not pre/post-increment, e.g., LDB ,S+ -// LDX/LEAX {bar} ; {bar} does not involve B, e.g., LDX B,U -// LDB {baz} -// Remove the first LDB. +// LDB {foo} ; no pre/post-increment side effects +// LDX/LEAX {bar} ; must not read B, including D-indexed operands +// LDB {baz} ; must not read the previous B in its effective address +// Remove the first LDB only when its value and side effects are unused. // bool ASMText::removeUselessLdaOrLdb(size_t index) { if (! isInstrAnyArg(index, "LDB")) return false; - const string &arg = elements[index].fields[0]; - if (startsWith(arg, ",-") || endsWith(arg, "+")) // if pre/post increment - return false; + if (isInstrWithPreDecrOrPostIncr(index)) + return false; // Removing the load must not remove an index/stack update. size_t otherLoad = findNextInstrBeforeLabel(index + 1); if (! isInstrAnyArg(otherLoad, "LDX") && ! isInstrAnyArg(otherLoad, "LEAX")) return false; - const string &otherLoadArg = elements[otherLoad].fields[1]; - if (startsWith(otherLoadArg, "B,")) - return false; // we have "B,reg" as the arg to LDX/LEAX, so previous LDB is useful + // B is also the low byte of D. Use the common instruction-effects + // analysis for both direct and indirect accumulator-indexed operands. + if (InsEffects(elements[otherLoad]).read & B) + return false; size_t secondLdAcc = findNextInstrBeforeLabel(otherLoad + 1); if (! isInstrAnyArg(secondLdAcc, "LDB")) return false; - + if (InsEffects(elements[secondLdAcc]).read & B) + return false; // The overwritten value may still select the byte to load. + // Apply the optimization. commentOut(index, "optim: removeUselessLdaOrLdb (removed LDB b/c later LDB)"); return true;