blob: 52110a1206cb6f2200a51fcddcda55440eef34ca [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"
40#include "llvm/Support/MathExtras.h"
41#include "llvm/Target/TargetLowering.h"
42#include <cmath>
43using namespace llvm;
44
45namespace {
46 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
47
48 class DAGCombiner {
49 SelectionDAG &DAG;
50 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000051 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000052
53 // Worklist of all of the nodes that need to be simplified.
54 std::vector<SDNode*> WorkList;
55
56 /// AddUsersToWorkList - When an instruction is simplified, add all users of
57 /// the instruction to the work lists because they might get more simplified
58 /// now.
59 ///
60 void AddUsersToWorkList(SDNode *N) {
61 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000062 UI != UE; ++UI)
63 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 }
65
66 /// removeFromWorkList - remove all instances of N from the worklist.
67 void removeFromWorkList(SDNode *N) {
68 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
69 WorkList.end());
70 }
71
72 /// visit - call the node-specific routine that knows how to fold each
73 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +000074 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +000075
76 // Visitation implementation - Implement dag node combining for different
77 // node types. The semantics are as follows:
78 // Return Value:
79 // null - No change was made
80 // otherwise - Node N should be replaced by the returned node.
81 //
Nate Begeman83e75ec2005-09-06 04:43:02 +000082 SDOperand visitTokenFactor(SDNode *N);
83 SDOperand visitADD(SDNode *N);
84 SDOperand visitSUB(SDNode *N);
85 SDOperand visitMUL(SDNode *N);
86 SDOperand visitSDIV(SDNode *N);
87 SDOperand visitUDIV(SDNode *N);
88 SDOperand visitSREM(SDNode *N);
89 SDOperand visitUREM(SDNode *N);
90 SDOperand visitMULHU(SDNode *N);
91 SDOperand visitMULHS(SDNode *N);
92 SDOperand visitAND(SDNode *N);
93 SDOperand visitOR(SDNode *N);
94 SDOperand visitXOR(SDNode *N);
95 SDOperand visitSHL(SDNode *N);
96 SDOperand visitSRA(SDNode *N);
97 SDOperand visitSRL(SDNode *N);
98 SDOperand visitCTLZ(SDNode *N);
99 SDOperand visitCTTZ(SDNode *N);
100 SDOperand visitCTPOP(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000101 // select
102 // select_cc
103 // setcc
Nate Begeman83e75ec2005-09-06 04:43:02 +0000104 SDOperand visitSIGN_EXTEND(SDNode *N);
105 SDOperand visitZERO_EXTEND(SDNode *N);
106 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
107 SDOperand visitTRUNCATE(SDNode *N);
108 SDOperand visitSINT_TO_FP(SDNode *N);
109 SDOperand visitUINT_TO_FP(SDNode *N);
110 SDOperand visitFP_TO_SINT(SDNode *N);
111 SDOperand visitFP_TO_UINT(SDNode *N);
112 SDOperand visitFP_ROUND(SDNode *N);
113 SDOperand visitFP_ROUND_INREG(SDNode *N);
114 SDOperand visitFP_EXTEND(SDNode *N);
115 SDOperand visitFNEG(SDNode *N);
116 SDOperand visitFABS(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000117 // brcond
118 // brcondtwoway
119 // br_cc
120 // brtwoway_cc
121public:
122 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000123 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000124
125 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000126 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000127 };
128}
129
130/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
131/// this predicate to simplify operations downstream. V and Mask are known to
132/// be the same type.
133static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
134 const TargetLowering &TLI) {
135 unsigned SrcBits;
136 if (Mask == 0) return true;
137
138 // If we know the result of a setcc has the top bits zero, use this info.
139 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000140 case ISD::Constant:
141 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
142 case ISD::SETCC:
Nate Begeman646d7e22005-09-02 21:18:40 +0000143 // FIXME: teach this about non ZeroOrOne values, such as 0 or -1
Nate Begeman4ebd8052005-09-01 23:24:04 +0000144 return ((Mask & 1) == 0) &&
145 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
146 case ISD::ZEXTLOAD:
147 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
148 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
149 case ISD::ZERO_EXTEND:
150 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
151 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
152 case ISD::AssertZext:
153 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
154 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
155 case ISD::AND:
156 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
157 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
158 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
159 // FALL THROUGH
160 case ISD::OR:
161 case ISD::XOR:
162 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
163 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
164 case ISD::SELECT:
165 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
166 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
167 case ISD::SELECT_CC:
168 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
169 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
170 case ISD::SRL:
171 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
172 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
173 uint64_t NewVal = Mask << ShAmt->getValue();
174 SrcBits = MVT::getSizeInBits(Op.getValueType());
175 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
176 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
177 }
178 return false;
179 case ISD::SHL:
180 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
181 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
182 uint64_t NewVal = Mask >> ShAmt->getValue();
183 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
184 }
185 return false;
186 case ISD::CTTZ:
187 case ISD::CTLZ:
188 case ISD::CTPOP:
189 // Bit counting instructions can not set the high bits of the result
190 // register. The max number of bits sets depends on the input.
191 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
192
193 // TODO we could handle some SRA cases here.
194 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000195 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000196 return false;
197}
198
Nate Begeman4ebd8052005-09-01 23:24:04 +0000199// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
200// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000201// Also, set the incoming LHS, RHS, and CC references to the appropriate
202// nodes based on the type of node we are checking. This simplifies life a
203// bit for the callers.
204static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
205 SDOperand &CC) {
206 if (N.getOpcode() == ISD::SETCC) {
207 LHS = N.getOperand(0);
208 RHS = N.getOperand(1);
209 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000210 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000211 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000212 if (N.getOpcode() == ISD::SELECT_CC &&
213 N.getOperand(2).getOpcode() == ISD::Constant &&
214 N.getOperand(3).getOpcode() == ISD::Constant &&
215 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000216 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
217 LHS = N.getOperand(0);
218 RHS = N.getOperand(1);
219 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000220 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000221 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000222 return false;
223}
224
Nate Begeman4ebd8052005-09-01 23:24:04 +0000225// isInvertibleForFree - Return true if there is no cost to emitting the logical
226// inverse of this node.
227static bool isInvertibleForFree(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000228 SDOperand N0, N1, N2;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000229 if (isa<ConstantSDNode>(N.Val)) return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000230 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000231 return true;
232 return false;
233}
234
235void DAGCombiner::Run(bool RunningAfterLegalize) {
236 // set the instance variable, so that the various visit routines may use it.
237 AfterLegalize = RunningAfterLegalize;
238
Nate Begeman646d7e22005-09-02 21:18:40 +0000239 // Add all the dag nodes to the worklist.
240 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000241
Nate Begeman1d4d4142005-09-01 00:19:25 +0000242 // while the worklist isn't empty, inspect the node on the end of it and
243 // try and combine it.
244 while (!WorkList.empty()) {
245 SDNode *N = WorkList.back();
246 WorkList.pop_back();
247
248 // If N has no uses, it is dead. Make sure to revisit all N's operands once
249 // N is deleted from the DAG, since they too may now be dead.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000250 // FIXME: is there a better way to keep from deleting the dag root because
251 // we think it has no uses? This works for now...
252 if (N->use_empty() && N != DAG.getRoot().Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000253 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
254 WorkList.push_back(N->getOperand(i).Val);
255
256 DAG.DeleteNode(N);
257 removeFromWorkList(N);
258 continue;
259 }
260
Nate Begeman83e75ec2005-09-06 04:43:02 +0000261 SDOperand RV = visit(N);
262 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000263 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000264 // If we get back the same node we passed in, rather than a new node or
265 // zero, we know that the node must have defined multiple values and
266 // CombineTo was used. Since CombineTo takes care of the worklist
267 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000268 if (RV.Val != N) {
269 std::cerr << "\nReplacing "; N->dump();
270 std::cerr << "\nWith: "; RV.Val->dump();
271 std::cerr << '\n';
272 DAG.ReplaceAllUsesWith(SDOperand(N, 0), RV);
Nate Begeman646d7e22005-09-02 21:18:40 +0000273
274 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000275 WorkList.push_back(RV.Val);
276 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000277
278 // Nodes can end up on the worklist more than once. Make sure we do
279 // not process a node that has been replaced.
280 removeFromWorkList(N);
281 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000282 }
283 }
284}
285
Nate Begeman83e75ec2005-09-06 04:43:02 +0000286SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000287 switch(N->getOpcode()) {
288 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000289 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000290 case ISD::ADD: return visitADD(N);
291 case ISD::SUB: return visitSUB(N);
292 case ISD::MUL: return visitMUL(N);
293 case ISD::SDIV: return visitSDIV(N);
294 case ISD::UDIV: return visitUDIV(N);
295 case ISD::SREM: return visitSREM(N);
296 case ISD::UREM: return visitUREM(N);
297 case ISD::MULHU: return visitMULHU(N);
298 case ISD::MULHS: return visitMULHS(N);
299 case ISD::AND: return visitAND(N);
300 case ISD::OR: return visitOR(N);
301 case ISD::XOR: return visitXOR(N);
302 case ISD::SHL: return visitSHL(N);
303 case ISD::SRA: return visitSRA(N);
304 case ISD::SRL: return visitSRL(N);
305 case ISD::CTLZ: return visitCTLZ(N);
306 case ISD::CTTZ: return visitCTTZ(N);
307 case ISD::CTPOP: return visitCTPOP(N);
308 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
309 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
310 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
311 case ISD::TRUNCATE: return visitTRUNCATE(N);
312 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
313 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
314 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
315 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
316 case ISD::FP_ROUND: return visitFP_ROUND(N);
317 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
318 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
319 case ISD::FNEG: return visitFNEG(N);
320 case ISD::FABS: return visitFABS(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000321 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000322 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000323}
324
Nate Begeman83e75ec2005-09-06 04:43:02 +0000325SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000326 // If the token factor has two operands and one is the entry token, replace
327 // the token factor with the other operand.
328 if (N->getNumOperands() == 2) {
329 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000330 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000331 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000332 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000333 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000334 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000335}
336
Nate Begeman83e75ec2005-09-06 04:43:02 +0000337SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000338 SDOperand N0 = N->getOperand(0);
339 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000340 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
341 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
342 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
343 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000344
345 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000346 if (N0C && N1C)
347 return DAG.getConstant(N0C->getValue() + N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000348 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000349 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000350 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000351 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000352 // fold floating point (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000353 if (N0CFP && N1CFP)
354 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000355 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000356 // fold (A + (-B)) -> A-B
357 if (N1.getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000358 return DAG.getNode(ISD::SUB, N->getValueType(0), N0, N1.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000359 // fold ((-A) + B) -> B-A
360 if (N0.getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000361 return DAG.getNode(ISD::SUB, N->getValueType(0), N1, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000362 // fold ((0-A) + B) -> B-A
363 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
364 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000365 return DAG.getNode(ISD::SUB, N->getValueType(0), N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000366 // fold (A + (0-B)) -> A-B
367 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
368 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000369 return DAG.getNode(ISD::SUB, N->getValueType(0), N0, N1.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000370 // fold (A+(B-A)) -> B for non-fp types
371 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1) &&
372 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000373 return N1.getOperand(0);
374 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000375}
376
Nate Begeman83e75ec2005-09-06 04:43:02 +0000377SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000378 SDOperand N0 = N->getOperand(0);
379 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000380 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
381 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
382 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
383 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000384
385 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000386 if (N0C && N1C)
387 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000388 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000389 // fold (sub x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000390 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000391 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000392 // fold floating point (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000393 if (N0CFP && N1CFP)
394 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000395 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000396 // fold (A+B)-A -> B
397 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
398 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000399 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000400 // fold (A+B)-B -> A
401 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1 &&
402 !MVT::isFloatingPoint(N1.getValueType()))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000403 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000404 // fold (A-(-B)) -> A+B
405 if (N1.getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000406 return DAG.getNode(ISD::ADD, N0.getValueType(), N0, N1.getOperand(0));
407 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000408}
409
Nate Begeman83e75ec2005-09-06 04:43:02 +0000410SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000411 SDOperand N0 = N->getOperand(0);
412 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000413 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
414 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
415 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
416 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000417
418 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000419 if (N0C && N1C)
420 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000421 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000422 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000423 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000424 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000425 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000426 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000427 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000428 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000429 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000430 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000431 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000432 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000433 TLI.getShiftAmountTy()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000434 // fold floating point (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000435 if (N0CFP && N1CFP)
436 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000437 N->getValueType(0));
438 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000439}
440
Nate Begeman83e75ec2005-09-06 04:43:02 +0000441SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000442 SDOperand N0 = N->getOperand(0);
443 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000444 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
446 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0.Val);
447 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000448
449 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000450 if (N0C && N1C && !N1C->isNullValue())
451 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000452 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 // fold floating point (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000454 if (N0CFP && N1CFP)
455 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000456 N->getValueType(0));
457 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000458}
459
Nate Begeman83e75ec2005-09-06 04:43:02 +0000460SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 SDOperand N0 = N->getOperand(0);
462 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000463 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
464 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000465
466 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000467 if (N0C && N1C && !N1C->isNullValue())
468 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000469 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000470 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000471 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000472 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000473 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000474 TLI.getShiftAmountTy()));
475 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000476}
477
Nate Begeman83e75ec2005-09-06 04:43:02 +0000478SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000479 SDOperand N0 = N->getOperand(0);
480 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000481 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
482 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
483 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
484 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000485
486 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000487 if (N0C && N1C && !N1C->isNullValue())
488 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000489 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000490 // fold floating point (srem c1, c2) -> fmod(c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000491 if (N0CFP && N1CFP)
492 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000493 N->getValueType(0));
494 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000495}
496
Nate Begeman83e75ec2005-09-06 04:43:02 +0000497SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000498 SDOperand N0 = N->getOperand(0);
499 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000500 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
501 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502
503 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000504 if (N0C && N1C && !N1C->isNullValue())
505 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000506 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000507 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000508 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000509}
510
Nate Begeman83e75ec2005-09-06 04:43:02 +0000511SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000512 SDOperand N0 = N->getOperand(0);
513 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000514 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000515
516 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000517 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000518 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000519 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000520 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000521 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
522 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000523 TLI.getShiftAmountTy()));
524 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000525}
526
Nate Begeman83e75ec2005-09-06 04:43:02 +0000527SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000528 SDOperand N0 = N->getOperand(0);
529 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000530 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000531
532 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000533 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000534 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000535 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000536 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000537 return DAG.getConstant(0, N0.getValueType());
538 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000539}
540
Nate Begeman83e75ec2005-09-06 04:43:02 +0000541SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000542 SDOperand N0 = N->getOperand(0);
543 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000544 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
545 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000546 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000547 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000548
549 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000550 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000551 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000552 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000553 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000554 return N0;
555 // if (and x, c) is known to be zero, return 0
556 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
557 return DAG.getConstant(0, VT);
558 // fold (and x, c) -> x iff (x & ~c) == 0
559 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
560 TLI))
561 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000562 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
563 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
564 unsigned ExtendBits =
565 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000566 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000567 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568 }
569 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
570 if (N0.getOpcode() == ISD::OR)
571 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000572 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000573 return N1;
574 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000575}
576
Nate Begeman83e75ec2005-09-06 04:43:02 +0000577SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000578 SDOperand N0 = N->getOperand(0);
579 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000580 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
581 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582 MVT::ValueType VT = N1.getValueType();
583 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000584
585 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000586 if (N0C && N1C)
587 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000588 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000589 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000590 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000591 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000592 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000593 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000594 return N1;
595 // fold (or x, c) -> c iff (x & ~c) == 0
596 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
597 TLI))
598 return N1;
599 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000600}
601
Nate Begeman83e75ec2005-09-06 04:43:02 +0000602SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000603 SDOperand N0 = N->getOperand(0);
604 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000605 SDOperand LHS, RHS, CC;
606 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
607 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 MVT::ValueType VT = N0.getValueType();
609
610 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000611 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000612 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000614 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000615 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000616 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000617 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
618 bool isInt = MVT::isInteger(LHS.getValueType());
619 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
620 isInt);
621 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000622 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000623 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000624 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000625 assert(0 && "Unhandled SetCC Equivalent!");
626 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000627 }
628 // fold !(x or y) -> (!x and !y) iff x or y are freely invertible
Nate Begeman646d7e22005-09-02 21:18:40 +0000629 if (N1C && N1C->isAllOnesValue() && N0.getOpcode() == ISD::OR) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
631 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
632 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
633 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman83e75ec2005-09-06 04:43:02 +0000634 return DAG.getNode(ISD::AND, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000635 }
636 }
637 // fold !(x and y) -> (!x or !y) iff x or y are freely invertible
Nate Begeman646d7e22005-09-02 21:18:40 +0000638 if (N1C && N1C->isAllOnesValue() && N0.getOpcode() == ISD::AND) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000639 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
640 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
641 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
642 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman83e75ec2005-09-06 04:43:02 +0000643 return DAG.getNode(ISD::OR, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000644 }
645 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000646 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000647}
648
Nate Begeman83e75ec2005-09-06 04:43:02 +0000649SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000650 SDOperand N0 = N->getOperand(0);
651 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000652 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
653 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000654 MVT::ValueType VT = N0.getValueType();
655 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
656
657 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000658 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000659 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000660 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000661 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000662 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000663 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000664 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000665 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000666 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000667 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000668 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000669 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000670 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
671 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000673 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000674 N0.getOperand(1).getOpcode() == ISD::Constant) {
675 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000676 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000677 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000678 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000680 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000681 }
682 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
683 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000684 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000685 N0.getOperand(1).getOpcode() == ISD::Constant) {
686 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000687 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000688 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
689 DAG.getConstant(~0ULL << c1, VT));
690 if (c2 > c1)
691 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000692 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000693 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000694 return DAG.getNode(ISD::SRL, VT, Mask,
695 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000696 }
697 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000698 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000699 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700 DAG.getConstant(~0ULL << N1C->getValue(), VT));
701 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000702}
703
Nate Begeman83e75ec2005-09-06 04:43:02 +0000704SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000705 SDOperand N0 = N->getOperand(0);
706 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000707 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
708 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000709 MVT::ValueType VT = N0.getValueType();
710 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
711
712 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000713 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000714 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000715 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000716 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000717 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000718 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000719 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000720 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000721 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000722 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000723 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000724 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000725 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000726 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000727 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000728 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000729 return DAG.getNode(ISD::SRL, VT, N0, N1);
730 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000731}
732
Nate Begeman83e75ec2005-09-06 04:43:02 +0000733SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000734 SDOperand N0 = N->getOperand(0);
735 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000736 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
737 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000738 MVT::ValueType VT = N0.getValueType();
739 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
740
741 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000742 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000743 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000744 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000745 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000746 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000747 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000748 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000749 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000750 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000751 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000752 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000753 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000754 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
755 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000756 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000757 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000758 N0.getOperand(1).getOpcode() == ISD::Constant) {
759 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000760 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000761 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000762 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000763 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000764 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000766 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000767}
768
Nate Begeman83e75ec2005-09-06 04:43:02 +0000769SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000770 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000771 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000772
773 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000774 if (N0C)
775 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000776 N0.getValueType());
777 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000778}
779
Nate Begeman83e75ec2005-09-06 04:43:02 +0000780SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000781 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000782 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000783
784 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000785 if (N0C)
786 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000787 N0.getValueType());
788 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000789}
790
Nate Begeman83e75ec2005-09-06 04:43:02 +0000791SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000792 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000793 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000794
795 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000796 if (N0C)
797 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000798 N0.getValueType());
799 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000800}
801
Nate Begeman83e75ec2005-09-06 04:43:02 +0000802SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000803 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000804 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000805 MVT::ValueType VT = N->getValueType(0);
806
Nate Begeman1d4d4142005-09-01 00:19:25 +0000807 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000808 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000809 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000810 // fold (sext (sext x)) -> (sext x)
811 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000812 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
813 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000814}
815
Nate Begeman83e75ec2005-09-06 04:43:02 +0000816SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000817 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000818 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000819 MVT::ValueType VT = N->getValueType(0);
820
Nate Begeman1d4d4142005-09-01 00:19:25 +0000821 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000822 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000823 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000824 // fold (zext (zext x)) -> (zext x)
825 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000826 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
827 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000828}
829
Nate Begeman83e75ec2005-09-06 04:43:02 +0000830SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000831 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000832 SDOperand N1 = N->getOperand(1);
833 SDOperand LHS, RHS, CC;
834 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000835 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000836 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000837
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 if (N0C) {
840 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000841 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000843 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +0000844 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000845 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000846 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000847 }
Nate Begeman646d7e22005-09-02 21:18:40 +0000848 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
849 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
850 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000851 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000852 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
854 if (N0.getOpcode() == ISD::AssertSext &&
855 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000856 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000857 }
858 // fold (sext_in_reg (sextload x)) -> (sextload x)
859 if (N0.getOpcode() == ISD::SEXTLOAD &&
860 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +0000861 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000862 }
Nate Begeman4ebd8052005-09-01 23:24:04 +0000863 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000864 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865 if (N0.getOpcode() == ISD::SETCC &&
866 TLI.getSetCCResultContents() ==
867 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000868 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000869 // FIXME: this code is currently just ported over from SelectionDAG.cpp
870 // we probably actually want to handle this in two pieces. Rather than
871 // checking all the top bits for zero, just check the sign bit here and turn
872 // it into a zero extend inreg (AND with constant).
873 // then, let the code for AND figure out if the mask is superfluous rather
874 // than doing so here.
875 if (N0.getOpcode() == ISD::AND &&
876 N0.getOperand(1).getOpcode() == ISD::Constant) {
877 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
878 unsigned NumBits = MVT::getSizeInBits(EVT);
879 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000880 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000881 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000882 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000883}
884
Nate Begeman83e75ec2005-09-06 04:43:02 +0000885SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000886 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000887 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000888 MVT::ValueType VT = N->getValueType(0);
889
890 // noop truncate
891 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000892 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000893 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000894 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000895 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000896 // fold (truncate (truncate x)) -> (truncate x)
897 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000898 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000899 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
900 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
901 if (N0.getValueType() < VT)
902 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +0000903 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000904 else if (N0.getValueType() > VT)
905 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +0000906 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000907 else
908 // if the source and dest are the same type, we can drop both the extend
909 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000912 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000913}
914
Nate Begeman83e75ec2005-09-06 04:43:02 +0000915SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000916 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000917 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918
919 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000920 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000921 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
922 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000923}
924
Nate Begeman83e75ec2005-09-06 04:43:02 +0000925SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000926 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000927 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928
929 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000930 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000931 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
932 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000933}
934
Nate Begeman83e75ec2005-09-06 04:43:02 +0000935SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000936 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000937
938 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000940 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
941 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942}
943
Nate Begeman83e75ec2005-09-06 04:43:02 +0000944SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000945 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000946
947 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000948 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000949 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
950 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951}
952
Nate Begeman83e75ec2005-09-06 04:43:02 +0000953SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000954 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000955
956 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000957 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000958 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
959 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000960}
961
Nate Begeman83e75ec2005-09-06 04:43:02 +0000962SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963 SDOperand N0 = N->getOperand(0);
964 MVT::ValueType VT = N->getValueType(0);
965 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +0000966 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967
968 // noop fp_round_inreg
969 if (EVT == VT)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000970 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 if (N0CFP) {
973 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000974 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000976 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000977}
978
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000980 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981
982 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +0000983 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000984 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
985 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000986}
987
Nate Begeman83e75ec2005-09-06 04:43:02 +0000988SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000989 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000990 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +0000991 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000992 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000993 // fold (neg (sub x, y)) -> (sub y, x)
994 if (N->getOperand(0).getOpcode() == ISD::SUB)
995 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000996 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000997 // fold (neg (neg x)) -> x
998 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000999 return N->getOperand(0).getOperand(0);
1000 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001001}
1002
Nate Begeman83e75ec2005-09-06 04:43:02 +00001003SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001004 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001005 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001006 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001007 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001008 // fold (fabs (fabs x)) -> (fabs x)
1009 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001010 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011 // fold (fabs (fneg x)) -> (fabs x)
1012 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1013 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001014 N->getOperand(0).getOperand(0));
1015 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016}
1017
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018// SelectionDAG::Combine - This is the entry point for the file.
1019//
Nate Begeman4ebd8052005-09-01 23:24:04 +00001020void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001021 /// run - This is the main entry point to this class.
1022 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00001023 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001024}