[LICM] Hoist loads with invariant.group metadata
authorArthur Eubanks <aeubanks@google.com>
Fri, 2 Apr 2021 03:22:29 +0000 (20:22 -0700)
committerArthur Eubanks <aeubanks@google.com>
Fri, 9 Apr 2021 04:57:37 +0000 (21:57 -0700)
Previously loading the vtable used in calling a virtual method in a loop
was not hoisted out of the loop. This fixes that.

canSinkOrHoistInst() itself doesn't check that the load operands are
loop invariant, callers also check that separately.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D99784

llvm/lib/Transforms/Scalar/LICM.cpp
llvm/test/Transforms/LICM/invariant.group.ll [new file with mode: 0644]

index 587b58f..0692e4c 100644 (file)
@@ -1198,6 +1198,10 @@ bool llvm::canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT,
     if (isLoadInvariantInLoop(LI, DT, CurLoop))
       return true;
 
+    // Stores with an invariant.group metadata are ok to sink/hoist.
+    if (LI->hasMetadata(LLVMContext::MD_invariant_group))
+      return true;
+
     bool Invalidated;
     if (CurAST)
       Invalidated = pointerInvalidatedByLoop(MemoryLocation::get(LI), CurAST,
diff --git a/llvm/test/Transforms/LICM/invariant.group.ll b/llvm/test/Transforms/LICM/invariant.group.ll
new file mode 100644 (file)
index 0000000..0d33ac1
--- /dev/null
@@ -0,0 +1,58 @@
+; RUN: opt -S < %s -passes=licm | FileCheck %s
+
+declare i8* @llvm.launder.invariant.group.p0i8(i8* %a)
+
+; CHECK-LABEL: define{{.*}}@f
+define void @f(i32* %x) {
+; CHECK: entry:
+; CHECK-NOT: {{.*}}:
+; CHECK: load {{.*}} !invariant.group
+entry:
+  %x_i8 = bitcast i32* %x to i8*
+  %x_i8_inv = call i8* @llvm.launder.invariant.group.p0i8(i8* %x_i8)
+  br label %for.body
+
+for.cond.cleanup:
+  ret void
+
+for.body:
+  %i.04 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+
+  %x_inv = bitcast i8* %x_i8_inv to i32*
+  %0 = load i32, i32* %x_inv, !invariant.group !0
+
+  call void @a(i32 %0)
+  %inc = add nuw nsw i32 %i.04, 1
+  %exitcond.not = icmp eq i32 %inc, 100
+  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
+}
+
+; CHECK-LABEL: define{{.*}}@g
+define void @g(i32* %x) {
+; CHECK: for.body:
+; CHECK-NOT: {{.*}}:
+; CHECK: load {{.*}} !invariant.group
+entry:
+  %x_i8 = bitcast i32* %x to i8*
+  br label %for.body
+
+for.cond.cleanup:
+  ret void
+
+for.body:
+  %i.04 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+
+  %x_i8_inv = call i8* @llvm.launder.invariant.group.p0i8(i8* %x_i8)
+  %x_inv = bitcast i8* %x_i8_inv to i32*
+
+  %0 = load i32, i32* %x_inv, !invariant.group !0
+
+  call void @a(i32 %0)
+  %inc = add nuw nsw i32 %i.04, 1
+  %exitcond.not = icmp eq i32 %inc, 100
+  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
+}
+
+declare void @a(i32)
+
+!0 = !{}