Skip to content

Commit a58e351

Browse files
committed
[AIEX] refactor Load/Store Imm check
1 parent abc5964 commit a58e351

File tree

8 files changed

+292
-207
lines changed

8 files changed

+292
-207
lines changed

llvm/lib/Target/AIE/AIE2InstrInfo.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,75 @@ AIE2InstrInfo::getZOLSupport() const {
14341434
return Result;
14351435
}
14361436

1437+
bool AIE2InstrInfo::isOffsetInImmediateRange(
1438+
unsigned Opcode, unsigned LoadStoreSize,
1439+
std::optional<APInt> Offset) const {
1440+
if (!Offset)
1441+
return false;
1442+
1443+
switch (Opcode) {
1444+
case AIE2::G_AIE_OFFSET_LOAD:
1445+
case AIE2::G_AIE_OFFSET_STORE: {
1446+
switch (LoadStoreSize) {
1447+
case 8:
1448+
case 16:
1449+
return checkSignedImmediateRange<3, 1>(Offset);
1450+
case 20:
1451+
case 32:
1452+
return checkSignedImmediateRange<6, 4>(Offset);
1453+
case 128:
1454+
return checkSignedImmediateRange<6, 16>(Offset);
1455+
case 256:
1456+
return checkSignedImmediateRange<6, 32>(Offset);
1457+
case 512:
1458+
return checkSignedImmediateRangeSplitting<6, 32, 32>(Offset);
1459+
default:
1460+
return false;
1461+
}
1462+
}
1463+
case AIE2::G_AIE_POSTINC_LOAD:
1464+
case AIE2::G_AIE_POSTINC_STORE: {
1465+
switch (LoadStoreSize) {
1466+
case 8:
1467+
case 16:
1468+
return checkSignedImmediateRange<4, 1>(Offset);
1469+
case 20:
1470+
case 32:
1471+
return checkSignedImmediateRange<7, 4>(Offset);
1472+
case 128:
1473+
return checkSignedImmediateRange<7, 16>(Offset);
1474+
case 256:
1475+
case 512:
1476+
return checkSignedImmediateRange<7, 32>(Offset);
1477+
default:
1478+
return false;
1479+
}
1480+
}
1481+
case AIE2::G_AIE_OFFSET_ZEXTLOAD:
1482+
case AIE2::G_AIE_OFFSET_SEXTLOAD: {
1483+
switch (LoadStoreSize) {
1484+
case 8:
1485+
case 16:
1486+
return checkSignedImmediateRange<3, 1>(Offset);
1487+
default:
1488+
return false;
1489+
}
1490+
}
1491+
case AIE2::G_AIE_POSTINC_SEXTLOAD:
1492+
case AIE2::G_AIE_POSTINC_ZEXTLOAD: {
1493+
switch (LoadStoreSize) {
1494+
case 8:
1495+
case 16:
1496+
return checkSignedImmediateRange<4, 1>(Offset);
1497+
default:
1498+
return false;
1499+
}
1500+
}
1501+
default:
1502+
return false;
1503+
}
1504+
}
1505+
14371506
unsigned AIE2InstrInfo::getPseudoJNZDOpcode() const { return AIE2::PseudoJNZD; }
14381507

14391508
unsigned AIE2InstrInfo::getNumBypassedCycles(const InstrItineraryData *ItinData,

llvm/lib/Target/AIE/AIE2InstrInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class AIE2InstrInfo : public AIE2GenInstrInfo {
105105

106106
virtual std::optional<ZOLSupport> getZOLSupport() const override;
107107

108+
virtual bool
109+
isOffsetInImmediateRange(unsigned Opcode, unsigned LoadStoreSize,
110+
std::optional<APInt> Immediate) const override;
111+
108112
virtual unsigned getPseudoJNZDOpcode() const override;
109113

110114
unsigned getNumBypassedCycles(const InstrItineraryData *ItinData,

llvm/lib/Target/AIE/AIE2InstructionSelector.cpp

Lines changed: 65 additions & 96 deletions
Large diffs are not rendered by default.

llvm/lib/Target/AIE/AIEBaseInstrInfo.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ struct AIEBaseInstrInfo : public TargetInstrInfo {
5959
// of bundles.
6060
unsigned LoopSetupDistance;
6161
};
62+
virtual bool isOffsetInImmediateRange(unsigned Opcode, unsigned LoadStoreSize,
63+
std::optional<APInt> Offset) const {
64+
llvm_unreachable("Target didn't implement OffsetFitImmRange");
65+
}
6266

6367
/// Return the opcode for a return instruction
6468
virtual unsigned getReturnOpcode() const {
@@ -755,6 +759,24 @@ struct AIEBaseInstrInfo : public TargetInstrInfo {
755759
const MIRFormatter *getMIRFormatter() const override;
756760
mutable std::unique_ptr<AIEMIRFormatter> Formatter;
757761
};
762+
763+
template <unsigned NumEncodingBits, unsigned Step>
764+
bool checkSignedImmediateRange(std::optional<APInt> Immediate) {
765+
const unsigned MaxPow2 = NumEncodingBits + llvm::Log2_64(Step);
766+
if (Immediate && isIntN(MaxPow2, Immediate->getSExtValue()) &&
767+
Immediate->getSExtValue() % Step == 0) {
768+
return true;
769+
}
770+
return false;
771+
}
772+
773+
template <unsigned NumEncodingBits, unsigned Step, unsigned SplitOffset>
774+
bool checkSignedImmediateRangeSplitting(std::optional<APInt> Immediate) {
775+
return Immediate &&
776+
checkSignedImmediateRange<NumEncodingBits, Step>(Immediate) &&
777+
checkSignedImmediateRange<NumEncodingBits, Step>(*Immediate +
778+
SplitOffset);
779+
}
758780
} // namespace llvm
759781

760782
#endif // LLVM_LIB_TARGET_AIE_AIEBASEINSTRRINFO_H

llvm/lib/Target/AIE/AIEBaseInstructionSelector.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,6 @@ inline unsigned getLoadStoreSize(const MachineInstr &MI) {
198198
return (*MI.memoperands_begin())->getSizeInBits().getValue();
199199
}
200200

201-
template <unsigned NumEncodingBits, unsigned Step>
202-
bool checkImmediateRange(std::optional<APInt> Immediate) {
203-
unsigned MaxPow2 = NumEncodingBits + llvm::Log2_64(Step);
204-
if (Immediate && isIntN(MaxPow2, Immediate->getSExtValue()) &&
205-
Immediate->getSExtValue() % Step == 0) {
206-
LLVM_DEBUG(dbgs() << "Immediate " << Immediate << " is valid for MaxPow2 "
207-
<< MaxPow2 << " and Step " << Step << ".\n");
208-
return true;
209-
}
210-
return false;
211-
}
212-
213-
template <unsigned NumEncodingBits, unsigned Step, unsigned SplitOffset>
214-
bool checkImmediateRangeSplitting(std::optional<APInt> Immediate) {
215-
return Immediate && checkImmediateRange<NumEncodingBits, Step>(Immediate) &&
216-
checkImmediateRange<NumEncodingBits, Step>(*Immediate + SplitOffset);
217-
}
218-
219201
inline unsigned deriveRegBankID(Register Reg, const MachineRegisterInfo &MRI,
220202
const RegisterBankInfo &RBI) {
221203
const RegisterBank *RB = MRI.getRegBankOrNull(Reg);

llvm/lib/Target/AIE/aie2p/AIE2PInstrInfo.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,77 @@ AIE2PInstrInfo::getZOLSupport() const {
17431743
return Result;
17441744
}
17451745

1746+
bool AIE2PInstrInfo::isOffsetInImmediateRange(
1747+
unsigned Opcode, unsigned LoadStoreSize,
1748+
std::optional<APInt> Offset) const {
1749+
if (!Offset)
1750+
return false;
1751+
1752+
switch (Opcode) {
1753+
case AIE2P::G_AIE_OFFSET_STORE:
1754+
case AIE2P::G_AIE_OFFSET_LOAD: {
1755+
switch (LoadStoreSize) {
1756+
case 8:
1757+
return checkSignedImmediateRange<4, 1>(Offset);
1758+
case 16:
1759+
return checkSignedImmediateRange<4, 2>(Offset);
1760+
case 20:
1761+
case 32:
1762+
return checkSignedImmediateRange<4, 4>(Offset);
1763+
case 128:
1764+
return checkSignedImmediateRange<4, 16>(Offset);
1765+
case 256:
1766+
return checkSignedImmediateRange<4, 32>(Offset);
1767+
case 512:
1768+
return checkSignedImmediateRange<4, 64>(Offset);
1769+
case 1024:
1770+
return checkSignedImmediateRangeSplitting<4, 64, 64>(Offset);
1771+
case 2048:
1772+
return checkSignedImmediateRangeSplitting<4, 64, 192>(Offset);
1773+
default:
1774+
return false;
1775+
}
1776+
}
1777+
case AIE2P::G_AIE_OFFSET_SEXTLOAD:
1778+
case AIE2P::G_AIE_OFFSET_ZEXTLOAD:
1779+
case AIE2P::G_AIE_POSTINC_ZEXTLOAD:
1780+
case AIE2P::G_AIE_POSTINC_SEXTLOAD: {
1781+
switch (LoadStoreSize) {
1782+
case 8:
1783+
return checkSignedImmediateRange<4, 1>(Offset);
1784+
case 16:
1785+
return checkSignedImmediateRange<4, 2>(Offset);
1786+
default:
1787+
return false;
1788+
}
1789+
}
1790+
case AIE2P::G_AIE_POSTINC_STORE:
1791+
case AIE2P::G_AIE_POSTINC_LOAD: {
1792+
switch (LoadStoreSize) {
1793+
case 8:
1794+
return checkSignedImmediateRange<4, 1>(Offset);
1795+
case 16:
1796+
return checkSignedImmediateRange<4, 2>(Offset);
1797+
case 20:
1798+
case 32:
1799+
return checkSignedImmediateRange<4, 4>(Offset);
1800+
case 128:
1801+
return checkSignedImmediateRange<4, 16>(Offset);
1802+
case 256:
1803+
return checkSignedImmediateRange<4, 32>(Offset);
1804+
case 512:
1805+
case 1024:
1806+
case 2048:
1807+
return checkSignedImmediateRange<4, 64>(Offset);
1808+
default:
1809+
return false;
1810+
}
1811+
}
1812+
default:
1813+
return false;
1814+
}
1815+
}
1816+
17461817
unsigned AIE2PInstrInfo::getGenericAddVectorEltOpcode() const {
17471818
return AIE2P::G_AIE_ADD_VECTOR_ELT_HI;
17481819
}

llvm/lib/Target/AIE/aie2p/AIE2PInstrInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ class AIE2PInstrInfo : public AIE2PGenInstrInfo {
110110

111111
virtual std::optional<ZOLSupport> getZOLSupport() const override;
112112

113+
bool isOffsetInImmediateRange(unsigned Opcode, unsigned LoadStoreSize,
114+
std::optional<APInt> Immediate) const override;
115+
113116
unsigned getNumBypassedCycles(const InstrItineraryData *ItinData,
114117
const MachineInstr &DefMI, unsigned DefIdx,
115118
const MachineInstr &UseMI,

0 commit comments

Comments
 (0)