Skip to content

Instantly share code, notes, and snippets.

@nikic
Created March 7, 2020 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikic/751f25b3b9d9e0860db5dde934f70f46 to your computer and use it in GitHub Desktop.
Save nikic/751f25b3b9d9e0860db5dde934f70f46 to your computer and use it in GitHub Desktop.
commit 089252cb30d457d731aaddab7a5ee0a4f3fafde7
Author: Nikita Popov <nikita.ppv@gmail.com>
Date: Fri Mar 6 22:08:14 2020 +0100
known bits experiment
diff --git llvm/lib/Transforms/InstCombine/InstructionCombining.cpp llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index eb4541eba13..5cbb88486ac 100644
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -116,6 +116,8 @@ using namespace llvm::PatternMatch;
STATISTIC(NumCombined , "Number of insts combined");
STATISTIC(NumConstProp, "Number of constant folds");
STATISTIC(NumConstPropKnownBits, "Number of constant folds using known bits");
+STATISTIC(NumConstPropKnownBitsComputed,
+ "Number of times known bits was computed for constant folding");
STATISTIC(NumDeadInst , "Number of dead inst eliminated");
STATISTIC(NumSunkInst , "Number of instructions sunk");
STATISTIC(NumExpand, "Number of expansions");
@@ -3453,26 +3455,6 @@ bool InstCombiner::run() {
}
}
- // In general, it is possible for computeKnownBits to determine all bits in
- // a value even when the operands are not all constants.
- Type *Ty = I->getType();
- if (ExpensiveCombines && !I->use_empty() && Ty->isIntOrIntVectorTy()) {
- KnownBits Known = computeKnownBits(I, /*Depth*/0, I);
- if (Known.isConstant()) {
- Constant *C = ConstantInt::get(Ty, Known.getConstant());
- LLVM_DEBUG(dbgs() << "IC: ConstFold (all bits known) to: " << *C
- << " from: " << *I << '\n');
-
- // Add operands to the worklist.
- replaceInstUsesWith(*I, C);
- ++NumConstPropKnownBits;
- if (isInstructionTriviallyDead(I, &TLI))
- eraseInstFromFunction(*I);
- MadeIRChange = true;
- continue;
- }
- }
-
// See if we can trivially sink this instruction to a successor basic block.
if (EnableCodeSinking && I->hasOneUse()) {
BasicBlock *BB = I->getParent();
@@ -3523,6 +3505,8 @@ bool InstCombiner::run() {
LLVM_DEBUG(raw_string_ostream SS(OrigI); I->print(SS); OrigI = SS.str(););
LLVM_DEBUG(dbgs() << "IC: Visiting: " << OrigI << '\n');
+ bool OldMadeIRChange = MadeIRChange;
+ MadeIRChange = false;
if (Instruction *Result = visit(*I)) {
++NumCombined;
// Should we replace the old instruction with a new one?
@@ -3568,6 +3552,31 @@ bool InstCombiner::run() {
}
}
MadeIRChange = true;
+ continue;
+ }
+
+ if (!MadeIRChange) {
+ // In general, it is possible for computeKnownBits to determine all bits
+ // in a value even when the operands are not all constants.
+ Type *Ty = I->getType();
+ if (ExpensiveCombines && !I->use_empty() && Ty->isIntOrIntVectorTy()) {
+ KnownBits Known = computeKnownBits(I, /*Depth*/0, I);
+ ++NumConstPropKnownBitsComputed;
+ if (Known.isConstant()) {
+ Constant *C = ConstantInt::get(Ty, Known.getConstant());
+ LLVM_DEBUG(dbgs() << "IC: ConstFold (all bits known) to: " << *C
+ << " from: " << *I << '\n');
+
+ // Add operands to the worklist.
+ replaceInstUsesWith(*I, C);
+ ++NumConstPropKnownBits;
+ if (isInstructionTriviallyDead(I, &TLI))
+ eraseInstFromFunction(*I);
+ MadeIRChange = true;
+ continue;
+ }
+ }
+ MadeIRChange = OldMadeIRChange;
}
}
diff --git llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll
index 02be57a4d15..fbab3275cf4 100644
--- llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll
+++ llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll
@@ -6,7 +6,7 @@ define i32 @test_out_of_bounds(i32 %a, i1 %x, i1 %y) {
; CHECK-LABEL: @test_out_of_bounds(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], 3
-; CHECK-NEXT: tail call void @llvm.assume(i1 false)
+; CHECK-NEXT: tail call void @llvm.assume(i1 undef)
; CHECK-NEXT: ret i32 [[AND1]]
;
entry:
@@ -20,7 +20,7 @@ entry:
define i128 @test_non64bit(i128 %a) {
; CHECK-LABEL: @test_non64bit(
; CHECK-NEXT: [[AND1:%.*]] = and i128 [[A:%.*]], 3
-; CHECK-NEXT: tail call void @llvm.assume(i1 false)
+; CHECK-NEXT: tail call void @llvm.assume(i1 undef)
; CHECK-NEXT: ret i128 [[AND1]]
;
%and1 = and i128 %a, 3
diff --git llvm/test/Transforms/InstCombine/phi-shifts.ll llvm/test/Transforms/InstCombine/phi-shifts.ll
index cc36c9d9e25..186ef1222f9 100644
--- llvm/test/Transforms/InstCombine/phi-shifts.ll
+++ llvm/test/Transforms/InstCombine/phi-shifts.ll
@@ -9,7 +9,7 @@ define i64 @fuzz15217(i1 %cond, i8* %Ptr, i64 %Val) {
; CHECK: two:
; CHECK-NEXT: br label [[END]]
; CHECK: end:
-; CHECK-NEXT: ret i64 0
+; CHECK-NEXT: ret i64 undef
;
entry:
br i1 %cond, label %end, label %two
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment