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

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 16 03:38:21 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Florian Hahn (fhahn)

<details>
<summary>Changes</summary>

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?

---
Full diff: https://github.com/llvm/llvm-project/pull/123188.diff


2 Files Affected:

- (modified) llvm/include/llvm/IR/Instruction.h (+4) 
- (modified) llvm/lib/IR/Instruction.cpp (+4-6) 


``````````diff
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 {

``````````

</details>


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


More information about the llvm-commits mailing list