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 XformToShuffleWithZero(SDNode *N);
222 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
224 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
225 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
226 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
227 SDOperand N3, ISD::CondCode CC);
228 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
229 ISD::CondCode Cond, bool foldBooleans = true);
230 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
231 SDOperand BuildSDIV(SDNode *N);
232 SDOperand BuildUDIV(SDNode *N);
234 DAGCombiner(SelectionDAG &D)
235 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
237 /// Run - runs the dag combiner on all nodes in the work list
238 void Run(bool RunningAfterLegalize);
242 //===----------------------------------------------------------------------===//
243 // TargetLowering::DAGCombinerInfo implementation
244 //===----------------------------------------------------------------------===//
246 void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
247 ((DAGCombiner*)DC)->AddToWorkList(N);
250 SDOperand TargetLowering::DAGCombinerInfo::
251 CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
252 return ((DAGCombiner*)DC)->CombineTo(N, To);
255 SDOperand TargetLowering::DAGCombinerInfo::
256 CombineTo(SDNode *N, SDOperand Res) {
257 return ((DAGCombiner*)DC)->CombineTo(N, Res);
261 SDOperand TargetLowering::DAGCombinerInfo::
262 CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
263 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
269 //===----------------------------------------------------------------------===//
273 int64_t m; // magic number
274 int64_t s; // shift amount
278 uint64_t m; // magic number
279 int64_t a; // add indicator
280 int64_t s; // shift amount
283 /// magic - calculate the magic numbers required to codegen an integer sdiv as
284 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
286 static ms magic32(int32_t d) {
288 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
289 const uint32_t two31 = 0x80000000U;
293 t = two31 + ((uint32_t)d >> 31);
294 anc = t - 1 - t%ad; // absolute value of nc
295 p = 31; // initialize p
296 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
297 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
298 q2 = two31/ad; // initialize q2 = 2p/abs(d)
299 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
302 q1 = 2*q1; // update q1 = 2p/abs(nc)
303 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
304 if (r1 >= anc) { // must be unsigned comparison
308 q2 = 2*q2; // update q2 = 2p/abs(d)
309 r2 = 2*r2; // update r2 = rem(2p/abs(d))
310 if (r2 >= ad) { // must be unsigned comparison
315 } while (q1 < delta || (q1 == delta && r1 == 0));
317 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
318 if (d < 0) mag.m = -mag.m; // resulting magic number
319 mag.s = p - 32; // resulting shift
323 /// magicu - calculate the magic numbers required to codegen an integer udiv as
324 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
325 static mu magicu32(uint32_t d) {
327 uint32_t nc, delta, q1, r1, q2, r2;
329 magu.a = 0; // initialize "add" indicator
331 p = 31; // initialize p
332 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
333 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
334 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
335 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
338 if (r1 >= nc - r1 ) {
339 q1 = 2*q1 + 1; // update q1
340 r1 = 2*r1 - nc; // update r1
343 q1 = 2*q1; // update q1
344 r1 = 2*r1; // update r1
346 if (r2 + 1 >= d - r2) {
347 if (q2 >= 0x7FFFFFFF) magu.a = 1;
348 q2 = 2*q2 + 1; // update q2
349 r2 = 2*r2 + 1 - d; // update r2
352 if (q2 >= 0x80000000) magu.a = 1;
353 q2 = 2*q2; // update q2
354 r2 = 2*r2 + 1; // update r2
357 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
358 magu.m = q2 + 1; // resulting magic number
359 magu.s = p - 32; // resulting shift
363 /// magic - calculate the magic numbers required to codegen an integer sdiv as
364 /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
366 static ms magic64(int64_t d) {
368 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
369 const uint64_t two63 = 9223372036854775808ULL; // 2^63
372 ad = d >= 0 ? d : -d;
373 t = two63 + ((uint64_t)d >> 63);
374 anc = t - 1 - t%ad; // absolute value of nc
375 p = 63; // initialize p
376 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
377 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
378 q2 = two63/ad; // initialize q2 = 2p/abs(d)
379 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
382 q1 = 2*q1; // update q1 = 2p/abs(nc)
383 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
384 if (r1 >= anc) { // must be unsigned comparison
388 q2 = 2*q2; // update q2 = 2p/abs(d)
389 r2 = 2*r2; // update r2 = rem(2p/abs(d))
390 if (r2 >= ad) { // must be unsigned comparison
395 } while (q1 < delta || (q1 == delta && r1 == 0));
398 if (d < 0) mag.m = -mag.m; // resulting magic number
399 mag.s = p - 64; // resulting shift
403 /// magicu - calculate the magic numbers required to codegen an integer udiv as
404 /// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
405 static mu magicu64(uint64_t d)
408 uint64_t nc, delta, q1, r1, q2, r2;
410 magu.a = 0; // initialize "add" indicator
412 p = 63; // initialize p
413 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
414 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
415 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
416 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
419 if (r1 >= nc - r1 ) {
420 q1 = 2*q1 + 1; // update q1
421 r1 = 2*r1 - nc; // update r1
424 q1 = 2*q1; // update q1
425 r1 = 2*r1; // update r1
427 if (r2 + 1 >= d - r2) {
428 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
429 q2 = 2*q2 + 1; // update q2
430 r2 = 2*r2 + 1 - d; // update r2
433 if (q2 >= 0x8000000000000000ull) magu.a = 1;
434 q2 = 2*q2; // update q2
435 r2 = 2*r2 + 1; // update r2
438 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
439 magu.m = q2 + 1; // resulting magic number
440 magu.s = p - 64; // resulting shift
444 // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
445 // that selects between the values 1 and 0, making it equivalent to a setcc.
446 // Also, set the incoming LHS, RHS, and CC references to the appropriate
447 // nodes based on the type of node we are checking. This simplifies life a
448 // bit for the callers.
449 static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
451 if (N.getOpcode() == ISD::SETCC) {
452 LHS = N.getOperand(0);
453 RHS = N.getOperand(1);
454 CC = N.getOperand(2);
457 if (N.getOpcode() == ISD::SELECT_CC &&
458 N.getOperand(2).getOpcode() == ISD::Constant &&
459 N.getOperand(3).getOpcode() == ISD::Constant &&
460 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
461 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
462 LHS = N.getOperand(0);
463 RHS = N.getOperand(1);
464 CC = N.getOperand(4);
470 // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
471 // one use. If this is true, it allows the users to invert the operation for
472 // free when it is profitable to do so.
473 static bool isOneUseSetCC(SDOperand N) {
474 SDOperand N0, N1, N2;
475 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
480 // FIXME: This should probably go in the ISD class rather than being duplicated
482 static bool isCommutativeBinOp(unsigned Opcode) {
488 case ISD::XOR: return true;
489 default: return false; // FIXME: Need commutative info for user ops!
493 SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
494 MVT::ValueType VT = N0.getValueType();
495 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
496 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
497 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
498 if (isa<ConstantSDNode>(N1)) {
499 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
500 AddToWorkList(OpNode.Val);
501 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
502 } else if (N0.hasOneUse()) {
503 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
504 AddToWorkList(OpNode.Val);
505 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
508 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
509 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
510 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
511 if (isa<ConstantSDNode>(N0)) {
512 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
513 AddToWorkList(OpNode.Val);
514 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
515 } else if (N1.hasOneUse()) {
516 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
517 AddToWorkList(OpNode.Val);
518 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
524 void DAGCombiner::Run(bool RunningAfterLegalize) {
525 // set the instance variable, so that the various visit routines may use it.
526 AfterLegalize = RunningAfterLegalize;
528 // Add all the dag nodes to the worklist.
529 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
530 E = DAG.allnodes_end(); I != E; ++I)
531 WorkList.push_back(I);
533 // Create a dummy node (which is not added to allnodes), that adds a reference
534 // to the root node, preventing it from being deleted, and tracking any
535 // changes of the root.
536 HandleSDNode Dummy(DAG.getRoot());
539 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
540 TargetLowering::DAGCombinerInfo
541 DagCombineInfo(DAG, !RunningAfterLegalize, this);
543 // while the worklist isn't empty, inspect the node on the end of it and
544 // try and combine it.
545 while (!WorkList.empty()) {
546 SDNode *N = WorkList.back();
549 // If N has no uses, it is dead. Make sure to revisit all N's operands once
550 // N is deleted from the DAG, since they too may now be dead or may have a
551 // reduced number of uses, allowing other xforms.
552 if (N->use_empty() && N != &Dummy) {
553 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
554 WorkList.push_back(N->getOperand(i).Val);
556 removeFromWorkList(N);
561 SDOperand RV = visit(N);
563 // If nothing happened, try a target-specific DAG combine.
565 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
566 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
567 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
572 // If we get back the same node we passed in, rather than a new node or
573 // zero, we know that the node must have defined multiple values and
574 // CombineTo was used. Since CombineTo takes care of the worklist
575 // mechanics for us, we have no work to do in this case.
577 DEBUG(std::cerr << "\nReplacing "; N->dump();
578 std::cerr << "\nWith: "; RV.Val->dump();
580 std::vector<SDNode*> NowDead;
581 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
583 // Push the new node and any users onto the worklist
584 WorkList.push_back(RV.Val);
585 AddUsersToWorkList(RV.Val);
587 // Nodes can end up on the worklist more than once. Make sure we do
588 // not process a node that has been replaced.
589 removeFromWorkList(N);
590 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
591 removeFromWorkList(NowDead[i]);
593 // Finally, since the node is now dead, remove it from the graph.
599 // If the root changed (e.g. it was a dead load, update the root).
600 DAG.setRoot(Dummy.getValue());
603 SDOperand DAGCombiner::visit(SDNode *N) {
604 switch(N->getOpcode()) {
606 case ISD::TokenFactor: return visitTokenFactor(N);
607 case ISD::ADD: return visitADD(N);
608 case ISD::SUB: return visitSUB(N);
609 case ISD::MUL: return visitMUL(N);
610 case ISD::SDIV: return visitSDIV(N);
611 case ISD::UDIV: return visitUDIV(N);
612 case ISD::SREM: return visitSREM(N);
613 case ISD::UREM: return visitUREM(N);
614 case ISD::MULHU: return visitMULHU(N);
615 case ISD::MULHS: return visitMULHS(N);
616 case ISD::AND: return visitAND(N);
617 case ISD::OR: return visitOR(N);
618 case ISD::XOR: return visitXOR(N);
619 case ISD::SHL: return visitSHL(N);
620 case ISD::SRA: return visitSRA(N);
621 case ISD::SRL: return visitSRL(N);
622 case ISD::CTLZ: return visitCTLZ(N);
623 case ISD::CTTZ: return visitCTTZ(N);
624 case ISD::CTPOP: return visitCTPOP(N);
625 case ISD::SELECT: return visitSELECT(N);
626 case ISD::SELECT_CC: return visitSELECT_CC(N);
627 case ISD::SETCC: return visitSETCC(N);
628 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
629 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
630 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
631 case ISD::TRUNCATE: return visitTRUNCATE(N);
632 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
633 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
634 case ISD::FADD: return visitFADD(N);
635 case ISD::FSUB: return visitFSUB(N);
636 case ISD::FMUL: return visitFMUL(N);
637 case ISD::FDIV: return visitFDIV(N);
638 case ISD::FREM: return visitFREM(N);
639 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
640 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
641 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
642 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
643 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
644 case ISD::FP_ROUND: return visitFP_ROUND(N);
645 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
646 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
647 case ISD::FNEG: return visitFNEG(N);
648 case ISD::FABS: return visitFABS(N);
649 case ISD::BRCOND: return visitBRCOND(N);
650 case ISD::BR_CC: return visitBR_CC(N);
651 case ISD::LOAD: return visitLOAD(N);
654 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
655 case ISD::STORE: return visitSTORE(N);
656 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
657 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
658 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
659 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
660 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
661 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
662 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
663 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
664 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
665 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
666 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
667 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
668 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
673 SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
674 std::vector<SDOperand> Ops;
675 bool Changed = false;
677 // If the token factor has two operands and one is the entry token, replace
678 // the token factor with the other operand.
679 if (N->getNumOperands() == 2) {
680 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
681 return N->getOperand(1);
682 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
683 return N->getOperand(0);
686 // fold (tokenfactor (tokenfactor)) -> tokenfactor
687 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
688 SDOperand Op = N->getOperand(i);
689 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
690 AddToWorkList(Op.Val); // Remove dead node.
692 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
693 Ops.push_back(Op.getOperand(j));
699 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
703 SDOperand DAGCombiner::visitADD(SDNode *N) {
704 SDOperand N0 = N->getOperand(0);
705 SDOperand N1 = N->getOperand(1);
706 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
707 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
708 MVT::ValueType VT = N0.getValueType();
710 // fold (add c1, c2) -> c1+c2
712 return DAG.getNode(ISD::ADD, VT, N0, N1);
713 // canonicalize constant to RHS
715 return DAG.getNode(ISD::ADD, VT, N1, N0);
716 // fold (add x, 0) -> x
717 if (N1C && N1C->isNullValue())
719 // fold ((c1-A)+c2) -> (c1+c2)-A
720 if (N1C && N0.getOpcode() == ISD::SUB)
721 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
722 return DAG.getNode(ISD::SUB, VT,
723 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
726 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
729 // fold ((0-A) + B) -> B-A
730 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
731 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
732 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
733 // fold (A + (0-B)) -> A-B
734 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
735 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
736 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
737 // fold (A+(B-A)) -> B
738 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
739 return N1.getOperand(0);
741 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
742 return SDOperand(N, 0);
744 // fold (a+b) -> (a|b) iff a and b share no bits.
745 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
746 uint64_t LHSZero, LHSOne;
747 uint64_t RHSZero, RHSOne;
748 uint64_t Mask = MVT::getIntVTBitMask(VT);
749 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
751 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
753 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
754 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
755 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
756 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
757 return DAG.getNode(ISD::OR, VT, N0, N1);
764 SDOperand DAGCombiner::visitSUB(SDNode *N) {
765 SDOperand N0 = N->getOperand(0);
766 SDOperand N1 = N->getOperand(1);
767 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
768 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
769 MVT::ValueType VT = N0.getValueType();
771 // fold (sub x, x) -> 0
773 return DAG.getConstant(0, N->getValueType(0));
774 // fold (sub c1, c2) -> c1-c2
776 return DAG.getNode(ISD::SUB, VT, N0, N1);
777 // fold (sub x, c) -> (add x, -c)
779 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
781 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
782 return N0.getOperand(1);
784 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
785 return N0.getOperand(0);
789 SDOperand DAGCombiner::visitMUL(SDNode *N) {
790 SDOperand N0 = N->getOperand(0);
791 SDOperand N1 = N->getOperand(1);
792 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
793 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
794 MVT::ValueType VT = N0.getValueType();
796 // fold (mul c1, c2) -> c1*c2
798 return DAG.getNode(ISD::MUL, VT, N0, N1);
799 // canonicalize constant to RHS
801 return DAG.getNode(ISD::MUL, VT, N1, N0);
802 // fold (mul x, 0) -> 0
803 if (N1C && N1C->isNullValue())
805 // fold (mul x, -1) -> 0-x
806 if (N1C && N1C->isAllOnesValue())
807 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
808 // fold (mul x, (1 << c)) -> x << c
809 if (N1C && isPowerOf2_64(N1C->getValue()))
810 return DAG.getNode(ISD::SHL, VT, N0,
811 DAG.getConstant(Log2_64(N1C->getValue()),
812 TLI.getShiftAmountTy()));
813 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
814 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
815 // FIXME: If the input is something that is easily negated (e.g. a
816 // single-use add), we should put the negate there.
817 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
818 DAG.getNode(ISD::SHL, VT, N0,
819 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
820 TLI.getShiftAmountTy())));
823 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
824 if (N1C && N0.getOpcode() == ISD::SHL &&
825 isa<ConstantSDNode>(N0.getOperand(1))) {
826 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
827 AddToWorkList(C3.Val);
828 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
831 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
834 SDOperand Sh(0,0), Y(0,0);
835 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
836 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
837 N0.Val->hasOneUse()) {
839 } else if (N1.getOpcode() == ISD::SHL &&
840 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
844 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
845 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
848 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
849 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
850 isa<ConstantSDNode>(N0.getOperand(1))) {
851 return DAG.getNode(ISD::ADD, VT,
852 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
853 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
857 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
863 SDOperand DAGCombiner::visitSDIV(SDNode *N) {
864 SDOperand N0 = N->getOperand(0);
865 SDOperand N1 = N->getOperand(1);
866 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
867 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
868 MVT::ValueType VT = N->getValueType(0);
870 // fold (sdiv c1, c2) -> c1/c2
871 if (N0C && N1C && !N1C->isNullValue())
872 return DAG.getNode(ISD::SDIV, VT, N0, N1);
873 // fold (sdiv X, 1) -> X
874 if (N1C && N1C->getSignExtended() == 1LL)
876 // fold (sdiv X, -1) -> 0-X
877 if (N1C && N1C->isAllOnesValue())
878 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
879 // If we know the sign bits of both operands are zero, strength reduce to a
880 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
881 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
882 if (TLI.MaskedValueIsZero(N1, SignBit) &&
883 TLI.MaskedValueIsZero(N0, SignBit))
884 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
885 // fold (sdiv X, pow2) -> simple ops after legalize
886 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
887 (isPowerOf2_64(N1C->getSignExtended()) ||
888 isPowerOf2_64(-N1C->getSignExtended()))) {
889 // If dividing by powers of two is cheap, then don't perform the following
891 if (TLI.isPow2DivCheap())
893 int64_t pow2 = N1C->getSignExtended();
894 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
895 unsigned lg2 = Log2_64(abs2);
896 // Splat the sign bit into the register
897 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
898 DAG.getConstant(MVT::getSizeInBits(VT)-1,
899 TLI.getShiftAmountTy()));
900 AddToWorkList(SGN.Val);
901 // Add (N0 < 0) ? abs2 - 1 : 0;
902 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
903 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
904 TLI.getShiftAmountTy()));
905 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
906 AddToWorkList(SRL.Val);
907 AddToWorkList(ADD.Val); // Divide by pow2
908 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
909 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
910 // If we're dividing by a positive value, we're done. Otherwise, we must
911 // negate the result.
914 AddToWorkList(SRA.Val);
915 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
917 // if integer divide is expensive and we satisfy the requirements, emit an
918 // alternate sequence.
919 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
920 !TLI.isIntDivCheap()) {
921 SDOperand Op = BuildSDIV(N);
922 if (Op.Val) return Op;
927 SDOperand DAGCombiner::visitUDIV(SDNode *N) {
928 SDOperand N0 = N->getOperand(0);
929 SDOperand N1 = N->getOperand(1);
930 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
931 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
932 MVT::ValueType VT = N->getValueType(0);
934 // fold (udiv c1, c2) -> c1/c2
935 if (N0C && N1C && !N1C->isNullValue())
936 return DAG.getNode(ISD::UDIV, VT, N0, N1);
937 // fold (udiv x, (1 << c)) -> x >>u c
938 if (N1C && isPowerOf2_64(N1C->getValue()))
939 return DAG.getNode(ISD::SRL, VT, N0,
940 DAG.getConstant(Log2_64(N1C->getValue()),
941 TLI.getShiftAmountTy()));
942 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
943 if (N1.getOpcode() == ISD::SHL) {
944 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
945 if (isPowerOf2_64(SHC->getValue())) {
946 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
947 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
948 DAG.getConstant(Log2_64(SHC->getValue()),
950 AddToWorkList(Add.Val);
951 return DAG.getNode(ISD::SRL, VT, N0, Add);
955 // fold (udiv x, c) -> alternate
956 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
957 SDOperand Op = BuildUDIV(N);
958 if (Op.Val) return Op;
963 SDOperand DAGCombiner::visitSREM(SDNode *N) {
964 SDOperand N0 = N->getOperand(0);
965 SDOperand N1 = N->getOperand(1);
966 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
967 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
968 MVT::ValueType VT = N->getValueType(0);
970 // fold (srem c1, c2) -> c1%c2
971 if (N0C && N1C && !N1C->isNullValue())
972 return DAG.getNode(ISD::SREM, VT, N0, N1);
973 // If we know the sign bits of both operands are zero, strength reduce to a
974 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
975 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
976 if (TLI.MaskedValueIsZero(N1, SignBit) &&
977 TLI.MaskedValueIsZero(N0, SignBit))
978 return DAG.getNode(ISD::UREM, VT, N0, N1);
982 SDOperand DAGCombiner::visitUREM(SDNode *N) {
983 SDOperand N0 = N->getOperand(0);
984 SDOperand N1 = N->getOperand(1);
985 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
986 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
987 MVT::ValueType VT = N->getValueType(0);
989 // fold (urem c1, c2) -> c1%c2
990 if (N0C && N1C && !N1C->isNullValue())
991 return DAG.getNode(ISD::UREM, VT, N0, N1);
992 // fold (urem x, pow2) -> (and x, pow2-1)
993 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
994 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
995 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
996 if (N1.getOpcode() == ISD::SHL) {
997 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
998 if (isPowerOf2_64(SHC->getValue())) {
999 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
1000 AddToWorkList(Add.Val);
1001 return DAG.getNode(ISD::AND, VT, N0, Add);
1008 SDOperand DAGCombiner::visitMULHS(SDNode *N) {
1009 SDOperand N0 = N->getOperand(0);
1010 SDOperand N1 = N->getOperand(1);
1011 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1013 // fold (mulhs x, 0) -> 0
1014 if (N1C && N1C->isNullValue())
1016 // fold (mulhs x, 1) -> (sra x, size(x)-1)
1017 if (N1C && N1C->getValue() == 1)
1018 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1019 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
1020 TLI.getShiftAmountTy()));
1024 SDOperand DAGCombiner::visitMULHU(SDNode *N) {
1025 SDOperand N0 = N->getOperand(0);
1026 SDOperand N1 = N->getOperand(1);
1027 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1029 // fold (mulhu x, 0) -> 0
1030 if (N1C && N1C->isNullValue())
1032 // fold (mulhu x, 1) -> 0
1033 if (N1C && N1C->getValue() == 1)
1034 return DAG.getConstant(0, N0.getValueType());
1038 SDOperand DAGCombiner::visitAND(SDNode *N) {
1039 SDOperand N0 = N->getOperand(0);
1040 SDOperand N1 = N->getOperand(1);
1041 SDOperand LL, LR, RL, RR, CC0, CC1;
1042 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1043 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1044 MVT::ValueType VT = N1.getValueType();
1045 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1047 // fold (and c1, c2) -> c1&c2
1049 return DAG.getNode(ISD::AND, VT, N0, N1);
1050 // canonicalize constant to RHS
1052 return DAG.getNode(ISD::AND, VT, N1, N0);
1053 // fold (and x, -1) -> x
1054 if (N1C && N1C->isAllOnesValue())
1056 // if (and x, c) is known to be zero, return 0
1057 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
1058 return DAG.getConstant(0, VT);
1060 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1063 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
1064 if (N1C && N0.getOpcode() == ISD::OR)
1065 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1066 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
1068 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1069 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1070 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
1071 if (TLI.MaskedValueIsZero(N0.getOperand(0),
1072 ~N1C->getValue() & InMask)) {
1073 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1076 // Replace uses of the AND with uses of the Zero extend node.
1079 // We actually want to replace all uses of the any_extend with the
1080 // zero_extend, to avoid duplicating things. This will later cause this
1081 // AND to be folded.
1082 CombineTo(N0.Val, Zext);
1083 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1086 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1087 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1088 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1089 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1091 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1092 MVT::isInteger(LL.getValueType())) {
1093 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1094 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1095 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1096 AddToWorkList(ORNode.Val);
1097 return DAG.getSetCC(VT, ORNode, LR, Op1);
1099 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1100 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1101 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1102 AddToWorkList(ANDNode.Val);
1103 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1105 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1106 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1107 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1108 AddToWorkList(ORNode.Val);
1109 return DAG.getSetCC(VT, ORNode, LR, Op1);
1112 // canonicalize equivalent to ll == rl
1113 if (LL == RR && LR == RL) {
1114 Op1 = ISD::getSetCCSwappedOperands(Op1);
1117 if (LL == RL && LR == RR) {
1118 bool isInteger = MVT::isInteger(LL.getValueType());
1119 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1120 if (Result != ISD::SETCC_INVALID)
1121 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1124 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1125 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1126 N1.getOpcode() == ISD::ZERO_EXTEND &&
1127 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1128 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1129 N0.getOperand(0), N1.getOperand(0));
1130 AddToWorkList(ANDNode.Val);
1131 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1133 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
1134 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1135 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1136 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1137 N0.getOperand(1) == N1.getOperand(1)) {
1138 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1139 N0.getOperand(0), N1.getOperand(0));
1140 AddToWorkList(ANDNode.Val);
1141 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1143 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1144 // fold (and (sra)) -> (and (srl)) when possible.
1145 if (!MVT::isVector(VT) &&
1146 SimplifyDemandedBits(SDOperand(N, 0)))
1147 return SDOperand(N, 0);
1148 // fold (zext_inreg (extload x)) -> (zextload x)
1149 if (N0.getOpcode() == ISD::EXTLOAD) {
1150 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1151 // If we zero all the possible extended bits, then we can turn this into
1152 // a zextload if we are running before legalize or the operation is legal.
1153 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
1154 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1155 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1156 N0.getOperand(1), N0.getOperand(2),
1159 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1160 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1163 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
1164 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
1165 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1166 // If we zero all the possible extended bits, then we can turn this into
1167 // a zextload if we are running before legalize or the operation is legal.
1168 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
1169 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1170 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1171 N0.getOperand(1), N0.getOperand(2),
1174 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1175 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1179 // fold (and (load x), 255) -> (zextload x, i8)
1180 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1182 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1183 N0.getOpcode() == ISD::ZEXTLOAD) &&
1185 MVT::ValueType EVT, LoadedVT;
1186 if (N1C->getValue() == 255)
1188 else if (N1C->getValue() == 65535)
1190 else if (N1C->getValue() == ~0U)
1195 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1196 cast<VTSDNode>(N0.getOperand(3))->getVT();
1197 if (EVT != MVT::Other && LoadedVT > EVT &&
1198 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1199 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1200 // For big endian targets, we need to add an offset to the pointer to load
1201 // the correct bytes. For little endian systems, we merely need to read
1202 // fewer bytes from the same pointer.
1204 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1205 SDOperand NewPtr = N0.getOperand(1);
1206 if (!TLI.isLittleEndian())
1207 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1208 DAG.getConstant(PtrOff, PtrType));
1209 AddToWorkList(NewPtr.Val);
1211 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1212 N0.getOperand(2), EVT);
1214 CombineTo(N0.Val, Load, Load.getValue(1));
1215 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1222 SDOperand DAGCombiner::visitOR(SDNode *N) {
1223 SDOperand N0 = N->getOperand(0);
1224 SDOperand N1 = N->getOperand(1);
1225 SDOperand LL, LR, RL, RR, CC0, CC1;
1226 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1227 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1228 MVT::ValueType VT = N1.getValueType();
1229 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1231 // fold (or c1, c2) -> c1|c2
1233 return DAG.getNode(ISD::OR, VT, N0, N1);
1234 // canonicalize constant to RHS
1236 return DAG.getNode(ISD::OR, VT, N1, N0);
1237 // fold (or x, 0) -> x
1238 if (N1C && N1C->isNullValue())
1240 // fold (or x, -1) -> -1
1241 if (N1C && N1C->isAllOnesValue())
1243 // fold (or x, c) -> c iff (x & ~c) == 0
1245 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
1248 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1251 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1252 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
1253 isa<ConstantSDNode>(N0.getOperand(1))) {
1254 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1255 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1257 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
1259 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1260 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1261 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1262 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1264 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1265 MVT::isInteger(LL.getValueType())) {
1266 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1267 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1268 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1269 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1270 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1271 AddToWorkList(ORNode.Val);
1272 return DAG.getSetCC(VT, ORNode, LR, Op1);
1274 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1275 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1276 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1277 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1278 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1279 AddToWorkList(ANDNode.Val);
1280 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1283 // canonicalize equivalent to ll == rl
1284 if (LL == RR && LR == RL) {
1285 Op1 = ISD::getSetCCSwappedOperands(Op1);
1288 if (LL == RL && LR == RR) {
1289 bool isInteger = MVT::isInteger(LL.getValueType());
1290 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1291 if (Result != ISD::SETCC_INVALID)
1292 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1295 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1296 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1297 N1.getOpcode() == ISD::ZERO_EXTEND &&
1298 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1299 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1300 N0.getOperand(0), N1.getOperand(0));
1301 AddToWorkList(ORNode.Val);
1302 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1304 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1305 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1306 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1307 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1308 N0.getOperand(1) == N1.getOperand(1)) {
1309 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1310 N0.getOperand(0), N1.getOperand(0));
1311 AddToWorkList(ORNode.Val);
1312 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1314 // canonicalize shl to left side in a shl/srl pair, to match rotate
1315 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1317 // check for rotl, rotr
1318 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1319 N0.getOperand(0) == N1.getOperand(0) &&
1320 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
1321 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1322 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1323 N1.getOperand(1).getOpcode() == ISD::Constant) {
1324 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1325 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1326 if ((c1val + c2val) == OpSizeInBits)
1327 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1329 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1330 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1331 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1332 if (ConstantSDNode *SUBC =
1333 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1334 if (SUBC->getValue() == OpSizeInBits)
1335 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1336 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1337 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1338 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1339 if (ConstantSDNode *SUBC =
1340 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1341 if (SUBC->getValue() == OpSizeInBits) {
1342 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
1343 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1346 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1353 SDOperand DAGCombiner::visitXOR(SDNode *N) {
1354 SDOperand N0 = N->getOperand(0);
1355 SDOperand N1 = N->getOperand(1);
1356 SDOperand LHS, RHS, CC;
1357 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1358 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1359 MVT::ValueType VT = N0.getValueType();
1361 // fold (xor c1, c2) -> c1^c2
1363 return DAG.getNode(ISD::XOR, VT, N0, N1);
1364 // canonicalize constant to RHS
1366 return DAG.getNode(ISD::XOR, VT, N1, N0);
1367 // fold (xor x, 0) -> x
1368 if (N1C && N1C->isNullValue())
1371 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1374 // fold !(x cc y) -> (x !cc y)
1375 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1376 bool isInt = MVT::isInteger(LHS.getValueType());
1377 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1379 if (N0.getOpcode() == ISD::SETCC)
1380 return DAG.getSetCC(VT, LHS, RHS, NotCC);
1381 if (N0.getOpcode() == ISD::SELECT_CC)
1382 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
1383 assert(0 && "Unhandled SetCC Equivalent!");
1386 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1387 if (N1C && N1C->getValue() == 1 &&
1388 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
1389 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
1390 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1391 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
1392 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1393 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
1394 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
1395 return DAG.getNode(NewOpcode, VT, LHS, RHS);
1398 // fold !(x or y) -> (!x and !y) iff x or y are constants
1399 if (N1C && N1C->isAllOnesValue() &&
1400 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
1401 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
1402 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1403 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
1404 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1405 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
1406 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
1407 return DAG.getNode(NewOpcode, VT, LHS, RHS);
1410 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1411 if (N1C && N0.getOpcode() == ISD::XOR) {
1412 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1413 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1415 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1416 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1418 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1419 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1421 // fold (xor x, x) -> 0
1423 if (!MVT::isVector(VT)) {
1424 return DAG.getConstant(0, VT);
1425 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1426 // Produce a vector of zeros.
1427 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1428 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1429 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
1432 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1433 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1434 N1.getOpcode() == ISD::ZERO_EXTEND &&
1435 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1436 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1437 N0.getOperand(0), N1.getOperand(0));
1438 AddToWorkList(XORNode.Val);
1439 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1441 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1442 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1443 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1444 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1445 N0.getOperand(1) == N1.getOperand(1)) {
1446 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1447 N0.getOperand(0), N1.getOperand(0));
1448 AddToWorkList(XORNode.Val);
1449 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1452 // Simplify the expression using non-local knowledge.
1453 if (!MVT::isVector(VT) &&
1454 SimplifyDemandedBits(SDOperand(N, 0)))
1455 return SDOperand(N, 0);
1460 SDOperand DAGCombiner::visitSHL(SDNode *N) {
1461 SDOperand N0 = N->getOperand(0);
1462 SDOperand N1 = N->getOperand(1);
1463 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1464 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1465 MVT::ValueType VT = N0.getValueType();
1466 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1468 // fold (shl c1, c2) -> c1<<c2
1470 return DAG.getNode(ISD::SHL, VT, N0, N1);
1471 // fold (shl 0, x) -> 0
1472 if (N0C && N0C->isNullValue())
1474 // fold (shl x, c >= size(x)) -> undef
1475 if (N1C && N1C->getValue() >= OpSizeInBits)
1476 return DAG.getNode(ISD::UNDEF, VT);
1477 // fold (shl x, 0) -> x
1478 if (N1C && N1C->isNullValue())
1480 // if (shl x, c) is known to be zero, return 0
1481 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
1482 return DAG.getConstant(0, VT);
1483 if (SimplifyDemandedBits(SDOperand(N, 0)))
1484 return SDOperand(N, 0);
1485 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
1486 if (N1C && N0.getOpcode() == ISD::SHL &&
1487 N0.getOperand(1).getOpcode() == ISD::Constant) {
1488 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1489 uint64_t c2 = N1C->getValue();
1490 if (c1 + c2 > OpSizeInBits)
1491 return DAG.getConstant(0, VT);
1492 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
1493 DAG.getConstant(c1 + c2, N1.getValueType()));
1495 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1496 // (srl (and x, -1 << c1), c1-c2)
1497 if (N1C && N0.getOpcode() == ISD::SRL &&
1498 N0.getOperand(1).getOpcode() == ISD::Constant) {
1499 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1500 uint64_t c2 = N1C->getValue();
1501 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1502 DAG.getConstant(~0ULL << c1, VT));
1504 return DAG.getNode(ISD::SHL, VT, Mask,
1505 DAG.getConstant(c2-c1, N1.getValueType()));
1507 return DAG.getNode(ISD::SRL, VT, Mask,
1508 DAG.getConstant(c1-c2, N1.getValueType()));
1510 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
1511 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
1512 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1513 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1514 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1515 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1516 isa<ConstantSDNode>(N0.getOperand(1))) {
1517 return DAG.getNode(ISD::ADD, VT,
1518 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1519 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1524 SDOperand DAGCombiner::visitSRA(SDNode *N) {
1525 SDOperand N0 = N->getOperand(0);
1526 SDOperand N1 = N->getOperand(1);
1527 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1528 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1529 MVT::ValueType VT = N0.getValueType();
1531 // fold (sra c1, c2) -> c1>>c2
1533 return DAG.getNode(ISD::SRA, VT, N0, N1);
1534 // fold (sra 0, x) -> 0
1535 if (N0C && N0C->isNullValue())
1537 // fold (sra -1, x) -> -1
1538 if (N0C && N0C->isAllOnesValue())
1540 // fold (sra x, c >= size(x)) -> undef
1541 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
1542 return DAG.getNode(ISD::UNDEF, VT);
1543 // fold (sra x, 0) -> x
1544 if (N1C && N1C->isNullValue())
1546 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1548 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1549 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1552 default: EVT = MVT::Other; break;
1553 case 1: EVT = MVT::i1; break;
1554 case 8: EVT = MVT::i8; break;
1555 case 16: EVT = MVT::i16; break;
1556 case 32: EVT = MVT::i32; break;
1558 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1559 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1560 DAG.getValueType(EVT));
1563 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1564 if (N1C && N0.getOpcode() == ISD::SRA) {
1565 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1566 unsigned Sum = N1C->getValue() + C1->getValue();
1567 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1568 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1569 DAG.getConstant(Sum, N1C->getValueType(0)));
1573 // If the sign bit is known to be zero, switch this to a SRL.
1574 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
1575 return DAG.getNode(ISD::SRL, VT, N0, N1);
1579 SDOperand DAGCombiner::visitSRL(SDNode *N) {
1580 SDOperand N0 = N->getOperand(0);
1581 SDOperand N1 = N->getOperand(1);
1582 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1583 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1584 MVT::ValueType VT = N0.getValueType();
1585 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1587 // fold (srl c1, c2) -> c1 >>u c2
1589 return DAG.getNode(ISD::SRL, VT, N0, N1);
1590 // fold (srl 0, x) -> 0
1591 if (N0C && N0C->isNullValue())
1593 // fold (srl x, c >= size(x)) -> undef
1594 if (N1C && N1C->getValue() >= OpSizeInBits)
1595 return DAG.getNode(ISD::UNDEF, VT);
1596 // fold (srl x, 0) -> x
1597 if (N1C && N1C->isNullValue())
1599 // if (srl x, c) is known to be zero, return 0
1600 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
1601 return DAG.getConstant(0, VT);
1602 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
1603 if (N1C && N0.getOpcode() == ISD::SRL &&
1604 N0.getOperand(1).getOpcode() == ISD::Constant) {
1605 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1606 uint64_t c2 = N1C->getValue();
1607 if (c1 + c2 > OpSizeInBits)
1608 return DAG.getConstant(0, VT);
1609 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
1610 DAG.getConstant(c1 + c2, N1.getValueType()));
1613 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1614 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1615 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1616 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1617 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1619 // If any of the input bits are KnownOne, then the input couldn't be all
1620 // zeros, thus the result of the srl will always be zero.
1621 if (KnownOne) return DAG.getConstant(0, VT);
1623 // If all of the bits input the to ctlz node are known to be zero, then
1624 // the result of the ctlz is "32" and the result of the shift is one.
1625 uint64_t UnknownBits = ~KnownZero & Mask;
1626 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1628 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1629 if ((UnknownBits & (UnknownBits-1)) == 0) {
1630 // Okay, we know that only that the single bit specified by UnknownBits
1631 // could be set on input to the CTLZ node. If this bit is set, the SRL
1632 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1633 // to an SRL,XOR pair, which is likely to simplify more.
1634 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1635 SDOperand Op = N0.getOperand(0);
1637 Op = DAG.getNode(ISD::SRL, VT, Op,
1638 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1639 AddToWorkList(Op.Val);
1641 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1648 SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
1649 SDOperand N0 = N->getOperand(0);
1650 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1651 MVT::ValueType VT = N->getValueType(0);
1653 // fold (ctlz c1) -> c2
1655 return DAG.getNode(ISD::CTLZ, VT, N0);
1659 SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
1660 SDOperand N0 = N->getOperand(0);
1661 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1662 MVT::ValueType VT = N->getValueType(0);
1664 // fold (cttz c1) -> c2
1666 return DAG.getNode(ISD::CTTZ, VT, N0);
1670 SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
1671 SDOperand N0 = N->getOperand(0);
1672 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1673 MVT::ValueType VT = N->getValueType(0);
1675 // fold (ctpop c1) -> c2
1677 return DAG.getNode(ISD::CTPOP, VT, N0);
1681 SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1682 SDOperand N0 = N->getOperand(0);
1683 SDOperand N1 = N->getOperand(1);
1684 SDOperand N2 = N->getOperand(2);
1685 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1686 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1687 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1688 MVT::ValueType VT = N->getValueType(0);
1690 // fold select C, X, X -> X
1693 // fold select true, X, Y -> X
1694 if (N0C && !N0C->isNullValue())
1696 // fold select false, X, Y -> Y
1697 if (N0C && N0C->isNullValue())
1699 // fold select C, 1, X -> C | X
1700 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
1701 return DAG.getNode(ISD::OR, VT, N0, N2);
1702 // fold select C, 0, X -> ~C & X
1703 // FIXME: this should check for C type == X type, not i1?
1704 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1705 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1706 AddToWorkList(XORNode.Val);
1707 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1709 // fold select C, X, 1 -> ~C | X
1710 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
1711 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1712 AddToWorkList(XORNode.Val);
1713 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1715 // fold select C, X, 0 -> C & X
1716 // FIXME: this should check for C type == X type, not i1?
1717 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1718 return DAG.getNode(ISD::AND, VT, N0, N1);
1719 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1720 if (MVT::i1 == VT && N0 == N1)
1721 return DAG.getNode(ISD::OR, VT, N0, N2);
1722 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1723 if (MVT::i1 == VT && N0 == N2)
1724 return DAG.getNode(ISD::AND, VT, N0, N1);
1725 // If we can fold this based on the true/false value, do so.
1726 if (SimplifySelectOps(N, N1, N2))
1728 // fold selects based on a setcc into other things, such as min/max/abs
1729 if (N0.getOpcode() == ISD::SETCC)
1731 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1732 // having to say they don't support SELECT_CC on every type the DAG knows
1733 // about, since there is no way to mark an opcode illegal at all value types
1734 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1735 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1736 N1, N2, N0.getOperand(2));
1738 return SimplifySelect(N0, N1, N2);
1742 SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
1743 SDOperand N0 = N->getOperand(0);
1744 SDOperand N1 = N->getOperand(1);
1745 SDOperand N2 = N->getOperand(2);
1746 SDOperand N3 = N->getOperand(3);
1747 SDOperand N4 = N->getOperand(4);
1748 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1749 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1750 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1751 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1753 // Determine if the condition we're dealing with is constant
1754 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1755 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1757 // fold select_cc lhs, rhs, x, x, cc -> x
1761 // If we can fold this based on the true/false value, do so.
1762 if (SimplifySelectOps(N, N2, N3))
1765 // fold select_cc into other things, such as min/max/abs
1766 return SimplifySelectCC(N0, N1, N2, N3, CC);
1769 SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1770 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1771 cast<CondCodeSDNode>(N->getOperand(2))->get());
1774 SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
1775 SDOperand N0 = N->getOperand(0);
1776 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1777 MVT::ValueType VT = N->getValueType(0);
1779 // fold (sext c1) -> c1
1781 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
1782 // fold (sext (sext x)) -> (sext x)
1783 if (N0.getOpcode() == ISD::SIGN_EXTEND)
1784 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1785 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
1786 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1788 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
1789 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1790 DAG.getValueType(N0.getValueType()));
1791 // fold (sext (load x)) -> (sext (truncate (sextload x)))
1792 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1793 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
1794 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1795 N0.getOperand(1), N0.getOperand(2),
1797 CombineTo(N, ExtLoad);
1798 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1799 ExtLoad.getValue(1));
1800 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1803 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1804 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1805 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1807 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1808 N0.getOperand(1), N0.getOperand(2),
1810 CombineTo(N, ExtLoad);
1811 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1812 ExtLoad.getValue(1));
1813 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1819 SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
1820 SDOperand N0 = N->getOperand(0);
1821 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1822 MVT::ValueType VT = N->getValueType(0);
1824 // fold (zext c1) -> c1
1826 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1827 // fold (zext (zext x)) -> (zext x)
1828 if (N0.getOpcode() == ISD::ZERO_EXTEND)
1829 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1830 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1831 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1832 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
1833 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
1834 // fold (zext (load x)) -> (zext (truncate (zextload x)))
1835 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1836 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
1837 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1838 N0.getOperand(1), N0.getOperand(2),
1840 CombineTo(N, ExtLoad);
1841 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1842 ExtLoad.getValue(1));
1843 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1846 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1847 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1848 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1850 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1851 N0.getOperand(1), N0.getOperand(2),
1853 CombineTo(N, ExtLoad);
1854 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1855 ExtLoad.getValue(1));
1856 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1861 SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
1862 SDOperand N0 = N->getOperand(0);
1863 SDOperand N1 = N->getOperand(1);
1864 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1865 MVT::ValueType VT = N->getValueType(0);
1866 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
1867 unsigned EVTBits = MVT::getSizeInBits(EVT);
1869 // fold (sext_in_reg c1) -> c1
1871 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
1872 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
1874 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
1875 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1876 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
1879 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1880 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1881 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
1882 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
1884 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1885 if (N0.getOpcode() == ISD::AssertSext &&
1886 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
1889 // fold (sext_in_reg (sextload x)) -> (sextload x)
1890 if (N0.getOpcode() == ISD::SEXTLOAD &&
1891 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
1894 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
1895 if (N0.getOpcode() == ISD::SETCC &&
1896 TLI.getSetCCResultContents() ==
1897 TargetLowering::ZeroOrNegativeOneSetCCResult)
1899 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1900 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
1901 return DAG.getZeroExtendInReg(N0, EVT);
1902 // fold (sext_inreg (extload x)) -> (sextload x)
1903 if (N0.getOpcode() == ISD::EXTLOAD &&
1904 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
1905 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
1906 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1907 N0.getOperand(1), N0.getOperand(2),
1909 CombineTo(N, ExtLoad);
1910 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1911 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1913 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
1914 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
1915 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
1916 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
1917 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1918 N0.getOperand(1), N0.getOperand(2),
1920 CombineTo(N, ExtLoad);
1921 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1922 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1927 SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
1928 SDOperand N0 = N->getOperand(0);
1929 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1930 MVT::ValueType VT = N->getValueType(0);
1933 if (N0.getValueType() == N->getValueType(0))
1935 // fold (truncate c1) -> c1
1937 return DAG.getNode(ISD::TRUNCATE, VT, N0);
1938 // fold (truncate (truncate x)) -> (truncate x)
1939 if (N0.getOpcode() == ISD::TRUNCATE)
1940 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
1941 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1942 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1943 if (N0.getValueType() < VT)
1944 // if the source is smaller than the dest, we still need an extend
1945 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1946 else if (N0.getValueType() > VT)
1947 // if the source is larger than the dest, than we just need the truncate
1948 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
1950 // if the source and dest are the same type, we can drop both the extend
1952 return N0.getOperand(0);
1954 // fold (truncate (load x)) -> (smaller load x)
1955 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
1956 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1957 "Cannot truncate to larger type!");
1958 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1959 // For big endian targets, we need to add an offset to the pointer to load
1960 // the correct bytes. For little endian systems, we merely need to read
1961 // fewer bytes from the same pointer.
1963 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
1964 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1965 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1966 DAG.getConstant(PtrOff, PtrType));
1967 AddToWorkList(NewPtr.Val);
1968 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
1970 CombineTo(N0.Val, Load, Load.getValue(1));
1971 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1976 SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1977 SDOperand N0 = N->getOperand(0);
1978 MVT::ValueType VT = N->getValueType(0);
1980 // If the input is a constant, let getNode() fold it.
1981 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1982 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1983 if (Res.Val != N) return Res;
1986 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1987 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1989 // fold (conv (load x)) -> (load (conv*)x)
1990 // FIXME: These xforms need to know that the resultant load doesn't need a
1991 // higher alignment than the original!
1992 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
1993 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1996 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2004 SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2005 SDOperand N0 = N->getOperand(0);
2006 MVT::ValueType VT = N->getValueType(0);
2008 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2009 // First check to see if this is all constant.
2010 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2011 VT == MVT::Vector) {
2012 bool isSimple = true;
2013 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2014 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2015 N0.getOperand(i).getOpcode() != ISD::Constant &&
2016 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2021 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2022 if (isSimple && !MVT::isVector(DestEltVT)) {
2023 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2030 /// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2031 /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2032 /// destination element value type.
2033 SDOperand DAGCombiner::
2034 ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2035 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2037 // If this is already the right type, we're done.
2038 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2040 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2041 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2043 // If this is a conversion of N elements of one type to N elements of another
2044 // type, convert each element. This handles FP<->INT cases.
2045 if (SrcBitSize == DstBitSize) {
2046 std::vector<SDOperand> Ops;
2047 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2048 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
2049 AddToWorkList(Ops.back().Val);
2051 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2052 Ops.push_back(DAG.getValueType(DstEltVT));
2053 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2056 // Otherwise, we're growing or shrinking the elements. To avoid having to
2057 // handle annoying details of growing/shrinking FP values, we convert them to
2059 if (MVT::isFloatingPoint(SrcEltVT)) {
2060 // Convert the input float vector to a int vector where the elements are the
2062 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2063 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2064 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2068 // Now we know the input is an integer vector. If the output is a FP type,
2069 // convert to integer first, then to FP of the right size.
2070 if (MVT::isFloatingPoint(DstEltVT)) {
2071 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2072 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2073 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2075 // Next, convert to FP elements of the same size.
2076 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2079 // Okay, we know the src/dst types are both integers of differing types.
2080 // Handling growing first.
2081 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2082 if (SrcBitSize < DstBitSize) {
2083 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2085 std::vector<SDOperand> Ops;
2086 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2087 i += NumInputsPerOutput) {
2088 bool isLE = TLI.isLittleEndian();
2089 uint64_t NewBits = 0;
2090 bool EltIsUndef = true;
2091 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2092 // Shift the previously computed bits over.
2093 NewBits <<= SrcBitSize;
2094 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2095 if (Op.getOpcode() == ISD::UNDEF) continue;
2098 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2102 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2104 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2107 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2108 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2109 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2112 // Finally, this must be the case where we are shrinking elements: each input
2113 // turns into multiple outputs.
2114 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
2115 std::vector<SDOperand> Ops;
2116 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2117 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2118 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2119 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2122 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2124 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2125 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2126 OpVal >>= DstBitSize;
2127 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2130 // For big endian targets, swap the order of the pieces of each element.
2131 if (!TLI.isLittleEndian())
2132 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2134 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2135 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2136 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2141 SDOperand DAGCombiner::visitFADD(SDNode *N) {
2142 SDOperand N0 = N->getOperand(0);
2143 SDOperand N1 = N->getOperand(1);
2144 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2145 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2146 MVT::ValueType VT = N->getValueType(0);
2148 // fold (fadd c1, c2) -> c1+c2
2150 return DAG.getNode(ISD::FADD, VT, N0, N1);
2151 // canonicalize constant to RHS
2152 if (N0CFP && !N1CFP)
2153 return DAG.getNode(ISD::FADD, VT, N1, N0);
2154 // fold (A + (-B)) -> A-B
2155 if (N1.getOpcode() == ISD::FNEG)
2156 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
2157 // fold ((-A) + B) -> B-A
2158 if (N0.getOpcode() == ISD::FNEG)
2159 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
2163 SDOperand DAGCombiner::visitFSUB(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 (fsub c1, c2) -> c1-c2
2172 return DAG.getNode(ISD::FSUB, VT, N0, N1);
2173 // fold (A-(-B)) -> A+B
2174 if (N1.getOpcode() == ISD::FNEG)
2175 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
2179 SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2180 SDOperand N0 = N->getOperand(0);
2181 SDOperand N1 = N->getOperand(1);
2182 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2183 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2184 MVT::ValueType VT = N->getValueType(0);
2186 // fold (fmul c1, c2) -> c1*c2
2188 return DAG.getNode(ISD::FMUL, VT, N0, N1);
2189 // canonicalize constant to RHS
2190 if (N0CFP && !N1CFP)
2191 return DAG.getNode(ISD::FMUL, VT, N1, N0);
2192 // fold (fmul X, 2.0) -> (fadd X, X)
2193 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2194 return DAG.getNode(ISD::FADD, VT, N0, N0);
2198 SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2199 SDOperand N0 = N->getOperand(0);
2200 SDOperand N1 = N->getOperand(1);
2201 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2202 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2203 MVT::ValueType VT = N->getValueType(0);
2205 // fold (fdiv c1, c2) -> c1/c2
2207 return DAG.getNode(ISD::FDIV, VT, N0, N1);
2211 SDOperand DAGCombiner::visitFREM(SDNode *N) {
2212 SDOperand N0 = N->getOperand(0);
2213 SDOperand N1 = N->getOperand(1);
2214 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2215 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2216 MVT::ValueType VT = N->getValueType(0);
2218 // fold (frem c1, c2) -> fmod(c1,c2)
2220 return DAG.getNode(ISD::FREM, VT, N0, N1);
2224 SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2225 SDOperand N0 = N->getOperand(0);
2226 SDOperand N1 = N->getOperand(1);
2227 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2228 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2229 MVT::ValueType VT = N->getValueType(0);
2231 if (N0CFP && N1CFP) // Constant fold
2232 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2235 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2236 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2241 u.d = N1CFP->getValue();
2243 return DAG.getNode(ISD::FABS, VT, N0);
2245 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2248 // copysign(fabs(x), y) -> copysign(x, y)
2249 // copysign(fneg(x), y) -> copysign(x, y)
2250 // copysign(copysign(x,z), y) -> copysign(x, y)
2251 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2252 N0.getOpcode() == ISD::FCOPYSIGN)
2253 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2255 // copysign(x, abs(y)) -> abs(x)
2256 if (N1.getOpcode() == ISD::FABS)
2257 return DAG.getNode(ISD::FABS, VT, N0);
2259 // copysign(x, copysign(y,z)) -> copysign(x, z)
2260 if (N1.getOpcode() == ISD::FCOPYSIGN)
2261 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2263 // copysign(x, fp_extend(y)) -> copysign(x, y)
2264 // copysign(x, fp_round(y)) -> copysign(x, y)
2265 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2266 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2273 SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
2274 SDOperand N0 = N->getOperand(0);
2275 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2276 MVT::ValueType VT = N->getValueType(0);
2278 // fold (sint_to_fp c1) -> c1fp
2280 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
2284 SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
2285 SDOperand N0 = N->getOperand(0);
2286 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2287 MVT::ValueType VT = N->getValueType(0);
2289 // fold (uint_to_fp c1) -> c1fp
2291 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
2295 SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
2296 SDOperand N0 = N->getOperand(0);
2297 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2298 MVT::ValueType VT = N->getValueType(0);
2300 // fold (fp_to_sint c1fp) -> c1
2302 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
2306 SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
2307 SDOperand N0 = N->getOperand(0);
2308 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2309 MVT::ValueType VT = N->getValueType(0);
2311 // fold (fp_to_uint c1fp) -> c1
2313 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
2317 SDOperand DAGCombiner::visitFP_ROUND(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_round c1fp) -> c1fp
2324 return DAG.getNode(ISD::FP_ROUND, VT, N0);
2326 // fold (fp_round (fp_extend x)) -> x
2327 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2328 return N0.getOperand(0);
2330 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2331 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2332 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2333 AddToWorkList(Tmp.Val);
2334 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2340 SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
2341 SDOperand N0 = N->getOperand(0);
2342 MVT::ValueType VT = N->getValueType(0);
2343 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2344 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2346 // fold (fp_round_inreg c1fp) -> c1fp
2348 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
2349 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
2354 SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
2355 SDOperand N0 = N->getOperand(0);
2356 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2357 MVT::ValueType VT = N->getValueType(0);
2359 // fold (fp_extend c1fp) -> c1fp
2361 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
2365 SDOperand DAGCombiner::visitFNEG(SDNode *N) {
2366 SDOperand N0 = N->getOperand(0);
2367 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2368 MVT::ValueType VT = N->getValueType(0);
2370 // fold (fneg c1) -> -c1
2372 return DAG.getNode(ISD::FNEG, VT, N0);
2373 // fold (fneg (sub x, y)) -> (sub y, x)
2374 if (N0.getOpcode() == ISD::SUB)
2375 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
2376 // fold (fneg (fneg x)) -> x
2377 if (N0.getOpcode() == ISD::FNEG)
2378 return N0.getOperand(0);
2382 SDOperand DAGCombiner::visitFABS(SDNode *N) {
2383 SDOperand N0 = N->getOperand(0);
2384 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2385 MVT::ValueType VT = N->getValueType(0);
2387 // fold (fabs c1) -> fabs(c1)
2389 return DAG.getNode(ISD::FABS, VT, N0);
2390 // fold (fabs (fabs x)) -> (fabs x)
2391 if (N0.getOpcode() == ISD::FABS)
2392 return N->getOperand(0);
2393 // fold (fabs (fneg x)) -> (fabs x)
2394 // fold (fabs (fcopysign x, y)) -> (fabs x)
2395 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2396 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2401 SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2402 SDOperand Chain = N->getOperand(0);
2403 SDOperand N1 = N->getOperand(1);
2404 SDOperand N2 = N->getOperand(2);
2405 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2407 // never taken branch, fold to chain
2408 if (N1C && N1C->isNullValue())
2410 // unconditional branch
2411 if (N1C && N1C->getValue() == 1)
2412 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2413 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2415 if (N1.getOpcode() == ISD::SETCC &&
2416 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2417 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2418 N1.getOperand(0), N1.getOperand(1), N2);
2423 // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2425 SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
2426 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2427 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2429 // Use SimplifySetCC to simplify SETCC's.
2430 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2431 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2433 // fold br_cc true, dest -> br dest (unconditional branch)
2434 if (SCCC && SCCC->getValue())
2435 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2437 // fold br_cc false, dest -> unconditional fall through
2438 if (SCCC && SCCC->isNullValue())
2439 return N->getOperand(0);
2440 // fold to a simpler setcc
2441 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2442 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2443 Simp.getOperand(2), Simp.getOperand(0),
2444 Simp.getOperand(1), N->getOperand(4));
2448 SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2449 SDOperand Chain = N->getOperand(0);
2450 SDOperand Ptr = N->getOperand(1);
2451 SDOperand SrcValue = N->getOperand(2);
2453 // If there are no uses of the loaded value, change uses of the chain value
2454 // into uses of the chain input (i.e. delete the dead load).
2455 if (N->hasNUsesOfValue(0, 0))
2456 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2458 // If this load is directly stored, replace the load value with the stored
2460 // TODO: Handle store large -> read small portion.
2461 // TODO: Handle TRUNCSTORE/EXTLOAD
2462 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2463 Chain.getOperand(1).getValueType() == N->getValueType(0))
2464 return CombineTo(N, Chain.getOperand(1), Chain);
2469 /// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2470 SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2471 SDOperand Chain = N->getOperand(0);
2472 SDOperand Ptr = N->getOperand(1);
2473 SDOperand SrcValue = N->getOperand(2);
2474 SDOperand EVT = N->getOperand(3);
2476 // If there are no uses of the loaded value, change uses of the chain value
2477 // into uses of the chain input (i.e. delete the dead load).
2478 if (N->hasNUsesOfValue(0, 0))
2479 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2484 SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2485 SDOperand Chain = N->getOperand(0);
2486 SDOperand Value = N->getOperand(1);
2487 SDOperand Ptr = N->getOperand(2);
2488 SDOperand SrcValue = N->getOperand(3);
2490 // If this is a store that kills a previous store, remove the previous store.
2491 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2492 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2493 // Make sure that these stores are the same value type:
2494 // FIXME: we really care that the second store is >= size of the first.
2495 Value.getValueType() == Chain.getOperand(1).getValueType()) {
2496 // Create a new store of Value that replaces both stores.
2497 SDNode *PrevStore = Chain.Val;
2498 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2500 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2501 PrevStore->getOperand(0), Value, Ptr,
2503 CombineTo(N, NewStore); // Nuke this store.
2504 CombineTo(PrevStore, NewStore); // Nuke the previous store.
2505 return SDOperand(N, 0);
2508 // If this is a store of a bit convert, store the input value.
2509 // FIXME: This needs to know that the resultant store does not need a
2510 // higher alignment than the original.
2511 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
2512 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2518 SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2519 SDOperand InVec = N->getOperand(0);
2520 SDOperand InVal = N->getOperand(1);
2521 SDOperand EltNo = N->getOperand(2);
2523 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2524 // vector with the inserted element.
2525 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2526 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2527 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2528 if (Elt < Ops.size())
2530 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), Ops);
2536 SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2537 SDOperand InVec = N->getOperand(0);
2538 SDOperand InVal = N->getOperand(1);
2539 SDOperand EltNo = N->getOperand(2);
2540 SDOperand NumElts = N->getOperand(3);
2541 SDOperand EltType = N->getOperand(4);
2543 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2544 // vector with the inserted element.
2545 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2546 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2547 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2548 if (Elt < Ops.size()-2)
2550 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), Ops);
2556 SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2557 unsigned NumInScalars = N->getNumOperands()-2;
2558 SDOperand NumElts = N->getOperand(NumInScalars);
2559 SDOperand EltType = N->getOperand(NumInScalars+1);
2561 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2562 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2563 // two distinct vectors, turn this into a shuffle node.
2564 SDOperand VecIn1, VecIn2;
2565 for (unsigned i = 0; i != NumInScalars; ++i) {
2566 // Ignore undef inputs.
2567 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2569 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2570 // constant index, bail out.
2571 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2572 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2573 VecIn1 = VecIn2 = SDOperand(0, 0);
2577 // If the input vector type disagrees with the result of the vbuild_vector,
2578 // we can't make a shuffle.
2579 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2580 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2581 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2582 VecIn1 = VecIn2 = SDOperand(0, 0);
2586 // Otherwise, remember this. We allow up to two distinct input vectors.
2587 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2590 if (VecIn1.Val == 0) {
2591 VecIn1 = ExtractedFromVec;
2592 } else if (VecIn2.Val == 0) {
2593 VecIn2 = ExtractedFromVec;
2596 VecIn1 = VecIn2 = SDOperand(0, 0);
2601 // If everything is good, we can make a shuffle operation.
2603 std::vector<SDOperand> BuildVecIndices;
2604 for (unsigned i = 0; i != NumInScalars; ++i) {
2605 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2606 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2610 SDOperand Extract = N->getOperand(i);
2612 // If extracting from the first vector, just use the index directly.
2613 if (Extract.getOperand(0) == VecIn1) {
2614 BuildVecIndices.push_back(Extract.getOperand(1));
2618 // Otherwise, use InIdx + VecSize
2619 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2620 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2623 // Add count and size info.
2624 BuildVecIndices.push_back(NumElts);
2625 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2627 // Return the new VVECTOR_SHUFFLE node.
2628 std::vector<SDOperand> Ops;
2629 Ops.push_back(VecIn1);
2631 Ops.push_back(VecIn2);
2633 // Use an undef vbuild_vector as input for the second operand.
2634 std::vector<SDOperand> UnOps(NumInScalars,
2635 DAG.getNode(ISD::UNDEF,
2636 cast<VTSDNode>(EltType)->getVT()));
2637 UnOps.push_back(NumElts);
2638 UnOps.push_back(EltType);
2639 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, UnOps));
2640 AddToWorkList(Ops.back().Val);
2642 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR,MVT::Vector, BuildVecIndices));
2643 Ops.push_back(NumElts);
2644 Ops.push_back(EltType);
2645 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2651 SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
2652 SDOperand ShufMask = N->getOperand(2);
2653 unsigned NumElts = ShufMask.getNumOperands();
2655 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2656 bool isIdentity = true;
2657 for (unsigned i = 0; i != NumElts; ++i) {
2658 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2659 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2664 if (isIdentity) return N->getOperand(0);
2666 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2668 for (unsigned i = 0; i != NumElts; ++i) {
2669 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2670 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2675 if (isIdentity) return N->getOperand(1);
2677 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2678 if (N->getOperand(0) == N->getOperand(1)) {
2679 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
2680 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
2681 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2683 std::vector<SDOperand> MappedOps;
2684 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
2685 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2686 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2687 MappedOps.push_back(ShufMask.getOperand(i));
2690 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2691 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2694 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
2696 AddToWorkList(ShufMask.Val);
2697 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
2699 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2706 SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2707 SDOperand ShufMask = N->getOperand(2);
2708 unsigned NumElts = ShufMask.getNumOperands()-2;
2710 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2711 bool isIdentity = true;
2712 for (unsigned i = 0; i != NumElts; ++i) {
2713 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2714 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2719 if (isIdentity) return N->getOperand(0);
2721 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2723 for (unsigned i = 0; i != NumElts; ++i) {
2724 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2725 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2730 if (isIdentity) return N->getOperand(1);
2732 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2733 if (N->getOperand(0) == N->getOperand(1)) {
2734 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2736 std::vector<SDOperand> MappedOps;
2737 for (unsigned i = 0; i != NumElts; ++i) {
2738 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2739 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2740 MappedOps.push_back(ShufMask.getOperand(i));
2743 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2744 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2747 // Add the type/#elts values.
2748 MappedOps.push_back(ShufMask.getOperand(NumElts));
2749 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
2751 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
2753 AddToWorkList(ShufMask.Val);
2755 // Build the undef vector.
2756 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
2757 for (unsigned i = 0; i != NumElts; ++i)
2758 MappedOps[i] = UDVal;
2759 MappedOps[NumElts ] = *(N->getOperand(0).Val->op_end()-2);
2760 MappedOps[NumElts+1] = *(N->getOperand(0).Val->op_end()-1);
2761 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, MappedOps);
2763 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
2764 N->getOperand(0), UDVal, ShufMask,
2765 MappedOps[NumElts], MappedOps[NumElts+1]);
2771 /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
2772 /// a VAND to a vector_shuffle with the destination vector and a zero vector.
2773 /// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
2774 /// vector_shuffle V, Zero, <0, 4, 2, 4>
2775 SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
2776 SDOperand LHS = N->getOperand(0);
2777 SDOperand RHS = N->getOperand(1);
2778 if (N->getOpcode() == ISD::VAND) {
2779 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
2780 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
2781 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
2782 RHS = RHS.getOperand(0);
2783 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2784 std::vector<SDOperand> IdxOps;
2785 unsigned NumOps = RHS.getNumOperands();
2786 unsigned NumElts = NumOps-2;
2787 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
2788 for (unsigned i = 0; i != NumElts; ++i) {
2789 SDOperand Elt = RHS.getOperand(i);
2790 if (!isa<ConstantSDNode>(Elt))
2792 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
2793 IdxOps.push_back(DAG.getConstant(i, EVT));
2794 else if (cast<ConstantSDNode>(Elt)->isNullValue())
2795 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
2800 // Let's see if the target supports this vector_shuffle.
2801 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
2804 // Return the new VVECTOR_SHUFFLE node.
2805 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
2806 SDOperand EVTNode = DAG.getValueType(EVT);
2807 std::vector<SDOperand> Ops;
2808 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode, EVTNode);
2810 AddToWorkList(LHS.Val);
2811 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
2812 ZeroOps.push_back(NumEltsNode);
2813 ZeroOps.push_back(EVTNode);
2814 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, ZeroOps));
2815 IdxOps.push_back(NumEltsNode);
2816 IdxOps.push_back(EVTNode);
2817 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, IdxOps));
2818 Ops.push_back(NumEltsNode);
2819 Ops.push_back(EVTNode);
2820 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2821 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
2822 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
2823 DstVecSize, DstVecEVT);
2831 /// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
2832 /// the scalar operation of the vop if it is operating on an integer vector
2833 /// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
2834 SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
2835 ISD::NodeType FPOp) {
2836 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
2837 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
2838 SDOperand LHS = N->getOperand(0);
2839 SDOperand RHS = N->getOperand(1);
2840 SDOperand Shuffle = XformToShuffleWithZero(N);
2841 if (Shuffle.Val) return Shuffle;
2843 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
2845 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
2846 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
2847 std::vector<SDOperand> Ops;
2848 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
2849 SDOperand LHSOp = LHS.getOperand(i);
2850 SDOperand RHSOp = RHS.getOperand(i);
2851 // If these two elements can't be folded, bail out.
2852 if ((LHSOp.getOpcode() != ISD::UNDEF &&
2853 LHSOp.getOpcode() != ISD::Constant &&
2854 LHSOp.getOpcode() != ISD::ConstantFP) ||
2855 (RHSOp.getOpcode() != ISD::UNDEF &&
2856 RHSOp.getOpcode() != ISD::Constant &&
2857 RHSOp.getOpcode() != ISD::ConstantFP))
2859 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
2860 AddToWorkList(Ops.back().Val);
2861 assert((Ops.back().getOpcode() == ISD::UNDEF ||
2862 Ops.back().getOpcode() == ISD::Constant ||
2863 Ops.back().getOpcode() == ISD::ConstantFP) &&
2864 "Scalar binop didn't fold!");
2867 if (Ops.size() == LHS.getNumOperands()-2) {
2868 Ops.push_back(*(LHS.Val->op_end()-2));
2869 Ops.push_back(*(LHS.Val->op_end()-1));
2870 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
2877 SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
2878 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2880 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2881 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2882 // If we got a simplified select_cc node back from SimplifySelectCC, then
2883 // break it down into a new SETCC node, and a new SELECT node, and then return
2884 // the SELECT node, since we were called with a SELECT node.
2886 // Check to see if we got a select_cc back (to turn into setcc/select).
2887 // Otherwise, just return whatever node we got back, like fabs.
2888 if (SCC.getOpcode() == ISD::SELECT_CC) {
2889 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2890 SCC.getOperand(0), SCC.getOperand(1),
2892 AddToWorkList(SETCC.Val);
2893 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2894 SCC.getOperand(3), SETCC);
2901 /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2902 /// are the two values being selected between, see if we can simplify the
2905 bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2908 // If this is a select from two identical things, try to pull the operation
2909 // through the select.
2910 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2912 std::cerr << "SELECT: ["; LHS.Val->dump();
2913 std::cerr << "] ["; RHS.Val->dump();
2917 // If this is a load and the token chain is identical, replace the select
2918 // of two loads with a load through a select of the address to load from.
2919 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2920 // constants have been dropped into the constant pool.
2921 if ((LHS.getOpcode() == ISD::LOAD ||
2922 LHS.getOpcode() == ISD::EXTLOAD ||
2923 LHS.getOpcode() == ISD::ZEXTLOAD ||
2924 LHS.getOpcode() == ISD::SEXTLOAD) &&
2925 // Token chains must be identical.
2926 LHS.getOperand(0) == RHS.getOperand(0) &&
2927 // If this is an EXTLOAD, the VT's must match.
2928 (LHS.getOpcode() == ISD::LOAD ||
2929 LHS.getOperand(3) == RHS.getOperand(3))) {
2930 // FIXME: this conflates two src values, discarding one. This is not
2931 // the right thing to do, but nothing uses srcvalues now. When they do,
2932 // turn SrcValue into a list of locations.
2934 if (TheSelect->getOpcode() == ISD::SELECT)
2935 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2936 TheSelect->getOperand(0), LHS.getOperand(1),
2939 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2940 TheSelect->getOperand(0),
2941 TheSelect->getOperand(1),
2942 LHS.getOperand(1), RHS.getOperand(1),
2943 TheSelect->getOperand(4));
2946 if (LHS.getOpcode() == ISD::LOAD)
2947 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2948 Addr, LHS.getOperand(2));
2950 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2951 LHS.getOperand(0), Addr, LHS.getOperand(2),
2952 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2953 // Users of the select now use the result of the load.
2954 CombineTo(TheSelect, Load);
2956 // Users of the old loads now use the new load's chain. We know the
2957 // old-load value is dead now.
2958 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2959 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2967 SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2968 SDOperand N2, SDOperand N3,
2971 MVT::ValueType VT = N2.getValueType();
2972 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2973 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2974 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2975 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2977 // Determine if the condition we're dealing with is constant
2978 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2979 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2981 // fold select_cc true, x, y -> x
2982 if (SCCC && SCCC->getValue())
2984 // fold select_cc false, x, y -> y
2985 if (SCCC && SCCC->getValue() == 0)
2988 // Check to see if we can simplify the select into an fabs node
2989 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2990 // Allow either -0.0 or 0.0
2991 if (CFP->getValue() == 0.0) {
2992 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2993 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2994 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2995 N2 == N3.getOperand(0))
2996 return DAG.getNode(ISD::FABS, VT, N0);
2998 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2999 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3000 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3001 N2.getOperand(0) == N3)
3002 return DAG.getNode(ISD::FABS, VT, N3);
3006 // Check to see if we can perform the "gzip trick", transforming
3007 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
3008 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
3009 MVT::isInteger(N0.getValueType()) &&
3010 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
3011 MVT::ValueType XType = N0.getValueType();
3012 MVT::ValueType AType = N2.getValueType();
3013 if (XType >= AType) {
3014 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
3015 // single-bit constant.
3016 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3017 unsigned ShCtV = Log2_64(N2C->getValue());
3018 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3019 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3020 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
3021 AddToWorkList(Shift.Val);
3022 if (XType > AType) {
3023 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
3024 AddToWorkList(Shift.Val);
3026 return DAG.getNode(ISD::AND, AType, Shift, N2);
3028 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3029 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3030 TLI.getShiftAmountTy()));
3031 AddToWorkList(Shift.Val);
3032 if (XType > AType) {
3033 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
3034 AddToWorkList(Shift.Val);
3036 return DAG.getNode(ISD::AND, AType, Shift, N2);
3040 // fold select C, 16, 0 -> shl C, 4
3041 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3042 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3043 // Get a SetCC of the condition
3044 // FIXME: Should probably make sure that setcc is legal if we ever have a
3045 // target where it isn't.
3046 SDOperand Temp, SCC;
3047 // cast from setcc result type to select result type
3048 if (AfterLegalize) {
3049 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3050 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
3052 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
3053 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
3055 AddToWorkList(SCC.Val);
3056 AddToWorkList(Temp.Val);
3057 // shl setcc result by log2 n2c
3058 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3059 DAG.getConstant(Log2_64(N2C->getValue()),
3060 TLI.getShiftAmountTy()));
3063 // Check to see if this is the equivalent of setcc
3064 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3065 // otherwise, go ahead with the folds.
3066 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3067 MVT::ValueType XType = N0.getValueType();
3068 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3069 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3070 if (Res.getValueType() != VT)
3071 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3075 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3076 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3077 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3078 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3079 return DAG.getNode(ISD::SRL, XType, Ctlz,
3080 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3081 TLI.getShiftAmountTy()));
3083 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3084 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3085 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3087 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3088 DAG.getConstant(~0ULL, XType));
3089 return DAG.getNode(ISD::SRL, XType,
3090 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3091 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3092 TLI.getShiftAmountTy()));
3094 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3095 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3096 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3097 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3098 TLI.getShiftAmountTy()));
3099 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3103 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3104 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3105 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3106 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3107 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3108 MVT::ValueType XType = N0.getValueType();
3109 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3110 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3111 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3112 TLI.getShiftAmountTy()));
3113 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
3114 AddToWorkList(Shift.Val);
3115 AddToWorkList(Add.Val);
3116 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3124 SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
3125 SDOperand N1, ISD::CondCode Cond,
3126 bool foldBooleans) {
3127 // These setcc operations always fold.
3131 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3133 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3136 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3137 uint64_t C1 = N1C->getValue();
3138 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3139 uint64_t C0 = N0C->getValue();
3141 // Sign extend the operands if required
3142 if (ISD::isSignedIntSetCC(Cond)) {
3143 C0 = N0C->getSignExtended();
3144 C1 = N1C->getSignExtended();
3148 default: assert(0 && "Unknown integer setcc!");
3149 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3150 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3151 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3152 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3153 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3154 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3155 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3156 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3157 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3158 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3161 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3162 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3163 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3165 // If the comparison constant has bits in the upper part, the
3166 // zero-extended value could never match.
3167 if (C1 & (~0ULL << InSize)) {
3168 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3172 case ISD::SETEQ: return DAG.getConstant(0, VT);
3175 case ISD::SETNE: return DAG.getConstant(1, VT);
3178 // True if the sign bit of C1 is set.
3179 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3182 // True if the sign bit of C1 isn't set.
3183 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3189 // Otherwise, we can perform the comparison with the low bits.
3197 return DAG.getSetCC(VT, N0.getOperand(0),
3198 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3201 break; // todo, be more careful with signed comparisons
3203 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3204 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3205 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3206 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3207 MVT::ValueType ExtDstTy = N0.getValueType();
3208 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3210 // If the extended part has any inconsistent bits, it cannot ever
3211 // compare equal. In other words, they have to be all ones or all
3214 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3215 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3216 return DAG.getConstant(Cond == ISD::SETNE, VT);
3219 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3220 if (Op0Ty == ExtSrcTy) {
3221 ZextOp = N0.getOperand(0);
3223 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3224 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3225 DAG.getConstant(Imm, Op0Ty));
3227 AddToWorkList(ZextOp.Val);
3228 // Otherwise, make this a use of a zext.
3229 return DAG.getSetCC(VT, ZextOp,
3230 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3233 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3234 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3235 (N0.getOpcode() == ISD::XOR ||
3236 (N0.getOpcode() == ISD::AND &&
3237 N0.getOperand(0).getOpcode() == ISD::XOR &&
3238 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3239 isa<ConstantSDNode>(N0.getOperand(1)) &&
3240 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3241 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3242 // only do this if the top bits are known zero.
3243 if (TLI.MaskedValueIsZero(N1,
3244 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3245 // Okay, get the un-inverted input value.
3247 if (N0.getOpcode() == ISD::XOR)
3248 Val = N0.getOperand(0);
3250 assert(N0.getOpcode() == ISD::AND &&
3251 N0.getOperand(0).getOpcode() == ISD::XOR);
3252 // ((X^1)&1)^1 -> X & 1
3253 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3254 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3256 return DAG.getSetCC(VT, Val, N1,
3257 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3261 uint64_t MinVal, MaxVal;
3262 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3263 if (ISD::isSignedIntSetCC(Cond)) {
3264 MinVal = 1ULL << (OperandBitSize-1);
3265 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3266 MaxVal = ~0ULL >> (65-OperandBitSize);
3271 MaxVal = ~0ULL >> (64-OperandBitSize);
3274 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3275 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3276 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3277 --C1; // X >= C0 --> X > (C0-1)
3278 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3279 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3282 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3283 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3284 ++C1; // X <= C0 --> X < (C0+1)
3285 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3286 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3289 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3290 return DAG.getConstant(0, VT); // X < MIN --> false
3292 // Canonicalize setgt X, Min --> setne X, Min
3293 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3294 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
3295 // Canonicalize setlt X, Max --> setne X, Max
3296 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3297 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
3299 // If we have setult X, 1, turn it into seteq X, 0
3300 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3301 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3303 // If we have setugt X, Max-1, turn it into seteq X, Max
3304 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3305 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3308 // If we have "setcc X, C0", check to see if we can shrink the immediate
3311 // SETUGT X, SINTMAX -> SETLT X, 0
3312 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3313 C1 == (~0ULL >> (65-OperandBitSize)))
3314 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3317 // FIXME: Implement the rest of these.
3319 // Fold bit comparisons when we can.
3320 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3321 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3322 if (ConstantSDNode *AndRHS =
3323 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3324 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3325 // Perform the xform if the AND RHS is a single bit.
3326 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3327 return DAG.getNode(ISD::SRL, VT, N0,
3328 DAG.getConstant(Log2_64(AndRHS->getValue()),
3329 TLI.getShiftAmountTy()));
3331 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3332 // (X & 8) == 8 --> (X & 8) >> 3
3333 // Perform the xform if C1 is a single bit.
3334 if ((C1 & (C1-1)) == 0) {
3335 return DAG.getNode(ISD::SRL, VT, N0,
3336 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
3341 } else if (isa<ConstantSDNode>(N0.Val)) {
3342 // Ensure that the constant occurs on the RHS.
3343 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3346 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3347 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3348 double C0 = N0C->getValue(), C1 = N1C->getValue();
3351 default: break; // FIXME: Implement the rest of these!
3352 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3353 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3354 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3355 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3356 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3357 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3360 // Ensure that the constant occurs on the RHS.
3361 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3365 // We can always fold X == Y for integer setcc's.
3366 if (MVT::isInteger(N0.getValueType()))
3367 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3368 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3369 if (UOF == 2) // FP operators that are undefined on NaNs.
3370 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3371 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3372 return DAG.getConstant(UOF, VT);
3373 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3374 // if it is not already.
3375 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
3376 if (NewCond != Cond)
3377 return DAG.getSetCC(VT, N0, N1, NewCond);
3380 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3381 MVT::isInteger(N0.getValueType())) {
3382 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3383 N0.getOpcode() == ISD::XOR) {
3384 // Simplify (X+Y) == (X+Z) --> Y == Z
3385 if (N0.getOpcode() == N1.getOpcode()) {
3386 if (N0.getOperand(0) == N1.getOperand(0))
3387 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3388 if (N0.getOperand(1) == N1.getOperand(1))
3389 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3390 if (isCommutativeBinOp(N0.getOpcode())) {
3391 // If X op Y == Y op X, try other combinations.
3392 if (N0.getOperand(0) == N1.getOperand(1))
3393 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3394 if (N0.getOperand(1) == N1.getOperand(0))
3395 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
3399 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3400 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3401 // Turn (X+C1) == C2 --> X == C2-C1
3402 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3403 return DAG.getSetCC(VT, N0.getOperand(0),
3404 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3405 N0.getValueType()), Cond);
3408 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3409 if (N0.getOpcode() == ISD::XOR)
3410 // If we know that all of the inverted bits are zero, don't bother
3411 // performing the inversion.
3412 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
3413 return DAG.getSetCC(VT, N0.getOperand(0),
3414 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
3415 N0.getValueType()), Cond);
3418 // Turn (C1-X) == C2 --> X == C1-C2
3419 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3420 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3421 return DAG.getSetCC(VT, N0.getOperand(1),
3422 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3423 N0.getValueType()), Cond);
3428 // Simplify (X+Z) == X --> Z == 0
3429 if (N0.getOperand(0) == N1)
3430 return DAG.getSetCC(VT, N0.getOperand(1),
3431 DAG.getConstant(0, N0.getValueType()), Cond);
3432 if (N0.getOperand(1) == N1) {
3433 if (isCommutativeBinOp(N0.getOpcode()))
3434 return DAG.getSetCC(VT, N0.getOperand(0),
3435 DAG.getConstant(0, N0.getValueType()), Cond);
3437 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3438 // (Z-X) == X --> Z == X<<1
3439 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3441 DAG.getConstant(1,TLI.getShiftAmountTy()));
3442 AddToWorkList(SH.Val);
3443 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3448 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3449 N1.getOpcode() == ISD::XOR) {
3450 // Simplify X == (X+Z) --> Z == 0
3451 if (N1.getOperand(0) == N0) {
3452 return DAG.getSetCC(VT, N1.getOperand(1),
3453 DAG.getConstant(0, N1.getValueType()), Cond);
3454 } else if (N1.getOperand(1) == N0) {
3455 if (isCommutativeBinOp(N1.getOpcode())) {
3456 return DAG.getSetCC(VT, N1.getOperand(0),
3457 DAG.getConstant(0, N1.getValueType()), Cond);
3459 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3460 // X == (Z-X) --> X<<1 == Z
3461 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3462 DAG.getConstant(1,TLI.getShiftAmountTy()));
3463 AddToWorkList(SH.Val);
3464 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3470 // Fold away ALL boolean setcc's.
3472 if (N0.getValueType() == MVT::i1 && foldBooleans) {
3474 default: assert(0 && "Unknown integer setcc!");
3475 case ISD::SETEQ: // X == Y -> (X^Y)^1
3476 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3477 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
3478 AddToWorkList(Temp.Val);
3480 case ISD::SETNE: // X != Y --> (X^Y)
3481 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3483 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3484 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3485 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3486 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
3487 AddToWorkList(Temp.Val);
3489 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3490 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3491 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3492 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
3493 AddToWorkList(Temp.Val);
3495 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3496 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3497 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3498 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
3499 AddToWorkList(Temp.Val);
3501 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3502 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3503 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3504 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3507 if (VT != MVT::i1) {
3508 AddToWorkList(N0.Val);
3509 // FIXME: If running after legalize, we probably can't do this.
3510 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3515 // Could not fold it.
3519 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3520 /// return a DAG expression to select that will generate the same value by
3521 /// multiplying by a magic number. See:
3522 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3523 SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
3524 MVT::ValueType VT = N->getValueType(0);
3526 // Check to see if we can do this.
3527 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3528 return SDOperand(); // BuildSDIV only operates on i32 or i64
3529 if (!TLI.isOperationLegal(ISD::MULHS, VT))
3530 return SDOperand(); // Make sure the target supports MULHS.
3532 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
3533 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
3535 // Multiply the numerator (operand 0) by the magic value
3536 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
3537 DAG.getConstant(magics.m, VT));
3538 // If d > 0 and m < 0, add the numerator
3539 if (d > 0 && magics.m < 0) {
3540 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
3541 AddToWorkList(Q.Val);
3543 // If d < 0 and m > 0, subtract the numerator.
3544 if (d < 0 && magics.m > 0) {
3545 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
3546 AddToWorkList(Q.Val);
3548 // Shift right algebraic if shift value is nonzero
3550 Q = DAG.getNode(ISD::SRA, VT, Q,
3551 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3552 AddToWorkList(Q.Val);
3554 // Extract the sign bit and add it to the quotient
3556 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
3557 TLI.getShiftAmountTy()));
3558 AddToWorkList(T.Val);
3559 return DAG.getNode(ISD::ADD, VT, Q, T);
3562 /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3563 /// return a DAG expression to select that will generate the same value by
3564 /// multiplying by a magic number. See:
3565 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3566 SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3567 MVT::ValueType VT = N->getValueType(0);
3569 // Check to see if we can do this.
3570 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3571 return SDOperand(); // BuildUDIV only operates on i32 or i64
3572 if (!TLI.isOperationLegal(ISD::MULHU, VT))
3573 return SDOperand(); // Make sure the target supports MULHU.
3575 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
3576 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
3578 // Multiply the numerator (operand 0) by the magic value
3579 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
3580 DAG.getConstant(magics.m, VT));
3581 AddToWorkList(Q.Val);
3583 if (magics.a == 0) {
3584 return DAG.getNode(ISD::SRL, VT, Q,
3585 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3587 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
3588 AddToWorkList(NPQ.Val);
3589 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
3590 DAG.getConstant(1, TLI.getShiftAmountTy()));
3591 AddToWorkList(NPQ.Val);
3592 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
3593 AddToWorkList(NPQ.Val);
3594 return DAG.getNode(ISD::SRL, VT, NPQ,
3595 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
3599 // SelectionDAG::Combine - This is the entry point for the file.
3601 void SelectionDAG::Combine(bool RunningAfterLegalize) {
3602 /// run - This is the main entry point to this class.
3604 DAGCombiner(*this).Run(RunningAfterLegalize);