reland r332579: [llvm-exegesis] Update to cover latency through another opcode.

Restructuring the code to measure latency and uops.
The end goal is to have this program spawn another process to deal with SIGILL and other malformed programs. It is not yet the case in this redesign, it is still the main program that runs the code (and may crash).
It now uses BitVector instead of Graph for performance reasons.

https://reviews.llvm.org/D46821

(with fixed ARM tests)

Authored by Guillaume Chatelet

llvm-svn: 332592
diff --git a/llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp
index a043ea4..b1083f4 100644
--- a/llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp
+++ b/llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp
@@ -61,10 +61,8 @@
 
 namespace exegesis {
 
-namespace {
-
 template <typename ObjectOrList>
-ObjectOrList readYamlOrDieCommon(llvm::StringRef Filename) {
+static ObjectOrList readYamlOrDieCommon(llvm::StringRef Filename) {
   std::unique_ptr<llvm::MemoryBuffer> MemBuffer = llvm::cantFail(
       llvm::errorOrToExpected(llvm::MemoryBuffer::getFile(Filename)));
   llvm::yaml::Input Yin(*MemBuffer);
@@ -73,8 +71,6 @@
   return Benchmark;
 }
 
-} // namespace
-
 InstructionBenchmark
 InstructionBenchmark::readYamlOrDie(llvm::StringRef Filename) {
   return readYamlOrDieCommon<InstructionBenchmark>(Filename);
@@ -85,19 +81,26 @@
   return readYamlOrDieCommon<std::vector<InstructionBenchmark>>(Filename);
 }
 
+void InstructionBenchmark::writeYamlTo(llvm::raw_ostream &S) {
+  llvm::yaml::Output Yout(S);
+  Yout << *this;
+}
+
+void InstructionBenchmark::readYamlFrom(llvm::StringRef InputContent) {
+  llvm::yaml::Input Yin(InputContent);
+  Yin >> *this;
+}
+
+// FIXME: Change the API to let the caller handle errors.
 void InstructionBenchmark::writeYamlOrDie(const llvm::StringRef Filename) {
   if (Filename == "-") {
-    llvm::yaml::Output Yout(llvm::outs());
-    Yout << *this;
+    writeYamlTo(llvm::outs());
   } else {
-    llvm::SmallString<1024> Buffer;
-    llvm::raw_svector_ostream Ostr(Buffer);
-    llvm::yaml::Output Yout(Ostr);
-    Yout << *this;
-    std::unique_ptr<llvm::FileOutputBuffer> File =
-        llvm::cantFail(llvm::FileOutputBuffer::create(Filename, Buffer.size()));
-    memcpy(File->getBufferStart(), Buffer.data(), Buffer.size());
-    llvm::cantFail(File->commit());
+    int ResultFD = 0;
+    llvm::cantFail(llvm::errorCodeToError(
+        openFileForWrite(Filename, ResultFD, llvm::sys::fs::F_Text)));
+    llvm::raw_fd_ostream Ostr(ResultFD, true /*shouldClose*/);
+    writeYamlTo(Ostr);
   }
 }