Skip to content

Commit ab6677e

Browse files
authored
[LICM] Only set AA metadata on hoisted load if it executes. (#117204)
#116220 clarified that violations of aliasing metadata are UB. Only set the AA metadata after hoisting a log, if it is guaranteed to execute in the original loop. PR: #117204
1 parent 4028bb1 commit ab6677e

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,9 @@ bool llvm::promoteLoopAccessesToScalars(
20302030

20312031
bool DereferenceableInPH = false;
20322032
bool StoreIsGuanteedToExecute = false;
2033+
bool LoadIsGuaranteedToExecute = false;
20332034
bool FoundLoadToPromote = false;
2035+
20342036
// Goes from Unknown to either Safe or Unsafe, but can't switch between them.
20352037
enum {
20362038
StoreSafe,
@@ -2089,6 +2091,10 @@ bool llvm::promoteLoopAccessesToScalars(
20892091

20902092
Align InstAlignment = Load->getAlign();
20912093

2094+
if (!LoadIsGuaranteedToExecute)
2095+
LoadIsGuaranteedToExecute =
2096+
SafetyInfo->isGuaranteedToExecute(*UI, DT, CurLoop);
2097+
20922098
// Note that proving a load safe to speculate requires proving
20932099
// sufficient alignment at the target location. Proving it guaranteed
20942100
// to execute does as well. Thus we can increase our guaranteed
@@ -2233,8 +2239,9 @@ bool llvm::promoteLoopAccessesToScalars(
22332239
SSAUpdater SSA(&NewPHIs);
22342240
LoopPromoter Promoter(SomePtr, LoopUses, SSA, ExitBlocks, InsertPts,
22352241
MSSAInsertPts, PIC, MSSAU, *LI, DL, Alignment,
2236-
SawUnorderedAtomic, AATags, *SafetyInfo,
2237-
StoreSafety == StoreSafe);
2242+
SawUnorderedAtomic,
2243+
StoreIsGuanteedToExecute ? AATags : AAMDNodes(),
2244+
*SafetyInfo, StoreSafety == StoreSafe);
22382245

22392246
// Set up the preheader to have a definition of the value. It is the live-out
22402247
// value from the preheader that uses in the loop will use.
@@ -2247,7 +2254,7 @@ bool llvm::promoteLoopAccessesToScalars(
22472254
PreheaderLoad->setOrdering(AtomicOrdering::Unordered);
22482255
PreheaderLoad->setAlignment(Alignment);
22492256
PreheaderLoad->setDebugLoc(DebugLoc());
2250-
if (AATags)
2257+
if (AATags && LoadIsGuaranteedToExecute)
22512258
PreheaderLoad->setAAMetadata(AATags);
22522259

22532260
MemoryAccess *PreheaderLoadMemoryAccess = MSSAU.createMemoryAccessInBB(

llvm/test/Transforms/LICM/hoist-metadata.ll

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ define void @noalias_metadata_load_may_not_execute() {
7777
; CHECK-NEXT: entry:
7878
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 16
7979
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i32, ptr [[A]]
80-
; CHECK-NEXT: [[GEP_PROMOTED:%.*]] = load i32, ptr [[GEP]], align 4, !tbaa [[TBAA3:![0-9]+]], !noalias [[META7:![0-9]+]]
80+
; CHECK-NEXT: [[GEP_PROMOTED:%.*]] = load i32, ptr [[GEP]], align 4
8181
; CHECK-NEXT: br label [[LOOP_HEADER:%.*]]
8282
; CHECK: loop.header:
8383
; CHECK-NEXT: [[ADD1:%.*]] = phi i32 [ [[GEP_PROMOTED]], [[ENTRY:%.*]] ], [ [[ADD:%.*]], [[LOOP_LATCH:%.*]] ]
@@ -92,7 +92,7 @@ define void @noalias_metadata_load_may_not_execute() {
9292
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_HEADER]], label [[EXIT]]
9393
; CHECK: exit:
9494
; CHECK-NEXT: [[ADD2:%.*]] = phi i32 [ [[ADD]], [[LOOP_LATCH]] ], [ [[ADD1]], [[LOOP_HEADER]] ]
95-
; CHECK-NEXT: store i32 [[ADD2]], ptr [[GEP]], align 4, !tbaa [[TBAA3]], !noalias [[META7]]
95+
; CHECK-NEXT: store i32 [[ADD2]], ptr [[GEP]], align 4
9696
; CHECK-NEXT: ret void
9797
;
9898
entry:
@@ -132,11 +132,4 @@ exit:
132132
; CHECK: [[RNG0]] = !{i32 0, i32 10}
133133
; CHECK: [[META1]] = !{}
134134
; CHECK: [[META2]] = !{i64 4}
135-
; CHECK: [[TBAA3]] = !{[[META4:![0-9]+]], [[META4]], i64 0}
136-
; CHECK: [[META4]] = !{!"short", [[META5:![0-9]+]], i64 0}
137-
; CHECK: [[META5]] = !{!"omnipotent char", [[META6:![0-9]+]], i64 0}
138-
; CHECK: [[META6]] = !{!"Simple C/C++ TBAA"}
139-
; CHECK: [[META7]] = !{[[META8:![0-9]+]]}
140-
; CHECK: [[META8]] = distinct !{[[META8]], [[META9:![0-9]+]]}
141-
; CHECK: [[META9]] = distinct !{[[META9]]}
142135
;.

llvm/test/Transforms/LICM/hoisting-preheader-debugloc.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
; RUN: opt -passes=licm %s -S | FileCheck %s
22

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

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

0 commit comments

Comments
 (0)