blob: ee8d22cb07ec0bf887550c2faac4d02ace1c74af [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
Nate Begeman44728a72005-09-19 22:34:01 +000019// FIXME: select C, pow2, pow2 -> something smart
20// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
Nate Begeman44728a72005-09-19 22:34:01 +000021// FIXME: Dead stores -> nuke
Chris Lattner40c62d52005-10-18 06:04:22 +000022// FIXME: shr X, (and Y,31) -> shr X, Y (TRICKY!)
Nate Begeman1d4d4142005-09-01 00:19:25 +000023// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000024// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000025// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman646d7e22005-09-02 21:18:40 +000026// FIXME: divide by zero is currently left unfolded. do we want to turn this
27// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000028// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Nate Begeman1d4d4142005-09-01 00:19:25 +000029//
30//===----------------------------------------------------------------------===//
31
32#define DEBUG_TYPE "dagcombine"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000035#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000038#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000039#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000040#include <iostream>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041using namespace llvm;
42
43namespace {
44 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
45
46 class DAGCombiner {
47 SelectionDAG &DAG;
48 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000049 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000050
51 // Worklist of all of the nodes that need to be simplified.
52 std::vector<SDNode*> WorkList;
53
54 /// AddUsersToWorkList - When an instruction is simplified, add all users of
55 /// the instruction to the work lists because they might get more simplified
56 /// now.
57 ///
58 void AddUsersToWorkList(SDNode *N) {
59 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000060 UI != UE; ++UI)
61 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000062 }
63
64 /// removeFromWorkList - remove all instances of N from the worklist.
65 void removeFromWorkList(SDNode *N) {
66 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
67 WorkList.end());
68 }
69
Chris Lattner01a22022005-10-10 22:04:48 +000070 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000071 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000072 DEBUG(std::cerr << "\nReplacing "; N->dump();
73 std::cerr << "\nWith: "; To[0].Val->dump();
74 std::cerr << " and " << To.size()-1 << " other values\n");
75 std::vector<SDNode*> NowDead;
76 DAG.ReplaceAllUsesWith(N, To, &NowDead);
77
78 // Push the new nodes and any users onto the worklist
79 for (unsigned i = 0, e = To.size(); i != e; ++i) {
80 WorkList.push_back(To[i].Val);
81 AddUsersToWorkList(To[i].Val);
82 }
83
84 // Nodes can end up on the worklist more than once. Make sure we do
85 // not process a node that has been replaced.
86 removeFromWorkList(N);
87 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
88 removeFromWorkList(NowDead[i]);
89
90 // Finally, since the node is now dead, remove it from the graph.
91 DAG.DeleteNode(N);
92 return SDOperand(N, 0);
93 }
Nate Begeman368e18d2006-02-16 21:11:51 +000094
Chris Lattner012f2412006-02-17 21:58:01 +000095 /// SimplifyDemandedBits - Check the specified integer node value to see if
96 /// it can be simplified or if things is uses can be simplified by bit
97 /// propagation. If so, return true.
98 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +000099 TargetLowering::TargetLoweringOpt TLO(DAG);
100 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000101 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
102 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
103 return false;
104
105 // Revisit the node.
106 WorkList.push_back(Op.Val);
107
108 // Replace the old value with the new one.
109 ++NodesCombined;
110 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
111 std::cerr << "\nWith: "; TLO.New.Val->dump());
112
113 std::vector<SDNode*> NowDead;
114 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
115
Chris Lattner7d20d392006-02-20 06:51:04 +0000116 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000117 WorkList.push_back(TLO.New.Val);
118 AddUsersToWorkList(TLO.New.Val);
119
120 // Nodes can end up on the worklist more than once. Make sure we do
121 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000122 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
123 removeFromWorkList(NowDead[i]);
124
Chris Lattner7d20d392006-02-20 06:51:04 +0000125 // Finally, if the node is now dead, remove it from the graph. The node
126 // may not be dead if the replacement process recursively simplified to
127 // something else needing this node.
128 if (TLO.Old.Val->use_empty()) {
129 removeFromWorkList(TLO.Old.Val);
130 DAG.DeleteNode(TLO.Old.Val);
131 }
Chris Lattner012f2412006-02-17 21:58:01 +0000132 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000133 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000134
135 SDOperand CombineTo(SDNode *N, SDOperand Res) {
136 std::vector<SDOperand> To;
137 To.push_back(Res);
138 return CombineTo(N, To);
139 }
Chris Lattner01a22022005-10-10 22:04:48 +0000140
141 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
142 std::vector<SDOperand> To;
143 To.push_back(Res0);
144 To.push_back(Res1);
145 return CombineTo(N, To);
146 }
147
Nate Begeman1d4d4142005-09-01 00:19:25 +0000148 /// visit - call the node-specific routine that knows how to fold each
149 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000150 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000151
152 // Visitation implementation - Implement dag node combining for different
153 // node types. The semantics are as follows:
154 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000155 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000156 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000157 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000158 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000159 SDOperand visitTokenFactor(SDNode *N);
160 SDOperand visitADD(SDNode *N);
161 SDOperand visitSUB(SDNode *N);
162 SDOperand visitMUL(SDNode *N);
163 SDOperand visitSDIV(SDNode *N);
164 SDOperand visitUDIV(SDNode *N);
165 SDOperand visitSREM(SDNode *N);
166 SDOperand visitUREM(SDNode *N);
167 SDOperand visitMULHU(SDNode *N);
168 SDOperand visitMULHS(SDNode *N);
169 SDOperand visitAND(SDNode *N);
170 SDOperand visitOR(SDNode *N);
171 SDOperand visitXOR(SDNode *N);
172 SDOperand visitSHL(SDNode *N);
173 SDOperand visitSRA(SDNode *N);
174 SDOperand visitSRL(SDNode *N);
175 SDOperand visitCTLZ(SDNode *N);
176 SDOperand visitCTTZ(SDNode *N);
177 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000178 SDOperand visitSELECT(SDNode *N);
179 SDOperand visitSELECT_CC(SDNode *N);
180 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000181 SDOperand visitSIGN_EXTEND(SDNode *N);
182 SDOperand visitZERO_EXTEND(SDNode *N);
183 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
184 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000185 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000186 SDOperand visitFADD(SDNode *N);
187 SDOperand visitFSUB(SDNode *N);
188 SDOperand visitFMUL(SDNode *N);
189 SDOperand visitFDIV(SDNode *N);
190 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000191 SDOperand visitSINT_TO_FP(SDNode *N);
192 SDOperand visitUINT_TO_FP(SDNode *N);
193 SDOperand visitFP_TO_SINT(SDNode *N);
194 SDOperand visitFP_TO_UINT(SDNode *N);
195 SDOperand visitFP_ROUND(SDNode *N);
196 SDOperand visitFP_ROUND_INREG(SDNode *N);
197 SDOperand visitFP_EXTEND(SDNode *N);
198 SDOperand visitFNEG(SDNode *N);
199 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000200 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000201 SDOperand visitBRCONDTWOWAY(SDNode *N);
202 SDOperand visitBR_CC(SDNode *N);
203 SDOperand visitBRTWOWAY_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000204 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000205 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000206
Nate Begemancd4d58c2006-02-03 06:46:56 +0000207 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
208
Chris Lattner40c62d52005-10-18 06:04:22 +0000209 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000210 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
211 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
212 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000213 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000214 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000215
216 SDOperand BuildSDIV(SDNode *N);
217 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000218public:
219 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000220 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000221
222 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000223 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000224 };
225}
226
Nate Begeman69575232005-10-20 02:15:44 +0000227struct ms {
228 int64_t m; // magic number
229 int64_t s; // shift amount
230};
231
232struct mu {
233 uint64_t m; // magic number
234 int64_t a; // add indicator
235 int64_t s; // shift amount
236};
237
238/// magic - calculate the magic numbers required to codegen an integer sdiv as
239/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
240/// or -1.
241static ms magic32(int32_t d) {
242 int32_t p;
243 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
244 const uint32_t two31 = 0x80000000U;
245 struct ms mag;
246
247 ad = abs(d);
248 t = two31 + ((uint32_t)d >> 31);
249 anc = t - 1 - t%ad; // absolute value of nc
250 p = 31; // initialize p
251 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
252 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
253 q2 = two31/ad; // initialize q2 = 2p/abs(d)
254 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
255 do {
256 p = p + 1;
257 q1 = 2*q1; // update q1 = 2p/abs(nc)
258 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
259 if (r1 >= anc) { // must be unsigned comparison
260 q1 = q1 + 1;
261 r1 = r1 - anc;
262 }
263 q2 = 2*q2; // update q2 = 2p/abs(d)
264 r2 = 2*r2; // update r2 = rem(2p/abs(d))
265 if (r2 >= ad) { // must be unsigned comparison
266 q2 = q2 + 1;
267 r2 = r2 - ad;
268 }
269 delta = ad - r2;
270 } while (q1 < delta || (q1 == delta && r1 == 0));
271
272 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
273 if (d < 0) mag.m = -mag.m; // resulting magic number
274 mag.s = p - 32; // resulting shift
275 return mag;
276}
277
278/// magicu - calculate the magic numbers required to codegen an integer udiv as
279/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
280static mu magicu32(uint32_t d) {
281 int32_t p;
282 uint32_t nc, delta, q1, r1, q2, r2;
283 struct mu magu;
284 magu.a = 0; // initialize "add" indicator
285 nc = - 1 - (-d)%d;
286 p = 31; // initialize p
287 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
288 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
289 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
290 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
291 do {
292 p = p + 1;
293 if (r1 >= nc - r1 ) {
294 q1 = 2*q1 + 1; // update q1
295 r1 = 2*r1 - nc; // update r1
296 }
297 else {
298 q1 = 2*q1; // update q1
299 r1 = 2*r1; // update r1
300 }
301 if (r2 + 1 >= d - r2) {
302 if (q2 >= 0x7FFFFFFF) magu.a = 1;
303 q2 = 2*q2 + 1; // update q2
304 r2 = 2*r2 + 1 - d; // update r2
305 }
306 else {
307 if (q2 >= 0x80000000) magu.a = 1;
308 q2 = 2*q2; // update q2
309 r2 = 2*r2 + 1; // update r2
310 }
311 delta = d - 1 - r2;
312 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
313 magu.m = q2 + 1; // resulting magic number
314 magu.s = p - 32; // resulting shift
315 return magu;
316}
317
318/// magic - calculate the magic numbers required to codegen an integer sdiv as
319/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
320/// or -1.
321static ms magic64(int64_t d) {
322 int64_t p;
323 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
324 const uint64_t two63 = 9223372036854775808ULL; // 2^63
325 struct ms mag;
326
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000327 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000328 t = two63 + ((uint64_t)d >> 63);
329 anc = t - 1 - t%ad; // absolute value of nc
330 p = 63; // initialize p
331 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
332 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
333 q2 = two63/ad; // initialize q2 = 2p/abs(d)
334 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
335 do {
336 p = p + 1;
337 q1 = 2*q1; // update q1 = 2p/abs(nc)
338 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
339 if (r1 >= anc) { // must be unsigned comparison
340 q1 = q1 + 1;
341 r1 = r1 - anc;
342 }
343 q2 = 2*q2; // update q2 = 2p/abs(d)
344 r2 = 2*r2; // update r2 = rem(2p/abs(d))
345 if (r2 >= ad) { // must be unsigned comparison
346 q2 = q2 + 1;
347 r2 = r2 - ad;
348 }
349 delta = ad - r2;
350 } while (q1 < delta || (q1 == delta && r1 == 0));
351
352 mag.m = q2 + 1;
353 if (d < 0) mag.m = -mag.m; // resulting magic number
354 mag.s = p - 64; // resulting shift
355 return mag;
356}
357
358/// magicu - calculate the magic numbers required to codegen an integer udiv as
359/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
360static mu magicu64(uint64_t d)
361{
362 int64_t p;
363 uint64_t nc, delta, q1, r1, q2, r2;
364 struct mu magu;
365 magu.a = 0; // initialize "add" indicator
366 nc = - 1 - (-d)%d;
367 p = 63; // initialize p
368 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
369 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
370 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
371 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
372 do {
373 p = p + 1;
374 if (r1 >= nc - r1 ) {
375 q1 = 2*q1 + 1; // update q1
376 r1 = 2*r1 - nc; // update r1
377 }
378 else {
379 q1 = 2*q1; // update q1
380 r1 = 2*r1; // update r1
381 }
382 if (r2 + 1 >= d - r2) {
383 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
384 q2 = 2*q2 + 1; // update q2
385 r2 = 2*r2 + 1 - d; // update r2
386 }
387 else {
388 if (q2 >= 0x8000000000000000ull) magu.a = 1;
389 q2 = 2*q2; // update q2
390 r2 = 2*r2 + 1; // update r2
391 }
392 delta = d - 1 - r2;
393 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
394 magu.m = q2 + 1; // resulting magic number
395 magu.s = p - 64; // resulting shift
396 return magu;
397}
398
Nate Begeman4ebd8052005-09-01 23:24:04 +0000399// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
400// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000401// Also, set the incoming LHS, RHS, and CC references to the appropriate
402// nodes based on the type of node we are checking. This simplifies life a
403// bit for the callers.
404static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
405 SDOperand &CC) {
406 if (N.getOpcode() == ISD::SETCC) {
407 LHS = N.getOperand(0);
408 RHS = N.getOperand(1);
409 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000410 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000411 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000412 if (N.getOpcode() == ISD::SELECT_CC &&
413 N.getOperand(2).getOpcode() == ISD::Constant &&
414 N.getOperand(3).getOpcode() == ISD::Constant &&
415 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000416 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
417 LHS = N.getOperand(0);
418 RHS = N.getOperand(1);
419 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000420 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000421 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000422 return false;
423}
424
Nate Begeman99801192005-09-07 23:25:52 +0000425// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
426// one use. If this is true, it allows the users to invert the operation for
427// free when it is profitable to do so.
428static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000429 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000430 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000431 return true;
432 return false;
433}
434
Nate Begeman452d7be2005-09-16 00:54:12 +0000435// FIXME: This should probably go in the ISD class rather than being duplicated
436// in several files.
437static bool isCommutativeBinOp(unsigned Opcode) {
438 switch (Opcode) {
439 case ISD::ADD:
440 case ISD::MUL:
441 case ISD::AND:
442 case ISD::OR:
443 case ISD::XOR: return true;
444 default: return false; // FIXME: Need commutative info for user ops!
445 }
446}
447
Nate Begemancd4d58c2006-02-03 06:46:56 +0000448SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
449 MVT::ValueType VT = N0.getValueType();
450 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
451 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
452 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
453 if (isa<ConstantSDNode>(N1)) {
454 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
455 WorkList.push_back(OpNode.Val);
456 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
457 } else if (N0.hasOneUse()) {
458 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
459 WorkList.push_back(OpNode.Val);
460 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
461 }
462 }
463 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
464 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
465 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
466 if (isa<ConstantSDNode>(N0)) {
467 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
468 WorkList.push_back(OpNode.Val);
469 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
470 } else if (N1.hasOneUse()) {
471 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
472 WorkList.push_back(OpNode.Val);
473 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
474 }
475 }
476 return SDOperand();
477}
478
Nate Begeman4ebd8052005-09-01 23:24:04 +0000479void DAGCombiner::Run(bool RunningAfterLegalize) {
480 // set the instance variable, so that the various visit routines may use it.
481 AfterLegalize = RunningAfterLegalize;
482
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000484 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
485 E = DAG.allnodes_end(); I != E; ++I)
486 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000487
Chris Lattner95038592005-10-05 06:35:28 +0000488 // Create a dummy node (which is not added to allnodes), that adds a reference
489 // to the root node, preventing it from being deleted, and tracking any
490 // changes of the root.
491 HandleSDNode Dummy(DAG.getRoot());
492
Nate Begeman1d4d4142005-09-01 00:19:25 +0000493 // while the worklist isn't empty, inspect the node on the end of it and
494 // try and combine it.
495 while (!WorkList.empty()) {
496 SDNode *N = WorkList.back();
497 WorkList.pop_back();
498
499 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000500 // N is deleted from the DAG, since they too may now be dead or may have a
501 // reduced number of uses, allowing other xforms.
502 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000503 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
504 WorkList.push_back(N->getOperand(i).Val);
505
Nate Begeman1d4d4142005-09-01 00:19:25 +0000506 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000507 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000508 continue;
509 }
510
Nate Begeman83e75ec2005-09-06 04:43:02 +0000511 SDOperand RV = visit(N);
512 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000513 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000514 // If we get back the same node we passed in, rather than a new node or
515 // zero, we know that the node must have defined multiple values and
516 // CombineTo was used. Since CombineTo takes care of the worklist
517 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000518 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000519 DEBUG(std::cerr << "\nReplacing "; N->dump();
520 std::cerr << "\nWith: "; RV.Val->dump();
521 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000522 std::vector<SDNode*> NowDead;
523 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000524
525 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000526 WorkList.push_back(RV.Val);
527 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000528
529 // Nodes can end up on the worklist more than once. Make sure we do
530 // not process a node that has been replaced.
531 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000532 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
533 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000534
535 // Finally, since the node is now dead, remove it from the graph.
536 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000537 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000538 }
539 }
Chris Lattner95038592005-10-05 06:35:28 +0000540
541 // If the root changed (e.g. it was a dead load, update the root).
542 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543}
544
Nate Begeman83e75ec2005-09-06 04:43:02 +0000545SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000546 switch(N->getOpcode()) {
547 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000548 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000549 case ISD::ADD: return visitADD(N);
550 case ISD::SUB: return visitSUB(N);
551 case ISD::MUL: return visitMUL(N);
552 case ISD::SDIV: return visitSDIV(N);
553 case ISD::UDIV: return visitUDIV(N);
554 case ISD::SREM: return visitSREM(N);
555 case ISD::UREM: return visitUREM(N);
556 case ISD::MULHU: return visitMULHU(N);
557 case ISD::MULHS: return visitMULHS(N);
558 case ISD::AND: return visitAND(N);
559 case ISD::OR: return visitOR(N);
560 case ISD::XOR: return visitXOR(N);
561 case ISD::SHL: return visitSHL(N);
562 case ISD::SRA: return visitSRA(N);
563 case ISD::SRL: return visitSRL(N);
564 case ISD::CTLZ: return visitCTLZ(N);
565 case ISD::CTTZ: return visitCTTZ(N);
566 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000567 case ISD::SELECT: return visitSELECT(N);
568 case ISD::SELECT_CC: return visitSELECT_CC(N);
569 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000570 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
571 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
572 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
573 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000574 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000575 case ISD::FADD: return visitFADD(N);
576 case ISD::FSUB: return visitFSUB(N);
577 case ISD::FMUL: return visitFMUL(N);
578 case ISD::FDIV: return visitFDIV(N);
579 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000580 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
581 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
582 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
583 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
584 case ISD::FP_ROUND: return visitFP_ROUND(N);
585 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
586 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
587 case ISD::FNEG: return visitFNEG(N);
588 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000589 case ISD::BRCOND: return visitBRCOND(N);
590 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
591 case ISD::BR_CC: return visitBR_CC(N);
592 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000593 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000594 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000596 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597}
598
Nate Begeman83e75ec2005-09-06 04:43:02 +0000599SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000600 std::vector<SDOperand> Ops;
601 bool Changed = false;
602
Nate Begeman1d4d4142005-09-01 00:19:25 +0000603 // If the token factor has two operands and one is the entry token, replace
604 // the token factor with the other operand.
605 if (N->getNumOperands() == 2) {
606 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000607 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000609 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000610 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000611
Nate Begemanded49632005-10-13 03:11:28 +0000612 // fold (tokenfactor (tokenfactor)) -> tokenfactor
613 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
614 SDOperand Op = N->getOperand(i);
615 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
616 Changed = true;
617 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
618 Ops.push_back(Op.getOperand(j));
619 } else {
620 Ops.push_back(Op);
621 }
622 }
623 if (Changed)
624 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000625 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000626}
627
Nate Begeman83e75ec2005-09-06 04:43:02 +0000628SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000629 SDOperand N0 = N->getOperand(0);
630 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000631 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
632 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000633 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634
635 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000636 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000637 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000638 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000639 if (N0C && !N1C)
640 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000641 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000642 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000643 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000644 // fold ((c1-A)+c2) -> (c1+c2)-A
645 if (N1C && N0.getOpcode() == ISD::SUB)
646 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
647 return DAG.getNode(ISD::SUB, VT,
648 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
649 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000650 // reassociate add
651 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
652 if (RADD.Val != 0)
653 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000654 // fold ((0-A) + B) -> B-A
655 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
656 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000657 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000658 // fold (A + (0-B)) -> A-B
659 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
660 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000661 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000662 // fold (A+(B-A)) -> B
663 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000664 return N1.getOperand(0);
Nate Begemanb0d04a72006-02-18 02:40:58 +0000665 //
Evan Cheng860771d2006-03-01 01:09:54 +0000666 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemanb0d04a72006-02-18 02:40:58 +0000667 return SDOperand();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000668 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 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000729
730 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
731 if (N1C && N0.getOpcode() == ISD::SHL &&
732 isa<ConstantSDNode>(N0.getOperand(1))) {
733 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
734 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
735 }
736
737 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
738 // use.
739 {
740 SDOperand Sh(0,0), Y(0,0);
741 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
742 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
743 N0.Val->hasOneUse()) {
744 Sh = N0; Y = N1;
745 } else if (N1.getOpcode() == ISD::SHL &&
746 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
747 Sh = N1; Y = N0;
748 }
749 if (Sh.Val) {
750 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
751 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
752 }
753 }
754
755
Nate Begemancd4d58c2006-02-03 06:46:56 +0000756 // reassociate mul
757 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
758 if (RMUL.Val != 0)
759 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000760 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000761}
762
Nate Begeman83e75ec2005-09-06 04:43:02 +0000763SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000764 SDOperand N0 = N->getOperand(0);
765 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000766 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
767 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000768 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000769
770 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000771 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000772 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000773 // fold (sdiv X, 1) -> X
774 if (N1C && N1C->getSignExtended() == 1LL)
775 return N0;
776 // fold (sdiv X, -1) -> 0-X
777 if (N1C && N1C->isAllOnesValue())
778 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000779 // If we know the sign bits of both operands are zero, strength reduce to a
780 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
781 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000782 if (TLI.MaskedValueIsZero(N1, SignBit) &&
783 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000784 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000785 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000786 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000787 (isPowerOf2_64(N1C->getSignExtended()) ||
788 isPowerOf2_64(-N1C->getSignExtended()))) {
789 // If dividing by powers of two is cheap, then don't perform the following
790 // fold.
791 if (TLI.isPow2DivCheap())
792 return SDOperand();
793 int64_t pow2 = N1C->getSignExtended();
794 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000795 unsigned lg2 = Log2_64(abs2);
796 // Splat the sign bit into the register
797 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000798 DAG.getConstant(MVT::getSizeInBits(VT)-1,
799 TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000800 WorkList.push_back(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000801 // Add (N0 < 0) ? abs2 - 1 : 0;
802 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
803 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000804 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000805 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
806 WorkList.push_back(SRL.Val);
807 WorkList.push_back(ADD.Val); // Divide by pow2
808 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
809 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000810 // If we're dividing by a positive value, we're done. Otherwise, we must
811 // negate the result.
812 if (pow2 > 0)
813 return SRA;
814 WorkList.push_back(SRA.Val);
815 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
816 }
Nate Begeman69575232005-10-20 02:15:44 +0000817 // if integer divide is expensive and we satisfy the requirements, emit an
818 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000819 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000820 !TLI.isIntDivCheap()) {
821 SDOperand Op = BuildSDIV(N);
822 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000823 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000824 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000825}
826
Nate Begeman83e75ec2005-09-06 04:43:02 +0000827SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000828 SDOperand N0 = N->getOperand(0);
829 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000830 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
831 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000832 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000833
834 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000835 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000836 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000837 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000838 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000839 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000840 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000841 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000842 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
843 if (N1.getOpcode() == ISD::SHL) {
844 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
845 if (isPowerOf2_64(SHC->getValue())) {
846 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000847 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
848 DAG.getConstant(Log2_64(SHC->getValue()),
849 ADDVT));
850 WorkList.push_back(Add.Val);
851 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000852 }
853 }
854 }
Nate Begeman69575232005-10-20 02:15:44 +0000855 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000856 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
857 SDOperand Op = BuildUDIV(N);
858 if (Op.Val) return Op;
859 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000860 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000861}
862
Nate Begeman83e75ec2005-09-06 04:43:02 +0000863SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000864 SDOperand N0 = N->getOperand(0);
865 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000866 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
867 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000868 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000869
870 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000871 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000872 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000873 // If we know the sign bits of both operands are zero, strength reduce to a
874 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
875 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000876 if (TLI.MaskedValueIsZero(N1, SignBit) &&
877 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000878 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000879 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000880}
881
Nate Begeman83e75ec2005-09-06 04:43:02 +0000882SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000883 SDOperand N0 = N->getOperand(0);
884 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000885 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
886 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000887 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000888
889 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000890 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000891 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000892 // fold (urem x, pow2) -> (and x, pow2-1)
893 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000894 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000895 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
896 if (N1.getOpcode() == ISD::SHL) {
897 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
898 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000899 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000900 WorkList.push_back(Add.Val);
901 return DAG.getNode(ISD::AND, VT, N0, Add);
902 }
903 }
904 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000905 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000906}
907
Nate Begeman83e75ec2005-09-06 04:43:02 +0000908SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000909 SDOperand N0 = N->getOperand(0);
910 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000911 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000912
913 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000914 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000915 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000916 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000917 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
919 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000920 TLI.getShiftAmountTy()));
921 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922}
923
Nate Begeman83e75ec2005-09-06 04:43:02 +0000924SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000925 SDOperand N0 = N->getOperand(0);
926 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000927 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928
929 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000930 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000931 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000933 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000934 return DAG.getConstant(0, N0.getValueType());
935 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000936}
937
Nate Begeman83e75ec2005-09-06 04:43:02 +0000938SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000939 SDOperand N0 = N->getOperand(0);
940 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000941 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000942 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
943 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000945 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000946
947 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000948 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000949 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000950 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000951 if (N0C && !N1C)
952 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000953 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000954 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000955 return N0;
956 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000957 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000958 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000959 // reassociate and
960 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
961 if (RAND.Val != 0)
962 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000964 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000965 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000966 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000967 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000968 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
969 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
970 unsigned InBits = MVT::getSizeInBits(N0.getOperand(0).getValueType());
971 if (TLI.MaskedValueIsZero(N0.getOperand(0),
972 ~N1C->getValue() & ((1ULL << InBits)-1))) {
973 // We actually want to replace all uses of the any_extend with the
974 // zero_extend, to avoid duplicating things. This will later cause this
975 // AND to be folded.
976 CombineTo(N0.Val, DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
977 N0.getOperand(0)));
978 return SDOperand();
979 }
980 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000981 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
982 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
983 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
984 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
985
986 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
987 MVT::isInteger(LL.getValueType())) {
988 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
989 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
990 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
991 WorkList.push_back(ORNode.Val);
992 return DAG.getSetCC(VT, ORNode, LR, Op1);
993 }
994 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
995 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
996 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
997 WorkList.push_back(ANDNode.Val);
998 return DAG.getSetCC(VT, ANDNode, LR, Op1);
999 }
1000 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1001 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1002 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1003 WorkList.push_back(ORNode.Val);
1004 return DAG.getSetCC(VT, ORNode, LR, Op1);
1005 }
1006 }
1007 // canonicalize equivalent to ll == rl
1008 if (LL == RR && LR == RL) {
1009 Op1 = ISD::getSetCCSwappedOperands(Op1);
1010 std::swap(RL, RR);
1011 }
1012 if (LL == RL && LR == RR) {
1013 bool isInteger = MVT::isInteger(LL.getValueType());
1014 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1015 if (Result != ISD::SETCC_INVALID)
1016 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1017 }
1018 }
1019 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1020 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1021 N1.getOpcode() == ISD::ZERO_EXTEND &&
1022 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1023 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1024 N0.getOperand(0), N1.getOperand(0));
1025 WorkList.push_back(ANDNode.Val);
1026 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1027 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001028 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001029 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001030 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1031 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001032 N0.getOperand(1) == N1.getOperand(1)) {
1033 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1034 N0.getOperand(0), N1.getOperand(0));
1035 WorkList.push_back(ANDNode.Val);
1036 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1037 }
Nate Begemande996292006-02-03 22:24:05 +00001038 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1039 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner012f2412006-02-17 21:58:01 +00001040 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001041 return SDOperand();
Nate Begemanded49632005-10-13 03:11:28 +00001042 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001043 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001044 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001045 // If we zero all the possible extended bits, then we can turn this into
1046 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001047 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001048 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001049 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1050 N0.getOperand(1), N0.getOperand(2),
1051 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001052 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001053 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001054 return SDOperand();
1055 }
1056 }
1057 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001058 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001059 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001060 // If we zero all the possible extended bits, then we can turn this into
1061 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001062 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001063 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001064 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1065 N0.getOperand(1), N0.getOperand(2),
1066 EVT);
Nate Begemanded49632005-10-13 03:11:28 +00001067 WorkList.push_back(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001068 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001069 return SDOperand();
1070 }
1071 }
Chris Lattner15045b62006-02-28 06:35:35 +00001072
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001073 // fold (and (load x), 255) -> (zextload x, i8)
1074 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1075 if (N1C &&
1076 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1077 N0.getOpcode() == ISD::ZEXTLOAD) &&
1078 N0.hasOneUse()) {
1079 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001080 if (N1C->getValue() == 255)
1081 EVT = MVT::i8;
1082 else if (N1C->getValue() == 65535)
1083 EVT = MVT::i16;
1084 else if (N1C->getValue() == ~0U)
1085 EVT = MVT::i32;
1086 else
1087 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001088
1089 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1090 cast<VTSDNode>(N0.getOperand(3))->getVT();
1091 if (EVT != MVT::Other && LoadedVT > EVT) {
Chris Lattner15045b62006-02-28 06:35:35 +00001092 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1093 // For big endian targets, we need to add an offset to the pointer to load
1094 // the correct bytes. For little endian systems, we merely need to read
1095 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001096 unsigned PtrOff =
1097 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1098 SDOperand NewPtr = N0.getOperand(1);
1099 if (!TLI.isLittleEndian())
1100 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1101 DAG.getConstant(PtrOff, PtrType));
Chris Lattner15045b62006-02-28 06:35:35 +00001102 WorkList.push_back(NewPtr.Val);
1103 SDOperand Load =
1104 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1105 N0.getOperand(2), EVT);
1106 WorkList.push_back(N);
1107 CombineTo(N0.Val, Load, Load.getValue(1));
1108 return SDOperand();
1109 }
1110 }
1111
Nate Begeman83e75ec2005-09-06 04:43:02 +00001112 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001113}
1114
Nate Begeman83e75ec2005-09-06 04:43:02 +00001115SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001116 SDOperand N0 = N->getOperand(0);
1117 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001118 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001119 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1120 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001121 MVT::ValueType VT = N1.getValueType();
1122 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001123
1124 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001125 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001126 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001127 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001128 if (N0C && !N1C)
1129 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001130 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001131 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001132 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001133 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001134 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001135 return N1;
1136 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001137 if (N1C &&
1138 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001139 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001140 // reassociate or
1141 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1142 if (ROR.Val != 0)
1143 return ROR;
1144 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1145 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001146 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001147 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1148 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1149 N1),
1150 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001151 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001152 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1153 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1154 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1155 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1156
1157 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1158 MVT::isInteger(LL.getValueType())) {
1159 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1160 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1161 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1162 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1163 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
1164 WorkList.push_back(ORNode.Val);
1165 return DAG.getSetCC(VT, ORNode, LR, Op1);
1166 }
1167 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1168 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1169 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1170 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1171 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
1172 WorkList.push_back(ANDNode.Val);
1173 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1174 }
1175 }
1176 // canonicalize equivalent to ll == rl
1177 if (LL == RR && LR == RL) {
1178 Op1 = ISD::getSetCCSwappedOperands(Op1);
1179 std::swap(RL, RR);
1180 }
1181 if (LL == RL && LR == RR) {
1182 bool isInteger = MVT::isInteger(LL.getValueType());
1183 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1184 if (Result != ISD::SETCC_INVALID)
1185 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1186 }
1187 }
1188 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1189 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1190 N1.getOpcode() == ISD::ZERO_EXTEND &&
1191 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1192 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1193 N0.getOperand(0), N1.getOperand(0));
1194 WorkList.push_back(ORNode.Val);
1195 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1196 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001197 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1198 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1199 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1200 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1201 N0.getOperand(1) == N1.getOperand(1)) {
1202 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1203 N0.getOperand(0), N1.getOperand(0));
1204 WorkList.push_back(ORNode.Val);
1205 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1206 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001207 // canonicalize shl to left side in a shl/srl pair, to match rotate
1208 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1209 std::swap(N0, N1);
1210 // check for rotl, rotr
1211 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1212 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001213 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001214 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1215 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1216 N1.getOperand(1).getOpcode() == ISD::Constant) {
1217 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1218 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1219 if ((c1val + c2val) == OpSizeInBits)
1220 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1221 }
1222 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1223 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1224 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1225 if (ConstantSDNode *SUBC =
1226 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1227 if (SUBC->getValue() == OpSizeInBits)
1228 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1229 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1230 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1231 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1232 if (ConstantSDNode *SUBC =
1233 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1234 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001235 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001236 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1237 N1.getOperand(1));
1238 else
1239 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1240 N0.getOperand(1));
1241 }
1242 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001243 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001244}
1245
Nate Begeman83e75ec2005-09-06 04:43:02 +00001246SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001247 SDOperand N0 = N->getOperand(0);
1248 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001249 SDOperand LHS, RHS, CC;
1250 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1251 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001252 MVT::ValueType VT = N0.getValueType();
1253
1254 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001255 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001256 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001257 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001258 if (N0C && !N1C)
1259 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001260 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001261 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001262 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001263 // reassociate xor
1264 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1265 if (RXOR.Val != 0)
1266 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001267 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001268 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1269 bool isInt = MVT::isInteger(LHS.getValueType());
1270 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1271 isInt);
1272 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001273 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001274 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001275 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001276 assert(0 && "Unhandled SetCC Equivalent!");
1277 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001278 }
Nate Begeman99801192005-09-07 23:25:52 +00001279 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1280 if (N1C && N1C->getValue() == 1 &&
1281 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001282 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001283 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1284 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001285 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1286 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001287 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1288 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001289 }
1290 }
Nate Begeman99801192005-09-07 23:25:52 +00001291 // fold !(x or y) -> (!x and !y) iff x or y are constants
1292 if (N1C && N1C->isAllOnesValue() &&
1293 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001294 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001295 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1296 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1298 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +00001299 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
1300 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001301 }
1302 }
Nate Begeman223df222005-09-08 20:18:10 +00001303 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1304 if (N1C && N0.getOpcode() == ISD::XOR) {
1305 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1306 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1307 if (N00C)
1308 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1309 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1310 if (N01C)
1311 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1312 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1313 }
1314 // fold (xor x, x) -> 0
1315 if (N0 == N1)
1316 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001317 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1318 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1319 N1.getOpcode() == ISD::ZERO_EXTEND &&
1320 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1321 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1322 N0.getOperand(0), N1.getOperand(0));
1323 WorkList.push_back(XORNode.Val);
1324 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1325 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001326 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1327 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1328 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1329 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1330 N0.getOperand(1) == N1.getOperand(1)) {
1331 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1332 N0.getOperand(0), N1.getOperand(0));
1333 WorkList.push_back(XORNode.Val);
1334 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1335 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001336 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337}
1338
Nate Begeman83e75ec2005-09-06 04:43:02 +00001339SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340 SDOperand N0 = N->getOperand(0);
1341 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001342 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1343 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001344 MVT::ValueType VT = N0.getValueType();
1345 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1346
1347 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001348 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001349 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001351 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001352 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001353 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001354 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001355 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001357 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001358 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001359 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001360 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001361 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001362 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001363 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001365 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001366 N0.getOperand(1).getOpcode() == ISD::Constant) {
1367 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001370 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001371 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001372 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001373 }
1374 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1375 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001376 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377 N0.getOperand(1).getOpcode() == ISD::Constant) {
1378 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001379 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001380 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1381 DAG.getConstant(~0ULL << c1, VT));
1382 if (c2 > c1)
1383 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001384 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001386 return DAG.getNode(ISD::SRL, VT, Mask,
1387 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001388 }
1389 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001390 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001391 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001392 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1393 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001394}
1395
Nate Begeman83e75ec2005-09-06 04:43:02 +00001396SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397 SDOperand N0 = N->getOperand(0);
1398 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001399 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1400 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001401 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001402
1403 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001404 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001405 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001406 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001407 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001408 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001409 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001410 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001411 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001412 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001413 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001414 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001415 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001416 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001417 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001418 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1419 // sext_inreg.
1420 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1421 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1422 MVT::ValueType EVT;
1423 switch (LowBits) {
1424 default: EVT = MVT::Other; break;
1425 case 1: EVT = MVT::i1; break;
1426 case 8: EVT = MVT::i8; break;
1427 case 16: EVT = MVT::i16; break;
1428 case 32: EVT = MVT::i32; break;
1429 }
1430 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1431 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1432 DAG.getValueType(EVT));
1433 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001434
1435 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1436 if (N1C && N0.getOpcode() == ISD::SRA) {
1437 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1438 unsigned Sum = N1C->getValue() + C1->getValue();
1439 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1440 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1441 DAG.getConstant(Sum, N1C->getValueType(0)));
1442 }
1443 }
1444
Nate Begeman1d4d4142005-09-01 00:19:25 +00001445 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001446 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001447 return DAG.getNode(ISD::SRL, VT, N0, N1);
1448 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001449}
1450
Nate Begeman83e75ec2005-09-06 04:43:02 +00001451SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001452 SDOperand N0 = N->getOperand(0);
1453 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001454 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1455 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 MVT::ValueType VT = N0.getValueType();
1457 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1458
1459 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001460 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001461 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001462 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001463 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001464 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001465 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001466 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001467 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001468 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001469 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001470 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001472 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001473 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001474 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001475 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001476 N0.getOperand(1).getOpcode() == ISD::Constant) {
1477 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001478 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001479 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001480 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001481 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001482 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001483 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001484 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001485}
1486
Nate Begeman83e75ec2005-09-06 04:43:02 +00001487SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001488 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001489 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001490 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491
1492 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001493 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001494 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001495 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001496}
1497
Nate Begeman83e75ec2005-09-06 04:43:02 +00001498SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001499 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001500 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001501 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001502
1503 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001504 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001505 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001506 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001507}
1508
Nate Begeman83e75ec2005-09-06 04:43:02 +00001509SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001510 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001511 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001512 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001513
1514 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001515 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001516 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001517 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001518}
1519
Nate Begeman452d7be2005-09-16 00:54:12 +00001520SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1521 SDOperand N0 = N->getOperand(0);
1522 SDOperand N1 = N->getOperand(1);
1523 SDOperand N2 = N->getOperand(2);
1524 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1525 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1526 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1527 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001528
Nate Begeman452d7be2005-09-16 00:54:12 +00001529 // fold select C, X, X -> X
1530 if (N1 == N2)
1531 return N1;
1532 // fold select true, X, Y -> X
1533 if (N0C && !N0C->isNullValue())
1534 return N1;
1535 // fold select false, X, Y -> Y
1536 if (N0C && N0C->isNullValue())
1537 return N2;
1538 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001539 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001540 return DAG.getNode(ISD::OR, VT, N0, N2);
1541 // fold select C, 0, X -> ~C & X
1542 // FIXME: this should check for C type == X type, not i1?
1543 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1544 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1545 WorkList.push_back(XORNode.Val);
1546 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1547 }
1548 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001549 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001550 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1551 WorkList.push_back(XORNode.Val);
1552 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1553 }
1554 // fold select C, X, 0 -> C & X
1555 // FIXME: this should check for C type == X type, not i1?
1556 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1557 return DAG.getNode(ISD::AND, VT, N0, N1);
1558 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1559 if (MVT::i1 == VT && N0 == N1)
1560 return DAG.getNode(ISD::OR, VT, N0, N2);
1561 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1562 if (MVT::i1 == VT && N0 == N2)
1563 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001564 // If we can fold this based on the true/false value, do so.
1565 if (SimplifySelectOps(N, N1, N2))
1566 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001567 // fold selects based on a setcc into other things, such as min/max/abs
1568 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001569 // FIXME:
1570 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1571 // having to say they don't support SELECT_CC on every type the DAG knows
1572 // about, since there is no way to mark an opcode illegal at all value types
1573 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1574 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1575 N1, N2, N0.getOperand(2));
1576 else
1577 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001578 return SDOperand();
1579}
1580
1581SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001582 SDOperand N0 = N->getOperand(0);
1583 SDOperand N1 = N->getOperand(1);
1584 SDOperand N2 = N->getOperand(2);
1585 SDOperand N3 = N->getOperand(3);
1586 SDOperand N4 = N->getOperand(4);
1587 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1588 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1589 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1590 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1591
1592 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001593 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001594 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1595
Nate Begeman44728a72005-09-19 22:34:01 +00001596 // fold select_cc lhs, rhs, x, x, cc -> x
1597 if (N2 == N3)
1598 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001599
1600 // If we can fold this based on the true/false value, do so.
1601 if (SimplifySelectOps(N, N2, N3))
1602 return SDOperand();
1603
Nate Begeman44728a72005-09-19 22:34:01 +00001604 // fold select_cc into other things, such as min/max/abs
1605 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001606}
1607
1608SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1609 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1610 cast<CondCodeSDNode>(N->getOperand(2))->get());
1611}
1612
Nate Begeman83e75ec2005-09-06 04:43:02 +00001613SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001614 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001615 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001616 MVT::ValueType VT = N->getValueType(0);
1617
Nate Begeman1d4d4142005-09-01 00:19:25 +00001618 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001619 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001620 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621 // fold (sext (sext x)) -> (sext x)
1622 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001623 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001624 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001625 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1626 (!AfterLegalize ||
1627 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001628 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1629 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001630 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001631 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1632 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001633 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1634 N0.getOperand(1), N0.getOperand(2),
1635 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001636 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001637 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1638 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001639 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001640 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001641
1642 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1643 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1644 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1645 N0.hasOneUse()) {
1646 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1647 N0.getOperand(1), N0.getOperand(2),
1648 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001649 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001650 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1651 ExtLoad.getValue(1));
1652 return SDOperand();
1653 }
1654
Nate Begeman83e75ec2005-09-06 04:43:02 +00001655 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001656}
1657
Nate Begeman83e75ec2005-09-06 04:43:02 +00001658SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001659 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001660 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001661 MVT::ValueType VT = N->getValueType(0);
1662
Nate Begeman1d4d4142005-09-01 00:19:25 +00001663 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001664 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001665 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001666 // fold (zext (zext x)) -> (zext x)
1667 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001668 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001669 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1670 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001671 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001672 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001673 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001674 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1675 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001676 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1677 N0.getOperand(1), N0.getOperand(2),
1678 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001679 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001680 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1681 ExtLoad.getValue(1));
1682 return SDOperand();
1683 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001684
1685 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1686 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1687 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1688 N0.hasOneUse()) {
1689 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1690 N0.getOperand(1), N0.getOperand(2),
1691 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001692 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001693 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1694 ExtLoad.getValue(1));
1695 return SDOperand();
1696 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001697 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001698}
1699
Nate Begeman83e75ec2005-09-06 04:43:02 +00001700SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001701 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001702 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001703 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001704 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001705 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001706 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001707
Nate Begeman1d4d4142005-09-01 00:19:25 +00001708 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001709 if (N0C) {
1710 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001711 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001712 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001713 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001714 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001715 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001716 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001717 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001718 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1719 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1720 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001721 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001722 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001723 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1724 if (N0.getOpcode() == ISD::AssertSext &&
1725 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001726 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001727 }
1728 // fold (sext_in_reg (sextload x)) -> (sextload x)
1729 if (N0.getOpcode() == ISD::SEXTLOAD &&
1730 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001731 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001732 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001733 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001734 if (N0.getOpcode() == ISD::SETCC &&
1735 TLI.getSetCCResultContents() ==
1736 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001737 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001738 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001739 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001740 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001741 // fold (sext_in_reg (srl x)) -> sra x
1742 if (N0.getOpcode() == ISD::SRL &&
1743 N0.getOperand(1).getOpcode() == ISD::Constant &&
1744 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1745 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1746 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001747 }
Nate Begemanded49632005-10-13 03:11:28 +00001748 // fold (sext_inreg (extload x)) -> (sextload x)
1749 if (N0.getOpcode() == ISD::EXTLOAD &&
1750 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001751 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001752 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1753 N0.getOperand(1), N0.getOperand(2),
1754 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001755 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001756 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001757 return SDOperand();
1758 }
1759 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001760 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001761 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001762 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001763 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1764 N0.getOperand(1), N0.getOperand(2),
1765 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001766 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001767 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001768 return SDOperand();
1769 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001770 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001771}
1772
Nate Begeman83e75ec2005-09-06 04:43:02 +00001773SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001774 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001775 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001776 MVT::ValueType VT = N->getValueType(0);
1777
1778 // noop truncate
1779 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001780 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001782 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001783 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001784 // fold (truncate (truncate x)) -> (truncate x)
1785 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001786 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001787 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1788 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1789 if (N0.getValueType() < VT)
1790 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001791 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001792 else if (N0.getValueType() > VT)
1793 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001794 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001795 else
1796 // if the source and dest are the same type, we can drop both the extend
1797 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001798 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001799 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001800 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001801 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001802 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1803 "Cannot truncate to larger type!");
1804 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001805 // For big endian targets, we need to add an offset to the pointer to load
1806 // the correct bytes. For little endian systems, we merely need to read
1807 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001808 uint64_t PtrOff =
1809 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001810 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1811 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1812 DAG.getConstant(PtrOff, PtrType));
1813 WorkList.push_back(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001814 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Nate Begeman765784a2005-10-12 23:18:53 +00001815 WorkList.push_back(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001816 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001817 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001818 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001819 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001820}
1821
Chris Lattner94683772005-12-23 05:30:37 +00001822SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1823 SDOperand N0 = N->getOperand(0);
1824 MVT::ValueType VT = N->getValueType(0);
1825
1826 // If the input is a constant, let getNode() fold it.
1827 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1828 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1829 if (Res.Val != N) return Res;
1830 }
1831
Chris Lattnerc8547d82005-12-23 05:37:50 +00001832 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1833 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1834
Chris Lattner57104102005-12-23 05:44:41 +00001835 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001836 // FIXME: These xforms need to know that the resultant load doesn't need a
1837 // higher alignment than the original!
1838 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001839 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1840 N0.getOperand(2));
1841 WorkList.push_back(N);
1842 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1843 Load.getValue(1));
1844 return Load;
1845 }
1846
Chris Lattner94683772005-12-23 05:30:37 +00001847 return SDOperand();
1848}
1849
Chris Lattner01b3d732005-09-28 22:28:18 +00001850SDOperand DAGCombiner::visitFADD(SDNode *N) {
1851 SDOperand N0 = N->getOperand(0);
1852 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001853 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1854 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001855 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001856
1857 // fold (fadd c1, c2) -> c1+c2
1858 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001859 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001860 // canonicalize constant to RHS
1861 if (N0CFP && !N1CFP)
1862 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001863 // fold (A + (-B)) -> A-B
1864 if (N1.getOpcode() == ISD::FNEG)
1865 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001866 // fold ((-A) + B) -> B-A
1867 if (N0.getOpcode() == ISD::FNEG)
1868 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001869 return SDOperand();
1870}
1871
1872SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1873 SDOperand N0 = N->getOperand(0);
1874 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001875 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1876 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001877 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001878
1879 // fold (fsub c1, c2) -> c1-c2
1880 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001881 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001882 // fold (A-(-B)) -> A+B
1883 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001884 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001885 return SDOperand();
1886}
1887
1888SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1889 SDOperand N0 = N->getOperand(0);
1890 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001891 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1892 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001893 MVT::ValueType VT = N->getValueType(0);
1894
Nate Begeman11af4ea2005-10-17 20:40:11 +00001895 // fold (fmul c1, c2) -> c1*c2
1896 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001897 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001898 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001899 if (N0CFP && !N1CFP)
1900 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001901 // fold (fmul X, 2.0) -> (fadd X, X)
1902 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1903 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001904 return SDOperand();
1905}
1906
1907SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1908 SDOperand N0 = N->getOperand(0);
1909 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001910 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1911 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001912 MVT::ValueType VT = N->getValueType(0);
1913
Nate Begemana148d982006-01-18 22:35:16 +00001914 // fold (fdiv c1, c2) -> c1/c2
1915 if (N0CFP && N1CFP)
1916 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001917 return SDOperand();
1918}
1919
1920SDOperand DAGCombiner::visitFREM(SDNode *N) {
1921 SDOperand N0 = N->getOperand(0);
1922 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001923 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1924 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001925 MVT::ValueType VT = N->getValueType(0);
1926
Nate Begemana148d982006-01-18 22:35:16 +00001927 // fold (frem c1, c2) -> fmod(c1,c2)
1928 if (N0CFP && N1CFP)
1929 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001930 return SDOperand();
1931}
1932
1933
Nate Begeman83e75ec2005-09-06 04:43:02 +00001934SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001935 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001936 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001937 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001938
1939 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001940 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001941 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001942 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001943}
1944
Nate Begeman83e75ec2005-09-06 04:43:02 +00001945SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001946 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001947 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001948 MVT::ValueType VT = N->getValueType(0);
1949
Nate Begeman1d4d4142005-09-01 00:19:25 +00001950 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001951 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001952 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001953 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001954}
1955
Nate Begeman83e75ec2005-09-06 04:43:02 +00001956SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001957 SDOperand N0 = N->getOperand(0);
1958 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1959 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001960
1961 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001962 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001963 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001964 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001965}
1966
Nate Begeman83e75ec2005-09-06 04:43:02 +00001967SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001968 SDOperand N0 = N->getOperand(0);
1969 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1970 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001971
1972 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001973 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001974 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001975 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001976}
1977
Nate Begeman83e75ec2005-09-06 04:43:02 +00001978SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001979 SDOperand N0 = N->getOperand(0);
1980 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1981 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001982
1983 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001984 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001985 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001986 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001987}
1988
Nate Begeman83e75ec2005-09-06 04:43:02 +00001989SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001990 SDOperand N0 = N->getOperand(0);
1991 MVT::ValueType VT = N->getValueType(0);
1992 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001993 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001994
Nate Begeman1d4d4142005-09-01 00:19:25 +00001995 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001996 if (N0CFP) {
1997 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001998 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001999 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002000 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002001}
2002
Nate Begeman83e75ec2005-09-06 04:43:02 +00002003SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002004 SDOperand N0 = N->getOperand(0);
2005 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2006 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002007
2008 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002009 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002010 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002011 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002012}
2013
Nate Begeman83e75ec2005-09-06 04:43:02 +00002014SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002015 SDOperand N0 = N->getOperand(0);
2016 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2017 MVT::ValueType VT = N->getValueType(0);
2018
2019 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002020 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002021 return DAG.getNode(ISD::FNEG, VT, N0);
2022 // fold (fneg (sub x, y)) -> (sub y, x)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002023 if (N->getOperand(0).getOpcode() == ISD::SUB)
Nate Begemana148d982006-01-18 22:35:16 +00002024 return DAG.getNode(ISD::SUB, VT, N->getOperand(1), N->getOperand(0));
2025 // fold (fneg (fneg x)) -> x
Nate Begeman1d4d4142005-09-01 00:19:25 +00002026 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002027 return N->getOperand(0).getOperand(0);
2028 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002029}
2030
Nate Begeman83e75ec2005-09-06 04:43:02 +00002031SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002032 SDOperand N0 = N->getOperand(0);
2033 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2034 MVT::ValueType VT = N->getValueType(0);
2035
Nate Begeman1d4d4142005-09-01 00:19:25 +00002036 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002037 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002038 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002039 // fold (fabs (fabs x)) -> (fabs x)
2040 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002041 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002042 // fold (fabs (fneg x)) -> (fabs x)
2043 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002044 return DAG.getNode(ISD::FABS, VT, N->getOperand(0).getOperand(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +00002045 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002046}
2047
Nate Begeman44728a72005-09-19 22:34:01 +00002048SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2049 SDOperand Chain = N->getOperand(0);
2050 SDOperand N1 = N->getOperand(1);
2051 SDOperand N2 = N->getOperand(2);
2052 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2053
2054 // never taken branch, fold to chain
2055 if (N1C && N1C->isNullValue())
2056 return Chain;
2057 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002058 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002059 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002060 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2061 // on the target.
2062 if (N1.getOpcode() == ISD::SETCC &&
2063 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2064 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2065 N1.getOperand(0), N1.getOperand(1), N2);
2066 }
Nate Begeman44728a72005-09-19 22:34:01 +00002067 return SDOperand();
2068}
2069
2070SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
2071 SDOperand Chain = N->getOperand(0);
2072 SDOperand N1 = N->getOperand(1);
2073 SDOperand N2 = N->getOperand(2);
2074 SDOperand N3 = N->getOperand(3);
2075 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2076
2077 // unconditional branch to true mbb
2078 if (N1C && N1C->getValue() == 1)
2079 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2080 // unconditional branch to false mbb
2081 if (N1C && N1C->isNullValue())
2082 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002083 // fold a brcondtwoway with a setcc condition into a BRTWOWAY_CC node if
2084 // BRTWOWAY_CC is legal on the target.
2085 if (N1.getOpcode() == ISD::SETCC &&
2086 TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
2087 std::vector<SDOperand> Ops;
2088 Ops.push_back(Chain);
2089 Ops.push_back(N1.getOperand(2));
2090 Ops.push_back(N1.getOperand(0));
2091 Ops.push_back(N1.getOperand(1));
2092 Ops.push_back(N2);
2093 Ops.push_back(N3);
2094 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2095 }
Nate Begeman44728a72005-09-19 22:34:01 +00002096 return SDOperand();
2097}
2098
Chris Lattner3ea0b472005-10-05 06:47:48 +00002099// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2100//
Nate Begeman44728a72005-09-19 22:34:01 +00002101SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002102 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2103 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2104
2105 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002106 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2107 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2108
2109 // fold br_cc true, dest -> br dest (unconditional branch)
2110 if (SCCC && SCCC->getValue())
2111 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2112 N->getOperand(4));
2113 // fold br_cc false, dest -> unconditional fall through
2114 if (SCCC && SCCC->isNullValue())
2115 return N->getOperand(0);
2116 // fold to a simpler setcc
2117 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2118 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2119 Simp.getOperand(2), Simp.getOperand(0),
2120 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002121 return SDOperand();
2122}
2123
2124SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00002125 SDOperand Chain = N->getOperand(0);
2126 SDOperand CCN = N->getOperand(1);
2127 SDOperand LHS = N->getOperand(2);
2128 SDOperand RHS = N->getOperand(3);
2129 SDOperand N4 = N->getOperand(4);
2130 SDOperand N5 = N->getOperand(5);
2131
2132 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
2133 cast<CondCodeSDNode>(CCN)->get(), false);
2134 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2135
2136 // fold select_cc lhs, rhs, x, x, cc -> x
2137 if (N4 == N5)
2138 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2139 // fold select_cc true, x, y -> x
2140 if (SCCC && SCCC->getValue())
2141 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2142 // fold select_cc false, x, y -> y
2143 if (SCCC && SCCC->isNullValue())
2144 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
2145 // fold to a simpler setcc
Chris Lattner03d5e872006-01-29 06:00:45 +00002146 if (SCC.Val && SCC.getOpcode() == ISD::SETCC) {
2147 std::vector<SDOperand> Ops;
2148 Ops.push_back(Chain);
2149 Ops.push_back(SCC.getOperand(2));
2150 Ops.push_back(SCC.getOperand(0));
2151 Ops.push_back(SCC.getOperand(1));
2152 Ops.push_back(N4);
2153 Ops.push_back(N5);
2154 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2155 }
Nate Begeman44728a72005-09-19 22:34:01 +00002156 return SDOperand();
2157}
2158
Chris Lattner01a22022005-10-10 22:04:48 +00002159SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2160 SDOperand Chain = N->getOperand(0);
2161 SDOperand Ptr = N->getOperand(1);
2162 SDOperand SrcValue = N->getOperand(2);
2163
2164 // If this load is directly stored, replace the load value with the stored
2165 // value.
2166 // TODO: Handle store large -> read small portion.
2167 // TODO: Handle TRUNCSTORE/EXTLOAD
2168 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2169 Chain.getOperand(1).getValueType() == N->getValueType(0))
2170 return CombineTo(N, Chain.getOperand(1), Chain);
2171
2172 return SDOperand();
2173}
2174
Chris Lattner87514ca2005-10-10 22:31:19 +00002175SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2176 SDOperand Chain = N->getOperand(0);
2177 SDOperand Value = N->getOperand(1);
2178 SDOperand Ptr = N->getOperand(2);
2179 SDOperand SrcValue = N->getOperand(3);
2180
2181 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002182 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002183 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2184 // Make sure that these stores are the same value type:
2185 // FIXME: we really care that the second store is >= size of the first.
2186 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002187 // Create a new store of Value that replaces both stores.
2188 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002189 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2190 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002191 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2192 PrevStore->getOperand(0), Value, Ptr,
2193 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002194 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002195 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002196 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002197 }
2198
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002199 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002200 // FIXME: This needs to know that the resultant store does not need a
2201 // higher alignment than the original.
2202 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002203 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2204 Ptr, SrcValue);
2205
Chris Lattner87514ca2005-10-10 22:31:19 +00002206 return SDOperand();
2207}
2208
Nate Begeman44728a72005-09-19 22:34:01 +00002209SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002210 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2211
2212 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2213 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2214 // If we got a simplified select_cc node back from SimplifySelectCC, then
2215 // break it down into a new SETCC node, and a new SELECT node, and then return
2216 // the SELECT node, since we were called with a SELECT node.
2217 if (SCC.Val) {
2218 // Check to see if we got a select_cc back (to turn into setcc/select).
2219 // Otherwise, just return whatever node we got back, like fabs.
2220 if (SCC.getOpcode() == ISD::SELECT_CC) {
2221 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2222 SCC.getOperand(0), SCC.getOperand(1),
2223 SCC.getOperand(4));
2224 WorkList.push_back(SETCC.Val);
2225 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2226 SCC.getOperand(3), SETCC);
2227 }
2228 return SCC;
2229 }
Nate Begeman44728a72005-09-19 22:34:01 +00002230 return SDOperand();
2231}
2232
Chris Lattner40c62d52005-10-18 06:04:22 +00002233/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2234/// are the two values being selected between, see if we can simplify the
2235/// select.
2236///
2237bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2238 SDOperand RHS) {
2239
2240 // If this is a select from two identical things, try to pull the operation
2241 // through the select.
2242 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2243#if 0
2244 std::cerr << "SELECT: ["; LHS.Val->dump();
2245 std::cerr << "] ["; RHS.Val->dump();
2246 std::cerr << "]\n";
2247#endif
2248
2249 // If this is a load and the token chain is identical, replace the select
2250 // of two loads with a load through a select of the address to load from.
2251 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2252 // constants have been dropped into the constant pool.
2253 if ((LHS.getOpcode() == ISD::LOAD ||
2254 LHS.getOpcode() == ISD::EXTLOAD ||
2255 LHS.getOpcode() == ISD::ZEXTLOAD ||
2256 LHS.getOpcode() == ISD::SEXTLOAD) &&
2257 // Token chains must be identical.
2258 LHS.getOperand(0) == RHS.getOperand(0) &&
2259 // If this is an EXTLOAD, the VT's must match.
2260 (LHS.getOpcode() == ISD::LOAD ||
2261 LHS.getOperand(3) == RHS.getOperand(3))) {
2262 // FIXME: this conflates two src values, discarding one. This is not
2263 // the right thing to do, but nothing uses srcvalues now. When they do,
2264 // turn SrcValue into a list of locations.
2265 SDOperand Addr;
2266 if (TheSelect->getOpcode() == ISD::SELECT)
2267 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2268 TheSelect->getOperand(0), LHS.getOperand(1),
2269 RHS.getOperand(1));
2270 else
2271 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2272 TheSelect->getOperand(0),
2273 TheSelect->getOperand(1),
2274 LHS.getOperand(1), RHS.getOperand(1),
2275 TheSelect->getOperand(4));
2276
2277 SDOperand Load;
2278 if (LHS.getOpcode() == ISD::LOAD)
2279 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2280 Addr, LHS.getOperand(2));
2281 else
2282 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2283 LHS.getOperand(0), Addr, LHS.getOperand(2),
2284 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2285 // Users of the select now use the result of the load.
2286 CombineTo(TheSelect, Load);
2287
2288 // Users of the old loads now use the new load's chain. We know the
2289 // old-load value is dead now.
2290 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2291 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2292 return true;
2293 }
2294 }
2295
2296 return false;
2297}
2298
Nate Begeman44728a72005-09-19 22:34:01 +00002299SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2300 SDOperand N2, SDOperand N3,
2301 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002302
2303 MVT::ValueType VT = N2.getValueType();
2304 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2305 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2306 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2307 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2308
2309 // Determine if the condition we're dealing with is constant
2310 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2311 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2312
2313 // fold select_cc true, x, y -> x
2314 if (SCCC && SCCC->getValue())
2315 return N2;
2316 // fold select_cc false, x, y -> y
2317 if (SCCC && SCCC->getValue() == 0)
2318 return N3;
2319
2320 // Check to see if we can simplify the select into an fabs node
2321 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2322 // Allow either -0.0 or 0.0
2323 if (CFP->getValue() == 0.0) {
2324 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2325 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2326 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2327 N2 == N3.getOperand(0))
2328 return DAG.getNode(ISD::FABS, VT, N0);
2329
2330 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2331 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2332 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2333 N2.getOperand(0) == N3)
2334 return DAG.getNode(ISD::FABS, VT, N3);
2335 }
2336 }
2337
2338 // Check to see if we can perform the "gzip trick", transforming
2339 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2340 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2341 MVT::isInteger(N0.getValueType()) &&
2342 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2343 MVT::ValueType XType = N0.getValueType();
2344 MVT::ValueType AType = N2.getValueType();
2345 if (XType >= AType) {
2346 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002347 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002348 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2349 unsigned ShCtV = Log2_64(N2C->getValue());
2350 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2351 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2352 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
2353 WorkList.push_back(Shift.Val);
2354 if (XType > AType) {
2355 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2356 WorkList.push_back(Shift.Val);
2357 }
2358 return DAG.getNode(ISD::AND, AType, Shift, N2);
2359 }
2360 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2361 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2362 TLI.getShiftAmountTy()));
2363 WorkList.push_back(Shift.Val);
2364 if (XType > AType) {
2365 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
2366 WorkList.push_back(Shift.Val);
2367 }
2368 return DAG.getNode(ISD::AND, AType, Shift, N2);
2369 }
2370 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002371
2372 // fold select C, 16, 0 -> shl C, 4
2373 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2374 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2375 // Get a SetCC of the condition
2376 // FIXME: Should probably make sure that setcc is legal if we ever have a
2377 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00002378 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00002379 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00002380 if (AfterLegalize) {
2381 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002382 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00002383 } else {
2384 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002385 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00002386 }
2387 WorkList.push_back(SCC.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00002388 WorkList.push_back(Temp.Val);
2389 // shl setcc result by log2 n2c
2390 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2391 DAG.getConstant(Log2_64(N2C->getValue()),
2392 TLI.getShiftAmountTy()));
2393 }
2394
Nate Begemanf845b452005-10-08 00:29:44 +00002395 // Check to see if this is the equivalent of setcc
2396 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2397 // otherwise, go ahead with the folds.
2398 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2399 MVT::ValueType XType = N0.getValueType();
2400 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2401 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2402 if (Res.getValueType() != VT)
2403 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2404 return Res;
2405 }
2406
2407 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2408 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2409 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2410 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2411 return DAG.getNode(ISD::SRL, XType, Ctlz,
2412 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2413 TLI.getShiftAmountTy()));
2414 }
2415 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2416 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2417 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2418 N0);
2419 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2420 DAG.getConstant(~0ULL, XType));
2421 return DAG.getNode(ISD::SRL, XType,
2422 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2423 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2424 TLI.getShiftAmountTy()));
2425 }
2426 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2427 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2428 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2429 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2430 TLI.getShiftAmountTy()));
2431 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2432 }
2433 }
2434
2435 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2436 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2437 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2438 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2439 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2440 MVT::ValueType XType = N0.getValueType();
2441 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2442 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2443 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2444 TLI.getShiftAmountTy()));
2445 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
2446 WorkList.push_back(Shift.Val);
2447 WorkList.push_back(Add.Val);
2448 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2449 }
2450 }
2451 }
2452
Nate Begeman44728a72005-09-19 22:34:01 +00002453 return SDOperand();
2454}
2455
Nate Begeman452d7be2005-09-16 00:54:12 +00002456SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002457 SDOperand N1, ISD::CondCode Cond,
2458 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002459 // These setcc operations always fold.
2460 switch (Cond) {
2461 default: break;
2462 case ISD::SETFALSE:
2463 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2464 case ISD::SETTRUE:
2465 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2466 }
2467
2468 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2469 uint64_t C1 = N1C->getValue();
2470 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2471 uint64_t C0 = N0C->getValue();
2472
2473 // Sign extend the operands if required
2474 if (ISD::isSignedIntSetCC(Cond)) {
2475 C0 = N0C->getSignExtended();
2476 C1 = N1C->getSignExtended();
2477 }
2478
2479 switch (Cond) {
2480 default: assert(0 && "Unknown integer setcc!");
2481 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2482 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2483 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2484 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2485 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2486 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2487 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2488 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2489 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2490 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2491 }
2492 } else {
2493 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2494 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2495 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2496
2497 // If the comparison constant has bits in the upper part, the
2498 // zero-extended value could never match.
2499 if (C1 & (~0ULL << InSize)) {
2500 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2501 switch (Cond) {
2502 case ISD::SETUGT:
2503 case ISD::SETUGE:
2504 case ISD::SETEQ: return DAG.getConstant(0, VT);
2505 case ISD::SETULT:
2506 case ISD::SETULE:
2507 case ISD::SETNE: return DAG.getConstant(1, VT);
2508 case ISD::SETGT:
2509 case ISD::SETGE:
2510 // True if the sign bit of C1 is set.
2511 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2512 case ISD::SETLT:
2513 case ISD::SETLE:
2514 // True if the sign bit of C1 isn't set.
2515 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2516 default:
2517 break;
2518 }
2519 }
2520
2521 // Otherwise, we can perform the comparison with the low bits.
2522 switch (Cond) {
2523 case ISD::SETEQ:
2524 case ISD::SETNE:
2525 case ISD::SETUGT:
2526 case ISD::SETUGE:
2527 case ISD::SETULT:
2528 case ISD::SETULE:
2529 return DAG.getSetCC(VT, N0.getOperand(0),
2530 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2531 Cond);
2532 default:
2533 break; // todo, be more careful with signed comparisons
2534 }
2535 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2536 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2537 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2538 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2539 MVT::ValueType ExtDstTy = N0.getValueType();
2540 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2541
2542 // If the extended part has any inconsistent bits, it cannot ever
2543 // compare equal. In other words, they have to be all ones or all
2544 // zeros.
2545 uint64_t ExtBits =
2546 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2547 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2548 return DAG.getConstant(Cond == ISD::SETNE, VT);
2549
2550 SDOperand ZextOp;
2551 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2552 if (Op0Ty == ExtSrcTy) {
2553 ZextOp = N0.getOperand(0);
2554 } else {
2555 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2556 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2557 DAG.getConstant(Imm, Op0Ty));
2558 }
2559 WorkList.push_back(ZextOp.Val);
2560 // Otherwise, make this a use of a zext.
2561 return DAG.getSetCC(VT, ZextOp,
2562 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2563 ExtDstTy),
2564 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002565 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2566 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2567 (N0.getOpcode() == ISD::XOR ||
2568 (N0.getOpcode() == ISD::AND &&
2569 N0.getOperand(0).getOpcode() == ISD::XOR &&
2570 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2571 isa<ConstantSDNode>(N0.getOperand(1)) &&
2572 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2573 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2574 // only do this if the top bits are known zero.
2575 if (TLI.MaskedValueIsZero(N1,
2576 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2577 // Okay, get the un-inverted input value.
2578 SDOperand Val;
2579 if (N0.getOpcode() == ISD::XOR)
2580 Val = N0.getOperand(0);
2581 else {
2582 assert(N0.getOpcode() == ISD::AND &&
2583 N0.getOperand(0).getOpcode() == ISD::XOR);
2584 // ((X^1)&1)^1 -> X & 1
2585 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2586 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2587 }
2588 return DAG.getSetCC(VT, Val, N1,
2589 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2590 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002591 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002592
Nate Begeman452d7be2005-09-16 00:54:12 +00002593 uint64_t MinVal, MaxVal;
2594 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2595 if (ISD::isSignedIntSetCC(Cond)) {
2596 MinVal = 1ULL << (OperandBitSize-1);
2597 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2598 MaxVal = ~0ULL >> (65-OperandBitSize);
2599 else
2600 MaxVal = 0;
2601 } else {
2602 MinVal = 0;
2603 MaxVal = ~0ULL >> (64-OperandBitSize);
2604 }
2605
2606 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2607 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2608 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2609 --C1; // X >= C0 --> X > (C0-1)
2610 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2611 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2612 }
2613
2614 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2615 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2616 ++C1; // X <= C0 --> X < (C0+1)
2617 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2618 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2619 }
2620
2621 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2622 return DAG.getConstant(0, VT); // X < MIN --> false
2623
2624 // Canonicalize setgt X, Min --> setne X, Min
2625 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2626 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002627 // Canonicalize setlt X, Max --> setne X, Max
2628 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2629 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002630
2631 // If we have setult X, 1, turn it into seteq X, 0
2632 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2633 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2634 ISD::SETEQ);
2635 // If we have setugt X, Max-1, turn it into seteq X, Max
2636 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2637 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2638 ISD::SETEQ);
2639
2640 // If we have "setcc X, C0", check to see if we can shrink the immediate
2641 // by changing cc.
2642
2643 // SETUGT X, SINTMAX -> SETLT X, 0
2644 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2645 C1 == (~0ULL >> (65-OperandBitSize)))
2646 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2647 ISD::SETLT);
2648
2649 // FIXME: Implement the rest of these.
2650
2651 // Fold bit comparisons when we can.
2652 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2653 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2654 if (ConstantSDNode *AndRHS =
2655 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2656 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2657 // Perform the xform if the AND RHS is a single bit.
2658 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2659 return DAG.getNode(ISD::SRL, VT, N0,
2660 DAG.getConstant(Log2_64(AndRHS->getValue()),
2661 TLI.getShiftAmountTy()));
2662 }
2663 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2664 // (X & 8) == 8 --> (X & 8) >> 3
2665 // Perform the xform if C1 is a single bit.
2666 if ((C1 & (C1-1)) == 0) {
2667 return DAG.getNode(ISD::SRL, VT, N0,
2668 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2669 }
2670 }
2671 }
2672 }
2673 } else if (isa<ConstantSDNode>(N0.Val)) {
2674 // Ensure that the constant occurs on the RHS.
2675 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2676 }
2677
2678 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2679 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2680 double C0 = N0C->getValue(), C1 = N1C->getValue();
2681
2682 switch (Cond) {
2683 default: break; // FIXME: Implement the rest of these!
2684 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2685 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2686 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2687 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2688 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2689 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2690 }
2691 } else {
2692 // Ensure that the constant occurs on the RHS.
2693 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2694 }
2695
2696 if (N0 == N1) {
2697 // We can always fold X == Y for integer setcc's.
2698 if (MVT::isInteger(N0.getValueType()))
2699 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2700 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2701 if (UOF == 2) // FP operators that are undefined on NaNs.
2702 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2703 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2704 return DAG.getConstant(UOF, VT);
2705 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2706 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002707 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002708 if (NewCond != Cond)
2709 return DAG.getSetCC(VT, N0, N1, NewCond);
2710 }
2711
2712 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2713 MVT::isInteger(N0.getValueType())) {
2714 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2715 N0.getOpcode() == ISD::XOR) {
2716 // Simplify (X+Y) == (X+Z) --> Y == Z
2717 if (N0.getOpcode() == N1.getOpcode()) {
2718 if (N0.getOperand(0) == N1.getOperand(0))
2719 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2720 if (N0.getOperand(1) == N1.getOperand(1))
2721 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2722 if (isCommutativeBinOp(N0.getOpcode())) {
2723 // If X op Y == Y op X, try other combinations.
2724 if (N0.getOperand(0) == N1.getOperand(1))
2725 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2726 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002727 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002728 }
2729 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002730
2731 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2732 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2733 // Turn (X+C1) == C2 --> X == C2-C1
2734 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2735 return DAG.getSetCC(VT, N0.getOperand(0),
2736 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2737 N0.getValueType()), Cond);
2738 }
2739
2740 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2741 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002742 // If we know that all of the inverted bits are zero, don't bother
2743 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002744 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002745 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002746 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002747 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002748 }
2749
2750 // Turn (C1-X) == C2 --> X == C1-C2
2751 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2752 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
2753 return DAG.getSetCC(VT, N0.getOperand(1),
2754 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
2755 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00002756 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002757 }
2758 }
2759
Nate Begeman452d7be2005-09-16 00:54:12 +00002760 // Simplify (X+Z) == X --> Z == 0
2761 if (N0.getOperand(0) == N1)
2762 return DAG.getSetCC(VT, N0.getOperand(1),
2763 DAG.getConstant(0, N0.getValueType()), Cond);
2764 if (N0.getOperand(1) == N1) {
2765 if (isCommutativeBinOp(N0.getOpcode()))
2766 return DAG.getSetCC(VT, N0.getOperand(0),
2767 DAG.getConstant(0, N0.getValueType()), Cond);
2768 else {
2769 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2770 // (Z-X) == X --> Z == X<<1
2771 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2772 N1,
2773 DAG.getConstant(1,TLI.getShiftAmountTy()));
2774 WorkList.push_back(SH.Val);
2775 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2776 }
2777 }
2778 }
2779
2780 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2781 N1.getOpcode() == ISD::XOR) {
2782 // Simplify X == (X+Z) --> Z == 0
2783 if (N1.getOperand(0) == N0) {
2784 return DAG.getSetCC(VT, N1.getOperand(1),
2785 DAG.getConstant(0, N1.getValueType()), Cond);
2786 } else if (N1.getOperand(1) == N0) {
2787 if (isCommutativeBinOp(N1.getOpcode())) {
2788 return DAG.getSetCC(VT, N1.getOperand(0),
2789 DAG.getConstant(0, N1.getValueType()), Cond);
2790 } else {
2791 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2792 // X == (Z-X) --> X<<1 == Z
2793 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2794 DAG.getConstant(1,TLI.getShiftAmountTy()));
2795 WorkList.push_back(SH.Val);
2796 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2797 }
2798 }
2799 }
2800 }
2801
2802 // Fold away ALL boolean setcc's.
2803 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002804 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002805 switch (Cond) {
2806 default: assert(0 && "Unknown integer setcc!");
2807 case ISD::SETEQ: // X == Y -> (X^Y)^1
2808 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2809 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2810 WorkList.push_back(Temp.Val);
2811 break;
2812 case ISD::SETNE: // X != Y --> (X^Y)
2813 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2814 break;
2815 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2816 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2817 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2818 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2819 WorkList.push_back(Temp.Val);
2820 break;
2821 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2822 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2823 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2824 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2825 WorkList.push_back(Temp.Val);
2826 break;
2827 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2828 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2829 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2830 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2831 WorkList.push_back(Temp.Val);
2832 break;
2833 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2834 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2835 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2836 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2837 break;
2838 }
2839 if (VT != MVT::i1) {
2840 WorkList.push_back(N0.Val);
2841 // FIXME: If running after legalize, we probably can't do this.
2842 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2843 }
2844 return N0;
2845 }
2846
2847 // Could not fold it.
2848 return SDOperand();
2849}
2850
Nate Begeman69575232005-10-20 02:15:44 +00002851/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2852/// return a DAG expression to select that will generate the same value by
2853/// multiplying by a magic number. See:
2854/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2855SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2856 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002857
2858 // Check to see if we can do this.
2859 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2860 return SDOperand(); // BuildSDIV only operates on i32 or i64
2861 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2862 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002863
Nate Begemanc6a454e2005-10-20 17:45:03 +00002864 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002865 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2866
2867 // Multiply the numerator (operand 0) by the magic value
2868 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2869 DAG.getConstant(magics.m, VT));
2870 // If d > 0 and m < 0, add the numerator
2871 if (d > 0 && magics.m < 0) {
2872 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
2873 WorkList.push_back(Q.Val);
2874 }
2875 // If d < 0 and m > 0, subtract the numerator.
2876 if (d < 0 && magics.m > 0) {
2877 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
2878 WorkList.push_back(Q.Val);
2879 }
2880 // Shift right algebraic if shift value is nonzero
2881 if (magics.s > 0) {
2882 Q = DAG.getNode(ISD::SRA, VT, Q,
2883 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2884 WorkList.push_back(Q.Val);
2885 }
2886 // Extract the sign bit and add it to the quotient
2887 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002888 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2889 TLI.getShiftAmountTy()));
Nate Begeman69575232005-10-20 02:15:44 +00002890 WorkList.push_back(T.Val);
2891 return DAG.getNode(ISD::ADD, VT, Q, T);
2892}
2893
2894/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2895/// return a DAG expression to select that will generate the same value by
2896/// multiplying by a magic number. See:
2897/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2898SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2899 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002900
2901 // Check to see if we can do this.
2902 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2903 return SDOperand(); // BuildUDIV only operates on i32 or i64
2904 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2905 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002906
2907 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2908 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2909
2910 // Multiply the numerator (operand 0) by the magic value
2911 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2912 DAG.getConstant(magics.m, VT));
2913 WorkList.push_back(Q.Val);
2914
2915 if (magics.a == 0) {
2916 return DAG.getNode(ISD::SRL, VT, Q,
2917 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2918 } else {
2919 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
2920 WorkList.push_back(NPQ.Val);
2921 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2922 DAG.getConstant(1, TLI.getShiftAmountTy()));
2923 WorkList.push_back(NPQ.Val);
2924 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
2925 WorkList.push_back(NPQ.Val);
2926 return DAG.getNode(ISD::SRL, VT, NPQ,
2927 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
2928 }
2929}
2930
Nate Begeman1d4d4142005-09-01 00:19:25 +00002931// SelectionDAG::Combine - This is the entry point for the file.
2932//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002933void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002934 /// run - This is the main entry point to this class.
2935 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002936 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002937}