blob: ad09869d27b943c5a7271a376437ee81d52a9c3a [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//
19// FIXME: Should add a corresponding version of fold AND with
20// ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
21// we don't have yet.
22//
Nate Begeman44728a72005-09-19 22:34:01 +000023// FIXME: select C, pow2, pow2 -> something smart
24// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000025// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000026// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000027// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000028// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000029// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman4ebd8052005-09-01 23:24:04 +000030// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000031// FIXME: verify that getNode can't return extends with an operand whose type
32// is >= to that of the extend.
33// FIXME: divide by zero is currently left unfolded. do we want to turn this
34// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000035// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000036//
37//===----------------------------------------------------------------------===//
38
39#define DEBUG_TYPE "dagcombine"
40#include "llvm/ADT/Statistic.h"
41#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000042#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000043#include "llvm/Support/MathExtras.h"
44#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000045#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000046#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000047#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000048using namespace llvm;
49
50namespace {
51 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
52
53 class DAGCombiner {
54 SelectionDAG &DAG;
55 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000056 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
59 std::vector<SDNode*> WorkList;
60
61 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
63 /// now.
64 ///
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 UI != UE; ++UI)
68 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000069 }
70
71 /// removeFromWorkList - remove all instances of N from the worklist.
72 void removeFromWorkList(SDNode *N) {
73 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
74 WorkList.end());
75 }
76
Chris Lattner01a22022005-10-10 22:04:48 +000077 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000078 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000079 DEBUG(std::cerr << "\nReplacing "; N->dump();
80 std::cerr << "\nWith: "; To[0].Val->dump();
81 std::cerr << " and " << To.size()-1 << " other values\n");
82 std::vector<SDNode*> NowDead;
83 DAG.ReplaceAllUsesWith(N, To, &NowDead);
84
85 // Push the new nodes and any users onto the worklist
86 for (unsigned i = 0, e = To.size(); i != e; ++i) {
87 WorkList.push_back(To[i].Val);
88 AddUsersToWorkList(To[i].Val);
89 }
90
91 // Nodes can end up on the worklist more than once. Make sure we do
92 // not process a node that has been replaced.
93 removeFromWorkList(N);
94 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
95 removeFromWorkList(NowDead[i]);
96
97 // Finally, since the node is now dead, remove it from the graph.
98 DAG.DeleteNode(N);
99 return SDOperand(N, 0);
100 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000101
Chris Lattner012f2412006-02-17 21:58:01 +0000102 /// SimplifyDemandedBits - Check the specified integer node value to see if
103 /// it can be simplified or if things is uses can be simplified by bit
104 /// propagation. If so, return true.
105 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000106 TargetLowering::TargetLoweringOpt TLO(DAG);
107 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000108 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
109 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
110 return false;
111
112 // Revisit the node.
113 WorkList.push_back(Op.Val);
114
115 // Replace the old value with the new one.
116 ++NodesCombined;
117 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
118 std::cerr << "\nWith: "; TLO.New.Val->dump());
119
120 std::vector<SDNode*> NowDead;
121 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
122
Chris Lattner7d20d392006-02-20 06:51:04 +0000123 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000124 WorkList.push_back(TLO.New.Val);
125 AddUsersToWorkList(TLO.New.Val);
126
127 // Nodes can end up on the worklist more than once. Make sure we do
128 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000129 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
130 removeFromWorkList(NowDead[i]);
131
Chris Lattner7d20d392006-02-20 06:51:04 +0000132 // Finally, if the node is now dead, remove it from the graph. The node
133 // may not be dead if the replacement process recursively simplified to
134 // something else needing this node.
135 if (TLO.Old.Val->use_empty()) {
136 removeFromWorkList(TLO.Old.Val);
137 DAG.DeleteNode(TLO.Old.Val);
138 }
Chris Lattner012f2412006-02-17 21:58:01 +0000139 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000140 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000141
142 SDOperand CombineTo(SDNode *N, SDOperand Res) {
143 std::vector<SDOperand> To;
144 To.push_back(Res);
145 return CombineTo(N, To);
146 }
Chris Lattner01a22022005-10-10 22:04:48 +0000147
148 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
149 std::vector<SDOperand> To;
150 To.push_back(Res0);
151 To.push_back(Res1);
152 return CombineTo(N, To);
153 }
154
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);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000198 SDOperand visitSINT_TO_FP(SDNode *N);
199 SDOperand visitUINT_TO_FP(SDNode *N);
200 SDOperand visitFP_TO_SINT(SDNode *N);
201 SDOperand visitFP_TO_UINT(SDNode *N);
202 SDOperand visitFP_ROUND(SDNode *N);
203 SDOperand visitFP_ROUND_INREG(SDNode *N);
204 SDOperand visitFP_EXTEND(SDNode *N);
205 SDOperand visitFNEG(SDNode *N);
206 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000207 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000208 SDOperand visitBRCONDTWOWAY(SDNode *N);
209 SDOperand visitBR_CC(SDNode *N);
210 SDOperand visitBRTWOWAY_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000211 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000212 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000213
Nate Begemancd4d58c2006-02-03 06:46:56 +0000214 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
215
Chris Lattner40c62d52005-10-18 06:04:22 +0000216 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000217 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
218 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
219 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000220 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000221 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000222
223 SDOperand BuildSDIV(SDNode *N);
224 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000225public:
226 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000227 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000228
229 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000230 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000231 };
232}
233
Nate Begeman69575232005-10-20 02:15:44 +0000234struct ms {
235 int64_t m; // magic number
236 int64_t s; // shift amount
237};
238
239struct mu {
240 uint64_t m; // magic number
241 int64_t a; // add indicator
242 int64_t s; // shift amount
243};
244
245/// magic - calculate the magic numbers required to codegen an integer sdiv as
246/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
247/// or -1.
248static ms magic32(int32_t d) {
249 int32_t p;
250 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
251 const uint32_t two31 = 0x80000000U;
252 struct ms mag;
253
254 ad = abs(d);
255 t = two31 + ((uint32_t)d >> 31);
256 anc = t - 1 - t%ad; // absolute value of nc
257 p = 31; // initialize p
258 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
259 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
260 q2 = two31/ad; // initialize q2 = 2p/abs(d)
261 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
262 do {
263 p = p + 1;
264 q1 = 2*q1; // update q1 = 2p/abs(nc)
265 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
266 if (r1 >= anc) { // must be unsigned comparison
267 q1 = q1 + 1;
268 r1 = r1 - anc;
269 }
270 q2 = 2*q2; // update q2 = 2p/abs(d)
271 r2 = 2*r2; // update r2 = rem(2p/abs(d))
272 if (r2 >= ad) { // must be unsigned comparison
273 q2 = q2 + 1;
274 r2 = r2 - ad;
275 }
276 delta = ad - r2;
277 } while (q1 < delta || (q1 == delta && r1 == 0));
278
279 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
280 if (d < 0) mag.m = -mag.m; // resulting magic number
281 mag.s = p - 32; // resulting shift
282 return mag;
283}
284
285/// magicu - calculate the magic numbers required to codegen an integer udiv as
286/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
287static mu magicu32(uint32_t d) {
288 int32_t p;
289 uint32_t nc, delta, q1, r1, q2, r2;
290 struct mu magu;
291 magu.a = 0; // initialize "add" indicator
292 nc = - 1 - (-d)%d;
293 p = 31; // initialize p
294 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
295 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
296 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
297 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
298 do {
299 p = p + 1;
300 if (r1 >= nc - r1 ) {
301 q1 = 2*q1 + 1; // update q1
302 r1 = 2*r1 - nc; // update r1
303 }
304 else {
305 q1 = 2*q1; // update q1
306 r1 = 2*r1; // update r1
307 }
308 if (r2 + 1 >= d - r2) {
309 if (q2 >= 0x7FFFFFFF) magu.a = 1;
310 q2 = 2*q2 + 1; // update q2
311 r2 = 2*r2 + 1 - d; // update r2
312 }
313 else {
314 if (q2 >= 0x80000000) magu.a = 1;
315 q2 = 2*q2; // update q2
316 r2 = 2*r2 + 1; // update r2
317 }
318 delta = d - 1 - r2;
319 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
320 magu.m = q2 + 1; // resulting magic number
321 magu.s = p - 32; // resulting shift
322 return magu;
323}
324
325/// magic - calculate the magic numbers required to codegen an integer sdiv as
326/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
327/// or -1.
328static ms magic64(int64_t d) {
329 int64_t p;
330 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
331 const uint64_t two63 = 9223372036854775808ULL; // 2^63
332 struct ms mag;
333
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000334 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000335 t = two63 + ((uint64_t)d >> 63);
336 anc = t - 1 - t%ad; // absolute value of nc
337 p = 63; // initialize p
338 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
339 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
340 q2 = two63/ad; // initialize q2 = 2p/abs(d)
341 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
342 do {
343 p = p + 1;
344 q1 = 2*q1; // update q1 = 2p/abs(nc)
345 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
346 if (r1 >= anc) { // must be unsigned comparison
347 q1 = q1 + 1;
348 r1 = r1 - anc;
349 }
350 q2 = 2*q2; // update q2 = 2p/abs(d)
351 r2 = 2*r2; // update r2 = rem(2p/abs(d))
352 if (r2 >= ad) { // must be unsigned comparison
353 q2 = q2 + 1;
354 r2 = r2 - ad;
355 }
356 delta = ad - r2;
357 } while (q1 < delta || (q1 == delta && r1 == 0));
358
359 mag.m = q2 + 1;
360 if (d < 0) mag.m = -mag.m; // resulting magic number
361 mag.s = p - 64; // resulting shift
362 return mag;
363}
364
365/// magicu - calculate the magic numbers required to codegen an integer udiv as
366/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
367static mu magicu64(uint64_t d)
368{
369 int64_t p;
370 uint64_t nc, delta, q1, r1, q2, r2;
371 struct mu magu;
372 magu.a = 0; // initialize "add" indicator
373 nc = - 1 - (-d)%d;
374 p = 63; // initialize p
375 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
376 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
377 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
378 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
379 do {
380 p = p + 1;
381 if (r1 >= nc - r1 ) {
382 q1 = 2*q1 + 1; // update q1
383 r1 = 2*r1 - nc; // update r1
384 }
385 else {
386 q1 = 2*q1; // update q1
387 r1 = 2*r1; // update r1
388 }
389 if (r2 + 1 >= d - r2) {
390 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
391 q2 = 2*q2 + 1; // update q2
392 r2 = 2*r2 + 1 - d; // update r2
393 }
394 else {
395 if (q2 >= 0x8000000000000000ull) magu.a = 1;
396 q2 = 2*q2; // update q2
397 r2 = 2*r2 + 1; // update r2
398 }
399 delta = d - 1 - r2;
400 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
401 magu.m = q2 + 1; // resulting magic number
402 magu.s = p - 64; // resulting shift
403 return magu;
404}
405
Nate Begeman4ebd8052005-09-01 23:24:04 +0000406// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
407// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000408// Also, set the incoming LHS, RHS, and CC references to the appropriate
409// nodes based on the type of node we are checking. This simplifies life a
410// bit for the callers.
411static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
412 SDOperand &CC) {
413 if (N.getOpcode() == ISD::SETCC) {
414 LHS = N.getOperand(0);
415 RHS = N.getOperand(1);
416 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000417 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000418 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000419 if (N.getOpcode() == ISD::SELECT_CC &&
420 N.getOperand(2).getOpcode() == ISD::Constant &&
421 N.getOperand(3).getOpcode() == ISD::Constant &&
422 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000423 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
424 LHS = N.getOperand(0);
425 RHS = N.getOperand(1);
426 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000427 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000428 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000429 return false;
430}
431
Nate Begeman99801192005-09-07 23:25:52 +0000432// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
433// one use. If this is true, it allows the users to invert the operation for
434// free when it is profitable to do so.
435static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000436 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000437 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000438 return true;
439 return false;
440}
441
Nate Begeman452d7be2005-09-16 00:54:12 +0000442// FIXME: This should probably go in the ISD class rather than being duplicated
443// in several files.
444static bool isCommutativeBinOp(unsigned Opcode) {
445 switch (Opcode) {
446 case ISD::ADD:
447 case ISD::MUL:
448 case ISD::AND:
449 case ISD::OR:
450 case ISD::XOR: return true;
451 default: return false; // FIXME: Need commutative info for user ops!
452 }
453}
454
Nate Begemancd4d58c2006-02-03 06:46:56 +0000455SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
456 MVT::ValueType VT = N0.getValueType();
457 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
458 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
459 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
460 if (isa<ConstantSDNode>(N1)) {
461 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
462 WorkList.push_back(OpNode.Val);
463 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
464 } else if (N0.hasOneUse()) {
465 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
466 WorkList.push_back(OpNode.Val);
467 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
468 }
469 }
470 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
471 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
472 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
473 if (isa<ConstantSDNode>(N0)) {
474 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
475 WorkList.push_back(OpNode.Val);
476 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
477 } else if (N1.hasOneUse()) {
478 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
479 WorkList.push_back(OpNode.Val);
480 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
481 }
482 }
483 return SDOperand();
484}
485
Nate Begeman4ebd8052005-09-01 23:24:04 +0000486void DAGCombiner::Run(bool RunningAfterLegalize) {
487 // set the instance variable, so that the various visit routines may use it.
488 AfterLegalize = RunningAfterLegalize;
489
Nate Begeman646d7e22005-09-02 21:18:40 +0000490 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000491 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
492 E = DAG.allnodes_end(); I != E; ++I)
493 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000494
Chris Lattner95038592005-10-05 06:35:28 +0000495 // Create a dummy node (which is not added to allnodes), that adds a reference
496 // to the root node, preventing it from being deleted, and tracking any
497 // changes of the root.
498 HandleSDNode Dummy(DAG.getRoot());
499
Nate Begeman1d4d4142005-09-01 00:19:25 +0000500 // while the worklist isn't empty, inspect the node on the end of it and
501 // try and combine it.
502 while (!WorkList.empty()) {
503 SDNode *N = WorkList.back();
504 WorkList.pop_back();
505
506 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000507 // N is deleted from the DAG, since they too may now be dead or may have a
508 // reduced number of uses, allowing other xforms.
509 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000510 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
511 WorkList.push_back(N->getOperand(i).Val);
512
Nate Begeman1d4d4142005-09-01 00:19:25 +0000513 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000514 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000515 continue;
516 }
517
Nate Begeman83e75ec2005-09-06 04:43:02 +0000518 SDOperand RV = visit(N);
519 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000520 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000521 // If we get back the same node we passed in, rather than a new node or
522 // zero, we know that the node must have defined multiple values and
523 // CombineTo was used. Since CombineTo takes care of the worklist
524 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000525 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000526 DEBUG(std::cerr << "\nReplacing "; N->dump();
527 std::cerr << "\nWith: "; RV.Val->dump();
528 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000529 std::vector<SDNode*> NowDead;
530 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000531
532 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000533 WorkList.push_back(RV.Val);
534 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000535
536 // Nodes can end up on the worklist more than once. Make sure we do
537 // not process a node that has been replaced.
538 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000539 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
540 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000541
542 // Finally, since the node is now dead, remove it from the graph.
543 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000544 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000545 }
546 }
Chris Lattner95038592005-10-05 06:35:28 +0000547
548 // If the root changed (e.g. it was a dead load, update the root).
549 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000550}
551
Nate Begeman83e75ec2005-09-06 04:43:02 +0000552SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553 switch(N->getOpcode()) {
554 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000555 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000556 case ISD::ADD: return visitADD(N);
557 case ISD::SUB: return visitSUB(N);
558 case ISD::MUL: return visitMUL(N);
559 case ISD::SDIV: return visitSDIV(N);
560 case ISD::UDIV: return visitUDIV(N);
561 case ISD::SREM: return visitSREM(N);
562 case ISD::UREM: return visitUREM(N);
563 case ISD::MULHU: return visitMULHU(N);
564 case ISD::MULHS: return visitMULHS(N);
565 case ISD::AND: return visitAND(N);
566 case ISD::OR: return visitOR(N);
567 case ISD::XOR: return visitXOR(N);
568 case ISD::SHL: return visitSHL(N);
569 case ISD::SRA: return visitSRA(N);
570 case ISD::SRL: return visitSRL(N);
571 case ISD::CTLZ: return visitCTLZ(N);
572 case ISD::CTTZ: return visitCTTZ(N);
573 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000574 case ISD::SELECT: return visitSELECT(N);
575 case ISD::SELECT_CC: return visitSELECT_CC(N);
576 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000577 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
578 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
579 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
580 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000581 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000582 case ISD::FADD: return visitFADD(N);
583 case ISD::FSUB: return visitFSUB(N);
584 case ISD::FMUL: return visitFMUL(N);
585 case ISD::FDIV: return visitFDIV(N);
586 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000587 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
588 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
589 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
590 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
591 case ISD::FP_ROUND: return visitFP_ROUND(N);
592 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
593 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
594 case ISD::FNEG: return visitFNEG(N);
595 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000596 case ISD::BRCOND: return visitBRCOND(N);
597 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
598 case ISD::BR_CC: return visitBR_CC(N);
599 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000600 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000601 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000602 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000603 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000604}
605
Nate Begeman83e75ec2005-09-06 04:43:02 +0000606SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000607 std::vector<SDOperand> Ops;
608 bool Changed = false;
609
Nate Begeman1d4d4142005-09-01 00:19:25 +0000610 // If the token factor has two operands and one is the entry token, replace
611 // the token factor with the other operand.
612 if (N->getNumOperands() == 2) {
613 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000614 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000616 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000618
Nate Begemanded49632005-10-13 03:11:28 +0000619 // fold (tokenfactor (tokenfactor)) -> tokenfactor
620 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
621 SDOperand Op = N->getOperand(i);
622 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
623 Changed = true;
624 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
625 Ops.push_back(Op.getOperand(j));
626 } else {
627 Ops.push_back(Op);
628 }
629 }
630 if (Changed)
631 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000632 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000633}
634
Nate Begeman83e75ec2005-09-06 04:43:02 +0000635SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000636 SDOperand N0 = N->getOperand(0);
637 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000638 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
639 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000640 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000641
642 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000643 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000644 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000645 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000646 if (N0C && !N1C)
647 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000648 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000649 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000650 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000651 // fold ((c1-A)+c2) -> (c1+c2)-A
652 if (N1C && N0.getOpcode() == ISD::SUB)
653 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
654 return DAG.getNode(ISD::SUB, VT,
655 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
656 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000657 // reassociate add
658 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
659 if (RADD.Val != 0)
660 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000661 // fold ((0-A) + B) -> B-A
662 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
663 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000664 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000665 // fold (A + (0-B)) -> A-B
666 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
667 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000668 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000669 // fold (A+(B-A)) -> B
670 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000671 return N1.getOperand(0);
Nate Begemanb0d04a72006-02-18 02:40:58 +0000672 //
673 if (SimplifyDemandedBits(SDOperand(N, 0)))
674 return SDOperand();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000675 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000676}
677
Nate Begeman83e75ec2005-09-06 04:43:02 +0000678SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679 SDOperand N0 = N->getOperand(0);
680 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000681 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
682 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000683 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684
Chris Lattner854077d2005-10-17 01:07:11 +0000685 // fold (sub x, x) -> 0
686 if (N0 == N1)
687 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000688 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000689 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000690 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000691 // fold (sub x, c) -> (add x, -c)
692 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000693 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000694 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000695 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000696 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000697 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000698 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000699 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000701}
702
Nate Begeman83e75ec2005-09-06 04:43:02 +0000703SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704 SDOperand N0 = N->getOperand(0);
705 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000706 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
707 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000708 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000709
710 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000711 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000712 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000713 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000714 if (N0C && !N1C)
715 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000716 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000717 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000718 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000719 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000720 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000721 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000722 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000723 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000724 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000725 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000726 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000727 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
728 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
729 // FIXME: If the input is something that is easily negated (e.g. a
730 // single-use add), we should put the negate there.
731 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
732 DAG.getNode(ISD::SHL, VT, N0,
733 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
734 TLI.getShiftAmountTy())));
735 }
Nate Begemancd4d58c2006-02-03 06:46:56 +0000736 // reassociate mul
737 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
738 if (RMUL.Val != 0)
739 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000740 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000741}
742
Nate Begeman83e75ec2005-09-06 04:43:02 +0000743SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000744 SDOperand N0 = N->getOperand(0);
745 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000746 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
747 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000748 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000749
750 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000751 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000752 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000753 // fold (sdiv X, 1) -> X
754 if (N1C && N1C->getSignExtended() == 1LL)
755 return N0;
756 // fold (sdiv X, -1) -> 0-X
757 if (N1C && N1C->isAllOnesValue())
758 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000759 // If we know the sign bits of both operands are zero, strength reduce to a
760 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
761 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000762 if (TLI.MaskedValueIsZero(N1, SignBit) &&
763 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000764 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000765 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000766 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000767 (isPowerOf2_64(N1C->getSignExtended()) ||
768 isPowerOf2_64(-N1C->getSignExtended()))) {
769 // If dividing by powers of two is cheap, then don't perform the following
770 // fold.
771 if (TLI.isPow2DivCheap())
772 return SDOperand();
773 int64_t pow2 = N1C->getSignExtended();
774 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000775 unsigned lg2 = Log2_64(abs2);
776 // Splat the sign bit into the register
777 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000778 DAG.getConstant(MVT::getSizeInBits(VT)-1,
779 TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000780 WorkList.push_back(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000781 // Add (N0 < 0) ? abs2 - 1 : 0;
782 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
783 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000784 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000785 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
786 WorkList.push_back(SRL.Val);
787 WorkList.push_back(ADD.Val); // Divide by pow2
788 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
789 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000790 // If we're dividing by a positive value, we're done. Otherwise, we must
791 // negate the result.
792 if (pow2 > 0)
793 return SRA;
794 WorkList.push_back(SRA.Val);
795 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
796 }
Nate Begeman69575232005-10-20 02:15:44 +0000797 // if integer divide is expensive and we satisfy the requirements, emit an
798 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000799 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000800 !TLI.isIntDivCheap()) {
801 SDOperand Op = BuildSDIV(N);
802 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000803 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000804 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000805}
806
Nate Begeman83e75ec2005-09-06 04:43:02 +0000807SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000808 SDOperand N0 = N->getOperand(0);
809 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000810 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
811 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000812 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000813
814 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000815 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000816 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000817 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000818 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000819 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000820 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000821 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000822 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
823 if (N1.getOpcode() == ISD::SHL) {
824 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
825 if (isPowerOf2_64(SHC->getValue())) {
826 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000827 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
828 DAG.getConstant(Log2_64(SHC->getValue()),
829 ADDVT));
830 WorkList.push_back(Add.Val);
831 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000832 }
833 }
834 }
Nate Begeman69575232005-10-20 02:15:44 +0000835 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000836 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
837 SDOperand Op = BuildUDIV(N);
838 if (Op.Val) return Op;
839 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000840 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000841}
842
Nate Begeman83e75ec2005-09-06 04:43:02 +0000843SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000844 SDOperand N0 = N->getOperand(0);
845 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000846 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
847 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000848 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000849
850 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000851 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000852 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000853 // If we know the sign bits of both operands are zero, strength reduce to a
854 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
855 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000856 if (TLI.MaskedValueIsZero(N1, SignBit) &&
857 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000858 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000859 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000860}
861
Nate Begeman83e75ec2005-09-06 04:43:02 +0000862SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000863 SDOperand N0 = N->getOperand(0);
864 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000865 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
866 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000867 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000868
869 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000870 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000871 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000872 // fold (urem x, pow2) -> (and x, pow2-1)
873 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000874 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000875 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
876 if (N1.getOpcode() == ISD::SHL) {
877 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
878 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000879 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000880 WorkList.push_back(Add.Val);
881 return DAG.getNode(ISD::AND, VT, N0, Add);
882 }
883 }
884 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000885 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000886}
887
Nate Begeman83e75ec2005-09-06 04:43:02 +0000888SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000889 SDOperand N0 = N->getOperand(0);
890 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000891 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000892
893 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000894 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000895 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000896 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000897 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000898 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
899 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000900 TLI.getShiftAmountTy()));
901 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000902}
903
Nate Begeman83e75ec2005-09-06 04:43:02 +0000904SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000905 SDOperand N0 = N->getOperand(0);
906 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000907 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000908
909 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000910 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000911 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000912 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000913 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000914 return DAG.getConstant(0, N0.getValueType());
915 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000916}
917
Nate Begeman83e75ec2005-09-06 04:43:02 +0000918SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919 SDOperand N0 = N->getOperand(0);
920 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000921 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000922 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
923 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000924 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000925 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000926
927 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000928 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000929 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000930 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000931 if (N0C && !N1C)
932 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000933 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000934 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000935 return N0;
936 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000937 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000938 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000939 // reassociate and
940 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
941 if (RAND.Val != 0)
942 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000943 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000944 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000945 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000946 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000947 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000948 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
949 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
950 unsigned InBits = MVT::getSizeInBits(N0.getOperand(0).getValueType());
951 if (TLI.MaskedValueIsZero(N0.getOperand(0),
952 ~N1C->getValue() & ((1ULL << InBits)-1))) {
953 // We actually want to replace all uses of the any_extend with the
954 // zero_extend, to avoid duplicating things. This will later cause this
955 // AND to be folded.
956 CombineTo(N0.Val, DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
957 N0.getOperand(0)));
958 return SDOperand();
959 }
960 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000961 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
962 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
963 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
964 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
965
966 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
967 MVT::isInteger(LL.getValueType())) {
968 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
969 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
970 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
971 WorkList.push_back(ORNode.Val);
972 return DAG.getSetCC(VT, ORNode, LR, Op1);
973 }
974 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
975 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
976 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
977 WorkList.push_back(ANDNode.Val);
978 return DAG.getSetCC(VT, ANDNode, LR, Op1);
979 }
980 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
981 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
982 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
983 WorkList.push_back(ORNode.Val);
984 return DAG.getSetCC(VT, ORNode, LR, Op1);
985 }
986 }
987 // canonicalize equivalent to ll == rl
988 if (LL == RR && LR == RL) {
989 Op1 = ISD::getSetCCSwappedOperands(Op1);
990 std::swap(RL, RR);
991 }
992 if (LL == RL && LR == RR) {
993 bool isInteger = MVT::isInteger(LL.getValueType());
994 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
995 if (Result != ISD::SETCC_INVALID)
996 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
997 }
998 }
999 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1000 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1001 N1.getOpcode() == ISD::ZERO_EXTEND &&
1002 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1003 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1004 N0.getOperand(0), N1.getOperand(0));
1005 WorkList.push_back(ANDNode.Val);
1006 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1007 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001008 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001009 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001010 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1011 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001012 N0.getOperand(1) == N1.getOperand(1)) {
1013 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1014 N0.getOperand(0), N1.getOperand(0));
1015 WorkList.push_back(ANDNode.Val);
1016 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1017 }
Nate Begemande996292006-02-03 22:24:05 +00001018 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1019 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner012f2412006-02-17 21:58:01 +00001020 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001021 return SDOperand();
Nate Begemanded49632005-10-13 03:11:28 +00001022 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001023 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001024 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001025 // If we zero all the possible extended bits, then we can turn this into
1026 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001027 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001028 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001029 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1030 N0.getOperand(1), N0.getOperand(2),
1031 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001032 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001033 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001034 return SDOperand();
1035 }
1036 }
1037 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001038 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001039 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001040 // If we zero all the possible extended bits, then we can turn this into
1041 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001042 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001043 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001044 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1045 N0.getOperand(1), N0.getOperand(2),
1046 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001047 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001048 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001049 return SDOperand();
1050 }
1051 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001052 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001053}
1054
Nate Begeman83e75ec2005-09-06 04:43:02 +00001055SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001056 SDOperand N0 = N->getOperand(0);
1057 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001058 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001059 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1060 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001061 MVT::ValueType VT = N1.getValueType();
1062 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001063
1064 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001065 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001066 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001067 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001068 if (N0C && !N1C)
1069 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001070 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001071 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001072 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001073 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001074 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001075 return N1;
1076 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001077 if (N1C &&
1078 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001079 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001080 // reassociate or
1081 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1082 if (ROR.Val != 0)
1083 return ROR;
1084 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1085 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001086 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001087 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1088 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1089 N1),
1090 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001091 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001092 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1093 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1094 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1095 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1096
1097 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1098 MVT::isInteger(LL.getValueType())) {
1099 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1100 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1101 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1102 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1103 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1104 WorkList.push_back(ORNode.Val);
1105 return DAG.getSetCC(VT, ORNode, LR, Op1);
1106 }
1107 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1108 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1109 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1110 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1111 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1112 WorkList.push_back(ANDNode.Val);
1113 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1114 }
1115 }
1116 // canonicalize equivalent to ll == rl
1117 if (LL == RR && LR == RL) {
1118 Op1 = ISD::getSetCCSwappedOperands(Op1);
1119 std::swap(RL, RR);
1120 }
1121 if (LL == RL && LR == RR) {
1122 bool isInteger = MVT::isInteger(LL.getValueType());
1123 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1124 if (Result != ISD::SETCC_INVALID)
1125 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1126 }
1127 }
1128 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1129 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1130 N1.getOpcode() == ISD::ZERO_EXTEND &&
1131 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1132 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1133 N0.getOperand(0), N1.getOperand(0));
1134 WorkList.push_back(ORNode.Val);
1135 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1136 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001137 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1138 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1139 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1140 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1141 N0.getOperand(1) == N1.getOperand(1)) {
1142 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1143 N0.getOperand(0), N1.getOperand(0));
1144 WorkList.push_back(ORNode.Val);
1145 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1146 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001147 // canonicalize shl to left side in a shl/srl pair, to match rotate
1148 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1149 std::swap(N0, N1);
1150 // check for rotl, rotr
1151 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1152 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001153 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001154 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1155 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1156 N1.getOperand(1).getOpcode() == ISD::Constant) {
1157 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1158 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1159 if ((c1val + c2val) == OpSizeInBits)
1160 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1161 }
1162 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1163 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1164 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1165 if (ConstantSDNode *SUBC =
1166 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1167 if (SUBC->getValue() == OpSizeInBits)
1168 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1169 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1170 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1171 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1172 if (ConstantSDNode *SUBC =
1173 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1174 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001175 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001176 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1177 N1.getOperand(1));
1178 else
1179 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1180 N0.getOperand(1));
1181 }
1182 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001183 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001184}
1185
Nate Begeman83e75ec2005-09-06 04:43:02 +00001186SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001187 SDOperand N0 = N->getOperand(0);
1188 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001189 SDOperand LHS, RHS, CC;
1190 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1191 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001192 MVT::ValueType VT = N0.getValueType();
1193
1194 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001195 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001196 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001197 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001198 if (N0C && !N1C)
1199 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001200 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001201 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001202 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001203 // reassociate xor
1204 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1205 if (RXOR.Val != 0)
1206 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001207 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001208 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1209 bool isInt = MVT::isInteger(LHS.getValueType());
1210 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1211 isInt);
1212 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001213 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001214 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001215 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001216 assert(0 && "Unhandled SetCC Equivalent!");
1217 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218 }
Nate Begeman99801192005-09-07 23:25:52 +00001219 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1220 if (N1C && N1C->getValue() == 1 &&
1221 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001222 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001223 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1224 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001225 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1226 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001227 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1228 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001229 }
1230 }
Nate Begeman99801192005-09-07 23:25:52 +00001231 // fold !(x or y) -> (!x and !y) iff x or y are constants
1232 if (N1C && N1C->isAllOnesValue() &&
1233 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001235 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1236 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001237 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1238 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001239 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1240 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001241 }
1242 }
Nate Begeman223df222005-09-08 20:18:10 +00001243 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1244 if (N1C && N0.getOpcode() == ISD::XOR) {
1245 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1246 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1247 if (N00C)
1248 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1249 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1250 if (N01C)
1251 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1252 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1253 }
1254 // fold (xor x, x) -> 0
1255 if (N0 == N1)
1256 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001257 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1258 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1259 N1.getOpcode() == ISD::ZERO_EXTEND &&
1260 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1261 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1262 N0.getOperand(0), N1.getOperand(0));
1263 WorkList.push_back(XORNode.Val);
1264 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1265 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001266 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1267 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1268 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1269 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1270 N0.getOperand(1) == N1.getOperand(1)) {
1271 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1272 N0.getOperand(0), N1.getOperand(0));
1273 WorkList.push_back(XORNode.Val);
1274 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1275 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001276 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001277}
1278
Nate Begeman83e75ec2005-09-06 04:43:02 +00001279SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001280 SDOperand N0 = N->getOperand(0);
1281 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001282 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1283 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001284 MVT::ValueType VT = N0.getValueType();
1285 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1286
1287 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001288 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001289 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001290 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001291 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001292 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001293 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001294 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001295 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001296 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001297 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001298 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001300 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001301 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001302 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001303 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001304 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001305 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001306 N0.getOperand(1).getOpcode() == ISD::Constant) {
1307 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001308 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001309 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001310 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001311 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001312 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313 }
1314 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1315 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001316 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001317 N0.getOperand(1).getOpcode() == ISD::Constant) {
1318 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001319 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001320 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1321 DAG.getConstant(~0ULL << c1, VT));
1322 if (c2 > c1)
1323 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001324 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001325 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001326 return DAG.getNode(ISD::SRL, VT, Mask,
1327 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001328 }
1329 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001330 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001331 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001332 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1333 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334}
1335
Nate Begeman83e75ec2005-09-06 04:43:02 +00001336SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337 SDOperand N0 = N->getOperand(0);
1338 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001339 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1340 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001341 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342
1343 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001344 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001345 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001346 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001347 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001348 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001350 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001351 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001352 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001353 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001354 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001355 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001356 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001357 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001358 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1359 // sext_inreg.
1360 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1361 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1362 MVT::ValueType EVT;
1363 switch (LowBits) {
1364 default: EVT = MVT::Other; break;
1365 case 1: EVT = MVT::i1; break;
1366 case 8: EVT = MVT::i8; break;
1367 case 16: EVT = MVT::i16; break;
1368 case 32: EVT = MVT::i32; break;
1369 }
1370 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1371 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1372 DAG.getValueType(EVT));
1373 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001375 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001376 return DAG.getNode(ISD::SRL, VT, N0, N1);
1377 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378}
1379
Nate Begeman83e75ec2005-09-06 04:43:02 +00001380SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381 SDOperand N0 = N->getOperand(0);
1382 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001383 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1384 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385 MVT::ValueType VT = N0.getValueType();
1386 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1387
1388 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001389 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001390 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001391 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001392 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001393 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001394 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001395 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001396 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001398 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001399 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001400 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001401 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001402 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001404 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001405 N0.getOperand(1).getOpcode() == ISD::Constant) {
1406 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001407 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001409 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001411 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001412 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001413 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001414}
1415
Nate Begeman83e75ec2005-09-06 04:43:02 +00001416SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001417 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001418 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001419 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001420
1421 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001422 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001423 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001424 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001425}
1426
Nate Begeman83e75ec2005-09-06 04:43:02 +00001427SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001428 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001429 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001430 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001431
1432 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001433 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001434 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001435 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001436}
1437
Nate Begeman83e75ec2005-09-06 04:43:02 +00001438SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001439 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001440 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001441 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001442
1443 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001444 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001445 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001446 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001447}
1448
Nate Begeman452d7be2005-09-16 00:54:12 +00001449SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1450 SDOperand N0 = N->getOperand(0);
1451 SDOperand N1 = N->getOperand(1);
1452 SDOperand N2 = N->getOperand(2);
1453 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1454 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1455 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1456 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001457
Nate Begeman452d7be2005-09-16 00:54:12 +00001458 // fold select C, X, X -> X
1459 if (N1 == N2)
1460 return N1;
1461 // fold select true, X, Y -> X
1462 if (N0C && !N0C->isNullValue())
1463 return N1;
1464 // fold select false, X, Y -> Y
1465 if (N0C && N0C->isNullValue())
1466 return N2;
1467 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001468 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001469 return DAG.getNode(ISD::OR, VT, N0, N2);
1470 // fold select C, 0, X -> ~C & X
1471 // FIXME: this should check for C type == X type, not i1?
1472 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1473 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1474 WorkList.push_back(XORNode.Val);
1475 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1476 }
1477 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001478 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001479 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1480 WorkList.push_back(XORNode.Val);
1481 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1482 }
1483 // fold select C, X, 0 -> C & X
1484 // FIXME: this should check for C type == X type, not i1?
1485 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1486 return DAG.getNode(ISD::AND, VT, N0, N1);
1487 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1488 if (MVT::i1 == VT && N0 == N1)
1489 return DAG.getNode(ISD::OR, VT, N0, N2);
1490 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1491 if (MVT::i1 == VT && N0 == N2)
1492 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001493 // If we can fold this based on the true/false value, do so.
1494 if (SimplifySelectOps(N, N1, N2))
1495 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001496 // fold selects based on a setcc into other things, such as min/max/abs
1497 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001498 // FIXME:
1499 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1500 // having to say they don't support SELECT_CC on every type the DAG knows
1501 // about, since there is no way to mark an opcode illegal at all value types
1502 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1503 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1504 N1, N2, N0.getOperand(2));
1505 else
1506 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001507 return SDOperand();
1508}
1509
1510SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001511 SDOperand N0 = N->getOperand(0);
1512 SDOperand N1 = N->getOperand(1);
1513 SDOperand N2 = N->getOperand(2);
1514 SDOperand N3 = N->getOperand(3);
1515 SDOperand N4 = N->getOperand(4);
1516 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1517 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1518 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1519 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1520
1521 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001522 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001523 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1524
Nate Begeman44728a72005-09-19 22:34:01 +00001525 // fold select_cc lhs, rhs, x, x, cc -> x
1526 if (N2 == N3)
1527 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001528
1529 // If we can fold this based on the true/false value, do so.
1530 if (SimplifySelectOps(N, N2, N3))
1531 return SDOperand();
1532
Nate Begeman44728a72005-09-19 22:34:01 +00001533 // fold select_cc into other things, such as min/max/abs
1534 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001535}
1536
1537SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1538 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1539 cast<CondCodeSDNode>(N->getOperand(2))->get());
1540}
1541
Nate Begeman83e75ec2005-09-06 04:43:02 +00001542SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001543 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001544 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001545 MVT::ValueType VT = N->getValueType(0);
1546
Nate Begeman1d4d4142005-09-01 00:19:25 +00001547 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001548 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001549 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001550 // fold (sext (sext x)) -> (sext x)
1551 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001552 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001553 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001554 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1555 (!AfterLegalize ||
1556 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001557 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1558 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001559 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001560 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1561 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001562 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1563 N0.getOperand(1), N0.getOperand(2),
1564 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001565 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001566 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1567 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001568 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001569 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001570
1571 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1572 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1573 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1574 N0.hasOneUse()) {
1575 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1576 N0.getOperand(1), N0.getOperand(2),
1577 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001578 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001579 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1580 ExtLoad.getValue(1));
1581 return SDOperand();
1582 }
1583
Nate Begeman83e75ec2005-09-06 04:43:02 +00001584 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001585}
1586
Nate Begeman83e75ec2005-09-06 04:43:02 +00001587SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001588 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001589 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001590 MVT::ValueType VT = N->getValueType(0);
1591
Nate Begeman1d4d4142005-09-01 00:19:25 +00001592 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001593 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001594 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001595 // fold (zext (zext x)) -> (zext x)
1596 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001597 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001598 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1599 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001600 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001601 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001602 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001603 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1604 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001605 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1606 N0.getOperand(1), N0.getOperand(2),
1607 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001608 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001609 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1610 ExtLoad.getValue(1));
1611 return SDOperand();
1612 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001613
1614 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1615 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1616 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1617 N0.hasOneUse()) {
1618 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1619 N0.getOperand(1), N0.getOperand(2),
1620 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001621 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001622 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1623 ExtLoad.getValue(1));
1624 return SDOperand();
1625 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001626 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001627}
1628
Nate Begeman83e75ec2005-09-06 04:43:02 +00001629SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001630 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001631 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001632 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001633 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001634 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001635 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001636
Nate Begeman1d4d4142005-09-01 00:19:25 +00001637 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001638 if (N0C) {
1639 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001640 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001642 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001643 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001644 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001645 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001646 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001647 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1648 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1649 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001650 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001651 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001652 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1653 if (N0.getOpcode() == ISD::AssertSext &&
1654 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001655 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001656 }
1657 // fold (sext_in_reg (sextload x)) -> (sextload x)
1658 if (N0.getOpcode() == ISD::SEXTLOAD &&
1659 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001660 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001661 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001662 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001663 if (N0.getOpcode() == ISD::SETCC &&
1664 TLI.getSetCCResultContents() ==
1665 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001666 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001667 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001668 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001669 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001670 // fold (sext_in_reg (srl x)) -> sra x
1671 if (N0.getOpcode() == ISD::SRL &&
1672 N0.getOperand(1).getOpcode() == ISD::Constant &&
1673 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1674 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1675 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001676 }
Nate Begemanded49632005-10-13 03:11:28 +00001677 // fold (sext_inreg (extload x)) -> (sextload x)
1678 if (N0.getOpcode() == ISD::EXTLOAD &&
1679 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001680 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001681 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1682 N0.getOperand(1), N0.getOperand(2),
1683 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001684 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001685 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001686 return SDOperand();
1687 }
1688 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001689 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001690 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001691 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001692 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1693 N0.getOperand(1), N0.getOperand(2),
1694 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001695 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001696 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001697 return SDOperand();
1698 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001699 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001700}
1701
Nate Begeman83e75ec2005-09-06 04:43:02 +00001702SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001703 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001704 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001705 MVT::ValueType VT = N->getValueType(0);
1706
1707 // noop truncate
1708 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001709 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001710 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001711 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001712 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001713 // fold (truncate (truncate x)) -> (truncate x)
1714 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001715 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001716 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1717 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1718 if (N0.getValueType() < VT)
1719 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001720 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001721 else if (N0.getValueType() > VT)
1722 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001723 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001724 else
1725 // if the source and dest are the same type, we can drop both the extend
1726 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001727 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001728 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001729 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001730 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001731 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1732 "Cannot truncate to larger type!");
1733 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001734 // For big endian targets, we need to add an offset to the pointer to load
1735 // the correct bytes. For little endian systems, we merely need to read
1736 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001737 uint64_t PtrOff =
1738 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001739 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1740 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1741 DAG.getConstant(PtrOff, PtrType));
1742 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001743 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001744 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001745 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001746 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001747 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001748 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001749}
1750
Chris Lattner94683772005-12-23 05:30:37 +00001751SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1752 SDOperand N0 = N->getOperand(0);
1753 MVT::ValueType VT = N->getValueType(0);
1754
1755 // If the input is a constant, let getNode() fold it.
1756 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1757 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1758 if (Res.Val != N) return Res;
1759 }
1760
Chris Lattnerc8547d82005-12-23 05:37:50 +00001761 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1762 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1763
Chris Lattner57104102005-12-23 05:44:41 +00001764 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001765 // FIXME: These xforms need to know that the resultant load doesn't need a
1766 // higher alignment than the original!
1767 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001768 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1769 N0.getOperand(2));
1770 WorkList.push_back(N);
1771 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1772 Load.getValue(1));
1773 return Load;
1774 }
1775
Chris Lattner94683772005-12-23 05:30:37 +00001776 return SDOperand();
1777}
1778
Chris Lattner01b3d732005-09-28 22:28:18 +00001779SDOperand DAGCombiner::visitFADD(SDNode *N) {
1780 SDOperand N0 = N->getOperand(0);
1781 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001782 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1783 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001784 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001785
1786 // fold (fadd c1, c2) -> c1+c2
1787 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001788 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001789 // canonicalize constant to RHS
1790 if (N0CFP && !N1CFP)
1791 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001792 // fold (A + (-B)) -> A-B
1793 if (N1.getOpcode() == ISD::FNEG)
1794 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001795 // fold ((-A) + B) -> B-A
1796 if (N0.getOpcode() == ISD::FNEG)
1797 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001798 return SDOperand();
1799}
1800
1801SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1802 SDOperand N0 = N->getOperand(0);
1803 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001804 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1805 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001806 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001807
1808 // fold (fsub c1, c2) -> c1-c2
1809 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001810 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001811 // fold (A-(-B)) -> A+B
1812 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001813 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001814 return SDOperand();
1815}
1816
1817SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1818 SDOperand N0 = N->getOperand(0);
1819 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001820 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1821 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001822 MVT::ValueType VT = N->getValueType(0);
1823
Nate Begeman11af4ea2005-10-17 20:40:11 +00001824 // fold (fmul c1, c2) -> c1*c2
1825 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001826 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001827 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001828 if (N0CFP && !N1CFP)
1829 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001830 // fold (fmul X, 2.0) -> (fadd X, X)
1831 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1832 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001833 return SDOperand();
1834}
1835
1836SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1837 SDOperand N0 = N->getOperand(0);
1838 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001839 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1840 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001841 MVT::ValueType VT = N->getValueType(0);
1842
Nate Begemana148d982006-01-18 22:35:16 +00001843 // fold (fdiv c1, c2) -> c1/c2
1844 if (N0CFP && N1CFP)
1845 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001846 return SDOperand();
1847}
1848
1849SDOperand DAGCombiner::visitFREM(SDNode *N) {
1850 SDOperand N0 = N->getOperand(0);
1851 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001852 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1853 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001854 MVT::ValueType VT = N->getValueType(0);
1855
Nate Begemana148d982006-01-18 22:35:16 +00001856 // fold (frem c1, c2) -> fmod(c1,c2)
1857 if (N0CFP && N1CFP)
1858 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001859 return SDOperand();
1860}
1861
1862
Nate Begeman83e75ec2005-09-06 04:43:02 +00001863SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001864 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001865 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001866 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001867
1868 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001869 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001870 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001871 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001872}
1873
Nate Begeman83e75ec2005-09-06 04:43:02 +00001874SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001875 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001876 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001877 MVT::ValueType VT = N->getValueType(0);
1878
Nate Begeman1d4d4142005-09-01 00:19:25 +00001879 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001880 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001881 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001882 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001883}
1884
Nate Begeman83e75ec2005-09-06 04:43:02 +00001885SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001886 SDOperand N0 = N->getOperand(0);
1887 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1888 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001889
1890 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001891 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001892 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001893 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894}
1895
Nate Begeman83e75ec2005-09-06 04:43:02 +00001896SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001897 SDOperand N0 = N->getOperand(0);
1898 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1899 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001900
1901 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001902 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001903 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001904 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001905}
1906
Nate Begeman83e75ec2005-09-06 04:43:02 +00001907SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001908 SDOperand N0 = N->getOperand(0);
1909 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1910 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001911
1912 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001913 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001914 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001915 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001916}
1917
Nate Begeman83e75ec2005-09-06 04:43:02 +00001918SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001919 SDOperand N0 = N->getOperand(0);
1920 MVT::ValueType VT = N->getValueType(0);
1921 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001922 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001923
Nate Begeman1d4d4142005-09-01 00:19:25 +00001924 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001925 if (N0CFP) {
1926 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001927 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001928 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001929 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001930}
1931
Nate Begeman83e75ec2005-09-06 04:43:02 +00001932SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001933 SDOperand N0 = N->getOperand(0);
1934 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1935 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001936
1937 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001938 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001939 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001940 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941}
1942
Nate Begeman83e75ec2005-09-06 04:43:02 +00001943SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001944 SDOperand N0 = N->getOperand(0);
1945 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1946 MVT::ValueType VT = N->getValueType(0);
1947
1948 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001949 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001950 return DAG.getNode(ISD::FNEG, VT, N0);
1951 // fold (fneg (sub x, y)) -> (sub y, x)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001952 if (N->getOperand(0).getOpcode() == ISD::SUB)
Nate Begemana148d982006-01-18 22:35:16 +00001953 return DAG.getNode(ISD::SUB, VT, N->getOperand(1), N->getOperand(0));
1954 // fold (fneg (fneg x)) -> x
Nate Begeman1d4d4142005-09-01 00:19:25 +00001955 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001956 return N->getOperand(0).getOperand(0);
1957 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958}
1959
Nate Begeman83e75ec2005-09-06 04:43:02 +00001960SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001961 SDOperand N0 = N->getOperand(0);
1962 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1963 MVT::ValueType VT = N->getValueType(0);
1964
Nate Begeman1d4d4142005-09-01 00:19:25 +00001965 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001966 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001967 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001968 // fold (fabs (fabs x)) -> (fabs x)
1969 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001970 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001971 // fold (fabs (fneg x)) -> (fabs x)
1972 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001973 return DAG.getNode(ISD::FABS, VT, N->getOperand(0).getOperand(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +00001974 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001975}
1976
Nate Begeman44728a72005-09-19 22:34:01 +00001977SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1978 SDOperand Chain = N->getOperand(0);
1979 SDOperand N1 = N->getOperand(1);
1980 SDOperand N2 = N->getOperand(2);
1981 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1982
1983 // never taken branch, fold to chain
1984 if (N1C && N1C->isNullValue())
1985 return Chain;
1986 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001987 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001988 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001989 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
1990 // on the target.
1991 if (N1.getOpcode() == ISD::SETCC &&
1992 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
1993 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
1994 N1.getOperand(0), N1.getOperand(1), N2);
1995 }
Nate Begeman44728a72005-09-19 22:34:01 +00001996 return SDOperand();
1997}
1998
1999SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
2000 SDOperand Chain = N->getOperand(0);
2001 SDOperand N1 = N->getOperand(1);
2002 SDOperand N2 = N->getOperand(2);
2003 SDOperand N3 = N->getOperand(3);
2004 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2005
2006 // unconditional branch to true mbb
2007 if (N1C && N1C->getValue() == 1)
2008 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2009 // unconditional branch to false mbb
2010 if (N1C && N1C->isNullValue())
2011 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002012 // fold a brcondtwoway with a setcc condition into a BRTWOWAY_CC node if
2013 // BRTWOWAY_CC is legal on the target.
2014 if (N1.getOpcode() == ISD::SETCC &&
2015 TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
2016 std::vector<SDOperand> Ops;
2017 Ops.push_back(Chain);
2018 Ops.push_back(N1.getOperand(2));
2019 Ops.push_back(N1.getOperand(0));
2020 Ops.push_back(N1.getOperand(1));
2021 Ops.push_back(N2);
2022 Ops.push_back(N3);
2023 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2024 }
Nate Begeman44728a72005-09-19 22:34:01 +00002025 return SDOperand();
2026}
2027
Chris Lattner3ea0b472005-10-05 06:47:48 +00002028// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2029//
Nate Begeman44728a72005-09-19 22:34:01 +00002030SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002031 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2032 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2033
2034 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002035 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2036 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2037
2038 // fold br_cc true, dest -> br dest (unconditional branch)
2039 if (SCCC && SCCC->getValue())
2040 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2041 N->getOperand(4));
2042 // fold br_cc false, dest -> unconditional fall through
2043 if (SCCC && SCCC->isNullValue())
2044 return N->getOperand(0);
2045 // fold to a simpler setcc
2046 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2047 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2048 Simp.getOperand(2), Simp.getOperand(0),
2049 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002050 return SDOperand();
2051}
2052
2053SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00002054 SDOperand Chain = N->getOperand(0);
2055 SDOperand CCN = N->getOperand(1);
2056 SDOperand LHS = N->getOperand(2);
2057 SDOperand RHS = N->getOperand(3);
2058 SDOperand N4 = N->getOperand(4);
2059 SDOperand N5 = N->getOperand(5);
2060
2061 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
2062 cast<CondCodeSDNode>(CCN)->get(), false);
2063 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2064
2065 // fold select_cc lhs, rhs, x, x, cc -> x
2066 if (N4 == N5)
2067 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2068 // fold select_cc true, x, y -> x
2069 if (SCCC && SCCC->getValue())
2070 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2071 // fold select_cc false, x, y -> y
2072 if (SCCC && SCCC->isNullValue())
2073 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
2074 // fold to a simpler setcc
Chris Lattner03d5e872006-01-29 06:00:45 +00002075 if (SCC.Val && SCC.getOpcode() == ISD::SETCC) {
2076 std::vector<SDOperand> Ops;
2077 Ops.push_back(Chain);
2078 Ops.push_back(SCC.getOperand(2));
2079 Ops.push_back(SCC.getOperand(0));
2080 Ops.push_back(SCC.getOperand(1));
2081 Ops.push_back(N4);
2082 Ops.push_back(N5);
2083 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2084 }
Nate Begeman44728a72005-09-19 22:34:01 +00002085 return SDOperand();
2086}
2087
Chris Lattner01a22022005-10-10 22:04:48 +00002088SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2089 SDOperand Chain = N->getOperand(0);
2090 SDOperand Ptr = N->getOperand(1);
2091 SDOperand SrcValue = N->getOperand(2);
2092
2093 // If this load is directly stored, replace the load value with the stored
2094 // value.
2095 // TODO: Handle store large -> read small portion.
2096 // TODO: Handle TRUNCSTORE/EXTLOAD
2097 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2098 Chain.getOperand(1).getValueType() == N->getValueType(0))
2099 return CombineTo(N, Chain.getOperand(1), Chain);
2100
2101 return SDOperand();
2102}
2103
Chris Lattner87514ca2005-10-10 22:31:19 +00002104SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2105 SDOperand Chain = N->getOperand(0);
2106 SDOperand Value = N->getOperand(1);
2107 SDOperand Ptr = N->getOperand(2);
2108 SDOperand SrcValue = N->getOperand(3);
2109
2110 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002111 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002112 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2113 // Make sure that these stores are the same value type:
2114 // FIXME: we really care that the second store is >= size of the first.
2115 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002116 // Create a new store of Value that replaces both stores.
2117 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002118 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2119 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002120 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2121 PrevStore->getOperand(0), Value, Ptr,
2122 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002123 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002124 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002125 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002126 }
2127
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002128 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002129 // FIXME: This needs to know that the resultant store does not need a
2130 // higher alignment than the original.
2131 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002132 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2133 Ptr, SrcValue);
2134
Chris Lattner87514ca2005-10-10 22:31:19 +00002135 return SDOperand();
2136}
2137
Nate Begeman44728a72005-09-19 22:34:01 +00002138SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002139 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2140
2141 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2142 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2143 // If we got a simplified select_cc node back from SimplifySelectCC, then
2144 // break it down into a new SETCC node, and a new SELECT node, and then return
2145 // the SELECT node, since we were called with a SELECT node.
2146 if (SCC.Val) {
2147 // Check to see if we got a select_cc back (to turn into setcc/select).
2148 // Otherwise, just return whatever node we got back, like fabs.
2149 if (SCC.getOpcode() == ISD::SELECT_CC) {
2150 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2151 SCC.getOperand(0), SCC.getOperand(1),
2152 SCC.getOperand(4));
2153 WorkList.push_back(SETCC.Val);
2154 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2155 SCC.getOperand(3), SETCC);
2156 }
2157 return SCC;
2158 }
Nate Begeman44728a72005-09-19 22:34:01 +00002159 return SDOperand();
2160}
2161
Chris Lattner40c62d52005-10-18 06:04:22 +00002162/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2163/// are the two values being selected between, see if we can simplify the
2164/// select.
2165///
2166bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2167 SDOperand RHS) {
2168
2169 // If this is a select from two identical things, try to pull the operation
2170 // through the select.
2171 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2172#if 0
2173 std::cerr << "SELECT: ["; LHS.Val->dump();
2174 std::cerr << "] ["; RHS.Val->dump();
2175 std::cerr << "]\n";
2176#endif
2177
2178 // If this is a load and the token chain is identical, replace the select
2179 // of two loads with a load through a select of the address to load from.
2180 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2181 // constants have been dropped into the constant pool.
2182 if ((LHS.getOpcode() == ISD::LOAD ||
2183 LHS.getOpcode() == ISD::EXTLOAD ||
2184 LHS.getOpcode() == ISD::ZEXTLOAD ||
2185 LHS.getOpcode() == ISD::SEXTLOAD) &&
2186 // Token chains must be identical.
2187 LHS.getOperand(0) == RHS.getOperand(0) &&
2188 // If this is an EXTLOAD, the VT's must match.
2189 (LHS.getOpcode() == ISD::LOAD ||
2190 LHS.getOperand(3) == RHS.getOperand(3))) {
2191 // FIXME: this conflates two src values, discarding one. This is not
2192 // the right thing to do, but nothing uses srcvalues now. When they do,
2193 // turn SrcValue into a list of locations.
2194 SDOperand Addr;
2195 if (TheSelect->getOpcode() == ISD::SELECT)
2196 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2197 TheSelect->getOperand(0), LHS.getOperand(1),
2198 RHS.getOperand(1));
2199 else
2200 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2201 TheSelect->getOperand(0),
2202 TheSelect->getOperand(1),
2203 LHS.getOperand(1), RHS.getOperand(1),
2204 TheSelect->getOperand(4));
2205
2206 SDOperand Load;
2207 if (LHS.getOpcode() == ISD::LOAD)
2208 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2209 Addr, LHS.getOperand(2));
2210 else
2211 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2212 LHS.getOperand(0), Addr, LHS.getOperand(2),
2213 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2214 // Users of the select now use the result of the load.
2215 CombineTo(TheSelect, Load);
2216
2217 // Users of the old loads now use the new load's chain. We know the
2218 // old-load value is dead now.
2219 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2220 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2221 return true;
2222 }
2223 }
2224
2225 return false;
2226}
2227
Nate Begeman44728a72005-09-19 22:34:01 +00002228SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2229 SDOperand N2, SDOperand N3,
2230 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002231
2232 MVT::ValueType VT = N2.getValueType();
2233 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2234 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2235 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2236 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2237
2238 // Determine if the condition we're dealing with is constant
2239 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2240 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2241
2242 // fold select_cc true, x, y -> x
2243 if (SCCC && SCCC->getValue())
2244 return N2;
2245 // fold select_cc false, x, y -> y
2246 if (SCCC && SCCC->getValue() == 0)
2247 return N3;
2248
2249 // Check to see if we can simplify the select into an fabs node
2250 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2251 // Allow either -0.0 or 0.0
2252 if (CFP->getValue() == 0.0) {
2253 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2254 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2255 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2256 N2 == N3.getOperand(0))
2257 return DAG.getNode(ISD::FABS, VT, N0);
2258
2259 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2260 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2261 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2262 N2.getOperand(0) == N3)
2263 return DAG.getNode(ISD::FABS, VT, N3);
2264 }
2265 }
2266
2267 // Check to see if we can perform the "gzip trick", transforming
2268 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2269 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2270 MVT::isInteger(N0.getValueType()) &&
2271 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2272 MVT::ValueType XType = N0.getValueType();
2273 MVT::ValueType AType = N2.getValueType();
2274 if (XType >= AType) {
2275 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002276 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002277 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2278 unsigned ShCtV = Log2_64(N2C->getValue());
2279 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2280 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2281 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
2282 WorkList.push_back(Shift.Val);
2283 if (XType > AType) {
2284 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2285 WorkList.push_back(Shift.Val);
2286 }
2287 return DAG.getNode(ISD::AND, AType, Shift, N2);
2288 }
2289 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2290 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2291 TLI.getShiftAmountTy()));
2292 WorkList.push_back(Shift.Val);
2293 if (XType > AType) {
2294 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2295 WorkList.push_back(Shift.Val);
2296 }
2297 return DAG.getNode(ISD::AND, AType, Shift, N2);
2298 }
2299 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002300
2301 // fold select C, 16, 0 -> shl C, 4
2302 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2303 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2304 // Get a SetCC of the condition
2305 // FIXME: Should probably make sure that setcc is legal if we ever have a
2306 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00002307 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00002308 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00002309 if (AfterLegalize) {
2310 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002311 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00002312 } else {
2313 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002314 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00002315 }
2316 WorkList.push_back(SCC.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00002317 WorkList.push_back(Temp.Val);
2318 // shl setcc result by log2 n2c
2319 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2320 DAG.getConstant(Log2_64(N2C->getValue()),
2321 TLI.getShiftAmountTy()));
2322 }
2323
Nate Begemanf845b452005-10-08 00:29:44 +00002324 // Check to see if this is the equivalent of setcc
2325 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2326 // otherwise, go ahead with the folds.
2327 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2328 MVT::ValueType XType = N0.getValueType();
2329 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2330 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2331 if (Res.getValueType() != VT)
2332 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2333 return Res;
2334 }
2335
2336 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2337 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2338 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2339 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2340 return DAG.getNode(ISD::SRL, XType, Ctlz,
2341 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2342 TLI.getShiftAmountTy()));
2343 }
2344 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2345 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2346 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2347 N0);
2348 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2349 DAG.getConstant(~0ULL, XType));
2350 return DAG.getNode(ISD::SRL, XType,
2351 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2352 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2353 TLI.getShiftAmountTy()));
2354 }
2355 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2356 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2357 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2358 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2359 TLI.getShiftAmountTy()));
2360 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2361 }
2362 }
2363
2364 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2365 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2366 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2367 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2368 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2369 MVT::ValueType XType = N0.getValueType();
2370 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2371 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2372 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2373 TLI.getShiftAmountTy()));
2374 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
2375 WorkList.push_back(Shift.Val);
2376 WorkList.push_back(Add.Val);
2377 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2378 }
2379 }
2380 }
2381
Nate Begeman44728a72005-09-19 22:34:01 +00002382 return SDOperand();
2383}
2384
Nate Begeman452d7be2005-09-16 00:54:12 +00002385SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002386 SDOperand N1, ISD::CondCode Cond,
2387 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002388 // These setcc operations always fold.
2389 switch (Cond) {
2390 default: break;
2391 case ISD::SETFALSE:
2392 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2393 case ISD::SETTRUE:
2394 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2395 }
2396
2397 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2398 uint64_t C1 = N1C->getValue();
2399 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2400 uint64_t C0 = N0C->getValue();
2401
2402 // Sign extend the operands if required
2403 if (ISD::isSignedIntSetCC(Cond)) {
2404 C0 = N0C->getSignExtended();
2405 C1 = N1C->getSignExtended();
2406 }
2407
2408 switch (Cond) {
2409 default: assert(0 && "Unknown integer setcc!");
2410 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2411 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2412 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2413 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2414 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2415 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2416 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2417 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2418 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2419 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2420 }
2421 } else {
2422 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2423 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2424 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2425
2426 // If the comparison constant has bits in the upper part, the
2427 // zero-extended value could never match.
2428 if (C1 & (~0ULL << InSize)) {
2429 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2430 switch (Cond) {
2431 case ISD::SETUGT:
2432 case ISD::SETUGE:
2433 case ISD::SETEQ: return DAG.getConstant(0, VT);
2434 case ISD::SETULT:
2435 case ISD::SETULE:
2436 case ISD::SETNE: return DAG.getConstant(1, VT);
2437 case ISD::SETGT:
2438 case ISD::SETGE:
2439 // True if the sign bit of C1 is set.
2440 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2441 case ISD::SETLT:
2442 case ISD::SETLE:
2443 // True if the sign bit of C1 isn't set.
2444 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2445 default:
2446 break;
2447 }
2448 }
2449
2450 // Otherwise, we can perform the comparison with the low bits.
2451 switch (Cond) {
2452 case ISD::SETEQ:
2453 case ISD::SETNE:
2454 case ISD::SETUGT:
2455 case ISD::SETUGE:
2456 case ISD::SETULT:
2457 case ISD::SETULE:
2458 return DAG.getSetCC(VT, N0.getOperand(0),
2459 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2460 Cond);
2461 default:
2462 break; // todo, be more careful with signed comparisons
2463 }
2464 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2465 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2466 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2467 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2468 MVT::ValueType ExtDstTy = N0.getValueType();
2469 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2470
2471 // If the extended part has any inconsistent bits, it cannot ever
2472 // compare equal. In other words, they have to be all ones or all
2473 // zeros.
2474 uint64_t ExtBits =
2475 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2476 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2477 return DAG.getConstant(Cond == ISD::SETNE, VT);
2478
2479 SDOperand ZextOp;
2480 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2481 if (Op0Ty == ExtSrcTy) {
2482 ZextOp = N0.getOperand(0);
2483 } else {
2484 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2485 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2486 DAG.getConstant(Imm, Op0Ty));
2487 }
2488 WorkList.push_back(ZextOp.Val);
2489 // Otherwise, make this a use of a zext.
2490 return DAG.getSetCC(VT, ZextOp,
2491 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2492 ExtDstTy),
2493 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002494 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2495 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2496 (N0.getOpcode() == ISD::XOR ||
2497 (N0.getOpcode() == ISD::AND &&
2498 N0.getOperand(0).getOpcode() == ISD::XOR &&
2499 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2500 isa<ConstantSDNode>(N0.getOperand(1)) &&
2501 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2502 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2503 // only do this if the top bits are known zero.
2504 if (TLI.MaskedValueIsZero(N1,
2505 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2506 // Okay, get the un-inverted input value.
2507 SDOperand Val;
2508 if (N0.getOpcode() == ISD::XOR)
2509 Val = N0.getOperand(0);
2510 else {
2511 assert(N0.getOpcode() == ISD::AND &&
2512 N0.getOperand(0).getOpcode() == ISD::XOR);
2513 // ((X^1)&1)^1 -> X & 1
2514 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2515 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2516 }
2517 return DAG.getSetCC(VT, Val, N1,
2518 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2519 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002520 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002521
Nate Begeman452d7be2005-09-16 00:54:12 +00002522 uint64_t MinVal, MaxVal;
2523 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2524 if (ISD::isSignedIntSetCC(Cond)) {
2525 MinVal = 1ULL << (OperandBitSize-1);
2526 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2527 MaxVal = ~0ULL >> (65-OperandBitSize);
2528 else
2529 MaxVal = 0;
2530 } else {
2531 MinVal = 0;
2532 MaxVal = ~0ULL >> (64-OperandBitSize);
2533 }
2534
2535 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2536 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2537 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2538 --C1; // X >= C0 --> X > (C0-1)
2539 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2540 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2541 }
2542
2543 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2544 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2545 ++C1; // X <= C0 --> X < (C0+1)
2546 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2547 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2548 }
2549
2550 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2551 return DAG.getConstant(0, VT); // X < MIN --> false
2552
2553 // Canonicalize setgt X, Min --> setne X, Min
2554 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2555 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002556 // Canonicalize setlt X, Max --> setne X, Max
2557 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2558 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002559
2560 // If we have setult X, 1, turn it into seteq X, 0
2561 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2562 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2563 ISD::SETEQ);
2564 // If we have setugt X, Max-1, turn it into seteq X, Max
2565 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2566 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2567 ISD::SETEQ);
2568
2569 // If we have "setcc X, C0", check to see if we can shrink the immediate
2570 // by changing cc.
2571
2572 // SETUGT X, SINTMAX -> SETLT X, 0
2573 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2574 C1 == (~0ULL >> (65-OperandBitSize)))
2575 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2576 ISD::SETLT);
2577
2578 // FIXME: Implement the rest of these.
2579
2580 // Fold bit comparisons when we can.
2581 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2582 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2583 if (ConstantSDNode *AndRHS =
2584 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2585 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2586 // Perform the xform if the AND RHS is a single bit.
2587 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2588 return DAG.getNode(ISD::SRL, VT, N0,
2589 DAG.getConstant(Log2_64(AndRHS->getValue()),
2590 TLI.getShiftAmountTy()));
2591 }
2592 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2593 // (X & 8) == 8 --> (X & 8) >> 3
2594 // Perform the xform if C1 is a single bit.
2595 if ((C1 & (C1-1)) == 0) {
2596 return DAG.getNode(ISD::SRL, VT, N0,
2597 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2598 }
2599 }
2600 }
2601 }
2602 } else if (isa<ConstantSDNode>(N0.Val)) {
2603 // Ensure that the constant occurs on the RHS.
2604 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2605 }
2606
2607 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2608 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2609 double C0 = N0C->getValue(), C1 = N1C->getValue();
2610
2611 switch (Cond) {
2612 default: break; // FIXME: Implement the rest of these!
2613 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2614 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2615 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2616 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2617 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2618 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2619 }
2620 } else {
2621 // Ensure that the constant occurs on the RHS.
2622 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2623 }
2624
2625 if (N0 == N1) {
2626 // We can always fold X == Y for integer setcc's.
2627 if (MVT::isInteger(N0.getValueType()))
2628 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2629 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2630 if (UOF == 2) // FP operators that are undefined on NaNs.
2631 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2632 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2633 return DAG.getConstant(UOF, VT);
2634 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2635 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002636 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002637 if (NewCond != Cond)
2638 return DAG.getSetCC(VT, N0, N1, NewCond);
2639 }
2640
2641 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2642 MVT::isInteger(N0.getValueType())) {
2643 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2644 N0.getOpcode() == ISD::XOR) {
2645 // Simplify (X+Y) == (X+Z) --> Y == Z
2646 if (N0.getOpcode() == N1.getOpcode()) {
2647 if (N0.getOperand(0) == N1.getOperand(0))
2648 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2649 if (N0.getOperand(1) == N1.getOperand(1))
2650 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2651 if (isCommutativeBinOp(N0.getOpcode())) {
2652 // If X op Y == Y op X, try other combinations.
2653 if (N0.getOperand(0) == N1.getOperand(1))
2654 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2655 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002656 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002657 }
2658 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002659
2660 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2661 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2662 // Turn (X+C1) == C2 --> X == C2-C1
2663 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2664 return DAG.getSetCC(VT, N0.getOperand(0),
2665 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2666 N0.getValueType()), Cond);
2667 }
2668
2669 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2670 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002671 // If we know that all of the inverted bits are zero, don't bother
2672 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002673 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002674 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002675 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002676 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002677 }
2678
2679 // Turn (C1-X) == C2 --> X == C1-C2
2680 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2681 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
2682 return DAG.getSetCC(VT, N0.getOperand(1),
2683 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
2684 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00002685 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002686 }
2687 }
2688
Nate Begeman452d7be2005-09-16 00:54:12 +00002689 // Simplify (X+Z) == X --> Z == 0
2690 if (N0.getOperand(0) == N1)
2691 return DAG.getSetCC(VT, N0.getOperand(1),
2692 DAG.getConstant(0, N0.getValueType()), Cond);
2693 if (N0.getOperand(1) == N1) {
2694 if (isCommutativeBinOp(N0.getOpcode()))
2695 return DAG.getSetCC(VT, N0.getOperand(0),
2696 DAG.getConstant(0, N0.getValueType()), Cond);
2697 else {
2698 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2699 // (Z-X) == X --> Z == X<<1
2700 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2701 N1,
2702 DAG.getConstant(1,TLI.getShiftAmountTy()));
2703 WorkList.push_back(SH.Val);
2704 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2705 }
2706 }
2707 }
2708
2709 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2710 N1.getOpcode() == ISD::XOR) {
2711 // Simplify X == (X+Z) --> Z == 0
2712 if (N1.getOperand(0) == N0) {
2713 return DAG.getSetCC(VT, N1.getOperand(1),
2714 DAG.getConstant(0, N1.getValueType()), Cond);
2715 } else if (N1.getOperand(1) == N0) {
2716 if (isCommutativeBinOp(N1.getOpcode())) {
2717 return DAG.getSetCC(VT, N1.getOperand(0),
2718 DAG.getConstant(0, N1.getValueType()), Cond);
2719 } else {
2720 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2721 // X == (Z-X) --> X<<1 == Z
2722 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2723 DAG.getConstant(1,TLI.getShiftAmountTy()));
2724 WorkList.push_back(SH.Val);
2725 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2726 }
2727 }
2728 }
2729 }
2730
2731 // Fold away ALL boolean setcc's.
2732 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002733 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002734 switch (Cond) {
2735 default: assert(0 && "Unknown integer setcc!");
2736 case ISD::SETEQ: // X == Y -> (X^Y)^1
2737 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2738 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2739 WorkList.push_back(Temp.Val);
2740 break;
2741 case ISD::SETNE: // X != Y --> (X^Y)
2742 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2743 break;
2744 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2745 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2746 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2747 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2748 WorkList.push_back(Temp.Val);
2749 break;
2750 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2751 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2752 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2753 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2754 WorkList.push_back(Temp.Val);
2755 break;
2756 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2757 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2758 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2759 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2760 WorkList.push_back(Temp.Val);
2761 break;
2762 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2763 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2764 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2765 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2766 break;
2767 }
2768 if (VT != MVT::i1) {
2769 WorkList.push_back(N0.Val);
2770 // FIXME: If running after legalize, we probably can't do this.
2771 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2772 }
2773 return N0;
2774 }
2775
2776 // Could not fold it.
2777 return SDOperand();
2778}
2779
Nate Begeman69575232005-10-20 02:15:44 +00002780/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2781/// return a DAG expression to select that will generate the same value by
2782/// multiplying by a magic number. See:
2783/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2784SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2785 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002786
2787 // Check to see if we can do this.
2788 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2789 return SDOperand(); // BuildSDIV only operates on i32 or i64
2790 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2791 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002792
Nate Begemanc6a454e2005-10-20 17:45:03 +00002793 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002794 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2795
2796 // Multiply the numerator (operand 0) by the magic value
2797 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2798 DAG.getConstant(magics.m, VT));
2799 // If d > 0 and m < 0, add the numerator
2800 if (d > 0 && magics.m < 0) {
2801 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2802 WorkList.push_back(Q.Val);
2803 }
2804 // If d < 0 and m > 0, subtract the numerator.
2805 if (d < 0 && magics.m > 0) {
2806 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2807 WorkList.push_back(Q.Val);
2808 }
2809 // Shift right algebraic if shift value is nonzero
2810 if (magics.s > 0) {
2811 Q = DAG.getNode(ISD::SRA, VT, Q,
2812 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2813 WorkList.push_back(Q.Val);
2814 }
2815 // Extract the sign bit and add it to the quotient
2816 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002817 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2818 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +00002819 WorkList.push_back(T.Val);
2820 return DAG.getNode(ISD::ADD, VT, Q, T);
2821}
2822
2823/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2824/// return a DAG expression to select that will generate the same value by
2825/// multiplying by a magic number. See:
2826/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2827SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2828 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002829
2830 // Check to see if we can do this.
2831 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2832 return SDOperand(); // BuildUDIV only operates on i32 or i64
2833 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2834 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002835
2836 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2837 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2838
2839 // Multiply the numerator (operand 0) by the magic value
2840 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2841 DAG.getConstant(magics.m, VT));
2842 WorkList.push_back(Q.Val);
2843
2844 if (magics.a == 0) {
2845 return DAG.getNode(ISD::SRL, VT, Q,
2846 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2847 } else {
2848 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2849 WorkList.push_back(NPQ.Val);
2850 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2851 DAG.getConstant(1, TLI.getShiftAmountTy()));
2852 WorkList.push_back(NPQ.Val);
2853 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2854 WorkList.push_back(NPQ.Val);
2855 return DAG.getNode(ISD::SRL, VT, NPQ,
2856 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
2857 }
2858}
2859
Nate Begeman1d4d4142005-09-01 00:19:25 +00002860// SelectionDAG::Combine - This is the entry point for the file.
2861//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002862void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002863 /// run - This is the main entry point to this class.
2864 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002865 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002866}