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