[PATCH] D157408: [SCCP][PhaseOrdering] Mark Overdefined for loading from null

luxufan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 8 09:25:13 PDT 2023


StephenFan created this revision.
StephenFan added a reviewer: nikic.
Herald added a subscriber: hiraditya.
Herald added a project: All.
StephenFan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Currently, SCCPSolver preserves the unknown state of lattice value when
solving a load from null. But a unknown state would be replaced as undef
in the end at method 'getConstantOrNull'.

As a comparison, InstCombine creates a non-terminal unreachable to
inform the SimplifyCFG pass inserting an unreachable instruction.

As test file load-from-null.ll shows, The way that InstCombine does is
better than SCCP. By marking it as overdefined, SCCP would not replace
it as undef, and let InstCombine optimize it to just an unreachable.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157408

Files:
  llvm/lib/Transforms/Utils/SCCPSolver.cpp
  llvm/test/Transforms/PhaseOrdering/load-from-null.ll


Index: llvm/test/Transforms/PhaseOrdering/load-from-null.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/PhaseOrdering/load-from-null.ll
@@ -0,0 +1,24 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt -O1 -S < %s | FileCheck %s
+; RUN: opt -O2 -S < %s | FileCheck %s
+; RUN: opt -O3 -S < %s | FileCheck %s
+;
+define void @load_invalid_address(ptr %p, i1 %a) {
+; CHECK-LABEL: define void @load_invalid_address
+; CHECK-SAME: (ptr nocapture readnone [[P:%.*]], i1 [[A:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    unreachable
+;
+entry:
+  br i1 %a, label %if.then, label %if.else
+
+if.else:
+  br label %if.then
+
+if.then:                                           ; preds = %lbl, %for.cond1
+  %e.2 = phi ptr [ undef, %entry ], [ null, %if.else ]
+  %0 = load i32, ptr %e.2, align 4
+  store i32 %0, ptr %p
+  ret void
+
+}
Index: llvm/lib/Transforms/Utils/SCCPSolver.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SCCPSolver.cpp
+++ llvm/lib/Transforms/Utils/SCCPSolver.cpp
@@ -1600,12 +1600,8 @@
     Constant *Ptr = getConstant(PtrVal, I.getOperand(0)->getType());
 
     // load null is undefined.
-    if (isa<ConstantPointerNull>(Ptr)) {
-      if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
-        return (void)markOverdefined(IV, &I);
-      else
-        return;
-    }
+    if (isa<ConstantPointerNull>(Ptr))
+      return (void)markOverdefined(IV, &I);
 
     // Transform load (constant global) into the value loaded.
     if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157408.548253.patch
Type: text/x-patch
Size: 1733 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230808/528bcc03/attachment.bin>


More information about the llvm-commits mailing list