DwarfWriter reading basic type information from llvm-gcc4 code.
[oota-llvm.git] / lib / VMCore / Constants.cpp
index 610ed784c89a110ab751ebdc2c0a0effcea4aadb..c8a5f356bc032d91cf36a09461feb2bd4fe37061 100644 (file)
@@ -347,8 +347,9 @@ public:
   }
 };
 
-/// ExtractElementConstantExpr - This class is private to Constants.cpp, and is used
-/// behind the scenes to implement extractelement constant exprs.
+/// ExtractElementConstantExpr - This class is private to
+/// Constants.cpp, and is used behind the scenes to implement
+/// extractelement constant exprs.
 class ExtractElementConstantExpr : public ConstantExpr {
   Use Ops[2];
 public:
@@ -360,6 +361,21 @@ public:
   }
 };
 
+/// InsertElementConstantExpr - This class is private to
+/// Constants.cpp, and is used behind the scenes to implement
+/// insertelement constant exprs.
+class InsertElementConstantExpr : public ConstantExpr {
+  Use Ops[3];
+public:
+  InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
+    : ConstantExpr(C1->getType(), Instruction::InsertElement, 
+                   Ops, 3) {
+    Ops[0].init(C1, this);
+    Ops[1].init(C2, this);
+    Ops[2].init(C3, this);
+  }
+};
+
 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
 /// used behind the scenes to implement getelementpr constant exprs.
 struct GetElementPtrConstantExpr : public ConstantExpr {
@@ -1156,6 +1172,9 @@ namespace llvm {
         return new SelectConstantExpr(V.second[0], V.second[1], V.second[2]);
       if (V.first == Instruction::ExtractElement)
         return new ExtractElementConstantExpr(V.second[0], V.second[1]);
+      if (V.first == Instruction::InsertElement)
+        return new InsertElementConstantExpr(V.second[0], V.second[1],
+                                             V.second[2]);
 
       assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
 
@@ -1403,6 +1422,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);
@@ -1414,11 +1435,35 @@ Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
   assert(isa<PackedType>(Val->getType()) &&
          "Tried to create extractelement operation on non-packed type!");
   assert(Idx->getType() == Type::UIntTy &&
-         "Index must be uint type!");
+         "Extractelement index must be uint type!");
   return getExtractElementTy(cast<PackedType>(Val->getType())->getElementType(),
                              Val, Idx);
 }
 
+Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
+                                           Constant *Elt, Constant *Idx) {
+  if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, 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(Elt);
+  ArgVec.push_back(Idx);
+  const ExprMapKeyType &Key = std::make_pair(Instruction::InsertElement,ArgVec);
+  return ExprConstants.getOrCreate(ReqTy, Key);
+}
+
+Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt, 
+                                         Constant *Idx) {
+  assert(isa<PackedType>(Val->getType()) &&
+         "Tried to create insertelement operation on non-packed type!");
+  assert(Elt->getType() == cast<PackedType>(Val->getType())->getElementType()
+         && "Insertelement types must match!");
+  assert(Idx->getType() == Type::UIntTy &&
+         "Insertelement index must be uint type!");
+  return getInsertElementTy(cast<PackedType>(Val->getType())->getElementType(),
+                            Val, Elt, Idx);
+}
+
 // destroyConstant - Remove the constant from the constant table...
 //
 void ConstantExpr::destroyConstant() {