blob: 069a8f9176bf5940e97a67dd49d9412dc4cbbadf [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//
Nate Begeman44728a72005-09-19 22:34:01 +000023// FIXME: select C, 16, 0 -> shr C, 4
24// FIXME: select C, pow2, pow2 -> something smart
25// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
26// FIXME: (select C, load A, load B) -> load (select C, A, B)
27// FIXME: store -> load -> forward substitute
28// FIXME: Dead stores -> nuke
29// FIXME: shr X, (and Y,31) -> shr X, Y
30// FIXME: TRUNC (LOAD) -> EXT_LOAD/LOAD(smaller)
Nate Begeman1d4d4142005-09-01 00:19:25 +000031// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000032// FIXME: undef values
Nate Begeman1d4d4142005-09-01 00:19:25 +000033// FIXME: zero extend when top bits are 0 -> drop it ?
Nate Begeman4ebd8052005-09-01 23:24:04 +000034// FIXME: make truncate see through SIGN_EXTEND and AND
35// FIXME: sext_in_reg(setcc) on targets that return zero or one, and where
36// EVT != MVT::i1 can drop the sext.
Nate Begeman4ebd8052005-09-01 23:24:04 +000037// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000038// FIXME: verify that getNode can't return extends with an operand whose type
39// is >= to that of the extend.
40// FIXME: divide by zero is currently left unfolded. do we want to turn this
41// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000042// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000043//
44//===----------------------------------------------------------------------===//
45
46#define DEBUG_TYPE "dagcombine"
47#include "llvm/ADT/Statistic.h"
48#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000049#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000050#include "llvm/Support/MathExtras.h"
51#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000052#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000053#include <cmath>
54using namespace llvm;
55
56namespace {
57 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
58
59 class DAGCombiner {
60 SelectionDAG &DAG;
61 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000062 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000063
64 // Worklist of all of the nodes that need to be simplified.
65 std::vector<SDNode*> WorkList;
66
67 /// AddUsersToWorkList - When an instruction is simplified, add all users of
68 /// the instruction to the work lists because they might get more simplified
69 /// now.
70 ///
71 void AddUsersToWorkList(SDNode *N) {
72 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000073 UI != UE; ++UI)
74 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000075 }
76
77 /// removeFromWorkList - remove all instances of N from the worklist.
78 void removeFromWorkList(SDNode *N) {
79 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
80 WorkList.end());
81 }
82
83 /// visit - call the node-specific routine that knows how to fold each
84 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +000085 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +000086
87 // Visitation implementation - Implement dag node combining for different
88 // node types. The semantics are as follows:
89 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +000090 // SDOperand.Val == 0 - No change was made
91 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +000092 //
Nate Begeman83e75ec2005-09-06 04:43:02 +000093 SDOperand visitTokenFactor(SDNode *N);
94 SDOperand visitADD(SDNode *N);
95 SDOperand visitSUB(SDNode *N);
96 SDOperand visitMUL(SDNode *N);
97 SDOperand visitSDIV(SDNode *N);
98 SDOperand visitUDIV(SDNode *N);
99 SDOperand visitSREM(SDNode *N);
100 SDOperand visitUREM(SDNode *N);
101 SDOperand visitMULHU(SDNode *N);
102 SDOperand visitMULHS(SDNode *N);
103 SDOperand visitAND(SDNode *N);
104 SDOperand visitOR(SDNode *N);
105 SDOperand visitXOR(SDNode *N);
106 SDOperand visitSHL(SDNode *N);
107 SDOperand visitSRA(SDNode *N);
108 SDOperand visitSRL(SDNode *N);
109 SDOperand visitCTLZ(SDNode *N);
110 SDOperand visitCTTZ(SDNode *N);
111 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000112 SDOperand visitSELECT(SDNode *N);
113 SDOperand visitSELECT_CC(SDNode *N);
114 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000115 SDOperand visitSIGN_EXTEND(SDNode *N);
116 SDOperand visitZERO_EXTEND(SDNode *N);
117 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
118 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000119
120 SDOperand visitFADD(SDNode *N);
121 SDOperand visitFSUB(SDNode *N);
122 SDOperand visitFMUL(SDNode *N);
123 SDOperand visitFDIV(SDNode *N);
124 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000125 SDOperand visitSINT_TO_FP(SDNode *N);
126 SDOperand visitUINT_TO_FP(SDNode *N);
127 SDOperand visitFP_TO_SINT(SDNode *N);
128 SDOperand visitFP_TO_UINT(SDNode *N);
129 SDOperand visitFP_ROUND(SDNode *N);
130 SDOperand visitFP_ROUND_INREG(SDNode *N);
131 SDOperand visitFP_EXTEND(SDNode *N);
132 SDOperand visitFNEG(SDNode *N);
133 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000134 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000135 SDOperand visitBRCONDTWOWAY(SDNode *N);
136 SDOperand visitBR_CC(SDNode *N);
137 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000138
Nate Begeman44728a72005-09-19 22:34:01 +0000139 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
140 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
141 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000142 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000143 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000144public:
145 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000146 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000147
148 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000149 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000150 };
151}
152
153/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
154/// this predicate to simplify operations downstream. V and Mask are known to
155/// be the same type.
156static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
157 const TargetLowering &TLI) {
158 unsigned SrcBits;
159 if (Mask == 0) return true;
160
161 // If we know the result of a setcc has the top bits zero, use this info.
162 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000163 case ISD::Constant:
164 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
165 case ISD::SETCC:
Nate Begeman646d7e22005-09-02 21:18:40 +0000166 // FIXME: teach this about non ZeroOrOne values, such as 0 or -1
Nate Begeman4ebd8052005-09-01 23:24:04 +0000167 return ((Mask & 1) == 0) &&
168 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
169 case ISD::ZEXTLOAD:
170 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
171 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
172 case ISD::ZERO_EXTEND:
173 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
174 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
175 case ISD::AssertZext:
176 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
177 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
178 case ISD::AND:
Chris Lattneree899e62005-10-09 22:12:36 +0000179 // If either of the operands has zero bits, the result will too.
180 if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) ||
181 MaskedValueIsZero(Op.getOperand(0), Mask, TLI))
182 return true;
183
Nate Begeman4ebd8052005-09-01 23:24:04 +0000184 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
185 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
186 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
Chris Lattneree899e62005-10-09 22:12:36 +0000187 return false;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000188 case ISD::OR:
189 case ISD::XOR:
190 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
191 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
192 case ISD::SELECT:
193 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
194 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
195 case ISD::SELECT_CC:
196 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
197 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
198 case ISD::SRL:
199 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
200 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
201 uint64_t NewVal = Mask << ShAmt->getValue();
202 SrcBits = MVT::getSizeInBits(Op.getValueType());
203 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
204 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
205 }
206 return false;
207 case ISD::SHL:
208 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
209 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
210 uint64_t NewVal = Mask >> ShAmt->getValue();
211 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
212 }
213 return false;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000214 case ISD::SUB:
215 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
216 // We know that the top bits of C-X are clear if X contains less bits
217 // than C (i.e. no wrap-around can happen). For example, 20-X is
218 // positive if we can prove that X is >= 0 and < 16.
219 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
220 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
221 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
222 uint64_t MaskV = (1ULL << (63-NLZ))-1;
223 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
224 // High bits are clear this value is known to be >= C.
225 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
226 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
227 return true;
228 }
229 }
230 }
231 break;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000232 case ISD::CTTZ:
233 case ISD::CTLZ:
234 case ISD::CTPOP:
235 // Bit counting instructions can not set the high bits of the result
236 // register. The max number of bits sets depends on the input.
237 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
238
239 // TODO we could handle some SRA cases here.
240 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000241 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000242 return false;
243}
244
Nate Begeman4ebd8052005-09-01 23:24:04 +0000245// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
246// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000247// Also, set the incoming LHS, RHS, and CC references to the appropriate
248// nodes based on the type of node we are checking. This simplifies life a
249// bit for the callers.
250static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
251 SDOperand &CC) {
252 if (N.getOpcode() == ISD::SETCC) {
253 LHS = N.getOperand(0);
254 RHS = N.getOperand(1);
255 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000256 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000257 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000258 if (N.getOpcode() == ISD::SELECT_CC &&
259 N.getOperand(2).getOpcode() == ISD::Constant &&
260 N.getOperand(3).getOpcode() == ISD::Constant &&
261 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000262 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
263 LHS = N.getOperand(0);
264 RHS = N.getOperand(1);
265 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000266 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000267 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000268 return false;
269}
270
Nate Begeman99801192005-09-07 23:25:52 +0000271// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
272// one use. If this is true, it allows the users to invert the operation for
273// free when it is profitable to do so.
274static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000275 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000276 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000277 return true;
278 return false;
279}
280
Nate Begeman452d7be2005-09-16 00:54:12 +0000281// FIXME: This should probably go in the ISD class rather than being duplicated
282// in several files.
283static bool isCommutativeBinOp(unsigned Opcode) {
284 switch (Opcode) {
285 case ISD::ADD:
286 case ISD::MUL:
287 case ISD::AND:
288 case ISD::OR:
289 case ISD::XOR: return true;
290 default: return false; // FIXME: Need commutative info for user ops!
291 }
292}
293
Nate Begeman4ebd8052005-09-01 23:24:04 +0000294void DAGCombiner::Run(bool RunningAfterLegalize) {
295 // set the instance variable, so that the various visit routines may use it.
296 AfterLegalize = RunningAfterLegalize;
297
Nate Begeman646d7e22005-09-02 21:18:40 +0000298 // Add all the dag nodes to the worklist.
299 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000300
Chris Lattner95038592005-10-05 06:35:28 +0000301 // Create a dummy node (which is not added to allnodes), that adds a reference
302 // to the root node, preventing it from being deleted, and tracking any
303 // changes of the root.
304 HandleSDNode Dummy(DAG.getRoot());
305
Nate Begeman1d4d4142005-09-01 00:19:25 +0000306 // while the worklist isn't empty, inspect the node on the end of it and
307 // try and combine it.
308 while (!WorkList.empty()) {
309 SDNode *N = WorkList.back();
310 WorkList.pop_back();
311
312 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000313 // N is deleted from the DAG, since they too may now be dead or may have a
314 // reduced number of uses, allowing other xforms.
315 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000316 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
317 WorkList.push_back(N->getOperand(i).Val);
318
Nate Begeman1d4d4142005-09-01 00:19:25 +0000319 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000320 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000321 continue;
322 }
323
Nate Begeman83e75ec2005-09-06 04:43:02 +0000324 SDOperand RV = visit(N);
325 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000326 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000327 // If we get back the same node we passed in, rather than a new node or
328 // zero, we know that the node must have defined multiple values and
329 // CombineTo was used. Since CombineTo takes care of the worklist
330 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000331 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000332 DEBUG(std::cerr << "\nReplacing "; N->dump();
333 std::cerr << "\nWith: "; RV.Val->dump();
334 std::cerr << '\n');
Nate Begeman99801192005-09-07 23:25:52 +0000335 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV));
Nate Begeman646d7e22005-09-02 21:18:40 +0000336
337 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000338 WorkList.push_back(RV.Val);
339 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000340
341 // Nodes can end up on the worklist more than once. Make sure we do
342 // not process a node that has been replaced.
343 removeFromWorkList(N);
Chris Lattner5c46f742005-10-05 06:11:08 +0000344
345 // Finally, since the node is now dead, remove it from the graph.
346 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000347 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000348 }
349 }
Chris Lattner95038592005-10-05 06:35:28 +0000350
351 // If the root changed (e.g. it was a dead load, update the root).
352 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000353}
354
Nate Begeman83e75ec2005-09-06 04:43:02 +0000355SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000356 switch(N->getOpcode()) {
357 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000358 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000359 case ISD::ADD: return visitADD(N);
360 case ISD::SUB: return visitSUB(N);
361 case ISD::MUL: return visitMUL(N);
362 case ISD::SDIV: return visitSDIV(N);
363 case ISD::UDIV: return visitUDIV(N);
364 case ISD::SREM: return visitSREM(N);
365 case ISD::UREM: return visitUREM(N);
366 case ISD::MULHU: return visitMULHU(N);
367 case ISD::MULHS: return visitMULHS(N);
368 case ISD::AND: return visitAND(N);
369 case ISD::OR: return visitOR(N);
370 case ISD::XOR: return visitXOR(N);
371 case ISD::SHL: return visitSHL(N);
372 case ISD::SRA: return visitSRA(N);
373 case ISD::SRL: return visitSRL(N);
374 case ISD::CTLZ: return visitCTLZ(N);
375 case ISD::CTTZ: return visitCTTZ(N);
376 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000377 case ISD::SELECT: return visitSELECT(N);
378 case ISD::SELECT_CC: return visitSELECT_CC(N);
379 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000380 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
381 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
382 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
383 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000384 case ISD::FADD: return visitFADD(N);
385 case ISD::FSUB: return visitFSUB(N);
386 case ISD::FMUL: return visitFMUL(N);
387 case ISD::FDIV: return visitFDIV(N);
388 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000389 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
390 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
391 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
392 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
393 case ISD::FP_ROUND: return visitFP_ROUND(N);
394 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
395 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
396 case ISD::FNEG: return visitFNEG(N);
397 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000398 case ISD::BRCOND: return visitBRCOND(N);
399 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
400 case ISD::BR_CC: return visitBR_CC(N);
401 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000402 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000403 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000404}
405
Nate Begeman83e75ec2005-09-06 04:43:02 +0000406SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000407 // If the token factor has two operands and one is the entry token, replace
408 // the token factor with the other operand.
409 if (N->getNumOperands() == 2) {
410 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000411 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000412 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000413 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000414 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000415 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000416}
417
Nate Begeman83e75ec2005-09-06 04:43:02 +0000418SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000419 SDOperand N0 = N->getOperand(0);
420 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000421 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
422 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000423 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000424
425 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000426 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000427 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000428 // canonicalize constant to RHS
429 if (N0C && !N1C) {
430 std::swap(N0, N1);
431 std::swap(N0C, N1C);
432 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000433 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000434 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000435 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000436 // fold (add (add x, c1), c2) -> (add x, c1+c2)
437 if (N1C && N0.getOpcode() == ISD::ADD) {
438 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
439 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
440 if (N00C)
441 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
442 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
443 if (N01C)
444 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
445 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
446 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000447 // fold ((0-A) + B) -> B-A
448 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
449 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000450 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000451 // fold (A + (0-B)) -> A-B
452 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
453 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000454 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000455 // fold (A+(B-A)) -> B
456 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000457 return N1.getOperand(0);
458 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000459}
460
Nate Begeman83e75ec2005-09-06 04:43:02 +0000461SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000462 SDOperand N0 = N->getOperand(0);
463 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000464 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
465 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000466
467 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000468 if (N0C && N1C)
469 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000470 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000471 // fold (sub x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000472 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000473 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000474 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000475 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000476 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000477 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000478 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000479 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000480 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000481}
482
Nate Begeman83e75ec2005-09-06 04:43:02 +0000483SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484 SDOperand N0 = N->getOperand(0);
485 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000486 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
487 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000488 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000489
490 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000491 if (N0C && N1C)
492 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000493 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000494 // canonicalize constant to RHS
495 if (N0C && !N1C) {
496 std::swap(N0, N1);
497 std::swap(N0C, N1C);
498 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000499 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000500 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000501 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000503 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000504 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000505 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000506 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000507 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000508 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000509 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000510 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000511 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
512 if (N1C && N0.getOpcode() == ISD::MUL) {
513 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
514 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
515 if (N00C)
516 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
517 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
518 if (N01C)
519 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
520 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
521 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000522 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000523}
524
Nate Begeman83e75ec2005-09-06 04:43:02 +0000525SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000526 SDOperand N0 = N->getOperand(0);
527 SDOperand N1 = N->getOperand(1);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000528 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000529 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
530 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000531
532 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000533 if (N0C && N1C && !N1C->isNullValue())
534 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000535 N->getValueType(0));
Chris Lattner094c8fc2005-10-07 06:10:46 +0000536
537 // If we know the sign bits of both operands are zero, strength reduce to a
538 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
539 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
540 if (MaskedValueIsZero(N1, SignBit, TLI) &&
541 MaskedValueIsZero(N0, SignBit, TLI))
542 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
543
544
Nate Begeman83e75ec2005-09-06 04:43:02 +0000545 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000546}
547
Nate Begeman83e75ec2005-09-06 04:43:02 +0000548SDOperand DAGCombiner::visitUDIV(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.Val);
552 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553
554 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000555 if (N0C && N1C && !N1C->isNullValue())
556 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000557 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000558 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000559 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000560 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000561 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000562 TLI.getShiftAmountTy()));
563 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000564}
565
Nate Begeman83e75ec2005-09-06 04:43:02 +0000566SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000567 SDOperand N0 = N->getOperand(0);
568 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000569 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
570 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000571
572 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000573 if (N0C && N1C && !N1C->isNullValue())
574 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000575 N->getValueType(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000576 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000577}
578
Nate Begeman83e75ec2005-09-06 04:43:02 +0000579SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580 SDOperand N0 = N->getOperand(0);
581 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000582 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
583 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000584
585 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000586 if (N0C && N1C && !N1C->isNullValue())
587 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000588 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000589 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000590 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000591}
592
Nate Begeman83e75ec2005-09-06 04:43:02 +0000593SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000594 SDOperand N0 = N->getOperand(0);
595 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000596 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597
598 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000599 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000600 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000601 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000602 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000603 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
604 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000605 TLI.getShiftAmountTy()));
606 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000607}
608
Nate Begeman83e75ec2005-09-06 04:43:02 +0000609SDOperand DAGCombiner::visitMULHU(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 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613
614 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000615 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000616 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000618 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000619 return DAG.getConstant(0, N0.getValueType());
620 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000621}
622
Nate Begeman83e75ec2005-09-06 04:43:02 +0000623SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000624 SDOperand N0 = N->getOperand(0);
625 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000626 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000627 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
628 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000629 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000630 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631
632 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000633 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000634 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000635 // canonicalize constant to RHS
636 if (N0C && !N1C) {
637 std::swap(N0, N1);
638 std::swap(N0C, N1C);
639 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000640 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000641 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000642 return N0;
643 // if (and x, c) is known to be zero, return 0
644 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
645 return DAG.getConstant(0, VT);
646 // fold (and x, c) -> x iff (x & ~c) == 0
647 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
648 TLI))
649 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000650 // fold (and (and x, c1), c2) -> (and x, c1^c2)
651 if (N1C && N0.getOpcode() == ISD::AND) {
652 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
653 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
654 if (N00C)
655 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
656 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
657 if (N01C)
658 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
659 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
660 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000661 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
662 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
663 unsigned ExtendBits =
664 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000665 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000666 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000667 }
668 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
669 if (N0.getOpcode() == ISD::OR)
670 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000671 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000672 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000673 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
674 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
675 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
676 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
677
678 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
679 MVT::isInteger(LL.getValueType())) {
680 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
681 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
682 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
683 WorkList.push_back(ORNode.Val);
684 return DAG.getSetCC(VT, ORNode, LR, Op1);
685 }
686 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
687 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
688 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
689 WorkList.push_back(ANDNode.Val);
690 return DAG.getSetCC(VT, ANDNode, LR, Op1);
691 }
692 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
693 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
694 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
695 WorkList.push_back(ORNode.Val);
696 return DAG.getSetCC(VT, ORNode, LR, Op1);
697 }
698 }
699 // canonicalize equivalent to ll == rl
700 if (LL == RR && LR == RL) {
701 Op1 = ISD::getSetCCSwappedOperands(Op1);
702 std::swap(RL, RR);
703 }
704 if (LL == RL && LR == RR) {
705 bool isInteger = MVT::isInteger(LL.getValueType());
706 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
707 if (Result != ISD::SETCC_INVALID)
708 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
709 }
710 }
711 // fold (and (zext x), (zext y)) -> (zext (and x, y))
712 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
713 N1.getOpcode() == ISD::ZERO_EXTEND &&
714 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
715 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
716 N0.getOperand(0), N1.getOperand(0));
717 WorkList.push_back(ANDNode.Val);
718 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
719 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000720 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
721 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
722 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
723 N0.getOperand(1) == N1.getOperand(1)) {
724 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
725 N0.getOperand(0), N1.getOperand(0));
726 WorkList.push_back(ANDNode.Val);
727 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
728 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000729 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000730}
731
Nate Begeman83e75ec2005-09-06 04:43:02 +0000732SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000733 SDOperand N0 = N->getOperand(0);
734 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000735 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000736 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
737 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000738 MVT::ValueType VT = N1.getValueType();
739 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000740
741 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000742 if (N0C && N1C)
743 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000744 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000745 // canonicalize constant to RHS
746 if (N0C && !N1C) {
747 std::swap(N0, N1);
748 std::swap(N0C, N1C);
749 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000750 // fold (or 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 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000754 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000755 return N1;
756 // fold (or x, c) -> c iff (x & ~c) == 0
757 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
758 TLI))
759 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000760 // fold (or (or x, c1), c2) -> (or x, c1|c2)
761 if (N1C && N0.getOpcode() == ISD::OR) {
762 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
763 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
764 if (N00C)
765 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
766 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
767 if (N01C)
768 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
769 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
770 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000771 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
772 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
773 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
774 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
775
776 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
777 MVT::isInteger(LL.getValueType())) {
778 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
779 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
780 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
781 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
782 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
783 WorkList.push_back(ORNode.Val);
784 return DAG.getSetCC(VT, ORNode, LR, Op1);
785 }
786 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
787 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
788 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
789 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
790 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
791 WorkList.push_back(ANDNode.Val);
792 return DAG.getSetCC(VT, ANDNode, LR, Op1);
793 }
794 }
795 // canonicalize equivalent to ll == rl
796 if (LL == RR && LR == RL) {
797 Op1 = ISD::getSetCCSwappedOperands(Op1);
798 std::swap(RL, RR);
799 }
800 if (LL == RL && LR == RR) {
801 bool isInteger = MVT::isInteger(LL.getValueType());
802 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
803 if (Result != ISD::SETCC_INVALID)
804 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
805 }
806 }
807 // fold (or (zext x), (zext y)) -> (zext (or x, y))
808 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
809 N1.getOpcode() == ISD::ZERO_EXTEND &&
810 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
811 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
812 N0.getOperand(0), N1.getOperand(0));
813 WorkList.push_back(ORNode.Val);
814 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
815 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000816 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000817}
818
Nate Begeman83e75ec2005-09-06 04:43:02 +0000819SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000820 SDOperand N0 = N->getOperand(0);
821 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000822 SDOperand LHS, RHS, CC;
823 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
824 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000825 MVT::ValueType VT = N0.getValueType();
826
827 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000828 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000829 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000830 // canonicalize constant to RHS
831 if (N0C && !N1C) {
832 std::swap(N0, N1);
833 std::swap(N0C, N1C);
834 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000835 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000836 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000837 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
840 bool isInt = MVT::isInteger(LHS.getValueType());
841 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
842 isInt);
843 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000844 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000845 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000846 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000847 assert(0 && "Unhandled SetCC Equivalent!");
848 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000849 }
Nate Begeman99801192005-09-07 23:25:52 +0000850 // fold !(x or y) -> (!x and !y) iff x or y are setcc
851 if (N1C && N1C->getValue() == 1 &&
852 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000854 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
855 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000856 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
857 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000858 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
859 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000860 }
861 }
Nate Begeman99801192005-09-07 23:25:52 +0000862 // fold !(x or y) -> (!x and !y) iff x or y are constants
863 if (N1C && N1C->isAllOnesValue() &&
864 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000866 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
867 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000868 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
869 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000870 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
871 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000872 }
873 }
Nate Begeman223df222005-09-08 20:18:10 +0000874 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
875 if (N1C && N0.getOpcode() == ISD::XOR) {
876 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
877 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
878 if (N00C)
879 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
880 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
881 if (N01C)
882 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
883 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
884 }
885 // fold (xor x, x) -> 0
886 if (N0 == N1)
887 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000888 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
889 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
890 N1.getOpcode() == ISD::ZERO_EXTEND &&
891 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
892 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
893 N0.getOperand(0), N1.getOperand(0));
894 WorkList.push_back(XORNode.Val);
895 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
896 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000897 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000898}
899
Nate Begeman83e75ec2005-09-06 04:43:02 +0000900SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000901 SDOperand N0 = N->getOperand(0);
902 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000903 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
904 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000905 MVT::ValueType VT = N0.getValueType();
906 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
907
908 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000909 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000912 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000915 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000916 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000917 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000918 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000919 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000920 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000921 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
922 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000923 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000924 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925 N0.getOperand(1).getOpcode() == ISD::Constant) {
926 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000927 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000929 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000930 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000931 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932 }
933 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
934 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000935 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000936 N0.getOperand(1).getOpcode() == ISD::Constant) {
937 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000938 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000939 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
940 DAG.getConstant(~0ULL << c1, VT));
941 if (c2 > c1)
942 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000943 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000945 return DAG.getNode(ISD::SRL, VT, Mask,
946 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000947 }
948 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000949 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000950 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000951 DAG.getConstant(~0ULL << N1C->getValue(), VT));
952 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000953}
954
Nate Begeman83e75ec2005-09-06 04:43:02 +0000955SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000956 SDOperand N0 = N->getOperand(0);
957 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
959 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000960 MVT::ValueType VT = N0.getValueType();
961 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
962
963 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000964 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000965 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000966 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000968 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000969 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000970 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000971 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000972 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000974 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000976 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000977 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000979 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000980 return DAG.getNode(ISD::SRL, VT, N0, N1);
981 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000982}
983
Nate Begeman83e75ec2005-09-06 04:43:02 +0000984SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000985 SDOperand N0 = N->getOperand(0);
986 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000987 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
988 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000989 MVT::ValueType VT = N0.getValueType();
990 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
991
992 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000993 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000994 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000995 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000996 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000997 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000999 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001000 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001001 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001002 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001003 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001005 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1006 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001007 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001008 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001009 N0.getOperand(1).getOpcode() == ISD::Constant) {
1010 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001011 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001012 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001013 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001015 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001017 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018}
1019
Nate Begeman83e75ec2005-09-06 04:43:02 +00001020SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001021 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001022 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001023
1024 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001025 if (N0C)
1026 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001027 N0.getValueType());
1028 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001029}
1030
Nate Begeman83e75ec2005-09-06 04:43:02 +00001031SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001032 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001033 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001034
1035 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001036 if (N0C)
1037 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001038 N0.getValueType());
1039 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001040}
1041
Nate Begeman83e75ec2005-09-06 04:43:02 +00001042SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001043 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001044 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045
1046 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001047 if (N0C)
1048 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001049 N0.getValueType());
1050 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001051}
1052
Nate Begeman452d7be2005-09-16 00:54:12 +00001053SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1054 SDOperand N0 = N->getOperand(0);
1055 SDOperand N1 = N->getOperand(1);
1056 SDOperand N2 = N->getOperand(2);
1057 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1058 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1059 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1060 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001061
Nate Begeman452d7be2005-09-16 00:54:12 +00001062 // fold select C, X, X -> X
1063 if (N1 == N2)
1064 return N1;
1065 // fold select true, X, Y -> X
1066 if (N0C && !N0C->isNullValue())
1067 return N1;
1068 // fold select false, X, Y -> Y
1069 if (N0C && N0C->isNullValue())
1070 return N2;
1071 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001072 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001073 return DAG.getNode(ISD::OR, VT, N0, N2);
1074 // fold select C, 0, X -> ~C & X
1075 // FIXME: this should check for C type == X type, not i1?
1076 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1077 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1078 WorkList.push_back(XORNode.Val);
1079 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1080 }
1081 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001082 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001083 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1084 WorkList.push_back(XORNode.Val);
1085 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1086 }
1087 // fold select C, X, 0 -> C & X
1088 // FIXME: this should check for C type == X type, not i1?
1089 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1090 return DAG.getNode(ISD::AND, VT, N0, N1);
1091 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1092 if (MVT::i1 == VT && N0 == N1)
1093 return DAG.getNode(ISD::OR, VT, N0, N2);
1094 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1095 if (MVT::i1 == VT && N0 == N2)
1096 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman44728a72005-09-19 22:34:01 +00001097 // fold selects based on a setcc into other things, such as min/max/abs
1098 if (N0.getOpcode() == ISD::SETCC)
1099 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001100 return SDOperand();
1101}
1102
1103SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001104 SDOperand N0 = N->getOperand(0);
1105 SDOperand N1 = N->getOperand(1);
1106 SDOperand N2 = N->getOperand(2);
1107 SDOperand N3 = N->getOperand(3);
1108 SDOperand N4 = N->getOperand(4);
1109 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1110 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1111 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1112 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1113
1114 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001115 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001116 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1117
Nate Begeman44728a72005-09-19 22:34:01 +00001118 // fold select_cc lhs, rhs, x, x, cc -> x
1119 if (N2 == N3)
1120 return N2;
Nate Begeman44728a72005-09-19 22:34:01 +00001121 // fold select_cc into other things, such as min/max/abs
1122 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001123}
1124
1125SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1126 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1127 cast<CondCodeSDNode>(N->getOperand(2))->get());
1128}
1129
Nate Begeman83e75ec2005-09-06 04:43:02 +00001130SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001131 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001133 MVT::ValueType VT = N->getValueType(0);
1134
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001137 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001138 // fold (sext (sext x)) -> (sext x)
1139 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001140 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1141 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001142}
1143
Nate Begeman83e75ec2005-09-06 04:43:02 +00001144SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001145 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001146 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001147 MVT::ValueType VT = N->getValueType(0);
1148
Nate Begeman1d4d4142005-09-01 00:19:25 +00001149 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001150 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001151 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001152 // fold (zext (zext x)) -> (zext x)
1153 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001154 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1155 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001156}
1157
Nate Begeman83e75ec2005-09-06 04:43:02 +00001158SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001159 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001160 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001161 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001162 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001163 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001164
Nate Begeman1d4d4142005-09-01 00:19:25 +00001165 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001166 if (N0C) {
1167 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001168 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001169 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001170 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001173 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001174 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001175 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1176 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1177 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001178 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1181 if (N0.getOpcode() == ISD::AssertSext &&
1182 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001183 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001184 }
1185 // fold (sext_in_reg (sextload x)) -> (sextload x)
1186 if (N0.getOpcode() == ISD::SEXTLOAD &&
1187 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001188 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001189 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001190 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001191 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +00001192 if (N0.getOpcode() == ISD::SETCC &&
1193 TLI.getSetCCResultContents() ==
1194 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001195 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196 // FIXME: this code is currently just ported over from SelectionDAG.cpp
1197 // we probably actually want to handle this in two pieces. Rather than
1198 // checking all the top bits for zero, just check the sign bit here and turn
1199 // it into a zero extend inreg (AND with constant).
1200 // then, let the code for AND figure out if the mask is superfluous rather
1201 // than doing so here.
1202 if (N0.getOpcode() == ISD::AND &&
1203 N0.getOperand(1).getOpcode() == ISD::Constant) {
1204 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1205 unsigned NumBits = MVT::getSizeInBits(EVT);
1206 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001207 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001208 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001209 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210}
1211
Nate Begeman83e75ec2005-09-06 04:43:02 +00001212SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001213 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001214 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001215 MVT::ValueType VT = N->getValueType(0);
1216
1217 // noop truncate
1218 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001219 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001221 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001222 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001223 // fold (truncate (truncate x)) -> (truncate x)
1224 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001225 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001226 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1227 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1228 if (N0.getValueType() < VT)
1229 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001230 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001231 else if (N0.getValueType() > VT)
1232 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001233 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234 else
1235 // if the source and dest are the same type, we can drop both the extend
1236 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001237 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001238 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001239 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001240}
1241
Chris Lattner01b3d732005-09-28 22:28:18 +00001242SDOperand DAGCombiner::visitFADD(SDNode *N) {
1243 SDOperand N0 = N->getOperand(0);
1244 SDOperand N1 = N->getOperand(1);
1245 MVT::ValueType VT = N->getValueType(0);
1246
1247 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1248 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1249 // fold floating point (fadd c1, c2)
1250 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(),
1251 N->getValueType(0));
1252 }
1253 // fold (A + (-B)) -> A-B
1254 if (N1.getOpcode() == ISD::FNEG)
1255 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
1256
1257 // fold ((-A) + B) -> B-A
1258 if (N0.getOpcode() == ISD::FNEG)
1259 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
1260
1261 return SDOperand();
1262}
1263
1264SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1265 SDOperand N0 = N->getOperand(0);
1266 SDOperand N1 = N->getOperand(1);
1267 MVT::ValueType VT = N->getValueType(0);
1268
1269 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1270 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1271 // fold floating point (fsub c1, c2)
1272 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
1273 N->getValueType(0));
1274 }
1275 // fold (A-(-B)) -> A+B
1276 if (N1.getOpcode() == ISD::FNEG)
1277 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
1278
1279 return SDOperand();
1280}
1281
1282SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1283 SDOperand N0 = N->getOperand(0);
1284 SDOperand N1 = N->getOperand(1);
1285 MVT::ValueType VT = N->getValueType(0);
1286
1287 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1288 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1289 // fold floating point (fmul c1, c2)
1290 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
1291 N->getValueType(0));
1292 }
1293 return SDOperand();
1294}
1295
1296SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1297 SDOperand N0 = N->getOperand(0);
1298 SDOperand N1 = N->getOperand(1);
1299 MVT::ValueType VT = N->getValueType(0);
1300
1301 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1302 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1303 // fold floating point (fdiv c1, c2)
1304 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
1305 N->getValueType(0));
1306 }
1307 return SDOperand();
1308}
1309
1310SDOperand DAGCombiner::visitFREM(SDNode *N) {
1311 SDOperand N0 = N->getOperand(0);
1312 SDOperand N1 = N->getOperand(1);
1313 MVT::ValueType VT = N->getValueType(0);
1314
1315 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1316 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1317 // fold floating point (frem c1, c2) -> fmod(c1, c2)
1318 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
1319 N->getValueType(0));
1320 }
1321 return SDOperand();
1322}
1323
1324
Nate Begeman83e75ec2005-09-06 04:43:02 +00001325SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001328
1329 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001330 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1332 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333}
1334
Nate Begeman83e75ec2005-09-06 04:43:02 +00001335SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001336 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001337 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338
1339 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001340 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001341 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1342 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343}
1344
Nate Begeman83e75ec2005-09-06 04:43:02 +00001345SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001346 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347
1348 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001349 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001350 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1351 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001352}
1353
Nate Begeman83e75ec2005-09-06 04:43:02 +00001354SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001355 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356
1357 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001358 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001359 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1360 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361}
1362
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001364 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001365
1366 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001367 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001368 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1369 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370}
1371
Nate Begeman83e75ec2005-09-06 04:43:02 +00001372SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001373 SDOperand N0 = N->getOperand(0);
1374 MVT::ValueType VT = N->getValueType(0);
1375 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001376 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001379 if (N0CFP) {
1380 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001381 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001382 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001383 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001384}
1385
Nate Begeman83e75ec2005-09-06 04:43:02 +00001386SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001387 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001388
1389 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001390 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1392 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001393}
1394
Nate Begeman83e75ec2005-09-06 04:43:02 +00001395SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001396 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001398 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001399 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001400 // fold (neg (sub x, y)) -> (sub y, x)
1401 if (N->getOperand(0).getOpcode() == ISD::SUB)
1402 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001403 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001404 // fold (neg (neg x)) -> x
1405 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001406 return N->getOperand(0).getOperand(0);
1407 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408}
1409
Nate Begeman83e75ec2005-09-06 04:43:02 +00001410SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001411 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001412 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001413 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001414 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001415 // fold (fabs (fabs x)) -> (fabs x)
1416 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001417 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001418 // fold (fabs (fneg x)) -> (fabs x)
1419 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1420 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001421 N->getOperand(0).getOperand(0));
1422 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001423}
1424
Nate Begeman44728a72005-09-19 22:34:01 +00001425SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1426 SDOperand Chain = N->getOperand(0);
1427 SDOperand N1 = N->getOperand(1);
1428 SDOperand N2 = N->getOperand(2);
1429 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1430
1431 // never taken branch, fold to chain
1432 if (N1C && N1C->isNullValue())
1433 return Chain;
1434 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001435 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001436 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1437 return SDOperand();
1438}
1439
1440SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1441 SDOperand Chain = N->getOperand(0);
1442 SDOperand N1 = N->getOperand(1);
1443 SDOperand N2 = N->getOperand(2);
1444 SDOperand N3 = N->getOperand(3);
1445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1446
1447 // unconditional branch to true mbb
1448 if (N1C && N1C->getValue() == 1)
1449 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1450 // unconditional branch to false mbb
1451 if (N1C && N1C->isNullValue())
1452 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1453 return SDOperand();
1454}
1455
Chris Lattner3ea0b472005-10-05 06:47:48 +00001456// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
1457//
Nate Begeman44728a72005-09-19 22:34:01 +00001458SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00001459 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
1460 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
1461
1462 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00001463 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
1464 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
1465
1466 // fold br_cc true, dest -> br dest (unconditional branch)
1467 if (SCCC && SCCC->getValue())
1468 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
1469 N->getOperand(4));
1470 // fold br_cc false, dest -> unconditional fall through
1471 if (SCCC && SCCC->isNullValue())
1472 return N->getOperand(0);
1473 // fold to a simpler setcc
1474 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
1475 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
1476 Simp.getOperand(2), Simp.getOperand(0),
1477 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00001478 return SDOperand();
1479}
1480
1481SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00001482 SDOperand Chain = N->getOperand(0);
1483 SDOperand CCN = N->getOperand(1);
1484 SDOperand LHS = N->getOperand(2);
1485 SDOperand RHS = N->getOperand(3);
1486 SDOperand N4 = N->getOperand(4);
1487 SDOperand N5 = N->getOperand(5);
1488
1489 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
1490 cast<CondCodeSDNode>(CCN)->get(), false);
1491 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1492
1493 // fold select_cc lhs, rhs, x, x, cc -> x
1494 if (N4 == N5)
1495 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1496 // fold select_cc true, x, y -> x
1497 if (SCCC && SCCC->getValue())
1498 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1499 // fold select_cc false, x, y -> y
1500 if (SCCC && SCCC->isNullValue())
1501 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
1502 // fold to a simpler setcc
1503 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1504 return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0),
1505 SCC.getOperand(1), N4, N5);
Nate Begeman44728a72005-09-19 22:34:01 +00001506 return SDOperand();
1507}
1508
1509SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00001510 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
1511
1512 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
1513 cast<CondCodeSDNode>(N0.getOperand(2))->get());
1514 // If we got a simplified select_cc node back from SimplifySelectCC, then
1515 // break it down into a new SETCC node, and a new SELECT node, and then return
1516 // the SELECT node, since we were called with a SELECT node.
1517 if (SCC.Val) {
1518 // Check to see if we got a select_cc back (to turn into setcc/select).
1519 // Otherwise, just return whatever node we got back, like fabs.
1520 if (SCC.getOpcode() == ISD::SELECT_CC) {
1521 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
1522 SCC.getOperand(0), SCC.getOperand(1),
1523 SCC.getOperand(4));
1524 WorkList.push_back(SETCC.Val);
1525 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
1526 SCC.getOperand(3), SETCC);
1527 }
1528 return SCC;
1529 }
Nate Begeman44728a72005-09-19 22:34:01 +00001530 return SDOperand();
1531}
1532
1533SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1534 SDOperand N2, SDOperand N3,
1535 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00001536
1537 MVT::ValueType VT = N2.getValueType();
1538 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1539 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1540 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1541 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1542
1543 // Determine if the condition we're dealing with is constant
1544 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1545 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1546
1547 // fold select_cc true, x, y -> x
1548 if (SCCC && SCCC->getValue())
1549 return N2;
1550 // fold select_cc false, x, y -> y
1551 if (SCCC && SCCC->getValue() == 0)
1552 return N3;
1553
1554 // Check to see if we can simplify the select into an fabs node
1555 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1556 // Allow either -0.0 or 0.0
1557 if (CFP->getValue() == 0.0) {
1558 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1559 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
1560 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
1561 N2 == N3.getOperand(0))
1562 return DAG.getNode(ISD::FABS, VT, N0);
1563
1564 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1565 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
1566 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
1567 N2.getOperand(0) == N3)
1568 return DAG.getNode(ISD::FABS, VT, N3);
1569 }
1570 }
1571
1572 // Check to see if we can perform the "gzip trick", transforming
1573 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
1574 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
1575 MVT::isInteger(N0.getValueType()) &&
1576 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
1577 MVT::ValueType XType = N0.getValueType();
1578 MVT::ValueType AType = N2.getValueType();
1579 if (XType >= AType) {
1580 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
1581 // single-bit constant. FIXME: remove once the dag combiner
1582 // exists.
1583 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
1584 unsigned ShCtV = Log2_64(N2C->getValue());
1585 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1586 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
1587 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
1588 WorkList.push_back(Shift.Val);
1589 if (XType > AType) {
1590 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1591 WorkList.push_back(Shift.Val);
1592 }
1593 return DAG.getNode(ISD::AND, AType, Shift, N2);
1594 }
1595 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1596 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1597 TLI.getShiftAmountTy()));
1598 WorkList.push_back(Shift.Val);
1599 if (XType > AType) {
1600 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1601 WorkList.push_back(Shift.Val);
1602 }
1603 return DAG.getNode(ISD::AND, AType, Shift, N2);
1604 }
1605 }
1606
1607 // Check to see if this is the equivalent of setcc
1608 // FIXME: Turn all of these into setcc if setcc if setcc is legal
1609 // otherwise, go ahead with the folds.
1610 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
1611 MVT::ValueType XType = N0.getValueType();
1612 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
1613 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1614 if (Res.getValueType() != VT)
1615 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
1616 return Res;
1617 }
1618
1619 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
1620 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
1621 TLI.isOperationLegal(ISD::CTLZ, XType)) {
1622 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
1623 return DAG.getNode(ISD::SRL, XType, Ctlz,
1624 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
1625 TLI.getShiftAmountTy()));
1626 }
1627 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
1628 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
1629 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
1630 N0);
1631 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
1632 DAG.getConstant(~0ULL, XType));
1633 return DAG.getNode(ISD::SRL, XType,
1634 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
1635 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1636 TLI.getShiftAmountTy()));
1637 }
1638 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
1639 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
1640 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
1641 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1642 TLI.getShiftAmountTy()));
1643 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
1644 }
1645 }
1646
1647 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
1648 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
1649 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
1650 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
1651 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
1652 MVT::ValueType XType = N0.getValueType();
1653 if (SubC->isNullValue() && MVT::isInteger(XType)) {
1654 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1655 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1656 TLI.getShiftAmountTy()));
1657 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
1658 WorkList.push_back(Shift.Val);
1659 WorkList.push_back(Add.Val);
1660 return DAG.getNode(ISD::XOR, XType, Add, Shift);
1661 }
1662 }
1663 }
1664
Nate Begeman44728a72005-09-19 22:34:01 +00001665 return SDOperand();
1666}
1667
Nate Begeman452d7be2005-09-16 00:54:12 +00001668SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00001669 SDOperand N1, ISD::CondCode Cond,
1670 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001671 // These setcc operations always fold.
1672 switch (Cond) {
1673 default: break;
1674 case ISD::SETFALSE:
1675 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1676 case ISD::SETTRUE:
1677 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1678 }
1679
1680 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1681 uint64_t C1 = N1C->getValue();
1682 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1683 uint64_t C0 = N0C->getValue();
1684
1685 // Sign extend the operands if required
1686 if (ISD::isSignedIntSetCC(Cond)) {
1687 C0 = N0C->getSignExtended();
1688 C1 = N1C->getSignExtended();
1689 }
1690
1691 switch (Cond) {
1692 default: assert(0 && "Unknown integer setcc!");
1693 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1694 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1695 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1696 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1697 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1698 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1699 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1700 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1701 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1702 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1703 }
1704 } else {
1705 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1706 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1707 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1708
1709 // If the comparison constant has bits in the upper part, the
1710 // zero-extended value could never match.
1711 if (C1 & (~0ULL << InSize)) {
1712 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1713 switch (Cond) {
1714 case ISD::SETUGT:
1715 case ISD::SETUGE:
1716 case ISD::SETEQ: return DAG.getConstant(0, VT);
1717 case ISD::SETULT:
1718 case ISD::SETULE:
1719 case ISD::SETNE: return DAG.getConstant(1, VT);
1720 case ISD::SETGT:
1721 case ISD::SETGE:
1722 // True if the sign bit of C1 is set.
1723 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
1724 case ISD::SETLT:
1725 case ISD::SETLE:
1726 // True if the sign bit of C1 isn't set.
1727 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
1728 default:
1729 break;
1730 }
1731 }
1732
1733 // Otherwise, we can perform the comparison with the low bits.
1734 switch (Cond) {
1735 case ISD::SETEQ:
1736 case ISD::SETNE:
1737 case ISD::SETUGT:
1738 case ISD::SETUGE:
1739 case ISD::SETULT:
1740 case ISD::SETULE:
1741 return DAG.getSetCC(VT, N0.getOperand(0),
1742 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
1743 Cond);
1744 default:
1745 break; // todo, be more careful with signed comparisons
1746 }
1747 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1748 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1749 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1750 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1751 MVT::ValueType ExtDstTy = N0.getValueType();
1752 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1753
1754 // If the extended part has any inconsistent bits, it cannot ever
1755 // compare equal. In other words, they have to be all ones or all
1756 // zeros.
1757 uint64_t ExtBits =
1758 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1759 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1760 return DAG.getConstant(Cond == ISD::SETNE, VT);
1761
1762 SDOperand ZextOp;
1763 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1764 if (Op0Ty == ExtSrcTy) {
1765 ZextOp = N0.getOperand(0);
1766 } else {
1767 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1768 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1769 DAG.getConstant(Imm, Op0Ty));
1770 }
1771 WorkList.push_back(ZextOp.Val);
1772 // Otherwise, make this a use of a zext.
1773 return DAG.getSetCC(VT, ZextOp,
1774 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1775 ExtDstTy),
1776 Cond);
1777 }
Chris Lattner5c46f742005-10-05 06:11:08 +00001778
Nate Begeman452d7be2005-09-16 00:54:12 +00001779 uint64_t MinVal, MaxVal;
1780 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1781 if (ISD::isSignedIntSetCC(Cond)) {
1782 MinVal = 1ULL << (OperandBitSize-1);
1783 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1784 MaxVal = ~0ULL >> (65-OperandBitSize);
1785 else
1786 MaxVal = 0;
1787 } else {
1788 MinVal = 0;
1789 MaxVal = ~0ULL >> (64-OperandBitSize);
1790 }
1791
1792 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1793 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1794 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1795 --C1; // X >= C0 --> X > (C0-1)
1796 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1797 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1798 }
1799
1800 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1801 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1802 ++C1; // X <= C0 --> X < (C0+1)
1803 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1804 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1805 }
1806
1807 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1808 return DAG.getConstant(0, VT); // X < MIN --> false
1809
1810 // Canonicalize setgt X, Min --> setne X, Min
1811 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1812 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1813
1814 // If we have setult X, 1, turn it into seteq X, 0
1815 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1816 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1817 ISD::SETEQ);
1818 // If we have setugt X, Max-1, turn it into seteq X, Max
1819 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1820 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1821 ISD::SETEQ);
1822
1823 // If we have "setcc X, C0", check to see if we can shrink the immediate
1824 // by changing cc.
1825
1826 // SETUGT X, SINTMAX -> SETLT X, 0
1827 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1828 C1 == (~0ULL >> (65-OperandBitSize)))
1829 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1830 ISD::SETLT);
1831
1832 // FIXME: Implement the rest of these.
1833
1834 // Fold bit comparisons when we can.
1835 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1836 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1837 if (ConstantSDNode *AndRHS =
1838 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1839 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1840 // Perform the xform if the AND RHS is a single bit.
1841 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1842 return DAG.getNode(ISD::SRL, VT, N0,
1843 DAG.getConstant(Log2_64(AndRHS->getValue()),
1844 TLI.getShiftAmountTy()));
1845 }
1846 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1847 // (X & 8) == 8 --> (X & 8) >> 3
1848 // Perform the xform if C1 is a single bit.
1849 if ((C1 & (C1-1)) == 0) {
1850 return DAG.getNode(ISD::SRL, VT, N0,
1851 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
1852 }
1853 }
1854 }
1855 }
1856 } else if (isa<ConstantSDNode>(N0.Val)) {
1857 // Ensure that the constant occurs on the RHS.
1858 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1859 }
1860
1861 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
1862 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1863 double C0 = N0C->getValue(), C1 = N1C->getValue();
1864
1865 switch (Cond) {
1866 default: break; // FIXME: Implement the rest of these!
1867 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1868 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1869 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
1870 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
1871 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
1872 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
1873 }
1874 } else {
1875 // Ensure that the constant occurs on the RHS.
1876 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1877 }
1878
1879 if (N0 == N1) {
1880 // We can always fold X == Y for integer setcc's.
1881 if (MVT::isInteger(N0.getValueType()))
1882 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1883 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1884 if (UOF == 2) // FP operators that are undefined on NaNs.
1885 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1886 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1887 return DAG.getConstant(UOF, VT);
1888 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1889 // if it is not already.
1890 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
1891 if (NewCond != Cond)
1892 return DAG.getSetCC(VT, N0, N1, NewCond);
1893 }
1894
1895 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1896 MVT::isInteger(N0.getValueType())) {
1897 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1898 N0.getOpcode() == ISD::XOR) {
1899 // Simplify (X+Y) == (X+Z) --> Y == Z
1900 if (N0.getOpcode() == N1.getOpcode()) {
1901 if (N0.getOperand(0) == N1.getOperand(0))
1902 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1903 if (N0.getOperand(1) == N1.getOperand(1))
1904 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1905 if (isCommutativeBinOp(N0.getOpcode())) {
1906 // If X op Y == Y op X, try other combinations.
1907 if (N0.getOperand(0) == N1.getOperand(1))
1908 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1909 if (N0.getOperand(1) == N1.getOperand(0))
1910 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1911 }
1912 }
1913
Chris Lattner5c46f742005-10-05 06:11:08 +00001914 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
1915 if (N0.getOpcode() == ISD::XOR)
1916 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1917 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1918 // If we know that all of the inverted bits are zero, don't bother
1919 // performing the inversion.
1920 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
1921 return DAG.getSetCC(VT, N0.getOperand(0),
1922 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
1923 N0.getValueType()), Cond);
1924 }
1925
Nate Begeman452d7be2005-09-16 00:54:12 +00001926 // Simplify (X+Z) == X --> Z == 0
1927 if (N0.getOperand(0) == N1)
1928 return DAG.getSetCC(VT, N0.getOperand(1),
1929 DAG.getConstant(0, N0.getValueType()), Cond);
1930 if (N0.getOperand(1) == N1) {
1931 if (isCommutativeBinOp(N0.getOpcode()))
1932 return DAG.getSetCC(VT, N0.getOperand(0),
1933 DAG.getConstant(0, N0.getValueType()), Cond);
1934 else {
1935 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1936 // (Z-X) == X --> Z == X<<1
1937 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1938 N1,
1939 DAG.getConstant(1,TLI.getShiftAmountTy()));
1940 WorkList.push_back(SH.Val);
1941 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1942 }
1943 }
1944 }
1945
1946 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1947 N1.getOpcode() == ISD::XOR) {
1948 // Simplify X == (X+Z) --> Z == 0
1949 if (N1.getOperand(0) == N0) {
1950 return DAG.getSetCC(VT, N1.getOperand(1),
1951 DAG.getConstant(0, N1.getValueType()), Cond);
1952 } else if (N1.getOperand(1) == N0) {
1953 if (isCommutativeBinOp(N1.getOpcode())) {
1954 return DAG.getSetCC(VT, N1.getOperand(0),
1955 DAG.getConstant(0, N1.getValueType()), Cond);
1956 } else {
1957 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1958 // X == (Z-X) --> X<<1 == Z
1959 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1960 DAG.getConstant(1,TLI.getShiftAmountTy()));
1961 WorkList.push_back(SH.Val);
1962 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1963 }
1964 }
1965 }
1966 }
1967
1968 // Fold away ALL boolean setcc's.
1969 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00001970 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001971 switch (Cond) {
1972 default: assert(0 && "Unknown integer setcc!");
1973 case ISD::SETEQ: // X == Y -> (X^Y)^1
1974 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1975 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1976 WorkList.push_back(Temp.Val);
1977 break;
1978 case ISD::SETNE: // X != Y --> (X^Y)
1979 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1980 break;
1981 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1982 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1983 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1984 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1985 WorkList.push_back(Temp.Val);
1986 break;
1987 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1988 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1989 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1990 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1991 WorkList.push_back(Temp.Val);
1992 break;
1993 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1994 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1995 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1996 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1997 WorkList.push_back(Temp.Val);
1998 break;
1999 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2000 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2001 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2002 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2003 break;
2004 }
2005 if (VT != MVT::i1) {
2006 WorkList.push_back(N0.Val);
2007 // FIXME: If running after legalize, we probably can't do this.
2008 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2009 }
2010 return N0;
2011 }
2012
2013 // Could not fold it.
2014 return SDOperand();
2015}
2016
Nate Begeman1d4d4142005-09-01 00:19:25 +00002017// SelectionDAG::Combine - This is the entry point for the file.
2018//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002019void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002020 /// run - This is the main entry point to this class.
2021 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002022 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002023}