[llvm] 657f26f - [SCCP] Add more non-null roots

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 27 07:02:57 PDT 2024


Author: Nikita Popov
Date: 2024-08-27T15:53:22+02:00
New Revision: 657f26f038a733769909e0db44e234e9369294b6

URL: https://github.com/llvm/llvm-project/commit/657f26f038a733769909e0db44e234e9369294b6
DIFF: https://github.com/llvm/llvm-project/commit/657f26f038a733769909e0db44e234e9369294b6.diff

LOG: [SCCP] Add more non-null roots

Also consider allocas non-null (subject to the usual caveats),
and consider nonnull/dereferenceable metadata on calls.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SCCPSolver.cpp
    llvm/test/Transforms/SCCP/pointer-nonnull.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
index c21b4c0b3a1c87..982b1041c7c514 100644
--- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp
+++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
@@ -673,6 +673,7 @@ class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
   void visitStoreInst(StoreInst &I);
   void visitLoadInst(LoadInst &I);
   void visitGetElementPtrInst(GetElementPtrInst &I);
+  void visitAllocaInst(AllocaInst &AI);
 
   void visitInvokeInst(InvokeInst &II) {
     visitCallBase(II);
@@ -1626,6 +1627,13 @@ void SCCPInstVisitor::visitGetElementPtrInst(GetElementPtrInst &I) {
     markConstant(&I, C);
 }
 
+void SCCPInstVisitor::visitAllocaInst(AllocaInst &I) {
+  if (!NullPointerIsDefined(I.getFunction(), I.getAddressSpace()))
+    return (void)markNotNull(ValueState[&I], &I);
+
+  markOverdefined(&I);
+}
+
 void SCCPInstVisitor::visitStoreInst(StoreInst &SI) {
   // If this store is of a struct, ignore it.
   if (SI.getOperand(0)->getType()->isStructTy())
@@ -1647,18 +1655,23 @@ void SCCPInstVisitor::visitStoreInst(StoreInst &SI) {
 }
 
 static ValueLatticeElement getValueFromMetadata(const Instruction *I) {
-  if (I->getType()->isIntOrIntVectorTy()) {
-    if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range))
-      return ValueLatticeElement::getRange(
-          getConstantRangeFromMetadata(*Ranges));
-
-    if (const auto *CB = dyn_cast<CallBase>(I))
+  if (const auto *CB = dyn_cast<CallBase>(I)) {
+    if (CB->getType()->isIntOrIntVectorTy())
       if (std::optional<ConstantRange> Range = CB->getRange())
         return ValueLatticeElement::getRange(*Range);
+    if (CB->getType()->isPointerTy() && CB->isReturnNonNull())
+      return ValueLatticeElement::getNot(
+          ConstantPointerNull::get(cast<PointerType>(I->getType())));
   }
+
+  if (I->getType()->isIntOrIntVectorTy())
+    if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range))
+      return ValueLatticeElement::getRange(
+          getConstantRangeFromMetadata(*Ranges));
   if (I->hasMetadata(LLVMContext::MD_nonnull))
     return ValueLatticeElement::getNot(
         ConstantPointerNull::get(cast<PointerType>(I->getType())));
+
   return ValueLatticeElement::getOverdefined();
 }
 

diff  --git a/llvm/test/Transforms/SCCP/pointer-nonnull.ll b/llvm/test/Transforms/SCCP/pointer-nonnull.ll
index 54ec04fbdeb176..cd04c1c2d39d9b 100644
--- a/llvm/test/Transforms/SCCP/pointer-nonnull.ll
+++ b/llvm/test/Transforms/SCCP/pointer-nonnull.ll
@@ -43,8 +43,7 @@ define i1 @test_dereferenceable(ptr dereferenceable(4) %p) {
 define i1 @test_alloca() {
 ; CHECK-LABEL: define i1 @test_alloca() {
 ; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ne ptr [[A]], null
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %a = alloca i32
   %cmp = icmp ne ptr %a, null
@@ -88,8 +87,7 @@ define i1 @test_load_nonnull(ptr %p) {
 define i1 @test_call_nonnull() {
 ; CHECK-LABEL: define i1 @test_call_nonnull() {
 ; CHECK-NEXT:    [[P:%.*]] = call nonnull ptr @get()
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ne ptr [[P]], null
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %p = call nonnull ptr @get()
   %cmp = icmp ne ptr %p, null
@@ -99,8 +97,7 @@ define i1 @test_call_nonnull() {
 define i1 @test_call_dereferenceable() {
 ; CHECK-LABEL: define i1 @test_call_dereferenceable() {
 ; CHECK-NEXT:    [[P:%.*]] = call dereferenceable(4) ptr @get()
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ne ptr [[P]], null
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %p = call dereferenceable(4) ptr @get()
   %cmp = icmp ne ptr %p, null


        


More information about the llvm-commits mailing list