Optimize load from aggregate stores

deadal nix deadalnix at gmail.com
Thu Feb 6 22:52:57 PST 2014


This is patch to fix http://llvm.org/bugs/show_bug.cgi?id=16316

I have some test cases, but I'm not sure how the LLVM test suite work and
what is the right way to add them. I'll need some directions here to
complete the diff.Anyway, the code :

diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp
index 955ea1b..fa15697 100644
--- a/lib/Transforms/Scalar/GVN.cpp
+++ b/lib/Transforms/Scalar/GVN.cpp
@@ -1015,11 +1015,6 @@ static int AnalyzeLoadFromClobberingWrite(Type
*LoadTy, Value *LoadPtr,
 static int AnalyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
                                           StoreInst *DepSI,
                                           const DataLayout &TD) {
-  // Cannot handle reading from store of first-class aggregate yet.
-  if (DepSI->getValueOperand()->getType()->isStructTy() ||
-      DepSI->getValueOperand()->getType()->isArrayTy())
-    return -1;
-
   Value *StorePtr = DepSI->getPointerOperand();
   uint64_t StoreSize
=TD.getTypeSizeInBits(DepSI->getValueOperand()->getType());
   return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr,
@@ -1117,6 +1112,25 @@ static Value *GetStoreValueForLoad(Value *SrcVal,
unsigned Offset,

   IRBuilder<> Builder(InsertPt->getParent(), InsertPt);

+  // When facing an Aggregate, we have to find the element to fetch from.
+  while (SrcVal->getType()->isAggregateType()) {
+    if (StructType *ST = dyn_cast<StructType>(SrcVal->getType())) {
+      const StructLayout &Layout = *TD.getStructLayout(ST);
+      unsigned i = 0;
+      for (unsigned cd = 0, e = ST->getNumElements(); cd != e; ++cd) {
+        if (Layout.getElementOffsetInBits(cd) > Offset) break;
+        i = cd;
+      }
+
+      SrcVal = Builder.CreateExtractValue(SrcVal, i);
+      Offset -= Layout.getElementOffsetInBits(i);
+    } else if (ArrayType *AT = dyn_cast<ArrayType>(SrcVal->getType())) {
+      uint64_t EltSize = TD.getTypeAllocSizeInBits(AT->getElementType());
+      SrcVal = Builder.CreateExtractValue(SrcVal, Offset / EltSize);
+      Offset %= EltSize;
+    }
+  }
+
   // Compute which bits of the stored value are being used by the load.
Convert
   // to an integer type to start with.
   if (SrcVal->getType()->getScalarType()->isPointerTy())

Nothing super fancy, but you got to strat somewhere !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140206/682be4b8/attachment.html>


More information about the llvm-commits mailing list