summaryrefslogtreecommitdiff
path: root/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorMatt Arsenault <Matthew.Arsenault@amd.com>2016-12-02 02:26:02 +0000
committerMatt Arsenault <Matthew.Arsenault@amd.com>2016-12-02 02:26:02 +0000
commit749e583e8c08c8954bf0fd82df02079b82ae5444 (patch)
treea899e5cb1b0875b29f79c16f2f4c73d18f10e063 /lib/Analysis/ConstantFolding.cpp
parent06115803f95a8a104403c3ebba317fb2f778db5b (diff)
ConstantFolding: Factor code into helper function
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288459 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis/ConstantFolding.cpp')
-rw-r--r--lib/Analysis/ConstantFolding.cpp57
1 files changed, 34 insertions, 23 deletions
diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp
index 500faed39b1..04d36ffbb7e 100644
--- a/lib/Analysis/ConstantFolding.cpp
+++ b/lib/Analysis/ConstantFolding.cpp
@@ -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 *DestTy, const DataLayout &DL) {
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);
}