blob: 4e058640ff413df3f9771e41a212f8e6f2638327 [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, pow2, pow2 -> something smart
24// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
25// FIXME: (select C, load A, load B) -> load (select C, A, B)
Nate Begeman44728a72005-09-19 22:34:01 +000026// FIXME: Dead stores -> nuke
27// FIXME: shr X, (and Y,31) -> shr X, Y
28// FIXME: TRUNC (LOAD) -> EXT_LOAD/LOAD(smaller)
Nate Begeman1d4d4142005-09-01 00:19:25 +000029// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000030// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000031// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman4ebd8052005-09-01 23:24:04 +000032// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000033// FIXME: verify that getNode can't return extends with an operand whose type
34// is >= to that of the extend.
35// FIXME: divide by zero is currently left unfolded. do we want to turn this
36// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000037// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Chris Lattner01a22022005-10-10 22:04:48 +000038// FIXME: reassociate (X+C)+Y into (X+Y)+C if the inner expression has one use
Nate Begeman1d4d4142005-09-01 00:19:25 +000039//
40//===----------------------------------------------------------------------===//
41
42#define DEBUG_TYPE "dagcombine"
43#include "llvm/ADT/Statistic.h"
44#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000045#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000046#include "llvm/Support/MathExtras.h"
47#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000048#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000049#include <cmath>
50using namespace llvm;
51
52namespace {
53 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
54
55 class DAGCombiner {
56 SelectionDAG &DAG;
57 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000058 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000059
60 // Worklist of all of the nodes that need to be simplified.
61 std::vector<SDNode*> WorkList;
62
63 /// AddUsersToWorkList - When an instruction is simplified, add all users of
64 /// the instruction to the work lists because they might get more simplified
65 /// now.
66 ///
67 void AddUsersToWorkList(SDNode *N) {
68 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000069 UI != UE; ++UI)
70 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000071 }
72
73 /// removeFromWorkList - remove all instances of N from the worklist.
74 void removeFromWorkList(SDNode *N) {
75 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
76 WorkList.end());
77 }
78
Chris Lattner01a22022005-10-10 22:04:48 +000079 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000080 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000081 DEBUG(std::cerr << "\nReplacing "; N->dump();
82 std::cerr << "\nWith: "; To[0].Val->dump();
83 std::cerr << " and " << To.size()-1 << " other values\n");
84 std::vector<SDNode*> NowDead;
85 DAG.ReplaceAllUsesWith(N, To, &NowDead);
86
87 // Push the new nodes and any users onto the worklist
88 for (unsigned i = 0, e = To.size(); i != e; ++i) {
89 WorkList.push_back(To[i].Val);
90 AddUsersToWorkList(To[i].Val);
91 }
92
93 // Nodes can end up on the worklist more than once. Make sure we do
94 // not process a node that has been replaced.
95 removeFromWorkList(N);
96 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
97 removeFromWorkList(NowDead[i]);
98
99 // Finally, since the node is now dead, remove it from the graph.
100 DAG.DeleteNode(N);
101 return SDOperand(N, 0);
102 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000103
104 SDOperand CombineTo(SDNode *N, SDOperand Res) {
105 std::vector<SDOperand> To;
106 To.push_back(Res);
107 return CombineTo(N, To);
108 }
Chris Lattner01a22022005-10-10 22:04:48 +0000109
110 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
111 std::vector<SDOperand> To;
112 To.push_back(Res0);
113 To.push_back(Res1);
114 return CombineTo(N, To);
115 }
116
Nate Begeman1d4d4142005-09-01 00:19:25 +0000117 /// visit - call the node-specific routine that knows how to fold each
118 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000119 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000120
121 // Visitation implementation - Implement dag node combining for different
122 // node types. The semantics are as follows:
123 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000124 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000125 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000126 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000127 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000128 SDOperand visitTokenFactor(SDNode *N);
129 SDOperand visitADD(SDNode *N);
130 SDOperand visitSUB(SDNode *N);
131 SDOperand visitMUL(SDNode *N);
132 SDOperand visitSDIV(SDNode *N);
133 SDOperand visitUDIV(SDNode *N);
134 SDOperand visitSREM(SDNode *N);
135 SDOperand visitUREM(SDNode *N);
136 SDOperand visitMULHU(SDNode *N);
137 SDOperand visitMULHS(SDNode *N);
138 SDOperand visitAND(SDNode *N);
139 SDOperand visitOR(SDNode *N);
140 SDOperand visitXOR(SDNode *N);
141 SDOperand visitSHL(SDNode *N);
142 SDOperand visitSRA(SDNode *N);
143 SDOperand visitSRL(SDNode *N);
144 SDOperand visitCTLZ(SDNode *N);
145 SDOperand visitCTTZ(SDNode *N);
146 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000147 SDOperand visitSELECT(SDNode *N);
148 SDOperand visitSELECT_CC(SDNode *N);
149 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000150 SDOperand visitSIGN_EXTEND(SDNode *N);
151 SDOperand visitZERO_EXTEND(SDNode *N);
152 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
153 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000154
155 SDOperand visitFADD(SDNode *N);
156 SDOperand visitFSUB(SDNode *N);
157 SDOperand visitFMUL(SDNode *N);
158 SDOperand visitFDIV(SDNode *N);
159 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000160 SDOperand visitSINT_TO_FP(SDNode *N);
161 SDOperand visitUINT_TO_FP(SDNode *N);
162 SDOperand visitFP_TO_SINT(SDNode *N);
163 SDOperand visitFP_TO_UINT(SDNode *N);
164 SDOperand visitFP_ROUND(SDNode *N);
165 SDOperand visitFP_ROUND_INREG(SDNode *N);
166 SDOperand visitFP_EXTEND(SDNode *N);
167 SDOperand visitFNEG(SDNode *N);
168 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000169 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000170 SDOperand visitBRCONDTWOWAY(SDNode *N);
171 SDOperand visitBR_CC(SDNode *N);
172 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000173
Chris Lattner01a22022005-10-10 22:04:48 +0000174 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000175 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000176
Nate Begeman44728a72005-09-19 22:34:01 +0000177 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
178 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
179 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000180 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000181 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000182public:
183 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000184 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000185
186 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000187 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000188 };
189}
190
Nate Begeman07ed4172005-10-10 21:26:48 +0000191/// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We use
192/// this predicate to simplify operations downstream. Op and Mask are known to
Nate Begeman1d4d4142005-09-01 00:19:25 +0000193/// be the same type.
194static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
195 const TargetLowering &TLI) {
196 unsigned SrcBits;
197 if (Mask == 0) return true;
198
199 // If we know the result of a setcc has the top bits zero, use this info.
200 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000201 case ISD::Constant:
202 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
203 case ISD::SETCC:
204 return ((Mask & 1) == 0) &&
205 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
206 case ISD::ZEXTLOAD:
207 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
208 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
209 case ISD::ZERO_EXTEND:
210 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
211 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
212 case ISD::AssertZext:
213 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
214 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
215 case ISD::AND:
Chris Lattneree899e62005-10-09 22:12:36 +0000216 // If either of the operands has zero bits, the result will too.
217 if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) ||
218 MaskedValueIsZero(Op.getOperand(0), Mask, TLI))
219 return true;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000220 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
221 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
222 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
Chris Lattneree899e62005-10-09 22:12:36 +0000223 return false;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000224 case ISD::OR:
225 case ISD::XOR:
226 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
227 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
228 case ISD::SELECT:
229 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
230 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
231 case ISD::SELECT_CC:
232 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
233 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
234 case ISD::SRL:
235 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
236 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
237 uint64_t NewVal = Mask << ShAmt->getValue();
238 SrcBits = MVT::getSizeInBits(Op.getValueType());
239 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
240 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
241 }
242 return false;
243 case ISD::SHL:
244 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
245 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
246 uint64_t NewVal = Mask >> ShAmt->getValue();
247 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
248 }
249 return false;
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000250 case ISD::ADD:
Chris Lattnerd7390752005-10-10 16:52:03 +0000251 // (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits.
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000252 if ((Mask&(Mask+1)) == 0) { // All low bits
253 if (MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
254 MaskedValueIsZero(Op.getOperand(1), Mask, TLI))
255 return true;
256 }
257 break;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000258 case ISD::SUB:
259 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
260 // We know that the top bits of C-X are clear if X contains less bits
261 // than C (i.e. no wrap-around can happen). For example, 20-X is
262 // positive if we can prove that X is >= 0 and < 16.
263 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
264 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
265 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
266 uint64_t MaskV = (1ULL << (63-NLZ))-1;
267 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
268 // High bits are clear this value is known to be >= C.
269 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
270 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
271 return true;
272 }
273 }
274 }
275 break;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000276 case ISD::CTTZ:
277 case ISD::CTLZ:
278 case ISD::CTPOP:
279 // Bit counting instructions can not set the high bits of the result
280 // register. The max number of bits sets depends on the input.
281 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000282 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000283 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000284 return false;
285}
286
Nate Begeman4ebd8052005-09-01 23:24:04 +0000287// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
288// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000289// Also, set the incoming LHS, RHS, and CC references to the appropriate
290// nodes based on the type of node we are checking. This simplifies life a
291// bit for the callers.
292static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
293 SDOperand &CC) {
294 if (N.getOpcode() == ISD::SETCC) {
295 LHS = N.getOperand(0);
296 RHS = N.getOperand(1);
297 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000298 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000299 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000300 if (N.getOpcode() == ISD::SELECT_CC &&
301 N.getOperand(2).getOpcode() == ISD::Constant &&
302 N.getOperand(3).getOpcode() == ISD::Constant &&
303 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000304 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
305 LHS = N.getOperand(0);
306 RHS = N.getOperand(1);
307 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000308 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000309 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000310 return false;
311}
312
Nate Begeman99801192005-09-07 23:25:52 +0000313// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
314// one use. If this is true, it allows the users to invert the operation for
315// free when it is profitable to do so.
316static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000317 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000318 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000319 return true;
320 return false;
321}
322
Nate Begeman452d7be2005-09-16 00:54:12 +0000323// FIXME: This should probably go in the ISD class rather than being duplicated
324// in several files.
325static bool isCommutativeBinOp(unsigned Opcode) {
326 switch (Opcode) {
327 case ISD::ADD:
328 case ISD::MUL:
329 case ISD::AND:
330 case ISD::OR:
331 case ISD::XOR: return true;
332 default: return false; // FIXME: Need commutative info for user ops!
333 }
334}
335
Nate Begeman4ebd8052005-09-01 23:24:04 +0000336void DAGCombiner::Run(bool RunningAfterLegalize) {
337 // set the instance variable, so that the various visit routines may use it.
338 AfterLegalize = RunningAfterLegalize;
339
Nate Begeman646d7e22005-09-02 21:18:40 +0000340 // Add all the dag nodes to the worklist.
341 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000342
Chris Lattner95038592005-10-05 06:35:28 +0000343 // Create a dummy node (which is not added to allnodes), that adds a reference
344 // to the root node, preventing it from being deleted, and tracking any
345 // changes of the root.
346 HandleSDNode Dummy(DAG.getRoot());
347
Nate Begeman1d4d4142005-09-01 00:19:25 +0000348 // while the worklist isn't empty, inspect the node on the end of it and
349 // try and combine it.
350 while (!WorkList.empty()) {
351 SDNode *N = WorkList.back();
352 WorkList.pop_back();
353
354 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000355 // N is deleted from the DAG, since they too may now be dead or may have a
356 // reduced number of uses, allowing other xforms.
357 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000358 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
359 WorkList.push_back(N->getOperand(i).Val);
360
Nate Begeman1d4d4142005-09-01 00:19:25 +0000361 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000362 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000363 continue;
364 }
365
Nate Begeman83e75ec2005-09-06 04:43:02 +0000366 SDOperand RV = visit(N);
367 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000368 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000369 // If we get back the same node we passed in, rather than a new node or
370 // zero, we know that the node must have defined multiple values and
371 // CombineTo was used. Since CombineTo takes care of the worklist
372 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000373 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000374 DEBUG(std::cerr << "\nReplacing "; N->dump();
375 std::cerr << "\nWith: "; RV.Val->dump();
376 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000377 std::vector<SDNode*> NowDead;
378 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000379
380 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000381 WorkList.push_back(RV.Val);
382 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000383
384 // Nodes can end up on the worklist more than once. Make sure we do
385 // not process a node that has been replaced.
386 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000387 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
388 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000389
390 // Finally, since the node is now dead, remove it from the graph.
391 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000392 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000393 }
394 }
Chris Lattner95038592005-10-05 06:35:28 +0000395
396 // If the root changed (e.g. it was a dead load, update the root).
397 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000398}
399
Nate Begeman83e75ec2005-09-06 04:43:02 +0000400SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000401 switch(N->getOpcode()) {
402 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000403 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000404 case ISD::ADD: return visitADD(N);
405 case ISD::SUB: return visitSUB(N);
406 case ISD::MUL: return visitMUL(N);
407 case ISD::SDIV: return visitSDIV(N);
408 case ISD::UDIV: return visitUDIV(N);
409 case ISD::SREM: return visitSREM(N);
410 case ISD::UREM: return visitUREM(N);
411 case ISD::MULHU: return visitMULHU(N);
412 case ISD::MULHS: return visitMULHS(N);
413 case ISD::AND: return visitAND(N);
414 case ISD::OR: return visitOR(N);
415 case ISD::XOR: return visitXOR(N);
416 case ISD::SHL: return visitSHL(N);
417 case ISD::SRA: return visitSRA(N);
418 case ISD::SRL: return visitSRL(N);
419 case ISD::CTLZ: return visitCTLZ(N);
420 case ISD::CTTZ: return visitCTTZ(N);
421 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000422 case ISD::SELECT: return visitSELECT(N);
423 case ISD::SELECT_CC: return visitSELECT_CC(N);
424 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000425 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
426 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
427 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
428 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000429 case ISD::FADD: return visitFADD(N);
430 case ISD::FSUB: return visitFSUB(N);
431 case ISD::FMUL: return visitFMUL(N);
432 case ISD::FDIV: return visitFDIV(N);
433 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000434 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
435 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
436 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
437 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
438 case ISD::FP_ROUND: return visitFP_ROUND(N);
439 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
440 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
441 case ISD::FNEG: return visitFNEG(N);
442 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000443 case ISD::BRCOND: return visitBRCOND(N);
444 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
445 case ISD::BR_CC: return visitBR_CC(N);
446 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000447 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000448 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000449 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000450 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000451}
452
Nate Begeman83e75ec2005-09-06 04:43:02 +0000453SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000454 std::vector<SDOperand> Ops;
455 bool Changed = false;
456
Nate Begeman1d4d4142005-09-01 00:19:25 +0000457 // If the token factor has two operands and one is the entry token, replace
458 // the token factor with the other operand.
459 if (N->getNumOperands() == 2) {
460 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000461 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000462 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000463 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000464 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000465
Nate Begemanded49632005-10-13 03:11:28 +0000466 // fold (tokenfactor (tokenfactor)) -> tokenfactor
467 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
468 SDOperand Op = N->getOperand(i);
469 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
470 Changed = true;
471 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
472 Ops.push_back(Op.getOperand(j));
473 } else {
474 Ops.push_back(Op);
475 }
476 }
477 if (Changed)
478 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000479 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000480}
481
Nate Begeman83e75ec2005-09-06 04:43:02 +0000482SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000483 SDOperand N0 = N->getOperand(0);
484 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000485 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
486 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000487 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000488
489 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000490 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000491 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000492 // canonicalize constant to RHS
493 if (N0C && !N1C) {
494 std::swap(N0, N1);
495 std::swap(N0C, N1C);
496 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000497 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000498 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000499 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000500 // fold (add (add x, c1), c2) -> (add x, c1+c2)
501 if (N1C && N0.getOpcode() == ISD::ADD) {
502 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
503 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
504 if (N00C)
505 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
506 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
507 if (N01C)
508 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
509 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
510 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000511 // fold ((0-A) + B) -> B-A
512 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
513 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000514 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000515 // fold (A + (0-B)) -> A-B
516 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
517 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000518 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000519 // fold (A+(B-A)) -> B
520 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000521 return N1.getOperand(0);
522 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000523}
524
Nate Begeman83e75ec2005-09-06 04:43:02 +0000525SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000526 SDOperand N0 = N->getOperand(0);
527 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000528 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
529 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000530
531 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000532 if (N0C && N1C)
533 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000534 N->getValueType(0));
Chris Lattner05b57432005-10-11 06:07:15 +0000535 // fold (sub x, c) -> (add x, -c)
536 if (N1C)
537 return DAG.getNode(ISD::ADD, N0.getValueType(), N0,
538 DAG.getConstant(-N1C->getValue(), N0.getValueType()));
539
Nate Begeman1d4d4142005-09-01 00:19:25 +0000540 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000541 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000542 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000544 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000545 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000546 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000547}
548
Nate Begeman83e75ec2005-09-06 04:43:02 +0000549SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000550 SDOperand N0 = N->getOperand(0);
551 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000552 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
553 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000554 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000555
556 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000557 if (N0C && N1C)
558 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000559 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000560 // canonicalize constant to RHS
561 if (N0C && !N1C) {
562 std::swap(N0, N1);
563 std::swap(N0C, N1C);
564 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000565 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000566 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000567 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000569 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000570 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000571 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000573 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000574 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000575 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000576 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000577 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
578 if (N1C && N0.getOpcode() == ISD::MUL) {
579 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
580 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
581 if (N00C)
582 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
583 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
584 if (N01C)
585 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
586 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
587 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000588 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000589}
590
Nate Begeman83e75ec2005-09-06 04:43:02 +0000591SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000592 SDOperand N0 = N->getOperand(0);
593 SDOperand N1 = N->getOperand(1);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000594 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000595 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
596 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597
598 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000599 if (N0C && N1C && !N1C->isNullValue())
600 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000601 N->getValueType(0));
Chris Lattner094c8fc2005-10-07 06:10:46 +0000602 // If we know the sign bits of both operands are zero, strength reduce to a
603 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
604 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
605 if (MaskedValueIsZero(N1, SignBit, TLI) &&
606 MaskedValueIsZero(N0, SignBit, TLI))
607 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000608 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000609}
610
Nate Begeman83e75ec2005-09-06 04:43:02 +0000611SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000612 SDOperand N0 = N->getOperand(0);
613 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000614 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
615 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000616
617 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000618 if (N0C && N1C && !N1C->isNullValue())
619 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000620 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000621 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000622 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000624 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000625 TLI.getShiftAmountTy()));
626 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000627}
628
Nate Begeman83e75ec2005-09-06 04:43:02 +0000629SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000630 SDOperand N0 = N->getOperand(0);
631 SDOperand N1 = N->getOperand(1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000632 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000633 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
634 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000635
636 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000637 if (N0C && N1C && !N1C->isNullValue())
638 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000639 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000640 // If we know the sign bits of both operands are zero, strength reduce to a
641 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
642 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
643 if (MaskedValueIsZero(N1, SignBit, TLI) &&
644 MaskedValueIsZero(N0, SignBit, TLI))
645 return DAG.getNode(ISD::UREM, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000646 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000647}
648
Nate Begeman83e75ec2005-09-06 04:43:02 +0000649SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000650 SDOperand N0 = N->getOperand(0);
651 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000652 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
653 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000654
655 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000656 if (N0C && N1C && !N1C->isNullValue())
657 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000658 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000659 // fold (urem x, pow2) -> (and x, pow2-1)
660 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
661 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
662 DAG.getConstant(N1C->getValue()-1, N1.getValueType()));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000663 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000664}
665
Nate Begeman83e75ec2005-09-06 04:43:02 +0000666SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000667 SDOperand N0 = N->getOperand(0);
668 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000669 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670
671 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000672 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000673 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000674 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000675 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000676 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
677 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000678 TLI.getShiftAmountTy()));
679 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000680}
681
Nate Begeman83e75ec2005-09-06 04:43:02 +0000682SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000683 SDOperand N0 = N->getOperand(0);
684 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000685 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000686
687 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000689 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000690 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000691 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000692 return DAG.getConstant(0, N0.getValueType());
693 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000694}
695
Nate Begeman83e75ec2005-09-06 04:43:02 +0000696SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000697 SDOperand N0 = N->getOperand(0);
698 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000699 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000700 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
701 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000702 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000703 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704
705 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000706 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000707 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000708 // canonicalize constant to RHS
709 if (N0C && !N1C) {
710 std::swap(N0, N1);
711 std::swap(N0C, N1C);
712 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000713 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000715 return N0;
716 // if (and x, c) is known to be zero, return 0
717 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
718 return DAG.getConstant(0, VT);
719 // fold (and x, c) -> x iff (x & ~c) == 0
720 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
721 TLI))
722 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000723 // fold (and (and x, c1), c2) -> (and x, c1^c2)
724 if (N1C && N0.getOpcode() == ISD::AND) {
725 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
726 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
727 if (N00C)
728 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
729 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
730 if (N01C)
731 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
732 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
733 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000734 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
735 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
736 unsigned ExtendBits =
737 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000738 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000739 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000740 }
741 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Chris Lattnera179ab32005-10-11 17:56:34 +0000742 if (N0.getOpcode() == ISD::OR && N1C)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000743 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000744 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000745 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000746 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
747 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
748 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
749 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
750
751 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
752 MVT::isInteger(LL.getValueType())) {
753 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
754 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
755 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
756 WorkList.push_back(ORNode.Val);
757 return DAG.getSetCC(VT, ORNode, LR, Op1);
758 }
759 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
760 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
761 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
762 WorkList.push_back(ANDNode.Val);
763 return DAG.getSetCC(VT, ANDNode, LR, Op1);
764 }
765 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
766 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
767 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
768 WorkList.push_back(ORNode.Val);
769 return DAG.getSetCC(VT, ORNode, LR, Op1);
770 }
771 }
772 // canonicalize equivalent to ll == rl
773 if (LL == RR && LR == RL) {
774 Op1 = ISD::getSetCCSwappedOperands(Op1);
775 std::swap(RL, RR);
776 }
777 if (LL == RL && LR == RR) {
778 bool isInteger = MVT::isInteger(LL.getValueType());
779 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
780 if (Result != ISD::SETCC_INVALID)
781 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
782 }
783 }
784 // fold (and (zext x), (zext y)) -> (zext (and x, y))
785 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
786 N1.getOpcode() == ISD::ZERO_EXTEND &&
787 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
788 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
789 N0.getOperand(0), N1.getOperand(0));
790 WorkList.push_back(ANDNode.Val);
791 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
792 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000793 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
794 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
795 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
796 N0.getOperand(1) == N1.getOperand(1)) {
797 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
798 N0.getOperand(0), N1.getOperand(0));
799 WorkList.push_back(ANDNode.Val);
800 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
801 }
Nate Begemanded49632005-10-13 03:11:28 +0000802 // fold (zext_inreg (extload x)) -> (zextload x)
803 if (N1C && N0.getOpcode() == ISD::EXTLOAD) {
804 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000805 // If we zero all the possible extended bits, then we can turn this into
806 // a zextload if we are running before legalize or the operation is legal.
807 if (MaskedValueIsZero(SDOperand(N,0), ~0ULL<<MVT::getSizeInBits(EVT),TLI) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +0000808 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000809 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
810 N0.getOperand(1), N0.getOperand(2),
811 EVT);
Nate Begemanded49632005-10-13 03:11:28 +0000812 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +0000813 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +0000814 return SDOperand();
815 }
816 }
817 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
818 if (N1C && N0.getOpcode() == ISD::SEXTLOAD && N0.Val->hasNUsesOfValue(1, 0)) {
819 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000820 // If we zero all the possible extended bits, then we can turn this into
821 // a zextload if we are running before legalize or the operation is legal.
822 if (MaskedValueIsZero(SDOperand(N,0), ~0ULL<<MVT::getSizeInBits(EVT),TLI) &&
823 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000824 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
825 N0.getOperand(1), N0.getOperand(2),
826 EVT);
Nate Begemanded49632005-10-13 03:11:28 +0000827 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +0000828 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +0000829 return SDOperand();
830 }
831 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000832 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000833}
834
Nate Begeman83e75ec2005-09-06 04:43:02 +0000835SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000836 SDOperand N0 = N->getOperand(0);
837 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000838 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
840 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000841 MVT::ValueType VT = N1.getValueType();
842 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000843
844 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000845 if (N0C && N1C)
846 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000847 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000848 // canonicalize constant to RHS
849 if (N0C && !N1C) {
850 std::swap(N0, N1);
851 std::swap(N0C, N1C);
852 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000854 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000855 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000856 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000857 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000858 return N1;
859 // fold (or x, c) -> c iff (x & ~c) == 0
860 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
861 TLI))
862 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000863 // fold (or (or x, c1), c2) -> (or x, c1|c2)
864 if (N1C && N0.getOpcode() == ISD::OR) {
865 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
866 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
867 if (N00C)
868 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
869 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
870 if (N01C)
871 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
872 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
873 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000874 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
875 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
876 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
877 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
878
879 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
880 MVT::isInteger(LL.getValueType())) {
881 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
882 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
883 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
884 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
885 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
886 WorkList.push_back(ORNode.Val);
887 return DAG.getSetCC(VT, ORNode, LR, Op1);
888 }
889 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
890 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
891 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
892 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
893 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
894 WorkList.push_back(ANDNode.Val);
895 return DAG.getSetCC(VT, ANDNode, LR, Op1);
896 }
897 }
898 // canonicalize equivalent to ll == rl
899 if (LL == RR && LR == RL) {
900 Op1 = ISD::getSetCCSwappedOperands(Op1);
901 std::swap(RL, RR);
902 }
903 if (LL == RL && LR == RR) {
904 bool isInteger = MVT::isInteger(LL.getValueType());
905 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
906 if (Result != ISD::SETCC_INVALID)
907 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
908 }
909 }
910 // fold (or (zext x), (zext y)) -> (zext (or x, y))
911 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
912 N1.getOpcode() == ISD::ZERO_EXTEND &&
913 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
914 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
915 N0.getOperand(0), N1.getOperand(0));
916 WorkList.push_back(ORNode.Val);
917 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
918 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000919 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000920}
921
Nate Begeman83e75ec2005-09-06 04:43:02 +0000922SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000923 SDOperand N0 = N->getOperand(0);
924 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000925 SDOperand LHS, RHS, CC;
926 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
927 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928 MVT::ValueType VT = N0.getValueType();
929
930 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000931 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000932 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000933 // canonicalize constant to RHS
934 if (N0C && !N1C) {
935 std::swap(N0, N1);
936 std::swap(N0C, N1C);
937 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000940 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000941 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000942 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
943 bool isInt = MVT::isInteger(LHS.getValueType());
944 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
945 isInt);
946 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000947 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000948 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000949 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000950 assert(0 && "Unhandled SetCC Equivalent!");
951 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000952 }
Nate Begeman99801192005-09-07 23:25:52 +0000953 // fold !(x or y) -> (!x and !y) iff x or y are setcc
954 if (N1C && N1C->getValue() == 1 &&
955 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000956 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000957 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
958 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000959 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
960 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000961 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
962 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963 }
964 }
Nate Begeman99801192005-09-07 23:25:52 +0000965 // fold !(x or y) -> (!x and !y) iff x or y are constants
966 if (N1C && N1C->isAllOnesValue() &&
967 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000968 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000969 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
970 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
972 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000973 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
974 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 }
976 }
Nate Begeman223df222005-09-08 20:18:10 +0000977 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
978 if (N1C && N0.getOpcode() == ISD::XOR) {
979 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
980 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
981 if (N00C)
982 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
983 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
984 if (N01C)
985 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
986 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
987 }
988 // fold (xor x, x) -> 0
989 if (N0 == N1)
990 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000991 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
992 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
993 N1.getOpcode() == ISD::ZERO_EXTEND &&
994 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
995 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
996 N0.getOperand(0), N1.getOperand(0));
997 WorkList.push_back(XORNode.Val);
998 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
999 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001000 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001001}
1002
Nate Begeman83e75ec2005-09-06 04:43:02 +00001003SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004 SDOperand N0 = N->getOperand(0);
1005 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001006 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1007 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001008 MVT::ValueType VT = N0.getValueType();
1009 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1010
1011 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001012 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001013 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001015 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001016 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001018 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001019 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001020 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001021 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001022 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001023 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001024 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1025 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001026 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001027 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001028 N0.getOperand(1).getOpcode() == ISD::Constant) {
1029 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001030 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001031 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001032 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001033 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001034 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001035 }
1036 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1037 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001038 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001039 N0.getOperand(1).getOpcode() == ISD::Constant) {
1040 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001041 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001042 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1043 DAG.getConstant(~0ULL << c1, VT));
1044 if (c2 > c1)
1045 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001046 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001047 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001048 return DAG.getNode(ISD::SRL, VT, Mask,
1049 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001050 }
1051 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001052 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001053 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001054 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1055 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001056}
1057
Nate Begeman83e75ec2005-09-06 04:43:02 +00001058SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001059 SDOperand N0 = N->getOperand(0);
1060 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001061 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1062 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001063 MVT::ValueType VT = N0.getValueType();
1064 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1065
1066 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001067 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001068 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001069 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001070 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001071 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001072 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001073 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001074 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001075 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001076 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001077 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001078 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001079 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001080 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001081 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman3df4d522005-10-12 20:40:40 +00001082 if (MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001083 return DAG.getNode(ISD::SRL, VT, N0, N1);
1084 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001085}
1086
Nate Begeman83e75ec2005-09-06 04:43:02 +00001087SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001088 SDOperand N0 = N->getOperand(0);
1089 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001090 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1091 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092 MVT::ValueType VT = N0.getValueType();
1093 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1094
1095 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001096 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001097 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001098 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001099 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001100 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001101 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001102 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001103 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001104 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001105 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001106 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001107 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001108 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1109 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001112 N0.getOperand(1).getOpcode() == ISD::Constant) {
1113 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001114 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001115 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001116 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001117 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001118 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001119 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001120 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001121}
1122
Nate Begeman83e75ec2005-09-06 04:43:02 +00001123SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001124 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001125 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001126
1127 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001128 if (N0C)
1129 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001130 N0.getValueType());
1131 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001132}
1133
Nate Begeman83e75ec2005-09-06 04:43:02 +00001134SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001137
1138 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001139 if (N0C)
1140 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001141 N0.getValueType());
1142 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001143}
1144
Nate Begeman83e75ec2005-09-06 04:43:02 +00001145SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001146 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001147 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001148
1149 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001150 if (N0C)
1151 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001152 N0.getValueType());
1153 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001154}
1155
Nate Begeman452d7be2005-09-16 00:54:12 +00001156SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1157 SDOperand N0 = N->getOperand(0);
1158 SDOperand N1 = N->getOperand(1);
1159 SDOperand N2 = N->getOperand(2);
1160 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1161 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1162 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1163 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001164
Nate Begeman452d7be2005-09-16 00:54:12 +00001165 // fold select C, X, X -> X
1166 if (N1 == N2)
1167 return N1;
1168 // fold select true, X, Y -> X
1169 if (N0C && !N0C->isNullValue())
1170 return N1;
1171 // fold select false, X, Y -> Y
1172 if (N0C && N0C->isNullValue())
1173 return N2;
1174 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001175 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001176 return DAG.getNode(ISD::OR, VT, N0, N2);
1177 // fold select C, 0, X -> ~C & X
1178 // FIXME: this should check for C type == X type, not i1?
1179 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1180 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1181 WorkList.push_back(XORNode.Val);
1182 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1183 }
1184 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001185 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001186 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1187 WorkList.push_back(XORNode.Val);
1188 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1189 }
1190 // fold select C, X, 0 -> C & X
1191 // FIXME: this should check for C type == X type, not i1?
1192 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1193 return DAG.getNode(ISD::AND, VT, N0, N1);
1194 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1195 if (MVT::i1 == VT && N0 == N1)
1196 return DAG.getNode(ISD::OR, VT, N0, N2);
1197 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1198 if (MVT::i1 == VT && N0 == N2)
1199 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman44728a72005-09-19 22:34:01 +00001200 // fold selects based on a setcc into other things, such as min/max/abs
1201 if (N0.getOpcode() == ISD::SETCC)
1202 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001203 return SDOperand();
1204}
1205
1206SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001207 SDOperand N0 = N->getOperand(0);
1208 SDOperand N1 = N->getOperand(1);
1209 SDOperand N2 = N->getOperand(2);
1210 SDOperand N3 = N->getOperand(3);
1211 SDOperand N4 = N->getOperand(4);
1212 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1213 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1214 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1215 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1216
1217 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001218 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001219 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1220
Nate Begeman44728a72005-09-19 22:34:01 +00001221 // fold select_cc lhs, rhs, x, x, cc -> x
1222 if (N2 == N3)
1223 return N2;
Nate Begeman44728a72005-09-19 22:34:01 +00001224 // fold select_cc into other things, such as min/max/abs
1225 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001226}
1227
1228SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1229 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1230 cast<CondCodeSDNode>(N->getOperand(2))->get());
1231}
1232
Nate Begeman83e75ec2005-09-06 04:43:02 +00001233SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001235 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001236 MVT::ValueType VT = N->getValueType(0);
1237
Nate Begeman1d4d4142005-09-01 00:19:25 +00001238 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001239 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001240 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001241 // fold (sext (sext x)) -> (sext x)
1242 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001243 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Nate Begeman765784a2005-10-12 23:18:53 +00001244 // fold (sext (sextload x)) -> (sextload x)
1245 if (N0.getOpcode() == ISD::SEXTLOAD && VT == N0.getValueType())
1246 return N0;
Nate Begeman3df4d522005-10-12 20:40:40 +00001247 // fold (sext (load x)) -> (sextload x)
1248 if (N0.getOpcode() == ISD::LOAD && N0.Val->hasNUsesOfValue(1, 0)) {
1249 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1250 N0.getOperand(1), N0.getOperand(2),
1251 N0.getValueType());
Nate Begeman765784a2005-10-12 23:18:53 +00001252 WorkList.push_back(N);
Chris Lattnerf9884052005-10-13 21:52:31 +00001253 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1254 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001255 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001256 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001257 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001258}
1259
Nate Begeman83e75ec2005-09-06 04:43:02 +00001260SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001261 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001262 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001263 MVT::ValueType VT = N->getValueType(0);
1264
Nate Begeman1d4d4142005-09-01 00:19:25 +00001265 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001266 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001267 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001268 // fold (zext (zext x)) -> (zext x)
1269 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001270 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1271 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001272}
1273
Nate Begeman83e75ec2005-09-06 04:43:02 +00001274SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001275 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001276 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001277 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001278 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001279 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001280 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001281
Nate Begeman1d4d4142005-09-01 00:19:25 +00001282 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001283 if (N0C) {
1284 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001285 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001286 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001287 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001288 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +00001289 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001290 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001291 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001292 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1293 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1294 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001295 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001296 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1298 if (N0.getOpcode() == ISD::AssertSext &&
1299 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001300 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001301 }
1302 // fold (sext_in_reg (sextload x)) -> (sextload x)
1303 if (N0.getOpcode() == ISD::SEXTLOAD &&
1304 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001305 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001306 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001307 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001308 if (N0.getOpcode() == ISD::SETCC &&
1309 TLI.getSetCCResultContents() ==
1310 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001311 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001312 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1313 if (MaskedValueIsZero(N0, 1ULL << (EVTBits-1), TLI))
1314 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
1315 DAG.getConstant(~0ULL >> (64-EVTBits), VT));
1316 // fold (sext_in_reg (srl x)) -> sra x
1317 if (N0.getOpcode() == ISD::SRL &&
1318 N0.getOperand(1).getOpcode() == ISD::Constant &&
1319 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1320 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1321 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001322 }
Nate Begemanded49632005-10-13 03:11:28 +00001323 // fold (sext_inreg (extload x)) -> (sextload x)
1324 if (N0.getOpcode() == ISD::EXTLOAD &&
1325 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001326 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001327 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1328 N0.getOperand(1), N0.getOperand(2),
1329 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001330 WorkList.push_back(N);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001331 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001332 return SDOperand();
1333 }
1334 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
1335 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.Val->hasNUsesOfValue(1, 0) &&
1336 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001337 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001338 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1339 N0.getOperand(1), N0.getOperand(2),
1340 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001341 WorkList.push_back(N);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001342 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001343 return SDOperand();
1344 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001345 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001346}
1347
Nate Begeman83e75ec2005-09-06 04:43:02 +00001348SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001350 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001351 MVT::ValueType VT = N->getValueType(0);
1352
1353 // noop truncate
1354 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001355 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001357 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001358 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001359 // fold (truncate (truncate x)) -> (truncate x)
1360 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001361 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001362 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1363 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1364 if (N0.getValueType() < VT)
1365 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001366 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 else if (N0.getValueType() > VT)
1368 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001369 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370 else
1371 // if the source and dest are the same type, we can drop both the extend
1372 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001373 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001375 // fold (truncate (load x)) -> (smaller load x)
1376 if (N0.getOpcode() == ISD::LOAD && N0.Val->hasNUsesOfValue(1, 0)) {
1377 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1378 "Cannot truncate to larger type!");
1379 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001380 // For big endian targets, we need to add an offset to the pointer to load
1381 // the correct bytes. For little endian systems, we merely need to read
1382 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001383 uint64_t PtrOff =
1384 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001385 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1386 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1387 DAG.getConstant(PtrOff, PtrType));
1388 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001389 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001390 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001391 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001392 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001393 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001394 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001395}
1396
Chris Lattner01b3d732005-09-28 22:28:18 +00001397SDOperand DAGCombiner::visitFADD(SDNode *N) {
1398 SDOperand N0 = N->getOperand(0);
1399 SDOperand N1 = N->getOperand(1);
1400 MVT::ValueType VT = N->getValueType(0);
1401
1402 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1403 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1404 // fold floating point (fadd c1, c2)
1405 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(),
1406 N->getValueType(0));
1407 }
1408 // fold (A + (-B)) -> A-B
1409 if (N1.getOpcode() == ISD::FNEG)
1410 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
1411
1412 // fold ((-A) + B) -> B-A
1413 if (N0.getOpcode() == ISD::FNEG)
1414 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
1415
1416 return SDOperand();
1417}
1418
1419SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1420 SDOperand N0 = N->getOperand(0);
1421 SDOperand N1 = N->getOperand(1);
1422 MVT::ValueType VT = N->getValueType(0);
1423
1424 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1425 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1426 // fold floating point (fsub c1, c2)
1427 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
1428 N->getValueType(0));
1429 }
1430 // fold (A-(-B)) -> A+B
1431 if (N1.getOpcode() == ISD::FNEG)
1432 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
1433
1434 return SDOperand();
1435}
1436
1437SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1438 SDOperand N0 = N->getOperand(0);
1439 SDOperand N1 = N->getOperand(1);
1440 MVT::ValueType VT = N->getValueType(0);
1441
1442 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1443 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1444 // fold floating point (fmul c1, c2)
1445 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
1446 N->getValueType(0));
1447 }
1448 return SDOperand();
1449}
1450
1451SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1452 SDOperand N0 = N->getOperand(0);
1453 SDOperand N1 = N->getOperand(1);
1454 MVT::ValueType VT = N->getValueType(0);
1455
1456 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1457 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1458 // fold floating point (fdiv c1, c2)
1459 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
1460 N->getValueType(0));
1461 }
1462 return SDOperand();
1463}
1464
1465SDOperand DAGCombiner::visitFREM(SDNode *N) {
1466 SDOperand N0 = N->getOperand(0);
1467 SDOperand N1 = N->getOperand(1);
1468 MVT::ValueType VT = N->getValueType(0);
1469
1470 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1471 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1472 // fold floating point (frem c1, c2) -> fmod(c1, c2)
1473 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
1474 N->getValueType(0));
1475 }
1476 return SDOperand();
1477}
1478
1479
Nate Begeman83e75ec2005-09-06 04:43:02 +00001480SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001481 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001482 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001483
1484 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001485 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001486 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1487 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001488}
1489
Nate Begeman83e75ec2005-09-06 04:43:02 +00001490SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001492 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001493
1494 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001495 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001496 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1497 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001498}
1499
Nate Begeman83e75ec2005-09-06 04:43:02 +00001500SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001501 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001502
1503 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001504 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001505 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1506 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001507}
1508
Nate Begeman83e75ec2005-09-06 04:43:02 +00001509SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001510 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001511
1512 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001513 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001514 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1515 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001516}
1517
Nate Begeman83e75ec2005-09-06 04:43:02 +00001518SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001519 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001520
1521 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001522 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001523 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1524 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001525}
1526
Nate Begeman83e75ec2005-09-06 04:43:02 +00001527SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001528 SDOperand N0 = N->getOperand(0);
1529 MVT::ValueType VT = N->getValueType(0);
1530 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001531 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001532
Nate Begeman1d4d4142005-09-01 00:19:25 +00001533 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001534 if (N0CFP) {
1535 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001536 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001537 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001538 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001539}
1540
Nate Begeman83e75ec2005-09-06 04:43:02 +00001541SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001542 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001543
1544 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001545 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001546 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1547 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548}
1549
Nate Begeman83e75ec2005-09-06 04:43:02 +00001550SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001551 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001552 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001553 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001554 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001555 // fold (neg (sub x, y)) -> (sub y, x)
1556 if (N->getOperand(0).getOpcode() == ISD::SUB)
1557 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001558 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559 // fold (neg (neg x)) -> x
1560 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001561 return N->getOperand(0).getOperand(0);
1562 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001563}
1564
Nate Begeman83e75ec2005-09-06 04:43:02 +00001565SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001566 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001568 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001569 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001570 // fold (fabs (fabs x)) -> (fabs x)
1571 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001572 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001573 // fold (fabs (fneg x)) -> (fabs x)
1574 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1575 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 N->getOperand(0).getOperand(0));
1577 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001578}
1579
Nate Begeman44728a72005-09-19 22:34:01 +00001580SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1581 SDOperand Chain = N->getOperand(0);
1582 SDOperand N1 = N->getOperand(1);
1583 SDOperand N2 = N->getOperand(2);
1584 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1585
1586 // never taken branch, fold to chain
1587 if (N1C && N1C->isNullValue())
1588 return Chain;
1589 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001590 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001591 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1592 return SDOperand();
1593}
1594
1595SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1596 SDOperand Chain = N->getOperand(0);
1597 SDOperand N1 = N->getOperand(1);
1598 SDOperand N2 = N->getOperand(2);
1599 SDOperand N3 = N->getOperand(3);
1600 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1601
1602 // unconditional branch to true mbb
1603 if (N1C && N1C->getValue() == 1)
1604 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1605 // unconditional branch to false mbb
1606 if (N1C && N1C->isNullValue())
1607 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1608 return SDOperand();
1609}
1610
Chris Lattner3ea0b472005-10-05 06:47:48 +00001611// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
1612//
Nate Begeman44728a72005-09-19 22:34:01 +00001613SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00001614 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
1615 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
1616
1617 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00001618 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
1619 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
1620
1621 // fold br_cc true, dest -> br dest (unconditional branch)
1622 if (SCCC && SCCC->getValue())
1623 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
1624 N->getOperand(4));
1625 // fold br_cc false, dest -> unconditional fall through
1626 if (SCCC && SCCC->isNullValue())
1627 return N->getOperand(0);
1628 // fold to a simpler setcc
1629 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
1630 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
1631 Simp.getOperand(2), Simp.getOperand(0),
1632 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00001633 return SDOperand();
1634}
1635
1636SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00001637 SDOperand Chain = N->getOperand(0);
1638 SDOperand CCN = N->getOperand(1);
1639 SDOperand LHS = N->getOperand(2);
1640 SDOperand RHS = N->getOperand(3);
1641 SDOperand N4 = N->getOperand(4);
1642 SDOperand N5 = N->getOperand(5);
1643
1644 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
1645 cast<CondCodeSDNode>(CCN)->get(), false);
1646 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1647
1648 // fold select_cc lhs, rhs, x, x, cc -> x
1649 if (N4 == N5)
1650 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1651 // fold select_cc true, x, y -> x
1652 if (SCCC && SCCC->getValue())
1653 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1654 // fold select_cc false, x, y -> y
1655 if (SCCC && SCCC->isNullValue())
1656 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
1657 // fold to a simpler setcc
1658 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1659 return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0),
1660 SCC.getOperand(1), N4, N5);
Nate Begeman44728a72005-09-19 22:34:01 +00001661 return SDOperand();
1662}
1663
Chris Lattner01a22022005-10-10 22:04:48 +00001664SDOperand DAGCombiner::visitLOAD(SDNode *N) {
1665 SDOperand Chain = N->getOperand(0);
1666 SDOperand Ptr = N->getOperand(1);
1667 SDOperand SrcValue = N->getOperand(2);
1668
1669 // If this load is directly stored, replace the load value with the stored
1670 // value.
1671 // TODO: Handle store large -> read small portion.
1672 // TODO: Handle TRUNCSTORE/EXTLOAD
1673 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
1674 Chain.getOperand(1).getValueType() == N->getValueType(0))
1675 return CombineTo(N, Chain.getOperand(1), Chain);
1676
1677 return SDOperand();
1678}
1679
Chris Lattner87514ca2005-10-10 22:31:19 +00001680SDOperand DAGCombiner::visitSTORE(SDNode *N) {
1681 SDOperand Chain = N->getOperand(0);
1682 SDOperand Value = N->getOperand(1);
1683 SDOperand Ptr = N->getOperand(2);
1684 SDOperand SrcValue = N->getOperand(3);
1685
1686 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001687 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
1688 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */) {
Chris Lattner87514ca2005-10-10 22:31:19 +00001689 // Create a new store of Value that replaces both stores.
1690 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001691 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
1692 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00001693 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
1694 PrevStore->getOperand(0), Value, Ptr,
1695 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001696 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00001697 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001698 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00001699 }
1700
1701 return SDOperand();
1702}
1703
Nate Begeman44728a72005-09-19 22:34:01 +00001704SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00001705 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
1706
1707 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
1708 cast<CondCodeSDNode>(N0.getOperand(2))->get());
1709 // If we got a simplified select_cc node back from SimplifySelectCC, then
1710 // break it down into a new SETCC node, and a new SELECT node, and then return
1711 // the SELECT node, since we were called with a SELECT node.
1712 if (SCC.Val) {
1713 // Check to see if we got a select_cc back (to turn into setcc/select).
1714 // Otherwise, just return whatever node we got back, like fabs.
1715 if (SCC.getOpcode() == ISD::SELECT_CC) {
1716 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
1717 SCC.getOperand(0), SCC.getOperand(1),
1718 SCC.getOperand(4));
1719 WorkList.push_back(SETCC.Val);
1720 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
1721 SCC.getOperand(3), SETCC);
1722 }
1723 return SCC;
1724 }
Nate Begeman44728a72005-09-19 22:34:01 +00001725 return SDOperand();
1726}
1727
1728SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1729 SDOperand N2, SDOperand N3,
1730 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00001731
1732 MVT::ValueType VT = N2.getValueType();
1733 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1734 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1735 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1736 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1737
1738 // Determine if the condition we're dealing with is constant
1739 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1740 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1741
1742 // fold select_cc true, x, y -> x
1743 if (SCCC && SCCC->getValue())
1744 return N2;
1745 // fold select_cc false, x, y -> y
1746 if (SCCC && SCCC->getValue() == 0)
1747 return N3;
1748
1749 // Check to see if we can simplify the select into an fabs node
1750 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1751 // Allow either -0.0 or 0.0
1752 if (CFP->getValue() == 0.0) {
1753 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1754 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
1755 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
1756 N2 == N3.getOperand(0))
1757 return DAG.getNode(ISD::FABS, VT, N0);
1758
1759 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1760 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
1761 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
1762 N2.getOperand(0) == N3)
1763 return DAG.getNode(ISD::FABS, VT, N3);
1764 }
1765 }
1766
1767 // Check to see if we can perform the "gzip trick", transforming
1768 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
1769 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
1770 MVT::isInteger(N0.getValueType()) &&
1771 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
1772 MVT::ValueType XType = N0.getValueType();
1773 MVT::ValueType AType = N2.getValueType();
1774 if (XType >= AType) {
1775 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00001776 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00001777 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
1778 unsigned ShCtV = Log2_64(N2C->getValue());
1779 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1780 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
1781 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
1782 WorkList.push_back(Shift.Val);
1783 if (XType > AType) {
1784 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1785 WorkList.push_back(Shift.Val);
1786 }
1787 return DAG.getNode(ISD::AND, AType, Shift, N2);
1788 }
1789 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1790 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1791 TLI.getShiftAmountTy()));
1792 WorkList.push_back(Shift.Val);
1793 if (XType > AType) {
1794 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1795 WorkList.push_back(Shift.Val);
1796 }
1797 return DAG.getNode(ISD::AND, AType, Shift, N2);
1798 }
1799 }
Nate Begeman07ed4172005-10-10 21:26:48 +00001800
1801 // fold select C, 16, 0 -> shl C, 4
1802 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
1803 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
1804 // Get a SetCC of the condition
1805 // FIXME: Should probably make sure that setcc is legal if we ever have a
1806 // target where it isn't.
1807 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1808 WorkList.push_back(SCC.Val);
1809 // cast from setcc result type to select result type
1810 if (AfterLegalize)
1811 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
1812 else
1813 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
1814 WorkList.push_back(Temp.Val);
1815 // shl setcc result by log2 n2c
1816 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
1817 DAG.getConstant(Log2_64(N2C->getValue()),
1818 TLI.getShiftAmountTy()));
1819 }
1820
Nate Begemanf845b452005-10-08 00:29:44 +00001821 // Check to see if this is the equivalent of setcc
1822 // FIXME: Turn all of these into setcc if setcc if setcc is legal
1823 // otherwise, go ahead with the folds.
1824 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
1825 MVT::ValueType XType = N0.getValueType();
1826 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
1827 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1828 if (Res.getValueType() != VT)
1829 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
1830 return Res;
1831 }
1832
1833 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
1834 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
1835 TLI.isOperationLegal(ISD::CTLZ, XType)) {
1836 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
1837 return DAG.getNode(ISD::SRL, XType, Ctlz,
1838 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
1839 TLI.getShiftAmountTy()));
1840 }
1841 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
1842 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
1843 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
1844 N0);
1845 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
1846 DAG.getConstant(~0ULL, XType));
1847 return DAG.getNode(ISD::SRL, XType,
1848 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
1849 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1850 TLI.getShiftAmountTy()));
1851 }
1852 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
1853 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
1854 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
1855 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1856 TLI.getShiftAmountTy()));
1857 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
1858 }
1859 }
1860
1861 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
1862 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
1863 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
1864 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
1865 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
1866 MVT::ValueType XType = N0.getValueType();
1867 if (SubC->isNullValue() && MVT::isInteger(XType)) {
1868 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1869 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1870 TLI.getShiftAmountTy()));
1871 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
1872 WorkList.push_back(Shift.Val);
1873 WorkList.push_back(Add.Val);
1874 return DAG.getNode(ISD::XOR, XType, Add, Shift);
1875 }
1876 }
1877 }
1878
Nate Begeman44728a72005-09-19 22:34:01 +00001879 return SDOperand();
1880}
1881
Nate Begeman452d7be2005-09-16 00:54:12 +00001882SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00001883 SDOperand N1, ISD::CondCode Cond,
1884 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001885 // These setcc operations always fold.
1886 switch (Cond) {
1887 default: break;
1888 case ISD::SETFALSE:
1889 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1890 case ISD::SETTRUE:
1891 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1892 }
1893
1894 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1895 uint64_t C1 = N1C->getValue();
1896 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1897 uint64_t C0 = N0C->getValue();
1898
1899 // Sign extend the operands if required
1900 if (ISD::isSignedIntSetCC(Cond)) {
1901 C0 = N0C->getSignExtended();
1902 C1 = N1C->getSignExtended();
1903 }
1904
1905 switch (Cond) {
1906 default: assert(0 && "Unknown integer setcc!");
1907 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1908 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1909 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1910 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1911 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1912 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1913 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1914 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1915 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1916 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1917 }
1918 } else {
1919 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1920 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1921 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1922
1923 // If the comparison constant has bits in the upper part, the
1924 // zero-extended value could never match.
1925 if (C1 & (~0ULL << InSize)) {
1926 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1927 switch (Cond) {
1928 case ISD::SETUGT:
1929 case ISD::SETUGE:
1930 case ISD::SETEQ: return DAG.getConstant(0, VT);
1931 case ISD::SETULT:
1932 case ISD::SETULE:
1933 case ISD::SETNE: return DAG.getConstant(1, VT);
1934 case ISD::SETGT:
1935 case ISD::SETGE:
1936 // True if the sign bit of C1 is set.
1937 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
1938 case ISD::SETLT:
1939 case ISD::SETLE:
1940 // True if the sign bit of C1 isn't set.
1941 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
1942 default:
1943 break;
1944 }
1945 }
1946
1947 // Otherwise, we can perform the comparison with the low bits.
1948 switch (Cond) {
1949 case ISD::SETEQ:
1950 case ISD::SETNE:
1951 case ISD::SETUGT:
1952 case ISD::SETUGE:
1953 case ISD::SETULT:
1954 case ISD::SETULE:
1955 return DAG.getSetCC(VT, N0.getOperand(0),
1956 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
1957 Cond);
1958 default:
1959 break; // todo, be more careful with signed comparisons
1960 }
1961 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1962 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1963 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1964 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1965 MVT::ValueType ExtDstTy = N0.getValueType();
1966 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1967
1968 // If the extended part has any inconsistent bits, it cannot ever
1969 // compare equal. In other words, they have to be all ones or all
1970 // zeros.
1971 uint64_t ExtBits =
1972 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1973 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1974 return DAG.getConstant(Cond == ISD::SETNE, VT);
1975
1976 SDOperand ZextOp;
1977 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1978 if (Op0Ty == ExtSrcTy) {
1979 ZextOp = N0.getOperand(0);
1980 } else {
1981 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1982 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1983 DAG.getConstant(Imm, Op0Ty));
1984 }
1985 WorkList.push_back(ZextOp.Val);
1986 // Otherwise, make this a use of a zext.
1987 return DAG.getSetCC(VT, ZextOp,
1988 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1989 ExtDstTy),
1990 Cond);
1991 }
Chris Lattner5c46f742005-10-05 06:11:08 +00001992
Nate Begeman452d7be2005-09-16 00:54:12 +00001993 uint64_t MinVal, MaxVal;
1994 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1995 if (ISD::isSignedIntSetCC(Cond)) {
1996 MinVal = 1ULL << (OperandBitSize-1);
1997 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1998 MaxVal = ~0ULL >> (65-OperandBitSize);
1999 else
2000 MaxVal = 0;
2001 } else {
2002 MinVal = 0;
2003 MaxVal = ~0ULL >> (64-OperandBitSize);
2004 }
2005
2006 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2007 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2008 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2009 --C1; // X >= C0 --> X > (C0-1)
2010 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2011 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2012 }
2013
2014 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2015 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2016 ++C1; // X <= C0 --> X < (C0+1)
2017 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2018 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2019 }
2020
2021 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2022 return DAG.getConstant(0, VT); // X < MIN --> false
2023
2024 // Canonicalize setgt X, Min --> setne X, Min
2025 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2026 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
2027
2028 // If we have setult X, 1, turn it into seteq X, 0
2029 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2030 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2031 ISD::SETEQ);
2032 // If we have setugt X, Max-1, turn it into seteq X, Max
2033 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2034 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2035 ISD::SETEQ);
2036
2037 // If we have "setcc X, C0", check to see if we can shrink the immediate
2038 // by changing cc.
2039
2040 // SETUGT X, SINTMAX -> SETLT X, 0
2041 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2042 C1 == (~0ULL >> (65-OperandBitSize)))
2043 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2044 ISD::SETLT);
2045
2046 // FIXME: Implement the rest of these.
2047
2048 // Fold bit comparisons when we can.
2049 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2050 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2051 if (ConstantSDNode *AndRHS =
2052 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2053 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2054 // Perform the xform if the AND RHS is a single bit.
2055 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2056 return DAG.getNode(ISD::SRL, VT, N0,
2057 DAG.getConstant(Log2_64(AndRHS->getValue()),
2058 TLI.getShiftAmountTy()));
2059 }
2060 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2061 // (X & 8) == 8 --> (X & 8) >> 3
2062 // Perform the xform if C1 is a single bit.
2063 if ((C1 & (C1-1)) == 0) {
2064 return DAG.getNode(ISD::SRL, VT, N0,
2065 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2066 }
2067 }
2068 }
2069 }
2070 } else if (isa<ConstantSDNode>(N0.Val)) {
2071 // Ensure that the constant occurs on the RHS.
2072 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2073 }
2074
2075 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2076 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2077 double C0 = N0C->getValue(), C1 = N1C->getValue();
2078
2079 switch (Cond) {
2080 default: break; // FIXME: Implement the rest of these!
2081 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2082 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2083 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2084 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2085 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2086 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2087 }
2088 } else {
2089 // Ensure that the constant occurs on the RHS.
2090 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2091 }
2092
2093 if (N0 == N1) {
2094 // We can always fold X == Y for integer setcc's.
2095 if (MVT::isInteger(N0.getValueType()))
2096 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2097 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2098 if (UOF == 2) // FP operators that are undefined on NaNs.
2099 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2100 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2101 return DAG.getConstant(UOF, VT);
2102 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2103 // if it is not already.
2104 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
2105 if (NewCond != Cond)
2106 return DAG.getSetCC(VT, N0, N1, NewCond);
2107 }
2108
2109 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2110 MVT::isInteger(N0.getValueType())) {
2111 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2112 N0.getOpcode() == ISD::XOR) {
2113 // Simplify (X+Y) == (X+Z) --> Y == Z
2114 if (N0.getOpcode() == N1.getOpcode()) {
2115 if (N0.getOperand(0) == N1.getOperand(0))
2116 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2117 if (N0.getOperand(1) == N1.getOperand(1))
2118 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2119 if (isCommutativeBinOp(N0.getOpcode())) {
2120 // If X op Y == Y op X, try other combinations.
2121 if (N0.getOperand(0) == N1.getOperand(1))
2122 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2123 if (N0.getOperand(1) == N1.getOperand(0))
2124 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2125 }
2126 }
2127
Chris Lattner5c46f742005-10-05 06:11:08 +00002128 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
2129 if (N0.getOpcode() == ISD::XOR)
2130 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2131 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2132 // If we know that all of the inverted bits are zero, don't bother
2133 // performing the inversion.
2134 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
2135 return DAG.getSetCC(VT, N0.getOperand(0),
2136 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
2137 N0.getValueType()), Cond);
2138 }
2139
Nate Begeman452d7be2005-09-16 00:54:12 +00002140 // Simplify (X+Z) == X --> Z == 0
2141 if (N0.getOperand(0) == N1)
2142 return DAG.getSetCC(VT, N0.getOperand(1),
2143 DAG.getConstant(0, N0.getValueType()), Cond);
2144 if (N0.getOperand(1) == N1) {
2145 if (isCommutativeBinOp(N0.getOpcode()))
2146 return DAG.getSetCC(VT, N0.getOperand(0),
2147 DAG.getConstant(0, N0.getValueType()), Cond);
2148 else {
2149 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2150 // (Z-X) == X --> Z == X<<1
2151 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2152 N1,
2153 DAG.getConstant(1,TLI.getShiftAmountTy()));
2154 WorkList.push_back(SH.Val);
2155 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2156 }
2157 }
2158 }
2159
2160 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2161 N1.getOpcode() == ISD::XOR) {
2162 // Simplify X == (X+Z) --> Z == 0
2163 if (N1.getOperand(0) == N0) {
2164 return DAG.getSetCC(VT, N1.getOperand(1),
2165 DAG.getConstant(0, N1.getValueType()), Cond);
2166 } else if (N1.getOperand(1) == N0) {
2167 if (isCommutativeBinOp(N1.getOpcode())) {
2168 return DAG.getSetCC(VT, N1.getOperand(0),
2169 DAG.getConstant(0, N1.getValueType()), Cond);
2170 } else {
2171 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2172 // X == (Z-X) --> X<<1 == Z
2173 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2174 DAG.getConstant(1,TLI.getShiftAmountTy()));
2175 WorkList.push_back(SH.Val);
2176 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2177 }
2178 }
2179 }
2180 }
2181
2182 // Fold away ALL boolean setcc's.
2183 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002184 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002185 switch (Cond) {
2186 default: assert(0 && "Unknown integer setcc!");
2187 case ISD::SETEQ: // X == Y -> (X^Y)^1
2188 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2189 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2190 WorkList.push_back(Temp.Val);
2191 break;
2192 case ISD::SETNE: // X != Y --> (X^Y)
2193 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2194 break;
2195 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2196 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2197 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2198 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2199 WorkList.push_back(Temp.Val);
2200 break;
2201 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2202 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2203 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2204 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2205 WorkList.push_back(Temp.Val);
2206 break;
2207 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2208 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2209 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2210 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2211 WorkList.push_back(Temp.Val);
2212 break;
2213 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2214 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2215 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2216 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2217 break;
2218 }
2219 if (VT != MVT::i1) {
2220 WorkList.push_back(N0.Val);
2221 // FIXME: If running after legalize, we probably can't do this.
2222 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2223 }
2224 return N0;
2225 }
2226
2227 // Could not fold it.
2228 return SDOperand();
2229}
2230
Nate Begeman1d4d4142005-09-01 00:19:25 +00002231// SelectionDAG::Combine - This is the entry point for the file.
2232//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002233void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002234 /// run - This is the main entry point to this class.
2235 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002236 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002237}