Skip to content

Commit f891820

Browse files
committed
[LoopUnroll] Consider simplified operands while retrieving TTI instruction cost
Get more precise cost of instruction after LoopUnroll considering that some operands of it can be simplified, e.g. induction variable will be replaced by constant after full unrolling.
1 parent 1b6cdef commit f891820

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,15 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
443443

444444
// First accumulate the cost of this instruction.
445445
if (!Cost.IsFree) {
446-
UnrolledCost += TTI.getInstructionCost(I, CostKind);
446+
// Consider simplified operands in instruction cost.
447+
SmallVector<Value *, 4> Operands;
448+
transform(I->operands(), std::back_inserter(Operands),
449+
[&](Value *Op) {
450+
if (auto Res = SimplifiedValues.lookup(Op))
451+
return Res;
452+
return Op;
453+
});
454+
UnrolledCost += TTI.getInstructionCost(I, Operands, CostKind);
447455
LLVM_DEBUG(dbgs() << "Adding cost of instruction (iteration "
448456
<< Iteration << "): ");
449457
LLVM_DEBUG(I->dump());

llvm/test/Transforms/LoopUnroll/RISCV/unroll-Os.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ define void @foo(ptr %array, i32 %x) #0 {
88
; CHECK-NEXT: entry:
99
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
1010
; CHECK: for.body:
11-
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ]
12-
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[ARRAY]], i64 [[INDVARS_IV]]
13-
; CHECK-NEXT: store i32 [[X]], ptr [[ARRAYIDX]], align 4
14-
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
15-
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[INDVARS_IV_NEXT]], 4
16-
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_COND_CLEANUP:%.*]], label [[FOR_BODY]]
17-
; CHECK: for.cond.cleanup:
11+
; CHECK-NEXT: store i32 [[X]], ptr [[ARRAY]], align 4
12+
; CHECK-NEXT: [[ARRAYIDX_1:%.*]] = getelementptr inbounds i32, ptr [[ARRAY]], i64 1
13+
; CHECK-NEXT: store i32 [[X]], ptr [[ARRAYIDX_1]], align 4
14+
; CHECK-NEXT: [[ARRAYIDX_2:%.*]] = getelementptr inbounds i32, ptr [[ARRAY]], i64 2
15+
; CHECK-NEXT: store i32 [[X]], ptr [[ARRAYIDX_2]], align 4
16+
; CHECK-NEXT: [[ARRAYIDX_3:%.*]] = getelementptr inbounds i32, ptr [[ARRAY]], i64 3
17+
; CHECK-NEXT: store i32 [[X]], ptr [[ARRAYIDX_3]], align 4
1818
; CHECK-NEXT: ret void
1919
;
2020
entry:

0 commit comments

Comments
 (0)