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