blob: 7eeb100359928e111ba2a765c4a3ab6890ce3ed7 [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//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000025// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman646d7e22005-09-02 21:18:40 +000026// FIXME: divide by zero is currently left unfolded. do we want to turn this
27// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000028// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000029//
30//===----------------------------------------------------------------------===//
31
32#define DEBUG_TYPE "dagcombine"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000038#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000039#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000040#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041using namespace llvm;
42
43namespace {
44 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
45
46 class DAGCombiner {
47 SelectionDAG &DAG;
48 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000049 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000050
51 // Worklist of all of the nodes that need to be simplified.
52 std::vector<SDNode*> WorkList;
53
54 /// AddUsersToWorkList - When an instruction is simplified, add all users of
55 /// the instruction to the work lists because they might get more simplified
56 /// now.
57 ///
58 void AddUsersToWorkList(SDNode *N) {
59 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000060 UI != UE; ++UI)
61 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000062 }
63
64 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000065 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000066 void removeFromWorkList(SDNode *N) {
67 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
68 WorkList.end());
69 }
70
Chris Lattner24664722006-03-01 04:53:38 +000071 public:
Chris Lattner5750df92006-03-01 04:03:14 +000072 void AddToWorkList(SDNode *N) {
73 WorkList.push_back(N);
74 }
75
Chris Lattner01a22022005-10-10 22:04:48 +000076 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000077 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000078 DEBUG(std::cerr << "\nReplacing "; N->dump();
79 std::cerr << "\nWith: "; To[0].Val->dump();
80 std::cerr << " and " << To.size()-1 << " other values\n");
81 std::vector<SDNode*> NowDead;
82 DAG.ReplaceAllUsesWith(N, To, &NowDead);
83
84 // Push the new nodes and any users onto the worklist
85 for (unsigned i = 0, e = To.size(); i != e; ++i) {
86 WorkList.push_back(To[i].Val);
87 AddUsersToWorkList(To[i].Val);
88 }
89
90 // Nodes can end up on the worklist more than once. Make sure we do
91 // not process a node that has been replaced.
92 removeFromWorkList(N);
93 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
94 removeFromWorkList(NowDead[i]);
95
96 // Finally, since the node is now dead, remove it from the graph.
97 DAG.DeleteNode(N);
98 return SDOperand(N, 0);
99 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000100
Chris Lattner24664722006-03-01 04:53:38 +0000101 SDOperand CombineTo(SDNode *N, SDOperand Res) {
102 std::vector<SDOperand> To;
103 To.push_back(Res);
104 return CombineTo(N, To);
105 }
106
107 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
108 std::vector<SDOperand> To;
109 To.push_back(Res0);
110 To.push_back(Res1);
111 return CombineTo(N, To);
112 }
113 private:
114
Chris Lattner012f2412006-02-17 21:58:01 +0000115 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000116 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000117 /// propagation. If so, return true.
118 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000119 TargetLowering::TargetLoweringOpt TLO(DAG);
120 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000121 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
122 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
123 return false;
124
125 // Revisit the node.
126 WorkList.push_back(Op.Val);
127
128 // Replace the old value with the new one.
129 ++NodesCombined;
130 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
131 std::cerr << "\nWith: "; TLO.New.Val->dump());
132
133 std::vector<SDNode*> NowDead;
134 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
135
Chris Lattner7d20d392006-02-20 06:51:04 +0000136 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000137 WorkList.push_back(TLO.New.Val);
138 AddUsersToWorkList(TLO.New.Val);
139
140 // Nodes can end up on the worklist more than once. Make sure we do
141 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000142 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
143 removeFromWorkList(NowDead[i]);
144
Chris Lattner7d20d392006-02-20 06:51:04 +0000145 // Finally, if the node is now dead, remove it from the graph. The node
146 // may not be dead if the replacement process recursively simplified to
147 // something else needing this node.
148 if (TLO.Old.Val->use_empty()) {
149 removeFromWorkList(TLO.Old.Val);
150 DAG.DeleteNode(TLO.Old.Val);
151 }
Chris Lattner012f2412006-02-17 21:58:01 +0000152 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000153 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000154
Nate Begeman1d4d4142005-09-01 00:19:25 +0000155 /// visit - call the node-specific routine that knows how to fold each
156 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000157 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000158
159 // Visitation implementation - Implement dag node combining for different
160 // node types. The semantics are as follows:
161 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000162 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000163 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000164 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000166 SDOperand visitTokenFactor(SDNode *N);
167 SDOperand visitADD(SDNode *N);
168 SDOperand visitSUB(SDNode *N);
169 SDOperand visitMUL(SDNode *N);
170 SDOperand visitSDIV(SDNode *N);
171 SDOperand visitUDIV(SDNode *N);
172 SDOperand visitSREM(SDNode *N);
173 SDOperand visitUREM(SDNode *N);
174 SDOperand visitMULHU(SDNode *N);
175 SDOperand visitMULHS(SDNode *N);
176 SDOperand visitAND(SDNode *N);
177 SDOperand visitOR(SDNode *N);
178 SDOperand visitXOR(SDNode *N);
179 SDOperand visitSHL(SDNode *N);
180 SDOperand visitSRA(SDNode *N);
181 SDOperand visitSRL(SDNode *N);
182 SDOperand visitCTLZ(SDNode *N);
183 SDOperand visitCTTZ(SDNode *N);
184 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000185 SDOperand visitSELECT(SDNode *N);
186 SDOperand visitSELECT_CC(SDNode *N);
187 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000188 SDOperand visitSIGN_EXTEND(SDNode *N);
189 SDOperand visitZERO_EXTEND(SDNode *N);
190 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
191 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000192 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000193 SDOperand visitFADD(SDNode *N);
194 SDOperand visitFSUB(SDNode *N);
195 SDOperand visitFMUL(SDNode *N);
196 SDOperand visitFDIV(SDNode *N);
197 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000198 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000199 SDOperand visitSINT_TO_FP(SDNode *N);
200 SDOperand visitUINT_TO_FP(SDNode *N);
201 SDOperand visitFP_TO_SINT(SDNode *N);
202 SDOperand visitFP_TO_UINT(SDNode *N);
203 SDOperand visitFP_ROUND(SDNode *N);
204 SDOperand visitFP_ROUND_INREG(SDNode *N);
205 SDOperand visitFP_EXTEND(SDNode *N);
206 SDOperand visitFNEG(SDNode *N);
207 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000208 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000209 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000210 SDOperand visitLOAD(SDNode *N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000211 SDOperand visitXEXTLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000212 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000213 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
214 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000215 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000216 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000217
Nate Begemancd4d58c2006-02-03 06:46:56 +0000218 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
219
Chris Lattner40c62d52005-10-18 06:04:22 +0000220 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000221 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
222 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
223 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000224 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000225 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000226
227 SDOperand BuildSDIV(SDNode *N);
228 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000229public:
230 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000231 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000232
233 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000234 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000235 };
236}
237
Chris Lattner24664722006-03-01 04:53:38 +0000238//===----------------------------------------------------------------------===//
239// TargetLowering::DAGCombinerInfo implementation
240//===----------------------------------------------------------------------===//
241
242void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
243 ((DAGCombiner*)DC)->AddToWorkList(N);
244}
245
246SDOperand TargetLowering::DAGCombinerInfo::
247CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
248 return ((DAGCombiner*)DC)->CombineTo(N, To);
249}
250
251SDOperand TargetLowering::DAGCombinerInfo::
252CombineTo(SDNode *N, SDOperand Res) {
253 return ((DAGCombiner*)DC)->CombineTo(N, Res);
254}
255
256
257SDOperand TargetLowering::DAGCombinerInfo::
258CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
259 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
260}
261
262
263
264
265//===----------------------------------------------------------------------===//
266
267
Nate Begeman69575232005-10-20 02:15:44 +0000268struct ms {
269 int64_t m; // magic number
270 int64_t s; // shift amount
271};
272
273struct mu {
274 uint64_t m; // magic number
275 int64_t a; // add indicator
276 int64_t s; // shift amount
277};
278
279/// magic - calculate the magic numbers required to codegen an integer sdiv as
280/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
281/// or -1.
282static ms magic32(int32_t d) {
283 int32_t p;
284 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
285 const uint32_t two31 = 0x80000000U;
286 struct ms mag;
287
288 ad = abs(d);
289 t = two31 + ((uint32_t)d >> 31);
290 anc = t - 1 - t%ad; // absolute value of nc
291 p = 31; // initialize p
292 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
293 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
294 q2 = two31/ad; // initialize q2 = 2p/abs(d)
295 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
296 do {
297 p = p + 1;
298 q1 = 2*q1; // update q1 = 2p/abs(nc)
299 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
300 if (r1 >= anc) { // must be unsigned comparison
301 q1 = q1 + 1;
302 r1 = r1 - anc;
303 }
304 q2 = 2*q2; // update q2 = 2p/abs(d)
305 r2 = 2*r2; // update r2 = rem(2p/abs(d))
306 if (r2 >= ad) { // must be unsigned comparison
307 q2 = q2 + 1;
308 r2 = r2 - ad;
309 }
310 delta = ad - r2;
311 } while (q1 < delta || (q1 == delta && r1 == 0));
312
313 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
314 if (d < 0) mag.m = -mag.m; // resulting magic number
315 mag.s = p - 32; // resulting shift
316 return mag;
317}
318
319/// magicu - calculate the magic numbers required to codegen an integer udiv as
320/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
321static mu magicu32(uint32_t d) {
322 int32_t p;
323 uint32_t nc, delta, q1, r1, q2, r2;
324 struct mu magu;
325 magu.a = 0; // initialize "add" indicator
326 nc = - 1 - (-d)%d;
327 p = 31; // initialize p
328 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
329 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
330 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
331 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
332 do {
333 p = p + 1;
334 if (r1 >= nc - r1 ) {
335 q1 = 2*q1 + 1; // update q1
336 r1 = 2*r1 - nc; // update r1
337 }
338 else {
339 q1 = 2*q1; // update q1
340 r1 = 2*r1; // update r1
341 }
342 if (r2 + 1 >= d - r2) {
343 if (q2 >= 0x7FFFFFFF) magu.a = 1;
344 q2 = 2*q2 + 1; // update q2
345 r2 = 2*r2 + 1 - d; // update r2
346 }
347 else {
348 if (q2 >= 0x80000000) magu.a = 1;
349 q2 = 2*q2; // update q2
350 r2 = 2*r2 + 1; // update r2
351 }
352 delta = d - 1 - r2;
353 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
354 magu.m = q2 + 1; // resulting magic number
355 magu.s = p - 32; // resulting shift
356 return magu;
357}
358
359/// magic - calculate the magic numbers required to codegen an integer sdiv as
360/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
361/// or -1.
362static ms magic64(int64_t d) {
363 int64_t p;
364 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
365 const uint64_t two63 = 9223372036854775808ULL; // 2^63
366 struct ms mag;
367
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000368 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000369 t = two63 + ((uint64_t)d >> 63);
370 anc = t - 1 - t%ad; // absolute value of nc
371 p = 63; // initialize p
372 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
373 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
374 q2 = two63/ad; // initialize q2 = 2p/abs(d)
375 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
376 do {
377 p = p + 1;
378 q1 = 2*q1; // update q1 = 2p/abs(nc)
379 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
380 if (r1 >= anc) { // must be unsigned comparison
381 q1 = q1 + 1;
382 r1 = r1 - anc;
383 }
384 q2 = 2*q2; // update q2 = 2p/abs(d)
385 r2 = 2*r2; // update r2 = rem(2p/abs(d))
386 if (r2 >= ad) { // must be unsigned comparison
387 q2 = q2 + 1;
388 r2 = r2 - ad;
389 }
390 delta = ad - r2;
391 } while (q1 < delta || (q1 == delta && r1 == 0));
392
393 mag.m = q2 + 1;
394 if (d < 0) mag.m = -mag.m; // resulting magic number
395 mag.s = p - 64; // resulting shift
396 return mag;
397}
398
399/// magicu - calculate the magic numbers required to codegen an integer udiv as
400/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
401static mu magicu64(uint64_t d)
402{
403 int64_t p;
404 uint64_t nc, delta, q1, r1, q2, r2;
405 struct mu magu;
406 magu.a = 0; // initialize "add" indicator
407 nc = - 1 - (-d)%d;
408 p = 63; // initialize p
409 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
410 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
411 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
412 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
413 do {
414 p = p + 1;
415 if (r1 >= nc - r1 ) {
416 q1 = 2*q1 + 1; // update q1
417 r1 = 2*r1 - nc; // update r1
418 }
419 else {
420 q1 = 2*q1; // update q1
421 r1 = 2*r1; // update r1
422 }
423 if (r2 + 1 >= d - r2) {
424 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
425 q2 = 2*q2 + 1; // update q2
426 r2 = 2*r2 + 1 - d; // update r2
427 }
428 else {
429 if (q2 >= 0x8000000000000000ull) magu.a = 1;
430 q2 = 2*q2; // update q2
431 r2 = 2*r2 + 1; // update r2
432 }
433 delta = d - 1 - r2;
434 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
435 magu.m = q2 + 1; // resulting magic number
436 magu.s = p - 64; // resulting shift
437 return magu;
438}
439
Nate Begeman4ebd8052005-09-01 23:24:04 +0000440// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
441// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000442// Also, set the incoming LHS, RHS, and CC references to the appropriate
443// nodes based on the type of node we are checking. This simplifies life a
444// bit for the callers.
445static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
446 SDOperand &CC) {
447 if (N.getOpcode() == ISD::SETCC) {
448 LHS = N.getOperand(0);
449 RHS = N.getOperand(1);
450 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000451 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000452 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 if (N.getOpcode() == ISD::SELECT_CC &&
454 N.getOperand(2).getOpcode() == ISD::Constant &&
455 N.getOperand(3).getOpcode() == ISD::Constant &&
456 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000457 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
458 LHS = N.getOperand(0);
459 RHS = N.getOperand(1);
460 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000463 return false;
464}
465
Nate Begeman99801192005-09-07 23:25:52 +0000466// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
467// one use. If this is true, it allows the users to invert the operation for
468// free when it is profitable to do so.
469static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000470 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000471 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000472 return true;
473 return false;
474}
475
Nate Begeman452d7be2005-09-16 00:54:12 +0000476// FIXME: This should probably go in the ISD class rather than being duplicated
477// in several files.
478static bool isCommutativeBinOp(unsigned Opcode) {
479 switch (Opcode) {
480 case ISD::ADD:
481 case ISD::MUL:
482 case ISD::AND:
483 case ISD::OR:
484 case ISD::XOR: return true;
485 default: return false; // FIXME: Need commutative info for user ops!
486 }
487}
488
Nate Begemancd4d58c2006-02-03 06:46:56 +0000489SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
490 MVT::ValueType VT = N0.getValueType();
491 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
492 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
493 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
494 if (isa<ConstantSDNode>(N1)) {
495 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000496 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000497 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
498 } else if (N0.hasOneUse()) {
499 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000500 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
502 }
503 }
504 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
505 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
506 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
507 if (isa<ConstantSDNode>(N0)) {
508 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000509 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000510 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
511 } else if (N1.hasOneUse()) {
512 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000513 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000514 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
515 }
516 }
517 return SDOperand();
518}
519
Nate Begeman4ebd8052005-09-01 23:24:04 +0000520void DAGCombiner::Run(bool RunningAfterLegalize) {
521 // set the instance variable, so that the various visit routines may use it.
522 AfterLegalize = RunningAfterLegalize;
523
Nate Begeman646d7e22005-09-02 21:18:40 +0000524 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000525 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
526 E = DAG.allnodes_end(); I != E; ++I)
527 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000528
Chris Lattner95038592005-10-05 06:35:28 +0000529 // Create a dummy node (which is not added to allnodes), that adds a reference
530 // to the root node, preventing it from being deleted, and tracking any
531 // changes of the root.
532 HandleSDNode Dummy(DAG.getRoot());
533
Chris Lattner24664722006-03-01 04:53:38 +0000534
535 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
536 TargetLowering::DAGCombinerInfo
537 DagCombineInfo(DAG, !RunningAfterLegalize, this);
538
Nate Begeman1d4d4142005-09-01 00:19:25 +0000539 // while the worklist isn't empty, inspect the node on the end of it and
540 // try and combine it.
541 while (!WorkList.empty()) {
542 SDNode *N = WorkList.back();
543 WorkList.pop_back();
544
545 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000546 // N is deleted from the DAG, since they too may now be dead or may have a
547 // reduced number of uses, allowing other xforms.
548 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000549 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
550 WorkList.push_back(N->getOperand(i).Val);
551
Nate Begeman1d4d4142005-09-01 00:19:25 +0000552 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000553 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 continue;
555 }
556
Nate Begeman83e75ec2005-09-06 04:43:02 +0000557 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000558
559 // If nothing happened, try a target-specific DAG combine.
560 if (RV.Val == 0) {
561 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
562 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
563 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
564 }
565
Nate Begeman83e75ec2005-09-06 04:43:02 +0000566 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000567 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000568 // If we get back the same node we passed in, rather than a new node or
569 // zero, we know that the node must have defined multiple values and
570 // CombineTo was used. Since CombineTo takes care of the worklist
571 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000572 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000573 DEBUG(std::cerr << "\nReplacing "; N->dump();
574 std::cerr << "\nWith: "; RV.Val->dump();
575 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000576 std::vector<SDNode*> NowDead;
577 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000578
579 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000580 WorkList.push_back(RV.Val);
581 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000582
583 // Nodes can end up on the worklist more than once. Make sure we do
584 // not process a node that has been replaced.
585 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000586 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
587 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000588
589 // Finally, since the node is now dead, remove it from the graph.
590 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000591 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000592 }
593 }
Chris Lattner95038592005-10-05 06:35:28 +0000594
595 // If the root changed (e.g. it was a dead load, update the root).
596 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597}
598
Nate Begeman83e75ec2005-09-06 04:43:02 +0000599SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000600 switch(N->getOpcode()) {
601 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000602 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000603 case ISD::ADD: return visitADD(N);
604 case ISD::SUB: return visitSUB(N);
605 case ISD::MUL: return visitMUL(N);
606 case ISD::SDIV: return visitSDIV(N);
607 case ISD::UDIV: return visitUDIV(N);
608 case ISD::SREM: return visitSREM(N);
609 case ISD::UREM: return visitUREM(N);
610 case ISD::MULHU: return visitMULHU(N);
611 case ISD::MULHS: return visitMULHS(N);
612 case ISD::AND: return visitAND(N);
613 case ISD::OR: return visitOR(N);
614 case ISD::XOR: return visitXOR(N);
615 case ISD::SHL: return visitSHL(N);
616 case ISD::SRA: return visitSRA(N);
617 case ISD::SRL: return visitSRL(N);
618 case ISD::CTLZ: return visitCTLZ(N);
619 case ISD::CTTZ: return visitCTTZ(N);
620 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000621 case ISD::SELECT: return visitSELECT(N);
622 case ISD::SELECT_CC: return visitSELECT_CC(N);
623 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000624 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
625 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
626 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
627 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000628 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000629 case ISD::FADD: return visitFADD(N);
630 case ISD::FSUB: return visitFSUB(N);
631 case ISD::FMUL: return visitFMUL(N);
632 case ISD::FDIV: return visitFDIV(N);
633 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000634 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000635 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
636 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
637 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
638 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
639 case ISD::FP_ROUND: return visitFP_ROUND(N);
640 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
641 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
642 case ISD::FNEG: return visitFNEG(N);
643 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000644 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000645 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000646 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000647 case ISD::EXTLOAD:
648 case ISD::SEXTLOAD:
649 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000650 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000651 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
652 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000653 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000654 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000655 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000656 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000657}
658
Nate Begeman83e75ec2005-09-06 04:43:02 +0000659SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000660 std::vector<SDOperand> Ops;
661 bool Changed = false;
662
Nate Begeman1d4d4142005-09-01 00:19:25 +0000663 // If the token factor has two operands and one is the entry token, replace
664 // the token factor with the other operand.
665 if (N->getNumOperands() == 2) {
666 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000667 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000668 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000669 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000671
Nate Begemanded49632005-10-13 03:11:28 +0000672 // fold (tokenfactor (tokenfactor)) -> tokenfactor
673 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
674 SDOperand Op = N->getOperand(i);
675 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000676 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000677 Changed = true;
678 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
679 Ops.push_back(Op.getOperand(j));
680 } else {
681 Ops.push_back(Op);
682 }
683 }
684 if (Changed)
685 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000686 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687}
688
Nate Begeman83e75ec2005-09-06 04:43:02 +0000689SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000690 SDOperand N0 = N->getOperand(0);
691 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000692 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
693 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000694 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000695
696 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000697 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000698 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000699 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000700 if (N0C && !N1C)
701 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000702 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000703 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000704 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000705 // fold ((c1-A)+c2) -> (c1+c2)-A
706 if (N1C && N0.getOpcode() == ISD::SUB)
707 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
708 return DAG.getNode(ISD::SUB, VT,
709 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
710 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000711 // reassociate add
712 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
713 if (RADD.Val != 0)
714 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000715 // fold ((0-A) + B) -> B-A
716 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
717 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000718 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000719 // fold (A + (0-B)) -> A-B
720 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
721 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000722 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000723 // fold (A+(B-A)) -> B
724 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000725 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000726
Evan Cheng860771d2006-03-01 01:09:54 +0000727 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemanb0d04a72006-02-18 02:40:58 +0000728 return SDOperand();
Chris Lattner947c2892006-03-13 06:51:27 +0000729
730 // fold (a+b) -> (a|b) iff a and b share no bits.
731 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
732 uint64_t LHSZero, LHSOne;
733 uint64_t RHSZero, RHSOne;
734 uint64_t Mask = MVT::getIntVTBitMask(VT);
735 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
736 if (LHSZero) {
737 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
738
739 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
740 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
741 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
742 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
743 return DAG.getNode(ISD::OR, VT, N0, N1);
744 }
745 }
746
Nate Begeman83e75ec2005-09-06 04:43:02 +0000747 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000748}
749
Nate Begeman83e75ec2005-09-06 04:43:02 +0000750SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000751 SDOperand N0 = N->getOperand(0);
752 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000753 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
754 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000755 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000756
Chris Lattner854077d2005-10-17 01:07:11 +0000757 // fold (sub x, x) -> 0
758 if (N0 == N1)
759 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000760 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000761 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000762 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000763 // fold (sub x, c) -> (add x, -c)
764 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000765 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000766 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000767 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000768 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000769 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000770 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000771 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000772 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000773}
774
Nate Begeman83e75ec2005-09-06 04:43:02 +0000775SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000776 SDOperand N0 = N->getOperand(0);
777 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000778 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
779 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000780 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000781
782 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000783 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000784 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000785 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000786 if (N0C && !N1C)
787 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000788 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000789 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000790 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000791 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000792 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000793 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000794 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000795 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000796 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000797 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000798 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000799 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
800 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
801 // FIXME: If the input is something that is easily negated (e.g. a
802 // single-use add), we should put the negate there.
803 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
804 DAG.getNode(ISD::SHL, VT, N0,
805 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
806 TLI.getShiftAmountTy())));
807 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000808
809 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
810 if (N1C && N0.getOpcode() == ISD::SHL &&
811 isa<ConstantSDNode>(N0.getOperand(1))) {
812 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000813 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000814 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
815 }
816
817 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
818 // use.
819 {
820 SDOperand Sh(0,0), Y(0,0);
821 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
822 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
823 N0.Val->hasOneUse()) {
824 Sh = N0; Y = N1;
825 } else if (N1.getOpcode() == ISD::SHL &&
826 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
827 Sh = N1; Y = N0;
828 }
829 if (Sh.Val) {
830 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
831 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
832 }
833 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000834 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
835 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
836 isa<ConstantSDNode>(N0.getOperand(1))) {
837 return DAG.getNode(ISD::ADD, VT,
838 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
839 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
840 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000841
Nate Begemancd4d58c2006-02-03 06:46:56 +0000842 // reassociate mul
843 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
844 if (RMUL.Val != 0)
845 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000846 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000847}
848
Nate Begeman83e75ec2005-09-06 04:43:02 +0000849SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000850 SDOperand N0 = N->getOperand(0);
851 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000852 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
853 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000854 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000855
856 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000857 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000858 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000859 // fold (sdiv X, 1) -> X
860 if (N1C && N1C->getSignExtended() == 1LL)
861 return N0;
862 // fold (sdiv X, -1) -> 0-X
863 if (N1C && N1C->isAllOnesValue())
864 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000865 // If we know the sign bits of both operands are zero, strength reduce to a
866 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
867 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000868 if (TLI.MaskedValueIsZero(N1, SignBit) &&
869 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000870 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000871 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000872 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000873 (isPowerOf2_64(N1C->getSignExtended()) ||
874 isPowerOf2_64(-N1C->getSignExtended()))) {
875 // If dividing by powers of two is cheap, then don't perform the following
876 // fold.
877 if (TLI.isPow2DivCheap())
878 return SDOperand();
879 int64_t pow2 = N1C->getSignExtended();
880 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000881 unsigned lg2 = Log2_64(abs2);
882 // Splat the sign bit into the register
883 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000884 DAG.getConstant(MVT::getSizeInBits(VT)-1,
885 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000886 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000887 // Add (N0 < 0) ? abs2 - 1 : 0;
888 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
889 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000890 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000891 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000892 AddToWorkList(SRL.Val);
893 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000894 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
895 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000896 // If we're dividing by a positive value, we're done. Otherwise, we must
897 // negate the result.
898 if (pow2 > 0)
899 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000900 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000901 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
902 }
Nate Begeman69575232005-10-20 02:15:44 +0000903 // if integer divide is expensive and we satisfy the requirements, emit an
904 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000905 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000906 !TLI.isIntDivCheap()) {
907 SDOperand Op = BuildSDIV(N);
908 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000909 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000910 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911}
912
Nate Begeman83e75ec2005-09-06 04:43:02 +0000913SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 SDOperand N0 = N->getOperand(0);
915 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000916 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
917 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000918 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919
920 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000921 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000922 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000923 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000924 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000925 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000926 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000927 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000928 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
929 if (N1.getOpcode() == ISD::SHL) {
930 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
931 if (isPowerOf2_64(SHC->getValue())) {
932 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000933 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
934 DAG.getConstant(Log2_64(SHC->getValue()),
935 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000936 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000937 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000938 }
939 }
940 }
Nate Begeman69575232005-10-20 02:15:44 +0000941 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000942 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
943 SDOperand Op = BuildUDIV(N);
944 if (Op.Val) return Op;
945 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000946 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000947}
948
Nate Begeman83e75ec2005-09-06 04:43:02 +0000949SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000950 SDOperand N0 = N->getOperand(0);
951 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000952 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
953 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000954 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000955
956 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000957 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000958 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000959 // If we know the sign bits of both operands are zero, strength reduce to a
960 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
961 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000962 if (TLI.MaskedValueIsZero(N1, SignBit) &&
963 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000964 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000965 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000966}
967
Nate Begeman83e75ec2005-09-06 04:43:02 +0000968SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000969 SDOperand N0 = N->getOperand(0);
970 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000971 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
972 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000973 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974
975 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000976 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000977 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000978 // fold (urem x, pow2) -> (and x, pow2-1)
979 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000980 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000981 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
982 if (N1.getOpcode() == ISD::SHL) {
983 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
984 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000985 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000986 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000987 return DAG.getNode(ISD::AND, VT, N0, Add);
988 }
989 }
990 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000991 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000992}
993
Nate Begeman83e75ec2005-09-06 04:43:02 +0000994SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000995 SDOperand N0 = N->getOperand(0);
996 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000997 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998
999 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001000 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001001 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001003 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1005 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001006 TLI.getShiftAmountTy()));
1007 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001008}
1009
Nate Begeman83e75ec2005-09-06 04:43:02 +00001010SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011 SDOperand N0 = N->getOperand(0);
1012 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001013 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014
1015 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001016 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001017 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001019 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001020 return DAG.getConstant(0, N0.getValueType());
1021 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001022}
1023
Nate Begeman83e75ec2005-09-06 04:43:02 +00001024SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001025 SDOperand N0 = N->getOperand(0);
1026 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001027 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001028 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1029 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001030 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +00001031 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001032
1033 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001034 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001035 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001036 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001037 if (N0C && !N1C)
1038 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001039 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001040 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001041 return N0;
1042 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001043 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001044 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001045 // reassociate and
1046 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1047 if (RAND.Val != 0)
1048 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001049 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001050 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001051 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001052 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001053 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001054 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1055 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001056 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001057 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001058 ~N1C->getValue() & InMask)) {
1059 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1060 N0.getOperand(0));
1061
1062 // Replace uses of the AND with uses of the Zero extend node.
1063 CombineTo(N, Zext);
1064
Chris Lattner3603cd62006-02-02 07:17:31 +00001065 // We actually want to replace all uses of the any_extend with the
1066 // zero_extend, to avoid duplicating things. This will later cause this
1067 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001068 CombineTo(N0.Val, Zext);
Chris Lattner3603cd62006-02-02 07:17:31 +00001069 return SDOperand();
1070 }
1071 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001072 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1073 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1074 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1075 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1076
1077 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1078 MVT::isInteger(LL.getValueType())) {
1079 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1080 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1081 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001082 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001083 return DAG.getSetCC(VT, ORNode, LR, Op1);
1084 }
1085 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1086 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1087 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001088 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001089 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1090 }
1091 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1092 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1093 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001094 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001095 return DAG.getSetCC(VT, ORNode, LR, Op1);
1096 }
1097 }
1098 // canonicalize equivalent to ll == rl
1099 if (LL == RR && LR == RL) {
1100 Op1 = ISD::getSetCCSwappedOperands(Op1);
1101 std::swap(RL, RR);
1102 }
1103 if (LL == RL && LR == RR) {
1104 bool isInteger = MVT::isInteger(LL.getValueType());
1105 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1106 if (Result != ISD::SETCC_INVALID)
1107 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1108 }
1109 }
1110 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1111 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1112 N1.getOpcode() == ISD::ZERO_EXTEND &&
1113 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1114 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1115 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001116 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001117 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1118 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001119 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001120 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001121 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1122 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001123 N0.getOperand(1) == N1.getOperand(1)) {
1124 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1125 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001126 AddToWorkList(ANDNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001127 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1128 }
Nate Begemande996292006-02-03 22:24:05 +00001129 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1130 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001131 if (!MVT::isVector(VT) &&
1132 SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001133 return SDOperand();
Nate Begemanded49632005-10-13 03:11:28 +00001134 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001135 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001136 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001137 // If we zero all the possible extended bits, then we can turn this into
1138 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001139 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001140 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001141 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1142 N0.getOperand(1), N0.getOperand(2),
1143 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001144 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001145 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001146 return SDOperand();
1147 }
1148 }
1149 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001150 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001151 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001152 // If we zero all the possible extended bits, then we can turn this into
1153 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001154 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001155 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001156 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1157 N0.getOperand(1), N0.getOperand(2),
1158 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001159 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001160 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001161 return SDOperand();
1162 }
1163 }
Chris Lattner15045b62006-02-28 06:35:35 +00001164
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001165 // fold (and (load x), 255) -> (zextload x, i8)
1166 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1167 if (N1C &&
1168 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1169 N0.getOpcode() == ISD::ZEXTLOAD) &&
1170 N0.hasOneUse()) {
1171 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001172 if (N1C->getValue() == 255)
1173 EVT = MVT::i8;
1174 else if (N1C->getValue() == 65535)
1175 EVT = MVT::i16;
1176 else if (N1C->getValue() == ~0U)
1177 EVT = MVT::i32;
1178 else
1179 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001180
1181 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1182 cast<VTSDNode>(N0.getOperand(3))->getVT();
1183 if (EVT != MVT::Other && LoadedVT > EVT) {
Chris Lattner15045b62006-02-28 06:35:35 +00001184 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1185 // For big endian targets, we need to add an offset to the pointer to load
1186 // the correct bytes. For little endian systems, we merely need to read
1187 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001188 unsigned PtrOff =
1189 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1190 SDOperand NewPtr = N0.getOperand(1);
1191 if (!TLI.isLittleEndian())
1192 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1193 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001194 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001195 SDOperand Load =
1196 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1197 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001198 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001199 CombineTo(N0.Val, Load, Load.getValue(1));
1200 return SDOperand();
1201 }
1202 }
1203
Nate Begeman83e75ec2005-09-06 04:43:02 +00001204 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001205}
1206
Nate Begeman83e75ec2005-09-06 04:43:02 +00001207SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001208 SDOperand N0 = N->getOperand(0);
1209 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001210 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001211 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1212 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001213 MVT::ValueType VT = N1.getValueType();
1214 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001215
1216 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001217 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001218 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001219 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001220 if (N0C && !N1C)
1221 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001222 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001223 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001224 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001225 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001226 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001227 return N1;
1228 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001229 if (N1C &&
1230 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001231 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001232 // reassociate or
1233 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1234 if (ROR.Val != 0)
1235 return ROR;
1236 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1237 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001238 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001239 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1240 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1241 N1),
1242 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001243 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001244 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1245 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1246 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1247 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1248
1249 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1250 MVT::isInteger(LL.getValueType())) {
1251 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1252 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1253 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1254 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1255 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001256 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001257 return DAG.getSetCC(VT, ORNode, LR, Op1);
1258 }
1259 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1260 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1261 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1262 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1263 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001264 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001265 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1266 }
1267 }
1268 // canonicalize equivalent to ll == rl
1269 if (LL == RR && LR == RL) {
1270 Op1 = ISD::getSetCCSwappedOperands(Op1);
1271 std::swap(RL, RR);
1272 }
1273 if (LL == RL && LR == RR) {
1274 bool isInteger = MVT::isInteger(LL.getValueType());
1275 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1276 if (Result != ISD::SETCC_INVALID)
1277 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1278 }
1279 }
1280 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1281 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1282 N1.getOpcode() == ISD::ZERO_EXTEND &&
1283 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1284 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1285 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001286 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001287 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1288 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001289 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1290 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1291 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1292 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1293 N0.getOperand(1) == N1.getOperand(1)) {
1294 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1295 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001296 AddToWorkList(ORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001297 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1298 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001299 // canonicalize shl to left side in a shl/srl pair, to match rotate
1300 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1301 std::swap(N0, N1);
1302 // check for rotl, rotr
1303 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1304 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001305 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001306 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1307 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1308 N1.getOperand(1).getOpcode() == ISD::Constant) {
1309 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1310 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1311 if ((c1val + c2val) == OpSizeInBits)
1312 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1313 }
1314 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1315 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1316 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1317 if (ConstantSDNode *SUBC =
1318 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1319 if (SUBC->getValue() == OpSizeInBits)
1320 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1321 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1322 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1323 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1324 if (ConstantSDNode *SUBC =
1325 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1326 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001327 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001328 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1329 N1.getOperand(1));
1330 else
1331 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1332 N0.getOperand(1));
1333 }
1334 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001335 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001336}
1337
Nate Begeman83e75ec2005-09-06 04:43:02 +00001338SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001339 SDOperand N0 = N->getOperand(0);
1340 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001341 SDOperand LHS, RHS, CC;
1342 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1343 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001344 MVT::ValueType VT = N0.getValueType();
1345
1346 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001347 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001348 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001349 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001350 if (N0C && !N1C)
1351 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001352 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001354 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001355 // reassociate xor
1356 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1357 if (RXOR.Val != 0)
1358 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001359 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001360 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1361 bool isInt = MVT::isInteger(LHS.getValueType());
1362 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1363 isInt);
1364 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001365 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001366 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001367 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 assert(0 && "Unhandled SetCC Equivalent!");
1369 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370 }
Nate Begeman99801192005-09-07 23:25:52 +00001371 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1372 if (N1C && N1C->getValue() == 1 &&
1373 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001375 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1376 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1378 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001379 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001380 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381 }
1382 }
Nate Begeman99801192005-09-07 23:25:52 +00001383 // fold !(x or y) -> (!x and !y) iff x or y are constants
1384 if (N1C && N1C->isAllOnesValue() &&
1385 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001386 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001387 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1388 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1390 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001391 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001392 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001393 }
1394 }
Nate Begeman223df222005-09-08 20:18:10 +00001395 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1396 if (N1C && N0.getOpcode() == ISD::XOR) {
1397 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1398 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1399 if (N00C)
1400 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1401 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1402 if (N01C)
1403 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1404 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1405 }
1406 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001407 if (N0 == N1) {
1408 if (!MVT::isVector(VT)) {
1409 return DAG.getConstant(0, VT);
1410 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1411 // Produce a vector of zeros.
1412 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1413 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1414 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
1415 }
1416 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001417 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1418 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1419 N1.getOpcode() == ISD::ZERO_EXTEND &&
1420 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1421 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1422 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001423 AddToWorkList(XORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001424 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1425 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001426 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1427 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1428 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1429 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1430 N0.getOperand(1) == N1.getOperand(1)) {
1431 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1432 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001433 AddToWorkList(XORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001434 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1435 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001436 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001437}
1438
Nate Begeman83e75ec2005-09-06 04:43:02 +00001439SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440 SDOperand N0 = N->getOperand(0);
1441 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001442 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1443 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001444 MVT::ValueType VT = N0.getValueType();
1445 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1446
1447 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001448 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001449 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001450 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001451 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001452 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001454 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001455 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001457 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001458 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001460 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001461 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001462 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001463 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001464 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001465 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466 N0.getOperand(1).getOpcode() == ISD::Constant) {
1467 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001468 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001469 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001470 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001472 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001473 }
1474 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1475 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001476 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001477 N0.getOperand(1).getOpcode() == ISD::Constant) {
1478 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001479 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1481 DAG.getConstant(~0ULL << c1, VT));
1482 if (c2 > c1)
1483 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001484 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001485 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001486 return DAG.getNode(ISD::SRL, VT, Mask,
1487 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001488 }
1489 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001490 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001491 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001492 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001493 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1494 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1495 isa<ConstantSDNode>(N0.getOperand(1))) {
1496 return DAG.getNode(ISD::ADD, VT,
1497 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1498 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1499 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001500 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001501}
1502
Nate Begeman83e75ec2005-09-06 04:43:02 +00001503SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001504 SDOperand N0 = N->getOperand(0);
1505 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001506 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1507 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001508 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001509
1510 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001511 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001512 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001513 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001514 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001515 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001516 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001517 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001518 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001519 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001520 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001521 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001522 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001523 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001524 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001525 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1526 // sext_inreg.
1527 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1528 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1529 MVT::ValueType EVT;
1530 switch (LowBits) {
1531 default: EVT = MVT::Other; break;
1532 case 1: EVT = MVT::i1; break;
1533 case 8: EVT = MVT::i8; break;
1534 case 16: EVT = MVT::i16; break;
1535 case 32: EVT = MVT::i32; break;
1536 }
1537 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1538 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1539 DAG.getValueType(EVT));
1540 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001541
1542 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1543 if (N1C && N0.getOpcode() == ISD::SRA) {
1544 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1545 unsigned Sum = N1C->getValue() + C1->getValue();
1546 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1547 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1548 DAG.getConstant(Sum, N1C->getValueType(0)));
1549 }
1550 }
1551
Nate Begeman1d4d4142005-09-01 00:19:25 +00001552 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001553 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001554 return DAG.getNode(ISD::SRL, VT, N0, N1);
1555 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001556}
1557
Nate Begeman83e75ec2005-09-06 04:43:02 +00001558SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559 SDOperand N0 = N->getOperand(0);
1560 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001561 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1562 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001563 MVT::ValueType VT = N0.getValueType();
1564 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1565
1566 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001567 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001568 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001569 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001570 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001571 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001572 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001573 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001574 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001575 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001576 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001577 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001578 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001579 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001580 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001581 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001582 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001583 N0.getOperand(1).getOpcode() == ISD::Constant) {
1584 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001585 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001586 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001587 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001588 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001589 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001591 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001592}
1593
Nate Begeman83e75ec2005-09-06 04:43:02 +00001594SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001595 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001596 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001597 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001598
1599 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001600 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001601 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001602 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001603}
1604
Nate Begeman83e75ec2005-09-06 04:43:02 +00001605SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001606 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001607 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001608 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001609
1610 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001611 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001612 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001613 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001614}
1615
Nate Begeman83e75ec2005-09-06 04:43:02 +00001616SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001618 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001619 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001620
1621 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001622 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001623 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001624 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625}
1626
Nate Begeman452d7be2005-09-16 00:54:12 +00001627SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1628 SDOperand N0 = N->getOperand(0);
1629 SDOperand N1 = N->getOperand(1);
1630 SDOperand N2 = N->getOperand(2);
1631 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1632 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1633 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1634 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001635
Nate Begeman452d7be2005-09-16 00:54:12 +00001636 // fold select C, X, X -> X
1637 if (N1 == N2)
1638 return N1;
1639 // fold select true, X, Y -> X
1640 if (N0C && !N0C->isNullValue())
1641 return N1;
1642 // fold select false, X, Y -> Y
1643 if (N0C && N0C->isNullValue())
1644 return N2;
1645 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001646 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001647 return DAG.getNode(ISD::OR, VT, N0, N2);
1648 // fold select C, 0, X -> ~C & X
1649 // FIXME: this should check for C type == X type, not i1?
1650 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1651 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001652 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001653 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1654 }
1655 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001656 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001657 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001658 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001659 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1660 }
1661 // fold select C, X, 0 -> C & X
1662 // FIXME: this should check for C type == X type, not i1?
1663 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1664 return DAG.getNode(ISD::AND, VT, N0, N1);
1665 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1666 if (MVT::i1 == VT && N0 == N1)
1667 return DAG.getNode(ISD::OR, VT, N0, N2);
1668 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1669 if (MVT::i1 == VT && N0 == N2)
1670 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001671 // If we can fold this based on the true/false value, do so.
1672 if (SimplifySelectOps(N, N1, N2))
1673 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001674 // fold selects based on a setcc into other things, such as min/max/abs
1675 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001676 // FIXME:
1677 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1678 // having to say they don't support SELECT_CC on every type the DAG knows
1679 // about, since there is no way to mark an opcode illegal at all value types
1680 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1681 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1682 N1, N2, N0.getOperand(2));
1683 else
1684 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001685 return SDOperand();
1686}
1687
1688SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001689 SDOperand N0 = N->getOperand(0);
1690 SDOperand N1 = N->getOperand(1);
1691 SDOperand N2 = N->getOperand(2);
1692 SDOperand N3 = N->getOperand(3);
1693 SDOperand N4 = N->getOperand(4);
1694 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1695 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1696 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1697 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1698
1699 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001700 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001701 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1702
Nate Begeman44728a72005-09-19 22:34:01 +00001703 // fold select_cc lhs, rhs, x, x, cc -> x
1704 if (N2 == N3)
1705 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001706
1707 // If we can fold this based on the true/false value, do so.
1708 if (SimplifySelectOps(N, N2, N3))
1709 return SDOperand();
1710
Nate Begeman44728a72005-09-19 22:34:01 +00001711 // fold select_cc into other things, such as min/max/abs
1712 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001713}
1714
1715SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1716 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1717 cast<CondCodeSDNode>(N->getOperand(2))->get());
1718}
1719
Nate Begeman83e75ec2005-09-06 04:43:02 +00001720SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001721 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001722 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001723 MVT::ValueType VT = N->getValueType(0);
1724
Nate Begeman1d4d4142005-09-01 00:19:25 +00001725 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001726 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001727 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001728 // fold (sext (sext x)) -> (sext x)
1729 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001730 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001731 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001732 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1733 (!AfterLegalize ||
1734 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001735 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1736 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001737 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001738 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1739 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001740 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1741 N0.getOperand(1), N0.getOperand(2),
1742 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001743 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001744 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1745 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001746 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001747 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001748
1749 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1750 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1751 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1752 N0.hasOneUse()) {
1753 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1754 N0.getOperand(1), N0.getOperand(2),
1755 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001756 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001757 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1758 ExtLoad.getValue(1));
1759 return SDOperand();
1760 }
1761
Nate Begeman83e75ec2005-09-06 04:43:02 +00001762 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001763}
1764
Nate Begeman83e75ec2005-09-06 04:43:02 +00001765SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001766 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001767 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001768 MVT::ValueType VT = N->getValueType(0);
1769
Nate Begeman1d4d4142005-09-01 00:19:25 +00001770 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001771 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001772 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001773 // fold (zext (zext x)) -> (zext x)
1774 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001775 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001776 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1777 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001778 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001779 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001780 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001781 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1782 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001783 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1784 N0.getOperand(1), N0.getOperand(2),
1785 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001786 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001787 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1788 ExtLoad.getValue(1));
1789 return SDOperand();
1790 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001791
1792 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1793 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1794 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1795 N0.hasOneUse()) {
1796 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1797 N0.getOperand(1), N0.getOperand(2),
1798 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001799 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001800 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1801 ExtLoad.getValue(1));
1802 return SDOperand();
1803 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001804 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001805}
1806
Nate Begeman83e75ec2005-09-06 04:43:02 +00001807SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001808 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001809 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001810 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001811 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001812 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001813 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001814
Nate Begeman1d4d4142005-09-01 00:19:25 +00001815 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001816 if (N0C) {
1817 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001818 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001819 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001820 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001821 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001822 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001823 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001824 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001825 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1826 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1827 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001828 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001829 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001830 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1831 if (N0.getOpcode() == ISD::AssertSext &&
1832 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001833 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001834 }
1835 // fold (sext_in_reg (sextload x)) -> (sextload x)
1836 if (N0.getOpcode() == ISD::SEXTLOAD &&
1837 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001838 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001839 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001840 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841 if (N0.getOpcode() == ISD::SETCC &&
1842 TLI.getSetCCResultContents() ==
1843 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001844 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001845 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001846 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001847 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001848 // fold (sext_in_reg (srl x)) -> sra x
1849 if (N0.getOpcode() == ISD::SRL &&
1850 N0.getOperand(1).getOpcode() == ISD::Constant &&
1851 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1852 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1853 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001854 }
Nate Begemanded49632005-10-13 03:11:28 +00001855 // fold (sext_inreg (extload x)) -> (sextload x)
1856 if (N0.getOpcode() == ISD::EXTLOAD &&
1857 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001858 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001859 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1860 N0.getOperand(1), N0.getOperand(2),
1861 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001862 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001863 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001864 return SDOperand();
1865 }
1866 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001867 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001868 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001869 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001870 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1871 N0.getOperand(1), N0.getOperand(2),
1872 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001873 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001874 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001875 return SDOperand();
1876 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001877 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001878}
1879
Nate Begeman83e75ec2005-09-06 04:43:02 +00001880SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001881 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001882 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001883 MVT::ValueType VT = N->getValueType(0);
1884
1885 // noop truncate
1886 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001887 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001888 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001889 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001890 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001891 // fold (truncate (truncate x)) -> (truncate x)
1892 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001893 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1895 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1896 if (N0.getValueType() < VT)
1897 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001898 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001899 else if (N0.getValueType() > VT)
1900 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001901 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001902 else
1903 // if the source and dest are the same type, we can drop both the extend
1904 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001905 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001906 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001907 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001908 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001909 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1910 "Cannot truncate to larger type!");
1911 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001912 // For big endian targets, we need to add an offset to the pointer to load
1913 // the correct bytes. For little endian systems, we merely need to read
1914 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001915 uint64_t PtrOff =
1916 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001917 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1918 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1919 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001920 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001921 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001922 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001923 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001924 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001925 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001926 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001927}
1928
Chris Lattner94683772005-12-23 05:30:37 +00001929SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1930 SDOperand N0 = N->getOperand(0);
1931 MVT::ValueType VT = N->getValueType(0);
1932
1933 // If the input is a constant, let getNode() fold it.
1934 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1935 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1936 if (Res.Val != N) return Res;
1937 }
1938
Chris Lattnerc8547d82005-12-23 05:37:50 +00001939 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1940 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1941
Chris Lattner57104102005-12-23 05:44:41 +00001942 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001943 // FIXME: These xforms need to know that the resultant load doesn't need a
1944 // higher alignment than the original!
1945 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001946 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1947 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001948 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00001949 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1950 Load.getValue(1));
1951 return Load;
1952 }
1953
Chris Lattner94683772005-12-23 05:30:37 +00001954 return SDOperand();
1955}
1956
Chris Lattner01b3d732005-09-28 22:28:18 +00001957SDOperand DAGCombiner::visitFADD(SDNode *N) {
1958 SDOperand N0 = N->getOperand(0);
1959 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001960 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1961 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001962 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001963
1964 // fold (fadd c1, c2) -> c1+c2
1965 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001966 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001967 // canonicalize constant to RHS
1968 if (N0CFP && !N1CFP)
1969 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001970 // fold (A + (-B)) -> A-B
1971 if (N1.getOpcode() == ISD::FNEG)
1972 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001973 // fold ((-A) + B) -> B-A
1974 if (N0.getOpcode() == ISD::FNEG)
1975 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001976 return SDOperand();
1977}
1978
1979SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1980 SDOperand N0 = N->getOperand(0);
1981 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001982 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1983 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001984 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001985
1986 // fold (fsub c1, c2) -> c1-c2
1987 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001988 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001989 // fold (A-(-B)) -> A+B
1990 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001991 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001992 return SDOperand();
1993}
1994
1995SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1996 SDOperand N0 = N->getOperand(0);
1997 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001998 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1999 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002000 MVT::ValueType VT = N->getValueType(0);
2001
Nate Begeman11af4ea2005-10-17 20:40:11 +00002002 // fold (fmul c1, c2) -> c1*c2
2003 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002004 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002005 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002006 if (N0CFP && !N1CFP)
2007 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002008 // fold (fmul X, 2.0) -> (fadd X, X)
2009 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2010 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002011 return SDOperand();
2012}
2013
2014SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2015 SDOperand N0 = N->getOperand(0);
2016 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002017 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2018 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002019 MVT::ValueType VT = N->getValueType(0);
2020
Nate Begemana148d982006-01-18 22:35:16 +00002021 // fold (fdiv c1, c2) -> c1/c2
2022 if (N0CFP && N1CFP)
2023 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002024 return SDOperand();
2025}
2026
2027SDOperand DAGCombiner::visitFREM(SDNode *N) {
2028 SDOperand N0 = N->getOperand(0);
2029 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002030 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2031 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002032 MVT::ValueType VT = N->getValueType(0);
2033
Nate Begemana148d982006-01-18 22:35:16 +00002034 // fold (frem c1, c2) -> fmod(c1,c2)
2035 if (N0CFP && N1CFP)
2036 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002037 return SDOperand();
2038}
2039
Chris Lattner12d83032006-03-05 05:30:57 +00002040SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2041 SDOperand N0 = N->getOperand(0);
2042 SDOperand N1 = N->getOperand(1);
2043 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2044 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2045 MVT::ValueType VT = N->getValueType(0);
2046
2047 if (N0CFP && N1CFP) // Constant fold
2048 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2049
2050 if (N1CFP) {
2051 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2052 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2053 union {
2054 double d;
2055 int64_t i;
2056 } u;
2057 u.d = N1CFP->getValue();
2058 if (u.i >= 0)
2059 return DAG.getNode(ISD::FABS, VT, N0);
2060 else
2061 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2062 }
2063
2064 // copysign(fabs(x), y) -> copysign(x, y)
2065 // copysign(fneg(x), y) -> copysign(x, y)
2066 // copysign(copysign(x,z), y) -> copysign(x, y)
2067 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2068 N0.getOpcode() == ISD::FCOPYSIGN)
2069 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2070
2071 // copysign(x, abs(y)) -> abs(x)
2072 if (N1.getOpcode() == ISD::FABS)
2073 return DAG.getNode(ISD::FABS, VT, N0);
2074
2075 // copysign(x, copysign(y,z)) -> copysign(x, z)
2076 if (N1.getOpcode() == ISD::FCOPYSIGN)
2077 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2078
2079 // copysign(x, fp_extend(y)) -> copysign(x, y)
2080 // copysign(x, fp_round(y)) -> copysign(x, y)
2081 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2082 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2083
2084 return SDOperand();
2085}
2086
2087
Chris Lattner01b3d732005-09-28 22:28:18 +00002088
Nate Begeman83e75ec2005-09-06 04:43:02 +00002089SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002090 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002091 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002092 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002093
2094 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002095 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002096 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002097 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002098}
2099
Nate Begeman83e75ec2005-09-06 04:43:02 +00002100SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002101 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002102 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002103 MVT::ValueType VT = N->getValueType(0);
2104
Nate Begeman1d4d4142005-09-01 00:19:25 +00002105 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002106 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002107 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002108 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109}
2110
Nate Begeman83e75ec2005-09-06 04:43:02 +00002111SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002112 SDOperand N0 = N->getOperand(0);
2113 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2114 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002115
2116 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002117 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002118 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002119 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002120}
2121
Nate Begeman83e75ec2005-09-06 04:43:02 +00002122SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002123 SDOperand N0 = N->getOperand(0);
2124 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2125 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002126
2127 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002128 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002129 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002130 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002131}
2132
Nate Begeman83e75ec2005-09-06 04:43:02 +00002133SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002134 SDOperand N0 = N->getOperand(0);
2135 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2136 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002137
2138 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002139 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002140 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002141
2142 // fold (fp_round (fp_extend x)) -> x
2143 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2144 return N0.getOperand(0);
2145
2146 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2147 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2148 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2149 AddToWorkList(Tmp.Val);
2150 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2151 }
2152
Nate Begeman83e75ec2005-09-06 04:43:02 +00002153 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002154}
2155
Nate Begeman83e75ec2005-09-06 04:43:02 +00002156SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002157 SDOperand N0 = N->getOperand(0);
2158 MVT::ValueType VT = N->getValueType(0);
2159 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002160 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002161
Nate Begeman1d4d4142005-09-01 00:19:25 +00002162 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002163 if (N0CFP) {
2164 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002165 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002166 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002167 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002168}
2169
Nate Begeman83e75ec2005-09-06 04:43:02 +00002170SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002171 SDOperand N0 = N->getOperand(0);
2172 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2173 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002174
2175 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002176 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002177 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002178 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002179}
2180
Nate Begeman83e75ec2005-09-06 04:43:02 +00002181SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002182 SDOperand N0 = N->getOperand(0);
2183 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2184 MVT::ValueType VT = N->getValueType(0);
2185
2186 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002187 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002188 return DAG.getNode(ISD::FNEG, VT, N0);
2189 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002190 if (N0.getOpcode() == ISD::SUB)
2191 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002192 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002193 if (N0.getOpcode() == ISD::FNEG)
2194 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002195 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002196}
2197
Nate Begeman83e75ec2005-09-06 04:43:02 +00002198SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002199 SDOperand N0 = N->getOperand(0);
2200 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2201 MVT::ValueType VT = N->getValueType(0);
2202
Nate Begeman1d4d4142005-09-01 00:19:25 +00002203 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002204 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002205 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002206 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002207 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002208 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002209 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002210 // fold (fabs (fcopysign x, y)) -> (fabs x)
2211 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2212 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2213
Nate Begeman83e75ec2005-09-06 04:43:02 +00002214 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002215}
2216
Nate Begeman44728a72005-09-19 22:34:01 +00002217SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2218 SDOperand Chain = N->getOperand(0);
2219 SDOperand N1 = N->getOperand(1);
2220 SDOperand N2 = N->getOperand(2);
2221 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2222
2223 // never taken branch, fold to chain
2224 if (N1C && N1C->isNullValue())
2225 return Chain;
2226 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002227 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002228 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002229 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2230 // on the target.
2231 if (N1.getOpcode() == ISD::SETCC &&
2232 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2233 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2234 N1.getOperand(0), N1.getOperand(1), N2);
2235 }
Nate Begeman44728a72005-09-19 22:34:01 +00002236 return SDOperand();
2237}
2238
Chris Lattner3ea0b472005-10-05 06:47:48 +00002239// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2240//
Nate Begeman44728a72005-09-19 22:34:01 +00002241SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002242 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2243 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2244
2245 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002246 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2247 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2248
2249 // fold br_cc true, dest -> br dest (unconditional branch)
2250 if (SCCC && SCCC->getValue())
2251 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2252 N->getOperand(4));
2253 // fold br_cc false, dest -> unconditional fall through
2254 if (SCCC && SCCC->isNullValue())
2255 return N->getOperand(0);
2256 // fold to a simpler setcc
2257 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2258 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2259 Simp.getOperand(2), Simp.getOperand(0),
2260 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002261 return SDOperand();
2262}
2263
Chris Lattner01a22022005-10-10 22:04:48 +00002264SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2265 SDOperand Chain = N->getOperand(0);
2266 SDOperand Ptr = N->getOperand(1);
2267 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002268
2269 // If there are no uses of the loaded value, change uses of the chain value
2270 // into uses of the chain input (i.e. delete the dead load).
2271 if (N->hasNUsesOfValue(0, 0))
2272 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002273
2274 // If this load is directly stored, replace the load value with the stored
2275 // value.
2276 // TODO: Handle store large -> read small portion.
2277 // TODO: Handle TRUNCSTORE/EXTLOAD
2278 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2279 Chain.getOperand(1).getValueType() == N->getValueType(0))
2280 return CombineTo(N, Chain.getOperand(1), Chain);
2281
2282 return SDOperand();
2283}
2284
Chris Lattner29cd7db2006-03-31 18:10:41 +00002285/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2286SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2287 SDOperand Chain = N->getOperand(0);
2288 SDOperand Ptr = N->getOperand(1);
2289 SDOperand SrcValue = N->getOperand(2);
2290 SDOperand EVT = N->getOperand(3);
2291
2292 // If there are no uses of the loaded value, change uses of the chain value
2293 // into uses of the chain input (i.e. delete the dead load).
2294 if (N->hasNUsesOfValue(0, 0))
2295 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2296
2297 return SDOperand();
2298}
2299
Chris Lattner87514ca2005-10-10 22:31:19 +00002300SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2301 SDOperand Chain = N->getOperand(0);
2302 SDOperand Value = N->getOperand(1);
2303 SDOperand Ptr = N->getOperand(2);
2304 SDOperand SrcValue = N->getOperand(3);
2305
2306 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002307 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002308 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2309 // Make sure that these stores are the same value type:
2310 // FIXME: we really care that the second store is >= size of the first.
2311 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002312 // Create a new store of Value that replaces both stores.
2313 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002314 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2315 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002316 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2317 PrevStore->getOperand(0), Value, Ptr,
2318 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002319 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002320 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002321 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002322 }
2323
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002324 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002325 // FIXME: This needs to know that the resultant store does not need a
2326 // higher alignment than the original.
2327 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002328 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2329 Ptr, SrcValue);
2330
Chris Lattner87514ca2005-10-10 22:31:19 +00002331 return SDOperand();
2332}
2333
Chris Lattnerca242442006-03-19 01:27:56 +00002334SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2335 SDOperand InVec = N->getOperand(0);
2336 SDOperand InVal = N->getOperand(1);
2337 SDOperand EltNo = N->getOperand(2);
2338
2339 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2340 // vector with the inserted element.
2341 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2342 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2343 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2344 if (Elt < Ops.size())
2345 Ops[Elt] = InVal;
2346 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), Ops);
2347 }
2348
2349 return SDOperand();
2350}
2351
2352SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2353 SDOperand InVec = N->getOperand(0);
2354 SDOperand InVal = N->getOperand(1);
2355 SDOperand EltNo = N->getOperand(2);
2356 SDOperand NumElts = N->getOperand(3);
2357 SDOperand EltType = N->getOperand(4);
2358
2359 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2360 // vector with the inserted element.
2361 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2362 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2363 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2364 if (Elt < Ops.size()-2)
2365 Ops[Elt] = InVal;
2366 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), Ops);
2367 }
2368
2369 return SDOperand();
2370}
2371
Chris Lattnerd7648c82006-03-28 20:28:38 +00002372SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2373 unsigned NumInScalars = N->getNumOperands()-2;
2374 SDOperand NumElts = N->getOperand(NumInScalars);
2375 SDOperand EltType = N->getOperand(NumInScalars+1);
2376
2377 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2378 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2379 // two distinct vectors, turn this into a shuffle node.
2380 SDOperand VecIn1, VecIn2;
2381 for (unsigned i = 0; i != NumInScalars; ++i) {
2382 // Ignore undef inputs.
2383 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2384
2385 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2386 // constant index, bail out.
2387 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2388 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2389 VecIn1 = VecIn2 = SDOperand(0, 0);
2390 break;
2391 }
2392
2393 // If the input vector type disagrees with the result of the vbuild_vector,
2394 // we can't make a shuffle.
2395 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2396 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2397 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2398 VecIn1 = VecIn2 = SDOperand(0, 0);
2399 break;
2400 }
2401
2402 // Otherwise, remember this. We allow up to two distinct input vectors.
2403 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2404 continue;
2405
2406 if (VecIn1.Val == 0) {
2407 VecIn1 = ExtractedFromVec;
2408 } else if (VecIn2.Val == 0) {
2409 VecIn2 = ExtractedFromVec;
2410 } else {
2411 // Too many inputs.
2412 VecIn1 = VecIn2 = SDOperand(0, 0);
2413 break;
2414 }
2415 }
2416
2417 // If everything is good, we can make a shuffle operation.
2418 if (VecIn1.Val) {
2419 std::vector<SDOperand> BuildVecIndices;
2420 for (unsigned i = 0; i != NumInScalars; ++i) {
2421 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2422 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2423 continue;
2424 }
2425
2426 SDOperand Extract = N->getOperand(i);
2427
2428 // If extracting from the first vector, just use the index directly.
2429 if (Extract.getOperand(0) == VecIn1) {
2430 BuildVecIndices.push_back(Extract.getOperand(1));
2431 continue;
2432 }
2433
2434 // Otherwise, use InIdx + VecSize
2435 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2436 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2437 }
2438
2439 // Add count and size info.
2440 BuildVecIndices.push_back(NumElts);
2441 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2442
2443 // Return the new VVECTOR_SHUFFLE node.
2444 std::vector<SDOperand> Ops;
2445 Ops.push_back(VecIn1);
Chris Lattnercef896e2006-03-28 22:19:47 +00002446 if (VecIn2.Val) {
2447 Ops.push_back(VecIn2);
2448 } else {
2449 // Use an undef vbuild_vector as input for the second operand.
2450 std::vector<SDOperand> UnOps(NumInScalars,
2451 DAG.getNode(ISD::UNDEF,
2452 cast<VTSDNode>(EltType)->getVT()));
2453 UnOps.push_back(NumElts);
2454 UnOps.push_back(EltType);
2455 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, UnOps));
2456 }
Chris Lattnerd7648c82006-03-28 20:28:38 +00002457 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR,MVT::Vector, BuildVecIndices));
2458 Ops.push_back(NumElts);
2459 Ops.push_back(EltType);
2460 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2461 }
2462
2463 return SDOperand();
2464}
2465
Chris Lattner66445d32006-03-28 22:11:53 +00002466SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
2467 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2468 if (N->getOperand(0) == N->getOperand(1)) {
2469 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2470 // first operand.
2471 std::vector<SDOperand> MappedOps;
2472 SDOperand ShufMask = N->getOperand(2);
2473 unsigned NumElts = ShufMask.getNumOperands();
2474 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
2475 if (cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() >= NumElts) {
2476 unsigned NewIdx =
2477 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2478 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2479 } else {
2480 MappedOps.push_back(ShufMask.getOperand(i));
2481 }
2482 }
2483 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
2484 MappedOps);
2485 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
2486 N->getOperand(0),
2487 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2488 ShufMask);
2489 }
2490
2491 return SDOperand();
2492}
2493
Nate Begeman44728a72005-09-19 22:34:01 +00002494SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002495 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2496
2497 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2498 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2499 // If we got a simplified select_cc node back from SimplifySelectCC, then
2500 // break it down into a new SETCC node, and a new SELECT node, and then return
2501 // the SELECT node, since we were called with a SELECT node.
2502 if (SCC.Val) {
2503 // Check to see if we got a select_cc back (to turn into setcc/select).
2504 // Otherwise, just return whatever node we got back, like fabs.
2505 if (SCC.getOpcode() == ISD::SELECT_CC) {
2506 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2507 SCC.getOperand(0), SCC.getOperand(1),
2508 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002509 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002510 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2511 SCC.getOperand(3), SETCC);
2512 }
2513 return SCC;
2514 }
Nate Begeman44728a72005-09-19 22:34:01 +00002515 return SDOperand();
2516}
2517
Chris Lattner40c62d52005-10-18 06:04:22 +00002518/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2519/// are the two values being selected between, see if we can simplify the
2520/// select.
2521///
2522bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2523 SDOperand RHS) {
2524
2525 // If this is a select from two identical things, try to pull the operation
2526 // through the select.
2527 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2528#if 0
2529 std::cerr << "SELECT: ["; LHS.Val->dump();
2530 std::cerr << "] ["; RHS.Val->dump();
2531 std::cerr << "]\n";
2532#endif
2533
2534 // If this is a load and the token chain is identical, replace the select
2535 // of two loads with a load through a select of the address to load from.
2536 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2537 // constants have been dropped into the constant pool.
2538 if ((LHS.getOpcode() == ISD::LOAD ||
2539 LHS.getOpcode() == ISD::EXTLOAD ||
2540 LHS.getOpcode() == ISD::ZEXTLOAD ||
2541 LHS.getOpcode() == ISD::SEXTLOAD) &&
2542 // Token chains must be identical.
2543 LHS.getOperand(0) == RHS.getOperand(0) &&
2544 // If this is an EXTLOAD, the VT's must match.
2545 (LHS.getOpcode() == ISD::LOAD ||
2546 LHS.getOperand(3) == RHS.getOperand(3))) {
2547 // FIXME: this conflates two src values, discarding one. This is not
2548 // the right thing to do, but nothing uses srcvalues now. When they do,
2549 // turn SrcValue into a list of locations.
2550 SDOperand Addr;
2551 if (TheSelect->getOpcode() == ISD::SELECT)
2552 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2553 TheSelect->getOperand(0), LHS.getOperand(1),
2554 RHS.getOperand(1));
2555 else
2556 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2557 TheSelect->getOperand(0),
2558 TheSelect->getOperand(1),
2559 LHS.getOperand(1), RHS.getOperand(1),
2560 TheSelect->getOperand(4));
2561
2562 SDOperand Load;
2563 if (LHS.getOpcode() == ISD::LOAD)
2564 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2565 Addr, LHS.getOperand(2));
2566 else
2567 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2568 LHS.getOperand(0), Addr, LHS.getOperand(2),
2569 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2570 // Users of the select now use the result of the load.
2571 CombineTo(TheSelect, Load);
2572
2573 // Users of the old loads now use the new load's chain. We know the
2574 // old-load value is dead now.
2575 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2576 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2577 return true;
2578 }
2579 }
2580
2581 return false;
2582}
2583
Nate Begeman44728a72005-09-19 22:34:01 +00002584SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2585 SDOperand N2, SDOperand N3,
2586 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002587
2588 MVT::ValueType VT = N2.getValueType();
2589 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2590 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2591 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2592 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2593
2594 // Determine if the condition we're dealing with is constant
2595 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2596 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2597
2598 // fold select_cc true, x, y -> x
2599 if (SCCC && SCCC->getValue())
2600 return N2;
2601 // fold select_cc false, x, y -> y
2602 if (SCCC && SCCC->getValue() == 0)
2603 return N3;
2604
2605 // Check to see if we can simplify the select into an fabs node
2606 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2607 // Allow either -0.0 or 0.0
2608 if (CFP->getValue() == 0.0) {
2609 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2610 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2611 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2612 N2 == N3.getOperand(0))
2613 return DAG.getNode(ISD::FABS, VT, N0);
2614
2615 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2616 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2617 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2618 N2.getOperand(0) == N3)
2619 return DAG.getNode(ISD::FABS, VT, N3);
2620 }
2621 }
2622
2623 // Check to see if we can perform the "gzip trick", transforming
2624 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2625 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2626 MVT::isInteger(N0.getValueType()) &&
2627 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2628 MVT::ValueType XType = N0.getValueType();
2629 MVT::ValueType AType = N2.getValueType();
2630 if (XType >= AType) {
2631 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002632 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002633 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2634 unsigned ShCtV = Log2_64(N2C->getValue());
2635 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2636 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2637 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00002638 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002639 if (XType > AType) {
2640 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002641 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002642 }
2643 return DAG.getNode(ISD::AND, AType, Shift, N2);
2644 }
2645 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2646 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2647 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002648 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002649 if (XType > AType) {
2650 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002651 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002652 }
2653 return DAG.getNode(ISD::AND, AType, Shift, N2);
2654 }
2655 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002656
2657 // fold select C, 16, 0 -> shl C, 4
2658 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2659 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2660 // Get a SetCC of the condition
2661 // FIXME: Should probably make sure that setcc is legal if we ever have a
2662 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00002663 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00002664 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00002665 if (AfterLegalize) {
2666 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002667 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00002668 } else {
2669 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002670 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00002671 }
Chris Lattner5750df92006-03-01 04:03:14 +00002672 AddToWorkList(SCC.Val);
2673 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00002674 // shl setcc result by log2 n2c
2675 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2676 DAG.getConstant(Log2_64(N2C->getValue()),
2677 TLI.getShiftAmountTy()));
2678 }
2679
Nate Begemanf845b452005-10-08 00:29:44 +00002680 // Check to see if this is the equivalent of setcc
2681 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2682 // otherwise, go ahead with the folds.
2683 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2684 MVT::ValueType XType = N0.getValueType();
2685 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2686 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2687 if (Res.getValueType() != VT)
2688 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2689 return Res;
2690 }
2691
2692 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2693 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2694 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2695 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2696 return DAG.getNode(ISD::SRL, XType, Ctlz,
2697 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2698 TLI.getShiftAmountTy()));
2699 }
2700 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2701 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2702 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2703 N0);
2704 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2705 DAG.getConstant(~0ULL, XType));
2706 return DAG.getNode(ISD::SRL, XType,
2707 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2708 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2709 TLI.getShiftAmountTy()));
2710 }
2711 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2712 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2713 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2714 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2715 TLI.getShiftAmountTy()));
2716 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2717 }
2718 }
2719
2720 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2721 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2722 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2723 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2724 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2725 MVT::ValueType XType = N0.getValueType();
2726 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2727 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2728 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2729 TLI.getShiftAmountTy()));
2730 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002731 AddToWorkList(Shift.Val);
2732 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002733 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2734 }
2735 }
2736 }
2737
Nate Begeman44728a72005-09-19 22:34:01 +00002738 return SDOperand();
2739}
2740
Nate Begeman452d7be2005-09-16 00:54:12 +00002741SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002742 SDOperand N1, ISD::CondCode Cond,
2743 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002744 // These setcc operations always fold.
2745 switch (Cond) {
2746 default: break;
2747 case ISD::SETFALSE:
2748 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2749 case ISD::SETTRUE:
2750 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2751 }
2752
2753 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2754 uint64_t C1 = N1C->getValue();
2755 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2756 uint64_t C0 = N0C->getValue();
2757
2758 // Sign extend the operands if required
2759 if (ISD::isSignedIntSetCC(Cond)) {
2760 C0 = N0C->getSignExtended();
2761 C1 = N1C->getSignExtended();
2762 }
2763
2764 switch (Cond) {
2765 default: assert(0 && "Unknown integer setcc!");
2766 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2767 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2768 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2769 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2770 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2771 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2772 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2773 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2774 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2775 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2776 }
2777 } else {
2778 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2779 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2780 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2781
2782 // If the comparison constant has bits in the upper part, the
2783 // zero-extended value could never match.
2784 if (C1 & (~0ULL << InSize)) {
2785 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2786 switch (Cond) {
2787 case ISD::SETUGT:
2788 case ISD::SETUGE:
2789 case ISD::SETEQ: return DAG.getConstant(0, VT);
2790 case ISD::SETULT:
2791 case ISD::SETULE:
2792 case ISD::SETNE: return DAG.getConstant(1, VT);
2793 case ISD::SETGT:
2794 case ISD::SETGE:
2795 // True if the sign bit of C1 is set.
2796 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2797 case ISD::SETLT:
2798 case ISD::SETLE:
2799 // True if the sign bit of C1 isn't set.
2800 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2801 default:
2802 break;
2803 }
2804 }
2805
2806 // Otherwise, we can perform the comparison with the low bits.
2807 switch (Cond) {
2808 case ISD::SETEQ:
2809 case ISD::SETNE:
2810 case ISD::SETUGT:
2811 case ISD::SETUGE:
2812 case ISD::SETULT:
2813 case ISD::SETULE:
2814 return DAG.getSetCC(VT, N0.getOperand(0),
2815 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2816 Cond);
2817 default:
2818 break; // todo, be more careful with signed comparisons
2819 }
2820 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2821 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2822 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2823 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2824 MVT::ValueType ExtDstTy = N0.getValueType();
2825 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2826
2827 // If the extended part has any inconsistent bits, it cannot ever
2828 // compare equal. In other words, they have to be all ones or all
2829 // zeros.
2830 uint64_t ExtBits =
2831 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2832 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2833 return DAG.getConstant(Cond == ISD::SETNE, VT);
2834
2835 SDOperand ZextOp;
2836 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2837 if (Op0Ty == ExtSrcTy) {
2838 ZextOp = N0.getOperand(0);
2839 } else {
2840 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2841 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2842 DAG.getConstant(Imm, Op0Ty));
2843 }
Chris Lattner5750df92006-03-01 04:03:14 +00002844 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002845 // Otherwise, make this a use of a zext.
2846 return DAG.getSetCC(VT, ZextOp,
2847 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2848 ExtDstTy),
2849 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002850 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2851 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2852 (N0.getOpcode() == ISD::XOR ||
2853 (N0.getOpcode() == ISD::AND &&
2854 N0.getOperand(0).getOpcode() == ISD::XOR &&
2855 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2856 isa<ConstantSDNode>(N0.getOperand(1)) &&
2857 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2858 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2859 // only do this if the top bits are known zero.
2860 if (TLI.MaskedValueIsZero(N1,
2861 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2862 // Okay, get the un-inverted input value.
2863 SDOperand Val;
2864 if (N0.getOpcode() == ISD::XOR)
2865 Val = N0.getOperand(0);
2866 else {
2867 assert(N0.getOpcode() == ISD::AND &&
2868 N0.getOperand(0).getOpcode() == ISD::XOR);
2869 // ((X^1)&1)^1 -> X & 1
2870 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2871 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2872 }
2873 return DAG.getSetCC(VT, Val, N1,
2874 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2875 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002876 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002877
Nate Begeman452d7be2005-09-16 00:54:12 +00002878 uint64_t MinVal, MaxVal;
2879 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2880 if (ISD::isSignedIntSetCC(Cond)) {
2881 MinVal = 1ULL << (OperandBitSize-1);
2882 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2883 MaxVal = ~0ULL >> (65-OperandBitSize);
2884 else
2885 MaxVal = 0;
2886 } else {
2887 MinVal = 0;
2888 MaxVal = ~0ULL >> (64-OperandBitSize);
2889 }
2890
2891 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2892 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2893 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2894 --C1; // X >= C0 --> X > (C0-1)
2895 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2896 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2897 }
2898
2899 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2900 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2901 ++C1; // X <= C0 --> X < (C0+1)
2902 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2903 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2904 }
2905
2906 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2907 return DAG.getConstant(0, VT); // X < MIN --> false
2908
2909 // Canonicalize setgt X, Min --> setne X, Min
2910 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2911 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002912 // Canonicalize setlt X, Max --> setne X, Max
2913 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2914 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002915
2916 // If we have setult X, 1, turn it into seteq X, 0
2917 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2918 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2919 ISD::SETEQ);
2920 // If we have setugt X, Max-1, turn it into seteq X, Max
2921 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2922 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2923 ISD::SETEQ);
2924
2925 // If we have "setcc X, C0", check to see if we can shrink the immediate
2926 // by changing cc.
2927
2928 // SETUGT X, SINTMAX -> SETLT X, 0
2929 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2930 C1 == (~0ULL >> (65-OperandBitSize)))
2931 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2932 ISD::SETLT);
2933
2934 // FIXME: Implement the rest of these.
2935
2936 // Fold bit comparisons when we can.
2937 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2938 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2939 if (ConstantSDNode *AndRHS =
2940 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2941 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2942 // Perform the xform if the AND RHS is a single bit.
2943 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2944 return DAG.getNode(ISD::SRL, VT, N0,
2945 DAG.getConstant(Log2_64(AndRHS->getValue()),
2946 TLI.getShiftAmountTy()));
2947 }
2948 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2949 // (X & 8) == 8 --> (X & 8) >> 3
2950 // Perform the xform if C1 is a single bit.
2951 if ((C1 & (C1-1)) == 0) {
2952 return DAG.getNode(ISD::SRL, VT, N0,
2953 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2954 }
2955 }
2956 }
2957 }
2958 } else if (isa<ConstantSDNode>(N0.Val)) {
2959 // Ensure that the constant occurs on the RHS.
2960 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2961 }
2962
2963 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2964 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2965 double C0 = N0C->getValue(), C1 = N1C->getValue();
2966
2967 switch (Cond) {
2968 default: break; // FIXME: Implement the rest of these!
2969 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2970 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2971 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2972 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2973 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2974 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2975 }
2976 } else {
2977 // Ensure that the constant occurs on the RHS.
2978 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2979 }
2980
2981 if (N0 == N1) {
2982 // We can always fold X == Y for integer setcc's.
2983 if (MVT::isInteger(N0.getValueType()))
2984 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2985 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2986 if (UOF == 2) // FP operators that are undefined on NaNs.
2987 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2988 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2989 return DAG.getConstant(UOF, VT);
2990 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2991 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002992 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002993 if (NewCond != Cond)
2994 return DAG.getSetCC(VT, N0, N1, NewCond);
2995 }
2996
2997 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2998 MVT::isInteger(N0.getValueType())) {
2999 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3000 N0.getOpcode() == ISD::XOR) {
3001 // Simplify (X+Y) == (X+Z) --> Y == Z
3002 if (N0.getOpcode() == N1.getOpcode()) {
3003 if (N0.getOperand(0) == N1.getOperand(0))
3004 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3005 if (N0.getOperand(1) == N1.getOperand(1))
3006 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3007 if (isCommutativeBinOp(N0.getOpcode())) {
3008 // If X op Y == Y op X, try other combinations.
3009 if (N0.getOperand(0) == N1.getOperand(1))
3010 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3011 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003012 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003013 }
3014 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003015
3016 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3017 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3018 // Turn (X+C1) == C2 --> X == C2-C1
3019 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3020 return DAG.getSetCC(VT, N0.getOperand(0),
3021 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3022 N0.getValueType()), Cond);
3023 }
3024
3025 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3026 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003027 // If we know that all of the inverted bits are zero, don't bother
3028 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003029 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003030 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003031 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003032 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003033 }
3034
3035 // Turn (C1-X) == C2 --> X == C1-C2
3036 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3037 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3038 return DAG.getSetCC(VT, N0.getOperand(1),
3039 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3040 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003041 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003042 }
3043 }
3044
Nate Begeman452d7be2005-09-16 00:54:12 +00003045 // Simplify (X+Z) == X --> Z == 0
3046 if (N0.getOperand(0) == N1)
3047 return DAG.getSetCC(VT, N0.getOperand(1),
3048 DAG.getConstant(0, N0.getValueType()), Cond);
3049 if (N0.getOperand(1) == N1) {
3050 if (isCommutativeBinOp(N0.getOpcode()))
3051 return DAG.getSetCC(VT, N0.getOperand(0),
3052 DAG.getConstant(0, N0.getValueType()), Cond);
3053 else {
3054 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3055 // (Z-X) == X --> Z == X<<1
3056 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3057 N1,
3058 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003059 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003060 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3061 }
3062 }
3063 }
3064
3065 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3066 N1.getOpcode() == ISD::XOR) {
3067 // Simplify X == (X+Z) --> Z == 0
3068 if (N1.getOperand(0) == N0) {
3069 return DAG.getSetCC(VT, N1.getOperand(1),
3070 DAG.getConstant(0, N1.getValueType()), Cond);
3071 } else if (N1.getOperand(1) == N0) {
3072 if (isCommutativeBinOp(N1.getOpcode())) {
3073 return DAG.getSetCC(VT, N1.getOperand(0),
3074 DAG.getConstant(0, N1.getValueType()), Cond);
3075 } else {
3076 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3077 // X == (Z-X) --> X<<1 == Z
3078 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3079 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003080 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003081 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3082 }
3083 }
3084 }
3085 }
3086
3087 // Fold away ALL boolean setcc's.
3088 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003089 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003090 switch (Cond) {
3091 default: assert(0 && "Unknown integer setcc!");
3092 case ISD::SETEQ: // X == Y -> (X^Y)^1
3093 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3094 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003095 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003096 break;
3097 case ISD::SETNE: // X != Y --> (X^Y)
3098 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3099 break;
3100 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3101 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3102 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3103 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003104 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003105 break;
3106 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3107 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3108 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3109 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003110 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003111 break;
3112 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3113 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3114 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3115 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003116 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003117 break;
3118 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3119 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3120 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3121 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3122 break;
3123 }
3124 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003125 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003126 // FIXME: If running after legalize, we probably can't do this.
3127 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3128 }
3129 return N0;
3130 }
3131
3132 // Could not fold it.
3133 return SDOperand();
3134}
3135
Nate Begeman69575232005-10-20 02:15:44 +00003136/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3137/// return a DAG expression to select that will generate the same value by
3138/// multiplying by a magic number. See:
3139/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3140SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
3141 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003142
3143 // Check to see if we can do this.
3144 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3145 return SDOperand(); // BuildSDIV only operates on i32 or i64
3146 if (!TLI.isOperationLegal(ISD::MULHS, VT))
3147 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00003148
Nate Begemanc6a454e2005-10-20 17:45:03 +00003149 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00003150 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
3151
3152 // Multiply the numerator (operand 0) by the magic value
3153 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
3154 DAG.getConstant(magics.m, VT));
3155 // If d > 0 and m < 0, add the numerator
3156 if (d > 0 && magics.m < 0) {
3157 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003158 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003159 }
3160 // If d < 0 and m > 0, subtract the numerator.
3161 if (d < 0 && magics.m > 0) {
3162 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003163 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003164 }
3165 // Shift right algebraic if shift value is nonzero
3166 if (magics.s > 0) {
3167 Q = DAG.getNode(ISD::SRA, VT, Q,
3168 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003169 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003170 }
3171 // Extract the sign bit and add it to the quotient
3172 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00003173 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
3174 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003175 AddToWorkList(T.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003176 return DAG.getNode(ISD::ADD, VT, Q, T);
3177}
3178
3179/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3180/// return a DAG expression to select that will generate the same value by
3181/// multiplying by a magic number. See:
3182/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3183SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3184 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003185
3186 // Check to see if we can do this.
3187 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3188 return SDOperand(); // BuildUDIV only operates on i32 or i64
3189 if (!TLI.isOperationLegal(ISD::MULHU, VT))
3190 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00003191
3192 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
3193 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
3194
3195 // Multiply the numerator (operand 0) by the magic value
3196 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
3197 DAG.getConstant(magics.m, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00003198 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003199
3200 if (magics.a == 0) {
3201 return DAG.getNode(ISD::SRL, VT, Q,
3202 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3203 } else {
3204 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003205 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003206 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
3207 DAG.getConstant(1, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003208 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003209 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003210 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003211 return DAG.getNode(ISD::SRL, VT, NPQ,
3212 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
3213 }
3214}
3215
Nate Begeman1d4d4142005-09-01 00:19:25 +00003216// SelectionDAG::Combine - This is the entry point for the file.
3217//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003218void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003219 /// run - This is the main entry point to this class.
3220 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003221 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003222}