[llvm] r347325 - [LoopSink] Add preheader to alias set
Guozhi Wei via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 20 08:49:08 PST 2018
Author: carrot
Date: Tue Nov 20 08:49:07 2018
New Revision: 347325
URL: http://llvm.org/viewvc/llvm-project?rev=347325&view=rev
Log:
[LoopSink] Add preheader to alias set
This patch fixes PR39695.
The original LoopSink only considers memory alias in loop body. But PR39695 shows that instructions following sink candidate in preheader should also be checked. This is a conservative patch, it simply adds whole preheader block to alias set. It may lose some optimization opportunity, but I think that is very rare because: 1 in the most common case st/ld to the same address, the load should already be optimized away. 2 usually preheader is not very large.
Differential Revision: https://reviews.llvm.org/D54659
Added:
llvm/trunk/test/Transforms/LICM/loopsink-pr39695.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/LoopSink.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/LoopSink.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopSink.cpp?rev=347325&r1=347324&r2=347325&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopSink.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopSink.cpp Tue Nov 20 08:49:07 2018
@@ -280,6 +280,7 @@ static bool sinkLoopInvariantInstruction
// Compute alias set.
for (BasicBlock *BB : L.blocks())
CurAST.add(*BB);
+ CurAST.add(*Preheader);
// Sort loop's basic blocks by frequency
SmallVector<BasicBlock *, 10> ColdLoopBBs;
Added: llvm/trunk/test/Transforms/LICM/loopsink-pr39695.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LICM/loopsink-pr39695.ll?rev=347325&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/LICM/loopsink-pr39695.ll (added)
+++ llvm/trunk/test/Transforms/LICM/loopsink-pr39695.ll Tue Nov 20 08:49:07 2018
@@ -0,0 +1,37 @@
+; RUN: opt -S -loop-sink < %s | FileCheck %s
+
+; The load instruction should not be sunk into following loop.
+; CHECK: @foo
+; CHECK-NEXT: entry
+; CHECK-NEXT: %ptr = load i8*, i8** %pp, align 8
+; CHECK-NEXT: store i8* null, i8** %pp, align 8
+
+define i32 @foo(i32 %n, i8** %pp) !prof !0 {
+entry:
+ %ptr = load i8*, i8** %pp, align 8
+ store i8* null, i8** %pp, align 8
+ br label %for.cond
+
+for.cond: ; preds = %for.body, %entry
+ %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+ %cmp = icmp ult i32 %i.0, %n
+ br i1 %cmp, label %for.body, label %for.end, !prof !1
+
+for.body: ; preds = %for.cond
+ %0 = sext i32 %i.0 to i64
+ %arrayidx = getelementptr inbounds i8, i8* %ptr, i64 %0
+ %1 = load i8, i8* %arrayidx, align 1
+ %or19 = call i8 @llvm.bitreverse.i8(i8 %1)
+ %v = sext i8 %or19 to i32
+ %inc = add i32 %i.0, %v
+ br label %for.cond
+
+for.end: ; preds = %for.cond
+ ret i32 %i.0
+}
+
+declare i8 @llvm.bitreverse.i8(i8) #0
+attributes #0 = { nounwind readnone speculatable }
+
+!0 = !{!"function_entry_count", i64 1}
+!1 = !{!"branch_weights", i32 1, i32 2000}
More information about the llvm-commits
mailing list