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 | // |
| 19 | // FIXME: Should add a corresponding version of fold AND with |
| 20 | // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which |
| 21 | // we don't have yet. |
| 22 | // |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 23 | // FIXME: select C, pow2, pow2 -> something smart |
| 24 | // FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z) |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 25 | // FIXME: Dead stores -> nuke |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 26 | // FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 27 | // FIXME: mul (x, const) -> shifts + adds |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 28 | // FIXME: undef values |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 29 | // FIXME: make truncate see through SIGN_EXTEND and AND |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 30 | // FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 31 | // FIXME: verify that getNode can't return extends with an operand whose type |
| 32 | // is >= to that of the extend. |
| 33 | // FIXME: divide by zero is currently left unfolded. do we want to turn this |
| 34 | // into an undef? |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 35 | // FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 36 | // FIXME: reassociate (X+C)+Y into (X+Y)+C if the inner expression has one use |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 37 | // |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
| 40 | #define DEBUG_TYPE "dagcombine" |
| 41 | #include "llvm/ADT/Statistic.h" |
| 42 | #include "llvm/CodeGen/SelectionDAG.h" |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Debug.h" |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 44 | #include "llvm/Support/MathExtras.h" |
| 45 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | a500fc6 | 2005-09-09 23:53:39 +0000 | [diff] [blame] | 46 | #include <algorithm> |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 47 | #include <cmath> |
| 48 | using namespace llvm; |
| 49 | |
| 50 | namespace { |
| 51 | Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined"); |
| 52 | |
| 53 | class DAGCombiner { |
| 54 | SelectionDAG &DAG; |
| 55 | TargetLowering &TLI; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 56 | bool AfterLegalize; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 57 | |
| 58 | // Worklist of all of the nodes that need to be simplified. |
| 59 | std::vector<SDNode*> WorkList; |
| 60 | |
| 61 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 62 | /// the instruction to the work lists because they might get more simplified |
| 63 | /// now. |
| 64 | /// |
| 65 | void AddUsersToWorkList(SDNode *N) { |
| 66 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 67 | UI != UE; ++UI) |
| 68 | WorkList.push_back(*UI); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /// removeFromWorkList - remove all instances of N from the worklist. |
| 72 | void removeFromWorkList(SDNode *N) { |
| 73 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N), |
| 74 | WorkList.end()); |
| 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(); |
| 80 | std::cerr << "\nWith: "; To[0].Val->dump(); |
| 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 | } |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 101 | |
| 102 | SDOperand CombineTo(SDNode *N, SDOperand Res) { |
| 103 | std::vector<SDOperand> To; |
| 104 | To.push_back(Res); |
| 105 | return CombineTo(N, To); |
| 106 | } |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 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 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 115 | /// visit - call the node-specific routine that knows how to fold each |
| 116 | /// particular type of node. |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 117 | SDOperand visit(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 118 | |
| 119 | // Visitation implementation - Implement dag node combining for different |
| 120 | // node types. The semantics are as follows: |
| 121 | // Return Value: |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 122 | // SDOperand.Val == 0 - No change was made |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 123 | // SDOperand.Val == N - N was replaced, is dead, and is already handled. |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 124 | // otherwise - N should be replaced by the returned Operand. |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 125 | // |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 126 | SDOperand visitTokenFactor(SDNode *N); |
| 127 | SDOperand visitADD(SDNode *N); |
| 128 | SDOperand visitSUB(SDNode *N); |
| 129 | SDOperand visitMUL(SDNode *N); |
| 130 | SDOperand visitSDIV(SDNode *N); |
| 131 | SDOperand visitUDIV(SDNode *N); |
| 132 | SDOperand visitSREM(SDNode *N); |
| 133 | SDOperand visitUREM(SDNode *N); |
| 134 | SDOperand visitMULHU(SDNode *N); |
| 135 | SDOperand visitMULHS(SDNode *N); |
| 136 | SDOperand visitAND(SDNode *N); |
| 137 | SDOperand visitOR(SDNode *N); |
| 138 | SDOperand visitXOR(SDNode *N); |
| 139 | SDOperand visitSHL(SDNode *N); |
| 140 | SDOperand visitSRA(SDNode *N); |
| 141 | SDOperand visitSRL(SDNode *N); |
| 142 | SDOperand visitCTLZ(SDNode *N); |
| 143 | SDOperand visitCTTZ(SDNode *N); |
| 144 | SDOperand visitCTPOP(SDNode *N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 145 | SDOperand visitSELECT(SDNode *N); |
| 146 | SDOperand visitSELECT_CC(SDNode *N); |
| 147 | SDOperand visitSETCC(SDNode *N); |
Nate Begeman | 5054f16 | 2005-10-14 01:12:21 +0000 | [diff] [blame] | 148 | SDOperand visitADD_PARTS(SDNode *N); |
| 149 | SDOperand visitSUB_PARTS(SDNode *N); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 150 | SDOperand visitSIGN_EXTEND(SDNode *N); |
| 151 | SDOperand visitZERO_EXTEND(SDNode *N); |
| 152 | SDOperand visitSIGN_EXTEND_INREG(SDNode *N); |
| 153 | SDOperand visitTRUNCATE(SDNode *N); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 154 | SDOperand visitBIT_CONVERT(SDNode *N); |
Nate Begeman | 5054f16 | 2005-10-14 01:12:21 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 156 | SDOperand visitFADD(SDNode *N); |
| 157 | SDOperand visitFSUB(SDNode *N); |
| 158 | SDOperand visitFMUL(SDNode *N); |
| 159 | SDOperand visitFDIV(SDNode *N); |
| 160 | SDOperand visitFREM(SDNode *N); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 161 | SDOperand visitSINT_TO_FP(SDNode *N); |
| 162 | SDOperand visitUINT_TO_FP(SDNode *N); |
| 163 | SDOperand visitFP_TO_SINT(SDNode *N); |
| 164 | SDOperand visitFP_TO_UINT(SDNode *N); |
| 165 | SDOperand visitFP_ROUND(SDNode *N); |
| 166 | SDOperand visitFP_ROUND_INREG(SDNode *N); |
| 167 | SDOperand visitFP_EXTEND(SDNode *N); |
| 168 | SDOperand visitFNEG(SDNode *N); |
| 169 | SDOperand visitFABS(SDNode *N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 170 | SDOperand visitBRCOND(SDNode *N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 171 | SDOperand visitBRCONDTWOWAY(SDNode *N); |
| 172 | SDOperand visitBR_CC(SDNode *N); |
| 173 | SDOperand visitBRTWOWAY_CC(SDNode *N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 175 | SDOperand visitLOAD(SDNode *N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 176 | SDOperand visitSTORE(SDNode *N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 177 | |
Jim Laskey | d6e8d41 | 2005-12-23 20:08:28 +0000 | [diff] [blame] | 178 | SDOperand visitLOCATION(SDNode *N); |
| 179 | SDOperand visitDEBUGLOC(SDNode *N); |
| 180 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 181 | bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 182 | SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2); |
| 183 | SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2, |
| 184 | SDOperand N3, ISD::CondCode CC); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 185 | SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1, |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 186 | ISD::CondCode Cond, bool foldBooleans = true); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 187 | |
| 188 | SDOperand BuildSDIV(SDNode *N); |
| 189 | SDOperand BuildUDIV(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 190 | public: |
| 191 | DAGCombiner(SelectionDAG &D) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 192 | : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {} |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 193 | |
| 194 | /// Run - runs the dag combiner on all nodes in the work list |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 195 | void Run(bool RunningAfterLegalize); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 196 | }; |
| 197 | } |
| 198 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 199 | struct ms { |
| 200 | int64_t m; // magic number |
| 201 | int64_t s; // shift amount |
| 202 | }; |
| 203 | |
| 204 | struct mu { |
| 205 | uint64_t m; // magic number |
| 206 | int64_t a; // add indicator |
| 207 | int64_t s; // shift amount |
| 208 | }; |
| 209 | |
| 210 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
| 211 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
| 212 | /// or -1. |
| 213 | static ms magic32(int32_t d) { |
| 214 | int32_t p; |
| 215 | uint32_t ad, anc, delta, q1, r1, q2, r2, t; |
| 216 | const uint32_t two31 = 0x80000000U; |
| 217 | struct ms mag; |
| 218 | |
| 219 | ad = abs(d); |
| 220 | t = two31 + ((uint32_t)d >> 31); |
| 221 | anc = t - 1 - t%ad; // absolute value of nc |
| 222 | p = 31; // initialize p |
| 223 | q1 = two31/anc; // initialize q1 = 2p/abs(nc) |
| 224 | r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 225 | q2 = two31/ad; // initialize q2 = 2p/abs(d) |
| 226 | r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 227 | do { |
| 228 | p = p + 1; |
| 229 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 230 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 231 | if (r1 >= anc) { // must be unsigned comparison |
| 232 | q1 = q1 + 1; |
| 233 | r1 = r1 - anc; |
| 234 | } |
| 235 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 236 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 237 | if (r2 >= ad) { // must be unsigned comparison |
| 238 | q2 = q2 + 1; |
| 239 | r2 = r2 - ad; |
| 240 | } |
| 241 | delta = ad - r2; |
| 242 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 243 | |
| 244 | mag.m = (int32_t)(q2 + 1); // make sure to sign extend |
| 245 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 246 | mag.s = p - 32; // resulting shift |
| 247 | return mag; |
| 248 | } |
| 249 | |
| 250 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 251 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 252 | static mu magicu32(uint32_t d) { |
| 253 | int32_t p; |
| 254 | uint32_t nc, delta, q1, r1, q2, r2; |
| 255 | struct mu magu; |
| 256 | magu.a = 0; // initialize "add" indicator |
| 257 | nc = - 1 - (-d)%d; |
| 258 | p = 31; // initialize p |
| 259 | q1 = 0x80000000/nc; // initialize q1 = 2p/nc |
| 260 | r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc) |
| 261 | q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d |
| 262 | r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d) |
| 263 | do { |
| 264 | p = p + 1; |
| 265 | if (r1 >= nc - r1 ) { |
| 266 | q1 = 2*q1 + 1; // update q1 |
| 267 | r1 = 2*r1 - nc; // update r1 |
| 268 | } |
| 269 | else { |
| 270 | q1 = 2*q1; // update q1 |
| 271 | r1 = 2*r1; // update r1 |
| 272 | } |
| 273 | if (r2 + 1 >= d - r2) { |
| 274 | if (q2 >= 0x7FFFFFFF) magu.a = 1; |
| 275 | q2 = 2*q2 + 1; // update q2 |
| 276 | r2 = 2*r2 + 1 - d; // update r2 |
| 277 | } |
| 278 | else { |
| 279 | if (q2 >= 0x80000000) magu.a = 1; |
| 280 | q2 = 2*q2; // update q2 |
| 281 | r2 = 2*r2 + 1; // update r2 |
| 282 | } |
| 283 | delta = d - 1 - r2; |
| 284 | } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); |
| 285 | magu.m = q2 + 1; // resulting magic number |
| 286 | magu.s = p - 32; // resulting shift |
| 287 | return magu; |
| 288 | } |
| 289 | |
| 290 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
| 291 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
| 292 | /// or -1. |
| 293 | static ms magic64(int64_t d) { |
| 294 | int64_t p; |
| 295 | uint64_t ad, anc, delta, q1, r1, q2, r2, t; |
| 296 | const uint64_t two63 = 9223372036854775808ULL; // 2^63 |
| 297 | struct ms mag; |
| 298 | |
Chris Lattner | f75f2a0 | 2005-10-20 17:01:00 +0000 | [diff] [blame] | 299 | ad = d >= 0 ? d : -d; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 300 | t = two63 + ((uint64_t)d >> 63); |
| 301 | anc = t - 1 - t%ad; // absolute value of nc |
| 302 | p = 63; // initialize p |
| 303 | q1 = two63/anc; // initialize q1 = 2p/abs(nc) |
| 304 | r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 305 | q2 = two63/ad; // initialize q2 = 2p/abs(d) |
| 306 | r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 307 | do { |
| 308 | p = p + 1; |
| 309 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 310 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 311 | if (r1 >= anc) { // must be unsigned comparison |
| 312 | q1 = q1 + 1; |
| 313 | r1 = r1 - anc; |
| 314 | } |
| 315 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 316 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 317 | if (r2 >= ad) { // must be unsigned comparison |
| 318 | q2 = q2 + 1; |
| 319 | r2 = r2 - ad; |
| 320 | } |
| 321 | delta = ad - r2; |
| 322 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 323 | |
| 324 | mag.m = q2 + 1; |
| 325 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 326 | mag.s = p - 64; // resulting shift |
| 327 | return mag; |
| 328 | } |
| 329 | |
| 330 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 331 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 332 | static mu magicu64(uint64_t d) |
| 333 | { |
| 334 | int64_t p; |
| 335 | uint64_t nc, delta, q1, r1, q2, r2; |
| 336 | struct mu magu; |
| 337 | magu.a = 0; // initialize "add" indicator |
| 338 | nc = - 1 - (-d)%d; |
| 339 | p = 63; // initialize p |
| 340 | q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc |
| 341 | r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc) |
| 342 | q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d |
| 343 | r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d) |
| 344 | do { |
| 345 | p = p + 1; |
| 346 | if (r1 >= nc - r1 ) { |
| 347 | q1 = 2*q1 + 1; // update q1 |
| 348 | r1 = 2*r1 - nc; // update r1 |
| 349 | } |
| 350 | else { |
| 351 | q1 = 2*q1; // update q1 |
| 352 | r1 = 2*r1; // update r1 |
| 353 | } |
| 354 | if (r2 + 1 >= d - r2) { |
| 355 | if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1; |
| 356 | q2 = 2*q2 + 1; // update q2 |
| 357 | r2 = 2*r2 + 1 - d; // update r2 |
| 358 | } |
| 359 | else { |
| 360 | if (q2 >= 0x8000000000000000ull) magu.a = 1; |
| 361 | q2 = 2*q2; // update q2 |
| 362 | r2 = 2*r2 + 1; // update r2 |
| 363 | } |
| 364 | delta = d - 1 - r2; |
| 365 | } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); |
| 366 | magu.m = q2 + 1; // resulting magic number |
| 367 | magu.s = p - 64; // resulting shift |
| 368 | return magu; |
| 369 | } |
| 370 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 371 | /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We use |
| 372 | /// this predicate to simplify operations downstream. Op and Mask are known to |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 373 | /// be the same type. |
| 374 | static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask, |
| 375 | const TargetLowering &TLI) { |
| 376 | unsigned SrcBits; |
| 377 | if (Mask == 0) return true; |
| 378 | |
| 379 | // If we know the result of a setcc has the top bits zero, use this info. |
| 380 | switch (Op.getOpcode()) { |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 381 | case ISD::Constant: |
| 382 | return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0; |
| 383 | case ISD::SETCC: |
| 384 | return ((Mask & 1) == 0) && |
| 385 | TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult; |
| 386 | case ISD::ZEXTLOAD: |
| 387 | SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT()); |
| 388 | return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits. |
| 389 | case ISD::ZERO_EXTEND: |
| 390 | SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType()); |
Chris Lattner | 7c22575 | 2005-11-02 01:47:04 +0000 | [diff] [blame] | 391 | return MaskedValueIsZero(Op.getOperand(0),Mask & (~0ULL >> (64-SrcBits)),TLI); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 392 | case ISD::AssertZext: |
| 393 | SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT()); |
| 394 | return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits. |
| 395 | case ISD::AND: |
Chris Lattner | ee899e6 | 2005-10-09 22:12:36 +0000 | [diff] [blame] | 396 | // If either of the operands has zero bits, the result will too. |
| 397 | if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) || |
| 398 | MaskedValueIsZero(Op.getOperand(0), Mask, TLI)) |
| 399 | return true; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 400 | // (X & C1) & C2 == 0 iff C1 & C2 == 0. |
| 401 | if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) |
| 402 | return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI); |
Chris Lattner | ee899e6 | 2005-10-09 22:12:36 +0000 | [diff] [blame] | 403 | return false; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 404 | case ISD::OR: |
| 405 | case ISD::XOR: |
| 406 | return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) && |
| 407 | MaskedValueIsZero(Op.getOperand(1), Mask, TLI); |
| 408 | case ISD::SELECT: |
| 409 | return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) && |
| 410 | MaskedValueIsZero(Op.getOperand(2), Mask, TLI); |
| 411 | case ISD::SELECT_CC: |
| 412 | return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) && |
| 413 | MaskedValueIsZero(Op.getOperand(3), Mask, TLI); |
| 414 | case ISD::SRL: |
| 415 | // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0 |
| 416 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 417 | uint64_t NewVal = Mask << ShAmt->getValue(); |
| 418 | SrcBits = MVT::getSizeInBits(Op.getValueType()); |
| 419 | if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1; |
| 420 | return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI); |
| 421 | } |
| 422 | return false; |
| 423 | case ISD::SHL: |
| 424 | // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0 |
| 425 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 426 | uint64_t NewVal = Mask >> ShAmt->getValue(); |
| 427 | return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI); |
| 428 | } |
| 429 | return false; |
Chris Lattner | bba9aa3 | 2005-10-10 16:51:40 +0000 | [diff] [blame] | 430 | case ISD::ADD: |
Chris Lattner | d739075 | 2005-10-10 16:52:03 +0000 | [diff] [blame] | 431 | // (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits. |
Chris Lattner | bba9aa3 | 2005-10-10 16:51:40 +0000 | [diff] [blame] | 432 | if ((Mask&(Mask+1)) == 0) { // All low bits |
| 433 | if (MaskedValueIsZero(Op.getOperand(0), Mask, TLI) && |
| 434 | MaskedValueIsZero(Op.getOperand(1), Mask, TLI)) |
| 435 | return true; |
| 436 | } |
| 437 | break; |
Chris Lattner | c4ced26 | 2005-10-07 15:30:32 +0000 | [diff] [blame] | 438 | case ISD::SUB: |
| 439 | if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) { |
| 440 | // We know that the top bits of C-X are clear if X contains less bits |
| 441 | // than C (i.e. no wrap-around can happen). For example, 20-X is |
| 442 | // positive if we can prove that X is >= 0 and < 16. |
| 443 | unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0)); |
| 444 | if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear |
| 445 | unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1); |
| 446 | uint64_t MaskV = (1ULL << (63-NLZ))-1; |
| 447 | if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) { |
| 448 | // High bits are clear this value is known to be >= C. |
| 449 | unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue()); |
| 450 | if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0) |
| 451 | return true; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | break; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 456 | case ISD::CTTZ: |
| 457 | case ISD::CTLZ: |
| 458 | case ISD::CTPOP: |
| 459 | // Bit counting instructions can not set the high bits of the result |
| 460 | // register. The max number of bits sets depends on the input. |
| 461 | return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0; |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 462 | default: |
| 463 | if (Op.getOpcode() >= ISD::BUILTIN_OP_END) |
| 464 | return TLI.isMaskedValueZeroForTargetNode(Op, Mask); |
| 465 | break; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 466 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 467 | return false; |
| 468 | } |
| 469 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 470 | // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc |
| 471 | // 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] | 472 | // Also, set the incoming LHS, RHS, and CC references to the appropriate |
| 473 | // nodes based on the type of node we are checking. This simplifies life a |
| 474 | // bit for the callers. |
| 475 | static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS, |
| 476 | SDOperand &CC) { |
| 477 | if (N.getOpcode() == ISD::SETCC) { |
| 478 | LHS = N.getOperand(0); |
| 479 | RHS = N.getOperand(1); |
| 480 | CC = N.getOperand(2); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 481 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 482 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 483 | if (N.getOpcode() == ISD::SELECT_CC && |
| 484 | N.getOperand(2).getOpcode() == ISD::Constant && |
| 485 | N.getOperand(3).getOpcode() == ISD::Constant && |
| 486 | cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 487 | cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) { |
| 488 | LHS = N.getOperand(0); |
| 489 | RHS = N.getOperand(1); |
| 490 | CC = N.getOperand(4); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 491 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 492 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 493 | return false; |
| 494 | } |
| 495 | |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 496 | // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only |
| 497 | // one use. If this is true, it allows the users to invert the operation for |
| 498 | // free when it is profitable to do so. |
| 499 | static bool isOneUseSetCC(SDOperand N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 500 | SDOperand N0, N1, N2; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 501 | if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse()) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 502 | return true; |
| 503 | return false; |
| 504 | } |
| 505 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 506 | // FIXME: This should probably go in the ISD class rather than being duplicated |
| 507 | // in several files. |
| 508 | static bool isCommutativeBinOp(unsigned Opcode) { |
| 509 | switch (Opcode) { |
| 510 | case ISD::ADD: |
| 511 | case ISD::MUL: |
| 512 | case ISD::AND: |
| 513 | case ISD::OR: |
| 514 | case ISD::XOR: return true; |
| 515 | default: return false; // FIXME: Need commutative info for user ops! |
| 516 | } |
| 517 | } |
| 518 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 519 | void DAGCombiner::Run(bool RunningAfterLegalize) { |
| 520 | // set the instance variable, so that the various visit routines may use it. |
| 521 | AfterLegalize = RunningAfterLegalize; |
| 522 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 523 | // Add all the dag nodes to the worklist. |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 524 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 525 | E = DAG.allnodes_end(); I != E; ++I) |
| 526 | WorkList.push_back(I); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 528 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 529 | // to the root node, preventing it from being deleted, and tracking any |
| 530 | // changes of the root. |
| 531 | HandleSDNode Dummy(DAG.getRoot()); |
| 532 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 533 | // while the worklist isn't empty, inspect the node on the end of it and |
| 534 | // try and combine it. |
| 535 | while (!WorkList.empty()) { |
| 536 | SDNode *N = WorkList.back(); |
| 537 | WorkList.pop_back(); |
| 538 | |
| 539 | // 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] | 540 | // N is deleted from the DAG, since they too may now be dead or may have a |
| 541 | // reduced number of uses, allowing other xforms. |
| 542 | if (N->use_empty() && N != &Dummy) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 543 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 544 | WorkList.push_back(N->getOperand(i).Val); |
| 545 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 546 | removeFromWorkList(N); |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 547 | DAG.DeleteNode(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 548 | continue; |
| 549 | } |
| 550 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 551 | SDOperand RV = visit(N); |
| 552 | if (RV.Val) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 553 | ++NodesCombined; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 554 | // If we get back the same node we passed in, rather than a new node or |
| 555 | // zero, we know that the node must have defined multiple values and |
| 556 | // CombineTo was used. Since CombineTo takes care of the worklist |
| 557 | // mechanics for us, we have no work to do in this case. |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 558 | if (RV.Val != N) { |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 559 | DEBUG(std::cerr << "\nReplacing "; N->dump(); |
| 560 | std::cerr << "\nWith: "; RV.Val->dump(); |
| 561 | std::cerr << '\n'); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 562 | std::vector<SDNode*> NowDead; |
| 563 | DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 564 | |
| 565 | // Push the new node and any users onto the worklist |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 566 | WorkList.push_back(RV.Val); |
| 567 | AddUsersToWorkList(RV.Val); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 568 | |
| 569 | // Nodes can end up on the worklist more than once. Make sure we do |
| 570 | // not process a node that has been replaced. |
| 571 | removeFromWorkList(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 572 | for (unsigned i = 0, e = NowDead.size(); i != e; ++i) |
| 573 | removeFromWorkList(NowDead[i]); |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 574 | |
| 575 | // Finally, since the node is now dead, remove it from the graph. |
| 576 | DAG.DeleteNode(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 577 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 580 | |
| 581 | // If the root changed (e.g. it was a dead load, update the root). |
| 582 | DAG.setRoot(Dummy.getValue()); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 585 | SDOperand DAGCombiner::visit(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 586 | switch(N->getOpcode()) { |
| 587 | default: break; |
Nate Begeman | 4942a96 | 2005-09-01 00:33:32 +0000 | [diff] [blame] | 588 | case ISD::TokenFactor: return visitTokenFactor(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 589 | case ISD::ADD: return visitADD(N); |
| 590 | case ISD::SUB: return visitSUB(N); |
| 591 | case ISD::MUL: return visitMUL(N); |
| 592 | case ISD::SDIV: return visitSDIV(N); |
| 593 | case ISD::UDIV: return visitUDIV(N); |
| 594 | case ISD::SREM: return visitSREM(N); |
| 595 | case ISD::UREM: return visitUREM(N); |
| 596 | case ISD::MULHU: return visitMULHU(N); |
| 597 | case ISD::MULHS: return visitMULHS(N); |
| 598 | case ISD::AND: return visitAND(N); |
| 599 | case ISD::OR: return visitOR(N); |
| 600 | case ISD::XOR: return visitXOR(N); |
| 601 | case ISD::SHL: return visitSHL(N); |
| 602 | case ISD::SRA: return visitSRA(N); |
| 603 | case ISD::SRL: return visitSRL(N); |
| 604 | case ISD::CTLZ: return visitCTLZ(N); |
| 605 | case ISD::CTTZ: return visitCTTZ(N); |
| 606 | case ISD::CTPOP: return visitCTPOP(N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 607 | case ISD::SELECT: return visitSELECT(N); |
| 608 | case ISD::SELECT_CC: return visitSELECT_CC(N); |
| 609 | case ISD::SETCC: return visitSETCC(N); |
Nate Begeman | 5054f16 | 2005-10-14 01:12:21 +0000 | [diff] [blame] | 610 | case ISD::ADD_PARTS: return visitADD_PARTS(N); |
| 611 | case ISD::SUB_PARTS: return visitSUB_PARTS(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 612 | case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); |
| 613 | case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); |
| 614 | case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); |
| 615 | case ISD::TRUNCATE: return visitTRUNCATE(N); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 616 | case ISD::BIT_CONVERT: return visitBIT_CONVERT(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 617 | case ISD::FADD: return visitFADD(N); |
| 618 | case ISD::FSUB: return visitFSUB(N); |
| 619 | case ISD::FMUL: return visitFMUL(N); |
| 620 | case ISD::FDIV: return visitFDIV(N); |
| 621 | case ISD::FREM: return visitFREM(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 622 | case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); |
| 623 | case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); |
| 624 | case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); |
| 625 | case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); |
| 626 | case ISD::FP_ROUND: return visitFP_ROUND(N); |
| 627 | case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); |
| 628 | case ISD::FP_EXTEND: return visitFP_EXTEND(N); |
| 629 | case ISD::FNEG: return visitFNEG(N); |
| 630 | case ISD::FABS: return visitFABS(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 631 | case ISD::BRCOND: return visitBRCOND(N); |
| 632 | case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N); |
| 633 | case ISD::BR_CC: return visitBR_CC(N); |
| 634 | case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 635 | case ISD::LOAD: return visitLOAD(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 636 | case ISD::STORE: return visitSTORE(N); |
Jim Laskey | d6e8d41 | 2005-12-23 20:08:28 +0000 | [diff] [blame] | 637 | case ISD::LOCATION: return visitLOCATION(N); |
| 638 | case ISD::DEBUG_LOC: return visitDEBUGLOC(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 639 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 640 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 643 | SDOperand DAGCombiner::visitTokenFactor(SDNode *N) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 644 | std::vector<SDOperand> Ops; |
| 645 | bool Changed = false; |
| 646 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 647 | // If the token factor has two operands and one is the entry token, replace |
| 648 | // the token factor with the other operand. |
| 649 | if (N->getNumOperands() == 2) { |
| 650 | if (N->getOperand(0).getOpcode() == ISD::EntryToken) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 651 | return N->getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 652 | if (N->getOperand(1).getOpcode() == ISD::EntryToken) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 653 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 654 | } |
Chris Lattner | 24edbb7 | 2005-10-13 22:10:05 +0000 | [diff] [blame] | 655 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 656 | // fold (tokenfactor (tokenfactor)) -> tokenfactor |
| 657 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 658 | SDOperand Op = N->getOperand(i); |
| 659 | if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) { |
| 660 | Changed = true; |
| 661 | for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j) |
| 662 | Ops.push_back(Op.getOperand(j)); |
| 663 | } else { |
| 664 | Ops.push_back(Op); |
| 665 | } |
| 666 | } |
| 667 | if (Changed) |
| 668 | return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 669 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 672 | SDOperand DAGCombiner::visitADD(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 673 | SDOperand N0 = N->getOperand(0); |
| 674 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 675 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 676 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 677 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 678 | |
| 679 | // fold (add c1, c2) -> c1+c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 680 | if (N0C && N1C) |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 681 | return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 682 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 683 | if (N0C && !N1C) |
| 684 | return DAG.getNode(ISD::ADD, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 685 | // fold (add x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 686 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 687 | return N0; |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 688 | // fold (add (add x, c1), c2) -> (add x, c1+c2) |
| 689 | if (N1C && N0.getOpcode() == ISD::ADD) { |
| 690 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 691 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 692 | if (N00C) |
| 693 | return DAG.getNode(ISD::ADD, VT, N0.getOperand(1), |
| 694 | DAG.getConstant(N1C->getValue()+N00C->getValue(), VT)); |
| 695 | if (N01C) |
| 696 | return DAG.getNode(ISD::ADD, VT, N0.getOperand(0), |
| 697 | DAG.getConstant(N1C->getValue()+N01C->getValue(), VT)); |
| 698 | } |
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame^] | 699 | |
| 700 | // fold ((c1-A)+c2) -> (c1+c2)-A |
| 701 | if (N1C && N0.getOpcode() == ISD::SUB) |
| 702 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0))) |
| 703 | return DAG.getNode(ISD::SUB, VT, |
| 704 | DAG.getConstant(N1C->getValue()+N0C->getValue(), VT), |
| 705 | N0.getOperand(1)); |
| 706 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 707 | // fold ((0-A) + B) -> B-A |
| 708 | if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) && |
| 709 | cast<ConstantSDNode>(N0.getOperand(0))->isNullValue()) |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 710 | return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 711 | // fold (A + (0-B)) -> A-B |
| 712 | if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && |
| 713 | cast<ConstantSDNode>(N1.getOperand(0))->isNullValue()) |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 714 | return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 715 | // fold (A+(B-A)) -> B |
| 716 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 717 | return N1.getOperand(0); |
| 718 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 721 | SDOperand DAGCombiner::visitSUB(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 722 | SDOperand N0 = N->getOperand(0); |
| 723 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 724 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 725 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 726 | |
Chris Lattner | 854077d | 2005-10-17 01:07:11 +0000 | [diff] [blame] | 727 | // fold (sub x, x) -> 0 |
| 728 | if (N0 == N1) |
| 729 | return DAG.getConstant(0, N->getValueType(0)); |
| 730 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 731 | // fold (sub c1, c2) -> c1-c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 732 | if (N0C && N1C) |
| 733 | return DAG.getConstant(N0C->getValue() - N1C->getValue(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 734 | N->getValueType(0)); |
Chris Lattner | 05b5743 | 2005-10-11 06:07:15 +0000 | [diff] [blame] | 735 | // fold (sub x, c) -> (add x, -c) |
| 736 | if (N1C) |
| 737 | return DAG.getNode(ISD::ADD, N0.getValueType(), N0, |
| 738 | DAG.getConstant(-N1C->getValue(), N0.getValueType())); |
| 739 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 740 | // fold (A+B)-A -> B |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 741 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 742 | return N0.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 743 | // fold (A+B)-B -> A |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 744 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 745 | return N0.getOperand(0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 746 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 749 | SDOperand DAGCombiner::visitMUL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 750 | SDOperand N0 = N->getOperand(0); |
| 751 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 752 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 753 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 754 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 755 | |
| 756 | // fold (mul c1, c2) -> c1*c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 757 | if (N0C && N1C) |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 758 | return DAG.getConstant(N0C->getValue() * N1C->getValue(), VT); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 759 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 760 | if (N0C && !N1C) |
| 761 | return DAG.getNode(ISD::MUL, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 762 | // fold (mul x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 763 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 764 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 765 | // fold (mul x, -1) -> 0-x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 766 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 767 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 768 | // fold (mul x, (1 << c)) -> x << c |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 769 | if (N1C && isPowerOf2_64(N1C->getValue())) |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 770 | return DAG.getNode(ISD::SHL, VT, N0, |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 771 | DAG.getConstant(Log2_64(N1C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 772 | TLI.getShiftAmountTy())); |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 773 | // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c |
| 774 | if (N1C && isPowerOf2_64(-N1C->getSignExtended())) { |
| 775 | // FIXME: If the input is something that is easily negated (e.g. a |
| 776 | // single-use add), we should put the negate there. |
| 777 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), |
| 778 | DAG.getNode(ISD::SHL, VT, N0, |
| 779 | DAG.getConstant(Log2_64(-N1C->getSignExtended()), |
| 780 | TLI.getShiftAmountTy()))); |
| 781 | } |
| 782 | |
| 783 | |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 784 | // fold (mul (mul x, c1), c2) -> (mul x, c1*c2) |
| 785 | if (N1C && N0.getOpcode() == ISD::MUL) { |
| 786 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 787 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 788 | if (N00C) |
| 789 | return DAG.getNode(ISD::MUL, VT, N0.getOperand(1), |
| 790 | DAG.getConstant(N1C->getValue()*N00C->getValue(), VT)); |
| 791 | if (N01C) |
| 792 | return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), |
| 793 | DAG.getConstant(N1C->getValue()*N01C->getValue(), VT)); |
| 794 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 795 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 798 | SDOperand DAGCombiner::visitSDIV(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 799 | SDOperand N0 = N->getOperand(0); |
| 800 | SDOperand N1 = N->getOperand(1); |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 801 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 802 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 803 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 804 | |
| 805 | // fold (sdiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 806 | if (N0C && N1C && !N1C->isNullValue()) |
| 807 | return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 808 | N->getValueType(0)); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 809 | // fold (sdiv X, 1) -> X |
| 810 | if (N1C && N1C->getSignExtended() == 1LL) |
| 811 | return N0; |
| 812 | // fold (sdiv X, -1) -> 0-X |
| 813 | if (N1C && N1C->isAllOnesValue()) |
| 814 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0); |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 815 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 816 | // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2 |
| 817 | uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1); |
| 818 | if (MaskedValueIsZero(N1, SignBit, TLI) && |
| 819 | MaskedValueIsZero(N0, SignBit, TLI)) |
| 820 | return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 821 | // fold (sdiv X, pow2) -> (add (sra X, log(pow2)), (srl X, sizeof(X)-1)) |
| 822 | if (N1C && N1C->getValue() && !TLI.isIntDivCheap() && |
| 823 | (isPowerOf2_64(N1C->getSignExtended()) || |
| 824 | isPowerOf2_64(-N1C->getSignExtended()))) { |
| 825 | // If dividing by powers of two is cheap, then don't perform the following |
| 826 | // fold. |
| 827 | if (TLI.isPow2DivCheap()) |
| 828 | return SDOperand(); |
| 829 | int64_t pow2 = N1C->getSignExtended(); |
| 830 | int64_t abs2 = pow2 > 0 ? pow2 : -pow2; |
| 831 | SDOperand SRL = DAG.getNode(ISD::SRL, VT, N0, |
| 832 | DAG.getConstant(MVT::getSizeInBits(VT)-1, |
| 833 | TLI.getShiftAmountTy())); |
| 834 | WorkList.push_back(SRL.Val); |
| 835 | SDOperand SGN = DAG.getNode(ISD::ADD, VT, N0, SRL); |
| 836 | WorkList.push_back(SGN.Val); |
| 837 | SDOperand SRA = DAG.getNode(ISD::SRA, VT, SGN, |
| 838 | DAG.getConstant(Log2_64(abs2), |
| 839 | TLI.getShiftAmountTy())); |
| 840 | // If we're dividing by a positive value, we're done. Otherwise, we must |
| 841 | // negate the result. |
| 842 | if (pow2 > 0) |
| 843 | return SRA; |
| 844 | WorkList.push_back(SRA.Val); |
| 845 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA); |
| 846 | } |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 847 | // if integer divide is expensive and we satisfy the requirements, emit an |
| 848 | // alternate sequence. |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 849 | if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) && |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 850 | !TLI.isIntDivCheap()) { |
| 851 | SDOperand Op = BuildSDIV(N); |
| 852 | if (Op.Val) return Op; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 853 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 854 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 857 | SDOperand DAGCombiner::visitUDIV(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 858 | SDOperand N0 = N->getOperand(0); |
| 859 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 860 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 861 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 862 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 863 | |
| 864 | // fold (udiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 865 | if (N0C && N1C && !N1C->isNullValue()) |
| 866 | return DAG.getConstant(N0C->getValue() / N1C->getValue(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 867 | N->getValueType(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 868 | // fold (udiv x, (1 << c)) -> x >>u c |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 869 | if (N1C && isPowerOf2_64(N1C->getValue())) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 870 | return DAG.getNode(ISD::SRL, N->getValueType(0), N0, |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 871 | DAG.getConstant(Log2_64(N1C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 872 | TLI.getShiftAmountTy())); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 873 | // fold (udiv x, c) -> alternate |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 874 | if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) { |
| 875 | SDOperand Op = BuildUDIV(N); |
| 876 | if (Op.Val) return Op; |
| 877 | } |
| 878 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 879 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 882 | SDOperand DAGCombiner::visitSREM(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 883 | SDOperand N0 = N->getOperand(0); |
| 884 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 885 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 886 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 887 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 888 | |
| 889 | // fold (srem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 890 | if (N0C && N1C && !N1C->isNullValue()) |
| 891 | return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 892 | N->getValueType(0)); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 893 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 894 | // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 |
| 895 | uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1); |
| 896 | if (MaskedValueIsZero(N1, SignBit, TLI) && |
| 897 | MaskedValueIsZero(N0, SignBit, TLI)) |
| 898 | return DAG.getNode(ISD::UREM, N1.getValueType(), N0, N1); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 899 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 902 | SDOperand DAGCombiner::visitUREM(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 903 | SDOperand N0 = N->getOperand(0); |
| 904 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 905 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 906 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 907 | |
| 908 | // fold (urem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 909 | if (N0C && N1C && !N1C->isNullValue()) |
| 910 | return DAG.getConstant(N0C->getValue() % N1C->getValue(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 911 | N->getValueType(0)); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 912 | // fold (urem x, pow2) -> (and x, pow2-1) |
| 913 | if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue())) |
| 914 | return DAG.getNode(ISD::AND, N0.getValueType(), N0, |
| 915 | DAG.getConstant(N1C->getValue()-1, N1.getValueType())); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 916 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 919 | SDOperand DAGCombiner::visitMULHS(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 | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 922 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 923 | |
| 924 | // fold (mulhs x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 925 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 926 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 927 | // fold (mulhs x, 1) -> (sra x, size(x)-1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 928 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 929 | return DAG.getNode(ISD::SRA, N0.getValueType(), N0, |
| 930 | DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1, |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 931 | TLI.getShiftAmountTy())); |
| 932 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 935 | SDOperand DAGCombiner::visitMULHU(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 936 | SDOperand N0 = N->getOperand(0); |
| 937 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 938 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 939 | |
| 940 | // fold (mulhu x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 941 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 942 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 943 | // fold (mulhu x, 1) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 944 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 945 | return DAG.getConstant(0, N0.getValueType()); |
| 946 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 949 | SDOperand DAGCombiner::visitAND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 950 | SDOperand N0 = N->getOperand(0); |
| 951 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 952 | SDOperand LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 953 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 954 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 955 | MVT::ValueType VT = N1.getValueType(); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 956 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 957 | |
| 958 | // fold (and c1, c2) -> c1&c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 959 | if (N0C && N1C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 960 | return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 961 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 962 | if (N0C && !N1C) |
| 963 | return DAG.getNode(ISD::AND, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 964 | // fold (and x, -1) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 965 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 966 | return N0; |
| 967 | // if (and x, c) is known to be zero, return 0 |
| 968 | if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI)) |
| 969 | return DAG.getConstant(0, VT); |
| 970 | // fold (and x, c) -> x iff (x & ~c) == 0 |
| 971 | if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)), |
| 972 | TLI)) |
| 973 | return N0; |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 974 | // fold (and (and x, c1), c2) -> (and x, c1^c2) |
| 975 | if (N1C && N0.getOpcode() == ISD::AND) { |
| 976 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 977 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 978 | if (N00C) |
| 979 | return DAG.getNode(ISD::AND, VT, N0.getOperand(1), |
| 980 | DAG.getConstant(N1C->getValue()&N00C->getValue(), VT)); |
| 981 | if (N01C) |
| 982 | return DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
| 983 | DAG.getConstant(N1C->getValue()&N01C->getValue(), VT)); |
| 984 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 985 | // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) |
Nate Begeman | 5dc7e86 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 986 | if (N1C && N0.getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 987 | unsigned ExtendBits = |
Jeff Cohen | 06d9b4a | 2005-11-12 00:59:01 +0000 | [diff] [blame] | 988 | MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT()); |
| 989 | if (ExtendBits == 64 || ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 990 | return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 991 | } |
| 992 | // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF |
Nate Begeman | 5dc7e86 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 993 | if (N1C && N0.getOpcode() == ISD::OR) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 994 | if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 995 | if ((ORI->getValue() & N1C->getValue()) == N1C->getValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 996 | return N1; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 997 | // fold (and (setcc x), (setcc y)) -> (setcc (and x, y)) |
| 998 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 999 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 1000 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
| 1001 | |
| 1002 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
| 1003 | MVT::isInteger(LL.getValueType())) { |
| 1004 | // fold (X == 0) & (Y == 0) -> (X|Y == 0) |
| 1005 | if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) { |
| 1006 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
| 1007 | WorkList.push_back(ORNode.Val); |
| 1008 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 1009 | } |
| 1010 | // fold (X == -1) & (Y == -1) -> (X&Y == -1) |
| 1011 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) { |
| 1012 | SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL); |
| 1013 | WorkList.push_back(ANDNode.Val); |
| 1014 | return DAG.getSetCC(VT, ANDNode, LR, Op1); |
| 1015 | } |
| 1016 | // fold (X > -1) & (Y > -1) -> (X|Y > -1) |
| 1017 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) { |
| 1018 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
| 1019 | WorkList.push_back(ORNode.Val); |
| 1020 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 1021 | } |
| 1022 | } |
| 1023 | // canonicalize equivalent to ll == rl |
| 1024 | if (LL == RR && LR == RL) { |
| 1025 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 1026 | std::swap(RL, RR); |
| 1027 | } |
| 1028 | if (LL == RL && LR == RR) { |
| 1029 | bool isInteger = MVT::isInteger(LL.getValueType()); |
| 1030 | ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger); |
| 1031 | if (Result != ISD::SETCC_INVALID) |
| 1032 | return DAG.getSetCC(N0.getValueType(), LL, LR, Result); |
| 1033 | } |
| 1034 | } |
| 1035 | // fold (and (zext x), (zext y)) -> (zext (and x, y)) |
| 1036 | if (N0.getOpcode() == ISD::ZERO_EXTEND && |
| 1037 | N1.getOpcode() == ISD::ZERO_EXTEND && |
| 1038 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) { |
| 1039 | SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(), |
| 1040 | N0.getOperand(0), N1.getOperand(0)); |
| 1041 | WorkList.push_back(ANDNode.Val); |
| 1042 | return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode); |
| 1043 | } |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1044 | // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y)) |
| 1045 | if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) || |
| 1046 | (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) && |
| 1047 | N0.getOperand(1) == N1.getOperand(1)) { |
| 1048 | SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(), |
| 1049 | N0.getOperand(0), N1.getOperand(0)); |
| 1050 | WorkList.push_back(ANDNode.Val); |
| 1051 | return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1)); |
| 1052 | } |
Chris Lattner | 85d63bb | 2005-10-15 22:18:08 +0000 | [diff] [blame] | 1053 | // fold (and (sra)) -> (and (srl)) when possible. |
Nate Begeman | 5dc7e86 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 1054 | if (N0.getOpcode() == ISD::SRA && N0.Val->hasOneUse()) { |
Chris Lattner | 85d63bb | 2005-10-15 22:18:08 +0000 | [diff] [blame] | 1055 | if (ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 1056 | // If the RHS of the AND has zeros where the sign bits of the SRA will |
| 1057 | // land, turn the SRA into an SRL. |
Chris Lattner | 750dbd5 | 2005-10-15 22:35:40 +0000 | [diff] [blame] | 1058 | if (MaskedValueIsZero(N1, (~0ULL << (OpSizeInBits-N01C->getValue())) & |
Chris Lattner | 85d63bb | 2005-10-15 22:18:08 +0000 | [diff] [blame] | 1059 | (~0ULL>>(64-OpSizeInBits)), TLI)) { |
| 1060 | WorkList.push_back(N); |
| 1061 | CombineTo(N0.Val, DAG.getNode(ISD::SRL, VT, N0.getOperand(0), |
| 1062 | N0.getOperand(1))); |
| 1063 | return SDOperand(); |
| 1064 | } |
| 1065 | } |
Nate Begeman | 5dc7e86 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 1066 | } |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1067 | // fold (zext_inreg (extload x)) -> (zextload x) |
Nate Begeman | 5054f16 | 2005-10-14 01:12:21 +0000 | [diff] [blame] | 1068 | if (N0.getOpcode() == ISD::EXTLOAD) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1069 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1070 | // If we zero all the possible extended bits, then we can turn this into |
| 1071 | // a zextload if we are running before legalize or the operation is legal. |
Nate Begeman | 5054f16 | 2005-10-14 01:12:21 +0000 | [diff] [blame] | 1072 | if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) && |
Chris Lattner | 67a44cd | 2005-10-13 18:16:34 +0000 | [diff] [blame] | 1073 | (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1074 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1075 | N0.getOperand(1), N0.getOperand(2), |
| 1076 | EVT); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1077 | WorkList.push_back(N); |
Chris Lattner | 67a44cd | 2005-10-13 18:16:34 +0000 | [diff] [blame] | 1078 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1079 | return SDOperand(); |
| 1080 | } |
| 1081 | } |
| 1082 | // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1083 | if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1084 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1085 | // If we zero all the possible extended bits, then we can turn this into |
| 1086 | // a zextload if we are running before legalize or the operation is legal. |
Nate Begeman | 5054f16 | 2005-10-14 01:12:21 +0000 | [diff] [blame] | 1087 | if (MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT), TLI) && |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1088 | (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1089 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1090 | N0.getOperand(1), N0.getOperand(2), |
| 1091 | EVT); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1092 | WorkList.push_back(N); |
Chris Lattner | 67a44cd | 2005-10-13 18:16:34 +0000 | [diff] [blame] | 1093 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1094 | return SDOperand(); |
| 1095 | } |
| 1096 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1097 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1100 | SDOperand DAGCombiner::visitOR(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1101 | SDOperand N0 = N->getOperand(0); |
| 1102 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1103 | SDOperand LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1104 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1105 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1106 | MVT::ValueType VT = N1.getValueType(); |
| 1107 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1108 | |
| 1109 | // fold (or c1, c2) -> c1|c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1110 | if (N0C && N1C) |
| 1111 | return DAG.getConstant(N0C->getValue() | N1C->getValue(), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1112 | N->getValueType(0)); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1113 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1114 | if (N0C && !N1C) |
| 1115 | return DAG.getNode(ISD::OR, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1116 | // fold (or x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1117 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1118 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1119 | // fold (or x, -1) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1120 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1121 | return N1; |
| 1122 | // fold (or x, c) -> c iff (x & ~c) == 0 |
| 1123 | if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)), |
| 1124 | TLI)) |
| 1125 | return N1; |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 1126 | // fold (or (or x, c1), c2) -> (or x, c1|c2) |
| 1127 | if (N1C && N0.getOpcode() == ISD::OR) { |
| 1128 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 1129 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 1130 | if (N00C) |
| 1131 | return DAG.getNode(ISD::OR, VT, N0.getOperand(1), |
| 1132 | DAG.getConstant(N1C->getValue()|N00C->getValue(), VT)); |
| 1133 | if (N01C) |
| 1134 | return DAG.getNode(ISD::OR, VT, N0.getOperand(0), |
| 1135 | DAG.getConstant(N1C->getValue()|N01C->getValue(), VT)); |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 1136 | } else if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() && |
| 1137 | isa<ConstantSDNode>(N0.getOperand(1))) { |
| 1138 | // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2) |
| 1139 | ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1)); |
| 1140 | return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0), |
| 1141 | N1), |
| 1142 | DAG.getConstant(N1C->getValue() | C1->getValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 1143 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1144 | // fold (or (setcc x), (setcc y)) -> (setcc (or x, y)) |
| 1145 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 1146 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 1147 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
| 1148 | |
| 1149 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
| 1150 | MVT::isInteger(LL.getValueType())) { |
| 1151 | // fold (X != 0) | (Y != 0) -> (X|Y != 0) |
| 1152 | // fold (X < 0) | (Y < 0) -> (X|Y < 0) |
| 1153 | if (cast<ConstantSDNode>(LR)->getValue() == 0 && |
| 1154 | (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) { |
| 1155 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
| 1156 | WorkList.push_back(ORNode.Val); |
| 1157 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 1158 | } |
| 1159 | // fold (X != -1) | (Y != -1) -> (X&Y != -1) |
| 1160 | // fold (X > -1) | (Y > -1) -> (X&Y > -1) |
| 1161 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && |
| 1162 | (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) { |
| 1163 | SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL); |
| 1164 | WorkList.push_back(ANDNode.Val); |
| 1165 | return DAG.getSetCC(VT, ANDNode, LR, Op1); |
| 1166 | } |
| 1167 | } |
| 1168 | // canonicalize equivalent to ll == rl |
| 1169 | if (LL == RR && LR == RL) { |
| 1170 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 1171 | std::swap(RL, RR); |
| 1172 | } |
| 1173 | if (LL == RL && LR == RR) { |
| 1174 | bool isInteger = MVT::isInteger(LL.getValueType()); |
| 1175 | ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger); |
| 1176 | if (Result != ISD::SETCC_INVALID) |
| 1177 | return DAG.getSetCC(N0.getValueType(), LL, LR, Result); |
| 1178 | } |
| 1179 | } |
| 1180 | // fold (or (zext x), (zext y)) -> (zext (or x, y)) |
| 1181 | if (N0.getOpcode() == ISD::ZERO_EXTEND && |
| 1182 | N1.getOpcode() == ISD::ZERO_EXTEND && |
| 1183 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) { |
| 1184 | SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(), |
| 1185 | N0.getOperand(0), N1.getOperand(0)); |
| 1186 | WorkList.push_back(ORNode.Val); |
| 1187 | return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode); |
| 1188 | } |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 1189 | // canonicalize shl to left side in a shl/srl pair, to match rotate |
| 1190 | if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL) |
| 1191 | std::swap(N0, N1); |
| 1192 | // check for rotl, rotr |
| 1193 | if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL && |
| 1194 | N0.getOperand(0) == N1.getOperand(0) && |
Chris Lattner | af551bc | 2006-01-12 18:57:33 +0000 | [diff] [blame] | 1195 | TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) { |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 1196 | // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1) |
| 1197 | if (N0.getOperand(1).getOpcode() == ISD::Constant && |
| 1198 | N1.getOperand(1).getOpcode() == ISD::Constant) { |
| 1199 | uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
| 1200 | uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue(); |
| 1201 | if ((c1val + c2val) == OpSizeInBits) |
| 1202 | return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1)); |
| 1203 | } |
| 1204 | // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y) |
| 1205 | if (N1.getOperand(1).getOpcode() == ISD::SUB && |
| 1206 | N0.getOperand(1) == N1.getOperand(1).getOperand(1)) |
| 1207 | if (ConstantSDNode *SUBC = |
| 1208 | dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0))) |
| 1209 | if (SUBC->getValue() == OpSizeInBits) |
| 1210 | return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1)); |
| 1211 | // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y) |
| 1212 | if (N0.getOperand(1).getOpcode() == ISD::SUB && |
| 1213 | N1.getOperand(1) == N0.getOperand(1).getOperand(1)) |
| 1214 | if (ConstantSDNode *SUBC = |
| 1215 | dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0))) |
| 1216 | if (SUBC->getValue() == OpSizeInBits) { |
Chris Lattner | af551bc | 2006-01-12 18:57:33 +0000 | [diff] [blame] | 1217 | if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT)) |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 1218 | return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0), |
| 1219 | N1.getOperand(1)); |
| 1220 | else |
| 1221 | return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), |
| 1222 | N0.getOperand(1)); |
| 1223 | } |
| 1224 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1225 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1228 | SDOperand DAGCombiner::visitXOR(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1229 | SDOperand N0 = N->getOperand(0); |
| 1230 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1231 | SDOperand LHS, RHS, CC; |
| 1232 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1233 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1234 | MVT::ValueType VT = N0.getValueType(); |
| 1235 | |
| 1236 | // fold (xor c1, c2) -> c1^c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1237 | if (N0C && N1C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1238 | return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1239 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1240 | if (N0C && !N1C) |
| 1241 | return DAG.getNode(ISD::XOR, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1242 | // fold (xor x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1243 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1244 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1245 | // fold !(x cc y) -> (x !cc y) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1246 | if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) { |
| 1247 | bool isInt = MVT::isInteger(LHS.getValueType()); |
| 1248 | ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
| 1249 | isInt); |
| 1250 | if (N0.getOpcode() == ISD::SETCC) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1251 | return DAG.getSetCC(VT, LHS, RHS, NotCC); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1252 | if (N0.getOpcode() == ISD::SELECT_CC) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1253 | return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1254 | assert(0 && "Unhandled SetCC Equivalent!"); |
| 1255 | abort(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1256 | } |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1257 | // fold !(x or y) -> (!x and !y) iff x or y are setcc |
| 1258 | if (N1C && N1C->getValue() == 1 && |
| 1259 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1260 | SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1261 | if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) { |
| 1262 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1263 | LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS |
| 1264 | RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1265 | WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val); |
| 1266 | return DAG.getNode(NewOpcode, VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1269 | // fold !(x or y) -> (!x and !y) iff x or y are constants |
| 1270 | if (N1C && N1C->isAllOnesValue() && |
| 1271 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1272 | SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1273 | if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) { |
| 1274 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1275 | LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS |
| 1276 | RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1277 | WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val); |
| 1278 | return DAG.getNode(NewOpcode, VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1279 | } |
| 1280 | } |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 1281 | // fold (xor (xor x, c1), c2) -> (xor x, c1^c2) |
| 1282 | if (N1C && N0.getOpcode() == ISD::XOR) { |
| 1283 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 1284 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 1285 | if (N00C) |
| 1286 | return DAG.getNode(ISD::XOR, VT, N0.getOperand(1), |
| 1287 | DAG.getConstant(N1C->getValue()^N00C->getValue(), VT)); |
| 1288 | if (N01C) |
| 1289 | return DAG.getNode(ISD::XOR, VT, N0.getOperand(0), |
| 1290 | DAG.getConstant(N1C->getValue()^N01C->getValue(), VT)); |
| 1291 | } |
| 1292 | // fold (xor x, x) -> 0 |
| 1293 | if (N0 == N1) |
| 1294 | return DAG.getConstant(0, VT); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1295 | // fold (xor (zext x), (zext y)) -> (zext (xor x, y)) |
| 1296 | if (N0.getOpcode() == ISD::ZERO_EXTEND && |
| 1297 | N1.getOpcode() == ISD::ZERO_EXTEND && |
| 1298 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) { |
| 1299 | SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(), |
| 1300 | N0.getOperand(0), N1.getOperand(0)); |
| 1301 | WorkList.push_back(XORNode.Val); |
| 1302 | return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode); |
| 1303 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1304 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1307 | SDOperand DAGCombiner::visitSHL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1308 | SDOperand N0 = N->getOperand(0); |
| 1309 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1310 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1311 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1312 | MVT::ValueType VT = N0.getValueType(); |
| 1313 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 1314 | |
| 1315 | // fold (shl c1, c2) -> c1<<c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1316 | if (N0C && N1C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1317 | return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1318 | // fold (shl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1319 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1320 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1321 | // fold (shl x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1322 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1323 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1324 | // fold (shl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1325 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1326 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1327 | // if (shl x, c) is known to be zero, return 0 |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1328 | if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI)) |
| 1329 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1330 | // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1331 | if (N1C && N0.getOpcode() == ISD::SHL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1332 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1333 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1334 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1335 | if (c1 + c2 > OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1336 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1337 | return DAG.getNode(ISD::SHL, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1338 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1339 | } |
| 1340 | // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or |
| 1341 | // (srl (and x, -1 << c1), c1-c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1342 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1343 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1344 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1345 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1346 | SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
| 1347 | DAG.getConstant(~0ULL << c1, VT)); |
| 1348 | if (c2 > c1) |
| 1349 | return DAG.getNode(ISD::SHL, VT, Mask, |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1350 | DAG.getConstant(c2-c1, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1351 | else |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1352 | return DAG.getNode(ISD::SRL, VT, Mask, |
| 1353 | DAG.getConstant(c1-c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1354 | } |
| 1355 | // fold (shl (sra x, c1), c1) -> (and x, -1 << c1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1356 | if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1357 | return DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1358 | DAG.getConstant(~0ULL << N1C->getValue(), VT)); |
| 1359 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1362 | SDOperand DAGCombiner::visitSRA(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1363 | SDOperand N0 = N->getOperand(0); |
| 1364 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1365 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1366 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1367 | MVT::ValueType VT = N0.getValueType(); |
| 1368 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 1369 | |
| 1370 | // fold (sra c1, c2) -> c1>>c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1371 | if (N0C && N1C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1372 | return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1373 | // fold (sra 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1374 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1375 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1376 | // fold (sra -1, x) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1377 | if (N0C && N0C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1378 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1379 | // fold (sra x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1380 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1381 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1382 | // fold (sra x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1383 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1384 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1385 | // If the sign bit is known to be zero, switch this to a SRL. |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1386 | if (MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1387 | return DAG.getNode(ISD::SRL, VT, N0, N1); |
| 1388 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1391 | SDOperand DAGCombiner::visitSRL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1392 | SDOperand N0 = N->getOperand(0); |
| 1393 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1394 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1395 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1396 | MVT::ValueType VT = N0.getValueType(); |
| 1397 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 1398 | |
| 1399 | // fold (srl c1, c2) -> c1 >>u c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1400 | if (N0C && N1C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1401 | return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1402 | // fold (srl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1403 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1404 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1405 | // fold (srl x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1406 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1407 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1408 | // fold (srl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1409 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1410 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1411 | // if (srl x, c) is known to be zero, return 0 |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1412 | if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI)) |
| 1413 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1414 | // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1415 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1416 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1417 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1418 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1419 | if (c1 + c2 > OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1420 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1421 | return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1422 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1423 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1424 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1427 | SDOperand DAGCombiner::visitCTLZ(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1428 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1429 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1430 | |
| 1431 | // fold (ctlz c1) -> c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1432 | if (N0C) |
| 1433 | return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1434 | N0.getValueType()); |
| 1435 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1438 | SDOperand DAGCombiner::visitCTTZ(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1439 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1440 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1441 | |
| 1442 | // fold (cttz c1) -> c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1443 | if (N0C) |
| 1444 | return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1445 | N0.getValueType()); |
| 1446 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1449 | SDOperand DAGCombiner::visitCTPOP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1450 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1451 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1452 | |
| 1453 | // fold (ctpop c1) -> c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1454 | if (N0C) |
| 1455 | return DAG.getConstant(CountPopulation_64(N0C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1456 | N0.getValueType()); |
| 1457 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1460 | SDOperand DAGCombiner::visitSELECT(SDNode *N) { |
| 1461 | SDOperand N0 = N->getOperand(0); |
| 1462 | SDOperand N1 = N->getOperand(1); |
| 1463 | SDOperand N2 = N->getOperand(2); |
| 1464 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1465 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1466 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
| 1467 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1468 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1469 | // fold select C, X, X -> X |
| 1470 | if (N1 == N2) |
| 1471 | return N1; |
| 1472 | // fold select true, X, Y -> X |
| 1473 | if (N0C && !N0C->isNullValue()) |
| 1474 | return N1; |
| 1475 | // fold select false, X, Y -> Y |
| 1476 | if (N0C && N0C->isNullValue()) |
| 1477 | return N2; |
| 1478 | // fold select C, 1, X -> C | X |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1479 | if (MVT::i1 == VT && N1C && N1C->getValue() == 1) |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1480 | return DAG.getNode(ISD::OR, VT, N0, N2); |
| 1481 | // fold select C, 0, X -> ~C & X |
| 1482 | // FIXME: this should check for C type == X type, not i1? |
| 1483 | if (MVT::i1 == VT && N1C && N1C->isNullValue()) { |
| 1484 | SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT)); |
| 1485 | WorkList.push_back(XORNode.Val); |
| 1486 | return DAG.getNode(ISD::AND, VT, XORNode, N2); |
| 1487 | } |
| 1488 | // fold select C, X, 1 -> ~C | X |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1489 | if (MVT::i1 == VT && N2C && N2C->getValue() == 1) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1490 | SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT)); |
| 1491 | WorkList.push_back(XORNode.Val); |
| 1492 | return DAG.getNode(ISD::OR, VT, XORNode, N1); |
| 1493 | } |
| 1494 | // fold select C, X, 0 -> C & X |
| 1495 | // FIXME: this should check for C type == X type, not i1? |
| 1496 | if (MVT::i1 == VT && N2C && N2C->isNullValue()) |
| 1497 | return DAG.getNode(ISD::AND, VT, N0, N1); |
| 1498 | // fold X ? X : Y --> X ? 1 : Y --> X | Y |
| 1499 | if (MVT::i1 == VT && N0 == N1) |
| 1500 | return DAG.getNode(ISD::OR, VT, N0, N2); |
| 1501 | // fold X ? Y : X --> X ? Y : 0 --> X & Y |
| 1502 | if (MVT::i1 == VT && N0 == N2) |
| 1503 | return DAG.getNode(ISD::AND, VT, N0, N1); |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1504 | |
| 1505 | // If we can fold this based on the true/false value, do so. |
| 1506 | if (SimplifySelectOps(N, N1, N2)) |
| 1507 | return SDOperand(); |
| 1508 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1509 | // fold selects based on a setcc into other things, such as min/max/abs |
| 1510 | if (N0.getOpcode() == ISD::SETCC) |
| 1511 | return SimplifySelect(N0, N1, N2); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1512 | return SDOperand(); |
| 1513 | } |
| 1514 | |
| 1515 | SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) { |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1516 | SDOperand N0 = N->getOperand(0); |
| 1517 | SDOperand N1 = N->getOperand(1); |
| 1518 | SDOperand N2 = N->getOperand(2); |
| 1519 | SDOperand N3 = N->getOperand(3); |
| 1520 | SDOperand N4 = N->getOperand(4); |
| 1521 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1522 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1523 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
| 1524 | ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get(); |
| 1525 | |
| 1526 | // Determine if the condition we're dealing with is constant |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 1527 | SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false); |
Chris Lattner | 9155902 | 2005-10-05 04:45:43 +0000 | [diff] [blame] | 1528 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val); |
| 1529 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1530 | // fold select_cc lhs, rhs, x, x, cc -> x |
| 1531 | if (N2 == N3) |
| 1532 | return N2; |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1533 | |
| 1534 | // If we can fold this based on the true/false value, do so. |
| 1535 | if (SimplifySelectOps(N, N2, N3)) |
| 1536 | return SDOperand(); |
| 1537 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1538 | // fold select_cc into other things, such as min/max/abs |
| 1539 | return SimplifySelectCC(N0, N1, N2, N3, CC); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | SDOperand DAGCombiner::visitSETCC(SDNode *N) { |
| 1543 | return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1), |
| 1544 | cast<CondCodeSDNode>(N->getOperand(2))->get()); |
| 1545 | } |
| 1546 | |
Nate Begeman | 5054f16 | 2005-10-14 01:12:21 +0000 | [diff] [blame] | 1547 | SDOperand DAGCombiner::visitADD_PARTS(SDNode *N) { |
| 1548 | SDOperand LHSLo = N->getOperand(0); |
| 1549 | SDOperand RHSLo = N->getOperand(2); |
| 1550 | MVT::ValueType VT = LHSLo.getValueType(); |
| 1551 | |
| 1552 | // fold (a_Hi, 0) + (b_Hi, b_Lo) -> (b_Hi + a_Hi, b_Lo) |
| 1553 | if (MaskedValueIsZero(LHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) { |
| 1554 | SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1), |
| 1555 | N->getOperand(3)); |
| 1556 | WorkList.push_back(Hi.Val); |
| 1557 | CombineTo(N, RHSLo, Hi); |
| 1558 | return SDOperand(); |
| 1559 | } |
| 1560 | // fold (a_Hi, a_Lo) + (b_Hi, 0) -> (a_Hi + b_Hi, a_Lo) |
| 1561 | if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) { |
| 1562 | SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1), |
| 1563 | N->getOperand(3)); |
| 1564 | WorkList.push_back(Hi.Val); |
| 1565 | CombineTo(N, LHSLo, Hi); |
| 1566 | return SDOperand(); |
| 1567 | } |
| 1568 | return SDOperand(); |
| 1569 | } |
| 1570 | |
| 1571 | SDOperand DAGCombiner::visitSUB_PARTS(SDNode *N) { |
| 1572 | SDOperand LHSLo = N->getOperand(0); |
| 1573 | SDOperand RHSLo = N->getOperand(2); |
| 1574 | MVT::ValueType VT = LHSLo.getValueType(); |
| 1575 | |
| 1576 | // fold (a_Hi, a_Lo) - (b_Hi, 0) -> (a_Hi - b_Hi, a_Lo) |
| 1577 | if (MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1, TLI)) { |
| 1578 | SDOperand Hi = DAG.getNode(ISD::SUB, VT, N->getOperand(1), |
| 1579 | N->getOperand(3)); |
| 1580 | WorkList.push_back(Hi.Val); |
| 1581 | CombineTo(N, LHSLo, Hi); |
| 1582 | return SDOperand(); |
| 1583 | } |
| 1584 | return SDOperand(); |
| 1585 | } |
| 1586 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1587 | SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1588 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1589 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1590 | MVT::ValueType VT = N->getValueType(0); |
| 1591 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1592 | // fold (sext c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1593 | if (N0C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1594 | return DAG.getConstant(N0C->getSignExtended(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1595 | // fold (sext (sext x)) -> (sext x) |
| 1596 | if (N0.getOpcode() == ISD::SIGN_EXTEND) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1597 | return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0)); |
Chris Lattner | b14ab8a | 2005-12-07 07:11:03 +0000 | [diff] [blame] | 1598 | // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size. |
Chris Lattner | cc2210b | 2005-12-07 18:02:05 +0000 | [diff] [blame] | 1599 | if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&& |
| 1600 | (!AfterLegalize || |
| 1601 | TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType()))) |
Chris Lattner | b14ab8a | 2005-12-07 07:11:03 +0000 | [diff] [blame] | 1602 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), |
| 1603 | DAG.getValueType(N0.getValueType())); |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1604 | // fold (sext (load x)) -> (sext (truncate (sextload x))) |
Chris Lattner | d0f6d18 | 2005-12-15 19:02:38 +0000 | [diff] [blame] | 1605 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() && |
| 1606 | (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){ |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1607 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1608 | N0.getOperand(1), N0.getOperand(2), |
| 1609 | N0.getValueType()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1610 | CombineTo(N, ExtLoad); |
Chris Lattner | f988405 | 2005-10-13 21:52:31 +0000 | [diff] [blame] | 1611 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1612 | ExtLoad.getValue(1)); |
Nate Begeman | 765784a | 2005-10-12 23:18:53 +0000 | [diff] [blame] | 1613 | return SDOperand(); |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1614 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1615 | |
| 1616 | // fold (sext (sextload x)) -> (sext (truncate (sextload x))) |
| 1617 | // fold (sext ( extload x)) -> (sext (truncate (sextload x))) |
| 1618 | if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) && |
| 1619 | N0.hasOneUse()) { |
| 1620 | SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1621 | N0.getOperand(1), N0.getOperand(2), |
| 1622 | N0.getOperand(3)); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1623 | CombineTo(N, ExtLoad); |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1624 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1625 | ExtLoad.getValue(1)); |
| 1626 | return SDOperand(); |
| 1627 | } |
| 1628 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1629 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1632 | SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1633 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1634 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1635 | MVT::ValueType VT = N->getValueType(0); |
| 1636 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1637 | // fold (zext c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1638 | if (N0C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1639 | return DAG.getConstant(N0C->getValue(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1640 | // fold (zext (zext x)) -> (zext x) |
| 1641 | if (N0.getOpcode() == ISD::ZERO_EXTEND) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1642 | return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0)); |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1643 | // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size. |
| 1644 | if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&& |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1645 | (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType()))) |
Chris Lattner | 00cb95c | 2005-12-14 07:58:38 +0000 | [diff] [blame] | 1646 | return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType()); |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1647 | // fold (zext (load x)) -> (zext (truncate (zextload x))) |
Chris Lattner | d0f6d18 | 2005-12-15 19:02:38 +0000 | [diff] [blame] | 1648 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() && |
| 1649 | (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){ |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1650 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1651 | N0.getOperand(1), N0.getOperand(2), |
| 1652 | N0.getValueType()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1653 | CombineTo(N, ExtLoad); |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1654 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1655 | ExtLoad.getValue(1)); |
| 1656 | return SDOperand(); |
| 1657 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1658 | |
| 1659 | // fold (zext (zextload x)) -> (zext (truncate (zextload x))) |
| 1660 | // fold (zext ( extload x)) -> (zext (truncate (zextload x))) |
| 1661 | if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) && |
| 1662 | N0.hasOneUse()) { |
| 1663 | SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1664 | N0.getOperand(1), N0.getOperand(2), |
| 1665 | N0.getOperand(3)); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1666 | CombineTo(N, ExtLoad); |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1667 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1668 | ExtLoad.getValue(1)); |
| 1669 | return SDOperand(); |
| 1670 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1671 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1674 | SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1675 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1676 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1677 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1678 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1679 | MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT(); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 1680 | unsigned EVTBits = MVT::getSizeInBits(EVT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1681 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1682 | // fold (sext_in_reg c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1683 | if (N0C) { |
| 1684 | SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1685 | return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1686 | } |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1687 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1 |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1688 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Nate Begeman | 216def8 | 2005-10-14 01:29:07 +0000 | [diff] [blame] | 1689 | cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) { |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1690 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1691 | } |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1692 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 |
| 1693 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 1694 | EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) { |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1695 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1696 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1697 | // fold (sext_in_reg (assert_sext x)) -> (assert_sext x) |
| 1698 | if (N0.getOpcode() == ISD::AssertSext && |
| 1699 | cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) { |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1700 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1701 | } |
| 1702 | // fold (sext_in_reg (sextload x)) -> (sextload x) |
| 1703 | if (N0.getOpcode() == ISD::SEXTLOAD && |
| 1704 | cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) { |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1705 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1706 | } |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1707 | // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1 |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1708 | if (N0.getOpcode() == ISD::SETCC && |
| 1709 | TLI.getSetCCResultContents() == |
| 1710 | TargetLowering::ZeroOrNegativeOneSetCCResult) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1711 | return N0; |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 1712 | // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero |
| 1713 | if (MaskedValueIsZero(N0, 1ULL << (EVTBits-1), TLI)) |
| 1714 | return DAG.getNode(ISD::AND, N0.getValueType(), N0, |
| 1715 | DAG.getConstant(~0ULL >> (64-EVTBits), VT)); |
| 1716 | // fold (sext_in_reg (srl x)) -> sra x |
| 1717 | if (N0.getOpcode() == ISD::SRL && |
| 1718 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 1719 | cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) { |
| 1720 | return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0), |
| 1721 | N0.getOperand(1)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1722 | } |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1723 | // fold (sext_inreg (extload x)) -> (sextload x) |
| 1724 | if (N0.getOpcode() == ISD::EXTLOAD && |
| 1725 | EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() && |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1726 | (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1727 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1728 | N0.getOperand(1), N0.getOperand(2), |
| 1729 | EVT); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1730 | CombineTo(N, ExtLoad); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1731 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1732 | return SDOperand(); |
| 1733 | } |
| 1734 | // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1735 | if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() && |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1736 | EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() && |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1737 | (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1738 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1739 | N0.getOperand(1), N0.getOperand(2), |
| 1740 | EVT); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1741 | CombineTo(N, ExtLoad); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1742 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1743 | return SDOperand(); |
| 1744 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1745 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1748 | SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1749 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1750 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1751 | MVT::ValueType VT = N->getValueType(0); |
| 1752 | |
| 1753 | // noop truncate |
| 1754 | if (N0.getValueType() == N->getValueType(0)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1755 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1756 | // fold (truncate c1) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1757 | if (N0C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1758 | return DAG.getConstant(N0C->getValue(), VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1759 | // fold (truncate (truncate x)) -> (truncate x) |
| 1760 | if (N0.getOpcode() == ISD::TRUNCATE) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1761 | return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1762 | // fold (truncate (ext x)) -> (ext x) or (truncate x) or x |
| 1763 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){ |
| 1764 | if (N0.getValueType() < VT) |
| 1765 | // 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] | 1766 | return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1767 | else if (N0.getValueType() > VT) |
| 1768 | // 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] | 1769 | return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1770 | else |
| 1771 | // if the source and dest are the same type, we can drop both the extend |
| 1772 | // and the truncate |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1773 | return N0.getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1774 | } |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1775 | // fold (truncate (load x)) -> (smaller load x) |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1776 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) { |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1777 | assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) && |
| 1778 | "Cannot truncate to larger type!"); |
| 1779 | MVT::ValueType PtrType = N0.getOperand(1).getValueType(); |
Nate Begeman | 765784a | 2005-10-12 23:18:53 +0000 | [diff] [blame] | 1780 | // For big endian targets, we need to add an offset to the pointer to load |
| 1781 | // the correct bytes. For little endian systems, we merely need to read |
| 1782 | // fewer bytes from the same pointer. |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1783 | uint64_t PtrOff = |
| 1784 | (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8; |
Nate Begeman | 765784a | 2005-10-12 23:18:53 +0000 | [diff] [blame] | 1785 | SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) : |
| 1786 | DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1), |
| 1787 | DAG.getConstant(PtrOff, PtrType)); |
| 1788 | WorkList.push_back(NewPtr.Val); |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1789 | SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2)); |
Nate Begeman | 765784a | 2005-10-12 23:18:53 +0000 | [diff] [blame] | 1790 | WorkList.push_back(N); |
Chris Lattner | 24edbb7 | 2005-10-13 22:10:05 +0000 | [diff] [blame] | 1791 | CombineTo(N0.Val, Load, Load.getValue(1)); |
Nate Begeman | 765784a | 2005-10-12 23:18:53 +0000 | [diff] [blame] | 1792 | return SDOperand(); |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1793 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1794 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1797 | SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) { |
| 1798 | SDOperand N0 = N->getOperand(0); |
| 1799 | MVT::ValueType VT = N->getValueType(0); |
| 1800 | |
| 1801 | // If the input is a constant, let getNode() fold it. |
| 1802 | if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) { |
| 1803 | SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0); |
| 1804 | if (Res.Val != N) return Res; |
| 1805 | } |
| 1806 | |
Chris Lattner | c8547d8 | 2005-12-23 05:37:50 +0000 | [diff] [blame] | 1807 | if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2) |
| 1808 | return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0)); |
| 1809 | |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 1810 | // fold (conv (load x)) -> (load (conv*)x) |
| 1811 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) { |
| 1812 | SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1), |
| 1813 | N0.getOperand(2)); |
| 1814 | WorkList.push_back(N); |
| 1815 | CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load), |
| 1816 | Load.getValue(1)); |
| 1817 | return Load; |
| 1818 | } |
| 1819 | |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1820 | return SDOperand(); |
| 1821 | } |
| 1822 | |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1823 | SDOperand DAGCombiner::visitFADD(SDNode *N) { |
| 1824 | SDOperand N0 = N->getOperand(0); |
| 1825 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1826 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 1827 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1828 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1829 | |
| 1830 | // fold (fadd c1, c2) -> c1+c2 |
| 1831 | if (N0CFP && N1CFP) |
| 1832 | return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), VT); |
| 1833 | // canonicalize constant to RHS |
| 1834 | if (N0CFP && !N1CFP) |
| 1835 | return DAG.getNode(ISD::FADD, VT, N1, N0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1836 | // fold (A + (-B)) -> A-B |
| 1837 | if (N1.getOpcode() == ISD::FNEG) |
| 1838 | return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1839 | // fold ((-A) + B) -> B-A |
| 1840 | if (N0.getOpcode() == ISD::FNEG) |
| 1841 | return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1842 | return SDOperand(); |
| 1843 | } |
| 1844 | |
| 1845 | SDOperand DAGCombiner::visitFSUB(SDNode *N) { |
| 1846 | SDOperand N0 = N->getOperand(0); |
| 1847 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1848 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 1849 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1850 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1851 | |
| 1852 | // fold (fsub c1, c2) -> c1-c2 |
| 1853 | if (N0CFP && N1CFP) |
| 1854 | return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(), VT); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1855 | // fold (A-(-B)) -> A+B |
| 1856 | if (N1.getOpcode() == ISD::FNEG) |
| 1857 | return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1858 | return SDOperand(); |
| 1859 | } |
| 1860 | |
| 1861 | SDOperand DAGCombiner::visitFMUL(SDNode *N) { |
| 1862 | SDOperand N0 = N->getOperand(0); |
| 1863 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 1864 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 1865 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1866 | MVT::ValueType VT = N->getValueType(0); |
| 1867 | |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 1868 | // fold (fmul c1, c2) -> c1*c2 |
| 1869 | if (N0CFP && N1CFP) |
| 1870 | return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(), VT); |
| 1871 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1872 | if (N0CFP && !N1CFP) |
| 1873 | return DAG.getNode(ISD::FMUL, VT, N1, N0); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 1874 | // fold (fmul X, 2.0) -> (fadd X, X) |
| 1875 | if (N1CFP && N1CFP->isExactlyValue(+2.0)) |
| 1876 | return DAG.getNode(ISD::FADD, VT, N0, N0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1877 | return SDOperand(); |
| 1878 | } |
| 1879 | |
| 1880 | SDOperand DAGCombiner::visitFDIV(SDNode *N) { |
| 1881 | SDOperand N0 = N->getOperand(0); |
| 1882 | SDOperand N1 = N->getOperand(1); |
| 1883 | MVT::ValueType VT = N->getValueType(0); |
| 1884 | |
| 1885 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0)) |
| 1886 | if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 1887 | // fold floating point (fdiv c1, c2) |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 1888 | return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(), VT); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1889 | } |
| 1890 | return SDOperand(); |
| 1891 | } |
| 1892 | |
| 1893 | SDOperand DAGCombiner::visitFREM(SDNode *N) { |
| 1894 | SDOperand N0 = N->getOperand(0); |
| 1895 | SDOperand N1 = N->getOperand(1); |
| 1896 | MVT::ValueType VT = N->getValueType(0); |
| 1897 | |
| 1898 | if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0)) |
| 1899 | if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 1900 | // fold floating point (frem c1, c2) -> fmod(c1, c2) |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 1901 | return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()), VT); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1902 | } |
| 1903 | return SDOperand(); |
| 1904 | } |
| 1905 | |
| 1906 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1907 | SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1908 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1909 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1910 | |
| 1911 | // fold (sint_to_fp c1) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1912 | if (N0C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1913 | return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0)); |
| 1914 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1917 | SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1918 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1919 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1920 | |
| 1921 | // fold (uint_to_fp c1) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1922 | if (N0C) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1923 | return DAG.getConstantFP(N0C->getValue(), N->getValueType(0)); |
| 1924 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1927 | SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1928 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1929 | |
| 1930 | // fold (fp_to_sint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1931 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1932 | return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0)); |
| 1933 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1936 | SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1937 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1938 | |
| 1939 | // fold (fp_to_uint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1940 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1941 | return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0)); |
| 1942 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1945 | SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1946 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1947 | |
| 1948 | // fold (fp_round c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1949 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1950 | return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0)); |
| 1951 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1954 | SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1955 | SDOperand N0 = N->getOperand(0); |
| 1956 | MVT::ValueType VT = N->getValueType(0); |
| 1957 | MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1958 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1959 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1960 | // fold (fp_round_inreg c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1961 | if (N0CFP) { |
| 1962 | SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1963 | return DAG.getNode(ISD::FP_EXTEND, VT, Round); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1964 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1965 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1968 | SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1969 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1970 | |
| 1971 | // fold (fp_extend c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1972 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1973 | return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0)); |
| 1974 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1977 | SDOperand DAGCombiner::visitFNEG(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1978 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1979 | // fold (neg c1) -> -c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1980 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1981 | return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1982 | // fold (neg (sub x, y)) -> (sub y, x) |
| 1983 | if (N->getOperand(0).getOpcode() == ISD::SUB) |
| 1984 | return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1985 | N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1986 | // fold (neg (neg x)) -> x |
| 1987 | if (N->getOperand(0).getOpcode() == ISD::FNEG) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1988 | return N->getOperand(0).getOperand(0); |
| 1989 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1992 | SDOperand DAGCombiner::visitFABS(SDNode *N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1993 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1994 | // fold (fabs c1) -> fabs(c1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1995 | if (N0CFP) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1996 | return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1997 | // fold (fabs (fabs x)) -> (fabs x) |
| 1998 | if (N->getOperand(0).getOpcode() == ISD::FABS) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1999 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2000 | // fold (fabs (fneg x)) -> (fabs x) |
| 2001 | if (N->getOperand(0).getOpcode() == ISD::FNEG) |
| 2002 | return DAG.getNode(ISD::FABS, N->getValueType(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2003 | N->getOperand(0).getOperand(0)); |
| 2004 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2007 | SDOperand DAGCombiner::visitBRCOND(SDNode *N) { |
| 2008 | SDOperand Chain = N->getOperand(0); |
| 2009 | SDOperand N1 = N->getOperand(1); |
| 2010 | SDOperand N2 = N->getOperand(2); |
| 2011 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 2012 | |
| 2013 | // never taken branch, fold to chain |
| 2014 | if (N1C && N1C->isNullValue()) |
| 2015 | return Chain; |
| 2016 | // unconditional branch |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 2017 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2018 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N2); |
| 2019 | return SDOperand(); |
| 2020 | } |
| 2021 | |
| 2022 | SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) { |
| 2023 | SDOperand Chain = N->getOperand(0); |
| 2024 | SDOperand N1 = N->getOperand(1); |
| 2025 | SDOperand N2 = N->getOperand(2); |
| 2026 | SDOperand N3 = N->getOperand(3); |
| 2027 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 2028 | |
| 2029 | // unconditional branch to true mbb |
| 2030 | if (N1C && N1C->getValue() == 1) |
| 2031 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N2); |
| 2032 | // unconditional branch to false mbb |
| 2033 | if (N1C && N1C->isNullValue()) |
| 2034 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N3); |
| 2035 | return SDOperand(); |
| 2036 | } |
| 2037 | |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 2038 | // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB. |
| 2039 | // |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2040 | SDOperand DAGCombiner::visitBR_CC(SDNode *N) { |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 2041 | CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1)); |
| 2042 | SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3); |
| 2043 | |
| 2044 | // Use SimplifySetCC to simplify SETCC's. |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 2045 | SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false); |
| 2046 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val); |
| 2047 | |
| 2048 | // fold br_cc true, dest -> br dest (unconditional branch) |
| 2049 | if (SCCC && SCCC->getValue()) |
| 2050 | return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0), |
| 2051 | N->getOperand(4)); |
| 2052 | // fold br_cc false, dest -> unconditional fall through |
| 2053 | if (SCCC && SCCC->isNullValue()) |
| 2054 | return N->getOperand(0); |
| 2055 | // fold to a simpler setcc |
| 2056 | if (Simp.Val && Simp.getOpcode() == ISD::SETCC) |
| 2057 | return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0), |
| 2058 | Simp.getOperand(2), Simp.getOperand(0), |
| 2059 | Simp.getOperand(1), N->getOperand(4)); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2060 | return SDOperand(); |
| 2061 | } |
| 2062 | |
| 2063 | SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) { |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 2064 | SDOperand Chain = N->getOperand(0); |
| 2065 | SDOperand CCN = N->getOperand(1); |
| 2066 | SDOperand LHS = N->getOperand(2); |
| 2067 | SDOperand RHS = N->getOperand(3); |
| 2068 | SDOperand N4 = N->getOperand(4); |
| 2069 | SDOperand N5 = N->getOperand(5); |
| 2070 | |
| 2071 | SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS, |
| 2072 | cast<CondCodeSDNode>(CCN)->get(), false); |
| 2073 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val); |
| 2074 | |
| 2075 | // fold select_cc lhs, rhs, x, x, cc -> x |
| 2076 | if (N4 == N5) |
| 2077 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N4); |
| 2078 | // fold select_cc true, x, y -> x |
| 2079 | if (SCCC && SCCC->getValue()) |
| 2080 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N4); |
| 2081 | // fold select_cc false, x, y -> y |
| 2082 | if (SCCC && SCCC->isNullValue()) |
| 2083 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N5); |
| 2084 | // fold to a simpler setcc |
| 2085 | if (SCC.Val && SCC.getOpcode() == ISD::SETCC) |
| 2086 | return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0), |
| 2087 | SCC.getOperand(1), N4, N5); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2088 | return SDOperand(); |
| 2089 | } |
| 2090 | |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 2091 | SDOperand DAGCombiner::visitLOAD(SDNode *N) { |
| 2092 | SDOperand Chain = N->getOperand(0); |
| 2093 | SDOperand Ptr = N->getOperand(1); |
| 2094 | SDOperand SrcValue = N->getOperand(2); |
| 2095 | |
| 2096 | // If this load is directly stored, replace the load value with the stored |
| 2097 | // value. |
| 2098 | // TODO: Handle store large -> read small portion. |
| 2099 | // TODO: Handle TRUNCSTORE/EXTLOAD |
| 2100 | if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr && |
| 2101 | Chain.getOperand(1).getValueType() == N->getValueType(0)) |
| 2102 | return CombineTo(N, Chain.getOperand(1), Chain); |
| 2103 | |
| 2104 | return SDOperand(); |
| 2105 | } |
| 2106 | |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2107 | SDOperand DAGCombiner::visitSTORE(SDNode *N) { |
| 2108 | SDOperand Chain = N->getOperand(0); |
| 2109 | SDOperand Value = N->getOperand(1); |
| 2110 | SDOperand Ptr = N->getOperand(2); |
| 2111 | SDOperand SrcValue = N->getOperand(3); |
| 2112 | |
| 2113 | // 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] | 2114 | if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr && |
Chris Lattner | fe7f046 | 2005-10-27 07:10:34 +0000 | [diff] [blame] | 2115 | Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ && |
| 2116 | // Make sure that these stores are the same value type: |
| 2117 | // FIXME: we really care that the second store is >= size of the first. |
| 2118 | Value.getValueType() == Chain.getOperand(1).getValueType()) { |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2119 | // Create a new store of Value that replaces both stores. |
| 2120 | SDNode *PrevStore = Chain.Val; |
Chris Lattner | 04ecf6d | 2005-10-10 23:00:08 +0000 | [diff] [blame] | 2121 | if (PrevStore->getOperand(1) == Value) // Same value multiply stored. |
| 2122 | return Chain; |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2123 | SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other, |
| 2124 | PrevStore->getOperand(0), Value, Ptr, |
| 2125 | SrcValue); |
Chris Lattner | 04ecf6d | 2005-10-10 23:00:08 +0000 | [diff] [blame] | 2126 | CombineTo(N, NewStore); // Nuke this store. |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2127 | CombineTo(PrevStore, NewStore); // Nuke the previous store. |
Chris Lattner | 04ecf6d | 2005-10-10 23:00:08 +0000 | [diff] [blame] | 2128 | return SDOperand(N, 0); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Chris Lattner | c33baaa | 2005-12-23 05:48:07 +0000 | [diff] [blame] | 2131 | // If this is a store of a bit convert, store the input value. |
| 2132 | if (Value.getOpcode() == ISD::BIT_CONVERT) |
| 2133 | return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0), |
| 2134 | Ptr, SrcValue); |
| 2135 | |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2136 | return SDOperand(); |
| 2137 | } |
| 2138 | |
Jim Laskey | d6e8d41 | 2005-12-23 20:08:28 +0000 | [diff] [blame] | 2139 | SDOperand DAGCombiner::visitLOCATION(SDNode *N) { |
| 2140 | SDOperand Chain = N->getOperand(0); |
| 2141 | |
| 2142 | // Remove redundant locations (last one holds) |
| 2143 | if (Chain.getOpcode() == ISD::LOCATION && Chain.hasOneUse()) { |
| 2144 | return DAG.getNode(ISD::LOCATION, MVT::Other, Chain.getOperand(0), |
| 2145 | N->getOperand(1), |
| 2146 | N->getOperand(2), |
| 2147 | N->getOperand(3), |
| 2148 | N->getOperand(4)); |
| 2149 | } |
| 2150 | |
| 2151 | return SDOperand(); |
| 2152 | } |
| 2153 | |
| 2154 | SDOperand DAGCombiner::visitDEBUGLOC(SDNode *N) { |
| 2155 | SDOperand Chain = N->getOperand(0); |
| 2156 | |
| 2157 | // Remove redundant debug locations (last one holds) |
| 2158 | if (Chain.getOpcode() == ISD::DEBUG_LOC && Chain.hasOneUse()) { |
| 2159 | return DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Chain.getOperand(0), |
| 2160 | N->getOperand(1), |
| 2161 | N->getOperand(2), |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 2162 | N->getOperand(3)); |
Jim Laskey | d6e8d41 | 2005-12-23 20:08:28 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | return SDOperand(); |
| 2166 | } |
| 2167 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2168 | SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){ |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 2169 | assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!"); |
| 2170 | |
| 2171 | SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2, |
| 2172 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 2173 | // If we got a simplified select_cc node back from SimplifySelectCC, then |
| 2174 | // break it down into a new SETCC node, and a new SELECT node, and then return |
| 2175 | // the SELECT node, since we were called with a SELECT node. |
| 2176 | if (SCC.Val) { |
| 2177 | // Check to see if we got a select_cc back (to turn into setcc/select). |
| 2178 | // Otherwise, just return whatever node we got back, like fabs. |
| 2179 | if (SCC.getOpcode() == ISD::SELECT_CC) { |
| 2180 | SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(), |
| 2181 | SCC.getOperand(0), SCC.getOperand(1), |
| 2182 | SCC.getOperand(4)); |
| 2183 | WorkList.push_back(SETCC.Val); |
| 2184 | return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2), |
| 2185 | SCC.getOperand(3), SETCC); |
| 2186 | } |
| 2187 | return SCC; |
| 2188 | } |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2189 | return SDOperand(); |
| 2190 | } |
| 2191 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 2192 | /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS |
| 2193 | /// are the two values being selected between, see if we can simplify the |
| 2194 | /// select. |
| 2195 | /// |
| 2196 | bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS, |
| 2197 | SDOperand RHS) { |
| 2198 | |
| 2199 | // If this is a select from two identical things, try to pull the operation |
| 2200 | // through the select. |
| 2201 | if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){ |
| 2202 | #if 0 |
| 2203 | std::cerr << "SELECT: ["; LHS.Val->dump(); |
| 2204 | std::cerr << "] ["; RHS.Val->dump(); |
| 2205 | std::cerr << "]\n"; |
| 2206 | #endif |
| 2207 | |
| 2208 | // If this is a load and the token chain is identical, replace the select |
| 2209 | // of two loads with a load through a select of the address to load from. |
| 2210 | // This triggers in things like "select bool X, 10.0, 123.0" after the FP |
| 2211 | // constants have been dropped into the constant pool. |
| 2212 | if ((LHS.getOpcode() == ISD::LOAD || |
| 2213 | LHS.getOpcode() == ISD::EXTLOAD || |
| 2214 | LHS.getOpcode() == ISD::ZEXTLOAD || |
| 2215 | LHS.getOpcode() == ISD::SEXTLOAD) && |
| 2216 | // Token chains must be identical. |
| 2217 | LHS.getOperand(0) == RHS.getOperand(0) && |
| 2218 | // If this is an EXTLOAD, the VT's must match. |
| 2219 | (LHS.getOpcode() == ISD::LOAD || |
| 2220 | LHS.getOperand(3) == RHS.getOperand(3))) { |
| 2221 | // FIXME: this conflates two src values, discarding one. This is not |
| 2222 | // the right thing to do, but nothing uses srcvalues now. When they do, |
| 2223 | // turn SrcValue into a list of locations. |
| 2224 | SDOperand Addr; |
| 2225 | if (TheSelect->getOpcode() == ISD::SELECT) |
| 2226 | Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(), |
| 2227 | TheSelect->getOperand(0), LHS.getOperand(1), |
| 2228 | RHS.getOperand(1)); |
| 2229 | else |
| 2230 | Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(), |
| 2231 | TheSelect->getOperand(0), |
| 2232 | TheSelect->getOperand(1), |
| 2233 | LHS.getOperand(1), RHS.getOperand(1), |
| 2234 | TheSelect->getOperand(4)); |
| 2235 | |
| 2236 | SDOperand Load; |
| 2237 | if (LHS.getOpcode() == ISD::LOAD) |
| 2238 | Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0), |
| 2239 | Addr, LHS.getOperand(2)); |
| 2240 | else |
| 2241 | Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0), |
| 2242 | LHS.getOperand(0), Addr, LHS.getOperand(2), |
| 2243 | cast<VTSDNode>(LHS.getOperand(3))->getVT()); |
| 2244 | // Users of the select now use the result of the load. |
| 2245 | CombineTo(TheSelect, Load); |
| 2246 | |
| 2247 | // Users of the old loads now use the new load's chain. We know the |
| 2248 | // old-load value is dead now. |
| 2249 | CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1)); |
| 2250 | CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1)); |
| 2251 | return true; |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | return false; |
| 2256 | } |
| 2257 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2258 | SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1, |
| 2259 | SDOperand N2, SDOperand N3, |
| 2260 | ISD::CondCode CC) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 2261 | |
| 2262 | MVT::ValueType VT = N2.getValueType(); |
| 2263 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 2264 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 2265 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); |
| 2266 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); |
| 2267 | |
| 2268 | // Determine if the condition we're dealing with is constant |
| 2269 | SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false); |
| 2270 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val); |
| 2271 | |
| 2272 | // fold select_cc true, x, y -> x |
| 2273 | if (SCCC && SCCC->getValue()) |
| 2274 | return N2; |
| 2275 | // fold select_cc false, x, y -> y |
| 2276 | if (SCCC && SCCC->getValue() == 0) |
| 2277 | return N3; |
| 2278 | |
| 2279 | // Check to see if we can simplify the select into an fabs node |
| 2280 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 2281 | // Allow either -0.0 or 0.0 |
| 2282 | if (CFP->getValue() == 0.0) { |
| 2283 | // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs |
| 2284 | if ((CC == ISD::SETGE || CC == ISD::SETGT) && |
| 2285 | N0 == N2 && N3.getOpcode() == ISD::FNEG && |
| 2286 | N2 == N3.getOperand(0)) |
| 2287 | return DAG.getNode(ISD::FABS, VT, N0); |
| 2288 | |
| 2289 | // select (setl[te] X, +/-0.0), fneg(X), X -> fabs |
| 2290 | if ((CC == ISD::SETLT || CC == ISD::SETLE) && |
| 2291 | N0 == N3 && N2.getOpcode() == ISD::FNEG && |
| 2292 | N2.getOperand(0) == N3) |
| 2293 | return DAG.getNode(ISD::FABS, VT, N3); |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | // Check to see if we can perform the "gzip trick", transforming |
| 2298 | // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A |
| 2299 | if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() && |
| 2300 | MVT::isInteger(N0.getValueType()) && |
| 2301 | MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) { |
| 2302 | MVT::ValueType XType = N0.getValueType(); |
| 2303 | MVT::ValueType AType = N2.getValueType(); |
| 2304 | if (XType >= AType) { |
| 2305 | // 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] | 2306 | // single-bit constant. |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 2307 | if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) { |
| 2308 | unsigned ShCtV = Log2_64(N2C->getValue()); |
| 2309 | ShCtV = MVT::getSizeInBits(XType)-ShCtV-1; |
| 2310 | SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy()); |
| 2311 | SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt); |
| 2312 | WorkList.push_back(Shift.Val); |
| 2313 | if (XType > AType) { |
| 2314 | Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift); |
| 2315 | WorkList.push_back(Shift.Val); |
| 2316 | } |
| 2317 | return DAG.getNode(ISD::AND, AType, Shift, N2); |
| 2318 | } |
| 2319 | SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0, |
| 2320 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 2321 | TLI.getShiftAmountTy())); |
| 2322 | WorkList.push_back(Shift.Val); |
| 2323 | if (XType > AType) { |
| 2324 | Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift); |
| 2325 | WorkList.push_back(Shift.Val); |
| 2326 | } |
| 2327 | return DAG.getNode(ISD::AND, AType, Shift, N2); |
| 2328 | } |
| 2329 | } |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 2330 | |
| 2331 | // fold select C, 16, 0 -> shl C, 4 |
| 2332 | if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) && |
| 2333 | TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) { |
| 2334 | // Get a SetCC of the condition |
| 2335 | // FIXME: Should probably make sure that setcc is legal if we ever have a |
| 2336 | // target where it isn't. |
| 2337 | SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC); |
| 2338 | WorkList.push_back(SCC.Val); |
| 2339 | // cast from setcc result type to select result type |
| 2340 | if (AfterLegalize) |
| 2341 | Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType()); |
| 2342 | else |
| 2343 | Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC); |
| 2344 | WorkList.push_back(Temp.Val); |
| 2345 | // shl setcc result by log2 n2c |
| 2346 | return DAG.getNode(ISD::SHL, N2.getValueType(), Temp, |
| 2347 | DAG.getConstant(Log2_64(N2C->getValue()), |
| 2348 | TLI.getShiftAmountTy())); |
| 2349 | } |
| 2350 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 2351 | // Check to see if this is the equivalent of setcc |
| 2352 | // FIXME: Turn all of these into setcc if setcc if setcc is legal |
| 2353 | // otherwise, go ahead with the folds. |
| 2354 | if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) { |
| 2355 | MVT::ValueType XType = N0.getValueType(); |
| 2356 | if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) { |
| 2357 | SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC); |
| 2358 | if (Res.getValueType() != VT) |
| 2359 | Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res); |
| 2360 | return Res; |
| 2361 | } |
| 2362 | |
| 2363 | // seteq X, 0 -> srl (ctlz X, log2(size(X))) |
| 2364 | if (N1C && N1C->isNullValue() && CC == ISD::SETEQ && |
| 2365 | TLI.isOperationLegal(ISD::CTLZ, XType)) { |
| 2366 | SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0); |
| 2367 | return DAG.getNode(ISD::SRL, XType, Ctlz, |
| 2368 | DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)), |
| 2369 | TLI.getShiftAmountTy())); |
| 2370 | } |
| 2371 | // setgt X, 0 -> srl (and (-X, ~X), size(X)-1) |
| 2372 | if (N1C && N1C->isNullValue() && CC == ISD::SETGT) { |
| 2373 | SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType), |
| 2374 | N0); |
| 2375 | SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0, |
| 2376 | DAG.getConstant(~0ULL, XType)); |
| 2377 | return DAG.getNode(ISD::SRL, XType, |
| 2378 | DAG.getNode(ISD::AND, XType, NegN0, NotN0), |
| 2379 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 2380 | TLI.getShiftAmountTy())); |
| 2381 | } |
| 2382 | // setgt X, -1 -> xor (srl (X, size(X)-1), 1) |
| 2383 | if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) { |
| 2384 | SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0, |
| 2385 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 2386 | TLI.getShiftAmountTy())); |
| 2387 | return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType)); |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X -> |
| 2392 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
| 2393 | if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) && |
| 2394 | N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) { |
| 2395 | if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) { |
| 2396 | MVT::ValueType XType = N0.getValueType(); |
| 2397 | if (SubC->isNullValue() && MVT::isInteger(XType)) { |
| 2398 | SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0, |
| 2399 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 2400 | TLI.getShiftAmountTy())); |
| 2401 | SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift); |
| 2402 | WorkList.push_back(Shift.Val); |
| 2403 | WorkList.push_back(Add.Val); |
| 2404 | return DAG.getNode(ISD::XOR, XType, Add, Shift); |
| 2405 | } |
| 2406 | } |
| 2407 | } |
| 2408 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2409 | return SDOperand(); |
| 2410 | } |
| 2411 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 2412 | SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0, |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 2413 | SDOperand N1, ISD::CondCode Cond, |
| 2414 | bool foldBooleans) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 2415 | // These setcc operations always fold. |
| 2416 | switch (Cond) { |
| 2417 | default: break; |
| 2418 | case ISD::SETFALSE: |
| 2419 | case ISD::SETFALSE2: return DAG.getConstant(0, VT); |
| 2420 | case ISD::SETTRUE: |
| 2421 | case ISD::SETTRUE2: return DAG.getConstant(1, VT); |
| 2422 | } |
| 2423 | |
| 2424 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { |
| 2425 | uint64_t C1 = N1C->getValue(); |
| 2426 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) { |
| 2427 | uint64_t C0 = N0C->getValue(); |
| 2428 | |
| 2429 | // Sign extend the operands if required |
| 2430 | if (ISD::isSignedIntSetCC(Cond)) { |
| 2431 | C0 = N0C->getSignExtended(); |
| 2432 | C1 = N1C->getSignExtended(); |
| 2433 | } |
| 2434 | |
| 2435 | switch (Cond) { |
| 2436 | default: assert(0 && "Unknown integer setcc!"); |
| 2437 | case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT); |
| 2438 | case ISD::SETNE: return DAG.getConstant(C0 != C1, VT); |
| 2439 | case ISD::SETULT: return DAG.getConstant(C0 < C1, VT); |
| 2440 | case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT); |
| 2441 | case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT); |
| 2442 | case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT); |
| 2443 | case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT); |
| 2444 | case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT); |
| 2445 | case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT); |
| 2446 | case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT); |
| 2447 | } |
| 2448 | } else { |
| 2449 | // If the LHS is a ZERO_EXTEND, perform the comparison on the input. |
| 2450 | if (N0.getOpcode() == ISD::ZERO_EXTEND) { |
| 2451 | unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType()); |
| 2452 | |
| 2453 | // If the comparison constant has bits in the upper part, the |
| 2454 | // zero-extended value could never match. |
| 2455 | if (C1 & (~0ULL << InSize)) { |
| 2456 | unsigned VSize = MVT::getSizeInBits(N0.getValueType()); |
| 2457 | switch (Cond) { |
| 2458 | case ISD::SETUGT: |
| 2459 | case ISD::SETUGE: |
| 2460 | case ISD::SETEQ: return DAG.getConstant(0, VT); |
| 2461 | case ISD::SETULT: |
| 2462 | case ISD::SETULE: |
| 2463 | case ISD::SETNE: return DAG.getConstant(1, VT); |
| 2464 | case ISD::SETGT: |
| 2465 | case ISD::SETGE: |
| 2466 | // True if the sign bit of C1 is set. |
| 2467 | return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT); |
| 2468 | case ISD::SETLT: |
| 2469 | case ISD::SETLE: |
| 2470 | // True if the sign bit of C1 isn't set. |
| 2471 | return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT); |
| 2472 | default: |
| 2473 | break; |
| 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | // Otherwise, we can perform the comparison with the low bits. |
| 2478 | switch (Cond) { |
| 2479 | case ISD::SETEQ: |
| 2480 | case ISD::SETNE: |
| 2481 | case ISD::SETUGT: |
| 2482 | case ISD::SETUGE: |
| 2483 | case ISD::SETULT: |
| 2484 | case ISD::SETULE: |
| 2485 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 2486 | DAG.getConstant(C1, N0.getOperand(0).getValueType()), |
| 2487 | Cond); |
| 2488 | default: |
| 2489 | break; // todo, be more careful with signed comparisons |
| 2490 | } |
| 2491 | } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 2492 | (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { |
| 2493 | MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT(); |
| 2494 | unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy); |
| 2495 | MVT::ValueType ExtDstTy = N0.getValueType(); |
| 2496 | unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy); |
| 2497 | |
| 2498 | // If the extended part has any inconsistent bits, it cannot ever |
| 2499 | // compare equal. In other words, they have to be all ones or all |
| 2500 | // zeros. |
| 2501 | uint64_t ExtBits = |
| 2502 | (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1)); |
| 2503 | if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits) |
| 2504 | return DAG.getConstant(Cond == ISD::SETNE, VT); |
| 2505 | |
| 2506 | SDOperand ZextOp; |
| 2507 | MVT::ValueType Op0Ty = N0.getOperand(0).getValueType(); |
| 2508 | if (Op0Ty == ExtSrcTy) { |
| 2509 | ZextOp = N0.getOperand(0); |
| 2510 | } else { |
| 2511 | int64_t Imm = ~0ULL >> (64-ExtSrcTyBits); |
| 2512 | ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0), |
| 2513 | DAG.getConstant(Imm, Op0Ty)); |
| 2514 | } |
| 2515 | WorkList.push_back(ZextOp.Val); |
| 2516 | // Otherwise, make this a use of a zext. |
| 2517 | return DAG.getSetCC(VT, ZextOp, |
| 2518 | DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)), |
| 2519 | ExtDstTy), |
| 2520 | Cond); |
| 2521 | } |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 2522 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 2523 | uint64_t MinVal, MaxVal; |
| 2524 | unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0)); |
| 2525 | if (ISD::isSignedIntSetCC(Cond)) { |
| 2526 | MinVal = 1ULL << (OperandBitSize-1); |
| 2527 | if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. |
| 2528 | MaxVal = ~0ULL >> (65-OperandBitSize); |
| 2529 | else |
| 2530 | MaxVal = 0; |
| 2531 | } else { |
| 2532 | MinVal = 0; |
| 2533 | MaxVal = ~0ULL >> (64-OperandBitSize); |
| 2534 | } |
| 2535 | |
| 2536 | // Canonicalize GE/LE comparisons to use GT/LT comparisons. |
| 2537 | if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { |
| 2538 | if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true |
| 2539 | --C1; // X >= C0 --> X > (C0-1) |
| 2540 | return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()), |
| 2541 | (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); |
| 2542 | } |
| 2543 | |
| 2544 | if (Cond == ISD::SETLE || Cond == ISD::SETULE) { |
| 2545 | if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true |
| 2546 | ++C1; // X <= C0 --> X < (C0+1) |
| 2547 | return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()), |
| 2548 | (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); |
| 2549 | } |
| 2550 | |
| 2551 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal) |
| 2552 | return DAG.getConstant(0, VT); // X < MIN --> false |
| 2553 | |
| 2554 | // Canonicalize setgt X, Min --> setne X, Min |
| 2555 | if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal) |
| 2556 | return DAG.getSetCC(VT, N0, N1, ISD::SETNE); |
Chris Lattner | c8597ca | 2005-10-21 21:23:25 +0000 | [diff] [blame] | 2557 | // Canonicalize setlt X, Max --> setne X, Max |
| 2558 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal) |
| 2559 | return DAG.getSetCC(VT, N0, N1, ISD::SETNE); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 2560 | |
| 2561 | // If we have setult X, 1, turn it into seteq X, 0 |
| 2562 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1) |
| 2563 | return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()), |
| 2564 | ISD::SETEQ); |
| 2565 | // If we have setugt X, Max-1, turn it into seteq X, Max |
| 2566 | else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1) |
| 2567 | return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()), |
| 2568 | ISD::SETEQ); |
| 2569 | |
| 2570 | // If we have "setcc X, C0", check to see if we can shrink the immediate |
| 2571 | // by changing cc. |
| 2572 | |
| 2573 | // SETUGT X, SINTMAX -> SETLT X, 0 |
| 2574 | if (Cond == ISD::SETUGT && OperandBitSize != 1 && |
| 2575 | C1 == (~0ULL >> (65-OperandBitSize))) |
| 2576 | return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()), |
| 2577 | ISD::SETLT); |
| 2578 | |
| 2579 | // FIXME: Implement the rest of these. |
| 2580 | |
| 2581 | // Fold bit comparisons when we can. |
| 2582 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 2583 | VT == N0.getValueType() && N0.getOpcode() == ISD::AND) |
| 2584 | if (ConstantSDNode *AndRHS = |
| 2585 | dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 2586 | if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 |
| 2587 | // Perform the xform if the AND RHS is a single bit. |
| 2588 | if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) { |
| 2589 | return DAG.getNode(ISD::SRL, VT, N0, |
| 2590 | DAG.getConstant(Log2_64(AndRHS->getValue()), |
| 2591 | TLI.getShiftAmountTy())); |
| 2592 | } |
| 2593 | } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) { |
| 2594 | // (X & 8) == 8 --> (X & 8) >> 3 |
| 2595 | // Perform the xform if C1 is a single bit. |
| 2596 | if ((C1 & (C1-1)) == 0) { |
| 2597 | return DAG.getNode(ISD::SRL, VT, N0, |
| 2598 | DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy())); |
| 2599 | } |
| 2600 | } |
| 2601 | } |
| 2602 | } |
| 2603 | } else if (isa<ConstantSDNode>(N0.Val)) { |
| 2604 | // Ensure that the constant occurs on the RHS. |
| 2605 | return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond)); |
| 2606 | } |
| 2607 | |
| 2608 | if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val)) |
| 2609 | if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) { |
| 2610 | double C0 = N0C->getValue(), C1 = N1C->getValue(); |
| 2611 | |
| 2612 | switch (Cond) { |
| 2613 | default: break; // FIXME: Implement the rest of these! |
| 2614 | case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT); |
| 2615 | case ISD::SETNE: return DAG.getConstant(C0 != C1, VT); |
| 2616 | case ISD::SETLT: return DAG.getConstant(C0 < C1, VT); |
| 2617 | case ISD::SETGT: return DAG.getConstant(C0 > C1, VT); |
| 2618 | case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT); |
| 2619 | case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT); |
| 2620 | } |
| 2621 | } else { |
| 2622 | // Ensure that the constant occurs on the RHS. |
| 2623 | return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond)); |
| 2624 | } |
| 2625 | |
| 2626 | if (N0 == N1) { |
| 2627 | // We can always fold X == Y for integer setcc's. |
| 2628 | if (MVT::isInteger(N0.getValueType())) |
| 2629 | return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); |
| 2630 | unsigned UOF = ISD::getUnorderedFlavor(Cond); |
| 2631 | if (UOF == 2) // FP operators that are undefined on NaNs. |
| 2632 | return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); |
| 2633 | if (UOF == unsigned(ISD::isTrueWhenEqual(Cond))) |
| 2634 | return DAG.getConstant(UOF, VT); |
| 2635 | // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO |
| 2636 | // if it is not already. |
| 2637 | ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO; |
| 2638 | if (NewCond != Cond) |
| 2639 | return DAG.getSetCC(VT, N0, N1, NewCond); |
| 2640 | } |
| 2641 | |
| 2642 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 2643 | MVT::isInteger(N0.getValueType())) { |
| 2644 | if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB || |
| 2645 | N0.getOpcode() == ISD::XOR) { |
| 2646 | // Simplify (X+Y) == (X+Z) --> Y == Z |
| 2647 | if (N0.getOpcode() == N1.getOpcode()) { |
| 2648 | if (N0.getOperand(0) == N1.getOperand(0)) |
| 2649 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond); |
| 2650 | if (N0.getOperand(1) == N1.getOperand(1)) |
| 2651 | return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond); |
| 2652 | if (isCommutativeBinOp(N0.getOpcode())) { |
| 2653 | // If X op Y == Y op X, try other combinations. |
| 2654 | if (N0.getOperand(0) == N1.getOperand(1)) |
| 2655 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond); |
| 2656 | if (N0.getOperand(1) == N1.getOperand(0)) |
Chris Lattner | a158eee | 2005-10-25 18:57:30 +0000 | [diff] [blame] | 2657 | return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 2658 | } |
| 2659 | } |
| 2660 | |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 2661 | // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes. |
| 2662 | if (N0.getOpcode() == ISD::XOR) |
| 2663 | if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
| 2664 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) { |
| 2665 | // If we know that all of the inverted bits are zero, don't bother |
| 2666 | // performing the inversion. |
| 2667 | if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI)) |
| 2668 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 2669 | DAG.getConstant(XORC->getValue()^RHSC->getValue(), |
| 2670 | N0.getValueType()), Cond); |
| 2671 | } |
| 2672 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 2673 | // Simplify (X+Z) == X --> Z == 0 |
| 2674 | if (N0.getOperand(0) == N1) |
| 2675 | return DAG.getSetCC(VT, N0.getOperand(1), |
| 2676 | DAG.getConstant(0, N0.getValueType()), Cond); |
| 2677 | if (N0.getOperand(1) == N1) { |
| 2678 | if (isCommutativeBinOp(N0.getOpcode())) |
| 2679 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 2680 | DAG.getConstant(0, N0.getValueType()), Cond); |
| 2681 | else { |
| 2682 | assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 2683 | // (Z-X) == X --> Z == X<<1 |
| 2684 | SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), |
| 2685 | N1, |
| 2686 | DAG.getConstant(1,TLI.getShiftAmountTy())); |
| 2687 | WorkList.push_back(SH.Val); |
| 2688 | return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond); |
| 2689 | } |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB || |
| 2694 | N1.getOpcode() == ISD::XOR) { |
| 2695 | // Simplify X == (X+Z) --> Z == 0 |
| 2696 | if (N1.getOperand(0) == N0) { |
| 2697 | return DAG.getSetCC(VT, N1.getOperand(1), |
| 2698 | DAG.getConstant(0, N1.getValueType()), Cond); |
| 2699 | } else if (N1.getOperand(1) == N0) { |
| 2700 | if (isCommutativeBinOp(N1.getOpcode())) { |
| 2701 | return DAG.getSetCC(VT, N1.getOperand(0), |
| 2702 | DAG.getConstant(0, N1.getValueType()), Cond); |
| 2703 | } else { |
| 2704 | assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 2705 | // X == (Z-X) --> X<<1 == Z |
| 2706 | SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0, |
| 2707 | DAG.getConstant(1,TLI.getShiftAmountTy())); |
| 2708 | WorkList.push_back(SH.Val); |
| 2709 | return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond); |
| 2710 | } |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | // Fold away ALL boolean setcc's. |
| 2716 | SDOperand Temp; |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 2717 | if (N0.getValueType() == MVT::i1 && foldBooleans) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 2718 | switch (Cond) { |
| 2719 | default: assert(0 && "Unknown integer setcc!"); |
| 2720 | case ISD::SETEQ: // X == Y -> (X^Y)^1 |
| 2721 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1); |
| 2722 | N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1)); |
| 2723 | WorkList.push_back(Temp.Val); |
| 2724 | break; |
| 2725 | case ISD::SETNE: // X != Y --> (X^Y) |
| 2726 | N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1); |
| 2727 | break; |
| 2728 | case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y |
| 2729 | case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y |
| 2730 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1)); |
| 2731 | N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp); |
| 2732 | WorkList.push_back(Temp.Val); |
| 2733 | break; |
| 2734 | case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X |
| 2735 | case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X |
| 2736 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1)); |
| 2737 | N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp); |
| 2738 | WorkList.push_back(Temp.Val); |
| 2739 | break; |
| 2740 | case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y |
| 2741 | case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y |
| 2742 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1)); |
| 2743 | N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp); |
| 2744 | WorkList.push_back(Temp.Val); |
| 2745 | break; |
| 2746 | case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X |
| 2747 | case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X |
| 2748 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1)); |
| 2749 | N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp); |
| 2750 | break; |
| 2751 | } |
| 2752 | if (VT != MVT::i1) { |
| 2753 | WorkList.push_back(N0.Val); |
| 2754 | // FIXME: If running after legalize, we probably can't do this. |
| 2755 | N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0); |
| 2756 | } |
| 2757 | return N0; |
| 2758 | } |
| 2759 | |
| 2760 | // Could not fold it. |
| 2761 | return SDOperand(); |
| 2762 | } |
| 2763 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 2764 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 2765 | /// return a DAG expression to select that will generate the same value by |
| 2766 | /// multiplying by a magic number. See: |
| 2767 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 2768 | SDOperand DAGCombiner::BuildSDIV(SDNode *N) { |
| 2769 | MVT::ValueType VT = N->getValueType(0); |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 2770 | |
| 2771 | // Check to see if we can do this. |
| 2772 | if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64)) |
| 2773 | return SDOperand(); // BuildSDIV only operates on i32 or i64 |
| 2774 | if (!TLI.isOperationLegal(ISD::MULHS, VT)) |
| 2775 | return SDOperand(); // Make sure the target supports MULHS. |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 2776 | |
Nate Begeman | c6a454e | 2005-10-20 17:45:03 +0000 | [diff] [blame] | 2777 | int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended(); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 2778 | ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d); |
| 2779 | |
| 2780 | // Multiply the numerator (operand 0) by the magic value |
| 2781 | SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0), |
| 2782 | DAG.getConstant(magics.m, VT)); |
| 2783 | // If d > 0 and m < 0, add the numerator |
| 2784 | if (d > 0 && magics.m < 0) { |
| 2785 | Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0)); |
| 2786 | WorkList.push_back(Q.Val); |
| 2787 | } |
| 2788 | // If d < 0 and m > 0, subtract the numerator. |
| 2789 | if (d < 0 && magics.m > 0) { |
| 2790 | Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0)); |
| 2791 | WorkList.push_back(Q.Val); |
| 2792 | } |
| 2793 | // Shift right algebraic if shift value is nonzero |
| 2794 | if (magics.s > 0) { |
| 2795 | Q = DAG.getNode(ISD::SRA, VT, Q, |
| 2796 | DAG.getConstant(magics.s, TLI.getShiftAmountTy())); |
| 2797 | WorkList.push_back(Q.Val); |
| 2798 | } |
| 2799 | // Extract the sign bit and add it to the quotient |
| 2800 | SDOperand T = |
Nate Begeman | 4d38567 | 2005-10-21 01:51:45 +0000 | [diff] [blame] | 2801 | DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1, |
| 2802 | TLI.getShiftAmountTy())); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 2803 | WorkList.push_back(T.Val); |
| 2804 | return DAG.getNode(ISD::ADD, VT, Q, T); |
| 2805 | } |
| 2806 | |
| 2807 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 2808 | /// return a DAG expression to select that will generate the same value by |
| 2809 | /// multiplying by a magic number. See: |
| 2810 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 2811 | SDOperand DAGCombiner::BuildUDIV(SDNode *N) { |
| 2812 | MVT::ValueType VT = N->getValueType(0); |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 2813 | |
| 2814 | // Check to see if we can do this. |
| 2815 | if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64)) |
| 2816 | return SDOperand(); // BuildUDIV only operates on i32 or i64 |
| 2817 | if (!TLI.isOperationLegal(ISD::MULHU, VT)) |
| 2818 | return SDOperand(); // Make sure the target supports MULHU. |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 2819 | |
| 2820 | uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue(); |
| 2821 | mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d); |
| 2822 | |
| 2823 | // Multiply the numerator (operand 0) by the magic value |
| 2824 | SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0), |
| 2825 | DAG.getConstant(magics.m, VT)); |
| 2826 | WorkList.push_back(Q.Val); |
| 2827 | |
| 2828 | if (magics.a == 0) { |
| 2829 | return DAG.getNode(ISD::SRL, VT, Q, |
| 2830 | DAG.getConstant(magics.s, TLI.getShiftAmountTy())); |
| 2831 | } else { |
| 2832 | SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q); |
| 2833 | WorkList.push_back(NPQ.Val); |
| 2834 | NPQ = DAG.getNode(ISD::SRL, VT, NPQ, |
| 2835 | DAG.getConstant(1, TLI.getShiftAmountTy())); |
| 2836 | WorkList.push_back(NPQ.Val); |
| 2837 | NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q); |
| 2838 | WorkList.push_back(NPQ.Val); |
| 2839 | return DAG.getNode(ISD::SRL, VT, NPQ, |
| 2840 | DAG.getConstant(magics.s-1, TLI.getShiftAmountTy())); |
| 2841 | } |
| 2842 | } |
| 2843 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2844 | // SelectionDAG::Combine - This is the entry point for the file. |
| 2845 | // |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 2846 | void SelectionDAG::Combine(bool RunningAfterLegalize) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2847 | /// run - This is the main entry point to this class. |
| 2848 | /// |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 2849 | DAGCombiner(*this).Run(RunningAfterLegalize); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2850 | } |