50 Commits

Author SHA1 Message Date
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
Rahul Joshi
dc8d35aedf
[NFC][TableGen] Adopt IfDefEmitter in CompressInstEmitter (#178998) 2026-02-02 08:03:34 -08:00
Alexander Richardson
700d1f029d
[TableGen] Support RegClassByHwMode in CompressPat
This does not yet handle all cases but at least for the simple
cases such as:
```
def : CompressPat<(PTR_MOV PtrRC:$dst, PtrRC:$src),
                  (PTR_MOV_SMALL PtrRC:$dst, PtrRC:$src)>;
```
tablegen generates sensible code instead of emitting confusing errors.

Reviewed By: arsenm

Pull Request: https://github.com/llvm/llvm-project/pull/171061
2026-01-08 11:32:30 -08:00
Alexander Richardson
5079260ec7
[TableGen] Slightly improve error location for a fatal error
I was hitting this error and the error location was pointing to the
register class definition instead of the incorrect InstAlias. Pass in
the InstAlias location to make it easier to debug.

Happens with `def : InstAlias<"foo", (Inst X0)>`, where `Inst` takes
a RegClassByHwMode operand that is not necessarily satisfied by
register X0. Similar problem with the CompressPat backend.

Reviewed By: arsenm

Pull Request: https://github.com/llvm/llvm-project/pull/170790
2025-12-08 21:53:46 -08:00
Owen Anderson
f3703f36ee
[TableGen] Look up registers directly in the CodeGenRegBank in CompressInstEmitter, rather than indirecting via the name. (#161853)
The previous code was subtly incorrect, as it indexed the RegistersByName map using the tblgen Def name of the register, rather than the AsmName with which the table was initialized. But all of this indirection via the name was unnecessary.
2025-10-04 22:45:13 +09:00
Rahul Joshi
927eba2456
[NFC][TableGen] Adopt CodeGenInstruction::getName() (#156968)
Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-09-04 16:49:26 -07:00
Craig Topper
dcff523123
[TableGen] Use ListSeparator to handle joining condition checks in CompressInstEmitter. NFC (#151089)
This avoids needing to remove the leading indentation and trailing '
&&\n' when we are done with all conditions.

While there remove a few extra parentheses and fix a case where 6
spaces of indentation was used instead of 8.
2025-07-28 23:51:30 -07:00
Craig Topper
b103311c1d
[TableGen] Check destination instruction predicates in CompressInstEmitter. (#151061)
In addition to checking the predicate from the CompressPat, also check
the destination instruction. This prevents creating bad instructions if
CompressPat isn't a proper subset of the destination instruction. This
prevents mistakes that we can't catch at compile time.

We are able to verify RegisterClass hierarchy at compile time so don't
have to check the destination register class.

I've added comments for the operand names to make auditing easier.
2025-07-28 21:36:19 -07:00
Craig Topper
18397f6063
[RISCV] Use Record from CompressPat in compress/uncompress functions. (#150664)
Instead of using the Record from the instruction definition, use the
Record specified in the CompressPat DAG. This will allow us to use
Records that are subsets of both the source and destination.

I want to use this to merge the C_*_HINT instructions back into their
regular non-HINT versions, but prevent those encodings from being part
of compress/uncompress. For example, we will use GPRNoX0 for the C_ADDI
CompressPat while both C_ADDI and ADDI will use GPR in their instruction
definitions.

To do this I've recorded the original DAG Record in the OperandMap using
a struct in the union to represent the 3 fields needed for an Operand.
Previously we stored TiedOpIdx outside the union, but only used it for
Operand.

There is a verification hole here where we don't have any way to check
that an immediate predicate is a subset of an instruction predicate at
tablegen time. Prior to #148660 we had this hole in one direction, but
that patch made it in two directions. I'm not sure if this patch makes
it any worse. Now we're using what is in the CompressPat where before we
were using whatever was in the instructions and ignoring the predicate
in the CompressPat.
2025-07-25 14:15:02 -07:00
Craig Topper
83e51773f8 [TableGen] Improve formatting of CompressInstEmitter output. NFC 2025-07-25 09:42:19 -07:00
Craig Topper
413c0800ce [TableGen] Add const and avoid a copy in CompressInstEmitter::emitCompressInstEmitter. NFC 2025-07-25 09:16:42 -07:00
Craig Topper
ffc5385f8e [TableGen] Fix typo in CompressInstEmitter.
We should use ImmVal field instead of Imm which is from the Kind enum.

This caused us to check a value of 1 instead of of the what was
parsed.
2025-07-24 23:47:06 -07:00
Craig Topper
bd91e8a5bd
[TableGen] Strengthen check for what operands can be an immediate in CompressInstEmitter. (#150568)
Registers can be represented by RegisterOperand, not just RegisterClass.
Instead of trying to block certain classes, only allow Operand.
2025-07-24 21:54:51 -07:00
Craig Topper
076d3050f1
[RISCV] Merge verifyDagOpCount into addDagOperandMapping in CompressInstEmitter. (#150548)
This reduces the number of times we need to iterate over the operands.
2025-07-24 19:50:13 -07:00
Craig Topper
236b315a98
[RISCV] Remove extra operands from Zcb compression patterns. NFC (#150541)
Tied operands in the destination are not supposed to be listed. This
didn't cause a functional issue because none of the code noticed that
extra operand was even there.

Simplify verifyDagOpCount to catch this case.
2025-07-24 18:03:30 -07:00
Sudharsan Veeravalli
085e8f1e52
[RISCV] Relax destination instruction dag operand matching in CompresInstEmitter (#148660)
We have some 48-bit instructions in the `Xqci` spec that currently
cannot be compressed to their 32-bit variants due to the constraint in
`CompressInstEmitter` on destination instruction operands not being
allowed to mismatch with the DAG operands.

For eg. the` QC_E_ADDI` instruction can be compressed to the `ADDI`
instruction when the immediate is signed-12 bit but this is currently
not possible since the `QC_E_ADDI` instruction has `GPRNoX0` register
operands while the `ADDI` instruction has `GPR` register operands
leading to an operand type validation error.

I think we can remove the check that only source instruction operands
can mismatch with the corresponding DAG operands and rely on the fact
that we check if the DAG register operand type is a subclass of the
instruction register operand type.
2025-07-15 04:52:51 +05:30
Craig Topper
0f3bdc3e43
[TableGen] Remove the name from the union in OpData in PseudoLoweringEmitter and CompressInstEmitter (#147896)
We can use an anonymous union here, the name doesn't provide any
additional information.
2025-07-10 09:45:16 -07:00
Craig Topper
965b94428c [TableGen] Use emplace_back and const correct two reference arguments. NFC 2025-07-03 18:22:32 -07:00
Craig Topper
6ff3b43700
[TableGen] More generically handle tied source operands in CompressInstEmitter. (#146183)
Move the creation of OperandMap from createDagOperandMapping to the loop
in addDagOperandMapping. Expand it to store the DAG operand number and
the MI operand number which will be different when there are tied
operands.

Rename createDagOperandMapping to checkDagOperandMapping to better
describe the remaining code.

I didn't lift the restriction that a source instruction can only have
one tied operand, but we should be able to if we have a use case.

There's a slight difference in the generate output. We now check that
operand 0 and 2 of QC_MVEQI are equal instead of operand 1 and 2. This
should be equivalent since operand 0 and 1 have a tied constraint.
2025-07-02 13:09:35 -07:00
Craig Topper
9a93de58f7 [TableGen] Simplify copying OperandMap entries for tied operands in CompressInstEmitter. NFC
Copy the whole struct instead of copying both fields.
2025-06-27 14:58:53 -07:00
Craig Topper
457c9aec9e [TableGen] Simplify how we calculate NumMIOperands in addDagOperandMapping. NFC
We can use the MIOperandNo and NumMIOperands from the last operand
instead using a loop.
2025-06-27 14:58:39 -07:00
Craig Topper
a3fcfacc35 [TableGen] Simplify verifyDagOpCount in CompressInstEmitter.cpp. NFC
We were counting the number of tied operands in two different loops.
2025-06-27 10:08:03 -07:00
quic_hchandel
deb3464192
[RISCV] Add Tied operands in Xqcicm instructions and changes to handle a single tied operand in source DAG and instruction (#145538)
Tied Operands change is required for adding codegen patterns for
Qualcomm uC Xqcicm instructions
which will be done in a follow-up PR. This change leads to one of
instructions getting compressed even
when it shouldn't be. This case was not covered in #143660. Added
changes to correctly handle this case.
2025-06-25 10:25:00 +05:30
quic_hchandel
408e55098d
[RISCV] Add support for handling one tied operand in the source instruction for compress patterns (#143660)
This update enables compress patterns to handle one tied operand in
source instructions, which was previously unsupported. Qualcomm's uC
extension Xqci includes several instructions with tied operands that can
be compressed into smaller forms. This change adds the necessary support
to enable such compression. Additionally, a compress pattern for the
qc.muliadd instruction has been implemented.
2025-06-19 12:36:20 +05:30
Jay Foad
432c5f2c60
[TableGen] Use emplace instead of insert and similar. NFC. (#143164) 2025-06-07 09:32:36 +01:00
Rahul Joshi
2e8b539e71
[NFC][TableGen] Add {} for else when if body has {} (#139420) 2025-05-12 08:34:12 -07:00
Sudharsan Veeravalli
9b8f5340dd
Improve readability of <Target>GenCompressionInstEmitter. NFC (#134834)
Use indent() instead of manually indenting the code in the
CompressInstEmitter.cpp. Also modify the current indentation in a few
places.
2025-04-09 11:15:14 +05:30
Craig Topper
ebe1ece4bb
[TableGen][RISCV] Support sub-operands in CompressInstEmitter.cpp. (#133039)
I'm looking into using sub-operands for memory operands. This would use
MIOperandInfo to create a single operand that contains a register and
immediate as sub-operands. We can treat this as a single operand for
parsing and matching in the assembler. I believe this will provide some
simplifications like removing the InstAliases we need to support "(rs1)"
without an immediate.

Doing this requires making CompressInstEmitter aware of sub-operands.

I've chosen to use a flat list of operands in the CompressPats so each
sub-operand is represented individually.
2025-03-27 19:10:40 -07:00
Craig Topper
616737c386 [TableGen] Use std:vector instead of SmallVector for the list of CompressPatterns. NFC
There are very likely more than 4 compress patterns.
2025-03-22 15:33:55 -07:00
Craig Topper
726cfc67b6
[RISCV] Don't convert virtual register Register to MCRegister in isCompressibleInst. (#122843)
Calling MCRegisterClass::contains with a Register does an implicit
conversion from Register to MCRegister. I think MCRegister is only
intended to be used for physical registers. We should protect this
implicit conversion by checking for physical registers first.

While I was here I removed some unnecessary parentheses from the output.
2025-01-13 23:36:09 -08:00
abhishek-kaushik22
31ce47b5d6
[TableGen] Use std::move to avoid copy (#113061) 2024-11-21 11:48:46 -08:00
Rahul Joshi
62e2c7fb2d
[LLVM][TableGen] Change all Init pointers to const (#112705)
This is a part of effort to have better const correctness in TableGen
backends:


https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
2024-10-18 07:50:22 -07:00
Rahul Joshi
a140931be5
[TableGen] Change getValueAsListOfDefs to return const pointer vector (#110713)
Change `getValueAsListOfDefs` to return a vector of const Record
pointer, and remove `getValueAsListOfConstDefs` that was added as a
transition aid.

This is a part of effort to have better const correctness in TableGen
backends:


https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
2024-10-01 14:30:38 -07:00
Rahul Joshi
2c966709b7
[LLVM][TableGen] Change CompressInstEmitter to use const RecordKeeper (#109035)
Change CompressInstEmitter to use const RecordKeeper.

This is a part of effort to have better const correctness in TableGen
backends:


https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
2024-09-18 10:42:51 -07:00
Rahul Joshi
bdf02249e7
[TableGen] Change CGIOperandList::OperandInfo::Rec to const pointer (#107858)
Change CGIOperandList::OperandInfo::Rec and CGIOperandList::TheDef to
const pointer.

This is a part of effort to have better const correctness in TableGen
backends:


https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
2024-09-09 14:33:21 -07:00
Youngsuk Kim
34855405b0 [llvm] Avoid 'raw_string_ostream::str' (NFC)
Since `raw_string_ostream` doesn't own the string buffer, it is
desirable (in terms of memory safety) for users to directly reference
the string buffer rather than use `raw_string_ostream::str()`.

Work towards TODO item to remove `raw_string_ostream::str()`.
2024-07-05 17:22:03 -05: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
Wang Pengcheng
44193a0573
[TableGen][NFC] Format CompressInstEmitter (#68564)
Format code, fix linter warnings and format comments.
2023-10-26 10:39:52 +08:00
Craig Topper
0f4c9c016c [RISCV] Replace RISCV->RISC-V in strings.
To be consistent with RISC-V branding guidelines
https://riscv.org/about/risc-v-branding-guidelines/
Think we should be using RISC-V where possible.

D146449 already updated comments. Strings may have more user impact.

Reviewed By: asb

Differential Revision: https://reviews.llvm.org/D146451
2023-03-27 09:50:17 -07:00
NAKAMURA Takumi
9c93e728bf llvm-tblgen: Rewrite emitters to use TableGen::Emitter
Each emitter became self-contained since it has the registration of option.

Differential Revision: https://reviews.llvm.org/D144351
2023-03-21 16:21:27 +09:00
NAKAMURA Takumi
aeafcbcd75 llvm-tblgen: Add "TableGenBackends.h" to each emitter.
"TableGenBackends.h" has declarations of emitters.
2023-02-19 03:06:32 +09:00
NAKAMURA Takumi
afde3f549d llvm-tblgen: Apply IWYU partially 2023-02-17 00:32:46 +09:00
wangpc
fcc2e5aa39 [TableGen][NFC] Add postfix for validators of CompressPat
So that we won't get redefinition errors if we use compressInst
and uncompressInst in the same file.

Reviewed By: asb

Differential Revision: https://reviews.llvm.org/D141896
2023-01-18 14:34:06 +08:00
Craig Topper
a971ce70aa [RISCV] Eliminate the need to pass both RISCVSubtarget and MCSubtargetInfo to isCompressibleInst.
RISCVSubtarget should be a superclass of MCSubtargetInfo so should
have all the same information. Now we pass RISCVSubtarget by
reference and name it STI.

Confusingly, we seem to have been using an MCSubtargetInfo from
the TargetMachine rather than the one associated with the function
we are operating. I'm going to assume that was a mistake and not
intentional.

Reviewed By: kito-cheng

Differential Revision: https://reviews.llvm.org/D141966
2023-01-17 18:28:07 -08:00
Craig Topper
b8c06f15de [RISCV][TableGen] Correct formatting in RISCVGenCompressInstEmitter.inc. NFC 2023-01-17 13:35:13 -08:00
Craig Topper
0b38636f77 [RISCV] Remove MCRegisterInfo dependency from compressInst/uncompresInst/isCompressibleInst.
This was being used to lookup the register class for a register number,
but those live in a tablegened array. We can index that array directly
just like RISCVAsmParser does.

Differential Revision: https://reviews.llvm.org/D141951
2023-01-17 11:56:43 -08:00
Dmitry Bushev
95ef005230 [RISCV][NFC] Mark rs1 in most memory instructions as memory operand.
Marking rs1 (memory offset base) as memory operand provides additional
semantic value to this operand that can be used by different tools
(e.g. llvm-exegesis).

This change does not affect neigther Isel nor assembler. However it
required some tweaks in tablegen compressed inst emmiter.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D136847
2022-11-22 16:42:44 +03:00
ZHU Zijia
d51581ff2c [RISCV][TableGen] Mark MachineInstr with FrameIndex as not compressible
If a MachineInstr's operand should be Reg in compiler's output but is
currently FrameIndex, `isCompressibleInst()` will terminate at
`MachineOperandType::getReg()`.

This patch adds `.isReg()` checks to make `isCompressibleInst()` return
false for these MachineInstr, allowing `getInstSizeInBytes()` to return
a value and `EstimateFunctionSizeInBytes()` to work as intended.

See https://reviews.llvm.org/D129999#3694222 for details.

Reviewed By: luismarques

Differential Revision: https://reviews.llvm.org/D129999
2022-08-24 13:23:38 +08:00
serge-sans-paille
2dde5c9734 Cleanup llvm/utils/TableGen headers
Based on the output of include-what-you-use.
It's an utility directory, so no much impact on other code areas.

clang++ -E  -Iinclude -I../llvm/include ../llvm/utils/TableGen/*.cpp -std=c++14 -fno-rtti -fno-exceptions | wc -l
before: 4327274
after:  4316190

Related discourse thread: https://llvm.discourse.group/t/include-what-you-use-include-cleanup
Differential Revision: https://reviews.llvm.org/D118466
2022-01-31 11:16:28 +01:00
Zi Xuan Wu
24d1673c8b [llvm-tblgen][RISCV] Make llvm-tblgen RISCVCompressInstEmitter to be common infra across different targets
Not only RISCV but also other target such as CSKY, there are compressed instructions mixed with normal instructions.
To reuse the basic infra to compress/uncompress and predict instruction, we need reconstruct the RISCVCompressInstEmitter
and make it more general and suitable for other target.

Differential Revision: https://reviews.llvm.org/D113475
2021-11-18 11:14:27 +08:00