Skip to content

Commit d661b67

Browse files
nikickbluck
authored andcommitted
[InstCombine] Remove root special case in demanded bits simplification
When calling SimplifyDemandedBits (as opposed to SimplifyDemandedInstructionBits), and there are multiple uses, always use SimplifyMultipleUseDemandedBits and drop the special case for root values. This fixes the ephemeral value detection, as seen by the restored assumes in tests. It may result in more or less simplification, depending on whether we get more out of having demanded bits or the ability to perform non-multi-use transforms. The change in the phi-known-bits.ll test is because the icmp operand now gets simplified based on demanded bits, which then prevents a different known bits simplification later. This also makes the code safe against future changes like llvm#97289, which add more context that would have to be discarded for the multi-use case.
1 parent 7c68249 commit d661b67

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

clang/test/CodeGen/inline-asm-x86-flag-output.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,10 @@ int test_assume_boolean_flag(long nr, volatile long *addr) {
380380
//CHECK: %0 = tail call { i32, i32 } asm "cmp $2,$1", "={@cca},={@ccae},=*m,r,~{cc},~{dirflag},~{fpsr},~{flags}"(ptr elementtype(i64) %addr, i64 %nr)
381381
//CHECK: [[RES1:%.*]] = extractvalue { i32, i32 } %0, 0
382382
//CHECK: [[RES2:%.*]] = extractvalue { i32, i32 } %0, 1
383-
//CHECK: %1 = icmp ult i32 [[RES2]], 2
383+
//CHECK: %1 = icmp ult i32 [[RES1]], 2
384384
//CHECK: tail call void @llvm.assume(i1 %1)
385+
//CHECK: %2 = icmp ult i32 [[RES2]], 2
386+
//CHECK: tail call void @llvm.assume(i1 %2)
385387
int x,y;
386388
asm("cmp %2,%1"
387389
: "=@cca"(x), "=@ccae"(y), "=m"(*(volatile long *)(addr))

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,11 @@ bool InstCombinerImpl::SimplifyDemandedBits(Instruction *I, unsigned OpNo,
117117
if (VInst->hasOneUse()) {
118118
// If the instruction has one use, we can directly simplify it.
119119
NewVal = SimplifyDemandedUseBits(VInst, DemandedMask, Known, Depth, Q);
120-
} else if (Depth != 0) {
121-
// If there are multiple uses of this instruction and we aren't at the root,
122-
// then we can simplify VInst to some other value, but not modify the
123-
// instruction.
124-
NewVal =
125-
SimplifyMultipleUseDemandedBits(VInst, DemandedMask, Known, Depth, Q);
126120
} else {
127-
// If this is the root being simplified, allow it to have multiple uses,
128-
// just set the DemandedMask to all bits and reset the context instruction.
129-
// This allows visitTruncInst (for example) to simplify the operand of a
130-
// trunc without duplicating all the SimplifyDemandedUseBits() logic.
121+
// If there are multiple uses of this instruction, then we can simplify
122+
// VInst to some other value, but not modify the instruction.
131123
NewVal =
132-
SimplifyDemandedUseBits(VInst, APInt::getAllOnes(Known.getBitWidth()),
133-
Known, Depth, Q.getWithInstruction(VInst));
124+
SimplifyMultipleUseDemandedBits(VInst, DemandedMask, Known, Depth, Q);
134125
}
135126
if (!NewVal) return false;
136127
if (Instruction* OpInst = dyn_cast<Instruction>(U))

llvm/test/Analysis/ValueTracking/phi-known-bits.ll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,13 @@ F:
375375
define i8 @phi_ugt_high_bits_and_known(i8 %xx) {
376376
; CHECK-LABEL: @phi_ugt_high_bits_and_known(
377377
; CHECK-NEXT: entry:
378-
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[XX:%.*]], -65
378+
; CHECK-NEXT: [[X:%.*]] = or i8 [[XX:%.*]], 1
379+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[XX]], -65
379380
; CHECK-NEXT: br i1 [[CMP]], label [[T:%.*]], label [[F:%.*]]
380381
; CHECK: T:
381-
; CHECK-NEXT: ret i8 65
382+
; CHECK-NEXT: [[V:%.*]] = phi i8 [ [[X]], [[ENTRY:%.*]] ], [ -1, [[F]] ]
383+
; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 65
384+
; CHECK-NEXT: ret i8 [[R]]
382385
; CHECK: F:
383386
; CHECK-NEXT: br label [[T]]
384387
;

llvm/test/Transforms/InstCombine/assume-inseltpoison.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ define i32 @PR40940(<4 x i8> %x) {
1515
; CHECK-LABEL: @PR40940(
1616
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i8> [[X:%.*]], <4 x i8> poison, <4 x i32> <i32 1, i32 1, i32 2, i32 3>
1717
; CHECK-NEXT: [[T2:%.*]] = bitcast <4 x i8> [[SHUF]] to i32
18+
; CHECK-NEXT: [[T3:%.*]] = icmp ult i32 [[T2]], 65536
19+
; CHECK-NEXT: call void @llvm.assume(i1 [[T3]])
1820
; CHECK-NEXT: ret i32 [[T2]]
1921
;
2022
%shuf = shufflevector <4 x i8> %x, <4 x i8> poison, <4 x i32> <i32 1, i32 1, i32 2, i32 3>

llvm/test/Transforms/InstCombine/assume.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ define i32 @PR40940(<4 x i8> %x) {
428428
; CHECK-LABEL: @PR40940(
429429
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i8> [[X:%.*]], <4 x i8> poison, <4 x i32> <i32 1, i32 1, i32 2, i32 3>
430430
; CHECK-NEXT: [[T2:%.*]] = bitcast <4 x i8> [[SHUF]] to i32
431+
; CHECK-NEXT: [[T3:%.*]] = icmp ult i32 [[T2]], 65536
432+
; CHECK-NEXT: call void @llvm.assume(i1 [[T3]])
431433
; CHECK-NEXT: ret i32 [[T2]]
432434
;
433435
%shuf = shufflevector <4 x i8> %x, <4 x i8> undef, <4 x i32> <i32 1, i32 1, i32 2, i32 3>

0 commit comments

Comments
 (0)