Subzero: Update for LLVM 3.9 (trunk).

The purpose is to allow pnacl-sz to be built within an LLVM checkout (version 3.9 or later), and not require the complex PNaCl build environment.

Within an LLVM checkout, one would do something like the following:
  cd projects
  git clone https://chromium.googlesource.com/native_client/pnacl-subzero
  # change to LLVM build directory
  cmake -G "Ninja" <path_to_llvm_source_dir>
  ninja

A follow-on CL will add in the PNaCl bitcode reader source files that are needed for compiling and linking.

BUG= none
R=jpp@chromium.org

Review URL: https://codereview.chromium.org/1961743002 .
diff --git a/src/IceBitVector.h b/src/IceBitVector.h
index cdb4f33..3a5f8e6 100644
--- a/src/IceBitVector.h
+++ b/src/IceBitVector.h
@@ -260,6 +260,14 @@
   unsigned Capacity; // Size of allocated memory in BitWord.
   Allocator Alloc;
 
+  uint64_t alignTo(uint64_t Value, uint64_t Align) {
+#ifdef PNACL_LLVM
+    return llvm::RoundUpToAlignment(Value, Align);
+#else // !PNACL_LLVM
+    return llvm::alignTo(Value, Align);
+#endif // !PNACL_LLVM
+  }
+
 public:
   typedef unsigned size_type;
   // Encapsulation of a single bit.
@@ -469,7 +477,7 @@
 
     BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
     Bits[I / BITWORD_SIZE] |= PrefixMask;
-    I = llvm::RoundUpToAlignment(I, BITWORD_SIZE);
+    I = alignTo(I, BITWORD_SIZE);
 
     for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
       Bits[I / BITWORD_SIZE] = ~0UL;
@@ -509,7 +517,7 @@
 
     BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
     Bits[I / BITWORD_SIZE] &= ~PrefixMask;
-    I = llvm::RoundUpToAlignment(I, BITWORD_SIZE);
+    I = alignTo(I, BITWORD_SIZE);
 
     for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
       Bits[I / BITWORD_SIZE] = 0UL;
diff --git a/src/IceCfg.cpp b/src/IceCfg.cpp
index 9a47dd6..f717379 100644
--- a/src/IceCfg.cpp
+++ b/src/IceCfg.cpp
@@ -1102,7 +1102,7 @@
 
       Instr.setDeleted();
       auto &LoweringContext = getTarget()->getContext();
-      LoweringContext.setInsertPoint(Instr);
+      LoweringContext.setInsertPoint(instToIterator(&Instr));
       LoweringContext.insert(ShuffleVector);
     }
   }
diff --git a/src/IceCfgNode.cpp b/src/IceCfgNode.cpp
index fb3708b..78e3d18 100644
--- a/src/IceCfgNode.cpp
+++ b/src/IceCfgNode.cpp
@@ -1342,7 +1342,7 @@
       I->emitIAS(Func);
       // Only update stats during the final pass.
       if (Retrying)
-        updateStats(Func, I);
+        updateStats(Func, iteratorToInst(I));
     } else {
       // Treat it as though there were an implicit bundle_lock and
       // bundle_unlock wrapping the instruction.
@@ -1352,7 +1352,7 @@
       Helper.rollback();
       Helper.padToNextBundle();
       I->emitIAS(Func);
-      updateStats(Func, I);
+      updateStats(Func, iteratorToInst(I));
       Helper.leaveBundleLockRegion();
     }
   }
diff --git a/src/IceClFlags.cpp b/src/IceClFlags.cpp
index 257c6f1..4982cbf 100644
--- a/src/IceClFlags.cpp
+++ b/src/IceClFlags.cpp
@@ -163,6 +163,10 @@
   using ReturnType = Ice::VerboseMask;
 };
 
+template <> struct ToSetterParam<cl::list<std::string>> {
+  using ReturnType = std::vector<std::string>;
+};
+
 template <typename T>
 typename ToSetterParam<T>::ReturnType toSetterParam(const T &Param) {
   return Param;
@@ -181,6 +185,12 @@
   return VMask;
 }
 
+template <>
+ToSetterParam<cl::list<std::string>>::ReturnType
+toSetterParam(const cl::list<std::string> &Param) {
+  return *&Param;
+}
+
 } // end of anonymous namespace
 
 void ClFlags::getParsedClFlags(ClFlags &OutFlags) {
diff --git a/src/IceClFlags.h b/src/IceClFlags.h
index 19785bf..8c43471 100644
--- a/src/IceClFlags.h
+++ b/src/IceClFlags.h
@@ -36,6 +36,20 @@
 #include <utility>
 #include <vector>
 
+#ifndef PNACL_LLVM
+namespace llvm {
+// \brief Define the expected format of the file.
+enum NaClFileFormat {
+  // LLVM IR source or bitcode file (as appropriate).
+  LLVMFormat,
+  // PNaCl bitcode file.
+  PNaClFormat,
+  // Autodetect if PNaCl or LLVM format.
+  AutodetectFileFormat
+};
+} // end of namespace llvm
+#endif // !PNACL_LLVM
+
 namespace Ice {
 // detail defines the type cl_type_traits, which is used to define the
 // getters/setters for the ClFlags class. It converts the cl_detail::*_flag
diff --git a/src/IceCompileServer.cpp b/src/IceCompileServer.cpp
index e700b1e..15a86c3 100644
--- a/src/IceCompileServer.cpp
+++ b/src/IceCompileServer.cpp
@@ -50,7 +50,12 @@
 public:
   TextDataStreamer() = default;
   ~TextDataStreamer() final = default;
-  static TextDataStreamer *create(const std::string &Filename,
+#ifdef PNACL_LLVM
+  using CreateType = TextDataStreamer *;
+#else // !PNACL_LLVM
+  using CreateType = std::unique_ptr<TextDataStreamer>;
+#endif // !PNACL_LLVM
+  static CreateType create(const std::string &Filename,
                                   std::string *Err);
   size_t GetBytes(unsigned char *Buf, size_t Len) final;
 
@@ -59,8 +64,9 @@
   size_t Cursor = 0;
 };
 
-TextDataStreamer *TextDataStreamer::create(const std::string &Filename,
+TextDataStreamer::CreateType TextDataStreamer::create(const std::string &Filename,
                                            std::string *Err) {
+#ifdef PNACL_LLVM
   TextDataStreamer *Streamer = new TextDataStreamer();
   llvm::raw_string_ostream ErrStrm(*Err);
   if (std::error_code EC = llvm::readNaClRecordTextAndBuildBitcode(
@@ -72,6 +78,9 @@
   }
   ErrStrm.flush();
   return Streamer;
+#else // !PNACL_LLVM
+  return CreateType();
+#endif // !PNACL_LLVM
 }
 
 size_t TextDataStreamer::GetBytes(unsigned char *Buf, size_t Len) {
diff --git a/src/IceCompiler.cpp b/src/IceCompiler.cpp
index fbe0577..08e1a14 100644
--- a/src/IceCompiler.cpp
+++ b/src/IceCompiler.cpp
@@ -86,8 +86,11 @@
   const bool WasmBuildOnRead = Flags.getBuildOnRead() && wasmInput(IRFilename);
   if (BuildOnRead) {
     std::unique_ptr<PNaClTranslator> PTranslator(new PNaClTranslator(&Ctx));
-    std::unique_ptr<llvm::StreamingMemoryObject> MemObj(
-        new llvm::StreamingMemoryObjectImpl(InputStream.release()));
+#ifdef PNACL_LLVM
+    std::unique_ptr<llvm::StreamingMemoryObject> MemObj(new llvm::StreamingMemoryObjectImpl(InputStream.release()));
+#else // !PNACL_LLVM
+    std::unique_ptr<llvm::StreamingMemoryObject> MemObj(new llvm::StreamingMemoryObject(std::move(InputStream)));
+#endif // !PNACL_LLVM
     PTranslator->translate(IRFilename, std::move(MemObj));
     Translator.reset(PTranslator.release());
   } else if (WasmBuildOnRead) {
@@ -120,6 +123,7 @@
     // Parse the input LLVM IR file into a module.
     llvm::SMDiagnostic Err;
     TimerMarker T1(Ice::TimerStack::TT_parse, &Ctx);
+#ifdef PNACL_LLVM
     llvm::DiagnosticHandlerFunction DiagnosticHandler =
         Flags.getLLVMVerboseErrors()
             ? redirectNaClDiagnosticToStream(llvm::errs())
@@ -127,6 +131,12 @@
     std::unique_ptr<llvm::Module> Mod =
         NaClParseIRFile(IRFilename, Flags.getInputFileFormat(), Err,
                         llvm::getGlobalContext(), DiagnosticHandler);
+#else // !PNACL_LLVM
+    llvm::DiagnosticHandlerFunction DiagnosticHandler = nullptr;
+    llvm::LLVMContext Context;
+    std::unique_ptr<llvm::Module> Mod =
+        parseIRFile(IRFilename, Err, Context);
+#endif // !PNACL_LLVM
     if (!Mod) {
       Err.print(Flags.getAppName().c_str(), llvm::errs());
       Ctx.getErrorStatus()->assign(EC_Bitcode);
diff --git a/src/IceConverter.cpp b/src/IceConverter.cpp
index 0729b73..7d192e6 100644
--- a/src/IceConverter.cpp
+++ b/src/IceConverter.cpp
@@ -111,7 +111,7 @@
       // The initial definition/use of each arg is the entry node.
       for (auto ArgI = F->arg_begin(), ArgE = F->arg_end(); ArgI != ArgE;
            ++ArgI) {
-        Func->addArg(mapValueToIceVar(ArgI));
+        Func->addArg(mapValueToIceVar(&*ArgI));
       }
 
       // Make an initial pass through the block list just to resolve the blocks
@@ -708,7 +708,7 @@
                                      E = Mod->global_end();
        I != E; ++I) {
 
-    const GlobalVariable *GV = I;
+    const GlobalVariable *GV = &*I;
 
     Ice::GlobalDeclaration *Var = getConverter().getGlobalDeclaration(GV);
     auto *VarDecl = cast<Ice::VariableDeclaration>(Var);
@@ -888,7 +888,7 @@
   for (Module::const_global_iterator I = Mod->global_begin(),
                                      E = Mod->global_end();
        I != E; ++I) {
-    const GlobalVariable *GV = I;
+    const GlobalVariable *GV = &*I;
     constexpr bool NoSuppressMangling = false;
     auto *Var = VariableDeclaration::create(
         GlobalDeclarationsPool.get(), NoSuppressMangling, GV->getLinkage());
diff --git a/src/IceInst.h b/src/IceInst.h
index 352a5d4..769d405 100644
--- a/src/IceInst.h
+++ b/src/IceInst.h
@@ -1131,4 +1131,24 @@
 
 } // end of namespace llvm
 
+namespace Ice {
+
+inline InstList::iterator instToIterator(Inst *Instr) {
+#ifdef PNACL_LLVM
+  return Instr;
+#else // !PNACL_LLVM
+  return Instr->getIterator();
+#endif // !PNACL_LLVM
+}
+
+inline Inst *iteratorToInst(InstList::iterator Iter) {
+  return &*Iter;
+}
+
+inline const Inst *iteratorToInst(InstList::const_iterator Iter) {
+  return &*Iter;
+}
+
+} // end of namespace Ice
+
 #endif // SUBZERO_SRC_ICEINST_H
diff --git a/src/IceTargetLowering.cpp b/src/IceTargetLowering.cpp
index 275516e..207a76c 100644
--- a/src/IceTargetLowering.cpp
+++ b/src/IceTargetLowering.cpp
@@ -338,7 +338,7 @@
     Context.init(Node);
     while (!Context.atEnd()) {
       PostIncrLoweringContext _(Context);
-      genTargetHelperCallFor(Context.getCur());
+      genTargetHelperCallFor(iteratorToInst(Context.getCur()));
     }
   }
 }
@@ -353,7 +353,7 @@
 }
 
 void TargetLowering::doNopInsertion(RandomNumberGenerator &RNG) {
-  Inst *I = Context.getCur();
+  Inst *I = iteratorToInst(Context.getCur());
   bool ShouldSkip = llvm::isa<InstFakeUse>(I) || llvm::isa<InstFakeDef>(I) ||
                     llvm::isa<InstFakeKill>(I) || I->isRedundantAssign() ||
                     I->isDeleted();
@@ -378,7 +378,7 @@
 // should delete any additional instructions it consumes.
 void TargetLowering::lower() {
   assert(!Context.atEnd());
-  Inst *Instr = Context.getCur();
+  Inst *Instr = iteratorToInst(Context.getCur());
   Instr->deleteIfDead();
   if (!Instr->isDeleted() && !llvm::isa<InstFakeDef>(Instr) &&
       !llvm::isa<InstFakeUse>(Instr)) {
@@ -473,7 +473,7 @@
   Context.setNext(Next);
   Context.insert(Instr);
   --Next;
-  assert(&*Next == Instr);
+  assert(iteratorToInst(Next) == Instr);
   Context.setCur(Next);
   lower();
 }
diff --git a/src/IceTargetLowering.h b/src/IceTargetLowering.h
index 88de3d5..dcba5ce 100644
--- a/src/IceTargetLowering.h
+++ b/src/IceTargetLowering.h
@@ -79,13 +79,13 @@
   Inst *getNextInst() const {
     if (Next == End)
       return nullptr;
-    return Next;
+    return iteratorToInst(Next);
   }
   Inst *getNextInst(InstList::iterator &Iter) const {
     advanceForward(Iter);
     if (Iter == End)
       return nullptr;
-    return Iter;
+    return iteratorToInst(Iter);
   }
   CfgNode *getNode() const { return Node; }
   bool atEnd() const { return Cur == End; }
diff --git a/src/IceTargetLoweringARM32.cpp b/src/IceTargetLoweringARM32.cpp
index 22d4c0a..f83eb59 100644
--- a/src/IceTargetLoweringARM32.cpp
+++ b/src/IceTargetLoweringARM32.cpp
@@ -842,7 +842,7 @@
     Context.init(Node);
     while (!Context.atEnd()) {
       PostIncrLoweringContext PostIncrement(Context);
-      Inst *CurInstr = Context.getCur();
+      Inst *CurInstr = iteratorToInst(Context.getCur());
       if (auto *Call = llvm::dyn_cast<InstCall>(CurInstr)) {
         SizeT OutArgsSizeBytes = getCallStackArgumentsSizeBytes(Call);
         MaxOutArgsSizeBytes = std::max(MaxOutArgsSizeBytes, OutArgsSizeBytes);
@@ -933,7 +933,7 @@
 
   // The got addr needs to be materialized at the same point where DefGotPtr
   // lives.
-  Context.setInsertPoint(DefGotPtr);
+  Context.setInsertPoint(instToIterator(DefGotPtr));
   assert(DefGotPtr->getSrcSize() == 1);
   auto *T = llvm::cast<Variable>(DefGotPtr->getSrc(0));
   loadNamedConstantRelocatablePIC(Ctx->getGlobalString(GlobalOffsetTable), T,
@@ -2039,7 +2039,7 @@
     PostLoweringLegalizer Legalizer(this);
     while (!Context.atEnd()) {
       PostIncrLoweringContext PostIncrement(Context);
-      Inst *CurInstr = Context.getCur();
+      Inst *CurInstr = iteratorToInst(Context.getCur());
 
       // Check if the previous TempBaseReg is clobbered, and reset if needed.
       Legalizer.resetTempBaseIfClobberedBy(CurInstr);
@@ -5750,7 +5750,7 @@
 }
 
 void TargetARM32::doAddressOptLoad() {
-  Inst *Instr = Context.getCur();
+  Inst *Instr = iteratorToInst(Context.getCur());
   assert(llvm::isa<InstLoad>(Instr));
   Variable *Dest = Instr->getDest();
   Operand *Addr = Instr->getSrc(0);
@@ -5912,7 +5912,7 @@
 }
 
 void TargetARM32::doAddressOptStore() {
-  Inst *Instr = Context.getCur();
+  Inst *Instr = iteratorToInst(Context.getCur());
   assert(llvm::isa<InstStore>(Instr));
   Operand *Src = Instr->getSrc(0);
   Operand *Addr = Instr->getSrc(1);
diff --git a/src/PNaClTranslator.cpp b/src/PNaClTranslator.cpp
index 69498ce..95619a0 100644
--- a/src/PNaClTranslator.cpp
+++ b/src/PNaClTranslator.cpp
@@ -2970,6 +2970,9 @@
 }
 
 bool FunctionParser::ParseBlock(unsigned BlockID) {
+#ifndef PNACL_LLVM
+  constexpr bool PNaClAllowLocalSymbolTables = true;
+#endif // !PNACL_LLVM
   switch (BlockID) {
   case naclbitc::CONSTANTS_BLOCK_ID: {
     ConstantsParser Parser(BlockID, this);
diff --git a/src/SZTargets.def b/src/SZTargets.def
index b07ac47..15fcb96 100644
--- a/src/SZTargets.def
+++ b/src/SZTargets.def
@@ -14,6 +14,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+#ifdef PNACL_LLVM
+
 #ifdef SZTARGET
 
 #ifndef SUBZERO_TARGET
@@ -29,3 +31,12 @@
 #include "llvm/Config/SZTargets.def"
 
 #endif // !SZTARGET
+
+#else // !PNACL_LLVM
+
+SUBZERO_TARGET(ARM32)
+SUBZERO_TARGET(MIPS32)
+SUBZERO_TARGET(X8632)
+SUBZERO_TARGET(X8664)
+
+#endif // !PNACL_LLVM