Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1 | //===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===// |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 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. |
| 12 | // |
| 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. |
| 18 | // |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 19 | // FIXME: select C, pow2, pow2 -> something smart |
| 20 | // FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z) |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 21 | // FIXME: Dead stores -> nuke |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 22 | // FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 23 | // FIXME: mul (x, const) -> shifts + adds |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 24 | // FIXME: undef values |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 25 | // FIXME: make truncate see through SIGN_EXTEND and AND |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 26 | // FIXME: divide by zero is currently left unfolded. do we want to turn this |
| 27 | // into an undef? |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 28 | // FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 29 | // |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | #define DEBUG_TYPE "dagcombine" |
| 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include "llvm/CodeGen/SelectionDAG.h" |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MathExtras.h" |
| 37 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Compiler.h" |
Chris Lattner | a500fc6 | 2005-09-09 23:53:39 +0000 | [diff] [blame] | 39 | #include <algorithm> |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 40 | #include <cmath> |
Chris Lattner | 2c2c6c6 | 2006-01-22 23:41:00 +0000 | [diff] [blame] | 41 | #include <iostream> |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
| 44 | namespace { |
Andrew Lenharth | ae6153f | 2006-07-20 17:43:27 +0000 | [diff] [blame] | 45 | static Statistic<> NodesCombined ("dagcombiner", |
| 46 | "Number of dag nodes combined"); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 360e820 | 2006-06-28 21:58:30 +0000 | [diff] [blame] | 48 | class VISIBILITY_HIDDEN DAGCombiner { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 49 | SelectionDAG &DAG; |
| 50 | TargetLowering &TLI; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 51 | bool AfterLegalize; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 52 | |
| 53 | // Worklist of all of the nodes that need to be simplified. |
| 54 | std::vector<SDNode*> WorkList; |
| 55 | |
| 56 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 57 | /// the instruction to the work lists because they might get more simplified |
| 58 | /// now. |
| 59 | /// |
| 60 | void AddUsersToWorkList(SDNode *N) { |
| 61 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 62 | UI != UE; ++UI) |
| 63 | WorkList.push_back(*UI); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /// removeFromWorkList - remove all instances of N from the worklist. |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 67 | /// |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 68 | void removeFromWorkList(SDNode *N) { |
| 69 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N), |
| 70 | WorkList.end()); |
| 71 | } |
| 72 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 73 | public: |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 74 | void AddToWorkList(SDNode *N) { |
| 75 | WorkList.push_back(N); |
| 76 | } |
| 77 | |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 78 | SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) { |
| 79 | assert(N->getNumValues() == NumTo && "Broken CombineTo call!"); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 80 | ++NodesCombined; |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 81 | DEBUG(std::cerr << "\nReplacing "; N->dump(); |
Evan Cheng | 60e8c71 | 2006-05-09 06:55:15 +0000 | [diff] [blame] | 82 | std::cerr << "\nWith: "; To[0].Val->dump(&DAG); |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 83 | std::cerr << " and " << NumTo-1 << " other values\n"); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 84 | std::vector<SDNode*> NowDead; |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 85 | DAG.ReplaceAllUsesWith(N, To, &NowDead); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 86 | |
| 87 | // Push the new nodes and any users onto the worklist |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 88 | for (unsigned i = 0, e = NumTo; i != e; ++i) { |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 89 | WorkList.push_back(To[i].Val); |
| 90 | AddUsersToWorkList(To[i].Val); |
| 91 | } |
| 92 | |
| 93 | // Nodes can end up on the worklist more than once. Make sure we do |
| 94 | // not process a node that has been replaced. |
| 95 | removeFromWorkList(N); |
| 96 | for (unsigned i = 0, e = NowDead.size(); i != e; ++i) |
| 97 | removeFromWorkList(NowDead[i]); |
| 98 | |
| 99 | // Finally, since the node is now dead, remove it from the graph. |
| 100 | DAG.DeleteNode(N); |
| 101 | return SDOperand(N, 0); |
| 102 | } |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 104 | SDOperand CombineTo(SDNode *N, SDOperand Res) { |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 105 | return CombineTo(N, &Res, 1); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) { |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 109 | SDOperand To[] = { Res0, Res1 }; |
| 110 | return CombineTo(N, To, 2); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 111 | } |
| 112 | private: |
| 113 | |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 114 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
Chris Lattner | b2742f4 | 2006-03-01 19:55:35 +0000 | [diff] [blame] | 115 | /// it can be simplified or if things it uses can be simplified by bit |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 116 | /// propagation. If so, return true. |
| 117 | bool SimplifyDemandedBits(SDOperand Op) { |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 118 | TargetLowering::TargetLoweringOpt TLO(DAG); |
| 119 | uint64_t KnownZero, KnownOne; |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 120 | uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType()); |
| 121 | if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) |
| 122 | return false; |
| 123 | |
| 124 | // Revisit the node. |
| 125 | WorkList.push_back(Op.Val); |
| 126 | |
| 127 | // Replace the old value with the new one. |
| 128 | ++NodesCombined; |
| 129 | DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump(); |
Evan Cheng | 60e8c71 | 2006-05-09 06:55:15 +0000 | [diff] [blame] | 130 | std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG)); |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 131 | |
| 132 | std::vector<SDNode*> NowDead; |
| 133 | DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead); |
| 134 | |
Chris Lattner | 7d20d39 | 2006-02-20 06:51:04 +0000 | [diff] [blame] | 135 | // Push the new node and any (possibly new) users onto the worklist. |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 136 | WorkList.push_back(TLO.New.Val); |
| 137 | AddUsersToWorkList(TLO.New.Val); |
| 138 | |
| 139 | // Nodes can end up on the worklist more than once. Make sure we do |
| 140 | // not process a node that has been replaced. |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 141 | for (unsigned i = 0, e = NowDead.size(); i != e; ++i) |
| 142 | removeFromWorkList(NowDead[i]); |
| 143 | |
Chris Lattner | 7d20d39 | 2006-02-20 06:51:04 +0000 | [diff] [blame] | 144 | // Finally, if the node is now dead, remove it from the graph. The node |
| 145 | // may not be dead if the replacement process recursively simplified to |
| 146 | // something else needing this node. |
| 147 | if (TLO.Old.Val->use_empty()) { |
| 148 | removeFromWorkList(TLO.Old.Val); |
| 149 | DAG.DeleteNode(TLO.Old.Val); |
| 150 | } |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 151 | return true; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 152 | } |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 153 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 154 | /// visit - call the node-specific routine that knows how to fold each |
| 155 | /// particular type of node. |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 156 | SDOperand visit(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 157 | |
| 158 | // Visitation implementation - Implement dag node combining for different |
| 159 | // node types. The semantics are as follows: |
| 160 | // Return Value: |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 161 | // SDOperand.Val == 0 - No change was made |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 162 | // SDOperand.Val == N - N was replaced, is dead, and is already handled. |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 163 | // otherwise - N should be replaced by the returned Operand. |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 164 | // |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 165 | SDOperand visitTokenFactor(SDNode *N); |
| 166 | SDOperand visitADD(SDNode *N); |
| 167 | SDOperand visitSUB(SDNode *N); |
| 168 | SDOperand visitMUL(SDNode *N); |
| 169 | SDOperand visitSDIV(SDNode *N); |
| 170 | SDOperand visitUDIV(SDNode *N); |
| 171 | SDOperand visitSREM(SDNode *N); |
| 172 | SDOperand visitUREM(SDNode *N); |
| 173 | SDOperand visitMULHU(SDNode *N); |
| 174 | SDOperand visitMULHS(SDNode *N); |
| 175 | SDOperand visitAND(SDNode *N); |
| 176 | SDOperand visitOR(SDNode *N); |
| 177 | SDOperand visitXOR(SDNode *N); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 178 | SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 179 | SDOperand visitSHL(SDNode *N); |
| 180 | SDOperand visitSRA(SDNode *N); |
| 181 | SDOperand visitSRL(SDNode *N); |
| 182 | SDOperand visitCTLZ(SDNode *N); |
| 183 | SDOperand visitCTTZ(SDNode *N); |
| 184 | SDOperand visitCTPOP(SDNode *N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 185 | SDOperand visitSELECT(SDNode *N); |
| 186 | SDOperand visitSELECT_CC(SDNode *N); |
| 187 | SDOperand visitSETCC(SDNode *N); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 188 | SDOperand visitSIGN_EXTEND(SDNode *N); |
| 189 | SDOperand visitZERO_EXTEND(SDNode *N); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 190 | SDOperand visitANY_EXTEND(SDNode *N); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 191 | SDOperand visitSIGN_EXTEND_INREG(SDNode *N); |
| 192 | SDOperand visitTRUNCATE(SDNode *N); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 193 | SDOperand visitBIT_CONVERT(SDNode *N); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 194 | SDOperand visitVBIT_CONVERT(SDNode *N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 195 | SDOperand visitFADD(SDNode *N); |
| 196 | SDOperand visitFSUB(SDNode *N); |
| 197 | SDOperand visitFMUL(SDNode *N); |
| 198 | SDOperand visitFDIV(SDNode *N); |
| 199 | SDOperand visitFREM(SDNode *N); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 200 | SDOperand visitFCOPYSIGN(SDNode *N); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 201 | SDOperand visitSINT_TO_FP(SDNode *N); |
| 202 | SDOperand visitUINT_TO_FP(SDNode *N); |
| 203 | SDOperand visitFP_TO_SINT(SDNode *N); |
| 204 | SDOperand visitFP_TO_UINT(SDNode *N); |
| 205 | SDOperand visitFP_ROUND(SDNode *N); |
| 206 | SDOperand visitFP_ROUND_INREG(SDNode *N); |
| 207 | SDOperand visitFP_EXTEND(SDNode *N); |
| 208 | SDOperand visitFNEG(SDNode *N); |
| 209 | SDOperand visitFABS(SDNode *N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 210 | SDOperand visitBRCOND(SDNode *N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 211 | SDOperand visitBR_CC(SDNode *N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 212 | SDOperand visitLOAD(SDNode *N); |
Chris Lattner | 29cd7db | 2006-03-31 18:10:41 +0000 | [diff] [blame] | 213 | SDOperand visitXEXTLOAD(SDNode *N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 214 | SDOperand visitSTORE(SDNode *N); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 215 | SDOperand visitINSERT_VECTOR_ELT(SDNode *N); |
| 216 | SDOperand visitVINSERT_VECTOR_ELT(SDNode *N); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 217 | SDOperand visitVBUILD_VECTOR(SDNode *N); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 218 | SDOperand visitVECTOR_SHUFFLE(SDNode *N); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 219 | SDOperand visitVVECTOR_SHUFFLE(SDNode *N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 220 | |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 221 | SDOperand XformToShuffleWithZero(SDNode *N); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 222 | SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS); |
| 223 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 224 | bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 225 | SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 226 | SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2); |
| 227 | SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2, |
| 228 | SDOperand N3, ISD::CondCode CC); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 229 | SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1, |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 230 | ISD::CondCode Cond, bool foldBooleans = true); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 231 | SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 232 | SDOperand BuildSDIV(SDNode *N); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame^] | 233 | SDOperand BuildUDIV(SDNode *N); |
| 234 | SDNode *MatchRotate(SDOperand LHS, SDOperand RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 235 | public: |
| 236 | DAGCombiner(SelectionDAG &D) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 237 | : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {} |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 238 | |
| 239 | /// Run - runs the dag combiner on all nodes in the work list |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 240 | void Run(bool RunningAfterLegalize); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 241 | }; |
| 242 | } |
| 243 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 244 | //===----------------------------------------------------------------------===// |
| 245 | // TargetLowering::DAGCombinerInfo implementation |
| 246 | //===----------------------------------------------------------------------===// |
| 247 | |
| 248 | void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) { |
| 249 | ((DAGCombiner*)DC)->AddToWorkList(N); |
| 250 | } |
| 251 | |
| 252 | SDOperand TargetLowering::DAGCombinerInfo:: |
| 253 | CombineTo(SDNode *N, const std::vector<SDOperand> &To) { |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 254 | return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size()); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | SDOperand TargetLowering::DAGCombinerInfo:: |
| 258 | CombineTo(SDNode *N, SDOperand Res) { |
| 259 | return ((DAGCombiner*)DC)->CombineTo(N, Res); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | SDOperand TargetLowering::DAGCombinerInfo:: |
| 264 | CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) { |
| 265 | return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | //===----------------------------------------------------------------------===// |
| 272 | |
| 273 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 274 | // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc |
| 275 | // that selects between the values 1 and 0, making it equivalent to a setcc. |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 276 | // Also, set the incoming LHS, RHS, and CC references to the appropriate |
| 277 | // nodes based on the type of node we are checking. This simplifies life a |
| 278 | // bit for the callers. |
| 279 | static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS, |
| 280 | SDOperand &CC) { |
| 281 | if (N.getOpcode() == ISD::SETCC) { |
| 282 | LHS = N.getOperand(0); |
| 283 | RHS = N.getOperand(1); |
| 284 | CC = N.getOperand(2); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 285 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 286 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 287 | if (N.getOpcode() == ISD::SELECT_CC && |
| 288 | N.getOperand(2).getOpcode() == ISD::Constant && |
| 289 | N.getOperand(3).getOpcode() == ISD::Constant && |
| 290 | cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 291 | cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) { |
| 292 | LHS = N.getOperand(0); |
| 293 | RHS = N.getOperand(1); |
| 294 | CC = N.getOperand(4); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 295 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 296 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 300 | // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only |
| 301 | // one use. If this is true, it allows the users to invert the operation for |
| 302 | // free when it is profitable to do so. |
| 303 | static bool isOneUseSetCC(SDOperand N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 304 | SDOperand N0, N1, N2; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 305 | if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse()) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 306 | return true; |
| 307 | return false; |
| 308 | } |
| 309 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 310 | SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){ |
| 311 | MVT::ValueType VT = N0.getValueType(); |
| 312 | // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use |
| 313 | // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2)) |
| 314 | if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) { |
| 315 | if (isa<ConstantSDNode>(N1)) { |
| 316 | SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 317 | AddToWorkList(OpNode.Val); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 318 | return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0)); |
| 319 | } else if (N0.hasOneUse()) { |
| 320 | SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 321 | AddToWorkList(OpNode.Val); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 322 | return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1)); |
| 323 | } |
| 324 | } |
| 325 | // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use |
| 326 | // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2)) |
| 327 | if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) { |
| 328 | if (isa<ConstantSDNode>(N0)) { |
| 329 | SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 330 | AddToWorkList(OpNode.Val); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 331 | return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0)); |
| 332 | } else if (N1.hasOneUse()) { |
| 333 | SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 334 | AddToWorkList(OpNode.Val); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 335 | return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1)); |
| 336 | } |
| 337 | } |
| 338 | return SDOperand(); |
| 339 | } |
| 340 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 341 | void DAGCombiner::Run(bool RunningAfterLegalize) { |
| 342 | // set the instance variable, so that the various visit routines may use it. |
| 343 | AfterLegalize = RunningAfterLegalize; |
| 344 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 345 | // Add all the dag nodes to the worklist. |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 346 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 347 | E = DAG.allnodes_end(); I != E; ++I) |
| 348 | WorkList.push_back(I); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 350 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 351 | // to the root node, preventing it from being deleted, and tracking any |
| 352 | // changes of the root. |
| 353 | HandleSDNode Dummy(DAG.getRoot()); |
| 354 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 355 | |
| 356 | /// DagCombineInfo - Expose the DAG combiner to the target combiner impls. |
| 357 | TargetLowering::DAGCombinerInfo |
| 358 | DagCombineInfo(DAG, !RunningAfterLegalize, this); |
| 359 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 360 | // while the worklist isn't empty, inspect the node on the end of it and |
| 361 | // try and combine it. |
| 362 | while (!WorkList.empty()) { |
| 363 | SDNode *N = WorkList.back(); |
| 364 | WorkList.pop_back(); |
| 365 | |
| 366 | // If N has no uses, it is dead. Make sure to revisit all N's operands once |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 367 | // N is deleted from the DAG, since they too may now be dead or may have a |
| 368 | // reduced number of uses, allowing other xforms. |
| 369 | if (N->use_empty() && N != &Dummy) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 370 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 371 | WorkList.push_back(N->getOperand(i).Val); |
| 372 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 373 | removeFromWorkList(N); |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 374 | DAG.DeleteNode(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 375 | continue; |
| 376 | } |
| 377 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 378 | SDOperand RV = visit(N); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 379 | |
| 380 | // If nothing happened, try a target-specific DAG combine. |
| 381 | if (RV.Val == 0) { |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 382 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 383 | "Node was deleted but visit returned NULL!"); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 384 | if (N->getOpcode() >= ISD::BUILTIN_OP_END || |
| 385 | TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) |
| 386 | RV = TLI.PerformDAGCombine(N, DagCombineInfo); |
| 387 | } |
| 388 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 389 | if (RV.Val) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 390 | ++NodesCombined; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 391 | // If we get back the same node we passed in, rather than a new node or |
| 392 | // zero, we know that the node must have defined multiple values and |
| 393 | // CombineTo was used. Since CombineTo takes care of the worklist |
| 394 | // mechanics for us, we have no work to do in this case. |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 395 | if (RV.Val != N) { |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 396 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 397 | RV.Val->getOpcode() != ISD::DELETED_NODE && |
| 398 | "Node was deleted but visit returned new node!"); |
| 399 | |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 400 | DEBUG(std::cerr << "\nReplacing "; N->dump(); |
Evan Cheng | 60e8c71 | 2006-05-09 06:55:15 +0000 | [diff] [blame] | 401 | std::cerr << "\nWith: "; RV.Val->dump(&DAG); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 402 | std::cerr << '\n'); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 403 | std::vector<SDNode*> NowDead; |
Chris Lattner | b9ea4a3 | 2006-08-11 17:46:28 +0000 | [diff] [blame] | 404 | SDOperand OpV = RV; |
| 405 | DAG.ReplaceAllUsesWith(N, &OpV, &NowDead); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 406 | |
| 407 | // Push the new node and any users onto the worklist |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 408 | WorkList.push_back(RV.Val); |
| 409 | AddUsersToWorkList(RV.Val); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 410 | |
| 411 | // Nodes can end up on the worklist more than once. Make sure we do |
| 412 | // not process a node that has been replaced. |
| 413 | removeFromWorkList(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 414 | for (unsigned i = 0, e = NowDead.size(); i != e; ++i) |
| 415 | removeFromWorkList(NowDead[i]); |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 416 | |
| 417 | // Finally, since the node is now dead, remove it from the graph. |
| 418 | DAG.DeleteNode(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 419 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 422 | |
| 423 | // If the root changed (e.g. it was a dead load, update the root). |
| 424 | DAG.setRoot(Dummy.getValue()); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 427 | SDOperand DAGCombiner::visit(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 428 | switch(N->getOpcode()) { |
| 429 | default: break; |
Nate Begeman | 4942a96 | 2005-09-01 00:33:32 +0000 | [diff] [blame] | 430 | case ISD::TokenFactor: return visitTokenFactor(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 431 | case ISD::ADD: return visitADD(N); |
| 432 | case ISD::SUB: return visitSUB(N); |
| 433 | case ISD::MUL: return visitMUL(N); |
| 434 | case ISD::SDIV: return visitSDIV(N); |
| 435 | case ISD::UDIV: return visitUDIV(N); |
| 436 | case ISD::SREM: return visitSREM(N); |
| 437 | case ISD::UREM: return visitUREM(N); |
| 438 | case ISD::MULHU: return visitMULHU(N); |
| 439 | case ISD::MULHS: return visitMULHS(N); |
| 440 | case ISD::AND: return visitAND(N); |
| 441 | case ISD::OR: return visitOR(N); |
| 442 | case ISD::XOR: return visitXOR(N); |
| 443 | case ISD::SHL: return visitSHL(N); |
| 444 | case ISD::SRA: return visitSRA(N); |
| 445 | case ISD::SRL: return visitSRL(N); |
| 446 | case ISD::CTLZ: return visitCTLZ(N); |
| 447 | case ISD::CTTZ: return visitCTTZ(N); |
| 448 | case ISD::CTPOP: return visitCTPOP(N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 449 | case ISD::SELECT: return visitSELECT(N); |
| 450 | case ISD::SELECT_CC: return visitSELECT_CC(N); |
| 451 | case ISD::SETCC: return visitSETCC(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 452 | case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); |
| 453 | case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 454 | case ISD::ANY_EXTEND: return visitANY_EXTEND(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 455 | case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); |
| 456 | case ISD::TRUNCATE: return visitTRUNCATE(N); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 457 | case ISD::BIT_CONVERT: return visitBIT_CONVERT(N); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 458 | case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 459 | case ISD::FADD: return visitFADD(N); |
| 460 | case ISD::FSUB: return visitFSUB(N); |
| 461 | case ISD::FMUL: return visitFMUL(N); |
| 462 | case ISD::FDIV: return visitFDIV(N); |
| 463 | case ISD::FREM: return visitFREM(N); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 464 | case ISD::FCOPYSIGN: return visitFCOPYSIGN(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 465 | case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); |
| 466 | case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); |
| 467 | case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); |
| 468 | case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); |
| 469 | case ISD::FP_ROUND: return visitFP_ROUND(N); |
| 470 | case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); |
| 471 | case ISD::FP_EXTEND: return visitFP_EXTEND(N); |
| 472 | case ISD::FNEG: return visitFNEG(N); |
| 473 | case ISD::FABS: return visitFABS(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 474 | case ISD::BRCOND: return visitBRCOND(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 475 | case ISD::BR_CC: return visitBR_CC(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 476 | case ISD::LOAD: return visitLOAD(N); |
Chris Lattner | 29cd7db | 2006-03-31 18:10:41 +0000 | [diff] [blame] | 477 | case ISD::EXTLOAD: |
| 478 | case ISD::SEXTLOAD: |
| 479 | case ISD::ZEXTLOAD: return visitXEXTLOAD(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 480 | case ISD::STORE: return visitSTORE(N); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 481 | case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N); |
| 482 | case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 483 | case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 484 | case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 485 | case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 486 | case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD); |
| 487 | case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB); |
| 488 | case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL); |
| 489 | case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV); |
| 490 | case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV); |
| 491 | case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND); |
| 492 | case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR); |
| 493 | case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 494 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 495 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 498 | SDOperand DAGCombiner::visitTokenFactor(SDNode *N) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 499 | SmallVector<SDOperand, 8> Ops; |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 500 | bool Changed = false; |
| 501 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 502 | // If the token factor has two operands and one is the entry token, replace |
| 503 | // the token factor with the other operand. |
| 504 | if (N->getNumOperands() == 2) { |
Chris Lattner | 21a57dc | 2006-05-12 05:01:37 +0000 | [diff] [blame] | 505 | if (N->getOperand(0).getOpcode() == ISD::EntryToken || |
| 506 | N->getOperand(0) == N->getOperand(1)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 507 | return N->getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 508 | if (N->getOperand(1).getOpcode() == ISD::EntryToken) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 509 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 510 | } |
Chris Lattner | 24edbb7 | 2005-10-13 22:10:05 +0000 | [diff] [blame] | 511 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 512 | // fold (tokenfactor (tokenfactor)) -> tokenfactor |
| 513 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 514 | SDOperand Op = N->getOperand(i); |
| 515 | if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) { |
Chris Lattner | ac0f8f2 | 2006-03-13 18:37:30 +0000 | [diff] [blame] | 516 | AddToWorkList(Op.Val); // Remove dead node. |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 517 | Changed = true; |
| 518 | for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j) |
| 519 | Ops.push_back(Op.getOperand(j)); |
Chris Lattner | 21a57dc | 2006-05-12 05:01:37 +0000 | [diff] [blame] | 520 | } else if (i == 0 || N->getOperand(i) != N->getOperand(i-1)) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 521 | Ops.push_back(Op); |
Chris Lattner | 21a57dc | 2006-05-12 05:01:37 +0000 | [diff] [blame] | 522 | } else { |
| 523 | // Deleted an operand that was the same as the last one. |
| 524 | Changed = true; |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | if (Changed) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 528 | return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size()); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 529 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 532 | SDOperand DAGCombiner::visitADD(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 533 | SDOperand N0 = N->getOperand(0); |
| 534 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 535 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 536 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 537 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 538 | |
| 539 | // fold (add c1, c2) -> c1+c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 540 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 541 | return DAG.getNode(ISD::ADD, VT, N0, N1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 542 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 543 | if (N0C && !N1C) |
| 544 | return DAG.getNode(ISD::ADD, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 545 | // fold (add x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 546 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 547 | return N0; |
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 548 | // fold ((c1-A)+c2) -> (c1+c2)-A |
| 549 | if (N1C && N0.getOpcode() == ISD::SUB) |
| 550 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0))) |
| 551 | return DAG.getNode(ISD::SUB, VT, |
| 552 | DAG.getConstant(N1C->getValue()+N0C->getValue(), VT), |
| 553 | N0.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 554 | // reassociate add |
| 555 | SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1); |
| 556 | if (RADD.Val != 0) |
| 557 | return RADD; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 558 | // fold ((0-A) + B) -> B-A |
| 559 | if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) && |
| 560 | cast<ConstantSDNode>(N0.getOperand(0))->isNullValue()) |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 561 | return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 562 | // fold (A + (0-B)) -> A-B |
| 563 | if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && |
| 564 | cast<ConstantSDNode>(N1.getOperand(0))->isNullValue()) |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 565 | return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 566 | // fold (A+(B-A)) -> B |
| 567 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 568 | return N1.getOperand(0); |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 569 | |
Evan Cheng | 860771d | 2006-03-01 01:09:54 +0000 | [diff] [blame] | 570 | if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0))) |
Chris Lattner | ef027f9 | 2006-04-21 15:32:26 +0000 | [diff] [blame] | 571 | return SDOperand(N, 0); |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 572 | |
| 573 | // fold (a+b) -> (a|b) iff a and b share no bits. |
| 574 | if (MVT::isInteger(VT) && !MVT::isVector(VT)) { |
| 575 | uint64_t LHSZero, LHSOne; |
| 576 | uint64_t RHSZero, RHSOne; |
| 577 | uint64_t Mask = MVT::getIntVTBitMask(VT); |
| 578 | TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne); |
| 579 | if (LHSZero) { |
| 580 | TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne); |
| 581 | |
| 582 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
| 583 | // If all possibly-set bits on the RHS are clear on the LHS, return an OR. |
| 584 | if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) || |
| 585 | (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask)) |
| 586 | return DAG.getNode(ISD::OR, VT, N0, N1); |
| 587 | } |
| 588 | } |
| 589 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 590 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 593 | SDOperand DAGCombiner::visitSUB(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 594 | SDOperand N0 = N->getOperand(0); |
| 595 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 596 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 597 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 598 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 854077d | 2005-10-17 01:07:11 +0000 | [diff] [blame] | 600 | // fold (sub x, x) -> 0 |
| 601 | if (N0 == N1) |
| 602 | return DAG.getConstant(0, N->getValueType(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 603 | // fold (sub c1, c2) -> c1-c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 604 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 605 | return DAG.getNode(ISD::SUB, VT, N0, N1); |
Chris Lattner | 05b5743 | 2005-10-11 06:07:15 +0000 | [diff] [blame] | 606 | // fold (sub x, c) -> (add x, -c) |
| 607 | if (N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 608 | return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 609 | // fold (A+B)-A -> B |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 610 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 611 | return N0.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 612 | // fold (A+B)-B -> A |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 613 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 614 | return N0.getOperand(0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 615 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 618 | SDOperand DAGCombiner::visitMUL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 619 | SDOperand N0 = N->getOperand(0); |
| 620 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 621 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 622 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 623 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 624 | |
| 625 | // fold (mul c1, c2) -> c1*c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 626 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 627 | return DAG.getNode(ISD::MUL, VT, N0, N1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 628 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 629 | if (N0C && !N1C) |
| 630 | return DAG.getNode(ISD::MUL, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 631 | // fold (mul x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 632 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 633 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 634 | // fold (mul x, -1) -> 0-x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 635 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 636 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 637 | // fold (mul x, (1 << c)) -> x << c |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 638 | if (N1C && isPowerOf2_64(N1C->getValue())) |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 639 | return DAG.getNode(ISD::SHL, VT, N0, |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 640 | DAG.getConstant(Log2_64(N1C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 641 | TLI.getShiftAmountTy())); |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 642 | // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c |
| 643 | if (N1C && isPowerOf2_64(-N1C->getSignExtended())) { |
| 644 | // FIXME: If the input is something that is easily negated (e.g. a |
| 645 | // single-use add), we should put the negate there. |
| 646 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), |
| 647 | DAG.getNode(ISD::SHL, VT, N0, |
| 648 | DAG.getConstant(Log2_64(-N1C->getSignExtended()), |
| 649 | TLI.getShiftAmountTy()))); |
| 650 | } |
Andrew Lenharth | 50a0d42 | 2006-04-02 21:42:45 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 652 | // (mul (shl X, c1), c2) -> (mul X, c2 << c1) |
| 653 | if (N1C && N0.getOpcode() == ISD::SHL && |
| 654 | isa<ConstantSDNode>(N0.getOperand(1))) { |
| 655 | SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 656 | AddToWorkList(C3.Val); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 657 | return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3); |
| 658 | } |
| 659 | |
| 660 | // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one |
| 661 | // use. |
| 662 | { |
| 663 | SDOperand Sh(0,0), Y(0,0); |
| 664 | // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)). |
| 665 | if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) && |
| 666 | N0.Val->hasOneUse()) { |
| 667 | Sh = N0; Y = N1; |
| 668 | } else if (N1.getOpcode() == ISD::SHL && |
| 669 | isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) { |
| 670 | Sh = N1; Y = N0; |
| 671 | } |
| 672 | if (Sh.Val) { |
| 673 | SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y); |
| 674 | return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1)); |
| 675 | } |
| 676 | } |
Chris Lattner | a1deca3 | 2006-03-04 23:33:26 +0000 | [diff] [blame] | 677 | // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2) |
| 678 | if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() && |
| 679 | isa<ConstantSDNode>(N0.getOperand(1))) { |
| 680 | return DAG.getNode(ISD::ADD, VT, |
| 681 | DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1), |
| 682 | DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1)); |
| 683 | } |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 684 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 685 | // reassociate mul |
| 686 | SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1); |
| 687 | if (RMUL.Val != 0) |
| 688 | return RMUL; |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 689 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 692 | SDOperand DAGCombiner::visitSDIV(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 693 | SDOperand N0 = N->getOperand(0); |
| 694 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 695 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 696 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 697 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 698 | |
| 699 | // fold (sdiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 700 | if (N0C && N1C && !N1C->isNullValue()) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 701 | return DAG.getNode(ISD::SDIV, VT, N0, N1); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 702 | // fold (sdiv X, 1) -> X |
| 703 | if (N1C && N1C->getSignExtended() == 1LL) |
| 704 | return N0; |
| 705 | // fold (sdiv X, -1) -> 0-X |
| 706 | if (N1C && N1C->isAllOnesValue()) |
| 707 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0); |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 708 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 709 | // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2 |
| 710 | uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1); |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 711 | if (TLI.MaskedValueIsZero(N1, SignBit) && |
| 712 | TLI.MaskedValueIsZero(N0, SignBit)) |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 713 | return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1); |
Nate Begeman | cd6a6ed | 2006-02-17 07:26:20 +0000 | [diff] [blame] | 714 | // fold (sdiv X, pow2) -> simple ops after legalize |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 715 | if (N1C && N1C->getValue() && !TLI.isIntDivCheap() && |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 716 | (isPowerOf2_64(N1C->getSignExtended()) || |
| 717 | isPowerOf2_64(-N1C->getSignExtended()))) { |
| 718 | // If dividing by powers of two is cheap, then don't perform the following |
| 719 | // fold. |
| 720 | if (TLI.isPow2DivCheap()) |
| 721 | return SDOperand(); |
| 722 | int64_t pow2 = N1C->getSignExtended(); |
| 723 | int64_t abs2 = pow2 > 0 ? pow2 : -pow2; |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 724 | unsigned lg2 = Log2_64(abs2); |
| 725 | // Splat the sign bit into the register |
| 726 | SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0, |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 727 | DAG.getConstant(MVT::getSizeInBits(VT)-1, |
| 728 | TLI.getShiftAmountTy())); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 729 | AddToWorkList(SGN.Val); |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 730 | // Add (N0 < 0) ? abs2 - 1 : 0; |
| 731 | SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN, |
| 732 | DAG.getConstant(MVT::getSizeInBits(VT)-lg2, |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 733 | TLI.getShiftAmountTy())); |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 734 | SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 735 | AddToWorkList(SRL.Val); |
| 736 | AddToWorkList(ADD.Val); // Divide by pow2 |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 737 | SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD, |
| 738 | DAG.getConstant(lg2, TLI.getShiftAmountTy())); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 739 | // If we're dividing by a positive value, we're done. Otherwise, we must |
| 740 | // negate the result. |
| 741 | if (pow2 > 0) |
| 742 | return SRA; |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 743 | AddToWorkList(SRA.Val); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 744 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA); |
| 745 | } |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 746 | // if integer divide is expensive and we satisfy the requirements, emit an |
| 747 | // alternate sequence. |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 748 | if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) && |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 749 | !TLI.isIntDivCheap()) { |
| 750 | SDOperand Op = BuildSDIV(N); |
| 751 | if (Op.Val) return Op; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 752 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 753 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 756 | SDOperand DAGCombiner::visitUDIV(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 757 | SDOperand N0 = N->getOperand(0); |
| 758 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 759 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 760 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 761 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 762 | |
| 763 | // fold (udiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 764 | if (N0C && N1C && !N1C->isNullValue()) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 765 | return DAG.getNode(ISD::UDIV, VT, N0, N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 766 | // fold (udiv x, (1 << c)) -> x >>u c |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 767 | if (N1C && isPowerOf2_64(N1C->getValue())) |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 768 | return DAG.getNode(ISD::SRL, VT, N0, |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 769 | DAG.getConstant(Log2_64(N1C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 770 | TLI.getShiftAmountTy())); |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 771 | // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2 |
| 772 | if (N1.getOpcode() == ISD::SHL) { |
| 773 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
| 774 | if (isPowerOf2_64(SHC->getValue())) { |
| 775 | MVT::ValueType ADDVT = N1.getOperand(1).getValueType(); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 776 | SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1), |
| 777 | DAG.getConstant(Log2_64(SHC->getValue()), |
| 778 | ADDVT)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 779 | AddToWorkList(Add.Val); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 780 | return DAG.getNode(ISD::SRL, VT, N0, Add); |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 784 | // fold (udiv x, c) -> alternate |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 785 | if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) { |
| 786 | SDOperand Op = BuildUDIV(N); |
| 787 | if (Op.Val) return Op; |
| 788 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 789 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 792 | SDOperand DAGCombiner::visitSREM(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 793 | SDOperand N0 = N->getOperand(0); |
| 794 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 795 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 796 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 797 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 798 | |
| 799 | // fold (srem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 800 | if (N0C && N1C && !N1C->isNullValue()) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 801 | return DAG.getNode(ISD::SREM, VT, N0, N1); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 802 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 803 | // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 |
| 804 | uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1); |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 805 | if (TLI.MaskedValueIsZero(N1, SignBit) && |
| 806 | TLI.MaskedValueIsZero(N0, SignBit)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 807 | return DAG.getNode(ISD::UREM, VT, N0, N1); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 808 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 811 | SDOperand DAGCombiner::visitUREM(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 812 | SDOperand N0 = N->getOperand(0); |
| 813 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 814 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 815 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 816 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 817 | |
| 818 | // fold (urem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 819 | if (N0C && N1C && !N1C->isNullValue()) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 820 | return DAG.getNode(ISD::UREM, VT, N0, N1); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 821 | // fold (urem x, pow2) -> (and x, pow2-1) |
| 822 | if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue())) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 823 | return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT)); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 824 | // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1)) |
| 825 | if (N1.getOpcode() == ISD::SHL) { |
| 826 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
| 827 | if (isPowerOf2_64(SHC->getValue())) { |
Nate Begeman | bab9239 | 2006-02-05 08:07:24 +0000 | [diff] [blame] | 828 | SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 829 | AddToWorkList(Add.Val); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 830 | return DAG.getNode(ISD::AND, VT, N0, Add); |
| 831 | } |
| 832 | } |
| 833 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 834 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 837 | SDOperand DAGCombiner::visitMULHS(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 838 | SDOperand N0 = N->getOperand(0); |
| 839 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 840 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 841 | |
| 842 | // fold (mulhs x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 843 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 844 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 845 | // fold (mulhs x, 1) -> (sra x, size(x)-1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 846 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 847 | return DAG.getNode(ISD::SRA, N0.getValueType(), N0, |
| 848 | DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1, |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 849 | TLI.getShiftAmountTy())); |
| 850 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 853 | SDOperand DAGCombiner::visitMULHU(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 854 | SDOperand N0 = N->getOperand(0); |
| 855 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 856 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 857 | |
| 858 | // fold (mulhu x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 859 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 860 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 861 | // fold (mulhu x, 1) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 862 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 863 | return DAG.getConstant(0, N0.getValueType()); |
| 864 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 867 | /// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with |
| 868 | /// two operands of the same opcode, try to simplify it. |
| 869 | SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) { |
| 870 | SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1); |
| 871 | MVT::ValueType VT = N0.getValueType(); |
| 872 | assert(N0.getOpcode() == N1.getOpcode() && "Bad input!"); |
| 873 | |
Chris Lattner | 540121f | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 874 | // For each of OP in AND/OR/XOR: |
| 875 | // fold (OP (zext x), (zext y)) -> (zext (OP x, y)) |
| 876 | // fold (OP (sext x), (sext y)) -> (sext (OP x, y)) |
| 877 | // fold (OP (aext x), (aext y)) -> (aext (OP x, y)) |
Chris Lattner | 0d8dae7 | 2006-05-05 06:32:04 +0000 | [diff] [blame] | 878 | // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) |
Chris Lattner | 540121f | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 879 | if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND|| |
Chris Lattner | 0d8dae7 | 2006-05-05 06:32:04 +0000 | [diff] [blame] | 880 | N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) && |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 881 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) { |
| 882 | SDOperand ORNode = DAG.getNode(N->getOpcode(), |
| 883 | N0.getOperand(0).getValueType(), |
| 884 | N0.getOperand(0), N1.getOperand(0)); |
| 885 | AddToWorkList(ORNode.Val); |
Chris Lattner | 540121f | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 886 | return DAG.getNode(N0.getOpcode(), VT, ORNode); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 889 | // For each of OP in SHL/SRL/SRA/AND... |
| 890 | // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z) |
| 891 | // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z) |
| 892 | // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 893 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL || |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 894 | N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) && |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 895 | N0.getOperand(1) == N1.getOperand(1)) { |
| 896 | SDOperand ORNode = DAG.getNode(N->getOpcode(), |
| 897 | N0.getOperand(0).getValueType(), |
| 898 | N0.getOperand(0), N1.getOperand(0)); |
| 899 | AddToWorkList(ORNode.Val); |
| 900 | return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1)); |
| 901 | } |
| 902 | |
| 903 | return SDOperand(); |
| 904 | } |
| 905 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 906 | SDOperand DAGCombiner::visitAND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 907 | SDOperand N0 = N->getOperand(0); |
| 908 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 909 | SDOperand LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 910 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 911 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 912 | MVT::ValueType VT = N1.getValueType(); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 913 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 914 | |
| 915 | // fold (and c1, c2) -> c1&c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 916 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 917 | return DAG.getNode(ISD::AND, VT, N0, N1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 918 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 919 | if (N0C && !N1C) |
| 920 | return DAG.getNode(ISD::AND, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 921 | // fold (and x, -1) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 922 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 923 | return N0; |
| 924 | // if (and x, c) is known to be zero, return 0 |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 925 | if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 926 | return DAG.getConstant(0, VT); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 927 | // reassociate and |
| 928 | SDOperand RAND = ReassociateOps(ISD::AND, N0, N1); |
| 929 | if (RAND.Val != 0) |
| 930 | return RAND; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 931 | // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF |
Nate Begeman | 5dc7e86 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 932 | if (N1C && N0.getOpcode() == ISD::OR) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 933 | if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 934 | if ((ORI->getValue() & N1C->getValue()) == N1C->getValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 935 | return N1; |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 936 | // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits. |
| 937 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
Chris Lattner | 1ec05d1 | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 938 | unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType()); |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 939 | if (TLI.MaskedValueIsZero(N0.getOperand(0), |
Chris Lattner | 1ec05d1 | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 940 | ~N1C->getValue() & InMask)) { |
| 941 | SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(), |
| 942 | N0.getOperand(0)); |
| 943 | |
| 944 | // Replace uses of the AND with uses of the Zero extend node. |
| 945 | CombineTo(N, Zext); |
| 946 | |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 947 | // We actually want to replace all uses of the any_extend with the |
| 948 | // zero_extend, to avoid duplicating things. This will later cause this |
| 949 | // AND to be folded. |
Chris Lattner | 1ec05d1 | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 950 | CombineTo(N0.Val, Zext); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 951 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 954 | // fold (and (setcc x), (setcc y)) -> (setcc (and x, y)) |
| 955 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 956 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 957 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
| 958 | |
| 959 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
| 960 | MVT::isInteger(LL.getValueType())) { |
| 961 | // fold (X == 0) & (Y == 0) -> (X|Y == 0) |
| 962 | if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) { |
| 963 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 964 | AddToWorkList(ORNode.Val); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 965 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 966 | } |
| 967 | // fold (X == -1) & (Y == -1) -> (X&Y == -1) |
| 968 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) { |
| 969 | SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 970 | AddToWorkList(ANDNode.Val); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 971 | return DAG.getSetCC(VT, ANDNode, LR, Op1); |
| 972 | } |
| 973 | // fold (X > -1) & (Y > -1) -> (X|Y > -1) |
| 974 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) { |
| 975 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 976 | AddToWorkList(ORNode.Val); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 977 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 978 | } |
| 979 | } |
| 980 | // canonicalize equivalent to ll == rl |
| 981 | if (LL == RR && LR == RL) { |
| 982 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 983 | std::swap(RL, RR); |
| 984 | } |
| 985 | if (LL == RL && LR == RR) { |
| 986 | bool isInteger = MVT::isInteger(LL.getValueType()); |
| 987 | ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger); |
| 988 | if (Result != ISD::SETCC_INVALID) |
| 989 | return DAG.getSetCC(N0.getValueType(), LL, LR, Result); |
| 990 | } |
| 991 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 992 | |
| 993 | // Simplify: and (op x...), (op y...) -> (op (and x, y)) |
| 994 | if (N0.getOpcode() == N1.getOpcode()) { |
| 995 | SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
| 996 | if (Tmp.Val) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 997 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 998 | |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 999 | // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) |
| 1000 | // fold (and (sra)) -> (and (srl)) when possible. |
Chris Lattner | 6ea2dee | 2006-03-25 22:19:00 +0000 | [diff] [blame] | 1001 | if (!MVT::isVector(VT) && |
| 1002 | SimplifyDemandedBits(SDOperand(N, 0))) |
Chris Lattner | ef027f9 | 2006-04-21 15:32:26 +0000 | [diff] [blame] | 1003 | return SDOperand(N, 0); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1004 | // fold (zext_inreg (extload x)) -> (zextload x) |
Nate Begeman | 5054f16 | 2005-10-14 01:12:21 +0000 | [diff] [blame] | 1005 | if (N0.getOpcode() == ISD::EXTLOAD) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1006 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1007 | // If we zero all the possible extended bits, then we can turn this into |
| 1008 | // a zextload if we are running before legalize or the operation is legal. |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 1009 | if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) && |
Chris Lattner | 67a44cd | 2005-10-13 18:16:34 +0000 | [diff] [blame] | 1010 | (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1011 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1012 | N0.getOperand(1), N0.getOperand(2), |
| 1013 | EVT); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1014 | AddToWorkList(N); |
Chris Lattner | 67a44cd | 2005-10-13 18:16:34 +0000 | [diff] [blame] | 1015 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1016 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1020 | if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1021 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1022 | // If we zero all the possible extended bits, then we can turn this into |
| 1023 | // a zextload if we are running before legalize or the operation is legal. |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 1024 | if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) && |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1025 | (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1026 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1027 | N0.getOperand(1), N0.getOperand(2), |
| 1028 | EVT); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1029 | AddToWorkList(N); |
Chris Lattner | 67a44cd | 2005-10-13 18:16:34 +0000 | [diff] [blame] | 1030 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1031 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1032 | } |
| 1033 | } |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1034 | |
Chris Lattner | 35a9f5a | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 1035 | // fold (and (load x), 255) -> (zextload x, i8) |
| 1036 | // fold (and (extload x, i16), 255) -> (zextload x, i8) |
| 1037 | if (N1C && |
| 1038 | (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD || |
| 1039 | N0.getOpcode() == ISD::ZEXTLOAD) && |
| 1040 | N0.hasOneUse()) { |
| 1041 | MVT::ValueType EVT, LoadedVT; |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1042 | if (N1C->getValue() == 255) |
| 1043 | EVT = MVT::i8; |
| 1044 | else if (N1C->getValue() == 65535) |
| 1045 | EVT = MVT::i16; |
| 1046 | else if (N1C->getValue() == ~0U) |
| 1047 | EVT = MVT::i32; |
| 1048 | else |
| 1049 | EVT = MVT::Other; |
Chris Lattner | 35a9f5a | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 1050 | |
| 1051 | LoadedVT = N0.getOpcode() == ISD::LOAD ? VT : |
| 1052 | cast<VTSDNode>(N0.getOperand(3))->getVT(); |
Chris Lattner | e44be60 | 2006-04-04 17:39:18 +0000 | [diff] [blame] | 1053 | if (EVT != MVT::Other && LoadedVT > EVT && |
| 1054 | (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) { |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1055 | MVT::ValueType PtrType = N0.getOperand(1).getValueType(); |
| 1056 | // For big endian targets, we need to add an offset to the pointer to load |
| 1057 | // the correct bytes. For little endian systems, we merely need to read |
| 1058 | // fewer bytes from the same pointer. |
Chris Lattner | 35a9f5a | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 1059 | unsigned PtrOff = |
| 1060 | (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8; |
| 1061 | SDOperand NewPtr = N0.getOperand(1); |
| 1062 | if (!TLI.isLittleEndian()) |
| 1063 | NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr, |
| 1064 | DAG.getConstant(PtrOff, PtrType)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1065 | AddToWorkList(NewPtr.Val); |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1066 | SDOperand Load = |
| 1067 | DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr, |
| 1068 | N0.getOperand(2), EVT); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1069 | AddToWorkList(N); |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1070 | CombineTo(N0.Val, Load, Load.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1071 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1075 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1078 | SDOperand DAGCombiner::visitOR(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1079 | SDOperand N0 = N->getOperand(0); |
| 1080 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1081 | SDOperand LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1082 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1083 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1084 | MVT::ValueType VT = N1.getValueType(); |
| 1085 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1086 | |
| 1087 | // fold (or c1, c2) -> c1|c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1088 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1089 | return DAG.getNode(ISD::OR, VT, N0, N1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1090 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1091 | if (N0C && !N1C) |
| 1092 | return DAG.getNode(ISD::OR, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1093 | // fold (or x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1094 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1095 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1096 | // fold (or x, -1) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1097 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1098 | return N1; |
| 1099 | // fold (or x, c) -> c iff (x & ~c) == 0 |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 1100 | if (N1C && |
| 1101 | TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1102 | return N1; |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1103 | // reassociate or |
| 1104 | SDOperand ROR = ReassociateOps(ISD::OR, N0, N1); |
| 1105 | if (ROR.Val != 0) |
| 1106 | return ROR; |
| 1107 | // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2) |
| 1108 | if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() && |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 1109 | isa<ConstantSDNode>(N0.getOperand(1))) { |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 1110 | ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1)); |
| 1111 | return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0), |
| 1112 | N1), |
| 1113 | DAG.getConstant(N1C->getValue() | C1->getValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 1114 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1115 | // fold (or (setcc x), (setcc y)) -> (setcc (or x, y)) |
| 1116 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 1117 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 1118 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
| 1119 | |
| 1120 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
| 1121 | MVT::isInteger(LL.getValueType())) { |
| 1122 | // fold (X != 0) | (Y != 0) -> (X|Y != 0) |
| 1123 | // fold (X < 0) | (Y < 0) -> (X|Y < 0) |
| 1124 | if (cast<ConstantSDNode>(LR)->getValue() == 0 && |
| 1125 | (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) { |
| 1126 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1127 | AddToWorkList(ORNode.Val); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1128 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 1129 | } |
| 1130 | // fold (X != -1) | (Y != -1) -> (X&Y != -1) |
| 1131 | // fold (X > -1) | (Y > -1) -> (X&Y > -1) |
| 1132 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && |
| 1133 | (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) { |
| 1134 | SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1135 | AddToWorkList(ANDNode.Val); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1136 | return DAG.getSetCC(VT, ANDNode, LR, Op1); |
| 1137 | } |
| 1138 | } |
| 1139 | // canonicalize equivalent to ll == rl |
| 1140 | if (LL == RR && LR == RL) { |
| 1141 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 1142 | std::swap(RL, RR); |
| 1143 | } |
| 1144 | if (LL == RL && LR == RR) { |
| 1145 | bool isInteger = MVT::isInteger(LL.getValueType()); |
| 1146 | ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger); |
| 1147 | if (Result != ISD::SETCC_INVALID) |
| 1148 | return DAG.getSetCC(N0.getValueType(), LL, LR, Result); |
| 1149 | } |
| 1150 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 1151 | |
| 1152 | // Simplify: or (op x...), (op y...) -> (op (or x, y)) |
| 1153 | if (N0.getOpcode() == N1.getOpcode()) { |
| 1154 | SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
| 1155 | if (Tmp.Val) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1156 | } |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame^] | 1157 | |
| 1158 | // See if this is some rotate idiom. |
| 1159 | if (SDNode *Rot = MatchRotate(N0, N1)) |
| 1160 | return SDOperand(Rot, 0); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 1161 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1162 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame^] | 1165 | |
| 1166 | /// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present. |
| 1167 | static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) { |
| 1168 | if (Op.getOpcode() == ISD::AND) { |
| 1169 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 1170 | Mask = Op.getOperand(1); |
| 1171 | Op = Op.getOperand(0); |
| 1172 | } else { |
| 1173 | return false; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) { |
| 1178 | Shift = Op; |
| 1179 | return true; |
| 1180 | } |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
| 1184 | |
| 1185 | // MatchRotate - Handle an 'or' of two operands. If this is one of the many |
| 1186 | // idioms for rotate, and if the target supports rotation instructions, generate |
| 1187 | // a rot[lr]. |
| 1188 | SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) { |
| 1189 | // Must be a legal type. Expanded an promoted things won't work with rotates. |
| 1190 | MVT::ValueType VT = LHS.getValueType(); |
| 1191 | if (!TLI.isTypeLegal(VT)) return 0; |
| 1192 | |
| 1193 | // The target must have at least one rotate flavor. |
| 1194 | bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT); |
| 1195 | bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT); |
| 1196 | if (!HasROTL && !HasROTR) return 0; |
| 1197 | |
| 1198 | // Match "(X shl/srl V1) & V2" where V2 may not be present. |
| 1199 | SDOperand LHSShift; // The shift. |
| 1200 | SDOperand LHSMask; // AND value if any. |
| 1201 | if (!MatchRotateHalf(LHS, LHSShift, LHSMask)) |
| 1202 | return 0; // Not part of a rotate. |
| 1203 | |
| 1204 | SDOperand RHSShift; // The shift. |
| 1205 | SDOperand RHSMask; // AND value if any. |
| 1206 | if (!MatchRotateHalf(RHS, RHSShift, RHSMask)) |
| 1207 | return 0; // Not part of a rotate. |
| 1208 | |
| 1209 | if (LHSShift.getOperand(0) != RHSShift.getOperand(0)) |
| 1210 | return 0; // Not shifting the same value. |
| 1211 | |
| 1212 | if (LHSShift.getOpcode() == RHSShift.getOpcode()) |
| 1213 | return 0; // Shifts must disagree. |
| 1214 | |
| 1215 | // Canonicalize shl to left side in a shl/srl pair. |
| 1216 | if (RHSShift.getOpcode() == ISD::SHL) { |
| 1217 | std::swap(LHS, RHS); |
| 1218 | std::swap(LHSShift, RHSShift); |
| 1219 | std::swap(LHSMask , RHSMask ); |
| 1220 | } |
| 1221 | |
| 1222 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 1223 | |
| 1224 | // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1) |
| 1225 | // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2) |
| 1226 | if (LHSShift.getOperand(1).getOpcode() == ISD::Constant && |
| 1227 | RHSShift.getOperand(1).getOpcode() == ISD::Constant) { |
| 1228 | uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue(); |
| 1229 | uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue(); |
| 1230 | if ((LShVal + RShVal) != OpSizeInBits) |
| 1231 | return 0; |
| 1232 | |
| 1233 | SDOperand Rot; |
| 1234 | if (HasROTL) |
| 1235 | Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0), |
| 1236 | LHSShift.getOperand(1)); |
| 1237 | else |
| 1238 | Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0), |
| 1239 | RHSShift.getOperand(1)); |
| 1240 | |
| 1241 | // If there is an AND of either shifted operand, apply it to the result. |
| 1242 | if (LHSMask.Val || RHSMask.Val) { |
| 1243 | uint64_t Mask = MVT::getIntVTBitMask(VT); |
| 1244 | |
| 1245 | if (LHSMask.Val) { |
| 1246 | uint64_t RHSBits = (1ULL << LShVal)-1; |
| 1247 | Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits; |
| 1248 | } |
| 1249 | if (RHSMask.Val) { |
| 1250 | uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1); |
| 1251 | Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits; |
| 1252 | } |
| 1253 | |
| 1254 | Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT)); |
| 1255 | } |
| 1256 | |
| 1257 | return Rot.Val; |
| 1258 | } |
| 1259 | |
| 1260 | // If there is a mask here, and we have a variable shift, we can't be sure |
| 1261 | // that we're masking out the right stuff. |
| 1262 | if (LHSMask.Val || RHSMask.Val) |
| 1263 | return 0; |
| 1264 | |
| 1265 | // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y) |
| 1266 | // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y)) |
| 1267 | if (RHSShift.getOperand(1).getOpcode() == ISD::SUB && |
| 1268 | LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) { |
| 1269 | if (ConstantSDNode *SUBC = |
| 1270 | dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) { |
| 1271 | if (SUBC->getValue() == OpSizeInBits) |
| 1272 | if (HasROTL) |
| 1273 | return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0), |
| 1274 | LHSShift.getOperand(1)).Val; |
| 1275 | else |
| 1276 | return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0), |
| 1277 | LHSShift.getOperand(1)).Val; |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y) |
| 1282 | // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y)) |
| 1283 | if (LHSShift.getOperand(1).getOpcode() == ISD::SUB && |
| 1284 | RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) { |
| 1285 | if (ConstantSDNode *SUBC = |
| 1286 | dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) { |
| 1287 | if (SUBC->getValue() == OpSizeInBits) |
| 1288 | if (HasROTL) |
| 1289 | return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0), |
| 1290 | LHSShift.getOperand(1)).Val; |
| 1291 | else |
| 1292 | return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0), |
| 1293 | RHSShift.getOperand(1)).Val; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
| 1300 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1301 | SDOperand DAGCombiner::visitXOR(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1302 | SDOperand N0 = N->getOperand(0); |
| 1303 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1304 | SDOperand LHS, RHS, CC; |
| 1305 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1306 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1307 | MVT::ValueType VT = N0.getValueType(); |
| 1308 | |
| 1309 | // fold (xor c1, c2) -> c1^c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1310 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1311 | return DAG.getNode(ISD::XOR, VT, N0, N1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1312 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1313 | if (N0C && !N1C) |
| 1314 | return DAG.getNode(ISD::XOR, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1315 | // fold (xor x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1316 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1317 | return N0; |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1318 | // reassociate xor |
| 1319 | SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1); |
| 1320 | if (RXOR.Val != 0) |
| 1321 | return RXOR; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1322 | // fold !(x cc y) -> (x !cc y) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1323 | if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) { |
| 1324 | bool isInt = MVT::isInteger(LHS.getValueType()); |
| 1325 | ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
| 1326 | isInt); |
| 1327 | if (N0.getOpcode() == ISD::SETCC) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1328 | return DAG.getSetCC(VT, LHS, RHS, NotCC); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1329 | if (N0.getOpcode() == ISD::SELECT_CC) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1330 | return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1331 | assert(0 && "Unhandled SetCC Equivalent!"); |
| 1332 | abort(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1333 | } |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1334 | // fold !(x or y) -> (!x and !y) iff x or y are setcc |
| 1335 | if (N1C && N1C->getValue() == 1 && |
| 1336 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1337 | SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1338 | if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) { |
| 1339 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1340 | LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS |
| 1341 | RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1342 | AddToWorkList(LHS.Val); AddToWorkList(RHS.Val); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1343 | return DAG.getNode(NewOpcode, VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1344 | } |
| 1345 | } |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1346 | // fold !(x or y) -> (!x and !y) iff x or y are constants |
| 1347 | if (N1C && N1C->isAllOnesValue() && |
| 1348 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1349 | SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1350 | if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) { |
| 1351 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1352 | LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS |
| 1353 | RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1354 | AddToWorkList(LHS.Val); AddToWorkList(RHS.Val); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1355 | return DAG.getNode(NewOpcode, VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1356 | } |
| 1357 | } |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 1358 | // fold (xor (xor x, c1), c2) -> (xor x, c1^c2) |
| 1359 | if (N1C && N0.getOpcode() == ISD::XOR) { |
| 1360 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 1361 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 1362 | if (N00C) |
| 1363 | return DAG.getNode(ISD::XOR, VT, N0.getOperand(1), |
| 1364 | DAG.getConstant(N1C->getValue()^N00C->getValue(), VT)); |
| 1365 | if (N01C) |
| 1366 | return DAG.getNode(ISD::XOR, VT, N0.getOperand(0), |
| 1367 | DAG.getConstant(N1C->getValue()^N01C->getValue(), VT)); |
| 1368 | } |
| 1369 | // fold (xor x, x) -> 0 |
Chris Lattner | 4fbdd59 | 2006-03-28 19:11:05 +0000 | [diff] [blame] | 1370 | if (N0 == N1) { |
| 1371 | if (!MVT::isVector(VT)) { |
| 1372 | return DAG.getConstant(0, VT); |
| 1373 | } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) { |
| 1374 | // Produce a vector of zeros. |
| 1375 | SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT)); |
| 1376 | std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1377 | return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); |
Chris Lattner | 4fbdd59 | 2006-03-28 19:11:05 +0000 | [diff] [blame] | 1378 | } |
| 1379 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 1380 | |
| 1381 | // Simplify: xor (op x...), (op y...) -> (op (xor x, y)) |
| 1382 | if (N0.getOpcode() == N1.getOpcode()) { |
| 1383 | SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
| 1384 | if (Tmp.Val) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1385 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 1386 | |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 1387 | // Simplify the expression using non-local knowledge. |
| 1388 | if (!MVT::isVector(VT) && |
| 1389 | SimplifyDemandedBits(SDOperand(N, 0))) |
Chris Lattner | ef027f9 | 2006-04-21 15:32:26 +0000 | [diff] [blame] | 1390 | return SDOperand(N, 0); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 1391 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1392 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1395 | SDOperand DAGCombiner::visitSHL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1396 | SDOperand N0 = N->getOperand(0); |
| 1397 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1398 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1399 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1400 | MVT::ValueType VT = N0.getValueType(); |
| 1401 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 1402 | |
| 1403 | // fold (shl c1, c2) -> c1<<c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1404 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1405 | return DAG.getNode(ISD::SHL, VT, N0, N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1406 | // fold (shl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1407 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1408 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1409 | // fold (shl x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1410 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1411 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1412 | // fold (shl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1413 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1414 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1415 | // if (shl x, c) is known to be zero, return 0 |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 1416 | if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1417 | return DAG.getConstant(0, VT); |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 1418 | if (SimplifyDemandedBits(SDOperand(N, 0))) |
Chris Lattner | ef027f9 | 2006-04-21 15:32:26 +0000 | [diff] [blame] | 1419 | return SDOperand(N, 0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1420 | // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1421 | if (N1C && N0.getOpcode() == ISD::SHL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1422 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1423 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1424 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1425 | if (c1 + c2 > OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1426 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1427 | return DAG.getNode(ISD::SHL, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1428 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1429 | } |
| 1430 | // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or |
| 1431 | // (srl (and x, -1 << c1), c1-c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1432 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1433 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1434 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1435 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1436 | SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
| 1437 | DAG.getConstant(~0ULL << c1, VT)); |
| 1438 | if (c2 > c1) |
| 1439 | return DAG.getNode(ISD::SHL, VT, Mask, |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1440 | DAG.getConstant(c2-c1, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1441 | else |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1442 | return DAG.getNode(ISD::SRL, VT, Mask, |
| 1443 | DAG.getConstant(c1-c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1444 | } |
| 1445 | // fold (shl (sra x, c1), c1) -> (and x, -1 << c1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1446 | if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1447 | return DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1448 | DAG.getConstant(~0ULL << N1C->getValue(), VT)); |
Chris Lattner | cac7059 | 2006-03-05 19:53:55 +0000 | [diff] [blame] | 1449 | // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2) |
| 1450 | if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() && |
| 1451 | isa<ConstantSDNode>(N0.getOperand(1))) { |
| 1452 | return DAG.getNode(ISD::ADD, VT, |
| 1453 | DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1), |
| 1454 | DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1)); |
| 1455 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1456 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1459 | SDOperand DAGCombiner::visitSRA(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1460 | SDOperand N0 = N->getOperand(0); |
| 1461 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1462 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1463 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1464 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1465 | |
| 1466 | // fold (sra c1, c2) -> c1>>c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1467 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1468 | return DAG.getNode(ISD::SRA, VT, N0, N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1469 | // fold (sra 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1470 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1471 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1472 | // fold (sra -1, x) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1473 | if (N0C && N0C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1474 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1475 | // fold (sra x, c >= size(x)) -> undef |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 1476 | if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1477 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1478 | // fold (sra x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1479 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1480 | return N0; |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 1481 | // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports |
| 1482 | // sext_inreg. |
| 1483 | if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) { |
| 1484 | unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue(); |
| 1485 | MVT::ValueType EVT; |
| 1486 | switch (LowBits) { |
| 1487 | default: EVT = MVT::Other; break; |
| 1488 | case 1: EVT = MVT::i1; break; |
| 1489 | case 8: EVT = MVT::i8; break; |
| 1490 | case 16: EVT = MVT::i16; break; |
| 1491 | case 32: EVT = MVT::i32; break; |
| 1492 | } |
| 1493 | if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)) |
| 1494 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), |
| 1495 | DAG.getValueType(EVT)); |
| 1496 | } |
Chris Lattner | 71d9ebc | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 1497 | |
| 1498 | // fold (sra (sra x, c1), c2) -> (sra x, c1+c2) |
| 1499 | if (N1C && N0.getOpcode() == ISD::SRA) { |
| 1500 | if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 1501 | unsigned Sum = N1C->getValue() + C1->getValue(); |
| 1502 | if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1; |
| 1503 | return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), |
| 1504 | DAG.getConstant(Sum, N1C->getValueType(0))); |
| 1505 | } |
| 1506 | } |
| 1507 | |
Chris Lattner | a850446 | 2006-05-08 20:51:54 +0000 | [diff] [blame] | 1508 | // Simplify, based on bits shifted out of the LHS. |
| 1509 | if (N1C && SimplifyDemandedBits(SDOperand(N, 0))) |
| 1510 | return SDOperand(N, 0); |
| 1511 | |
| 1512 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1513 | // If the sign bit is known to be zero, switch this to a SRL. |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 1514 | if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1515 | return DAG.getNode(ISD::SRL, VT, N0, N1); |
| 1516 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1519 | SDOperand DAGCombiner::visitSRL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1520 | SDOperand N0 = N->getOperand(0); |
| 1521 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1522 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1523 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1524 | MVT::ValueType VT = N0.getValueType(); |
| 1525 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 1526 | |
| 1527 | // fold (srl c1, c2) -> c1 >>u c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1528 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1529 | return DAG.getNode(ISD::SRL, VT, N0, N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1530 | // fold (srl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1531 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1532 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1533 | // fold (srl x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1534 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1535 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1536 | // fold (srl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1537 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1538 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1539 | // if (srl x, c) is known to be zero, return 0 |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 1540 | if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1541 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1542 | // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1543 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1544 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1545 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1546 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1547 | if (c1 + c2 > OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1548 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1549 | return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1550 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1551 | } |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 1553 | // fold (srl (anyextend x), c) -> (anyextend (srl x, c)) |
| 1554 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
| 1555 | // Shifting in all undef bits? |
| 1556 | MVT::ValueType SmallVT = N0.getOperand(0).getValueType(); |
| 1557 | if (N1C->getValue() >= MVT::getSizeInBits(SmallVT)) |
| 1558 | return DAG.getNode(ISD::UNDEF, VT); |
| 1559 | |
| 1560 | SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1); |
| 1561 | AddToWorkList(SmallShift.Val); |
| 1562 | return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift); |
| 1563 | } |
| 1564 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 1565 | // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit). |
| 1566 | if (N1C && N0.getOpcode() == ISD::CTLZ && |
| 1567 | N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) { |
| 1568 | uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT); |
| 1569 | TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne); |
| 1570 | |
| 1571 | // If any of the input bits are KnownOne, then the input couldn't be all |
| 1572 | // zeros, thus the result of the srl will always be zero. |
| 1573 | if (KnownOne) return DAG.getConstant(0, VT); |
| 1574 | |
| 1575 | // If all of the bits input the to ctlz node are known to be zero, then |
| 1576 | // the result of the ctlz is "32" and the result of the shift is one. |
| 1577 | uint64_t UnknownBits = ~KnownZero & Mask; |
| 1578 | if (UnknownBits == 0) return DAG.getConstant(1, VT); |
| 1579 | |
| 1580 | // Otherwise, check to see if there is exactly one bit input to the ctlz. |
| 1581 | if ((UnknownBits & (UnknownBits-1)) == 0) { |
| 1582 | // Okay, we know that only that the single bit specified by UnknownBits |
| 1583 | // could be set on input to the CTLZ node. If this bit is set, the SRL |
| 1584 | // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair |
| 1585 | // to an SRL,XOR pair, which is likely to simplify more. |
| 1586 | unsigned ShAmt = CountTrailingZeros_64(UnknownBits); |
| 1587 | SDOperand Op = N0.getOperand(0); |
| 1588 | if (ShAmt) { |
| 1589 | Op = DAG.getNode(ISD::SRL, VT, Op, |
| 1590 | DAG.getConstant(ShAmt, TLI.getShiftAmountTy())); |
| 1591 | AddToWorkList(Op.Val); |
| 1592 | } |
| 1593 | return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT)); |
| 1594 | } |
| 1595 | } |
| 1596 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1597 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1600 | SDOperand DAGCombiner::visitCTLZ(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1601 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1602 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1603 | |
| 1604 | // fold (ctlz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1605 | if (isa<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1606 | return DAG.getNode(ISD::CTLZ, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1607 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1610 | SDOperand DAGCombiner::visitCTTZ(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1611 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1612 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1613 | |
| 1614 | // fold (cttz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1615 | if (isa<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1616 | return DAG.getNode(ISD::CTTZ, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1617 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1620 | SDOperand DAGCombiner::visitCTPOP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1621 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1622 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1623 | |
| 1624 | // fold (ctpop c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1625 | if (isa<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1626 | return DAG.getNode(ISD::CTPOP, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1627 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1630 | SDOperand DAGCombiner::visitSELECT(SDNode *N) { |
| 1631 | SDOperand N0 = N->getOperand(0); |
| 1632 | SDOperand N1 = N->getOperand(1); |
| 1633 | SDOperand N2 = N->getOperand(2); |
| 1634 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1635 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1636 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
| 1637 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1638 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1639 | // fold select C, X, X -> X |
| 1640 | if (N1 == N2) |
| 1641 | return N1; |
| 1642 | // fold select true, X, Y -> X |
| 1643 | if (N0C && !N0C->isNullValue()) |
| 1644 | return N1; |
| 1645 | // fold select false, X, Y -> Y |
| 1646 | if (N0C && N0C->isNullValue()) |
| 1647 | return N2; |
| 1648 | // fold select C, 1, X -> C | X |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1649 | if (MVT::i1 == VT && N1C && N1C->getValue() == 1) |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1650 | return DAG.getNode(ISD::OR, VT, N0, N2); |
| 1651 | // fold select C, 0, X -> ~C & X |
| 1652 | // FIXME: this should check for C type == X type, not i1? |
| 1653 | if (MVT::i1 == VT && N1C && N1C->isNullValue()) { |
| 1654 | SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1655 | AddToWorkList(XORNode.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1656 | return DAG.getNode(ISD::AND, VT, XORNode, N2); |
| 1657 | } |
| 1658 | // fold select C, X, 1 -> ~C | X |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1659 | if (MVT::i1 == VT && N2C && N2C->getValue() == 1) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1660 | SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1661 | AddToWorkList(XORNode.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1662 | return DAG.getNode(ISD::OR, VT, XORNode, N1); |
| 1663 | } |
| 1664 | // fold select C, X, 0 -> C & X |
| 1665 | // FIXME: this should check for C type == X type, not i1? |
| 1666 | if (MVT::i1 == VT && N2C && N2C->isNullValue()) |
| 1667 | return DAG.getNode(ISD::AND, VT, N0, N1); |
| 1668 | // fold X ? X : Y --> X ? 1 : Y --> X | Y |
| 1669 | if (MVT::i1 == VT && N0 == N1) |
| 1670 | return DAG.getNode(ISD::OR, VT, N0, N2); |
| 1671 | // fold X ? Y : X --> X ? Y : 0 --> X & Y |
| 1672 | if (MVT::i1 == VT && N0 == N2) |
| 1673 | return DAG.getNode(ISD::AND, VT, N0, N1); |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 1674 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1675 | // If we can fold this based on the true/false value, do so. |
| 1676 | if (SimplifySelectOps(N, N1, N2)) |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 1677 | return SDOperand(N, 0); // Don't revisit N. |
| 1678 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1679 | // fold selects based on a setcc into other things, such as min/max/abs |
| 1680 | if (N0.getOpcode() == ISD::SETCC) |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1681 | // FIXME: |
| 1682 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets |
| 1683 | // having to say they don't support SELECT_CC on every type the DAG knows |
| 1684 | // about, since there is no way to mark an opcode illegal at all value types |
| 1685 | if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other)) |
| 1686 | return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1), |
| 1687 | N1, N2, N0.getOperand(2)); |
| 1688 | else |
| 1689 | return SimplifySelect(N0, N1, N2); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1690 | return SDOperand(); |
| 1691 | } |
| 1692 | |
| 1693 | SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) { |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1694 | SDOperand N0 = N->getOperand(0); |
| 1695 | SDOperand N1 = N->getOperand(1); |
| 1696 | SDOperand N2 = N->getOperand(2); |
| 1697 | SDOperand N3 = N->getOperand(3); |
| 1698 | SDOperand N4 = N->getOperand(4); |
| 1699 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1700 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1701 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
| 1702 | ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get(); |
| 1703 | |
| 1704 | // Determine if the condition we're dealing with is constant |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 1705 | SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false); |
Chris Lattner | 5eed34d | 2006-05-12 17:57:54 +0000 | [diff] [blame] | 1706 | //ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val); |
Chris Lattner | 9155902 | 2005-10-05 04:45:43 +0000 | [diff] [blame] | 1707 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1708 | // fold select_cc lhs, rhs, x, x, cc -> x |
| 1709 | if (N2 == N3) |
| 1710 | return N2; |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1711 | |
| 1712 | // If we can fold this based on the true/false value, do so. |
| 1713 | if (SimplifySelectOps(N, N2, N3)) |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 1714 | return SDOperand(N, 0); // Don't revisit N. |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1715 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1716 | // fold select_cc into other things, such as min/max/abs |
| 1717 | return SimplifySelectCC(N0, N1, N2, N3, CC); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | SDOperand DAGCombiner::visitSETCC(SDNode *N) { |
| 1721 | return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1), |
| 1722 | cast<CondCodeSDNode>(N->getOperand(2))->get()); |
| 1723 | } |
| 1724 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1725 | SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1726 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1727 | MVT::ValueType VT = N->getValueType(0); |
| 1728 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1729 | // fold (sext c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1730 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1731 | return DAG.getNode(ISD::SIGN_EXTEND, VT, N0); |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1732 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1733 | // fold (sext (sext x)) -> (sext x) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1734 | // fold (sext (aext x)) -> (sext x) |
| 1735 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1736 | return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0)); |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1737 | |
Chris Lattner | b14ab8a | 2005-12-07 07:11:03 +0000 | [diff] [blame] | 1738 | // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size. |
Chris Lattner | cc2210b | 2005-12-07 18:02:05 +0000 | [diff] [blame] | 1739 | if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&& |
| 1740 | (!AfterLegalize || |
| 1741 | TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType()))) |
Chris Lattner | b14ab8a | 2005-12-07 07:11:03 +0000 | [diff] [blame] | 1742 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), |
| 1743 | DAG.getValueType(N0.getValueType())); |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1744 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1745 | // fold (sext (load x)) -> (sext (truncate (sextload x))) |
Chris Lattner | d0f6d18 | 2005-12-15 19:02:38 +0000 | [diff] [blame] | 1746 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() && |
| 1747 | (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){ |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1748 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1749 | N0.getOperand(1), N0.getOperand(2), |
| 1750 | N0.getValueType()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1751 | CombineTo(N, ExtLoad); |
Chris Lattner | f988405 | 2005-10-13 21:52:31 +0000 | [diff] [blame] | 1752 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1753 | ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1754 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1755 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1756 | |
| 1757 | // fold (sext (sextload x)) -> (sext (truncate (sextload x))) |
| 1758 | // fold (sext ( extload x)) -> (sext (truncate (sextload x))) |
| 1759 | if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) && |
| 1760 | N0.hasOneUse()) { |
Nate Begeman | 5c74268 | 2006-05-08 01:35:01 +0000 | [diff] [blame] | 1761 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
| 1762 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1763 | N0.getOperand(1), N0.getOperand(2), EVT); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1764 | CombineTo(N, ExtLoad); |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1765 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1766 | ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1767 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1770 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1773 | SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1774 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1775 | MVT::ValueType VT = N->getValueType(0); |
| 1776 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1777 | // fold (zext c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1778 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1779 | return DAG.getNode(ISD::ZERO_EXTEND, VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1780 | // fold (zext (zext x)) -> (zext x) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1781 | // fold (zext (aext x)) -> (zext x) |
| 1782 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1783 | return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0)); |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1784 | // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size. |
| 1785 | if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&& |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1786 | (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType()))) |
Chris Lattner | 00cb95c | 2005-12-14 07:58:38 +0000 | [diff] [blame] | 1787 | return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType()); |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1788 | // fold (zext (load x)) -> (zext (truncate (zextload x))) |
Chris Lattner | d0f6d18 | 2005-12-15 19:02:38 +0000 | [diff] [blame] | 1789 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() && |
| 1790 | (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){ |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1791 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1792 | N0.getOperand(1), N0.getOperand(2), |
| 1793 | N0.getValueType()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1794 | CombineTo(N, ExtLoad); |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1795 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1796 | ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1797 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1798 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1799 | |
| 1800 | // fold (zext (zextload x)) -> (zext (truncate (zextload x))) |
| 1801 | // fold (zext ( extload x)) -> (zext (truncate (zextload x))) |
| 1802 | if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) && |
| 1803 | N0.hasOneUse()) { |
Nate Begeman | 5c74268 | 2006-05-08 01:35:01 +0000 | [diff] [blame] | 1804 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
| 1805 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1806 | N0.getOperand(1), N0.getOperand(2), EVT); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1807 | CombineTo(N, ExtLoad); |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1808 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1809 | ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1810 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1811 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1812 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1815 | SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) { |
| 1816 | SDOperand N0 = N->getOperand(0); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1817 | MVT::ValueType VT = N->getValueType(0); |
| 1818 | |
| 1819 | // fold (aext c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1820 | if (isa<ConstantSDNode>(N0)) |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1821 | return DAG.getNode(ISD::ANY_EXTEND, VT, N0); |
| 1822 | // fold (aext (aext x)) -> (aext x) |
| 1823 | // fold (aext (zext x)) -> (zext x) |
| 1824 | // fold (aext (sext x)) -> (sext x) |
| 1825 | if (N0.getOpcode() == ISD::ANY_EXTEND || |
| 1826 | N0.getOpcode() == ISD::ZERO_EXTEND || |
| 1827 | N0.getOpcode() == ISD::SIGN_EXTEND) |
| 1828 | return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0)); |
| 1829 | |
| 1830 | // fold (aext (truncate x)) -> x iff x size == zext size. |
| 1831 | if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT) |
| 1832 | return N0.getOperand(0); |
| 1833 | // fold (aext (load x)) -> (aext (truncate (extload x))) |
| 1834 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() && |
| 1835 | (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) { |
| 1836 | SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0), |
| 1837 | N0.getOperand(1), N0.getOperand(2), |
| 1838 | N0.getValueType()); |
| 1839 | CombineTo(N, ExtLoad); |
| 1840 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1841 | ExtLoad.getValue(1)); |
| 1842 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
| 1843 | } |
| 1844 | |
| 1845 | // fold (aext (zextload x)) -> (aext (truncate (zextload x))) |
| 1846 | // fold (aext (sextload x)) -> (aext (truncate (sextload x))) |
| 1847 | // fold (aext ( extload x)) -> (aext (truncate (extload x))) |
| 1848 | if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD || |
| 1849 | N0.getOpcode() == ISD::SEXTLOAD) && |
| 1850 | N0.hasOneUse()) { |
Nate Begeman | 5c74268 | 2006-05-08 01:35:01 +0000 | [diff] [blame] | 1851 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
| 1852 | SDOperand ExtLoad = DAG.getExtLoad(N0.getOpcode(), VT, N0.getOperand(0), |
| 1853 | N0.getOperand(1), N0.getOperand(2), EVT); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1854 | CombineTo(N, ExtLoad); |
| 1855 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1856 | ExtLoad.getValue(1)); |
| 1857 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
| 1858 | } |
| 1859 | return SDOperand(); |
| 1860 | } |
| 1861 | |
| 1862 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1863 | SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1864 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1865 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1866 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1867 | MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT(); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 1868 | unsigned EVTBits = MVT::getSizeInBits(EVT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1869 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1870 | // fold (sext_in_reg c1) -> c1 |
Chris Lattner | eaeda56 | 2006-05-08 20:59:41 +0000 | [diff] [blame] | 1871 | if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1872 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1); |
Chris Lattner | ee4ea92 | 2006-05-06 09:30:03 +0000 | [diff] [blame] | 1873 | |
Chris Lattner | 541a24f | 2006-05-06 22:43:44 +0000 | [diff] [blame] | 1874 | // If the input is already sign extended, just drop the extension. |
Chris Lattner | ee4ea92 | 2006-05-06 09:30:03 +0000 | [diff] [blame] | 1875 | if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1) |
| 1876 | return N0; |
| 1877 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1878 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 |
| 1879 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 1880 | EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) { |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1881 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1882 | } |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 1883 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 1884 | // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 1885 | if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1))) |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 1886 | return DAG.getZeroExtendInReg(N0, EVT); |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 1887 | |
| 1888 | // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24 |
| 1889 | // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible. |
| 1890 | // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above. |
| 1891 | if (N0.getOpcode() == ISD::SRL) { |
| 1892 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
| 1893 | if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) { |
| 1894 | // We can turn this into an SRA iff the input to the SRL is already sign |
| 1895 | // extended enough. |
| 1896 | unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0)); |
| 1897 | if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits) |
| 1898 | return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1)); |
| 1899 | } |
| 1900 | } |
| 1901 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1902 | // fold (sext_inreg (extload x)) -> (sextload x) |
| 1903 | if (N0.getOpcode() == ISD::EXTLOAD && |
| 1904 | EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() && |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1905 | (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1906 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1907 | N0.getOperand(1), N0.getOperand(2), |
| 1908 | EVT); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1909 | CombineTo(N, ExtLoad); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1910 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1911 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1912 | } |
| 1913 | // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1914 | if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() && |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1915 | EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() && |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1916 | (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1917 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1918 | N0.getOperand(1), N0.getOperand(2), |
| 1919 | EVT); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1920 | CombineTo(N, ExtLoad); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1921 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1922 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1923 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1924 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1927 | SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1928 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1929 | MVT::ValueType VT = N->getValueType(0); |
| 1930 | |
| 1931 | // noop truncate |
| 1932 | if (N0.getValueType() == N->getValueType(0)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1933 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1934 | // fold (truncate c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1935 | if (isa<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1936 | return DAG.getNode(ISD::TRUNCATE, VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1937 | // fold (truncate (truncate x)) -> (truncate x) |
| 1938 | if (N0.getOpcode() == ISD::TRUNCATE) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1939 | return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1940 | // fold (truncate (ext x)) -> (ext x) or (truncate x) or x |
Chris Lattner | b72773b | 2006-05-05 22:56:26 +0000 | [diff] [blame] | 1941 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND|| |
| 1942 | N0.getOpcode() == ISD::ANY_EXTEND) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1943 | if (N0.getValueType() < VT) |
| 1944 | // if the source is smaller than the dest, we still need an extend |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1945 | return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1946 | else if (N0.getValueType() > VT) |
| 1947 | // if the source is larger than the dest, than we just need the truncate |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1948 | return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1949 | else |
| 1950 | // if the source and dest are the same type, we can drop both the extend |
| 1951 | // and the truncate |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1952 | return N0.getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1953 | } |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1954 | // fold (truncate (load x)) -> (smaller load x) |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1955 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) { |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1956 | assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) && |
| 1957 | "Cannot truncate to larger type!"); |
| 1958 | MVT::ValueType PtrType = N0.getOperand(1).getValueType(); |
Nate Begeman | 765784a | 2005-10-12 23:18:53 +0000 | [diff] [blame] | 1959 | // For big endian targets, we need to add an offset to the pointer to load |
| 1960 | // the correct bytes. For little endian systems, we merely need to read |
| 1961 | // fewer bytes from the same pointer. |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1962 | uint64_t PtrOff = |
| 1963 | (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8; |
Nate Begeman | 765784a | 2005-10-12 23:18:53 +0000 | [diff] [blame] | 1964 | SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) : |
| 1965 | DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1), |
| 1966 | DAG.getConstant(PtrOff, PtrType)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1967 | AddToWorkList(NewPtr.Val); |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1968 | SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1969 | AddToWorkList(N); |
Chris Lattner | 24edbb7 | 2005-10-13 22:10:05 +0000 | [diff] [blame] | 1970 | CombineTo(N0.Val, Load, Load.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1971 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1972 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1973 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1976 | SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) { |
| 1977 | SDOperand N0 = N->getOperand(0); |
| 1978 | MVT::ValueType VT = N->getValueType(0); |
| 1979 | |
| 1980 | // If the input is a constant, let getNode() fold it. |
| 1981 | if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) { |
| 1982 | SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0); |
| 1983 | if (Res.Val != N) return Res; |
| 1984 | } |
| 1985 | |
Chris Lattner | c8547d8 | 2005-12-23 05:37:50 +0000 | [diff] [blame] | 1986 | if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2) |
| 1987 | return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 1988 | |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 1989 | // fold (conv (load x)) -> (load (conv*)x) |
Chris Lattner | bf40c4b | 2006-01-15 18:58:59 +0000 | [diff] [blame] | 1990 | // FIXME: These xforms need to know that the resultant load doesn't need a |
| 1991 | // higher alignment than the original! |
| 1992 | if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) { |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 1993 | SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1), |
| 1994 | N0.getOperand(2)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1995 | AddToWorkList(N); |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 1996 | CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load), |
| 1997 | Load.getValue(1)); |
| 1998 | return Load; |
| 1999 | } |
| 2000 | |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 2001 | return SDOperand(); |
| 2002 | } |
| 2003 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2004 | SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) { |
| 2005 | SDOperand N0 = N->getOperand(0); |
| 2006 | MVT::ValueType VT = N->getValueType(0); |
| 2007 | |
| 2008 | // If the input is a VBUILD_VECTOR with all constant elements, fold this now. |
| 2009 | // First check to see if this is all constant. |
| 2010 | if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() && |
| 2011 | VT == MVT::Vector) { |
| 2012 | bool isSimple = true; |
| 2013 | for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i) |
| 2014 | if (N0.getOperand(i).getOpcode() != ISD::UNDEF && |
| 2015 | N0.getOperand(i).getOpcode() != ISD::Constant && |
| 2016 | N0.getOperand(i).getOpcode() != ISD::ConstantFP) { |
| 2017 | isSimple = false; |
| 2018 | break; |
| 2019 | } |
| 2020 | |
Chris Lattner | 97c2073 | 2006-04-03 17:29:28 +0000 | [diff] [blame] | 2021 | MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT(); |
| 2022 | if (isSimple && !MVT::isVector(DestEltVT)) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2023 | return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | return SDOperand(); |
| 2028 | } |
| 2029 | |
| 2030 | /// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector |
| 2031 | /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the |
| 2032 | /// destination element value type. |
| 2033 | SDOperand DAGCombiner:: |
| 2034 | ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) { |
| 2035 | MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType(); |
| 2036 | |
| 2037 | // If this is already the right type, we're done. |
| 2038 | if (SrcEltVT == DstEltVT) return SDOperand(BV, 0); |
| 2039 | |
| 2040 | unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT); |
| 2041 | unsigned DstBitSize = MVT::getSizeInBits(DstEltVT); |
| 2042 | |
| 2043 | // If this is a conversion of N elements of one type to N elements of another |
| 2044 | // type, convert each element. This handles FP<->INT cases. |
| 2045 | if (SrcBitSize == DstBitSize) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2046 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 2047 | for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2048 | Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i))); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 2049 | AddToWorkList(Ops.back().Val); |
| 2050 | } |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2051 | Ops.push_back(*(BV->op_end()-2)); // Add num elements. |
| 2052 | Ops.push_back(DAG.getValueType(DstEltVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2053 | return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | // Otherwise, we're growing or shrinking the elements. To avoid having to |
| 2057 | // handle annoying details of growing/shrinking FP values, we convert them to |
| 2058 | // int first. |
| 2059 | if (MVT::isFloatingPoint(SrcEltVT)) { |
| 2060 | // Convert the input float vector to a int vector where the elements are the |
| 2061 | // same sizes. |
| 2062 | assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!"); |
| 2063 | MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2064 | BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val; |
| 2065 | SrcEltVT = IntVT; |
| 2066 | } |
| 2067 | |
| 2068 | // Now we know the input is an integer vector. If the output is a FP type, |
| 2069 | // convert to integer first, then to FP of the right size. |
| 2070 | if (MVT::isFloatingPoint(DstEltVT)) { |
| 2071 | assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!"); |
| 2072 | MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2073 | SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val; |
| 2074 | |
| 2075 | // Next, convert to FP elements of the same size. |
| 2076 | return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT); |
| 2077 | } |
| 2078 | |
| 2079 | // Okay, we know the src/dst types are both integers of differing types. |
| 2080 | // Handling growing first. |
| 2081 | assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT)); |
| 2082 | if (SrcBitSize < DstBitSize) { |
| 2083 | unsigned NumInputsPerOutput = DstBitSize/SrcBitSize; |
| 2084 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2085 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2086 | for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; |
| 2087 | i += NumInputsPerOutput) { |
| 2088 | bool isLE = TLI.isLittleEndian(); |
| 2089 | uint64_t NewBits = 0; |
| 2090 | bool EltIsUndef = true; |
| 2091 | for (unsigned j = 0; j != NumInputsPerOutput; ++j) { |
| 2092 | // Shift the previously computed bits over. |
| 2093 | NewBits <<= SrcBitSize; |
| 2094 | SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j)); |
| 2095 | if (Op.getOpcode() == ISD::UNDEF) continue; |
| 2096 | EltIsUndef = false; |
| 2097 | |
| 2098 | NewBits |= cast<ConstantSDNode>(Op)->getValue(); |
| 2099 | } |
| 2100 | |
| 2101 | if (EltIsUndef) |
| 2102 | Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT)); |
| 2103 | else |
| 2104 | Ops.push_back(DAG.getConstant(NewBits, DstEltVT)); |
| 2105 | } |
| 2106 | |
| 2107 | Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements. |
| 2108 | Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2109 | return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | // Finally, this must be the case where we are shrinking elements: each input |
| 2113 | // turns into multiple outputs. |
| 2114 | unsigned NumOutputsPerInput = SrcBitSize/DstBitSize; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2115 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2116 | for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) { |
| 2117 | if (BV->getOperand(i).getOpcode() == ISD::UNDEF) { |
| 2118 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) |
| 2119 | Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT)); |
| 2120 | continue; |
| 2121 | } |
| 2122 | uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue(); |
| 2123 | |
| 2124 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) { |
| 2125 | unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1); |
| 2126 | OpVal >>= DstBitSize; |
| 2127 | Ops.push_back(DAG.getConstant(ThisVal, DstEltVT)); |
| 2128 | } |
| 2129 | |
| 2130 | // For big endian targets, swap the order of the pieces of each element. |
| 2131 | if (!TLI.isLittleEndian()) |
| 2132 | std::reverse(Ops.end()-NumOutputsPerInput, Ops.end()); |
| 2133 | } |
| 2134 | Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements. |
| 2135 | Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2136 | return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | |
| 2140 | |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2141 | SDOperand DAGCombiner::visitFADD(SDNode *N) { |
| 2142 | SDOperand N0 = N->getOperand(0); |
| 2143 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2144 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2145 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2146 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2147 | |
| 2148 | // fold (fadd c1, c2) -> c1+c2 |
| 2149 | if (N0CFP && N1CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2150 | return DAG.getNode(ISD::FADD, VT, N0, N1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2151 | // canonicalize constant to RHS |
| 2152 | if (N0CFP && !N1CFP) |
| 2153 | return DAG.getNode(ISD::FADD, VT, N1, N0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2154 | // fold (A + (-B)) -> A-B |
| 2155 | if (N1.getOpcode() == ISD::FNEG) |
| 2156 | return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2157 | // fold ((-A) + B) -> B-A |
| 2158 | if (N0.getOpcode() == ISD::FNEG) |
| 2159 | return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2160 | return SDOperand(); |
| 2161 | } |
| 2162 | |
| 2163 | SDOperand DAGCombiner::visitFSUB(SDNode *N) { |
| 2164 | SDOperand N0 = N->getOperand(0); |
| 2165 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2166 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2167 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2168 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2169 | |
| 2170 | // fold (fsub c1, c2) -> c1-c2 |
| 2171 | if (N0CFP && N1CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2172 | return DAG.getNode(ISD::FSUB, VT, N0, N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2173 | // fold (A-(-B)) -> A+B |
| 2174 | if (N1.getOpcode() == ISD::FNEG) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2175 | return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2176 | return SDOperand(); |
| 2177 | } |
| 2178 | |
| 2179 | SDOperand DAGCombiner::visitFMUL(SDNode *N) { |
| 2180 | SDOperand N0 = N->getOperand(0); |
| 2181 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 2182 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2183 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2184 | MVT::ValueType VT = N->getValueType(0); |
| 2185 | |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 2186 | // fold (fmul c1, c2) -> c1*c2 |
| 2187 | if (N0CFP && N1CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2188 | return DAG.getNode(ISD::FMUL, VT, N0, N1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 2189 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2190 | if (N0CFP && !N1CFP) |
| 2191 | return DAG.getNode(ISD::FMUL, VT, N1, N0); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 2192 | // fold (fmul X, 2.0) -> (fadd X, X) |
| 2193 | if (N1CFP && N1CFP->isExactlyValue(+2.0)) |
| 2194 | return DAG.getNode(ISD::FADD, VT, N0, N0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2195 | return SDOperand(); |
| 2196 | } |
| 2197 | |
| 2198 | SDOperand DAGCombiner::visitFDIV(SDNode *N) { |
| 2199 | SDOperand N0 = N->getOperand(0); |
| 2200 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2201 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2202 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2203 | MVT::ValueType VT = N->getValueType(0); |
| 2204 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2205 | // fold (fdiv c1, c2) -> c1/c2 |
| 2206 | if (N0CFP && N1CFP) |
| 2207 | return DAG.getNode(ISD::FDIV, VT, N0, N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2208 | return SDOperand(); |
| 2209 | } |
| 2210 | |
| 2211 | SDOperand DAGCombiner::visitFREM(SDNode *N) { |
| 2212 | SDOperand N0 = N->getOperand(0); |
| 2213 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2214 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2215 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2216 | MVT::ValueType VT = N->getValueType(0); |
| 2217 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2218 | // fold (frem c1, c2) -> fmod(c1,c2) |
| 2219 | if (N0CFP && N1CFP) |
| 2220 | return DAG.getNode(ISD::FREM, VT, N0, N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2221 | return SDOperand(); |
| 2222 | } |
| 2223 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 2224 | SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) { |
| 2225 | SDOperand N0 = N->getOperand(0); |
| 2226 | SDOperand N1 = N->getOperand(1); |
| 2227 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2228 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| 2229 | MVT::ValueType VT = N->getValueType(0); |
| 2230 | |
| 2231 | if (N0CFP && N1CFP) // Constant fold |
| 2232 | return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1); |
| 2233 | |
| 2234 | if (N1CFP) { |
| 2235 | // copysign(x, c1) -> fabs(x) iff ispos(c1) |
| 2236 | // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1) |
| 2237 | union { |
| 2238 | double d; |
| 2239 | int64_t i; |
| 2240 | } u; |
| 2241 | u.d = N1CFP->getValue(); |
| 2242 | if (u.i >= 0) |
| 2243 | return DAG.getNode(ISD::FABS, VT, N0); |
| 2244 | else |
| 2245 | return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0)); |
| 2246 | } |
| 2247 | |
| 2248 | // copysign(fabs(x), y) -> copysign(x, y) |
| 2249 | // copysign(fneg(x), y) -> copysign(x, y) |
| 2250 | // copysign(copysign(x,z), y) -> copysign(x, y) |
| 2251 | if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG || |
| 2252 | N0.getOpcode() == ISD::FCOPYSIGN) |
| 2253 | return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1); |
| 2254 | |
| 2255 | // copysign(x, abs(y)) -> abs(x) |
| 2256 | if (N1.getOpcode() == ISD::FABS) |
| 2257 | return DAG.getNode(ISD::FABS, VT, N0); |
| 2258 | |
| 2259 | // copysign(x, copysign(y,z)) -> copysign(x, z) |
| 2260 | if (N1.getOpcode() == ISD::FCOPYSIGN) |
| 2261 | return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1)); |
| 2262 | |
| 2263 | // copysign(x, fp_extend(y)) -> copysign(x, y) |
| 2264 | // copysign(x, fp_round(y)) -> copysign(x, y) |
| 2265 | if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND) |
| 2266 | return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0)); |
| 2267 | |
| 2268 | return SDOperand(); |
| 2269 | } |
| 2270 | |
| 2271 | |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2272 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2273 | SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2274 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2275 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2276 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2277 | |
| 2278 | // fold (sint_to_fp c1) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2279 | if (N0C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2280 | return DAG.getNode(ISD::SINT_TO_FP, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2281 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2284 | SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2285 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2286 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2287 | MVT::ValueType VT = N->getValueType(0); |
| 2288 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2289 | // fold (uint_to_fp c1) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2290 | if (N0C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2291 | return DAG.getNode(ISD::UINT_TO_FP, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2292 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2295 | SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2296 | SDOperand N0 = N->getOperand(0); |
| 2297 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2298 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2299 | |
| 2300 | // fold (fp_to_sint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2301 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2302 | return DAG.getNode(ISD::FP_TO_SINT, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2303 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2306 | SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2307 | SDOperand N0 = N->getOperand(0); |
| 2308 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2309 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2310 | |
| 2311 | // fold (fp_to_uint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2312 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2313 | return DAG.getNode(ISD::FP_TO_UINT, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2314 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2317 | SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2318 | SDOperand N0 = N->getOperand(0); |
| 2319 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2320 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2321 | |
| 2322 | // fold (fp_round c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2323 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2324 | return DAG.getNode(ISD::FP_ROUND, VT, N0); |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 2325 | |
| 2326 | // fold (fp_round (fp_extend x)) -> x |
| 2327 | if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType()) |
| 2328 | return N0.getOperand(0); |
| 2329 | |
| 2330 | // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y) |
| 2331 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) { |
| 2332 | SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0)); |
| 2333 | AddToWorkList(Tmp.Val); |
| 2334 | return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1)); |
| 2335 | } |
| 2336 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2337 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2340 | SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2341 | SDOperand N0 = N->getOperand(0); |
| 2342 | MVT::ValueType VT = N->getValueType(0); |
| 2343 | MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2344 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2345 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2346 | // fold (fp_round_inreg c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2347 | if (N0CFP) { |
| 2348 | SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2349 | return DAG.getNode(ISD::FP_EXTEND, VT, Round); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2350 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2351 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2354 | SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2355 | SDOperand N0 = N->getOperand(0); |
| 2356 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2357 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2358 | |
| 2359 | // fold (fp_extend c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2360 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2361 | return DAG.getNode(ISD::FP_EXTEND, VT, N0); |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 2362 | |
| 2363 | // fold (fpext (load x)) -> (fpext (fpround (extload x))) |
| 2364 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() && |
| 2365 | (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) { |
| 2366 | SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0), |
| 2367 | N0.getOperand(1), N0.getOperand(2), |
| 2368 | N0.getValueType()); |
| 2369 | CombineTo(N, ExtLoad); |
| 2370 | CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad), |
| 2371 | ExtLoad.getValue(1)); |
| 2372 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
| 2373 | } |
| 2374 | |
| 2375 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2376 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2379 | SDOperand DAGCombiner::visitFNEG(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2380 | SDOperand N0 = N->getOperand(0); |
| 2381 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2382 | MVT::ValueType VT = N->getValueType(0); |
| 2383 | |
| 2384 | // fold (fneg c1) -> -c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2385 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2386 | return DAG.getNode(ISD::FNEG, VT, N0); |
| 2387 | // fold (fneg (sub x, y)) -> (sub y, x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 2388 | if (N0.getOpcode() == ISD::SUB) |
| 2389 | return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0)); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2390 | // fold (fneg (fneg x)) -> x |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 2391 | if (N0.getOpcode() == ISD::FNEG) |
| 2392 | return N0.getOperand(0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2393 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2396 | SDOperand DAGCombiner::visitFABS(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2397 | SDOperand N0 = N->getOperand(0); |
| 2398 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2399 | MVT::ValueType VT = N->getValueType(0); |
| 2400 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2401 | // fold (fabs c1) -> fabs(c1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2402 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2403 | return DAG.getNode(ISD::FABS, VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2404 | // fold (fabs (fabs x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 2405 | if (N0.getOpcode() == ISD::FABS) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2406 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2407 | // fold (fabs (fneg x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 2408 | // fold (fabs (fcopysign x, y)) -> (fabs x) |
| 2409 | if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN) |
| 2410 | return DAG.getNode(ISD::FABS, VT, N0.getOperand(0)); |
| 2411 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2412 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2415 | SDOperand DAGCombiner::visitBRCOND(SDNode *N) { |
| 2416 | SDOperand Chain = N->getOperand(0); |
| 2417 | SDOperand N1 = N->getOperand(1); |
| 2418 | SDOperand N2 = N->getOperand(2); |
| 2419 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 2420 | |
| 2421 | // never taken branch, fold to chain |
| 2422 | if (N1C && N1C->isNullValue()) |
| 2423 | return Chain; |
| 2424 | // unconditional branch |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 2425 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2426 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N2); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2427 | // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal |
| 2428 | // on the target. |
| 2429 | if (N1.getOpcode() == ISD::SETCC && |
| 2430 | TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) { |
| 2431 | return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2), |
| 2432 | N1.getOperand(0), N1.getOperand(1), N2); |
| 2433 | } |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2434 | return SDOperand(); |
| 2435 | } |
| 2436 | |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 2437 | // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB. |
| 2438 | // |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2439 | SDOperand DAGCombiner::visitBR_CC(SDNode *N) { |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 2440 | CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1)); |
| 2441 | SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3); |
| 2442 | |
| 2443 | // Use SimplifySetCC to simplify SETCC's. |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 2444 | SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false); |
| 2445 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val); |
| 2446 | |
| 2447 | // fold br_cc true, dest -> br dest (unconditional branch) |
| 2448 | if (SCCC && SCCC->getValue()) |
| 2449 | return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0), |
| 2450 | N->getOperand(4)); |
| 2451 | // fold br_cc false, dest -> unconditional fall through |
| 2452 | if (SCCC && SCCC->isNullValue()) |
| 2453 | return N->getOperand(0); |
| 2454 | // fold to a simpler setcc |
| 2455 | if (Simp.Val && Simp.getOpcode() == ISD::SETCC) |
| 2456 | return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0), |
| 2457 | Simp.getOperand(2), Simp.getOperand(0), |
| 2458 | Simp.getOperand(1), N->getOperand(4)); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2459 | return SDOperand(); |
| 2460 | } |
| 2461 | |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 2462 | SDOperand DAGCombiner::visitLOAD(SDNode *N) { |
| 2463 | SDOperand Chain = N->getOperand(0); |
| 2464 | SDOperand Ptr = N->getOperand(1); |
| 2465 | SDOperand SrcValue = N->getOperand(2); |
Chris Lattner | e4b9539 | 2006-03-31 18:06:18 +0000 | [diff] [blame] | 2466 | |
| 2467 | // If there are no uses of the loaded value, change uses of the chain value |
| 2468 | // into uses of the chain input (i.e. delete the dead load). |
| 2469 | if (N->hasNUsesOfValue(0, 0)) |
| 2470 | return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 2471 | |
| 2472 | // If this load is directly stored, replace the load value with the stored |
| 2473 | // value. |
| 2474 | // TODO: Handle store large -> read small portion. |
| 2475 | // TODO: Handle TRUNCSTORE/EXTLOAD |
| 2476 | if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr && |
| 2477 | Chain.getOperand(1).getValueType() == N->getValueType(0)) |
| 2478 | return CombineTo(N, Chain.getOperand(1), Chain); |
| 2479 | |
| 2480 | return SDOperand(); |
| 2481 | } |
| 2482 | |
Chris Lattner | 29cd7db | 2006-03-31 18:10:41 +0000 | [diff] [blame] | 2483 | /// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD. |
| 2484 | SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) { |
| 2485 | SDOperand Chain = N->getOperand(0); |
| 2486 | SDOperand Ptr = N->getOperand(1); |
| 2487 | SDOperand SrcValue = N->getOperand(2); |
| 2488 | SDOperand EVT = N->getOperand(3); |
| 2489 | |
| 2490 | // If there are no uses of the loaded value, change uses of the chain value |
| 2491 | // into uses of the chain input (i.e. delete the dead load). |
| 2492 | if (N->hasNUsesOfValue(0, 0)) |
| 2493 | return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain); |
| 2494 | |
| 2495 | return SDOperand(); |
| 2496 | } |
| 2497 | |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2498 | SDOperand DAGCombiner::visitSTORE(SDNode *N) { |
| 2499 | SDOperand Chain = N->getOperand(0); |
| 2500 | SDOperand Value = N->getOperand(1); |
| 2501 | SDOperand Ptr = N->getOperand(2); |
| 2502 | SDOperand SrcValue = N->getOperand(3); |
| 2503 | |
| 2504 | // If this is a store that kills a previous store, remove the previous store. |
Chris Lattner | 04ecf6d | 2005-10-10 23:00:08 +0000 | [diff] [blame] | 2505 | if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr && |
Chris Lattner | fe7f046 | 2005-10-27 07:10:34 +0000 | [diff] [blame] | 2506 | Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ && |
| 2507 | // Make sure that these stores are the same value type: |
| 2508 | // FIXME: we really care that the second store is >= size of the first. |
| 2509 | Value.getValueType() == Chain.getOperand(1).getValueType()) { |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2510 | // Create a new store of Value that replaces both stores. |
| 2511 | SDNode *PrevStore = Chain.Val; |
Chris Lattner | 04ecf6d | 2005-10-10 23:00:08 +0000 | [diff] [blame] | 2512 | if (PrevStore->getOperand(1) == Value) // Same value multiply stored. |
| 2513 | return Chain; |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2514 | SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other, |
| 2515 | PrevStore->getOperand(0), Value, Ptr, |
| 2516 | SrcValue); |
Chris Lattner | 04ecf6d | 2005-10-10 23:00:08 +0000 | [diff] [blame] | 2517 | CombineTo(N, NewStore); // Nuke this store. |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2518 | CombineTo(PrevStore, NewStore); // Nuke the previous store. |
Chris Lattner | 04ecf6d | 2005-10-10 23:00:08 +0000 | [diff] [blame] | 2519 | return SDOperand(N, 0); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Chris Lattner | c33baaa | 2005-12-23 05:48:07 +0000 | [diff] [blame] | 2522 | // If this is a store of a bit convert, store the input value. |
Chris Lattner | bf40c4b | 2006-01-15 18:58:59 +0000 | [diff] [blame] | 2523 | // FIXME: This needs to know that the resultant store does not need a |
| 2524 | // higher alignment than the original. |
| 2525 | if (0 && Value.getOpcode() == ISD::BIT_CONVERT) |
Chris Lattner | c33baaa | 2005-12-23 05:48:07 +0000 | [diff] [blame] | 2526 | return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0), |
| 2527 | Ptr, SrcValue); |
| 2528 | |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2529 | return SDOperand(); |
| 2530 | } |
| 2531 | |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 2532 | SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) { |
| 2533 | SDOperand InVec = N->getOperand(0); |
| 2534 | SDOperand InVal = N->getOperand(1); |
| 2535 | SDOperand EltNo = N->getOperand(2); |
| 2536 | |
| 2537 | // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new |
| 2538 | // vector with the inserted element. |
| 2539 | if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) { |
| 2540 | unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue(); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2541 | SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 2542 | if (Elt < Ops.size()) |
| 2543 | Ops[Elt] = InVal; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2544 | return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), |
| 2545 | &Ops[0], Ops.size()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | return SDOperand(); |
| 2549 | } |
| 2550 | |
| 2551 | SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) { |
| 2552 | SDOperand InVec = N->getOperand(0); |
| 2553 | SDOperand InVal = N->getOperand(1); |
| 2554 | SDOperand EltNo = N->getOperand(2); |
| 2555 | SDOperand NumElts = N->getOperand(3); |
| 2556 | SDOperand EltType = N->getOperand(4); |
| 2557 | |
| 2558 | // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new |
| 2559 | // vector with the inserted element. |
| 2560 | if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) { |
| 2561 | unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue(); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2562 | SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 2563 | if (Elt < Ops.size()-2) |
| 2564 | Ops[Elt] = InVal; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2565 | return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), |
| 2566 | &Ops[0], Ops.size()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | return SDOperand(); |
| 2570 | } |
| 2571 | |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 2572 | SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) { |
| 2573 | unsigned NumInScalars = N->getNumOperands()-2; |
| 2574 | SDOperand NumElts = N->getOperand(NumInScalars); |
| 2575 | SDOperand EltType = N->getOperand(NumInScalars+1); |
| 2576 | |
| 2577 | // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT |
| 2578 | // operations. If so, and if the EXTRACT_ELT vector inputs come from at most |
| 2579 | // two distinct vectors, turn this into a shuffle node. |
| 2580 | SDOperand VecIn1, VecIn2; |
| 2581 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 2582 | // Ignore undef inputs. |
| 2583 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 2584 | |
| 2585 | // If this input is something other than a VEXTRACT_VECTOR_ELT with a |
| 2586 | // constant index, bail out. |
| 2587 | if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT || |
| 2588 | !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) { |
| 2589 | VecIn1 = VecIn2 = SDOperand(0, 0); |
| 2590 | break; |
| 2591 | } |
| 2592 | |
| 2593 | // If the input vector type disagrees with the result of the vbuild_vector, |
| 2594 | // we can't make a shuffle. |
| 2595 | SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0); |
| 2596 | if (*(ExtractedFromVec.Val->op_end()-2) != NumElts || |
| 2597 | *(ExtractedFromVec.Val->op_end()-1) != EltType) { |
| 2598 | VecIn1 = VecIn2 = SDOperand(0, 0); |
| 2599 | break; |
| 2600 | } |
| 2601 | |
| 2602 | // Otherwise, remember this. We allow up to two distinct input vectors. |
| 2603 | if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2) |
| 2604 | continue; |
| 2605 | |
| 2606 | if (VecIn1.Val == 0) { |
| 2607 | VecIn1 = ExtractedFromVec; |
| 2608 | } else if (VecIn2.Val == 0) { |
| 2609 | VecIn2 = ExtractedFromVec; |
| 2610 | } else { |
| 2611 | // Too many inputs. |
| 2612 | VecIn1 = VecIn2 = SDOperand(0, 0); |
| 2613 | break; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | // If everything is good, we can make a shuffle operation. |
| 2618 | if (VecIn1.Val) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2619 | SmallVector<SDOperand, 8> BuildVecIndices; |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 2620 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 2621 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) { |
| 2622 | BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 2623 | continue; |
| 2624 | } |
| 2625 | |
| 2626 | SDOperand Extract = N->getOperand(i); |
| 2627 | |
| 2628 | // If extracting from the first vector, just use the index directly. |
| 2629 | if (Extract.getOperand(0) == VecIn1) { |
| 2630 | BuildVecIndices.push_back(Extract.getOperand(1)); |
| 2631 | continue; |
| 2632 | } |
| 2633 | |
| 2634 | // Otherwise, use InIdx + VecSize |
| 2635 | unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue(); |
| 2636 | BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32)); |
| 2637 | } |
| 2638 | |
| 2639 | // Add count and size info. |
| 2640 | BuildVecIndices.push_back(NumElts); |
| 2641 | BuildVecIndices.push_back(DAG.getValueType(MVT::i32)); |
| 2642 | |
| 2643 | // Return the new VVECTOR_SHUFFLE node. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2644 | SDOperand Ops[5]; |
| 2645 | Ops[0] = VecIn1; |
Chris Lattner | cef896e | 2006-03-28 22:19:47 +0000 | [diff] [blame] | 2646 | if (VecIn2.Val) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2647 | Ops[1] = VecIn2; |
Chris Lattner | cef896e | 2006-03-28 22:19:47 +0000 | [diff] [blame] | 2648 | } else { |
| 2649 | // Use an undef vbuild_vector as input for the second operand. |
| 2650 | std::vector<SDOperand> UnOps(NumInScalars, |
| 2651 | DAG.getNode(ISD::UNDEF, |
| 2652 | cast<VTSDNode>(EltType)->getVT())); |
| 2653 | UnOps.push_back(NumElts); |
| 2654 | UnOps.push_back(EltType); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2655 | Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 2656 | &UnOps[0], UnOps.size()); |
| 2657 | AddToWorkList(Ops[1].Val); |
Chris Lattner | cef896e | 2006-03-28 22:19:47 +0000 | [diff] [blame] | 2658 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2659 | Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 2660 | &BuildVecIndices[0], BuildVecIndices.size()); |
| 2661 | Ops[3] = NumElts; |
| 2662 | Ops[4] = EltType; |
| 2663 | return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | return SDOperand(); |
| 2667 | } |
| 2668 | |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2669 | SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) { |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 2670 | SDOperand ShufMask = N->getOperand(2); |
| 2671 | unsigned NumElts = ShufMask.getNumOperands(); |
| 2672 | |
| 2673 | // If the shuffle mask is an identity operation on the LHS, return the LHS. |
| 2674 | bool isIdentity = true; |
| 2675 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2676 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF && |
| 2677 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) { |
| 2678 | isIdentity = false; |
| 2679 | break; |
| 2680 | } |
| 2681 | } |
| 2682 | if (isIdentity) return N->getOperand(0); |
| 2683 | |
| 2684 | // If the shuffle mask is an identity operation on the RHS, return the RHS. |
| 2685 | isIdentity = true; |
| 2686 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2687 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF && |
| 2688 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) { |
| 2689 | isIdentity = false; |
| 2690 | break; |
| 2691 | } |
| 2692 | } |
| 2693 | if (isIdentity) return N->getOperand(1); |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2694 | |
| 2695 | // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not |
| 2696 | // needed at all. |
| 2697 | bool isUnary = true; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2698 | bool isSplat = true; |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2699 | int VecNum = -1; |
Reid Spencer | 9160a6a | 2006-07-25 20:44:41 +0000 | [diff] [blame] | 2700 | unsigned BaseIdx = 0; |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2701 | for (unsigned i = 0; i != NumElts; ++i) |
| 2702 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) { |
| 2703 | unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue(); |
| 2704 | int V = (Idx < NumElts) ? 0 : 1; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2705 | if (VecNum == -1) { |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2706 | VecNum = V; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2707 | BaseIdx = Idx; |
| 2708 | } else { |
| 2709 | if (BaseIdx != Idx) |
| 2710 | isSplat = false; |
| 2711 | if (VecNum != V) { |
| 2712 | isUnary = false; |
| 2713 | break; |
| 2714 | } |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | SDOperand N0 = N->getOperand(0); |
| 2719 | SDOperand N1 = N->getOperand(1); |
| 2720 | // Normalize unary shuffle so the RHS is undef. |
| 2721 | if (isUnary && VecNum == 1) |
| 2722 | std::swap(N0, N1); |
| 2723 | |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2724 | // If it is a splat, check if the argument vector is a build_vector with |
| 2725 | // all scalar elements the same. |
| 2726 | if (isSplat) { |
| 2727 | SDNode *V = N0.Val; |
| 2728 | if (V->getOpcode() == ISD::BIT_CONVERT) |
| 2729 | V = V->getOperand(0).Val; |
| 2730 | if (V->getOpcode() == ISD::BUILD_VECTOR) { |
| 2731 | unsigned NumElems = V->getNumOperands()-2; |
| 2732 | if (NumElems > BaseIdx) { |
| 2733 | SDOperand Base; |
| 2734 | bool AllSame = true; |
| 2735 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2736 | if (V->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 2737 | Base = V->getOperand(i); |
| 2738 | break; |
| 2739 | } |
| 2740 | } |
| 2741 | // Splat of <u, u, u, u>, return <u, u, u, u> |
| 2742 | if (!Base.Val) |
| 2743 | return N0; |
| 2744 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2745 | if (V->getOperand(i).getOpcode() != ISD::UNDEF && |
| 2746 | V->getOperand(i) != Base) { |
| 2747 | AllSame = false; |
| 2748 | break; |
| 2749 | } |
| 2750 | } |
| 2751 | // Splat of <x, x, x, x>, return <x, x, x, x> |
| 2752 | if (AllSame) |
| 2753 | return N0; |
| 2754 | } |
| 2755 | } |
| 2756 | } |
| 2757 | |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2758 | // If it is a unary or the LHS and the RHS are the same node, turn the RHS |
| 2759 | // into an undef. |
| 2760 | if (isUnary || N0 == N1) { |
| 2761 | if (N0.getOpcode() == ISD::UNDEF) |
Evan Cheng | c04766a | 2006-04-06 23:20:43 +0000 | [diff] [blame] | 2762 | return DAG.getNode(ISD::UNDEF, N->getValueType(0)); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2763 | // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the |
| 2764 | // first operand. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2765 | SmallVector<SDOperand, 8> MappedOps; |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2766 | for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) { |
Evan Cheng | c04766a | 2006-04-06 23:20:43 +0000 | [diff] [blame] | 2767 | if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF || |
| 2768 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) { |
| 2769 | MappedOps.push_back(ShufMask.getOperand(i)); |
| 2770 | } else { |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2771 | unsigned NewIdx = |
| 2772 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts; |
| 2773 | MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32)); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(), |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2777 | &MappedOps[0], MappedOps.size()); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 2778 | AddToWorkList(ShufMask.Val); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2779 | return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0), |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2780 | N0, |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2781 | DAG.getNode(ISD::UNDEF, N->getValueType(0)), |
| 2782 | ShufMask); |
| 2783 | } |
| 2784 | |
| 2785 | return SDOperand(); |
| 2786 | } |
| 2787 | |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 2788 | SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) { |
| 2789 | SDOperand ShufMask = N->getOperand(2); |
| 2790 | unsigned NumElts = ShufMask.getNumOperands()-2; |
| 2791 | |
| 2792 | // If the shuffle mask is an identity operation on the LHS, return the LHS. |
| 2793 | bool isIdentity = true; |
| 2794 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2795 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF && |
| 2796 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) { |
| 2797 | isIdentity = false; |
| 2798 | break; |
| 2799 | } |
| 2800 | } |
| 2801 | if (isIdentity) return N->getOperand(0); |
| 2802 | |
| 2803 | // If the shuffle mask is an identity operation on the RHS, return the RHS. |
| 2804 | isIdentity = true; |
| 2805 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2806 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF && |
| 2807 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) { |
| 2808 | isIdentity = false; |
| 2809 | break; |
| 2810 | } |
| 2811 | } |
| 2812 | if (isIdentity) return N->getOperand(1); |
| 2813 | |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2814 | // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not |
| 2815 | // needed at all. |
| 2816 | bool isUnary = true; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2817 | bool isSplat = true; |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2818 | int VecNum = -1; |
Reid Spencer | 9160a6a | 2006-07-25 20:44:41 +0000 | [diff] [blame] | 2819 | unsigned BaseIdx = 0; |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2820 | for (unsigned i = 0; i != NumElts; ++i) |
| 2821 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) { |
| 2822 | unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue(); |
| 2823 | int V = (Idx < NumElts) ? 0 : 1; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2824 | if (VecNum == -1) { |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2825 | VecNum = V; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2826 | BaseIdx = Idx; |
| 2827 | } else { |
| 2828 | if (BaseIdx != Idx) |
| 2829 | isSplat = false; |
| 2830 | if (VecNum != V) { |
| 2831 | isUnary = false; |
| 2832 | break; |
| 2833 | } |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | SDOperand N0 = N->getOperand(0); |
| 2838 | SDOperand N1 = N->getOperand(1); |
| 2839 | // Normalize unary shuffle so the RHS is undef. |
| 2840 | if (isUnary && VecNum == 1) |
| 2841 | std::swap(N0, N1); |
| 2842 | |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2843 | // If it is a splat, check if the argument vector is a build_vector with |
| 2844 | // all scalar elements the same. |
| 2845 | if (isSplat) { |
| 2846 | SDNode *V = N0.Val; |
| 2847 | if (V->getOpcode() == ISD::VBIT_CONVERT) |
| 2848 | V = V->getOperand(0).Val; |
| 2849 | if (V->getOpcode() == ISD::VBUILD_VECTOR) { |
| 2850 | unsigned NumElems = V->getNumOperands()-2; |
| 2851 | if (NumElems > BaseIdx) { |
| 2852 | SDOperand Base; |
| 2853 | bool AllSame = true; |
| 2854 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2855 | if (V->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 2856 | Base = V->getOperand(i); |
| 2857 | break; |
| 2858 | } |
| 2859 | } |
| 2860 | // Splat of <u, u, u, u>, return <u, u, u, u> |
| 2861 | if (!Base.Val) |
| 2862 | return N0; |
| 2863 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2864 | if (V->getOperand(i).getOpcode() != ISD::UNDEF && |
| 2865 | V->getOperand(i) != Base) { |
| 2866 | AllSame = false; |
| 2867 | break; |
| 2868 | } |
| 2869 | } |
| 2870 | // Splat of <x, x, x, x>, return <x, x, x, x> |
| 2871 | if (AllSame) |
| 2872 | return N0; |
| 2873 | } |
| 2874 | } |
| 2875 | } |
| 2876 | |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2877 | // If it is a unary or the LHS and the RHS are the same node, turn the RHS |
| 2878 | // into an undef. |
| 2879 | if (isUnary || N0 == N1) { |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 2880 | // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the |
| 2881 | // first operand. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2882 | SmallVector<SDOperand, 8> MappedOps; |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 2883 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2884 | if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF || |
| 2885 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) { |
| 2886 | MappedOps.push_back(ShufMask.getOperand(i)); |
| 2887 | } else { |
| 2888 | unsigned NewIdx = |
| 2889 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts; |
| 2890 | MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32)); |
| 2891 | } |
| 2892 | } |
| 2893 | // Add the type/#elts values. |
| 2894 | MappedOps.push_back(ShufMask.getOperand(NumElts)); |
| 2895 | MappedOps.push_back(ShufMask.getOperand(NumElts+1)); |
| 2896 | |
| 2897 | ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(), |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2898 | &MappedOps[0], MappedOps.size()); |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 2899 | AddToWorkList(ShufMask.Val); |
| 2900 | |
| 2901 | // Build the undef vector. |
| 2902 | SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType()); |
| 2903 | for (unsigned i = 0; i != NumElts; ++i) |
| 2904 | MappedOps[i] = UDVal; |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2905 | MappedOps[NumElts ] = *(N0.Val->op_end()-2); |
| 2906 | MappedOps[NumElts+1] = *(N0.Val->op_end()-1); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2907 | UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 2908 | &MappedOps[0], MappedOps.size()); |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 2909 | |
| 2910 | return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2911 | N0, UDVal, ShufMask, |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 2912 | MappedOps[NumElts], MappedOps[NumElts+1]); |
| 2913 | } |
| 2914 | |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 2915 | return SDOperand(); |
| 2916 | } |
| 2917 | |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 2918 | /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform |
| 2919 | /// a VAND to a vector_shuffle with the destination vector and a zero vector. |
| 2920 | /// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==> |
| 2921 | /// vector_shuffle V, Zero, <0, 4, 2, 4> |
| 2922 | SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) { |
| 2923 | SDOperand LHS = N->getOperand(0); |
| 2924 | SDOperand RHS = N->getOperand(1); |
| 2925 | if (N->getOpcode() == ISD::VAND) { |
| 2926 | SDOperand DstVecSize = *(LHS.Val->op_end()-2); |
| 2927 | SDOperand DstVecEVT = *(LHS.Val->op_end()-1); |
| 2928 | if (RHS.getOpcode() == ISD::VBIT_CONVERT) |
| 2929 | RHS = RHS.getOperand(0); |
| 2930 | if (RHS.getOpcode() == ISD::VBUILD_VECTOR) { |
| 2931 | std::vector<SDOperand> IdxOps; |
| 2932 | unsigned NumOps = RHS.getNumOperands(); |
| 2933 | unsigned NumElts = NumOps-2; |
| 2934 | MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT(); |
| 2935 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2936 | SDOperand Elt = RHS.getOperand(i); |
| 2937 | if (!isa<ConstantSDNode>(Elt)) |
| 2938 | return SDOperand(); |
| 2939 | else if (cast<ConstantSDNode>(Elt)->isAllOnesValue()) |
| 2940 | IdxOps.push_back(DAG.getConstant(i, EVT)); |
| 2941 | else if (cast<ConstantSDNode>(Elt)->isNullValue()) |
| 2942 | IdxOps.push_back(DAG.getConstant(NumElts, EVT)); |
| 2943 | else |
| 2944 | return SDOperand(); |
| 2945 | } |
| 2946 | |
| 2947 | // Let's see if the target supports this vector_shuffle. |
| 2948 | if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG)) |
| 2949 | return SDOperand(); |
| 2950 | |
| 2951 | // Return the new VVECTOR_SHUFFLE node. |
| 2952 | SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32); |
| 2953 | SDOperand EVTNode = DAG.getValueType(EVT); |
| 2954 | std::vector<SDOperand> Ops; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame^] | 2955 | LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode, |
| 2956 | EVTNode); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 2957 | Ops.push_back(LHS); |
| 2958 | AddToWorkList(LHS.Val); |
| 2959 | std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT)); |
| 2960 | ZeroOps.push_back(NumEltsNode); |
| 2961 | ZeroOps.push_back(EVTNode); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2962 | Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 2963 | &ZeroOps[0], ZeroOps.size())); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 2964 | IdxOps.push_back(NumEltsNode); |
| 2965 | IdxOps.push_back(EVTNode); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2966 | Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 2967 | &IdxOps[0], IdxOps.size())); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 2968 | Ops.push_back(NumEltsNode); |
| 2969 | Ops.push_back(EVTNode); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2970 | SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, |
| 2971 | &Ops[0], Ops.size()); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 2972 | if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) { |
| 2973 | Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result, |
| 2974 | DstVecSize, DstVecEVT); |
| 2975 | } |
| 2976 | return Result; |
| 2977 | } |
| 2978 | } |
| 2979 | return SDOperand(); |
| 2980 | } |
| 2981 | |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 2982 | /// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates |
| 2983 | /// the scalar operation of the vop if it is operating on an integer vector |
| 2984 | /// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD). |
| 2985 | SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp, |
| 2986 | ISD::NodeType FPOp) { |
| 2987 | MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT(); |
| 2988 | ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp; |
| 2989 | SDOperand LHS = N->getOperand(0); |
| 2990 | SDOperand RHS = N->getOperand(1); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 2991 | SDOperand Shuffle = XformToShuffleWithZero(N); |
| 2992 | if (Shuffle.Val) return Shuffle; |
| 2993 | |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 2994 | // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold |
| 2995 | // this operation. |
| 2996 | if (LHS.getOpcode() == ISD::VBUILD_VECTOR && |
| 2997 | RHS.getOpcode() == ISD::VBUILD_VECTOR) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2998 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 2999 | for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) { |
| 3000 | SDOperand LHSOp = LHS.getOperand(i); |
| 3001 | SDOperand RHSOp = RHS.getOperand(i); |
| 3002 | // If these two elements can't be folded, bail out. |
| 3003 | if ((LHSOp.getOpcode() != ISD::UNDEF && |
| 3004 | LHSOp.getOpcode() != ISD::Constant && |
| 3005 | LHSOp.getOpcode() != ISD::ConstantFP) || |
| 3006 | (RHSOp.getOpcode() != ISD::UNDEF && |
| 3007 | RHSOp.getOpcode() != ISD::Constant && |
| 3008 | RHSOp.getOpcode() != ISD::ConstantFP)) |
| 3009 | break; |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 3010 | // Can't fold divide by zero. |
| 3011 | if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) { |
| 3012 | if ((RHSOp.getOpcode() == ISD::Constant && |
| 3013 | cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) || |
| 3014 | (RHSOp.getOpcode() == ISD::ConstantFP && |
| 3015 | !cast<ConstantFPSDNode>(RHSOp.Val)->getValue())) |
| 3016 | break; |
| 3017 | } |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 3018 | Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp)); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 3019 | AddToWorkList(Ops.back().Val); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 3020 | assert((Ops.back().getOpcode() == ISD::UNDEF || |
| 3021 | Ops.back().getOpcode() == ISD::Constant || |
| 3022 | Ops.back().getOpcode() == ISD::ConstantFP) && |
| 3023 | "Scalar binop didn't fold!"); |
| 3024 | } |
Chris Lattner | a4c5d8c | 2006-04-03 17:21:50 +0000 | [diff] [blame] | 3025 | |
| 3026 | if (Ops.size() == LHS.getNumOperands()-2) { |
| 3027 | Ops.push_back(*(LHS.Val->op_end()-2)); |
| 3028 | Ops.push_back(*(LHS.Val->op_end()-1)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3029 | return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size()); |
Chris Lattner | a4c5d8c | 2006-04-03 17:21:50 +0000 | [diff] [blame] | 3030 | } |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | return SDOperand(); |
| 3034 | } |
| 3035 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3036 | SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){ |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3037 | assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!"); |
| 3038 | |
| 3039 | SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2, |
| 3040 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 3041 | // If we got a simplified select_cc node back from SimplifySelectCC, then |
| 3042 | // break it down into a new SETCC node, and a new SELECT node, and then return |
| 3043 | // the SELECT node, since we were called with a SELECT node. |
| 3044 | if (SCC.Val) { |
| 3045 | // Check to see if we got a select_cc back (to turn into setcc/select). |
| 3046 | // Otherwise, just return whatever node we got back, like fabs. |
| 3047 | if (SCC.getOpcode() == ISD::SELECT_CC) { |
| 3048 | SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(), |
| 3049 | SCC.getOperand(0), SCC.getOperand(1), |
| 3050 | SCC.getOperand(4)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3051 | AddToWorkList(SETCC.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3052 | return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2), |
| 3053 | SCC.getOperand(3), SETCC); |
| 3054 | } |
| 3055 | return SCC; |
| 3056 | } |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3057 | return SDOperand(); |
| 3058 | } |
| 3059 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 3060 | /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS |
| 3061 | /// are the two values being selected between, see if we can simplify the |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 3062 | /// select. Callers of this should assume that TheSelect is deleted if this |
| 3063 | /// returns true. As such, they should return the appropriate thing (e.g. the |
| 3064 | /// node) back to the top-level of the DAG combiner loop to avoid it being |
| 3065 | /// looked at. |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 3066 | /// |
| 3067 | bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS, |
| 3068 | SDOperand RHS) { |
| 3069 | |
| 3070 | // If this is a select from two identical things, try to pull the operation |
| 3071 | // through the select. |
| 3072 | if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){ |
| 3073 | #if 0 |
| 3074 | std::cerr << "SELECT: ["; LHS.Val->dump(); |
| 3075 | std::cerr << "] ["; RHS.Val->dump(); |
| 3076 | std::cerr << "]\n"; |
| 3077 | #endif |
| 3078 | |
| 3079 | // If this is a load and the token chain is identical, replace the select |
| 3080 | // of two loads with a load through a select of the address to load from. |
| 3081 | // This triggers in things like "select bool X, 10.0, 123.0" after the FP |
| 3082 | // constants have been dropped into the constant pool. |
| 3083 | if ((LHS.getOpcode() == ISD::LOAD || |
| 3084 | LHS.getOpcode() == ISD::EXTLOAD || |
| 3085 | LHS.getOpcode() == ISD::ZEXTLOAD || |
| 3086 | LHS.getOpcode() == ISD::SEXTLOAD) && |
| 3087 | // Token chains must be identical. |
| 3088 | LHS.getOperand(0) == RHS.getOperand(0) && |
| 3089 | // If this is an EXTLOAD, the VT's must match. |
| 3090 | (LHS.getOpcode() == ISD::LOAD || |
| 3091 | LHS.getOperand(3) == RHS.getOperand(3))) { |
| 3092 | // FIXME: this conflates two src values, discarding one. This is not |
| 3093 | // the right thing to do, but nothing uses srcvalues now. When they do, |
| 3094 | // turn SrcValue into a list of locations. |
| 3095 | SDOperand Addr; |
| 3096 | if (TheSelect->getOpcode() == ISD::SELECT) |
| 3097 | Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(), |
| 3098 | TheSelect->getOperand(0), LHS.getOperand(1), |
| 3099 | RHS.getOperand(1)); |
| 3100 | else |
| 3101 | Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(), |
| 3102 | TheSelect->getOperand(0), |
| 3103 | TheSelect->getOperand(1), |
| 3104 | LHS.getOperand(1), RHS.getOperand(1), |
| 3105 | TheSelect->getOperand(4)); |
| 3106 | |
| 3107 | SDOperand Load; |
| 3108 | if (LHS.getOpcode() == ISD::LOAD) |
| 3109 | Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0), |
| 3110 | Addr, LHS.getOperand(2)); |
| 3111 | else |
| 3112 | Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0), |
| 3113 | LHS.getOperand(0), Addr, LHS.getOperand(2), |
| 3114 | cast<VTSDNode>(LHS.getOperand(3))->getVT()); |
| 3115 | // Users of the select now use the result of the load. |
| 3116 | CombineTo(TheSelect, Load); |
| 3117 | |
| 3118 | // Users of the old loads now use the new load's chain. We know the |
| 3119 | // old-load value is dead now. |
| 3120 | CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1)); |
| 3121 | CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1)); |
| 3122 | return true; |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | return false; |
| 3127 | } |
| 3128 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3129 | SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1, |
| 3130 | SDOperand N2, SDOperand N3, |
| 3131 | ISD::CondCode CC) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3132 | |
| 3133 | MVT::ValueType VT = N2.getValueType(); |
Chris Lattner | 5eed34d | 2006-05-12 17:57:54 +0000 | [diff] [blame] | 3134 | //ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3135 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 3136 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); |
| 3137 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); |
| 3138 | |
| 3139 | // Determine if the condition we're dealing with is constant |
| 3140 | SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false); |
| 3141 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val); |
| 3142 | |
| 3143 | // fold select_cc true, x, y -> x |
| 3144 | if (SCCC && SCCC->getValue()) |
| 3145 | return N2; |
| 3146 | // fold select_cc false, x, y -> y |
| 3147 | if (SCCC && SCCC->getValue() == 0) |
| 3148 | return N3; |
| 3149 | |
| 3150 | // Check to see if we can simplify the select into an fabs node |
| 3151 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 3152 | // Allow either -0.0 or 0.0 |
| 3153 | if (CFP->getValue() == 0.0) { |
| 3154 | // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs |
| 3155 | if ((CC == ISD::SETGE || CC == ISD::SETGT) && |
| 3156 | N0 == N2 && N3.getOpcode() == ISD::FNEG && |
| 3157 | N2 == N3.getOperand(0)) |
| 3158 | return DAG.getNode(ISD::FABS, VT, N0); |
| 3159 | |
| 3160 | // select (setl[te] X, +/-0.0), fneg(X), X -> fabs |
| 3161 | if ((CC == ISD::SETLT || CC == ISD::SETLE) && |
| 3162 | N0 == N3 && N2.getOpcode() == ISD::FNEG && |
| 3163 | N2.getOperand(0) == N3) |
| 3164 | return DAG.getNode(ISD::FABS, VT, N3); |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | // Check to see if we can perform the "gzip trick", transforming |
| 3169 | // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A |
| 3170 | if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() && |
| 3171 | MVT::isInteger(N0.getValueType()) && |
| 3172 | MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) { |
| 3173 | MVT::ValueType XType = N0.getValueType(); |
| 3174 | MVT::ValueType AType = N2.getValueType(); |
| 3175 | if (XType >= AType) { |
| 3176 | // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3177 | // single-bit constant. |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3178 | if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) { |
| 3179 | unsigned ShCtV = Log2_64(N2C->getValue()); |
| 3180 | ShCtV = MVT::getSizeInBits(XType)-ShCtV-1; |
| 3181 | SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy()); |
| 3182 | SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3183 | AddToWorkList(Shift.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3184 | if (XType > AType) { |
| 3185 | Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3186 | AddToWorkList(Shift.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3187 | } |
| 3188 | return DAG.getNode(ISD::AND, AType, Shift, N2); |
| 3189 | } |
| 3190 | SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0, |
| 3191 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 3192 | TLI.getShiftAmountTy())); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3193 | AddToWorkList(Shift.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3194 | if (XType > AType) { |
| 3195 | Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3196 | AddToWorkList(Shift.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3197 | } |
| 3198 | return DAG.getNode(ISD::AND, AType, Shift, N2); |
| 3199 | } |
| 3200 | } |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3201 | |
| 3202 | // fold select C, 16, 0 -> shl C, 4 |
| 3203 | if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) && |
| 3204 | TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) { |
| 3205 | // Get a SetCC of the condition |
| 3206 | // FIXME: Should probably make sure that setcc is legal if we ever have a |
| 3207 | // target where it isn't. |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 3208 | SDOperand Temp, SCC; |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3209 | // cast from setcc result type to select result type |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 3210 | if (AfterLegalize) { |
| 3211 | SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3212 | Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType()); |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 3213 | } else { |
| 3214 | SCC = DAG.getSetCC(MVT::i1, N0, N1, CC); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3215 | Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC); |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 3216 | } |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3217 | AddToWorkList(SCC.Val); |
| 3218 | AddToWorkList(Temp.Val); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3219 | // shl setcc result by log2 n2c |
| 3220 | return DAG.getNode(ISD::SHL, N2.getValueType(), Temp, |
| 3221 | DAG.getConstant(Log2_64(N2C->getValue()), |
| 3222 | TLI.getShiftAmountTy())); |
| 3223 | } |
| 3224 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3225 | // Check to see if this is the equivalent of setcc |
| 3226 | // FIXME: Turn all of these into setcc if setcc if setcc is legal |
| 3227 | // otherwise, go ahead with the folds. |
| 3228 | if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) { |
| 3229 | MVT::ValueType XType = N0.getValueType(); |
| 3230 | if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) { |
| 3231 | SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC); |
| 3232 | if (Res.getValueType() != VT) |
| 3233 | Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res); |
| 3234 | return Res; |
| 3235 | } |
| 3236 | |
| 3237 | // seteq X, 0 -> srl (ctlz X, log2(size(X))) |
| 3238 | if (N1C && N1C->isNullValue() && CC == ISD::SETEQ && |
| 3239 | TLI.isOperationLegal(ISD::CTLZ, XType)) { |
| 3240 | SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0); |
| 3241 | return DAG.getNode(ISD::SRL, XType, Ctlz, |
| 3242 | DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)), |
| 3243 | TLI.getShiftAmountTy())); |
| 3244 | } |
| 3245 | // setgt X, 0 -> srl (and (-X, ~X), size(X)-1) |
| 3246 | if (N1C && N1C->isNullValue() && CC == ISD::SETGT) { |
| 3247 | SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType), |
| 3248 | N0); |
| 3249 | SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0, |
| 3250 | DAG.getConstant(~0ULL, XType)); |
| 3251 | return DAG.getNode(ISD::SRL, XType, |
| 3252 | DAG.getNode(ISD::AND, XType, NegN0, NotN0), |
| 3253 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 3254 | TLI.getShiftAmountTy())); |
| 3255 | } |
| 3256 | // setgt X, -1 -> xor (srl (X, size(X)-1), 1) |
| 3257 | if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) { |
| 3258 | SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0, |
| 3259 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 3260 | TLI.getShiftAmountTy())); |
| 3261 | return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType)); |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X -> |
| 3266 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
| 3267 | if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) && |
| 3268 | N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) { |
| 3269 | if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) { |
| 3270 | MVT::ValueType XType = N0.getValueType(); |
| 3271 | if (SubC->isNullValue() && MVT::isInteger(XType)) { |
| 3272 | SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0, |
| 3273 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 3274 | TLI.getShiftAmountTy())); |
| 3275 | SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3276 | AddToWorkList(Shift.Val); |
| 3277 | AddToWorkList(Add.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3278 | return DAG.getNode(ISD::XOR, XType, Add, Shift); |
| 3279 | } |
| 3280 | } |
| 3281 | } |
| 3282 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3283 | return SDOperand(); |
| 3284 | } |
| 3285 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3286 | SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0, |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 3287 | SDOperand N1, ISD::CondCode Cond, |
| 3288 | bool foldBooleans) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3289 | // These setcc operations always fold. |
| 3290 | switch (Cond) { |
| 3291 | default: break; |
| 3292 | case ISD::SETFALSE: |
| 3293 | case ISD::SETFALSE2: return DAG.getConstant(0, VT); |
| 3294 | case ISD::SETTRUE: |
| 3295 | case ISD::SETTRUE2: return DAG.getConstant(1, VT); |
| 3296 | } |
| 3297 | |
| 3298 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { |
| 3299 | uint64_t C1 = N1C->getValue(); |
| 3300 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) { |
| 3301 | uint64_t C0 = N0C->getValue(); |
| 3302 | |
| 3303 | // Sign extend the operands if required |
| 3304 | if (ISD::isSignedIntSetCC(Cond)) { |
| 3305 | C0 = N0C->getSignExtended(); |
| 3306 | C1 = N1C->getSignExtended(); |
| 3307 | } |
| 3308 | |
| 3309 | switch (Cond) { |
| 3310 | default: assert(0 && "Unknown integer setcc!"); |
| 3311 | case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT); |
| 3312 | case ISD::SETNE: return DAG.getConstant(C0 != C1, VT); |
| 3313 | case ISD::SETULT: return DAG.getConstant(C0 < C1, VT); |
| 3314 | case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT); |
| 3315 | case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT); |
| 3316 | case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT); |
| 3317 | case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT); |
| 3318 | case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT); |
| 3319 | case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT); |
| 3320 | case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT); |
| 3321 | } |
| 3322 | } else { |
| 3323 | // If the LHS is a ZERO_EXTEND, perform the comparison on the input. |
| 3324 | if (N0.getOpcode() == ISD::ZERO_EXTEND) { |
| 3325 | unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType()); |
| 3326 | |
| 3327 | // If the comparison constant has bits in the upper part, the |
| 3328 | // zero-extended value could never match. |
| 3329 | if (C1 & (~0ULL << InSize)) { |
| 3330 | unsigned VSize = MVT::getSizeInBits(N0.getValueType()); |
| 3331 | switch (Cond) { |
| 3332 | case ISD::SETUGT: |
| 3333 | case ISD::SETUGE: |
| 3334 | case ISD::SETEQ: return DAG.getConstant(0, VT); |
| 3335 | case ISD::SETULT: |
| 3336 | case ISD::SETULE: |
| 3337 | case ISD::SETNE: return DAG.getConstant(1, VT); |
| 3338 | case ISD::SETGT: |
| 3339 | case ISD::SETGE: |
| 3340 | // True if the sign bit of C1 is set. |
| 3341 | return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT); |
| 3342 | case ISD::SETLT: |
| 3343 | case ISD::SETLE: |
| 3344 | // True if the sign bit of C1 isn't set. |
| 3345 | return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT); |
| 3346 | default: |
| 3347 | break; |
| 3348 | } |
| 3349 | } |
| 3350 | |
| 3351 | // Otherwise, we can perform the comparison with the low bits. |
| 3352 | switch (Cond) { |
| 3353 | case ISD::SETEQ: |
| 3354 | case ISD::SETNE: |
| 3355 | case ISD::SETUGT: |
| 3356 | case ISD::SETUGE: |
| 3357 | case ISD::SETULT: |
| 3358 | case ISD::SETULE: |
| 3359 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 3360 | DAG.getConstant(C1, N0.getOperand(0).getValueType()), |
| 3361 | Cond); |
| 3362 | default: |
| 3363 | break; // todo, be more careful with signed comparisons |
| 3364 | } |
| 3365 | } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 3366 | (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { |
| 3367 | MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT(); |
| 3368 | unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy); |
| 3369 | MVT::ValueType ExtDstTy = N0.getValueType(); |
| 3370 | unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy); |
| 3371 | |
| 3372 | // If the extended part has any inconsistent bits, it cannot ever |
| 3373 | // compare equal. In other words, they have to be all ones or all |
| 3374 | // zeros. |
| 3375 | uint64_t ExtBits = |
| 3376 | (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1)); |
| 3377 | if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits) |
| 3378 | return DAG.getConstant(Cond == ISD::SETNE, VT); |
| 3379 | |
| 3380 | SDOperand ZextOp; |
| 3381 | MVT::ValueType Op0Ty = N0.getOperand(0).getValueType(); |
| 3382 | if (Op0Ty == ExtSrcTy) { |
| 3383 | ZextOp = N0.getOperand(0); |
| 3384 | } else { |
| 3385 | int64_t Imm = ~0ULL >> (64-ExtSrcTyBits); |
| 3386 | ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0), |
| 3387 | DAG.getConstant(Imm, Op0Ty)); |
| 3388 | } |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3389 | AddToWorkList(ZextOp.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3390 | // Otherwise, make this a use of a zext. |
| 3391 | return DAG.getSetCC(VT, ZextOp, |
| 3392 | DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)), |
| 3393 | ExtDstTy), |
| 3394 | Cond); |
Chris Lattner | 3391bcd | 2006-02-08 02:13:15 +0000 | [diff] [blame] | 3395 | } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) && |
| 3396 | (Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 3397 | (N0.getOpcode() == ISD::XOR || |
| 3398 | (N0.getOpcode() == ISD::AND && |
| 3399 | N0.getOperand(0).getOpcode() == ISD::XOR && |
| 3400 | N0.getOperand(1) == N0.getOperand(0).getOperand(1))) && |
| 3401 | isa<ConstantSDNode>(N0.getOperand(1)) && |
| 3402 | cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) { |
| 3403 | // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can |
| 3404 | // only do this if the top bits are known zero. |
| 3405 | if (TLI.MaskedValueIsZero(N1, |
| 3406 | MVT::getIntVTBitMask(N0.getValueType())-1)) { |
| 3407 | // Okay, get the un-inverted input value. |
| 3408 | SDOperand Val; |
| 3409 | if (N0.getOpcode() == ISD::XOR) |
| 3410 | Val = N0.getOperand(0); |
| 3411 | else { |
| 3412 | assert(N0.getOpcode() == ISD::AND && |
| 3413 | N0.getOperand(0).getOpcode() == ISD::XOR); |
| 3414 | // ((X^1)&1)^1 -> X & 1 |
| 3415 | Val = DAG.getNode(ISD::AND, N0.getValueType(), |
| 3416 | N0.getOperand(0).getOperand(0), N0.getOperand(1)); |
| 3417 | } |
| 3418 | return DAG.getSetCC(VT, Val, N1, |
| 3419 | Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); |
| 3420 | } |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3421 | } |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 3422 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3423 | uint64_t MinVal, MaxVal; |
| 3424 | unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0)); |
| 3425 | if (ISD::isSignedIntSetCC(Cond)) { |
| 3426 | MinVal = 1ULL << (OperandBitSize-1); |
| 3427 | if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. |
| 3428 | MaxVal = ~0ULL >> (65-OperandBitSize); |
| 3429 | else |
| 3430 | MaxVal = 0; |
| 3431 | } else { |
| 3432 | MinVal = 0; |
| 3433 | MaxVal = ~0ULL >> (64-OperandBitSize); |
| 3434 | } |
| 3435 | |
| 3436 | // Canonicalize GE/LE comparisons to use GT/LT comparisons. |
| 3437 | if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { |
| 3438 | if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true |
| 3439 | --C1; // X >= C0 --> X > (C0-1) |
| 3440 | return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()), |
| 3441 | (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); |
| 3442 | } |
| 3443 | |
| 3444 | if (Cond == ISD::SETLE || Cond == ISD::SETULE) { |
| 3445 | if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true |
| 3446 | ++C1; // X <= C0 --> X < (C0+1) |
| 3447 | return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()), |
| 3448 | (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); |
| 3449 | } |
| 3450 | |
| 3451 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal) |
| 3452 | return DAG.getConstant(0, VT); // X < MIN --> false |
| 3453 | |
| 3454 | // Canonicalize setgt X, Min --> setne X, Min |
| 3455 | if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal) |
| 3456 | return DAG.getSetCC(VT, N0, N1, ISD::SETNE); |
Chris Lattner | c8597ca | 2005-10-21 21:23:25 +0000 | [diff] [blame] | 3457 | // Canonicalize setlt X, Max --> setne X, Max |
| 3458 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal) |
| 3459 | return DAG.getSetCC(VT, N0, N1, ISD::SETNE); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3460 | |
| 3461 | // If we have setult X, 1, turn it into seteq X, 0 |
| 3462 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1) |
| 3463 | return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()), |
| 3464 | ISD::SETEQ); |
| 3465 | // If we have setugt X, Max-1, turn it into seteq X, Max |
| 3466 | else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1) |
| 3467 | return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()), |
| 3468 | ISD::SETEQ); |
| 3469 | |
| 3470 | // If we have "setcc X, C0", check to see if we can shrink the immediate |
| 3471 | // by changing cc. |
| 3472 | |
| 3473 | // SETUGT X, SINTMAX -> SETLT X, 0 |
| 3474 | if (Cond == ISD::SETUGT && OperandBitSize != 1 && |
| 3475 | C1 == (~0ULL >> (65-OperandBitSize))) |
| 3476 | return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()), |
| 3477 | ISD::SETLT); |
| 3478 | |
| 3479 | // FIXME: Implement the rest of these. |
| 3480 | |
| 3481 | // Fold bit comparisons when we can. |
| 3482 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 3483 | VT == N0.getValueType() && N0.getOpcode() == ISD::AND) |
| 3484 | if (ConstantSDNode *AndRHS = |
| 3485 | dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 3486 | if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 |
| 3487 | // Perform the xform if the AND RHS is a single bit. |
| 3488 | if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) { |
| 3489 | return DAG.getNode(ISD::SRL, VT, N0, |
| 3490 | DAG.getConstant(Log2_64(AndRHS->getValue()), |
| 3491 | TLI.getShiftAmountTy())); |
| 3492 | } |
| 3493 | } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) { |
| 3494 | // (X & 8) == 8 --> (X & 8) >> 3 |
| 3495 | // Perform the xform if C1 is a single bit. |
| 3496 | if ((C1 & (C1-1)) == 0) { |
| 3497 | return DAG.getNode(ISD::SRL, VT, N0, |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 3498 | DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy())); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3499 | } |
| 3500 | } |
| 3501 | } |
| 3502 | } |
| 3503 | } else if (isa<ConstantSDNode>(N0.Val)) { |
| 3504 | // Ensure that the constant occurs on the RHS. |
| 3505 | return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond)); |
| 3506 | } |
| 3507 | |
| 3508 | if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val)) |
| 3509 | if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) { |
| 3510 | double C0 = N0C->getValue(), C1 = N1C->getValue(); |
| 3511 | |
| 3512 | switch (Cond) { |
| 3513 | default: break; // FIXME: Implement the rest of these! |
| 3514 | case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT); |
| 3515 | case ISD::SETNE: return DAG.getConstant(C0 != C1, VT); |
| 3516 | case ISD::SETLT: return DAG.getConstant(C0 < C1, VT); |
| 3517 | case ISD::SETGT: return DAG.getConstant(C0 > C1, VT); |
| 3518 | case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT); |
| 3519 | case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT); |
| 3520 | } |
| 3521 | } else { |
| 3522 | // Ensure that the constant occurs on the RHS. |
| 3523 | return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond)); |
| 3524 | } |
| 3525 | |
| 3526 | if (N0 == N1) { |
| 3527 | // We can always fold X == Y for integer setcc's. |
| 3528 | if (MVT::isInteger(N0.getValueType())) |
| 3529 | return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); |
| 3530 | unsigned UOF = ISD::getUnorderedFlavor(Cond); |
| 3531 | if (UOF == 2) // FP operators that are undefined on NaNs. |
| 3532 | return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); |
| 3533 | if (UOF == unsigned(ISD::isTrueWhenEqual(Cond))) |
| 3534 | return DAG.getConstant(UOF, VT); |
| 3535 | // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO |
| 3536 | // if it is not already. |
Chris Lattner | 4090aee | 2006-01-18 19:13:41 +0000 | [diff] [blame] | 3537 | ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO; |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3538 | if (NewCond != Cond) |
| 3539 | return DAG.getSetCC(VT, N0, N1, NewCond); |
| 3540 | } |
| 3541 | |
| 3542 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 3543 | MVT::isInteger(N0.getValueType())) { |
| 3544 | if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB || |
| 3545 | N0.getOpcode() == ISD::XOR) { |
| 3546 | // Simplify (X+Y) == (X+Z) --> Y == Z |
| 3547 | if (N0.getOpcode() == N1.getOpcode()) { |
| 3548 | if (N0.getOperand(0) == N1.getOperand(0)) |
| 3549 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond); |
| 3550 | if (N0.getOperand(1) == N1.getOperand(1)) |
| 3551 | return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond); |
Evan Cheng | 1efba0e | 2006-08-29 06:42:35 +0000 | [diff] [blame] | 3552 | if (DAG.isCommutativeBinOp(N0.getOpcode())) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3553 | // If X op Y == Y op X, try other combinations. |
| 3554 | if (N0.getOperand(0) == N1.getOperand(1)) |
| 3555 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond); |
| 3556 | if (N0.getOperand(1) == N1.getOperand(0)) |
Chris Lattner | a158eee | 2005-10-25 18:57:30 +0000 | [diff] [blame] | 3557 | return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3558 | } |
| 3559 | } |
Chris Lattner | b3ddfc4 | 2006-02-02 06:36:13 +0000 | [diff] [blame] | 3560 | |
| 3561 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) { |
| 3562 | if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 3563 | // Turn (X+C1) == C2 --> X == C2-C1 |
| 3564 | if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) { |
| 3565 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 3566 | DAG.getConstant(RHSC->getValue()-LHSR->getValue(), |
| 3567 | N0.getValueType()), Cond); |
| 3568 | } |
| 3569 | |
| 3570 | // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. |
| 3571 | if (N0.getOpcode() == ISD::XOR) |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 3572 | // If we know that all of the inverted bits are zero, don't bother |
| 3573 | // performing the inversion. |
Chris Lattner | b3ddfc4 | 2006-02-02 06:36:13 +0000 | [diff] [blame] | 3574 | if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue())) |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 3575 | return DAG.getSetCC(VT, N0.getOperand(0), |
Chris Lattner | b3ddfc4 | 2006-02-02 06:36:13 +0000 | [diff] [blame] | 3576 | DAG.getConstant(LHSR->getValue()^RHSC->getValue(), |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 3577 | N0.getValueType()), Cond); |
Chris Lattner | b3ddfc4 | 2006-02-02 06:36:13 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | // Turn (C1-X) == C2 --> X == C1-C2 |
| 3581 | if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) { |
| 3582 | if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) { |
| 3583 | return DAG.getSetCC(VT, N0.getOperand(1), |
| 3584 | DAG.getConstant(SUBC->getValue()-RHSC->getValue(), |
| 3585 | N0.getValueType()), Cond); |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 3586 | } |
Chris Lattner | b3ddfc4 | 2006-02-02 06:36:13 +0000 | [diff] [blame] | 3587 | } |
| 3588 | } |
| 3589 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3590 | // Simplify (X+Z) == X --> Z == 0 |
| 3591 | if (N0.getOperand(0) == N1) |
| 3592 | return DAG.getSetCC(VT, N0.getOperand(1), |
| 3593 | DAG.getConstant(0, N0.getValueType()), Cond); |
| 3594 | if (N0.getOperand(1) == N1) { |
Evan Cheng | 1efba0e | 2006-08-29 06:42:35 +0000 | [diff] [blame] | 3595 | if (DAG.isCommutativeBinOp(N0.getOpcode())) |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3596 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 3597 | DAG.getConstant(0, N0.getValueType()), Cond); |
| 3598 | else { |
| 3599 | assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 3600 | // (Z-X) == X --> Z == X<<1 |
| 3601 | SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), |
| 3602 | N1, |
| 3603 | DAG.getConstant(1,TLI.getShiftAmountTy())); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3604 | AddToWorkList(SH.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3605 | return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond); |
| 3606 | } |
| 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB || |
| 3611 | N1.getOpcode() == ISD::XOR) { |
| 3612 | // Simplify X == (X+Z) --> Z == 0 |
| 3613 | if (N1.getOperand(0) == N0) { |
| 3614 | return DAG.getSetCC(VT, N1.getOperand(1), |
| 3615 | DAG.getConstant(0, N1.getValueType()), Cond); |
| 3616 | } else if (N1.getOperand(1) == N0) { |
Evan Cheng | 1efba0e | 2006-08-29 06:42:35 +0000 | [diff] [blame] | 3617 | if (DAG.isCommutativeBinOp(N1.getOpcode())) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3618 | return DAG.getSetCC(VT, N1.getOperand(0), |
| 3619 | DAG.getConstant(0, N1.getValueType()), Cond); |
| 3620 | } else { |
| 3621 | assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 3622 | // X == (Z-X) --> X<<1 == Z |
| 3623 | SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0, |
| 3624 | DAG.getConstant(1,TLI.getShiftAmountTy())); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3625 | AddToWorkList(SH.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3626 | return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond); |
| 3627 | } |
| 3628 | } |
| 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | // Fold away ALL boolean setcc's. |
| 3633 | SDOperand Temp; |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 3634 | if (N0.getValueType() == MVT::i1 && foldBooleans) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3635 | switch (Cond) { |
| 3636 | default: assert(0 && "Unknown integer setcc!"); |
| 3637 | case ISD::SETEQ: // X == Y -> (X^Y)^1 |
| 3638 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1); |
| 3639 | N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3640 | AddToWorkList(Temp.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3641 | break; |
| 3642 | case ISD::SETNE: // X != Y --> (X^Y) |
| 3643 | N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1); |
| 3644 | break; |
| 3645 | case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y |
| 3646 | case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y |
| 3647 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1)); |
| 3648 | N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3649 | AddToWorkList(Temp.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3650 | break; |
| 3651 | case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X |
| 3652 | case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X |
| 3653 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1)); |
| 3654 | N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3655 | AddToWorkList(Temp.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3656 | break; |
| 3657 | case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y |
| 3658 | case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y |
| 3659 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1)); |
| 3660 | N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3661 | AddToWorkList(Temp.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3662 | break; |
| 3663 | case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X |
| 3664 | case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X |
| 3665 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1)); |
| 3666 | N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp); |
| 3667 | break; |
| 3668 | } |
| 3669 | if (VT != MVT::i1) { |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3670 | AddToWorkList(N0.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3671 | // FIXME: If running after legalize, we probably can't do this. |
| 3672 | N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0); |
| 3673 | } |
| 3674 | return N0; |
| 3675 | } |
| 3676 | |
| 3677 | // Could not fold it. |
| 3678 | return SDOperand(); |
| 3679 | } |
| 3680 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 3681 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 3682 | /// return a DAG expression to select that will generate the same value by |
| 3683 | /// multiplying by a magic number. See: |
| 3684 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 3685 | SDOperand DAGCombiner::BuildSDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 3686 | std::vector<SDNode*> Built; |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 3687 | SDOperand S = TLI.BuildSDIV(N, DAG, &Built); |
| 3688 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 3689 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 3690 | ii != ee; ++ii) |
| 3691 | AddToWorkList(*ii); |
| 3692 | return S; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 3696 | /// return a DAG expression to select that will generate the same value by |
| 3697 | /// multiplying by a magic number. See: |
| 3698 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 3699 | SDOperand DAGCombiner::BuildUDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 3700 | std::vector<SDNode*> Built; |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 3701 | SDOperand S = TLI.BuildUDIV(N, DAG, &Built); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 3702 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 3703 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 3704 | ii != ee; ++ii) |
| 3705 | AddToWorkList(*ii); |
| 3706 | return S; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3709 | // SelectionDAG::Combine - This is the entry point for the file. |
| 3710 | // |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 3711 | void SelectionDAG::Combine(bool RunningAfterLegalize) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3712 | /// run - This is the main entry point to this class. |
| 3713 | /// |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 3714 | DAGCombiner(*this).Run(RunningAfterLegalize); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3715 | } |