108 Commits

Author SHA1 Message Date
Fangrui Song
e559d0a97a [TableGen] Fix -Wunused-variable in non-assertion builds 2026-02-16 10:51:10 -08:00
Craig Topper
23374f95ed Reapply "[TableGen] Reduce number of call to FactorNodes. NFC"
With more fixes to avoid deferencing a before_begin iterator.

Original commit message:

Previously we recursively called FactorNodes all the way down the
tree any time FactorNodes was called. Additionally, on returning
from the recursiion we would flatten any child ScopeMatchers into
the parent.

There are additional calls to FactorNodes every time a new ScopeMatcher
is created. These calls cause a lot of visiting of parts of the tree that
have already been factored as much as possible.

We can remove the primary recursion by ensuring we flatten
scopes when building a new ScopeMatcher. If the Matcher we are
going to insert into the new ScopeMatcher is itself a ScopeMatcher,
we add the children into the new ScopeMatcher instead. This makes
the FactorNodes call for the newly created scope more powerful
eliminating the need for the recursion
2026-02-15 20:47:37 -08:00
Craig Topper
915eb46f51 Revert "Reapply "[TableGen] Reduce number of call to FactorNodes. NFC""
This reverts commit 193c1f4af7f1bd9249c99bc3c0b7d9ebe4698d11.

Still failing on the bots, but it doesn't show up in my local
testing with multiple compilers.
2026-02-15 20:06:43 -08:00
Craig Topper
193c1f4af7 Reapply "[TableGen] Reduce number of call to FactorNodes. NFC"
With a fix to avoid deferencing a before_begin iterator.

Original commit message:

Previously we recursively called FactorNodes all the way down the
tree any time FactorNodes was called. Additionally, on returning
from the recursiion we would flatten any child ScopeMatchers into
the parent.

There are additional calls to FactorNodes every time a new ScopeMatcher
is created. These calls cause a lot of visiting of parts of the tree that
have already been factored as much as possible.

We can remove the primary recursion by ensuring we flatten
scopes when building a new ScopeMatcher. If the Matcher we are
going to insert into the new ScopeMatcher is itself a ScopeMatcher,
we add the children into the new ScopeMatcher instead. This makes
the FactorNodes call for the newly created scope more powerful
eliminating the need for the recursion
2026-02-15 19:01:42 -08:00
Craig Topper
3f83bf3594 Revert "[TableGen] Reduce number of call to FactorNodes. NFC"
This reverts commit 3ec6ca7d58c4a1a7ee68df41388d71c4e4b858d0.

Failing a couple bots even though it passed locally.
2026-02-15 18:18:39 -08:00
Craig Topper
3ec6ca7d58 [TableGen] Reduce number of call to FactorNodes. NFC
Previously we recursively called FactorNodes all the way down the
tree any time FactorNodes was called. Additionally, on returning
from the recursiion we would flatten any child ScopeMatchers into
the parent.

There are additional calls to FactorNodes every time a new ScopeMatcher
is created. These calls cause a lot of visiting of parts of the tree that
have already been factored as much as possible.

We can remove the primary recursion by ensuring we flatten
scopes when building a new ScopeMatcher. If the Matcher we are
going to insert into the new ScopeMatcher is itself a ScopeMatcher,
we add the children into the new ScopeMatcher instead. This makes
the FactorNodes call for the newly created scope more powerful
eliminating the need for the recursion.
2026-02-15 16:42:56 -08:00
Jie Fu
241b8876dd [TableGen] Silence a warning (NFC)
llvm-project/llvm/utils/TableGen/DAGISelMatcherOpt.cpp:495:16:
 error: unused variable 'Tmp' [-Werror,-Wunused-variable]
      Matcher *Tmp =
               ^
1 error generated.
2026-02-15 17:34:13 +08:00
Craig Topper
4ab66e444b [TableGen] Combine FactorNodes and FactorScope. NFC
These were separated in 31db7afacf4dae051fcd0da22e440813663b61f3
due to the complexity of the rebindable unique_ptr. With the
switch to MatcherList, the code is not as complicated.
2026-02-15 00:18:16 -08:00
Craig Topper
8d971c0360 Reapply "[TableGen] Introduce MatcherList to manage a linked list of Matchers. NFC (#177875)"
With fixes to the slice_after functions to avoid dereferencing the
before_begin() iterator. Deferencing the iterator casts a pointer
to the BeforeBegin object to Matcher*, but the BeforeBegin object
is not a Matcher. This caused a failure when built with gcc.

Original commit message:

The previous memory management involved passing around references to the
std::unique_ptr next pointer in each Matcher. Scopes and
SwitchOpcode/SwitchType did not use std::unique_ptr internally, but
would sometimes need to have the pointers to their children moved to
temporary std::unique_ptrs that may be modified and then put back into
the Scope/Switch. Other code used manual deletion.

This patch introduces a MatcherList object that encapsulates list
management and ownership. The interface is based on std::forward_list
using the libcxx implementation for reference.

Once a Matcher is added to a MatcherList it can only be in that list or
spliced into another list. This ensures it is always owned by a list
that will be responsible for deleting it.

Scope and SwitchOpcode/Type children are now MatcherLists rather than
Matcher*. A nice side effect of this is that we no longer need a Size
field in each Matcher to store the size of Scope/Switch children during
emission. This field was only used for the Matcher that the Scope/Switch
children pointed to. Now we can store it in the MatcherList instead.

There is no change to the generated output.

There's still a little bit of cleanup that can be done in follow ups. For example,
renaming "TheMatcher" and merging FactorScope and FactorNodes together.
2026-02-14 23:35:58 -08:00
Craig Topper
4833c51d11 Revert "[TableGen] Introduce MatcherList to manage a linked list of Matchers. NFC (#177875)"
This reverts commit 39613943855e6a82b1bb4c3d16fbaf2289a5d739.

It's failing across multiple build bots.
2026-02-13 18:04:40 -08:00
Craig Topper
3961394385
[TableGen] Introduce MatcherList to manage a linked list of Matchers. NFC (#177875)
The previous memory management involved passing around references to the
std::unique_ptr next pointer in each Matcher. Scopes and
SwitchOpcode/SwitchType did not use std::unique_ptr internally, but
would sometimes need to have the pointers to their children moved to
temporary std::unique_ptrs that may be modified and then put back into
the Scope/Switch. Other code used manual deletion.

This patch introduces a MatcherList object that encapsulates list
management and ownership. The interface is based on std::forward_list
using the libcxx implementation for reference.

Once a Matcher is added to a MatcherList it can only be in that list or
spliced into another list. This ensures it is always owned by a list
that will be responsible for deleting it.

Scope and SwitchOpcode/Type children are now MatcherLists rather than
Matcher*. A nice side effect of this is that we no longer need a Size
field in each Matcher to store the size of Scope/Switch children during
emission. This field was only used for the Matcher that the Scope/Switch
children pointed to. Now we can store it in the MatcherList instead.

There is no change to the generated output.

There's still a little bit of cleanup that can be done in follow ups. For example,
renaming "TheMatcher" and merging FactorScope and FactorNodes together.
2026-02-13 17:56:48 -08:00
Craig Topper
13c7640406 [TableGen] Fix incorrect indentation in DAGISelMatcherOpt.cpp. NFC 2026-01-19 12:02:45 -08:00
Craig Topper
0a7cafc51d
[TableGen] Remove temporary vector from FactorScope. NFC (#176547)
Operate directly on the vector in the ScopeMatcher.
2026-01-17 21:31:24 -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
13055d60bb
[TableGen] Move DAGISelMatcher.cpp/h out of the Common library. NFC (#175271)
These files contain the implementation of classes used by only the
-gen-dag-isel backend. I don't think they are reusable without
DAGISelMatcherGen.cpp and DAGISelMatcherEmitter.cpp. I don't think
there's anything worth unittesting in just these files either.
2026-01-09 22:35:32 -08: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
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
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
Kazu Hirata
4ec473e0e1
[llvm] Remove redundant calls to std::unique_ptr<T>::get (NFC) (#138236) 2025-05-02 08:53:53 -07:00
Craig Topper
04e5ea5237 [TableGen] Remove recursive walk of linked list from ContractNodes. NFC
After f9250401ef120a4605ad67bb43d3b25500900498, this function is
tail recursive so it was straightforward to convert this to iteratively
walk the linkd list.
2025-02-09 14:23:34 -08:00
Craig Topper
4aa71f0d4c [TableGen] Fix an unused variable warning. NFC 2025-02-08 16:03:44 -08:00
Craig Topper
f9250401ef [TableGen] Move formation of MoveSiblingMatcher earlier in ContractNodes. NFC
ContractNodes recursively walks forward through a linked list. During
this recursion, Matchers are combined into other Matchers.

Previously the formation of MoveSiblingMatcher was after the
recursive call so it occurred as we were unwinding. If a
MoveSiblingMatcher was formed, we would recursively walk forward
to the end of the linked list again which isn't efficient.

To make this more efficient, move the formation of MoveSiblingMatcher
to the forward pass. Add additional rules to unfold MoveSiblingMatcher
if it would be more efficient to use CheckChildType, CheckChildInteger,
CheckChildSame, etc.

As an added benefit, this makes the function tail recursive which
the compiler can better optimize.
2025-02-08 15:58:12 -08:00
Craig Topper
01ac6fc909 [TableGen] Use range-based for loops. NFC 2025-02-06 19:14:10 -08:00
Craig Topper
5eed019080 [TableGen] Use std::copy instead of a manual copy loop. NFC 2025-02-06 00:47:18 -08:00
Craig Topper
31bd82cdcc [TableGen] Don't try to move CheckOpcode before CheckType/CheckChildType in ContractNodes. NFC
It appears that CheckOpcode is already emitted before CheckType so
this hasn't been doing anything on any in tree targets.
2025-02-04 21:17:20 -08:00
Craig Topper
31db7afacf
[TableGen] Split DAGISelMatcherOpt FactorNodes into 2 functions. NFC (#125330)
The loop at the top of FactorNodes creates additional variables to deal
with needing to use a pointer to a unique_ptr instead of a reference.
Encapsulate this to its own function for better scoping.

This also allows us to directly skip this loop when we already know we
have a ScopeMatcher.
2025-02-02 19:04:59 -08:00
Craig Topper
2d17fc4ca3 [TableGen] Reorder code in ContractNodes to prevents unnecessary recursion. NFC
The code that moves CheckOpcode before CheckType/CheckChildType/RecordDwith
was running after ContractNodes started unwinding its recursion. If a
move occurs we would start a new recursion going forward
through the list again. I don't believe this can lead to any new
combines so it was just wasted work.

This patch moves the code earlier so it doesn't start a new recursion.
2025-01-31 23:45:40 -08:00
Craig Topper
028b69009a [TableGen] Remove unnecessary check before calling SmallVector::erase. NFC
This was checking whether the erase is needed, but erase is safe
to call with equal iterators.
2025-01-31 21:42:11 -08: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
Rahul Joshi
708567ab0b
[LLVM][TableGen] Adopt indent for indentation (#109275)
Adopt `indent` for indentation DAGISelMatcher and DecoderEmitter.
2024-09-20 04:28:01 -07:00
Pierre van Houtryve
fa3d789df1
[RFC][TableGen] Restructure TableGen Source (#80847)
Refactor of the llvm-tblgen source into:
- a "Basic" library, which contains the bare minimum utilities to build
`llvm-min-tablegen`
- a "Common" library which contains all of the helpers for TableGen
backends. Such helpers can be shared by more than one backend, and even
unit tested (e.g. CodeExpander is, maybe we can add more over time)

Fixes #80647
2024-03-25 09:40:35 +01:00
Jay Foad
f723260a80
[TableGen] Stop using make_pair and make_tuple. NFC. (#81730)
These are unnecessary since C++17.
2024-02-14 13:16:20 +00:00
Tomas Matheson
a9e546cc71
[TableGen][NFC] convert TreePatternNode pointers to references (#81134)
Almost all uses of `*TreePatternNode` expect it to be non-null. There
was the occasional check that it wasn't, which I have removed. Making
them references makes it clear that they exist.

This was attempted in 2018 (1b465767d6ca69f4b7201503f5f21e6125fe049a)
for `TreePatternNode::getChild()` but that was reverted.
2024-02-09 13:35:42 +00:00
Pierre van Houtryve
b9079baadd
[NFC] clang-format utils/TableGen (#80973)
```
find llvm/utils/TableGen -iname "*.h" -o -iname "*.cpp" | xargs clang-format-16 -i
```

Split from #80847
2024-02-09 09:27:04 +01:00
Wang Pengcheng
714417455d
[SelectionDAG] Add OPC_MoveSibling (#73643)
There are a lot of operations to move current node to parent and
then move to another child.

So `OPC_MoveSibling` and its space-optimized forms are added to do
this "move to sibling" operations.

These new operations will be generated when optimizing matcher in
`ContractNodes`. Currently `MoveParent+MoveChild` will be optimized
to `MoveSibling` and sequences `MoveParent+RecordChild+MoveChild`
will be transformed into `MoveSibling+RecordNode`.

Overall this reduces the llc binary size with all in-tree targets by
about 30K.
2023-12-12 17:48:45 +08:00
Craig Topper
d280187093 [TableGen] Re-work FactorNodes to avoid an extra temporary vector.
Instead of copying elements to the NewOptionsToMatchVector, we
now shift down elements in OptionsToMatch as we create holes by
pulling out equal matchers. After we finish processing the vector,
we'll trim the size to just the elements that are still in use.
2023-04-22 21:04:55 -07:00
Craig Topper
44d46c4b3c [TableGen] Store CodeGenInstruction reference in EmitNodeMatcherCommon. NFC
Instead of storing a string containing the instruction name, store a
reference to the instruction. We can use that reference to print the
instruction name when we emit the table.

The only slightly annoying part is that we have to find the
CodeGenInstruction for IMPLICIT_DEF. GlobalISel is doing
a similar thing.
2023-04-12 20:44:36 -07:00
Craig Topper
ef2d2a11e3 [TableGen] Rename InFlag/OutFlag->InGlue/OutGlue. NFC
Flag was renamed to Glue a long time ago, but rename was incomplete.
2023-04-02 14:12:10 -07:00
Craig Topper
2b644270de [TableGen] clang-format DAGISelMatcherOpt.cpp. NFC 2023-04-02 14:05:02 -07:00
Craig Topper
2be67e14ba [TableGen] Avoid creating a ScopeMatcher full of nullptrs.
The call to FactorNodes will catch it and remove it, but it's easy
to catch at creation.

Remove the now unnecessary null checks from a loop in factor nodes.
2023-04-02 12:02:55 -07:00
Craig Topper
50c45e0ac7 [TableGen] Move some vectors into place instead of copying them. 2023-04-01 15:41:29 -07:00
NAKAMURA Takumi
afde3f549d llvm-tblgen: Apply IWYU partially 2023-02-17 00:32:46 +09:00
Kazu Hirata
2aed08131d [llvm] Use true/false instead of 1/0 (NFC)
Identified with modernize-use-bool-literals.
2022-01-07 00:39:14 -08:00
Simon Pilgrim
22d225a2b2 DAGISelMatcherOpt - TGParser::ParseOperation - silence static analyzer cast_or_null<CheckTypeMatcher> null dereference warning. NFCI.
The static analyzer is warning about a potential null dereference, replace with an null/isa assertion and cast<CheckTypeMatcher>.

llvm-svn: 373001
2019-09-26 17:38:47 +00:00
Craig Topper
8c9724ea4f [SelectionDAG] Add a OPC_CheckChild2CondCode to SelectionDAGISel to remove a MoveChild and MoveParent pair.
OPC_CheckCondCode is always used as operand 2 of a setcc. And its always surrounded by a MoveChild2 and a MoveParent. By having a dedicated opcode for this case we can reduce the number of bytes needed for this pattern from 4 bytes to 2.

This saves ~3000 bytes in the X86 table.

llvm-svn: 354763
2019-02-25 03:11:44 +00:00
Chandler Carruth
2946cd7010 Update the file headers across all of the LLVM projects in the monorepo
to reflect the new license.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

llvm-svn: 351636
2019-01-19 08:50:56 +00:00
Nicola Zaghen
d34e60ca85 Rename DEBUG macro to LLVM_DEBUG.
The DEBUG() macro is very generic so it might clash with other projects.
The renaming was done as follows:
- git grep -l 'DEBUG' | xargs sed -i 's/\bDEBUG\s\?(/LLVM_DEBUG(/g'
- git diff -U0 master | ../clang/tools/clang-format/clang-format-diff.py -i -p1 -style LLVM
- Manual change to APInt
- Manually chage DOCS as regex doesn't match it.

In the transition period the DEBUG() macro is still present and aliased
to the LLVM_DEBUG() one.

Differential Revision: https://reviews.llvm.org/D43624

llvm-svn: 332240
2018-05-14 12:53:11 +00:00
Jon Chesterfield
1b4eed4c1e [TableGen] Use less stack in DAGISelMatcherOpt
Refactor a helper function, FactorNodes, to search for a push node in constant space. This resolves a problem in a not-yet-upstreamed backend where a recursive pattern blew the call stack (at a depth of 255) under a debug build of tablegen. No functional change so no new test coverage. The change is minimal to avoid disturbing existing behaviour.

Differential Revision: https://reviews.llvm.org/D29080

llvm-svn: 294230
2017-02-06 19:41:44 +00:00
Craig Topper
3dc066754c [TableGen][ISel] When factoring ScopeMatcher, if the child of the ScopeMatcher we're working on is also a ScopeMatcher, merge all its children into the one we're working on.
There were several cases in X86 where we were unable to fully factor a ScopeMatcher but created nested ScopeMatchers for some portions of it. Then we created a SwitchType that split it up and further factored it so that we ended up with something like this:

SwitchType
  Scope
    Scope
      Sequence of matchers
      Some other sequence of matchers
    EndScope
    Another sequence of matchers
  EndScope
...Next type

This change turns it into this:

SwitchType
  Scope
    Sequence of matchers
    Some other sequence of matchers
    Another sequence of matchers
  EndScope
...Next type

Several other in-tree targets had similar nested scopes like this. Overall this doesn't save many bytes, but makes the isel output a little more regular.

llvm-svn: 287624
2016-11-22 07:00:06 +00:00
Craig Topper
5e87d5264f [TableGen][ISel] Do a better job of factoring ScopeMatchers created during creation of SwitchTypeMatcher.
Previously we were factoring when the ScopeMatcher was initially created, but it might get more Matchers added to it later. Delay factoring until we have fully created/populated the ScopeMatchers.

This reduces X86 isel tables by 154 bytes.

llvm-svn: 287520
2016-11-21 04:07:58 +00:00