Disable loop vectorizer unrolling when no unrolling requested

In addition to the regular loop unrolling transformation, the loop vectorizer
can also unroll loops. If no unrolling has specifically been requested (by
-fno-unroll-loops), and the loop vectorizer will be used, then add the backend
option to (also) prevent the loop vectorizer from unrolling loops.

I confirmed with Nadav (off list) that disabling vectorizer loop unrolling when
-fno-unroll-loops is provided is the desired behavior.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189440 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index c30c45d..89df632 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -3382,9 +3382,19 @@
   OptSpecifier VectorizeAliasOption = EnableVec ? options::OPT_O_Group :
     options::OPT_fvectorize;
   if (Args.hasFlag(options::OPT_fvectorize, VectorizeAliasOption,
-                   options::OPT_fno_vectorize, EnableVec))
+                   options::OPT_fno_vectorize, EnableVec)) {
     CmdArgs.push_back("-vectorize-loops");
 
+    // In addition to the regular loop unrolling transformation, the loop
+    // vectorizer can also unroll loops. If no unrolling has specifically been
+    // requested, then also prevent the loop vectorizer from unrolling loops.
+    if (Args.hasFlag(options::OPT_fno_unroll_loops,
+        options::OPT_funroll_loops, false)) {
+      CmdArgs.push_back("-backend-option");
+      CmdArgs.push_back("-force-vector-unroll=1");
+    }
+  }
+
   // -fslp-vectorize is default.
   if (Args.hasFlag(options::OPT_fslp_vectorize,
                    options::OPT_fno_slp_vectorize, true))