[llvm] r288459 - ConstantFolding: Factor code into helper function

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 1 18:26:02 PST 2016


Author: arsenm
Date: Thu Dec  1 20:26:02 2016
New Revision: 288459

URL: http://llvm.org/viewvc/llvm-project?rev=288459&view=rev
Log:
ConstantFolding: Factor code into helper function

Modified:
    llvm/trunk/lib/Analysis/ConstantFolding.cpp

Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=288459&r1=288458&r2=288459&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Thu Dec  1 20:26:02 2016
@@ -58,7 +58,37 @@ namespace {
 // Constant Folding internal helper functions
 //===----------------------------------------------------------------------===//
 
-/// Constant fold bitcast, symbolically evaluating it with DataLayout.
+static Constant *foldConstVectorToAPInt(APInt &Result, Type *DestTy,
+                                        Constant *C, Type *SrcEltTy,
+                                        unsigned NumSrcElts,
+                                        const DataLayout &DL) {
+  // Now that we know that the input value is a vector of integers, just shift
+  // and insert them into our result.
+  unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy);
+  for (unsigned i = 0; i != NumSrcElts; ++i) {
+    Constant *Element;
+    if (DL.isLittleEndian())
+      Element = C->getAggregateElement(NumSrcElts - i - 1);
+    else
+      Element = C->getAggregateElement(i);
+
+    if (Element && isa<UndefValue>(Element)) {
+      Result <<= BitShift;
+      continue;
+    }
+
+    auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
+    if (!ElementCI)
+      return ConstantExpr::getBitCast(C, DestTy);
+
+    Result <<= BitShift;
+    Result |= ElementCI->getValue().zextOrSelf(Result.getBitWidth());
+  }
+
+  return nullptr;
+}
+
+// Constant fold bitcast, symbolically evaluating it with DataLayout.
 /// This always returns a non-null constant, but it may be a
 /// ConstantExpr if unfoldable.
 Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
@@ -88,29 +118,10 @@ Constant *FoldBitCast(Constant *C, Type
       C = ConstantExpr::getBitCast(C, SrcIVTy);
     }
 
-    // Now that we know that the input value is a vector of integers, just shift
-    // and insert them into our result.
-    unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy);
     APInt Result(IT->getBitWidth(), 0);
-    for (unsigned i = 0; i != NumSrcElts; ++i) {
-      Constant *Element;
-      if (DL.isLittleEndian())
-        Element = C->getAggregateElement(NumSrcElts-i-1);
-      else
-        Element = C->getAggregateElement(i);
-
-      if (Element && isa<UndefValue>(Element)) {
-        Result <<= BitShift;
-        continue;
-      }
-
-      auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
-      if (!ElementCI)
-        return ConstantExpr::getBitCast(C, DestTy);
-
-      Result <<= BitShift;
-      Result |= ElementCI->getValue().zextOrSelf(IT->getBitWidth());
-    }
+    if (Constant *CE = foldConstVectorToAPInt(Result, DestTy, C,
+                                              SrcEltTy, NumSrcElts, DL))
+      return CE;
 
     return ConstantInt::get(IT, Result);
   }




More information about the llvm-commits mailing list