From de7f1c9a63eca3f46538302c1feed6219225707f Mon Sep 17 00:00:00 2001 From: Robert Bocchino Date: Tue, 10 Jan 2006 20:03:46 +0000 Subject: [PATCH] Added constant folding support for the extractelement operation. llvm-svn: 25187 --- llvm/lib/VMCore/ConstantFolding.cpp | 10 ++++++++++ llvm/lib/VMCore/ConstantFolding.h | 2 ++ llvm/lib/VMCore/Constants.cpp | 2 ++ 3 files changed, 14 insertions(+) diff --git a/llvm/lib/VMCore/ConstantFolding.cpp b/llvm/lib/VMCore/ConstantFolding.cpp index 40a51cc8901b..3f7bc7db33a9 100644 --- a/llvm/lib/VMCore/ConstantFolding.cpp +++ b/llvm/lib/VMCore/ConstantFolding.cpp @@ -724,6 +724,16 @@ Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond, return 0; } +Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val, + const Constant *Idx) { + if (const ConstantPacked *CVal = dyn_cast(Val)) { + if (const ConstantUInt *CIdx = dyn_cast(Idx)) { + return const_cast(CVal->getOperand(CIdx->getValue())); + } + } + return 0; +} + /// isZeroSizedType - This type is zero sized if its an array or structure of /// zero sized types. The only leaf zero sized type is an empty structure. static bool isMaybeZeroSizedType(const Type *Ty) { diff --git a/llvm/lib/VMCore/ConstantFolding.h b/llvm/lib/VMCore/ConstantFolding.h index a864aa2cd80c..e8580c429051 100644 --- a/llvm/lib/VMCore/ConstantFolding.h +++ b/llvm/lib/VMCore/ConstantFolding.h @@ -31,6 +31,8 @@ namespace llvm { Constant *ConstantFoldSelectInstruction(const Constant *Cond, const Constant *V1, const Constant *V2); + Constant *ConstantFoldExtractElementInstruction(const Constant *Val, + const Constant *Idx); Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1, const Constant *V2); Constant *ConstantFoldGetElementPtr(const Constant *C, diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index 610ed784c89a..f4ff31f24f21 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -1403,6 +1403,8 @@ Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val, Constant *Idx) { + if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx)) + return FC; // Fold a few common cases... // Look up the constant in the table first to ensure uniqueness std::vector ArgVec(1, Val); ArgVec.push_back(Idx);