[llvm] [IR] Provide array with poison-generating metadata IDs. (PR #123188)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 16 03:37:48 PST 2025


https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/123188

Add Instruction::PoisonGeneratingMetadataIDs containing IDs of poison-generating metadata to allow easier re-use.

Currently it is a static const array in Instruction, maybe here's a better place?

>From a679a5b14b61f12848baa9d3cc5b8972edf0151a Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Wed, 15 Jan 2025 21:44:46 +0000
Subject: [PATCH] [IR] Provide array with poison-generating metadata IDs.

Add Instruction::PoisonGeneratingMetadataIDs containing IDs of
poison-generating metadata to allow easier re-use.

Currently it is a static const array in Instruction, maybe here's a
better place?
---
 llvm/include/llvm/IR/Instruction.h |  4 ++++
 llvm/lib/IR/Instruction.cpp        | 10 ++++------
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h
index aa480aa8d98636..645aa57d053e2e 100644
--- a/llvm/include/llvm/IR/Instruction.h
+++ b/llvm/include/llvm/IR/Instruction.h
@@ -503,6 +503,10 @@ class Instruction : public User,
   /// Determine whether the the nneg flag is set.
   bool hasNonNeg() const LLVM_READONLY;
 
+  /// Metadata IDs that may generate poison.
+  constexpr static const unsigned PoisonGeneratingMetadataIDs[] = {
+      LLVMContext::MD_range, LLVMContext::MD_nonnull, LLVMContext::MD_align};
+
   /// Return true if this operator has flags which may cause this instruction
   /// to evaluate to poison despite having non-poison inputs.
   bool hasPoisonGeneratingFlags() const LLVM_READONLY;
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index 147cd84125c8d1..12aefd8b1e96c1 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -458,9 +458,8 @@ void Instruction::dropPoisonGeneratingFlags() {
 }
 
 bool Instruction::hasPoisonGeneratingMetadata() const {
-  return hasMetadata(LLVMContext::MD_range) ||
-         hasMetadata(LLVMContext::MD_nonnull) ||
-         hasMetadata(LLVMContext::MD_align);
+  return any_of(PoisonGeneratingMetadataIDs,
+                [this](unsigned ID) { return hasMetadata(ID); });
 }
 
 bool Instruction::hasNonDebugLocLoopMetadata() const {
@@ -487,9 +486,8 @@ bool Instruction::hasNonDebugLocLoopMetadata() const {
 }
 
 void Instruction::dropPoisonGeneratingMetadata() {
-  eraseMetadata(LLVMContext::MD_range);
-  eraseMetadata(LLVMContext::MD_nonnull);
-  eraseMetadata(LLVMContext::MD_align);
+  for (unsigned ID : PoisonGeneratingMetadataIDs)
+    eraseMetadata(ID);
 }
 
 bool Instruction::hasPoisonGeneratingReturnAttributes() const {



More information about the llvm-commits mailing list