blob: d142c85024bf4665c2d3ddb68d61e578beae4d05 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
19// FIXME: Should add a corresponding version of fold AND with
20// ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
21// we don't have yet.
22//
23// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman1d4d4142005-09-01 00:19:25 +000025// FIXME: zero extend when top bits are 0 -> drop it ?
Nate Begeman4ebd8052005-09-01 23:24:04 +000026// FIXME: make truncate see through SIGN_EXTEND and AND
27// FIXME: sext_in_reg(setcc) on targets that return zero or one, and where
28// EVT != MVT::i1 can drop the sext.
Nate Begeman4ebd8052005-09-01 23:24:04 +000029// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000030// FIXME: verify that getNode can't return extends with an operand whose type
31// is >= to that of the extend.
32// FIXME: divide by zero is currently left unfolded. do we want to turn this
33// into an undef?
Nate Begeman1d4d4142005-09-01 00:19:25 +000034//
35//===----------------------------------------------------------------------===//
36
37#define DEBUG_TYPE "dagcombine"
38#include "llvm/ADT/Statistic.h"
39#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000040#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000041#include "llvm/Support/MathExtras.h"
42#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000043#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000044#include <cmath>
45using namespace llvm;
46
47namespace {
48 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
49
50 class DAGCombiner {
51 SelectionDAG &DAG;
52 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000053 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000054
55 // Worklist of all of the nodes that need to be simplified.
56 std::vector<SDNode*> WorkList;
57
58 /// AddUsersToWorkList - When an instruction is simplified, add all users of
59 /// the instruction to the work lists because they might get more simplified
60 /// now.
61 ///
62 void AddUsersToWorkList(SDNode *N) {
63 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000064 UI != UE; ++UI)
65 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000066 }
67
68 /// removeFromWorkList - remove all instances of N from the worklist.
69 void removeFromWorkList(SDNode *N) {
70 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
71 WorkList.end());
72 }
73
74 /// visit - call the node-specific routine that knows how to fold each
75 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +000076 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +000077
78 // Visitation implementation - Implement dag node combining for different
79 // node types. The semantics are as follows:
80 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +000081 // SDOperand.Val == 0 - No change was made
82 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +000083 //
Nate Begeman83e75ec2005-09-06 04:43:02 +000084 SDOperand visitTokenFactor(SDNode *N);
85 SDOperand visitADD(SDNode *N);
86 SDOperand visitSUB(SDNode *N);
87 SDOperand visitMUL(SDNode *N);
88 SDOperand visitSDIV(SDNode *N);
89 SDOperand visitUDIV(SDNode *N);
90 SDOperand visitSREM(SDNode *N);
91 SDOperand visitUREM(SDNode *N);
92 SDOperand visitMULHU(SDNode *N);
93 SDOperand visitMULHS(SDNode *N);
94 SDOperand visitAND(SDNode *N);
95 SDOperand visitOR(SDNode *N);
96 SDOperand visitXOR(SDNode *N);
97 SDOperand visitSHL(SDNode *N);
98 SDOperand visitSRA(SDNode *N);
99 SDOperand visitSRL(SDNode *N);
100 SDOperand visitCTLZ(SDNode *N);
101 SDOperand visitCTTZ(SDNode *N);
102 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000103 SDOperand visitSELECT(SDNode *N);
104 SDOperand visitSELECT_CC(SDNode *N);
105 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000106 SDOperand visitSIGN_EXTEND(SDNode *N);
107 SDOperand visitZERO_EXTEND(SDNode *N);
108 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
109 SDOperand visitTRUNCATE(SDNode *N);
110 SDOperand visitSINT_TO_FP(SDNode *N);
111 SDOperand visitUINT_TO_FP(SDNode *N);
112 SDOperand visitFP_TO_SINT(SDNode *N);
113 SDOperand visitFP_TO_UINT(SDNode *N);
114 SDOperand visitFP_ROUND(SDNode *N);
115 SDOperand visitFP_ROUND_INREG(SDNode *N);
116 SDOperand visitFP_EXTEND(SDNode *N);
117 SDOperand visitFNEG(SDNode *N);
118 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000119 SDOperand visitBRCOND(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000120 // brcondtwoway
121 // br_cc
122 // brtwoway_cc
Nate Begeman452d7be2005-09-16 00:54:12 +0000123
124 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
125 ISD::CondCode Cond);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000126public:
127 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000128 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000129
130 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000131 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000132 };
133}
134
135/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
136/// this predicate to simplify operations downstream. V and Mask are known to
137/// be the same type.
138static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
139 const TargetLowering &TLI) {
140 unsigned SrcBits;
141 if (Mask == 0) return true;
142
143 // If we know the result of a setcc has the top bits zero, use this info.
144 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000145 case ISD::Constant:
146 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
147 case ISD::SETCC:
Nate Begeman646d7e22005-09-02 21:18:40 +0000148 // FIXME: teach this about non ZeroOrOne values, such as 0 or -1
Nate Begeman4ebd8052005-09-01 23:24:04 +0000149 return ((Mask & 1) == 0) &&
150 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
151 case ISD::ZEXTLOAD:
152 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
153 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
154 case ISD::ZERO_EXTEND:
155 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
156 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
157 case ISD::AssertZext:
158 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
159 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
160 case ISD::AND:
161 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
162 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
163 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
164 // FALL THROUGH
165 case ISD::OR:
166 case ISD::XOR:
167 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
168 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
169 case ISD::SELECT:
170 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
171 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
172 case ISD::SELECT_CC:
173 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
174 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
175 case ISD::SRL:
176 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
177 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
178 uint64_t NewVal = Mask << ShAmt->getValue();
179 SrcBits = MVT::getSizeInBits(Op.getValueType());
180 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
181 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
182 }
183 return false;
184 case ISD::SHL:
185 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
186 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
187 uint64_t NewVal = Mask >> ShAmt->getValue();
188 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
189 }
190 return false;
191 case ISD::CTTZ:
192 case ISD::CTLZ:
193 case ISD::CTPOP:
194 // Bit counting instructions can not set the high bits of the result
195 // register. The max number of bits sets depends on the input.
196 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
197
198 // TODO we could handle some SRA cases here.
199 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000200 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000201 return false;
202}
203
Nate Begeman4ebd8052005-09-01 23:24:04 +0000204// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
205// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000206// Also, set the incoming LHS, RHS, and CC references to the appropriate
207// nodes based on the type of node we are checking. This simplifies life a
208// bit for the callers.
209static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
210 SDOperand &CC) {
211 if (N.getOpcode() == ISD::SETCC) {
212 LHS = N.getOperand(0);
213 RHS = N.getOperand(1);
214 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000215 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000216 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000217 if (N.getOpcode() == ISD::SELECT_CC &&
218 N.getOperand(2).getOpcode() == ISD::Constant &&
219 N.getOperand(3).getOpcode() == ISD::Constant &&
220 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000221 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
222 LHS = N.getOperand(0);
223 RHS = N.getOperand(1);
224 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000225 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000226 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000227 return false;
228}
229
Nate Begeman99801192005-09-07 23:25:52 +0000230// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
231// one use. If this is true, it allows the users to invert the operation for
232// free when it is profitable to do so.
233static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000234 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000235 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000236 return true;
237 return false;
238}
239
Nate Begeman452d7be2005-09-16 00:54:12 +0000240// FIXME: This should probably go in the ISD class rather than being duplicated
241// in several files.
242static bool isCommutativeBinOp(unsigned Opcode) {
243 switch (Opcode) {
244 case ISD::ADD:
245 case ISD::MUL:
246 case ISD::AND:
247 case ISD::OR:
248 case ISD::XOR: return true;
249 default: return false; // FIXME: Need commutative info for user ops!
250 }
251}
252
Nate Begeman4ebd8052005-09-01 23:24:04 +0000253void DAGCombiner::Run(bool RunningAfterLegalize) {
254 // set the instance variable, so that the various visit routines may use it.
255 AfterLegalize = RunningAfterLegalize;
256
Nate Begeman646d7e22005-09-02 21:18:40 +0000257 // Add all the dag nodes to the worklist.
258 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000259
Nate Begeman1d4d4142005-09-01 00:19:25 +0000260 // while the worklist isn't empty, inspect the node on the end of it and
261 // try and combine it.
262 while (!WorkList.empty()) {
263 SDNode *N = WorkList.back();
264 WorkList.pop_back();
265
266 // If N has no uses, it is dead. Make sure to revisit all N's operands once
267 // N is deleted from the DAG, since they too may now be dead.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000268 // FIXME: is there a better way to keep from deleting the dag root because
269 // we think it has no uses? This works for now...
270 if (N->use_empty() && N != DAG.getRoot().Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000271 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
272 WorkList.push_back(N->getOperand(i).Val);
273
274 DAG.DeleteNode(N);
275 removeFromWorkList(N);
276 continue;
277 }
278
Nate Begeman83e75ec2005-09-06 04:43:02 +0000279 SDOperand RV = visit(N);
280 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000281 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000282 // If we get back the same node we passed in, rather than a new node or
283 // zero, we know that the node must have defined multiple values and
284 // CombineTo was used. Since CombineTo takes care of the worklist
285 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000286 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000287 DEBUG(std::cerr << "\nReplacing "; N->dump();
288 std::cerr << "\nWith: "; RV.Val->dump();
289 std::cerr << '\n');
Nate Begeman99801192005-09-07 23:25:52 +0000290 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV));
Nate Begeman646d7e22005-09-02 21:18:40 +0000291
292 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000293 WorkList.push_back(RV.Val);
294 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000295
296 // Nodes can end up on the worklist more than once. Make sure we do
297 // not process a node that has been replaced.
298 removeFromWorkList(N);
299 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000300 }
301 }
302}
303
Nate Begeman83e75ec2005-09-06 04:43:02 +0000304SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000305 switch(N->getOpcode()) {
306 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000307 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000308 case ISD::ADD: return visitADD(N);
309 case ISD::SUB: return visitSUB(N);
310 case ISD::MUL: return visitMUL(N);
311 case ISD::SDIV: return visitSDIV(N);
312 case ISD::UDIV: return visitUDIV(N);
313 case ISD::SREM: return visitSREM(N);
314 case ISD::UREM: return visitUREM(N);
315 case ISD::MULHU: return visitMULHU(N);
316 case ISD::MULHS: return visitMULHS(N);
317 case ISD::AND: return visitAND(N);
318 case ISD::OR: return visitOR(N);
319 case ISD::XOR: return visitXOR(N);
320 case ISD::SHL: return visitSHL(N);
321 case ISD::SRA: return visitSRA(N);
322 case ISD::SRL: return visitSRL(N);
323 case ISD::CTLZ: return visitCTLZ(N);
324 case ISD::CTTZ: return visitCTTZ(N);
325 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000326 case ISD::SELECT: return visitSELECT(N);
327 case ISD::SELECT_CC: return visitSELECT_CC(N);
328 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000329 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
330 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
331 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
332 case ISD::TRUNCATE: return visitTRUNCATE(N);
333 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
334 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
335 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
336 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
337 case ISD::FP_ROUND: return visitFP_ROUND(N);
338 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
339 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
340 case ISD::FNEG: return visitFNEG(N);
341 case ISD::FABS: return visitFABS(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000342 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000343 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000344}
345
Nate Begeman83e75ec2005-09-06 04:43:02 +0000346SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000347 // If the token factor has two operands and one is the entry token, replace
348 // the token factor with the other operand.
349 if (N->getNumOperands() == 2) {
350 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000351 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000352 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000353 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000354 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000355 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000356}
357
Nate Begeman83e75ec2005-09-06 04:43:02 +0000358SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000359 SDOperand N0 = N->getOperand(0);
360 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000361 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
362 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
363 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
364 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000365 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000366
367 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000368 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000369 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000370 // canonicalize constant to RHS
371 if (N0C && !N1C) {
372 std::swap(N0, N1);
373 std::swap(N0C, N1C);
374 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000375 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000376 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000377 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000378 // fold floating point (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000379 if (N0CFP && N1CFP)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000380 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), VT);
Nate Begeman223df222005-09-08 20:18:10 +0000381 // fold (add (add x, c1), c2) -> (add x, c1+c2)
382 if (N1C && N0.getOpcode() == ISD::ADD) {
383 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
384 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
385 if (N00C)
386 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
387 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
388 if (N01C)
389 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
390 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
391 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000392 // fold (A + (-B)) -> A-B
393 if (N1.getOpcode() == ISD::FNEG)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000394 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000395 // fold ((-A) + B) -> B-A
396 if (N0.getOpcode() == ISD::FNEG)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000397 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000398 // fold ((0-A) + B) -> B-A
399 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
400 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000401 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000402 // fold (A + (0-B)) -> A-B
403 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
404 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000405 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000406 // fold (A+(B-A)) -> B for non-fp types
407 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1) &&
408 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000409 return N1.getOperand(0);
410 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000411}
412
Nate Begeman83e75ec2005-09-06 04:43:02 +0000413SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000414 SDOperand N0 = N->getOperand(0);
415 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000416 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
417 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
418 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
419 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000420
421 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000422 if (N0C && N1C)
423 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000424 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000425 // fold (sub x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000426 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000427 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000428 // fold floating point (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000429 if (N0CFP && N1CFP)
430 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000431 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000432 // fold (A+B)-A -> B
433 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
434 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000435 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000436 // fold (A+B)-B -> A
437 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 &&
438 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000439 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000440 // fold (A-(-B)) -> A+B
441 if (N1.getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000442 return DAG.getNode(ISD::ADD, N0.getValueType(), N0, N1.getOperand(0));
443 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000444}
445
Nate Begeman83e75ec2005-09-06 04:43:02 +0000446SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000447 SDOperand N0 = N->getOperand(0);
448 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000449 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
450 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
451 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
452 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000453 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000454
455 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000456 if (N0C && N1C)
457 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000458 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000459 // canonicalize constant to RHS
460 if (N0C && !N1C) {
461 std::swap(N0, N1);
462 std::swap(N0C, N1C);
463 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000464 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000465 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000466 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000467 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000468 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000469 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000470 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000471 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000472 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000473 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000474 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000475 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000476 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
477 if (N1C && N0.getOpcode() == ISD::MUL) {
478 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
479 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
480 if (N00C)
481 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
482 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
483 if (N01C)
484 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
485 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
486 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000487 // fold floating point (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 if (N0CFP && N1CFP)
489 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000490 N->getValueType(0));
491 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000492}
493
Nate Begeman83e75ec2005-09-06 04:43:02 +0000494SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000495 SDOperand N0 = N->getOperand(0);
496 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000497 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
498 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
499 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
500 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000501
502 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000503 if (N0C && N1C && !N1C->isNullValue())
504 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000505 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000506 // fold floating point (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000507 if (N0CFP && N1CFP)
508 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000509 N->getValueType(0));
510 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000511}
512
Nate Begeman83e75ec2005-09-06 04:43:02 +0000513SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000514 SDOperand N0 = N->getOperand(0);
515 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000516 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
517 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000518
519 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000520 if (N0C && N1C && !N1C->isNullValue())
521 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000522 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000523 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000524 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000525 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000526 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000527 TLI.getShiftAmountTy()));
528 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000529}
530
Nate Begeman83e75ec2005-09-06 04:43:02 +0000531SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000532 SDOperand N0 = N->getOperand(0);
533 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000534 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
535 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
536 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
537 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000538
539 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000540 if (N0C && N1C && !N1C->isNullValue())
541 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000542 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543 // fold floating point (srem c1, c2) -> fmod(c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000544 if (N0CFP && N1CFP)
545 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000546 N->getValueType(0));
547 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000548}
549
Nate Begeman83e75ec2005-09-06 04:43:02 +0000550SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000551 SDOperand N0 = N->getOperand(0);
552 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000553 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
554 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000555
556 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000557 if (N0C && N1C && !N1C->isNullValue())
558 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000559 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000560 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000561 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000562}
563
Nate Begeman83e75ec2005-09-06 04:43:02 +0000564SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000565 SDOperand N0 = N->getOperand(0);
566 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000567 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568
569 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000570 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000571 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000573 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000574 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
575 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000576 TLI.getShiftAmountTy()));
577 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000578}
579
Nate Begeman83e75ec2005-09-06 04:43:02 +0000580SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000581 SDOperand N0 = N->getOperand(0);
582 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000583 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000584
585 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000586 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000587 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000588 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000589 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000590 return DAG.getConstant(0, N0.getValueType());
591 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000592}
593
Nate Begeman83e75ec2005-09-06 04:43:02 +0000594SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595 SDOperand N0 = N->getOperand(0);
596 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000597 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000598 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
599 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000600 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000601 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000602
603 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000604 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000605 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000606 // canonicalize constant to RHS
607 if (N0C && !N1C) {
608 std::swap(N0, N1);
609 std::swap(N0C, N1C);
610 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000611 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000612 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000613 return N0;
614 // if (and x, c) is known to be zero, return 0
615 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
616 return DAG.getConstant(0, VT);
617 // fold (and x, c) -> x iff (x & ~c) == 0
618 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
619 TLI))
620 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000621 // fold (and (and x, c1), c2) -> (and x, c1^c2)
622 if (N1C && N0.getOpcode() == ISD::AND) {
623 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
624 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
625 if (N00C)
626 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
627 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
628 if (N01C)
629 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
630 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
631 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000632 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
633 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
634 unsigned ExtendBits =
635 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000636 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000637 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000638 }
639 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
640 if (N0.getOpcode() == ISD::OR)
641 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000642 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000643 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000644 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
645 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
646 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
647 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
648
649 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
650 MVT::isInteger(LL.getValueType())) {
651 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
652 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
653 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
654 WorkList.push_back(ORNode.Val);
655 return DAG.getSetCC(VT, ORNode, LR, Op1);
656 }
657 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
658 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
659 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
660 WorkList.push_back(ANDNode.Val);
661 return DAG.getSetCC(VT, ANDNode, LR, Op1);
662 }
663 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
664 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
665 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
666 WorkList.push_back(ORNode.Val);
667 return DAG.getSetCC(VT, ORNode, LR, Op1);
668 }
669 }
670 // canonicalize equivalent to ll == rl
671 if (LL == RR && LR == RL) {
672 Op1 = ISD::getSetCCSwappedOperands(Op1);
673 std::swap(RL, RR);
674 }
675 if (LL == RL && LR == RR) {
676 bool isInteger = MVT::isInteger(LL.getValueType());
677 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
678 if (Result != ISD::SETCC_INVALID)
679 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
680 }
681 }
682 // fold (and (zext x), (zext y)) -> (zext (and x, y))
683 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
684 N1.getOpcode() == ISD::ZERO_EXTEND &&
685 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
686 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
687 N0.getOperand(0), N1.getOperand(0));
688 WorkList.push_back(ANDNode.Val);
689 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
690 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000691 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
692 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
693 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
694 N0.getOperand(1) == N1.getOperand(1)) {
695 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
696 N0.getOperand(0), N1.getOperand(0));
697 WorkList.push_back(ANDNode.Val);
698 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
699 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000701}
702
Nate Begeman83e75ec2005-09-06 04:43:02 +0000703SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704 SDOperand N0 = N->getOperand(0);
705 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000706 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000707 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
708 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000709 MVT::ValueType VT = N1.getValueType();
710 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000711
712 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000713 if (N0C && N1C)
714 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000715 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000716 // canonicalize constant to RHS
717 if (N0C && !N1C) {
718 std::swap(N0, N1);
719 std::swap(N0C, N1C);
720 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000721 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000722 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000723 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000724 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000725 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000726 return N1;
727 // fold (or x, c) -> c iff (x & ~c) == 0
728 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
729 TLI))
730 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000731 // fold (or (or x, c1), c2) -> (or x, c1|c2)
732 if (N1C && N0.getOpcode() == ISD::OR) {
733 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
734 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
735 if (N00C)
736 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
737 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
738 if (N01C)
739 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
740 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
741 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000742 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
743 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
744 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
745 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
746
747 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
748 MVT::isInteger(LL.getValueType())) {
749 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
750 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
751 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
752 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
753 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
754 WorkList.push_back(ORNode.Val);
755 return DAG.getSetCC(VT, ORNode, LR, Op1);
756 }
757 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
758 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
759 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
760 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
761 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
762 WorkList.push_back(ANDNode.Val);
763 return DAG.getSetCC(VT, ANDNode, LR, Op1);
764 }
765 }
766 // canonicalize equivalent to ll == rl
767 if (LL == RR && LR == RL) {
768 Op1 = ISD::getSetCCSwappedOperands(Op1);
769 std::swap(RL, RR);
770 }
771 if (LL == RL && LR == RR) {
772 bool isInteger = MVT::isInteger(LL.getValueType());
773 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
774 if (Result != ISD::SETCC_INVALID)
775 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
776 }
777 }
778 // fold (or (zext x), (zext y)) -> (zext (or x, y))
779 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
780 N1.getOpcode() == ISD::ZERO_EXTEND &&
781 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
782 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
783 N0.getOperand(0), N1.getOperand(0));
784 WorkList.push_back(ORNode.Val);
785 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
786 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000787 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000788}
789
Nate Begeman83e75ec2005-09-06 04:43:02 +0000790SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000791 SDOperand N0 = N->getOperand(0);
792 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000793 SDOperand LHS, RHS, CC;
794 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
795 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000796 MVT::ValueType VT = N0.getValueType();
797
798 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000799 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000800 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000801 // canonicalize constant to RHS
802 if (N0C && !N1C) {
803 std::swap(N0, N1);
804 std::swap(N0C, N1C);
805 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000806 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000807 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000808 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000809 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000810 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
811 bool isInt = MVT::isInteger(LHS.getValueType());
812 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
813 isInt);
814 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000815 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000816 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000817 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000818 assert(0 && "Unhandled SetCC Equivalent!");
819 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000820 }
Nate Begeman99801192005-09-07 23:25:52 +0000821 // fold !(x or y) -> (!x and !y) iff x or y are setcc
822 if (N1C && N1C->getValue() == 1 &&
823 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000824 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000825 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
826 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000827 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
828 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000829 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
830 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000831 }
832 }
Nate Begeman99801192005-09-07 23:25:52 +0000833 // fold !(x or y) -> (!x and !y) iff x or y are constants
834 if (N1C && N1C->isAllOnesValue() &&
835 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000836 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000837 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
838 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000839 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
840 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000841 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
842 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000843 }
844 }
Nate Begeman223df222005-09-08 20:18:10 +0000845 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
846 if (N1C && N0.getOpcode() == ISD::XOR) {
847 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
848 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
849 if (N00C)
850 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
851 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
852 if (N01C)
853 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
854 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
855 }
856 // fold (xor x, x) -> 0
857 if (N0 == N1)
858 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000859 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
860 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
861 N1.getOpcode() == ISD::ZERO_EXTEND &&
862 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
863 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
864 N0.getOperand(0), N1.getOperand(0));
865 WorkList.push_back(XORNode.Val);
866 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
867 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000868 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000869}
870
Nate Begeman83e75ec2005-09-06 04:43:02 +0000871SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000872 SDOperand N0 = N->getOperand(0);
873 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000874 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
875 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000876 MVT::ValueType VT = N0.getValueType();
877 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
878
879 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000880 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000881 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000883 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000884 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000885 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000886 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000887 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000888 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000889 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000890 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000891 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000892 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
893 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000894 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000895 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000896 N0.getOperand(1).getOpcode() == ISD::Constant) {
897 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000898 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000899 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000900 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000901 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000902 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000903 }
904 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
905 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000906 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000907 N0.getOperand(1).getOpcode() == ISD::Constant) {
908 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000909 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000910 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
911 DAG.getConstant(~0ULL << c1, VT));
912 if (c2 > c1)
913 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000914 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000915 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000916 return DAG.getNode(ISD::SRL, VT, Mask,
917 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 }
919 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000920 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000921 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000922 DAG.getConstant(~0ULL << N1C->getValue(), VT));
923 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000924}
925
Nate Begeman83e75ec2005-09-06 04:43:02 +0000926SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000927 SDOperand N0 = N->getOperand(0);
928 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000929 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
930 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000931 MVT::ValueType VT = N0.getValueType();
932 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
933
934 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000935 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000936 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000938 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000939 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000940 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000941 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000942 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000943 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000944 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000945 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000946 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000947 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000948 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000949 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000950 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000951 return DAG.getNode(ISD::SRL, VT, N0, N1);
952 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000953}
954
Nate Begeman83e75ec2005-09-06 04:43:02 +0000955SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000956 SDOperand N0 = N->getOperand(0);
957 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
959 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000960 MVT::ValueType VT = N0.getValueType();
961 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
962
963 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000964 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000965 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000966 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000968 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000969 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000970 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000971 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000972 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000974 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000976 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
977 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000979 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980 N0.getOperand(1).getOpcode() == ISD::Constant) {
981 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000982 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000984 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000985 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000986 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000987 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000988 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000989}
990
Nate Begeman83e75ec2005-09-06 04:43:02 +0000991SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000992 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000993 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994
995 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000996 if (N0C)
997 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000998 N0.getValueType());
999 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000}
1001
Nate Begeman83e75ec2005-09-06 04:43:02 +00001002SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001003 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001004 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001005
1006 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001007 if (N0C)
1008 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001009 N0.getValueType());
1010 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011}
1012
Nate Begeman83e75ec2005-09-06 04:43:02 +00001013SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001015 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016
1017 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001018 if (N0C)
1019 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001020 N0.getValueType());
1021 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001022}
1023
Nate Begeman452d7be2005-09-16 00:54:12 +00001024SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1025 SDOperand N0 = N->getOperand(0);
1026 SDOperand N1 = N->getOperand(1);
1027 SDOperand N2 = N->getOperand(2);
1028 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1029 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1030 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1031 MVT::ValueType VT = N->getValueType(0);
1032
1033 // fold select C, X, X -> X
1034 if (N1 == N2)
1035 return N1;
1036 // fold select true, X, Y -> X
1037 if (N0C && !N0C->isNullValue())
1038 return N1;
1039 // fold select false, X, Y -> Y
1040 if (N0C && N0C->isNullValue())
1041 return N2;
1042 // fold select C, 1, X -> C | X
1043 if (MVT::i1 == VT && N1C && !N1C->isNullValue())
1044 return DAG.getNode(ISD::OR, VT, N0, N2);
1045 // fold select C, 0, X -> ~C & X
1046 // FIXME: this should check for C type == X type, not i1?
1047 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1048 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1049 WorkList.push_back(XORNode.Val);
1050 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1051 }
1052 // fold select C, X, 1 -> ~C | X
1053 if (MVT::i1 == VT && N2C && !N2C->isNullValue()) {
1054 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1055 WorkList.push_back(XORNode.Val);
1056 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1057 }
1058 // fold select C, X, 0 -> C & X
1059 // FIXME: this should check for C type == X type, not i1?
1060 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1061 return DAG.getNode(ISD::AND, VT, N0, N1);
1062 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1063 if (MVT::i1 == VT && N0 == N1)
1064 return DAG.getNode(ISD::OR, VT, N0, N2);
1065 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1066 if (MVT::i1 == VT && N0 == N2)
1067 return DAG.getNode(ISD::AND, VT, N0, N1);
1068
1069 return SDOperand();
1070}
1071
1072SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
1073 return SDOperand();
1074}
1075
1076SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1077 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1078 cast<CondCodeSDNode>(N->getOperand(2))->get());
1079}
1080
Nate Begeman83e75ec2005-09-06 04:43:02 +00001081SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001082 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001083 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001084 MVT::ValueType VT = N->getValueType(0);
1085
Nate Begeman1d4d4142005-09-01 00:19:25 +00001086 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001087 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001088 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001089 // fold (sext (sext x)) -> (sext x)
1090 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001091 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1092 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001093}
1094
Nate Begeman83e75ec2005-09-06 04:43:02 +00001095SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001096 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001097 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001098 MVT::ValueType VT = N->getValueType(0);
1099
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001101 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001102 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 // fold (zext (zext x)) -> (zext x)
1104 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001105 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1106 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001107}
1108
Nate Begeman83e75ec2005-09-06 04:43:02 +00001109SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001112 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001113 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001114 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001115
Nate Begeman1d4d4142005-09-01 00:19:25 +00001116 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001117 if (N0C) {
1118 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001119 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001120 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001121 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001122 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +00001123 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001124 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001125 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001126 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1127 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1128 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001129 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001130 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001131 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1132 if (N0.getOpcode() == ISD::AssertSext &&
1133 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001134 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 }
1136 // fold (sext_in_reg (sextload x)) -> (sextload x)
1137 if (N0.getOpcode() == ISD::SEXTLOAD &&
1138 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001139 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001140 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001141 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001142 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +00001143 if (N0.getOpcode() == ISD::SETCC &&
1144 TLI.getSetCCResultContents() ==
1145 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001146 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001147 // FIXME: this code is currently just ported over from SelectionDAG.cpp
1148 // we probably actually want to handle this in two pieces. Rather than
1149 // checking all the top bits for zero, just check the sign bit here and turn
1150 // it into a zero extend inreg (AND with constant).
1151 // then, let the code for AND figure out if the mask is superfluous rather
1152 // than doing so here.
1153 if (N0.getOpcode() == ISD::AND &&
1154 N0.getOperand(1).getOpcode() == ISD::Constant) {
1155 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1156 unsigned NumBits = MVT::getSizeInBits(EVT);
1157 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001158 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001159 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001160 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161}
1162
Nate Begeman83e75ec2005-09-06 04:43:02 +00001163SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001164 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001165 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001166 MVT::ValueType VT = N->getValueType(0);
1167
1168 // noop truncate
1169 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001170 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001173 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001174 // fold (truncate (truncate x)) -> (truncate x)
1175 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001176 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001177 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1178 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1179 if (N0.getValueType() < VT)
1180 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001181 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001182 else if (N0.getValueType() > VT)
1183 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001184 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001185 else
1186 // if the source and dest are the same type, we can drop both the extend
1187 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001188 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001189 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001190 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001191}
1192
Nate Begeman83e75ec2005-09-06 04:43:02 +00001193SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001194 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001195 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196
1197 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001198 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001199 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1200 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001201}
1202
Nate Begeman83e75ec2005-09-06 04:43:02 +00001203SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001204 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001205 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001206
1207 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001208 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001209 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1210 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001211}
1212
Nate Begeman83e75ec2005-09-06 04:43:02 +00001213SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001214 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001215
1216 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001217 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001218 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1219 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220}
1221
Nate Begeman83e75ec2005-09-06 04:43:02 +00001222SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001223 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001224
1225 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001226 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001227 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1228 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001229}
1230
Nate Begeman83e75ec2005-09-06 04:43:02 +00001231SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001232 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001233
1234 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001235 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001236 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1237 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001238}
1239
Nate Begeman83e75ec2005-09-06 04:43:02 +00001240SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001241 SDOperand N0 = N->getOperand(0);
1242 MVT::ValueType VT = N->getValueType(0);
1243 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001244 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001245
Nate Begeman1d4d4142005-09-01 00:19:25 +00001246 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001247 if (N0CFP) {
1248 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001249 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001250 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001251 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001252}
1253
Nate Begeman83e75ec2005-09-06 04:43:02 +00001254SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001255 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001256
1257 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001258 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001259 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1260 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001261}
1262
Nate Begeman83e75ec2005-09-06 04:43:02 +00001263SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001264 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001265 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001266 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001267 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001268 // fold (neg (sub x, y)) -> (sub y, x)
1269 if (N->getOperand(0).getOpcode() == ISD::SUB)
1270 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001271 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001272 // fold (neg (neg x)) -> x
1273 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001274 return N->getOperand(0).getOperand(0);
1275 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001276}
1277
Nate Begeman83e75ec2005-09-06 04:43:02 +00001278SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001279 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001280 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001281 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001282 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001283 // fold (fabs (fabs x)) -> (fabs x)
1284 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001285 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001286 // fold (fabs (fneg x)) -> (fabs x)
1287 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1288 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001289 N->getOperand(0).getOperand(0));
1290 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001291}
1292
Nate Begeman452d7be2005-09-16 00:54:12 +00001293SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
1294 SDOperand N1, ISD::CondCode Cond) {
1295 // These setcc operations always fold.
1296 switch (Cond) {
1297 default: break;
1298 case ISD::SETFALSE:
1299 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1300 case ISD::SETTRUE:
1301 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1302 }
1303
1304 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1305 uint64_t C1 = N1C->getValue();
1306 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1307 uint64_t C0 = N0C->getValue();
1308
1309 // Sign extend the operands if required
1310 if (ISD::isSignedIntSetCC(Cond)) {
1311 C0 = N0C->getSignExtended();
1312 C1 = N1C->getSignExtended();
1313 }
1314
1315 switch (Cond) {
1316 default: assert(0 && "Unknown integer setcc!");
1317 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1318 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1319 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1320 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1321 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1322 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1323 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1324 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1325 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1326 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1327 }
1328 } else {
1329 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1330 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1331 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1332
1333 // If the comparison constant has bits in the upper part, the
1334 // zero-extended value could never match.
1335 if (C1 & (~0ULL << InSize)) {
1336 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1337 switch (Cond) {
1338 case ISD::SETUGT:
1339 case ISD::SETUGE:
1340 case ISD::SETEQ: return DAG.getConstant(0, VT);
1341 case ISD::SETULT:
1342 case ISD::SETULE:
1343 case ISD::SETNE: return DAG.getConstant(1, VT);
1344 case ISD::SETGT:
1345 case ISD::SETGE:
1346 // True if the sign bit of C1 is set.
1347 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
1348 case ISD::SETLT:
1349 case ISD::SETLE:
1350 // True if the sign bit of C1 isn't set.
1351 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
1352 default:
1353 break;
1354 }
1355 }
1356
1357 // Otherwise, we can perform the comparison with the low bits.
1358 switch (Cond) {
1359 case ISD::SETEQ:
1360 case ISD::SETNE:
1361 case ISD::SETUGT:
1362 case ISD::SETUGE:
1363 case ISD::SETULT:
1364 case ISD::SETULE:
1365 return DAG.getSetCC(VT, N0.getOperand(0),
1366 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
1367 Cond);
1368 default:
1369 break; // todo, be more careful with signed comparisons
1370 }
1371 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1372 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1373 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1374 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1375 MVT::ValueType ExtDstTy = N0.getValueType();
1376 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1377
1378 // If the extended part has any inconsistent bits, it cannot ever
1379 // compare equal. In other words, they have to be all ones or all
1380 // zeros.
1381 uint64_t ExtBits =
1382 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1383 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1384 return DAG.getConstant(Cond == ISD::SETNE, VT);
1385
1386 SDOperand ZextOp;
1387 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1388 if (Op0Ty == ExtSrcTy) {
1389 ZextOp = N0.getOperand(0);
1390 } else {
1391 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1392 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1393 DAG.getConstant(Imm, Op0Ty));
1394 }
1395 WorkList.push_back(ZextOp.Val);
1396 // Otherwise, make this a use of a zext.
1397 return DAG.getSetCC(VT, ZextOp,
1398 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1399 ExtDstTy),
1400 Cond);
1401 }
1402
1403 uint64_t MinVal, MaxVal;
1404 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1405 if (ISD::isSignedIntSetCC(Cond)) {
1406 MinVal = 1ULL << (OperandBitSize-1);
1407 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1408 MaxVal = ~0ULL >> (65-OperandBitSize);
1409 else
1410 MaxVal = 0;
1411 } else {
1412 MinVal = 0;
1413 MaxVal = ~0ULL >> (64-OperandBitSize);
1414 }
1415
1416 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1417 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1418 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1419 --C1; // X >= C0 --> X > (C0-1)
1420 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1421 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1422 }
1423
1424 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1425 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1426 ++C1; // X <= C0 --> X < (C0+1)
1427 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1428 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1429 }
1430
1431 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1432 return DAG.getConstant(0, VT); // X < MIN --> false
1433
1434 // Canonicalize setgt X, Min --> setne X, Min
1435 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1436 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1437
1438 // If we have setult X, 1, turn it into seteq X, 0
1439 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1440 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1441 ISD::SETEQ);
1442 // If we have setugt X, Max-1, turn it into seteq X, Max
1443 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1444 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1445 ISD::SETEQ);
1446
1447 // If we have "setcc X, C0", check to see if we can shrink the immediate
1448 // by changing cc.
1449
1450 // SETUGT X, SINTMAX -> SETLT X, 0
1451 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1452 C1 == (~0ULL >> (65-OperandBitSize)))
1453 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1454 ISD::SETLT);
1455
1456 // FIXME: Implement the rest of these.
1457
1458 // Fold bit comparisons when we can.
1459 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1460 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1461 if (ConstantSDNode *AndRHS =
1462 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1463 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1464 // Perform the xform if the AND RHS is a single bit.
1465 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1466 return DAG.getNode(ISD::SRL, VT, N0,
1467 DAG.getConstant(Log2_64(AndRHS->getValue()),
1468 TLI.getShiftAmountTy()));
1469 }
1470 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1471 // (X & 8) == 8 --> (X & 8) >> 3
1472 // Perform the xform if C1 is a single bit.
1473 if ((C1 & (C1-1)) == 0) {
1474 return DAG.getNode(ISD::SRL, VT, N0,
1475 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
1476 }
1477 }
1478 }
1479 }
1480 } else if (isa<ConstantSDNode>(N0.Val)) {
1481 // Ensure that the constant occurs on the RHS.
1482 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1483 }
1484
1485 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
1486 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1487 double C0 = N0C->getValue(), C1 = N1C->getValue();
1488
1489 switch (Cond) {
1490 default: break; // FIXME: Implement the rest of these!
1491 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1492 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1493 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
1494 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
1495 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
1496 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
1497 }
1498 } else {
1499 // Ensure that the constant occurs on the RHS.
1500 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1501 }
1502
1503 if (N0 == N1) {
1504 // We can always fold X == Y for integer setcc's.
1505 if (MVT::isInteger(N0.getValueType()))
1506 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1507 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1508 if (UOF == 2) // FP operators that are undefined on NaNs.
1509 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1510 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1511 return DAG.getConstant(UOF, VT);
1512 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1513 // if it is not already.
1514 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
1515 if (NewCond != Cond)
1516 return DAG.getSetCC(VT, N0, N1, NewCond);
1517 }
1518
1519 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1520 MVT::isInteger(N0.getValueType())) {
1521 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1522 N0.getOpcode() == ISD::XOR) {
1523 // Simplify (X+Y) == (X+Z) --> Y == Z
1524 if (N0.getOpcode() == N1.getOpcode()) {
1525 if (N0.getOperand(0) == N1.getOperand(0))
1526 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1527 if (N0.getOperand(1) == N1.getOperand(1))
1528 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1529 if (isCommutativeBinOp(N0.getOpcode())) {
1530 // If X op Y == Y op X, try other combinations.
1531 if (N0.getOperand(0) == N1.getOperand(1))
1532 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1533 if (N0.getOperand(1) == N1.getOperand(0))
1534 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1535 }
1536 }
1537
1538 // Simplify (X+Z) == X --> Z == 0
1539 if (N0.getOperand(0) == N1)
1540 return DAG.getSetCC(VT, N0.getOperand(1),
1541 DAG.getConstant(0, N0.getValueType()), Cond);
1542 if (N0.getOperand(1) == N1) {
1543 if (isCommutativeBinOp(N0.getOpcode()))
1544 return DAG.getSetCC(VT, N0.getOperand(0),
1545 DAG.getConstant(0, N0.getValueType()), Cond);
1546 else {
1547 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1548 // (Z-X) == X --> Z == X<<1
1549 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1550 N1,
1551 DAG.getConstant(1,TLI.getShiftAmountTy()));
1552 WorkList.push_back(SH.Val);
1553 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1554 }
1555 }
1556 }
1557
1558 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1559 N1.getOpcode() == ISD::XOR) {
1560 // Simplify X == (X+Z) --> Z == 0
1561 if (N1.getOperand(0) == N0) {
1562 return DAG.getSetCC(VT, N1.getOperand(1),
1563 DAG.getConstant(0, N1.getValueType()), Cond);
1564 } else if (N1.getOperand(1) == N0) {
1565 if (isCommutativeBinOp(N1.getOpcode())) {
1566 return DAG.getSetCC(VT, N1.getOperand(0),
1567 DAG.getConstant(0, N1.getValueType()), Cond);
1568 } else {
1569 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1570 // X == (Z-X) --> X<<1 == Z
1571 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1572 DAG.getConstant(1,TLI.getShiftAmountTy()));
1573 WorkList.push_back(SH.Val);
1574 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1575 }
1576 }
1577 }
1578 }
1579
1580 // Fold away ALL boolean setcc's.
1581 SDOperand Temp;
1582 if (N0.getValueType() == MVT::i1) {
1583 switch (Cond) {
1584 default: assert(0 && "Unknown integer setcc!");
1585 case ISD::SETEQ: // X == Y -> (X^Y)^1
1586 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1587 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1588 WorkList.push_back(Temp.Val);
1589 break;
1590 case ISD::SETNE: // X != Y --> (X^Y)
1591 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1592 break;
1593 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1594 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1595 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1596 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1597 WorkList.push_back(Temp.Val);
1598 break;
1599 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1600 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1601 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1602 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1603 WorkList.push_back(Temp.Val);
1604 break;
1605 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1606 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1607 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1608 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1609 WorkList.push_back(Temp.Val);
1610 break;
1611 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
1612 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
1613 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1614 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
1615 break;
1616 }
1617 if (VT != MVT::i1) {
1618 WorkList.push_back(N0.Val);
1619 // FIXME: If running after legalize, we probably can't do this.
1620 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1621 }
1622 return N0;
1623 }
1624
1625 // Could not fold it.
1626 return SDOperand();
1627}
1628
Nate Begeman1d4d4142005-09-01 00:19:25 +00001629// SelectionDAG::Combine - This is the entry point for the file.
1630//
Nate Begeman4ebd8052005-09-01 23:24:04 +00001631void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001632 /// run - This is the main entry point to this class.
1633 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00001634 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001635}