Fix PNaCl-local files after merging LLVM 3.4

These are mostly compile fixes:

 * Update for interface changes:
    * readBytes() (unused arg was removed)
    * tool_output_file takes different flags
    * Materialize() uses error_code instead of bool + string
 * Remove uses of case ranges, which were removed in 3.4
 * pnacl-llc: Some TargetOptions fields were removed in 3.4

Also update test expectations.  LLVM's 'not' tool now treats crashes
as a failure unless '--crash' is passed.

BUG=https://code.google.com/p/nativeclient/issues/detail?id=3757
TEST=PNaCl toolchain trybots

Review URL: https://codereview.chromium.org/180483005/
diff --git a/include/llvm/Bitcode/NaCl/NaClBitstreamReader.h b/include/llvm/Bitcode/NaCl/NaClBitstreamReader.h
index 4304dba..92db870 100644
--- a/include/llvm/Bitcode/NaCl/NaClBitstreamReader.h
+++ b/include/llvm/Bitcode/NaCl/NaClBitstreamReader.h
@@ -363,8 +363,7 @@
     // Read the next word from the stream.
     uint8_t Array[sizeof(word_t)] = {0};
     
-    BitStream->getBitcodeBytes().readBytes(NextChar, sizeof(Array),
-                                           Array, NULL);
+    BitStream->getBitcodeBytes().readBytes(NextChar, sizeof(Array), Array);
     
     // Handle big-endian byte-swapping if necessary.
     support::detail::packed_endian_specific_integral
diff --git a/lib/Analysis/NaCl/PNaClABIVerifyFunctions.cpp b/lib/Analysis/NaCl/PNaClABIVerifyFunctions.cpp
index ea515ec..c1c909c 100644
--- a/lib/Analysis/NaCl/PNaClABIVerifyFunctions.cpp
+++ b/lib/Analysis/NaCl/PNaClABIVerifyFunctions.cpp
@@ -466,11 +466,6 @@
       // value, so check all the cases too.
       for (SwitchInst::ConstCaseIt Case = Switch->case_begin(),
              E = Switch->case_end(); Case != E; ++Case) {
-        // This check will go away when we merge upstream's r190328,
-        // which removes all case range support.
-        if (!Case.getCaseValueEx().isSingleNumber())
-          return "case range in switch instruction";
-
         if (!isValidScalarOperand(Case.getCaseValue()))
           return "bad switch case";
       }
diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeHeader.cpp b/lib/Bitcode/NaCl/Reader/NaClBitcodeHeader.cpp
index f058e25..cabda7d 100644
--- a/lib/Bitcode/NaCl/Reader/NaClBitcodeHeader.cpp
+++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeHeader.cpp
@@ -199,14 +199,14 @@
   unsigned NumBytes;
   {
     unsigned char Buffer[2 * WordSize];
-    if (Bytes->readBytes(0, sizeof(Buffer), Buffer, NULL))
+    if (Bytes->readBytes(0, sizeof(Buffer), Buffer))
       return UnsupportedError("Bitcode read failure");
     if (ReadPrefix(Buffer, Buffer + sizeof(Buffer), NumFields, NumBytes))
       return true; // ReadPrefix sets UnsupportedMessage
   }
   uint8_t *Header = new uint8_t[NumBytes];
   bool failed =
-      Bytes->readBytes(2 * WordSize, NumBytes, Header, NULL) ||
+      Bytes->readBytes(2 * WordSize, NumBytes, Header) ||
       ReadFields(Header, Header + NumBytes, NumFields, NumBytes);
   delete[] Header;
   if (failed)
diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
index 7bfe378..05c75cd 100644
--- a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
+++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
@@ -1490,24 +1490,34 @@
   return false;
 }
 
-bool NaClBitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
+error_code NaClBitcodeReader::Materialize(GlobalValue *GV) {
   Function *F = dyn_cast<Function>(GV);
   // If it's not a function or is already material, ignore the request.
-  if (!F || !F->isMaterializable()) return false;
+  if (!F || !F->isMaterializable())
+    return error_code::success();
 
   DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
   assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
   // If its position is recorded as 0, its body is somewhere in the stream
   // but we haven't seen it yet.
-  if (DFII->second == 0)
-    if (LazyStreamer && FindFunctionInStream(F, DFII)) return true;
+  if (DFII->second == 0) {
+    if (FindFunctionInStream(F, DFII)) {
+      // Refactoring upstream in LLVM 3.4 means we can no longer
+      // return an error string here, so return a catch-all error
+      // code.
+      // TODO(mseaborn): Clean up the reader to return a more
+      // meaningful error_code here.
+      return make_error_code(errc::invalid_argument);
+    }
+  }
 
   // Move the bit stream to the saved position of the deferred function body.
   Stream.JumpToBit(DFII->second);
 
   if (ParseFunctionBody(F)) {
-    if (ErrInfo) *ErrInfo = ErrorString;
-    return true;
+    // TODO(mseaborn): Clean up the reader to return a more meaningful
+    // error_code instead of a catch-all.
+    return make_error_code(errc::invalid_argument);
   }
 
   // Upgrade any old intrinsic calls in the function.
@@ -1522,7 +1532,7 @@
     }
   }
 
-  return false;
+  return error_code::success();
 }
 
 bool NaClBitcodeReader::isDematerializable(const GlobalValue *GV) const {
@@ -1545,16 +1555,18 @@
 }
 
 
-bool NaClBitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) {
+error_code NaClBitcodeReader::MaterializeModule(Module *M) {
   assert(M == TheModule &&
          "Can only Materialize the Module this NaClBitcodeReader is attached to.");
   // Iterate over the module, deserializing any functions that are still on
   // disk.
   for (Module::iterator F = TheModule->begin(), E = TheModule->end();
-       F != E; ++F)
-    if (F->isMaterializable() &&
-        Materialize(F, ErrInfo))
-      return true;
+       F != E; ++F) {
+    if (F->isMaterializable()) {
+      if (error_code EC = Materialize(F))
+        return EC;
+    }
+  }
 
   // At this point, if there are any function bodies, the current bit is
   // pointing to the END_BLOCK record after them. Now make sure the rest
@@ -1581,7 +1593,7 @@
   }
   std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
 
-  return false;
+  return error_code::success();
 }
 
 bool NaClBitcodeReader::InitStream() {
diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h
index 62ea1b7..32e9f6e 100644
--- a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h
+++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h
@@ -225,8 +225,8 @@
 
   virtual bool isMaterializable(const GlobalValue *GV) const;
   virtual bool isDematerializable(const GlobalValue *GV) const;
-  virtual bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
-  virtual bool MaterializeModule(Module *M, std::string *ErrInfo = 0);
+  virtual error_code Materialize(GlobalValue *GV);
+  virtual error_code MaterializeModule(Module *M);
   virtual void Dematerialize(GlobalValue *GV);
 
   bool Error(const std::string &Str) {
diff --git a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp
index 86ae1b3..6f12c55 100644
--- a/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp
+++ b/lib/Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp
@@ -695,11 +695,6 @@
       Vals64.push_back(SI.getNumCases());
       for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
            i != e; ++i) {
-        // This check will go away when we merge upstream's r190328,
-        // which removes all case range support.
-        if (!i.getCaseValueEx().isSingleNumber())
-          report_fatal_error("Case ranges are not supported in PNaCl bitcode");
-
         // The PNaCl bitcode format has vestigial support for case
         // ranges, but we no longer support reading or writing them,
         // so the next two fields always have the same values.
diff --git a/lib/Support/Unix/RWMutex.inc b/lib/Support/Unix/RWMutex.inc
index 40e87ff..edcbd52 100644
--- a/lib/Support/Unix/RWMutex.inc
+++ b/lib/Support/Unix/RWMutex.inc
@@ -16,28 +16,36 @@
 //===          is guaranteed to work on *all* UNIX variants.
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Support/Mutex.h"
+
 namespace llvm {
 
 using namespace sys;
 
-RWMutexImpl::RWMutexImpl() { }
+// This naive implementation treats readers the same as writers.  This
+// will therefore deadlock if a thread tries to acquire a read lock
+// multiple times.
 
-RWMutexImpl::~RWMutexImpl() { }
+RWMutexImpl::RWMutexImpl() : data_(new Mutex(false)) { }
+
+RWMutexImpl::~RWMutexImpl() {
+  delete static_cast<Mutex *>(data_);
+}
 
 bool RWMutexImpl::reader_acquire() {
-  return true;
+  return static_cast<Mutex *>(data_)->acquire();
 }
 
 bool RWMutexImpl::reader_release() {
-  return true;
+  return static_cast<Mutex *>(data_)->release();
 }
 
 bool RWMutexImpl::writer_acquire() {
-  return true;
+  return static_cast<Mutex *>(data_)->acquire();
 }
 
 bool RWMutexImpl::writer_release() {
-  return true;
+  return static_cast<Mutex *>(data_)->release();
 }
 
 }
diff --git a/lib/Target/X86/MCTargetDesc/X86MCNaCl.cpp b/lib/Target/X86/MCTargetDesc/X86MCNaCl.cpp
index 2346927..0bb950f 100644
--- a/lib/Target/X86/MCTargetDesc/X86MCNaCl.cpp
+++ b/lib/Target/X86/MCTargetDesc/X86MCNaCl.cpp
@@ -56,7 +56,7 @@
 static MCSymbol *CreateTempLabel(MCContext &Context, const char *Prefix) {
   SmallString<128> NameSV;
   raw_svector_ostream(NameSV)
-    << Context.getAsmInfo().getPrivateGlobalPrefix() // get internal label
+    << Context.getAsmInfo()->getPrivateGlobalPrefix() // get internal label
     << Prefix << Context.getUniqueSymbolID();
   return Context.GetOrCreateSymbol(NameSV);
 }
diff --git a/lib/Transforms/NaCl/PromoteIntegers.cpp b/lib/Transforms/NaCl/PromoteIntegers.cpp
index e56bd78..5a18244 100644
--- a/lib/Transforms/NaCl/PromoteIntegers.cpp
+++ b/lib/Transforms/NaCl/PromoteIntegers.cpp
@@ -551,12 +551,6 @@
     for (SwitchInst::CaseIt I = Switch->case_begin(),
              E = Switch->case_end();
          I != E; ++I) {
-      // This sanity check should never trigger because no-one
-      // generates case ranges.  It will go away when we merge
-      // upstream's r190328, which removes all case range support.
-      if (!I.getCaseValueEx().isSingleNumber())
-        report_fatal_error("Case ranges are not supported in PNaCl");
-
       NewInst->addCase(cast<ConstantInt>(convertConstant(I.getCaseValue())),
                        I.getCaseSuccessor());
     }
diff --git a/test/CodeGen/ARM/varargs-spill-stack-align-nacl.ll b/test/CodeGen/ARM/varargs-spill-stack-align-nacl.ll
index 19d6cbe..c0b9eb2 100644
--- a/test/CodeGen/ARM/varargs-spill-stack-align-nacl.ll
+++ b/test/CodeGen/ARM/varargs-spill-stack-align-nacl.ll
@@ -27,5 +27,7 @@
 ; CHECK: sub sp, sp, #12
 ; Calculate the address of the varargs save area and save varargs
 ; arguments into it.
-; CHECK-NEXT: add r0, sp, #20
+; @LOCALMOD: Adjust test expectation, removing NEXT to allow CFI
+; directive to intervene in the output.
+; CHECK: add r0, sp, #20
 ; CHECK-NEXT: stm r0, {r1, r2, r3}
diff --git a/test/NaCl/PNaClLLC/test-runs-verify.ll b/test/NaCl/PNaClLLC/test-runs-verify.ll
index 9e7c9d3..0b10960 100644
--- a/test/NaCl/PNaClLLC/test-runs-verify.ll
+++ b/test/NaCl/PNaClLLC/test-runs-verify.ll
@@ -1,4 +1,4 @@
-; RUN: not pnacl-llc -mtriple=i386-unknown-nacl -filetype=asm %s -o - 2>&1 | FileCheck %s
+; RUN: not --crash pnacl-llc -mtriple=i386-unknown-nacl -filetype=asm %s -o - 2>&1 | FileCheck %s
 
 ; Test that the Verifier pass is running in pnacl-llc.
 
diff --git a/tools/pnacl-bccompress/pnacl-bccompress.cpp b/tools/pnacl-bccompress/pnacl-bccompress.cpp
index 6aceef8..f96e554 100644
--- a/tools/pnacl-bccompress/pnacl-bccompress.cpp
+++ b/tools/pnacl-bccompress/pnacl-bccompress.cpp
@@ -1415,7 +1415,7 @@
   std::string ErrorInfo;
   OwningPtr<tool_output_file> OutFile(
       new tool_output_file(OutputFilename.c_str(), ErrorInfo,
-                           raw_fd_ostream::F_Binary));
+                           sys::fs::F_Binary));
   if (!ErrorInfo.empty())
     return Error(ErrorInfo);
 
diff --git a/tools/pnacl-freeze/pnacl-freeze.cpp b/tools/pnacl-freeze/pnacl-freeze.cpp
index a3bc1a6..0e878ea 100644
--- a/tools/pnacl-freeze/pnacl-freeze.cpp
+++ b/tools/pnacl-freeze/pnacl-freeze.cpp
@@ -41,7 +41,7 @@
   std::string ErrorInfo;
   OwningPtr<tool_output_file> Out
     (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
-			  raw_fd_ostream::F_Binary));
+                          sys::fs::F_Binary));
   if (!ErrorInfo.empty()) {
     errs() << ErrorInfo << '\n';
     exit(1);
diff --git a/tools/pnacl-llc/ThreadedStreamingCache.cpp b/tools/pnacl-llc/ThreadedStreamingCache.cpp
index 4176ea2..b4092c0 100644
--- a/tools/pnacl-llc/ThreadedStreamingCache.cpp
+++ b/tools/pnacl-llc/ThreadedStreamingCache.cpp
@@ -25,19 +25,16 @@
 
 int ThreadedStreamingCache::fetchCacheLine(uint64_t address) const {
   uint64_t Base = address & kCacheSizeMask;
-  uint64_t Copied;
   int Ret;
   ScopedLock L(StreamerLock);
   if (Streamer->isValidAddress(Base + kCacheSize - 1)) {
-    Ret = Streamer->readBytes(Base, kCacheSize, &Cache[0], &Copied);
-    assert(Copied == kCacheSize);
+    Ret = Streamer->readBytes(Base, kCacheSize, &Cache[0]);
     assert(Ret == 0);
     MinObjectSize = Base + kCacheSize;
   } else {
     uint64_t End = Streamer->getExtent();
     assert(End > address && End <= Base + kCacheSize);
-    Ret = Streamer->readBytes(Base, End - Base, &Cache[0], &Copied);
-    assert(Copied == End - Base);
+    Ret = Streamer->readBytes(Base, End - Base, &Cache[0]);
     assert(Ret == 0);
     MinObjectSize = End;
   }
@@ -56,7 +53,7 @@
 }
 
 int ThreadedStreamingCache::readBytes(
-    uint64_t address, uint64_t size, uint8_t* buf, uint64_t* copied) const {
+    uint64_t address, uint64_t size, uint8_t* buf) const {
   // To keep the cache fetch simple, we currently require that no request cross
   // the cache line. This isn't a problem for the bitcode reader because it only
   // fetches a byte or a word at a time.
@@ -66,7 +63,6 @@
     if(fetchCacheLine(address))
       return -1;
   }
-  if (copied) *copied = size;
   memcpy(buf, &Cache[address - CacheBase], size);
   return 0;
 }
diff --git a/tools/pnacl-llc/ThreadedStreamingCache.h b/tools/pnacl-llc/ThreadedStreamingCache.h
index b9fca12..7f1e71f 100644
--- a/tools/pnacl-llc/ThreadedStreamingCache.h
+++ b/tools/pnacl-llc/ThreadedStreamingCache.h
@@ -29,8 +29,7 @@
   virtual int readByte(uint64_t address, uint8_t* ptr) const LLVM_OVERRIDE;
   virtual int readBytes(uint64_t address,
                         uint64_t size,
-                        uint8_t* buf,
-                        uint64_t* copied) const LLVM_OVERRIDE;
+                        uint8_t* buf) const LLVM_OVERRIDE;
   virtual const uint8_t *getPointer(uint64_t address,
                                     uint64_t size) const LLVM_OVERRIDE {
     // This could be fixed by ensuring the bytes are fetched and making a copy,
diff --git a/tools/pnacl-llc/pnacl-llc.cpp b/tools/pnacl-llc/pnacl-llc.cpp
index 5f16b6f..5264d6c 100644
--- a/tools/pnacl-llc/pnacl-llc.cpp
+++ b/tools/pnacl-llc/pnacl-llc.cpp
@@ -205,8 +205,9 @@
 
   // Open the file.
   std::string error;
-  unsigned OpenFlags = 0;
-  if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
+  sys::fs::OpenFlags OpenFlags = sys::fs::F_None;
+  if (Binary)
+    OpenFlags |= sys::fs::F_Binary;
   OwningPtr<tool_output_file> FDOut(
       new tool_output_file(Filename.c_str(), error, OpenFlags));
   if (!error.empty()) {
@@ -602,7 +603,6 @@
   TargetOptions Options;
   Options.LessPreciseFPMADOption = EnableFPMAD;
   Options.NoFramePointerElim = DisableFPElim;
-  Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf;
   Options.AllowFPOpFusion = FuseFPOps;
   Options.UnsafeFPMath = EnableUnsafeFPMath;
   Options.NoInfsFPMath = EnableNoInfsFPMath;
@@ -616,12 +616,10 @@
   Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
   Options.DisableTailCalls = DisableTailCalls;
   Options.StackAlignmentOverride = OverrideStackAlignment;
-  Options.RealignStack = EnableRealignStack;
   Options.TrapFuncName = TrapFuncName;
   Options.PositionIndependentExecutable = EnablePIE;
   Options.EnableSegmentedStacks = SegmentedStacks;
   Options.UseInitArray = UseInitArray;
-  Options.SSPBufferSize = SSPBufferSize;
 
   if (GenerateSoftFloatCalls)
     FloatABIForCalls = FloatABI::Soft;
diff --git a/tools/pnacl-thaw/pnacl-thaw.cpp b/tools/pnacl-thaw/pnacl-thaw.cpp
index adde120..2d39f12 100644
--- a/tools/pnacl-thaw/pnacl-thaw.cpp
+++ b/tools/pnacl-thaw/pnacl-thaw.cpp
@@ -41,7 +41,7 @@
   std::string ErrorInfo;
   OwningPtr<tool_output_file> Out
     (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
-			  raw_fd_ostream::F_Binary));
+                          sys::fs::F_Binary));
   if (!ErrorInfo.empty()) {
     errs() << ErrorInfo << '\n';
     exit(1);