The primary motivation for this is BINARY_OP, but it can apply to BINARY_SUBSCR, COMPARE_OP, and CONTAINS_OP.
The idea is that we add a single tier1 specialization that contains a table lookup. The table is const table of function pointers.
That is:
replaced op(_BINARY_OP_TABLE_LOOKUP, (index/1, left, right -- res)) {
res = BinaryOpTable[index](left, right);
}
macro (BINARY_OP_TABLE) = _BINARY_TYPE_CHECK + _BINARY_OP_TABLE_LOOKUP;
We replace _BINARY_OP_TABLE_LOOKUP in tier 2 with
replicate(N) op(_BINARY_OP_INLINE, (left, right -- res)) {
res = BinaryOpTable[oparg](left, right);
}
Note how _BINARY_OP_INLINE replaces index with oparg.
As oparg is a const, the C compiler will (at least it should) eliminate the table lookup.
If we declare the relevant function as inline, it will be inlined and we end up with an optimal tier 2 uop.