blob: f79f783214ac8376e1cf4862985a70ba499c5af5 [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
Chris Lattner01a22022005-10-10 22:04:48 +000036// FIXME: reassociate (X+C)+Y into (X+Y)+C if the inner expression has one use
Nate Begeman1d4d4142005-09-01 00:19:25 +000037//
38//===----------------------------------------------------------------------===//
39
40#define DEBUG_TYPE "dagcombine"
41#include "llvm/ADT/Statistic.h"
42#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000043#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000044#include "llvm/Support/MathExtras.h"
45#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000046#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000047#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000048#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000049using namespace llvm;
50
51namespace {
52 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
53
54 class DAGCombiner {
55 SelectionDAG &DAG;
56 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000057 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000058
59 // Worklist of all of the nodes that need to be simplified.
60 std::vector<SDNode*> WorkList;
61
62 /// AddUsersToWorkList - When an instruction is simplified, add all users of
63 /// the instruction to the work lists because they might get more simplified
64 /// now.
65 ///
66 void AddUsersToWorkList(SDNode *N) {
67 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000068 UI != UE; ++UI)
69 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000070 }
71
72 /// removeFromWorkList - remove all instances of N from the worklist.
73 void removeFromWorkList(SDNode *N) {
74 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
75 WorkList.end());
76 }
77
Chris Lattner01a22022005-10-10 22:04:48 +000078 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000079 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000080 DEBUG(std::cerr << "\nReplacing "; N->dump();
81 std::cerr << "\nWith: "; To[0].Val->dump();
82 std::cerr << " and " << To.size()-1 << " other values\n");
83 std::vector<SDNode*> NowDead;
84 DAG.ReplaceAllUsesWith(N, To, &NowDead);
85
86 // Push the new nodes and any users onto the worklist
87 for (unsigned i = 0, e = To.size(); i != e; ++i) {
88 WorkList.push_back(To[i].Val);
89 AddUsersToWorkList(To[i].Val);
90 }
91
92 // Nodes can end up on the worklist more than once. Make sure we do
93 // not process a node that has been replaced.
94 removeFromWorkList(N);
95 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
96 removeFromWorkList(NowDead[i]);
97
98 // Finally, since the node is now dead, remove it from the graph.
99 DAG.DeleteNode(N);
100 return SDOperand(N, 0);
101 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000102
103 SDOperand CombineTo(SDNode *N, SDOperand Res) {
104 std::vector<SDOperand> To;
105 To.push_back(Res);
106 return CombineTo(N, To);
107 }
Chris Lattner01a22022005-10-10 22:04:48 +0000108
109 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
110 std::vector<SDOperand> To;
111 To.push_back(Res0);
112 To.push_back(Res1);
113 return CombineTo(N, To);
114 }
115
Nate Begeman1d4d4142005-09-01 00:19:25 +0000116 /// visit - call the node-specific routine that knows how to fold each
117 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000118 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000119
120 // Visitation implementation - Implement dag node combining for different
121 // node types. The semantics are as follows:
122 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000123 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000124 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000125 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000126 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000127 SDOperand visitTokenFactor(SDNode *N);
128 SDOperand visitADD(SDNode *N);
129 SDOperand visitSUB(SDNode *N);
130 SDOperand visitMUL(SDNode *N);
131 SDOperand visitSDIV(SDNode *N);
132 SDOperand visitUDIV(SDNode *N);
133 SDOperand visitSREM(SDNode *N);
134 SDOperand visitUREM(SDNode *N);
135 SDOperand visitMULHU(SDNode *N);
136 SDOperand visitMULHS(SDNode *N);
137 SDOperand visitAND(SDNode *N);
138 SDOperand visitOR(SDNode *N);
139 SDOperand visitXOR(SDNode *N);
140 SDOperand visitSHL(SDNode *N);
141 SDOperand visitSRA(SDNode *N);
142 SDOperand visitSRL(SDNode *N);
143 SDOperand visitCTLZ(SDNode *N);
144 SDOperand visitCTTZ(SDNode *N);
145 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000146 SDOperand visitSELECT(SDNode *N);
147 SDOperand visitSELECT_CC(SDNode *N);
148 SDOperand visitSETCC(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000149 SDOperand visitADD_PARTS(SDNode *N);
150 SDOperand visitSUB_PARTS(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000151 SDOperand visitSIGN_EXTEND(SDNode *N);
152 SDOperand visitZERO_EXTEND(SDNode *N);
153 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
154 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000155 SDOperand visitBIT_CONVERT(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000156
Chris Lattner01b3d732005-09-28 22:28:18 +0000157 SDOperand visitFADD(SDNode *N);
158 SDOperand visitFSUB(SDNode *N);
159 SDOperand visitFMUL(SDNode *N);
160 SDOperand visitFDIV(SDNode *N);
161 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000162 SDOperand visitSINT_TO_FP(SDNode *N);
163 SDOperand visitUINT_TO_FP(SDNode *N);
164 SDOperand visitFP_TO_SINT(SDNode *N);
165 SDOperand visitFP_TO_UINT(SDNode *N);
166 SDOperand visitFP_ROUND(SDNode *N);
167 SDOperand visitFP_ROUND_INREG(SDNode *N);
168 SDOperand visitFP_EXTEND(SDNode *N);
169 SDOperand visitFNEG(SDNode *N);
170 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000171 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000172 SDOperand visitBRCONDTWOWAY(SDNode *N);
173 SDOperand visitBR_CC(SDNode *N);
174 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000175
Chris Lattner01a22022005-10-10 22:04:48 +0000176 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000177 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000178
Jim Laskeyd6e8d412005-12-23 20:08:28 +0000179 SDOperand visitLOCATION(SDNode *N);
180 SDOperand visitDEBUGLOC(SDNode *N);
181
Chris Lattner40c62d52005-10-18 06:04:22 +0000182 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000183 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
184 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
185 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000186 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000187 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000188
189 SDOperand BuildSDIV(SDNode *N);
190 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000191public:
192 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000193 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000194
195 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000196 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000197 };
198}
199
Nate Begeman69575232005-10-20 02:15:44 +0000200struct ms {
201 int64_t m; // magic number
202 int64_t s; // shift amount
203};
204
205struct mu {
206 uint64_t m; // magic number
207 int64_t a; // add indicator
208 int64_t s; // shift amount
209};
210
211/// magic - calculate the magic numbers required to codegen an integer sdiv as
212/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
213/// or -1.
214static ms magic32(int32_t d) {
215 int32_t p;
216 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
217 const uint32_t two31 = 0x80000000U;
218 struct ms mag;
219
220 ad = abs(d);
221 t = two31 + ((uint32_t)d >> 31);
222 anc = t - 1 - t%ad; // absolute value of nc
223 p = 31; // initialize p
224 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
225 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
226 q2 = two31/ad; // initialize q2 = 2p/abs(d)
227 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
228 do {
229 p = p + 1;
230 q1 = 2*q1; // update q1 = 2p/abs(nc)
231 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
232 if (r1 >= anc) { // must be unsigned comparison
233 q1 = q1 + 1;
234 r1 = r1 - anc;
235 }
236 q2 = 2*q2; // update q2 = 2p/abs(d)
237 r2 = 2*r2; // update r2 = rem(2p/abs(d))
238 if (r2 >= ad) { // must be unsigned comparison
239 q2 = q2 + 1;
240 r2 = r2 - ad;
241 }
242 delta = ad - r2;
243 } while (q1 < delta || (q1 == delta && r1 == 0));
244
245 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
246 if (d < 0) mag.m = -mag.m; // resulting magic number
247 mag.s = p - 32; // resulting shift
248 return mag;
249}
250
251/// magicu - calculate the magic numbers required to codegen an integer udiv as
252/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
253static mu magicu32(uint32_t d) {
254 int32_t p;
255 uint32_t nc, delta, q1, r1, q2, r2;
256 struct mu magu;
257 magu.a = 0; // initialize "add" indicator
258 nc = - 1 - (-d)%d;
259 p = 31; // initialize p
260 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
261 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
262 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
263 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
264 do {
265 p = p + 1;
266 if (r1 >= nc - r1 ) {
267 q1 = 2*q1 + 1; // update q1
268 r1 = 2*r1 - nc; // update r1
269 }
270 else {
271 q1 = 2*q1; // update q1
272 r1 = 2*r1; // update r1
273 }
274 if (r2 + 1 >= d - r2) {
275 if (q2 >= 0x7FFFFFFF) magu.a = 1;
276 q2 = 2*q2 + 1; // update q2
277 r2 = 2*r2 + 1 - d; // update r2
278 }
279 else {
280 if (q2 >= 0x80000000) magu.a = 1;
281 q2 = 2*q2; // update q2
282 r2 = 2*r2 + 1; // update r2
283 }
284 delta = d - 1 - r2;
285 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
286 magu.m = q2 + 1; // resulting magic number
287 magu.s = p - 32; // resulting shift
288 return magu;
289}
290
291/// magic - calculate the magic numbers required to codegen an integer sdiv as
292/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
293/// or -1.
294static ms magic64(int64_t d) {
295 int64_t p;
296 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
297 const uint64_t two63 = 9223372036854775808ULL; // 2^63
298 struct ms mag;
299
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000300 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000301 t = two63 + ((uint64_t)d >> 63);
302 anc = t - 1 - t%ad; // absolute value of nc
303 p = 63; // initialize p
304 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
305 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
306 q2 = two63/ad; // initialize q2 = 2p/abs(d)
307 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
308 do {
309 p = p + 1;
310 q1 = 2*q1; // update q1 = 2p/abs(nc)
311 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
312 if (r1 >= anc) { // must be unsigned comparison
313 q1 = q1 + 1;
314 r1 = r1 - anc;
315 }
316 q2 = 2*q2; // update q2 = 2p/abs(d)
317 r2 = 2*r2; // update r2 = rem(2p/abs(d))
318 if (r2 >= ad) { // must be unsigned comparison
319 q2 = q2 + 1;
320 r2 = r2 - ad;
321 }
322 delta = ad - r2;
323 } while (q1 < delta || (q1 == delta && r1 == 0));
324
325 mag.m = q2 + 1;
326 if (d < 0) mag.m = -mag.m; // resulting magic number
327 mag.s = p - 64; // resulting shift
328 return mag;
329}
330
331/// magicu - calculate the magic numbers required to codegen an integer udiv as
332/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
333static mu magicu64(uint64_t d)
334{
335 int64_t p;
336 uint64_t nc, delta, q1, r1, q2, r2;
337 struct mu magu;
338 magu.a = 0; // initialize "add" indicator
339 nc = - 1 - (-d)%d;
340 p = 63; // initialize p
341 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
342 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
343 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
344 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
345 do {
346 p = p + 1;
347 if (r1 >= nc - r1 ) {
348 q1 = 2*q1 + 1; // update q1
349 r1 = 2*r1 - nc; // update r1
350 }
351 else {
352 q1 = 2*q1; // update q1
353 r1 = 2*r1; // update r1
354 }
355 if (r2 + 1 >= d - r2) {
356 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
357 q2 = 2*q2 + 1; // update q2
358 r2 = 2*r2 + 1 - d; // update r2
359 }
360 else {
361 if (q2 >= 0x8000000000000000ull) magu.a = 1;
362 q2 = 2*q2; // update q2
363 r2 = 2*r2 + 1; // update r2
364 }
365 delta = d - 1 - r2;
366 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
367 magu.m = q2 + 1; // resulting magic number
368 magu.s = p - 64; // resulting shift
369 return magu;
370}
371
Nate Begeman4ebd8052005-09-01 23:24:04 +0000372// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
373// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000374// Also, set the incoming LHS, RHS, and CC references to the appropriate
375// nodes based on the type of node we are checking. This simplifies life a
376// bit for the callers.
377static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
378 SDOperand &CC) {
379 if (N.getOpcode() == ISD::SETCC) {
380 LHS = N.getOperand(0);
381 RHS = N.getOperand(1);
382 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000383 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000384 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000385 if (N.getOpcode() == ISD::SELECT_CC &&
386 N.getOperand(2).getOpcode() == ISD::Constant &&
387 N.getOperand(3).getOpcode() == ISD::Constant &&
388 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000389 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
390 LHS = N.getOperand(0);
391 RHS = N.getOperand(1);
392 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000393 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000394 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000395 return false;
396}
397
Nate Begeman99801192005-09-07 23:25:52 +0000398// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
399// one use. If this is true, it allows the users to invert the operation for
400// free when it is profitable to do so.
401static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000402 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000403 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000404 return true;
405 return false;
406}
407
Nate Begeman452d7be2005-09-16 00:54:12 +0000408// FIXME: This should probably go in the ISD class rather than being duplicated
409// in several files.
410static bool isCommutativeBinOp(unsigned Opcode) {
411 switch (Opcode) {
412 case ISD::ADD:
413 case ISD::MUL:
414 case ISD::AND:
415 case ISD::OR:
416 case ISD::XOR: return true;
417 default: return false; // FIXME: Need commutative info for user ops!
418 }
419}
420
Nate Begeman4ebd8052005-09-01 23:24:04 +0000421void DAGCombiner::Run(bool RunningAfterLegalize) {
422 // set the instance variable, so that the various visit routines may use it.
423 AfterLegalize = RunningAfterLegalize;
424
Nate Begeman646d7e22005-09-02 21:18:40 +0000425 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000426 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
427 E = DAG.allnodes_end(); I != E; ++I)
428 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000429
Chris Lattner95038592005-10-05 06:35:28 +0000430 // Create a dummy node (which is not added to allnodes), that adds a reference
431 // to the root node, preventing it from being deleted, and tracking any
432 // changes of the root.
433 HandleSDNode Dummy(DAG.getRoot());
434
Nate Begeman1d4d4142005-09-01 00:19:25 +0000435 // while the worklist isn't empty, inspect the node on the end of it and
436 // try and combine it.
437 while (!WorkList.empty()) {
438 SDNode *N = WorkList.back();
439 WorkList.pop_back();
440
441 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000442 // N is deleted from the DAG, since they too may now be dead or may have a
443 // reduced number of uses, allowing other xforms.
444 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000445 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
446 WorkList.push_back(N->getOperand(i).Val);
447
Nate Begeman1d4d4142005-09-01 00:19:25 +0000448 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000449 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000450 continue;
451 }
452
Nate Begeman83e75ec2005-09-06 04:43:02 +0000453 SDOperand RV = visit(N);
454 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000455 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000456 // If we get back the same node we passed in, rather than a new node or
457 // zero, we know that the node must have defined multiple values and
458 // CombineTo was used. Since CombineTo takes care of the worklist
459 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000460 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000461 DEBUG(std::cerr << "\nReplacing "; N->dump();
462 std::cerr << "\nWith: "; RV.Val->dump();
463 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000464 std::vector<SDNode*> NowDead;
465 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000466
467 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000468 WorkList.push_back(RV.Val);
469 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000470
471 // Nodes can end up on the worklist more than once. Make sure we do
472 // not process a node that has been replaced.
473 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000474 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
475 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000476
477 // Finally, since the node is now dead, remove it from the graph.
478 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000479 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000480 }
481 }
Chris Lattner95038592005-10-05 06:35:28 +0000482
483 // If the root changed (e.g. it was a dead load, update the root).
484 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000485}
486
Nate Begeman83e75ec2005-09-06 04:43:02 +0000487SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000488 switch(N->getOpcode()) {
489 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000490 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000491 case ISD::ADD: return visitADD(N);
492 case ISD::SUB: return visitSUB(N);
493 case ISD::MUL: return visitMUL(N);
494 case ISD::SDIV: return visitSDIV(N);
495 case ISD::UDIV: return visitUDIV(N);
496 case ISD::SREM: return visitSREM(N);
497 case ISD::UREM: return visitUREM(N);
498 case ISD::MULHU: return visitMULHU(N);
499 case ISD::MULHS: return visitMULHS(N);
500 case ISD::AND: return visitAND(N);
501 case ISD::OR: return visitOR(N);
502 case ISD::XOR: return visitXOR(N);
503 case ISD::SHL: return visitSHL(N);
504 case ISD::SRA: return visitSRA(N);
505 case ISD::SRL: return visitSRL(N);
506 case ISD::CTLZ: return visitCTLZ(N);
507 case ISD::CTTZ: return visitCTTZ(N);
508 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000509 case ISD::SELECT: return visitSELECT(N);
510 case ISD::SELECT_CC: return visitSELECT_CC(N);
511 case ISD::SETCC: return visitSETCC(N);
Nate Begeman5054f162005-10-14 01:12:21 +0000512 case ISD::ADD_PARTS: return visitADD_PARTS(N);
513 case ISD::SUB_PARTS: return visitSUB_PARTS(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000514 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
515 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
516 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
517 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000518 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000519 case ISD::FADD: return visitFADD(N);
520 case ISD::FSUB: return visitFSUB(N);
521 case ISD::FMUL: return visitFMUL(N);
522 case ISD::FDIV: return visitFDIV(N);
523 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000524 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
525 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
526 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
527 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
528 case ISD::FP_ROUND: return visitFP_ROUND(N);
529 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
530 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
531 case ISD::FNEG: return visitFNEG(N);
532 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000533 case ISD::BRCOND: return visitBRCOND(N);
534 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
535 case ISD::BR_CC: return visitBR_CC(N);
536 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000537 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000538 case ISD::STORE: return visitSTORE(N);
Jim Laskeyd6e8d412005-12-23 20:08:28 +0000539 case ISD::LOCATION: return visitLOCATION(N);
540 case ISD::DEBUG_LOC: return visitDEBUGLOC(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000541 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000542 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543}
544
Nate Begeman83e75ec2005-09-06 04:43:02 +0000545SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000546 std::vector<SDOperand> Ops;
547 bool Changed = false;
548
Nate Begeman1d4d4142005-09-01 00:19:25 +0000549 // If the token factor has two operands and one is the entry token, replace
550 // the token factor with the other operand.
551 if (N->getNumOperands() == 2) {
552 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000553 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000555 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000556 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000557
Nate Begemanded49632005-10-13 03:11:28 +0000558 // fold (tokenfactor (tokenfactor)) -> tokenfactor
559 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
560 SDOperand Op = N->getOperand(i);
561 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
562 Changed = true;
563 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
564 Ops.push_back(Op.getOperand(j));
565 } else {
566 Ops.push_back(Op);
567 }
568 }
569 if (Changed)
570 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000571 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572}
573
Nate Begeman83e75ec2005-09-06 04:43:02 +0000574SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000575 SDOperand N0 = N->getOperand(0);
576 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000577 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
578 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000579 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000580
581 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000582 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000583 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000584 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000585 if (N0C && !N1C)
586 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000587 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000588 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000589 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000590 // fold (add (add x, c1), c2) -> (add x, c1+c2)
591 if (N1C && N0.getOpcode() == ISD::ADD) {
592 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
593 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
594 if (N00C)
595 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
596 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
597 if (N01C)
598 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
599 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
600 }
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000601
602 // fold ((c1-A)+c2) -> (c1+c2)-A
603 if (N1C && N0.getOpcode() == ISD::SUB)
604 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
605 return DAG.getNode(ISD::SUB, VT,
606 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
607 N0.getOperand(1));
608
Nate Begeman1d4d4142005-09-01 00:19:25 +0000609 // fold ((0-A) + B) -> B-A
610 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
611 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000612 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613 // fold (A + (0-B)) -> A-B
614 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
615 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000616 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000617 // fold (A+(B-A)) -> B
618 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000619 return N1.getOperand(0);
620 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000621}
622
Nate Begeman83e75ec2005-09-06 04:43:02 +0000623SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000624 SDOperand N0 = N->getOperand(0);
625 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000626 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
627 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000628 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000629
Chris Lattner854077d2005-10-17 01:07:11 +0000630 // fold (sub x, x) -> 0
631 if (N0 == N1)
632 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000633 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000634 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000635 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000636 // fold (sub x, c) -> (add x, -c)
637 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000638 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000639 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000640 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000641 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000642 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000643 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000644 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000645 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000646}
647
Nate Begeman83e75ec2005-09-06 04:43:02 +0000648SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000649 SDOperand N0 = N->getOperand(0);
650 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000651 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
652 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000653 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000654
655 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000656 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000657 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000658 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000659 if (N0C && !N1C)
660 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000661 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000662 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000663 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000664 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000665 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000666 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000667 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000668 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000669 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000670 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000671 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000672 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
673 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
674 // FIXME: If the input is something that is easily negated (e.g. a
675 // single-use add), we should put the negate there.
676 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
677 DAG.getNode(ISD::SHL, VT, N0,
678 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
679 TLI.getShiftAmountTy())));
680 }
681
682
Nate Begeman223df222005-09-08 20:18:10 +0000683 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
684 if (N1C && N0.getOpcode() == ISD::MUL) {
685 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
686 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
687 if (N00C)
688 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
689 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
690 if (N01C)
691 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
692 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
693 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000694 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000695}
696
Nate Begeman83e75ec2005-09-06 04:43:02 +0000697SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000698 SDOperand N0 = N->getOperand(0);
699 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000700 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
701 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000702 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000703
704 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000705 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000706 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000707 // fold (sdiv X, 1) -> X
708 if (N1C && N1C->getSignExtended() == 1LL)
709 return N0;
710 // fold (sdiv X, -1) -> 0-X
711 if (N1C && N1C->isAllOnesValue())
712 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000713 // If we know the sign bits of both operands are zero, strength reduce to a
714 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
715 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000716 if (TLI.MaskedValueIsZero(N1, SignBit) &&
717 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000718 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000719 // fold (sdiv X, pow2) -> (add (sra X, log(pow2)), (srl X, sizeof(X)-1))
720 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
721 (isPowerOf2_64(N1C->getSignExtended()) ||
722 isPowerOf2_64(-N1C->getSignExtended()))) {
723 // If dividing by powers of two is cheap, then don't perform the following
724 // fold.
725 if (TLI.isPow2DivCheap())
726 return SDOperand();
727 int64_t pow2 = N1C->getSignExtended();
728 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
729 SDOperand SRL = DAG.getNode(ISD::SRL, VT, N0,
730 DAG.getConstant(MVT::getSizeInBits(VT)-1,
731 TLI.getShiftAmountTy()));
732 WorkList.push_back(SRL.Val);
733 SDOperand SGN = DAG.getNode(ISD::ADD, VT, N0, SRL);
734 WorkList.push_back(SGN.Val);
735 SDOperand SRA = DAG.getNode(ISD::SRA, VT, SGN,
736 DAG.getConstant(Log2_64(abs2),
737 TLI.getShiftAmountTy()));
738 // If we're dividing by a positive value, we're done. Otherwise, we must
739 // negate the result.
740 if (pow2 > 0)
741 return SRA;
742 WorkList.push_back(SRA.Val);
743 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
744 }
Nate Begeman69575232005-10-20 02:15:44 +0000745 // if integer divide is expensive and we satisfy the requirements, emit an
746 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000747 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000748 !TLI.isIntDivCheap()) {
749 SDOperand Op = BuildSDIV(N);
750 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000751 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000752 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000753}
754
Nate Begeman83e75ec2005-09-06 04:43:02 +0000755SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000756 SDOperand N0 = N->getOperand(0);
757 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000758 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
759 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000760 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000761
762 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000763 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000764 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000766 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000767 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000768 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000769 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +0000770 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000771 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
772 SDOperand Op = BuildUDIV(N);
773 if (Op.Val) return Op;
774 }
775
Nate Begeman83e75ec2005-09-06 04:43:02 +0000776 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000777}
778
Nate Begeman83e75ec2005-09-06 04:43:02 +0000779SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000780 SDOperand N0 = N->getOperand(0);
781 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000782 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
783 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000784 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000785
786 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000787 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000788 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000789 // If we know the sign bits of both operands are zero, strength reduce to a
790 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
791 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000792 if (TLI.MaskedValueIsZero(N1, SignBit) &&
793 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000794 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000795 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000796}
797
Nate Begeman83e75ec2005-09-06 04:43:02 +0000798SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000799 SDOperand N0 = N->getOperand(0);
800 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000801 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
802 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000803 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000804
805 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000806 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000807 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000808 // fold (urem x, pow2) -> (and x, pow2-1)
809 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000810 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000811 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000812}
813
Nate Begeman83e75ec2005-09-06 04:43:02 +0000814SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000815 SDOperand N0 = N->getOperand(0);
816 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000817 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000818
819 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000820 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000821 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000822 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000823 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000824 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
825 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000826 TLI.getShiftAmountTy()));
827 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000828}
829
Nate Begeman83e75ec2005-09-06 04:43:02 +0000830SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000831 SDOperand N0 = N->getOperand(0);
832 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000833 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000834
835 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000836 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000837 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000840 return DAG.getConstant(0, N0.getValueType());
841 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842}
843
Nate Begeman83e75ec2005-09-06 04:43:02 +0000844SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000845 SDOperand N0 = N->getOperand(0);
846 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000847 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000848 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
849 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000850 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000851 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000852
853 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000854 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000855 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000856 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000857 if (N0C && !N1C)
858 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000859 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000860 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000861 return N0;
862 // if (and x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000863 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000864 return DAG.getConstant(0, VT);
865 // fold (and x, c) -> x iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000866 if (N1C &&
867 TLI.MaskedValueIsZero(N0, ~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000868 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000869 // fold (and (and x, c1), c2) -> (and x, c1^c2)
870 if (N1C && N0.getOpcode() == ISD::AND) {
871 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
872 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
873 if (N00C)
874 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
875 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
876 if (N01C)
877 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
878 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
879 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000880 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
Nate Begeman5dc7e862005-11-02 18:42:59 +0000881 if (N1C && N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882 unsigned ExtendBits =
Jeff Cohen06d9b4a2005-11-12 00:59:01 +0000883 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
884 if (ExtendBits == 64 || ((N1C->getValue() & (~0ULL << ExtendBits)) == 0))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000885 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000886 }
887 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000888 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000889 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000890 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000891 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000892 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
893 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
894 unsigned InBits = MVT::getSizeInBits(N0.getOperand(0).getValueType());
895 if (TLI.MaskedValueIsZero(N0.getOperand(0),
896 ~N1C->getValue() & ((1ULL << InBits)-1))) {
897 // We actually want to replace all uses of the any_extend with the
898 // zero_extend, to avoid duplicating things. This will later cause this
899 // AND to be folded.
900 CombineTo(N0.Val, DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
901 N0.getOperand(0)));
902 return SDOperand();
903 }
904 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000905 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
906 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
907 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
908 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
909
910 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
911 MVT::isInteger(LL.getValueType())) {
912 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
913 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
914 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
915 WorkList.push_back(ORNode.Val);
916 return DAG.getSetCC(VT, ORNode, LR, Op1);
917 }
918 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
919 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
920 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
921 WorkList.push_back(ANDNode.Val);
922 return DAG.getSetCC(VT, ANDNode, LR, Op1);
923 }
924 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
925 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
926 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
927 WorkList.push_back(ORNode.Val);
928 return DAG.getSetCC(VT, ORNode, LR, Op1);
929 }
930 }
931 // canonicalize equivalent to ll == rl
932 if (LL == RR && LR == RL) {
933 Op1 = ISD::getSetCCSwappedOperands(Op1);
934 std::swap(RL, RR);
935 }
936 if (LL == RL && LR == RR) {
937 bool isInteger = MVT::isInteger(LL.getValueType());
938 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
939 if (Result != ISD::SETCC_INVALID)
940 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
941 }
942 }
943 // fold (and (zext x), (zext y)) -> (zext (and x, y))
944 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
945 N1.getOpcode() == ISD::ZERO_EXTEND &&
946 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
947 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
948 N0.getOperand(0), N1.getOperand(0));
949 WorkList.push_back(ANDNode.Val);
950 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
951 }
Nate Begeman61af66e2006-01-28 01:06:30 +0000952 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +0000953 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +0000954 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
955 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +0000956 N0.getOperand(1) == N1.getOperand(1)) {
957 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
958 N0.getOperand(0), N1.getOperand(0));
959 WorkList.push_back(ANDNode.Val);
960 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
961 }
Chris Lattner85d63bb2005-10-15 22:18:08 +0000962 // fold (and (sra)) -> (and (srl)) when possible.
Nate Begeman5dc7e862005-11-02 18:42:59 +0000963 if (N0.getOpcode() == ISD::SRA && N0.Val->hasOneUse()) {
Chris Lattner85d63bb2005-10-15 22:18:08 +0000964 if (ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
965 // If the RHS of the AND has zeros where the sign bits of the SRA will
966 // land, turn the SRA into an SRL.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000967 if (TLI.MaskedValueIsZero(N1, (~0ULL << (OpSizeInBits-N01C->getValue())) &
968 (~0ULL>>(64-OpSizeInBits)))) {
Chris Lattner85d63bb2005-10-15 22:18:08 +0000969 WorkList.push_back(N);
970 CombineTo(N0.Val, DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
971 N0.getOperand(1)));
972 return SDOperand();
973 }
974 }
Nate Begeman5dc7e862005-11-02 18:42:59 +0000975 }
Nate Begemanded49632005-10-13 03:11:28 +0000976 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +0000977 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +0000978 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000979 // If we zero all the possible extended bits, then we can turn this into
980 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000981 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +0000982 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000983 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
984 N0.getOperand(1), N0.getOperand(2),
985 EVT);
Nate Begemanded49632005-10-13 03:11:28 +0000986 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +0000987 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +0000988 return SDOperand();
989 }
990 }
991 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +0000992 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +0000993 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +0000994 // If we zero all the possible extended bits, then we can turn this into
995 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000996 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +0000997 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +0000998 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
999 N0.getOperand(1), N0.getOperand(2),
1000 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001001 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001002 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001003 return SDOperand();
1004 }
1005 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001006 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001007}
1008
Nate Begeman83e75ec2005-09-06 04:43:02 +00001009SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001010 SDOperand N0 = N->getOperand(0);
1011 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001012 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001013 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1014 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001015 MVT::ValueType VT = N1.getValueType();
1016 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017
1018 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001019 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001020 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001021 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001022 if (N0C && !N1C)
1023 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001024 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001025 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001026 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001027 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001028 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001029 return N1;
1030 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001031 if (N1C &&
1032 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001033 return N1;
Nate Begeman223df222005-09-08 20:18:10 +00001034 // fold (or (or x, c1), c2) -> (or x, c1|c2)
1035 if (N1C && N0.getOpcode() == ISD::OR) {
1036 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1037 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1038 if (N00C)
1039 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
1040 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
1041 if (N01C)
1042 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1043 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
Chris Lattner731d3482005-10-27 05:06:38 +00001044 } else if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
1045 isa<ConstantSDNode>(N0.getOperand(1))) {
1046 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1047 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1048 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1049 N1),
1050 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001051 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001052 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1053 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1054 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1055 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1056
1057 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1058 MVT::isInteger(LL.getValueType())) {
1059 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1060 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1061 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1062 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1063 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1064 WorkList.push_back(ORNode.Val);
1065 return DAG.getSetCC(VT, ORNode, LR, Op1);
1066 }
1067 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1068 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1069 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1070 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1071 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1072 WorkList.push_back(ANDNode.Val);
1073 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1074 }
1075 }
1076 // canonicalize equivalent to ll == rl
1077 if (LL == RR && LR == RL) {
1078 Op1 = ISD::getSetCCSwappedOperands(Op1);
1079 std::swap(RL, RR);
1080 }
1081 if (LL == RL && LR == RR) {
1082 bool isInteger = MVT::isInteger(LL.getValueType());
1083 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1084 if (Result != ISD::SETCC_INVALID)
1085 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1086 }
1087 }
1088 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1089 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1090 N1.getOpcode() == ISD::ZERO_EXTEND &&
1091 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1092 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1093 N0.getOperand(0), N1.getOperand(0));
1094 WorkList.push_back(ORNode.Val);
1095 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1096 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001097 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1098 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1099 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1100 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1101 N0.getOperand(1) == N1.getOperand(1)) {
1102 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1103 N0.getOperand(0), N1.getOperand(0));
1104 WorkList.push_back(ORNode.Val);
1105 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1106 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001107 // canonicalize shl to left side in a shl/srl pair, to match rotate
1108 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1109 std::swap(N0, N1);
1110 // check for rotl, rotr
1111 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1112 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001113 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001114 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1115 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1116 N1.getOperand(1).getOpcode() == ISD::Constant) {
1117 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1118 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1119 if ((c1val + c2val) == OpSizeInBits)
1120 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1121 }
1122 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1123 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1124 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1125 if (ConstantSDNode *SUBC =
1126 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1127 if (SUBC->getValue() == OpSizeInBits)
1128 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1129 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1130 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1131 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1132 if (ConstantSDNode *SUBC =
1133 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1134 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001135 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001136 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1137 N1.getOperand(1));
1138 else
1139 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1140 N0.getOperand(1));
1141 }
1142 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001143 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001144}
1145
Nate Begeman83e75ec2005-09-06 04:43:02 +00001146SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001147 SDOperand N0 = N->getOperand(0);
1148 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001149 SDOperand LHS, RHS, CC;
1150 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1151 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001152 MVT::ValueType VT = N0.getValueType();
1153
1154 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001155 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001156 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001157 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001158 if (N0C && !N1C)
1159 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001161 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001162 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001164 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1165 bool isInt = MVT::isInteger(LHS.getValueType());
1166 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1167 isInt);
1168 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001169 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001170 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001171 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001172 assert(0 && "Unhandled SetCC Equivalent!");
1173 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001174 }
Nate Begeman99801192005-09-07 23:25:52 +00001175 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1176 if (N1C && N1C->getValue() == 1 &&
1177 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001179 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1180 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001181 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1182 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001183 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1184 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001185 }
1186 }
Nate Begeman99801192005-09-07 23:25:52 +00001187 // fold !(x or y) -> (!x and !y) iff x or y are constants
1188 if (N1C && N1C->isAllOnesValue() &&
1189 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001190 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001191 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1192 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001193 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1194 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001195 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1196 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001197 }
1198 }
Nate Begeman223df222005-09-08 20:18:10 +00001199 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1200 if (N1C && N0.getOpcode() == ISD::XOR) {
1201 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1202 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1203 if (N00C)
1204 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1205 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1206 if (N01C)
1207 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1208 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1209 }
1210 // fold (xor x, x) -> 0
1211 if (N0 == N1)
1212 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001213 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1214 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1215 N1.getOpcode() == ISD::ZERO_EXTEND &&
1216 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1217 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1218 N0.getOperand(0), N1.getOperand(0));
1219 WorkList.push_back(XORNode.Val);
1220 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1221 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001222 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1223 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1224 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1225 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1226 N0.getOperand(1) == N1.getOperand(1)) {
1227 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1228 N0.getOperand(0), N1.getOperand(0));
1229 WorkList.push_back(XORNode.Val);
1230 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1231 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001232 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001233}
1234
Nate Begeman83e75ec2005-09-06 04:43:02 +00001235SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001236 SDOperand N0 = N->getOperand(0);
1237 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001238 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1239 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001240 MVT::ValueType VT = N0.getValueType();
1241 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1242
1243 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001244 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001245 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001246 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001247 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001248 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001249 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001250 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001251 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001252 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001253 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001254 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001255 // if (shl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001256 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001257 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001258 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001259 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001260 N0.getOperand(1).getOpcode() == ISD::Constant) {
1261 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001262 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001263 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001264 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001265 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001266 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001267 }
1268 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1269 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001270 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001271 N0.getOperand(1).getOpcode() == ISD::Constant) {
1272 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001273 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001274 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1275 DAG.getConstant(~0ULL << c1, VT));
1276 if (c2 > c1)
1277 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001278 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001279 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001280 return DAG.getNode(ISD::SRL, VT, Mask,
1281 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001282 }
1283 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001284 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001285 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001286 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1287 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001288}
1289
Nate Begeman83e75ec2005-09-06 04:43:02 +00001290SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001291 SDOperand N0 = N->getOperand(0);
1292 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001293 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1294 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001295 MVT::ValueType VT = N0.getValueType();
1296 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1297
1298 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001299 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001300 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001301 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001302 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001303 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001304 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001305 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001306 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001307 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001308 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001309 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001310 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001311 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001312 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313 // If the sign bit is known to be zero, switch this to a SRL.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001314 if (TLI.MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001315 return DAG.getNode(ISD::SRL, VT, N0, N1);
1316 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001317}
1318
Nate Begeman83e75ec2005-09-06 04:43:02 +00001319SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001320 SDOperand N0 = N->getOperand(0);
1321 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001322 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1323 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001324 MVT::ValueType VT = N0.getValueType();
1325 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1326
1327 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001328 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001329 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001331 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001332 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001334 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001335 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001336 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001337 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001338 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001339 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001340 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001341 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001343 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001344 N0.getOperand(1).getOpcode() == ISD::Constant) {
1345 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001346 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001348 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001350 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001351 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001353}
1354
Nate Begeman83e75ec2005-09-06 04:43:02 +00001355SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001357 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001358 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001359
1360 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001361 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001362 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364}
1365
Nate Begeman83e75ec2005-09-06 04:43:02 +00001366SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001369 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370
1371 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001372 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001373 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001374 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375}
1376
Nate Begeman83e75ec2005-09-06 04:43:02 +00001377SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001379 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001380 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381
1382 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001383 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001384 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001385 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001386}
1387
Nate Begeman452d7be2005-09-16 00:54:12 +00001388SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1389 SDOperand N0 = N->getOperand(0);
1390 SDOperand N1 = N->getOperand(1);
1391 SDOperand N2 = N->getOperand(2);
1392 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1393 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1394 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1395 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001396
Nate Begeman452d7be2005-09-16 00:54:12 +00001397 // fold select C, X, X -> X
1398 if (N1 == N2)
1399 return N1;
1400 // fold select true, X, Y -> X
1401 if (N0C && !N0C->isNullValue())
1402 return N1;
1403 // fold select false, X, Y -> Y
1404 if (N0C && N0C->isNullValue())
1405 return N2;
1406 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001407 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001408 return DAG.getNode(ISD::OR, VT, N0, N2);
1409 // fold select C, 0, X -> ~C & X
1410 // FIXME: this should check for C type == X type, not i1?
1411 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1412 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1413 WorkList.push_back(XORNode.Val);
1414 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1415 }
1416 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001417 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001418 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1419 WorkList.push_back(XORNode.Val);
1420 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1421 }
1422 // fold select C, X, 0 -> C & X
1423 // FIXME: this should check for C type == X type, not i1?
1424 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1425 return DAG.getNode(ISD::AND, VT, N0, N1);
1426 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1427 if (MVT::i1 == VT && N0 == N1)
1428 return DAG.getNode(ISD::OR, VT, N0, N2);
1429 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1430 if (MVT::i1 == VT && N0 == N2)
1431 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001432 // If we can fold this based on the true/false value, do so.
1433 if (SimplifySelectOps(N, N1, N2))
1434 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001435 // fold selects based on a setcc into other things, such as min/max/abs
1436 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001437 // FIXME:
1438 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1439 // having to say they don't support SELECT_CC on every type the DAG knows
1440 // about, since there is no way to mark an opcode illegal at all value types
1441 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1442 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1443 N1, N2, N0.getOperand(2));
1444 else
1445 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001446 return SDOperand();
1447}
1448
1449SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001450 SDOperand N0 = N->getOperand(0);
1451 SDOperand N1 = N->getOperand(1);
1452 SDOperand N2 = N->getOperand(2);
1453 SDOperand N3 = N->getOperand(3);
1454 SDOperand N4 = N->getOperand(4);
1455 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1456 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1457 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1458 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1459
1460 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001461 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001462 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1463
Nate Begeman44728a72005-09-19 22:34:01 +00001464 // fold select_cc lhs, rhs, x, x, cc -> x
1465 if (N2 == N3)
1466 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001467
1468 // If we can fold this based on the true/false value, do so.
1469 if (SimplifySelectOps(N, N2, N3))
1470 return SDOperand();
1471
Nate Begeman44728a72005-09-19 22:34:01 +00001472 // fold select_cc into other things, such as min/max/abs
1473 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001474}
1475
1476SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1477 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1478 cast<CondCodeSDNode>(N->getOperand(2))->get());
1479}
1480
Nate Begeman5054f162005-10-14 01:12:21 +00001481SDOperand DAGCombiner::visitADD_PARTS(SDNode *N) {
1482 SDOperand LHSLo = N->getOperand(0);
1483 SDOperand RHSLo = N->getOperand(2);
1484 MVT::ValueType VT = LHSLo.getValueType();
1485
1486 // fold (a_Hi, 0) + (b_Hi, b_Lo) -> (b_Hi + a_Hi, b_Lo)
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001487 if (TLI.MaskedValueIsZero(LHSLo, (1ULL << MVT::getSizeInBits(VT))-1)) {
Nate Begeman5054f162005-10-14 01:12:21 +00001488 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1489 N->getOperand(3));
1490 WorkList.push_back(Hi.Val);
1491 CombineTo(N, RHSLo, Hi);
1492 return SDOperand();
1493 }
1494 // fold (a_Hi, a_Lo) + (b_Hi, 0) -> (a_Hi + b_Hi, a_Lo)
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001495 if (TLI.MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1)) {
Nate Begeman5054f162005-10-14 01:12:21 +00001496 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1497 N->getOperand(3));
1498 WorkList.push_back(Hi.Val);
1499 CombineTo(N, LHSLo, Hi);
1500 return SDOperand();
1501 }
1502 return SDOperand();
1503}
1504
1505SDOperand DAGCombiner::visitSUB_PARTS(SDNode *N) {
1506 SDOperand LHSLo = N->getOperand(0);
1507 SDOperand RHSLo = N->getOperand(2);
1508 MVT::ValueType VT = LHSLo.getValueType();
1509
1510 // fold (a_Hi, a_Lo) - (b_Hi, 0) -> (a_Hi - b_Hi, a_Lo)
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001511 if (TLI.MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1)) {
Nate Begeman5054f162005-10-14 01:12:21 +00001512 SDOperand Hi = DAG.getNode(ISD::SUB, VT, N->getOperand(1),
1513 N->getOperand(3));
1514 WorkList.push_back(Hi.Val);
1515 CombineTo(N, LHSLo, Hi);
1516 return SDOperand();
1517 }
1518 return SDOperand();
1519}
1520
Nate Begeman83e75ec2005-09-06 04:43:02 +00001521SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001522 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001523 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001524 MVT::ValueType VT = N->getValueType(0);
1525
Nate Begeman1d4d4142005-09-01 00:19:25 +00001526 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001527 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001528 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001529 // fold (sext (sext x)) -> (sext x)
1530 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001531 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001532 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001533 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1534 (!AfterLegalize ||
1535 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001536 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1537 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001538 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001539 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1540 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001541 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1542 N0.getOperand(1), N0.getOperand(2),
1543 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001544 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001545 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1546 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001547 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001548 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001549
1550 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1551 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1552 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1553 N0.hasOneUse()) {
1554 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1555 N0.getOperand(1), N0.getOperand(2),
1556 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001557 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001558 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1559 ExtLoad.getValue(1));
1560 return SDOperand();
1561 }
1562
Nate Begeman83e75ec2005-09-06 04:43:02 +00001563 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001564}
1565
Nate Begeman83e75ec2005-09-06 04:43:02 +00001566SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001568 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001569 MVT::ValueType VT = N->getValueType(0);
1570
Nate Begeman1d4d4142005-09-01 00:19:25 +00001571 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001572 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001573 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001574 // fold (zext (zext x)) -> (zext x)
1575 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001576 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001577 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1578 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001579 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001580 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001581 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001582 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1583 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001584 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1585 N0.getOperand(1), N0.getOperand(2),
1586 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001587 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001588 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1589 ExtLoad.getValue(1));
1590 return SDOperand();
1591 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001592
1593 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1594 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1595 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1596 N0.hasOneUse()) {
1597 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1598 N0.getOperand(1), N0.getOperand(2),
1599 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001600 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001601 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1602 ExtLoad.getValue(1));
1603 return SDOperand();
1604 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001605 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001606}
1607
Nate Begeman83e75ec2005-09-06 04:43:02 +00001608SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001609 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001610 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001611 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001612 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001613 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001614 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001615
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001617 if (N0C) {
1618 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001620 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001621 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001623 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001624 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001626 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1627 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1628 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001629 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001630 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001631 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1632 if (N0.getOpcode() == ISD::AssertSext &&
1633 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001634 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001635 }
1636 // fold (sext_in_reg (sextload x)) -> (sextload x)
1637 if (N0.getOpcode() == ISD::SEXTLOAD &&
1638 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001639 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001640 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001641 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001642 if (N0.getOpcode() == ISD::SETCC &&
1643 TLI.getSetCCResultContents() ==
1644 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001645 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001646 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001647 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begeman07ed4172005-10-10 21:26:48 +00001648 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
1649 DAG.getConstant(~0ULL >> (64-EVTBits), VT));
1650 // fold (sext_in_reg (srl x)) -> sra x
1651 if (N0.getOpcode() == ISD::SRL &&
1652 N0.getOperand(1).getOpcode() == ISD::Constant &&
1653 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1654 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1655 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001656 }
Nate Begemanded49632005-10-13 03:11:28 +00001657 // fold (sext_inreg (extload x)) -> (sextload x)
1658 if (N0.getOpcode() == ISD::EXTLOAD &&
1659 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001660 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001661 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1662 N0.getOperand(1), N0.getOperand(2),
1663 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001664 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001665 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001666 return SDOperand();
1667 }
1668 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001669 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001670 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001671 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001672 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1673 N0.getOperand(1), N0.getOperand(2),
1674 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001675 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001676 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001677 return SDOperand();
1678 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001679 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001680}
1681
Nate Begeman83e75ec2005-09-06 04:43:02 +00001682SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001683 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001684 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001685 MVT::ValueType VT = N->getValueType(0);
1686
1687 // noop truncate
1688 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001689 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001690 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001691 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001692 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001693 // fold (truncate (truncate x)) -> (truncate x)
1694 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001695 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001696 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1697 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1698 if (N0.getValueType() < VT)
1699 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001700 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001701 else if (N0.getValueType() > VT)
1702 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001703 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001704 else
1705 // if the source and dest are the same type, we can drop both the extend
1706 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001707 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001708 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001709 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001710 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001711 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1712 "Cannot truncate to larger type!");
1713 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001714 // For big endian targets, we need to add an offset to the pointer to load
1715 // the correct bytes. For little endian systems, we merely need to read
1716 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001717 uint64_t PtrOff =
1718 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001719 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1720 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1721 DAG.getConstant(PtrOff, PtrType));
1722 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001723 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001724 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001725 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001726 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001727 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001728 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001729}
1730
Chris Lattner94683772005-12-23 05:30:37 +00001731SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1732 SDOperand N0 = N->getOperand(0);
1733 MVT::ValueType VT = N->getValueType(0);
1734
1735 // If the input is a constant, let getNode() fold it.
1736 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1737 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1738 if (Res.Val != N) return Res;
1739 }
1740
Chris Lattnerc8547d82005-12-23 05:37:50 +00001741 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1742 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1743
Chris Lattner57104102005-12-23 05:44:41 +00001744 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001745 // FIXME: These xforms need to know that the resultant load doesn't need a
1746 // higher alignment than the original!
1747 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001748 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1749 N0.getOperand(2));
1750 WorkList.push_back(N);
1751 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1752 Load.getValue(1));
1753 return Load;
1754 }
1755
Chris Lattner94683772005-12-23 05:30:37 +00001756 return SDOperand();
1757}
1758
Chris Lattner01b3d732005-09-28 22:28:18 +00001759SDOperand DAGCombiner::visitFADD(SDNode *N) {
1760 SDOperand N0 = N->getOperand(0);
1761 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001762 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1763 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001764 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001765
1766 // fold (fadd c1, c2) -> c1+c2
1767 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001768 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001769 // canonicalize constant to RHS
1770 if (N0CFP && !N1CFP)
1771 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001772 // fold (A + (-B)) -> A-B
1773 if (N1.getOpcode() == ISD::FNEG)
1774 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001775 // fold ((-A) + B) -> B-A
1776 if (N0.getOpcode() == ISD::FNEG)
1777 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001778 return SDOperand();
1779}
1780
1781SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1782 SDOperand N0 = N->getOperand(0);
1783 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001784 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1785 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001786 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001787
1788 // fold (fsub c1, c2) -> c1-c2
1789 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001790 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001791 // fold (A-(-B)) -> A+B
1792 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001793 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001794 return SDOperand();
1795}
1796
1797SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1798 SDOperand N0 = N->getOperand(0);
1799 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001800 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1801 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001802 MVT::ValueType VT = N->getValueType(0);
1803
Nate Begeman11af4ea2005-10-17 20:40:11 +00001804 // fold (fmul c1, c2) -> c1*c2
1805 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001806 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001807 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001808 if (N0CFP && !N1CFP)
1809 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001810 // fold (fmul X, 2.0) -> (fadd X, X)
1811 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1812 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001813 return SDOperand();
1814}
1815
1816SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1817 SDOperand N0 = N->getOperand(0);
1818 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001819 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1820 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001821 MVT::ValueType VT = N->getValueType(0);
1822
Nate Begemana148d982006-01-18 22:35:16 +00001823 // fold (fdiv c1, c2) -> c1/c2
1824 if (N0CFP && N1CFP)
1825 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001826 return SDOperand();
1827}
1828
1829SDOperand DAGCombiner::visitFREM(SDNode *N) {
1830 SDOperand N0 = N->getOperand(0);
1831 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001832 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1833 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001834 MVT::ValueType VT = N->getValueType(0);
1835
Nate Begemana148d982006-01-18 22:35:16 +00001836 // fold (frem c1, c2) -> fmod(c1,c2)
1837 if (N0CFP && N1CFP)
1838 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001839 return SDOperand();
1840}
1841
1842
Nate Begeman83e75ec2005-09-06 04:43:02 +00001843SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001844 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001845 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001846 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001847
1848 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001849 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001850 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001851 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001852}
1853
Nate Begeman83e75ec2005-09-06 04:43:02 +00001854SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001855 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001856 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001857 MVT::ValueType VT = N->getValueType(0);
1858
Nate Begeman1d4d4142005-09-01 00:19:25 +00001859 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001860 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001861 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001862 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001863}
1864
Nate Begeman83e75ec2005-09-06 04:43:02 +00001865SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001866 SDOperand N0 = N->getOperand(0);
1867 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1868 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001869
1870 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001871 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001872 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001873 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001874}
1875
Nate Begeman83e75ec2005-09-06 04:43:02 +00001876SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001877 SDOperand N0 = N->getOperand(0);
1878 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1879 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001880
1881 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001882 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001883 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001884 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001885}
1886
Nate Begeman83e75ec2005-09-06 04:43:02 +00001887SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001888 SDOperand N0 = N->getOperand(0);
1889 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1890 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001891
1892 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001893 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001894 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001895 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001896}
1897
Nate Begeman83e75ec2005-09-06 04:43:02 +00001898SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001899 SDOperand N0 = N->getOperand(0);
1900 MVT::ValueType VT = N->getValueType(0);
1901 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001902 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001903
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001905 if (N0CFP) {
1906 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001907 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001908 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001909 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001910}
1911
Nate Begeman83e75ec2005-09-06 04:43:02 +00001912SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001913 SDOperand N0 = N->getOperand(0);
1914 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1915 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001916
1917 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001918 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001919 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001920 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001921}
1922
Nate Begeman83e75ec2005-09-06 04:43:02 +00001923SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001924 SDOperand N0 = N->getOperand(0);
1925 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1926 MVT::ValueType VT = N->getValueType(0);
1927
1928 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001929 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001930 return DAG.getNode(ISD::FNEG, VT, N0);
1931 // fold (fneg (sub x, y)) -> (sub y, x)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001932 if (N->getOperand(0).getOpcode() == ISD::SUB)
Nate Begemana148d982006-01-18 22:35:16 +00001933 return DAG.getNode(ISD::SUB, VT, N->getOperand(1), N->getOperand(0));
1934 // fold (fneg (fneg x)) -> x
Nate Begeman1d4d4142005-09-01 00:19:25 +00001935 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001936 return N->getOperand(0).getOperand(0);
1937 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001938}
1939
Nate Begeman83e75ec2005-09-06 04:43:02 +00001940SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001941 SDOperand N0 = N->getOperand(0);
1942 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1943 MVT::ValueType VT = N->getValueType(0);
1944
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001946 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001947 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001948 // fold (fabs (fabs x)) -> (fabs x)
1949 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001950 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001951 // fold (fabs (fneg x)) -> (fabs x)
1952 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001953 return DAG.getNode(ISD::FABS, VT, N->getOperand(0).getOperand(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +00001954 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001955}
1956
Nate Begeman44728a72005-09-19 22:34:01 +00001957SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1958 SDOperand Chain = N->getOperand(0);
1959 SDOperand N1 = N->getOperand(1);
1960 SDOperand N2 = N->getOperand(2);
1961 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1962
1963 // never taken branch, fold to chain
1964 if (N1C && N1C->isNullValue())
1965 return Chain;
1966 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001967 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001968 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001969 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
1970 // on the target.
1971 if (N1.getOpcode() == ISD::SETCC &&
1972 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
1973 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
1974 N1.getOperand(0), N1.getOperand(1), N2);
1975 }
Nate Begeman44728a72005-09-19 22:34:01 +00001976 return SDOperand();
1977}
1978
1979SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1980 SDOperand Chain = N->getOperand(0);
1981 SDOperand N1 = N->getOperand(1);
1982 SDOperand N2 = N->getOperand(2);
1983 SDOperand N3 = N->getOperand(3);
1984 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1985
1986 // unconditional branch to true mbb
1987 if (N1C && N1C->getValue() == 1)
1988 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1989 // unconditional branch to false mbb
1990 if (N1C && N1C->isNullValue())
1991 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001992 // fold a brcondtwoway with a setcc condition into a BRTWOWAY_CC node if
1993 // BRTWOWAY_CC is legal on the target.
1994 if (N1.getOpcode() == ISD::SETCC &&
1995 TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
1996 std::vector<SDOperand> Ops;
1997 Ops.push_back(Chain);
1998 Ops.push_back(N1.getOperand(2));
1999 Ops.push_back(N1.getOperand(0));
2000 Ops.push_back(N1.getOperand(1));
2001 Ops.push_back(N2);
2002 Ops.push_back(N3);
2003 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2004 }
Nate Begeman44728a72005-09-19 22:34:01 +00002005 return SDOperand();
2006}
2007
Chris Lattner3ea0b472005-10-05 06:47:48 +00002008// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2009//
Nate Begeman44728a72005-09-19 22:34:01 +00002010SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002011 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2012 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2013
2014 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002015 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2016 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2017
2018 // fold br_cc true, dest -> br dest (unconditional branch)
2019 if (SCCC && SCCC->getValue())
2020 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2021 N->getOperand(4));
2022 // fold br_cc false, dest -> unconditional fall through
2023 if (SCCC && SCCC->isNullValue())
2024 return N->getOperand(0);
2025 // fold to a simpler setcc
2026 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2027 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2028 Simp.getOperand(2), Simp.getOperand(0),
2029 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002030 return SDOperand();
2031}
2032
2033SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00002034 SDOperand Chain = N->getOperand(0);
2035 SDOperand CCN = N->getOperand(1);
2036 SDOperand LHS = N->getOperand(2);
2037 SDOperand RHS = N->getOperand(3);
2038 SDOperand N4 = N->getOperand(4);
2039 SDOperand N5 = N->getOperand(5);
2040
2041 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
2042 cast<CondCodeSDNode>(CCN)->get(), false);
2043 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2044
2045 // fold select_cc lhs, rhs, x, x, cc -> x
2046 if (N4 == N5)
2047 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2048 // fold select_cc true, x, y -> x
2049 if (SCCC && SCCC->getValue())
2050 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2051 // fold select_cc false, x, y -> y
2052 if (SCCC && SCCC->isNullValue())
2053 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
2054 // fold to a simpler setcc
Chris Lattner03d5e872006-01-29 06:00:45 +00002055 if (SCC.Val && SCC.getOpcode() == ISD::SETCC) {
2056 std::vector<SDOperand> Ops;
2057 Ops.push_back(Chain);
2058 Ops.push_back(SCC.getOperand(2));
2059 Ops.push_back(SCC.getOperand(0));
2060 Ops.push_back(SCC.getOperand(1));
2061 Ops.push_back(N4);
2062 Ops.push_back(N5);
2063 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2064 }
Nate Begeman44728a72005-09-19 22:34:01 +00002065 return SDOperand();
2066}
2067
Chris Lattner01a22022005-10-10 22:04:48 +00002068SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2069 SDOperand Chain = N->getOperand(0);
2070 SDOperand Ptr = N->getOperand(1);
2071 SDOperand SrcValue = N->getOperand(2);
2072
2073 // If this load is directly stored, replace the load value with the stored
2074 // value.
2075 // TODO: Handle store large -> read small portion.
2076 // TODO: Handle TRUNCSTORE/EXTLOAD
2077 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2078 Chain.getOperand(1).getValueType() == N->getValueType(0))
2079 return CombineTo(N, Chain.getOperand(1), Chain);
2080
2081 return SDOperand();
2082}
2083
Chris Lattner87514ca2005-10-10 22:31:19 +00002084SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2085 SDOperand Chain = N->getOperand(0);
2086 SDOperand Value = N->getOperand(1);
2087 SDOperand Ptr = N->getOperand(2);
2088 SDOperand SrcValue = N->getOperand(3);
2089
2090 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002091 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002092 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2093 // Make sure that these stores are the same value type:
2094 // FIXME: we really care that the second store is >= size of the first.
2095 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002096 // Create a new store of Value that replaces both stores.
2097 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002098 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2099 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002100 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2101 PrevStore->getOperand(0), Value, Ptr,
2102 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002103 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002104 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002105 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002106 }
2107
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002108 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002109 // FIXME: This needs to know that the resultant store does not need a
2110 // higher alignment than the original.
2111 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002112 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2113 Ptr, SrcValue);
2114
Chris Lattner87514ca2005-10-10 22:31:19 +00002115 return SDOperand();
2116}
2117
Jim Laskeyd6e8d412005-12-23 20:08:28 +00002118SDOperand DAGCombiner::visitLOCATION(SDNode *N) {
2119 SDOperand Chain = N->getOperand(0);
2120
2121 // Remove redundant locations (last one holds)
2122 if (Chain.getOpcode() == ISD::LOCATION && Chain.hasOneUse()) {
2123 return DAG.getNode(ISD::LOCATION, MVT::Other, Chain.getOperand(0),
2124 N->getOperand(1),
2125 N->getOperand(2),
2126 N->getOperand(3),
2127 N->getOperand(4));
2128 }
2129
2130 return SDOperand();
2131}
2132
2133SDOperand DAGCombiner::visitDEBUGLOC(SDNode *N) {
2134 SDOperand Chain = N->getOperand(0);
2135
2136 // Remove redundant debug locations (last one holds)
2137 if (Chain.getOpcode() == ISD::DEBUG_LOC && Chain.hasOneUse()) {
2138 return DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Chain.getOperand(0),
2139 N->getOperand(1),
2140 N->getOperand(2),
Jim Laskeyabf6d172006-01-05 01:25:28 +00002141 N->getOperand(3));
Jim Laskeyd6e8d412005-12-23 20:08:28 +00002142 }
2143
2144 return SDOperand();
2145}
2146
Nate Begeman44728a72005-09-19 22:34:01 +00002147SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002148 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2149
2150 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2151 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2152 // If we got a simplified select_cc node back from SimplifySelectCC, then
2153 // break it down into a new SETCC node, and a new SELECT node, and then return
2154 // the SELECT node, since we were called with a SELECT node.
2155 if (SCC.Val) {
2156 // Check to see if we got a select_cc back (to turn into setcc/select).
2157 // Otherwise, just return whatever node we got back, like fabs.
2158 if (SCC.getOpcode() == ISD::SELECT_CC) {
2159 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2160 SCC.getOperand(0), SCC.getOperand(1),
2161 SCC.getOperand(4));
2162 WorkList.push_back(SETCC.Val);
2163 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2164 SCC.getOperand(3), SETCC);
2165 }
2166 return SCC;
2167 }
Nate Begeman44728a72005-09-19 22:34:01 +00002168 return SDOperand();
2169}
2170
Chris Lattner40c62d52005-10-18 06:04:22 +00002171/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2172/// are the two values being selected between, see if we can simplify the
2173/// select.
2174///
2175bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2176 SDOperand RHS) {
2177
2178 // If this is a select from two identical things, try to pull the operation
2179 // through the select.
2180 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2181#if 0
2182 std::cerr << "SELECT: ["; LHS.Val->dump();
2183 std::cerr << "] ["; RHS.Val->dump();
2184 std::cerr << "]\n";
2185#endif
2186
2187 // If this is a load and the token chain is identical, replace the select
2188 // of two loads with a load through a select of the address to load from.
2189 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2190 // constants have been dropped into the constant pool.
2191 if ((LHS.getOpcode() == ISD::LOAD ||
2192 LHS.getOpcode() == ISD::EXTLOAD ||
2193 LHS.getOpcode() == ISD::ZEXTLOAD ||
2194 LHS.getOpcode() == ISD::SEXTLOAD) &&
2195 // Token chains must be identical.
2196 LHS.getOperand(0) == RHS.getOperand(0) &&
2197 // If this is an EXTLOAD, the VT's must match.
2198 (LHS.getOpcode() == ISD::LOAD ||
2199 LHS.getOperand(3) == RHS.getOperand(3))) {
2200 // FIXME: this conflates two src values, discarding one. This is not
2201 // the right thing to do, but nothing uses srcvalues now. When they do,
2202 // turn SrcValue into a list of locations.
2203 SDOperand Addr;
2204 if (TheSelect->getOpcode() == ISD::SELECT)
2205 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2206 TheSelect->getOperand(0), LHS.getOperand(1),
2207 RHS.getOperand(1));
2208 else
2209 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2210 TheSelect->getOperand(0),
2211 TheSelect->getOperand(1),
2212 LHS.getOperand(1), RHS.getOperand(1),
2213 TheSelect->getOperand(4));
2214
2215 SDOperand Load;
2216 if (LHS.getOpcode() == ISD::LOAD)
2217 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2218 Addr, LHS.getOperand(2));
2219 else
2220 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2221 LHS.getOperand(0), Addr, LHS.getOperand(2),
2222 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2223 // Users of the select now use the result of the load.
2224 CombineTo(TheSelect, Load);
2225
2226 // Users of the old loads now use the new load's chain. We know the
2227 // old-load value is dead now.
2228 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2229 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2230 return true;
2231 }
2232 }
2233
2234 return false;
2235}
2236
Nate Begeman44728a72005-09-19 22:34:01 +00002237SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2238 SDOperand N2, SDOperand N3,
2239 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002240
2241 MVT::ValueType VT = N2.getValueType();
2242 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2243 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2244 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2245 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2246
2247 // Determine if the condition we're dealing with is constant
2248 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2249 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2250
2251 // fold select_cc true, x, y -> x
2252 if (SCCC && SCCC->getValue())
2253 return N2;
2254 // fold select_cc false, x, y -> y
2255 if (SCCC && SCCC->getValue() == 0)
2256 return N3;
2257
2258 // Check to see if we can simplify the select into an fabs node
2259 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2260 // Allow either -0.0 or 0.0
2261 if (CFP->getValue() == 0.0) {
2262 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2263 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2264 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2265 N2 == N3.getOperand(0))
2266 return DAG.getNode(ISD::FABS, VT, N0);
2267
2268 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2269 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2270 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2271 N2.getOperand(0) == N3)
2272 return DAG.getNode(ISD::FABS, VT, N3);
2273 }
2274 }
2275
2276 // Check to see if we can perform the "gzip trick", transforming
2277 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2278 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2279 MVT::isInteger(N0.getValueType()) &&
2280 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2281 MVT::ValueType XType = N0.getValueType();
2282 MVT::ValueType AType = N2.getValueType();
2283 if (XType >= AType) {
2284 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002285 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002286 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2287 unsigned ShCtV = Log2_64(N2C->getValue());
2288 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2289 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2290 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
2291 WorkList.push_back(Shift.Val);
2292 if (XType > AType) {
2293 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2294 WorkList.push_back(Shift.Val);
2295 }
2296 return DAG.getNode(ISD::AND, AType, Shift, N2);
2297 }
2298 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2299 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2300 TLI.getShiftAmountTy()));
2301 WorkList.push_back(Shift.Val);
2302 if (XType > AType) {
2303 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2304 WorkList.push_back(Shift.Val);
2305 }
2306 return DAG.getNode(ISD::AND, AType, Shift, N2);
2307 }
2308 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002309
2310 // fold select C, 16, 0 -> shl C, 4
2311 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2312 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2313 // Get a SetCC of the condition
2314 // FIXME: Should probably make sure that setcc is legal if we ever have a
2315 // target where it isn't.
2316 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2317 WorkList.push_back(SCC.Val);
2318 // cast from setcc result type to select result type
2319 if (AfterLegalize)
2320 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
2321 else
2322 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
2323 WorkList.push_back(Temp.Val);
2324 // shl setcc result by log2 n2c
2325 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2326 DAG.getConstant(Log2_64(N2C->getValue()),
2327 TLI.getShiftAmountTy()));
2328 }
2329
Nate Begemanf845b452005-10-08 00:29:44 +00002330 // Check to see if this is the equivalent of setcc
2331 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2332 // otherwise, go ahead with the folds.
2333 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2334 MVT::ValueType XType = N0.getValueType();
2335 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2336 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2337 if (Res.getValueType() != VT)
2338 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2339 return Res;
2340 }
2341
2342 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2343 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2344 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2345 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2346 return DAG.getNode(ISD::SRL, XType, Ctlz,
2347 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2348 TLI.getShiftAmountTy()));
2349 }
2350 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2351 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2352 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2353 N0);
2354 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2355 DAG.getConstant(~0ULL, XType));
2356 return DAG.getNode(ISD::SRL, XType,
2357 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2358 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2359 TLI.getShiftAmountTy()));
2360 }
2361 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2362 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2363 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2364 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2365 TLI.getShiftAmountTy()));
2366 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2367 }
2368 }
2369
2370 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2371 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2372 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2373 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2374 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2375 MVT::ValueType XType = N0.getValueType();
2376 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2377 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2378 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2379 TLI.getShiftAmountTy()));
2380 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
2381 WorkList.push_back(Shift.Val);
2382 WorkList.push_back(Add.Val);
2383 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2384 }
2385 }
2386 }
2387
Nate Begeman44728a72005-09-19 22:34:01 +00002388 return SDOperand();
2389}
2390
Nate Begeman452d7be2005-09-16 00:54:12 +00002391SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002392 SDOperand N1, ISD::CondCode Cond,
2393 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002394 // These setcc operations always fold.
2395 switch (Cond) {
2396 default: break;
2397 case ISD::SETFALSE:
2398 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2399 case ISD::SETTRUE:
2400 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2401 }
2402
2403 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2404 uint64_t C1 = N1C->getValue();
2405 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2406 uint64_t C0 = N0C->getValue();
2407
2408 // Sign extend the operands if required
2409 if (ISD::isSignedIntSetCC(Cond)) {
2410 C0 = N0C->getSignExtended();
2411 C1 = N1C->getSignExtended();
2412 }
2413
2414 switch (Cond) {
2415 default: assert(0 && "Unknown integer setcc!");
2416 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2417 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2418 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2419 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2420 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2421 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2422 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2423 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2424 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2425 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2426 }
2427 } else {
2428 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2429 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2430 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2431
2432 // If the comparison constant has bits in the upper part, the
2433 // zero-extended value could never match.
2434 if (C1 & (~0ULL << InSize)) {
2435 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2436 switch (Cond) {
2437 case ISD::SETUGT:
2438 case ISD::SETUGE:
2439 case ISD::SETEQ: return DAG.getConstant(0, VT);
2440 case ISD::SETULT:
2441 case ISD::SETULE:
2442 case ISD::SETNE: return DAG.getConstant(1, VT);
2443 case ISD::SETGT:
2444 case ISD::SETGE:
2445 // True if the sign bit of C1 is set.
2446 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2447 case ISD::SETLT:
2448 case ISD::SETLE:
2449 // True if the sign bit of C1 isn't set.
2450 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2451 default:
2452 break;
2453 }
2454 }
2455
2456 // Otherwise, we can perform the comparison with the low bits.
2457 switch (Cond) {
2458 case ISD::SETEQ:
2459 case ISD::SETNE:
2460 case ISD::SETUGT:
2461 case ISD::SETUGE:
2462 case ISD::SETULT:
2463 case ISD::SETULE:
2464 return DAG.getSetCC(VT, N0.getOperand(0),
2465 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2466 Cond);
2467 default:
2468 break; // todo, be more careful with signed comparisons
2469 }
2470 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2471 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2472 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2473 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2474 MVT::ValueType ExtDstTy = N0.getValueType();
2475 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2476
2477 // If the extended part has any inconsistent bits, it cannot ever
2478 // compare equal. In other words, they have to be all ones or all
2479 // zeros.
2480 uint64_t ExtBits =
2481 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2482 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2483 return DAG.getConstant(Cond == ISD::SETNE, VT);
2484
2485 SDOperand ZextOp;
2486 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2487 if (Op0Ty == ExtSrcTy) {
2488 ZextOp = N0.getOperand(0);
2489 } else {
2490 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2491 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2492 DAG.getConstant(Imm, Op0Ty));
2493 }
2494 WorkList.push_back(ZextOp.Val);
2495 // Otherwise, make this a use of a zext.
2496 return DAG.getSetCC(VT, ZextOp,
2497 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2498 ExtDstTy),
2499 Cond);
2500 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002501
Nate Begeman452d7be2005-09-16 00:54:12 +00002502 uint64_t MinVal, MaxVal;
2503 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2504 if (ISD::isSignedIntSetCC(Cond)) {
2505 MinVal = 1ULL << (OperandBitSize-1);
2506 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2507 MaxVal = ~0ULL >> (65-OperandBitSize);
2508 else
2509 MaxVal = 0;
2510 } else {
2511 MinVal = 0;
2512 MaxVal = ~0ULL >> (64-OperandBitSize);
2513 }
2514
2515 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2516 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2517 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2518 --C1; // X >= C0 --> X > (C0-1)
2519 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2520 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2521 }
2522
2523 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2524 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2525 ++C1; // X <= C0 --> X < (C0+1)
2526 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2527 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2528 }
2529
2530 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2531 return DAG.getConstant(0, VT); // X < MIN --> false
2532
2533 // Canonicalize setgt X, Min --> setne X, Min
2534 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2535 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002536 // Canonicalize setlt X, Max --> setne X, Max
2537 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2538 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002539
2540 // If we have setult X, 1, turn it into seteq X, 0
2541 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2542 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2543 ISD::SETEQ);
2544 // If we have setugt X, Max-1, turn it into seteq X, Max
2545 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2546 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2547 ISD::SETEQ);
2548
2549 // If we have "setcc X, C0", check to see if we can shrink the immediate
2550 // by changing cc.
2551
2552 // SETUGT X, SINTMAX -> SETLT X, 0
2553 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2554 C1 == (~0ULL >> (65-OperandBitSize)))
2555 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2556 ISD::SETLT);
2557
2558 // FIXME: Implement the rest of these.
2559
2560 // Fold bit comparisons when we can.
2561 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2562 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2563 if (ConstantSDNode *AndRHS =
2564 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2565 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2566 // Perform the xform if the AND RHS is a single bit.
2567 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2568 return DAG.getNode(ISD::SRL, VT, N0,
2569 DAG.getConstant(Log2_64(AndRHS->getValue()),
2570 TLI.getShiftAmountTy()));
2571 }
2572 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2573 // (X & 8) == 8 --> (X & 8) >> 3
2574 // Perform the xform if C1 is a single bit.
2575 if ((C1 & (C1-1)) == 0) {
2576 return DAG.getNode(ISD::SRL, VT, N0,
2577 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2578 }
2579 }
2580 }
2581 }
2582 } else if (isa<ConstantSDNode>(N0.Val)) {
2583 // Ensure that the constant occurs on the RHS.
2584 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2585 }
2586
2587 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2588 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2589 double C0 = N0C->getValue(), C1 = N1C->getValue();
2590
2591 switch (Cond) {
2592 default: break; // FIXME: Implement the rest of these!
2593 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2594 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2595 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2596 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2597 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2598 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2599 }
2600 } else {
2601 // Ensure that the constant occurs on the RHS.
2602 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2603 }
2604
2605 if (N0 == N1) {
2606 // We can always fold X == Y for integer setcc's.
2607 if (MVT::isInteger(N0.getValueType()))
2608 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2609 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2610 if (UOF == 2) // FP operators that are undefined on NaNs.
2611 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2612 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2613 return DAG.getConstant(UOF, VT);
2614 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2615 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002616 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002617 if (NewCond != Cond)
2618 return DAG.getSetCC(VT, N0, N1, NewCond);
2619 }
2620
2621 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2622 MVT::isInteger(N0.getValueType())) {
2623 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2624 N0.getOpcode() == ISD::XOR) {
2625 // Simplify (X+Y) == (X+Z) --> Y == Z
2626 if (N0.getOpcode() == N1.getOpcode()) {
2627 if (N0.getOperand(0) == N1.getOperand(0))
2628 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2629 if (N0.getOperand(1) == N1.getOperand(1))
2630 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2631 if (isCommutativeBinOp(N0.getOpcode())) {
2632 // If X op Y == Y op X, try other combinations.
2633 if (N0.getOperand(0) == N1.getOperand(1))
2634 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2635 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002636 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002637 }
2638 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002639
2640 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2641 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2642 // Turn (X+C1) == C2 --> X == C2-C1
2643 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2644 return DAG.getSetCC(VT, N0.getOperand(0),
2645 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2646 N0.getValueType()), Cond);
2647 }
2648
2649 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2650 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002651 // If we know that all of the inverted bits are zero, don't bother
2652 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002653 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002654 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002655 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002656 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002657 }
2658
2659 // Turn (C1-X) == C2 --> X == C1-C2
2660 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2661 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
2662 return DAG.getSetCC(VT, N0.getOperand(1),
2663 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
2664 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00002665 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002666 }
2667 }
2668
Nate Begeman452d7be2005-09-16 00:54:12 +00002669 // Simplify (X+Z) == X --> Z == 0
2670 if (N0.getOperand(0) == N1)
2671 return DAG.getSetCC(VT, N0.getOperand(1),
2672 DAG.getConstant(0, N0.getValueType()), Cond);
2673 if (N0.getOperand(1) == N1) {
2674 if (isCommutativeBinOp(N0.getOpcode()))
2675 return DAG.getSetCC(VT, N0.getOperand(0),
2676 DAG.getConstant(0, N0.getValueType()), Cond);
2677 else {
2678 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2679 // (Z-X) == X --> Z == X<<1
2680 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2681 N1,
2682 DAG.getConstant(1,TLI.getShiftAmountTy()));
2683 WorkList.push_back(SH.Val);
2684 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2685 }
2686 }
2687 }
2688
2689 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2690 N1.getOpcode() == ISD::XOR) {
2691 // Simplify X == (X+Z) --> Z == 0
2692 if (N1.getOperand(0) == N0) {
2693 return DAG.getSetCC(VT, N1.getOperand(1),
2694 DAG.getConstant(0, N1.getValueType()), Cond);
2695 } else if (N1.getOperand(1) == N0) {
2696 if (isCommutativeBinOp(N1.getOpcode())) {
2697 return DAG.getSetCC(VT, N1.getOperand(0),
2698 DAG.getConstant(0, N1.getValueType()), Cond);
2699 } else {
2700 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2701 // X == (Z-X) --> X<<1 == Z
2702 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2703 DAG.getConstant(1,TLI.getShiftAmountTy()));
2704 WorkList.push_back(SH.Val);
2705 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2706 }
2707 }
2708 }
2709 }
2710
2711 // Fold away ALL boolean setcc's.
2712 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002713 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002714 switch (Cond) {
2715 default: assert(0 && "Unknown integer setcc!");
2716 case ISD::SETEQ: // X == Y -> (X^Y)^1
2717 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2718 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2719 WorkList.push_back(Temp.Val);
2720 break;
2721 case ISD::SETNE: // X != Y --> (X^Y)
2722 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2723 break;
2724 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2725 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2726 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2727 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2728 WorkList.push_back(Temp.Val);
2729 break;
2730 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2731 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2732 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2733 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2734 WorkList.push_back(Temp.Val);
2735 break;
2736 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2737 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2738 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2739 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2740 WorkList.push_back(Temp.Val);
2741 break;
2742 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2743 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2744 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2745 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2746 break;
2747 }
2748 if (VT != MVT::i1) {
2749 WorkList.push_back(N0.Val);
2750 // FIXME: If running after legalize, we probably can't do this.
2751 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2752 }
2753 return N0;
2754 }
2755
2756 // Could not fold it.
2757 return SDOperand();
2758}
2759
Nate Begeman69575232005-10-20 02:15:44 +00002760/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2761/// return a DAG expression to select that will generate the same value by
2762/// multiplying by a magic number. See:
2763/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2764SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2765 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002766
2767 // Check to see if we can do this.
2768 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2769 return SDOperand(); // BuildSDIV only operates on i32 or i64
2770 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2771 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002772
Nate Begemanc6a454e2005-10-20 17:45:03 +00002773 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002774 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2775
2776 // Multiply the numerator (operand 0) by the magic value
2777 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2778 DAG.getConstant(magics.m, VT));
2779 // If d > 0 and m < 0, add the numerator
2780 if (d > 0 && magics.m < 0) {
2781 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2782 WorkList.push_back(Q.Val);
2783 }
2784 // If d < 0 and m > 0, subtract the numerator.
2785 if (d < 0 && magics.m > 0) {
2786 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2787 WorkList.push_back(Q.Val);
2788 }
2789 // Shift right algebraic if shift value is nonzero
2790 if (magics.s > 0) {
2791 Q = DAG.getNode(ISD::SRA, VT, Q,
2792 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2793 WorkList.push_back(Q.Val);
2794 }
2795 // Extract the sign bit and add it to the quotient
2796 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002797 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2798 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +00002799 WorkList.push_back(T.Val);
2800 return DAG.getNode(ISD::ADD, VT, Q, T);
2801}
2802
2803/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2804/// return a DAG expression to select that will generate the same value by
2805/// multiplying by a magic number. See:
2806/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2807SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2808 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002809
2810 // Check to see if we can do this.
2811 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2812 return SDOperand(); // BuildUDIV only operates on i32 or i64
2813 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2814 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002815
2816 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2817 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2818
2819 // Multiply the numerator (operand 0) by the magic value
2820 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2821 DAG.getConstant(magics.m, VT));
2822 WorkList.push_back(Q.Val);
2823
2824 if (magics.a == 0) {
2825 return DAG.getNode(ISD::SRL, VT, Q,
2826 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2827 } else {
2828 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2829 WorkList.push_back(NPQ.Val);
2830 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2831 DAG.getConstant(1, TLI.getShiftAmountTy()));
2832 WorkList.push_back(NPQ.Val);
2833 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2834 WorkList.push_back(NPQ.Val);
2835 return DAG.getNode(ISD::SRL, VT, NPQ,
2836 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
2837 }
2838}
2839
Nate Begeman1d4d4142005-09-01 00:19:25 +00002840// SelectionDAG::Combine - This is the entry point for the file.
2841//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002842void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002843 /// run - This is the main entry point to this class.
2844 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002845 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002846}