diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 40a51cc8901..3f7bc7db33a 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.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/lib/VMCore/ConstantFold.h b/lib/VMCore/ConstantFold.h index a864aa2cd80..e8580c42905 100644 --- a/lib/VMCore/ConstantFold.h +++ b/lib/VMCore/ConstantFold.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/lib/VMCore/ConstantFolding.h b/lib/VMCore/ConstantFolding.h index a864aa2cd80..e8580c42905 100644 --- a/lib/VMCore/ConstantFolding.h +++ b/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/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 610ed784c89..f4ff31f24f2 100644 --- a/lib/VMCore/Constants.cpp +++ b/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);