Skip to content

Commit 60dd1ee

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 c85611e commit 60dd1ee

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
@@ -3845,6 +3845,29 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
38453845
dbgs() << " T: " << IfTrue->getName()
38463846
<< " F: " << IfFalse->getName() << "\n");
38473847

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

@@ -3853,6 +3876,10 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
38533876
for (BasicBlock *IfBlock : IfBlocks)
38543877
hoistAllInstructionsInto(DomBlock, DomBI, IfBlock);
38553878

3879+
for (Instruction &I : *DomBlock)
3880+
if (auto *MD = CommonTBAA.lookup(&I))
3881+
I.setMetadata(LLVMContext::MD_tbaa, MD);
3882+
38563883
IRBuilder<NoFolder> Builder(DomBI);
38573884
// Propagate fast-math-flags from phi nodes to replacement selects.
38583885
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)