<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/68166>68166</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [TableGen][GISel] Incorrect handling of ignored ComplexPattern with `GISelPredicateCode ` + `PredicateCodeUsesOperands = 1`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            tablegen,
            llvm:globalisel
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mshockwave
      </td>
    </tr>
</table>

<pre>
    Here is the code that triggers this issue (please paste boilerplate code from `test/TableGen/GlobalISelEmitterCustomPredicate.td` at the beginning of this file). Also, this bug follows up the patch presented in #68125.
```
def DOP : RegisterOperand<DRegs>;
def MUL_OR : I<(outs DRegs:$dst), (ins DOP:$src0, DOP:$src1, DOP:$src2), []>;

def or_complex_pattern : ComplexPattern<i32, 2, "selectOrComplex">;

def mul_pat : PatFrag<
  (ops node:$x, node:$y),
  (mul node:$x, node:$y), [{ ... }]> {
  let GISelPredicateCode = [{ ... }];
  let PredicateCodeUsesOperands = 1;
}

def : Pat<
  (i32 (mul_pat (or_complex_pattern DOP:$src0, DOP:$src1), DOP:$src2)),
  (MUL_OR DOP:$src0, DOP:$src1, DOP:$src2)
>;
```

When running with `-gen-global-isel`, llvm-tblgen cashes with the following message:
```
llvm-project/llvm/utils/TableGen/GlobalISelEmitter.cpp:775: Expected<llvm::gi::InstructionMatcher &> (anonymous namespace)::GlobalISelEmitter::createAndImportSelDAGMatcher(llvm::gi::RuleMatcher &, llvm::gi::InstructionMatcher &, const llvm::TreePatternNode *, unsigned int &): Assertion `WaitingForNamedOperands == 0 && "previous predicate didn't find all operands or " "nested predicate that uses operands"' failed.
```

Here is the root cause: `WaitingForNamedOperands` is a counter that keeps tracks of operand mapping while using `GISelPredicateCode ` + `PredicateCodeUsesOperands = 1`. It's subtracted by one for each PatFrag operand and it should be zero, namely the assertion message, before we start processing another PatFrag.
For instance, when processing `(mul_pat (or_complex_pattern DOP:$src0, DOP:$src1), DOP:$src2)`, `WaitingForNamedOperands ` is initialized with 2, the number of PatFrag operands. It is subtracted by 1 after `(or_complex_pattern DOP:$src0, DOP:$src1)` is handled, but since `or_complex_pattern` has no GISel counterpart, GlobalISelEmitter bails out (too) early and skips `DOP:$src2` entirely, leaving `WaitingForNamedOperands` a non-zero value, hence the assertion failure.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vl9v274O_TTKC9HAkRMnechDmiy9Be7Wotuwx0G2GVu3smSIcrvu019Qctv0D9aLXfyANollkSIPDw-liHRjETdicS4W-4kaQuv8pqPWVbf36g4npasfNv9Cj6AJQotQuRohtCpA8Lpp0POyJtBEA4KQq96gIoReUUAonTboe6PCaHn0rgNRZAEpCHn4pkqDF2iFPFwYVypz-RXNp06HgH43UHDdtcdaVyrgNNSiyIAPbhFKbLS12jbgjimAozYo5HoKW0NOyF1aLYcGjs4Yd08w9NG0V6FqofdIaAPWoC0ImRermVxMRbYX2VYU2fgXH2s8wv7qGkS-hRtsNAX0Vz16ZWuR7_Y32JDIP4n8_Hn75-___nl1Ey0uRb4TcuWGQDDu3Qo5rzn_Nccp5Epb4hPSG_JVxuunC7PXC_LROBbu9PjnIJz_WbmuN_jrZ68YURsD2qW167Qk8p3OJbuSKRhJaLAKV37cJ6R83303GPYbfV6rcPCq4VTje-CsXE9gXY0p6F_s_vnxISVwsr0bzIfbY77Lc5hOpyCW-5Q6iOX5ox-DAS6YRE-82THtRL5_z_SF2QuL74Q01pii9ewZgeX-NRQjBC_T17kc00owydU7Bfmo6uv3C_8KupFuf0GhlMhpfV9yP33-aNGCH1LD3evQcgufNWjPmti1Z5rQsI3cgTF33VkoTYMWKkUtUrLg1kudyE46JFJNLO57x0YnvXf_wYplgh-FPAxBG_pANaZV34t8u1wuuCiffvVYBeQ-jT7yrci3jU7fl5aCH6qgnf3MmoAehCwioeRKWW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