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

Chris Lattner lattner at cs.uiuc.edu
Tue Oct 4 11:47:21 PDT 2005



Changes in directory llvm/lib/VMCore:

Constants.cpp updated: 1.136 -> 1.137
---
Log message:

Minor speedup to avoid array searches given a Use*.  This speeds up bc reading
of the python test from 1:00 to 54s.


---
Diffs of the changes:  (+39 -23)

 Constants.cpp |   62 ++++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 39 insertions(+), 23 deletions(-)


Index: llvm/lib/VMCore/Constants.cpp
diff -u llvm/lib/VMCore/Constants.cpp:1.136 llvm/lib/VMCore/Constants.cpp:1.137
--- llvm/lib/VMCore/Constants.cpp:1.136	Tue Oct  4 13:13:04 2005
+++ llvm/lib/VMCore/Constants.cpp	Tue Oct  4 13:47:09 2005
@@ -1390,22 +1390,32 @@
                                                 Use *U) {
   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
   Constant *ToC = cast<Constant>(To);
-  
+
+  unsigned OperandToUpdate = U-OperandList;
+  assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
+
   std::pair<ArrayConstantsTy::MapKey, ConstantArray*> Lookup;
   Lookup.first.first = getType();
   Lookup.second = this;
+
   std::vector<Constant*> &Values = Lookup.first.second;
   Values.reserve(getNumOperands());  // Build replacement array.
-  
+
   // Fill values with the modified operands of the constant array.  Also, 
   // compute whether this turns into an all-zeros array.
-  bool isAllZeros = ToC->isNullValue();
-  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
-    Constant *Val = getOperand(i);
-    if (Val == From) Val = ToC;
-    Values.push_back(Val);
-    if (isAllZeros) isAllZeros = Val->isNullValue();
+  bool isAllZeros = false;
+  if (!ToC->isNullValue()) {
+    for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
+      Values.push_back(cast<Constant>(O->get()));
+  } else {
+    isAllZeros = true;
+    for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
+      Constant *Val = cast<Constant>(O->get());
+      Values.push_back(Val);
+      if (isAllZeros) isAllZeros = Val->isNullValue();
+    }
   }
+  Values[OperandToUpdate] = ToC;
   
   Constant *Replacement = 0;
   if (isAllZeros) {
@@ -1429,10 +1439,8 @@
       // located at descriptor I.
       ArrayConstants.UpdateInverseMap(this, I);
       
-      // Update to the new values.
-      for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
-        if (getOperand(i) == From)
-          setOperand(i, ToC);
+      // Update to the new value.
+      setOperand(OperandToUpdate, ToC);
       return;
     }
   }
@@ -1452,22 +1460,32 @@
   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
   Constant *ToC = cast<Constant>(To);
 
+  unsigned OperandToUpdate = U-OperandList;
+  assert(getOperand(OperandToUpdate) == From && "ReplaceAllUsesWith broken!");
+
   std::pair<StructConstantsTy::MapKey, ConstantStruct*> Lookup;
   Lookup.first.first = getType();
   Lookup.second = this;
   std::vector<Constant*> &Values = Lookup.first.second;
   Values.reserve(getNumOperands());  // Build replacement struct.
   
+  
   // Fill values with the modified operands of the constant struct.  Also, 
   // compute whether this turns into an all-zeros struct.
-  bool isAllZeros = ToC->isNullValue();
-  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
-    Constant *Val = getOperand(i);
-    if (Val == From) Val = ToC;
-    Values.push_back(Val);
-    if (isAllZeros) isAllZeros = Val->isNullValue();
+  bool isAllZeros = false;
+  if (!ToC->isNullValue()) {
+    for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O)
+      Values.push_back(cast<Constant>(O->get()));
+  } else {
+    isAllZeros = true;
+    for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
+      Constant *Val = cast<Constant>(O->get());
+      Values.push_back(Val);
+      if (isAllZeros) isAllZeros = Val->isNullValue();
+    }
   }
-    
+  Values[OperandToUpdate] = ToC;
+  
   Constant *Replacement = 0;
   if (isAllZeros) {
     Replacement = ConstantAggregateZero::get(getType());
@@ -1490,10 +1508,8 @@
       // located at descriptor I.
       StructConstants.UpdateInverseMap(this, I);
       
-      // Update to the new values.
-      for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
-        if (getOperand(i) == From)
-          setOperand(i, ToC);
+      // Update to the new value.
+      setOperand(OperandToUpdate, ToC);
       return;
     }
   }






More information about the llvm-commits mailing list