Index: docs/LangRef.rst =================================================================== --- docs/LangRef.rst +++ docs/LangRef.rst @@ -5610,6 +5610,17 @@ !0 = !{!"llvm.loop.licm_versioning.disable"} +'``llvm.loop.licm.disable``' Metadata +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +This metadata indicates that loop-invariant code motion (LICM) should not be +performed on this loop. The metadata has a single operand which is the string +``llvm.loop.licm.disable``. For example: + +.. code-block:: llvm + + !0 = !{!"llvm.loop.licm.disable"} + '``llvm.loop.distribute.enable``' Metadata ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Index: include/llvm/Transforms/Utils/LoopUtils.h =================================================================== --- include/llvm/Transforms/Utils/LoopUtils.h +++ include/llvm/Transforms/Utils/LoopUtils.h @@ -215,6 +215,9 @@ /// Look for the loop attribute that disables all transformation heuristic. bool hasDisableAllTransformsHint(const Loop *L); +/// Look for the loop attribute that disables the LICM transformation heuristics. +bool hasDisableLICMTransformsHint(const Loop *L); + /// The mode sets how eager a transformation should be applied. enum TransformationMode { /// The pass can use heuristics to determine whether a transformation should Index: lib/Transforms/Scalar/LICM.cpp =================================================================== --- lib/Transforms/Scalar/LICM.cpp +++ lib/Transforms/Scalar/LICM.cpp @@ -330,6 +330,12 @@ assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form."); + // If this loop has metadata indicating that LICM is not to be performed then + // just exit. + if (hasDisableLICMTransformsHint(L)) { + return false; + } + std::unique_ptr CurAST; std::unique_ptr MSSAU; bool NoOfMemAccTooLarge = false; Index: lib/Transforms/Utils/LoopUtils.cpp =================================================================== --- lib/Transforms/Utils/LoopUtils.cpp +++ lib/Transforms/Utils/LoopUtils.cpp @@ -45,6 +45,7 @@ #define DEBUG_TYPE "loop-utils" static const char *LLVMLoopDisableNonforced = "llvm.loop.disable_nonforced"; +static const char *LLVMLoopDisableLICM = "llvm.loop.licm.disable"; bool llvm::formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, @@ -332,6 +333,10 @@ return getBooleanLoopAttribute(L, LLVMLoopDisableNonforced); } +bool llvm::hasDisableLICMTransformsHint(const Loop *L) { + return getBooleanLoopAttribute(L, LLVMLoopDisableLICM); +} + TransformationMode llvm::hasUnrollTransformation(Loop *L) { if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable")) return TM_SuppressedByUser; Index: test/Transforms/LICM/pragma-licm-disable.ll =================================================================== --- /dev/null +++ test/Transforms/LICM/pragma-licm-disable.ll @@ -0,0 +1,52 @@ +; Check that the LICM pass does not operate on a loop which has the +; llvm.loop.licm.disable metadata. +; This test is simply a modified copy of constexpr.ll + +; RUN: opt < %s -S -basicaa -licm | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-windows-msvc" + +@in = internal unnamed_addr global i32* null, align 8 +@out = internal unnamed_addr global i32* null, align 8 + +; CHECK-LABEL: @licm_disable +; CHECK: entry: +; CHECK-NOT: load +; CHECK: do.body: +; CHECK: load i64, i64* bitcast (i32** @in to i64*) + +define i64 @licm_disable(i32 %N) { +entry: + br label %do.body + +do.body: ; preds = %l2, %entry + %i.0 = phi i32 [ 0, %entry ], [ %inc, %l2 ] + %total = phi i64 [ 0, %entry ], [ %next, %l2 ] + %c = icmp eq i32 %N, 6 + br i1 %c, label %l1, label %do.body.l2_crit_edge + +do.body.l2_crit_edge: ; preds = %do.body + %inval.pre = load i32*, i32** @in, align 8 + br label %l2 + +l1: ; preds = %do.body + %v1 = load i64, i64* bitcast (i32** @in to i64*), align 8 + store i64 %v1, i64* bitcast (i32** @out to i64*), align 8 + %0 = inttoptr i64 %v1 to i32* + br label %l2 + +l2: ; preds = %do.body.l2_crit_edge, %l1 + %inval = phi i32* [ %inval.pre, %do.body.l2_crit_edge ], [ %0, %l1 ] + %int = ptrtoint i32* %inval to i64 + %next = add i64 %total, %int + %inc = add nsw i32 %i.0, 1 + %cmp = icmp slt i32 %inc, %N + br i1 %cmp, label %do.body, label %do.end, !llvm.loop !1 + +do.end: ; preds = %l2 + ret i64 %total +} +!1 = !{!1, !2} +!2 = !{!"llvm.loop.licm.disable"} +