Skip to content

Commit 3679062

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 e62bf7c commit 3679062

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

3792+
// Collect common TBAA metadata, for instructions that match in all if-blocks
3793+
// and have the same TBAA metadata. If that is the case, they access the same
3794+
// type on all paths and the TBAA info can be preserved after hoisting.
3795+
// TODO: preserve other common metadata.
3796+
LockstepReverseIterator LRI(IfBlocks);
3797+
DenseMap<Instruction *, MDNode *> CommonTBAA;
3798+
while (LRI.isValid()) {
3799+
auto Insts = *LRI;
3800+
Instruction *I0 = Insts.front();
3801+
MDNode *MD = I0->getMetadata(LLVMContext::MD_tbaa);
3802+
if (!MD || any_of(Insts, [I0, MD](Instruction *I) {
3803+
return !I->isSameOperationAs(I0) ||
3804+
!equal(I->operands(), I0->operands()) ||
3805+
I->getMetadata(LLVMContext::MD_tbaa) != MD;
3806+
})) {
3807+
--LRI;
3808+
continue;
3809+
}
3810+
for (Instruction *I : Insts)
3811+
CommonTBAA[I] = MD;
3812+
--LRI;
3813+
}
3814+
37923815
// If we can still promote the PHI nodes after this gauntlet of tests,
37933816
// do all of the PHI's now.
37943817

@@ -3797,6 +3820,10 @@ static bool foldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
37973820
for (BasicBlock *IfBlock : IfBlocks)
37983821
hoistAllInstructionsInto(DomBlock, DomBI, IfBlock);
37993822

3823+
for (Instruction &I : *DomBlock)
3824+
if (auto *MD = CommonTBAA.lookup(&I))
3825+
I.setMetadata(LLVMContext::MD_tbaa, MD);
3826+
38003827
IRBuilder<NoFolder> Builder(DomBI);
38013828
// Propagate fast-math-flags from phi nodes to replacement selects.
38023829
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)