Skip to content

Commit d51b278

Browse files
committed
[IR] Intersect call and fn range in CallBase::getRange()
To make sure that a larger range on the call-site does not suppress information from a smaller range at the declaration.
1 parent 278062f commit d51b278

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

llvm/lib/IR/Instructions.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,17 @@ FPClassTest CallBase::getParamNoFPClass(unsigned i) const {
376376
}
377377

378378
std::optional<ConstantRange> CallBase::getRange() const {
379-
const Attribute RangeAttr = getRetAttr(llvm::Attribute::Range);
380-
if (RangeAttr.isValid())
381-
return RangeAttr.getRange();
379+
Attribute CallAttr = Attrs.getRetAttr(Attribute::Range);
380+
Attribute FnAttr;
381+
if (const Function *F = getCalledFunction())
382+
FnAttr = F->getRetAttribute(Attribute::Range);
383+
384+
if (CallAttr.isValid() && FnAttr.isValid())
385+
return CallAttr.getRange().intersectWith(FnAttr.getRange());
386+
if (CallAttr.isValid())
387+
return CallAttr.getRange();
388+
if (FnAttr.isValid())
389+
return FnAttr.getRange();
382390
return std::nullopt;
383391
}
384392

llvm/test/Transforms/CorrelatedValuePropagation/uscmp.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,9 @@ define i8 @ucmp_switch_callsite_range(i32 %x, i32 %y) {
312312
; CHECK-LABEL: @ucmp_switch_callsite_range(
313313
; CHECK-NEXT: [[CMP:%.*]] = call range(i8 -2, 3) i8 @llvm.ucmp.i8.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
314314
; CHECK-NEXT: switch i8 [[CMP]], label [[DEFAULT_UNREACHABLE:%.*]] [
315-
; CHECK-NEXT: i8 -2, label [[BB_NEG2:%.*]]
315+
; CHECK-NEXT: i8 1, label [[BB_1:%.*]]
316316
; CHECK-NEXT: i8 -1, label [[BB_NEG1:%.*]]
317317
; CHECK-NEXT: i8 0, label [[BB_0:%.*]]
318-
; CHECK-NEXT: i8 1, label [[BB_1:%.*]]
319-
; CHECK-NEXT: i8 2, label [[BB_2:%.*]]
320318
; CHECK-NEXT: ]
321319
; CHECK: bb.neg2:
322320
; CHECK-NEXT: ret i8 -2

0 commit comments

Comments
 (0)