|
| 1 | +using System.Collections.Generic; |
| 2 | +using Cpp2IL.Core.Graphs; |
| 3 | +using Cpp2IL.Core.ISIL; |
| 4 | +using Cpp2IL.Core.Model.Contexts; |
| 5 | + |
| 6 | +namespace Cpp2IL.Core.Analysis; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Recovers high-level relational conditions from the explicit EFLAGS computations the lifter emits. |
| 10 | +/// |
| 11 | +/// The x86 lifter models a <c>cmp</c>/<c>test</c> as a cluster of flag pseudo-registers |
| 12 | +/// (ZF = (a-b)==0, SF = (a-b)<0, OF = signed overflow, ...) and lowers each <c>jcc</c> into a |
| 13 | +/// boolean expression over those flags. This pass recognises those canonical shapes for every |
| 14 | +/// <see cref="OpCode.ConditionalJump"/> condition and rewrites the condition's defining instruction |
| 15 | +/// into a single relational comparison (==, !=, <, <=, >, >=) on the original compare |
| 16 | +/// operands. The now-orphaned flag arithmetic is removed by the dead-code pass that runs next. |
| 17 | +/// |
| 18 | +/// Runs in SSA form, where each flag/temporary has a single, version-stable definition, so the |
| 19 | +/// operands referenced at the branch are provably the ones captured at the compare. |
| 20 | +/// |
| 21 | +/// Note: the lifter lowers the unsigned conditions (ja/jae/jb/jbe) with the same flag expressions as |
| 22 | +/// their signed counterparts, so they are recovered as signed comparisons too - matching the |
| 23 | +/// existing (signed) behaviour rather than introducing a new inaccuracy. |
| 24 | +/// </summary> |
| 25 | +public static class FlagConditionRecovery |
| 26 | +{ |
| 27 | + public static void Run(MethodAnalysisContext method) => Run(method.ControlFlowGraph!); |
| 28 | + |
| 29 | + public static void Run(ISILControlFlowGraph cfg) |
| 30 | + { |
| 31 | + var defOf = BuildDefMap(cfg); |
| 32 | + |
| 33 | + foreach (var block in cfg.Blocks) |
| 34 | + { |
| 35 | + foreach (var instruction in block.Instructions) |
| 36 | + { |
| 37 | + if (instruction.OpCode != OpCode.ConditionalJump) |
| 38 | + continue; |
| 39 | + |
| 40 | + if (instruction.Operands[1] is not LocalVariable condition) |
| 41 | + continue; |
| 42 | + |
| 43 | + if (!TryClassify(condition, defOf, out var relop, out var op0, out var op1)) |
| 44 | + continue; |
| 45 | + |
| 46 | + // Rewrite the condition's defining instruction in place into a single comparison. |
| 47 | + // Its destination (the condition local the branch reads) is preserved. |
| 48 | + var definition = defOf[condition]; |
| 49 | + definition.OpCode = relop; |
| 50 | + definition.Operands = new List<object> { definition.Operands[0], op0!, op1! }; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static Dictionary<LocalVariable, Instruction> BuildDefMap(ISILControlFlowGraph cfg) |
| 56 | + { |
| 57 | + var defs = new Dictionary<LocalVariable, Instruction>(); |
| 58 | + |
| 59 | + foreach (var block in cfg.Blocks) |
| 60 | + foreach (var instruction in block.Instructions) |
| 61 | + if (instruction.Destination is LocalVariable destination) |
| 62 | + defs[destination] = instruction; |
| 63 | + |
| 64 | + return defs; |
| 65 | + } |
| 66 | + |
| 67 | + private static bool TryClassify(LocalVariable condition, Dictionary<LocalVariable, Instruction> defOf, |
| 68 | + out OpCode relop, out object? op0, out object? op1) |
| 69 | + { |
| 70 | + relop = default; |
| 71 | + |
| 72 | + // ZF on its own => a == b (je) |
| 73 | + if (IsZeroFlag(condition, defOf, out op0, out op1)) { relop = OpCode.CheckEqual; return true; } |
| 74 | + // SF on its own => a < b (js; exact for the common test-against-self case) |
| 75 | + if (IsSignFlag(condition, defOf, out op0, out op1)) { relop = OpCode.CheckLess; return true; } |
| 76 | + |
| 77 | + var definition = Def(condition, defOf); |
| 78 | + if (definition == null) |
| 79 | + return false; |
| 80 | + |
| 81 | + switch (definition.OpCode) |
| 82 | + { |
| 83 | + case OpCode.Not: |
| 84 | + var inner = AsLocal(definition.Operands[1]); |
| 85 | + if (IsZeroFlag(inner, defOf, out op0, out op1)) { relop = OpCode.CheckNotEqual; return true; } // !ZF => != (jne) |
| 86 | + if (IsSignFlag(inner, defOf, out op0, out op1)) { relop = OpCode.CheckGreaterOrEqual; return true; } // !SF => >= (jns) |
| 87 | + if (IsSignEqualsOverflow(inner, defOf, out op0, out op1)) { relop = OpCode.CheckLess; return true; } // !(SF==OF) => < (jl/jb) |
| 88 | + return false; |
| 89 | + |
| 90 | + case OpCode.CheckEqual: |
| 91 | + // SF == OF => a >= b (jge/jae) |
| 92 | + if (IsSignFlag(AsLocal(definition.Operands[1]), defOf, out op0, out op1)) { relop = OpCode.CheckGreaterOrEqual; return true; } |
| 93 | + return false; |
| 94 | + |
| 95 | + case OpCode.And: |
| 96 | + // (SF==OF) && !ZF => a > b (jg/ja) |
| 97 | + if (IsSignGreater(definition, defOf, out op0, out op1)) { relop = OpCode.CheckGreater; return true; } |
| 98 | + return false; |
| 99 | + |
| 100 | + case OpCode.Or: |
| 101 | + // !(SF==OF) || ZF => a <= b (jle/jbe) |
| 102 | + if (IsSignLessOrEqual(definition, defOf, out op0, out op1)) { relop = OpCode.CheckLessOrEqual; return true; } |
| 103 | + return false; |
| 104 | + |
| 105 | + default: |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // ZF: local := CheckEqual(t, 0) where t := Subtract(a, b) |
| 111 | + private static bool IsZeroFlag(LocalVariable? local, Dictionary<LocalVariable, Instruction> defOf, out object? op0, out object? op1) |
| 112 | + { |
| 113 | + op0 = op1 = null; |
| 114 | + var def = Def(local, defOf); |
| 115 | + if (def is not { OpCode: OpCode.CheckEqual } || !IsZeroConstant(def.Operands[2])) |
| 116 | + return false; |
| 117 | + return IsSubtraction(AsLocal(def.Operands[1]), defOf, out op0, out op1); |
| 118 | + } |
| 119 | + |
| 120 | + // SF: local := CheckLess(t, 0) where t := Subtract(a, b) |
| 121 | + private static bool IsSignFlag(LocalVariable? local, Dictionary<LocalVariable, Instruction> defOf, out object? op0, out object? op1) |
| 122 | + { |
| 123 | + op0 = op1 = null; |
| 124 | + var def = Def(local, defOf); |
| 125 | + if (def is not { OpCode: OpCode.CheckLess } || !IsZeroConstant(def.Operands[2])) |
| 126 | + return false; |
| 127 | + return IsSubtraction(AsLocal(def.Operands[1]), defOf, out op0, out op1); |
| 128 | + } |
| 129 | + |
| 130 | + // local := Subtract(a, b) |
| 131 | + private static bool IsSubtraction(LocalVariable? local, Dictionary<LocalVariable, Instruction> defOf, out object? op0, out object? op1) |
| 132 | + { |
| 133 | + op0 = op1 = null; |
| 134 | + var def = Def(local, defOf); |
| 135 | + if (def is not { OpCode: OpCode.Subtract }) |
| 136 | + return false; |
| 137 | + op0 = def.Operands[1]; |
| 138 | + op1 = def.Operands[2]; |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + // local := CheckEqual(SF, OF) - the signed "not less" test. Operands are taken from the SF side. |
| 143 | + private static bool IsSignEqualsOverflow(LocalVariable? local, Dictionary<LocalVariable, Instruction> defOf, out object? op0, out object? op1) |
| 144 | + { |
| 145 | + op0 = op1 = null; |
| 146 | + var def = Def(local, defOf); |
| 147 | + if (def is not { OpCode: OpCode.CheckEqual }) |
| 148 | + return false; |
| 149 | + return IsSignFlag(AsLocal(def.Operands[1]), defOf, out op0, out op1); |
| 150 | + } |
| 151 | + |
| 152 | + // local := Not(CheckEqual(SF, OF)) |
| 153 | + private static bool IsNotSignEqualsOverflow(LocalVariable? local, Dictionary<LocalVariable, Instruction> defOf, out object? op0, out object? op1) |
| 154 | + { |
| 155 | + op0 = op1 = null; |
| 156 | + var def = Def(local, defOf); |
| 157 | + if (def is not { OpCode: OpCode.Not }) |
| 158 | + return false; |
| 159 | + return IsSignEqualsOverflow(AsLocal(def.Operands[1]), defOf, out op0, out op1); |
| 160 | + } |
| 161 | + |
| 162 | + // local := Not(ZF) |
| 163 | + private static bool IsNotZeroFlag(LocalVariable? local, Dictionary<LocalVariable, Instruction> defOf) |
| 164 | + { |
| 165 | + var def = Def(local, defOf); |
| 166 | + return def is { OpCode: OpCode.Not } && IsZeroFlag(AsLocal(def.Operands[1]), defOf, out _, out _); |
| 167 | + } |
| 168 | + |
| 169 | + // And((SF==OF), !ZF), in either operand order |
| 170 | + private static bool IsSignGreater(Instruction and, Dictionary<LocalVariable, Instruction> defOf, out object? op0, out object? op1) |
| 171 | + { |
| 172 | + var left = AsLocal(and.Operands[1]); |
| 173 | + var right = AsLocal(and.Operands[2]); |
| 174 | + |
| 175 | + if (IsSignEqualsOverflow(left, defOf, out op0, out op1) && IsNotZeroFlag(right, defOf)) |
| 176 | + return true; |
| 177 | + if (IsSignEqualsOverflow(right, defOf, out op0, out op1) && IsNotZeroFlag(left, defOf)) |
| 178 | + return true; |
| 179 | + |
| 180 | + op0 = op1 = null; |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
| 184 | + // Or(!(SF==OF), ZF), in either operand order |
| 185 | + private static bool IsSignLessOrEqual(Instruction or, Dictionary<LocalVariable, Instruction> defOf, out object? op0, out object? op1) |
| 186 | + { |
| 187 | + var left = AsLocal(or.Operands[1]); |
| 188 | + var right = AsLocal(or.Operands[2]); |
| 189 | + |
| 190 | + if (IsNotSignEqualsOverflow(left, defOf, out op0, out op1) && IsZeroFlag(right, defOf, out _, out _)) |
| 191 | + return true; |
| 192 | + if (IsNotSignEqualsOverflow(right, defOf, out op0, out op1) && IsZeroFlag(left, defOf, out _, out _)) |
| 193 | + return true; |
| 194 | + |
| 195 | + op0 = op1 = null; |
| 196 | + return false; |
| 197 | + } |
| 198 | + |
| 199 | + private static Instruction? Def(LocalVariable? local, Dictionary<LocalVariable, Instruction> defOf) |
| 200 | + => local != null && defOf.TryGetValue(local, out var def) ? def : null; |
| 201 | + |
| 202 | + private static LocalVariable? AsLocal(object operand) => operand as LocalVariable; |
| 203 | + |
| 204 | + private static bool IsZeroConstant(object operand) => |
| 205 | + operand switch |
| 206 | + { |
| 207 | + int v => v == 0, |
| 208 | + long v => v == 0, |
| 209 | + uint v => v == 0, |
| 210 | + ulong v => v == 0, |
| 211 | + short v => v == 0, |
| 212 | + byte v => v == 0, |
| 213 | + sbyte v => v == 0, |
| 214 | + _ => false |
| 215 | + }; |
| 216 | +} |
0 commit comments