blob: 57c72ac125d898e5e141faefe78219232c547fe5 [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.
Chris Lattner5750df92006-03-01 04:03:14 +000065 ///
Nate Begeman1d4d4142005-09-01 00:19:25 +000066 void removeFromWorkList(SDNode *N) {
67 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
68 WorkList.end());
69 }
70
Chris Lattner5750df92006-03-01 04:03:14 +000071 void AddToWorkList(SDNode *N) {
72 WorkList.push_back(N);
73 }
74
Chris Lattner01a22022005-10-10 22:04:48 +000075 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000076 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000077 DEBUG(std::cerr << "\nReplacing "; N->dump();
78 std::cerr << "\nWith: "; To[0].Val->dump();
79 std::cerr << " and " << To.size()-1 << " other values\n");
80 std::vector<SDNode*> NowDead;
81 DAG.ReplaceAllUsesWith(N, To, &NowDead);
82
83 // Push the new nodes and any users onto the worklist
84 for (unsigned i = 0, e = To.size(); i != e; ++i) {
85 WorkList.push_back(To[i].Val);
86 AddUsersToWorkList(To[i].Val);
87 }
88
89 // Nodes can end up on the worklist more than once. Make sure we do
90 // not process a node that has been replaced.
91 removeFromWorkList(N);
92 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
93 removeFromWorkList(NowDead[i]);
94
95 // Finally, since the node is now dead, remove it from the graph.
96 DAG.DeleteNode(N);
97 return SDOperand(N, 0);
98 }
Nate Begeman368e18d2006-02-16 21:11:51 +000099
Chris Lattner012f2412006-02-17 21:58:01 +0000100 /// SimplifyDemandedBits - Check the specified integer node value to see if
101 /// it can be simplified or if things is uses can be simplified by bit
102 /// propagation. If so, return true.
103 bool SimplifyDemandedBits(SDOperand Op) {
Nate Begeman368e18d2006-02-16 21:11:51 +0000104 TargetLowering::TargetLoweringOpt TLO(DAG);
105 uint64_t KnownZero, KnownOne;
Chris Lattner012f2412006-02-17 21:58:01 +0000106 uint64_t Demanded = MVT::getIntVTBitMask(Op.getValueType());
107 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
108 return false;
109
110 // Revisit the node.
111 WorkList.push_back(Op.Val);
112
113 // Replace the old value with the new one.
114 ++NodesCombined;
115 DEBUG(std::cerr << "\nReplacing "; TLO.Old.Val->dump();
116 std::cerr << "\nWith: "; TLO.New.Val->dump());
117
118 std::vector<SDNode*> NowDead;
119 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, NowDead);
120
Chris Lattner7d20d392006-02-20 06:51:04 +0000121 // Push the new node and any (possibly new) users onto the worklist.
Chris Lattner012f2412006-02-17 21:58:01 +0000122 WorkList.push_back(TLO.New.Val);
123 AddUsersToWorkList(TLO.New.Val);
124
125 // Nodes can end up on the worklist more than once. Make sure we do
126 // not process a node that has been replaced.
Chris Lattner012f2412006-02-17 21:58:01 +0000127 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
128 removeFromWorkList(NowDead[i]);
129
Chris Lattner7d20d392006-02-20 06:51:04 +0000130 // Finally, if the node is now dead, remove it from the graph. The node
131 // may not be dead if the replacement process recursively simplified to
132 // something else needing this node.
133 if (TLO.Old.Val->use_empty()) {
134 removeFromWorkList(TLO.Old.Val);
135 DAG.DeleteNode(TLO.Old.Val);
136 }
Chris Lattner012f2412006-02-17 21:58:01 +0000137 return true;
Nate Begeman368e18d2006-02-16 21:11:51 +0000138 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000139
140 SDOperand CombineTo(SDNode *N, SDOperand Res) {
141 std::vector<SDOperand> To;
142 To.push_back(Res);
143 return CombineTo(N, To);
144 }
Chris Lattner01a22022005-10-10 22:04:48 +0000145
146 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
147 std::vector<SDOperand> To;
148 To.push_back(Res0);
149 To.push_back(Res1);
150 return CombineTo(N, To);
151 }
152
Nate Begeman1d4d4142005-09-01 00:19:25 +0000153 /// visit - call the node-specific routine that knows how to fold each
154 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000155 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000156
157 // Visitation implementation - Implement dag node combining for different
158 // node types. The semantics are as follows:
159 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000160 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000161 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000162 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000163 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000164 SDOperand visitTokenFactor(SDNode *N);
165 SDOperand visitADD(SDNode *N);
166 SDOperand visitSUB(SDNode *N);
167 SDOperand visitMUL(SDNode *N);
168 SDOperand visitSDIV(SDNode *N);
169 SDOperand visitUDIV(SDNode *N);
170 SDOperand visitSREM(SDNode *N);
171 SDOperand visitUREM(SDNode *N);
172 SDOperand visitMULHU(SDNode *N);
173 SDOperand visitMULHS(SDNode *N);
174 SDOperand visitAND(SDNode *N);
175 SDOperand visitOR(SDNode *N);
176 SDOperand visitXOR(SDNode *N);
177 SDOperand visitSHL(SDNode *N);
178 SDOperand visitSRA(SDNode *N);
179 SDOperand visitSRL(SDNode *N);
180 SDOperand visitCTLZ(SDNode *N);
181 SDOperand visitCTTZ(SDNode *N);
182 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000183 SDOperand visitSELECT(SDNode *N);
184 SDOperand visitSELECT_CC(SDNode *N);
185 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000186 SDOperand visitSIGN_EXTEND(SDNode *N);
187 SDOperand visitZERO_EXTEND(SDNode *N);
188 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
189 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000190 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000191 SDOperand visitFADD(SDNode *N);
192 SDOperand visitFSUB(SDNode *N);
193 SDOperand visitFMUL(SDNode *N);
194 SDOperand visitFDIV(SDNode *N);
195 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000196 SDOperand visitSINT_TO_FP(SDNode *N);
197 SDOperand visitUINT_TO_FP(SDNode *N);
198 SDOperand visitFP_TO_SINT(SDNode *N);
199 SDOperand visitFP_TO_UINT(SDNode *N);
200 SDOperand visitFP_ROUND(SDNode *N);
201 SDOperand visitFP_ROUND_INREG(SDNode *N);
202 SDOperand visitFP_EXTEND(SDNode *N);
203 SDOperand visitFNEG(SDNode *N);
204 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000205 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000206 SDOperand visitBRCONDTWOWAY(SDNode *N);
207 SDOperand visitBR_CC(SDNode *N);
208 SDOperand visitBRTWOWAY_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000209 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000210 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000211
Nate Begemancd4d58c2006-02-03 06:46:56 +0000212 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
213
Chris Lattner40c62d52005-10-18 06:04:22 +0000214 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Nate Begeman44728a72005-09-19 22:34:01 +0000215 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
216 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
217 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000218 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000219 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman69575232005-10-20 02:15:44 +0000220
221 SDOperand BuildSDIV(SDNode *N);
222 SDOperand BuildUDIV(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000223public:
224 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000225 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000226
227 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000228 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000229 };
230}
231
Nate Begeman69575232005-10-20 02:15:44 +0000232struct ms {
233 int64_t m; // magic number
234 int64_t s; // shift amount
235};
236
237struct mu {
238 uint64_t m; // magic number
239 int64_t a; // add indicator
240 int64_t s; // shift amount
241};
242
243/// magic - calculate the magic numbers required to codegen an integer sdiv as
244/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
245/// or -1.
246static ms magic32(int32_t d) {
247 int32_t p;
248 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
249 const uint32_t two31 = 0x80000000U;
250 struct ms mag;
251
252 ad = abs(d);
253 t = two31 + ((uint32_t)d >> 31);
254 anc = t - 1 - t%ad; // absolute value of nc
255 p = 31; // initialize p
256 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
257 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
258 q2 = two31/ad; // initialize q2 = 2p/abs(d)
259 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
260 do {
261 p = p + 1;
262 q1 = 2*q1; // update q1 = 2p/abs(nc)
263 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
264 if (r1 >= anc) { // must be unsigned comparison
265 q1 = q1 + 1;
266 r1 = r1 - anc;
267 }
268 q2 = 2*q2; // update q2 = 2p/abs(d)
269 r2 = 2*r2; // update r2 = rem(2p/abs(d))
270 if (r2 >= ad) { // must be unsigned comparison
271 q2 = q2 + 1;
272 r2 = r2 - ad;
273 }
274 delta = ad - r2;
275 } while (q1 < delta || (q1 == delta && r1 == 0));
276
277 mag.m = (int32_t)(q2 + 1); // make sure to sign extend
278 if (d < 0) mag.m = -mag.m; // resulting magic number
279 mag.s = p - 32; // resulting shift
280 return mag;
281}
282
283/// magicu - calculate the magic numbers required to codegen an integer udiv as
284/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
285static mu magicu32(uint32_t d) {
286 int32_t p;
287 uint32_t nc, delta, q1, r1, q2, r2;
288 struct mu magu;
289 magu.a = 0; // initialize "add" indicator
290 nc = - 1 - (-d)%d;
291 p = 31; // initialize p
292 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
293 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
294 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
295 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
296 do {
297 p = p + 1;
298 if (r1 >= nc - r1 ) {
299 q1 = 2*q1 + 1; // update q1
300 r1 = 2*r1 - nc; // update r1
301 }
302 else {
303 q1 = 2*q1; // update q1
304 r1 = 2*r1; // update r1
305 }
306 if (r2 + 1 >= d - r2) {
307 if (q2 >= 0x7FFFFFFF) magu.a = 1;
308 q2 = 2*q2 + 1; // update q2
309 r2 = 2*r2 + 1 - d; // update r2
310 }
311 else {
312 if (q2 >= 0x80000000) magu.a = 1;
313 q2 = 2*q2; // update q2
314 r2 = 2*r2 + 1; // update r2
315 }
316 delta = d - 1 - r2;
317 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
318 magu.m = q2 + 1; // resulting magic number
319 magu.s = p - 32; // resulting shift
320 return magu;
321}
322
323/// magic - calculate the magic numbers required to codegen an integer sdiv as
324/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
325/// or -1.
326static ms magic64(int64_t d) {
327 int64_t p;
328 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
329 const uint64_t two63 = 9223372036854775808ULL; // 2^63
330 struct ms mag;
331
Chris Lattnerf75f2a02005-10-20 17:01:00 +0000332 ad = d >= 0 ? d : -d;
Nate Begeman69575232005-10-20 02:15:44 +0000333 t = two63 + ((uint64_t)d >> 63);
334 anc = t - 1 - t%ad; // absolute value of nc
335 p = 63; // initialize p
336 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
337 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
338 q2 = two63/ad; // initialize q2 = 2p/abs(d)
339 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
340 do {
341 p = p + 1;
342 q1 = 2*q1; // update q1 = 2p/abs(nc)
343 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
344 if (r1 >= anc) { // must be unsigned comparison
345 q1 = q1 + 1;
346 r1 = r1 - anc;
347 }
348 q2 = 2*q2; // update q2 = 2p/abs(d)
349 r2 = 2*r2; // update r2 = rem(2p/abs(d))
350 if (r2 >= ad) { // must be unsigned comparison
351 q2 = q2 + 1;
352 r2 = r2 - ad;
353 }
354 delta = ad - r2;
355 } while (q1 < delta || (q1 == delta && r1 == 0));
356
357 mag.m = q2 + 1;
358 if (d < 0) mag.m = -mag.m; // resulting magic number
359 mag.s = p - 64; // resulting shift
360 return mag;
361}
362
363/// magicu - calculate the magic numbers required to codegen an integer udiv as
364/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
365static mu magicu64(uint64_t d)
366{
367 int64_t p;
368 uint64_t nc, delta, q1, r1, q2, r2;
369 struct mu magu;
370 magu.a = 0; // initialize "add" indicator
371 nc = - 1 - (-d)%d;
372 p = 63; // initialize p
373 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
374 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
375 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
376 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
377 do {
378 p = p + 1;
379 if (r1 >= nc - r1 ) {
380 q1 = 2*q1 + 1; // update q1
381 r1 = 2*r1 - nc; // update r1
382 }
383 else {
384 q1 = 2*q1; // update q1
385 r1 = 2*r1; // update r1
386 }
387 if (r2 + 1 >= d - r2) {
388 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
389 q2 = 2*q2 + 1; // update q2
390 r2 = 2*r2 + 1 - d; // update r2
391 }
392 else {
393 if (q2 >= 0x8000000000000000ull) magu.a = 1;
394 q2 = 2*q2; // update q2
395 r2 = 2*r2 + 1; // update r2
396 }
397 delta = d - 1 - r2;
398 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
399 magu.m = q2 + 1; // resulting magic number
400 magu.s = p - 64; // resulting shift
401 return magu;
402}
403
Nate Begeman4ebd8052005-09-01 23:24:04 +0000404// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
405// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000406// Also, set the incoming LHS, RHS, and CC references to the appropriate
407// nodes based on the type of node we are checking. This simplifies life a
408// bit for the callers.
409static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
410 SDOperand &CC) {
411 if (N.getOpcode() == ISD::SETCC) {
412 LHS = N.getOperand(0);
413 RHS = N.getOperand(1);
414 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000415 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000416 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000417 if (N.getOpcode() == ISD::SELECT_CC &&
418 N.getOperand(2).getOpcode() == ISD::Constant &&
419 N.getOperand(3).getOpcode() == ISD::Constant &&
420 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000421 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
422 LHS = N.getOperand(0);
423 RHS = N.getOperand(1);
424 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000425 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000426 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000427 return false;
428}
429
Nate Begeman99801192005-09-07 23:25:52 +0000430// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
431// one use. If this is true, it allows the users to invert the operation for
432// free when it is profitable to do so.
433static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000434 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000435 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000436 return true;
437 return false;
438}
439
Nate Begeman452d7be2005-09-16 00:54:12 +0000440// FIXME: This should probably go in the ISD class rather than being duplicated
441// in several files.
442static bool isCommutativeBinOp(unsigned Opcode) {
443 switch (Opcode) {
444 case ISD::ADD:
445 case ISD::MUL:
446 case ISD::AND:
447 case ISD::OR:
448 case ISD::XOR: return true;
449 default: return false; // FIXME: Need commutative info for user ops!
450 }
451}
452
Nate Begemancd4d58c2006-02-03 06:46:56 +0000453SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
454 MVT::ValueType VT = N0.getValueType();
455 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
456 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
457 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
458 if (isa<ConstantSDNode>(N1)) {
459 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000460 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000461 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
462 } else if (N0.hasOneUse()) {
463 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000464 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000465 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
466 }
467 }
468 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
469 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
470 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
471 if (isa<ConstantSDNode>(N0)) {
472 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000473 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000474 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
475 } else if (N1.hasOneUse()) {
476 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000477 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000478 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
479 }
480 }
481 return SDOperand();
482}
483
Nate Begeman4ebd8052005-09-01 23:24:04 +0000484void DAGCombiner::Run(bool RunningAfterLegalize) {
485 // set the instance variable, so that the various visit routines may use it.
486 AfterLegalize = RunningAfterLegalize;
487
Nate Begeman646d7e22005-09-02 21:18:40 +0000488 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000489 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
490 E = DAG.allnodes_end(); I != E; ++I)
491 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000492
Chris Lattner95038592005-10-05 06:35:28 +0000493 // Create a dummy node (which is not added to allnodes), that adds a reference
494 // to the root node, preventing it from being deleted, and tracking any
495 // changes of the root.
496 HandleSDNode Dummy(DAG.getRoot());
497
Nate Begeman1d4d4142005-09-01 00:19:25 +0000498 // while the worklist isn't empty, inspect the node on the end of it and
499 // try and combine it.
500 while (!WorkList.empty()) {
501 SDNode *N = WorkList.back();
502 WorkList.pop_back();
503
504 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000505 // N is deleted from the DAG, since they too may now be dead or may have a
506 // reduced number of uses, allowing other xforms.
507 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000508 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
509 WorkList.push_back(N->getOperand(i).Val);
510
Nate Begeman1d4d4142005-09-01 00:19:25 +0000511 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000512 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000513 continue;
514 }
515
Nate Begeman83e75ec2005-09-06 04:43:02 +0000516 SDOperand RV = visit(N);
517 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000518 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000519 // If we get back the same node we passed in, rather than a new node or
520 // zero, we know that the node must have defined multiple values and
521 // CombineTo was used. Since CombineTo takes care of the worklist
522 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000523 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000524 DEBUG(std::cerr << "\nReplacing "; N->dump();
525 std::cerr << "\nWith: "; RV.Val->dump();
526 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000527 std::vector<SDNode*> NowDead;
528 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000529
530 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000531 WorkList.push_back(RV.Val);
532 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000533
534 // Nodes can end up on the worklist more than once. Make sure we do
535 // not process a node that has been replaced.
536 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000537 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
538 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000539
540 // Finally, since the node is now dead, remove it from the graph.
541 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000542 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543 }
544 }
Chris Lattner95038592005-10-05 06:35:28 +0000545
546 // If the root changed (e.g. it was a dead load, update the root).
547 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000548}
549
Nate Begeman83e75ec2005-09-06 04:43:02 +0000550SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000551 switch(N->getOpcode()) {
552 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000553 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000554 case ISD::ADD: return visitADD(N);
555 case ISD::SUB: return visitSUB(N);
556 case ISD::MUL: return visitMUL(N);
557 case ISD::SDIV: return visitSDIV(N);
558 case ISD::UDIV: return visitUDIV(N);
559 case ISD::SREM: return visitSREM(N);
560 case ISD::UREM: return visitUREM(N);
561 case ISD::MULHU: return visitMULHU(N);
562 case ISD::MULHS: return visitMULHS(N);
563 case ISD::AND: return visitAND(N);
564 case ISD::OR: return visitOR(N);
565 case ISD::XOR: return visitXOR(N);
566 case ISD::SHL: return visitSHL(N);
567 case ISD::SRA: return visitSRA(N);
568 case ISD::SRL: return visitSRL(N);
569 case ISD::CTLZ: return visitCTLZ(N);
570 case ISD::CTTZ: return visitCTTZ(N);
571 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000572 case ISD::SELECT: return visitSELECT(N);
573 case ISD::SELECT_CC: return visitSELECT_CC(N);
574 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000575 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
576 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
577 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
578 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000579 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000580 case ISD::FADD: return visitFADD(N);
581 case ISD::FSUB: return visitFSUB(N);
582 case ISD::FMUL: return visitFMUL(N);
583 case ISD::FDIV: return visitFDIV(N);
584 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000585 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
586 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
587 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
588 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
589 case ISD::FP_ROUND: return visitFP_ROUND(N);
590 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
591 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
592 case ISD::FNEG: return visitFNEG(N);
593 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000594 case ISD::BRCOND: return visitBRCOND(N);
595 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
596 case ISD::BR_CC: return visitBR_CC(N);
597 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000598 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000599 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000600 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000601 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000602}
603
Nate Begeman83e75ec2005-09-06 04:43:02 +0000604SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begemanded49632005-10-13 03:11:28 +0000605 std::vector<SDOperand> Ops;
606 bool Changed = false;
607
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 // If the token factor has two operands and one is the entry token, replace
609 // the token factor with the other operand.
610 if (N->getNumOperands() == 2) {
611 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000612 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000613 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000614 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 }
Chris Lattner24edbb72005-10-13 22:10:05 +0000616
Nate Begemanded49632005-10-13 03:11:28 +0000617 // fold (tokenfactor (tokenfactor)) -> tokenfactor
618 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
619 SDOperand Op = N->getOperand(i);
620 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
621 Changed = true;
622 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
623 Ops.push_back(Op.getOperand(j));
624 } else {
625 Ops.push_back(Op);
626 }
627 }
628 if (Changed)
629 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000630 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000631}
632
Nate Begeman83e75ec2005-09-06 04:43:02 +0000633SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000634 SDOperand N0 = N->getOperand(0);
635 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000636 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
637 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000638 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000639
640 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000641 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000642 return DAG.getNode(ISD::ADD, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000643 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000644 if (N0C && !N1C)
645 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000646 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000647 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000648 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000649 // fold ((c1-A)+c2) -> (c1+c2)-A
650 if (N1C && N0.getOpcode() == ISD::SUB)
651 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
652 return DAG.getNode(ISD::SUB, VT,
653 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
654 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000655 // reassociate add
656 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
657 if (RADD.Val != 0)
658 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000659 // fold ((0-A) + B) -> B-A
660 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
661 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000662 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000663 // fold (A + (0-B)) -> A-B
664 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
665 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000666 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000667 // fold (A+(B-A)) -> B
668 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000669 return N1.getOperand(0);
Nate Begemanb0d04a72006-02-18 02:40:58 +0000670 //
Evan Cheng860771d2006-03-01 01:09:54 +0000671 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemanb0d04a72006-02-18 02:40:58 +0000672 return SDOperand();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000673 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000674}
675
Nate Begeman83e75ec2005-09-06 04:43:02 +0000676SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000677 SDOperand N0 = N->getOperand(0);
678 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000679 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
680 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000681 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000682
Chris Lattner854077d2005-10-17 01:07:11 +0000683 // fold (sub x, x) -> 0
684 if (N0 == N1)
685 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000686 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000687 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000688 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +0000689 // fold (sub x, c) -> (add x, -c)
690 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000691 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000692 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000693 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000694 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000695 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000696 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000697 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000698 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000699}
700
Nate Begeman83e75ec2005-09-06 04:43:02 +0000701SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000702 SDOperand N0 = N->getOperand(0);
703 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000704 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
705 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000706 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000707
708 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000709 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000710 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000711 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000712 if (N0C && !N1C)
713 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000714 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000715 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000716 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000717 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000718 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +0000719 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000720 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000721 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +0000722 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000723 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000724 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +0000725 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
726 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
727 // FIXME: If the input is something that is easily negated (e.g. a
728 // single-use add), we should put the negate there.
729 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
730 DAG.getNode(ISD::SHL, VT, N0,
731 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
732 TLI.getShiftAmountTy())));
733 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000734
735 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
736 if (N1C && N0.getOpcode() == ISD::SHL &&
737 isa<ConstantSDNode>(N0.getOperand(1))) {
738 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +0000739 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +0000740 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
741 }
742
743 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
744 // use.
745 {
746 SDOperand Sh(0,0), Y(0,0);
747 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
748 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
749 N0.Val->hasOneUse()) {
750 Sh = N0; Y = N1;
751 } else if (N1.getOpcode() == ISD::SHL &&
752 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
753 Sh = N1; Y = N0;
754 }
755 if (Sh.Val) {
756 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
757 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
758 }
759 }
760
761
Nate Begemancd4d58c2006-02-03 06:46:56 +0000762 // reassociate mul
763 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
764 if (RMUL.Val != 0)
765 return RMUL;
Nate Begeman83e75ec2005-09-06 04:43:02 +0000766 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000767}
768
Nate Begeman83e75ec2005-09-06 04:43:02 +0000769SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000770 SDOperand N0 = N->getOperand(0);
771 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000772 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
773 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000774 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000775
776 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000777 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000778 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000779 // fold (sdiv X, 1) -> X
780 if (N1C && N1C->getSignExtended() == 1LL)
781 return N0;
782 // fold (sdiv X, -1) -> 0-X
783 if (N1C && N1C->isAllOnesValue())
784 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000785 // If we know the sign bits of both operands are zero, strength reduce to a
786 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
787 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000788 if (TLI.MaskedValueIsZero(N1, SignBit) &&
789 TLI.MaskedValueIsZero(N0, SignBit))
Chris Lattner094c8fc2005-10-07 06:10:46 +0000790 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begemancd6a6ed2006-02-17 07:26:20 +0000791 // fold (sdiv X, pow2) -> simple ops after legalize
Nate Begemanfb7217b2006-02-17 19:54:08 +0000792 if (N1C && N1C->getValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +0000793 (isPowerOf2_64(N1C->getSignExtended()) ||
794 isPowerOf2_64(-N1C->getSignExtended()))) {
795 // If dividing by powers of two is cheap, then don't perform the following
796 // fold.
797 if (TLI.isPow2DivCheap())
798 return SDOperand();
799 int64_t pow2 = N1C->getSignExtended();
800 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +0000801 unsigned lg2 = Log2_64(abs2);
802 // Splat the sign bit into the register
803 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000804 DAG.getConstant(MVT::getSizeInBits(VT)-1,
805 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +0000806 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +0000807 // Add (N0 < 0) ? abs2 - 1 : 0;
808 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
809 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +0000810 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +0000811 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +0000812 AddToWorkList(SRL.Val);
813 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +0000814 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
815 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +0000816 // If we're dividing by a positive value, we're done. Otherwise, we must
817 // negate the result.
818 if (pow2 > 0)
819 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +0000820 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +0000821 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
822 }
Nate Begeman69575232005-10-20 02:15:44 +0000823 // if integer divide is expensive and we satisfy the requirements, emit an
824 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +0000825 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +0000826 !TLI.isIntDivCheap()) {
827 SDOperand Op = BuildSDIV(N);
828 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +0000829 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000830 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000831}
832
Nate Begeman83e75ec2005-09-06 04:43:02 +0000833SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000834 SDOperand N0 = N->getOperand(0);
835 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000836 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
837 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +0000838 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000839
840 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000841 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000842 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000843 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000844 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000845 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000846 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000847 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000848 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
849 if (N1.getOpcode() == ISD::SHL) {
850 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
851 if (isPowerOf2_64(SHC->getValue())) {
852 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +0000853 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
854 DAG.getConstant(Log2_64(SHC->getValue()),
855 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +0000856 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000857 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +0000858 }
859 }
860 }
Nate Begeman69575232005-10-20 02:15:44 +0000861 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +0000862 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
863 SDOperand Op = BuildUDIV(N);
864 if (Op.Val) return Op;
865 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000866 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867}
868
Nate Begeman83e75ec2005-09-06 04:43:02 +0000869SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000870 SDOperand N0 = N->getOperand(0);
871 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000872 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
873 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000874 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000875
876 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000877 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000878 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000879 // If we know the sign bits of both operands are zero, strength reduce to a
880 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
881 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000882 if (TLI.MaskedValueIsZero(N1, SignBit) &&
883 TLI.MaskedValueIsZero(N0, SignBit))
Nate Begemana148d982006-01-18 22:35:16 +0000884 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000885 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000886}
887
Nate Begeman83e75ec2005-09-06 04:43:02 +0000888SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000889 SDOperand N0 = N->getOperand(0);
890 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000891 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
892 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +0000893 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000894
895 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000896 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +0000897 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000898 // fold (urem x, pow2) -> (and x, pow2-1)
899 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +0000900 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +0000901 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
902 if (N1.getOpcode() == ISD::SHL) {
903 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
904 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +0000905 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +0000906 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +0000907 return DAG.getNode(ISD::AND, VT, N0, Add);
908 }
909 }
910 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000911 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000912}
913
Nate Begeman83e75ec2005-09-06 04:43:02 +0000914SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000915 SDOperand N0 = N->getOperand(0);
916 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000917 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918
919 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000920 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000921 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000923 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000924 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
925 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000926 TLI.getShiftAmountTy()));
927 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000928}
929
Nate Begeman83e75ec2005-09-06 04:43:02 +0000930SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000931 SDOperand N0 = N->getOperand(0);
932 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000933 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934
935 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000936 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000937 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000940 return DAG.getConstant(0, N0.getValueType());
941 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000942}
943
Nate Begeman83e75ec2005-09-06 04:43:02 +0000944SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000945 SDOperand N0 = N->getOperand(0);
946 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +0000947 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000948 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
949 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000950 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000951 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000952
953 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000954 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +0000955 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +0000956 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000957 if (N0C && !N1C)
958 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000959 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000960 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000961 return N0;
962 // if (and x, c) is known to be zero, return 0
Nate Begeman368e18d2006-02-16 21:11:51 +0000963 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000964 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000965 // reassociate and
966 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
967 if (RAND.Val != 0)
968 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000969 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +0000970 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000971 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000972 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000973 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +0000974 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
975 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
976 unsigned InBits = MVT::getSizeInBits(N0.getOperand(0).getValueType());
977 if (TLI.MaskedValueIsZero(N0.getOperand(0),
978 ~N1C->getValue() & ((1ULL << InBits)-1))) {
979 // We actually want to replace all uses of the any_extend with the
980 // zero_extend, to avoid duplicating things. This will later cause this
981 // AND to be folded.
982 CombineTo(N0.Val, DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
983 N0.getOperand(0)));
984 return SDOperand();
985 }
986 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000987 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
988 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
989 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
990 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
991
992 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
993 MVT::isInteger(LL.getValueType())) {
994 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
995 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
996 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +0000997 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000998 return DAG.getSetCC(VT, ORNode, LR, Op1);
999 }
1000 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1001 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1002 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001003 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001004 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1005 }
1006 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1007 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1008 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001009 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001010 return DAG.getSetCC(VT, ORNode, LR, Op1);
1011 }
1012 }
1013 // canonicalize equivalent to ll == rl
1014 if (LL == RR && LR == RL) {
1015 Op1 = ISD::getSetCCSwappedOperands(Op1);
1016 std::swap(RL, RR);
1017 }
1018 if (LL == RL && LR == RR) {
1019 bool isInteger = MVT::isInteger(LL.getValueType());
1020 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1021 if (Result != ISD::SETCC_INVALID)
1022 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1023 }
1024 }
1025 // fold (and (zext x), (zext y)) -> (zext (and x, y))
1026 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1027 N1.getOpcode() == ISD::ZERO_EXTEND &&
1028 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1029 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1030 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001031 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001032 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
1033 }
Nate Begeman61af66e2006-01-28 01:06:30 +00001034 // fold (and (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (and x, y))
Nate Begeman452d7be2005-09-16 00:54:12 +00001035 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
Nate Begeman61af66e2006-01-28 01:06:30 +00001036 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1037 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
Nate Begeman452d7be2005-09-16 00:54:12 +00001038 N0.getOperand(1) == N1.getOperand(1)) {
1039 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
1040 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001041 AddToWorkList(ANDNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001042 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
1043 }
Nate Begemande996292006-02-03 22:24:05 +00001044 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1045 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner012f2412006-02-17 21:58:01 +00001046 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001047 return SDOperand();
Nate Begemanded49632005-10-13 03:11:28 +00001048 // fold (zext_inreg (extload x)) -> (zextload x)
Nate Begeman5054f162005-10-14 01:12:21 +00001049 if (N0.getOpcode() == ISD::EXTLOAD) {
Nate Begemanded49632005-10-13 03:11:28 +00001050 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001051 // If we zero all the possible extended bits, then we can turn this into
1052 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001053 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Chris Lattner67a44cd2005-10-13 18:16:34 +00001054 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001055 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1056 N0.getOperand(1), N0.getOperand(2),
1057 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001058 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001059 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001060 return SDOperand();
1061 }
1062 }
1063 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001064 if (N0.getOpcode() == ISD::SEXTLOAD && N0.hasOneUse()) {
Nate Begemanded49632005-10-13 03:11:28 +00001065 MVT::ValueType EVT = cast<VTSDNode>(N0.getOperand(3))->getVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001066 // If we zero all the possible extended bits, then we can turn this into
1067 // a zextload if we are running before legalize or the operation is legal.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001068 if (TLI.MaskedValueIsZero(N1, ~0ULL << MVT::getSizeInBits(EVT)) &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001069 (!AfterLegalize || TLI.isOperationLegal(ISD::ZEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001070 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1071 N0.getOperand(1), N0.getOperand(2),
1072 EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001073 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001074 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001075 return SDOperand();
1076 }
1077 }
Chris Lattner15045b62006-02-28 06:35:35 +00001078
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001079 // fold (and (load x), 255) -> (zextload x, i8)
1080 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1081 if (N1C &&
1082 (N0.getOpcode() == ISD::LOAD || N0.getOpcode() == ISD::EXTLOAD ||
1083 N0.getOpcode() == ISD::ZEXTLOAD) &&
1084 N0.hasOneUse()) {
1085 MVT::ValueType EVT, LoadedVT;
Chris Lattner15045b62006-02-28 06:35:35 +00001086 if (N1C->getValue() == 255)
1087 EVT = MVT::i8;
1088 else if (N1C->getValue() == 65535)
1089 EVT = MVT::i16;
1090 else if (N1C->getValue() == ~0U)
1091 EVT = MVT::i32;
1092 else
1093 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001094
1095 LoadedVT = N0.getOpcode() == ISD::LOAD ? VT :
1096 cast<VTSDNode>(N0.getOperand(3))->getVT();
1097 if (EVT != MVT::Other && LoadedVT > EVT) {
Chris Lattner15045b62006-02-28 06:35:35 +00001098 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1099 // For big endian targets, we need to add an offset to the pointer to load
1100 // the correct bytes. For little endian systems, we merely need to read
1101 // fewer bytes from the same pointer.
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001102 unsigned PtrOff =
1103 (MVT::getSizeInBits(LoadedVT) - MVT::getSizeInBits(EVT)) / 8;
1104 SDOperand NewPtr = N0.getOperand(1);
1105 if (!TLI.isLittleEndian())
1106 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1107 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001108 AddToWorkList(NewPtr.Val);
Chris Lattner15045b62006-02-28 06:35:35 +00001109 SDOperand Load =
1110 DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
1111 N0.getOperand(2), EVT);
Chris Lattner5750df92006-03-01 04:03:14 +00001112 AddToWorkList(N);
Chris Lattner15045b62006-02-28 06:35:35 +00001113 CombineTo(N0.Val, Load, Load.getValue(1));
1114 return SDOperand();
1115 }
1116 }
1117
Nate Begeman83e75ec2005-09-06 04:43:02 +00001118 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001119}
1120
Nate Begeman83e75ec2005-09-06 04:43:02 +00001121SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001122 SDOperand N0 = N->getOperand(0);
1123 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001124 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001125 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1126 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001127 MVT::ValueType VT = N1.getValueType();
1128 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001129
1130 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001131 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001132 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001133 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001134 if (N0C && !N1C)
1135 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001138 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001140 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001141 return N1;
1142 // fold (or x, c) -> c iff (x & ~c) == 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001143 if (N1C &&
1144 TLI.MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001145 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001146 // reassociate or
1147 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1148 if (ROR.Val != 0)
1149 return ROR;
1150 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1151 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001152 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001153 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1154 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1155 N1),
1156 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001157 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001158 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1159 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1160 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1161 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1162
1163 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1164 MVT::isInteger(LL.getValueType())) {
1165 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1166 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1167 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1168 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1169 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001170 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001171 return DAG.getSetCC(VT, ORNode, LR, Op1);
1172 }
1173 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1174 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1175 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1176 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1177 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001178 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001179 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1180 }
1181 }
1182 // canonicalize equivalent to ll == rl
1183 if (LL == RR && LR == RL) {
1184 Op1 = ISD::getSetCCSwappedOperands(Op1);
1185 std::swap(RL, RR);
1186 }
1187 if (LL == RL && LR == RR) {
1188 bool isInteger = MVT::isInteger(LL.getValueType());
1189 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1190 if (Result != ISD::SETCC_INVALID)
1191 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1192 }
1193 }
1194 // fold (or (zext x), (zext y)) -> (zext (or x, y))
1195 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1196 N1.getOpcode() == ISD::ZERO_EXTEND &&
1197 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1198 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1199 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001200 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001201 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
1202 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001203 // fold (or (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (or x, y))
1204 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1205 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1206 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1207 N0.getOperand(1) == N1.getOperand(1)) {
1208 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
1209 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001210 AddToWorkList(ORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001211 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1212 }
Nate Begeman35ef9132006-01-11 21:21:00 +00001213 // canonicalize shl to left side in a shl/srl pair, to match rotate
1214 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
1215 std::swap(N0, N1);
1216 // check for rotl, rotr
1217 if (N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SRL &&
1218 N0.getOperand(0) == N1.getOperand(0) &&
Chris Lattneraf551bc2006-01-12 18:57:33 +00001219 TLI.isOperationLegal(ISD::ROTL, VT) && TLI.isTypeLegal(VT)) {
Nate Begeman35ef9132006-01-11 21:21:00 +00001220 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1221 if (N0.getOperand(1).getOpcode() == ISD::Constant &&
1222 N1.getOperand(1).getOpcode() == ISD::Constant) {
1223 uint64_t c1val = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1224 uint64_t c2val = cast<ConstantSDNode>(N1.getOperand(1))->getValue();
1225 if ((c1val + c2val) == OpSizeInBits)
1226 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1227 }
1228 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
1229 if (N1.getOperand(1).getOpcode() == ISD::SUB &&
1230 N0.getOperand(1) == N1.getOperand(1).getOperand(1))
1231 if (ConstantSDNode *SUBC =
1232 dyn_cast<ConstantSDNode>(N1.getOperand(1).getOperand(0)))
1233 if (SUBC->getValue() == OpSizeInBits)
1234 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0), N0.getOperand(1));
1235 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
1236 if (N0.getOperand(1).getOpcode() == ISD::SUB &&
1237 N1.getOperand(1) == N0.getOperand(1).getOperand(1))
1238 if (ConstantSDNode *SUBC =
1239 dyn_cast<ConstantSDNode>(N0.getOperand(1).getOperand(0)))
1240 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattneraf551bc2006-01-12 18:57:33 +00001241 if (TLI.isOperationLegal(ISD::ROTR, VT) && TLI.isTypeLegal(VT))
Nate Begeman35ef9132006-01-11 21:21:00 +00001242 return DAG.getNode(ISD::ROTR, VT, N0.getOperand(0),
1243 N1.getOperand(1));
1244 else
1245 return DAG.getNode(ISD::ROTL, VT, N0.getOperand(0),
1246 N0.getOperand(1));
1247 }
1248 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001249 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001250}
1251
Nate Begeman83e75ec2005-09-06 04:43:02 +00001252SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001253 SDOperand N0 = N->getOperand(0);
1254 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001255 SDOperand LHS, RHS, CC;
1256 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1257 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001258 MVT::ValueType VT = N0.getValueType();
1259
1260 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001261 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001262 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001263 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001264 if (N0C && !N1C)
1265 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001266 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001267 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001268 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001269 // reassociate xor
1270 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
1271 if (RXOR.Val != 0)
1272 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001273 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00001274 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
1275 bool isInt = MVT::isInteger(LHS.getValueType());
1276 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
1277 isInt);
1278 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001279 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001280 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001281 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00001282 assert(0 && "Unhandled SetCC Equivalent!");
1283 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001284 }
Nate Begeman99801192005-09-07 23:25:52 +00001285 // fold !(x or y) -> (!x and !y) iff x or y are setcc
1286 if (N1C && N1C->getValue() == 1 &&
1287 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001288 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001289 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
1290 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001291 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1292 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001293 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001294 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001295 }
1296 }
Nate Begeman99801192005-09-07 23:25:52 +00001297 // fold !(x or y) -> (!x and !y) iff x or y are constants
1298 if (N1C && N1C->isAllOnesValue() &&
1299 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001300 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00001301 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
1302 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001303 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
1304 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00001305 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00001306 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001307 }
1308 }
Nate Begeman223df222005-09-08 20:18:10 +00001309 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
1310 if (N1C && N0.getOpcode() == ISD::XOR) {
1311 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
1312 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1313 if (N00C)
1314 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
1315 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
1316 if (N01C)
1317 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
1318 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
1319 }
1320 // fold (xor x, x) -> 0
1321 if (N0 == N1)
1322 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001323 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
1324 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
1325 N1.getOpcode() == ISD::ZERO_EXTEND &&
1326 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1327 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1328 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001329 AddToWorkList(XORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001330 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
1331 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001332 // fold (xor (shl/srl/sra x), (shl/srl/sra y)) -> (shl/srl/sra (xor x, y))
1333 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
1334 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL) ||
1335 (N0.getOpcode() == ISD::SRA && N1.getOpcode() == ISD::SRA)) &&
1336 N0.getOperand(1) == N1.getOperand(1)) {
1337 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
1338 N0.getOperand(0), N1.getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00001339 AddToWorkList(XORNode.Val);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001340 return DAG.getNode(N0.getOpcode(), VT, XORNode, N0.getOperand(1));
1341 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001342 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001343}
1344
Nate Begeman83e75ec2005-09-06 04:43:02 +00001345SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001346 SDOperand N0 = N->getOperand(0);
1347 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001348 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1349 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350 MVT::ValueType VT = N0.getValueType();
1351 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1352
1353 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001354 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001355 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001357 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001358 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001359 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001360 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001361 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001362 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001363 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001364 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001365 // if (shl x, c) is known to be zero, return 0
Nate Begemanfb7217b2006-02-17 19:54:08 +00001366 if (TLI.MaskedValueIsZero(SDOperand(N, 0), MVT::getIntVTBitMask(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001367 return DAG.getConstant(0, VT);
Chris Lattner012f2412006-02-17 21:58:01 +00001368 if (SimplifyDemandedBits(SDOperand(N, 0)))
Nate Begemande996292006-02-03 22:24:05 +00001369 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001370 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001371 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001372 N0.getOperand(1).getOpcode() == ISD::Constant) {
1373 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001374 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001375 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001376 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001378 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001379 }
1380 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
1381 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001382 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 N0.getOperand(1).getOpcode() == ISD::Constant) {
1384 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001385 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001386 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
1387 DAG.getConstant(~0ULL << c1, VT));
1388 if (c2 > c1)
1389 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001390 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001391 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001392 return DAG.getNode(ISD::SRL, VT, Mask,
1393 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001394 }
1395 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001396 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001397 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001398 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1399 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001400}
1401
Nate Begeman83e75ec2005-09-06 04:43:02 +00001402SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403 SDOperand N0 = N->getOperand(0);
1404 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001405 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1406 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001407 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408
1409 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001410 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001411 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001412 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001413 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001414 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001415 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001416 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001417 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001418 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00001419 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001420 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001421 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001422 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001423 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00001424 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
1425 // sext_inreg.
1426 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
1427 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
1428 MVT::ValueType EVT;
1429 switch (LowBits) {
1430 default: EVT = MVT::Other; break;
1431 case 1: EVT = MVT::i1; break;
1432 case 8: EVT = MVT::i8; break;
1433 case 16: EVT = MVT::i16; break;
1434 case 32: EVT = MVT::i32; break;
1435 }
1436 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
1437 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1438 DAG.getValueType(EVT));
1439 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00001440
1441 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
1442 if (N1C && N0.getOpcode() == ISD::SRA) {
1443 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1444 unsigned Sum = N1C->getValue() + C1->getValue();
1445 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
1446 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
1447 DAG.getConstant(Sum, N1C->getValueType(0)));
1448 }
1449 }
1450
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begemanfb7217b2006-02-17 19:54:08 +00001452 if (TLI.MaskedValueIsZero(N0, MVT::getIntVTSignBit(VT)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001453 return DAG.getNode(ISD::SRL, VT, N0, N1);
1454 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001455}
1456
Nate Begeman83e75ec2005-09-06 04:43:02 +00001457SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001458 SDOperand N0 = N->getOperand(0);
1459 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001460 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1461 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001462 MVT::ValueType VT = N0.getValueType();
1463 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1464
1465 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001466 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001467 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001468 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001469 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001470 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001472 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001473 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001474 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001475 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001476 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001477 // if (srl x, c) is known to be zero, return 0
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001478 if (N1C && TLI.MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001479 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001481 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001482 N0.getOperand(1).getOpcode() == ISD::Constant) {
1483 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001484 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001485 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001486 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001487 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001488 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001489 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001490 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001491}
1492
Nate Begeman83e75ec2005-09-06 04:43:02 +00001493SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001494 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001495 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001496 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001497
1498 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001499 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001500 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001501 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001502}
1503
Nate Begeman83e75ec2005-09-06 04:43:02 +00001504SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001505 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001506 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001507 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001508
1509 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001510 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001511 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001512 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001513}
1514
Nate Begeman83e75ec2005-09-06 04:43:02 +00001515SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001516 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001517 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001518 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001519
1520 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001521 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001522 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001523 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001524}
1525
Nate Begeman452d7be2005-09-16 00:54:12 +00001526SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1527 SDOperand N0 = N->getOperand(0);
1528 SDOperand N1 = N->getOperand(1);
1529 SDOperand N2 = N->getOperand(2);
1530 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1531 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1532 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1533 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001534
Nate Begeman452d7be2005-09-16 00:54:12 +00001535 // fold select C, X, X -> X
1536 if (N1 == N2)
1537 return N1;
1538 // fold select true, X, Y -> X
1539 if (N0C && !N0C->isNullValue())
1540 return N1;
1541 // fold select false, X, Y -> Y
1542 if (N0C && N0C->isNullValue())
1543 return N2;
1544 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001545 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001546 return DAG.getNode(ISD::OR, VT, N0, N2);
1547 // fold select C, 0, X -> ~C & X
1548 // FIXME: this should check for C type == X type, not i1?
1549 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1550 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001551 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001552 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1553 }
1554 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001555 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001556 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001557 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00001558 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1559 }
1560 // fold select C, X, 0 -> C & X
1561 // FIXME: this should check for C type == X type, not i1?
1562 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1563 return DAG.getNode(ISD::AND, VT, N0, N1);
1564 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1565 if (MVT::i1 == VT && N0 == N1)
1566 return DAG.getNode(ISD::OR, VT, N0, N2);
1567 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1568 if (MVT::i1 == VT && N0 == N2)
1569 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner40c62d52005-10-18 06:04:22 +00001570 // If we can fold this based on the true/false value, do so.
1571 if (SimplifySelectOps(N, N1, N2))
1572 return SDOperand();
Nate Begeman44728a72005-09-19 22:34:01 +00001573 // fold selects based on a setcc into other things, such as min/max/abs
1574 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman750ac1b2006-02-01 07:19:44 +00001575 // FIXME:
1576 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
1577 // having to say they don't support SELECT_CC on every type the DAG knows
1578 // about, since there is no way to mark an opcode illegal at all value types
1579 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
1580 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
1581 N1, N2, N0.getOperand(2));
1582 else
1583 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001584 return SDOperand();
1585}
1586
1587SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001588 SDOperand N0 = N->getOperand(0);
1589 SDOperand N1 = N->getOperand(1);
1590 SDOperand N2 = N->getOperand(2);
1591 SDOperand N3 = N->getOperand(3);
1592 SDOperand N4 = N->getOperand(4);
1593 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1594 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1595 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1596 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1597
1598 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001599 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001600 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1601
Nate Begeman44728a72005-09-19 22:34:01 +00001602 // fold select_cc lhs, rhs, x, x, cc -> x
1603 if (N2 == N3)
1604 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00001605
1606 // If we can fold this based on the true/false value, do so.
1607 if (SimplifySelectOps(N, N2, N3))
1608 return SDOperand();
1609
Nate Begeman44728a72005-09-19 22:34:01 +00001610 // fold select_cc into other things, such as min/max/abs
1611 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001612}
1613
1614SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1615 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1616 cast<CondCodeSDNode>(N->getOperand(2))->get());
1617}
1618
Nate Begeman83e75ec2005-09-06 04:43:02 +00001619SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001620 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001621 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622 MVT::ValueType VT = N->getValueType(0);
1623
Nate Begeman1d4d4142005-09-01 00:19:25 +00001624 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001625 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001626 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001627 // fold (sext (sext x)) -> (sext x)
1628 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001629 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001630 // fold (sext (truncate x)) -> (sextinreg x) iff x size == sext size.
Chris Lattnercc2210b2005-12-07 18:02:05 +00001631 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
1632 (!AfterLegalize ||
1633 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, N0.getValueType())))
Chris Lattnerb14ab8a2005-12-07 07:11:03 +00001634 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
1635 DAG.getValueType(N0.getValueType()));
Evan Cheng110dec22005-12-14 02:19:23 +00001636 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001637 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1638 (!AfterLegalize||TLI.isOperationLegal(ISD::SEXTLOAD, N0.getValueType()))){
Nate Begeman3df4d522005-10-12 20:40:40 +00001639 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1640 N0.getOperand(1), N0.getOperand(2),
1641 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001642 CombineTo(N, ExtLoad);
Chris Lattnerf9884052005-10-13 21:52:31 +00001643 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1644 ExtLoad.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001645 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001646 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001647
1648 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
1649 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
1650 if ((N0.getOpcode() == ISD::SEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1651 N0.hasOneUse()) {
1652 SDOperand ExtLoad = DAG.getNode(ISD::SEXTLOAD, VT, N0.getOperand(0),
1653 N0.getOperand(1), N0.getOperand(2),
1654 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001655 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001656 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1657 ExtLoad.getValue(1));
1658 return SDOperand();
1659 }
1660
Nate Begeman83e75ec2005-09-06 04:43:02 +00001661 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001662}
1663
Nate Begeman83e75ec2005-09-06 04:43:02 +00001664SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001665 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001666 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001667 MVT::ValueType VT = N->getValueType(0);
1668
Nate Begeman1d4d4142005-09-01 00:19:25 +00001669 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001670 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001671 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001672 // fold (zext (zext x)) -> (zext x)
1673 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001674 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Evan Cheng110dec22005-12-14 02:19:23 +00001675 // fold (zext (truncate x)) -> (zextinreg x) iff x size == zext size.
1676 if (N0.getOpcode() == ISD::TRUNCATE && N0.getOperand(0).getValueType() == VT&&
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001677 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, N0.getValueType())))
Chris Lattner00cb95c2005-12-14 07:58:38 +00001678 return DAG.getZeroExtendInReg(N0.getOperand(0), N0.getValueType());
Evan Cheng110dec22005-12-14 02:19:23 +00001679 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Chris Lattnerd0f6d182005-12-15 19:02:38 +00001680 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse() &&
1681 (!AfterLegalize||TLI.isOperationLegal(ISD::ZEXTLOAD, N0.getValueType()))){
Evan Cheng110dec22005-12-14 02:19:23 +00001682 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1683 N0.getOperand(1), N0.getOperand(2),
1684 N0.getValueType());
Chris Lattnerd4771842005-12-14 19:25:30 +00001685 CombineTo(N, ExtLoad);
Evan Cheng110dec22005-12-14 02:19:23 +00001686 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1687 ExtLoad.getValue(1));
1688 return SDOperand();
1689 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001690
1691 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
1692 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
1693 if ((N0.getOpcode() == ISD::ZEXTLOAD || N0.getOpcode() == ISD::EXTLOAD) &&
1694 N0.hasOneUse()) {
1695 SDOperand ExtLoad = DAG.getNode(ISD::ZEXTLOAD, VT, N0.getOperand(0),
1696 N0.getOperand(1), N0.getOperand(2),
1697 N0.getOperand(3));
Chris Lattnerd4771842005-12-14 19:25:30 +00001698 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00001699 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
1700 ExtLoad.getValue(1));
1701 return SDOperand();
1702 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001703 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001704}
1705
Nate Begeman83e75ec2005-09-06 04:43:02 +00001706SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001707 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001708 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001709 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001710 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001711 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001712 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001713
Nate Begeman1d4d4142005-09-01 00:19:25 +00001714 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001715 if (N0C) {
1716 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001717 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001718 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001719 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001720 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman216def82005-10-14 01:29:07 +00001721 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001722 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001723 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001724 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1725 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1726 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001727 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001728 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001729 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1730 if (N0.getOpcode() == ISD::AssertSext &&
1731 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001732 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001733 }
1734 // fold (sext_in_reg (sextload x)) -> (sextload x)
1735 if (N0.getOpcode() == ISD::SEXTLOAD &&
1736 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001737 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001738 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001739 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001740 if (N0.getOpcode() == ISD::SETCC &&
1741 TLI.getSetCCResultContents() ==
1742 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001743 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001744 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +00001745 if (TLI.MaskedValueIsZero(N0, 1ULL << (EVTBits-1)))
Nate Begemande996292006-02-03 22:24:05 +00001746 return DAG.getZeroExtendInReg(N0, EVT);
Nate Begeman07ed4172005-10-10 21:26:48 +00001747 // fold (sext_in_reg (srl x)) -> sra x
1748 if (N0.getOpcode() == ISD::SRL &&
1749 N0.getOperand(1).getOpcode() == ISD::Constant &&
1750 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1751 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1752 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001753 }
Nate Begemanded49632005-10-13 03:11:28 +00001754 // fold (sext_inreg (extload x)) -> (sextload x)
1755 if (N0.getOpcode() == ISD::EXTLOAD &&
1756 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001757 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001758 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1759 N0.getOperand(1), N0.getOperand(2),
1760 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001761 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001762 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001763 return SDOperand();
1764 }
1765 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Chris Lattner40c62d52005-10-18 06:04:22 +00001766 if (N0.getOpcode() == ISD::ZEXTLOAD && N0.hasOneUse() &&
Nate Begemanded49632005-10-13 03:11:28 +00001767 EVT == cast<VTSDNode>(N0.getOperand(3))->getVT() &&
Nate Begemanbfd65a02005-10-13 18:34:58 +00001768 (!AfterLegalize || TLI.isOperationLegal(ISD::SEXTLOAD, EVT))) {
Nate Begemanded49632005-10-13 03:11:28 +00001769 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, N0.getOperand(0),
1770 N0.getOperand(1), N0.getOperand(2),
1771 EVT);
Chris Lattnerd4771842005-12-14 19:25:30 +00001772 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00001773 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Nate Begemanded49632005-10-13 03:11:28 +00001774 return SDOperand();
1775 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001776 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001777}
1778
Nate Begeman83e75ec2005-09-06 04:43:02 +00001779SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001780 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001781 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001782 MVT::ValueType VT = N->getValueType(0);
1783
1784 // noop truncate
1785 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001786 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001787 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001788 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001789 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001790 // fold (truncate (truncate x)) -> (truncate x)
1791 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001792 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001793 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1794 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1795 if (N0.getValueType() < VT)
1796 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001797 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001798 else if (N0.getValueType() > VT)
1799 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001800 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001801 else
1802 // if the source and dest are the same type, we can drop both the extend
1803 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001804 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001805 }
Nate Begeman3df4d522005-10-12 20:40:40 +00001806 // fold (truncate (load x)) -> (smaller load x)
Chris Lattner40c62d52005-10-18 06:04:22 +00001807 if (N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Nate Begeman3df4d522005-10-12 20:40:40 +00001808 assert(MVT::getSizeInBits(N0.getValueType()) > MVT::getSizeInBits(VT) &&
1809 "Cannot truncate to larger type!");
1810 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Nate Begeman765784a2005-10-12 23:18:53 +00001811 // For big endian targets, we need to add an offset to the pointer to load
1812 // the correct bytes. For little endian systems, we merely need to read
1813 // fewer bytes from the same pointer.
Nate Begeman3df4d522005-10-12 20:40:40 +00001814 uint64_t PtrOff =
1815 (MVT::getSizeInBits(N0.getValueType()) - MVT::getSizeInBits(VT)) / 8;
Nate Begeman765784a2005-10-12 23:18:53 +00001816 SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) :
1817 DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
1818 DAG.getConstant(PtrOff, PtrType));
Chris Lattner5750df92006-03-01 04:03:14 +00001819 AddToWorkList(NewPtr.Val);
Nate Begeman3df4d522005-10-12 20:40:40 +00001820 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), NewPtr,N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001821 AddToWorkList(N);
Chris Lattner24edbb72005-10-13 22:10:05 +00001822 CombineTo(N0.Val, Load, Load.getValue(1));
Nate Begeman765784a2005-10-12 23:18:53 +00001823 return SDOperand();
Nate Begeman3df4d522005-10-12 20:40:40 +00001824 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001825 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001826}
1827
Chris Lattner94683772005-12-23 05:30:37 +00001828SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
1829 SDOperand N0 = N->getOperand(0);
1830 MVT::ValueType VT = N->getValueType(0);
1831
1832 // If the input is a constant, let getNode() fold it.
1833 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
1834 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
1835 if (Res.Val != N) return Res;
1836 }
1837
Chris Lattnerc8547d82005-12-23 05:37:50 +00001838 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
1839 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
1840
Chris Lattner57104102005-12-23 05:44:41 +00001841 // fold (conv (load x)) -> (load (conv*)x)
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00001842 // FIXME: These xforms need to know that the resultant load doesn't need a
1843 // higher alignment than the original!
1844 if (0 && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
Chris Lattner57104102005-12-23 05:44:41 +00001845 SDOperand Load = DAG.getLoad(VT, N0.getOperand(0), N0.getOperand(1),
1846 N0.getOperand(2));
Chris Lattner5750df92006-03-01 04:03:14 +00001847 AddToWorkList(N);
Chris Lattner57104102005-12-23 05:44:41 +00001848 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
1849 Load.getValue(1));
1850 return Load;
1851 }
1852
Chris Lattner94683772005-12-23 05:30:37 +00001853 return SDOperand();
1854}
1855
Chris Lattner01b3d732005-09-28 22:28:18 +00001856SDOperand DAGCombiner::visitFADD(SDNode *N) {
1857 SDOperand N0 = N->getOperand(0);
1858 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001859 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1860 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001861 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001862
1863 // fold (fadd c1, c2) -> c1+c2
1864 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001865 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001866 // canonicalize constant to RHS
1867 if (N0CFP && !N1CFP)
1868 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001869 // fold (A + (-B)) -> A-B
1870 if (N1.getOpcode() == ISD::FNEG)
1871 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001872 // fold ((-A) + B) -> B-A
1873 if (N0.getOpcode() == ISD::FNEG)
1874 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001875 return SDOperand();
1876}
1877
1878SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1879 SDOperand N0 = N->getOperand(0);
1880 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00001881 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1882 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001883 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00001884
1885 // fold (fsub c1, c2) -> c1-c2
1886 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001887 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001888 // fold (A-(-B)) -> A+B
1889 if (N1.getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00001890 return DAG.getNode(ISD::FADD, VT, N0, N1.getOperand(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001891 return SDOperand();
1892}
1893
1894SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1895 SDOperand N0 = N->getOperand(0);
1896 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001897 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1898 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001899 MVT::ValueType VT = N->getValueType(0);
1900
Nate Begeman11af4ea2005-10-17 20:40:11 +00001901 // fold (fmul c1, c2) -> c1*c2
1902 if (N0CFP && N1CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001903 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001904 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001905 if (N0CFP && !N1CFP)
1906 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00001907 // fold (fmul X, 2.0) -> (fadd X, X)
1908 if (N1CFP && N1CFP->isExactlyValue(+2.0))
1909 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00001910 return SDOperand();
1911}
1912
1913SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1914 SDOperand N0 = N->getOperand(0);
1915 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001916 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1917 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001918 MVT::ValueType VT = N->getValueType(0);
1919
Nate Begemana148d982006-01-18 22:35:16 +00001920 // fold (fdiv c1, c2) -> c1/c2
1921 if (N0CFP && N1CFP)
1922 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001923 return SDOperand();
1924}
1925
1926SDOperand DAGCombiner::visitFREM(SDNode *N) {
1927 SDOperand N0 = N->getOperand(0);
1928 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00001929 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1930 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001931 MVT::ValueType VT = N->getValueType(0);
1932
Nate Begemana148d982006-01-18 22:35:16 +00001933 // fold (frem c1, c2) -> fmod(c1,c2)
1934 if (N0CFP && N1CFP)
1935 return DAG.getNode(ISD::FREM, VT, N0, N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00001936 return SDOperand();
1937}
1938
1939
Nate Begeman83e75ec2005-09-06 04:43:02 +00001940SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001941 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001942 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001943 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001944
1945 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001946 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001947 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001948 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001949}
1950
Nate Begeman83e75ec2005-09-06 04:43:02 +00001951SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001952 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001953 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00001954 MVT::ValueType VT = N->getValueType(0);
1955
Nate Begeman1d4d4142005-09-01 00:19:25 +00001956 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001957 if (N0C)
Nate Begemana148d982006-01-18 22:35:16 +00001958 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001959 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001960}
1961
Nate Begeman83e75ec2005-09-06 04:43:02 +00001962SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001963 SDOperand N0 = N->getOperand(0);
1964 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1965 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001966
1967 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001968 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001969 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001970 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001971}
1972
Nate Begeman83e75ec2005-09-06 04:43:02 +00001973SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001974 SDOperand N0 = N->getOperand(0);
1975 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1976 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001977
1978 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001979 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001980 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001981 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001982}
1983
Nate Begeman83e75ec2005-09-06 04:43:02 +00001984SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00001985 SDOperand N0 = N->getOperand(0);
1986 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
1987 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001988
1989 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001990 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00001991 return DAG.getNode(ISD::FP_ROUND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001992 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001993}
1994
Nate Begeman83e75ec2005-09-06 04:43:02 +00001995SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001996 SDOperand N0 = N->getOperand(0);
1997 MVT::ValueType VT = N->getValueType(0);
1998 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001999 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002000
Nate Begeman1d4d4142005-09-01 00:19:25 +00002001 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002002 if (N0CFP) {
2003 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002004 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002005 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00002006 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002007}
2008
Nate Begeman83e75ec2005-09-06 04:43:02 +00002009SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002010 SDOperand N0 = N->getOperand(0);
2011 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2012 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002013
2014 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00002015 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002016 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002017 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002018}
2019
Nate Begeman83e75ec2005-09-06 04:43:02 +00002020SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002021 SDOperand N0 = N->getOperand(0);
2022 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2023 MVT::ValueType VT = N->getValueType(0);
2024
2025 // fold (fneg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00002026 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002027 return DAG.getNode(ISD::FNEG, VT, N0);
2028 // fold (fneg (sub x, y)) -> (sub y, x)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002029 if (N->getOperand(0).getOpcode() == ISD::SUB)
Nate Begemana148d982006-01-18 22:35:16 +00002030 return DAG.getNode(ISD::SUB, VT, N->getOperand(1), N->getOperand(0));
2031 // fold (fneg (fneg x)) -> x
Nate Begeman1d4d4142005-09-01 00:19:25 +00002032 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002033 return N->getOperand(0).getOperand(0);
2034 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002035}
2036
Nate Begeman83e75ec2005-09-06 04:43:02 +00002037SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00002038 SDOperand N0 = N->getOperand(0);
2039 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
2040 MVT::ValueType VT = N->getValueType(0);
2041
Nate Begeman1d4d4142005-09-01 00:19:25 +00002042 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002043 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00002044 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002045 // fold (fabs (fabs x)) -> (fabs x)
2046 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002047 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002048 // fold (fabs (fneg x)) -> (fabs x)
2049 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begemana148d982006-01-18 22:35:16 +00002050 return DAG.getNode(ISD::FABS, VT, N->getOperand(0).getOperand(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +00002051 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002052}
2053
Nate Begeman44728a72005-09-19 22:34:01 +00002054SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
2055 SDOperand Chain = N->getOperand(0);
2056 SDOperand N1 = N->getOperand(1);
2057 SDOperand N2 = N->getOperand(2);
2058 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2059
2060 // never taken branch, fold to chain
2061 if (N1C && N1C->isNullValue())
2062 return Chain;
2063 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00002064 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00002065 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002066 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
2067 // on the target.
2068 if (N1.getOpcode() == ISD::SETCC &&
2069 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
2070 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
2071 N1.getOperand(0), N1.getOperand(1), N2);
2072 }
Nate Begeman44728a72005-09-19 22:34:01 +00002073 return SDOperand();
2074}
2075
2076SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
2077 SDOperand Chain = N->getOperand(0);
2078 SDOperand N1 = N->getOperand(1);
2079 SDOperand N2 = N->getOperand(2);
2080 SDOperand N3 = N->getOperand(3);
2081 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2082
2083 // unconditional branch to true mbb
2084 if (N1C && N1C->getValue() == 1)
2085 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
2086 // unconditional branch to false mbb
2087 if (N1C && N1C->isNullValue())
2088 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002089 // fold a brcondtwoway with a setcc condition into a BRTWOWAY_CC node if
2090 // BRTWOWAY_CC is legal on the target.
2091 if (N1.getOpcode() == ISD::SETCC &&
2092 TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
2093 std::vector<SDOperand> Ops;
2094 Ops.push_back(Chain);
2095 Ops.push_back(N1.getOperand(2));
2096 Ops.push_back(N1.getOperand(0));
2097 Ops.push_back(N1.getOperand(1));
2098 Ops.push_back(N2);
2099 Ops.push_back(N3);
2100 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2101 }
Nate Begeman44728a72005-09-19 22:34:01 +00002102 return SDOperand();
2103}
2104
Chris Lattner3ea0b472005-10-05 06:47:48 +00002105// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
2106//
Nate Begeman44728a72005-09-19 22:34:01 +00002107SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00002108 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
2109 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
2110
2111 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00002112 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
2113 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
2114
2115 // fold br_cc true, dest -> br dest (unconditional branch)
2116 if (SCCC && SCCC->getValue())
2117 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
2118 N->getOperand(4));
2119 // fold br_cc false, dest -> unconditional fall through
2120 if (SCCC && SCCC->isNullValue())
2121 return N->getOperand(0);
2122 // fold to a simpler setcc
2123 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
2124 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
2125 Simp.getOperand(2), Simp.getOperand(0),
2126 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00002127 return SDOperand();
2128}
2129
2130SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00002131 SDOperand Chain = N->getOperand(0);
2132 SDOperand CCN = N->getOperand(1);
2133 SDOperand LHS = N->getOperand(2);
2134 SDOperand RHS = N->getOperand(3);
2135 SDOperand N4 = N->getOperand(4);
2136 SDOperand N5 = N->getOperand(5);
2137
2138 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
2139 cast<CondCodeSDNode>(CCN)->get(), false);
2140 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2141
2142 // fold select_cc lhs, rhs, x, x, cc -> x
2143 if (N4 == N5)
2144 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2145 // fold select_cc true, x, y -> x
2146 if (SCCC && SCCC->getValue())
2147 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
2148 // fold select_cc false, x, y -> y
2149 if (SCCC && SCCC->isNullValue())
2150 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
2151 // fold to a simpler setcc
Chris Lattner03d5e872006-01-29 06:00:45 +00002152 if (SCC.Val && SCC.getOpcode() == ISD::SETCC) {
2153 std::vector<SDOperand> Ops;
2154 Ops.push_back(Chain);
2155 Ops.push_back(SCC.getOperand(2));
2156 Ops.push_back(SCC.getOperand(0));
2157 Ops.push_back(SCC.getOperand(1));
2158 Ops.push_back(N4);
2159 Ops.push_back(N5);
2160 return DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
2161 }
Nate Begeman44728a72005-09-19 22:34:01 +00002162 return SDOperand();
2163}
2164
Chris Lattner01a22022005-10-10 22:04:48 +00002165SDOperand DAGCombiner::visitLOAD(SDNode *N) {
2166 SDOperand Chain = N->getOperand(0);
2167 SDOperand Ptr = N->getOperand(1);
2168 SDOperand SrcValue = N->getOperand(2);
2169
2170 // If this load is directly stored, replace the load value with the stored
2171 // value.
2172 // TODO: Handle store large -> read small portion.
2173 // TODO: Handle TRUNCSTORE/EXTLOAD
2174 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
2175 Chain.getOperand(1).getValueType() == N->getValueType(0))
2176 return CombineTo(N, Chain.getOperand(1), Chain);
2177
2178 return SDOperand();
2179}
2180
Chris Lattner87514ca2005-10-10 22:31:19 +00002181SDOperand DAGCombiner::visitSTORE(SDNode *N) {
2182 SDOperand Chain = N->getOperand(0);
2183 SDOperand Value = N->getOperand(1);
2184 SDOperand Ptr = N->getOperand(2);
2185 SDOperand SrcValue = N->getOperand(3);
2186
2187 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002188 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
Chris Lattnerfe7f0462005-10-27 07:10:34 +00002189 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */ &&
2190 // Make sure that these stores are the same value type:
2191 // FIXME: we really care that the second store is >= size of the first.
2192 Value.getValueType() == Chain.getOperand(1).getValueType()) {
Chris Lattner87514ca2005-10-10 22:31:19 +00002193 // Create a new store of Value that replaces both stores.
2194 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002195 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
2196 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00002197 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
2198 PrevStore->getOperand(0), Value, Ptr,
2199 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002200 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00002201 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00002202 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00002203 }
2204
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002205 // If this is a store of a bit convert, store the input value.
Chris Lattnerbf40c4b2006-01-15 18:58:59 +00002206 // FIXME: This needs to know that the resultant store does not need a
2207 // higher alignment than the original.
2208 if (0 && Value.getOpcode() == ISD::BIT_CONVERT)
Chris Lattnerc33baaa2005-12-23 05:48:07 +00002209 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Value.getOperand(0),
2210 Ptr, SrcValue);
2211
Chris Lattner87514ca2005-10-10 22:31:19 +00002212 return SDOperand();
2213}
2214
Nate Begeman44728a72005-09-19 22:34:01 +00002215SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00002216 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
2217
2218 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
2219 cast<CondCodeSDNode>(N0.getOperand(2))->get());
2220 // If we got a simplified select_cc node back from SimplifySelectCC, then
2221 // break it down into a new SETCC node, and a new SELECT node, and then return
2222 // the SELECT node, since we were called with a SELECT node.
2223 if (SCC.Val) {
2224 // Check to see if we got a select_cc back (to turn into setcc/select).
2225 // Otherwise, just return whatever node we got back, like fabs.
2226 if (SCC.getOpcode() == ISD::SELECT_CC) {
2227 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
2228 SCC.getOperand(0), SCC.getOperand(1),
2229 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00002230 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002231 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
2232 SCC.getOperand(3), SETCC);
2233 }
2234 return SCC;
2235 }
Nate Begeman44728a72005-09-19 22:34:01 +00002236 return SDOperand();
2237}
2238
Chris Lattner40c62d52005-10-18 06:04:22 +00002239/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
2240/// are the two values being selected between, see if we can simplify the
2241/// select.
2242///
2243bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
2244 SDOperand RHS) {
2245
2246 // If this is a select from two identical things, try to pull the operation
2247 // through the select.
2248 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
2249#if 0
2250 std::cerr << "SELECT: ["; LHS.Val->dump();
2251 std::cerr << "] ["; RHS.Val->dump();
2252 std::cerr << "]\n";
2253#endif
2254
2255 // If this is a load and the token chain is identical, replace the select
2256 // of two loads with a load through a select of the address to load from.
2257 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
2258 // constants have been dropped into the constant pool.
2259 if ((LHS.getOpcode() == ISD::LOAD ||
2260 LHS.getOpcode() == ISD::EXTLOAD ||
2261 LHS.getOpcode() == ISD::ZEXTLOAD ||
2262 LHS.getOpcode() == ISD::SEXTLOAD) &&
2263 // Token chains must be identical.
2264 LHS.getOperand(0) == RHS.getOperand(0) &&
2265 // If this is an EXTLOAD, the VT's must match.
2266 (LHS.getOpcode() == ISD::LOAD ||
2267 LHS.getOperand(3) == RHS.getOperand(3))) {
2268 // FIXME: this conflates two src values, discarding one. This is not
2269 // the right thing to do, but nothing uses srcvalues now. When they do,
2270 // turn SrcValue into a list of locations.
2271 SDOperand Addr;
2272 if (TheSelect->getOpcode() == ISD::SELECT)
2273 Addr = DAG.getNode(ISD::SELECT, LHS.getOperand(1).getValueType(),
2274 TheSelect->getOperand(0), LHS.getOperand(1),
2275 RHS.getOperand(1));
2276 else
2277 Addr = DAG.getNode(ISD::SELECT_CC, LHS.getOperand(1).getValueType(),
2278 TheSelect->getOperand(0),
2279 TheSelect->getOperand(1),
2280 LHS.getOperand(1), RHS.getOperand(1),
2281 TheSelect->getOperand(4));
2282
2283 SDOperand Load;
2284 if (LHS.getOpcode() == ISD::LOAD)
2285 Load = DAG.getLoad(TheSelect->getValueType(0), LHS.getOperand(0),
2286 Addr, LHS.getOperand(2));
2287 else
2288 Load = DAG.getExtLoad(LHS.getOpcode(), TheSelect->getValueType(0),
2289 LHS.getOperand(0), Addr, LHS.getOperand(2),
2290 cast<VTSDNode>(LHS.getOperand(3))->getVT());
2291 // Users of the select now use the result of the load.
2292 CombineTo(TheSelect, Load);
2293
2294 // Users of the old loads now use the new load's chain. We know the
2295 // old-load value is dead now.
2296 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
2297 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
2298 return true;
2299 }
2300 }
2301
2302 return false;
2303}
2304
Nate Begeman44728a72005-09-19 22:34:01 +00002305SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
2306 SDOperand N2, SDOperand N3,
2307 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00002308
2309 MVT::ValueType VT = N2.getValueType();
2310 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
2311 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2312 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2313 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
2314
2315 // Determine if the condition we're dealing with is constant
2316 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
2317 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
2318
2319 // fold select_cc true, x, y -> x
2320 if (SCCC && SCCC->getValue())
2321 return N2;
2322 // fold select_cc false, x, y -> y
2323 if (SCCC && SCCC->getValue() == 0)
2324 return N3;
2325
2326 // Check to see if we can simplify the select into an fabs node
2327 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
2328 // Allow either -0.0 or 0.0
2329 if (CFP->getValue() == 0.0) {
2330 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
2331 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
2332 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
2333 N2 == N3.getOperand(0))
2334 return DAG.getNode(ISD::FABS, VT, N0);
2335
2336 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
2337 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
2338 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
2339 N2.getOperand(0) == N3)
2340 return DAG.getNode(ISD::FABS, VT, N3);
2341 }
2342 }
2343
2344 // Check to see if we can perform the "gzip trick", transforming
2345 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
2346 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
2347 MVT::isInteger(N0.getValueType()) &&
2348 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
2349 MVT::ValueType XType = N0.getValueType();
2350 MVT::ValueType AType = N2.getValueType();
2351 if (XType >= AType) {
2352 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00002353 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00002354 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
2355 unsigned ShCtV = Log2_64(N2C->getValue());
2356 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
2357 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
2358 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00002359 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002360 if (XType > AType) {
2361 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002362 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002363 }
2364 return DAG.getNode(ISD::AND, AType, Shift, N2);
2365 }
2366 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2367 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2368 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002369 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002370 if (XType > AType) {
2371 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002372 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002373 }
2374 return DAG.getNode(ISD::AND, AType, Shift, N2);
2375 }
2376 }
Nate Begeman07ed4172005-10-10 21:26:48 +00002377
2378 // fold select C, 16, 0 -> shl C, 4
2379 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
2380 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
2381 // Get a SetCC of the condition
2382 // FIXME: Should probably make sure that setcc is legal if we ever have a
2383 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00002384 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00002385 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00002386 if (AfterLegalize) {
2387 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002388 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
Nate Begemanb0d04a72006-02-18 02:40:58 +00002389 } else {
2390 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00002391 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00002392 }
Chris Lattner5750df92006-03-01 04:03:14 +00002393 AddToWorkList(SCC.Val);
2394 AddToWorkList(Temp.Val);
Nate Begeman07ed4172005-10-10 21:26:48 +00002395 // shl setcc result by log2 n2c
2396 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
2397 DAG.getConstant(Log2_64(N2C->getValue()),
2398 TLI.getShiftAmountTy()));
2399 }
2400
Nate Begemanf845b452005-10-08 00:29:44 +00002401 // Check to see if this is the equivalent of setcc
2402 // FIXME: Turn all of these into setcc if setcc if setcc is legal
2403 // otherwise, go ahead with the folds.
2404 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
2405 MVT::ValueType XType = N0.getValueType();
2406 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
2407 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
2408 if (Res.getValueType() != VT)
2409 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
2410 return Res;
2411 }
2412
2413 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
2414 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
2415 TLI.isOperationLegal(ISD::CTLZ, XType)) {
2416 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
2417 return DAG.getNode(ISD::SRL, XType, Ctlz,
2418 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
2419 TLI.getShiftAmountTy()));
2420 }
2421 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
2422 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
2423 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
2424 N0);
2425 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
2426 DAG.getConstant(~0ULL, XType));
2427 return DAG.getNode(ISD::SRL, XType,
2428 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
2429 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2430 TLI.getShiftAmountTy()));
2431 }
2432 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
2433 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
2434 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
2435 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2436 TLI.getShiftAmountTy()));
2437 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
2438 }
2439 }
2440
2441 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
2442 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2443 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
2444 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
2445 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
2446 MVT::ValueType XType = N0.getValueType();
2447 if (SubC->isNullValue() && MVT::isInteger(XType)) {
2448 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
2449 DAG.getConstant(MVT::getSizeInBits(XType)-1,
2450 TLI.getShiftAmountTy()));
2451 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00002452 AddToWorkList(Shift.Val);
2453 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00002454 return DAG.getNode(ISD::XOR, XType, Add, Shift);
2455 }
2456 }
2457 }
2458
Nate Begeman44728a72005-09-19 22:34:01 +00002459 return SDOperand();
2460}
2461
Nate Begeman452d7be2005-09-16 00:54:12 +00002462SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00002463 SDOperand N1, ISD::CondCode Cond,
2464 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002465 // These setcc operations always fold.
2466 switch (Cond) {
2467 default: break;
2468 case ISD::SETFALSE:
2469 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
2470 case ISD::SETTRUE:
2471 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
2472 }
2473
2474 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
2475 uint64_t C1 = N1C->getValue();
2476 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
2477 uint64_t C0 = N0C->getValue();
2478
2479 // Sign extend the operands if required
2480 if (ISD::isSignedIntSetCC(Cond)) {
2481 C0 = N0C->getSignExtended();
2482 C1 = N1C->getSignExtended();
2483 }
2484
2485 switch (Cond) {
2486 default: assert(0 && "Unknown integer setcc!");
2487 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2488 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2489 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
2490 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
2491 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
2492 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
2493 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
2494 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
2495 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
2496 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
2497 }
2498 } else {
2499 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
2500 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
2501 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
2502
2503 // If the comparison constant has bits in the upper part, the
2504 // zero-extended value could never match.
2505 if (C1 & (~0ULL << InSize)) {
2506 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
2507 switch (Cond) {
2508 case ISD::SETUGT:
2509 case ISD::SETUGE:
2510 case ISD::SETEQ: return DAG.getConstant(0, VT);
2511 case ISD::SETULT:
2512 case ISD::SETULE:
2513 case ISD::SETNE: return DAG.getConstant(1, VT);
2514 case ISD::SETGT:
2515 case ISD::SETGE:
2516 // True if the sign bit of C1 is set.
2517 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
2518 case ISD::SETLT:
2519 case ISD::SETLE:
2520 // True if the sign bit of C1 isn't set.
2521 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
2522 default:
2523 break;
2524 }
2525 }
2526
2527 // Otherwise, we can perform the comparison with the low bits.
2528 switch (Cond) {
2529 case ISD::SETEQ:
2530 case ISD::SETNE:
2531 case ISD::SETUGT:
2532 case ISD::SETUGE:
2533 case ISD::SETULT:
2534 case ISD::SETULE:
2535 return DAG.getSetCC(VT, N0.getOperand(0),
2536 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
2537 Cond);
2538 default:
2539 break; // todo, be more careful with signed comparisons
2540 }
2541 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2542 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
2543 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
2544 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
2545 MVT::ValueType ExtDstTy = N0.getValueType();
2546 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
2547
2548 // If the extended part has any inconsistent bits, it cannot ever
2549 // compare equal. In other words, they have to be all ones or all
2550 // zeros.
2551 uint64_t ExtBits =
2552 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
2553 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
2554 return DAG.getConstant(Cond == ISD::SETNE, VT);
2555
2556 SDOperand ZextOp;
2557 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
2558 if (Op0Ty == ExtSrcTy) {
2559 ZextOp = N0.getOperand(0);
2560 } else {
2561 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
2562 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
2563 DAG.getConstant(Imm, Op0Ty));
2564 }
Chris Lattner5750df92006-03-01 04:03:14 +00002565 AddToWorkList(ZextOp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002566 // Otherwise, make this a use of a zext.
2567 return DAG.getSetCC(VT, ZextOp,
2568 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
2569 ExtDstTy),
2570 Cond);
Chris Lattner3391bcd2006-02-08 02:13:15 +00002571 } else if ((N1C->getValue() == 0 || N1C->getValue() == 1) &&
2572 (Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2573 (N0.getOpcode() == ISD::XOR ||
2574 (N0.getOpcode() == ISD::AND &&
2575 N0.getOperand(0).getOpcode() == ISD::XOR &&
2576 N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
2577 isa<ConstantSDNode>(N0.getOperand(1)) &&
2578 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == 1) {
2579 // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We can
2580 // only do this if the top bits are known zero.
2581 if (TLI.MaskedValueIsZero(N1,
2582 MVT::getIntVTBitMask(N0.getValueType())-1)) {
2583 // Okay, get the un-inverted input value.
2584 SDOperand Val;
2585 if (N0.getOpcode() == ISD::XOR)
2586 Val = N0.getOperand(0);
2587 else {
2588 assert(N0.getOpcode() == ISD::AND &&
2589 N0.getOperand(0).getOpcode() == ISD::XOR);
2590 // ((X^1)&1)^1 -> X & 1
2591 Val = DAG.getNode(ISD::AND, N0.getValueType(),
2592 N0.getOperand(0).getOperand(0), N0.getOperand(1));
2593 }
2594 return DAG.getSetCC(VT, Val, N1,
2595 Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
2596 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002597 }
Chris Lattner5c46f742005-10-05 06:11:08 +00002598
Nate Begeman452d7be2005-09-16 00:54:12 +00002599 uint64_t MinVal, MaxVal;
2600 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
2601 if (ISD::isSignedIntSetCC(Cond)) {
2602 MinVal = 1ULL << (OperandBitSize-1);
2603 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
2604 MaxVal = ~0ULL >> (65-OperandBitSize);
2605 else
2606 MaxVal = 0;
2607 } else {
2608 MinVal = 0;
2609 MaxVal = ~0ULL >> (64-OperandBitSize);
2610 }
2611
2612 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
2613 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
2614 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
2615 --C1; // X >= C0 --> X > (C0-1)
2616 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2617 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
2618 }
2619
2620 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
2621 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
2622 ++C1; // X <= C0 --> X < (C0+1)
2623 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
2624 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
2625 }
2626
2627 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
2628 return DAG.getConstant(0, VT); // X < MIN --> false
2629
2630 // Canonicalize setgt X, Min --> setne X, Min
2631 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
2632 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Chris Lattnerc8597ca2005-10-21 21:23:25 +00002633 // Canonicalize setlt X, Max --> setne X, Max
2634 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
2635 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
Nate Begeman452d7be2005-09-16 00:54:12 +00002636
2637 // If we have setult X, 1, turn it into seteq X, 0
2638 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
2639 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
2640 ISD::SETEQ);
2641 // If we have setugt X, Max-1, turn it into seteq X, Max
2642 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
2643 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
2644 ISD::SETEQ);
2645
2646 // If we have "setcc X, C0", check to see if we can shrink the immediate
2647 // by changing cc.
2648
2649 // SETUGT X, SINTMAX -> SETLT X, 0
2650 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
2651 C1 == (~0ULL >> (65-OperandBitSize)))
2652 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
2653 ISD::SETLT);
2654
2655 // FIXME: Implement the rest of these.
2656
2657 // Fold bit comparisons when we can.
2658 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2659 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
2660 if (ConstantSDNode *AndRHS =
2661 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2662 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
2663 // Perform the xform if the AND RHS is a single bit.
2664 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
2665 return DAG.getNode(ISD::SRL, VT, N0,
2666 DAG.getConstant(Log2_64(AndRHS->getValue()),
2667 TLI.getShiftAmountTy()));
2668 }
2669 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
2670 // (X & 8) == 8 --> (X & 8) >> 3
2671 // Perform the xform if C1 is a single bit.
2672 if ((C1 & (C1-1)) == 0) {
2673 return DAG.getNode(ISD::SRL, VT, N0,
2674 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
2675 }
2676 }
2677 }
2678 }
2679 } else if (isa<ConstantSDNode>(N0.Val)) {
2680 // Ensure that the constant occurs on the RHS.
2681 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2682 }
2683
2684 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
2685 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
2686 double C0 = N0C->getValue(), C1 = N1C->getValue();
2687
2688 switch (Cond) {
2689 default: break; // FIXME: Implement the rest of these!
2690 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
2691 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
2692 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
2693 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
2694 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
2695 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
2696 }
2697 } else {
2698 // Ensure that the constant occurs on the RHS.
2699 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
2700 }
2701
2702 if (N0 == N1) {
2703 // We can always fold X == Y for integer setcc's.
2704 if (MVT::isInteger(N0.getValueType()))
2705 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2706 unsigned UOF = ISD::getUnorderedFlavor(Cond);
2707 if (UOF == 2) // FP operators that are undefined on NaNs.
2708 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2709 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2710 return DAG.getConstant(UOF, VT);
2711 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2712 // if it is not already.
Chris Lattner4090aee2006-01-18 19:13:41 +00002713 ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
Nate Begeman452d7be2005-09-16 00:54:12 +00002714 if (NewCond != Cond)
2715 return DAG.getSetCC(VT, N0, N1, NewCond);
2716 }
2717
2718 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2719 MVT::isInteger(N0.getValueType())) {
2720 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2721 N0.getOpcode() == ISD::XOR) {
2722 // Simplify (X+Y) == (X+Z) --> Y == Z
2723 if (N0.getOpcode() == N1.getOpcode()) {
2724 if (N0.getOperand(0) == N1.getOperand(0))
2725 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2726 if (N0.getOperand(1) == N1.getOperand(1))
2727 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2728 if (isCommutativeBinOp(N0.getOpcode())) {
2729 // If X op Y == Y op X, try other combinations.
2730 if (N0.getOperand(0) == N1.getOperand(1))
2731 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2732 if (N0.getOperand(1) == N1.getOperand(0))
Chris Lattnera158eee2005-10-25 18:57:30 +00002733 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(1), Cond);
Nate Begeman452d7be2005-09-16 00:54:12 +00002734 }
2735 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002736
2737 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2738 if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2739 // Turn (X+C1) == C2 --> X == C2-C1
2740 if (N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse()) {
2741 return DAG.getSetCC(VT, N0.getOperand(0),
2742 DAG.getConstant(RHSC->getValue()-LHSR->getValue(),
2743 N0.getValueType()), Cond);
2744 }
2745
2746 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
2747 if (N0.getOpcode() == ISD::XOR)
Chris Lattner5c46f742005-10-05 06:11:08 +00002748 // If we know that all of the inverted bits are zero, don't bother
2749 // performing the inversion.
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002750 if (TLI.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getValue()))
Chris Lattner5c46f742005-10-05 06:11:08 +00002751 return DAG.getSetCC(VT, N0.getOperand(0),
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002752 DAG.getConstant(LHSR->getValue()^RHSC->getValue(),
Chris Lattner5c46f742005-10-05 06:11:08 +00002753 N0.getValueType()), Cond);
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002754 }
2755
2756 // Turn (C1-X) == C2 --> X == C1-C2
2757 if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
2758 if (N0.getOpcode() == ISD::SUB && N0.Val->hasOneUse()) {
2759 return DAG.getSetCC(VT, N0.getOperand(1),
2760 DAG.getConstant(SUBC->getValue()-RHSC->getValue(),
2761 N0.getValueType()), Cond);
Chris Lattner5c46f742005-10-05 06:11:08 +00002762 }
Chris Lattnerb3ddfc42006-02-02 06:36:13 +00002763 }
2764 }
2765
Nate Begeman452d7be2005-09-16 00:54:12 +00002766 // Simplify (X+Z) == X --> Z == 0
2767 if (N0.getOperand(0) == N1)
2768 return DAG.getSetCC(VT, N0.getOperand(1),
2769 DAG.getConstant(0, N0.getValueType()), Cond);
2770 if (N0.getOperand(1) == N1) {
2771 if (isCommutativeBinOp(N0.getOpcode()))
2772 return DAG.getSetCC(VT, N0.getOperand(0),
2773 DAG.getConstant(0, N0.getValueType()), Cond);
2774 else {
2775 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2776 // (Z-X) == X --> Z == X<<1
2777 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2778 N1,
2779 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002780 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002781 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2782 }
2783 }
2784 }
2785
2786 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2787 N1.getOpcode() == ISD::XOR) {
2788 // Simplify X == (X+Z) --> Z == 0
2789 if (N1.getOperand(0) == N0) {
2790 return DAG.getSetCC(VT, N1.getOperand(1),
2791 DAG.getConstant(0, N1.getValueType()), Cond);
2792 } else if (N1.getOperand(1) == N0) {
2793 if (isCommutativeBinOp(N1.getOpcode())) {
2794 return DAG.getSetCC(VT, N1.getOperand(0),
2795 DAG.getConstant(0, N1.getValueType()), Cond);
2796 } else {
2797 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2798 // X == (Z-X) --> X<<1 == Z
2799 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2800 DAG.getConstant(1,TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002801 AddToWorkList(SH.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002802 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2803 }
2804 }
2805 }
2806 }
2807
2808 // Fold away ALL boolean setcc's.
2809 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002810 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002811 switch (Cond) {
2812 default: assert(0 && "Unknown integer setcc!");
2813 case ISD::SETEQ: // X == Y -> (X^Y)^1
2814 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2815 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
Chris Lattner5750df92006-03-01 04:03:14 +00002816 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002817 break;
2818 case ISD::SETNE: // X != Y --> (X^Y)
2819 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2820 break;
2821 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2822 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2823 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2824 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00002825 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002826 break;
2827 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2828 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2829 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2830 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00002831 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002832 break;
2833 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2834 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2835 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2836 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
Chris Lattner5750df92006-03-01 04:03:14 +00002837 AddToWorkList(Temp.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002838 break;
2839 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2840 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2841 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2842 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2843 break;
2844 }
2845 if (VT != MVT::i1) {
Chris Lattner5750df92006-03-01 04:03:14 +00002846 AddToWorkList(N0.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002847 // FIXME: If running after legalize, we probably can't do this.
2848 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2849 }
2850 return N0;
2851 }
2852
2853 // Could not fold it.
2854 return SDOperand();
2855}
2856
Nate Begeman69575232005-10-20 02:15:44 +00002857/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
2858/// return a DAG expression to select that will generate the same value by
2859/// multiplying by a magic number. See:
2860/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2861SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
2862 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002863
2864 // Check to see if we can do this.
2865 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2866 return SDOperand(); // BuildSDIV only operates on i32 or i64
2867 if (!TLI.isOperationLegal(ISD::MULHS, VT))
2868 return SDOperand(); // Make sure the target supports MULHS.
Nate Begeman69575232005-10-20 02:15:44 +00002869
Nate Begemanc6a454e2005-10-20 17:45:03 +00002870 int64_t d = cast<ConstantSDNode>(N->getOperand(1))->getSignExtended();
Nate Begeman69575232005-10-20 02:15:44 +00002871 ms magics = (VT == MVT::i32) ? magic32(d) : magic64(d);
2872
2873 // Multiply the numerator (operand 0) by the magic value
2874 SDOperand Q = DAG.getNode(ISD::MULHS, VT, N->getOperand(0),
2875 DAG.getConstant(magics.m, VT));
2876 // If d > 0 and m < 0, add the numerator
2877 if (d > 0 && magics.m < 0) {
2878 Q = DAG.getNode(ISD::ADD, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00002879 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002880 }
2881 // If d < 0 and m > 0, subtract the numerator.
2882 if (d < 0 && magics.m > 0) {
2883 Q = DAG.getNode(ISD::SUB, VT, Q, N->getOperand(0));
Chris Lattner5750df92006-03-01 04:03:14 +00002884 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002885 }
2886 // Shift right algebraic if shift value is nonzero
2887 if (magics.s > 0) {
2888 Q = DAG.getNode(ISD::SRA, VT, Q,
2889 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002890 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002891 }
2892 // Extract the sign bit and add it to the quotient
2893 SDOperand T =
Nate Begeman4d385672005-10-21 01:51:45 +00002894 DAG.getNode(ISD::SRL, VT, Q, DAG.getConstant(MVT::getSizeInBits(VT)-1,
2895 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002896 AddToWorkList(T.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002897 return DAG.getNode(ISD::ADD, VT, Q, T);
2898}
2899
2900/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
2901/// return a DAG expression to select that will generate the same value by
2902/// multiplying by a magic number. See:
2903/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
2904SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
2905 MVT::ValueType VT = N->getValueType(0);
Chris Lattnere9936d12005-10-22 18:50:15 +00002906
2907 // Check to see if we can do this.
2908 if (!TLI.isTypeLegal(VT) || (VT != MVT::i32 && VT != MVT::i64))
2909 return SDOperand(); // BuildUDIV only operates on i32 or i64
2910 if (!TLI.isOperationLegal(ISD::MULHU, VT))
2911 return SDOperand(); // Make sure the target supports MULHU.
Nate Begeman69575232005-10-20 02:15:44 +00002912
2913 uint64_t d = cast<ConstantSDNode>(N->getOperand(1))->getValue();
2914 mu magics = (VT == MVT::i32) ? magicu32(d) : magicu64(d);
2915
2916 // Multiply the numerator (operand 0) by the magic value
2917 SDOperand Q = DAG.getNode(ISD::MULHU, VT, N->getOperand(0),
2918 DAG.getConstant(magics.m, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002919 AddToWorkList(Q.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002920
2921 if (magics.a == 0) {
2922 return DAG.getNode(ISD::SRL, VT, Q,
2923 DAG.getConstant(magics.s, TLI.getShiftAmountTy()));
2924 } else {
2925 SDOperand NPQ = DAG.getNode(ISD::SUB, VT, N->getOperand(0), Q);
Chris Lattner5750df92006-03-01 04:03:14 +00002926 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002927 NPQ = DAG.getNode(ISD::SRL, VT, NPQ,
2928 DAG.getConstant(1, TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00002929 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002930 NPQ = DAG.getNode(ISD::ADD, VT, NPQ, Q);
Chris Lattner5750df92006-03-01 04:03:14 +00002931 AddToWorkList(NPQ.Val);
Nate Begeman69575232005-10-20 02:15:44 +00002932 return DAG.getNode(ISD::SRL, VT, NPQ,
2933 DAG.getConstant(magics.s-1, TLI.getShiftAmountTy()));
2934 }
2935}
2936
Nate Begeman1d4d4142005-09-01 00:19:25 +00002937// SelectionDAG::Combine - This is the entry point for the file.
2938//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002939void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002940 /// run - This is the main entry point to this class.
2941 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002942 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002943}