Skip to content

Commit 87d14b1

Browse files
committed
Add helper functions to get range attribute
1 parent 85b6af1 commit 87d14b1

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

llvm/include/llvm/IR/Argument.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
#include "llvm/ADT/Twine.h"
1717
#include "llvm/IR/Attributes.h"
1818
#include "llvm/IR/Value.h"
19+
#include <optional>
1920

2021
namespace llvm {
2122

23+
class ConstantRange;
24+
2225
/// This class represents an incoming formal argument to a Function. A formal
2326
/// argument, since it is ``formal'', does not contain an actual value but
2427
/// instead represents the type, argument number, and attributes of an argument
@@ -67,6 +70,10 @@ class Argument final : public Value {
6770
/// disallowed floating-point values. Otherwise, fcNone is returned.
6871
FPClassTest getNoFPClass() const;
6972

73+
/// If this argument has a range attribute, return the value range of the
74+
/// argument. Otherwise, std::nullopt is returned.
75+
std::optional<ConstantRange> getRange() const;
76+
7077
/// Return true if this argument has the byval attribute.
7178
bool hasByValAttr() const;
7279

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace llvm {
4343
class StringRef;
4444
class Type;
4545
class Value;
46+
class ConstantRange;
4647

4748
namespace Intrinsic {
4849
typedef unsigned ID;
@@ -1917,7 +1918,7 @@ class CallBase : public Instruction {
19171918

19181919
// Look at the callee, if available.
19191920
if (const Function *F = getCalledFunction())
1920-
return F->getAttributes().getRetAttr(Kind);
1921+
return F->getRetAttribute(Kind);
19211922
return Attribute();
19221923
}
19231924

@@ -2154,6 +2155,10 @@ class CallBase : public Instruction {
21542155
/// parameter.
21552156
FPClassTest getParamNoFPClass(unsigned i) const;
21562157

2158+
/// If this return value has a range attribute, return the value range of the
2159+
/// argument. Otherwise, std::nullopt is returned.
2160+
std::optional<ConstantRange> getRange() const;
2161+
21572162
/// Return true if the return value is known to be not null.
21582163
/// This may be because it has the nonnull attribute, or because at least
21592164
/// one byte is dereferenceable and the pointer is in addrspace(0).

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3736,15 +3736,10 @@ static std::optional<ConstantRange> getRange(Value *V,
37363736
if (MDNode *MD = IIQ.getMetadata(I, LLVMContext::MD_range))
37373737
return getConstantRangeFromMetadata(*MD);
37383738

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();
3739+
if (const Argument *A = dyn_cast<Argument>(V))
3740+
return A->getRange();
3741+
else if (const CallBase *CB = dyn_cast<CallBase>(V))
3742+
return CB->getRange();
37483743

37493744
return std::nullopt;
37503745
}

llvm/lib/IR/Function.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/IR/Attributes.h"
2525
#include "llvm/IR/BasicBlock.h"
2626
#include "llvm/IR/Constant.h"
27+
#include "llvm/IR/ConstantRange.h"
2728
#include "llvm/IR/Constants.h"
2829
#include "llvm/IR/DerivedTypes.h"
2930
#include "llvm/IR/GlobalValue.h"
@@ -256,6 +257,13 @@ FPClassTest Argument::getNoFPClass() const {
256257
return getParent()->getParamNoFPClass(getArgNo());
257258
}
258259

260+
std::optional<ConstantRange> Argument::getRange() const {
261+
const Attribute RangeAttr = getAttribute(llvm::Attribute::Range);
262+
if (RangeAttr.isValid())
263+
return RangeAttr.getRange();
264+
return std::nullopt;
265+
}
266+
259267
bool Argument::hasNestAttr() const {
260268
if (!getType()->isPointerTy()) return false;
261269
return hasAttribute(Attribute::Nest);

llvm/lib/IR/Instructions.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/IR/Attributes.h"
2020
#include "llvm/IR/BasicBlock.h"
2121
#include "llvm/IR/Constant.h"
22+
#include "llvm/IR/ConstantRange.h"
2223
#include "llvm/IR/Constants.h"
2324
#include "llvm/IR/DataLayout.h"
2425
#include "llvm/IR/DerivedTypes.h"
@@ -395,6 +396,13 @@ FPClassTest CallBase::getParamNoFPClass(unsigned i) const {
395396
return Mask;
396397
}
397398

399+
std::optional<ConstantRange> CallBase::getRange() const {
400+
const Attribute RangeAttr = getRetAttr(llvm::Attribute::Range);
401+
if (RangeAttr.isValid())
402+
return RangeAttr.getRange();
403+
return std::nullopt;
404+
}
405+
398406
bool CallBase::isReturnNonNull() const {
399407
if (hasRetAttr(Attribute::NonNull))
400408
return true;

0 commit comments

Comments
 (0)