Skip to content

Commit 4a9dc5d

Browse files
committed
[InstCombine] Don't use dominating conditions to transform sub into xor.
Other passes are unable to reverse this transform if we use dominating conditions. Fixes #88239.
1 parent 0c05324 commit 4a9dc5d

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

llvm/include/llvm/Analysis/SimplifyQuery.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ struct SimplifyQuery {
113113
using namespace PatternMatch;
114114
return match(V, m_Undef());
115115
}
116+
117+
SimplifyQuery getWithoutDomCondCache() const {
118+
SimplifyQuery Copy(*this);
119+
Copy.DC = nullptr;
120+
return Copy;
121+
}
116122
};
117123

118124
} // end namespace llvm

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,8 +2281,10 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
22812281
if (match(Op0, m_APInt(Op0C))) {
22822282
if (Op0C->isMask()) {
22832283
// Turn this into a xor if LHS is 2^n-1 and the remaining bits are known
2284-
// zero.
2285-
KnownBits RHSKnown = computeKnownBits(Op1, 0, &I);
2284+
// zero. We don't use information from dominating conditions so this
2285+
// transform is easier to reverse if necessary.
2286+
KnownBits RHSKnown = llvm::computeKnownBits(
2287+
Op1, 0, SQ.getWithInstruction(&I).getWithoutDomCondCache());
22862288
if ((*Op0C | RHSKnown.Zero).isAllOnes())
22872289
return BinaryOperator::CreateXor(Op1, Op0);
22882290
}

llvm/test/Transforms/InstCombine/sub-xor.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ define <2 x i8> @xor_add_splat_undef(<2 x i8> %x) {
158158
ret <2 x i8> %add
159159
}
160160

161+
; Make sure we don't convert sub to xor using dominating condition. That makes
162+
; it hard for other passe to reverse.
161163
define i32 @xor_dominating_cond(i32 %x) {
162164
; CHECK-LABEL: @xor_dominating_cond(
163165
; CHECK-NEXT: entry:
164166
; CHECK-NEXT: [[COND:%.*]] = icmp ult i32 [[X:%.*]], 256
165167
; CHECK-NEXT: br i1 [[COND]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
166168
; CHECK: if.then:
167-
; CHECK-NEXT: [[A:%.*]] = xor i32 [[X]], 255
169+
; CHECK-NEXT: [[A:%.*]] = sub nuw nsw i32 255, [[X]]
168170
; CHECK-NEXT: ret i32 [[A]]
169171
; CHECK: if.end:
170172
; CHECK-NEXT: ret i32 [[X]]

0 commit comments

Comments
 (0)