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