Skip to content

Commit d12dffa

Browse files
committed
[X86] Add X86::getConstantFromPool helper function to replace duplicate implementations.
We had the same helper function in shuffle decode / vector constant code - move this to X86InstrInfo to avoid duplication.
1 parent cf799b3 commit d12dffa

File tree

4 files changed

+32
-42
lines changed

4 files changed

+32
-42
lines changed

llvm/lib/Target/X86/X86FixupVectorConstants.cpp

+1-18
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,6 @@ FunctionPass *llvm::createX86FixupVectorConstants() {
6363
return new X86FixupVectorConstantsPass();
6464
}
6565

66-
static const Constant *getConstantFromPool(const MachineInstr &MI,
67-
const MachineOperand &Op) {
68-
if (!Op.isCPI() || Op.getOffset() != 0)
69-
return nullptr;
70-
71-
ArrayRef<MachineConstantPoolEntry> Constants =
72-
MI.getParent()->getParent()->getConstantPool()->getConstants();
73-
const MachineConstantPoolEntry &ConstantEntry = Constants[Op.getIndex()];
74-
75-
// Bail if this is a machine constant pool entry, we won't be able to dig out
76-
// anything useful.
77-
if (ConstantEntry.isMachineConstantPoolEntry())
78-
return nullptr;
79-
80-
return ConstantEntry.Val.ConstVal;
81-
}
82-
8366
// Attempt to extract the full width of bits data from the constant.
8467
static std::optional<APInt> extractConstantBits(const Constant *C) {
8568
unsigned NumBits = C->getType()->getPrimitiveSizeInBits();
@@ -244,7 +227,7 @@ bool X86FixupVectorConstantsPass::processInstruction(MachineFunction &MF,
244227
"Unexpected number of operands!");
245228

246229
MachineOperand &CstOp = MI.getOperand(OperandNo + X86::AddrDisp);
247-
if (auto *C = getConstantFromPool(MI, CstOp)) {
230+
if (auto *C = X86::getConstantFromPool(MI, CstOp)) {
248231
// Attempt to detect a suitable splat from increasing splat widths.
249232
std::pair<unsigned, unsigned> Broadcasts[] = {
250233
{8, OpBcst8}, {16, OpBcst16}, {32, OpBcst32},

llvm/lib/Target/X86/X86InstrInfo.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -3514,6 +3514,23 @@ int X86::getFirstAddrOperandIdx(const MachineInstr &MI) {
35143514
return -1;
35153515
}
35163516

3517+
const Constant *X86::getConstantFromPool(const MachineInstr &MI,
3518+
const MachineOperand &Op) {
3519+
if (!Op.isCPI() || Op.getOffset() != 0)
3520+
return nullptr;
3521+
3522+
ArrayRef<MachineConstantPoolEntry> Constants =
3523+
MI.getParent()->getParent()->getConstantPool()->getConstants();
3524+
const MachineConstantPoolEntry &ConstantEntry = Constants[Op.getIndex()];
3525+
3526+
// Bail if this is a machine constant pool entry, we won't be able to dig out
3527+
// anything useful.
3528+
if (ConstantEntry.isMachineConstantPoolEntry())
3529+
return nullptr;
3530+
3531+
return ConstantEntry.Val.ConstVal;
3532+
}
3533+
35173534
bool X86InstrInfo::isUnconditionalTailCall(const MachineInstr &MI) const {
35183535
switch (MI.getOpcode()) {
35193536
case X86::TCRETURNdi:

llvm/lib/Target/X86/X86InstrInfo.h

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ bool isX87Instruction(MachineInstr &MI);
8686
/// real instructions (e.g., JMP64m).
8787
int getFirstAddrOperandIdx(const MachineInstr &MI);
8888

89+
/// Find any constant pool entry associated with a specific instruction operand.
90+
const Constant *getConstantFromPool(const MachineInstr &MI,
91+
const MachineOperand &Op);
92+
8993
} // namespace X86
9094

9195
/// isGlobalStubReference - Return true if the specified TargetFlag operand is

llvm/lib/Target/X86/X86MCInstLower.cpp

+10-24
Original file line numberDiff line numberDiff line change
@@ -1399,23 +1399,6 @@ PrevCrossBBInst(MachineBasicBlock::const_iterator MBBI) {
13991399
return MBBI;
14001400
}
14011401

1402-
static const Constant *getConstantFromPool(const MachineInstr &MI,
1403-
const MachineOperand &Op) {
1404-
if (!Op.isCPI() || Op.getOffset() != 0)
1405-
return nullptr;
1406-
1407-
ArrayRef<MachineConstantPoolEntry> Constants =
1408-
MI.getParent()->getParent()->getConstantPool()->getConstants();
1409-
const MachineConstantPoolEntry &ConstantEntry = Constants[Op.getIndex()];
1410-
1411-
// Bail if this is a machine constant pool entry, we won't be able to dig out
1412-
// anything useful.
1413-
if (ConstantEntry.isMachineConstantPoolEntry())
1414-
return nullptr;
1415-
1416-
return ConstantEntry.Val.ConstVal;
1417-
}
1418-
14191402
static std::string getShuffleComment(const MachineInstr *MI, unsigned SrcOp1Idx,
14201403
unsigned SrcOp2Idx, ArrayRef<int> Mask) {
14211404
std::string Comment;
@@ -1674,7 +1657,7 @@ static void addConstantComments(const MachineInstr *MI,
16741657
"Unexpected number of operands!");
16751658

16761659
const MachineOperand &MaskOp = MI->getOperand(MaskIdx);
1677-
if (auto *C = getConstantFromPool(*MI, MaskOp)) {
1660+
if (auto *C = X86::getConstantFromPool(*MI, MaskOp)) {
16781661
unsigned Width = getRegisterWidth(MI->getDesc().operands()[0]);
16791662
SmallVector<int, 64> Mask;
16801663
DecodePSHUFBMask(C, Width, Mask);
@@ -1752,7 +1735,7 @@ static void addConstantComments(const MachineInstr *MI,
17521735
"Unexpected number of operands!");
17531736

17541737
const MachineOperand &MaskOp = MI->getOperand(MaskIdx);
1755-
if (auto *C = getConstantFromPool(*MI, MaskOp)) {
1738+
if (auto *C = X86::getConstantFromPool(*MI, MaskOp)) {
17561739
unsigned Width = getRegisterWidth(MI->getDesc().operands()[0]);
17571740
SmallVector<int, 16> Mask;
17581741
DecodeVPERMILPMask(C, ElSize, Width, Mask);
@@ -1781,7 +1764,7 @@ static void addConstantComments(const MachineInstr *MI,
17811764
}
17821765

17831766
const MachineOperand &MaskOp = MI->getOperand(3 + X86::AddrDisp);
1784-
if (auto *C = getConstantFromPool(*MI, MaskOp)) {
1767+
if (auto *C = X86::getConstantFromPool(*MI, MaskOp)) {
17851768
unsigned Width = getRegisterWidth(MI->getDesc().operands()[0]);
17861769
SmallVector<int, 16> Mask;
17871770
DecodeVPERMIL2PMask(C, (unsigned)CtrlOp.getImm(), ElSize, Width, Mask);
@@ -1796,7 +1779,7 @@ static void addConstantComments(const MachineInstr *MI,
17961779
"Unexpected number of operands!");
17971780

17981781
const MachineOperand &MaskOp = MI->getOperand(3 + X86::AddrDisp);
1799-
if (auto *C = getConstantFromPool(*MI, MaskOp)) {
1782+
if (auto *C = X86::getConstantFromPool(*MI, MaskOp)) {
18001783
unsigned Width = getRegisterWidth(MI->getDesc().operands()[0]);
18011784
SmallVector<int, 16> Mask;
18021785
DecodeVPPERMMask(C, Width, Mask);
@@ -1809,7 +1792,8 @@ static void addConstantComments(const MachineInstr *MI,
18091792
case X86::MMX_MOVQ64rm: {
18101793
assert(MI->getNumOperands() == (1 + X86::AddrNumOperands) &&
18111794
"Unexpected number of operands!");
1812-
if (auto *C = getConstantFromPool(*MI, MI->getOperand(1 + X86::AddrDisp))) {
1795+
if (auto *C =
1796+
X86::getConstantFromPool(*MI, MI->getOperand(1 + X86::AddrDisp))) {
18131797
std::string Comment;
18141798
raw_string_ostream CS(Comment);
18151799
const MachineOperand &DstOp = MI->getOperand(0);
@@ -1881,7 +1865,8 @@ static void addConstantComments(const MachineInstr *MI,
18811865
case X86::VBROADCASTI64X4rm:
18821866
assert(MI->getNumOperands() >= (1 + X86::AddrNumOperands) &&
18831867
"Unexpected number of operands!");
1884-
if (auto *C = getConstantFromPool(*MI, MI->getOperand(1 + X86::AddrDisp))) {
1868+
if (auto *C =
1869+
X86::getConstantFromPool(*MI, MI->getOperand(1 + X86::AddrDisp))) {
18851870
int NumLanes = 1;
18861871
int BitWidth = 128;
18871872
int CstEltSize = C->getType()->getScalarSizeInBits();
@@ -1984,7 +1969,8 @@ static void addConstantComments(const MachineInstr *MI,
19841969
case X86::VPBROADCASTWZrm:
19851970
assert(MI->getNumOperands() >= (1 + X86::AddrNumOperands) &&
19861971
"Unexpected number of operands!");
1987-
if (auto *C = getConstantFromPool(*MI, MI->getOperand(1 + X86::AddrDisp))) {
1972+
if (auto *C =
1973+
X86::getConstantFromPool(*MI, MI->getOperand(1 + X86::AddrDisp))) {
19881974
int NumElts, EltBits;
19891975
switch (MI->getOpcode()) {
19901976
default: llvm_unreachable("Invalid opcode");

0 commit comments

Comments
 (0)