blob: 86784b7ed06e8d356f669a6d229d7d8351029a22 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000025// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman646d7e22005-09-02 21:18:40 +000026// FIXME: divide by zero is currently left unfolded. do we want to turn this
27// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000028// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000029//
30//===----------------------------------------------------------------------===//
31
32#define DEBUG_TYPE "dagcombine"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000038#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000039#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000040#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041using namespace llvm;
42
43namespace {
44 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
45
46 class DAGCombiner {
47 SelectionDAG &DAG;
48 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000049 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000050
51 // Worklist of all of the nodes that need to be simplified.
52 std::vector<SDNode*> WorkList;
53
54 /// AddUsersToWorkList - When an instruction is simplified, add all users of
55 /// the instruction to the work lists because they might get more simplified
56 /// now.
57 ///
58 void AddUsersToWorkList(SDNode *N) {
59 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000060 UI != UE; ++UI)
61 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000062 }
63
64 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000065 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000066 void removeFromWorkList(SDNode *N) {
67 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
68 WorkList.end());
69 }
70
Chris Lattner24664722006-03-01 04:53:38 +000071 public:
Chris Lattner5750df92006-03-01 04:03:14 +000072 void AddToWorkList(SDNode *N) {
73 WorkList.push_back(N);
74 }
75
Chris Lattner01a22022005-10-10 22:04:48 +000076 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000077 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000078 DEBUG(std::cerr << "\nReplacing "; N->dump();
79 std::cerr << "\nWith: "; To[0].Val->dump();
80 std::cerr << " and " << To.size()-1 << " other values\n");
81 std::vector<SDNode*> NowDead;
82 DAG.ReplaceAllUsesWith(N, To, &NowDead);
83
84 // Push the new nodes and any users onto the worklist
85 for (unsigned i = 0, e = To.size(); i != e; ++i) {
86 WorkList.push_back(To[i].Val);
87 AddUsersToWorkList(To[i].Val);
88 }
89
90 // Nodes can end up on the worklist more than once. Make sure we do
91 // not process a node that has been replaced.
92 removeFromWorkList(N);
93 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
94 removeFromWorkList(NowDead[i]);
95
96 // Finally, since the node is now dead, remove it from the graph.
97 DAG.DeleteNode(N);
98 return SDOperand(N, 0);
99 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000100
Chris Lattner24664722006-03-01 04:53:38 +0000101 SDOperand CombineTo(SDNode *N, SDOperand Res) {
102 std::vector<SDOperand> To;
103 To.push_back(Res);
104 return CombineTo(N, To);
105 }
106
107 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
108 std::vector<SDOperand> To;
109 To.push_back(Res0);
110 To.push_back(Res1);
111 return CombineTo(N, To);
112 }
113 private:
114
Chris Lattner012f2412006-02-17 21:58:01 +0000115 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000116 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000117 /// propagation. If so, return true.
118 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000119 TargetLowering::TargetLoweringOpt TLO(DAG);
120 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000121 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
122 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
123 return false;
124
125 // Revisit the node.
126 WorkList.push_back(Op.Val);
127
128 // Replace the old value with the new one.
129 ++NodesCombined;
130 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
131 std::cerr << "\nWith: "; TLO.New.Val->dump());
132
133 std::vector<SDNode*> NowDead;
134 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
135
Chris Lattner7d20d392006-02-20 06:51:04 +0000136 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000137 WorkList.push_back(TLO.New.Val);
138 AddUsersToWorkList(TLO.New.Val);
139
140 // Nodes can end up on the worklist more than once. Make sure we do
141 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000142 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
143 removeFromWorkList(NowDead[i]);
144
Chris Lattner7d20d392006-02-20 06:51:04 +0000145 // Finally, if the node is now dead, remove it from the graph. The node
146 // may not be dead if the replacement process recursively simplified to
147 // something else needing this node.
148 if (TLO.Old.Val->use_empty()) {
149 removeFromWorkList(TLO.Old.Val);
150 DAG.DeleteNode(TLO.Old.Val);
151 }
Chris Lattner012f2412006-02-17 21:58:01 +0000152 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000153 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000154
Nate Begeman1d4d4142005-09-01 00:19:25 +0000155 /// visit - call the node-specific routine that knows how to fold each
156 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000157 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000158
159 // Visitation implementation - Implement dag node combining for different
160 // node types. The semantics are as follows:
161 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000162 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000163 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000164 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000166 SDOperand visitTokenFactor(SDNode *N);
167 SDOperand visitADD(SDNode *N);
168 SDOperand visitSUB(SDNode *N);
169 SDOperand visitMUL(SDNode *N);
170 SDOperand visitSDIV(SDNode *N);
171 SDOperand visitUDIV(SDNode *N);
172 SDOperand visitSREM(SDNode *N);
173 SDOperand visitUREM(SDNode *N);
174 SDOperand visitMULHU(SDNode *N);
175 SDOperand visitMULHS(SDNode *N);
176 SDOperand visitAND(SDNode *N);
177 SDOperand visitOR(SDNode *N);
178 SDOperand visitXOR(SDNode *N);
179 SDOperand visitSHL(SDNode *N);
180 SDOperand visitSRA(SDNode *N);
181 SDOperand visitSRL(SDNode *N);
182 SDOperand visitCTLZ(SDNode *N);
183 SDOperand visitCTTZ(SDNode *N);
184 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000185 SDOperand visitSELECT(SDNode *N);
186 SDOperand visitSELECT_CC(SDNode *N);
187 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000188 SDOperand visitSIGN_EXTEND(SDNode *N);
189 SDOperand visitZERO_EXTEND(SDNode *N);
190 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
191 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000192 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000193 SDOperand visitFADD(SDNode *N);
194 SDOperand visitFSUB(SDNode *N);
195 SDOperand visitFMUL(SDNode *N);
196 SDOperand visitFDIV(SDNode *N);
197 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000198 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000199 SDOperand visitSINT_TO_FP(SDNode *N);
200 SDOperand visitUINT_TO_FP(SDNode *N);
201 SDOperand visitFP_TO_SINT(SDNode *N);
202 SDOperand visitFP_TO_UINT(SDNode *N);
203 SDOperand visitFP_ROUND(SDNode *N);
204 SDOperand visitFP_ROUND_INREG(SDNode *N);
205 SDOperand visitFP_EXTEND(SDNode *N);
206 SDOperand visitFNEG(SDNode *N);
207 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000208 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000209 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000210 SDOperand visitLOAD(SDNode *N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000211 SDOperand visitXEXTLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000212 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000213 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
214 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000215 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000216 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000217 SDOperand visitVVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000218
Nate Begemancd4d58c2006-02-03 06:46:56 +0000219 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
220
Chris Lattner40c62d52005-10-18 06:04:22 +0000221 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000222 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
223 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
224 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000225 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000226 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000227
228 SDOperand BuildSDIV(SDNode *N);
229 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000230public:
231 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000232 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000233
234 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000235 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000236 };
237}
238
Chris Lattner24664722006-03-01 04:53:38 +0000239//===----------------------------------------------------------------------===//
240// TargetLowering::DAGCombinerInfo implementation
241//===----------------------------------------------------------------------===//
242
243void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
244 ((DAGCombiner*)DC)->AddToWorkList(N);
245}
246
247SDOperand TargetLowering::DAGCombinerInfo::
248CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
249 return ((DAGCombiner*)DC)->CombineTo(N, To);
250}
251
252SDOperand TargetLowering::DAGCombinerInfo::
253CombineTo(SDNode *N, SDOperand Res) {
254 return ((DAGCombiner*)DC)->CombineTo(N, Res);
255}
256
257
258SDOperand TargetLowering::DAGCombinerInfo::
259CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
260 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
261}
262
263
264
265
266//===----------------------------------------------------------------------===//
267
268
Nate Begeman69575232005-10-20 02:15:44 +0000269struct ms {
270 int64_t m; // magic number
271 int64_t s; // shift amount
272};
273
274struct mu {
275 uint64_t m; // magic number
276 int64_t a; // add indicator
277 int64_t s; // shift amount
278};
279
280/// magic - calculate the magic numbers required to codegen an integer sdiv as
281/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
282/// or -1.
283static ms magic32(int32_t d) {
284 int32_t p;
285 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
286 const uint32_t two31 = 0x80000000U;
287 struct ms mag;
288
289 ad = abs(d);
290 t = two31 + ((uint32_t)d >> 31);
291 anc = t - 1 - t%ad; // absolute value of nc
292 p = 31; // initialize p
293 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
294 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
295 q2 = two31/ad; // initialize q2 = 2p/abs(d)
296 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
297 do {
298 p = p + 1;
299 q1 = 2*q1; // update q1 = 2p/abs(nc)
300 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
301 if (r1 >= anc) { // must be unsigned comparison
302 q1 = q1 + 1;
303 r1 = r1 - anc;
304 }
305 q2 = 2*q2; // update q2 = 2p/abs(d)
306 r2 = 2*r2; // update r2 = rem(2p/abs(d))
307 if (r2 >= ad) { // must be unsigned comparison
308 q2 = q2 + 1;
309 r2 = r2 - ad;
310 }
311 delta = ad - r2;
312 } while (q1 < delta || (q1 == delta && r1 == 0));
313
314 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
315 if (d < 0) mag.m = -mag.m; // resulting magic number
316 mag.s = p - 32; // resulting shift
317 return mag;
318}
319
320/// magicu - calculate the magic numbers required to codegen an integer udiv as
321/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
322static mu magicu32(uint32_t d) {
323 int32_t p;
324 uint32_t nc, delta, q1, r1, q2, r2;
325 struct mu magu;
326 magu.a = 0; // initialize "add" indicator
327 nc = - 1 - (-d)%d;
328 p = 31; // initialize p
329 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
330 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
331 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
332 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
333 do {
334 p = p + 1;
335 if (r1 >= nc - r1 ) {
336 q1 = 2*q1 + 1; // update q1
337 r1 = 2*r1 - nc; // update r1
338 }
339 else {
340 q1 = 2*q1; // update q1
341 r1 = 2*r1; // update r1
342 }
343 if (r2 + 1 >= d - r2) {
344 if (q2 >= 0x7FFFFFFF) magu.a = 1;
345 q2 = 2*q2 + 1; // update q2
346 r2 = 2*r2 + 1 - d; // update r2
347 }
348 else {
349 if (q2 >= 0x80000000) magu.a = 1;
350 q2 = 2*q2; // update q2
351 r2 = 2*r2 + 1; // update r2
352 }
353 delta = d - 1 - r2;
354 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
355 magu.m = q2 + 1; // resulting magic number
356 magu.s = p - 32; // resulting shift
357 return magu;
358}
359
360/// magic - calculate the magic numbers required to codegen an integer sdiv as
361/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
362/// or -1.
363static ms magic64(int64_t d) {
364 int64_t p;
365 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
366 const uint64_t two63 = 9223372036854775808ULL; // 2^63
367 struct ms mag;
368
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000369 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000370 t = two63 + ((uint64_t)d >> 63);
371 anc = t - 1 - t%ad; // absolute value of nc
372 p = 63; // initialize p
373 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
374 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
375 q2 = two63/ad; // initialize q2 = 2p/abs(d)
376 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
377 do {
378 p = p + 1;
379 q1 = 2*q1; // update q1 = 2p/abs(nc)
380 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
381 if (r1 >= anc) { // must be unsigned comparison
382 q1 = q1 + 1;
383 r1 = r1 - anc;
384 }
385 q2 = 2*q2; // update q2 = 2p/abs(d)
386 r2 = 2*r2; // update r2 = rem(2p/abs(d))
387 if (r2 >= ad) { // must be unsigned comparison
388 q2 = q2 + 1;
389 r2 = r2 - ad;
390 }
391 delta = ad - r2;
392 } while (q1 < delta || (q1 == delta && r1 == 0));
393
394 mag.m = q2 + 1;
395 if (d < 0) mag.m = -mag.m; // resulting magic number
396 mag.s = p - 64; // resulting shift
397 return mag;
398}
399
400/// magicu - calculate the magic numbers required to codegen an integer udiv as
401/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
402static mu magicu64(uint64_t d)
403{
404 int64_t p;
405 uint64_t nc, delta, q1, r1, q2, r2;
406 struct mu magu;
407 magu.a = 0; // initialize "add" indicator
408 nc = - 1 - (-d)%d;
409 p = 63; // initialize p
410 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
411 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
412 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
413 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
414 do {
415 p = p + 1;
416 if (r1 >= nc - r1 ) {
417 q1 = 2*q1 + 1; // update q1
418 r1 = 2*r1 - nc; // update r1
419 }
420 else {
421 q1 = 2*q1; // update q1
422 r1 = 2*r1; // update r1
423 }
424 if (r2 + 1 >= d - r2) {
425 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
426 q2 = 2*q2 + 1; // update q2
427 r2 = 2*r2 + 1 - d; // update r2
428 }
429 else {
430 if (q2 >= 0x8000000000000000ull) magu.a = 1;
431 q2 = 2*q2; // update q2
432 r2 = 2*r2 + 1; // update r2
433 }
434 delta = d - 1 - r2;
435 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
436 magu.m = q2 + 1; // resulting magic number
437 magu.s = p - 64; // resulting shift
438 return magu;
439}
440
Nate Begeman4ebd8052005-09-01 23:24:04 +0000441// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
442// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000443// Also, set the incoming LHS, RHS, and CC references to the appropriate
444// nodes based on the type of node we are checking. This simplifies life a
445// bit for the callers.
446static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
447 SDOperand &CC) {
448 if (N.getOpcode() == ISD::SETCC) {
449 LHS = N.getOperand(0);
450 RHS = N.getOperand(1);
451 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000452 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000453 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000454 if (N.getOpcode() == ISD::SELECT_CC &&
455 N.getOperand(2).getOpcode() == ISD::Constant &&
456 N.getOperand(3).getOpcode() == ISD::Constant &&
457 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000458 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
459 LHS = N.getOperand(0);
460 RHS = N.getOperand(1);
461 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000462 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000463 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000464 return false;
465}
466
Nate Begeman99801192005-09-07 23:25:52 +0000467// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
468// one use. If this is true, it allows the users to invert the operation for
469// free when it is profitable to do so.
470static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000471 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000472 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000473 return true;
474 return false;
475}
476
Nate Begeman452d7be2005-09-16 00:54:12 +0000477// FIXME: This should probably go in the ISD class rather than being duplicated
478// in several files.
479static bool isCommutativeBinOp(unsigned Opcode) {
480 switch (Opcode) {
481 case ISD::ADD:
482 case ISD::MUL:
483 case ISD::AND:
484 case ISD::OR:
485 case ISD::XOR: return true;
486 default: return false; // FIXME: Need commutative info for user ops!
487 }
488}
489
Nate Begemancd4d58c2006-02-03 06:46:56 +0000490SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
491 MVT::ValueType VT = N0.getValueType();
492 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
493 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
494 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
495 if (isa<ConstantSDNode>(N1)) {
496 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000497 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000498 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
499 } else if (N0.hasOneUse()) {
500 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000501 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000502 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
503 }
504 }
505 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
506 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
507 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
508 if (isa<ConstantSDNode>(N0)) {
509 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000510 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000511 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
512 } else if (N1.hasOneUse()) {
513 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000514 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000515 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
516 }
517 }
518 return SDOperand();
519}
520
Nate Begeman4ebd8052005-09-01 23:24:04 +0000521void DAGCombiner::Run(bool RunningAfterLegalize) {
522 // set the instance variable, so that the various visit routines may use it.
523 AfterLegalize = RunningAfterLegalize;
524
Nate Begeman646d7e22005-09-02 21:18:40 +0000525 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000526 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
527 E = DAG.allnodes_end(); I != E; ++I)
528 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000529
Chris Lattner95038592005-10-05 06:35:28 +0000530 // Create a dummy node (which is not added to allnodes), that adds a reference
531 // to the root node, preventing it from being deleted, and tracking any
532 // changes of the root.
533 HandleSDNode Dummy(DAG.getRoot());
534
Chris Lattner24664722006-03-01 04:53:38 +0000535
536 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
537 TargetLowering::DAGCombinerInfo
538 DagCombineInfo(DAG, !RunningAfterLegalize, this);
539
Nate Begeman1d4d4142005-09-01 00:19:25 +0000540 // while the worklist isn't empty, inspect the node on the end of it and
541 // try and combine it.
542 while (!WorkList.empty()) {
543 SDNode *N = WorkList.back();
544 WorkList.pop_back();
545
546 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000547 // N is deleted from the DAG, since they too may now be dead or may have a
548 // reduced number of uses, allowing other xforms.
549 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000550 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
551 WorkList.push_back(N->getOperand(i).Val);
552
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000554 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000555 continue;
556 }
557
Nate Begeman83e75ec2005-09-06 04:43:02 +0000558 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000559
560 // If nothing happened, try a target-specific DAG combine.
561 if (RV.Val == 0) {
562 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
563 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
564 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
565 }
566
Nate Begeman83e75ec2005-09-06 04:43:02 +0000567 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000569 // If we get back the same node we passed in, rather than a new node or
570 // zero, we know that the node must have defined multiple values and
571 // CombineTo was used. Since CombineTo takes care of the worklist
572 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000573 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000574 DEBUG(std::cerr << "\nReplacing "; N->dump();
575 std::cerr << "\nWith: "; RV.Val->dump();
576 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000577 std::vector<SDNode*> NowDead;
578 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000579
580 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000581 WorkList.push_back(RV.Val);
582 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000583
584 // Nodes can end up on the worklist more than once. Make sure we do
585 // not process a node that has been replaced.
586 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000587 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
588 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000589
590 // Finally, since the node is now dead, remove it from the graph.
591 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000592 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000593 }
594 }
Chris Lattner95038592005-10-05 06:35:28 +0000595
596 // If the root changed (e.g. it was a dead load, update the root).
597 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000598}
599
Nate Begeman83e75ec2005-09-06 04:43:02 +0000600SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000601 switch(N->getOpcode()) {
602 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000603 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000604 case ISD::ADD: return visitADD(N);
605 case ISD::SUB: return visitSUB(N);
606 case ISD::MUL: return visitMUL(N);
607 case ISD::SDIV: return visitSDIV(N);
608 case ISD::UDIV: return visitUDIV(N);
609 case ISD::SREM: return visitSREM(N);
610 case ISD::UREM: return visitUREM(N);
611 case ISD::MULHU: return visitMULHU(N);
612 case ISD::MULHS: return visitMULHS(N);
613 case ISD::AND: return visitAND(N);
614 case ISD::OR: return visitOR(N);
615 case ISD::XOR: return visitXOR(N);
616 case ISD::SHL: return visitSHL(N);
617 case ISD::SRA: return visitSRA(N);
618 case ISD::SRL: return visitSRL(N);
619 case ISD::CTLZ: return visitCTLZ(N);
620 case ISD::CTTZ: return visitCTTZ(N);
621 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000622 case ISD::SELECT: return visitSELECT(N);
623 case ISD::SELECT_CC: return visitSELECT_CC(N);
624 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000625 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
626 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
627 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
628 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000629 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000630 case ISD::FADD: return visitFADD(N);
631 case ISD::FSUB: return visitFSUB(N);
632 case ISD::FMUL: return visitFMUL(N);
633 case ISD::FDIV: return visitFDIV(N);
634 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000635 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000636 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
637 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
638 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
639 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
640 case ISD::FP_ROUND: return visitFP_ROUND(N);
641 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
642 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
643 case ISD::FNEG: return visitFNEG(N);
644 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000645 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000646 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000647 case ISD::LOAD: return visitLOAD(N);
Chris Lattner29cd7db2006-03-31 18:10:41 +0000648 case ISD::EXTLOAD:
649 case ISD::SEXTLOAD:
650 case ISD::ZEXTLOAD: return visitXEXTLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000651 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000652 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
653 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000654 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000655 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Chris Lattnerf1d0c622006-03-31 22:16:43 +0000656 case ISD::VVECTOR_SHUFFLE: return visitVVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000657 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000658 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000659}
660
Nate Begeman83e75ec2005-09-06 04:43:02 +0000661SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000662 std::vector<SDOperand> Ops;
663 bool Changed = false;
664
Nate Begeman1d4d4142005-09-01 00:19:25 +0000665 // If the token factor has two operands and one is the entry token, replace
666 // the token factor with the other operand.
667 if (N->getNumOperands() == 2) {
668 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000669 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000670 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000671 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000673
Nate Begemanded49632005-10-13 03:11:28 +0000674 // fold (tokenfactor (tokenfactor)) -> tokenfactor
675 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
676 SDOperand Op = N->getOperand(i);
677 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000678 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000679 Changed = true;
680 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
681 Ops.push_back(Op.getOperand(j));
682 } else {
683 Ops.push_back(Op);
684 }
685 }
686 if (Changed)
687 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000688 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000689}
690
Nate Begeman83e75ec2005-09-06 04:43:02 +0000691SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000692 SDOperand N0 = N->getOperand(0);
693 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000694 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
695 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000696 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000697
698 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000700 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000701 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000702 if (N0C && !N1C)
703 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000705 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000706 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000707 // fold ((c1-A)+c2) -> (c1+c2)-A
708 if (N1C && N0.getOpcode() == ISD::SUB)
709 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
710 return DAG.getNode(ISD::SUB, VT,
711 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
712 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000713 // reassociate add
714 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
715 if (RADD.Val != 0)
716 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000717 // fold ((0-A) + B) -> B-A
718 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
719 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000720 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000721 // fold (A + (0-B)) -> A-B
722 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
723 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000724 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000725 // fold (A+(B-A)) -> B
726 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000727 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000728
Evan Cheng860771d2006-03-01 01:09:54 +0000729 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemanb0d04a72006-02-18 02:40:58 +0000730 return SDOperand();
Chris Lattner947c2892006-03-13 06:51:27 +0000731
732 // fold (a+b) -> (a|b) iff a and b share no bits.
733 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
734 uint64_t LHSZero, LHSOne;
735 uint64_t RHSZero, RHSOne;
736 uint64_t Mask = MVT::getIntVTBitMask(VT);
737 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
738 if (LHSZero) {
739 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
740
741 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
742 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
743 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
744 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
745 return DAG.getNode(ISD::OR, VT, N0, N1);
746 }
747 }
748
Nate Begeman83e75ec2005-09-06 04:43:02 +0000749 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000750}
751
Nate Begeman83e75ec2005-09-06 04:43:02 +0000752SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000753 SDOperand N0 = N->getOperand(0);
754 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000755 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
756 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000757 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000758
Chris Lattner854077d2005-10-17 01:07:11 +0000759 // fold (sub x, x) -> 0
760 if (N0 == N1)
761 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000762 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000763 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000764 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000765 // fold (sub x, c) -> (add x, -c)
766 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000767 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000768 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000769 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000770 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000771 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000772 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000773 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000774 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000775}
776
Nate Begeman83e75ec2005-09-06 04:43:02 +0000777SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000778 SDOperand N0 = N->getOperand(0);
779 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000780 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
781 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000782 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000783
784 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000785 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000786 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000787 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000788 if (N0C && !N1C)
789 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000790 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000791 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000792 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000793 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000794 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000795 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000796 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000797 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000798 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000799 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000800 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000801 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
802 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
803 // FIXME: If the input is something that is easily negated (e.g. a
804 // single-use add), we should put the negate there.
805 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
806 DAG.getNode(ISD::SHL, VT, N0,
807 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
808 TLI.getShiftAmountTy())));
809 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000810
811 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
812 if (N1C && N0.getOpcode() == ISD::SHL &&
813 isa<ConstantSDNode>(N0.getOperand(1))) {
814 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000815 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000816 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
817 }
818
819 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
820 // use.
821 {
822 SDOperand Sh(0,0), Y(0,0);
823 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
824 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
825 N0.Val->hasOneUse()) {
826 Sh = N0; Y = N1;
827 } else if (N1.getOpcode() == ISD::SHL &&
828 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
829 Sh = N1; Y = N0;
830 }
831 if (Sh.Val) {
832 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
833 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
834 }
835 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000836 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
837 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
838 isa<ConstantSDNode>(N0.getOperand(1))) {
839 return DAG.getNode(ISD::ADD, VT,
840 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
841 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
842 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000843
Nate Begemancd4d58c2006-02-03 06:46:56 +0000844 // reassociate mul
845 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
846 if (RMUL.Val != 0)
847 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000848 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000849}
850
Nate Begeman83e75ec2005-09-06 04:43:02 +0000851SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000852 SDOperand N0 = N->getOperand(0);
853 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000854 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
855 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000856 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000857
858 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000859 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000860 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000861 // fold (sdiv X, 1) -> X
862 if (N1C && N1C->getSignExtended() == 1LL)
863 return N0;
864 // fold (sdiv X, -1) -> 0-X
865 if (N1C && N1C->isAllOnesValue())
866 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000867 // If we know the sign bits of both operands are zero, strength reduce to a
868 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
869 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000870 if (TLI.MaskedValueIsZero(N1, SignBit) &&
871 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000872 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000873 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000874 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000875 (isPowerOf2_64(N1C->getSignExtended()) ||
876 isPowerOf2_64(-N1C->getSignExtended()))) {
877 // If dividing by powers of two is cheap, then don't perform the following
878 // fold.
879 if (TLI.isPow2DivCheap())
880 return SDOperand();
881 int64_t pow2 = N1C->getSignExtended();
882 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000883 unsigned lg2 = Log2_64(abs2);
884 // Splat the sign bit into the register
885 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000886 DAG.getConstant(MVT::getSizeInBits(VT)-1,
887 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000888 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000889 // Add (N0 < 0) ? abs2 - 1 : 0;
890 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
891 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000892 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000893 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000894 AddToWorkList(SRL.Val);
895 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000896 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
897 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000898 // If we're dividing by a positive value, we're done. Otherwise, we must
899 // negate the result.
900 if (pow2 > 0)
901 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000902 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000903 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
904 }
Nate Begeman69575232005-10-20 02:15:44 +0000905 // if integer divide is expensive and we satisfy the requirements, emit an
906 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000907 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000908 !TLI.isIntDivCheap()) {
909 SDOperand Op = BuildSDIV(N);
910 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000911 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000912 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000913}
914
Nate Begeman83e75ec2005-09-06 04:43:02 +0000915SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000916 SDOperand N0 = N->getOperand(0);
917 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000918 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
919 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000920 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000921
922 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000923 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000924 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000926 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000927 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000928 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000929 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000930 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
931 if (N1.getOpcode() == ISD::SHL) {
932 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
933 if (isPowerOf2_64(SHC->getValue())) {
934 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000935 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
936 DAG.getConstant(Log2_64(SHC->getValue()),
937 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000938 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000939 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000940 }
941 }
942 }
Nate Begeman69575232005-10-20 02:15:44 +0000943 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000944 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
945 SDOperand Op = BuildUDIV(N);
946 if (Op.Val) return Op;
947 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000948 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000949}
950
Nate Begeman83e75ec2005-09-06 04:43:02 +0000951SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000952 SDOperand N0 = N->getOperand(0);
953 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000954 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
955 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000956 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957
958 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000959 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000960 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000961 // If we know the sign bits of both operands are zero, strength reduce to a
962 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
963 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000964 if (TLI.MaskedValueIsZero(N1, SignBit) &&
965 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000966 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000967 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000968}
969
Nate Begeman83e75ec2005-09-06 04:43:02 +0000970SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 SDOperand N0 = N->getOperand(0);
972 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
974 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000975 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000976
977 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000978 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000979 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000980 // fold (urem x, pow2) -> (and x, pow2-1)
981 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000982 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000983 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
984 if (N1.getOpcode() == ISD::SHL) {
985 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
986 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000987 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000988 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000989 return DAG.getNode(ISD::AND, VT, N0, Add);
990 }
991 }
992 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000993 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994}
995
Nate Begeman83e75ec2005-09-06 04:43:02 +0000996SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000997 SDOperand N0 = N->getOperand(0);
998 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000999 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000
1001 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001002 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001003 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001005 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1007 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001008 TLI.getShiftAmountTy()));
1009 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010}
1011
Nate Begeman83e75ec2005-09-06 04:43:02 +00001012SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001013 SDOperand N0 = N->getOperand(0);
1014 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001015 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016
1017 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001018 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001019 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001020 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001021 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001022 return DAG.getConstant(0, N0.getValueType());
1023 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001024}
1025
Nate Begeman83e75ec2005-09-06 04:43:02 +00001026SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001027 SDOperand N0 = N->getOperand(0);
1028 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001029 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001030 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1031 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001032 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +00001033 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001034
1035 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001036 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001037 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001038 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001039 if (N0C && !N1C)
1040 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001041 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001042 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001043 return N0;
1044 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001045 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001046 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001047 // reassociate and
1048 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1049 if (RAND.Val != 0)
1050 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001051 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001052 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001053 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001054 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001055 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001056 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1057 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001058 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001059 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001060 ~N1C->getValue() & InMask)) {
1061 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1062 N0.getOperand(0));
1063
1064 // Replace uses of the AND with uses of the Zero extend node.
1065 CombineTo(N, Zext);
1066
Chris Lattner3603cd62006-02-02 07:17:31 +00001067 // We actually want to replace all uses of the any_extend with the
1068 // zero_extend, to avoid duplicating things. This will later cause this
1069 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001070 CombineTo(N0.Val, Zext);
Chris Lattner3603cd62006-02-02 07:17:31 +00001071 return SDOperand();
1072 }
1073 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001074 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1075 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1076 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1077 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1078
1079 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1080 MVT::isInteger(LL.getValueType())) {
1081 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1082 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1083 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001084 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001085 return DAG.getSetCC(VT, ORNode, LR, Op1);
1086 }
1087 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1088 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1089 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001090 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001091 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1092 }
1093 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1094 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1095 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001096 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001097 return DAG.getSetCC(VT, ORNode, LR, Op1);
1098 }
1099 }
1100 // canonicalize equivalent to ll == rl
1101 if (LL == RR && LR == RL) {
1102 Op1 = ISD::getSetCCSwappedOperands(Op1);
1103 std::swap(RL, RR);
1104 }
1105 if (LL == RL && LR == RR) {
1106 bool isInteger = MVT::isInteger(LL.getValueType());
1107 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1108 if (Result != ISD::SETCC_INVALID)
1109 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1110 }
1111 }
1112 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1113 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1114 N1.getOpcode() == ISD::ZERO_EXTEND &&
1115 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1116 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1117 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001118 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001119 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1120 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001121 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001122 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001123 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1124 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001125 N0.getOperand(1) == N1.getOperand(1)) {
1126 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1127 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001128 AddToWorkList(ANDNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001129 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1130 }
Nate Begemande996292006-02-03 22:24:05 +00001131 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1132 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001133 if (!MVT::isVector(VT) &&
1134 SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001135 return SDOperand();
Nate Begemanded49632005-10-13 03:11:28 +00001136 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001137 if (N0.getOpcode() == ISD::EXTLOAD) {
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)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +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 }
1151 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001152 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001153 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001154 // If we zero all the possible extended bits, then we can turn this into
1155 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001156 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001157 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001158 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1159 N0.getOperand(1), N0.getOperand(2),
1160 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001161 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001162 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001163 return SDOperand();
1164 }
1165 }
Chris Lattner15045b62006-02-28 06:35:35 +00001166
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001167 // fold (and (load x), 255) -> (zextload x, i8)
1168 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1169 if (N1C &&
1170 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1171 N0.getOpcode() == ISD::ZEXTLOAD) &&
1172 N0.hasOneUse()) {
1173 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001174 if (N1C->getValue() == 255)
1175 EVT = MVT::i8;
1176 else if (N1C->getValue() == 65535)
1177 EVT = MVT::i16;
1178 else if (N1C->getValue() == ~0U)
1179 EVT = MVT::i32;
1180 else
1181 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001182
1183 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1184 cast<VTSDNode>(N0.getOperand(3))->getVT();
1185 if (EVT != MVT::Other && LoadedVT > EVT) {
Chris Lattner15045b62006-02-28 06:35:35 +00001186 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1187 // For big endian targets, we need to add an offset to the pointer to load
1188 // the correct bytes. For little endian systems, we merely need to read
1189 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001190 unsigned PtrOff =
1191 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1192 SDOperand NewPtr = N0.getOperand(1);
1193 if (!TLI.isLittleEndian())
1194 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1195 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001196 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001197 SDOperand Load =
1198 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1199 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001200 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001201 CombineTo(N0.Val, Load, Load.getValue(1));
1202 return SDOperand();
1203 }
1204 }
1205
Nate Begeman83e75ec2005-09-06 04:43:02 +00001206 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001207}
1208
Nate Begeman83e75ec2005-09-06 04:43:02 +00001209SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210 SDOperand N0 = N->getOperand(0);
1211 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001212 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001213 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1214 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001215 MVT::ValueType VT = N1.getValueType();
1216 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217
1218 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001219 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001220 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001221 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001222 if (N0C && !N1C)
1223 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001224 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001225 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001226 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001227 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001228 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001229 return N1;
1230 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001231 if (N1C &&
1232 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001233 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001234 // reassociate or
1235 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1236 if (ROR.Val != 0)
1237 return ROR;
1238 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1239 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001240 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001241 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1242 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1243 N1),
1244 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001245 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001246 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1247 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1248 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1249 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1250
1251 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1252 MVT::isInteger(LL.getValueType())) {
1253 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1254 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1255 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1256 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1257 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001258 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001259 return DAG.getSetCC(VT, ORNode, LR, Op1);
1260 }
1261 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1262 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1263 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1264 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1265 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001266 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001267 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1268 }
1269 }
1270 // canonicalize equivalent to ll == rl
1271 if (LL == RR && LR == RL) {
1272 Op1 = ISD::getSetCCSwappedOperands(Op1);
1273 std::swap(RL, RR);
1274 }
1275 if (LL == RL && LR == RR) {
1276 bool isInteger = MVT::isInteger(LL.getValueType());
1277 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1278 if (Result != ISD::SETCC_INVALID)
1279 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1280 }
1281 }
1282 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1283 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1284 N1.getOpcode() == ISD::ZERO_EXTEND &&
1285 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1286 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1287 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001288 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001289 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1290 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001291 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1292 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1293 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1294 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1295 N0.getOperand(1) == N1.getOperand(1)) {
1296 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1297 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001298 AddToWorkList(ORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001299 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1300 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001301 // canonicalize shl to left side in a shl/srl pair, to match rotate
1302 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1303 std::swap(N0, N1);
1304 // check for rotl, rotr
1305 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1306 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001307 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001308 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1309 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1310 N1.getOperand(1).getOpcode() == ISD::Constant) {
1311 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1312 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1313 if ((c1val + c2val) == OpSizeInBits)
1314 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1315 }
1316 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1317 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1318 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1319 if (ConstantSDNode *SUBC =
1320 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1321 if (SUBC->getValue() == OpSizeInBits)
1322 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1323 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1324 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1325 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1326 if (ConstantSDNode *SUBC =
1327 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1328 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001329 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001330 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1331 N1.getOperand(1));
1332 else
1333 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1334 N0.getOperand(1));
1335 }
1336 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001337 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338}
1339
Nate Begeman83e75ec2005-09-06 04:43:02 +00001340SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001341 SDOperand N0 = N->getOperand(0);
1342 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001343 SDOperand LHS, RHS, CC;
1344 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1345 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001346 MVT::ValueType VT = N0.getValueType();
1347
1348 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001349 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001350 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001351 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001352 if (N0C && !N1C)
1353 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001354 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001355 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001356 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001357 // reassociate xor
1358 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1359 if (RXOR.Val != 0)
1360 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001362 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1363 bool isInt = MVT::isInteger(LHS.getValueType());
1364 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1365 isInt);
1366 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001367 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001369 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001370 assert(0 && "Unhandled SetCC Equivalent!");
1371 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001372 }
Nate Begeman99801192005-09-07 23:25:52 +00001373 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1374 if (N1C && N1C->getValue() == 1 &&
1375 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001377 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1378 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001379 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1380 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001381 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001382 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 }
1384 }
Nate Begeman99801192005-09-07 23:25:52 +00001385 // fold !(x or y) -> (!x and !y) iff x or y are constants
1386 if (N1C && N1C->isAllOnesValue() &&
1387 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001388 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001389 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1390 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001391 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1392 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001393 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001394 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001395 }
1396 }
Nate Begeman223df222005-09-08 20:18:10 +00001397 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1398 if (N1C && N0.getOpcode() == ISD::XOR) {
1399 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1400 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1401 if (N00C)
1402 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1403 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1404 if (N01C)
1405 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1406 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1407 }
1408 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001409 if (N0 == N1) {
1410 if (!MVT::isVector(VT)) {
1411 return DAG.getConstant(0, VT);
1412 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1413 // Produce a vector of zeros.
1414 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1415 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1416 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
1417 }
1418 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001419 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1420 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1421 N1.getOpcode() == ISD::ZERO_EXTEND &&
1422 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1423 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1424 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001425 AddToWorkList(XORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001426 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1427 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001428 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1429 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1430 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1431 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1432 N0.getOperand(1) == N1.getOperand(1)) {
1433 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1434 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001435 AddToWorkList(XORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001436 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1437 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001438 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001439}
1440
Nate Begeman83e75ec2005-09-06 04:43:02 +00001441SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001442 SDOperand N0 = N->getOperand(0);
1443 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001444 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001446 MVT::ValueType VT = N0.getValueType();
1447 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1448
1449 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001450 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001451 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001452 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001453 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001454 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001456 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001457 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001459 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001460 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001462 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001463 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001464 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001465 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001467 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001468 N0.getOperand(1).getOpcode() == ISD::Constant) {
1469 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001470 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001472 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001473 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001474 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001475 }
1476 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1477 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001478 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001479 N0.getOperand(1).getOpcode() == ISD::Constant) {
1480 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001481 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001482 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1483 DAG.getConstant(~0ULL << c1, VT));
1484 if (c2 > c1)
1485 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001486 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001487 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001488 return DAG.getNode(ISD::SRL, VT, Mask,
1489 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001490 }
1491 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001492 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001493 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001494 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001495 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1496 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1497 isa<ConstantSDNode>(N0.getOperand(1))) {
1498 return DAG.getNode(ISD::ADD, VT,
1499 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1500 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1501 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001502 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001503}
1504
Nate Begeman83e75ec2005-09-06 04:43:02 +00001505SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001506 SDOperand N0 = N->getOperand(0);
1507 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001508 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1509 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001510 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001511
1512 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001513 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001514 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001515 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001516 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001517 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001518 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001519 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001520 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001521 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001522 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001523 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001524 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001525 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001526 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001527 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1528 // sext_inreg.
1529 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1530 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1531 MVT::ValueType EVT;
1532 switch (LowBits) {
1533 default: EVT = MVT::Other; break;
1534 case 1: EVT = MVT::i1; break;
1535 case 8: EVT = MVT::i8; break;
1536 case 16: EVT = MVT::i16; break;
1537 case 32: EVT = MVT::i32; break;
1538 }
1539 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1540 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1541 DAG.getValueType(EVT));
1542 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001543
1544 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1545 if (N1C && N0.getOpcode() == ISD::SRA) {
1546 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1547 unsigned Sum = N1C->getValue() + C1->getValue();
1548 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1549 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1550 DAG.getConstant(Sum, N1C->getValueType(0)));
1551 }
1552 }
1553
Nate Begeman1d4d4142005-09-01 00:19:25 +00001554 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001555 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001556 return DAG.getNode(ISD::SRL, VT, N0, N1);
1557 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001558}
1559
Nate Begeman83e75ec2005-09-06 04:43:02 +00001560SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001561 SDOperand N0 = N->getOperand(0);
1562 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001563 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1564 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001565 MVT::ValueType VT = N0.getValueType();
1566 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1567
1568 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001569 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001570 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001571 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001572 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001573 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001574 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001575 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001578 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001579 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001580 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001581 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001582 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001583 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001584 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001585 N0.getOperand(1).getOpcode() == ISD::Constant) {
1586 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001587 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001588 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001589 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001591 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001592 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001593 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001594}
1595
Nate Begeman83e75ec2005-09-06 04:43:02 +00001596SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001597 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001598 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001599 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001600
1601 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001602 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001603 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001604 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001605}
1606
Nate Begeman83e75ec2005-09-06 04:43:02 +00001607SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001608 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001609 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001610 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001611
1612 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001613 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001614 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001615 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616}
1617
Nate Begeman83e75ec2005-09-06 04:43:02 +00001618SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001619 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001620 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001621 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622
1623 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001624 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001625 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001626 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001627}
1628
Nate Begeman452d7be2005-09-16 00:54:12 +00001629SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1630 SDOperand N0 = N->getOperand(0);
1631 SDOperand N1 = N->getOperand(1);
1632 SDOperand N2 = N->getOperand(2);
1633 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1634 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1635 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1636 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001637
Nate Begeman452d7be2005-09-16 00:54:12 +00001638 // fold select C, X, X -> X
1639 if (N1 == N2)
1640 return N1;
1641 // fold select true, X, Y -> X
1642 if (N0C && !N0C->isNullValue())
1643 return N1;
1644 // fold select false, X, Y -> Y
1645 if (N0C && N0C->isNullValue())
1646 return N2;
1647 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001648 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001649 return DAG.getNode(ISD::OR, VT, N0, N2);
1650 // fold select C, 0, X -> ~C & X
1651 // FIXME: this should check for C type == X type, not i1?
1652 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1653 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001654 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001655 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1656 }
1657 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001658 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001659 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001660 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001661 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1662 }
1663 // fold select C, X, 0 -> C & X
1664 // FIXME: this should check for C type == X type, not i1?
1665 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1666 return DAG.getNode(ISD::AND, VT, N0, N1);
1667 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1668 if (MVT::i1 == VT && N0 == N1)
1669 return DAG.getNode(ISD::OR, VT, N0, N2);
1670 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1671 if (MVT::i1 == VT && N0 == N2)
1672 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001673 // If we can fold this based on the true/false value, do so.
1674 if (SimplifySelectOps(N, N1, N2))
1675 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001676 // fold selects based on a setcc into other things, such as min/max/abs
1677 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001678 // FIXME:
1679 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1680 // having to say they don't support SELECT_CC on every type the DAG knows
1681 // about, since there is no way to mark an opcode illegal at all value types
1682 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1683 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1684 N1, N2, N0.getOperand(2));
1685 else
1686 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001687 return SDOperand();
1688}
1689
1690SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001691 SDOperand N0 = N->getOperand(0);
1692 SDOperand N1 = N->getOperand(1);
1693 SDOperand N2 = N->getOperand(2);
1694 SDOperand N3 = N->getOperand(3);
1695 SDOperand N4 = N->getOperand(4);
1696 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1697 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1698 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1699 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1700
1701 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001702 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001703 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1704
Nate Begeman44728a72005-09-19 22:34:01 +00001705 // fold select_cc lhs, rhs, x, x, cc -> x
1706 if (N2 == N3)
1707 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001708
1709 // If we can fold this based on the true/false value, do so.
1710 if (SimplifySelectOps(N, N2, N3))
1711 return SDOperand();
1712
Nate Begeman44728a72005-09-19 22:34:01 +00001713 // fold select_cc into other things, such as min/max/abs
1714 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001715}
1716
1717SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1718 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1719 cast<CondCodeSDNode>(N->getOperand(2))->get());
1720}
1721
Nate Begeman83e75ec2005-09-06 04:43:02 +00001722SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001723 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001724 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001725 MVT::ValueType VT = N->getValueType(0);
1726
Nate Begeman1d4d4142005-09-01 00:19:25 +00001727 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001728 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001729 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001730 // fold (sext (sext x)) -> (sext x)
1731 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001732 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001733 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001734 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1735 (!AfterLegalize ||
1736 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001737 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1738 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001739 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001740 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1741 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001742 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1743 N0.getOperand(1), N0.getOperand(2),
1744 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001745 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001746 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1747 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001748 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001749 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001750
1751 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1752 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1753 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1754 N0.hasOneUse()) {
1755 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1756 N0.getOperand(1), N0.getOperand(2),
1757 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001758 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001759 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1760 ExtLoad.getValue(1));
1761 return SDOperand();
1762 }
1763
Nate Begeman83e75ec2005-09-06 04:43:02 +00001764 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001765}
1766
Nate Begeman83e75ec2005-09-06 04:43:02 +00001767SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001768 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001769 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001770 MVT::ValueType VT = N->getValueType(0);
1771
Nate Begeman1d4d4142005-09-01 00:19:25 +00001772 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001773 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001774 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001775 // fold (zext (zext x)) -> (zext x)
1776 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001777 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001778 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1779 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001780 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001781 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001782 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001783 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1784 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001785 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1786 N0.getOperand(1), N0.getOperand(2),
1787 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001788 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001789 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1790 ExtLoad.getValue(1));
1791 return SDOperand();
1792 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001793
1794 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1795 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1796 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1797 N0.hasOneUse()) {
1798 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1799 N0.getOperand(1), N0.getOperand(2),
1800 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001801 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001802 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1803 ExtLoad.getValue(1));
1804 return SDOperand();
1805 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001806 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001807}
1808
Nate Begeman83e75ec2005-09-06 04:43:02 +00001809SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001810 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001811 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001812 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001813 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001814 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001815 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001816
Nate Begeman1d4d4142005-09-01 00:19:25 +00001817 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001818 if (N0C) {
1819 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001820 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001821 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001822 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001823 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001824 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001825 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001826 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001827 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1828 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1829 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001830 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001831 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001832 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1833 if (N0.getOpcode() == ISD::AssertSext &&
1834 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001835 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001836 }
1837 // fold (sext_in_reg (sextload x)) -> (sextload x)
1838 if (N0.getOpcode() == ISD::SEXTLOAD &&
1839 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001840 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001841 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001842 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001843 if (N0.getOpcode() == ISD::SETCC &&
1844 TLI.getSetCCResultContents() ==
1845 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001846 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001847 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001848 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001849 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001850 // fold (sext_in_reg (srl x)) -> sra x
1851 if (N0.getOpcode() == ISD::SRL &&
1852 N0.getOperand(1).getOpcode() == ISD::Constant &&
1853 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1854 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1855 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001856 }
Nate Begemanded49632005-10-13 03:11:28 +00001857 // fold (sext_inreg (extload x)) -> (sextload x)
1858 if (N0.getOpcode() == ISD::EXTLOAD &&
1859 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001860 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001861 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1862 N0.getOperand(1), N0.getOperand(2),
1863 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001864 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001865 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001866 return SDOperand();
1867 }
1868 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001869 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001870 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001871 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001872 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1873 N0.getOperand(1), N0.getOperand(2),
1874 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001875 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001876 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001877 return SDOperand();
1878 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001879 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001880}
1881
Nate Begeman83e75ec2005-09-06 04:43:02 +00001882SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001883 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001884 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885 MVT::ValueType VT = N->getValueType(0);
1886
1887 // noop truncate
1888 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001889 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001890 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001891 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001892 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001893 // fold (truncate (truncate x)) -> (truncate x)
1894 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001895 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001896 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1897 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1898 if (N0.getValueType() < VT)
1899 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001900 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001901 else if (N0.getValueType() > VT)
1902 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001903 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904 else
1905 // if the source and dest are the same type, we can drop both the extend
1906 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001907 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001908 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001909 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001910 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001911 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1912 "Cannot truncate to larger type!");
1913 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001914 // For big endian targets, we need to add an offset to the pointer to load
1915 // the correct bytes. For little endian systems, we merely need to read
1916 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001917 uint64_t PtrOff =
1918 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001919 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1920 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1921 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001922 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001923 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001924 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001925 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001926 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001927 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001928 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001929}
1930
Chris Lattner94683772005-12-23 05:30:37 +00001931SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1932 SDOperand N0 = N->getOperand(0);
1933 MVT::ValueType VT = N->getValueType(0);
1934
1935 // If the input is a constant, let getNode() fold it.
1936 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1937 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1938 if (Res.Val != N) return Res;
1939 }
1940
Chris Lattnerc8547d82005-12-23 05:37:50 +00001941 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1942 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1943
Chris Lattner57104102005-12-23 05:44:41 +00001944 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001945 // FIXME: These xforms need to know that the resultant load doesn't need a
1946 // higher alignment than the original!
1947 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001948 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1949 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001950 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00001951 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1952 Load.getValue(1));
1953 return Load;
1954 }
1955
Chris Lattner94683772005-12-23 05:30:37 +00001956 return SDOperand();
1957}
1958
Chris Lattner01b3d732005-09-28 22:28:18 +00001959SDOperand DAGCombiner::visitFADD(SDNode *N) {
1960 SDOperand N0 = N->getOperand(0);
1961 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001962 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1963 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001964 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001965
1966 // fold (fadd c1, c2) -> c1+c2
1967 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001968 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001969 // canonicalize constant to RHS
1970 if (N0CFP && !N1CFP)
1971 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001972 // fold (A + (-B)) -> A-B
1973 if (N1.getOpcode() == ISD::FNEG)
1974 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001975 // fold ((-A) + B) -> B-A
1976 if (N0.getOpcode() == ISD::FNEG)
1977 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001978 return SDOperand();
1979}
1980
1981SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1982 SDOperand N0 = N->getOperand(0);
1983 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001984 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1985 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001986 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001987
1988 // fold (fsub c1, c2) -> c1-c2
1989 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001990 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001991 // fold (A-(-B)) -> A+B
1992 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001993 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001994 return SDOperand();
1995}
1996
1997SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1998 SDOperand N0 = N->getOperand(0);
1999 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002000 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2001 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002002 MVT::ValueType VT = N->getValueType(0);
2003
Nate Begeman11af4ea2005-10-17 20:40:11 +00002004 // fold (fmul c1, c2) -> c1*c2
2005 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002006 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002007 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002008 if (N0CFP && !N1CFP)
2009 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002010 // fold (fmul X, 2.0) -> (fadd X, X)
2011 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2012 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002013 return SDOperand();
2014}
2015
2016SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2017 SDOperand N0 = N->getOperand(0);
2018 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002019 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2020 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002021 MVT::ValueType VT = N->getValueType(0);
2022
Nate Begemana148d982006-01-18 22:35:16 +00002023 // fold (fdiv c1, c2) -> c1/c2
2024 if (N0CFP && N1CFP)
2025 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002026 return SDOperand();
2027}
2028
2029SDOperand DAGCombiner::visitFREM(SDNode *N) {
2030 SDOperand N0 = N->getOperand(0);
2031 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002032 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2033 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002034 MVT::ValueType VT = N->getValueType(0);
2035
Nate Begemana148d982006-01-18 22:35:16 +00002036 // fold (frem c1, c2) -> fmod(c1,c2)
2037 if (N0CFP && N1CFP)
2038 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002039 return SDOperand();
2040}
2041
Chris Lattner12d83032006-03-05 05:30:57 +00002042SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2043 SDOperand N0 = N->getOperand(0);
2044 SDOperand N1 = N->getOperand(1);
2045 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2046 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2047 MVT::ValueType VT = N->getValueType(0);
2048
2049 if (N0CFP && N1CFP) // Constant fold
2050 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2051
2052 if (N1CFP) {
2053 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2054 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2055 union {
2056 double d;
2057 int64_t i;
2058 } u;
2059 u.d = N1CFP->getValue();
2060 if (u.i >= 0)
2061 return DAG.getNode(ISD::FABS, VT, N0);
2062 else
2063 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2064 }
2065
2066 // copysign(fabs(x), y) -> copysign(x, y)
2067 // copysign(fneg(x), y) -> copysign(x, y)
2068 // copysign(copysign(x,z), y) -> copysign(x, y)
2069 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2070 N0.getOpcode() == ISD::FCOPYSIGN)
2071 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2072
2073 // copysign(x, abs(y)) -> abs(x)
2074 if (N1.getOpcode() == ISD::FABS)
2075 return DAG.getNode(ISD::FABS, VT, N0);
2076
2077 // copysign(x, copysign(y,z)) -> copysign(x, z)
2078 if (N1.getOpcode() == ISD::FCOPYSIGN)
2079 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2080
2081 // copysign(x, fp_extend(y)) -> copysign(x, y)
2082 // copysign(x, fp_round(y)) -> copysign(x, y)
2083 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2084 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2085
2086 return SDOperand();
2087}
2088
2089
Chris Lattner01b3d732005-09-28 22:28:18 +00002090
Nate Begeman83e75ec2005-09-06 04:43:02 +00002091SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002092 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002093 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002094 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002095
2096 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002097 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002098 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002099 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002100}
2101
Nate Begeman83e75ec2005-09-06 04:43:02 +00002102SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002103 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002104 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002105 MVT::ValueType VT = N->getValueType(0);
2106
Nate Begeman1d4d4142005-09-01 00:19:25 +00002107 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002108 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002109 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002110 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002111}
2112
Nate Begeman83e75ec2005-09-06 04:43:02 +00002113SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002114 SDOperand N0 = N->getOperand(0);
2115 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2116 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002117
2118 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002119 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002120 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002121 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002122}
2123
Nate Begeman83e75ec2005-09-06 04:43:02 +00002124SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002125 SDOperand N0 = N->getOperand(0);
2126 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2127 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002128
2129 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002130 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002131 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
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(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002136 SDOperand N0 = N->getOperand(0);
2137 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2138 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002139
2140 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002141 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002142 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002143
2144 // fold (fp_round (fp_extend x)) -> x
2145 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2146 return N0.getOperand(0);
2147
2148 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2149 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2150 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2151 AddToWorkList(Tmp.Val);
2152 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2153 }
2154
Nate Begeman83e75ec2005-09-06 04:43:02 +00002155 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002156}
2157
Nate Begeman83e75ec2005-09-06 04:43:02 +00002158SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002159 SDOperand N0 = N->getOperand(0);
2160 MVT::ValueType VT = N->getValueType(0);
2161 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002162 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002163
Nate Begeman1d4d4142005-09-01 00:19:25 +00002164 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002165 if (N0CFP) {
2166 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002167 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002168 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002169 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002170}
2171
Nate Begeman83e75ec2005-09-06 04:43:02 +00002172SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002173 SDOperand N0 = N->getOperand(0);
2174 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2175 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002176
2177 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002178 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002179 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002180 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002181}
2182
Nate Begeman83e75ec2005-09-06 04:43:02 +00002183SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002184 SDOperand N0 = N->getOperand(0);
2185 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2186 MVT::ValueType VT = N->getValueType(0);
2187
2188 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002189 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002190 return DAG.getNode(ISD::FNEG, VT, N0);
2191 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002192 if (N0.getOpcode() == ISD::SUB)
2193 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002194 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002195 if (N0.getOpcode() == ISD::FNEG)
2196 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002197 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002198}
2199
Nate Begeman83e75ec2005-09-06 04:43:02 +00002200SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002201 SDOperand N0 = N->getOperand(0);
2202 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2203 MVT::ValueType VT = N->getValueType(0);
2204
Nate Begeman1d4d4142005-09-01 00:19:25 +00002205 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002206 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002207 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002208 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002209 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002210 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002211 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002212 // fold (fabs (fcopysign x, y)) -> (fabs x)
2213 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2214 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2215
Nate Begeman83e75ec2005-09-06 04:43:02 +00002216 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002217}
2218
Nate Begeman44728a72005-09-19 22:34:01 +00002219SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2220 SDOperand Chain = N->getOperand(0);
2221 SDOperand N1 = N->getOperand(1);
2222 SDOperand N2 = N->getOperand(2);
2223 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2224
2225 // never taken branch, fold to chain
2226 if (N1C && N1C->isNullValue())
2227 return Chain;
2228 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002229 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002230 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002231 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2232 // on the target.
2233 if (N1.getOpcode() == ISD::SETCC &&
2234 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2235 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2236 N1.getOperand(0), N1.getOperand(1), N2);
2237 }
Nate Begeman44728a72005-09-19 22:34:01 +00002238 return SDOperand();
2239}
2240
Chris Lattner3ea0b472005-10-05 06:47:48 +00002241// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2242//
Nate Begeman44728a72005-09-19 22:34:01 +00002243SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002244 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2245 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2246
2247 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002248 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2249 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2250
2251 // fold br_cc true, dest -> br dest (unconditional branch)
2252 if (SCCC && SCCC->getValue())
2253 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2254 N->getOperand(4));
2255 // fold br_cc false, dest -> unconditional fall through
2256 if (SCCC && SCCC->isNullValue())
2257 return N->getOperand(0);
2258 // fold to a simpler setcc
2259 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2260 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2261 Simp.getOperand(2), Simp.getOperand(0),
2262 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002263 return SDOperand();
2264}
2265
Chris Lattner01a22022005-10-10 22:04:48 +00002266SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2267 SDOperand Chain = N->getOperand(0);
2268 SDOperand Ptr = N->getOperand(1);
2269 SDOperand SrcValue = N->getOperand(2);
Chris Lattnere4b95392006-03-31 18:06:18 +00002270
2271 // If there are no uses of the loaded value, change uses of the chain value
2272 // into uses of the chain input (i.e. delete the dead load).
2273 if (N->hasNUsesOfValue(0, 0))
2274 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
Chris Lattner01a22022005-10-10 22:04:48 +00002275
2276 // If this load is directly stored, replace the load value with the stored
2277 // value.
2278 // TODO: Handle store large -> read small portion.
2279 // TODO: Handle TRUNCSTORE/EXTLOAD
2280 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2281 Chain.getOperand(1).getValueType() == N->getValueType(0))
2282 return CombineTo(N, Chain.getOperand(1), Chain);
2283
2284 return SDOperand();
2285}
2286
Chris Lattner29cd7db2006-03-31 18:10:41 +00002287/// visitXEXTLOAD - Handle EXTLOAD/ZEXTLOAD/SEXTLOAD.
2288SDOperand DAGCombiner::visitXEXTLOAD(SDNode *N) {
2289 SDOperand Chain = N->getOperand(0);
2290 SDOperand Ptr = N->getOperand(1);
2291 SDOperand SrcValue = N->getOperand(2);
2292 SDOperand EVT = N->getOperand(3);
2293
2294 // If there are no uses of the loaded value, change uses of the chain value
2295 // into uses of the chain input (i.e. delete the dead load).
2296 if (N->hasNUsesOfValue(0, 0))
2297 return CombineTo(N, DAG.getNode(ISD::UNDEF, N->getValueType(0)), Chain);
2298
2299 return SDOperand();
2300}
2301
Chris Lattner87514ca2005-10-10 22:31:19 +00002302SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2303 SDOperand Chain = N->getOperand(0);
2304 SDOperand Value = N->getOperand(1);
2305 SDOperand Ptr = N->getOperand(2);
2306 SDOperand SrcValue = N->getOperand(3);
2307
2308 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002309 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002310 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2311 // Make sure that these stores are the same value type:
2312 // FIXME: we really care that the second store is >= size of the first.
2313 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002314 // Create a new store of Value that replaces both stores.
2315 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002316 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2317 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002318 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2319 PrevStore->getOperand(0), Value, Ptr,
2320 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002321 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002322 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002323 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002324 }
2325
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002326 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002327 // FIXME: This needs to know that the resultant store does not need a
2328 // higher alignment than the original.
2329 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002330 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2331 Ptr, SrcValue);
2332
Chris Lattner87514ca2005-10-10 22:31:19 +00002333 return SDOperand();
2334}
2335
Chris Lattnerca242442006-03-19 01:27:56 +00002336SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2337 SDOperand InVec = N->getOperand(0);
2338 SDOperand InVal = N->getOperand(1);
2339 SDOperand EltNo = N->getOperand(2);
2340
2341 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2342 // vector with the inserted element.
2343 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2344 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2345 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2346 if (Elt < Ops.size())
2347 Ops[Elt] = InVal;
2348 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), Ops);
2349 }
2350
2351 return SDOperand();
2352}
2353
2354SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2355 SDOperand InVec = N->getOperand(0);
2356 SDOperand InVal = N->getOperand(1);
2357 SDOperand EltNo = N->getOperand(2);
2358 SDOperand NumElts = N->getOperand(3);
2359 SDOperand EltType = N->getOperand(4);
2360
2361 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2362 // vector with the inserted element.
2363 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2364 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2365 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2366 if (Elt < Ops.size()-2)
2367 Ops[Elt] = InVal;
2368 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), Ops);
2369 }
2370
2371 return SDOperand();
2372}
2373
Chris Lattnerd7648c82006-03-28 20:28:38 +00002374SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2375 unsigned NumInScalars = N->getNumOperands()-2;
2376 SDOperand NumElts = N->getOperand(NumInScalars);
2377 SDOperand EltType = N->getOperand(NumInScalars+1);
2378
2379 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2380 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2381 // two distinct vectors, turn this into a shuffle node.
2382 SDOperand VecIn1, VecIn2;
2383 for (unsigned i = 0; i != NumInScalars; ++i) {
2384 // Ignore undef inputs.
2385 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2386
2387 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2388 // constant index, bail out.
2389 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2390 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2391 VecIn1 = VecIn2 = SDOperand(0, 0);
2392 break;
2393 }
2394
2395 // If the input vector type disagrees with the result of the vbuild_vector,
2396 // we can't make a shuffle.
2397 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2398 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2399 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2400 VecIn1 = VecIn2 = SDOperand(0, 0);
2401 break;
2402 }
2403
2404 // Otherwise, remember this. We allow up to two distinct input vectors.
2405 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2406 continue;
2407
2408 if (VecIn1.Val == 0) {
2409 VecIn1 = ExtractedFromVec;
2410 } else if (VecIn2.Val == 0) {
2411 VecIn2 = ExtractedFromVec;
2412 } else {
2413 // Too many inputs.
2414 VecIn1 = VecIn2 = SDOperand(0, 0);
2415 break;
2416 }
2417 }
2418
2419 // If everything is good, we can make a shuffle operation.
2420 if (VecIn1.Val) {
2421 std::vector<SDOperand> BuildVecIndices;
2422 for (unsigned i = 0; i != NumInScalars; ++i) {
2423 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2424 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2425 continue;
2426 }
2427
2428 SDOperand Extract = N->getOperand(i);
2429
2430 // If extracting from the first vector, just use the index directly.
2431 if (Extract.getOperand(0) == VecIn1) {
2432 BuildVecIndices.push_back(Extract.getOperand(1));
2433 continue;
2434 }
2435
2436 // Otherwise, use InIdx + VecSize
2437 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2438 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2439 }
2440
2441 // Add count and size info.
2442 BuildVecIndices.push_back(NumElts);
2443 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2444
2445 // Return the new VVECTOR_SHUFFLE node.
2446 std::vector<SDOperand> Ops;
2447 Ops.push_back(VecIn1);
Chris Lattnercef896e2006-03-28 22:19:47 +00002448 if (VecIn2.Val) {
2449 Ops.push_back(VecIn2);
2450 } else {
2451 // Use an undef vbuild_vector as input for the second operand.
2452 std::vector<SDOperand> UnOps(NumInScalars,
2453 DAG.getNode(ISD::UNDEF,
2454 cast<VTSDNode>(EltType)->getVT()));
2455 UnOps.push_back(NumElts);
2456 UnOps.push_back(EltType);
2457 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, UnOps));
2458 }
Chris Lattnerd7648c82006-03-28 20:28:38 +00002459 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR,MVT::Vector, BuildVecIndices));
2460 Ops.push_back(NumElts);
2461 Ops.push_back(EltType);
2462 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2463 }
2464
2465 return SDOperand();
2466}
2467
Chris Lattner66445d32006-03-28 22:11:53 +00002468SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002469 SDOperand ShufMask = N->getOperand(2);
2470 unsigned NumElts = ShufMask.getNumOperands();
2471
2472 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2473 bool isIdentity = true;
2474 for (unsigned i = 0; i != NumElts; ++i) {
2475 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2476 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2477 isIdentity = false;
2478 break;
2479 }
2480 }
2481 if (isIdentity) return N->getOperand(0);
2482
2483 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2484 isIdentity = true;
2485 for (unsigned i = 0; i != NumElts; ++i) {
2486 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2487 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2488 isIdentity = false;
2489 break;
2490 }
2491 }
2492 if (isIdentity) return N->getOperand(1);
2493
Chris Lattner66445d32006-03-28 22:11:53 +00002494 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2495 if (N->getOperand(0) == N->getOperand(1)) {
2496 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2497 // first operand.
2498 std::vector<SDOperand> MappedOps;
Chris Lattner66445d32006-03-28 22:11:53 +00002499 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
2500 if (cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() >= NumElts) {
2501 unsigned NewIdx =
2502 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2503 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2504 } else {
2505 MappedOps.push_back(ShufMask.getOperand(i));
2506 }
2507 }
2508 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
2509 MappedOps);
2510 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
2511 N->getOperand(0),
2512 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2513 ShufMask);
2514 }
2515
2516 return SDOperand();
2517}
2518
Chris Lattnerf1d0c622006-03-31 22:16:43 +00002519SDOperand DAGCombiner::visitVVECTOR_SHUFFLE(SDNode *N) {
2520 SDOperand ShufMask = N->getOperand(2);
2521 unsigned NumElts = ShufMask.getNumOperands()-2;
2522
2523 // If the shuffle mask is an identity operation on the LHS, return the LHS.
2524 bool isIdentity = true;
2525 for (unsigned i = 0; i != NumElts; ++i) {
2526 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2527 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
2528 isIdentity = false;
2529 break;
2530 }
2531 }
2532 if (isIdentity) return N->getOperand(0);
2533
2534 // If the shuffle mask is an identity operation on the RHS, return the RHS.
2535 isIdentity = true;
2536 for (unsigned i = 0; i != NumElts; ++i) {
2537 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
2538 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
2539 isIdentity = false;
2540 break;
2541 }
2542 }
2543 if (isIdentity) return N->getOperand(1);
2544
2545 return SDOperand();
2546}
2547
Nate Begeman44728a72005-09-19 22:34:01 +00002548SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002549 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2550
2551 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2552 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2553 // If we got a simplified select_cc node back from SimplifySelectCC, then
2554 // break it down into a new SETCC node, and a new SELECT node, and then return
2555 // the SELECT node, since we were called with a SELECT node.
2556 if (SCC.Val) {
2557 // Check to see if we got a select_cc back (to turn into setcc/select).
2558 // Otherwise, just return whatever node we got back, like fabs.
2559 if (SCC.getOpcode() == ISD::SELECT_CC) {
2560 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2561 SCC.getOperand(0), SCC.getOperand(1),
2562 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002563 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002564 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2565 SCC.getOperand(3), SETCC);
2566 }
2567 return SCC;
2568 }
Nate Begeman44728a72005-09-19 22:34:01 +00002569 return SDOperand();
2570}
2571
Chris Lattner40c62d52005-10-18 06:04:22 +00002572/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2573/// are the two values being selected between, see if we can simplify the
2574/// select.
2575///
2576bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2577 SDOperand RHS) {
2578
2579 // If this is a select from two identical things, try to pull the operation
2580 // through the select.
2581 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2582#if 0
2583 std::cerr << "SELECT: ["; LHS.Val->dump();
2584 std::cerr << "] ["; RHS.Val->dump();
2585 std::cerr << "]\n";
2586#endif
2587
2588 // If this is a load and the token chain is identical, replace the select
2589 // of two loads with a load through a select of the address to load from.
2590 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2591 // constants have been dropped into the constant pool.
2592 if ((LHS.getOpcode() == ISD::LOAD ||
2593 LHS.getOpcode() == ISD::EXTLOAD ||
2594 LHS.getOpcode() == ISD::ZEXTLOAD ||
2595 LHS.getOpcode() == ISD::SEXTLOAD) &&
2596 // Token chains must be identical.
2597 LHS.getOperand(0) == RHS.getOperand(0) &&
2598 // If this is an EXTLOAD, the VT's must match.
2599 (LHS.getOpcode() == ISD::LOAD ||
2600 LHS.getOperand(3) == RHS.getOperand(3))) {
2601 // FIXME: this conflates two src values, discarding one. This is not
2602 // the right thing to do, but nothing uses srcvalues now. When they do,
2603 // turn SrcValue into a list of locations.
2604 SDOperand Addr;
2605 if (TheSelect->getOpcode() == ISD::SELECT)
2606 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2607 TheSelect->getOperand(0), LHS.getOperand(1),
2608 RHS.getOperand(1));
2609 else
2610 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2611 TheSelect->getOperand(0),
2612 TheSelect->getOperand(1),
2613 LHS.getOperand(1), RHS.getOperand(1),
2614 TheSelect->getOperand(4));
2615
2616 SDOperand Load;
2617 if (LHS.getOpcode() == ISD::LOAD)
2618 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2619 Addr, LHS.getOperand(2));
2620 else
2621 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2622 LHS.getOperand(0), Addr, LHS.getOperand(2),
2623 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2624 // Users of the select now use the result of the load.
2625 CombineTo(TheSelect, Load);
2626
2627 // Users of the old loads now use the new load's chain. We know the
2628 // old-load value is dead now.
2629 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2630 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2631 return true;
2632 }
2633 }
2634
2635 return false;
2636}
2637
Nate Begeman44728a72005-09-19 22:34:01 +00002638SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2639 SDOperand N2, SDOperand N3,
2640 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002641
2642 MVT::ValueType VT = N2.getValueType();
2643 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2644 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2645 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2646 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2647
2648 // Determine if the condition we're dealing with is constant
2649 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2650 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2651
2652 // fold select_cc true, x, y -> x
2653 if (SCCC && SCCC->getValue())
2654 return N2;
2655 // fold select_cc false, x, y -> y
2656 if (SCCC && SCCC->getValue() == 0)
2657 return N3;
2658
2659 // Check to see if we can simplify the select into an fabs node
2660 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2661 // Allow either -0.0 or 0.0
2662 if (CFP->getValue() == 0.0) {
2663 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2664 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2665 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2666 N2 == N3.getOperand(0))
2667 return DAG.getNode(ISD::FABS, VT, N0);
2668
2669 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2670 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2671 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2672 N2.getOperand(0) == N3)
2673 return DAG.getNode(ISD::FABS, VT, N3);
2674 }
2675 }
2676
2677 // Check to see if we can perform the "gzip trick", transforming
2678 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2679 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2680 MVT::isInteger(N0.getValueType()) &&
2681 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2682 MVT::ValueType XType = N0.getValueType();
2683 MVT::ValueType AType = N2.getValueType();
2684 if (XType >= AType) {
2685 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002686 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002687 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2688 unsigned ShCtV = Log2_64(N2C->getValue());
2689 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2690 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2691 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00002692 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002693 if (XType > AType) {
2694 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002695 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002696 }
2697 return DAG.getNode(ISD::AND, AType, Shift, N2);
2698 }
2699 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2700 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2701 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002702 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002703 if (XType > AType) {
2704 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002705 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002706 }
2707 return DAG.getNode(ISD::AND, AType, Shift, N2);
2708 }
2709 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002710
2711 // fold select C, 16, 0 -> shl C, 4
2712 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2713 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2714 // Get a SetCC of the condition
2715 // FIXME: Should probably make sure that setcc is legal if we ever have a
2716 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00002717 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00002718 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00002719 if (AfterLegalize) {
2720 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002721 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00002722 } else {
2723 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002724 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00002725 }
Chris Lattner5750df92006-03-01 04:03:14 +00002726 AddToWorkList(SCC.Val);
2727 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00002728 // shl setcc result by log2 n2c
2729 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2730 DAG.getConstant(Log2_64(N2C->getValue()),
2731 TLI.getShiftAmountTy()));
2732 }
2733
Nate Begemanf845b452005-10-08 00:29:44 +00002734 // Check to see if this is the equivalent of setcc
2735 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2736 // otherwise, go ahead with the folds.
2737 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2738 MVT::ValueType XType = N0.getValueType();
2739 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2740 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2741 if (Res.getValueType() != VT)
2742 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2743 return Res;
2744 }
2745
2746 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2747 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2748 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2749 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2750 return DAG.getNode(ISD::SRL, XType, Ctlz,
2751 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2752 TLI.getShiftAmountTy()));
2753 }
2754 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2755 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2756 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2757 N0);
2758 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2759 DAG.getConstant(~0ULL, XType));
2760 return DAG.getNode(ISD::SRL, XType,
2761 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2762 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2763 TLI.getShiftAmountTy()));
2764 }
2765 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2766 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2767 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2768 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2769 TLI.getShiftAmountTy()));
2770 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2771 }
2772 }
2773
2774 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2775 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2776 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2777 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2778 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2779 MVT::ValueType XType = N0.getValueType();
2780 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2781 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2782 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2783 TLI.getShiftAmountTy()));
2784 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002785 AddToWorkList(Shift.Val);
2786 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002787 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2788 }
2789 }
2790 }
2791
Nate Begeman44728a72005-09-19 22:34:01 +00002792 return SDOperand();
2793}
2794
Nate Begeman452d7be2005-09-16 00:54:12 +00002795SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002796 SDOperand N1, ISD::CondCode Cond,
2797 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002798 // These setcc operations always fold.
2799 switch (Cond) {
2800 default: break;
2801 case ISD::SETFALSE:
2802 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2803 case ISD::SETTRUE:
2804 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2805 }
2806
2807 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2808 uint64_t C1 = N1C->getValue();
2809 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2810 uint64_t C0 = N0C->getValue();
2811
2812 // Sign extend the operands if required
2813 if (ISD::isSignedIntSetCC(Cond)) {
2814 C0 = N0C->getSignExtended();
2815 C1 = N1C->getSignExtended();
2816 }
2817
2818 switch (Cond) {
2819 default: assert(0 && "Unknown integer setcc!");
2820 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2821 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2822 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2823 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2824 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2825 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2826 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2827 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2828 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2829 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2830 }
2831 } else {
2832 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2833 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2834 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2835
2836 // If the comparison constant has bits in the upper part, the
2837 // zero-extended value could never match.
2838 if (C1 & (~0ULL << InSize)) {
2839 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2840 switch (Cond) {
2841 case ISD::SETUGT:
2842 case ISD::SETUGE:
2843 case ISD::SETEQ: return DAG.getConstant(0, VT);
2844 case ISD::SETULT:
2845 case ISD::SETULE:
2846 case ISD::SETNE: return DAG.getConstant(1, VT);
2847 case ISD::SETGT:
2848 case ISD::SETGE:
2849 // True if the sign bit of C1 is set.
2850 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2851 case ISD::SETLT:
2852 case ISD::SETLE:
2853 // True if the sign bit of C1 isn't set.
2854 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2855 default:
2856 break;
2857 }
2858 }
2859
2860 // Otherwise, we can perform the comparison with the low bits.
2861 switch (Cond) {
2862 case ISD::SETEQ:
2863 case ISD::SETNE:
2864 case ISD::SETUGT:
2865 case ISD::SETUGE:
2866 case ISD::SETULT:
2867 case ISD::SETULE:
2868 return DAG.getSetCC(VT, N0.getOperand(0),
2869 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2870 Cond);
2871 default:
2872 break; // todo, be more careful with signed comparisons
2873 }
2874 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2875 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2876 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2877 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2878 MVT::ValueType ExtDstTy = N0.getValueType();
2879 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2880
2881 // If the extended part has any inconsistent bits, it cannot ever
2882 // compare equal. In other words, they have to be all ones or all
2883 // zeros.
2884 uint64_t ExtBits =
2885 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2886 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2887 return DAG.getConstant(Cond == ISD::SETNE, VT);
2888
2889 SDOperand ZextOp;
2890 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2891 if (Op0Ty == ExtSrcTy) {
2892 ZextOp = N0.getOperand(0);
2893 } else {
2894 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2895 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2896 DAG.getConstant(Imm, Op0Ty));
2897 }
Chris Lattner5750df92006-03-01 04:03:14 +00002898 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002899 // Otherwise, make this a use of a zext.
2900 return DAG.getSetCC(VT, ZextOp,
2901 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2902 ExtDstTy),
2903 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002904 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2905 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2906 (N0.getOpcode() == ISD::XOR ||
2907 (N0.getOpcode() == ISD::AND &&
2908 N0.getOperand(0).getOpcode() == ISD::XOR &&
2909 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2910 isa<ConstantSDNode>(N0.getOperand(1)) &&
2911 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2912 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2913 // only do this if the top bits are known zero.
2914 if (TLI.MaskedValueIsZero(N1,
2915 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2916 // Okay, get the un-inverted input value.
2917 SDOperand Val;
2918 if (N0.getOpcode() == ISD::XOR)
2919 Val = N0.getOperand(0);
2920 else {
2921 assert(N0.getOpcode() == ISD::AND &&
2922 N0.getOperand(0).getOpcode() == ISD::XOR);
2923 // ((X^1)&1)^1 -> X & 1
2924 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2925 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2926 }
2927 return DAG.getSetCC(VT, Val, N1,
2928 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2929 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002930 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002931
Nate Begeman452d7be2005-09-16 00:54:12 +00002932 uint64_t MinVal, MaxVal;
2933 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2934 if (ISD::isSignedIntSetCC(Cond)) {
2935 MinVal = 1ULL << (OperandBitSize-1);
2936 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2937 MaxVal = ~0ULL >> (65-OperandBitSize);
2938 else
2939 MaxVal = 0;
2940 } else {
2941 MinVal = 0;
2942 MaxVal = ~0ULL >> (64-OperandBitSize);
2943 }
2944
2945 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2946 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2947 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2948 --C1; // X >= C0 --> X > (C0-1)
2949 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2950 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2951 }
2952
2953 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2954 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2955 ++C1; // X <= C0 --> X < (C0+1)
2956 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2957 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2958 }
2959
2960 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2961 return DAG.getConstant(0, VT); // X < MIN --> false
2962
2963 // Canonicalize setgt X, Min --> setne X, Min
2964 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2965 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002966 // Canonicalize setlt X, Max --> setne X, Max
2967 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2968 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002969
2970 // If we have setult X, 1, turn it into seteq X, 0
2971 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2972 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2973 ISD::SETEQ);
2974 // If we have setugt X, Max-1, turn it into seteq X, Max
2975 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2976 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2977 ISD::SETEQ);
2978
2979 // If we have "setcc X, C0", check to see if we can shrink the immediate
2980 // by changing cc.
2981
2982 // SETUGT X, SINTMAX -> SETLT X, 0
2983 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2984 C1 == (~0ULL >> (65-OperandBitSize)))
2985 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2986 ISD::SETLT);
2987
2988 // FIXME: Implement the rest of these.
2989
2990 // Fold bit comparisons when we can.
2991 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2992 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2993 if (ConstantSDNode *AndRHS =
2994 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2995 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2996 // Perform the xform if the AND RHS is a single bit.
2997 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2998 return DAG.getNode(ISD::SRL, VT, N0,
2999 DAG.getConstant(Log2_64(AndRHS->getValue()),
3000 TLI.getShiftAmountTy()));
3001 }
3002 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
3003 // (X & 8) == 8 --> (X & 8) >> 3
3004 // Perform the xform if C1 is a single bit.
3005 if ((C1 & (C1-1)) == 0) {
3006 return DAG.getNode(ISD::SRL, VT, N0,
3007 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
3008 }
3009 }
3010 }
3011 }
3012 } else if (isa<ConstantSDNode>(N0.Val)) {
3013 // Ensure that the constant occurs on the RHS.
3014 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3015 }
3016
3017 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
3018 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
3019 double C0 = N0C->getValue(), C1 = N1C->getValue();
3020
3021 switch (Cond) {
3022 default: break; // FIXME: Implement the rest of these!
3023 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
3024 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
3025 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
3026 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
3027 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
3028 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
3029 }
3030 } else {
3031 // Ensure that the constant occurs on the RHS.
3032 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
3033 }
3034
3035 if (N0 == N1) {
3036 // We can always fold X == Y for integer setcc's.
3037 if (MVT::isInteger(N0.getValueType()))
3038 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3039 unsigned UOF = ISD::getUnorderedFlavor(Cond);
3040 if (UOF == 2) // FP operators that are undefined on NaNs.
3041 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
3042 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
3043 return DAG.getConstant(UOF, VT);
3044 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
3045 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00003046 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00003047 if (NewCond != Cond)
3048 return DAG.getSetCC(VT, N0, N1, NewCond);
3049 }
3050
3051 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
3052 MVT::isInteger(N0.getValueType())) {
3053 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
3054 N0.getOpcode() == ISD::XOR) {
3055 // Simplify (X+Y) == (X+Z) --> Y == Z
3056 if (N0.getOpcode() == N1.getOpcode()) {
3057 if (N0.getOperand(0) == N1.getOperand(0))
3058 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
3059 if (N0.getOperand(1) == N1.getOperand(1))
3060 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
3061 if (isCommutativeBinOp(N0.getOpcode())) {
3062 // If X op Y == Y op X, try other combinations.
3063 if (N0.getOperand(0) == N1.getOperand(1))
3064 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
3065 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00003066 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00003067 }
3068 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003069
3070 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
3071 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3072 // Turn (X+C1) == C2 --> X == C2-C1
3073 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
3074 return DAG.getSetCC(VT, N0.getOperand(0),
3075 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
3076 N0.getValueType()), Cond);
3077 }
3078
3079 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
3080 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00003081 // If we know that all of the inverted bits are zero, don't bother
3082 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003083 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00003084 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003085 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00003086 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003087 }
3088
3089 // Turn (C1-X) == C2 --> X == C1-C2
3090 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3091 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3092 return DAG.getSetCC(VT, N0.getOperand(1),
3093 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3094 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003095 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003096 }
3097 }
3098
Nate Begeman452d7be2005-09-16 00:54:12 +00003099 // Simplify (X+Z) == X --> Z == 0
3100 if (N0.getOperand(0) == N1)
3101 return DAG.getSetCC(VT, N0.getOperand(1),
3102 DAG.getConstant(0, N0.getValueType()), Cond);
3103 if (N0.getOperand(1) == N1) {
3104 if (isCommutativeBinOp(N0.getOpcode()))
3105 return DAG.getSetCC(VT, N0.getOperand(0),
3106 DAG.getConstant(0, N0.getValueType()), Cond);
3107 else {
3108 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3109 // (Z-X) == X --> Z == X<<1
3110 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3111 N1,
3112 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003113 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003114 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3115 }
3116 }
3117 }
3118
3119 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3120 N1.getOpcode() == ISD::XOR) {
3121 // Simplify X == (X+Z) --> Z == 0
3122 if (N1.getOperand(0) == N0) {
3123 return DAG.getSetCC(VT, N1.getOperand(1),
3124 DAG.getConstant(0, N1.getValueType()), Cond);
3125 } else if (N1.getOperand(1) == N0) {
3126 if (isCommutativeBinOp(N1.getOpcode())) {
3127 return DAG.getSetCC(VT, N1.getOperand(0),
3128 DAG.getConstant(0, N1.getValueType()), Cond);
3129 } else {
3130 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3131 // X == (Z-X) --> X<<1 == Z
3132 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3133 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003134 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003135 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3136 }
3137 }
3138 }
3139 }
3140
3141 // Fold away ALL boolean setcc's.
3142 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003143 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003144 switch (Cond) {
3145 default: assert(0 && "Unknown integer setcc!");
3146 case ISD::SETEQ: // X == Y -> (X^Y)^1
3147 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3148 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003149 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003150 break;
3151 case ISD::SETNE: // X != Y --> (X^Y)
3152 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3153 break;
3154 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3155 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3156 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3157 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003158 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003159 break;
3160 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3161 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3162 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3163 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003164 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003165 break;
3166 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3167 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3168 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3169 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003170 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003171 break;
3172 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3173 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3174 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3175 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3176 break;
3177 }
3178 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003179 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003180 // FIXME: If running after legalize, we probably can't do this.
3181 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3182 }
3183 return N0;
3184 }
3185
3186 // Could not fold it.
3187 return SDOperand();
3188}
3189
Nate Begeman69575232005-10-20 02:15:44 +00003190/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3191/// return a DAG expression to select that will generate the same value by
3192/// multiplying by a magic number. See:
3193/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3194SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
3195 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003196
3197 // Check to see if we can do this.
3198 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3199 return SDOperand(); // BuildSDIV only operates on i32 or i64
3200 if (!TLI.isOperationLegal(ISD::MULHS, VT))
3201 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00003202
Nate Begemanc6a454e2005-10-20 17:45:03 +00003203 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00003204 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
3205
3206 // Multiply the numerator (operand 0) by the magic value
3207 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
3208 DAG.getConstant(magics.m, VT));
3209 // If d > 0 and m < 0, add the numerator
3210 if (d > 0 && magics.m < 0) {
3211 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003212 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003213 }
3214 // If d < 0 and m > 0, subtract the numerator.
3215 if (d < 0 && magics.m > 0) {
3216 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003217 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003218 }
3219 // Shift right algebraic if shift value is nonzero
3220 if (magics.s > 0) {
3221 Q = DAG.getNode(ISD::SRA, VT, Q,
3222 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003223 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003224 }
3225 // Extract the sign bit and add it to the quotient
3226 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00003227 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
3228 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003229 AddToWorkList(T.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003230 return DAG.getNode(ISD::ADD, VT, Q, T);
3231}
3232
3233/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3234/// return a DAG expression to select that will generate the same value by
3235/// multiplying by a magic number. See:
3236/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3237SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3238 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003239
3240 // Check to see if we can do this.
3241 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3242 return SDOperand(); // BuildUDIV only operates on i32 or i64
3243 if (!TLI.isOperationLegal(ISD::MULHU, VT))
3244 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00003245
3246 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
3247 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
3248
3249 // Multiply the numerator (operand 0) by the magic value
3250 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
3251 DAG.getConstant(magics.m, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00003252 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003253
3254 if (magics.a == 0) {
3255 return DAG.getNode(ISD::SRL, VT, Q,
3256 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3257 } else {
3258 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003259 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003260 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
3261 DAG.getConstant(1, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003262 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003263 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003264 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003265 return DAG.getNode(ISD::SRL, VT, NPQ,
3266 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
3267 }
3268}
3269
Nate Begeman1d4d4142005-09-01 00:19:25 +00003270// SelectionDAG::Combine - This is the entry point for the file.
3271//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003272void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003273 /// run - This is the main entry point to this class.
3274 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003275 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003276}