[llvm] Use range attribute to constant fold comparisons with constant values. (PR #84627)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 9 07:32:16 PST 2024


================
@@ -3729,6 +3729,19 @@ static Value *simplifyICmpWithIntrinsicOnLHS(CmpInst::Predicate Pred,
   }
 }
 
+/// Helper method to get range from metadata or attribute.
+static std::optional<ConstantRange> getRange(Value *V,
+                                             const InstrInfoQuery &IIQ) {
+  if (Instruction *I = dyn_cast<Instruction>(V))
+    if (MDNode *MD = IIQ.getMetadata(I, LLVMContext::MD_range))
+      return getConstantRangeFromMetadata(*MD);
+  if (const Argument *A = dyn_cast<Argument>(V))
+    if (A->hasAttribute(llvm::Attribute::Range))
+      return A->getAttribute(llvm::Attribute::Range).getRange();
----------------
nikic wrote:

You should call `getAttribute()` and then check whether it's valid instead of doing two queries.

https://github.com/llvm/llvm-project/pull/84627


More information about the llvm-commits mailing list