Skip to content

Commit da86d4a

Browse files
committed
[ValueTracking] Reduce duplication in haveNoCommonBitsSet() (NFC)
Extract a function and call it with both operand orders, so that we don't have to explicitly commute every single pattern.
1 parent 977af42 commit da86d4a

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -186,47 +186,30 @@ KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
186186
SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
187187
}
188188

189-
bool llvm::haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
190-
const WithCache<const Value *> &RHSCache,
191-
const SimplifyQuery &SQ) {
192-
const Value *LHS = LHSCache.getValue();
193-
const Value *RHS = RHSCache.getValue();
194-
195-
assert(LHS->getType() == RHS->getType() &&
196-
"LHS and RHS should have the same type");
197-
assert(LHS->getType()->isIntOrIntVectorTy() &&
198-
"LHS and RHS should be integers");
189+
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS,
190+
const Value *RHS) {
199191
// Look for an inverted mask: (X & ~M) op (Y & M).
200192
{
201193
Value *M;
202194
if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
203195
match(RHS, m_c_And(m_Specific(M), m_Value())))
204196
return true;
205-
if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
206-
match(LHS, m_c_And(m_Specific(M), m_Value())))
207-
return true;
208197
}
209198

210199
// X op (Y & ~X)
211-
if (match(RHS, m_c_And(m_Not(m_Specific(LHS)), m_Value())) ||
212-
match(LHS, m_c_And(m_Not(m_Specific(RHS)), m_Value())))
200+
if (match(RHS, m_c_And(m_Not(m_Specific(LHS)), m_Value())))
213201
return true;
214202

215203
// X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
216204
// for constant Y.
217205
Value *Y;
218-
if (match(RHS,
219-
m_c_Xor(m_c_And(m_Specific(LHS), m_Value(Y)), m_Deferred(Y))) ||
220-
match(LHS, m_c_Xor(m_c_And(m_Specific(RHS), m_Value(Y)), m_Deferred(Y))))
206+
if (match(RHS, m_c_Xor(m_c_And(m_Specific(LHS), m_Value(Y)), m_Deferred(Y))))
221207
return true;
222208

223209
// Peek through extends to find a 'not' of the other side:
224210
// (ext Y) op ext(~Y)
225-
// (ext ~Y) op ext(Y)
226-
if ((match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
227-
match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y))))) ||
228-
(match(RHS, m_ZExtOrSExt(m_Value(Y))) &&
229-
match(LHS, m_ZExtOrSExt(m_Not(m_Specific(Y))))))
211+
if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
212+
match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y)))))
230213
return true;
231214

232215
// Look for: (A & B) op ~(A | B)
@@ -235,11 +218,26 @@ bool llvm::haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
235218
if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
236219
match(RHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
237220
return true;
238-
if (match(RHS, m_And(m_Value(A), m_Value(B))) &&
239-
match(LHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
240-
return true;
241221
}
242222

223+
return false;
224+
}
225+
226+
bool llvm::haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
227+
const WithCache<const Value *> &RHSCache,
228+
const SimplifyQuery &SQ) {
229+
const Value *LHS = LHSCache.getValue();
230+
const Value *RHS = RHSCache.getValue();
231+
232+
assert(LHS->getType() == RHS->getType() &&
233+
"LHS and RHS should have the same type");
234+
assert(LHS->getType()->isIntOrIntVectorTy() &&
235+
"LHS and RHS should be integers");
236+
237+
if (haveNoCommonBitsSetSpecialCases(LHS, RHS) ||
238+
haveNoCommonBitsSetSpecialCases(RHS, LHS))
239+
return true;
240+
243241
return KnownBits::haveNoCommonBitsSet(LHSCache.getKnownBits(SQ),
244242
RHSCache.getKnownBits(SQ));
245243
}

0 commit comments

Comments
 (0)