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