LLVM 24.0.0git
HexagonFindMask.cpp
Go to the documentation of this file.
1//===- HexagonFindMask.cpp - Hexagon relocation-application helper ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Hexagon instruction encodings scatter an operand's bits across
10// non-contiguous positions in the 32-bit instruction word. This file
11// provides the helpers needed to apply an ELF relocation to such an
12// instruction: locate which bits belong to the operand (using the
13// generated per-instruction encoding table), scatter the relocated value
14// into exactly those bit positions, and verify the result against the
15// range/alignment rules from the Hexagon ABI.
16//
17// The functions are exposed with C linkage so that external tools that do
18// not link against LLVM's C++ ABI (e.g. other Hexagon toolchain components)
19// can compute relocations identically to LLVM itself. Inside LLVM, they are
20// used by RuntimeDyldELF to resolve Hexagon relocations for MCJIT.
21//
22//===----------------------------------------------------------------------===//
23
25#include "llvm/ADT/STLExtras.h"
29
30namespace {
31struct HexagonInstruction {
32 uint32_t insnMask;
33 uint32_t insnCmpMask;
34 uint32_t insnBitMask;
35 bool isDuplex;
36};
37} // namespace
38
39#include "HexagonDepMask.h"
40
41// Finds the bit mask for Insn by scanning the generated instruction
42// encoding table.
44 const HexagonInstruction *Encodings,
45 size_t NumInsns) {
46 for (size_t I = 0; I < NumInsns; ++I) {
47 if (((Insn & 0xc000) == 0) && !Encodings[I].isDuplex)
48 continue;
49 if (((Insn & 0xc000) != 0) && Encodings[I].isDuplex)
50 continue;
51 if (((Encodings[I].insnMask) & Insn) == Encodings[I].insnCmpMask) {
52 return Encodings[I].insnBitMask;
53 }
54 }
55 llvm_unreachable("Cannot find encoding for Instruction!");
56}
57
60 sizeof(InstructionEncodings) /
61 sizeof(HexagonInstruction));
62}
63
64// Scatters the bits of Data over the positions set in Mask.
65static uint32_t applyMask(uint32_t Mask, uint32_t Data) {
66 uint32_t Result = 0;
67 size_t Off = 0;
68
69 for (size_t Bit = 0; Bit != sizeof(uint32_t) * 8; ++Bit) {
70 const uint32_t ValBit = (Data >> Off) & 1;
71 const bool MaskBit = (Mask >> Bit) & 1;
72 if (MaskBit) {
73 Result |= (ValBit << Bit);
74 ++Off;
75 }
76 }
77 return Result;
78}
79
80// The low relocation in an extended branch encodes only bits 5:0. The
81// EffectiveBits table entry retains the ABI-specified branch width.
82static uint32_t getAppliedBits(uint32_t RelocType) {
83 switch (RelocType) {
84 case llvm::ELF::R_HEX_B22_PCREL_X:
85 case llvm::ELF::R_HEX_B15_PCREL_X:
86 case llvm::ELF::R_HEX_B13_PCREL_X:
87 case llvm::ELF::R_HEX_B9_PCREL_X:
88 case llvm::ELF::R_HEX_B7_PCREL_X:
89 case llvm::ELF::R_HEX_GD_PLT_B22_PCREL_X:
90 case llvm::ELF::R_HEX_LD_PLT_B22_PCREL_X:
91 return 6;
92 default:
93 return llvm::Hexagon::Relocs[RelocType].EffectiveBits;
94 }
95}
96
97// Computes the instruction bits with the relocation applied: shift, then
98// truncate to the relocation's effective bit width, then scatter into the
99// instruction's bit mask.
100template <typename T>
101static uint32_t doRelocHelper(uint32_t RelocType, uint32_t Instruction,
102 T Value) {
103 const auto &Reloc = llvm::Hexagon::Relocs[RelocType];
104 const uint32_t AppliedBits = getAppliedBits(RelocType);
105 if (AppliedBits == 0)
106 return Instruction;
107
108 T ValueAfterShift = Value >> Reloc.Shift;
109 uint32_t EffectiveValue =
110 ValueAfterShift & llvm::maskTrailingOnes<uint32_t>(AppliedBits);
111 uint32_t BitMask = Reloc.BitMask;
112 if (BitMask == 0)
113 BitMask = findMask(Instruction);
114 Instruction &= ~BitMask;
115 EffectiveValue = applyMask(BitMask, EffectiveValue);
116 return EffectiveValue | Instruction;
117}
118
121 if (RelocType >= std::size(llvm::Hexagon::Relocs))
122 return nullptr;
123 return &llvm::Hexagon::Relocs[RelocType];
124}
125
126extern "C" {
127
129 uint32_t Instruction,
130 uint32_t Value) {
131 const auto *R = getRelocInfo(RelocType);
132 if (!R)
133 return Instruction;
134 if (R->IsSigned)
135 return doRelocHelper<int32_t>(RelocType, Instruction, Value);
136 return doRelocHelper<uint32_t>(RelocType, Instruction, Value);
137}
138
140 LLVMHexagonRelocInfo *Info) {
141 const auto *R = getRelocInfo(RelocType);
142 if (!R)
143 return false;
144 Info->Name = R->Name;
145 Info->Type = R->Type;
146 Info->EffectiveBits = R->EffectiveBits;
147 Info->BitMask = R->BitMask;
148 Info->Alignment = R->Alignment;
149 Info->Shift = R->Shift;
150 Info->VerifyRange = R->VerifyRange;
151 Info->VerifyAlignment = R->VerifyAlignment;
152 Info->IsSigned = R->IsSigned;
153 return true;
154}
155
157 const auto *R = getRelocInfo(RelocType);
158 if (!R)
159 return false;
160 if (R->IsSigned)
161 return llvm::isIntN(R->EffectiveBits, (int32_t)Value);
162 return llvm::isUIntN(R->EffectiveBits, Value);
163}
164
166 const auto *R = getRelocInfo(RelocType);
167 return R && (R->Alignment == 0 || Value % R->Alignment == 0);
168}
169
171 const auto *R = getRelocInfo(RelocType);
172 if (!R || R->IsSigned || R->EffectiveBits == 0)
173 return false;
174 return Value & ~llvm::maskTrailingOnes<uint32_t>(R->EffectiveBits);
175}
176
177} // extern "C"
#define LLVM_ABI
Definition Compiler.h:211
static const HexagonInstruction InstructionEncodings[]
static uint32_t doRelocHelper(uint32_t RelocType, uint32_t Instruction, T Value)
static uint32_t applyMask(uint32_t Mask, uint32_t Data)
LLVM_ABI bool LLVMHexagonVerifyAlignment(uint32_t RelocType, uint32_t Value)
Return true if Value satisfies the alignment required by RelocType.
LLVM_ABI bool LLVMHexagonIsTruncated(uint32_t RelocType, uint32_t Value)
Return true if applying the unsigned relocation RelocType to Value would lose set bits.
LLVM_ABI uint32_t LLVMHexagonApplyRelocation(uint32_t RelocType, uint32_t Instruction, uint32_t Value)
Apply the ELF relocation RelocType with value Value to Instruction.
static const llvm::Hexagon::HexagonRelocationInfo * getRelocInfo(uint32_t RelocType)
static uint32_t getAppliedBits(uint32_t RelocType)
LLVM_ABI bool LLVMHexagonVerifyRange(uint32_t RelocType, uint32_t Value)
Return true if Value fits the range the ABI specifies for RelocType.
static uint32_t findBitMaskHelper(uint32_t Insn, const HexagonInstruction *Encodings, size_t NumInsns)
LLVM_ABI bool LLVMHexagonGetRelocationInfo(uint32_t RelocType, LLVMHexagonRelocInfo *Info)
Fill Info with the properties of RelocType.
static uint32_t findMask(uint32_t I)
#define I(x, y, z)
Definition MD5.cpp:57
#define T
This file contains some templates that are useful if you are working with the STL at all.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static constexpr HexagonRelocationInfo Relocs[]
This is an optimization pass for GlobalISel generic memory operations.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition MathExtras.h:249
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:78
ABI properties of a Hexagon ELF relocation type, as listed in the Hexagon ABI document.