Skip to content

Commit a236dae

Browse files
committed
[InstCombine] Retain TBAA when narrowing memory accesses
Summary: As discussed on the mailing list it is legal to propagate TBAA to loads/stores from/to smaller regions of a larger load tagged with TBAA. Do so for (load->extractvalue)=>(gep->load) and similar foldings. Reviewed By: sanjoy Differential Revision: https://reviews.llvm.org/D31954 llvm-svn: 306615
1 parent b042905 commit a236dae

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,9 @@ static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) {
661661
if (NumElements == 1) {
662662
LoadInst *NewLoad = combineLoadToNewType(IC, LI, ST->getTypeAtIndex(0U),
663663
".unpack");
664+
AAMDNodes AAMD;
665+
LI.getAAMetadata(AAMD);
666+
NewLoad->setAAMetadata(AAMD);
664667
return IC.replaceInstUsesWith(LI, IC.Builder->CreateInsertValue(
665668
UndefValue::get(T), NewLoad, 0, Name));
666669
}
@@ -690,6 +693,10 @@ static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) {
690693
Name + ".elt");
691694
auto EltAlign = MinAlign(Align, SL->getElementOffset(i));
692695
auto *L = IC.Builder->CreateAlignedLoad(Ptr, EltAlign, Name + ".unpack");
696+
// Propagate AA metadata. It'll still be valid on the narrowed load.
697+
AAMDNodes AAMD;
698+
LI.getAAMetadata(AAMD);
699+
L->setAAMetadata(AAMD);
693700
V = IC.Builder->CreateInsertValue(V, L, i);
694701
}
695702

@@ -702,6 +709,9 @@ static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) {
702709
auto NumElements = AT->getNumElements();
703710
if (NumElements == 1) {
704711
LoadInst *NewLoad = combineLoadToNewType(IC, LI, ET, ".unpack");
712+
AAMDNodes AAMD;
713+
LI.getAAMetadata(AAMD);
714+
NewLoad->setAAMetadata(AAMD);
705715
return IC.replaceInstUsesWith(LI, IC.Builder->CreateInsertValue(
706716
UndefValue::get(T), NewLoad, 0, Name));
707717
}
@@ -734,6 +744,9 @@ static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) {
734744
Name + ".elt");
735745
auto *L = IC.Builder->CreateAlignedLoad(Ptr, MinAlign(Align, Offset),
736746
Name + ".unpack");
747+
AAMDNodes AAMD;
748+
LI.getAAMetadata(AAMD);
749+
L->setAAMetadata(AAMD);
737750
V = IC.Builder->CreateInsertValue(V, L, i);
738751
Offset += EltSize;
739752
}
@@ -1192,7 +1205,11 @@ static bool unpackStoreToAggregate(InstCombiner &IC, StoreInst &SI) {
11921205
AddrName);
11931206
auto *Val = IC.Builder->CreateExtractValue(V, i, EltName);
11941207
auto EltAlign = MinAlign(Align, SL->getElementOffset(i));
1195-
IC.Builder->CreateAlignedStore(Val, Ptr, EltAlign);
1208+
llvm::Instruction *NS =
1209+
IC.Builder->CreateAlignedStore(Val, Ptr, EltAlign);
1210+
AAMDNodes AAMD;
1211+
SI.getAAMetadata(AAMD);
1212+
NS->setAAMetadata(AAMD);
11961213
}
11971214

11981215
return true;
@@ -1239,7 +1256,10 @@ static bool unpackStoreToAggregate(InstCombiner &IC, StoreInst &SI) {
12391256
AddrName);
12401257
auto *Val = IC.Builder->CreateExtractValue(V, i, EltName);
12411258
auto EltAlign = MinAlign(Align, Offset);
1242-
IC.Builder->CreateAlignedStore(Val, Ptr, EltAlign);
1259+
Instruction *NS = IC.Builder->CreateAlignedStore(Val, Ptr, EltAlign);
1260+
AAMDNodes AAMD;
1261+
SI.getAAMetadata(AAMD);
1262+
NS->setAAMetadata(AAMD);
12431263
Offset += EltSize;
12441264
}
12451265

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,9 +2425,15 @@ Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) {
24252425
Builder->SetInsertPoint(L);
24262426
Value *GEP = Builder->CreateInBoundsGEP(L->getType(),
24272427
L->getPointerOperand(), Indices);
2428+
Instruction *NL = Builder->CreateLoad(GEP);
2429+
// Whatever aliasing information we had for the orignal load must also
2430+
// hold for the smaller load, so propagate the annotations.
2431+
AAMDNodes Nodes;
2432+
L->getAAMetadata(Nodes);
2433+
NL->setAAMetadata(Nodes);
24282434
// Returning the load directly will cause the main loop to insert it in
24292435
// the wrong spot, so use replaceInstUsesWith().
2430-
return replaceInstUsesWith(EV, Builder->CreateLoad(GEP));
2436+
return replaceInstUsesWith(EV, NL);
24312437
}
24322438
// We could simplify extracts from other values. Note that nested extracts may
24332439
// already be simplified implicitly by the above: extract (extract (insert) )
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; RUN: opt -S -instcombine %s -o - | FileCheck %s
2+
3+
%Complex = type { double, double }
4+
5+
; Check that instcombine preserves TBAA when narrowing loads
6+
define double @teststructextract(%Complex *%val) {
7+
; CHECK: load double, {{.*}}, !tbaa
8+
; CHECK-NOT: load %Complex
9+
%loaded = load %Complex, %Complex *%val, !tbaa !1
10+
%real = extractvalue %Complex %loaded, 0
11+
ret double %real
12+
}
13+
14+
define double @testarrayextract([2 x double] *%val) {
15+
; CHECK: load double, {{.*}}, !tbaa
16+
; CHECK-NOT: load [2 x double]
17+
%loaded = load [2 x double], [2 x double] *%val, !tbaa !1
18+
%real = extractvalue [2 x double] %loaded, 0
19+
ret double %real
20+
}
21+
22+
; Check that inscombine preserves TBAA when breaking up stores
23+
define void @teststructinsert(%Complex *%loc, double %a, double %b) {
24+
; CHECK: store double %a, {{.*}}, !tbaa
25+
; CHECK: store double %b, {{.*}}, !tbaa
26+
; CHECK-NOT: store %Complex
27+
%inserted = insertvalue %Complex undef, double %a, 0
28+
%inserted2 = insertvalue %Complex %inserted, double %b, 1
29+
store %Complex %inserted2, %Complex *%loc, !tbaa !1
30+
ret void
31+
}
32+
33+
define void @testarrayinsert([2 x double] *%loc, double %a, double %b) {
34+
; CHECK: store double %a, {{.*}}, !tbaa
35+
; CHECK: store double %b, {{.*}}, !tbaa
36+
; CHECK-NOT: store [2 x double]
37+
%inserted = insertvalue [2 x double] undef, double %a, 0
38+
%inserted2 = insertvalue [2 x double] %inserted, double %b, 1
39+
store [2 x double] %inserted2, [2 x double] *%loc, !tbaa !1
40+
ret void
41+
}
42+
43+
!0 = !{!"tbaa_root"}
44+
!1 = !{!2, !2, i64 0}
45+
!2 = !{!"Complex", !0, i64 0}

0 commit comments

Comments
 (0)