[llvm] 42229b9 - [LICM] Only create load in pre-header when promoting load.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 11 07:45:35 PDT 2022
Author: Florian Hahn
Date: 2022-04-11T16:45:18+02:00
New Revision: 42229b96bf94ec896d5c62fa643d83ba96e86eea
URL: https://github.com/llvm/llvm-project/commit/42229b96bf94ec896d5c62fa643d83ba96e86eea
DIFF: https://github.com/llvm/llvm-project/commit/42229b96bf94ec896d5c62fa643d83ba96e86eea.diff
LOG: [LICM] Only create load in pre-header when promoting load.
When only a store is sunk, there is no need to create a load in the
pre-header, as the result of the load will never get used.
The dead load can can introduce UB, if the function is marked as
writeonly.
Fixes #51248.
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D123473
Added:
Modified:
llvm/lib/Transforms/Scalar/LICM.cpp
llvm/test/Transforms/LICM/scalar-promote.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp
index 74ae5977a3663..2f036d676e8e9 100644
--- a/llvm/lib/Transforms/Scalar/LICM.cpp
+++ b/llvm/lib/Transforms/Scalar/LICM.cpp
@@ -2156,21 +2156,24 @@ bool llvm::promoteLoopAccessesToScalars(
// Set up the preheader to have a definition of the value. It is the live-out
// value from the preheader that uses in the loop will use.
- LoadInst *PreheaderLoad = new LoadInst(
- AccessTy, SomePtr, SomePtr->getName() + ".promoted",
- Preheader->getTerminator());
- if (SawUnorderedAtomic)
- PreheaderLoad->setOrdering(AtomicOrdering::Unordered);
- PreheaderLoad->setAlignment(Alignment);
- PreheaderLoad->setDebugLoc(DebugLoc());
- if (AATags)
- PreheaderLoad->setAAMetadata(AATags);
- SSA.AddAvailableValue(Preheader, PreheaderLoad);
-
- MemoryAccess *PreheaderLoadMemoryAccess = MSSAU.createMemoryAccessInBB(
- PreheaderLoad, nullptr, PreheaderLoad->getParent(), MemorySSA::End);
- MemoryUse *NewMemUse = cast<MemoryUse>(PreheaderLoadMemoryAccess);
- MSSAU.insertUse(NewMemUse, /*RenameUses=*/true);
+ LoadInst *PreheaderLoad = nullptr;
+ if (FoundLoadToPromote) {
+ PreheaderLoad =
+ new LoadInst(AccessTy, SomePtr, SomePtr->getName() + ".promoted",
+ Preheader->getTerminator());
+ if (SawUnorderedAtomic)
+ PreheaderLoad->setOrdering(AtomicOrdering::Unordered);
+ PreheaderLoad->setAlignment(Alignment);
+ PreheaderLoad->setDebugLoc(DebugLoc());
+ if (AATags)
+ PreheaderLoad->setAAMetadata(AATags);
+
+ MemoryAccess *PreheaderLoadMemoryAccess = MSSAU.createMemoryAccessInBB(
+ PreheaderLoad, nullptr, PreheaderLoad->getParent(), MemorySSA::End);
+ MemoryUse *NewMemUse = cast<MemoryUse>(PreheaderLoadMemoryAccess);
+ MSSAU.insertUse(NewMemUse, /*RenameUses=*/true);
+ SSA.AddAvailableValue(Preheader, PreheaderLoad);
+ }
if (VerifyMemorySSA)
MSSAU.getMemorySSA()->verifyMemorySSA();
@@ -2181,7 +2184,7 @@ bool llvm::promoteLoopAccessesToScalars(
if (VerifyMemorySSA)
MSSAU.getMemorySSA()->verifyMemorySSA();
// If the SSAUpdater didn't use the load in the preheader, just zap it now.
- if (PreheaderLoad->use_empty())
+ if (PreheaderLoad && PreheaderLoad->use_empty())
eraseInstruction(*PreheaderLoad, *SafetyInfo, MSSAU);
return true;
diff --git a/llvm/test/Transforms/LICM/scalar-promote.ll b/llvm/test/Transforms/LICM/scalar-promote.ll
index ad60bc87b910a..78cbbeb2c9c20 100644
--- a/llvm/test/Transforms/LICM/scalar-promote.ll
+++ b/llvm/test/Transforms/LICM/scalar-promote.ll
@@ -605,10 +605,9 @@ Out:
define void @test_sink_store_only() writeonly {
; CHECK-LABEL: @test_sink_store_only(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[GLB_PROMOTED:%.*]] = load i8, i8* @glb, align 1
; CHECK-NEXT: br label [[LOOP_HEADER:%.*]]
; CHECK: loop.header:
-; CHECK-NEXT: [[DIV1:%.*]] = phi i8 [ [[GLB_PROMOTED]], [[ENTRY:%.*]] ], [ [[DIV:%.*]], [[LOOP_LATCH:%.*]] ]
+; CHECK-NEXT: [[DIV1:%.*]] = phi i8 [ undef, [[ENTRY:%.*]] ], [ [[DIV:%.*]], [[LOOP_LATCH:%.*]] ]
; CHECK-NEXT: [[I:%.*]] = phi i8 [ 0, [[ENTRY]] ], [ [[ADD:%.*]], [[LOOP_LATCH]] ]
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[I]], 4
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_LATCH]], label [[EXIT:%.*]]
More information about the llvm-commits
mailing list