Skip to content

Commit dcf1daa

Browse files
committed
Use range attribute to constant fold comparisons with constant values.
1 parent a3d1212 commit dcf1daa

File tree

6 files changed

+57
-35
lines changed

6 files changed

+57
-35
lines changed

llvm/include/llvm/IR/Attributes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,11 @@ class AttributeList {
848848
return getAttributeAtIndex(FunctionIndex, Kind);
849849
}
850850

851+
/// Return the attribute for the given attribute kind for the return value.
852+
Attribute getRetAttr(Attribute::AttrKind Kind) const {
853+
return getAttributeAtIndex(ReturnIndex, Kind);
854+
}
855+
851856
/// Return the alignment of the return value.
852857
MaybeAlign getRetAlignment() const;
853858

llvm/include/llvm/IR/Function.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
430430
/// Return the attribute for the given attribute kind.
431431
Attribute getFnAttribute(StringRef Kind) const;
432432

433+
/// Return the attribute for the given attribute kind for the return value.
434+
Attribute getRetAttribute(Attribute::AttrKind Kind) const;
435+
433436
/// For a string attribute \p Kind, parse attribute as an integer.
434437
///
435438
/// \returns \p Default if attribute is not present.

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,18 @@ class CallBase : public Instruction {
19091909
/// Determine whether the return value has the given attribute.
19101910
bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); }
19111911

1912+
/// Return the attribute for the given attribute kind for the return value.
1913+
Attribute getRetAttr(Attribute::AttrKind Kind) const {
1914+
Attribute RetAttr = Attrs.getRetAttr(Kind);
1915+
if (RetAttr.isValid())
1916+
return RetAttr;
1917+
1918+
// Look at the callee, if available.
1919+
if (const Function *F = getCalledFunction())
1920+
return F->getAttributes().getRetAttr(Kind);
1921+
return Attribute();
1922+
}
1923+
19121924
/// Determine whether the argument or parameter has the given attribute.
19131925
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const;
19141926

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3729,6 +3729,26 @@ static Value *simplifyICmpWithIntrinsicOnLHS(CmpInst::Predicate Pred,
37293729
}
37303730
}
37313731

3732+
/// Helper method to get range from metadata or attribute.
3733+
static std::optional<ConstantRange> getRange(Value *V,
3734+
const InstrInfoQuery &IIQ) {
3735+
if (Instruction *I = dyn_cast<Instruction>(V))
3736+
if (MDNode *MD = IIQ.getMetadata(I, LLVMContext::MD_range))
3737+
return getConstantRangeFromMetadata(*MD);
3738+
3739+
Attribute Range;
3740+
if (const Argument *A = dyn_cast<Argument>(V)) {
3741+
Range = A->getAttribute(llvm::Attribute::Range);
3742+
} else if (const CallBase *CB = dyn_cast<CallBase>(V)) {
3743+
Range = CB->getRetAttr(llvm::Attribute::Range);
3744+
}
3745+
3746+
if (Range.isValid())
3747+
return Range.getRange();
3748+
3749+
return std::nullopt;
3750+
}
3751+
37323752
/// Given operands for an ICmpInst, see if we can fold the result.
37333753
/// If not, this returns null.
37343754
static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
@@ -3776,24 +3796,14 @@ static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
37763796

37773797
// If both operands have range metadata, use the metadata
37783798
// to simplify the comparison.
3779-
if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) {
3780-
auto RHS_Instr = cast<Instruction>(RHS);
3781-
auto LHS_Instr = cast<Instruction>(LHS);
3782-
3783-
if (Q.IIQ.getMetadata(RHS_Instr, LLVMContext::MD_range) &&
3784-
Q.IIQ.getMetadata(LHS_Instr, LLVMContext::MD_range)) {
3785-
auto RHS_CR = getConstantRangeFromMetadata(
3786-
*RHS_Instr->getMetadata(LLVMContext::MD_range));
3787-
auto LHS_CR = getConstantRangeFromMetadata(
3788-
*LHS_Instr->getMetadata(LLVMContext::MD_range));
3789-
3790-
if (LHS_CR.icmp(Pred, RHS_CR))
3799+
if (std::optional<ConstantRange> RhsCr = getRange(RHS, Q.IIQ))
3800+
if (std::optional<ConstantRange> LhsCr = getRange(LHS, Q.IIQ)) {
3801+
if (LhsCr->icmp(Pred, *RhsCr))
37913802
return ConstantInt::getTrue(ITy);
37923803

3793-
if (LHS_CR.icmp(CmpInst::getInversePredicate(Pred), RHS_CR))
3804+
if (LhsCr->icmp(CmpInst::getInversePredicate(Pred), *RhsCr))
37943805
return ConstantInt::getFalse(ITy);
37953806
}
3796-
}
37973807

37983808
// Compare of cast, for example (zext X) != 0 -> X != 0
37993809
if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {

llvm/lib/IR/Function.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,10 @@ Attribute Function::getFnAttribute(StringRef Kind) const {
700700
return AttributeSets.getFnAttr(Kind);
701701
}
702702

703+
Attribute Function::getRetAttribute(Attribute::AttrKind Kind) const {
704+
return AttributeSets.getRetAttr(Kind);
705+
}
706+
703707
uint64_t Function::getFnAttributeAsParsedInteger(StringRef Name,
704708
uint64_t Default) const {
705709
Attribute A = getFnAttribute(Name);

llvm/test/Transforms/InstCombine/icmp-range.ll

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ define i1 @test_two_ranges2(ptr nocapture readonly %arg1, ptr nocapture readonly
173173
; Values' ranges do not overlap each other, so it can simplified to false.
174174
define i1 @test_two_argument_ranges(i32 range(i32 1, 6) %arg1, i32 range(i32 8, 16) %arg2) {
175175
; CHECK-LABEL: @test_two_argument_ranges(
176-
; CHECK-NEXT: [[RVAL:%.*]] = icmp ult i32 [[ARG2:%.*]], [[ARG1:%.*]]
177-
; CHECK-NEXT: ret i1 [[RVAL]]
176+
; CHECK-NEXT: ret i1 false
178177
;
179178
%rval = icmp ult i32 %arg2, %arg1
180179
ret i1 %rval
@@ -183,9 +182,7 @@ define i1 @test_two_argument_ranges(i32 range(i32 1, 6) %arg1, i32 range(i32 8,
183182
; Values' ranges do not overlap each other, so it can simplified to false.
184183
define i1 @test_one_range_and_one_argument_range(ptr nocapture readonly %arg1, i32 range(i32 8, 16) %arg2) {
185184
; CHECK-LABEL: @test_one_range_and_one_argument_range(
186-
; CHECK-NEXT: [[VAL1:%.*]] = load i32, ptr [[ARG1:%.*]], align 4, !range [[RNG2]]
187-
; CHECK-NEXT: [[RVAL:%.*]] = icmp ugt i32 [[VAL1]], [[ARG2:%.*]]
188-
; CHECK-NEXT: ret i1 [[RVAL]]
185+
; CHECK-NEXT: ret i1 false
189186
;
190187
%val1 = load i32, ptr %arg1, !range !0
191188
%rval = icmp ult i32 %arg2, %val1
@@ -195,9 +192,7 @@ define i1 @test_one_range_and_one_argument_range(ptr nocapture readonly %arg1, i
195192
; Values' ranges do not overlap each other, so it can simplified to false.
196193
define i1 @test_one_argument_range_and_one_range(i32 range(i32 1, 6) %arg1, ptr nocapture readonly %arg2) {
197194
; CHECK-LABEL: @test_one_argument_range_and_one_range(
198-
; CHECK-NEXT: [[VAL1:%.*]] = load i32, ptr [[ARG2:%.*]], align 4, !range [[RNG5]]
199-
; CHECK-NEXT: [[RVAL:%.*]] = icmp ult i32 [[VAL1]], [[ARG1:%.*]]
200-
; CHECK-NEXT: ret i1 [[RVAL]]
195+
; CHECK-NEXT: ret i1 false
201196
;
202197
%val1 = load i32, ptr %arg2, !range !6
203198
%rval = icmp ult i32 %val1, %arg1
@@ -264,8 +259,7 @@ define <2 x i1> @test_two_argument_ranges_vec(<2 x i32> range(i32 5, 10) %arg1,
264259
; Values' ranges do not overlap each other, so it can simplified to false.
265260
define <2 x i1> @test_two_argument_ranges_vec_false(<2 x i32> range(i32 1, 6) %arg1, <2 x i32> range(i32 8, 16) %arg2) {
266261
; CHECK-LABEL: @test_two_argument_ranges_vec_false(
267-
; CHECK-NEXT: [[RVAL:%.*]] = icmp ult <2 x i32> [[ARG2:%.*]], [[ARG1:%.*]]
268-
; CHECK-NEXT: ret <2 x i1> [[RVAL]]
262+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
269263
;
270264
%rval = icmp ult <2 x i32> %arg2, %arg1
271265
ret <2 x i1> %rval
@@ -274,8 +268,7 @@ define <2 x i1> @test_two_argument_ranges_vec_false(<2 x i32> range(i32 1, 6) %a
274268
; Values' ranges do not overlap each other, so it can simplified to true.
275269
define <2 x i1> @test_two_argument_ranges_vec_true(<2 x i32> range(i32 1, 6) %arg1, <2 x i32> range(i32 8, 16) %arg2) {
276270
; CHECK-LABEL: @test_two_argument_ranges_vec_true(
277-
; CHECK-NEXT: [[RVAL:%.*]] = icmp ugt <2 x i32> [[ARG2:%.*]], [[ARG1:%.*]]
278-
; CHECK-NEXT: ret <2 x i1> [[RVAL]]
271+
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
279272
;
280273
%rval = icmp ugt <2 x i32> %arg2, %arg1
281274
ret <2 x i1> %rval
@@ -304,8 +297,7 @@ define i1 @test_two_return_attribute_ranges_one_in_call() {
304297
; CHECK-LABEL: @test_two_return_attribute_ranges_one_in_call(
305298
; CHECK-NEXT: [[VAL1:%.*]] = call range(i32 1, 6) i32 @create_range1()
306299
; CHECK-NEXT: [[ARG1:%.*]] = call i32 @create_range2()
307-
; CHECK-NEXT: [[RVAL:%.*]] = icmp ult i32 [[ARG1]], [[VAL1]]
308-
; CHECK-NEXT: ret i1 [[RVAL]]
300+
; CHECK-NEXT: ret i1 false
309301
;
310302
%val1 = call range(i32 1, 6) i32 @create_range1()
311303
%val2 = call i32 @create_range2()
@@ -318,8 +310,7 @@ define i1 @test_two_return_attribute_ranges() {
318310
; CHECK-LABEL: @test_two_return_attribute_ranges(
319311
; CHECK-NEXT: [[VAL1:%.*]] = call i32 @create_range3()
320312
; CHECK-NEXT: [[ARG1:%.*]] = call i32 @create_range2()
321-
; CHECK-NEXT: [[RVAL:%.*]] = icmp ult i32 [[ARG1]], [[VAL1]]
322-
; CHECK-NEXT: ret i1 [[RVAL]]
313+
; CHECK-NEXT: ret i1 false
323314
;
324315
%val1 = call i32 @create_range3()
325316
%val2 = call i32 @create_range2()
@@ -331,8 +322,7 @@ define i1 @test_two_return_attribute_ranges() {
331322
define i1 @test_one_return_argument_and_one_argument_range(i32 range(i32 8, 16) %arg1) {
332323
; CHECK-LABEL: @test_one_return_argument_and_one_argument_range(
333324
; CHECK-NEXT: [[VAL1:%.*]] = call i32 @create_range3()
334-
; CHECK-NEXT: [[RVAL:%.*]] = icmp ugt i32 [[VAL1]], [[ARG1:%.*]]
335-
; CHECK-NEXT: ret i1 [[RVAL]]
325+
; CHECK-NEXT: ret i1 false
336326
;
337327
%val1 = call i32 @create_range3()
338328
%rval = icmp ult i32 %arg1, %val1
@@ -343,9 +333,7 @@ define i1 @test_one_return_argument_and_one_argument_range(i32 range(i32 8, 16)
343333
define i1 @test_one_range_and_one_return_argument(ptr nocapture readonly %arg1) {
344334
; CHECK-LABEL: @test_one_range_and_one_return_argument(
345335
; CHECK-NEXT: [[VAL1:%.*]] = call i32 @create_range3()
346-
; CHECK-NEXT: [[VAL2:%.*]] = load i32, ptr [[ARG1:%.*]], align 4, !range [[RNG5]]
347-
; CHECK-NEXT: [[RVAL:%.*]] = icmp ult i32 [[VAL2]], [[VAL1]]
348-
; CHECK-NEXT: ret i1 [[RVAL]]
336+
; CHECK-NEXT: ret i1 false
349337
;
350338
%val1 = call i32 @create_range3()
351339
%val2 = load i32, ptr %arg1, !range !6

0 commit comments

Comments
 (0)