blob: d2501d81dd268437ed1e21612d8a208af251ae81 [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 Begeman4ebd8052005-09-01 23:24:04 +0000226// isInvertibleForFree - Return true if there is no cost to emitting the logical
227// inverse of this node.
228static bool isInvertibleForFree(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000229 SDOperand N0, N1, N2;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000230 if (isa<ConstantSDNode>(N.Val)) return true;
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 Begeman83e75ec2005-09-06 04:43:02 +0000273 DAG.ReplaceAllUsesWith(SDOperand(N, 0), 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 Begeman1d4d4142005-09-01 00:19:25 +0000345
346 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000347 if (N0C && N1C)
348 return DAG.getConstant(N0C->getValue() + N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000349 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000350 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000351 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000352 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000353 // fold floating point (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000354 if (N0CFP && N1CFP)
355 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000356 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000357 // fold (A + (-B)) -> A-B
358 if (N1.getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000359 return DAG.getNode(ISD::SUB, N->getValueType(0), N0, N1.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000360 // fold ((-A) + B) -> B-A
361 if (N0.getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000362 return DAG.getNode(ISD::SUB, N->getValueType(0), N1, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000363 // fold ((0-A) + B) -> B-A
364 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
365 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000366 return DAG.getNode(ISD::SUB, N->getValueType(0), N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000367 // fold (A + (0-B)) -> A-B
368 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
369 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000370 return DAG.getNode(ISD::SUB, N->getValueType(0), N0, N1.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000371 // fold (A+(B-A)) -> B for non-fp types
372 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1) &&
373 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000374 return N1.getOperand(0);
375 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000376}
377
Nate Begeman83e75ec2005-09-06 04:43:02 +0000378SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000379 SDOperand N0 = N->getOperand(0);
380 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000381 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
382 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
383 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
384 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000385
386 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000387 if (N0C && N1C)
388 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000389 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000390 // fold (sub x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000391 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000392 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000393 // fold floating point (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000394 if (N0CFP && N1CFP)
395 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000396 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000397 // fold (A+B)-A -> B
398 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
399 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000400 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000401 // fold (A+B)-B -> A
402 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 &&
403 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000404 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000405 // fold (A-(-B)) -> A+B
406 if (N1.getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000407 return DAG.getNode(ISD::ADD, N0.getValueType(), N0, N1.getOperand(0));
408 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000409}
410
Nate Begeman83e75ec2005-09-06 04:43:02 +0000411SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000412 SDOperand N0 = N->getOperand(0);
413 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000414 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
415 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
416 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
417 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000418
419 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000420 if (N0C && N1C)
421 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000422 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000423 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000424 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000425 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000426 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000427 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000428 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000429 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000430 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000431 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000432 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000433 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000434 TLI.getShiftAmountTy()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000435 // fold floating point (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000436 if (N0CFP && N1CFP)
437 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000438 N->getValueType(0));
439 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000440}
441
Nate Begeman83e75ec2005-09-06 04:43:02 +0000442SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000443 SDOperand N0 = N->getOperand(0);
444 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000445 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
446 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
447 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
448 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000449
450 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000451 if (N0C && N1C && !N1C->isNullValue())
452 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000453 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000454 // fold floating point (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000455 if (N0CFP && N1CFP)
456 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000457 N->getValueType(0));
458 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000459}
460
Nate Begeman83e75ec2005-09-06 04:43:02 +0000461SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000462 SDOperand N0 = N->getOperand(0);
463 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000464 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
465 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000466
467 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000468 if (N0C && N1C && !N1C->isNullValue())
469 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000470 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000471 // fold (udiv x, (1 << c)) -> x >>u 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::SRL, 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()));
476 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000477}
478
Nate Begeman83e75ec2005-09-06 04:43:02 +0000479SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000480 SDOperand N0 = N->getOperand(0);
481 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000482 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
483 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
484 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
485 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000486
487 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 if (N0C && N1C && !N1C->isNullValue())
489 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000490 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000491 // fold floating point (srem c1, c2) -> fmod(c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000492 if (N0CFP && N1CFP)
493 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000494 N->getValueType(0));
495 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000496}
497
Nate Begeman83e75ec2005-09-06 04:43:02 +0000498SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000499 SDOperand N0 = N->getOperand(0);
500 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000501 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
502 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000503
504 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000505 if (N0C && N1C && !N1C->isNullValue())
506 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000507 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000508 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000509 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000510}
511
Nate Begeman83e75ec2005-09-06 04:43:02 +0000512SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000513 SDOperand N0 = N->getOperand(0);
514 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000515 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000516
517 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000518 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000519 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000520 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000521 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000522 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
523 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000524 TLI.getShiftAmountTy()));
525 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000526}
527
Nate Begeman83e75ec2005-09-06 04:43:02 +0000528SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000529 SDOperand N0 = N->getOperand(0);
530 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000531 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000532
533 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000534 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000535 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000536 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000537 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000538 return DAG.getConstant(0, N0.getValueType());
539 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000540}
541
Nate Begeman83e75ec2005-09-06 04:43:02 +0000542SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543 SDOperand N0 = N->getOperand(0);
544 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000545 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
546 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000547 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000548 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000549
550 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000551 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000552 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000554 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000555 return N0;
556 // if (and x, c) is known to be zero, return 0
557 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
558 return DAG.getConstant(0, VT);
559 // fold (and x, c) -> x iff (x & ~c) == 0
560 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
561 TLI))
562 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000563 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
564 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
565 unsigned ExtendBits =
566 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000567 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000568 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000569 }
570 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
571 if (N0.getOpcode() == ISD::OR)
572 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000573 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000574 return N1;
575 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000576}
577
Nate Begeman83e75ec2005-09-06 04:43:02 +0000578SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000579 SDOperand N0 = N->getOperand(0);
580 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000581 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
582 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000583 MVT::ValueType VT = N1.getValueType();
584 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000585
586 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000587 if (N0C && N1C)
588 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000589 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000590 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000591 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000592 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000593 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000594 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000595 return N1;
596 // fold (or x, c) -> c iff (x & ~c) == 0
597 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
598 TLI))
599 return N1;
600 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000601}
602
Nate Begeman83e75ec2005-09-06 04:43:02 +0000603SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000604 SDOperand N0 = N->getOperand(0);
605 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000606 SDOperand LHS, RHS, CC;
607 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
608 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000609 MVT::ValueType VT = N0.getValueType();
610
611 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000612 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000613 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000614 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000615 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000616 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000618 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
619 bool isInt = MVT::isInteger(LHS.getValueType());
620 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
621 isInt);
622 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000623 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000624 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000625 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000626 assert(0 && "Unhandled SetCC Equivalent!");
627 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000628 }
629 // fold !(x or y) -> (!x and !y) iff x or y are freely invertible
Nate Begeman646d7e22005-09-02 21:18:40 +0000630 if (N1C && N1C->isAllOnesValue() && N0.getOpcode() == ISD::OR) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
632 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
633 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
634 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman83e75ec2005-09-06 04:43:02 +0000635 return DAG.getNode(ISD::AND, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000636 }
637 }
638 // fold !(x and y) -> (!x or !y) iff x or y are freely invertible
Nate Begeman646d7e22005-09-02 21:18:40 +0000639 if (N1C && N1C->isAllOnesValue() && N0.getOpcode() == ISD::AND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000640 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
641 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
642 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
643 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman83e75ec2005-09-06 04:43:02 +0000644 return DAG.getNode(ISD::OR, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000645 }
646 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000647 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000648}
649
Nate Begeman83e75ec2005-09-06 04:43:02 +0000650SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651 SDOperand N0 = N->getOperand(0);
652 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000653 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
654 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000655 MVT::ValueType VT = N0.getValueType();
656 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
657
658 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000659 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000660 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000661 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000662 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000663 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000664 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000665 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000666 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000667 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000668 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000669 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000671 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
672 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000673 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000674 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000675 N0.getOperand(1).getOpcode() == ISD::Constant) {
676 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000677 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000678 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000679 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000680 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000681 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000682 }
683 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
684 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000685 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000686 N0.getOperand(1).getOpcode() == ISD::Constant) {
687 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000689 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
690 DAG.getConstant(~0ULL << c1, VT));
691 if (c2 > c1)
692 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000693 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000694 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000695 return DAG.getNode(ISD::SRL, VT, Mask,
696 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000697 }
698 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000700 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000701 DAG.getConstant(~0ULL << N1C->getValue(), VT));
702 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000703}
704
Nate Begeman83e75ec2005-09-06 04:43:02 +0000705SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000706 SDOperand N0 = N->getOperand(0);
707 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000708 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
709 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000710 MVT::ValueType VT = N0.getValueType();
711 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
712
713 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000715 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000716 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000717 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000718 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000719 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000720 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000721 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000722 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000723 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000724 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000725 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000726 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000727 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000728 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000729 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000730 return DAG.getNode(ISD::SRL, VT, N0, N1);
731 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000732}
733
Nate Begeman83e75ec2005-09-06 04:43:02 +0000734SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000735 SDOperand N0 = N->getOperand(0);
736 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000737 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
738 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000739 MVT::ValueType VT = N0.getValueType();
740 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
741
742 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000743 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000744 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000745 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000746 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000747 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000748 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000749 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000750 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000751 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000752 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000753 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000754 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000755 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
756 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000757 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000758 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000759 N0.getOperand(1).getOpcode() == ISD::Constant) {
760 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000761 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000762 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000763 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000764 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000765 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000766 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000767 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000768}
769
Nate Begeman83e75ec2005-09-06 04:43:02 +0000770SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000771 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000772 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000773
774 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000775 if (N0C)
776 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000777 N0.getValueType());
778 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000779}
780
Nate Begeman83e75ec2005-09-06 04:43:02 +0000781SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000782 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000783 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000784
785 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000786 if (N0C)
787 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000788 N0.getValueType());
789 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000790}
791
Nate Begeman83e75ec2005-09-06 04:43:02 +0000792SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000793 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000794 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000795
796 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000797 if (N0C)
798 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000799 N0.getValueType());
800 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000801}
802
Nate Begeman83e75ec2005-09-06 04:43:02 +0000803SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000804 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000805 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000806 MVT::ValueType VT = N->getValueType(0);
807
Nate Begeman1d4d4142005-09-01 00:19:25 +0000808 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000809 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000810 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000811 // fold (sext (sext x)) -> (sext x)
812 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000813 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
814 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000815}
816
Nate Begeman83e75ec2005-09-06 04:43:02 +0000817SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000818 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000819 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000820 MVT::ValueType VT = N->getValueType(0);
821
Nate Begeman1d4d4142005-09-01 00:19:25 +0000822 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000823 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000824 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000825 // fold (zext (zext x)) -> (zext x)
826 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000827 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
828 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000829}
830
Nate Begeman83e75ec2005-09-06 04:43:02 +0000831SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000832 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000833 SDOperand N1 = N->getOperand(1);
834 SDOperand LHS, RHS, CC;
835 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000836 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000837 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838
Nate Begeman1d4d4142005-09-01 00:19:25 +0000839 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000840 if (N0C) {
841 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000842 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000843 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000844 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +0000845 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000846 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000847 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000848 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000849 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
850 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
851 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000852 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000853 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000854 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
855 if (N0.getOpcode() == ISD::AssertSext &&
856 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000857 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000858 }
859 // fold (sext_in_reg (sextload x)) -> (sextload x)
860 if (N0.getOpcode() == ISD::SEXTLOAD &&
861 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000862 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000863 }
Nate Begeman4ebd8052005-09-01 23:24:04 +0000864 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000865 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +0000866 if (N0.getOpcode() == ISD::SETCC &&
867 TLI.getSetCCResultContents() ==
868 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870 // FIXME: this code is currently just ported over from SelectionDAG.cpp
871 // we probably actually want to handle this in two pieces. Rather than
872 // checking all the top bits for zero, just check the sign bit here and turn
873 // it into a zero extend inreg (AND with constant).
874 // then, let the code for AND figure out if the mask is superfluous rather
875 // than doing so here.
876 if (N0.getOpcode() == ISD::AND &&
877 N0.getOperand(1).getOpcode() == ISD::Constant) {
878 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
879 unsigned NumBits = MVT::getSizeInBits(EVT);
880 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000881 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000883 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000884}
885
Nate Begeman83e75ec2005-09-06 04:43:02 +0000886SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000887 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000888 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000889 MVT::ValueType VT = N->getValueType(0);
890
891 // noop truncate
892 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000893 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000894 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000895 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000896 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000897 // fold (truncate (truncate x)) -> (truncate x)
898 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000899 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000900 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
901 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
902 if (N0.getValueType() < VT)
903 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +0000904 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000905 else if (N0.getValueType() > VT)
906 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +0000907 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000908 else
909 // if the source and dest are the same type, we can drop both the extend
910 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +0000911 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000912 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914}
915
Nate Begeman83e75ec2005-09-06 04:43:02 +0000916SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000917 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000918 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919
920 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000921 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000922 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
923 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000924}
925
Nate Begeman83e75ec2005-09-06 04:43:02 +0000926SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000927 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000928 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000929
930 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000931 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000932 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
933 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934}
935
Nate Begeman83e75ec2005-09-06 04:43:02 +0000936SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000937 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938
939 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000940 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000941 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
942 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000943}
944
Nate Begeman83e75ec2005-09-06 04:43:02 +0000945SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000946 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000947
948 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000949 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000950 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
951 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000952}
953
Nate Begeman83e75ec2005-09-06 04:43:02 +0000954SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000955 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000956
957 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000959 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
960 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000961}
962
Nate Begeman83e75ec2005-09-06 04:43:02 +0000963SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000964 SDOperand N0 = N->getOperand(0);
965 MVT::ValueType VT = N->getValueType(0);
966 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000968
969 // noop fp_round_inreg
970 if (EVT == VT)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000971 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000972 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 if (N0CFP) {
974 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000975 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000976 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000977 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978}
979
Nate Begeman83e75ec2005-09-06 04:43:02 +0000980SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000981 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000982
983 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000984 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000985 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
986 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000987}
988
Nate Begeman83e75ec2005-09-06 04:43:02 +0000989SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000990 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000991 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000992 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000993 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994 // fold (neg (sub x, y)) -> (sub y, x)
995 if (N->getOperand(0).getOpcode() == ISD::SUB)
996 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000997 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998 // fold (neg (neg x)) -> x
999 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001000 return N->getOperand(0).getOperand(0);
1001 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002}
1003
Nate Begeman83e75ec2005-09-06 04:43:02 +00001004SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001005 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001007 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001008 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001009 // fold (fabs (fabs x)) -> (fabs x)
1010 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001011 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001012 // fold (fabs (fneg x)) -> (fabs x)
1013 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1014 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001015 N->getOperand(0).getOperand(0));
1016 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017}
1018
Nate Begeman1d4d4142005-09-01 00:19:25 +00001019// SelectionDAG::Combine - This is the entry point for the file.
1020//
Nate Begeman4ebd8052005-09-01 23:24:04 +00001021void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001022 /// run - This is the main entry point to this class.
1023 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00001024 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001025}