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