72 Commits

Author SHA1 Message Date
Craig Topper
59e01a1e2c
[TableGen] Add new line to end of TreePatternNode::dump. (#186865) 2026-03-16 15:59:32 -07:00
Craig Topper
17cfa269c7
[TableGen] Use dbgs() instead of errs() in dump functions and LLVM_DEBUG (#186508) 2026-03-13 14:23:21 -07:00
Alexander Richardson
3459bb4f27
[TableGen] Introduce RegisterByHwMode
This is useful for `InstAlias` where a fixed register may depend on the
HwMode. The motivating use case for this is the RISC-V RVY ISA where
certain instructions mnemonics are remapped to take a different
register class depending on the HwMode and can be used as follows:
```
def NullReg : RegisterByHwMode<PtrRC, [RV32I, RV64I, RV64Y, RV64Y],
                                      [X0,    X0,    X0_Y,  X0_Y]>;
```

Pull Request: https://github.com/llvm/llvm-project/pull/175227
2026-02-18 17:23:10 -08:00
Prerona Chaudhuri
8f1427d269
[TableGen] Gracefully error out in ParseTreePattern when DAG has zero operands so that llvm-tblgen doesn't crash (#161417)
Also handle the case when Pat->Child(i) is null in
CodeGenDAGPatterns::FindPatternInputsAndOutputs().
Fixes issue #157619 : TableGen asserts on invalid cast
2026-01-21 17:31:59 +00:00
Daniel Kuts
ade2435ca5
Add missing return on nullptr check (#92125)
Fixes #92123
2026-01-20 15:38:41 -08:00
Craig Topper
08de4fd0d4
[SelectionDAG] Move HwMode expansion from tablegen to SelectionISel. (#174471)
The way HwMode is currently implemented, tablegen duplicates each
pattern that is dependent on hardware mode. The HwMode predicate is
added as a pattern predicate on the duplicated pattern.
    
RISC-V uses HwMode on the GPR register class which means almost every
isel pattern is affected by HwMode. This results in the isel table
being nearly twice the size it would be if we only had a single GPR
size.

This patch proposes to do the expansion at instruction selection time
instead. To accomplish this new opcodes like OPC_CheckTypeByHwMode
are added to the isel table. The unique combinations of types and HwMode
are converted to an index that is the payload for the new opcodes.
TableGen emits a new virtual function getValueTypeByHwMode that uses
this index and the current HwMode to look up the type.

This reduces the size of the isel table on RISC-V from ~2.38 million
bytes to ~1.38 million bytes.

I did not add an OPC_SwitchTypeByHwMode opcode yet. If the VT requires a
hardware mode, we emit an OPC_Scope+OPC_CheckTypeByHwMode instead. I
expect adding an OPC_SwitchTypeByHwMode could further reduce the table
size. I will investigate this as a follow up.
    
Many of the matcher classes in tablegen now use ValueTypeByHwMode
insteadof MVT. This may have an impact on the memory usage and runtime of
tablegen. We can mitigate some of this by splitting the matchers into MVT and
ValueTypeByHwMode versions. We can also explore alternate data
structures for ValueTypeByHwMode instead of a std::map. Maybe a sorted vector.

A similar change can be made to GlobalISel as a follow up.
2026-01-15 09:35:02 -08:00
Craig Topper
af50f9c4d8
[TableGen] Return unknown type for RegClassByHwMode when NotRegister is set in getImplicitType. (#174488)
This matches what is done for regular RegisterClass. This flag is used
by DAGISelMatcherGen when it is deciding which type checks are needed.
2026-01-05 15:22:50 -08:00
Craig Topper
a052b578ad
[TableGen] Remove TypeInfer::isConcrete/getConcrete. NFC (#174235)
These don't use any state from TypeInfer and only wrap methods in
TypeSetByHwMode. We can use the TypeSetByHwMode methods directly.
2026-01-02 12:49:56 -08:00
Craig Topper
bb98de9349
[TableGen] Fix TypeSetByHwMode::getValueTypeByHwMode. (#174182)
This should convert the type set for each HwMode to an MVT for that
HwMode. Instead, if a single type existed for the DefaultMode, that was
used for the MVT of every other mode.

This didn't cause an issue because there is only one place this function
is used before HwModes are expanded. That's just verifying that
constants are small enough for the MVT for each mode. So you would need
a large constant and a HwMode with a smaller VT than the default mode.
2026-01-02 09:12:37 -08:00
Craig Topper
3eef4f52c4
[TableGen] Remove unused pattern rewriting functionality from CodeGenDAGPatterns. NFC (#174032)
This was originally added for GlobalISel and has been unused since
f84bc3793e9d1ba170a35b1909dd1057b63c2f15, 7.5 years ago.
2025-12-31 09:50:30 -08:00
Alexander Richardson
f672f32fa2
[TableGen] Improve error message for bad VTByHwMode in RegisterByHwMode
Previously we would assert when a ValueTypeByHwMode was missing a case
for the current mode, now we report an error instead. Interestingly this
error only ocurrs when the DAG patterns use RegClassByHwMode, but not
normal RegisterClass instances. Found while I added RegClassByHwMode
to RISC-V and was getting an assertion due to `XLenFVT`/`XLenVecI32VT`
not having an entry for the default mode.

Reviewed By: arsenm

Pull Request: https://github.com/llvm/llvm-project/pull/171254
2025-12-10 13:59:38 -08:00
Matt Arsenault
9b88cd9945
CodeGen: Remove PointerLikeRegClass handling from codegen (#159883)
All uses have been migrated to RegClassByHwMode. This is now
an implementation detail of InstrInfoEmitter for pseudoinstructions.
2025-11-26 10:14:37 -05:00
Anatoly Trosinenko
d416289417
[TableGen] Eliminate the dependency on SDNode definition order (#168745)
Fix the dependency of `CodeGenDAGPatterns::ParseDefaultOperands()` on
the particular order of SDNode definitions. Implicit usage of the first
definition as a placeholder makes `llvm-tblgen -gen-dag-isel` fail if
that SDNode is not usable as an output pattern operator and an instance
of `OperandWithDefaultOps` is used in a pattern.

Presently, each `OperandWithDefaultOps` record is processed by
constructing an instance of TreePattern from its `DefaultOps` argument
that has the form `(ops ...)`. Even though the result of processing the
root operator of that DAG is not inspected by `ParseDefaultOperands()`
function itself, that operator has to be supported by the underlying
`TreePattern::ParseTreePattern()` function. For that reason, a temporary
DAG is created by replacing the root operator of `DefaultOps` argument
with the first SDNode defined, which is usually `def imm : ...` defined
in `TargetSelectionDAG.td` file.

This results in misleading errors being reported when implementing new
`SDNode` types, if the new definition happens to be added before the
`def imm : ...` line. The error is reported by several test cases
executed by `check-llvm` target, as well as by the regular build, if one
of the enabled targets inherit one of its operand types from
`OperandWithDefaultOps`:

    OptionalIntOperand: ../llvm/test/TableGen/DAGDefaultOps.td:28:5: error: In OptionalIntOperand: Cannot use 'unexpected_node' in an output pattern!
    def OptionalIntOperand: OperandWithDefaultOps<i32, (ops (i32 0))>;

This commit implements a dedicated constructor of `TreePattern` to be
used if the caller does not care about the particular root operator of
the pattern being processed.
2025-11-24 14:59:19 +03:00
Craig Topper
0ef522ff68
[TableGen] Use MVT instead of MVT::SimpleValueType. NFC (#169180)
This improves type safety and is less verbose. Use SimpleTy only where
an integer is needed like switches or emitting a VBR.

---------

Co-authored-by: Sergei Barannikov <barannikov88@gmail.com>
2025-11-22 17:00:56 -08:00
Sergei Barannikov
0619292195
[TableGen] Constify CodeGenInstruction where possible (NFC) (#169193) 2025-11-23 00:22:48 +00:00
Kazu Hirata
02976f5ffa
[TableGen] Use "using" instead of "typedef" (NFC) (#167168)
Identified with modernize-use-using.
2025-11-08 13:09:03 -08:00
Rahul Joshi
bd7e228fa4
[NFC][TableGen] Fix namespace usage in various files (#161839)
- Move standalone functions and variables out of anonymous namespace and
make them static.
- Eliminate `namespace llvm {}` wrapping all code in .cpp files, and
instead use namespace qualifier to define such functions
(https://llvm.org/docs/CodingStandards.html#use-namespace-qualifiers-to-implement-previously-declared-functions)
- Add namespace for X86DisassemblerShared.h.
2025-10-03 09:27:04 -07:00
Owen Anderson
b05101b864
[TableGen, CodeGen, CHERI] Add support for the cPTR wildcard value type. (#158426)
cPTR is a wildcard CHERI capability value type, used analogously to iPTR. This allows TableGen patterns to abstract over CHERI capability widths.

Co-authored-by: Jessica Clarke <jrtc27@jrtc27.com>
2025-09-25 22:49:40 +09:00
Matt Arsenault
6b54c92be0
CodeGen: Add RegisterClass by HwMode (#158269)
This is a generalization of the LookupPtrRegClass mechanism.
AMDGPU has several use cases for swapping the register class of
instruction operands based on the subtarget, but none of them
really fit into the box of being pointer-like.

The current system requires manual management of an arbitrary integer
ID. For the AMDGPU use case, this would end up being around 40 new
entries to manage.

This just introduces the base infrastructure. I have ports of all
the target specific usage of PointerLikeRegClass ready.
2025-09-19 20:08:51 +09:00
Matt Arsenault
e79f4511a2
TableGen: Replace assertion with error for unexpected pattern inputs (#159687) 2025-09-19 03:57:50 +00:00
Sergei Barannikov
7f4c297e94
[TableGen][CodeGen] Remove feature string from HwMode (#157600)
`Predicates` and `Features` fields serve the same purpose. They should
be kept in sync, but not all predicates are based on features. This
resulted in introducing dummy features for that only reason.

This patch removes `Features` field and changes TableGen emitters to use
`Predicates` instead.

Historically, predicates were written with the assumption that the
checking code will be used in `SelectionDAGISel` subclasses, meaning
they will have access to the subclass variables, such as `Subtarget`.
There are no such variables in the generated
`GenSubtargetInfo::getHwModeSet()`, so we need to provide them. This can
be achieved by subclassing `HwModePredicateProlog`, see an example in
`Hexagon.td`.
2025-09-10 12:39:47 +03:00
Rahul Joshi
5f2e88a125
[NFC][TableGen] Rename CodeGenTarget instruction accessors (#146767)
Rename `getXYZInstructionsByEnumValue()` to just `getXYZInstructions`
and drop the `ByEnumValue` in the name.
2025-07-07 08:01:14 -07:00
Austin
a550fef906
[llvm] Use llvm::fill instead of std::fill(NFC) (#146911)
Use llvm::fill instead of std::fill
2025-07-04 14:10:28 +08:00
Rahul Joshi
fba63e3281
[NFC][TableGen] Use ArrayRef instead of const vector reference (#145323)
- Use `ArrayRef` instead of `SmallVector` reference in a few places.
- Drop redundant `llvm::` in a few places.
2025-06-24 07:30:00 -07:00
Jay Foad
39ad3151e0
[TableGen] Use default member initializers. NFC. (#144349)
Automated with clang-tidy -fix -checks=-*,modernize-use-default-member-init
2025-06-16 15:26:47 +01:00
Kazu Hirata
7a4a83b551
[TableGen] Use range-based for loops (NFC) (#144283) 2025-06-15 21:00:29 -07:00
jyli0116
f3ffee601c
[GISel][AArch64] Allow PatLeafs to be imported in GISel which were previously causing warnings (#140935)
Previously PatLeafs could not be imported, causing the following
warnings to be emitted when running tblgen with
`-warn-on-skipped-patterns:`
```
/work/clean/llvm/lib/Target/AArch64/AArch64InstrInfo.td:2631:1: warning: Skipped pattern: Src pattern child has unsupported predicate
def : Pat<(i64 (mul top32Zero:$Rn, top32Zero:$Rm)),
^
```
These changes allow the patterns to now be imported successfully.
2025-06-09 09:02:56 +01:00
Jay Foad
432c5f2c60
[TableGen] Use emplace instead of insert and similar. NFC. (#143164) 2025-06-07 09:32:36 +01:00
Jay Foad
bb9dcb27df
[TableGen] Use contains instead of count. NFC. (#143156) 2025-06-07 09:31:42 +01:00
Rahul Joshi
b5e3d8ec08
[LLVM][TableGen] Use StringRef for various members CGIOperandList::OperandInfo (#140625)
- Change `Name`, `SubopNames`, `PrinterMethodName`, and
`EncoderMethodNames` to be stored as StringRef.
- Also changed `CheckComplexPatMatcher::Name` to StringRef as a fallout
from the above.

Verified that all the tablegen generated files within LLVM are
unchanged.
2025-05-21 06:23:01 -07:00
Rahul Joshi
3932360b14
[LLVM][TableGen] Rename ListInit::getValues() to getElements() (#140289)
Rename `ListInit::getValues()` to `getElements()` to better match with
other `ListInit` members like `getElement`. Keep `getValues()` for
existing downstream code but mark it deprecated.
2025-05-19 12:16:33 -07:00
Rahul Joshi
7674d6fa9e
[LLVM][TableGen] Simplify DagInit::get (#140056)
- Add `DagInit::get` overloads that do not need ValName to be specified.
- Fix some calls to either not create temporary arrays for DAG args or
use the std::pair<> overload.
2025-05-16 09:45:58 -07:00
Rahul Joshi
9981afc5f9
[NFC][TableGen] Use StringRef::str() instead of casting (#139332)
- Also eliminate unneeded std::string() around some literal strings.
2025-05-12 15:41:27 -07:00
Rahul Joshi
2e8b539e71
[NFC][TableGen] Add {} for else when if body has {} (#139420) 2025-05-12 08:34:12 -07:00
David Green
9b1051281e
[DAG] Use SDValue for PatFrag checks (#137519)
If the SDNode is used it can pick up the wrong results number, for
example looking at the known bits of the first result where it should be
looking at the second. The SDValue is already present as the
SelectCodeCommon checks move from parent to child, pass the SDValue
through to CheckNodePredicate as Op so that it can use it if necessary.
SDNode *N is still generated, keeping most PatFrags the same.

Fixes #137274
2025-05-01 08:58:59 +01:00
Kazu Hirata
4c1dc85e26
[TableGen] Use llvm::interleaved (NFC) (#137483) 2025-04-26 18:18:46 -07:00
Craig Topper
9cf08b409c
[TableGen][SelectionDAG][GISel][RISCV] Support IsNonExtLoad for IsAtomic PatFrags. (#137401)
Use it for RISC-V as a demonstration. Other targets will follow.
2025-04-25 15:45:43 -07:00
Craig Topper
2ca071b1de
[TableGen][RISCV][AArch64][GISel] Properly implement isAnyExtLoad/isSignExtLoad/isZeroExtLoad for IsAtomic in SelectionDAG. (#137096)
Support isAnyExtLoad() for IsAtomic in GISel.

Modify atomic_load_az* to check for extload or zextload. And rename to
atomic_load_azext*

Add atomic_load_asext* and use in RISC-V. I used "asext" rather than
"as" so it wouldn't be confused with the word "as".
2025-04-24 08:27:38 -07:00
Craig Topper
e020fc1895
[TableGen] Directly use SDNode functions to implement HasOneUse and HasNoUse. NFC (#133976)
The SDValue functions we were calling wrap SDNode functions we can call
directly.
2025-04-01 22:14:17 -07:00
Kazu Hirata
673f4705a8
[llvm] Use *Set::insert_range (NFC) (#133353)
We can use *Set::insert_range to collapse:

  for (auto Elem : Range)
    Set.insert(E.first);

down to:

  Set.insert_range(llvm::make_first_range(Range));

In some cases, we can further fold that into the set declaration.
2025-03-27 20:44:20 -07:00
Kazu Hirata
41b76119ec
[llvm] Use range constructors for *Set (NFC) (#132636) 2025-03-23 15:50:34 -07:00
Kazu Hirata
599005686a
[llvm] Use *Set::insert_range (NFC) (#132325)
DenseSet, SmallPtrSet, SmallSet, SetVector, and StringSet recently
gained C++23-style insert_range.  This patch replaces:

  Dest.insert(Src.begin(), Src.end());

with:

  Dest.insert_range(Src);

This patch does not touch custom begin like succ_begin for now.
2025-03-20 22:24:06 -07:00
Craig Topper
146ef7a5f4
[TableGen] Remove unnecessary const_cast and use range-based for loops. NFC (#130717)
In order to use a range-based loop, I reduced a needed const_cast to
only the one line that needed it.
2025-03-11 09:21:10 -07:00
Sergei Barannikov
6aeffcdb91
[TableGen] Add a backend generating SDNode descriptions (#123002)
This patch adds a simplistic backend that gathers all target-specific
SelectionDAG nodes and emits descriptions for most of them.

This includes generating node enumeration, node names, and information
about node "prototype" that can be used to verify that a node is valid.

The patch also extends SDNode by adding target-specific flags, which are
also included in the generated tables.

Part of #119709,
[RFC](https://discourse.llvm.org/t/rfc-tablegen-erating-sdnode-descriptions/83627).

Pull Request: https://github.com/llvm/llvm-project/pull/123002
2025-01-22 09:01:08 +03:00
Jay Foad
4e8c9d2813
[TableGen] Use std::pair instead of std::make_pair. NFC. (#123174)
Also use brace initialization and emplace to avoid explicitly 
constructing std::pair, and the same for std::tuple.
2025-01-16 13:20:41 +00:00
Kazu Hirata
0575815b70
[TableGen] Avoid repeated hash lookups (NFC) (#120681) 2024-12-20 10:28:58 -08:00
Kazu Hirata
5b5b241edf
[TableGen] Avoid repeated hash lookups (NFC) (#120619) 2024-12-19 13:02:55 -08:00
Kazu Hirata
b0a4b5b35a
[TableGen] Avoid repeated hash lookups (NFC) (#120532) 2024-12-19 08:00:02 -08:00
Sergei Barannikov
97c3c32372
[TableGen][SystemZ] Correctly check the range of a leaf immediate (#119931)
The "Size >= 32" check probably dates back to when TableGen integers
were 32-bit. Delete it and simplify code by using `isInt`/`isUInt`.
2024-12-14 13:58:23 +03:00
Sergei Barannikov
d1f51c67fd
[TableGen] Add TreePatternNode::children and use it in for loops (NFC) (#119877) 2024-12-13 22:05:57 +03:00