-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[InstCombine] Retain inbounds when canonicalising add+gep #72244
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
Closed
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2450,10 +2450,51 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) { | |
// as: | ||
// %newptr = getelementptr i32, ptr %ptr, i64 %idx1 | ||
// %newgep = getelementptr i32, ptr %newptr, i64 %idx2 | ||
auto *NewPtr = Builder.CreateGEP(GEP.getResultElementType(), | ||
GEP.getPointerOperand(), Idx1); | ||
return GetElementPtrInst::Create(GEP.getResultElementType(), NewPtr, | ||
Idx2); | ||
// If %gep is inbounds then %newgep can be inbounds only if %newptr is as | ||
// well, as an inbounds gep requires the base pointer to be inbounds. We | ||
// can mark %newptr as inbounds if we have a loop like | ||
// for (i = 0; ...) | ||
// ptr[i+x] | ||
// If x is the same in each loop iteration then we know that we have a | ||
// series of geps starting with ptr[x], which means that ptr[x] must be | ||
// inbounds. | ||
auto CheckIdx = [&](Value *LoopIdx, Value *FixedIdx) { | ||
// Check that LoopIdx is a loop induction variable that starts at 0. | ||
auto *PHI = dyn_cast<PHINode>(LoopIdx); | ||
BinaryOperator *BO; | ||
Value *Start, *End; | ||
if (!PHI || !matchSimpleRecurrence(PHI, BO, Start, End) || | ||
!match(Start, m_Zero())) | ||
return false; | ||
// If FixedIdx dominates the phi then it's the same in each loop | ||
// iteration. | ||
if (DT.dominates(FixedIdx, PHI)) | ||
return true; | ||
// If FixedIdx is a binary expression of values that dominate the phi | ||
// then it's the same in each loop iteration. | ||
Value *Left, *Right; | ||
if (match(FixedIdx, m_BinOp(m_Value(Left), m_Value(Right))) && | ||
DT.dominates(Left, PHI) && DT.dominates(Right, PHI)) | ||
return true; | ||
// We can't handle anything else. | ||
return false; | ||
}; | ||
bool InBounds = false; | ||
if (GEP.isInBounds()) { | ||
if (CheckIdx(Idx2, Idx1)) { | ||
InBounds = true; | ||
} else if (CheckIdx(Idx1, Idx2)) { | ||
std::swap(Idx1, Idx2); | ||
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. Why do we have to swap the indices here? |
||
InBounds = true; | ||
} | ||
} | ||
auto *NewPtr = | ||
Builder.CreateGEP(GEP.getResultElementType(), GEP.getPointerOperand(), | ||
Idx1, "", InBounds); | ||
auto *NewGEP = | ||
GetElementPtrInst::Create(GEP.getResultElementType(), NewPtr, Idx2); | ||
NewGEP->setIsInBounds(InBounds); | ||
return NewGEP; | ||
} | ||
ConstantInt *C; | ||
if (match(GEP.getOperand(1), m_OneUse(m_SExt(m_OneUse(m_NSWAdd( | ||
|
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,228 @@ | ||
; RUN: opt < %s -S -passes=instcombine | FileCheck %s | ||
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 use update_test_checks.py to generate check lines. |
||
|
||
target datalayout = "e-p:32:32" | ||
|
||
; CHECK-LABEL: @test1 | ||
define void @test1(i32 %N, i32 %k, ptr %A) { | ||
entry: | ||
br label %for.cond | ||
|
||
for.cond: | ||
%i = phi i32 [ 0, %entry ], [ %inc, %for.body ] | ||
%cmp = icmp ult i32 %i, %N | ||
br i1 %cmp, label %for.body, label %for.end | ||
|
||
; CHECK-LABEL: for.body: | ||
; CHECK: [[GEP:%.*]] = getelementptr inbounds i8, ptr %A, i32 %k | ||
; CHECK-NEXT: %arrayidx = getelementptr inbounds i8, ptr [[GEP]], i32 %i | ||
for.body: | ||
%add = add i32 %i, %k | ||
%arrayidx = getelementptr inbounds i8, ptr %A, i32 %add | ||
store i8 1, ptr %arrayidx, align 4 | ||
%inc = add i32 %i, 1 | ||
br label %for.cond | ||
|
||
for.end: | ||
ret void | ||
} | ||
|
||
; CHECK-LABEL: @test2 | ||
define void @test2(i32 %N, i32 %k, ptr %A) { | ||
entry: | ||
br label %for.cond | ||
|
||
for.cond: | ||
%i = phi i32 [ 0, %entry ], [ %inc, %for.body ] | ||
%cmp = icmp ult i32 %i, %N | ||
br i1 %cmp, label %for.body, label %for.end | ||
|
||
; CHECK-LABEL: for.body: | ||
; CHECK: [[GEP:%.*]] = getelementptr inbounds i8, ptr %A, i32 %mul | ||
; CHECK-NEXT: %arrayidx = getelementptr inbounds i8, ptr [[GEP]], i32 %i | ||
for.body: | ||
%mul = mul i32 %k, 42 | ||
%add = add i32 %i, %mul | ||
%arrayidx = getelementptr inbounds i8, ptr %A, i32 %add | ||
store i8 1, ptr %arrayidx, align 4 | ||
%inc = add i32 %i, 1 | ||
br label %for.cond | ||
|
||
for.end: | ||
ret void | ||
} | ||
|
||
; CHECK-LABEL: @test3 | ||
define void @test3(i32 %N, ptr %A, i32 %val) { | ||
entry: | ||
br label %for.cond | ||
|
||
for.cond: | ||
%i = phi i32 [ 0, %entry ], [ %inc6, %for.inc5 ] | ||
%cmp = icmp ult i32 %i, %N | ||
br i1 %cmp, label %for.body, label %for.end7 | ||
|
||
for.body: | ||
br label %for.cond1 | ||
|
||
for.cond1: | ||
%j = phi i32 [ 0, %for.body ], [ %inc, %for.body3 ] | ||
%cmp2 = icmp ult i32 %j, %N | ||
br i1 %cmp2, label %for.body3, label %for.inc5 | ||
|
||
; CHECK-LABEL: for.body3: | ||
; CHECK: [[GEP:%.*]] = getelementptr inbounds i8, ptr %A, i32 %i | ||
; CHECK-NEXT: %arrayidx = getelementptr inbounds i8, ptr [[GEP]], i32 %j | ||
for.body3: | ||
%add = add i32 %i, %j | ||
%arrayidx = getelementptr inbounds i8, ptr %A, i32 %add | ||
store i8 1, ptr %arrayidx, align 4 | ||
%inc = add i32 %j, 1 | ||
br label %for.cond1 | ||
|
||
for.inc5: | ||
%inc6 = add i32 %i, 1 | ||
br label %for.cond | ||
|
||
for.end7: | ||
ret void | ||
} | ||
|
||
; CHECK-LABEL: @test4 | ||
define void @test4(i32 %N, ptr %A, i32 %val) { | ||
entry: | ||
br label %for.cond | ||
|
||
for.cond: | ||
%i = phi i32 [ 0, %entry ], [ %inc6, %for.inc5 ] | ||
%cmp = icmp ult i32 %i, %N | ||
br i1 %cmp, label %for.body, label %for.end7 | ||
|
||
for.body: | ||
br label %for.cond1 | ||
|
||
for.cond1: | ||
%j = phi i32 [ 0, %for.body ], [ %inc, %for.body3 ] | ||
%cmp2 = icmp ult i32 %j, %N | ||
br i1 %cmp2, label %for.body3, label %for.inc5 | ||
|
||
; CHECK-LABEL: for.body3: | ||
; CHECK: [[GEP:%.*]] = getelementptr inbounds i8, ptr %A, i32 %mul | ||
; CHECK-NEXT: %arrayidx = getelementptr inbounds i8, ptr [[GEP]], i32 %j | ||
for.body3: | ||
%mul = mul i32 %i, %N | ||
%add = add i32 %mul, %j | ||
%arrayidx = getelementptr inbounds i8, ptr %A, i32 %add | ||
store i8 1, ptr %arrayidx, align 4 | ||
%inc = add i32 %j, 1 | ||
br label %for.cond1 | ||
|
||
for.inc5: | ||
%inc6 = add i32 %i, 1 | ||
br label %for.cond | ||
|
||
for.end7: | ||
ret void | ||
} | ||
|
||
; We can't use inbounds here because the add operand doesn't dominate the loop | ||
; CHECK-LABEL: @test5 | ||
define void @test5(i32 %N, ptr %A, ptr %B) { | ||
entry: | ||
br label %for.cond | ||
|
||
for.cond: | ||
%i = phi i32 [ 0, %entry ], [ %inc, %for.body ] | ||
%cmp = icmp ult i32 %i, %N | ||
br i1 %cmp, label %for.body, label %for.end | ||
|
||
; CHECK-LABEL: for.body: | ||
; CHECK: [[GEP:%.*]] = getelementptr i8, ptr %A, i32 %i | ||
; CHECK-NEXT: %arrayidx = getelementptr i8, ptr [[GEP]], i32 %0 | ||
for.body: | ||
%0 = load i32, ptr %B, align 4 | ||
%add = add i32 %i, %0 | ||
%arrayidx = getelementptr inbounds i8, ptr %A, i32 %add | ||
store i8 1, ptr %arrayidx, align 4 | ||
%inc = add i32 %i, 1 | ||
br label %for.cond | ||
|
||
for.end: | ||
ret void | ||
} | ||
|
||
; We can't use inbounds here because we don't have a loop | ||
; CHECK-LABEL: @test6 | ||
define void @test6(i32 %k, i32 %j, ptr %A) { | ||
entry: | ||
%cmp = icmp ugt i32 %k, 10 | ||
br i1 %cmp, label %if.then, label %if.else | ||
|
||
if.then: | ||
br label %if.end | ||
|
||
if.else: | ||
br label %if.end | ||
|
||
; CHECK-LABEL: if.end: | ||
; CHECK: [[GEP:%.*]] = getelementptr i8, ptr %A, i32 %val | ||
; CHECK-NEXT: %arrayidx = getelementptr i8, ptr [[GEP]], i32 %j | ||
if.end: | ||
%val = phi i32 [ 0, %if.then ], [ 1, %if.else ] | ||
%add = add i32 %val, %j | ||
%arrayidx = getelementptr inbounds i8, ptr %A, i32 %add | ||
store i8 1, ptr %arrayidx, align 4 | ||
ret void | ||
} | ||
|
||
; Inbounds gep would be invalid because of potential overflow in the add, though | ||
; we don't convert to gep+gep as we insert an explicit sext instead of using i16 | ||
; gep offset. | ||
; CHECK-LABEL: @test7 | ||
define void @test7(i16 %N, i16 %k, ptr %A) { | ||
entry: | ||
br label %for.cond | ||
|
||
for.cond: | ||
%i = phi i16 [ 0, %entry ], [ %inc, %for.body ] | ||
%cmp = icmp ult i16 %i, %N | ||
br i1 %cmp, label %for.body, label %for.end | ||
|
||
; CHECK-LABEL: for.body: | ||
; CHECK: %add = add i16 %i, %k | ||
; CHECK-NEXT: [[SEXT:%.*]] = sext i16 %add to i32 | ||
; CHECK-NEXT: %arrayidx = getelementptr inbounds i8, ptr %A, i32 [[SEXT]] | ||
for.body: | ||
%add = add i16 %i, %k | ||
%arrayidx = getelementptr inbounds i8, ptr %A, i16 %add | ||
store i8 1, ptr %arrayidx, align 4 | ||
%inc = add i16 %i, 1 | ||
br label %for.cond | ||
|
||
for.end: | ||
ret void | ||
} | ||
|
||
; %i starts at 1 so we can't use inbounds | ||
; CHECK-LABEL: @test8 | ||
define void @test8(i32 %N, i32 %k, ptr %A) { | ||
entry: | ||
br label %for.cond | ||
|
||
for.cond: | ||
%i = phi i32 [ 1, %entry ], [ %inc, %for.body ] | ||
%cmp = icmp ult i32 %i, %N | ||
br i1 %cmp, label %for.body, label %for.end | ||
|
||
; CHECK-LABEL: for.body: | ||
; CHECK: [[GEP:%.*]] = getelementptr i8, ptr %A, i32 %i | ||
; CHECK-NEXT: %arrayidx = getelementptr i8, ptr [[GEP]], i32 %k | ||
for.body: | ||
%add = add i32 %i, %k | ||
%arrayidx = getelementptr inbounds i8, ptr %A, i32 %add | ||
store i8 1, ptr %arrayidx, align 4 | ||
%inc = add i32 %i, 1 | ||
br label %for.cond | ||
|
||
for.end: | ||
ret void | ||
} |
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.
This reasoning is not correct. Violating inbounds will result in a poison return value, not immediate UB. As such, you can't do inductive reasoning along the lines of "It was inbounds on the first iteration, and each iteration advances it by an inbounds amount". For example, you could have the case that the result of the GEP is only actually used on a single iteration, in which case it doesn't matter whether all the others are poison or not.
Take this variant of your first test: https://alive2.llvm.org/ce/z/fR9p5X It replaces the store to the pointer (which will convert poison into UB) with a call to a function. The alive2 counter-example is that the pointer is null, and k is -1. On the first iteration you have gep inbounds (null, -1) which is poison. On the second you have gep inbounds (null, 1-1) which is not. After the transform you have gep inbounds (gep inbounds (null, 1), -1) which is poison.
To salvage that approach, I think you would have to require that a) the GEP being poison implies UB and b) the GEP is executed on each loop iteration.
For your original motivating case for this patch, is the (non-IV) add operand known to be non-negative by chance? The usual way we would preserve inbounds in a transform like this is to check that both add operands are non-negatives. We can prove that for the IV, but I'm not sure whether the information exists for the other operand in cases that you care about.
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.
I've spent some time trying to implement something that can work given the problems you've highlighted, but I didn't get very far. The reason I was doing this was because loop flattening was no longer able to flatten any loops due to the lack of the inbounds qualifier, but I've decided instead to implement loop versioning in loop flattening (which is currently a TODO) as then it can handle the lack of inbounds, which is now #78576.