blob: f7d9558ad27ee9f6a9dcee95116b3e81a9200909 [file] [log] [blame]
Chris Lattner0a8191e2010-01-05 07:50:36 +00001//===- InstCombineAndOrXor.cpp --------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the visitAnd, visitOr, and visitXor functions.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carrutha9174582015-01-22 05:25:13 +000014#include "InstCombineInternal.h"
Chris Lattner0a8191e2010-01-05 07:50:36 +000015#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000016#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Intrinsics.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000018#include "llvm/IR/PatternMatch.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Transforms/Utils/CmpInstAnalysis.h"
James Molloyf01488e2016-01-15 09:20:19 +000020#include "llvm/Transforms/Utils/Local.h"
Chris Lattner0a8191e2010-01-05 07:50:36 +000021using namespace llvm;
22using namespace PatternMatch;
23
Chandler Carruth964daaa2014-04-22 02:55:47 +000024#define DEBUG_TYPE "instcombine"
25
Sanjay Patel18549272015-09-08 18:24:36 +000026/// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate into
Tim Shenaec68b22016-06-29 20:10:17 +000027/// a four bit mask.
28static unsigned getFCmpCode(FCmpInst::Predicate CC) {
29 assert(FCmpInst::FCMP_FALSE <= CC && CC <= FCmpInst::FCMP_TRUE &&
30 "Unexpected FCmp predicate!");
31 // Take advantage of the bit pattern of FCmpInst::Predicate here.
32 // U L G E
33 static_assert(FCmpInst::FCMP_FALSE == 0, ""); // 0 0 0 0
34 static_assert(FCmpInst::FCMP_OEQ == 1, ""); // 0 0 0 1
35 static_assert(FCmpInst::FCMP_OGT == 2, ""); // 0 0 1 0
36 static_assert(FCmpInst::FCMP_OGE == 3, ""); // 0 0 1 1
37 static_assert(FCmpInst::FCMP_OLT == 4, ""); // 0 1 0 0
38 static_assert(FCmpInst::FCMP_OLE == 5, ""); // 0 1 0 1
39 static_assert(FCmpInst::FCMP_ONE == 6, ""); // 0 1 1 0
40 static_assert(FCmpInst::FCMP_ORD == 7, ""); // 0 1 1 1
41 static_assert(FCmpInst::FCMP_UNO == 8, ""); // 1 0 0 0
42 static_assert(FCmpInst::FCMP_UEQ == 9, ""); // 1 0 0 1
43 static_assert(FCmpInst::FCMP_UGT == 10, ""); // 1 0 1 0
44 static_assert(FCmpInst::FCMP_UGE == 11, ""); // 1 0 1 1
45 static_assert(FCmpInst::FCMP_ULT == 12, ""); // 1 1 0 0
46 static_assert(FCmpInst::FCMP_ULE == 13, ""); // 1 1 0 1
47 static_assert(FCmpInst::FCMP_UNE == 14, ""); // 1 1 1 0
48 static_assert(FCmpInst::FCMP_TRUE == 15, ""); // 1 1 1 1
49 return CC;
Chris Lattner0a8191e2010-01-05 07:50:36 +000050}
51
Sanjay Patel18549272015-09-08 18:24:36 +000052/// This is the complement of getICmpCode, which turns an opcode and two
53/// operands into either a constant true or false, or a brand new ICmp
54/// instruction. The sign is passed in to determine which kind of predicate to
55/// use in the new icmp instruction.
Benjamin Kramerbaba1aa2012-02-06 11:28:19 +000056static Value *getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
57 InstCombiner::BuilderTy *Builder) {
Pete Cooperebf98c12011-12-17 01:20:32 +000058 ICmpInst::Predicate NewPred;
59 if (Value *NewConstant = getICmpValue(Sign, Code, LHS, RHS, NewPred))
60 return NewConstant;
61 return Builder->CreateICmp(NewPred, LHS, RHS);
Chris Lattner0a8191e2010-01-05 07:50:36 +000062}
63
Sanjay Patel18549272015-09-08 18:24:36 +000064/// This is the complement of getFCmpCode, which turns an opcode and two
Tim Shenaec68b22016-06-29 20:10:17 +000065/// operands into either a FCmp instruction, or a true/false constant.
66static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS,
Chris Lattner067459c2010-03-05 08:46:26 +000067 InstCombiner::BuilderTy *Builder) {
Tim Shenaec68b22016-06-29 20:10:17 +000068 const auto Pred = static_cast<FCmpInst::Predicate>(Code);
69 assert(FCmpInst::FCMP_FALSE <= Pred && Pred <= FCmpInst::FCMP_TRUE &&
70 "Unexpected FCmp predicate!");
71 if (Pred == FCmpInst::FCMP_FALSE)
72 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
73 if (Pred == FCmpInst::FCMP_TRUE)
74 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
Chris Lattner067459c2010-03-05 08:46:26 +000075 return Builder->CreateFCmp(Pred, LHS, RHS);
Chris Lattner0a8191e2010-01-05 07:50:36 +000076}
77
Simon Pilgrimbe24ab32014-12-04 09:44:01 +000078/// \brief Transform BITWISE_OP(BSWAP(A),BSWAP(B)) to BSWAP(BITWISE_OP(A, B))
79/// \param I Binary operator to transform.
80/// \return Pointer to node that must replace the original binary operator, or
81/// null pointer if no transformation was made.
82Value *InstCombiner::SimplifyBSwap(BinaryOperator &I) {
83 IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
84
85 // Can't do vectors.
Sanjay Patel1e6ca442016-11-22 22:54:36 +000086 if (I.getType()->isVectorTy())
87 return nullptr;
Simon Pilgrimbe24ab32014-12-04 09:44:01 +000088
89 // Can only do bitwise ops.
Sanjay Patel1e6ca442016-11-22 22:54:36 +000090 if (!I.isBitwiseLogicOp())
Simon Pilgrimbe24ab32014-12-04 09:44:01 +000091 return nullptr;
92
93 Value *OldLHS = I.getOperand(0);
94 Value *OldRHS = I.getOperand(1);
95 ConstantInt *ConstLHS = dyn_cast<ConstantInt>(OldLHS);
96 ConstantInt *ConstRHS = dyn_cast<ConstantInt>(OldRHS);
97 IntrinsicInst *IntrLHS = dyn_cast<IntrinsicInst>(OldLHS);
98 IntrinsicInst *IntrRHS = dyn_cast<IntrinsicInst>(OldRHS);
99 bool IsBswapLHS = (IntrLHS && IntrLHS->getIntrinsicID() == Intrinsic::bswap);
100 bool IsBswapRHS = (IntrRHS && IntrRHS->getIntrinsicID() == Intrinsic::bswap);
101
102 if (!IsBswapLHS && !IsBswapRHS)
103 return nullptr;
104
105 if (!IsBswapLHS && !ConstLHS)
106 return nullptr;
107
108 if (!IsBswapRHS && !ConstRHS)
109 return nullptr;
110
111 /// OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) )
112 /// OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) )
113 Value *NewLHS = IsBswapLHS ? IntrLHS->getOperand(0) :
114 Builder->getInt(ConstLHS->getValue().byteSwap());
115
116 Value *NewRHS = IsBswapRHS ? IntrRHS->getOperand(0) :
117 Builder->getInt(ConstRHS->getValue().byteSwap());
118
Sanjay Patel1e6ca442016-11-22 22:54:36 +0000119 Value *BinOp = Builder->CreateBinOp(I.getOpcode(), NewLHS, NewRHS);
Sanjay Patelaf674fb2015-12-14 17:24:23 +0000120 Function *F = Intrinsic::getDeclaration(I.getModule(), Intrinsic::bswap, ITy);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +0000121 return Builder->CreateCall(F, BinOp);
122}
123
Sanjay Patel18549272015-09-08 18:24:36 +0000124/// This handles expressions of the form ((val OP C1) & C2). Where
Craig Topper70e4f432017-04-02 17:57:30 +0000125/// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'.
126Instruction *InstCombiner::OptAndOp(BinaryOperator *Op,
Chris Lattner0a8191e2010-01-05 07:50:36 +0000127 ConstantInt *OpRHS,
128 ConstantInt *AndRHS,
129 BinaryOperator &TheAnd) {
130 Value *X = Op->getOperand(0);
Craig Topperf40110f2014-04-25 05:29:35 +0000131 Constant *Together = nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000132 if (!Op->isShift())
133 Together = ConstantExpr::getAnd(AndRHS, OpRHS);
134
135 switch (Op->getOpcode()) {
Craig Topper70e4f432017-04-02 17:57:30 +0000136 default: break;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000137 case Instruction::Xor:
138 if (Op->hasOneUse()) {
139 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
140 Value *And = Builder->CreateAnd(X, AndRHS);
141 And->takeName(Op);
142 return BinaryOperator::CreateXor(And, Together);
143 }
144 break;
145 case Instruction::Or:
Owen Andersonc237a842010-09-13 17:59:27 +0000146 if (Op->hasOneUse()){
Owen Andersonc237a842010-09-13 17:59:27 +0000147 ConstantInt *TogetherCI = dyn_cast<ConstantInt>(Together);
148 if (TogetherCI && !TogetherCI->isZero()){
149 // (X | C1) & C2 --> (X & (C2^(C1&C2))) | C1
150 // NOTE: This reduces the number of bits set in the & mask, which
151 // can expose opportunities for store narrowing.
152 Together = ConstantExpr::getXor(AndRHS, Together);
153 Value *And = Builder->CreateAnd(X, Together);
154 And->takeName(Op);
155 return BinaryOperator::CreateOr(And, OpRHS);
156 }
Chris Lattner0a8191e2010-01-05 07:50:36 +0000157 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000158
Chris Lattner0a8191e2010-01-05 07:50:36 +0000159 break;
160 case Instruction::Add:
161 if (Op->hasOneUse()) {
162 // Adding a one to a single bit bit-field should be turned into an XOR
163 // of the bit. First thing to check is to see if this AND is with a
164 // single bit constant.
Jakub Staszak9de494e2013-06-06 00:49:57 +0000165 const APInt &AndRHSV = AndRHS->getValue();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000166
167 // If there is only one bit set.
168 if (AndRHSV.isPowerOf2()) {
169 // Ok, at this point, we know that we are masking the result of the
170 // ADD down to exactly one bit. If the constant we are adding has
171 // no bits set below this bit, then we can eliminate the ADD.
Jakub Staszak9de494e2013-06-06 00:49:57 +0000172 const APInt& AddRHS = OpRHS->getValue();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000173
174 // Check to see if any bits below the one bit set in AndRHSV are set.
175 if ((AddRHS & (AndRHSV-1)) == 0) {
176 // If not, the only thing that can effect the output of the AND is
177 // the bit specified by AndRHSV. If that bit is set, the effect of
178 // the XOR is to toggle the bit. If it is clear, then the ADD has
179 // no effect.
180 if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
181 TheAnd.setOperand(0, X);
182 return &TheAnd;
183 } else {
184 // Pull the XOR out of the AND.
185 Value *NewAnd = Builder->CreateAnd(X, AndRHS);
186 NewAnd->takeName(Op);
187 return BinaryOperator::CreateXor(NewAnd, AndRHS);
188 }
189 }
190 }
191 }
192 break;
193
194 case Instruction::Shl: {
195 // We know that the AND will not produce any of the bits shifted in, so if
196 // the anded constant includes them, clear them now!
197 //
198 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
199 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
200 APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000201 ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShlMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000202
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000203 if (CI->getValue() == ShlMask)
204 // Masking out bits that the shift already masks.
Sanjay Patel4b198802016-02-01 22:23:39 +0000205 return replaceInstUsesWith(TheAnd, Op); // No need for the and.
Craig Topper9d4171a2012-12-20 07:09:41 +0000206
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000207 if (CI != AndRHS) { // Reducing bits set in and.
Chris Lattner0a8191e2010-01-05 07:50:36 +0000208 TheAnd.setOperand(1, CI);
209 return &TheAnd;
210 }
211 break;
212 }
213 case Instruction::LShr: {
214 // We know that the AND will not produce any of the bits shifted in, so if
215 // the anded constant includes them, clear them now! This only applies to
216 // unsigned shifts, because a signed shr may bring in set bits!
217 //
218 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
219 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
220 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000221 ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShrMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000222
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000223 if (CI->getValue() == ShrMask)
224 // Masking out bits that the shift already masks.
Sanjay Patel4b198802016-02-01 22:23:39 +0000225 return replaceInstUsesWith(TheAnd, Op);
Craig Topper9d4171a2012-12-20 07:09:41 +0000226
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000227 if (CI != AndRHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000228 TheAnd.setOperand(1, CI); // Reduce bits set in and cst.
229 return &TheAnd;
230 }
231 break;
232 }
233 case Instruction::AShr:
234 // Signed shr.
235 // See if this is shifting in some sign extension, then masking it out
236 // with an and.
237 if (Op->hasOneUse()) {
238 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
239 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
240 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000241 Constant *C = Builder->getInt(AndRHS->getValue() & ShrMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000242 if (C == AndRHS) { // Masking out bits shifted in.
243 // (Val ashr C1) & C2 -> (Val lshr C1) & C2
244 // Make the argument unsigned.
245 Value *ShVal = Op->getOperand(0);
246 ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
247 return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
248 }
249 }
250 break;
251 }
Craig Topperf40110f2014-04-25 05:29:35 +0000252 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000253}
254
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000255/// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000256/// (V < Lo || V >= Hi). This method expects that Lo <= Hi. IsSigned indicates
257/// whether to treat V, Lo, and Hi as signed or not.
Sanjay Patel85d79742016-08-31 19:49:56 +0000258Value *InstCombiner::insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
Chris Lattner067459c2010-03-05 08:46:26 +0000259 bool isSigned, bool Inside) {
Sanjay Patel85d79742016-08-31 19:49:56 +0000260 assert((isSigned ? Lo.sle(Hi) : Lo.ule(Hi)) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +0000261 "Lo is not <= Hi in range emission code!");
Craig Topper9d4171a2012-12-20 07:09:41 +0000262
Sanjay Patel85d79742016-08-31 19:49:56 +0000263 Type *Ty = V->getType();
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000264 if (Lo == Hi)
Sanjay Patel85d79742016-08-31 19:49:56 +0000265 return Inside ? ConstantInt::getFalse(Ty) : ConstantInt::getTrue(Ty);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000266
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000267 // V >= Min && V < Hi --> V < Hi
268 // V < Min || V >= Hi --> V >= Hi
269 ICmpInst::Predicate Pred = Inside ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
Sanjay Patel85d79742016-08-31 19:49:56 +0000270 if (isSigned ? Lo.isMinSignedValue() : Lo.isMinValue()) {
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000271 Pred = isSigned ? ICmpInst::getSignedPredicate(Pred) : Pred;
Sanjay Patel85d79742016-08-31 19:49:56 +0000272 return Builder->CreateICmp(Pred, V, ConstantInt::get(Ty, Hi));
Chris Lattner0a8191e2010-01-05 07:50:36 +0000273 }
274
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000275 // V >= Lo && V < Hi --> V - Lo u< Hi - Lo
276 // V < Lo || V >= Hi --> V - Lo u>= Hi - Lo
Sanjay Patel85d79742016-08-31 19:49:56 +0000277 Value *VMinusLo =
278 Builder->CreateSub(V, ConstantInt::get(Ty, Lo), V->getName() + ".off");
279 Constant *HiMinusLo = ConstantInt::get(Ty, Hi - Lo);
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000280 return Builder->CreateICmp(Pred, VMinusLo, HiMinusLo);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000281}
282
Sanjay Patel77bf6222017-04-03 16:53:12 +0000283/// Classify (icmp eq (A & B), C) and (icmp ne (A & B), C) as matching patterns
284/// that can be simplified.
285/// One of A and B is considered the mask. The other is the value. This is
286/// described as the "AMask" or "BMask" part of the enum. If the enum contains
287/// only "Mask", then both A and B can be considered masks. If A is the mask,
288/// then it was proven that (A & C) == C. This is trivial if C == A or C == 0.
289/// If both A and C are constants, this proof is also easy.
290/// For the following explanations, we assume that A is the mask.
291///
292/// "AllOnes" declares that the comparison is true only if (A & B) == A or all
293/// bits of A are set in B.
294/// Example: (icmp eq (A & 3), 3) -> AMask_AllOnes
295///
296/// "AllZeros" declares that the comparison is true only if (A & B) == 0 or all
297/// bits of A are cleared in B.
298/// Example: (icmp eq (A & 3), 0) -> Mask_AllZeroes
299///
300/// "Mixed" declares that (A & B) == C and C might or might not contain any
301/// number of one bits and zero bits.
302/// Example: (icmp eq (A & 3), 1) -> AMask_Mixed
303///
304/// "Not" means that in above descriptions "==" should be replaced by "!=".
305/// Example: (icmp ne (A & 3), 3) -> AMask_NotAllOnes
306///
Owen Anderson3fe002d2010-09-08 22:16:17 +0000307/// If the mask A contains a single bit, then the following is equivalent:
308/// (icmp eq (A & B), A) equals (icmp ne (A & B), 0)
309/// (icmp ne (A & B), A) equals (icmp eq (A & B), 0)
310enum MaskedICmpType {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000311 AMask_AllOnes = 1,
312 AMask_NotAllOnes = 2,
313 BMask_AllOnes = 4,
314 BMask_NotAllOnes = 8,
315 Mask_AllZeros = 16,
316 Mask_NotAllZeros = 32,
317 AMask_Mixed = 64,
318 AMask_NotMixed = 128,
319 BMask_Mixed = 256,
320 BMask_NotMixed = 512
Owen Anderson3fe002d2010-09-08 22:16:17 +0000321};
322
Sanjay Patel77bf6222017-04-03 16:53:12 +0000323/// Return the set of patterns (from MaskedICmpType) that (icmp SCC (A & B), C)
324/// satisfies.
325static unsigned getMaskedICmpType(Value *A, Value *B, Value *C,
326 ICmpInst::Predicate Pred) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000327 ConstantInt *ACst = dyn_cast<ConstantInt>(A);
328 ConstantInt *BCst = dyn_cast<ConstantInt>(B);
329 ConstantInt *CCst = dyn_cast<ConstantInt>(C);
Sanjay Patel77bf6222017-04-03 16:53:12 +0000330 bool IsEq = (Pred == ICmpInst::ICMP_EQ);
331 bool IsAPow2 = (ACst && !ACst->isZero() && ACst->getValue().isPowerOf2());
332 bool IsBPow2 = (BCst && !BCst->isZero() && BCst->getValue().isPowerOf2());
333 unsigned MaskVal = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000334 if (CCst && CCst->isZero()) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000335 // if C is zero, then both A and B qualify as mask
Sanjay Patel77bf6222017-04-03 16:53:12 +0000336 MaskVal |= (IsEq ? (Mask_AllZeros | AMask_Mixed | BMask_Mixed)
337 : (Mask_NotAllZeros | AMask_NotMixed | BMask_NotMixed));
338 if (IsAPow2)
339 MaskVal |= (IsEq ? (AMask_NotAllOnes | AMask_NotMixed)
340 : (AMask_AllOnes | AMask_Mixed));
341 if (IsBPow2)
342 MaskVal |= (IsEq ? (BMask_NotAllOnes | BMask_NotMixed)
343 : (BMask_AllOnes | BMask_Mixed));
344 return MaskVal;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000345 }
Sanjay Patel77bf6222017-04-03 16:53:12 +0000346
Owen Anderson3fe002d2010-09-08 22:16:17 +0000347 if (A == C) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000348 MaskVal |= (IsEq ? (AMask_AllOnes | AMask_Mixed)
349 : (AMask_NotAllOnes | AMask_NotMixed));
350 if (IsAPow2)
351 MaskVal |= (IsEq ? (Mask_NotAllZeros | AMask_NotMixed)
352 : (Mask_AllZeros | AMask_Mixed));
353 } else if (ACst && CCst && ConstantExpr::getAnd(ACst, CCst) == CCst) {
354 MaskVal |= (IsEq ? AMask_Mixed : AMask_NotMixed);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000355 }
Sanjay Patel77bf6222017-04-03 16:53:12 +0000356
Craig Topperae48cb22012-12-20 07:15:54 +0000357 if (B == C) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000358 MaskVal |= (IsEq ? (BMask_AllOnes | BMask_Mixed)
359 : (BMask_NotAllOnes | BMask_NotMixed));
360 if (IsBPow2)
361 MaskVal |= (IsEq ? (Mask_NotAllZeros | BMask_NotMixed)
362 : (Mask_AllZeros | BMask_Mixed));
363 } else if (BCst && CCst && ConstantExpr::getAnd(BCst, CCst) == CCst) {
364 MaskVal |= (IsEq ? BMask_Mixed : BMask_NotMixed);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000365 }
Sanjay Patel77bf6222017-04-03 16:53:12 +0000366
367 return MaskVal;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000368}
369
Tim Northoverc0756c42013-09-04 11:57:13 +0000370/// Convert an analysis of a masked ICmp into its equivalent if all boolean
371/// operations had the opposite sense. Since each "NotXXX" flag (recording !=)
372/// is adjacent to the corresponding normal flag (recording ==), this just
373/// involves swapping those bits over.
374static unsigned conjugateICmpMask(unsigned Mask) {
375 unsigned NewMask;
Sanjay Patel77bf6222017-04-03 16:53:12 +0000376 NewMask = (Mask & (AMask_AllOnes | BMask_AllOnes | Mask_AllZeros |
377 AMask_Mixed | BMask_Mixed))
Tim Northoverc0756c42013-09-04 11:57:13 +0000378 << 1;
379
Sanjay Patel77bf6222017-04-03 16:53:12 +0000380 NewMask |= (Mask & (AMask_NotAllOnes | BMask_NotAllOnes | Mask_NotAllZeros |
381 AMask_NotMixed | BMask_NotMixed))
382 >> 1;
Tim Northoverc0756c42013-09-04 11:57:13 +0000383
384 return NewMask;
385}
386
Sanjay Patel77bf6222017-04-03 16:53:12 +0000387/// Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E).
388/// Return the set of pattern classes (from MaskedICmpType) that both LHS and
389/// RHS satisfy.
390static unsigned getMaskedTypeForICmpPair(Value *&A, Value *&B, Value *&C,
391 Value *&D, Value *&E, ICmpInst *LHS,
392 ICmpInst *RHS,
393 ICmpInst::Predicate &PredL,
394 ICmpInst::Predicate &PredR) {
395 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType())
396 return 0;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000397 // vectors are not (yet?) supported
Sanjay Patel77bf6222017-04-03 16:53:12 +0000398 if (LHS->getOperand(0)->getType()->isVectorTy())
399 return 0;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000400
401 // Here comes the tricky part:
Craig Topper9d4171a2012-12-20 07:09:41 +0000402 // LHS might be of the form L11 & L12 == X, X == L21 & L22,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000403 // and L11 & L12 == L21 & L22. The same goes for RHS.
404 // Now we must find those components L** and R**, that are equal, so
Craig Topper9d4171a2012-12-20 07:09:41 +0000405 // that we can extract the parameters A, B, C, D, and E for the canonical
Owen Anderson3fe002d2010-09-08 22:16:17 +0000406 // above.
407 Value *L1 = LHS->getOperand(0);
408 Value *L2 = LHS->getOperand(1);
Sanjay Patel77bf6222017-04-03 16:53:12 +0000409 Value *L11, *L12, *L21, *L22;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000410 // Check whether the icmp can be decomposed into a bit test.
Sanjay Patel77bf6222017-04-03 16:53:12 +0000411 if (decomposeBitTestICmp(LHS, PredL, L11, L12, L2)) {
Craig Topperf40110f2014-04-25 05:29:35 +0000412 L21 = L22 = L1 = nullptr;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000413 } else {
414 // Look for ANDs in the LHS icmp.
Tim Northoverdc647a22013-09-04 11:57:17 +0000415 if (!L1->getType()->isIntegerTy()) {
416 // You can icmp pointers, for example. They really aren't masks.
Craig Topperf40110f2014-04-25 05:29:35 +0000417 L11 = L12 = nullptr;
Tim Northoverdc647a22013-09-04 11:57:17 +0000418 } else if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) {
419 // Any icmp can be viewed as being trivially masked; if it allows us to
420 // remove one, it's worth it.
421 L11 = L1;
422 L12 = Constant::getAllOnesValue(L1->getType());
423 }
424
425 if (!L2->getType()->isIntegerTy()) {
426 // You can icmp pointers, for example. They really aren't masks.
Craig Topperf40110f2014-04-25 05:29:35 +0000427 L21 = L22 = nullptr;
Tim Northoverdc647a22013-09-04 11:57:17 +0000428 } else if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) {
429 L21 = L2;
430 L22 = Constant::getAllOnesValue(L2->getType());
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000431 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000432 }
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000433
434 // Bail if LHS was a icmp that can't be decomposed into an equality.
Sanjay Patel77bf6222017-04-03 16:53:12 +0000435 if (!ICmpInst::isEquality(PredL))
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000436 return 0;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000437
438 Value *R1 = RHS->getOperand(0);
439 Value *R2 = RHS->getOperand(1);
Sanjay Patel77bf6222017-04-03 16:53:12 +0000440 Value *R11, *R12;
441 bool Ok = false;
442 if (decomposeBitTestICmp(RHS, PredR, R11, R12, R2)) {
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000443 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000444 A = R11;
445 D = R12;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000446 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000447 A = R12;
448 D = R11;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000449 } else {
450 return 0;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000451 }
Sanjay Patel77bf6222017-04-03 16:53:12 +0000452 E = R2;
453 R1 = nullptr;
454 Ok = true;
Tim Northoverdc647a22013-09-04 11:57:17 +0000455 } else if (R1->getType()->isIntegerTy()) {
456 if (!match(R1, m_And(m_Value(R11), m_Value(R12)))) {
457 // As before, model no mask as a trivial mask if it'll let us do an
Mayur Pandey75b76c62014-08-19 06:41:55 +0000458 // optimization.
Tim Northoverdc647a22013-09-04 11:57:17 +0000459 R11 = R1;
460 R12 = Constant::getAllOnesValue(R1->getType());
461 }
462
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000463 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000464 A = R11;
465 D = R12;
466 E = R2;
467 Ok = true;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000468 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000469 A = R12;
470 D = R11;
471 E = R2;
472 Ok = true;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000473 }
474 }
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000475
476 // Bail if RHS was a icmp that can't be decomposed into an equality.
Sanjay Patel77bf6222017-04-03 16:53:12 +0000477 if (!ICmpInst::isEquality(PredR))
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000478 return 0;
479
Chad Rosier58919cc2016-05-09 21:37:43 +0000480 // Look for ANDs on the right side of the RHS icmp.
Sanjay Patel77bf6222017-04-03 16:53:12 +0000481 if (!Ok && R2->getType()->isIntegerTy()) {
Tim Northoverdc647a22013-09-04 11:57:17 +0000482 if (!match(R2, m_And(m_Value(R11), m_Value(R12)))) {
483 R11 = R2;
484 R12 = Constant::getAllOnesValue(R2->getType());
485 }
486
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000487 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000488 A = R11;
489 D = R12;
490 E = R1;
491 Ok = true;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000492 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000493 A = R12;
494 D = R11;
495 E = R1;
496 Ok = true;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000497 } else {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000498 return 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000499 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000500 }
Sanjay Patel77bf6222017-04-03 16:53:12 +0000501 if (!Ok)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000502 return 0;
503
504 if (L11 == A) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000505 B = L12;
506 C = L2;
Craig Topperae48cb22012-12-20 07:15:54 +0000507 } else if (L12 == A) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000508 B = L11;
509 C = L2;
Craig Topperae48cb22012-12-20 07:15:54 +0000510 } else if (L21 == A) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000511 B = L22;
512 C = L1;
Craig Topperae48cb22012-12-20 07:15:54 +0000513 } else if (L22 == A) {
Sanjay Patel77bf6222017-04-03 16:53:12 +0000514 B = L21;
515 C = L1;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000516 }
517
Sanjay Patel77bf6222017-04-03 16:53:12 +0000518 unsigned LeftType = getMaskedICmpType(A, B, C, PredL);
519 unsigned RightType = getMaskedICmpType(A, D, E, PredR);
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000520 return LeftType & RightType;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000521}
Sanjay Patel18549272015-09-08 18:24:36 +0000522
523/// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
524/// into a single (icmp(A & X) ==/!= Y).
David Majnemer1a3327b2014-11-18 09:31:36 +0000525static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
526 llvm::InstCombiner::BuilderTy *Builder) {
Craig Topperf40110f2014-04-25 05:29:35 +0000527 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr;
Sanjay Patel77bf6222017-04-03 16:53:12 +0000528 ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
529 unsigned Mask =
530 getMaskedTypeForICmpPair(A, B, C, D, E, LHS, RHS, PredL, PredR);
531 if (Mask == 0)
532 return nullptr;
533
534 assert(ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) &&
535 "Expected equality predicates for masked type of icmps.");
Owen Anderson3fe002d2010-09-08 22:16:17 +0000536
Tim Northoverc0756c42013-09-04 11:57:13 +0000537 // In full generality:
538 // (icmp (A & B) Op C) | (icmp (A & D) Op E)
539 // == ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ]
540 //
541 // If the latter can be converted into (icmp (A & X) Op Y) then the former is
542 // equivalent to (icmp (A & X) !Op Y).
543 //
544 // Therefore, we can pretend for the rest of this function that we're dealing
545 // with the conjunction, provided we flip the sense of any comparisons (both
546 // input and output).
547
548 // In most cases we're going to produce an EQ for the "&&" case.
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000549 ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
Tim Northoverc0756c42013-09-04 11:57:13 +0000550 if (!IsAnd) {
551 // Convert the masking analysis into its equivalent with negated
552 // comparisons.
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000553 Mask = conjugateICmpMask(Mask);
Tim Northoverc0756c42013-09-04 11:57:13 +0000554 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000555
Sanjay Patel77bf6222017-04-03 16:53:12 +0000556 if (Mask & Mask_AllZeros) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000557 // (icmp eq (A & B), 0) & (icmp eq (A & D), 0)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000558 // -> (icmp eq (A & (B|D)), 0)
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000559 Value *NewOr = Builder->CreateOr(B, D);
560 Value *NewAnd = Builder->CreateAnd(A, NewOr);
561 // We can't use C as zero because we might actually handle
Craig Topper9d4171a2012-12-20 07:09:41 +0000562 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000563 // with B and D, having a single bit set.
564 Value *Zero = Constant::getNullValue(A->getType());
565 return Builder->CreateICmp(NewCC, NewAnd, Zero);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000566 }
Sanjay Patel77bf6222017-04-03 16:53:12 +0000567 if (Mask & BMask_AllOnes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000568 // (icmp eq (A & B), B) & (icmp eq (A & D), D)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000569 // -> (icmp eq (A & (B|D)), (B|D))
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000570 Value *NewOr = Builder->CreateOr(B, D);
571 Value *NewAnd = Builder->CreateAnd(A, NewOr);
572 return Builder->CreateICmp(NewCC, NewAnd, NewOr);
Craig Topper9d4171a2012-12-20 07:09:41 +0000573 }
Sanjay Patel77bf6222017-04-03 16:53:12 +0000574 if (Mask & AMask_AllOnes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000575 // (icmp eq (A & B), A) & (icmp eq (A & D), A)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000576 // -> (icmp eq (A & (B&D)), A)
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000577 Value *NewAnd1 = Builder->CreateAnd(B, D);
578 Value *NewAnd2 = Builder->CreateAnd(A, NewAnd1);
579 return Builder->CreateICmp(NewCC, NewAnd2, A);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000580 }
Tim Northoverc0756c42013-09-04 11:57:13 +0000581
582 // Remaining cases assume at least that B and D are constant, and depend on
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000583 // their actual values. This isn't strictly necessary, just a "handle the
Tim Northoverc0756c42013-09-04 11:57:13 +0000584 // easy cases for now" decision.
585 ConstantInt *BCst = dyn_cast<ConstantInt>(B);
Sanjay Patel77bf6222017-04-03 16:53:12 +0000586 if (!BCst)
587 return nullptr;
Tim Northoverc0756c42013-09-04 11:57:13 +0000588 ConstantInt *DCst = dyn_cast<ConstantInt>(D);
Sanjay Patel77bf6222017-04-03 16:53:12 +0000589 if (!DCst)
590 return nullptr;
Tim Northoverc0756c42013-09-04 11:57:13 +0000591
Sanjay Patel77bf6222017-04-03 16:53:12 +0000592 if (Mask & (Mask_NotAllZeros | BMask_NotAllOnes)) {
Tim Northoverc0756c42013-09-04 11:57:13 +0000593 // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and
594 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
595 // -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0)
596 // Only valid if one of the masks is a superset of the other (check "B&D" is
597 // the same as either B or D).
598 APInt NewMask = BCst->getValue() & DCst->getValue();
599
600 if (NewMask == BCst->getValue())
601 return LHS;
602 else if (NewMask == DCst->getValue())
603 return RHS;
604 }
Sanjay Patel77bf6222017-04-03 16:53:12 +0000605
606 if (Mask & AMask_NotAllOnes) {
Tim Northoverc0756c42013-09-04 11:57:13 +0000607 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
608 // -> (icmp ne (A & B), A) or (icmp ne (A & D), A)
609 // Only valid if one of the masks is a superset of the other (check "B|D" is
610 // the same as either B or D).
611 APInt NewMask = BCst->getValue() | DCst->getValue();
612
613 if (NewMask == BCst->getValue())
614 return LHS;
615 else if (NewMask == DCst->getValue())
616 return RHS;
617 }
Sanjay Patel77bf6222017-04-03 16:53:12 +0000618
619 if (Mask & BMask_Mixed) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000620 // (icmp eq (A & B), C) & (icmp eq (A & D), E)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000621 // We already know that B & C == C && D & E == E.
622 // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of
623 // C and E, which are shared by both the mask B and the mask D, don't
624 // contradict, then we can transform to
625 // -> (icmp eq (A & (B|D)), (C|E))
626 // Currently, we only handle the case of B, C, D, and E being constant.
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000627 // We can't simply use C and E because we might actually handle
Craig Topper9d4171a2012-12-20 07:09:41 +0000628 // (icmp ne (A & B), B) & (icmp eq (A & D), D)
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000629 // with B and D, having a single bit set.
Owen Anderson3fe002d2010-09-08 22:16:17 +0000630 ConstantInt *CCst = dyn_cast<ConstantInt>(C);
Sanjay Patel77bf6222017-04-03 16:53:12 +0000631 if (!CCst)
632 return nullptr;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000633 ConstantInt *ECst = dyn_cast<ConstantInt>(E);
Sanjay Patel77bf6222017-04-03 16:53:12 +0000634 if (!ECst)
635 return nullptr;
636 if (PredL != NewCC)
David Majnemer1a3327b2014-11-18 09:31:36 +0000637 CCst = cast<ConstantInt>(ConstantExpr::getXor(BCst, CCst));
Sanjay Patel77bf6222017-04-03 16:53:12 +0000638 if (PredR != NewCC)
David Majnemer1a3327b2014-11-18 09:31:36 +0000639 ECst = cast<ConstantInt>(ConstantExpr::getXor(DCst, ECst));
Sanjay Patel77bf6222017-04-03 16:53:12 +0000640
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000641 // If there is a conflict, we should actually return a false for the
642 // whole construct.
David Majnemer1a3327b2014-11-18 09:31:36 +0000643 if (((BCst->getValue() & DCst->getValue()) &
644 (CCst->getValue() ^ ECst->getValue())) != 0)
David Majnemer6fdb6b82014-11-18 09:31:41 +0000645 return ConstantInt::get(LHS->getType(), !IsAnd);
Sanjay Patel77bf6222017-04-03 16:53:12 +0000646
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000647 Value *NewOr1 = Builder->CreateOr(B, D);
648 Value *NewOr2 = ConstantExpr::getOr(CCst, ECst);
649 Value *NewAnd = Builder->CreateAnd(A, NewOr1);
650 return Builder->CreateICmp(NewCC, NewAnd, NewOr2);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000651 }
Sanjay Patel77bf6222017-04-03 16:53:12 +0000652
Craig Topperf40110f2014-04-25 05:29:35 +0000653 return nullptr;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000654}
655
Erik Ecksteind1817522014-12-03 10:39:15 +0000656/// Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
657/// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
658/// If \p Inverted is true then the check is for the inverted range, e.g.
659/// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
660Value *InstCombiner::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1,
661 bool Inverted) {
662 // Check the lower range comparison, e.g. x >= 0
663 // InstCombine already ensured that if there is a constant it's on the RHS.
664 ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1));
665 if (!RangeStart)
666 return nullptr;
667
668 ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() :
669 Cmp0->getPredicate());
670
671 // Accept x > -1 or x >= 0 (after potentially inverting the predicate).
672 if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) ||
673 (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero())))
674 return nullptr;
675
676 ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() :
677 Cmp1->getPredicate());
678
679 Value *Input = Cmp0->getOperand(0);
680 Value *RangeEnd;
681 if (Cmp1->getOperand(0) == Input) {
682 // For the upper range compare we have: icmp x, n
683 RangeEnd = Cmp1->getOperand(1);
684 } else if (Cmp1->getOperand(1) == Input) {
685 // For the upper range compare we have: icmp n, x
686 RangeEnd = Cmp1->getOperand(0);
687 Pred1 = ICmpInst::getSwappedPredicate(Pred1);
688 } else {
689 return nullptr;
690 }
691
692 // Check the upper range comparison, e.g. x < n
693 ICmpInst::Predicate NewPred;
694 switch (Pred1) {
695 case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break;
696 case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break;
697 default: return nullptr;
698 }
699
700 // This simplification is only valid if the upper range is not negative.
701 bool IsNegative, IsNotNegative;
David Majnemer54c2ca22014-12-26 09:10:14 +0000702 ComputeSignBit(RangeEnd, IsNotNegative, IsNegative, /*Depth=*/0, Cmp1);
Erik Ecksteind1817522014-12-03 10:39:15 +0000703 if (!IsNotNegative)
704 return nullptr;
705
706 if (Inverted)
707 NewPred = ICmpInst::getInversePredicate(NewPred);
708
709 return Builder->CreateICmp(NewPred, Input, RangeEnd);
710}
711
Sanjay Patel7cfe4162017-04-14 19:23:50 +0000712static Value *
713foldAndOrOfEqualityCmpsWithConstants(ICmpInst *LHS, ICmpInst *RHS,
714 bool JoinedByAnd,
715 InstCombiner::BuilderTy *Builder) {
Sanjay Patelef9f5862017-04-15 17:55:06 +0000716 Value *X = LHS->getOperand(0);
717 if (X != RHS->getOperand(0))
Sanjay Patel7cfe4162017-04-14 19:23:50 +0000718 return nullptr;
719
Sanjay Patelef9f5862017-04-15 17:55:06 +0000720 const APInt *C1, *C2;
721 if (!match(LHS->getOperand(1), m_APInt(C1)) ||
722 !match(RHS->getOperand(1), m_APInt(C2)))
Sanjay Patel7cfe4162017-04-14 19:23:50 +0000723 return nullptr;
724
725 // We only handle (X != C1 && X != C2) and (X == C1 || X == C2).
726 ICmpInst::Predicate Pred = LHS->getPredicate();
727 if (Pred != RHS->getPredicate())
728 return nullptr;
729 if (JoinedByAnd && Pred != ICmpInst::ICMP_NE)
730 return nullptr;
731 if (!JoinedByAnd && Pred != ICmpInst::ICMP_EQ)
732 return nullptr;
733
734 // The larger unsigned constant goes on the right.
Sanjay Patelef9f5862017-04-15 17:55:06 +0000735 if (C1->ugt(*C2))
736 std::swap(C1, C2);
Sanjay Patel7cfe4162017-04-14 19:23:50 +0000737
Sanjay Patelef9f5862017-04-15 17:55:06 +0000738 APInt Xor = *C1 ^ *C2;
Sanjay Patel7cfe4162017-04-14 19:23:50 +0000739 if (Xor.isPowerOf2()) {
740 // If LHSC and RHSC differ by only one bit, then set that bit in X and
741 // compare against the larger constant:
742 // (X == C1 || X == C2) --> (X | (C1 ^ C2)) == C2
743 // (X != C1 && X != C2) --> (X | (C1 ^ C2)) != C2
744 // We choose an 'or' with a Pow2 constant rather than the inverse mask with
745 // 'and' because that may lead to smaller codegen from a smaller constant.
746 Value *Or = Builder->CreateOr(X, ConstantInt::get(X->getType(), Xor));
Sanjay Patelef9f5862017-04-15 17:55:06 +0000747 return Builder->CreateICmp(Pred, Or, ConstantInt::get(X->getType(), *C2));
Sanjay Patel7cfe4162017-04-14 19:23:50 +0000748 }
749
750 // Special case: get the ordering right when the values wrap around zero.
751 // Ie, we assumed the constants were unsigned when swapping earlier.
Sanjay Patelef9f5862017-04-15 17:55:06 +0000752 if (*C1 == 0 && C2->isAllOnesValue())
753 std::swap(C1, C2);
Sanjay Patel7cfe4162017-04-14 19:23:50 +0000754
Sanjay Patelef9f5862017-04-15 17:55:06 +0000755 if (*C1 == *C2 - 1) {
Sanjay Patel7cfe4162017-04-14 19:23:50 +0000756 // (X == 13 || X == 14) --> X - 13 <=u 1
757 // (X != 13 && X != 14) --> X - 13 >u 1
758 // An 'add' is the canonical IR form, so favor that over a 'sub'.
Sanjay Patelef9f5862017-04-15 17:55:06 +0000759 Value *Add = Builder->CreateAdd(X, ConstantInt::get(X->getType(), -(*C1)));
Sanjay Patel7cfe4162017-04-14 19:23:50 +0000760 auto NewPred = JoinedByAnd ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_ULE;
761 return Builder->CreateICmp(NewPred, Add, ConstantInt::get(X->getType(), 1));
762 }
763
764 return nullptr;
765}
766
Sanjay Patel18549272015-09-08 18:24:36 +0000767/// Fold (icmp)&(icmp) if possible.
Chris Lattner067459c2010-03-05 08:46:26 +0000768Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
Sanjay Patel519a87a2017-04-05 17:38:34 +0000769 ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000770
771 // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
Sanjay Patel519a87a2017-04-05 17:38:34 +0000772 if (PredicatesFoldable(PredL, PredR)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000773 if (LHS->getOperand(0) == RHS->getOperand(1) &&
774 LHS->getOperand(1) == RHS->getOperand(0))
775 LHS->swapOperands();
776 if (LHS->getOperand(0) == RHS->getOperand(0) &&
777 LHS->getOperand(1) == RHS->getOperand(1)) {
778 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
779 unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
780 bool isSigned = LHS->isSigned() || RHS->isSigned();
Pete Cooperebf98c12011-12-17 01:20:32 +0000781 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000782 }
783 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000784
Chris Lattnerdcef03f2011-02-10 05:17:27 +0000785 // handle (roughly): (icmp eq (A & B), C) & (icmp eq (A & D), E)
Tim Northoverc0756c42013-09-04 11:57:13 +0000786 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder))
Chris Lattnerdcef03f2011-02-10 05:17:27 +0000787 return V;
Craig Topper9d4171a2012-12-20 07:09:41 +0000788
Erik Ecksteind1817522014-12-03 10:39:15 +0000789 // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
790 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/false))
791 return V;
792
793 // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n
794 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/false))
795 return V;
796
Sanjay Patelef9f5862017-04-15 17:55:06 +0000797 if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, true, Builder))
798 return V;
799
Chris Lattner0a8191e2010-01-05 07:50:36 +0000800 // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
Sanjay Patele4159d22017-04-10 19:38:36 +0000801 Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0);
Sanjay Patel519a87a2017-04-05 17:38:34 +0000802 ConstantInt *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1));
803 ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS->getOperand(1));
804 if (!LHSC || !RHSC)
805 return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +0000806
Sanjay Patel519a87a2017-04-05 17:38:34 +0000807 if (LHSC == RHSC && PredL == PredR) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000808 // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
Sanjay Patelc2ceb8b2016-01-18 19:17:58 +0000809 // where C is a power of 2 or
Chris Lattner0a8191e2010-01-05 07:50:36 +0000810 // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
Sanjay Patel519a87a2017-04-05 17:38:34 +0000811 if ((PredL == ICmpInst::ICMP_ULT && LHSC->getValue().isPowerOf2()) ||
812 (PredL == ICmpInst::ICMP_EQ && LHSC->isZero())) {
Sanjay Patele4159d22017-04-10 19:38:36 +0000813 Value *NewOr = Builder->CreateOr(LHS0, RHS0);
Sanjay Patel519a87a2017-04-05 17:38:34 +0000814 return Builder->CreateICmp(PredL, NewOr, LHSC);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000815 }
816 }
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000817
Benjamin Kramer101720f2011-04-28 20:09:57 +0000818 // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000819 // where CMAX is the all ones value for the truncated type,
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000820 // iff the lower bits of C2 and CA are zero.
Sanjay Patel519a87a2017-04-05 17:38:34 +0000821 if (PredL == ICmpInst::ICMP_EQ && PredL == PredR && LHS->hasOneUse() &&
822 RHS->hasOneUse()) {
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000823 Value *V;
Sanjay Patel519a87a2017-04-05 17:38:34 +0000824 ConstantInt *AndC, *SmallC = nullptr, *BigC = nullptr;
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000825
826 // (trunc x) == C1 & (and x, CA) == C2
Craig Topperae48cb22012-12-20 07:15:54 +0000827 // (and x, CA) == C2 & (trunc x) == C1
Sanjay Patele4159d22017-04-10 19:38:36 +0000828 if (match(RHS0, m_Trunc(m_Value(V))) &&
829 match(LHS0, m_And(m_Specific(V), m_ConstantInt(AndC)))) {
Sanjay Patel519a87a2017-04-05 17:38:34 +0000830 SmallC = RHSC;
831 BigC = LHSC;
Sanjay Patele4159d22017-04-10 19:38:36 +0000832 } else if (match(LHS0, m_Trunc(m_Value(V))) &&
833 match(RHS0, m_And(m_Specific(V), m_ConstantInt(AndC)))) {
Sanjay Patel519a87a2017-04-05 17:38:34 +0000834 SmallC = LHSC;
835 BigC = RHSC;
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000836 }
837
Sanjay Patel519a87a2017-04-05 17:38:34 +0000838 if (SmallC && BigC) {
839 unsigned BigBitSize = BigC->getType()->getBitWidth();
840 unsigned SmallBitSize = SmallC->getType()->getBitWidth();
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000841
842 // Check that the low bits are zero.
843 APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
Sanjay Patel519a87a2017-04-05 17:38:34 +0000844 if ((Low & AndC->getValue()) == 0 && (Low & BigC->getValue()) == 0) {
845 Value *NewAnd = Builder->CreateAnd(V, Low | AndC->getValue());
846 APInt N = SmallC->getValue().zext(BigBitSize) | BigC->getValue();
847 Value *NewVal = ConstantInt::get(AndC->getType()->getContext(), N);
848 return Builder->CreateICmp(PredL, NewAnd, NewVal);
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000849 }
850 }
851 }
Benjamin Kramerda37e152012-01-08 18:32:24 +0000852
Chris Lattner0a8191e2010-01-05 07:50:36 +0000853 // From here on, we only handle:
854 // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
Sanjay Patele4159d22017-04-10 19:38:36 +0000855 if (LHS0 != RHS0)
Sanjay Patel519a87a2017-04-05 17:38:34 +0000856 return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +0000857
Sanjay Patel519a87a2017-04-05 17:38:34 +0000858 // ICMP_[US][GL]E X, C is folded to ICMP_[US][GL]T elsewhere.
859 if (PredL == ICmpInst::ICMP_UGE || PredL == ICmpInst::ICMP_ULE ||
860 PredR == ICmpInst::ICMP_UGE || PredR == ICmpInst::ICMP_ULE ||
861 PredL == ICmpInst::ICMP_SGE || PredL == ICmpInst::ICMP_SLE ||
862 PredR == ICmpInst::ICMP_SGE || PredR == ICmpInst::ICMP_SLE)
Craig Topperf40110f2014-04-25 05:29:35 +0000863 return nullptr;
Anders Carlssonda80afe2011-03-01 15:05:01 +0000864
Chris Lattner0a8191e2010-01-05 07:50:36 +0000865 // We can't fold (ugt x, C) & (sgt x, C2).
Sanjay Patel519a87a2017-04-05 17:38:34 +0000866 if (!PredicatesFoldable(PredL, PredR))
Craig Topperf40110f2014-04-25 05:29:35 +0000867 return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +0000868
Chris Lattner0a8191e2010-01-05 07:50:36 +0000869 // Ensure that the larger constant is on the RHS.
870 bool ShouldSwap;
Sanjay Patel28611ac2017-04-11 15:57:32 +0000871 if (CmpInst::isSigned(PredL) ||
872 (ICmpInst::isEquality(PredL) && CmpInst::isSigned(PredR)))
Sanjay Patel570e35c2017-04-10 16:55:57 +0000873 ShouldSwap = LHSC->getValue().sgt(RHSC->getValue());
Sanjay Patel28611ac2017-04-11 15:57:32 +0000874 else
875 ShouldSwap = LHSC->getValue().ugt(RHSC->getValue());
Craig Topper9d4171a2012-12-20 07:09:41 +0000876
Chris Lattner0a8191e2010-01-05 07:50:36 +0000877 if (ShouldSwap) {
878 std::swap(LHS, RHS);
Sanjay Patel519a87a2017-04-05 17:38:34 +0000879 std::swap(LHSC, RHSC);
880 std::swap(PredL, PredR);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000881 }
882
Dan Gohman4a618822010-02-10 16:03:48 +0000883 // At this point, we know we have two icmp instructions
Chris Lattner0a8191e2010-01-05 07:50:36 +0000884 // comparing a value against two constants and and'ing the result
885 // together. Because of the above check, we know that we only have
Craig Topper9d4171a2012-12-20 07:09:41 +0000886 // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
887 // (from the icmp folding check above), that the two constants
Chris Lattner0a8191e2010-01-05 07:50:36 +0000888 // are not equal and that the larger constant is on the RHS
Sanjay Patel519a87a2017-04-05 17:38:34 +0000889 assert(LHSC != RHSC && "Compares not folded above?");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000890
Sanjay Patel519a87a2017-04-05 17:38:34 +0000891 switch (PredL) {
892 default:
893 llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000894 case ICmpInst::ICMP_NE:
Sanjay Patel519a87a2017-04-05 17:38:34 +0000895 switch (PredR) {
896 default:
897 llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000898 case ICmpInst::ICMP_ULT:
Sanjay Patel519a87a2017-04-05 17:38:34 +0000899 if (LHSC == SubOne(RHSC)) // (X != 13 & X u< 14) -> X < 13
Sanjay Patele4159d22017-04-10 19:38:36 +0000900 return Builder->CreateICmpULT(LHS0, LHSC);
Sanjay Patel519a87a2017-04-05 17:38:34 +0000901 if (LHSC->isNullValue()) // (X != 0 & X u< 14) -> X-1 u< 13
Sanjay Patele4159d22017-04-10 19:38:36 +0000902 return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(),
Sanjay Patel85d79742016-08-31 19:49:56 +0000903 false, true);
Sanjay Patel519a87a2017-04-05 17:38:34 +0000904 break; // (X != 13 & X u< 15) -> no change
Chris Lattner0a8191e2010-01-05 07:50:36 +0000905 case ICmpInst::ICMP_SLT:
Sanjay Patel519a87a2017-04-05 17:38:34 +0000906 if (LHSC == SubOne(RHSC)) // (X != 13 & X s< 14) -> X < 13
Sanjay Patele4159d22017-04-10 19:38:36 +0000907 return Builder->CreateICmpSLT(LHS0, LHSC);
Sanjay Patel519a87a2017-04-05 17:38:34 +0000908 break; // (X != 13 & X s< 15) -> no change
Chris Lattner0a8191e2010-01-05 07:50:36 +0000909 case ICmpInst::ICMP_NE:
Sanjay Patel7cfe4162017-04-14 19:23:50 +0000910 // Potential folds for this case should already be handled.
911 break;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000912 }
913 break;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000914 case ICmpInst::ICMP_UGT:
Sanjay Patel519a87a2017-04-05 17:38:34 +0000915 switch (PredR) {
916 default:
917 llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000918 case ICmpInst::ICMP_NE:
Sanjay Patel519a87a2017-04-05 17:38:34 +0000919 if (RHSC == AddOne(LHSC)) // (X u> 13 & X != 14) -> X u> 14
Sanjay Patele4159d22017-04-10 19:38:36 +0000920 return Builder->CreateICmp(PredL, LHS0, RHSC);
Sanjay Patel519a87a2017-04-05 17:38:34 +0000921 break; // (X u> 13 & X != 15) -> no change
922 case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1
Sanjay Patele4159d22017-04-10 19:38:36 +0000923 return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(),
924 false, true);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000925 }
926 break;
927 case ICmpInst::ICMP_SGT:
Sanjay Patel519a87a2017-04-05 17:38:34 +0000928 switch (PredR) {
929 default:
930 llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000931 case ICmpInst::ICMP_NE:
Sanjay Patel519a87a2017-04-05 17:38:34 +0000932 if (RHSC == AddOne(LHSC)) // (X s> 13 & X != 14) -> X s> 14
Sanjay Patele4159d22017-04-10 19:38:36 +0000933 return Builder->CreateICmp(PredL, LHS0, RHSC);
Sanjay Patel519a87a2017-04-05 17:38:34 +0000934 break; // (X s> 13 & X != 15) -> no change
935 case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1
Sanjay Patele4159d22017-04-10 19:38:36 +0000936 return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(), true,
Sanjay Patel519a87a2017-04-05 17:38:34 +0000937 true);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000938 }
939 break;
940 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000941
Craig Topperf40110f2014-04-25 05:29:35 +0000942 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000943}
944
Sanjay Patel18549272015-09-08 18:24:36 +0000945/// Optimize (fcmp)&(fcmp). NOTE: Unlike the rest of instcombine, this returns
946/// a Value which should already be inserted into the function.
Chris Lattner067459c2010-03-05 08:46:26 +0000947Value *InstCombiner::FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
Tim Shenaec68b22016-06-29 20:10:17 +0000948 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
949 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
950 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
951
952 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
953 // Swap RHS operands to match LHS.
954 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
955 std::swap(Op1LHS, Op1RHS);
956 }
957
958 // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
959 // Suppose the relation between x and y is R, where R is one of
960 // U(1000), L(0100), G(0010) or E(0001), and CC0 and CC1 are the bitmasks for
961 // testing the desired relations.
962 //
963 // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
964 // bool(R & CC0) && bool(R & CC1)
965 // = bool((R & CC0) & (R & CC1))
966 // = bool(R & (CC0 & CC1)) <= by re-association, commutation, and idempotency
967 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
968 return getFCmpValue(getFCmpCode(Op0CC) & getFCmpCode(Op1CC), Op0LHS, Op0RHS,
969 Builder);
970
Chris Lattner0a8191e2010-01-05 07:50:36 +0000971 if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
972 RHS->getPredicate() == FCmpInst::FCMP_ORD) {
Benjamin Kramere89c7052013-04-12 21:56:23 +0000973 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType())
Craig Topperf40110f2014-04-25 05:29:35 +0000974 return nullptr;
Benjamin Kramere89c7052013-04-12 21:56:23 +0000975
Chris Lattner0a8191e2010-01-05 07:50:36 +0000976 // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
977 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
978 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
979 // If either of the constants are nans, then the whole thing returns
980 // false.
981 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000982 return Builder->getFalse();
Chris Lattner067459c2010-03-05 08:46:26 +0000983 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
Chris Lattner0a8191e2010-01-05 07:50:36 +0000984 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000985
Chris Lattner0a8191e2010-01-05 07:50:36 +0000986 // Handle vector zeros. This occurs because the canonical form of
987 // "fcmp ord x,x" is "fcmp ord x, 0".
988 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
989 isa<ConstantAggregateZero>(RHS->getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +0000990 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
Craig Topperf40110f2014-04-25 05:29:35 +0000991 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000992 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000993
Craig Topperf40110f2014-04-25 05:29:35 +0000994 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000995}
996
Sanjay Patelb54e62f2015-09-08 20:14:13 +0000997/// Match De Morgan's Laws:
998/// (~A & ~B) == (~(A | B))
999/// (~A | ~B) == (~(A & B))
1000static Instruction *matchDeMorgansLaws(BinaryOperator &I,
Sanjay Patel7caaa792017-05-09 20:05:05 +00001001 InstCombiner::BuilderTy &Builder) {
Sanjay Patelb54e62f2015-09-08 20:14:13 +00001002 auto Opcode = I.getOpcode();
1003 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1004 "Trying to match De Morgan's Laws with something other than and/or");
1005
Sanjay Patel7caaa792017-05-09 20:05:05 +00001006 // Flip the logic operation.
1007 Opcode = (Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
1008
1009 Value *A, *B;
1010 if (match(I.getOperand(0), m_OneUse(m_Not(m_Value(A)))) &&
1011 match(I.getOperand(1), m_OneUse(m_Not(m_Value(B)))) &&
1012 !IsFreeToInvert(A, A->hasOneUse()) &&
1013 !IsFreeToInvert(B, B->hasOneUse())) {
1014 Value *AndOr = Builder.CreateBinOp(Opcode, A, B, I.getName() + ".demorgan");
1015 return BinaryOperator::CreateNot(AndOr);
1016 }
Sanjay Patelb54e62f2015-09-08 20:14:13 +00001017
1018 return nullptr;
1019}
1020
Tobias Grosser8ef834c2016-07-19 09:06:08 +00001021bool InstCombiner::shouldOptimizeCast(CastInst *CI) {
1022 Value *CastSrc = CI->getOperand(0);
1023
1024 // Noop casts and casts of constants should be eliminated trivially.
1025 if (CI->getSrcTy() == CI->getDestTy() || isa<Constant>(CastSrc))
1026 return false;
1027
1028 // If this cast is paired with another cast that can be eliminated, we prefer
1029 // to have it eliminated.
1030 if (const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc))
1031 if (isEliminableCastPair(PrecedingCI, CI))
1032 return false;
1033
1034 // If this is a vector sext from a compare, then we don't want to break the
1035 // idiom where each element of the extended vector is either zero or all ones.
1036 if (CI->getOpcode() == Instruction::SExt &&
1037 isa<CmpInst>(CastSrc) && CI->getDestTy()->isVectorTy())
1038 return false;
1039
1040 return true;
1041}
1042
Sanjay Patel60312bc42016-09-12 00:16:23 +00001043/// Fold {and,or,xor} (cast X), C.
1044static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast,
1045 InstCombiner::BuilderTy *Builder) {
1046 Constant *C;
1047 if (!match(Logic.getOperand(1), m_Constant(C)))
1048 return nullptr;
1049
1050 auto LogicOpc = Logic.getOpcode();
1051 Type *DestTy = Logic.getType();
1052 Type *SrcTy = Cast->getSrcTy();
1053
1054 // If the first operand is bitcast, move the logic operation ahead of the
1055 // bitcast (do the logic operation in the original type). This can eliminate
1056 // bitcasts and allow combines that would otherwise be impeded by the bitcast.
1057 Value *X;
1058 if (match(Cast, m_BitCast(m_Value(X)))) {
1059 Value *NewConstant = ConstantExpr::getBitCast(C, SrcTy);
1060 Value *NewOp = Builder->CreateBinOp(LogicOpc, X, NewConstant);
1061 return CastInst::CreateBitOrPointerCast(NewOp, DestTy);
1062 }
1063
1064 // Similarly, move the logic operation ahead of a zext if the constant is
1065 // unchanged in the smaller source type. Performing the logic in a smaller
1066 // type may provide more information to later folds, and the smaller logic
1067 // instruction may be cheaper (particularly in the case of vectors).
1068 if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) {
1069 Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy);
1070 Constant *ZextTruncC = ConstantExpr::getZExt(TruncC, DestTy);
1071 if (ZextTruncC == C) {
1072 // LogicOpc (zext X), C --> zext (LogicOpc X, C)
1073 Value *NewOp = Builder->CreateBinOp(LogicOpc, X, TruncC);
1074 return new ZExtInst(NewOp, DestTy);
1075 }
1076 }
1077
1078 return nullptr;
1079}
1080
1081/// Fold {and,or,xor} (cast X), Y.
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001082Instruction *InstCombiner::foldCastedBitwiseLogic(BinaryOperator &I) {
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001083 auto LogicOpc = I.getOpcode();
Sanjay Patel1e6ca442016-11-22 22:54:36 +00001084 assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding");
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001085
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001086 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patel713f25e2016-02-23 17:41:34 +00001087 CastInst *Cast0 = dyn_cast<CastInst>(Op0);
Sanjay Patel9bba7502016-03-03 19:19:04 +00001088 if (!Cast0)
Sanjay Patel7d0d8102016-02-23 16:59:21 +00001089 return nullptr;
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001090
Sanjay Patel9bba7502016-03-03 19:19:04 +00001091 // This must be a cast from an integer or integer vector source type to allow
1092 // transformation of the logic operation to the source type.
1093 Type *DestTy = I.getType();
Sanjay Patel713f25e2016-02-23 17:41:34 +00001094 Type *SrcTy = Cast0->getSrcTy();
Sanjay Patel9bba7502016-03-03 19:19:04 +00001095 if (!SrcTy->isIntOrIntVectorTy())
1096 return nullptr;
1097
Sanjay Patel60312bc42016-09-12 00:16:23 +00001098 if (Instruction *Ret = foldLogicCastConstant(I, Cast0, Builder))
1099 return Ret;
Sanjay Patel0753c062016-07-21 00:24:18 +00001100
Sanjay Patel9bba7502016-03-03 19:19:04 +00001101 CastInst *Cast1 = dyn_cast<CastInst>(Op1);
1102 if (!Cast1)
1103 return nullptr;
1104
1105 // Both operands of the logic operation are casts. The casts must be of the
1106 // same type for reduction.
1107 auto CastOpcode = Cast0->getOpcode();
1108 if (CastOpcode != Cast1->getOpcode() || SrcTy != Cast1->getSrcTy())
Sanjay Patel713f25e2016-02-23 17:41:34 +00001109 return nullptr;
1110
1111 Value *Cast0Src = Cast0->getOperand(0);
1112 Value *Cast1Src = Cast1->getOperand(0);
Sanjay Patel713f25e2016-02-23 17:41:34 +00001113
Tobias Grosser8ef834c2016-07-19 09:06:08 +00001114 // fold logic(cast(A), cast(B)) -> cast(logic(A, B))
Tobias Grosser8757e382016-08-03 19:30:35 +00001115 if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001116 Value *NewOp = Builder->CreateBinOp(LogicOpc, Cast0Src, Cast1Src,
1117 I.getName());
Sanjay Patel713f25e2016-02-23 17:41:34 +00001118 return CastInst::Create(CastOpcode, NewOp, DestTy);
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001119 }
Sanjay Patel713f25e2016-02-23 17:41:34 +00001120
Sanjay Pateldbbaca02016-02-24 17:00:34 +00001121 // For now, only 'and'/'or' have optimizations after this.
1122 if (LogicOpc == Instruction::Xor)
1123 return nullptr;
1124
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001125 // If this is logic(cast(icmp), cast(icmp)), try to fold this even if the
Sanjay Patel713f25e2016-02-23 17:41:34 +00001126 // cast is otherwise not optimizable. This happens for vector sexts.
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001127 ICmpInst *ICmp0 = dyn_cast<ICmpInst>(Cast0Src);
1128 ICmpInst *ICmp1 = dyn_cast<ICmpInst>(Cast1Src);
1129 if (ICmp0 && ICmp1) {
1130 Value *Res = LogicOpc == Instruction::And ? FoldAndOfICmps(ICmp0, ICmp1)
1131 : FoldOrOfICmps(ICmp0, ICmp1, &I);
1132 if (Res)
1133 return CastInst::Create(CastOpcode, Res, DestTy);
1134 return nullptr;
1135 }
Sanjay Patel713f25e2016-02-23 17:41:34 +00001136
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001137 // If this is logic(cast(fcmp), cast(fcmp)), try to fold this even if the
Sanjay Patel713f25e2016-02-23 17:41:34 +00001138 // cast is otherwise not optimizable. This happens for vector sexts.
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001139 FCmpInst *FCmp0 = dyn_cast<FCmpInst>(Cast0Src);
1140 FCmpInst *FCmp1 = dyn_cast<FCmpInst>(Cast1Src);
1141 if (FCmp0 && FCmp1) {
1142 Value *Res = LogicOpc == Instruction::And ? FoldAndOfFCmps(FCmp0, FCmp1)
1143 : FoldOrOfFCmps(FCmp0, FCmp1);
1144 if (Res)
1145 return CastInst::Create(CastOpcode, Res, DestTy);
1146 return nullptr;
1147 }
Sanjay Patel713f25e2016-02-23 17:41:34 +00001148
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001149 return nullptr;
1150}
1151
Sanjay Patel74d23ad2016-05-27 21:41:29 +00001152static Instruction *foldBoolSextMaskToSelect(BinaryOperator &I) {
1153 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1154
1155 // Canonicalize SExt or Not to the LHS
1156 if (match(Op1, m_SExt(m_Value())) || match(Op1, m_Not(m_Value()))) {
1157 std::swap(Op0, Op1);
1158 }
1159
1160 // Fold (and (sext bool to A), B) --> (select bool, B, 0)
1161 Value *X = nullptr;
1162 if (match(Op0, m_SExt(m_Value(X))) &&
1163 X->getType()->getScalarType()->isIntegerTy(1)) {
1164 Value *Zero = Constant::getNullValue(Op1->getType());
1165 return SelectInst::Create(X, Op1, Zero);
1166 }
1167
1168 // Fold (and ~(sext bool to A), B) --> (select bool, 0, B)
1169 if (match(Op0, m_Not(m_SExt(m_Value(X)))) &&
1170 X->getType()->getScalarType()->isIntegerTy(1)) {
1171 Value *Zero = Constant::getNullValue(Op0->getType());
1172 return SelectInst::Create(X, Zero, Op1);
1173 }
Justin Bognerc7e4fbe2016-08-05 01:09:48 +00001174
Sanjay Patel74d23ad2016-05-27 21:41:29 +00001175 return nullptr;
1176}
1177
Sanjay Patele0c26e02017-04-23 22:00:02 +00001178static Instruction *foldAndToXor(BinaryOperator &I,
1179 InstCombiner::BuilderTy &Builder) {
1180 assert(I.getOpcode() == Instruction::And);
1181 Value *Op0 = I.getOperand(0);
1182 Value *Op1 = I.getOperand(1);
1183 Value *A, *B;
1184
1185 // Operand complexity canonicalization guarantees that the 'or' is Op0.
1186 // (A | B) & ~(A & B) --> A ^ B
1187 // (A | B) & ~(B & A) --> A ^ B
1188 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1189 match(Op1, m_Not(m_c_And(m_Specific(A), m_Specific(B)))))
1190 return BinaryOperator::CreateXor(A, B);
1191
1192 // (A | ~B) & (~A | B) --> ~(A ^ B)
1193 // (A | ~B) & (B | ~A) --> ~(A ^ B)
1194 // (~B | A) & (~A | B) --> ~(A ^ B)
1195 // (~B | A) & (B | ~A) --> ~(A ^ B)
1196 if (match(Op0, m_c_Or(m_Value(A), m_Not(m_Value(B)))) &&
Sanjay Patel73d8c432017-04-27 21:55:03 +00001197 match(Op1, m_c_Or(m_Not(m_Specific(A)), m_Specific(B))))
Sanjay Patele0c26e02017-04-23 22:00:02 +00001198 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
1199
1200 return nullptr;
1201}
1202
1203static Instruction *foldOrToXor(BinaryOperator &I,
1204 InstCombiner::BuilderTy &Builder) {
1205 assert(I.getOpcode() == Instruction::Or);
1206 Value *Op0 = I.getOperand(0);
1207 Value *Op1 = I.getOperand(1);
1208 Value *A, *B;
1209
1210 // Operand complexity canonicalization guarantees that the 'and' is Op0.
1211 // (A & B) | ~(A | B) --> ~(A ^ B)
1212 // (A & B) | ~(B | A) --> ~(A ^ B)
1213 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
1214 match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
1215 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
1216
1217 // (A & ~B) | (~A & B) --> A ^ B
1218 // (A & ~B) | (B & ~A) --> A ^ B
1219 // (~B & A) | (~A & B) --> A ^ B
1220 // (~B & A) | (B & ~A) --> A ^ B
1221 if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
1222 match(Op1, m_c_And(m_Not(m_Specific(A)), m_Specific(B))))
1223 return BinaryOperator::CreateXor(A, B);
1224
1225 return nullptr;
1226}
1227
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00001228// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
1229// here. We should standardize that construct where it is needed or choose some
1230// other way to ensure that commutated variants of patterns are not missed.
Chris Lattner0a8191e2010-01-05 07:50:36 +00001231Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00001232 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001233 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1234
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001235 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00001236 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001237
Daniel Berlin2c75c632017-04-26 20:56:07 +00001238 if (Value *V = SimplifyAndInst(Op0, Op1, SQ))
Sanjay Patel4b198802016-02-01 22:23:39 +00001239 return replaceInstUsesWith(I, V);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001240
Craig Topper9d4171a2012-12-20 07:09:41 +00001241 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00001242 // purpose is to compute bits we don't care about.
1243 if (SimplifyDemandedInstructionBits(I))
Craig Topper9d4171a2012-12-20 07:09:41 +00001244 return &I;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001245
Sanjay Patele0c26e02017-04-23 22:00:02 +00001246 // Do this before using distributive laws to catch simple and/or/not patterns.
1247 if (Instruction *Xor = foldAndToXor(I, *Builder))
1248 return Xor;
1249
1250 // (A|B)&(A|C) -> A|(B&C) etc
1251 if (Value *V = SimplifyUsingDistributiveLaws(I))
1252 return replaceInstUsesWith(I, V);
1253
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00001254 if (Value *V = SimplifyBSwap(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00001255 return replaceInstUsesWith(I, V);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00001256
Chris Lattner0a8191e2010-01-05 07:50:36 +00001257 if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1258 const APInt &AndRHSMask = AndRHS->getValue();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001259
1260 // Optimize a variety of ((val OP C1) & C2) combinations...
1261 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1262 Value *Op0LHS = Op0I->getOperand(0);
1263 Value *Op0RHS = Op0I->getOperand(1);
1264 switch (Op0I->getOpcode()) {
1265 default: break;
1266 case Instruction::Xor:
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001267 case Instruction::Or: {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001268 // If the mask is only needed on one incoming arm, push it up.
1269 if (!Op0I->hasOneUse()) break;
Craig Topper9d4171a2012-12-20 07:09:41 +00001270
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001271 APInt NotAndRHS(~AndRHSMask);
Hal Finkel60db0582014-09-07 18:57:58 +00001272 if (MaskedValueIsZero(Op0LHS, NotAndRHS, 0, &I)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001273 // Not masking anything out for the LHS, move to RHS.
1274 Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
1275 Op0RHS->getName()+".masked");
1276 return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
1277 }
1278 if (!isa<Constant>(Op0RHS) &&
Hal Finkel60db0582014-09-07 18:57:58 +00001279 MaskedValueIsZero(Op0RHS, NotAndRHS, 0, &I)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001280 // Not masking anything out for the RHS, move to LHS.
1281 Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
1282 Op0LHS->getName()+".masked");
1283 return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
1284 }
1285
1286 break;
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001287 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001288 case Instruction::Sub:
Balaram Makamccf59732015-08-20 15:35:00 +00001289 // -x & 1 -> x & 1
1290 if (AndRHSMask == 1 && match(Op0LHS, m_Zero()))
1291 return BinaryOperator::CreateAnd(Op0RHS, AndRHS);
1292
Chris Lattner0a8191e2010-01-05 07:50:36 +00001293 break;
1294
1295 case Instruction::Shl:
1296 case Instruction::LShr:
1297 // (1 << x) & 1 --> zext(x == 0)
1298 // (1 >> x) & 1 --> zext(x == 0)
1299 if (AndRHSMask == 1 && Op0LHS == AndRHS) {
1300 Value *NewICmp =
1301 Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
1302 return new ZExtInst(NewICmp, I.getType());
1303 }
1304 break;
1305 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001306
David Majnemerde55c602017-01-17 18:08:06 +00001307 // ((C1 OP zext(X)) & C2) -> zext((C1-X) & C2) if C2 fits in the bitwidth
1308 // of X and OP behaves well when given trunc(C1) and X.
1309 switch (Op0I->getOpcode()) {
1310 default:
1311 break;
1312 case Instruction::Xor:
1313 case Instruction::Or:
1314 case Instruction::Mul:
1315 case Instruction::Add:
1316 case Instruction::Sub:
1317 Value *X;
1318 ConstantInt *C1;
Craig Topperb5194ee2017-04-12 05:49:28 +00001319 if (match(Op0I, m_c_BinOp(m_ZExt(m_Value(X)), m_ConstantInt(C1)))) {
David Majnemerde55c602017-01-17 18:08:06 +00001320 if (AndRHSMask.isIntN(X->getType()->getScalarSizeInBits())) {
1321 auto *TruncC1 = ConstantExpr::getTrunc(C1, X->getType());
1322 Value *BinOp;
1323 if (isa<ZExtInst>(Op0LHS))
1324 BinOp = Builder->CreateBinOp(Op0I->getOpcode(), X, TruncC1);
1325 else
1326 BinOp = Builder->CreateBinOp(Op0I->getOpcode(), TruncC1, X);
1327 auto *TruncC2 = ConstantExpr::getTrunc(AndRHS, X->getType());
1328 auto *And = Builder->CreateAnd(BinOp, TruncC2);
1329 return new ZExtInst(And, I.getType());
1330 }
1331 }
1332 }
1333
Chris Lattner0a8191e2010-01-05 07:50:36 +00001334 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1335 if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1336 return Res;
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001337 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001338
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001339 // If this is an integer truncation, and if the source is an 'and' with
1340 // immediate, transform it. This frequently occurs for bitfield accesses.
1341 {
Craig Topperf40110f2014-04-25 05:29:35 +00001342 Value *X = nullptr; ConstantInt *YC = nullptr;
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001343 if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) {
1344 // Change: and (trunc (and X, YC) to T), C2
1345 // into : and (trunc X to T), trunc(YC) & C2
Craig Topper9d4171a2012-12-20 07:09:41 +00001346 // This will fold the two constants together, which may allow
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001347 // other simplifications.
1348 Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk");
1349 Constant *C3 = ConstantExpr::getTrunc(YC, I.getType());
1350 C3 = ConstantExpr::getAnd(C3, AndRHS);
1351 return BinaryOperator::CreateAnd(NewCast, C3);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001352 }
1353 }
Craig Topper86173602017-04-04 20:26:25 +00001354 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001355
Craig Topper86173602017-04-04 20:26:25 +00001356 if (isa<Constant>(Op1))
Sanjay Pateldb0938f2017-01-10 23:49:07 +00001357 if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
1358 return FoldedLogic;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001359
Sanjay Patel7caaa792017-05-09 20:05:05 +00001360 if (Instruction *DeMorgan = matchDeMorgansLaws(I, *Builder))
Sanjay Patelb54e62f2015-09-08 20:14:13 +00001361 return DeMorgan;
Craig Topper9d4171a2012-12-20 07:09:41 +00001362
Chris Lattner0a8191e2010-01-05 07:50:36 +00001363 {
Sanjay Patele0c26e02017-04-23 22:00:02 +00001364 Value *A = nullptr, *B = nullptr, *C = nullptr;
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001365 // A&(A^B) => A & ~B
1366 {
1367 Value *tmpOp0 = Op0;
1368 Value *tmpOp1 = Op1;
Sanjay Patel7b7eec12016-01-18 18:36:38 +00001369 if (match(Op0, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) {
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001370 if (A == Op1 || B == Op1 ) {
1371 tmpOp1 = Op0;
1372 tmpOp0 = Op1;
1373 // Simplify below
1374 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001375 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001376
Sanjay Patel7b7eec12016-01-18 18:36:38 +00001377 if (match(tmpOp1, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) {
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001378 if (B == tmpOp0) {
1379 std::swap(A, B);
1380 }
Sanjay Pateld09b44a2016-01-18 17:50:23 +00001381 // Notice that the pattern (A&(~B)) is actually (A&(-1^B)), so if
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001382 // A is originally -1 (or a vector of -1 and undefs), then we enter
1383 // an endless loop. By checking that A is non-constant we ensure that
1384 // we will never get to the loop.
1385 if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B
Benjamin Kramer547b6c52011-09-27 20:39:19 +00001386 return BinaryOperator::CreateAnd(A, Builder->CreateNot(B));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001387 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001388 }
1389
1390 // (A&((~A)|B)) -> A&B
Craig Topperc4b48a32017-04-25 06:02:11 +00001391 if (match(Op0, m_c_Or(m_Not(m_Specific(Op1)), m_Value(A))))
Chris Lattner0a8191e2010-01-05 07:50:36 +00001392 return BinaryOperator::CreateAnd(A, Op1);
Craig Topperc4b48a32017-04-25 06:02:11 +00001393 if (match(Op1, m_c_Or(m_Not(m_Specific(Op0)), m_Value(A))))
Chris Lattner0a8191e2010-01-05 07:50:36 +00001394 return BinaryOperator::CreateAnd(A, Op0);
David Majnemer42af3602014-07-30 21:26:37 +00001395
1396 // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
1397 if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
1398 if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
1399 if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
1400 return BinaryOperator::CreateAnd(Op0, Builder->CreateNot(C));
1401
1402 // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
1403 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
1404 if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
1405 if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
1406 return BinaryOperator::CreateAnd(Op1, Builder->CreateNot(C));
Suyog Sarda1c6c2f62014-08-01 04:59:26 +00001407
1408 // (A | B) & ((~A) ^ B) -> (A & B)
Craig Topperba011432017-04-25 15:19:04 +00001409 // (A | B) & (B ^ (~A)) -> (A & B)
1410 // (B | A) & ((~A) ^ B) -> (A & B)
1411 // (B | A) & (B ^ (~A)) -> (A & B)
1412 if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
1413 match(Op0, m_c_Or(m_Specific(A), m_Specific(B))))
Suyog Sarda1c6c2f62014-08-01 04:59:26 +00001414 return BinaryOperator::CreateAnd(A, B);
1415
1416 // ((~A) ^ B) & (A | B) -> (A & B)
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00001417 // ((~A) ^ B) & (B | A) -> (A & B)
Craig Topperba011432017-04-25 15:19:04 +00001418 // (B ^ (~A)) & (A | B) -> (A & B)
1419 // (B ^ (~A)) & (B | A) -> (A & B)
1420 if (match(Op0, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00001421 match(Op1, m_c_Or(m_Specific(A), m_Specific(B))))
Suyog Sarda1c6c2f62014-08-01 04:59:26 +00001422 return BinaryOperator::CreateAnd(A, B);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001423 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001424
David Majnemer5e96f1b2014-08-30 06:18:20 +00001425 {
1426 ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
1427 ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
1428 if (LHS && RHS)
Chris Lattner067459c2010-03-05 08:46:26 +00001429 if (Value *Res = FoldAndOfICmps(LHS, RHS))
Sanjay Patel4b198802016-02-01 22:23:39 +00001430 return replaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00001431
David Majnemer5e96f1b2014-08-30 06:18:20 +00001432 // TODO: Make this recursive; it's a little tricky because an arbitrary
1433 // number of 'and' instructions might have to be created.
1434 Value *X, *Y;
1435 if (LHS && match(Op1, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
1436 if (auto *Cmp = dyn_cast<ICmpInst>(X))
1437 if (Value *Res = FoldAndOfICmps(LHS, Cmp))
Sanjay Patel4b198802016-02-01 22:23:39 +00001438 return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y));
David Majnemer5e96f1b2014-08-30 06:18:20 +00001439 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
1440 if (Value *Res = FoldAndOfICmps(LHS, Cmp))
Sanjay Patel4b198802016-02-01 22:23:39 +00001441 return replaceInstUsesWith(I, Builder->CreateAnd(Res, X));
David Majnemer5e96f1b2014-08-30 06:18:20 +00001442 }
1443 if (RHS && match(Op0, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
1444 if (auto *Cmp = dyn_cast<ICmpInst>(X))
1445 if (Value *Res = FoldAndOfICmps(Cmp, RHS))
Sanjay Patel4b198802016-02-01 22:23:39 +00001446 return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y));
David Majnemer5e96f1b2014-08-30 06:18:20 +00001447 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
1448 if (Value *Res = FoldAndOfICmps(Cmp, RHS))
Sanjay Patel4b198802016-02-01 22:23:39 +00001449 return replaceInstUsesWith(I, Builder->CreateAnd(Res, X));
David Majnemer5e96f1b2014-08-30 06:18:20 +00001450 }
1451 }
1452
Chris Lattner4e8137d2010-02-11 06:26:33 +00001453 // If and'ing two fcmp, try combine them into one.
1454 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
1455 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00001456 if (Value *Res = FoldAndOfFCmps(LHS, RHS))
Sanjay Patel4b198802016-02-01 22:23:39 +00001457 return replaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00001458
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001459 if (Instruction *CastedAnd = foldCastedBitwiseLogic(I))
1460 return CastedAnd;
Craig Topper9d4171a2012-12-20 07:09:41 +00001461
Sanjay Patel74d23ad2016-05-27 21:41:29 +00001462 if (Instruction *Select = foldBoolSextMaskToSelect(I))
1463 return Select;
Nadav Rotem513bd8a2013-01-30 06:35:22 +00001464
Craig Topperf40110f2014-04-25 05:29:35 +00001465 return Changed ? &I : nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001466}
1467
Chad Rosiera00df492016-05-25 16:22:14 +00001468/// Given an OR instruction, check to see if this is a bswap idiom. If so,
1469/// insert the new intrinsic and return it.
1470Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
Chad Rosiere5819e22016-05-26 14:58:51 +00001471 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1472
1473 // Look through zero extends.
1474 if (Instruction *Ext = dyn_cast<ZExtInst>(Op0))
1475 Op0 = Ext->getOperand(0);
1476
1477 if (Instruction *Ext = dyn_cast<ZExtInst>(Op1))
1478 Op1 = Ext->getOperand(0);
1479
1480 // (A | B) | C and A | (B | C) -> bswap if possible.
1481 bool OrOfOrs = match(Op0, m_Or(m_Value(), m_Value())) ||
1482 match(Op1, m_Or(m_Value(), m_Value()));
1483
1484 // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
1485 bool OrOfShifts = match(Op0, m_LogicalShift(m_Value(), m_Value())) &&
1486 match(Op1, m_LogicalShift(m_Value(), m_Value()));
1487
1488 // (A & B) | (C & D) -> bswap if possible.
1489 bool OrOfAnds = match(Op0, m_And(m_Value(), m_Value())) &&
1490 match(Op1, m_And(m_Value(), m_Value()));
1491
1492 if (!OrOfOrs && !OrOfShifts && !OrOfAnds)
1493 return nullptr;
1494
James Molloyf01488e2016-01-15 09:20:19 +00001495 SmallVector<Instruction*, 4> Insts;
Chad Rosiera00df492016-05-25 16:22:14 +00001496 if (!recognizeBSwapOrBitReverseIdiom(&I, true, false, Insts))
Craig Topperf40110f2014-04-25 05:29:35 +00001497 return nullptr;
James Molloyf01488e2016-01-15 09:20:19 +00001498 Instruction *LastInst = Insts.pop_back_val();
1499 LastInst->removeFromParent();
Craig Topper9d4171a2012-12-20 07:09:41 +00001500
James Molloyf01488e2016-01-15 09:20:19 +00001501 for (auto *Inst : Insts)
1502 Worklist.Add(Inst);
1503 return LastInst;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001504}
1505
Sanjay Patelc00e48a2016-07-13 18:07:02 +00001506/// If all elements of two constant vectors are 0/-1 and inverses, return true.
1507static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) {
1508 unsigned NumElts = C1->getType()->getVectorNumElements();
1509 for (unsigned i = 0; i != NumElts; ++i) {
1510 Constant *EltC1 = C1->getAggregateElement(i);
1511 Constant *EltC2 = C2->getAggregateElement(i);
1512 if (!EltC1 || !EltC2)
1513 return false;
1514
1515 // One element must be all ones, and the other must be all zeros.
1516 // FIXME: Allow undef elements.
1517 if (!((match(EltC1, m_Zero()) && match(EltC2, m_AllOnes())) ||
1518 (match(EltC2, m_Zero()) && match(EltC1, m_AllOnes()))))
1519 return false;
1520 }
1521 return true;
1522}
1523
Sanjay Patel7ad98ba2016-06-30 14:18:18 +00001524/// We have an expression of the form (A & C) | (B & D). If A is a scalar or
1525/// vector composed of all-zeros or all-ones values and is the bitwise 'not' of
1526/// B, it can be used as the condition operand of a select instruction.
Sanjay Patelc00e48a2016-07-13 18:07:02 +00001527static Value *getSelectCondition(Value *A, Value *B,
1528 InstCombiner::BuilderTy &Builder) {
Sanjay Patel7ad98ba2016-06-30 14:18:18 +00001529 // If these are scalars or vectors of i1, A can be used directly.
1530 Type *Ty = A->getType();
1531 if (match(A, m_Not(m_Specific(B))) && Ty->getScalarType()->isIntegerTy(1))
1532 return A;
1533
1534 // If A and B are sign-extended, look through the sexts to find the booleans.
1535 Value *Cond;
1536 if (match(A, m_SExt(m_Value(Cond))) &&
1537 Cond->getType()->getScalarType()->isIntegerTy(1) &&
1538 match(B, m_CombineOr(m_Not(m_SExt(m_Specific(Cond))),
1539 m_SExt(m_Not(m_Specific(Cond))))))
1540 return Cond;
1541
Sanjay Patelc00e48a2016-07-13 18:07:02 +00001542 // All scalar (and most vector) possibilities should be handled now.
1543 // Try more matches that only apply to non-splat constant vectors.
1544 if (!Ty->isVectorTy())
1545 return nullptr;
Sanjay Patel7ad98ba2016-06-30 14:18:18 +00001546
Sanjay Patelc00e48a2016-07-13 18:07:02 +00001547 // If both operands are constants, see if the constants are inverse bitmasks.
1548 Constant *AC, *BC;
1549 if (match(A, m_Constant(AC)) && match(B, m_Constant(BC)) &&
1550 areInverseVectorBitmasks(AC, BC))
1551 return ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty));
1552
1553 // If both operands are xor'd with constants using the same sexted boolean
1554 // operand, see if the constants are inverse bitmasks.
1555 if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AC)))) &&
1556 match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BC)))) &&
1557 Cond->getType()->getScalarType()->isIntegerTy(1) &&
1558 areInverseVectorBitmasks(AC, BC)) {
1559 AC = ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty));
1560 return Builder.CreateXor(Cond, AC);
1561 }
Sanjay Patel7ad98ba2016-06-30 14:18:18 +00001562 return nullptr;
1563}
1564
1565/// We have an expression of the form (A & C) | (B & D). Try to simplify this
1566/// to "A' ? C : D", where A' is a boolean or vector of booleans.
Sanjay Patel4e8ebce2016-06-24 18:55:27 +00001567static Value *matchSelectFromAndOr(Value *A, Value *C, Value *B, Value *D,
Sanjay Patel7ad98ba2016-06-30 14:18:18 +00001568 InstCombiner::BuilderTy &Builder) {
Sanjay Patel4e8ebce2016-06-24 18:55:27 +00001569 // The potential condition of the select may be bitcasted. In that case, look
1570 // through its bitcast and the corresponding bitcast of the 'not' condition.
1571 Type *OrigType = A->getType();
1572 Value *SrcA, *SrcB;
Sanjay Patel664514f2016-07-08 21:17:51 +00001573 if (match(A, m_OneUse(m_BitCast(m_Value(SrcA)))) &&
1574 match(B, m_OneUse(m_BitCast(m_Value(SrcB))))) {
Sanjay Patel4e8ebce2016-06-24 18:55:27 +00001575 A = SrcA;
1576 B = SrcB;
Sanjay Patel6cf18af2016-06-03 14:42:07 +00001577 }
1578
Sanjay Patelc00e48a2016-07-13 18:07:02 +00001579 if (Value *Cond = getSelectCondition(A, B, Builder)) {
Sanjay Patel6cf18af2016-06-03 14:42:07 +00001580 // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D))
Sanjay Patel4e8ebce2016-06-24 18:55:27 +00001581 // The bitcasts will either all exist or all not exist. The builder will
1582 // not create unnecessary casts if the types already match.
1583 Value *BitcastC = Builder.CreateBitCast(C, A->getType());
1584 Value *BitcastD = Builder.CreateBitCast(D, A->getType());
1585 Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD);
1586 return Builder.CreateBitCast(Select, OrigType);
Sanjay Patel6cf18af2016-06-03 14:42:07 +00001587 }
Sanjay Patel5c0bc022016-06-02 18:03:05 +00001588
Craig Topperf40110f2014-04-25 05:29:35 +00001589 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001590}
1591
Sanjay Patel18549272015-09-08 18:24:36 +00001592/// Fold (icmp)|(icmp) if possible.
Hal Finkel60db0582014-09-07 18:57:58 +00001593Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
1594 Instruction *CxtI) {
Sanjay Patel519a87a2017-04-05 17:38:34 +00001595 ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001596
Nadav Rotem0ed2fdb2013-11-12 22:38:59 +00001597 // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2)
1598 // if K1 and K2 are a one-bit mask.
Sanjay Patel519a87a2017-04-05 17:38:34 +00001599 ConstantInt *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1));
1600 ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS->getOperand(1));
Nadav Rotem0ed2fdb2013-11-12 22:38:59 +00001601
Sanjay Patel519a87a2017-04-05 17:38:34 +00001602 if (LHS->getPredicate() == ICmpInst::ICMP_EQ && LHSC && LHSC->isZero() &&
1603 RHS->getPredicate() == ICmpInst::ICMP_EQ && RHSC && RHSC->isZero()) {
Nadav Rotem0ed2fdb2013-11-12 22:38:59 +00001604
1605 BinaryOperator *LAnd = dyn_cast<BinaryOperator>(LHS->getOperand(0));
1606 BinaryOperator *RAnd = dyn_cast<BinaryOperator>(RHS->getOperand(0));
1607 if (LAnd && RAnd && LAnd->hasOneUse() && RHS->hasOneUse() &&
1608 LAnd->getOpcode() == Instruction::And &&
1609 RAnd->getOpcode() == Instruction::And) {
1610
Craig Topperf40110f2014-04-25 05:29:35 +00001611 Value *Mask = nullptr;
1612 Value *Masked = nullptr;
Nadav Rotem0ed2fdb2013-11-12 22:38:59 +00001613 if (LAnd->getOperand(0) == RAnd->getOperand(0) &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001614 isKnownToBeAPowerOfTwo(LAnd->getOperand(1), DL, false, 0, &AC, CxtI,
Justin Bogner99798402016-08-05 01:06:44 +00001615 &DT) &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001616 isKnownToBeAPowerOfTwo(RAnd->getOperand(1), DL, false, 0, &AC, CxtI,
Justin Bogner99798402016-08-05 01:06:44 +00001617 &DT)) {
Nadav Rotem0ed2fdb2013-11-12 22:38:59 +00001618 Mask = Builder->CreateOr(LAnd->getOperand(1), RAnd->getOperand(1));
1619 Masked = Builder->CreateAnd(LAnd->getOperand(0), Mask);
1620 } else if (LAnd->getOperand(1) == RAnd->getOperand(1) &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001621 isKnownToBeAPowerOfTwo(LAnd->getOperand(0), DL, false, 0, &AC,
1622 CxtI, &DT) &&
1623 isKnownToBeAPowerOfTwo(RAnd->getOperand(0), DL, false, 0, &AC,
1624 CxtI, &DT)) {
Nadav Rotem0ed2fdb2013-11-12 22:38:59 +00001625 Mask = Builder->CreateOr(LAnd->getOperand(0), RAnd->getOperand(0));
1626 Masked = Builder->CreateAnd(LAnd->getOperand(1), Mask);
1627 }
1628
1629 if (Masked)
1630 return Builder->CreateICmp(ICmpInst::ICMP_NE, Masked, Mask);
1631 }
1632 }
1633
Yi Jiang1a4e73d2014-08-20 22:55:40 +00001634 // Fold (icmp ult/ule (A + C1), C3) | (icmp ult/ule (A + C2), C3)
1635 // --> (icmp ult/ule ((A & ~(C1 ^ C2)) + max(C1, C2)), C3)
1636 // The original condition actually refers to the following two ranges:
1637 // [MAX_UINT-C1+1, MAX_UINT-C1+1+C3] and [MAX_UINT-C2+1, MAX_UINT-C2+1+C3]
1638 // We can fold these two ranges if:
1639 // 1) C1 and C2 is unsigned greater than C3.
1640 // 2) The two ranges are separated.
1641 // 3) C1 ^ C2 is one-bit mask.
1642 // 4) LowRange1 ^ LowRange2 and HighRange1 ^ HighRange2 are one-bit mask.
1643 // This implies all values in the two ranges differ by exactly one bit.
1644
Sanjay Patel519a87a2017-04-05 17:38:34 +00001645 if ((PredL == ICmpInst::ICMP_ULT || PredL == ICmpInst::ICMP_ULE) &&
1646 PredL == PredR && LHSC && RHSC && LHS->hasOneUse() && RHS->hasOneUse() &&
1647 LHSC->getType() == RHSC->getType() &&
1648 LHSC->getValue() == (RHSC->getValue())) {
Yi Jiang1a4e73d2014-08-20 22:55:40 +00001649
1650 Value *LAdd = LHS->getOperand(0);
1651 Value *RAdd = RHS->getOperand(0);
1652
1653 Value *LAddOpnd, *RAddOpnd;
Sanjay Patel519a87a2017-04-05 17:38:34 +00001654 ConstantInt *LAddC, *RAddC;
1655 if (match(LAdd, m_Add(m_Value(LAddOpnd), m_ConstantInt(LAddC))) &&
1656 match(RAdd, m_Add(m_Value(RAddOpnd), m_ConstantInt(RAddC))) &&
1657 LAddC->getValue().ugt(LHSC->getValue()) &&
1658 RAddC->getValue().ugt(LHSC->getValue())) {
Yi Jiang1a4e73d2014-08-20 22:55:40 +00001659
Sanjay Patel519a87a2017-04-05 17:38:34 +00001660 APInt DiffC = LAddC->getValue() ^ RAddC->getValue();
1661 if (LAddOpnd == RAddOpnd && DiffC.isPowerOf2()) {
1662 ConstantInt *MaxAddC = nullptr;
1663 if (LAddC->getValue().ult(RAddC->getValue()))
1664 MaxAddC = RAddC;
Yi Jiang1a4e73d2014-08-20 22:55:40 +00001665 else
Sanjay Patel519a87a2017-04-05 17:38:34 +00001666 MaxAddC = LAddC;
Yi Jiang1a4e73d2014-08-20 22:55:40 +00001667
Sanjay Patel519a87a2017-04-05 17:38:34 +00001668 APInt RRangeLow = -RAddC->getValue();
1669 APInt RRangeHigh = RRangeLow + LHSC->getValue();
1670 APInt LRangeLow = -LAddC->getValue();
1671 APInt LRangeHigh = LRangeLow + LHSC->getValue();
Yi Jiang1a4e73d2014-08-20 22:55:40 +00001672 APInt LowRangeDiff = RRangeLow ^ LRangeLow;
1673 APInt HighRangeDiff = RRangeHigh ^ LRangeHigh;
1674 APInt RangeDiff = LRangeLow.sgt(RRangeLow) ? LRangeLow - RRangeLow
1675 : RRangeLow - LRangeLow;
1676
1677 if (LowRangeDiff.isPowerOf2() && LowRangeDiff == HighRangeDiff &&
Sanjay Patel519a87a2017-04-05 17:38:34 +00001678 RangeDiff.ugt(LHSC->getValue())) {
1679 Value *MaskC = ConstantInt::get(LAddC->getType(), ~DiffC);
Yi Jiang1a4e73d2014-08-20 22:55:40 +00001680
Sanjay Patel519a87a2017-04-05 17:38:34 +00001681 Value *NewAnd = Builder->CreateAnd(LAddOpnd, MaskC);
1682 Value *NewAdd = Builder->CreateAdd(NewAnd, MaxAddC);
1683 return (Builder->CreateICmp(LHS->getPredicate(), NewAdd, LHSC));
Yi Jiang1a4e73d2014-08-20 22:55:40 +00001684 }
1685 }
1686 }
1687 }
1688
Chris Lattner0a8191e2010-01-05 07:50:36 +00001689 // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
Sanjay Patel519a87a2017-04-05 17:38:34 +00001690 if (PredicatesFoldable(PredL, PredR)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001691 if (LHS->getOperand(0) == RHS->getOperand(1) &&
1692 LHS->getOperand(1) == RHS->getOperand(0))
1693 LHS->swapOperands();
1694 if (LHS->getOperand(0) == RHS->getOperand(0) &&
1695 LHS->getOperand(1) == RHS->getOperand(1)) {
1696 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
1697 unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
1698 bool isSigned = LHS->isSigned() || RHS->isSigned();
Pete Cooperebf98c12011-12-17 01:20:32 +00001699 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001700 }
1701 }
Benjamin Kramer2bca3a62010-12-20 16:21:59 +00001702
1703 // handle (roughly):
1704 // (icmp ne (A & B), C) | (icmp ne (A & D), E)
Tim Northoverc0756c42013-09-04 11:57:13 +00001705 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder))
Benjamin Kramer2bca3a62010-12-20 16:21:59 +00001706 return V;
Owen Anderson3fe002d2010-09-08 22:16:17 +00001707
Sanjay Patele4159d22017-04-10 19:38:36 +00001708 Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0);
David Majnemerc2a990b2013-07-05 00:31:17 +00001709 if (LHS->hasOneUse() || RHS->hasOneUse()) {
1710 // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1)
1711 // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1)
Craig Topperf40110f2014-04-25 05:29:35 +00001712 Value *A = nullptr, *B = nullptr;
Sanjay Patel519a87a2017-04-05 17:38:34 +00001713 if (PredL == ICmpInst::ICMP_EQ && LHSC && LHSC->isZero()) {
Sanjay Patele4159d22017-04-10 19:38:36 +00001714 B = LHS0;
1715 if (PredR == ICmpInst::ICMP_ULT && LHS0 == RHS->getOperand(1))
1716 A = RHS0;
1717 else if (PredR == ICmpInst::ICMP_UGT && LHS0 == RHS0)
David Majnemerc2a990b2013-07-05 00:31:17 +00001718 A = RHS->getOperand(1);
1719 }
1720 // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1)
1721 // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1)
Sanjay Patel519a87a2017-04-05 17:38:34 +00001722 else if (PredR == ICmpInst::ICMP_EQ && RHSC && RHSC->isZero()) {
Sanjay Patele4159d22017-04-10 19:38:36 +00001723 B = RHS0;
1724 if (PredL == ICmpInst::ICMP_ULT && RHS0 == LHS->getOperand(1))
1725 A = LHS0;
1726 else if (PredL == ICmpInst::ICMP_UGT && LHS0 == RHS0)
David Majnemerc2a990b2013-07-05 00:31:17 +00001727 A = LHS->getOperand(1);
1728 }
1729 if (A && B)
1730 return Builder->CreateICmp(
1731 ICmpInst::ICMP_UGE,
1732 Builder->CreateAdd(B, ConstantInt::getSigned(B->getType(), -1)), A);
1733 }
1734
Erik Ecksteind1817522014-12-03 10:39:15 +00001735 // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
1736 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/true))
1737 return V;
1738
1739 // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n
1740 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/true))
1741 return V;
Justin Bognerc7e4fbe2016-08-05 01:09:48 +00001742
Sanjay Patelef9f5862017-04-15 17:55:06 +00001743 if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, false, Builder))
1744 return V;
1745
David Majnemerc2a990b2013-07-05 00:31:17 +00001746 // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
Sanjay Patel519a87a2017-04-05 17:38:34 +00001747 if (!LHSC || !RHSC)
1748 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001749
Sanjay Patel519a87a2017-04-05 17:38:34 +00001750 if (LHSC == RHSC && PredL == PredR) {
Owen Anderson8f306a72010-08-02 09:32:13 +00001751 // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
Sanjay Patel519a87a2017-04-05 17:38:34 +00001752 if (PredL == ICmpInst::ICMP_NE && LHSC->isZero()) {
Sanjay Patele4159d22017-04-10 19:38:36 +00001753 Value *NewOr = Builder->CreateOr(LHS0, RHS0);
Sanjay Patel519a87a2017-04-05 17:38:34 +00001754 return Builder->CreateICmp(PredL, NewOr, LHSC);
Owen Anderson8f306a72010-08-02 09:32:13 +00001755 }
Benjamin Kramerda37e152012-01-08 18:32:24 +00001756 }
1757
Benjamin Kramerf7957d02010-12-20 20:00:31 +00001758 // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001759 // iff C2 + CA == C1.
Sanjay Patel519a87a2017-04-05 17:38:34 +00001760 if (PredL == ICmpInst::ICMP_ULT && PredR == ICmpInst::ICMP_EQ) {
1761 ConstantInt *AddC;
Sanjay Patele4159d22017-04-10 19:38:36 +00001762 if (match(LHS0, m_Add(m_Specific(RHS0), m_ConstantInt(AddC))))
Sanjay Patel519a87a2017-04-05 17:38:34 +00001763 if (RHSC->getValue() + AddC->getValue() == LHSC->getValue())
Sanjay Patele4159d22017-04-10 19:38:36 +00001764 return Builder->CreateICmpULE(LHS0, LHSC);
Benjamin Kramer68531ba2010-12-20 16:18:51 +00001765 }
1766
Chris Lattner0a8191e2010-01-05 07:50:36 +00001767 // From here on, we only handle:
1768 // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
Sanjay Patele4159d22017-04-10 19:38:36 +00001769 if (LHS0 != RHS0)
Sanjay Patel519a87a2017-04-05 17:38:34 +00001770 return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +00001771
Sanjay Patel519a87a2017-04-05 17:38:34 +00001772 // ICMP_[US][GL]E X, C is folded to ICMP_[US][GL]T elsewhere.
1773 if (PredL == ICmpInst::ICMP_UGE || PredL == ICmpInst::ICMP_ULE ||
1774 PredR == ICmpInst::ICMP_UGE || PredR == ICmpInst::ICMP_ULE ||
1775 PredL == ICmpInst::ICMP_SGE || PredL == ICmpInst::ICMP_SLE ||
1776 PredR == ICmpInst::ICMP_SGE || PredR == ICmpInst::ICMP_SLE)
Craig Topperf40110f2014-04-25 05:29:35 +00001777 return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +00001778
Chris Lattner0a8191e2010-01-05 07:50:36 +00001779 // We can't fold (ugt x, C) | (sgt x, C2).
Sanjay Patel519a87a2017-04-05 17:38:34 +00001780 if (!PredicatesFoldable(PredL, PredR))
Craig Topperf40110f2014-04-25 05:29:35 +00001781 return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +00001782
Chris Lattner0a8191e2010-01-05 07:50:36 +00001783 // Ensure that the larger constant is on the RHS.
1784 bool ShouldSwap;
Sanjay Patel28611ac2017-04-11 15:57:32 +00001785 if (CmpInst::isSigned(PredL) ||
1786 (ICmpInst::isEquality(PredL) && CmpInst::isSigned(PredR)))
Sanjay Patel570e35c2017-04-10 16:55:57 +00001787 ShouldSwap = LHSC->getValue().sgt(RHSC->getValue());
Sanjay Patel28611ac2017-04-11 15:57:32 +00001788 else
1789 ShouldSwap = LHSC->getValue().ugt(RHSC->getValue());
Craig Topper9d4171a2012-12-20 07:09:41 +00001790
Chris Lattner0a8191e2010-01-05 07:50:36 +00001791 if (ShouldSwap) {
1792 std::swap(LHS, RHS);
Sanjay Patel519a87a2017-04-05 17:38:34 +00001793 std::swap(LHSC, RHSC);
1794 std::swap(PredL, PredR);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001795 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001796
Dan Gohman4a618822010-02-10 16:03:48 +00001797 // At this point, we know we have two icmp instructions
Chris Lattner0a8191e2010-01-05 07:50:36 +00001798 // comparing a value against two constants and or'ing the result
1799 // together. Because of the above check, we know that we only have
1800 // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
1801 // icmp folding check above), that the two constants are not
1802 // equal.
Sanjay Patel519a87a2017-04-05 17:38:34 +00001803 assert(LHSC != RHSC && "Compares not folded above?");
Chris Lattner0a8191e2010-01-05 07:50:36 +00001804
Sanjay Patel519a87a2017-04-05 17:38:34 +00001805 switch (PredL) {
1806 default:
1807 llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +00001808 case ICmpInst::ICMP_EQ:
Sanjay Patel519a87a2017-04-05 17:38:34 +00001809 switch (PredR) {
1810 default:
1811 llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +00001812 case ICmpInst::ICMP_EQ:
Sanjay Patel7cfe4162017-04-14 19:23:50 +00001813 // Potential folds for this case should already be handled.
1814 break;
Sanjay Patel519a87a2017-04-05 17:38:34 +00001815 case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
1816 case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change
Chris Lattner0a8191e2010-01-05 07:50:36 +00001817 break;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001818 }
1819 break;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001820 case ICmpInst::ICMP_ULT:
Sanjay Patel519a87a2017-04-05 17:38:34 +00001821 switch (PredR) {
1822 default:
1823 llvm_unreachable("Unknown integer condition code!");
1824 case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change
Chris Lattner0a8191e2010-01-05 07:50:36 +00001825 break;
Sanjay Patel519a87a2017-04-05 17:38:34 +00001826 case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2
Sanjay Patel599e65b2017-05-07 15:11:40 +00001827 assert(!RHSC->isMaxValue(false) && "Missed icmp simplification");
Sanjay Patele4159d22017-04-10 19:38:36 +00001828 return insertRangeTest(LHS0, LHSC->getValue(), RHSC->getValue() + 1,
1829 false, false);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001830 }
1831 break;
1832 case ICmpInst::ICMP_SLT:
Sanjay Patel519a87a2017-04-05 17:38:34 +00001833 switch (PredR) {
1834 default:
1835 llvm_unreachable("Unknown integer condition code!");
1836 case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change
Chris Lattner0a8191e2010-01-05 07:50:36 +00001837 break;
Sanjay Patel519a87a2017-04-05 17:38:34 +00001838 case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2
Sanjay Patel599e65b2017-05-07 15:11:40 +00001839 assert(!RHSC->isMaxValue(true) && "Missed icmp simplification");
Sanjay Patele4159d22017-04-10 19:38:36 +00001840 return insertRangeTest(LHS0, LHSC->getValue(), RHSC->getValue() + 1, true,
Sanjay Patel519a87a2017-04-05 17:38:34 +00001841 false);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001842 }
1843 break;
1844 }
Craig Topperf40110f2014-04-25 05:29:35 +00001845 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001846}
1847
Sanjay Patel18549272015-09-08 18:24:36 +00001848/// Optimize (fcmp)|(fcmp). NOTE: Unlike the rest of instcombine, this returns
1849/// a Value which should already be inserted into the function.
Chris Lattner067459c2010-03-05 08:46:26 +00001850Value *InstCombiner::FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
Tim Shenaec68b22016-06-29 20:10:17 +00001851 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1852 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1853 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
1854
1855 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1856 // Swap RHS operands to match LHS.
1857 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1858 std::swap(Op1LHS, Op1RHS);
1859 }
1860
1861 // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
1862 // This is a similar transformation to the one in FoldAndOfFCmps.
1863 //
1864 // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1865 // bool(R & CC0) || bool(R & CC1)
1866 // = bool((R & CC0) | (R & CC1))
1867 // = bool(R & (CC0 | CC1)) <= by reversed distribution (contribution? ;)
1868 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
1869 return getFCmpValue(getFCmpCode(Op0CC) | getFCmpCode(Op1CC), Op0LHS, Op0RHS,
1870 Builder);
1871
Chris Lattner0a8191e2010-01-05 07:50:36 +00001872 if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
Craig Topper9d4171a2012-12-20 07:09:41 +00001873 RHS->getPredicate() == FCmpInst::FCMP_UNO &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001874 LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
1875 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1876 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1877 // If either of the constants are nans, then the whole thing returns
1878 // true.
1879 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001880 return Builder->getTrue();
Craig Topper9d4171a2012-12-20 07:09:41 +00001881
Chris Lattner0a8191e2010-01-05 07:50:36 +00001882 // Otherwise, no need to compare the two constants, compare the
1883 // rest.
Chris Lattner067459c2010-03-05 08:46:26 +00001884 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001885 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001886
Chris Lattner0a8191e2010-01-05 07:50:36 +00001887 // Handle vector zeros. This occurs because the canonical form of
1888 // "fcmp uno x,x" is "fcmp uno x, 0".
1889 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1890 isa<ConstantAggregateZero>(RHS->getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00001891 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
Craig Topper9d4171a2012-12-20 07:09:41 +00001892
Craig Topperf40110f2014-04-25 05:29:35 +00001893 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001894 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001895
Craig Topperf40110f2014-04-25 05:29:35 +00001896 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001897}
1898
Sanjay Patel18549272015-09-08 18:24:36 +00001899/// This helper function folds:
Chris Lattner0a8191e2010-01-05 07:50:36 +00001900///
1901/// ((A | B) & C1) | (B & C2)
1902///
1903/// into:
Craig Topper9d4171a2012-12-20 07:09:41 +00001904///
Chris Lattner0a8191e2010-01-05 07:50:36 +00001905/// (A & C1) | B
1906///
1907/// when the XOR of the two constants is "all ones" (-1).
1908Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
1909 Value *A, Value *B, Value *C) {
1910 ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
Craig Topperf40110f2014-04-25 05:29:35 +00001911 if (!CI1) return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001912
Craig Topperf40110f2014-04-25 05:29:35 +00001913 Value *V1 = nullptr;
1914 ConstantInt *CI2 = nullptr;
1915 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001916
1917 APInt Xor = CI1->getValue() ^ CI2->getValue();
Craig Topperf40110f2014-04-25 05:29:35 +00001918 if (!Xor.isAllOnesValue()) return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001919
1920 if (V1 == A || V1 == B) {
1921 Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
1922 return BinaryOperator::CreateOr(NewOp, V1);
1923 }
1924
Craig Topperf40110f2014-04-25 05:29:35 +00001925 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001926}
1927
David Majnemer5d1aeba2014-08-21 05:14:48 +00001928/// \brief This helper function folds:
1929///
1930/// ((A | B) & C1) ^ (B & C2)
1931///
1932/// into:
1933///
1934/// (A & C1) ^ B
1935///
1936/// when the XOR of the two constants is "all ones" (-1).
1937Instruction *InstCombiner::FoldXorWithConstants(BinaryOperator &I, Value *Op,
1938 Value *A, Value *B, Value *C) {
1939 ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
1940 if (!CI1)
1941 return nullptr;
1942
1943 Value *V1 = nullptr;
1944 ConstantInt *CI2 = nullptr;
1945 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2))))
1946 return nullptr;
1947
1948 APInt Xor = CI1->getValue() ^ CI2->getValue();
1949 if (!Xor.isAllOnesValue())
1950 return nullptr;
1951
1952 if (V1 == A || V1 == B) {
1953 Value *NewOp = Builder->CreateAnd(V1 == A ? B : A, CI1);
1954 return BinaryOperator::CreateXor(NewOp, V1);
1955 }
1956
1957 return nullptr;
1958}
1959
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00001960// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
1961// here. We should standardize that construct where it is needed or choose some
1962// other way to ensure that commutated variants of patterns are not missed.
Chris Lattner0a8191e2010-01-05 07:50:36 +00001963Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00001964 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001965 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1966
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001967 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00001968 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001969
Daniel Berlin2c75c632017-04-26 20:56:07 +00001970 if (Value *V = SimplifyOrInst(Op0, Op1, SQ))
Sanjay Patel4b198802016-02-01 22:23:39 +00001971 return replaceInstUsesWith(I, V);
Bill Wendlingaf13d822010-03-03 00:35:56 +00001972
Craig Topper9d4171a2012-12-20 07:09:41 +00001973 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00001974 // purpose is to compute bits we don't care about.
1975 if (SimplifyDemandedInstructionBits(I))
1976 return &I;
1977
Sanjay Patele0c26e02017-04-23 22:00:02 +00001978 // Do this before using distributive laws to catch simple and/or/not patterns.
1979 if (Instruction *Xor = foldOrToXor(I, *Builder))
1980 return Xor;
1981
1982 // (A&B)|(A&C) -> A&(B|C) etc
1983 if (Value *V = SimplifyUsingDistributiveLaws(I))
1984 return replaceInstUsesWith(I, V);
1985
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00001986 if (Value *V = SimplifyBSwap(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00001987 return replaceInstUsesWith(I, V);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00001988
Chris Lattner0a8191e2010-01-05 07:50:36 +00001989 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
Craig Topperf40110f2014-04-25 05:29:35 +00001990 ConstantInt *C1 = nullptr; Value *X = nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001991 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
1992 if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
1993 Op0->hasOneUse()) {
1994 Value *Or = Builder->CreateOr(X, RHS);
1995 Or->takeName(Op0);
1996 return BinaryOperator::CreateXor(Or,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001997 Builder->getInt(C1->getValue() & ~RHS->getValue()));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001998 }
Craig Topper86173602017-04-04 20:26:25 +00001999 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002000
Craig Topper86173602017-04-04 20:26:25 +00002001 if (isa<Constant>(Op1))
Sanjay Pateldb0938f2017-01-10 23:49:07 +00002002 if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
2003 return FoldedLogic;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002004
Chad Rosiere5819e22016-05-26 14:58:51 +00002005 // Given an OR instruction, check to see if this is a bswap.
2006 if (Instruction *BSwap = MatchBSwap(I))
2007 return BSwap;
2008
Craig Topperafa07c52017-04-09 06:12:41 +00002009 {
2010 Value *A;
2011 const APInt *C;
2012 // (X^C)|Y -> (X|Y)^C iff Y&C == 0
2013 if (match(Op0, m_OneUse(m_Xor(m_Value(A), m_APInt(C)))) &&
2014 MaskedValueIsZero(Op1, *C, 0, &I)) {
2015 Value *NOr = Builder->CreateOr(A, Op1);
2016 NOr->takeName(Op0);
2017 return BinaryOperator::CreateXor(NOr,
Davide Italianocdc937d2017-04-17 20:49:50 +00002018 ConstantInt::get(NOr->getType(), *C));
Craig Topperafa07c52017-04-09 06:12:41 +00002019 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002020
Craig Topperafa07c52017-04-09 06:12:41 +00002021 // Y|(X^C) -> (X|Y)^C iff Y&C == 0
2022 if (match(Op1, m_OneUse(m_Xor(m_Value(A), m_APInt(C)))) &&
2023 MaskedValueIsZero(Op0, *C, 0, &I)) {
2024 Value *NOr = Builder->CreateOr(A, Op0);
2025 NOr->takeName(Op0);
2026 return BinaryOperator::CreateXor(NOr,
Davide Italianocdc937d2017-04-17 20:49:50 +00002027 ConstantInt::get(NOr->getType(), *C));
Craig Topperafa07c52017-04-09 06:12:41 +00002028 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002029 }
2030
Craig Topperafa07c52017-04-09 06:12:41 +00002031 Value *A, *B;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002032
Suyog Sardad64faf62014-07-22 18:09:41 +00002033 // ((~A & B) | A) -> (A | B)
Craig Topper72a622c2017-04-07 00:29:47 +00002034 if (match(Op0, m_c_And(m_Not(m_Specific(Op1)), m_Value(A))))
2035 return BinaryOperator::CreateOr(A, Op1);
2036 if (match(Op1, m_c_And(m_Not(m_Specific(Op0)), m_Value(A))))
2037 return BinaryOperator::CreateOr(Op0, A);
Suyog Sardad64faf62014-07-22 18:09:41 +00002038
2039 // ((A & B) | ~A) -> (~A | B)
Craig Topper33e0dbc2017-04-07 07:32:00 +00002040 // The NOT is guaranteed to be in the RHS by complexity ordering.
2041 if (match(Op1, m_Not(m_Value(A))) &&
2042 match(Op0, m_c_And(m_Specific(A), m_Value(B))))
2043 return BinaryOperator::CreateOr(Op1, B);
Suyog Sardad64faf62014-07-22 18:09:41 +00002044
Chris Lattner0a8191e2010-01-05 07:50:36 +00002045 // (A & C)|(B & D)
Craig Topperf40110f2014-04-25 05:29:35 +00002046 Value *C = nullptr, *D = nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002047 if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
2048 match(Op1, m_And(m_Value(B), m_Value(D)))) {
Craig Topperf40110f2014-04-25 05:29:35 +00002049 Value *V1 = nullptr, *V2 = nullptr;
Craig Topperafa07c52017-04-09 06:12:41 +00002050 ConstantInt *C1 = dyn_cast<ConstantInt>(C);
2051 ConstantInt *C2 = dyn_cast<ConstantInt>(D);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002052 if (C1 && C2) { // (A & C1)|(B & C2)
Chris Lattner0a8191e2010-01-05 07:50:36 +00002053 if ((C1->getValue() & C2->getValue()) == 0) {
Chris Lattner95188692010-01-11 06:55:24 +00002054 // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002055 // iff (C1&C2) == 0 and (N&~C1) == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00002056 if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
Hal Finkel60db0582014-09-07 18:57:58 +00002057 ((V1 == B &&
2058 MaskedValueIsZero(V2, ~C1->getValue(), 0, &I)) || // (V|N)
2059 (V2 == B &&
2060 MaskedValueIsZero(V1, ~C1->getValue(), 0, &I)))) // (N|V)
Chris Lattner0a8191e2010-01-05 07:50:36 +00002061 return BinaryOperator::CreateAnd(A,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002062 Builder->getInt(C1->getValue()|C2->getValue()));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002063 // Or commutes, try both ways.
2064 if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
Hal Finkel60db0582014-09-07 18:57:58 +00002065 ((V1 == A &&
2066 MaskedValueIsZero(V2, ~C2->getValue(), 0, &I)) || // (V|N)
2067 (V2 == A &&
2068 MaskedValueIsZero(V1, ~C2->getValue(), 0, &I)))) // (N|V)
Chris Lattner0a8191e2010-01-05 07:50:36 +00002069 return BinaryOperator::CreateAnd(B,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002070 Builder->getInt(C1->getValue()|C2->getValue()));
Craig Topper9d4171a2012-12-20 07:09:41 +00002071
Chris Lattner95188692010-01-11 06:55:24 +00002072 // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002073 // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
Craig Topperf40110f2014-04-25 05:29:35 +00002074 ConstantInt *C3 = nullptr, *C4 = nullptr;
Chris Lattner95188692010-01-11 06:55:24 +00002075 if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
2076 (C3->getValue() & ~C1->getValue()) == 0 &&
2077 match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) &&
2078 (C4->getValue() & ~C2->getValue()) == 0) {
2079 V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield");
2080 return BinaryOperator::CreateAnd(V2,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002081 Builder->getInt(C1->getValue()|C2->getValue()));
Chris Lattner95188692010-01-11 06:55:24 +00002082 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002083 }
2084 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002085
Sanjay Patelf4a08ed2016-07-08 20:53:29 +00002086 // Don't try to form a select if it's unlikely that we'll get rid of at
2087 // least one of the operands. A select is generally more expensive than the
2088 // 'or' that it is replacing.
2089 if (Op0->hasOneUse() || Op1->hasOneUse()) {
2090 // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants.
2091 if (Value *V = matchSelectFromAndOr(A, C, B, D, *Builder))
2092 return replaceInstUsesWith(I, V);
2093 if (Value *V = matchSelectFromAndOr(A, C, D, B, *Builder))
2094 return replaceInstUsesWith(I, V);
2095 if (Value *V = matchSelectFromAndOr(C, A, B, D, *Builder))
2096 return replaceInstUsesWith(I, V);
2097 if (Value *V = matchSelectFromAndOr(C, A, D, B, *Builder))
2098 return replaceInstUsesWith(I, V);
2099 if (Value *V = matchSelectFromAndOr(B, D, A, C, *Builder))
2100 return replaceInstUsesWith(I, V);
2101 if (Value *V = matchSelectFromAndOr(B, D, C, A, *Builder))
2102 return replaceInstUsesWith(I, V);
2103 if (Value *V = matchSelectFromAndOr(D, B, A, C, *Builder))
2104 return replaceInstUsesWith(I, V);
2105 if (Value *V = matchSelectFromAndOr(D, B, C, A, *Builder))
2106 return replaceInstUsesWith(I, V);
2107 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002108
Benjamin Kramer11743242010-07-12 13:34:22 +00002109 // ((A|B)&1)|(B&-2) -> (A&1) | B
2110 if (match(A, m_Or(m_Value(V1), m_Specific(B))) ||
2111 match(A, m_Or(m_Specific(B), m_Value(V1)))) {
2112 Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C);
2113 if (Ret) return Ret;
2114 }
2115 // (B&-2)|((A|B)&1) -> (A&1) | B
2116 if (match(B, m_Or(m_Specific(A), m_Value(V1))) ||
2117 match(B, m_Or(m_Value(V1), m_Specific(A)))) {
2118 Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D);
2119 if (Ret) return Ret;
2120 }
David Majnemer5d1aeba2014-08-21 05:14:48 +00002121 // ((A^B)&1)|(B&-2) -> (A&1) ^ B
2122 if (match(A, m_Xor(m_Value(V1), m_Specific(B))) ||
2123 match(A, m_Xor(m_Specific(B), m_Value(V1)))) {
2124 Instruction *Ret = FoldXorWithConstants(I, Op1, V1, B, C);
2125 if (Ret) return Ret;
2126 }
2127 // (B&-2)|((A^B)&1) -> (A&1) ^ B
2128 if (match(B, m_Xor(m_Specific(A), m_Value(V1))) ||
2129 match(B, m_Xor(m_Value(V1), m_Specific(A)))) {
2130 Instruction *Ret = FoldXorWithConstants(I, Op0, A, V1, D);
2131 if (Ret) return Ret;
2132 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002133 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002134
David Majnemer42af3602014-07-30 21:26:37 +00002135 // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C
2136 if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2137 if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2138 if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
2139 return BinaryOperator::CreateOr(Op0, C);
2140
2141 // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C
2142 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2143 if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2144 if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
2145 return BinaryOperator::CreateOr(Op1, C);
2146
David Majnemerf1eda232014-08-14 06:41:38 +00002147 // ((B | C) & A) | B -> B | (A & C)
2148 if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
2149 return BinaryOperator::CreateOr(Op1, Builder->CreateAnd(A, C));
2150
Sanjay Patel7caaa792017-05-09 20:05:05 +00002151 if (Instruction *DeMorgan = matchDeMorgansLaws(I, *Builder))
Sanjay Patelb54e62f2015-09-08 20:14:13 +00002152 return DeMorgan;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002153
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002154 // Canonicalize xor to the RHS.
Eli Friedmane06535b2012-03-16 00:52:42 +00002155 bool SwappedForXor = false;
2156 if (match(Op0, m_Xor(m_Value(), m_Value()))) {
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002157 std::swap(Op0, Op1);
Eli Friedmane06535b2012-03-16 00:52:42 +00002158 SwappedForXor = true;
2159 }
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002160
2161 // A | ( A ^ B) -> A | B
2162 // A | (~A ^ B) -> A | ~B
Chad Rosier7813dce2012-04-26 23:29:14 +00002163 // (A & B) | (A ^ B)
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002164 if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2165 if (Op0 == A || Op0 == B)
2166 return BinaryOperator::CreateOr(A, B);
2167
Chad Rosier7813dce2012-04-26 23:29:14 +00002168 if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
2169 match(Op0, m_And(m_Specific(B), m_Specific(A))))
2170 return BinaryOperator::CreateOr(A, B);
2171
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002172 if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
2173 Value *Not = Builder->CreateNot(B, B->getName()+".not");
2174 return BinaryOperator::CreateOr(Not, Op0);
2175 }
2176 if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
2177 Value *Not = Builder->CreateNot(A, A->getName()+".not");
2178 return BinaryOperator::CreateOr(Not, Op0);
2179 }
2180 }
2181
2182 // A | ~(A | B) -> A | ~B
2183 // A | ~(A ^ B) -> A | ~B
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002184 if (match(Op1, m_Not(m_Value(A))))
2185 if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
Benjamin Kramer5b7a4e02011-02-20 15:20:01 +00002186 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2187 Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2188 B->getOpcode() == Instruction::Xor)) {
2189 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2190 B->getOperand(0);
2191 Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not");
2192 return BinaryOperator::CreateOr(Not, Op0);
2193 }
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002194
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002195 // (A & B) | (~A ^ B) -> (~A ^ B)
2196 // (A & B) | (B ^ ~A) -> (~A ^ B)
2197 // (B & A) | (~A ^ B) -> (~A ^ B)
2198 // (B & A) | (B ^ ~A) -> (~A ^ B)
2199 // The match order is important: match the xor first because the 'not'
2200 // operation defines 'A'. We do not need to match the xor as Op0 because the
2201 // xor was canonicalized to Op1 above.
2202 if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2203 match(Op0, m_c_And(m_Specific(A), m_Specific(B))))
Suyog Sarda16d64652014-08-01 04:41:43 +00002204 return BinaryOperator::CreateXor(Builder->CreateNot(A), B);
2205
Eli Friedmane06535b2012-03-16 00:52:42 +00002206 if (SwappedForXor)
2207 std::swap(Op0, Op1);
2208
David Majnemer3d6f80b2014-11-28 19:58:29 +00002209 {
2210 ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2211 ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2212 if (LHS && RHS)
Hal Finkel60db0582014-09-07 18:57:58 +00002213 if (Value *Res = FoldOrOfICmps(LHS, RHS, &I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002214 return replaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00002215
David Majnemer3d6f80b2014-11-28 19:58:29 +00002216 // TODO: Make this recursive; it's a little tricky because an arbitrary
2217 // number of 'or' instructions might have to be created.
2218 Value *X, *Y;
2219 if (LHS && match(Op1, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2220 if (auto *Cmp = dyn_cast<ICmpInst>(X))
2221 if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002222 return replaceInstUsesWith(I, Builder->CreateOr(Res, Y));
David Majnemer3d6f80b2014-11-28 19:58:29 +00002223 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2224 if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002225 return replaceInstUsesWith(I, Builder->CreateOr(Res, X));
David Majnemer3d6f80b2014-11-28 19:58:29 +00002226 }
2227 if (RHS && match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2228 if (auto *Cmp = dyn_cast<ICmpInst>(X))
2229 if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002230 return replaceInstUsesWith(I, Builder->CreateOr(Res, Y));
David Majnemer3d6f80b2014-11-28 19:58:29 +00002231 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2232 if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002233 return replaceInstUsesWith(I, Builder->CreateOr(Res, X));
David Majnemer3d6f80b2014-11-28 19:58:29 +00002234 }
2235 }
2236
Chris Lattner4e8137d2010-02-11 06:26:33 +00002237 // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y)
2238 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2239 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00002240 if (Value *Res = FoldOrOfFCmps(LHS, RHS))
Sanjay Patel4b198802016-02-01 22:23:39 +00002241 return replaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00002242
Sanjay Patel75b4ae22016-02-23 23:56:23 +00002243 if (Instruction *CastedOr = foldCastedBitwiseLogic(I))
2244 return CastedOr;
Eli Friedman23956262011-04-14 22:41:27 +00002245
Sanjay Patelcbfca9e2016-07-08 17:01:15 +00002246 // or(sext(A), B) / or(B, sext(A)) --> A ? -1 : B, where A is i1 or <N x i1>.
Sanjay Patel1b6b8242016-07-08 17:26:47 +00002247 if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) &&
Sanjay Patelcbfca9e2016-07-08 17:01:15 +00002248 A->getType()->getScalarType()->isIntegerTy(1))
Eli Friedman23956262011-04-14 22:41:27 +00002249 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1);
Sanjay Patel1b6b8242016-07-08 17:26:47 +00002250 if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) &&
Sanjay Patelcbfca9e2016-07-08 17:01:15 +00002251 A->getType()->getScalarType()->isIntegerTy(1))
Eli Friedman23956262011-04-14 22:41:27 +00002252 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0);
2253
Owen Andersonc237a842010-09-13 17:59:27 +00002254 // Note: If we've gotten to the point of visiting the outer OR, then the
2255 // inner one couldn't be simplified. If it was a constant, then it won't
2256 // be simplified by a later pass either, so we try swapping the inner/outer
2257 // ORs in the hopes that we'll be able to simplify it this way.
2258 // (X|C) | V --> (X|V) | C
Craig Topperafa07c52017-04-09 06:12:41 +00002259 ConstantInt *C1;
Owen Andersonc237a842010-09-13 17:59:27 +00002260 if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) &&
2261 match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) {
2262 Value *Inner = Builder->CreateOr(A, Op1);
2263 Inner->takeName(Op0);
2264 return BinaryOperator::CreateOr(Inner, C1);
2265 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002266
Bill Wendling23242092013-02-16 23:41:36 +00002267 // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2268 // Since this OR statement hasn't been optimized further yet, we hope
2269 // that this transformation will allow the new ORs to be optimized.
2270 {
Craig Topperf40110f2014-04-25 05:29:35 +00002271 Value *X = nullptr, *Y = nullptr;
Bill Wendling23242092013-02-16 23:41:36 +00002272 if (Op0->hasOneUse() && Op1->hasOneUse() &&
2273 match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2274 match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2275 Value *orTrue = Builder->CreateOr(A, C);
2276 Value *orFalse = Builder->CreateOr(B, D);
2277 return SelectInst::Create(X, orTrue, orFalse);
2278 }
2279 }
2280
Craig Topperf40110f2014-04-25 05:29:35 +00002281 return Changed ? &I : nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002282}
2283
Sanjay Pateld13b0bf2017-04-23 16:03:00 +00002284/// A ^ B can be specified using other logic ops in a variety of patterns. We
2285/// can fold these early and efficiently by morphing an existing instruction.
2286static Instruction *foldXorToXor(BinaryOperator &I) {
2287 assert(I.getOpcode() == Instruction::Xor);
2288 Value *Op0 = I.getOperand(0);
2289 Value *Op1 = I.getOperand(1);
2290 Value *A, *B;
2291
2292 // There are 4 commuted variants for each of the basic patterns.
2293
2294 // (A & B) ^ (A | B) -> A ^ B
2295 // (A & B) ^ (B | A) -> A ^ B
2296 // (A | B) ^ (A & B) -> A ^ B
2297 // (A | B) ^ (B & A) -> A ^ B
2298 if ((match(Op0, m_And(m_Value(A), m_Value(B))) &&
2299 match(Op1, m_c_Or(m_Specific(A), m_Specific(B)))) ||
2300 (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
2301 match(Op1, m_c_And(m_Specific(A), m_Specific(B))))) {
2302 I.setOperand(0, A);
2303 I.setOperand(1, B);
2304 return &I;
2305 }
2306
2307 // (A | ~B) ^ (~A | B) -> A ^ B
2308 // (~B | A) ^ (~A | B) -> A ^ B
2309 // (~A | B) ^ (A | ~B) -> A ^ B
2310 // (B | ~A) ^ (A | ~B) -> A ^ B
2311 if ((match(Op0, m_c_Or(m_Value(A), m_Not(m_Value(B)))) &&
2312 match(Op1, m_Or(m_Not(m_Specific(A)), m_Specific(B)))) ||
2313 (match(Op0, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2314 match(Op1, m_Or(m_Specific(A), m_Not(m_Specific(B)))))) {
2315 I.setOperand(0, A);
2316 I.setOperand(1, B);
2317 return &I;
2318 }
2319
2320 // (A & ~B) ^ (~A & B) -> A ^ B
2321 // (~B & A) ^ (~A & B) -> A ^ B
2322 // (~A & B) ^ (A & ~B) -> A ^ B
2323 // (B & ~A) ^ (A & ~B) -> A ^ B
2324 if ((match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2325 match(Op1, m_And(m_Not(m_Specific(A)), m_Specific(B)))) ||
2326 (match(Op0, m_c_And(m_Not(m_Value(A)), m_Value(B))) &&
2327 match(Op1, m_And(m_Specific(A), m_Not(m_Specific(B)))))) {
2328 I.setOperand(0, A);
2329 I.setOperand(1, B);
2330 return &I;
2331 }
2332
2333 return nullptr;
2334}
2335
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002336// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
2337// here. We should standardize that construct where it is needed or choose some
2338// other way to ensure that commutated variants of patterns are not missed.
Chris Lattner0a8191e2010-01-05 07:50:36 +00002339Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00002340 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002341 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2342
Serge Pavlov9ef66a82014-05-11 08:46:12 +00002343 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002344 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +00002345
Daniel Berlin2c75c632017-04-26 20:56:07 +00002346 if (Value *V = SimplifyXorInst(Op0, Op1, SQ))
Sanjay Patel4b198802016-02-01 22:23:39 +00002347 return replaceInstUsesWith(I, V);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002348
Sanjay Pateld13b0bf2017-04-23 16:03:00 +00002349 if (Instruction *NewXor = foldXorToXor(I))
2350 return NewXor;
2351
Duncan Sandsfbb9ac32010-12-22 13:36:08 +00002352 // (A&B)^(A&C) -> A&(B^C) etc
2353 if (Value *V = SimplifyUsingDistributiveLaws(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002354 return replaceInstUsesWith(I, V);
Duncan Sandsadc7771f2010-11-23 14:23:47 +00002355
Craig Topper9d4171a2012-12-20 07:09:41 +00002356 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00002357 // purpose is to compute bits we don't care about.
2358 if (SimplifyDemandedInstructionBits(I))
2359 return &I;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002360
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00002361 if (Value *V = SimplifyBSwap(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002362 return replaceInstUsesWith(I, V);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00002363
Sanjay Patel6381db12017-05-02 15:31:40 +00002364 // Apply DeMorgan's Law for 'nand' / 'nor' logic with an inverted operand.
2365 Value *X, *Y;
2366
2367 // We must eliminate the and/or (one-use) for these transforms to not increase
2368 // the instruction count.
2369 // ~(~X & Y) --> (X | ~Y)
2370 // ~(Y & ~X) --> (X | ~Y)
2371 if (match(&I, m_Not(m_OneUse(m_c_And(m_Not(m_Value(X)), m_Value(Y)))))) {
2372 Value *NotY = Builder->CreateNot(Y, Y->getName() + ".not");
2373 return BinaryOperator::CreateOr(X, NotY);
2374 }
2375 // ~(~X | Y) --> (X & ~Y)
2376 // ~(Y | ~X) --> (X & ~Y)
2377 if (match(&I, m_Not(m_OneUse(m_c_Or(m_Not(m_Value(X)), m_Value(Y)))))) {
2378 Value *NotY = Builder->CreateNot(Y, Y->getName() + ".not");
2379 return BinaryOperator::CreateAnd(X, NotY);
2380 }
2381
Sanjay Patel3b863f82017-04-22 18:05:35 +00002382 // Is this a 'not' (~) fed by a binary operator?
Sanjay Patela1c88142017-05-08 20:49:59 +00002383 BinaryOperator *NotVal;
2384 if (match(&I, m_Not(m_BinOp(NotVal)))) {
2385 if (NotVal->getOpcode() == Instruction::And ||
2386 NotVal->getOpcode() == Instruction::Or) {
Sanjay Patel6381db12017-05-02 15:31:40 +00002387 // Apply DeMorgan's Law when inverts are free:
2388 // ~(X & Y) --> (~X | ~Y)
2389 // ~(X | Y) --> (~X & ~Y)
Sanjay Patela1c88142017-05-08 20:49:59 +00002390 if (IsFreeToInvert(NotVal->getOperand(0),
2391 NotVal->getOperand(0)->hasOneUse()) &&
2392 IsFreeToInvert(NotVal->getOperand(1),
2393 NotVal->getOperand(1)->hasOneUse())) {
2394 Value *NotX = Builder->CreateNot(NotVal->getOperand(0), "notlhs");
2395 Value *NotY = Builder->CreateNot(NotVal->getOperand(1), "notrhs");
2396 if (NotVal->getOpcode() == Instruction::And)
Sanjay Patel3b863f82017-04-22 18:05:35 +00002397 return BinaryOperator::CreateOr(NotX, NotY);
2398 return BinaryOperator::CreateAnd(NotX, NotY);
2399 }
Sanjay Patela1c88142017-05-08 20:49:59 +00002400 }
2401
2402 // ~(~X >>s Y) --> (X >>s Y)
2403 if (match(NotVal, m_AShr(m_Not(m_Value(X)), m_Value(Y))))
2404 return BinaryOperator::CreateAShr(X, Y);
2405
2406 // If we are inverting a right-shifted constant, we may be able to eliminate
2407 // the 'not' by inverting the constant and using the opposite shift type.
2408 // Canonicalization rules ensure that only a negative constant uses 'ashr',
2409 // but we must check that in case that transform has not fired yet.
2410 const APInt *C;
2411 if (match(NotVal, m_AShr(m_APInt(C), m_Value(Y))) && C->isNegative()) {
2412 // ~(C >>s Y) --> ~C >>u Y (when inverting the replicated sign bits)
2413 Constant *NotC = ConstantInt::get(I.getType(), ~(*C));
2414 return BinaryOperator::CreateLShr(NotC, Y);
2415 }
2416
2417 if (match(NotVal, m_LShr(m_APInt(C), m_Value(Y))) && C->isNonNegative()) {
2418 // ~(C >>u Y) --> ~C >>s Y (when inverting the replicated sign bits)
2419 Constant *NotC = ConstantInt::get(I.getType(), ~(*C));
2420 return BinaryOperator::CreateAShr(NotC, Y);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002421 }
2422 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002423
Sanjay Patel33439f92017-04-12 15:11:33 +00002424 // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
2425 ICmpInst::Predicate Pred;
2426 if (match(Op0, m_OneUse(m_Cmp(Pred, m_Value(), m_Value()))) &&
2427 match(Op1, m_AllOnes())) {
2428 cast<CmpInst>(Op0)->setPredicate(CmpInst::getInversePredicate(Pred));
2429 return replaceInstUsesWith(I, Op0);
Benjamin Kramer443c7962015-02-12 20:26:46 +00002430 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002431
Craig Topper9d1821b2017-04-09 06:12:31 +00002432 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002433 // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
2434 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2435 if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
2436 if (CI->hasOneUse() && Op0C->hasOneUse()) {
2437 Instruction::CastOps Opcode = Op0C->getOpcode();
2438 if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
Craig Topper9d1821b2017-04-09 06:12:31 +00002439 (RHSC == ConstantExpr::getCast(Opcode, Builder->getTrue(),
Chris Lattner0a8191e2010-01-05 07:50:36 +00002440 Op0C->getDestTy()))) {
2441 CI->setPredicate(CI->getInversePredicate());
2442 return CastInst::Create(Opcode, CI, Op0C->getType());
2443 }
2444 }
2445 }
2446 }
2447
2448 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2449 // ~(c-X) == X-c-1 == X+(-c-1)
Craig Topper9d1821b2017-04-09 06:12:31 +00002450 if (Op0I->getOpcode() == Instruction::Sub && RHSC->isAllOnesValue())
Chris Lattner0a8191e2010-01-05 07:50:36 +00002451 if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
2452 Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
Craig Topper437c9762017-04-09 06:12:34 +00002453 return BinaryOperator::CreateAdd(Op0I->getOperand(1),
2454 SubOne(NegOp0I0C));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002455 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002456
Chris Lattner0a8191e2010-01-05 07:50:36 +00002457 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2458 if (Op0I->getOpcode() == Instruction::Add) {
2459 // ~(X-c) --> (-c-1)-X
Craig Topper9d1821b2017-04-09 06:12:31 +00002460 if (RHSC->isAllOnesValue()) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002461 Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
Craig Topper437c9762017-04-09 06:12:34 +00002462 return BinaryOperator::CreateSub(SubOne(NegOp0CI),
2463 Op0I->getOperand(0));
Craig Topperbcfd2d12017-04-20 16:56:25 +00002464 } else if (RHSC->getValue().isSignMask()) {
2465 // (X + C) ^ signmask -> (X + C + signmask)
Craig Topper9d1821b2017-04-09 06:12:31 +00002466 Constant *C = Builder->getInt(RHSC->getValue() + Op0CI->getValue());
Chris Lattner0a8191e2010-01-05 07:50:36 +00002467 return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
2468
2469 }
2470 } else if (Op0I->getOpcode() == Instruction::Or) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002471 // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
Hal Finkel60db0582014-09-07 18:57:58 +00002472 if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue(),
2473 0, &I)) {
Craig Topper9d1821b2017-04-09 06:12:31 +00002474 Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHSC);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002475 // Anything in both C1 and C2 is known to be zero, remove it from
2476 // NewRHS.
Craig Topper9d1821b2017-04-09 06:12:31 +00002477 Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHSC);
Craig Topper9d4171a2012-12-20 07:09:41 +00002478 NewRHS = ConstantExpr::getAnd(NewRHS,
Chris Lattner0a8191e2010-01-05 07:50:36 +00002479 ConstantExpr::getNot(CommonBits));
2480 Worklist.Add(Op0I);
2481 I.setOperand(0, Op0I->getOperand(0));
2482 I.setOperand(1, NewRHS);
2483 return &I;
2484 }
Shuxin Yang6ea79e82012-11-26 21:44:25 +00002485 } else if (Op0I->getOpcode() == Instruction::LShr) {
2486 // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
2487 // E1 = "X ^ C1"
Craig Topper9d4171a2012-12-20 07:09:41 +00002488 BinaryOperator *E1;
Shuxin Yang6ea79e82012-11-26 21:44:25 +00002489 ConstantInt *C1;
2490 if (Op0I->hasOneUse() &&
2491 (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) &&
2492 E1->getOpcode() == Instruction::Xor &&
2493 (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) {
2494 // fold (C1 >> C2) ^ C3
Craig Topper9d1821b2017-04-09 06:12:31 +00002495 ConstantInt *C2 = Op0CI, *C3 = RHSC;
Shuxin Yang6ea79e82012-11-26 21:44:25 +00002496 APInt FoldConst = C1->getValue().lshr(C2->getValue());
2497 FoldConst ^= C3->getValue();
2498 // Prepare the two operands.
2499 Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2);
2500 Opnd0->takeName(Op0I);
2501 cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc());
2502 Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst);
2503
2504 return BinaryOperator::CreateXor(Opnd0, FoldVal);
2505 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002506 }
2507 }
2508 }
Craig Topper86173602017-04-04 20:26:25 +00002509 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002510
Craig Topper86173602017-04-04 20:26:25 +00002511 if (isa<Constant>(Op1))
Sanjay Pateldb0938f2017-01-10 23:49:07 +00002512 if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
2513 return FoldedLogic;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002514
Craig Topper76394602017-04-10 06:53:23 +00002515 {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002516 Value *A, *B;
Craig Topper76394602017-04-10 06:53:23 +00002517 if (match(Op1, m_OneUse(m_Or(m_Value(A), m_Value(B))))) {
Craig Topper47383212017-04-10 06:53:21 +00002518 if (A == Op0) { // A^(A|B) == A^(B|A)
Craig Topper76394602017-04-10 06:53:23 +00002519 cast<BinaryOperator>(Op1)->swapOperands();
Craig Topper47383212017-04-10 06:53:21 +00002520 std::swap(A, B);
2521 }
2522 if (B == Op0) { // A^(B|A) == (B|A)^A
Chris Lattner0a8191e2010-01-05 07:50:36 +00002523 I.swapOperands(); // Simplified below.
2524 std::swap(Op0, Op1);
2525 }
Craig Topper76394602017-04-10 06:53:23 +00002526 } else if (match(Op1, m_OneUse(m_And(m_Value(A), m_Value(B))))) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002527 if (A == Op0) { // A^(A&B) -> A^(B&A)
Craig Topper76394602017-04-10 06:53:23 +00002528 cast<BinaryOperator>(Op1)->swapOperands();
Chris Lattner0a8191e2010-01-05 07:50:36 +00002529 std::swap(A, B);
2530 }
2531 if (B == Op0) { // A^(B&A) -> (B&A)^A
2532 I.swapOperands(); // Simplified below.
2533 std::swap(Op0, Op1);
2534 }
2535 }
2536 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002537
Craig Topper76394602017-04-10 06:53:23 +00002538 {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002539 Value *A, *B;
Craig Topper76394602017-04-10 06:53:23 +00002540 if (match(Op0, m_OneUse(m_Or(m_Value(A), m_Value(B))))) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002541 if (A == Op1) // (B|A)^B == (A|B)^B
2542 std::swap(A, B);
2543 if (B == Op1) // (A|B)^B == A & ~B
Benjamin Kramer547b6c52011-09-27 20:39:19 +00002544 return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1));
Craig Topper76394602017-04-10 06:53:23 +00002545 } else if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B))))) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002546 if (A == Op1) // (A&B)^A -> (B&A)^A
2547 std::swap(A, B);
Craig Toppere63c21b2017-04-09 06:12:39 +00002548 const APInt *C;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002549 if (B == Op1 && // (B&A)^A == ~B & A
Craig Toppere63c21b2017-04-09 06:12:39 +00002550 !match(Op1, m_APInt(C))) { // Canonical form is (B&C)^C
Benjamin Kramer547b6c52011-09-27 20:39:19 +00002551 return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002552 }
2553 }
2554 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002555
Craig Topper76394602017-04-10 06:53:23 +00002556 {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002557 Value *A, *B, *C, *D;
David Majnemer6fe6ea72014-09-05 06:09:24 +00002558 // (A ^ C)^(A | B) -> ((~A) & B) ^ C
Craig Topper76394602017-04-10 06:53:23 +00002559 if (match(Op0, m_Xor(m_Value(D), m_Value(C))) &&
2560 match(Op1, m_Or(m_Value(A), m_Value(B)))) {
David Majnemer6fe6ea72014-09-05 06:09:24 +00002561 if (D == A)
2562 return BinaryOperator::CreateXor(
2563 Builder->CreateAnd(Builder->CreateNot(A), B), C);
2564 if (D == B)
2565 return BinaryOperator::CreateXor(
2566 Builder->CreateAnd(Builder->CreateNot(B), A), C);
Karthik Bhata4a4db92014-08-13 05:13:14 +00002567 }
David Majnemer6fe6ea72014-09-05 06:09:24 +00002568 // (A | B)^(A ^ C) -> ((~A) & B) ^ C
Craig Topper76394602017-04-10 06:53:23 +00002569 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
2570 match(Op1, m_Xor(m_Value(D), m_Value(C)))) {
David Majnemer6fe6ea72014-09-05 06:09:24 +00002571 if (D == A)
2572 return BinaryOperator::CreateXor(
2573 Builder->CreateAnd(Builder->CreateNot(A), B), C);
2574 if (D == B)
2575 return BinaryOperator::CreateXor(
2576 Builder->CreateAnd(Builder->CreateNot(B), A), C);
Karthik Bhata4a4db92014-08-13 05:13:14 +00002577 }
Suyog Sardab60ec902014-07-22 18:30:54 +00002578 // (A & B) ^ (A ^ B) -> (A | B)
Craig Topper76394602017-04-10 06:53:23 +00002579 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
Craig Topperd8840d72017-04-10 06:53:28 +00002580 match(Op1, m_c_Xor(m_Specific(A), m_Specific(B))))
Suyog Sardab60ec902014-07-22 18:30:54 +00002581 return BinaryOperator::CreateOr(A, B);
2582 // (A ^ B) ^ (A & B) -> (A | B)
Craig Topper76394602017-04-10 06:53:23 +00002583 if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
Craig Topperd8840d72017-04-10 06:53:28 +00002584 match(Op1, m_c_And(m_Specific(A), m_Specific(B))))
Suyog Sardab60ec902014-07-22 18:30:54 +00002585 return BinaryOperator::CreateOr(A, B);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002586 }
Duncan Sandsadc7771f2010-11-23 14:23:47 +00002587
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002588 // (A & ~B) ^ ~A -> ~(A & B)
2589 // (~B & A) ^ ~A -> ~(A & B)
2590 Value *A, *B;
2591 if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
Suyog Sarda56c9a872014-08-01 05:07:20 +00002592 match(Op1, m_Not(m_Specific(A))))
2593 return BinaryOperator::CreateNot(Builder->CreateAnd(A, B));
2594
David Majnemerb0761a02016-12-21 19:21:59 +00002595 // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
Chris Lattner0a8191e2010-01-05 07:50:36 +00002596 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
David Majnemerb0761a02016-12-21 19:21:59 +00002597 if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
Chris Lattner0a8191e2010-01-05 07:50:36 +00002598 if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
2599 if (LHS->getOperand(0) == RHS->getOperand(1) &&
2600 LHS->getOperand(1) == RHS->getOperand(0))
2601 LHS->swapOperands();
2602 if (LHS->getOperand(0) == RHS->getOperand(0) &&
2603 LHS->getOperand(1) == RHS->getOperand(1)) {
2604 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
2605 unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
2606 bool isSigned = LHS->isSigned() || RHS->isSigned();
Sanjay Patel4b198802016-02-01 22:23:39 +00002607 return replaceInstUsesWith(I,
Pete Cooperebf98c12011-12-17 01:20:32 +00002608 getNewICmpValue(isSigned, Code, Op0, Op1,
2609 Builder));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002610 }
2611 }
2612
Sanjay Pateldbbaca02016-02-24 17:00:34 +00002613 if (Instruction *CastedXor = foldCastedBitwiseLogic(I))
2614 return CastedXor;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002615
Craig Topperf40110f2014-04-25 05:29:35 +00002616 return Changed ? &I : nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002617}