"""(Triforce) A subtraction's V flag as N xor (a < b, signed), so that clang folds a signed compare to one host compare (A64_SUB_V). suyu's translator (third_party/a64-kit/lift/suyu/arm64_to_c.h SetFlags) writes every flag-setting subtraction's NZCV from its operands and result: SUBS and CMP (immediate, shifted and extended register) and CCMP when its condition holds. V is the overflow of a - b as bits, ((a ^ b) & (a ^ r)) >> 63 (31 at 32 bits), and the signed conditions read it against N: LT is N != V, GE N == V, GT and LE the same with Z. clang does not see through that: CMP x1, x2; B.LT, the game's most common signed loop and bounds test, became sub, xor, andn, and, or and a test of the sign (seven instructions and two registers where the CPU has cmp and jl), at -O1 and at -O2 alike, in every function, hot or not. For a subtraction N xor V is exactly a < b as signed integers of the operation's width: N is the sign of the wrapped difference and V says whether the wrap flipped it (that is what Arm's LT condition means after a CMP). So V = N xor (a n, so that dead_flags sees no new read of N), a reader's N != V is (N xor N) xor (a v = (... & sign) ? 1 : 0), so neither is touched. Rekarina, another recompiler of the game, compares a SUBS's operands directly where only that kind reaches the reader (its recomp/flags.py, runtime/cpu.h fc_sub: https://git.rekarina.dev/rekarina/rekarina); the identity here needs no such analysis and is exact for every reader. (Triforce) A second form, "ovf": V as the compiler's own signed-overflow test of the subtraction, __builtin_sub_overflow((intN_t)a, (intN_t)b, ...), which is V by definition. clang makes it the subtraction's own overflow flag (sub, seto) and a signed condition after it sub, seto, a shift and a compare of two bytes: five instructions where the kit's text takes nine and the form above two. Its reason: the hot pieces are compiled with a recorded profile (nx2/build.py RX_PGO), which the compiler matches to a function by a count of its conditions, comparisons, labels and jumps. The form above adds a `<` to every function that compares, so every recorded function is compiled without its profile until a new one is recorded with the new text; a builtin call is not counted, so with "ovf" the recorded profile still matches. A64_SUB_V (lift time, so in the lift key: an A64_ variable): unset, empty or "0" off, and the lift's text is the kit's to the byte (Triforce's default until it is measured in the game); "1" the form above (record a new profile with it); "ovf" the overflow form. Game-free check: tests/test_cmpflags.py (each form against the old V on edge and random operands at both widths, the substitution on translated words, and that it leaves ADDS/ADCS/SBCS alone). install() runs at nx2/lift.py's module level, so the lift's emit processes (nx2/emit_pool.py) run it too. """ import os import a64emit # SetFlags' subtraction V, at 64 and 32 bits (arm64_to_c.h: "c->v=(int)((((_fa^_fb)&(_fa^_fr))>>" top ")&1); }") OLD = {64: "c->v=(int)((((_fa^_fb)&(_fa^_fr))>>63)&1);", 32: "c->v=(int)((((_fa^_fb)&(_fa^_fr))>>31)&1);"} # (_fa, _fb, _fr: the width's unsigned operands and result, as SetFlags declares them) FORMS = {"lt": {64: "c->v=(int)(((_fr>>63)&1)^((int64_t)_fa<(int64_t)_fb));", 32: "c->v=(int)(((_fr>>31)&1)^((int32_t)_fa<(int32_t)_fb));"}, # (the difference itself goes into a temporary nobody reads; no ';' inside: dead_flags cuts a store at its first one) "ovf": {64: "c->v=(int)__builtin_sub_overflow((int64_t)_fa,(int64_t)_fb,&(int64_t){0});", 32: "c->v=(int)__builtin_sub_overflow((int32_t)_fa,(int32_t)_fb,&(int32_t){0});"}} def mode(): """the form A64_SUB_V asks for: an empty string (off), 'lt' or 'ovf'""" v = os.environ.get("A64_SUB_V", "").strip().lower() if v in ("", "0", "off", "false", "no"): return "" if v in ("1", "on", "true", "yes", "lt"): return "lt" if v == "ovf": return "ovf" raise SystemExit("A64_SUB_V=%s: 0, 1 or ovf" % os.environ.get("A64_SUB_V")) def on(): return bool(mode()) NEW = FORMS[mode() or "lt"] # (the form this process writes; "lt" where it writes none, for the tests' text checks) def has_sub_v(text): """whether text has SetFlags' subtraction V: its exact text, since "_fa^_fb" alone is in ADDS/CMN's ~(_fa^_fb) too""" return OLD[64] in text or OLD[32] in text def rewrite(text): """an instruction's translated C with its subtraction V (if any) written as N xor (a