-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[AggressiveInstCombine] Implement store merge optimization #147540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nikic
wants to merge
2
commits into
llvm:main
Choose a base branch
from
nikic:aggressive-instcombine-store-merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -842,6 +842,138 @@ static bool foldConsecutiveLoads(Instruction &I, const DataLayout &DL, | |
return true; | ||
} | ||
|
||
/// ValWidth bits starting at ValOffset of Val stored at PtrBase+PtrOffset. | ||
struct PartStore { | ||
Value *PtrBase; | ||
APInt PtrOffset; | ||
Value *Val; | ||
uint64_t ValOffset; | ||
uint64_t ValWidth; | ||
StoreInst *Store; | ||
|
||
bool isCompatibleWith(const PartStore &Other) const { | ||
return PtrBase == Other.PtrBase && Val == Other.Val; | ||
} | ||
|
||
bool operator<(const PartStore &Other) const { | ||
return PtrOffset.slt(Other.PtrOffset); | ||
} | ||
}; | ||
|
||
static std::optional<PartStore> matchPartStore(Instruction &I, | ||
const DataLayout &DL) { | ||
auto *Store = dyn_cast<StoreInst>(&I); | ||
if (!Store || !Store->isSimple()) | ||
return std::nullopt; | ||
|
||
Value *StoredVal = Store->getValueOperand(); | ||
Type *StoredTy = StoredVal->getType(); | ||
if (!StoredTy->isIntegerTy() || !DL.typeSizeEqualsStoreSize(StoredTy)) | ||
return std::nullopt; | ||
|
||
uint64_t ValWidth = StoredTy->getPrimitiveSizeInBits(); | ||
uint64_t ValOffset = 0; | ||
Value *Val; | ||
if (!match(StoredVal, m_CombineOr(m_Trunc(m_LShr(m_Value(Val), | ||
m_ConstantInt(ValOffset))), | ||
m_Trunc(m_Value(Val))))) | ||
return std::nullopt; | ||
|
||
Value *Ptr = Store->getPointerOperand(); | ||
APInt PtrOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0); | ||
Value *PtrBase = Ptr->stripAndAccumulateConstantOffsets( | ||
DL, PtrOffset, /*AllowNonInbounds=*/true); | ||
return {{PtrBase, PtrOffset, Val, ValOffset, ValWidth, Store}}; | ||
} | ||
|
||
static bool mergePartStores(SmallVectorImpl<PartStore> &Parts, | ||
const DataLayout &DL, TargetTransformInfo &TTI) { | ||
if (Parts.size() < 2) | ||
return false; | ||
|
||
// We now have multiple parts of the same value stored to the same pointer. | ||
// Sort the parts by pointer offset, and make sure they are consistent with | ||
// the value offsets. Also check that the value is fully covered without | ||
// overlaps. | ||
// FIXME: We could support merging stores for only part of the value here. | ||
llvm::sort(Parts); | ||
int64_t LastEndOffsetFromFirst = 0; | ||
const PartStore &First = Parts[0]; | ||
for (const PartStore &Part : Parts) { | ||
APInt PtrOffsetFromFirst = Part.PtrOffset - First.PtrOffset; | ||
int64_t ValOffsetFromFirst = Part.ValOffset - First.ValOffset; | ||
if (PtrOffsetFromFirst * 8 != ValOffsetFromFirst || | ||
LastEndOffsetFromFirst != ValOffsetFromFirst) | ||
return false; | ||
LastEndOffsetFromFirst = ValOffsetFromFirst + Part.ValWidth; | ||
} | ||
|
||
// Check whether combining the stores is profitable. | ||
// FIXME: We could generate smaller stores if we can't produce a large one. | ||
LLVMContext &Ctx = First.Store->getContext(); | ||
Type *NewTy = Type::getIntNTy(Ctx, LastEndOffsetFromFirst); | ||
unsigned Fast = 0; | ||
if (!TTI.isTypeLegal(NewTy) || | ||
!TTI.allowsMisalignedMemoryAccesses(Ctx, LastEndOffsetFromFirst, | ||
First.Store->getPointerAddressSpace(), | ||
First.Store->getAlign(), &Fast) || | ||
!Fast) | ||
return false; | ||
|
||
// Generate the combined store. | ||
IRBuilder<> Builder(First.Store); | ||
Value *Val = First.Val; | ||
if (First.ValOffset != 0) | ||
Val = Builder.CreateLShr(Val, First.ValOffset); | ||
Val = Builder.CreateTrunc(Val, NewTy); | ||
StoreInst *Store = Builder.CreateAlignedStore( | ||
Val, First.Store->getPointerOperand(), First.Store->getAlign()); | ||
|
||
AAMDNodes AATags = First.Store->getAAMetadata(); | ||
for (const PartStore &Part : drop_begin(Parts)) | ||
AATags = AATags.concat(Part.Store->getAAMetadata()); | ||
Store->setAAMetadata(AATags); | ||
|
||
// Remove the old stores. | ||
for (const PartStore &Part : Parts) | ||
Part.Store->eraseFromParent(); | ||
|
||
return true; | ||
} | ||
|
||
static bool foldConsecutiveStores(BasicBlock &BB, const DataLayout &DL, | ||
TargetTransformInfo &TTI, AliasAnalysis &AA) { | ||
// FIXME: Add big endian support. | ||
if (DL.isBigEndian()) | ||
return false; | ||
|
||
SmallVector<PartStore, 8> Parts; | ||
bool MadeChange = false; | ||
for (Instruction &I : make_early_inc_range(BB)) { | ||
if (std::optional<PartStore> Part = matchPartStore(I, DL)) { | ||
if (Parts.empty() || Part->isCompatibleWith(Parts[0])) { | ||
Parts.push_back(std::move(*Part)); | ||
continue; | ||
} | ||
|
||
MadeChange |= mergePartStores(Parts, DL, TTI); | ||
Parts.clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some tests for multiple store groups:
|
||
Parts.push_back(std::move(*Part)); | ||
continue; | ||
} | ||
|
||
// FIXME: Use AA to make this more precise. | ||
if (I.mayReadOrWriteMemory() || I.mayThrow()) { | ||
MadeChange |= mergePartStores(Parts, DL, TTI); | ||
Parts.clear(); | ||
continue; | ||
} | ||
} | ||
|
||
MadeChange |= mergePartStores(Parts, DL, TTI); | ||
return MadeChange; | ||
} | ||
|
||
/// Combine away instructions providing they are still equivalent when compared | ||
/// against 0. i.e do they have any bits set. | ||
static Value *optimizeShiftInOrChain(Value *V, IRBuilder<> &Builder) { | ||
|
@@ -1330,6 +1462,9 @@ static bool foldUnusualPatterns(Function &F, DominatorTree &DT, | |
// bugs. | ||
MadeChange |= foldLibCalls(I, TTI, TLI, AC, DT, DL, MadeCFGChange); | ||
} | ||
|
||
// Do this separately to avoid redundantly scanning stores multiple times. | ||
MadeChange |= foldConsecutiveStores(BB, DL, TTI, AA); | ||
} | ||
|
||
// We're done with transforms, so remove dead instructions. | ||
|
106 changes: 106 additions & 0 deletions
106
llvm/test/Transforms/AggressiveInstCombine/X86/store-merge-be.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: opt -S -passes=aggressive-instcombine -mtriple=x86_64-unknown-linux-gnu -data-layout="E-n64" < %s | FileCheck %s | ||
|
||
; Pretend X86 is big endian. | ||
|
||
; FIXME: Big endian not supported yet. | ||
|
||
define void @test_i32_be(i32 %x, ptr %p) { | ||
; CHECK-LABEL: define void @test_i32_be( | ||
; CHECK-SAME: i32 [[X:%.*]], ptr [[P:%.*]]) { | ||
; CHECK-NEXT: [[X_0:%.*]] = trunc i32 [[X]] to i8 | ||
; CHECK-NEXT: [[GEP_0:%.*]] = getelementptr i8, ptr [[P]], i64 3 | ||
; CHECK-NEXT: store i8 [[X_0]], ptr [[GEP_0]], align 1 | ||
; CHECK-NEXT: [[SHR_1:%.*]] = lshr i32 [[X]], 8 | ||
; CHECK-NEXT: [[X_1:%.*]] = trunc i32 [[SHR_1]] to i8 | ||
; CHECK-NEXT: [[GEP_1:%.*]] = getelementptr i8, ptr [[P]], i64 2 | ||
; CHECK-NEXT: store i8 [[X_1]], ptr [[GEP_1]], align 1 | ||
; CHECK-NEXT: [[SHR_2:%.*]] = lshr i32 [[X]], 16 | ||
; CHECK-NEXT: [[X_2:%.*]] = trunc i32 [[SHR_2]] to i8 | ||
; CHECK-NEXT: [[GEP_2:%.*]] = getelementptr i8, ptr [[P]], i64 1 | ||
; CHECK-NEXT: store i8 [[X_2]], ptr [[GEP_2]], align 1 | ||
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X]], 24 | ||
; CHECK-NEXT: [[X_3:%.*]] = trunc i32 [[TMP1]] to i8 | ||
; CHECK-NEXT: store i8 [[X_3]], ptr [[P]], align 1 | ||
; CHECK-NEXT: ret void | ||
; | ||
%x.0 = trunc i32 %x to i8 | ||
%gep.0 = getelementptr i8, ptr %p, i64 3 | ||
store i8 %x.0, ptr %gep.0 | ||
%shr.1 = lshr i32 %x, 8 | ||
%x.1 = trunc i32 %shr.1 to i8 | ||
%gep.1 = getelementptr i8, ptr %p, i64 2 | ||
store i8 %x.1, ptr %gep.1 | ||
%shr.2 = lshr i32 %x, 16 | ||
%x.2 = trunc i32 %shr.2 to i8 | ||
%gep.2 = getelementptr i8, ptr %p, i64 1 | ||
store i8 %x.2, ptr %gep.2 | ||
%shr.3 = lshr i32 %x, 24 | ||
%x.3 = trunc i32 %shr.3 to i8 | ||
store i8 %x.3, ptr %p | ||
ret void | ||
} | ||
|
||
define void @test_i32_le(i32 %x, ptr %p) { | ||
; CHECK-LABEL: define void @test_i32_le( | ||
; CHECK-SAME: i32 [[X:%.*]], ptr [[P:%.*]]) { | ||
; CHECK-NEXT: [[X_0:%.*]] = trunc i32 [[X]] to i8 | ||
; CHECK-NEXT: store i8 [[X_0]], ptr [[P]], align 1 | ||
; CHECK-NEXT: [[SHR_1:%.*]] = lshr i32 [[X]], 8 | ||
; CHECK-NEXT: [[X_1:%.*]] = trunc i32 [[SHR_1]] to i8 | ||
; CHECK-NEXT: [[GEP_1:%.*]] = getelementptr i8, ptr [[P]], i64 1 | ||
; CHECK-NEXT: store i8 [[X_1]], ptr [[GEP_1]], align 1 | ||
; CHECK-NEXT: [[SHR_2:%.*]] = lshr i32 [[X]], 16 | ||
; CHECK-NEXT: [[X_2:%.*]] = trunc i32 [[SHR_2]] to i8 | ||
; CHECK-NEXT: [[GEP_2:%.*]] = getelementptr i8, ptr [[P]], i64 2 | ||
; CHECK-NEXT: store i8 [[X_2]], ptr [[GEP_2]], align 1 | ||
; CHECK-NEXT: [[SHR_3:%.*]] = lshr i32 [[X]], 24 | ||
; CHECK-NEXT: [[X_3:%.*]] = trunc i32 [[SHR_3]] to i8 | ||
; CHECK-NEXT: [[GEP_3:%.*]] = getelementptr i8, ptr [[P]], i64 3 | ||
; CHECK-NEXT: store i8 [[X_3]], ptr [[GEP_3]], align 1 | ||
; CHECK-NEXT: ret void | ||
; | ||
%x.0 = trunc i32 %x to i8 | ||
store i8 %x.0, ptr %p | ||
%shr.1 = lshr i32 %x, 8 | ||
%x.1 = trunc i32 %shr.1 to i8 | ||
%gep.1 = getelementptr i8, ptr %p, i64 1 | ||
store i8 %x.1, ptr %gep.1 | ||
%shr.2 = lshr i32 %x, 16 | ||
%x.2 = trunc i32 %shr.2 to i8 | ||
%gep.2 = getelementptr i8, ptr %p, i64 2 | ||
store i8 %x.2, ptr %gep.2 | ||
%shr.3 = lshr i32 %x, 24 | ||
%x.3 = trunc i32 %shr.3 to i8 | ||
%gep.3 = getelementptr i8, ptr %p, i64 3 | ||
store i8 %x.3, ptr %gep.3 | ||
ret void | ||
} | ||
|
||
define void @test_i32_mixed_parts(i32 %x, ptr %p) { | ||
; CHECK-LABEL: define void @test_i32_mixed_parts( | ||
; CHECK-SAME: i32 [[X:%.*]], ptr [[P:%.*]]) { | ||
; CHECK-NEXT: [[X_0:%.*]] = trunc i32 [[X]] to i8 | ||
; CHECK-NEXT: [[GEP_0:%.*]] = getelementptr i8, ptr [[P]], i64 3 | ||
; CHECK-NEXT: store i8 [[X_0]], ptr [[GEP_0]], align 1 | ||
; CHECK-NEXT: [[SHR_1:%.*]] = lshr i32 [[X]], 8 | ||
; CHECK-NEXT: [[X_1:%.*]] = trunc i32 [[SHR_1]] to i16 | ||
; CHECK-NEXT: [[GEP_1:%.*]] = getelementptr i8, ptr [[P]], i64 1 | ||
; CHECK-NEXT: store i16 [[X_1]], ptr [[GEP_1]], align 2 | ||
; CHECK-NEXT: [[SHR_3:%.*]] = lshr i32 [[X]], 24 | ||
; CHECK-NEXT: [[X_3:%.*]] = trunc i32 [[SHR_3]] to i8 | ||
; CHECK-NEXT: store i8 [[X_3]], ptr [[P]], align 1 | ||
; CHECK-NEXT: ret void | ||
; | ||
%x.0 = trunc i32 %x to i8 | ||
%gep.0 = getelementptr i8, ptr %p, i64 3 | ||
store i8 %x.0, ptr %gep.0 | ||
%shr.1 = lshr i32 %x, 8 | ||
%x.1 = trunc i32 %shr.1 to i16 | ||
%gep.1 = getelementptr i8, ptr %p, i64 1 | ||
store i16 %x.1, ptr %gep.1 | ||
%shr.3 = lshr i32 %x, 24 | ||
%x.3 = trunc i32 %shr.3 to i8 | ||
store i8 %x.3, ptr %p | ||
ret void | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: We can merge two stores with different constant value operands.
See https://github.com/dtcxzyw/llvm-opt-benchmark/pull/2560/files#r2192918824