[PATCH] D90286: [Analysis] Improve EmitGEPOffset by avoiding summ with zero

Yevgeny Rouban via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 27 22:47:20 PDT 2020


yrouban created this revision.
yrouban added reviewers: craig.topper, miyuki, apilipenko.
Herald added subscribers: pengfei, hiraditya.
Herald added a project: LLVM.
yrouban requested review of this revision.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90286

Files:
  llvm/include/llvm/Analysis/Utils/Local.h
  llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp


Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1672,12 +1672,10 @@
   Value *Result = EmitGEPOffset(GEP1);
 
   // If this is a single inbounds GEP and the original sub was nuw,
-  // then the final multiplication is also nuw. We match an extra add zero
-  // here, because that's what EmitGEPOffset() generates.
+  // then the final multiplication is also nuw.
   Instruction *I;
   if (IsNUW && !GEP2 && !Swapped && GEP1->isInBounds() &&
-      match(Result, m_Add(m_Instruction(I), m_Zero())) &&
-      I->getOpcode() == Instruction::Mul)
+      match(Result, m_Instruction(I)) && I->getOpcode() == Instruction::Mul)
     I->setHasNoUnsignedWrap();
 
   // If we had a constant expression GEP on the other side offsetting the
Index: llvm/include/llvm/Analysis/Utils/Local.h
===================================================================
--- llvm/include/llvm/Analysis/Utils/Local.h
+++ llvm/include/llvm/Analysis/Utils/Local.h
@@ -30,7 +30,7 @@
                      bool NoAssumptions = false) {
   GEPOperator *GEPOp = cast<GEPOperator>(GEP);
   Type *IntIdxTy = DL.getIndexType(GEP->getType());
-  Value *Result = Constant::getNullValue(IntIdxTy);
+  Value *Result = nullptr; // nullptr stands for zero.
 
   // If the GEP is inbounds, we know that none of the addressing operations will
   // overflow in a signed sense.
@@ -56,8 +56,11 @@
         Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
 
         if (Size)
-          Result = Builder->CreateAdd(Result, ConstantInt::get(IntIdxTy, Size),
-                                      GEP->getName().str()+".offs");
+          Result =
+              !Result
+                  ? ConstantInt::get(IntIdxTy, Size)
+                  : Builder->CreateAdd(Result, ConstantInt::get(IntIdxTy, Size),
+                                       GEP->getName().str() + ".offs");
         continue;
       }
 
@@ -71,7 +74,9 @@
       Scale =
           ConstantExpr::getMul(OC, Scale, false /*NUW*/, isInBounds /*NSW*/);
       // Emit an add instruction.
-      Result = Builder->CreateAdd(Result, Scale, GEP->getName().str()+".offs");
+      Result = !Result ? Scale
+                       : Builder->CreateAdd(Result, Scale,
+                                            GEP->getName().str() + ".offs");
       continue;
     }
 
@@ -91,9 +96,12 @@
     }
 
     // Emit an add instruction.
-    Result = Builder->CreateAdd(Op, Result, GEP->getName().str()+".offs");
+    Result = !Result ? Op
+                     : Builder->CreateAdd(Op, Result,
+                                          GEP->getName().str() + ".offs");
   }
-  return Result;
+
+  return Result ? Result : Constant::getNullValue(IntIdxTy);
 }
 
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90286.301181.patch
Type: text/x-patch
Size: 2890 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201028/c6885683/attachment.bin>


More information about the llvm-commits mailing list