Skip to content

[SDAG] Lower range attribute to AssertZext #95450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 15 additions & 7 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4487,6 +4487,17 @@ static const MDNode *getRangeMetadata(const Instruction &I) {
return I.getMetadata(LLVMContext::MD_range);
}

static std::optional<ConstantRange> getRange(const Instruction &I) {
if (const auto *CB = dyn_cast<CallBase>(&I)) {
// see comment in getRangeMetadata about this check
if (CB->hasRetAttr(Attribute::NoUndef))
return CB->getRange();
}
if (const MDNode *Range = getRangeMetadata(I))
return getConstantRangeFromMetadata(*Range);
return std::nullopt;
}

void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
if (I.isAtomic())
return visitAtomicLoad(I);
Expand Down Expand Up @@ -10229,19 +10240,16 @@ void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
SDValue SelectionDAGBuilder::lowerRangeToAssertZExt(SelectionDAG &DAG,
const Instruction &I,
SDValue Op) {
const MDNode *Range = getRangeMetadata(I);
if (!Range)
return Op;
std::optional<ConstantRange> CR = getRange(I);

ConstantRange CR = getConstantRangeFromMetadata(*Range);
if (CR.isFullSet() || CR.isEmptySet() || CR.isUpperWrapped())
if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
return Op;

APInt Lo = CR.getUnsignedMin();
APInt Lo = CR->getUnsignedMin();
if (!Lo.isMinValue())
return Op;

APInt Hi = CR.getUnsignedMax();
APInt Hi = CR->getUnsignedMax();
unsigned Bits = std::max(Hi.getActiveBits(),
static_cast<unsigned>(IntegerType::MIN_INT_BITS));

Expand Down
22 changes: 22 additions & 0 deletions llvm/test/CodeGen/AArch64/lower-range-metadata-func-call.ll
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ entry:
ret i32 %and
}

; and can be eliminated
; CHECK-LABEL: {{^}}test_call_known_max_range_attr:
; CHECK: bl foo
; CHECK-NOT: and
; CHECK: ret
define i32 @test_call_known_max_range_attr() #0 {
entry:
%id = tail call noundef range(i32 0, 1024) i32 @foo()
%and = and i32 %id, 1023
ret i32 %and
}

; CHECK-LABEL: {{^}}test_call_known_max_range_attr_no_noundef:
; CHECK: bl foo
; CHECK: and w{{[0-9]+}}, w0, #0x3ff
; CHECK: ret
define i32 @test_call_known_max_range_attr_no_noundef() #0 {
entry:
%id = tail call range(i32 0, 1024) i32 @foo()
%and = and i32 %id, 1023
ret i32 %and
}

declare i32 @foo()

Expand Down
18 changes: 18 additions & 0 deletions llvm/test/CodeGen/X86/legalize-vec-assertzext.ll
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ define i64 @widen_assertzext(ptr %x) nounwind {
ret i64 %d
}

define i64 @widen_assertzext_range_attr(ptr %x) nounwind {
; CHECK-LABEL: widen_assertzext_range_attr:
; CHECK: # %bb.0:
; CHECK-NEXT: pushq %rax
; CHECK-NEXT: callq test2@PLT
; CHECK-NEXT: movb $127, %al
; CHECK-NEXT: kmovw %eax, %k1
; CHECK-NEXT: vpexpandq %zmm0, %zmm0 {%k1} {z}
; CHECK-NEXT: vextracti32x4 $3, %zmm0, %xmm0
; CHECK-NEXT: vmovq %xmm0, %rax
; CHECK-NEXT: popq %rcx
; CHECK-NEXT: vzeroupper
; CHECK-NEXT: retq
%e = call noundef range(i64 0, 2) <7 x i64> @test2()
%d = extractelement <7 x i64> %e, i32 6
ret i64 %d
}

declare <16 x i64> @test()
declare <7 x i64> @test2()
!0 = !{ i64 0, i64 2 }
Loading