blob: 8f1fa3b8f6a34d8b0bfc9331a18d418445dff1ac [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
19// FIXME: Should add a corresponding version of fold AND with
20// ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
21// we don't have yet.
22//
Nate Begeman44728a72005-09-19 22:34:01 +000023// FIXME: select C, pow2, pow2 -> something smart
24// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000025// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000026// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000027// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000028// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000029// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman4ebd8052005-09-01 23:24:04 +000030// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000031// FIXME: verify that getNode can't return extends with an operand whose type
32// is >= to that of the extend.
33// FIXME: divide by zero is currently left unfolded. do we want to turn this
34// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000035// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000036//
37//===----------------------------------------------------------------------===//
38
39#define DEBUG_TYPE "dagcombine"
40#include "llvm/ADT/Statistic.h"
41#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000042#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000043#include "llvm/Support/MathExtras.h"
44#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000045#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000046#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000047#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000048using namespace llvm;
49
50namespace {
51 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
52
53 class DAGCombiner {
54 SelectionDAG &DAG;
55 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000056 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
59 std::vector<SDNode*> WorkList;
60
61 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
63 /// now.
64 ///
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 UI != UE; ++UI)
68 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000069 }
70
71 /// removeFromWorkList - remove all instances of N from the worklist.
72 void removeFromWorkList(SDNode *N) {
73 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
74 WorkList.end());
75 }
76
Chris Lattner01a22022005-10-10 22:04:48 +000077 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000078 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000079 DEBUG(std::cerr << "\nReplacing "; N->dump();
80 std::cerr << "\nWith: "; To[0].Val->dump();
81 std::cerr << " and " << To.size()-1 << " other values\n");
82 std::vector<SDNode*> NowDead;
83 DAG.ReplaceAllUsesWith(N, To, &NowDead);
84
85 // Push the new nodes and any users onto the worklist
86 for (unsigned i = 0, e = To.size(); i != e; ++i) {
87 WorkList.push_back(To[i].Val);
88 AddUsersToWorkList(To[i].Val);
89 }
90
91 // Nodes can end up on the worklist more than once. Make sure we do
92 // not process a node that has been replaced.
93 removeFromWorkList(N);
94 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
95 removeFromWorkList(NowDead[i]);
96
97 // Finally, since the node is now dead, remove it from the graph.
98 DAG.DeleteNode(N);
99 return SDOperand(N, 0);
100 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000101
102 SDOperand CombineTo(SDNode *N, SDOperand Res) {
103 std::vector<SDOperand> To;
104 To.push_back(Res);
105 return CombineTo(N, To);
106 }
Chris Lattner01a22022005-10-10 22:04:48 +0000107
108 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
109 std::vector<SDOperand> To;
110 To.push_back(Res0);
111 To.push_back(Res1);
112 return CombineTo(N, To);
113 }
114
Nate Begeman1d4d4142005-09-01 00:19:25 +0000115 /// visit - call the node-specific routine that knows how to fold each
116 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000117 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000118
119 // Visitation implementation - Implement dag node combining for different
120 // node types. The semantics are as follows:
121 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000122 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000123 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000124 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000125 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000126 SDOperand visitTokenFactor(SDNode *N);
127 SDOperand visitADD(SDNode *N);
128 SDOperand visitSUB(SDNode *N);
129 SDOperand visitMUL(SDNode *N);
130 SDOperand visitSDIV(SDNode *N);
131 SDOperand visitUDIV(SDNode *N);
132 SDOperand visitSREM(SDNode *N);
133 SDOperand visitUREM(SDNode *N);
134 SDOperand visitMULHU(SDNode *N);
135 SDOperand visitMULHS(SDNode *N);
136 SDOperand visitAND(SDNode *N);
137 SDOperand visitOR(SDNode *N);
138 SDOperand visitXOR(SDNode *N);
139 SDOperand visitSHL(SDNode *N);
140 SDOperand visitSRA(SDNode *N);
141 SDOperand visitSRL(SDNode *N);
142 SDOperand visitCTLZ(SDNode *N);
143 SDOperand visitCTTZ(SDNode *N);
144 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000145 SDOperand visitSELECT(SDNode *N);
146 SDOperand visitSELECT_CC(SDNode *N);
147 SDOperand visitSETCC(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000148 SDOperand visitADD_PARTS(SDNode *N);
149 SDOperand visitSUB_PARTS(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000150 SDOperand visitSIGN_EXTEND(SDNode *N);
151 SDOperand visitZERO_EXTEND(SDNode *N);
152 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
153 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000154 SDOperand visitBIT_CONVERT(SDNode *N);
Nate Begeman5054f162005-10-14 01:12:21 +0000155
Chris Lattner01b3d732005-09-28 22:28:18 +0000156 SDOperand visitFADD(SDNode *N);
157 SDOperand visitFSUB(SDNode *N);
158 SDOperand visitFMUL(SDNode *N);
159 SDOperand visitFDIV(SDNode *N);
160 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000161 SDOperand visitSINT_TO_FP(SDNode *N);
162 SDOperand visitUINT_TO_FP(SDNode *N);
163 SDOperand visitFP_TO_SINT(SDNode *N);
164 SDOperand visitFP_TO_UINT(SDNode *N);
165 SDOperand visitFP_ROUND(SDNode *N);
166 SDOperand visitFP_ROUND_INREG(SDNode *N);
167 SDOperand visitFP_EXTEND(SDNode *N);
168 SDOperand visitFNEG(SDNode *N);
169 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000170 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000171 SDOperand visitBRCONDTWOWAY(SDNode *N);
172 SDOperand visitBR_CC(SDNode *N);
173 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000174
Chris Lattner01a22022005-10-10 22:04:48 +0000175 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000176 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000177
Nate Begemancd4d58c2006-02-03 06:46:56 +0000178 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
179
Chris Lattner40c62d52005-10-18 06:04:22 +0000180 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000181 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
182 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
183 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000184 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000185 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000186
187 SDOperand BuildSDIV(SDNode *N);
188 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000189public:
190 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000191 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000192
193 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000194 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000195 };
196}
197
Nate Begeman69575232005-10-20 02:15:44 +0000198struct ms {
199 int64_t m; // magic number
200 int64_t s; // shift amount
201};
202
203struct mu {
204 uint64_t m; // magic number
205 int64_t a; // add indicator
206 int64_t s; // shift amount
207};
208
209/// magic - calculate the magic numbers required to codegen an integer sdiv as
210/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
211/// or -1.
212static ms magic32(int32_t d) {
213 int32_t p;
214 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
215 const uint32_t two31 = 0x80000000U;
216 struct ms mag;
217
218 ad = abs(d);
219 t = two31 + ((uint32_t)d >> 31);
220 anc = t - 1 - t%ad; // absolute value of nc
221 p = 31; // initialize p
222 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
223 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
224 q2 = two31/ad; // initialize q2 = 2p/abs(d)
225 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
226 do {
227 p = p + 1;
228 q1 = 2*q1; // update q1 = 2p/abs(nc)
229 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
230 if (r1 >= anc) { // must be unsigned comparison
231 q1 = q1 + 1;
232 r1 = r1 - anc;
233 }
234 q2 = 2*q2; // update q2 = 2p/abs(d)
235 r2 = 2*r2; // update r2 = rem(2p/abs(d))
236 if (r2 >= ad) { // must be unsigned comparison
237 q2 = q2 + 1;
238 r2 = r2 - ad;
239 }
240 delta = ad - r2;
241 } while (q1 < delta || (q1 == delta && r1 == 0));
242
243 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
244 if (d < 0) mag.m = -mag.m; // resulting magic number
245 mag.s = p - 32; // resulting shift
246 return mag;
247}
248
249/// magicu - calculate the magic numbers required to codegen an integer udiv as
250/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
251static mu magicu32(uint32_t d) {
252 int32_t p;
253 uint32_t nc, delta, q1, r1, q2, r2;
254 struct mu magu;
255 magu.a = 0; // initialize "add" indicator
256 nc = - 1 - (-d)%d;
257 p = 31; // initialize p
258 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
259 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
260 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
261 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
262 do {
263 p = p + 1;
264 if (r1 >= nc - r1 ) {
265 q1 = 2*q1 + 1; // update q1
266 r1 = 2*r1 - nc; // update r1
267 }
268 else {
269 q1 = 2*q1; // update q1
270 r1 = 2*r1; // update r1
271 }
272 if (r2 + 1 >= d - r2) {
273 if (q2 >= 0x7FFFFFFF) magu.a = 1;
274 q2 = 2*q2 + 1; // update q2
275 r2 = 2*r2 + 1 - d; // update r2
276 }
277 else {
278 if (q2 >= 0x80000000) magu.a = 1;
279 q2 = 2*q2; // update q2
280 r2 = 2*r2 + 1; // update r2
281 }
282 delta = d - 1 - r2;
283 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
284 magu.m = q2 + 1; // resulting magic number
285 magu.s = p - 32; // resulting shift
286 return magu;
287}
288
289/// magic - calculate the magic numbers required to codegen an integer sdiv as
290/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
291/// or -1.
292static ms magic64(int64_t d) {
293 int64_t p;
294 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
295 const uint64_t two63 = 9223372036854775808ULL; // 2^63
296 struct ms mag;
297
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000298 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000299 t = two63 + ((uint64_t)d >> 63);
300 anc = t - 1 - t%ad; // absolute value of nc
301 p = 63; // initialize p
302 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
303 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
304 q2 = two63/ad; // initialize q2 = 2p/abs(d)
305 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
306 do {
307 p = p + 1;
308 q1 = 2*q1; // update q1 = 2p/abs(nc)
309 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
310 if (r1 >= anc) { // must be unsigned comparison
311 q1 = q1 + 1;
312 r1 = r1 - anc;
313 }
314 q2 = 2*q2; // update q2 = 2p/abs(d)
315 r2 = 2*r2; // update r2 = rem(2p/abs(d))
316 if (r2 >= ad) { // must be unsigned comparison
317 q2 = q2 + 1;
318 r2 = r2 - ad;
319 }
320 delta = ad - r2;
321 } while (q1 < delta || (q1 == delta && r1 == 0));
322
323 mag.m = q2 + 1;
324 if (d < 0) mag.m = -mag.m; // resulting magic number
325 mag.s = p - 64; // resulting shift
326 return mag;
327}
328
329/// magicu - calculate the magic numbers required to codegen an integer udiv as
330/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
331static mu magicu64(uint64_t d)
332{
333 int64_t p;
334 uint64_t nc, delta, q1, r1, q2, r2;
335 struct mu magu;
336 magu.a = 0; // initialize "add" indicator
337 nc = - 1 - (-d)%d;
338 p = 63; // initialize p
339 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
340 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
341 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
342 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
343 do {
344 p = p + 1;
345 if (r1 >= nc - r1 ) {
346 q1 = 2*q1 + 1; // update q1
347 r1 = 2*r1 - nc; // update r1
348 }
349 else {
350 q1 = 2*q1; // update q1
351 r1 = 2*r1; // update r1
352 }
353 if (r2 + 1 >= d - r2) {
354 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
355 q2 = 2*q2 + 1; // update q2
356 r2 = 2*r2 + 1 - d; // update r2
357 }
358 else {
359 if (q2 >= 0x8000000000000000ull) magu.a = 1;
360 q2 = 2*q2; // update q2
361 r2 = 2*r2 + 1; // update r2
362 }
363 delta = d - 1 - r2;
364 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
365 magu.m = q2 + 1; // resulting magic number
366 magu.s = p - 64; // resulting shift
367 return magu;
368}
369
Nate Begeman4ebd8052005-09-01 23:24:04 +0000370// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
371// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000372// Also, set the incoming LHS, RHS, and CC references to the appropriate
373// nodes based on the type of node we are checking. This simplifies life a
374// bit for the callers.
375static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
376 SDOperand &CC) {
377 if (N.getOpcode() == ISD::SETCC) {
378 LHS = N.getOperand(0);
379 RHS = N.getOperand(1);
380 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000381 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000382 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000383 if (N.getOpcode() == ISD::SELECT_CC &&
384 N.getOperand(2).getOpcode() == ISD::Constant &&
385 N.getOperand(3).getOpcode() == ISD::Constant &&
386 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000387 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
388 LHS = N.getOperand(0);
389 RHS = N.getOperand(1);
390 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000391 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000392 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000393 return false;
394}
395
Nate Begeman99801192005-09-07 23:25:52 +0000396// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
397// one use. If this is true, it allows the users to invert the operation for
398// free when it is profitable to do so.
399static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000400 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000401 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000402 return true;
403 return false;
404}
405
Nate Begeman452d7be2005-09-16 00:54:12 +0000406// FIXME: This should probably go in the ISD class rather than being duplicated
407// in several files.
408static bool isCommutativeBinOp(unsigned Opcode) {
409 switch (Opcode) {
410 case ISD::ADD:
411 case ISD::MUL:
412 case ISD::AND:
413 case ISD::OR:
414 case ISD::XOR: return true;
415 default: return false; // FIXME: Need commutative info for user ops!
416 }
417}
418
Nate Begemancd4d58c2006-02-03 06:46:56 +0000419SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
420 MVT::ValueType VT = N0.getValueType();
421 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
422 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
423 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
424 if (isa<ConstantSDNode>(N1)) {
425 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
426 WorkList.push_back(OpNode.Val);
427 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
428 } else if (N0.hasOneUse()) {
429 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
430 WorkList.push_back(OpNode.Val);
431 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
432 }
433 }
434 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
435 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
436 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
437 if (isa<ConstantSDNode>(N0)) {
438 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
439 WorkList.push_back(OpNode.Val);
440 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
441 } else if (N1.hasOneUse()) {
442 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
443 WorkList.push_back(OpNode.Val);
444 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
445 }
446 }
447 return SDOperand();
448}
449
Nate Begeman4ebd8052005-09-01 23:24:04 +0000450void DAGCombiner::Run(bool RunningAfterLegalize) {
451 // set the instance variable, so that the various visit routines may use it.
452 AfterLegalize = RunningAfterLegalize;
453
Nate Begeman646d7e22005-09-02 21:18:40 +0000454 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000455 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
456 E = DAG.allnodes_end(); I != E; ++I)
457 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000458
Chris Lattner95038592005-10-05 06:35:28 +0000459 // Create a dummy node (which is not added to allnodes), that adds a reference
460 // to the root node, preventing it from being deleted, and tracking any
461 // changes of the root.
462 HandleSDNode Dummy(DAG.getRoot());
463
Nate Begeman1d4d4142005-09-01 00:19:25 +0000464 // while the worklist isn't empty, inspect the node on the end of it and
465 // try and combine it.
466 while (!WorkList.empty()) {
467 SDNode *N = WorkList.back();
468 WorkList.pop_back();
469
470 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000471 // N is deleted from the DAG, since they too may now be dead or may have a
472 // reduced number of uses, allowing other xforms.
473 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000474 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
475 WorkList.push_back(N->getOperand(i).Val);
476
Nate Begeman1d4d4142005-09-01 00:19:25 +0000477 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000478 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000479 continue;
480 }
481
Nate Begeman83e75ec2005-09-06 04:43:02 +0000482 SDOperand RV = visit(N);
483 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000485 // If we get back the same node we passed in, rather than a new node or
486 // zero, we know that the node must have defined multiple values and
487 // CombineTo was used. Since CombineTo takes care of the worklist
488 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000489 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000490 DEBUG(std::cerr << "\nReplacing "; N->dump();
491 std::cerr << "\nWith: "; RV.Val->dump();
492 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000493 std::vector<SDNode*> NowDead;
494 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000495
496 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000497 WorkList.push_back(RV.Val);
498 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000499
500 // Nodes can end up on the worklist more than once. Make sure we do
501 // not process a node that has been replaced.
502 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000503 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
504 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000505
506 // Finally, since the node is now dead, remove it from the graph.
507 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000508 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000509 }
510 }
Chris Lattner95038592005-10-05 06:35:28 +0000511
512 // If the root changed (e.g. it was a dead load, update the root).
513 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000514}
515
Nate Begeman83e75ec2005-09-06 04:43:02 +0000516SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000517 switch(N->getOpcode()) {
518 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000519 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000520 case ISD::ADD: return visitADD(N);
521 case ISD::SUB: return visitSUB(N);
522 case ISD::MUL: return visitMUL(N);
523 case ISD::SDIV: return visitSDIV(N);
524 case ISD::UDIV: return visitUDIV(N);
525 case ISD::SREM: return visitSREM(N);
526 case ISD::UREM: return visitUREM(N);
527 case ISD::MULHU: return visitMULHU(N);
528 case ISD::MULHS: return visitMULHS(N);
529 case ISD::AND: return visitAND(N);
530 case ISD::OR: return visitOR(N);
531 case ISD::XOR: return visitXOR(N);
532 case ISD::SHL: return visitSHL(N);
533 case ISD::SRA: return visitSRA(N);
534 case ISD::SRL: return visitSRL(N);
535 case ISD::CTLZ: return visitCTLZ(N);
536 case ISD::CTTZ: return visitCTTZ(N);
537 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000538 case ISD::SELECT: return visitSELECT(N);
539 case ISD::SELECT_CC: return visitSELECT_CC(N);
540 case ISD::SETCC: return visitSETCC(N);
Nate Begeman5054f162005-10-14 01:12:21 +0000541 case ISD::ADD_PARTS: return visitADD_PARTS(N);
542 case ISD::SUB_PARTS: return visitSUB_PARTS(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000543 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
544 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
545 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
546 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000547 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000548 case ISD::FADD: return visitFADD(N);
549 case ISD::FSUB: return visitFSUB(N);
550 case ISD::FMUL: return visitFMUL(N);
551 case ISD::FDIV: return visitFDIV(N);
552 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000553 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
554 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
555 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
556 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
557 case ISD::FP_ROUND: return visitFP_ROUND(N);
558 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
559 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
560 case ISD::FNEG: return visitFNEG(N);
561 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000562 case ISD::BRCOND: return visitBRCOND(N);
563 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
564 case ISD::BR_CC: return visitBR_CC(N);
565 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000566 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000567 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000569 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000570}
571
Nate Begeman83e75ec2005-09-06 04:43:02 +0000572SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000573 std::vector<SDOperand> Ops;
574 bool Changed = false;
575
Nate Begeman1d4d4142005-09-01 00:19:25 +0000576 // If the token factor has two operands and one is the entry token, replace
577 // the token factor with the other operand.
578 if (N->getNumOperands() == 2) {
579 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000580 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000581 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000583 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000584
Nate Begemanded49632005-10-13 03:11:28 +0000585 // fold (tokenfactor (tokenfactor)) -> tokenfactor
586 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
587 SDOperand Op = N->getOperand(i);
588 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
589 Changed = true;
590 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
591 Ops.push_back(Op.getOperand(j));
592 } else {
593 Ops.push_back(Op);
594 }
595 }
596 if (Changed)
597 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000598 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000599}
600
Nate Begeman83e75ec2005-09-06 04:43:02 +0000601SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000602 SDOperand N0 = N->getOperand(0);
603 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000604 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
605 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000606 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000607
608 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000609 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000610 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000611 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000612 if (N0C && !N1C)
613 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000614 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000615 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000616 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000617 // fold ((c1-A)+c2) -> (c1+c2)-A
618 if (N1C && N0.getOpcode() == ISD::SUB)
619 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
620 return DAG.getNode(ISD::SUB, VT,
621 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
622 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000623 // reassociate add
624 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
625 if (RADD.Val != 0)
626 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000627 // fold ((0-A) + B) -> B-A
628 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
629 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000630 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631 // fold (A + (0-B)) -> A-B
632 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
633 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000634 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000635 // fold (A+(B-A)) -> B
636 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000637 return N1.getOperand(0);
638 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000639}
640
Nate Begeman83e75ec2005-09-06 04:43:02 +0000641SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000642 SDOperand N0 = N->getOperand(0);
643 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000644 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
645 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000646 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000647
Chris Lattner854077d2005-10-17 01:07:11 +0000648 // fold (sub x, x) -> 0
649 if (N0 == N1)
650 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000652 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000653 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000654 // fold (sub x, c) -> (add x, -c)
655 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000656 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000657 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000658 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000659 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000660 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000661 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000662 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000663 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000664}
665
Nate Begeman83e75ec2005-09-06 04:43:02 +0000666SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000667 SDOperand N0 = N->getOperand(0);
668 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000669 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
670 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000671 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672
673 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000674 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000675 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000676 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000677 if (N0C && !N1C)
678 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000680 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000681 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000682 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000683 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000684 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000685 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000686 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000687 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000689 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000690 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
691 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
692 // FIXME: If the input is something that is easily negated (e.g. a
693 // single-use add), we should put the negate there.
694 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
695 DAG.getNode(ISD::SHL, VT, N0,
696 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
697 TLI.getShiftAmountTy())));
698 }
Nate Begemancd4d58c2006-02-03 06:46:56 +0000699 // reassociate mul
700 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
701 if (RMUL.Val != 0)
702 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000703 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000704}
705
Nate Begeman83e75ec2005-09-06 04:43:02 +0000706SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000707 SDOperand N0 = N->getOperand(0);
708 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000709 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
710 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000711 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000712
713 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000714 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000715 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000716 // fold (sdiv X, 1) -> X
717 if (N1C && N1C->getSignExtended() == 1LL)
718 return N0;
719 // fold (sdiv X, -1) -> 0-X
720 if (N1C && N1C->isAllOnesValue())
721 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000722 // If we know the sign bits of both operands are zero, strength reduce to a
723 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
724 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000725 if (TLI.MaskedValueIsZero(N1, SignBit) &&
726 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000727 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000728 // fold (sdiv X, pow2) -> (add (sra X, log(pow2)), (srl X, sizeof(X)-1))
729 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
730 (isPowerOf2_64(N1C->getSignExtended()) ||
731 isPowerOf2_64(-N1C->getSignExtended()))) {
732 // If dividing by powers of two is cheap, then don't perform the following
733 // fold.
734 if (TLI.isPow2DivCheap())
735 return SDOperand();
736 int64_t pow2 = N1C->getSignExtended();
737 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
738 SDOperand SRL = DAG.getNode(ISD::SRL, VT, N0,
739 DAG.getConstant(MVT::getSizeInBits(VT)-1,
740 TLI.getShiftAmountTy()));
741 WorkList.push_back(SRL.Val);
742 SDOperand SGN = DAG.getNode(ISD::ADD, VT, N0, SRL);
743 WorkList.push_back(SGN.Val);
744 SDOperand SRA = DAG.getNode(ISD::SRA, VT, SGN,
745 DAG.getConstant(Log2_64(abs2),
746 TLI.getShiftAmountTy()));
747 // If we're dividing by a positive value, we're done. Otherwise, we must
748 // negate the result.
749 if (pow2 > 0)
750 return SRA;
751 WorkList.push_back(SRA.Val);
752 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
753 }
Nate Begeman69575232005-10-20 02:15:44 +0000754 // if integer divide is expensive and we satisfy the requirements, emit an
755 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000756 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000757 !TLI.isIntDivCheap()) {
758 SDOperand Op = BuildSDIV(N);
759 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000760 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000761 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000762}
763
Nate Begeman83e75ec2005-09-06 04:43:02 +0000764SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000765 SDOperand N0 = N->getOperand(0);
766 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000767 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
768 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000769 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000770
771 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000772 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000773 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000774 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000775 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000776 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000777 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000778 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000779 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
780 if (N1.getOpcode() == ISD::SHL) {
781 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
782 if (isPowerOf2_64(SHC->getValue())) {
783 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000784 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
785 DAG.getConstant(Log2_64(SHC->getValue()),
786 ADDVT));
787 WorkList.push_back(Add.Val);
788 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000789 }
790 }
791 }
Nate Begeman69575232005-10-20 02:15:44 +0000792 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000793 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
794 SDOperand Op = BuildUDIV(N);
795 if (Op.Val) return Op;
796 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000797 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000798}
799
Nate Begeman83e75ec2005-09-06 04:43:02 +0000800SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000801 SDOperand N0 = N->getOperand(0);
802 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000803 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
804 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000805 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000806
807 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000808 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000809 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000810 // If we know the sign bits of both operands are zero, strength reduce to a
811 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
812 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000813 if (TLI.MaskedValueIsZero(N1, SignBit) &&
814 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000815 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000816 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000817}
818
Nate Begeman83e75ec2005-09-06 04:43:02 +0000819SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000820 SDOperand N0 = N->getOperand(0);
821 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000822 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
823 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000824 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000825
826 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000827 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000828 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000829 // fold (urem x, pow2) -> (and x, pow2-1)
830 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000831 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000832 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
833 if (N1.getOpcode() == ISD::SHL) {
834 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
835 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000836 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000837 WorkList.push_back(Add.Val);
838 return DAG.getNode(ISD::AND, VT, N0, Add);
839 }
840 }
841 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000842 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000843}
844
Nate Begeman83e75ec2005-09-06 04:43:02 +0000845SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000846 SDOperand N0 = N->getOperand(0);
847 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000848 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000849
850 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000851 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000852 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000854 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000855 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
856 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000857 TLI.getShiftAmountTy()));
858 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000859}
860
Nate Begeman83e75ec2005-09-06 04:43:02 +0000861SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000862 SDOperand N0 = N->getOperand(0);
863 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000864 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000865
866 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000867 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000868 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000869 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000870 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000871 return DAG.getConstant(0, N0.getValueType());
872 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000873}
874
Nate Begeman83e75ec2005-09-06 04:43:02 +0000875SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000876 SDOperand N0 = N->getOperand(0);
877 SDOperand N1 = N->getOperand(1);
Nate Begemande996292006-02-03 22:24:05 +0000878 SDOperand LL, LR, RL, RR, CC0, CC1, Old, New;
Nate Begeman646d7e22005-09-02 21:18:40 +0000879 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
880 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000881 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000882 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000883
884 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000885 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000886 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000887 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000888 if (N0C && !N1C)
889 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000890 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000891 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000892 return N0;
893 // if (and x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000894 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000895 return DAG.getConstant(0, VT);
896 // fold (and x, c) -> x iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000897 if (N1C &&
898 TLI.MaskedValueIsZero(N0, ~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000899 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +0000900 // reassociate and
901 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
902 if (RAND.Val != 0)
903 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000904 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000905 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000906 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000907 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000908 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000909 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
910 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
911 unsigned InBits = MVT::getSizeInBits(N0.getOperand(0).getValueType());
912 if (TLI.MaskedValueIsZero(N0.getOperand(0),
913 ~N1C->getValue() & ((1ULL << InBits)-1))) {
914 // We actually want to replace all uses of the any_extend with the
915 // zero_extend, to avoid duplicating things. This will later cause this
916 // AND to be folded.
917 CombineTo(N0.Val, DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
918 N0.getOperand(0)));
919 return SDOperand();
920 }
921 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000922 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
923 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
924 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
925 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
926
927 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
928 MVT::isInteger(LL.getValueType())) {
929 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
930 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
931 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
932 WorkList.push_back(ORNode.Val);
933 return DAG.getSetCC(VT, ORNode, LR, Op1);
934 }
935 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
936 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
937 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
938 WorkList.push_back(ANDNode.Val);
939 return DAG.getSetCC(VT, ANDNode, LR, Op1);
940 }
941 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
942 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
943 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
944 WorkList.push_back(ORNode.Val);
945 return DAG.getSetCC(VT, ORNode, LR, Op1);
946 }
947 }
948 // canonicalize equivalent to ll == rl
949 if (LL == RR && LR == RL) {
950 Op1 = ISD::getSetCCSwappedOperands(Op1);
951 std::swap(RL, RR);
952 }
953 if (LL == RL && LR == RR) {
954 bool isInteger = MVT::isInteger(LL.getValueType());
955 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
956 if (Result != ISD::SETCC_INVALID)
957 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
958 }
959 }
960 // fold (and (zext x), (zext y)) -> (zext (and x, y))
961 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
962 N1.getOpcode() == ISD::ZERO_EXTEND &&
963 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
964 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
965 N0.getOperand(0), N1.getOperand(0));
966 WorkList.push_back(ANDNode.Val);
967 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
968 }
Nate Begeman61af66e2006-01-28 01:06:30 +0000969 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +0000970 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +0000971 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
972 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +0000973 N0.getOperand(1) == N1.getOperand(1)) {
974 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
975 N0.getOperand(0), N1.getOperand(0));
976 WorkList.push_back(ANDNode.Val);
977 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
978 }
Nate Begemande996292006-02-03 22:24:05 +0000979 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
980 // fold (and (sra)) -> (and (srl)) when possible.
981 if (TLI.DemandedBitsAreZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits), Old,
982 New, DAG)) {
983 WorkList.push_back(N);
984 CombineTo(Old.Val, New);
985 return SDOperand();
986 }
987 // FIXME: DemandedBitsAreZero cannot currently handle AND with non-constant
988 // RHS and propagate known cleared bits to LHS. For this reason, we must keep
989 // this fold, for now, for the following testcase:
990 //
991 //int %test2(uint %mode.0.i.0) {
992 // %tmp.79 = cast uint %mode.0.i.0 to int
993 // %tmp.80 = shr int %tmp.79, ubyte 15
994 // %tmp.81 = shr uint %mode.0.i.0, ubyte 16
995 // %tmp.82 = cast uint %tmp.81 to int
996 // %tmp.83 = and int %tmp.80, %tmp.82
997 // ret int %tmp.83
998 //}
Chris Lattner85d63bb2005-10-15 22:18:08 +0000999 // fold (and (sra)) -> (and (srl)) when possible.
Nate Begeman5dc7e862005-11-02 18:42:59 +00001000 if (N0.getOpcode() == ISD::SRA && N0.Val->hasOneUse()) {
Chris Lattner85d63bb2005-10-15 22:18:08 +00001001 if (ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1002 // If the RHS of the AND has zeros where the sign bits of the SRA will
1003 // land, turn the SRA into an SRL.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001004 if (TLI.MaskedValueIsZero(N1, (~0ULL << (OpSizeInBits-N01C->getValue())) &
1005 (~0ULL>>(64-OpSizeInBits)))) {
Chris Lattner85d63bb2005-10-15 22:18:08 +00001006 WorkList.push_back(N);
1007 CombineTo(N0.Val, DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
1008 N0.getOperand(1)));
1009 return SDOperand();
1010 }
1011 }
Nate Begeman5dc7e862005-11-02 18:42:59 +00001012 }
Nate Begemanded49632005-10-13 03:11:28 +00001013 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001014 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001015 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001016 // If we zero all the possible extended bits, then we can turn this into
1017 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001018 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001019 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001020 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1021 N0.getOperand(1), N0.getOperand(2),
1022 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001023 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001024 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001025 return SDOperand();
1026 }
1027 }
1028 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001029 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001030 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001031 // If we zero all the possible extended bits, then we can turn this into
1032 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001033 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001034 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001035 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1036 N0.getOperand(1), N0.getOperand(2),
1037 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001038 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001039 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001040 return SDOperand();
1041 }
1042 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001043 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001044}
1045
Nate Begeman83e75ec2005-09-06 04:43:02 +00001046SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001047 SDOperand N0 = N->getOperand(0);
1048 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001049 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001050 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1051 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001052 MVT::ValueType VT = N1.getValueType();
1053 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001054
1055 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001056 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001057 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001058 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001059 if (N0C && !N1C)
1060 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001061 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001062 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001063 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001064 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001065 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001066 return N1;
1067 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001068 if (N1C &&
1069 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001070 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001071 // reassociate or
1072 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1073 if (ROR.Val != 0)
1074 return ROR;
1075 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1076 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001077 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001078 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1079 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1080 N1),
1081 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001082 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001083 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1084 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1085 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1086 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1087
1088 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1089 MVT::isInteger(LL.getValueType())) {
1090 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1091 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1092 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1093 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1094 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1095 WorkList.push_back(ORNode.Val);
1096 return DAG.getSetCC(VT, ORNode, LR, Op1);
1097 }
1098 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1099 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1100 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1101 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1102 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1103 WorkList.push_back(ANDNode.Val);
1104 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1105 }
1106 }
1107 // canonicalize equivalent to ll == rl
1108 if (LL == RR && LR == RL) {
1109 Op1 = ISD::getSetCCSwappedOperands(Op1);
1110 std::swap(RL, RR);
1111 }
1112 if (LL == RL && LR == RR) {
1113 bool isInteger = MVT::isInteger(LL.getValueType());
1114 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1115 if (Result != ISD::SETCC_INVALID)
1116 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1117 }
1118 }
1119 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1120 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1121 N1.getOpcode() == ISD::ZERO_EXTEND &&
1122 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1123 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1124 N0.getOperand(0), N1.getOperand(0));
1125 WorkList.push_back(ORNode.Val);
1126 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1127 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001128 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1129 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1130 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1131 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1132 N0.getOperand(1) == N1.getOperand(1)) {
1133 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1134 N0.getOperand(0), N1.getOperand(0));
1135 WorkList.push_back(ORNode.Val);
1136 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1137 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001138 // canonicalize shl to left side in a shl/srl pair, to match rotate
1139 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1140 std::swap(N0, N1);
1141 // check for rotl, rotr
1142 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1143 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001144 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001145 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1146 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1147 N1.getOperand(1).getOpcode() == ISD::Constant) {
1148 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1149 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1150 if ((c1val + c2val) == OpSizeInBits)
1151 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1152 }
1153 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1154 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1155 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1156 if (ConstantSDNode *SUBC =
1157 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1158 if (SUBC->getValue() == OpSizeInBits)
1159 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1160 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1161 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1162 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1163 if (ConstantSDNode *SUBC =
1164 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1165 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001166 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001167 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1168 N1.getOperand(1));
1169 else
1170 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1171 N0.getOperand(1));
1172 }
1173 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001174 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001175}
1176
Nate Begeman83e75ec2005-09-06 04:43:02 +00001177SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 SDOperand N0 = N->getOperand(0);
1179 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001180 SDOperand LHS, RHS, CC;
1181 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1182 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001183 MVT::ValueType VT = N0.getValueType();
1184
1185 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001186 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001187 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001188 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001189 if (N0C && !N1C)
1190 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001191 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001192 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001193 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001194 // reassociate xor
1195 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1196 if (RXOR.Val != 0)
1197 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001198 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001199 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1200 bool isInt = MVT::isInteger(LHS.getValueType());
1201 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1202 isInt);
1203 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001204 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001205 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001206 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001207 assert(0 && "Unhandled SetCC Equivalent!");
1208 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001209 }
Nate Begeman99801192005-09-07 23:25:52 +00001210 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1211 if (N1C && N1C->getValue() == 1 &&
1212 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001213 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001214 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1215 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001216 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1217 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001218 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1219 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220 }
1221 }
Nate Begeman99801192005-09-07 23:25:52 +00001222 // fold !(x or y) -> (!x and !y) iff x or y are constants
1223 if (N1C && N1C->isAllOnesValue() &&
1224 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001225 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001226 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1227 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001228 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1229 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001230 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1231 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001232 }
1233 }
Nate Begeman223df222005-09-08 20:18:10 +00001234 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1235 if (N1C && N0.getOpcode() == ISD::XOR) {
1236 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1237 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1238 if (N00C)
1239 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1240 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1241 if (N01C)
1242 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1243 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1244 }
1245 // fold (xor x, x) -> 0
1246 if (N0 == N1)
1247 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001248 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1249 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1250 N1.getOpcode() == ISD::ZERO_EXTEND &&
1251 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1252 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1253 N0.getOperand(0), N1.getOperand(0));
1254 WorkList.push_back(XORNode.Val);
1255 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1256 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001257 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1258 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1259 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1260 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1261 N0.getOperand(1) == N1.getOperand(1)) {
1262 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1263 N0.getOperand(0), N1.getOperand(0));
1264 WorkList.push_back(XORNode.Val);
1265 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1266 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001267 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001268}
1269
Nate Begeman83e75ec2005-09-06 04:43:02 +00001270SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001271 SDOperand N0 = N->getOperand(0);
1272 SDOperand N1 = N->getOperand(1);
Nate Begemande996292006-02-03 22:24:05 +00001273 SDOperand Old = SDOperand();
1274 SDOperand New = SDOperand();
Nate Begeman646d7e22005-09-02 21:18:40 +00001275 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1276 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001277 MVT::ValueType VT = N0.getValueType();
1278 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1279
1280 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001281 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001282 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001283 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001284 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001285 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001286 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001287 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001288 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001289 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001290 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001291 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001292 // if (shl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001293 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001294 return DAG.getConstant(0, VT);
Nate Begemande996292006-02-03 22:24:05 +00001295 if (N1C && TLI.DemandedBitsAreZero(SDOperand(N,0), ~0ULL >> (64-OpSizeInBits),
1296 Old, New, DAG)) {
1297 WorkList.push_back(N);
1298 CombineTo(Old.Val, New);
1299 return SDOperand();
1300 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001301 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001302 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001303 N0.getOperand(1).getOpcode() == ISD::Constant) {
1304 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001305 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001306 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001307 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001308 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001309 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001310 }
1311 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1312 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001313 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001314 N0.getOperand(1).getOpcode() == ISD::Constant) {
1315 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001316 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001317 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1318 DAG.getConstant(~0ULL << c1, VT));
1319 if (c2 > c1)
1320 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001321 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001322 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001323 return DAG.getNode(ISD::SRL, VT, Mask,
1324 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001325 }
1326 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001328 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001329 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1330 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001331}
1332
Nate Begeman83e75ec2005-09-06 04:43:02 +00001333SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334 SDOperand N0 = N->getOperand(0);
1335 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1337 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001338 MVT::ValueType VT = N0.getValueType();
1339 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1340
1341 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001342 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001343 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001344 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001345 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001346 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001348 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001349 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001351 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001353 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001354 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001355 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356 // If the sign bit is known to be zero, switch this to a SRL.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001357 if (TLI.MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001358 return DAG.getNode(ISD::SRL, VT, N0, N1);
1359 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001360}
1361
Nate Begeman83e75ec2005-09-06 04:43:02 +00001362SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001363 SDOperand N0 = N->getOperand(0);
1364 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001365 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1366 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 MVT::ValueType VT = N0.getValueType();
1368 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1369
1370 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001371 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001372 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001373 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001374 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001375 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001377 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001378 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001379 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001380 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001381 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001382 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001383 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001384 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001386 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001387 N0.getOperand(1).getOpcode() == ISD::Constant) {
1388 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001389 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001390 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001393 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001394 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001395 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001396}
1397
Nate Begeman83e75ec2005-09-06 04:43:02 +00001398SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001399 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001400 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001401 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001402
1403 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001404 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001405 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001406 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001407}
1408
Nate Begeman83e75ec2005-09-06 04:43:02 +00001409SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001411 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001412 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413
1414 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001415 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001416 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001417 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001418}
1419
Nate Begeman83e75ec2005-09-06 04:43:02 +00001420SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001421 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001422 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001423 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001424
1425 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001426 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001427 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001428 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429}
1430
Nate Begeman452d7be2005-09-16 00:54:12 +00001431SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1432 SDOperand N0 = N->getOperand(0);
1433 SDOperand N1 = N->getOperand(1);
1434 SDOperand N2 = N->getOperand(2);
1435 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1436 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1437 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1438 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001439
Nate Begeman452d7be2005-09-16 00:54:12 +00001440 // fold select C, X, X -> X
1441 if (N1 == N2)
1442 return N1;
1443 // fold select true, X, Y -> X
1444 if (N0C && !N0C->isNullValue())
1445 return N1;
1446 // fold select false, X, Y -> Y
1447 if (N0C && N0C->isNullValue())
1448 return N2;
1449 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001450 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001451 return DAG.getNode(ISD::OR, VT, N0, N2);
1452 // fold select C, 0, X -> ~C & X
1453 // FIXME: this should check for C type == X type, not i1?
1454 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1455 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1456 WorkList.push_back(XORNode.Val);
1457 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1458 }
1459 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001460 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001461 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1462 WorkList.push_back(XORNode.Val);
1463 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1464 }
1465 // fold select C, X, 0 -> C & X
1466 // FIXME: this should check for C type == X type, not i1?
1467 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1468 return DAG.getNode(ISD::AND, VT, N0, N1);
1469 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1470 if (MVT::i1 == VT && N0 == N1)
1471 return DAG.getNode(ISD::OR, VT, N0, N2);
1472 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1473 if (MVT::i1 == VT && N0 == N2)
1474 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001475 // If we can fold this based on the true/false value, do so.
1476 if (SimplifySelectOps(N, N1, N2))
1477 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001478 // fold selects based on a setcc into other things, such as min/max/abs
1479 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001480 // FIXME:
1481 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1482 // having to say they don't support SELECT_CC on every type the DAG knows
1483 // about, since there is no way to mark an opcode illegal at all value types
1484 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1485 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1486 N1, N2, N0.getOperand(2));
1487 else
1488 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001489 return SDOperand();
1490}
1491
1492SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001493 SDOperand N0 = N->getOperand(0);
1494 SDOperand N1 = N->getOperand(1);
1495 SDOperand N2 = N->getOperand(2);
1496 SDOperand N3 = N->getOperand(3);
1497 SDOperand N4 = N->getOperand(4);
1498 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1499 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1500 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1501 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1502
1503 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001504 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001505 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1506
Nate Begeman44728a72005-09-19 22:34:01 +00001507 // fold select_cc lhs, rhs, x, x, cc -> x
1508 if (N2 == N3)
1509 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001510
1511 // If we can fold this based on the true/false value, do so.
1512 if (SimplifySelectOps(N, N2, N3))
1513 return SDOperand();
1514
Nate Begeman44728a72005-09-19 22:34:01 +00001515 // fold select_cc into other things, such as min/max/abs
1516 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001517}
1518
1519SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1520 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1521 cast<CondCodeSDNode>(N->getOperand(2))->get());
1522}
1523
Nate Begeman5054f162005-10-14 01:12:21 +00001524SDOperand DAGCombiner::visitADD_PARTS(SDNode *N) {
1525 SDOperand LHSLo = N->getOperand(0);
1526 SDOperand RHSLo = N->getOperand(2);
1527 MVT::ValueType VT = LHSLo.getValueType();
1528
1529 // fold (a_Hi, 0) + (b_Hi, b_Lo) -> (b_Hi + a_Hi, b_Lo)
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001530 if (TLI.MaskedValueIsZero(LHSLo, (1ULL << MVT::getSizeInBits(VT))-1)) {
Nate Begeman5054f162005-10-14 01:12:21 +00001531 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1532 N->getOperand(3));
1533 WorkList.push_back(Hi.Val);
1534 CombineTo(N, RHSLo, Hi);
1535 return SDOperand();
1536 }
1537 // fold (a_Hi, a_Lo) + (b_Hi, 0) -> (a_Hi + b_Hi, a_Lo)
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001538 if (TLI.MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1)) {
Nate Begeman5054f162005-10-14 01:12:21 +00001539 SDOperand Hi = DAG.getNode(ISD::ADD, VT, N->getOperand(1),
1540 N->getOperand(3));
1541 WorkList.push_back(Hi.Val);
1542 CombineTo(N, LHSLo, Hi);
1543 return SDOperand();
1544 }
1545 return SDOperand();
1546}
1547
1548SDOperand DAGCombiner::visitSUB_PARTS(SDNode *N) {
1549 SDOperand LHSLo = N->getOperand(0);
1550 SDOperand RHSLo = N->getOperand(2);
1551 MVT::ValueType VT = LHSLo.getValueType();
1552
1553 // fold (a_Hi, a_Lo) - (b_Hi, 0) -> (a_Hi - b_Hi, a_Lo)
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001554 if (TLI.MaskedValueIsZero(RHSLo, (1ULL << MVT::getSizeInBits(VT))-1)) {
Nate Begeman5054f162005-10-14 01:12:21 +00001555 SDOperand Hi = DAG.getNode(ISD::SUB, VT, N->getOperand(1),
1556 N->getOperand(3));
1557 WorkList.push_back(Hi.Val);
1558 CombineTo(N, LHSLo, Hi);
1559 return SDOperand();
1560 }
1561 return SDOperand();
1562}
1563
Nate Begeman83e75ec2005-09-06 04:43:02 +00001564SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001565 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001566 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001567 MVT::ValueType VT = N->getValueType(0);
1568
Nate Begeman1d4d4142005-09-01 00:19:25 +00001569 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001570 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001571 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001572 // fold (sext (sext x)) -> (sext x)
1573 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001574 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001575 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001576 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1577 (!AfterLegalize ||
1578 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001579 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1580 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001581 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001582 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1583 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001584 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, 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);
Chris Lattnerf9884052005-10-13 21:52:31 +00001588 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1589 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001590 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001591 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001592
1593 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1594 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1595 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1596 N0.hasOneUse()) {
1597 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, 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 }
1605
Nate Begeman83e75ec2005-09-06 04:43:02 +00001606 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001607}
1608
Nate Begeman83e75ec2005-09-06 04:43:02 +00001609SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001610 SDOperand N0 = N->getOperand(0);
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);
1613
Nate Begeman1d4d4142005-09-01 00:19:25 +00001614 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001615 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001616 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001617 // fold (zext (zext x)) -> (zext x)
1618 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001620 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1621 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001622 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001623 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001624 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001625 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1626 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001627 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1628 N0.getOperand(1), N0.getOperand(2),
1629 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001630 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001631 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1632 ExtLoad.getValue(1));
1633 return SDOperand();
1634 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001635
1636 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1637 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1638 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1639 N0.hasOneUse()) {
1640 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1641 N0.getOperand(1), N0.getOperand(2),
1642 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001643 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001644 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1645 ExtLoad.getValue(1));
1646 return SDOperand();
1647 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001648 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001649}
1650
Nate Begeman83e75ec2005-09-06 04:43:02 +00001651SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001652 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001653 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001654 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001655 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001656 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001657 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001658
Nate Begeman1d4d4142005-09-01 00:19:25 +00001659 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001660 if (N0C) {
1661 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001662 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001663 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001664 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001665 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001666 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001667 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001668 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001669 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1670 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1671 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001672 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001673 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001674 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1675 if (N0.getOpcode() == ISD::AssertSext &&
1676 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001677 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001678 }
1679 // fold (sext_in_reg (sextload x)) -> (sextload x)
1680 if (N0.getOpcode() == ISD::SEXTLOAD &&
1681 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001682 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001683 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001684 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001685 if (N0.getOpcode() == ISD::SETCC &&
1686 TLI.getSetCCResultContents() ==
1687 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001688 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001689 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001690 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001691 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001692 // fold (sext_in_reg (srl x)) -> sra x
1693 if (N0.getOpcode() == ISD::SRL &&
1694 N0.getOperand(1).getOpcode() == ISD::Constant &&
1695 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1696 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1697 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001698 }
Nate Begemanded49632005-10-13 03:11:28 +00001699 // fold (sext_inreg (extload x)) -> (sextload x)
1700 if (N0.getOpcode() == ISD::EXTLOAD &&
1701 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001702 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001703 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1704 N0.getOperand(1), N0.getOperand(2),
1705 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001706 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001707 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001708 return SDOperand();
1709 }
1710 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001711 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001712 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001713 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001714 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1715 N0.getOperand(1), N0.getOperand(2),
1716 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001717 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001718 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001719 return SDOperand();
1720 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001721 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001722}
1723
Nate Begeman83e75ec2005-09-06 04:43:02 +00001724SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001725 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001726 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001727 MVT::ValueType VT = N->getValueType(0);
1728
1729 // noop truncate
1730 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001731 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001732 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001733 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001734 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001735 // fold (truncate (truncate x)) -> (truncate x)
1736 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001737 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001738 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1739 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1740 if (N0.getValueType() < VT)
1741 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001742 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001743 else if (N0.getValueType() > VT)
1744 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001745 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001746 else
1747 // if the source and dest are the same type, we can drop both the extend
1748 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001749 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001750 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001751 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001752 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001753 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1754 "Cannot truncate to larger type!");
1755 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001756 // For big endian targets, we need to add an offset to the pointer to load
1757 // the correct bytes. For little endian systems, we merely need to read
1758 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001759 uint64_t PtrOff =
1760 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001761 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1762 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1763 DAG.getConstant(PtrOff, PtrType));
1764 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001765 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001766 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001767 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001768 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001769 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001770 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001771}
1772
Chris Lattner94683772005-12-23 05:30:37 +00001773SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1774 SDOperand N0 = N->getOperand(0);
1775 MVT::ValueType VT = N->getValueType(0);
1776
1777 // If the input is a constant, let getNode() fold it.
1778 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1779 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1780 if (Res.Val != N) return Res;
1781 }
1782
Chris Lattnerc8547d82005-12-23 05:37:50 +00001783 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1784 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1785
Chris Lattner57104102005-12-23 05:44:41 +00001786 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001787 // FIXME: These xforms need to know that the resultant load doesn't need a
1788 // higher alignment than the original!
1789 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001790 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1791 N0.getOperand(2));
1792 WorkList.push_back(N);
1793 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1794 Load.getValue(1));
1795 return Load;
1796 }
1797
Chris Lattner94683772005-12-23 05:30:37 +00001798 return SDOperand();
1799}
1800
Chris Lattner01b3d732005-09-28 22:28:18 +00001801SDOperand DAGCombiner::visitFADD(SDNode *N) {
1802 SDOperand N0 = N->getOperand(0);
1803 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001804 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1805 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001806 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001807
1808 // fold (fadd c1, c2) -> c1+c2
1809 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001810 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001811 // canonicalize constant to RHS
1812 if (N0CFP && !N1CFP)
1813 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001814 // fold (A + (-B)) -> A-B
1815 if (N1.getOpcode() == ISD::FNEG)
1816 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001817 // fold ((-A) + B) -> B-A
1818 if (N0.getOpcode() == ISD::FNEG)
1819 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001820 return SDOperand();
1821}
1822
1823SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1824 SDOperand N0 = N->getOperand(0);
1825 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001826 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1827 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001828 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001829
1830 // fold (fsub c1, c2) -> c1-c2
1831 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001832 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001833 // fold (A-(-B)) -> A+B
1834 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001835 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001836 return SDOperand();
1837}
1838
1839SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1840 SDOperand N0 = N->getOperand(0);
1841 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001842 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1843 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001844 MVT::ValueType VT = N->getValueType(0);
1845
Nate Begeman11af4ea2005-10-17 20:40:11 +00001846 // fold (fmul c1, c2) -> c1*c2
1847 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001848 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001849 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001850 if (N0CFP && !N1CFP)
1851 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001852 // fold (fmul X, 2.0) -> (fadd X, X)
1853 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1854 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001855 return SDOperand();
1856}
1857
1858SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1859 SDOperand N0 = N->getOperand(0);
1860 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001861 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1862 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001863 MVT::ValueType VT = N->getValueType(0);
1864
Nate Begemana148d982006-01-18 22:35:16 +00001865 // fold (fdiv c1, c2) -> c1/c2
1866 if (N0CFP && N1CFP)
1867 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001868 return SDOperand();
1869}
1870
1871SDOperand DAGCombiner::visitFREM(SDNode *N) {
1872 SDOperand N0 = N->getOperand(0);
1873 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001874 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1875 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001876 MVT::ValueType VT = N->getValueType(0);
1877
Nate Begemana148d982006-01-18 22:35:16 +00001878 // fold (frem c1, c2) -> fmod(c1,c2)
1879 if (N0CFP && N1CFP)
1880 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001881 return SDOperand();
1882}
1883
1884
Nate Begeman83e75ec2005-09-06 04:43:02 +00001885SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001886 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001887 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001888 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001889
1890 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001891 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001892 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001893 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001894}
1895
Nate Begeman83e75ec2005-09-06 04:43:02 +00001896SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001897 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001898 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001899 MVT::ValueType VT = N->getValueType(0);
1900
Nate Begeman1d4d4142005-09-01 00:19:25 +00001901 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001902 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001903 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001904 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001905}
1906
Nate Begeman83e75ec2005-09-06 04:43:02 +00001907SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001908 SDOperand N0 = N->getOperand(0);
1909 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1910 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001911
1912 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001913 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001914 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001915 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001916}
1917
Nate Begeman83e75ec2005-09-06 04:43:02 +00001918SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001919 SDOperand N0 = N->getOperand(0);
1920 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1921 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001922
1923 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001924 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001925 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001926 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001927}
1928
Nate Begeman83e75ec2005-09-06 04:43:02 +00001929SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001930 SDOperand N0 = N->getOperand(0);
1931 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1932 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001933
1934 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001935 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001936 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001937 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001938}
1939
Nate Begeman83e75ec2005-09-06 04:43:02 +00001940SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941 SDOperand N0 = N->getOperand(0);
1942 MVT::ValueType VT = N->getValueType(0);
1943 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001944 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945
Nate Begeman1d4d4142005-09-01 00:19:25 +00001946 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001947 if (N0CFP) {
1948 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001949 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001950 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001951 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001952}
1953
Nate Begeman83e75ec2005-09-06 04:43:02 +00001954SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001955 SDOperand N0 = N->getOperand(0);
1956 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1957 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958
1959 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001960 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001961 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001962 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001963}
1964
Nate Begeman83e75ec2005-09-06 04:43:02 +00001965SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001966 SDOperand N0 = N->getOperand(0);
1967 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1968 MVT::ValueType VT = N->getValueType(0);
1969
1970 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001971 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001972 return DAG.getNode(ISD::FNEG, VT, N0);
1973 // fold (fneg (sub x, y)) -> (sub y, x)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001974 if (N->getOperand(0).getOpcode() == ISD::SUB)
Nate Begemana148d982006-01-18 22:35:16 +00001975 return DAG.getNode(ISD::SUB, VT, N->getOperand(1), N->getOperand(0));
1976 // fold (fneg (fneg x)) -> x
Nate Begeman1d4d4142005-09-01 00:19:25 +00001977 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001978 return N->getOperand(0).getOperand(0);
1979 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001980}
1981
Nate Begeman83e75ec2005-09-06 04:43:02 +00001982SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001983 SDOperand N0 = N->getOperand(0);
1984 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1985 MVT::ValueType VT = N->getValueType(0);
1986
Nate Begeman1d4d4142005-09-01 00:19:25 +00001987 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001988 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001989 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001990 // fold (fabs (fabs x)) -> (fabs x)
1991 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001992 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001993 // fold (fabs (fneg x)) -> (fabs x)
1994 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001995 return DAG.getNode(ISD::FABS, VT, N->getOperand(0).getOperand(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +00001996 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001997}
1998
Nate Begeman44728a72005-09-19 22:34:01 +00001999SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2000 SDOperand Chain = N->getOperand(0);
2001 SDOperand N1 = N->getOperand(1);
2002 SDOperand N2 = N->getOperand(2);
2003 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2004
2005 // never taken branch, fold to chain
2006 if (N1C && N1C->isNullValue())
2007 return Chain;
2008 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002009 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002010 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002011 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2012 // on the target.
2013 if (N1.getOpcode() == ISD::SETCC &&
2014 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2015 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2016 N1.getOperand(0), N1.getOperand(1), N2);
2017 }
Nate Begeman44728a72005-09-19 22:34:01 +00002018 return SDOperand();
2019}
2020
2021SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
2022 SDOperand Chain = N->getOperand(0);
2023 SDOperand N1 = N->getOperand(1);
2024 SDOperand N2 = N->getOperand(2);
2025 SDOperand N3 = N->getOperand(3);
2026 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2027
2028 // unconditional branch to true mbb
2029 if (N1C && N1C->getValue() == 1)
2030 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2031 // unconditional branch to false mbb
2032 if (N1C && N1C->isNullValue())
2033 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002034 // fold a brcondtwoway with a setcc condition into a BRTWOWAY_CC node if
2035 // BRTWOWAY_CC is legal on the target.
2036 if (N1.getOpcode() == ISD::SETCC &&
2037 TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
2038 std::vector<SDOperand> Ops;
2039 Ops.push_back(Chain);
2040 Ops.push_back(N1.getOperand(2));
2041 Ops.push_back(N1.getOperand(0));
2042 Ops.push_back(N1.getOperand(1));
2043 Ops.push_back(N2);
2044 Ops.push_back(N3);
2045 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2046 }
Nate Begeman44728a72005-09-19 22:34:01 +00002047 return SDOperand();
2048}
2049
Chris Lattner3ea0b472005-10-05 06:47:48 +00002050// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2051//
Nate Begeman44728a72005-09-19 22:34:01 +00002052SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002053 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2054 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2055
2056 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002057 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2058 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2059
2060 // fold br_cc true, dest -> br dest (unconditional branch)
2061 if (SCCC && SCCC->getValue())
2062 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2063 N->getOperand(4));
2064 // fold br_cc false, dest -> unconditional fall through
2065 if (SCCC && SCCC->isNullValue())
2066 return N->getOperand(0);
2067 // fold to a simpler setcc
2068 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2069 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2070 Simp.getOperand(2), Simp.getOperand(0),
2071 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002072 return SDOperand();
2073}
2074
2075SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00002076 SDOperand Chain = N->getOperand(0);
2077 SDOperand CCN = N->getOperand(1);
2078 SDOperand LHS = N->getOperand(2);
2079 SDOperand RHS = N->getOperand(3);
2080 SDOperand N4 = N->getOperand(4);
2081 SDOperand N5 = N->getOperand(5);
2082
2083 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
2084 cast<CondCodeSDNode>(CCN)->get(), false);
2085 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2086
2087 // fold select_cc lhs, rhs, x, x, cc -> x
2088 if (N4 == N5)
2089 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2090 // fold select_cc true, x, y -> x
2091 if (SCCC && SCCC->getValue())
2092 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2093 // fold select_cc false, x, y -> y
2094 if (SCCC && SCCC->isNullValue())
2095 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
2096 // fold to a simpler setcc
Chris Lattner03d5e872006-01-29 06:00:45 +00002097 if (SCC.Val && SCC.getOpcode() == ISD::SETCC) {
2098 std::vector<SDOperand> Ops;
2099 Ops.push_back(Chain);
2100 Ops.push_back(SCC.getOperand(2));
2101 Ops.push_back(SCC.getOperand(0));
2102 Ops.push_back(SCC.getOperand(1));
2103 Ops.push_back(N4);
2104 Ops.push_back(N5);
2105 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2106 }
Nate Begeman44728a72005-09-19 22:34:01 +00002107 return SDOperand();
2108}
2109
Chris Lattner01a22022005-10-10 22:04:48 +00002110SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2111 SDOperand Chain = N->getOperand(0);
2112 SDOperand Ptr = N->getOperand(1);
2113 SDOperand SrcValue = N->getOperand(2);
2114
2115 // If this load is directly stored, replace the load value with the stored
2116 // value.
2117 // TODO: Handle store large -> read small portion.
2118 // TODO: Handle TRUNCSTORE/EXTLOAD
2119 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2120 Chain.getOperand(1).getValueType() == N->getValueType(0))
2121 return CombineTo(N, Chain.getOperand(1), Chain);
2122
2123 return SDOperand();
2124}
2125
Chris Lattner87514ca2005-10-10 22:31:19 +00002126SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2127 SDOperand Chain = N->getOperand(0);
2128 SDOperand Value = N->getOperand(1);
2129 SDOperand Ptr = N->getOperand(2);
2130 SDOperand SrcValue = N->getOperand(3);
2131
2132 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002133 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002134 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2135 // Make sure that these stores are the same value type:
2136 // FIXME: we really care that the second store is >= size of the first.
2137 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002138 // Create a new store of Value that replaces both stores.
2139 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002140 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2141 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002142 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2143 PrevStore->getOperand(0), Value, Ptr,
2144 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002145 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002146 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002147 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002148 }
2149
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002150 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002151 // FIXME: This needs to know that the resultant store does not need a
2152 // higher alignment than the original.
2153 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002154 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2155 Ptr, SrcValue);
2156
Chris Lattner87514ca2005-10-10 22:31:19 +00002157 return SDOperand();
2158}
2159
Nate Begeman44728a72005-09-19 22:34:01 +00002160SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002161 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2162
2163 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2164 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2165 // If we got a simplified select_cc node back from SimplifySelectCC, then
2166 // break it down into a new SETCC node, and a new SELECT node, and then return
2167 // the SELECT node, since we were called with a SELECT node.
2168 if (SCC.Val) {
2169 // Check to see if we got a select_cc back (to turn into setcc/select).
2170 // Otherwise, just return whatever node we got back, like fabs.
2171 if (SCC.getOpcode() == ISD::SELECT_CC) {
2172 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2173 SCC.getOperand(0), SCC.getOperand(1),
2174 SCC.getOperand(4));
2175 WorkList.push_back(SETCC.Val);
2176 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2177 SCC.getOperand(3), SETCC);
2178 }
2179 return SCC;
2180 }
Nate Begeman44728a72005-09-19 22:34:01 +00002181 return SDOperand();
2182}
2183
Chris Lattner40c62d52005-10-18 06:04:22 +00002184/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2185/// are the two values being selected between, see if we can simplify the
2186/// select.
2187///
2188bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2189 SDOperand RHS) {
2190
2191 // If this is a select from two identical things, try to pull the operation
2192 // through the select.
2193 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2194#if 0
2195 std::cerr << "SELECT: ["; LHS.Val->dump();
2196 std::cerr << "] ["; RHS.Val->dump();
2197 std::cerr << "]\n";
2198#endif
2199
2200 // If this is a load and the token chain is identical, replace the select
2201 // of two loads with a load through a select of the address to load from.
2202 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2203 // constants have been dropped into the constant pool.
2204 if ((LHS.getOpcode() == ISD::LOAD ||
2205 LHS.getOpcode() == ISD::EXTLOAD ||
2206 LHS.getOpcode() == ISD::ZEXTLOAD ||
2207 LHS.getOpcode() == ISD::SEXTLOAD) &&
2208 // Token chains must be identical.
2209 LHS.getOperand(0) == RHS.getOperand(0) &&
2210 // If this is an EXTLOAD, the VT's must match.
2211 (LHS.getOpcode() == ISD::LOAD ||
2212 LHS.getOperand(3) == RHS.getOperand(3))) {
2213 // FIXME: this conflates two src values, discarding one. This is not
2214 // the right thing to do, but nothing uses srcvalues now. When they do,
2215 // turn SrcValue into a list of locations.
2216 SDOperand Addr;
2217 if (TheSelect->getOpcode() == ISD::SELECT)
2218 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2219 TheSelect->getOperand(0), LHS.getOperand(1),
2220 RHS.getOperand(1));
2221 else
2222 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2223 TheSelect->getOperand(0),
2224 TheSelect->getOperand(1),
2225 LHS.getOperand(1), RHS.getOperand(1),
2226 TheSelect->getOperand(4));
2227
2228 SDOperand Load;
2229 if (LHS.getOpcode() == ISD::LOAD)
2230 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2231 Addr, LHS.getOperand(2));
2232 else
2233 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2234 LHS.getOperand(0), Addr, LHS.getOperand(2),
2235 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2236 // Users of the select now use the result of the load.
2237 CombineTo(TheSelect, Load);
2238
2239 // Users of the old loads now use the new load's chain. We know the
2240 // old-load value is dead now.
2241 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2242 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2243 return true;
2244 }
2245 }
2246
2247 return false;
2248}
2249
Nate Begeman44728a72005-09-19 22:34:01 +00002250SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2251 SDOperand N2, SDOperand N3,
2252 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002253
2254 MVT::ValueType VT = N2.getValueType();
2255 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2256 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2257 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2258 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2259
2260 // Determine if the condition we're dealing with is constant
2261 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2262 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2263
2264 // fold select_cc true, x, y -> x
2265 if (SCCC && SCCC->getValue())
2266 return N2;
2267 // fold select_cc false, x, y -> y
2268 if (SCCC && SCCC->getValue() == 0)
2269 return N3;
2270
2271 // Check to see if we can simplify the select into an fabs node
2272 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2273 // Allow either -0.0 or 0.0
2274 if (CFP->getValue() == 0.0) {
2275 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2276 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2277 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2278 N2 == N3.getOperand(0))
2279 return DAG.getNode(ISD::FABS, VT, N0);
2280
2281 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2282 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2283 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2284 N2.getOperand(0) == N3)
2285 return DAG.getNode(ISD::FABS, VT, N3);
2286 }
2287 }
2288
2289 // Check to see if we can perform the "gzip trick", transforming
2290 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2291 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2292 MVT::isInteger(N0.getValueType()) &&
2293 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2294 MVT::ValueType XType = N0.getValueType();
2295 MVT::ValueType AType = N2.getValueType();
2296 if (XType >= AType) {
2297 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002298 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002299 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2300 unsigned ShCtV = Log2_64(N2C->getValue());
2301 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2302 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2303 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
2304 WorkList.push_back(Shift.Val);
2305 if (XType > AType) {
2306 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2307 WorkList.push_back(Shift.Val);
2308 }
2309 return DAG.getNode(ISD::AND, AType, Shift, N2);
2310 }
2311 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2312 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2313 TLI.getShiftAmountTy()));
2314 WorkList.push_back(Shift.Val);
2315 if (XType > AType) {
2316 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2317 WorkList.push_back(Shift.Val);
2318 }
2319 return DAG.getNode(ISD::AND, AType, Shift, N2);
2320 }
2321 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002322
2323 // fold select C, 16, 0 -> shl C, 4
2324 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2325 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2326 // Get a SetCC of the condition
2327 // FIXME: Should probably make sure that setcc is legal if we ever have a
2328 // target where it isn't.
2329 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2330 WorkList.push_back(SCC.Val);
2331 // cast from setcc result type to select result type
2332 if (AfterLegalize)
2333 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
2334 else
2335 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
2336 WorkList.push_back(Temp.Val);
2337 // shl setcc result by log2 n2c
2338 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2339 DAG.getConstant(Log2_64(N2C->getValue()),
2340 TLI.getShiftAmountTy()));
2341 }
2342
Nate Begemanf845b452005-10-08 00:29:44 +00002343 // Check to see if this is the equivalent of setcc
2344 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2345 // otherwise, go ahead with the folds.
2346 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2347 MVT::ValueType XType = N0.getValueType();
2348 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2349 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2350 if (Res.getValueType() != VT)
2351 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2352 return Res;
2353 }
2354
2355 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2356 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2357 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2358 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2359 return DAG.getNode(ISD::SRL, XType, Ctlz,
2360 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2361 TLI.getShiftAmountTy()));
2362 }
2363 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2364 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2365 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2366 N0);
2367 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2368 DAG.getConstant(~0ULL, XType));
2369 return DAG.getNode(ISD::SRL, XType,
2370 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2371 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2372 TLI.getShiftAmountTy()));
2373 }
2374 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2375 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2376 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2377 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2378 TLI.getShiftAmountTy()));
2379 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2380 }
2381 }
2382
2383 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2384 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2385 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2386 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2387 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2388 MVT::ValueType XType = N0.getValueType();
2389 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2390 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2391 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2392 TLI.getShiftAmountTy()));
2393 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
2394 WorkList.push_back(Shift.Val);
2395 WorkList.push_back(Add.Val);
2396 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2397 }
2398 }
2399 }
2400
Nate Begeman44728a72005-09-19 22:34:01 +00002401 return SDOperand();
2402}
2403
Nate Begeman452d7be2005-09-16 00:54:12 +00002404SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002405 SDOperand N1, ISD::CondCode Cond,
2406 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002407 // These setcc operations always fold.
2408 switch (Cond) {
2409 default: break;
2410 case ISD::SETFALSE:
2411 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2412 case ISD::SETTRUE:
2413 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2414 }
2415
2416 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2417 uint64_t C1 = N1C->getValue();
2418 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2419 uint64_t C0 = N0C->getValue();
2420
2421 // Sign extend the operands if required
2422 if (ISD::isSignedIntSetCC(Cond)) {
2423 C0 = N0C->getSignExtended();
2424 C1 = N1C->getSignExtended();
2425 }
2426
2427 switch (Cond) {
2428 default: assert(0 && "Unknown integer setcc!");
2429 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2430 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2431 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2432 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2433 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2434 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2435 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2436 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2437 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2438 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2439 }
2440 } else {
2441 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2442 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2443 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2444
2445 // If the comparison constant has bits in the upper part, the
2446 // zero-extended value could never match.
2447 if (C1 & (~0ULL << InSize)) {
2448 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2449 switch (Cond) {
2450 case ISD::SETUGT:
2451 case ISD::SETUGE:
2452 case ISD::SETEQ: return DAG.getConstant(0, VT);
2453 case ISD::SETULT:
2454 case ISD::SETULE:
2455 case ISD::SETNE: return DAG.getConstant(1, VT);
2456 case ISD::SETGT:
2457 case ISD::SETGE:
2458 // True if the sign bit of C1 is set.
2459 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2460 case ISD::SETLT:
2461 case ISD::SETLE:
2462 // True if the sign bit of C1 isn't set.
2463 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2464 default:
2465 break;
2466 }
2467 }
2468
2469 // Otherwise, we can perform the comparison with the low bits.
2470 switch (Cond) {
2471 case ISD::SETEQ:
2472 case ISD::SETNE:
2473 case ISD::SETUGT:
2474 case ISD::SETUGE:
2475 case ISD::SETULT:
2476 case ISD::SETULE:
2477 return DAG.getSetCC(VT, N0.getOperand(0),
2478 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2479 Cond);
2480 default:
2481 break; // todo, be more careful with signed comparisons
2482 }
2483 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2484 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2485 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2486 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2487 MVT::ValueType ExtDstTy = N0.getValueType();
2488 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2489
2490 // If the extended part has any inconsistent bits, it cannot ever
2491 // compare equal. In other words, they have to be all ones or all
2492 // zeros.
2493 uint64_t ExtBits =
2494 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2495 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2496 return DAG.getConstant(Cond == ISD::SETNE, VT);
2497
2498 SDOperand ZextOp;
2499 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2500 if (Op0Ty == ExtSrcTy) {
2501 ZextOp = N0.getOperand(0);
2502 } else {
2503 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2504 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2505 DAG.getConstant(Imm, Op0Ty));
2506 }
2507 WorkList.push_back(ZextOp.Val);
2508 // Otherwise, make this a use of a zext.
2509 return DAG.getSetCC(VT, ZextOp,
2510 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2511 ExtDstTy),
2512 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002513 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2514 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2515 (N0.getOpcode() == ISD::XOR ||
2516 (N0.getOpcode() == ISD::AND &&
2517 N0.getOperand(0).getOpcode() == ISD::XOR &&
2518 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2519 isa<ConstantSDNode>(N0.getOperand(1)) &&
2520 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2521 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2522 // only do this if the top bits are known zero.
2523 if (TLI.MaskedValueIsZero(N1,
2524 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2525 // Okay, get the un-inverted input value.
2526 SDOperand Val;
2527 if (N0.getOpcode() == ISD::XOR)
2528 Val = N0.getOperand(0);
2529 else {
2530 assert(N0.getOpcode() == ISD::AND &&
2531 N0.getOperand(0).getOpcode() == ISD::XOR);
2532 // ((X^1)&1)^1 -> X & 1
2533 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2534 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2535 }
2536 return DAG.getSetCC(VT, Val, N1,
2537 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2538 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002539 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002540
Nate Begeman452d7be2005-09-16 00:54:12 +00002541 uint64_t MinVal, MaxVal;
2542 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2543 if (ISD::isSignedIntSetCC(Cond)) {
2544 MinVal = 1ULL << (OperandBitSize-1);
2545 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2546 MaxVal = ~0ULL >> (65-OperandBitSize);
2547 else
2548 MaxVal = 0;
2549 } else {
2550 MinVal = 0;
2551 MaxVal = ~0ULL >> (64-OperandBitSize);
2552 }
2553
2554 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2555 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2556 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2557 --C1; // X >= C0 --> X > (C0-1)
2558 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2559 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2560 }
2561
2562 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2563 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2564 ++C1; // X <= C0 --> X < (C0+1)
2565 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2566 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2567 }
2568
2569 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2570 return DAG.getConstant(0, VT); // X < MIN --> false
2571
2572 // Canonicalize setgt X, Min --> setne X, Min
2573 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2574 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002575 // Canonicalize setlt X, Max --> setne X, Max
2576 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2577 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002578
2579 // If we have setult X, 1, turn it into seteq X, 0
2580 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2581 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2582 ISD::SETEQ);
2583 // If we have setugt X, Max-1, turn it into seteq X, Max
2584 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2585 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2586 ISD::SETEQ);
2587
2588 // If we have "setcc X, C0", check to see if we can shrink the immediate
2589 // by changing cc.
2590
2591 // SETUGT X, SINTMAX -> SETLT X, 0
2592 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2593 C1 == (~0ULL >> (65-OperandBitSize)))
2594 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2595 ISD::SETLT);
2596
2597 // FIXME: Implement the rest of these.
2598
2599 // Fold bit comparisons when we can.
2600 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2601 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2602 if (ConstantSDNode *AndRHS =
2603 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2604 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2605 // Perform the xform if the AND RHS is a single bit.
2606 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2607 return DAG.getNode(ISD::SRL, VT, N0,
2608 DAG.getConstant(Log2_64(AndRHS->getValue()),
2609 TLI.getShiftAmountTy()));
2610 }
2611 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2612 // (X & 8) == 8 --> (X & 8) >> 3
2613 // Perform the xform if C1 is a single bit.
2614 if ((C1 & (C1-1)) == 0) {
2615 return DAG.getNode(ISD::SRL, VT, N0,
2616 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2617 }
2618 }
2619 }
2620 }
2621 } else if (isa<ConstantSDNode>(N0.Val)) {
2622 // Ensure that the constant occurs on the RHS.
2623 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2624 }
2625
2626 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2627 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2628 double C0 = N0C->getValue(), C1 = N1C->getValue();
2629
2630 switch (Cond) {
2631 default: break; // FIXME: Implement the rest of these!
2632 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2633 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2634 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2635 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2636 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2637 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2638 }
2639 } else {
2640 // Ensure that the constant occurs on the RHS.
2641 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2642 }
2643
2644 if (N0 == N1) {
2645 // We can always fold X == Y for integer setcc's.
2646 if (MVT::isInteger(N0.getValueType()))
2647 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2648 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2649 if (UOF == 2) // FP operators that are undefined on NaNs.
2650 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2651 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2652 return DAG.getConstant(UOF, VT);
2653 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2654 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002655 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002656 if (NewCond != Cond)
2657 return DAG.getSetCC(VT, N0, N1, NewCond);
2658 }
2659
2660 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2661 MVT::isInteger(N0.getValueType())) {
2662 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2663 N0.getOpcode() == ISD::XOR) {
2664 // Simplify (X+Y) == (X+Z) --> Y == Z
2665 if (N0.getOpcode() == N1.getOpcode()) {
2666 if (N0.getOperand(0) == N1.getOperand(0))
2667 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2668 if (N0.getOperand(1) == N1.getOperand(1))
2669 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2670 if (isCommutativeBinOp(N0.getOpcode())) {
2671 // If X op Y == Y op X, try other combinations.
2672 if (N0.getOperand(0) == N1.getOperand(1))
2673 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2674 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002675 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002676 }
2677 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002678
2679 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2680 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2681 // Turn (X+C1) == C2 --> X == C2-C1
2682 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2683 return DAG.getSetCC(VT, N0.getOperand(0),
2684 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2685 N0.getValueType()), Cond);
2686 }
2687
2688 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2689 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002690 // If we know that all of the inverted bits are zero, don't bother
2691 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002692 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002693 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002694 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002695 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002696 }
2697
2698 // Turn (C1-X) == C2 --> X == C1-C2
2699 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2700 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
2701 return DAG.getSetCC(VT, N0.getOperand(1),
2702 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
2703 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00002704 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002705 }
2706 }
2707
Nate Begeman452d7be2005-09-16 00:54:12 +00002708 // Simplify (X+Z) == X --> Z == 0
2709 if (N0.getOperand(0) == N1)
2710 return DAG.getSetCC(VT, N0.getOperand(1),
2711 DAG.getConstant(0, N0.getValueType()), Cond);
2712 if (N0.getOperand(1) == N1) {
2713 if (isCommutativeBinOp(N0.getOpcode()))
2714 return DAG.getSetCC(VT, N0.getOperand(0),
2715 DAG.getConstant(0, N0.getValueType()), Cond);
2716 else {
2717 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2718 // (Z-X) == X --> Z == X<<1
2719 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2720 N1,
2721 DAG.getConstant(1,TLI.getShiftAmountTy()));
2722 WorkList.push_back(SH.Val);
2723 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2724 }
2725 }
2726 }
2727
2728 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2729 N1.getOpcode() == ISD::XOR) {
2730 // Simplify X == (X+Z) --> Z == 0
2731 if (N1.getOperand(0) == N0) {
2732 return DAG.getSetCC(VT, N1.getOperand(1),
2733 DAG.getConstant(0, N1.getValueType()), Cond);
2734 } else if (N1.getOperand(1) == N0) {
2735 if (isCommutativeBinOp(N1.getOpcode())) {
2736 return DAG.getSetCC(VT, N1.getOperand(0),
2737 DAG.getConstant(0, N1.getValueType()), Cond);
2738 } else {
2739 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2740 // X == (Z-X) --> X<<1 == Z
2741 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2742 DAG.getConstant(1,TLI.getShiftAmountTy()));
2743 WorkList.push_back(SH.Val);
2744 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2745 }
2746 }
2747 }
2748 }
2749
2750 // Fold away ALL boolean setcc's.
2751 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002752 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002753 switch (Cond) {
2754 default: assert(0 && "Unknown integer setcc!");
2755 case ISD::SETEQ: // X == Y -> (X^Y)^1
2756 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2757 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2758 WorkList.push_back(Temp.Val);
2759 break;
2760 case ISD::SETNE: // X != Y --> (X^Y)
2761 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2762 break;
2763 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2764 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2765 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2766 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2767 WorkList.push_back(Temp.Val);
2768 break;
2769 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2770 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2771 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2772 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2773 WorkList.push_back(Temp.Val);
2774 break;
2775 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2776 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2777 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2778 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2779 WorkList.push_back(Temp.Val);
2780 break;
2781 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2782 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2783 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2784 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2785 break;
2786 }
2787 if (VT != MVT::i1) {
2788 WorkList.push_back(N0.Val);
2789 // FIXME: If running after legalize, we probably can't do this.
2790 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2791 }
2792 return N0;
2793 }
2794
2795 // Could not fold it.
2796 return SDOperand();
2797}
2798
Nate Begeman69575232005-10-20 02:15:44 +00002799/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2800/// return a DAG expression to select that will generate the same value by
2801/// multiplying by a magic number. See:
2802/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2803SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2804 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002805
2806 // Check to see if we can do this.
2807 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2808 return SDOperand(); // BuildSDIV only operates on i32 or i64
2809 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2810 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002811
Nate Begemanc6a454e2005-10-20 17:45:03 +00002812 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002813 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2814
2815 // Multiply the numerator (operand 0) by the magic value
2816 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2817 DAG.getConstant(magics.m, VT));
2818 // If d > 0 and m < 0, add the numerator
2819 if (d > 0 && magics.m < 0) {
2820 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2821 WorkList.push_back(Q.Val);
2822 }
2823 // If d < 0 and m > 0, subtract the numerator.
2824 if (d < 0 && magics.m > 0) {
2825 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2826 WorkList.push_back(Q.Val);
2827 }
2828 // Shift right algebraic if shift value is nonzero
2829 if (magics.s > 0) {
2830 Q = DAG.getNode(ISD::SRA, VT, Q,
2831 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2832 WorkList.push_back(Q.Val);
2833 }
2834 // Extract the sign bit and add it to the quotient
2835 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002836 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2837 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +00002838 WorkList.push_back(T.Val);
2839 return DAG.getNode(ISD::ADD, VT, Q, T);
2840}
2841
2842/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2843/// return a DAG expression to select that will generate the same value by
2844/// multiplying by a magic number. See:
2845/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2846SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2847 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002848
2849 // Check to see if we can do this.
2850 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2851 return SDOperand(); // BuildUDIV only operates on i32 or i64
2852 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2853 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002854
2855 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2856 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2857
2858 // Multiply the numerator (operand 0) by the magic value
2859 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2860 DAG.getConstant(magics.m, VT));
2861 WorkList.push_back(Q.Val);
2862
2863 if (magics.a == 0) {
2864 return DAG.getNode(ISD::SRL, VT, Q,
2865 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2866 } else {
2867 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2868 WorkList.push_back(NPQ.Val);
2869 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2870 DAG.getConstant(1, TLI.getShiftAmountTy()));
2871 WorkList.push_back(NPQ.Val);
2872 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2873 WorkList.push_back(NPQ.Val);
2874 return DAG.getNode(ISD::SRL, VT, NPQ,
2875 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
2876 }
2877}
2878
Nate Begeman1d4d4142005-09-01 00:19:25 +00002879// SelectionDAG::Combine - This is the entry point for the file.
2880//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002881void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002882 /// run - This is the main entry point to this class.
2883 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002884 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002885}