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 | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 25 | // FIXME: divide by zero is currently left unfolded. do we want to turn this |
| 26 | // into an undef? |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 27 | // 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] | 28 | // |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | #define DEBUG_TYPE "dagcombine" |
| 32 | #include "llvm/ADT/Statistic.h" |
| 33 | #include "llvm/CodeGen/SelectionDAG.h" |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MathExtras.h" |
| 36 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Compiler.h" |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a500fc6 | 2005-09-09 23:53:39 +0000 | [diff] [blame] | 39 | #include <algorithm> |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 40 | #include <cmath> |
Chris Lattner | 2c2c6c6 | 2006-01-22 23:41:00 +0000 | [diff] [blame] | 41 | #include <iostream> |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
| 44 | namespace { |
Andrew Lenharth | ae6153f | 2006-07-20 17:43:27 +0000 | [diff] [blame] | 45 | static Statistic<> NodesCombined ("dagcombiner", |
| 46 | "Number of dag nodes combined"); |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 47 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 48 | |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 49 | static cl::opt<bool> |
| 50 | CombinerAA("combiner-alias-analysis", cl::Hidden, |
| 51 | cl::desc("Turn on alias analysis turning testing")); |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 52 | |
| 53 | class VISIBILITY_HIDDEN DAGCombiner { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 54 | SelectionDAG &DAG; |
| 55 | TargetLowering &TLI; |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 56 | bool AfterLegalize; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 57 | |
| 58 | // Worklist of all of the nodes that need to be simplified. |
| 59 | std::vector<SDNode*> WorkList; |
| 60 | |
| 61 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 62 | /// the instruction to the work lists because they might get more simplified |
| 63 | /// now. |
| 64 | /// |
| 65 | void AddUsersToWorkList(SDNode *N) { |
| 66 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 67 | UI != UE; ++UI) |
| 68 | WorkList.push_back(*UI); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /// removeFromWorkList - remove all instances of N from the worklist. |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 72 | /// |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 73 | void removeFromWorkList(SDNode *N) { |
| 74 | WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N), |
| 75 | WorkList.end()); |
| 76 | } |
| 77 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 78 | public: |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 79 | void AddToWorkList(SDNode *N) { |
| 80 | WorkList.push_back(N); |
| 81 | } |
| 82 | |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 83 | SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo) { |
| 84 | assert(N->getNumValues() == NumTo && "Broken CombineTo call!"); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 85 | ++NodesCombined; |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 86 | DEBUG(std::cerr << "\nReplacing "; N->dump(); |
Evan Cheng | 60e8c71 | 2006-05-09 06:55:15 +0000 | [diff] [blame] | 87 | std::cerr << "\nWith: "; To[0].Val->dump(&DAG); |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 88 | std::cerr << " and " << NumTo-1 << " other values\n"); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 89 | std::vector<SDNode*> NowDead; |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 90 | DAG.ReplaceAllUsesWith(N, To, &NowDead); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 91 | |
| 92 | // Push the new nodes and any users onto the worklist |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 93 | for (unsigned i = 0, e = NumTo; i != e; ++i) { |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 94 | WorkList.push_back(To[i].Val); |
| 95 | AddUsersToWorkList(To[i].Val); |
| 96 | } |
| 97 | |
| 98 | // Nodes can end up on the worklist more than once. Make sure we do |
| 99 | // not process a node that has been replaced. |
| 100 | removeFromWorkList(N); |
| 101 | for (unsigned i = 0, e = NowDead.size(); i != e; ++i) |
| 102 | removeFromWorkList(NowDead[i]); |
| 103 | |
| 104 | // Finally, since the node is now dead, remove it from the graph. |
| 105 | DAG.DeleteNode(N); |
| 106 | return SDOperand(N, 0); |
| 107 | } |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 109 | SDOperand CombineTo(SDNode *N, SDOperand Res) { |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 110 | return CombineTo(N, &Res, 1); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) { |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 114 | SDOperand To[] = { Res0, Res1 }; |
| 115 | return CombineTo(N, To, 2); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 116 | } |
| 117 | private: |
| 118 | |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 119 | /// SimplifyDemandedBits - Check the specified integer node value to see if |
Chris Lattner | b2742f4 | 2006-03-01 19:55:35 +0000 | [diff] [blame] | 120 | /// 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] | 121 | /// propagation. If so, return true. |
| 122 | bool SimplifyDemandedBits(SDOperand Op) { |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 123 | TargetLowering::TargetLoweringOpt TLO(DAG); |
| 124 | uint64_t KnownZero, KnownOne; |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 125 | uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType()); |
| 126 | if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) |
| 127 | return false; |
| 128 | |
| 129 | // Revisit the node. |
| 130 | WorkList.push_back(Op.Val); |
| 131 | |
| 132 | // Replace the old value with the new one. |
| 133 | ++NodesCombined; |
| 134 | DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump(); |
Evan Cheng | 60e8c71 | 2006-05-09 06:55:15 +0000 | [diff] [blame] | 135 | std::cerr << "\nWith: "; TLO.New.Val->dump(&DAG)); |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 136 | |
| 137 | std::vector<SDNode*> NowDead; |
| 138 | DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead); |
| 139 | |
Chris Lattner | 7d20d39 | 2006-02-20 06:51:04 +0000 | [diff] [blame] | 140 | // Push the new node and any (possibly new) users onto the worklist. |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 141 | WorkList.push_back(TLO.New.Val); |
| 142 | AddUsersToWorkList(TLO.New.Val); |
| 143 | |
| 144 | // Nodes can end up on the worklist more than once. Make sure we do |
| 145 | // not process a node that has been replaced. |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 146 | for (unsigned i = 0, e = NowDead.size(); i != e; ++i) |
| 147 | removeFromWorkList(NowDead[i]); |
| 148 | |
Chris Lattner | 7d20d39 | 2006-02-20 06:51:04 +0000 | [diff] [blame] | 149 | // Finally, if the node is now dead, remove it from the graph. The node |
| 150 | // may not be dead if the replacement process recursively simplified to |
| 151 | // something else needing this node. |
| 152 | if (TLO.Old.Val->use_empty()) { |
| 153 | removeFromWorkList(TLO.Old.Val); |
| 154 | DAG.DeleteNode(TLO.Old.Val); |
| 155 | } |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 156 | return true; |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 157 | } |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 158 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 159 | /// visit - call the node-specific routine that knows how to fold each |
| 160 | /// particular type of node. |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 161 | SDOperand visit(SDNode *N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 162 | |
| 163 | // Visitation implementation - Implement dag node combining for different |
| 164 | // node types. The semantics are as follows: |
| 165 | // Return Value: |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 166 | // SDOperand.Val == 0 - No change was made |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 167 | // SDOperand.Val == N - N was replaced, is dead, and is already handled. |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 168 | // otherwise - N should be replaced by the returned Operand. |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 169 | // |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 170 | SDOperand visitTokenFactor(SDNode *N); |
| 171 | SDOperand visitADD(SDNode *N); |
| 172 | SDOperand visitSUB(SDNode *N); |
| 173 | SDOperand visitMUL(SDNode *N); |
| 174 | SDOperand visitSDIV(SDNode *N); |
| 175 | SDOperand visitUDIV(SDNode *N); |
| 176 | SDOperand visitSREM(SDNode *N); |
| 177 | SDOperand visitUREM(SDNode *N); |
| 178 | SDOperand visitMULHU(SDNode *N); |
| 179 | SDOperand visitMULHS(SDNode *N); |
| 180 | SDOperand visitAND(SDNode *N); |
| 181 | SDOperand visitOR(SDNode *N); |
| 182 | SDOperand visitXOR(SDNode *N); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 183 | SDOperand visitVBinOp(SDNode *N, ISD::NodeType IntOp, ISD::NodeType FPOp); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 184 | SDOperand visitSHL(SDNode *N); |
| 185 | SDOperand visitSRA(SDNode *N); |
| 186 | SDOperand visitSRL(SDNode *N); |
| 187 | SDOperand visitCTLZ(SDNode *N); |
| 188 | SDOperand visitCTTZ(SDNode *N); |
| 189 | SDOperand visitCTPOP(SDNode *N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 190 | SDOperand visitSELECT(SDNode *N); |
| 191 | SDOperand visitSELECT_CC(SDNode *N); |
| 192 | SDOperand visitSETCC(SDNode *N); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 193 | SDOperand visitSIGN_EXTEND(SDNode *N); |
| 194 | SDOperand visitZERO_EXTEND(SDNode *N); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 195 | SDOperand visitANY_EXTEND(SDNode *N); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 196 | SDOperand visitSIGN_EXTEND_INREG(SDNode *N); |
| 197 | SDOperand visitTRUNCATE(SDNode *N); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 198 | SDOperand visitBIT_CONVERT(SDNode *N); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 199 | SDOperand visitVBIT_CONVERT(SDNode *N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 200 | SDOperand visitFADD(SDNode *N); |
| 201 | SDOperand visitFSUB(SDNode *N); |
| 202 | SDOperand visitFMUL(SDNode *N); |
| 203 | SDOperand visitFDIV(SDNode *N); |
| 204 | SDOperand visitFREM(SDNode *N); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 205 | SDOperand visitFCOPYSIGN(SDNode *N); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 206 | SDOperand visitSINT_TO_FP(SDNode *N); |
| 207 | SDOperand visitUINT_TO_FP(SDNode *N); |
| 208 | SDOperand visitFP_TO_SINT(SDNode *N); |
| 209 | SDOperand visitFP_TO_UINT(SDNode *N); |
| 210 | SDOperand visitFP_ROUND(SDNode *N); |
| 211 | SDOperand visitFP_ROUND_INREG(SDNode *N); |
| 212 | SDOperand visitFP_EXTEND(SDNode *N); |
| 213 | SDOperand visitFNEG(SDNode *N); |
| 214 | SDOperand visitFABS(SDNode *N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 215 | SDOperand visitBRCOND(SDNode *N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 216 | SDOperand visitBR_CC(SDNode *N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 217 | SDOperand visitLOAD(SDNode *N); |
Chris Lattner | 29cd7db | 2006-03-31 18:10:41 +0000 | [diff] [blame] | 218 | SDOperand visitXEXTLOAD(SDNode *N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 219 | SDOperand visitSTORE(SDNode *N); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 220 | SDOperand visitINSERT_VECTOR_ELT(SDNode *N); |
| 221 | SDOperand visitVINSERT_VECTOR_ELT(SDNode *N); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 222 | SDOperand visitVBUILD_VECTOR(SDNode *N); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 223 | SDOperand visitVECTOR_SHUFFLE(SDNode *N); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 224 | SDOperand visitVVECTOR_SHUFFLE(SDNode *N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 225 | |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 226 | SDOperand XformToShuffleWithZero(SDNode *N); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 227 | SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS); |
| 228 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 229 | bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 230 | SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 231 | SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2); |
| 232 | SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2, |
| 233 | SDOperand N3, ISD::CondCode CC); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 234 | SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1, |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 235 | ISD::CondCode Cond, bool foldBooleans = true); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 236 | SDOperand ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *, MVT::ValueType); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 237 | SDOperand BuildSDIV(SDNode *N); |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 238 | SDOperand BuildUDIV(SDNode *N); |
| 239 | SDNode *MatchRotate(SDOperand LHS, SDOperand RHS); |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 240 | bool isNotAlias(SDOperand Ptr1, SDOperand Ptr2); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 241 | public: |
| 242 | DAGCombiner(SelectionDAG &D) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 243 | : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {} |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 244 | |
| 245 | /// Run - runs the dag combiner on all nodes in the work list |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 246 | void Run(bool RunningAfterLegalize); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 247 | }; |
| 248 | } |
| 249 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 250 | //===----------------------------------------------------------------------===// |
| 251 | // TargetLowering::DAGCombinerInfo implementation |
| 252 | //===----------------------------------------------------------------------===// |
| 253 | |
| 254 | void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) { |
| 255 | ((DAGCombiner*)DC)->AddToWorkList(N); |
| 256 | } |
| 257 | |
| 258 | SDOperand TargetLowering::DAGCombinerInfo:: |
| 259 | CombineTo(SDNode *N, const std::vector<SDOperand> &To) { |
Chris Lattner | 3577e38 | 2006-08-11 17:56:38 +0000 | [diff] [blame] | 260 | return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size()); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | SDOperand TargetLowering::DAGCombinerInfo:: |
| 264 | CombineTo(SDNode *N, SDOperand Res) { |
| 265 | return ((DAGCombiner*)DC)->CombineTo(N, Res); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | SDOperand TargetLowering::DAGCombinerInfo:: |
| 270 | CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) { |
| 271 | return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | //===----------------------------------------------------------------------===// |
| 278 | |
| 279 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 280 | // isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc |
| 281 | // 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] | 282 | // Also, set the incoming LHS, RHS, and CC references to the appropriate |
| 283 | // nodes based on the type of node we are checking. This simplifies life a |
| 284 | // bit for the callers. |
| 285 | static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS, |
| 286 | SDOperand &CC) { |
| 287 | if (N.getOpcode() == ISD::SETCC) { |
| 288 | LHS = N.getOperand(0); |
| 289 | RHS = N.getOperand(1); |
| 290 | CC = N.getOperand(2); |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 291 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 292 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 293 | if (N.getOpcode() == ISD::SELECT_CC && |
| 294 | N.getOperand(2).getOpcode() == ISD::Constant && |
| 295 | N.getOperand(3).getOpcode() == ISD::Constant && |
| 296 | cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 && |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 297 | cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) { |
| 298 | LHS = N.getOperand(0); |
| 299 | RHS = N.getOperand(1); |
| 300 | CC = N.getOperand(4); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 301 | return true; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 302 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 306 | // isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only |
| 307 | // one use. If this is true, it allows the users to invert the operation for |
| 308 | // free when it is profitable to do so. |
| 309 | static bool isOneUseSetCC(SDOperand N) { |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 310 | SDOperand N0, N1, N2; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 311 | if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse()) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 312 | return true; |
| 313 | return false; |
| 314 | } |
| 315 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 316 | SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){ |
| 317 | MVT::ValueType VT = N0.getValueType(); |
| 318 | // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use |
| 319 | // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2)) |
| 320 | if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) { |
| 321 | if (isa<ConstantSDNode>(N1)) { |
| 322 | SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 323 | AddToWorkList(OpNode.Val); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 324 | return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0)); |
| 325 | } else if (N0.hasOneUse()) { |
| 326 | SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 327 | AddToWorkList(OpNode.Val); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 328 | return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1)); |
| 329 | } |
| 330 | } |
| 331 | // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use |
| 332 | // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2)) |
| 333 | if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) { |
| 334 | if (isa<ConstantSDNode>(N0)) { |
| 335 | SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 336 | AddToWorkList(OpNode.Val); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 337 | return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0)); |
| 338 | } else if (N1.hasOneUse()) { |
| 339 | SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 340 | AddToWorkList(OpNode.Val); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 341 | return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1)); |
| 342 | } |
| 343 | } |
| 344 | return SDOperand(); |
| 345 | } |
| 346 | |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 347 | void DAGCombiner::Run(bool RunningAfterLegalize) { |
| 348 | // set the instance variable, so that the various visit routines may use it. |
| 349 | AfterLegalize = RunningAfterLegalize; |
| 350 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 351 | // Add all the dag nodes to the worklist. |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 352 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 353 | E = DAG.allnodes_end(); I != E; ++I) |
| 354 | WorkList.push_back(I); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 356 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 357 | // to the root node, preventing it from being deleted, and tracking any |
| 358 | // changes of the root. |
| 359 | HandleSDNode Dummy(DAG.getRoot()); |
| 360 | |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 361 | |
| 362 | /// DagCombineInfo - Expose the DAG combiner to the target combiner impls. |
| 363 | TargetLowering::DAGCombinerInfo |
| 364 | DagCombineInfo(DAG, !RunningAfterLegalize, this); |
| 365 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 366 | // while the worklist isn't empty, inspect the node on the end of it and |
| 367 | // try and combine it. |
| 368 | while (!WorkList.empty()) { |
| 369 | SDNode *N = WorkList.back(); |
| 370 | WorkList.pop_back(); |
| 371 | |
| 372 | // 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] | 373 | // N is deleted from the DAG, since they too may now be dead or may have a |
| 374 | // reduced number of uses, allowing other xforms. |
| 375 | if (N->use_empty() && N != &Dummy) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 376 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 377 | WorkList.push_back(N->getOperand(i).Val); |
| 378 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 379 | removeFromWorkList(N); |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 380 | DAG.DeleteNode(N); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 381 | continue; |
| 382 | } |
| 383 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 384 | SDOperand RV = visit(N); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 385 | |
| 386 | // If nothing happened, try a target-specific DAG combine. |
| 387 | if (RV.Val == 0) { |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 388 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 389 | "Node was deleted but visit returned NULL!"); |
Chris Lattner | 2466472 | 2006-03-01 04:53:38 +0000 | [diff] [blame] | 390 | if (N->getOpcode() >= ISD::BUILTIN_OP_END || |
| 391 | TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) |
| 392 | RV = TLI.PerformDAGCombine(N, DagCombineInfo); |
| 393 | } |
| 394 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 395 | if (RV.Val) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 396 | ++NodesCombined; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 397 | // If we get back the same node we passed in, rather than a new node or |
| 398 | // zero, we know that the node must have defined multiple values and |
| 399 | // CombineTo was used. Since CombineTo takes care of the worklist |
| 400 | // mechanics for us, we have no work to do in this case. |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 401 | if (RV.Val != N) { |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 402 | assert(N->getOpcode() != ISD::DELETED_NODE && |
| 403 | RV.Val->getOpcode() != ISD::DELETED_NODE && |
| 404 | "Node was deleted but visit returned new node!"); |
| 405 | |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 406 | DEBUG(std::cerr << "\nReplacing "; N->dump(); |
Evan Cheng | 60e8c71 | 2006-05-09 06:55:15 +0000 | [diff] [blame] | 407 | std::cerr << "\nWith: "; RV.Val->dump(&DAG); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 408 | std::cerr << '\n'); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 409 | std::vector<SDNode*> NowDead; |
Chris Lattner | b9ea4a3 | 2006-08-11 17:46:28 +0000 | [diff] [blame] | 410 | SDOperand OpV = RV; |
| 411 | DAG.ReplaceAllUsesWith(N, &OpV, &NowDead); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 412 | |
| 413 | // Push the new node and any users onto the worklist |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 414 | WorkList.push_back(RV.Val); |
| 415 | AddUsersToWorkList(RV.Val); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 416 | |
| 417 | // Nodes can end up on the worklist more than once. Make sure we do |
| 418 | // not process a node that has been replaced. |
| 419 | removeFromWorkList(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 420 | for (unsigned i = 0, e = NowDead.size(); i != e; ++i) |
| 421 | removeFromWorkList(NowDead[i]); |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 422 | |
| 423 | // Finally, since the node is now dead, remove it from the graph. |
| 424 | DAG.DeleteNode(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 425 | } |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 428 | |
| 429 | // If the root changed (e.g. it was a dead load, update the root). |
| 430 | DAG.setRoot(Dummy.getValue()); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 433 | SDOperand DAGCombiner::visit(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 434 | switch(N->getOpcode()) { |
| 435 | default: break; |
Nate Begeman | 4942a96 | 2005-09-01 00:33:32 +0000 | [diff] [blame] | 436 | case ISD::TokenFactor: return visitTokenFactor(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 437 | case ISD::ADD: return visitADD(N); |
| 438 | case ISD::SUB: return visitSUB(N); |
| 439 | case ISD::MUL: return visitMUL(N); |
| 440 | case ISD::SDIV: return visitSDIV(N); |
| 441 | case ISD::UDIV: return visitUDIV(N); |
| 442 | case ISD::SREM: return visitSREM(N); |
| 443 | case ISD::UREM: return visitUREM(N); |
| 444 | case ISD::MULHU: return visitMULHU(N); |
| 445 | case ISD::MULHS: return visitMULHS(N); |
| 446 | case ISD::AND: return visitAND(N); |
| 447 | case ISD::OR: return visitOR(N); |
| 448 | case ISD::XOR: return visitXOR(N); |
| 449 | case ISD::SHL: return visitSHL(N); |
| 450 | case ISD::SRA: return visitSRA(N); |
| 451 | case ISD::SRL: return visitSRL(N); |
| 452 | case ISD::CTLZ: return visitCTLZ(N); |
| 453 | case ISD::CTTZ: return visitCTTZ(N); |
| 454 | case ISD::CTPOP: return visitCTPOP(N); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 455 | case ISD::SELECT: return visitSELECT(N); |
| 456 | case ISD::SELECT_CC: return visitSELECT_CC(N); |
| 457 | case ISD::SETCC: return visitSETCC(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 458 | case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N); |
| 459 | case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 460 | case ISD::ANY_EXTEND: return visitANY_EXTEND(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 461 | case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N); |
| 462 | case ISD::TRUNCATE: return visitTRUNCATE(N); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 463 | case ISD::BIT_CONVERT: return visitBIT_CONVERT(N); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 464 | case ISD::VBIT_CONVERT: return visitVBIT_CONVERT(N); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 465 | case ISD::FADD: return visitFADD(N); |
| 466 | case ISD::FSUB: return visitFSUB(N); |
| 467 | case ISD::FMUL: return visitFMUL(N); |
| 468 | case ISD::FDIV: return visitFDIV(N); |
| 469 | case ISD::FREM: return visitFREM(N); |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 470 | case ISD::FCOPYSIGN: return visitFCOPYSIGN(N); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 471 | case ISD::SINT_TO_FP: return visitSINT_TO_FP(N); |
| 472 | case ISD::UINT_TO_FP: return visitUINT_TO_FP(N); |
| 473 | case ISD::FP_TO_SINT: return visitFP_TO_SINT(N); |
| 474 | case ISD::FP_TO_UINT: return visitFP_TO_UINT(N); |
| 475 | case ISD::FP_ROUND: return visitFP_ROUND(N); |
| 476 | case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N); |
| 477 | case ISD::FP_EXTEND: return visitFP_EXTEND(N); |
| 478 | case ISD::FNEG: return visitFNEG(N); |
| 479 | case ISD::FABS: return visitFABS(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 480 | case ISD::BRCOND: return visitBRCOND(N); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 481 | case ISD::BR_CC: return visitBR_CC(N); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 482 | case ISD::LOAD: return visitLOAD(N); |
Chris Lattner | 29cd7db | 2006-03-31 18:10:41 +0000 | [diff] [blame] | 483 | case ISD::EXTLOAD: |
| 484 | case ISD::SEXTLOAD: |
| 485 | case ISD::ZEXTLOAD: return visitXEXTLOAD(N); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 486 | case ISD::STORE: return visitSTORE(N); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 487 | case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N); |
| 488 | case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 489 | case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 490 | case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N); |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 491 | case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 492 | case ISD::VADD: return visitVBinOp(N, ISD::ADD , ISD::FADD); |
| 493 | case ISD::VSUB: return visitVBinOp(N, ISD::SUB , ISD::FSUB); |
| 494 | case ISD::VMUL: return visitVBinOp(N, ISD::MUL , ISD::FMUL); |
| 495 | case ISD::VSDIV: return visitVBinOp(N, ISD::SDIV, ISD::FDIV); |
| 496 | case ISD::VUDIV: return visitVBinOp(N, ISD::UDIV, ISD::UDIV); |
| 497 | case ISD::VAND: return visitVBinOp(N, ISD::AND , ISD::AND); |
| 498 | case ISD::VOR: return visitVBinOp(N, ISD::OR , ISD::OR); |
| 499 | case ISD::VXOR: return visitVBinOp(N, ISD::XOR , ISD::XOR); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 500 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 501 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 504 | SDOperand DAGCombiner::visitTokenFactor(SDNode *N) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 505 | SmallVector<SDOperand, 8> Ops; |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 506 | bool Changed = false; |
| 507 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 508 | // If the token factor has two operands and one is the entry token, replace |
| 509 | // the token factor with the other operand. |
| 510 | if (N->getNumOperands() == 2) { |
Chris Lattner | 21a57dc | 2006-05-12 05:01:37 +0000 | [diff] [blame] | 511 | if (N->getOperand(0).getOpcode() == ISD::EntryToken || |
| 512 | N->getOperand(0) == N->getOperand(1)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 513 | return N->getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 514 | if (N->getOperand(1).getOpcode() == ISD::EntryToken) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 515 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 516 | } |
Chris Lattner | 24edbb7 | 2005-10-13 22:10:05 +0000 | [diff] [blame] | 517 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 518 | // fold (tokenfactor (tokenfactor)) -> tokenfactor |
| 519 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 520 | SDOperand Op = N->getOperand(i); |
| 521 | if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) { |
Chris Lattner | ac0f8f2 | 2006-03-13 18:37:30 +0000 | [diff] [blame] | 522 | AddToWorkList(Op.Val); // Remove dead node. |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 523 | Changed = true; |
| 524 | for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j) |
| 525 | Ops.push_back(Op.getOperand(j)); |
Chris Lattner | 21a57dc | 2006-05-12 05:01:37 +0000 | [diff] [blame] | 526 | } else if (i == 0 || N->getOperand(i) != N->getOperand(i-1)) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 527 | Ops.push_back(Op); |
Chris Lattner | 21a57dc | 2006-05-12 05:01:37 +0000 | [diff] [blame] | 528 | } else { |
| 529 | // Deleted an operand that was the same as the last one. |
| 530 | Changed = true; |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | if (Changed) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 534 | return DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size()); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 535 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 538 | SDOperand DAGCombiner::visitADD(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 539 | SDOperand N0 = N->getOperand(0); |
| 540 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 541 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 542 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 543 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 544 | |
| 545 | // fold (add c1, c2) -> c1+c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 546 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 547 | return DAG.getNode(ISD::ADD, VT, N0, N1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 548 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 549 | if (N0C && !N1C) |
| 550 | return DAG.getNode(ISD::ADD, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 551 | // fold (add x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 552 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 553 | return N0; |
Chris Lattner | 4aafb4f | 2006-01-12 20:22:43 +0000 | [diff] [blame] | 554 | // fold ((c1-A)+c2) -> (c1+c2)-A |
| 555 | if (N1C && N0.getOpcode() == ISD::SUB) |
| 556 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0))) |
| 557 | return DAG.getNode(ISD::SUB, VT, |
| 558 | DAG.getConstant(N1C->getValue()+N0C->getValue(), VT), |
| 559 | N0.getOperand(1)); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 560 | // reassociate add |
| 561 | SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1); |
| 562 | if (RADD.Val != 0) |
| 563 | return RADD; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 564 | // fold ((0-A) + B) -> B-A |
| 565 | if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) && |
| 566 | cast<ConstantSDNode>(N0.getOperand(0))->isNullValue()) |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 567 | return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 568 | // fold (A + (0-B)) -> A-B |
| 569 | if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) && |
| 570 | cast<ConstantSDNode>(N1.getOperand(0))->isNullValue()) |
Nate Begeman | f89d78d | 2005-09-07 16:09:19 +0000 | [diff] [blame] | 571 | return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 572 | // fold (A+(B-A)) -> B |
| 573 | if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 574 | return N1.getOperand(0); |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 575 | |
Evan Cheng | 860771d | 2006-03-01 01:09:54 +0000 | [diff] [blame] | 576 | if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0))) |
Chris Lattner | ef027f9 | 2006-04-21 15:32:26 +0000 | [diff] [blame] | 577 | return SDOperand(N, 0); |
Chris Lattner | 947c289 | 2006-03-13 06:51:27 +0000 | [diff] [blame] | 578 | |
| 579 | // fold (a+b) -> (a|b) iff a and b share no bits. |
| 580 | if (MVT::isInteger(VT) && !MVT::isVector(VT)) { |
| 581 | uint64_t LHSZero, LHSOne; |
| 582 | uint64_t RHSZero, RHSOne; |
| 583 | uint64_t Mask = MVT::getIntVTBitMask(VT); |
| 584 | TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne); |
| 585 | if (LHSZero) { |
| 586 | TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne); |
| 587 | |
| 588 | // If all possibly-set bits on the LHS are clear on the RHS, return an OR. |
| 589 | // If all possibly-set bits on the RHS are clear on the LHS, return an OR. |
| 590 | if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) || |
| 591 | (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask)) |
| 592 | return DAG.getNode(ISD::OR, VT, N0, N1); |
| 593 | } |
| 594 | } |
| 595 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 596 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 599 | SDOperand DAGCombiner::visitSUB(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 600 | SDOperand N0 = N->getOperand(0); |
| 601 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 602 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 603 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 604 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 854077d | 2005-10-17 01:07:11 +0000 | [diff] [blame] | 606 | // fold (sub x, x) -> 0 |
| 607 | if (N0 == N1) |
| 608 | return DAG.getConstant(0, N->getValueType(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 609 | // fold (sub c1, c2) -> c1-c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 610 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 611 | return DAG.getNode(ISD::SUB, VT, N0, N1); |
Chris Lattner | 05b5743 | 2005-10-11 06:07:15 +0000 | [diff] [blame] | 612 | // fold (sub x, c) -> (add x, -c) |
| 613 | if (N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 614 | return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 615 | // fold (A+B)-A -> B |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 616 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 617 | return N0.getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 618 | // fold (A+B)-B -> A |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 619 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 620 | return N0.getOperand(0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 621 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 624 | SDOperand DAGCombiner::visitMUL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 625 | SDOperand N0 = N->getOperand(0); |
| 626 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 627 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 628 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 629 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 630 | |
| 631 | // fold (mul c1, c2) -> c1*c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 632 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 633 | return DAG.getNode(ISD::MUL, VT, N0, N1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 634 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 635 | if (N0C && !N1C) |
| 636 | return DAG.getNode(ISD::MUL, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 637 | // fold (mul x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 638 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 639 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 640 | // fold (mul x, -1) -> 0-x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 641 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 642 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 643 | // fold (mul x, (1 << c)) -> x << c |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 644 | if (N1C && isPowerOf2_64(N1C->getValue())) |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 645 | return DAG.getNode(ISD::SHL, VT, N0, |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 646 | DAG.getConstant(Log2_64(N1C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 647 | TLI.getShiftAmountTy())); |
Chris Lattner | 3e6099b | 2005-10-30 06:41:49 +0000 | [diff] [blame] | 648 | // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c |
| 649 | if (N1C && isPowerOf2_64(-N1C->getSignExtended())) { |
| 650 | // FIXME: If the input is something that is easily negated (e.g. a |
| 651 | // single-use add), we should put the negate there. |
| 652 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), |
| 653 | DAG.getNode(ISD::SHL, VT, N0, |
| 654 | DAG.getConstant(Log2_64(-N1C->getSignExtended()), |
| 655 | TLI.getShiftAmountTy()))); |
| 656 | } |
Andrew Lenharth | 50a0d42 | 2006-04-02 21:42:45 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 658 | // (mul (shl X, c1), c2) -> (mul X, c2 << c1) |
| 659 | if (N1C && N0.getOpcode() == ISD::SHL && |
| 660 | isa<ConstantSDNode>(N0.getOperand(1))) { |
| 661 | SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 662 | AddToWorkList(C3.Val); |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 663 | return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3); |
| 664 | } |
| 665 | |
| 666 | // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one |
| 667 | // use. |
| 668 | { |
| 669 | SDOperand Sh(0,0), Y(0,0); |
| 670 | // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)). |
| 671 | if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) && |
| 672 | N0.Val->hasOneUse()) { |
| 673 | Sh = N0; Y = N1; |
| 674 | } else if (N1.getOpcode() == ISD::SHL && |
| 675 | isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) { |
| 676 | Sh = N1; Y = N0; |
| 677 | } |
| 678 | if (Sh.Val) { |
| 679 | SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y); |
| 680 | return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1)); |
| 681 | } |
| 682 | } |
Chris Lattner | a1deca3 | 2006-03-04 23:33:26 +0000 | [diff] [blame] | 683 | // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2) |
| 684 | if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() && |
| 685 | isa<ConstantSDNode>(N0.getOperand(1))) { |
| 686 | return DAG.getNode(ISD::ADD, VT, |
| 687 | DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1), |
| 688 | DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1)); |
| 689 | } |
Chris Lattner | 0b1a85f | 2006-03-01 03:44:24 +0000 | [diff] [blame] | 690 | |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 691 | // reassociate mul |
| 692 | SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1); |
| 693 | if (RMUL.Val != 0) |
| 694 | return RMUL; |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 695 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 698 | SDOperand DAGCombiner::visitSDIV(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 699 | SDOperand N0 = N->getOperand(0); |
| 700 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 701 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 702 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 703 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 704 | |
| 705 | // fold (sdiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 706 | if (N0C && N1C && !N1C->isNullValue()) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 707 | return DAG.getNode(ISD::SDIV, VT, N0, N1); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 708 | // fold (sdiv X, 1) -> X |
| 709 | if (N1C && N1C->getSignExtended() == 1LL) |
| 710 | return N0; |
| 711 | // fold (sdiv X, -1) -> 0-X |
| 712 | if (N1C && N1C->isAllOnesValue()) |
| 713 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0); |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 714 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 715 | // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2 |
| 716 | uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1); |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 717 | if (TLI.MaskedValueIsZero(N1, SignBit) && |
| 718 | TLI.MaskedValueIsZero(N0, SignBit)) |
Chris Lattner | 094c8fc | 2005-10-07 06:10:46 +0000 | [diff] [blame] | 719 | return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1); |
Nate Begeman | cd6a6ed | 2006-02-17 07:26:20 +0000 | [diff] [blame] | 720 | // fold (sdiv X, pow2) -> simple ops after legalize |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 721 | if (N1C && N1C->getValue() && !TLI.isIntDivCheap() && |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 722 | (isPowerOf2_64(N1C->getSignExtended()) || |
| 723 | isPowerOf2_64(-N1C->getSignExtended()))) { |
| 724 | // If dividing by powers of two is cheap, then don't perform the following |
| 725 | // fold. |
| 726 | if (TLI.isPow2DivCheap()) |
| 727 | return SDOperand(); |
| 728 | int64_t pow2 = N1C->getSignExtended(); |
| 729 | int64_t abs2 = pow2 > 0 ? pow2 : -pow2; |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 730 | unsigned lg2 = Log2_64(abs2); |
| 731 | // Splat the sign bit into the register |
| 732 | SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0, |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 733 | DAG.getConstant(MVT::getSizeInBits(VT)-1, |
| 734 | TLI.getShiftAmountTy())); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 735 | AddToWorkList(SGN.Val); |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 736 | // Add (N0 < 0) ? abs2 - 1 : 0; |
| 737 | SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN, |
| 738 | DAG.getConstant(MVT::getSizeInBits(VT)-lg2, |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 739 | TLI.getShiftAmountTy())); |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 740 | SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 741 | AddToWorkList(SRL.Val); |
| 742 | AddToWorkList(ADD.Val); // Divide by pow2 |
Chris Lattner | 8f4880b | 2006-02-16 08:02:36 +0000 | [diff] [blame] | 743 | SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD, |
| 744 | DAG.getConstant(lg2, TLI.getShiftAmountTy())); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 745 | // If we're dividing by a positive value, we're done. Otherwise, we must |
| 746 | // negate the result. |
| 747 | if (pow2 > 0) |
| 748 | return SRA; |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 749 | AddToWorkList(SRA.Val); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 750 | return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA); |
| 751 | } |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 752 | // if integer divide is expensive and we satisfy the requirements, emit an |
| 753 | // alternate sequence. |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 754 | if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) && |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 755 | !TLI.isIntDivCheap()) { |
| 756 | SDOperand Op = BuildSDIV(N); |
| 757 | if (Op.Val) return Op; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 758 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 759 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 762 | SDOperand DAGCombiner::visitUDIV(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 763 | SDOperand N0 = N->getOperand(0); |
| 764 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 765 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val); |
| 766 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 767 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 768 | |
| 769 | // fold (udiv c1, c2) -> c1/c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 770 | if (N0C && N1C && !N1C->isNullValue()) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 771 | return DAG.getNode(ISD::UDIV, VT, N0, N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 772 | // fold (udiv x, (1 << c)) -> x >>u c |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 773 | if (N1C && isPowerOf2_64(N1C->getValue())) |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 774 | return DAG.getNode(ISD::SRL, VT, N0, |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 775 | DAG.getConstant(Log2_64(N1C->getValue()), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 776 | TLI.getShiftAmountTy())); |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 777 | // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2 |
| 778 | if (N1.getOpcode() == ISD::SHL) { |
| 779 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
| 780 | if (isPowerOf2_64(SHC->getValue())) { |
| 781 | MVT::ValueType ADDVT = N1.getOperand(1).getValueType(); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 782 | SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1), |
| 783 | DAG.getConstant(Log2_64(SHC->getValue()), |
| 784 | ADDVT)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 785 | AddToWorkList(Add.Val); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 786 | return DAG.getNode(ISD::SRL, VT, N0, Add); |
Nate Begeman | fb5e4bd | 2006-02-05 07:20:23 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | } |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 790 | // fold (udiv x, c) -> alternate |
Chris Lattner | e9936d1 | 2005-10-22 18:50:15 +0000 | [diff] [blame] | 791 | if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) { |
| 792 | SDOperand Op = BuildUDIV(N); |
| 793 | if (Op.Val) return Op; |
| 794 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 795 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 798 | SDOperand DAGCombiner::visitSREM(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 799 | SDOperand N0 = N->getOperand(0); |
| 800 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 801 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 802 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 803 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 804 | |
| 805 | // fold (srem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 806 | if (N0C && N1C && !N1C->isNullValue()) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 807 | return DAG.getNode(ISD::SREM, VT, N0, N1); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 808 | // If we know the sign bits of both operands are zero, strength reduce to a |
| 809 | // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15 |
| 810 | uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1); |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 811 | if (TLI.MaskedValueIsZero(N1, SignBit) && |
| 812 | TLI.MaskedValueIsZero(N0, SignBit)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 813 | return DAG.getNode(ISD::UREM, VT, N0, N1); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 814 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 817 | SDOperand DAGCombiner::visitUREM(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 818 | SDOperand N0 = N->getOperand(0); |
| 819 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 820 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 821 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 822 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 823 | |
| 824 | // fold (urem c1, c2) -> c1%c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 825 | if (N0C && N1C && !N1C->isNullValue()) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 826 | return DAG.getNode(ISD::UREM, VT, N0, N1); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 827 | // fold (urem x, pow2) -> (and x, pow2-1) |
| 828 | if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue())) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 829 | 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] | 830 | // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1)) |
| 831 | if (N1.getOpcode() == ISD::SHL) { |
| 832 | if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) { |
| 833 | if (isPowerOf2_64(SHC->getValue())) { |
Nate Begeman | bab9239 | 2006-02-05 08:07:24 +0000 | [diff] [blame] | 834 | SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 835 | AddToWorkList(Add.Val); |
Nate Begeman | c031e33 | 2006-02-05 07:36:48 +0000 | [diff] [blame] | 836 | return DAG.getNode(ISD::AND, VT, N0, Add); |
| 837 | } |
| 838 | } |
| 839 | } |
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::visitMULHS(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 *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 847 | |
| 848 | // fold (mulhs x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 849 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 850 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 851 | // fold (mulhs x, 1) -> (sra x, size(x)-1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 852 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 853 | return DAG.getNode(ISD::SRA, N0.getValueType(), N0, |
| 854 | DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1, |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 855 | TLI.getShiftAmountTy())); |
| 856 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 859 | SDOperand DAGCombiner::visitMULHU(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 860 | SDOperand N0 = N->getOperand(0); |
| 861 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 862 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 863 | |
| 864 | // fold (mulhu x, 0) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 865 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 866 | return N1; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 867 | // fold (mulhu x, 1) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 868 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 869 | return DAG.getConstant(0, N0.getValueType()); |
| 870 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 873 | /// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with |
| 874 | /// two operands of the same opcode, try to simplify it. |
| 875 | SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) { |
| 876 | SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1); |
| 877 | MVT::ValueType VT = N0.getValueType(); |
| 878 | assert(N0.getOpcode() == N1.getOpcode() && "Bad input!"); |
| 879 | |
Chris Lattner | 540121f | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 880 | // For each of OP in AND/OR/XOR: |
| 881 | // fold (OP (zext x), (zext y)) -> (zext (OP x, y)) |
| 882 | // fold (OP (sext x), (sext y)) -> (sext (OP x, y)) |
| 883 | // fold (OP (aext x), (aext y)) -> (aext (OP x, y)) |
Chris Lattner | 0d8dae7 | 2006-05-05 06:32:04 +0000 | [diff] [blame] | 884 | // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) |
Chris Lattner | 540121f | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 885 | if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND|| |
Chris Lattner | 0d8dae7 | 2006-05-05 06:32:04 +0000 | [diff] [blame] | 886 | N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) && |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 887 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) { |
| 888 | SDOperand ORNode = DAG.getNode(N->getOpcode(), |
| 889 | N0.getOperand(0).getValueType(), |
| 890 | N0.getOperand(0), N1.getOperand(0)); |
| 891 | AddToWorkList(ORNode.Val); |
Chris Lattner | 540121f | 2006-05-05 06:31:05 +0000 | [diff] [blame] | 892 | return DAG.getNode(N0.getOpcode(), VT, ORNode); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 895 | // For each of OP in SHL/SRL/SRA/AND... |
| 896 | // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z) |
| 897 | // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z) |
| 898 | // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z) |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 899 | if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL || |
Chris Lattner | a3dc3f6 | 2006-05-05 06:10:43 +0000 | [diff] [blame] | 900 | N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) && |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 901 | N0.getOperand(1) == N1.getOperand(1)) { |
| 902 | SDOperand ORNode = DAG.getNode(N->getOpcode(), |
| 903 | N0.getOperand(0).getValueType(), |
| 904 | N0.getOperand(0), N1.getOperand(0)); |
| 905 | AddToWorkList(ORNode.Val); |
| 906 | return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1)); |
| 907 | } |
| 908 | |
| 909 | return SDOperand(); |
| 910 | } |
| 911 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 912 | SDOperand DAGCombiner::visitAND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 913 | SDOperand N0 = N->getOperand(0); |
| 914 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 915 | SDOperand LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 916 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 917 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 918 | MVT::ValueType VT = N1.getValueType(); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 919 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 920 | |
| 921 | // fold (and c1, c2) -> c1&c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 922 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 923 | return DAG.getNode(ISD::AND, VT, N0, N1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 924 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 925 | if (N0C && !N1C) |
| 926 | return DAG.getNode(ISD::AND, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 927 | // fold (and x, -1) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 928 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 929 | return N0; |
| 930 | // if (and x, c) is known to be zero, return 0 |
Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 931 | if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 932 | return DAG.getConstant(0, VT); |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 933 | // reassociate and |
| 934 | SDOperand RAND = ReassociateOps(ISD::AND, N0, N1); |
| 935 | if (RAND.Val != 0) |
| 936 | return RAND; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 937 | // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF |
Nate Begeman | 5dc7e86 | 2005-11-02 18:42:59 +0000 | [diff] [blame] | 938 | if (N1C && N0.getOpcode() == ISD::OR) |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 939 | if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 940 | if ((ORI->getValue() & N1C->getValue()) == N1C->getValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 941 | return N1; |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 942 | // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits. |
| 943 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
Chris Lattner | 1ec05d1 | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 944 | unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType()); |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 945 | if (TLI.MaskedValueIsZero(N0.getOperand(0), |
Chris Lattner | 1ec05d1 | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 946 | ~N1C->getValue() & InMask)) { |
| 947 | SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(), |
| 948 | N0.getOperand(0)); |
| 949 | |
| 950 | // Replace uses of the AND with uses of the Zero extend node. |
| 951 | CombineTo(N, Zext); |
| 952 | |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 953 | // We actually want to replace all uses of the any_extend with the |
| 954 | // zero_extend, to avoid duplicating things. This will later cause this |
| 955 | // AND to be folded. |
Chris Lattner | 1ec05d1 | 2006-03-01 21:47:21 +0000 | [diff] [blame] | 956 | CombineTo(N0.Val, Zext); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 957 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 3603cd6 | 2006-02-02 07:17:31 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 960 | // fold (and (setcc x), (setcc y)) -> (setcc (and x, y)) |
| 961 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 962 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 963 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
| 964 | |
| 965 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
| 966 | MVT::isInteger(LL.getValueType())) { |
| 967 | // fold (X == 0) & (Y == 0) -> (X|Y == 0) |
| 968 | if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) { |
| 969 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 970 | AddToWorkList(ORNode.Val); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 971 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 972 | } |
| 973 | // fold (X == -1) & (Y == -1) -> (X&Y == -1) |
| 974 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) { |
| 975 | SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 976 | AddToWorkList(ANDNode.Val); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 977 | return DAG.getSetCC(VT, ANDNode, LR, Op1); |
| 978 | } |
| 979 | // fold (X > -1) & (Y > -1) -> (X|Y > -1) |
| 980 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) { |
| 981 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 982 | AddToWorkList(ORNode.Val); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 983 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 984 | } |
| 985 | } |
| 986 | // canonicalize equivalent to ll == rl |
| 987 | if (LL == RR && LR == RL) { |
| 988 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 989 | std::swap(RL, RR); |
| 990 | } |
| 991 | if (LL == RL && LR == RR) { |
| 992 | bool isInteger = MVT::isInteger(LL.getValueType()); |
| 993 | ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger); |
| 994 | if (Result != ISD::SETCC_INVALID) |
| 995 | return DAG.getSetCC(N0.getValueType(), LL, LR, Result); |
| 996 | } |
| 997 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 998 | |
| 999 | // Simplify: and (op x...), (op y...) -> (op (and x, y)) |
| 1000 | if (N0.getOpcode() == N1.getOpcode()) { |
| 1001 | SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
| 1002 | if (Tmp.Val) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1003 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 1004 | |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 1005 | // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1) |
| 1006 | // fold (and (sra)) -> (and (srl)) when possible. |
Chris Lattner | 6ea2dee | 2006-03-25 22:19:00 +0000 | [diff] [blame] | 1007 | if (!MVT::isVector(VT) && |
| 1008 | SimplifyDemandedBits(SDOperand(N, 0))) |
Chris Lattner | ef027f9 | 2006-04-21 15:32:26 +0000 | [diff] [blame] | 1009 | return SDOperand(N, 0); |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1010 | // fold (zext_inreg (extload x)) -> (zextload x) |
Nate Begeman | 5054f16 | 2005-10-14 01:12:21 +0000 | [diff] [blame] | 1011 | if (N0.getOpcode() == ISD::EXTLOAD) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1012 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1013 | // If we zero all the possible extended bits, then we can turn this into |
| 1014 | // a zextload if we are running before legalize or the operation is legal. |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 1015 | if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) && |
Chris Lattner | 67a44cd | 2005-10-13 18:16:34 +0000 | [diff] [blame] | 1016 | (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1017 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1018 | N0.getOperand(1), N0.getOperand(2), |
| 1019 | EVT); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1020 | AddToWorkList(N); |
Chris Lattner | 67a44cd | 2005-10-13 18:16:34 +0000 | [diff] [blame] | 1021 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1022 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1026 | if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1027 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1028 | // If we zero all the possible extended bits, then we can turn this into |
| 1029 | // 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] | 1030 | if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) && |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1031 | (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1032 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1033 | N0.getOperand(1), N0.getOperand(2), |
| 1034 | EVT); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1035 | AddToWorkList(N); |
Chris Lattner | 67a44cd | 2005-10-13 18:16:34 +0000 | [diff] [blame] | 1036 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1037 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1038 | } |
| 1039 | } |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | 35a9f5a | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 1041 | // fold (and (load x), 255) -> (zextload x, i8) |
| 1042 | // fold (and (extload x, i16), 255) -> (zextload x, i8) |
| 1043 | if (N1C && |
| 1044 | (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD || |
| 1045 | N0.getOpcode() == ISD::ZEXTLOAD) && |
| 1046 | N0.hasOneUse()) { |
| 1047 | MVT::ValueType EVT, LoadedVT; |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1048 | if (N1C->getValue() == 255) |
| 1049 | EVT = MVT::i8; |
| 1050 | else if (N1C->getValue() == 65535) |
| 1051 | EVT = MVT::i16; |
| 1052 | else if (N1C->getValue() == ~0U) |
| 1053 | EVT = MVT::i32; |
| 1054 | else |
| 1055 | EVT = MVT::Other; |
Chris Lattner | 35a9f5a | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 1056 | |
| 1057 | LoadedVT = N0.getOpcode() == ISD::LOAD ? VT : |
| 1058 | cast<VTSDNode>(N0.getOperand(3))->getVT(); |
Chris Lattner | e44be60 | 2006-04-04 17:39:18 +0000 | [diff] [blame] | 1059 | if (EVT != MVT::Other && LoadedVT > EVT && |
| 1060 | (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) { |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1061 | MVT::ValueType PtrType = N0.getOperand(1).getValueType(); |
| 1062 | // For big endian targets, we need to add an offset to the pointer to load |
| 1063 | // the correct bytes. For little endian systems, we merely need to read |
| 1064 | // fewer bytes from the same pointer. |
Chris Lattner | 35a9f5a | 2006-02-28 06:49:37 +0000 | [diff] [blame] | 1065 | unsigned PtrOff = |
| 1066 | (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8; |
| 1067 | SDOperand NewPtr = N0.getOperand(1); |
| 1068 | if (!TLI.isLittleEndian()) |
| 1069 | NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr, |
| 1070 | DAG.getConstant(PtrOff, PtrType)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1071 | AddToWorkList(NewPtr.Val); |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1072 | SDOperand Load = |
| 1073 | DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr, |
| 1074 | N0.getOperand(2), EVT); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1075 | AddToWorkList(N); |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1076 | CombineTo(N0.Val, Load, Load.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1077 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | 15045b6 | 2006-02-28 06:35:35 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1081 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1084 | SDOperand DAGCombiner::visitOR(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1085 | SDOperand N0 = N->getOperand(0); |
| 1086 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1087 | SDOperand LL, LR, RL, RR, CC0, CC1; |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1088 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1089 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1090 | MVT::ValueType VT = N1.getValueType(); |
| 1091 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1092 | |
| 1093 | // fold (or c1, c2) -> c1|c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1094 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1095 | return DAG.getNode(ISD::OR, VT, N0, N1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1096 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1097 | if (N0C && !N1C) |
| 1098 | return DAG.getNode(ISD::OR, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1099 | // fold (or x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1100 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1101 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1102 | // fold (or x, -1) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1103 | if (N1C && N1C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1104 | return N1; |
| 1105 | // fold (or x, c) -> c iff (x & ~c) == 0 |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 1106 | if (N1C && |
| 1107 | TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1108 | return N1; |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1109 | // reassociate or |
| 1110 | SDOperand ROR = ReassociateOps(ISD::OR, N0, N1); |
| 1111 | if (ROR.Val != 0) |
| 1112 | return ROR; |
| 1113 | // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2) |
| 1114 | if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() && |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 1115 | isa<ConstantSDNode>(N0.getOperand(1))) { |
Chris Lattner | 731d348 | 2005-10-27 05:06:38 +0000 | [diff] [blame] | 1116 | ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1)); |
| 1117 | return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0), |
| 1118 | N1), |
| 1119 | DAG.getConstant(N1C->getValue() | C1->getValue(), VT)); |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 1120 | } |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1121 | // fold (or (setcc x), (setcc y)) -> (setcc (or x, y)) |
| 1122 | if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){ |
| 1123 | ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get(); |
| 1124 | ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get(); |
| 1125 | |
| 1126 | if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 && |
| 1127 | MVT::isInteger(LL.getValueType())) { |
| 1128 | // fold (X != 0) | (Y != 0) -> (X|Y != 0) |
| 1129 | // fold (X < 0) | (Y < 0) -> (X|Y < 0) |
| 1130 | if (cast<ConstantSDNode>(LR)->getValue() == 0 && |
| 1131 | (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) { |
| 1132 | SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1133 | AddToWorkList(ORNode.Val); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1134 | return DAG.getSetCC(VT, ORNode, LR, Op1); |
| 1135 | } |
| 1136 | // fold (X != -1) | (Y != -1) -> (X&Y != -1) |
| 1137 | // fold (X > -1) | (Y > -1) -> (X&Y > -1) |
| 1138 | if (cast<ConstantSDNode>(LR)->isAllOnesValue() && |
| 1139 | (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) { |
| 1140 | SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1141 | AddToWorkList(ANDNode.Val); |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1142 | return DAG.getSetCC(VT, ANDNode, LR, Op1); |
| 1143 | } |
| 1144 | } |
| 1145 | // canonicalize equivalent to ll == rl |
| 1146 | if (LL == RR && LR == RL) { |
| 1147 | Op1 = ISD::getSetCCSwappedOperands(Op1); |
| 1148 | std::swap(RL, RR); |
| 1149 | } |
| 1150 | if (LL == RL && LR == RR) { |
| 1151 | bool isInteger = MVT::isInteger(LL.getValueType()); |
| 1152 | ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger); |
| 1153 | if (Result != ISD::SETCC_INVALID) |
| 1154 | return DAG.getSetCC(N0.getValueType(), LL, LR, Result); |
| 1155 | } |
| 1156 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 1157 | |
| 1158 | // Simplify: or (op x...), (op y...) -> (op (or x, y)) |
| 1159 | if (N0.getOpcode() == N1.getOpcode()) { |
| 1160 | SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
| 1161 | if (Tmp.Val) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1162 | } |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | 1ec7273 | 2006-09-14 21:11:37 +0000 | [diff] [blame] | 1164 | // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible. |
| 1165 | if (N0.getOpcode() == ISD::AND && |
| 1166 | N1.getOpcode() == ISD::AND && |
| 1167 | N0.getOperand(1).getOpcode() == ISD::Constant && |
| 1168 | N1.getOperand(1).getOpcode() == ISD::Constant && |
| 1169 | // Don't increase # computations. |
| 1170 | (N0.Val->hasOneUse() || N1.Val->hasOneUse())) { |
| 1171 | // We can only do this xform if we know that bits from X that are set in C2 |
| 1172 | // but not in C1 are already zero. Likewise for Y. |
| 1173 | uint64_t LHSMask = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
| 1174 | uint64_t RHSMask = cast<ConstantSDNode>(N1.getOperand(1))->getValue(); |
| 1175 | |
| 1176 | if (TLI.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) && |
| 1177 | TLI.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) { |
| 1178 | SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0)); |
| 1179 | return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT)); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 1184 | // See if this is some rotate idiom. |
| 1185 | if (SDNode *Rot = MatchRotate(N0, N1)) |
| 1186 | return SDOperand(Rot, 0); |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 1187 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1188 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 1191 | |
| 1192 | /// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present. |
| 1193 | static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) { |
| 1194 | if (Op.getOpcode() == ISD::AND) { |
| 1195 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 1196 | Mask = Op.getOperand(1); |
| 1197 | Op = Op.getOperand(0); |
| 1198 | } else { |
| 1199 | return false; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) { |
| 1204 | Shift = Op; |
| 1205 | return true; |
| 1206 | } |
| 1207 | return false; |
| 1208 | } |
| 1209 | |
| 1210 | |
| 1211 | // MatchRotate - Handle an 'or' of two operands. If this is one of the many |
| 1212 | // idioms for rotate, and if the target supports rotation instructions, generate |
| 1213 | // a rot[lr]. |
| 1214 | SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) { |
| 1215 | // Must be a legal type. Expanded an promoted things won't work with rotates. |
| 1216 | MVT::ValueType VT = LHS.getValueType(); |
| 1217 | if (!TLI.isTypeLegal(VT)) return 0; |
| 1218 | |
| 1219 | // The target must have at least one rotate flavor. |
| 1220 | bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT); |
| 1221 | bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT); |
| 1222 | if (!HasROTL && !HasROTR) return 0; |
| 1223 | |
| 1224 | // Match "(X shl/srl V1) & V2" where V2 may not be present. |
| 1225 | SDOperand LHSShift; // The shift. |
| 1226 | SDOperand LHSMask; // AND value if any. |
| 1227 | if (!MatchRotateHalf(LHS, LHSShift, LHSMask)) |
| 1228 | return 0; // Not part of a rotate. |
| 1229 | |
| 1230 | SDOperand RHSShift; // The shift. |
| 1231 | SDOperand RHSMask; // AND value if any. |
| 1232 | if (!MatchRotateHalf(RHS, RHSShift, RHSMask)) |
| 1233 | return 0; // Not part of a rotate. |
| 1234 | |
| 1235 | if (LHSShift.getOperand(0) != RHSShift.getOperand(0)) |
| 1236 | return 0; // Not shifting the same value. |
| 1237 | |
| 1238 | if (LHSShift.getOpcode() == RHSShift.getOpcode()) |
| 1239 | return 0; // Shifts must disagree. |
| 1240 | |
| 1241 | // Canonicalize shl to left side in a shl/srl pair. |
| 1242 | if (RHSShift.getOpcode() == ISD::SHL) { |
| 1243 | std::swap(LHS, RHS); |
| 1244 | std::swap(LHSShift, RHSShift); |
| 1245 | std::swap(LHSMask , RHSMask ); |
| 1246 | } |
| 1247 | |
| 1248 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 1249 | |
| 1250 | // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1) |
| 1251 | // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2) |
| 1252 | if (LHSShift.getOperand(1).getOpcode() == ISD::Constant && |
| 1253 | RHSShift.getOperand(1).getOpcode() == ISD::Constant) { |
| 1254 | uint64_t LShVal = cast<ConstantSDNode>(LHSShift.getOperand(1))->getValue(); |
| 1255 | uint64_t RShVal = cast<ConstantSDNode>(RHSShift.getOperand(1))->getValue(); |
| 1256 | if ((LShVal + RShVal) != OpSizeInBits) |
| 1257 | return 0; |
| 1258 | |
| 1259 | SDOperand Rot; |
| 1260 | if (HasROTL) |
| 1261 | Rot = DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0), |
| 1262 | LHSShift.getOperand(1)); |
| 1263 | else |
| 1264 | Rot = DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0), |
| 1265 | RHSShift.getOperand(1)); |
| 1266 | |
| 1267 | // If there is an AND of either shifted operand, apply it to the result. |
| 1268 | if (LHSMask.Val || RHSMask.Val) { |
| 1269 | uint64_t Mask = MVT::getIntVTBitMask(VT); |
| 1270 | |
| 1271 | if (LHSMask.Val) { |
| 1272 | uint64_t RHSBits = (1ULL << LShVal)-1; |
| 1273 | Mask &= cast<ConstantSDNode>(LHSMask)->getValue() | RHSBits; |
| 1274 | } |
| 1275 | if (RHSMask.Val) { |
| 1276 | uint64_t LHSBits = ~((1ULL << (OpSizeInBits-RShVal))-1); |
| 1277 | Mask &= cast<ConstantSDNode>(RHSMask)->getValue() | LHSBits; |
| 1278 | } |
| 1279 | |
| 1280 | Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT)); |
| 1281 | } |
| 1282 | |
| 1283 | return Rot.Val; |
| 1284 | } |
| 1285 | |
| 1286 | // If there is a mask here, and we have a variable shift, we can't be sure |
| 1287 | // that we're masking out the right stuff. |
| 1288 | if (LHSMask.Val || RHSMask.Val) |
| 1289 | return 0; |
| 1290 | |
| 1291 | // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y) |
| 1292 | // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y)) |
| 1293 | if (RHSShift.getOperand(1).getOpcode() == ISD::SUB && |
| 1294 | LHSShift.getOperand(1) == RHSShift.getOperand(1).getOperand(1)) { |
| 1295 | if (ConstantSDNode *SUBC = |
| 1296 | dyn_cast<ConstantSDNode>(RHSShift.getOperand(1).getOperand(0))) { |
| 1297 | if (SUBC->getValue() == OpSizeInBits) |
| 1298 | if (HasROTL) |
| 1299 | return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0), |
| 1300 | LHSShift.getOperand(1)).Val; |
| 1301 | else |
| 1302 | return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0), |
| 1303 | LHSShift.getOperand(1)).Val; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y) |
| 1308 | // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y)) |
| 1309 | if (LHSShift.getOperand(1).getOpcode() == ISD::SUB && |
| 1310 | RHSShift.getOperand(1) == LHSShift.getOperand(1).getOperand(1)) { |
| 1311 | if (ConstantSDNode *SUBC = |
| 1312 | dyn_cast<ConstantSDNode>(LHSShift.getOperand(1).getOperand(0))) { |
| 1313 | if (SUBC->getValue() == OpSizeInBits) |
| 1314 | if (HasROTL) |
| 1315 | return DAG.getNode(ISD::ROTL, VT, LHSShift.getOperand(0), |
| 1316 | LHSShift.getOperand(1)).Val; |
| 1317 | else |
| 1318 | return DAG.getNode(ISD::ROTR, VT, LHSShift.getOperand(0), |
| 1319 | RHSShift.getOperand(1)).Val; |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
| 1326 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1327 | SDOperand DAGCombiner::visitXOR(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1328 | SDOperand N0 = N->getOperand(0); |
| 1329 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1330 | SDOperand LHS, RHS, CC; |
| 1331 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1332 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1333 | MVT::ValueType VT = N0.getValueType(); |
| 1334 | |
| 1335 | // fold (xor c1, c2) -> c1^c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1336 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1337 | return DAG.getNode(ISD::XOR, VT, N0, N1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1338 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 1339 | if (N0C && !N1C) |
| 1340 | return DAG.getNode(ISD::XOR, VT, N1, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1341 | // fold (xor x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1342 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1343 | return N0; |
Nate Begeman | cd4d58c | 2006-02-03 06:46:56 +0000 | [diff] [blame] | 1344 | // reassociate xor |
| 1345 | SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1); |
| 1346 | if (RXOR.Val != 0) |
| 1347 | return RXOR; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1348 | // fold !(x cc y) -> (x !cc y) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1349 | if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) { |
| 1350 | bool isInt = MVT::isInteger(LHS.getValueType()); |
| 1351 | ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
| 1352 | isInt); |
| 1353 | if (N0.getOpcode() == ISD::SETCC) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1354 | return DAG.getSetCC(VT, LHS, RHS, NotCC); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1355 | if (N0.getOpcode() == ISD::SELECT_CC) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1356 | return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1357 | assert(0 && "Unhandled SetCC Equivalent!"); |
| 1358 | abort(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1359 | } |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1360 | // fold !(x or y) -> (!x and !y) iff x or y are setcc |
| 1361 | if (N1C && N1C->getValue() == 1 && |
| 1362 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1363 | SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1364 | if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) { |
| 1365 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1366 | LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS |
| 1367 | RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1368 | AddToWorkList(LHS.Val); AddToWorkList(RHS.Val); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1369 | return DAG.getNode(NewOpcode, VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1370 | } |
| 1371 | } |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1372 | // fold !(x or y) -> (!x and !y) iff x or y are constants |
| 1373 | if (N1C && N1C->isAllOnesValue() && |
| 1374 | (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1375 | SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1376 | if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) { |
| 1377 | unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1378 | LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS |
| 1379 | RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1380 | AddToWorkList(LHS.Val); AddToWorkList(RHS.Val); |
Nate Begeman | 9980119 | 2005-09-07 23:25:52 +0000 | [diff] [blame] | 1381 | return DAG.getNode(NewOpcode, VT, LHS, RHS); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
Nate Begeman | 223df22 | 2005-09-08 20:18:10 +0000 | [diff] [blame] | 1384 | // fold (xor (xor x, c1), c2) -> (xor x, c1^c2) |
| 1385 | if (N1C && N0.getOpcode() == ISD::XOR) { |
| 1386 | ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0)); |
| 1387 | ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 1388 | if (N00C) |
| 1389 | return DAG.getNode(ISD::XOR, VT, N0.getOperand(1), |
| 1390 | DAG.getConstant(N1C->getValue()^N00C->getValue(), VT)); |
| 1391 | if (N01C) |
| 1392 | return DAG.getNode(ISD::XOR, VT, N0.getOperand(0), |
| 1393 | DAG.getConstant(N1C->getValue()^N01C->getValue(), VT)); |
| 1394 | } |
| 1395 | // fold (xor x, x) -> 0 |
Chris Lattner | 4fbdd59 | 2006-03-28 19:11:05 +0000 | [diff] [blame] | 1396 | if (N0 == N1) { |
| 1397 | if (!MVT::isVector(VT)) { |
| 1398 | return DAG.getConstant(0, VT); |
| 1399 | } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) { |
| 1400 | // Produce a vector of zeros. |
| 1401 | SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT)); |
| 1402 | std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1403 | return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); |
Chris Lattner | 4fbdd59 | 2006-03-28 19:11:05 +0000 | [diff] [blame] | 1404 | } |
| 1405 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 1406 | |
| 1407 | // Simplify: xor (op x...), (op y...) -> (op (xor x, y)) |
| 1408 | if (N0.getOpcode() == N1.getOpcode()) { |
| 1409 | SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N); |
| 1410 | if (Tmp.Val) return Tmp; |
Nate Begeman | 39ee1ac | 2005-09-09 19:49:52 +0000 | [diff] [blame] | 1411 | } |
Chris Lattner | 35e5c14 | 2006-05-05 05:51:50 +0000 | [diff] [blame] | 1412 | |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 1413 | // Simplify the expression using non-local knowledge. |
| 1414 | if (!MVT::isVector(VT) && |
| 1415 | SimplifyDemandedBits(SDOperand(N, 0))) |
Chris Lattner | ef027f9 | 2006-04-21 15:32:26 +0000 | [diff] [blame] | 1416 | return SDOperand(N, 0); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 1417 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1418 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1421 | SDOperand DAGCombiner::visitSHL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1422 | SDOperand N0 = N->getOperand(0); |
| 1423 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1424 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1425 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1426 | MVT::ValueType VT = N0.getValueType(); |
| 1427 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 1428 | |
| 1429 | // fold (shl c1, c2) -> c1<<c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1430 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1431 | return DAG.getNode(ISD::SHL, VT, N0, N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1432 | // fold (shl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1433 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1434 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1435 | // fold (shl x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1436 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1437 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1438 | // fold (shl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1439 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1440 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1441 | // if (shl x, c) is known to be zero, return 0 |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 1442 | if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1443 | return DAG.getConstant(0, VT); |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 1444 | if (SimplifyDemandedBits(SDOperand(N, 0))) |
Chris Lattner | ef027f9 | 2006-04-21 15:32:26 +0000 | [diff] [blame] | 1445 | return SDOperand(N, 0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1446 | // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1447 | if (N1C && N0.getOpcode() == ISD::SHL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1448 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1449 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1450 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1451 | if (c1 + c2 > OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1452 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1453 | return DAG.getNode(ISD::SHL, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1454 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1455 | } |
| 1456 | // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or |
| 1457 | // (srl (and x, -1 << c1), c1-c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1458 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1459 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1460 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1461 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1462 | SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
| 1463 | DAG.getConstant(~0ULL << c1, VT)); |
| 1464 | if (c2 > c1) |
| 1465 | return DAG.getNode(ISD::SHL, VT, Mask, |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1466 | DAG.getConstant(c2-c1, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1467 | else |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1468 | return DAG.getNode(ISD::SRL, VT, Mask, |
| 1469 | DAG.getConstant(c1-c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1470 | } |
| 1471 | // fold (shl (sra x, c1), c1) -> (and x, -1 << c1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1472 | if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 1473 | return DAG.getNode(ISD::AND, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1474 | DAG.getConstant(~0ULL << N1C->getValue(), VT)); |
Chris Lattner | cac7059 | 2006-03-05 19:53:55 +0000 | [diff] [blame] | 1475 | // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2) |
| 1476 | if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() && |
| 1477 | isa<ConstantSDNode>(N0.getOperand(1))) { |
| 1478 | return DAG.getNode(ISD::ADD, VT, |
| 1479 | DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1), |
| 1480 | DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1)); |
| 1481 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1482 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1485 | SDOperand DAGCombiner::visitSRA(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1486 | SDOperand N0 = N->getOperand(0); |
| 1487 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1488 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1489 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1490 | MVT::ValueType VT = N0.getValueType(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1491 | |
| 1492 | // fold (sra c1, c2) -> c1>>c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1493 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1494 | return DAG.getNode(ISD::SRA, VT, N0, N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1495 | // fold (sra 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1496 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1497 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1498 | // fold (sra -1, x) -> -1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1499 | if (N0C && N0C->isAllOnesValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1500 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1501 | // fold (sra x, c >= size(x)) -> undef |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 1502 | if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1503 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1504 | // fold (sra x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1505 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1506 | return N0; |
Nate Begeman | fb7217b | 2006-02-17 19:54:08 +0000 | [diff] [blame] | 1507 | // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports |
| 1508 | // sext_inreg. |
| 1509 | if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) { |
| 1510 | unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue(); |
| 1511 | MVT::ValueType EVT; |
| 1512 | switch (LowBits) { |
| 1513 | default: EVT = MVT::Other; break; |
| 1514 | case 1: EVT = MVT::i1; break; |
| 1515 | case 8: EVT = MVT::i8; break; |
| 1516 | case 16: EVT = MVT::i16; break; |
| 1517 | case 32: EVT = MVT::i32; break; |
| 1518 | } |
| 1519 | if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)) |
| 1520 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), |
| 1521 | DAG.getValueType(EVT)); |
| 1522 | } |
Chris Lattner | 71d9ebc | 2006-02-28 06:23:04 +0000 | [diff] [blame] | 1523 | |
| 1524 | // fold (sra (sra x, c1), c2) -> (sra x, c1+c2) |
| 1525 | if (N1C && N0.getOpcode() == ISD::SRA) { |
| 1526 | if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 1527 | unsigned Sum = N1C->getValue() + C1->getValue(); |
| 1528 | if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1; |
| 1529 | return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), |
| 1530 | DAG.getConstant(Sum, N1C->getValueType(0))); |
| 1531 | } |
| 1532 | } |
| 1533 | |
Chris Lattner | a850446 | 2006-05-08 20:51:54 +0000 | [diff] [blame] | 1534 | // Simplify, based on bits shifted out of the LHS. |
| 1535 | if (N1C && SimplifyDemandedBits(SDOperand(N, 0))) |
| 1536 | return SDOperand(N, 0); |
| 1537 | |
| 1538 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1539 | // 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] | 1540 | if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1541 | return DAG.getNode(ISD::SRL, VT, N0, N1); |
| 1542 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1545 | SDOperand DAGCombiner::visitSRL(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1546 | SDOperand N0 = N->getOperand(0); |
| 1547 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1548 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1549 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1550 | MVT::ValueType VT = N0.getValueType(); |
| 1551 | unsigned OpSizeInBits = MVT::getSizeInBits(VT); |
| 1552 | |
| 1553 | // fold (srl c1, c2) -> c1 >>u c2 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1554 | if (N0C && N1C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1555 | return DAG.getNode(ISD::SRL, VT, N0, N1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1556 | // fold (srl 0, x) -> 0 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1557 | if (N0C && N0C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1558 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1559 | // fold (srl x, c >= size(x)) -> undef |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1560 | if (N1C && N1C->getValue() >= OpSizeInBits) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1561 | return DAG.getNode(ISD::UNDEF, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1562 | // fold (srl x, 0) -> x |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1563 | if (N1C && N1C->isNullValue()) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1564 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1565 | // if (srl x, c) is known to be zero, return 0 |
Chris Lattner | c6fd6cd | 2006-01-30 04:09:27 +0000 | [diff] [blame] | 1566 | if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits))) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1567 | return DAG.getConstant(0, VT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1568 | // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1569 | if (N1C && N0.getOpcode() == ISD::SRL && |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1570 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1571 | uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1572 | uint64_t c2 = N1C->getValue(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1573 | if (c1 + c2 > 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 | return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1576 | DAG.getConstant(c1 + c2, N1.getValueType())); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1577 | } |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 1578 | |
Chris Lattner | 06afe07 | 2006-05-05 22:53:17 +0000 | [diff] [blame] | 1579 | // fold (srl (anyextend x), c) -> (anyextend (srl x, c)) |
| 1580 | if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) { |
| 1581 | // Shifting in all undef bits? |
| 1582 | MVT::ValueType SmallVT = N0.getOperand(0).getValueType(); |
| 1583 | if (N1C->getValue() >= MVT::getSizeInBits(SmallVT)) |
| 1584 | return DAG.getNode(ISD::UNDEF, VT); |
| 1585 | |
| 1586 | SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1); |
| 1587 | AddToWorkList(SmallShift.Val); |
| 1588 | return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift); |
| 1589 | } |
| 1590 | |
Chris Lattner | 350bec0 | 2006-04-02 06:11:11 +0000 | [diff] [blame] | 1591 | // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit). |
| 1592 | if (N1C && N0.getOpcode() == ISD::CTLZ && |
| 1593 | N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) { |
| 1594 | uint64_t KnownZero, KnownOne, Mask = MVT::getIntVTBitMask(VT); |
| 1595 | TLI.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne); |
| 1596 | |
| 1597 | // If any of the input bits are KnownOne, then the input couldn't be all |
| 1598 | // zeros, thus the result of the srl will always be zero. |
| 1599 | if (KnownOne) return DAG.getConstant(0, VT); |
| 1600 | |
| 1601 | // If all of the bits input the to ctlz node are known to be zero, then |
| 1602 | // the result of the ctlz is "32" and the result of the shift is one. |
| 1603 | uint64_t UnknownBits = ~KnownZero & Mask; |
| 1604 | if (UnknownBits == 0) return DAG.getConstant(1, VT); |
| 1605 | |
| 1606 | // Otherwise, check to see if there is exactly one bit input to the ctlz. |
| 1607 | if ((UnknownBits & (UnknownBits-1)) == 0) { |
| 1608 | // Okay, we know that only that the single bit specified by UnknownBits |
| 1609 | // could be set on input to the CTLZ node. If this bit is set, the SRL |
| 1610 | // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair |
| 1611 | // to an SRL,XOR pair, which is likely to simplify more. |
| 1612 | unsigned ShAmt = CountTrailingZeros_64(UnknownBits); |
| 1613 | SDOperand Op = N0.getOperand(0); |
| 1614 | if (ShAmt) { |
| 1615 | Op = DAG.getNode(ISD::SRL, VT, Op, |
| 1616 | DAG.getConstant(ShAmt, TLI.getShiftAmountTy())); |
| 1617 | AddToWorkList(Op.Val); |
| 1618 | } |
| 1619 | return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT)); |
| 1620 | } |
| 1621 | } |
| 1622 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1623 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1626 | SDOperand DAGCombiner::visitCTLZ(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1627 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1628 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1629 | |
| 1630 | // fold (ctlz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1631 | if (isa<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1632 | return DAG.getNode(ISD::CTLZ, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1633 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1636 | SDOperand DAGCombiner::visitCTTZ(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1637 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1638 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1639 | |
| 1640 | // fold (cttz c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1641 | if (isa<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1642 | return DAG.getNode(ISD::CTTZ, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1643 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1646 | SDOperand DAGCombiner::visitCTPOP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1647 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1648 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1649 | |
| 1650 | // fold (ctpop c1) -> c2 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1651 | if (isa<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1652 | return DAG.getNode(ISD::CTPOP, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1653 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1656 | SDOperand DAGCombiner::visitSELECT(SDNode *N) { |
| 1657 | SDOperand N0 = N->getOperand(0); |
| 1658 | SDOperand N1 = N->getOperand(1); |
| 1659 | SDOperand N2 = N->getOperand(2); |
| 1660 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1661 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1662 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
| 1663 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1664 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1665 | // fold select C, X, X -> X |
| 1666 | if (N1 == N2) |
| 1667 | return N1; |
| 1668 | // fold select true, X, Y -> X |
| 1669 | if (N0C && !N0C->isNullValue()) |
| 1670 | return N1; |
| 1671 | // fold select false, X, Y -> Y |
| 1672 | if (N0C && N0C->isNullValue()) |
| 1673 | return N2; |
| 1674 | // fold select C, 1, X -> C | X |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1675 | if (MVT::i1 == VT && N1C && N1C->getValue() == 1) |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1676 | return DAG.getNode(ISD::OR, VT, N0, N2); |
| 1677 | // fold select C, 0, X -> ~C & X |
| 1678 | // FIXME: this should check for C type == X type, not i1? |
| 1679 | if (MVT::i1 == VT && N1C && N1C->isNullValue()) { |
| 1680 | SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1681 | AddToWorkList(XORNode.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1682 | return DAG.getNode(ISD::AND, VT, XORNode, N2); |
| 1683 | } |
| 1684 | // fold select C, X, 1 -> ~C | X |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1685 | if (MVT::i1 == VT && N2C && N2C->getValue() == 1) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1686 | SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 1687 | AddToWorkList(XORNode.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1688 | return DAG.getNode(ISD::OR, VT, XORNode, N1); |
| 1689 | } |
| 1690 | // fold select C, X, 0 -> C & X |
| 1691 | // FIXME: this should check for C type == X type, not i1? |
| 1692 | if (MVT::i1 == VT && N2C && N2C->isNullValue()) |
| 1693 | return DAG.getNode(ISD::AND, VT, N0, N1); |
| 1694 | // fold X ? X : Y --> X ? 1 : Y --> X | Y |
| 1695 | if (MVT::i1 == VT && N0 == N1) |
| 1696 | return DAG.getNode(ISD::OR, VT, N0, N2); |
| 1697 | // fold X ? Y : X --> X ? Y : 0 --> X & Y |
| 1698 | if (MVT::i1 == VT && N0 == N2) |
| 1699 | return DAG.getNode(ISD::AND, VT, N0, N1); |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1701 | // If we can fold this based on the true/false value, do so. |
| 1702 | if (SimplifySelectOps(N, N1, N2)) |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 1703 | return SDOperand(N, 0); // Don't revisit N. |
| 1704 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1705 | // fold selects based on a setcc into other things, such as min/max/abs |
| 1706 | if (N0.getOpcode() == ISD::SETCC) |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1707 | // FIXME: |
| 1708 | // Check against MVT::Other for SELECT_CC, which is a workaround for targets |
| 1709 | // having to say they don't support SELECT_CC on every type the DAG knows |
| 1710 | // about, since there is no way to mark an opcode illegal at all value types |
| 1711 | if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other)) |
| 1712 | return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1), |
| 1713 | N1, N2, N0.getOperand(2)); |
| 1714 | else |
| 1715 | return SimplifySelect(N0, N1, N2); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1716 | return SDOperand(); |
| 1717 | } |
| 1718 | |
| 1719 | SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) { |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1720 | SDOperand N0 = N->getOperand(0); |
| 1721 | SDOperand N1 = N->getOperand(1); |
| 1722 | SDOperand N2 = N->getOperand(2); |
| 1723 | SDOperand N3 = N->getOperand(3); |
| 1724 | SDOperand N4 = N->getOperand(4); |
| 1725 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
| 1726 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 1727 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2); |
| 1728 | ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get(); |
| 1729 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1730 | // fold select_cc lhs, rhs, x, x, cc -> x |
| 1731 | if (N2 == N3) |
| 1732 | return N2; |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1733 | |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 1734 | // Determine if the condition we're dealing with is constant |
| 1735 | SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false); |
| 1736 | |
| 1737 | if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) { |
| 1738 | if (SCCC->getValue()) |
| 1739 | return N2; // cond always true -> true val |
| 1740 | else |
| 1741 | return N3; // cond always false -> false val |
| 1742 | } |
| 1743 | |
| 1744 | // Fold to a simpler select_cc |
| 1745 | if (SCC.Val && SCC.getOpcode() == ISD::SETCC) |
| 1746 | return DAG.getNode(ISD::SELECT_CC, N2.getValueType(), |
| 1747 | SCC.getOperand(0), SCC.getOperand(1), N2, N3, |
| 1748 | SCC.getOperand(2)); |
| 1749 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1750 | // If we can fold this based on the true/false value, do so. |
| 1751 | if (SimplifySelectOps(N, N2, N3)) |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 1752 | return SDOperand(N, 0); // Don't revisit N. |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 1753 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 1754 | // fold select_cc into other things, such as min/max/abs |
| 1755 | return SimplifySelectCC(N0, N1, N2, N3, CC); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | SDOperand DAGCombiner::visitSETCC(SDNode *N) { |
| 1759 | return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1), |
| 1760 | cast<CondCodeSDNode>(N->getOperand(2))->get()); |
| 1761 | } |
| 1762 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1763 | SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1764 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1765 | MVT::ValueType VT = N->getValueType(0); |
| 1766 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1767 | // fold (sext c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1768 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1769 | return DAG.getNode(ISD::SIGN_EXTEND, VT, N0); |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1770 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1771 | // fold (sext (sext x)) -> (sext x) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1772 | // fold (sext (aext x)) -> (sext x) |
| 1773 | if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1774 | return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0)); |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1775 | |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 1776 | // fold (sext (truncate x)) -> (sextinreg x). |
| 1777 | if (N0.getOpcode() == ISD::TRUNCATE && |
Chris Lattner | bf37087 | 2006-09-21 06:17:39 +0000 | [diff] [blame] | 1778 | (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, |
| 1779 | N0.getValueType()))) { |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 1780 | SDOperand Op = N0.getOperand(0); |
| 1781 | if (Op.getValueType() < VT) { |
| 1782 | Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op); |
| 1783 | } else if (Op.getValueType() > VT) { |
| 1784 | Op = DAG.getNode(ISD::TRUNCATE, VT, Op); |
| 1785 | } |
| 1786 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op, |
Chris Lattner | b14ab8a | 2005-12-07 07:11:03 +0000 | [diff] [blame] | 1787 | DAG.getValueType(N0.getValueType())); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 1788 | } |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1789 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1790 | // fold (sext (load x)) -> (sext (truncate (sextload x))) |
Chris Lattner | d0f6d18 | 2005-12-15 19:02:38 +0000 | [diff] [blame] | 1791 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() && |
| 1792 | (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){ |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1793 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1794 | N0.getOperand(1), N0.getOperand(2), |
| 1795 | N0.getValueType()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1796 | CombineTo(N, ExtLoad); |
Chris Lattner | f988405 | 2005-10-13 21:52:31 +0000 | [diff] [blame] | 1797 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1798 | ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1799 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 1800 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1801 | |
| 1802 | // fold (sext (sextload x)) -> (sext (truncate (sextload x))) |
| 1803 | // fold (sext ( extload x)) -> (sext (truncate (sextload x))) |
| 1804 | if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) && |
| 1805 | N0.hasOneUse()) { |
Nate Begeman | 5c74268 | 2006-05-08 01:35:01 +0000 | [diff] [blame] | 1806 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
| 1807 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1808 | N0.getOperand(1), N0.getOperand(2), EVT); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1809 | CombineTo(N, ExtLoad); |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1810 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1811 | ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1812 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1815 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1818 | SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1819 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1820 | MVT::ValueType VT = N->getValueType(0); |
| 1821 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1822 | // fold (zext c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1823 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 1824 | return DAG.getNode(ISD::ZERO_EXTEND, VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1825 | // fold (zext (zext x)) -> (zext x) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1826 | // fold (zext (aext x)) -> (zext x) |
| 1827 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1828 | return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0)); |
Chris Lattner | 6007b84 | 2006-09-21 06:00:20 +0000 | [diff] [blame] | 1829 | |
| 1830 | // fold (zext (truncate x)) -> (and x, mask) |
| 1831 | if (N0.getOpcode() == ISD::TRUNCATE && |
| 1832 | (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) { |
| 1833 | SDOperand Op = N0.getOperand(0); |
| 1834 | if (Op.getValueType() < VT) { |
| 1835 | Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op); |
| 1836 | } else if (Op.getValueType() > VT) { |
| 1837 | Op = DAG.getNode(ISD::TRUNCATE, VT, Op); |
| 1838 | } |
| 1839 | return DAG.getZeroExtendInReg(Op, N0.getValueType()); |
| 1840 | } |
| 1841 | |
Chris Lattner | 111c228 | 2006-09-21 06:14:31 +0000 | [diff] [blame] | 1842 | // fold (zext (and (trunc x), cst)) -> (and x, cst). |
| 1843 | if (N0.getOpcode() == ISD::AND && |
| 1844 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && |
| 1845 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1846 | SDOperand X = N0.getOperand(0).getOperand(0); |
| 1847 | if (X.getValueType() < VT) { |
| 1848 | X = DAG.getNode(ISD::ANY_EXTEND, VT, X); |
| 1849 | } else if (X.getValueType() > VT) { |
| 1850 | X = DAG.getNode(ISD::TRUNCATE, VT, X); |
| 1851 | } |
| 1852 | uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
| 1853 | return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT)); |
| 1854 | } |
| 1855 | |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1856 | // fold (zext (load x)) -> (zext (truncate (zextload x))) |
Chris Lattner | d0f6d18 | 2005-12-15 19:02:38 +0000 | [diff] [blame] | 1857 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() && |
| 1858 | (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){ |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1859 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1860 | N0.getOperand(1), N0.getOperand(2), |
| 1861 | N0.getValueType()); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1862 | CombineTo(N, ExtLoad); |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1863 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1864 | ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1865 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Evan Cheng | 110dec2 | 2005-12-14 02:19:23 +0000 | [diff] [blame] | 1866 | } |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1867 | |
| 1868 | // fold (zext (zextload x)) -> (zext (truncate (zextload x))) |
| 1869 | // fold (zext ( extload x)) -> (zext (truncate (zextload x))) |
| 1870 | if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) && |
| 1871 | N0.hasOneUse()) { |
Nate Begeman | 5c74268 | 2006-05-08 01:35:01 +0000 | [diff] [blame] | 1872 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
| 1873 | SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), |
| 1874 | N0.getOperand(1), N0.getOperand(2), EVT); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1875 | CombineTo(N, ExtLoad); |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1876 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1877 | ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 1878 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Chris Lattner | ad25d4e | 2005-12-14 19:05:06 +0000 | [diff] [blame] | 1879 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1880 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1883 | SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) { |
| 1884 | SDOperand N0 = N->getOperand(0); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1885 | MVT::ValueType VT = N->getValueType(0); |
| 1886 | |
| 1887 | // fold (aext c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1888 | if (isa<ConstantSDNode>(N0)) |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1889 | return DAG.getNode(ISD::ANY_EXTEND, VT, N0); |
| 1890 | // fold (aext (aext x)) -> (aext x) |
| 1891 | // fold (aext (zext x)) -> (zext x) |
| 1892 | // fold (aext (sext x)) -> (sext x) |
| 1893 | if (N0.getOpcode() == ISD::ANY_EXTEND || |
| 1894 | N0.getOpcode() == ISD::ZERO_EXTEND || |
| 1895 | N0.getOpcode() == ISD::SIGN_EXTEND) |
| 1896 | return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0)); |
| 1897 | |
Chris Lattner | 8475058 | 2006-09-20 06:29:17 +0000 | [diff] [blame] | 1898 | // fold (aext (truncate x)) |
| 1899 | if (N0.getOpcode() == ISD::TRUNCATE) { |
| 1900 | SDOperand TruncOp = N0.getOperand(0); |
| 1901 | if (TruncOp.getValueType() == VT) |
| 1902 | return TruncOp; // x iff x size == zext size. |
| 1903 | if (TruncOp.getValueType() > VT) |
| 1904 | return DAG.getNode(ISD::TRUNCATE, VT, TruncOp); |
| 1905 | return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp); |
| 1906 | } |
Chris Lattner | 0e4b922 | 2006-09-21 06:40:43 +0000 | [diff] [blame] | 1907 | |
| 1908 | // fold (aext (and (trunc x), cst)) -> (and x, cst). |
| 1909 | if (N0.getOpcode() == ISD::AND && |
| 1910 | N0.getOperand(0).getOpcode() == ISD::TRUNCATE && |
| 1911 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 1912 | SDOperand X = N0.getOperand(0).getOperand(0); |
| 1913 | if (X.getValueType() < VT) { |
| 1914 | X = DAG.getNode(ISD::ANY_EXTEND, VT, X); |
| 1915 | } else if (X.getValueType() > VT) { |
| 1916 | X = DAG.getNode(ISD::TRUNCATE, VT, X); |
| 1917 | } |
| 1918 | uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
| 1919 | return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT)); |
| 1920 | } |
| 1921 | |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1922 | // fold (aext (load x)) -> (aext (truncate (extload x))) |
| 1923 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() && |
| 1924 | (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) { |
| 1925 | SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0), |
| 1926 | N0.getOperand(1), N0.getOperand(2), |
| 1927 | N0.getValueType()); |
| 1928 | CombineTo(N, ExtLoad); |
| 1929 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1930 | ExtLoad.getValue(1)); |
| 1931 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
| 1932 | } |
| 1933 | |
| 1934 | // fold (aext (zextload x)) -> (aext (truncate (zextload x))) |
| 1935 | // fold (aext (sextload x)) -> (aext (truncate (sextload x))) |
| 1936 | // fold (aext ( extload x)) -> (aext (truncate (extload x))) |
| 1937 | if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD || |
| 1938 | N0.getOpcode() == ISD::SEXTLOAD) && |
| 1939 | N0.hasOneUse()) { |
Nate Begeman | 5c74268 | 2006-05-08 01:35:01 +0000 | [diff] [blame] | 1940 | MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT(); |
| 1941 | SDOperand ExtLoad = DAG.getExtLoad(N0.getOpcode(), VT, N0.getOperand(0), |
| 1942 | N0.getOperand(1), N0.getOperand(2), EVT); |
Chris Lattner | 5ffc066 | 2006-05-05 05:58:59 +0000 | [diff] [blame] | 1943 | CombineTo(N, ExtLoad); |
| 1944 | CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad), |
| 1945 | ExtLoad.getValue(1)); |
| 1946 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
| 1947 | } |
| 1948 | return SDOperand(); |
| 1949 | } |
| 1950 | |
| 1951 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1952 | SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1953 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1954 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1955 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1956 | MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT(); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 1957 | unsigned EVTBits = MVT::getSizeInBits(EVT); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1958 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 1959 | // fold (sext_in_reg c1) -> c1 |
Chris Lattner | eaeda56 | 2006-05-08 20:59:41 +0000 | [diff] [blame] | 1960 | if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF) |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 1961 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1); |
Chris Lattner | ee4ea92 | 2006-05-06 09:30:03 +0000 | [diff] [blame] | 1962 | |
Chris Lattner | 541a24f | 2006-05-06 22:43:44 +0000 | [diff] [blame] | 1963 | // If the input is already sign extended, just drop the extension. |
Chris Lattner | ee4ea92 | 2006-05-06 09:30:03 +0000 | [diff] [blame] | 1964 | if (TLI.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1) |
| 1965 | return N0; |
| 1966 | |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1967 | // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2 |
| 1968 | if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 1969 | EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) { |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 1970 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 1971 | } |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 1972 | |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 1973 | // 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] | 1974 | if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1))) |
Nate Begeman | de99629 | 2006-02-03 22:24:05 +0000 | [diff] [blame] | 1975 | return DAG.getZeroExtendInReg(N0, EVT); |
Chris Lattner | 4b37e87 | 2006-05-08 21:18:59 +0000 | [diff] [blame] | 1976 | |
| 1977 | // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24 |
| 1978 | // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible. |
| 1979 | // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above. |
| 1980 | if (N0.getOpcode() == ISD::SRL) { |
| 1981 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1))) |
| 1982 | if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) { |
| 1983 | // We can turn this into an SRA iff the input to the SRL is already sign |
| 1984 | // extended enough. |
| 1985 | unsigned InSignBits = TLI.ComputeNumSignBits(N0.getOperand(0)); |
| 1986 | if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits) |
| 1987 | return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1)); |
| 1988 | } |
| 1989 | } |
| 1990 | |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1991 | // fold (sext_inreg (extload x)) -> (sextload x) |
| 1992 | if (N0.getOpcode() == ISD::EXTLOAD && |
| 1993 | EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() && |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1994 | (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 1995 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 1996 | N0.getOperand(1), N0.getOperand(2), |
| 1997 | EVT); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 1998 | CombineTo(N, ExtLoad); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 1999 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 2000 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2001 | } |
| 2002 | // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 2003 | if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() && |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2004 | EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() && |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2005 | (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) { |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2006 | SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0), |
| 2007 | N0.getOperand(1), N0.getOperand(2), |
| 2008 | EVT); |
Chris Lattner | d477184 | 2005-12-14 19:25:30 +0000 | [diff] [blame] | 2009 | CombineTo(N, ExtLoad); |
Nate Begeman | bfd65a0 | 2005-10-13 18:34:58 +0000 | [diff] [blame] | 2010 | CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 2011 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | ded4963 | 2005-10-13 03:11:28 +0000 | [diff] [blame] | 2012 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2013 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2016 | SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2017 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2018 | MVT::ValueType VT = N->getValueType(0); |
| 2019 | |
| 2020 | // noop truncate |
| 2021 | if (N0.getValueType() == N->getValueType(0)) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2022 | return N0; |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2023 | // fold (truncate c1) -> c1 |
Chris Lattner | 310b578 | 2006-05-06 23:06:26 +0000 | [diff] [blame] | 2024 | if (isa<ConstantSDNode>(N0)) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2025 | return DAG.getNode(ISD::TRUNCATE, VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2026 | // fold (truncate (truncate x)) -> (truncate x) |
| 2027 | if (N0.getOpcode() == ISD::TRUNCATE) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2028 | return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2029 | // fold (truncate (ext x)) -> (ext x) or (truncate x) or x |
Chris Lattner | b72773b | 2006-05-05 22:56:26 +0000 | [diff] [blame] | 2030 | if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND|| |
| 2031 | N0.getOpcode() == ISD::ANY_EXTEND) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2032 | if (N0.getValueType() < VT) |
| 2033 | // 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] | 2034 | return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2035 | else if (N0.getValueType() > VT) |
| 2036 | // 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] | 2037 | return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0)); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2038 | else |
| 2039 | // if the source and dest are the same type, we can drop both the extend |
| 2040 | // and the truncate |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2041 | return N0.getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2042 | } |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 2043 | // fold (truncate (load x)) -> (smaller load x) |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 2044 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) { |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 2045 | assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) && |
| 2046 | "Cannot truncate to larger type!"); |
| 2047 | MVT::ValueType PtrType = N0.getOperand(1).getValueType(); |
Nate Begeman | 765784a | 2005-10-12 23:18:53 +0000 | [diff] [blame] | 2048 | // For big endian targets, we need to add an offset to the pointer to load |
| 2049 | // the correct bytes. For little endian systems, we merely need to read |
| 2050 | // fewer bytes from the same pointer. |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 2051 | uint64_t PtrOff = |
| 2052 | (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8; |
Nate Begeman | 765784a | 2005-10-12 23:18:53 +0000 | [diff] [blame] | 2053 | SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) : |
| 2054 | DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1), |
| 2055 | DAG.getConstant(PtrOff, PtrType)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2056 | AddToWorkList(NewPtr.Val); |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 2057 | SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2058 | AddToWorkList(N); |
Chris Lattner | 24edbb7 | 2005-10-13 22:10:05 +0000 | [diff] [blame] | 2059 | CombineTo(N0.Val, Load, Load.getValue(1)); |
Chris Lattner | fedced7 | 2006-04-20 23:55:59 +0000 | [diff] [blame] | 2060 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
Nate Begeman | 3df4d52 | 2005-10-12 20:40:40 +0000 | [diff] [blame] | 2061 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2062 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 2065 | SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) { |
| 2066 | SDOperand N0 = N->getOperand(0); |
| 2067 | MVT::ValueType VT = N->getValueType(0); |
| 2068 | |
| 2069 | // If the input is a constant, let getNode() fold it. |
| 2070 | if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) { |
| 2071 | SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0); |
| 2072 | if (Res.Val != N) return Res; |
| 2073 | } |
| 2074 | |
Chris Lattner | c8547d8 | 2005-12-23 05:37:50 +0000 | [diff] [blame] | 2075 | if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2) |
| 2076 | return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0)); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2077 | |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 2078 | // fold (conv (load x)) -> (load (conv*)x) |
Chris Lattner | bf40c4b | 2006-01-15 18:58:59 +0000 | [diff] [blame] | 2079 | // FIXME: These xforms need to know that the resultant load doesn't need a |
| 2080 | // higher alignment than the original! |
| 2081 | if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) { |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 2082 | SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1), |
| 2083 | N0.getOperand(2)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 2084 | AddToWorkList(N); |
Chris Lattner | 5710410 | 2005-12-23 05:44:41 +0000 | [diff] [blame] | 2085 | CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load), |
| 2086 | Load.getValue(1)); |
| 2087 | return Load; |
| 2088 | } |
| 2089 | |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 2090 | return SDOperand(); |
| 2091 | } |
| 2092 | |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2093 | SDOperand DAGCombiner::visitVBIT_CONVERT(SDNode *N) { |
| 2094 | SDOperand N0 = N->getOperand(0); |
| 2095 | MVT::ValueType VT = N->getValueType(0); |
| 2096 | |
| 2097 | // If the input is a VBUILD_VECTOR with all constant elements, fold this now. |
| 2098 | // First check to see if this is all constant. |
| 2099 | if (N0.getOpcode() == ISD::VBUILD_VECTOR && N0.Val->hasOneUse() && |
| 2100 | VT == MVT::Vector) { |
| 2101 | bool isSimple = true; |
| 2102 | for (unsigned i = 0, e = N0.getNumOperands()-2; i != e; ++i) |
| 2103 | if (N0.getOperand(i).getOpcode() != ISD::UNDEF && |
| 2104 | N0.getOperand(i).getOpcode() != ISD::Constant && |
| 2105 | N0.getOperand(i).getOpcode() != ISD::ConstantFP) { |
| 2106 | isSimple = false; |
| 2107 | break; |
| 2108 | } |
| 2109 | |
Chris Lattner | 97c2073 | 2006-04-03 17:29:28 +0000 | [diff] [blame] | 2110 | MVT::ValueType DestEltVT = cast<VTSDNode>(N->getOperand(2))->getVT(); |
| 2111 | if (isSimple && !MVT::isVector(DestEltVT)) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2112 | return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(N0.Val, DestEltVT); |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | return SDOperand(); |
| 2117 | } |
| 2118 | |
| 2119 | /// ConstantFoldVBIT_CONVERTofVBUILD_VECTOR - We know that BV is a vbuild_vector |
| 2120 | /// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the |
| 2121 | /// destination element value type. |
| 2122 | SDOperand DAGCombiner:: |
| 2123 | ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) { |
| 2124 | MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType(); |
| 2125 | |
| 2126 | // If this is already the right type, we're done. |
| 2127 | if (SrcEltVT == DstEltVT) return SDOperand(BV, 0); |
| 2128 | |
| 2129 | unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT); |
| 2130 | unsigned DstBitSize = MVT::getSizeInBits(DstEltVT); |
| 2131 | |
| 2132 | // If this is a conversion of N elements of one type to N elements of another |
| 2133 | // type, convert each element. This handles FP<->INT cases. |
| 2134 | if (SrcBitSize == DstBitSize) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2135 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 2136 | for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) { |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2137 | Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i))); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 2138 | AddToWorkList(Ops.back().Val); |
| 2139 | } |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2140 | Ops.push_back(*(BV->op_end()-2)); // Add num elements. |
| 2141 | Ops.push_back(DAG.getValueType(DstEltVT)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2142 | return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | // Otherwise, we're growing or shrinking the elements. To avoid having to |
| 2146 | // handle annoying details of growing/shrinking FP values, we convert them to |
| 2147 | // int first. |
| 2148 | if (MVT::isFloatingPoint(SrcEltVT)) { |
| 2149 | // Convert the input float vector to a int vector where the elements are the |
| 2150 | // same sizes. |
| 2151 | assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!"); |
| 2152 | MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2153 | BV = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, IntVT).Val; |
| 2154 | SrcEltVT = IntVT; |
| 2155 | } |
| 2156 | |
| 2157 | // Now we know the input is an integer vector. If the output is a FP type, |
| 2158 | // convert to integer first, then to FP of the right size. |
| 2159 | if (MVT::isFloatingPoint(DstEltVT)) { |
| 2160 | assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!"); |
| 2161 | MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2162 | SDNode *Tmp = ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(BV, TmpVT).Val; |
| 2163 | |
| 2164 | // Next, convert to FP elements of the same size. |
| 2165 | return ConstantFoldVBIT_CONVERTofVBUILD_VECTOR(Tmp, DstEltVT); |
| 2166 | } |
| 2167 | |
| 2168 | // Okay, we know the src/dst types are both integers of differing types. |
| 2169 | // Handling growing first. |
| 2170 | assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT)); |
| 2171 | if (SrcBitSize < DstBitSize) { |
| 2172 | unsigned NumInputsPerOutput = DstBitSize/SrcBitSize; |
| 2173 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2174 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2175 | for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; |
| 2176 | i += NumInputsPerOutput) { |
| 2177 | bool isLE = TLI.isLittleEndian(); |
| 2178 | uint64_t NewBits = 0; |
| 2179 | bool EltIsUndef = true; |
| 2180 | for (unsigned j = 0; j != NumInputsPerOutput; ++j) { |
| 2181 | // Shift the previously computed bits over. |
| 2182 | NewBits <<= SrcBitSize; |
| 2183 | SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j)); |
| 2184 | if (Op.getOpcode() == ISD::UNDEF) continue; |
| 2185 | EltIsUndef = false; |
| 2186 | |
| 2187 | NewBits |= cast<ConstantSDNode>(Op)->getValue(); |
| 2188 | } |
| 2189 | |
| 2190 | if (EltIsUndef) |
| 2191 | Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT)); |
| 2192 | else |
| 2193 | Ops.push_back(DAG.getConstant(NewBits, DstEltVT)); |
| 2194 | } |
| 2195 | |
| 2196 | Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements. |
| 2197 | Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2198 | return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | // Finally, this must be the case where we are shrinking elements: each input |
| 2202 | // turns into multiple outputs. |
| 2203 | unsigned NumOutputsPerInput = SrcBitSize/DstBitSize; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2204 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2205 | for (unsigned i = 0, e = BV->getNumOperands()-2; i != e; ++i) { |
| 2206 | if (BV->getOperand(i).getOpcode() == ISD::UNDEF) { |
| 2207 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) |
| 2208 | Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT)); |
| 2209 | continue; |
| 2210 | } |
| 2211 | uint64_t OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getValue(); |
| 2212 | |
| 2213 | for (unsigned j = 0; j != NumOutputsPerInput; ++j) { |
| 2214 | unsigned ThisVal = OpVal & ((1ULL << DstBitSize)-1); |
| 2215 | OpVal >>= DstBitSize; |
| 2216 | Ops.push_back(DAG.getConstant(ThisVal, DstEltVT)); |
| 2217 | } |
| 2218 | |
| 2219 | // For big endian targets, swap the order of the pieces of each element. |
| 2220 | if (!TLI.isLittleEndian()) |
| 2221 | std::reverse(Ops.end()-NumOutputsPerInput, Ops.end()); |
| 2222 | } |
| 2223 | Ops.push_back(DAG.getConstant(Ops.size(), MVT::i32)); // Add num elements. |
| 2224 | Ops.push_back(DAG.getValueType(DstEltVT)); // Add element size. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2225 | return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size()); |
Chris Lattner | 6258fb2 | 2006-04-02 02:53:43 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | |
| 2229 | |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2230 | SDOperand DAGCombiner::visitFADD(SDNode *N) { |
| 2231 | SDOperand N0 = N->getOperand(0); |
| 2232 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2233 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2234 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2235 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2236 | |
| 2237 | // fold (fadd c1, c2) -> c1+c2 |
| 2238 | if (N0CFP && N1CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2239 | return DAG.getNode(ISD::FADD, VT, N0, N1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2240 | // canonicalize constant to RHS |
| 2241 | if (N0CFP && !N1CFP) |
| 2242 | return DAG.getNode(ISD::FADD, VT, N1, N0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2243 | // fold (A + (-B)) -> A-B |
| 2244 | if (N1.getOpcode() == ISD::FNEG) |
| 2245 | return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2246 | // fold ((-A) + B) -> B-A |
| 2247 | if (N0.getOpcode() == ISD::FNEG) |
| 2248 | return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2249 | return SDOperand(); |
| 2250 | } |
| 2251 | |
| 2252 | SDOperand DAGCombiner::visitFSUB(SDNode *N) { |
| 2253 | SDOperand N0 = N->getOperand(0); |
| 2254 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2255 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2256 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2257 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2258 | |
| 2259 | // fold (fsub c1, c2) -> c1-c2 |
| 2260 | if (N0CFP && N1CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2261 | return DAG.getNode(ISD::FSUB, VT, N0, N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2262 | // fold (A-(-B)) -> A+B |
| 2263 | if (N1.getOpcode() == ISD::FNEG) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2264 | return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0)); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2265 | return SDOperand(); |
| 2266 | } |
| 2267 | |
| 2268 | SDOperand DAGCombiner::visitFMUL(SDNode *N) { |
| 2269 | SDOperand N0 = N->getOperand(0); |
| 2270 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 2271 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2272 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2273 | MVT::ValueType VT = N->getValueType(0); |
| 2274 | |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 2275 | // fold (fmul c1, c2) -> c1*c2 |
| 2276 | if (N0CFP && N1CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2277 | return DAG.getNode(ISD::FMUL, VT, N0, N1); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 2278 | // canonicalize constant to RHS |
Nate Begeman | a0e221d | 2005-10-18 00:28:13 +0000 | [diff] [blame] | 2279 | if (N0CFP && !N1CFP) |
| 2280 | return DAG.getNode(ISD::FMUL, VT, N1, N0); |
Nate Begeman | 11af4ea | 2005-10-17 20:40:11 +0000 | [diff] [blame] | 2281 | // fold (fmul X, 2.0) -> (fadd X, X) |
| 2282 | if (N1CFP && N1CFP->isExactlyValue(+2.0)) |
| 2283 | return DAG.getNode(ISD::FADD, VT, N0, N0); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2284 | return SDOperand(); |
| 2285 | } |
| 2286 | |
| 2287 | SDOperand DAGCombiner::visitFDIV(SDNode *N) { |
| 2288 | SDOperand N0 = N->getOperand(0); |
| 2289 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2290 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2291 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2292 | MVT::ValueType VT = N->getValueType(0); |
| 2293 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2294 | // fold (fdiv c1, c2) -> c1/c2 |
| 2295 | if (N0CFP && N1CFP) |
| 2296 | return DAG.getNode(ISD::FDIV, VT, N0, N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2297 | return SDOperand(); |
| 2298 | } |
| 2299 | |
| 2300 | SDOperand DAGCombiner::visitFREM(SDNode *N) { |
| 2301 | SDOperand N0 = N->getOperand(0); |
| 2302 | SDOperand N1 = N->getOperand(1); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2303 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2304 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2305 | MVT::ValueType VT = N->getValueType(0); |
| 2306 | |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2307 | // fold (frem c1, c2) -> fmod(c1,c2) |
| 2308 | if (N0CFP && N1CFP) |
| 2309 | return DAG.getNode(ISD::FREM, VT, N0, N1); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2310 | return SDOperand(); |
| 2311 | } |
| 2312 | |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 2313 | SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) { |
| 2314 | SDOperand N0 = N->getOperand(0); |
| 2315 | SDOperand N1 = N->getOperand(1); |
| 2316 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2317 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| 2318 | MVT::ValueType VT = N->getValueType(0); |
| 2319 | |
| 2320 | if (N0CFP && N1CFP) // Constant fold |
| 2321 | return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1); |
| 2322 | |
| 2323 | if (N1CFP) { |
| 2324 | // copysign(x, c1) -> fabs(x) iff ispos(c1) |
| 2325 | // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1) |
| 2326 | union { |
| 2327 | double d; |
| 2328 | int64_t i; |
| 2329 | } u; |
| 2330 | u.d = N1CFP->getValue(); |
| 2331 | if (u.i >= 0) |
| 2332 | return DAG.getNode(ISD::FABS, VT, N0); |
| 2333 | else |
| 2334 | return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0)); |
| 2335 | } |
| 2336 | |
| 2337 | // copysign(fabs(x), y) -> copysign(x, y) |
| 2338 | // copysign(fneg(x), y) -> copysign(x, y) |
| 2339 | // copysign(copysign(x,z), y) -> copysign(x, y) |
| 2340 | if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG || |
| 2341 | N0.getOpcode() == ISD::FCOPYSIGN) |
| 2342 | return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1); |
| 2343 | |
| 2344 | // copysign(x, abs(y)) -> abs(x) |
| 2345 | if (N1.getOpcode() == ISD::FABS) |
| 2346 | return DAG.getNode(ISD::FABS, VT, N0); |
| 2347 | |
| 2348 | // copysign(x, copysign(y,z)) -> copysign(x, z) |
| 2349 | if (N1.getOpcode() == ISD::FCOPYSIGN) |
| 2350 | return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1)); |
| 2351 | |
| 2352 | // copysign(x, fp_extend(y)) -> copysign(x, y) |
| 2353 | // copysign(x, fp_round(y)) -> copysign(x, y) |
| 2354 | if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND) |
| 2355 | return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0)); |
| 2356 | |
| 2357 | return SDOperand(); |
| 2358 | } |
| 2359 | |
| 2360 | |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2361 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2362 | SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2363 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2364 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2365 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2366 | |
| 2367 | // fold (sint_to_fp c1) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2368 | if (N0C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2369 | return DAG.getNode(ISD::SINT_TO_FP, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2370 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2373 | SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2374 | SDOperand N0 = N->getOperand(0); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2375 | ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2376 | MVT::ValueType VT = N->getValueType(0); |
| 2377 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2378 | // fold (uint_to_fp c1) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2379 | if (N0C) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2380 | return DAG.getNode(ISD::UINT_TO_FP, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2381 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2384 | SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2385 | SDOperand N0 = N->getOperand(0); |
| 2386 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2387 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2388 | |
| 2389 | // fold (fp_to_sint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2390 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2391 | return DAG.getNode(ISD::FP_TO_SINT, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2392 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2395 | SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2396 | SDOperand N0 = N->getOperand(0); |
| 2397 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2398 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2399 | |
| 2400 | // fold (fp_to_uint c1fp) -> c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2401 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2402 | return DAG.getNode(ISD::FP_TO_UINT, VT, N0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2403 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2406 | SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2407 | SDOperand N0 = N->getOperand(0); |
| 2408 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2409 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2410 | |
| 2411 | // fold (fp_round c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2412 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2413 | return DAG.getNode(ISD::FP_ROUND, VT, N0); |
Chris Lattner | 79dbea5 | 2006-03-13 06:26:26 +0000 | [diff] [blame] | 2414 | |
| 2415 | // fold (fp_round (fp_extend x)) -> x |
| 2416 | if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType()) |
| 2417 | return N0.getOperand(0); |
| 2418 | |
| 2419 | // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y) |
| 2420 | if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) { |
| 2421 | SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0)); |
| 2422 | AddToWorkList(Tmp.Val); |
| 2423 | return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1)); |
| 2424 | } |
| 2425 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2426 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2429 | SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2430 | SDOperand N0 = N->getOperand(0); |
| 2431 | MVT::ValueType VT = N->getValueType(0); |
| 2432 | MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT(); |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2433 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2434 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2435 | // fold (fp_round_inreg c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2436 | if (N0CFP) { |
| 2437 | SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2438 | return DAG.getNode(ISD::FP_EXTEND, VT, Round); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2439 | } |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2440 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2443 | SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2444 | SDOperand N0 = N->getOperand(0); |
| 2445 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2446 | MVT::ValueType VT = N->getValueType(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2447 | |
| 2448 | // fold (fp_extend c1fp) -> c1fp |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2449 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2450 | return DAG.getNode(ISD::FP_EXTEND, VT, N0); |
Chris Lattner | e564dbb | 2006-05-05 21:34:35 +0000 | [diff] [blame] | 2451 | |
| 2452 | // fold (fpext (load x)) -> (fpext (fpround (extload x))) |
| 2453 | if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() && |
| 2454 | (!AfterLegalize||TLI.isOperationLegal(ISD::EXTLOAD, N0.getValueType()))) { |
| 2455 | SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, N0.getOperand(0), |
| 2456 | N0.getOperand(1), N0.getOperand(2), |
| 2457 | N0.getValueType()); |
| 2458 | CombineTo(N, ExtLoad); |
| 2459 | CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad), |
| 2460 | ExtLoad.getValue(1)); |
| 2461 | return SDOperand(N, 0); // Return N so it doesn't get rechecked! |
| 2462 | } |
| 2463 | |
| 2464 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2465 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2468 | SDOperand DAGCombiner::visitFNEG(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2469 | SDOperand N0 = N->getOperand(0); |
| 2470 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2471 | MVT::ValueType VT = N->getValueType(0); |
| 2472 | |
| 2473 | // fold (fneg c1) -> -c1 |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2474 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2475 | return DAG.getNode(ISD::FNEG, VT, N0); |
| 2476 | // fold (fneg (sub x, y)) -> (sub y, x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 2477 | if (N0.getOpcode() == ISD::SUB) |
| 2478 | return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0)); |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2479 | // fold (fneg (fneg x)) -> x |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 2480 | if (N0.getOpcode() == ISD::FNEG) |
| 2481 | return N0.getOperand(0); |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2482 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2485 | SDOperand DAGCombiner::visitFABS(SDNode *N) { |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2486 | SDOperand N0 = N->getOperand(0); |
| 2487 | ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0); |
| 2488 | MVT::ValueType VT = N->getValueType(0); |
| 2489 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2490 | // fold (fabs c1) -> fabs(c1) |
Nate Begeman | 646d7e2 | 2005-09-02 21:18:40 +0000 | [diff] [blame] | 2491 | if (N0CFP) |
Nate Begeman | a148d98 | 2006-01-18 22:35:16 +0000 | [diff] [blame] | 2492 | return DAG.getNode(ISD::FABS, VT, N0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2493 | // fold (fabs (fabs x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 2494 | if (N0.getOpcode() == ISD::FABS) |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2495 | return N->getOperand(0); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2496 | // fold (fabs (fneg x)) -> (fabs x) |
Chris Lattner | 12d8303 | 2006-03-05 05:30:57 +0000 | [diff] [blame] | 2497 | // fold (fabs (fcopysign x, y)) -> (fabs x) |
| 2498 | if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN) |
| 2499 | return DAG.getNode(ISD::FABS, VT, N0.getOperand(0)); |
| 2500 | |
Nate Begeman | 83e75ec | 2005-09-06 04:43:02 +0000 | [diff] [blame] | 2501 | return SDOperand(); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2504 | SDOperand DAGCombiner::visitBRCOND(SDNode *N) { |
| 2505 | SDOperand Chain = N->getOperand(0); |
| 2506 | SDOperand N1 = N->getOperand(1); |
| 2507 | SDOperand N2 = N->getOperand(2); |
| 2508 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 2509 | |
| 2510 | // never taken branch, fold to chain |
| 2511 | if (N1C && N1C->isNullValue()) |
| 2512 | return Chain; |
| 2513 | // unconditional branch |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 2514 | if (N1C && N1C->getValue() == 1) |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2515 | return DAG.getNode(ISD::BR, MVT::Other, Chain, N2); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2516 | // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal |
| 2517 | // on the target. |
| 2518 | if (N1.getOpcode() == ISD::SETCC && |
| 2519 | TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) { |
| 2520 | return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2), |
| 2521 | N1.getOperand(0), N1.getOperand(1), N2); |
| 2522 | } |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2523 | return SDOperand(); |
| 2524 | } |
| 2525 | |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 2526 | // Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB. |
| 2527 | // |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2528 | SDOperand DAGCombiner::visitBR_CC(SDNode *N) { |
Chris Lattner | 3ea0b47 | 2005-10-05 06:47:48 +0000 | [diff] [blame] | 2529 | CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1)); |
| 2530 | SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3); |
| 2531 | |
| 2532 | // Use SimplifySetCC to simplify SETCC's. |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 2533 | SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false); |
| 2534 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val); |
| 2535 | |
| 2536 | // fold br_cc true, dest -> br dest (unconditional branch) |
| 2537 | if (SCCC && SCCC->getValue()) |
| 2538 | return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0), |
| 2539 | N->getOperand(4)); |
| 2540 | // fold br_cc false, dest -> unconditional fall through |
| 2541 | if (SCCC && SCCC->isNullValue()) |
| 2542 | return N->getOperand(0); |
| 2543 | // fold to a simpler setcc |
| 2544 | if (Simp.Val && Simp.getOpcode() == ISD::SETCC) |
| 2545 | return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0), |
| 2546 | Simp.getOperand(2), Simp.getOperand(0), |
| 2547 | Simp.getOperand(1), N->getOperand(4)); |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 2548 | return SDOperand(); |
| 2549 | } |
| 2550 | |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 2551 | SDOperand DAGCombiner::visitLOAD(SDNode *N) { |
| 2552 | SDOperand Chain = N->getOperand(0); |
| 2553 | SDOperand Ptr = N->getOperand(1); |
| 2554 | SDOperand SrcValue = N->getOperand(2); |
Chris Lattner | e4b9539 | 2006-03-31 18:06:18 +0000 | [diff] [blame] | 2555 | |
| 2556 | // If there are no uses of the loaded value, change uses of the chain value |
| 2557 | // into uses of the chain input (i.e. delete the dead load). |
| 2558 | if (N->hasNUsesOfValue(0, 0)) |
| 2559 | return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain); |
Chris Lattner | 01a2202 | 2005-10-10 22:04:48 +0000 | [diff] [blame] | 2560 | |
| 2561 | // If this load is directly stored, replace the load value with the stored |
| 2562 | // value. |
| 2563 | // TODO: Handle store large -> read small portion. |
| 2564 | // TODO: Handle TRUNCSTORE/EXTLOAD |
| 2565 | if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr && |
| 2566 | Chain.getOperand(1).getValueType() == N->getValueType(0)) |
| 2567 | return CombineTo(N, Chain.getOperand(1), Chain); |
| 2568 | |
| 2569 | return SDOperand(); |
| 2570 | } |
| 2571 | |
Chris Lattner | 29cd7db | 2006-03-31 18:10:41 +0000 | [diff] [blame] | 2572 | /// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD. |
| 2573 | SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) { |
| 2574 | SDOperand Chain = N->getOperand(0); |
| 2575 | SDOperand Ptr = N->getOperand(1); |
| 2576 | SDOperand SrcValue = N->getOperand(2); |
| 2577 | SDOperand EVT = N->getOperand(3); |
| 2578 | |
| 2579 | // If there are no uses of the loaded value, change uses of the chain value |
| 2580 | // into uses of the chain input (i.e. delete the dead load). |
| 2581 | if (N->hasNUsesOfValue(0, 0)) |
| 2582 | return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain); |
| 2583 | |
| 2584 | return SDOperand(); |
| 2585 | } |
| 2586 | |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 2587 | /// isNotAlias - Return true if we have definitive knowlege that the two |
| 2588 | /// addresses don't overlap. |
| 2589 | bool DAGCombiner::isNotAlias(SDOperand Ptr1, SDOperand Ptr2) { |
| 2590 | // Mind the flag. |
| 2591 | if (!CombinerAA) return false; |
| 2592 | |
Jim Laskey | 516b0ea | 2006-09-21 17:35:47 +0000 | [diff] [blame^] | 2593 | // If they are the same then they must be aliases. |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 2594 | if (Ptr1 == Ptr2) return false; |
| 2595 | |
Jim Laskey | 516b0ea | 2006-09-21 17:35:47 +0000 | [diff] [blame^] | 2596 | // If both operands are frame values (not the same location from above test) |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 2597 | // then they can't alias. |
| 2598 | FrameIndexSDNode *FI1 = dyn_cast<FrameIndexSDNode>(Ptr1); |
| 2599 | FrameIndexSDNode *FI2 = dyn_cast<FrameIndexSDNode>(Ptr2); |
Jim Laskey | 516b0ea | 2006-09-21 17:35:47 +0000 | [diff] [blame^] | 2600 | if (FI1 && FI2) { |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 2601 | return true; |
| 2602 | } |
| 2603 | |
| 2604 | // Otherwise we don't know and have to play it safe. |
| 2605 | return false; |
| 2606 | } |
| 2607 | |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2608 | SDOperand DAGCombiner::visitSTORE(SDNode *N) { |
| 2609 | SDOperand Chain = N->getOperand(0); |
| 2610 | SDOperand Value = N->getOperand(1); |
| 2611 | SDOperand Ptr = N->getOperand(2); |
| 2612 | SDOperand SrcValue = N->getOperand(3); |
| 2613 | |
| 2614 | // 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] | 2615 | if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr && |
Chris Lattner | fe7f046 | 2005-10-27 07:10:34 +0000 | [diff] [blame] | 2616 | Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ && |
| 2617 | // Make sure that these stores are the same value type: |
| 2618 | // FIXME: we really care that the second store is >= size of the first. |
| 2619 | Value.getValueType() == Chain.getOperand(1).getValueType()) { |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2620 | // Create a new store of Value that replaces both stores. |
| 2621 | SDNode *PrevStore = Chain.Val; |
Chris Lattner | 04ecf6d | 2005-10-10 23:00:08 +0000 | [diff] [blame] | 2622 | if (PrevStore->getOperand(1) == Value) // Same value multiply stored. |
| 2623 | return Chain; |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2624 | SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other, |
| 2625 | PrevStore->getOperand(0), Value, Ptr, |
| 2626 | SrcValue); |
Chris Lattner | 04ecf6d | 2005-10-10 23:00:08 +0000 | [diff] [blame] | 2627 | CombineTo(N, NewStore); // Nuke this store. |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2628 | CombineTo(PrevStore, NewStore); // Nuke the previous store. |
Chris Lattner | 04ecf6d | 2005-10-10 23:00:08 +0000 | [diff] [blame] | 2629 | return SDOperand(N, 0); |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Chris Lattner | c33baaa | 2005-12-23 05:48:07 +0000 | [diff] [blame] | 2632 | // 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] | 2633 | // FIXME: This needs to know that the resultant store does not need a |
| 2634 | // higher alignment than the original. |
| 2635 | if (0 && Value.getOpcode() == ISD::BIT_CONVERT) |
Chris Lattner | c33baaa | 2005-12-23 05:48:07 +0000 | [diff] [blame] | 2636 | return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0), |
| 2637 | Ptr, SrcValue); |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 2638 | |
| 2639 | // If the previous store is not an alias then break artificial chain. |
| 2640 | if (Chain.getOpcode() == ISD::STORE && isNotAlias(Ptr, Chain.getOperand(2))) { |
| 2641 | // Replace the chain to void dependency. |
| 2642 | SDNode *PrevStore = Chain.Val; |
| 2643 | SDOperand ReplStore = DAG.getNode(ISD::STORE, MVT::Other, |
| 2644 | PrevStore->getOperand(0), Value, Ptr, |
| 2645 | SrcValue); |
| 2646 | // Create token to keep both stores around. |
| 2647 | SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 2648 | Chain, ReplStore); |
| 2649 | // Replace uses with token. |
Jim Laskey | 516b0ea | 2006-09-21 17:35:47 +0000 | [diff] [blame^] | 2650 | return Token; |
Jim Laskey | d1aed7a | 2006-09-21 16:28:59 +0000 | [diff] [blame] | 2651 | } |
Chris Lattner | c33baaa | 2005-12-23 05:48:07 +0000 | [diff] [blame] | 2652 | |
Chris Lattner | 87514ca | 2005-10-10 22:31:19 +0000 | [diff] [blame] | 2653 | return SDOperand(); |
| 2654 | } |
| 2655 | |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 2656 | SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) { |
| 2657 | SDOperand InVec = N->getOperand(0); |
| 2658 | SDOperand InVal = N->getOperand(1); |
| 2659 | SDOperand EltNo = N->getOperand(2); |
| 2660 | |
| 2661 | // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new |
| 2662 | // vector with the inserted element. |
| 2663 | if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) { |
| 2664 | unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue(); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2665 | SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 2666 | if (Elt < Ops.size()) |
| 2667 | Ops[Elt] = InVal; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2668 | return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), |
| 2669 | &Ops[0], Ops.size()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | return SDOperand(); |
| 2673 | } |
| 2674 | |
| 2675 | SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) { |
| 2676 | SDOperand InVec = N->getOperand(0); |
| 2677 | SDOperand InVal = N->getOperand(1); |
| 2678 | SDOperand EltNo = N->getOperand(2); |
| 2679 | SDOperand NumElts = N->getOperand(3); |
| 2680 | SDOperand EltType = N->getOperand(4); |
| 2681 | |
| 2682 | // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new |
| 2683 | // vector with the inserted element. |
| 2684 | if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) { |
| 2685 | unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue(); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2686 | SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 2687 | if (Elt < Ops.size()-2) |
| 2688 | Ops[Elt] = InVal; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2689 | return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), |
| 2690 | &Ops[0], Ops.size()); |
Chris Lattner | ca24244 | 2006-03-19 01:27:56 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | return SDOperand(); |
| 2694 | } |
| 2695 | |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 2696 | SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) { |
| 2697 | unsigned NumInScalars = N->getNumOperands()-2; |
| 2698 | SDOperand NumElts = N->getOperand(NumInScalars); |
| 2699 | SDOperand EltType = N->getOperand(NumInScalars+1); |
| 2700 | |
| 2701 | // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT |
| 2702 | // operations. If so, and if the EXTRACT_ELT vector inputs come from at most |
| 2703 | // two distinct vectors, turn this into a shuffle node. |
| 2704 | SDOperand VecIn1, VecIn2; |
| 2705 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 2706 | // Ignore undef inputs. |
| 2707 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 2708 | |
| 2709 | // If this input is something other than a VEXTRACT_VECTOR_ELT with a |
| 2710 | // constant index, bail out. |
| 2711 | if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT || |
| 2712 | !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) { |
| 2713 | VecIn1 = VecIn2 = SDOperand(0, 0); |
| 2714 | break; |
| 2715 | } |
| 2716 | |
| 2717 | // If the input vector type disagrees with the result of the vbuild_vector, |
| 2718 | // we can't make a shuffle. |
| 2719 | SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0); |
| 2720 | if (*(ExtractedFromVec.Val->op_end()-2) != NumElts || |
| 2721 | *(ExtractedFromVec.Val->op_end()-1) != EltType) { |
| 2722 | VecIn1 = VecIn2 = SDOperand(0, 0); |
| 2723 | break; |
| 2724 | } |
| 2725 | |
| 2726 | // Otherwise, remember this. We allow up to two distinct input vectors. |
| 2727 | if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2) |
| 2728 | continue; |
| 2729 | |
| 2730 | if (VecIn1.Val == 0) { |
| 2731 | VecIn1 = ExtractedFromVec; |
| 2732 | } else if (VecIn2.Val == 0) { |
| 2733 | VecIn2 = ExtractedFromVec; |
| 2734 | } else { |
| 2735 | // Too many inputs. |
| 2736 | VecIn1 = VecIn2 = SDOperand(0, 0); |
| 2737 | break; |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | // If everything is good, we can make a shuffle operation. |
| 2742 | if (VecIn1.Val) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2743 | SmallVector<SDOperand, 8> BuildVecIndices; |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 2744 | for (unsigned i = 0; i != NumInScalars; ++i) { |
| 2745 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) { |
| 2746 | BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 2747 | continue; |
| 2748 | } |
| 2749 | |
| 2750 | SDOperand Extract = N->getOperand(i); |
| 2751 | |
| 2752 | // If extracting from the first vector, just use the index directly. |
| 2753 | if (Extract.getOperand(0) == VecIn1) { |
| 2754 | BuildVecIndices.push_back(Extract.getOperand(1)); |
| 2755 | continue; |
| 2756 | } |
| 2757 | |
| 2758 | // Otherwise, use InIdx + VecSize |
| 2759 | unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue(); |
| 2760 | BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32)); |
| 2761 | } |
| 2762 | |
| 2763 | // Add count and size info. |
| 2764 | BuildVecIndices.push_back(NumElts); |
| 2765 | BuildVecIndices.push_back(DAG.getValueType(MVT::i32)); |
| 2766 | |
| 2767 | // Return the new VVECTOR_SHUFFLE node. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2768 | SDOperand Ops[5]; |
| 2769 | Ops[0] = VecIn1; |
Chris Lattner | cef896e | 2006-03-28 22:19:47 +0000 | [diff] [blame] | 2770 | if (VecIn2.Val) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2771 | Ops[1] = VecIn2; |
Chris Lattner | cef896e | 2006-03-28 22:19:47 +0000 | [diff] [blame] | 2772 | } else { |
| 2773 | // Use an undef vbuild_vector as input for the second operand. |
| 2774 | std::vector<SDOperand> UnOps(NumInScalars, |
| 2775 | DAG.getNode(ISD::UNDEF, |
| 2776 | cast<VTSDNode>(EltType)->getVT())); |
| 2777 | UnOps.push_back(NumElts); |
| 2778 | UnOps.push_back(EltType); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2779 | Ops[1] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 2780 | &UnOps[0], UnOps.size()); |
| 2781 | AddToWorkList(Ops[1].Val); |
Chris Lattner | cef896e | 2006-03-28 22:19:47 +0000 | [diff] [blame] | 2782 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2783 | Ops[2] = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 2784 | &BuildVecIndices[0], BuildVecIndices.size()); |
| 2785 | Ops[3] = NumElts; |
| 2786 | Ops[4] = EltType; |
| 2787 | return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops, 5); |
Chris Lattner | d7648c8 | 2006-03-28 20:28:38 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | return SDOperand(); |
| 2791 | } |
| 2792 | |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2793 | SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) { |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 2794 | SDOperand ShufMask = N->getOperand(2); |
| 2795 | unsigned NumElts = ShufMask.getNumOperands(); |
| 2796 | |
| 2797 | // If the shuffle mask is an identity operation on the LHS, return the LHS. |
| 2798 | bool isIdentity = true; |
| 2799 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2800 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF && |
| 2801 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) { |
| 2802 | isIdentity = false; |
| 2803 | break; |
| 2804 | } |
| 2805 | } |
| 2806 | if (isIdentity) return N->getOperand(0); |
| 2807 | |
| 2808 | // If the shuffle mask is an identity operation on the RHS, return the RHS. |
| 2809 | isIdentity = true; |
| 2810 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2811 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF && |
| 2812 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) { |
| 2813 | isIdentity = false; |
| 2814 | break; |
| 2815 | } |
| 2816 | } |
| 2817 | if (isIdentity) return N->getOperand(1); |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2818 | |
| 2819 | // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not |
| 2820 | // needed at all. |
| 2821 | bool isUnary = true; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2822 | bool isSplat = true; |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2823 | int VecNum = -1; |
Reid Spencer | 9160a6a | 2006-07-25 20:44:41 +0000 | [diff] [blame] | 2824 | unsigned BaseIdx = 0; |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2825 | for (unsigned i = 0; i != NumElts; ++i) |
| 2826 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) { |
| 2827 | unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue(); |
| 2828 | int V = (Idx < NumElts) ? 0 : 1; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2829 | if (VecNum == -1) { |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2830 | VecNum = V; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2831 | BaseIdx = Idx; |
| 2832 | } else { |
| 2833 | if (BaseIdx != Idx) |
| 2834 | isSplat = false; |
| 2835 | if (VecNum != V) { |
| 2836 | isUnary = false; |
| 2837 | break; |
| 2838 | } |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | SDOperand N0 = N->getOperand(0); |
| 2843 | SDOperand N1 = N->getOperand(1); |
| 2844 | // Normalize unary shuffle so the RHS is undef. |
| 2845 | if (isUnary && VecNum == 1) |
| 2846 | std::swap(N0, N1); |
| 2847 | |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2848 | // If it is a splat, check if the argument vector is a build_vector with |
| 2849 | // all scalar elements the same. |
| 2850 | if (isSplat) { |
| 2851 | SDNode *V = N0.Val; |
| 2852 | if (V->getOpcode() == ISD::BIT_CONVERT) |
| 2853 | V = V->getOperand(0).Val; |
| 2854 | if (V->getOpcode() == ISD::BUILD_VECTOR) { |
| 2855 | unsigned NumElems = V->getNumOperands()-2; |
| 2856 | if (NumElems > BaseIdx) { |
| 2857 | SDOperand Base; |
| 2858 | bool AllSame = true; |
| 2859 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2860 | if (V->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 2861 | Base = V->getOperand(i); |
| 2862 | break; |
| 2863 | } |
| 2864 | } |
| 2865 | // Splat of <u, u, u, u>, return <u, u, u, u> |
| 2866 | if (!Base.Val) |
| 2867 | return N0; |
| 2868 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2869 | if (V->getOperand(i).getOpcode() != ISD::UNDEF && |
| 2870 | V->getOperand(i) != Base) { |
| 2871 | AllSame = false; |
| 2872 | break; |
| 2873 | } |
| 2874 | } |
| 2875 | // Splat of <x, x, x, x>, return <x, x, x, x> |
| 2876 | if (AllSame) |
| 2877 | return N0; |
| 2878 | } |
| 2879 | } |
| 2880 | } |
| 2881 | |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2882 | // If it is a unary or the LHS and the RHS are the same node, turn the RHS |
| 2883 | // into an undef. |
| 2884 | if (isUnary || N0 == N1) { |
| 2885 | if (N0.getOpcode() == ISD::UNDEF) |
Evan Cheng | c04766a | 2006-04-06 23:20:43 +0000 | [diff] [blame] | 2886 | return DAG.getNode(ISD::UNDEF, N->getValueType(0)); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2887 | // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the |
| 2888 | // first operand. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2889 | SmallVector<SDOperand, 8> MappedOps; |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2890 | for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) { |
Evan Cheng | c04766a | 2006-04-06 23:20:43 +0000 | [diff] [blame] | 2891 | if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF || |
| 2892 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) { |
| 2893 | MappedOps.push_back(ShufMask.getOperand(i)); |
| 2894 | } else { |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2895 | unsigned NewIdx = |
| 2896 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts; |
| 2897 | MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32)); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2898 | } |
| 2899 | } |
| 2900 | ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(), |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2901 | &MappedOps[0], MappedOps.size()); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 2902 | AddToWorkList(ShufMask.Val); |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2903 | return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0), |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2904 | N0, |
Chris Lattner | 66445d3 | 2006-03-28 22:11:53 +0000 | [diff] [blame] | 2905 | DAG.getNode(ISD::UNDEF, N->getValueType(0)), |
| 2906 | ShufMask); |
| 2907 | } |
| 2908 | |
| 2909 | return SDOperand(); |
| 2910 | } |
| 2911 | |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 2912 | SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) { |
| 2913 | SDOperand ShufMask = N->getOperand(2); |
| 2914 | unsigned NumElts = ShufMask.getNumOperands()-2; |
| 2915 | |
| 2916 | // If the shuffle mask is an identity operation on the LHS, return the LHS. |
| 2917 | bool isIdentity = true; |
| 2918 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2919 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF && |
| 2920 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) { |
| 2921 | isIdentity = false; |
| 2922 | break; |
| 2923 | } |
| 2924 | } |
| 2925 | if (isIdentity) return N->getOperand(0); |
| 2926 | |
| 2927 | // If the shuffle mask is an identity operation on the RHS, return the RHS. |
| 2928 | isIdentity = true; |
| 2929 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2930 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF && |
| 2931 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) { |
| 2932 | isIdentity = false; |
| 2933 | break; |
| 2934 | } |
| 2935 | } |
| 2936 | if (isIdentity) return N->getOperand(1); |
| 2937 | |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2938 | // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not |
| 2939 | // needed at all. |
| 2940 | bool isUnary = true; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2941 | bool isSplat = true; |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2942 | int VecNum = -1; |
Reid Spencer | 9160a6a | 2006-07-25 20:44:41 +0000 | [diff] [blame] | 2943 | unsigned BaseIdx = 0; |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2944 | for (unsigned i = 0; i != NumElts; ++i) |
| 2945 | if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) { |
| 2946 | unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue(); |
| 2947 | int V = (Idx < NumElts) ? 0 : 1; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2948 | if (VecNum == -1) { |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2949 | VecNum = V; |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2950 | BaseIdx = Idx; |
| 2951 | } else { |
| 2952 | if (BaseIdx != Idx) |
| 2953 | isSplat = false; |
| 2954 | if (VecNum != V) { |
| 2955 | isUnary = false; |
| 2956 | break; |
| 2957 | } |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 2958 | } |
| 2959 | } |
| 2960 | |
| 2961 | SDOperand N0 = N->getOperand(0); |
| 2962 | SDOperand N1 = N->getOperand(1); |
| 2963 | // Normalize unary shuffle so the RHS is undef. |
| 2964 | if (isUnary && VecNum == 1) |
| 2965 | std::swap(N0, N1); |
| 2966 | |
Evan Cheng | 917ec98 | 2006-07-21 08:25:53 +0000 | [diff] [blame] | 2967 | // If it is a splat, check if the argument vector is a build_vector with |
| 2968 | // all scalar elements the same. |
| 2969 | if (isSplat) { |
| 2970 | SDNode *V = N0.Val; |
| 2971 | if (V->getOpcode() == ISD::VBIT_CONVERT) |
| 2972 | V = V->getOperand(0).Val; |
| 2973 | if (V->getOpcode() == ISD::VBUILD_VECTOR) { |
| 2974 | unsigned NumElems = V->getNumOperands()-2; |
| 2975 | if (NumElems > BaseIdx) { |
| 2976 | SDOperand Base; |
| 2977 | bool AllSame = true; |
| 2978 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2979 | if (V->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 2980 | Base = V->getOperand(i); |
| 2981 | break; |
| 2982 | } |
| 2983 | } |
| 2984 | // Splat of <u, u, u, u>, return <u, u, u, u> |
| 2985 | if (!Base.Val) |
| 2986 | return N0; |
| 2987 | for (unsigned i = 0; i != NumElems; ++i) { |
| 2988 | if (V->getOperand(i).getOpcode() != ISD::UNDEF && |
| 2989 | V->getOperand(i) != Base) { |
| 2990 | AllSame = false; |
| 2991 | break; |
| 2992 | } |
| 2993 | } |
| 2994 | // Splat of <x, x, x, x>, return <x, x, x, x> |
| 2995 | if (AllSame) |
| 2996 | return N0; |
| 2997 | } |
| 2998 | } |
| 2999 | } |
| 3000 | |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 3001 | // If it is a unary or the LHS and the RHS are the same node, turn the RHS |
| 3002 | // into an undef. |
| 3003 | if (isUnary || N0 == N1) { |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 3004 | // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the |
| 3005 | // first operand. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3006 | SmallVector<SDOperand, 8> MappedOps; |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 3007 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3008 | if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF || |
| 3009 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) { |
| 3010 | MappedOps.push_back(ShufMask.getOperand(i)); |
| 3011 | } else { |
| 3012 | unsigned NewIdx = |
| 3013 | cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts; |
| 3014 | MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32)); |
| 3015 | } |
| 3016 | } |
| 3017 | // Add the type/#elts values. |
| 3018 | MappedOps.push_back(ShufMask.getOperand(NumElts)); |
| 3019 | MappedOps.push_back(ShufMask.getOperand(NumElts+1)); |
| 3020 | |
| 3021 | ShufMask = DAG.getNode(ISD::VBUILD_VECTOR, ShufMask.getValueType(), |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3022 | &MappedOps[0], MappedOps.size()); |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 3023 | AddToWorkList(ShufMask.Val); |
| 3024 | |
| 3025 | // Build the undef vector. |
| 3026 | SDOperand UDVal = DAG.getNode(ISD::UNDEF, MappedOps[0].getValueType()); |
| 3027 | for (unsigned i = 0; i != NumElts; ++i) |
| 3028 | MappedOps[i] = UDVal; |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 3029 | MappedOps[NumElts ] = *(N0.Val->op_end()-2); |
| 3030 | MappedOps[NumElts+1] = *(N0.Val->op_end()-1); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3031 | UDVal = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 3032 | &MappedOps[0], MappedOps.size()); |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 3033 | |
| 3034 | return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, |
Evan Cheng | e7bec0d | 2006-07-20 22:44:41 +0000 | [diff] [blame] | 3035 | N0, UDVal, ShufMask, |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 3036 | MappedOps[NumElts], MappedOps[NumElts+1]); |
| 3037 | } |
| 3038 | |
Chris Lattner | f1d0c62 | 2006-03-31 22:16:43 +0000 | [diff] [blame] | 3039 | return SDOperand(); |
| 3040 | } |
| 3041 | |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 3042 | /// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform |
| 3043 | /// a VAND to a vector_shuffle with the destination vector and a zero vector. |
| 3044 | /// e.g. VAND V, <0xffffffff, 0, 0xffffffff, 0>. ==> |
| 3045 | /// vector_shuffle V, Zero, <0, 4, 2, 4> |
| 3046 | SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) { |
| 3047 | SDOperand LHS = N->getOperand(0); |
| 3048 | SDOperand RHS = N->getOperand(1); |
| 3049 | if (N->getOpcode() == ISD::VAND) { |
| 3050 | SDOperand DstVecSize = *(LHS.Val->op_end()-2); |
| 3051 | SDOperand DstVecEVT = *(LHS.Val->op_end()-1); |
| 3052 | if (RHS.getOpcode() == ISD::VBIT_CONVERT) |
| 3053 | RHS = RHS.getOperand(0); |
| 3054 | if (RHS.getOpcode() == ISD::VBUILD_VECTOR) { |
| 3055 | std::vector<SDOperand> IdxOps; |
| 3056 | unsigned NumOps = RHS.getNumOperands(); |
| 3057 | unsigned NumElts = NumOps-2; |
| 3058 | MVT::ValueType EVT = cast<VTSDNode>(RHS.getOperand(NumOps-1))->getVT(); |
| 3059 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3060 | SDOperand Elt = RHS.getOperand(i); |
| 3061 | if (!isa<ConstantSDNode>(Elt)) |
| 3062 | return SDOperand(); |
| 3063 | else if (cast<ConstantSDNode>(Elt)->isAllOnesValue()) |
| 3064 | IdxOps.push_back(DAG.getConstant(i, EVT)); |
| 3065 | else if (cast<ConstantSDNode>(Elt)->isNullValue()) |
| 3066 | IdxOps.push_back(DAG.getConstant(NumElts, EVT)); |
| 3067 | else |
| 3068 | return SDOperand(); |
| 3069 | } |
| 3070 | |
| 3071 | // Let's see if the target supports this vector_shuffle. |
| 3072 | if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG)) |
| 3073 | return SDOperand(); |
| 3074 | |
| 3075 | // Return the new VVECTOR_SHUFFLE node. |
| 3076 | SDOperand NumEltsNode = DAG.getConstant(NumElts, MVT::i32); |
| 3077 | SDOperand EVTNode = DAG.getValueType(EVT); |
| 3078 | std::vector<SDOperand> Ops; |
Chris Lattner | 516b962 | 2006-09-14 20:50:57 +0000 | [diff] [blame] | 3079 | LHS = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, LHS, NumEltsNode, |
| 3080 | EVTNode); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 3081 | Ops.push_back(LHS); |
| 3082 | AddToWorkList(LHS.Val); |
| 3083 | std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT)); |
| 3084 | ZeroOps.push_back(NumEltsNode); |
| 3085 | ZeroOps.push_back(EVTNode); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3086 | Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 3087 | &ZeroOps[0], ZeroOps.size())); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 3088 | IdxOps.push_back(NumEltsNode); |
| 3089 | IdxOps.push_back(EVTNode); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3090 | Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, |
| 3091 | &IdxOps[0], IdxOps.size())); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 3092 | Ops.push_back(NumEltsNode); |
| 3093 | Ops.push_back(EVTNode); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3094 | SDOperand Result = DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, |
| 3095 | &Ops[0], Ops.size()); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 3096 | if (NumEltsNode != DstVecSize || EVTNode != DstVecEVT) { |
| 3097 | Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result, |
| 3098 | DstVecSize, DstVecEVT); |
| 3099 | } |
| 3100 | return Result; |
| 3101 | } |
| 3102 | } |
| 3103 | return SDOperand(); |
| 3104 | } |
| 3105 | |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 3106 | /// visitVBinOp - Visit a binary vector operation, like VADD. IntOp indicates |
| 3107 | /// the scalar operation of the vop if it is operating on an integer vector |
| 3108 | /// (e.g. ADD) and FPOp indicates the FP version (e.g. FADD). |
| 3109 | SDOperand DAGCombiner::visitVBinOp(SDNode *N, ISD::NodeType IntOp, |
| 3110 | ISD::NodeType FPOp) { |
| 3111 | MVT::ValueType EltType = cast<VTSDNode>(*(N->op_end()-1))->getVT(); |
| 3112 | ISD::NodeType ScalarOp = MVT::isInteger(EltType) ? IntOp : FPOp; |
| 3113 | SDOperand LHS = N->getOperand(0); |
| 3114 | SDOperand RHS = N->getOperand(1); |
Evan Cheng | 44f1f09 | 2006-04-20 08:56:16 +0000 | [diff] [blame] | 3115 | SDOperand Shuffle = XformToShuffleWithZero(N); |
| 3116 | if (Shuffle.Val) return Shuffle; |
| 3117 | |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 3118 | // If the LHS and RHS are VBUILD_VECTOR nodes, see if we can constant fold |
| 3119 | // this operation. |
| 3120 | if (LHS.getOpcode() == ISD::VBUILD_VECTOR && |
| 3121 | RHS.getOpcode() == ISD::VBUILD_VECTOR) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3122 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 3123 | for (unsigned i = 0, e = LHS.getNumOperands()-2; i != e; ++i) { |
| 3124 | SDOperand LHSOp = LHS.getOperand(i); |
| 3125 | SDOperand RHSOp = RHS.getOperand(i); |
| 3126 | // If these two elements can't be folded, bail out. |
| 3127 | if ((LHSOp.getOpcode() != ISD::UNDEF && |
| 3128 | LHSOp.getOpcode() != ISD::Constant && |
| 3129 | LHSOp.getOpcode() != ISD::ConstantFP) || |
| 3130 | (RHSOp.getOpcode() != ISD::UNDEF && |
| 3131 | RHSOp.getOpcode() != ISD::Constant && |
| 3132 | RHSOp.getOpcode() != ISD::ConstantFP)) |
| 3133 | break; |
Evan Cheng | 7b336a8 | 2006-05-31 06:08:35 +0000 | [diff] [blame] | 3134 | // Can't fold divide by zero. |
| 3135 | if (N->getOpcode() == ISD::VSDIV || N->getOpcode() == ISD::VUDIV) { |
| 3136 | if ((RHSOp.getOpcode() == ISD::Constant && |
| 3137 | cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) || |
| 3138 | (RHSOp.getOpcode() == ISD::ConstantFP && |
| 3139 | !cast<ConstantFPSDNode>(RHSOp.Val)->getValue())) |
| 3140 | break; |
| 3141 | } |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 3142 | Ops.push_back(DAG.getNode(ScalarOp, EltType, LHSOp, RHSOp)); |
Chris Lattner | 3e104b1 | 2006-04-08 04:15:24 +0000 | [diff] [blame] | 3143 | AddToWorkList(Ops.back().Val); |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 3144 | assert((Ops.back().getOpcode() == ISD::UNDEF || |
| 3145 | Ops.back().getOpcode() == ISD::Constant || |
| 3146 | Ops.back().getOpcode() == ISD::ConstantFP) && |
| 3147 | "Scalar binop didn't fold!"); |
| 3148 | } |
Chris Lattner | a4c5d8c | 2006-04-03 17:21:50 +0000 | [diff] [blame] | 3149 | |
| 3150 | if (Ops.size() == LHS.getNumOperands()-2) { |
| 3151 | Ops.push_back(*(LHS.Val->op_end()-2)); |
| 3152 | Ops.push_back(*(LHS.Val->op_end()-1)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3153 | return DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &Ops[0], Ops.size()); |
Chris Lattner | a4c5d8c | 2006-04-03 17:21:50 +0000 | [diff] [blame] | 3154 | } |
Chris Lattner | edab1b9 | 2006-04-02 03:25:57 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | return SDOperand(); |
| 3158 | } |
| 3159 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3160 | SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){ |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3161 | assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!"); |
| 3162 | |
| 3163 | SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2, |
| 3164 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); |
| 3165 | // If we got a simplified select_cc node back from SimplifySelectCC, then |
| 3166 | // break it down into a new SETCC node, and a new SELECT node, and then return |
| 3167 | // the SELECT node, since we were called with a SELECT node. |
| 3168 | if (SCC.Val) { |
| 3169 | // Check to see if we got a select_cc back (to turn into setcc/select). |
| 3170 | // Otherwise, just return whatever node we got back, like fabs. |
| 3171 | if (SCC.getOpcode() == ISD::SELECT_CC) { |
| 3172 | SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(), |
| 3173 | SCC.getOperand(0), SCC.getOperand(1), |
| 3174 | SCC.getOperand(4)); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3175 | AddToWorkList(SETCC.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3176 | return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2), |
| 3177 | SCC.getOperand(3), SETCC); |
| 3178 | } |
| 3179 | return SCC; |
| 3180 | } |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3181 | return SDOperand(); |
| 3182 | } |
| 3183 | |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 3184 | /// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS |
| 3185 | /// are the two values being selected between, see if we can simplify the |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 3186 | /// select. Callers of this should assume that TheSelect is deleted if this |
| 3187 | /// returns true. As such, they should return the appropriate thing (e.g. the |
| 3188 | /// node) back to the top-level of the DAG combiner loop to avoid it being |
| 3189 | /// looked at. |
Chris Lattner | 40c62d5 | 2005-10-18 06:04:22 +0000 | [diff] [blame] | 3190 | /// |
| 3191 | bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS, |
| 3192 | SDOperand RHS) { |
| 3193 | |
| 3194 | // If this is a select from two identical things, try to pull the operation |
| 3195 | // through the select. |
| 3196 | if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){ |
| 3197 | #if 0 |
| 3198 | std::cerr << "SELECT: ["; LHS.Val->dump(); |
| 3199 | std::cerr << "] ["; RHS.Val->dump(); |
| 3200 | std::cerr << "]\n"; |
| 3201 | #endif |
| 3202 | |
| 3203 | // If this is a load and the token chain is identical, replace the select |
| 3204 | // of two loads with a load through a select of the address to load from. |
| 3205 | // This triggers in things like "select bool X, 10.0, 123.0" after the FP |
| 3206 | // constants have been dropped into the constant pool. |
| 3207 | if ((LHS.getOpcode() == ISD::LOAD || |
| 3208 | LHS.getOpcode() == ISD::EXTLOAD || |
| 3209 | LHS.getOpcode() == ISD::ZEXTLOAD || |
| 3210 | LHS.getOpcode() == ISD::SEXTLOAD) && |
| 3211 | // Token chains must be identical. |
| 3212 | LHS.getOperand(0) == RHS.getOperand(0) && |
| 3213 | // If this is an EXTLOAD, the VT's must match. |
| 3214 | (LHS.getOpcode() == ISD::LOAD || |
| 3215 | LHS.getOperand(3) == RHS.getOperand(3))) { |
| 3216 | // FIXME: this conflates two src values, discarding one. This is not |
| 3217 | // the right thing to do, but nothing uses srcvalues now. When they do, |
| 3218 | // turn SrcValue into a list of locations. |
| 3219 | SDOperand Addr; |
| 3220 | if (TheSelect->getOpcode() == ISD::SELECT) |
| 3221 | Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(), |
| 3222 | TheSelect->getOperand(0), LHS.getOperand(1), |
| 3223 | RHS.getOperand(1)); |
| 3224 | else |
| 3225 | Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(), |
| 3226 | TheSelect->getOperand(0), |
| 3227 | TheSelect->getOperand(1), |
| 3228 | LHS.getOperand(1), RHS.getOperand(1), |
| 3229 | TheSelect->getOperand(4)); |
| 3230 | |
| 3231 | SDOperand Load; |
| 3232 | if (LHS.getOpcode() == ISD::LOAD) |
| 3233 | Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0), |
| 3234 | Addr, LHS.getOperand(2)); |
| 3235 | else |
| 3236 | Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0), |
| 3237 | LHS.getOperand(0), Addr, LHS.getOperand(2), |
| 3238 | cast<VTSDNode>(LHS.getOperand(3))->getVT()); |
| 3239 | // Users of the select now use the result of the load. |
| 3240 | CombineTo(TheSelect, Load); |
| 3241 | |
| 3242 | // Users of the old loads now use the new load's chain. We know the |
| 3243 | // old-load value is dead now. |
| 3244 | CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1)); |
| 3245 | CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1)); |
| 3246 | return true; |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | return false; |
| 3251 | } |
| 3252 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3253 | SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1, |
| 3254 | SDOperand N2, SDOperand N3, |
| 3255 | ISD::CondCode CC) { |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3256 | |
| 3257 | MVT::ValueType VT = N2.getValueType(); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3258 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 3259 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); |
| 3260 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); |
| 3261 | |
| 3262 | // Determine if the condition we're dealing with is constant |
| 3263 | SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false); |
| 3264 | ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val); |
| 3265 | |
| 3266 | // fold select_cc true, x, y -> x |
| 3267 | if (SCCC && SCCC->getValue()) |
| 3268 | return N2; |
| 3269 | // fold select_cc false, x, y -> y |
| 3270 | if (SCCC && SCCC->getValue() == 0) |
| 3271 | return N3; |
| 3272 | |
| 3273 | // Check to see if we can simplify the select into an fabs node |
| 3274 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) { |
| 3275 | // Allow either -0.0 or 0.0 |
| 3276 | if (CFP->getValue() == 0.0) { |
| 3277 | // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs |
| 3278 | if ((CC == ISD::SETGE || CC == ISD::SETGT) && |
| 3279 | N0 == N2 && N3.getOpcode() == ISD::FNEG && |
| 3280 | N2 == N3.getOperand(0)) |
| 3281 | return DAG.getNode(ISD::FABS, VT, N0); |
| 3282 | |
| 3283 | // select (setl[te] X, +/-0.0), fneg(X), X -> fabs |
| 3284 | if ((CC == ISD::SETLT || CC == ISD::SETLE) && |
| 3285 | N0 == N3 && N2.getOpcode() == ISD::FNEG && |
| 3286 | N2.getOperand(0) == N3) |
| 3287 | return DAG.getNode(ISD::FABS, VT, N3); |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | // Check to see if we can perform the "gzip trick", transforming |
| 3292 | // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A |
Chris Lattner | e3152e5 | 2006-09-20 06:41:35 +0000 | [diff] [blame] | 3293 | if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT && |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3294 | MVT::isInteger(N0.getValueType()) && |
Chris Lattner | e3152e5 | 2006-09-20 06:41:35 +0000 | [diff] [blame] | 3295 | MVT::isInteger(N2.getValueType()) && |
| 3296 | (N1C->isNullValue() || // (a < 0) ? b : 0 |
| 3297 | (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0 |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3298 | MVT::ValueType XType = N0.getValueType(); |
| 3299 | MVT::ValueType AType = N2.getValueType(); |
| 3300 | if (XType >= AType) { |
| 3301 | // 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] | 3302 | // single-bit constant. |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3303 | if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) { |
| 3304 | unsigned ShCtV = Log2_64(N2C->getValue()); |
| 3305 | ShCtV = MVT::getSizeInBits(XType)-ShCtV-1; |
| 3306 | SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy()); |
| 3307 | SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3308 | AddToWorkList(Shift.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3309 | if (XType > AType) { |
| 3310 | Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3311 | AddToWorkList(Shift.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3312 | } |
| 3313 | return DAG.getNode(ISD::AND, AType, Shift, N2); |
| 3314 | } |
| 3315 | SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0, |
| 3316 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 3317 | TLI.getShiftAmountTy())); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3318 | AddToWorkList(Shift.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3319 | if (XType > AType) { |
| 3320 | Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3321 | AddToWorkList(Shift.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3322 | } |
| 3323 | return DAG.getNode(ISD::AND, AType, Shift, N2); |
| 3324 | } |
| 3325 | } |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3326 | |
| 3327 | // fold select C, 16, 0 -> shl C, 4 |
| 3328 | if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) && |
| 3329 | TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) { |
| 3330 | // Get a SetCC of the condition |
| 3331 | // FIXME: Should probably make sure that setcc is legal if we ever have a |
| 3332 | // target where it isn't. |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 3333 | SDOperand Temp, SCC; |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3334 | // cast from setcc result type to select result type |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 3335 | if (AfterLegalize) { |
| 3336 | SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3337 | Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType()); |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 3338 | } else { |
| 3339 | SCC = DAG.getSetCC(MVT::i1, N0, N1, CC); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3340 | Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC); |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 3341 | } |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3342 | AddToWorkList(SCC.Val); |
| 3343 | AddToWorkList(Temp.Val); |
Nate Begeman | 07ed417 | 2005-10-10 21:26:48 +0000 | [diff] [blame] | 3344 | // shl setcc result by log2 n2c |
| 3345 | return DAG.getNode(ISD::SHL, N2.getValueType(), Temp, |
| 3346 | DAG.getConstant(Log2_64(N2C->getValue()), |
| 3347 | TLI.getShiftAmountTy())); |
| 3348 | } |
| 3349 | |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3350 | // Check to see if this is the equivalent of setcc |
| 3351 | // FIXME: Turn all of these into setcc if setcc if setcc is legal |
| 3352 | // otherwise, go ahead with the folds. |
| 3353 | if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) { |
| 3354 | MVT::ValueType XType = N0.getValueType(); |
| 3355 | if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) { |
| 3356 | SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC); |
| 3357 | if (Res.getValueType() != VT) |
| 3358 | Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res); |
| 3359 | return Res; |
| 3360 | } |
| 3361 | |
| 3362 | // seteq X, 0 -> srl (ctlz X, log2(size(X))) |
| 3363 | if (N1C && N1C->isNullValue() && CC == ISD::SETEQ && |
| 3364 | TLI.isOperationLegal(ISD::CTLZ, XType)) { |
| 3365 | SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0); |
| 3366 | return DAG.getNode(ISD::SRL, XType, Ctlz, |
| 3367 | DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)), |
| 3368 | TLI.getShiftAmountTy())); |
| 3369 | } |
| 3370 | // setgt X, 0 -> srl (and (-X, ~X), size(X)-1) |
| 3371 | if (N1C && N1C->isNullValue() && CC == ISD::SETGT) { |
| 3372 | SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType), |
| 3373 | N0); |
| 3374 | SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0, |
| 3375 | DAG.getConstant(~0ULL, XType)); |
| 3376 | return DAG.getNode(ISD::SRL, XType, |
| 3377 | DAG.getNode(ISD::AND, XType, NegN0, NotN0), |
| 3378 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 3379 | TLI.getShiftAmountTy())); |
| 3380 | } |
| 3381 | // setgt X, -1 -> xor (srl (X, size(X)-1), 1) |
| 3382 | if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) { |
| 3383 | SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0, |
| 3384 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 3385 | TLI.getShiftAmountTy())); |
| 3386 | return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType)); |
| 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X -> |
| 3391 | // Y = sra (X, size(X)-1); xor (add (X, Y), Y) |
| 3392 | if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) && |
| 3393 | N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) { |
| 3394 | if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) { |
| 3395 | MVT::ValueType XType = N0.getValueType(); |
| 3396 | if (SubC->isNullValue() && MVT::isInteger(XType)) { |
| 3397 | SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0, |
| 3398 | DAG.getConstant(MVT::getSizeInBits(XType)-1, |
| 3399 | TLI.getShiftAmountTy())); |
| 3400 | SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3401 | AddToWorkList(Shift.Val); |
| 3402 | AddToWorkList(Add.Val); |
Nate Begeman | f845b45 | 2005-10-08 00:29:44 +0000 | [diff] [blame] | 3403 | return DAG.getNode(ISD::XOR, XType, Add, Shift); |
| 3404 | } |
| 3405 | } |
| 3406 | } |
| 3407 | |
Nate Begeman | 44728a7 | 2005-09-19 22:34:01 +0000 | [diff] [blame] | 3408 | return SDOperand(); |
| 3409 | } |
| 3410 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3411 | SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0, |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 3412 | SDOperand N1, ISD::CondCode Cond, |
| 3413 | bool foldBooleans) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3414 | // These setcc operations always fold. |
| 3415 | switch (Cond) { |
| 3416 | default: break; |
| 3417 | case ISD::SETFALSE: |
| 3418 | case ISD::SETFALSE2: return DAG.getConstant(0, VT); |
| 3419 | case ISD::SETTRUE: |
| 3420 | case ISD::SETTRUE2: return DAG.getConstant(1, VT); |
| 3421 | } |
| 3422 | |
| 3423 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { |
| 3424 | uint64_t C1 = N1C->getValue(); |
| 3425 | if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) { |
| 3426 | uint64_t C0 = N0C->getValue(); |
| 3427 | |
| 3428 | // Sign extend the operands if required |
| 3429 | if (ISD::isSignedIntSetCC(Cond)) { |
| 3430 | C0 = N0C->getSignExtended(); |
| 3431 | C1 = N1C->getSignExtended(); |
| 3432 | } |
| 3433 | |
| 3434 | switch (Cond) { |
| 3435 | default: assert(0 && "Unknown integer setcc!"); |
| 3436 | case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT); |
| 3437 | case ISD::SETNE: return DAG.getConstant(C0 != C1, VT); |
| 3438 | case ISD::SETULT: return DAG.getConstant(C0 < C1, VT); |
| 3439 | case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT); |
| 3440 | case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT); |
| 3441 | case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT); |
| 3442 | case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT); |
| 3443 | case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT); |
| 3444 | case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT); |
| 3445 | case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT); |
| 3446 | } |
| 3447 | } else { |
Chris Lattner | 5f42a24 | 2006-09-20 06:19:26 +0000 | [diff] [blame] | 3448 | // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an |
| 3449 | // equality comparison, then we're just comparing whether X itself is |
| 3450 | // zero. |
| 3451 | if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) && |
| 3452 | N0.getOperand(0).getOpcode() == ISD::CTLZ && |
| 3453 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 3454 | unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getValue(); |
| 3455 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 3456 | ShAmt == Log2_32(MVT::getSizeInBits(N0.getValueType()))) { |
| 3457 | if ((C1 == 0) == (Cond == ISD::SETEQ)) { |
| 3458 | // (srl (ctlz x), 5) == 0 -> X != 0 |
| 3459 | // (srl (ctlz x), 5) != 1 -> X != 0 |
| 3460 | Cond = ISD::SETNE; |
| 3461 | } else { |
| 3462 | // (srl (ctlz x), 5) != 0 -> X == 0 |
| 3463 | // (srl (ctlz x), 5) == 1 -> X == 0 |
| 3464 | Cond = ISD::SETEQ; |
| 3465 | } |
| 3466 | SDOperand Zero = DAG.getConstant(0, N0.getValueType()); |
| 3467 | return DAG.getSetCC(VT, N0.getOperand(0).getOperand(0), |
| 3468 | Zero, Cond); |
| 3469 | } |
| 3470 | } |
| 3471 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3472 | // If the LHS is a ZERO_EXTEND, perform the comparison on the input. |
| 3473 | if (N0.getOpcode() == ISD::ZERO_EXTEND) { |
| 3474 | unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType()); |
| 3475 | |
| 3476 | // If the comparison constant has bits in the upper part, the |
| 3477 | // zero-extended value could never match. |
| 3478 | if (C1 & (~0ULL << InSize)) { |
| 3479 | unsigned VSize = MVT::getSizeInBits(N0.getValueType()); |
| 3480 | switch (Cond) { |
| 3481 | case ISD::SETUGT: |
| 3482 | case ISD::SETUGE: |
| 3483 | case ISD::SETEQ: return DAG.getConstant(0, VT); |
| 3484 | case ISD::SETULT: |
| 3485 | case ISD::SETULE: |
| 3486 | case ISD::SETNE: return DAG.getConstant(1, VT); |
| 3487 | case ISD::SETGT: |
| 3488 | case ISD::SETGE: |
| 3489 | // True if the sign bit of C1 is set. |
| 3490 | return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT); |
| 3491 | case ISD::SETLT: |
| 3492 | case ISD::SETLE: |
| 3493 | // True if the sign bit of C1 isn't set. |
| 3494 | return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT); |
| 3495 | default: |
| 3496 | break; |
| 3497 | } |
| 3498 | } |
| 3499 | |
| 3500 | // Otherwise, we can perform the comparison with the low bits. |
| 3501 | switch (Cond) { |
| 3502 | case ISD::SETEQ: |
| 3503 | case ISD::SETNE: |
| 3504 | case ISD::SETUGT: |
| 3505 | case ISD::SETUGE: |
| 3506 | case ISD::SETULT: |
| 3507 | case ISD::SETULE: |
| 3508 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 3509 | DAG.getConstant(C1, N0.getOperand(0).getValueType()), |
| 3510 | Cond); |
| 3511 | default: |
| 3512 | break; // todo, be more careful with signed comparisons |
| 3513 | } |
| 3514 | } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 3515 | (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { |
| 3516 | MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT(); |
| 3517 | unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy); |
| 3518 | MVT::ValueType ExtDstTy = N0.getValueType(); |
| 3519 | unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy); |
| 3520 | |
| 3521 | // If the extended part has any inconsistent bits, it cannot ever |
| 3522 | // compare equal. In other words, they have to be all ones or all |
| 3523 | // zeros. |
| 3524 | uint64_t ExtBits = |
| 3525 | (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1)); |
| 3526 | if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits) |
| 3527 | return DAG.getConstant(Cond == ISD::SETNE, VT); |
| 3528 | |
| 3529 | SDOperand ZextOp; |
| 3530 | MVT::ValueType Op0Ty = N0.getOperand(0).getValueType(); |
| 3531 | if (Op0Ty == ExtSrcTy) { |
| 3532 | ZextOp = N0.getOperand(0); |
| 3533 | } else { |
| 3534 | int64_t Imm = ~0ULL >> (64-ExtSrcTyBits); |
| 3535 | ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0), |
| 3536 | DAG.getConstant(Imm, Op0Ty)); |
| 3537 | } |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3538 | AddToWorkList(ZextOp.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3539 | // Otherwise, make this a use of a zext. |
| 3540 | return DAG.getSetCC(VT, ZextOp, |
| 3541 | DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)), |
| 3542 | ExtDstTy), |
| 3543 | Cond); |
Chris Lattner | 3391bcd | 2006-02-08 02:13:15 +0000 | [diff] [blame] | 3544 | } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) && |
| 3545 | (Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 3546 | (N0.getOpcode() == ISD::XOR || |
| 3547 | (N0.getOpcode() == ISD::AND && |
| 3548 | N0.getOperand(0).getOpcode() == ISD::XOR && |
| 3549 | N0.getOperand(1) == N0.getOperand(0).getOperand(1))) && |
| 3550 | isa<ConstantSDNode>(N0.getOperand(1)) && |
| 3551 | cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) { |
| 3552 | // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can |
| 3553 | // only do this if the top bits are known zero. |
| 3554 | if (TLI.MaskedValueIsZero(N1, |
| 3555 | MVT::getIntVTBitMask(N0.getValueType())-1)) { |
| 3556 | // Okay, get the un-inverted input value. |
| 3557 | SDOperand Val; |
| 3558 | if (N0.getOpcode() == ISD::XOR) |
| 3559 | Val = N0.getOperand(0); |
| 3560 | else { |
| 3561 | assert(N0.getOpcode() == ISD::AND && |
| 3562 | N0.getOperand(0).getOpcode() == ISD::XOR); |
| 3563 | // ((X^1)&1)^1 -> X & 1 |
| 3564 | Val = DAG.getNode(ISD::AND, N0.getValueType(), |
| 3565 | N0.getOperand(0).getOperand(0), N0.getOperand(1)); |
| 3566 | } |
| 3567 | return DAG.getSetCC(VT, Val, N1, |
| 3568 | Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ); |
| 3569 | } |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3570 | } |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 3571 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3572 | uint64_t MinVal, MaxVal; |
| 3573 | unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0)); |
| 3574 | if (ISD::isSignedIntSetCC(Cond)) { |
| 3575 | MinVal = 1ULL << (OperandBitSize-1); |
| 3576 | if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. |
| 3577 | MaxVal = ~0ULL >> (65-OperandBitSize); |
| 3578 | else |
| 3579 | MaxVal = 0; |
| 3580 | } else { |
| 3581 | MinVal = 0; |
| 3582 | MaxVal = ~0ULL >> (64-OperandBitSize); |
| 3583 | } |
| 3584 | |
| 3585 | // Canonicalize GE/LE comparisons to use GT/LT comparisons. |
| 3586 | if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { |
| 3587 | if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true |
| 3588 | --C1; // X >= C0 --> X > (C0-1) |
| 3589 | return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()), |
| 3590 | (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); |
| 3591 | } |
| 3592 | |
| 3593 | if (Cond == ISD::SETLE || Cond == ISD::SETULE) { |
| 3594 | if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true |
| 3595 | ++C1; // X <= C0 --> X < (C0+1) |
| 3596 | return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()), |
| 3597 | (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); |
| 3598 | } |
| 3599 | |
| 3600 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal) |
| 3601 | return DAG.getConstant(0, VT); // X < MIN --> false |
| 3602 | |
| 3603 | // Canonicalize setgt X, Min --> setne X, Min |
| 3604 | if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal) |
| 3605 | return DAG.getSetCC(VT, N0, N1, ISD::SETNE); |
Chris Lattner | c8597ca | 2005-10-21 21:23:25 +0000 | [diff] [blame] | 3606 | // Canonicalize setlt X, Max --> setne X, Max |
| 3607 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal) |
| 3608 | return DAG.getSetCC(VT, N0, N1, ISD::SETNE); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3609 | |
| 3610 | // If we have setult X, 1, turn it into seteq X, 0 |
| 3611 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1) |
| 3612 | return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()), |
| 3613 | ISD::SETEQ); |
| 3614 | // If we have setugt X, Max-1, turn it into seteq X, Max |
| 3615 | else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1) |
| 3616 | return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()), |
| 3617 | ISD::SETEQ); |
| 3618 | |
| 3619 | // If we have "setcc X, C0", check to see if we can shrink the immediate |
| 3620 | // by changing cc. |
| 3621 | |
| 3622 | // SETUGT X, SINTMAX -> SETLT X, 0 |
| 3623 | if (Cond == ISD::SETUGT && OperandBitSize != 1 && |
| 3624 | C1 == (~0ULL >> (65-OperandBitSize))) |
| 3625 | return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()), |
| 3626 | ISD::SETLT); |
| 3627 | |
| 3628 | // FIXME: Implement the rest of these. |
| 3629 | |
| 3630 | // Fold bit comparisons when we can. |
| 3631 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 3632 | VT == N0.getValueType() && N0.getOpcode() == ISD::AND) |
| 3633 | if (ConstantSDNode *AndRHS = |
| 3634 | dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 3635 | if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 |
| 3636 | // Perform the xform if the AND RHS is a single bit. |
| 3637 | if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) { |
| 3638 | return DAG.getNode(ISD::SRL, VT, N0, |
| 3639 | DAG.getConstant(Log2_64(AndRHS->getValue()), |
| 3640 | TLI.getShiftAmountTy())); |
| 3641 | } |
| 3642 | } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) { |
| 3643 | // (X & 8) == 8 --> (X & 8) >> 3 |
| 3644 | // Perform the xform if C1 is a single bit. |
| 3645 | if ((C1 & (C1-1)) == 0) { |
| 3646 | return DAG.getNode(ISD::SRL, VT, N0, |
Chris Lattner | 729c6d1 | 2006-05-27 00:43:02 +0000 | [diff] [blame] | 3647 | DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy())); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3648 | } |
| 3649 | } |
| 3650 | } |
| 3651 | } |
| 3652 | } else if (isa<ConstantSDNode>(N0.Val)) { |
| 3653 | // Ensure that the constant occurs on the RHS. |
| 3654 | return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond)); |
| 3655 | } |
| 3656 | |
| 3657 | if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val)) |
| 3658 | if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) { |
| 3659 | double C0 = N0C->getValue(), C1 = N1C->getValue(); |
| 3660 | |
| 3661 | switch (Cond) { |
| 3662 | default: break; // FIXME: Implement the rest of these! |
| 3663 | case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT); |
| 3664 | case ISD::SETNE: return DAG.getConstant(C0 != C1, VT); |
| 3665 | case ISD::SETLT: return DAG.getConstant(C0 < C1, VT); |
| 3666 | case ISD::SETGT: return DAG.getConstant(C0 > C1, VT); |
| 3667 | case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT); |
| 3668 | case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT); |
| 3669 | } |
| 3670 | } else { |
| 3671 | // Ensure that the constant occurs on the RHS. |
| 3672 | return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond)); |
| 3673 | } |
| 3674 | |
| 3675 | if (N0 == N1) { |
| 3676 | // We can always fold X == Y for integer setcc's. |
| 3677 | if (MVT::isInteger(N0.getValueType())) |
| 3678 | return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); |
| 3679 | unsigned UOF = ISD::getUnorderedFlavor(Cond); |
| 3680 | if (UOF == 2) // FP operators that are undefined on NaNs. |
| 3681 | return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT); |
| 3682 | if (UOF == unsigned(ISD::isTrueWhenEqual(Cond))) |
| 3683 | return DAG.getConstant(UOF, VT); |
| 3684 | // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO |
| 3685 | // if it is not already. |
Chris Lattner | 4090aee | 2006-01-18 19:13:41 +0000 | [diff] [blame] | 3686 | ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO; |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3687 | if (NewCond != Cond) |
| 3688 | return DAG.getSetCC(VT, N0, N1, NewCond); |
| 3689 | } |
| 3690 | |
| 3691 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 3692 | MVT::isInteger(N0.getValueType())) { |
| 3693 | if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB || |
| 3694 | N0.getOpcode() == ISD::XOR) { |
| 3695 | // Simplify (X+Y) == (X+Z) --> Y == Z |
| 3696 | if (N0.getOpcode() == N1.getOpcode()) { |
| 3697 | if (N0.getOperand(0) == N1.getOperand(0)) |
| 3698 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond); |
| 3699 | if (N0.getOperand(1) == N1.getOperand(1)) |
| 3700 | return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond); |
Evan Cheng | 1efba0e | 2006-08-29 06:42:35 +0000 | [diff] [blame] | 3701 | if (DAG.isCommutativeBinOp(N0.getOpcode())) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3702 | // If X op Y == Y op X, try other combinations. |
| 3703 | if (N0.getOperand(0) == N1.getOperand(1)) |
| 3704 | return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond); |
| 3705 | if (N0.getOperand(1) == N1.getOperand(0)) |
Chris Lattner | a158eee | 2005-10-25 18:57:30 +0000 | [diff] [blame] | 3706 | return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3707 | } |
| 3708 | } |
Chris Lattner | b3ddfc4 | 2006-02-02 06:36:13 +0000 | [diff] [blame] | 3709 | |
| 3710 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) { |
| 3711 | if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) { |
| 3712 | // Turn (X+C1) == C2 --> X == C2-C1 |
| 3713 | if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) { |
| 3714 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 3715 | DAG.getConstant(RHSC->getValue()-LHSR->getValue(), |
| 3716 | N0.getValueType()), Cond); |
| 3717 | } |
| 3718 | |
| 3719 | // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. |
| 3720 | if (N0.getOpcode() == ISD::XOR) |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 3721 | // If we know that all of the inverted bits are zero, don't bother |
| 3722 | // performing the inversion. |
Chris Lattner | b3ddfc4 | 2006-02-02 06:36:13 +0000 | [diff] [blame] | 3723 | if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue())) |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 3724 | return DAG.getSetCC(VT, N0.getOperand(0), |
Chris Lattner | b3ddfc4 | 2006-02-02 06:36:13 +0000 | [diff] [blame] | 3725 | DAG.getConstant(LHSR->getValue()^RHSC->getValue(), |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 3726 | N0.getValueType()), Cond); |
Chris Lattner | b3ddfc4 | 2006-02-02 06:36:13 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | // Turn (C1-X) == C2 --> X == C1-C2 |
| 3730 | if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) { |
| 3731 | if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) { |
| 3732 | return DAG.getSetCC(VT, N0.getOperand(1), |
| 3733 | DAG.getConstant(SUBC->getValue()-RHSC->getValue(), |
| 3734 | N0.getValueType()), Cond); |
Chris Lattner | 5c46f74 | 2005-10-05 06:11:08 +0000 | [diff] [blame] | 3735 | } |
Chris Lattner | b3ddfc4 | 2006-02-02 06:36:13 +0000 | [diff] [blame] | 3736 | } |
| 3737 | } |
| 3738 | |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3739 | // Simplify (X+Z) == X --> Z == 0 |
| 3740 | if (N0.getOperand(0) == N1) |
| 3741 | return DAG.getSetCC(VT, N0.getOperand(1), |
| 3742 | DAG.getConstant(0, N0.getValueType()), Cond); |
| 3743 | if (N0.getOperand(1) == N1) { |
Evan Cheng | 1efba0e | 2006-08-29 06:42:35 +0000 | [diff] [blame] | 3744 | if (DAG.isCommutativeBinOp(N0.getOpcode())) |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3745 | return DAG.getSetCC(VT, N0.getOperand(0), |
| 3746 | DAG.getConstant(0, N0.getValueType()), Cond); |
| 3747 | else { |
| 3748 | assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 3749 | // (Z-X) == X --> Z == X<<1 |
| 3750 | SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), |
| 3751 | N1, |
| 3752 | DAG.getConstant(1,TLI.getShiftAmountTy())); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3753 | AddToWorkList(SH.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3754 | return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond); |
| 3755 | } |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB || |
| 3760 | N1.getOpcode() == ISD::XOR) { |
| 3761 | // Simplify X == (X+Z) --> Z == 0 |
| 3762 | if (N1.getOperand(0) == N0) { |
| 3763 | return DAG.getSetCC(VT, N1.getOperand(1), |
| 3764 | DAG.getConstant(0, N1.getValueType()), Cond); |
| 3765 | } else if (N1.getOperand(1) == N0) { |
Evan Cheng | 1efba0e | 2006-08-29 06:42:35 +0000 | [diff] [blame] | 3766 | if (DAG.isCommutativeBinOp(N1.getOpcode())) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3767 | return DAG.getSetCC(VT, N1.getOperand(0), |
| 3768 | DAG.getConstant(0, N1.getValueType()), Cond); |
| 3769 | } else { |
| 3770 | assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 3771 | // X == (Z-X) --> X<<1 == Z |
| 3772 | SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0, |
| 3773 | DAG.getConstant(1,TLI.getShiftAmountTy())); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3774 | AddToWorkList(SH.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3775 | return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond); |
| 3776 | } |
| 3777 | } |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | // Fold away ALL boolean setcc's. |
| 3782 | SDOperand Temp; |
Nate Begeman | e17daeb | 2005-10-05 21:43:42 +0000 | [diff] [blame] | 3783 | if (N0.getValueType() == MVT::i1 && foldBooleans) { |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3784 | switch (Cond) { |
| 3785 | default: assert(0 && "Unknown integer setcc!"); |
| 3786 | case ISD::SETEQ: // X == Y -> (X^Y)^1 |
| 3787 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1); |
| 3788 | 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] | 3789 | AddToWorkList(Temp.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3790 | break; |
| 3791 | case ISD::SETNE: // X != Y --> (X^Y) |
| 3792 | N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1); |
| 3793 | break; |
| 3794 | case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y |
| 3795 | case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y |
| 3796 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1)); |
| 3797 | N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3798 | AddToWorkList(Temp.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3799 | break; |
| 3800 | case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X |
| 3801 | case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X |
| 3802 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1)); |
| 3803 | N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3804 | AddToWorkList(Temp.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3805 | break; |
| 3806 | case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y |
| 3807 | case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y |
| 3808 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1)); |
| 3809 | N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp); |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3810 | AddToWorkList(Temp.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3811 | break; |
| 3812 | case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X |
| 3813 | case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X |
| 3814 | Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1)); |
| 3815 | N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp); |
| 3816 | break; |
| 3817 | } |
| 3818 | if (VT != MVT::i1) { |
Chris Lattner | 5750df9 | 2006-03-01 04:03:14 +0000 | [diff] [blame] | 3819 | AddToWorkList(N0.Val); |
Nate Begeman | 452d7be | 2005-09-16 00:54:12 +0000 | [diff] [blame] | 3820 | // FIXME: If running after legalize, we probably can't do this. |
| 3821 | N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0); |
| 3822 | } |
| 3823 | return N0; |
| 3824 | } |
| 3825 | |
| 3826 | // Could not fold it. |
| 3827 | return SDOperand(); |
| 3828 | } |
| 3829 | |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 3830 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 3831 | /// return a DAG expression to select that will generate the same value by |
| 3832 | /// multiplying by a magic number. See: |
| 3833 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 3834 | SDOperand DAGCombiner::BuildSDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 3835 | std::vector<SDNode*> Built; |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 3836 | SDOperand S = TLI.BuildSDIV(N, DAG, &Built); |
| 3837 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 3838 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 3839 | ii != ee; ++ii) |
| 3840 | AddToWorkList(*ii); |
| 3841 | return S; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 3845 | /// return a DAG expression to select that will generate the same value by |
| 3846 | /// multiplying by a magic number. See: |
| 3847 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 3848 | SDOperand DAGCombiner::BuildUDIV(SDNode *N) { |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 3849 | std::vector<SDNode*> Built; |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 3850 | SDOperand S = TLI.BuildUDIV(N, DAG, &Built); |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 3851 | |
Andrew Lenharth | 232c910 | 2006-06-12 16:07:18 +0000 | [diff] [blame] | 3852 | for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end(); |
Andrew Lenharth | dae9cbe | 2006-05-16 17:42:15 +0000 | [diff] [blame] | 3853 | ii != ee; ++ii) |
| 3854 | AddToWorkList(*ii); |
| 3855 | return S; |
Nate Begeman | 6957523 | 2005-10-20 02:15:44 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3858 | // SelectionDAG::Combine - This is the entry point for the file. |
| 3859 | // |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 3860 | void SelectionDAG::Combine(bool RunningAfterLegalize) { |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3861 | /// run - This is the main entry point to this class. |
| 3862 | /// |
Nate Begeman | 4ebd805 | 2005-09-01 23:24:04 +0000 | [diff] [blame] | 3863 | DAGCombiner(*this).Run(RunningAfterLegalize); |
Nate Begeman | 1d4d414 | 2005-09-01 00:19:25 +0000 | [diff] [blame] | 3864 | } |