diff --git a/llvm/include/llvm/Transforms/Utils/LoopVersioning.h b/llvm/include/llvm/Transforms/Utils/LoopVersioning.h --- a/llvm/include/llvm/Transforms/Utils/LoopVersioning.h +++ b/llvm/include/llvm/Transforms/Utils/LoopVersioning.h @@ -60,11 +60,24 @@ /// analyze L /// if versioning is necessary version L /// transform L - void versionLoop() { versionLoop(findDefsUsedOutsideOfLoop(VersionedLoop)); } + void versionLoop() { versionLoop(findDefsUsedOutsideOfLoop(VersionedLoop), false); } + + /// Clone loops with runtime check. After this function call, the LLVM IR + /// would look like the following: + /// + /// if (true) { + /// NonVersionedLoop + /// } else { + /// VersionedLoop + /// } + void versionLoopWithPlainRuntimeCheck() { + versionLoop(findDefsUsedOutsideOfLoop(VersionedLoop), true); + } /// Same but if the client has already precomputed the set of values /// used outside the loop, this API will allows passing that. - void versionLoop(const SmallVectorImpl &DefsUsedOutside); + void versionLoop(const SmallVectorImpl &DefsUsedOutside, + bool AddPlainRuntimeCheck = false); /// Returns the versioned loop. Control flows here if pointers in the /// loop don't alias (i.e. all memchecks passed). (This loop is actually the diff --git a/llvm/lib/Transforms/Utils/LoopVersioning.cpp b/llvm/lib/Transforms/Utils/LoopVersioning.cpp --- a/llvm/lib/Transforms/Utils/LoopVersioning.cpp +++ b/llvm/lib/Transforms/Utils/LoopVersioning.cpp @@ -48,7 +48,8 @@ } void LoopVersioning::versionLoop( - const SmallVectorImpl &DefsUsedOutside) { + const SmallVectorImpl &DefsUsedOutside, + bool AddPlainRuntimeCheck) { assert(VersionedLoop->isLoopSimplifyForm() && "Loop is not in loop-simplify form"); @@ -85,8 +86,13 @@ } else RuntimeCheck = MemRuntimeCheck ? MemRuntimeCheck : SCEVRuntimeCheck; - assert(RuntimeCheck && "called even though we don't need " - "any runtime checks"); + if (AddPlainRuntimeCheck) { + RuntimeCheck = ConstantInt::getTrue(RuntimeCheckBB->getContext()); + } + + assert(RuntimeCheck && "called versionLoop even though we don't need " + "any runtime checks" + "use versionLoopWithPlainRuntimeCheck instead"); // Rename the block to make the IR more readable. RuntimeCheckBB->setName(VersionedLoop->getHeader()->getName() +