[RISCV][MC] Refine MCInstrAnalysis based on registers used
authorJob Noorman <jnoorman@igalia.com>
Wed, 17 May 2023 09:43:38 +0000 (11:43 +0200)
committerJob Noorman <jnoorman@igalia.com>
Wed, 17 May 2023 09:43:39 +0000 (11:43 +0200)
commited90cf1873aa696f7c99ea2eafc540206c2391e1
treed56951a2a836f2a516fbb65f850ec3be4cde0cbd
parentd045f1d393317962251ba6dc58e3f8edc7e2fd99
[RISCV][MC] Refine MCInstrAnalysis based on registers used

MCInstrAnalysis provides a number of methods to query properties of
instructions (e.g., isTerminator(), isCall(),...). The default
implementation of these methods forwards the query to MCInstrDesc which
returns information based on various RISCVInstrInfo*.td files.

Since the info in MCInstrDesc is based on opcodes only, it is often
quite inaccurate. For example, JAL/JALR are never recognized as
terminators or branches while they certainly can be. However,
MCInstrAnalysis has access to the full MCInst so can improve accuracy by
inspecting registers used by the instruction.

This patch refines the following MCInstrAnalysis methods:
- isTerminator: JAL/JALR with RD=X0;
- isCall: JAL/JALR with RD!=X0
- isReturn: JALR/C_JR with RD=X0, RS1 in {X1, X5}
- isBranch: JAL/JALR/C_JR with RD=X0, RS1 not in {X1, X5};
- isUnconditionalBranch: JAL/JALR/C_JR with RD=X0, RS1 not in {X1, X5};
- isIndirectBranch: JALR/C_JR with RD=X0, RS1 not in {X1, X5};

Note that the reason for this patch is to simplify the RISCV target in
BOLT. While it's possible to implement everything there, it seems more
logical to implement it directly in the RISCV backend as other tools
might also be able to benefit from it.

Reviewed By: craig.topper, MaskRay

Differential Revision: https://reviews.llvm.org/D146438
llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
llvm/unittests/Target/RISCV/CMakeLists.txt [new file with mode: 0644]
llvm/unittests/Target/RISCV/MCInstrAnalysisTest.cpp [new file with mode: 0644]