gollvm: sync with LLVM trunk

Sync with LLVM trunk at 94d912296de. Main change is to update the
bridge to eliminate uses of the now-removed llvm::CompositeType
class.

Change-Id: I52f053783c70e92f971ccc3555238a7f9b3cb74c
Reviewed-on: https://go-review.googlesource.com/c/gollvm/+/226637
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/bridge/go-llvm-materialize.cpp b/bridge/go-llvm-materialize.cpp
index d833706..27a826f 100644
--- a/bridge/go-llvm-materialize.cpp
+++ b/bridge/go-llvm-materialize.cpp
@@ -853,7 +853,7 @@
   llvm::Type *llt = btype->type();
   unsigned numElements = 0;
   assert(llt->isStructTy() || llt->isArrayTy());
-  llvm::CompositeType *llct = nullptr;
+  llvm::Type *llct = nullptr;
   if (llt->isStructTy()) {
     llvm::StructType *llst = llvm::cast<llvm::StructType>(llt);
     numElements = llst->getNumElements();
@@ -877,7 +877,7 @@
 
 Bexpression *
 Llvm_backend::makeDelayedCompositeExpr(Btype *btype,
-                                       llvm::CompositeType *llct,
+                                       llvm::Type *llct,
                                        unsigned numElements,
                                        const std::vector<unsigned long> *indexes,
                                        const std::vector<Bexpression *> &vals,
@@ -916,7 +916,7 @@
 
 Bexpression *
 Llvm_backend::makeConstCompositeExpr(Btype *btype,
-                                     llvm::CompositeType *llct,
+                                     llvm::Type *llct,
                                      unsigned numElements,
                                      const std::vector<unsigned long> *indexes,
                                      const std::vector<Bexpression *> &vals,
@@ -949,7 +949,7 @@
           touched.insert(idx);
         Bexpression *bex = vals[ii];
         llvm::Constant *con = llvm::cast<llvm::Constant>(bex->value());
-        llvm::Type *elt = llct->getTypeAtIndex(ii);
+        llvm::Type *elt = TypeManager::getLlvmTypeAtIndex(llct, ii);
         if (elt != con->getType()) {
           con = genConvertedConstant(con, elt);
           assert(con != nullptr);
@@ -959,7 +959,7 @@
       if (numElements != nvals) {
         for (unsigned long ii = 0; ii < numElements; ++ii) {
           if (touched.find(ii) == touched.end()) {
-            llvm::Type *elt = llct->getTypeAtIndex(ii);
+            llvm::Type *elt = TypeManager::getLlvmTypeAtIndex(llct, ii);
             llvals[ii] = llvm::Constant::getNullValue(elt);
           }
         }
@@ -967,7 +967,7 @@
     } else {
       for (unsigned long ii = 0; ii < numElements; ++ii) {
         llvm::Constant *con = llvm::cast<llvm::Constant>(vals[ii]->value());
-        llvm::Type *elt = llct->getTypeAtIndex(ii);
+        llvm::Type *elt = TypeManager::getLlvmTypeAtIndex(llct, ii);
         if (elt != con->getType()) {
           con = genConvertedConstant(con, elt);
           assert(con != nullptr);
diff --git a/bridge/go-llvm-typemanager.cpp b/bridge/go-llvm-typemanager.cpp
index a74ff99..513e34f 100644
--- a/bridge/go-llvm-typemanager.cpp
+++ b/bridge/go-llvm-typemanager.cpp
@@ -1382,6 +1382,23 @@
   return llft;
 }
 
+bool TypeManager::isLlvmCompositeType(llvm::Type *t)
+{
+  return llvm::isa<llvm::StructType>(t) || llvm::isa<llvm::SequentialType>(t);
+}
+
+llvm::Type *TypeManager::getLlvmTypeAtIndex(llvm::Type *t, unsigned i)
+{
+  if (llvm::isa<llvm::StructType>(t)) {
+    llvm::StructType *st = llvm::cast<llvm::StructType>(t);
+    return st->getTypeAtIndex(i);
+  } else {
+    assert(llvm::isa<llvm::SequentialType>(t));
+    llvm::SequentialType *st = llvm::cast<llvm::SequentialType>(t);
+    return st->getElementType();
+  }
+}
+
 llvm::Type *TypeManager::placeholderProxyType(Btype *typ,
                                               pproxymap *pmap)
 {
@@ -1614,7 +1631,7 @@
   if (! elt->isArrayTy())
     return false;
   llvm::ArrayType *llat = llvm::cast<llvm::ArrayType>(elt);
-  llvm::Type *aelt = llat->getTypeAtIndex(0u);
+  llvm::Type *aelt = llat->getElementType();
   if (aelt == arElmTyp)
     return true;
   if (isCircularFunctionType(aelt) && isCircularFunctionType(arElmTyp))
diff --git a/bridge/go-llvm-typemanager.h b/bridge/go-llvm-typemanager.h
index 57d391f..789b5d5 100644
--- a/bridge/go-llvm-typemanager.h
+++ b/bridge/go-llvm-typemanager.h
@@ -130,6 +130,10 @@
   llvm::Type *landingPadExceptionType();
   llvm::FunctionType *personalityFunctionType();
 
+  // Composite LLVM type helpers.
+  static bool isLlvmCompositeType(llvm::Type *t);
+  static llvm::Type *getLlvmTypeAtIndex(llvm::Type *t, unsigned i);
+
   // Size calculation methods for LLVM types.
 
   // Returns the offset in bytes between successive objects of a
diff --git a/bridge/go-llvm.cpp b/bridge/go-llvm.cpp
index 1379198..bd00cdc 100644
--- a/bridge/go-llvm.cpp
+++ b/bridge/go-llvm.cpp
@@ -743,8 +743,8 @@
     return fromVal;
 
   // There must be agreement in type class (struct -> struct, etc).
-  bool isFromComposite = llvm::isa<llvm::CompositeType>(llFromType);
-  bool isToComposite = llvm::isa<llvm::CompositeType>(llToType);
+  bool isFromComposite = TypeManager::isLlvmCompositeType(llFromType);
+  bool isToComposite = TypeManager::isLlvmCompositeType(llToType);
   if (isFromComposite != isToComposite)
     return nullptr;
 
@@ -789,16 +789,10 @@
   if (it != genConvConstMap_.end())
     return it->second;
 
-  // Grab from/to types as composites.
-  llvm::CompositeType *llFromCT = llvm::cast<llvm::CompositeType>(llFromType);
-  llvm::CompositeType *llToCT = llvm::cast<llvm::CompositeType>(llToType);
-  assert(llFromCT != nullptr);
-  assert(llToCT != nullptr);
-
   // Walk through the child values and convert them.
   llvm::SmallVector<llvm::Constant *, 64> newvals(numElements);
   for (unsigned idx = 0; idx < numElements; idx++) {
-    llvm::Type *toEltType = llToCT->getTypeAtIndex(idx);
+    llvm::Type *toEltType = TypeManager::getLlvmTypeAtIndex(llToType, idx);
     llvm::Constant *constElt = fromVal->getAggregateElement(idx);
     llvm::Constant *convElt = genConvertedConstant(constElt, toEltType);
     if (convElt == nullptr)
diff --git a/bridge/go-llvm.h b/bridge/go-llvm.h
index 947a13a..5cc83a5 100644
--- a/bridge/go-llvm.h
+++ b/bridge/go-llvm.h
@@ -460,7 +460,7 @@
 
   // Helper for creating a constant-valued array/struct expression.
   Bexpression *makeConstCompositeExpr(Btype *btype,
-                                      llvm::CompositeType *llct,
+                                      llvm::Type *llct,
                                       unsigned numElements,
                                       const std::vector<unsigned long> *indexes,
                                       const std::vector<Bexpression *> &vals,
@@ -468,7 +468,7 @@
 
   // Helper for creating a non-constant-valued array or struct expression.
   Bexpression *makeDelayedCompositeExpr(Btype *btype,
-                                        llvm::CompositeType *llct,
+                                        llvm::Type *llct,
                                         unsigned numElements,
                                         const std::vector<unsigned long> *idxs,
                                         const std::vector<Bexpression *> &vals,
diff --git a/driver/CompileGo.cpp b/driver/CompileGo.cpp
index 0393812..59b2f3d 100644
--- a/driver/CompileGo.cpp
+++ b/driver/CompileGo.cpp
@@ -56,6 +56,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
diff --git a/driver/Driver.cpp b/driver/Driver.cpp
index 8db03f2..c69f3c9 100644
--- a/driver/Driver.cpp
+++ b/driver/Driver.cpp
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Program.h"