Moved Inliner.h to include/llvm/Transforms/IPO/InlinerPass.h
[oota-llvm.git] / lib / Transforms / IPO / GlobalOpt.cpp
index f5b12712f3aec2bbe7a5e37e0df2505a3a5e69c1..520af8750e18bfadf268f0df1f406199ff046bf2 100644 (file)
@@ -50,6 +50,8 @@ namespace {
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.addRequired<TargetData>();
     }
+    static char ID; // Pass identification, replacement for typeid
+    GlobalOpt() : ModulePass((intptr_t)&ID) {}
 
     bool runOnModule(Module &M);
 
@@ -61,6 +63,7 @@ namespace {
     bool ProcessInternalGlobal(GlobalVariable *GV,Module::global_iterator &GVI);
   };
 
+  char GlobalOpt::ID = 0;
   RegisterPass<GlobalOpt> X("globalopt", "Global Variable Optimizer");
 }
 
@@ -278,7 +281,7 @@ static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx) {
     if (IdxV < CS->getNumOperands()) return CS->getOperand(IdxV);
   } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg)) {
     if (IdxV < CA->getNumOperands()) return CA->getOperand(IdxV);
-  } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(Agg)) {
+  } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Agg)) {
     if (IdxV < CP->getNumOperands()) return CP->getOperand(IdxV);
   } else if (isa<ConstantAggregateZero>(Agg)) {
     if (const StructType *STy = dyn_cast<StructType>(Agg->getType())) {
@@ -390,7 +393,9 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV) {
       assert(In && "Couldn't get element of initializer?");
       GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
                                                GlobalVariable::InternalLinkage,
-                                               In, GV->getName()+"."+utostr(i));
+                                               In, GV->getName()+"."+utostr(i),
+                                               (Module *)NULL,
+                                               GV->isThreadLocal());
       Globals.insert(GV, NGV);
       NewGlobals.push_back(NGV);
     }
@@ -398,7 +403,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV) {
     unsigned NumElements = 0;
     if (const ArrayType *ATy = dyn_cast<ArrayType>(STy))
       NumElements = ATy->getNumElements();
-    else if (const PackedType *PTy = dyn_cast<PackedType>(STy))
+    else if (const VectorType *PTy = dyn_cast<VectorType>(STy))
       NumElements = PTy->getNumElements();
     else
       assert(0 && "Unknown aggregate sequential type!");
@@ -413,7 +418,9 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV) {
 
       GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
                                                GlobalVariable::InternalLinkage,
-                                               In, GV->getName()+"."+utostr(i));
+                                               In, GV->getName()+"."+utostr(i),
+                                               (Module *)NULL,
+                                               GV->isThreadLocal());
       Globals.insert(GV, NGV);
       NewGlobals.push_back(NGV);
     }
@@ -616,7 +623,11 @@ static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV) {
       Loads.push_back(LI);
       Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
     } else {
-      assert(isa<StoreInst>(*GUI) && "Only expect load and stores!");
+      // If we get here we could have stores, selects, or phi nodes whose values
+      // are loaded.
+      assert((isa<StoreInst>(*GUI) || isa<PHINode>(*GUI) ||
+              isa<SelectInst>(*GUI)) &&
+             "Only expect load and stores!");
     }
 
   if (Changed) {
@@ -699,7 +710,9 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
   Constant *Init = UndefValue::get(MI->getAllocatedType());
   GlobalVariable *NewGV = new GlobalVariable(MI->getAllocatedType(), false,
                                              GlobalValue::InternalLinkage, Init,
-                                             GV->getName()+".body");
+                                             GV->getName()+".body",
+                                             (Module *)NULL,
+                                             GV->isThreadLocal());
   GV->getParent()->getGlobalList().insert(GV, NewGV);
 
   // Anything that used the malloc now uses the global directly.
@@ -714,7 +727,8 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
   // keep track of whether the global was initialized yet or not.
   GlobalVariable *InitBool =
     new GlobalVariable(Type::Int1Ty, false, GlobalValue::InternalLinkage,
-                       ConstantInt::getFalse(), GV->getName()+".init");
+                       ConstantInt::getFalse(), GV->getName()+".init",
+                       (Module *)NULL, GV->isThreadLocal());
   bool InitBoolUsed = false;
 
   // Loop over all uses of GV, processing them in turn.
@@ -908,11 +922,12 @@ static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Ptr,
     Value *NewPtr = InsertedLoadsForPtr[FieldNo];
 
     // Create the new GEP idx vector.
-    std::vector<Value*> GEPIdx;
+    SmallVector<Value*, 8> GEPIdx;
     GEPIdx.push_back(GEPI->getOperand(1));
-    GEPIdx.insert(GEPIdx.end(), GEPI->op_begin()+3, GEPI->op_end());
+    GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end());
 
-    Value *NGEPI = new GetElementPtrInst(NewPtr, GEPIdx, GEPI->getName(), GEPI);
+    Value *NGEPI = new GetElementPtrInst(NewPtr, &GEPIdx[0], GEPIdx.size(),
+                                         GEPI->getName(), GEPI);
     GEPI->replaceAllUsesWith(NGEPI);
     GEPI->eraseFromParent();
   }
@@ -942,7 +957,8 @@ static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, MallocInst *MI){
     GlobalVariable *NGV =
       new GlobalVariable(PFieldTy, false, GlobalValue::InternalLinkage,
                          Constant::getNullValue(PFieldTy),
-                         GV->getName() + ".f" + utostr(FieldNo), GV);
+                         GV->getName() + ".f" + utostr(FieldNo), GV,
+                         GV->isThreadLocal());
     FieldGlobals.push_back(NGV);
     
     MallocInst *NMI = new MallocInst(FieldTy, MI->getArraySize(),
@@ -1144,7 +1160,9 @@ static void ShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
   // Create the new global, initializing it to false.
   GlobalVariable *NewGV = new GlobalVariable(Type::Int1Ty, false,
          GlobalValue::InternalLinkage, ConstantInt::getFalse(),
-                                             GV->getName()+".b");
+                                             GV->getName()+".b",
+                                             (Module *)NULL,
+                                             GV->isThreadLocal());
   GV->getParent()->getGlobalList().insert(GV, NewGV);
 
   Constant *InitVal = GV->getInitializer();
@@ -1154,7 +1172,7 @@ static void ShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
   // instead of a select to synthesize the desired value.
   bool IsOneZero = false;
   if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal))
-    IsOneZero = InitVal->isNullValue() && CI->equalsInt(1);
+    IsOneZero = InitVal->isNullValue() && CI->isOne();
 
   while (!GV->use_empty()) {
     Instruction *UI = cast<Instruction>(GV->use_back());
@@ -1345,8 +1363,8 @@ bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
       if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
         if (GV->getType()->getElementType() != Type::Int1Ty &&
             !GV->getType()->getElementType()->isFloatingPoint() &&
-            !isa<PackedType>(GV->getType()->getElementType()) &&
-            !GS.HasPHIUser) {
+            !isa<VectorType>(GV->getType()->getElementType()) &&
+            !GS.HasPHIUser && !GS.isNotSuitableForSRA) {
           DOUT << "   *** SHRINKING TO BOOL: " << *GV;
           ShrinkGlobalToBoolean(GV, SOVConstant);
           ++NumShrunkToBool;
@@ -1518,7 +1536,9 @@ static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
   
   // Create the new global and insert it next to the existing list.
   GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(),
-                                           GCL->getLinkage(), CA);
+                                           GCL->getLinkage(), CA, "",
+                                           (Module *)NULL,
+                                           GCL->isThreadLocal());
   GCL->getParent()->getGlobalList().insert(GCL, NGV);
   NGV->takeName(GCL);
   
@@ -1605,7 +1625,7 @@ static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
     Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1);
     
     // Return the modified struct.
-    return ConstantStruct::get(Elts);
+    return ConstantStruct::get(&Elts[0], Elts.size(), STy->isPacked());
   } else {
     ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo));
     const ArrayType *ATy = cast<ArrayType>(Init->getType());