Skip to content

Commit 022b1c3

Browse files
committed
[SimplifyCFG] Preserve common TBAA metadata when hoisting instructions.
Update FoldTwoEntryPHINode to collect common TBAA metadata for instructions that match in all if-blocks and have the same TBAA metadata. If that is the case, they access the same type on all paths and the TBAA info can be preserved after hoisting. I think we should be able to preserve most metadata, if it is available on matching instructions in all blocks, i.e. preserve the intersection of metadata on all matching instructions. I couldn't find any utility that already computes that intersection. At the moment, the order of of matching instructions must be the same.
1 parent 7537142 commit 022b1c3

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3841,6 +3841,29 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
38413841
dbgs() << " T: " << IfTrue->getName()
38423842
<< " F: " << IfFalse->getName() << "\n");
38433843

3844+
// Collect common TBAA metadata, for instructions that match in all if-blocks
3845+
// and have the same TBAA metadata. If that is the case, they access the same
3846+
// type on all paths and the TBAA info can be preserved after hoisting.
3847+
// TODO: preserve other common metadata.
3848+
LockstepReverseIterator LRI(IfBlocks);
3849+
DenseMap<Instruction *, MDNode *> CommonTBAA;
3850+
while (LRI.isValid()) {
3851+
auto Insts = *LRI;
3852+
Instruction *I0 = Insts.front();
3853+
MDNode *MD = I0->getMetadata(LLVMContext::MD_tbaa);
3854+
if (!MD || any_of(Insts, [I0, MD](Instruction *I) {
3855+
return !I->isSameOperationAs(I0) ||
3856+
!equal(I->operands(), I0->operands()) ||
3857+
I->getMetadata(LLVMContext::MD_tbaa) != MD;
3858+
})) {
3859+
--LRI;
3860+
continue;
3861+
}
3862+
for (Instruction *I : Insts)
3863+
CommonTBAA[I] = MD;
3864+
--LRI;
3865+
}
3866+
38443867
// If we can still promote the PHI nodes after this gauntlet of tests,
38453868
// do all of the PHI's now.
38463869

@@ -3849,6 +3872,10 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
38493872
for (BasicBlock *IfBlock : IfBlocks)
38503873
hoistAllInstructionsInto(DomBlock, DomBI, IfBlock);
38513874

3875+
for (Instruction &I : *DomBlock)
3876+
if (auto *MD = CommonTBAA.lookup(&I))
3877+
I.setMetadata(LLVMContext::MD_tbaa, MD);
3878+
38523879
IRBuilder<NoFolder> Builder(DomBI);
38533880
// Propagate fast-math-flags from phi nodes to replacement selects.
38543881
IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);

llvm/test/Transforms/SimplifyCFG/hoisting-metadata.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ define i64 @hoist_load_with_matching_pointers_and_tbaa(i1 %c) {
88
; CHECK-NEXT: [[ENTRY:.*:]]
99
; CHECK-NEXT: [[TMP:%.*]] = alloca i64, align 8
1010
; CHECK-NEXT: call void @init(ptr [[TMP]])
11-
; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[TMP]], align 8
12-
; CHECK-NOT: !tbaa
13-
; CHECK-NEXT: [[TMP1:%.*]] = load i64, ptr [[TMP]], align 8
14-
; CHECK-NOT: !tbaa
11+
; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr [[TMP]], align 8, !tbaa [[M:!.+]]
12+
; CHECK-NEXT: [[TMP1:%.*]] = load i64, ptr [[TMP]], align 8, !tbaa [[M]]
1513
; CHECK-NEXT: [[P:%.*]] = select i1 [[C]], i64 [[TMP0]], i64 [[TMP1]]
1614
; CHECK-NEXT: ret i64 [[P]]
1715
;

0 commit comments

Comments
 (0)