Index: include/llvm/Analysis/LoopAccessAnalysis.h =================================================================== --- include/llvm/Analysis/LoopAccessAnalysis.h +++ include/llvm/Analysis/LoopAccessAnalysis.h @@ -102,6 +102,8 @@ // Can vectorize safely without RT checks. All dependences are known to be // safe. Safe, + // Can vectorize with RT checks to overcome unknown dependencies. + SafeWithRtChecks, // Cannot vectorize due to known unsafe dependencies. Unsafe, }; @@ -218,7 +220,7 @@ /// vectorize the loop with a dynamic array access check. bool shouldRetryWithRuntimeCheck() const { return ShouldRetryWithRuntimeCheck && - Status == VectorizationSafetyStatus::Unsafe; + Status == VectorizationSafetyStatus::SafeWithRtChecks; } /// Returns the memory dependences. If null is returned we exceeded @@ -284,7 +286,8 @@ bool ShouldRetryWithRuntimeCheck; /// Result of the dependence checks, indicating whether the checked - /// dependences are safe for vectorization or not. + /// dependences are safe for vectorization, require RT checks or are known to + /// be unsafe. VectorizationSafetyStatus Status; //// True if Dependences reflects the dependences in the @@ -320,7 +323,7 @@ bool couldPreventStoreLoadForward(uint64_t Distance, uint64_t TypeByteSize); /// Updates the current safety status with \p S. We can go from Safe to - /// to Unsafe. + /// either SafeWithRtChecks or Unsafe and from SafeWithRtChecks to Unsafe. void mergeInStatus(VectorizationSafetyStatus S); }; Index: lib/Analysis/LoopAccessAnalysis.cpp =================================================================== --- lib/Analysis/LoopAccessAnalysis.cpp +++ lib/Analysis/LoopAccessAnalysis.cpp @@ -1230,6 +1230,7 @@ return VectorizationSafetyStatus::Safe; case Unknown: + return VectorizationSafetyStatus::SafeWithRtChecks; case ForwardButPreventsForwarding: case Backward: case BackwardVectorizableButPreventsForwarding: Index: test/Transforms/LoopVectorize/runtime-check.ll =================================================================== --- test/Transforms/LoopVectorize/runtime-check.ll +++ test/Transforms/LoopVectorize/runtime-check.ll @@ -117,7 +117,9 @@ ret void } -; Check we do generate unnecessary runtime checks. They will always fail. +; Check we do not generate runtime checks if we found a known dependence preventing +; vectorization. In this case, it is a read of c[i-1] followed by a write of c[i]. +; The runtime checks would always fail. ; void test_runtime_check2(float *a, float b, unsigned offset, unsigned offset2, unsigned n, float *c) { ; for (unsigned i = 1; i < n; i++) { @@ -127,7 +129,7 @@ ; } ; ; CHECK-LABEL: test_runtime_check2 -; CHECK: <4 x float> +; CHECK-NOT: <4 x float> define void @test_runtime_check2(float* %a, float %b, i64 %offset, i64 %offset2, i64 %n, float* %c) { entry: br label %for.body