blob: bffc176e40ec21e7603b2c636f487c1274e57042 [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"
43#include <cmath>
44using namespace llvm;
45
46namespace {
47 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
48
49 class DAGCombiner {
50 SelectionDAG &DAG;
51 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000052 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000053
54 // Worklist of all of the nodes that need to be simplified.
55 std::vector<SDNode*> WorkList;
56
57 /// AddUsersToWorkList - When an instruction is simplified, add all users of
58 /// the instruction to the work lists because they might get more simplified
59 /// now.
60 ///
61 void AddUsersToWorkList(SDNode *N) {
62 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000063 UI != UE; ++UI)
64 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000065 }
66
67 /// removeFromWorkList - remove all instances of N from the worklist.
68 void removeFromWorkList(SDNode *N) {
69 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
70 WorkList.end());
71 }
72
73 /// visit - call the node-specific routine that knows how to fold each
74 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +000075 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +000076
77 // Visitation implementation - Implement dag node combining for different
78 // node types. The semantics are as follows:
79 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +000080 // SDOperand.Val == 0 - No change was made
81 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +000082 //
Nate Begeman83e75ec2005-09-06 04:43:02 +000083 SDOperand visitTokenFactor(SDNode *N);
84 SDOperand visitADD(SDNode *N);
85 SDOperand visitSUB(SDNode *N);
86 SDOperand visitMUL(SDNode *N);
87 SDOperand visitSDIV(SDNode *N);
88 SDOperand visitUDIV(SDNode *N);
89 SDOperand visitSREM(SDNode *N);
90 SDOperand visitUREM(SDNode *N);
91 SDOperand visitMULHU(SDNode *N);
92 SDOperand visitMULHS(SDNode *N);
93 SDOperand visitAND(SDNode *N);
94 SDOperand visitOR(SDNode *N);
95 SDOperand visitXOR(SDNode *N);
96 SDOperand visitSHL(SDNode *N);
97 SDOperand visitSRA(SDNode *N);
98 SDOperand visitSRL(SDNode *N);
99 SDOperand visitCTLZ(SDNode *N);
100 SDOperand visitCTTZ(SDNode *N);
101 SDOperand visitCTPOP(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000102 // select
103 // select_cc
104 // setcc
Nate Begeman83e75ec2005-09-06 04:43:02 +0000105 SDOperand visitSIGN_EXTEND(SDNode *N);
106 SDOperand visitZERO_EXTEND(SDNode *N);
107 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
108 SDOperand visitTRUNCATE(SDNode *N);
109 SDOperand visitSINT_TO_FP(SDNode *N);
110 SDOperand visitUINT_TO_FP(SDNode *N);
111 SDOperand visitFP_TO_SINT(SDNode *N);
112 SDOperand visitFP_TO_UINT(SDNode *N);
113 SDOperand visitFP_ROUND(SDNode *N);
114 SDOperand visitFP_ROUND_INREG(SDNode *N);
115 SDOperand visitFP_EXTEND(SDNode *N);
116 SDOperand visitFNEG(SDNode *N);
117 SDOperand visitFABS(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000118 // brcond
119 // brcondtwoway
120 // br_cc
121 // brtwoway_cc
122public:
123 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000124 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000125
126 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000127 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000128 };
129}
130
131/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
132/// this predicate to simplify operations downstream. V and Mask are known to
133/// be the same type.
134static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
135 const TargetLowering &TLI) {
136 unsigned SrcBits;
137 if (Mask == 0) return true;
138
139 // If we know the result of a setcc has the top bits zero, use this info.
140 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000141 case ISD::Constant:
142 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
143 case ISD::SETCC:
Nate Begeman646d7e22005-09-02 21:18:40 +0000144 // FIXME: teach this about non ZeroOrOne values, such as 0 or -1
Nate Begeman4ebd8052005-09-01 23:24:04 +0000145 return ((Mask & 1) == 0) &&
146 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
147 case ISD::ZEXTLOAD:
148 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
149 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
150 case ISD::ZERO_EXTEND:
151 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
152 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
153 case ISD::AssertZext:
154 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
155 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
156 case ISD::AND:
157 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
158 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
159 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
160 // FALL THROUGH
161 case ISD::OR:
162 case ISD::XOR:
163 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
164 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
165 case ISD::SELECT:
166 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
167 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
168 case ISD::SELECT_CC:
169 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
170 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
171 case ISD::SRL:
172 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
173 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
174 uint64_t NewVal = Mask << ShAmt->getValue();
175 SrcBits = MVT::getSizeInBits(Op.getValueType());
176 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
177 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
178 }
179 return false;
180 case ISD::SHL:
181 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
182 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
183 uint64_t NewVal = Mask >> ShAmt->getValue();
184 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
185 }
186 return false;
187 case ISD::CTTZ:
188 case ISD::CTLZ:
189 case ISD::CTPOP:
190 // Bit counting instructions can not set the high bits of the result
191 // register. The max number of bits sets depends on the input.
192 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
193
194 // TODO we could handle some SRA cases here.
195 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000196 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000197 return false;
198}
199
Nate Begeman4ebd8052005-09-01 23:24:04 +0000200// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
201// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000202// Also, set the incoming LHS, RHS, and CC references to the appropriate
203// nodes based on the type of node we are checking. This simplifies life a
204// bit for the callers.
205static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
206 SDOperand &CC) {
207 if (N.getOpcode() == ISD::SETCC) {
208 LHS = N.getOperand(0);
209 RHS = N.getOperand(1);
210 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000211 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000212 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000213 if (N.getOpcode() == ISD::SELECT_CC &&
214 N.getOperand(2).getOpcode() == ISD::Constant &&
215 N.getOperand(3).getOpcode() == ISD::Constant &&
216 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000217 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
218 LHS = N.getOperand(0);
219 RHS = N.getOperand(1);
220 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000221 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000222 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000223 return false;
224}
225
Nate Begeman99801192005-09-07 23:25:52 +0000226// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
227// one use. If this is true, it allows the users to invert the operation for
228// free when it is profitable to do so.
229static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000230 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000231 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000232 return true;
233 return false;
234}
235
236void DAGCombiner::Run(bool RunningAfterLegalize) {
237 // set the instance variable, so that the various visit routines may use it.
238 AfterLegalize = RunningAfterLegalize;
239
Nate Begeman646d7e22005-09-02 21:18:40 +0000240 // Add all the dag nodes to the worklist.
241 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000242
Nate Begeman1d4d4142005-09-01 00:19:25 +0000243 // while the worklist isn't empty, inspect the node on the end of it and
244 // try and combine it.
245 while (!WorkList.empty()) {
246 SDNode *N = WorkList.back();
247 WorkList.pop_back();
248
249 // If N has no uses, it is dead. Make sure to revisit all N's operands once
250 // N is deleted from the DAG, since they too may now be dead.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000251 // FIXME: is there a better way to keep from deleting the dag root because
252 // we think it has no uses? This works for now...
253 if (N->use_empty() && N != DAG.getRoot().Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000254 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
255 WorkList.push_back(N->getOperand(i).Val);
256
257 DAG.DeleteNode(N);
258 removeFromWorkList(N);
259 continue;
260 }
261
Nate Begeman83e75ec2005-09-06 04:43:02 +0000262 SDOperand RV = visit(N);
263 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000264 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000265 // If we get back the same node we passed in, rather than a new node or
266 // zero, we know that the node must have defined multiple values and
267 // CombineTo was used. Since CombineTo takes care of the worklist
268 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000269 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000270 DEBUG(std::cerr << "\nReplacing "; N->dump();
271 std::cerr << "\nWith: "; RV.Val->dump();
272 std::cerr << '\n');
Nate Begeman99801192005-09-07 23:25:52 +0000273 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV));
Nate Begeman646d7e22005-09-02 21:18:40 +0000274
275 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000276 WorkList.push_back(RV.Val);
277 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000278
279 // Nodes can end up on the worklist more than once. Make sure we do
280 // not process a node that has been replaced.
281 removeFromWorkList(N);
282 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000283 }
284 }
285}
286
Nate Begeman83e75ec2005-09-06 04:43:02 +0000287SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000288 switch(N->getOpcode()) {
289 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000290 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000291 case ISD::ADD: return visitADD(N);
292 case ISD::SUB: return visitSUB(N);
293 case ISD::MUL: return visitMUL(N);
294 case ISD::SDIV: return visitSDIV(N);
295 case ISD::UDIV: return visitUDIV(N);
296 case ISD::SREM: return visitSREM(N);
297 case ISD::UREM: return visitUREM(N);
298 case ISD::MULHU: return visitMULHU(N);
299 case ISD::MULHS: return visitMULHS(N);
300 case ISD::AND: return visitAND(N);
301 case ISD::OR: return visitOR(N);
302 case ISD::XOR: return visitXOR(N);
303 case ISD::SHL: return visitSHL(N);
304 case ISD::SRA: return visitSRA(N);
305 case ISD::SRL: return visitSRL(N);
306 case ISD::CTLZ: return visitCTLZ(N);
307 case ISD::CTTZ: return visitCTTZ(N);
308 case ISD::CTPOP: return visitCTPOP(N);
309 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
310 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
311 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
312 case ISD::TRUNCATE: return visitTRUNCATE(N);
313 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
314 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
315 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
316 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
317 case ISD::FP_ROUND: return visitFP_ROUND(N);
318 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
319 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
320 case ISD::FNEG: return visitFNEG(N);
321 case ISD::FABS: return visitFABS(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000322 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000323 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000324}
325
Nate Begeman83e75ec2005-09-06 04:43:02 +0000326SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000327 // If the token factor has two operands and one is the entry token, replace
328 // the token factor with the other operand.
329 if (N->getNumOperands() == 2) {
330 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000331 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000332 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000333 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000334 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000335 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000336}
337
Nate Begeman83e75ec2005-09-06 04:43:02 +0000338SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000339 SDOperand N0 = N->getOperand(0);
340 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000341 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
342 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
343 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
344 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000345 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000346
347 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000348 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000349 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000350 // canonicalize constant to RHS
351 if (N0C && !N1C) {
352 std::swap(N0, N1);
353 std::swap(N0C, N1C);
354 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000355 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000356 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000357 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000358 // fold floating point (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000359 if (N0CFP && N1CFP)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000360 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), VT);
Nate Begeman223df222005-09-08 20:18:10 +0000361 // fold (add (add x, c1), c2) -> (add x, c1+c2)
362 if (N1C && N0.getOpcode() == ISD::ADD) {
363 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
364 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
365 if (N00C)
366 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
367 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
368 if (N01C)
369 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
370 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
371 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000372 // fold (A + (-B)) -> A-B
373 if (N1.getOpcode() == ISD::FNEG)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000374 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000375 // fold ((-A) + B) -> B-A
376 if (N0.getOpcode() == ISD::FNEG)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000377 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000378 // fold ((0-A) + B) -> B-A
379 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
380 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000381 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000382 // fold (A + (0-B)) -> A-B
383 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
384 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000385 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000386 // fold (A+(B-A)) -> B for non-fp types
387 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1) &&
388 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000389 return N1.getOperand(0);
390 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000391}
392
Nate Begeman83e75ec2005-09-06 04:43:02 +0000393SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000394 SDOperand N0 = N->getOperand(0);
395 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000396 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
397 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
398 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
399 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000400
401 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000402 if (N0C && N1C)
403 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000404 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000405 // fold (sub x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000406 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000407 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000408 // fold floating point (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000409 if (N0CFP && N1CFP)
410 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000411 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000412 // fold (A+B)-A -> B
413 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
414 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000415 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000416 // fold (A+B)-B -> A
417 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 &&
418 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000419 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000420 // fold (A-(-B)) -> A+B
421 if (N1.getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000422 return DAG.getNode(ISD::ADD, N0.getValueType(), N0, N1.getOperand(0));
423 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000424}
425
Nate Begeman83e75ec2005-09-06 04:43:02 +0000426SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000427 SDOperand N0 = N->getOperand(0);
428 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000429 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
430 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
431 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
432 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000433 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000434
435 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000436 if (N0C && N1C)
437 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000438 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000439 // canonicalize constant to RHS
440 if (N0C && !N1C) {
441 std::swap(N0, N1);
442 std::swap(N0C, N1C);
443 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000444 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000445 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000446 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000447 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000448 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000449 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000450 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000451 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000452 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000454 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000455 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000456 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
457 if (N1C && N0.getOpcode() == ISD::MUL) {
458 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
459 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
460 if (N00C)
461 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
462 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
463 if (N01C)
464 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
465 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
466 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000467 // fold floating point (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000468 if (N0CFP && N1CFP)
469 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000470 N->getValueType(0));
471 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000472}
473
Nate Begeman83e75ec2005-09-06 04:43:02 +0000474SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000475 SDOperand N0 = N->getOperand(0);
476 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000477 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
478 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
479 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
480 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000481
482 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 if (N0C && N1C && !N1C->isNullValue())
484 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000485 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000486 // fold floating point (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000487 if (N0CFP && N1CFP)
488 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000489 N->getValueType(0));
490 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000491}
492
Nate Begeman83e75ec2005-09-06 04:43:02 +0000493SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000494 SDOperand N0 = N->getOperand(0);
495 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000496 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
497 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000498
499 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000500 if (N0C && N1C && !N1C->isNullValue())
501 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000502 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000503 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000504 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000505 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000506 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000507 TLI.getShiftAmountTy()));
508 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000509}
510
Nate Begeman83e75ec2005-09-06 04:43:02 +0000511SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000512 SDOperand N0 = N->getOperand(0);
513 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000514 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
515 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
516 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
517 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000518
519 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000520 if (N0C && N1C && !N1C->isNullValue())
521 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000522 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000523 // fold floating point (srem c1, c2) -> fmod(c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000524 if (N0CFP && N1CFP)
525 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000526 N->getValueType(0));
527 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000528}
529
Nate Begeman83e75ec2005-09-06 04:43:02 +0000530SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000531 SDOperand N0 = N->getOperand(0);
532 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000533 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
534 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000535
536 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000537 if (N0C && N1C && !N1C->isNullValue())
538 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000539 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000540 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000541 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000542}
543
Nate Begeman83e75ec2005-09-06 04:43:02 +0000544SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000545 SDOperand N0 = N->getOperand(0);
546 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000547 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000548
549 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000550 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000551 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000552 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000553 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
555 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000556 TLI.getShiftAmountTy()));
557 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000558}
559
Nate Begeman83e75ec2005-09-06 04:43:02 +0000560SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000561 SDOperand N0 = N->getOperand(0);
562 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000563 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000564
565 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000566 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000567 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000569 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000570 return DAG.getConstant(0, N0.getValueType());
571 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572}
573
Nate Begeman83e75ec2005-09-06 04:43:02 +0000574SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000575 SDOperand N0 = N->getOperand(0);
576 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000577 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000578 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
579 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000581 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000582
583 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000584 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000585 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000586 // canonicalize constant to RHS
587 if (N0C && !N1C) {
588 std::swap(N0, N1);
589 std::swap(N0C, N1C);
590 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000591 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000592 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000593 return N0;
594 // if (and x, c) is known to be zero, return 0
595 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
596 return DAG.getConstant(0, VT);
597 // fold (and x, c) -> x iff (x & ~c) == 0
598 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
599 TLI))
600 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000601 // fold (and (and x, c1), c2) -> (and x, c1^c2)
602 if (N1C && N0.getOpcode() == ISD::AND) {
603 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
604 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
605 if (N00C)
606 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
607 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
608 if (N01C)
609 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
610 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
611 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000612 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
613 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
614 unsigned ExtendBits =
615 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000616 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000617 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 }
619 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
620 if (N0.getOpcode() == ISD::OR)
621 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000622 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000623 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000624 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
625 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
626 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
627 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
628
629 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
630 MVT::isInteger(LL.getValueType())) {
631 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
632 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
633 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
634 WorkList.push_back(ORNode.Val);
635 return DAG.getSetCC(VT, ORNode, LR, Op1);
636 }
637 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
638 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
639 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
640 WorkList.push_back(ANDNode.Val);
641 return DAG.getSetCC(VT, ANDNode, LR, Op1);
642 }
643 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
644 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
645 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
646 WorkList.push_back(ORNode.Val);
647 return DAG.getSetCC(VT, ORNode, LR, Op1);
648 }
649 }
650 // canonicalize equivalent to ll == rl
651 if (LL == RR && LR == RL) {
652 Op1 = ISD::getSetCCSwappedOperands(Op1);
653 std::swap(RL, RR);
654 }
655 if (LL == RL && LR == RR) {
656 bool isInteger = MVT::isInteger(LL.getValueType());
657 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
658 if (Result != ISD::SETCC_INVALID)
659 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
660 }
661 }
662 // fold (and (zext x), (zext y)) -> (zext (and x, y))
663 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
664 N1.getOpcode() == ISD::ZERO_EXTEND &&
665 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
666 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
667 N0.getOperand(0), N1.getOperand(0));
668 WorkList.push_back(ANDNode.Val);
669 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
670 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000671 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672}
673
Nate Begeman83e75ec2005-09-06 04:43:02 +0000674SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000675 SDOperand N0 = N->getOperand(0);
676 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000677 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000678 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
679 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000680 MVT::ValueType VT = N1.getValueType();
681 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000682
683 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000684 if (N0C && N1C)
685 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000686 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000687 // canonicalize constant to RHS
688 if (N0C && !N1C) {
689 std::swap(N0, N1);
690 std::swap(N0C, N1C);
691 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000692 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000693 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000694 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000695 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000696 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000697 return N1;
698 // fold (or x, c) -> c iff (x & ~c) == 0
699 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
700 TLI))
701 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000702 // fold (or (or x, c1), c2) -> (or x, c1|c2)
703 if (N1C && N0.getOpcode() == ISD::OR) {
704 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
705 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
706 if (N00C)
707 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
708 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
709 if (N01C)
710 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
711 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
712 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000713 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
714 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
715 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
716 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
717
718 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
719 MVT::isInteger(LL.getValueType())) {
720 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
721 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
722 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
723 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
724 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
725 WorkList.push_back(ORNode.Val);
726 return DAG.getSetCC(VT, ORNode, LR, Op1);
727 }
728 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
729 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
730 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
731 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
732 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
733 WorkList.push_back(ANDNode.Val);
734 return DAG.getSetCC(VT, ANDNode, LR, Op1);
735 }
736 }
737 // canonicalize equivalent to ll == rl
738 if (LL == RR && LR == RL) {
739 Op1 = ISD::getSetCCSwappedOperands(Op1);
740 std::swap(RL, RR);
741 }
742 if (LL == RL && LR == RR) {
743 bool isInteger = MVT::isInteger(LL.getValueType());
744 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
745 if (Result != ISD::SETCC_INVALID)
746 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
747 }
748 }
749 // fold (or (zext x), (zext y)) -> (zext (or x, y))
750 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
751 N1.getOpcode() == ISD::ZERO_EXTEND &&
752 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
753 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
754 N0.getOperand(0), N1.getOperand(0));
755 WorkList.push_back(ORNode.Val);
756 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
757 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000758 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000759}
760
Nate Begeman83e75ec2005-09-06 04:43:02 +0000761SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000762 SDOperand N0 = N->getOperand(0);
763 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000764 SDOperand LHS, RHS, CC;
765 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
766 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000767 MVT::ValueType VT = N0.getValueType();
768
769 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000770 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000771 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000772 // canonicalize constant to RHS
773 if (N0C && !N1C) {
774 std::swap(N0, N1);
775 std::swap(N0C, N1C);
776 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000777 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000778 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000779 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000780 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000781 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
782 bool isInt = MVT::isInteger(LHS.getValueType());
783 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
784 isInt);
785 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000786 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000787 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000788 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000789 assert(0 && "Unhandled SetCC Equivalent!");
790 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000791 }
Nate Begeman99801192005-09-07 23:25:52 +0000792 // fold !(x or y) -> (!x and !y) iff x or y are setcc
793 if (N1C && N1C->getValue() == 1 &&
794 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000795 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000796 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
797 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000798 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
799 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000800 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
801 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000802 }
803 }
Nate Begeman99801192005-09-07 23:25:52 +0000804 // fold !(x or y) -> (!x and !y) iff x or y are constants
805 if (N1C && N1C->isAllOnesValue() &&
806 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000807 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000808 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
809 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000810 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
811 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000812 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
813 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000814 }
815 }
Nate Begeman223df222005-09-08 20:18:10 +0000816 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
817 if (N1C && N0.getOpcode() == ISD::XOR) {
818 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
819 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
820 if (N00C)
821 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
822 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
823 if (N01C)
824 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
825 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
826 }
827 // fold (xor x, x) -> 0
828 if (N0 == N1)
829 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000830 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
831 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
832 N1.getOpcode() == ISD::ZERO_EXTEND &&
833 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
834 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
835 N0.getOperand(0), N1.getOperand(0));
836 WorkList.push_back(XORNode.Val);
837 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
838 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000839 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000840}
841
Nate Begeman83e75ec2005-09-06 04:43:02 +0000842SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000843 SDOperand N0 = N->getOperand(0);
844 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000845 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
846 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000847 MVT::ValueType VT = N0.getValueType();
848 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
849
850 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000851 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000852 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000854 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000855 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000856 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000857 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000858 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000859 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000860 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000861 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000862 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000863 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
864 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000866 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867 N0.getOperand(1).getOpcode() == ISD::Constant) {
868 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000869 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000871 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000872 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000873 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000874 }
875 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
876 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000877 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000878 N0.getOperand(1).getOpcode() == ISD::Constant) {
879 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000880 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000881 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
882 DAG.getConstant(~0ULL << c1, VT));
883 if (c2 > c1)
884 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000885 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000886 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000887 return DAG.getNode(ISD::SRL, VT, Mask,
888 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000889 }
890 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000891 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000892 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000893 DAG.getConstant(~0ULL << N1C->getValue(), VT));
894 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000895}
896
Nate Begeman83e75ec2005-09-06 04:43:02 +0000897SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000898 SDOperand N0 = N->getOperand(0);
899 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000900 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
901 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000902 MVT::ValueType VT = N0.getValueType();
903 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
904
905 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000906 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000907 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000908 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000909 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000912 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000915 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000916 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000917 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000918 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000919 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000920 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000921 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000922 return DAG.getNode(ISD::SRL, VT, N0, N1);
923 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000924}
925
Nate Begeman83e75ec2005-09-06 04:43:02 +0000926SDOperand DAGCombiner::visitSRL(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 (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000935 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000936 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937 // fold (srl 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 (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000941 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000942 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000943 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000944 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000945 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000946 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000947 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
948 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000949 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000950 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951 N0.getOperand(1).getOpcode() == ISD::Constant) {
952 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000953 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000954 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000955 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000956 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000957 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000958 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000959 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000960}
961
Nate Begeman83e75ec2005-09-06 04:43:02 +0000962SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000964 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000965
966 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 if (N0C)
968 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000969 N0.getValueType());
970 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971}
972
Nate Begeman83e75ec2005-09-06 04:43:02 +0000973SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000975 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000976
977 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000978 if (N0C)
979 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000980 N0.getValueType());
981 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000982}
983
Nate Begeman83e75ec2005-09-06 04:43:02 +0000984SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000985 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000986 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000987
988 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000989 if (N0C)
990 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000991 N0.getValueType());
992 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000993}
994
Nate Begeman83e75ec2005-09-06 04:43:02 +0000995SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000996 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000997 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998 MVT::ValueType VT = N->getValueType(0);
999
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001001 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001002 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001003 // fold (sext (sext x)) -> (sext x)
1004 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001005 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1006 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001007}
1008
Nate Begeman83e75ec2005-09-06 04:43:02 +00001009SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001011 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001012 MVT::ValueType VT = N->getValueType(0);
1013
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001015 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001016 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017 // fold (zext (zext x)) -> (zext x)
1018 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001019 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1020 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001021}
1022
Nate Begeman83e75ec2005-09-06 04:43:02 +00001023SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001024 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001025 SDOperand N1 = N->getOperand(1);
1026 SDOperand LHS, RHS, CC;
1027 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001028 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001029 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001030
Nate Begeman1d4d4142005-09-01 00:19:25 +00001031 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001032 if (N0C) {
1033 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001034 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001035 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001036 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001037 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +00001038 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001039 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001040 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001041 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1042 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1043 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001044 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001045 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001046 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1047 if (N0.getOpcode() == ISD::AssertSext &&
1048 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001049 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001050 }
1051 // fold (sext_in_reg (sextload x)) -> (sextload x)
1052 if (N0.getOpcode() == ISD::SEXTLOAD &&
1053 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001054 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001055 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001056 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001057 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +00001058 if (N0.getOpcode() == ISD::SETCC &&
1059 TLI.getSetCCResultContents() ==
1060 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001061 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001062 // FIXME: this code is currently just ported over from SelectionDAG.cpp
1063 // we probably actually want to handle this in two pieces. Rather than
1064 // checking all the top bits for zero, just check the sign bit here and turn
1065 // it into a zero extend inreg (AND with constant).
1066 // then, let the code for AND figure out if the mask is superfluous rather
1067 // than doing so here.
1068 if (N0.getOpcode() == ISD::AND &&
1069 N0.getOperand(1).getOpcode() == ISD::Constant) {
1070 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1071 unsigned NumBits = MVT::getSizeInBits(EVT);
1072 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001073 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001074 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001075 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001076}
1077
Nate Begeman83e75ec2005-09-06 04:43:02 +00001078SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001079 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001080 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001081 MVT::ValueType VT = N->getValueType(0);
1082
1083 // noop truncate
1084 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001085 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001086 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001087 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001088 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001089 // fold (truncate (truncate x)) -> (truncate x)
1090 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001091 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1093 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1094 if (N0.getValueType() < VT)
1095 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001096 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001097 else if (N0.getValueType() > VT)
1098 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001099 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100 else
1101 // if the source and dest are the same type, we can drop both the extend
1102 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001103 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001104 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001105 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001106}
1107
Nate Begeman83e75ec2005-09-06 04:43:02 +00001108SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001109 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001110 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001111
1112 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001113 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001114 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1115 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001116}
1117
Nate Begeman83e75ec2005-09-06 04:43:02 +00001118SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001119 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001120 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001121
1122 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001123 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001124 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1125 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001126}
1127
Nate Begeman83e75ec2005-09-06 04:43:02 +00001128SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001129 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001130
1131 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001132 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001133 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1134 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135}
1136
Nate Begeman83e75ec2005-09-06 04:43:02 +00001137SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001138 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139
1140 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001141 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001142 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1143 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001144}
1145
Nate Begeman83e75ec2005-09-06 04:43:02 +00001146SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001147 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001148
1149 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001150 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001151 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1152 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001153}
1154
Nate Begeman83e75ec2005-09-06 04:43:02 +00001155SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001156 SDOperand N0 = N->getOperand(0);
1157 MVT::ValueType VT = N->getValueType(0);
1158 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001159 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001162 if (N0CFP) {
1163 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001164 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001165 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001166 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001167}
1168
Nate Begeman83e75ec2005-09-06 04:43:02 +00001169SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001170 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171
1172 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001173 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001174 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1175 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001176}
1177
Nate Begeman83e75ec2005-09-06 04:43:02 +00001178SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001181 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001182 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001183 // fold (neg (sub x, y)) -> (sub y, x)
1184 if (N->getOperand(0).getOpcode() == ISD::SUB)
1185 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001186 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001187 // fold (neg (neg x)) -> x
1188 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001189 return N->getOperand(0).getOperand(0);
1190 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001191}
1192
Nate Begeman83e75ec2005-09-06 04:43:02 +00001193SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001194 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001195 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001196 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001197 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001198 // fold (fabs (fabs x)) -> (fabs x)
1199 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001200 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001201 // fold (fabs (fneg x)) -> (fabs x)
1202 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1203 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001204 N->getOperand(0).getOperand(0));
1205 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001206}
1207
Nate Begeman1d4d4142005-09-01 00:19:25 +00001208// SelectionDAG::Combine - This is the entry point for the file.
1209//
Nate Begeman4ebd8052005-09-01 23:24:04 +00001210void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001211 /// run - This is the main entry point to this class.
1212 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00001213 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214}