Skip to content

Commit b5e6565

Browse files
committed
[LICM] Only set AA metadata on hoisted load if it executes.
llvm#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.
1 parent 7f19b1e commit b5e6565

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 7 additions & 1 deletion
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 LoadIsGuanteedToExecute = 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,
@@ -2088,11 +2090,15 @@ bool llvm::promoteLoopAccessesToScalars(
20882090
FoundLoadToPromote = true;
20892091

20902092
Align InstAlignment = Load->getAlign();
2093+
bool GuaranteedToExecute =
2094+
SafetyInfo->isGuaranteedToExecute(*UI, DT, CurLoop);
2095+
LoadIsGuanteedToExecute |= GuaranteedToExecute;
20912096

20922097
// Note that proving a load safe to speculate requires proving
20932098
// sufficient alignment at the target location. Proving it guaranteed
20942099
// to execute does as well. Thus we can increase our guaranteed
20952100
// alignment as well.
2101+
20962102
if (!DereferenceableInPH || (InstAlignment > Alignment))
20972103
if (isSafeToExecuteUnconditionally(
20982104
*Load, DT, TLI, CurLoop, SafetyInfo, ORE,
@@ -2247,7 +2253,7 @@ bool llvm::promoteLoopAccessesToScalars(
22472253
PreheaderLoad->setOrdering(AtomicOrdering::Unordered);
22482254
PreheaderLoad->setAlignment(Alignment);
22492255
PreheaderLoad->setDebugLoc(DebugLoc());
2250-
if (AATags)
2256+
if (AATags && LoadIsGuanteedToExecute)
22512257
PreheaderLoad->setAAMetadata(AATags);
22522258

22532259
MemoryAccess *PreheaderLoadMemoryAccess = MSSAU.createMemoryAccessInBB(

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

Lines changed: 2 additions & 2 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, !tbaa [[TBAA3:![0-9]+]], !noalias [[META7:![0-9]+]]
9696
; CHECK-NEXT: ret void
9797
;
9898
entry:

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)