Transform (extractvalue (load P), ...) to (load (gep P, 0, ...)) if the load has no other uses, shrinking the load.

llvm-svn: 120323
This commit is contained in:
Frits van Bommel 2010-11-29 21:56:20 +00:00
parent 71042b51a1
commit a59a8cf49f
2 changed files with 103 additions and 7 deletions

View File

@ -1171,10 +1171,37 @@ Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) {
}
}
}
// Can't simplify extracts from other values. Note that nested extracts are
// already simplified implicitely by the above (extract ( extract (insert) )
if (LoadInst *L = dyn_cast<LoadInst>(Agg))
// If the (non-volatile) load only has one use, we can rewrite this to a
// load from a GEP. This reduces the size of the load.
// FIXME: If a load is used only by extractvalue instructions then this
// could be done regardless of having multiple uses.
if (!L->isVolatile() && L->hasOneUse()) {
// extractvalue has integer indices, getelementptr has Value*s. Convert.
SmallVector<Value*, 4> Indices;
// Prefix an i32 0 since we need the first element.
Indices.push_back(Builder->getInt32(0));
for (ExtractValueInst::idx_iterator I = EV.idx_begin(), E = EV.idx_end();
I != E; ++I)
Indices.push_back(Builder->getInt32(*I));
// We need to insert these at the location of the old load, not at that of
// the extractvalue.
Builder->SetInsertPoint(L->getParent(), L);
Value *GEP = Builder->CreateInBoundsGEP(L->getPointerOperand(),
Indices.begin(), Indices.end());
// Returning the load directly will cause the main loop to insert it in
// the wrong spot, so use ReplaceInstUsesWith().
return ReplaceInstUsesWith(EV, Builder->CreateLoad(GEP));
}
// We could simplify extracts from other values. Note that nested extracts may
// already be simplified implicitly by the above: extract (extract (insert) )
// will be translated into extract ( insert ( extract ) ) first and then just
// the value inserted, if appropriate).
// the value inserted, if appropriate. Similarly for extracts from single-use
// loads: extract (extract (load)) will be translated to extract (load (gep))
// and if again single-use then via load (gep (gep)) to load (gep).
// However, double extracts from e.g. function arguments or return values
// aren't handled yet.
return 0;
}

View File

@ -1,10 +1,13 @@
; RUN: opt < %s -instcombine -S | not grep extractvalue
; RUN: opt < %s -instcombine -S | FileCheck %s
declare void @bar({i32, i32} %a)
declare i32 @baz(i32 %a)
; CHECK: define i32 @foo
; CHECK-NOT: extractvalue
define i32 @foo(i32 %a, i32 %b) {
; Instcombine should fold various combinations of insertvalue and extractvalue
; together
declare void @bar({i32, i32} %a)
define i32 @foo(i32 %a, i32 %b) {
; Build a simple struct and pull values out again
%s1.1 = insertvalue {i32, i32} undef, i32 %a, 0
%s1 = insertvalue {i32, i32} %s1.1, i32 %b, 1
@ -36,3 +39,69 @@ define i32 @foo(i32 %a, i32 %b) {
ret i32 %v5
}
; CHECK: define i32 @extract2gep
; CHECK-NEXT: [[GEP:%[a-z0-9]+]] = getelementptr inbounds {{.*}}* %pair, i32 0, i32 1
; CHECK-NEXT: [[LOAD:%[A-Za-z0-9]+]] = load i32* [[GEP]]
; CHECK-NEXT: store
; CHECK-NEXT: br label %loop
; CHECK-NOT: extractvalue
; CHECK: call {{.*}}(i32 [[LOAD]])
; CHECK-NOT: extractvalue
; CHECK: ret i32 [[LOAD]]
define i32 @extract2gep({i32, i32}* %pair, i32* %P) {
; The load + extractvalue should be converted
; to an inbounds gep + smaller load.
; The new load should be in the same spot as the old load.
%L = load {i32, i32}* %pair
store i32 0, i32* %P
br label %loop
loop:
%E = extractvalue {i32, i32} %L, 1
%C = call i32 @baz(i32 %E)
store i32 %C, i32* %P
%cond = icmp eq i32 %C, 0
br i1 %cond, label %end, label %loop
end:
ret i32 %E
}
; CHECK: define i32 @doubleextract2gep
; CHECK-NEXT: [[GEP:%[a-z0-9]+]] = getelementptr inbounds {{.*}}* %arg, i32 0, i32 1, i32 1
; CHECK-NEXT: [[LOAD:%[A-Za-z0-9]+]] = load i32* [[GEP]]
; CHECK-NEXT: ret i32 [[LOAD]]
define i32 @doubleextract2gep({i32, {i32, i32}}* %arg) {
; The load + extractvalues should be converted
; to a 3-index inbounds gep + smaller load.
%L = load {i32, {i32, i32}}* %arg
%E1 = extractvalue {i32, {i32, i32}} %L, 1
%E2 = extractvalue {i32, i32} %E1, 1
ret i32 %E2
}
; CHECK: define i32 @nogep-multiuse
; CHECK-NEXT: load {{.*}} %pair
; CHECK-NEXT: extractvalue
; CHECK-NEXT: extractvalue
; CHECK-NEXT: add
; CHECK-NEXT: ret
define i32 @nogep-multiuse({i32, i32}* %pair) {
; The load should be left unchanged since both parts are needed.
%L = volatile load {i32, i32}* %pair
%LHS = extractvalue {i32, i32} %L, 0
%RHS = extractvalue {i32, i32} %L, 1
%R = add i32 %LHS, %RHS
ret i32 %R
}
; CHECK: define i32 @nogep-volatile
; CHECK-NEXT: volatile load {{.*}} %pair
; CHECK-NEXT: extractvalue
; CHECK-NEXT: ret
define i32 @nogep-volatile({i32, i32}* %pair) {
; The volatile load should be left unchanged.
%L = volatile load {i32, i32}* %pair
%E = extractvalue {i32, i32} %L, 1
ret i32 %E
}