Skip to content

Commit f289010

Browse files
[InlineCost] Simplify extractvalue across callsite
1 parent 1128a4f commit f289010

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

llvm/lib/Analysis/InlineCost.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,9 +2316,18 @@ bool CallAnalyzer::visitStore(StoreInst &I) {
23162316
}
23172317

23182318
bool CallAnalyzer::visitExtractValue(ExtractValueInst &I) {
2319-
// Constant folding for extract value is trivial.
2320-
if (simplifyInstruction(I))
2321-
return true;
2319+
Value *Op = I.getAggregateOperand();
2320+
2321+
// Special handling, because we want to simplify extractvalue with a
2322+
// potential insertvalue from the caller.
2323+
if (Value *SimpleOp = getSimplifiedValueUnchecked(Op)) {
2324+
SimplifyQuery SQ(DL);
2325+
Value *SimpleV = simplifyExtractValueInst(SimpleOp, I.getIndices(), SQ);
2326+
if (SimpleV) {
2327+
SimplifiedValues[&I] = SimpleV;
2328+
return true;
2329+
}
2330+
}
23222331

23232332
// SROA can't look through these, but they may be free.
23242333
return Base::visitExtractValue(I);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -S -passes=inline | FileCheck %s
3+
4+
define i32 @callee([2 x i32] %agg) {
5+
; CHECK-LABEL: define i32 @callee(
6+
; CHECK-SAME: [2 x i32] [[AGG:%.*]]) {
7+
; CHECK-NEXT: [[V:%.*]] = extractvalue [2 x i32] [[AGG]], 0
8+
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[V]], 0
9+
; CHECK-NEXT: br i1 [[C]], label %[[IS_NULL:.*]], label %[[NON_NULL:.*]]
10+
; CHECK: [[IS_NULL]]:
11+
; CHECK-NEXT: ret i32 0
12+
; CHECK: [[NON_NULL]]:
13+
; CHECK-NEXT: [[R:%.*]] = call i32 @callee([2 x i32] [[AGG]])
14+
; CHECK-NEXT: ret i32 [[R]]
15+
;
16+
%v = extractvalue [2 x i32] %agg, 0
17+
%c = icmp eq i32 %v, 0
18+
br i1 %c, label %is_null, label %non_null
19+
20+
is_null:
21+
ret i32 0
22+
23+
non_null:
24+
%r = call i32 @callee([2 x i32] %agg)
25+
ret i32 %r
26+
}
27+
28+
define i32 @caller(i32 %arg) {
29+
; CHECK-LABEL: define i32 @caller(
30+
; CHECK-SAME: i32 [[ARG:%.*]]) {
31+
; CHECK-NEXT: [[AGG0:%.*]] = insertvalue [2 x i32] poison, i32 0, 0
32+
; CHECK-NEXT: [[AGG1:%.*]] = insertvalue [2 x i32] [[AGG0]], i32 [[ARG]], 1
33+
; CHECK-NEXT: ret i32 0
34+
;
35+
%agg0 = insertvalue [2 x i32] poison, i32 0, 0
36+
%agg1 = insertvalue [2 x i32] %agg0, i32 %arg, 1
37+
%v = call i32 @callee([2 x i32] %agg1)
38+
ret i32 %v
39+
}

0 commit comments

Comments
 (0)