blob: de9c957f81edccdf93bf7819a814d9ad86591a7d [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
Chris Lattner0a8191e2010-01-05 07:50:36 +000026static inline Value *dyn_castNotVal(Value *V) {
27 // If this is not(not(x)) don't return that this is a not: we want the two
28 // not's to be folded first.
29 if (BinaryOperator::isNot(V)) {
30 Value *Operand = BinaryOperator::getNotArgument(V);
Sanjoy Das82ea3d42015-02-24 00:08:41 +000031 if (!IsFreeToInvert(Operand, Operand->hasOneUse()))
Chris Lattner0a8191e2010-01-05 07:50:36 +000032 return Operand;
33 }
Craig Topper9d4171a2012-12-20 07:09:41 +000034
Chris Lattner0a8191e2010-01-05 07:50:36 +000035 // Constants can be considered to be not'ed values...
36 if (ConstantInt *C = dyn_cast<ConstantInt>(V))
37 return ConstantInt::get(C->getType(), ~C->getValue());
Craig Topperf40110f2014-04-25 05:29:35 +000038 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +000039}
40
Sanjay Patel18549272015-09-08 18:24:36 +000041/// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate into
Tim Shenaec68b22016-06-29 20:10:17 +000042/// a four bit mask.
43static unsigned getFCmpCode(FCmpInst::Predicate CC) {
44 assert(FCmpInst::FCMP_FALSE <= CC && CC <= FCmpInst::FCMP_TRUE &&
45 "Unexpected FCmp predicate!");
46 // Take advantage of the bit pattern of FCmpInst::Predicate here.
47 // U L G E
48 static_assert(FCmpInst::FCMP_FALSE == 0, ""); // 0 0 0 0
49 static_assert(FCmpInst::FCMP_OEQ == 1, ""); // 0 0 0 1
50 static_assert(FCmpInst::FCMP_OGT == 2, ""); // 0 0 1 0
51 static_assert(FCmpInst::FCMP_OGE == 3, ""); // 0 0 1 1
52 static_assert(FCmpInst::FCMP_OLT == 4, ""); // 0 1 0 0
53 static_assert(FCmpInst::FCMP_OLE == 5, ""); // 0 1 0 1
54 static_assert(FCmpInst::FCMP_ONE == 6, ""); // 0 1 1 0
55 static_assert(FCmpInst::FCMP_ORD == 7, ""); // 0 1 1 1
56 static_assert(FCmpInst::FCMP_UNO == 8, ""); // 1 0 0 0
57 static_assert(FCmpInst::FCMP_UEQ == 9, ""); // 1 0 0 1
58 static_assert(FCmpInst::FCMP_UGT == 10, ""); // 1 0 1 0
59 static_assert(FCmpInst::FCMP_UGE == 11, ""); // 1 0 1 1
60 static_assert(FCmpInst::FCMP_ULT == 12, ""); // 1 1 0 0
61 static_assert(FCmpInst::FCMP_ULE == 13, ""); // 1 1 0 1
62 static_assert(FCmpInst::FCMP_UNE == 14, ""); // 1 1 1 0
63 static_assert(FCmpInst::FCMP_TRUE == 15, ""); // 1 1 1 1
64 return CC;
Chris Lattner0a8191e2010-01-05 07:50:36 +000065}
66
Sanjay Patel18549272015-09-08 18:24:36 +000067/// This is the complement of getICmpCode, which turns an opcode and two
68/// operands into either a constant true or false, or a brand new ICmp
69/// instruction. The sign is passed in to determine which kind of predicate to
70/// use in the new icmp instruction.
Benjamin Kramerbaba1aa2012-02-06 11:28:19 +000071static Value *getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
72 InstCombiner::BuilderTy *Builder) {
Pete Cooperebf98c12011-12-17 01:20:32 +000073 ICmpInst::Predicate NewPred;
74 if (Value *NewConstant = getICmpValue(Sign, Code, LHS, RHS, NewPred))
75 return NewConstant;
76 return Builder->CreateICmp(NewPred, LHS, RHS);
Chris Lattner0a8191e2010-01-05 07:50:36 +000077}
78
Sanjay Patel18549272015-09-08 18:24:36 +000079/// This is the complement of getFCmpCode, which turns an opcode and two
Tim Shenaec68b22016-06-29 20:10:17 +000080/// operands into either a FCmp instruction, or a true/false constant.
81static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS,
Chris Lattner067459c2010-03-05 08:46:26 +000082 InstCombiner::BuilderTy *Builder) {
Tim Shenaec68b22016-06-29 20:10:17 +000083 const auto Pred = static_cast<FCmpInst::Predicate>(Code);
84 assert(FCmpInst::FCMP_FALSE <= Pred && Pred <= FCmpInst::FCMP_TRUE &&
85 "Unexpected FCmp predicate!");
86 if (Pred == FCmpInst::FCMP_FALSE)
87 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
88 if (Pred == FCmpInst::FCMP_TRUE)
89 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
Chris Lattner067459c2010-03-05 08:46:26 +000090 return Builder->CreateFCmp(Pred, LHS, RHS);
Chris Lattner0a8191e2010-01-05 07:50:36 +000091}
92
Simon Pilgrimbe24ab32014-12-04 09:44:01 +000093/// \brief Transform BITWISE_OP(BSWAP(A),BSWAP(B)) to BSWAP(BITWISE_OP(A, B))
94/// \param I Binary operator to transform.
95/// \return Pointer to node that must replace the original binary operator, or
96/// null pointer if no transformation was made.
97Value *InstCombiner::SimplifyBSwap(BinaryOperator &I) {
98 IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
99
100 // Can't do vectors.
Sanjay Patel1e6ca442016-11-22 22:54:36 +0000101 if (I.getType()->isVectorTy())
102 return nullptr;
Simon Pilgrimbe24ab32014-12-04 09:44:01 +0000103
104 // Can only do bitwise ops.
Sanjay Patel1e6ca442016-11-22 22:54:36 +0000105 if (!I.isBitwiseLogicOp())
Simon Pilgrimbe24ab32014-12-04 09:44:01 +0000106 return nullptr;
107
108 Value *OldLHS = I.getOperand(0);
109 Value *OldRHS = I.getOperand(1);
110 ConstantInt *ConstLHS = dyn_cast<ConstantInt>(OldLHS);
111 ConstantInt *ConstRHS = dyn_cast<ConstantInt>(OldRHS);
112 IntrinsicInst *IntrLHS = dyn_cast<IntrinsicInst>(OldLHS);
113 IntrinsicInst *IntrRHS = dyn_cast<IntrinsicInst>(OldRHS);
114 bool IsBswapLHS = (IntrLHS && IntrLHS->getIntrinsicID() == Intrinsic::bswap);
115 bool IsBswapRHS = (IntrRHS && IntrRHS->getIntrinsicID() == Intrinsic::bswap);
116
117 if (!IsBswapLHS && !IsBswapRHS)
118 return nullptr;
119
120 if (!IsBswapLHS && !ConstLHS)
121 return nullptr;
122
123 if (!IsBswapRHS && !ConstRHS)
124 return nullptr;
125
126 /// OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) )
127 /// OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) )
128 Value *NewLHS = IsBswapLHS ? IntrLHS->getOperand(0) :
129 Builder->getInt(ConstLHS->getValue().byteSwap());
130
131 Value *NewRHS = IsBswapRHS ? IntrRHS->getOperand(0) :
132 Builder->getInt(ConstRHS->getValue().byteSwap());
133
Sanjay Patel1e6ca442016-11-22 22:54:36 +0000134 Value *BinOp = Builder->CreateBinOp(I.getOpcode(), NewLHS, NewRHS);
Sanjay Patelaf674fb2015-12-14 17:24:23 +0000135 Function *F = Intrinsic::getDeclaration(I.getModule(), Intrinsic::bswap, ITy);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +0000136 return Builder->CreateCall(F, BinOp);
137}
138
Sanjay Patel18549272015-09-08 18:24:36 +0000139/// This handles expressions of the form ((val OP C1) & C2). Where
Craig Topper70e4f432017-04-02 17:57:30 +0000140/// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'.
141Instruction *InstCombiner::OptAndOp(BinaryOperator *Op,
Chris Lattner0a8191e2010-01-05 07:50:36 +0000142 ConstantInt *OpRHS,
143 ConstantInt *AndRHS,
144 BinaryOperator &TheAnd) {
145 Value *X = Op->getOperand(0);
Craig Topperf40110f2014-04-25 05:29:35 +0000146 Constant *Together = nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000147 if (!Op->isShift())
148 Together = ConstantExpr::getAnd(AndRHS, OpRHS);
149
150 switch (Op->getOpcode()) {
Craig Topper70e4f432017-04-02 17:57:30 +0000151 default: break;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000152 case Instruction::Xor:
153 if (Op->hasOneUse()) {
154 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
155 Value *And = Builder->CreateAnd(X, AndRHS);
156 And->takeName(Op);
157 return BinaryOperator::CreateXor(And, Together);
158 }
159 break;
160 case Instruction::Or:
Owen Andersonc237a842010-09-13 17:59:27 +0000161 if (Op->hasOneUse()){
162 if (Together != OpRHS) {
163 // (X | C1) & C2 --> (X | (C1&C2)) & C2
164 Value *Or = Builder->CreateOr(X, Together);
165 Or->takeName(Op);
166 return BinaryOperator::CreateAnd(Or, AndRHS);
167 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000168
Owen Andersonc237a842010-09-13 17:59:27 +0000169 ConstantInt *TogetherCI = dyn_cast<ConstantInt>(Together);
170 if (TogetherCI && !TogetherCI->isZero()){
171 // (X | C1) & C2 --> (X & (C2^(C1&C2))) | C1
172 // NOTE: This reduces the number of bits set in the & mask, which
173 // can expose opportunities for store narrowing.
174 Together = ConstantExpr::getXor(AndRHS, Together);
175 Value *And = Builder->CreateAnd(X, Together);
176 And->takeName(Op);
177 return BinaryOperator::CreateOr(And, OpRHS);
178 }
Chris Lattner0a8191e2010-01-05 07:50:36 +0000179 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000180
Chris Lattner0a8191e2010-01-05 07:50:36 +0000181 break;
182 case Instruction::Add:
183 if (Op->hasOneUse()) {
184 // Adding a one to a single bit bit-field should be turned into an XOR
185 // of the bit. First thing to check is to see if this AND is with a
186 // single bit constant.
Jakub Staszak9de494e2013-06-06 00:49:57 +0000187 const APInt &AndRHSV = AndRHS->getValue();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000188
189 // If there is only one bit set.
190 if (AndRHSV.isPowerOf2()) {
191 // Ok, at this point, we know that we are masking the result of the
192 // ADD down to exactly one bit. If the constant we are adding has
193 // no bits set below this bit, then we can eliminate the ADD.
Jakub Staszak9de494e2013-06-06 00:49:57 +0000194 const APInt& AddRHS = OpRHS->getValue();
Chris Lattner0a8191e2010-01-05 07:50:36 +0000195
196 // Check to see if any bits below the one bit set in AndRHSV are set.
197 if ((AddRHS & (AndRHSV-1)) == 0) {
198 // If not, the only thing that can effect the output of the AND is
199 // the bit specified by AndRHSV. If that bit is set, the effect of
200 // the XOR is to toggle the bit. If it is clear, then the ADD has
201 // no effect.
202 if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
203 TheAnd.setOperand(0, X);
204 return &TheAnd;
205 } else {
206 // Pull the XOR out of the AND.
207 Value *NewAnd = Builder->CreateAnd(X, AndRHS);
208 NewAnd->takeName(Op);
209 return BinaryOperator::CreateXor(NewAnd, AndRHS);
210 }
211 }
212 }
213 }
214 break;
215
216 case Instruction::Shl: {
217 // We know that the AND will not produce any of the bits shifted in, so if
218 // the anded constant includes them, clear them now!
219 //
220 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
221 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
222 APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000223 ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShlMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000224
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000225 if (CI->getValue() == ShlMask)
226 // Masking out bits that the shift already masks.
Sanjay Patel4b198802016-02-01 22:23:39 +0000227 return replaceInstUsesWith(TheAnd, Op); // No need for the and.
Craig Topper9d4171a2012-12-20 07:09:41 +0000228
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000229 if (CI != AndRHS) { // Reducing bits set in and.
Chris Lattner0a8191e2010-01-05 07:50:36 +0000230 TheAnd.setOperand(1, CI);
231 return &TheAnd;
232 }
233 break;
234 }
235 case Instruction::LShr: {
236 // We know that the AND will not produce any of the bits shifted in, so if
237 // the anded constant includes them, clear them now! This only applies to
238 // unsigned shifts, because a signed shr may bring in set bits!
239 //
240 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
241 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
242 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000243 ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShrMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000244
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000245 if (CI->getValue() == ShrMask)
246 // Masking out bits that the shift already masks.
Sanjay Patel4b198802016-02-01 22:23:39 +0000247 return replaceInstUsesWith(TheAnd, Op);
Craig Topper9d4171a2012-12-20 07:09:41 +0000248
Chris Lattner9f0ac0d2011-02-15 01:56:08 +0000249 if (CI != AndRHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000250 TheAnd.setOperand(1, CI); // Reduce bits set in and cst.
251 return &TheAnd;
252 }
253 break;
254 }
255 case Instruction::AShr:
256 // Signed shr.
257 // See if this is shifting in some sign extension, then masking it out
258 // with an and.
259 if (Op->hasOneUse()) {
260 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
261 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
262 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000263 Constant *C = Builder->getInt(AndRHS->getValue() & ShrMask);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000264 if (C == AndRHS) { // Masking out bits shifted in.
265 // (Val ashr C1) & C2 -> (Val lshr C1) & C2
266 // Make the argument unsigned.
267 Value *ShVal = Op->getOperand(0);
268 ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName());
269 return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
270 }
271 }
272 break;
273 }
Craig Topperf40110f2014-04-25 05:29:35 +0000274 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000275}
276
Jim Grosbachbdbd7342013-04-05 21:20:12 +0000277/// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000278/// (V < Lo || V >= Hi). This method expects that Lo <= Hi. IsSigned indicates
279/// whether to treat V, Lo, and Hi as signed or not.
Sanjay Patel85d79742016-08-31 19:49:56 +0000280Value *InstCombiner::insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
Chris Lattner067459c2010-03-05 08:46:26 +0000281 bool isSigned, bool Inside) {
Sanjay Patel85d79742016-08-31 19:49:56 +0000282 assert((isSigned ? Lo.sle(Hi) : Lo.ule(Hi)) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +0000283 "Lo is not <= Hi in range emission code!");
Craig Topper9d4171a2012-12-20 07:09:41 +0000284
Sanjay Patel85d79742016-08-31 19:49:56 +0000285 Type *Ty = V->getType();
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000286 if (Lo == Hi)
Sanjay Patel85d79742016-08-31 19:49:56 +0000287 return Inside ? ConstantInt::getFalse(Ty) : ConstantInt::getTrue(Ty);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000288
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000289 // V >= Min && V < Hi --> V < Hi
290 // V < Min || V >= Hi --> V >= Hi
291 ICmpInst::Predicate Pred = Inside ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
Sanjay Patel85d79742016-08-31 19:49:56 +0000292 if (isSigned ? Lo.isMinSignedValue() : Lo.isMinValue()) {
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000293 Pred = isSigned ? ICmpInst::getSignedPredicate(Pred) : Pred;
Sanjay Patel85d79742016-08-31 19:49:56 +0000294 return Builder->CreateICmp(Pred, V, ConstantInt::get(Ty, Hi));
Chris Lattner0a8191e2010-01-05 07:50:36 +0000295 }
296
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000297 // V >= Lo && V < Hi --> V - Lo u< Hi - Lo
298 // V < Lo || V >= Hi --> V - Lo u>= Hi - Lo
Sanjay Patel85d79742016-08-31 19:49:56 +0000299 Value *VMinusLo =
300 Builder->CreateSub(V, ConstantInt::get(Ty, Lo), V->getName() + ".off");
301 Constant *HiMinusLo = ConstantInt::get(Ty, Hi - Lo);
Sanjay Patel7d9ebaf2016-08-31 00:19:35 +0000302 return Builder->CreateICmp(Pred, VMinusLo, HiMinusLo);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000303}
304
Owen Anderson3fe002d2010-09-08 22:16:17 +0000305/// enum for classifying (icmp eq (A & B), C) and (icmp ne (A & B), C)
Craig Topper9d4171a2012-12-20 07:09:41 +0000306/// One of A and B is considered the mask, the other the value. This is
307/// described as the "AMask" or "BMask" part of the enum. If the enum
Owen Anderson3fe002d2010-09-08 22:16:17 +0000308/// contains only "Mask", then both A and B can be considered masks.
309/// If A is the mask, then it was proven, that (A & C) == C. This
310/// is trivial if C == A, or C == 0. If both A and C are constants, this
311/// proof is also easy.
312/// For the following explanations we assume that A is the mask.
Craig Topper9d4171a2012-12-20 07:09:41 +0000313/// The part "AllOnes" declares, that the comparison is true only
Owen Anderson3fe002d2010-09-08 22:16:17 +0000314/// if (A & B) == A, or all bits of A are set in B.
315/// Example: (icmp eq (A & 3), 3) -> FoldMskICmp_AMask_AllOnes
Craig Topper9d4171a2012-12-20 07:09:41 +0000316/// The part "AllZeroes" declares, that the comparison is true only
Owen Anderson3fe002d2010-09-08 22:16:17 +0000317/// if (A & B) == 0, or all bits of A are cleared in B.
318/// Example: (icmp eq (A & 3), 0) -> FoldMskICmp_Mask_AllZeroes
Craig Topper9d4171a2012-12-20 07:09:41 +0000319/// The part "Mixed" declares, that (A & B) == C and C might or might not
Owen Anderson3fe002d2010-09-08 22:16:17 +0000320/// contain any number of one bits and zero bits.
321/// Example: (icmp eq (A & 3), 1) -> FoldMskICmp_AMask_Mixed
322/// The Part "Not" means, that in above descriptions "==" should be replaced
323/// by "!=".
324/// Example: (icmp ne (A & 3), 3) -> FoldMskICmp_AMask_NotAllOnes
325/// If the mask A contains a single bit, then the following is equivalent:
326/// (icmp eq (A & B), A) equals (icmp ne (A & B), 0)
327/// (icmp ne (A & B), A) equals (icmp eq (A & B), 0)
328enum MaskedICmpType {
329 FoldMskICmp_AMask_AllOnes = 1,
330 FoldMskICmp_AMask_NotAllOnes = 2,
331 FoldMskICmp_BMask_AllOnes = 4,
332 FoldMskICmp_BMask_NotAllOnes = 8,
333 FoldMskICmp_Mask_AllZeroes = 16,
334 FoldMskICmp_Mask_NotAllZeroes = 32,
335 FoldMskICmp_AMask_Mixed = 64,
336 FoldMskICmp_AMask_NotMixed = 128,
337 FoldMskICmp_BMask_Mixed = 256,
338 FoldMskICmp_BMask_NotMixed = 512
339};
340
Sanjay Patel18549272015-09-08 18:24:36 +0000341/// Return the set of pattern classes (from MaskedICmpType)
342/// that (icmp SCC (A & B), C) satisfies.
Craig Topper9d4171a2012-12-20 07:09:41 +0000343static unsigned getTypeOfMaskedICmp(Value* A, Value* B, Value* C,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000344 ICmpInst::Predicate SCC)
345{
346 ConstantInt *ACst = dyn_cast<ConstantInt>(A);
347 ConstantInt *BCst = dyn_cast<ConstantInt>(B);
348 ConstantInt *CCst = dyn_cast<ConstantInt>(C);
349 bool icmp_eq = (SCC == ICmpInst::ICMP_EQ);
Craig Topperf40110f2014-04-25 05:29:35 +0000350 bool icmp_abit = (ACst && !ACst->isZero() &&
Owen Anderson3fe002d2010-09-08 22:16:17 +0000351 ACst->getValue().isPowerOf2());
Craig Topperf40110f2014-04-25 05:29:35 +0000352 bool icmp_bbit = (BCst && !BCst->isZero() &&
Owen Anderson3fe002d2010-09-08 22:16:17 +0000353 BCst->getValue().isPowerOf2());
354 unsigned result = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000355 if (CCst && CCst->isZero()) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000356 // if C is zero, then both A and B qualify as mask
357 result |= (icmp_eq ? (FoldMskICmp_Mask_AllZeroes |
Owen Anderson3fe002d2010-09-08 22:16:17 +0000358 FoldMskICmp_AMask_Mixed |
359 FoldMskICmp_BMask_Mixed)
360 : (FoldMskICmp_Mask_NotAllZeroes |
Owen Anderson3fe002d2010-09-08 22:16:17 +0000361 FoldMskICmp_AMask_NotMixed |
362 FoldMskICmp_BMask_NotMixed));
363 if (icmp_abit)
364 result |= (icmp_eq ? (FoldMskICmp_AMask_NotAllOnes |
Craig Topper9d4171a2012-12-20 07:09:41 +0000365 FoldMskICmp_AMask_NotMixed)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000366 : (FoldMskICmp_AMask_AllOnes |
367 FoldMskICmp_AMask_Mixed));
368 if (icmp_bbit)
369 result |= (icmp_eq ? (FoldMskICmp_BMask_NotAllOnes |
Craig Topper9d4171a2012-12-20 07:09:41 +0000370 FoldMskICmp_BMask_NotMixed)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000371 : (FoldMskICmp_BMask_AllOnes |
372 FoldMskICmp_BMask_Mixed));
373 return result;
374 }
375 if (A == C) {
376 result |= (icmp_eq ? (FoldMskICmp_AMask_AllOnes |
377 FoldMskICmp_AMask_Mixed)
378 : (FoldMskICmp_AMask_NotAllOnes |
379 FoldMskICmp_AMask_NotMixed));
380 if (icmp_abit)
381 result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
382 FoldMskICmp_AMask_NotMixed)
383 : (FoldMskICmp_Mask_AllZeroes |
384 FoldMskICmp_AMask_Mixed));
Craig Topperf40110f2014-04-25 05:29:35 +0000385 } else if (ACst && CCst &&
Craig Topperae48cb22012-12-20 07:15:54 +0000386 ConstantExpr::getAnd(ACst, CCst) == CCst) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000387 result |= (icmp_eq ? FoldMskICmp_AMask_Mixed
388 : FoldMskICmp_AMask_NotMixed);
389 }
Craig Topperae48cb22012-12-20 07:15:54 +0000390 if (B == C) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000391 result |= (icmp_eq ? (FoldMskICmp_BMask_AllOnes |
392 FoldMskICmp_BMask_Mixed)
393 : (FoldMskICmp_BMask_NotAllOnes |
394 FoldMskICmp_BMask_NotMixed));
395 if (icmp_bbit)
396 result |= (icmp_eq ? (FoldMskICmp_Mask_NotAllZeroes |
Craig Topper9d4171a2012-12-20 07:09:41 +0000397 FoldMskICmp_BMask_NotMixed)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000398 : (FoldMskICmp_Mask_AllZeroes |
399 FoldMskICmp_BMask_Mixed));
Craig Topperf40110f2014-04-25 05:29:35 +0000400 } else if (BCst && CCst &&
Craig Topperae48cb22012-12-20 07:15:54 +0000401 ConstantExpr::getAnd(BCst, CCst) == CCst) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000402 result |= (icmp_eq ? FoldMskICmp_BMask_Mixed
403 : FoldMskICmp_BMask_NotMixed);
404 }
405 return result;
406}
407
Tim Northoverc0756c42013-09-04 11:57:13 +0000408/// Convert an analysis of a masked ICmp into its equivalent if all boolean
409/// operations had the opposite sense. Since each "NotXXX" flag (recording !=)
410/// is adjacent to the corresponding normal flag (recording ==), this just
411/// involves swapping those bits over.
412static unsigned conjugateICmpMask(unsigned Mask) {
413 unsigned NewMask;
414 NewMask = (Mask & (FoldMskICmp_AMask_AllOnes | FoldMskICmp_BMask_AllOnes |
415 FoldMskICmp_Mask_AllZeroes | FoldMskICmp_AMask_Mixed |
416 FoldMskICmp_BMask_Mixed))
417 << 1;
418
419 NewMask |=
420 (Mask & (FoldMskICmp_AMask_NotAllOnes | FoldMskICmp_BMask_NotAllOnes |
421 FoldMskICmp_Mask_NotAllZeroes | FoldMskICmp_AMask_NotMixed |
422 FoldMskICmp_BMask_NotMixed))
423 >> 1;
424
425 return NewMask;
426}
427
Sanjay Patel18549272015-09-08 18:24:36 +0000428/// Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
429/// Return the set of pattern classes (from MaskedICmpType)
430/// that both LHS and RHS satisfy.
Craig Topper9d4171a2012-12-20 07:09:41 +0000431static unsigned foldLogOpOfMaskedICmpsHelper(Value*& A,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000432 Value*& B, Value*& C,
433 Value*& D, Value*& E,
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000434 ICmpInst *LHS, ICmpInst *RHS,
435 ICmpInst::Predicate &LHSCC,
436 ICmpInst::Predicate &RHSCC) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000437 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType()) return 0;
438 // vectors are not (yet?) supported
439 if (LHS->getOperand(0)->getType()->isVectorTy()) return 0;
440
441 // Here comes the tricky part:
Craig Topper9d4171a2012-12-20 07:09:41 +0000442 // LHS might be of the form L11 & L12 == X, X == L21 & L22,
Owen Anderson3fe002d2010-09-08 22:16:17 +0000443 // and L11 & L12 == L21 & L22. The same goes for RHS.
444 // Now we must find those components L** and R**, that are equal, so
Craig Topper9d4171a2012-12-20 07:09:41 +0000445 // that we can extract the parameters A, B, C, D, and E for the canonical
Owen Anderson3fe002d2010-09-08 22:16:17 +0000446 // above.
447 Value *L1 = LHS->getOperand(0);
448 Value *L2 = LHS->getOperand(1);
449 Value *L11,*L12,*L21,*L22;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000450 // Check whether the icmp can be decomposed into a bit test.
451 if (decomposeBitTestICmp(LHS, LHSCC, L11, L12, L2)) {
Craig Topperf40110f2014-04-25 05:29:35 +0000452 L21 = L22 = L1 = nullptr;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000453 } else {
454 // Look for ANDs in the LHS icmp.
Tim Northoverdc647a22013-09-04 11:57:17 +0000455 if (!L1->getType()->isIntegerTy()) {
456 // You can icmp pointers, for example. They really aren't masks.
Craig Topperf40110f2014-04-25 05:29:35 +0000457 L11 = L12 = nullptr;
Tim Northoverdc647a22013-09-04 11:57:17 +0000458 } else if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) {
459 // Any icmp can be viewed as being trivially masked; if it allows us to
460 // remove one, it's worth it.
461 L11 = L1;
462 L12 = Constant::getAllOnesValue(L1->getType());
463 }
464
465 if (!L2->getType()->isIntegerTy()) {
466 // You can icmp pointers, for example. They really aren't masks.
Craig Topperf40110f2014-04-25 05:29:35 +0000467 L21 = L22 = nullptr;
Tim Northoverdc647a22013-09-04 11:57:17 +0000468 } else if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) {
469 L21 = L2;
470 L22 = Constant::getAllOnesValue(L2->getType());
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000471 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000472 }
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000473
474 // Bail if LHS was a icmp that can't be decomposed into an equality.
475 if (!ICmpInst::isEquality(LHSCC))
476 return 0;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000477
478 Value *R1 = RHS->getOperand(0);
479 Value *R2 = RHS->getOperand(1);
480 Value *R11,*R12;
481 bool ok = false;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000482 if (decomposeBitTestICmp(RHS, RHSCC, R11, R12, R2)) {
483 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
484 A = R11; D = R12;
485 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
486 A = R12; D = R11;
487 } else {
488 return 0;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000489 }
Craig Topperf40110f2014-04-25 05:29:35 +0000490 E = R2; R1 = nullptr; ok = true;
Tim Northoverdc647a22013-09-04 11:57:17 +0000491 } else if (R1->getType()->isIntegerTy()) {
492 if (!match(R1, m_And(m_Value(R11), m_Value(R12)))) {
493 // As before, model no mask as a trivial mask if it'll let us do an
Mayur Pandey75b76c62014-08-19 06:41:55 +0000494 // optimization.
Tim Northoverdc647a22013-09-04 11:57:17 +0000495 R11 = R1;
496 R12 = Constant::getAllOnesValue(R1->getType());
497 }
498
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000499 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
500 A = R11; D = R12; E = R2; ok = true;
501 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000502 A = R12; D = R11; E = R2; ok = true;
503 }
504 }
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000505
506 // Bail if RHS was a icmp that can't be decomposed into an equality.
507 if (!ICmpInst::isEquality(RHSCC))
508 return 0;
509
Chad Rosier58919cc2016-05-09 21:37:43 +0000510 // Look for ANDs on the right side of the RHS icmp.
Tim Northoverdc647a22013-09-04 11:57:17 +0000511 if (!ok && R2->getType()->isIntegerTy()) {
512 if (!match(R2, m_And(m_Value(R11), m_Value(R12)))) {
513 R11 = R2;
514 R12 = Constant::getAllOnesValue(R2->getType());
515 }
516
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000517 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
518 A = R11; D = R12; E = R1; ok = true;
519 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000520 A = R12; D = R11; E = R1; ok = true;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000521 } else {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000522 return 0;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000523 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000524 }
525 if (!ok)
526 return 0;
527
528 if (L11 == A) {
529 B = L12; C = L2;
Craig Topperae48cb22012-12-20 07:15:54 +0000530 } else if (L12 == A) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000531 B = L11; C = L2;
Craig Topperae48cb22012-12-20 07:15:54 +0000532 } else if (L21 == A) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000533 B = L22; C = L1;
Craig Topperae48cb22012-12-20 07:15:54 +0000534 } else if (L22 == A) {
Owen Anderson3fe002d2010-09-08 22:16:17 +0000535 B = L21; C = L1;
536 }
537
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000538 unsigned LeftType = getTypeOfMaskedICmp(A, B, C, LHSCC);
539 unsigned RightType = getTypeOfMaskedICmp(A, D, E, RHSCC);
540 return LeftType & RightType;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000541}
Sanjay Patel18549272015-09-08 18:24:36 +0000542
543/// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
544/// into a single (icmp(A & X) ==/!= Y).
David Majnemer1a3327b2014-11-18 09:31:36 +0000545static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
546 llvm::InstCombiner::BuilderTy *Builder) {
Craig Topperf40110f2014-04-25 05:29:35 +0000547 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000548 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000549 unsigned Mask = foldLogOpOfMaskedICmpsHelper(A, B, C, D, E, LHS, RHS,
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000550 LHSCC, RHSCC);
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000551 if (Mask == 0) return nullptr;
Benjamin Kramerf9d0cc02012-01-09 17:23:27 +0000552 assert(ICmpInst::isEquality(LHSCC) && ICmpInst::isEquality(RHSCC) &&
553 "foldLogOpOfMaskedICmpsHelper must return an equality predicate.");
Owen Anderson3fe002d2010-09-08 22:16:17 +0000554
Tim Northoverc0756c42013-09-04 11:57:13 +0000555 // In full generality:
556 // (icmp (A & B) Op C) | (icmp (A & D) Op E)
557 // == ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ]
558 //
559 // If the latter can be converted into (icmp (A & X) Op Y) then the former is
560 // equivalent to (icmp (A & X) !Op Y).
561 //
562 // Therefore, we can pretend for the rest of this function that we're dealing
563 // with the conjunction, provided we flip the sense of any comparisons (both
564 // input and output).
565
566 // In most cases we're going to produce an EQ for the "&&" case.
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000567 ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
Tim Northoverc0756c42013-09-04 11:57:13 +0000568 if (!IsAnd) {
569 // Convert the masking analysis into its equivalent with negated
570 // comparisons.
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000571 Mask = conjugateICmpMask(Mask);
Tim Northoverc0756c42013-09-04 11:57:13 +0000572 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000573
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000574 if (Mask & FoldMskICmp_Mask_AllZeroes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000575 // (icmp eq (A & B), 0) & (icmp eq (A & D), 0)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000576 // -> (icmp eq (A & (B|D)), 0)
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000577 Value *NewOr = Builder->CreateOr(B, D);
578 Value *NewAnd = Builder->CreateAnd(A, NewOr);
579 // We can't use C as zero because we might actually handle
Craig Topper9d4171a2012-12-20 07:09:41 +0000580 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000581 // with B and D, having a single bit set.
582 Value *Zero = Constant::getNullValue(A->getType());
583 return Builder->CreateICmp(NewCC, NewAnd, Zero);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000584 }
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000585 if (Mask & FoldMskICmp_BMask_AllOnes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000586 // (icmp eq (A & B), B) & (icmp eq (A & D), D)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000587 // -> (icmp eq (A & (B|D)), (B|D))
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000588 Value *NewOr = Builder->CreateOr(B, D);
589 Value *NewAnd = Builder->CreateAnd(A, NewOr);
590 return Builder->CreateICmp(NewCC, NewAnd, NewOr);
Craig Topper9d4171a2012-12-20 07:09:41 +0000591 }
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000592 if (Mask & FoldMskICmp_AMask_AllOnes) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000593 // (icmp eq (A & B), A) & (icmp eq (A & D), A)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000594 // -> (icmp eq (A & (B&D)), A)
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000595 Value *NewAnd1 = Builder->CreateAnd(B, D);
596 Value *NewAnd2 = Builder->CreateAnd(A, NewAnd1);
597 return Builder->CreateICmp(NewCC, NewAnd2, A);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000598 }
Tim Northoverc0756c42013-09-04 11:57:13 +0000599
600 // Remaining cases assume at least that B and D are constant, and depend on
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000601 // their actual values. This isn't strictly necessary, just a "handle the
Tim Northoverc0756c42013-09-04 11:57:13 +0000602 // easy cases for now" decision.
603 ConstantInt *BCst = dyn_cast<ConstantInt>(B);
Craig Topperf40110f2014-04-25 05:29:35 +0000604 if (!BCst) return nullptr;
Tim Northoverc0756c42013-09-04 11:57:13 +0000605 ConstantInt *DCst = dyn_cast<ConstantInt>(D);
Craig Topperf40110f2014-04-25 05:29:35 +0000606 if (!DCst) return nullptr;
Tim Northoverc0756c42013-09-04 11:57:13 +0000607
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000608 if (Mask & (FoldMskICmp_Mask_NotAllZeroes | FoldMskICmp_BMask_NotAllOnes)) {
Tim Northoverc0756c42013-09-04 11:57:13 +0000609 // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and
610 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
611 // -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0)
612 // Only valid if one of the masks is a superset of the other (check "B&D" is
613 // the same as either B or D).
614 APInt NewMask = BCst->getValue() & DCst->getValue();
615
616 if (NewMask == BCst->getValue())
617 return LHS;
618 else if (NewMask == DCst->getValue())
619 return RHS;
620 }
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000621 if (Mask & FoldMskICmp_AMask_NotAllOnes) {
Tim Northoverc0756c42013-09-04 11:57:13 +0000622 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
623 // -> (icmp ne (A & B), A) or (icmp ne (A & D), A)
624 // Only valid if one of the masks is a superset of the other (check "B|D" is
625 // the same as either B or D).
626 APInt NewMask = BCst->getValue() | DCst->getValue();
627
628 if (NewMask == BCst->getValue())
629 return LHS;
630 else if (NewMask == DCst->getValue())
631 return RHS;
632 }
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000633 if (Mask & FoldMskICmp_BMask_Mixed) {
Craig Topper9d4171a2012-12-20 07:09:41 +0000634 // (icmp eq (A & B), C) & (icmp eq (A & D), E)
Owen Anderson3fe002d2010-09-08 22:16:17 +0000635 // We already know that B & C == C && D & E == E.
636 // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of
637 // C and E, which are shared by both the mask B and the mask D, don't
638 // contradict, then we can transform to
639 // -> (icmp eq (A & (B|D)), (C|E))
640 // Currently, we only handle the case of B, C, D, and E being constant.
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000641 // We can't simply use C and E because we might actually handle
Craig Topper9d4171a2012-12-20 07:09:41 +0000642 // (icmp ne (A & B), B) & (icmp eq (A & D), D)
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000643 // with B and D, having a single bit set.
Owen Anderson3fe002d2010-09-08 22:16:17 +0000644 ConstantInt *CCst = dyn_cast<ConstantInt>(C);
Craig Topperf40110f2014-04-25 05:29:35 +0000645 if (!CCst) return nullptr;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000646 ConstantInt *ECst = dyn_cast<ConstantInt>(E);
Craig Topperf40110f2014-04-25 05:29:35 +0000647 if (!ECst) return nullptr;
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000648 if (LHSCC != NewCC)
David Majnemer1a3327b2014-11-18 09:31:36 +0000649 CCst = cast<ConstantInt>(ConstantExpr::getXor(BCst, CCst));
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000650 if (RHSCC != NewCC)
David Majnemer1a3327b2014-11-18 09:31:36 +0000651 ECst = cast<ConstantInt>(ConstantExpr::getXor(DCst, ECst));
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000652 // If there is a conflict, we should actually return a false for the
653 // whole construct.
David Majnemer1a3327b2014-11-18 09:31:36 +0000654 if (((BCst->getValue() & DCst->getValue()) &
655 (CCst->getValue() ^ ECst->getValue())) != 0)
David Majnemer6fdb6b82014-11-18 09:31:41 +0000656 return ConstantInt::get(LHS->getType(), !IsAnd);
Sanjay Patel3b8dcc72016-01-18 18:28:09 +0000657 Value *NewOr1 = Builder->CreateOr(B, D);
658 Value *NewOr2 = ConstantExpr::getOr(CCst, ECst);
659 Value *NewAnd = Builder->CreateAnd(A, NewOr1);
660 return Builder->CreateICmp(NewCC, NewAnd, NewOr2);
Owen Anderson3fe002d2010-09-08 22:16:17 +0000661 }
Craig Topperf40110f2014-04-25 05:29:35 +0000662 return nullptr;
Owen Anderson3fe002d2010-09-08 22:16:17 +0000663}
664
Erik Ecksteind1817522014-12-03 10:39:15 +0000665/// Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
666/// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
667/// If \p Inverted is true then the check is for the inverted range, e.g.
668/// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
669Value *InstCombiner::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1,
670 bool Inverted) {
671 // Check the lower range comparison, e.g. x >= 0
672 // InstCombine already ensured that if there is a constant it's on the RHS.
673 ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1));
674 if (!RangeStart)
675 return nullptr;
676
677 ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() :
678 Cmp0->getPredicate());
679
680 // Accept x > -1 or x >= 0 (after potentially inverting the predicate).
681 if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) ||
682 (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero())))
683 return nullptr;
684
685 ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() :
686 Cmp1->getPredicate());
687
688 Value *Input = Cmp0->getOperand(0);
689 Value *RangeEnd;
690 if (Cmp1->getOperand(0) == Input) {
691 // For the upper range compare we have: icmp x, n
692 RangeEnd = Cmp1->getOperand(1);
693 } else if (Cmp1->getOperand(1) == Input) {
694 // For the upper range compare we have: icmp n, x
695 RangeEnd = Cmp1->getOperand(0);
696 Pred1 = ICmpInst::getSwappedPredicate(Pred1);
697 } else {
698 return nullptr;
699 }
700
701 // Check the upper range comparison, e.g. x < n
702 ICmpInst::Predicate NewPred;
703 switch (Pred1) {
704 case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break;
705 case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break;
706 default: return nullptr;
707 }
708
709 // This simplification is only valid if the upper range is not negative.
710 bool IsNegative, IsNotNegative;
David Majnemer54c2ca22014-12-26 09:10:14 +0000711 ComputeSignBit(RangeEnd, IsNotNegative, IsNegative, /*Depth=*/0, Cmp1);
Erik Ecksteind1817522014-12-03 10:39:15 +0000712 if (!IsNotNegative)
713 return nullptr;
714
715 if (Inverted)
716 NewPred = ICmpInst::getInversePredicate(NewPred);
717
718 return Builder->CreateICmp(NewPred, Input, RangeEnd);
719}
720
Sanjay Patel18549272015-09-08 18:24:36 +0000721/// Fold (icmp)&(icmp) if possible.
Chris Lattner067459c2010-03-05 08:46:26 +0000722Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000723 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
724
725 // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
726 if (PredicatesFoldable(LHSCC, RHSCC)) {
727 if (LHS->getOperand(0) == RHS->getOperand(1) &&
728 LHS->getOperand(1) == RHS->getOperand(0))
729 LHS->swapOperands();
730 if (LHS->getOperand(0) == RHS->getOperand(0) &&
731 LHS->getOperand(1) == RHS->getOperand(1)) {
732 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
733 unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
734 bool isSigned = LHS->isSigned() || RHS->isSigned();
Pete Cooperebf98c12011-12-17 01:20:32 +0000735 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000736 }
737 }
Owen Anderson3fe002d2010-09-08 22:16:17 +0000738
Chris Lattnerdcef03f2011-02-10 05:17:27 +0000739 // handle (roughly): (icmp eq (A & B), C) & (icmp eq (A & D), E)
Tim Northoverc0756c42013-09-04 11:57:13 +0000740 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder))
Chris Lattnerdcef03f2011-02-10 05:17:27 +0000741 return V;
Craig Topper9d4171a2012-12-20 07:09:41 +0000742
Erik Ecksteind1817522014-12-03 10:39:15 +0000743 // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
744 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/false))
745 return V;
746
747 // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n
748 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/false))
749 return V;
750
Chris Lattner0a8191e2010-01-05 07:50:36 +0000751 // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
752 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
753 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
754 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
Craig Topperf40110f2014-04-25 05:29:35 +0000755 if (!LHSCst || !RHSCst) return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +0000756
Chris Lattner0a8191e2010-01-05 07:50:36 +0000757 if (LHSCst == RHSCst && LHSCC == RHSCC) {
758 // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C)
Sanjay Patelc2ceb8b2016-01-18 19:17:58 +0000759 // where C is a power of 2 or
Chris Lattner0a8191e2010-01-05 07:50:36 +0000760 // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
Sanjay Patelc2ceb8b2016-01-18 19:17:58 +0000761 if ((LHSCC == ICmpInst::ICMP_ULT && LHSCst->getValue().isPowerOf2()) ||
762 (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero())) {
Chris Lattner0a8191e2010-01-05 07:50:36 +0000763 Value *NewOr = Builder->CreateOr(Val, Val2);
Chris Lattner067459c2010-03-05 08:46:26 +0000764 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000765 }
766 }
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000767
Benjamin Kramer101720f2011-04-28 20:09:57 +0000768 // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000769 // where CMAX is the all ones value for the truncated type,
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000770 // iff the lower bits of C2 and CA are zero.
Bill Wendlingf2c78f32012-02-29 01:46:50 +0000771 if (LHSCC == ICmpInst::ICMP_EQ && LHSCC == RHSCC &&
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000772 LHS->hasOneUse() && RHS->hasOneUse()) {
773 Value *V;
Craig Topperf40110f2014-04-25 05:29:35 +0000774 ConstantInt *AndCst, *SmallCst = nullptr, *BigCst = nullptr;
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000775
776 // (trunc x) == C1 & (and x, CA) == C2
Craig Topperae48cb22012-12-20 07:15:54 +0000777 // (and x, CA) == C2 & (trunc x) == C1
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000778 if (match(Val2, m_Trunc(m_Value(V))) &&
779 match(Val, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
780 SmallCst = RHSCst;
781 BigCst = LHSCst;
Craig Topperae48cb22012-12-20 07:15:54 +0000782 } else if (match(Val, m_Trunc(m_Value(V))) &&
783 match(Val2, m_And(m_Specific(V), m_ConstantInt(AndCst)))) {
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000784 SmallCst = LHSCst;
785 BigCst = RHSCst;
786 }
787
788 if (SmallCst && BigCst) {
789 unsigned BigBitSize = BigCst->getType()->getBitWidth();
790 unsigned SmallBitSize = SmallCst->getType()->getBitWidth();
791
792 // Check that the low bits are zero.
793 APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
Benjamin Kramercf9d1ad2011-04-28 21:38:51 +0000794 if ((Low & AndCst->getValue()) == 0 && (Low & BigCst->getValue()) == 0) {
Benjamin Kramer4145c0d2011-04-28 16:58:40 +0000795 Value *NewAnd = Builder->CreateAnd(V, Low | AndCst->getValue());
796 APInt N = SmallCst->getValue().zext(BigBitSize) | BigCst->getValue();
797 Value *NewVal = ConstantInt::get(AndCst->getType()->getContext(), N);
798 return Builder->CreateICmp(LHSCC, NewAnd, NewVal);
799 }
800 }
801 }
Benjamin Kramerda37e152012-01-08 18:32:24 +0000802
Chris Lattner0a8191e2010-01-05 07:50:36 +0000803 // From here on, we only handle:
804 // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler.
Craig Topperf40110f2014-04-25 05:29:35 +0000805 if (Val != Val2) return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +0000806
Chris Lattner0a8191e2010-01-05 07:50:36 +0000807 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
808 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
809 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
810 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
811 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
Craig Topperf40110f2014-04-25 05:29:35 +0000812 return nullptr;
Anders Carlssonda80afe2011-03-01 15:05:01 +0000813
Chris Lattner0a8191e2010-01-05 07:50:36 +0000814 // We can't fold (ugt x, C) & (sgt x, C2).
815 if (!PredicatesFoldable(LHSCC, RHSCC))
Craig Topperf40110f2014-04-25 05:29:35 +0000816 return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +0000817
Chris Lattner0a8191e2010-01-05 07:50:36 +0000818 // Ensure that the larger constant is on the RHS.
819 bool ShouldSwap;
820 if (CmpInst::isSigned(LHSCC) ||
Craig Topper9d4171a2012-12-20 07:09:41 +0000821 (ICmpInst::isEquality(LHSCC) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +0000822 CmpInst::isSigned(RHSCC)))
823 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
824 else
825 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
Craig Topper9d4171a2012-12-20 07:09:41 +0000826
Chris Lattner0a8191e2010-01-05 07:50:36 +0000827 if (ShouldSwap) {
828 std::swap(LHS, RHS);
829 std::swap(LHSCst, RHSCst);
830 std::swap(LHSCC, RHSCC);
831 }
832
Dan Gohman4a618822010-02-10 16:03:48 +0000833 // At this point, we know we have two icmp instructions
Chris Lattner0a8191e2010-01-05 07:50:36 +0000834 // comparing a value against two constants and and'ing the result
835 // together. Because of the above check, we know that we only have
Craig Topper9d4171a2012-12-20 07:09:41 +0000836 // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know
837 // (from the icmp folding check above), that the two constants
Chris Lattner0a8191e2010-01-05 07:50:36 +0000838 // are not equal and that the larger constant is on the RHS
839 assert(LHSCst != RHSCst && "Compares not folded above?");
840
841 switch (LHSCC) {
842 default: llvm_unreachable("Unknown integer condition code!");
843 case ICmpInst::ICMP_EQ:
844 switch (RHSCC) {
845 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000846 case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13
847 case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13
848 case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13
Chris Lattner067459c2010-03-05 08:46:26 +0000849 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000850 }
851 case ICmpInst::ICMP_NE:
852 switch (RHSCC) {
853 default: llvm_unreachable("Unknown integer condition code!");
854 case ICmpInst::ICMP_ULT:
855 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X u< 14) -> X < 13
Chris Lattner067459c2010-03-05 08:46:26 +0000856 return Builder->CreateICmpULT(Val, LHSCst);
Benjamin Kramer240b85e2014-10-12 14:02:34 +0000857 if (LHSCst->isNullValue()) // (X != 0 & X u< 14) -> X-1 u< 13
Sanjay Patel85d79742016-08-31 19:49:56 +0000858 return insertRangeTest(Val, LHSCst->getValue() + 1, RHSCst->getValue(),
859 false, true);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000860 break; // (X != 13 & X u< 15) -> no change
861 case ICmpInst::ICMP_SLT:
862 if (LHSCst == SubOne(RHSCst)) // (X != 13 & X s< 14) -> X < 13
Chris Lattner067459c2010-03-05 08:46:26 +0000863 return Builder->CreateICmpSLT(Val, LHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000864 break; // (X != 13 & X s< 15) -> no change
865 case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15
866 case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15
867 case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
Chris Lattner067459c2010-03-05 08:46:26 +0000868 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000869 case ICmpInst::ICMP_NE:
Jim Grosbach20e3b9a2013-08-16 00:15:20 +0000870 // Special case to get the ordering right when the values wrap around
871 // zero.
Jim Grosbachd0de8ac2013-08-16 17:03:36 +0000872 if (LHSCst->getValue() == 0 && RHSCst->getValue().isAllOnesValue())
Jim Grosbach20e3b9a2013-08-16 00:15:20 +0000873 std::swap(LHSCst, RHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000874 if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
875 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
876 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
Jim Grosbach20e3b9a2013-08-16 00:15:20 +0000877 return Builder->CreateICmpUGT(Add, ConstantInt::get(Add->getType(), 1),
878 Val->getName()+".cmp");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000879 }
880 break; // (X != 13 & X != 15) -> no change
881 }
882 break;
883 case ICmpInst::ICMP_ULT:
884 switch (RHSCC) {
885 default: llvm_unreachable("Unknown integer condition code!");
886 case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false
887 case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false
Chris Lattner067459c2010-03-05 08:46:26 +0000888 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000889 case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change
890 break;
891 case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13
892 case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13
Chris Lattner067459c2010-03-05 08:46:26 +0000893 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000894 case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change
895 break;
896 }
897 break;
898 case ICmpInst::ICMP_SLT:
899 switch (RHSCC) {
900 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a8191e2010-01-05 07:50:36 +0000901 case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change
902 break;
903 case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13
904 case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13
Chris Lattner067459c2010-03-05 08:46:26 +0000905 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000906 case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change
907 break;
908 }
909 break;
910 case ICmpInst::ICMP_UGT:
911 switch (RHSCC) {
912 default: llvm_unreachable("Unknown integer condition code!");
913 case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15
914 case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15
Chris Lattner067459c2010-03-05 08:46:26 +0000915 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000916 case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change
917 break;
918 case ICmpInst::ICMP_NE:
919 if (RHSCst == AddOne(LHSCst)) // (X u> 13 & X != 14) -> X u> 14
Chris Lattner067459c2010-03-05 08:46:26 +0000920 return Builder->CreateICmp(LHSCC, Val, RHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +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 Patel85d79742016-08-31 19:49:56 +0000923 return insertRangeTest(Val, LHSCst->getValue() + 1, RHSCst->getValue(),
924 false, true);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000925 case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change
926 break;
927 }
928 break;
929 case ICmpInst::ICMP_SGT:
930 switch (RHSCC) {
931 default: llvm_unreachable("Unknown integer condition code!");
932 case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15
933 case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15
Chris Lattner067459c2010-03-05 08:46:26 +0000934 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000935 case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
936 break;
937 case ICmpInst::ICMP_NE:
938 if (RHSCst == AddOne(LHSCst)) // (X s> 13 & X != 14) -> X s> 14
Chris Lattner067459c2010-03-05 08:46:26 +0000939 return Builder->CreateICmp(LHSCC, Val, RHSCst);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000940 break; // (X s> 13 & X != 15) -> no change
941 case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1
Sanjay Patel85d79742016-08-31 19:49:56 +0000942 return insertRangeTest(Val, LHSCst->getValue() + 1, RHSCst->getValue(),
943 true, true);
Chris Lattner0a8191e2010-01-05 07:50:36 +0000944 case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change
945 break;
946 }
947 break;
948 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000949
Craig Topperf40110f2014-04-25 05:29:35 +0000950 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +0000951}
952
Sanjay Patel18549272015-09-08 18:24:36 +0000953/// Optimize (fcmp)&(fcmp). NOTE: Unlike the rest of instcombine, this returns
954/// a Value which should already be inserted into the function.
Chris Lattner067459c2010-03-05 08:46:26 +0000955Value *InstCombiner::FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
Tim Shenaec68b22016-06-29 20:10:17 +0000956 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
957 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
958 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
959
960 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
961 // Swap RHS operands to match LHS.
962 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
963 std::swap(Op1LHS, Op1RHS);
964 }
965
966 // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
967 // Suppose the relation between x and y is R, where R is one of
968 // U(1000), L(0100), G(0010) or E(0001), and CC0 and CC1 are the bitmasks for
969 // testing the desired relations.
970 //
971 // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
972 // bool(R & CC0) && bool(R & CC1)
973 // = bool((R & CC0) & (R & CC1))
974 // = bool(R & (CC0 & CC1)) <= by re-association, commutation, and idempotency
975 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
976 return getFCmpValue(getFCmpCode(Op0CC) & getFCmpCode(Op1CC), Op0LHS, Op0RHS,
977 Builder);
978
Chris Lattner0a8191e2010-01-05 07:50:36 +0000979 if (LHS->getPredicate() == FCmpInst::FCMP_ORD &&
980 RHS->getPredicate() == FCmpInst::FCMP_ORD) {
Benjamin Kramere89c7052013-04-12 21:56:23 +0000981 if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType())
Craig Topperf40110f2014-04-25 05:29:35 +0000982 return nullptr;
Benjamin Kramere89c7052013-04-12 21:56:23 +0000983
Chris Lattner0a8191e2010-01-05 07:50:36 +0000984 // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y)
985 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
986 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
987 // If either of the constants are nans, then the whole thing returns
988 // false.
989 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
Jakub Staszak461d1fe2013-06-06 00:37:23 +0000990 return Builder->getFalse();
Chris Lattner067459c2010-03-05 08:46:26 +0000991 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
Chris Lattner0a8191e2010-01-05 07:50:36 +0000992 }
Craig Topper9d4171a2012-12-20 07:09:41 +0000993
Chris Lattner0a8191e2010-01-05 07:50:36 +0000994 // Handle vector zeros. This occurs because the canonical form of
995 // "fcmp ord x,x" is "fcmp ord x, 0".
996 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
997 isa<ConstantAggregateZero>(RHS->getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +0000998 return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0));
Craig Topperf40110f2014-04-25 05:29:35 +0000999 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001000 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001001
Craig Topperf40110f2014-04-25 05:29:35 +00001002 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001003}
1004
Sanjay Patelb54e62f2015-09-08 20:14:13 +00001005/// Match De Morgan's Laws:
1006/// (~A & ~B) == (~(A | B))
1007/// (~A | ~B) == (~(A & B))
1008static Instruction *matchDeMorgansLaws(BinaryOperator &I,
1009 InstCombiner::BuilderTy *Builder) {
1010 auto Opcode = I.getOpcode();
1011 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1012 "Trying to match De Morgan's Laws with something other than and/or");
Sanjay Patele1b09ca2015-09-25 23:21:38 +00001013 // Flip the logic operation.
1014 if (Opcode == Instruction::And)
1015 Opcode = Instruction::Or;
1016 else
1017 Opcode = Instruction::And;
Sanjay Patelb54e62f2015-09-08 20:14:13 +00001018
1019 Value *Op0 = I.getOperand(0);
1020 Value *Op1 = I.getOperand(1);
1021 // TODO: Use pattern matchers instead of dyn_cast.
1022 if (Value *Op0NotVal = dyn_castNotVal(Op0))
1023 if (Value *Op1NotVal = dyn_castNotVal(Op1))
1024 if (Op0->hasOneUse() && Op1->hasOneUse()) {
Sanjay Patelb54e62f2015-09-08 20:14:13 +00001025 Value *LogicOp = Builder->CreateBinOp(Opcode, Op0NotVal, Op1NotVal,
1026 I.getName() + ".demorgan");
1027 return BinaryOperator::CreateNot(LogicOp);
1028 }
1029
1030 return nullptr;
1031}
1032
Tobias Grosser8ef834c2016-07-19 09:06:08 +00001033bool InstCombiner::shouldOptimizeCast(CastInst *CI) {
1034 Value *CastSrc = CI->getOperand(0);
1035
1036 // Noop casts and casts of constants should be eliminated trivially.
1037 if (CI->getSrcTy() == CI->getDestTy() || isa<Constant>(CastSrc))
1038 return false;
1039
1040 // If this cast is paired with another cast that can be eliminated, we prefer
1041 // to have it eliminated.
1042 if (const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc))
1043 if (isEliminableCastPair(PrecedingCI, CI))
1044 return false;
1045
1046 // If this is a vector sext from a compare, then we don't want to break the
1047 // idiom where each element of the extended vector is either zero or all ones.
1048 if (CI->getOpcode() == Instruction::SExt &&
1049 isa<CmpInst>(CastSrc) && CI->getDestTy()->isVectorTy())
1050 return false;
1051
1052 return true;
1053}
1054
Sanjay Patel60312bc42016-09-12 00:16:23 +00001055/// Fold {and,or,xor} (cast X), C.
1056static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast,
1057 InstCombiner::BuilderTy *Builder) {
1058 Constant *C;
1059 if (!match(Logic.getOperand(1), m_Constant(C)))
1060 return nullptr;
1061
1062 auto LogicOpc = Logic.getOpcode();
1063 Type *DestTy = Logic.getType();
1064 Type *SrcTy = Cast->getSrcTy();
1065
1066 // If the first operand is bitcast, move the logic operation ahead of the
1067 // bitcast (do the logic operation in the original type). This can eliminate
1068 // bitcasts and allow combines that would otherwise be impeded by the bitcast.
1069 Value *X;
1070 if (match(Cast, m_BitCast(m_Value(X)))) {
1071 Value *NewConstant = ConstantExpr::getBitCast(C, SrcTy);
1072 Value *NewOp = Builder->CreateBinOp(LogicOpc, X, NewConstant);
1073 return CastInst::CreateBitOrPointerCast(NewOp, DestTy);
1074 }
1075
1076 // Similarly, move the logic operation ahead of a zext if the constant is
1077 // unchanged in the smaller source type. Performing the logic in a smaller
1078 // type may provide more information to later folds, and the smaller logic
1079 // instruction may be cheaper (particularly in the case of vectors).
1080 if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) {
1081 Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy);
1082 Constant *ZextTruncC = ConstantExpr::getZExt(TruncC, DestTy);
1083 if (ZextTruncC == C) {
1084 // LogicOpc (zext X), C --> zext (LogicOpc X, C)
1085 Value *NewOp = Builder->CreateBinOp(LogicOpc, X, TruncC);
1086 return new ZExtInst(NewOp, DestTy);
1087 }
1088 }
1089
1090 return nullptr;
1091}
1092
1093/// Fold {and,or,xor} (cast X), Y.
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001094Instruction *InstCombiner::foldCastedBitwiseLogic(BinaryOperator &I) {
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001095 auto LogicOpc = I.getOpcode();
Sanjay Patel1e6ca442016-11-22 22:54:36 +00001096 assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding");
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001097
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001098 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patel713f25e2016-02-23 17:41:34 +00001099 CastInst *Cast0 = dyn_cast<CastInst>(Op0);
Sanjay Patel9bba7502016-03-03 19:19:04 +00001100 if (!Cast0)
Sanjay Patel7d0d8102016-02-23 16:59:21 +00001101 return nullptr;
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001102
Sanjay Patel9bba7502016-03-03 19:19:04 +00001103 // This must be a cast from an integer or integer vector source type to allow
1104 // transformation of the logic operation to the source type.
1105 Type *DestTy = I.getType();
Sanjay Patel713f25e2016-02-23 17:41:34 +00001106 Type *SrcTy = Cast0->getSrcTy();
Sanjay Patel9bba7502016-03-03 19:19:04 +00001107 if (!SrcTy->isIntOrIntVectorTy())
1108 return nullptr;
1109
Sanjay Patel60312bc42016-09-12 00:16:23 +00001110 if (Instruction *Ret = foldLogicCastConstant(I, Cast0, Builder))
1111 return Ret;
Sanjay Patel0753c062016-07-21 00:24:18 +00001112
Sanjay Patel9bba7502016-03-03 19:19:04 +00001113 CastInst *Cast1 = dyn_cast<CastInst>(Op1);
1114 if (!Cast1)
1115 return nullptr;
1116
1117 // Both operands of the logic operation are casts. The casts must be of the
1118 // same type for reduction.
1119 auto CastOpcode = Cast0->getOpcode();
1120 if (CastOpcode != Cast1->getOpcode() || SrcTy != Cast1->getSrcTy())
Sanjay Patel713f25e2016-02-23 17:41:34 +00001121 return nullptr;
1122
1123 Value *Cast0Src = Cast0->getOperand(0);
1124 Value *Cast1Src = Cast1->getOperand(0);
Sanjay Patel713f25e2016-02-23 17:41:34 +00001125
Tobias Grosser8ef834c2016-07-19 09:06:08 +00001126 // fold logic(cast(A), cast(B)) -> cast(logic(A, B))
Tobias Grosser8757e382016-08-03 19:30:35 +00001127 if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001128 Value *NewOp = Builder->CreateBinOp(LogicOpc, Cast0Src, Cast1Src,
1129 I.getName());
Sanjay Patel713f25e2016-02-23 17:41:34 +00001130 return CastInst::Create(CastOpcode, NewOp, DestTy);
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001131 }
Sanjay Patel713f25e2016-02-23 17:41:34 +00001132
Sanjay Pateldbbaca02016-02-24 17:00:34 +00001133 // For now, only 'and'/'or' have optimizations after this.
1134 if (LogicOpc == Instruction::Xor)
1135 return nullptr;
1136
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001137 // If this is logic(cast(icmp), cast(icmp)), 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 ICmpInst *ICmp0 = dyn_cast<ICmpInst>(Cast0Src);
1140 ICmpInst *ICmp1 = dyn_cast<ICmpInst>(Cast1Src);
1141 if (ICmp0 && ICmp1) {
1142 Value *Res = LogicOpc == Instruction::And ? FoldAndOfICmps(ICmp0, ICmp1)
1143 : FoldOrOfICmps(ICmp0, ICmp1, &I);
1144 if (Res)
1145 return CastInst::Create(CastOpcode, Res, DestTy);
1146 return nullptr;
1147 }
Sanjay Patel713f25e2016-02-23 17:41:34 +00001148
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001149 // If this is logic(cast(fcmp), cast(fcmp)), try to fold this even if the
Sanjay Patel713f25e2016-02-23 17:41:34 +00001150 // cast is otherwise not optimizable. This happens for vector sexts.
Sanjay Patel75b4ae22016-02-23 23:56:23 +00001151 FCmpInst *FCmp0 = dyn_cast<FCmpInst>(Cast0Src);
1152 FCmpInst *FCmp1 = dyn_cast<FCmpInst>(Cast1Src);
1153 if (FCmp0 && FCmp1) {
1154 Value *Res = LogicOpc == Instruction::And ? FoldAndOfFCmps(FCmp0, FCmp1)
1155 : FoldOrOfFCmps(FCmp0, FCmp1);
1156 if (Res)
1157 return CastInst::Create(CastOpcode, Res, DestTy);
1158 return nullptr;
1159 }
Sanjay Patel713f25e2016-02-23 17:41:34 +00001160
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001161 return nullptr;
1162}
1163
Sanjay Patel74d23ad2016-05-27 21:41:29 +00001164static Instruction *foldBoolSextMaskToSelect(BinaryOperator &I) {
1165 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1166
1167 // Canonicalize SExt or Not to the LHS
1168 if (match(Op1, m_SExt(m_Value())) || match(Op1, m_Not(m_Value()))) {
1169 std::swap(Op0, Op1);
1170 }
1171
1172 // Fold (and (sext bool to A), B) --> (select bool, B, 0)
1173 Value *X = nullptr;
1174 if (match(Op0, m_SExt(m_Value(X))) &&
1175 X->getType()->getScalarType()->isIntegerTy(1)) {
1176 Value *Zero = Constant::getNullValue(Op1->getType());
1177 return SelectInst::Create(X, Op1, Zero);
1178 }
1179
1180 // Fold (and ~(sext bool to A), B) --> (select bool, 0, B)
1181 if (match(Op0, m_Not(m_SExt(m_Value(X)))) &&
1182 X->getType()->getScalarType()->isIntegerTy(1)) {
1183 Value *Zero = Constant::getNullValue(Op0->getType());
1184 return SelectInst::Create(X, Zero, Op1);
1185 }
Justin Bognerc7e4fbe2016-08-05 01:09:48 +00001186
Sanjay Patel74d23ad2016-05-27 21:41:29 +00001187 return nullptr;
1188}
1189
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00001190// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
1191// here. We should standardize that construct where it is needed or choose some
1192// other way to ensure that commutated variants of patterns are not missed.
Chris Lattner0a8191e2010-01-05 07:50:36 +00001193Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00001194 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001195 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1196
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001197 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00001198 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +00001199
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001200 if (Value *V = SimplifyAndInst(Op0, Op1, DL, &TLI, &DT, &AC))
Sanjay Patel4b198802016-02-01 22:23:39 +00001201 return replaceInstUsesWith(I, V);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001202
Duncan Sandsfbb9ac32010-12-22 13:36:08 +00001203 // (A|B)&(A|C) -> A|(B&C) etc
1204 if (Value *V = SimplifyUsingDistributiveLaws(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00001205 return replaceInstUsesWith(I, V);
Duncan Sandsadc7771f2010-11-23 14:23:47 +00001206
Craig Topper9d4171a2012-12-20 07:09:41 +00001207 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00001208 // purpose is to compute bits we don't care about.
1209 if (SimplifyDemandedInstructionBits(I))
Craig Topper9d4171a2012-12-20 07:09:41 +00001210 return &I;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001211
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00001212 if (Value *V = SimplifyBSwap(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00001213 return replaceInstUsesWith(I, V);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00001214
Chris Lattner0a8191e2010-01-05 07:50:36 +00001215 if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) {
1216 const APInt &AndRHSMask = AndRHS->getValue();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001217
1218 // Optimize a variety of ((val OP C1) & C2) combinations...
1219 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
1220 Value *Op0LHS = Op0I->getOperand(0);
1221 Value *Op0RHS = Op0I->getOperand(1);
1222 switch (Op0I->getOpcode()) {
1223 default: break;
1224 case Instruction::Xor:
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001225 case Instruction::Or: {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001226 // If the mask is only needed on one incoming arm, push it up.
1227 if (!Op0I->hasOneUse()) break;
Craig Topper9d4171a2012-12-20 07:09:41 +00001228
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001229 APInt NotAndRHS(~AndRHSMask);
Hal Finkel60db0582014-09-07 18:57:58 +00001230 if (MaskedValueIsZero(Op0LHS, NotAndRHS, 0, &I)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001231 // Not masking anything out for the LHS, move to RHS.
1232 Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS,
1233 Op0RHS->getName()+".masked");
1234 return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS);
1235 }
1236 if (!isa<Constant>(Op0RHS) &&
Hal Finkel60db0582014-09-07 18:57:58 +00001237 MaskedValueIsZero(Op0RHS, NotAndRHS, 0, &I)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001238 // Not masking anything out for the RHS, move to LHS.
1239 Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS,
1240 Op0LHS->getName()+".masked");
1241 return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS);
1242 }
1243
1244 break;
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001245 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001246 case Instruction::Sub:
Balaram Makamccf59732015-08-20 15:35:00 +00001247 // -x & 1 -> x & 1
1248 if (AndRHSMask == 1 && match(Op0LHS, m_Zero()))
1249 return BinaryOperator::CreateAnd(Op0RHS, AndRHS);
1250
Chris Lattner0a8191e2010-01-05 07:50:36 +00001251 break;
1252
1253 case Instruction::Shl:
1254 case Instruction::LShr:
1255 // (1 << x) & 1 --> zext(x == 0)
1256 // (1 >> x) & 1 --> zext(x == 0)
1257 if (AndRHSMask == 1 && Op0LHS == AndRHS) {
1258 Value *NewICmp =
1259 Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType()));
1260 return new ZExtInst(NewICmp, I.getType());
1261 }
1262 break;
1263 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001264
David Majnemerde55c602017-01-17 18:08:06 +00001265 // ((C1 OP zext(X)) & C2) -> zext((C1-X) & C2) if C2 fits in the bitwidth
1266 // of X and OP behaves well when given trunc(C1) and X.
1267 switch (Op0I->getOpcode()) {
1268 default:
1269 break;
1270 case Instruction::Xor:
1271 case Instruction::Or:
1272 case Instruction::Mul:
1273 case Instruction::Add:
1274 case Instruction::Sub:
1275 Value *X;
1276 ConstantInt *C1;
1277 if (match(Op0I, m_BinOp(m_ZExt(m_Value(X)), m_ConstantInt(C1))) ||
1278 match(Op0I, m_BinOp(m_ConstantInt(C1), m_ZExt(m_Value(X))))) {
1279 if (AndRHSMask.isIntN(X->getType()->getScalarSizeInBits())) {
1280 auto *TruncC1 = ConstantExpr::getTrunc(C1, X->getType());
1281 Value *BinOp;
1282 if (isa<ZExtInst>(Op0LHS))
1283 BinOp = Builder->CreateBinOp(Op0I->getOpcode(), X, TruncC1);
1284 else
1285 BinOp = Builder->CreateBinOp(Op0I->getOpcode(), TruncC1, X);
1286 auto *TruncC2 = ConstantExpr::getTrunc(AndRHS, X->getType());
1287 auto *And = Builder->CreateAnd(BinOp, TruncC2);
1288 return new ZExtInst(And, I.getType());
1289 }
1290 }
1291 }
1292
Chris Lattner0a8191e2010-01-05 07:50:36 +00001293 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
1294 if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I))
1295 return Res;
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001296 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001297
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001298 // If this is an integer truncation, and if the source is an 'and' with
1299 // immediate, transform it. This frequently occurs for bitfield accesses.
1300 {
Craig Topperf40110f2014-04-25 05:29:35 +00001301 Value *X = nullptr; ConstantInt *YC = nullptr;
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001302 if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) {
1303 // Change: and (trunc (and X, YC) to T), C2
1304 // into : and (trunc X to T), trunc(YC) & C2
Craig Topper9d4171a2012-12-20 07:09:41 +00001305 // This will fold the two constants together, which may allow
Chris Lattnerdcef03f2011-02-10 05:17:27 +00001306 // other simplifications.
1307 Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk");
1308 Constant *C3 = ConstantExpr::getTrunc(YC, I.getType());
1309 C3 = ConstantExpr::getAnd(C3, AndRHS);
1310 return BinaryOperator::CreateAnd(NewCast, C3);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001311 }
1312 }
1313
Sanjay Pateldb0938f2017-01-10 23:49:07 +00001314 if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
1315 return FoldedLogic;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001316 }
1317
Sanjay Patelb54e62f2015-09-08 20:14:13 +00001318 if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
1319 return DeMorgan;
Craig Topper9d4171a2012-12-20 07:09:41 +00001320
Chris Lattner0a8191e2010-01-05 07:50:36 +00001321 {
Craig Topperf40110f2014-04-25 05:29:35 +00001322 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001323 // (A|B) & ~(A&B) -> A^B
1324 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1325 match(Op1, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1326 ((A == C && B == D) || (A == D && B == C)))
1327 return BinaryOperator::CreateXor(A, B);
Craig Topper9d4171a2012-12-20 07:09:41 +00001328
Chris Lattner0a8191e2010-01-05 07:50:36 +00001329 // ~(A&B) & (A|B) -> A^B
1330 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
1331 match(Op0, m_Not(m_And(m_Value(C), m_Value(D)))) &&
1332 ((A == C && B == D) || (A == D && B == C)))
1333 return BinaryOperator::CreateXor(A, B);
Craig Topper9d4171a2012-12-20 07:09:41 +00001334
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001335 // A&(A^B) => A & ~B
1336 {
1337 Value *tmpOp0 = Op0;
1338 Value *tmpOp1 = Op1;
Sanjay Patel7b7eec12016-01-18 18:36:38 +00001339 if (match(Op0, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) {
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001340 if (A == Op1 || B == Op1 ) {
1341 tmpOp1 = Op0;
1342 tmpOp0 = Op1;
1343 // Simplify below
1344 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001345 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001346
Sanjay Patel7b7eec12016-01-18 18:36:38 +00001347 if (match(tmpOp1, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) {
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001348 if (B == tmpOp0) {
1349 std::swap(A, B);
1350 }
Sanjay Pateld09b44a2016-01-18 17:50:23 +00001351 // Notice that the pattern (A&(~B)) is actually (A&(-1^B)), so if
Eli Friedman61d7c8a2011-09-19 21:58:15 +00001352 // A is originally -1 (or a vector of -1 and undefs), then we enter
1353 // an endless loop. By checking that A is non-constant we ensure that
1354 // we will never get to the loop.
1355 if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B
Benjamin Kramer547b6c52011-09-27 20:39:19 +00001356 return BinaryOperator::CreateAnd(A, Builder->CreateNot(B));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001357 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001358 }
1359
1360 // (A&((~A)|B)) -> A&B
1361 if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) ||
1362 match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1)))))
1363 return BinaryOperator::CreateAnd(A, Op1);
1364 if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) ||
1365 match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0)))))
1366 return BinaryOperator::CreateAnd(A, Op0);
David Majnemer42af3602014-07-30 21:26:37 +00001367
1368 // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
1369 if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
1370 if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
1371 if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
1372 return BinaryOperator::CreateAnd(Op0, Builder->CreateNot(C));
1373
1374 // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
1375 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
1376 if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
1377 if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
1378 return BinaryOperator::CreateAnd(Op1, Builder->CreateNot(C));
Suyog Sarda1c6c2f62014-08-01 04:59:26 +00001379
1380 // (A | B) & ((~A) ^ B) -> (A & B)
1381 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
1382 match(Op1, m_Xor(m_Not(m_Specific(A)), m_Specific(B))))
1383 return BinaryOperator::CreateAnd(A, B);
1384
1385 // ((~A) ^ B) & (A | B) -> (A & B)
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00001386 // ((~A) ^ B) & (B | A) -> (A & B)
Suyog Sarda1c6c2f62014-08-01 04:59:26 +00001387 if (match(Op0, m_Xor(m_Not(m_Value(A)), m_Value(B))) &&
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00001388 match(Op1, m_c_Or(m_Specific(A), m_Specific(B))))
Suyog Sarda1c6c2f62014-08-01 04:59:26 +00001389 return BinaryOperator::CreateAnd(A, B);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001390 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001391
David Majnemer5e96f1b2014-08-30 06:18:20 +00001392 {
1393 ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
1394 ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
1395 if (LHS && RHS)
Chris Lattner067459c2010-03-05 08:46:26 +00001396 if (Value *Res = FoldAndOfICmps(LHS, RHS))
Sanjay Patel4b198802016-02-01 22:23:39 +00001397 return replaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00001398
David Majnemer5e96f1b2014-08-30 06:18:20 +00001399 // TODO: Make this recursive; it's a little tricky because an arbitrary
1400 // number of 'and' instructions might have to be created.
1401 Value *X, *Y;
1402 if (LHS && match(Op1, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
1403 if (auto *Cmp = dyn_cast<ICmpInst>(X))
1404 if (Value *Res = FoldAndOfICmps(LHS, Cmp))
Sanjay Patel4b198802016-02-01 22:23:39 +00001405 return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y));
David Majnemer5e96f1b2014-08-30 06:18:20 +00001406 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
1407 if (Value *Res = FoldAndOfICmps(LHS, Cmp))
Sanjay Patel4b198802016-02-01 22:23:39 +00001408 return replaceInstUsesWith(I, Builder->CreateAnd(Res, X));
David Majnemer5e96f1b2014-08-30 06:18:20 +00001409 }
1410 if (RHS && match(Op0, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
1411 if (auto *Cmp = dyn_cast<ICmpInst>(X))
1412 if (Value *Res = FoldAndOfICmps(Cmp, RHS))
Sanjay Patel4b198802016-02-01 22:23:39 +00001413 return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y));
David Majnemer5e96f1b2014-08-30 06:18:20 +00001414 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
1415 if (Value *Res = FoldAndOfICmps(Cmp, RHS))
Sanjay Patel4b198802016-02-01 22:23:39 +00001416 return replaceInstUsesWith(I, Builder->CreateAnd(Res, X));
David Majnemer5e96f1b2014-08-30 06:18:20 +00001417 }
1418 }
1419
Chris Lattner4e8137d2010-02-11 06:26:33 +00001420 // If and'ing two fcmp, try combine them into one.
1421 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
1422 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00001423 if (Value *Res = FoldAndOfFCmps(LHS, RHS))
Sanjay Patel4b198802016-02-01 22:23:39 +00001424 return replaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00001425
Sanjay Patel40e7ba02016-02-23 16:36:07 +00001426 if (Instruction *CastedAnd = foldCastedBitwiseLogic(I))
1427 return CastedAnd;
Craig Topper9d4171a2012-12-20 07:09:41 +00001428
Sanjay Patel74d23ad2016-05-27 21:41:29 +00001429 if (Instruction *Select = foldBoolSextMaskToSelect(I))
1430 return Select;
Nadav Rotem513bd8a2013-01-30 06:35:22 +00001431
Craig Topperf40110f2014-04-25 05:29:35 +00001432 return Changed ? &I : nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001433}
1434
Chad Rosiera00df492016-05-25 16:22:14 +00001435/// Given an OR instruction, check to see if this is a bswap idiom. If so,
1436/// insert the new intrinsic and return it.
1437Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
Chad Rosiere5819e22016-05-26 14:58:51 +00001438 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1439
1440 // Look through zero extends.
1441 if (Instruction *Ext = dyn_cast<ZExtInst>(Op0))
1442 Op0 = Ext->getOperand(0);
1443
1444 if (Instruction *Ext = dyn_cast<ZExtInst>(Op1))
1445 Op1 = Ext->getOperand(0);
1446
1447 // (A | B) | C and A | (B | C) -> bswap if possible.
1448 bool OrOfOrs = match(Op0, m_Or(m_Value(), m_Value())) ||
1449 match(Op1, m_Or(m_Value(), m_Value()));
1450
1451 // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible.
1452 bool OrOfShifts = match(Op0, m_LogicalShift(m_Value(), m_Value())) &&
1453 match(Op1, m_LogicalShift(m_Value(), m_Value()));
1454
1455 // (A & B) | (C & D) -> bswap if possible.
1456 bool OrOfAnds = match(Op0, m_And(m_Value(), m_Value())) &&
1457 match(Op1, m_And(m_Value(), m_Value()));
1458
1459 if (!OrOfOrs && !OrOfShifts && !OrOfAnds)
1460 return nullptr;
1461
James Molloyf01488e2016-01-15 09:20:19 +00001462 SmallVector<Instruction*, 4> Insts;
Chad Rosiera00df492016-05-25 16:22:14 +00001463 if (!recognizeBSwapOrBitReverseIdiom(&I, true, false, Insts))
Craig Topperf40110f2014-04-25 05:29:35 +00001464 return nullptr;
James Molloyf01488e2016-01-15 09:20:19 +00001465 Instruction *LastInst = Insts.pop_back_val();
1466 LastInst->removeFromParent();
Craig Topper9d4171a2012-12-20 07:09:41 +00001467
James Molloyf01488e2016-01-15 09:20:19 +00001468 for (auto *Inst : Insts)
1469 Worklist.Add(Inst);
1470 return LastInst;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001471}
1472
Sanjay Patelc00e48a2016-07-13 18:07:02 +00001473/// If all elements of two constant vectors are 0/-1 and inverses, return true.
1474static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) {
1475 unsigned NumElts = C1->getType()->getVectorNumElements();
1476 for (unsigned i = 0; i != NumElts; ++i) {
1477 Constant *EltC1 = C1->getAggregateElement(i);
1478 Constant *EltC2 = C2->getAggregateElement(i);
1479 if (!EltC1 || !EltC2)
1480 return false;
1481
1482 // One element must be all ones, and the other must be all zeros.
1483 // FIXME: Allow undef elements.
1484 if (!((match(EltC1, m_Zero()) && match(EltC2, m_AllOnes())) ||
1485 (match(EltC2, m_Zero()) && match(EltC1, m_AllOnes()))))
1486 return false;
1487 }
1488 return true;
1489}
1490
Sanjay Patel7ad98ba2016-06-30 14:18:18 +00001491/// We have an expression of the form (A & C) | (B & D). If A is a scalar or
1492/// vector composed of all-zeros or all-ones values and is the bitwise 'not' of
1493/// B, it can be used as the condition operand of a select instruction.
Sanjay Patelc00e48a2016-07-13 18:07:02 +00001494static Value *getSelectCondition(Value *A, Value *B,
1495 InstCombiner::BuilderTy &Builder) {
Sanjay Patel7ad98ba2016-06-30 14:18:18 +00001496 // If these are scalars or vectors of i1, A can be used directly.
1497 Type *Ty = A->getType();
1498 if (match(A, m_Not(m_Specific(B))) && Ty->getScalarType()->isIntegerTy(1))
1499 return A;
1500
1501 // If A and B are sign-extended, look through the sexts to find the booleans.
1502 Value *Cond;
1503 if (match(A, m_SExt(m_Value(Cond))) &&
1504 Cond->getType()->getScalarType()->isIntegerTy(1) &&
1505 match(B, m_CombineOr(m_Not(m_SExt(m_Specific(Cond))),
1506 m_SExt(m_Not(m_Specific(Cond))))))
1507 return Cond;
1508
Sanjay Patelc00e48a2016-07-13 18:07:02 +00001509 // All scalar (and most vector) possibilities should be handled now.
1510 // Try more matches that only apply to non-splat constant vectors.
1511 if (!Ty->isVectorTy())
1512 return nullptr;
Sanjay Patel7ad98ba2016-06-30 14:18:18 +00001513
Sanjay Patelc00e48a2016-07-13 18:07:02 +00001514 // If both operands are constants, see if the constants are inverse bitmasks.
1515 Constant *AC, *BC;
1516 if (match(A, m_Constant(AC)) && match(B, m_Constant(BC)) &&
1517 areInverseVectorBitmasks(AC, BC))
1518 return ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty));
1519
1520 // If both operands are xor'd with constants using the same sexted boolean
1521 // operand, see if the constants are inverse bitmasks.
1522 if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AC)))) &&
1523 match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BC)))) &&
1524 Cond->getType()->getScalarType()->isIntegerTy(1) &&
1525 areInverseVectorBitmasks(AC, BC)) {
1526 AC = ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty));
1527 return Builder.CreateXor(Cond, AC);
1528 }
Sanjay Patel7ad98ba2016-06-30 14:18:18 +00001529 return nullptr;
1530}
1531
1532/// We have an expression of the form (A & C) | (B & D). Try to simplify this
1533/// to "A' ? C : D", where A' is a boolean or vector of booleans.
Sanjay Patel4e8ebce2016-06-24 18:55:27 +00001534static Value *matchSelectFromAndOr(Value *A, Value *C, Value *B, Value *D,
Sanjay Patel7ad98ba2016-06-30 14:18:18 +00001535 InstCombiner::BuilderTy &Builder) {
Sanjay Patel4e8ebce2016-06-24 18:55:27 +00001536 // The potential condition of the select may be bitcasted. In that case, look
1537 // through its bitcast and the corresponding bitcast of the 'not' condition.
1538 Type *OrigType = A->getType();
1539 Value *SrcA, *SrcB;
Sanjay Patel664514f2016-07-08 21:17:51 +00001540 if (match(A, m_OneUse(m_BitCast(m_Value(SrcA)))) &&
1541 match(B, m_OneUse(m_BitCast(m_Value(SrcB))))) {
Sanjay Patel4e8ebce2016-06-24 18:55:27 +00001542 A = SrcA;
1543 B = SrcB;
Sanjay Patel6cf18af2016-06-03 14:42:07 +00001544 }
1545
Sanjay Patelc00e48a2016-07-13 18:07:02 +00001546 if (Value *Cond = getSelectCondition(A, B, Builder)) {
Sanjay Patel6cf18af2016-06-03 14:42:07 +00001547 // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D))
Sanjay Patel4e8ebce2016-06-24 18:55:27 +00001548 // The bitcasts will either all exist or all not exist. The builder will
1549 // not create unnecessary casts if the types already match.
1550 Value *BitcastC = Builder.CreateBitCast(C, A->getType());
1551 Value *BitcastD = Builder.CreateBitCast(D, A->getType());
1552 Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD);
1553 return Builder.CreateBitCast(Select, OrigType);
Sanjay Patel6cf18af2016-06-03 14:42:07 +00001554 }
Sanjay Patel5c0bc022016-06-02 18:03:05 +00001555
Craig Topperf40110f2014-04-25 05:29:35 +00001556 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001557}
1558
Sanjay Patel18549272015-09-08 18:24:36 +00001559/// Fold (icmp)|(icmp) if possible.
Hal Finkel60db0582014-09-07 18:57:58 +00001560Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
1561 Instruction *CxtI) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00001562 ICmpInst::Predicate LHSCC = LHS->getPredicate(), RHSCC = RHS->getPredicate();
1563
Nadav Rotem0ed2fdb2013-11-12 22:38:59 +00001564 // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2)
1565 // if K1 and K2 are a one-bit mask.
1566 ConstantInt *LHSCst = dyn_cast<ConstantInt>(LHS->getOperand(1));
1567 ConstantInt *RHSCst = dyn_cast<ConstantInt>(RHS->getOperand(1));
1568
1569 if (LHS->getPredicate() == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero() &&
1570 RHS->getPredicate() == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1571
1572 BinaryOperator *LAnd = dyn_cast<BinaryOperator>(LHS->getOperand(0));
1573 BinaryOperator *RAnd = dyn_cast<BinaryOperator>(RHS->getOperand(0));
1574 if (LAnd && RAnd && LAnd->hasOneUse() && RHS->hasOneUse() &&
1575 LAnd->getOpcode() == Instruction::And &&
1576 RAnd->getOpcode() == Instruction::And) {
1577
Craig Topperf40110f2014-04-25 05:29:35 +00001578 Value *Mask = nullptr;
1579 Value *Masked = nullptr;
Nadav Rotem0ed2fdb2013-11-12 22:38:59 +00001580 if (LAnd->getOperand(0) == RAnd->getOperand(0) &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001581 isKnownToBeAPowerOfTwo(LAnd->getOperand(1), DL, false, 0, &AC, CxtI,
Justin Bogner99798402016-08-05 01:06:44 +00001582 &DT) &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001583 isKnownToBeAPowerOfTwo(RAnd->getOperand(1), DL, false, 0, &AC, CxtI,
Justin Bogner99798402016-08-05 01:06:44 +00001584 &DT)) {
Nadav Rotem0ed2fdb2013-11-12 22:38:59 +00001585 Mask = Builder->CreateOr(LAnd->getOperand(1), RAnd->getOperand(1));
1586 Masked = Builder->CreateAnd(LAnd->getOperand(0), Mask);
1587 } else if (LAnd->getOperand(1) == RAnd->getOperand(1) &&
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001588 isKnownToBeAPowerOfTwo(LAnd->getOperand(0), DL, false, 0, &AC,
1589 CxtI, &DT) &&
1590 isKnownToBeAPowerOfTwo(RAnd->getOperand(0), DL, false, 0, &AC,
1591 CxtI, &DT)) {
Nadav Rotem0ed2fdb2013-11-12 22:38:59 +00001592 Mask = Builder->CreateOr(LAnd->getOperand(0), RAnd->getOperand(0));
1593 Masked = Builder->CreateAnd(LAnd->getOperand(1), Mask);
1594 }
1595
1596 if (Masked)
1597 return Builder->CreateICmp(ICmpInst::ICMP_NE, Masked, Mask);
1598 }
1599 }
1600
Yi Jiang1a4e73d2014-08-20 22:55:40 +00001601 // Fold (icmp ult/ule (A + C1), C3) | (icmp ult/ule (A + C2), C3)
1602 // --> (icmp ult/ule ((A & ~(C1 ^ C2)) + max(C1, C2)), C3)
1603 // The original condition actually refers to the following two ranges:
1604 // [MAX_UINT-C1+1, MAX_UINT-C1+1+C3] and [MAX_UINT-C2+1, MAX_UINT-C2+1+C3]
1605 // We can fold these two ranges if:
1606 // 1) C1 and C2 is unsigned greater than C3.
1607 // 2) The two ranges are separated.
1608 // 3) C1 ^ C2 is one-bit mask.
1609 // 4) LowRange1 ^ LowRange2 and HighRange1 ^ HighRange2 are one-bit mask.
1610 // This implies all values in the two ranges differ by exactly one bit.
1611
1612 if ((LHSCC == ICmpInst::ICMP_ULT || LHSCC == ICmpInst::ICMP_ULE) &&
1613 LHSCC == RHSCC && LHSCst && RHSCst && LHS->hasOneUse() &&
1614 RHS->hasOneUse() && LHSCst->getType() == RHSCst->getType() &&
1615 LHSCst->getValue() == (RHSCst->getValue())) {
1616
1617 Value *LAdd = LHS->getOperand(0);
1618 Value *RAdd = RHS->getOperand(0);
1619
1620 Value *LAddOpnd, *RAddOpnd;
1621 ConstantInt *LAddCst, *RAddCst;
1622 if (match(LAdd, m_Add(m_Value(LAddOpnd), m_ConstantInt(LAddCst))) &&
1623 match(RAdd, m_Add(m_Value(RAddOpnd), m_ConstantInt(RAddCst))) &&
1624 LAddCst->getValue().ugt(LHSCst->getValue()) &&
1625 RAddCst->getValue().ugt(LHSCst->getValue())) {
1626
1627 APInt DiffCst = LAddCst->getValue() ^ RAddCst->getValue();
1628 if (LAddOpnd == RAddOpnd && DiffCst.isPowerOf2()) {
1629 ConstantInt *MaxAddCst = nullptr;
1630 if (LAddCst->getValue().ult(RAddCst->getValue()))
1631 MaxAddCst = RAddCst;
1632 else
1633 MaxAddCst = LAddCst;
1634
1635 APInt RRangeLow = -RAddCst->getValue();
1636 APInt RRangeHigh = RRangeLow + LHSCst->getValue();
1637 APInt LRangeLow = -LAddCst->getValue();
1638 APInt LRangeHigh = LRangeLow + LHSCst->getValue();
1639 APInt LowRangeDiff = RRangeLow ^ LRangeLow;
1640 APInt HighRangeDiff = RRangeHigh ^ LRangeHigh;
1641 APInt RangeDiff = LRangeLow.sgt(RRangeLow) ? LRangeLow - RRangeLow
1642 : RRangeLow - LRangeLow;
1643
1644 if (LowRangeDiff.isPowerOf2() && LowRangeDiff == HighRangeDiff &&
1645 RangeDiff.ugt(LHSCst->getValue())) {
1646 Value *MaskCst = ConstantInt::get(LAddCst->getType(), ~DiffCst);
1647
1648 Value *NewAnd = Builder->CreateAnd(LAddOpnd, MaskCst);
1649 Value *NewAdd = Builder->CreateAdd(NewAnd, MaxAddCst);
1650 return (Builder->CreateICmp(LHS->getPredicate(), NewAdd, LHSCst));
1651 }
1652 }
1653 }
1654 }
1655
Chris Lattner0a8191e2010-01-05 07:50:36 +00001656 // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
1657 if (PredicatesFoldable(LHSCC, RHSCC)) {
1658 if (LHS->getOperand(0) == RHS->getOperand(1) &&
1659 LHS->getOperand(1) == RHS->getOperand(0))
1660 LHS->swapOperands();
1661 if (LHS->getOperand(0) == RHS->getOperand(0) &&
1662 LHS->getOperand(1) == RHS->getOperand(1)) {
1663 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
1664 unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
1665 bool isSigned = LHS->isSigned() || RHS->isSigned();
Pete Cooperebf98c12011-12-17 01:20:32 +00001666 return getNewICmpValue(isSigned, Code, Op0, Op1, Builder);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001667 }
1668 }
Benjamin Kramer2bca3a62010-12-20 16:21:59 +00001669
1670 // handle (roughly):
1671 // (icmp ne (A & B), C) | (icmp ne (A & D), E)
Tim Northoverc0756c42013-09-04 11:57:13 +00001672 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder))
Benjamin Kramer2bca3a62010-12-20 16:21:59 +00001673 return V;
Owen Anderson3fe002d2010-09-08 22:16:17 +00001674
Chris Lattner0a8191e2010-01-05 07:50:36 +00001675 Value *Val = LHS->getOperand(0), *Val2 = RHS->getOperand(0);
David Majnemerc2a990b2013-07-05 00:31:17 +00001676 if (LHS->hasOneUse() || RHS->hasOneUse()) {
1677 // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1)
1678 // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1)
Craig Topperf40110f2014-04-25 05:29:35 +00001679 Value *A = nullptr, *B = nullptr;
David Majnemerc2a990b2013-07-05 00:31:17 +00001680 if (LHSCC == ICmpInst::ICMP_EQ && LHSCst && LHSCst->isZero()) {
1681 B = Val;
1682 if (RHSCC == ICmpInst::ICMP_ULT && Val == RHS->getOperand(1))
1683 A = Val2;
1684 else if (RHSCC == ICmpInst::ICMP_UGT && Val == Val2)
1685 A = RHS->getOperand(1);
1686 }
1687 // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1)
1688 // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1)
1689 else if (RHSCC == ICmpInst::ICMP_EQ && RHSCst && RHSCst->isZero()) {
1690 B = Val2;
1691 if (LHSCC == ICmpInst::ICMP_ULT && Val2 == LHS->getOperand(1))
1692 A = Val;
1693 else if (LHSCC == ICmpInst::ICMP_UGT && Val2 == Val)
1694 A = LHS->getOperand(1);
1695 }
1696 if (A && B)
1697 return Builder->CreateICmp(
1698 ICmpInst::ICMP_UGE,
1699 Builder->CreateAdd(B, ConstantInt::getSigned(B->getType(), -1)), A);
1700 }
1701
Erik Ecksteind1817522014-12-03 10:39:15 +00001702 // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
1703 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/true))
1704 return V;
1705
1706 // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n
1707 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/true))
1708 return V;
Justin Bognerc7e4fbe2016-08-05 01:09:48 +00001709
David Majnemerc2a990b2013-07-05 00:31:17 +00001710 // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
Craig Topperf40110f2014-04-25 05:29:35 +00001711 if (!LHSCst || !RHSCst) return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001712
Owen Anderson8f306a72010-08-02 09:32:13 +00001713 if (LHSCst == RHSCst && LHSCC == RHSCC) {
1714 // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
1715 if (LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) {
1716 Value *NewOr = Builder->CreateOr(Val, Val2);
1717 return Builder->CreateICmp(LHSCC, NewOr, LHSCst);
1718 }
Benjamin Kramerda37e152012-01-08 18:32:24 +00001719 }
1720
Benjamin Kramerf7957d02010-12-20 20:00:31 +00001721 // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001722 // iff C2 + CA == C1.
Benjamin Kramerf7957d02010-12-20 20:00:31 +00001723 if (LHSCC == ICmpInst::ICMP_ULT && RHSCC == ICmpInst::ICMP_EQ) {
Benjamin Kramer68531ba2010-12-20 16:18:51 +00001724 ConstantInt *AddCst;
1725 if (match(Val, m_Add(m_Specific(Val2), m_ConstantInt(AddCst))))
1726 if (RHSCst->getValue() + AddCst->getValue() == LHSCst->getValue())
Benjamin Kramerf7957d02010-12-20 20:00:31 +00001727 return Builder->CreateICmpULE(Val, LHSCst);
Benjamin Kramer68531ba2010-12-20 16:18:51 +00001728 }
1729
Chris Lattner0a8191e2010-01-05 07:50:36 +00001730 // From here on, we only handle:
1731 // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler.
Craig Topperf40110f2014-04-25 05:29:35 +00001732 if (Val != Val2) return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +00001733
Chris Lattner0a8191e2010-01-05 07:50:36 +00001734 // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere.
1735 if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE ||
1736 RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE ||
1737 LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE ||
1738 RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE)
Craig Topperf40110f2014-04-25 05:29:35 +00001739 return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +00001740
Chris Lattner0a8191e2010-01-05 07:50:36 +00001741 // We can't fold (ugt x, C) | (sgt x, C2).
1742 if (!PredicatesFoldable(LHSCC, RHSCC))
Craig Topperf40110f2014-04-25 05:29:35 +00001743 return nullptr;
Craig Topper9d4171a2012-12-20 07:09:41 +00001744
Chris Lattner0a8191e2010-01-05 07:50:36 +00001745 // Ensure that the larger constant is on the RHS.
1746 bool ShouldSwap;
1747 if (CmpInst::isSigned(LHSCC) ||
Craig Topper9d4171a2012-12-20 07:09:41 +00001748 (ICmpInst::isEquality(LHSCC) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001749 CmpInst::isSigned(RHSCC)))
1750 ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue());
1751 else
1752 ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue());
Craig Topper9d4171a2012-12-20 07:09:41 +00001753
Chris Lattner0a8191e2010-01-05 07:50:36 +00001754 if (ShouldSwap) {
1755 std::swap(LHS, RHS);
1756 std::swap(LHSCst, RHSCst);
1757 std::swap(LHSCC, RHSCC);
1758 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001759
Dan Gohman4a618822010-02-10 16:03:48 +00001760 // At this point, we know we have two icmp instructions
Chris Lattner0a8191e2010-01-05 07:50:36 +00001761 // comparing a value against two constants and or'ing the result
1762 // together. Because of the above check, we know that we only have
1763 // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the
1764 // icmp folding check above), that the two constants are not
1765 // equal.
1766 assert(LHSCst != RHSCst && "Compares not folded above?");
1767
1768 switch (LHSCC) {
1769 default: llvm_unreachable("Unknown integer condition code!");
1770 case ICmpInst::ICMP_EQ:
1771 switch (RHSCC) {
1772 default: llvm_unreachable("Unknown integer condition code!");
1773 case ICmpInst::ICMP_EQ:
Jakub Staszakea2b9b92012-12-31 00:34:55 +00001774 if (LHS->getOperand(0) == RHS->getOperand(0)) {
Jakub Staszakf5849772012-12-31 01:40:44 +00001775 // if LHSCst and RHSCst differ only by one bit:
David Majnemer942003a2015-12-02 16:15:07 +00001776 // (A == C1 || A == C2) -> (A | (C1 ^ C2)) == C2
Jakub Staszakc48bbe72012-12-31 18:26:42 +00001777 assert(LHSCst->getValue().ule(LHSCst->getValue()));
1778
Jakub Staszakea2b9b92012-12-31 00:34:55 +00001779 APInt Xor = LHSCst->getValue() ^ RHSCst->getValue();
1780 if (Xor.isPowerOf2()) {
David Majnemer942003a2015-12-02 16:15:07 +00001781 Value *Cst = Builder->getInt(Xor);
1782 Value *Or = Builder->CreateOr(LHS->getOperand(0), Cst);
1783 return Builder->CreateICmp(ICmpInst::ICMP_EQ, Or, RHSCst);
Jakub Staszakea2b9b92012-12-31 00:34:55 +00001784 }
1785 }
1786
David Majnemer1fae1952013-04-14 21:15:43 +00001787 if (LHSCst == SubOne(RHSCst)) {
1788 // (X == 13 | X == 14) -> X-13 <u 2
1789 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
1790 Value *Add = Builder->CreateAdd(Val, AddCST, Val->getName()+".off");
1791 AddCST = ConstantExpr::getSub(AddOne(RHSCst), LHSCst);
1792 return Builder->CreateICmpULT(Add, AddCST);
1793 }
1794
Chris Lattner0a8191e2010-01-05 07:50:36 +00001795 break; // (X == 13 | X == 15) -> no change
1796 case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
1797 case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change
1798 break;
1799 case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15
1800 case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15
1801 case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15
Chris Lattner067459c2010-03-05 08:46:26 +00001802 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001803 }
1804 break;
1805 case ICmpInst::ICMP_NE:
1806 switch (RHSCC) {
1807 default: llvm_unreachable("Unknown integer condition code!");
1808 case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13
1809 case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13
1810 case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13
Chris Lattner067459c2010-03-05 08:46:26 +00001811 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001812 case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true
1813 case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true
1814 case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001815 return Builder->getTrue();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001816 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00001817 case ICmpInst::ICMP_ULT:
1818 switch (RHSCC) {
1819 default: llvm_unreachable("Unknown integer condition code!");
1820 case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change
1821 break;
1822 case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2
1823 // If RHSCst is [us]MAXINT, it is always false. Not handling
1824 // this can cause overflow.
1825 if (RHSCst->isMaxValue(false))
Chris Lattner067459c2010-03-05 08:46:26 +00001826 return LHS;
Sanjay Patel85d79742016-08-31 19:49:56 +00001827 return insertRangeTest(Val, LHSCst->getValue(), RHSCst->getValue() + 1,
1828 false, false);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001829 case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
1830 break;
1831 case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15
1832 case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15
Chris Lattner067459c2010-03-05 08:46:26 +00001833 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001834 case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change
1835 break;
1836 }
1837 break;
1838 case ICmpInst::ICMP_SLT:
1839 switch (RHSCC) {
1840 default: llvm_unreachable("Unknown integer condition code!");
1841 case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change
1842 break;
1843 case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2
1844 // If RHSCst is [us]MAXINT, it is always false. Not handling
1845 // this can cause overflow.
1846 if (RHSCst->isMaxValue(true))
Chris Lattner067459c2010-03-05 08:46:26 +00001847 return LHS;
Sanjay Patel85d79742016-08-31 19:49:56 +00001848 return insertRangeTest(Val, LHSCst->getValue(), RHSCst->getValue() + 1,
1849 true, false);
Chris Lattner0a8191e2010-01-05 07:50:36 +00001850 case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
1851 break;
1852 case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15
1853 case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15
Chris Lattner067459c2010-03-05 08:46:26 +00001854 return RHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001855 case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change
1856 break;
1857 }
1858 break;
1859 case ICmpInst::ICMP_UGT:
1860 switch (RHSCC) {
1861 default: llvm_unreachable("Unknown integer condition code!");
1862 case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13
1863 case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13
Chris Lattner067459c2010-03-05 08:46:26 +00001864 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001865 case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change
1866 break;
1867 case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true
1868 case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001869 return Builder->getTrue();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001870 case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change
1871 break;
1872 }
1873 break;
1874 case ICmpInst::ICMP_SGT:
1875 switch (RHSCC) {
1876 default: llvm_unreachable("Unknown integer condition code!");
1877 case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13
1878 case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13
Chris Lattner067459c2010-03-05 08:46:26 +00001879 return LHS;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001880 case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change
1881 break;
1882 case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true
1883 case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001884 return Builder->getTrue();
Chris Lattner0a8191e2010-01-05 07:50:36 +00001885 case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change
1886 break;
1887 }
1888 break;
1889 }
Craig Topperf40110f2014-04-25 05:29:35 +00001890 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001891}
1892
Sanjay Patel18549272015-09-08 18:24:36 +00001893/// Optimize (fcmp)|(fcmp). NOTE: Unlike the rest of instcombine, this returns
1894/// a Value which should already be inserted into the function.
Chris Lattner067459c2010-03-05 08:46:26 +00001895Value *InstCombiner::FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS) {
Tim Shenaec68b22016-06-29 20:10:17 +00001896 Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1);
1897 Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1);
1898 FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate();
1899
1900 if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) {
1901 // Swap RHS operands to match LHS.
1902 Op1CC = FCmpInst::getSwappedPredicate(Op1CC);
1903 std::swap(Op1LHS, Op1RHS);
1904 }
1905
1906 // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y).
1907 // This is a similar transformation to the one in FoldAndOfFCmps.
1908 //
1909 // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1910 // bool(R & CC0) || bool(R & CC1)
1911 // = bool((R & CC0) | (R & CC1))
1912 // = bool(R & (CC0 | CC1)) <= by reversed distribution (contribution? ;)
1913 if (Op0LHS == Op1LHS && Op0RHS == Op1RHS)
1914 return getFCmpValue(getFCmpCode(Op0CC) | getFCmpCode(Op1CC), Op0LHS, Op0RHS,
1915 Builder);
1916
Chris Lattner0a8191e2010-01-05 07:50:36 +00001917 if (LHS->getPredicate() == FCmpInst::FCMP_UNO &&
Craig Topper9d4171a2012-12-20 07:09:41 +00001918 RHS->getPredicate() == FCmpInst::FCMP_UNO &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00001919 LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) {
1920 if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1)))
1921 if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) {
1922 // If either of the constants are nans, then the whole thing returns
1923 // true.
1924 if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN())
Jakub Staszak461d1fe2013-06-06 00:37:23 +00001925 return Builder->getTrue();
Craig Topper9d4171a2012-12-20 07:09:41 +00001926
Chris Lattner0a8191e2010-01-05 07:50:36 +00001927 // Otherwise, no need to compare the two constants, compare the
1928 // rest.
Chris Lattner067459c2010-03-05 08:46:26 +00001929 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
Chris Lattner0a8191e2010-01-05 07:50:36 +00001930 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001931
Chris Lattner0a8191e2010-01-05 07:50:36 +00001932 // Handle vector zeros. This occurs because the canonical form of
1933 // "fcmp uno x,x" is "fcmp uno x, 0".
1934 if (isa<ConstantAggregateZero>(LHS->getOperand(1)) &&
1935 isa<ConstantAggregateZero>(RHS->getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00001936 return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0));
Craig Topper9d4171a2012-12-20 07:09:41 +00001937
Craig Topperf40110f2014-04-25 05:29:35 +00001938 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001939 }
Craig Topper9d4171a2012-12-20 07:09:41 +00001940
Craig Topperf40110f2014-04-25 05:29:35 +00001941 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001942}
1943
Sanjay Patel18549272015-09-08 18:24:36 +00001944/// This helper function folds:
Chris Lattner0a8191e2010-01-05 07:50:36 +00001945///
1946/// ((A | B) & C1) | (B & C2)
1947///
1948/// into:
Craig Topper9d4171a2012-12-20 07:09:41 +00001949///
Chris Lattner0a8191e2010-01-05 07:50:36 +00001950/// (A & C1) | B
1951///
1952/// when the XOR of the two constants is "all ones" (-1).
1953Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op,
1954 Value *A, Value *B, Value *C) {
1955 ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
Craig Topperf40110f2014-04-25 05:29:35 +00001956 if (!CI1) return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001957
Craig Topperf40110f2014-04-25 05:29:35 +00001958 Value *V1 = nullptr;
1959 ConstantInt *CI2 = nullptr;
1960 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001961
1962 APInt Xor = CI1->getValue() ^ CI2->getValue();
Craig Topperf40110f2014-04-25 05:29:35 +00001963 if (!Xor.isAllOnesValue()) return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001964
1965 if (V1 == A || V1 == B) {
1966 Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1);
1967 return BinaryOperator::CreateOr(NewOp, V1);
1968 }
1969
Craig Topperf40110f2014-04-25 05:29:35 +00001970 return nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00001971}
1972
David Majnemer5d1aeba2014-08-21 05:14:48 +00001973/// \brief This helper function folds:
1974///
1975/// ((A | B) & C1) ^ (B & C2)
1976///
1977/// into:
1978///
1979/// (A & C1) ^ B
1980///
1981/// when the XOR of the two constants is "all ones" (-1).
1982Instruction *InstCombiner::FoldXorWithConstants(BinaryOperator &I, Value *Op,
1983 Value *A, Value *B, Value *C) {
1984 ConstantInt *CI1 = dyn_cast<ConstantInt>(C);
1985 if (!CI1)
1986 return nullptr;
1987
1988 Value *V1 = nullptr;
1989 ConstantInt *CI2 = nullptr;
1990 if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2))))
1991 return nullptr;
1992
1993 APInt Xor = CI1->getValue() ^ CI2->getValue();
1994 if (!Xor.isAllOnesValue())
1995 return nullptr;
1996
1997 if (V1 == A || V1 == B) {
1998 Value *NewOp = Builder->CreateAnd(V1 == A ? B : A, CI1);
1999 return BinaryOperator::CreateXor(NewOp, V1);
2000 }
2001
2002 return nullptr;
2003}
2004
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002005// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
2006// here. We should standardize that construct where it is needed or choose some
2007// other way to ensure that commutated variants of patterns are not missed.
Chris Lattner0a8191e2010-01-05 07:50:36 +00002008Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00002009 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002010 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2011
Serge Pavlov9ef66a82014-05-11 08:46:12 +00002012 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002013 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +00002014
Daniel Jasperaec2fa32016-12-19 08:22:17 +00002015 if (Value *V = SimplifyOrInst(Op0, Op1, DL, &TLI, &DT, &AC))
Sanjay Patel4b198802016-02-01 22:23:39 +00002016 return replaceInstUsesWith(I, V);
Bill Wendlingaf13d822010-03-03 00:35:56 +00002017
Duncan Sandsfbb9ac32010-12-22 13:36:08 +00002018 // (A&B)|(A&C) -> A&(B|C) etc
2019 if (Value *V = SimplifyUsingDistributiveLaws(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002020 return replaceInstUsesWith(I, V);
Duncan Sandsadc7771f2010-11-23 14:23:47 +00002021
Craig Topper9d4171a2012-12-20 07:09:41 +00002022 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00002023 // purpose is to compute bits we don't care about.
2024 if (SimplifyDemandedInstructionBits(I))
2025 return &I;
2026
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00002027 if (Value *V = SimplifyBSwap(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002028 return replaceInstUsesWith(I, V);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00002029
Chris Lattner0a8191e2010-01-05 07:50:36 +00002030 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
Craig Topperf40110f2014-04-25 05:29:35 +00002031 ConstantInt *C1 = nullptr; Value *X = nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002032 // (X & C1) | C2 --> (X | C2) & (C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002033 // iff (C1 & C2) == 0.
Chris Lattner0a8191e2010-01-05 07:50:36 +00002034 if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) &&
Bill Wendlingaf13d822010-03-03 00:35:56 +00002035 (RHS->getValue() & C1->getValue()) != 0 &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002036 Op0->hasOneUse()) {
2037 Value *Or = Builder->CreateOr(X, RHS);
2038 Or->takeName(Op0);
Craig Topper9d4171a2012-12-20 07:09:41 +00002039 return BinaryOperator::CreateAnd(Or,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002040 Builder->getInt(RHS->getValue() | C1->getValue()));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002041 }
2042
2043 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
2044 if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) &&
2045 Op0->hasOneUse()) {
2046 Value *Or = Builder->CreateOr(X, RHS);
2047 Or->takeName(Op0);
2048 return BinaryOperator::CreateXor(Or,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002049 Builder->getInt(C1->getValue() & ~RHS->getValue()));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002050 }
2051
Sanjay Pateldb0938f2017-01-10 23:49:07 +00002052 if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
2053 return FoldedLogic;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002054 }
2055
Chad Rosiere5819e22016-05-26 14:58:51 +00002056 // Given an OR instruction, check to see if this is a bswap.
2057 if (Instruction *BSwap = MatchBSwap(I))
2058 return BSwap;
2059
Craig Topperf40110f2014-04-25 05:29:35 +00002060 Value *A = nullptr, *B = nullptr;
2061 ConstantInt *C1 = nullptr, *C2 = nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002062
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002063 // (X^C)|Y -> (X|Y)^C iff Y&C == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00002064 if (Op0->hasOneUse() &&
2065 match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
Hal Finkel60db0582014-09-07 18:57:58 +00002066 MaskedValueIsZero(Op1, C1->getValue(), 0, &I)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002067 Value *NOr = Builder->CreateOr(A, Op1);
2068 NOr->takeName(Op0);
2069 return BinaryOperator::CreateXor(NOr, C1);
2070 }
2071
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002072 // Y|(X^C) -> (X|Y)^C iff Y&C == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00002073 if (Op1->hasOneUse() &&
2074 match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
Hal Finkel60db0582014-09-07 18:57:58 +00002075 MaskedValueIsZero(Op0, C1->getValue(), 0, &I)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002076 Value *NOr = Builder->CreateOr(A, Op0);
2077 NOr->takeName(Op0);
2078 return BinaryOperator::CreateXor(NOr, C1);
2079 }
2080
Suyog Sardad64faf62014-07-22 18:09:41 +00002081 // ((~A & B) | A) -> (A | B)
2082 if (match(Op0, m_And(m_Not(m_Value(A)), m_Value(B))) &&
2083 match(Op1, m_Specific(A)))
2084 return BinaryOperator::CreateOr(A, B);
2085
2086 // ((A & B) | ~A) -> (~A | B)
2087 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
2088 match(Op1, m_Not(m_Specific(A))))
2089 return BinaryOperator::CreateOr(Builder->CreateNot(A), B);
2090
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002091 // (A & ~B) | (A ^ B) -> (A ^ B)
2092 // (~B & A) | (A ^ B) -> (A ^ B)
2093 if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
Suyog Sarda52324c82014-08-01 04:50:31 +00002094 match(Op1, m_Xor(m_Specific(A), m_Specific(B))))
2095 return BinaryOperator::CreateXor(A, B);
2096
Sanjay Patel5a443ac2016-12-19 18:35:37 +00002097 // Commute the 'or' operands.
2098 // (A ^ B) | (A & ~B) -> (A ^ B)
2099 // (A ^ B) | (~B & A) -> (A ^ B)
2100 if (match(Op1, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2101 match(Op0, m_Xor(m_Specific(A), m_Specific(B))))
Suyog Sarda52324c82014-08-01 04:50:31 +00002102 return BinaryOperator::CreateXor(A, B);
2103
Chris Lattner0a8191e2010-01-05 07:50:36 +00002104 // (A & C)|(B & D)
Craig Topperf40110f2014-04-25 05:29:35 +00002105 Value *C = nullptr, *D = nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002106 if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
2107 match(Op1, m_And(m_Value(B), m_Value(D)))) {
Craig Topperf40110f2014-04-25 05:29:35 +00002108 Value *V1 = nullptr, *V2 = nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002109 C1 = dyn_cast<ConstantInt>(C);
2110 C2 = dyn_cast<ConstantInt>(D);
2111 if (C1 && C2) { // (A & C1)|(B & C2)
Chris Lattner0a8191e2010-01-05 07:50:36 +00002112 if ((C1->getValue() & C2->getValue()) == 0) {
Chris Lattner95188692010-01-11 06:55:24 +00002113 // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002114 // iff (C1&C2) == 0 and (N&~C1) == 0
Chris Lattner0a8191e2010-01-05 07:50:36 +00002115 if (match(A, m_Or(m_Value(V1), m_Value(V2))) &&
Hal Finkel60db0582014-09-07 18:57:58 +00002116 ((V1 == B &&
2117 MaskedValueIsZero(V2, ~C1->getValue(), 0, &I)) || // (V|N)
2118 (V2 == B &&
2119 MaskedValueIsZero(V1, ~C1->getValue(), 0, &I)))) // (N|V)
Chris Lattner0a8191e2010-01-05 07:50:36 +00002120 return BinaryOperator::CreateAnd(A,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002121 Builder->getInt(C1->getValue()|C2->getValue()));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002122 // Or commutes, try both ways.
2123 if (match(B, m_Or(m_Value(V1), m_Value(V2))) &&
Hal Finkel60db0582014-09-07 18:57:58 +00002124 ((V1 == A &&
2125 MaskedValueIsZero(V2, ~C2->getValue(), 0, &I)) || // (V|N)
2126 (V2 == A &&
2127 MaskedValueIsZero(V1, ~C2->getValue(), 0, &I)))) // (N|V)
Chris Lattner0a8191e2010-01-05 07:50:36 +00002128 return BinaryOperator::CreateAnd(B,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002129 Builder->getInt(C1->getValue()|C2->getValue()));
Craig Topper9d4171a2012-12-20 07:09:41 +00002130
Chris Lattner95188692010-01-11 06:55:24 +00002131 // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002132 // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
Craig Topperf40110f2014-04-25 05:29:35 +00002133 ConstantInt *C3 = nullptr, *C4 = nullptr;
Chris Lattner95188692010-01-11 06:55:24 +00002134 if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
2135 (C3->getValue() & ~C1->getValue()) == 0 &&
2136 match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) &&
2137 (C4->getValue() & ~C2->getValue()) == 0) {
2138 V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield");
2139 return BinaryOperator::CreateAnd(V2,
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002140 Builder->getInt(C1->getValue()|C2->getValue()));
Chris Lattner95188692010-01-11 06:55:24 +00002141 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002142 }
2143 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002144
Sanjay Patelf4a08ed2016-07-08 20:53:29 +00002145 // Don't try to form a select if it's unlikely that we'll get rid of at
2146 // least one of the operands. A select is generally more expensive than the
2147 // 'or' that it is replacing.
2148 if (Op0->hasOneUse() || Op1->hasOneUse()) {
2149 // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants.
2150 if (Value *V = matchSelectFromAndOr(A, C, B, D, *Builder))
2151 return replaceInstUsesWith(I, V);
2152 if (Value *V = matchSelectFromAndOr(A, C, D, B, *Builder))
2153 return replaceInstUsesWith(I, V);
2154 if (Value *V = matchSelectFromAndOr(C, A, B, D, *Builder))
2155 return replaceInstUsesWith(I, V);
2156 if (Value *V = matchSelectFromAndOr(C, A, D, B, *Builder))
2157 return replaceInstUsesWith(I, V);
2158 if (Value *V = matchSelectFromAndOr(B, D, A, C, *Builder))
2159 return replaceInstUsesWith(I, V);
2160 if (Value *V = matchSelectFromAndOr(B, D, C, A, *Builder))
2161 return replaceInstUsesWith(I, V);
2162 if (Value *V = matchSelectFromAndOr(D, B, A, C, *Builder))
2163 return replaceInstUsesWith(I, V);
2164 if (Value *V = matchSelectFromAndOr(D, B, C, A, *Builder))
2165 return replaceInstUsesWith(I, V);
2166 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002167
2168 // ((A&~B)|(~A&B)) -> A^B
2169 if ((match(C, m_Not(m_Specific(D))) &&
2170 match(B, m_Not(m_Specific(A)))))
2171 return BinaryOperator::CreateXor(A, D);
2172 // ((~B&A)|(~A&B)) -> A^B
2173 if ((match(A, m_Not(m_Specific(D))) &&
2174 match(B, m_Not(m_Specific(C)))))
2175 return BinaryOperator::CreateXor(C, D);
2176 // ((A&~B)|(B&~A)) -> A^B
2177 if ((match(C, m_Not(m_Specific(B))) &&
2178 match(D, m_Not(m_Specific(A)))))
2179 return BinaryOperator::CreateXor(A, B);
2180 // ((~B&A)|(B&~A)) -> A^B
2181 if ((match(A, m_Not(m_Specific(B))) &&
2182 match(D, m_Not(m_Specific(C)))))
2183 return BinaryOperator::CreateXor(C, B);
Benjamin Kramer11743242010-07-12 13:34:22 +00002184
2185 // ((A|B)&1)|(B&-2) -> (A&1) | B
2186 if (match(A, m_Or(m_Value(V1), m_Specific(B))) ||
2187 match(A, m_Or(m_Specific(B), m_Value(V1)))) {
2188 Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C);
2189 if (Ret) return Ret;
2190 }
2191 // (B&-2)|((A|B)&1) -> (A&1) | B
2192 if (match(B, m_Or(m_Specific(A), m_Value(V1))) ||
2193 match(B, m_Or(m_Value(V1), m_Specific(A)))) {
2194 Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D);
2195 if (Ret) return Ret;
2196 }
David Majnemer5d1aeba2014-08-21 05:14:48 +00002197 // ((A^B)&1)|(B&-2) -> (A&1) ^ B
2198 if (match(A, m_Xor(m_Value(V1), m_Specific(B))) ||
2199 match(A, m_Xor(m_Specific(B), m_Value(V1)))) {
2200 Instruction *Ret = FoldXorWithConstants(I, Op1, V1, B, C);
2201 if (Ret) return Ret;
2202 }
2203 // (B&-2)|((A^B)&1) -> (A&1) ^ B
2204 if (match(B, m_Xor(m_Specific(A), m_Value(V1))) ||
2205 match(B, m_Xor(m_Value(V1), m_Specific(A)))) {
2206 Instruction *Ret = FoldXorWithConstants(I, Op0, A, V1, D);
2207 if (Ret) return Ret;
2208 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002209 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002210
David Majnemer42af3602014-07-30 21:26:37 +00002211 // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C
2212 if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2213 if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2214 if (Op1->hasOneUse() || cast<BinaryOperator>(Op1)->hasOneUse())
2215 return BinaryOperator::CreateOr(Op0, C);
2216
2217 // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C
2218 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2219 if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2220 if (Op0->hasOneUse() || cast<BinaryOperator>(Op0)->hasOneUse())
2221 return BinaryOperator::CreateOr(Op1, C);
2222
David Majnemerf1eda232014-08-14 06:41:38 +00002223 // ((B | C) & A) | B -> B | (A & C)
2224 if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
2225 return BinaryOperator::CreateOr(Op1, Builder->CreateAnd(A, C));
2226
Sanjay Patelb54e62f2015-09-08 20:14:13 +00002227 if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
2228 return DeMorgan;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002229
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002230 // Canonicalize xor to the RHS.
Eli Friedmane06535b2012-03-16 00:52:42 +00002231 bool SwappedForXor = false;
2232 if (match(Op0, m_Xor(m_Value(), m_Value()))) {
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002233 std::swap(Op0, Op1);
Eli Friedmane06535b2012-03-16 00:52:42 +00002234 SwappedForXor = true;
2235 }
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002236
2237 // A | ( A ^ B) -> A | B
2238 // A | (~A ^ B) -> A | ~B
Chad Rosier7813dce2012-04-26 23:29:14 +00002239 // (A & B) | (A ^ B)
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002240 if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2241 if (Op0 == A || Op0 == B)
2242 return BinaryOperator::CreateOr(A, B);
2243
Chad Rosier7813dce2012-04-26 23:29:14 +00002244 if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
2245 match(Op0, m_And(m_Specific(B), m_Specific(A))))
2246 return BinaryOperator::CreateOr(A, B);
2247
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002248 if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
2249 Value *Not = Builder->CreateNot(B, B->getName()+".not");
2250 return BinaryOperator::CreateOr(Not, Op0);
2251 }
2252 if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
2253 Value *Not = Builder->CreateNot(A, A->getName()+".not");
2254 return BinaryOperator::CreateOr(Not, Op0);
2255 }
2256 }
2257
2258 // A | ~(A | B) -> A | ~B
2259 // A | ~(A ^ B) -> A | ~B
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002260 if (match(Op1, m_Not(m_Value(A))))
2261 if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
Benjamin Kramer5b7a4e02011-02-20 15:20:01 +00002262 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2263 Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2264 B->getOpcode() == Instruction::Xor)) {
2265 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2266 B->getOperand(0);
2267 Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not");
2268 return BinaryOperator::CreateOr(Not, Op0);
2269 }
Benjamin Kramerd5d7f372011-02-20 13:23:43 +00002270
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002271 // (A & B) | (~A ^ B) -> (~A ^ B)
2272 // (A & B) | (B ^ ~A) -> (~A ^ B)
2273 // (B & A) | (~A ^ B) -> (~A ^ B)
2274 // (B & A) | (B ^ ~A) -> (~A ^ B)
2275 // The match order is important: match the xor first because the 'not'
2276 // operation defines 'A'. We do not need to match the xor as Op0 because the
2277 // xor was canonicalized to Op1 above.
2278 if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2279 match(Op0, m_c_And(m_Specific(A), m_Specific(B))))
Suyog Sarda16d64652014-08-01 04:41:43 +00002280 return BinaryOperator::CreateXor(Builder->CreateNot(A), B);
2281
Eli Friedmane06535b2012-03-16 00:52:42 +00002282 if (SwappedForXor)
2283 std::swap(Op0, Op1);
2284
David Majnemer3d6f80b2014-11-28 19:58:29 +00002285 {
2286 ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2287 ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2288 if (LHS && RHS)
Hal Finkel60db0582014-09-07 18:57:58 +00002289 if (Value *Res = FoldOrOfICmps(LHS, RHS, &I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002290 return replaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00002291
David Majnemer3d6f80b2014-11-28 19:58:29 +00002292 // TODO: Make this recursive; it's a little tricky because an arbitrary
2293 // number of 'or' instructions might have to be created.
2294 Value *X, *Y;
2295 if (LHS && match(Op1, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2296 if (auto *Cmp = dyn_cast<ICmpInst>(X))
2297 if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002298 return replaceInstUsesWith(I, Builder->CreateOr(Res, Y));
David Majnemer3d6f80b2014-11-28 19:58:29 +00002299 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2300 if (Value *Res = FoldOrOfICmps(LHS, Cmp, &I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002301 return replaceInstUsesWith(I, Builder->CreateOr(Res, X));
David Majnemer3d6f80b2014-11-28 19:58:29 +00002302 }
2303 if (RHS && match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2304 if (auto *Cmp = dyn_cast<ICmpInst>(X))
2305 if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002306 return replaceInstUsesWith(I, Builder->CreateOr(Res, Y));
David Majnemer3d6f80b2014-11-28 19:58:29 +00002307 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2308 if (Value *Res = FoldOrOfICmps(Cmp, RHS, &I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002309 return replaceInstUsesWith(I, Builder->CreateOr(Res, X));
David Majnemer3d6f80b2014-11-28 19:58:29 +00002310 }
2311 }
2312
Chris Lattner4e8137d2010-02-11 06:26:33 +00002313 // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y)
2314 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2315 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
Chris Lattner067459c2010-03-05 08:46:26 +00002316 if (Value *Res = FoldOrOfFCmps(LHS, RHS))
Sanjay Patel4b198802016-02-01 22:23:39 +00002317 return replaceInstUsesWith(I, Res);
Craig Topper9d4171a2012-12-20 07:09:41 +00002318
Sanjay Patel75b4ae22016-02-23 23:56:23 +00002319 if (Instruction *CastedOr = foldCastedBitwiseLogic(I))
2320 return CastedOr;
Eli Friedman23956262011-04-14 22:41:27 +00002321
Sanjay Patelcbfca9e2016-07-08 17:01:15 +00002322 // 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 +00002323 if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) &&
Sanjay Patelcbfca9e2016-07-08 17:01:15 +00002324 A->getType()->getScalarType()->isIntegerTy(1))
Eli Friedman23956262011-04-14 22:41:27 +00002325 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1);
Sanjay Patel1b6b8242016-07-08 17:26:47 +00002326 if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) &&
Sanjay Patelcbfca9e2016-07-08 17:01:15 +00002327 A->getType()->getScalarType()->isIntegerTy(1))
Eli Friedman23956262011-04-14 22:41:27 +00002328 return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0);
2329
Owen Andersonc237a842010-09-13 17:59:27 +00002330 // Note: If we've gotten to the point of visiting the outer OR, then the
2331 // inner one couldn't be simplified. If it was a constant, then it won't
2332 // be simplified by a later pass either, so we try swapping the inner/outer
2333 // ORs in the hopes that we'll be able to simplify it this way.
2334 // (X|C) | V --> (X|V) | C
2335 if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) &&
2336 match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) {
2337 Value *Inner = Builder->CreateOr(A, Op1);
2338 Inner->takeName(Op0);
2339 return BinaryOperator::CreateOr(Inner, C1);
2340 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002341
Bill Wendling23242092013-02-16 23:41:36 +00002342 // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2343 // Since this OR statement hasn't been optimized further yet, we hope
2344 // that this transformation will allow the new ORs to be optimized.
2345 {
Craig Topperf40110f2014-04-25 05:29:35 +00002346 Value *X = nullptr, *Y = nullptr;
Bill Wendling23242092013-02-16 23:41:36 +00002347 if (Op0->hasOneUse() && Op1->hasOneUse() &&
2348 match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2349 match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2350 Value *orTrue = Builder->CreateOr(A, C);
2351 Value *orFalse = Builder->CreateOr(B, D);
2352 return SelectInst::Create(X, orTrue, orFalse);
2353 }
2354 }
2355
Craig Topperf40110f2014-04-25 05:29:35 +00002356 return Changed ? &I : nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002357}
2358
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002359// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
2360// here. We should standardize that construct where it is needed or choose some
2361// other way to ensure that commutated variants of patterns are not missed.
Chris Lattner0a8191e2010-01-05 07:50:36 +00002362Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Duncan Sands641baf12010-11-13 15:10:37 +00002363 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002364 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2365
Serge Pavlov9ef66a82014-05-11 08:46:12 +00002366 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002367 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +00002368
Daniel Jasperaec2fa32016-12-19 08:22:17 +00002369 if (Value *V = SimplifyXorInst(Op0, Op1, DL, &TLI, &DT, &AC))
Sanjay Patel4b198802016-02-01 22:23:39 +00002370 return replaceInstUsesWith(I, V);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002371
Duncan Sandsfbb9ac32010-12-22 13:36:08 +00002372 // (A&B)^(A&C) -> A&(B^C) etc
2373 if (Value *V = SimplifyUsingDistributiveLaws(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002374 return replaceInstUsesWith(I, V);
Duncan Sandsadc7771f2010-11-23 14:23:47 +00002375
Craig Topper9d4171a2012-12-20 07:09:41 +00002376 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattner0a8191e2010-01-05 07:50:36 +00002377 // purpose is to compute bits we don't care about.
2378 if (SimplifyDemandedInstructionBits(I))
2379 return &I;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002380
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00002381 if (Value *V = SimplifyBSwap(I))
Sanjay Patel4b198802016-02-01 22:23:39 +00002382 return replaceInstUsesWith(I, V);
Simon Pilgrimbe24ab32014-12-04 09:44:01 +00002383
Chris Lattner0a8191e2010-01-05 07:50:36 +00002384 // Is this a ~ operation?
2385 if (Value *NotOp = dyn_castNotVal(&I)) {
2386 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) {
Craig Topper9d4171a2012-12-20 07:09:41 +00002387 if (Op0I->getOpcode() == Instruction::And ||
Chris Lattner0a8191e2010-01-05 07:50:36 +00002388 Op0I->getOpcode() == Instruction::Or) {
2389 // ~(~X & Y) --> (X | ~Y) - De Morgan's Law
2390 // ~(~X | Y) === (X & ~Y) - De Morgan's Law
2391 if (dyn_castNotVal(Op0I->getOperand(1)))
2392 Op0I->swapOperands();
2393 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
2394 Value *NotY =
2395 Builder->CreateNot(Op0I->getOperand(1),
2396 Op0I->getOperand(1)->getName()+".not");
2397 if (Op0I->getOpcode() == Instruction::And)
2398 return BinaryOperator::CreateOr(Op0NotVal, NotY);
2399 return BinaryOperator::CreateAnd(Op0NotVal, NotY);
2400 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002401
Chris Lattner0a8191e2010-01-05 07:50:36 +00002402 // ~(X & Y) --> (~X | ~Y) - De Morgan's Law
2403 // ~(X | Y) === (~X & ~Y) - De Morgan's Law
Sanjoy Das82ea3d42015-02-24 00:08:41 +00002404 if (IsFreeToInvert(Op0I->getOperand(0),
2405 Op0I->getOperand(0)->hasOneUse()) &&
2406 IsFreeToInvert(Op0I->getOperand(1),
2407 Op0I->getOperand(1)->hasOneUse())) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002408 Value *NotX =
2409 Builder->CreateNot(Op0I->getOperand(0), "notlhs");
2410 Value *NotY =
2411 Builder->CreateNot(Op0I->getOperand(1), "notrhs");
2412 if (Op0I->getOpcode() == Instruction::And)
2413 return BinaryOperator::CreateOr(NotX, NotY);
2414 return BinaryOperator::CreateAnd(NotX, NotY);
2415 }
Chris Lattner18f49ce2010-01-19 18:16:19 +00002416
2417 } else if (Op0I->getOpcode() == Instruction::AShr) {
2418 // ~(~X >>s Y) --> (X >>s Y)
2419 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0)))
2420 return BinaryOperator::CreateAShr(Op0NotVal, Op0I->getOperand(1));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002421 }
2422 }
2423 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002424
Benjamin Kramer443c7962015-02-12 20:26:46 +00002425 if (Constant *RHS = dyn_cast<Constant>(Op1)) {
2426 if (RHS->isAllOnesValue() && Op0->hasOneUse())
Chris Lattner0a8191e2010-01-05 07:50:36 +00002427 // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B
Dan Gohman0a8175d2010-04-09 14:53:59 +00002428 if (CmpInst *CI = dyn_cast<CmpInst>(Op0))
2429 return CmpInst::Create(CI->getOpcode(),
2430 CI->getInversePredicate(),
2431 CI->getOperand(0), CI->getOperand(1));
Benjamin Kramer443c7962015-02-12 20:26:46 +00002432 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002433
Benjamin Kramer443c7962015-02-12 20:26:46 +00002434 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002435 // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp).
2436 if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) {
2437 if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) {
2438 if (CI->hasOneUse() && Op0C->hasOneUse()) {
2439 Instruction::CastOps Opcode = Op0C->getOpcode();
2440 if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) &&
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002441 (RHS == ConstantExpr::getCast(Opcode, Builder->getTrue(),
Chris Lattner0a8191e2010-01-05 07:50:36 +00002442 Op0C->getDestTy()))) {
2443 CI->setPredicate(CI->getInversePredicate());
2444 return CastInst::Create(Opcode, CI, Op0C->getType());
2445 }
2446 }
2447 }
2448 }
2449
2450 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
2451 // ~(c-X) == X-c-1 == X+(-c-1)
2452 if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue())
2453 if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) {
2454 Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
2455 Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
2456 ConstantInt::get(I.getType(), 1));
2457 return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
2458 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002459
Chris Lattner0a8191e2010-01-05 07:50:36 +00002460 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
2461 if (Op0I->getOpcode() == Instruction::Add) {
2462 // ~(X-c) --> (-c-1)-X
2463 if (RHS->isAllOnesValue()) {
2464 Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
2465 return BinaryOperator::CreateSub(
2466 ConstantExpr::getSub(NegOp0CI,
2467 ConstantInt::get(I.getType(), 1)),
2468 Op0I->getOperand(0));
2469 } else if (RHS->getValue().isSignBit()) {
2470 // (X + C) ^ signbit -> (X + C + signbit)
Jakub Staszak461d1fe2013-06-06 00:37:23 +00002471 Constant *C = Builder->getInt(RHS->getValue() + Op0CI->getValue());
Chris Lattner0a8191e2010-01-05 07:50:36 +00002472 return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
2473
2474 }
2475 } else if (Op0I->getOpcode() == Instruction::Or) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002476 // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0
Hal Finkel60db0582014-09-07 18:57:58 +00002477 if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue(),
2478 0, &I)) {
Chris Lattner0a8191e2010-01-05 07:50:36 +00002479 Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHS);
2480 // Anything in both C1 and C2 is known to be zero, remove it from
2481 // NewRHS.
2482 Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHS);
Craig Topper9d4171a2012-12-20 07:09:41 +00002483 NewRHS = ConstantExpr::getAnd(NewRHS,
Chris Lattner0a8191e2010-01-05 07:50:36 +00002484 ConstantExpr::getNot(CommonBits));
2485 Worklist.Add(Op0I);
2486 I.setOperand(0, Op0I->getOperand(0));
2487 I.setOperand(1, NewRHS);
2488 return &I;
2489 }
Shuxin Yang6ea79e82012-11-26 21:44:25 +00002490 } else if (Op0I->getOpcode() == Instruction::LShr) {
2491 // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
2492 // E1 = "X ^ C1"
Craig Topper9d4171a2012-12-20 07:09:41 +00002493 BinaryOperator *E1;
Shuxin Yang6ea79e82012-11-26 21:44:25 +00002494 ConstantInt *C1;
2495 if (Op0I->hasOneUse() &&
2496 (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) &&
2497 E1->getOpcode() == Instruction::Xor &&
2498 (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) {
2499 // fold (C1 >> C2) ^ C3
2500 ConstantInt *C2 = Op0CI, *C3 = RHS;
2501 APInt FoldConst = C1->getValue().lshr(C2->getValue());
2502 FoldConst ^= C3->getValue();
2503 // Prepare the two operands.
2504 Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2);
2505 Opnd0->takeName(Op0I);
2506 cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc());
2507 Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst);
2508
2509 return BinaryOperator::CreateXor(Opnd0, FoldVal);
2510 }
Chris Lattner0a8191e2010-01-05 07:50:36 +00002511 }
2512 }
2513 }
2514
Sanjay Pateldb0938f2017-01-10 23:49:07 +00002515 if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
2516 return FoldedLogic;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002517 }
2518
Chris Lattner0a8191e2010-01-05 07:50:36 +00002519 BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1);
2520 if (Op1I) {
2521 Value *A, *B;
2522 if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2523 if (A == Op0) { // B^(B|A) == (A|B)^B
2524 Op1I->swapOperands();
2525 I.swapOperands();
2526 std::swap(Op0, Op1);
2527 } else if (B == Op0) { // B^(A|B) == (A|B)^B
2528 I.swapOperands(); // Simplified below.
2529 std::swap(Op0, Op1);
2530 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002531 } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002532 Op1I->hasOneUse()){
2533 if (A == Op0) { // A^(A&B) -> A^(B&A)
2534 Op1I->swapOperands();
2535 std::swap(A, B);
2536 }
2537 if (B == Op0) { // A^(B&A) -> (B&A)^A
2538 I.swapOperands(); // Simplified below.
2539 std::swap(Op0, Op1);
2540 }
2541 }
2542 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002543
Chris Lattner0a8191e2010-01-05 07:50:36 +00002544 BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0);
2545 if (Op0I) {
2546 Value *A, *B;
2547 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2548 Op0I->hasOneUse()) {
2549 if (A == Op1) // (B|A)^B == (A|B)^B
2550 std::swap(A, B);
2551 if (B == Op1) // (A|B)^B == A & ~B
Benjamin Kramer547b6c52011-09-27 20:39:19 +00002552 return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1));
Craig Topper9d4171a2012-12-20 07:09:41 +00002553 } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
Chris Lattner0a8191e2010-01-05 07:50:36 +00002554 Op0I->hasOneUse()){
2555 if (A == Op1) // (A&B)^A -> (B&A)^A
2556 std::swap(A, B);
2557 if (B == Op1 && // (B&A)^A == ~B & A
2558 !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C
Benjamin Kramer547b6c52011-09-27 20:39:19 +00002559 return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002560 }
2561 }
2562 }
Craig Topper9d4171a2012-12-20 07:09:41 +00002563
Chris Lattner0a8191e2010-01-05 07:50:36 +00002564 if (Op0I && Op1I) {
2565 Value *A, *B, *C, *D;
2566 // (A & B)^(A | B) -> A ^ B
2567 if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2568 match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
Craig Topper9d4171a2012-12-20 07:09:41 +00002569 if ((A == C && B == D) || (A == D && B == C))
Chris Lattner0a8191e2010-01-05 07:50:36 +00002570 return BinaryOperator::CreateXor(A, B);
2571 }
2572 // (A | B)^(A & B) -> A ^ B
2573 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
2574 match(Op1I, m_And(m_Value(C), m_Value(D)))) {
Craig Topper9d4171a2012-12-20 07:09:41 +00002575 if ((A == C && B == D) || (A == D && B == C))
Chris Lattner0a8191e2010-01-05 07:50:36 +00002576 return BinaryOperator::CreateXor(A, B);
2577 }
David Majnemer698dca02014-08-14 06:46:25 +00002578 // (A | ~B) ^ (~A | B) -> A ^ B
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002579 // (~B | A) ^ (~A | B) -> A ^ B
2580 if (match(Op0I, m_c_Or(m_Value(A), m_Not(m_Value(B)))) &&
2581 match(Op1I, m_Or(m_Not(m_Specific(A)), m_Specific(B))))
David Majnemer698dca02014-08-14 06:46:25 +00002582 return BinaryOperator::CreateXor(A, B);
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002583
David Majnemer698dca02014-08-14 06:46:25 +00002584 // (~A | B) ^ (A | ~B) -> A ^ B
2585 if (match(Op0I, m_Or(m_Not(m_Value(A)), m_Value(B))) &&
2586 match(Op1I, m_Or(m_Specific(A), m_Not(m_Specific(B))))) {
2587 return BinaryOperator::CreateXor(A, B);
2588 }
Mayur Pandey960507b2014-08-19 08:19:19 +00002589 // (A & ~B) ^ (~A & B) -> A ^ B
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002590 // (~B & A) ^ (~A & B) -> A ^ B
2591 if (match(Op0I, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
2592 match(Op1I, m_And(m_Not(m_Specific(A)), m_Specific(B))))
Mayur Pandey960507b2014-08-19 08:19:19 +00002593 return BinaryOperator::CreateXor(A, B);
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002594
Mayur Pandey960507b2014-08-19 08:19:19 +00002595 // (~A & B) ^ (A & ~B) -> A ^ B
2596 if (match(Op0I, m_And(m_Not(m_Value(A)), m_Value(B))) &&
2597 match(Op1I, m_And(m_Specific(A), m_Not(m_Specific(B))))) {
2598 return BinaryOperator::CreateXor(A, B);
2599 }
David Majnemer6fe6ea72014-09-05 06:09:24 +00002600 // (A ^ C)^(A | B) -> ((~A) & B) ^ C
2601 if (match(Op0I, m_Xor(m_Value(D), m_Value(C))) &&
2602 match(Op1I, m_Or(m_Value(A), m_Value(B)))) {
2603 if (D == A)
2604 return BinaryOperator::CreateXor(
2605 Builder->CreateAnd(Builder->CreateNot(A), B), C);
2606 if (D == B)
2607 return BinaryOperator::CreateXor(
2608 Builder->CreateAnd(Builder->CreateNot(B), A), C);
Karthik Bhata4a4db92014-08-13 05:13:14 +00002609 }
David Majnemer6fe6ea72014-09-05 06:09:24 +00002610 // (A | B)^(A ^ C) -> ((~A) & B) ^ C
Karthik Bhata4a4db92014-08-13 05:13:14 +00002611 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
David Majnemer6fe6ea72014-09-05 06:09:24 +00002612 match(Op1I, m_Xor(m_Value(D), m_Value(C)))) {
2613 if (D == A)
2614 return BinaryOperator::CreateXor(
2615 Builder->CreateAnd(Builder->CreateNot(A), B), C);
2616 if (D == B)
2617 return BinaryOperator::CreateXor(
2618 Builder->CreateAnd(Builder->CreateNot(B), A), C);
Karthik Bhata4a4db92014-08-13 05:13:14 +00002619 }
Suyog Sardab60ec902014-07-22 18:30:54 +00002620 // (A & B) ^ (A ^ B) -> (A | B)
2621 if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
2622 match(Op1I, m_Xor(m_Specific(A), m_Specific(B))))
2623 return BinaryOperator::CreateOr(A, B);
2624 // (A ^ B) ^ (A & B) -> (A | B)
2625 if (match(Op0I, m_Xor(m_Value(A), m_Value(B))) &&
2626 match(Op1I, m_And(m_Specific(A), m_Specific(B))))
2627 return BinaryOperator::CreateOr(A, B);
Chris Lattner0a8191e2010-01-05 07:50:36 +00002628 }
Duncan Sandsadc7771f2010-11-23 14:23:47 +00002629
Sanjay Patel2b9d4b42016-12-18 18:49:48 +00002630 // (A & ~B) ^ ~A -> ~(A & B)
2631 // (~B & A) ^ ~A -> ~(A & B)
2632 Value *A, *B;
2633 if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
Suyog Sarda56c9a872014-08-01 05:07:20 +00002634 match(Op1, m_Not(m_Specific(A))))
2635 return BinaryOperator::CreateNot(Builder->CreateAnd(A, B));
2636
David Majnemerb0761a02016-12-21 19:21:59 +00002637 // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
Chris Lattner0a8191e2010-01-05 07:50:36 +00002638 if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
David Majnemerb0761a02016-12-21 19:21:59 +00002639 if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
Chris Lattner0a8191e2010-01-05 07:50:36 +00002640 if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
2641 if (LHS->getOperand(0) == RHS->getOperand(1) &&
2642 LHS->getOperand(1) == RHS->getOperand(0))
2643 LHS->swapOperands();
2644 if (LHS->getOperand(0) == RHS->getOperand(0) &&
2645 LHS->getOperand(1) == RHS->getOperand(1)) {
2646 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
2647 unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
2648 bool isSigned = LHS->isSigned() || RHS->isSigned();
Sanjay Patel4b198802016-02-01 22:23:39 +00002649 return replaceInstUsesWith(I,
Pete Cooperebf98c12011-12-17 01:20:32 +00002650 getNewICmpValue(isSigned, Code, Op0, Op1,
2651 Builder));
Chris Lattner0a8191e2010-01-05 07:50:36 +00002652 }
2653 }
2654
Sanjay Pateldbbaca02016-02-24 17:00:34 +00002655 if (Instruction *CastedXor = foldCastedBitwiseLogic(I))
2656 return CastedXor;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002657
Craig Topperf40110f2014-04-25 05:29:35 +00002658 return Changed ? &I : nullptr;
Chris Lattner0a8191e2010-01-05 07:50:36 +00002659}