[clang] [analyzer] Check the nonnull annotation first in NullabilityChecker::checkLocation (PR #245166)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 01:18:48 PST 2026


https://github.com/Xazax-hun created https://github.com/llvm/llvm-project/pull/245166

NullabilityChecker::checkLocation is called for every load from a typed region. It looked up the stored value in the store before checking whether the region's type is annotated _Nonnull, which it rarely is. Check the annotation first, so that the store lookup only happens for annotated locations.

On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces the instructions of the static analyzer by 0.1-0.2%.

Assisted-by: Claude Opus 5.5

>From fd11a350468750be48baad89560a9c80696009f1 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Sun, 22 Nov 2026 19:57:47 +0000
Subject: [PATCH] [analyzer] Check the nonnull annotation first in
 NullabilityChecker::checkLocation

NullabilityChecker::checkLocation is called for every load from a typed
region. It looked up the stored value in the store before checking
whether the region's type is annotated _Nonnull, which it rarely is.
Check the annotation first, so that the store lookup only happens for
annotated locations.

On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this reduces
the instructions of the static analyzer by 0.1-0.2%. Cycles and peak
memory are unchanged within noise. The diagnostics and the analyzer
statistics are unchanged.

Assisted-by: Claude Opus 5.5
---
 .../Checkers/NullabilityChecker.cpp              | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
index fd82fa2ff92a58..43e4f9ed4e2d39 100644
--- a/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
@@ -621,21 +621,19 @@ void NullabilityChecker::checkLocation(SVal Location, bool IsLoad,
   if (!Region)
     return;
 
+  if (getNullabilityAnnotation(Region->getValueType()) != Nullability::Nonnull)
+    return;
+
   ProgramStateRef State = Context.getState();
 
   auto StoredVal = State->getSVal(Region).getAs<loc::MemRegionVal>();
   if (!StoredVal)
     return;
 
-  Nullability NullabilityOfTheLoadedValue =
-      getNullabilityAnnotation(Region->getValueType());
-
-  if (NullabilityOfTheLoadedValue == Nullability::Nonnull) {
-    // It doesn't matter what we think about this particular pointer, it should
-    // be considered non-null as annotated by the developer.
-    if (ProgramStateRef NewState = State->assume(*StoredVal, true)) {
-      Context.addTransition(NewState);
-    }
+  // It doesn't matter what we think about this particular pointer, it should
+  // be considered non-null as annotated by the developer.
+  if (ProgramStateRef NewState = State->assume(*StoredVal, true)) {
+    Context.addTransition(NewState);
   }
 }
 



More information about the cfe-commits mailing list