blob: 6152d228cc9ba20d22fce17af22299abc2a83f12 [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 Lattner01a22022005-10-10 22:04:48 +0000212
Nate Begemancd4d58c2006-02-03 06:46:56 +0000213 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
214
Chris Lattner40c62d52005-10-18 06:04:22 +0000215 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000216 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
217 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
218 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000219 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000220 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000221
222 SDOperand BuildSDIV(SDNode *N);
223 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000224public:
225 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000226 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000227
228 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000229 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000230 };
231}
232
Chris Lattner24664722006-03-01 04:53:38 +0000233//===----------------------------------------------------------------------===//
234// TargetLowering::DAGCombinerInfo implementation
235//===----------------------------------------------------------------------===//
236
237void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
238 ((DAGCombiner*)DC)->AddToWorkList(N);
239}
240
241SDOperand TargetLowering::DAGCombinerInfo::
242CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
243 return ((DAGCombiner*)DC)->CombineTo(N, To);
244}
245
246SDOperand TargetLowering::DAGCombinerInfo::
247CombineTo(SDNode *N, SDOperand Res) {
248 return ((DAGCombiner*)DC)->CombineTo(N, Res);
249}
250
251
252SDOperand TargetLowering::DAGCombinerInfo::
253CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
254 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
255}
256
257
258
259
260//===----------------------------------------------------------------------===//
261
262
Nate Begeman69575232005-10-20 02:15:44 +0000263struct ms {
264 int64_t m; // magic number
265 int64_t s; // shift amount
266};
267
268struct mu {
269 uint64_t m; // magic number
270 int64_t a; // add indicator
271 int64_t s; // shift amount
272};
273
274/// magic - calculate the magic numbers required to codegen an integer sdiv as
275/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
276/// or -1.
277static ms magic32(int32_t d) {
278 int32_t p;
279 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
280 const uint32_t two31 = 0x80000000U;
281 struct ms mag;
282
283 ad = abs(d);
284 t = two31 + ((uint32_t)d >> 31);
285 anc = t - 1 - t%ad; // absolute value of nc
286 p = 31; // initialize p
287 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
288 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
289 q2 = two31/ad; // initialize q2 = 2p/abs(d)
290 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
291 do {
292 p = p + 1;
293 q1 = 2*q1; // update q1 = 2p/abs(nc)
294 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
295 if (r1 >= anc) { // must be unsigned comparison
296 q1 = q1 + 1;
297 r1 = r1 - anc;
298 }
299 q2 = 2*q2; // update q2 = 2p/abs(d)
300 r2 = 2*r2; // update r2 = rem(2p/abs(d))
301 if (r2 >= ad) { // must be unsigned comparison
302 q2 = q2 + 1;
303 r2 = r2 - ad;
304 }
305 delta = ad - r2;
306 } while (q1 < delta || (q1 == delta && r1 == 0));
307
308 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
309 if (d < 0) mag.m = -mag.m; // resulting magic number
310 mag.s = p - 32; // resulting shift
311 return mag;
312}
313
314/// magicu - calculate the magic numbers required to codegen an integer udiv as
315/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
316static mu magicu32(uint32_t d) {
317 int32_t p;
318 uint32_t nc, delta, q1, r1, q2, r2;
319 struct mu magu;
320 magu.a = 0; // initialize "add" indicator
321 nc = - 1 - (-d)%d;
322 p = 31; // initialize p
323 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
324 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
325 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
326 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
327 do {
328 p = p + 1;
329 if (r1 >= nc - r1 ) {
330 q1 = 2*q1 + 1; // update q1
331 r1 = 2*r1 - nc; // update r1
332 }
333 else {
334 q1 = 2*q1; // update q1
335 r1 = 2*r1; // update r1
336 }
337 if (r2 + 1 >= d - r2) {
338 if (q2 >= 0x7FFFFFFF) magu.a = 1;
339 q2 = 2*q2 + 1; // update q2
340 r2 = 2*r2 + 1 - d; // update r2
341 }
342 else {
343 if (q2 >= 0x80000000) magu.a = 1;
344 q2 = 2*q2; // update q2
345 r2 = 2*r2 + 1; // update r2
346 }
347 delta = d - 1 - r2;
348 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
349 magu.m = q2 + 1; // resulting magic number
350 magu.s = p - 32; // resulting shift
351 return magu;
352}
353
354/// magic - calculate the magic numbers required to codegen an integer sdiv as
355/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
356/// or -1.
357static ms magic64(int64_t d) {
358 int64_t p;
359 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
360 const uint64_t two63 = 9223372036854775808ULL; // 2^63
361 struct ms mag;
362
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000363 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000364 t = two63 + ((uint64_t)d >> 63);
365 anc = t - 1 - t%ad; // absolute value of nc
366 p = 63; // initialize p
367 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
368 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
369 q2 = two63/ad; // initialize q2 = 2p/abs(d)
370 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
371 do {
372 p = p + 1;
373 q1 = 2*q1; // update q1 = 2p/abs(nc)
374 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
375 if (r1 >= anc) { // must be unsigned comparison
376 q1 = q1 + 1;
377 r1 = r1 - anc;
378 }
379 q2 = 2*q2; // update q2 = 2p/abs(d)
380 r2 = 2*r2; // update r2 = rem(2p/abs(d))
381 if (r2 >= ad) { // must be unsigned comparison
382 q2 = q2 + 1;
383 r2 = r2 - ad;
384 }
385 delta = ad - r2;
386 } while (q1 < delta || (q1 == delta && r1 == 0));
387
388 mag.m = q2 + 1;
389 if (d < 0) mag.m = -mag.m; // resulting magic number
390 mag.s = p - 64; // resulting shift
391 return mag;
392}
393
394/// magicu - calculate the magic numbers required to codegen an integer udiv as
395/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
396static mu magicu64(uint64_t d)
397{
398 int64_t p;
399 uint64_t nc, delta, q1, r1, q2, r2;
400 struct mu magu;
401 magu.a = 0; // initialize "add" indicator
402 nc = - 1 - (-d)%d;
403 p = 63; // initialize p
404 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
405 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
406 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
407 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
408 do {
409 p = p + 1;
410 if (r1 >= nc - r1 ) {
411 q1 = 2*q1 + 1; // update q1
412 r1 = 2*r1 - nc; // update r1
413 }
414 else {
415 q1 = 2*q1; // update q1
416 r1 = 2*r1; // update r1
417 }
418 if (r2 + 1 >= d - r2) {
419 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
420 q2 = 2*q2 + 1; // update q2
421 r2 = 2*r2 + 1 - d; // update r2
422 }
423 else {
424 if (q2 >= 0x8000000000000000ull) magu.a = 1;
425 q2 = 2*q2; // update q2
426 r2 = 2*r2 + 1; // update r2
427 }
428 delta = d - 1 - r2;
429 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
430 magu.m = q2 + 1; // resulting magic number
431 magu.s = p - 64; // resulting shift
432 return magu;
433}
434
Nate Begeman4ebd8052005-09-01 23:24:04 +0000435// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
436// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000437// Also, set the incoming LHS, RHS, and CC references to the appropriate
438// nodes based on the type of node we are checking. This simplifies life a
439// bit for the callers.
440static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
441 SDOperand &CC) {
442 if (N.getOpcode() == ISD::SETCC) {
443 LHS = N.getOperand(0);
444 RHS = N.getOperand(1);
445 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000446 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000447 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000448 if (N.getOpcode() == ISD::SELECT_CC &&
449 N.getOperand(2).getOpcode() == ISD::Constant &&
450 N.getOperand(3).getOpcode() == ISD::Constant &&
451 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000452 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
453 LHS = N.getOperand(0);
454 RHS = N.getOperand(1);
455 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000456 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000457 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000458 return false;
459}
460
Nate Begeman99801192005-09-07 23:25:52 +0000461// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
462// one use. If this is true, it allows the users to invert the operation for
463// free when it is profitable to do so.
464static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000465 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000466 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000467 return true;
468 return false;
469}
470
Nate Begeman452d7be2005-09-16 00:54:12 +0000471// FIXME: This should probably go in the ISD class rather than being duplicated
472// in several files.
473static bool isCommutativeBinOp(unsigned Opcode) {
474 switch (Opcode) {
475 case ISD::ADD:
476 case ISD::MUL:
477 case ISD::AND:
478 case ISD::OR:
479 case ISD::XOR: return true;
480 default: return false; // FIXME: Need commutative info for user ops!
481 }
482}
483
Nate Begemancd4d58c2006-02-03 06:46:56 +0000484SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
485 MVT::ValueType VT = N0.getValueType();
486 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
487 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
488 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
489 if (isa<ConstantSDNode>(N1)) {
490 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000491 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000492 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
493 } else if (N0.hasOneUse()) {
494 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000495 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000496 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
497 }
498 }
499 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
500 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
501 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
502 if (isa<ConstantSDNode>(N0)) {
503 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000504 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000505 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
506 } else if (N1.hasOneUse()) {
507 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000508 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000509 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
510 }
511 }
512 return SDOperand();
513}
514
Nate Begeman4ebd8052005-09-01 23:24:04 +0000515void DAGCombiner::Run(bool RunningAfterLegalize) {
516 // set the instance variable, so that the various visit routines may use it.
517 AfterLegalize = RunningAfterLegalize;
518
Nate Begeman646d7e22005-09-02 21:18:40 +0000519 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000520 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
521 E = DAG.allnodes_end(); I != E; ++I)
522 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000523
Chris Lattner95038592005-10-05 06:35:28 +0000524 // Create a dummy node (which is not added to allnodes), that adds a reference
525 // to the root node, preventing it from being deleted, and tracking any
526 // changes of the root.
527 HandleSDNode Dummy(DAG.getRoot());
528
Chris Lattner24664722006-03-01 04:53:38 +0000529
530 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
531 TargetLowering::DAGCombinerInfo
532 DagCombineInfo(DAG, !RunningAfterLegalize, this);
533
Nate Begeman1d4d4142005-09-01 00:19:25 +0000534 // while the worklist isn't empty, inspect the node on the end of it and
535 // try and combine it.
536 while (!WorkList.empty()) {
537 SDNode *N = WorkList.back();
538 WorkList.pop_back();
539
540 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000541 // N is deleted from the DAG, since they too may now be dead or may have a
542 // reduced number of uses, allowing other xforms.
543 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000544 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
545 WorkList.push_back(N->getOperand(i).Val);
546
Nate Begeman1d4d4142005-09-01 00:19:25 +0000547 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000548 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000549 continue;
550 }
551
Nate Begeman83e75ec2005-09-06 04:43:02 +0000552 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000553
554 // If nothing happened, try a target-specific DAG combine.
555 if (RV.Val == 0) {
556 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
557 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
558 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
559 }
560
Nate Begeman83e75ec2005-09-06 04:43:02 +0000561 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000562 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000563 // If we get back the same node we passed in, rather than a new node or
564 // zero, we know that the node must have defined multiple values and
565 // CombineTo was used. Since CombineTo takes care of the worklist
566 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000567 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000568 DEBUG(std::cerr << "\nReplacing "; N->dump();
569 std::cerr << "\nWith: "; RV.Val->dump();
570 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000571 std::vector<SDNode*> NowDead;
572 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000573
574 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000575 WorkList.push_back(RV.Val);
576 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000577
578 // Nodes can end up on the worklist more than once. Make sure we do
579 // not process a node that has been replaced.
580 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000581 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
582 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000583
584 // Finally, since the node is now dead, remove it from the graph.
585 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000586 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000587 }
588 }
Chris Lattner95038592005-10-05 06:35:28 +0000589
590 // If the root changed (e.g. it was a dead load, update the root).
591 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000592}
593
Nate Begeman83e75ec2005-09-06 04:43:02 +0000594SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595 switch(N->getOpcode()) {
596 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000597 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000598 case ISD::ADD: return visitADD(N);
599 case ISD::SUB: return visitSUB(N);
600 case ISD::MUL: return visitMUL(N);
601 case ISD::SDIV: return visitSDIV(N);
602 case ISD::UDIV: return visitUDIV(N);
603 case ISD::SREM: return visitSREM(N);
604 case ISD::UREM: return visitUREM(N);
605 case ISD::MULHU: return visitMULHU(N);
606 case ISD::MULHS: return visitMULHS(N);
607 case ISD::AND: return visitAND(N);
608 case ISD::OR: return visitOR(N);
609 case ISD::XOR: return visitXOR(N);
610 case ISD::SHL: return visitSHL(N);
611 case ISD::SRA: return visitSRA(N);
612 case ISD::SRL: return visitSRL(N);
613 case ISD::CTLZ: return visitCTLZ(N);
614 case ISD::CTTZ: return visitCTTZ(N);
615 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000616 case ISD::SELECT: return visitSELECT(N);
617 case ISD::SELECT_CC: return visitSELECT_CC(N);
618 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000619 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
620 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
621 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
622 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000623 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000624 case ISD::FADD: return visitFADD(N);
625 case ISD::FSUB: return visitFSUB(N);
626 case ISD::FMUL: return visitFMUL(N);
627 case ISD::FDIV: return visitFDIV(N);
628 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000629 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000630 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
631 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
632 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
633 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
634 case ISD::FP_ROUND: return visitFP_ROUND(N);
635 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
636 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
637 case ISD::FNEG: return visitFNEG(N);
638 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000639 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000640 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000641 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000642 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000643 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000644 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000645}
646
Nate Begeman83e75ec2005-09-06 04:43:02 +0000647SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000648 std::vector<SDOperand> Ops;
649 bool Changed = false;
650
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651 // If the token factor has two operands and one is the entry token, replace
652 // the token factor with the other operand.
653 if (N->getNumOperands() == 2) {
654 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000655 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000656 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000657 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000658 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000659
Nate Begemanded49632005-10-13 03:11:28 +0000660 // fold (tokenfactor (tokenfactor)) -> tokenfactor
661 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
662 SDOperand Op = N->getOperand(i);
663 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000664 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000665 Changed = true;
666 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
667 Ops.push_back(Op.getOperand(j));
668 } else {
669 Ops.push_back(Op);
670 }
671 }
672 if (Changed)
673 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000674 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000675}
676
Nate Begeman83e75ec2005-09-06 04:43:02 +0000677SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000678 SDOperand N0 = N->getOperand(0);
679 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000680 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
681 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000682 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000683
684 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000685 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000686 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000687 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000688 if (N0C && !N1C)
689 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000690 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000691 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000692 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000693 // fold ((c1-A)+c2) -> (c1+c2)-A
694 if (N1C && N0.getOpcode() == ISD::SUB)
695 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
696 return DAG.getNode(ISD::SUB, VT,
697 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
698 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000699 // reassociate add
700 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
701 if (RADD.Val != 0)
702 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000703 // fold ((0-A) + B) -> B-A
704 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
705 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000706 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000707 // fold (A + (0-B)) -> A-B
708 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
709 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000710 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000711 // fold (A+(B-A)) -> B
712 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000713 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000714
Evan Cheng860771d2006-03-01 01:09:54 +0000715 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemanb0d04a72006-02-18 02:40:58 +0000716 return SDOperand();
Chris Lattner947c2892006-03-13 06:51:27 +0000717
718 // fold (a+b) -> (a|b) iff a and b share no bits.
719 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
720 uint64_t LHSZero, LHSOne;
721 uint64_t RHSZero, RHSOne;
722 uint64_t Mask = MVT::getIntVTBitMask(VT);
723 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
724 if (LHSZero) {
725 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
726
727 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
728 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
729 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
730 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
731 return DAG.getNode(ISD::OR, VT, N0, N1);
732 }
733 }
734
Nate Begeman83e75ec2005-09-06 04:43:02 +0000735 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000736}
737
Nate Begeman83e75ec2005-09-06 04:43:02 +0000738SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000739 SDOperand N0 = N->getOperand(0);
740 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000741 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
742 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000743 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000744
Chris Lattner854077d2005-10-17 01:07:11 +0000745 // fold (sub x, x) -> 0
746 if (N0 == N1)
747 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000748 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000749 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000750 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000751 // fold (sub x, c) -> (add x, -c)
752 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000753 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000754 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000755 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000756 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000757 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000758 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000759 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000760 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000761}
762
Nate Begeman83e75ec2005-09-06 04:43:02 +0000763SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000764 SDOperand N0 = N->getOperand(0);
765 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000766 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
767 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000768 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000769
770 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000771 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000772 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000773 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000774 if (N0C && !N1C)
775 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000776 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000777 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000778 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000779 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000780 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000781 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000782 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000783 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000784 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000785 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000786 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000787 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
788 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
789 // FIXME: If the input is something that is easily negated (e.g. a
790 // single-use add), we should put the negate there.
791 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
792 DAG.getNode(ISD::SHL, VT, N0,
793 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
794 TLI.getShiftAmountTy())));
795 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000796
797 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
798 if (N1C && N0.getOpcode() == ISD::SHL &&
799 isa<ConstantSDNode>(N0.getOperand(1))) {
800 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000801 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000802 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
803 }
804
805 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
806 // use.
807 {
808 SDOperand Sh(0,0), Y(0,0);
809 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
810 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
811 N0.Val->hasOneUse()) {
812 Sh = N0; Y = N1;
813 } else if (N1.getOpcode() == ISD::SHL &&
814 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
815 Sh = N1; Y = N0;
816 }
817 if (Sh.Val) {
818 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
819 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
820 }
821 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000822 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
823 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
824 isa<ConstantSDNode>(N0.getOperand(1))) {
825 return DAG.getNode(ISD::ADD, VT,
826 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
827 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
828 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000829
Nate Begemancd4d58c2006-02-03 06:46:56 +0000830 // reassociate mul
831 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
832 if (RMUL.Val != 0)
833 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000834 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000835}
836
Nate Begeman83e75ec2005-09-06 04:43:02 +0000837SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838 SDOperand N0 = N->getOperand(0);
839 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000840 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
841 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000842 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000843
844 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000845 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000846 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000847 // fold (sdiv X, 1) -> X
848 if (N1C && N1C->getSignExtended() == 1LL)
849 return N0;
850 // fold (sdiv X, -1) -> 0-X
851 if (N1C && N1C->isAllOnesValue())
852 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000853 // If we know the sign bits of both operands are zero, strength reduce to a
854 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
855 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000856 if (TLI.MaskedValueIsZero(N1, SignBit) &&
857 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000858 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000859 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000860 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000861 (isPowerOf2_64(N1C->getSignExtended()) ||
862 isPowerOf2_64(-N1C->getSignExtended()))) {
863 // If dividing by powers of two is cheap, then don't perform the following
864 // fold.
865 if (TLI.isPow2DivCheap())
866 return SDOperand();
867 int64_t pow2 = N1C->getSignExtended();
868 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000869 unsigned lg2 = Log2_64(abs2);
870 // Splat the sign bit into the register
871 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000872 DAG.getConstant(MVT::getSizeInBits(VT)-1,
873 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000874 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000875 // Add (N0 < 0) ? abs2 - 1 : 0;
876 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
877 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000878 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000879 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000880 AddToWorkList(SRL.Val);
881 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000882 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
883 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000884 // If we're dividing by a positive value, we're done. Otherwise, we must
885 // negate the result.
886 if (pow2 > 0)
887 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000888 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000889 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
890 }
Nate Begeman69575232005-10-20 02:15:44 +0000891 // if integer divide is expensive and we satisfy the requirements, emit an
892 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000893 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000894 !TLI.isIntDivCheap()) {
895 SDOperand Op = BuildSDIV(N);
896 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000897 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000898 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000899}
900
Nate Begeman83e75ec2005-09-06 04:43:02 +0000901SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000902 SDOperand N0 = N->getOperand(0);
903 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000904 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
905 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000906 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000907
908 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000909 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000910 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000912 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000913 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000914 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000915 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000916 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
917 if (N1.getOpcode() == ISD::SHL) {
918 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
919 if (isPowerOf2_64(SHC->getValue())) {
920 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000921 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
922 DAG.getConstant(Log2_64(SHC->getValue()),
923 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000924 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000925 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000926 }
927 }
928 }
Nate Begeman69575232005-10-20 02:15:44 +0000929 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000930 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
931 SDOperand Op = BuildUDIV(N);
932 if (Op.Val) return Op;
933 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000934 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000935}
936
Nate Begeman83e75ec2005-09-06 04:43:02 +0000937SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938 SDOperand N0 = N->getOperand(0);
939 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000940 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
941 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000942 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000943
944 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000945 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000946 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000947 // If we know the sign bits of both operands are zero, strength reduce to a
948 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
949 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000950 if (TLI.MaskedValueIsZero(N1, SignBit) &&
951 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000952 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000953 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000954}
955
Nate Begeman83e75ec2005-09-06 04:43:02 +0000956SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957 SDOperand N0 = N->getOperand(0);
958 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000959 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
960 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000961 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000962
963 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000964 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000965 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000966 // fold (urem x, pow2) -> (and x, pow2-1)
967 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000968 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000969 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
970 if (N1.getOpcode() == ISD::SHL) {
971 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
972 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000973 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000974 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000975 return DAG.getNode(ISD::AND, VT, N0, Add);
976 }
977 }
978 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980}
981
Nate Begeman83e75ec2005-09-06 04:43:02 +0000982SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 SDOperand N0 = N->getOperand(0);
984 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000985 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000986
987 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000988 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000989 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000990 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000991 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000992 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
993 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000994 TLI.getShiftAmountTy()));
995 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000996}
997
Nate Begeman83e75ec2005-09-06 04:43:02 +0000998SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000999 SDOperand N0 = N->getOperand(0);
1000 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001001 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002
1003 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001004 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001005 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001007 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001008 return DAG.getConstant(0, N0.getValueType());
1009 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010}
1011
Nate Begeman83e75ec2005-09-06 04:43:02 +00001012SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001013 SDOperand N0 = N->getOperand(0);
1014 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001015 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001016 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1017 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +00001019 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001020
1021 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001022 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001023 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001024 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001025 if (N0C && !N1C)
1026 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001027 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001028 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001029 return N0;
1030 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001031 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001032 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001033 // reassociate and
1034 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1035 if (RAND.Val != 0)
1036 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001037 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001038 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001039 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001040 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001041 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001042 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1043 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001044 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001045 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001046 ~N1C->getValue() & InMask)) {
1047 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1048 N0.getOperand(0));
1049
1050 // Replace uses of the AND with uses of the Zero extend node.
1051 CombineTo(N, Zext);
1052
Chris Lattner3603cd62006-02-02 07:17:31 +00001053 // We actually want to replace all uses of the any_extend with the
1054 // zero_extend, to avoid duplicating things. This will later cause this
1055 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001056 CombineTo(N0.Val, Zext);
Chris Lattner3603cd62006-02-02 07:17:31 +00001057 return SDOperand();
1058 }
1059 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001060 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1061 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1062 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1063 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1064
1065 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1066 MVT::isInteger(LL.getValueType())) {
1067 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1068 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1069 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001070 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001071 return DAG.getSetCC(VT, ORNode, LR, Op1);
1072 }
1073 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1074 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1075 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001076 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001077 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1078 }
1079 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1080 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1081 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001082 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001083 return DAG.getSetCC(VT, ORNode, LR, Op1);
1084 }
1085 }
1086 // canonicalize equivalent to ll == rl
1087 if (LL == RR && LR == RL) {
1088 Op1 = ISD::getSetCCSwappedOperands(Op1);
1089 std::swap(RL, RR);
1090 }
1091 if (LL == RL && LR == RR) {
1092 bool isInteger = MVT::isInteger(LL.getValueType());
1093 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1094 if (Result != ISD::SETCC_INVALID)
1095 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1096 }
1097 }
1098 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1099 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1100 N1.getOpcode() == ISD::ZERO_EXTEND &&
1101 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1102 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1103 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001104 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001105 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1106 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001107 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001108 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001109 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1110 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001111 N0.getOperand(1) == N1.getOperand(1)) {
1112 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1113 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001114 AddToWorkList(ANDNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001115 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1116 }
Nate Begemande996292006-02-03 22:24:05 +00001117 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1118 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner012f2412006-02-17 21:58:01 +00001119 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001120 return SDOperand();
Nate Begemanded49632005-10-13 03:11:28 +00001121 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001122 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001123 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001124 // If we zero all the possible extended bits, then we can turn this into
1125 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001126 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001127 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001128 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1129 N0.getOperand(1), N0.getOperand(2),
1130 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001131 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001132 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001133 return SDOperand();
1134 }
1135 }
1136 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001137 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001138 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001139 // If we zero all the possible extended bits, then we can turn this into
1140 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001141 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001142 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001143 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1144 N0.getOperand(1), N0.getOperand(2),
1145 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001146 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001147 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001148 return SDOperand();
1149 }
1150 }
Chris Lattner15045b62006-02-28 06:35:35 +00001151
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001152 // fold (and (load x), 255) -> (zextload x, i8)
1153 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1154 if (N1C &&
1155 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1156 N0.getOpcode() == ISD::ZEXTLOAD) &&
1157 N0.hasOneUse()) {
1158 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001159 if (N1C->getValue() == 255)
1160 EVT = MVT::i8;
1161 else if (N1C->getValue() == 65535)
1162 EVT = MVT::i16;
1163 else if (N1C->getValue() == ~0U)
1164 EVT = MVT::i32;
1165 else
1166 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001167
1168 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1169 cast<VTSDNode>(N0.getOperand(3))->getVT();
1170 if (EVT != MVT::Other && LoadedVT > EVT) {
Chris Lattner15045b62006-02-28 06:35:35 +00001171 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1172 // For big endian targets, we need to add an offset to the pointer to load
1173 // the correct bytes. For little endian systems, we merely need to read
1174 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001175 unsigned PtrOff =
1176 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1177 SDOperand NewPtr = N0.getOperand(1);
1178 if (!TLI.isLittleEndian())
1179 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1180 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001181 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001182 SDOperand Load =
1183 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1184 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001185 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001186 CombineTo(N0.Val, Load, Load.getValue(1));
1187 return SDOperand();
1188 }
1189 }
1190
Nate Begeman83e75ec2005-09-06 04:43:02 +00001191 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001192}
1193
Nate Begeman83e75ec2005-09-06 04:43:02 +00001194SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001195 SDOperand N0 = N->getOperand(0);
1196 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001197 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001198 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1199 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001200 MVT::ValueType VT = N1.getValueType();
1201 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001202
1203 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001204 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001205 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001206 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001207 if (N0C && !N1C)
1208 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001209 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001210 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001211 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001212 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001213 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001214 return N1;
1215 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001216 if (N1C &&
1217 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001218 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001219 // reassociate or
1220 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1221 if (ROR.Val != 0)
1222 return ROR;
1223 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1224 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001225 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001226 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1227 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1228 N1),
1229 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001230 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001231 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1232 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1233 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1234 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1235
1236 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1237 MVT::isInteger(LL.getValueType())) {
1238 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1239 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1240 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1241 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1242 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001243 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001244 return DAG.getSetCC(VT, ORNode, LR, Op1);
1245 }
1246 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1247 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1248 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1249 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1250 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001251 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001252 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1253 }
1254 }
1255 // canonicalize equivalent to ll == rl
1256 if (LL == RR && LR == RL) {
1257 Op1 = ISD::getSetCCSwappedOperands(Op1);
1258 std::swap(RL, RR);
1259 }
1260 if (LL == RL && LR == RR) {
1261 bool isInteger = MVT::isInteger(LL.getValueType());
1262 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1263 if (Result != ISD::SETCC_INVALID)
1264 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1265 }
1266 }
1267 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1268 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1269 N1.getOpcode() == ISD::ZERO_EXTEND &&
1270 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1271 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1272 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001273 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001274 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1275 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001276 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1277 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1278 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1279 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1280 N0.getOperand(1) == N1.getOperand(1)) {
1281 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1282 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001283 AddToWorkList(ORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001284 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1285 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001286 // canonicalize shl to left side in a shl/srl pair, to match rotate
1287 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1288 std::swap(N0, N1);
1289 // check for rotl, rotr
1290 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1291 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001292 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001293 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1294 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1295 N1.getOperand(1).getOpcode() == ISD::Constant) {
1296 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1297 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1298 if ((c1val + c2val) == OpSizeInBits)
1299 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1300 }
1301 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1302 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1303 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1304 if (ConstantSDNode *SUBC =
1305 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1306 if (SUBC->getValue() == OpSizeInBits)
1307 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1308 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1309 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1310 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1311 if (ConstantSDNode *SUBC =
1312 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1313 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001314 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001315 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1316 N1.getOperand(1));
1317 else
1318 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1319 N0.getOperand(1));
1320 }
1321 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001322 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001323}
1324
Nate Begeman83e75ec2005-09-06 04:43:02 +00001325SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001326 SDOperand N0 = N->getOperand(0);
1327 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001328 SDOperand LHS, RHS, CC;
1329 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1330 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001331 MVT::ValueType VT = N0.getValueType();
1332
1333 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001334 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001335 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001336 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001337 if (N0C && !N1C)
1338 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001339 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001340 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001341 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001342 // reassociate xor
1343 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1344 if (RXOR.Val != 0)
1345 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001346 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001347 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1348 bool isInt = MVT::isInteger(LHS.getValueType());
1349 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1350 isInt);
1351 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001354 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001355 assert(0 && "Unhandled SetCC Equivalent!");
1356 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001357 }
Nate Begeman99801192005-09-07 23:25:52 +00001358 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1359 if (N1C && N1C->getValue() == 1 &&
1360 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001362 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1363 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1365 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001366 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001367 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001368 }
1369 }
Nate Begeman99801192005-09-07 23:25:52 +00001370 // fold !(x or y) -> (!x and !y) iff x or y are constants
1371 if (N1C && N1C->isAllOnesValue() &&
1372 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001373 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001374 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1375 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1377 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001378 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001379 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001380 }
1381 }
Nate Begeman223df222005-09-08 20:18:10 +00001382 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1383 if (N1C && N0.getOpcode() == ISD::XOR) {
1384 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1385 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1386 if (N00C)
1387 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1388 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1389 if (N01C)
1390 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1391 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1392 }
1393 // fold (xor x, x) -> 0
1394 if (N0 == N1)
1395 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001396 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1397 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1398 N1.getOpcode() == ISD::ZERO_EXTEND &&
1399 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1400 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1401 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001402 AddToWorkList(XORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001403 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1404 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001405 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1406 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1407 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1408 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1409 N0.getOperand(1) == N1.getOperand(1)) {
1410 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1411 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001412 AddToWorkList(XORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001413 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1414 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001415 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416}
1417
Nate Begeman83e75ec2005-09-06 04:43:02 +00001418SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001419 SDOperand N0 = N->getOperand(0);
1420 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001421 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1422 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001423 MVT::ValueType VT = N0.getValueType();
1424 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1425
1426 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001427 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001428 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001430 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001431 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001432 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001433 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001434 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001436 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001439 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001440 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001441 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001442 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001443 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001444 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001445 N0.getOperand(1).getOpcode() == ISD::Constant) {
1446 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001447 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001449 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001450 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001451 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001452 }
1453 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1454 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001455 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 N0.getOperand(1).getOpcode() == ISD::Constant) {
1457 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001458 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1460 DAG.getConstant(~0ULL << c1, VT));
1461 if (c2 > c1)
1462 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001463 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001464 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001465 return DAG.getNode(ISD::SRL, VT, Mask,
1466 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001467 }
1468 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001469 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001470 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001471 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001472 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1473 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1474 isa<ConstantSDNode>(N0.getOperand(1))) {
1475 return DAG.getNode(ISD::ADD, VT,
1476 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1477 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1478 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001479 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480}
1481
Nate Begeman83e75ec2005-09-06 04:43:02 +00001482SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001483 SDOperand N0 = N->getOperand(0);
1484 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001485 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1486 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001487 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001488
1489 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001490 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001491 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001492 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001493 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001494 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001496 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001497 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001498 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001499 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001500 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001501 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001502 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001503 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001504 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1505 // sext_inreg.
1506 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1507 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1508 MVT::ValueType EVT;
1509 switch (LowBits) {
1510 default: EVT = MVT::Other; break;
1511 case 1: EVT = MVT::i1; break;
1512 case 8: EVT = MVT::i8; break;
1513 case 16: EVT = MVT::i16; break;
1514 case 32: EVT = MVT::i32; break;
1515 }
1516 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1517 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1518 DAG.getValueType(EVT));
1519 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001520
1521 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1522 if (N1C && N0.getOpcode() == ISD::SRA) {
1523 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1524 unsigned Sum = N1C->getValue() + C1->getValue();
1525 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1526 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1527 DAG.getConstant(Sum, N1C->getValueType(0)));
1528 }
1529 }
1530
Nate Begeman1d4d4142005-09-01 00:19:25 +00001531 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001532 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001533 return DAG.getNode(ISD::SRL, VT, N0, N1);
1534 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001535}
1536
Nate Begeman83e75ec2005-09-06 04:43:02 +00001537SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001538 SDOperand N0 = N->getOperand(0);
1539 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001540 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1541 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001542 MVT::ValueType VT = N0.getValueType();
1543 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1544
1545 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001546 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001547 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001549 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001550 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001552 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001553 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001554 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001555 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001556 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001557 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001558 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001559 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001560 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001561 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001562 N0.getOperand(1).getOpcode() == ISD::Constant) {
1563 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001564 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001565 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001566 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001568 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001569 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001570 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001571}
1572
Nate Begeman83e75ec2005-09-06 04:43:02 +00001573SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001574 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001575 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001576 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577
1578 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001579 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001580 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001581 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001582}
1583
Nate Begeman83e75ec2005-09-06 04:43:02 +00001584SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001585 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001586 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001587 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001588
1589 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001590 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001591 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001592 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001593}
1594
Nate Begeman83e75ec2005-09-06 04:43:02 +00001595SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001596 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001597 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001598 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001599
1600 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001601 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001602 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001603 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001604}
1605
Nate Begeman452d7be2005-09-16 00:54:12 +00001606SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1607 SDOperand N0 = N->getOperand(0);
1608 SDOperand N1 = N->getOperand(1);
1609 SDOperand N2 = N->getOperand(2);
1610 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1611 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1612 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1613 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001614
Nate Begeman452d7be2005-09-16 00:54:12 +00001615 // fold select C, X, X -> X
1616 if (N1 == N2)
1617 return N1;
1618 // fold select true, X, Y -> X
1619 if (N0C && !N0C->isNullValue())
1620 return N1;
1621 // fold select false, X, Y -> Y
1622 if (N0C && N0C->isNullValue())
1623 return N2;
1624 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001625 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001626 return DAG.getNode(ISD::OR, VT, N0, N2);
1627 // fold select C, 0, X -> ~C & X
1628 // FIXME: this should check for C type == X type, not i1?
1629 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1630 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001631 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001632 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1633 }
1634 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001635 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001636 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001637 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001638 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1639 }
1640 // fold select C, X, 0 -> C & X
1641 // FIXME: this should check for C type == X type, not i1?
1642 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1643 return DAG.getNode(ISD::AND, VT, N0, N1);
1644 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1645 if (MVT::i1 == VT && N0 == N1)
1646 return DAG.getNode(ISD::OR, VT, N0, N2);
1647 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1648 if (MVT::i1 == VT && N0 == N2)
1649 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001650 // If we can fold this based on the true/false value, do so.
1651 if (SimplifySelectOps(N, N1, N2))
1652 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001653 // fold selects based on a setcc into other things, such as min/max/abs
1654 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001655 // FIXME:
1656 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1657 // having to say they don't support SELECT_CC on every type the DAG knows
1658 // about, since there is no way to mark an opcode illegal at all value types
1659 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1660 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1661 N1, N2, N0.getOperand(2));
1662 else
1663 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001664 return SDOperand();
1665}
1666
1667SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001668 SDOperand N0 = N->getOperand(0);
1669 SDOperand N1 = N->getOperand(1);
1670 SDOperand N2 = N->getOperand(2);
1671 SDOperand N3 = N->getOperand(3);
1672 SDOperand N4 = N->getOperand(4);
1673 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1674 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1675 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1676 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1677
1678 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001679 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001680 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1681
Nate Begeman44728a72005-09-19 22:34:01 +00001682 // fold select_cc lhs, rhs, x, x, cc -> x
1683 if (N2 == N3)
1684 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001685
1686 // If we can fold this based on the true/false value, do so.
1687 if (SimplifySelectOps(N, N2, N3))
1688 return SDOperand();
1689
Nate Begeman44728a72005-09-19 22:34:01 +00001690 // fold select_cc into other things, such as min/max/abs
1691 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001692}
1693
1694SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1695 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1696 cast<CondCodeSDNode>(N->getOperand(2))->get());
1697}
1698
Nate Begeman83e75ec2005-09-06 04:43:02 +00001699SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001700 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001701 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001702 MVT::ValueType VT = N->getValueType(0);
1703
Nate Begeman1d4d4142005-09-01 00:19:25 +00001704 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001705 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001706 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001707 // fold (sext (sext x)) -> (sext x)
1708 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001709 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001710 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001711 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1712 (!AfterLegalize ||
1713 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001714 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1715 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001716 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001717 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1718 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001719 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1720 N0.getOperand(1), N0.getOperand(2),
1721 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001722 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001723 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1724 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001725 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001726 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001727
1728 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1729 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1730 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1731 N0.hasOneUse()) {
1732 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1733 N0.getOperand(1), N0.getOperand(2),
1734 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001735 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001736 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1737 ExtLoad.getValue(1));
1738 return SDOperand();
1739 }
1740
Nate Begeman83e75ec2005-09-06 04:43:02 +00001741 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001742}
1743
Nate Begeman83e75ec2005-09-06 04:43:02 +00001744SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001745 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001746 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747 MVT::ValueType VT = N->getValueType(0);
1748
Nate Begeman1d4d4142005-09-01 00:19:25 +00001749 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001750 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001751 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001752 // fold (zext (zext x)) -> (zext x)
1753 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001754 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001755 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1756 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001757 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001758 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001759 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001760 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1761 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001762 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1763 N0.getOperand(1), N0.getOperand(2),
1764 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001765 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001766 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1767 ExtLoad.getValue(1));
1768 return SDOperand();
1769 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001770
1771 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1772 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1773 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1774 N0.hasOneUse()) {
1775 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1776 N0.getOperand(1), N0.getOperand(2),
1777 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001778 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001779 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1780 ExtLoad.getValue(1));
1781 return SDOperand();
1782 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001783 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001784}
1785
Nate Begeman83e75ec2005-09-06 04:43:02 +00001786SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001787 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001788 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001789 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001790 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001791 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001792 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001793
Nate Begeman1d4d4142005-09-01 00:19:25 +00001794 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001795 if (N0C) {
1796 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001797 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001798 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001799 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001800 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001801 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001802 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001803 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001804 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1805 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1806 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001807 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001808 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001809 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1810 if (N0.getOpcode() == ISD::AssertSext &&
1811 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001812 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001813 }
1814 // fold (sext_in_reg (sextload x)) -> (sextload x)
1815 if (N0.getOpcode() == ISD::SEXTLOAD &&
1816 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001817 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001818 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001819 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001820 if (N0.getOpcode() == ISD::SETCC &&
1821 TLI.getSetCCResultContents() ==
1822 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001823 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001824 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001825 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001826 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001827 // fold (sext_in_reg (srl x)) -> sra x
1828 if (N0.getOpcode() == ISD::SRL &&
1829 N0.getOperand(1).getOpcode() == ISD::Constant &&
1830 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1831 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1832 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001833 }
Nate Begemanded49632005-10-13 03:11:28 +00001834 // fold (sext_inreg (extload x)) -> (sextload x)
1835 if (N0.getOpcode() == ISD::EXTLOAD &&
1836 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001837 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001838 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1839 N0.getOperand(1), N0.getOperand(2),
1840 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001841 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001842 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001843 return SDOperand();
1844 }
1845 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001846 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001847 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001848 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001849 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1850 N0.getOperand(1), N0.getOperand(2),
1851 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001852 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001853 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001854 return SDOperand();
1855 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001856 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857}
1858
Nate Begeman83e75ec2005-09-06 04:43:02 +00001859SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001860 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001861 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001862 MVT::ValueType VT = N->getValueType(0);
1863
1864 // noop truncate
1865 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001866 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001867 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001868 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001869 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001870 // fold (truncate (truncate x)) -> (truncate x)
1871 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001872 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001873 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1874 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1875 if (N0.getValueType() < VT)
1876 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001877 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001878 else if (N0.getValueType() > VT)
1879 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001880 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001881 else
1882 // if the source and dest are the same type, we can drop both the extend
1883 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001884 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001886 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001887 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001888 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1889 "Cannot truncate to larger type!");
1890 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001891 // For big endian targets, we need to add an offset to the pointer to load
1892 // the correct bytes. For little endian systems, we merely need to read
1893 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001894 uint64_t PtrOff =
1895 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001896 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1897 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1898 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001899 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001900 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001901 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001902 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001903 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001904 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001905 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001906}
1907
Chris Lattner94683772005-12-23 05:30:37 +00001908SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1909 SDOperand N0 = N->getOperand(0);
1910 MVT::ValueType VT = N->getValueType(0);
1911
1912 // If the input is a constant, let getNode() fold it.
1913 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1914 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1915 if (Res.Val != N) return Res;
1916 }
1917
Chris Lattnerc8547d82005-12-23 05:37:50 +00001918 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1919 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1920
Chris Lattner57104102005-12-23 05:44:41 +00001921 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001922 // FIXME: These xforms need to know that the resultant load doesn't need a
1923 // higher alignment than the original!
1924 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001925 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1926 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001927 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00001928 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1929 Load.getValue(1));
1930 return Load;
1931 }
1932
Chris Lattner94683772005-12-23 05:30:37 +00001933 return SDOperand();
1934}
1935
Chris Lattner01b3d732005-09-28 22:28:18 +00001936SDOperand DAGCombiner::visitFADD(SDNode *N) {
1937 SDOperand N0 = N->getOperand(0);
1938 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001939 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1940 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001941 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001942
1943 // fold (fadd c1, c2) -> c1+c2
1944 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001945 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001946 // canonicalize constant to RHS
1947 if (N0CFP && !N1CFP)
1948 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001949 // fold (A + (-B)) -> A-B
1950 if (N1.getOpcode() == ISD::FNEG)
1951 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001952 // fold ((-A) + B) -> B-A
1953 if (N0.getOpcode() == ISD::FNEG)
1954 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001955 return SDOperand();
1956}
1957
1958SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1959 SDOperand N0 = N->getOperand(0);
1960 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001961 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1962 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001963 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001964
1965 // fold (fsub c1, c2) -> c1-c2
1966 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001967 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001968 // fold (A-(-B)) -> A+B
1969 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001970 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001971 return SDOperand();
1972}
1973
1974SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1975 SDOperand N0 = N->getOperand(0);
1976 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001977 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1978 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001979 MVT::ValueType VT = N->getValueType(0);
1980
Nate Begeman11af4ea2005-10-17 20:40:11 +00001981 // fold (fmul c1, c2) -> c1*c2
1982 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001983 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001984 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001985 if (N0CFP && !N1CFP)
1986 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001987 // fold (fmul X, 2.0) -> (fadd X, X)
1988 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1989 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001990 return SDOperand();
1991}
1992
1993SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1994 SDOperand N0 = N->getOperand(0);
1995 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001996 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1997 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001998 MVT::ValueType VT = N->getValueType(0);
1999
Nate Begemana148d982006-01-18 22:35:16 +00002000 // fold (fdiv c1, c2) -> c1/c2
2001 if (N0CFP && N1CFP)
2002 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002003 return SDOperand();
2004}
2005
2006SDOperand DAGCombiner::visitFREM(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 (frem c1, c2) -> fmod(c1,c2)
2014 if (N0CFP && N1CFP)
2015 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002016 return SDOperand();
2017}
2018
Chris Lattner12d83032006-03-05 05:30:57 +00002019SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2020 SDOperand N0 = N->getOperand(0);
2021 SDOperand N1 = N->getOperand(1);
2022 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2023 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2024 MVT::ValueType VT = N->getValueType(0);
2025
2026 if (N0CFP && N1CFP) // Constant fold
2027 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2028
2029 if (N1CFP) {
2030 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2031 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2032 union {
2033 double d;
2034 int64_t i;
2035 } u;
2036 u.d = N1CFP->getValue();
2037 if (u.i >= 0)
2038 return DAG.getNode(ISD::FABS, VT, N0);
2039 else
2040 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2041 }
2042
2043 // copysign(fabs(x), y) -> copysign(x, y)
2044 // copysign(fneg(x), y) -> copysign(x, y)
2045 // copysign(copysign(x,z), y) -> copysign(x, y)
2046 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2047 N0.getOpcode() == ISD::FCOPYSIGN)
2048 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2049
2050 // copysign(x, abs(y)) -> abs(x)
2051 if (N1.getOpcode() == ISD::FABS)
2052 return DAG.getNode(ISD::FABS, VT, N0);
2053
2054 // copysign(x, copysign(y,z)) -> copysign(x, z)
2055 if (N1.getOpcode() == ISD::FCOPYSIGN)
2056 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2057
2058 // copysign(x, fp_extend(y)) -> copysign(x, y)
2059 // copysign(x, fp_round(y)) -> copysign(x, y)
2060 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2061 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2062
2063 return SDOperand();
2064}
2065
2066
Chris Lattner01b3d732005-09-28 22:28:18 +00002067
Nate Begeman83e75ec2005-09-06 04:43:02 +00002068SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002069 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002070 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002071 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002072
2073 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002074 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002075 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002076 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002077}
2078
Nate Begeman83e75ec2005-09-06 04:43:02 +00002079SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002080 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002081 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002082 MVT::ValueType VT = N->getValueType(0);
2083
Nate Begeman1d4d4142005-09-01 00:19:25 +00002084 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002085 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002086 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002087 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002088}
2089
Nate Begeman83e75ec2005-09-06 04:43:02 +00002090SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002091 SDOperand N0 = N->getOperand(0);
2092 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2093 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002094
2095 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002096 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002097 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002098 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002099}
2100
Nate Begeman83e75ec2005-09-06 04:43:02 +00002101SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002102 SDOperand N0 = N->getOperand(0);
2103 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2104 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002105
2106 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002107 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002108 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002109 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002110}
2111
Nate Begeman83e75ec2005-09-06 04:43:02 +00002112SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002113 SDOperand N0 = N->getOperand(0);
2114 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2115 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002116
2117 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002118 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002119 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002120
2121 // fold (fp_round (fp_extend x)) -> x
2122 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2123 return N0.getOperand(0);
2124
2125 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2126 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2127 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2128 AddToWorkList(Tmp.Val);
2129 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2130 }
2131
Nate Begeman83e75ec2005-09-06 04:43:02 +00002132 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002133}
2134
Nate Begeman83e75ec2005-09-06 04:43:02 +00002135SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002136 SDOperand N0 = N->getOperand(0);
2137 MVT::ValueType VT = N->getValueType(0);
2138 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002139 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002140
Nate Begeman1d4d4142005-09-01 00:19:25 +00002141 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002142 if (N0CFP) {
2143 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002144 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002145 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002146 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002147}
2148
Nate Begeman83e75ec2005-09-06 04:43:02 +00002149SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002150 SDOperand N0 = N->getOperand(0);
2151 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2152 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002153
2154 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002155 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002156 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002157 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002158}
2159
Nate Begeman83e75ec2005-09-06 04:43:02 +00002160SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002161 SDOperand N0 = N->getOperand(0);
2162 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2163 MVT::ValueType VT = N->getValueType(0);
2164
2165 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002166 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002167 return DAG.getNode(ISD::FNEG, VT, N0);
2168 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002169 if (N0.getOpcode() == ISD::SUB)
2170 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002171 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002172 if (N0.getOpcode() == ISD::FNEG)
2173 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002174 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002175}
2176
Nate Begeman83e75ec2005-09-06 04:43:02 +00002177SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002178 SDOperand N0 = N->getOperand(0);
2179 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2180 MVT::ValueType VT = N->getValueType(0);
2181
Nate Begeman1d4d4142005-09-01 00:19:25 +00002182 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002183 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002184 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002185 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002186 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002187 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002188 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002189 // fold (fabs (fcopysign x, y)) -> (fabs x)
2190 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2191 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2192
Nate Begeman83e75ec2005-09-06 04:43:02 +00002193 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002194}
2195
Nate Begeman44728a72005-09-19 22:34:01 +00002196SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2197 SDOperand Chain = N->getOperand(0);
2198 SDOperand N1 = N->getOperand(1);
2199 SDOperand N2 = N->getOperand(2);
2200 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2201
2202 // never taken branch, fold to chain
2203 if (N1C && N1C->isNullValue())
2204 return Chain;
2205 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002206 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002207 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002208 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2209 // on the target.
2210 if (N1.getOpcode() == ISD::SETCC &&
2211 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2212 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2213 N1.getOperand(0), N1.getOperand(1), N2);
2214 }
Nate Begeman44728a72005-09-19 22:34:01 +00002215 return SDOperand();
2216}
2217
Chris Lattner3ea0b472005-10-05 06:47:48 +00002218// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2219//
Nate Begeman44728a72005-09-19 22:34:01 +00002220SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002221 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2222 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2223
2224 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002225 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2226 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2227
2228 // fold br_cc true, dest -> br dest (unconditional branch)
2229 if (SCCC && SCCC->getValue())
2230 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2231 N->getOperand(4));
2232 // fold br_cc false, dest -> unconditional fall through
2233 if (SCCC && SCCC->isNullValue())
2234 return N->getOperand(0);
2235 // fold to a simpler setcc
2236 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2237 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2238 Simp.getOperand(2), Simp.getOperand(0),
2239 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002240 return SDOperand();
2241}
2242
Chris Lattner01a22022005-10-10 22:04:48 +00002243SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2244 SDOperand Chain = N->getOperand(0);
2245 SDOperand Ptr = N->getOperand(1);
2246 SDOperand SrcValue = N->getOperand(2);
2247
2248 // If this load is directly stored, replace the load value with the stored
2249 // value.
2250 // TODO: Handle store large -> read small portion.
2251 // TODO: Handle TRUNCSTORE/EXTLOAD
2252 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2253 Chain.getOperand(1).getValueType() == N->getValueType(0))
2254 return CombineTo(N, Chain.getOperand(1), Chain);
2255
2256 return SDOperand();
2257}
2258
Chris Lattner87514ca2005-10-10 22:31:19 +00002259SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2260 SDOperand Chain = N->getOperand(0);
2261 SDOperand Value = N->getOperand(1);
2262 SDOperand Ptr = N->getOperand(2);
2263 SDOperand SrcValue = N->getOperand(3);
2264
2265 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002266 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002267 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2268 // Make sure that these stores are the same value type:
2269 // FIXME: we really care that the second store is >= size of the first.
2270 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002271 // Create a new store of Value that replaces both stores.
2272 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002273 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2274 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002275 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2276 PrevStore->getOperand(0), Value, Ptr,
2277 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002278 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002279 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002280 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002281 }
2282
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002283 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002284 // FIXME: This needs to know that the resultant store does not need a
2285 // higher alignment than the original.
2286 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002287 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2288 Ptr, SrcValue);
2289
Chris Lattner87514ca2005-10-10 22:31:19 +00002290 return SDOperand();
2291}
2292
Nate Begeman44728a72005-09-19 22:34:01 +00002293SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002294 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2295
2296 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2297 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2298 // If we got a simplified select_cc node back from SimplifySelectCC, then
2299 // break it down into a new SETCC node, and a new SELECT node, and then return
2300 // the SELECT node, since we were called with a SELECT node.
2301 if (SCC.Val) {
2302 // Check to see if we got a select_cc back (to turn into setcc/select).
2303 // Otherwise, just return whatever node we got back, like fabs.
2304 if (SCC.getOpcode() == ISD::SELECT_CC) {
2305 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2306 SCC.getOperand(0), SCC.getOperand(1),
2307 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002308 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002309 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2310 SCC.getOperand(3), SETCC);
2311 }
2312 return SCC;
2313 }
Nate Begeman44728a72005-09-19 22:34:01 +00002314 return SDOperand();
2315}
2316
Chris Lattner40c62d52005-10-18 06:04:22 +00002317/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2318/// are the two values being selected between, see if we can simplify the
2319/// select.
2320///
2321bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2322 SDOperand RHS) {
2323
2324 // If this is a select from two identical things, try to pull the operation
2325 // through the select.
2326 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2327#if 0
2328 std::cerr << "SELECT: ["; LHS.Val->dump();
2329 std::cerr << "] ["; RHS.Val->dump();
2330 std::cerr << "]\n";
2331#endif
2332
2333 // If this is a load and the token chain is identical, replace the select
2334 // of two loads with a load through a select of the address to load from.
2335 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2336 // constants have been dropped into the constant pool.
2337 if ((LHS.getOpcode() == ISD::LOAD ||
2338 LHS.getOpcode() == ISD::EXTLOAD ||
2339 LHS.getOpcode() == ISD::ZEXTLOAD ||
2340 LHS.getOpcode() == ISD::SEXTLOAD) &&
2341 // Token chains must be identical.
2342 LHS.getOperand(0) == RHS.getOperand(0) &&
2343 // If this is an EXTLOAD, the VT's must match.
2344 (LHS.getOpcode() == ISD::LOAD ||
2345 LHS.getOperand(3) == RHS.getOperand(3))) {
2346 // FIXME: this conflates two src values, discarding one. This is not
2347 // the right thing to do, but nothing uses srcvalues now. When they do,
2348 // turn SrcValue into a list of locations.
2349 SDOperand Addr;
2350 if (TheSelect->getOpcode() == ISD::SELECT)
2351 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2352 TheSelect->getOperand(0), LHS.getOperand(1),
2353 RHS.getOperand(1));
2354 else
2355 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2356 TheSelect->getOperand(0),
2357 TheSelect->getOperand(1),
2358 LHS.getOperand(1), RHS.getOperand(1),
2359 TheSelect->getOperand(4));
2360
2361 SDOperand Load;
2362 if (LHS.getOpcode() == ISD::LOAD)
2363 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2364 Addr, LHS.getOperand(2));
2365 else
2366 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2367 LHS.getOperand(0), Addr, LHS.getOperand(2),
2368 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2369 // Users of the select now use the result of the load.
2370 CombineTo(TheSelect, Load);
2371
2372 // Users of the old loads now use the new load's chain. We know the
2373 // old-load value is dead now.
2374 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2375 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2376 return true;
2377 }
2378 }
2379
2380 return false;
2381}
2382
Nate Begeman44728a72005-09-19 22:34:01 +00002383SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2384 SDOperand N2, SDOperand N3,
2385 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002386
2387 MVT::ValueType VT = N2.getValueType();
2388 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2389 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2390 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2391 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2392
2393 // Determine if the condition we're dealing with is constant
2394 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2395 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2396
2397 // fold select_cc true, x, y -> x
2398 if (SCCC && SCCC->getValue())
2399 return N2;
2400 // fold select_cc false, x, y -> y
2401 if (SCCC && SCCC->getValue() == 0)
2402 return N3;
2403
2404 // Check to see if we can simplify the select into an fabs node
2405 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2406 // Allow either -0.0 or 0.0
2407 if (CFP->getValue() == 0.0) {
2408 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2409 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2410 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2411 N2 == N3.getOperand(0))
2412 return DAG.getNode(ISD::FABS, VT, N0);
2413
2414 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2415 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2416 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2417 N2.getOperand(0) == N3)
2418 return DAG.getNode(ISD::FABS, VT, N3);
2419 }
2420 }
2421
2422 // Check to see if we can perform the "gzip trick", transforming
2423 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2424 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2425 MVT::isInteger(N0.getValueType()) &&
2426 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2427 MVT::ValueType XType = N0.getValueType();
2428 MVT::ValueType AType = N2.getValueType();
2429 if (XType >= AType) {
2430 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002431 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002432 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2433 unsigned ShCtV = Log2_64(N2C->getValue());
2434 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2435 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2436 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00002437 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002438 if (XType > AType) {
2439 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002440 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002441 }
2442 return DAG.getNode(ISD::AND, AType, Shift, N2);
2443 }
2444 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2445 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2446 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002447 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002448 if (XType > AType) {
2449 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002450 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002451 }
2452 return DAG.getNode(ISD::AND, AType, Shift, N2);
2453 }
2454 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002455
2456 // fold select C, 16, 0 -> shl C, 4
2457 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2458 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2459 // Get a SetCC of the condition
2460 // FIXME: Should probably make sure that setcc is legal if we ever have a
2461 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00002462 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00002463 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00002464 if (AfterLegalize) {
2465 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002466 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00002467 } else {
2468 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002469 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00002470 }
Chris Lattner5750df92006-03-01 04:03:14 +00002471 AddToWorkList(SCC.Val);
2472 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00002473 // shl setcc result by log2 n2c
2474 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2475 DAG.getConstant(Log2_64(N2C->getValue()),
2476 TLI.getShiftAmountTy()));
2477 }
2478
Nate Begemanf845b452005-10-08 00:29:44 +00002479 // Check to see if this is the equivalent of setcc
2480 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2481 // otherwise, go ahead with the folds.
2482 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2483 MVT::ValueType XType = N0.getValueType();
2484 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2485 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2486 if (Res.getValueType() != VT)
2487 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2488 return Res;
2489 }
2490
2491 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2492 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2493 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2494 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2495 return DAG.getNode(ISD::SRL, XType, Ctlz,
2496 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2497 TLI.getShiftAmountTy()));
2498 }
2499 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2500 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2501 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2502 N0);
2503 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2504 DAG.getConstant(~0ULL, XType));
2505 return DAG.getNode(ISD::SRL, XType,
2506 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2507 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2508 TLI.getShiftAmountTy()));
2509 }
2510 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2511 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2512 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2513 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2514 TLI.getShiftAmountTy()));
2515 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2516 }
2517 }
2518
2519 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2520 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2521 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2522 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2523 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2524 MVT::ValueType XType = N0.getValueType();
2525 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2526 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2527 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2528 TLI.getShiftAmountTy()));
2529 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002530 AddToWorkList(Shift.Val);
2531 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002532 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2533 }
2534 }
2535 }
2536
Nate Begeman44728a72005-09-19 22:34:01 +00002537 return SDOperand();
2538}
2539
Nate Begeman452d7be2005-09-16 00:54:12 +00002540SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002541 SDOperand N1, ISD::CondCode Cond,
2542 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002543 // These setcc operations always fold.
2544 switch (Cond) {
2545 default: break;
2546 case ISD::SETFALSE:
2547 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2548 case ISD::SETTRUE:
2549 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2550 }
2551
2552 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2553 uint64_t C1 = N1C->getValue();
2554 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2555 uint64_t C0 = N0C->getValue();
2556
2557 // Sign extend the operands if required
2558 if (ISD::isSignedIntSetCC(Cond)) {
2559 C0 = N0C->getSignExtended();
2560 C1 = N1C->getSignExtended();
2561 }
2562
2563 switch (Cond) {
2564 default: assert(0 && "Unknown integer setcc!");
2565 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2566 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2567 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2568 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2569 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2570 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2571 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2572 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2573 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2574 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2575 }
2576 } else {
2577 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2578 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2579 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2580
2581 // If the comparison constant has bits in the upper part, the
2582 // zero-extended value could never match.
2583 if (C1 & (~0ULL << InSize)) {
2584 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2585 switch (Cond) {
2586 case ISD::SETUGT:
2587 case ISD::SETUGE:
2588 case ISD::SETEQ: return DAG.getConstant(0, VT);
2589 case ISD::SETULT:
2590 case ISD::SETULE:
2591 case ISD::SETNE: return DAG.getConstant(1, VT);
2592 case ISD::SETGT:
2593 case ISD::SETGE:
2594 // True if the sign bit of C1 is set.
2595 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2596 case ISD::SETLT:
2597 case ISD::SETLE:
2598 // True if the sign bit of C1 isn't set.
2599 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2600 default:
2601 break;
2602 }
2603 }
2604
2605 // Otherwise, we can perform the comparison with the low bits.
2606 switch (Cond) {
2607 case ISD::SETEQ:
2608 case ISD::SETNE:
2609 case ISD::SETUGT:
2610 case ISD::SETUGE:
2611 case ISD::SETULT:
2612 case ISD::SETULE:
2613 return DAG.getSetCC(VT, N0.getOperand(0),
2614 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2615 Cond);
2616 default:
2617 break; // todo, be more careful with signed comparisons
2618 }
2619 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2620 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2621 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2622 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2623 MVT::ValueType ExtDstTy = N0.getValueType();
2624 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2625
2626 // If the extended part has any inconsistent bits, it cannot ever
2627 // compare equal. In other words, they have to be all ones or all
2628 // zeros.
2629 uint64_t ExtBits =
2630 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2631 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2632 return DAG.getConstant(Cond == ISD::SETNE, VT);
2633
2634 SDOperand ZextOp;
2635 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2636 if (Op0Ty == ExtSrcTy) {
2637 ZextOp = N0.getOperand(0);
2638 } else {
2639 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2640 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2641 DAG.getConstant(Imm, Op0Ty));
2642 }
Chris Lattner5750df92006-03-01 04:03:14 +00002643 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002644 // Otherwise, make this a use of a zext.
2645 return DAG.getSetCC(VT, ZextOp,
2646 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2647 ExtDstTy),
2648 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002649 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2650 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2651 (N0.getOpcode() == ISD::XOR ||
2652 (N0.getOpcode() == ISD::AND &&
2653 N0.getOperand(0).getOpcode() == ISD::XOR &&
2654 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2655 isa<ConstantSDNode>(N0.getOperand(1)) &&
2656 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2657 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2658 // only do this if the top bits are known zero.
2659 if (TLI.MaskedValueIsZero(N1,
2660 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2661 // Okay, get the un-inverted input value.
2662 SDOperand Val;
2663 if (N0.getOpcode() == ISD::XOR)
2664 Val = N0.getOperand(0);
2665 else {
2666 assert(N0.getOpcode() == ISD::AND &&
2667 N0.getOperand(0).getOpcode() == ISD::XOR);
2668 // ((X^1)&1)^1 -> X & 1
2669 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2670 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2671 }
2672 return DAG.getSetCC(VT, Val, N1,
2673 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2674 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002675 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002676
Nate Begeman452d7be2005-09-16 00:54:12 +00002677 uint64_t MinVal, MaxVal;
2678 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2679 if (ISD::isSignedIntSetCC(Cond)) {
2680 MinVal = 1ULL << (OperandBitSize-1);
2681 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2682 MaxVal = ~0ULL >> (65-OperandBitSize);
2683 else
2684 MaxVal = 0;
2685 } else {
2686 MinVal = 0;
2687 MaxVal = ~0ULL >> (64-OperandBitSize);
2688 }
2689
2690 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2691 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2692 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2693 --C1; // X >= C0 --> X > (C0-1)
2694 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2695 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2696 }
2697
2698 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2699 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2700 ++C1; // X <= C0 --> X < (C0+1)
2701 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2702 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2703 }
2704
2705 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2706 return DAG.getConstant(0, VT); // X < MIN --> false
2707
2708 // Canonicalize setgt X, Min --> setne X, Min
2709 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2710 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002711 // Canonicalize setlt X, Max --> setne X, Max
2712 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2713 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002714
2715 // If we have setult X, 1, turn it into seteq X, 0
2716 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2717 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2718 ISD::SETEQ);
2719 // If we have setugt X, Max-1, turn it into seteq X, Max
2720 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2721 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2722 ISD::SETEQ);
2723
2724 // If we have "setcc X, C0", check to see if we can shrink the immediate
2725 // by changing cc.
2726
2727 // SETUGT X, SINTMAX -> SETLT X, 0
2728 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2729 C1 == (~0ULL >> (65-OperandBitSize)))
2730 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2731 ISD::SETLT);
2732
2733 // FIXME: Implement the rest of these.
2734
2735 // Fold bit comparisons when we can.
2736 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2737 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2738 if (ConstantSDNode *AndRHS =
2739 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2740 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2741 // Perform the xform if the AND RHS is a single bit.
2742 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2743 return DAG.getNode(ISD::SRL, VT, N0,
2744 DAG.getConstant(Log2_64(AndRHS->getValue()),
2745 TLI.getShiftAmountTy()));
2746 }
2747 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2748 // (X & 8) == 8 --> (X & 8) >> 3
2749 // Perform the xform if C1 is a single bit.
2750 if ((C1 & (C1-1)) == 0) {
2751 return DAG.getNode(ISD::SRL, VT, N0,
2752 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2753 }
2754 }
2755 }
2756 }
2757 } else if (isa<ConstantSDNode>(N0.Val)) {
2758 // Ensure that the constant occurs on the RHS.
2759 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2760 }
2761
2762 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2763 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2764 double C0 = N0C->getValue(), C1 = N1C->getValue();
2765
2766 switch (Cond) {
2767 default: break; // FIXME: Implement the rest of these!
2768 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2769 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2770 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2771 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2772 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2773 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2774 }
2775 } else {
2776 // Ensure that the constant occurs on the RHS.
2777 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2778 }
2779
2780 if (N0 == N1) {
2781 // We can always fold X == Y for integer setcc's.
2782 if (MVT::isInteger(N0.getValueType()))
2783 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2784 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2785 if (UOF == 2) // FP operators that are undefined on NaNs.
2786 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2787 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2788 return DAG.getConstant(UOF, VT);
2789 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2790 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002791 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002792 if (NewCond != Cond)
2793 return DAG.getSetCC(VT, N0, N1, NewCond);
2794 }
2795
2796 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2797 MVT::isInteger(N0.getValueType())) {
2798 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2799 N0.getOpcode() == ISD::XOR) {
2800 // Simplify (X+Y) == (X+Z) --> Y == Z
2801 if (N0.getOpcode() == N1.getOpcode()) {
2802 if (N0.getOperand(0) == N1.getOperand(0))
2803 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2804 if (N0.getOperand(1) == N1.getOperand(1))
2805 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2806 if (isCommutativeBinOp(N0.getOpcode())) {
2807 // If X op Y == Y op X, try other combinations.
2808 if (N0.getOperand(0) == N1.getOperand(1))
2809 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2810 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002811 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002812 }
2813 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002814
2815 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2816 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2817 // Turn (X+C1) == C2 --> X == C2-C1
2818 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2819 return DAG.getSetCC(VT, N0.getOperand(0),
2820 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2821 N0.getValueType()), Cond);
2822 }
2823
2824 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2825 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002826 // If we know that all of the inverted bits are zero, don't bother
2827 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002828 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002829 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002830 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002831 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002832 }
2833
2834 // Turn (C1-X) == C2 --> X == C1-C2
2835 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2836 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
2837 return DAG.getSetCC(VT, N0.getOperand(1),
2838 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
2839 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00002840 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002841 }
2842 }
2843
Nate Begeman452d7be2005-09-16 00:54:12 +00002844 // Simplify (X+Z) == X --> Z == 0
2845 if (N0.getOperand(0) == N1)
2846 return DAG.getSetCC(VT, N0.getOperand(1),
2847 DAG.getConstant(0, N0.getValueType()), Cond);
2848 if (N0.getOperand(1) == N1) {
2849 if (isCommutativeBinOp(N0.getOpcode()))
2850 return DAG.getSetCC(VT, N0.getOperand(0),
2851 DAG.getConstant(0, N0.getValueType()), Cond);
2852 else {
2853 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2854 // (Z-X) == X --> Z == X<<1
2855 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2856 N1,
2857 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002858 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002859 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2860 }
2861 }
2862 }
2863
2864 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2865 N1.getOpcode() == ISD::XOR) {
2866 // Simplify X == (X+Z) --> Z == 0
2867 if (N1.getOperand(0) == N0) {
2868 return DAG.getSetCC(VT, N1.getOperand(1),
2869 DAG.getConstant(0, N1.getValueType()), Cond);
2870 } else if (N1.getOperand(1) == N0) {
2871 if (isCommutativeBinOp(N1.getOpcode())) {
2872 return DAG.getSetCC(VT, N1.getOperand(0),
2873 DAG.getConstant(0, N1.getValueType()), Cond);
2874 } else {
2875 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2876 // X == (Z-X) --> X<<1 == Z
2877 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2878 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002879 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002880 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2881 }
2882 }
2883 }
2884 }
2885
2886 // Fold away ALL boolean setcc's.
2887 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002888 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002889 switch (Cond) {
2890 default: assert(0 && "Unknown integer setcc!");
2891 case ISD::SETEQ: // X == Y -> (X^Y)^1
2892 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2893 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00002894 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002895 break;
2896 case ISD::SETNE: // X != Y --> (X^Y)
2897 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2898 break;
2899 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2900 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2901 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2902 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00002903 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002904 break;
2905 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2906 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2907 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2908 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00002909 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002910 break;
2911 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2912 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2913 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2914 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00002915 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002916 break;
2917 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2918 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2919 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2920 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2921 break;
2922 }
2923 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00002924 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002925 // FIXME: If running after legalize, we probably can't do this.
2926 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2927 }
2928 return N0;
2929 }
2930
2931 // Could not fold it.
2932 return SDOperand();
2933}
2934
Nate Begeman69575232005-10-20 02:15:44 +00002935/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2936/// return a DAG expression to select that will generate the same value by
2937/// multiplying by a magic number. See:
2938/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2939SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2940 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002941
2942 // Check to see if we can do this.
2943 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2944 return SDOperand(); // BuildSDIV only operates on i32 or i64
2945 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2946 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002947
Nate Begemanc6a454e2005-10-20 17:45:03 +00002948 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002949 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2950
2951 // Multiply the numerator (operand 0) by the magic value
2952 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2953 DAG.getConstant(magics.m, VT));
2954 // If d > 0 and m < 0, add the numerator
2955 if (d > 0 && magics.m < 0) {
2956 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00002957 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002958 }
2959 // If d < 0 and m > 0, subtract the numerator.
2960 if (d < 0 && magics.m > 0) {
2961 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00002962 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002963 }
2964 // Shift right algebraic if shift value is nonzero
2965 if (magics.s > 0) {
2966 Q = DAG.getNode(ISD::SRA, VT, Q,
2967 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002968 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002969 }
2970 // Extract the sign bit and add it to the quotient
2971 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002972 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2973 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002974 AddToWorkList(T.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002975 return DAG.getNode(ISD::ADD, VT, Q, T);
2976}
2977
2978/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2979/// return a DAG expression to select that will generate the same value by
2980/// multiplying by a magic number. See:
2981/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2982SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2983 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002984
2985 // Check to see if we can do this.
2986 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2987 return SDOperand(); // BuildUDIV only operates on i32 or i64
2988 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2989 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002990
2991 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2992 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2993
2994 // Multiply the numerator (operand 0) by the magic value
2995 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2996 DAG.getConstant(magics.m, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002997 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002998
2999 if (magics.a == 0) {
3000 return DAG.getNode(ISD::SRL, VT, Q,
3001 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3002 } else {
3003 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003004 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003005 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
3006 DAG.getConstant(1, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003007 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003008 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003009 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003010 return DAG.getNode(ISD::SRL, VT, NPQ,
3011 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
3012 }
3013}
3014
Nate Begeman1d4d4142005-09-01 00:19:25 +00003015// SelectionDAG::Combine - This is the entry point for the file.
3016//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003017void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003018 /// run - This is the main entry point to this class.
3019 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003020 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003021}