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: Should add a corresponding version of fold AND with
20 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
23 // FIXME: mul (x, const) -> shifts + adds
24 // FIXME: undef values
25 // FIXME: zero extend when top bits are 0 -> drop it ?
26 // FIXME: make truncate see through SIGN_EXTEND and AND
27 // FIXME: sext_in_reg(setcc) on targets that return zero or one, and where
28 // EVT != MVT::i1 can drop the sext.
29 // FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
30 // FIXME: verify that getNode can't return extends with an operand whose type
31 // is >= to that of the extend.
32 // FIXME: divide by zero is currently left unfolded. do we want to turn this
35 //===----------------------------------------------------------------------===//
37 #define DEBUG_TYPE "dagcombine"
38 #include "llvm/ADT/Statistic.h"
39 #include "llvm/CodeGen/SelectionDAG.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/Target/TargetLowering.h"
47 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
54 // Worklist of all of the nodes that need to be simplified.
55 std::vector<SDNode*> WorkList;
57 /// AddUsersToWorkList - When an instruction is simplified, add all users of
58 /// the instruction to the work lists because they might get more simplified
61 void AddUsersToWorkList(SDNode *N) {
62 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
64 WorkList.push_back(*UI);
67 /// removeFromWorkList - remove all instances of N from the worklist.
68 void removeFromWorkList(SDNode *N) {
69 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
73 /// visit - call the node-specific routine that knows how to fold each
74 /// particular type of node.
75 SDOperand visit(SDNode *N);
77 // Visitation implementation - Implement dag node combining for different
78 // node types. The semantics are as follows:
80 // SDOperand.Val == 0 - No change was made
81 // otherwise - N should be replaced by the returned Operand.
83 SDOperand visitTokenFactor(SDNode *N);
84 SDOperand visitADD(SDNode *N);
85 SDOperand visitSUB(SDNode *N);
86 SDOperand visitMUL(SDNode *N);
87 SDOperand visitSDIV(SDNode *N);
88 SDOperand visitUDIV(SDNode *N);
89 SDOperand visitSREM(SDNode *N);
90 SDOperand visitUREM(SDNode *N);
91 SDOperand visitMULHU(SDNode *N);
92 SDOperand visitMULHS(SDNode *N);
93 SDOperand visitAND(SDNode *N);
94 SDOperand visitOR(SDNode *N);
95 SDOperand visitXOR(SDNode *N);
96 SDOperand visitSHL(SDNode *N);
97 SDOperand visitSRA(SDNode *N);
98 SDOperand visitSRL(SDNode *N);
99 SDOperand visitCTLZ(SDNode *N);
100 SDOperand visitCTTZ(SDNode *N);
101 SDOperand visitCTPOP(SDNode *N);
105 SDOperand visitSIGN_EXTEND(SDNode *N);
106 SDOperand visitZERO_EXTEND(SDNode *N);
107 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
108 SDOperand visitTRUNCATE(SDNode *N);
109 SDOperand visitSINT_TO_FP(SDNode *N);
110 SDOperand visitUINT_TO_FP(SDNode *N);
111 SDOperand visitFP_TO_SINT(SDNode *N);
112 SDOperand visitFP_TO_UINT(SDNode *N);
113 SDOperand visitFP_ROUND(SDNode *N);
114 SDOperand visitFP_ROUND_INREG(SDNode *N);
115 SDOperand visitFP_EXTEND(SDNode *N);
116 SDOperand visitFNEG(SDNode *N);
117 SDOperand visitFABS(SDNode *N);
123 DAGCombiner(SelectionDAG &D)
124 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
126 /// Run - runs the dag combiner on all nodes in the work list
127 void Run(bool RunningAfterLegalize);
131 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
132 /// this predicate to simplify operations downstream. V and Mask are known to
133 /// be the same type.
134 static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
135 const TargetLowering &TLI) {
137 if (Mask == 0) return true;
139 // If we know the result of a setcc has the top bits zero, use this info.
140 switch (Op.getOpcode()) {
142 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
144 // FIXME: teach this about non ZeroOrOne values, such as 0 or -1
145 return ((Mask & 1) == 0) &&
146 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
148 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
149 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
150 case ISD::ZERO_EXTEND:
151 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
152 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
153 case ISD::AssertZext:
154 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
155 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
157 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
158 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
159 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
163 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
164 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
166 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
167 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
169 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
170 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
172 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
173 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
174 uint64_t NewVal = Mask << ShAmt->getValue();
175 SrcBits = MVT::getSizeInBits(Op.getValueType());
176 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
177 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
181 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
182 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
183 uint64_t NewVal = Mask >> ShAmt->getValue();
184 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
190 // Bit counting instructions can not set the high bits of the result
191 // register. The max number of bits sets depends on the input.
192 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
194 // TODO we could handle some SRA cases here.
200 // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
201 // that selects between the values 1 and 0, making it equivalent to a setcc.
202 // Also, set the incoming LHS, RHS, and CC references to the appropriate
203 // nodes based on the type of node we are checking. This simplifies life a
204 // bit for the callers.
205 static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
207 if (N.getOpcode() == ISD::SETCC) {
208 LHS = N.getOperand(0);
209 RHS = N.getOperand(1);
210 CC = N.getOperand(2);
213 if (N.getOpcode() == ISD::SELECT_CC &&
214 N.getOperand(2).getOpcode() == ISD::Constant &&
215 N.getOperand(3).getOpcode() == ISD::Constant &&
216 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
217 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
218 LHS = N.getOperand(0);
219 RHS = N.getOperand(1);
220 CC = N.getOperand(4);
226 // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
227 // one use. If this is true, it allows the users to invert the operation for
228 // free when it is profitable to do so.
229 static bool isOneUseSetCC(SDOperand N) {
230 SDOperand N0, N1, N2;
231 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
236 void DAGCombiner::Run(bool RunningAfterLegalize) {
237 // set the instance variable, so that the various visit routines may use it.
238 AfterLegalize = RunningAfterLegalize;
240 // Add all the dag nodes to the worklist.
241 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
243 // while the worklist isn't empty, inspect the node on the end of it and
244 // try and combine it.
245 while (!WorkList.empty()) {
246 SDNode *N = WorkList.back();
249 // If N has no uses, it is dead. Make sure to revisit all N's operands once
250 // N is deleted from the DAG, since they too may now be dead.
251 // FIXME: is there a better way to keep from deleting the dag root because
252 // we think it has no uses? This works for now...
253 if (N->use_empty() && N != DAG.getRoot().Val) {
254 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
255 WorkList.push_back(N->getOperand(i).Val);
258 removeFromWorkList(N);
262 SDOperand RV = visit(N);
265 // If we get back the same node we passed in, rather than a new node or
266 // zero, we know that the node must have defined multiple values and
267 // CombineTo was used. Since CombineTo takes care of the worklist
268 // mechanics for us, we have no work to do in this case.
270 DEBUG(std::cerr << "\nReplacing "; N->dump();
271 std::cerr << "\nWith: "; RV.Val->dump();
273 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV));
275 // Push the new node and any users onto the worklist
276 WorkList.push_back(RV.Val);
277 AddUsersToWorkList(RV.Val);
279 // Nodes can end up on the worklist more than once. Make sure we do
280 // not process a node that has been replaced.
281 removeFromWorkList(N);
287 SDOperand DAGCombiner::visit(SDNode *N) {
288 switch(N->getOpcode()) {
290 case ISD::TokenFactor: return visitTokenFactor(N);
291 case ISD::ADD: return visitADD(N);
292 case ISD::SUB: return visitSUB(N);
293 case ISD::MUL: return visitMUL(N);
294 case ISD::SDIV: return visitSDIV(N);
295 case ISD::UDIV: return visitUDIV(N);
296 case ISD::SREM: return visitSREM(N);
297 case ISD::UREM: return visitUREM(N);
298 case ISD::MULHU: return visitMULHU(N);
299 case ISD::MULHS: return visitMULHS(N);
300 case ISD::AND: return visitAND(N);
301 case ISD::OR: return visitOR(N);
302 case ISD::XOR: return visitXOR(N);
303 case ISD::SHL: return visitSHL(N);
304 case ISD::SRA: return visitSRA(N);
305 case ISD::SRL: return visitSRL(N);
306 case ISD::CTLZ: return visitCTLZ(N);
307 case ISD::CTTZ: return visitCTTZ(N);
308 case ISD::CTPOP: return visitCTPOP(N);
309 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
310 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
311 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
312 case ISD::TRUNCATE: return visitTRUNCATE(N);
313 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
314 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
315 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
316 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
317 case ISD::FP_ROUND: return visitFP_ROUND(N);
318 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
319 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
320 case ISD::FNEG: return visitFNEG(N);
321 case ISD::FABS: return visitFABS(N);
326 SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
327 // If the token factor has two operands and one is the entry token, replace
328 // the token factor with the other operand.
329 if (N->getNumOperands() == 2) {
330 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
331 return N->getOperand(1);
332 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
333 return N->getOperand(0);
338 SDOperand DAGCombiner::visitADD(SDNode *N) {
339 SDOperand N0 = N->getOperand(0);
340 SDOperand N1 = N->getOperand(1);
341 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
342 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
343 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
344 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
345 MVT::ValueType VT = N0.getValueType();
347 // fold (add c1, c2) -> c1+c2
349 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
350 // canonicalize constant to RHS
355 // fold (add x, 0) -> x
356 if (N1C && N1C->isNullValue())
358 // fold floating point (add c1, c2) -> c1+c2
360 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), VT);
361 // fold (add (add x, c1), c2) -> (add x, c1+c2)
362 if (N1C && N0.getOpcode() == ISD::ADD) {
363 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
364 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
366 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
367 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
369 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
370 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
372 // fold (A + (-B)) -> A-B
373 if (N1.getOpcode() == ISD::FNEG)
374 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(0));
375 // fold ((-A) + B) -> B-A
376 if (N0.getOpcode() == ISD::FNEG)
377 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(0));
378 // fold ((0-A) + B) -> B-A
379 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
380 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
381 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
382 // fold (A + (0-B)) -> A-B
383 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
384 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
385 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
386 // fold (A+(B-A)) -> B for non-fp types
387 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1) &&
388 !MVT::isFloatingPoint(N1.getValueType()))
389 return N1.getOperand(0);
393 SDOperand DAGCombiner::visitSUB(SDNode *N) {
394 SDOperand N0 = N->getOperand(0);
395 SDOperand N1 = N->getOperand(1);
396 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
397 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
398 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
399 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
401 // fold (sub c1, c2) -> c1-c2
403 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
405 // fold (sub x, 0) -> x
406 if (N1C && N1C->isNullValue())
408 // fold floating point (sub c1, c2) -> c1-c2
410 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
413 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
414 !MVT::isFloatingPoint(N1.getValueType()))
415 return N0.getOperand(1);
417 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 &&
418 !MVT::isFloatingPoint(N1.getValueType()))
419 return N0.getOperand(0);
420 // fold (A-(-B)) -> A+B
421 if (N1.getOpcode() == ISD::FNEG)
422 return DAG.getNode(ISD::ADD, N0.getValueType(), N0, N1.getOperand(0));
426 SDOperand DAGCombiner::visitMUL(SDNode *N) {
427 SDOperand N0 = N->getOperand(0);
428 SDOperand N1 = N->getOperand(1);
429 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
430 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
431 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
432 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
433 MVT::ValueType VT = N0.getValueType();
435 // fold (mul c1, c2) -> c1*c2
437 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
439 // canonicalize constant to RHS
444 // fold (mul x, 0) -> 0
445 if (N1C && N1C->isNullValue())
447 // fold (mul x, -1) -> 0-x
448 if (N1C && N1C->isAllOnesValue())
449 return DAG.getNode(ISD::SUB, N->getValueType(0),
450 DAG.getConstant(0, N->getValueType(0)), N0);
451 // fold (mul x, (1 << c)) -> x << c
452 if (N1C && isPowerOf2_64(N1C->getValue()))
453 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
454 DAG.getConstant(Log2_64(N1C->getValue()),
455 TLI.getShiftAmountTy()));
456 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
457 if (N1C && N0.getOpcode() == ISD::MUL) {
458 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
459 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
461 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
462 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
464 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
465 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
467 // fold floating point (mul c1, c2) -> c1*c2
469 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
474 SDOperand DAGCombiner::visitSDIV(SDNode *N) {
475 SDOperand N0 = N->getOperand(0);
476 SDOperand N1 = N->getOperand(1);
477 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
478 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
479 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
480 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
482 // fold (sdiv c1, c2) -> c1/c2
483 if (N0C && N1C && !N1C->isNullValue())
484 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
486 // fold floating point (sdiv c1, c2) -> c1/c2
488 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
493 SDOperand DAGCombiner::visitUDIV(SDNode *N) {
494 SDOperand N0 = N->getOperand(0);
495 SDOperand N1 = N->getOperand(1);
496 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
497 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
499 // fold (udiv c1, c2) -> c1/c2
500 if (N0C && N1C && !N1C->isNullValue())
501 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
503 // fold (udiv x, (1 << c)) -> x >>u c
504 if (N1C && isPowerOf2_64(N1C->getValue()))
505 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
506 DAG.getConstant(Log2_64(N1C->getValue()),
507 TLI.getShiftAmountTy()));
511 SDOperand DAGCombiner::visitSREM(SDNode *N) {
512 SDOperand N0 = N->getOperand(0);
513 SDOperand N1 = N->getOperand(1);
514 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
515 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
516 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
517 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
519 // fold (srem c1, c2) -> c1%c2
520 if (N0C && N1C && !N1C->isNullValue())
521 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
523 // fold floating point (srem c1, c2) -> fmod(c1, c2)
525 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
530 SDOperand DAGCombiner::visitUREM(SDNode *N) {
531 SDOperand N0 = N->getOperand(0);
532 SDOperand N1 = N->getOperand(1);
533 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
534 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
536 // fold (urem c1, c2) -> c1%c2
537 if (N0C && N1C && !N1C->isNullValue())
538 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
540 // FIXME: c2 power of 2 -> mask?
544 SDOperand DAGCombiner::visitMULHS(SDNode *N) {
545 SDOperand N0 = N->getOperand(0);
546 SDOperand N1 = N->getOperand(1);
547 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
549 // fold (mulhs x, 0) -> 0
550 if (N1C && N1C->isNullValue())
552 // fold (mulhs x, 1) -> (sra x, size(x)-1)
553 if (N1C && N1C->getValue() == 1)
554 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
555 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
556 TLI.getShiftAmountTy()));
560 SDOperand DAGCombiner::visitMULHU(SDNode *N) {
561 SDOperand N0 = N->getOperand(0);
562 SDOperand N1 = N->getOperand(1);
563 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
565 // fold (mulhu x, 0) -> 0
566 if (N1C && N1C->isNullValue())
568 // fold (mulhu x, 1) -> 0
569 if (N1C && N1C->getValue() == 1)
570 return DAG.getConstant(0, N0.getValueType());
574 SDOperand DAGCombiner::visitAND(SDNode *N) {
575 SDOperand N0 = N->getOperand(0);
576 SDOperand N1 = N->getOperand(1);
577 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
578 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
579 MVT::ValueType VT = N1.getValueType();
580 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
582 // fold (and c1, c2) -> c1&c2
584 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
585 // canonicalize constant to RHS
590 // fold (and x, -1) -> x
591 if (N1C && N1C->isAllOnesValue())
593 // if (and x, c) is known to be zero, return 0
594 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
595 return DAG.getConstant(0, VT);
596 // fold (and x, c) -> x iff (x & ~c) == 0
597 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
600 // fold (and (and x, c1), c2) -> (and x, c1^c2)
601 if (N1C && N0.getOpcode() == ISD::AND) {
602 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
603 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
605 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
606 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
608 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
609 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
611 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
612 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
613 unsigned ExtendBits =
614 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
615 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
616 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
618 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
619 if (N0.getOpcode() == ISD::OR)
620 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
621 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
626 SDOperand DAGCombiner::visitOR(SDNode *N) {
627 SDOperand N0 = N->getOperand(0);
628 SDOperand N1 = N->getOperand(1);
629 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
630 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
631 MVT::ValueType VT = N1.getValueType();
632 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
634 // fold (or c1, c2) -> c1|c2
636 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
638 // canonicalize constant to RHS
643 // fold (or x, 0) -> x
644 if (N1C && N1C->isNullValue())
646 // fold (or x, -1) -> -1
647 if (N1C && N1C->isAllOnesValue())
649 // fold (or x, c) -> c iff (x & ~c) == 0
650 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
653 // fold (or (or x, c1), c2) -> (or x, c1|c2)
654 if (N1C && N0.getOpcode() == ISD::OR) {
655 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
656 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
658 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
659 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
661 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
662 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
667 SDOperand DAGCombiner::visitXOR(SDNode *N) {
668 SDOperand N0 = N->getOperand(0);
669 SDOperand N1 = N->getOperand(1);
670 SDOperand LHS, RHS, CC;
671 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
672 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
673 MVT::ValueType VT = N0.getValueType();
675 // fold (xor c1, c2) -> c1^c2
677 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
678 // canonicalize constant to RHS
683 // fold (xor x, 0) -> x
684 if (N1C && N1C->isNullValue())
686 // fold !(x cc y) -> (x !cc y)
687 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
688 bool isInt = MVT::isInteger(LHS.getValueType());
689 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
691 if (N0.getOpcode() == ISD::SETCC)
692 return DAG.getSetCC(VT, LHS, RHS, NotCC);
693 if (N0.getOpcode() == ISD::SELECT_CC)
694 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
695 assert(0 && "Unhandled SetCC Equivalent!");
698 // fold !(x or y) -> (!x and !y) iff x or y are setcc
699 if (N1C && N1C->getValue() == 1 &&
700 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
701 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
702 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
703 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
704 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
705 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
706 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
707 return DAG.getNode(NewOpcode, VT, LHS, RHS);
710 // fold !(x or y) -> (!x and !y) iff x or y are constants
711 if (N1C && N1C->isAllOnesValue() &&
712 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
713 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
714 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
715 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
716 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
717 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
718 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
719 return DAG.getNode(NewOpcode, VT, LHS, RHS);
722 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
723 if (N1C && N0.getOpcode() == ISD::XOR) {
724 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
725 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
727 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
728 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
730 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
731 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
733 // fold (xor x, x) -> 0
735 return DAG.getConstant(0, VT);
739 SDOperand DAGCombiner::visitSHL(SDNode *N) {
740 SDOperand N0 = N->getOperand(0);
741 SDOperand N1 = N->getOperand(1);
742 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
743 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
744 MVT::ValueType VT = N0.getValueType();
745 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
747 // fold (shl c1, c2) -> c1<<c2
749 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
750 // fold (shl 0, x) -> 0
751 if (N0C && N0C->isNullValue())
753 // fold (shl x, c >= size(x)) -> undef
754 if (N1C && N1C->getValue() >= OpSizeInBits)
755 return DAG.getNode(ISD::UNDEF, VT);
756 // fold (shl x, 0) -> x
757 if (N1C && N1C->isNullValue())
759 // if (shl x, c) is known to be zero, return 0
760 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
761 return DAG.getConstant(0, VT);
762 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
763 if (N1C && N0.getOpcode() == ISD::SHL &&
764 N0.getOperand(1).getOpcode() == ISD::Constant) {
765 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
766 uint64_t c2 = N1C->getValue();
767 if (c1 + c2 > OpSizeInBits)
768 return DAG.getConstant(0, VT);
769 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
770 DAG.getConstant(c1 + c2, N1.getValueType()));
772 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
773 // (srl (and x, -1 << c1), c1-c2)
774 if (N1C && N0.getOpcode() == ISD::SRL &&
775 N0.getOperand(1).getOpcode() == ISD::Constant) {
776 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
777 uint64_t c2 = N1C->getValue();
778 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
779 DAG.getConstant(~0ULL << c1, VT));
781 return DAG.getNode(ISD::SHL, VT, Mask,
782 DAG.getConstant(c2-c1, N1.getValueType()));
784 return DAG.getNode(ISD::SRL, VT, Mask,
785 DAG.getConstant(c1-c2, N1.getValueType()));
787 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
788 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
789 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
790 DAG.getConstant(~0ULL << N1C->getValue(), VT));
794 SDOperand DAGCombiner::visitSRA(SDNode *N) {
795 SDOperand N0 = N->getOperand(0);
796 SDOperand N1 = N->getOperand(1);
797 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
798 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
799 MVT::ValueType VT = N0.getValueType();
800 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
802 // fold (sra c1, c2) -> c1>>c2
804 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
805 // fold (sra 0, x) -> 0
806 if (N0C && N0C->isNullValue())
808 // fold (sra -1, x) -> -1
809 if (N0C && N0C->isAllOnesValue())
811 // fold (sra x, c >= size(x)) -> undef
812 if (N1C && N1C->getValue() >= OpSizeInBits)
813 return DAG.getNode(ISD::UNDEF, VT);
814 // fold (sra x, 0) -> x
815 if (N1C && N1C->isNullValue())
817 // If the sign bit is known to be zero, switch this to a SRL.
818 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
819 return DAG.getNode(ISD::SRL, VT, N0, N1);
823 SDOperand DAGCombiner::visitSRL(SDNode *N) {
824 SDOperand N0 = N->getOperand(0);
825 SDOperand N1 = N->getOperand(1);
826 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
827 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
828 MVT::ValueType VT = N0.getValueType();
829 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
831 // fold (srl c1, c2) -> c1 >>u c2
833 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
834 // fold (srl 0, x) -> 0
835 if (N0C && N0C->isNullValue())
837 // fold (srl x, c >= size(x)) -> undef
838 if (N1C && N1C->getValue() >= OpSizeInBits)
839 return DAG.getNode(ISD::UNDEF, VT);
840 // fold (srl x, 0) -> x
841 if (N1C && N1C->isNullValue())
843 // if (srl x, c) is known to be zero, return 0
844 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
845 return DAG.getConstant(0, VT);
846 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
847 if (N1C && N0.getOpcode() == ISD::SRL &&
848 N0.getOperand(1).getOpcode() == ISD::Constant) {
849 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
850 uint64_t c2 = N1C->getValue();
851 if (c1 + c2 > OpSizeInBits)
852 return DAG.getConstant(0, VT);
853 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
854 DAG.getConstant(c1 + c2, N1.getValueType()));
859 SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
860 SDOperand N0 = N->getOperand(0);
861 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
863 // fold (ctlz c1) -> c2
865 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
870 SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
871 SDOperand N0 = N->getOperand(0);
872 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
874 // fold (cttz c1) -> c2
876 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
881 SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
882 SDOperand N0 = N->getOperand(0);
883 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
885 // fold (ctpop c1) -> c2
887 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
892 SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
893 SDOperand N0 = N->getOperand(0);
894 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
895 MVT::ValueType VT = N->getValueType(0);
897 // fold (sext c1) -> c1
899 return DAG.getConstant(N0C->getSignExtended(), VT);
900 // fold (sext (sext x)) -> (sext x)
901 if (N0.getOpcode() == ISD::SIGN_EXTEND)
902 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
906 SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
907 SDOperand N0 = N->getOperand(0);
908 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
909 MVT::ValueType VT = N->getValueType(0);
911 // fold (zext c1) -> c1
913 return DAG.getConstant(N0C->getValue(), VT);
914 // fold (zext (zext x)) -> (zext x)
915 if (N0.getOpcode() == ISD::ZERO_EXTEND)
916 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
920 SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
921 SDOperand N0 = N->getOperand(0);
922 SDOperand N1 = N->getOperand(1);
923 SDOperand LHS, RHS, CC;
924 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
925 MVT::ValueType VT = N->getValueType(0);
926 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
928 // fold (sext_in_reg c1) -> c1
930 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
931 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
933 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
934 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
935 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
938 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
939 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
940 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
941 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
943 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
944 if (N0.getOpcode() == ISD::AssertSext &&
945 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
948 // fold (sext_in_reg (sextload x)) -> (sextload x)
949 if (N0.getOpcode() == ISD::SEXTLOAD &&
950 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
953 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
954 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
955 if (N0.getOpcode() == ISD::SETCC &&
956 TLI.getSetCCResultContents() ==
957 TargetLowering::ZeroOrNegativeOneSetCCResult)
959 // FIXME: this code is currently just ported over from SelectionDAG.cpp
960 // we probably actually want to handle this in two pieces. Rather than
961 // checking all the top bits for zero, just check the sign bit here and turn
962 // it into a zero extend inreg (AND with constant).
963 // then, let the code for AND figure out if the mask is superfluous rather
964 // than doing so here.
965 if (N0.getOpcode() == ISD::AND &&
966 N0.getOperand(1).getOpcode() == ISD::Constant) {
967 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
968 unsigned NumBits = MVT::getSizeInBits(EVT);
969 if ((Mask & (~0ULL << (NumBits-1))) == 0)
975 SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
976 SDOperand N0 = N->getOperand(0);
977 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
978 MVT::ValueType VT = N->getValueType(0);
981 if (N0.getValueType() == N->getValueType(0))
983 // fold (truncate c1) -> c1
985 return DAG.getConstant(N0C->getValue(), VT);
986 // fold (truncate (truncate x)) -> (truncate x)
987 if (N0.getOpcode() == ISD::TRUNCATE)
988 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
989 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
990 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
991 if (N0.getValueType() < VT)
992 // if the source is smaller than the dest, we still need an extend
993 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
994 else if (N0.getValueType() > VT)
995 // if the source is larger than the dest, than we just need the truncate
996 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
998 // if the source and dest are the same type, we can drop both the extend
1000 return N0.getOperand(0);
1005 SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
1006 SDOperand N0 = N->getOperand(0);
1007 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1009 // fold (sint_to_fp c1) -> c1fp
1011 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1015 SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
1016 SDOperand N0 = N->getOperand(0);
1017 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1019 // fold (uint_to_fp c1) -> c1fp
1021 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1025 SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
1026 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
1028 // fold (fp_to_sint c1fp) -> c1
1030 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1034 SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
1035 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
1037 // fold (fp_to_uint c1fp) -> c1
1039 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1043 SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
1044 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
1046 // fold (fp_round c1fp) -> c1fp
1048 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1052 SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
1053 SDOperand N0 = N->getOperand(0);
1054 MVT::ValueType VT = N->getValueType(0);
1055 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1056 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1058 // fold (fp_round_inreg c1fp) -> c1fp
1060 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
1061 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
1066 SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
1067 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
1069 // fold (fp_extend c1fp) -> c1fp
1071 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1075 SDOperand DAGCombiner::visitFNEG(SDNode *N) {
1076 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
1077 // fold (neg c1) -> -c1
1079 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
1080 // fold (neg (sub x, y)) -> (sub y, x)
1081 if (N->getOperand(0).getOpcode() == ISD::SUB)
1082 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
1084 // fold (neg (neg x)) -> x
1085 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1086 return N->getOperand(0).getOperand(0);
1090 SDOperand DAGCombiner::visitFABS(SDNode *N) {
1091 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
1092 // fold (fabs c1) -> fabs(c1)
1094 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
1095 // fold (fabs (fabs x)) -> (fabs x)
1096 if (N->getOperand(0).getOpcode() == ISD::FABS)
1097 return N->getOperand(0);
1098 // fold (fabs (fneg x)) -> (fabs x)
1099 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1100 return DAG.getNode(ISD::FABS, N->getValueType(0),
1101 N->getOperand(0).getOperand(0));
1105 // SelectionDAG::Combine - This is the entry point for the file.
1107 void SelectionDAG::Combine(bool RunningAfterLegalize) {
1108 /// run - This is the main entry point to this class.
1110 DAGCombiner(*this).Run(RunningAfterLegalize);