[llvm] [llubi] Add support for poison-generating/UB-implying annotations (PR #195339)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Sun May 3 03:46:39 PDT 2026
================
@@ -64,6 +64,79 @@ static AnyValue mulNoWrap(const APInt &LHS, const APInt &RHS, bool HasNSW,
return Res;
}
+/// Visit the scalar values recursively. The callback function may modify the
+/// value in-place.
+static void forEachScalarValue(AnyValue &V,
+ function_ref<void(AnyValue &)> Visit) {
+ if (V.isNone())
+ return;
+
+ if (V.isAggregate()) {
+ for (auto &SubValue : V.asAggregate())
+ forEachScalarValue(SubValue, Visit);
+ return;
+ }
+
+ Visit(V);
+}
+
+static void applyRangeAttr(AnyValue &V, const ConstantRange &CR) {
+ forEachScalarValue(V, [&](AnyValue &Scalar) {
+ if (Scalar.isInteger() && !CR.contains(Scalar.asInteger()))
+ Scalar = AnyValue::poison();
+ });
+}
+
+static void applyNoFPClassAttr(AnyValue &V, FPClassTest NoFPClass) {
+ forEachScalarValue(V, [NoFPClass](AnyValue &Scalar) {
+ if (Scalar.isFloat() && (Scalar.asFloat().classify() & NoFPClass))
+ Scalar = AnyValue::poison();
+ });
+}
+
+static void applyNonNullAttr(AnyValue &V) {
+ if (V.isPointer() && V.asPointer().address().isZero())
+ V = AnyValue::poison();
+}
+
+static void applyAlignAttr(AnyValue &V, Align Alignment) {
+ forEachScalarValue(V, [Alignment](AnyValue &Scalar) {
+ if (Scalar.isPointer() &&
+ Scalar.asPointer().address().countr_zero() < Log2(Alignment))
+ Scalar = AnyValue::poison();
+ });
+}
+
+static bool applyNoUndefAttr(AnyValue &V) {
----------------
nikic wrote:
```suggestion
static bool violatesNoUndefAttr(AnyValue &V) {
```
Or something, to make clear what the return value means.
https://github.com/llvm/llvm-project/pull/195339
More information about the llvm-commits
mailing list