Skip to content

Commit 65238d6

Browse files
committed
[SDAG] Lower range attribute to AssertZext
1 parent d0df146 commit 65238d6

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4487,6 +4487,18 @@ static const MDNode *getRangeMetadata(const Instruction &I) {
44874487
return I.getMetadata(LLVMContext::MD_range);
44884488
}
44894489

4490+
static std::optional<ConstantRange> getRange(const Instruction &I) {
4491+
if (const auto *CB = dyn_cast<CallBase>(&I)) {
4492+
// see comment in getRangeMetadata about this check
4493+
if (CB->hasRetAttr(Attribute::NoUndef))
4494+
return CB->getRange();
4495+
}
4496+
if (const MDNode *Range = getRangeMetadata(I)) {
4497+
return getConstantRangeFromMetadata(*Range);
4498+
}
4499+
return std::nullopt;
4500+
}
4501+
44904502
void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
44914503
if (I.isAtomic())
44924504
return visitAtomicLoad(I);
@@ -10229,19 +10241,16 @@ void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
1022910241
SDValue SelectionDAGBuilder::lowerRangeToAssertZExt(SelectionDAG &DAG,
1023010242
const Instruction &I,
1023110243
SDValue Op) {
10232-
const MDNode *Range = getRangeMetadata(I);
10233-
if (!Range)
10234-
return Op;
10244+
std::optional<ConstantRange> CR = getRange(I);
1023510245

10236-
ConstantRange CR = getConstantRangeFromMetadata(*Range);
10237-
if (CR.isFullSet() || CR.isEmptySet() || CR.isUpperWrapped())
10246+
if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
1023810247
return Op;
1023910248

10240-
APInt Lo = CR.getUnsignedMin();
10249+
APInt Lo = CR->getUnsignedMin();
1024110250
if (!Lo.isMinValue())
1024210251
return Op;
1024310252

10244-
APInt Hi = CR.getUnsignedMax();
10253+
APInt Hi = CR->getUnsignedMax();
1024510254
unsigned Bits = std::max(Hi.getActiveBits(),
1024610255
static_cast<unsigned>(IntegerType::MIN_INT_BITS));
1024710256

llvm/test/CodeGen/AArch64/lower-range-metadata-func-call.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ entry:
3434
ret i32 %and
3535
}
3636

37+
; and can be eliminated
3738
; CHECK-LABEL: {{^}}test_call_known_max_range_attr:
3839
; CHECK: bl foo
39-
; CHECK: and w{{[0-9]+}}, w0, #0x3ff
40+
; CHECK-NOT: and
4041
; CHECK: ret
4142
define i32 @test_call_known_max_range_attr() #0 {
4243
entry:
@@ -56,7 +57,6 @@ entry:
5657
ret i32 %and
5758
}
5859

59-
6060
declare i32 @foo()
6161

6262
attributes #0 = { norecurse nounwind }

llvm/test/CodeGen/X86/legalize-vec-assertzext.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ define i64 @widen_assertzext_range_attr(ptr %x) nounwind {
3939
; CHECK: # %bb.0:
4040
; CHECK-NEXT: pushq %rax
4141
; CHECK-NEXT: callq test2@PLT
42+
; CHECK-NEXT: movb $127, %al
43+
; CHECK-NEXT: kmovw %eax, %k1
44+
; CHECK-NEXT: vpexpandq %zmm0, %zmm0 {%k1} {z}
4245
; CHECK-NEXT: vextracti32x4 $3, %zmm0, %xmm0
4346
; CHECK-NEXT: vmovq %xmm0, %rax
4447
; CHECK-NEXT: popq %rcx

0 commit comments

Comments
 (0)