blob: d5ace37f6ddff1a5bf6b0c53c4e25dcd1fa9126c [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 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 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 Begemanf89d78d2005-09-07 16:09:19 +0000353 // fold (add (add x, c1), c2) -> (add x, c1+c2)
354 if (N1C && N0.getOpcode() == ISD::ADD &&
355 N0.getOperand(1).getOpcode() == ISD::Constant)
356 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
357 DAG.getConstant(N1C->getValue() +
358 cast<ConstantSDNode>(N0.getOperand(1))->getValue(),
359 VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000360 // fold floating point (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000361 if (N0CFP && N1CFP)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000362 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000363 // fold (A + (-B)) -> A-B
364 if (N1.getOpcode() == ISD::FNEG)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000365 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000366 // fold ((-A) + B) -> B-A
367 if (N0.getOpcode() == ISD::FNEG)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000368 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000369 // fold ((0-A) + B) -> B-A
370 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
371 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000372 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000373 // fold (A + (0-B)) -> A-B
374 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
375 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000376 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000377 // fold (A+(B-A)) -> B for non-fp types
378 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1) &&
379 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000380 return N1.getOperand(0);
381 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000382}
383
Nate Begeman83e75ec2005-09-06 04:43:02 +0000384SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000385 SDOperand N0 = N->getOperand(0);
386 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000387 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
388 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
389 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
390 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000391
392 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000393 if (N0C && N1C)
394 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000395 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000396 // fold (sub x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000397 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000398 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000399 // fold floating point (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000400 if (N0CFP && N1CFP)
401 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000402 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000403 // fold (A+B)-A -> B
404 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
405 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000406 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000407 // fold (A+B)-B -> A
408 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 &&
409 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000410 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000411 // fold (A-(-B)) -> A+B
412 if (N1.getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000413 return DAG.getNode(ISD::ADD, N0.getValueType(), N0, N1.getOperand(0));
414 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000415}
416
Nate Begeman83e75ec2005-09-06 04:43:02 +0000417SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000418 SDOperand N0 = N->getOperand(0);
419 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000420 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
421 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
422 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
423 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000424
425 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000426 if (N0C && N1C)
427 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000428 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000429 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000430 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000431 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000432 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000433 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000434 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000435 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000436 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000437 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000438 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000439 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000440 TLI.getShiftAmountTy()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000441 // fold floating point (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000442 if (N0CFP && N1CFP)
443 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000444 N->getValueType(0));
445 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000446}
447
Nate Begeman83e75ec2005-09-06 04:43:02 +0000448SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000449 SDOperand N0 = N->getOperand(0);
450 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000451 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
452 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
453 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
454 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000455
456 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000457 if (N0C && N1C && !N1C->isNullValue())
458 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000459 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000460 // fold floating point (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000461 if (N0CFP && N1CFP)
462 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000463 N->getValueType(0));
464 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000465}
466
Nate Begeman83e75ec2005-09-06 04:43:02 +0000467SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 SDOperand N0 = N->getOperand(0);
469 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000470 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
471 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000472
473 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000474 if (N0C && N1C && !N1C->isNullValue())
475 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000476 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000477 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000478 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000479 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000480 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000481 TLI.getShiftAmountTy()));
482 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000483}
484
Nate Begeman83e75ec2005-09-06 04:43:02 +0000485SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000486 SDOperand N0 = N->getOperand(0);
487 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
489 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
490 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
491 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000492
493 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000494 if (N0C && N1C && !N1C->isNullValue())
495 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000496 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000497 // fold floating point (srem c1, c2) -> fmod(c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000498 if (N0CFP && N1CFP)
499 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000500 N->getValueType(0));
501 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502}
503
Nate Begeman83e75ec2005-09-06 04:43:02 +0000504SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000505 SDOperand N0 = N->getOperand(0);
506 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000507 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
508 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000509
510 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000511 if (N0C && N1C && !N1C->isNullValue())
512 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000513 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000514 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000515 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000516}
517
Nate Begeman83e75ec2005-09-06 04:43:02 +0000518SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000519 SDOperand N0 = N->getOperand(0);
520 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000521 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000522
523 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000524 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000525 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000526 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000527 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000528 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
529 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000530 TLI.getShiftAmountTy()));
531 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000532}
533
Nate Begeman83e75ec2005-09-06 04:43:02 +0000534SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000535 SDOperand N0 = N->getOperand(0);
536 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000537 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000538
539 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000540 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000541 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000542 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000543 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000544 return DAG.getConstant(0, N0.getValueType());
545 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000546}
547
Nate Begeman83e75ec2005-09-06 04:43:02 +0000548SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000549 SDOperand N0 = N->getOperand(0);
550 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000551 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
552 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000554 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000555
556 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000557 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000558 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000559 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000560 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000561 return N0;
562 // if (and x, c) is known to be zero, return 0
563 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
564 return DAG.getConstant(0, VT);
565 // fold (and x, c) -> x iff (x & ~c) == 0
566 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
567 TLI))
568 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000569 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
570 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
571 unsigned ExtendBits =
572 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000573 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000574 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000575 }
576 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
577 if (N0.getOpcode() == ISD::OR)
578 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000579 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000580 return N1;
581 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000582}
583
Nate Begeman83e75ec2005-09-06 04:43:02 +0000584SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000585 SDOperand N0 = N->getOperand(0);
586 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000587 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
588 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000589 MVT::ValueType VT = N1.getValueType();
590 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000591
592 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000593 if (N0C && N1C)
594 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000595 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000596 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000597 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000598 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000599 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000600 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000601 return N1;
602 // fold (or x, c) -> c iff (x & ~c) == 0
603 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
604 TLI))
605 return N1;
606 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000607}
608
Nate Begeman83e75ec2005-09-06 04:43:02 +0000609SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000610 SDOperand N0 = N->getOperand(0);
611 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000612 SDOperand LHS, RHS, CC;
613 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
614 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 MVT::ValueType VT = N0.getValueType();
616
617 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000618 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000619 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000621 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000622 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000624 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
625 bool isInt = MVT::isInteger(LHS.getValueType());
626 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
627 isInt);
628 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000629 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000630 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000631 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000632 assert(0 && "Unhandled SetCC Equivalent!");
633 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634 }
635 // fold !(x or y) -> (!x and !y) iff x or y are freely invertible
Nate Begeman646d7e22005-09-02 21:18:40 +0000636 if (N1C && N1C->isAllOnesValue() && N0.getOpcode() == ISD::OR) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000637 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
638 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
639 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
640 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman83e75ec2005-09-06 04:43:02 +0000641 return DAG.getNode(ISD::AND, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000642 }
643 }
644 // fold !(x and y) -> (!x or !y) iff x or y are freely invertible
Nate Begeman646d7e22005-09-02 21:18:40 +0000645 if (N1C && N1C->isAllOnesValue() && N0.getOpcode() == ISD::AND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000646 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
647 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
648 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
649 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman83e75ec2005-09-06 04:43:02 +0000650 return DAG.getNode(ISD::OR, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651 }
652 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000653 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000654}
655
Nate Begeman83e75ec2005-09-06 04:43:02 +0000656SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000657 SDOperand N0 = N->getOperand(0);
658 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000659 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
660 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000661 MVT::ValueType VT = N0.getValueType();
662 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
663
664 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000665 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000666 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000667 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000668 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000669 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000671 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000672 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000673 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000674 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000675 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000676 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000677 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
678 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000680 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000681 N0.getOperand(1).getOpcode() == ISD::Constant) {
682 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000683 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000685 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000686 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000687 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000688 }
689 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
690 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000691 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000692 N0.getOperand(1).getOpcode() == ISD::Constant) {
693 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000694 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000695 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
696 DAG.getConstant(~0ULL << c1, VT));
697 if (c2 > c1)
698 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000699 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000700 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000701 return DAG.getNode(ISD::SRL, VT, Mask,
702 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000703 }
704 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000705 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000706 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000707 DAG.getConstant(~0ULL << N1C->getValue(), VT));
708 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000709}
710
Nate Begeman83e75ec2005-09-06 04:43:02 +0000711SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000712 SDOperand N0 = N->getOperand(0);
713 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
715 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000716 MVT::ValueType VT = N0.getValueType();
717 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
718
719 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000720 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000721 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000722 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000723 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000724 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000725 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000726 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000727 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000728 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000729 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000730 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000731 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000732 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000733 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000734 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000735 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000736 return DAG.getNode(ISD::SRL, VT, N0, N1);
737 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000738}
739
Nate Begeman83e75ec2005-09-06 04:43:02 +0000740SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000741 SDOperand N0 = N->getOperand(0);
742 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000743 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
744 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000745 MVT::ValueType VT = N0.getValueType();
746 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
747
748 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000749 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000750 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000751 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000752 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000753 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000754 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000755 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000756 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000757 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000758 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000759 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000760 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000761 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
762 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000763 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000764 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765 N0.getOperand(1).getOpcode() == ISD::Constant) {
766 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000767 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000768 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000769 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000770 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000771 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000772 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000773 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000774}
775
Nate Begeman83e75ec2005-09-06 04:43:02 +0000776SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000777 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000778 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000779
780 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000781 if (N0C)
782 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000783 N0.getValueType());
784 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000785}
786
Nate Begeman83e75ec2005-09-06 04:43:02 +0000787SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000788 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000789 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000790
791 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000792 if (N0C)
793 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000794 N0.getValueType());
795 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000796}
797
Nate Begeman83e75ec2005-09-06 04:43:02 +0000798SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000799 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000800 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000801
802 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000803 if (N0C)
804 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000805 N0.getValueType());
806 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000807}
808
Nate Begeman83e75ec2005-09-06 04:43:02 +0000809SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000810 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000811 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000812 MVT::ValueType VT = N->getValueType(0);
813
Nate Begeman1d4d4142005-09-01 00:19:25 +0000814 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000815 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000816 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000817 // fold (sext (sext x)) -> (sext x)
818 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000819 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
820 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000821}
822
Nate Begeman83e75ec2005-09-06 04:43:02 +0000823SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000824 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000825 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000826 MVT::ValueType VT = N->getValueType(0);
827
Nate Begeman1d4d4142005-09-01 00:19:25 +0000828 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000829 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000830 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000831 // fold (zext (zext x)) -> (zext x)
832 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000833 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
834 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000835}
836
Nate Begeman83e75ec2005-09-06 04:43:02 +0000837SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 SDOperand N1 = N->getOperand(1);
840 SDOperand LHS, RHS, CC;
841 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000843 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000844
Nate Begeman1d4d4142005-09-01 00:19:25 +0000845 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000846 if (N0C) {
847 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000848 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000849 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000850 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +0000851 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000852 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000853 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000854 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000855 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
856 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
857 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000858 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000859 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000860 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
861 if (N0.getOpcode() == ISD::AssertSext &&
862 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000863 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000864 }
865 // fold (sext_in_reg (sextload x)) -> (sextload x)
866 if (N0.getOpcode() == ISD::SEXTLOAD &&
867 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000868 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000869 }
Nate Begeman4ebd8052005-09-01 23:24:04 +0000870 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000871 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +0000872 if (N0.getOpcode() == ISD::SETCC &&
873 TLI.getSetCCResultContents() ==
874 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000875 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000876 // FIXME: this code is currently just ported over from SelectionDAG.cpp
877 // we probably actually want to handle this in two pieces. Rather than
878 // checking all the top bits for zero, just check the sign bit here and turn
879 // it into a zero extend inreg (AND with constant).
880 // then, let the code for AND figure out if the mask is superfluous rather
881 // than doing so here.
882 if (N0.getOpcode() == ISD::AND &&
883 N0.getOperand(1).getOpcode() == ISD::Constant) {
884 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
885 unsigned NumBits = MVT::getSizeInBits(EVT);
886 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000887 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000888 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000889 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000890}
891
Nate Begeman83e75ec2005-09-06 04:43:02 +0000892SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000893 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000894 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000895 MVT::ValueType VT = N->getValueType(0);
896
897 // noop truncate
898 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000899 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000900 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000901 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000902 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000903 // fold (truncate (truncate x)) -> (truncate x)
904 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000905 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000906 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
907 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
908 if (N0.getValueType() < VT)
909 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 else if (N0.getValueType() > VT)
912 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 else
915 // if the source and dest are the same type, we can drop both the extend
916 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +0000917 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000919 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000920}
921
Nate Begeman83e75ec2005-09-06 04:43:02 +0000922SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000923 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000924 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925
926 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000927 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000928 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
929 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000930}
931
Nate Begeman83e75ec2005-09-06 04:43:02 +0000932SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000933 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000934 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000935
936 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000937 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000938 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
939 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000940}
941
Nate Begeman83e75ec2005-09-06 04:43:02 +0000942SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000943 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944
945 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000946 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000947 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
948 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000949}
950
Nate Begeman83e75ec2005-09-06 04:43:02 +0000951SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000952 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000953
954 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000955 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000956 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
957 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000958}
959
Nate Begeman83e75ec2005-09-06 04:43:02 +0000960SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000961 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000962
963 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000964 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000965 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
966 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967}
968
Nate Begeman83e75ec2005-09-06 04:43:02 +0000969SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970 SDOperand N0 = N->getOperand(0);
971 MVT::ValueType VT = N->getValueType(0);
972 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974
975 // noop fp_round_inreg
976 if (EVT == VT)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000977 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000979 if (N0CFP) {
980 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000981 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000982 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000983 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000984}
985
Nate Begeman83e75ec2005-09-06 04:43:02 +0000986SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000987 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000988
989 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000990 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000991 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
992 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000993}
994
Nate Begeman83e75ec2005-09-06 04:43:02 +0000995SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000996 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000997 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000998 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000999 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000 // fold (neg (sub x, y)) -> (sub y, x)
1001 if (N->getOperand(0).getOpcode() == ISD::SUB)
1002 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001003 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004 // fold (neg (neg x)) -> x
1005 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001006 return N->getOperand(0).getOperand(0);
1007 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001008}
1009
Nate Begeman83e75ec2005-09-06 04:43:02 +00001010SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001011 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001012 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001013 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001014 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001015 // fold (fabs (fabs x)) -> (fabs x)
1016 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001017 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018 // fold (fabs (fneg x)) -> (fabs x)
1019 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1020 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001021 N->getOperand(0).getOperand(0));
1022 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001023}
1024
Nate Begeman1d4d4142005-09-01 00:19:25 +00001025// SelectionDAG::Combine - This is the entry point for the file.
1026//
Nate Begeman4ebd8052005-09-01 23:24:04 +00001027void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001028 /// run - This is the main entry point to this class.
1029 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00001030 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001031}