Add missing newlines at EOF (for clang++).
[oota-llvm.git] / lib / Transforms / Scalar / SCCP.cpp
index 05a0eeef2d3701387a7a9fe5d00be34d2b97f6f5..d8c59b1d7421e9f81c3775f5f109970cfa2122da 100644 (file)
@@ -370,13 +370,13 @@ private:
   /// by properly seeding constants etc.
   LatticeVal &getValueState(Value *V) {
     assert(!isa<StructType>(V->getType()) && "Should use getStructValueState");
-    
-    // TODO: Change to do insert+find in one operation.
-    DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(V);
-    if (I != ValueState.end())
-      return I->second;  // Common case, already in the map.
 
-    LatticeVal &LV = ValueState[V];
+    std::pair<DenseMap<Value*, LatticeVal>::iterator, bool> I =
+      ValueState.insert(std::make_pair(V, LatticeVal()));
+    LatticeVal &LV = I.first->second;
+
+    if (!I.second)
+      return LV;  // Common case, already in the map.
 
     if (Constant *C = dyn_cast<Constant>(V)) {
       // Undef values remain undefined.
@@ -395,15 +395,15 @@ private:
     assert(isa<StructType>(V->getType()) && "Should use getValueState");
     assert(i < cast<StructType>(V->getType())->getNumElements() &&
            "Invalid element #");
-    
-    // TODO: Change to do insert+find in one operation.
-    DenseMap<std::pair<Value*, unsigned>, LatticeVal>::iterator
-      I = StructValueState.find(std::make_pair(V, i));
-    if (I != StructValueState.end())
-      return I->second;  // Common case, already in the map.
-    
-    LatticeVal &LV = StructValueState[std::make_pair(V, i)];
-    
+
+    std::pair<DenseMap<std::pair<Value*, unsigned>, LatticeVal>::iterator,
+              bool> I = StructValueState.insert(
+                        std::make_pair(std::make_pair(V, i), LatticeVal()));
+    LatticeVal &LV = I.first->second;
+
+    if (!I.second)
+      return LV;  // Common case, already in the map.
+
     if (Constant *C = dyn_cast<Constant>(V)) {
       if (isa<UndefValue>(C))
         ; // Undef values remain undefined.
@@ -795,9 +795,14 @@ void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
     return markOverdefined(&EVI);
 
   Value *AggVal = EVI.getAggregateOperand();
-  unsigned i = *EVI.idx_begin();
-  LatticeVal EltVal = getStructValueState(AggVal, i);
-  mergeInValue(getValueState(&EVI), &EVI, EltVal);
+  if (isa<StructType>(AggVal->getType())) {
+    unsigned i = *EVI.idx_begin();
+    LatticeVal EltVal = getStructValueState(AggVal, i);
+    mergeInValue(getValueState(&EVI), &EVI, EltVal);
+  } else {
+    // Otherwise, must be extracting from an array.
+    return markOverdefined(&EVI);
+  }
 }
 
 void SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) {
@@ -1280,9 +1285,10 @@ CallOverdefined:
       }
       
       if (const StructType *STy = dyn_cast<StructType>(AI->getType())) {
-        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
-          mergeInValue(getStructValueState(AI, i), AI,
-                       getStructValueState(*CAI, i));
+        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
+          LatticeVal CallArg = getStructValueState(*CAI, i);
+          mergeInValue(getStructValueState(AI, i), AI, CallArg);
+        }
       } else {
         mergeInValue(AI, getValueState(*CAI));
       }
@@ -1863,8 +1869,16 @@ bool IPSCCP::runOnModule(Module &M) {
     for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
       // If there are any PHI nodes in this successor, drop entries for BB now.
       BasicBlock *DeadBB = BlocksToErase[i];
-      while (!DeadBB->use_empty()) {
-        Instruction *I = cast<Instruction>(DeadBB->use_back());
+      for (Value::use_iterator UI = DeadBB->use_begin(), UE = DeadBB->use_end();
+           UI != UE; ) {
+        // Grab the user and then increment the iterator early, as the user
+        // will be deleted. Step past all adjacent uses from the same user.
+        Instruction *I = dyn_cast<Instruction>(*UI);
+        do { ++UI; } while (UI != UE && *UI == I);
+
+        // Ignore blockaddress users; BasicBlock's dtor will handle them.
+        if (!I) continue;
+
         bool Folded = ConstantFoldTerminator(I->getParent());
         if (!Folded) {
           // The constant folder may not have been able to fold the terminator