[llvm] [LVI][ValueTracking] Take UB-implying attributes into account in `isSafeToSpeculativelyExecute` (PR #137604)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 29 01:01:15 PDT 2025


================
@@ -554,6 +554,20 @@ void Instruction::dropUBImplyingAttrsAndMetadata() {
   dropUBImplyingAttrsAndUnknownMetadata(KnownIDs);
 }
 
+bool Instruction::hasUBImplyingAttrs() const {
+  auto *CB = dyn_cast<CallBase>(this);
+  if (!CB)
+    return false;
+  // For call instructions, we also need to check parameter and return
+  // attributes that are can cause UB.
+  for (unsigned ArgNo = 0; ArgNo < CB->arg_size(); ArgNo++)
+    if (CB->isPassingUndefUB(ArgNo))
+      return true;
+  return CB->hasRetAttr(Attribute::NoUndef) ||
+         CB->getRetDereferenceableBytes() > 0 ||
+         CB->getRetDereferenceableOrNullBytes() > 0;
----------------
nikic wrote:

Using hasRetAttr(Attribute::Dereferenceable) is more efficient (avoids looking up the actual attribute).

Though it would be nicer to base these checks on getUBImplyingAttributes() instead.

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


More information about the llvm-commits mailing list