-
Notifications
You must be signed in to change notification settings - Fork 13.6k
split load to bytes to deduce load value #72364
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
mohammed-nurulhoque
wants to merge
1
commit into
llvm:main
Choose a base branch
from
mohammed-nurulhoque:us-split-load
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
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
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
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 |
---|---|---|
|
@@ -112,6 +112,7 @@ static cl::opt<bool> | |
GVNEnableSplitBackedgeInLoadPRE("enable-split-backedge-in-load-pre", | ||
cl::init(false)); | ||
static cl::opt<bool> GVNEnableMemDep("enable-gvn-memdep", cl::init(true)); | ||
static cl::opt<bool> GVNEnableSplitLoad("enable-split-load", cl::init(true)); | ||
|
||
static cl::opt<uint32_t> MaxNumDeps( | ||
"gvn-max-num-deps", cl::Hidden, cl::init(100), | ||
|
@@ -2128,6 +2129,105 @@ static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) { | |
I->replaceAllUsesWith(Repl); | ||
} | ||
|
||
// split load to single byte loads and check if the value can be deduced | ||
// | ||
// Example: | ||
// define i32 @f(i8* %P) | ||
// 1: %b2 = getelementptr inbounds i8, i8* %P, i64 1 | ||
// 2: store i8 0, i8* %b2, align 1 | ||
// 3: store i8 0, i8* %P, align 1 | ||
// 4: %s1 = bitcast i8* %P to i16* | ||
// 5: %L = load i16, i16* %s1, align 4 | ||
// | ||
// The last clobbering write for the load is (3) but it doesn't cover the whole | ||
// read. So AnalyzeLoadAvailability would give up. | ||
// This function emit temporary byte-sized loads that cover the original load, | ||
// so that any last write covers the read. We run AnalyzeLoadAvailability on | ||
// each byte to try to construct the load as a constant. | ||
bool GVNPass::splitAndprocessLoad(LoadInst *L) { | ||
if (L->isAtomic()) | ||
return false; | ||
|
||
Type *LTy = L->getType(); | ||
if (!LTy->isIntegerTy()) | ||
return false; | ||
|
||
unsigned BW = LTy->getIntegerBitWidth(); | ||
if (BW % 8) | ||
return false; | ||
|
||
IntegerType *ByteTy = IntegerType::getInt8Ty(LTy->getContext()); | ||
Type *BytePtrTy = PointerType::get(ByteTy, L->getPointerAddressSpace()); | ||
BitCastInst *Base = new BitCastInst(L->getPointerOperand(), BytePtrTy, "", L); | ||
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. Not necessary with opaque pointers. |
||
|
||
// we update this byte by byte as we deduce the load value | ||
APInt ConstructedValue = APInt::getZero(BW); | ||
|
||
for (unsigned i=0; i<BW/8; i++) { | ||
Value *Offset = Constant::getIntegerValue(LTy, APInt(BW, i)); | ||
GetElementPtrInst *GEP = GetElementPtrInst::Create( | ||
ByteTy, Base, ArrayRef<Value*>(Offset), "", L); | ||
LoadInst *ByteLoad = new LoadInst(ByteTy, GEP, "", L); | ||
ByteLoad->setDebugLoc(L->getDebugLoc()); | ||
|
||
auto CleanupTmps = [&]() { | ||
MD->removeInstruction(ByteLoad); | ||
ByteLoad->eraseFromParent(); | ||
GEP->eraseFromParent(); | ||
}; | ||
|
||
MemDepResult ByteDep = MD->getDependency(ByteLoad); | ||
// AnalyzeLoadAvailability only analyzes local deps. | ||
// If dep is not def or clobber, we cannot get a value from it. | ||
if (ByteDep.isNonLocal() || (!ByteDep.isDef() && !ByteDep.isClobber())) { | ||
CleanupTmps(); | ||
Base->eraseFromParent(); | ||
return false; | ||
} | ||
|
||
auto ByteRes = AnalyzeLoadAvailability(ByteLoad, ByteDep, GEP); | ||
// If it doesn't reduce to a constant, give up. Creating the value at runtime by | ||
// shifting the bytes is not worth it to remove a load. | ||
if (ByteRes && | ||
(ByteRes->isMemIntrinValue() || | ||
(ByteRes->isSimpleValue() && | ||
(isa<ConstantInt>(ByteRes->getSimpleValue()) || | ||
isa<UndefValue>(ByteRes->getSimpleValue()))))) { | ||
Value *V = ByteRes->MaterializeAdjustedValue(ByteLoad, ByteLoad, *this); | ||
ConstantInt *ByteConst = dyn_cast<ConstantInt>(V); | ||
if (!ByteConst) { | ||
// replace undef with 0. This helps optimize cases where some bits of | ||
// load are constant integer, and some are undef. So we can optimize | ||
// the load to a particular integer. | ||
ByteConst = ConstantInt::get(ByteTy, 0); | ||
} | ||
ConstructedValue.insertBits(ByteConst->getValue(), 8*i); | ||
} else { | ||
LLVM_DEBUG(dbgs() << "GVN split load: byte " << i | ||
<< " did not reduce to a constant. Giving up"); | ||
CleanupTmps(); | ||
Base->eraseFromParent(); | ||
return false; | ||
} | ||
CleanupTmps(); | ||
} | ||
Base->eraseFromParent(); | ||
|
||
// Replace the load! | ||
Constant *LoadValue = ConstantInt::get(LTy, ConstructedValue); | ||
patchAndReplaceAllUsesWith(L, LoadValue); | ||
markInstructionForDeletion(L); | ||
if (MSSAU) | ||
MSSAU->removeMemoryAccess(L); | ||
++NumGVNLoad; | ||
reportLoadElim(L, LoadValue, ORE); | ||
// Tell MDA to reexamine the reused pointer since we might have more | ||
// information after forwarding it. | ||
if (MD && LoadValue->getType()->isPtrOrPtrVectorTy()) | ||
MD->invalidateCachedPointerInfo(LoadValue); | ||
return true; | ||
} | ||
|
||
/// Attempt to eliminate a load, first by eliminating it | ||
/// locally, and then attempting non-local elimination if that fails. | ||
bool GVNPass::processLoad(LoadInst *L) { | ||
|
@@ -2161,8 +2261,11 @@ bool GVNPass::processLoad(LoadInst *L) { | |
} | ||
|
||
auto AV = AnalyzeLoadAvailability(L, Dep, L->getPointerOperand()); | ||
if (!AV) | ||
if (!AV) { | ||
if (GVNEnableSplitLoad) | ||
return splitAndprocessLoad(L); | ||
return false; | ||
} | ||
|
||
Value *AvailableValue = AV->MaterializeAdjustedValue(L, L, *this); | ||
|
||
|
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 |
---|---|---|
@@ -1,19 +1,19 @@ | ||
; This testcase makes sure that size is taken to account when alias analysis | ||
; is performed. It is not legal to delete the second load instruction because | ||
; the value computed by the first load instruction is changed by the store. | ||
; is performed. The stores rights to parts of the second load | ||
|
||
; RUN: opt < %s -aa-pipeline=basic-aa -passes=gvn,instcombine -S | FileCheck %s | ||
|
||
define i32 @test() { | ||
; CHECK: %Y.DONOTREMOVE = load i32, ptr %A | ||
; CHECK: %Z = sub i32 0, %Y.DONOTREMOVE | ||
; CHECK: @test | ||
; CHECK-NEXT: ret i32 -256 | ||
|
||
%A = alloca i32 | ||
store i32 0, ptr %A | ||
%X = load i32, ptr %A | ||
%C = getelementptr i8, ptr %A, i64 1 | ||
store i8 1, ptr %C ; Aliases %A | ||
%Y.DONOTREMOVE = load i32, ptr %A | ||
%Z = sub i32 %X, %Y.DONOTREMOVE | ||
%D = load i32, ptr %A | ||
%Z = sub i32 %X, %D | ||
ret i32 %Z | ||
} | ||
|
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
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
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
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
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
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.