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"
46 static Statistic<> NodesCombined ("dagcombiner",
47 "Number of dag nodes combined");
50 CombinerAA("combiner-alias-analysis", cl::Hidden,
51 cl::desc("Turn on alias analysis turning testing"));
53 //------------------------------ DAGCombiner ---------------------------------//
55 class VISIBILITY_HIDDEN DAGCombiner {
60 // Worklist of all of the nodes that need to be simplified.
61 std::vector<SDNode*> WorkList;
63 /// AddUsersToWorkList - When an instruction is simplified, add all users of
64 /// the instruction to the work lists because they might get more simplified
67 void AddUsersToWorkList(SDNode *N) {
68 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
73 /// removeFromWorkList - remove all instances of N from the worklist.
75 void removeFromWorkList(SDNode *N) {
76 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
81 /// AddToWorkList - Add to the work list making sure it's instance is at the
82 /// the back (next to be processed.)
83 void AddToWorkList(SDNode *N) {
84 removeFromWorkList(N);
85 WorkList.push_back(N);
88 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) {
89 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
91 DEBUG(std::cerr << "\nReplacing.1 "; N->dump();
92 std::cerr << "\nWith: "; To[0].Val->dump(&DAG);
93 std::cerr << " and " << NumTo-1 << " other values\n");
94 std::vector<SDNode*> NowDead;
95 DAG.ReplaceAllUsesWith(N, To, &NowDead);
97 // Push the new nodes and any users onto the worklist
98 for (unsigned i = 0, e = NumTo; i != e; ++i) {
99 AddToWorkList(To[i].Val);
100 AddUsersToWorkList(To[i].Val);
103 // Nodes can be reintroduced into the worklist. Make sure we do not
104 // process a node that has been replaced.
105 removeFromWorkList(N);
106 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
107 removeFromWorkList(NowDead[i]);
109 // Finally, since the node is now dead, remove it from the graph.
111 return SDOperand(N, 0);
114 SDOperand CombineTo(SDNode *N, SDOperand Res) {
115 return CombineTo(N, &Res, 1);
118 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
119 SDOperand To[] = { Res0, Res1 };
120 return CombineTo(N, To, 2);
124 /// SimplifyDemandedBits - Check the specified integer node value to see if
125 /// it can be simplified or if things it uses can be simplified by bit
126 /// propagation. If so, return true.
127 bool SimplifyDemandedBits(SDOperand Op) {
128 TargetLowering::TargetLoweringOpt TLO(DAG);
129 uint64_t KnownZero, KnownOne;
130 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
131 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
135 AddToWorkList(Op.Val);
137 // Replace the old value with the new one.
139 DEBUG(std::cerr << "\nReplacing.2 "; TLO.Old.Val->dump();
140 std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG);
143 std::vector<SDNode*> NowDead;
144 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
146 // Push the new node and any (possibly new) users onto the worklist.
147 AddToWorkList(TLO.New.Val);
148 AddUsersToWorkList(TLO.New.Val);
150 // Nodes can end up on the worklist more than once. Make sure we do
151 // not process a node that has been replaced.
152 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
153 removeFromWorkList(NowDead[i]);
155 // Finally, if the node is now dead, remove it from the graph. The node
156 // may not be dead if the replacement process recursively simplified to
157 // something else needing this node.
158 if (TLO.Old.Val->use_empty()) {
159 removeFromWorkList(TLO.Old.Val);
160 DAG.DeleteNode(TLO.Old.Val);
165 /// visit - call the node-specific routine that knows how to fold each
166 /// particular type of node.
167 SDOperand visit(SDNode *N);
169 // Visitation implementation - Implement dag node combining for different
170 // node types. The semantics are as follows:
172 // SDOperand.Val == 0 - No change was made
173 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
174 // otherwise - N should be replaced by the returned Operand.
176 SDOperand visitTokenFactor(SDNode *N);
177 SDOperand visitADD(SDNode *N);
178 SDOperand visitSUB(SDNode *N);
179 SDOperand visitMUL(SDNode *N);
180 SDOperand visitSDIV(SDNode *N);
181 SDOperand visitUDIV(SDNode *N);
182 SDOperand visitSREM(SDNode *N);
183 SDOperand visitUREM(SDNode *N);
184 SDOperand visitMULHU(SDNode *N);
185 SDOperand visitMULHS(SDNode *N);
186 SDOperand visitAND(SDNode *N);
187 SDOperand visitOR(SDNode *N);
188 SDOperand visitXOR(SDNode *N);
189 SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp);
190 SDOperand visitSHL(SDNode *N);
191 SDOperand visitSRA(SDNode *N);
192 SDOperand visitSRL(SDNode *N);
193 SDOperand visitCTLZ(SDNode *N);
194 SDOperand visitCTTZ(SDNode *N);
195 SDOperand visitCTPOP(SDNode *N);
196 SDOperand visitSELECT(SDNode *N);
197 SDOperand visitSELECT_CC(SDNode *N);
198 SDOperand visitSETCC(SDNode *N);
199 SDOperand visitSIGN_EXTEND(SDNode *N);
200 SDOperand visitZERO_EXTEND(SDNode *N);
201 SDOperand visitANY_EXTEND(SDNode *N);
202 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
203 SDOperand visitTRUNCATE(SDNode *N);
204 SDOperand visitBIT_CONVERT(SDNode *N);
205 SDOperand visitVBIT_CONVERT(SDNode *N);
206 SDOperand visitFADD(SDNode *N);
207 SDOperand visitFSUB(SDNode *N);
208 SDOperand visitFMUL(SDNode *N);
209 SDOperand visitFDIV(SDNode *N);
210 SDOperand visitFREM(SDNode *N);
211 SDOperand visitFCOPYSIGN(SDNode *N);
212 SDOperand visitSINT_TO_FP(SDNode *N);
213 SDOperand visitUINT_TO_FP(SDNode *N);
214 SDOperand visitFP_TO_SINT(SDNode *N);
215 SDOperand visitFP_TO_UINT(SDNode *N);
216 SDOperand visitFP_ROUND(SDNode *N);
217 SDOperand visitFP_ROUND_INREG(SDNode *N);
218 SDOperand visitFP_EXTEND(SDNode *N);
219 SDOperand visitFNEG(SDNode *N);
220 SDOperand visitFABS(SDNode *N);
221 SDOperand visitBRCOND(SDNode *N);
222 SDOperand visitBR_CC(SDNode *N);
223 SDOperand visitLOAD(SDNode *N);
224 SDOperand visitSTORE(SDNode *N);
225 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
226 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
227 SDOperand visitVBUILD_VECTOR(SDNode *N);
228 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
229 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
231 SDOperand XformToShuffleWithZero(SDNode *N);
232 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
234 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
235 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
236 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
237 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
238 SDOperand N3, ISD::CondCode CC);
239 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
240 ISD::CondCode Cond, bool foldBooleans = true);
241 SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType);
242 SDOperand BuildSDIV(SDNode *N);
243 SDOperand BuildUDIV(SDNode *N);
244 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
246 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
247 /// looking for aliasing nodes and adding them to the Aliases vector.
248 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
249 SmallVector<SDOperand, 8> &Aliases);
251 /// FindAliasInfo - Extracts the relevant alias information from the memory
252 /// node. Returns true if the operand was a load.
253 bool FindAliasInfo(SDNode *N,
254 SDOperand &Ptr, int64_t &Size, const Value *&SrcValue);
256 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
257 /// looking for a better chain (aliasing node.)
258 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
261 DAGCombiner(SelectionDAG &D)
262 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
264 /// Run - runs the dag combiner on all nodes in the work list
265 void Run(bool RunningAfterLegalize);
269 //===----------------------------------------------------------------------===//
270 // TargetLowering::DAGCombinerInfo implementation
271 //===----------------------------------------------------------------------===//
273 void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
274 ((DAGCombiner*)DC)->AddToWorkList(N);
277 SDOperand TargetLowering::DAGCombinerInfo::
278 CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
279 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
282 SDOperand TargetLowering::DAGCombinerInfo::
283 CombineTo(SDNode *N, SDOperand Res) {
284 return ((DAGCombiner*)DC)->CombineTo(N, Res);
288 SDOperand TargetLowering::DAGCombinerInfo::
289 CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
290 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
296 //===----------------------------------------------------------------------===//
299 // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
300 // that selects between the values 1 and 0, making it equivalent to a setcc.
301 // Also, set the incoming LHS, RHS, and CC references to the appropriate
302 // nodes based on the type of node we are checking. This simplifies life a
303 // bit for the callers.
304 static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
306 if (N.getOpcode() == ISD::SETCC) {
307 LHS = N.getOperand(0);
308 RHS = N.getOperand(1);
309 CC = N.getOperand(2);
312 if (N.getOpcode() == ISD::SELECT_CC &&
313 N.getOperand(2).getOpcode() == ISD::Constant &&
314 N.getOperand(3).getOpcode() == ISD::Constant &&
315 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
316 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
317 LHS = N.getOperand(0);
318 RHS = N.getOperand(1);
319 CC = N.getOperand(4);
325 // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
326 // one use. If this is true, it allows the users to invert the operation for
327 // free when it is profitable to do so.
328 static bool isOneUseSetCC(SDOperand N) {
329 SDOperand N0, N1, N2;
330 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
335 SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
336 MVT::ValueType VT = N0.getValueType();
337 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
338 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
339 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
340 if (isa<ConstantSDNode>(N1)) {
341 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
342 AddToWorkList(OpNode.Val);
343 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
344 } else if (N0.hasOneUse()) {
345 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
346 AddToWorkList(OpNode.Val);
347 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
350 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
351 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
352 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
353 if (isa<ConstantSDNode>(N0)) {
354 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
355 AddToWorkList(OpNode.Val);
356 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
357 } else if (N1.hasOneUse()) {
358 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
359 AddToWorkList(OpNode.Val);
360 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
366 void DAGCombiner::Run(bool RunningAfterLegalize) {
367 // set the instance variable, so that the various visit routines may use it.
368 AfterLegalize = RunningAfterLegalize;
370 // Add all the dag nodes to the worklist.
371 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
372 E = DAG.allnodes_end(); I != E; ++I)
373 WorkList.push_back(I);
375 // Create a dummy node (which is not added to allnodes), that adds a reference
376 // to the root node, preventing it from being deleted, and tracking any
377 // changes of the root.
378 HandleSDNode Dummy(DAG.getRoot());
381 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
382 TargetLowering::DAGCombinerInfo
383 DagCombineInfo(DAG, !RunningAfterLegalize, this);
385 // while the worklist isn't empty, inspect the node on the end of it and
386 // try and combine it.
387 while (!WorkList.empty()) {
388 SDNode *N = WorkList.back();
391 // If N has no uses, it is dead. Make sure to revisit all N's operands once
392 // N is deleted from the DAG, since they too may now be dead or may have a
393 // reduced number of uses, allowing other xforms.
394 if (N->use_empty() && N != &Dummy) {
395 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
396 AddToWorkList(N->getOperand(i).Val);
402 SDOperand RV = visit(N);
404 // If nothing happened, try a target-specific DAG combine.
406 assert(N->getOpcode() != ISD::DELETED_NODE &&
407 "Node was deleted but visit returned NULL!");
408 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
409 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
410 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
415 // If we get back the same node we passed in, rather than a new node or
416 // zero, we know that the node must have defined multiple values and
417 // CombineTo was used. Since CombineTo takes care of the worklist
418 // mechanics for us, we have no work to do in this case.
420 assert(N->getOpcode() != ISD::DELETED_NODE &&
421 RV.Val->getOpcode() != ISD::DELETED_NODE &&
422 "Node was deleted but visit returned new node!");
424 DEBUG(std::cerr << "\nReplacing.3 "; N->dump();
425 std::cerr << "\nWith: "; RV.Val->dump(&DAG);
427 std::vector<SDNode*> NowDead;
428 if (N->getNumValues() == RV.Val->getNumValues())
429 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
431 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
433 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
436 // Push the new node and any users onto the worklist
437 AddToWorkList(RV.Val);
438 AddUsersToWorkList(RV.Val);
440 // Nodes can be reintroduced into the worklist. Make sure we do not
441 // process a node that has been replaced.
442 removeFromWorkList(N);
443 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
444 removeFromWorkList(NowDead[i]);
446 // Finally, since the node is now dead, remove it from the graph.
452 // If the root changed (e.g. it was a dead load, update the root).
453 DAG.setRoot(Dummy.getValue());
456 SDOperand DAGCombiner::visit(SDNode *N) {
457 switch(N->getOpcode()) {
459 case ISD::TokenFactor: return visitTokenFactor(N);
460 case ISD::ADD: return visitADD(N);
461 case ISD::SUB: return visitSUB(N);
462 case ISD::MUL: return visitMUL(N);
463 case ISD::SDIV: return visitSDIV(N);
464 case ISD::UDIV: return visitUDIV(N);
465 case ISD::SREM: return visitSREM(N);
466 case ISD::UREM: return visitUREM(N);
467 case ISD::MULHU: return visitMULHU(N);
468 case ISD::MULHS: return visitMULHS(N);
469 case ISD::AND: return visitAND(N);
470 case ISD::OR: return visitOR(N);
471 case ISD::XOR: return visitXOR(N);
472 case ISD::SHL: return visitSHL(N);
473 case ISD::SRA: return visitSRA(N);
474 case ISD::SRL: return visitSRL(N);
475 case ISD::CTLZ: return visitCTLZ(N);
476 case ISD::CTTZ: return visitCTTZ(N);
477 case ISD::CTPOP: return visitCTPOP(N);
478 case ISD::SELECT: return visitSELECT(N);
479 case ISD::SELECT_CC: return visitSELECT_CC(N);
480 case ISD::SETCC: return visitSETCC(N);
481 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
482 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
483 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
484 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
485 case ISD::TRUNCATE: return visitTRUNCATE(N);
486 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
487 case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N);
488 case ISD::FADD: return visitFADD(N);
489 case ISD::FSUB: return visitFSUB(N);
490 case ISD::FMUL: return visitFMUL(N);
491 case ISD::FDIV: return visitFDIV(N);
492 case ISD::FREM: return visitFREM(N);
493 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
494 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
495 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
496 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
497 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
498 case ISD::FP_ROUND: return visitFP_ROUND(N);
499 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
500 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
501 case ISD::FNEG: return visitFNEG(N);
502 case ISD::FABS: return visitFABS(N);
503 case ISD::BRCOND: return visitBRCOND(N);
504 case ISD::BR_CC: return visitBR_CC(N);
505 case ISD::LOAD: return visitLOAD(N);
506 // FIXME - Switch over after StoreSDNode comes online.
507 case ISD::TRUNCSTORE: // Fall thru
508 case ISD::STORE: return visitSTORE(N);
509 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
510 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
511 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
512 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
513 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
514 case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD);
515 case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB);
516 case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL);
517 case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV);
518 case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV);
519 case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND);
520 case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR);
521 case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR);
526 /// getInputChainForNode - Given a node, return its input chain if it has one,
527 /// otherwise return a null sd operand.
528 static SDOperand getInputChainForNode(SDNode *N) {
529 if (unsigned NumOps = N->getNumOperands()) {
530 if (N->getOperand(0).getValueType() == MVT::Other)
531 return N->getOperand(0);
532 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
533 return N->getOperand(NumOps-1);
534 for (unsigned i = 1; i < NumOps-1; ++i)
535 if (N->getOperand(i).getValueType() == MVT::Other)
536 return N->getOperand(i);
538 return SDOperand(0, 0);
541 SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
542 // If N has two operands, where one has an input chain equal to the other,
543 // the 'other' chain is redundant.
544 if (N->getNumOperands() == 2) {
545 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
546 return N->getOperand(0);
547 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
548 return N->getOperand(1);
552 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
553 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
554 bool Changed = false; // If we should replace this token factor.
556 // Start out with this token factor.
559 // Iterate through token factors. The TFs grows when new token factors are
561 for (unsigned i = 0; i < TFs.size(); ++i) {
564 // Check each of the operands.
565 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
566 SDOperand Op = TF->getOperand(i);
568 switch (Op.getOpcode()) {
569 case ISD::EntryToken:
570 // Entry tokens don't need to be added to the list. They are
575 case ISD::TokenFactor:
576 if ((CombinerAA || Op.hasOneUse()) &&
577 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
578 // Queue up for processing.
579 TFs.push_back(Op.Val);
580 // Clean up in case the token factor is removed.
581 AddToWorkList(Op.Val);
588 // Only add if not there prior.
589 if (std::find(Ops.begin(), Ops.end(), Op) == Ops.end())
598 // If we've change things around then replace token factor.
600 if (Ops.size() == 0) {
601 // The entry token is the only possible outcome.
602 Result = DAG.getEntryNode();
604 // New and improved token factor.
605 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
612 SDOperand DAGCombiner::visitADD(SDNode *N) {
613 SDOperand N0 = N->getOperand(0);
614 SDOperand N1 = N->getOperand(1);
615 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
616 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
617 MVT::ValueType VT = N0.getValueType();
619 // fold (add c1, c2) -> c1+c2
621 return DAG.getNode(ISD::ADD, VT, N0, N1);
622 // canonicalize constant to RHS
624 return DAG.getNode(ISD::ADD, VT, N1, N0);
625 // fold (add x, 0) -> x
626 if (N1C && N1C->isNullValue())
628 // fold ((c1-A)+c2) -> (c1+c2)-A
629 if (N1C && N0.getOpcode() == ISD::SUB)
630 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
631 return DAG.getNode(ISD::SUB, VT,
632 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
635 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
638 // fold ((0-A) + B) -> B-A
639 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
640 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
641 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
642 // fold (A + (0-B)) -> A-B
643 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
644 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
645 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
646 // fold (A+(B-A)) -> B
647 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
648 return N1.getOperand(0);
650 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
651 return SDOperand(N, 0);
653 // fold (a+b) -> (a|b) iff a and b share no bits.
654 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
655 uint64_t LHSZero, LHSOne;
656 uint64_t RHSZero, RHSOne;
657 uint64_t Mask = MVT::getIntVTBitMask(VT);
658 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
660 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
662 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
663 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
664 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
665 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
666 return DAG.getNode(ISD::OR, VT, N0, N1);
673 SDOperand DAGCombiner::visitSUB(SDNode *N) {
674 SDOperand N0 = N->getOperand(0);
675 SDOperand N1 = N->getOperand(1);
676 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
677 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
678 MVT::ValueType VT = N0.getValueType();
680 // fold (sub x, x) -> 0
682 return DAG.getConstant(0, N->getValueType(0));
683 // fold (sub c1, c2) -> c1-c2
685 return DAG.getNode(ISD::SUB, VT, N0, N1);
686 // fold (sub x, c) -> (add x, -c)
688 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
690 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
691 return N0.getOperand(1);
693 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
694 return N0.getOperand(0);
698 SDOperand DAGCombiner::visitMUL(SDNode *N) {
699 SDOperand N0 = N->getOperand(0);
700 SDOperand N1 = N->getOperand(1);
701 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
702 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
703 MVT::ValueType VT = N0.getValueType();
705 // fold (mul c1, c2) -> c1*c2
707 return DAG.getNode(ISD::MUL, VT, N0, N1);
708 // canonicalize constant to RHS
710 return DAG.getNode(ISD::MUL, VT, N1, N0);
711 // fold (mul x, 0) -> 0
712 if (N1C && N1C->isNullValue())
714 // fold (mul x, -1) -> 0-x
715 if (N1C && N1C->isAllOnesValue())
716 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
717 // fold (mul x, (1 << c)) -> x << c
718 if (N1C && isPowerOf2_64(N1C->getValue()))
719 return DAG.getNode(ISD::SHL, VT, N0,
720 DAG.getConstant(Log2_64(N1C->getValue()),
721 TLI.getShiftAmountTy()));
722 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
723 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
724 // FIXME: If the input is something that is easily negated (e.g. a
725 // single-use add), we should put the negate there.
726 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
727 DAG.getNode(ISD::SHL, VT, N0,
728 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
729 TLI.getShiftAmountTy())));
732 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
733 if (N1C && N0.getOpcode() == ISD::SHL &&
734 isa<ConstantSDNode>(N0.getOperand(1))) {
735 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
736 AddToWorkList(C3.Val);
737 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
740 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
743 SDOperand Sh(0,0), Y(0,0);
744 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
745 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
746 N0.Val->hasOneUse()) {
748 } else if (N1.getOpcode() == ISD::SHL &&
749 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
753 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
754 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
757 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
758 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
759 isa<ConstantSDNode>(N0.getOperand(1))) {
760 return DAG.getNode(ISD::ADD, VT,
761 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
762 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
766 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
772 SDOperand DAGCombiner::visitSDIV(SDNode *N) {
773 SDOperand N0 = N->getOperand(0);
774 SDOperand N1 = N->getOperand(1);
775 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
776 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
777 MVT::ValueType VT = N->getValueType(0);
779 // fold (sdiv c1, c2) -> c1/c2
780 if (N0C && N1C && !N1C->isNullValue())
781 return DAG.getNode(ISD::SDIV, VT, N0, N1);
782 // fold (sdiv X, 1) -> X
783 if (N1C && N1C->getSignExtended() == 1LL)
785 // fold (sdiv X, -1) -> 0-X
786 if (N1C && N1C->isAllOnesValue())
787 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
788 // If we know the sign bits of both operands are zero, strength reduce to a
789 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
790 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
791 if (TLI.MaskedValueIsZero(N1, SignBit) &&
792 TLI.MaskedValueIsZero(N0, SignBit))
793 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
794 // fold (sdiv X, pow2) -> simple ops after legalize
795 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
796 (isPowerOf2_64(N1C->getSignExtended()) ||
797 isPowerOf2_64(-N1C->getSignExtended()))) {
798 // If dividing by powers of two is cheap, then don't perform the following
800 if (TLI.isPow2DivCheap())
802 int64_t pow2 = N1C->getSignExtended();
803 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
804 unsigned lg2 = Log2_64(abs2);
805 // Splat the sign bit into the register
806 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
807 DAG.getConstant(MVT::getSizeInBits(VT)-1,
808 TLI.getShiftAmountTy()));
809 AddToWorkList(SGN.Val);
810 // Add (N0 < 0) ? abs2 - 1 : 0;
811 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
812 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
813 TLI.getShiftAmountTy()));
814 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
815 AddToWorkList(SRL.Val);
816 AddToWorkList(ADD.Val); // Divide by pow2
817 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
818 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
819 // If we're dividing by a positive value, we're done. Otherwise, we must
820 // negate the result.
823 AddToWorkList(SRA.Val);
824 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
826 // if integer divide is expensive and we satisfy the requirements, emit an
827 // alternate sequence.
828 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
829 !TLI.isIntDivCheap()) {
830 SDOperand Op = BuildSDIV(N);
831 if (Op.Val) return Op;
836 SDOperand DAGCombiner::visitUDIV(SDNode *N) {
837 SDOperand N0 = N->getOperand(0);
838 SDOperand N1 = N->getOperand(1);
839 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
840 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
841 MVT::ValueType VT = N->getValueType(0);
843 // fold (udiv c1, c2) -> c1/c2
844 if (N0C && N1C && !N1C->isNullValue())
845 return DAG.getNode(ISD::UDIV, VT, N0, N1);
846 // fold (udiv x, (1 << c)) -> x >>u c
847 if (N1C && isPowerOf2_64(N1C->getValue()))
848 return DAG.getNode(ISD::SRL, VT, N0,
849 DAG.getConstant(Log2_64(N1C->getValue()),
850 TLI.getShiftAmountTy()));
851 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
852 if (N1.getOpcode() == ISD::SHL) {
853 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
854 if (isPowerOf2_64(SHC->getValue())) {
855 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
856 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
857 DAG.getConstant(Log2_64(SHC->getValue()),
859 AddToWorkList(Add.Val);
860 return DAG.getNode(ISD::SRL, VT, N0, Add);
864 // fold (udiv x, c) -> alternate
865 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
866 SDOperand Op = BuildUDIV(N);
867 if (Op.Val) return Op;
872 SDOperand DAGCombiner::visitSREM(SDNode *N) {
873 SDOperand N0 = N->getOperand(0);
874 SDOperand N1 = N->getOperand(1);
875 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
876 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
877 MVT::ValueType VT = N->getValueType(0);
879 // fold (srem c1, c2) -> c1%c2
880 if (N0C && N1C && !N1C->isNullValue())
881 return DAG.getNode(ISD::SREM, VT, N0, N1);
882 // If we know the sign bits of both operands are zero, strength reduce to a
883 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
884 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
885 if (TLI.MaskedValueIsZero(N1, SignBit) &&
886 TLI.MaskedValueIsZero(N0, SignBit))
887 return DAG.getNode(ISD::UREM, VT, N0, N1);
891 SDOperand DAGCombiner::visitUREM(SDNode *N) {
892 SDOperand N0 = N->getOperand(0);
893 SDOperand N1 = N->getOperand(1);
894 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
895 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
896 MVT::ValueType VT = N->getValueType(0);
898 // fold (urem c1, c2) -> c1%c2
899 if (N0C && N1C && !N1C->isNullValue())
900 return DAG.getNode(ISD::UREM, VT, N0, N1);
901 // fold (urem x, pow2) -> (and x, pow2-1)
902 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
903 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
904 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
905 if (N1.getOpcode() == ISD::SHL) {
906 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
907 if (isPowerOf2_64(SHC->getValue())) {
908 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
909 AddToWorkList(Add.Val);
910 return DAG.getNode(ISD::AND, VT, N0, Add);
917 SDOperand DAGCombiner::visitMULHS(SDNode *N) {
918 SDOperand N0 = N->getOperand(0);
919 SDOperand N1 = N->getOperand(1);
920 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
922 // fold (mulhs x, 0) -> 0
923 if (N1C && N1C->isNullValue())
925 // fold (mulhs x, 1) -> (sra x, size(x)-1)
926 if (N1C && N1C->getValue() == 1)
927 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
928 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
929 TLI.getShiftAmountTy()));
933 SDOperand DAGCombiner::visitMULHU(SDNode *N) {
934 SDOperand N0 = N->getOperand(0);
935 SDOperand N1 = N->getOperand(1);
936 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
938 // fold (mulhu x, 0) -> 0
939 if (N1C && N1C->isNullValue())
941 // fold (mulhu x, 1) -> 0
942 if (N1C && N1C->getValue() == 1)
943 return DAG.getConstant(0, N0.getValueType());
947 /// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
948 /// two operands of the same opcode, try to simplify it.
949 SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
950 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
951 MVT::ValueType VT = N0.getValueType();
952 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
954 // For each of OP in AND/OR/XOR:
955 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
956 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
957 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
958 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
959 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
960 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
961 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
962 SDOperand ORNode = DAG.getNode(N->getOpcode(),
963 N0.getOperand(0).getValueType(),
964 N0.getOperand(0), N1.getOperand(0));
965 AddToWorkList(ORNode.Val);
966 return DAG.getNode(N0.getOpcode(), VT, ORNode);
969 // For each of OP in SHL/SRL/SRA/AND...
970 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
971 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
972 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
973 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
974 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
975 N0.getOperand(1) == N1.getOperand(1)) {
976 SDOperand ORNode = DAG.getNode(N->getOpcode(),
977 N0.getOperand(0).getValueType(),
978 N0.getOperand(0), N1.getOperand(0));
979 AddToWorkList(ORNode.Val);
980 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
986 SDOperand DAGCombiner::visitAND(SDNode *N) {
987 SDOperand N0 = N->getOperand(0);
988 SDOperand N1 = N->getOperand(1);
989 SDOperand LL, LR, RL, RR, CC0, CC1;
990 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
991 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
992 MVT::ValueType VT = N1.getValueType();
993 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
995 // fold (and c1, c2) -> c1&c2
997 return DAG.getNode(ISD::AND, VT, N0, N1);
998 // canonicalize constant to RHS
1000 return DAG.getNode(ISD::AND, VT, N1, N0);
1001 // fold (and x, -1) -> x
1002 if (N1C && N1C->isAllOnesValue())
1004 // if (and x, c) is known to be zero, return 0
1005 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
1006 return DAG.getConstant(0, VT);
1008 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1011 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
1012 if (N1C && N0.getOpcode() == ISD::OR)
1013 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1014 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
1016 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1017 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1018 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
1019 if (TLI.MaskedValueIsZero(N0.getOperand(0),
1020 ~N1C->getValue() & InMask)) {
1021 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1024 // Replace uses of the AND with uses of the Zero extend node.
1027 // We actually want to replace all uses of the any_extend with the
1028 // zero_extend, to avoid duplicating things. This will later cause this
1029 // AND to be folded.
1030 CombineTo(N0.Val, Zext);
1031 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1034 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1035 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1036 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1037 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1039 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1040 MVT::isInteger(LL.getValueType())) {
1041 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1042 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1043 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1044 AddToWorkList(ORNode.Val);
1045 return DAG.getSetCC(VT, ORNode, LR, Op1);
1047 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1048 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1049 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1050 AddToWorkList(ANDNode.Val);
1051 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1053 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1054 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1055 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1056 AddToWorkList(ORNode.Val);
1057 return DAG.getSetCC(VT, ORNode, LR, Op1);
1060 // canonicalize equivalent to ll == rl
1061 if (LL == RR && LR == RL) {
1062 Op1 = ISD::getSetCCSwappedOperands(Op1);
1065 if (LL == RL && LR == RR) {
1066 bool isInteger = MVT::isInteger(LL.getValueType());
1067 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1068 if (Result != ISD::SETCC_INVALID)
1069 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1073 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1074 if (N0.getOpcode() == N1.getOpcode()) {
1075 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1076 if (Tmp.Val) return Tmp;
1079 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1080 // fold (and (sra)) -> (and (srl)) when possible.
1081 if (!MVT::isVector(VT) &&
1082 SimplifyDemandedBits(SDOperand(N, 0)))
1083 return SDOperand(N, 0);
1084 // fold (zext_inreg (extload x)) -> (zextload x)
1085 if (ISD::isEXTLoad(N0.Val)) {
1086 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1087 MVT::ValueType EVT = LN0->getLoadedVT();
1088 // If we zero all the possible extended bits, then we can turn this into
1089 // a zextload if we are running before legalize or the operation is legal.
1090 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
1091 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1092 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1093 LN0->getBasePtr(), LN0->getSrcValue(),
1094 LN0->getSrcValueOffset(), EVT);
1096 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1097 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1100 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
1101 if (ISD::isSEXTLoad(N0.Val) && N0.hasOneUse()) {
1102 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1103 MVT::ValueType EVT = LN0->getLoadedVT();
1104 // If we zero all the possible extended bits, then we can turn this into
1105 // a zextload if we are running before legalize or the operation is legal.
1106 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
1107 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1108 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1109 LN0->getBasePtr(), LN0->getSrcValue(),
1110 LN0->getSrcValueOffset(), EVT);
1112 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
1113 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1117 // fold (and (load x), 255) -> (zextload x, i8)
1118 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1119 if (N1C && N0.getOpcode() == ISD::LOAD) {
1120 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1121 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
1123 MVT::ValueType EVT, LoadedVT;
1124 if (N1C->getValue() == 255)
1126 else if (N1C->getValue() == 65535)
1128 else if (N1C->getValue() == ~0U)
1133 LoadedVT = LN0->getLoadedVT();
1134 if (EVT != MVT::Other && LoadedVT > EVT &&
1135 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1136 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1137 // For big endian targets, we need to add an offset to the pointer to
1138 // load the correct bytes. For little endian systems, we merely need to
1139 // read fewer bytes from the same pointer.
1141 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1142 SDOperand NewPtr = LN0->getBasePtr();
1143 if (!TLI.isLittleEndian())
1144 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1145 DAG.getConstant(PtrOff, PtrType));
1146 AddToWorkList(NewPtr.Val);
1148 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1149 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT);
1151 CombineTo(N0.Val, Load, Load.getValue(1));
1152 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1160 SDOperand DAGCombiner::visitOR(SDNode *N) {
1161 SDOperand N0 = N->getOperand(0);
1162 SDOperand N1 = N->getOperand(1);
1163 SDOperand LL, LR, RL, RR, CC0, CC1;
1164 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1165 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1166 MVT::ValueType VT = N1.getValueType();
1167 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1169 // fold (or c1, c2) -> c1|c2
1171 return DAG.getNode(ISD::OR, VT, N0, N1);
1172 // canonicalize constant to RHS
1174 return DAG.getNode(ISD::OR, VT, N1, N0);
1175 // fold (or x, 0) -> x
1176 if (N1C && N1C->isNullValue())
1178 // fold (or x, -1) -> -1
1179 if (N1C && N1C->isAllOnesValue())
1181 // fold (or x, c) -> c iff (x & ~c) == 0
1183 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
1186 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1189 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1190 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
1191 isa<ConstantSDNode>(N0.getOperand(1))) {
1192 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1193 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1195 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
1197 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1198 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1199 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1200 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1202 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1203 MVT::isInteger(LL.getValueType())) {
1204 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1205 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1206 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1207 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1208 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1209 AddToWorkList(ORNode.Val);
1210 return DAG.getSetCC(VT, ORNode, LR, Op1);
1212 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1213 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1214 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1215 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1216 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1217 AddToWorkList(ANDNode.Val);
1218 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1221 // canonicalize equivalent to ll == rl
1222 if (LL == RR && LR == RL) {
1223 Op1 = ISD::getSetCCSwappedOperands(Op1);
1226 if (LL == RL && LR == RR) {
1227 bool isInteger = MVT::isInteger(LL.getValueType());
1228 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1229 if (Result != ISD::SETCC_INVALID)
1230 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1234 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1235 if (N0.getOpcode() == N1.getOpcode()) {
1236 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1237 if (Tmp.Val) return Tmp;
1240 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1241 if (N0.getOpcode() == ISD::AND &&
1242 N1.getOpcode() == ISD::AND &&
1243 N0.getOperand(1).getOpcode() == ISD::Constant &&
1244 N1.getOperand(1).getOpcode() == ISD::Constant &&
1245 // Don't increase # computations.
1246 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1247 // We can only do this xform if we know that bits from X that are set in C2
1248 // but not in C1 are already zero. Likewise for Y.
1249 uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1250 uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1252 if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1253 TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
1254 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1255 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1260 // See if this is some rotate idiom.
1261 if (SDNode *Rot = MatchRotate(N0, N1))
1262 return SDOperand(Rot, 0);
1268 /// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1269 static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1270 if (Op.getOpcode() == ISD::AND) {
1271 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1272 Mask = Op.getOperand(1);
1273 Op = Op.getOperand(0);
1279 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1287 // MatchRotate - Handle an 'or' of two operands. If this is one of the many
1288 // idioms for rotate, and if the target supports rotation instructions, generate
1290 SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1291 // Must be a legal type. Expanded an promoted things won't work with rotates.
1292 MVT::ValueType VT = LHS.getValueType();
1293 if (!TLI.isTypeLegal(VT)) return 0;
1295 // The target must have at least one rotate flavor.
1296 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1297 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1298 if (!HasROTL && !HasROTR) return 0;
1300 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1301 SDOperand LHSShift; // The shift.
1302 SDOperand LHSMask; // AND value if any.
1303 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1304 return 0; // Not part of a rotate.
1306 SDOperand RHSShift; // The shift.
1307 SDOperand RHSMask; // AND value if any.
1308 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1309 return 0; // Not part of a rotate.
1311 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1312 return 0; // Not shifting the same value.
1314 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1315 return 0; // Shifts must disagree.
1317 // Canonicalize shl to left side in a shl/srl pair.
1318 if (RHSShift.getOpcode() == ISD::SHL) {
1319 std::swap(LHS, RHS);
1320 std::swap(LHSShift, RHSShift);
1321 std::swap(LHSMask , RHSMask );
1324 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1326 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1327 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
1328 if (LHSShift.getOperand(1).getOpcode() == ISD::Constant &&
1329 RHSShift.getOperand(1).getOpcode() == ISD::Constant) {
1330 uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue();
1331 uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue();
1332 if ((LShVal + RShVal) != OpSizeInBits)
1337 Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1338 LHSShift.getOperand(1));
1340 Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1341 RHSShift.getOperand(1));
1343 // If there is an AND of either shifted operand, apply it to the result.
1344 if (LHSMask.Val || RHSMask.Val) {
1345 uint64_t Mask = MVT::getIntVTBitMask(VT);
1348 uint64_t RHSBits = (1ULL << LShVal)-1;
1349 Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits;
1352 uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1);
1353 Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits;
1356 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
1362 // If there is a mask here, and we have a variable shift, we can't be sure
1363 // that we're masking out the right stuff.
1364 if (LHSMask.Val || RHSMask.Val)
1367 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1368 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
1369 if (RHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1370 LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) {
1371 if (ConstantSDNode *SUBC =
1372 dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) {
1373 if (SUBC->getValue() == OpSizeInBits)
1375 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1376 LHSShift.getOperand(1)).Val;
1378 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1379 LHSShift.getOperand(1)).Val;
1383 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1384 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
1385 if (LHSShift.getOperand(1).getOpcode() == ISD::SUB &&
1386 RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) {
1387 if (ConstantSDNode *SUBC =
1388 dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) {
1389 if (SUBC->getValue() == OpSizeInBits)
1391 return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0),
1392 LHSShift.getOperand(1)).Val;
1394 return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0),
1395 RHSShift.getOperand(1)).Val;
1403 SDOperand DAGCombiner::visitXOR(SDNode *N) {
1404 SDOperand N0 = N->getOperand(0);
1405 SDOperand N1 = N->getOperand(1);
1406 SDOperand LHS, RHS, CC;
1407 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1408 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1409 MVT::ValueType VT = N0.getValueType();
1411 // fold (xor c1, c2) -> c1^c2
1413 return DAG.getNode(ISD::XOR, VT, N0, N1);
1414 // canonicalize constant to RHS
1416 return DAG.getNode(ISD::XOR, VT, N1, N0);
1417 // fold (xor x, 0) -> x
1418 if (N1C && N1C->isNullValue())
1421 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1424 // fold !(x cc y) -> (x !cc y)
1425 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1426 bool isInt = MVT::isInteger(LHS.getValueType());
1427 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1429 if (N0.getOpcode() == ISD::SETCC)
1430 return DAG.getSetCC(VT, LHS, RHS, NotCC);
1431 if (N0.getOpcode() == ISD::SELECT_CC)
1432 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
1433 assert(0 && "Unhandled SetCC Equivalent!");
1436 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1437 if (N1C && N1C->getValue() == 1 &&
1438 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
1439 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
1440 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1441 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
1442 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1443 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
1444 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
1445 return DAG.getNode(NewOpcode, VT, LHS, RHS);
1448 // fold !(x or y) -> (!x and !y) iff x or y are constants
1449 if (N1C && N1C->isAllOnesValue() &&
1450 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
1451 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
1452 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1453 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
1454 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1455 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
1456 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
1457 return DAG.getNode(NewOpcode, VT, LHS, RHS);
1460 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1461 if (N1C && N0.getOpcode() == ISD::XOR) {
1462 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1463 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1465 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1466 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1468 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1469 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1471 // fold (xor x, x) -> 0
1473 if (!MVT::isVector(VT)) {
1474 return DAG.getConstant(0, VT);
1475 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1476 // Produce a vector of zeros.
1477 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1478 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1479 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
1483 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
1484 if (N0.getOpcode() == N1.getOpcode()) {
1485 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1486 if (Tmp.Val) return Tmp;
1489 // Simplify the expression using non-local knowledge.
1490 if (!MVT::isVector(VT) &&
1491 SimplifyDemandedBits(SDOperand(N, 0)))
1492 return SDOperand(N, 0);
1497 SDOperand DAGCombiner::visitSHL(SDNode *N) {
1498 SDOperand N0 = N->getOperand(0);
1499 SDOperand N1 = N->getOperand(1);
1500 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1501 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1502 MVT::ValueType VT = N0.getValueType();
1503 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1505 // fold (shl c1, c2) -> c1<<c2
1507 return DAG.getNode(ISD::SHL, VT, N0, N1);
1508 // fold (shl 0, x) -> 0
1509 if (N0C && N0C->isNullValue())
1511 // fold (shl x, c >= size(x)) -> undef
1512 if (N1C && N1C->getValue() >= OpSizeInBits)
1513 return DAG.getNode(ISD::UNDEF, VT);
1514 // fold (shl x, 0) -> x
1515 if (N1C && N1C->isNullValue())
1517 // if (shl x, c) is known to be zero, return 0
1518 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
1519 return DAG.getConstant(0, VT);
1520 if (SimplifyDemandedBits(SDOperand(N, 0)))
1521 return SDOperand(N, 0);
1522 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
1523 if (N1C && N0.getOpcode() == ISD::SHL &&
1524 N0.getOperand(1).getOpcode() == ISD::Constant) {
1525 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1526 uint64_t c2 = N1C->getValue();
1527 if (c1 + c2 > OpSizeInBits)
1528 return DAG.getConstant(0, VT);
1529 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
1530 DAG.getConstant(c1 + c2, N1.getValueType()));
1532 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1533 // (srl (and x, -1 << c1), c1-c2)
1534 if (N1C && N0.getOpcode() == ISD::SRL &&
1535 N0.getOperand(1).getOpcode() == ISD::Constant) {
1536 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1537 uint64_t c2 = N1C->getValue();
1538 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1539 DAG.getConstant(~0ULL << c1, VT));
1541 return DAG.getNode(ISD::SHL, VT, Mask,
1542 DAG.getConstant(c2-c1, N1.getValueType()));
1544 return DAG.getNode(ISD::SRL, VT, Mask,
1545 DAG.getConstant(c1-c2, N1.getValueType()));
1547 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
1548 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
1549 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1550 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1551 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1552 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1553 isa<ConstantSDNode>(N0.getOperand(1))) {
1554 return DAG.getNode(ISD::ADD, VT,
1555 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1556 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1561 SDOperand DAGCombiner::visitSRA(SDNode *N) {
1562 SDOperand N0 = N->getOperand(0);
1563 SDOperand N1 = N->getOperand(1);
1564 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1565 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1566 MVT::ValueType VT = N0.getValueType();
1568 // fold (sra c1, c2) -> c1>>c2
1570 return DAG.getNode(ISD::SRA, VT, N0, N1);
1571 // fold (sra 0, x) -> 0
1572 if (N0C && N0C->isNullValue())
1574 // fold (sra -1, x) -> -1
1575 if (N0C && N0C->isAllOnesValue())
1577 // fold (sra x, c >= size(x)) -> undef
1578 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
1579 return DAG.getNode(ISD::UNDEF, VT);
1580 // fold (sra x, 0) -> x
1581 if (N1C && N1C->isNullValue())
1583 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1585 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1586 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1589 default: EVT = MVT::Other; break;
1590 case 1: EVT = MVT::i1; break;
1591 case 8: EVT = MVT::i8; break;
1592 case 16: EVT = MVT::i16; break;
1593 case 32: EVT = MVT::i32; break;
1595 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1596 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1597 DAG.getValueType(EVT));
1600 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1601 if (N1C && N0.getOpcode() == ISD::SRA) {
1602 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1603 unsigned Sum = N1C->getValue() + C1->getValue();
1604 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1605 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1606 DAG.getConstant(Sum, N1C->getValueType(0)));
1610 // Simplify, based on bits shifted out of the LHS.
1611 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
1612 return SDOperand(N, 0);
1615 // If the sign bit is known to be zero, switch this to a SRL.
1616 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
1617 return DAG.getNode(ISD::SRL, VT, N0, N1);
1621 SDOperand DAGCombiner::visitSRL(SDNode *N) {
1622 SDOperand N0 = N->getOperand(0);
1623 SDOperand N1 = N->getOperand(1);
1624 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1625 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1626 MVT::ValueType VT = N0.getValueType();
1627 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1629 // fold (srl c1, c2) -> c1 >>u c2
1631 return DAG.getNode(ISD::SRL, VT, N0, N1);
1632 // fold (srl 0, x) -> 0
1633 if (N0C && N0C->isNullValue())
1635 // fold (srl x, c >= size(x)) -> undef
1636 if (N1C && N1C->getValue() >= OpSizeInBits)
1637 return DAG.getNode(ISD::UNDEF, VT);
1638 // fold (srl x, 0) -> x
1639 if (N1C && N1C->isNullValue())
1641 // if (srl x, c) is known to be zero, return 0
1642 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
1643 return DAG.getConstant(0, VT);
1644 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
1645 if (N1C && N0.getOpcode() == ISD::SRL &&
1646 N0.getOperand(1).getOpcode() == ISD::Constant) {
1647 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1648 uint64_t c2 = N1C->getValue();
1649 if (c1 + c2 > OpSizeInBits)
1650 return DAG.getConstant(0, VT);
1651 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
1652 DAG.getConstant(c1 + c2, N1.getValueType()));
1655 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
1656 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
1657 // Shifting in all undef bits?
1658 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
1659 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
1660 return DAG.getNode(ISD::UNDEF, VT);
1662 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
1663 AddToWorkList(SmallShift.Val);
1664 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
1667 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
1668 // bit, which is unmodified by sra.
1669 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
1670 if (N0.getOpcode() == ISD::SRA)
1671 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
1674 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
1675 if (N1C && N0.getOpcode() == ISD::CTLZ &&
1676 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
1677 uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT);
1678 TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
1680 // If any of the input bits are KnownOne, then the input couldn't be all
1681 // zeros, thus the result of the srl will always be zero.
1682 if (KnownOne) return DAG.getConstant(0, VT);
1684 // If all of the bits input the to ctlz node are known to be zero, then
1685 // the result of the ctlz is "32" and the result of the shift is one.
1686 uint64_t UnknownBits = ~KnownZero & Mask;
1687 if (UnknownBits == 0) return DAG.getConstant(1, VT);
1689 // Otherwise, check to see if there is exactly one bit input to the ctlz.
1690 if ((UnknownBits & (UnknownBits-1)) == 0) {
1691 // Okay, we know that only that the single bit specified by UnknownBits
1692 // could be set on input to the CTLZ node. If this bit is set, the SRL
1693 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
1694 // to an SRL,XOR pair, which is likely to simplify more.
1695 unsigned ShAmt = CountTrailingZeros_64(UnknownBits);
1696 SDOperand Op = N0.getOperand(0);
1698 Op = DAG.getNode(ISD::SRL, VT, Op,
1699 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
1700 AddToWorkList(Op.Val);
1702 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
1709 SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
1710 SDOperand N0 = N->getOperand(0);
1711 MVT::ValueType VT = N->getValueType(0);
1713 // fold (ctlz c1) -> c2
1714 if (isa<ConstantSDNode>(N0))
1715 return DAG.getNode(ISD::CTLZ, VT, N0);
1719 SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
1720 SDOperand N0 = N->getOperand(0);
1721 MVT::ValueType VT = N->getValueType(0);
1723 // fold (cttz c1) -> c2
1724 if (isa<ConstantSDNode>(N0))
1725 return DAG.getNode(ISD::CTTZ, VT, N0);
1729 SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
1730 SDOperand N0 = N->getOperand(0);
1731 MVT::ValueType VT = N->getValueType(0);
1733 // fold (ctpop c1) -> c2
1734 if (isa<ConstantSDNode>(N0))
1735 return DAG.getNode(ISD::CTPOP, VT, N0);
1739 SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1740 SDOperand N0 = N->getOperand(0);
1741 SDOperand N1 = N->getOperand(1);
1742 SDOperand N2 = N->getOperand(2);
1743 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1744 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1745 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1746 MVT::ValueType VT = N->getValueType(0);
1748 // fold select C, X, X -> X
1751 // fold select true, X, Y -> X
1752 if (N0C && !N0C->isNullValue())
1754 // fold select false, X, Y -> Y
1755 if (N0C && N0C->isNullValue())
1757 // fold select C, 1, X -> C | X
1758 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
1759 return DAG.getNode(ISD::OR, VT, N0, N2);
1760 // fold select C, 0, X -> ~C & X
1761 // FIXME: this should check for C type == X type, not i1?
1762 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1763 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1764 AddToWorkList(XORNode.Val);
1765 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1767 // fold select C, X, 1 -> ~C | X
1768 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
1769 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1770 AddToWorkList(XORNode.Val);
1771 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1773 // fold select C, X, 0 -> C & X
1774 // FIXME: this should check for C type == X type, not i1?
1775 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1776 return DAG.getNode(ISD::AND, VT, N0, N1);
1777 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1778 if (MVT::i1 == VT && N0 == N1)
1779 return DAG.getNode(ISD::OR, VT, N0, N2);
1780 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1781 if (MVT::i1 == VT && N0 == N2)
1782 return DAG.getNode(ISD::AND, VT, N0, N1);
1784 // If we can fold this based on the true/false value, do so.
1785 if (SimplifySelectOps(N, N1, N2))
1786 return SDOperand(N, 0); // Don't revisit N.
1788 // fold selects based on a setcc into other things, such as min/max/abs
1789 if (N0.getOpcode() == ISD::SETCC)
1791 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1792 // having to say they don't support SELECT_CC on every type the DAG knows
1793 // about, since there is no way to mark an opcode illegal at all value types
1794 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1795 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1796 N1, N2, N0.getOperand(2));
1798 return SimplifySelect(N0, N1, N2);
1802 SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
1803 SDOperand N0 = N->getOperand(0);
1804 SDOperand N1 = N->getOperand(1);
1805 SDOperand N2 = N->getOperand(2);
1806 SDOperand N3 = N->getOperand(3);
1807 SDOperand N4 = N->getOperand(4);
1808 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1809 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1810 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1811 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1813 // fold select_cc lhs, rhs, x, x, cc -> x
1817 // Determine if the condition we're dealing with is constant
1818 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1820 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
1821 if (SCCC->getValue())
1822 return N2; // cond always true -> true val
1824 return N3; // cond always false -> false val
1827 // Fold to a simpler select_cc
1828 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1829 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
1830 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
1833 // If we can fold this based on the true/false value, do so.
1834 if (SimplifySelectOps(N, N2, N3))
1835 return SDOperand(N, 0); // Don't revisit N.
1837 // fold select_cc into other things, such as min/max/abs
1838 return SimplifySelectCC(N0, N1, N2, N3, CC);
1841 SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1842 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1843 cast<CondCodeSDNode>(N->getOperand(2))->get());
1846 SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
1847 SDOperand N0 = N->getOperand(0);
1848 MVT::ValueType VT = N->getValueType(0);
1850 // fold (sext c1) -> c1
1851 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
1852 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
1854 // fold (sext (sext x)) -> (sext x)
1855 // fold (sext (aext x)) -> (sext x)
1856 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
1857 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1859 // fold (sext (truncate x)) -> (sextinreg x).
1860 if (N0.getOpcode() == ISD::TRUNCATE &&
1861 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
1862 N0.getValueType()))) {
1863 SDOperand Op = N0.getOperand(0);
1864 if (Op.getValueType() < VT) {
1865 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1866 } else if (Op.getValueType() > VT) {
1867 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1869 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
1870 DAG.getValueType(N0.getValueType()));
1873 // fold (sext (load x)) -> (sext (truncate (sextload x)))
1874 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
1875 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
1876 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1877 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
1878 LN0->getBasePtr(), LN0->getSrcValue(),
1879 LN0->getSrcValueOffset(),
1881 CombineTo(N, ExtLoad);
1882 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1883 ExtLoad.getValue(1));
1884 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1887 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1888 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1889 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
1890 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1891 MVT::ValueType EVT = LN0->getLoadedVT();
1892 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
1893 LN0->getBasePtr(), LN0->getSrcValue(),
1894 LN0->getSrcValueOffset(), EVT);
1895 CombineTo(N, ExtLoad);
1896 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1897 ExtLoad.getValue(1));
1898 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1904 SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
1905 SDOperand N0 = N->getOperand(0);
1906 MVT::ValueType VT = N->getValueType(0);
1908 // fold (zext c1) -> c1
1909 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0))
1910 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1911 // fold (zext (zext x)) -> (zext x)
1912 // fold (zext (aext x)) -> (zext x)
1913 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
1914 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1916 // fold (zext (truncate x)) -> (and x, mask)
1917 if (N0.getOpcode() == ISD::TRUNCATE &&
1918 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
1919 SDOperand Op = N0.getOperand(0);
1920 if (Op.getValueType() < VT) {
1921 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
1922 } else if (Op.getValueType() > VT) {
1923 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
1925 return DAG.getZeroExtendInReg(Op, N0.getValueType());
1928 // fold (zext (and (trunc x), cst)) -> (and x, cst).
1929 if (N0.getOpcode() == ISD::AND &&
1930 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
1931 N0.getOperand(1).getOpcode() == ISD::Constant) {
1932 SDOperand X = N0.getOperand(0).getOperand(0);
1933 if (X.getValueType() < VT) {
1934 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
1935 } else if (X.getValueType() > VT) {
1936 X = DAG.getNode(ISD::TRUNCATE, VT, X);
1938 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1939 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
1942 // fold (zext (load x)) -> (zext (truncate (zextload x)))
1943 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
1944 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
1945 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1946 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1947 LN0->getBasePtr(), LN0->getSrcValue(),
1948 LN0->getSrcValueOffset(),
1950 CombineTo(N, ExtLoad);
1951 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1952 ExtLoad.getValue(1));
1953 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1956 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1957 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1958 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) && N0.hasOneUse()) {
1959 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1960 MVT::ValueType EVT = LN0->getLoadedVT();
1961 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1962 LN0->getBasePtr(), LN0->getSrcValue(),
1963 LN0->getSrcValueOffset(), EVT);
1964 CombineTo(N, ExtLoad);
1965 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1966 ExtLoad.getValue(1));
1967 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1972 SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
1973 SDOperand N0 = N->getOperand(0);
1974 MVT::ValueType VT = N->getValueType(0);
1976 // fold (aext c1) -> c1
1977 if (isa<ConstantSDNode>(N0))
1978 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
1979 // fold (aext (aext x)) -> (aext x)
1980 // fold (aext (zext x)) -> (zext x)
1981 // fold (aext (sext x)) -> (sext x)
1982 if (N0.getOpcode() == ISD::ANY_EXTEND ||
1983 N0.getOpcode() == ISD::ZERO_EXTEND ||
1984 N0.getOpcode() == ISD::SIGN_EXTEND)
1985 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
1987 // fold (aext (truncate x))
1988 if (N0.getOpcode() == ISD::TRUNCATE) {
1989 SDOperand TruncOp = N0.getOperand(0);
1990 if (TruncOp.getValueType() == VT)
1991 return TruncOp; // x iff x size == zext size.
1992 if (TruncOp.getValueType() > VT)
1993 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
1994 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
1997 // fold (aext (and (trunc x), cst)) -> (and x, cst).
1998 if (N0.getOpcode() == ISD::AND &&
1999 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2000 N0.getOperand(1).getOpcode() == ISD::Constant) {
2001 SDOperand X = N0.getOperand(0).getOperand(0);
2002 if (X.getValueType() < VT) {
2003 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2004 } else if (X.getValueType() > VT) {
2005 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2007 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
2008 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2011 // fold (aext (load x)) -> (aext (truncate (extload x)))
2012 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2013 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
2014 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2015 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2016 LN0->getBasePtr(), LN0->getSrcValue(),
2017 LN0->getSrcValueOffset(),
2019 CombineTo(N, ExtLoad);
2020 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2021 ExtLoad.getValue(1));
2022 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2025 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
2026 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
2027 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
2028 if (N0.getOpcode() == ISD::LOAD && !ISD::isNON_EXTLoad(N0.Val) &&
2030 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2031 MVT::ValueType EVT = LN0->getLoadedVT();
2032 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
2033 LN0->getChain(), LN0->getBasePtr(),
2035 LN0->getSrcValueOffset(), EVT);
2036 CombineTo(N, ExtLoad);
2037 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2038 ExtLoad.getValue(1));
2039 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2045 SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
2046 SDOperand N0 = N->getOperand(0);
2047 SDOperand N1 = N->getOperand(1);
2048 MVT::ValueType VT = N->getValueType(0);
2049 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
2050 unsigned EVTBits = MVT::getSizeInBits(EVT);
2052 // fold (sext_in_reg c1) -> c1
2053 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
2054 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
2056 // If the input is already sign extended, just drop the extension.
2057 if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
2060 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
2061 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2062 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
2063 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
2066 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
2067 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
2068 return DAG.getZeroExtendInReg(N0, EVT);
2070 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
2071 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
2072 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
2073 if (N0.getOpcode() == ISD::SRL) {
2074 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2075 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
2076 // We can turn this into an SRA iff the input to the SRL is already sign
2078 unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0));
2079 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
2080 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
2084 // fold (sext_inreg (extload x)) -> (sextload x)
2085 if (ISD::isEXTLoad(N0.Val) &&
2086 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
2087 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
2088 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2089 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2090 LN0->getBasePtr(), LN0->getSrcValue(),
2091 LN0->getSrcValueOffset(), EVT);
2092 CombineTo(N, ExtLoad);
2093 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
2094 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2096 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
2097 if (ISD::isZEXTLoad(N0.Val) && N0.hasOneUse() &&
2098 EVT == cast<LoadSDNode>(N0)->getLoadedVT() &&
2099 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
2100 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2101 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2102 LN0->getBasePtr(), LN0->getSrcValue(),
2103 LN0->getSrcValueOffset(), EVT);
2104 CombineTo(N, ExtLoad);
2105 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
2106 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2111 SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
2112 SDOperand N0 = N->getOperand(0);
2113 MVT::ValueType VT = N->getValueType(0);
2116 if (N0.getValueType() == N->getValueType(0))
2118 // fold (truncate c1) -> c1
2119 if (isa<ConstantSDNode>(N0))
2120 return DAG.getNode(ISD::TRUNCATE, VT, N0);
2121 // fold (truncate (truncate x)) -> (truncate x)
2122 if (N0.getOpcode() == ISD::TRUNCATE)
2123 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
2124 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
2125 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
2126 N0.getOpcode() == ISD::ANY_EXTEND) {
2127 if (N0.getValueType() < VT)
2128 // if the source is smaller than the dest, we still need an extend
2129 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2130 else if (N0.getValueType() > VT)
2131 // if the source is larger than the dest, than we just need the truncate
2132 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
2134 // if the source and dest are the same type, we can drop both the extend
2136 return N0.getOperand(0);
2138 // fold (truncate (load x)) -> (smaller load x)
2139 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
2140 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
2141 "Cannot truncate to larger type!");
2142 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2143 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
2144 // For big endian targets, we need to add an offset to the pointer to load
2145 // the correct bytes. For little endian systems, we merely need to read
2146 // fewer bytes from the same pointer.
2148 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
2149 SDOperand NewPtr = TLI.isLittleEndian() ? LN0->getBasePtr() :
2150 DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
2151 DAG.getConstant(PtrOff, PtrType));
2152 AddToWorkList(NewPtr.Val);
2153 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), NewPtr,
2154 LN0->getSrcValue(), LN0->getSrcValueOffset());
2156 CombineTo(N0.Val, Load, Load.getValue(1));
2157 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2162 SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
2163 SDOperand N0 = N->getOperand(0);
2164 MVT::ValueType VT = N->getValueType(0);
2166 // If the input is a constant, let getNode() fold it.
2167 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
2168 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2169 if (Res.Val != N) return Res;
2172 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
2173 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
2175 // fold (conv (load x)) -> (load (conv*)x)
2176 // FIXME: These xforms need to know that the resultant load doesn't need a
2177 // higher alignment than the original!
2178 if (0 && ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse()) {
2179 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2180 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
2181 LN0->getSrcValue(), LN0->getSrcValueOffset());
2183 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
2191 SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) {
2192 SDOperand N0 = N->getOperand(0);
2193 MVT::ValueType VT = N->getValueType(0);
2195 // If the input is a VBUILD_VECTOR with all constant elements, fold this now.
2196 // First check to see if this is all constant.
2197 if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() &&
2198 VT == MVT::Vector) {
2199 bool isSimple = true;
2200 for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i)
2201 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
2202 N0.getOperand(i).getOpcode() != ISD::Constant &&
2203 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
2208 MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT();
2209 if (isSimple && !MVT::isVector(DestEltVT)) {
2210 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT);
2217 /// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector
2218 /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
2219 /// destination element value type.
2220 SDOperand DAGCombiner::
2221 ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
2222 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
2224 // If this is already the right type, we're done.
2225 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
2227 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
2228 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
2230 // If this is a conversion of N elements of one type to N elements of another
2231 // type, convert each element. This handles FP<->INT cases.
2232 if (SrcBitSize == DstBitSize) {
2233 SmallVector<SDOperand, 8> Ops;
2234 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2235 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
2236 AddToWorkList(Ops.back().Val);
2238 Ops.push_back(*(BV->op_end()-2)); // Add num elements.
2239 Ops.push_back(DAG.getValueType(DstEltVT));
2240 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
2243 // Otherwise, we're growing or shrinking the elements. To avoid having to
2244 // handle annoying details of growing/shrinking FP values, we convert them to
2246 if (MVT::isFloatingPoint(SrcEltVT)) {
2247 // Convert the input float vector to a int vector where the elements are the
2249 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
2250 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2251 BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val;
2255 // Now we know the input is an integer vector. If the output is a FP type,
2256 // convert to integer first, then to FP of the right size.
2257 if (MVT::isFloatingPoint(DstEltVT)) {
2258 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
2259 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
2260 SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val;
2262 // Next, convert to FP elements of the same size.
2263 return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT);
2266 // Okay, we know the src/dst types are both integers of differing types.
2267 // Handling growing first.
2268 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
2269 if (SrcBitSize < DstBitSize) {
2270 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
2272 SmallVector<SDOperand, 8> Ops;
2273 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e;
2274 i += NumInputsPerOutput) {
2275 bool isLE = TLI.isLittleEndian();
2276 uint64_t NewBits = 0;
2277 bool EltIsUndef = true;
2278 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
2279 // Shift the previously computed bits over.
2280 NewBits <<= SrcBitSize;
2281 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
2282 if (Op.getOpcode() == ISD::UNDEF) continue;
2285 NewBits |= cast<ConstantSDNode>(Op)->getValue();
2289 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2291 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
2294 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2295 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2296 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
2299 // Finally, this must be the case where we are shrinking elements: each input
2300 // turns into multiple outputs.
2301 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
2302 SmallVector<SDOperand, 8> Ops;
2303 for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) {
2304 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
2305 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
2306 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
2309 uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue();
2311 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
2312 unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1);
2313 OpVal >>= DstBitSize;
2314 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
2317 // For big endian targets, swap the order of the pieces of each element.
2318 if (!TLI.isLittleEndian())
2319 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
2321 Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements.
2322 Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size.
2323 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
2328 SDOperand DAGCombiner::visitFADD(SDNode *N) {
2329 SDOperand N0 = N->getOperand(0);
2330 SDOperand N1 = N->getOperand(1);
2331 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2332 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2333 MVT::ValueType VT = N->getValueType(0);
2335 // fold (fadd c1, c2) -> c1+c2
2337 return DAG.getNode(ISD::FADD, VT, N0, N1);
2338 // canonicalize constant to RHS
2339 if (N0CFP && !N1CFP)
2340 return DAG.getNode(ISD::FADD, VT, N1, N0);
2341 // fold (A + (-B)) -> A-B
2342 if (N1.getOpcode() == ISD::FNEG)
2343 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
2344 // fold ((-A) + B) -> B-A
2345 if (N0.getOpcode() == ISD::FNEG)
2346 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
2350 SDOperand DAGCombiner::visitFSUB(SDNode *N) {
2351 SDOperand N0 = N->getOperand(0);
2352 SDOperand N1 = N->getOperand(1);
2353 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2354 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2355 MVT::ValueType VT = N->getValueType(0);
2357 // fold (fsub c1, c2) -> c1-c2
2359 return DAG.getNode(ISD::FSUB, VT, N0, N1);
2360 // fold (A-(-B)) -> A+B
2361 if (N1.getOpcode() == ISD::FNEG)
2362 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
2366 SDOperand DAGCombiner::visitFMUL(SDNode *N) {
2367 SDOperand N0 = N->getOperand(0);
2368 SDOperand N1 = N->getOperand(1);
2369 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2370 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2371 MVT::ValueType VT = N->getValueType(0);
2373 // fold (fmul c1, c2) -> c1*c2
2375 return DAG.getNode(ISD::FMUL, VT, N0, N1);
2376 // canonicalize constant to RHS
2377 if (N0CFP && !N1CFP)
2378 return DAG.getNode(ISD::FMUL, VT, N1, N0);
2379 // fold (fmul X, 2.0) -> (fadd X, X)
2380 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2381 return DAG.getNode(ISD::FADD, VT, N0, N0);
2385 SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2386 SDOperand N0 = N->getOperand(0);
2387 SDOperand N1 = N->getOperand(1);
2388 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2389 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2390 MVT::ValueType VT = N->getValueType(0);
2392 // fold (fdiv c1, c2) -> c1/c2
2394 return DAG.getNode(ISD::FDIV, VT, N0, N1);
2398 SDOperand DAGCombiner::visitFREM(SDNode *N) {
2399 SDOperand N0 = N->getOperand(0);
2400 SDOperand N1 = N->getOperand(1);
2401 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2402 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2403 MVT::ValueType VT = N->getValueType(0);
2405 // fold (frem c1, c2) -> fmod(c1,c2)
2407 return DAG.getNode(ISD::FREM, VT, N0, N1);
2411 SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2412 SDOperand N0 = N->getOperand(0);
2413 SDOperand N1 = N->getOperand(1);
2414 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2415 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2416 MVT::ValueType VT = N->getValueType(0);
2418 if (N0CFP && N1CFP) // Constant fold
2419 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2422 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2423 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2428 u.d = N1CFP->getValue();
2430 return DAG.getNode(ISD::FABS, VT, N0);
2432 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2435 // copysign(fabs(x), y) -> copysign(x, y)
2436 // copysign(fneg(x), y) -> copysign(x, y)
2437 // copysign(copysign(x,z), y) -> copysign(x, y)
2438 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2439 N0.getOpcode() == ISD::FCOPYSIGN)
2440 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2442 // copysign(x, abs(y)) -> abs(x)
2443 if (N1.getOpcode() == ISD::FABS)
2444 return DAG.getNode(ISD::FABS, VT, N0);
2446 // copysign(x, copysign(y,z)) -> copysign(x, z)
2447 if (N1.getOpcode() == ISD::FCOPYSIGN)
2448 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2450 // copysign(x, fp_extend(y)) -> copysign(x, y)
2451 // copysign(x, fp_round(y)) -> copysign(x, y)
2452 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2453 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2460 SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
2461 SDOperand N0 = N->getOperand(0);
2462 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2463 MVT::ValueType VT = N->getValueType(0);
2465 // fold (sint_to_fp c1) -> c1fp
2467 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
2471 SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
2472 SDOperand N0 = N->getOperand(0);
2473 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2474 MVT::ValueType VT = N->getValueType(0);
2476 // fold (uint_to_fp c1) -> c1fp
2478 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
2482 SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
2483 SDOperand N0 = N->getOperand(0);
2484 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2485 MVT::ValueType VT = N->getValueType(0);
2487 // fold (fp_to_sint c1fp) -> c1
2489 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
2493 SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
2494 SDOperand N0 = N->getOperand(0);
2495 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2496 MVT::ValueType VT = N->getValueType(0);
2498 // fold (fp_to_uint c1fp) -> c1
2500 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
2504 SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
2505 SDOperand N0 = N->getOperand(0);
2506 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2507 MVT::ValueType VT = N->getValueType(0);
2509 // fold (fp_round c1fp) -> c1fp
2511 return DAG.getNode(ISD::FP_ROUND, VT, N0);
2513 // fold (fp_round (fp_extend x)) -> x
2514 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2515 return N0.getOperand(0);
2517 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2518 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2519 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2520 AddToWorkList(Tmp.Val);
2521 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2527 SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
2528 SDOperand N0 = N->getOperand(0);
2529 MVT::ValueType VT = N->getValueType(0);
2530 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2531 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2533 // fold (fp_round_inreg c1fp) -> c1fp
2535 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
2536 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
2541 SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
2542 SDOperand N0 = N->getOperand(0);
2543 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2544 MVT::ValueType VT = N->getValueType(0);
2546 // fold (fp_extend c1fp) -> c1fp
2548 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
2550 // fold (fpext (load x)) -> (fpext (fpround (extload x)))
2551 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
2552 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
2553 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2554 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
2555 LN0->getBasePtr(), LN0->getSrcValue(),
2556 LN0->getSrcValueOffset(),
2558 CombineTo(N, ExtLoad);
2559 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad),
2560 ExtLoad.getValue(1));
2561 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2568 SDOperand DAGCombiner::visitFNEG(SDNode *N) {
2569 SDOperand N0 = N->getOperand(0);
2570 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2571 MVT::ValueType VT = N->getValueType(0);
2573 // fold (fneg c1) -> -c1
2575 return DAG.getNode(ISD::FNEG, VT, N0);
2576 // fold (fneg (sub x, y)) -> (sub y, x)
2577 if (N0.getOpcode() == ISD::SUB)
2578 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
2579 // fold (fneg (fneg x)) -> x
2580 if (N0.getOpcode() == ISD::FNEG)
2581 return N0.getOperand(0);
2585 SDOperand DAGCombiner::visitFABS(SDNode *N) {
2586 SDOperand N0 = N->getOperand(0);
2587 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2588 MVT::ValueType VT = N->getValueType(0);
2590 // fold (fabs c1) -> fabs(c1)
2592 return DAG.getNode(ISD::FABS, VT, N0);
2593 // fold (fabs (fabs x)) -> (fabs x)
2594 if (N0.getOpcode() == ISD::FABS)
2595 return N->getOperand(0);
2596 // fold (fabs (fneg x)) -> (fabs x)
2597 // fold (fabs (fcopysign x, y)) -> (fabs x)
2598 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2599 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2604 SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2605 SDOperand Chain = N->getOperand(0);
2606 SDOperand N1 = N->getOperand(1);
2607 SDOperand N2 = N->getOperand(2);
2608 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2610 // never taken branch, fold to chain
2611 if (N1C && N1C->isNullValue())
2613 // unconditional branch
2614 if (N1C && N1C->getValue() == 1)
2615 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2616 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2618 if (N1.getOpcode() == ISD::SETCC &&
2619 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2620 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2621 N1.getOperand(0), N1.getOperand(1), N2);
2626 // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2628 SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
2629 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2630 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2632 // Use SimplifySetCC to simplify SETCC's.
2633 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2634 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2636 // fold br_cc true, dest -> br dest (unconditional branch)
2637 if (SCCC && SCCC->getValue())
2638 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2640 // fold br_cc false, dest -> unconditional fall through
2641 if (SCCC && SCCC->isNullValue())
2642 return N->getOperand(0);
2643 // fold to a simpler setcc
2644 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2645 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2646 Simp.getOperand(2), Simp.getOperand(0),
2647 Simp.getOperand(1), N->getOperand(4));
2651 SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2652 LoadSDNode *LD = cast<LoadSDNode>(N);
2653 SDOperand Chain = LD->getChain();
2654 SDOperand Ptr = LD->getBasePtr();
2656 // If there are no uses of the loaded value, change uses of the chain value
2657 // into uses of the chain input (i.e. delete the dead load).
2658 if (N->hasNUsesOfValue(0, 0))
2659 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2661 // If this load is directly stored, replace the load value with the stored
2663 // TODO: Handle store large -> read small portion.
2664 // TODO: Handle TRUNCSTORE/LOADEXT
2665 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
2666 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2667 Chain.getOperand(1).getValueType() == N->getValueType(0))
2668 return CombineTo(N, Chain.getOperand(1), Chain);
2672 // Walk up chain skipping non-aliasing memory nodes.
2673 SDOperand BetterChain = FindBetterChain(N, Chain);
2675 // If there is a better chain.
2676 if (Chain != BetterChain) {
2679 // Replace the chain to void dependency.
2680 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
2681 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
2682 LD->getSrcValue(), LD->getSrcValueOffset());
2684 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
2685 LD->getValueType(0),
2686 BetterChain, Ptr, LD->getSrcValue(),
2687 LD->getSrcValueOffset(),
2691 // Create token factor to keep old chain connected.
2692 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
2693 Chain, ReplLoad.getValue(1));
2695 // Replace uses with load result and token factor.
2696 return CombineTo(N, ReplLoad.getValue(0), Token);
2703 SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2704 SDOperand Chain = N->getOperand(0);
2705 SDOperand Value = N->getOperand(1);
2706 SDOperand Ptr = N->getOperand(2);
2707 SDOperand SrcValue = N->getOperand(3);
2709 // FIXME - Switch over after StoreSDNode comes online.
2710 if (N->getOpcode() == ISD::TRUNCSTORE) {
2712 // Walk up chain skipping non-aliasing memory nodes.
2713 SDOperand BetterChain = FindBetterChain(N, Chain);
2715 // If there is a better chain.
2716 if (Chain != BetterChain) {
2717 // Replace the chain to avoid dependency.
2718 SDOperand ReplTStore = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
2719 BetterChain, Value, Ptr, SrcValue,
2722 // Create token to keep both nodes around.
2723 return DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplTStore);
2730 // If this is a store that kills a previous store, remove the previous store.
2731 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2732 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2733 // Make sure that these stores are the same value type:
2734 // FIXME: we really care that the second store is >= size of the first.
2735 Value.getValueType() == Chain.getOperand(1).getValueType()) {
2736 // Create a new store of Value that replaces both stores.
2737 SDNode *PrevStore = Chain.Val;
2738 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2740 SDOperand NewStore = DAG.getStore(PrevStore->getOperand(0), Value, Ptr,
2742 CombineTo(N, NewStore); // Nuke this store.
2743 CombineTo(PrevStore, NewStore); // Nuke the previous store.
2744 return SDOperand(N, 0);
2747 // If this is a store of a bit convert, store the input value.
2748 // FIXME: This needs to know that the resultant store does not need a
2749 // higher alignment than the original.
2750 if (0 && Value.getOpcode() == ISD::BIT_CONVERT) {
2751 return DAG.getStore(Chain, Value.getOperand(0), Ptr, SrcValue);
2755 // If the store ptr is a frame index and the frame index has a use of one
2756 // and this is a return block, then the store is redundant.
2757 if (Ptr.hasOneUse() && isa<FrameIndexSDNode>(Ptr) &&
2758 DAG.getRoot().getOpcode() == ISD::RET) {
2762 // Walk up chain skipping non-aliasing memory nodes.
2763 SDOperand BetterChain = FindBetterChain(N, Chain);
2765 // If there is a better chain.
2766 if (Chain != BetterChain) {
2767 // Replace the chain to avoid dependency.
2768 SDOperand ReplStore = DAG.getStore(BetterChain, Value, Ptr, SrcValue);
2769 // Create token to keep both nodes around.
2770 return DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
2777 SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2778 SDOperand InVec = N->getOperand(0);
2779 SDOperand InVal = N->getOperand(1);
2780 SDOperand EltNo = N->getOperand(2);
2782 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2783 // vector with the inserted element.
2784 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2785 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2786 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2787 if (Elt < Ops.size())
2789 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
2790 &Ops[0], Ops.size());
2796 SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2797 SDOperand InVec = N->getOperand(0);
2798 SDOperand InVal = N->getOperand(1);
2799 SDOperand EltNo = N->getOperand(2);
2800 SDOperand NumElts = N->getOperand(3);
2801 SDOperand EltType = N->getOperand(4);
2803 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2804 // vector with the inserted element.
2805 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2806 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2807 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2808 if (Elt < Ops.size()-2)
2810 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(),
2811 &Ops[0], Ops.size());
2817 SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2818 unsigned NumInScalars = N->getNumOperands()-2;
2819 SDOperand NumElts = N->getOperand(NumInScalars);
2820 SDOperand EltType = N->getOperand(NumInScalars+1);
2822 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2823 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2824 // two distinct vectors, turn this into a shuffle node.
2825 SDOperand VecIn1, VecIn2;
2826 for (unsigned i = 0; i != NumInScalars; ++i) {
2827 // Ignore undef inputs.
2828 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2830 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2831 // constant index, bail out.
2832 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2833 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2834 VecIn1 = VecIn2 = SDOperand(0, 0);
2838 // If the input vector type disagrees with the result of the vbuild_vector,
2839 // we can't make a shuffle.
2840 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2841 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2842 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2843 VecIn1 = VecIn2 = SDOperand(0, 0);
2847 // Otherwise, remember this. We allow up to two distinct input vectors.
2848 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2851 if (VecIn1.Val == 0) {
2852 VecIn1 = ExtractedFromVec;
2853 } else if (VecIn2.Val == 0) {
2854 VecIn2 = ExtractedFromVec;
2857 VecIn1 = VecIn2 = SDOperand(0, 0);
2862 // If everything is good, we can make a shuffle operation.
2864 SmallVector<SDOperand, 8> BuildVecIndices;
2865 for (unsigned i = 0; i != NumInScalars; ++i) {
2866 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2867 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2871 SDOperand Extract = N->getOperand(i);
2873 // If extracting from the first vector, just use the index directly.
2874 if (Extract.getOperand(0) == VecIn1) {
2875 BuildVecIndices.push_back(Extract.getOperand(1));
2879 // Otherwise, use InIdx + VecSize
2880 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2881 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2884 // Add count and size info.
2885 BuildVecIndices.push_back(NumElts);
2886 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2888 // Return the new VVECTOR_SHUFFLE node.
2894 // Use an undef vbuild_vector as input for the second operand.
2895 std::vector<SDOperand> UnOps(NumInScalars,
2896 DAG.getNode(ISD::UNDEF,
2897 cast<VTSDNode>(EltType)->getVT()));
2898 UnOps.push_back(NumElts);
2899 UnOps.push_back(EltType);
2900 Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2901 &UnOps[0], UnOps.size());
2902 AddToWorkList(Ops[1].Val);
2904 Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
2905 &BuildVecIndices[0], BuildVecIndices.size());
2908 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5);
2914 SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
2915 SDOperand ShufMask = N->getOperand(2);
2916 unsigned NumElts = ShufMask.getNumOperands();
2918 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2919 bool isIdentity = true;
2920 for (unsigned i = 0; i != NumElts; ++i) {
2921 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2922 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2927 if (isIdentity) return N->getOperand(0);
2929 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2931 for (unsigned i = 0; i != NumElts; ++i) {
2932 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2933 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2938 if (isIdentity) return N->getOperand(1);
2940 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
2942 bool isUnary = true;
2943 bool isSplat = true;
2945 unsigned BaseIdx = 0;
2946 for (unsigned i = 0; i != NumElts; ++i)
2947 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
2948 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
2949 int V = (Idx < NumElts) ? 0 : 1;
2963 SDOperand N0 = N->getOperand(0);
2964 SDOperand N1 = N->getOperand(1);
2965 // Normalize unary shuffle so the RHS is undef.
2966 if (isUnary && VecNum == 1)
2969 // If it is a splat, check if the argument vector is a build_vector with
2970 // all scalar elements the same.
2973 if (V->getOpcode() == ISD::BIT_CONVERT)
2974 V = V->getOperand(0).Val;
2975 if (V->getOpcode() == ISD::BUILD_VECTOR) {
2976 unsigned NumElems = V->getNumOperands()-2;
2977 if (NumElems > BaseIdx) {
2979 bool AllSame = true;
2980 for (unsigned i = 0; i != NumElems; ++i) {
2981 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
2982 Base = V->getOperand(i);
2986 // Splat of <u, u, u, u>, return <u, u, u, u>
2989 for (unsigned i = 0; i != NumElems; ++i) {
2990 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
2991 V->getOperand(i) != Base) {
2996 // Splat of <x, x, x, x>, return <x, x, x, x>
3003 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3005 if (isUnary || N0 == N1) {
3006 if (N0.getOpcode() == ISD::UNDEF)
3007 return DAG.getNode(ISD::UNDEF, N->getValueType(0));
3008 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3010 SmallVector<SDOperand, 8> MappedOps;
3011 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
3012 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3013 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3014 MappedOps.push_back(ShufMask.getOperand(i));
3017 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3018 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3021 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
3022 &MappedOps[0], MappedOps.size());
3023 AddToWorkList(ShufMask.Val);
3024 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
3026 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
3033 SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
3034 SDOperand ShufMask = N->getOperand(2);
3035 unsigned NumElts = ShufMask.getNumOperands()-2;
3037 // If the shuffle mask is an identity operation on the LHS, return the LHS.
3038 bool isIdentity = true;
3039 for (unsigned i = 0; i != NumElts; ++i) {
3040 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3041 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
3046 if (isIdentity) return N->getOperand(0);
3048 // If the shuffle mask is an identity operation on the RHS, return the RHS.
3050 for (unsigned i = 0; i != NumElts; ++i) {
3051 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
3052 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
3057 if (isIdentity) return N->getOperand(1);
3059 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
3061 bool isUnary = true;
3062 bool isSplat = true;
3064 unsigned BaseIdx = 0;
3065 for (unsigned i = 0; i != NumElts; ++i)
3066 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
3067 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
3068 int V = (Idx < NumElts) ? 0 : 1;
3082 SDOperand N0 = N->getOperand(0);
3083 SDOperand N1 = N->getOperand(1);
3084 // Normalize unary shuffle so the RHS is undef.
3085 if (isUnary && VecNum == 1)
3088 // If it is a splat, check if the argument vector is a build_vector with
3089 // all scalar elements the same.
3092 if (V->getOpcode() == ISD::VBIT_CONVERT)
3093 V = V->getOperand(0).Val;
3094 if (V->getOpcode() == ISD::VBUILD_VECTOR) {
3095 unsigned NumElems = V->getNumOperands()-2;
3096 if (NumElems > BaseIdx) {
3098 bool AllSame = true;
3099 for (unsigned i = 0; i != NumElems; ++i) {
3100 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
3101 Base = V->getOperand(i);
3105 // Splat of <u, u, u, u>, return <u, u, u, u>
3108 for (unsigned i = 0; i != NumElems; ++i) {
3109 if (V->getOperand(i).getOpcode() != ISD::UNDEF &&
3110 V->getOperand(i) != Base) {
3115 // Splat of <x, x, x, x>, return <x, x, x, x>
3122 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
3124 if (isUnary || N0 == N1) {
3125 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
3127 SmallVector<SDOperand, 8> MappedOps;
3128 for (unsigned i = 0; i != NumElts; ++i) {
3129 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
3130 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
3131 MappedOps.push_back(ShufMask.getOperand(i));
3134 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
3135 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
3138 // Add the type/#elts values.
3139 MappedOps.push_back(ShufMask.getOperand(NumElts));
3140 MappedOps.push_back(ShufMask.getOperand(NumElts+1));
3142 ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(),
3143 &MappedOps[0], MappedOps.size());
3144 AddToWorkList(ShufMask.Val);
3146 // Build the undef vector.
3147 SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType());
3148 for (unsigned i = 0; i != NumElts; ++i)
3149 MappedOps[i] = UDVal;
3150 MappedOps[NumElts ] = *(N0.Val->op_end()-2);
3151 MappedOps[NumElts+1] = *(N0.Val->op_end()-1);
3152 UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3153 &MappedOps[0], MappedOps.size());
3155 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3156 N0, UDVal, ShufMask,
3157 MappedOps[NumElts], MappedOps[NumElts+1]);
3163 /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
3164 /// a VAND to a vector_shuffle with the destination vector and a zero vector.
3165 /// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
3166 /// vector_shuffle V, Zero, <0, 4, 2, 4>
3167 SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
3168 SDOperand LHS = N->getOperand(0);
3169 SDOperand RHS = N->getOperand(1);
3170 if (N->getOpcode() == ISD::VAND) {
3171 SDOperand DstVecSize = *(LHS.Val->op_end()-2);
3172 SDOperand DstVecEVT = *(LHS.Val->op_end()-1);
3173 if (RHS.getOpcode() == ISD::VBIT_CONVERT)
3174 RHS = RHS.getOperand(0);
3175 if (RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3176 std::vector<SDOperand> IdxOps;
3177 unsigned NumOps = RHS.getNumOperands();
3178 unsigned NumElts = NumOps-2;
3179 MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT();
3180 for (unsigned i = 0; i != NumElts; ++i) {
3181 SDOperand Elt = RHS.getOperand(i);
3182 if (!isa<ConstantSDNode>(Elt))
3184 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
3185 IdxOps.push_back(DAG.getConstant(i, EVT));
3186 else if (cast<ConstantSDNode>(Elt)->isNullValue())
3187 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
3192 // Let's see if the target supports this vector_shuffle.
3193 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
3196 // Return the new VVECTOR_SHUFFLE node.
3197 SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32);
3198 SDOperand EVTNode = DAG.getValueType(EVT);
3199 std::vector<SDOperand> Ops;
3200 LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode,
3203 AddToWorkList(LHS.Val);
3204 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
3205 ZeroOps.push_back(NumEltsNode);
3206 ZeroOps.push_back(EVTNode);
3207 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3208 &ZeroOps[0], ZeroOps.size()));
3209 IdxOps.push_back(NumEltsNode);
3210 IdxOps.push_back(EVTNode);
3211 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector,
3212 &IdxOps[0], IdxOps.size()));
3213 Ops.push_back(NumEltsNode);
3214 Ops.push_back(EVTNode);
3215 SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector,
3216 &Ops[0], Ops.size());
3217 if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) {
3218 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
3219 DstVecSize, DstVecEVT);
3227 /// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates
3228 /// the scalar operation of the vop if it is operating on an integer vector
3229 /// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD).
3230 SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp,
3231 ISD::NodeType FPOp) {
3232 MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT();
3233 ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp;
3234 SDOperand LHS = N->getOperand(0);
3235 SDOperand RHS = N->getOperand(1);
3236 SDOperand Shuffle = XformToShuffleWithZero(N);
3237 if (Shuffle.Val) return Shuffle;
3239 // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold
3241 if (LHS.getOpcode() == ISD::VBUILD_VECTOR &&
3242 RHS.getOpcode() == ISD::VBUILD_VECTOR) {
3243 SmallVector<SDOperand, 8> Ops;
3244 for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) {
3245 SDOperand LHSOp = LHS.getOperand(i);
3246 SDOperand RHSOp = RHS.getOperand(i);
3247 // If these two elements can't be folded, bail out.
3248 if ((LHSOp.getOpcode() != ISD::UNDEF &&
3249 LHSOp.getOpcode() != ISD::Constant &&
3250 LHSOp.getOpcode() != ISD::ConstantFP) ||
3251 (RHSOp.getOpcode() != ISD::UNDEF &&
3252 RHSOp.getOpcode() != ISD::Constant &&
3253 RHSOp.getOpcode() != ISD::ConstantFP))
3255 // Can't fold divide by zero.
3256 if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) {
3257 if ((RHSOp.getOpcode() == ISD::Constant &&
3258 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
3259 (RHSOp.getOpcode() == ISD::ConstantFP &&
3260 !cast<ConstantFPSDNode>(RHSOp.Val)->getValue()))
3263 Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp));
3264 AddToWorkList(Ops.back().Val);
3265 assert((Ops.back().getOpcode() == ISD::UNDEF ||
3266 Ops.back().getOpcode() == ISD::Constant ||
3267 Ops.back().getOpcode() == ISD::ConstantFP) &&
3268 "Scalar binop didn't fold!");
3271 if (Ops.size() == LHS.getNumOperands()-2) {
3272 Ops.push_back(*(LHS.Val->op_end()-2));
3273 Ops.push_back(*(LHS.Val->op_end()-1));
3274 return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size());
3281 SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
3282 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
3284 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
3285 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3286 // If we got a simplified select_cc node back from SimplifySelectCC, then
3287 // break it down into a new SETCC node, and a new SELECT node, and then return
3288 // the SELECT node, since we were called with a SELECT node.
3290 // Check to see if we got a select_cc back (to turn into setcc/select).
3291 // Otherwise, just return whatever node we got back, like fabs.
3292 if (SCC.getOpcode() == ISD::SELECT_CC) {
3293 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
3294 SCC.getOperand(0), SCC.getOperand(1),
3296 AddToWorkList(SETCC.Val);
3297 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
3298 SCC.getOperand(3), SETCC);
3305 /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
3306 /// are the two values being selected between, see if we can simplify the
3307 /// select. Callers of this should assume that TheSelect is deleted if this
3308 /// returns true. As such, they should return the appropriate thing (e.g. the
3309 /// node) back to the top-level of the DAG combiner loop to avoid it being
3312 bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
3315 // If this is a select from two identical things, try to pull the operation
3316 // through the select.
3317 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
3318 // If this is a load and the token chain is identical, replace the select
3319 // of two loads with a load through a select of the address to load from.
3320 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
3321 // constants have been dropped into the constant pool.
3322 if (LHS.getOpcode() == ISD::LOAD &&
3323 // Token chains must be identical.
3324 LHS.getOperand(0) == RHS.getOperand(0)) {
3325 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
3326 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
3328 // If this is an EXTLOAD, the VT's must match.
3329 if (LLD->getLoadedVT() == RLD->getLoadedVT()) {
3330 // FIXME: this conflates two src values, discarding one. This is not
3331 // the right thing to do, but nothing uses srcvalues now. When they do,
3332 // turn SrcValue into a list of locations.
3334 if (TheSelect->getOpcode() == ISD::SELECT)
3335 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
3336 TheSelect->getOperand(0), LLD->getBasePtr(),
3339 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
3340 TheSelect->getOperand(0),
3341 TheSelect->getOperand(1),
3342 LLD->getBasePtr(), RLD->getBasePtr(),
3343 TheSelect->getOperand(4));
3346 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
3347 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
3348 Addr,LLD->getSrcValue(), LLD->getSrcValueOffset());
3350 Load = DAG.getExtLoad(LLD->getExtensionType(),
3351 TheSelect->getValueType(0),
3352 LLD->getChain(), Addr, LLD->getSrcValue(),
3353 LLD->getSrcValueOffset(),
3354 LLD->getLoadedVT());
3356 // Users of the select now use the result of the load.
3357 CombineTo(TheSelect, Load);
3359 // Users of the old loads now use the new load's chain. We know the
3360 // old-load value is dead now.
3361 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
3362 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
3371 SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
3372 SDOperand N2, SDOperand N3,
3375 MVT::ValueType VT = N2.getValueType();
3376 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
3377 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
3378 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
3380 // Determine if the condition we're dealing with is constant
3381 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
3382 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
3384 // fold select_cc true, x, y -> x
3385 if (SCCC && SCCC->getValue())
3387 // fold select_cc false, x, y -> y
3388 if (SCCC && SCCC->getValue() == 0)
3391 // Check to see if we can simplify the select into an fabs node
3392 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
3393 // Allow either -0.0 or 0.0
3394 if (CFP->getValue() == 0.0) {
3395 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
3396 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
3397 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
3398 N2 == N3.getOperand(0))
3399 return DAG.getNode(ISD::FABS, VT, N0);
3401 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
3402 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
3403 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
3404 N2.getOperand(0) == N3)
3405 return DAG.getNode(ISD::FABS, VT, N3);
3409 // Check to see if we can perform the "gzip trick", transforming
3410 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
3411 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
3412 MVT::isInteger(N0.getValueType()) &&
3413 MVT::isInteger(N2.getValueType()) &&
3414 (N1C->isNullValue() || // (a < 0) ? b : 0
3415 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
3416 MVT::ValueType XType = N0.getValueType();
3417 MVT::ValueType AType = N2.getValueType();
3418 if (XType >= AType) {
3419 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
3420 // single-bit constant.
3421 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
3422 unsigned ShCtV = Log2_64(N2C->getValue());
3423 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
3424 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
3425 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
3426 AddToWorkList(Shift.Val);
3427 if (XType > AType) {
3428 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
3429 AddToWorkList(Shift.Val);
3431 return DAG.getNode(ISD::AND, AType, Shift, N2);
3433 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3434 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3435 TLI.getShiftAmountTy()));
3436 AddToWorkList(Shift.Val);
3437 if (XType > AType) {
3438 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
3439 AddToWorkList(Shift.Val);
3441 return DAG.getNode(ISD::AND, AType, Shift, N2);
3445 // fold select C, 16, 0 -> shl C, 4
3446 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
3447 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
3448 // Get a SetCC of the condition
3449 // FIXME: Should probably make sure that setcc is legal if we ever have a
3450 // target where it isn't.
3451 SDOperand Temp, SCC;
3452 // cast from setcc result type to select result type
3453 if (AfterLegalize) {
3454 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3455 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
3457 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
3458 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
3460 AddToWorkList(SCC.Val);
3461 AddToWorkList(Temp.Val);
3462 // shl setcc result by log2 n2c
3463 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
3464 DAG.getConstant(Log2_64(N2C->getValue()),
3465 TLI.getShiftAmountTy()));
3468 // Check to see if this is the equivalent of setcc
3469 // FIXME: Turn all of these into setcc if setcc if setcc is legal
3470 // otherwise, go ahead with the folds.
3471 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
3472 MVT::ValueType XType = N0.getValueType();
3473 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
3474 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
3475 if (Res.getValueType() != VT)
3476 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
3480 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
3481 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
3482 TLI.isOperationLegal(ISD::CTLZ, XType)) {
3483 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
3484 return DAG.getNode(ISD::SRL, XType, Ctlz,
3485 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
3486 TLI.getShiftAmountTy()));
3488 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
3489 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
3490 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
3492 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
3493 DAG.getConstant(~0ULL, XType));
3494 return DAG.getNode(ISD::SRL, XType,
3495 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
3496 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3497 TLI.getShiftAmountTy()));
3499 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
3500 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
3501 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
3502 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3503 TLI.getShiftAmountTy()));
3504 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
3508 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
3509 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
3510 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
3511 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
3512 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
3513 MVT::ValueType XType = N0.getValueType();
3514 if (SubC->isNullValue() && MVT::isInteger(XType)) {
3515 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
3516 DAG.getConstant(MVT::getSizeInBits(XType)-1,
3517 TLI.getShiftAmountTy()));
3518 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
3519 AddToWorkList(Shift.Val);
3520 AddToWorkList(Add.Val);
3521 return DAG.getNode(ISD::XOR, XType, Add, Shift);
3529 SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
3530 SDOperand N1, ISD::CondCode Cond,
3531 bool foldBooleans) {
3532 // These setcc operations always fold.
3536 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
3538 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
3541 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
3542 uint64_t C1 = N1C->getValue();
3543 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
3544 uint64_t C0 = N0C->getValue();
3546 // Sign extend the operands if required
3547 if (ISD::isSignedIntSetCC(Cond)) {
3548 C0 = N0C->getSignExtended();
3549 C1 = N1C->getSignExtended();
3553 default: assert(0 && "Unknown integer setcc!");
3554 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3555 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3556 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
3557 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
3558 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
3559 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
3560 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
3561 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
3562 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
3563 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
3566 // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
3567 // equality comparison, then we're just comparing whether X itself is
3569 if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
3570 N0.getOperand(0).getOpcode() == ISD::CTLZ &&
3571 N0.getOperand(1).getOpcode() == ISD::Constant) {
3572 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
3573 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3574 ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) {
3575 if ((C1 == 0) == (Cond == ISD::SETEQ)) {
3576 // (srl (ctlz x), 5) == 0 -> X != 0
3577 // (srl (ctlz x), 5) != 1 -> X != 0
3580 // (srl (ctlz x), 5) != 0 -> X == 0
3581 // (srl (ctlz x), 5) == 1 -> X == 0
3584 SDOperand Zero = DAG.getConstant(0, N0.getValueType());
3585 return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0),
3590 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
3591 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
3592 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
3594 // If the comparison constant has bits in the upper part, the
3595 // zero-extended value could never match.
3596 if (C1 & (~0ULL << InSize)) {
3597 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
3601 case ISD::SETEQ: return DAG.getConstant(0, VT);
3604 case ISD::SETNE: return DAG.getConstant(1, VT);
3607 // True if the sign bit of C1 is set.
3608 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
3611 // True if the sign bit of C1 isn't set.
3612 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
3618 // Otherwise, we can perform the comparison with the low bits.
3626 return DAG.getSetCC(VT, N0.getOperand(0),
3627 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
3630 break; // todo, be more careful with signed comparisons
3632 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3633 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
3634 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
3635 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
3636 MVT::ValueType ExtDstTy = N0.getValueType();
3637 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
3639 // If the extended part has any inconsistent bits, it cannot ever
3640 // compare equal. In other words, they have to be all ones or all
3643 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
3644 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
3645 return DAG.getConstant(Cond == ISD::SETNE, VT);
3648 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
3649 if (Op0Ty == ExtSrcTy) {
3650 ZextOp = N0.getOperand(0);
3652 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
3653 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
3654 DAG.getConstant(Imm, Op0Ty));
3656 AddToWorkList(ZextOp.Val);
3657 // Otherwise, make this a use of a zext.
3658 return DAG.getSetCC(VT, ZextOp,
3659 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
3662 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
3663 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3664 (N0.getOpcode() == ISD::XOR ||
3665 (N0.getOpcode() == ISD::AND &&
3666 N0.getOperand(0).getOpcode() == ISD::XOR &&
3667 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
3668 isa<ConstantSDNode>(N0.getOperand(1)) &&
3669 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
3670 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
3671 // only do this if the top bits are known zero.
3672 if (TLI.MaskedValueIsZero(N1,
3673 MVT::getIntVTBitMask(N0.getValueType())-1)) {
3674 // Okay, get the un-inverted input value.
3676 if (N0.getOpcode() == ISD::XOR)
3677 Val = N0.getOperand(0);
3679 assert(N0.getOpcode() == ISD::AND &&
3680 N0.getOperand(0).getOpcode() == ISD::XOR);
3681 // ((X^1)&1)^1 -> X & 1
3682 Val = DAG.getNode(ISD::AND, N0.getValueType(),
3683 N0.getOperand(0).getOperand(0), N0.getOperand(1));
3685 return DAG.getSetCC(VT, Val, N1,
3686 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
3690 uint64_t MinVal, MaxVal;
3691 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
3692 if (ISD::isSignedIntSetCC(Cond)) {
3693 MinVal = 1ULL << (OperandBitSize-1);
3694 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
3695 MaxVal = ~0ULL >> (65-OperandBitSize);
3700 MaxVal = ~0ULL >> (64-OperandBitSize);
3703 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
3704 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
3705 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
3706 --C1; // X >= C0 --> X > (C0-1)
3707 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3708 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
3711 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
3712 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
3713 ++C1; // X <= C0 --> X < (C0+1)
3714 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
3715 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
3718 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
3719 return DAG.getConstant(0, VT); // X < MIN --> false
3721 // Canonicalize setgt X, Min --> setne X, Min
3722 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
3723 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
3724 // Canonicalize setlt X, Max --> setne X, Max
3725 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
3726 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
3728 // If we have setult X, 1, turn it into seteq X, 0
3729 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
3730 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
3732 // If we have setugt X, Max-1, turn it into seteq X, Max
3733 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
3734 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
3737 // If we have "setcc X, C0", check to see if we can shrink the immediate
3740 // SETUGT X, SINTMAX -> SETLT X, 0
3741 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
3742 C1 == (~0ULL >> (65-OperandBitSize)))
3743 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
3746 // FIXME: Implement the rest of these.
3748 // Fold bit comparisons when we can.
3749 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3750 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
3751 if (ConstantSDNode *AndRHS =
3752 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3753 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
3754 // Perform the xform if the AND RHS is a single bit.
3755 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
3756 return DAG.getNode(ISD::SRL, VT, N0,
3757 DAG.getConstant(Log2_64(AndRHS->getValue()),
3758 TLI.getShiftAmountTy()));
3760 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3761 // (X & 8) == 8 --> (X & 8) >> 3
3762 // Perform the xform if C1 is a single bit.
3763 if ((C1 & (C1-1)) == 0) {
3764 return DAG.getNode(ISD::SRL, VT, N0,
3765 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
3770 } else if (isa<ConstantSDNode>(N0.Val)) {
3771 // Ensure that the constant occurs on the RHS.
3772 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3775 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3776 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3777 double C0 = N0C->getValue(), C1 = N1C->getValue();
3780 default: break; // FIXME: Implement the rest of these!
3781 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3782 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3783 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3784 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3785 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3786 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3789 // Ensure that the constant occurs on the RHS.
3790 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3794 // We can always fold X == Y for integer setcc's.
3795 if (MVT::isInteger(N0.getValueType()))
3796 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3797 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3798 if (UOF == 2) // FP operators that are undefined on NaNs.
3799 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3800 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3801 return DAG.getConstant(UOF, VT);
3802 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3803 // if it is not already.
3804 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
3805 if (NewCond != Cond)
3806 return DAG.getSetCC(VT, N0, N1, NewCond);
3809 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3810 MVT::isInteger(N0.getValueType())) {
3811 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3812 N0.getOpcode() == ISD::XOR) {
3813 // Simplify (X+Y) == (X+Z) --> Y == Z
3814 if (N0.getOpcode() == N1.getOpcode()) {
3815 if (N0.getOperand(0) == N1.getOperand(0))
3816 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3817 if (N0.getOperand(1) == N1.getOperand(1))
3818 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3819 if (DAG.isCommutativeBinOp(N0.getOpcode())) {
3820 // If X op Y == Y op X, try other combinations.
3821 if (N0.getOperand(0) == N1.getOperand(1))
3822 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3823 if (N0.getOperand(1) == N1.getOperand(0))
3824 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
3828 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3829 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3830 // Turn (X+C1) == C2 --> X == C2-C1
3831 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3832 return DAG.getSetCC(VT, N0.getOperand(0),
3833 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3834 N0.getValueType()), Cond);
3837 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3838 if (N0.getOpcode() == ISD::XOR)
3839 // If we know that all of the inverted bits are zero, don't bother
3840 // performing the inversion.
3841 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
3842 return DAG.getSetCC(VT, N0.getOperand(0),
3843 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
3844 N0.getValueType()), Cond);
3847 // Turn (C1-X) == C2 --> X == C1-C2
3848 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3849 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3850 return DAG.getSetCC(VT, N0.getOperand(1),
3851 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3852 N0.getValueType()), Cond);
3857 // Simplify (X+Z) == X --> Z == 0
3858 if (N0.getOperand(0) == N1)
3859 return DAG.getSetCC(VT, N0.getOperand(1),
3860 DAG.getConstant(0, N0.getValueType()), Cond);
3861 if (N0.getOperand(1) == N1) {
3862 if (DAG.isCommutativeBinOp(N0.getOpcode()))
3863 return DAG.getSetCC(VT, N0.getOperand(0),
3864 DAG.getConstant(0, N0.getValueType()), Cond);
3866 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3867 // (Z-X) == X --> Z == X<<1
3868 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3870 DAG.getConstant(1,TLI.getShiftAmountTy()));
3871 AddToWorkList(SH.Val);
3872 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3877 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3878 N1.getOpcode() == ISD::XOR) {
3879 // Simplify X == (X+Z) --> Z == 0
3880 if (N1.getOperand(0) == N0) {
3881 return DAG.getSetCC(VT, N1.getOperand(1),
3882 DAG.getConstant(0, N1.getValueType()), Cond);
3883 } else if (N1.getOperand(1) == N0) {
3884 if (DAG.isCommutativeBinOp(N1.getOpcode())) {
3885 return DAG.getSetCC(VT, N1.getOperand(0),
3886 DAG.getConstant(0, N1.getValueType()), Cond);
3888 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3889 // X == (Z-X) --> X<<1 == Z
3890 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3891 DAG.getConstant(1,TLI.getShiftAmountTy()));
3892 AddToWorkList(SH.Val);
3893 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3899 // Fold away ALL boolean setcc's.
3901 if (N0.getValueType() == MVT::i1 && foldBooleans) {
3903 default: assert(0 && "Unknown integer setcc!");
3904 case ISD::SETEQ: // X == Y -> (X^Y)^1
3905 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3906 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
3907 AddToWorkList(Temp.Val);
3909 case ISD::SETNE: // X != Y --> (X^Y)
3910 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3912 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3913 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3914 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3915 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
3916 AddToWorkList(Temp.Val);
3918 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3919 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3920 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3921 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
3922 AddToWorkList(Temp.Val);
3924 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3925 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3926 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3927 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
3928 AddToWorkList(Temp.Val);
3930 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3931 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3932 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3933 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3936 if (VT != MVT::i1) {
3937 AddToWorkList(N0.Val);
3938 // FIXME: If running after legalize, we probably can't do this.
3939 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3944 // Could not fold it.
3948 /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3949 /// return a DAG expression to select that will generate the same value by
3950 /// multiplying by a magic number. See:
3951 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3952 SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
3953 std::vector<SDNode*> Built;
3954 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
3956 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
3962 /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3963 /// return a DAG expression to select that will generate the same value by
3964 /// multiplying by a magic number. See:
3965 /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3966 SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3967 std::vector<SDNode*> Built;
3968 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
3970 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
3976 /// FindBaseOffset - Return true if base is known not to alias with anything
3977 /// but itself. Provides base object and offset as results.
3978 static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
3979 // Assume it is a primitive operation.
3980 Base = Ptr; Offset = 0;
3982 // If it's an adding a simple constant then integrate the offset.
3983 if (Base.getOpcode() == ISD::ADD) {
3984 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
3985 Base = Base.getOperand(0);
3986 Offset += C->getValue();
3990 // If it's any of the following then it can't alias with anything but itself.
3991 return isa<FrameIndexSDNode>(Base) ||
3992 isa<ConstantPoolSDNode>(Base) ||
3993 isa<GlobalAddressSDNode>(Base);
3996 /// isAlias - Return true if there is any possibility that the two addresses
3998 static bool isAlias(SDOperand Ptr1, int64_t Size1, const Value *SrcValue1,
3999 SDOperand Ptr2, int64_t Size2, const Value *SrcValue2) {
4000 // If they are the same then they must be aliases.
4001 if (Ptr1 == Ptr2) return true;
4003 // Gather base node and offset information.
4004 SDOperand Base1, Base2;
4005 int64_t Offset1, Offset2;
4006 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
4007 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
4009 // If they have a same base address then...
4010 if (Base1 == Base2) {
4011 // Check to see if the addresses overlap.
4012 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
4015 // Otherwise they alias if either is unknown.
4016 return !KnownBase1 || !KnownBase2;
4019 /// FindAliasInfo - Extracts the relevant alias information from the memory
4020 /// node. Returns true if the operand was a load.
4021 bool DAGCombiner::FindAliasInfo(SDNode *N,
4022 SDOperand &Ptr, int64_t &Size, const Value *&SrcValue) {
4023 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4024 Ptr = LD->getBasePtr();
4025 Size = MVT::getSizeInBits(LD->getLoadedVT()) >> 3;
4026 SrcValue = LD->getSrcValue();
4028 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4029 #if 1 // FIXME - Switch over after StoreSDNode comes online.
4030 Ptr = ST->getOperand(2);
4031 Size = MVT::getSizeInBits(ST->getOperand(1).getValueType()) >> 3;
4034 Ptr = ST->getBasePtr();
4035 Size = MVT::getSizeInBits(ST->getOperand(1).getValueType()) >> 3;
4036 SrcValue = ST->getSrcValue();
4038 // FIXME - Switch over after StoreSDNode comes online.
4039 } else if (N->getOpcode() == ISD::TRUNCSTORE) {
4040 Ptr = N->getOperand(2);
4041 Size = MVT::getSizeInBits(cast<VTSDNode>(N->getOperand(4))->getVT()) >> 3;
4044 assert(0 && "FindAliasInfo expected a memory operand");
4050 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
4051 /// looking for aliasing nodes and adding them to the Aliases vector.
4052 void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
4053 SmallVector<SDOperand, 8> &Aliases) {
4054 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
4055 std::set<SDNode *> Visited; // Visited node set.
4057 // Get alias information for node.
4060 const Value *SrcValue;
4061 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue);
4064 Chains.push_back(OriginalChain);
4066 // Look at each chain and determine if it is an alias. If so, add it to the
4067 // aliases list. If not, then continue up the chain looking for the next
4069 while (!Chains.empty()) {
4070 SDOperand Chain = Chains.back();
4073 // Don't bother if we've been before.
4074 if (Visited.find(Chain.Val) != Visited.end()) continue;
4075 Visited.insert(Chain.Val);
4077 switch (Chain.getOpcode()) {
4078 case ISD::EntryToken:
4079 // Entry token is ideal chain operand, but handled in FindBetterChain.
4083 // FIXME - Switch over after StoreSDNode comes online.
4084 case ISD::TRUNCSTORE:
4086 // Get alias information for Chain.
4089 const Value *OpSrcValue;
4090 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize, OpSrcValue);
4092 // If chain is alias then stop here.
4093 if (!(IsLoad && IsOpLoad) &&
4094 isAlias(Ptr, Size, SrcValue, OpPtr, OpSize, OpSrcValue)) {
4095 Aliases.push_back(Chain);
4097 // Look further up the chain.
4098 Chains.push_back(Chain.getOperand(0));
4099 // Clean up old chain.
4100 AddToWorkList(Chain.Val);
4105 case ISD::TokenFactor:
4106 // We have to check each of the operands of the token factor, so we queue
4107 // then up. Adding the operands to the queue (stack) in reverse order
4108 // maintains the original order and increases the likelihood that getNode
4109 // will find a matching token factor (CSE.)
4110 for (unsigned n = Chain.getNumOperands(); n;)
4111 Chains.push_back(Chain.getOperand(--n));
4112 // Eliminate the token factor if we can.
4113 AddToWorkList(Chain.Val);
4117 // For all other instructions we will just have to take what we can get.
4118 Aliases.push_back(Chain);
4124 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
4125 /// for a better chain (aliasing node.)
4126 SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
4127 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
4129 // Accumulate all the aliases to this node.
4130 GatherAllAliases(N, OldChain, Aliases);
4132 if (Aliases.size() == 0) {
4133 // If no operands then chain to entry token.
4134 return DAG.getEntryNode();
4135 } else if (Aliases.size() == 1) {
4136 // If a single operand then chain to it. We don't need to revisit it.
4140 // Construct a custom tailored token factor.
4141 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4142 &Aliases[0], Aliases.size());
4144 // Make sure the old chain gets cleaned up.
4145 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
4150 // SelectionDAG::Combine - This is the entry point for the file.
4152 void SelectionDAG::Combine(bool RunningAfterLegalize) {
4153 /// run - This is the main entry point to this class.
4155 DAGCombiner(*this).Run(RunningAfterLegalize);