bridge: handle conversion expression applied on AuxT

We use AuxT to wrap an LLVM type used in LLVM intrinsics. A value
of such type could be fed into a conversion expression, like
uint64(__builtin_ctz(x)). materializeConversion needs to handle
this case.

For AuxT, we don't know if it is signed or not. For now we simply
assume it is signed. In most cases it doesn't matter. If this
turns to be an issue, we should define the correct Btype for
intrinsics.

Change-Id: If60c5ccb3e6aa1c4bf0cb985abbbce9f019456e5
Reviewed-on: https://go-review.googlesource.com/c/gollvm/+/176978
Reviewed-by: Than McIntosh <thanm@google.com>
diff --git a/bridge/go-llvm-materialize.cpp b/bridge/go-llvm-materialize.cpp
index 5bb59a3..249dccd 100644
--- a/bridge/go-llvm-materialize.cpp
+++ b/bridge/go-llvm-materialize.cpp
@@ -263,6 +263,10 @@
   }
 
   // Integer-to-integer conversions
+  // FIXME: the type of the the operand could be AuxT, which we use
+  // to wrap an LLVM type for intrinsics. Assume it is signed for now.
+  // If this turns to be an issue, we should define the correct Btype
+  // for intrinsics.
   if (valType->isIntegerTy() && toType->isIntegerTy()) {
     llvm::IntegerType *valIntTyp =
         llvm::cast<llvm::IntegerType>(valType);
@@ -273,7 +277,8 @@
     llvm::Value *conv = nullptr;
     if (tobits > valbits) {
       if (expr->btype()->type() == llvmBoolType() ||
-          expr->btype()->castToBIntegerType()->isUnsigned())
+          (expr->btype()->castToBIntegerType() &&
+           expr->btype()->castToBIntegerType()->isUnsigned()))
         conv = builder.CreateZExt(val, toType, namegen("zext"));
       else
         conv = builder.CreateSExt(val, toType, namegen("sext"));
@@ -308,7 +313,8 @@
   // Integer -> float conversions
   if (toType->isFloatingPointTy() && valType->isIntegerTy()) {
     llvm::Value *conv = nullptr;
-    if (expr->btype()->castToBIntegerType()->isUnsigned())
+    if (expr->btype()->castToBIntegerType() &&
+        expr->btype()->castToBIntegerType()->isUnsigned())
       conv = builder.CreateUIToFP(val, toType, namegen("uitof"));
     else
       conv = builder.CreateSIToFP(val, toType, namegen("sitof"));