1 //===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Nate Begeman and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11 // both before and after the DAG is legalized.
13 // FIXME: Missing folds
14 // sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15 // a sequence of multiplies, shifts, and adds. This should be controlled by
16 // some kind of hint from the target that int div is expensive.
17 // various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
19 // FIXME: select C, pow2, pow2 -> something smart
20 // FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
21 // FIXME: Dead stores -> nuke
22 // FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
23 // FIXME: mul (x, const) -> shifts + adds
24 // FIXME: undef values
25 // FIXME: make truncate see through SIGN_EXTEND and AND
26 // FIXME: divide by zero is currently left unfolded. do we want to turn this
28 // FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
30 //===----------------------------------------------------------------------===//
32 #define DEBUG_TYPE "dagcombine"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/CodeGen/SelectionDAG.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Target/TargetLowering.h"
44 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
51 // Worklist of all of the nodes that need to be simplified.
52 std::vector<SDNode*> WorkList;
54 /// AddUsersToWorkList - When an instruction is simplified, add all users of
55 /// the instruction to the work lists because they might get more simplified
58 void AddUsersToWorkList(SDNode *N) {
59 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
61 WorkList.push_back(*UI);
64 /// removeFromWorkList - remove all instances of N from the worklist.
66 void removeFromWorkList(SDNode *N) {
67 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
72 void AddToWorkList(SDNode *N) {
73 WorkList.push_back(N);
76 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
78 DEBUG(std::cerr << "\nReplacing "; N->dump();
79 std::cerr << "\nWith: "; To[0].Val->dump();
80 std::cerr << " and " << To.size()-1 << " other values\n");
81 std::vector<SDNode*> NowDead;
82 DAG.ReplaceAllUsesWith(N, To, &NowDead);
84 // Push the new nodes and any users onto the worklist
85 for (unsigned i = 0, e = To.size(); i != e; ++i) {
86 WorkList.push_back(To[i].Val);
87 AddUsersToWorkList(To[i].Val);
90 // Nodes can end up on the worklist more than once. Make sure we do
91 // not process a node that has been replaced.
92 removeFromWorkList(N);
93 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
94 removeFromWorkList(NowDead[i]);
96 // Finally, since the node is now dead, remove it from the graph.
98 return SDOperand(N, 0);
101 SDOperand CombineTo(SDNode *N, SDOperand Res) {
102 std::vector<SDOperand> To;
104 return CombineTo(N, To);
107 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
108 std::vector<SDOperand> To;
111 return CombineTo(N, To);
115 /// SimplifyDemandedBits - Check the specified integer node value to see if
116 /// it can be simplified or if things it uses can be simplified by bit
117 /// propagation. If so, return true.
118 bool SimplifyDemandedBits(SDOperand Op) {
119 TargetLowering::TargetLoweringOpt TLO(DAG);
120 uint64_t KnownZero, KnownOne;
121 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
122 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
126 WorkList.push_back(Op.Val);
128 // Replace the old value with the new one.
130 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
131 std::cerr << "\nWith: "; TLO.New.Val->dump());
133 std::vector<SDNode*> NowDead;
134 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
136 // Push the new node and any (possibly new) users onto the worklist.
137 WorkList.push_back(TLO.New.Val);
138 AddUsersToWorkList(TLO.New.Val);
140 // Nodes can end up on the worklist more than once. Make sure we do
141 // not process a node that has been replaced.
142 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
143 removeFromWorkList(NowDead[i]);
145 // Finally, if the node is now dead, remove it from the graph. The node
146 // may not be dead if the replacement process recursively simplified to
147 // something else needing this node.
148 if (TLO.Old.Val->use_empty()) {
149 removeFromWorkList(TLO.Old.Val);
150 DAG.DeleteNode(TLO.Old.Val);
155 /// visit - call the node-specific routine that knows how to fold each
156 /// particular type of node.
157 SDOperand visit(SDNode *N);
159 // Visitation implementation - Implement dag node combining for different
160 // node types. The semantics are as follows:
162 // SDOperand.Val == 0 - No change was made
163 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
164 // otherwise - N should be replaced by the returned Operand.
166 SDOperand visitTokenFactor(SDNode *N);
167 SDOperand visitADD(SDNode *N);
168 SDOperand visitSUB(SDNode *N);
169 SDOperand visitMUL(SDNode *N);
170 SDOperand visitSDIV(SDNode *N);
171 SDOperand visitUDIV(SDNode *N);
172 SDOperand visitSREM(SDNode *N);
173 SDOperand visitUREM(SDNode *N);
174 SDOperand visitMULHU(SDNode *N);
175 SDOperand visitMULHS(SDNode *N);
176 SDOperand visitAND(SDNode *N);
177 SDOperand visitOR(SDNode *N);
178 SDOperand visitXOR(SDNode *N);
179 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
180 SDOperand visitSHL(SDNode *N);
181 SDOperand visitSRA(SDNode *N);
182 SDOperand visitSRL(SDNode *N);
183 SDOperand visitCTLZ(SDNode *N);
184 SDOperand visitCTTZ(SDNode *N);
185 SDOperand visitCTPOP(SDNode *N);
186 SDOperand visitSELECT(SDNode *N);
187 SDOperand visitSELECT_CC(SDNode *N);
188 SDOperand visitSETCC(SDNode *N);
189 SDOperand visitSIGN_EXTEND(SDNode *N);
190 SDOperand visitZERO_EXTEND(SDNode *N);
191 SDOperand visitANY_EXTEND(SDNode *N);
192 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
193 SDOperand visitTRUNCATE(SDNode *N);
194 SDOperand visitBIT_CONVERT(SDNode *N);
195 SDOperand visitVBIT_CONVERT(SDNode *N);
196 SDOperand visitFADD(SDNode *N);
197 SDOperand visitFSUB(SDNode *N);
198 SDOperand visitFMUL(SDNode *N);
199 SDOperand visitFDIV(SDNode *N);
200 SDOperand visitFREM(SDNode *N);
201 SDOperand visitFCOPYSIGN(SDNode *N);
202 SDOperand visitSINT_TO_FP(SDNode *N);
203 SDOperand visitUINT_TO_FP(SDNode *N);
204 SDOperand visitFP_TO_SINT(SDNode *N);
205 SDOperand visitFP_TO_UINT(SDNode *N);
206 SDOperand visitFP_ROUND(SDNode *N);
207 SDOperand visitFP_ROUND_INREG(SDNode *N);
208 SDOperand visitFP_EXTEND(SDNode *N);
209 SDOperand visitFNEG(SDNode *N);
210 SDOperand visitFABS(SDNode *N);
211 SDOperand visitBRCOND(SDNode *N);
212 SDOperand visitBR_CC(SDNode *N);
213 SDOperand visitLOAD(SDNode *N);
214 SDOperand visitXEXTLOAD(SDNode *N);
215 SDOperand visitSTORE(SDNode *N);
216 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
217 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
218 SDOperand visitVBUILD_VECTOR(SDNode *N);
219 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
220 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
222 SDOperand XformToShuffleWithZero(SDNode *N);
223 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
225 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
226 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
227 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
228 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
229 SDOperand N3, ISD::CondCode CC);
230 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
231 ISD::CondCode Cond, bool foldBooleans = true);
232 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
233 SDOperand BuildSDIV(SDNode *N);
234 SDOperand BuildUDIV(SDNode *N);
236 DAGCombiner(SelectionDAG &D)
237 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
239 /// Run - runs the dag combiner on all nodes in the work list
240 void Run(bool RunningAfterLegalize);
244 //===----------------------------------------------------------------------===//
245 // TargetLowering::DAGCombinerInfo implementation
246 //===----------------------------------------------------------------------===//
248 void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
249 ((DAGCombiner*)DC)->AddToWorkList(N);
252 SDOperand TargetLowering::DAGCombinerInfo::
253 CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
254 return ((DAGCombiner*)DC)->CombineTo(N, To);
257 SDOperand TargetLowering::DAGCombinerInfo::
258 CombineTo(SDNode *N, SDOperand Res) {
259 return ((DAGCombiner*)DC)->CombineTo(N, Res);
263 SDOperand TargetLowering::DAGCombinerInfo::
264 CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
265 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
271 //===----------------------------------------------------------------------===//
275 int64_t m; // magic number
276 int64_t s; // shift amount
280 uint64_t m; // magic number
281 int64_t a; // add indicator
282 int64_t s; // shift amount
285 /// magic - calculate the magic numbers required to codegen an integer sdiv as
286 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
288 static ms magic32(int32_t d) {
290 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
291 const uint32_t two31 = 0x80000000U;
295 t = two31 + ((uint32_t)d >> 31);
296 anc = t - 1 - t%ad; // absolute value of nc
297 p = 31; // initialize p
298 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
299 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
300 q2 = two31/ad; // initialize q2 = 2p/abs(d)
301 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
304 q1 = 2*q1; // update q1 = 2p/abs(nc)
305 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
306 if (r1 >= anc) { // must be unsigned comparison
310 q2 = 2*q2; // update q2 = 2p/abs(d)
311 r2 = 2*r2; // update r2 = rem(2p/abs(d))
312 if (r2 >= ad) { // must be unsigned comparison
317 } while (q1 < delta || (q1 == delta && r1 == 0));
319 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
320 if (d < 0) mag.m = -mag.m; // resulting magic number
321 mag.s = p - 32; // resulting shift
325 /// magicu - calculate the magic numbers required to codegen an integer udiv as
326 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
327 static mu magicu32(uint32_t d) {
329 uint32_t nc, delta, q1, r1, q2, r2;
331 magu.a = 0; // initialize "add" indicator
333 p = 31; // initialize p
334 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
335 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
336 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
337 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
340 if (r1 >= nc - r1 ) {
341 q1 = 2*q1 + 1; // update q1
342 r1 = 2*r1 - nc; // update r1
345 q1 = 2*q1; // update q1
346 r1 = 2*r1; // update r1
348 if (r2 + 1 >= d - r2) {
349 if (q2 >= 0x7FFFFFFF) magu.a = 1;
350 q2 = 2*q2 + 1; // update q2
351 r2 = 2*r2 + 1 - d; // update r2
354 if (q2 >= 0x80000000) magu.a = 1;
355 q2 = 2*q2; // update q2
356 r2 = 2*r2 + 1; // update r2
359 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
360 magu.m = q2 + 1; // resulting magic number
361 magu.s = p - 32; // resulting shift
365 /// magic - calculate the magic numbers required to codegen an integer sdiv as
366 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
368 static ms magic64(int64_t d) {
370 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
371 const uint64_t two63 = 9223372036854775808ULL; // 2^63
374 ad = d >= 0 ? d : -d;
375 t = two63 + ((uint64_t)d >> 63);
376 anc = t - 1 - t%ad; // absolute value of nc
377 p = 63; // initialize p
378 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
379 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
380 q2 = two63/ad; // initialize q2 = 2p/abs(d)
381 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
384 q1 = 2*q1; // update q1 = 2p/abs(nc)
385 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
386 if (r1 >= anc) { // must be unsigned comparison
390 q2 = 2*q2; // update q2 = 2p/abs(d)
391 r2 = 2*r2; // update r2 = rem(2p/abs(d))
392 if (r2 >= ad) { // must be unsigned comparison
397 } while (q1 < delta || (q1 == delta && r1 == 0));
400 if (d < 0) mag.m = -mag.m; // resulting magic number
401 mag.s = p - 64; // resulting shift
405 /// magicu - calculate the magic numbers required to codegen an integer udiv as
406 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
407 static mu magicu64(uint64_t d)
410 uint64_t nc, delta, q1, r1, q2, r2;
412 magu.a = 0; // initialize "add" indicator
414 p = 63; // initialize p
415 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
416 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
417 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
418 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
421 if (r1 >= nc - r1 ) {
422 q1 = 2*q1 + 1; // update q1
423 r1 = 2*r1 - nc; // update r1
426 q1 = 2*q1; // update q1
427 r1 = 2*r1; // update r1
429 if (r2 + 1 >= d - r2) {
430 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
431 q2 = 2*q2 + 1; // update q2
432 r2 = 2*r2 + 1 - d; // update r2
435 if (q2 >= 0x8000000000000000ull) magu.a = 1;
436 q2 = 2*q2; // update q2
437 r2 = 2*r2 + 1; // update r2
440 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
441 magu.m = q2 + 1; // resulting magic number
442 magu.s = p - 64; // resulting shift
446 // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
447 // that selects between the values 1 and 0, making it equivalent to a setcc.
448 // Also, set the incoming LHS, RHS, and CC references to the appropriate
449 // nodes based on the type of node we are checking. This simplifies life a
450 // bit for the callers.
451 static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
453 if (N.getOpcode() == ISD::SETCC) {
454 LHS = N.getOperand(0);
455 RHS = N.getOperand(1);
456 CC = N.getOperand(2);
459 if (N.getOpcode() == ISD::SELECT_CC &&
460 N.getOperand(2).getOpcode() == ISD::Constant &&
461 N.getOperand(3).getOpcode() == ISD::Constant &&
462 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
463 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
464 LHS = N.getOperand(0);
465 RHS = N.getOperand(1);
466 CC = N.getOperand(4);
472 // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
473 // one use. If this is true, it allows the users to invert the operation for
474 // free when it is profitable to do so.
475 static bool isOneUseSetCC(SDOperand N) {
476 SDOperand N0, N1, N2;
477 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
482 // FIXME: This should probably go in the ISD class rather than being duplicated
484 static bool isCommutativeBinOp(unsigned Opcode) {
490 case ISD::XOR: return true;
491 default: return false; // FIXME: Need commutative info for user ops!
495 SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
496 MVT::ValueType VT = N0.getValueType();
497 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
498 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
499 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
500 if (isa<ConstantSDNode>(N1)) {
501 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
502 AddToWorkList(OpNode.Val);
503 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
504 } else if (N0.hasOneUse()) {
505 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
506 AddToWorkList(OpNode.Val);
507 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
510 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
511 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
512 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
513 if (isa<ConstantSDNode>(N0)) {
514 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
515 AddToWorkList(OpNode.Val);
516 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
517 } else if (N1.hasOneUse()) {
518 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
519 AddToWorkList(OpNode.Val);
520 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
526 void DAGCombiner::Run(bool RunningAfterLegalize) {
527 // set the instance variable, so that the various visit routines may use it.
528 AfterLegalize = RunningAfterLegalize;
530 // Add all the dag nodes to the worklist.
531 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
532 E = DAG.allnodes_end(); I != E; ++I)
533 WorkList.push_back(I);
535 // Create a dummy node (which is not added to allnodes), that adds a reference
536 // to the root node, preventing it from being deleted, and tracking any
537 // changes of the root.
538 HandleSDNode Dummy(DAG.getRoot());
541 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
542 TargetLowering::DAGCombinerInfo
543 DagCombineInfo(DAG, !RunningAfterLegalize, this);
545 // while the worklist isn't empty, inspect the node on the end of it and
546 // try and combine it.
547 while (!WorkList.empty()) {
548 SDNode *N = WorkList.back();
551 // If N has no uses, it is dead. Make sure to revisit all N's operands once
552 // N is deleted from the DAG, since they too may now be dead or may have a
553 // reduced number of uses, allowing other xforms.
554 if (N->use_empty() && N != &Dummy) {
555 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
556 WorkList.push_back(N->getOperand(i).Val);
558 removeFromWorkList(N);
563 SDOperand RV = visit(N);
565 // If nothing happened, try a target-specific DAG combine.
567 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
568 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
569 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
574 // If we get back the same node we passed in, rather than a new node or
575 // zero, we know that the node must have defined multiple values and
576 // CombineTo was used. Since CombineTo takes care of the worklist
577 // mechanics for us, we have no work to do in this case.
579 DEBUG(std::cerr << "\nReplacing "; N->dump();
580 std::cerr << "\nWith: "; RV.Val->dump();
582 std::vector<SDNode*> NowDead;
583 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
585 // Push the new node and any users onto the worklist
586 WorkList.push_back(RV.Val);
587 AddUsersToWorkList(RV.Val);
589 // Nodes can end up on the worklist more than once. Make sure we do
590 // not process a node that has been replaced.
591 removeFromWorkList(N);
592 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
593 removeFromWorkList(NowDead[i]);
595 // Finally, since the node is now dead, remove it from the graph.
601 // If the root changed (e.g. it was a dead load, update the root).
602 DAG.setRoot(Dummy.getValue());
605 SDOperand DAGCombiner::visit(SDNode *N) {
606 switch(N->getOpcode()) {
608 case ISD::TokenFactor: return visitTokenFactor(N);
609 case ISD::ADD: return visitADD(N);
610 case ISD::SUB: return visitSUB(N);
611 case ISD::MUL: return visitMUL(N);
612 case ISD::SDIV: return visitSDIV(N);
613 case ISD::UDIV: return visitUDIV(N);
614 case ISD::SREM: return visitSREM(N);
615 case ISD::UREM: return visitUREM(N);
616 case ISD::MULHU: return visitMULHU(N);
617 case ISD::MULHS: return visitMULHS(N);
618 case ISD::AND: return visitAND(N);
619 case ISD::OR: return visitOR(N);
620 case ISD::XOR: return visitXOR(N);
621 case ISD::SHL: return visitSHL(N);
622 case ISD::SRA: return visitSRA(N);
623 case ISD::SRL: return visitSRL(N);
624 case ISD::CTLZ: return visitCTLZ(N);
625 case ISD::CTTZ: return visitCTTZ(N);
626 case ISD::CTPOP: return visitCTPOP(N);
627 case ISD::SELECT: return visitSELECT(N);
628 case ISD::SELECT_CC: return visitSELECT_CC(N);
629 case ISD::SETCC: return visitSETCC(N);
630 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
631 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
632 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
633 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
634 case ISD::TRUNCATE: return visitTRUNCATE(N);
635 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
636 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
637 case ISD::FADD: return visitFADD(N);
638 case ISD::FSUB: return visitFSUB(N);
639 case ISD::FMUL: return visitFMUL(N);
640 case ISD::FDIV: return visitFDIV(N);
641 case ISD::FREM: return visitFREM(N);
642 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
643 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
644 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
645 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
646 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
647 case ISD::FP_ROUND: return visitFP_ROUND(N);
648 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
649 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
650 case ISD::FNEG: return visitFNEG(N);
651 case ISD::FABS: return visitFABS(N);
652 case ISD::BRCOND: return visitBRCOND(N);
653 case ISD::BR_CC: return visitBR_CC(N);
654 case ISD::LOAD: return visitLOAD(N);
657 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
658 case ISD::STORE: return visitSTORE(N);
659 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
660 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
661 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
662 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
663 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
664 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
665 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
666 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
667 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
668 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
669 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
670 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
671 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
676 SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
677 std::vector<SDOperand> Ops;
678 bool Changed = false;
680 // If the token factor has two operands and one is the entry token, replace
681 // the token factor with the other operand.
682 if (N->getNumOperands() == 2) {
683 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
684 return N->getOperand(1);
685 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
686 return N->getOperand(0);
689 // fold (tokenfactor (tokenfactor)) -> tokenfactor
690 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
691 SDOperand Op = N->getOperand(i);
692 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
693 AddToWorkList(Op.Val); // Remove dead node.
695 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
696 Ops.push_back(Op.getOperand(j));
702 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
706 SDOperand DAGCombiner::visitADD(SDNode *N) {
707 SDOperand N0 = N->getOperand(0);
708 SDOperand N1 = N->getOperand(1);
709 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
710 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
711 MVT::ValueType VT = N0.getValueType();
713 // fold (add c1, c2) -> c1+c2
715 return DAG.getNode(ISD::ADD, VT, N0, N1);
716 // canonicalize constant to RHS
718 return DAG.getNode(ISD::ADD, VT, N1, N0);
719 // fold (add x, 0) -> x
720 if (N1C && N1C->isNullValue())
722 // fold ((c1-A)+c2) -> (c1+c2)-A
723 if (N1C && N0.getOpcode() == ISD::SUB)
724 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
725 return DAG.getNode(ISD::SUB, VT,
726 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
729 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
732 // fold ((0-A) + B) -> B-A
733 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
734 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
735 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
736 // fold (A + (0-B)) -> A-B
737 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
738 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
739 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
740 // fold (A+(B-A)) -> B
741 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
742 return N1.getOperand(0);
744 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
745 return SDOperand(N, 0);
747 // fold (a+b) -> (a|b) iff a and b share no bits.
748 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
749 uint64_t LHSZero, LHSOne;
750 uint64_t RHSZero, RHSOne;
751 uint64_t Mask = MVT::getIntVTBitMask(VT);
752 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
754 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
756 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
757 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
758 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
759 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
760 return DAG.getNode(ISD::OR, VT, N0, N1);
767 SDOperand DAGCombiner::visitSUB(SDNode *N) {
768 SDOperand N0 = N->getOperand(0);
769 SDOperand N1 = N->getOperand(1);
770 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
771 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
772 MVT::ValueType VT = N0.getValueType();
774 // fold (sub x, x) -> 0
776 return DAG.getConstant(0, N->getValueType(0));
777 // fold (sub c1, c2) -> c1-c2
779 return DAG.getNode(ISD::SUB, VT, N0, N1);
780 // fold (sub x, c) -> (add x, -c)
782 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
784 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
785 return N0.getOperand(1);
787 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
788 return N0.getOperand(0);
792 SDOperand DAGCombiner::visitMUL(SDNode *N) {
793 SDOperand N0 = N->getOperand(0);
794 SDOperand N1 = N->getOperand(1);
795 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
796 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
797 MVT::ValueType VT = N0.getValueType();
799 // fold (mul c1, c2) -> c1*c2
801 return DAG.getNode(ISD::MUL, VT, N0, N1);
802 // canonicalize constant to RHS
804 return DAG.getNode(ISD::MUL, VT, N1, N0);
805 // fold (mul x, 0) -> 0
806 if (N1C && N1C->isNullValue())
808 // fold (mul x, -1) -> 0-x
809 if (N1C && N1C->isAllOnesValue())
810 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
811 // fold (mul x, (1 << c)) -> x << c
812 if (N1C && isPowerOf2_64(N1C->getValue()))
813 return DAG.getNode(ISD::SHL, VT, N0,
814 DAG.getConstant(Log2_64(N1C->getValue()),
815 TLI.getShiftAmountTy()));
816 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
817 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
818 // FIXME: If the input is something that is easily negated (e.g. a
819 // single-use add), we should put the negate there.
820 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
821 DAG.getNode(ISD::SHL, VT, N0,
822 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
823 TLI.getShiftAmountTy())));
826 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
827 if (N1C && N0.getOpcode() == ISD::SHL &&
828 isa<ConstantSDNode>(N0.getOperand(1))) {
829 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
830 AddToWorkList(C3.Val);
831 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
834 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
837 SDOperand Sh(0,0), Y(0,0);
838 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
839 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
840 N0.Val->hasOneUse()) {
842 } else if (N1.getOpcode() == ISD::SHL &&
843 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
847 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
848 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
851 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
852 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
853 isa<ConstantSDNode>(N0.getOperand(1))) {
854 return DAG.getNode(ISD::ADD, VT,
855 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
856 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
860 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
866 SDOperand DAGCombiner::visitSDIV(SDNode *N) {
867 SDOperand N0 = N->getOperand(0);
868 SDOperand N1 = N->getOperand(1);
869 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
870 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
871 MVT::ValueType VT = N->getValueType(0);
873 // fold (sdiv c1, c2) -> c1/c2
874 if (N0C && N1C && !N1C->isNullValue())
875 return DAG.getNode(ISD::SDIV, VT, N0, N1);
876 // fold (sdiv X, 1) -> X
877 if (N1C && N1C->getSignExtended() == 1LL)
879 // fold (sdiv X, -1) -> 0-X
880 if (N1C && N1C->isAllOnesValue())
881 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
882 // If we know the sign bits of both operands are zero, strength reduce to a
883 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
884 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
885 if (TLI.MaskedValueIsZero(N1, SignBit) &&
886 TLI.MaskedValueIsZero(N0, SignBit))
887 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
888 // fold (sdiv X, pow2) -> simple ops after legalize
889 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
890 (isPowerOf2_64(N1C->getSignExtended()) ||
891 isPowerOf2_64(-N1C->getSignExtended()))) {
892 // If dividing by powers of two is cheap, then don't perform the following
894 if (TLI.isPow2DivCheap())
896 int64_t pow2 = N1C->getSignExtended();
897 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
898 unsigned lg2 = Log2_64(abs2);
899 // Splat the sign bit into the register
900 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
901 DAG.getConstant(MVT::getSizeInBits(VT)-1,
902 TLI.getShiftAmountTy()));
903 AddToWorkList(SGN.Val);
904 // Add (N0 < 0) ? abs2 - 1 : 0;
905 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
906 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
907 TLI.getShiftAmountTy()));
908 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
909 AddToWorkList(SRL.Val);
910 AddToWorkList(ADD.Val); // Divide by pow2
911 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
912 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
913 // If we're dividing by a positive value, we're done. Otherwise, we must
914 // negate the result.
917 AddToWorkList(SRA.Val);
918 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
920 // if integer divide is expensive and we satisfy the requirements, emit an
921 // alternate sequence.
922 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
923 !TLI.isIntDivCheap()) {
924 SDOperand Op = BuildSDIV(N);
925 if (Op.Val) return Op;
930 SDOperand DAGCombiner::visitUDIV(SDNode *N) {
931 SDOperand N0 = N->getOperand(0);
932 SDOperand N1 = N->getOperand(1);
933 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
934 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
935 MVT::ValueType VT = N->getValueType(0);
937 // fold (udiv c1, c2) -> c1/c2
938 if (N0C && N1C && !N1C->isNullValue())
939 return DAG.getNode(ISD::UDIV, VT, N0, N1);
940 // fold (udiv x, (1 << c)) -> x >>u c
941 if (N1C && isPowerOf2_64(N1C->getValue()))
942 return DAG.getNode(ISD::SRL, VT, N0,
943 DAG.getConstant(Log2_64(N1C->getValue()),
944 TLI.getShiftAmountTy()));
945 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
946 if (N1.getOpcode() == ISD::SHL) {
947 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
948 if (isPowerOf2_64(SHC->getValue())) {
949 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
950 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
951 DAG.getConstant(Log2_64(SHC->getValue()),
953 AddToWorkList(Add.Val);
954 return DAG.getNode(ISD::SRL, VT, N0, Add);
958 // fold (udiv x, c) -> alternate
959 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
960 SDOperand Op = BuildUDIV(N);
961 if (Op.Val) return Op;
966 SDOperand DAGCombiner::visitSREM(SDNode *N) {
967 SDOperand N0 = N->getOperand(0);
968 SDOperand N1 = N->getOperand(1);
969 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
970 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
971 MVT::ValueType VT = N->getValueType(0);
973 // fold (srem c1, c2) -> c1%c2
974 if (N0C && N1C && !N1C->isNullValue())
975 return DAG.getNode(ISD::SREM, VT, N0, N1);
976 // If we know the sign bits of both operands are zero, strength reduce to a
977 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
978 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
979 if (TLI.MaskedValueIsZero(N1, SignBit) &&
980 TLI.MaskedValueIsZero(N0, SignBit))
981 return DAG.getNode(ISD::UREM, VT, N0, N1);
985 SDOperand DAGCombiner::visitUREM(SDNode *N) {
986 SDOperand N0 = N->getOperand(0);
987 SDOperand N1 = N->getOperand(1);
988 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
989 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
990 MVT::ValueType VT = N->getValueType(0);
992 // fold (urem c1, c2) -> c1%c2
993 if (N0C && N1C && !N1C->isNullValue())
994 return DAG.getNode(ISD::UREM, VT, N0, N1);
995 // fold (urem x, pow2) -> (and x, pow2-1)
996 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
997 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
998 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
999 if (N1.getOpcode() == ISD::SHL) {
1000 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1001 if (isPowerOf2_64(SHC->getValue())) {
1002 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
1003 AddToWorkList(Add.Val);
1004 return DAG.getNode(ISD::AND, VT, N0, Add);
1011 SDOperand DAGCombiner::visitMULHS(SDNode *N) {
1012 SDOperand N0 = N->getOperand(0);
1013 SDOperand N1 = N->getOperand(1);
1014 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1016 // fold (mulhs x, 0) -> 0
1017 if (N1C && N1C->isNullValue())
1019 // fold (mulhs x, 1) -> (sra x, size(x)-1)
1020 if (N1C && N1C->getValue() == 1)
1021 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1022 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
1023 TLI.getShiftAmountTy()));
1027 SDOperand DAGCombiner::visitMULHU(SDNode *N) {
1028 SDOperand N0 = N->getOperand(0);
1029 SDOperand N1 = N->getOperand(1);
1030 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1032 // fold (mulhu x, 0) -> 0
1033 if (N1C && N1C->isNullValue())
1035 // fold (mulhu x, 1) -> 0
1036 if (N1C && N1C->getValue() == 1)
1037 return DAG.getConstant(0, N0.getValueType());
1041 /// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1042 /// two operands of the same opcode, try to simplify it.
1043 SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1044 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1045 MVT::ValueType VT = N0.getValueType();
1046 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1048 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1049 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1050 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1051 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1052 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1053 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1054 N0.getOperand(0).getValueType(),
1055 N0.getOperand(0), N1.getOperand(0));
1056 AddToWorkList(ORNode.Val);
1057 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1060 // fold (and (trunc x), (trunc y)) -> (trunc (and x, y))
1061 // fold (or (trunc x), (trunc y)) -> (trunc (or x, y))
1062 // fold (xor (trunc x), (trunc y)) -> (trunc (xor x, y))
1063 if (N0.getOpcode() == ISD::TRUNCATE &&
1064 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1065 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1066 N0.getOperand(0).getValueType(),
1067 N0.getOperand(0), N1.getOperand(0));
1068 AddToWorkList(ORNode.Val);
1069 return DAG.getNode(ISD::TRUNCATE, VT, ORNode);
1073 // For each of OP in SHL/SRL/SRA/AND...
1074 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1075 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1076 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
1077 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
1078 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
1079 N0.getOperand(1) == N1.getOperand(1)) {
1080 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1081 N0.getOperand(0).getValueType(),
1082 N0.getOperand(0), N1.getOperand(0));
1083 AddToWorkList(ORNode.Val);
1084 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1090 SDOperand DAGCombiner::visitAND(SDNode *N) {
1091 SDOperand N0 = N->getOperand(0);
1092 SDOperand N1 = N->getOperand(1);
1093 SDOperand LL, LR, RL, RR, CC0, CC1;
1094 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1095 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1096 MVT::ValueType VT = N1.getValueType();
1097 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1099 // fold (and c1, c2) -> c1&c2
1101 return DAG.getNode(ISD::AND, VT, N0, N1);
1102 // canonicalize constant to RHS
1104 return DAG.getNode(ISD::AND, VT, N1, N0);
1105 // fold (and x, -1) -> x
1106 if (N1C && N1C->isAllOnesValue())
1108 // if (and x, c) is known to be zero, return 0
1109 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
1110 return DAG.getConstant(0, VT);
1112 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1115 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
1116 if (N1C && N0.getOpcode() == ISD::OR)
1117 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1118 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
1120 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1121 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1122 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
1123 if (TLI.MaskedValueIsZero(N0.getOperand(0),
1124 ~N1C->getValue() & InMask)) {
1125 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1128 // Replace uses of the AND with uses of the Zero extend node.
1131 // We actually want to replace all uses of the any_extend with the
1132 // zero_extend, to avoid duplicating things. This will later cause this
1133 // AND to be folded.
1134 CombineTo(N0.Val, Zext);
1135 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1138 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1139 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1140 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1141 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1143 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1144 MVT::isInteger(LL.getValueType())) {
1145 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1146 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1147 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1148 AddToWorkList(ORNode.Val);
1149 return DAG.getSetCC(VT, ORNode, LR, Op1);
1151 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1152 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1153 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1154 AddToWorkList(ANDNode.Val);
1155 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1157 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1158 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1159 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1160 AddToWorkList(ORNode.Val);
1161 return DAG.getSetCC(VT, ORNode, LR, Op1);
1164 // canonicalize equivalent to ll == rl
1165 if (LL == RR && LR == RL) {
1166 Op1 = ISD::getSetCCSwappedOperands(Op1);
1169 if (LL == RL && LR == RR) {
1170 bool isInteger = MVT::isInteger(LL.getValueType());
1171 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1172 if (Result != ISD::SETCC_INVALID)
1173 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1177 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1178 if (N0.getOpcode() == N1.getOpcode()) {
1179 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1180 if (Tmp.Val) return Tmp;
1183 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1184 // fold (and (sra)) -> (and (srl)) when possible.
1185 if (!MVT::isVector(VT) &&
1186 SimplifyDemandedBits(SDOperand(N, 0)))
1187 return SDOperand(N, 0);
1188 // fold (zext_inreg (extload x)) -> (zextload x)
1189 if (N0.getOpcode() == ISD::EXTLOAD) {
1190 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1191 // If we zero all the possible extended bits, then we can turn this into
1192 // a zextload if we are running before legalize or the operation is legal.
1193 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
1194 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1195 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1196 N0.getOperand(1), N0.getOperand(2),
1199 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1200 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1203 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
1204 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
1205 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1206 // If we zero all the possible extended bits, then we can turn this into
1207 // a zextload if we are running before legalize or the operation is legal.
1208 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
1209 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1210 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1211 N0.getOperand(1), N0.getOperand(2),
1214 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1215 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1219 // fold (and (load x), 255) -> (zextload x, i8)
1220 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1222 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1223 N0.getOpcode() == ISD::ZEXTLOAD) &&
1225 MVT::ValueType EVT, LoadedVT;
1226 if (N1C->getValue() == 255)
1228 else if (N1C->getValue() == 65535)
1230 else if (N1C->getValue() == ~0U)
1235 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1236 cast<VTSDNode>(N0.getOperand(3))->getVT();
1237 if (EVT != MVT::Other && LoadedVT > EVT &&
1238 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1239 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1240 // For big endian targets, we need to add an offset to the pointer to load
1241 // the correct bytes. For little endian systems, we merely need to read
1242 // fewer bytes from the same pointer.
1244 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1245 SDOperand NewPtr = N0.getOperand(1);
1246 if (!TLI.isLittleEndian())
1247 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1248 DAG.getConstant(PtrOff, PtrType));
1249 AddToWorkList(NewPtr.Val);
1251 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1252 N0.getOperand(2), EVT);
1254 CombineTo(N0.Val, Load, Load.getValue(1));
1255 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1262 SDOperand DAGCombiner::visitOR(SDNode *N) {
1263 SDOperand N0 = N->getOperand(0);
1264 SDOperand N1 = N->getOperand(1);
1265 SDOperand LL, LR, RL, RR, CC0, CC1;
1266 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1267 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1268 MVT::ValueType VT = N1.getValueType();
1269 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1271 // fold (or c1, c2) -> c1|c2
1273 return DAG.getNode(ISD::OR, VT, N0, N1);
1274 // canonicalize constant to RHS
1276 return DAG.getNode(ISD::OR, VT, N1, N0);
1277 // fold (or x, 0) -> x
1278 if (N1C && N1C->isNullValue())
1280 // fold (or x, -1) -> -1
1281 if (N1C && N1C->isAllOnesValue())
1283 // fold (or x, c) -> c iff (x & ~c) == 0
1285 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
1288 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1291 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1292 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
1293 isa<ConstantSDNode>(N0.getOperand(1))) {
1294 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1295 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1297 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
1299 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1300 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1301 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1302 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1304 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1305 MVT::isInteger(LL.getValueType())) {
1306 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1307 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1308 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1309 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1310 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1311 AddToWorkList(ORNode.Val);
1312 return DAG.getSetCC(VT, ORNode, LR, Op1);
1314 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1315 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1316 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1317 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1318 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1319 AddToWorkList(ANDNode.Val);
1320 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1323 // canonicalize equivalent to ll == rl
1324 if (LL == RR && LR == RL) {
1325 Op1 = ISD::getSetCCSwappedOperands(Op1);
1328 if (LL == RL && LR == RR) {
1329 bool isInteger = MVT::isInteger(LL.getValueType());
1330 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1331 if (Result != ISD::SETCC_INVALID)
1332 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1336 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1337 if (N0.getOpcode() == N1.getOpcode()) {
1338 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1339 if (Tmp.Val) return Tmp;
1342 // canonicalize shl to left side in a shl/srl pair, to match rotate
1343 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1345 // check for rotl, rotr
1346 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1347 N0.getOperand(0) == N1.getOperand(0) &&
1348 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
1349 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1350 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1351 N1.getOperand(1).getOpcode() == ISD::Constant) {
1352 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1353 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1354 if ((c1val + c2val) == OpSizeInBits)
1355 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1357 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1358 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1359 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1360 if (ConstantSDNode *SUBC =
1361 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1362 if (SUBC->getValue() == OpSizeInBits)
1363 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1364 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1365 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1366 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1367 if (ConstantSDNode *SUBC =
1368 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1369 if (SUBC->getValue() == OpSizeInBits) {
1370 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
1371 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1374 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1381 SDOperand DAGCombiner::visitXOR(SDNode *N) {
1382 SDOperand N0 = N->getOperand(0);
1383 SDOperand N1 = N->getOperand(1);
1384 SDOperand LHS, RHS, CC;
1385 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1386 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1387 MVT::ValueType VT = N0.getValueType();
1389 // fold (xor c1, c2) -> c1^c2
1391 return DAG.getNode(ISD::XOR, VT, N0, N1);
1392 // canonicalize constant to RHS
1394 return DAG.getNode(ISD::XOR, VT, N1, N0);
1395 // fold (xor x, 0) -> x
1396 if (N1C && N1C->isNullValue())
1399 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1402 // fold !(x cc y) -> (x !cc y)
1403 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1404 bool isInt = MVT::isInteger(LHS.getValueType());
1405 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1407 if (N0.getOpcode() == ISD::SETCC)
1408 return DAG.getSetCC(VT, LHS, RHS, NotCC);
1409 if (N0.getOpcode() == ISD::SELECT_CC)
1410 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
1411 assert(0 && "Unhandled SetCC Equivalent!");
1414 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1415 if (N1C && N1C->getValue() == 1 &&
1416 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
1417 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
1418 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1419 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
1420 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1421 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
1422 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
1423 return DAG.getNode(NewOpcode, VT, LHS, RHS);
1426 // fold !(x or y) -> (!x and !y) iff x or y are constants
1427 if (N1C && N1C->isAllOnesValue() &&
1428 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
1429 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
1430 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1431 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
1432 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1433 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
1434 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
1435 return DAG.getNode(NewOpcode, VT, LHS, RHS);
1438 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1439 if (N1C && N0.getOpcode() == ISD::XOR) {
1440 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1441 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1443 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1444 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1446 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1447 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1449 // fold (xor x, x) -> 0
1451 if (!MVT::isVector(VT)) {
1452 return DAG.getConstant(0, VT);
1453 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1454 // Produce a vector of zeros.
1455 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1456 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1457 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
1461 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1462 if (N0.getOpcode() == N1.getOpcode()) {
1463 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1464 if (Tmp.Val) return Tmp;
1467 // Simplify the expression using non-local knowledge.
1468 if (!MVT::isVector(VT) &&
1469 SimplifyDemandedBits(SDOperand(N, 0)))
1470 return SDOperand(N, 0);
1475 SDOperand DAGCombiner::visitSHL(SDNode *N) {
1476 SDOperand N0 = N->getOperand(0);
1477 SDOperand N1 = N->getOperand(1);
1478 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1479 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1480 MVT::ValueType VT = N0.getValueType();
1481 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1483 // fold (shl c1, c2) -> c1<<c2
1485 return DAG.getNode(ISD::SHL, VT, N0, N1);
1486 // fold (shl 0, x) -> 0
1487 if (N0C && N0C->isNullValue())
1489 // fold (shl x, c >= size(x)) -> undef
1490 if (N1C && N1C->getValue() >= OpSizeInBits)
1491 return DAG.getNode(ISD::UNDEF, VT);
1492 // fold (shl x, 0) -> x
1493 if (N1C && N1C->isNullValue())
1495 // if (shl x, c) is known to be zero, return 0
1496 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
1497 return DAG.getConstant(0, VT);
1498 if (SimplifyDemandedBits(SDOperand(N, 0)))
1499 return SDOperand(N, 0);
1500 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
1501 if (N1C && N0.getOpcode() == ISD::SHL &&
1502 N0.getOperand(1).getOpcode() == ISD::Constant) {
1503 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1504 uint64_t c2 = N1C->getValue();
1505 if (c1 + c2 > OpSizeInBits)
1506 return DAG.getConstant(0, VT);
1507 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
1508 DAG.getConstant(c1 + c2, N1.getValueType()));
1510 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1511 // (srl (and x, -1 << c1), c1-c2)
1512 if (N1C && N0.getOpcode() == ISD::SRL &&
1513 N0.getOperand(1).getOpcode() == ISD::Constant) {
1514 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1515 uint64_t c2 = N1C->getValue();
1516 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1517 DAG.getConstant(~0ULL << c1, VT));
1519 return DAG.getNode(ISD::SHL, VT, Mask,
1520 DAG.getConstant(c2-c1, N1.getValueType()));
1522 return DAG.getNode(ISD::SRL, VT, Mask,
1523 DAG.getConstant(c1-c2, N1.getValueType()));
1525 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
1526 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
1527 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1528 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1529 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1530 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1531 isa<ConstantSDNode>(N0.getOperand(1))) {
1532 return DAG.getNode(ISD::ADD, VT,
1533 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1534 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1539 SDOperand DAGCombiner::visitSRA(SDNode *N) {
1540 SDOperand N0 = N->getOperand(0);
1541 SDOperand N1 = N->getOperand(1);
1542 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1543 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1544 MVT::ValueType VT = N0.getValueType();
1546 // fold (sra c1, c2) -> c1>>c2
1548 return DAG.getNode(ISD::SRA, VT, N0, N1);
1549 // fold (sra 0, x) -> 0
1550 if (N0C && N0C->isNullValue())
1552 // fold (sra -1, x) -> -1
1553 if (N0C && N0C->isAllOnesValue())
1555 // fold (sra x, c >= size(x)) -> undef
1556 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
1557 return DAG.getNode(ISD::UNDEF, VT);
1558 // fold (sra x, 0) -> x
1559 if (N1C && N1C->isNullValue())
1561 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1563 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1564 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1567 default: EVT = MVT::Other; break;
1568 case 1: EVT = MVT::i1; break;
1569 case 8: EVT = MVT::i8; break;
1570 case 16: EVT = MVT::i16; break;
1571 case 32: EVT = MVT::i32; break;
1573 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1574 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1575 DAG.getValueType(EVT));
1578 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1579 if (N1C && N0.getOpcode() == ISD::SRA) {
1580 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1581 unsigned Sum = N1C->getValue() + C1->getValue();
1582 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1583 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1584 DAG.getConstant(Sum, N1C->getValueType(0)));
1588 // If the sign bit is known to be zero, switch this to a SRL.
1589 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
1590 return DAG.getNode(ISD::SRL, VT, N0, N1);
1594 SDOperand DAGCombiner::visitSRL(SDNode *N) {
1595 SDOperand N0 = N->getOperand(0);
1596 SDOperand N1 = N->getOperand(1);
1597 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1598 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1599 MVT::ValueType VT = N0.getValueType();
1600 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1602 // fold (srl c1, c2) -> c1 >>u c2
1604 return DAG.getNode(ISD::SRL, VT, N0, N1);
1605 // fold (srl 0, x) -> 0
1606 if (N0C && N0C->isNullValue())
1608 // fold (srl x, c >= size(x)) -> undef
1609 if (N1C && N1C->getValue() >= OpSizeInBits)
1610 return DAG.getNode(ISD::UNDEF, VT);
1611 // fold (srl x, 0) -> x
1612 if (N1C && N1C->isNullValue())
1614 // if (srl x, c) is known to be zero, return 0
1615 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
1616 return DAG.getConstant(0, VT);
1617 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
1618 if (N1C && N0.getOpcode() == ISD::SRL &&
1619 N0.getOperand(1).getOpcode() == ISD::Constant) {
1620 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1621 uint64_t c2 = N1C->getValue();
1622 if (c1 + c2 > OpSizeInBits)
1623 return DAG.getConstant(0, VT);
1624 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
1625 DAG.getConstant(c1 + c2, N1.getValueType()));
1628 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1629 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1630 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1631 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1632 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1634 // If any of the input bits are KnownOne, then the input couldn't be all
1635 // zeros, thus the result of the srl will always be zero.
1636 if (KnownOne) return DAG.getConstant(0, VT);
1638 // If all of the bits input the to ctlz node are known to be zero, then
1639 // the result of the ctlz is "32" and the result of the shift is one.
1640 uint64_t UnknownBits = ~KnownZero & Mask;
1641 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1643 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1644 if ((UnknownBits & (UnknownBits-1)) == 0) {
1645 // Okay, we know that only that the single bit specified by UnknownBits
1646 // could be set on input to the CTLZ node. If this bit is set, the SRL
1647 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1648 // to an SRL,XOR pair, which is likely to simplify more.
1649 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1650 SDOperand Op = N0.getOperand(0);
1652 Op = DAG.getNode(ISD::SRL, VT, Op,
1653 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1654 AddToWorkList(Op.Val);
1656 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1663 SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
1664 SDOperand N0 = N->getOperand(0);
1665 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1666 MVT::ValueType VT = N->getValueType(0);
1668 // fold (ctlz c1) -> c2
1670 return DAG.getNode(ISD::CTLZ, VT, N0);
1674 SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
1675 SDOperand N0 = N->getOperand(0);
1676 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1677 MVT::ValueType VT = N->getValueType(0);
1679 // fold (cttz c1) -> c2
1681 return DAG.getNode(ISD::CTTZ, VT, N0);
1685 SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
1686 SDOperand N0 = N->getOperand(0);
1687 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1688 MVT::ValueType VT = N->getValueType(0);
1690 // fold (ctpop c1) -> c2
1692 return DAG.getNode(ISD::CTPOP, VT, N0);
1696 SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1697 SDOperand N0 = N->getOperand(0);
1698 SDOperand N1 = N->getOperand(1);
1699 SDOperand N2 = N->getOperand(2);
1700 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1701 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1702 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1703 MVT::ValueType VT = N->getValueType(0);
1705 // fold select C, X, X -> X
1708 // fold select true, X, Y -> X
1709 if (N0C && !N0C->isNullValue())
1711 // fold select false, X, Y -> Y
1712 if (N0C && N0C->isNullValue())
1714 // fold select C, 1, X -> C | X
1715 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
1716 return DAG.getNode(ISD::OR, VT, N0, N2);
1717 // fold select C, 0, X -> ~C & X
1718 // FIXME: this should check for C type == X type, not i1?
1719 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1720 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1721 AddToWorkList(XORNode.Val);
1722 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1724 // fold select C, X, 1 -> ~C | X
1725 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
1726 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1727 AddToWorkList(XORNode.Val);
1728 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1730 // fold select C, X, 0 -> C & X
1731 // FIXME: this should check for C type == X type, not i1?
1732 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1733 return DAG.getNode(ISD::AND, VT, N0, N1);
1734 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1735 if (MVT::i1 == VT && N0 == N1)
1736 return DAG.getNode(ISD::OR, VT, N0, N2);
1737 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1738 if (MVT::i1 == VT && N0 == N2)
1739 return DAG.getNode(ISD::AND, VT, N0, N1);
1740 // If we can fold this based on the true/false value, do so.
1741 if (SimplifySelectOps(N, N1, N2))
1743 // fold selects based on a setcc into other things, such as min/max/abs
1744 if (N0.getOpcode() == ISD::SETCC)
1746 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1747 // having to say they don't support SELECT_CC on every type the DAG knows
1748 // about, since there is no way to mark an opcode illegal at all value types
1749 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1750 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1751 N1, N2, N0.getOperand(2));
1753 return SimplifySelect(N0, N1, N2);
1757 SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
1758 SDOperand N0 = N->getOperand(0);
1759 SDOperand N1 = N->getOperand(1);
1760 SDOperand N2 = N->getOperand(2);
1761 SDOperand N3 = N->getOperand(3);
1762 SDOperand N4 = N->getOperand(4);
1763 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1764 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1765 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1766 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1768 // Determine if the condition we're dealing with is constant
1769 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1770 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1772 // fold select_cc lhs, rhs, x, x, cc -> x
1776 // If we can fold this based on the true/false value, do so.
1777 if (SimplifySelectOps(N, N2, N3))
1780 // fold select_cc into other things, such as min/max/abs
1781 return SimplifySelectCC(N0, N1, N2, N3, CC);
1784 SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1785 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1786 cast<CondCodeSDNode>(N->getOperand(2))->get());
1789 SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
1790 SDOperand N0 = N->getOperand(0);
1791 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1792 MVT::ValueType VT = N->getValueType(0);
1794 // fold (sext c1) -> c1
1796 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
1797 // fold (sext (sext x)) -> (sext x)
1798 if (N0.getOpcode() == ISD::SIGN_EXTEND)
1799 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1800 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
1801 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1803 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
1804 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1805 DAG.getValueType(N0.getValueType()));
1806 // fold (sext (load x)) -> (sext (truncate (sextload x)))
1807 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1808 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
1809 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1810 N0.getOperand(1), N0.getOperand(2),
1812 CombineTo(N, ExtLoad);
1813 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1814 ExtLoad.getValue(1));
1815 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1818 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1819 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1820 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1822 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1823 N0.getOperand(1), N0.getOperand(2),
1825 CombineTo(N, ExtLoad);
1826 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1827 ExtLoad.getValue(1));
1828 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1834 SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
1835 SDOperand N0 = N->getOperand(0);
1836 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1837 MVT::ValueType VT = N->getValueType(0);
1839 // fold (zext c1) -> c1
1841 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1842 // fold (zext (zext x)) -> (zext x)
1843 if (N0.getOpcode() == ISD::ZERO_EXTEND)
1844 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1845 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1846 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1847 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
1848 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
1849 // fold (zext (load x)) -> (zext (truncate (zextload x)))
1850 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1851 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
1852 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1853 N0.getOperand(1), N0.getOperand(2),
1855 CombineTo(N, ExtLoad);
1856 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1857 ExtLoad.getValue(1));
1858 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1861 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1862 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1863 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1865 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1866 N0.getOperand(1), N0.getOperand(2),
1868 CombineTo(N, ExtLoad);
1869 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1870 ExtLoad.getValue(1));
1871 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1876 SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1877 SDOperand N0 = N->getOperand(0);
1878 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1879 MVT::ValueType VT = N->getValueType(0);
1881 // fold (aext c1) -> c1
1883 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1884 // fold (aext (aext x)) -> (aext x)
1885 // fold (aext (zext x)) -> (zext x)
1886 // fold (aext (sext x)) -> (sext x)
1887 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1888 N0.getOpcode() == ISD::ZERO_EXTEND ||
1889 N0.getOpcode() == ISD::SIGN_EXTEND)
1890 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1892 // fold (aext (truncate x)) -> x iff x size == zext size.
1893 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT)
1894 return N0.getOperand(0);
1895 // fold (aext (load x)) -> (aext (truncate (extload x)))
1896 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1897 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
1898 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1899 N0.getOperand(1), N0.getOperand(2),
1901 CombineTo(N, ExtLoad);
1902 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1903 ExtLoad.getValue(1));
1904 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1907 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
1908 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
1909 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
1910 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD ||
1911 N0.getOpcode() == ISD::SEXTLOAD) &&
1913 SDOperand ExtLoad = DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0),
1914 N0.getOperand(1), N0.getOperand(2),
1916 CombineTo(N, ExtLoad);
1917 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1918 ExtLoad.getValue(1));
1919 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1925 SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
1926 SDOperand N0 = N->getOperand(0);
1927 SDOperand N1 = N->getOperand(1);
1928 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1929 MVT::ValueType VT = N->getValueType(0);
1930 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
1931 unsigned EVTBits = MVT::getSizeInBits(EVT);
1933 // fold (sext_in_reg c1) -> c1
1935 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
1936 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
1938 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
1939 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1940 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
1943 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1944 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1945 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
1946 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
1948 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1949 if (N0.getOpcode() == ISD::AssertSext &&
1950 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
1953 // fold (sext_in_reg (sextload x)) -> (sextload x)
1954 if (N0.getOpcode() == ISD::SEXTLOAD &&
1955 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
1958 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
1959 if (N0.getOpcode() == ISD::SETCC &&
1960 TLI.getSetCCResultContents() ==
1961 TargetLowering::ZeroOrNegativeOneSetCCResult)
1963 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1964 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
1965 return DAG.getZeroExtendInReg(N0, EVT);
1966 // fold (sext_inreg (extload x)) -> (sextload x)
1967 if (N0.getOpcode() == ISD::EXTLOAD &&
1968 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
1969 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
1970 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1971 N0.getOperand(1), N0.getOperand(2),
1973 CombineTo(N, ExtLoad);
1974 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1975 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1977 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
1978 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
1979 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
1980 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
1981 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1982 N0.getOperand(1), N0.getOperand(2),
1984 CombineTo(N, ExtLoad);
1985 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1986 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1991 SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
1992 SDOperand N0 = N->getOperand(0);
1993 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1994 MVT::ValueType VT = N->getValueType(0);
1997 if (N0.getValueType() == N->getValueType(0))
1999 // fold (truncate c1) -> c1
2001 return DAG.getNode(ISD::TRUNCATE, VT, N0);
2002 // fold (truncate (truncate x)) -> (truncate x)
2003 if (N0.getOpcode() == ISD::TRUNCATE)
2004 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
2005 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
2006 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
2007 if (N0.getValueType() < VT)
2008 // if the source is smaller than the dest, we still need an extend
2009 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2010 else if (N0.getValueType() > VT)
2011 // if the source is larger than the dest, than we just need the truncate
2012 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
2014 // if the source and dest are the same type, we can drop both the extend
2016 return N0.getOperand(0);
2018 // fold (truncate (load x)) -> (smaller load x)
2019 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
2020 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2021 "Cannot truncate to larger type!");
2022 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
2023 // For big endian targets, we need to add an offset to the pointer to load
2024 // the correct bytes. For little endian systems, we merely need to read
2025 // fewer bytes from the same pointer.
2027 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
2028 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
2029 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
2030 DAG.getConstant(PtrOff, PtrType));
2031 AddToWorkList(NewPtr.Val);
2032 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
2034 CombineTo(N0.Val, Load, Load.getValue(1));
2035 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2040 SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2041 SDOperand N0 = N->getOperand(0);
2042 MVT::ValueType VT = N->getValueType(0);
2044 // If the input is a constant, let getNode() fold it.
2045 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2046 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2047 if (Res.Val != N) return Res;
2050 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2051 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
2053 // fold (conv (load x)) -> (load (conv*)x)
2054 // FIXME: These xforms need to know that the resultant load doesn't need a
2055 // higher alignment than the original!
2056 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
2057 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2060 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2068 SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2069 SDOperand N0 = N->getOperand(0);
2070 MVT::ValueType VT = N->getValueType(0);
2072 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2073 // First check to see if this is all constant.
2074 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2075 VT == MVT::Vector) {
2076 bool isSimple = true;
2077 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2078 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2079 N0.getOperand(i).getOpcode() != ISD::Constant &&
2080 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2085 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2086 if (isSimple && !MVT::isVector(DestEltVT)) {
2087 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2094 /// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2095 /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2096 /// destination element value type.
2097 SDOperand DAGCombiner::
2098 ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2099 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2101 // If this is already the right type, we're done.
2102 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2104 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2105 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2107 // If this is a conversion of N elements of one type to N elements of another
2108 // type, convert each element. This handles FP<->INT cases.
2109 if (SrcBitSize == DstBitSize) {
2110 std::vector<SDOperand> Ops;
2111 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2112 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
2113 AddToWorkList(Ops.back().Val);
2115 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2116 Ops.push_back(DAG.getValueType(DstEltVT));
2117 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2120 // Otherwise, we're growing or shrinking the elements. To avoid having to
2121 // handle annoying details of growing/shrinking FP values, we convert them to
2123 if (MVT::isFloatingPoint(SrcEltVT)) {
2124 // Convert the input float vector to a int vector where the elements are the
2126 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2127 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2128 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2132 // Now we know the input is an integer vector. If the output is a FP type,
2133 // convert to integer first, then to FP of the right size.
2134 if (MVT::isFloatingPoint(DstEltVT)) {
2135 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2136 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2137 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2139 // Next, convert to FP elements of the same size.
2140 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2143 // Okay, we know the src/dst types are both integers of differing types.
2144 // Handling growing first.
2145 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2146 if (SrcBitSize < DstBitSize) {
2147 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2149 std::vector<SDOperand> Ops;
2150 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2151 i += NumInputsPerOutput) {
2152 bool isLE = TLI.isLittleEndian();
2153 uint64_t NewBits = 0;
2154 bool EltIsUndef = true;
2155 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2156 // Shift the previously computed bits over.
2157 NewBits <<= SrcBitSize;
2158 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2159 if (Op.getOpcode() == ISD::UNDEF) continue;
2162 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2166 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2168 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2171 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2172 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2173 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2176 // Finally, this must be the case where we are shrinking elements: each input
2177 // turns into multiple outputs.
2178 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
2179 std::vector<SDOperand> Ops;
2180 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2181 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2182 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2183 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2186 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2188 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2189 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2190 OpVal >>= DstBitSize;
2191 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2194 // For big endian targets, swap the order of the pieces of each element.
2195 if (!TLI.isLittleEndian())
2196 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2198 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2199 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2200 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2205 SDOperand DAGCombiner::visitFADD(SDNode *N) {
2206 SDOperand N0 = N->getOperand(0);
2207 SDOperand N1 = N->getOperand(1);
2208 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2209 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2210 MVT::ValueType VT = N->getValueType(0);
2212 // fold (fadd c1, c2) -> c1+c2
2214 return DAG.getNode(ISD::FADD, VT, N0, N1);
2215 // canonicalize constant to RHS
2216 if (N0CFP && !N1CFP)
2217 return DAG.getNode(ISD::FADD, VT, N1, N0);
2218 // fold (A + (-B)) -> A-B
2219 if (N1.getOpcode() == ISD::FNEG)
2220 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
2221 // fold ((-A) + B) -> B-A
2222 if (N0.getOpcode() == ISD::FNEG)
2223 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
2227 SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2228 SDOperand N0 = N->getOperand(0);
2229 SDOperand N1 = N->getOperand(1);
2230 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2231 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2232 MVT::ValueType VT = N->getValueType(0);
2234 // fold (fsub c1, c2) -> c1-c2
2236 return DAG.getNode(ISD::FSUB, VT, N0, N1);
2237 // fold (A-(-B)) -> A+B
2238 if (N1.getOpcode() == ISD::FNEG)
2239 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
2243 SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2244 SDOperand N0 = N->getOperand(0);
2245 SDOperand N1 = N->getOperand(1);
2246 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2247 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2248 MVT::ValueType VT = N->getValueType(0);
2250 // fold (fmul c1, c2) -> c1*c2
2252 return DAG.getNode(ISD::FMUL, VT, N0, N1);
2253 // canonicalize constant to RHS
2254 if (N0CFP && !N1CFP)
2255 return DAG.getNode(ISD::FMUL, VT, N1, N0);
2256 // fold (fmul X, 2.0) -> (fadd X, X)
2257 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2258 return DAG.getNode(ISD::FADD, VT, N0, N0);
2262 SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2263 SDOperand N0 = N->getOperand(0);
2264 SDOperand N1 = N->getOperand(1);
2265 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2266 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2267 MVT::ValueType VT = N->getValueType(0);
2269 // fold (fdiv c1, c2) -> c1/c2
2271 return DAG.getNode(ISD::FDIV, VT, N0, N1);
2275 SDOperand DAGCombiner::visitFREM(SDNode *N) {
2276 SDOperand N0 = N->getOperand(0);
2277 SDOperand N1 = N->getOperand(1);
2278 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2279 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2280 MVT::ValueType VT = N->getValueType(0);
2282 // fold (frem c1, c2) -> fmod(c1,c2)
2284 return DAG.getNode(ISD::FREM, VT, N0, N1);
2288 SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2289 SDOperand N0 = N->getOperand(0);
2290 SDOperand N1 = N->getOperand(1);
2291 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2292 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2293 MVT::ValueType VT = N->getValueType(0);
2295 if (N0CFP && N1CFP) // Constant fold
2296 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2299 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2300 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2305 u.d = N1CFP->getValue();
2307 return DAG.getNode(ISD::FABS, VT, N0);
2309 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2312 // copysign(fabs(x), y) -> copysign(x, y)
2313 // copysign(fneg(x), y) -> copysign(x, y)
2314 // copysign(copysign(x,z), y) -> copysign(x, y)
2315 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2316 N0.getOpcode() == ISD::FCOPYSIGN)
2317 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2319 // copysign(x, abs(y)) -> abs(x)
2320 if (N1.getOpcode() == ISD::FABS)
2321 return DAG.getNode(ISD::FABS, VT, N0);
2323 // copysign(x, copysign(y,z)) -> copysign(x, z)
2324 if (N1.getOpcode() == ISD::FCOPYSIGN)
2325 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2327 // copysign(x, fp_extend(y)) -> copysign(x, y)
2328 // copysign(x, fp_round(y)) -> copysign(x, y)
2329 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2330 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2337 SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
2338 SDOperand N0 = N->getOperand(0);
2339 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2340 MVT::ValueType VT = N->getValueType(0);
2342 // fold (sint_to_fp c1) -> c1fp
2344 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
2348 SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
2349 SDOperand N0 = N->getOperand(0);
2350 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2351 MVT::ValueType VT = N->getValueType(0);
2353 // fold (uint_to_fp c1) -> c1fp
2355 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
2359 SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
2360 SDOperand N0 = N->getOperand(0);
2361 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2362 MVT::ValueType VT = N->getValueType(0);
2364 // fold (fp_to_sint c1fp) -> c1
2366 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
2370 SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
2371 SDOperand N0 = N->getOperand(0);
2372 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2373 MVT::ValueType VT = N->getValueType(0);
2375 // fold (fp_to_uint c1fp) -> c1
2377 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
2381 SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
2382 SDOperand N0 = N->getOperand(0);
2383 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2384 MVT::ValueType VT = N->getValueType(0);
2386 // fold (fp_round c1fp) -> c1fp
2388 return DAG.getNode(ISD::FP_ROUND, VT, N0);
2390 // fold (fp_round (fp_extend x)) -> x
2391 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2392 return N0.getOperand(0);
2394 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2395 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2396 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2397 AddToWorkList(Tmp.Val);
2398 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2404 SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
2405 SDOperand N0 = N->getOperand(0);
2406 MVT::ValueType VT = N->getValueType(0);
2407 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2408 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2410 // fold (fp_round_inreg c1fp) -> c1fp
2412 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
2413 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
2418 SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
2419 SDOperand N0 = N->getOperand(0);
2420 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2421 MVT::ValueType VT = N->getValueType(0);
2423 // fold (fp_extend c1fp) -> c1fp
2425 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
2429 SDOperand DAGCombiner::visitFNEG(SDNode *N) {
2430 SDOperand N0 = N->getOperand(0);
2431 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2432 MVT::ValueType VT = N->getValueType(0);
2434 // fold (fneg c1) -> -c1
2436 return DAG.getNode(ISD::FNEG, VT, N0);
2437 // fold (fneg (sub x, y)) -> (sub y, x)
2438 if (N0.getOpcode() == ISD::SUB)
2439 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
2440 // fold (fneg (fneg x)) -> x
2441 if (N0.getOpcode() == ISD::FNEG)
2442 return N0.getOperand(0);
2446 SDOperand DAGCombiner::visitFABS(SDNode *N) {
2447 SDOperand N0 = N->getOperand(0);
2448 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2449 MVT::ValueType VT = N->getValueType(0);
2451 // fold (fabs c1) -> fabs(c1)
2453 return DAG.getNode(ISD::FABS, VT, N0);
2454 // fold (fabs (fabs x)) -> (fabs x)
2455 if (N0.getOpcode() == ISD::FABS)
2456 return N->getOperand(0);
2457 // fold (fabs (fneg x)) -> (fabs x)
2458 // fold (fabs (fcopysign x, y)) -> (fabs x)
2459 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2460 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2465 SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2466 SDOperand Chain = N->getOperand(0);
2467 SDOperand N1 = N->getOperand(1);
2468 SDOperand N2 = N->getOperand(2);
2469 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2471 // never taken branch, fold to chain
2472 if (N1C && N1C->isNullValue())
2474 // unconditional branch
2475 if (N1C && N1C->getValue() == 1)
2476 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2477 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2479 if (N1.getOpcode() == ISD::SETCC &&
2480 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2481 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2482 N1.getOperand(0), N1.getOperand(1), N2);
2487 // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2489 SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
2490 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2491 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2493 // Use SimplifySetCC to simplify SETCC's.
2494 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2495 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2497 // fold br_cc true, dest -> br dest (unconditional branch)
2498 if (SCCC && SCCC->getValue())
2499 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2501 // fold br_cc false, dest -> unconditional fall through
2502 if (SCCC && SCCC->isNullValue())
2503 return N->getOperand(0);
2504 // fold to a simpler setcc
2505 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2506 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2507 Simp.getOperand(2), Simp.getOperand(0),
2508 Simp.getOperand(1), N->getOperand(4));
2512 SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2513 SDOperand Chain = N->getOperand(0);
2514 SDOperand Ptr = N->getOperand(1);
2515 SDOperand SrcValue = N->getOperand(2);
2517 // If there are no uses of the loaded value, change uses of the chain value
2518 // into uses of the chain input (i.e. delete the dead load).
2519 if (N->hasNUsesOfValue(0, 0))
2520 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2522 // If this load is directly stored, replace the load value with the stored
2524 // TODO: Handle store large -> read small portion.
2525 // TODO: Handle TRUNCSTORE/EXTLOAD
2526 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2527 Chain.getOperand(1).getValueType() == N->getValueType(0))
2528 return CombineTo(N, Chain.getOperand(1), Chain);
2533 /// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2534 SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2535 SDOperand Chain = N->getOperand(0);
2536 SDOperand Ptr = N->getOperand(1);
2537 SDOperand SrcValue = N->getOperand(2);
2538 SDOperand EVT = N->getOperand(3);
2540 // If there are no uses of the loaded value, change uses of the chain value
2541 // into uses of the chain input (i.e. delete the dead load).
2542 if (N->hasNUsesOfValue(0, 0))
2543 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2548 SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2549 SDOperand Chain = N->getOperand(0);
2550 SDOperand Value = N->getOperand(1);
2551 SDOperand Ptr = N->getOperand(2);
2552 SDOperand SrcValue = N->getOperand(3);
2554 // If this is a store that kills a previous store, remove the previous store.
2555 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2556 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2557 // Make sure that these stores are the same value type:
2558 // FIXME: we really care that the second store is >= size of the first.
2559 Value.getValueType() == Chain.getOperand(1).getValueType()) {
2560 // Create a new store of Value that replaces both stores.
2561 SDNode *PrevStore = Chain.Val;
2562 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2564 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2565 PrevStore->getOperand(0), Value, Ptr,
2567 CombineTo(N, NewStore); // Nuke this store.
2568 CombineTo(PrevStore, NewStore); // Nuke the previous store.
2569 return SDOperand(N, 0);
2572 // If this is a store of a bit convert, store the input value.
2573 // FIXME: This needs to know that the resultant store does not need a
2574 // higher alignment than the original.
2575 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
2576 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2582 SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2583 SDOperand InVec = N->getOperand(0);
2584 SDOperand InVal = N->getOperand(1);
2585 SDOperand EltNo = N->getOperand(2);
2587 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2588 // vector with the inserted element.
2589 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2590 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2591 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2592 if (Elt < Ops.size())
2594 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), Ops);
2600 SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2601 SDOperand InVec = N->getOperand(0);
2602 SDOperand InVal = N->getOperand(1);
2603 SDOperand EltNo = N->getOperand(2);
2604 SDOperand NumElts = N->getOperand(3);
2605 SDOperand EltType = N->getOperand(4);
2607 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2608 // vector with the inserted element.
2609 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2610 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2611 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2612 if (Elt < Ops.size()-2)
2614 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), Ops);
2620 SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2621 unsigned NumInScalars = N->getNumOperands()-2;
2622 SDOperand NumElts = N->getOperand(NumInScalars);
2623 SDOperand EltType = N->getOperand(NumInScalars+1);
2625 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2626 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2627 // two distinct vectors, turn this into a shuffle node.
2628 SDOperand VecIn1, VecIn2;
2629 for (unsigned i = 0; i != NumInScalars; ++i) {
2630 // Ignore undef inputs.
2631 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2633 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2634 // constant index, bail out.
2635 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2636 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2637 VecIn1 = VecIn2 = SDOperand(0, 0);
2641 // If the input vector type disagrees with the result of the vbuild_vector,
2642 // we can't make a shuffle.
2643 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2644 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2645 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2646 VecIn1 = VecIn2 = SDOperand(0, 0);
2650 // Otherwise, remember this. We allow up to two distinct input vectors.
2651 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2654 if (VecIn1.Val == 0) {
2655 VecIn1 = ExtractedFromVec;
2656 } else if (VecIn2.Val == 0) {
2657 VecIn2 = ExtractedFromVec;
2660 VecIn1 = VecIn2 = SDOperand(0, 0);
2665 // If everything is good, we can make a shuffle operation.
2667 std::vector<SDOperand> BuildVecIndices;
2668 for (unsigned i = 0; i != NumInScalars; ++i) {
2669 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2670 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2674 SDOperand Extract = N->getOperand(i);
2676 // If extracting from the first vector, just use the index directly.
2677 if (Extract.getOperand(0) == VecIn1) {
2678 BuildVecIndices.push_back(Extract.getOperand(1));
2682 // Otherwise, use InIdx + VecSize
2683 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2684 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2687 // Add count and size info.
2688 BuildVecIndices.push_back(NumElts);
2689 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2691 // Return the new VVECTOR_SHUFFLE node.
2692 std::vector<SDOperand> Ops;
2693 Ops.push_back(VecIn1);
2695 Ops.push_back(VecIn2);
2697 // Use an undef vbuild_vector as input for the second operand.
2698 std::vector<SDOperand> UnOps(NumInScalars,
2699 DAG.getNode(ISD::UNDEF,
2700 cast<VTSDNode>(EltType)->getVT()));
2701 UnOps.push_back(NumElts);
2702 UnOps.push_back(EltType);
2703 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, UnOps));
2704 AddToWorkList(Ops.back().Val);
2706 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR,MVT::Vector, BuildVecIndices));
2707 Ops.push_back(NumElts);
2708 Ops.push_back(EltType);
2709 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2715 SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
2716 SDOperand ShufMask = N->getOperand(2);
2717 unsigned NumElts = ShufMask.getNumOperands();
2719 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2720 bool isIdentity = true;
2721 for (unsigned i = 0; i != NumElts; ++i) {
2722 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2723 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2728 if (isIdentity) return N->getOperand(0);
2730 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2732 for (unsigned i = 0; i != NumElts; ++i) {
2733 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2734 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2739 if (isIdentity) return N->getOperand(1);
2741 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2742 if (N->getOperand(0) == N->getOperand(1)) {
2743 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
2744 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
2745 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2747 std::vector<SDOperand> MappedOps;
2748 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
2749 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2750 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2751 MappedOps.push_back(ShufMask.getOperand(i));
2754 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2755 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2758 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
2760 AddToWorkList(ShufMask.Val);
2761 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
2763 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2770 SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2771 SDOperand ShufMask = N->getOperand(2);
2772 unsigned NumElts = ShufMask.getNumOperands()-2;
2774 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2775 bool isIdentity = true;
2776 for (unsigned i = 0; i != NumElts; ++i) {
2777 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2778 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2783 if (isIdentity) return N->getOperand(0);
2785 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2787 for (unsigned i = 0; i != NumElts; ++i) {
2788 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2789 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2794 if (isIdentity) return N->getOperand(1);
2796 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2797 if (N->getOperand(0) == N->getOperand(1)) {
2798 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2800 std::vector<SDOperand> MappedOps;
2801 for (unsigned i = 0; i != NumElts; ++i) {
2802 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2803 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2804 MappedOps.push_back(ShufMask.getOperand(i));
2807 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2808 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2811 // Add the type/#elts values.
2812 MappedOps.push_back(ShufMask.getOperand(NumElts));
2813 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
2815 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
2817 AddToWorkList(ShufMask.Val);
2819 // Build the undef vector.
2820 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
2821 for (unsigned i = 0; i != NumElts; ++i)
2822 MappedOps[i] = UDVal;
2823 MappedOps[NumElts ] = *(N->getOperand(0).Val->op_end()-2);
2824 MappedOps[NumElts+1] = *(N->getOperand(0).Val->op_end()-1);
2825 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, MappedOps);
2827 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
2828 N->getOperand(0), UDVal, ShufMask,
2829 MappedOps[NumElts], MappedOps[NumElts+1]);
2835 /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
2836 /// a VAND to a vector_shuffle with the destination vector and a zero vector.
2837 /// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
2838 /// vector_shuffle V, Zero, <0, 4, 2, 4>
2839 SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
2840 SDOperand LHS = N->getOperand(0);
2841 SDOperand RHS = N->getOperand(1);
2842 if (N->getOpcode() == ISD::VAND) {
2843 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
2844 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
2845 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
2846 RHS = RHS.getOperand(0);
2847 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2848 std::vector<SDOperand> IdxOps;
2849 unsigned NumOps = RHS.getNumOperands();
2850 unsigned NumElts = NumOps-2;
2851 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
2852 for (unsigned i = 0; i != NumElts; ++i) {
2853 SDOperand Elt = RHS.getOperand(i);
2854 if (!isa<ConstantSDNode>(Elt))
2856 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
2857 IdxOps.push_back(DAG.getConstant(i, EVT));
2858 else if (cast<ConstantSDNode>(Elt)->isNullValue())
2859 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
2864 // Let's see if the target supports this vector_shuffle.
2865 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
2868 // Return the new VVECTOR_SHUFFLE node.
2869 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
2870 SDOperand EVTNode = DAG.getValueType(EVT);
2871 std::vector<SDOperand> Ops;
2872 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode, EVTNode);
2874 AddToWorkList(LHS.Val);
2875 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
2876 ZeroOps.push_back(NumEltsNode);
2877 ZeroOps.push_back(EVTNode);
2878 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, ZeroOps));
2879 IdxOps.push_back(NumEltsNode);
2880 IdxOps.push_back(EVTNode);
2881 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, IdxOps));
2882 Ops.push_back(NumEltsNode);
2883 Ops.push_back(EVTNode);
2884 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2885 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
2886 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
2887 DstVecSize, DstVecEVT);
2895 /// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
2896 /// the scalar operation of the vop if it is operating on an integer vector
2897 /// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
2898 SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
2899 ISD::NodeType FPOp) {
2900 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
2901 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
2902 SDOperand LHS = N->getOperand(0);
2903 SDOperand RHS = N->getOperand(1);
2904 SDOperand Shuffle = XformToShuffleWithZero(N);
2905 if (Shuffle.Val) return Shuffle;
2907 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
2909 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
2910 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2911 std::vector<SDOperand> Ops;
2912 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
2913 SDOperand LHSOp = LHS.getOperand(i);
2914 SDOperand RHSOp = RHS.getOperand(i);
2915 // If these two elements can't be folded, bail out.
2916 if ((LHSOp.getOpcode() != ISD::UNDEF &&
2917 LHSOp.getOpcode() != ISD::Constant &&
2918 LHSOp.getOpcode() != ISD::ConstantFP) ||
2919 (RHSOp.getOpcode() != ISD::UNDEF &&
2920 RHSOp.getOpcode() != ISD::Constant &&
2921 RHSOp.getOpcode() != ISD::ConstantFP))
2923 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
2924 AddToWorkList(Ops.back().Val);
2925 assert((Ops.back().getOpcode() == ISD::UNDEF ||
2926 Ops.back().getOpcode() == ISD::Constant ||
2927 Ops.back().getOpcode() == ISD::ConstantFP) &&
2928 "Scalar binop didn't fold!");
2931 if (Ops.size() == LHS.getNumOperands()-2) {
2932 Ops.push_back(*(LHS.Val->op_end()-2));
2933 Ops.push_back(*(LHS.Val->op_end()-1));
2934 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2941 SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
2942 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2944 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2945 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2946 // If we got a simplified select_cc node back from SimplifySelectCC, then
2947 // break it down into a new SETCC node, and a new SELECT node, and then return
2948 // the SELECT node, since we were called with a SELECT node.
2950 // Check to see if we got a select_cc back (to turn into setcc/select).
2951 // Otherwise, just return whatever node we got back, like fabs.
2952 if (SCC.getOpcode() == ISD::SELECT_CC) {
2953 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2954 SCC.getOperand(0), SCC.getOperand(1),
2956 AddToWorkList(SETCC.Val);
2957 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2958 SCC.getOperand(3), SETCC);
2965 /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2966 /// are the two values being selected between, see if we can simplify the
2969 bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2972 // If this is a select from two identical things, try to pull the operation
2973 // through the select.
2974 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2976 std::cerr << "SELECT: ["; LHS.Val->dump();
2977 std::cerr << "] ["; RHS.Val->dump();
2981 // If this is a load and the token chain is identical, replace the select
2982 // of two loads with a load through a select of the address to load from.
2983 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2984 // constants have been dropped into the constant pool.
2985 if ((LHS.getOpcode() == ISD::LOAD ||
2986 LHS.getOpcode() == ISD::EXTLOAD ||
2987 LHS.getOpcode() == ISD::ZEXTLOAD ||
2988 LHS.getOpcode() == ISD::SEXTLOAD) &&
2989 // Token chains must be identical.
2990 LHS.getOperand(0) == RHS.getOperand(0) &&
2991 // If this is an EXTLOAD, the VT's must match.
2992 (LHS.getOpcode() == ISD::LOAD ||
2993 LHS.getOperand(3) == RHS.getOperand(3))) {
2994 // FIXME: this conflates two src values, discarding one. This is not
2995 // the right thing to do, but nothing uses srcvalues now. When they do,
2996 // turn SrcValue into a list of locations.
2998 if (TheSelect->getOpcode() == ISD::SELECT)
2999 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3000 TheSelect->getOperand(0), LHS.getOperand(1),
3003 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3004 TheSelect->getOperand(0),
3005 TheSelect->getOperand(1),
3006 LHS.getOperand(1), RHS.getOperand(1),
3007 TheSelect->getOperand(4));
3010 if (LHS.getOpcode() == ISD::LOAD)
3011 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3012 Addr, LHS.getOperand(2));
3014 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
3015 LHS.getOperand(0), Addr, LHS.getOperand(2),
3016 cast<VTSDNode>(LHS.getOperand(3))->getVT());
3017 // Users of the select now use the result of the load.
3018 CombineTo(TheSelect, Load);
3020 // Users of the old loads now use the new load's chain. We know the
3021 // old-load value is dead now.
3022 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3023 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3031 SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3032 SDOperand N2, SDOperand N3,
3035 MVT::ValueType VT = N2.getValueType();
3036 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
3037 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3038 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3039 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3041 // Determine if the condition we're dealing with is constant
3042 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3043 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3045 // fold select_cc true, x, y -> x
3046 if (SCCC && SCCC->getValue())
3048 // fold select_cc false, x, y -> y
3049 if (SCCC && SCCC->getValue() == 0)
3052 // Check to see if we can simplify the select into an fabs node
3053 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3054 // Allow either -0.0 or 0.0
3055 if (CFP->getValue() == 0.0) {
3056 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3057 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3058 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3059 N2 == N3.getOperand(0))
3060 return DAG.getNode(ISD::FABS, VT, N0);
3062 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3063 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3064 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3065 N2.getOperand(0) == N3)
3066 return DAG.getNode(ISD::FABS, VT, N3);
3070 // Check to see if we can perform the "gzip trick", transforming
3071 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
3072 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
3073 MVT::isInteger(N0.getValueType()) &&
3074 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
3075 MVT::ValueType XType = N0.getValueType();
3076 MVT::ValueType AType = N2.getValueType();
3077 if (XType >= AType) {
3078 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
3079 // single-bit constant.
3080 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3081 unsigned ShCtV = Log2_64(N2C->getValue());
3082 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3083 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3084 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
3085 AddToWorkList(Shift.Val);
3086 if (XType > AType) {
3087 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
3088 AddToWorkList(Shift.Val);
3090 return DAG.getNode(ISD::AND, AType, Shift, N2);
3092 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3093 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3094 TLI.getShiftAmountTy()));
3095 AddToWorkList(Shift.Val);
3096 if (XType > AType) {
3097 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
3098 AddToWorkList(Shift.Val);
3100 return DAG.getNode(ISD::AND, AType, Shift, N2);
3104 // fold select C, 16, 0 -> shl C, 4
3105 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3106 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3107 // Get a SetCC of the condition
3108 // FIXME: Should probably make sure that setcc is legal if we ever have a
3109 // target where it isn't.
3110 SDOperand Temp, SCC;
3111 // cast from setcc result type to select result type
3112 if (AfterLegalize) {
3113 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3114 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
3116 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
3117 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
3119 AddToWorkList(SCC.Val);
3120 AddToWorkList(Temp.Val);
3121 // shl setcc result by log2 n2c
3122 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3123 DAG.getConstant(Log2_64(N2C->getValue()),
3124 TLI.getShiftAmountTy()));
3127 // Check to see if this is the equivalent of setcc
3128 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3129 // otherwise, go ahead with the folds.
3130 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3131 MVT::ValueType XType = N0.getValueType();
3132 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3133 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3134 if (Res.getValueType() != VT)
3135 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3139 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3140 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3141 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3142 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3143 return DAG.getNode(ISD::SRL, XType, Ctlz,
3144 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3145 TLI.getShiftAmountTy()));
3147 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3148 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3149 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3151 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3152 DAG.getConstant(~0ULL, XType));
3153 return DAG.getNode(ISD::SRL, XType,
3154 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3155 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3156 TLI.getShiftAmountTy()));
3158 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3159 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3160 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3161 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3162 TLI.getShiftAmountTy()));
3163 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3167 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3168 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3169 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3170 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3171 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3172 MVT::ValueType XType = N0.getValueType();
3173 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3174 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3175 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3176 TLI.getShiftAmountTy()));
3177 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
3178 AddToWorkList(Shift.Val);
3179 AddToWorkList(Add.Val);
3180 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3188 SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
3189 SDOperand N1, ISD::CondCode Cond,
3190 bool foldBooleans) {
3191 // These setcc operations always fold.
3195 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3197 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3200 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3201 uint64_t C1 = N1C->getValue();
3202 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3203 uint64_t C0 = N0C->getValue();
3205 // Sign extend the operands if required
3206 if (ISD::isSignedIntSetCC(Cond)) {
3207 C0 = N0C->getSignExtended();
3208 C1 = N1C->getSignExtended();
3212 default: assert(0 && "Unknown integer setcc!");
3213 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3214 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3215 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3216 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3217 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3218 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3219 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3220 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3221 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3222 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3225 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3226 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3227 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3229 // If the comparison constant has bits in the upper part, the
3230 // zero-extended value could never match.
3231 if (C1 & (~0ULL << InSize)) {
3232 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3236 case ISD::SETEQ: return DAG.getConstant(0, VT);
3239 case ISD::SETNE: return DAG.getConstant(1, VT);
3242 // True if the sign bit of C1 is set.
3243 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3246 // True if the sign bit of C1 isn't set.
3247 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3253 // Otherwise, we can perform the comparison with the low bits.
3261 return DAG.getSetCC(VT, N0.getOperand(0),
3262 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3265 break; // todo, be more careful with signed comparisons
3267 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3268 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3269 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3270 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3271 MVT::ValueType ExtDstTy = N0.getValueType();
3272 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3274 // If the extended part has any inconsistent bits, it cannot ever
3275 // compare equal. In other words, they have to be all ones or all
3278 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3279 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3280 return DAG.getConstant(Cond == ISD::SETNE, VT);
3283 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3284 if (Op0Ty == ExtSrcTy) {
3285 ZextOp = N0.getOperand(0);
3287 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3288 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3289 DAG.getConstant(Imm, Op0Ty));
3291 AddToWorkList(ZextOp.Val);
3292 // Otherwise, make this a use of a zext.
3293 return DAG.getSetCC(VT, ZextOp,
3294 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3297 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3298 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3299 (N0.getOpcode() == ISD::XOR ||
3300 (N0.getOpcode() == ISD::AND &&
3301 N0.getOperand(0).getOpcode() == ISD::XOR &&
3302 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3303 isa<ConstantSDNode>(N0.getOperand(1)) &&
3304 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3305 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3306 // only do this if the top bits are known zero.
3307 if (TLI.MaskedValueIsZero(N1,
3308 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3309 // Okay, get the un-inverted input value.
3311 if (N0.getOpcode() == ISD::XOR)
3312 Val = N0.getOperand(0);
3314 assert(N0.getOpcode() == ISD::AND &&
3315 N0.getOperand(0).getOpcode() == ISD::XOR);
3316 // ((X^1)&1)^1 -> X & 1
3317 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3318 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3320 return DAG.getSetCC(VT, Val, N1,
3321 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3325 uint64_t MinVal, MaxVal;
3326 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3327 if (ISD::isSignedIntSetCC(Cond)) {
3328 MinVal = 1ULL << (OperandBitSize-1);
3329 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3330 MaxVal = ~0ULL >> (65-OperandBitSize);
3335 MaxVal = ~0ULL >> (64-OperandBitSize);
3338 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3339 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3340 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3341 --C1; // X >= C0 --> X > (C0-1)
3342 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3343 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3346 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3347 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3348 ++C1; // X <= C0 --> X < (C0+1)
3349 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3350 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3353 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3354 return DAG.getConstant(0, VT); // X < MIN --> false
3356 // Canonicalize setgt X, Min --> setne X, Min
3357 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3358 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
3359 // Canonicalize setlt X, Max --> setne X, Max
3360 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3361 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
3363 // If we have setult X, 1, turn it into seteq X, 0
3364 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3365 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3367 // If we have setugt X, Max-1, turn it into seteq X, Max
3368 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3369 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3372 // If we have "setcc X, C0", check to see if we can shrink the immediate
3375 // SETUGT X, SINTMAX -> SETLT X, 0
3376 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3377 C1 == (~0ULL >> (65-OperandBitSize)))
3378 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3381 // FIXME: Implement the rest of these.
3383 // Fold bit comparisons when we can.
3384 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3385 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3386 if (ConstantSDNode *AndRHS =
3387 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3388 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3389 // Perform the xform if the AND RHS is a single bit.
3390 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3391 return DAG.getNode(ISD::SRL, VT, N0,
3392 DAG.getConstant(Log2_64(AndRHS->getValue()),
3393 TLI.getShiftAmountTy()));
3395 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3396 // (X & 8) == 8 --> (X & 8) >> 3
3397 // Perform the xform if C1 is a single bit.
3398 if ((C1 & (C1-1)) == 0) {
3399 return DAG.getNode(ISD::SRL, VT, N0,
3400 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
3405 } else if (isa<ConstantSDNode>(N0.Val)) {
3406 // Ensure that the constant occurs on the RHS.
3407 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3410 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3411 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3412 double C0 = N0C->getValue(), C1 = N1C->getValue();
3415 default: break; // FIXME: Implement the rest of these!
3416 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3417 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3418 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3419 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3420 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3421 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3424 // Ensure that the constant occurs on the RHS.
3425 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3429 // We can always fold X == Y for integer setcc's.
3430 if (MVT::isInteger(N0.getValueType()))
3431 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3432 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3433 if (UOF == 2) // FP operators that are undefined on NaNs.
3434 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3435 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3436 return DAG.getConstant(UOF, VT);
3437 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3438 // if it is not already.
3439 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
3440 if (NewCond != Cond)
3441 return DAG.getSetCC(VT, N0, N1, NewCond);
3444 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3445 MVT::isInteger(N0.getValueType())) {
3446 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3447 N0.getOpcode() == ISD::XOR) {
3448 // Simplify (X+Y) == (X+Z) --> Y == Z
3449 if (N0.getOpcode() == N1.getOpcode()) {
3450 if (N0.getOperand(0) == N1.getOperand(0))
3451 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3452 if (N0.getOperand(1) == N1.getOperand(1))
3453 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3454 if (isCommutativeBinOp(N0.getOpcode())) {
3455 // If X op Y == Y op X, try other combinations.
3456 if (N0.getOperand(0) == N1.getOperand(1))
3457 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3458 if (N0.getOperand(1) == N1.getOperand(0))
3459 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
3463 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3464 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3465 // Turn (X+C1) == C2 --> X == C2-C1
3466 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3467 return DAG.getSetCC(VT, N0.getOperand(0),
3468 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3469 N0.getValueType()), Cond);
3472 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3473 if (N0.getOpcode() == ISD::XOR)
3474 // If we know that all of the inverted bits are zero, don't bother
3475 // performing the inversion.
3476 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
3477 return DAG.getSetCC(VT, N0.getOperand(0),
3478 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
3479 N0.getValueType()), Cond);
3482 // Turn (C1-X) == C2 --> X == C1-C2
3483 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3484 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3485 return DAG.getSetCC(VT, N0.getOperand(1),
3486 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3487 N0.getValueType()), Cond);
3492 // Simplify (X+Z) == X --> Z == 0
3493 if (N0.getOperand(0) == N1)
3494 return DAG.getSetCC(VT, N0.getOperand(1),
3495 DAG.getConstant(0, N0.getValueType()), Cond);
3496 if (N0.getOperand(1) == N1) {
3497 if (isCommutativeBinOp(N0.getOpcode()))
3498 return DAG.getSetCC(VT, N0.getOperand(0),
3499 DAG.getConstant(0, N0.getValueType()), Cond);
3501 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3502 // (Z-X) == X --> Z == X<<1
3503 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3505 DAG.getConstant(1,TLI.getShiftAmountTy()));
3506 AddToWorkList(SH.Val);
3507 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3512 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3513 N1.getOpcode() == ISD::XOR) {
3514 // Simplify X == (X+Z) --> Z == 0
3515 if (N1.getOperand(0) == N0) {
3516 return DAG.getSetCC(VT, N1.getOperand(1),
3517 DAG.getConstant(0, N1.getValueType()), Cond);
3518 } else if (N1.getOperand(1) == N0) {
3519 if (isCommutativeBinOp(N1.getOpcode())) {
3520 return DAG.getSetCC(VT, N1.getOperand(0),
3521 DAG.getConstant(0, N1.getValueType()), Cond);
3523 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3524 // X == (Z-X) --> X<<1 == Z
3525 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3526 DAG.getConstant(1,TLI.getShiftAmountTy()));
3527 AddToWorkList(SH.Val);
3528 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3534 // Fold away ALL boolean setcc's.
3536 if (N0.getValueType() == MVT::i1 && foldBooleans) {
3538 default: assert(0 && "Unknown integer setcc!");
3539 case ISD::SETEQ: // X == Y -> (X^Y)^1
3540 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3541 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
3542 AddToWorkList(Temp.Val);
3544 case ISD::SETNE: // X != Y --> (X^Y)
3545 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3547 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3548 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3549 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3550 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
3551 AddToWorkList(Temp.Val);
3553 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3554 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3555 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3556 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
3557 AddToWorkList(Temp.Val);
3559 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3560 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3561 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3562 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
3563 AddToWorkList(Temp.Val);
3565 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3566 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3567 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3568 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3571 if (VT != MVT::i1) {
3572 AddToWorkList(N0.Val);
3573 // FIXME: If running after legalize, we probably can't do this.
3574 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3579 // Could not fold it.
3583 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3584 /// return a DAG expression to select that will generate the same value by
3585 /// multiplying by a magic number. See:
3586 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3587 SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
3588 MVT::ValueType VT = N->getValueType(0);
3590 // Check to see if we can do this.
3591 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3592 return SDOperand(); // BuildSDIV only operates on i32 or i64
3593 if (!TLI.isOperationLegal(ISD::MULHS, VT))
3594 return SDOperand(); // Make sure the target supports MULHS.
3596 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
3597 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
3599 // Multiply the numerator (operand 0) by the magic value
3600 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
3601 DAG.getConstant(magics.m, VT));
3602 // If d > 0 and m < 0, add the numerator
3603 if (d > 0 && magics.m < 0) {
3604 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
3605 AddToWorkList(Q.Val);
3607 // If d < 0 and m > 0, subtract the numerator.
3608 if (d < 0 && magics.m > 0) {
3609 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
3610 AddToWorkList(Q.Val);
3612 // Shift right algebraic if shift value is nonzero
3614 Q = DAG.getNode(ISD::SRA, VT, Q,
3615 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3616 AddToWorkList(Q.Val);
3618 // Extract the sign bit and add it to the quotient
3620 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
3621 TLI.getShiftAmountTy()));
3622 AddToWorkList(T.Val);
3623 return DAG.getNode(ISD::ADD, VT, Q, T);
3626 /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3627 /// return a DAG expression to select that will generate the same value by
3628 /// multiplying by a magic number. See:
3629 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3630 SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3631 MVT::ValueType VT = N->getValueType(0);
3633 // Check to see if we can do this.
3634 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3635 return SDOperand(); // BuildUDIV only operates on i32 or i64
3636 if (!TLI.isOperationLegal(ISD::MULHU, VT))
3637 return SDOperand(); // Make sure the target supports MULHU.
3639 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
3640 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
3642 // Multiply the numerator (operand 0) by the magic value
3643 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
3644 DAG.getConstant(magics.m, VT));
3645 AddToWorkList(Q.Val);
3647 if (magics.a == 0) {
3648 return DAG.getNode(ISD::SRL, VT, Q,
3649 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3651 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
3652 AddToWorkList(NPQ.Val);
3653 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
3654 DAG.getConstant(1, TLI.getShiftAmountTy()));
3655 AddToWorkList(NPQ.Val);
3656 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
3657 AddToWorkList(NPQ.Val);
3658 return DAG.getNode(ISD::SRL, VT, NPQ,
3659 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
3663 // SelectionDAG::Combine - This is the entry point for the file.
3665 void SelectionDAG::Combine(bool RunningAfterLegalize) {
3666 /// run - This is the main entry point to this class.
3668 DAGCombiner(*this).Run(RunningAfterLegalize);