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 visitSIGN_EXTEND_INREG(SDNode *N);
192 SDOperand visitTRUNCATE(SDNode *N);
193 SDOperand visitBIT_CONVERT(SDNode *N);
194 SDOperand visitVBIT_CONVERT(SDNode *N);
195 SDOperand visitFADD(SDNode *N);
196 SDOperand visitFSUB(SDNode *N);
197 SDOperand visitFMUL(SDNode *N);
198 SDOperand visitFDIV(SDNode *N);
199 SDOperand visitFREM(SDNode *N);
200 SDOperand visitFCOPYSIGN(SDNode *N);
201 SDOperand visitSINT_TO_FP(SDNode *N);
202 SDOperand visitUINT_TO_FP(SDNode *N);
203 SDOperand visitFP_TO_SINT(SDNode *N);
204 SDOperand visitFP_TO_UINT(SDNode *N);
205 SDOperand visitFP_ROUND(SDNode *N);
206 SDOperand visitFP_ROUND_INREG(SDNode *N);
207 SDOperand visitFP_EXTEND(SDNode *N);
208 SDOperand visitFNEG(SDNode *N);
209 SDOperand visitFABS(SDNode *N);
210 SDOperand visitBRCOND(SDNode *N);
211 SDOperand visitBR_CC(SDNode *N);
212 SDOperand visitLOAD(SDNode *N);
213 SDOperand visitXEXTLOAD(SDNode *N);
214 SDOperand visitSTORE(SDNode *N);
215 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
216 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
217 SDOperand visitVBUILD_VECTOR(SDNode *N);
218 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
219 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
221 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
223 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
224 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
225 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
226 SDOperand N3, ISD::CondCode CC);
227 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
228 ISD::CondCode Cond, bool foldBooleans = true);
229 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
230 SDOperand BuildSDIV(SDNode *N);
231 SDOperand BuildUDIV(SDNode *N);
233 DAGCombiner(SelectionDAG &D)
234 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
236 /// Run - runs the dag combiner on all nodes in the work list
237 void Run(bool RunningAfterLegalize);
241 //===----------------------------------------------------------------------===//
242 // TargetLowering::DAGCombinerInfo implementation
243 //===----------------------------------------------------------------------===//
245 void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
246 ((DAGCombiner*)DC)->AddToWorkList(N);
249 SDOperand TargetLowering::DAGCombinerInfo::
250 CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
251 return ((DAGCombiner*)DC)->CombineTo(N, To);
254 SDOperand TargetLowering::DAGCombinerInfo::
255 CombineTo(SDNode *N, SDOperand Res) {
256 return ((DAGCombiner*)DC)->CombineTo(N, Res);
260 SDOperand TargetLowering::DAGCombinerInfo::
261 CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
262 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
268 //===----------------------------------------------------------------------===//
272 int64_t m; // magic number
273 int64_t s; // shift amount
277 uint64_t m; // magic number
278 int64_t a; // add indicator
279 int64_t s; // shift amount
282 /// magic - calculate the magic numbers required to codegen an integer sdiv as
283 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
285 static ms magic32(int32_t d) {
287 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
288 const uint32_t two31 = 0x80000000U;
292 t = two31 + ((uint32_t)d >> 31);
293 anc = t - 1 - t%ad; // absolute value of nc
294 p = 31; // initialize p
295 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
296 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
297 q2 = two31/ad; // initialize q2 = 2p/abs(d)
298 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
301 q1 = 2*q1; // update q1 = 2p/abs(nc)
302 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
303 if (r1 >= anc) { // must be unsigned comparison
307 q2 = 2*q2; // update q2 = 2p/abs(d)
308 r2 = 2*r2; // update r2 = rem(2p/abs(d))
309 if (r2 >= ad) { // must be unsigned comparison
314 } while (q1 < delta || (q1 == delta && r1 == 0));
316 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
317 if (d < 0) mag.m = -mag.m; // resulting magic number
318 mag.s = p - 32; // resulting shift
322 /// magicu - calculate the magic numbers required to codegen an integer udiv as
323 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
324 static mu magicu32(uint32_t d) {
326 uint32_t nc, delta, q1, r1, q2, r2;
328 magu.a = 0; // initialize "add" indicator
330 p = 31; // initialize p
331 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
332 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
333 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
334 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
337 if (r1 >= nc - r1 ) {
338 q1 = 2*q1 + 1; // update q1
339 r1 = 2*r1 - nc; // update r1
342 q1 = 2*q1; // update q1
343 r1 = 2*r1; // update r1
345 if (r2 + 1 >= d - r2) {
346 if (q2 >= 0x7FFFFFFF) magu.a = 1;
347 q2 = 2*q2 + 1; // update q2
348 r2 = 2*r2 + 1 - d; // update r2
351 if (q2 >= 0x80000000) magu.a = 1;
352 q2 = 2*q2; // update q2
353 r2 = 2*r2 + 1; // update r2
356 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
357 magu.m = q2 + 1; // resulting magic number
358 magu.s = p - 32; // resulting shift
362 /// magic - calculate the magic numbers required to codegen an integer sdiv as
363 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
365 static ms magic64(int64_t d) {
367 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
368 const uint64_t two63 = 9223372036854775808ULL; // 2^63
371 ad = d >= 0 ? d : -d;
372 t = two63 + ((uint64_t)d >> 63);
373 anc = t - 1 - t%ad; // absolute value of nc
374 p = 63; // initialize p
375 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
376 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
377 q2 = two63/ad; // initialize q2 = 2p/abs(d)
378 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
381 q1 = 2*q1; // update q1 = 2p/abs(nc)
382 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
383 if (r1 >= anc) { // must be unsigned comparison
387 q2 = 2*q2; // update q2 = 2p/abs(d)
388 r2 = 2*r2; // update r2 = rem(2p/abs(d))
389 if (r2 >= ad) { // must be unsigned comparison
394 } while (q1 < delta || (q1 == delta && r1 == 0));
397 if (d < 0) mag.m = -mag.m; // resulting magic number
398 mag.s = p - 64; // resulting shift
402 /// magicu - calculate the magic numbers required to codegen an integer udiv as
403 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
404 static mu magicu64(uint64_t d)
407 uint64_t nc, delta, q1, r1, q2, r2;
409 magu.a = 0; // initialize "add" indicator
411 p = 63; // initialize p
412 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
413 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
414 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
415 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
418 if (r1 >= nc - r1 ) {
419 q1 = 2*q1 + 1; // update q1
420 r1 = 2*r1 - nc; // update r1
423 q1 = 2*q1; // update q1
424 r1 = 2*r1; // update r1
426 if (r2 + 1 >= d - r2) {
427 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
428 q2 = 2*q2 + 1; // update q2
429 r2 = 2*r2 + 1 - d; // update r2
432 if (q2 >= 0x8000000000000000ull) magu.a = 1;
433 q2 = 2*q2; // update q2
434 r2 = 2*r2 + 1; // update r2
437 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
438 magu.m = q2 + 1; // resulting magic number
439 magu.s = p - 64; // resulting shift
443 // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
444 // that selects between the values 1 and 0, making it equivalent to a setcc.
445 // Also, set the incoming LHS, RHS, and CC references to the appropriate
446 // nodes based on the type of node we are checking. This simplifies life a
447 // bit for the callers.
448 static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
450 if (N.getOpcode() == ISD::SETCC) {
451 LHS = N.getOperand(0);
452 RHS = N.getOperand(1);
453 CC = N.getOperand(2);
456 if (N.getOpcode() == ISD::SELECT_CC &&
457 N.getOperand(2).getOpcode() == ISD::Constant &&
458 N.getOperand(3).getOpcode() == ISD::Constant &&
459 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
460 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
461 LHS = N.getOperand(0);
462 RHS = N.getOperand(1);
463 CC = N.getOperand(4);
469 // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
470 // one use. If this is true, it allows the users to invert the operation for
471 // free when it is profitable to do so.
472 static bool isOneUseSetCC(SDOperand N) {
473 SDOperand N0, N1, N2;
474 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
479 // FIXME: This should probably go in the ISD class rather than being duplicated
481 static bool isCommutativeBinOp(unsigned Opcode) {
487 case ISD::XOR: return true;
488 default: return false; // FIXME: Need commutative info for user ops!
492 SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
493 MVT::ValueType VT = N0.getValueType();
494 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
495 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
496 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
497 if (isa<ConstantSDNode>(N1)) {
498 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
499 AddToWorkList(OpNode.Val);
500 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
501 } else if (N0.hasOneUse()) {
502 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
503 AddToWorkList(OpNode.Val);
504 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
507 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
508 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
509 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
510 if (isa<ConstantSDNode>(N0)) {
511 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
512 AddToWorkList(OpNode.Val);
513 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
514 } else if (N1.hasOneUse()) {
515 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
516 AddToWorkList(OpNode.Val);
517 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
523 void DAGCombiner::Run(bool RunningAfterLegalize) {
524 // set the instance variable, so that the various visit routines may use it.
525 AfterLegalize = RunningAfterLegalize;
527 // Add all the dag nodes to the worklist.
528 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
529 E = DAG.allnodes_end(); I != E; ++I)
530 WorkList.push_back(I);
532 // Create a dummy node (which is not added to allnodes), that adds a reference
533 // to the root node, preventing it from being deleted, and tracking any
534 // changes of the root.
535 HandleSDNode Dummy(DAG.getRoot());
538 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
539 TargetLowering::DAGCombinerInfo
540 DagCombineInfo(DAG, !RunningAfterLegalize, this);
542 // while the worklist isn't empty, inspect the node on the end of it and
543 // try and combine it.
544 while (!WorkList.empty()) {
545 SDNode *N = WorkList.back();
548 // If N has no uses, it is dead. Make sure to revisit all N's operands once
549 // N is deleted from the DAG, since they too may now be dead or may have a
550 // reduced number of uses, allowing other xforms.
551 if (N->use_empty() && N != &Dummy) {
552 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
553 WorkList.push_back(N->getOperand(i).Val);
555 removeFromWorkList(N);
560 SDOperand RV = visit(N);
562 // If nothing happened, try a target-specific DAG combine.
564 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
565 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
566 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
571 // If we get back the same node we passed in, rather than a new node or
572 // zero, we know that the node must have defined multiple values and
573 // CombineTo was used. Since CombineTo takes care of the worklist
574 // mechanics for us, we have no work to do in this case.
576 DEBUG(std::cerr << "\nReplacing "; N->dump();
577 std::cerr << "\nWith: "; RV.Val->dump();
579 std::vector<SDNode*> NowDead;
580 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
582 // Push the new node and any users onto the worklist
583 WorkList.push_back(RV.Val);
584 AddUsersToWorkList(RV.Val);
586 // Nodes can end up on the worklist more than once. Make sure we do
587 // not process a node that has been replaced.
588 removeFromWorkList(N);
589 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
590 removeFromWorkList(NowDead[i]);
592 // Finally, since the node is now dead, remove it from the graph.
598 // If the root changed (e.g. it was a dead load, update the root).
599 DAG.setRoot(Dummy.getValue());
602 SDOperand DAGCombiner::visit(SDNode *N) {
603 switch(N->getOpcode()) {
605 case ISD::TokenFactor: return visitTokenFactor(N);
606 case ISD::ADD: return visitADD(N);
607 case ISD::SUB: return visitSUB(N);
608 case ISD::MUL: return visitMUL(N);
609 case ISD::SDIV: return visitSDIV(N);
610 case ISD::UDIV: return visitUDIV(N);
611 case ISD::SREM: return visitSREM(N);
612 case ISD::UREM: return visitUREM(N);
613 case ISD::MULHU: return visitMULHU(N);
614 case ISD::MULHS: return visitMULHS(N);
615 case ISD::AND: return visitAND(N);
616 case ISD::OR: return visitOR(N);
617 case ISD::XOR: return visitXOR(N);
618 case ISD::SHL: return visitSHL(N);
619 case ISD::SRA: return visitSRA(N);
620 case ISD::SRL: return visitSRL(N);
621 case ISD::CTLZ: return visitCTLZ(N);
622 case ISD::CTTZ: return visitCTTZ(N);
623 case ISD::CTPOP: return visitCTPOP(N);
624 case ISD::SELECT: return visitSELECT(N);
625 case ISD::SELECT_CC: return visitSELECT_CC(N);
626 case ISD::SETCC: return visitSETCC(N);
627 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
628 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
629 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
630 case ISD::TRUNCATE: return visitTRUNCATE(N);
631 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
632 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
633 case ISD::FADD: return visitFADD(N);
634 case ISD::FSUB: return visitFSUB(N);
635 case ISD::FMUL: return visitFMUL(N);
636 case ISD::FDIV: return visitFDIV(N);
637 case ISD::FREM: return visitFREM(N);
638 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
639 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
640 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
641 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
642 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
643 case ISD::FP_ROUND: return visitFP_ROUND(N);
644 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
645 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
646 case ISD::FNEG: return visitFNEG(N);
647 case ISD::FABS: return visitFABS(N);
648 case ISD::BRCOND: return visitBRCOND(N);
649 case ISD::BR_CC: return visitBR_CC(N);
650 case ISD::LOAD: return visitLOAD(N);
653 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
654 case ISD::STORE: return visitSTORE(N);
655 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
656 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
657 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
658 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
659 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
660 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
661 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
662 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
663 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
664 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
665 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
666 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
667 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
672 SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
673 std::vector<SDOperand> Ops;
674 bool Changed = false;
676 // If the token factor has two operands and one is the entry token, replace
677 // the token factor with the other operand.
678 if (N->getNumOperands() == 2) {
679 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
680 return N->getOperand(1);
681 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
682 return N->getOperand(0);
685 // fold (tokenfactor (tokenfactor)) -> tokenfactor
686 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
687 SDOperand Op = N->getOperand(i);
688 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
689 AddToWorkList(Op.Val); // Remove dead node.
691 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
692 Ops.push_back(Op.getOperand(j));
698 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
702 SDOperand DAGCombiner::visitADD(SDNode *N) {
703 SDOperand N0 = N->getOperand(0);
704 SDOperand N1 = N->getOperand(1);
705 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
706 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
707 MVT::ValueType VT = N0.getValueType();
709 // fold (add c1, c2) -> c1+c2
711 return DAG.getNode(ISD::ADD, VT, N0, N1);
712 // canonicalize constant to RHS
714 return DAG.getNode(ISD::ADD, VT, N1, N0);
715 // fold (add x, 0) -> x
716 if (N1C && N1C->isNullValue())
718 // fold ((c1-A)+c2) -> (c1+c2)-A
719 if (N1C && N0.getOpcode() == ISD::SUB)
720 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
721 return DAG.getNode(ISD::SUB, VT,
722 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
725 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
728 // fold ((0-A) + B) -> B-A
729 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
730 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
731 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
732 // fold (A + (0-B)) -> A-B
733 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
734 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
735 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
736 // fold (A+(B-A)) -> B
737 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
738 return N1.getOperand(0);
740 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
743 // fold (a+b) -> (a|b) iff a and b share no bits.
744 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
745 uint64_t LHSZero, LHSOne;
746 uint64_t RHSZero, RHSOne;
747 uint64_t Mask = MVT::getIntVTBitMask(VT);
748 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
750 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
752 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
753 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
754 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
755 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
756 return DAG.getNode(ISD::OR, VT, N0, N1);
763 SDOperand DAGCombiner::visitSUB(SDNode *N) {
764 SDOperand N0 = N->getOperand(0);
765 SDOperand N1 = N->getOperand(1);
766 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
767 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
768 MVT::ValueType VT = N0.getValueType();
770 // fold (sub x, x) -> 0
772 return DAG.getConstant(0, N->getValueType(0));
773 // fold (sub c1, c2) -> c1-c2
775 return DAG.getNode(ISD::SUB, VT, N0, N1);
776 // fold (sub x, c) -> (add x, -c)
778 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
780 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
781 return N0.getOperand(1);
783 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
784 return N0.getOperand(0);
788 SDOperand DAGCombiner::visitMUL(SDNode *N) {
789 SDOperand N0 = N->getOperand(0);
790 SDOperand N1 = N->getOperand(1);
791 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
792 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
793 MVT::ValueType VT = N0.getValueType();
795 // fold (mul c1, c2) -> c1*c2
797 return DAG.getNode(ISD::MUL, VT, N0, N1);
798 // canonicalize constant to RHS
800 return DAG.getNode(ISD::MUL, VT, N1, N0);
801 // fold (mul x, 0) -> 0
802 if (N1C && N1C->isNullValue())
804 // fold (mul x, -1) -> 0-x
805 if (N1C && N1C->isAllOnesValue())
806 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
807 // fold (mul x, (1 << c)) -> x << c
808 if (N1C && isPowerOf2_64(N1C->getValue()))
809 return DAG.getNode(ISD::SHL, VT, N0,
810 DAG.getConstant(Log2_64(N1C->getValue()),
811 TLI.getShiftAmountTy()));
812 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
813 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
814 // FIXME: If the input is something that is easily negated (e.g. a
815 // single-use add), we should put the negate there.
816 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
817 DAG.getNode(ISD::SHL, VT, N0,
818 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
819 TLI.getShiftAmountTy())));
822 //These two might be better as:
823 // mul x, ((1 << c) + cn) -> (x << c) + (x * cn)
824 // where TargetInfo tells us cn is a cheap constant to multiply by
826 // fold (mul x, (1 << c) + 1) -> (x << c) + x
827 //FIXME: there should be a target hint to allow other constants based on
829 if (N1C && isPowerOf2_64(N1C->getSignExtended() - 1)) {
830 return DAG.getNode(ISD::ADD, VT,
831 DAG.getNode(ISD::SHL, VT, N0,
832 DAG.getConstant(Log2_64(N1C->getSignExtended() - 1),
833 TLI.getShiftAmountTy())),
836 // fold (mul x, (1 << c) - 1) -> (x << c) - x
837 //FIXME: there should be a target hint to allow other constants based on
838 // the expense of mul
839 if (N1C && isPowerOf2_64(N1C->getSignExtended() + 1)) {
840 return DAG.getNode(ISD::SUB, VT,
841 DAG.getNode(ISD::SHL, VT, N0,
842 DAG.getConstant(Log2_64(N1C->getSignExtended() + 1),
843 TLI.getShiftAmountTy())),
847 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
848 if (N1C && N0.getOpcode() == ISD::SHL &&
849 isa<ConstantSDNode>(N0.getOperand(1))) {
850 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
851 AddToWorkList(C3.Val);
852 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
855 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
858 SDOperand Sh(0,0), Y(0,0);
859 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
860 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
861 N0.Val->hasOneUse()) {
863 } else if (N1.getOpcode() == ISD::SHL &&
864 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
868 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
869 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
872 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
873 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
874 isa<ConstantSDNode>(N0.getOperand(1))) {
875 return DAG.getNode(ISD::ADD, VT,
876 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
877 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
881 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
887 SDOperand DAGCombiner::visitSDIV(SDNode *N) {
888 SDOperand N0 = N->getOperand(0);
889 SDOperand N1 = N->getOperand(1);
890 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
891 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
892 MVT::ValueType VT = N->getValueType(0);
894 // fold (sdiv c1, c2) -> c1/c2
895 if (N0C && N1C && !N1C->isNullValue())
896 return DAG.getNode(ISD::SDIV, VT, N0, N1);
897 // fold (sdiv X, 1) -> X
898 if (N1C && N1C->getSignExtended() == 1LL)
900 // fold (sdiv X, -1) -> 0-X
901 if (N1C && N1C->isAllOnesValue())
902 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
903 // If we know the sign bits of both operands are zero, strength reduce to a
904 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
905 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
906 if (TLI.MaskedValueIsZero(N1, SignBit) &&
907 TLI.MaskedValueIsZero(N0, SignBit))
908 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
909 // fold (sdiv X, pow2) -> simple ops after legalize
910 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
911 (isPowerOf2_64(N1C->getSignExtended()) ||
912 isPowerOf2_64(-N1C->getSignExtended()))) {
913 // If dividing by powers of two is cheap, then don't perform the following
915 if (TLI.isPow2DivCheap())
917 int64_t pow2 = N1C->getSignExtended();
918 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
919 unsigned lg2 = Log2_64(abs2);
920 // Splat the sign bit into the register
921 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
922 DAG.getConstant(MVT::getSizeInBits(VT)-1,
923 TLI.getShiftAmountTy()));
924 AddToWorkList(SGN.Val);
925 // Add (N0 < 0) ? abs2 - 1 : 0;
926 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
927 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
928 TLI.getShiftAmountTy()));
929 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
930 AddToWorkList(SRL.Val);
931 AddToWorkList(ADD.Val); // Divide by pow2
932 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
933 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
934 // If we're dividing by a positive value, we're done. Otherwise, we must
935 // negate the result.
938 AddToWorkList(SRA.Val);
939 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
941 // if integer divide is expensive and we satisfy the requirements, emit an
942 // alternate sequence.
943 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
944 !TLI.isIntDivCheap()) {
945 SDOperand Op = BuildSDIV(N);
946 if (Op.Val) return Op;
951 SDOperand DAGCombiner::visitUDIV(SDNode *N) {
952 SDOperand N0 = N->getOperand(0);
953 SDOperand N1 = N->getOperand(1);
954 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
955 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
956 MVT::ValueType VT = N->getValueType(0);
958 // fold (udiv c1, c2) -> c1/c2
959 if (N0C && N1C && !N1C->isNullValue())
960 return DAG.getNode(ISD::UDIV, VT, N0, N1);
961 // fold (udiv x, (1 << c)) -> x >>u c
962 if (N1C && isPowerOf2_64(N1C->getValue()))
963 return DAG.getNode(ISD::SRL, VT, N0,
964 DAG.getConstant(Log2_64(N1C->getValue()),
965 TLI.getShiftAmountTy()));
966 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
967 if (N1.getOpcode() == ISD::SHL) {
968 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
969 if (isPowerOf2_64(SHC->getValue())) {
970 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
971 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
972 DAG.getConstant(Log2_64(SHC->getValue()),
974 AddToWorkList(Add.Val);
975 return DAG.getNode(ISD::SRL, VT, N0, Add);
979 // fold (udiv x, c) -> alternate
980 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
981 SDOperand Op = BuildUDIV(N);
982 if (Op.Val) return Op;
987 SDOperand DAGCombiner::visitSREM(SDNode *N) {
988 SDOperand N0 = N->getOperand(0);
989 SDOperand N1 = N->getOperand(1);
990 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
991 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
992 MVT::ValueType VT = N->getValueType(0);
994 // fold (srem c1, c2) -> c1%c2
995 if (N0C && N1C && !N1C->isNullValue())
996 return DAG.getNode(ISD::SREM, VT, N0, N1);
997 // If we know the sign bits of both operands are zero, strength reduce to a
998 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
999 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
1000 if (TLI.MaskedValueIsZero(N1, SignBit) &&
1001 TLI.MaskedValueIsZero(N0, SignBit))
1002 return DAG.getNode(ISD::UREM, VT, N0, N1);
1006 SDOperand DAGCombiner::visitUREM(SDNode *N) {
1007 SDOperand N0 = N->getOperand(0);
1008 SDOperand N1 = N->getOperand(1);
1009 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1010 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1011 MVT::ValueType VT = N->getValueType(0);
1013 // fold (urem c1, c2) -> c1%c2
1014 if (N0C && N1C && !N1C->isNullValue())
1015 return DAG.getNode(ISD::UREM, VT, N0, N1);
1016 // fold (urem x, pow2) -> (and x, pow2-1)
1017 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
1018 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
1019 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1020 if (N1.getOpcode() == ISD::SHL) {
1021 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1022 if (isPowerOf2_64(SHC->getValue())) {
1023 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
1024 AddToWorkList(Add.Val);
1025 return DAG.getNode(ISD::AND, VT, N0, Add);
1032 SDOperand DAGCombiner::visitMULHS(SDNode *N) {
1033 SDOperand N0 = N->getOperand(0);
1034 SDOperand N1 = N->getOperand(1);
1035 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1037 // fold (mulhs x, 0) -> 0
1038 if (N1C && N1C->isNullValue())
1040 // fold (mulhs x, 1) -> (sra x, size(x)-1)
1041 if (N1C && N1C->getValue() == 1)
1042 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1043 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
1044 TLI.getShiftAmountTy()));
1048 SDOperand DAGCombiner::visitMULHU(SDNode *N) {
1049 SDOperand N0 = N->getOperand(0);
1050 SDOperand N1 = N->getOperand(1);
1051 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1053 // fold (mulhu x, 0) -> 0
1054 if (N1C && N1C->isNullValue())
1056 // fold (mulhu x, 1) -> 0
1057 if (N1C && N1C->getValue() == 1)
1058 return DAG.getConstant(0, N0.getValueType());
1062 SDOperand DAGCombiner::visitAND(SDNode *N) {
1063 SDOperand N0 = N->getOperand(0);
1064 SDOperand N1 = N->getOperand(1);
1065 SDOperand LL, LR, RL, RR, CC0, CC1;
1066 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1067 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1068 MVT::ValueType VT = N1.getValueType();
1069 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1071 // fold (and c1, c2) -> c1&c2
1073 return DAG.getNode(ISD::AND, VT, N0, N1);
1074 // canonicalize constant to RHS
1076 return DAG.getNode(ISD::AND, VT, N1, N0);
1077 // fold (and x, -1) -> x
1078 if (N1C && N1C->isAllOnesValue())
1080 // if (and x, c) is known to be zero, return 0
1081 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
1082 return DAG.getConstant(0, VT);
1084 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1087 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
1088 if (N1C && N0.getOpcode() == ISD::OR)
1089 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1090 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
1092 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1093 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1094 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
1095 if (TLI.MaskedValueIsZero(N0.getOperand(0),
1096 ~N1C->getValue() & InMask)) {
1097 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1100 // Replace uses of the AND with uses of the Zero extend node.
1103 // We actually want to replace all uses of the any_extend with the
1104 // zero_extend, to avoid duplicating things. This will later cause this
1105 // AND to be folded.
1106 CombineTo(N0.Val, Zext);
1110 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1111 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1112 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1113 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1115 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1116 MVT::isInteger(LL.getValueType())) {
1117 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1118 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1119 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1120 AddToWorkList(ORNode.Val);
1121 return DAG.getSetCC(VT, ORNode, LR, Op1);
1123 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1124 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1125 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1126 AddToWorkList(ANDNode.Val);
1127 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1129 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1130 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1131 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1132 AddToWorkList(ORNode.Val);
1133 return DAG.getSetCC(VT, ORNode, LR, Op1);
1136 // canonicalize equivalent to ll == rl
1137 if (LL == RR && LR == RL) {
1138 Op1 = ISD::getSetCCSwappedOperands(Op1);
1141 if (LL == RL && LR == RR) {
1142 bool isInteger = MVT::isInteger(LL.getValueType());
1143 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1144 if (Result != ISD::SETCC_INVALID)
1145 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1148 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1149 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1150 N1.getOpcode() == ISD::ZERO_EXTEND &&
1151 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1152 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1153 N0.getOperand(0), N1.getOperand(0));
1154 AddToWorkList(ANDNode.Val);
1155 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1157 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
1158 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1159 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1160 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1161 N0.getOperand(1) == N1.getOperand(1)) {
1162 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1163 N0.getOperand(0), N1.getOperand(0));
1164 AddToWorkList(ANDNode.Val);
1165 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1167 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1168 // fold (and (sra)) -> (and (srl)) when possible.
1169 if (!MVT::isVector(VT) &&
1170 SimplifyDemandedBits(SDOperand(N, 0)))
1172 // fold (zext_inreg (extload x)) -> (zextload x)
1173 if (N0.getOpcode() == ISD::EXTLOAD) {
1174 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1175 // If we zero all the possible extended bits, then we can turn this into
1176 // a zextload if we are running before legalize or the operation is legal.
1177 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
1178 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1179 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1180 N0.getOperand(1), N0.getOperand(2),
1183 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1187 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
1188 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
1189 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1190 // If we zero all the possible extended bits, then we can turn this into
1191 // a zextload if we are running before legalize or the operation is legal.
1192 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
1193 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1194 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1195 N0.getOperand(1), N0.getOperand(2),
1198 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1203 // fold (and (load x), 255) -> (zextload x, i8)
1204 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1206 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1207 N0.getOpcode() == ISD::ZEXTLOAD) &&
1209 MVT::ValueType EVT, LoadedVT;
1210 if (N1C->getValue() == 255)
1212 else if (N1C->getValue() == 65535)
1214 else if (N1C->getValue() == ~0U)
1219 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1220 cast<VTSDNode>(N0.getOperand(3))->getVT();
1221 if (EVT != MVT::Other && LoadedVT > EVT) {
1222 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1223 // For big endian targets, we need to add an offset to the pointer to load
1224 // the correct bytes. For little endian systems, we merely need to read
1225 // fewer bytes from the same pointer.
1227 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1228 SDOperand NewPtr = N0.getOperand(1);
1229 if (!TLI.isLittleEndian())
1230 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1231 DAG.getConstant(PtrOff, PtrType));
1232 AddToWorkList(NewPtr.Val);
1234 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1235 N0.getOperand(2), EVT);
1237 CombineTo(N0.Val, Load, Load.getValue(1));
1245 SDOperand DAGCombiner::visitOR(SDNode *N) {
1246 SDOperand N0 = N->getOperand(0);
1247 SDOperand N1 = N->getOperand(1);
1248 SDOperand LL, LR, RL, RR, CC0, CC1;
1249 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1250 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1251 MVT::ValueType VT = N1.getValueType();
1252 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1254 // fold (or c1, c2) -> c1|c2
1256 return DAG.getNode(ISD::OR, VT, N0, N1);
1257 // canonicalize constant to RHS
1259 return DAG.getNode(ISD::OR, VT, N1, N0);
1260 // fold (or x, 0) -> x
1261 if (N1C && N1C->isNullValue())
1263 // fold (or x, -1) -> -1
1264 if (N1C && N1C->isAllOnesValue())
1266 // fold (or x, c) -> c iff (x & ~c) == 0
1268 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
1271 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1274 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1275 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
1276 isa<ConstantSDNode>(N0.getOperand(1))) {
1277 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1278 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1280 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
1282 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1283 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1284 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1285 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1287 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1288 MVT::isInteger(LL.getValueType())) {
1289 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1290 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1291 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1292 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1293 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1294 AddToWorkList(ORNode.Val);
1295 return DAG.getSetCC(VT, ORNode, LR, Op1);
1297 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1298 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1299 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1300 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1301 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1302 AddToWorkList(ANDNode.Val);
1303 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1306 // canonicalize equivalent to ll == rl
1307 if (LL == RR && LR == RL) {
1308 Op1 = ISD::getSetCCSwappedOperands(Op1);
1311 if (LL == RL && LR == RR) {
1312 bool isInteger = MVT::isInteger(LL.getValueType());
1313 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1314 if (Result != ISD::SETCC_INVALID)
1315 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1318 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1319 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1320 N1.getOpcode() == ISD::ZERO_EXTEND &&
1321 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1322 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1323 N0.getOperand(0), N1.getOperand(0));
1324 AddToWorkList(ORNode.Val);
1325 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1327 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1328 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1329 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1330 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1331 N0.getOperand(1) == N1.getOperand(1)) {
1332 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1333 N0.getOperand(0), N1.getOperand(0));
1334 AddToWorkList(ORNode.Val);
1335 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1337 // canonicalize shl to left side in a shl/srl pair, to match rotate
1338 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1340 // check for rotl, rotr
1341 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1342 N0.getOperand(0) == N1.getOperand(0) &&
1343 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
1344 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1345 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1346 N1.getOperand(1).getOpcode() == ISD::Constant) {
1347 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1348 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1349 if ((c1val + c2val) == OpSizeInBits)
1350 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1352 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1353 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1354 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1355 if (ConstantSDNode *SUBC =
1356 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1357 if (SUBC->getValue() == OpSizeInBits)
1358 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1359 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1360 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1361 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1362 if (ConstantSDNode *SUBC =
1363 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1364 if (SUBC->getValue() == OpSizeInBits) {
1365 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
1366 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1369 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1376 SDOperand DAGCombiner::visitXOR(SDNode *N) {
1377 SDOperand N0 = N->getOperand(0);
1378 SDOperand N1 = N->getOperand(1);
1379 SDOperand LHS, RHS, CC;
1380 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1381 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1382 MVT::ValueType VT = N0.getValueType();
1384 // fold (xor c1, c2) -> c1^c2
1386 return DAG.getNode(ISD::XOR, VT, N0, N1);
1387 // canonicalize constant to RHS
1389 return DAG.getNode(ISD::XOR, VT, N1, N0);
1390 // fold (xor x, 0) -> x
1391 if (N1C && N1C->isNullValue())
1394 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1397 // fold !(x cc y) -> (x !cc y)
1398 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1399 bool isInt = MVT::isInteger(LHS.getValueType());
1400 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1402 if (N0.getOpcode() == ISD::SETCC)
1403 return DAG.getSetCC(VT, LHS, RHS, NotCC);
1404 if (N0.getOpcode() == ISD::SELECT_CC)
1405 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
1406 assert(0 && "Unhandled SetCC Equivalent!");
1409 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1410 if (N1C && N1C->getValue() == 1 &&
1411 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
1412 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
1413 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1414 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
1415 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1416 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
1417 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
1418 return DAG.getNode(NewOpcode, VT, LHS, RHS);
1421 // fold !(x or y) -> (!x and !y) iff x or y are constants
1422 if (N1C && N1C->isAllOnesValue() &&
1423 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
1424 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
1425 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1426 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
1427 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1428 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
1429 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
1430 return DAG.getNode(NewOpcode, VT, LHS, RHS);
1433 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1434 if (N1C && N0.getOpcode() == ISD::XOR) {
1435 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1436 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1438 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1439 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1441 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1442 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1444 // fold (xor x, x) -> 0
1446 if (!MVT::isVector(VT)) {
1447 return DAG.getConstant(0, VT);
1448 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1449 // Produce a vector of zeros.
1450 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1451 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1452 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
1455 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1456 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1457 N1.getOpcode() == ISD::ZERO_EXTEND &&
1458 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1459 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1460 N0.getOperand(0), N1.getOperand(0));
1461 AddToWorkList(XORNode.Val);
1462 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1464 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1465 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1466 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1467 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1468 N0.getOperand(1) == N1.getOperand(1)) {
1469 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1470 N0.getOperand(0), N1.getOperand(0));
1471 AddToWorkList(XORNode.Val);
1472 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1477 SDOperand DAGCombiner::visitSHL(SDNode *N) {
1478 SDOperand N0 = N->getOperand(0);
1479 SDOperand N1 = N->getOperand(1);
1480 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1481 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1482 MVT::ValueType VT = N0.getValueType();
1483 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1485 // fold (shl c1, c2) -> c1<<c2
1487 return DAG.getNode(ISD::SHL, VT, N0, N1);
1488 // fold (shl 0, x) -> 0
1489 if (N0C && N0C->isNullValue())
1491 // fold (shl x, c >= size(x)) -> undef
1492 if (N1C && N1C->getValue() >= OpSizeInBits)
1493 return DAG.getNode(ISD::UNDEF, VT);
1494 // fold (shl x, 0) -> x
1495 if (N1C && N1C->isNullValue())
1497 // if (shl x, c) is known to be zero, return 0
1498 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
1499 return DAG.getConstant(0, VT);
1500 if (SimplifyDemandedBits(SDOperand(N, 0)))
1502 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
1503 if (N1C && N0.getOpcode() == ISD::SHL &&
1504 N0.getOperand(1).getOpcode() == ISD::Constant) {
1505 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1506 uint64_t c2 = N1C->getValue();
1507 if (c1 + c2 > OpSizeInBits)
1508 return DAG.getConstant(0, VT);
1509 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
1510 DAG.getConstant(c1 + c2, N1.getValueType()));
1512 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1513 // (srl (and x, -1 << c1), c1-c2)
1514 if (N1C && N0.getOpcode() == ISD::SRL &&
1515 N0.getOperand(1).getOpcode() == ISD::Constant) {
1516 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1517 uint64_t c2 = N1C->getValue();
1518 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1519 DAG.getConstant(~0ULL << c1, VT));
1521 return DAG.getNode(ISD::SHL, VT, Mask,
1522 DAG.getConstant(c2-c1, N1.getValueType()));
1524 return DAG.getNode(ISD::SRL, VT, Mask,
1525 DAG.getConstant(c1-c2, N1.getValueType()));
1527 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
1528 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
1529 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1530 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1531 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1532 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1533 isa<ConstantSDNode>(N0.getOperand(1))) {
1534 return DAG.getNode(ISD::ADD, VT,
1535 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1536 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1541 SDOperand DAGCombiner::visitSRA(SDNode *N) {
1542 SDOperand N0 = N->getOperand(0);
1543 SDOperand N1 = N->getOperand(1);
1544 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1545 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1546 MVT::ValueType VT = N0.getValueType();
1548 // fold (sra c1, c2) -> c1>>c2
1550 return DAG.getNode(ISD::SRA, VT, N0, N1);
1551 // fold (sra 0, x) -> 0
1552 if (N0C && N0C->isNullValue())
1554 // fold (sra -1, x) -> -1
1555 if (N0C && N0C->isAllOnesValue())
1557 // fold (sra x, c >= size(x)) -> undef
1558 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
1559 return DAG.getNode(ISD::UNDEF, VT);
1560 // fold (sra x, 0) -> x
1561 if (N1C && N1C->isNullValue())
1563 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1565 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1566 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1569 default: EVT = MVT::Other; break;
1570 case 1: EVT = MVT::i1; break;
1571 case 8: EVT = MVT::i8; break;
1572 case 16: EVT = MVT::i16; break;
1573 case 32: EVT = MVT::i32; break;
1575 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1576 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1577 DAG.getValueType(EVT));
1580 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1581 if (N1C && N0.getOpcode() == ISD::SRA) {
1582 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1583 unsigned Sum = N1C->getValue() + C1->getValue();
1584 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1585 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1586 DAG.getConstant(Sum, N1C->getValueType(0)));
1590 // If the sign bit is known to be zero, switch this to a SRL.
1591 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
1592 return DAG.getNode(ISD::SRL, VT, N0, N1);
1596 SDOperand DAGCombiner::visitSRL(SDNode *N) {
1597 SDOperand N0 = N->getOperand(0);
1598 SDOperand N1 = N->getOperand(1);
1599 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1600 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1601 MVT::ValueType VT = N0.getValueType();
1602 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1604 // fold (srl c1, c2) -> c1 >>u c2
1606 return DAG.getNode(ISD::SRL, VT, N0, N1);
1607 // fold (srl 0, x) -> 0
1608 if (N0C && N0C->isNullValue())
1610 // fold (srl x, c >= size(x)) -> undef
1611 if (N1C && N1C->getValue() >= OpSizeInBits)
1612 return DAG.getNode(ISD::UNDEF, VT);
1613 // fold (srl x, 0) -> x
1614 if (N1C && N1C->isNullValue())
1616 // if (srl x, c) is known to be zero, return 0
1617 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
1618 return DAG.getConstant(0, VT);
1619 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
1620 if (N1C && N0.getOpcode() == ISD::SRL &&
1621 N0.getOperand(1).getOpcode() == ISD::Constant) {
1622 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1623 uint64_t c2 = N1C->getValue();
1624 if (c1 + c2 > OpSizeInBits)
1625 return DAG.getConstant(0, VT);
1626 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
1627 DAG.getConstant(c1 + c2, N1.getValueType()));
1630 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1631 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1632 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1633 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1634 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1636 // If any of the input bits are KnownOne, then the input couldn't be all
1637 // zeros, thus the result of the srl will always be zero.
1638 if (KnownOne) return DAG.getConstant(0, VT);
1640 // If all of the bits input the to ctlz node are known to be zero, then
1641 // the result of the ctlz is "32" and the result of the shift is one.
1642 uint64_t UnknownBits = ~KnownZero & Mask;
1643 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1645 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1646 if ((UnknownBits & (UnknownBits-1)) == 0) {
1647 // Okay, we know that only that the single bit specified by UnknownBits
1648 // could be set on input to the CTLZ node. If this bit is set, the SRL
1649 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1650 // to an SRL,XOR pair, which is likely to simplify more.
1651 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1652 SDOperand Op = N0.getOperand(0);
1654 Op = DAG.getNode(ISD::SRL, VT, Op,
1655 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1656 AddToWorkList(Op.Val);
1658 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1665 SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
1666 SDOperand N0 = N->getOperand(0);
1667 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1668 MVT::ValueType VT = N->getValueType(0);
1670 // fold (ctlz c1) -> c2
1672 return DAG.getNode(ISD::CTLZ, VT, N0);
1676 SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
1677 SDOperand N0 = N->getOperand(0);
1678 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1679 MVT::ValueType VT = N->getValueType(0);
1681 // fold (cttz c1) -> c2
1683 return DAG.getNode(ISD::CTTZ, VT, N0);
1687 SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
1688 SDOperand N0 = N->getOperand(0);
1689 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1690 MVT::ValueType VT = N->getValueType(0);
1692 // fold (ctpop c1) -> c2
1694 return DAG.getNode(ISD::CTPOP, VT, N0);
1698 SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1699 SDOperand N0 = N->getOperand(0);
1700 SDOperand N1 = N->getOperand(1);
1701 SDOperand N2 = N->getOperand(2);
1702 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1703 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1704 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1705 MVT::ValueType VT = N->getValueType(0);
1707 // fold select C, X, X -> X
1710 // fold select true, X, Y -> X
1711 if (N0C && !N0C->isNullValue())
1713 // fold select false, X, Y -> Y
1714 if (N0C && N0C->isNullValue())
1716 // fold select C, 1, X -> C | X
1717 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
1718 return DAG.getNode(ISD::OR, VT, N0, N2);
1719 // fold select C, 0, X -> ~C & X
1720 // FIXME: this should check for C type == X type, not i1?
1721 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1722 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1723 AddToWorkList(XORNode.Val);
1724 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1726 // fold select C, X, 1 -> ~C | X
1727 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
1728 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1729 AddToWorkList(XORNode.Val);
1730 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1732 // fold select C, X, 0 -> C & X
1733 // FIXME: this should check for C type == X type, not i1?
1734 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1735 return DAG.getNode(ISD::AND, VT, N0, N1);
1736 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1737 if (MVT::i1 == VT && N0 == N1)
1738 return DAG.getNode(ISD::OR, VT, N0, N2);
1739 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1740 if (MVT::i1 == VT && N0 == N2)
1741 return DAG.getNode(ISD::AND, VT, N0, N1);
1742 // If we can fold this based on the true/false value, do so.
1743 if (SimplifySelectOps(N, N1, N2))
1745 // fold selects based on a setcc into other things, such as min/max/abs
1746 if (N0.getOpcode() == ISD::SETCC)
1748 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1749 // having to say they don't support SELECT_CC on every type the DAG knows
1750 // about, since there is no way to mark an opcode illegal at all value types
1751 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1752 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1753 N1, N2, N0.getOperand(2));
1755 return SimplifySelect(N0, N1, N2);
1759 SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
1760 SDOperand N0 = N->getOperand(0);
1761 SDOperand N1 = N->getOperand(1);
1762 SDOperand N2 = N->getOperand(2);
1763 SDOperand N3 = N->getOperand(3);
1764 SDOperand N4 = N->getOperand(4);
1765 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1766 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1767 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1768 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1770 // Determine if the condition we're dealing with is constant
1771 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1772 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1774 // fold select_cc lhs, rhs, x, x, cc -> x
1778 // If we can fold this based on the true/false value, do so.
1779 if (SimplifySelectOps(N, N2, N3))
1782 // fold select_cc into other things, such as min/max/abs
1783 return SimplifySelectCC(N0, N1, N2, N3, CC);
1786 SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1787 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1788 cast<CondCodeSDNode>(N->getOperand(2))->get());
1791 SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
1792 SDOperand N0 = N->getOperand(0);
1793 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1794 MVT::ValueType VT = N->getValueType(0);
1796 // fold (sext c1) -> c1
1798 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
1799 // fold (sext (sext x)) -> (sext x)
1800 if (N0.getOpcode() == ISD::SIGN_EXTEND)
1801 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1802 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
1803 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1805 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
1806 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1807 DAG.getValueType(N0.getValueType()));
1808 // fold (sext (load x)) -> (sext (truncate (sextload x)))
1809 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1810 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
1811 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1812 N0.getOperand(1), N0.getOperand(2),
1814 CombineTo(N, ExtLoad);
1815 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1816 ExtLoad.getValue(1));
1820 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1821 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1822 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1824 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1825 N0.getOperand(1), N0.getOperand(2),
1827 CombineTo(N, ExtLoad);
1828 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1829 ExtLoad.getValue(1));
1836 SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
1837 SDOperand N0 = N->getOperand(0);
1838 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1839 MVT::ValueType VT = N->getValueType(0);
1841 // fold (zext c1) -> c1
1843 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1844 // fold (zext (zext x)) -> (zext x)
1845 if (N0.getOpcode() == ISD::ZERO_EXTEND)
1846 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1847 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1848 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1849 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
1850 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
1851 // fold (zext (load x)) -> (zext (truncate (zextload x)))
1852 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1853 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
1854 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1855 N0.getOperand(1), N0.getOperand(2),
1857 CombineTo(N, ExtLoad);
1858 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1859 ExtLoad.getValue(1));
1863 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1864 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1865 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1867 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1868 N0.getOperand(1), N0.getOperand(2),
1870 CombineTo(N, ExtLoad);
1871 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1872 ExtLoad.getValue(1));
1878 SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
1879 SDOperand N0 = N->getOperand(0);
1880 SDOperand N1 = N->getOperand(1);
1881 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1882 MVT::ValueType VT = N->getValueType(0);
1883 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
1884 unsigned EVTBits = MVT::getSizeInBits(EVT);
1886 // fold (sext_in_reg c1) -> c1
1888 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
1889 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
1891 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
1892 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1893 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
1896 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1897 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1898 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
1899 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
1901 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1902 if (N0.getOpcode() == ISD::AssertSext &&
1903 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
1906 // fold (sext_in_reg (sextload x)) -> (sextload x)
1907 if (N0.getOpcode() == ISD::SEXTLOAD &&
1908 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
1911 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
1912 if (N0.getOpcode() == ISD::SETCC &&
1913 TLI.getSetCCResultContents() ==
1914 TargetLowering::ZeroOrNegativeOneSetCCResult)
1916 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1917 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
1918 return DAG.getZeroExtendInReg(N0, EVT);
1919 // fold (sext_in_reg (srl x)) -> sra x
1920 if (N0.getOpcode() == ISD::SRL &&
1921 N0.getOperand(1).getOpcode() == ISD::Constant &&
1922 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1923 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1926 // fold (sext_inreg (extload x)) -> (sextload x)
1927 if (N0.getOpcode() == ISD::EXTLOAD &&
1928 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
1929 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
1930 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1931 N0.getOperand(1), N0.getOperand(2),
1933 CombineTo(N, ExtLoad);
1934 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1937 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
1938 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
1939 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
1940 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
1941 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1942 N0.getOperand(1), N0.getOperand(2),
1944 CombineTo(N, ExtLoad);
1945 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1951 SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
1952 SDOperand N0 = N->getOperand(0);
1953 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1954 MVT::ValueType VT = N->getValueType(0);
1957 if (N0.getValueType() == N->getValueType(0))
1959 // fold (truncate c1) -> c1
1961 return DAG.getNode(ISD::TRUNCATE, VT, N0);
1962 // fold (truncate (truncate x)) -> (truncate x)
1963 if (N0.getOpcode() == ISD::TRUNCATE)
1964 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
1965 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1966 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1967 if (N0.getValueType() < VT)
1968 // if the source is smaller than the dest, we still need an extend
1969 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1970 else if (N0.getValueType() > VT)
1971 // if the source is larger than the dest, than we just need the truncate
1972 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
1974 // if the source and dest are the same type, we can drop both the extend
1976 return N0.getOperand(0);
1978 // fold (truncate (load x)) -> (smaller load x)
1979 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
1980 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1981 "Cannot truncate to larger type!");
1982 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1983 // For big endian targets, we need to add an offset to the pointer to load
1984 // the correct bytes. For little endian systems, we merely need to read
1985 // fewer bytes from the same pointer.
1987 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
1988 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1989 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1990 DAG.getConstant(PtrOff, PtrType));
1991 AddToWorkList(NewPtr.Val);
1992 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
1994 CombineTo(N0.Val, Load, Load.getValue(1));
2000 SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2001 SDOperand N0 = N->getOperand(0);
2002 MVT::ValueType VT = N->getValueType(0);
2004 // If the input is a constant, let getNode() fold it.
2005 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2006 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2007 if (Res.Val != N) return Res;
2010 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2011 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
2013 // fold (conv (load x)) -> (load (conv*)x)
2014 // FIXME: These xforms need to know that the resultant load doesn't need a
2015 // higher alignment than the original!
2016 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
2017 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2020 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2028 SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2029 SDOperand N0 = N->getOperand(0);
2030 MVT::ValueType VT = N->getValueType(0);
2032 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2033 // First check to see if this is all constant.
2034 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2035 VT == MVT::Vector) {
2036 bool isSimple = true;
2037 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2038 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2039 N0.getOperand(i).getOpcode() != ISD::Constant &&
2040 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2046 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2047 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2054 /// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2055 /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2056 /// destination element value type.
2057 SDOperand DAGCombiner::
2058 ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2059 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2061 // If this is already the right type, we're done.
2062 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2064 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2065 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2067 // If this is a conversion of N elements of one type to N elements of another
2068 // type, convert each element. This handles FP<->INT cases.
2069 if (SrcBitSize == DstBitSize) {
2070 std::vector<SDOperand> Ops;
2071 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i)
2072 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
2073 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2074 Ops.push_back(DAG.getValueType(DstEltVT));
2075 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2078 // Otherwise, we're growing or shrinking the elements. To avoid having to
2079 // handle annoying details of growing/shrinking FP values, we convert them to
2081 if (MVT::isFloatingPoint(SrcEltVT)) {
2082 // Convert the input float vector to a int vector where the elements are the
2084 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2085 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2086 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2090 // Now we know the input is an integer vector. If the output is a FP type,
2091 // convert to integer first, then to FP of the right size.
2092 if (MVT::isFloatingPoint(DstEltVT)) {
2093 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2094 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2095 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2097 // Next, convert to FP elements of the same size.
2098 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2101 // Okay, we know the src/dst types are both integers of differing types.
2102 // Handling growing first.
2103 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2104 if (SrcBitSize < DstBitSize) {
2105 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2107 std::vector<SDOperand> Ops;
2108 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2109 i += NumInputsPerOutput) {
2110 bool isLE = TLI.isLittleEndian();
2111 uint64_t NewBits = 0;
2112 bool EltIsUndef = true;
2113 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2114 // Shift the previously computed bits over.
2115 NewBits <<= SrcBitSize;
2116 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2117 if (Op.getOpcode() == ISD::UNDEF) continue;
2120 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2124 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2126 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2129 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2130 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2131 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2134 // Finally, this must be the case where we are shrinking elements: each input
2135 // turns into multiple outputs.
2136 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
2137 std::vector<SDOperand> Ops;
2138 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2139 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2140 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2141 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2144 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2146 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2147 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2148 OpVal >>= DstBitSize;
2149 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2152 // For big endian targets, swap the order of the pieces of each element.
2153 if (!TLI.isLittleEndian())
2154 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2156 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2157 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2158 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2163 SDOperand DAGCombiner::visitFADD(SDNode *N) {
2164 SDOperand N0 = N->getOperand(0);
2165 SDOperand N1 = N->getOperand(1);
2166 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2167 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2168 MVT::ValueType VT = N->getValueType(0);
2170 // fold (fadd c1, c2) -> c1+c2
2172 return DAG.getNode(ISD::FADD, VT, N0, N1);
2173 // canonicalize constant to RHS
2174 if (N0CFP && !N1CFP)
2175 return DAG.getNode(ISD::FADD, VT, N1, N0);
2176 // fold (A + (-B)) -> A-B
2177 if (N1.getOpcode() == ISD::FNEG)
2178 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
2179 // fold ((-A) + B) -> B-A
2180 if (N0.getOpcode() == ISD::FNEG)
2181 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
2185 SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2186 SDOperand N0 = N->getOperand(0);
2187 SDOperand N1 = N->getOperand(1);
2188 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2189 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2190 MVT::ValueType VT = N->getValueType(0);
2192 // fold (fsub c1, c2) -> c1-c2
2194 return DAG.getNode(ISD::FSUB, VT, N0, N1);
2195 // fold (A-(-B)) -> A+B
2196 if (N1.getOpcode() == ISD::FNEG)
2197 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
2201 SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2202 SDOperand N0 = N->getOperand(0);
2203 SDOperand N1 = N->getOperand(1);
2204 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2205 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2206 MVT::ValueType VT = N->getValueType(0);
2208 // fold (fmul c1, c2) -> c1*c2
2210 return DAG.getNode(ISD::FMUL, VT, N0, N1);
2211 // canonicalize constant to RHS
2212 if (N0CFP && !N1CFP)
2213 return DAG.getNode(ISD::FMUL, VT, N1, N0);
2214 // fold (fmul X, 2.0) -> (fadd X, X)
2215 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2216 return DAG.getNode(ISD::FADD, VT, N0, N0);
2220 SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2221 SDOperand N0 = N->getOperand(0);
2222 SDOperand N1 = N->getOperand(1);
2223 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2224 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2225 MVT::ValueType VT = N->getValueType(0);
2227 // fold (fdiv c1, c2) -> c1/c2
2229 return DAG.getNode(ISD::FDIV, VT, N0, N1);
2233 SDOperand DAGCombiner::visitFREM(SDNode *N) {
2234 SDOperand N0 = N->getOperand(0);
2235 SDOperand N1 = N->getOperand(1);
2236 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2237 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2238 MVT::ValueType VT = N->getValueType(0);
2240 // fold (frem c1, c2) -> fmod(c1,c2)
2242 return DAG.getNode(ISD::FREM, VT, N0, N1);
2246 SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2247 SDOperand N0 = N->getOperand(0);
2248 SDOperand N1 = N->getOperand(1);
2249 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2250 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2251 MVT::ValueType VT = N->getValueType(0);
2253 if (N0CFP && N1CFP) // Constant fold
2254 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2257 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2258 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2263 u.d = N1CFP->getValue();
2265 return DAG.getNode(ISD::FABS, VT, N0);
2267 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2270 // copysign(fabs(x), y) -> copysign(x, y)
2271 // copysign(fneg(x), y) -> copysign(x, y)
2272 // copysign(copysign(x,z), y) -> copysign(x, y)
2273 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2274 N0.getOpcode() == ISD::FCOPYSIGN)
2275 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2277 // copysign(x, abs(y)) -> abs(x)
2278 if (N1.getOpcode() == ISD::FABS)
2279 return DAG.getNode(ISD::FABS, VT, N0);
2281 // copysign(x, copysign(y,z)) -> copysign(x, z)
2282 if (N1.getOpcode() == ISD::FCOPYSIGN)
2283 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2285 // copysign(x, fp_extend(y)) -> copysign(x, y)
2286 // copysign(x, fp_round(y)) -> copysign(x, y)
2287 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2288 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2295 SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
2296 SDOperand N0 = N->getOperand(0);
2297 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2298 MVT::ValueType VT = N->getValueType(0);
2300 // fold (sint_to_fp c1) -> c1fp
2302 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
2306 SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
2307 SDOperand N0 = N->getOperand(0);
2308 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2309 MVT::ValueType VT = N->getValueType(0);
2311 // fold (uint_to_fp c1) -> c1fp
2313 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
2317 SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
2318 SDOperand N0 = N->getOperand(0);
2319 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2320 MVT::ValueType VT = N->getValueType(0);
2322 // fold (fp_to_sint c1fp) -> c1
2324 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
2328 SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
2329 SDOperand N0 = N->getOperand(0);
2330 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2331 MVT::ValueType VT = N->getValueType(0);
2333 // fold (fp_to_uint c1fp) -> c1
2335 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
2339 SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
2340 SDOperand N0 = N->getOperand(0);
2341 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2342 MVT::ValueType VT = N->getValueType(0);
2344 // fold (fp_round c1fp) -> c1fp
2346 return DAG.getNode(ISD::FP_ROUND, VT, N0);
2348 // fold (fp_round (fp_extend x)) -> x
2349 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2350 return N0.getOperand(0);
2352 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2353 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2354 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2355 AddToWorkList(Tmp.Val);
2356 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2362 SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
2363 SDOperand N0 = N->getOperand(0);
2364 MVT::ValueType VT = N->getValueType(0);
2365 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2366 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2368 // fold (fp_round_inreg c1fp) -> c1fp
2370 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
2371 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
2376 SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
2377 SDOperand N0 = N->getOperand(0);
2378 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2379 MVT::ValueType VT = N->getValueType(0);
2381 // fold (fp_extend c1fp) -> c1fp
2383 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
2387 SDOperand DAGCombiner::visitFNEG(SDNode *N) {
2388 SDOperand N0 = N->getOperand(0);
2389 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2390 MVT::ValueType VT = N->getValueType(0);
2392 // fold (fneg c1) -> -c1
2394 return DAG.getNode(ISD::FNEG, VT, N0);
2395 // fold (fneg (sub x, y)) -> (sub y, x)
2396 if (N0.getOpcode() == ISD::SUB)
2397 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
2398 // fold (fneg (fneg x)) -> x
2399 if (N0.getOpcode() == ISD::FNEG)
2400 return N0.getOperand(0);
2404 SDOperand DAGCombiner::visitFABS(SDNode *N) {
2405 SDOperand N0 = N->getOperand(0);
2406 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2407 MVT::ValueType VT = N->getValueType(0);
2409 // fold (fabs c1) -> fabs(c1)
2411 return DAG.getNode(ISD::FABS, VT, N0);
2412 // fold (fabs (fabs x)) -> (fabs x)
2413 if (N0.getOpcode() == ISD::FABS)
2414 return N->getOperand(0);
2415 // fold (fabs (fneg x)) -> (fabs x)
2416 // fold (fabs (fcopysign x, y)) -> (fabs x)
2417 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2418 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2423 SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2424 SDOperand Chain = N->getOperand(0);
2425 SDOperand N1 = N->getOperand(1);
2426 SDOperand N2 = N->getOperand(2);
2427 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2429 // never taken branch, fold to chain
2430 if (N1C && N1C->isNullValue())
2432 // unconditional branch
2433 if (N1C && N1C->getValue() == 1)
2434 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2435 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2437 if (N1.getOpcode() == ISD::SETCC &&
2438 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2439 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2440 N1.getOperand(0), N1.getOperand(1), N2);
2445 // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2447 SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
2448 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2449 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2451 // Use SimplifySetCC to simplify SETCC's.
2452 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2453 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2455 // fold br_cc true, dest -> br dest (unconditional branch)
2456 if (SCCC && SCCC->getValue())
2457 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2459 // fold br_cc false, dest -> unconditional fall through
2460 if (SCCC && SCCC->isNullValue())
2461 return N->getOperand(0);
2462 // fold to a simpler setcc
2463 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2464 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2465 Simp.getOperand(2), Simp.getOperand(0),
2466 Simp.getOperand(1), N->getOperand(4));
2470 SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2471 SDOperand Chain = N->getOperand(0);
2472 SDOperand Ptr = N->getOperand(1);
2473 SDOperand SrcValue = N->getOperand(2);
2475 // If there are no uses of the loaded value, change uses of the chain value
2476 // into uses of the chain input (i.e. delete the dead load).
2477 if (N->hasNUsesOfValue(0, 0))
2478 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2480 // If this load is directly stored, replace the load value with the stored
2482 // TODO: Handle store large -> read small portion.
2483 // TODO: Handle TRUNCSTORE/EXTLOAD
2484 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2485 Chain.getOperand(1).getValueType() == N->getValueType(0))
2486 return CombineTo(N, Chain.getOperand(1), Chain);
2491 /// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2492 SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2493 SDOperand Chain = N->getOperand(0);
2494 SDOperand Ptr = N->getOperand(1);
2495 SDOperand SrcValue = N->getOperand(2);
2496 SDOperand EVT = N->getOperand(3);
2498 // If there are no uses of the loaded value, change uses of the chain value
2499 // into uses of the chain input (i.e. delete the dead load).
2500 if (N->hasNUsesOfValue(0, 0))
2501 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2506 SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2507 SDOperand Chain = N->getOperand(0);
2508 SDOperand Value = N->getOperand(1);
2509 SDOperand Ptr = N->getOperand(2);
2510 SDOperand SrcValue = N->getOperand(3);
2512 // If this is a store that kills a previous store, remove the previous store.
2513 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2514 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2515 // Make sure that these stores are the same value type:
2516 // FIXME: we really care that the second store is >= size of the first.
2517 Value.getValueType() == Chain.getOperand(1).getValueType()) {
2518 // Create a new store of Value that replaces both stores.
2519 SDNode *PrevStore = Chain.Val;
2520 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2522 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2523 PrevStore->getOperand(0), Value, Ptr,
2525 CombineTo(N, NewStore); // Nuke this store.
2526 CombineTo(PrevStore, NewStore); // Nuke the previous store.
2527 return SDOperand(N, 0);
2530 // If this is a store of a bit convert, store the input value.
2531 // FIXME: This needs to know that the resultant store does not need a
2532 // higher alignment than the original.
2533 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
2534 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2540 SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2541 SDOperand InVec = N->getOperand(0);
2542 SDOperand InVal = N->getOperand(1);
2543 SDOperand EltNo = N->getOperand(2);
2545 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2546 // vector with the inserted element.
2547 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2548 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2549 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2550 if (Elt < Ops.size())
2552 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), Ops);
2558 SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2559 SDOperand InVec = N->getOperand(0);
2560 SDOperand InVal = N->getOperand(1);
2561 SDOperand EltNo = N->getOperand(2);
2562 SDOperand NumElts = N->getOperand(3);
2563 SDOperand EltType = N->getOperand(4);
2565 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2566 // vector with the inserted element.
2567 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2568 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2569 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2570 if (Elt < Ops.size()-2)
2572 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), Ops);
2578 SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2579 unsigned NumInScalars = N->getNumOperands()-2;
2580 SDOperand NumElts = N->getOperand(NumInScalars);
2581 SDOperand EltType = N->getOperand(NumInScalars+1);
2583 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2584 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2585 // two distinct vectors, turn this into a shuffle node.
2586 SDOperand VecIn1, VecIn2;
2587 for (unsigned i = 0; i != NumInScalars; ++i) {
2588 // Ignore undef inputs.
2589 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2591 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2592 // constant index, bail out.
2593 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2594 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2595 VecIn1 = VecIn2 = SDOperand(0, 0);
2599 // If the input vector type disagrees with the result of the vbuild_vector,
2600 // we can't make a shuffle.
2601 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2602 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2603 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2604 VecIn1 = VecIn2 = SDOperand(0, 0);
2608 // Otherwise, remember this. We allow up to two distinct input vectors.
2609 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2612 if (VecIn1.Val == 0) {
2613 VecIn1 = ExtractedFromVec;
2614 } else if (VecIn2.Val == 0) {
2615 VecIn2 = ExtractedFromVec;
2618 VecIn1 = VecIn2 = SDOperand(0, 0);
2623 // If everything is good, we can make a shuffle operation.
2625 std::vector<SDOperand> BuildVecIndices;
2626 for (unsigned i = 0; i != NumInScalars; ++i) {
2627 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2628 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2632 SDOperand Extract = N->getOperand(i);
2634 // If extracting from the first vector, just use the index directly.
2635 if (Extract.getOperand(0) == VecIn1) {
2636 BuildVecIndices.push_back(Extract.getOperand(1));
2640 // Otherwise, use InIdx + VecSize
2641 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2642 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2645 // Add count and size info.
2646 BuildVecIndices.push_back(NumElts);
2647 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2649 // Return the new VVECTOR_SHUFFLE node.
2650 std::vector<SDOperand> Ops;
2651 Ops.push_back(VecIn1);
2653 Ops.push_back(VecIn2);
2655 // Use an undef vbuild_vector as input for the second operand.
2656 std::vector<SDOperand> UnOps(NumInScalars,
2657 DAG.getNode(ISD::UNDEF,
2658 cast<VTSDNode>(EltType)->getVT()));
2659 UnOps.push_back(NumElts);
2660 UnOps.push_back(EltType);
2661 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, UnOps));
2663 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR,MVT::Vector, BuildVecIndices));
2664 Ops.push_back(NumElts);
2665 Ops.push_back(EltType);
2666 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2672 SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
2673 SDOperand ShufMask = N->getOperand(2);
2674 unsigned NumElts = ShufMask.getNumOperands();
2676 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2677 bool isIdentity = true;
2678 for (unsigned i = 0; i != NumElts; ++i) {
2679 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2680 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2685 if (isIdentity) return N->getOperand(0);
2687 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2689 for (unsigned i = 0; i != NumElts; ++i) {
2690 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2691 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2696 if (isIdentity) return N->getOperand(1);
2698 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2699 if (N->getOperand(0) == N->getOperand(1)) {
2700 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2702 std::vector<SDOperand> MappedOps;
2703 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
2704 if (cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() >= NumElts) {
2706 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2707 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2709 MappedOps.push_back(ShufMask.getOperand(i));
2712 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
2714 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
2716 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2723 SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2724 SDOperand ShufMask = N->getOperand(2);
2725 unsigned NumElts = ShufMask.getNumOperands()-2;
2727 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2728 bool isIdentity = true;
2729 for (unsigned i = 0; i != NumElts; ++i) {
2730 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2731 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2736 if (isIdentity) return N->getOperand(0);
2738 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2740 for (unsigned i = 0; i != NumElts; ++i) {
2741 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2742 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2747 if (isIdentity) return N->getOperand(1);
2752 /// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
2753 /// the scalar operation of the vop if it is operating on an integer vector
2754 /// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
2755 SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
2756 ISD::NodeType FPOp) {
2757 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
2758 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
2759 SDOperand LHS = N->getOperand(0);
2760 SDOperand RHS = N->getOperand(1);
2762 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
2764 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
2765 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2766 std::vector<SDOperand> Ops;
2767 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
2768 SDOperand LHSOp = LHS.getOperand(i);
2769 SDOperand RHSOp = RHS.getOperand(i);
2770 // If these two elements can't be folded, bail out.
2771 if ((LHSOp.getOpcode() != ISD::UNDEF &&
2772 LHSOp.getOpcode() != ISD::Constant &&
2773 LHSOp.getOpcode() != ISD::ConstantFP) ||
2774 (RHSOp.getOpcode() != ISD::UNDEF &&
2775 RHSOp.getOpcode() != ISD::Constant &&
2776 RHSOp.getOpcode() != ISD::ConstantFP))
2778 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
2779 assert((Ops.back().getOpcode() == ISD::UNDEF ||
2780 Ops.back().getOpcode() == ISD::Constant ||
2781 Ops.back().getOpcode() == ISD::ConstantFP) &&
2782 "Scalar binop didn't fold!");
2784 Ops.push_back(*(LHS.Val->op_end()-2));
2785 Ops.push_back(*(LHS.Val->op_end()-1));
2786 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2792 SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
2793 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2795 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2796 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2797 // If we got a simplified select_cc node back from SimplifySelectCC, then
2798 // break it down into a new SETCC node, and a new SELECT node, and then return
2799 // the SELECT node, since we were called with a SELECT node.
2801 // Check to see if we got a select_cc back (to turn into setcc/select).
2802 // Otherwise, just return whatever node we got back, like fabs.
2803 if (SCC.getOpcode() == ISD::SELECT_CC) {
2804 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2805 SCC.getOperand(0), SCC.getOperand(1),
2807 AddToWorkList(SETCC.Val);
2808 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2809 SCC.getOperand(3), SETCC);
2816 /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2817 /// are the two values being selected between, see if we can simplify the
2820 bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2823 // If this is a select from two identical things, try to pull the operation
2824 // through the select.
2825 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2827 std::cerr << "SELECT: ["; LHS.Val->dump();
2828 std::cerr << "] ["; RHS.Val->dump();
2832 // If this is a load and the token chain is identical, replace the select
2833 // of two loads with a load through a select of the address to load from.
2834 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2835 // constants have been dropped into the constant pool.
2836 if ((LHS.getOpcode() == ISD::LOAD ||
2837 LHS.getOpcode() == ISD::EXTLOAD ||
2838 LHS.getOpcode() == ISD::ZEXTLOAD ||
2839 LHS.getOpcode() == ISD::SEXTLOAD) &&
2840 // Token chains must be identical.
2841 LHS.getOperand(0) == RHS.getOperand(0) &&
2842 // If this is an EXTLOAD, the VT's must match.
2843 (LHS.getOpcode() == ISD::LOAD ||
2844 LHS.getOperand(3) == RHS.getOperand(3))) {
2845 // FIXME: this conflates two src values, discarding one. This is not
2846 // the right thing to do, but nothing uses srcvalues now. When they do,
2847 // turn SrcValue into a list of locations.
2849 if (TheSelect->getOpcode() == ISD::SELECT)
2850 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2851 TheSelect->getOperand(0), LHS.getOperand(1),
2854 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2855 TheSelect->getOperand(0),
2856 TheSelect->getOperand(1),
2857 LHS.getOperand(1), RHS.getOperand(1),
2858 TheSelect->getOperand(4));
2861 if (LHS.getOpcode() == ISD::LOAD)
2862 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2863 Addr, LHS.getOperand(2));
2865 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2866 LHS.getOperand(0), Addr, LHS.getOperand(2),
2867 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2868 // Users of the select now use the result of the load.
2869 CombineTo(TheSelect, Load);
2871 // Users of the old loads now use the new load's chain. We know the
2872 // old-load value is dead now.
2873 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2874 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2882 SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2883 SDOperand N2, SDOperand N3,
2886 MVT::ValueType VT = N2.getValueType();
2887 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2888 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2889 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2890 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2892 // Determine if the condition we're dealing with is constant
2893 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2894 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2896 // fold select_cc true, x, y -> x
2897 if (SCCC && SCCC->getValue())
2899 // fold select_cc false, x, y -> y
2900 if (SCCC && SCCC->getValue() == 0)
2903 // Check to see if we can simplify the select into an fabs node
2904 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2905 // Allow either -0.0 or 0.0
2906 if (CFP->getValue() == 0.0) {
2907 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2908 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2909 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2910 N2 == N3.getOperand(0))
2911 return DAG.getNode(ISD::FABS, VT, N0);
2913 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2914 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2915 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2916 N2.getOperand(0) == N3)
2917 return DAG.getNode(ISD::FABS, VT, N3);
2921 // Check to see if we can perform the "gzip trick", transforming
2922 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2923 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2924 MVT::isInteger(N0.getValueType()) &&
2925 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2926 MVT::ValueType XType = N0.getValueType();
2927 MVT::ValueType AType = N2.getValueType();
2928 if (XType >= AType) {
2929 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
2930 // single-bit constant.
2931 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2932 unsigned ShCtV = Log2_64(N2C->getValue());
2933 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2934 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2935 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
2936 AddToWorkList(Shift.Val);
2937 if (XType > AType) {
2938 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2939 AddToWorkList(Shift.Val);
2941 return DAG.getNode(ISD::AND, AType, Shift, N2);
2943 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2944 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2945 TLI.getShiftAmountTy()));
2946 AddToWorkList(Shift.Val);
2947 if (XType > AType) {
2948 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2949 AddToWorkList(Shift.Val);
2951 return DAG.getNode(ISD::AND, AType, Shift, N2);
2955 // fold select C, 16, 0 -> shl C, 4
2956 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2957 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2958 // Get a SetCC of the condition
2959 // FIXME: Should probably make sure that setcc is legal if we ever have a
2960 // target where it isn't.
2961 SDOperand Temp, SCC;
2962 // cast from setcc result type to select result type
2963 if (AfterLegalize) {
2964 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2965 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
2967 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
2968 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
2970 AddToWorkList(SCC.Val);
2971 AddToWorkList(Temp.Val);
2972 // shl setcc result by log2 n2c
2973 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2974 DAG.getConstant(Log2_64(N2C->getValue()),
2975 TLI.getShiftAmountTy()));
2978 // Check to see if this is the equivalent of setcc
2979 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2980 // otherwise, go ahead with the folds.
2981 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2982 MVT::ValueType XType = N0.getValueType();
2983 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2984 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2985 if (Res.getValueType() != VT)
2986 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2990 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2991 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2992 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2993 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2994 return DAG.getNode(ISD::SRL, XType, Ctlz,
2995 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2996 TLI.getShiftAmountTy()));
2998 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2999 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3000 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3002 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3003 DAG.getConstant(~0ULL, XType));
3004 return DAG.getNode(ISD::SRL, XType,
3005 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3006 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3007 TLI.getShiftAmountTy()));
3009 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3010 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3011 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3012 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3013 TLI.getShiftAmountTy()));
3014 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3018 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3019 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3020 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3021 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3022 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3023 MVT::ValueType XType = N0.getValueType();
3024 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3025 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3026 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3027 TLI.getShiftAmountTy()));
3028 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
3029 AddToWorkList(Shift.Val);
3030 AddToWorkList(Add.Val);
3031 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3039 SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
3040 SDOperand N1, ISD::CondCode Cond,
3041 bool foldBooleans) {
3042 // These setcc operations always fold.
3046 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3048 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3051 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3052 uint64_t C1 = N1C->getValue();
3053 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3054 uint64_t C0 = N0C->getValue();
3056 // Sign extend the operands if required
3057 if (ISD::isSignedIntSetCC(Cond)) {
3058 C0 = N0C->getSignExtended();
3059 C1 = N1C->getSignExtended();
3063 default: assert(0 && "Unknown integer setcc!");
3064 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3065 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3066 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3067 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3068 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3069 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3070 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3071 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3072 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3073 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3076 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3077 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3078 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3080 // If the comparison constant has bits in the upper part, the
3081 // zero-extended value could never match.
3082 if (C1 & (~0ULL << InSize)) {
3083 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3087 case ISD::SETEQ: return DAG.getConstant(0, VT);
3090 case ISD::SETNE: return DAG.getConstant(1, VT);
3093 // True if the sign bit of C1 is set.
3094 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3097 // True if the sign bit of C1 isn't set.
3098 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3104 // Otherwise, we can perform the comparison with the low bits.
3112 return DAG.getSetCC(VT, N0.getOperand(0),
3113 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3116 break; // todo, be more careful with signed comparisons
3118 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3119 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3120 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3121 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3122 MVT::ValueType ExtDstTy = N0.getValueType();
3123 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3125 // If the extended part has any inconsistent bits, it cannot ever
3126 // compare equal. In other words, they have to be all ones or all
3129 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3130 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3131 return DAG.getConstant(Cond == ISD::SETNE, VT);
3134 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3135 if (Op0Ty == ExtSrcTy) {
3136 ZextOp = N0.getOperand(0);
3138 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3139 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3140 DAG.getConstant(Imm, Op0Ty));
3142 AddToWorkList(ZextOp.Val);
3143 // Otherwise, make this a use of a zext.
3144 return DAG.getSetCC(VT, ZextOp,
3145 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3148 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3149 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3150 (N0.getOpcode() == ISD::XOR ||
3151 (N0.getOpcode() == ISD::AND &&
3152 N0.getOperand(0).getOpcode() == ISD::XOR &&
3153 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3154 isa<ConstantSDNode>(N0.getOperand(1)) &&
3155 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3156 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3157 // only do this if the top bits are known zero.
3158 if (TLI.MaskedValueIsZero(N1,
3159 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3160 // Okay, get the un-inverted input value.
3162 if (N0.getOpcode() == ISD::XOR)
3163 Val = N0.getOperand(0);
3165 assert(N0.getOpcode() == ISD::AND &&
3166 N0.getOperand(0).getOpcode() == ISD::XOR);
3167 // ((X^1)&1)^1 -> X & 1
3168 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3169 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3171 return DAG.getSetCC(VT, Val, N1,
3172 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3176 uint64_t MinVal, MaxVal;
3177 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3178 if (ISD::isSignedIntSetCC(Cond)) {
3179 MinVal = 1ULL << (OperandBitSize-1);
3180 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3181 MaxVal = ~0ULL >> (65-OperandBitSize);
3186 MaxVal = ~0ULL >> (64-OperandBitSize);
3189 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3190 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3191 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3192 --C1; // X >= C0 --> X > (C0-1)
3193 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3194 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3197 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3198 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3199 ++C1; // X <= C0 --> X < (C0+1)
3200 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3201 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3204 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3205 return DAG.getConstant(0, VT); // X < MIN --> false
3207 // Canonicalize setgt X, Min --> setne X, Min
3208 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3209 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
3210 // Canonicalize setlt X, Max --> setne X, Max
3211 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3212 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
3214 // If we have setult X, 1, turn it into seteq X, 0
3215 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3216 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3218 // If we have setugt X, Max-1, turn it into seteq X, Max
3219 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3220 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3223 // If we have "setcc X, C0", check to see if we can shrink the immediate
3226 // SETUGT X, SINTMAX -> SETLT X, 0
3227 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3228 C1 == (~0ULL >> (65-OperandBitSize)))
3229 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3232 // FIXME: Implement the rest of these.
3234 // Fold bit comparisons when we can.
3235 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3236 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3237 if (ConstantSDNode *AndRHS =
3238 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3239 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3240 // Perform the xform if the AND RHS is a single bit.
3241 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3242 return DAG.getNode(ISD::SRL, VT, N0,
3243 DAG.getConstant(Log2_64(AndRHS->getValue()),
3244 TLI.getShiftAmountTy()));
3246 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3247 // (X & 8) == 8 --> (X & 8) >> 3
3248 // Perform the xform if C1 is a single bit.
3249 if ((C1 & (C1-1)) == 0) {
3250 return DAG.getNode(ISD::SRL, VT, N0,
3251 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
3256 } else if (isa<ConstantSDNode>(N0.Val)) {
3257 // Ensure that the constant occurs on the RHS.
3258 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3261 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3262 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3263 double C0 = N0C->getValue(), C1 = N1C->getValue();
3266 default: break; // FIXME: Implement the rest of these!
3267 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3268 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3269 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3270 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3271 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3272 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3275 // Ensure that the constant occurs on the RHS.
3276 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3280 // We can always fold X == Y for integer setcc's.
3281 if (MVT::isInteger(N0.getValueType()))
3282 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3283 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3284 if (UOF == 2) // FP operators that are undefined on NaNs.
3285 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3286 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3287 return DAG.getConstant(UOF, VT);
3288 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3289 // if it is not already.
3290 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
3291 if (NewCond != Cond)
3292 return DAG.getSetCC(VT, N0, N1, NewCond);
3295 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3296 MVT::isInteger(N0.getValueType())) {
3297 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3298 N0.getOpcode() == ISD::XOR) {
3299 // Simplify (X+Y) == (X+Z) --> Y == Z
3300 if (N0.getOpcode() == N1.getOpcode()) {
3301 if (N0.getOperand(0) == N1.getOperand(0))
3302 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3303 if (N0.getOperand(1) == N1.getOperand(1))
3304 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3305 if (isCommutativeBinOp(N0.getOpcode())) {
3306 // If X op Y == Y op X, try other combinations.
3307 if (N0.getOperand(0) == N1.getOperand(1))
3308 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3309 if (N0.getOperand(1) == N1.getOperand(0))
3310 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
3314 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3315 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3316 // Turn (X+C1) == C2 --> X == C2-C1
3317 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3318 return DAG.getSetCC(VT, N0.getOperand(0),
3319 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3320 N0.getValueType()), Cond);
3323 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3324 if (N0.getOpcode() == ISD::XOR)
3325 // If we know that all of the inverted bits are zero, don't bother
3326 // performing the inversion.
3327 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
3328 return DAG.getSetCC(VT, N0.getOperand(0),
3329 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
3330 N0.getValueType()), Cond);
3333 // Turn (C1-X) == C2 --> X == C1-C2
3334 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3335 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3336 return DAG.getSetCC(VT, N0.getOperand(1),
3337 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3338 N0.getValueType()), Cond);
3343 // Simplify (X+Z) == X --> Z == 0
3344 if (N0.getOperand(0) == N1)
3345 return DAG.getSetCC(VT, N0.getOperand(1),
3346 DAG.getConstant(0, N0.getValueType()), Cond);
3347 if (N0.getOperand(1) == N1) {
3348 if (isCommutativeBinOp(N0.getOpcode()))
3349 return DAG.getSetCC(VT, N0.getOperand(0),
3350 DAG.getConstant(0, N0.getValueType()), Cond);
3352 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3353 // (Z-X) == X --> Z == X<<1
3354 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3356 DAG.getConstant(1,TLI.getShiftAmountTy()));
3357 AddToWorkList(SH.Val);
3358 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3363 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3364 N1.getOpcode() == ISD::XOR) {
3365 // Simplify X == (X+Z) --> Z == 0
3366 if (N1.getOperand(0) == N0) {
3367 return DAG.getSetCC(VT, N1.getOperand(1),
3368 DAG.getConstant(0, N1.getValueType()), Cond);
3369 } else if (N1.getOperand(1) == N0) {
3370 if (isCommutativeBinOp(N1.getOpcode())) {
3371 return DAG.getSetCC(VT, N1.getOperand(0),
3372 DAG.getConstant(0, N1.getValueType()), Cond);
3374 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3375 // X == (Z-X) --> X<<1 == Z
3376 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3377 DAG.getConstant(1,TLI.getShiftAmountTy()));
3378 AddToWorkList(SH.Val);
3379 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3385 // Fold away ALL boolean setcc's.
3387 if (N0.getValueType() == MVT::i1 && foldBooleans) {
3389 default: assert(0 && "Unknown integer setcc!");
3390 case ISD::SETEQ: // X == Y -> (X^Y)^1
3391 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3392 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
3393 AddToWorkList(Temp.Val);
3395 case ISD::SETNE: // X != Y --> (X^Y)
3396 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3398 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3399 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3400 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3401 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
3402 AddToWorkList(Temp.Val);
3404 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3405 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3406 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3407 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
3408 AddToWorkList(Temp.Val);
3410 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3411 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3412 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3413 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
3414 AddToWorkList(Temp.Val);
3416 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3417 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3418 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3419 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3422 if (VT != MVT::i1) {
3423 AddToWorkList(N0.Val);
3424 // FIXME: If running after legalize, we probably can't do this.
3425 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3430 // Could not fold it.
3434 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3435 /// return a DAG expression to select that will generate the same value by
3436 /// multiplying by a magic number. See:
3437 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3438 SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
3439 MVT::ValueType VT = N->getValueType(0);
3441 // Check to see if we can do this.
3442 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3443 return SDOperand(); // BuildSDIV only operates on i32 or i64
3444 if (!TLI.isOperationLegal(ISD::MULHS, VT))
3445 return SDOperand(); // Make sure the target supports MULHS.
3447 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
3448 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
3450 // Multiply the numerator (operand 0) by the magic value
3451 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
3452 DAG.getConstant(magics.m, VT));
3453 // If d > 0 and m < 0, add the numerator
3454 if (d > 0 && magics.m < 0) {
3455 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
3456 AddToWorkList(Q.Val);
3458 // If d < 0 and m > 0, subtract the numerator.
3459 if (d < 0 && magics.m > 0) {
3460 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
3461 AddToWorkList(Q.Val);
3463 // Shift right algebraic if shift value is nonzero
3465 Q = DAG.getNode(ISD::SRA, VT, Q,
3466 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3467 AddToWorkList(Q.Val);
3469 // Extract the sign bit and add it to the quotient
3471 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
3472 TLI.getShiftAmountTy()));
3473 AddToWorkList(T.Val);
3474 return DAG.getNode(ISD::ADD, VT, Q, T);
3477 /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3478 /// return a DAG expression to select that will generate the same value by
3479 /// multiplying by a magic number. See:
3480 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3481 SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3482 MVT::ValueType VT = N->getValueType(0);
3484 // Check to see if we can do this.
3485 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3486 return SDOperand(); // BuildUDIV only operates on i32 or i64
3487 if (!TLI.isOperationLegal(ISD::MULHU, VT))
3488 return SDOperand(); // Make sure the target supports MULHU.
3490 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
3491 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
3493 // Multiply the numerator (operand 0) by the magic value
3494 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
3495 DAG.getConstant(magics.m, VT));
3496 AddToWorkList(Q.Val);
3498 if (magics.a == 0) {
3499 return DAG.getNode(ISD::SRL, VT, Q,
3500 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3502 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
3503 AddToWorkList(NPQ.Val);
3504 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
3505 DAG.getConstant(1, TLI.getShiftAmountTy()));
3506 AddToWorkList(NPQ.Val);
3507 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
3508 AddToWorkList(NPQ.Val);
3509 return DAG.getNode(ISD::SRL, VT, NPQ,
3510 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
3514 // SelectionDAG::Combine - This is the entry point for the file.
3516 void SelectionDAG::Combine(bool RunningAfterLegalize) {
3517 /// run - This is the main entry point to this class.
3519 DAGCombiner(*this).Run(RunningAfterLegalize);