Added constant folding support for the extractelement operation.
authorRobert Bocchino <bocchino@illinois.edu>
Tue, 10 Jan 2006 20:03:46 +0000 (20:03 +0000)
committerRobert Bocchino <bocchino@illinois.edu>
Tue, 10 Jan 2006 20:03:46 +0000 (20:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25187 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/ConstantFold.cpp
lib/VMCore/ConstantFold.h
lib/VMCore/ConstantFolding.h
lib/VMCore/Constants.cpp

index 40a51cc8901bdc5e8f312e0a92e4ada57f1e3d97..3f7bc7db33a9be871c2f9dbdb77c0be4a6e4640f 100644 (file)
@@ -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<ConstantPacked>(Val)) {
+    if (const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx)) {
+      return const_cast<Constant*>(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) {
index a864aa2cd80c08ab1cf40c716c78763ce939ac54..e8580c429051a5c94cfd0910852bdec44afb873d 100644 (file)
@@ -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,
index a864aa2cd80c08ab1cf40c716c78763ce939ac54..e8580c429051a5c94cfd0910852bdec44afb873d 100644 (file)
@@ -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,
index 610ed784c89a110ab751ebdc2c0a0effcea4aadb..f4ff31f24f21213076c1eab023d13dbd555ed7ef 100644 (file)
@@ -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<Constant*> ArgVec(1, Val);
   ArgVec.push_back(Idx);