[llvm-commits] CVS: llvm/lib/VMCore/ConstantFolding.cpp ConstantFolding.h Constants.cpp Instructions.cpp

Chris Lattner sabre at nondot.org
Tue Jan 30 20:40:44 PST 2007



Changes in directory llvm/lib/VMCore:

ConstantFolding.cpp updated: 1.133 -> 1.134
ConstantFolding.h updated: 1.52 -> 1.53
Constants.cpp updated: 1.205 -> 1.206
Instructions.cpp updated: 1.66 -> 1.67
---
Log message:

Revise APIs for creating constantexpr GEPs to not require the use of vectors.  
This allows us to eliminate many temporary vectors, and theirassociated malloc/free pairs.



---
Diffs of the changes:  (+47 -46)

 ConstantFolding.cpp |   37 +++++++++++++++++++++----------------
 ConstantFolding.h   |    4 +---
 Constants.cpp       |   41 +++++++++++++++++++----------------------
 Instructions.cpp    |   11 ++++++-----
 4 files changed, 47 insertions(+), 46 deletions(-)


Index: llvm/lib/VMCore/ConstantFolding.cpp
diff -u llvm/lib/VMCore/ConstantFolding.cpp:1.133 llvm/lib/VMCore/ConstantFolding.cpp:1.134
--- llvm/lib/VMCore/ConstantFolding.cpp:1.133	Fri Jan 19 15:13:56 2007
+++ llvm/lib/VMCore/ConstantFolding.cpp	Tue Jan 30 22:40:28 2007
@@ -23,6 +23,7 @@
 #include "llvm/Instructions.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
 #include "llvm/Support/ManagedStatic.h"
@@ -216,7 +217,7 @@
     // the first element.  If so, return the appropriate GEP instruction.
     if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
       if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
-        std::vector<Value*> IdxList;
+        SmallVector<Value*, 8> IdxList;
         IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
         const Type *ElTy = PTy->getElementType();
         while (ElTy != DPTy->getElementType()) {
@@ -236,7 +237,7 @@
 
         if (ElTy == DPTy->getElementType())
           return ConstantExpr::getGetElementPtr(
-              const_cast<Constant*>(V),IdxList);
+              const_cast<Constant*>(V), &IdxList[0], IdxList.size());
       }
         
     // Handle casts from one packed constant to another.  We know that the src 
@@ -1290,28 +1291,31 @@
 }
 
 Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
-                                          const std::vector<Value*> &IdxList) {
-  if (IdxList.size() == 0 ||
-      (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
+                                          Constant* const *Idxs, 
+                                          unsigned NumIdx) {
+  if (NumIdx == 0 ||
+      (NumIdx == 1 && Idxs[0]->isNullValue()))
     return const_cast<Constant*>(C);
 
   if (isa<UndefValue>(C)) {
-    const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
+    const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
+                                                       (Value**)Idxs, NumIdx,
                                                        true);
     assert(Ty != 0 && "Invalid indices for GEP!");
     return UndefValue::get(PointerType::get(Ty));
   }
 
-  Constant *Idx0 = cast<Constant>(IdxList[0]);
+  Constant *Idx0 = Idxs[0];
   if (C->isNullValue()) {
     bool isNull = true;
-    for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
-      if (!cast<Constant>(IdxList[i])->isNullValue()) {
+    for (unsigned i = 0, e = NumIdx; i != e; ++i)
+      if (!Idxs[i]->isNullValue()) {
         isNull = false;
         break;
       }
     if (isNull) {
-      const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
+      const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
+                                                         (Value**)Idxs, NumIdx,
                                                          true);
       assert(Ty != 0 && "Invalid indices for GEP!");
       return ConstantPointerNull::get(PointerType::get(Ty));
@@ -1330,8 +1334,8 @@
         LastTy = *I;
 
       if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
-        std::vector<Value*> NewIndices;
-        NewIndices.reserve(IdxList.size() + CE->getNumOperands());
+        SmallVector<Value*, 16> NewIndices;
+        NewIndices.reserve(NumIdx + CE->getNumOperands());
         for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
           NewIndices.push_back(CE->getOperand(i));
 
@@ -1353,8 +1357,9 @@
         }
 
         NewIndices.push_back(Combined);
-        NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
-        return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
+        NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
+        return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0],
+                                              NewIndices.size());
       }
     }
 
@@ -1363,7 +1368,7 @@
     //                        long 0, long 0)
     // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
     //
-    if (CE->isCast() && IdxList.size() > 1 && Idx0->isNullValue())
+    if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue())
       if (const PointerType *SPT =
           dyn_cast<PointerType>(CE->getOperand(0)->getType()))
         if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
@@ -1371,7 +1376,7 @@
         dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
             if (CAT->getElementType() == SAT->getElementType())
               return ConstantExpr::getGetElementPtr(
-                      (Constant*)CE->getOperand(0), IdxList);
+                      (Constant*)CE->getOperand(0), Idxs, NumIdx);
   }
   return 0;
 }


Index: llvm/lib/VMCore/ConstantFolding.h
diff -u llvm/lib/VMCore/ConstantFolding.h:1.52 llvm/lib/VMCore/ConstantFolding.h:1.53
--- llvm/lib/VMCore/ConstantFolding.h:1.52	Sun Dec 24 12:52:08 2006
+++ llvm/lib/VMCore/ConstantFolding.h	Tue Jan 30 22:40:28 2007
@@ -19,8 +19,6 @@
 #ifndef CONSTANTFOLDING_H
 #define CONSTANTFOLDING_H
 
-#include <vector>
-
 namespace llvm {
   class Value;
   class Constant;
@@ -49,7 +47,7 @@
                                            const Constant *C1, 
                                            const Constant *C2);
   Constant *ConstantFoldGetElementPtr(const Constant *C,
-                                      const std::vector<Value*> &IdxList);
+                                      Constant* const *Idxs, unsigned NumIdx);
 } // End llvm namespace
 
 #endif


Index: llvm/lib/VMCore/Constants.cpp
diff -u llvm/lib/VMCore/Constants.cpp:1.205 llvm/lib/VMCore/Constants.cpp:1.206
--- llvm/lib/VMCore/Constants.cpp:1.205	Fri Jan 26 01:37:34 2007
+++ llvm/lib/VMCore/Constants.cpp	Tue Jan 30 22:40:28 2007
@@ -1377,7 +1377,8 @@
       case Instruction::GetElementPtr:
         // Make everyone now use a constant of the new type...
         std::vector<Value*> Idx(OldC->op_begin()+1, OldC->op_end());
-        New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), Idx);
+        New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0),
+                                               &Idx[0], Idx.size());
         break;
       }
 
@@ -1744,45 +1745,41 @@
 }
 
 Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
-                                           const std::vector<Value*> &IdxList) {
-  assert(GetElementPtrInst::getIndexedType(C->getType(), IdxList, true) &&
+                                           Value* const *Idxs,
+                                           unsigned NumIdx) {
+  assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true) &&
          "GEP indices invalid!");
 
-  if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
+  if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
     return FC;          // Fold a few common cases...
 
   assert(isa<PointerType>(C->getType()) &&
          "Non-pointer type for constant GetElementPtr expression");
   // Look up the constant in the table first to ensure uniqueness
   std::vector<Constant*> ArgVec;
-  ArgVec.reserve(IdxList.size()+1);
+  ArgVec.reserve(NumIdx+1);
   ArgVec.push_back(C);
-  for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
-    ArgVec.push_back(cast<Constant>(IdxList[i]));
-  const ExprMapKeyType Key(Instruction::GetElementPtr,ArgVec);
+  for (unsigned i = 0; i != NumIdx; ++i)
+    ArgVec.push_back(cast<Constant>(Idxs[i]));
+  const ExprMapKeyType Key(Instruction::GetElementPtr, ArgVec);
   return ExprConstants->getOrCreate(ReqTy, Key);
 }
 
-Constant *ConstantExpr::getGetElementPtr(Constant *C,
-                                         const std::vector<Constant*> &IdxList){
+Constant *ConstantExpr::getGetElementPtr(Constant *C, Value* const *Idxs,
+                                         unsigned NumIdx) {
   // Get the result type of the getelementptr!
-  std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
-
-  const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
-                                                     true);
+  const Type *Ty = 
+    GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true);
   assert(Ty && "GEP indices invalid!");
-  return getGetElementPtrTy(PointerType::get(Ty), C, VIdxList);
+  return getGetElementPtrTy(PointerType::get(Ty), C, Idxs, NumIdx);
 }
 
-Constant *ConstantExpr::getGetElementPtr(Constant *C,
-                                         const std::vector<Value*> &IdxList) {
-  // Get the result type of the getelementptr!
-  const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
-                                                     true);
-  assert(Ty && "GEP indices invalid!");
-  return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
+Constant *ConstantExpr::getGetElementPtr(Constant *C, Constant* const *Idxs,
+                                         unsigned NumIdx) {
+  return getGetElementPtr(C, (Value* const *)Idxs, NumIdx);
 }
 
+
 Constant *
 ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
   assert(LHS->getType() == RHS->getType());


Index: llvm/lib/VMCore/Instructions.cpp
diff -u llvm/lib/VMCore/Instructions.cpp:1.66 llvm/lib/VMCore/Instructions.cpp:1.67
--- llvm/lib/VMCore/Instructions.cpp:1.66	Fri Jan 26 00:30:34 2007
+++ llvm/lib/VMCore/Instructions.cpp	Tue Jan 30 22:40:28 2007
@@ -765,12 +765,13 @@
 // pointer type.
 //
 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
-                                              const std::vector<Value*> &Idx,
+                                              Value* const *Idxs,
+                                              unsigned NumIdx,
                                               bool AllowCompositeLeaf) {
   if (!isa<PointerType>(Ptr)) return 0;   // Type isn't a pointer type!
 
   // Handle the special case of the empty set index set...
-  if (Idx.empty())
+  if (NumIdx == 0)
     if (AllowCompositeLeaf ||
         cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
       return cast<PointerType>(Ptr)->getElementType();
@@ -779,12 +780,12 @@
 
   unsigned CurIdx = 0;
   while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
-    if (Idx.size() == CurIdx) {
+    if (NumIdx == CurIdx) {
       if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
       return 0;   // Can't load a whole structure or array!?!?
     }
 
-    Value *Index = Idx[CurIdx++];
+    Value *Index = Idxs[CurIdx++];
     if (isa<PointerType>(CT) && CurIdx != 1)
       return 0;  // Can only index into pointer types at the first index!
     if (!CT->indexValid(Index)) return 0;
@@ -798,7 +799,7 @@
       Ptr = Ty;
     }
   }
-  return CurIdx == Idx.size() ? Ptr : 0;
+  return CurIdx == NumIdx ? Ptr : 0;
 }
 
 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,






More information about the llvm-commits mailing list