[llvm] 02678bc - [Local] Merge constant / non-constant code paths (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 11 07:27:42 PDT 2023


Author: Nikita Popov
Date: 2023-10-11T16:27:32+02:00
New Revision: 02678bc91afbaccfc803d56cec0af0e32cad1bd2

URL: https://github.com/llvm/llvm-project/commit/02678bc91afbaccfc803d56cec0af0e32cad1bd2
DIFF: https://github.com/llvm/llvm-project/commit/02678bc91afbaccfc803d56cec0af0e32cad1bd2.diff

LOG: [Local] Merge constant / non-constant code paths (NFC)

Let IRBuilder perform the constant folding. Avoids some uses of
ConstantExpr.

Added: 
    

Modified: 
    llvm/lib/Analysis/Local.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/Local.cpp b/llvm/lib/Analysis/Local.cpp
index 5d558de516d3500..672e6a190a57ff8 100644
--- a/llvm/lib/Analysis/Local.cpp
+++ b/llvm/lib/Analysis/Local.cpp
@@ -28,6 +28,13 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
   // If the GEP is inbounds, we know that none of the addressing operations will
   // overflow in a signed sense.
   bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
+  auto AddOffset = [&](Value *Offset) {
+    if (Result)
+      Result = Builder->CreateAdd(Result, Offset, GEP->getName() + ".offs",
+                                  false /*NUW*/, isInBounds /*NSW*/);
+    else
+      Result = Offset;
+  };
 
   // Build a mask for high order bits.
   unsigned IntPtrWidth = IntIdxTy->getScalarType()->getIntegerBitWidth();
@@ -39,7 +46,6 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
        ++i, ++GTI) {
     Value *Op = *i;
     uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
-    Value *Offset;
     if (Constant *OpC = dyn_cast<Constant>(Op)) {
       if (OpC->isZeroValue())
         continue;
@@ -51,42 +57,26 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
         if (!Size)
           continue;
 
-        Offset = ConstantInt::get(IntIdxTy, Size);
-      } else {
-        // Splat the constant if needed.
-        if (IntIdxTy->isVectorTy() && !OpC->getType()->isVectorTy())
-          OpC = ConstantVector::getSplat(
-              cast<VectorType>(IntIdxTy)->getElementCount(), OpC);
-
-        Constant *Scale = ConstantInt::get(IntIdxTy, Size);
-        Constant *OC =
-            ConstantExpr::getIntegerCast(OpC, IntIdxTy, true /*SExt*/);
-        Offset =
-            ConstantExpr::getMul(OC, Scale, false /*NUW*/, isInBounds /*NSW*/);
-      }
-    } else {
-      // Splat the index if needed.
-      if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy())
-        Op = Builder->CreateVectorSplat(
-            cast<FixedVectorType>(IntIdxTy)->getNumElements(), Op);
-
-      // Convert to correct type.
-      if (Op->getType() != IntIdxTy)
-        Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c");
-      if (Size != 1) {
-        // We'll let instcombine(mul) convert this to a shl if possible.
-        Op = Builder->CreateMul(Op, ConstantInt::get(IntIdxTy, Size),
-                                GEP->getName() + ".idx", false /*NUW*/,
-                                isInBounds /*NSW*/);
+        AddOffset(ConstantInt::get(IntIdxTy, Size));
+        continue;
       }
-      Offset = Op;
     }
 
-    if (Result)
-      Result = Builder->CreateAdd(Result, Offset, GEP->getName() + ".offs",
-                                  false /*NUW*/, isInBounds /*NSW*/);
-    else
-      Result = Offset;
+    // Splat the index if needed.
+    if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy())
+      Op = Builder->CreateVectorSplat(
+          cast<FixedVectorType>(IntIdxTy)->getNumElements(), Op);
+
+    // Convert to correct type.
+    if (Op->getType() != IntIdxTy)
+      Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c");
+    if (Size != 1) {
+      // We'll let instcombine(mul) convert this to a shl if possible.
+      Op = Builder->CreateMul(Op, ConstantInt::get(IntIdxTy, Size),
+                              GEP->getName() + ".idx", false /*NUW*/,
+                              isInBounds /*NSW*/);
+    }
+    AddOffset(Op);
   }
   return Result ? Result : Constant::getNullValue(IntIdxTy);
 }


        


More information about the llvm-commits mailing list