gollvm: fix for enabling vectorization

Fix the pass manager construction code to enable vectorization
based on optimization level (has to be turned on explicitly).
While doing this, add support for the -fvectorize/-fno-vectorize
and -fslp-vectorize/-fno-slp-vectorize command line options.

Change-Id: I6021f267c4ed46348aa106a1d14c3c488517ef16
Reviewed-on: https://go-review.googlesource.com/c/145018
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/driver/CompileGo.cpp b/driver/CompileGo.cpp
index b727162..77ae703 100644
--- a/driver/CompileGo.cpp
+++ b/driver/CompileGo.cpp
@@ -126,6 +126,9 @@
   // Helpers for -### output
   void dumpArg(opt::Arg &arg, bool doquote);
   void quoteDump(const std::string &str, bool doquote);
+
+  // Misc
+  bool enableVectorization(bool slp);
 };
 
 CompileGoImpl::CompileGoImpl(ToolChain &tc, const std::string &executablePath)
@@ -673,8 +676,21 @@
   return true;
 }
 
+bool CompileGoImpl::enableVectorization(bool slp)
+{
+  bool enable = (olvl_ > 1);
+  if (slp)
+    return driver_.reconcileOptionPair(gollvm::options::OPT_fslp_vectorize,
+                                       gollvm::options::OPT_fno_slp_vectorize,
+                                       enable);
+  else
+    return driver_.reconcileOptionPair(gollvm::options::OPT_fvectorize,
+                                       gollvm::options::OPT_fno_vectorize,
+                                       enable);
+}
+
 void CompileGoImpl::createPasses(legacy::PassManager &MPM,
-                                           legacy::FunctionPassManager &FPM)
+                                 legacy::FunctionPassManager &FPM)
 {
   if (args_.hasArg(gollvm::options::OPT_disable_llvm_passes))
     return;
@@ -697,6 +713,8 @@
   pmb.SizeLevel = 0; // TODO: decide on right value here
   pmb.PrepareForThinLTO = false;
   pmb.PrepareForLTO = false;
+  pmb.SLPVectorize = enableVectorization(true);
+  pmb.LoopVectorize = enableVectorization(false);
 
   FPM.add(new TargetLibraryInfoWrapperPass(*tlii_));
   if (! args_.hasArg(gollvm::options::OPT_noverify))
diff --git a/driver/GollvmOptions.td b/driver/GollvmOptions.td
index 1fe3d13..aa026a0 100644
--- a/driver/GollvmOptions.td
+++ b/driver/GollvmOptions.td
@@ -249,6 +249,16 @@
 def fno_inline : Flag<["-"], "fno-inline">, Group<f_Group>,
   HelpText<"Disable inlining">;
 
+def fvectorize : Flag<["-"], "fvectorize">, Group<f_Group>,
+  HelpText<"Enable the loop vectorization passes">;
+def fno_vectorize : Flag<["-"], "fno-vectorize">, Group<f_Group>,
+  HelpText<"Disable the loop vectorization passes">;
+
+def fslp_vectorize : Flag<["-"], "fslp-vectorize">, Group<f_Group>,
+  HelpText<"Enable the superword-level parallelism vectorization passes">;
+def fno_slp_vectorize : Flag<["-"], "fno-slp-vectorize">, Group<f_Group>,
+  HelpText<"Disable the superword-level parallelism vectorization passes">;
+
 def ffp_contract_EQ : Joined<["-"], "ffp-contract=">, Group<f_Group>,
   HelpText<"Form fused FP ops (e.g. FMAs): fast (everywhere)"
   " | on (according to FP_CONTRACT pragma, default) | off (never fuse)">,