blob: 1c90f7843589a7990493a1035f92327e2eda4052 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000025// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman646d7e22005-09-02 21:18:40 +000026// FIXME: divide by zero is currently left unfolded. do we want to turn this
27// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000028// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000029//
30//===----------------------------------------------------------------------===//
31
32#define DEBUG_TYPE "dagcombine"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000038#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000039#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000040#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041using namespace llvm;
42
43namespace {
44 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
45
46 class DAGCombiner {
47 SelectionDAG &DAG;
48 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000049 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000050
51 // Worklist of all of the nodes that need to be simplified.
52 std::vector<SDNode*> WorkList;
53
54 /// AddUsersToWorkList - When an instruction is simplified, add all users of
55 /// the instruction to the work lists because they might get more simplified
56 /// now.
57 ///
58 void AddUsersToWorkList(SDNode *N) {
59 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000060 UI != UE; ++UI)
61 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000062 }
63
64 /// removeFromWorkList - remove all instances of N from the worklist.
Chris Lattner5750df92006-03-01 04:03:14 +000065 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000066 void removeFromWorkList(SDNode *N) {
67 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
68 WorkList.end());
69 }
70
Chris Lattner24664722006-03-01 04:53:38 +000071 public:
Chris Lattner5750df92006-03-01 04:03:14 +000072 void AddToWorkList(SDNode *N) {
73 WorkList.push_back(N);
74 }
75
Chris Lattner01a22022005-10-10 22:04:48 +000076 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000077 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000078 DEBUG(std::cerr << "\nReplacing "; N->dump();
79 std::cerr << "\nWith: "; To[0].Val->dump();
80 std::cerr << " and " << To.size()-1 << " other values\n");
81 std::vector<SDNode*> NowDead;
82 DAG.ReplaceAllUsesWith(N, To, &NowDead);
83
84 // Push the new nodes and any users onto the worklist
85 for (unsigned i = 0, e = To.size(); i != e; ++i) {
86 WorkList.push_back(To[i].Val);
87 AddUsersToWorkList(To[i].Val);
88 }
89
90 // Nodes can end up on the worklist more than once. Make sure we do
91 // not process a node that has been replaced.
92 removeFromWorkList(N);
93 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
94 removeFromWorkList(NowDead[i]);
95
96 // Finally, since the node is now dead, remove it from the graph.
97 DAG.DeleteNode(N);
98 return SDOperand(N, 0);
99 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000100
Chris Lattner24664722006-03-01 04:53:38 +0000101 SDOperand CombineTo(SDNode *N, SDOperand Res) {
102 std::vector<SDOperand> To;
103 To.push_back(Res);
104 return CombineTo(N, To);
105 }
106
107 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
108 std::vector<SDOperand> To;
109 To.push_back(Res0);
110 To.push_back(Res1);
111 return CombineTo(N, To);
112 }
113 private:
114
Chris Lattner012f2412006-02-17 21:58:01 +0000115 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000116 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000117 /// propagation. If so, return true.
118 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000119 TargetLowering::TargetLoweringOpt TLO(DAG);
120 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000121 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
122 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
123 return false;
124
125 // Revisit the node.
126 WorkList.push_back(Op.Val);
127
128 // Replace the old value with the new one.
129 ++NodesCombined;
130 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
131 std::cerr << "\nWith: "; TLO.New.Val->dump());
132
133 std::vector<SDNode*> NowDead;
134 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
135
Chris Lattner7d20d392006-02-20 06:51:04 +0000136 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000137 WorkList.push_back(TLO.New.Val);
138 AddUsersToWorkList(TLO.New.Val);
139
140 // Nodes can end up on the worklist more than once. Make sure we do
141 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000142 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
143 removeFromWorkList(NowDead[i]);
144
Chris Lattner7d20d392006-02-20 06:51:04 +0000145 // Finally, if the node is now dead, remove it from the graph. The node
146 // may not be dead if the replacement process recursively simplified to
147 // something else needing this node.
148 if (TLO.Old.Val->use_empty()) {
149 removeFromWorkList(TLO.Old.Val);
150 DAG.DeleteNode(TLO.Old.Val);
151 }
Chris Lattner012f2412006-02-17 21:58:01 +0000152 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000153 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000154
Nate Begeman1d4d4142005-09-01 00:19:25 +0000155 /// visit - call the node-specific routine that knows how to fold each
156 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000157 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000158
159 // Visitation implementation - Implement dag node combining for different
160 // node types. The semantics are as follows:
161 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000162 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000163 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000164 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000166 SDOperand visitTokenFactor(SDNode *N);
167 SDOperand visitADD(SDNode *N);
168 SDOperand visitSUB(SDNode *N);
169 SDOperand visitMUL(SDNode *N);
170 SDOperand visitSDIV(SDNode *N);
171 SDOperand visitUDIV(SDNode *N);
172 SDOperand visitSREM(SDNode *N);
173 SDOperand visitUREM(SDNode *N);
174 SDOperand visitMULHU(SDNode *N);
175 SDOperand visitMULHS(SDNode *N);
176 SDOperand visitAND(SDNode *N);
177 SDOperand visitOR(SDNode *N);
178 SDOperand visitXOR(SDNode *N);
179 SDOperand visitSHL(SDNode *N);
180 SDOperand visitSRA(SDNode *N);
181 SDOperand visitSRL(SDNode *N);
182 SDOperand visitCTLZ(SDNode *N);
183 SDOperand visitCTTZ(SDNode *N);
184 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000185 SDOperand visitSELECT(SDNode *N);
186 SDOperand visitSELECT_CC(SDNode *N);
187 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000188 SDOperand visitSIGN_EXTEND(SDNode *N);
189 SDOperand visitZERO_EXTEND(SDNode *N);
190 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
191 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000192 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000193 SDOperand visitFADD(SDNode *N);
194 SDOperand visitFSUB(SDNode *N);
195 SDOperand visitFMUL(SDNode *N);
196 SDOperand visitFDIV(SDNode *N);
197 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000198 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000199 SDOperand visitSINT_TO_FP(SDNode *N);
200 SDOperand visitUINT_TO_FP(SDNode *N);
201 SDOperand visitFP_TO_SINT(SDNode *N);
202 SDOperand visitFP_TO_UINT(SDNode *N);
203 SDOperand visitFP_ROUND(SDNode *N);
204 SDOperand visitFP_ROUND_INREG(SDNode *N);
205 SDOperand visitFP_EXTEND(SDNode *N);
206 SDOperand visitFNEG(SDNode *N);
207 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000208 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000209 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000210 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000211 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000212 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
213 SDOperand visitVINSERT_VECTOR_ELT(SDNode *N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000214 SDOperand visitVBUILD_VECTOR(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000215 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000216
Nate Begemancd4d58c2006-02-03 06:46:56 +0000217 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
218
Chris Lattner40c62d52005-10-18 06:04:22 +0000219 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000220 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
221 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
222 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000223 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000224 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000225
226 SDOperand BuildSDIV(SDNode *N);
227 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000228public:
229 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000230 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000231
232 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000233 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000234 };
235}
236
Chris Lattner24664722006-03-01 04:53:38 +0000237//===----------------------------------------------------------------------===//
238// TargetLowering::DAGCombinerInfo implementation
239//===----------------------------------------------------------------------===//
240
241void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
242 ((DAGCombiner*)DC)->AddToWorkList(N);
243}
244
245SDOperand TargetLowering::DAGCombinerInfo::
246CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
247 return ((DAGCombiner*)DC)->CombineTo(N, To);
248}
249
250SDOperand TargetLowering::DAGCombinerInfo::
251CombineTo(SDNode *N, SDOperand Res) {
252 return ((DAGCombiner*)DC)->CombineTo(N, Res);
253}
254
255
256SDOperand TargetLowering::DAGCombinerInfo::
257CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
258 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
259}
260
261
262
263
264//===----------------------------------------------------------------------===//
265
266
Nate Begeman69575232005-10-20 02:15:44 +0000267struct ms {
268 int64_t m; // magic number
269 int64_t s; // shift amount
270};
271
272struct mu {
273 uint64_t m; // magic number
274 int64_t a; // add indicator
275 int64_t s; // shift amount
276};
277
278/// magic - calculate the magic numbers required to codegen an integer sdiv as
279/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
280/// or -1.
281static ms magic32(int32_t d) {
282 int32_t p;
283 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
284 const uint32_t two31 = 0x80000000U;
285 struct ms mag;
286
287 ad = abs(d);
288 t = two31 + ((uint32_t)d >> 31);
289 anc = t - 1 - t%ad; // absolute value of nc
290 p = 31; // initialize p
291 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
292 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
293 q2 = two31/ad; // initialize q2 = 2p/abs(d)
294 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
295 do {
296 p = p + 1;
297 q1 = 2*q1; // update q1 = 2p/abs(nc)
298 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
299 if (r1 >= anc) { // must be unsigned comparison
300 q1 = q1 + 1;
301 r1 = r1 - anc;
302 }
303 q2 = 2*q2; // update q2 = 2p/abs(d)
304 r2 = 2*r2; // update r2 = rem(2p/abs(d))
305 if (r2 >= ad) { // must be unsigned comparison
306 q2 = q2 + 1;
307 r2 = r2 - ad;
308 }
309 delta = ad - r2;
310 } while (q1 < delta || (q1 == delta && r1 == 0));
311
312 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
313 if (d < 0) mag.m = -mag.m; // resulting magic number
314 mag.s = p - 32; // resulting shift
315 return mag;
316}
317
318/// magicu - calculate the magic numbers required to codegen an integer udiv as
319/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
320static mu magicu32(uint32_t d) {
321 int32_t p;
322 uint32_t nc, delta, q1, r1, q2, r2;
323 struct mu magu;
324 magu.a = 0; // initialize "add" indicator
325 nc = - 1 - (-d)%d;
326 p = 31; // initialize p
327 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
328 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
329 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
330 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
331 do {
332 p = p + 1;
333 if (r1 >= nc - r1 ) {
334 q1 = 2*q1 + 1; // update q1
335 r1 = 2*r1 - nc; // update r1
336 }
337 else {
338 q1 = 2*q1; // update q1
339 r1 = 2*r1; // update r1
340 }
341 if (r2 + 1 >= d - r2) {
342 if (q2 >= 0x7FFFFFFF) magu.a = 1;
343 q2 = 2*q2 + 1; // update q2
344 r2 = 2*r2 + 1 - d; // update r2
345 }
346 else {
347 if (q2 >= 0x80000000) magu.a = 1;
348 q2 = 2*q2; // update q2
349 r2 = 2*r2 + 1; // update r2
350 }
351 delta = d - 1 - r2;
352 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
353 magu.m = q2 + 1; // resulting magic number
354 magu.s = p - 32; // resulting shift
355 return magu;
356}
357
358/// magic - calculate the magic numbers required to codegen an integer sdiv as
359/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
360/// or -1.
361static ms magic64(int64_t d) {
362 int64_t p;
363 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
364 const uint64_t two63 = 9223372036854775808ULL; // 2^63
365 struct ms mag;
366
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000367 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000368 t = two63 + ((uint64_t)d >> 63);
369 anc = t - 1 - t%ad; // absolute value of nc
370 p = 63; // initialize p
371 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
372 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
373 q2 = two63/ad; // initialize q2 = 2p/abs(d)
374 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
375 do {
376 p = p + 1;
377 q1 = 2*q1; // update q1 = 2p/abs(nc)
378 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
379 if (r1 >= anc) { // must be unsigned comparison
380 q1 = q1 + 1;
381 r1 = r1 - anc;
382 }
383 q2 = 2*q2; // update q2 = 2p/abs(d)
384 r2 = 2*r2; // update r2 = rem(2p/abs(d))
385 if (r2 >= ad) { // must be unsigned comparison
386 q2 = q2 + 1;
387 r2 = r2 - ad;
388 }
389 delta = ad - r2;
390 } while (q1 < delta || (q1 == delta && r1 == 0));
391
392 mag.m = q2 + 1;
393 if (d < 0) mag.m = -mag.m; // resulting magic number
394 mag.s = p - 64; // resulting shift
395 return mag;
396}
397
398/// magicu - calculate the magic numbers required to codegen an integer udiv as
399/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
400static mu magicu64(uint64_t d)
401{
402 int64_t p;
403 uint64_t nc, delta, q1, r1, q2, r2;
404 struct mu magu;
405 magu.a = 0; // initialize "add" indicator
406 nc = - 1 - (-d)%d;
407 p = 63; // initialize p
408 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
409 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
410 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
411 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
412 do {
413 p = p + 1;
414 if (r1 >= nc - r1 ) {
415 q1 = 2*q1 + 1; // update q1
416 r1 = 2*r1 - nc; // update r1
417 }
418 else {
419 q1 = 2*q1; // update q1
420 r1 = 2*r1; // update r1
421 }
422 if (r2 + 1 >= d - r2) {
423 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
424 q2 = 2*q2 + 1; // update q2
425 r2 = 2*r2 + 1 - d; // update r2
426 }
427 else {
428 if (q2 >= 0x8000000000000000ull) magu.a = 1;
429 q2 = 2*q2; // update q2
430 r2 = 2*r2 + 1; // update r2
431 }
432 delta = d - 1 - r2;
433 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
434 magu.m = q2 + 1; // resulting magic number
435 magu.s = p - 64; // resulting shift
436 return magu;
437}
438
Nate Begeman4ebd8052005-09-01 23:24:04 +0000439// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
440// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000441// Also, set the incoming LHS, RHS, and CC references to the appropriate
442// nodes based on the type of node we are checking. This simplifies life a
443// bit for the callers.
444static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
445 SDOperand &CC) {
446 if (N.getOpcode() == ISD::SETCC) {
447 LHS = N.getOperand(0);
448 RHS = N.getOperand(1);
449 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000450 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000451 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000452 if (N.getOpcode() == ISD::SELECT_CC &&
453 N.getOperand(2).getOpcode() == ISD::Constant &&
454 N.getOperand(3).getOpcode() == ISD::Constant &&
455 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000456 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
457 LHS = N.getOperand(0);
458 RHS = N.getOperand(1);
459 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000460 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000461 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000462 return false;
463}
464
Nate Begeman99801192005-09-07 23:25:52 +0000465// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
466// one use. If this is true, it allows the users to invert the operation for
467// free when it is profitable to do so.
468static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000469 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000470 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000471 return true;
472 return false;
473}
474
Nate Begeman452d7be2005-09-16 00:54:12 +0000475// FIXME: This should probably go in the ISD class rather than being duplicated
476// in several files.
477static bool isCommutativeBinOp(unsigned Opcode) {
478 switch (Opcode) {
479 case ISD::ADD:
480 case ISD::MUL:
481 case ISD::AND:
482 case ISD::OR:
483 case ISD::XOR: return true;
484 default: return false; // FIXME: Need commutative info for user ops!
485 }
486}
487
Nate Begemancd4d58c2006-02-03 06:46:56 +0000488SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
489 MVT::ValueType VT = N0.getValueType();
490 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
491 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
492 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
493 if (isa<ConstantSDNode>(N1)) {
494 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000495 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000496 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
497 } else if (N0.hasOneUse()) {
498 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000499 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000500 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
501 }
502 }
503 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
504 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
505 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
506 if (isa<ConstantSDNode>(N0)) {
507 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000508 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000509 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
510 } else if (N1.hasOneUse()) {
511 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000512 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000513 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
514 }
515 }
516 return SDOperand();
517}
518
Nate Begeman4ebd8052005-09-01 23:24:04 +0000519void DAGCombiner::Run(bool RunningAfterLegalize) {
520 // set the instance variable, so that the various visit routines may use it.
521 AfterLegalize = RunningAfterLegalize;
522
Nate Begeman646d7e22005-09-02 21:18:40 +0000523 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000524 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
525 E = DAG.allnodes_end(); I != E; ++I)
526 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000527
Chris Lattner95038592005-10-05 06:35:28 +0000528 // Create a dummy node (which is not added to allnodes), that adds a reference
529 // to the root node, preventing it from being deleted, and tracking any
530 // changes of the root.
531 HandleSDNode Dummy(DAG.getRoot());
532
Chris Lattner24664722006-03-01 04:53:38 +0000533
534 /// DagCombineInfo - Expose the DAG combiner to the target combiner impls.
535 TargetLowering::DAGCombinerInfo
536 DagCombineInfo(DAG, !RunningAfterLegalize, this);
537
Nate Begeman1d4d4142005-09-01 00:19:25 +0000538 // while the worklist isn't empty, inspect the node on the end of it and
539 // try and combine it.
540 while (!WorkList.empty()) {
541 SDNode *N = WorkList.back();
542 WorkList.pop_back();
543
544 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000545 // N is deleted from the DAG, since they too may now be dead or may have a
546 // reduced number of uses, allowing other xforms.
547 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000548 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
549 WorkList.push_back(N->getOperand(i).Val);
550
Nate Begeman1d4d4142005-09-01 00:19:25 +0000551 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000552 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553 continue;
554 }
555
Nate Begeman83e75ec2005-09-06 04:43:02 +0000556 SDOperand RV = visit(N);
Chris Lattner24664722006-03-01 04:53:38 +0000557
558 // If nothing happened, try a target-specific DAG combine.
559 if (RV.Val == 0) {
560 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
561 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode()))
562 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
563 }
564
Nate Begeman83e75ec2005-09-06 04:43:02 +0000565 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000566 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000567 // If we get back the same node we passed in, rather than a new node or
568 // zero, we know that the node must have defined multiple values and
569 // CombineTo was used. Since CombineTo takes care of the worklist
570 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000571 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000572 DEBUG(std::cerr << "\nReplacing "; N->dump();
573 std::cerr << "\nWith: "; RV.Val->dump();
574 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000575 std::vector<SDNode*> NowDead;
576 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000577
578 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000579 WorkList.push_back(RV.Val);
580 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000581
582 // Nodes can end up on the worklist more than once. Make sure we do
583 // not process a node that has been replaced.
584 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000585 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
586 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000587
588 // Finally, since the node is now dead, remove it from the graph.
589 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000590 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000591 }
592 }
Chris Lattner95038592005-10-05 06:35:28 +0000593
594 // If the root changed (e.g. it was a dead load, update the root).
595 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000596}
597
Nate Begeman83e75ec2005-09-06 04:43:02 +0000598SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000599 switch(N->getOpcode()) {
600 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000601 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000602 case ISD::ADD: return visitADD(N);
603 case ISD::SUB: return visitSUB(N);
604 case ISD::MUL: return visitMUL(N);
605 case ISD::SDIV: return visitSDIV(N);
606 case ISD::UDIV: return visitUDIV(N);
607 case ISD::SREM: return visitSREM(N);
608 case ISD::UREM: return visitUREM(N);
609 case ISD::MULHU: return visitMULHU(N);
610 case ISD::MULHS: return visitMULHS(N);
611 case ISD::AND: return visitAND(N);
612 case ISD::OR: return visitOR(N);
613 case ISD::XOR: return visitXOR(N);
614 case ISD::SHL: return visitSHL(N);
615 case ISD::SRA: return visitSRA(N);
616 case ISD::SRL: return visitSRL(N);
617 case ISD::CTLZ: return visitCTLZ(N);
618 case ISD::CTTZ: return visitCTTZ(N);
619 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000620 case ISD::SELECT: return visitSELECT(N);
621 case ISD::SELECT_CC: return visitSELECT_CC(N);
622 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000623 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
624 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
625 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
626 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000627 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000628 case ISD::FADD: return visitFADD(N);
629 case ISD::FSUB: return visitFSUB(N);
630 case ISD::FMUL: return visitFMUL(N);
631 case ISD::FDIV: return visitFDIV(N);
632 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000633 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000634 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
635 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
636 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
637 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
638 case ISD::FP_ROUND: return visitFP_ROUND(N);
639 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
640 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
641 case ISD::FNEG: return visitFNEG(N);
642 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000643 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000644 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000645 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000646 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000647 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
648 case ISD::VINSERT_VECTOR_ELT: return visitVINSERT_VECTOR_ELT(N);
Chris Lattnerd7648c82006-03-28 20:28:38 +0000649 case ISD::VBUILD_VECTOR: return visitVBUILD_VECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000650 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000652 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000653}
654
Nate Begeman83e75ec2005-09-06 04:43:02 +0000655SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000656 std::vector<SDOperand> Ops;
657 bool Changed = false;
658
Nate Begeman1d4d4142005-09-01 00:19:25 +0000659 // If the token factor has two operands and one is the entry token, replace
660 // the token factor with the other operand.
661 if (N->getNumOperands() == 2) {
662 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000663 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000664 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000665 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000666 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000667
Nate Begemanded49632005-10-13 03:11:28 +0000668 // fold (tokenfactor (tokenfactor)) -> tokenfactor
669 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
670 SDOperand Op = N->getOperand(i);
671 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
Chris Lattnerac0f8f22006-03-13 18:37:30 +0000672 AddToWorkList(Op.Val); // Remove dead node.
Nate Begemanded49632005-10-13 03:11:28 +0000673 Changed = true;
674 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
675 Ops.push_back(Op.getOperand(j));
676 } else {
677 Ops.push_back(Op);
678 }
679 }
680 if (Changed)
681 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000682 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000683}
684
Nate Begeman83e75ec2005-09-06 04:43:02 +0000685SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000686 SDOperand N0 = N->getOperand(0);
687 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
689 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000690 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000691
692 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000693 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000694 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000695 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000696 if (N0C && !N1C)
697 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000698 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000701 // fold ((c1-A)+c2) -> (c1+c2)-A
702 if (N1C && N0.getOpcode() == ISD::SUB)
703 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
704 return DAG.getNode(ISD::SUB, VT,
705 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
706 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000707 // reassociate add
708 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
709 if (RADD.Val != 0)
710 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000711 // fold ((0-A) + B) -> B-A
712 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
713 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000714 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000715 // fold (A + (0-B)) -> A-B
716 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
717 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000718 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000719 // fold (A+(B-A)) -> B
720 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000721 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000722
Evan Cheng860771d2006-03-01 01:09:54 +0000723 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemanb0d04a72006-02-18 02:40:58 +0000724 return SDOperand();
Chris Lattner947c2892006-03-13 06:51:27 +0000725
726 // fold (a+b) -> (a|b) iff a and b share no bits.
727 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
728 uint64_t LHSZero, LHSOne;
729 uint64_t RHSZero, RHSOne;
730 uint64_t Mask = MVT::getIntVTBitMask(VT);
731 TLI.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
732 if (LHSZero) {
733 TLI.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
734
735 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
736 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
737 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
738 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
739 return DAG.getNode(ISD::OR, VT, N0, N1);
740 }
741 }
742
Nate Begeman83e75ec2005-09-06 04:43:02 +0000743 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000744}
745
Nate Begeman83e75ec2005-09-06 04:43:02 +0000746SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000747 SDOperand N0 = N->getOperand(0);
748 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000749 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
750 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000751 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000752
Chris Lattner854077d2005-10-17 01:07:11 +0000753 // fold (sub x, x) -> 0
754 if (N0 == N1)
755 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000756 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000757 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000758 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000759 // fold (sub x, c) -> (add x, -c)
760 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000761 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000762 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000763 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000764 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000766 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000767 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000768 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000769}
770
Nate Begeman83e75ec2005-09-06 04:43:02 +0000771SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000772 SDOperand N0 = N->getOperand(0);
773 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000774 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
775 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000776 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000777
778 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000779 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000780 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000781 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000782 if (N0C && !N1C)
783 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000784 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000785 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000786 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000787 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000788 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000789 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000790 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000791 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000792 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000793 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000794 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000795 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
796 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
797 // FIXME: If the input is something that is easily negated (e.g. a
798 // single-use add), we should put the negate there.
799 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
800 DAG.getNode(ISD::SHL, VT, N0,
801 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
802 TLI.getShiftAmountTy())));
803 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000804
805 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
806 if (N1C && N0.getOpcode() == ISD::SHL &&
807 isa<ConstantSDNode>(N0.getOperand(1))) {
808 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000809 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000810 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
811 }
812
813 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
814 // use.
815 {
816 SDOperand Sh(0,0), Y(0,0);
817 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
818 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
819 N0.Val->hasOneUse()) {
820 Sh = N0; Y = N1;
821 } else if (N1.getOpcode() == ISD::SHL &&
822 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
823 Sh = N1; Y = N0;
824 }
825 if (Sh.Val) {
826 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
827 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
828 }
829 }
Chris Lattnera1deca32006-03-04 23:33:26 +0000830 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
831 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
832 isa<ConstantSDNode>(N0.getOperand(1))) {
833 return DAG.getNode(ISD::ADD, VT,
834 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
835 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
836 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000837
Nate Begemancd4d58c2006-02-03 06:46:56 +0000838 // reassociate mul
839 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
840 if (RMUL.Val != 0)
841 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000842 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000843}
844
Nate Begeman83e75ec2005-09-06 04:43:02 +0000845SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000846 SDOperand N0 = N->getOperand(0);
847 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000848 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
849 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000850 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000851
852 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000853 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000854 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000855 // fold (sdiv X, 1) -> X
856 if (N1C && N1C->getSignExtended() == 1LL)
857 return N0;
858 // fold (sdiv X, -1) -> 0-X
859 if (N1C && N1C->isAllOnesValue())
860 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000861 // If we know the sign bits of both operands are zero, strength reduce to a
862 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
863 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000864 if (TLI.MaskedValueIsZero(N1, SignBit) &&
865 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000866 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000867 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000868 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000869 (isPowerOf2_64(N1C->getSignExtended()) ||
870 isPowerOf2_64(-N1C->getSignExtended()))) {
871 // If dividing by powers of two is cheap, then don't perform the following
872 // fold.
873 if (TLI.isPow2DivCheap())
874 return SDOperand();
875 int64_t pow2 = N1C->getSignExtended();
876 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000877 unsigned lg2 = Log2_64(abs2);
878 // Splat the sign bit into the register
879 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000880 DAG.getConstant(MVT::getSizeInBits(VT)-1,
881 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000882 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000883 // Add (N0 < 0) ? abs2 - 1 : 0;
884 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
885 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000886 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000887 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000888 AddToWorkList(SRL.Val);
889 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000890 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
891 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000892 // If we're dividing by a positive value, we're done. Otherwise, we must
893 // negate the result.
894 if (pow2 > 0)
895 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000896 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000897 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
898 }
Nate Begeman69575232005-10-20 02:15:44 +0000899 // if integer divide is expensive and we satisfy the requirements, emit an
900 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000901 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000902 !TLI.isIntDivCheap()) {
903 SDOperand Op = BuildSDIV(N);
904 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000905 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000906 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000907}
908
Nate Begeman83e75ec2005-09-06 04:43:02 +0000909SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000910 SDOperand N0 = N->getOperand(0);
911 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000912 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
913 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000914 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000915
916 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000917 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000918 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000920 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000921 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000922 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000923 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000924 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
925 if (N1.getOpcode() == ISD::SHL) {
926 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
927 if (isPowerOf2_64(SHC->getValue())) {
928 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000929 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
930 DAG.getConstant(Log2_64(SHC->getValue()),
931 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000932 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000933 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000934 }
935 }
936 }
Nate Begeman69575232005-10-20 02:15:44 +0000937 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000938 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
939 SDOperand Op = BuildUDIV(N);
940 if (Op.Val) return Op;
941 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000942 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000943}
944
Nate Begeman83e75ec2005-09-06 04:43:02 +0000945SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000946 SDOperand N0 = N->getOperand(0);
947 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000948 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
949 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000950 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951
952 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000953 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000954 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000955 // If we know the sign bits of both operands are zero, strength reduce to a
956 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
957 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000958 if (TLI.MaskedValueIsZero(N1, SignBit) &&
959 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000960 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000961 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000962}
963
Nate Begeman83e75ec2005-09-06 04:43:02 +0000964SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000965 SDOperand N0 = N->getOperand(0);
966 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
968 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000969 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970
971 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000973 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000974 // fold (urem x, pow2) -> (and x, pow2-1)
975 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000976 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000977 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
978 if (N1.getOpcode() == ISD::SHL) {
979 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
980 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000981 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000982 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000983 return DAG.getNode(ISD::AND, VT, N0, Add);
984 }
985 }
986 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000987 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000988}
989
Nate Begeman83e75ec2005-09-06 04:43:02 +0000990SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000991 SDOperand N0 = N->getOperand(0);
992 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000993 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994
995 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000996 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000997 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000999 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1001 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001002 TLI.getShiftAmountTy()));
1003 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001004}
1005
Nate Begeman83e75ec2005-09-06 04:43:02 +00001006SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001007 SDOperand N0 = N->getOperand(0);
1008 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001009 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010
1011 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001012 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001013 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001015 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001016 return DAG.getConstant(0, N0.getValueType());
1017 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018}
1019
Nate Begeman83e75ec2005-09-06 04:43:02 +00001020SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001021 SDOperand N0 = N->getOperand(0);
1022 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001023 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001024 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1025 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001026 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +00001027 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001028
1029 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001030 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001031 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001032 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001033 if (N0C && !N1C)
1034 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001035 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001036 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001037 return N0;
1038 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +00001039 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001040 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001041 // reassociate and
1042 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1043 if (RAND.Val != 0)
1044 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001046 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001047 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001048 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001049 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001050 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1051 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001052 unsigned InMask = MVT::getIntVTBitMask(N0.getOperand(0).getValueType());
Chris Lattner3603cd62006-02-02 07:17:31 +00001053 if (TLI.MaskedValueIsZero(N0.getOperand(0),
Chris Lattner1ec05d12006-03-01 21:47:21 +00001054 ~N1C->getValue() & InMask)) {
1055 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
1056 N0.getOperand(0));
1057
1058 // Replace uses of the AND with uses of the Zero extend node.
1059 CombineTo(N, Zext);
1060
Chris Lattner3603cd62006-02-02 07:17:31 +00001061 // We actually want to replace all uses of the any_extend with the
1062 // zero_extend, to avoid duplicating things. This will later cause this
1063 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001064 CombineTo(N0.Val, Zext);
Chris Lattner3603cd62006-02-02 07:17:31 +00001065 return SDOperand();
1066 }
1067 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001068 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1069 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1070 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1071 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1072
1073 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1074 MVT::isInteger(LL.getValueType())) {
1075 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1076 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1077 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001078 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001079 return DAG.getSetCC(VT, ORNode, LR, Op1);
1080 }
1081 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1082 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1083 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001084 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001085 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1086 }
1087 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1088 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1089 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001090 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001091 return DAG.getSetCC(VT, ORNode, LR, Op1);
1092 }
1093 }
1094 // canonicalize equivalent to ll == rl
1095 if (LL == RR && LR == RL) {
1096 Op1 = ISD::getSetCCSwappedOperands(Op1);
1097 std::swap(RL, RR);
1098 }
1099 if (LL == RL && LR == RR) {
1100 bool isInteger = MVT::isInteger(LL.getValueType());
1101 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1102 if (Result != ISD::SETCC_INVALID)
1103 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1104 }
1105 }
1106 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1107 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1108 N1.getOpcode() == ISD::ZERO_EXTEND &&
1109 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1110 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1111 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001112 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001113 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1114 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001115 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001116 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001117 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1118 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001119 N0.getOperand(1) == N1.getOperand(1)) {
1120 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1121 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001122 AddToWorkList(ANDNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001123 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1124 }
Nate Begemande996292006-02-03 22:24:05 +00001125 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1126 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001127 if (!MVT::isVector(VT) &&
1128 SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001129 return SDOperand();
Nate Begemanded49632005-10-13 03:11:28 +00001130 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001131 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001132 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001133 // If we zero all the possible extended bits, then we can turn this into
1134 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001135 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001136 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001137 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1138 N0.getOperand(1), N0.getOperand(2),
1139 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001140 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001141 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001142 return SDOperand();
1143 }
1144 }
1145 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001146 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001147 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001148 // If we zero all the possible extended bits, then we can turn this into
1149 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001150 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001151 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001152 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1153 N0.getOperand(1), N0.getOperand(2),
1154 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001155 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001156 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001157 return SDOperand();
1158 }
1159 }
Chris Lattner15045b62006-02-28 06:35:35 +00001160
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001161 // fold (and (load x), 255) -> (zextload x, i8)
1162 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1163 if (N1C &&
1164 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1165 N0.getOpcode() == ISD::ZEXTLOAD) &&
1166 N0.hasOneUse()) {
1167 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001168 if (N1C->getValue() == 255)
1169 EVT = MVT::i8;
1170 else if (N1C->getValue() == 65535)
1171 EVT = MVT::i16;
1172 else if (N1C->getValue() == ~0U)
1173 EVT = MVT::i32;
1174 else
1175 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001176
1177 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1178 cast<VTSDNode>(N0.getOperand(3))->getVT();
1179 if (EVT != MVT::Other && LoadedVT > EVT) {
Chris Lattner15045b62006-02-28 06:35:35 +00001180 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1181 // For big endian targets, we need to add an offset to the pointer to load
1182 // the correct bytes. For little endian systems, we merely need to read
1183 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001184 unsigned PtrOff =
1185 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1186 SDOperand NewPtr = N0.getOperand(1);
1187 if (!TLI.isLittleEndian())
1188 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1189 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001190 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001191 SDOperand Load =
1192 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1193 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001194 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001195 CombineTo(N0.Val, Load, Load.getValue(1));
1196 return SDOperand();
1197 }
1198 }
1199
Nate Begeman83e75ec2005-09-06 04:43:02 +00001200 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001201}
1202
Nate Begeman83e75ec2005-09-06 04:43:02 +00001203SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001204 SDOperand N0 = N->getOperand(0);
1205 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001206 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001207 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1208 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001209 MVT::ValueType VT = N1.getValueType();
1210 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001211
1212 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001213 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001214 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001215 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001216 if (N0C && !N1C)
1217 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001219 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001220 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001221 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001222 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001223 return N1;
1224 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001225 if (N1C &&
1226 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001227 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001228 // reassociate or
1229 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1230 if (ROR.Val != 0)
1231 return ROR;
1232 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1233 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001234 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001235 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1236 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1237 N1),
1238 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001239 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001240 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1241 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1242 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1243 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1244
1245 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1246 MVT::isInteger(LL.getValueType())) {
1247 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1248 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1249 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1250 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1251 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001252 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001253 return DAG.getSetCC(VT, ORNode, LR, Op1);
1254 }
1255 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1256 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1257 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1258 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1259 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001260 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001261 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1262 }
1263 }
1264 // canonicalize equivalent to ll == rl
1265 if (LL == RR && LR == RL) {
1266 Op1 = ISD::getSetCCSwappedOperands(Op1);
1267 std::swap(RL, RR);
1268 }
1269 if (LL == RL && LR == RR) {
1270 bool isInteger = MVT::isInteger(LL.getValueType());
1271 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1272 if (Result != ISD::SETCC_INVALID)
1273 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1274 }
1275 }
1276 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1277 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1278 N1.getOpcode() == ISD::ZERO_EXTEND &&
1279 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1280 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1281 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001282 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001283 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1284 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001285 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1286 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1287 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1288 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1289 N0.getOperand(1) == N1.getOperand(1)) {
1290 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1291 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001292 AddToWorkList(ORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001293 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1294 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001295 // canonicalize shl to left side in a shl/srl pair, to match rotate
1296 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1297 std::swap(N0, N1);
1298 // check for rotl, rotr
1299 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1300 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001301 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001302 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1303 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1304 N1.getOperand(1).getOpcode() == ISD::Constant) {
1305 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1306 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1307 if ((c1val + c2val) == OpSizeInBits)
1308 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1309 }
1310 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1311 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1312 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1313 if (ConstantSDNode *SUBC =
1314 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1315 if (SUBC->getValue() == OpSizeInBits)
1316 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1317 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1318 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1319 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1320 if (ConstantSDNode *SUBC =
1321 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1322 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001323 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001324 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1325 N1.getOperand(1));
1326 else
1327 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1328 N0.getOperand(1));
1329 }
1330 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332}
1333
Nate Begeman83e75ec2005-09-06 04:43:02 +00001334SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001335 SDOperand N0 = N->getOperand(0);
1336 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001337 SDOperand LHS, RHS, CC;
1338 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1339 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340 MVT::ValueType VT = N0.getValueType();
1341
1342 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001343 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001344 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001345 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001346 if (N0C && !N1C)
1347 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001349 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001350 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001351 // reassociate xor
1352 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1353 if (RXOR.Val != 0)
1354 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001355 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001356 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1357 bool isInt = MVT::isInteger(LHS.getValueType());
1358 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1359 isInt);
1360 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001361 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001362 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001364 assert(0 && "Unhandled SetCC Equivalent!");
1365 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001366 }
Nate Begeman99801192005-09-07 23:25:52 +00001367 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1368 if (N1C && N1C->getValue() == 1 &&
1369 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001371 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1372 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001373 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1374 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001375 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001376 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377 }
1378 }
Nate Begeman99801192005-09-07 23:25:52 +00001379 // fold !(x or y) -> (!x and !y) iff x or y are constants
1380 if (N1C && N1C->isAllOnesValue() &&
1381 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001382 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001383 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1384 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1386 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001387 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001388 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389 }
1390 }
Nate Begeman223df222005-09-08 20:18:10 +00001391 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1392 if (N1C && N0.getOpcode() == ISD::XOR) {
1393 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1394 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1395 if (N00C)
1396 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1397 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1398 if (N01C)
1399 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1400 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1401 }
1402 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00001403 if (N0 == N1) {
1404 if (!MVT::isVector(VT)) {
1405 return DAG.getConstant(0, VT);
1406 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
1407 // Produce a vector of zeros.
1408 SDOperand El = DAG.getConstant(0, MVT::getVectorBaseType(VT));
1409 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
1410 return DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
1411 }
1412 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001413 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1414 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1415 N1.getOpcode() == ISD::ZERO_EXTEND &&
1416 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1417 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1418 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001419 AddToWorkList(XORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001420 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1421 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001422 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1423 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1424 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1425 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1426 N0.getOperand(1) == N1.getOperand(1)) {
1427 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1428 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001429 AddToWorkList(XORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001430 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1431 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001432 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001433}
1434
Nate Begeman83e75ec2005-09-06 04:43:02 +00001435SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001436 SDOperand N0 = N->getOperand(0);
1437 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001438 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1439 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440 MVT::ValueType VT = N0.getValueType();
1441 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1442
1443 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001444 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001445 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001446 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001447 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001448 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001449 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001450 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001451 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001452 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001453 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001454 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001456 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001457 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001458 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001459 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001460 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001461 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001462 N0.getOperand(1).getOpcode() == ISD::Constant) {
1463 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001464 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001465 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001466 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001467 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001468 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001469 }
1470 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1471 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001472 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001473 N0.getOperand(1).getOpcode() == ISD::Constant) {
1474 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001475 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001476 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1477 DAG.getConstant(~0ULL << c1, VT));
1478 if (c2 > c1)
1479 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001480 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001481 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001482 return DAG.getNode(ISD::SRL, VT, Mask,
1483 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001484 }
1485 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001486 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001487 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001488 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnercac70592006-03-05 19:53:55 +00001489 // fold (shl (add x, c1), c2) -> (add (shl x, c2), c1<<c2)
1490 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1491 isa<ConstantSDNode>(N0.getOperand(1))) {
1492 return DAG.getNode(ISD::ADD, VT,
1493 DAG.getNode(ISD::SHL, VT, N0.getOperand(0), N1),
1494 DAG.getNode(ISD::SHL, VT, N0.getOperand(1), N1));
1495 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001496 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001497}
1498
Nate Begeman83e75ec2005-09-06 04:43:02 +00001499SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001500 SDOperand N0 = N->getOperand(0);
1501 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001502 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1503 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001504 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001505
1506 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001507 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001508 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001509 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001510 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001511 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001512 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001513 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001514 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001515 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001516 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001517 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001518 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001519 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001520 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001521 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1522 // sext_inreg.
1523 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1524 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1525 MVT::ValueType EVT;
1526 switch (LowBits) {
1527 default: EVT = MVT::Other; break;
1528 case 1: EVT = MVT::i1; break;
1529 case 8: EVT = MVT::i8; break;
1530 case 16: EVT = MVT::i16; break;
1531 case 32: EVT = MVT::i32; break;
1532 }
1533 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1534 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1535 DAG.getValueType(EVT));
1536 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001537
1538 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1539 if (N1C && N0.getOpcode() == ISD::SRA) {
1540 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1541 unsigned Sum = N1C->getValue() + C1->getValue();
1542 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1543 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1544 DAG.getConstant(Sum, N1C->getValueType(0)));
1545 }
1546 }
1547
Nate Begeman1d4d4142005-09-01 00:19:25 +00001548 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001549 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001550 return DAG.getNode(ISD::SRL, VT, N0, N1);
1551 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001552}
1553
Nate Begeman83e75ec2005-09-06 04:43:02 +00001554SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001555 SDOperand N0 = N->getOperand(0);
1556 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001557 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1558 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001559 MVT::ValueType VT = N0.getValueType();
1560 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1561
1562 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001563 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001564 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001565 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001566 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001567 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001568 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001569 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001570 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001571 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001572 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001573 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001574 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001575 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001577 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001578 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001579 N0.getOperand(1).getOpcode() == ISD::Constant) {
1580 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001581 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001582 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001583 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001584 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001585 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001586 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001587 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001588}
1589
Nate Begeman83e75ec2005-09-06 04:43:02 +00001590SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001591 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001592 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001593 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001594
1595 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001596 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001597 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001598 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001599}
1600
Nate Begeman83e75ec2005-09-06 04:43:02 +00001601SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001602 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001603 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001604 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001605
1606 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001607 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001608 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001609 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001610}
1611
Nate Begeman83e75ec2005-09-06 04:43:02 +00001612SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001613 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001614 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001615 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616
1617 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001618 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001619 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001620 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621}
1622
Nate Begeman452d7be2005-09-16 00:54:12 +00001623SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1624 SDOperand N0 = N->getOperand(0);
1625 SDOperand N1 = N->getOperand(1);
1626 SDOperand N2 = N->getOperand(2);
1627 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1628 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1629 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1630 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001631
Nate Begeman452d7be2005-09-16 00:54:12 +00001632 // fold select C, X, X -> X
1633 if (N1 == N2)
1634 return N1;
1635 // fold select true, X, Y -> X
1636 if (N0C && !N0C->isNullValue())
1637 return N1;
1638 // fold select false, X, Y -> Y
1639 if (N0C && N0C->isNullValue())
1640 return N2;
1641 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001642 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001643 return DAG.getNode(ISD::OR, VT, N0, N2);
1644 // fold select C, 0, X -> ~C & X
1645 // FIXME: this should check for C type == X type, not i1?
1646 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1647 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001648 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001649 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1650 }
1651 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001652 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001653 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::OR, VT, XORNode, N1);
1656 }
1657 // fold select C, X, 0 -> C & X
1658 // FIXME: this should check for C type == X type, not i1?
1659 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1660 return DAG.getNode(ISD::AND, VT, N0, N1);
1661 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1662 if (MVT::i1 == VT && N0 == N1)
1663 return DAG.getNode(ISD::OR, VT, N0, N2);
1664 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1665 if (MVT::i1 == VT && N0 == N2)
1666 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001667 // If we can fold this based on the true/false value, do so.
1668 if (SimplifySelectOps(N, N1, N2))
1669 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001670 // fold selects based on a setcc into other things, such as min/max/abs
1671 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001672 // FIXME:
1673 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1674 // having to say they don't support SELECT_CC on every type the DAG knows
1675 // about, since there is no way to mark an opcode illegal at all value types
1676 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1677 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1678 N1, N2, N0.getOperand(2));
1679 else
1680 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001681 return SDOperand();
1682}
1683
1684SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001685 SDOperand N0 = N->getOperand(0);
1686 SDOperand N1 = N->getOperand(1);
1687 SDOperand N2 = N->getOperand(2);
1688 SDOperand N3 = N->getOperand(3);
1689 SDOperand N4 = N->getOperand(4);
1690 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1691 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1692 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1693 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1694
1695 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001696 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001697 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1698
Nate Begeman44728a72005-09-19 22:34:01 +00001699 // fold select_cc lhs, rhs, x, x, cc -> x
1700 if (N2 == N3)
1701 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001702
1703 // If we can fold this based on the true/false value, do so.
1704 if (SimplifySelectOps(N, N2, N3))
1705 return SDOperand();
1706
Nate Begeman44728a72005-09-19 22:34:01 +00001707 // fold select_cc into other things, such as min/max/abs
1708 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001709}
1710
1711SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1712 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1713 cast<CondCodeSDNode>(N->getOperand(2))->get());
1714}
1715
Nate Begeman83e75ec2005-09-06 04:43:02 +00001716SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001717 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001718 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001719 MVT::ValueType VT = N->getValueType(0);
1720
Nate Begeman1d4d4142005-09-01 00:19:25 +00001721 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001722 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001723 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001724 // fold (sext (sext x)) -> (sext x)
1725 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001726 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001727 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001728 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1729 (!AfterLegalize ||
1730 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001731 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1732 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001733 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001734 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1735 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001736 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1737 N0.getOperand(1), N0.getOperand(2),
1738 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001739 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001740 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1741 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001742 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001743 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001744
1745 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1746 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1747 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1748 N0.hasOneUse()) {
1749 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1750 N0.getOperand(1), N0.getOperand(2),
1751 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001752 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001753 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1754 ExtLoad.getValue(1));
1755 return SDOperand();
1756 }
1757
Nate Begeman83e75ec2005-09-06 04:43:02 +00001758 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001759}
1760
Nate Begeman83e75ec2005-09-06 04:43:02 +00001761SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001762 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001763 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001764 MVT::ValueType VT = N->getValueType(0);
1765
Nate Begeman1d4d4142005-09-01 00:19:25 +00001766 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001767 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001768 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001769 // fold (zext (zext x)) -> (zext x)
1770 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001771 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001772 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1773 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001774 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001775 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001776 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001777 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1778 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001779 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1780 N0.getOperand(1), N0.getOperand(2),
1781 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001782 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001783 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1784 ExtLoad.getValue(1));
1785 return SDOperand();
1786 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001787
1788 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1789 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1790 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1791 N0.hasOneUse()) {
1792 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1793 N0.getOperand(1), N0.getOperand(2),
1794 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001795 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001796 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1797 ExtLoad.getValue(1));
1798 return SDOperand();
1799 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001800 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001801}
1802
Nate Begeman83e75ec2005-09-06 04:43:02 +00001803SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001804 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001805 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001806 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001807 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001808 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001809 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001810
Nate Begeman1d4d4142005-09-01 00:19:25 +00001811 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001812 if (N0C) {
1813 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001814 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001815 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001816 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001817 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001818 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001819 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001820 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001821 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1822 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1823 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001824 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001825 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001826 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1827 if (N0.getOpcode() == ISD::AssertSext &&
1828 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001829 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001830 }
1831 // fold (sext_in_reg (sextload x)) -> (sextload x)
1832 if (N0.getOpcode() == ISD::SEXTLOAD &&
1833 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001834 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001835 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001836 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001837 if (N0.getOpcode() == ISD::SETCC &&
1838 TLI.getSetCCResultContents() ==
1839 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001840 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001841 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001842 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001843 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001844 // fold (sext_in_reg (srl x)) -> sra x
1845 if (N0.getOpcode() == ISD::SRL &&
1846 N0.getOperand(1).getOpcode() == ISD::Constant &&
1847 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1848 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1849 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001850 }
Nate Begemanded49632005-10-13 03:11:28 +00001851 // fold (sext_inreg (extload x)) -> (sextload x)
1852 if (N0.getOpcode() == ISD::EXTLOAD &&
1853 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001854 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001855 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1856 N0.getOperand(1), N0.getOperand(2),
1857 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001858 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001859 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001860 return SDOperand();
1861 }
1862 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001863 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001864 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001865 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001866 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1867 N0.getOperand(1), N0.getOperand(2),
1868 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001869 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001870 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001871 return SDOperand();
1872 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001873 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001874}
1875
Nate Begeman83e75ec2005-09-06 04:43:02 +00001876SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001877 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001878 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001879 MVT::ValueType VT = N->getValueType(0);
1880
1881 // noop truncate
1882 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001883 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001884 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001885 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001886 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001887 // fold (truncate (truncate x)) -> (truncate x)
1888 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001889 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001890 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1891 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1892 if (N0.getValueType() < VT)
1893 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001894 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001895 else if (N0.getValueType() > VT)
1896 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001897 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001898 else
1899 // if the source and dest are the same type, we can drop both the extend
1900 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001901 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001902 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001903 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001904 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001905 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1906 "Cannot truncate to larger type!");
1907 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001908 // For big endian targets, we need to add an offset to the pointer to load
1909 // the correct bytes. For little endian systems, we merely need to read
1910 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001911 uint64_t PtrOff =
1912 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001913 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1914 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1915 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001916 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001917 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001918 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001919 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001920 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001921 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001922 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001923}
1924
Chris Lattner94683772005-12-23 05:30:37 +00001925SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1926 SDOperand N0 = N->getOperand(0);
1927 MVT::ValueType VT = N->getValueType(0);
1928
1929 // If the input is a constant, let getNode() fold it.
1930 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1931 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1932 if (Res.Val != N) return Res;
1933 }
1934
Chris Lattnerc8547d82005-12-23 05:37:50 +00001935 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1936 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1937
Chris Lattner57104102005-12-23 05:44:41 +00001938 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001939 // FIXME: These xforms need to know that the resultant load doesn't need a
1940 // higher alignment than the original!
1941 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001942 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1943 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001944 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00001945 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1946 Load.getValue(1));
1947 return Load;
1948 }
1949
Chris Lattner94683772005-12-23 05:30:37 +00001950 return SDOperand();
1951}
1952
Chris Lattner01b3d732005-09-28 22:28:18 +00001953SDOperand DAGCombiner::visitFADD(SDNode *N) {
1954 SDOperand N0 = N->getOperand(0);
1955 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001956 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1957 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001958 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001959
1960 // fold (fadd c1, c2) -> c1+c2
1961 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001962 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001963 // canonicalize constant to RHS
1964 if (N0CFP && !N1CFP)
1965 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001966 // fold (A + (-B)) -> A-B
1967 if (N1.getOpcode() == ISD::FNEG)
1968 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001969 // fold ((-A) + B) -> B-A
1970 if (N0.getOpcode() == ISD::FNEG)
1971 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001972 return SDOperand();
1973}
1974
1975SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1976 SDOperand N0 = N->getOperand(0);
1977 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001978 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1979 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001980 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001981
1982 // fold (fsub c1, c2) -> c1-c2
1983 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001984 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001985 // fold (A-(-B)) -> A+B
1986 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001987 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001988 return SDOperand();
1989}
1990
1991SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1992 SDOperand N0 = N->getOperand(0);
1993 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001994 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1995 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001996 MVT::ValueType VT = N->getValueType(0);
1997
Nate Begeman11af4ea2005-10-17 20:40:11 +00001998 // fold (fmul c1, c2) -> c1*c2
1999 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002000 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002001 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002002 if (N0CFP && !N1CFP)
2003 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00002004 // fold (fmul X, 2.0) -> (fadd X, X)
2005 if (N1CFP && N1CFP->isExactlyValue(+2.0))
2006 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00002007 return SDOperand();
2008}
2009
2010SDOperand DAGCombiner::visitFDIV(SDNode *N) {
2011 SDOperand N0 = N->getOperand(0);
2012 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002013 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2014 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002015 MVT::ValueType VT = N->getValueType(0);
2016
Nate Begemana148d982006-01-18 22:35:16 +00002017 // fold (fdiv c1, c2) -> c1/c2
2018 if (N0CFP && N1CFP)
2019 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002020 return SDOperand();
2021}
2022
2023SDOperand DAGCombiner::visitFREM(SDNode *N) {
2024 SDOperand N0 = N->getOperand(0);
2025 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00002026 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2027 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002028 MVT::ValueType VT = N->getValueType(0);
2029
Nate Begemana148d982006-01-18 22:35:16 +00002030 // fold (frem c1, c2) -> fmod(c1,c2)
2031 if (N0CFP && N1CFP)
2032 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00002033 return SDOperand();
2034}
2035
Chris Lattner12d83032006-03-05 05:30:57 +00002036SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
2037 SDOperand N0 = N->getOperand(0);
2038 SDOperand N1 = N->getOperand(1);
2039 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2040 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2041 MVT::ValueType VT = N->getValueType(0);
2042
2043 if (N0CFP && N1CFP) // Constant fold
2044 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
2045
2046 if (N1CFP) {
2047 // copysign(x, c1) -> fabs(x) iff ispos(c1)
2048 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
2049 union {
2050 double d;
2051 int64_t i;
2052 } u;
2053 u.d = N1CFP->getValue();
2054 if (u.i >= 0)
2055 return DAG.getNode(ISD::FABS, VT, N0);
2056 else
2057 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
2058 }
2059
2060 // copysign(fabs(x), y) -> copysign(x, y)
2061 // copysign(fneg(x), y) -> copysign(x, y)
2062 // copysign(copysign(x,z), y) -> copysign(x, y)
2063 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
2064 N0.getOpcode() == ISD::FCOPYSIGN)
2065 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
2066
2067 // copysign(x, abs(y)) -> abs(x)
2068 if (N1.getOpcode() == ISD::FABS)
2069 return DAG.getNode(ISD::FABS, VT, N0);
2070
2071 // copysign(x, copysign(y,z)) -> copysign(x, z)
2072 if (N1.getOpcode() == ISD::FCOPYSIGN)
2073 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
2074
2075 // copysign(x, fp_extend(y)) -> copysign(x, y)
2076 // copysign(x, fp_round(y)) -> copysign(x, y)
2077 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
2078 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
2079
2080 return SDOperand();
2081}
2082
2083
Chris Lattner01b3d732005-09-28 22:28:18 +00002084
Nate Begeman83e75ec2005-09-06 04:43:02 +00002085SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002086 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002087 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002088 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002089
2090 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002091 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002092 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002093 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002094}
2095
Nate Begeman83e75ec2005-09-06 04:43:02 +00002096SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002097 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00002098 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00002099 MVT::ValueType VT = N->getValueType(0);
2100
Nate Begeman1d4d4142005-09-01 00:19:25 +00002101 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002102 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00002103 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002104 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002105}
2106
Nate Begeman83e75ec2005-09-06 04:43:02 +00002107SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002108 SDOperand N0 = N->getOperand(0);
2109 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2110 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002111
2112 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002113 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002114 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002115 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002116}
2117
Nate Begeman83e75ec2005-09-06 04:43:02 +00002118SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002119 SDOperand N0 = N->getOperand(0);
2120 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2121 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002122
2123 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002124 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002125 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002126 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002127}
2128
Nate Begeman83e75ec2005-09-06 04:43:02 +00002129SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002130 SDOperand N0 = N->getOperand(0);
2131 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2132 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002133
2134 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002135 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002136 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Chris Lattner79dbea52006-03-13 06:26:26 +00002137
2138 // fold (fp_round (fp_extend x)) -> x
2139 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
2140 return N0.getOperand(0);
2141
2142 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
2143 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
2144 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0));
2145 AddToWorkList(Tmp.Val);
2146 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
2147 }
2148
Nate Begeman83e75ec2005-09-06 04:43:02 +00002149 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002150}
2151
Nate Begeman83e75ec2005-09-06 04:43:02 +00002152SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002153 SDOperand N0 = N->getOperand(0);
2154 MVT::ValueType VT = N->getValueType(0);
2155 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00002156 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002157
Nate Begeman1d4d4142005-09-01 00:19:25 +00002158 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002159 if (N0CFP) {
2160 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002161 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002162 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002163 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002164}
2165
Nate Begeman83e75ec2005-09-06 04:43:02 +00002166SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002167 SDOperand N0 = N->getOperand(0);
2168 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2169 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002170
2171 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002172 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002173 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002174 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002175}
2176
Nate Begeman83e75ec2005-09-06 04:43:02 +00002177SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002178 SDOperand N0 = N->getOperand(0);
2179 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2180 MVT::ValueType VT = N->getValueType(0);
2181
2182 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002183 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002184 return DAG.getNode(ISD::FNEG, VT, N0);
2185 // fold (fneg (sub x, y)) -> (sub y, x)
Chris Lattner12d83032006-03-05 05:30:57 +00002186 if (N0.getOpcode() == ISD::SUB)
2187 return DAG.getNode(ISD::SUB, VT, N0.getOperand(1), N0.getOperand(0));
Nate Begemana148d982006-01-18 22:35:16 +00002188 // fold (fneg (fneg x)) -> x
Chris Lattner12d83032006-03-05 05:30:57 +00002189 if (N0.getOpcode() == ISD::FNEG)
2190 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002191 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002192}
2193
Nate Begeman83e75ec2005-09-06 04:43:02 +00002194SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002195 SDOperand N0 = N->getOperand(0);
2196 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2197 MVT::ValueType VT = N->getValueType(0);
2198
Nate Begeman1d4d4142005-09-01 00:19:25 +00002199 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002200 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002201 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002202 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002203 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002204 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002205 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00002206 // fold (fabs (fcopysign x, y)) -> (fabs x)
2207 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
2208 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
2209
Nate Begeman83e75ec2005-09-06 04:43:02 +00002210 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002211}
2212
Nate Begeman44728a72005-09-19 22:34:01 +00002213SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2214 SDOperand Chain = N->getOperand(0);
2215 SDOperand N1 = N->getOperand(1);
2216 SDOperand N2 = N->getOperand(2);
2217 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2218
2219 // never taken branch, fold to chain
2220 if (N1C && N1C->isNullValue())
2221 return Chain;
2222 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002223 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002224 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002225 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2226 // on the target.
2227 if (N1.getOpcode() == ISD::SETCC &&
2228 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2229 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2230 N1.getOperand(0), N1.getOperand(1), N2);
2231 }
Nate Begeman44728a72005-09-19 22:34:01 +00002232 return SDOperand();
2233}
2234
Chris Lattner3ea0b472005-10-05 06:47:48 +00002235// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2236//
Nate Begeman44728a72005-09-19 22:34:01 +00002237SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002238 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2239 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2240
2241 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002242 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2243 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2244
2245 // fold br_cc true, dest -> br dest (unconditional branch)
2246 if (SCCC && SCCC->getValue())
2247 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2248 N->getOperand(4));
2249 // fold br_cc false, dest -> unconditional fall through
2250 if (SCCC && SCCC->isNullValue())
2251 return N->getOperand(0);
2252 // fold to a simpler setcc
2253 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2254 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2255 Simp.getOperand(2), Simp.getOperand(0),
2256 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002257 return SDOperand();
2258}
2259
Chris Lattner01a22022005-10-10 22:04:48 +00002260SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2261 SDOperand Chain = N->getOperand(0);
2262 SDOperand Ptr = N->getOperand(1);
2263 SDOperand SrcValue = N->getOperand(2);
2264
2265 // If this load is directly stored, replace the load value with the stored
2266 // value.
2267 // TODO: Handle store large -> read small portion.
2268 // TODO: Handle TRUNCSTORE/EXTLOAD
2269 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2270 Chain.getOperand(1).getValueType() == N->getValueType(0))
2271 return CombineTo(N, Chain.getOperand(1), Chain);
2272
2273 return SDOperand();
2274}
2275
Chris Lattner87514ca2005-10-10 22:31:19 +00002276SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2277 SDOperand Chain = N->getOperand(0);
2278 SDOperand Value = N->getOperand(1);
2279 SDOperand Ptr = N->getOperand(2);
2280 SDOperand SrcValue = N->getOperand(3);
2281
2282 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002283 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002284 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2285 // Make sure that these stores are the same value type:
2286 // FIXME: we really care that the second store is >= size of the first.
2287 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002288 // Create a new store of Value that replaces both stores.
2289 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002290 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2291 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002292 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2293 PrevStore->getOperand(0), Value, Ptr,
2294 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002295 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002296 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002297 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002298 }
2299
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002300 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002301 // FIXME: This needs to know that the resultant store does not need a
2302 // higher alignment than the original.
2303 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002304 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2305 Ptr, SrcValue);
2306
Chris Lattner87514ca2005-10-10 22:31:19 +00002307 return SDOperand();
2308}
2309
Chris Lattnerca242442006-03-19 01:27:56 +00002310SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2311 SDOperand InVec = N->getOperand(0);
2312 SDOperand InVal = N->getOperand(1);
2313 SDOperand EltNo = N->getOperand(2);
2314
2315 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
2316 // vector with the inserted element.
2317 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2318 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2319 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2320 if (Elt < Ops.size())
2321 Ops[Elt] = InVal;
2322 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(), Ops);
2323 }
2324
2325 return SDOperand();
2326}
2327
2328SDOperand DAGCombiner::visitVINSERT_VECTOR_ELT(SDNode *N) {
2329 SDOperand InVec = N->getOperand(0);
2330 SDOperand InVal = N->getOperand(1);
2331 SDOperand EltNo = N->getOperand(2);
2332 SDOperand NumElts = N->getOperand(3);
2333 SDOperand EltType = N->getOperand(4);
2334
2335 // If the invec is a VBUILD_VECTOR and if EltNo is a constant, build a new
2336 // vector with the inserted element.
2337 if (InVec.getOpcode() == ISD::VBUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
2338 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
2339 std::vector<SDOperand> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
2340 if (Elt < Ops.size()-2)
2341 Ops[Elt] = InVal;
2342 return DAG.getNode(ISD::VBUILD_VECTOR, InVec.getValueType(), Ops);
2343 }
2344
2345 return SDOperand();
2346}
2347
Chris Lattnerd7648c82006-03-28 20:28:38 +00002348SDOperand DAGCombiner::visitVBUILD_VECTOR(SDNode *N) {
2349 unsigned NumInScalars = N->getNumOperands()-2;
2350 SDOperand NumElts = N->getOperand(NumInScalars);
2351 SDOperand EltType = N->getOperand(NumInScalars+1);
2352
2353 // Check to see if this is a VBUILD_VECTOR of a bunch of VEXTRACT_VECTOR_ELT
2354 // operations. If so, and if the EXTRACT_ELT vector inputs come from at most
2355 // two distinct vectors, turn this into a shuffle node.
2356 SDOperand VecIn1, VecIn2;
2357 for (unsigned i = 0; i != NumInScalars; ++i) {
2358 // Ignore undef inputs.
2359 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
2360
2361 // If this input is something other than a VEXTRACT_VECTOR_ELT with a
2362 // constant index, bail out.
2363 if (N->getOperand(i).getOpcode() != ISD::VEXTRACT_VECTOR_ELT ||
2364 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
2365 VecIn1 = VecIn2 = SDOperand(0, 0);
2366 break;
2367 }
2368
2369 // If the input vector type disagrees with the result of the vbuild_vector,
2370 // we can't make a shuffle.
2371 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
2372 if (*(ExtractedFromVec.Val->op_end()-2) != NumElts ||
2373 *(ExtractedFromVec.Val->op_end()-1) != EltType) {
2374 VecIn1 = VecIn2 = SDOperand(0, 0);
2375 break;
2376 }
2377
2378 // Otherwise, remember this. We allow up to two distinct input vectors.
2379 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
2380 continue;
2381
2382 if (VecIn1.Val == 0) {
2383 VecIn1 = ExtractedFromVec;
2384 } else if (VecIn2.Val == 0) {
2385 VecIn2 = ExtractedFromVec;
2386 } else {
2387 // Too many inputs.
2388 VecIn1 = VecIn2 = SDOperand(0, 0);
2389 break;
2390 }
2391 }
2392
2393 // If everything is good, we can make a shuffle operation.
2394 if (VecIn1.Val) {
2395 std::vector<SDOperand> BuildVecIndices;
2396 for (unsigned i = 0; i != NumInScalars; ++i) {
2397 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
2398 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
2399 continue;
2400 }
2401
2402 SDOperand Extract = N->getOperand(i);
2403
2404 // If extracting from the first vector, just use the index directly.
2405 if (Extract.getOperand(0) == VecIn1) {
2406 BuildVecIndices.push_back(Extract.getOperand(1));
2407 continue;
2408 }
2409
2410 // Otherwise, use InIdx + VecSize
2411 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
2412 BuildVecIndices.push_back(DAG.getConstant(Idx+NumInScalars, MVT::i32));
2413 }
2414
2415 // Add count and size info.
2416 BuildVecIndices.push_back(NumElts);
2417 BuildVecIndices.push_back(DAG.getValueType(MVT::i32));
2418
2419 // Return the new VVECTOR_SHUFFLE node.
2420 std::vector<SDOperand> Ops;
2421 Ops.push_back(VecIn1);
2422 Ops.push_back(VecIn2.Val ? VecIn2 : VecIn1); // Use V1 twice if no V2.
2423 Ops.push_back(DAG.getNode(ISD::VBUILD_VECTOR,MVT::Vector, BuildVecIndices));
2424 Ops.push_back(NumElts);
2425 Ops.push_back(EltType);
2426 return DAG.getNode(ISD::VVECTOR_SHUFFLE, MVT::Vector, Ops);
2427 }
2428
2429 return SDOperand();
2430}
2431
Chris Lattner66445d32006-03-28 22:11:53 +00002432SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
2433 // If the LHS and the RHS are the same node, turn the RHS into an undef.
2434 if (N->getOperand(0) == N->getOperand(1)) {
2435 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
2436 // first operand.
2437 std::vector<SDOperand> MappedOps;
2438 SDOperand ShufMask = N->getOperand(2);
2439 unsigned NumElts = ShufMask.getNumOperands();
2440 for (unsigned i = 0, e = ShufMask.getNumOperands(); i != e; ++i) {
2441 if (cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() >= NumElts) {
2442 unsigned NewIdx =
2443 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
2444 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
2445 } else {
2446 MappedOps.push_back(ShufMask.getOperand(i));
2447 }
2448 }
2449 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
2450 MappedOps);
2451 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
2452 N->getOperand(0),
2453 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
2454 ShufMask);
2455 }
2456
2457 return SDOperand();
2458}
2459
Nate Begeman44728a72005-09-19 22:34:01 +00002460SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002461 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2462
2463 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2464 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2465 // If we got a simplified select_cc node back from SimplifySelectCC, then
2466 // break it down into a new SETCC node, and a new SELECT node, and then return
2467 // the SELECT node, since we were called with a SELECT node.
2468 if (SCC.Val) {
2469 // Check to see if we got a select_cc back (to turn into setcc/select).
2470 // Otherwise, just return whatever node we got back, like fabs.
2471 if (SCC.getOpcode() == ISD::SELECT_CC) {
2472 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2473 SCC.getOperand(0), SCC.getOperand(1),
2474 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002475 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002476 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2477 SCC.getOperand(3), SETCC);
2478 }
2479 return SCC;
2480 }
Nate Begeman44728a72005-09-19 22:34:01 +00002481 return SDOperand();
2482}
2483
Chris Lattner40c62d52005-10-18 06:04:22 +00002484/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2485/// are the two values being selected between, see if we can simplify the
2486/// select.
2487///
2488bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2489 SDOperand RHS) {
2490
2491 // If this is a select from two identical things, try to pull the operation
2492 // through the select.
2493 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2494#if 0
2495 std::cerr << "SELECT: ["; LHS.Val->dump();
2496 std::cerr << "] ["; RHS.Val->dump();
2497 std::cerr << "]\n";
2498#endif
2499
2500 // If this is a load and the token chain is identical, replace the select
2501 // of two loads with a load through a select of the address to load from.
2502 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2503 // constants have been dropped into the constant pool.
2504 if ((LHS.getOpcode() == ISD::LOAD ||
2505 LHS.getOpcode() == ISD::EXTLOAD ||
2506 LHS.getOpcode() == ISD::ZEXTLOAD ||
2507 LHS.getOpcode() == ISD::SEXTLOAD) &&
2508 // Token chains must be identical.
2509 LHS.getOperand(0) == RHS.getOperand(0) &&
2510 // If this is an EXTLOAD, the VT's must match.
2511 (LHS.getOpcode() == ISD::LOAD ||
2512 LHS.getOperand(3) == RHS.getOperand(3))) {
2513 // FIXME: this conflates two src values, discarding one. This is not
2514 // the right thing to do, but nothing uses srcvalues now. When they do,
2515 // turn SrcValue into a list of locations.
2516 SDOperand Addr;
2517 if (TheSelect->getOpcode() == ISD::SELECT)
2518 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2519 TheSelect->getOperand(0), LHS.getOperand(1),
2520 RHS.getOperand(1));
2521 else
2522 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2523 TheSelect->getOperand(0),
2524 TheSelect->getOperand(1),
2525 LHS.getOperand(1), RHS.getOperand(1),
2526 TheSelect->getOperand(4));
2527
2528 SDOperand Load;
2529 if (LHS.getOpcode() == ISD::LOAD)
2530 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2531 Addr, LHS.getOperand(2));
2532 else
2533 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2534 LHS.getOperand(0), Addr, LHS.getOperand(2),
2535 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2536 // Users of the select now use the result of the load.
2537 CombineTo(TheSelect, Load);
2538
2539 // Users of the old loads now use the new load's chain. We know the
2540 // old-load value is dead now.
2541 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2542 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2543 return true;
2544 }
2545 }
2546
2547 return false;
2548}
2549
Nate Begeman44728a72005-09-19 22:34:01 +00002550SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2551 SDOperand N2, SDOperand N3,
2552 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002553
2554 MVT::ValueType VT = N2.getValueType();
2555 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2556 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2557 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2558 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2559
2560 // Determine if the condition we're dealing with is constant
2561 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2562 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2563
2564 // fold select_cc true, x, y -> x
2565 if (SCCC && SCCC->getValue())
2566 return N2;
2567 // fold select_cc false, x, y -> y
2568 if (SCCC && SCCC->getValue() == 0)
2569 return N3;
2570
2571 // Check to see if we can simplify the select into an fabs node
2572 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2573 // Allow either -0.0 or 0.0
2574 if (CFP->getValue() == 0.0) {
2575 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2576 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2577 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2578 N2 == N3.getOperand(0))
2579 return DAG.getNode(ISD::FABS, VT, N0);
2580
2581 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2582 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2583 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2584 N2.getOperand(0) == N3)
2585 return DAG.getNode(ISD::FABS, VT, N3);
2586 }
2587 }
2588
2589 // Check to see if we can perform the "gzip trick", transforming
2590 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2591 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2592 MVT::isInteger(N0.getValueType()) &&
2593 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2594 MVT::ValueType XType = N0.getValueType();
2595 MVT::ValueType AType = N2.getValueType();
2596 if (XType >= AType) {
2597 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002598 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002599 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2600 unsigned ShCtV = Log2_64(N2C->getValue());
2601 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2602 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2603 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00002604 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002605 if (XType > AType) {
2606 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002607 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002608 }
2609 return DAG.getNode(ISD::AND, AType, Shift, N2);
2610 }
2611 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2612 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2613 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002614 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002615 if (XType > AType) {
2616 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002617 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002618 }
2619 return DAG.getNode(ISD::AND, AType, Shift, N2);
2620 }
2621 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002622
2623 // fold select C, 16, 0 -> shl C, 4
2624 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2625 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2626 // Get a SetCC of the condition
2627 // FIXME: Should probably make sure that setcc is legal if we ever have a
2628 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00002629 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00002630 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00002631 if (AfterLegalize) {
2632 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002633 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00002634 } else {
2635 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002636 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00002637 }
Chris Lattner5750df92006-03-01 04:03:14 +00002638 AddToWorkList(SCC.Val);
2639 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00002640 // shl setcc result by log2 n2c
2641 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2642 DAG.getConstant(Log2_64(N2C->getValue()),
2643 TLI.getShiftAmountTy()));
2644 }
2645
Nate Begemanf845b452005-10-08 00:29:44 +00002646 // Check to see if this is the equivalent of setcc
2647 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2648 // otherwise, go ahead with the folds.
2649 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2650 MVT::ValueType XType = N0.getValueType();
2651 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2652 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2653 if (Res.getValueType() != VT)
2654 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2655 return Res;
2656 }
2657
2658 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2659 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2660 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2661 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2662 return DAG.getNode(ISD::SRL, XType, Ctlz,
2663 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2664 TLI.getShiftAmountTy()));
2665 }
2666 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2667 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2668 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2669 N0);
2670 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2671 DAG.getConstant(~0ULL, XType));
2672 return DAG.getNode(ISD::SRL, XType,
2673 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2674 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2675 TLI.getShiftAmountTy()));
2676 }
2677 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2678 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2679 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2680 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2681 TLI.getShiftAmountTy()));
2682 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2683 }
2684 }
2685
2686 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2687 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2688 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2689 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2690 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2691 MVT::ValueType XType = N0.getValueType();
2692 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2693 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2694 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2695 TLI.getShiftAmountTy()));
2696 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002697 AddToWorkList(Shift.Val);
2698 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002699 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2700 }
2701 }
2702 }
2703
Nate Begeman44728a72005-09-19 22:34:01 +00002704 return SDOperand();
2705}
2706
Nate Begeman452d7be2005-09-16 00:54:12 +00002707SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002708 SDOperand N1, ISD::CondCode Cond,
2709 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002710 // These setcc operations always fold.
2711 switch (Cond) {
2712 default: break;
2713 case ISD::SETFALSE:
2714 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2715 case ISD::SETTRUE:
2716 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2717 }
2718
2719 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2720 uint64_t C1 = N1C->getValue();
2721 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2722 uint64_t C0 = N0C->getValue();
2723
2724 // Sign extend the operands if required
2725 if (ISD::isSignedIntSetCC(Cond)) {
2726 C0 = N0C->getSignExtended();
2727 C1 = N1C->getSignExtended();
2728 }
2729
2730 switch (Cond) {
2731 default: assert(0 && "Unknown integer setcc!");
2732 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2733 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2734 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2735 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2736 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2737 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2738 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2739 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2740 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2741 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2742 }
2743 } else {
2744 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2745 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2746 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2747
2748 // If the comparison constant has bits in the upper part, the
2749 // zero-extended value could never match.
2750 if (C1 & (~0ULL << InSize)) {
2751 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2752 switch (Cond) {
2753 case ISD::SETUGT:
2754 case ISD::SETUGE:
2755 case ISD::SETEQ: return DAG.getConstant(0, VT);
2756 case ISD::SETULT:
2757 case ISD::SETULE:
2758 case ISD::SETNE: return DAG.getConstant(1, VT);
2759 case ISD::SETGT:
2760 case ISD::SETGE:
2761 // True if the sign bit of C1 is set.
2762 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2763 case ISD::SETLT:
2764 case ISD::SETLE:
2765 // True if the sign bit of C1 isn't set.
2766 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2767 default:
2768 break;
2769 }
2770 }
2771
2772 // Otherwise, we can perform the comparison with the low bits.
2773 switch (Cond) {
2774 case ISD::SETEQ:
2775 case ISD::SETNE:
2776 case ISD::SETUGT:
2777 case ISD::SETUGE:
2778 case ISD::SETULT:
2779 case ISD::SETULE:
2780 return DAG.getSetCC(VT, N0.getOperand(0),
2781 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2782 Cond);
2783 default:
2784 break; // todo, be more careful with signed comparisons
2785 }
2786 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2787 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2788 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2789 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2790 MVT::ValueType ExtDstTy = N0.getValueType();
2791 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2792
2793 // If the extended part has any inconsistent bits, it cannot ever
2794 // compare equal. In other words, they have to be all ones or all
2795 // zeros.
2796 uint64_t ExtBits =
2797 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2798 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2799 return DAG.getConstant(Cond == ISD::SETNE, VT);
2800
2801 SDOperand ZextOp;
2802 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2803 if (Op0Ty == ExtSrcTy) {
2804 ZextOp = N0.getOperand(0);
2805 } else {
2806 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2807 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2808 DAG.getConstant(Imm, Op0Ty));
2809 }
Chris Lattner5750df92006-03-01 04:03:14 +00002810 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002811 // Otherwise, make this a use of a zext.
2812 return DAG.getSetCC(VT, ZextOp,
2813 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2814 ExtDstTy),
2815 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002816 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2817 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2818 (N0.getOpcode() == ISD::XOR ||
2819 (N0.getOpcode() == ISD::AND &&
2820 N0.getOperand(0).getOpcode() == ISD::XOR &&
2821 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2822 isa<ConstantSDNode>(N0.getOperand(1)) &&
2823 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2824 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2825 // only do this if the top bits are known zero.
2826 if (TLI.MaskedValueIsZero(N1,
2827 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2828 // Okay, get the un-inverted input value.
2829 SDOperand Val;
2830 if (N0.getOpcode() == ISD::XOR)
2831 Val = N0.getOperand(0);
2832 else {
2833 assert(N0.getOpcode() == ISD::AND &&
2834 N0.getOperand(0).getOpcode() == ISD::XOR);
2835 // ((X^1)&1)^1 -> X & 1
2836 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2837 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2838 }
2839 return DAG.getSetCC(VT, Val, N1,
2840 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2841 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002842 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002843
Nate Begeman452d7be2005-09-16 00:54:12 +00002844 uint64_t MinVal, MaxVal;
2845 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2846 if (ISD::isSignedIntSetCC(Cond)) {
2847 MinVal = 1ULL << (OperandBitSize-1);
2848 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2849 MaxVal = ~0ULL >> (65-OperandBitSize);
2850 else
2851 MaxVal = 0;
2852 } else {
2853 MinVal = 0;
2854 MaxVal = ~0ULL >> (64-OperandBitSize);
2855 }
2856
2857 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2858 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2859 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2860 --C1; // X >= C0 --> X > (C0-1)
2861 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2862 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2863 }
2864
2865 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2866 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2867 ++C1; // X <= C0 --> X < (C0+1)
2868 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2869 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2870 }
2871
2872 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2873 return DAG.getConstant(0, VT); // X < MIN --> false
2874
2875 // Canonicalize setgt X, Min --> setne X, Min
2876 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2877 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002878 // Canonicalize setlt X, Max --> setne X, Max
2879 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2880 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002881
2882 // If we have setult X, 1, turn it into seteq X, 0
2883 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2884 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2885 ISD::SETEQ);
2886 // If we have setugt X, Max-1, turn it into seteq X, Max
2887 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2888 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2889 ISD::SETEQ);
2890
2891 // If we have "setcc X, C0", check to see if we can shrink the immediate
2892 // by changing cc.
2893
2894 // SETUGT X, SINTMAX -> SETLT X, 0
2895 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2896 C1 == (~0ULL >> (65-OperandBitSize)))
2897 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2898 ISD::SETLT);
2899
2900 // FIXME: Implement the rest of these.
2901
2902 // Fold bit comparisons when we can.
2903 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2904 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2905 if (ConstantSDNode *AndRHS =
2906 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2907 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2908 // Perform the xform if the AND RHS is a single bit.
2909 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2910 return DAG.getNode(ISD::SRL, VT, N0,
2911 DAG.getConstant(Log2_64(AndRHS->getValue()),
2912 TLI.getShiftAmountTy()));
2913 }
2914 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2915 // (X & 8) == 8 --> (X & 8) >> 3
2916 // Perform the xform if C1 is a single bit.
2917 if ((C1 & (C1-1)) == 0) {
2918 return DAG.getNode(ISD::SRL, VT, N0,
2919 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2920 }
2921 }
2922 }
2923 }
2924 } else if (isa<ConstantSDNode>(N0.Val)) {
2925 // Ensure that the constant occurs on the RHS.
2926 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2927 }
2928
2929 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2930 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2931 double C0 = N0C->getValue(), C1 = N1C->getValue();
2932
2933 switch (Cond) {
2934 default: break; // FIXME: Implement the rest of these!
2935 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2936 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2937 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2938 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2939 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2940 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2941 }
2942 } else {
2943 // Ensure that the constant occurs on the RHS.
2944 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2945 }
2946
2947 if (N0 == N1) {
2948 // We can always fold X == Y for integer setcc's.
2949 if (MVT::isInteger(N0.getValueType()))
2950 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2951 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2952 if (UOF == 2) // FP operators that are undefined on NaNs.
2953 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2954 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2955 return DAG.getConstant(UOF, VT);
2956 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2957 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002958 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002959 if (NewCond != Cond)
2960 return DAG.getSetCC(VT, N0, N1, NewCond);
2961 }
2962
2963 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2964 MVT::isInteger(N0.getValueType())) {
2965 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2966 N0.getOpcode() == ISD::XOR) {
2967 // Simplify (X+Y) == (X+Z) --> Y == Z
2968 if (N0.getOpcode() == N1.getOpcode()) {
2969 if (N0.getOperand(0) == N1.getOperand(0))
2970 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2971 if (N0.getOperand(1) == N1.getOperand(1))
2972 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2973 if (isCommutativeBinOp(N0.getOpcode())) {
2974 // If X op Y == Y op X, try other combinations.
2975 if (N0.getOperand(0) == N1.getOperand(1))
2976 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2977 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002978 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002979 }
2980 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002981
2982 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2983 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2984 // Turn (X+C1) == C2 --> X == C2-C1
2985 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2986 return DAG.getSetCC(VT, N0.getOperand(0),
2987 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2988 N0.getValueType()), Cond);
2989 }
2990
2991 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2992 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002993 // If we know that all of the inverted bits are zero, don't bother
2994 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002995 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002996 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002997 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002998 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002999 }
3000
3001 // Turn (C1-X) == C2 --> X == C1-C2
3002 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
3003 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
3004 return DAG.getSetCC(VT, N0.getOperand(1),
3005 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
3006 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00003007 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00003008 }
3009 }
3010
Nate Begeman452d7be2005-09-16 00:54:12 +00003011 // Simplify (X+Z) == X --> Z == 0
3012 if (N0.getOperand(0) == N1)
3013 return DAG.getSetCC(VT, N0.getOperand(1),
3014 DAG.getConstant(0, N0.getValueType()), Cond);
3015 if (N0.getOperand(1) == N1) {
3016 if (isCommutativeBinOp(N0.getOpcode()))
3017 return DAG.getSetCC(VT, N0.getOperand(0),
3018 DAG.getConstant(0, N0.getValueType()), Cond);
3019 else {
3020 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
3021 // (Z-X) == X --> Z == X<<1
3022 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
3023 N1,
3024 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003025 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003026 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
3027 }
3028 }
3029 }
3030
3031 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
3032 N1.getOpcode() == ISD::XOR) {
3033 // Simplify X == (X+Z) --> Z == 0
3034 if (N1.getOperand(0) == N0) {
3035 return DAG.getSetCC(VT, N1.getOperand(1),
3036 DAG.getConstant(0, N1.getValueType()), Cond);
3037 } else if (N1.getOperand(1) == N0) {
3038 if (isCommutativeBinOp(N1.getOpcode())) {
3039 return DAG.getSetCC(VT, N1.getOperand(0),
3040 DAG.getConstant(0, N1.getValueType()), Cond);
3041 } else {
3042 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
3043 // X == (Z-X) --> X<<1 == Z
3044 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
3045 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003046 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003047 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
3048 }
3049 }
3050 }
3051 }
3052
3053 // Fold away ALL boolean setcc's.
3054 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00003055 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00003056 switch (Cond) {
3057 default: assert(0 && "Unknown integer setcc!");
3058 case ISD::SETEQ: // X == Y -> (X^Y)^1
3059 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3060 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00003061 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003062 break;
3063 case ISD::SETNE: // X != Y --> (X^Y)
3064 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
3065 break;
3066 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
3067 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
3068 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3069 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003070 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003071 break;
3072 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
3073 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
3074 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3075 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003076 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003077 break;
3078 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
3079 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
3080 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
3081 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00003082 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003083 break;
3084 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
3085 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
3086 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
3087 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
3088 break;
3089 }
3090 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00003091 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00003092 // FIXME: If running after legalize, we probably can't do this.
3093 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3094 }
3095 return N0;
3096 }
3097
3098 // Could not fold it.
3099 return SDOperand();
3100}
3101
Nate Begeman69575232005-10-20 02:15:44 +00003102/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
3103/// return a DAG expression to select that will generate the same value by
3104/// multiplying by a magic number. See:
3105/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3106SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
3107 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003108
3109 // Check to see if we can do this.
3110 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3111 return SDOperand(); // BuildSDIV only operates on i32 or i64
3112 if (!TLI.isOperationLegal(ISD::MULHS, VT))
3113 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00003114
Nate Begemanc6a454e2005-10-20 17:45:03 +00003115 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00003116 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
3117
3118 // Multiply the numerator (operand 0) by the magic value
3119 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
3120 DAG.getConstant(magics.m, VT));
3121 // If d > 0 and m < 0, add the numerator
3122 if (d > 0 && magics.m < 0) {
3123 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003124 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003125 }
3126 // If d < 0 and m > 0, subtract the numerator.
3127 if (d < 0 && magics.m > 0) {
3128 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00003129 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003130 }
3131 // Shift right algebraic if shift value is nonzero
3132 if (magics.s > 0) {
3133 Q = DAG.getNode(ISD::SRA, VT, Q,
3134 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003135 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003136 }
3137 // Extract the sign bit and add it to the quotient
3138 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00003139 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
3140 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003141 AddToWorkList(T.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003142 return DAG.getNode(ISD::ADD, VT, Q, T);
3143}
3144
3145/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
3146/// return a DAG expression to select that will generate the same value by
3147/// multiplying by a magic number. See:
3148/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
3149SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
3150 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00003151
3152 // Check to see if we can do this.
3153 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
3154 return SDOperand(); // BuildUDIV only operates on i32 or i64
3155 if (!TLI.isOperationLegal(ISD::MULHU, VT))
3156 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00003157
3158 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
3159 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
3160
3161 // Multiply the numerator (operand 0) by the magic value
3162 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
3163 DAG.getConstant(magics.m, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00003164 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003165
3166 if (magics.a == 0) {
3167 return DAG.getNode(ISD::SRL, VT, Q,
3168 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
3169 } else {
3170 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003171 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003172 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
3173 DAG.getConstant(1, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00003174 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003175 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
Chris Lattner5750df92006-03-01 04:03:14 +00003176 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00003177 return DAG.getNode(ISD::SRL, VT, NPQ,
3178 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
3179 }
3180}
3181
Nate Begeman1d4d4142005-09-01 00:19:25 +00003182// SelectionDAG::Combine - This is the entry point for the file.
3183//
Nate Begeman4ebd8052005-09-01 23:24:04 +00003184void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003185 /// run - This is the main entry point to this class.
3186 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00003187 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003188}