blob: 4f7df4f43617399c2384af393098e511a189ae55 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
19// FIXME: Should add a corresponding version of fold AND with
20// ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
21// we don't have yet.
22//
Nate Begeman44728a72005-09-19 22:34:01 +000023// FIXME: select C, pow2, pow2 -> something smart
24// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000025// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000026// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000027// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000028// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000029// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman4ebd8052005-09-01 23:24:04 +000030// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000031// FIXME: verify that getNode can't return extends with an operand whose type
32// is >= to that of the extend.
33// FIXME: divide by zero is currently left unfolded. do we want to turn this
34// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000035// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000036//
37//===----------------------------------------------------------------------===//
38
39#define DEBUG_TYPE "dagcombine"
40#include "llvm/ADT/Statistic.h"
41#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000042#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000043#include "llvm/Support/MathExtras.h"
44#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000045#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000046#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000047#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000048using namespace llvm;
49
50namespace {
51 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
52
53 class DAGCombiner {
54 SelectionDAG &DAG;
55 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000056 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
59 std::vector<SDNode*> WorkList;
60
61 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
63 /// now.
64 ///
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 UI != UE; ++UI)
68 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000069 }
70
71 /// removeFromWorkList - remove all instances of N from the worklist.
72 void removeFromWorkList(SDNode *N) {
73 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
74 WorkList.end());
75 }
76
Chris Lattner01a22022005-10-10 22:04:48 +000077 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000078 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000079 DEBUG(std::cerr << "\nReplacing "; N->dump();
80 std::cerr << "\nWith: "; To[0].Val->dump();
81 std::cerr << " and " << To.size()-1 << " other values\n");
82 std::vector<SDNode*> NowDead;
83 DAG.ReplaceAllUsesWith(N, To, &NowDead);
84
85 // Push the new nodes and any users onto the worklist
86 for (unsigned i = 0, e = To.size(); i != e; ++i) {
87 WorkList.push_back(To[i].Val);
88 AddUsersToWorkList(To[i].Val);
89 }
90
91 // Nodes can end up on the worklist more than once. Make sure we do
92 // not process a node that has been replaced.
93 removeFromWorkList(N);
94 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
95 removeFromWorkList(NowDead[i]);
96
97 // Finally, since the node is now dead, remove it from the graph.
98 DAG.DeleteNode(N);
99 return SDOperand(N, 0);
100 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000101
Chris Lattner012f2412006-02-17 21:58:01 +0000102 /// SimplifyDemandedBits - Check the specified integer node value to see if
103 /// it can be simplified or if things is uses can be simplified by bit
104 /// propagation. If so, return true.
105 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000106 TargetLowering::TargetLoweringOpt TLO(DAG);
107 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000108 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
109 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
110 return false;
111
112 // Revisit the node.
113 WorkList.push_back(Op.Val);
114
115 // Replace the old value with the new one.
116 ++NodesCombined;
117 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
118 std::cerr << "\nWith: "; TLO.New.Val->dump());
119
120 std::vector<SDNode*> NowDead;
121 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
122
123 // Push the new node and any (now) users onto the worklist.
124 WorkList.push_back(TLO.New.Val);
125 AddUsersToWorkList(TLO.New.Val);
126
127 // Nodes can end up on the worklist more than once. Make sure we do
128 // not process a node that has been replaced.
129 removeFromWorkList(TLO.Old.Val);
130 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
131 removeFromWorkList(NowDead[i]);
132
133 // Finally, since the node is now dead, remove it from the graph.
134 DAG.DeleteNode(TLO.Old.Val);
135 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000136 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000137
138 SDOperand CombineTo(SDNode *N, SDOperand Res) {
139 std::vector<SDOperand> To;
140 To.push_back(Res);
141 return CombineTo(N, To);
142 }
Chris Lattner01a22022005-10-10 22:04:48 +0000143
144 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
145 std::vector<SDOperand> To;
146 To.push_back(Res0);
147 To.push_back(Res1);
148 return CombineTo(N, To);
149 }
150
Nate Begeman1d4d4142005-09-01 00:19:25 +0000151 /// visit - call the node-specific routine that knows how to fold each
152 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000153 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000154
155 // Visitation implementation - Implement dag node combining for different
156 // node types. The semantics are as follows:
157 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000158 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000159 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000160 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000161 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000162 SDOperand visitTokenFactor(SDNode *N);
163 SDOperand visitADD(SDNode *N);
164 SDOperand visitSUB(SDNode *N);
165 SDOperand visitMUL(SDNode *N);
166 SDOperand visitSDIV(SDNode *N);
167 SDOperand visitUDIV(SDNode *N);
168 SDOperand visitSREM(SDNode *N);
169 SDOperand visitUREM(SDNode *N);
170 SDOperand visitMULHU(SDNode *N);
171 SDOperand visitMULHS(SDNode *N);
172 SDOperand visitAND(SDNode *N);
173 SDOperand visitOR(SDNode *N);
174 SDOperand visitXOR(SDNode *N);
175 SDOperand visitSHL(SDNode *N);
176 SDOperand visitSRA(SDNode *N);
177 SDOperand visitSRL(SDNode *N);
178 SDOperand visitCTLZ(SDNode *N);
179 SDOperand visitCTTZ(SDNode *N);
180 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000181 SDOperand visitSELECT(SDNode *N);
182 SDOperand visitSELECT_CC(SDNode *N);
183 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000184 SDOperand visitSIGN_EXTEND(SDNode *N);
185 SDOperand visitZERO_EXTEND(SDNode *N);
186 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
187 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000188 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000189 SDOperand visitFADD(SDNode *N);
190 SDOperand visitFSUB(SDNode *N);
191 SDOperand visitFMUL(SDNode *N);
192 SDOperand visitFDIV(SDNode *N);
193 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000194 SDOperand visitSINT_TO_FP(SDNode *N);
195 SDOperand visitUINT_TO_FP(SDNode *N);
196 SDOperand visitFP_TO_SINT(SDNode *N);
197 SDOperand visitFP_TO_UINT(SDNode *N);
198 SDOperand visitFP_ROUND(SDNode *N);
199 SDOperand visitFP_ROUND_INREG(SDNode *N);
200 SDOperand visitFP_EXTEND(SDNode *N);
201 SDOperand visitFNEG(SDNode *N);
202 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000203 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000204 SDOperand visitBRCONDTWOWAY(SDNode *N);
205 SDOperand visitBR_CC(SDNode *N);
206 SDOperand visitBRTWOWAY_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000207 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000208 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000209
Nate Begemancd4d58c2006-02-03 06:46:56 +0000210 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
211
Chris Lattner40c62d52005-10-18 06:04:22 +0000212 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000213 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
214 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
215 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000216 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000217 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000218
219 SDOperand BuildSDIV(SDNode *N);
220 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000221public:
222 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000223 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000224
225 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000226 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000227 };
228}
229
Nate Begeman69575232005-10-20 02:15:44 +0000230struct ms {
231 int64_t m; // magic number
232 int64_t s; // shift amount
233};
234
235struct mu {
236 uint64_t m; // magic number
237 int64_t a; // add indicator
238 int64_t s; // shift amount
239};
240
241/// magic - calculate the magic numbers required to codegen an integer sdiv as
242/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
243/// or -1.
244static ms magic32(int32_t d) {
245 int32_t p;
246 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
247 const uint32_t two31 = 0x80000000U;
248 struct ms mag;
249
250 ad = abs(d);
251 t = two31 + ((uint32_t)d >> 31);
252 anc = t - 1 - t%ad; // absolute value of nc
253 p = 31; // initialize p
254 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
255 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
256 q2 = two31/ad; // initialize q2 = 2p/abs(d)
257 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
258 do {
259 p = p + 1;
260 q1 = 2*q1; // update q1 = 2p/abs(nc)
261 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
262 if (r1 >= anc) { // must be unsigned comparison
263 q1 = q1 + 1;
264 r1 = r1 - anc;
265 }
266 q2 = 2*q2; // update q2 = 2p/abs(d)
267 r2 = 2*r2; // update r2 = rem(2p/abs(d))
268 if (r2 >= ad) { // must be unsigned comparison
269 q2 = q2 + 1;
270 r2 = r2 - ad;
271 }
272 delta = ad - r2;
273 } while (q1 < delta || (q1 == delta && r1 == 0));
274
275 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
276 if (d < 0) mag.m = -mag.m; // resulting magic number
277 mag.s = p - 32; // resulting shift
278 return mag;
279}
280
281/// magicu - calculate the magic numbers required to codegen an integer udiv as
282/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
283static mu magicu32(uint32_t d) {
284 int32_t p;
285 uint32_t nc, delta, q1, r1, q2, r2;
286 struct mu magu;
287 magu.a = 0; // initialize "add" indicator
288 nc = - 1 - (-d)%d;
289 p = 31; // initialize p
290 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
291 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
292 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
293 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
294 do {
295 p = p + 1;
296 if (r1 >= nc - r1 ) {
297 q1 = 2*q1 + 1; // update q1
298 r1 = 2*r1 - nc; // update r1
299 }
300 else {
301 q1 = 2*q1; // update q1
302 r1 = 2*r1; // update r1
303 }
304 if (r2 + 1 >= d - r2) {
305 if (q2 >= 0x7FFFFFFF) magu.a = 1;
306 q2 = 2*q2 + 1; // update q2
307 r2 = 2*r2 + 1 - d; // update r2
308 }
309 else {
310 if (q2 >= 0x80000000) magu.a = 1;
311 q2 = 2*q2; // update q2
312 r2 = 2*r2 + 1; // update r2
313 }
314 delta = d - 1 - r2;
315 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
316 magu.m = q2 + 1; // resulting magic number
317 magu.s = p - 32; // resulting shift
318 return magu;
319}
320
321/// magic - calculate the magic numbers required to codegen an integer sdiv as
322/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
323/// or -1.
324static ms magic64(int64_t d) {
325 int64_t p;
326 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
327 const uint64_t two63 = 9223372036854775808ULL; // 2^63
328 struct ms mag;
329
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000330 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000331 t = two63 + ((uint64_t)d >> 63);
332 anc = t - 1 - t%ad; // absolute value of nc
333 p = 63; // initialize p
334 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
335 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
336 q2 = two63/ad; // initialize q2 = 2p/abs(d)
337 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
338 do {
339 p = p + 1;
340 q1 = 2*q1; // update q1 = 2p/abs(nc)
341 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
342 if (r1 >= anc) { // must be unsigned comparison
343 q1 = q1 + 1;
344 r1 = r1 - anc;
345 }
346 q2 = 2*q2; // update q2 = 2p/abs(d)
347 r2 = 2*r2; // update r2 = rem(2p/abs(d))
348 if (r2 >= ad) { // must be unsigned comparison
349 q2 = q2 + 1;
350 r2 = r2 - ad;
351 }
352 delta = ad - r2;
353 } while (q1 < delta || (q1 == delta && r1 == 0));
354
355 mag.m = q2 + 1;
356 if (d < 0) mag.m = -mag.m; // resulting magic number
357 mag.s = p - 64; // resulting shift
358 return mag;
359}
360
361/// magicu - calculate the magic numbers required to codegen an integer udiv as
362/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
363static mu magicu64(uint64_t d)
364{
365 int64_t p;
366 uint64_t nc, delta, q1, r1, q2, r2;
367 struct mu magu;
368 magu.a = 0; // initialize "add" indicator
369 nc = - 1 - (-d)%d;
370 p = 63; // initialize p
371 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
372 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
373 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
374 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
375 do {
376 p = p + 1;
377 if (r1 >= nc - r1 ) {
378 q1 = 2*q1 + 1; // update q1
379 r1 = 2*r1 - nc; // update r1
380 }
381 else {
382 q1 = 2*q1; // update q1
383 r1 = 2*r1; // update r1
384 }
385 if (r2 + 1 >= d - r2) {
386 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
387 q2 = 2*q2 + 1; // update q2
388 r2 = 2*r2 + 1 - d; // update r2
389 }
390 else {
391 if (q2 >= 0x8000000000000000ull) magu.a = 1;
392 q2 = 2*q2; // update q2
393 r2 = 2*r2 + 1; // update r2
394 }
395 delta = d - 1 - r2;
396 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
397 magu.m = q2 + 1; // resulting magic number
398 magu.s = p - 64; // resulting shift
399 return magu;
400}
401
Nate Begeman4ebd8052005-09-01 23:24:04 +0000402// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
403// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000404// Also, set the incoming LHS, RHS, and CC references to the appropriate
405// nodes based on the type of node we are checking. This simplifies life a
406// bit for the callers.
407static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
408 SDOperand &CC) {
409 if (N.getOpcode() == ISD::SETCC) {
410 LHS = N.getOperand(0);
411 RHS = N.getOperand(1);
412 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000413 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000414 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000415 if (N.getOpcode() == ISD::SELECT_CC &&
416 N.getOperand(2).getOpcode() == ISD::Constant &&
417 N.getOperand(3).getOpcode() == ISD::Constant &&
418 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000419 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
420 LHS = N.getOperand(0);
421 RHS = N.getOperand(1);
422 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000423 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000424 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000425 return false;
426}
427
Nate Begeman99801192005-09-07 23:25:52 +0000428// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
429// one use. If this is true, it allows the users to invert the operation for
430// free when it is profitable to do so.
431static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000432 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000433 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000434 return true;
435 return false;
436}
437
Nate Begeman452d7be2005-09-16 00:54:12 +0000438// FIXME: This should probably go in the ISD class rather than being duplicated
439// in several files.
440static bool isCommutativeBinOp(unsigned Opcode) {
441 switch (Opcode) {
442 case ISD::ADD:
443 case ISD::MUL:
444 case ISD::AND:
445 case ISD::OR:
446 case ISD::XOR: return true;
447 default: return false; // FIXME: Need commutative info for user ops!
448 }
449}
450
Nate Begemancd4d58c2006-02-03 06:46:56 +0000451SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
452 MVT::ValueType VT = N0.getValueType();
453 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
454 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
455 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
456 if (isa<ConstantSDNode>(N1)) {
457 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
458 WorkList.push_back(OpNode.Val);
459 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
460 } else if (N0.hasOneUse()) {
461 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
462 WorkList.push_back(OpNode.Val);
463 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
464 }
465 }
466 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
467 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
468 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
469 if (isa<ConstantSDNode>(N0)) {
470 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
471 WorkList.push_back(OpNode.Val);
472 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
473 } else if (N1.hasOneUse()) {
474 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
475 WorkList.push_back(OpNode.Val);
476 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
477 }
478 }
479 return SDOperand();
480}
481
Nate Begeman4ebd8052005-09-01 23:24:04 +0000482void DAGCombiner::Run(bool RunningAfterLegalize) {
483 // set the instance variable, so that the various visit routines may use it.
484 AfterLegalize = RunningAfterLegalize;
485
Nate Begeman646d7e22005-09-02 21:18:40 +0000486 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000487 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
488 E = DAG.allnodes_end(); I != E; ++I)
489 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000490
Chris Lattner95038592005-10-05 06:35:28 +0000491 // Create a dummy node (which is not added to allnodes), that adds a reference
492 // to the root node, preventing it from being deleted, and tracking any
493 // changes of the root.
494 HandleSDNode Dummy(DAG.getRoot());
495
Nate Begeman1d4d4142005-09-01 00:19:25 +0000496 // while the worklist isn't empty, inspect the node on the end of it and
497 // try and combine it.
498 while (!WorkList.empty()) {
499 SDNode *N = WorkList.back();
500 WorkList.pop_back();
501
502 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000503 // N is deleted from the DAG, since they too may now be dead or may have a
504 // reduced number of uses, allowing other xforms.
505 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000506 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
507 WorkList.push_back(N->getOperand(i).Val);
508
Nate Begeman1d4d4142005-09-01 00:19:25 +0000509 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000510 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000511 continue;
512 }
513
Nate Begeman83e75ec2005-09-06 04:43:02 +0000514 SDOperand RV = visit(N);
515 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000516 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000517 // If we get back the same node we passed in, rather than a new node or
518 // zero, we know that the node must have defined multiple values and
519 // CombineTo was used. Since CombineTo takes care of the worklist
520 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000521 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000522 DEBUG(std::cerr << "\nReplacing "; N->dump();
523 std::cerr << "\nWith: "; RV.Val->dump();
524 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000525 std::vector<SDNode*> NowDead;
526 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000527
528 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000529 WorkList.push_back(RV.Val);
530 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000531
532 // Nodes can end up on the worklist more than once. Make sure we do
533 // not process a node that has been replaced.
534 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000535 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
536 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000537
538 // Finally, since the node is now dead, remove it from the graph.
539 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000540 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000541 }
542 }
Chris Lattner95038592005-10-05 06:35:28 +0000543
544 // If the root changed (e.g. it was a dead load, update the root).
545 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000546}
547
Nate Begeman83e75ec2005-09-06 04:43:02 +0000548SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000549 switch(N->getOpcode()) {
550 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000551 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000552 case ISD::ADD: return visitADD(N);
553 case ISD::SUB: return visitSUB(N);
554 case ISD::MUL: return visitMUL(N);
555 case ISD::SDIV: return visitSDIV(N);
556 case ISD::UDIV: return visitUDIV(N);
557 case ISD::SREM: return visitSREM(N);
558 case ISD::UREM: return visitUREM(N);
559 case ISD::MULHU: return visitMULHU(N);
560 case ISD::MULHS: return visitMULHS(N);
561 case ISD::AND: return visitAND(N);
562 case ISD::OR: return visitOR(N);
563 case ISD::XOR: return visitXOR(N);
564 case ISD::SHL: return visitSHL(N);
565 case ISD::SRA: return visitSRA(N);
566 case ISD::SRL: return visitSRL(N);
567 case ISD::CTLZ: return visitCTLZ(N);
568 case ISD::CTTZ: return visitCTTZ(N);
569 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000570 case ISD::SELECT: return visitSELECT(N);
571 case ISD::SELECT_CC: return visitSELECT_CC(N);
572 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000573 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
574 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
575 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
576 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000577 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000578 case ISD::FADD: return visitFADD(N);
579 case ISD::FSUB: return visitFSUB(N);
580 case ISD::FMUL: return visitFMUL(N);
581 case ISD::FDIV: return visitFDIV(N);
582 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000583 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
584 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
585 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
586 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
587 case ISD::FP_ROUND: return visitFP_ROUND(N);
588 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
589 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
590 case ISD::FNEG: return visitFNEG(N);
591 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000592 case ISD::BRCOND: return visitBRCOND(N);
593 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
594 case ISD::BR_CC: return visitBR_CC(N);
595 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000596 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000597 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000598 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000599 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000600}
601
Nate Begeman83e75ec2005-09-06 04:43:02 +0000602SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000603 std::vector<SDOperand> Ops;
604 bool Changed = false;
605
Nate Begeman1d4d4142005-09-01 00:19:25 +0000606 // If the token factor has two operands and one is the entry token, replace
607 // the token factor with the other operand.
608 if (N->getNumOperands() == 2) {
609 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000610 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000611 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000612 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000614
Nate Begemanded49632005-10-13 03:11:28 +0000615 // fold (tokenfactor (tokenfactor)) -> tokenfactor
616 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
617 SDOperand Op = N->getOperand(i);
618 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
619 Changed = true;
620 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
621 Ops.push_back(Op.getOperand(j));
622 } else {
623 Ops.push_back(Op);
624 }
625 }
626 if (Changed)
627 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000628 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000629}
630
Nate Begeman83e75ec2005-09-06 04:43:02 +0000631SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000632 SDOperand N0 = N->getOperand(0);
633 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000634 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
635 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000636 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000637
638 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000639 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000640 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000641 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000642 if (N0C && !N1C)
643 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000644 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000645 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000646 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000647 // fold ((c1-A)+c2) -> (c1+c2)-A
648 if (N1C && N0.getOpcode() == ISD::SUB)
649 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
650 return DAG.getNode(ISD::SUB, VT,
651 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
652 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000653 // reassociate add
654 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
655 if (RADD.Val != 0)
656 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000657 // fold ((0-A) + B) -> B-A
658 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
659 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000660 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000661 // fold (A + (0-B)) -> A-B
662 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
663 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000664 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000665 // fold (A+(B-A)) -> B
666 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000667 return N1.getOperand(0);
668 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000669}
670
Nate Begeman83e75ec2005-09-06 04:43:02 +0000671SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672 SDOperand N0 = N->getOperand(0);
673 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000674 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
675 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000676 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000677
Chris Lattner854077d2005-10-17 01:07:11 +0000678 // fold (sub x, x) -> 0
679 if (N0 == N1)
680 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000681 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000682 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000683 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000684 // fold (sub x, c) -> (add x, -c)
685 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000686 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000688 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000689 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000690 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000691 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000692 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000693 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000694}
695
Nate Begeman83e75ec2005-09-06 04:43:02 +0000696SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000697 SDOperand N0 = N->getOperand(0);
698 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
700 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000701 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000702
703 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000704 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000705 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000706 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000707 if (N0C && !N1C)
708 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000709 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000711 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000712 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000713 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000714 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000715 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000716 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000717 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000718 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000719 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000720 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
721 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
722 // FIXME: If the input is something that is easily negated (e.g. a
723 // single-use add), we should put the negate there.
724 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
725 DAG.getNode(ISD::SHL, VT, N0,
726 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
727 TLI.getShiftAmountTy())));
728 }
Nate Begemancd4d58c2006-02-03 06:46:56 +0000729 // reassociate mul
730 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
731 if (RMUL.Val != 0)
732 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000733 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000734}
735
Nate Begeman83e75ec2005-09-06 04:43:02 +0000736SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000737 SDOperand N0 = N->getOperand(0);
738 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000739 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
740 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000741 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000742
743 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000744 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000745 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000746 // fold (sdiv X, 1) -> X
747 if (N1C && N1C->getSignExtended() == 1LL)
748 return N0;
749 // fold (sdiv X, -1) -> 0-X
750 if (N1C && N1C->isAllOnesValue())
751 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000752 // If we know the sign bits of both operands are zero, strength reduce to a
753 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
754 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000755 if (TLI.MaskedValueIsZero(N1, SignBit) &&
756 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000757 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000758 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000759 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000760 (isPowerOf2_64(N1C->getSignExtended()) ||
761 isPowerOf2_64(-N1C->getSignExtended()))) {
762 // If dividing by powers of two is cheap, then don't perform the following
763 // fold.
764 if (TLI.isPow2DivCheap())
765 return SDOperand();
766 int64_t pow2 = N1C->getSignExtended();
767 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000768 unsigned lg2 = Log2_64(abs2);
769 // Splat the sign bit into the register
770 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000771 DAG.getConstant(MVT::getSizeInBits(VT)-1,
772 TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000773 WorkList.push_back(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000774 // Add (N0 < 0) ? abs2 - 1 : 0;
775 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
776 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000777 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000778 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
779 WorkList.push_back(SRL.Val);
780 WorkList.push_back(ADD.Val); // Divide by pow2
781 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
782 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000783 // If we're dividing by a positive value, we're done. Otherwise, we must
784 // negate the result.
785 if (pow2 > 0)
786 return SRA;
787 WorkList.push_back(SRA.Val);
788 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
789 }
Nate Begeman69575232005-10-20 02:15:44 +0000790 // if integer divide is expensive and we satisfy the requirements, emit an
791 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000792 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000793 !TLI.isIntDivCheap()) {
794 SDOperand Op = BuildSDIV(N);
795 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000796 }
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::visitUDIV(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.Val);
804 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000805 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000806
807 // fold (udiv 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::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000810 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000811 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000812 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000813 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000814 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000815 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
816 if (N1.getOpcode() == ISD::SHL) {
817 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
818 if (isPowerOf2_64(SHC->getValue())) {
819 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000820 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
821 DAG.getConstant(Log2_64(SHC->getValue()),
822 ADDVT));
823 WorkList.push_back(Add.Val);
824 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000825 }
826 }
827 }
Nate Begeman69575232005-10-20 02:15:44 +0000828 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000829 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
830 SDOperand Op = BuildUDIV(N);
831 if (Op.Val) return Op;
832 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000833 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000834}
835
Nate Begeman83e75ec2005-09-06 04:43:02 +0000836SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000837 SDOperand N0 = N->getOperand(0);
838 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000839 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
840 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000841 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842
843 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000844 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000845 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000846 // If we know the sign bits of both operands are zero, strength reduce to a
847 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
848 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000849 if (TLI.MaskedValueIsZero(N1, SignBit) &&
850 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000851 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000852 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853}
854
Nate Begeman83e75ec2005-09-06 04:43:02 +0000855SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000856 SDOperand N0 = N->getOperand(0);
857 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000858 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
859 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000860 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000861
862 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000863 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000864 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000865 // fold (urem x, pow2) -> (and x, pow2-1)
866 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000867 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000868 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
869 if (N1.getOpcode() == ISD::SHL) {
870 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
871 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000872 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000873 WorkList.push_back(Add.Val);
874 return DAG.getNode(ISD::AND, VT, N0, Add);
875 }
876 }
877 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000878 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000879}
880
Nate Begeman83e75ec2005-09-06 04:43:02 +0000881SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882 SDOperand N0 = N->getOperand(0);
883 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000884 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000885
886 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000887 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000888 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000889 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000890 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000891 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
892 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000893 TLI.getShiftAmountTy()));
894 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000895}
896
Nate Begeman83e75ec2005-09-06 04:43:02 +0000897SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000898 SDOperand N0 = N->getOperand(0);
899 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000900 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000901
902 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000903 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000904 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000905 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000906 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000907 return DAG.getConstant(0, N0.getValueType());
908 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000909}
910
Nate Begeman83e75ec2005-09-06 04:43:02 +0000911SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000912 SDOperand N0 = N->getOperand(0);
913 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000914 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000915 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
916 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000917 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000918 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919
920 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000921 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000922 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000923 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000924 if (N0C && !N1C)
925 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000926 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000927 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000928 return N0;
929 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000930 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000931 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000932 // reassociate and
933 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
934 if (RAND.Val != 0)
935 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000936 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000937 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000940 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000941 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
942 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
943 unsigned InBits = MVT::getSizeInBits(N0.getOperand(0).getValueType());
944 if (TLI.MaskedValueIsZero(N0.getOperand(0),
945 ~N1C->getValue() & ((1ULL << InBits)-1))) {
946 // We actually want to replace all uses of the any_extend with the
947 // zero_extend, to avoid duplicating things. This will later cause this
948 // AND to be folded.
949 CombineTo(N0.Val, DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
950 N0.getOperand(0)));
951 return SDOperand();
952 }
953 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000954 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
955 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
956 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
957 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
958
959 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
960 MVT::isInteger(LL.getValueType())) {
961 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
962 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
963 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
964 WorkList.push_back(ORNode.Val);
965 return DAG.getSetCC(VT, ORNode, LR, Op1);
966 }
967 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
968 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
969 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
970 WorkList.push_back(ANDNode.Val);
971 return DAG.getSetCC(VT, ANDNode, LR, Op1);
972 }
973 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
974 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
975 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
976 WorkList.push_back(ORNode.Val);
977 return DAG.getSetCC(VT, ORNode, LR, Op1);
978 }
979 }
980 // canonicalize equivalent to ll == rl
981 if (LL == RR && LR == RL) {
982 Op1 = ISD::getSetCCSwappedOperands(Op1);
983 std::swap(RL, RR);
984 }
985 if (LL == RL && LR == RR) {
986 bool isInteger = MVT::isInteger(LL.getValueType());
987 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
988 if (Result != ISD::SETCC_INVALID)
989 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
990 }
991 }
992 // fold (and (zext x), (zext y)) -> (zext (and x, y))
993 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
994 N1.getOpcode() == ISD::ZERO_EXTEND &&
995 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
996 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
997 N0.getOperand(0), N1.getOperand(0));
998 WorkList.push_back(ANDNode.Val);
999 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1000 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001001 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001002 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001003 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1004 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001005 N0.getOperand(1) == N1.getOperand(1)) {
1006 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1007 N0.getOperand(0), N1.getOperand(0));
1008 WorkList.push_back(ANDNode.Val);
1009 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1010 }
Nate Begemande996292006-02-03 22:24:05 +00001011 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1012 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner012f2412006-02-17 21:58:01 +00001013 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001014 return SDOperand();
Nate Begemanded49632005-10-13 03:11:28 +00001015 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001016 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001017 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001018 // If we zero all the possible extended bits, then we can turn this into
1019 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001020 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001021 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001022 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1023 N0.getOperand(1), N0.getOperand(2),
1024 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001025 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001026 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001027 return SDOperand();
1028 }
1029 }
1030 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001031 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001032 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001033 // If we zero all the possible extended bits, then we can turn this into
1034 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001035 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001036 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001037 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1038 N0.getOperand(1), N0.getOperand(2),
1039 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001040 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001041 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001042 return SDOperand();
1043 }
1044 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001045 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001046}
1047
Nate Begeman83e75ec2005-09-06 04:43:02 +00001048SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001049 SDOperand N0 = N->getOperand(0);
1050 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001051 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001052 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1053 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001054 MVT::ValueType VT = N1.getValueType();
1055 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001056
1057 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001058 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001059 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001060 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001061 if (N0C && !N1C)
1062 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001063 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001064 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001065 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001066 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001067 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001068 return N1;
1069 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001070 if (N1C &&
1071 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001072 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001073 // reassociate or
1074 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1075 if (ROR.Val != 0)
1076 return ROR;
1077 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1078 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001079 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001080 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1081 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1082 N1),
1083 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001084 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001085 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1086 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1087 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1088 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1089
1090 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1091 MVT::isInteger(LL.getValueType())) {
1092 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1093 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1094 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1095 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1096 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1097 WorkList.push_back(ORNode.Val);
1098 return DAG.getSetCC(VT, ORNode, LR, Op1);
1099 }
1100 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1101 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1102 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1103 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1104 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1105 WorkList.push_back(ANDNode.Val);
1106 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1107 }
1108 }
1109 // canonicalize equivalent to ll == rl
1110 if (LL == RR && LR == RL) {
1111 Op1 = ISD::getSetCCSwappedOperands(Op1);
1112 std::swap(RL, RR);
1113 }
1114 if (LL == RL && LR == RR) {
1115 bool isInteger = MVT::isInteger(LL.getValueType());
1116 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1117 if (Result != ISD::SETCC_INVALID)
1118 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1119 }
1120 }
1121 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1122 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1123 N1.getOpcode() == ISD::ZERO_EXTEND &&
1124 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1125 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1126 N0.getOperand(0), N1.getOperand(0));
1127 WorkList.push_back(ORNode.Val);
1128 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1129 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001130 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1131 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1132 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1133 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1134 N0.getOperand(1) == N1.getOperand(1)) {
1135 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1136 N0.getOperand(0), N1.getOperand(0));
1137 WorkList.push_back(ORNode.Val);
1138 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1139 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001140 // canonicalize shl to left side in a shl/srl pair, to match rotate
1141 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1142 std::swap(N0, N1);
1143 // check for rotl, rotr
1144 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1145 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001146 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001147 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1148 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1149 N1.getOperand(1).getOpcode() == ISD::Constant) {
1150 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1151 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1152 if ((c1val + c2val) == OpSizeInBits)
1153 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1154 }
1155 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1156 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1157 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1158 if (ConstantSDNode *SUBC =
1159 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1160 if (SUBC->getValue() == OpSizeInBits)
1161 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1162 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1163 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1164 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1165 if (ConstantSDNode *SUBC =
1166 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1167 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001168 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001169 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1170 N1.getOperand(1));
1171 else
1172 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1173 N0.getOperand(1));
1174 }
1175 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001176 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001177}
1178
Nate Begeman83e75ec2005-09-06 04:43:02 +00001179SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 SDOperand N0 = N->getOperand(0);
1181 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001182 SDOperand LHS, RHS, CC;
1183 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1184 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001185 MVT::ValueType VT = N0.getValueType();
1186
1187 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001188 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001189 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001190 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001191 if (N0C && !N1C)
1192 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001193 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001194 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001195 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001196 // reassociate xor
1197 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1198 if (RXOR.Val != 0)
1199 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001200 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001201 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1202 bool isInt = MVT::isInteger(LHS.getValueType());
1203 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1204 isInt);
1205 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001206 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001207 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001208 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001209 assert(0 && "Unhandled SetCC Equivalent!");
1210 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001211 }
Nate Begeman99801192005-09-07 23:25:52 +00001212 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1213 if (N1C && N1C->getValue() == 1 &&
1214 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001215 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001216 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1217 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001218 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1219 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001220 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1221 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001222 }
1223 }
Nate Begeman99801192005-09-07 23:25:52 +00001224 // fold !(x or y) -> (!x and !y) iff x or y are constants
1225 if (N1C && N1C->isAllOnesValue() &&
1226 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001227 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001228 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1229 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001230 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1231 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001232 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1233 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234 }
1235 }
Nate Begeman223df222005-09-08 20:18:10 +00001236 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1237 if (N1C && N0.getOpcode() == ISD::XOR) {
1238 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1239 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1240 if (N00C)
1241 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1242 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1243 if (N01C)
1244 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1245 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1246 }
1247 // fold (xor x, x) -> 0
1248 if (N0 == N1)
1249 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001250 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1251 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1252 N1.getOpcode() == ISD::ZERO_EXTEND &&
1253 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1254 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1255 N0.getOperand(0), N1.getOperand(0));
1256 WorkList.push_back(XORNode.Val);
1257 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1258 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001259 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1260 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1261 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1262 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1263 N0.getOperand(1) == N1.getOperand(1)) {
1264 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1265 N0.getOperand(0), N1.getOperand(0));
1266 WorkList.push_back(XORNode.Val);
1267 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1268 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001269 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001270}
1271
Nate Begeman83e75ec2005-09-06 04:43:02 +00001272SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001273 SDOperand N0 = N->getOperand(0);
1274 SDOperand N1 = N->getOperand(1);
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
Nate Begemanfb7217b2006-02-17 19:54:08 +00001293 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001294 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001295 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001296 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001298 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299 N0.getOperand(1).getOpcode() == ISD::Constant) {
1300 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001301 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001302 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001303 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001304 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001305 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001306 }
1307 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1308 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001309 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001310 N0.getOperand(1).getOpcode() == ISD::Constant) {
1311 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001312 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1314 DAG.getConstant(~0ULL << c1, VT));
1315 if (c2 > c1)
1316 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001317 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001318 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001319 return DAG.getNode(ISD::SRL, VT, Mask,
1320 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001321 }
1322 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001323 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001324 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001325 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1326 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001327}
1328
Nate Begeman83e75ec2005-09-06 04:43:02 +00001329SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330 SDOperand N0 = N->getOperand(0);
1331 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001332 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1333 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001334 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001335
1336 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001337 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001338 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001339 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001340 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001341 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001343 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001344 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001345 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001346 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001347 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001349 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001350 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001351 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1352 // sext_inreg.
1353 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1354 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1355 MVT::ValueType EVT;
1356 switch (LowBits) {
1357 default: EVT = MVT::Other; break;
1358 case 1: EVT = MVT::i1; break;
1359 case 8: EVT = MVT::i8; break;
1360 case 16: EVT = MVT::i16; break;
1361 case 32: EVT = MVT::i32; break;
1362 }
1363 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1364 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1365 DAG.getValueType(EVT));
1366 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001368 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001369 return DAG.getNode(ISD::SRL, VT, N0, N1);
1370 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001371}
1372
Nate Begeman83e75ec2005-09-06 04:43:02 +00001373SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374 SDOperand N0 = N->getOperand(0);
1375 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001376 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1377 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001378 MVT::ValueType VT = N0.getValueType();
1379 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1380
1381 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001382 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001383 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001384 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001385 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001386 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001387 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001388 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001389 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001390 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001391 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001392 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001393 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001394 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001395 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001396 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001397 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001398 N0.getOperand(1).getOpcode() == ISD::Constant) {
1399 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001400 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001401 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001402 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001404 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001405 }
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::visitCTLZ(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 (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001415 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001416 return DAG.getNode(ISD::CTLZ, 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::visitCTTZ(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 (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001426 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001427 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001428 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429}
1430
Nate Begeman83e75ec2005-09-06 04:43:02 +00001431SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001432 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001433 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001434 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435
1436 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001437 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001438 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001439 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440}
1441
Nate Begeman452d7be2005-09-16 00:54:12 +00001442SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1443 SDOperand N0 = N->getOperand(0);
1444 SDOperand N1 = N->getOperand(1);
1445 SDOperand N2 = N->getOperand(2);
1446 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1447 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1448 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1449 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001450
Nate Begeman452d7be2005-09-16 00:54:12 +00001451 // fold select C, X, X -> X
1452 if (N1 == N2)
1453 return N1;
1454 // fold select true, X, Y -> X
1455 if (N0C && !N0C->isNullValue())
1456 return N1;
1457 // fold select false, X, Y -> Y
1458 if (N0C && N0C->isNullValue())
1459 return N2;
1460 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001461 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001462 return DAG.getNode(ISD::OR, VT, N0, N2);
1463 // fold select C, 0, X -> ~C & X
1464 // FIXME: this should check for C type == X type, not i1?
1465 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1466 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1467 WorkList.push_back(XORNode.Val);
1468 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1469 }
1470 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001471 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001472 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1473 WorkList.push_back(XORNode.Val);
1474 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1475 }
1476 // fold select C, X, 0 -> C & X
1477 // FIXME: this should check for C type == X type, not i1?
1478 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1479 return DAG.getNode(ISD::AND, VT, N0, N1);
1480 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1481 if (MVT::i1 == VT && N0 == N1)
1482 return DAG.getNode(ISD::OR, VT, N0, N2);
1483 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1484 if (MVT::i1 == VT && N0 == N2)
1485 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001486 // If we can fold this based on the true/false value, do so.
1487 if (SimplifySelectOps(N, N1, N2))
1488 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001489 // fold selects based on a setcc into other things, such as min/max/abs
1490 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001491 // FIXME:
1492 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1493 // having to say they don't support SELECT_CC on every type the DAG knows
1494 // about, since there is no way to mark an opcode illegal at all value types
1495 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1496 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1497 N1, N2, N0.getOperand(2));
1498 else
1499 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001500 return SDOperand();
1501}
1502
1503SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001504 SDOperand N0 = N->getOperand(0);
1505 SDOperand N1 = N->getOperand(1);
1506 SDOperand N2 = N->getOperand(2);
1507 SDOperand N3 = N->getOperand(3);
1508 SDOperand N4 = N->getOperand(4);
1509 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1510 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1511 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1512 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1513
1514 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001515 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001516 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1517
Nate Begeman44728a72005-09-19 22:34:01 +00001518 // fold select_cc lhs, rhs, x, x, cc -> x
1519 if (N2 == N3)
1520 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001521
1522 // If we can fold this based on the true/false value, do so.
1523 if (SimplifySelectOps(N, N2, N3))
1524 return SDOperand();
1525
Nate Begeman44728a72005-09-19 22:34:01 +00001526 // fold select_cc into other things, such as min/max/abs
1527 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001528}
1529
1530SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1531 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1532 cast<CondCodeSDNode>(N->getOperand(2))->get());
1533}
1534
Nate Begeman83e75ec2005-09-06 04:43:02 +00001535SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001536 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001537 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001538 MVT::ValueType VT = N->getValueType(0);
1539
Nate Begeman1d4d4142005-09-01 00:19:25 +00001540 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001541 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001542 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001543 // fold (sext (sext x)) -> (sext x)
1544 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001545 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001546 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001547 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1548 (!AfterLegalize ||
1549 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001550 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1551 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001552 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001553 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1554 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001555 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1556 N0.getOperand(1), N0.getOperand(2),
1557 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001558 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001559 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1560 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001561 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001562 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001563
1564 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1565 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1566 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1567 N0.hasOneUse()) {
1568 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1569 N0.getOperand(1), N0.getOperand(2),
1570 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001571 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001572 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1573 ExtLoad.getValue(1));
1574 return SDOperand();
1575 }
1576
Nate Begeman83e75ec2005-09-06 04:43:02 +00001577 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001578}
1579
Nate Begeman83e75ec2005-09-06 04:43:02 +00001580SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001581 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001582 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001583 MVT::ValueType VT = N->getValueType(0);
1584
Nate Begeman1d4d4142005-09-01 00:19:25 +00001585 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001586 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001587 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001588 // fold (zext (zext x)) -> (zext x)
1589 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001590 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001591 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1592 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001593 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001594 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001595 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001596 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1597 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001598 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1599 N0.getOperand(1), N0.getOperand(2),
1600 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001601 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001602 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1603 ExtLoad.getValue(1));
1604 return SDOperand();
1605 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001606
1607 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1608 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1609 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1610 N0.hasOneUse()) {
1611 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1612 N0.getOperand(1), N0.getOperand(2),
1613 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001614 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001615 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1616 ExtLoad.getValue(1));
1617 return SDOperand();
1618 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001620}
1621
Nate Begeman83e75ec2005-09-06 04:43:02 +00001622SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001623 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001624 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001625 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001626 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001627 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001628 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001629
Nate Begeman1d4d4142005-09-01 00:19:25 +00001630 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001631 if (N0C) {
1632 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001633 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001634 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001635 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001636 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001637 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001638 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001640 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1641 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1642 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001643 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001644 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001645 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1646 if (N0.getOpcode() == ISD::AssertSext &&
1647 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001648 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001649 }
1650 // fold (sext_in_reg (sextload x)) -> (sextload x)
1651 if (N0.getOpcode() == ISD::SEXTLOAD &&
1652 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001653 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001654 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001655 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001656 if (N0.getOpcode() == ISD::SETCC &&
1657 TLI.getSetCCResultContents() ==
1658 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001659 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001660 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001661 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001662 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001663 // fold (sext_in_reg (srl x)) -> sra x
1664 if (N0.getOpcode() == ISD::SRL &&
1665 N0.getOperand(1).getOpcode() == ISD::Constant &&
1666 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1667 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1668 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001669 }
Nate Begemanded49632005-10-13 03:11:28 +00001670 // fold (sext_inreg (extload x)) -> (sextload x)
1671 if (N0.getOpcode() == ISD::EXTLOAD &&
1672 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001673 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001674 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1675 N0.getOperand(1), N0.getOperand(2),
1676 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001677 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001678 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001679 return SDOperand();
1680 }
1681 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001682 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001683 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001684 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001685 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1686 N0.getOperand(1), N0.getOperand(2),
1687 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001688 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001689 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001690 return SDOperand();
1691 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001692 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001693}
1694
Nate Begeman83e75ec2005-09-06 04:43:02 +00001695SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001696 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001697 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001698 MVT::ValueType VT = N->getValueType(0);
1699
1700 // noop truncate
1701 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001702 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001703 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001704 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001705 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001706 // fold (truncate (truncate x)) -> (truncate x)
1707 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001708 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001709 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1710 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1711 if (N0.getValueType() < VT)
1712 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001713 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001714 else if (N0.getValueType() > VT)
1715 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001716 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001717 else
1718 // if the source and dest are the same type, we can drop both the extend
1719 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001720 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001721 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001722 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001723 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001724 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1725 "Cannot truncate to larger type!");
1726 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001727 // For big endian targets, we need to add an offset to the pointer to load
1728 // the correct bytes. For little endian systems, we merely need to read
1729 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001730 uint64_t PtrOff =
1731 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001732 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1733 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1734 DAG.getConstant(PtrOff, PtrType));
1735 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001736 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001737 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001738 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001739 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001740 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001741 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001742}
1743
Chris Lattner94683772005-12-23 05:30:37 +00001744SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1745 SDOperand N0 = N->getOperand(0);
1746 MVT::ValueType VT = N->getValueType(0);
1747
1748 // If the input is a constant, let getNode() fold it.
1749 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1750 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1751 if (Res.Val != N) return Res;
1752 }
1753
Chris Lattnerc8547d82005-12-23 05:37:50 +00001754 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1755 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1756
Chris Lattner57104102005-12-23 05:44:41 +00001757 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001758 // FIXME: These xforms need to know that the resultant load doesn't need a
1759 // higher alignment than the original!
1760 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001761 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1762 N0.getOperand(2));
1763 WorkList.push_back(N);
1764 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1765 Load.getValue(1));
1766 return Load;
1767 }
1768
Chris Lattner94683772005-12-23 05:30:37 +00001769 return SDOperand();
1770}
1771
Chris Lattner01b3d732005-09-28 22:28:18 +00001772SDOperand DAGCombiner::visitFADD(SDNode *N) {
1773 SDOperand N0 = N->getOperand(0);
1774 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001775 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1776 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001777 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001778
1779 // fold (fadd c1, c2) -> c1+c2
1780 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001781 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001782 // canonicalize constant to RHS
1783 if (N0CFP && !N1CFP)
1784 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001785 // fold (A + (-B)) -> A-B
1786 if (N1.getOpcode() == ISD::FNEG)
1787 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001788 // fold ((-A) + B) -> B-A
1789 if (N0.getOpcode() == ISD::FNEG)
1790 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001791 return SDOperand();
1792}
1793
1794SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1795 SDOperand N0 = N->getOperand(0);
1796 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001797 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1798 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001799 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001800
1801 // fold (fsub c1, c2) -> c1-c2
1802 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001803 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001804 // fold (A-(-B)) -> A+B
1805 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001806 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001807 return SDOperand();
1808}
1809
1810SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1811 SDOperand N0 = N->getOperand(0);
1812 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001813 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1814 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001815 MVT::ValueType VT = N->getValueType(0);
1816
Nate Begeman11af4ea2005-10-17 20:40:11 +00001817 // fold (fmul c1, c2) -> c1*c2
1818 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001819 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001820 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001821 if (N0CFP && !N1CFP)
1822 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001823 // fold (fmul X, 2.0) -> (fadd X, X)
1824 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1825 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001826 return SDOperand();
1827}
1828
1829SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1830 SDOperand N0 = N->getOperand(0);
1831 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001832 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1833 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001834 MVT::ValueType VT = N->getValueType(0);
1835
Nate Begemana148d982006-01-18 22:35:16 +00001836 // fold (fdiv c1, c2) -> c1/c2
1837 if (N0CFP && N1CFP)
1838 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001839 return SDOperand();
1840}
1841
1842SDOperand DAGCombiner::visitFREM(SDNode *N) {
1843 SDOperand N0 = N->getOperand(0);
1844 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001845 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1846 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001847 MVT::ValueType VT = N->getValueType(0);
1848
Nate Begemana148d982006-01-18 22:35:16 +00001849 // fold (frem c1, c2) -> fmod(c1,c2)
1850 if (N0CFP && N1CFP)
1851 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001852 return SDOperand();
1853}
1854
1855
Nate Begeman83e75ec2005-09-06 04:43:02 +00001856SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001857 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001858 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001859 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001860
1861 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001862 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001863 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001864 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001865}
1866
Nate Begeman83e75ec2005-09-06 04:43:02 +00001867SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001868 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001869 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001870 MVT::ValueType VT = N->getValueType(0);
1871
Nate Begeman1d4d4142005-09-01 00:19:25 +00001872 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001873 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001874 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001875 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001876}
1877
Nate Begeman83e75ec2005-09-06 04:43:02 +00001878SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001879 SDOperand N0 = N->getOperand(0);
1880 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1881 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001882
1883 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001884 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001885 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001886 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001887}
1888
Nate Begeman83e75ec2005-09-06 04:43:02 +00001889SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001890 SDOperand N0 = N->getOperand(0);
1891 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1892 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001893
1894 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001895 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001896 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001897 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001898}
1899
Nate Begeman83e75ec2005-09-06 04:43:02 +00001900SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001901 SDOperand N0 = N->getOperand(0);
1902 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1903 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904
1905 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001906 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001907 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001908 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001909}
1910
Nate Begeman83e75ec2005-09-06 04:43:02 +00001911SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001912 SDOperand N0 = N->getOperand(0);
1913 MVT::ValueType VT = N->getValueType(0);
1914 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001915 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001916
Nate Begeman1d4d4142005-09-01 00:19:25 +00001917 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001918 if (N0CFP) {
1919 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001920 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001921 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001922 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001923}
1924
Nate Begeman83e75ec2005-09-06 04:43:02 +00001925SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001926 SDOperand N0 = N->getOperand(0);
1927 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1928 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001929
1930 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001931 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001932 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001933 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001934}
1935
Nate Begeman83e75ec2005-09-06 04:43:02 +00001936SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001937 SDOperand N0 = N->getOperand(0);
1938 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1939 MVT::ValueType VT = N->getValueType(0);
1940
1941 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001942 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001943 return DAG.getNode(ISD::FNEG, VT, N0);
1944 // fold (fneg (sub x, y)) -> (sub y, x)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001945 if (N->getOperand(0).getOpcode() == ISD::SUB)
Nate Begemana148d982006-01-18 22:35:16 +00001946 return DAG.getNode(ISD::SUB, VT, N->getOperand(1), N->getOperand(0));
1947 // fold (fneg (fneg x)) -> x
Nate Begeman1d4d4142005-09-01 00:19:25 +00001948 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001949 return N->getOperand(0).getOperand(0);
1950 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001951}
1952
Nate Begeman83e75ec2005-09-06 04:43:02 +00001953SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001954 SDOperand N0 = N->getOperand(0);
1955 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1956 MVT::ValueType VT = N->getValueType(0);
1957
Nate Begeman1d4d4142005-09-01 00:19:25 +00001958 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001959 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001960 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001961 // fold (fabs (fabs x)) -> (fabs x)
1962 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001963 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001964 // fold (fabs (fneg x)) -> (fabs x)
1965 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001966 return DAG.getNode(ISD::FABS, VT, N->getOperand(0).getOperand(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +00001967 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001968}
1969
Nate Begeman44728a72005-09-19 22:34:01 +00001970SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1971 SDOperand Chain = N->getOperand(0);
1972 SDOperand N1 = N->getOperand(1);
1973 SDOperand N2 = N->getOperand(2);
1974 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1975
1976 // never taken branch, fold to chain
1977 if (N1C && N1C->isNullValue())
1978 return Chain;
1979 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001980 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001981 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001982 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
1983 // on the target.
1984 if (N1.getOpcode() == ISD::SETCC &&
1985 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
1986 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
1987 N1.getOperand(0), N1.getOperand(1), N2);
1988 }
Nate Begeman44728a72005-09-19 22:34:01 +00001989 return SDOperand();
1990}
1991
1992SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1993 SDOperand Chain = N->getOperand(0);
1994 SDOperand N1 = N->getOperand(1);
1995 SDOperand N2 = N->getOperand(2);
1996 SDOperand N3 = N->getOperand(3);
1997 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1998
1999 // unconditional branch to true mbb
2000 if (N1C && N1C->getValue() == 1)
2001 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2002 // unconditional branch to false mbb
2003 if (N1C && N1C->isNullValue())
2004 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002005 // fold a brcondtwoway with a setcc condition into a BRTWOWAY_CC node if
2006 // BRTWOWAY_CC is legal on the target.
2007 if (N1.getOpcode() == ISD::SETCC &&
2008 TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
2009 std::vector<SDOperand> Ops;
2010 Ops.push_back(Chain);
2011 Ops.push_back(N1.getOperand(2));
2012 Ops.push_back(N1.getOperand(0));
2013 Ops.push_back(N1.getOperand(1));
2014 Ops.push_back(N2);
2015 Ops.push_back(N3);
2016 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2017 }
Nate Begeman44728a72005-09-19 22:34:01 +00002018 return SDOperand();
2019}
2020
Chris Lattner3ea0b472005-10-05 06:47:48 +00002021// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2022//
Nate Begeman44728a72005-09-19 22:34:01 +00002023SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002024 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2025 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2026
2027 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002028 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2029 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2030
2031 // fold br_cc true, dest -> br dest (unconditional branch)
2032 if (SCCC && SCCC->getValue())
2033 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2034 N->getOperand(4));
2035 // fold br_cc false, dest -> unconditional fall through
2036 if (SCCC && SCCC->isNullValue())
2037 return N->getOperand(0);
2038 // fold to a simpler setcc
2039 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2040 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2041 Simp.getOperand(2), Simp.getOperand(0),
2042 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002043 return SDOperand();
2044}
2045
2046SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00002047 SDOperand Chain = N->getOperand(0);
2048 SDOperand CCN = N->getOperand(1);
2049 SDOperand LHS = N->getOperand(2);
2050 SDOperand RHS = N->getOperand(3);
2051 SDOperand N4 = N->getOperand(4);
2052 SDOperand N5 = N->getOperand(5);
2053
2054 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
2055 cast<CondCodeSDNode>(CCN)->get(), false);
2056 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2057
2058 // fold select_cc lhs, rhs, x, x, cc -> x
2059 if (N4 == N5)
2060 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2061 // fold select_cc true, x, y -> x
2062 if (SCCC && SCCC->getValue())
2063 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2064 // fold select_cc false, x, y -> y
2065 if (SCCC && SCCC->isNullValue())
2066 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
2067 // fold to a simpler setcc
Chris Lattner03d5e872006-01-29 06:00:45 +00002068 if (SCC.Val && SCC.getOpcode() == ISD::SETCC) {
2069 std::vector<SDOperand> Ops;
2070 Ops.push_back(Chain);
2071 Ops.push_back(SCC.getOperand(2));
2072 Ops.push_back(SCC.getOperand(0));
2073 Ops.push_back(SCC.getOperand(1));
2074 Ops.push_back(N4);
2075 Ops.push_back(N5);
2076 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2077 }
Nate Begeman44728a72005-09-19 22:34:01 +00002078 return SDOperand();
2079}
2080
Chris Lattner01a22022005-10-10 22:04:48 +00002081SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2082 SDOperand Chain = N->getOperand(0);
2083 SDOperand Ptr = N->getOperand(1);
2084 SDOperand SrcValue = N->getOperand(2);
2085
2086 // If this load is directly stored, replace the load value with the stored
2087 // value.
2088 // TODO: Handle store large -> read small portion.
2089 // TODO: Handle TRUNCSTORE/EXTLOAD
2090 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2091 Chain.getOperand(1).getValueType() == N->getValueType(0))
2092 return CombineTo(N, Chain.getOperand(1), Chain);
2093
2094 return SDOperand();
2095}
2096
Chris Lattner87514ca2005-10-10 22:31:19 +00002097SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2098 SDOperand Chain = N->getOperand(0);
2099 SDOperand Value = N->getOperand(1);
2100 SDOperand Ptr = N->getOperand(2);
2101 SDOperand SrcValue = N->getOperand(3);
2102
2103 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002104 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002105 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2106 // Make sure that these stores are the same value type:
2107 // FIXME: we really care that the second store is >= size of the first.
2108 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002109 // Create a new store of Value that replaces both stores.
2110 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002111 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2112 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002113 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2114 PrevStore->getOperand(0), Value, Ptr,
2115 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002116 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002117 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002118 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002119 }
2120
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002121 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002122 // FIXME: This needs to know that the resultant store does not need a
2123 // higher alignment than the original.
2124 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002125 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2126 Ptr, SrcValue);
2127
Chris Lattner87514ca2005-10-10 22:31:19 +00002128 return SDOperand();
2129}
2130
Nate Begeman44728a72005-09-19 22:34:01 +00002131SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002132 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2133
2134 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2135 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2136 // If we got a simplified select_cc node back from SimplifySelectCC, then
2137 // break it down into a new SETCC node, and a new SELECT node, and then return
2138 // the SELECT node, since we were called with a SELECT node.
2139 if (SCC.Val) {
2140 // Check to see if we got a select_cc back (to turn into setcc/select).
2141 // Otherwise, just return whatever node we got back, like fabs.
2142 if (SCC.getOpcode() == ISD::SELECT_CC) {
2143 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2144 SCC.getOperand(0), SCC.getOperand(1),
2145 SCC.getOperand(4));
2146 WorkList.push_back(SETCC.Val);
2147 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2148 SCC.getOperand(3), SETCC);
2149 }
2150 return SCC;
2151 }
Nate Begeman44728a72005-09-19 22:34:01 +00002152 return SDOperand();
2153}
2154
Chris Lattner40c62d52005-10-18 06:04:22 +00002155/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2156/// are the two values being selected between, see if we can simplify the
2157/// select.
2158///
2159bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2160 SDOperand RHS) {
2161
2162 // If this is a select from two identical things, try to pull the operation
2163 // through the select.
2164 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2165#if 0
2166 std::cerr << "SELECT: ["; LHS.Val->dump();
2167 std::cerr << "] ["; RHS.Val->dump();
2168 std::cerr << "]\n";
2169#endif
2170
2171 // If this is a load and the token chain is identical, replace the select
2172 // of two loads with a load through a select of the address to load from.
2173 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2174 // constants have been dropped into the constant pool.
2175 if ((LHS.getOpcode() == ISD::LOAD ||
2176 LHS.getOpcode() == ISD::EXTLOAD ||
2177 LHS.getOpcode() == ISD::ZEXTLOAD ||
2178 LHS.getOpcode() == ISD::SEXTLOAD) &&
2179 // Token chains must be identical.
2180 LHS.getOperand(0) == RHS.getOperand(0) &&
2181 // If this is an EXTLOAD, the VT's must match.
2182 (LHS.getOpcode() == ISD::LOAD ||
2183 LHS.getOperand(3) == RHS.getOperand(3))) {
2184 // FIXME: this conflates two src values, discarding one. This is not
2185 // the right thing to do, but nothing uses srcvalues now. When they do,
2186 // turn SrcValue into a list of locations.
2187 SDOperand Addr;
2188 if (TheSelect->getOpcode() == ISD::SELECT)
2189 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2190 TheSelect->getOperand(0), LHS.getOperand(1),
2191 RHS.getOperand(1));
2192 else
2193 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2194 TheSelect->getOperand(0),
2195 TheSelect->getOperand(1),
2196 LHS.getOperand(1), RHS.getOperand(1),
2197 TheSelect->getOperand(4));
2198
2199 SDOperand Load;
2200 if (LHS.getOpcode() == ISD::LOAD)
2201 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2202 Addr, LHS.getOperand(2));
2203 else
2204 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2205 LHS.getOperand(0), Addr, LHS.getOperand(2),
2206 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2207 // Users of the select now use the result of the load.
2208 CombineTo(TheSelect, Load);
2209
2210 // Users of the old loads now use the new load's chain. We know the
2211 // old-load value is dead now.
2212 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2213 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2214 return true;
2215 }
2216 }
2217
2218 return false;
2219}
2220
Nate Begeman44728a72005-09-19 22:34:01 +00002221SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2222 SDOperand N2, SDOperand N3,
2223 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002224
2225 MVT::ValueType VT = N2.getValueType();
2226 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2227 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2228 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2229 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2230
2231 // Determine if the condition we're dealing with is constant
2232 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2233 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2234
2235 // fold select_cc true, x, y -> x
2236 if (SCCC && SCCC->getValue())
2237 return N2;
2238 // fold select_cc false, x, y -> y
2239 if (SCCC && SCCC->getValue() == 0)
2240 return N3;
2241
2242 // Check to see if we can simplify the select into an fabs node
2243 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2244 // Allow either -0.0 or 0.0
2245 if (CFP->getValue() == 0.0) {
2246 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2247 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2248 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2249 N2 == N3.getOperand(0))
2250 return DAG.getNode(ISD::FABS, VT, N0);
2251
2252 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2253 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2254 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2255 N2.getOperand(0) == N3)
2256 return DAG.getNode(ISD::FABS, VT, N3);
2257 }
2258 }
2259
2260 // Check to see if we can perform the "gzip trick", transforming
2261 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2262 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2263 MVT::isInteger(N0.getValueType()) &&
2264 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2265 MVT::ValueType XType = N0.getValueType();
2266 MVT::ValueType AType = N2.getValueType();
2267 if (XType >= AType) {
2268 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002269 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002270 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2271 unsigned ShCtV = Log2_64(N2C->getValue());
2272 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2273 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2274 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
2275 WorkList.push_back(Shift.Val);
2276 if (XType > AType) {
2277 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2278 WorkList.push_back(Shift.Val);
2279 }
2280 return DAG.getNode(ISD::AND, AType, Shift, N2);
2281 }
2282 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2283 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2284 TLI.getShiftAmountTy()));
2285 WorkList.push_back(Shift.Val);
2286 if (XType > AType) {
2287 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2288 WorkList.push_back(Shift.Val);
2289 }
2290 return DAG.getNode(ISD::AND, AType, Shift, N2);
2291 }
2292 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002293
2294 // fold select C, 16, 0 -> shl C, 4
2295 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2296 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2297 // Get a SetCC of the condition
2298 // FIXME: Should probably make sure that setcc is legal if we ever have a
2299 // target where it isn't.
2300 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2301 WorkList.push_back(SCC.Val);
2302 // cast from setcc result type to select result type
2303 if (AfterLegalize)
2304 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
2305 else
2306 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
2307 WorkList.push_back(Temp.Val);
2308 // shl setcc result by log2 n2c
2309 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2310 DAG.getConstant(Log2_64(N2C->getValue()),
2311 TLI.getShiftAmountTy()));
2312 }
2313
Nate Begemanf845b452005-10-08 00:29:44 +00002314 // Check to see if this is the equivalent of setcc
2315 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2316 // otherwise, go ahead with the folds.
2317 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2318 MVT::ValueType XType = N0.getValueType();
2319 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2320 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2321 if (Res.getValueType() != VT)
2322 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2323 return Res;
2324 }
2325
2326 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2327 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2328 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2329 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2330 return DAG.getNode(ISD::SRL, XType, Ctlz,
2331 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2332 TLI.getShiftAmountTy()));
2333 }
2334 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2335 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2336 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2337 N0);
2338 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2339 DAG.getConstant(~0ULL, XType));
2340 return DAG.getNode(ISD::SRL, XType,
2341 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2342 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2343 TLI.getShiftAmountTy()));
2344 }
2345 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2346 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2347 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2348 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2349 TLI.getShiftAmountTy()));
2350 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2351 }
2352 }
2353
2354 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2355 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2356 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2357 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2358 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2359 MVT::ValueType XType = N0.getValueType();
2360 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2361 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2362 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2363 TLI.getShiftAmountTy()));
2364 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
2365 WorkList.push_back(Shift.Val);
2366 WorkList.push_back(Add.Val);
2367 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2368 }
2369 }
2370 }
2371
Nate Begeman44728a72005-09-19 22:34:01 +00002372 return SDOperand();
2373}
2374
Nate Begeman452d7be2005-09-16 00:54:12 +00002375SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002376 SDOperand N1, ISD::CondCode Cond,
2377 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002378 // These setcc operations always fold.
2379 switch (Cond) {
2380 default: break;
2381 case ISD::SETFALSE:
2382 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2383 case ISD::SETTRUE:
2384 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2385 }
2386
2387 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2388 uint64_t C1 = N1C->getValue();
2389 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2390 uint64_t C0 = N0C->getValue();
2391
2392 // Sign extend the operands if required
2393 if (ISD::isSignedIntSetCC(Cond)) {
2394 C0 = N0C->getSignExtended();
2395 C1 = N1C->getSignExtended();
2396 }
2397
2398 switch (Cond) {
2399 default: assert(0 && "Unknown integer setcc!");
2400 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2401 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2402 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2403 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2404 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2405 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2406 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2407 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2408 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2409 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2410 }
2411 } else {
2412 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2413 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2414 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2415
2416 // If the comparison constant has bits in the upper part, the
2417 // zero-extended value could never match.
2418 if (C1 & (~0ULL << InSize)) {
2419 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2420 switch (Cond) {
2421 case ISD::SETUGT:
2422 case ISD::SETUGE:
2423 case ISD::SETEQ: return DAG.getConstant(0, VT);
2424 case ISD::SETULT:
2425 case ISD::SETULE:
2426 case ISD::SETNE: return DAG.getConstant(1, VT);
2427 case ISD::SETGT:
2428 case ISD::SETGE:
2429 // True if the sign bit of C1 is set.
2430 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2431 case ISD::SETLT:
2432 case ISD::SETLE:
2433 // True if the sign bit of C1 isn't set.
2434 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2435 default:
2436 break;
2437 }
2438 }
2439
2440 // Otherwise, we can perform the comparison with the low bits.
2441 switch (Cond) {
2442 case ISD::SETEQ:
2443 case ISD::SETNE:
2444 case ISD::SETUGT:
2445 case ISD::SETUGE:
2446 case ISD::SETULT:
2447 case ISD::SETULE:
2448 return DAG.getSetCC(VT, N0.getOperand(0),
2449 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2450 Cond);
2451 default:
2452 break; // todo, be more careful with signed comparisons
2453 }
2454 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2455 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2456 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2457 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2458 MVT::ValueType ExtDstTy = N0.getValueType();
2459 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2460
2461 // If the extended part has any inconsistent bits, it cannot ever
2462 // compare equal. In other words, they have to be all ones or all
2463 // zeros.
2464 uint64_t ExtBits =
2465 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2466 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2467 return DAG.getConstant(Cond == ISD::SETNE, VT);
2468
2469 SDOperand ZextOp;
2470 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2471 if (Op0Ty == ExtSrcTy) {
2472 ZextOp = N0.getOperand(0);
2473 } else {
2474 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2475 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2476 DAG.getConstant(Imm, Op0Ty));
2477 }
2478 WorkList.push_back(ZextOp.Val);
2479 // Otherwise, make this a use of a zext.
2480 return DAG.getSetCC(VT, ZextOp,
2481 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2482 ExtDstTy),
2483 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002484 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2485 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2486 (N0.getOpcode() == ISD::XOR ||
2487 (N0.getOpcode() == ISD::AND &&
2488 N0.getOperand(0).getOpcode() == ISD::XOR &&
2489 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2490 isa<ConstantSDNode>(N0.getOperand(1)) &&
2491 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2492 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2493 // only do this if the top bits are known zero.
2494 if (TLI.MaskedValueIsZero(N1,
2495 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2496 // Okay, get the un-inverted input value.
2497 SDOperand Val;
2498 if (N0.getOpcode() == ISD::XOR)
2499 Val = N0.getOperand(0);
2500 else {
2501 assert(N0.getOpcode() == ISD::AND &&
2502 N0.getOperand(0).getOpcode() == ISD::XOR);
2503 // ((X^1)&1)^1 -> X & 1
2504 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2505 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2506 }
2507 return DAG.getSetCC(VT, Val, N1,
2508 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2509 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002510 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002511
Nate Begeman452d7be2005-09-16 00:54:12 +00002512 uint64_t MinVal, MaxVal;
2513 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2514 if (ISD::isSignedIntSetCC(Cond)) {
2515 MinVal = 1ULL << (OperandBitSize-1);
2516 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2517 MaxVal = ~0ULL >> (65-OperandBitSize);
2518 else
2519 MaxVal = 0;
2520 } else {
2521 MinVal = 0;
2522 MaxVal = ~0ULL >> (64-OperandBitSize);
2523 }
2524
2525 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2526 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2527 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2528 --C1; // X >= C0 --> X > (C0-1)
2529 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2530 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2531 }
2532
2533 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2534 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2535 ++C1; // X <= C0 --> X < (C0+1)
2536 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2537 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2538 }
2539
2540 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2541 return DAG.getConstant(0, VT); // X < MIN --> false
2542
2543 // Canonicalize setgt X, Min --> setne X, Min
2544 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2545 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002546 // Canonicalize setlt X, Max --> setne X, Max
2547 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2548 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002549
2550 // If we have setult X, 1, turn it into seteq X, 0
2551 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2552 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2553 ISD::SETEQ);
2554 // If we have setugt X, Max-1, turn it into seteq X, Max
2555 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2556 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2557 ISD::SETEQ);
2558
2559 // If we have "setcc X, C0", check to see if we can shrink the immediate
2560 // by changing cc.
2561
2562 // SETUGT X, SINTMAX -> SETLT X, 0
2563 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2564 C1 == (~0ULL >> (65-OperandBitSize)))
2565 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2566 ISD::SETLT);
2567
2568 // FIXME: Implement the rest of these.
2569
2570 // Fold bit comparisons when we can.
2571 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2572 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2573 if (ConstantSDNode *AndRHS =
2574 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2575 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2576 // Perform the xform if the AND RHS is a single bit.
2577 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2578 return DAG.getNode(ISD::SRL, VT, N0,
2579 DAG.getConstant(Log2_64(AndRHS->getValue()),
2580 TLI.getShiftAmountTy()));
2581 }
2582 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2583 // (X & 8) == 8 --> (X & 8) >> 3
2584 // Perform the xform if C1 is a single bit.
2585 if ((C1 & (C1-1)) == 0) {
2586 return DAG.getNode(ISD::SRL, VT, N0,
2587 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2588 }
2589 }
2590 }
2591 }
2592 } else if (isa<ConstantSDNode>(N0.Val)) {
2593 // Ensure that the constant occurs on the RHS.
2594 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2595 }
2596
2597 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2598 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2599 double C0 = N0C->getValue(), C1 = N1C->getValue();
2600
2601 switch (Cond) {
2602 default: break; // FIXME: Implement the rest of these!
2603 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2604 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2605 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2606 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2607 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2608 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2609 }
2610 } else {
2611 // Ensure that the constant occurs on the RHS.
2612 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2613 }
2614
2615 if (N0 == N1) {
2616 // We can always fold X == Y for integer setcc's.
2617 if (MVT::isInteger(N0.getValueType()))
2618 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2619 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2620 if (UOF == 2) // FP operators that are undefined on NaNs.
2621 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2622 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2623 return DAG.getConstant(UOF, VT);
2624 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2625 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002626 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002627 if (NewCond != Cond)
2628 return DAG.getSetCC(VT, N0, N1, NewCond);
2629 }
2630
2631 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2632 MVT::isInteger(N0.getValueType())) {
2633 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2634 N0.getOpcode() == ISD::XOR) {
2635 // Simplify (X+Y) == (X+Z) --> Y == Z
2636 if (N0.getOpcode() == N1.getOpcode()) {
2637 if (N0.getOperand(0) == N1.getOperand(0))
2638 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2639 if (N0.getOperand(1) == N1.getOperand(1))
2640 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2641 if (isCommutativeBinOp(N0.getOpcode())) {
2642 // If X op Y == Y op X, try other combinations.
2643 if (N0.getOperand(0) == N1.getOperand(1))
2644 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2645 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002646 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002647 }
2648 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002649
2650 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2651 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2652 // Turn (X+C1) == C2 --> X == C2-C1
2653 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2654 return DAG.getSetCC(VT, N0.getOperand(0),
2655 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2656 N0.getValueType()), Cond);
2657 }
2658
2659 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2660 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002661 // If we know that all of the inverted bits are zero, don't bother
2662 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002663 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002664 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002665 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002666 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002667 }
2668
2669 // Turn (C1-X) == C2 --> X == C1-C2
2670 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2671 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
2672 return DAG.getSetCC(VT, N0.getOperand(1),
2673 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
2674 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00002675 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002676 }
2677 }
2678
Nate Begeman452d7be2005-09-16 00:54:12 +00002679 // Simplify (X+Z) == X --> Z == 0
2680 if (N0.getOperand(0) == N1)
2681 return DAG.getSetCC(VT, N0.getOperand(1),
2682 DAG.getConstant(0, N0.getValueType()), Cond);
2683 if (N0.getOperand(1) == N1) {
2684 if (isCommutativeBinOp(N0.getOpcode()))
2685 return DAG.getSetCC(VT, N0.getOperand(0),
2686 DAG.getConstant(0, N0.getValueType()), Cond);
2687 else {
2688 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2689 // (Z-X) == X --> Z == X<<1
2690 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2691 N1,
2692 DAG.getConstant(1,TLI.getShiftAmountTy()));
2693 WorkList.push_back(SH.Val);
2694 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2695 }
2696 }
2697 }
2698
2699 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2700 N1.getOpcode() == ISD::XOR) {
2701 // Simplify X == (X+Z) --> Z == 0
2702 if (N1.getOperand(0) == N0) {
2703 return DAG.getSetCC(VT, N1.getOperand(1),
2704 DAG.getConstant(0, N1.getValueType()), Cond);
2705 } else if (N1.getOperand(1) == N0) {
2706 if (isCommutativeBinOp(N1.getOpcode())) {
2707 return DAG.getSetCC(VT, N1.getOperand(0),
2708 DAG.getConstant(0, N1.getValueType()), Cond);
2709 } else {
2710 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2711 // X == (Z-X) --> X<<1 == Z
2712 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2713 DAG.getConstant(1,TLI.getShiftAmountTy()));
2714 WorkList.push_back(SH.Val);
2715 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2716 }
2717 }
2718 }
2719 }
2720
2721 // Fold away ALL boolean setcc's.
2722 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002723 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002724 switch (Cond) {
2725 default: assert(0 && "Unknown integer setcc!");
2726 case ISD::SETEQ: // X == Y -> (X^Y)^1
2727 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2728 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2729 WorkList.push_back(Temp.Val);
2730 break;
2731 case ISD::SETNE: // X != Y --> (X^Y)
2732 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2733 break;
2734 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2735 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2736 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2737 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2738 WorkList.push_back(Temp.Val);
2739 break;
2740 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2741 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2742 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2743 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2744 WorkList.push_back(Temp.Val);
2745 break;
2746 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2747 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2748 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2749 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2750 WorkList.push_back(Temp.Val);
2751 break;
2752 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2753 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2754 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2755 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2756 break;
2757 }
2758 if (VT != MVT::i1) {
2759 WorkList.push_back(N0.Val);
2760 // FIXME: If running after legalize, we probably can't do this.
2761 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2762 }
2763 return N0;
2764 }
2765
2766 // Could not fold it.
2767 return SDOperand();
2768}
2769
Nate Begeman69575232005-10-20 02:15:44 +00002770/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2771/// return a DAG expression to select that will generate the same value by
2772/// multiplying by a magic number. See:
2773/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2774SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2775 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002776
2777 // Check to see if we can do this.
2778 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2779 return SDOperand(); // BuildSDIV only operates on i32 or i64
2780 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2781 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002782
Nate Begemanc6a454e2005-10-20 17:45:03 +00002783 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002784 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2785
2786 // Multiply the numerator (operand 0) by the magic value
2787 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2788 DAG.getConstant(magics.m, VT));
2789 // If d > 0 and m < 0, add the numerator
2790 if (d > 0 && magics.m < 0) {
2791 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2792 WorkList.push_back(Q.Val);
2793 }
2794 // If d < 0 and m > 0, subtract the numerator.
2795 if (d < 0 && magics.m > 0) {
2796 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2797 WorkList.push_back(Q.Val);
2798 }
2799 // Shift right algebraic if shift value is nonzero
2800 if (magics.s > 0) {
2801 Q = DAG.getNode(ISD::SRA, VT, Q,
2802 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2803 WorkList.push_back(Q.Val);
2804 }
2805 // Extract the sign bit and add it to the quotient
2806 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002807 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2808 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +00002809 WorkList.push_back(T.Val);
2810 return DAG.getNode(ISD::ADD, VT, Q, T);
2811}
2812
2813/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2814/// return a DAG expression to select that will generate the same value by
2815/// multiplying by a magic number. See:
2816/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2817SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2818 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002819
2820 // Check to see if we can do this.
2821 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2822 return SDOperand(); // BuildUDIV only operates on i32 or i64
2823 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2824 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002825
2826 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2827 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2828
2829 // Multiply the numerator (operand 0) by the magic value
2830 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2831 DAG.getConstant(magics.m, VT));
2832 WorkList.push_back(Q.Val);
2833
2834 if (magics.a == 0) {
2835 return DAG.getNode(ISD::SRL, VT, Q,
2836 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2837 } else {
2838 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2839 WorkList.push_back(NPQ.Val);
2840 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2841 DAG.getConstant(1, TLI.getShiftAmountTy()));
2842 WorkList.push_back(NPQ.Val);
2843 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2844 WorkList.push_back(NPQ.Val);
2845 return DAG.getNode(ISD::SRL, VT, NPQ,
2846 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
2847 }
2848}
2849
Nate Begeman1d4d4142005-09-01 00:19:25 +00002850// SelectionDAG::Combine - This is the entry point for the file.
2851//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002852void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002853 /// run - This is the main entry point to this class.
2854 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002855 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002856}