Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
52ebb65
Remove changes affecting non-RISCV targets
arjunUpatel Jun 10, 2025
49f276e
Update test output to match previous functionality
arjunUpatel Jun 12, 2025
5e1cf12
Add support for zclsd and zilsd extensions + tests
arjunUpatel Jun 17, 2025
11c50dc
Pass subtargetinfo as function argument
arjunUpatel Jun 18, 2025
2241583
Run clang format
arjunUpatel Jun 18, 2025
7635b70
Remove precanned binaries from tests and invoke clang during tests
arjunUpatel Jun 19, 2025
9dff558
Fix typo
arjunUpatel Jun 20, 2025
b738bf1
Enable address resolution for load/store instructions relative to zer…
arjunUpatel Jun 20, 2025
9361828
Edit comments to follow LLVM coding style
arjunUpatel Jun 23, 2025
e942f63
Use llvm-mc over clang for compilation in tests
arjunUpatel Jun 24, 2025
86d6bd5
Update comments
arjunUpatel Jun 24, 2025
1e1a37c
Differentiate comments in tests from llvm-lit directives
arjunUpatel Jun 24, 2025
b2a8928
Merge evaluateInstruction into evaluateBranch
arjunUpatel Jun 26, 2025
6034372
Run clang format
arjunUpatel Jun 26, 2025
3723ffe
Rename evaluateBranch to findTargetAddress for MCInstrAnalysis
arjunUpatel Jul 3, 2025
4156098
Update documentation for findTargetAddress
arjunUpatel Jul 3, 2025
dc35c0b
Formatting nits
arjunUpatel Jul 3, 2025
4f92b91
Revert changes to cross-project-tests
arjunUpatel Jul 30, 2025
090519f
Delete 32 bit tests
arjunUpatel Jul 30, 2025
06841d3
Fix linux build error
arjunUpatel Jul 30, 2025
49d69e1
Update tests. Improve test comments
arjunUpatel Aug 22, 2025
b356402
Rename test
arjunUpatel Aug 22, 2025
d666747
Add unit tests
arjunUpatel Aug 22, 2025
279d53b
Update llvm/include/llvm/MC/MCInstrAnalysis.h
arjunUpatel Aug 22, 2025
f3b6d7b
Format unit tests + address nits
arjunUpatel Aug 25, 2025
8c4cf89
Merge branch 'main' into riscv-address-resolution
arjunUpatel Aug 29, 2025
d177ccf
Add CMakeList for new unit test
arjunUpatel Aug 30, 2025
15b7ece
Update RISCVMCInstAnalysis.cpp
arjunUpatel Aug 31, 2025
ec109f9
Update llvm/test/tools/llvm-objdump/RISCV/riscv-disassembly-address-r…
arjunUpatel Sep 4, 2025
dab085a
Update llvm/test/tools/llvm-objdump/RISCV/riscv-disassembly-address-r…
arjunUpatel Sep 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bolt/include/bolt/Core/MCPlusBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ class MCPlusBuilder {
/// targets. Return true on success, and the address in Target.
virtual bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const {
return Analysis->evaluateBranch(Inst, Addr, Size, Target);
return Analysis->findTargetAddress(Inst, Addr, Size, Target);
}

/// Return true if one of the operands of the \p Inst instruction uses
Expand Down
12 changes: 7 additions & 5 deletions llvm/include/llvm/MC/MCInstrAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/MC/MCInstrDesc.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/Compiler.h"
#include <cstdint>
#include <vector>
Expand Down Expand Up @@ -180,11 +181,12 @@ class LLVM_ABI MCInstrAnalysis {
return false;
}

/// Given a branch instruction try to get the address the branch
/// targets. Return true on success, and the address in Target.
virtual bool
evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const;
/// Given an instruction that accesses memory or
/// that branches to another address, try to get the address it targets.
/// Return true on success, and the address in \p Target.
virtual bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI = nullptr) const;

/// Given an instruction tries to get the address of a memory operand. Returns
/// the address on success.
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/MC/MCInstrAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ bool MCInstrAnalysis::clearsSuperRegisters(const MCRegisterInfo &MRI,
return false;
}

bool MCInstrAnalysis::evaluateBranch(const MCInst & /*Inst*/, uint64_t /*Addr*/,
uint64_t /*Size*/,
uint64_t & /*Target*/) const {
bool MCInstrAnalysis::findTargetAddress(const MCInst &Inst, uint64_t Addr,
uint64_t Size, uint64_t &Target,
const MCSubtargetInfo *STI) const {
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,9 @@ class AArch64MCInstrAnalysis : public MCInstrAnalysis {
public:
AArch64MCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
// Search for a PC-relative argument.
// This will handle instructions like bcc (where the first argument is the
// condition code) and cbz (where it is a register).
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ class AMDGPUMCInstrAnalysis : public MCInstrAnalysis {
explicit AMDGPUMCInstrAnalysis(const MCInstrInfo *Info)
: MCInstrAnalysis(Info) {}

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
if (Inst.getNumOperands() == 0 || !Inst.getOperand(0).isImm() ||
Info->get(Inst.getOpcode()).operands()[0].OperandType !=
MCOI::OPERAND_PCREL)
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/ARM/MCTargetDesc/ARMMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,9 @@ class ARMMCInstrAnalysis : public MCInstrAnalysis {
return MCInstrAnalysis::isConditionalBranch(Inst);
}

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
const MCInstrDesc &Desc = Info->get(Inst.getOpcode());

// Find the PC-relative immediate operand in the instruction.
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ class BPFMCInstrAnalysis : public MCInstrAnalysis {
explicit BPFMCInstrAnalysis(const MCInstrInfo *Info)
: MCInstrAnalysis(Info) {}

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
// The target is the 3rd operand of cond inst and the 1st of uncond inst.
int32_t Imm;
if (isConditionalBranch(Inst)) {
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ class CSKYMCInstrAnalysis : public MCInstrAnalysis {
explicit CSKYMCInstrAnalysis(const MCInstrInfo *Info)
: MCInstrAnalysis(Info) {}

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
if (isConditionalBranch(Inst) || isUnconditionalBranch(Inst)) {
int64_t Imm;
Imm = Inst.getOperand(Inst.getNumOperands() - 1).getImm();
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,8 +730,9 @@ class HexagonMCInstrAnalysis : public MCInstrAnalysis {
return MCInstrAnalysis::isConditionalBranch(Inst);
}

bool evaluateBranch(MCInst const &Inst, uint64_t Addr,
uint64_t Size, uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
if (!(isCall(Inst) || isUnconditionalBranch(Inst) ||
isConditionalBranch(Inst)))
return false;
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/Lanai/MCTargetDesc/LanaiMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ class LanaiMCInstrAnalysis : public MCInstrAnalysis {
explicit LanaiMCInstrAnalysis(const MCInstrInfo *Info)
: MCInstrAnalysis(Info) {}

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
if (Inst.getNumOperands() == 0)
return false;
if (!isConditionalBranch(Inst) && !isUnconditionalBranch(Inst) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ class LoongArchMCInstrAnalysis : public MCInstrAnalysis {
}
}

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
unsigned NumOps = Inst.getNumOperands();
if ((isBranch(Inst) && !isIndirectBranch(Inst)) ||
Inst.getOpcode() == LoongArch::BL) {
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ class MipsMCInstrAnalysis : public MCInstrAnalysis {
public:
MipsMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
unsigned NumOps = Inst.getNumOperands();
if (NumOps == 0)
return false;
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,9 @@ class PPCMCInstrAnalysis : public MCInstrAnalysis {
explicit PPCMCInstrAnalysis(const MCInstrInfo *Info)
: MCInstrAnalysis(Info) {}

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
unsigned NumOps = Inst.getNumOperands();
if (NumOps == 0 ||
Info->get(Inst.getOpcode()).operands()[NumOps - 1].OperandType !=
Expand Down
105 changes: 96 additions & 9 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include <bitset>
#include <cstdint>

#define GET_INSTRINFO_MC_DESC
#define ENABLE_INSTR_PREDICATE_VERIFIER
Expand Down Expand Up @@ -184,6 +185,17 @@ class RISCVMCInstrAnalysis : public MCInstrAnalysis {
}

switch (Inst.getOpcode()) {
case RISCV::C_LUI:
case RISCV::LUI: {
setGPRState(Inst.getOperand(0).getReg(),
SignExtend64<32>(Inst.getOperand(1).getImm() << 12));
break;
}
case RISCV::AUIPC: {
setGPRState(Inst.getOperand(0).getReg(),
Addr + SignExtend64<32>(Inst.getOperand(1).getImm() << 12));
break;
}
default: {
// Clear the state of all defined registers for instructions that we don't
// explicitly support.
Expand All @@ -195,15 +207,12 @@ class RISCVMCInstrAnalysis : public MCInstrAnalysis {
}
break;
}
case RISCV::AUIPC:
setGPRState(Inst.getOperand(0).getReg(),
Addr + SignExtend64<32>(Inst.getOperand(1).getImm() << 12));
break;
}
}

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override {
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override {
if (isConditionalBranch(Inst)) {
int64_t Imm;
if (Size == 2)
Expand All @@ -215,6 +224,8 @@ class RISCVMCInstrAnalysis : public MCInstrAnalysis {
}

switch (Inst.getOpcode()) {
default:
return false;
case RISCV::C_J:
case RISCV::C_JAL:
case RISCV::QC_E_J:
Expand All @@ -231,8 +242,85 @@ class RISCVMCInstrAnalysis : public MCInstrAnalysis {
}
return false;
}
case RISCV::C_ADDI:
case RISCV::ADDI: {
if (!STI)
return false;
unsigned int ArchRegWidth =
STI->getTargetTriple().getArchPointerBitWidth();
MCRegister Reg = Inst.getOperand(1).getReg();
auto TargetRegState = getGPRState(Reg);
if (TargetRegState && Reg != RISCV::X0) {
Target = *TargetRegState + Inst.getOperand(2).getImm();
Target &= maskTrailingOnes<uint64_t>(ArchRegWidth);
return true;
}
break;
}
case RISCV::C_ADDIW:
case RISCV::ADDIW: {
MCRegister Reg = Inst.getOperand(1).getReg();
auto TargetRegState = getGPRState(Reg);
if (TargetRegState && Reg != RISCV::X0) {
Target = *TargetRegState + Inst.getOperand(2).getImm();
Target = SignExtend64<32>(Target);
return true;
}
break;
}
case RISCV::LB:
case RISCV::LH:
case RISCV::LD:
case RISCV::LW:
case RISCV::LBU:
case RISCV::LHU:
case RISCV::LWU:
case RISCV::SB:
case RISCV::SH:
case RISCV::SW:
case RISCV::SD:
case RISCV::FLH:
case RISCV::FLW:
case RISCV::FLD:
case RISCV::FSH:
case RISCV::FSW:
case RISCV::FSD:
case RISCV::C_LD:
case RISCV::C_SD:
case RISCV::C_FLD:
case RISCV::C_FSD:
case RISCV::C_SW:
case RISCV::C_LW:
case RISCV::C_FSW:
case RISCV::C_FLW:
case RISCV::C_LBU:
case RISCV::C_LH:
case RISCV::C_LHU:
case RISCV::C_SB:
case RISCV::C_SH:
case RISCV::C_LWSP:
case RISCV::C_SWSP:
case RISCV::C_LDSP:
case RISCV::C_SDSP:
case RISCV::C_FLWSP:
case RISCV::C_FSWSP:
case RISCV::C_FLDSP:
case RISCV::C_FSDSP:
case RISCV::C_LD_RV32:
case RISCV::C_SD_RV32:
case RISCV::C_SDSP_RV32:
case RISCV::LD_RV32:
case RISCV::C_LDSP_RV32:
case RISCV::SD_RV32: {
MCRegister Reg = Inst.getOperand(1).getReg();
auto TargetRegState = getGPRState(Reg);
if (TargetRegState) {
Target = *TargetRegState + Inst.getOperand(2).getImm();
return true;
}
break;
}
}

return false;
}

Expand Down Expand Up @@ -389,12 +477,11 @@ LLVMInitializeRISCVTargetMC() {
TargetRegistry::RegisterELFStreamer(*T, createRISCVELFStreamer);
TargetRegistry::RegisterObjectTargetStreamer(
*T, createRISCVObjectTargetStreamer);
TargetRegistry::RegisterMCInstrAnalysis(*T, createRISCVInstrAnalysis);

// Register the asm target streamer.
TargetRegistry::RegisterAsmTargetStreamer(*T, createRISCVAsmTargetStreamer);
// Register the null target streamer.
TargetRegistry::RegisterNullTargetStreamer(*T,
createRISCVNullTargetStreamer);
TargetRegistry::RegisterMCInstrAnalysis(*T, createRISCVInstrAnalysis);
}
}
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/MCTargetDesc/X86ATTInstPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
// binary object.
if (SymbolizeOperands && MIA) {
uint64_t Target;
if (MIA->evaluateBranch(*MI, 0, 0, Target))
if (MIA->findTargetAddress(*MI, 0, 0, Target))
return;
if (MIA->evaluateMemoryOperandAddress(*MI, /*STI=*/nullptr, 0, 0))
return;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/MCTargetDesc/X86IntelInstPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
// binary object.
if (SymbolizeOperands && MIA) {
uint64_t Target;
if (MIA->evaluateBranch(*MI, 0, 0, Target))
if (MIA->findTargetAddress(*MI, 0, 0, Target))
return;
if (MIA->evaluateMemoryOperandAddress(*MI, /*STI=*/nullptr, 0, 0))
return;
Expand Down
10 changes: 6 additions & 4 deletions llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,9 @@ class X86MCInstrAnalysis : public MCInstrAnalysis {
findPltEntries(uint64_t PltSectionVA, ArrayRef<uint8_t> PltContents,
const MCSubtargetInfo &STI) const override;

bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target) const override;
bool findTargetAddress(const MCInst &Inst, uint64_t Addr, uint64_t Size,
uint64_t &Target,
const MCSubtargetInfo *STI) const override;
std::optional<uint64_t>
evaluateMemoryOperandAddress(const MCInst &Inst, const MCSubtargetInfo *STI,
uint64_t Addr, uint64_t Size) const override;
Expand Down Expand Up @@ -641,8 +642,9 @@ X86MCInstrAnalysis::findPltEntries(uint64_t PltSectionVA,
}
}

bool X86MCInstrAnalysis::evaluateBranch(const MCInst &Inst, uint64_t Addr,
uint64_t Size, uint64_t &Target) const {
bool X86MCInstrAnalysis::findTargetAddress(
const MCInst &Inst, uint64_t Addr, uint64_t Size, uint64_t &Target,
const MCSubtargetInfo *STI = nullptr) const {
if (Inst.getNumOperands() == 0 ||
Info->get(Inst.getOpcode()).operands()[0].OperandType !=
MCOI::OPERAND_PCREL)
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/tools/llvm-objdump/RISCV/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if "RISCV" not in config.targets_to_build:
config.unsupported = True
Loading
Loading