[SROA] Fix the loop exit placement to be prior to indexing the splits
[oota-llvm.git] / lib / Transforms / Scalar / SampleProfile.cpp
index 4f746577f8e59a48a8370ecf6a7ee7d5eb1845e3..179bbf78366d118225bf956c3b62973d88ea6dbb 100644 (file)
@@ -305,7 +305,7 @@ void SampleProfileLoader::findEquivalencesFor(
   for (auto *BB2 : Descendants) {
     bool IsDomParent = DomTree->dominates(BB2, BB1);
     bool IsInSameLoop = LI->getLoopFor(BB1) == LI->getLoopFor(BB2);
-    if (BB1 != BB2 && VisitedBlocks.insert(BB2) && IsDomParent &&
+    if (BB1 != BB2 && VisitedBlocks.insert(BB2).second && IsDomParent &&
         IsInSameLoop) {
       EquivalenceClass[BB2] = BB1;
 
@@ -494,7 +494,7 @@ bool SampleProfileLoader::propagateThroughEdges(Function &F) {
                          << " known. Set weight for block: ";
                   printBlockWeight(dbgs(), BB););
           }
-          if (VisitedBlocks.insert(BB))
+          if (VisitedBlocks.insert(BB).second)
             Changed = true;
         } else if (NumUnknownEdges == 1 && VisitedBlocks.count(BB)) {
           // If there is a single unknown edge and the block has been
@@ -540,7 +540,7 @@ void SampleProfileLoader::buildEdges(Function &F) {
       llvm_unreachable("Found a stale predecessors list in a basic block.");
     for (pred_iterator PI = pred_begin(B1), PE = pred_end(B1); PI != PE; ++PI) {
       BasicBlock *B2 = *PI;
-      if (Visited.insert(B2))
+      if (Visited.insert(B2).second)
         Predecessors[B1].push_back(B2);
     }
 
@@ -550,7 +550,7 @@ void SampleProfileLoader::buildEdges(Function &F) {
       llvm_unreachable("Found a stale successors list in a basic block.");
     for (succ_iterator SI = succ_begin(B1), SE = succ_end(B1); SI != SE; ++SI) {
       BasicBlock *B2 = *SI;
-      if (Visited.insert(B2))
+      if (Visited.insert(B2).second)
         Successors[B1].push_back(B2);
     }
   }
@@ -627,29 +627,6 @@ void SampleProfileLoader::propagateWeights(Function &F) {
   }
 }
 
-/// \brief Locate the DISubprogram for F.
-///
-/// We look for the first instruction that has a debug annotation
-/// leading back to \p F.
-///
-/// \returns a valid DISubprogram, if found. Otherwise, it returns an empty
-/// DISubprogram.
-static const DISubprogram getDISubprogram(Function &F, const LLVMContext &Ctx) {
-  for (auto &BI : F) {
-    BasicBlock *BB = &BI;
-    for (auto &Inst : BB->getInstList()) {
-      DebugLoc DLoc = Inst.getDebugLoc();
-      if (DLoc.isUnknown())
-        continue;
-      const MDNode *Scope = DLoc.getScopeNode(Ctx);
-      DISubprogram Subprogram = getDISubprogram(Scope);
-      return Subprogram.describes(&F) ? Subprogram : DISubprogram();
-    }
-  }
-
-  return DISubprogram();
-}
-
 /// \brief Get the line number for the function header.
 ///
 /// This looks up function \p F in the current compilation unit and
@@ -662,7 +639,7 @@ static const DISubprogram getDISubprogram(Function &F, const LLVMContext &Ctx) {
 /// \returns the line number where \p F is defined. If it returns 0,
 ///          it means that there is no debug information available for \p F.
 unsigned SampleProfileLoader::getFunctionLoc(Function &F) {
-  const DISubprogram &S = getDISubprogram(F, *Ctx);
+  DISubprogram S = getDISubprogram(&F);
   if (S.isSubprogram())
     return S.getLineNumber();
 
@@ -760,8 +737,14 @@ INITIALIZE_PASS_END(SampleProfileLoader, "sample-profile",
                     "Sample Profile loader", false, false)
 
 bool SampleProfileLoader::doInitialization(Module &M) {
-  Reader.reset(new SampleProfileReader(M, Filename));
-  ProfileIsValid = Reader->load();
+  auto ReaderOrErr = SampleProfileReader::create(Filename, M.getContext());
+  if (std::error_code EC = ReaderOrErr.getError()) {
+    std::string Msg = "Could not open profile: " + EC.message();
+    M.getContext().diagnose(DiagnosticInfoSampleProfile(Filename.data(), Msg));
+    return false;
+  }
+  Reader = std::move(ReaderOrErr.get());
+  ProfileIsValid = (Reader->read() == sampleprof_error::success);
   return true;
 }