-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[DSE] Consider the aliasing through global variable while checking clobber #120044
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
Changes from 5 commits
69d43a5
fa652d9
defcdb1
1ff8ebc
56d75ec
64fe614
6626f36
5dc849e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2261,6 +2261,19 @@ struct DSEState { | |
bool eliminateDeadDefs(const MemoryDefWrapper &KillingDefWrapper); | ||
}; | ||
|
||
// Return true if "Arg" is function local and isn't captured before "CB" or | ||
// if "Arg" is GEP whose base pointer is function local and isn't captured | ||
// before "CB". | ||
bool IsFuncLocalAndNotCaptured(Value *Arg, const CallBase *CB, | ||
EarliestEscapeAnalysis &EA) { | ||
if (isIdentifiedFunctionLocal(Arg)) | ||
return EA.isNotCapturedBefore(Arg, CB, /*OrAt*/ true); | ||
const auto *GEP = dyn_cast<GetElementPtrInst>(Arg); | ||
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. Check the result of getUnderlyingObject instead, it will look through multiple GEPs. 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. Ah, done. Thanks! |
||
if (GEP && isIdentifiedFunctionLocal(GEP->getPointerOperand())) | ||
return EA.isNotCapturedBefore(GEP->getPointerOperand(), CB, /*OrAt*/ true); | ||
return false; | ||
} | ||
|
||
SmallVector<MemoryLocation, 1> | ||
DSEState::getInitializesArgMemLoc(const Instruction *I) { | ||
const CallBase *CB = dyn_cast<CallBase>(I); | ||
|
@@ -2276,6 +2289,13 @@ DSEState::getInitializesArgMemLoc(const Instruction *I) { | |
Inits = InitializesAttr.getValueAsConstantRangeList(); | ||
|
||
Value *CurArg = CB->getArgOperand(Idx); | ||
// Check whether "CurArg" could alias with global variables. We require | ||
// either it's function local and isn't captured before or the "CB" only | ||
// accesses arg or inaccessible mem. | ||
if (!Inits.empty() && !IsFuncLocalAndNotCaptured(CurArg, CB, EA) && | ||
!CB->onlyAccessesInaccessibleMemOrArgMem()) | ||
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. I'd check onlyAccessesInaccessibleMemOrArgMem before isFuncLocalAndNotCaptured as it's cheaper. 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. Ah, thanks for the reminder! Done. |
||
Inits = ConstantRangeList(); | ||
|
||
// We don't perform incorrect DSE on unwind edges in the current function, | ||
// and use the "initializes" attribute to kill dead stores if: | ||
// - The call does not throw exceptions, "CB->doesNotThrow()". | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,26 @@ define i16 @p1_write_then_read_caller() { | |
ret i16 %l | ||
} | ||
|
||
declare void @fn_capture(ptr noundef) | ||
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. Drop noundef, it's not relevant here. 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. Done. |
||
; Function Attrs: mustprogress nounwind uwtable | ||
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. Drop inaccurate Function Attrs comment. 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. Done. |
||
define i16 @p1_write_then_read_caller_escape() { | ||
; CHECK-LABEL: @p1_write_then_read_caller_escape( | ||
; CHECK-NEXT: [[PTR:%.*]] = alloca i16, align 2 | ||
; CHECK-NEXT: store i16 0, ptr [[PTR]], align 2 | ||
; CHECK-NEXT: call void @fn_capture(ptr [[PTR]]) | ||
; CHECK-NEXT: call void @p1_write_then_read(ptr [[PTR]]) | ||
; CHECK-NEXT: [[L:%.*]] = load i16, ptr [[PTR]], align 2 | ||
; CHECK-NEXT: ret i16 [[L]] | ||
; | ||
%ptr = alloca i16 | ||
store i16 0, ptr %ptr | ||
call void @fn_capture(ptr %ptr) | ||
call void @p1_write_then_read(ptr %ptr) | ||
%l = load i16, ptr %ptr | ||
ret i16 %l | ||
} | ||
|
||
|
||
; Function Attrs: mustprogress nounwind uwtable | ||
define i16 @p1_write_then_read_caller_with_clobber() { | ||
; CHECK-LABEL: @p1_write_then_read_caller_with_clobber( | ||
|
@@ -221,15 +241,17 @@ declare void @large_p2(ptr nocapture noundef initializes((0, 200)), ptr nocaptur | |
; Function Attrs: mustprogress nounwind uwtable | ||
define i16 @large_p1_caller() { | ||
; CHECK-LABEL: @large_p1_caller( | ||
; CHECK-NEXT: [[PTR:%.*]] = alloca [200 x i8], align 1 | ||
; CHECK-NEXT: call void @large_p1(ptr [[PTR]]) | ||
; CHECK-NEXT: [[L:%.*]] = load i16, ptr [[PTR]], align 2 | ||
; CHECK-NEXT: [[PTR:%.*]] = alloca [300 x i8], align 1 | ||
; CHECK-NEXT: [[TMP:%.*]] = getelementptr i8, ptr [[PTR]], i64 100 | ||
; CHECK-NEXT: call void @large_p1(ptr [[TMP]]) | ||
; CHECK-NEXT: [[L:%.*]] = load i16, ptr [[TMP]], align 2 | ||
; CHECK-NEXT: ret i16 [[L]] | ||
; | ||
%ptr = alloca [200 x i8] | ||
call void @llvm.memset.p0.i64(ptr %ptr, i8 42, i64 100, i1 false) | ||
call void @large_p1(ptr %ptr) | ||
%l = load i16, ptr %ptr | ||
%ptr = alloca [300 x i8] | ||
%tmp = getelementptr i8, ptr %ptr, i64 100 | ||
call void @llvm.memset.p0.i64(ptr %tmp, i8 42, i64 100, i1 false) | ||
call void @large_p1(ptr %tmp) | ||
%l = load i16, ptr %tmp | ||
ret i16 %l | ||
} | ||
|
||
|
@@ -299,3 +321,23 @@ define i16 @large_p2_may_or_partial_alias_caller2(ptr %base1, ptr %base2) { | |
ret i16 %l | ||
} | ||
|
||
@g = global i16 123, align 2 | ||
|
||
declare void @read_global(ptr nocapture noundef initializes((0, 2))) nounwind | ||
memory(read, argmem: write, inaccessiblemem: none) nounwind | ||
|
||
define i16 @global_var_alias() { | ||
; CHECK-LABEL: @global_var_alias( | ||
; CHECK-NEXT: store i16 0, ptr @g, align 4 | ||
; CHECK-NEXT: [[G_ADDR:%.*]] = getelementptr i8, ptr @g, i64 1 | ||
; CHECK-NEXT: call void @read_global(ptr [[G_ADDR]]) | ||
; CHECK-NEXT: [[L:%.*]] = load i16, ptr @g, align 2 | ||
; CHECK-NEXT: ret i16 [[L]] | ||
; | ||
store i16 0, ptr @g, align 4 | ||
%g_addr = getelementptr i8, ptr @g, i64 1 | ||
call void @read_global(ptr %g_addr) | ||
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. it seems like this case would still go out of bounds for the i16 global? 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. Ooops, removed the GEP and directly read/write @g. Thanks. |
||
%l = load i16, ptr @g | ||
ret i16 %l | ||
} | ||
|
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.
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.
Done.