Skip to content

Commit ef6eaa0

Browse files
authored
[GISel] Introduce MIFlags::InBounds (#150900)
This flag applies to G_PTR_ADD instructions and indicates that the operation implements an inbounds getelementptr operation, i.e., the pointer operand is in bounds wrt. the allocated object it is based on, and the arithmetic does not change that. It is set when the IRTranslator lowers inbounds GEPs (currently only in some cases, to be extended with a future PR), and in the (build|materialize)ObjectPtrOffset functions. Inbounds information is useful in ISel when we have instructions that perform address computations whose intermediate steps must be in the same memory region as the final result. A follow-up patch will start using it for AMDGPU's flat memory instructions, where the immediate offset must not affect the memory aperture of the address. This is analogous to a concurrent effort in SDAG: #131862 (related: #140017, #141725). For SWDEV-516125.
1 parent 8a09adc commit ef6eaa0

File tree

95 files changed

+10038
-10019
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+10038
-10019
lines changed

llvm/include/llvm/CodeGen/GlobalISel/GenericMachineInstrs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ namespace llvm {
2626

2727
/// A base class for all GenericMachineInstrs.
2828
class GenericMachineInstr : public MachineInstr {
29-
constexpr static unsigned PoisonFlags = NoUWrap | NoSWrap | NoUSWrap |
30-
IsExact | Disjoint | NonNeg |
31-
FmNoNans | FmNoInfs | SameSign;
29+
constexpr static unsigned PoisonFlags =
30+
NoUWrap | NoSWrap | NoUSWrap | IsExact | Disjoint | NonNeg | FmNoNans |
31+
FmNoInfs | SameSign | InBounds;
3232

3333
public:
3434
GenericMachineInstr() = delete;

llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ class LLVM_ABI MachineIRBuilder {
519519
std::optional<unsigned> Flags = std::nullopt);
520520

521521
/// Build and insert an instruction with appropriate flags for addressing some
522-
/// offset of an object, i.e.: \p Res = nuw G_PTR_ADD \p Op0, \p Op1
522+
/// offset of an object, i.e.: \p Res = nuw inbounds G_PTR_ADD \p Op0, \p Op1
523523
/// The value of \p Op0 must be a pointer into or just after an object, adding
524524
/// the value of \p Op1 to it must yield to a pointer into or just after the
525525
/// same object.
@@ -556,7 +556,7 @@ class LLVM_ABI MachineIRBuilder {
556556

557557
/// Materialize and insert an instruction with appropriate flags for
558558
/// addressing some offset of an object, i.e.:
559-
/// \p Res = nuw G_PTR_ADD \p Op0, (G_CONSTANT \p Value)
559+
/// \p Res = nuw inbounds G_PTR_ADD \p Op0, (G_CONSTANT \p Value)
560560
/// The value of \p Op0 must be a pointer into or just after an object, adding
561561
/// \p Value to it must yield to a pointer into or just after the same object.
562562
///

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ class MachineInstr
122122
Disjoint = 1 << 19, // Each bit is zero in at least one of the inputs.
123123
NoUSWrap = 1 << 20, // Instruction supports geps
124124
// no unsigned signed wrap.
125-
SameSign = 1 << 21 // Both operands have the same sign.
125+
SameSign = 1 << 21, // Both operands have the same sign.
126+
InBounds = 1 << 22 // Pointer arithmetic remains inbounds.
127+
// Implies NoUSWrap.
126128
};
127129

128130
private:

llvm/include/llvm/Target/GlobalISel/Combine.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def IsExact : MIFlagEnum<"IsExact">;
194194
def NoSWrap : MIFlagEnum<"NoSWrap">;
195195
def NoUWrap : MIFlagEnum<"NoUWrap">;
196196
def NonNeg : MIFlagEnum<"NonNeg">;
197+
def InBounds : MIFlagEnum<"InBounds">;
197198

198199
def MIFlags;
199200
// def not; -> Already defined as a SDNode

llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ MachineIRBuilder::buildPtrAdd(const DstOp &Res, const SrcOp &Op0,
211211
MachineInstrBuilder MachineIRBuilder::buildObjectPtrOffset(const DstOp &Res,
212212
const SrcOp &Op0,
213213
const SrcOp &Op1) {
214-
return buildPtrAdd(Res, Op0, Op1, MachineInstr::MIFlag::NoUWrap);
214+
return buildPtrAdd(Res, Op0, Op1,
215+
MachineInstr::MIFlag::NoUWrap |
216+
MachineInstr::MIFlag::InBounds);
215217
}
216218

217219
std::optional<MachineInstrBuilder>
@@ -234,7 +236,8 @@ MachineIRBuilder::materializePtrAdd(Register &Res, Register Op0,
234236
std::optional<MachineInstrBuilder> MachineIRBuilder::materializeObjectPtrOffset(
235237
Register &Res, Register Op0, const LLT ValueTy, uint64_t Value) {
236238
return materializePtrAdd(Res, Op0, ValueTy, Value,
237-
MachineInstr::MIFlag::NoUWrap);
239+
MachineInstr::MIFlag::NoUWrap |
240+
MachineInstr::MIFlag::InBounds);
238241
}
239242

240243
MachineInstrBuilder MachineIRBuilder::buildMaskLowPtrBits(const DstOp &Res,

llvm/lib/CodeGen/MIRParser/MILexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
217217
.Case("nneg", MIToken::kw_nneg)
218218
.Case("disjoint", MIToken::kw_disjoint)
219219
.Case("samesign", MIToken::kw_samesign)
220+
.Case("inbounds", MIToken::kw_inbounds)
220221
.Case("nofpexcept", MIToken::kw_nofpexcept)
221222
.Case("unpredictable", MIToken::kw_unpredictable)
222223
.Case("debug-location", MIToken::kw_debug_location)

llvm/lib/CodeGen/MIRParser/MILexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct MIToken {
7878
kw_nneg,
7979
kw_disjoint,
8080
kw_samesign,
81+
kw_inbounds,
8182
kw_debug_location,
8283
kw_debug_instr_number,
8384
kw_dbg_instr_ref,

llvm/lib/CodeGen/MIRParser/MIParser.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,8 @@ bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
14771477
Token.is(MIToken::kw_nneg) ||
14781478
Token.is(MIToken::kw_disjoint) ||
14791479
Token.is(MIToken::kw_nusw) ||
1480-
Token.is(MIToken::kw_samesign)) {
1480+
Token.is(MIToken::kw_samesign) ||
1481+
Token.is(MIToken::kw_inbounds)) {
14811482
// clang-format on
14821483
// Mine frame and fast math flags
14831484
if (Token.is(MIToken::kw_frame_setup))
@@ -1518,6 +1519,8 @@ bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
15181519
Flags |= MachineInstr::NoUSWrap;
15191520
if (Token.is(MIToken::kw_samesign))
15201521
Flags |= MachineInstr::SameSign;
1522+
if (Token.is(MIToken::kw_inbounds))
1523+
Flags |= MachineInstr::InBounds;
15211524

15221525
lex();
15231526
}

llvm/lib/CodeGen/MIRPrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,8 @@ static void printMI(raw_ostream &OS, MFPrintState &State,
820820
OS << "nusw ";
821821
if (MI.getFlag(MachineInstr::SameSign))
822822
OS << "samesign ";
823+
if (MI.getFlag(MachineInstr::InBounds))
824+
OS << "inbounds ";
823825

824826
// NOTE: Please add new MIFlags also to the MI_FLAGS_STR in
825827
// llvm/utils/update_mir_test_checks.py.

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ uint32_t MachineInstr::copyFlagsFromInstruction(const Instruction &I) {
585585
MIFlags |= MachineInstr::MIFlag::NoUSWrap;
586586
if (GEP->hasNoUnsignedWrap())
587587
MIFlags |= MachineInstr::MIFlag::NoUWrap;
588+
if (GEP->isInBounds())
589+
MIFlags |= MachineInstr::MIFlag::InBounds;
588590
}
589591

590592
// Copy the nonneg flag.
@@ -1860,8 +1862,12 @@ void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
18601862
OS << "nneg ";
18611863
if (getFlag(MachineInstr::Disjoint))
18621864
OS << "disjoint ";
1865+
if (getFlag(MachineInstr::NoUSWrap))
1866+
OS << "nusw ";
18631867
if (getFlag(MachineInstr::SameSign))
18641868
OS << "samesign ";
1869+
if (getFlag(MachineInstr::InBounds))
1870+
OS << "inbounds ";
18651871

18661872
// Print the opcode name.
18671873
if (TII)

0 commit comments

Comments
 (0)