[clang] [llvm] [FuncAttrs] Deduce `noundef` attributes for return values (PR #76553)

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 29 03:30:06 PST 2023


================
@@ -1279,6 +1280,43 @@ static void addNonNullAttrs(const SCCNodeSet &SCCNodes,
   }
 }
 
+/// Deduce noundef attributes for the SCC.
+static void addNoUndefAttrs(const SCCNodeSet &SCCNodes,
+                            SmallSet<Function *, 8> &Changed) {
+  // Check each function in turn, determining which functions return noundef
+  // values.
+  for (Function *F : SCCNodes) {
+    // Already noundef.
+    if (F->getAttributes().hasRetAttr(Attribute::NoUndef))
+      continue;
+
+    // We can infer and propagate function attributes only when we know that the
+    // definition we'll get at link time is *exactly* the definition we see now.
+    // For more details, see GlobalValue::mayBeDerefined.
+    if (!F->hasExactDefinition())
+      return;
+
+    if (F->getReturnType()->isVoidTy())
+      continue;
+
+    bool HasAnyUndefs = false;
+    for (BasicBlock &BB : *F)
+      if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
+        // TODO: performs context-sensitive analysis?
----------------
nikic wrote:

```suggestion
        // TODO: perform context-sensitive analysis?
```

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


More information about the cfe-commits mailing list