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: divide by zero is currently left unfolded. do we want to turn this
27 // FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
29 //===----------------------------------------------------------------------===//
31 #define DEBUG_TYPE "dagcombine"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/CodeGen/SelectionDAG.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Target/TargetLowering.h"
37 #include "llvm/Support/Compiler.h"
38 #include "llvm/Support/CommandLine.h"
45 static Statistic<> NodesCombined ("dagcombiner",
46 "Number of dag nodes combined");
50 CombinerAA("combiner-alias-analysis", cl::Hidden,
51 cl::desc("Turn on alias analysis turning testing"));
53 class VISIBILITY_HIDDEN DAGCombiner {
58 // Worklist of all of the nodes that need to be simplified.
59 std::vector<SDNode*> WorkList;
61 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
68 WorkList.push_back(*UI);
71 /// removeFromWorkList - remove all instances of N from the worklist.
73 void removeFromWorkList(SDNode *N) {
74 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
79 void AddToWorkList(SDNode *N) {
80 WorkList.push_back(N);
83 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
84 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
86 DEBUG(std::cerr << "\nReplacing "; N->dump();
87 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
88 std::cerr << " and " << NumTo-1 << " other values\n");
89 std::vector<SDNode*> NowDead;
90 DAG.ReplaceAllUsesWith(N, To, &NowDead);
92 // Push the new nodes and any users onto the worklist
93 for (unsigned i = 0, e = NumTo; i != e; ++i) {
94 WorkList.push_back(To[i].Val);
95 AddUsersToWorkList(To[i].Val);
98 // Nodes can end up on the worklist more than once. Make sure we do
99 // not process a node that has been replaced.
100 removeFromWorkList(N);
101 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
102 removeFromWorkList(NowDead[i]);
104 // Finally, since the node is now dead, remove it from the graph.
106 return SDOperand(N, 0);
109 SDOperand CombineTo(SDNode *N, SDOperand Res) {
110 return CombineTo(N, &Res, 1);
113 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
114 SDOperand To[] = { Res0, Res1 };
115 return CombineTo(N, To, 2);
119 /// SimplifyDemandedBits - Check the specified integer node value to see if
120 /// it can be simplified or if things it uses can be simplified by bit
121 /// propagation. If so, return true.
122 bool SimplifyDemandedBits(SDOperand Op) {
123 TargetLowering::TargetLoweringOpt TLO(DAG);
124 uint64_t KnownZero, KnownOne;
125 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
126 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
130 WorkList.push_back(Op.Val);
132 // Replace the old value with the new one.
134 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
135 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG));
137 std::vector<SDNode*> NowDead;
138 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
140 // Push the new node and any (possibly new) users onto the worklist.
141 WorkList.push_back(TLO.New.Val);
142 AddUsersToWorkList(TLO.New.Val);
144 // Nodes can end up on the worklist more than once. Make sure we do
145 // not process a node that has been replaced.
146 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
147 removeFromWorkList(NowDead[i]);
149 // Finally, if the node is now dead, remove it from the graph. The node
150 // may not be dead if the replacement process recursively simplified to
151 // something else needing this node.
152 if (TLO.Old.Val->use_empty()) {
153 removeFromWorkList(TLO.Old.Val);
154 DAG.DeleteNode(TLO.Old.Val);
159 /// visit - call the node-specific routine that knows how to fold each
160 /// particular type of node.
161 SDOperand visit(SDNode *N);
163 // Visitation implementation - Implement dag node combining for different
164 // node types. The semantics are as follows:
166 // SDOperand.Val == 0 - No change was made
167 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
168 // otherwise - N should be replaced by the returned Operand.
170 SDOperand visitTokenFactor(SDNode *N);
171 SDOperand visitADD(SDNode *N);
172 SDOperand visitSUB(SDNode *N);
173 SDOperand visitMUL(SDNode *N);
174 SDOperand visitSDIV(SDNode *N);
175 SDOperand visitUDIV(SDNode *N);
176 SDOperand visitSREM(SDNode *N);
177 SDOperand visitUREM(SDNode *N);
178 SDOperand visitMULHU(SDNode *N);
179 SDOperand visitMULHS(SDNode *N);
180 SDOperand visitAND(SDNode *N);
181 SDOperand visitOR(SDNode *N);
182 SDOperand visitXOR(SDNode *N);
183 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
184 SDOperand visitSHL(SDNode *N);
185 SDOperand visitSRA(SDNode *N);
186 SDOperand visitSRL(SDNode *N);
187 SDOperand visitCTLZ(SDNode *N);
188 SDOperand visitCTTZ(SDNode *N);
189 SDOperand visitCTPOP(SDNode *N);
190 SDOperand visitSELECT(SDNode *N);
191 SDOperand visitSELECT_CC(SDNode *N);
192 SDOperand visitSETCC(SDNode *N);
193 SDOperand visitSIGN_EXTEND(SDNode *N);
194 SDOperand visitZERO_EXTEND(SDNode *N);
195 SDOperand visitANY_EXTEND(SDNode *N);
196 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
197 SDOperand visitTRUNCATE(SDNode *N);
198 SDOperand visitBIT_CONVERT(SDNode *N);
199 SDOperand visitVBIT_CONVERT(SDNode *N);
200 SDOperand visitFADD(SDNode *N);
201 SDOperand visitFSUB(SDNode *N);
202 SDOperand visitFMUL(SDNode *N);
203 SDOperand visitFDIV(SDNode *N);
204 SDOperand visitFREM(SDNode *N);
205 SDOperand visitFCOPYSIGN(SDNode *N);
206 SDOperand visitSINT_TO_FP(SDNode *N);
207 SDOperand visitUINT_TO_FP(SDNode *N);
208 SDOperand visitFP_TO_SINT(SDNode *N);
209 SDOperand visitFP_TO_UINT(SDNode *N);
210 SDOperand visitFP_ROUND(SDNode *N);
211 SDOperand visitFP_ROUND_INREG(SDNode *N);
212 SDOperand visitFP_EXTEND(SDNode *N);
213 SDOperand visitFNEG(SDNode *N);
214 SDOperand visitFABS(SDNode *N);
215 SDOperand visitBRCOND(SDNode *N);
216 SDOperand visitBR_CC(SDNode *N);
217 SDOperand visitLOAD(SDNode *N);
218 SDOperand visitXEXTLOAD(SDNode *N);
219 SDOperand visitSTORE(SDNode *N);
220 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
221 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
222 SDOperand visitVBUILD_VECTOR(SDNode *N);
223 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
224 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
226 SDOperand XformToShuffleWithZero(SDNode *N);
227 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
229 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
230 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
231 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
232 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
233 SDOperand N3, ISD::CondCode CC);
234 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
235 ISD::CondCode Cond, bool foldBooleans = true);
236 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
237 SDOperand BuildSDIV(SDNode *N);
238 SDOperand BuildUDIV(SDNode *N);
239 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
240 bool isNotAlias(SDOperand Ptr1, SDOperand Ptr2);
242 DAGCombiner(SelectionDAG &D)
243 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
245 /// Run - runs the dag combiner on all nodes in the work list
246 void Run(bool RunningAfterLegalize);
250 //===----------------------------------------------------------------------===//
251 // TargetLowering::DAGCombinerInfo implementation
252 //===----------------------------------------------------------------------===//
254 void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
255 ((DAGCombiner*)DC)->AddToWorkList(N);
258 SDOperand TargetLowering::DAGCombinerInfo::
259 CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
260 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
263 SDOperand TargetLowering::DAGCombinerInfo::
264 CombineTo(SDNode *N, SDOperand Res) {
265 return ((DAGCombiner*)DC)->CombineTo(N, Res);
269 SDOperand TargetLowering::DAGCombinerInfo::
270 CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
271 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
277 //===----------------------------------------------------------------------===//
280 // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
281 // that selects between the values 1 and 0, making it equivalent to a setcc.
282 // Also, set the incoming LHS, RHS, and CC references to the appropriate
283 // nodes based on the type of node we are checking. This simplifies life a
284 // bit for the callers.
285 static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
287 if (N.getOpcode() == ISD::SETCC) {
288 LHS = N.getOperand(0);
289 RHS = N.getOperand(1);
290 CC = N.getOperand(2);
293 if (N.getOpcode() == ISD::SELECT_CC &&
294 N.getOperand(2).getOpcode() == ISD::Constant &&
295 N.getOperand(3).getOpcode() == ISD::Constant &&
296 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
297 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
298 LHS = N.getOperand(0);
299 RHS = N.getOperand(1);
300 CC = N.getOperand(4);
306 // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
307 // one use. If this is true, it allows the users to invert the operation for
308 // free when it is profitable to do so.
309 static bool isOneUseSetCC(SDOperand N) {
310 SDOperand N0, N1, N2;
311 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
316 SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
317 MVT::ValueType VT = N0.getValueType();
318 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
319 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
320 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
321 if (isa<ConstantSDNode>(N1)) {
322 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
323 AddToWorkList(OpNode.Val);
324 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
325 } else if (N0.hasOneUse()) {
326 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
327 AddToWorkList(OpNode.Val);
328 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
331 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
332 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
333 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
334 if (isa<ConstantSDNode>(N0)) {
335 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
336 AddToWorkList(OpNode.Val);
337 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
338 } else if (N1.hasOneUse()) {
339 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
340 AddToWorkList(OpNode.Val);
341 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
347 void DAGCombiner::Run(bool RunningAfterLegalize) {
348 // set the instance variable, so that the various visit routines may use it.
349 AfterLegalize = RunningAfterLegalize;
351 // Add all the dag nodes to the worklist.
352 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
353 E = DAG.allnodes_end(); I != E; ++I)
354 WorkList.push_back(I);
356 // Create a dummy node (which is not added to allnodes), that adds a reference
357 // to the root node, preventing it from being deleted, and tracking any
358 // changes of the root.
359 HandleSDNode Dummy(DAG.getRoot());
362 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
363 TargetLowering::DAGCombinerInfo
364 DagCombineInfo(DAG, !RunningAfterLegalize, this);
366 // while the worklist isn't empty, inspect the node on the end of it and
367 // try and combine it.
368 while (!WorkList.empty()) {
369 SDNode *N = WorkList.back();
372 // If N has no uses, it is dead. Make sure to revisit all N's operands once
373 // N is deleted from the DAG, since they too may now be dead or may have a
374 // reduced number of uses, allowing other xforms.
375 if (N->use_empty() && N != &Dummy) {
376 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
377 WorkList.push_back(N->getOperand(i).Val);
379 removeFromWorkList(N);
384 SDOperand RV = visit(N);
386 // If nothing happened, try a target-specific DAG combine.
388 assert(N->getOpcode() != ISD::DELETED_NODE &&
389 "Node was deleted but visit returned NULL!");
390 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
391 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
392 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
397 // If we get back the same node we passed in, rather than a new node or
398 // zero, we know that the node must have defined multiple values and
399 // CombineTo was used. Since CombineTo takes care of the worklist
400 // mechanics for us, we have no work to do in this case.
402 assert(N->getOpcode() != ISD::DELETED_NODE &&
403 RV.Val->getOpcode() != ISD::DELETED_NODE &&
404 "Node was deleted but visit returned new node!");
406 DEBUG(std::cerr << "\nReplacing "; N->dump();
407 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
409 std::vector<SDNode*> NowDead;
410 if (N->getNumValues() == RV.Val->getNumValues())
411 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
413 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
415 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
418 // Push the new node and any users onto the worklist
419 WorkList.push_back(RV.Val);
420 AddUsersToWorkList(RV.Val);
422 // Nodes can end up on the worklist more than once. Make sure we do
423 // not process a node that has been replaced.
424 removeFromWorkList(N);
425 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
426 removeFromWorkList(NowDead[i]);
428 // Finally, since the node is now dead, remove it from the graph.
434 // If the root changed (e.g. it was a dead load, update the root).
435 DAG.setRoot(Dummy.getValue());
438 SDOperand DAGCombiner::visit(SDNode *N) {
439 switch(N->getOpcode()) {
441 case ISD::TokenFactor: return visitTokenFactor(N);
442 case ISD::ADD: return visitADD(N);
443 case ISD::SUB: return visitSUB(N);
444 case ISD::MUL: return visitMUL(N);
445 case ISD::SDIV: return visitSDIV(N);
446 case ISD::UDIV: return visitUDIV(N);
447 case ISD::SREM: return visitSREM(N);
448 case ISD::UREM: return visitUREM(N);
449 case ISD::MULHU: return visitMULHU(N);
450 case ISD::MULHS: return visitMULHS(N);
451 case ISD::AND: return visitAND(N);
452 case ISD::OR: return visitOR(N);
453 case ISD::XOR: return visitXOR(N);
454 case ISD::SHL: return visitSHL(N);
455 case ISD::SRA: return visitSRA(N);
456 case ISD::SRL: return visitSRL(N);
457 case ISD::CTLZ: return visitCTLZ(N);
458 case ISD::CTTZ: return visitCTTZ(N);
459 case ISD::CTPOP: return visitCTPOP(N);
460 case ISD::SELECT: return visitSELECT(N);
461 case ISD::SELECT_CC: return visitSELECT_CC(N);
462 case ISD::SETCC: return visitSETCC(N);
463 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
464 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
465 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
466 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
467 case ISD::TRUNCATE: return visitTRUNCATE(N);
468 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
469 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
470 case ISD::FADD: return visitFADD(N);
471 case ISD::FSUB: return visitFSUB(N);
472 case ISD::FMUL: return visitFMUL(N);
473 case ISD::FDIV: return visitFDIV(N);
474 case ISD::FREM: return visitFREM(N);
475 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
476 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
477 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
478 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
479 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
480 case ISD::FP_ROUND: return visitFP_ROUND(N);
481 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
482 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
483 case ISD::FNEG: return visitFNEG(N);
484 case ISD::FABS: return visitFABS(N);
485 case ISD::BRCOND: return visitBRCOND(N);
486 case ISD::BR_CC: return visitBR_CC(N);
487 case ISD::LOAD: return visitLOAD(N);
490 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
491 case ISD::STORE: return visitSTORE(N);
492 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
493 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
494 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
495 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
496 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
497 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
498 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
499 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
500 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
501 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
502 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
503 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
504 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
509 SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
510 SmallVector<SDOperand, 8> Ops;
511 bool Changed = false;
513 // If the token factor has two operands and one is the entry token, replace
514 // the token factor with the other operand.
515 if (N->getNumOperands() == 2) {
516 if (N->getOperand(0).getOpcode() == ISD::EntryToken ||
517 N->getOperand(0) == N->getOperand(1))
518 return N->getOperand(1);
519 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
520 return N->getOperand(0);
523 // fold (tokenfactor (tokenfactor)) -> tokenfactor
524 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
525 SDOperand Op = N->getOperand(i);
526 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
527 AddToWorkList(Op.Val); // Remove dead node.
529 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
530 Ops.push_back(Op.getOperand(j));
531 } else if (i == 0 || N->getOperand(i) != N->getOperand(i-1)) {
534 // Deleted an operand that was the same as the last one.
539 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
543 SDOperand DAGCombiner::visitADD(SDNode *N) {
544 SDOperand N0 = N->getOperand(0);
545 SDOperand N1 = N->getOperand(1);
546 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
547 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
548 MVT::ValueType VT = N0.getValueType();
550 // fold (add c1, c2) -> c1+c2
552 return DAG.getNode(ISD::ADD, VT, N0, N1);
553 // canonicalize constant to RHS
555 return DAG.getNode(ISD::ADD, VT, N1, N0);
556 // fold (add x, 0) -> x
557 if (N1C && N1C->isNullValue())
559 // fold ((c1-A)+c2) -> (c1+c2)-A
560 if (N1C && N0.getOpcode() == ISD::SUB)
561 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
562 return DAG.getNode(ISD::SUB, VT,
563 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
566 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
569 // fold ((0-A) + B) -> B-A
570 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
571 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
572 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
573 // fold (A + (0-B)) -> A-B
574 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
575 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
576 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
577 // fold (A+(B-A)) -> B
578 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
579 return N1.getOperand(0);
581 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
582 return SDOperand(N, 0);
584 // fold (a+b) -> (a|b) iff a and b share no bits.
585 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
586 uint64_t LHSZero, LHSOne;
587 uint64_t RHSZero, RHSOne;
588 uint64_t Mask = MVT::getIntVTBitMask(VT);
589 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
591 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
593 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
594 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
595 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
596 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
597 return DAG.getNode(ISD::OR, VT, N0, N1);
604 SDOperand DAGCombiner::visitSUB(SDNode *N) {
605 SDOperand N0 = N->getOperand(0);
606 SDOperand N1 = N->getOperand(1);
607 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
608 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
609 MVT::ValueType VT = N0.getValueType();
611 // fold (sub x, x) -> 0
613 return DAG.getConstant(0, N->getValueType(0));
614 // fold (sub c1, c2) -> c1-c2
616 return DAG.getNode(ISD::SUB, VT, N0, N1);
617 // fold (sub x, c) -> (add x, -c)
619 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
621 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
622 return N0.getOperand(1);
624 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
625 return N0.getOperand(0);
629 SDOperand DAGCombiner::visitMUL(SDNode *N) {
630 SDOperand N0 = N->getOperand(0);
631 SDOperand N1 = N->getOperand(1);
632 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
633 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
634 MVT::ValueType VT = N0.getValueType();
636 // fold (mul c1, c2) -> c1*c2
638 return DAG.getNode(ISD::MUL, VT, N0, N1);
639 // canonicalize constant to RHS
641 return DAG.getNode(ISD::MUL, VT, N1, N0);
642 // fold (mul x, 0) -> 0
643 if (N1C && N1C->isNullValue())
645 // fold (mul x, -1) -> 0-x
646 if (N1C && N1C->isAllOnesValue())
647 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
648 // fold (mul x, (1 << c)) -> x << c
649 if (N1C && isPowerOf2_64(N1C->getValue()))
650 return DAG.getNode(ISD::SHL, VT, N0,
651 DAG.getConstant(Log2_64(N1C->getValue()),
652 TLI.getShiftAmountTy()));
653 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
654 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
655 // FIXME: If the input is something that is easily negated (e.g. a
656 // single-use add), we should put the negate there.
657 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
658 DAG.getNode(ISD::SHL, VT, N0,
659 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
660 TLI.getShiftAmountTy())));
663 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
664 if (N1C && N0.getOpcode() == ISD::SHL &&
665 isa<ConstantSDNode>(N0.getOperand(1))) {
666 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
667 AddToWorkList(C3.Val);
668 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
671 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
674 SDOperand Sh(0,0), Y(0,0);
675 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
676 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
677 N0.Val->hasOneUse()) {
679 } else if (N1.getOpcode() == ISD::SHL &&
680 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
684 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
685 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
688 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
689 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
690 isa<ConstantSDNode>(N0.getOperand(1))) {
691 return DAG.getNode(ISD::ADD, VT,
692 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
693 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
697 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
703 SDOperand DAGCombiner::visitSDIV(SDNode *N) {
704 SDOperand N0 = N->getOperand(0);
705 SDOperand N1 = N->getOperand(1);
706 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
707 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
708 MVT::ValueType VT = N->getValueType(0);
710 // fold (sdiv c1, c2) -> c1/c2
711 if (N0C && N1C && !N1C->isNullValue())
712 return DAG.getNode(ISD::SDIV, VT, N0, N1);
713 // fold (sdiv X, 1) -> X
714 if (N1C && N1C->getSignExtended() == 1LL)
716 // fold (sdiv X, -1) -> 0-X
717 if (N1C && N1C->isAllOnesValue())
718 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
719 // If we know the sign bits of both operands are zero, strength reduce to a
720 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
721 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
722 if (TLI.MaskedValueIsZero(N1, SignBit) &&
723 TLI.MaskedValueIsZero(N0, SignBit))
724 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
725 // fold (sdiv X, pow2) -> simple ops after legalize
726 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
727 (isPowerOf2_64(N1C->getSignExtended()) ||
728 isPowerOf2_64(-N1C->getSignExtended()))) {
729 // If dividing by powers of two is cheap, then don't perform the following
731 if (TLI.isPow2DivCheap())
733 int64_t pow2 = N1C->getSignExtended();
734 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
735 unsigned lg2 = Log2_64(abs2);
736 // Splat the sign bit into the register
737 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
738 DAG.getConstant(MVT::getSizeInBits(VT)-1,
739 TLI.getShiftAmountTy()));
740 AddToWorkList(SGN.Val);
741 // Add (N0 < 0) ? abs2 - 1 : 0;
742 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
743 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
744 TLI.getShiftAmountTy()));
745 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
746 AddToWorkList(SRL.Val);
747 AddToWorkList(ADD.Val); // Divide by pow2
748 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
749 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
750 // If we're dividing by a positive value, we're done. Otherwise, we must
751 // negate the result.
754 AddToWorkList(SRA.Val);
755 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
757 // if integer divide is expensive and we satisfy the requirements, emit an
758 // alternate sequence.
759 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
760 !TLI.isIntDivCheap()) {
761 SDOperand Op = BuildSDIV(N);
762 if (Op.Val) return Op;
767 SDOperand DAGCombiner::visitUDIV(SDNode *N) {
768 SDOperand N0 = N->getOperand(0);
769 SDOperand N1 = N->getOperand(1);
770 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
771 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
772 MVT::ValueType VT = N->getValueType(0);
774 // fold (udiv c1, c2) -> c1/c2
775 if (N0C && N1C && !N1C->isNullValue())
776 return DAG.getNode(ISD::UDIV, VT, N0, N1);
777 // fold (udiv x, (1 << c)) -> x >>u c
778 if (N1C && isPowerOf2_64(N1C->getValue()))
779 return DAG.getNode(ISD::SRL, VT, N0,
780 DAG.getConstant(Log2_64(N1C->getValue()),
781 TLI.getShiftAmountTy()));
782 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
783 if (N1.getOpcode() == ISD::SHL) {
784 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
785 if (isPowerOf2_64(SHC->getValue())) {
786 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
787 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
788 DAG.getConstant(Log2_64(SHC->getValue()),
790 AddToWorkList(Add.Val);
791 return DAG.getNode(ISD::SRL, VT, N0, Add);
795 // fold (udiv x, c) -> alternate
796 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
797 SDOperand Op = BuildUDIV(N);
798 if (Op.Val) return Op;
803 SDOperand DAGCombiner::visitSREM(SDNode *N) {
804 SDOperand N0 = N->getOperand(0);
805 SDOperand N1 = N->getOperand(1);
806 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
807 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
808 MVT::ValueType VT = N->getValueType(0);
810 // fold (srem c1, c2) -> c1%c2
811 if (N0C && N1C && !N1C->isNullValue())
812 return DAG.getNode(ISD::SREM, VT, N0, N1);
813 // If we know the sign bits of both operands are zero, strength reduce to a
814 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
815 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
816 if (TLI.MaskedValueIsZero(N1, SignBit) &&
817 TLI.MaskedValueIsZero(N0, SignBit))
818 return DAG.getNode(ISD::UREM, VT, N0, N1);
822 SDOperand DAGCombiner::visitUREM(SDNode *N) {
823 SDOperand N0 = N->getOperand(0);
824 SDOperand N1 = N->getOperand(1);
825 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
826 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
827 MVT::ValueType VT = N->getValueType(0);
829 // fold (urem c1, c2) -> c1%c2
830 if (N0C && N1C && !N1C->isNullValue())
831 return DAG.getNode(ISD::UREM, VT, N0, N1);
832 // fold (urem x, pow2) -> (and x, pow2-1)
833 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
834 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
835 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
836 if (N1.getOpcode() == ISD::SHL) {
837 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
838 if (isPowerOf2_64(SHC->getValue())) {
839 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
840 AddToWorkList(Add.Val);
841 return DAG.getNode(ISD::AND, VT, N0, Add);
848 SDOperand DAGCombiner::visitMULHS(SDNode *N) {
849 SDOperand N0 = N->getOperand(0);
850 SDOperand N1 = N->getOperand(1);
851 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
853 // fold (mulhs x, 0) -> 0
854 if (N1C && N1C->isNullValue())
856 // fold (mulhs x, 1) -> (sra x, size(x)-1)
857 if (N1C && N1C->getValue() == 1)
858 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
859 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
860 TLI.getShiftAmountTy()));
864 SDOperand DAGCombiner::visitMULHU(SDNode *N) {
865 SDOperand N0 = N->getOperand(0);
866 SDOperand N1 = N->getOperand(1);
867 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
869 // fold (mulhu x, 0) -> 0
870 if (N1C && N1C->isNullValue())
872 // fold (mulhu x, 1) -> 0
873 if (N1C && N1C->getValue() == 1)
874 return DAG.getConstant(0, N0.getValueType());
878 /// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
879 /// two operands of the same opcode, try to simplify it.
880 SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
881 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
882 MVT::ValueType VT = N0.getValueType();
883 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
885 // For each of OP in AND/OR/XOR:
886 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
887 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
888 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
889 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
890 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
891 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
892 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
893 SDOperand ORNode = DAG.getNode(N->getOpcode(),
894 N0.getOperand(0).getValueType(),
895 N0.getOperand(0), N1.getOperand(0));
896 AddToWorkList(ORNode.Val);
897 return DAG.getNode(N0.getOpcode(), VT, ORNode);
900 // For each of OP in SHL/SRL/SRA/AND...
901 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
902 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
903 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
904 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
905 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
906 N0.getOperand(1) == N1.getOperand(1)) {
907 SDOperand ORNode = DAG.getNode(N->getOpcode(),
908 N0.getOperand(0).getValueType(),
909 N0.getOperand(0), N1.getOperand(0));
910 AddToWorkList(ORNode.Val);
911 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
917 SDOperand DAGCombiner::visitAND(SDNode *N) {
918 SDOperand N0 = N->getOperand(0);
919 SDOperand N1 = N->getOperand(1);
920 SDOperand LL, LR, RL, RR, CC0, CC1;
921 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
922 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
923 MVT::ValueType VT = N1.getValueType();
924 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
926 // fold (and c1, c2) -> c1&c2
928 return DAG.getNode(ISD::AND, VT, N0, N1);
929 // canonicalize constant to RHS
931 return DAG.getNode(ISD::AND, VT, N1, N0);
932 // fold (and x, -1) -> x
933 if (N1C && N1C->isAllOnesValue())
935 // if (and x, c) is known to be zero, return 0
936 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
937 return DAG.getConstant(0, VT);
939 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
942 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
943 if (N1C && N0.getOpcode() == ISD::OR)
944 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
945 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
947 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
948 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
949 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
950 if (TLI.MaskedValueIsZero(N0.getOperand(0),
951 ~N1C->getValue() & InMask)) {
952 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
955 // Replace uses of the AND with uses of the Zero extend node.
958 // We actually want to replace all uses of the any_extend with the
959 // zero_extend, to avoid duplicating things. This will later cause this
961 CombineTo(N0.Val, Zext);
962 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
965 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
966 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
967 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
968 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
970 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
971 MVT::isInteger(LL.getValueType())) {
972 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
973 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
974 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
975 AddToWorkList(ORNode.Val);
976 return DAG.getSetCC(VT, ORNode, LR, Op1);
978 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
979 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
980 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
981 AddToWorkList(ANDNode.Val);
982 return DAG.getSetCC(VT, ANDNode, LR, Op1);
984 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
985 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
986 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
987 AddToWorkList(ORNode.Val);
988 return DAG.getSetCC(VT, ORNode, LR, Op1);
991 // canonicalize equivalent to ll == rl
992 if (LL == RR && LR == RL) {
993 Op1 = ISD::getSetCCSwappedOperands(Op1);
996 if (LL == RL && LR == RR) {
997 bool isInteger = MVT::isInteger(LL.getValueType());
998 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
999 if (Result != ISD::SETCC_INVALID)
1000 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1004 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1005 if (N0.getOpcode() == N1.getOpcode()) {
1006 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1007 if (Tmp.Val) return Tmp;
1010 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1011 // fold (and (sra)) -> (and (srl)) when possible.
1012 if (!MVT::isVector(VT) &&
1013 SimplifyDemandedBits(SDOperand(N, 0)))
1014 return SDOperand(N, 0);
1015 // fold (zext_inreg (extload x)) -> (zextload x)
1016 if (N0.getOpcode() == ISD::EXTLOAD) {
1017 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1018 // If we zero all the possible extended bits, then we can turn this into
1019 // a zextload if we are running before legalize or the operation is legal.
1020 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
1021 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1022 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1023 N0.getOperand(1), N0.getOperand(2),
1026 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1027 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1030 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
1031 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
1032 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1033 // If we zero all the possible extended bits, then we can turn this into
1034 // a zextload if we are running before legalize or the operation is legal.
1035 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
1036 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1037 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1038 N0.getOperand(1), N0.getOperand(2),
1041 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1042 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1046 // fold (and (load x), 255) -> (zextload x, i8)
1047 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1049 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1050 N0.getOpcode() == ISD::ZEXTLOAD) &&
1052 MVT::ValueType EVT, LoadedVT;
1053 if (N1C->getValue() == 255)
1055 else if (N1C->getValue() == 65535)
1057 else if (N1C->getValue() == ~0U)
1062 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1063 cast<VTSDNode>(N0.getOperand(3))->getVT();
1064 if (EVT != MVT::Other && LoadedVT > EVT &&
1065 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
1066 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1067 // For big endian targets, we need to add an offset to the pointer to load
1068 // the correct bytes. For little endian systems, we merely need to read
1069 // fewer bytes from the same pointer.
1071 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1072 SDOperand NewPtr = N0.getOperand(1);
1073 if (!TLI.isLittleEndian())
1074 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1075 DAG.getConstant(PtrOff, PtrType));
1076 AddToWorkList(NewPtr.Val);
1078 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1079 N0.getOperand(2), EVT);
1081 CombineTo(N0.Val, Load, Load.getValue(1));
1082 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1089 SDOperand DAGCombiner::visitOR(SDNode *N) {
1090 SDOperand N0 = N->getOperand(0);
1091 SDOperand N1 = N->getOperand(1);
1092 SDOperand LL, LR, RL, RR, CC0, CC1;
1093 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1094 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1095 MVT::ValueType VT = N1.getValueType();
1096 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1098 // fold (or c1, c2) -> c1|c2
1100 return DAG.getNode(ISD::OR, VT, N0, N1);
1101 // canonicalize constant to RHS
1103 return DAG.getNode(ISD::OR, VT, N1, N0);
1104 // fold (or x, 0) -> x
1105 if (N1C && N1C->isNullValue())
1107 // fold (or x, -1) -> -1
1108 if (N1C && N1C->isAllOnesValue())
1110 // fold (or x, c) -> c iff (x & ~c) == 0
1112 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
1115 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1118 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1119 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
1120 isa<ConstantSDNode>(N0.getOperand(1))) {
1121 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1122 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1124 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
1126 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1127 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1128 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1129 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1131 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1132 MVT::isInteger(LL.getValueType())) {
1133 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1134 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1135 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1136 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1137 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1138 AddToWorkList(ORNode.Val);
1139 return DAG.getSetCC(VT, ORNode, LR, Op1);
1141 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1142 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1143 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1144 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1145 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1146 AddToWorkList(ANDNode.Val);
1147 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1150 // canonicalize equivalent to ll == rl
1151 if (LL == RR && LR == RL) {
1152 Op1 = ISD::getSetCCSwappedOperands(Op1);
1155 if (LL == RL && LR == RR) {
1156 bool isInteger = MVT::isInteger(LL.getValueType());
1157 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1158 if (Result != ISD::SETCC_INVALID)
1159 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1163 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1164 if (N0.getOpcode() == N1.getOpcode()) {
1165 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1166 if (Tmp.Val) return Tmp;
1169 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1170 if (N0.getOpcode() == ISD::AND &&
1171 N1.getOpcode() == ISD::AND &&
1172 N0.getOperand(1).getOpcode() == ISD::Constant &&
1173 N1.getOperand(1).getOpcode() == ISD::Constant &&
1174 // Don't increase # computations.
1175 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1176 // We can only do this xform if we know that bits from X that are set in C2
1177 // but not in C1 are already zero. Likewise for Y.
1178 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1179 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1181 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1182 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1183 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1184 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1189 // See if this is some rotate idiom.
1190 if (SDNode *Rot = MatchRotate(N0, N1))
1191 return SDOperand(Rot, 0);
1197 /// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1198 static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1199 if (Op.getOpcode() == ISD::AND) {
1200 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1201 Mask = Op.getOperand(1);
1202 Op = Op.getOperand(0);
1208 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1216 // MatchRotate - Handle an 'or' of two operands. If this is one of the many
1217 // idioms for rotate, and if the target supports rotation instructions, generate
1219 SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1220 // Must be a legal type. Expanded an promoted things won't work with rotates.
1221 MVT::ValueType VT = LHS.getValueType();
1222 if (!TLI.isTypeLegal(VT)) return 0;
1224 // The target must have at least one rotate flavor.
1225 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1226 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1227 if (!HasROTL && !HasROTR) return 0;
1229 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1230 SDOperand LHSShift; // The shift.
1231 SDOperand LHSMask; // AND value if any.
1232 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1233 return 0; // Not part of a rotate.
1235 SDOperand RHSShift; // The shift.
1236 SDOperand RHSMask; // AND value if any.
1237 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1238 return 0; // Not part of a rotate.
1240 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1241 return 0; // Not shifting the same value.
1243 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1244 return 0; // Shifts must disagree.
1246 // Canonicalize shl to left side in a shl/srl pair.
1247 if (RHSShift.getOpcode() == ISD::SHL) {
1248 std::swap(LHS, RHS);
1249 std::swap(LHSShift, RHSShift);
1250 std::swap(LHSMask , RHSMask );
1253 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1255 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1256 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1257 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1258 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1259 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1260 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1261 if ((LShVal + RShVal) != OpSizeInBits)
1266 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1267 LHSShift.getOperand(1));
1269 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1270 RHSShift.getOperand(1));
1272 // If there is an AND of either shifted operand, apply it to the result.
1273 if (LHSMask.Val || RHSMask.Val) {
1274 uint64_t Mask = MVT::getIntVTBitMask(VT);
1277 uint64_t RHSBits = (1ULL << LShVal)-1;
1278 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1281 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1282 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1285 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1291 // If there is a mask here, and we have a variable shift, we can't be sure
1292 // that we're masking out the right stuff.
1293 if (LHSMask.Val || RHSMask.Val)
1296 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1297 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1298 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1299 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1300 if (ConstantSDNode *SUBC =
1301 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1302 if (SUBC->getValue() == OpSizeInBits)
1304 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1305 LHSShift.getOperand(1)).Val;
1307 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1308 LHSShift.getOperand(1)).Val;
1312 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1313 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1314 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1315 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1316 if (ConstantSDNode *SUBC =
1317 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1318 if (SUBC->getValue() == OpSizeInBits)
1320 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1321 LHSShift.getOperand(1)).Val;
1323 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1324 RHSShift.getOperand(1)).Val;
1332 SDOperand DAGCombiner::visitXOR(SDNode *N) {
1333 SDOperand N0 = N->getOperand(0);
1334 SDOperand N1 = N->getOperand(1);
1335 SDOperand LHS, RHS, CC;
1336 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1337 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1338 MVT::ValueType VT = N0.getValueType();
1340 // fold (xor c1, c2) -> c1^c2
1342 return DAG.getNode(ISD::XOR, VT, N0, N1);
1343 // canonicalize constant to RHS
1345 return DAG.getNode(ISD::XOR, VT, N1, N0);
1346 // fold (xor x, 0) -> x
1347 if (N1C && N1C->isNullValue())
1350 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1353 // fold !(x cc y) -> (x !cc y)
1354 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1355 bool isInt = MVT::isInteger(LHS.getValueType());
1356 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1358 if (N0.getOpcode() == ISD::SETCC)
1359 return DAG.getSetCC(VT, LHS, RHS, NotCC);
1360 if (N0.getOpcode() == ISD::SELECT_CC)
1361 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
1362 assert(0 && "Unhandled SetCC Equivalent!");
1365 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1366 if (N1C && N1C->getValue() == 1 &&
1367 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
1368 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
1369 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1370 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
1371 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1372 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
1373 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
1374 return DAG.getNode(NewOpcode, VT, LHS, RHS);
1377 // fold !(x or y) -> (!x and !y) iff x or y are constants
1378 if (N1C && N1C->isAllOnesValue() &&
1379 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
1380 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
1381 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1382 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
1383 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1384 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
1385 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
1386 return DAG.getNode(NewOpcode, VT, LHS, RHS);
1389 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1390 if (N1C && N0.getOpcode() == ISD::XOR) {
1391 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1392 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1394 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1395 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1397 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1398 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1400 // fold (xor x, x) -> 0
1402 if (!MVT::isVector(VT)) {
1403 return DAG.getConstant(0, VT);
1404 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1405 // Produce a vector of zeros.
1406 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1407 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1408 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1412 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1413 if (N0.getOpcode() == N1.getOpcode()) {
1414 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1415 if (Tmp.Val) return Tmp;
1418 // Simplify the expression using non-local knowledge.
1419 if (!MVT::isVector(VT) &&
1420 SimplifyDemandedBits(SDOperand(N, 0)))
1421 return SDOperand(N, 0);
1426 SDOperand DAGCombiner::visitSHL(SDNode *N) {
1427 SDOperand N0 = N->getOperand(0);
1428 SDOperand N1 = N->getOperand(1);
1429 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1430 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1431 MVT::ValueType VT = N0.getValueType();
1432 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1434 // fold (shl c1, c2) -> c1<<c2
1436 return DAG.getNode(ISD::SHL, VT, N0, N1);
1437 // fold (shl 0, x) -> 0
1438 if (N0C && N0C->isNullValue())
1440 // fold (shl x, c >= size(x)) -> undef
1441 if (N1C && N1C->getValue() >= OpSizeInBits)
1442 return DAG.getNode(ISD::UNDEF, VT);
1443 // fold (shl x, 0) -> x
1444 if (N1C && N1C->isNullValue())
1446 // if (shl x, c) is known to be zero, return 0
1447 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
1448 return DAG.getConstant(0, VT);
1449 if (SimplifyDemandedBits(SDOperand(N, 0)))
1450 return SDOperand(N, 0);
1451 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
1452 if (N1C && N0.getOpcode() == ISD::SHL &&
1453 N0.getOperand(1).getOpcode() == ISD::Constant) {
1454 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1455 uint64_t c2 = N1C->getValue();
1456 if (c1 + c2 > OpSizeInBits)
1457 return DAG.getConstant(0, VT);
1458 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
1459 DAG.getConstant(c1 + c2, N1.getValueType()));
1461 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1462 // (srl (and x, -1 << c1), c1-c2)
1463 if (N1C && N0.getOpcode() == ISD::SRL &&
1464 N0.getOperand(1).getOpcode() == ISD::Constant) {
1465 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1466 uint64_t c2 = N1C->getValue();
1467 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1468 DAG.getConstant(~0ULL << c1, VT));
1470 return DAG.getNode(ISD::SHL, VT, Mask,
1471 DAG.getConstant(c2-c1, N1.getValueType()));
1473 return DAG.getNode(ISD::SRL, VT, Mask,
1474 DAG.getConstant(c1-c2, N1.getValueType()));
1476 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
1477 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
1478 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1479 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1480 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1481 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1482 isa<ConstantSDNode>(N0.getOperand(1))) {
1483 return DAG.getNode(ISD::ADD, VT,
1484 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1485 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1490 SDOperand DAGCombiner::visitSRA(SDNode *N) {
1491 SDOperand N0 = N->getOperand(0);
1492 SDOperand N1 = N->getOperand(1);
1493 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1494 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1495 MVT::ValueType VT = N0.getValueType();
1497 // fold (sra c1, c2) -> c1>>c2
1499 return DAG.getNode(ISD::SRA, VT, N0, N1);
1500 // fold (sra 0, x) -> 0
1501 if (N0C && N0C->isNullValue())
1503 // fold (sra -1, x) -> -1
1504 if (N0C && N0C->isAllOnesValue())
1506 // fold (sra x, c >= size(x)) -> undef
1507 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
1508 return DAG.getNode(ISD::UNDEF, VT);
1509 // fold (sra x, 0) -> x
1510 if (N1C && N1C->isNullValue())
1512 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1514 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1515 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1518 default: EVT = MVT::Other; break;
1519 case 1: EVT = MVT::i1; break;
1520 case 8: EVT = MVT::i8; break;
1521 case 16: EVT = MVT::i16; break;
1522 case 32: EVT = MVT::i32; break;
1524 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1525 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1526 DAG.getValueType(EVT));
1529 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1530 if (N1C && N0.getOpcode() == ISD::SRA) {
1531 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1532 unsigned Sum = N1C->getValue() + C1->getValue();
1533 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1534 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1535 DAG.getConstant(Sum, N1C->getValueType(0)));
1539 // Simplify, based on bits shifted out of the LHS.
1540 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1541 return SDOperand(N, 0);
1544 // If the sign bit is known to be zero, switch this to a SRL.
1545 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
1546 return DAG.getNode(ISD::SRL, VT, N0, N1);
1550 SDOperand DAGCombiner::visitSRL(SDNode *N) {
1551 SDOperand N0 = N->getOperand(0);
1552 SDOperand N1 = N->getOperand(1);
1553 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1554 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1555 MVT::ValueType VT = N0.getValueType();
1556 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1558 // fold (srl c1, c2) -> c1 >>u c2
1560 return DAG.getNode(ISD::SRL, VT, N0, N1);
1561 // fold (srl 0, x) -> 0
1562 if (N0C && N0C->isNullValue())
1564 // fold (srl x, c >= size(x)) -> undef
1565 if (N1C && N1C->getValue() >= OpSizeInBits)
1566 return DAG.getNode(ISD::UNDEF, VT);
1567 // fold (srl x, 0) -> x
1568 if (N1C && N1C->isNullValue())
1570 // if (srl x, c) is known to be zero, return 0
1571 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
1572 return DAG.getConstant(0, VT);
1573 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
1574 if (N1C && N0.getOpcode() == ISD::SRL &&
1575 N0.getOperand(1).getOpcode() == ISD::Constant) {
1576 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1577 uint64_t c2 = N1C->getValue();
1578 if (c1 + c2 > OpSizeInBits)
1579 return DAG.getConstant(0, VT);
1580 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
1581 DAG.getConstant(c1 + c2, N1.getValueType()));
1584 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1585 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1586 // Shifting in all undef bits?
1587 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1588 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1589 return DAG.getNode(ISD::UNDEF, VT);
1591 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1592 AddToWorkList(SmallShift.Val);
1593 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1596 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1597 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1598 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1599 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1600 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1602 // If any of the input bits are KnownOne, then the input couldn't be all
1603 // zeros, thus the result of the srl will always be zero.
1604 if (KnownOne) return DAG.getConstant(0, VT);
1606 // If all of the bits input the to ctlz node are known to be zero, then
1607 // the result of the ctlz is "32" and the result of the shift is one.
1608 uint64_t UnknownBits = ~KnownZero & Mask;
1609 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1611 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1612 if ((UnknownBits & (UnknownBits-1)) == 0) {
1613 // Okay, we know that only that the single bit specified by UnknownBits
1614 // could be set on input to the CTLZ node. If this bit is set, the SRL
1615 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1616 // to an SRL,XOR pair, which is likely to simplify more.
1617 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1618 SDOperand Op = N0.getOperand(0);
1620 Op = DAG.getNode(ISD::SRL, VT, Op,
1621 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1622 AddToWorkList(Op.Val);
1624 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1631 SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
1632 SDOperand N0 = N->getOperand(0);
1633 MVT::ValueType VT = N->getValueType(0);
1635 // fold (ctlz c1) -> c2
1636 if (isa<ConstantSDNode>(N0))
1637 return DAG.getNode(ISD::CTLZ, VT, N0);
1641 SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
1642 SDOperand N0 = N->getOperand(0);
1643 MVT::ValueType VT = N->getValueType(0);
1645 // fold (cttz c1) -> c2
1646 if (isa<ConstantSDNode>(N0))
1647 return DAG.getNode(ISD::CTTZ, VT, N0);
1651 SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
1652 SDOperand N0 = N->getOperand(0);
1653 MVT::ValueType VT = N->getValueType(0);
1655 // fold (ctpop c1) -> c2
1656 if (isa<ConstantSDNode>(N0))
1657 return DAG.getNode(ISD::CTPOP, VT, N0);
1661 SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1662 SDOperand N0 = N->getOperand(0);
1663 SDOperand N1 = N->getOperand(1);
1664 SDOperand N2 = N->getOperand(2);
1665 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1666 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1667 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1668 MVT::ValueType VT = N->getValueType(0);
1670 // fold select C, X, X -> X
1673 // fold select true, X, Y -> X
1674 if (N0C && !N0C->isNullValue())
1676 // fold select false, X, Y -> Y
1677 if (N0C && N0C->isNullValue())
1679 // fold select C, 1, X -> C | X
1680 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
1681 return DAG.getNode(ISD::OR, VT, N0, N2);
1682 // fold select C, 0, X -> ~C & X
1683 // FIXME: this should check for C type == X type, not i1?
1684 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1685 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1686 AddToWorkList(XORNode.Val);
1687 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1689 // fold select C, X, 1 -> ~C | X
1690 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
1691 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1692 AddToWorkList(XORNode.Val);
1693 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1695 // fold select C, X, 0 -> C & X
1696 // FIXME: this should check for C type == X type, not i1?
1697 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1698 return DAG.getNode(ISD::AND, VT, N0, N1);
1699 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1700 if (MVT::i1 == VT && N0 == N1)
1701 return DAG.getNode(ISD::OR, VT, N0, N2);
1702 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1703 if (MVT::i1 == VT && N0 == N2)
1704 return DAG.getNode(ISD::AND, VT, N0, N1);
1706 // If we can fold this based on the true/false value, do so.
1707 if (SimplifySelectOps(N, N1, N2))
1708 return SDOperand(N, 0); // Don't revisit N.
1710 // fold selects based on a setcc into other things, such as min/max/abs
1711 if (N0.getOpcode() == ISD::SETCC)
1713 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1714 // having to say they don't support SELECT_CC on every type the DAG knows
1715 // about, since there is no way to mark an opcode illegal at all value types
1716 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1717 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1718 N1, N2, N0.getOperand(2));
1720 return SimplifySelect(N0, N1, N2);
1724 SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
1725 SDOperand N0 = N->getOperand(0);
1726 SDOperand N1 = N->getOperand(1);
1727 SDOperand N2 = N->getOperand(2);
1728 SDOperand N3 = N->getOperand(3);
1729 SDOperand N4 = N->getOperand(4);
1730 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1731 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1732 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1733 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1735 // fold select_cc lhs, rhs, x, x, cc -> x
1739 // Determine if the condition we're dealing with is constant
1740 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1742 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1743 if (SCCC->getValue())
1744 return N2; // cond always true -> true val
1746 return N3; // cond always false -> false val
1749 // Fold to a simpler select_cc
1750 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1751 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1752 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1755 // If we can fold this based on the true/false value, do so.
1756 if (SimplifySelectOps(N, N2, N3))
1757 return SDOperand(N, 0); // Don't revisit N.
1759 // fold select_cc into other things, such as min/max/abs
1760 return SimplifySelectCC(N0, N1, N2, N3, CC);
1763 SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1764 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1765 cast<CondCodeSDNode>(N->getOperand(2))->get());
1768 SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
1769 SDOperand N0 = N->getOperand(0);
1770 MVT::ValueType VT = N->getValueType(0);
1772 // fold (sext c1) -> c1
1773 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
1774 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
1776 // fold (sext (sext x)) -> (sext x)
1777 // fold (sext (aext x)) -> (sext x)
1778 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
1779 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1781 // fold (sext (truncate x)) -> (sextinreg x).
1782 if (N0.getOpcode() == ISD::TRUNCATE &&
1783 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1784 N0.getValueType()))) {
1785 SDOperand Op = N0.getOperand(0);
1786 if (Op.getValueType() < VT) {
1787 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1788 } else if (Op.getValueType() > VT) {
1789 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1791 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
1792 DAG.getValueType(N0.getValueType()));
1795 // fold (sext (load x)) -> (sext (truncate (sextload x)))
1796 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1797 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
1798 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1799 N0.getOperand(1), N0.getOperand(2),
1801 CombineTo(N, ExtLoad);
1802 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1803 ExtLoad.getValue(1));
1804 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1807 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1808 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1809 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1811 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1812 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1813 N0.getOperand(1), N0.getOperand(2), EVT);
1814 CombineTo(N, ExtLoad);
1815 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1816 ExtLoad.getValue(1));
1817 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1823 SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
1824 SDOperand N0 = N->getOperand(0);
1825 MVT::ValueType VT = N->getValueType(0);
1827 // fold (zext c1) -> c1
1828 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
1829 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1830 // fold (zext (zext x)) -> (zext x)
1831 // fold (zext (aext x)) -> (zext x)
1832 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
1833 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1835 // fold (zext (truncate x)) -> (and x, mask)
1836 if (N0.getOpcode() == ISD::TRUNCATE &&
1837 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1838 SDOperand Op = N0.getOperand(0);
1839 if (Op.getValueType() < VT) {
1840 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1841 } else if (Op.getValueType() > VT) {
1842 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1844 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1847 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1848 if (N0.getOpcode() == ISD::AND &&
1849 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1850 N0.getOperand(1).getOpcode() == ISD::Constant) {
1851 SDOperand X = N0.getOperand(0).getOperand(0);
1852 if (X.getValueType() < VT) {
1853 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1854 } else if (X.getValueType() > VT) {
1855 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1857 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1858 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1861 // fold (zext (load x)) -> (zext (truncate (zextload x)))
1862 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1863 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
1864 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1865 N0.getOperand(1), N0.getOperand(2),
1867 CombineTo(N, ExtLoad);
1868 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1869 ExtLoad.getValue(1));
1870 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1873 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1874 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1875 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1877 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1878 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1879 N0.getOperand(1), N0.getOperand(2), EVT);
1880 CombineTo(N, ExtLoad);
1881 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1882 ExtLoad.getValue(1));
1883 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1888 SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1889 SDOperand N0 = N->getOperand(0);
1890 MVT::ValueType VT = N->getValueType(0);
1892 // fold (aext c1) -> c1
1893 if (isa<ConstantSDNode>(N0))
1894 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1895 // fold (aext (aext x)) -> (aext x)
1896 // fold (aext (zext x)) -> (zext x)
1897 // fold (aext (sext x)) -> (sext x)
1898 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1899 N0.getOpcode() == ISD::ZERO_EXTEND ||
1900 N0.getOpcode() == ISD::SIGN_EXTEND)
1901 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1903 // fold (aext (truncate x))
1904 if (N0.getOpcode() == ISD::TRUNCATE) {
1905 SDOperand TruncOp = N0.getOperand(0);
1906 if (TruncOp.getValueType() == VT)
1907 return TruncOp; // x iff x size == zext size.
1908 if (TruncOp.getValueType() > VT)
1909 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
1910 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
1913 // fold (aext (and (trunc x), cst)) -> (and x, cst).
1914 if (N0.getOpcode() == ISD::AND &&
1915 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1916 N0.getOperand(1).getOpcode() == ISD::Constant) {
1917 SDOperand X = N0.getOperand(0).getOperand(0);
1918 if (X.getValueType() < VT) {
1919 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1920 } else if (X.getValueType() > VT) {
1921 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1923 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1924 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1927 // fold (aext (load x)) -> (aext (truncate (extload x)))
1928 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1929 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
1930 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
1931 N0.getOperand(1), N0.getOperand(2),
1933 CombineTo(N, ExtLoad);
1934 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1935 ExtLoad.getValue(1));
1936 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1939 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
1940 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
1941 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
1942 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD ||
1943 N0.getOpcode() == ISD::SEXTLOAD) &&
1945 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
1946 SDOperand ExtLoad = DAG.getExtLoad(N0.getOpcode(), VT, N0.getOperand(0),
1947 N0.getOperand(1), N0.getOperand(2), EVT);
1948 CombineTo(N, ExtLoad);
1949 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1950 ExtLoad.getValue(1));
1951 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1957 SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
1958 SDOperand N0 = N->getOperand(0);
1959 SDOperand N1 = N->getOperand(1);
1960 MVT::ValueType VT = N->getValueType(0);
1961 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
1962 unsigned EVTBits = MVT::getSizeInBits(EVT);
1964 // fold (sext_in_reg c1) -> c1
1965 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
1966 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
1968 // If the input is already sign extended, just drop the extension.
1969 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
1972 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1973 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1974 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
1975 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
1978 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1979 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
1980 return DAG.getZeroExtendInReg(N0, EVT);
1982 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
1983 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
1984 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
1985 if (N0.getOpcode() == ISD::SRL) {
1986 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1987 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
1988 // We can turn this into an SRA iff the input to the SRL is already sign
1990 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
1991 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
1992 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
1996 // fold (sext_inreg (extload x)) -> (sextload x)
1997 if (N0.getOpcode() == ISD::EXTLOAD &&
1998 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
1999 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
2000 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2001 N0.getOperand(1), N0.getOperand(2),
2003 CombineTo(N, ExtLoad);
2004 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
2005 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2007 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
2008 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
2009 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
2010 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
2011 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
2012 N0.getOperand(1), N0.getOperand(2),
2014 CombineTo(N, ExtLoad);
2015 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
2016 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2021 SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
2022 SDOperand N0 = N->getOperand(0);
2023 MVT::ValueType VT = N->getValueType(0);
2026 if (N0.getValueType() == N->getValueType(0))
2028 // fold (truncate c1) -> c1
2029 if (isa<ConstantSDNode>(N0))
2030 return DAG.getNode(ISD::TRUNCATE, VT, N0);
2031 // fold (truncate (truncate x)) -> (truncate x)
2032 if (N0.getOpcode() == ISD::TRUNCATE)
2033 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
2034 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
2035 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2036 N0.getOpcode() == ISD::ANY_EXTEND) {
2037 if (N0.getValueType() < VT)
2038 // if the source is smaller than the dest, we still need an extend
2039 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2040 else if (N0.getValueType() > VT)
2041 // if the source is larger than the dest, than we just need the truncate
2042 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
2044 // if the source and dest are the same type, we can drop both the extend
2046 return N0.getOperand(0);
2048 // fold (truncate (load x)) -> (smaller load x)
2049 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
2050 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2051 "Cannot truncate to larger type!");
2052 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
2053 // For big endian targets, we need to add an offset to the pointer to load
2054 // the correct bytes. For little endian systems, we merely need to read
2055 // fewer bytes from the same pointer.
2057 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
2058 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
2059 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
2060 DAG.getConstant(PtrOff, PtrType));
2061 AddToWorkList(NewPtr.Val);
2062 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
2064 CombineTo(N0.Val, Load, Load.getValue(1));
2065 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2070 SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2071 SDOperand N0 = N->getOperand(0);
2072 MVT::ValueType VT = N->getValueType(0);
2074 // If the input is a constant, let getNode() fold it.
2075 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2076 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2077 if (Res.Val != N) return Res;
2080 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2081 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
2083 // fold (conv (load x)) -> (load (conv*)x)
2084 // FIXME: These xforms need to know that the resultant load doesn't need a
2085 // higher alignment than the original!
2086 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
2087 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
2090 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2098 SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2099 SDOperand N0 = N->getOperand(0);
2100 MVT::ValueType VT = N->getValueType(0);
2102 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2103 // First check to see if this is all constant.
2104 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2105 VT == MVT::Vector) {
2106 bool isSimple = true;
2107 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2108 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2109 N0.getOperand(i).getOpcode() != ISD::Constant &&
2110 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2115 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2116 if (isSimple && !MVT::isVector(DestEltVT)) {
2117 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2124 /// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2125 /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2126 /// destination element value type.
2127 SDOperand DAGCombiner::
2128 ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2129 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2131 // If this is already the right type, we're done.
2132 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2134 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2135 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2137 // If this is a conversion of N elements of one type to N elements of another
2138 // type, convert each element. This handles FP<->INT cases.
2139 if (SrcBitSize == DstBitSize) {
2140 SmallVector<SDOperand, 8> Ops;
2141 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2142 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
2143 AddToWorkList(Ops.back().Val);
2145 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2146 Ops.push_back(DAG.getValueType(DstEltVT));
2147 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
2150 // Otherwise, we're growing or shrinking the elements. To avoid having to
2151 // handle annoying details of growing/shrinking FP values, we convert them to
2153 if (MVT::isFloatingPoint(SrcEltVT)) {
2154 // Convert the input float vector to a int vector where the elements are the
2156 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2157 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2158 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2162 // Now we know the input is an integer vector. If the output is a FP type,
2163 // convert to integer first, then to FP of the right size.
2164 if (MVT::isFloatingPoint(DstEltVT)) {
2165 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2166 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2167 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2169 // Next, convert to FP elements of the same size.
2170 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2173 // Okay, we know the src/dst types are both integers of differing types.
2174 // Handling growing first.
2175 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2176 if (SrcBitSize < DstBitSize) {
2177 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2179 SmallVector<SDOperand, 8> Ops;
2180 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2181 i += NumInputsPerOutput) {
2182 bool isLE = TLI.isLittleEndian();
2183 uint64_t NewBits = 0;
2184 bool EltIsUndef = true;
2185 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2186 // Shift the previously computed bits over.
2187 NewBits <<= SrcBitSize;
2188 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2189 if (Op.getOpcode() == ISD::UNDEF) continue;
2192 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2196 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2198 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2201 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2202 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2203 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
2206 // Finally, this must be the case where we are shrinking elements: each input
2207 // turns into multiple outputs.
2208 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
2209 SmallVector<SDOperand, 8> Ops;
2210 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2211 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2212 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2213 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2216 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2218 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2219 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2220 OpVal >>= DstBitSize;
2221 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2224 // For big endian targets, swap the order of the pieces of each element.
2225 if (!TLI.isLittleEndian())
2226 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2228 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2229 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2230 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
2235 SDOperand DAGCombiner::visitFADD(SDNode *N) {
2236 SDOperand N0 = N->getOperand(0);
2237 SDOperand N1 = N->getOperand(1);
2238 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2239 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2240 MVT::ValueType VT = N->getValueType(0);
2242 // fold (fadd c1, c2) -> c1+c2
2244 return DAG.getNode(ISD::FADD, VT, N0, N1);
2245 // canonicalize constant to RHS
2246 if (N0CFP && !N1CFP)
2247 return DAG.getNode(ISD::FADD, VT, N1, N0);
2248 // fold (A + (-B)) -> A-B
2249 if (N1.getOpcode() == ISD::FNEG)
2250 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
2251 // fold ((-A) + B) -> B-A
2252 if (N0.getOpcode() == ISD::FNEG)
2253 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
2257 SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2258 SDOperand N0 = N->getOperand(0);
2259 SDOperand N1 = N->getOperand(1);
2260 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2261 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2262 MVT::ValueType VT = N->getValueType(0);
2264 // fold (fsub c1, c2) -> c1-c2
2266 return DAG.getNode(ISD::FSUB, VT, N0, N1);
2267 // fold (A-(-B)) -> A+B
2268 if (N1.getOpcode() == ISD::FNEG)
2269 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
2273 SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2274 SDOperand N0 = N->getOperand(0);
2275 SDOperand N1 = N->getOperand(1);
2276 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2277 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2278 MVT::ValueType VT = N->getValueType(0);
2280 // fold (fmul c1, c2) -> c1*c2
2282 return DAG.getNode(ISD::FMUL, VT, N0, N1);
2283 // canonicalize constant to RHS
2284 if (N0CFP && !N1CFP)
2285 return DAG.getNode(ISD::FMUL, VT, N1, N0);
2286 // fold (fmul X, 2.0) -> (fadd X, X)
2287 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2288 return DAG.getNode(ISD::FADD, VT, N0, N0);
2292 SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2293 SDOperand N0 = N->getOperand(0);
2294 SDOperand N1 = N->getOperand(1);
2295 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2296 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2297 MVT::ValueType VT = N->getValueType(0);
2299 // fold (fdiv c1, c2) -> c1/c2
2301 return DAG.getNode(ISD::FDIV, VT, N0, N1);
2305 SDOperand DAGCombiner::visitFREM(SDNode *N) {
2306 SDOperand N0 = N->getOperand(0);
2307 SDOperand N1 = N->getOperand(1);
2308 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2309 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2310 MVT::ValueType VT = N->getValueType(0);
2312 // fold (frem c1, c2) -> fmod(c1,c2)
2314 return DAG.getNode(ISD::FREM, VT, N0, N1);
2318 SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2319 SDOperand N0 = N->getOperand(0);
2320 SDOperand N1 = N->getOperand(1);
2321 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2322 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2323 MVT::ValueType VT = N->getValueType(0);
2325 if (N0CFP && N1CFP) // Constant fold
2326 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2329 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2330 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2335 u.d = N1CFP->getValue();
2337 return DAG.getNode(ISD::FABS, VT, N0);
2339 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2342 // copysign(fabs(x), y) -> copysign(x, y)
2343 // copysign(fneg(x), y) -> copysign(x, y)
2344 // copysign(copysign(x,z), y) -> copysign(x, y)
2345 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2346 N0.getOpcode() == ISD::FCOPYSIGN)
2347 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2349 // copysign(x, abs(y)) -> abs(x)
2350 if (N1.getOpcode() == ISD::FABS)
2351 return DAG.getNode(ISD::FABS, VT, N0);
2353 // copysign(x, copysign(y,z)) -> copysign(x, z)
2354 if (N1.getOpcode() == ISD::FCOPYSIGN)
2355 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2357 // copysign(x, fp_extend(y)) -> copysign(x, y)
2358 // copysign(x, fp_round(y)) -> copysign(x, y)
2359 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2360 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2367 SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
2368 SDOperand N0 = N->getOperand(0);
2369 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2370 MVT::ValueType VT = N->getValueType(0);
2372 // fold (sint_to_fp c1) -> c1fp
2374 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
2378 SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
2379 SDOperand N0 = N->getOperand(0);
2380 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2381 MVT::ValueType VT = N->getValueType(0);
2383 // fold (uint_to_fp c1) -> c1fp
2385 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
2389 SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
2390 SDOperand N0 = N->getOperand(0);
2391 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2392 MVT::ValueType VT = N->getValueType(0);
2394 // fold (fp_to_sint c1fp) -> c1
2396 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
2400 SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
2401 SDOperand N0 = N->getOperand(0);
2402 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2403 MVT::ValueType VT = N->getValueType(0);
2405 // fold (fp_to_uint c1fp) -> c1
2407 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
2411 SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
2412 SDOperand N0 = N->getOperand(0);
2413 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2414 MVT::ValueType VT = N->getValueType(0);
2416 // fold (fp_round c1fp) -> c1fp
2418 return DAG.getNode(ISD::FP_ROUND, VT, N0);
2420 // fold (fp_round (fp_extend x)) -> x
2421 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2422 return N0.getOperand(0);
2424 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2425 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2426 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2427 AddToWorkList(Tmp.Val);
2428 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2434 SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
2435 SDOperand N0 = N->getOperand(0);
2436 MVT::ValueType VT = N->getValueType(0);
2437 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2438 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2440 // fold (fp_round_inreg c1fp) -> c1fp
2442 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
2443 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
2448 SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
2449 SDOperand N0 = N->getOperand(0);
2450 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2451 MVT::ValueType VT = N->getValueType(0);
2453 // fold (fp_extend c1fp) -> c1fp
2455 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
2457 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2458 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
2459 (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) {
2460 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0),
2461 N0.getOperand(1), N0.getOperand(2),
2463 CombineTo(N, ExtLoad);
2464 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2465 ExtLoad.getValue(1));
2466 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2473 SDOperand DAGCombiner::visitFNEG(SDNode *N) {
2474 SDOperand N0 = N->getOperand(0);
2475 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2476 MVT::ValueType VT = N->getValueType(0);
2478 // fold (fneg c1) -> -c1
2480 return DAG.getNode(ISD::FNEG, VT, N0);
2481 // fold (fneg (sub x, y)) -> (sub y, x)
2482 if (N0.getOpcode() == ISD::SUB)
2483 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
2484 // fold (fneg (fneg x)) -> x
2485 if (N0.getOpcode() == ISD::FNEG)
2486 return N0.getOperand(0);
2490 SDOperand DAGCombiner::visitFABS(SDNode *N) {
2491 SDOperand N0 = N->getOperand(0);
2492 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2493 MVT::ValueType VT = N->getValueType(0);
2495 // fold (fabs c1) -> fabs(c1)
2497 return DAG.getNode(ISD::FABS, VT, N0);
2498 // fold (fabs (fabs x)) -> (fabs x)
2499 if (N0.getOpcode() == ISD::FABS)
2500 return N->getOperand(0);
2501 // fold (fabs (fneg x)) -> (fabs x)
2502 // fold (fabs (fcopysign x, y)) -> (fabs x)
2503 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2504 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2509 SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2510 SDOperand Chain = N->getOperand(0);
2511 SDOperand N1 = N->getOperand(1);
2512 SDOperand N2 = N->getOperand(2);
2513 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2515 // never taken branch, fold to chain
2516 if (N1C && N1C->isNullValue())
2518 // unconditional branch
2519 if (N1C && N1C->getValue() == 1)
2520 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2521 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2523 if (N1.getOpcode() == ISD::SETCC &&
2524 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2525 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2526 N1.getOperand(0), N1.getOperand(1), N2);
2531 // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2533 SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
2534 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2535 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2537 // Use SimplifySetCC to simplify SETCC's.
2538 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2539 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2541 // fold br_cc true, dest -> br dest (unconditional branch)
2542 if (SCCC && SCCC->getValue())
2543 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2545 // fold br_cc false, dest -> unconditional fall through
2546 if (SCCC && SCCC->isNullValue())
2547 return N->getOperand(0);
2548 // fold to a simpler setcc
2549 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2550 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2551 Simp.getOperand(2), Simp.getOperand(0),
2552 Simp.getOperand(1), N->getOperand(4));
2556 SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2557 SDOperand Chain = N->getOperand(0);
2558 SDOperand Ptr = N->getOperand(1);
2559 SDOperand SrcValue = N->getOperand(2);
2561 // If there are no uses of the loaded value, change uses of the chain value
2562 // into uses of the chain input (i.e. delete the dead load).
2563 if (N->hasNUsesOfValue(0, 0))
2564 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2566 // If this load is directly stored, replace the load value with the stored
2568 // TODO: Handle store large -> read small portion.
2569 // TODO: Handle TRUNCSTORE/EXTLOAD
2570 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2571 Chain.getOperand(1).getValueType() == N->getValueType(0))
2572 return CombineTo(N, Chain.getOperand(1), Chain);
2577 /// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2578 SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2579 SDOperand Chain = N->getOperand(0);
2580 SDOperand Ptr = N->getOperand(1);
2581 SDOperand SrcValue = N->getOperand(2);
2582 SDOperand EVT = N->getOperand(3);
2584 // If there are no uses of the loaded value, change uses of the chain value
2585 // into uses of the chain input (i.e. delete the dead load).
2586 if (N->hasNUsesOfValue(0, 0))
2587 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2592 /// isNotAlias - Return true if we have definitive knowlege that the two
2593 /// addresses don't overlap.
2594 bool DAGCombiner::isNotAlias(SDOperand Ptr1, SDOperand Ptr2) {
2596 if (!CombinerAA) return false;
2598 // If they are the same then they must be aliases.
2599 if (Ptr1 == Ptr2) return false;
2601 // If both operands are frame values (not the same location from above test)
2602 // then they can't alias.
2603 FrameIndexSDNode *FI1 = dyn_cast<FrameIndexSDNode>(Ptr1);
2604 FrameIndexSDNode *FI2 = dyn_cast<FrameIndexSDNode>(Ptr2);
2609 // Otherwise we don't know and have to play it safe.
2613 SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2614 SDOperand Chain = N->getOperand(0);
2615 SDOperand Value = N->getOperand(1);
2616 SDOperand Ptr = N->getOperand(2);
2617 SDOperand SrcValue = N->getOperand(3);
2619 // If this is a store that kills a previous store, remove the previous store.
2620 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2621 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2622 // Make sure that these stores are the same value type:
2623 // FIXME: we really care that the second store is >= size of the first.
2624 Value.getValueType() == Chain.getOperand(1).getValueType()) {
2625 // Create a new store of Value that replaces both stores.
2626 SDNode *PrevStore = Chain.Val;
2627 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2629 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2630 PrevStore->getOperand(0), Value, Ptr,
2632 CombineTo(N, NewStore); // Nuke this store.
2633 CombineTo(PrevStore, NewStore); // Nuke the previous store.
2634 return SDOperand(N, 0);
2637 // If this is a store of a bit convert, store the input value.
2638 // FIXME: This needs to know that the resultant store does not need a
2639 // higher alignment than the original.
2640 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
2641 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2644 // If the previous store is not an alias then break artificial chain.
2645 if (Chain.getOpcode() == ISD::STORE && isNotAlias(Ptr, Chain.getOperand(2))) {
2646 // Replace the chain to void dependency.
2647 SDNode *PrevStore = Chain.Val;
2648 SDOperand ReplStore = DAG.getNode(ISD::STORE, MVT::Other,
2649 PrevStore->getOperand(0), Value, Ptr,
2651 // Create token to keep both stores around.
2652 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2654 // Replace uses with token.
2661 SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2662 SDOperand InVec = N->getOperand(0);
2663 SDOperand InVal = N->getOperand(1);
2664 SDOperand EltNo = N->getOperand(2);
2666 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2667 // vector with the inserted element.
2668 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2669 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2670 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2671 if (Elt < Ops.size())
2673 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2674 &Ops[0], Ops.size());
2680 SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2681 SDOperand InVec = N->getOperand(0);
2682 SDOperand InVal = N->getOperand(1);
2683 SDOperand EltNo = N->getOperand(2);
2684 SDOperand NumElts = N->getOperand(3);
2685 SDOperand EltType = N->getOperand(4);
2687 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2688 // vector with the inserted element.
2689 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2690 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2691 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2692 if (Elt < Ops.size()-2)
2694 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2695 &Ops[0], Ops.size());
2701 SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2702 unsigned NumInScalars = N->getNumOperands()-2;
2703 SDOperand NumElts = N->getOperand(NumInScalars);
2704 SDOperand EltType = N->getOperand(NumInScalars+1);
2706 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2707 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2708 // two distinct vectors, turn this into a shuffle node.
2709 SDOperand VecIn1, VecIn2;
2710 for (unsigned i = 0; i != NumInScalars; ++i) {
2711 // Ignore undef inputs.
2712 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2714 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2715 // constant index, bail out.
2716 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2717 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2718 VecIn1 = VecIn2 = SDOperand(0, 0);
2722 // If the input vector type disagrees with the result of the vbuild_vector,
2723 // we can't make a shuffle.
2724 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2725 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2726 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2727 VecIn1 = VecIn2 = SDOperand(0, 0);
2731 // Otherwise, remember this. We allow up to two distinct input vectors.
2732 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2735 if (VecIn1.Val == 0) {
2736 VecIn1 = ExtractedFromVec;
2737 } else if (VecIn2.Val == 0) {
2738 VecIn2 = ExtractedFromVec;
2741 VecIn1 = VecIn2 = SDOperand(0, 0);
2746 // If everything is good, we can make a shuffle operation.
2748 SmallVector<SDOperand, 8> BuildVecIndices;
2749 for (unsigned i = 0; i != NumInScalars; ++i) {
2750 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2751 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2755 SDOperand Extract = N->getOperand(i);
2757 // If extracting from the first vector, just use the index directly.
2758 if (Extract.getOperand(0) == VecIn1) {
2759 BuildVecIndices.push_back(Extract.getOperand(1));
2763 // Otherwise, use InIdx + VecSize
2764 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2765 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2768 // Add count and size info.
2769 BuildVecIndices.push_back(NumElts);
2770 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2772 // Return the new VVECTOR_SHUFFLE node.
2778 // Use an undef vbuild_vector as input for the second operand.
2779 std::vector<SDOperand> UnOps(NumInScalars,
2780 DAG.getNode(ISD::UNDEF,
2781 cast<VTSDNode>(EltType)->getVT()));
2782 UnOps.push_back(NumElts);
2783 UnOps.push_back(EltType);
2784 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2785 &UnOps[0], UnOps.size());
2786 AddToWorkList(Ops[1].Val);
2788 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2789 &BuildVecIndices[0], BuildVecIndices.size());
2792 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
2798 SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
2799 SDOperand ShufMask = N->getOperand(2);
2800 unsigned NumElts = ShufMask.getNumOperands();
2802 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2803 bool isIdentity = true;
2804 for (unsigned i = 0; i != NumElts; ++i) {
2805 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2806 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2811 if (isIdentity) return N->getOperand(0);
2813 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2815 for (unsigned i = 0; i != NumElts; ++i) {
2816 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2817 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2822 if (isIdentity) return N->getOperand(1);
2824 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2826 bool isUnary = true;
2827 bool isSplat = true;
2829 unsigned BaseIdx = 0;
2830 for (unsigned i = 0; i != NumElts; ++i)
2831 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2832 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2833 int V = (Idx < NumElts) ? 0 : 1;
2847 SDOperand N0 = N->getOperand(0);
2848 SDOperand N1 = N->getOperand(1);
2849 // Normalize unary shuffle so the RHS is undef.
2850 if (isUnary && VecNum == 1)
2853 // If it is a splat, check if the argument vector is a build_vector with
2854 // all scalar elements the same.
2857 if (V->getOpcode() == ISD::BIT_CONVERT)
2858 V = V->getOperand(0).Val;
2859 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2860 unsigned NumElems = V->getNumOperands()-2;
2861 if (NumElems > BaseIdx) {
2863 bool AllSame = true;
2864 for (unsigned i = 0; i != NumElems; ++i) {
2865 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2866 Base = V->getOperand(i);
2870 // Splat of <u, u, u, u>, return <u, u, u, u>
2873 for (unsigned i = 0; i != NumElems; ++i) {
2874 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2875 V->getOperand(i) != Base) {
2880 // Splat of <x, x, x, x>, return <x, x, x, x>
2887 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
2889 if (isUnary || N0 == N1) {
2890 if (N0.getOpcode() == ISD::UNDEF)
2891 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
2892 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2894 SmallVector<SDOperand, 8> MappedOps;
2895 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
2896 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
2897 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
2898 MappedOps.push_back(ShufMask.getOperand(i));
2901 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2902 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2905 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
2906 &MappedOps[0], MappedOps.size());
2907 AddToWorkList(ShufMask.Val);
2908 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
2910 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2917 SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2918 SDOperand ShufMask = N->getOperand(2);
2919 unsigned NumElts = ShufMask.getNumOperands()-2;
2921 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2922 bool isIdentity = true;
2923 for (unsigned i = 0; i != NumElts; ++i) {
2924 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2925 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2930 if (isIdentity) return N->getOperand(0);
2932 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2934 for (unsigned i = 0; i != NumElts; ++i) {
2935 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2936 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2941 if (isIdentity) return N->getOperand(1);
2943 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2945 bool isUnary = true;
2946 bool isSplat = true;
2948 unsigned BaseIdx = 0;
2949 for (unsigned i = 0; i != NumElts; ++i)
2950 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2951 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2952 int V = (Idx < NumElts) ? 0 : 1;
2966 SDOperand N0 = N->getOperand(0);
2967 SDOperand N1 = N->getOperand(1);
2968 // Normalize unary shuffle so the RHS is undef.
2969 if (isUnary && VecNum == 1)
2972 // If it is a splat, check if the argument vector is a build_vector with
2973 // all scalar elements the same.
2976 if (V->getOpcode() == ISD::VBIT_CONVERT)
2977 V = V->getOperand(0).Val;
2978 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
2979 unsigned NumElems = V->getNumOperands()-2;
2980 if (NumElems > BaseIdx) {
2982 bool AllSame = true;
2983 for (unsigned i = 0; i != NumElems; ++i) {
2984 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2985 Base = V->getOperand(i);
2989 // Splat of <u, u, u, u>, return <u, u, u, u>
2992 for (unsigned i = 0; i != NumElems; ++i) {
2993 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2994 V->getOperand(i) != Base) {
2999 // Splat of <x, x, x, x>, return <x, x, x, x>
3006 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3008 if (isUnary || N0 == N1) {
3009 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3011 SmallVector<SDOperand, 8> MappedOps;
3012 for (unsigned i = 0; i != NumElts; ++i) {
3013 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3014 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3015 MappedOps.push_back(ShufMask.getOperand(i));
3018 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3019 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3022 // Add the type/#elts values.
3023 MappedOps.push_back(ShufMask.getOperand(NumElts));
3024 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3026 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
3027 &MappedOps[0], MappedOps.size());
3028 AddToWorkList(ShufMask.Val);
3030 // Build the undef vector.
3031 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3032 for (unsigned i = 0; i != NumElts; ++i)
3033 MappedOps[i] = UDVal;
3034 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3035 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
3036 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3037 &MappedOps[0], MappedOps.size());
3039 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3040 N0, UDVal, ShufMask,
3041 MappedOps[NumElts], MappedOps[NumElts+1]);
3047 /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3048 /// a VAND to a vector_shuffle with the destination vector and a zero vector.
3049 /// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3050 /// vector_shuffle V, Zero, <0, 4, 2, 4>
3051 SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3052 SDOperand LHS = N->getOperand(0);
3053 SDOperand RHS = N->getOperand(1);
3054 if (N->getOpcode() == ISD::VAND) {
3055 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3056 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3057 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3058 RHS = RHS.getOperand(0);
3059 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3060 std::vector<SDOperand> IdxOps;
3061 unsigned NumOps = RHS.getNumOperands();
3062 unsigned NumElts = NumOps-2;
3063 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3064 for (unsigned i = 0; i != NumElts; ++i) {
3065 SDOperand Elt = RHS.getOperand(i);
3066 if (!isa<ConstantSDNode>(Elt))
3068 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3069 IdxOps.push_back(DAG.getConstant(i, EVT));
3070 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3071 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3076 // Let's see if the target supports this vector_shuffle.
3077 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3080 // Return the new VVECTOR_SHUFFLE node.
3081 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3082 SDOperand EVTNode = DAG.getValueType(EVT);
3083 std::vector<SDOperand> Ops;
3084 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3087 AddToWorkList(LHS.Val);
3088 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3089 ZeroOps.push_back(NumEltsNode);
3090 ZeroOps.push_back(EVTNode);
3091 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3092 &ZeroOps[0], ZeroOps.size()));
3093 IdxOps.push_back(NumEltsNode);
3094 IdxOps.push_back(EVTNode);
3095 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3096 &IdxOps[0], IdxOps.size()));
3097 Ops.push_back(NumEltsNode);
3098 Ops.push_back(EVTNode);
3099 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3100 &Ops[0], Ops.size());
3101 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3102 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3103 DstVecSize, DstVecEVT);
3111 /// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3112 /// the scalar operation of the vop if it is operating on an integer vector
3113 /// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3114 SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3115 ISD::NodeType FPOp) {
3116 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3117 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3118 SDOperand LHS = N->getOperand(0);
3119 SDOperand RHS = N->getOperand(1);
3120 SDOperand Shuffle = XformToShuffleWithZero(N);
3121 if (Shuffle.Val) return Shuffle;
3123 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3125 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3126 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3127 SmallVector<SDOperand, 8> Ops;
3128 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3129 SDOperand LHSOp = LHS.getOperand(i);
3130 SDOperand RHSOp = RHS.getOperand(i);
3131 // If these two elements can't be folded, bail out.
3132 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3133 LHSOp.getOpcode() != ISD::Constant &&
3134 LHSOp.getOpcode() != ISD::ConstantFP) ||
3135 (RHSOp.getOpcode() != ISD::UNDEF &&
3136 RHSOp.getOpcode() != ISD::Constant &&
3137 RHSOp.getOpcode() != ISD::ConstantFP))
3139 // Can't fold divide by zero.
3140 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3141 if ((RHSOp.getOpcode() == ISD::Constant &&
3142 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3143 (RHSOp.getOpcode() == ISD::ConstantFP &&
3144 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3147 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
3148 AddToWorkList(Ops.back().Val);
3149 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3150 Ops.back().getOpcode() == ISD::Constant ||
3151 Ops.back().getOpcode() == ISD::ConstantFP) &&
3152 "Scalar binop didn't fold!");
3155 if (Ops.size() == LHS.getNumOperands()-2) {
3156 Ops.push_back(*(LHS.Val->op_end()-2));
3157 Ops.push_back(*(LHS.Val->op_end()-1));
3158 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
3165 SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
3166 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3168 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3169 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3170 // If we got a simplified select_cc node back from SimplifySelectCC, then
3171 // break it down into a new SETCC node, and a new SELECT node, and then return
3172 // the SELECT node, since we were called with a SELECT node.
3174 // Check to see if we got a select_cc back (to turn into setcc/select).
3175 // Otherwise, just return whatever node we got back, like fabs.
3176 if (SCC.getOpcode() == ISD::SELECT_CC) {
3177 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3178 SCC.getOperand(0), SCC.getOperand(1),
3180 AddToWorkList(SETCC.Val);
3181 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3182 SCC.getOperand(3), SETCC);
3189 /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3190 /// are the two values being selected between, see if we can simplify the
3191 /// select. Callers of this should assume that TheSelect is deleted if this
3192 /// returns true. As such, they should return the appropriate thing (e.g. the
3193 /// node) back to the top-level of the DAG combiner loop to avoid it being
3196 bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3199 // If this is a select from two identical things, try to pull the operation
3200 // through the select.
3201 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
3203 std::cerr << "SELECT: ["; LHS.Val->dump();
3204 std::cerr << "] ["; RHS.Val->dump();
3208 // If this is a load and the token chain is identical, replace the select
3209 // of two loads with a load through a select of the address to load from.
3210 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3211 // constants have been dropped into the constant pool.
3212 if ((LHS.getOpcode() == ISD::LOAD ||
3213 LHS.getOpcode() == ISD::EXTLOAD ||
3214 LHS.getOpcode() == ISD::ZEXTLOAD ||
3215 LHS.getOpcode() == ISD::SEXTLOAD) &&
3216 // Token chains must be identical.
3217 LHS.getOperand(0) == RHS.getOperand(0) &&
3218 // If this is an EXTLOAD, the VT's must match.
3219 (LHS.getOpcode() == ISD::LOAD ||
3220 LHS.getOperand(3) == RHS.getOperand(3))) {
3221 // FIXME: this conflates two src values, discarding one. This is not
3222 // the right thing to do, but nothing uses srcvalues now. When they do,
3223 // turn SrcValue into a list of locations.
3225 if (TheSelect->getOpcode() == ISD::SELECT)
3226 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
3227 TheSelect->getOperand(0), LHS.getOperand(1),
3230 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
3231 TheSelect->getOperand(0),
3232 TheSelect->getOperand(1),
3233 LHS.getOperand(1), RHS.getOperand(1),
3234 TheSelect->getOperand(4));
3237 if (LHS.getOpcode() == ISD::LOAD)
3238 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
3239 Addr, LHS.getOperand(2));
3241 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
3242 LHS.getOperand(0), Addr, LHS.getOperand(2),
3243 cast<VTSDNode>(LHS.getOperand(3))->getVT());
3244 // Users of the select now use the result of the load.
3245 CombineTo(TheSelect, Load);
3247 // Users of the old loads now use the new load's chain. We know the
3248 // old-load value is dead now.
3249 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3250 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3258 SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3259 SDOperand N2, SDOperand N3,
3262 MVT::ValueType VT = N2.getValueType();
3263 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3264 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3265 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3267 // Determine if the condition we're dealing with is constant
3268 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3269 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3271 // fold select_cc true, x, y -> x
3272 if (SCCC && SCCC->getValue())
3274 // fold select_cc false, x, y -> y
3275 if (SCCC && SCCC->getValue() == 0)
3278 // Check to see if we can simplify the select into an fabs node
3279 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3280 // Allow either -0.0 or 0.0
3281 if (CFP->getValue() == 0.0) {
3282 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3283 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3284 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3285 N2 == N3.getOperand(0))
3286 return DAG.getNode(ISD::FABS, VT, N0);
3288 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3289 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3290 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3291 N2.getOperand(0) == N3)
3292 return DAG.getNode(ISD::FABS, VT, N3);
3296 // Check to see if we can perform the "gzip trick", transforming
3297 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
3298 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
3299 MVT::isInteger(N0.getValueType()) &&
3300 MVT::isInteger(N2.getValueType()) &&
3301 (N1C->isNullValue() || // (a < 0) ? b : 0
3302 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
3303 MVT::ValueType XType = N0.getValueType();
3304 MVT::ValueType AType = N2.getValueType();
3305 if (XType >= AType) {
3306 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
3307 // single-bit constant.
3308 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3309 unsigned ShCtV = Log2_64(N2C->getValue());
3310 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3311 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3312 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
3313 AddToWorkList(Shift.Val);
3314 if (XType > AType) {
3315 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
3316 AddToWorkList(Shift.Val);
3318 return DAG.getNode(ISD::AND, AType, Shift, N2);
3320 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3321 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3322 TLI.getShiftAmountTy()));
3323 AddToWorkList(Shift.Val);
3324 if (XType > AType) {
3325 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
3326 AddToWorkList(Shift.Val);
3328 return DAG.getNode(ISD::AND, AType, Shift, N2);
3332 // fold select C, 16, 0 -> shl C, 4
3333 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3334 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3335 // Get a SetCC of the condition
3336 // FIXME: Should probably make sure that setcc is legal if we ever have a
3337 // target where it isn't.
3338 SDOperand Temp, SCC;
3339 // cast from setcc result type to select result type
3340 if (AfterLegalize) {
3341 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3342 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
3344 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
3345 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
3347 AddToWorkList(SCC.Val);
3348 AddToWorkList(Temp.Val);
3349 // shl setcc result by log2 n2c
3350 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3351 DAG.getConstant(Log2_64(N2C->getValue()),
3352 TLI.getShiftAmountTy()));
3355 // Check to see if this is the equivalent of setcc
3356 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3357 // otherwise, go ahead with the folds.
3358 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3359 MVT::ValueType XType = N0.getValueType();
3360 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3361 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3362 if (Res.getValueType() != VT)
3363 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3367 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3368 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3369 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3370 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3371 return DAG.getNode(ISD::SRL, XType, Ctlz,
3372 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3373 TLI.getShiftAmountTy()));
3375 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3376 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3377 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3379 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3380 DAG.getConstant(~0ULL, XType));
3381 return DAG.getNode(ISD::SRL, XType,
3382 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3383 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3384 TLI.getShiftAmountTy()));
3386 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3387 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3388 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3389 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3390 TLI.getShiftAmountTy()));
3391 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3395 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3396 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3397 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3398 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3399 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3400 MVT::ValueType XType = N0.getValueType();
3401 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3402 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3403 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3404 TLI.getShiftAmountTy()));
3405 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
3406 AddToWorkList(Shift.Val);
3407 AddToWorkList(Add.Val);
3408 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3416 SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
3417 SDOperand N1, ISD::CondCode Cond,
3418 bool foldBooleans) {
3419 // These setcc operations always fold.
3423 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3425 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3428 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3429 uint64_t C1 = N1C->getValue();
3430 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3431 uint64_t C0 = N0C->getValue();
3433 // Sign extend the operands if required
3434 if (ISD::isSignedIntSetCC(Cond)) {
3435 C0 = N0C->getSignExtended();
3436 C1 = N1C->getSignExtended();
3440 default: assert(0 && "Unknown integer setcc!");
3441 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3442 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3443 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3444 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3445 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3446 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3447 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3448 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3449 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3450 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3453 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3454 // equality comparison, then we're just comparing whether X itself is
3456 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3457 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3458 N0.getOperand(1).getOpcode() == ISD::Constant) {
3459 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3460 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3461 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3462 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3463 // (srl (ctlz x), 5) == 0 -> X != 0
3464 // (srl (ctlz x), 5) != 1 -> X != 0
3467 // (srl (ctlz x), 5) != 0 -> X == 0
3468 // (srl (ctlz x), 5) == 1 -> X == 0
3471 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3472 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3477 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3478 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3479 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3481 // If the comparison constant has bits in the upper part, the
3482 // zero-extended value could never match.
3483 if (C1 & (~0ULL << InSize)) {
3484 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3488 case ISD::SETEQ: return DAG.getConstant(0, VT);
3491 case ISD::SETNE: return DAG.getConstant(1, VT);
3494 // True if the sign bit of C1 is set.
3495 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3498 // True if the sign bit of C1 isn't set.
3499 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3505 // Otherwise, we can perform the comparison with the low bits.
3513 return DAG.getSetCC(VT, N0.getOperand(0),
3514 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3517 break; // todo, be more careful with signed comparisons
3519 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3520 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3521 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3522 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3523 MVT::ValueType ExtDstTy = N0.getValueType();
3524 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3526 // If the extended part has any inconsistent bits, it cannot ever
3527 // compare equal. In other words, they have to be all ones or all
3530 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3531 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3532 return DAG.getConstant(Cond == ISD::SETNE, VT);
3535 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3536 if (Op0Ty == ExtSrcTy) {
3537 ZextOp = N0.getOperand(0);
3539 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3540 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3541 DAG.getConstant(Imm, Op0Ty));
3543 AddToWorkList(ZextOp.Val);
3544 // Otherwise, make this a use of a zext.
3545 return DAG.getSetCC(VT, ZextOp,
3546 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3549 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3550 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3551 (N0.getOpcode() == ISD::XOR ||
3552 (N0.getOpcode() == ISD::AND &&
3553 N0.getOperand(0).getOpcode() == ISD::XOR &&
3554 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3555 isa<ConstantSDNode>(N0.getOperand(1)) &&
3556 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3557 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3558 // only do this if the top bits are known zero.
3559 if (TLI.MaskedValueIsZero(N1,
3560 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3561 // Okay, get the un-inverted input value.
3563 if (N0.getOpcode() == ISD::XOR)
3564 Val = N0.getOperand(0);
3566 assert(N0.getOpcode() == ISD::AND &&
3567 N0.getOperand(0).getOpcode() == ISD::XOR);
3568 // ((X^1)&1)^1 -> X & 1
3569 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3570 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3572 return DAG.getSetCC(VT, Val, N1,
3573 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3577 uint64_t MinVal, MaxVal;
3578 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3579 if (ISD::isSignedIntSetCC(Cond)) {
3580 MinVal = 1ULL << (OperandBitSize-1);
3581 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3582 MaxVal = ~0ULL >> (65-OperandBitSize);
3587 MaxVal = ~0ULL >> (64-OperandBitSize);
3590 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3591 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3592 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3593 --C1; // X >= C0 --> X > (C0-1)
3594 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3595 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3598 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3599 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3600 ++C1; // X <= C0 --> X < (C0+1)
3601 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3602 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3605 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3606 return DAG.getConstant(0, VT); // X < MIN --> false
3608 // Canonicalize setgt X, Min --> setne X, Min
3609 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3610 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
3611 // Canonicalize setlt X, Max --> setne X, Max
3612 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3613 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
3615 // If we have setult X, 1, turn it into seteq X, 0
3616 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3617 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3619 // If we have setugt X, Max-1, turn it into seteq X, Max
3620 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3621 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3624 // If we have "setcc X, C0", check to see if we can shrink the immediate
3627 // SETUGT X, SINTMAX -> SETLT X, 0
3628 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3629 C1 == (~0ULL >> (65-OperandBitSize)))
3630 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3633 // FIXME: Implement the rest of these.
3635 // Fold bit comparisons when we can.
3636 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3637 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3638 if (ConstantSDNode *AndRHS =
3639 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3640 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3641 // Perform the xform if the AND RHS is a single bit.
3642 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3643 return DAG.getNode(ISD::SRL, VT, N0,
3644 DAG.getConstant(Log2_64(AndRHS->getValue()),
3645 TLI.getShiftAmountTy()));
3647 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3648 // (X & 8) == 8 --> (X & 8) >> 3
3649 // Perform the xform if C1 is a single bit.
3650 if ((C1 & (C1-1)) == 0) {
3651 return DAG.getNode(ISD::SRL, VT, N0,
3652 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
3657 } else if (isa<ConstantSDNode>(N0.Val)) {
3658 // Ensure that the constant occurs on the RHS.
3659 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3662 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3663 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3664 double C0 = N0C->getValue(), C1 = N1C->getValue();
3667 default: break; // FIXME: Implement the rest of these!
3668 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3669 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3670 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3671 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3672 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3673 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3676 // Ensure that the constant occurs on the RHS.
3677 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3681 // We can always fold X == Y for integer setcc's.
3682 if (MVT::isInteger(N0.getValueType()))
3683 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3684 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3685 if (UOF == 2) // FP operators that are undefined on NaNs.
3686 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3687 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3688 return DAG.getConstant(UOF, VT);
3689 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3690 // if it is not already.
3691 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
3692 if (NewCond != Cond)
3693 return DAG.getSetCC(VT, N0, N1, NewCond);
3696 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3697 MVT::isInteger(N0.getValueType())) {
3698 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3699 N0.getOpcode() == ISD::XOR) {
3700 // Simplify (X+Y) == (X+Z) --> Y == Z
3701 if (N0.getOpcode() == N1.getOpcode()) {
3702 if (N0.getOperand(0) == N1.getOperand(0))
3703 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3704 if (N0.getOperand(1) == N1.getOperand(1))
3705 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3706 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
3707 // If X op Y == Y op X, try other combinations.
3708 if (N0.getOperand(0) == N1.getOperand(1))
3709 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3710 if (N0.getOperand(1) == N1.getOperand(0))
3711 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
3715 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3716 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3717 // Turn (X+C1) == C2 --> X == C2-C1
3718 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3719 return DAG.getSetCC(VT, N0.getOperand(0),
3720 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3721 N0.getValueType()), Cond);
3724 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3725 if (N0.getOpcode() == ISD::XOR)
3726 // If we know that all of the inverted bits are zero, don't bother
3727 // performing the inversion.
3728 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
3729 return DAG.getSetCC(VT, N0.getOperand(0),
3730 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
3731 N0.getValueType()), Cond);
3734 // Turn (C1-X) == C2 --> X == C1-C2
3735 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3736 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3737 return DAG.getSetCC(VT, N0.getOperand(1),
3738 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3739 N0.getValueType()), Cond);
3744 // Simplify (X+Z) == X --> Z == 0
3745 if (N0.getOperand(0) == N1)
3746 return DAG.getSetCC(VT, N0.getOperand(1),
3747 DAG.getConstant(0, N0.getValueType()), Cond);
3748 if (N0.getOperand(1) == N1) {
3749 if (DAG.isCommutativeBinOp(N0.getOpcode()))
3750 return DAG.getSetCC(VT, N0.getOperand(0),
3751 DAG.getConstant(0, N0.getValueType()), Cond);
3753 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3754 // (Z-X) == X --> Z == X<<1
3755 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3757 DAG.getConstant(1,TLI.getShiftAmountTy()));
3758 AddToWorkList(SH.Val);
3759 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3764 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3765 N1.getOpcode() == ISD::XOR) {
3766 // Simplify X == (X+Z) --> Z == 0
3767 if (N1.getOperand(0) == N0) {
3768 return DAG.getSetCC(VT, N1.getOperand(1),
3769 DAG.getConstant(0, N1.getValueType()), Cond);
3770 } else if (N1.getOperand(1) == N0) {
3771 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
3772 return DAG.getSetCC(VT, N1.getOperand(0),
3773 DAG.getConstant(0, N1.getValueType()), Cond);
3775 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3776 // X == (Z-X) --> X<<1 == Z
3777 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3778 DAG.getConstant(1,TLI.getShiftAmountTy()));
3779 AddToWorkList(SH.Val);
3780 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3786 // Fold away ALL boolean setcc's.
3788 if (N0.getValueType() == MVT::i1 && foldBooleans) {
3790 default: assert(0 && "Unknown integer setcc!");
3791 case ISD::SETEQ: // X == Y -> (X^Y)^1
3792 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3793 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
3794 AddToWorkList(Temp.Val);
3796 case ISD::SETNE: // X != Y --> (X^Y)
3797 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3799 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3800 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3801 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3802 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
3803 AddToWorkList(Temp.Val);
3805 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3806 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3807 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3808 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
3809 AddToWorkList(Temp.Val);
3811 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3812 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3813 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3814 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
3815 AddToWorkList(Temp.Val);
3817 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3818 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3819 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3820 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3823 if (VT != MVT::i1) {
3824 AddToWorkList(N0.Val);
3825 // FIXME: If running after legalize, we probably can't do this.
3826 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3831 // Could not fold it.
3835 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3836 /// return a DAG expression to select that will generate the same value by
3837 /// multiplying by a magic number. See:
3838 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3839 SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
3840 std::vector<SDNode*> Built;
3841 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3843 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
3849 /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3850 /// return a DAG expression to select that will generate the same value by
3851 /// multiplying by a magic number. See:
3852 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3853 SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3854 std::vector<SDNode*> Built;
3855 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
3857 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
3863 // SelectionDAG::Combine - This is the entry point for the file.
3865 void SelectionDAG::Combine(bool RunningAfterLegalize) {
3866 /// run - This is the main entry point to this class.
3868 DAGCombiner(*this).Run(RunningAfterLegalize);