Skip to content

Commit 5d7e964

Browse files
committed
[TBAA] Extract logic to use TBAA tag for field of !tbaa.struct (NFC). (llvm#81284)
(cherry-picked from c609846)
1 parent 3e44914 commit 5d7e964

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

llvm/include/llvm/IR/Metadata.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,11 @@ struct AAMDNodes {
735735
/// together. Different from `merge`, where different locations should
736736
/// overlap each other, `concat` puts non-overlapping locations together.
737737
AAMDNodes concat(const AAMDNodes &Other) const;
738+
739+
/// Create a new AAMDNode for accessing \p AccessSize bytes of this AAMDNode.
740+
/// If his AAMDNode has !tbaa.struct and \p AccessSize matches the size of the
741+
/// field at offset 0, get the TBAA tag describing the accessed field.
742+
AAMDNodes adjustForAccess(unsigned AccessSize);
738743
};
739744

740745
// Specialize DenseMapInfo for AAMDNodes.

llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,3 +818,18 @@ MDNode *AAMDNodes::extendToTBAA(MDNode *MD, ssize_t Len) {
818818
ConstantAsMetadata::get(ConstantInt::get(PreviousSize->getType(), Len));
819819
return MDNode::get(MD->getContext(), NextNodes);
820820
}
821+
822+
AAMDNodes AAMDNodes::adjustForAccess(unsigned AccessSize) {
823+
AAMDNodes New = *this;
824+
MDNode *M = New.TBAAStruct;
825+
New.TBAAStruct = nullptr;
826+
if (M && M->getNumOperands() == 3 && M->getOperand(0) &&
827+
mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
828+
mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
829+
M->getOperand(1) && mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
830+
mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
831+
AccessSize &&
832+
M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
833+
New.TBAA = cast<MDNode>(M->getOperand(2));
834+
return New;
835+
}

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,28 +185,14 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
185185

186186
// If the memcpy has metadata describing the members, see if we can get the
187187
// TBAA tag describing our copy.
188-
MDNode *CopyMD = nullptr;
189-
if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa)) {
190-
CopyMD = M;
191-
} else if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) {
192-
if (M->getNumOperands() == 3 && M->getOperand(0) &&
193-
mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
194-
mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
195-
M->getOperand(1) &&
196-
mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
197-
mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
198-
Size &&
199-
M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
200-
CopyMD = cast<MDNode>(M->getOperand(2));
201-
}
188+
AAMDNodes AACopyMD = MI->getAAMetadata().adjustForAccess(Size);
202189

203190
Value *Src = Builder.CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy);
204191
Value *Dest = Builder.CreateBitCast(MI->getArgOperand(0), NewDstPtrTy);
205192
LoadInst *L = Builder.CreateLoad(IntType, Src);
206193
// Alignment from the mem intrinsic will be better, so use it.
207194
L->setAlignment(*CopySrcAlign);
208-
if (CopyMD)
209-
L->setMetadata(LLVMContext::MD_tbaa, CopyMD);
195+
L->setAAMetadata(AACopyMD);
210196
MDNode *LoopMemParallelMD =
211197
MI->getMetadata(LLVMContext::MD_mem_parallel_loop_access);
212198
if (LoopMemParallelMD)
@@ -218,8 +204,7 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
218204
StoreInst *S = Builder.CreateStore(L, Dest);
219205
// Alignment from the mem intrinsic will be better, so use it.
220206
S->setAlignment(*CopyDstAlign);
221-
if (CopyMD)
222-
S->setMetadata(LLVMContext::MD_tbaa, CopyMD);
207+
S->setAAMetadata(AACopyMD);
223208
if (LoopMemParallelMD)
224209
S->setMetadata(LLVMContext::MD_mem_parallel_loop_access, LoopMemParallelMD);
225210
if (AccessGroupMD)

0 commit comments

Comments
 (0)