Skip to content

Commit c609846

Browse files
authored
[TBAA] Extract logic to use TBAA tag for field of !tbaa.struct (NFC). (llvm#81284)
1 parent 213b0ae commit c609846

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

llvm/include/llvm/IR/Metadata.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,11 @@ struct AAMDNodes {
844844
/// together. Different from `merge`, where different locations should
845845
/// overlap each other, `concat` puts non-overlapping locations together.
846846
AAMDNodes concat(const AAMDNodes &Other) const;
847+
848+
/// Create a new AAMDNode for accessing \p AccessSize bytes of this AAMDNode.
849+
/// If his AAMDNode has !tbaa.struct and \p AccessSize matches the size of the
850+
/// field at offset 0, get the TBAA tag describing the accessed field.
851+
AAMDNodes adjustForAccess(unsigned AccessSize);
847852
};
848853

849854
// Specialize DenseMapInfo for AAMDNodes.

llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp

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

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,7 @@ Instruction *InstCombinerImpl::SimplifyAnyMemTransfer(AnyMemTransferInst *MI) {
172172

173173
// If the memcpy has metadata describing the members, see if we can get the
174174
// TBAA tag describing our copy.
175-
AAMDNodes AACopyMD = MI->getAAMetadata();
176-
177-
if (MDNode *M = AACopyMD.TBAAStruct) {
178-
AACopyMD.TBAAStruct = nullptr;
179-
if (M->getNumOperands() == 3 && M->getOperand(0) &&
180-
mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
181-
mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
182-
M->getOperand(1) &&
183-
mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
184-
mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
185-
Size &&
186-
M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
187-
AACopyMD.TBAA = cast<MDNode>(M->getOperand(2));
188-
}
175+
AAMDNodes AACopyMD = MI->getAAMetadata().adjustForAccess(Size);
189176

190177
Value *Src = MI->getArgOperand(1);
191178
Value *Dest = MI->getArgOperand(0);

0 commit comments

Comments
 (0)