Skip to content

[LICM] Only set AA metadata on hoisted load if it executes. #117204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions llvm/lib/Transforms/Scalar/LICM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,9 @@ bool llvm::promoteLoopAccessesToScalars(

bool DereferenceableInPH = false;
bool StoreIsGuanteedToExecute = false;
bool LoadIsGuaranteedToExecute = false;
bool FoundLoadToPromote = false;

// Goes from Unknown to either Safe or Unsafe, but can't switch between them.
enum {
StoreSafe,
Expand Down Expand Up @@ -2089,6 +2091,10 @@ bool llvm::promoteLoopAccessesToScalars(

Align InstAlignment = Load->getAlign();

if (!LoadIsGuaranteedToExecute)
LoadIsGuaranteedToExecute =
SafetyInfo->isGuaranteedToExecute(*UI, DT, CurLoop);

// Note that proving a load safe to speculate requires proving
// sufficient alignment at the target location. Proving it guaranteed
// to execute does as well. Thus we can increase our guaranteed
Expand Down Expand Up @@ -2233,8 +2239,9 @@ bool llvm::promoteLoopAccessesToScalars(
SSAUpdater SSA(&NewPHIs);
LoopPromoter Promoter(SomePtr, LoopUses, SSA, ExitBlocks, InsertPts,
MSSAInsertPts, PIC, MSSAU, *LI, DL, Alignment,
SawUnorderedAtomic, AATags, *SafetyInfo,
StoreSafety == StoreSafe);
SawUnorderedAtomic,
StoreIsGuanteedToExecute ? AATags : AAMDNodes(),
*SafetyInfo, StoreSafety == StoreSafe);

// 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.
Expand All @@ -2247,7 +2254,7 @@ bool llvm::promoteLoopAccessesToScalars(
PreheaderLoad->setOrdering(AtomicOrdering::Unordered);
PreheaderLoad->setAlignment(Alignment);
PreheaderLoad->setDebugLoc(DebugLoc());
if (AATags)
if (AATags && LoadIsGuaranteedToExecute)
PreheaderLoad->setAAMetadata(AATags);

MemoryAccess *PreheaderLoadMemoryAccess = MSSAU.createMemoryAccessInBB(
Expand Down
11 changes: 2 additions & 9 deletions llvm/test/Transforms/LICM/hoist-metadata.ll
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ define void @noalias_metadata_load_may_not_execute() {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 16
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i32, ptr [[A]]
; CHECK-NEXT: [[GEP_PROMOTED:%.*]] = load i32, ptr [[GEP]], align 4, !tbaa [[TBAA3:![0-9]+]], !noalias [[META7:![0-9]+]]
; CHECK-NEXT: [[GEP_PROMOTED:%.*]] = load i32, ptr [[GEP]], align 4
; CHECK-NEXT: br label [[LOOP_HEADER:%.*]]
; CHECK: loop.header:
; CHECK-NEXT: [[ADD1:%.*]] = phi i32 [ [[GEP_PROMOTED]], [[ENTRY:%.*]] ], [ [[ADD:%.*]], [[LOOP_LATCH:%.*]] ]
Expand All @@ -92,7 +92,7 @@ define void @noalias_metadata_load_may_not_execute() {
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_HEADER]], label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: [[ADD2:%.*]] = phi i32 [ [[ADD]], [[LOOP_LATCH]] ], [ [[ADD1]], [[LOOP_HEADER]] ]
; CHECK-NEXT: store i32 [[ADD2]], ptr [[GEP]], align 4, !tbaa [[TBAA3]], !noalias [[META7]]
; CHECK-NEXT: store i32 [[ADD2]], ptr [[GEP]], align 4
; CHECK-NEXT: ret void
;
entry:
Expand Down Expand Up @@ -132,11 +132,4 @@ exit:
; CHECK: [[RNG0]] = !{i32 0, i32 10}
; CHECK: [[META1]] = !{}
; CHECK: [[META2]] = !{i64 4}
; CHECK: [[TBAA3]] = !{[[META4:![0-9]+]], [[META4]], i64 0}
; CHECK: [[META4]] = !{!"short", [[META5:![0-9]+]], i64 0}
; CHECK: [[META5]] = !{!"omnipotent char", [[META6:![0-9]+]], i64 0}
; CHECK: [[META6]] = !{!"Simple C/C++ TBAA"}
; CHECK: [[META7]] = !{[[META8:![0-9]+]]}
; CHECK: [[META8]] = distinct !{[[META8]], [[META9:![0-9]+]]}
; CHECK: [[META9]] = distinct !{[[META9]]}
;.
4 changes: 3 additions & 1 deletion llvm/test/Transforms/LICM/hoisting-preheader-debugloc.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
; RUN: opt -passes=licm %s -S | FileCheck %s

; CHECK: %arrayidx4.promoted = load i32, ptr %arrayidx4, align 4, !tbaa !{{[0-9]+$}}
; CHECK: %arrayidx4.promoted = load i32, ptr %arrayidx4, align 4
; CHECK-NOT: !dbg
; CHECK: br label %for.body

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

Expand Down
Loading