Implement review feedback for the ConstantBool->ConstantInt merge. Chris
[oota-llvm.git] / lib / Transforms / IPO / GlobalOpt.cpp
index e17ded82d92fc3d985ee0c0dc24320430635b491..86657c2c0e51c0e8e9f1cf0992263ee3e869aec2 100644 (file)
@@ -710,8 +710,8 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
   // If there is a comparison against null, we will insert a global bool to
   // keep track of whether the global was initialized yet or not.
   GlobalVariable *InitBool =
-    new GlobalVariable(Type::BoolTy, false, GlobalValue::InternalLinkage,
-                       ConstantBool::getFalse(), GV->getName()+".init");
+    new GlobalVariable(Type::Int1Ty, false, GlobalValue::InternalLinkage,
+                       ConstantInt::getFalse(), GV->getName()+".init");
   bool InitBoolUsed = false;
 
   // Loop over all uses of GV, processing them in turn.
@@ -731,7 +731,7 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
           default: assert(0 && "Unknown ICmp Predicate!");
           case ICmpInst::ICMP_ULT:
           case ICmpInst::ICMP_SLT:
-            LV = ConstantBool::getFalse();   // X < null -> always false
+            LV = ConstantInt::getFalse();   // X < null -> always false
             break;
           case ICmpInst::ICMP_ULE:
           case ICmpInst::ICMP_SLE:
@@ -753,7 +753,7 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
     } else {
       StoreInst *SI = cast<StoreInst>(GV->use_back());
       // The global is initialized when the store to it occurs.
-      new StoreInst(ConstantBool::getTrue(), InitBool, SI);
+      new StoreInst(ConstantInt::getTrue(), InitBool, SI);
       SI->eraseFromParent();
     }
 
@@ -1016,9 +1016,25 @@ static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, MallocInst *MI){
   // loads, and all uses of those loads are simple.  Rewrite them to use loads
   // of the per-field globals instead.
   while (!GV->use_empty()) {
-    LoadInst *LI = cast<LoadInst>(GV->use_back());
-    RewriteUsesOfLoadForHeapSRoA(LI, FieldGlobals);
-    LI->eraseFromParent();
+    if (LoadInst *LI = dyn_cast<LoadInst>(GV->use_back())) {
+      RewriteUsesOfLoadForHeapSRoA(LI, FieldGlobals);
+      LI->eraseFromParent();
+    } else {
+      // Must be a store of null.
+      StoreInst *SI = cast<StoreInst>(GV->use_back());
+      assert(isa<Constant>(SI->getOperand(0)) &&
+             cast<Constant>(SI->getOperand(0))->isNullValue() &&
+             "Unexpected heap-sra user!");
+      
+      // Insert a store of null into each global.
+      for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
+        Constant *Null = 
+          Constant::getNullValue(FieldGlobals[i]->getType()->getElementType());
+        new StoreInst(Null, FieldGlobals[i], SI);
+      }
+      // Erase the original store.
+      SI->eraseFromParent();
+    }
   }
 
   // The old global is now dead, remove it.
@@ -1123,13 +1139,13 @@ static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
 /// values ever stored into GV are its initializer and OtherVal.
 static void ShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
   // Create the new global, initializing it to false.
-  GlobalVariable *NewGV = new GlobalVariable(Type::BoolTy, false,
-         GlobalValue::InternalLinkage, ConstantBool::getFalse(),
+  GlobalVariable *NewGV = new GlobalVariable(Type::Int1Ty, false,
+         GlobalValue::InternalLinkage, ConstantInt::getFalse(),
                                              GV->getName()+".b");
   GV->getParent()->getGlobalList().insert(GV, NewGV);
 
   Constant *InitVal = GV->getInitializer();
-  assert(InitVal->getType() != Type::BoolTy && "No reason to shrink to bool!");
+  assert(InitVal->getType() != Type::Int1Ty && "No reason to shrink to bool!");
 
   // If initialized to zero and storing one into the global, we can use a cast
   // instead of a select to synthesize the desired value.
@@ -1145,7 +1161,7 @@ static void ShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
       // Only do this if we weren't storing a loaded value.
       Value *StoreVal;
       if (StoringOther || SI->getOperand(0) == InitVal)
-        StoreVal = ConstantBool::get(StoringOther);
+        StoreVal = ConstantInt::get(Type::Int1Ty, StoringOther);
       else {
         // Otherwise, we are storing a previously loaded copy.  To do this,
         // change the copy from copying the original value to just copying the
@@ -1325,7 +1341,7 @@ bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
       // Otherwise, if the global was not a boolean, we can shrink it to be a
       // boolean.
       if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
-        if (GV->getType()->getElementType() != Type::BoolTy &&
+        if (GV->getType()->getElementType() != Type::Int1Ty &&
             !GV->getType()->getElementType()->isFloatingPoint() &&
             !GS.HasPHIUser) {
           DOUT << "   *** SHRINKING TO BOOL: " << *GV;
@@ -1781,10 +1797,13 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,
         if (BI->isUnconditional()) {
           NewBB = BI->getSuccessor(0);
         } else {
-          ConstantBool *Cond =
-            dyn_cast<ConstantBool>(getVal(Values, BI->getCondition()));
-          if (!Cond) return false;  // Cannot determine.
-          NewBB = BI->getSuccessor(!Cond->getValue());          
+          ConstantInt *Cond =
+            dyn_cast<ConstantInt>(getVal(Values, BI->getCondition()));
+
+          // Cannot determine.
+          if (!Cond || Cond->getType() != Type::Int1Ty) 
+            return false;  
+          NewBB = BI->getSuccessor(!Cond->getZExtValue());          
         }
       } else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) {
         ConstantInt *Val =