Skip to content

Commit 2b52e4e

Browse files
committed
[InstCombine] Remove known bits constant folding
If ExpensiveCombines is enabled (which is the case with -O3 on the legacy PM and always on the new PM), InstCombine tries to compute the known bits of all instructions in the hope that all bits end up being known, which is fairly expensive. How effective is it? If we add some statistics on how often the constant folding succeeds and how many KnownBits calculations are performed and run test-suite we get: "instcombine.NumConstPropKnownBits": 642, "instcombine.NumConstPropKnownBitsComputed": 18744965, In other words, we get one fold for every 30000 KnownBits calculations. However, the truth is actually much worse: Currently, known bits are computed before performing other folds, so there is a high chance that cases that get folded by known bits would also have been handled by other folds. What happens if we compute known bits after all other folds (hacky implementation: https://gist.github.com/nikic/751f25b3b9d9e0860db5dde934f70f46)? "instcombine.NumConstPropKnownBits": 0, "instcombine.NumConstPropKnownBitsComputed": 18105547, So it turns out despite doing 18 million known bits calculations, the known bits fold does not do anything useful on test-suite. I was originally planning to move this into AggressiveInstCombine so it only runs once in the pipeline, but seeing this, I think we're better off removing it entirely. As this is the only use of the "expensive combines" mechanism, it may be removed afterwards, but I'll leave that to a separate patch. Differential Revision: https://reviews.llvm.org/D75801
1 parent 9b95929 commit 2b52e4e

5 files changed

Lines changed: 27 additions & 76 deletions

File tree

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static cl::opt<bool>
130130
EnableCodeSinking("instcombine-code-sinking", cl::desc("Enable code sinking"),
131131
cl::init(true));
132132

133+
// FIXME: This option is no longer used for anything and may be removed.
133134
static cl::opt<bool>
134135
EnableExpensiveCombines("expensive-combines",
135136
cl::desc("Enable expensive instruction combines"));
@@ -3491,27 +3492,6 @@ bool InstCombiner::run() {
34913492
}
34923493
}
34933494

3494-
// In general, it is possible for computeKnownBits to determine all bits in
3495-
// a value even when the operands are not all constants.
3496-
Type *Ty = I->getType();
3497-
if (ExpensiveCombines && !I->use_empty() && Ty->isIntOrIntVectorTy() &&
3498-
!isMustTailCall(I)) {
3499-
KnownBits Known = computeKnownBits(I, /*Depth*/0, I);
3500-
if (Known.isConstant()) {
3501-
Constant *C = ConstantInt::get(Ty, Known.getConstant());
3502-
LLVM_DEBUG(dbgs() << "IC: ConstFold (all bits known) to: " << *C
3503-
<< " from: " << *I << '\n');
3504-
3505-
// Add operands to the worklist.
3506-
replaceInstUsesWith(*I, C);
3507-
++NumConstProp;
3508-
if (isInstructionTriviallyDead(I, &TLI))
3509-
eraseInstFromFunction(*I);
3510-
MadeIRChange = true;
3511-
continue;
3512-
}
3513-
}
3514-
35153495
// See if we can trivially sink this instruction to a successor basic block.
35163496
if (EnableCodeSinking && I->hasOneUse()) {
35173497
BasicBlock *BB = I->getParent();

llvm/test/Transforms/InstCombine/assume.ll

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,12 @@ define i1 @nonnull5(i32** %a) {
353353
; PR35846 - https://bugs.llvm.org/show_bug.cgi?id=35846
354354

355355
define i32 @assumption_conflicts_with_known_bits(i32 %a, i32 %b) {
356-
; EXPENSIVE-ON-LABEL: @assumption_conflicts_with_known_bits(
357-
; EXPENSIVE-ON-NEXT: tail call void @llvm.assume(i1 false)
358-
; EXPENSIVE-ON-NEXT: ret i32 0
359-
;
360-
; EXPENSIVE-OFF-LABEL: @assumption_conflicts_with_known_bits(
361-
; EXPENSIVE-OFF-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], 3
362-
; EXPENSIVE-OFF-NEXT: tail call void @llvm.assume(i1 false)
363-
; EXPENSIVE-OFF-NEXT: [[CMP2:%.*]] = icmp eq i32 [[AND1]], 0
364-
; EXPENSIVE-OFF-NEXT: tail call void @llvm.assume(i1 [[CMP2]])
365-
; EXPENSIVE-OFF-NEXT: ret i32 0
356+
; CHECK-LABEL: @assumption_conflicts_with_known_bits(
357+
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], 3
358+
; CHECK-NEXT: tail call void @llvm.assume(i1 false)
359+
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[AND1]], 0
360+
; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP2]])
361+
; CHECK-NEXT: ret i32 0
366362
;
367363
%and1 = and i32 %b, 3
368364
%B1 = lshr i32 %and1, %and1

llvm/test/Transforms/InstCombine/known-signbit-shift.ll

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ define i1 @test_shift_negative(i32 %a, i32 %b) {
3030
; If sign bit is a known zero, it cannot be a known one.
3131
; This test should not crash opt. The shift produces poison.
3232
define i32 @test_no_sign_bit_conflict1(i1 %b) {
33-
; EXPENSIVE-OFF-LABEL: @test_no_sign_bit_conflict1(
34-
; EXPENSIVE-OFF-NEXT: ret i32 undef
35-
;
36-
; EXPENSIVE-ON-LABEL: @test_no_sign_bit_conflict1(
37-
; EXPENSIVE-ON-NEXT: ret i32 0
33+
; CHECK-LABEL: @test_no_sign_bit_conflict1(
34+
; CHECK-NEXT: ret i32 undef
3835
;
3936
%sel = select i1 %b, i32 8193, i32 8192
4037
%mul = shl nsw i32 %sel, 18
@@ -44,11 +41,8 @@ define i32 @test_no_sign_bit_conflict1(i1 %b) {
4441
; If sign bit is a known one, it cannot be a known zero.
4542
; This test should not crash opt. The shift produces poison.
4643
define i32 @test_no_sign_bit_conflict2(i1 %b) {
47-
; EXPENSIVE-OFF-LABEL: @test_no_sign_bit_conflict2(
48-
; EXPENSIVE-OFF-NEXT: ret i32 undef
49-
;
50-
; EXPENSIVE-ON-LABEL: @test_no_sign_bit_conflict2(
51-
; EXPENSIVE-ON-NEXT: ret i32 0
44+
; CHECK-LABEL: @test_no_sign_bit_conflict2(
45+
; CHECK-NEXT: ret i32 undef
5246
;
5347
%sel = select i1 %b, i32 -8193, i32 -8194
5448
%mul = shl nsw i32 %sel, 18

llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44
; Check that we don't crash on unreasonable constant indexes
55

66
define i32 @test_out_of_bounds(i32 %a, i1 %x, i1 %y) {
7-
; EXPENSIVE-OFF-LABEL: @test_out_of_bounds(
8-
; EXPENSIVE-OFF-NEXT: entry:
9-
; EXPENSIVE-OFF-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], 3
10-
; EXPENSIVE-OFF-NEXT: tail call void @llvm.assume(i1 undef)
11-
; EXPENSIVE-OFF-NEXT: ret i32 [[AND1]]
12-
;
13-
; EXPENSIVE-ON-LABEL: @test_out_of_bounds(
14-
; EXPENSIVE-ON-NEXT: entry:
15-
; EXPENSIVE-ON-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], 3
16-
; EXPENSIVE-ON-NEXT: tail call void @llvm.assume(i1 false)
17-
; EXPENSIVE-ON-NEXT: ret i32 [[AND1]]
7+
; CHECK-LABEL: @test_out_of_bounds(
8+
; CHECK-NEXT: entry:
9+
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], 3
10+
; CHECK-NEXT: tail call void @llvm.assume(i1 undef)
11+
; CHECK-NEXT: ret i32 [[AND1]]
1812
;
1913
entry:
2014
%and1 = and i32 %a, 3
@@ -25,15 +19,10 @@ entry:
2519
}
2620

2721
define i128 @test_non64bit(i128 %a) {
28-
; EXPENSIVE-OFF-LABEL: @test_non64bit(
29-
; EXPENSIVE-OFF-NEXT: [[AND1:%.*]] = and i128 [[A:%.*]], 3
30-
; EXPENSIVE-OFF-NEXT: tail call void @llvm.assume(i1 undef)
31-
; EXPENSIVE-OFF-NEXT: ret i128 [[AND1]]
32-
;
33-
; EXPENSIVE-ON-LABEL: @test_non64bit(
34-
; EXPENSIVE-ON-NEXT: [[AND1:%.*]] = and i128 [[A:%.*]], 3
35-
; EXPENSIVE-ON-NEXT: tail call void @llvm.assume(i1 false)
36-
; EXPENSIVE-ON-NEXT: ret i128 [[AND1]]
22+
; CHECK-LABEL: @test_non64bit(
23+
; CHECK-NEXT: [[AND1:%.*]] = and i128 [[A:%.*]], 3
24+
; CHECK-NEXT: tail call void @llvm.assume(i1 undef)
25+
; CHECK-NEXT: ret i128 [[AND1]]
3726
;
3827
%and1 = and i128 %a, 3
3928
%B = lshr i128 %and1, -1

llvm/test/Transforms/InstCombine/phi-shifts.ll

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,13 @@
44

55
; OSS Fuzz: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=15217
66
define i64 @fuzz15217(i1 %cond, i8* %Ptr, i64 %Val) {
7-
; EXPENSIVE-OFF-LABEL: @fuzz15217(
8-
; EXPENSIVE-OFF-NEXT: entry:
9-
; EXPENSIVE-OFF-NEXT: br i1 [[COND:%.*]], label [[END:%.*]], label [[TWO:%.*]]
10-
; EXPENSIVE-OFF: two:
11-
; EXPENSIVE-OFF-NEXT: br label [[END]]
12-
; EXPENSIVE-OFF: end:
13-
; EXPENSIVE-OFF-NEXT: ret i64 undef
14-
;
15-
; EXPENSIVE-ON-LABEL: @fuzz15217(
16-
; EXPENSIVE-ON-NEXT: entry:
17-
; EXPENSIVE-ON-NEXT: br i1 [[COND:%.*]], label [[END:%.*]], label [[TWO:%.*]]
18-
; EXPENSIVE-ON: two:
19-
; EXPENSIVE-ON-NEXT: br label [[END]]
20-
; EXPENSIVE-ON: end:
21-
; EXPENSIVE-ON-NEXT: ret i64 0
7+
; CHECK-LABEL: @fuzz15217(
8+
; CHECK-NEXT: entry:
9+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[END:%.*]], label [[TWO:%.*]]
10+
; CHECK: two:
11+
; CHECK-NEXT: br label [[END]]
12+
; CHECK: end:
13+
; CHECK-NEXT: ret i64 undef
2214
;
2315
entry:
2416
br i1 %cond, label %end, label %two

0 commit comments

Comments
 (0)