// MemCpyOpt::runOnFunction - This is the main transformation entry point for a
// function.
//
bool AggregateGlobalOpsOpt::runOnFunction(Function &F) {
  bool MadeChange = false;
  bool DebugThis = DEBUG;
  
  if( debugThisFn[0] && F.getName() == debugThisFn ) {
    DebugThis = true;
  }

  //MD = &getAnalysis<MemoryDependenceAnalysis>();
#if HAVE_LLVM_VER >= 37
  TD = & F.getParent()->getDataLayout();
#elif HAVE_LLVM_VER >= 35
  TD = & getAnalysisIfAvailable<DataLayoutPass>()->getDataLayout();
#else
  TD = getAnalysisIfAvailable<DataLayout>();
#endif
  //TLI = &getAnalysis<TargetLibraryInfo>();

  // Walk all instruction in the function.
  for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) {
    if( DebugThis ) {
      errs() << "Working on BB ";
      BB->dump();
    }

    for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
      // Avoid invalidating the iterator.
      Instruction *I = BI++;

      if( isGlobalLoadOrStore(I, globalSpace, true, true) ) {
        Instruction* lastAdded = tryAggregating(I, getLoadStorePointer(I), DebugThis);
        if( lastAdded ) {
          MadeChange = true;
          BI = lastAdded;
        }
      }
    }

    if( DebugThis && MadeChange ) {
      errs() << "After transform BB is ";
      BB->dump();
    }

  }

  if( extraChecks ) {
#if HAVE_LLVM_VER >= 35
    assert(!verifyFunction(F, &errs()));
#else
    verifyFunction(F);
#endif
  }

  //MD = 0;
  return MadeChange;
}