blob: 424942b3e9b7fbdf8a267336be47f9edf6ce9361 [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 Lattner87514ca2005-10-10 22:31:19 +0000211 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000212 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
213 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000214
Nate Begemancd4d58c2006-02-03 06:46:56 +0000215 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
216
Chris Lattner40c62d52005-10-18 06:04:22 +0000217 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000218 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
219 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
220 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000221 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000222 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000223
224 SDOperand BuildSDIV(SDNode *N);
225 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000226public:
227 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000228 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000229
230 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000231 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000232 };
233}
234
Chris Lattner24664722006-03-01 04:53:38 +0000235//===----------------------------------------------------------------------===//
236// TargetLowering::DAGCombinerInfo implementation
237//===----------------------------------------------------------------------===//
238
239void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
240 ((DAGCombiner*)DC)->AddToWorkList(N);
241}
242
243SDOperand TargetLowering::DAGCombinerInfo::
244CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
245 return ((DAGCombiner*)DC)->CombineTo(N, To);
246}
247
248SDOperand TargetLowering::DAGCombinerInfo::
249CombineTo(SDNode *N, SDOperand Res) {
250 return ((DAGCombiner*)DC)->CombineTo(N, Res);
251}
252
253
254SDOperand TargetLowering::DAGCombinerInfo::
255CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
256 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
257}
258
259
260
261
262//===----------------------------------------------------------------------===//
263
264
Nate Begeman69575232005-10-20 02:15:44 +0000265struct ms {
266 int64_t m; // magic number
267 int64_t s; // shift amount
268};
269
270struct mu {
271 uint64_t m; // magic number
272 int64_t a; // add indicator
273 int64_t s; // shift amount
274};
275
276/// magic - calculate the magic numbers required to codegen an integer sdiv as
277/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
278/// or -1.
279static ms magic32(int32_t d) {
280 int32_t p;
281 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
282 const uint32_t two31 = 0x80000000U;
283 struct ms mag;
284
285 ad = abs(d);
286 t = two31 + ((uint32_t)d >> 31);
287 anc = t - 1 - t%ad; // absolute value of nc
288 p = 31; // initialize p
289 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
290 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
291 q2 = two31/ad; // initialize q2 = 2p/abs(d)
292 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
293 do {
294 p = p + 1;
295 q1 = 2*q1; // update q1 = 2p/abs(nc)
296 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
297 if (r1 >= anc) { // must be unsigned comparison
298 q1 = q1 + 1;
299 r1 = r1 - anc;
300 }
301 q2 = 2*q2; // update q2 = 2p/abs(d)
302 r2 = 2*r2; // update r2 = rem(2p/abs(d))
303 if (r2 >= ad) { // must be unsigned comparison
304 q2 = q2 + 1;
305 r2 = r2 - ad;
306 }
307 delta = ad - r2;
308 } while (q1 < delta || (q1 == delta && r1 == 0));
309
310 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
311 if (d < 0) mag.m = -mag.m; // resulting magic number
312 mag.s = p - 32; // resulting shift
313 return mag;
314}
315
316/// magicu - calculate the magic numbers required to codegen an integer udiv as
317/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
318static mu magicu32(uint32_t d) {
319 int32_t p;
320 uint32_t nc, delta, q1, r1, q2, r2;
321 struct mu magu;
322 magu.a = 0; // initialize "add" indicator
323 nc = - 1 - (-d)%d;
324 p = 31; // initialize p
325 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
326 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
327 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
328 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
329 do {
330 p = p + 1;
331 if (r1 >= nc - r1 ) {
332 q1 = 2*q1 + 1; // update q1
333 r1 = 2*r1 - nc; // update r1
334 }
335 else {
336 q1 = 2*q1; // update q1
337 r1 = 2*r1; // update r1
338 }
339 if (r2 + 1 >= d - r2) {
340 if (q2 >= 0x7FFFFFFF) magu.a = 1;
341 q2 = 2*q2 + 1; // update q2
342 r2 = 2*r2 + 1 - d; // update r2
343 }
344 else {
345 if (q2 >= 0x80000000) magu.a = 1;
346 q2 = 2*q2; // update q2
347 r2 = 2*r2 + 1; // update r2
348 }
349 delta = d - 1 - r2;
350 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
351 magu.m = q2 + 1; // resulting magic number
352 magu.s = p - 32; // resulting shift
353 return magu;
354}
355
356/// magic - calculate the magic numbers required to codegen an integer sdiv as
357/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
358/// or -1.
359static ms magic64(int64_t d) {
360 int64_t p;
361 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
362 const uint64_t two63 = 9223372036854775808ULL; // 2^63
363 struct ms mag;
364
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000365 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000366 t = two63 + ((uint64_t)d >> 63);
367 anc = t - 1 - t%ad; // absolute value of nc
368 p = 63; // initialize p
369 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
370 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
371 q2 = two63/ad; // initialize q2 = 2p/abs(d)
372 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
373 do {
374 p = p + 1;
375 q1 = 2*q1; // update q1 = 2p/abs(nc)
376 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
377 if (r1 >= anc) { // must be unsigned comparison
378 q1 = q1 + 1;
379 r1 = r1 - anc;
380 }
381 q2 = 2*q2; // update q2 = 2p/abs(d)
382 r2 = 2*r2; // update r2 = rem(2p/abs(d))
383 if (r2 >= ad) { // must be unsigned comparison
384 q2 = q2 + 1;
385 r2 = r2 - ad;
386 }
387 delta = ad - r2;
388 } while (q1 < delta || (q1 == delta && r1 == 0));
389
390 mag.m = q2 + 1;
391 if (d < 0) mag.m = -mag.m; // resulting magic number
392 mag.s = p - 64; // resulting shift
393 return mag;
394}
395
396/// magicu - calculate the magic numbers required to codegen an integer udiv as
397/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
398static mu magicu64(uint64_t d)
399{
400 int64_t p;
401 uint64_t nc, delta, q1, r1, q2, r2;
402 struct mu magu;
403 magu.a = 0; // initialize "add" indicator
404 nc = - 1 - (-d)%d;
405 p = 63; // initialize p
406 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
407 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
408 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
409 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
410 do {
411 p = p + 1;
412 if (r1 >= nc - r1 ) {
413 q1 = 2*q1 + 1; // update q1
414 r1 = 2*r1 - nc; // update r1
415 }
416 else {
417 q1 = 2*q1; // update q1
418 r1 = 2*r1; // update r1
419 }
420 if (r2 + 1 >= d - r2) {
421 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
422 q2 = 2*q2 + 1; // update q2
423 r2 = 2*r2 + 1 - d; // update r2
424 }
425 else {
426 if (q2 >= 0x8000000000000000ull) magu.a = 1;
427 q2 = 2*q2; // update q2
428 r2 = 2*r2 + 1; // update r2
429 }
430 delta = d - 1 - r2;
431 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
432 magu.m = q2 + 1; // resulting magic number
433 magu.s = p - 64; // resulting shift
434 return magu;
435}
436
Nate Begeman4ebd8052005-09-01 23:24:04 +0000437// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
438// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000439// Also, set the incoming LHS, RHS, and CC references to the appropriate
440// nodes based on the type of node we are checking. This simplifies life a
441// bit for the callers.
442static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
443 SDOperand &CC) {
444 if (N.getOpcode() == ISD::SETCC) {
445 LHS = N.getOperand(0);
446 RHS = N.getOperand(1);
447 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000448 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000449 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000450 if (N.getOpcode() == ISD::SELECT_CC &&
451 N.getOperand(2).getOpcode() == ISD::Constant &&
452 N.getOperand(3).getOpcode() == ISD::Constant &&
453 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000454 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
455 LHS = N.getOperand(0);
456 RHS = N.getOperand(1);
457 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000458 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000459 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000460 return false;
461}
462
Nate Begeman99801192005-09-07 23:25:52 +0000463// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
464// one use. If this is true, it allows the users to invert the operation for
465// free when it is profitable to do so.
466static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000467 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000468 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000469 return true;
470 return false;
471}
472
Nate Begeman452d7be2005-09-16 00:54:12 +0000473// FIXME: This should probably go in the ISD class rather than being duplicated
474// in several files.
475static bool isCommutativeBinOp(unsigned Opcode) {
476 switch (Opcode) {
477 case ISD::ADD:
478 case ISD::MUL:
479 case ISD::AND:
480 case ISD::OR:
481 case ISD::XOR: return true;
482 default: return false; // FIXME: Need commutative info for user ops!
483 }
484}
485
Nate Begemancd4d58c2006-02-03 06:46:56 +0000486SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
487 MVT::ValueType VT = N0.getValueType();
488 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
489 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
490 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
491 if (isa<ConstantSDNode>(N1)) {
492 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000493 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000494 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
495 } else if (N0.hasOneUse()) {
496 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000497 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000498 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
499 }
500 }
501 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
502 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
503 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
504 if (isa<ConstantSDNode>(N0)) {
505 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000506 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000507 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
508 } else if (N1.hasOneUse()) {
509 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000510 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000511 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
512 }
513 }
514 return SDOperand();
515}
516
Nate Begeman4ebd8052005-09-01 23:24:04 +0000517void DAGCombiner::Run(bool RunningAfterLegalize) {
518 // set the instance variable, so that the various visit routines may use it.
519 AfterLegalize = RunningAfterLegalize;
520
Nate Begeman646d7e22005-09-02 21:18:40 +0000521 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000522 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
523 E = DAG.allnodes_end(); I != E; ++I)
524 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000525
Chris Lattner95038592005-10-05 06:35:28 +0000526 // Create a dummy node (which is not added to allnodes), that adds a reference
527 // to the root node, preventing it from being deleted, and tracking any
528 // changes of the root.
529 HandleSDNode Dummy(DAG.getRoot());
530
Chris Lattner24664722006-03-01 04:53:38 +0000531
532 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
533 TargetLowering::DAGCombinerInfo
534 DagCombineInfo(DAG, !RunningAfterLegalize, this);
535
Nate Begeman1d4d4142005-09-01 00:19:25 +0000536 // while the worklist isn't empty, inspect the node on the end of it and
537 // try and combine it.
538 while (!WorkList.empty()) {
539 SDNode *N = WorkList.back();
540 WorkList.pop_back();
541
542 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000543 // N is deleted from the DAG, since they too may now be dead or may have a
544 // reduced number of uses, allowing other xforms.
545 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000546 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
547 WorkList.push_back(N->getOperand(i).Val);
548
Nate Begeman1d4d4142005-09-01 00:19:25 +0000549 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000550 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000551 continue;
552 }
553
Nate Begeman83e75ec2005-09-06 04:43:02 +0000554 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000555
556 // If nothing happened, try a target-specific DAG combine.
557 if (RV.Val == 0) {
558 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
559 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
560 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
561 }
562
Nate Begeman83e75ec2005-09-06 04:43:02 +0000563 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000564 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000565 // If we get back the same node we passed in, rather than a new node or
566 // zero, we know that the node must have defined multiple values and
567 // CombineTo was used. Since CombineTo takes care of the worklist
568 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000569 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000570 DEBUG(std::cerr << "\nReplacing "; N->dump();
571 std::cerr << "\nWith: "; RV.Val->dump();
572 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000573 std::vector<SDNode*> NowDead;
574 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000575
576 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000577 WorkList.push_back(RV.Val);
578 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000579
580 // Nodes can end up on the worklist more than once. Make sure we do
581 // not process a node that has been replaced.
582 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000583 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
584 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000585
586 // Finally, since the node is now dead, remove it from the graph.
587 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000588 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000589 }
590 }
Chris Lattner95038592005-10-05 06:35:28 +0000591
592 // If the root changed (e.g. it was a dead load, update the root).
593 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000594}
595
Nate Begeman83e75ec2005-09-06 04:43:02 +0000596SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597 switch(N->getOpcode()) {
598 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000599 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000600 case ISD::ADD: return visitADD(N);
601 case ISD::SUB: return visitSUB(N);
602 case ISD::MUL: return visitMUL(N);
603 case ISD::SDIV: return visitSDIV(N);
604 case ISD::UDIV: return visitUDIV(N);
605 case ISD::SREM: return visitSREM(N);
606 case ISD::UREM: return visitUREM(N);
607 case ISD::MULHU: return visitMULHU(N);
608 case ISD::MULHS: return visitMULHS(N);
609 case ISD::AND: return visitAND(N);
610 case ISD::OR: return visitOR(N);
611 case ISD::XOR: return visitXOR(N);
612 case ISD::SHL: return visitSHL(N);
613 case ISD::SRA: return visitSRA(N);
614 case ISD::SRL: return visitSRL(N);
615 case ISD::CTLZ: return visitCTLZ(N);
616 case ISD::CTTZ: return visitCTTZ(N);
617 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000618 case ISD::SELECT: return visitSELECT(N);
619 case ISD::SELECT_CC: return visitSELECT_CC(N);
620 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000621 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
622 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
623 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
624 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000625 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000626 case ISD::FADD: return visitFADD(N);
627 case ISD::FSUB: return visitFSUB(N);
628 case ISD::FMUL: return visitFMUL(N);
629 case ISD::FDIV: return visitFDIV(N);
630 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000631 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000632 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
633 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
634 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
635 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
636 case ISD::FP_ROUND: return visitFP_ROUND(N);
637 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
638 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
639 case ISD::FNEG: return visitFNEG(N);
640 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000641 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000642 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000643 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000644 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000645 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
646 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000647 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000648 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000649}
650
Nate Begeman83e75ec2005-09-06 04:43:02 +0000651SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000652 std::vector<SDOperand> Ops;
653 bool Changed = false;
654
Nate Begeman1d4d4142005-09-01 00:19:25 +0000655 // If the token factor has two operands and one is the entry token, replace
656 // the token factor with the other operand.
657 if (N->getNumOperands() == 2) {
658 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000659 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000660 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000661 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000662 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000663
Nate Begemanded49632005-10-13 03:11:28 +0000664 // fold (tokenfactor (tokenfactor)) -> tokenfactor
665 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
666 SDOperand Op = N->getOperand(i);
667 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000668 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000669 Changed = true;
670 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
671 Ops.push_back(Op.getOperand(j));
672 } else {
673 Ops.push_back(Op);
674 }
675 }
676 if (Changed)
677 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000678 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679}
680
Nate Begeman83e75ec2005-09-06 04:43:02 +0000681SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000682 SDOperand N0 = N->getOperand(0);
683 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000684 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
685 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000686 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687
688 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000689 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000690 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000691 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000692 if (N0C && !N1C)
693 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000694 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000695 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000696 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000697 // fold ((c1-A)+c2) -> (c1+c2)-A
698 if (N1C && N0.getOpcode() == ISD::SUB)
699 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
700 return DAG.getNode(ISD::SUB, VT,
701 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
702 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000703 // reassociate add
704 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
705 if (RADD.Val != 0)
706 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000707 // fold ((0-A) + B) -> B-A
708 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
709 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000710 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000711 // fold (A + (0-B)) -> A-B
712 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
713 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000714 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000715 // fold (A+(B-A)) -> B
716 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000717 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000718
Evan Cheng860771d2006-03-01 01:09:54 +0000719 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemanb0d04a72006-02-18 02:40:58 +0000720 return SDOperand();
Chris Lattner947c2892006-03-13 06:51:27 +0000721
722 // fold (a+b) -> (a|b) iff a and b share no bits.
723 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
724 uint64_t LHSZero, LHSOne;
725 uint64_t RHSZero, RHSOne;
726 uint64_t Mask = MVT::getIntVTBitMask(VT);
727 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
728 if (LHSZero) {
729 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
730
731 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
732 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
733 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
734 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
735 return DAG.getNode(ISD::OR, VT, N0, N1);
736 }
737 }
738
Nate Begeman83e75ec2005-09-06 04:43:02 +0000739 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000740}
741
Nate Begeman83e75ec2005-09-06 04:43:02 +0000742SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000743 SDOperand N0 = N->getOperand(0);
744 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000745 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
746 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000747 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000748
Chris Lattner854077d2005-10-17 01:07:11 +0000749 // fold (sub x, x) -> 0
750 if (N0 == N1)
751 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000752 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000753 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000754 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000755 // fold (sub x, c) -> (add x, -c)
756 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000757 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000758 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000759 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000760 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000761 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000762 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000763 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000764 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765}
766
Nate Begeman83e75ec2005-09-06 04:43:02 +0000767SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000768 SDOperand N0 = N->getOperand(0);
769 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000770 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
771 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000772 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000773
774 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000775 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000776 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000777 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000778 if (N0C && !N1C)
779 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000780 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000781 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000782 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000783 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000784 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000785 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000786 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000787 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000788 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000789 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000790 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000791 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
792 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
793 // FIXME: If the input is something that is easily negated (e.g. a
794 // single-use add), we should put the negate there.
795 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
796 DAG.getNode(ISD::SHL, VT, N0,
797 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
798 TLI.getShiftAmountTy())));
799 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000800
801 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
802 if (N1C && N0.getOpcode() == ISD::SHL &&
803 isa<ConstantSDNode>(N0.getOperand(1))) {
804 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000805 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000806 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
807 }
808
809 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
810 // use.
811 {
812 SDOperand Sh(0,0), Y(0,0);
813 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
814 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
815 N0.Val->hasOneUse()) {
816 Sh = N0; Y = N1;
817 } else if (N1.getOpcode() == ISD::SHL &&
818 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
819 Sh = N1; Y = N0;
820 }
821 if (Sh.Val) {
822 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
823 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
824 }
825 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000826 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
827 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
828 isa<ConstantSDNode>(N0.getOperand(1))) {
829 return DAG.getNode(ISD::ADD, VT,
830 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
831 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
832 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000833
Nate Begemancd4d58c2006-02-03 06:46:56 +0000834 // reassociate mul
835 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
836 if (RMUL.Val != 0)
837 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000838 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000839}
840
Nate Begeman83e75ec2005-09-06 04:43:02 +0000841SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842 SDOperand N0 = N->getOperand(0);
843 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000844 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
845 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000846 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000847
848 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000849 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000850 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000851 // fold (sdiv X, 1) -> X
852 if (N1C && N1C->getSignExtended() == 1LL)
853 return N0;
854 // fold (sdiv X, -1) -> 0-X
855 if (N1C && N1C->isAllOnesValue())
856 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000857 // If we know the sign bits of both operands are zero, strength reduce to a
858 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
859 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000860 if (TLI.MaskedValueIsZero(N1, SignBit) &&
861 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000862 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000863 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000864 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000865 (isPowerOf2_64(N1C->getSignExtended()) ||
866 isPowerOf2_64(-N1C->getSignExtended()))) {
867 // If dividing by powers of two is cheap, then don't perform the following
868 // fold.
869 if (TLI.isPow2DivCheap())
870 return SDOperand();
871 int64_t pow2 = N1C->getSignExtended();
872 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000873 unsigned lg2 = Log2_64(abs2);
874 // Splat the sign bit into the register
875 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000876 DAG.getConstant(MVT::getSizeInBits(VT)-1,
877 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000878 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000879 // Add (N0 < 0) ? abs2 - 1 : 0;
880 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
881 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000882 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000883 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000884 AddToWorkList(SRL.Val);
885 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000886 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
887 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000888 // If we're dividing by a positive value, we're done. Otherwise, we must
889 // negate the result.
890 if (pow2 > 0)
891 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000892 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000893 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
894 }
Nate Begeman69575232005-10-20 02:15:44 +0000895 // if integer divide is expensive and we satisfy the requirements, emit an
896 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000897 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000898 !TLI.isIntDivCheap()) {
899 SDOperand Op = BuildSDIV(N);
900 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000901 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000902 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000903}
904
Nate Begeman83e75ec2005-09-06 04:43:02 +0000905SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000906 SDOperand N0 = N->getOperand(0);
907 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000908 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
909 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000910 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911
912 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000913 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000914 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000915 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000916 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000917 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000918 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000919 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000920 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
921 if (N1.getOpcode() == ISD::SHL) {
922 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
923 if (isPowerOf2_64(SHC->getValue())) {
924 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000925 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
926 DAG.getConstant(Log2_64(SHC->getValue()),
927 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000928 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000929 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000930 }
931 }
932 }
Nate Begeman69575232005-10-20 02:15:44 +0000933 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000934 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
935 SDOperand Op = BuildUDIV(N);
936 if (Op.Val) return Op;
937 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000938 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000939}
940
Nate Begeman83e75ec2005-09-06 04:43:02 +0000941SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942 SDOperand N0 = N->getOperand(0);
943 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000944 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
945 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000946 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000947
948 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000949 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000950 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000951 // If we know the sign bits of both operands are zero, strength reduce to a
952 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
953 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000954 if (TLI.MaskedValueIsZero(N1, SignBit) &&
955 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000956 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000957 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000958}
959
Nate Begeman83e75ec2005-09-06 04:43:02 +0000960SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000961 SDOperand N0 = N->getOperand(0);
962 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000963 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
964 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000965 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000966
967 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000968 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000969 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000970 // fold (urem x, pow2) -> (and x, pow2-1)
971 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000972 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000973 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
974 if (N1.getOpcode() == ISD::SHL) {
975 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
976 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000977 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000978 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000979 return DAG.getNode(ISD::AND, VT, N0, Add);
980 }
981 }
982 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000983 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000984}
985
Nate Begeman83e75ec2005-09-06 04:43:02 +0000986SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000987 SDOperand N0 = N->getOperand(0);
988 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000989 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000990
991 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000992 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000993 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000995 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000996 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
997 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000998 TLI.getShiftAmountTy()));
999 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000}
1001
Nate Begeman83e75ec2005-09-06 04:43:02 +00001002SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001003 SDOperand N0 = N->getOperand(0);
1004 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001005 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006
1007 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001008 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001009 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001011 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001012 return DAG.getConstant(0, N0.getValueType());
1013 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014}
1015
Nate Begeman83e75ec2005-09-06 04:43:02 +00001016SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017 SDOperand N0 = N->getOperand(0);
1018 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001019 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001020 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1021 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001022 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +00001023 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001024
1025 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001026 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001027 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001028 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001029 if (N0C && !N1C)
1030 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001031 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001032 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001033 return N0;
1034 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001035 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001036 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001037 // reassociate and
1038 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1039 if (RAND.Val != 0)
1040 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001041 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001042 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001043 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001044 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001045 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001046 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1047 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001048 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001049 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001050 ~N1C->getValue() & InMask)) {
1051 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1052 N0.getOperand(0));
1053
1054 // Replace uses of the AND with uses of the Zero extend node.
1055 CombineTo(N, Zext);
1056
Chris Lattner3603cd62006-02-02 07:17:31 +00001057 // We actually want to replace all uses of the any_extend with the
1058 // zero_extend, to avoid duplicating things. This will later cause this
1059 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001060 CombineTo(N0.Val, Zext);
Chris Lattner3603cd62006-02-02 07:17:31 +00001061 return SDOperand();
1062 }
1063 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001064 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1065 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1066 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1067 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1068
1069 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1070 MVT::isInteger(LL.getValueType())) {
1071 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1072 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1073 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001074 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001075 return DAG.getSetCC(VT, ORNode, LR, Op1);
1076 }
1077 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1078 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1079 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001080 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001081 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1082 }
1083 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1084 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1085 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001086 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001087 return DAG.getSetCC(VT, ORNode, LR, Op1);
1088 }
1089 }
1090 // canonicalize equivalent to ll == rl
1091 if (LL == RR && LR == RL) {
1092 Op1 = ISD::getSetCCSwappedOperands(Op1);
1093 std::swap(RL, RR);
1094 }
1095 if (LL == RL && LR == RR) {
1096 bool isInteger = MVT::isInteger(LL.getValueType());
1097 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1098 if (Result != ISD::SETCC_INVALID)
1099 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1100 }
1101 }
1102 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1103 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1104 N1.getOpcode() == ISD::ZERO_EXTEND &&
1105 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1106 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1107 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001108 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001109 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1110 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001111 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001112 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001113 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1114 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001115 N0.getOperand(1) == N1.getOperand(1)) {
1116 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1117 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001118 AddToWorkList(ANDNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001119 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1120 }
Nate Begemande996292006-02-03 22:24:05 +00001121 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1122 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001123 if (!MVT::isVector(VT) &&
1124 SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001125 return SDOperand();
Nate Begemanded49632005-10-13 03:11:28 +00001126 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001127 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001128 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001129 // If we zero all the possible extended bits, then we can turn this into
1130 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001131 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001132 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001133 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1134 N0.getOperand(1), N0.getOperand(2),
1135 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001136 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001137 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001138 return SDOperand();
1139 }
1140 }
1141 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001142 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001143 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001144 // If we zero all the possible extended bits, then we can turn this into
1145 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001146 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001147 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001148 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1149 N0.getOperand(1), N0.getOperand(2),
1150 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001151 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001152 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001153 return SDOperand();
1154 }
1155 }
Chris Lattner15045b62006-02-28 06:35:35 +00001156
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001157 // fold (and (load x), 255) -> (zextload x, i8)
1158 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1159 if (N1C &&
1160 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1161 N0.getOpcode() == ISD::ZEXTLOAD) &&
1162 N0.hasOneUse()) {
1163 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001164 if (N1C->getValue() == 255)
1165 EVT = MVT::i8;
1166 else if (N1C->getValue() == 65535)
1167 EVT = MVT::i16;
1168 else if (N1C->getValue() == ~0U)
1169 EVT = MVT::i32;
1170 else
1171 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001172
1173 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1174 cast<VTSDNode>(N0.getOperand(3))->getVT();
1175 if (EVT != MVT::Other && LoadedVT > EVT) {
Chris Lattner15045b62006-02-28 06:35:35 +00001176 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1177 // For big endian targets, we need to add an offset to the pointer to load
1178 // the correct bytes. For little endian systems, we merely need to read
1179 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001180 unsigned PtrOff =
1181 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1182 SDOperand NewPtr = N0.getOperand(1);
1183 if (!TLI.isLittleEndian())
1184 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1185 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001186 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001187 SDOperand Load =
1188 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1189 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001190 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001191 CombineTo(N0.Val, Load, Load.getValue(1));
1192 return SDOperand();
1193 }
1194 }
1195
Nate Begeman83e75ec2005-09-06 04:43:02 +00001196 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001197}
1198
Nate Begeman83e75ec2005-09-06 04:43:02 +00001199SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001200 SDOperand N0 = N->getOperand(0);
1201 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001202 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001203 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1204 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001205 MVT::ValueType VT = N1.getValueType();
1206 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001207
1208 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001209 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001210 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001211 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001212 if (N0C && !N1C)
1213 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001215 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001216 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001218 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001219 return N1;
1220 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001221 if (N1C &&
1222 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001223 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001224 // reassociate or
1225 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1226 if (ROR.Val != 0)
1227 return ROR;
1228 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1229 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001230 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001231 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1232 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1233 N1),
1234 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001235 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001236 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1237 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1238 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1239 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1240
1241 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1242 MVT::isInteger(LL.getValueType())) {
1243 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1244 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1245 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1246 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1247 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001248 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001249 return DAG.getSetCC(VT, ORNode, LR, Op1);
1250 }
1251 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1252 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1253 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1254 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1255 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001256 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001257 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1258 }
1259 }
1260 // canonicalize equivalent to ll == rl
1261 if (LL == RR && LR == RL) {
1262 Op1 = ISD::getSetCCSwappedOperands(Op1);
1263 std::swap(RL, RR);
1264 }
1265 if (LL == RL && LR == RR) {
1266 bool isInteger = MVT::isInteger(LL.getValueType());
1267 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1268 if (Result != ISD::SETCC_INVALID)
1269 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1270 }
1271 }
1272 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1273 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1274 N1.getOpcode() == ISD::ZERO_EXTEND &&
1275 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1276 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1277 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001278 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001279 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1280 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001281 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1282 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1283 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1284 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1285 N0.getOperand(1) == N1.getOperand(1)) {
1286 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1287 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001288 AddToWorkList(ORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001289 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1290 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001291 // canonicalize shl to left side in a shl/srl pair, to match rotate
1292 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1293 std::swap(N0, N1);
1294 // check for rotl, rotr
1295 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1296 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001297 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001298 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1299 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1300 N1.getOperand(1).getOpcode() == ISD::Constant) {
1301 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1302 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1303 if ((c1val + c2val) == OpSizeInBits)
1304 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1305 }
1306 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1307 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1308 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1309 if (ConstantSDNode *SUBC =
1310 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1311 if (SUBC->getValue() == OpSizeInBits)
1312 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1313 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1314 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1315 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1316 if (ConstantSDNode *SUBC =
1317 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1318 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001319 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001320 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1321 N1.getOperand(1));
1322 else
1323 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1324 N0.getOperand(1));
1325 }
1326 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001327 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001328}
1329
Nate Begeman83e75ec2005-09-06 04:43:02 +00001330SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001331 SDOperand N0 = N->getOperand(0);
1332 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001333 SDOperand LHS, RHS, CC;
1334 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1335 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001336 MVT::ValueType VT = N0.getValueType();
1337
1338 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001339 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001340 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001341 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001342 if (N0C && !N1C)
1343 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001344 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001345 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001346 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001347 // reassociate xor
1348 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1349 if (RXOR.Val != 0)
1350 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001351 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001352 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1353 bool isInt = MVT::isInteger(LHS.getValueType());
1354 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1355 isInt);
1356 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001357 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001358 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001359 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001360 assert(0 && "Unhandled SetCC Equivalent!");
1361 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001362 }
Nate Begeman99801192005-09-07 23:25:52 +00001363 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1364 if (N1C && N1C->getValue() == 1 &&
1365 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001366 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001367 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1368 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1370 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001371 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001372 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001373 }
1374 }
Nate Begeman99801192005-09-07 23:25:52 +00001375 // fold !(x or y) -> (!x and !y) iff x or y are constants
1376 if (N1C && N1C->isAllOnesValue() &&
1377 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001379 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1380 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1382 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001383 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001384 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385 }
1386 }
Nate Begeman223df222005-09-08 20:18:10 +00001387 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1388 if (N1C && N0.getOpcode() == ISD::XOR) {
1389 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1390 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1391 if (N00C)
1392 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1393 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1394 if (N01C)
1395 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1396 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1397 }
1398 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001399 if (N0 == N1) {
1400 if (!MVT::isVector(VT)) {
1401 return DAG.getConstant(0, VT);
1402 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1403 // Produce a vector of zeros.
1404 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1405 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1406 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
1407 }
1408 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001409 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1410 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1411 N1.getOpcode() == ISD::ZERO_EXTEND &&
1412 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1413 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1414 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001415 AddToWorkList(XORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001416 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1417 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001418 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1419 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1420 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1421 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1422 N0.getOperand(1) == N1.getOperand(1)) {
1423 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1424 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001425 AddToWorkList(XORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001426 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1427 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001428 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429}
1430
Nate Begeman83e75ec2005-09-06 04:43:02 +00001431SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001432 SDOperand N0 = N->getOperand(0);
1433 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001434 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1435 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001436 MVT::ValueType VT = N0.getValueType();
1437 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1438
1439 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001440 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001441 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001442 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001443 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001444 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001445 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001446 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001447 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001449 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001450 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001452 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001453 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001454 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001455 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001457 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458 N0.getOperand(1).getOpcode() == ISD::Constant) {
1459 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001460 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001462 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001463 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001464 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001465 }
1466 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1467 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001468 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001469 N0.getOperand(1).getOpcode() == ISD::Constant) {
1470 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001471 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001472 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1473 DAG.getConstant(~0ULL << c1, VT));
1474 if (c2 > c1)
1475 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001476 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001477 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001478 return DAG.getNode(ISD::SRL, VT, Mask,
1479 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480 }
1481 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001482 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001483 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001484 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001485 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1486 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1487 isa<ConstantSDNode>(N0.getOperand(1))) {
1488 return DAG.getNode(ISD::ADD, VT,
1489 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1490 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1491 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001492 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001493}
1494
Nate Begeman83e75ec2005-09-06 04:43:02 +00001495SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001496 SDOperand N0 = N->getOperand(0);
1497 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001498 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1499 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001500 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001501
1502 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001503 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001504 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001505 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001506 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001507 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001508 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001509 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001510 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001511 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001512 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001513 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001514 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001515 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001516 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001517 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1518 // sext_inreg.
1519 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1520 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1521 MVT::ValueType EVT;
1522 switch (LowBits) {
1523 default: EVT = MVT::Other; break;
1524 case 1: EVT = MVT::i1; break;
1525 case 8: EVT = MVT::i8; break;
1526 case 16: EVT = MVT::i16; break;
1527 case 32: EVT = MVT::i32; break;
1528 }
1529 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1530 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1531 DAG.getValueType(EVT));
1532 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001533
1534 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1535 if (N1C && N0.getOpcode() == ISD::SRA) {
1536 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1537 unsigned Sum = N1C->getValue() + C1->getValue();
1538 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1539 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1540 DAG.getConstant(Sum, N1C->getValueType(0)));
1541 }
1542 }
1543
Nate Begeman1d4d4142005-09-01 00:19:25 +00001544 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001545 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001546 return DAG.getNode(ISD::SRL, VT, N0, N1);
1547 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548}
1549
Nate Begeman83e75ec2005-09-06 04:43:02 +00001550SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551 SDOperand N0 = N->getOperand(0);
1552 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001553 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1554 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001555 MVT::ValueType VT = N0.getValueType();
1556 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1557
1558 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001559 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001560 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001561 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001562 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001563 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001564 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001565 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001566 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001568 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001569 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001570 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001571 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001572 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001573 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001574 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001575 N0.getOperand(1).getOpcode() == ISD::Constant) {
1576 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001577 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001578 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001579 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001580 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001581 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001582 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001583 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001584}
1585
Nate Begeman83e75ec2005-09-06 04:43:02 +00001586SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001587 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001588 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001589 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590
1591 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001592 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001593 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001594 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001595}
1596
Nate Begeman83e75ec2005-09-06 04:43:02 +00001597SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001598 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001599 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001600 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001601
1602 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001603 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001604 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001605 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001606}
1607
Nate Begeman83e75ec2005-09-06 04:43:02 +00001608SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001609 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001610 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001611 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001612
1613 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001614 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001615 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001616 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617}
1618
Nate Begeman452d7be2005-09-16 00:54:12 +00001619SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1620 SDOperand N0 = N->getOperand(0);
1621 SDOperand N1 = N->getOperand(1);
1622 SDOperand N2 = N->getOperand(2);
1623 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1624 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1625 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1626 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001627
Nate Begeman452d7be2005-09-16 00:54:12 +00001628 // fold select C, X, X -> X
1629 if (N1 == N2)
1630 return N1;
1631 // fold select true, X, Y -> X
1632 if (N0C && !N0C->isNullValue())
1633 return N1;
1634 // fold select false, X, Y -> Y
1635 if (N0C && N0C->isNullValue())
1636 return N2;
1637 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001638 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001639 return DAG.getNode(ISD::OR, VT, N0, N2);
1640 // fold select C, 0, X -> ~C & X
1641 // FIXME: this should check for C type == X type, not i1?
1642 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1643 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001644 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001645 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1646 }
1647 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001648 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001649 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001650 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001651 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1652 }
1653 // fold select C, X, 0 -> C & X
1654 // FIXME: this should check for C type == X type, not i1?
1655 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1656 return DAG.getNode(ISD::AND, VT, N0, N1);
1657 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1658 if (MVT::i1 == VT && N0 == N1)
1659 return DAG.getNode(ISD::OR, VT, N0, N2);
1660 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1661 if (MVT::i1 == VT && N0 == N2)
1662 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001663 // If we can fold this based on the true/false value, do so.
1664 if (SimplifySelectOps(N, N1, N2))
1665 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001666 // fold selects based on a setcc into other things, such as min/max/abs
1667 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001668 // FIXME:
1669 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1670 // having to say they don't support SELECT_CC on every type the DAG knows
1671 // about, since there is no way to mark an opcode illegal at all value types
1672 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1673 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1674 N1, N2, N0.getOperand(2));
1675 else
1676 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001677 return SDOperand();
1678}
1679
1680SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001681 SDOperand N0 = N->getOperand(0);
1682 SDOperand N1 = N->getOperand(1);
1683 SDOperand N2 = N->getOperand(2);
1684 SDOperand N3 = N->getOperand(3);
1685 SDOperand N4 = N->getOperand(4);
1686 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1687 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1688 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1689 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1690
1691 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001692 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001693 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1694
Nate Begeman44728a72005-09-19 22:34:01 +00001695 // fold select_cc lhs, rhs, x, x, cc -> x
1696 if (N2 == N3)
1697 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001698
1699 // If we can fold this based on the true/false value, do so.
1700 if (SimplifySelectOps(N, N2, N3))
1701 return SDOperand();
1702
Nate Begeman44728a72005-09-19 22:34:01 +00001703 // fold select_cc into other things, such as min/max/abs
1704 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001705}
1706
1707SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1708 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1709 cast<CondCodeSDNode>(N->getOperand(2))->get());
1710}
1711
Nate Begeman83e75ec2005-09-06 04:43:02 +00001712SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001713 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001714 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001715 MVT::ValueType VT = N->getValueType(0);
1716
Nate Begeman1d4d4142005-09-01 00:19:25 +00001717 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001718 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001719 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001720 // fold (sext (sext x)) -> (sext x)
1721 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001722 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001723 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001724 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1725 (!AfterLegalize ||
1726 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001727 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1728 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001729 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001730 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1731 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001732 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1733 N0.getOperand(1), N0.getOperand(2),
1734 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001735 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001736 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1737 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001738 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001739 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001740
1741 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1742 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1743 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1744 N0.hasOneUse()) {
1745 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1746 N0.getOperand(1), N0.getOperand(2),
1747 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001748 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001749 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1750 ExtLoad.getValue(1));
1751 return SDOperand();
1752 }
1753
Nate Begeman83e75ec2005-09-06 04:43:02 +00001754 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001755}
1756
Nate Begeman83e75ec2005-09-06 04:43:02 +00001757SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001758 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001759 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001760 MVT::ValueType VT = N->getValueType(0);
1761
Nate Begeman1d4d4142005-09-01 00:19:25 +00001762 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001763 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001764 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001765 // fold (zext (zext x)) -> (zext x)
1766 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001767 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001768 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1769 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001770 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001771 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001772 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001773 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1774 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001775 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1776 N0.getOperand(1), N0.getOperand(2),
1777 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001778 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001779 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1780 ExtLoad.getValue(1));
1781 return SDOperand();
1782 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001783
1784 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1785 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1786 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1787 N0.hasOneUse()) {
1788 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1789 N0.getOperand(1), N0.getOperand(2),
1790 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001791 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001792 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1793 ExtLoad.getValue(1));
1794 return SDOperand();
1795 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001796 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001797}
1798
Nate Begeman83e75ec2005-09-06 04:43:02 +00001799SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001800 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001801 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001802 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001803 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001804 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001805 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001806
Nate Begeman1d4d4142005-09-01 00:19:25 +00001807 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001808 if (N0C) {
1809 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001810 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001811 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001812 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001813 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001814 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001815 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001816 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001817 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1818 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1819 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001820 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001821 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001822 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1823 if (N0.getOpcode() == ISD::AssertSext &&
1824 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001825 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001826 }
1827 // fold (sext_in_reg (sextload x)) -> (sextload x)
1828 if (N0.getOpcode() == ISD::SEXTLOAD &&
1829 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001830 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001832 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001833 if (N0.getOpcode() == ISD::SETCC &&
1834 TLI.getSetCCResultContents() ==
1835 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001836 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001837 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001838 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001839 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001840 // fold (sext_in_reg (srl x)) -> sra x
1841 if (N0.getOpcode() == ISD::SRL &&
1842 N0.getOperand(1).getOpcode() == ISD::Constant &&
1843 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1844 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1845 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846 }
Nate Begemanded49632005-10-13 03:11:28 +00001847 // fold (sext_inreg (extload x)) -> (sextload x)
1848 if (N0.getOpcode() == ISD::EXTLOAD &&
1849 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001850 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001851 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1852 N0.getOperand(1), N0.getOperand(2),
1853 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001854 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001855 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001856 return SDOperand();
1857 }
1858 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001859 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001860 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001861 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001862 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1863 N0.getOperand(1), N0.getOperand(2),
1864 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001865 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001866 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001867 return SDOperand();
1868 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001869 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001870}
1871
Nate Begeman83e75ec2005-09-06 04:43:02 +00001872SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001873 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001874 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001875 MVT::ValueType VT = N->getValueType(0);
1876
1877 // noop truncate
1878 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001879 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001880 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001881 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001882 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001883 // fold (truncate (truncate x)) -> (truncate x)
1884 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001885 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001886 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1887 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1888 if (N0.getValueType() < VT)
1889 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001890 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001891 else if (N0.getValueType() > VT)
1892 // if the source is larger than the dest, than we just need the 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 else
1895 // if the source and dest are the same type, we can drop both the extend
1896 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001897 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001898 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001899 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001900 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001901 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1902 "Cannot truncate to larger type!");
1903 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001904 // For big endian targets, we need to add an offset to the pointer to load
1905 // the correct bytes. For little endian systems, we merely need to read
1906 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001907 uint64_t PtrOff =
1908 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001909 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1910 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1911 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001912 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001913 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001914 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001915 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001916 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001917 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001918 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001919}
1920
Chris Lattner94683772005-12-23 05:30:37 +00001921SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1922 SDOperand N0 = N->getOperand(0);
1923 MVT::ValueType VT = N->getValueType(0);
1924
1925 // If the input is a constant, let getNode() fold it.
1926 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1927 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1928 if (Res.Val != N) return Res;
1929 }
1930
Chris Lattnerc8547d82005-12-23 05:37:50 +00001931 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1932 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1933
Chris Lattner57104102005-12-23 05:44:41 +00001934 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001935 // FIXME: These xforms need to know that the resultant load doesn't need a
1936 // higher alignment than the original!
1937 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001938 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1939 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001940 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00001941 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1942 Load.getValue(1));
1943 return Load;
1944 }
1945
Chris Lattner94683772005-12-23 05:30:37 +00001946 return SDOperand();
1947}
1948
Chris Lattner01b3d732005-09-28 22:28:18 +00001949SDOperand DAGCombiner::visitFADD(SDNode *N) {
1950 SDOperand N0 = N->getOperand(0);
1951 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001952 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1953 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001954 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001955
1956 // fold (fadd c1, c2) -> c1+c2
1957 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001958 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001959 // canonicalize constant to RHS
1960 if (N0CFP && !N1CFP)
1961 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001962 // fold (A + (-B)) -> A-B
1963 if (N1.getOpcode() == ISD::FNEG)
1964 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001965 // fold ((-A) + B) -> B-A
1966 if (N0.getOpcode() == ISD::FNEG)
1967 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001968 return SDOperand();
1969}
1970
1971SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1972 SDOperand N0 = N->getOperand(0);
1973 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001974 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1975 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001976 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001977
1978 // fold (fsub c1, c2) -> c1-c2
1979 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001980 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001981 // fold (A-(-B)) -> A+B
1982 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001983 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001984 return SDOperand();
1985}
1986
1987SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1988 SDOperand N0 = N->getOperand(0);
1989 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001990 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1991 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001992 MVT::ValueType VT = N->getValueType(0);
1993
Nate Begeman11af4ea2005-10-17 20:40:11 +00001994 // fold (fmul c1, c2) -> c1*c2
1995 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001996 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001997 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001998 if (N0CFP && !N1CFP)
1999 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002000 // fold (fmul X, 2.0) -> (fadd X, X)
2001 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2002 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002003 return SDOperand();
2004}
2005
2006SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2007 SDOperand N0 = N->getOperand(0);
2008 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002009 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2010 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002011 MVT::ValueType VT = N->getValueType(0);
2012
Nate Begemana148d982006-01-18 22:35:16 +00002013 // fold (fdiv c1, c2) -> c1/c2
2014 if (N0CFP && N1CFP)
2015 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002016 return SDOperand();
2017}
2018
2019SDOperand DAGCombiner::visitFREM(SDNode *N) {
2020 SDOperand N0 = N->getOperand(0);
2021 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002022 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2023 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002024 MVT::ValueType VT = N->getValueType(0);
2025
Nate Begemana148d982006-01-18 22:35:16 +00002026 // fold (frem c1, c2) -> fmod(c1,c2)
2027 if (N0CFP && N1CFP)
2028 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002029 return SDOperand();
2030}
2031
Chris Lattner12d83032006-03-05 05:30:57 +00002032SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2033 SDOperand N0 = N->getOperand(0);
2034 SDOperand N1 = N->getOperand(1);
2035 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2036 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2037 MVT::ValueType VT = N->getValueType(0);
2038
2039 if (N0CFP && N1CFP) // Constant fold
2040 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2041
2042 if (N1CFP) {
2043 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2044 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2045 union {
2046 double d;
2047 int64_t i;
2048 } u;
2049 u.d = N1CFP->getValue();
2050 if (u.i >= 0)
2051 return DAG.getNode(ISD::FABS, VT, N0);
2052 else
2053 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2054 }
2055
2056 // copysign(fabs(x), y) -> copysign(x, y)
2057 // copysign(fneg(x), y) -> copysign(x, y)
2058 // copysign(copysign(x,z), y) -> copysign(x, y)
2059 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2060 N0.getOpcode() == ISD::FCOPYSIGN)
2061 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2062
2063 // copysign(x, abs(y)) -> abs(x)
2064 if (N1.getOpcode() == ISD::FABS)
2065 return DAG.getNode(ISD::FABS, VT, N0);
2066
2067 // copysign(x, copysign(y,z)) -> copysign(x, z)
2068 if (N1.getOpcode() == ISD::FCOPYSIGN)
2069 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2070
2071 // copysign(x, fp_extend(y)) -> copysign(x, y)
2072 // copysign(x, fp_round(y)) -> copysign(x, y)
2073 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2074 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2075
2076 return SDOperand();
2077}
2078
2079
Chris Lattner01b3d732005-09-28 22:28:18 +00002080
Nate Begeman83e75ec2005-09-06 04:43:02 +00002081SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002082 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002083 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002084 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002085
2086 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002087 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002088 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002089 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002090}
2091
Nate Begeman83e75ec2005-09-06 04:43:02 +00002092SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002093 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002094 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002095 MVT::ValueType VT = N->getValueType(0);
2096
Nate Begeman1d4d4142005-09-01 00:19:25 +00002097 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002098 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002099 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002100 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002101}
2102
Nate Begeman83e75ec2005-09-06 04:43:02 +00002103SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002104 SDOperand N0 = N->getOperand(0);
2105 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2106 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002107
2108 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002109 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002110 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002111 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002112}
2113
Nate Begeman83e75ec2005-09-06 04:43:02 +00002114SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002115 SDOperand N0 = N->getOperand(0);
2116 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2117 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002118
2119 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002120 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002121 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002122 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002123}
2124
Nate Begeman83e75ec2005-09-06 04:43:02 +00002125SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002126 SDOperand N0 = N->getOperand(0);
2127 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2128 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002129
2130 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002131 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002132 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002133
2134 // fold (fp_round (fp_extend x)) -> x
2135 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2136 return N0.getOperand(0);
2137
2138 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2139 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2140 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2141 AddToWorkList(Tmp.Val);
2142 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2143 }
2144
Nate Begeman83e75ec2005-09-06 04:43:02 +00002145 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002146}
2147
Nate Begeman83e75ec2005-09-06 04:43:02 +00002148SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002149 SDOperand N0 = N->getOperand(0);
2150 MVT::ValueType VT = N->getValueType(0);
2151 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002152 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002153
Nate Begeman1d4d4142005-09-01 00:19:25 +00002154 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002155 if (N0CFP) {
2156 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002157 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002158 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002159 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002160}
2161
Nate Begeman83e75ec2005-09-06 04:43:02 +00002162SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002163 SDOperand N0 = N->getOperand(0);
2164 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2165 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002166
2167 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002168 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002169 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002170 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002171}
2172
Nate Begeman83e75ec2005-09-06 04:43:02 +00002173SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002174 SDOperand N0 = N->getOperand(0);
2175 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2176 MVT::ValueType VT = N->getValueType(0);
2177
2178 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002179 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002180 return DAG.getNode(ISD::FNEG, VT, N0);
2181 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002182 if (N0.getOpcode() == ISD::SUB)
2183 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002184 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002185 if (N0.getOpcode() == ISD::FNEG)
2186 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002187 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002188}
2189
Nate Begeman83e75ec2005-09-06 04:43:02 +00002190SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002191 SDOperand N0 = N->getOperand(0);
2192 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2193 MVT::ValueType VT = N->getValueType(0);
2194
Nate Begeman1d4d4142005-09-01 00:19:25 +00002195 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002196 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002197 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002198 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002199 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002200 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002201 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002202 // fold (fabs (fcopysign x, y)) -> (fabs x)
2203 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2204 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2205
Nate Begeman83e75ec2005-09-06 04:43:02 +00002206 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002207}
2208
Nate Begeman44728a72005-09-19 22:34:01 +00002209SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2210 SDOperand Chain = N->getOperand(0);
2211 SDOperand N1 = N->getOperand(1);
2212 SDOperand N2 = N->getOperand(2);
2213 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2214
2215 // never taken branch, fold to chain
2216 if (N1C && N1C->isNullValue())
2217 return Chain;
2218 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002219 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002220 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002221 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2222 // on the target.
2223 if (N1.getOpcode() == ISD::SETCC &&
2224 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2225 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2226 N1.getOperand(0), N1.getOperand(1), N2);
2227 }
Nate Begeman44728a72005-09-19 22:34:01 +00002228 return SDOperand();
2229}
2230
Chris Lattner3ea0b472005-10-05 06:47:48 +00002231// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2232//
Nate Begeman44728a72005-09-19 22:34:01 +00002233SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002234 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2235 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2236
2237 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002238 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2239 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2240
2241 // fold br_cc true, dest -> br dest (unconditional branch)
2242 if (SCCC && SCCC->getValue())
2243 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2244 N->getOperand(4));
2245 // fold br_cc false, dest -> unconditional fall through
2246 if (SCCC && SCCC->isNullValue())
2247 return N->getOperand(0);
2248 // fold to a simpler setcc
2249 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2250 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2251 Simp.getOperand(2), Simp.getOperand(0),
2252 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002253 return SDOperand();
2254}
2255
Chris Lattner01a22022005-10-10 22:04:48 +00002256SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2257 SDOperand Chain = N->getOperand(0);
2258 SDOperand Ptr = N->getOperand(1);
2259 SDOperand SrcValue = N->getOperand(2);
2260
2261 // If this load is directly stored, replace the load value with the stored
2262 // value.
2263 // TODO: Handle store large -> read small portion.
2264 // TODO: Handle TRUNCSTORE/EXTLOAD
2265 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2266 Chain.getOperand(1).getValueType() == N->getValueType(0))
2267 return CombineTo(N, Chain.getOperand(1), Chain);
2268
2269 return SDOperand();
2270}
2271
Chris Lattner87514ca2005-10-10 22:31:19 +00002272SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2273 SDOperand Chain = N->getOperand(0);
2274 SDOperand Value = N->getOperand(1);
2275 SDOperand Ptr = N->getOperand(2);
2276 SDOperand SrcValue = N->getOperand(3);
2277
2278 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002279 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002280 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2281 // Make sure that these stores are the same value type:
2282 // FIXME: we really care that the second store is >= size of the first.
2283 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002284 // Create a new store of Value that replaces both stores.
2285 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002286 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2287 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002288 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2289 PrevStore->getOperand(0), Value, Ptr,
2290 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002291 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002292 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002293 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002294 }
2295
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002296 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002297 // FIXME: This needs to know that the resultant store does not need a
2298 // higher alignment than the original.
2299 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002300 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2301 Ptr, SrcValue);
2302
Chris Lattner87514ca2005-10-10 22:31:19 +00002303 return SDOperand();
2304}
2305
Chris Lattnerca242442006-03-19 01:27:56 +00002306SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2307 SDOperand InVec = N->getOperand(0);
2308 SDOperand InVal = N->getOperand(1);
2309 SDOperand EltNo = N->getOperand(2);
2310
2311 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2312 // vector with the inserted element.
2313 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2314 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2315 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2316 if (Elt < Ops.size())
2317 Ops[Elt] = InVal;
2318 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), Ops);
2319 }
2320
2321 return SDOperand();
2322}
2323
2324SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2325 SDOperand InVec = N->getOperand(0);
2326 SDOperand InVal = N->getOperand(1);
2327 SDOperand EltNo = N->getOperand(2);
2328 SDOperand NumElts = N->getOperand(3);
2329 SDOperand EltType = N->getOperand(4);
2330
2331 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2332 // vector with the inserted element.
2333 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2334 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2335 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2336 if (Elt < Ops.size()-2)
2337 Ops[Elt] = InVal;
2338 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), Ops);
2339 }
2340
2341 return SDOperand();
2342}
2343
Nate Begeman44728a72005-09-19 22:34:01 +00002344SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002345 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2346
2347 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2348 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2349 // If we got a simplified select_cc node back from SimplifySelectCC, then
2350 // break it down into a new SETCC node, and a new SELECT node, and then return
2351 // the SELECT node, since we were called with a SELECT node.
2352 if (SCC.Val) {
2353 // Check to see if we got a select_cc back (to turn into setcc/select).
2354 // Otherwise, just return whatever node we got back, like fabs.
2355 if (SCC.getOpcode() == ISD::SELECT_CC) {
2356 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2357 SCC.getOperand(0), SCC.getOperand(1),
2358 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002359 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002360 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2361 SCC.getOperand(3), SETCC);
2362 }
2363 return SCC;
2364 }
Nate Begeman44728a72005-09-19 22:34:01 +00002365 return SDOperand();
2366}
2367
Chris Lattner40c62d52005-10-18 06:04:22 +00002368/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2369/// are the two values being selected between, see if we can simplify the
2370/// select.
2371///
2372bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2373 SDOperand RHS) {
2374
2375 // If this is a select from two identical things, try to pull the operation
2376 // through the select.
2377 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2378#if 0
2379 std::cerr << "SELECT: ["; LHS.Val->dump();
2380 std::cerr << "] ["; RHS.Val->dump();
2381 std::cerr << "]\n";
2382#endif
2383
2384 // If this is a load and the token chain is identical, replace the select
2385 // of two loads with a load through a select of the address to load from.
2386 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2387 // constants have been dropped into the constant pool.
2388 if ((LHS.getOpcode() == ISD::LOAD ||
2389 LHS.getOpcode() == ISD::EXTLOAD ||
2390 LHS.getOpcode() == ISD::ZEXTLOAD ||
2391 LHS.getOpcode() == ISD::SEXTLOAD) &&
2392 // Token chains must be identical.
2393 LHS.getOperand(0) == RHS.getOperand(0) &&
2394 // If this is an EXTLOAD, the VT's must match.
2395 (LHS.getOpcode() == ISD::LOAD ||
2396 LHS.getOperand(3) == RHS.getOperand(3))) {
2397 // FIXME: this conflates two src values, discarding one. This is not
2398 // the right thing to do, but nothing uses srcvalues now. When they do,
2399 // turn SrcValue into a list of locations.
2400 SDOperand Addr;
2401 if (TheSelect->getOpcode() == ISD::SELECT)
2402 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2403 TheSelect->getOperand(0), LHS.getOperand(1),
2404 RHS.getOperand(1));
2405 else
2406 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2407 TheSelect->getOperand(0),
2408 TheSelect->getOperand(1),
2409 LHS.getOperand(1), RHS.getOperand(1),
2410 TheSelect->getOperand(4));
2411
2412 SDOperand Load;
2413 if (LHS.getOpcode() == ISD::LOAD)
2414 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2415 Addr, LHS.getOperand(2));
2416 else
2417 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2418 LHS.getOperand(0), Addr, LHS.getOperand(2),
2419 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2420 // Users of the select now use the result of the load.
2421 CombineTo(TheSelect, Load);
2422
2423 // Users of the old loads now use the new load's chain. We know the
2424 // old-load value is dead now.
2425 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2426 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2427 return true;
2428 }
2429 }
2430
2431 return false;
2432}
2433
Nate Begeman44728a72005-09-19 22:34:01 +00002434SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2435 SDOperand N2, SDOperand N3,
2436 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002437
2438 MVT::ValueType VT = N2.getValueType();
2439 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2440 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2441 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2442 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2443
2444 // Determine if the condition we're dealing with is constant
2445 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2446 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2447
2448 // fold select_cc true, x, y -> x
2449 if (SCCC && SCCC->getValue())
2450 return N2;
2451 // fold select_cc false, x, y -> y
2452 if (SCCC && SCCC->getValue() == 0)
2453 return N3;
2454
2455 // Check to see if we can simplify the select into an fabs node
2456 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2457 // Allow either -0.0 or 0.0
2458 if (CFP->getValue() == 0.0) {
2459 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2460 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2461 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2462 N2 == N3.getOperand(0))
2463 return DAG.getNode(ISD::FABS, VT, N0);
2464
2465 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2466 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2467 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2468 N2.getOperand(0) == N3)
2469 return DAG.getNode(ISD::FABS, VT, N3);
2470 }
2471 }
2472
2473 // Check to see if we can perform the "gzip trick", transforming
2474 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2475 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2476 MVT::isInteger(N0.getValueType()) &&
2477 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2478 MVT::ValueType XType = N0.getValueType();
2479 MVT::ValueType AType = N2.getValueType();
2480 if (XType >= AType) {
2481 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002482 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002483 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2484 unsigned ShCtV = Log2_64(N2C->getValue());
2485 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2486 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2487 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00002488 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002489 if (XType > AType) {
2490 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002491 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002492 }
2493 return DAG.getNode(ISD::AND, AType, Shift, N2);
2494 }
2495 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2496 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2497 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002498 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002499 if (XType > AType) {
2500 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002501 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002502 }
2503 return DAG.getNode(ISD::AND, AType, Shift, N2);
2504 }
2505 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002506
2507 // fold select C, 16, 0 -> shl C, 4
2508 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2509 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2510 // Get a SetCC of the condition
2511 // FIXME: Should probably make sure that setcc is legal if we ever have a
2512 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00002513 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00002514 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00002515 if (AfterLegalize) {
2516 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002517 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00002518 } else {
2519 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002520 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00002521 }
Chris Lattner5750df92006-03-01 04:03:14 +00002522 AddToWorkList(SCC.Val);
2523 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00002524 // shl setcc result by log2 n2c
2525 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2526 DAG.getConstant(Log2_64(N2C->getValue()),
2527 TLI.getShiftAmountTy()));
2528 }
2529
Nate Begemanf845b452005-10-08 00:29:44 +00002530 // Check to see if this is the equivalent of setcc
2531 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2532 // otherwise, go ahead with the folds.
2533 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2534 MVT::ValueType XType = N0.getValueType();
2535 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2536 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2537 if (Res.getValueType() != VT)
2538 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2539 return Res;
2540 }
2541
2542 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2543 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2544 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2545 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2546 return DAG.getNode(ISD::SRL, XType, Ctlz,
2547 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2548 TLI.getShiftAmountTy()));
2549 }
2550 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2551 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2552 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2553 N0);
2554 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2555 DAG.getConstant(~0ULL, XType));
2556 return DAG.getNode(ISD::SRL, XType,
2557 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2558 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2559 TLI.getShiftAmountTy()));
2560 }
2561 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2562 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2563 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2564 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2565 TLI.getShiftAmountTy()));
2566 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2567 }
2568 }
2569
2570 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2571 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2572 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2573 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2574 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2575 MVT::ValueType XType = N0.getValueType();
2576 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2577 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2578 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2579 TLI.getShiftAmountTy()));
2580 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002581 AddToWorkList(Shift.Val);
2582 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002583 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2584 }
2585 }
2586 }
2587
Nate Begeman44728a72005-09-19 22:34:01 +00002588 return SDOperand();
2589}
2590
Nate Begeman452d7be2005-09-16 00:54:12 +00002591SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002592 SDOperand N1, ISD::CondCode Cond,
2593 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002594 // These setcc operations always fold.
2595 switch (Cond) {
2596 default: break;
2597 case ISD::SETFALSE:
2598 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2599 case ISD::SETTRUE:
2600 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2601 }
2602
2603 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2604 uint64_t C1 = N1C->getValue();
2605 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2606 uint64_t C0 = N0C->getValue();
2607
2608 // Sign extend the operands if required
2609 if (ISD::isSignedIntSetCC(Cond)) {
2610 C0 = N0C->getSignExtended();
2611 C1 = N1C->getSignExtended();
2612 }
2613
2614 switch (Cond) {
2615 default: assert(0 && "Unknown integer setcc!");
2616 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2617 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2618 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2619 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2620 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2621 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2622 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2623 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2624 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2625 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2626 }
2627 } else {
2628 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2629 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2630 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2631
2632 // If the comparison constant has bits in the upper part, the
2633 // zero-extended value could never match.
2634 if (C1 & (~0ULL << InSize)) {
2635 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2636 switch (Cond) {
2637 case ISD::SETUGT:
2638 case ISD::SETUGE:
2639 case ISD::SETEQ: return DAG.getConstant(0, VT);
2640 case ISD::SETULT:
2641 case ISD::SETULE:
2642 case ISD::SETNE: return DAG.getConstant(1, VT);
2643 case ISD::SETGT:
2644 case ISD::SETGE:
2645 // True if the sign bit of C1 is set.
2646 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2647 case ISD::SETLT:
2648 case ISD::SETLE:
2649 // True if the sign bit of C1 isn't set.
2650 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2651 default:
2652 break;
2653 }
2654 }
2655
2656 // Otherwise, we can perform the comparison with the low bits.
2657 switch (Cond) {
2658 case ISD::SETEQ:
2659 case ISD::SETNE:
2660 case ISD::SETUGT:
2661 case ISD::SETUGE:
2662 case ISD::SETULT:
2663 case ISD::SETULE:
2664 return DAG.getSetCC(VT, N0.getOperand(0),
2665 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2666 Cond);
2667 default:
2668 break; // todo, be more careful with signed comparisons
2669 }
2670 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2671 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2672 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2673 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2674 MVT::ValueType ExtDstTy = N0.getValueType();
2675 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2676
2677 // If the extended part has any inconsistent bits, it cannot ever
2678 // compare equal. In other words, they have to be all ones or all
2679 // zeros.
2680 uint64_t ExtBits =
2681 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2682 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2683 return DAG.getConstant(Cond == ISD::SETNE, VT);
2684
2685 SDOperand ZextOp;
2686 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2687 if (Op0Ty == ExtSrcTy) {
2688 ZextOp = N0.getOperand(0);
2689 } else {
2690 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2691 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2692 DAG.getConstant(Imm, Op0Ty));
2693 }
Chris Lattner5750df92006-03-01 04:03:14 +00002694 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002695 // Otherwise, make this a use of a zext.
2696 return DAG.getSetCC(VT, ZextOp,
2697 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2698 ExtDstTy),
2699 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002700 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2701 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2702 (N0.getOpcode() == ISD::XOR ||
2703 (N0.getOpcode() == ISD::AND &&
2704 N0.getOperand(0).getOpcode() == ISD::XOR &&
2705 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2706 isa<ConstantSDNode>(N0.getOperand(1)) &&
2707 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2708 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2709 // only do this if the top bits are known zero.
2710 if (TLI.MaskedValueIsZero(N1,
2711 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2712 // Okay, get the un-inverted input value.
2713 SDOperand Val;
2714 if (N0.getOpcode() == ISD::XOR)
2715 Val = N0.getOperand(0);
2716 else {
2717 assert(N0.getOpcode() == ISD::AND &&
2718 N0.getOperand(0).getOpcode() == ISD::XOR);
2719 // ((X^1)&1)^1 -> X & 1
2720 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2721 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2722 }
2723 return DAG.getSetCC(VT, Val, N1,
2724 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2725 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002726 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002727
Nate Begeman452d7be2005-09-16 00:54:12 +00002728 uint64_t MinVal, MaxVal;
2729 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2730 if (ISD::isSignedIntSetCC(Cond)) {
2731 MinVal = 1ULL << (OperandBitSize-1);
2732 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2733 MaxVal = ~0ULL >> (65-OperandBitSize);
2734 else
2735 MaxVal = 0;
2736 } else {
2737 MinVal = 0;
2738 MaxVal = ~0ULL >> (64-OperandBitSize);
2739 }
2740
2741 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2742 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2743 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2744 --C1; // X >= C0 --> X > (C0-1)
2745 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2746 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2747 }
2748
2749 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2750 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2751 ++C1; // X <= C0 --> X < (C0+1)
2752 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2753 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2754 }
2755
2756 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2757 return DAG.getConstant(0, VT); // X < MIN --> false
2758
2759 // Canonicalize setgt X, Min --> setne X, Min
2760 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2761 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002762 // Canonicalize setlt X, Max --> setne X, Max
2763 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2764 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002765
2766 // If we have setult X, 1, turn it into seteq X, 0
2767 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2768 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2769 ISD::SETEQ);
2770 // If we have setugt X, Max-1, turn it into seteq X, Max
2771 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2772 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2773 ISD::SETEQ);
2774
2775 // If we have "setcc X, C0", check to see if we can shrink the immediate
2776 // by changing cc.
2777
2778 // SETUGT X, SINTMAX -> SETLT X, 0
2779 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2780 C1 == (~0ULL >> (65-OperandBitSize)))
2781 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2782 ISD::SETLT);
2783
2784 // FIXME: Implement the rest of these.
2785
2786 // Fold bit comparisons when we can.
2787 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2788 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2789 if (ConstantSDNode *AndRHS =
2790 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2791 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2792 // Perform the xform if the AND RHS is a single bit.
2793 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2794 return DAG.getNode(ISD::SRL, VT, N0,
2795 DAG.getConstant(Log2_64(AndRHS->getValue()),
2796 TLI.getShiftAmountTy()));
2797 }
2798 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2799 // (X & 8) == 8 --> (X & 8) >> 3
2800 // Perform the xform if C1 is a single bit.
2801 if ((C1 & (C1-1)) == 0) {
2802 return DAG.getNode(ISD::SRL, VT, N0,
2803 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2804 }
2805 }
2806 }
2807 }
2808 } else if (isa<ConstantSDNode>(N0.Val)) {
2809 // Ensure that the constant occurs on the RHS.
2810 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2811 }
2812
2813 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2814 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2815 double C0 = N0C->getValue(), C1 = N1C->getValue();
2816
2817 switch (Cond) {
2818 default: break; // FIXME: Implement the rest of these!
2819 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2820 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2821 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2822 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2823 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2824 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2825 }
2826 } else {
2827 // Ensure that the constant occurs on the RHS.
2828 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2829 }
2830
2831 if (N0 == N1) {
2832 // We can always fold X == Y for integer setcc's.
2833 if (MVT::isInteger(N0.getValueType()))
2834 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2835 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2836 if (UOF == 2) // FP operators that are undefined on NaNs.
2837 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2838 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2839 return DAG.getConstant(UOF, VT);
2840 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2841 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002842 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002843 if (NewCond != Cond)
2844 return DAG.getSetCC(VT, N0, N1, NewCond);
2845 }
2846
2847 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2848 MVT::isInteger(N0.getValueType())) {
2849 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2850 N0.getOpcode() == ISD::XOR) {
2851 // Simplify (X+Y) == (X+Z) --> Y == Z
2852 if (N0.getOpcode() == N1.getOpcode()) {
2853 if (N0.getOperand(0) == N1.getOperand(0))
2854 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2855 if (N0.getOperand(1) == N1.getOperand(1))
2856 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2857 if (isCommutativeBinOp(N0.getOpcode())) {
2858 // If X op Y == Y op X, try other combinations.
2859 if (N0.getOperand(0) == N1.getOperand(1))
2860 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2861 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002862 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002863 }
2864 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002865
2866 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2867 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2868 // Turn (X+C1) == C2 --> X == C2-C1
2869 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2870 return DAG.getSetCC(VT, N0.getOperand(0),
2871 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2872 N0.getValueType()), Cond);
2873 }
2874
2875 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2876 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002877 // If we know that all of the inverted bits are zero, don't bother
2878 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002879 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002880 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002881 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002882 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002883 }
2884
2885 // Turn (C1-X) == C2 --> X == C1-C2
2886 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2887 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
2888 return DAG.getSetCC(VT, N0.getOperand(1),
2889 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
2890 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00002891 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002892 }
2893 }
2894
Nate Begeman452d7be2005-09-16 00:54:12 +00002895 // Simplify (X+Z) == X --> Z == 0
2896 if (N0.getOperand(0) == N1)
2897 return DAG.getSetCC(VT, N0.getOperand(1),
2898 DAG.getConstant(0, N0.getValueType()), Cond);
2899 if (N0.getOperand(1) == N1) {
2900 if (isCommutativeBinOp(N0.getOpcode()))
2901 return DAG.getSetCC(VT, N0.getOperand(0),
2902 DAG.getConstant(0, N0.getValueType()), Cond);
2903 else {
2904 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2905 // (Z-X) == X --> Z == X<<1
2906 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2907 N1,
2908 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002909 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002910 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2911 }
2912 }
2913 }
2914
2915 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2916 N1.getOpcode() == ISD::XOR) {
2917 // Simplify X == (X+Z) --> Z == 0
2918 if (N1.getOperand(0) == N0) {
2919 return DAG.getSetCC(VT, N1.getOperand(1),
2920 DAG.getConstant(0, N1.getValueType()), Cond);
2921 } else if (N1.getOperand(1) == N0) {
2922 if (isCommutativeBinOp(N1.getOpcode())) {
2923 return DAG.getSetCC(VT, N1.getOperand(0),
2924 DAG.getConstant(0, N1.getValueType()), Cond);
2925 } else {
2926 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2927 // X == (Z-X) --> X<<1 == Z
2928 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2929 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002930 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002931 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2932 }
2933 }
2934 }
2935 }
2936
2937 // Fold away ALL boolean setcc's.
2938 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002939 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002940 switch (Cond) {
2941 default: assert(0 && "Unknown integer setcc!");
2942 case ISD::SETEQ: // X == Y -> (X^Y)^1
2943 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2944 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00002945 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002946 break;
2947 case ISD::SETNE: // X != Y --> (X^Y)
2948 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2949 break;
2950 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2951 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2952 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2953 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00002954 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002955 break;
2956 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2957 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2958 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2959 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00002960 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002961 break;
2962 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2963 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2964 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2965 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00002966 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002967 break;
2968 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2969 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2970 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2971 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2972 break;
2973 }
2974 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00002975 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002976 // FIXME: If running after legalize, we probably can't do this.
2977 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2978 }
2979 return N0;
2980 }
2981
2982 // Could not fold it.
2983 return SDOperand();
2984}
2985
Nate Begeman69575232005-10-20 02:15:44 +00002986/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2987/// return a DAG expression to select that will generate the same value by
2988/// multiplying by a magic number. See:
2989/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2990SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2991 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002992
2993 // Check to see if we can do this.
2994 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2995 return SDOperand(); // BuildSDIV only operates on i32 or i64
2996 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2997 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002998
Nate Begemanc6a454e2005-10-20 17:45:03 +00002999 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00003000 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
3001
3002 // Multiply the numerator (operand 0) by the magic value
3003 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
3004 DAG.getConstant(magics.m, VT));
3005 // If d > 0 and m < 0, add the numerator
3006 if (d > 0 && magics.m < 0) {
3007 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003008 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003009 }
3010 // If d < 0 and m > 0, subtract the numerator.
3011 if (d < 0 && magics.m > 0) {
3012 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003013 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003014 }
3015 // Shift right algebraic if shift value is nonzero
3016 if (magics.s > 0) {
3017 Q = DAG.getNode(ISD::SRA, VT, Q,
3018 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003019 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003020 }
3021 // Extract the sign bit and add it to the quotient
3022 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00003023 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
3024 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003025 AddToWorkList(T.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003026 return DAG.getNode(ISD::ADD, VT, Q, T);
3027}
3028
3029/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3030/// return a DAG expression to select that will generate the same value by
3031/// multiplying by a magic number. See:
3032/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3033SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3034 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003035
3036 // Check to see if we can do this.
3037 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3038 return SDOperand(); // BuildUDIV only operates on i32 or i64
3039 if (!TLI.isOperationLegal(ISD::MULHU, VT))
3040 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00003041
3042 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
3043 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
3044
3045 // Multiply the numerator (operand 0) by the magic value
3046 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
3047 DAG.getConstant(magics.m, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00003048 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003049
3050 if (magics.a == 0) {
3051 return DAG.getNode(ISD::SRL, VT, Q,
3052 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3053 } else {
3054 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003055 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003056 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
3057 DAG.getConstant(1, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003058 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003059 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003060 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003061 return DAG.getNode(ISD::SRL, VT, NPQ,
3062 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
3063 }
3064}
3065
Nate Begeman1d4d4142005-09-01 00:19:25 +00003066// SelectionDAG::Combine - This is the entry point for the file.
3067//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003068void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003069 /// run - This is the main entry point to this class.
3070 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003071 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003072}