Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang/test/Frontend/optimization-remark-extra-analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
int foo(int *x, int *y) {
int a = *x;
*y = 2;
// expected-remark@+1 {{load of type i32 not eliminated}}
// expected-remark@+2 {{load of type i32 not eliminated}}
// expected-remark@+1 {{load of type i8 not eliminated}}
return a + *x;
}
1 change: 1 addition & 0 deletions llvm/include/llvm/Transforms/Scalar/GVN.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class GVNPass : public PassInfoMixin<GVNPass> {

// Helper functions of redundant load elimination
bool processLoad(LoadInst *L);
bool splitAndprocessLoad(LoadInst *L);
bool processNonLocalLoad(LoadInst *L);
bool processAssumeIntrinsic(AssumeInst *II);

Expand Down
105 changes: 104 additions & 1 deletion llvm/lib/Transforms/Scalar/GVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Comment on lines +2135 to +2140
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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
// define i32 @f(ptr %P)
// 1: %b2 = getelementptr inbounds i8, ptr %P, i64 1
// 2: store i8 0, ptr %b2, align 1
// 3: store i8 0, ptr %P, align 1
// 5: %L = load i16, ptr %P, 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Expand Down Expand Up @@ -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);

Expand Down
12 changes: 6 additions & 6 deletions llvm/test/Analysis/BasicAA/2003-02-26-AccessSizeTest.ll
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
}

5 changes: 2 additions & 3 deletions llvm/test/Analysis/TypeBasedAliasAnalysis/precedence.ll
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ entry:
ret i32 %tmp3
}

; Test for PartialAlias aliasing. GVN doesn't yet eliminate the load
; in the BasicAA case.
; Test for PartialAlias aliasing.

; TBAA: @offset
; TBAA: ret i64 0
; BASICAA: @offset
; BASICAA: ret i64 %tmp3
; BASICAA: ret i64 256
define i64 @offset(ptr %x) nounwind {
entry:
store i64 0, ptr %x, !tbaa !4
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/Transforms/GVN/2009-11-12-MemDepMallocBitCast.ll
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
; Test to make sure malloc's bitcast does not block detection of a store
; to aliased memory; GVN should not optimize away the load in this program.
; Test to make sure malloc's bitcast does not block detection of a store
; to aliased memory.
; RUN: opt < %s -passes=gvn -S | FileCheck %s

define i64 @test() {
%1 = tail call ptr @malloc(i64 mul (i64 4, i64 ptrtoint (ptr getelementptr (i64, ptr null, i64 1) to i64))) ; <ptr> [#uses=2]
store i8 42, ptr %1
%Y = load i64, ptr %1 ; <i64> [#uses=1]
ret i64 %Y
; CHECK: %Y = load i64, ptr %1
; CHECK: store i8 42, ptr %1, align 1
; CHECK: ret i64 %Y
}

Expand Down
20 changes: 7 additions & 13 deletions llvm/test/Transforms/GVN/no_speculative_loads_with_asan.ll
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ define i32 @TestNoAsan() {
; CHECK-NEXT: [[I1:%.*]] = getelementptr inbounds i8, ptr [[I]], i64 1
; CHECK-NEXT: store i8 0, ptr [[I1]], align 1
; CHECK-NEXT: store i8 0, ptr [[I]], align 1
; CHECK-NEXT: [[I3:%.*]] = load i16, ptr [[I]], align 4
; CHECK-NEXT: [[I4:%.*]] = icmp eq i16 [[I3]], 0
; CHECK-NEXT: br i1 [[I4]], label [[BB10:%.*]], label [[BB5:%.*]]
; CHECK-NEXT: br i1 true, label [[BB10:%.*]], label [[BB5:%.*]]
; CHECK: bb5:
; CHECK-NEXT: [[I6:%.*]] = getelementptr inbounds i8, ptr [[I]], i64 2
; CHECK-NEXT: [[I8:%.*]] = load i16, ptr [[I6]], align 2
; CHECK-NEXT: [[I9:%.*]] = sext i16 [[I8]] to i32
; CHECK-NEXT: br label [[BB10]]
; CHECK: bb10:
; CHECK-NEXT: ret i32 0
Expand Down Expand Up @@ -48,17 +48,14 @@ define i32 @TestAsan() sanitize_address {
; CHECK-NEXT: [[I1:%.*]] = getelementptr inbounds i8, ptr [[I]], i64 1
; CHECK-NEXT: store i8 0, ptr [[I1]], align 1
; CHECK-NEXT: store i8 0, ptr [[I]], align 1
; CHECK-NEXT: [[I3:%.*]] = load i16, ptr [[I]], align 4
; CHECK-NEXT: [[I4:%.*]] = icmp eq i16 [[I3]], 0
; CHECK-NEXT: br i1 [[I4]], label [[BB10:%.*]], label [[BB5:%.*]]
; CHECK-NEXT: br i1 true, label [[BB10:%.*]], label [[BB5:%.*]]
; CHECK: bb5:
; CHECK-NEXT: [[I6:%.*]] = getelementptr inbounds i8, ptr [[I]], i64 2
; CHECK-NEXT: [[I8:%.*]] = load i16, ptr [[I6]], align 2
; CHECK-NEXT: [[I9:%.*]] = sext i16 [[I8]] to i32
; CHECK-NEXT: br label [[BB10]]
; CHECK: bb10:
; CHECK-NEXT: [[I11:%.*]] = phi i32 [ [[I9]], [[BB5]] ], [ 0, [[BB:%.*]] ]
; CHECK-NEXT: ret i32 [[I11]]
; CHECK-NEXT: ret i32 0
;
bb:
%i = tail call noalias ptr @_Znam(i64 2)
Expand Down Expand Up @@ -87,17 +84,14 @@ define i32 @TestHWAsan() sanitize_hwaddress {
; CHECK-NEXT: [[I1:%.*]] = getelementptr inbounds i8, ptr [[I]], i64 1
; CHECK-NEXT: store i8 0, ptr [[I1]], align 1
; CHECK-NEXT: store i8 0, ptr [[I]], align 1
; CHECK-NEXT: [[I3:%.*]] = load i16, ptr [[I]], align 4
; CHECK-NEXT: [[I4:%.*]] = icmp eq i16 [[I3]], 0
; CHECK-NEXT: br i1 [[I4]], label [[BB10:%.*]], label [[BB5:%.*]]
; CHECK-NEXT: br i1 true, label [[BB10:%.*]], label [[BB5:%.*]]
; CHECK: bb5:
; CHECK-NEXT: [[I6:%.*]] = getelementptr inbounds i8, ptr [[I]], i64 2
; CHECK-NEXT: [[I8:%.*]] = load i16, ptr [[I6]], align 2
; CHECK-NEXT: [[I9:%.*]] = sext i16 [[I8]] to i32
; CHECK-NEXT: br label [[BB10]]
; CHECK: bb10:
; CHECK-NEXT: [[I11:%.*]] = phi i32 [ [[I9]], [[BB5]] ], [ 0, [[BB:%.*]] ]
; CHECK-NEXT: ret i32 [[I11]]
; CHECK-NEXT: ret i32 0
;
bb:
%i = tail call noalias ptr @_Znam(i64 2)
Expand Down
3 changes: 3 additions & 0 deletions llvm/test/Transforms/GVN/opt-remarks-multiple-users.ll
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 1, Column: 1 }
; CHECK-NEXT: ...
; CHECK: --- !Missed
; CHECK: --- !Missed
; CHECK-NEXT: Pass: gvn
; CHECK-NEXT: Name: LoadClobbered
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 4, Column: 4 }
Expand All @@ -32,6 +33,7 @@
; CHECK-NEXT: - ClobberedBy: call
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 3, Column: 3 }
; CHECK-NEXT: ...
; CHECK: --- !Missed

; ModuleID = 'bugpoint-reduced-simplified.bc'
source_filename = "gvn-test.c"
Expand Down Expand Up @@ -69,6 +71,7 @@ entry:
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 1, Column: 1 }
; CHECK-NEXT: ...
; CHECK: --- !Missed
; CHECK: --- !Missed
; CHECK-NEXT: Pass: gvn
; CHECK-NEXT: Name: LoadClobbered
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 4, Column: 4 }
Expand Down
3 changes: 3 additions & 0 deletions llvm/test/Transforms/GVN/opt-remarks-non-dominating.ll
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ if.end: ; preds = %if.then, %entry

declare dso_local void @clobberingFunc() local_unnamed_addr #0

; CHECK: --- !Missed
; CHECK: --- !Missed
; CHECK-NEXT: Pass: gvn
; CHECK-NEXT: Name: LoadClobbered
Expand All @@ -59,6 +60,7 @@ declare dso_local void @clobberingFunc() local_unnamed_addr #0
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 2, Column: 2 }
; CHECK-NEXT: ...
; CHECK: --- !Missed
; CHECK: --- !Missed
; CHECK-NEXT: Pass: gvn
; CHECK-NEXT: Name: LoadClobbered
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 5, Column: 5 }
Expand Down Expand Up @@ -136,6 +138,7 @@ if.end5: ; preds = %if.end5.sink.split,
ret void
}

; CHECK: --- !Missed
; CHECK: --- !Missed
; CHECK-NEXT: Pass: gvn
; CHECK-NEXT: Name: LoadClobbered
Expand Down
Loading