[PATCH] D14596: [SROA] Choose more profitable type in findCommonType

Guozhi Wei via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 11 16:26:45 PST 2015


Carrot created this revision.
Carrot added a subscriber: llvm-commits.

This patch fix PR25342.

Current implementation of findCommonType always chooses integer type if there are multiple different types for a field. It may hurt performance if the integer type is actually rarely used. This patch changes the function to choose the most frequently used type.

http://reviews.llvm.org/D14596

Files:
  lib/Transforms/Scalar/SROA.cpp

Index: lib/Transforms/Scalar/SROA.cpp
===================================================================
--- lib/Transforms/Scalar/SROA.cpp
+++ lib/Transforms/Scalar/SROA.cpp
@@ -1073,9 +1073,7 @@
 static Type *findCommonType(AllocaSlices::const_iterator B,
                             AllocaSlices::const_iterator E,
                             uint64_t EndOffset) {
-  Type *Ty = nullptr;
-  bool TyIsCommon = true;
-  IntegerType *ITy = nullptr;
+  SmallDenseMap<Type *, int> TypeCounters;
 
   // Note that we need to look at *every* alloca slice's Use to ensure we
   // always get consistent results regardless of the order of slices.
@@ -1101,22 +1099,30 @@
       if (UserITy->getBitWidth() % 8 != 0 ||
           UserITy->getBitWidth() / 8 > (EndOffset - B->beginOffset()))
         continue;
-
-      // Track the largest bitwidth integer type used in this way in case there
-      // is no common type.
-      if (!ITy || ITy->getBitWidth() < UserITy->getBitWidth())
-        ITy = UserITy;
     }
 
-    // To avoid depending on the order of slices, Ty and TyIsCommon must not
-    // depend on types skipped above.
-    if (!UserTy || (Ty && Ty != UserTy))
-      TyIsCommon = false; // Give up on anything but an iN type.
-    else
-      Ty = UserTy;
+    if (UserTy)
+      ++TypeCounters[UserTy];
+  }
+
+  // Look for the type that is most frequently used.
+  Type *Ty = nullptr;
+  int max_counter = 0;
+  for (auto I = TypeCounters.begin(); I != TypeCounters.end(); ++I) {
+    IntegerType *NewITy = dyn_cast_or_null<IntegerType>(I->getFirst());
+    if (I->getSecond() > max_counter) {
+      Ty = I->getFirst();
+      max_counter = I->getSecond();
+    } else if (I->getSecond() == max_counter && NewITy) {
+      IntegerType *MaxITy = dyn_cast_or_null<IntegerType>(Ty);
+      if (!MaxITy || MaxITy->getBitWidth() < NewITy->getBitWidth()) {
+        Ty = I->getFirst();
+        max_counter = I->getSecond();
+      }
+    }
   }
 
-  return TyIsCommon ? Ty : ITy;
+  return Ty;
 }
 
 /// PHI instructions that use an alloca and are subsequently loaded can be


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14596.39976.patch
Type: text/x-patch
Size: 2087 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151112/2219fa87/attachment.bin>


More information about the llvm-commits mailing list